xref: /linux/drivers/net/netdevsim/netdev.c (revision 06ed6aa56ffac9241e03a24649e8d048f8f1b10c)
1 /*
2  * Copyright (C) 2017 Netronome Systems, Inc.
3  *
4  * This software is licensed under the GNU General License Version 2,
5  * June 1991 as shown in the file COPYING in the top-level directory of this
6  * source tree.
7  *
8  * THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS"
9  * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
10  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
11  * FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE
12  * OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME
13  * THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
14  */
15 
16 #include <linux/debugfs.h>
17 #include <linux/etherdevice.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/netdevice.h>
21 #include <linux/slab.h>
22 #include <net/netlink.h>
23 #include <net/pkt_cls.h>
24 #include <net/rtnetlink.h>
25 
26 #include "netdevsim.h"
27 
28 static netdev_tx_t nsim_start_xmit(struct sk_buff *skb, struct net_device *dev)
29 {
30 	struct netdevsim *ns = netdev_priv(dev);
31 
32 	if (!nsim_ipsec_tx(ns, skb))
33 		goto out;
34 
35 	u64_stats_update_begin(&ns->syncp);
36 	ns->tx_packets++;
37 	ns->tx_bytes += skb->len;
38 	u64_stats_update_end(&ns->syncp);
39 
40 out:
41 	dev_kfree_skb(skb);
42 
43 	return NETDEV_TX_OK;
44 }
45 
46 static void nsim_set_rx_mode(struct net_device *dev)
47 {
48 }
49 
50 static int nsim_change_mtu(struct net_device *dev, int new_mtu)
51 {
52 	struct netdevsim *ns = netdev_priv(dev);
53 
54 	if (ns->xdp.prog && new_mtu > NSIM_XDP_MAX_MTU)
55 		return -EBUSY;
56 
57 	dev->mtu = new_mtu;
58 
59 	return 0;
60 }
61 
62 static void
63 nsim_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
64 {
65 	struct netdevsim *ns = netdev_priv(dev);
66 	unsigned int start;
67 
68 	do {
69 		start = u64_stats_fetch_begin(&ns->syncp);
70 		stats->tx_bytes = ns->tx_bytes;
71 		stats->tx_packets = ns->tx_packets;
72 	} while (u64_stats_fetch_retry(&ns->syncp, start));
73 }
74 
75 static int
76 nsim_setup_tc_block_cb(enum tc_setup_type type, void *type_data, void *cb_priv)
77 {
78 	return nsim_bpf_setup_tc_block_cb(type, type_data, cb_priv);
79 }
80 
81 static int nsim_set_vf_mac(struct net_device *dev, int vf, u8 *mac)
82 {
83 	struct netdevsim *ns = netdev_priv(dev);
84 	struct nsim_bus_dev *nsim_bus_dev = ns->nsim_bus_dev;
85 
86 	/* Only refuse multicast addresses, zero address can mean unset/any. */
87 	if (vf >= nsim_bus_dev->num_vfs || is_multicast_ether_addr(mac))
88 		return -EINVAL;
89 	memcpy(nsim_bus_dev->vfconfigs[vf].vf_mac, mac, ETH_ALEN);
90 
91 	return 0;
92 }
93 
94 static int nsim_set_vf_vlan(struct net_device *dev, int vf,
95 			    u16 vlan, u8 qos, __be16 vlan_proto)
96 {
97 	struct netdevsim *ns = netdev_priv(dev);
98 	struct nsim_bus_dev *nsim_bus_dev = ns->nsim_bus_dev;
99 
100 	if (vf >= nsim_bus_dev->num_vfs || vlan > 4095 || qos > 7)
101 		return -EINVAL;
102 
103 	nsim_bus_dev->vfconfigs[vf].vlan = vlan;
104 	nsim_bus_dev->vfconfigs[vf].qos = qos;
105 	nsim_bus_dev->vfconfigs[vf].vlan_proto = vlan_proto;
106 
107 	return 0;
108 }
109 
110 static int nsim_set_vf_rate(struct net_device *dev, int vf, int min, int max)
111 {
112 	struct netdevsim *ns = netdev_priv(dev);
113 	struct nsim_bus_dev *nsim_bus_dev = ns->nsim_bus_dev;
114 
115 	if (vf >= nsim_bus_dev->num_vfs)
116 		return -EINVAL;
117 
118 	nsim_bus_dev->vfconfigs[vf].min_tx_rate = min;
119 	nsim_bus_dev->vfconfigs[vf].max_tx_rate = max;
120 
121 	return 0;
122 }
123 
124 static int nsim_set_vf_spoofchk(struct net_device *dev, int vf, bool val)
125 {
126 	struct netdevsim *ns = netdev_priv(dev);
127 	struct nsim_bus_dev *nsim_bus_dev = ns->nsim_bus_dev;
128 
129 	if (vf >= nsim_bus_dev->num_vfs)
130 		return -EINVAL;
131 	nsim_bus_dev->vfconfigs[vf].spoofchk_enabled = val;
132 
133 	return 0;
134 }
135 
136 static int nsim_set_vf_rss_query_en(struct net_device *dev, int vf, bool val)
137 {
138 	struct netdevsim *ns = netdev_priv(dev);
139 	struct nsim_bus_dev *nsim_bus_dev = ns->nsim_bus_dev;
140 
141 	if (vf >= nsim_bus_dev->num_vfs)
142 		return -EINVAL;
143 	nsim_bus_dev->vfconfigs[vf].rss_query_enabled = val;
144 
145 	return 0;
146 }
147 
148 static int nsim_set_vf_trust(struct net_device *dev, int vf, bool val)
149 {
150 	struct netdevsim *ns = netdev_priv(dev);
151 	struct nsim_bus_dev *nsim_bus_dev = ns->nsim_bus_dev;
152 
153 	if (vf >= nsim_bus_dev->num_vfs)
154 		return -EINVAL;
155 	nsim_bus_dev->vfconfigs[vf].trusted = val;
156 
157 	return 0;
158 }
159 
160 static int
161 nsim_get_vf_config(struct net_device *dev, int vf, struct ifla_vf_info *ivi)
162 {
163 	struct netdevsim *ns = netdev_priv(dev);
164 	struct nsim_bus_dev *nsim_bus_dev = ns->nsim_bus_dev;
165 
166 	if (vf >= nsim_bus_dev->num_vfs)
167 		return -EINVAL;
168 
169 	ivi->vf = vf;
170 	ivi->linkstate = nsim_bus_dev->vfconfigs[vf].link_state;
171 	ivi->min_tx_rate = nsim_bus_dev->vfconfigs[vf].min_tx_rate;
172 	ivi->max_tx_rate = nsim_bus_dev->vfconfigs[vf].max_tx_rate;
173 	ivi->vlan = nsim_bus_dev->vfconfigs[vf].vlan;
174 	ivi->vlan_proto = nsim_bus_dev->vfconfigs[vf].vlan_proto;
175 	ivi->qos = nsim_bus_dev->vfconfigs[vf].qos;
176 	memcpy(&ivi->mac, nsim_bus_dev->vfconfigs[vf].vf_mac, ETH_ALEN);
177 	ivi->spoofchk = nsim_bus_dev->vfconfigs[vf].spoofchk_enabled;
178 	ivi->trusted = nsim_bus_dev->vfconfigs[vf].trusted;
179 	ivi->rss_query_en = nsim_bus_dev->vfconfigs[vf].rss_query_enabled;
180 
181 	return 0;
182 }
183 
184 static int nsim_set_vf_link_state(struct net_device *dev, int vf, int state)
185 {
186 	struct netdevsim *ns = netdev_priv(dev);
187 	struct nsim_bus_dev *nsim_bus_dev = ns->nsim_bus_dev;
188 
189 	if (vf >= nsim_bus_dev->num_vfs)
190 		return -EINVAL;
191 
192 	switch (state) {
193 	case IFLA_VF_LINK_STATE_AUTO:
194 	case IFLA_VF_LINK_STATE_ENABLE:
195 	case IFLA_VF_LINK_STATE_DISABLE:
196 		break;
197 	default:
198 		return -EINVAL;
199 	}
200 
201 	nsim_bus_dev->vfconfigs[vf].link_state = state;
202 
203 	return 0;
204 }
205 
206 static LIST_HEAD(nsim_block_cb_list);
207 
208 static int
209 nsim_setup_tc(struct net_device *dev, enum tc_setup_type type, void *type_data)
210 {
211 	struct netdevsim *ns = netdev_priv(dev);
212 
213 	switch (type) {
214 	case TC_SETUP_BLOCK:
215 		return flow_block_cb_setup_simple(type_data,
216 						  &nsim_block_cb_list,
217 						  nsim_setup_tc_block_cb,
218 						  ns, ns, true);
219 	default:
220 		return -EOPNOTSUPP;
221 	}
222 }
223 
224 static int
225 nsim_set_features(struct net_device *dev, netdev_features_t features)
226 {
227 	struct netdevsim *ns = netdev_priv(dev);
228 
229 	if ((dev->features & NETIF_F_HW_TC) > (features & NETIF_F_HW_TC))
230 		return nsim_bpf_disable_tc(ns);
231 
232 	return 0;
233 }
234 
235 static struct devlink_port *nsim_get_devlink_port(struct net_device *dev)
236 {
237 	struct netdevsim *ns = netdev_priv(dev);
238 
239 	return &ns->nsim_dev_port->devlink_port;
240 }
241 
242 static const struct net_device_ops nsim_netdev_ops = {
243 	.ndo_start_xmit		= nsim_start_xmit,
244 	.ndo_set_rx_mode	= nsim_set_rx_mode,
245 	.ndo_set_mac_address	= eth_mac_addr,
246 	.ndo_validate_addr	= eth_validate_addr,
247 	.ndo_change_mtu		= nsim_change_mtu,
248 	.ndo_get_stats64	= nsim_get_stats64,
249 	.ndo_set_vf_mac		= nsim_set_vf_mac,
250 	.ndo_set_vf_vlan	= nsim_set_vf_vlan,
251 	.ndo_set_vf_rate	= nsim_set_vf_rate,
252 	.ndo_set_vf_spoofchk	= nsim_set_vf_spoofchk,
253 	.ndo_set_vf_trust	= nsim_set_vf_trust,
254 	.ndo_get_vf_config	= nsim_get_vf_config,
255 	.ndo_set_vf_link_state	= nsim_set_vf_link_state,
256 	.ndo_set_vf_rss_query_en = nsim_set_vf_rss_query_en,
257 	.ndo_setup_tc		= nsim_setup_tc,
258 	.ndo_set_features	= nsim_set_features,
259 	.ndo_bpf		= nsim_bpf,
260 	.ndo_get_devlink_port	= nsim_get_devlink_port,
261 };
262 
263 static void nsim_setup(struct net_device *dev)
264 {
265 	ether_setup(dev);
266 	eth_hw_addr_random(dev);
267 
268 	dev->tx_queue_len = 0;
269 	dev->flags |= IFF_NOARP;
270 	dev->flags &= ~IFF_MULTICAST;
271 	dev->priv_flags |= IFF_LIVE_ADDR_CHANGE |
272 			   IFF_NO_QUEUE;
273 	dev->features |= NETIF_F_HIGHDMA |
274 			 NETIF_F_SG |
275 			 NETIF_F_FRAGLIST |
276 			 NETIF_F_HW_CSUM |
277 			 NETIF_F_TSO;
278 	dev->hw_features |= NETIF_F_HW_TC;
279 	dev->max_mtu = ETH_MAX_MTU;
280 }
281 
282 struct netdevsim *
283 nsim_create(struct nsim_dev *nsim_dev, struct nsim_dev_port *nsim_dev_port)
284 {
285 	struct net_device *dev;
286 	struct netdevsim *ns;
287 	int err;
288 
289 	dev = alloc_netdev(sizeof(*ns), "eth%d", NET_NAME_UNKNOWN, nsim_setup);
290 	if (!dev)
291 		return ERR_PTR(-ENOMEM);
292 
293 	dev_net_set(dev, nsim_dev_net(nsim_dev));
294 	ns = netdev_priv(dev);
295 	ns->netdev = dev;
296 	ns->nsim_dev = nsim_dev;
297 	ns->nsim_dev_port = nsim_dev_port;
298 	ns->nsim_bus_dev = nsim_dev->nsim_bus_dev;
299 	SET_NETDEV_DEV(dev, &ns->nsim_bus_dev->dev);
300 	dev->netdev_ops = &nsim_netdev_ops;
301 
302 	rtnl_lock();
303 	err = nsim_bpf_init(ns);
304 	if (err)
305 		goto err_free_netdev;
306 
307 	nsim_ipsec_init(ns);
308 
309 	err = register_netdevice(dev);
310 	if (err)
311 		goto err_ipsec_teardown;
312 	rtnl_unlock();
313 
314 	return ns;
315 
316 err_ipsec_teardown:
317 	nsim_ipsec_teardown(ns);
318 	nsim_bpf_uninit(ns);
319 	rtnl_unlock();
320 err_free_netdev:
321 	free_netdev(dev);
322 	return ERR_PTR(err);
323 }
324 
325 void nsim_destroy(struct netdevsim *ns)
326 {
327 	struct net_device *dev = ns->netdev;
328 
329 	rtnl_lock();
330 	unregister_netdevice(dev);
331 	nsim_ipsec_teardown(ns);
332 	nsim_bpf_uninit(ns);
333 	rtnl_unlock();
334 	free_netdev(dev);
335 }
336 
337 static int nsim_validate(struct nlattr *tb[], struct nlattr *data[],
338 			 struct netlink_ext_ack *extack)
339 {
340 	NL_SET_ERR_MSG_MOD(extack, "Please use: echo \"[ID] [PORT_COUNT]\" > /sys/bus/netdevsim/new_device");
341 	return -EOPNOTSUPP;
342 }
343 
344 static struct rtnl_link_ops nsim_link_ops __read_mostly = {
345 	.kind		= DRV_NAME,
346 	.validate	= nsim_validate,
347 };
348 
349 static int __init nsim_module_init(void)
350 {
351 	int err;
352 
353 	err = nsim_dev_init();
354 	if (err)
355 		return err;
356 
357 	err = nsim_bus_init();
358 	if (err)
359 		goto err_dev_exit;
360 
361 	err = rtnl_link_register(&nsim_link_ops);
362 	if (err)
363 		goto err_bus_exit;
364 
365 	return 0;
366 
367 err_bus_exit:
368 	nsim_bus_exit();
369 err_dev_exit:
370 	nsim_dev_exit();
371 	return err;
372 }
373 
374 static void __exit nsim_module_exit(void)
375 {
376 	rtnl_link_unregister(&nsim_link_ops);
377 	nsim_bus_exit();
378 	nsim_dev_exit();
379 }
380 
381 module_init(nsim_module_init);
382 module_exit(nsim_module_exit);
383 MODULE_LICENSE("GPL");
384 MODULE_ALIAS_RTNL_LINK(DRV_NAME);
385