perf tools: Don't clone maps from parent when synthesizing forks
[linux/fpc-iii.git] / drivers / net / netdevsim / netdev.c
blob8d8e2b3f263e6127d9af53a7a0eda62e44b5b42b
1 /*
2 * Copyright (C) 2017 Netronome Systems, Inc.
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.
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.
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>
27 #include "netdevsim.h"
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;
42 static u32 nsim_dev_id;
44 static struct dentry *nsim_ddir;
45 static struct dentry *nsim_sdev_ddir;
47 static int nsim_num_vf(struct device *dev)
49 struct netdevsim *ns = to_nsim(dev);
51 return ns->num_vfs;
54 static struct bus_type nsim_bus = {
55 .name = DRV_NAME,
56 .dev_name = DRV_NAME,
57 .num_vf = nsim_num_vf,
60 static int nsim_vfs_enable(struct netdevsim *ns, unsigned int num_vfs)
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;
68 return 0;
71 static void nsim_vfs_disable(struct netdevsim *ns)
73 kfree(ns->vfconfigs);
74 ns->vfconfigs = NULL;
75 ns->num_vfs = 0;
78 static ssize_t
79 nsim_numvfs_store(struct device *dev, struct device_attribute *attr,
80 const char *buf, size_t count)
82 struct netdevsim *ns = to_nsim(dev);
83 unsigned int num_vfs;
84 int ret;
86 ret = kstrtouint(buf, 0, &num_vfs);
87 if (ret)
88 return ret;
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;
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);
105 exit_good:
106 ret = count;
107 exit_unlock:
108 rtnl_unlock();
110 return ret;
113 static ssize_t
114 nsim_numvfs_show(struct device *dev, struct device_attribute *attr, char *buf)
116 struct netdevsim *ns = to_nsim(dev);
118 return sprintf(buf, "%u\n", ns->num_vfs);
121 static struct device_attribute nsim_numvfs_attr =
122 __ATTR(sriov_numvfs, 0664, nsim_numvfs_show, nsim_numvfs_store);
124 static struct attribute *nsim_dev_attrs[] = {
125 &nsim_numvfs_attr.attr,
126 NULL,
129 static const struct attribute_group nsim_dev_attr_group = {
130 .attrs = nsim_dev_attrs,
133 static const struct attribute_group *nsim_dev_attr_groups[] = {
134 &nsim_dev_attr_group,
135 NULL,
138 static void nsim_dev_release(struct device *dev)
140 struct netdevsim *ns = to_nsim(dev);
142 nsim_vfs_disable(ns);
143 free_netdev(ns->netdev);
146 static struct device_type nsim_dev_type = {
147 .groups = nsim_dev_attr_groups,
148 .release = nsim_dev_release,
151 static int
152 nsim_port_attr_get(struct net_device *dev, struct switchdev_attr *attr)
154 struct netdevsim *ns = netdev_priv(dev);
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;
167 static const struct switchdev_ops nsim_switchdev_ops = {
168 .switchdev_port_attr_get = nsim_port_attr_get,
171 static int nsim_init(struct net_device *dev)
173 char sdev_ddir_name[10], sdev_link_name[32];
174 struct netdevsim *ns = netdev_priv(dev);
175 int err;
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;
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;
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;
197 } else {
198 sprintf(sdev_ddir_name, "%u", ns->sdev->switch_id);
199 ns->sdev->refcnt++;
202 sprintf(sdev_link_name, "../../" DRV_NAME "_sdev/%s", sdev_ddir_name);
203 debugfs_create_symlink("sdev", ns->ddir, sdev_link_name);
205 err = nsim_bpf_init(ns);
206 if (err)
207 goto err_sdev_destroy;
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;
216 SET_NETDEV_DEV(dev, &ns->dev);
217 SWITCHDEV_SET_OPS(dev, &nsim_switchdev_ops);
219 err = nsim_devlink_setup(ns);
220 if (err)
221 goto err_unreg_dev;
223 nsim_ipsec_init(ns);
225 return 0;
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);
237 err_debugfs_destroy:
238 debugfs_remove_recursive(ns->ddir);
239 return err;
242 static void nsim_uninit(struct net_device *dev)
244 struct netdevsim *ns = netdev_priv(dev);
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);
256 static void nsim_free(struct net_device *dev)
258 struct netdevsim *ns = netdev_priv(dev);
260 device_unregister(&ns->dev);
261 /* netdev and vf state will be freed out of device_release() */
264 static netdev_tx_t nsim_start_xmit(struct sk_buff *skb, struct net_device *dev)
266 struct netdevsim *ns = netdev_priv(dev);
268 if (!nsim_ipsec_tx(ns, skb))
269 goto out;
271 u64_stats_update_begin(&ns->syncp);
272 ns->tx_packets++;
273 ns->tx_bytes += skb->len;
274 u64_stats_update_end(&ns->syncp);
276 out:
277 dev_kfree_skb(skb);
279 return NETDEV_TX_OK;
282 static void nsim_set_rx_mode(struct net_device *dev)
286 static int nsim_change_mtu(struct net_device *dev, int new_mtu)
288 struct netdevsim *ns = netdev_priv(dev);
290 if (ns->xdp.prog && new_mtu > NSIM_XDP_MAX_MTU)
291 return -EBUSY;
293 dev->mtu = new_mtu;
295 return 0;
298 static void
299 nsim_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
301 struct netdevsim *ns = netdev_priv(dev);
302 unsigned int start;
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));
311 static int
312 nsim_setup_tc_block_cb(enum tc_setup_type type, void *type_data, void *cb_priv)
314 return nsim_bpf_setup_tc_block_cb(type, type_data, cb_priv);
317 static int
318 nsim_setup_tc_block(struct net_device *dev, struct tc_block_offload *f)
320 struct netdevsim *ns = netdev_priv(dev);
322 if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
323 return -EOPNOTSUPP;
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;
337 static int nsim_set_vf_mac(struct net_device *dev, int vf, u8 *mac)
339 struct netdevsim *ns = netdev_priv(dev);
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);
346 return 0;
349 static int nsim_set_vf_vlan(struct net_device *dev, int vf,
350 u16 vlan, u8 qos, __be16 vlan_proto)
352 struct netdevsim *ns = netdev_priv(dev);
354 if (vf >= ns->num_vfs || vlan > 4095 || qos > 7)
355 return -EINVAL;
357 ns->vfconfigs[vf].vlan = vlan;
358 ns->vfconfigs[vf].qos = qos;
359 ns->vfconfigs[vf].vlan_proto = vlan_proto;
361 return 0;
364 static int nsim_set_vf_rate(struct net_device *dev, int vf, int min, int max)
366 struct netdevsim *ns = netdev_priv(dev);
368 if (vf >= ns->num_vfs)
369 return -EINVAL;
371 ns->vfconfigs[vf].min_tx_rate = min;
372 ns->vfconfigs[vf].max_tx_rate = max;
374 return 0;
377 static int nsim_set_vf_spoofchk(struct net_device *dev, int vf, bool val)
379 struct netdevsim *ns = netdev_priv(dev);
381 if (vf >= ns->num_vfs)
382 return -EINVAL;
383 ns->vfconfigs[vf].spoofchk_enabled = val;
385 return 0;
388 static int nsim_set_vf_rss_query_en(struct net_device *dev, int vf, bool val)
390 struct netdevsim *ns = netdev_priv(dev);
392 if (vf >= ns->num_vfs)
393 return -EINVAL;
394 ns->vfconfigs[vf].rss_query_enabled = val;
396 return 0;
399 static int nsim_set_vf_trust(struct net_device *dev, int vf, bool val)
401 struct netdevsim *ns = netdev_priv(dev);
403 if (vf >= ns->num_vfs)
404 return -EINVAL;
405 ns->vfconfigs[vf].trusted = val;
407 return 0;
410 static int
411 nsim_get_vf_config(struct net_device *dev, int vf, struct ifla_vf_info *ivi)
413 struct netdevsim *ns = netdev_priv(dev);
415 if (vf >= ns->num_vfs)
416 return -EINVAL;
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;
430 return 0;
433 static int nsim_set_vf_link_state(struct net_device *dev, int vf, int state)
435 struct netdevsim *ns = netdev_priv(dev);
437 if (vf >= ns->num_vfs)
438 return -EINVAL;
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;
449 ns->vfconfigs[vf].link_state = state;
451 return 0;
454 static int
455 nsim_setup_tc(struct net_device *dev, enum tc_setup_type type, void *type_data)
457 switch (type) {
458 case TC_SETUP_BLOCK:
459 return nsim_setup_tc_block(dev, type_data);
460 default:
461 return -EOPNOTSUPP;
465 static int
466 nsim_set_features(struct net_device *dev, netdev_features_t features)
468 struct netdevsim *ns = netdev_priv(dev);
470 if ((dev->features & NETIF_F_HW_TC) > (features & NETIF_F_HW_TC))
471 return nsim_bpf_disable_tc(ns);
473 return 0;
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,
498 static void nsim_setup(struct net_device *dev)
500 ether_setup(dev);
501 eth_hw_addr_random(dev);
503 dev->netdev_ops = &nsim_netdev_ops;
504 dev->priv_destructor = nsim_free;
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;
520 static int nsim_validate(struct nlattr *tb[], struct nlattr *data[],
521 struct netlink_ext_ack *extack)
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;
529 return 0;
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)
536 struct netdevsim *ns = netdev_priv(dev);
538 if (tb[IFLA_LINK]) {
539 struct net_device *joindev;
540 struct netdevsim *joinns;
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;
549 joinns = netdev_priv(joindev);
550 if (!joinns->sdev || !joinns->sdev->refcnt)
551 return -EINVAL;
552 ns->sdev = joinns->sdev;
555 return register_netdevice(dev);
558 static void nsim_dellink(struct net_device *dev, struct list_head *head)
560 unregister_netdevice_queue(dev, head);
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,
572 static int __init nsim_module_init(void)
574 int err;
576 nsim_ddir = debugfs_create_dir(DRV_NAME, NULL);
577 if (IS_ERR_OR_NULL(nsim_ddir))
578 return -ENOMEM;
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;
586 err = bus_register(&nsim_bus);
587 if (err)
588 goto err_sdir_destroy;
590 err = nsim_devlink_init();
591 if (err)
592 goto err_unreg_bus;
594 err = rtnl_link_register(&nsim_link_ops);
595 if (err)
596 goto err_dl_fini;
598 return 0;
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;
611 static void __exit nsim_module_exit(void)
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);
620 module_init(nsim_module_init);
621 module_exit(nsim_module_exit);
622 MODULE_LICENSE("GPL");
623 MODULE_ALIAS_RTNL_LINK(DRV_NAME);