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