xref: /linux-6.15/drivers/net/netdevsim/netdev.c (revision 93e21254)
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 struct nsim_vf_config {
29 	int link_state;
30 	u16 min_tx_rate;
31 	u16 max_tx_rate;
32 	u16 vlan;
33 	__be16 vlan_proto;
34 	u16 qos;
35 	u8 vf_mac[ETH_ALEN];
36 	bool spoofchk_enabled;
37 	bool trusted;
38 	bool rss_query_enabled;
39 };
40 
41 static u32 nsim_dev_id;
42 
43 static struct dentry *nsim_ddir;
44 static struct dentry *nsim_sdev_ddir;
45 
46 static int nsim_num_vf(struct device *dev)
47 {
48 	struct netdevsim *ns = to_nsim(dev);
49 
50 	return ns->num_vfs;
51 }
52 
53 static struct bus_type nsim_bus = {
54 	.name		= DRV_NAME,
55 	.dev_name	= DRV_NAME,
56 	.num_vf		= nsim_num_vf,
57 };
58 
59 static int nsim_vfs_enable(struct netdevsim *ns, unsigned int num_vfs)
60 {
61 	ns->vfconfigs = kcalloc(num_vfs, sizeof(struct nsim_vf_config),
62 				GFP_KERNEL);
63 	if (!ns->vfconfigs)
64 		return -ENOMEM;
65 	ns->num_vfs = num_vfs;
66 
67 	return 0;
68 }
69 
70 static void nsim_vfs_disable(struct netdevsim *ns)
71 {
72 	kfree(ns->vfconfigs);
73 	ns->vfconfigs = NULL;
74 	ns->num_vfs = 0;
75 }
76 
77 static ssize_t
78 nsim_numvfs_store(struct device *dev, struct device_attribute *attr,
79 		  const char *buf, size_t count)
80 {
81 	struct netdevsim *ns = to_nsim(dev);
82 	unsigned int num_vfs;
83 	int ret;
84 
85 	ret = kstrtouint(buf, 0, &num_vfs);
86 	if (ret)
87 		return ret;
88 
89 	rtnl_lock();
90 	if (ns->num_vfs == num_vfs)
91 		goto exit_good;
92 	if (ns->num_vfs && num_vfs) {
93 		ret = -EBUSY;
94 		goto exit_unlock;
95 	}
96 
97 	if (num_vfs) {
98 		ret = nsim_vfs_enable(ns, num_vfs);
99 		if (ret)
100 			goto exit_unlock;
101 	} else {
102 		nsim_vfs_disable(ns);
103 	}
104 exit_good:
105 	ret = count;
106 exit_unlock:
107 	rtnl_unlock();
108 
109 	return ret;
110 }
111 
112 static ssize_t
113 nsim_numvfs_show(struct device *dev, struct device_attribute *attr, char *buf)
114 {
115 	struct netdevsim *ns = to_nsim(dev);
116 
117 	return sprintf(buf, "%u\n", ns->num_vfs);
118 }
119 
120 static struct device_attribute nsim_numvfs_attr =
121 	__ATTR(sriov_numvfs, 0664, nsim_numvfs_show, nsim_numvfs_store);
122 
123 static struct attribute *nsim_dev_attrs[] = {
124 	&nsim_numvfs_attr.attr,
125 	NULL,
126 };
127 
128 static const struct attribute_group nsim_dev_attr_group = {
129 	.attrs = nsim_dev_attrs,
130 };
131 
132 static const struct attribute_group *nsim_dev_attr_groups[] = {
133 	&nsim_dev_attr_group,
134 	NULL,
135 };
136 
137 static void nsim_dev_release(struct device *dev)
138 {
139 	struct netdevsim *ns = to_nsim(dev);
140 
141 	nsim_vfs_disable(ns);
142 }
143 
144 static struct device_type nsim_dev_type = {
145 	.groups = nsim_dev_attr_groups,
146 	.release = nsim_dev_release,
147 };
148 
149 static int nsim_get_port_parent_id(struct net_device *dev,
150 				   struct netdev_phys_item_id *ppid)
151 {
152 	struct netdevsim *ns = netdev_priv(dev);
153 
154 	ppid->id_len = sizeof(ns->sdev->switch_id);
155 	memcpy(&ppid->id, &ns->sdev->switch_id, ppid->id_len);
156 	return 0;
157 }
158 
159 static int nsim_init(struct net_device *dev)
160 {
161 	char sdev_ddir_name[10], sdev_link_name[32];
162 	struct netdevsim *ns = netdev_priv(dev);
163 	int err;
164 
165 	ns->netdev = dev;
166 	ns->ddir = debugfs_create_dir(netdev_name(dev), nsim_ddir);
167 	if (IS_ERR_OR_NULL(ns->ddir))
168 		return -ENOMEM;
169 
170 	if (!ns->sdev) {
171 		ns->sdev = kzalloc(sizeof(*ns->sdev), GFP_KERNEL);
172 		if (!ns->sdev) {
173 			err = -ENOMEM;
174 			goto err_debugfs_destroy;
175 		}
176 		ns->sdev->refcnt = 1;
177 		ns->sdev->switch_id = nsim_dev_id;
178 		sprintf(sdev_ddir_name, "%u", ns->sdev->switch_id);
179 		ns->sdev->ddir = debugfs_create_dir(sdev_ddir_name,
180 						    nsim_sdev_ddir);
181 		if (IS_ERR_OR_NULL(ns->sdev->ddir)) {
182 			err = PTR_ERR_OR_ZERO(ns->sdev->ddir) ?: -EINVAL;
183 			goto err_sdev_free;
184 		}
185 	} else {
186 		sprintf(sdev_ddir_name, "%u", ns->sdev->switch_id);
187 		ns->sdev->refcnt++;
188 	}
189 
190 	sprintf(sdev_link_name, "../../" DRV_NAME "_sdev/%s", sdev_ddir_name);
191 	debugfs_create_symlink("sdev", ns->ddir, sdev_link_name);
192 
193 	err = nsim_bpf_init(ns);
194 	if (err)
195 		goto err_sdev_destroy;
196 
197 	ns->dev.id = nsim_dev_id++;
198 	ns->dev.bus = &nsim_bus;
199 	ns->dev.type = &nsim_dev_type;
200 	err = device_register(&ns->dev);
201 	if (err)
202 		goto err_bpf_uninit;
203 
204 	SET_NETDEV_DEV(dev, &ns->dev);
205 
206 	err = nsim_devlink_setup(ns);
207 	if (err)
208 		goto err_unreg_dev;
209 
210 	nsim_ipsec_init(ns);
211 
212 	return 0;
213 
214 err_unreg_dev:
215 	device_unregister(&ns->dev);
216 err_bpf_uninit:
217 	nsim_bpf_uninit(ns);
218 err_sdev_destroy:
219 	if (!--ns->sdev->refcnt) {
220 		debugfs_remove_recursive(ns->sdev->ddir);
221 err_sdev_free:
222 		kfree(ns->sdev);
223 	}
224 err_debugfs_destroy:
225 	debugfs_remove_recursive(ns->ddir);
226 	return err;
227 }
228 
229 static void nsim_uninit(struct net_device *dev)
230 {
231 	struct netdevsim *ns = netdev_priv(dev);
232 
233 	nsim_ipsec_teardown(ns);
234 	nsim_devlink_teardown(ns);
235 	debugfs_remove_recursive(ns->ddir);
236 	nsim_bpf_uninit(ns);
237 	if (!--ns->sdev->refcnt) {
238 		debugfs_remove_recursive(ns->sdev->ddir);
239 		kfree(ns->sdev);
240 	}
241 }
242 
243 static void nsim_free(struct net_device *dev)
244 {
245 	struct netdevsim *ns = netdev_priv(dev);
246 
247 	device_unregister(&ns->dev);
248 	/* netdev and vf state will be freed out of device_release() */
249 }
250 
251 static netdev_tx_t nsim_start_xmit(struct sk_buff *skb, struct net_device *dev)
252 {
253 	struct netdevsim *ns = netdev_priv(dev);
254 
255 	if (!nsim_ipsec_tx(ns, skb))
256 		goto out;
257 
258 	u64_stats_update_begin(&ns->syncp);
259 	ns->tx_packets++;
260 	ns->tx_bytes += skb->len;
261 	u64_stats_update_end(&ns->syncp);
262 
263 out:
264 	dev_kfree_skb(skb);
265 
266 	return NETDEV_TX_OK;
267 }
268 
269 static void nsim_set_rx_mode(struct net_device *dev)
270 {
271 }
272 
273 static int nsim_change_mtu(struct net_device *dev, int new_mtu)
274 {
275 	struct netdevsim *ns = netdev_priv(dev);
276 
277 	if (ns->xdp.prog && new_mtu > NSIM_XDP_MAX_MTU)
278 		return -EBUSY;
279 
280 	dev->mtu = new_mtu;
281 
282 	return 0;
283 }
284 
285 static void
286 nsim_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
287 {
288 	struct netdevsim *ns = netdev_priv(dev);
289 	unsigned int start;
290 
291 	do {
292 		start = u64_stats_fetch_begin(&ns->syncp);
293 		stats->tx_bytes = ns->tx_bytes;
294 		stats->tx_packets = ns->tx_packets;
295 	} while (u64_stats_fetch_retry(&ns->syncp, start));
296 }
297 
298 static int
299 nsim_setup_tc_block_cb(enum tc_setup_type type, void *type_data, void *cb_priv)
300 {
301 	return nsim_bpf_setup_tc_block_cb(type, type_data, cb_priv);
302 }
303 
304 static int
305 nsim_setup_tc_block(struct net_device *dev, struct tc_block_offload *f)
306 {
307 	struct netdevsim *ns = netdev_priv(dev);
308 
309 	if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
310 		return -EOPNOTSUPP;
311 
312 	switch (f->command) {
313 	case TC_BLOCK_BIND:
314 		return tcf_block_cb_register(f->block, nsim_setup_tc_block_cb,
315 					     ns, ns, f->extack);
316 	case TC_BLOCK_UNBIND:
317 		tcf_block_cb_unregister(f->block, nsim_setup_tc_block_cb, ns);
318 		return 0;
319 	default:
320 		return -EOPNOTSUPP;
321 	}
322 }
323 
324 static int nsim_set_vf_mac(struct net_device *dev, int vf, u8 *mac)
325 {
326 	struct netdevsim *ns = netdev_priv(dev);
327 
328 	/* Only refuse multicast addresses, zero address can mean unset/any. */
329 	if (vf >= ns->num_vfs || is_multicast_ether_addr(mac))
330 		return -EINVAL;
331 	memcpy(ns->vfconfigs[vf].vf_mac, mac, ETH_ALEN);
332 
333 	return 0;
334 }
335 
336 static int nsim_set_vf_vlan(struct net_device *dev, int vf,
337 			    u16 vlan, u8 qos, __be16 vlan_proto)
338 {
339 	struct netdevsim *ns = netdev_priv(dev);
340 
341 	if (vf >= ns->num_vfs || vlan > 4095 || qos > 7)
342 		return -EINVAL;
343 
344 	ns->vfconfigs[vf].vlan = vlan;
345 	ns->vfconfigs[vf].qos = qos;
346 	ns->vfconfigs[vf].vlan_proto = vlan_proto;
347 
348 	return 0;
349 }
350 
351 static int nsim_set_vf_rate(struct net_device *dev, int vf, int min, int max)
352 {
353 	struct netdevsim *ns = netdev_priv(dev);
354 
355 	if (vf >= ns->num_vfs)
356 		return -EINVAL;
357 
358 	ns->vfconfigs[vf].min_tx_rate = min;
359 	ns->vfconfigs[vf].max_tx_rate = max;
360 
361 	return 0;
362 }
363 
364 static int nsim_set_vf_spoofchk(struct net_device *dev, int vf, bool val)
365 {
366 	struct netdevsim *ns = netdev_priv(dev);
367 
368 	if (vf >= ns->num_vfs)
369 		return -EINVAL;
370 	ns->vfconfigs[vf].spoofchk_enabled = val;
371 
372 	return 0;
373 }
374 
375 static int nsim_set_vf_rss_query_en(struct net_device *dev, int vf, bool val)
376 {
377 	struct netdevsim *ns = netdev_priv(dev);
378 
379 	if (vf >= ns->num_vfs)
380 		return -EINVAL;
381 	ns->vfconfigs[vf].rss_query_enabled = val;
382 
383 	return 0;
384 }
385 
386 static int nsim_set_vf_trust(struct net_device *dev, int vf, bool val)
387 {
388 	struct netdevsim *ns = netdev_priv(dev);
389 
390 	if (vf >= ns->num_vfs)
391 		return -EINVAL;
392 	ns->vfconfigs[vf].trusted = val;
393 
394 	return 0;
395 }
396 
397 static int
398 nsim_get_vf_config(struct net_device *dev, int vf, struct ifla_vf_info *ivi)
399 {
400 	struct netdevsim *ns = netdev_priv(dev);
401 
402 	if (vf >= ns->num_vfs)
403 		return -EINVAL;
404 
405 	ivi->vf = vf;
406 	ivi->linkstate = ns->vfconfigs[vf].link_state;
407 	ivi->min_tx_rate = ns->vfconfigs[vf].min_tx_rate;
408 	ivi->max_tx_rate = ns->vfconfigs[vf].max_tx_rate;
409 	ivi->vlan = ns->vfconfigs[vf].vlan;
410 	ivi->vlan_proto = ns->vfconfigs[vf].vlan_proto;
411 	ivi->qos = ns->vfconfigs[vf].qos;
412 	memcpy(&ivi->mac, ns->vfconfigs[vf].vf_mac, ETH_ALEN);
413 	ivi->spoofchk = ns->vfconfigs[vf].spoofchk_enabled;
414 	ivi->trusted = ns->vfconfigs[vf].trusted;
415 	ivi->rss_query_en = ns->vfconfigs[vf].rss_query_enabled;
416 
417 	return 0;
418 }
419 
420 static int nsim_set_vf_link_state(struct net_device *dev, int vf, int state)
421 {
422 	struct netdevsim *ns = netdev_priv(dev);
423 
424 	if (vf >= ns->num_vfs)
425 		return -EINVAL;
426 
427 	switch (state) {
428 	case IFLA_VF_LINK_STATE_AUTO:
429 	case IFLA_VF_LINK_STATE_ENABLE:
430 	case IFLA_VF_LINK_STATE_DISABLE:
431 		break;
432 	default:
433 		return -EINVAL;
434 	}
435 
436 	ns->vfconfigs[vf].link_state = state;
437 
438 	return 0;
439 }
440 
441 static int
442 nsim_setup_tc(struct net_device *dev, enum tc_setup_type type, void *type_data)
443 {
444 	switch (type) {
445 	case TC_SETUP_BLOCK:
446 		return nsim_setup_tc_block(dev, type_data);
447 	default:
448 		return -EOPNOTSUPP;
449 	}
450 }
451 
452 static int
453 nsim_set_features(struct net_device *dev, netdev_features_t features)
454 {
455 	struct netdevsim *ns = netdev_priv(dev);
456 
457 	if ((dev->features & NETIF_F_HW_TC) > (features & NETIF_F_HW_TC))
458 		return nsim_bpf_disable_tc(ns);
459 
460 	return 0;
461 }
462 
463 static const struct net_device_ops nsim_netdev_ops = {
464 	.ndo_init		= nsim_init,
465 	.ndo_uninit		= nsim_uninit,
466 	.ndo_start_xmit		= nsim_start_xmit,
467 	.ndo_set_rx_mode	= nsim_set_rx_mode,
468 	.ndo_set_mac_address	= eth_mac_addr,
469 	.ndo_validate_addr	= eth_validate_addr,
470 	.ndo_change_mtu		= nsim_change_mtu,
471 	.ndo_get_stats64	= nsim_get_stats64,
472 	.ndo_set_vf_mac		= nsim_set_vf_mac,
473 	.ndo_set_vf_vlan	= nsim_set_vf_vlan,
474 	.ndo_set_vf_rate	= nsim_set_vf_rate,
475 	.ndo_set_vf_spoofchk	= nsim_set_vf_spoofchk,
476 	.ndo_set_vf_trust	= nsim_set_vf_trust,
477 	.ndo_get_vf_config	= nsim_get_vf_config,
478 	.ndo_set_vf_link_state	= nsim_set_vf_link_state,
479 	.ndo_set_vf_rss_query_en = nsim_set_vf_rss_query_en,
480 	.ndo_setup_tc		= nsim_setup_tc,
481 	.ndo_set_features	= nsim_set_features,
482 	.ndo_bpf		= nsim_bpf,
483 	.ndo_get_port_parent_id	= nsim_get_port_parent_id,
484 };
485 
486 static void nsim_setup(struct net_device *dev)
487 {
488 	ether_setup(dev);
489 	eth_hw_addr_random(dev);
490 
491 	dev->netdev_ops = &nsim_netdev_ops;
492 	dev->needs_free_netdev = true;
493 	dev->priv_destructor = nsim_free;
494 
495 	dev->tx_queue_len = 0;
496 	dev->flags |= IFF_NOARP;
497 	dev->flags &= ~IFF_MULTICAST;
498 	dev->priv_flags |= IFF_LIVE_ADDR_CHANGE |
499 			   IFF_NO_QUEUE;
500 	dev->features |= NETIF_F_HIGHDMA |
501 			 NETIF_F_SG |
502 			 NETIF_F_FRAGLIST |
503 			 NETIF_F_HW_CSUM |
504 			 NETIF_F_TSO;
505 	dev->hw_features |= NETIF_F_HW_TC;
506 	dev->max_mtu = ETH_MAX_MTU;
507 }
508 
509 static int nsim_validate(struct nlattr *tb[], struct nlattr *data[],
510 			 struct netlink_ext_ack *extack)
511 {
512 	if (tb[IFLA_ADDRESS]) {
513 		if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
514 			return -EINVAL;
515 		if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
516 			return -EADDRNOTAVAIL;
517 	}
518 	return 0;
519 }
520 
521 static int nsim_newlink(struct net *src_net, struct net_device *dev,
522 			struct nlattr *tb[], struct nlattr *data[],
523 			struct netlink_ext_ack *extack)
524 {
525 	struct netdevsim *ns = netdev_priv(dev);
526 
527 	if (tb[IFLA_LINK]) {
528 		struct net_device *joindev;
529 		struct netdevsim *joinns;
530 
531 		joindev = __dev_get_by_index(src_net,
532 					     nla_get_u32(tb[IFLA_LINK]));
533 		if (!joindev)
534 			return -ENODEV;
535 		if (joindev->netdev_ops != &nsim_netdev_ops)
536 			return -EINVAL;
537 
538 		joinns = netdev_priv(joindev);
539 		if (!joinns->sdev || !joinns->sdev->refcnt)
540 			return -EINVAL;
541 		ns->sdev = joinns->sdev;
542 	}
543 
544 	return register_netdevice(dev);
545 }
546 
547 static struct rtnl_link_ops nsim_link_ops __read_mostly = {
548 	.kind		= DRV_NAME,
549 	.priv_size	= sizeof(struct netdevsim),
550 	.setup		= nsim_setup,
551 	.validate	= nsim_validate,
552 	.newlink	= nsim_newlink,
553 };
554 
555 static int __init nsim_module_init(void)
556 {
557 	int err;
558 
559 	nsim_ddir = debugfs_create_dir(DRV_NAME, NULL);
560 	if (IS_ERR_OR_NULL(nsim_ddir))
561 		return -ENOMEM;
562 
563 	nsim_sdev_ddir = debugfs_create_dir(DRV_NAME "_sdev", NULL);
564 	if (IS_ERR_OR_NULL(nsim_sdev_ddir)) {
565 		err = -ENOMEM;
566 		goto err_debugfs_destroy;
567 	}
568 
569 	err = bus_register(&nsim_bus);
570 	if (err)
571 		goto err_sdir_destroy;
572 
573 	err = nsim_devlink_init();
574 	if (err)
575 		goto err_unreg_bus;
576 
577 	err = rtnl_link_register(&nsim_link_ops);
578 	if (err)
579 		goto err_dl_fini;
580 
581 	return 0;
582 
583 err_dl_fini:
584 	nsim_devlink_exit();
585 err_unreg_bus:
586 	bus_unregister(&nsim_bus);
587 err_sdir_destroy:
588 	debugfs_remove_recursive(nsim_sdev_ddir);
589 err_debugfs_destroy:
590 	debugfs_remove_recursive(nsim_ddir);
591 	return err;
592 }
593 
594 static void __exit nsim_module_exit(void)
595 {
596 	rtnl_link_unregister(&nsim_link_ops);
597 	nsim_devlink_exit();
598 	bus_unregister(&nsim_bus);
599 	debugfs_remove_recursive(nsim_sdev_ddir);
600 	debugfs_remove_recursive(nsim_ddir);
601 }
602 
603 module_init(nsim_module_init);
604 module_exit(nsim_module_exit);
605 MODULE_LICENSE("GPL");
606 MODULE_ALIAS_RTNL_LINK(DRV_NAME);
607