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