xref: /linux/drivers/net/netdevsim/netdev.c (revision 48ba00da2eb4b54a7e6ed2ca3a9f2e575dff48c9)
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/netdev_queues.h>
23 #include <net/netlink.h>
24 #include <net/pkt_cls.h>
25 #include <net/rtnetlink.h>
26 #include <net/udp_tunnel.h>
27 
28 #include "netdevsim.h"
29 
30 static netdev_tx_t nsim_start_xmit(struct sk_buff *skb, struct net_device *dev)
31 {
32 	struct netdevsim *ns = netdev_priv(dev);
33 	unsigned int len = skb->len;
34 	struct netdevsim *peer_ns;
35 
36 	rcu_read_lock();
37 	if (!nsim_ipsec_tx(ns, skb))
38 		goto out_drop_free;
39 
40 	peer_ns = rcu_dereference(ns->peer);
41 	if (!peer_ns)
42 		goto out_drop_free;
43 
44 	skb_tx_timestamp(skb);
45 	if (unlikely(dev_forward_skb(peer_ns->netdev, skb) == NET_RX_DROP))
46 		goto out_drop_cnt;
47 
48 	rcu_read_unlock();
49 	u64_stats_update_begin(&ns->syncp);
50 	ns->tx_packets++;
51 	ns->tx_bytes += len;
52 	u64_stats_update_end(&ns->syncp);
53 	return NETDEV_TX_OK;
54 
55 out_drop_free:
56 	dev_kfree_skb(skb);
57 out_drop_cnt:
58 	rcu_read_unlock();
59 	u64_stats_update_begin(&ns->syncp);
60 	ns->tx_dropped++;
61 	u64_stats_update_end(&ns->syncp);
62 	return NETDEV_TX_OK;
63 }
64 
65 static void nsim_set_rx_mode(struct net_device *dev)
66 {
67 }
68 
69 static int nsim_change_mtu(struct net_device *dev, int new_mtu)
70 {
71 	struct netdevsim *ns = netdev_priv(dev);
72 
73 	if (ns->xdp.prog && new_mtu > NSIM_XDP_MAX_MTU)
74 		return -EBUSY;
75 
76 	dev->mtu = new_mtu;
77 
78 	return 0;
79 }
80 
81 static void
82 nsim_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
83 {
84 	struct netdevsim *ns = netdev_priv(dev);
85 	unsigned int start;
86 
87 	do {
88 		start = u64_stats_fetch_begin(&ns->syncp);
89 		stats->tx_bytes = ns->tx_bytes;
90 		stats->tx_packets = ns->tx_packets;
91 		stats->tx_dropped = ns->tx_dropped;
92 	} while (u64_stats_fetch_retry(&ns->syncp, start));
93 }
94 
95 static int
96 nsim_setup_tc_block_cb(enum tc_setup_type type, void *type_data, void *cb_priv)
97 {
98 	return nsim_bpf_setup_tc_block_cb(type, type_data, cb_priv);
99 }
100 
101 static int nsim_set_vf_mac(struct net_device *dev, int vf, u8 *mac)
102 {
103 	struct netdevsim *ns = netdev_priv(dev);
104 	struct nsim_dev *nsim_dev = ns->nsim_dev;
105 
106 	/* Only refuse multicast addresses, zero address can mean unset/any. */
107 	if (vf >= nsim_dev_get_vfs(nsim_dev) || is_multicast_ether_addr(mac))
108 		return -EINVAL;
109 	memcpy(nsim_dev->vfconfigs[vf].vf_mac, mac, ETH_ALEN);
110 
111 	return 0;
112 }
113 
114 static int nsim_set_vf_vlan(struct net_device *dev, int vf,
115 			    u16 vlan, u8 qos, __be16 vlan_proto)
116 {
117 	struct netdevsim *ns = netdev_priv(dev);
118 	struct nsim_dev *nsim_dev = ns->nsim_dev;
119 
120 	if (vf >= nsim_dev_get_vfs(nsim_dev) || vlan > 4095 || qos > 7)
121 		return -EINVAL;
122 
123 	nsim_dev->vfconfigs[vf].vlan = vlan;
124 	nsim_dev->vfconfigs[vf].qos = qos;
125 	nsim_dev->vfconfigs[vf].vlan_proto = vlan_proto;
126 
127 	return 0;
128 }
129 
130 static int nsim_set_vf_rate(struct net_device *dev, int vf, int min, int max)
131 {
132 	struct netdevsim *ns = netdev_priv(dev);
133 	struct nsim_dev *nsim_dev = ns->nsim_dev;
134 
135 	if (nsim_esw_mode_is_switchdev(ns->nsim_dev)) {
136 		pr_err("Not supported in switchdev mode. Please use devlink API.\n");
137 		return -EOPNOTSUPP;
138 	}
139 
140 	if (vf >= nsim_dev_get_vfs(nsim_dev))
141 		return -EINVAL;
142 
143 	nsim_dev->vfconfigs[vf].min_tx_rate = min;
144 	nsim_dev->vfconfigs[vf].max_tx_rate = max;
145 
146 	return 0;
147 }
148 
149 static int nsim_set_vf_spoofchk(struct net_device *dev, int vf, bool val)
150 {
151 	struct netdevsim *ns = netdev_priv(dev);
152 	struct nsim_dev *nsim_dev = ns->nsim_dev;
153 
154 	if (vf >= nsim_dev_get_vfs(nsim_dev))
155 		return -EINVAL;
156 	nsim_dev->vfconfigs[vf].spoofchk_enabled = val;
157 
158 	return 0;
159 }
160 
161 static int nsim_set_vf_rss_query_en(struct net_device *dev, int vf, bool val)
162 {
163 	struct netdevsim *ns = netdev_priv(dev);
164 	struct nsim_dev *nsim_dev = ns->nsim_dev;
165 
166 	if (vf >= nsim_dev_get_vfs(nsim_dev))
167 		return -EINVAL;
168 	nsim_dev->vfconfigs[vf].rss_query_enabled = val;
169 
170 	return 0;
171 }
172 
173 static int nsim_set_vf_trust(struct net_device *dev, int vf, bool val)
174 {
175 	struct netdevsim *ns = netdev_priv(dev);
176 	struct nsim_dev *nsim_dev = ns->nsim_dev;
177 
178 	if (vf >= nsim_dev_get_vfs(nsim_dev))
179 		return -EINVAL;
180 	nsim_dev->vfconfigs[vf].trusted = val;
181 
182 	return 0;
183 }
184 
185 static int
186 nsim_get_vf_config(struct net_device *dev, int vf, struct ifla_vf_info *ivi)
187 {
188 	struct netdevsim *ns = netdev_priv(dev);
189 	struct nsim_dev *nsim_dev = ns->nsim_dev;
190 
191 	if (vf >= nsim_dev_get_vfs(nsim_dev))
192 		return -EINVAL;
193 
194 	ivi->vf = vf;
195 	ivi->linkstate = nsim_dev->vfconfigs[vf].link_state;
196 	ivi->min_tx_rate = nsim_dev->vfconfigs[vf].min_tx_rate;
197 	ivi->max_tx_rate = nsim_dev->vfconfigs[vf].max_tx_rate;
198 	ivi->vlan = nsim_dev->vfconfigs[vf].vlan;
199 	ivi->vlan_proto = nsim_dev->vfconfigs[vf].vlan_proto;
200 	ivi->qos = nsim_dev->vfconfigs[vf].qos;
201 	memcpy(&ivi->mac, nsim_dev->vfconfigs[vf].vf_mac, ETH_ALEN);
202 	ivi->spoofchk = nsim_dev->vfconfigs[vf].spoofchk_enabled;
203 	ivi->trusted = nsim_dev->vfconfigs[vf].trusted;
204 	ivi->rss_query_en = nsim_dev->vfconfigs[vf].rss_query_enabled;
205 
206 	return 0;
207 }
208 
209 static int nsim_set_vf_link_state(struct net_device *dev, int vf, int state)
210 {
211 	struct netdevsim *ns = netdev_priv(dev);
212 	struct nsim_dev *nsim_dev = ns->nsim_dev;
213 
214 	if (vf >= nsim_dev_get_vfs(nsim_dev))
215 		return -EINVAL;
216 
217 	switch (state) {
218 	case IFLA_VF_LINK_STATE_AUTO:
219 	case IFLA_VF_LINK_STATE_ENABLE:
220 	case IFLA_VF_LINK_STATE_DISABLE:
221 		break;
222 	default:
223 		return -EINVAL;
224 	}
225 
226 	nsim_dev->vfconfigs[vf].link_state = state;
227 
228 	return 0;
229 }
230 
231 static void nsim_taprio_stats(struct tc_taprio_qopt_stats *stats)
232 {
233 	stats->window_drops = 0;
234 	stats->tx_overruns = 0;
235 }
236 
237 static int nsim_setup_tc_taprio(struct net_device *dev,
238 				struct tc_taprio_qopt_offload *offload)
239 {
240 	int err = 0;
241 
242 	switch (offload->cmd) {
243 	case TAPRIO_CMD_REPLACE:
244 	case TAPRIO_CMD_DESTROY:
245 		break;
246 	case TAPRIO_CMD_STATS:
247 		nsim_taprio_stats(&offload->stats);
248 		break;
249 	default:
250 		err = -EOPNOTSUPP;
251 	}
252 
253 	return err;
254 }
255 
256 static LIST_HEAD(nsim_block_cb_list);
257 
258 static int
259 nsim_setup_tc(struct net_device *dev, enum tc_setup_type type, void *type_data)
260 {
261 	struct netdevsim *ns = netdev_priv(dev);
262 
263 	switch (type) {
264 	case TC_SETUP_QDISC_TAPRIO:
265 		return nsim_setup_tc_taprio(dev, type_data);
266 	case TC_SETUP_BLOCK:
267 		return flow_block_cb_setup_simple(type_data,
268 						  &nsim_block_cb_list,
269 						  nsim_setup_tc_block_cb,
270 						  ns, ns, true);
271 	default:
272 		return -EOPNOTSUPP;
273 	}
274 }
275 
276 static int
277 nsim_set_features(struct net_device *dev, netdev_features_t features)
278 {
279 	struct netdevsim *ns = netdev_priv(dev);
280 
281 	if ((dev->features & NETIF_F_HW_TC) > (features & NETIF_F_HW_TC))
282 		return nsim_bpf_disable_tc(ns);
283 
284 	return 0;
285 }
286 
287 static int nsim_get_iflink(const struct net_device *dev)
288 {
289 	struct netdevsim *nsim, *peer;
290 	int iflink;
291 
292 	nsim = netdev_priv(dev);
293 
294 	rcu_read_lock();
295 	peer = rcu_dereference(nsim->peer);
296 	iflink = peer ? READ_ONCE(peer->netdev->ifindex) : 0;
297 	rcu_read_unlock();
298 
299 	return iflink;
300 }
301 
302 static const struct net_device_ops nsim_netdev_ops = {
303 	.ndo_start_xmit		= nsim_start_xmit,
304 	.ndo_set_rx_mode	= nsim_set_rx_mode,
305 	.ndo_set_mac_address	= eth_mac_addr,
306 	.ndo_validate_addr	= eth_validate_addr,
307 	.ndo_change_mtu		= nsim_change_mtu,
308 	.ndo_get_stats64	= nsim_get_stats64,
309 	.ndo_set_vf_mac		= nsim_set_vf_mac,
310 	.ndo_set_vf_vlan	= nsim_set_vf_vlan,
311 	.ndo_set_vf_rate	= nsim_set_vf_rate,
312 	.ndo_set_vf_spoofchk	= nsim_set_vf_spoofchk,
313 	.ndo_set_vf_trust	= nsim_set_vf_trust,
314 	.ndo_get_vf_config	= nsim_get_vf_config,
315 	.ndo_set_vf_link_state	= nsim_set_vf_link_state,
316 	.ndo_set_vf_rss_query_en = nsim_set_vf_rss_query_en,
317 	.ndo_setup_tc		= nsim_setup_tc,
318 	.ndo_set_features	= nsim_set_features,
319 	.ndo_get_iflink		= nsim_get_iflink,
320 	.ndo_bpf		= nsim_bpf,
321 };
322 
323 static const struct net_device_ops nsim_vf_netdev_ops = {
324 	.ndo_start_xmit		= nsim_start_xmit,
325 	.ndo_set_rx_mode	= nsim_set_rx_mode,
326 	.ndo_set_mac_address	= eth_mac_addr,
327 	.ndo_validate_addr	= eth_validate_addr,
328 	.ndo_change_mtu		= nsim_change_mtu,
329 	.ndo_get_stats64	= nsim_get_stats64,
330 	.ndo_setup_tc		= nsim_setup_tc,
331 	.ndo_set_features	= nsim_set_features,
332 };
333 
334 /* We don't have true per-queue stats, yet, so do some random fakery here.
335  * Only report stuff for queue 0.
336  */
337 static void nsim_get_queue_stats_rx(struct net_device *dev, int idx,
338 				    struct netdev_queue_stats_rx *stats)
339 {
340 	struct rtnl_link_stats64 rtstats = {};
341 
342 	if (!idx)
343 		nsim_get_stats64(dev, &rtstats);
344 
345 	stats->packets = rtstats.rx_packets - !!rtstats.rx_packets;
346 	stats->bytes = rtstats.rx_bytes;
347 }
348 
349 static void nsim_get_queue_stats_tx(struct net_device *dev, int idx,
350 				    struct netdev_queue_stats_tx *stats)
351 {
352 	struct rtnl_link_stats64 rtstats = {};
353 
354 	if (!idx)
355 		nsim_get_stats64(dev, &rtstats);
356 
357 	stats->packets = rtstats.tx_packets - !!rtstats.tx_packets;
358 	stats->bytes = rtstats.tx_bytes;
359 }
360 
361 static void nsim_get_base_stats(struct net_device *dev,
362 				struct netdev_queue_stats_rx *rx,
363 				struct netdev_queue_stats_tx *tx)
364 {
365 	struct rtnl_link_stats64 rtstats = {};
366 
367 	nsim_get_stats64(dev, &rtstats);
368 
369 	rx->packets = !!rtstats.rx_packets;
370 	rx->bytes = 0;
371 	tx->packets = !!rtstats.tx_packets;
372 	tx->bytes = 0;
373 }
374 
375 static const struct netdev_stat_ops nsim_stat_ops = {
376 	.get_queue_stats_tx	= nsim_get_queue_stats_tx,
377 	.get_queue_stats_rx	= nsim_get_queue_stats_rx,
378 	.get_base_stats		= nsim_get_base_stats,
379 };
380 
381 static void nsim_setup(struct net_device *dev)
382 {
383 	ether_setup(dev);
384 	eth_hw_addr_random(dev);
385 
386 	dev->tx_queue_len = 0;
387 	dev->flags &= ~IFF_MULTICAST;
388 	dev->priv_flags |= IFF_LIVE_ADDR_CHANGE |
389 			   IFF_NO_QUEUE;
390 	dev->features |= NETIF_F_HIGHDMA |
391 			 NETIF_F_SG |
392 			 NETIF_F_FRAGLIST |
393 			 NETIF_F_HW_CSUM |
394 			 NETIF_F_TSO;
395 	dev->hw_features |= NETIF_F_HW_TC;
396 	dev->max_mtu = ETH_MAX_MTU;
397 	dev->xdp_features = NETDEV_XDP_ACT_HW_OFFLOAD;
398 }
399 
400 static int nsim_init_netdevsim(struct netdevsim *ns)
401 {
402 	struct mock_phc *phc;
403 	int err;
404 
405 	phc = mock_phc_create(&ns->nsim_bus_dev->dev);
406 	if (IS_ERR(phc))
407 		return PTR_ERR(phc);
408 
409 	ns->phc = phc;
410 	ns->netdev->netdev_ops = &nsim_netdev_ops;
411 	ns->netdev->stat_ops = &nsim_stat_ops;
412 
413 	err = nsim_udp_tunnels_info_create(ns->nsim_dev, ns->netdev);
414 	if (err)
415 		goto err_phc_destroy;
416 
417 	rtnl_lock();
418 	err = nsim_bpf_init(ns);
419 	if (err)
420 		goto err_utn_destroy;
421 
422 	nsim_macsec_init(ns);
423 	nsim_ipsec_init(ns);
424 
425 	err = register_netdevice(ns->netdev);
426 	if (err)
427 		goto err_ipsec_teardown;
428 	rtnl_unlock();
429 	return 0;
430 
431 err_ipsec_teardown:
432 	nsim_ipsec_teardown(ns);
433 	nsim_macsec_teardown(ns);
434 	nsim_bpf_uninit(ns);
435 err_utn_destroy:
436 	rtnl_unlock();
437 	nsim_udp_tunnels_info_destroy(ns->netdev);
438 err_phc_destroy:
439 	mock_phc_destroy(ns->phc);
440 	return err;
441 }
442 
443 static int nsim_init_netdevsim_vf(struct netdevsim *ns)
444 {
445 	int err;
446 
447 	ns->netdev->netdev_ops = &nsim_vf_netdev_ops;
448 	rtnl_lock();
449 	err = register_netdevice(ns->netdev);
450 	rtnl_unlock();
451 	return err;
452 }
453 
454 static void nsim_exit_netdevsim(struct netdevsim *ns)
455 {
456 	nsim_udp_tunnels_info_destroy(ns->netdev);
457 	mock_phc_destroy(ns->phc);
458 }
459 
460 struct netdevsim *
461 nsim_create(struct nsim_dev *nsim_dev, struct nsim_dev_port *nsim_dev_port)
462 {
463 	struct net_device *dev;
464 	struct netdevsim *ns;
465 	int err;
466 
467 	dev = alloc_netdev_mq(sizeof(*ns), "eth%d", NET_NAME_UNKNOWN, nsim_setup,
468 			      nsim_dev->nsim_bus_dev->num_queues);
469 	if (!dev)
470 		return ERR_PTR(-ENOMEM);
471 
472 	dev_net_set(dev, nsim_dev_net(nsim_dev));
473 	ns = netdev_priv(dev);
474 	ns->netdev = dev;
475 	u64_stats_init(&ns->syncp);
476 	ns->nsim_dev = nsim_dev;
477 	ns->nsim_dev_port = nsim_dev_port;
478 	ns->nsim_bus_dev = nsim_dev->nsim_bus_dev;
479 	SET_NETDEV_DEV(dev, &ns->nsim_bus_dev->dev);
480 	SET_NETDEV_DEVLINK_PORT(dev, &nsim_dev_port->devlink_port);
481 	nsim_ethtool_init(ns);
482 	if (nsim_dev_port_is_pf(nsim_dev_port))
483 		err = nsim_init_netdevsim(ns);
484 	else
485 		err = nsim_init_netdevsim_vf(ns);
486 	if (err)
487 		goto err_free_netdev;
488 	return ns;
489 
490 err_free_netdev:
491 	free_netdev(dev);
492 	return ERR_PTR(err);
493 }
494 
495 void nsim_destroy(struct netdevsim *ns)
496 {
497 	struct net_device *dev = ns->netdev;
498 	struct netdevsim *peer;
499 
500 	rtnl_lock();
501 	peer = rtnl_dereference(ns->peer);
502 	if (peer)
503 		RCU_INIT_POINTER(peer->peer, NULL);
504 	RCU_INIT_POINTER(ns->peer, NULL);
505 	unregister_netdevice(dev);
506 	if (nsim_dev_port_is_pf(ns->nsim_dev_port)) {
507 		nsim_macsec_teardown(ns);
508 		nsim_ipsec_teardown(ns);
509 		nsim_bpf_uninit(ns);
510 	}
511 	rtnl_unlock();
512 	if (nsim_dev_port_is_pf(ns->nsim_dev_port))
513 		nsim_exit_netdevsim(ns);
514 	free_netdev(dev);
515 }
516 
517 bool netdev_is_nsim(struct net_device *dev)
518 {
519 	return dev->netdev_ops == &nsim_netdev_ops;
520 }
521 
522 static int nsim_validate(struct nlattr *tb[], struct nlattr *data[],
523 			 struct netlink_ext_ack *extack)
524 {
525 	NL_SET_ERR_MSG_MOD(extack,
526 			   "Please use: echo \"[ID] [PORT_COUNT] [NUM_QUEUES]\" > /sys/bus/netdevsim/new_device");
527 	return -EOPNOTSUPP;
528 }
529 
530 static struct rtnl_link_ops nsim_link_ops __read_mostly = {
531 	.kind		= DRV_NAME,
532 	.validate	= nsim_validate,
533 };
534 
535 static int __init nsim_module_init(void)
536 {
537 	int err;
538 
539 	err = nsim_dev_init();
540 	if (err)
541 		return err;
542 
543 	err = nsim_bus_init();
544 	if (err)
545 		goto err_dev_exit;
546 
547 	err = rtnl_link_register(&nsim_link_ops);
548 	if (err)
549 		goto err_bus_exit;
550 
551 	return 0;
552 
553 err_bus_exit:
554 	nsim_bus_exit();
555 err_dev_exit:
556 	nsim_dev_exit();
557 	return err;
558 }
559 
560 static void __exit nsim_module_exit(void)
561 {
562 	rtnl_link_unregister(&nsim_link_ops);
563 	nsim_bus_exit();
564 	nsim_dev_exit();
565 }
566 
567 module_init(nsim_module_init);
568 module_exit(nsim_module_exit);
569 MODULE_LICENSE("GPL");
570 MODULE_DESCRIPTION("Simulated networking device for testing");
571 MODULE_ALIAS_RTNL_LINK(DRV_NAME);
572