Linux 4.16.11
[linux/fpc-iii.git] / drivers / net / netdevsim / netdev.c
blob3fd567928f3d7c7530919c95b00df398590b6d9d
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>
26 #include "netdevsim.h"
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;
41 static u32 nsim_dev_id;
43 static int nsim_num_vf(struct device *dev)
45 struct netdevsim *ns = to_nsim(dev);
47 return ns->num_vfs;
50 static struct bus_type nsim_bus = {
51 .name = DRV_NAME,
52 .dev_name = DRV_NAME,
53 .num_vf = nsim_num_vf,
56 static int nsim_vfs_enable(struct netdevsim *ns, unsigned int num_vfs)
58 ns->vfconfigs = kcalloc(num_vfs, sizeof(struct nsim_vf_config),
59 GFP_KERNEL);
60 if (!ns->vfconfigs)
61 return -ENOMEM;
62 ns->num_vfs = num_vfs;
64 return 0;
67 static void nsim_vfs_disable(struct netdevsim *ns)
69 kfree(ns->vfconfigs);
70 ns->vfconfigs = NULL;
71 ns->num_vfs = 0;
74 static ssize_t
75 nsim_numvfs_store(struct device *dev, struct device_attribute *attr,
76 const char *buf, size_t count)
78 struct netdevsim *ns = to_nsim(dev);
79 unsigned int num_vfs;
80 int ret;
82 ret = kstrtouint(buf, 0, &num_vfs);
83 if (ret)
84 return ret;
86 rtnl_lock();
87 if (ns->num_vfs == num_vfs)
88 goto exit_good;
89 if (ns->num_vfs && num_vfs) {
90 ret = -EBUSY;
91 goto exit_unlock;
94 if (num_vfs) {
95 ret = nsim_vfs_enable(ns, num_vfs);
96 if (ret)
97 goto exit_unlock;
98 } else {
99 nsim_vfs_disable(ns);
101 exit_good:
102 ret = count;
103 exit_unlock:
104 rtnl_unlock();
106 return ret;
109 static ssize_t
110 nsim_numvfs_show(struct device *dev, struct device_attribute *attr, char *buf)
112 struct netdevsim *ns = to_nsim(dev);
114 return sprintf(buf, "%u\n", ns->num_vfs);
117 static struct device_attribute nsim_numvfs_attr =
118 __ATTR(sriov_numvfs, 0664, nsim_numvfs_show, nsim_numvfs_store);
120 static struct attribute *nsim_dev_attrs[] = {
121 &nsim_numvfs_attr.attr,
122 NULL,
125 static const struct attribute_group nsim_dev_attr_group = {
126 .attrs = nsim_dev_attrs,
129 static const struct attribute_group *nsim_dev_attr_groups[] = {
130 &nsim_dev_attr_group,
131 NULL,
134 static void nsim_dev_release(struct device *dev)
136 struct netdevsim *ns = to_nsim(dev);
138 nsim_vfs_disable(ns);
139 free_netdev(ns->netdev);
142 static struct device_type nsim_dev_type = {
143 .groups = nsim_dev_attr_groups,
144 .release = nsim_dev_release,
147 static int nsim_init(struct net_device *dev)
149 struct netdevsim *ns = netdev_priv(dev);
150 int err;
152 ns->netdev = dev;
153 ns->ddir = debugfs_create_dir(netdev_name(dev), nsim_ddir);
154 if (IS_ERR_OR_NULL(ns->ddir))
155 return -ENOMEM;
157 err = nsim_bpf_init(ns);
158 if (err)
159 goto err_debugfs_destroy;
161 ns->dev.id = nsim_dev_id++;
162 ns->dev.bus = &nsim_bus;
163 ns->dev.type = &nsim_dev_type;
164 err = device_register(&ns->dev);
165 if (err)
166 goto err_bpf_uninit;
168 SET_NETDEV_DEV(dev, &ns->dev);
170 return 0;
172 err_bpf_uninit:
173 nsim_bpf_uninit(ns);
174 err_debugfs_destroy:
175 debugfs_remove_recursive(ns->ddir);
176 return err;
179 static void nsim_uninit(struct net_device *dev)
181 struct netdevsim *ns = netdev_priv(dev);
183 debugfs_remove_recursive(ns->ddir);
184 nsim_bpf_uninit(ns);
187 static void nsim_free(struct net_device *dev)
189 struct netdevsim *ns = netdev_priv(dev);
191 device_unregister(&ns->dev);
192 /* netdev and vf state will be freed out of device_release() */
195 static netdev_tx_t nsim_start_xmit(struct sk_buff *skb, struct net_device *dev)
197 struct netdevsim *ns = netdev_priv(dev);
199 u64_stats_update_begin(&ns->syncp);
200 ns->tx_packets++;
201 ns->tx_bytes += skb->len;
202 u64_stats_update_end(&ns->syncp);
204 dev_kfree_skb(skb);
206 return NETDEV_TX_OK;
209 static void nsim_set_rx_mode(struct net_device *dev)
213 static int nsim_change_mtu(struct net_device *dev, int new_mtu)
215 struct netdevsim *ns = netdev_priv(dev);
217 if (ns->xdp_prog_mode == XDP_ATTACHED_DRV &&
218 new_mtu > NSIM_XDP_MAX_MTU)
219 return -EBUSY;
221 dev->mtu = new_mtu;
223 return 0;
226 static void
227 nsim_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
229 struct netdevsim *ns = netdev_priv(dev);
230 unsigned int start;
232 do {
233 start = u64_stats_fetch_begin(&ns->syncp);
234 stats->tx_bytes = ns->tx_bytes;
235 stats->tx_packets = ns->tx_packets;
236 } while (u64_stats_fetch_retry(&ns->syncp, start));
239 static int
240 nsim_setup_tc_block_cb(enum tc_setup_type type, void *type_data, void *cb_priv)
242 return nsim_bpf_setup_tc_block_cb(type, type_data, cb_priv);
245 static int
246 nsim_setup_tc_block(struct net_device *dev, struct tc_block_offload *f)
248 struct netdevsim *ns = netdev_priv(dev);
250 if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
251 return -EOPNOTSUPP;
253 switch (f->command) {
254 case TC_BLOCK_BIND:
255 return tcf_block_cb_register(f->block, nsim_setup_tc_block_cb,
256 ns, ns);
257 case TC_BLOCK_UNBIND:
258 tcf_block_cb_unregister(f->block, nsim_setup_tc_block_cb, ns);
259 return 0;
260 default:
261 return -EOPNOTSUPP;
265 static int nsim_set_vf_mac(struct net_device *dev, int vf, u8 *mac)
267 struct netdevsim *ns = netdev_priv(dev);
269 /* Only refuse multicast addresses, zero address can mean unset/any. */
270 if (vf >= ns->num_vfs || is_multicast_ether_addr(mac))
271 return -EINVAL;
272 memcpy(ns->vfconfigs[vf].vf_mac, mac, ETH_ALEN);
274 return 0;
277 static int nsim_set_vf_vlan(struct net_device *dev, int vf,
278 u16 vlan, u8 qos, __be16 vlan_proto)
280 struct netdevsim *ns = netdev_priv(dev);
282 if (vf >= ns->num_vfs || vlan > 4095 || qos > 7)
283 return -EINVAL;
285 ns->vfconfigs[vf].vlan = vlan;
286 ns->vfconfigs[vf].qos = qos;
287 ns->vfconfigs[vf].vlan_proto = vlan_proto;
289 return 0;
292 static int nsim_set_vf_rate(struct net_device *dev, int vf, int min, int max)
294 struct netdevsim *ns = netdev_priv(dev);
296 if (vf >= ns->num_vfs)
297 return -EINVAL;
299 ns->vfconfigs[vf].min_tx_rate = min;
300 ns->vfconfigs[vf].max_tx_rate = max;
302 return 0;
305 static int nsim_set_vf_spoofchk(struct net_device *dev, int vf, bool val)
307 struct netdevsim *ns = netdev_priv(dev);
309 if (vf >= ns->num_vfs)
310 return -EINVAL;
311 ns->vfconfigs[vf].spoofchk_enabled = val;
313 return 0;
316 static int nsim_set_vf_rss_query_en(struct net_device *dev, int vf, bool val)
318 struct netdevsim *ns = netdev_priv(dev);
320 if (vf >= ns->num_vfs)
321 return -EINVAL;
322 ns->vfconfigs[vf].rss_query_enabled = val;
324 return 0;
327 static int nsim_set_vf_trust(struct net_device *dev, int vf, bool val)
329 struct netdevsim *ns = netdev_priv(dev);
331 if (vf >= ns->num_vfs)
332 return -EINVAL;
333 ns->vfconfigs[vf].trusted = val;
335 return 0;
338 static int
339 nsim_get_vf_config(struct net_device *dev, int vf, struct ifla_vf_info *ivi)
341 struct netdevsim *ns = netdev_priv(dev);
343 if (vf >= ns->num_vfs)
344 return -EINVAL;
346 ivi->vf = vf;
347 ivi->linkstate = ns->vfconfigs[vf].link_state;
348 ivi->min_tx_rate = ns->vfconfigs[vf].min_tx_rate;
349 ivi->max_tx_rate = ns->vfconfigs[vf].max_tx_rate;
350 ivi->vlan = ns->vfconfigs[vf].vlan;
351 ivi->vlan_proto = ns->vfconfigs[vf].vlan_proto;
352 ivi->qos = ns->vfconfigs[vf].qos;
353 memcpy(&ivi->mac, ns->vfconfigs[vf].vf_mac, ETH_ALEN);
354 ivi->spoofchk = ns->vfconfigs[vf].spoofchk_enabled;
355 ivi->trusted = ns->vfconfigs[vf].trusted;
356 ivi->rss_query_en = ns->vfconfigs[vf].rss_query_enabled;
358 return 0;
361 static int nsim_set_vf_link_state(struct net_device *dev, int vf, int state)
363 struct netdevsim *ns = netdev_priv(dev);
365 if (vf >= ns->num_vfs)
366 return -EINVAL;
368 switch (state) {
369 case IFLA_VF_LINK_STATE_AUTO:
370 case IFLA_VF_LINK_STATE_ENABLE:
371 case IFLA_VF_LINK_STATE_DISABLE:
372 break;
373 default:
374 return -EINVAL;
377 ns->vfconfigs[vf].link_state = state;
379 return 0;
382 static int
383 nsim_setup_tc(struct net_device *dev, enum tc_setup_type type, void *type_data)
385 switch (type) {
386 case TC_SETUP_BLOCK:
387 return nsim_setup_tc_block(dev, type_data);
388 default:
389 return -EOPNOTSUPP;
393 static int
394 nsim_set_features(struct net_device *dev, netdev_features_t features)
396 struct netdevsim *ns = netdev_priv(dev);
398 if ((dev->features & NETIF_F_HW_TC) > (features & NETIF_F_HW_TC))
399 return nsim_bpf_disable_tc(ns);
401 return 0;
404 static const struct net_device_ops nsim_netdev_ops = {
405 .ndo_init = nsim_init,
406 .ndo_uninit = nsim_uninit,
407 .ndo_start_xmit = nsim_start_xmit,
408 .ndo_set_rx_mode = nsim_set_rx_mode,
409 .ndo_set_mac_address = eth_mac_addr,
410 .ndo_validate_addr = eth_validate_addr,
411 .ndo_change_mtu = nsim_change_mtu,
412 .ndo_get_stats64 = nsim_get_stats64,
413 .ndo_set_vf_mac = nsim_set_vf_mac,
414 .ndo_set_vf_vlan = nsim_set_vf_vlan,
415 .ndo_set_vf_rate = nsim_set_vf_rate,
416 .ndo_set_vf_spoofchk = nsim_set_vf_spoofchk,
417 .ndo_set_vf_trust = nsim_set_vf_trust,
418 .ndo_get_vf_config = nsim_get_vf_config,
419 .ndo_set_vf_link_state = nsim_set_vf_link_state,
420 .ndo_set_vf_rss_query_en = nsim_set_vf_rss_query_en,
421 .ndo_setup_tc = nsim_setup_tc,
422 .ndo_set_features = nsim_set_features,
423 .ndo_bpf = nsim_bpf,
426 static void nsim_setup(struct net_device *dev)
428 ether_setup(dev);
429 eth_hw_addr_random(dev);
431 dev->netdev_ops = &nsim_netdev_ops;
432 dev->priv_destructor = nsim_free;
434 dev->tx_queue_len = 0;
435 dev->flags |= IFF_NOARP;
436 dev->flags &= ~IFF_MULTICAST;
437 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE |
438 IFF_NO_QUEUE;
439 dev->features |= NETIF_F_HIGHDMA |
440 NETIF_F_SG |
441 NETIF_F_FRAGLIST |
442 NETIF_F_HW_CSUM |
443 NETIF_F_TSO;
444 dev->hw_features |= NETIF_F_HW_TC;
445 dev->max_mtu = ETH_MAX_MTU;
448 static int nsim_validate(struct nlattr *tb[], struct nlattr *data[],
449 struct netlink_ext_ack *extack)
451 if (tb[IFLA_ADDRESS]) {
452 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
453 return -EINVAL;
454 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
455 return -EADDRNOTAVAIL;
457 return 0;
460 static struct rtnl_link_ops nsim_link_ops __read_mostly = {
461 .kind = DRV_NAME,
462 .priv_size = sizeof(struct netdevsim),
463 .setup = nsim_setup,
464 .validate = nsim_validate,
467 struct dentry *nsim_ddir;
469 static int __init nsim_module_init(void)
471 int err;
473 nsim_ddir = debugfs_create_dir(DRV_NAME, NULL);
474 if (IS_ERR_OR_NULL(nsim_ddir))
475 return -ENOMEM;
477 err = bus_register(&nsim_bus);
478 if (err)
479 goto err_debugfs_destroy;
481 err = rtnl_link_register(&nsim_link_ops);
482 if (err)
483 goto err_unreg_bus;
485 return 0;
487 err_unreg_bus:
488 bus_unregister(&nsim_bus);
489 err_debugfs_destroy:
490 debugfs_remove_recursive(nsim_ddir);
491 return err;
494 static void __exit nsim_module_exit(void)
496 rtnl_link_unregister(&nsim_link_ops);
497 bus_unregister(&nsim_bus);
498 debugfs_remove_recursive(nsim_ddir);
501 module_init(nsim_module_init);
502 module_exit(nsim_module_exit);
503 MODULE_LICENSE("GPL");
504 MODULE_ALIAS_RTNL_LINK(DRV_NAME);