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
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
{
37 bool spoofchk_enabled
;
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
);
54 static struct bus_type nsim_bus
= {
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
),
66 ns
->num_vfs
= num_vfs
;
71 static void nsim_vfs_disable(struct netdevsim
*ns
)
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
);
86 ret
= kstrtouint(buf
, 0, &num_vfs
);
91 if (ns
->num_vfs
== num_vfs
)
93 if (ns
->num_vfs
&& num_vfs
) {
99 ret
= nsim_vfs_enable(ns
, num_vfs
);
103 nsim_vfs_disable(ns
);
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
,
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
,
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
,
152 nsim_port_attr_get(struct net_device
*dev
, struct switchdev_attr
*attr
)
154 struct netdevsim
*ns
= netdev_priv(dev
);
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
);
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
);
178 ns
->ddir
= debugfs_create_dir(netdev_name(dev
), nsim_ddir
);
179 if (IS_ERR_OR_NULL(ns
->ddir
))
183 ns
->sdev
= kzalloc(sizeof(*ns
->sdev
), GFP_KERNEL
);
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
,
193 if (IS_ERR_OR_NULL(ns
->sdev
->ddir
)) {
194 err
= PTR_ERR_OR_ZERO(ns
->sdev
->ddir
) ?: -EINVAL
;
198 sprintf(sdev_ddir_name
, "%u", ns
->sdev
->switch_id
);
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
);
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
);
216 SET_NETDEV_DEV(dev
, &ns
->dev
);
217 SWITCHDEV_SET_OPS(dev
, &nsim_switchdev_ops
);
219 err
= nsim_devlink_setup(ns
);
228 device_unregister(&ns
->dev
);
232 if (!--ns
->sdev
->refcnt
) {
233 debugfs_remove_recursive(ns
->sdev
->ddir
);
238 debugfs_remove_recursive(ns
->ddir
);
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
);
250 if (!--ns
->sdev
->refcnt
) {
251 debugfs_remove_recursive(ns
->sdev
->ddir
);
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
))
271 u64_stats_update_begin(&ns
->syncp
);
273 ns
->tx_bytes
+= skb
->len
;
274 u64_stats_update_end(&ns
->syncp
);
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
)
299 nsim_get_stats64(struct net_device
*dev
, struct rtnl_link_stats64
*stats
)
301 struct netdevsim
*ns
= netdev_priv(dev
);
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
));
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
);
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
)
325 switch (f
->command
) {
327 return tcf_block_cb_register(f
->block
, nsim_setup_tc_block_cb
,
329 case TC_BLOCK_UNBIND
:
330 tcf_block_cb_unregister(f
->block
, nsim_setup_tc_block_cb
, ns
);
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
))
344 memcpy(ns
->vfconfigs
[vf
].vf_mac
, mac
, ETH_ALEN
);
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)
357 ns
->vfconfigs
[vf
].vlan
= vlan
;
358 ns
->vfconfigs
[vf
].qos
= qos
;
359 ns
->vfconfigs
[vf
].vlan_proto
= vlan_proto
;
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
)
371 ns
->vfconfigs
[vf
].min_tx_rate
= min
;
372 ns
->vfconfigs
[vf
].max_tx_rate
= max
;
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
)
383 ns
->vfconfigs
[vf
].spoofchk_enabled
= val
;
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
)
394 ns
->vfconfigs
[vf
].rss_query_enabled
= val
;
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
)
405 ns
->vfconfigs
[vf
].trusted
= val
;
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
)
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
;
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
)
441 case IFLA_VF_LINK_STATE_AUTO
:
442 case IFLA_VF_LINK_STATE_ENABLE
:
443 case IFLA_VF_LINK_STATE_DISABLE
:
449 ns
->vfconfigs
[vf
].link_state
= state
;
455 nsim_setup_tc(struct net_device
*dev
, enum tc_setup_type type
, void *type_data
)
459 return nsim_setup_tc_block(dev
, type_data
);
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
);
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
,
498 static void nsim_setup(struct net_device
*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
|
511 dev
->features
|= NETIF_F_HIGHDMA
|
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
)
526 if (!is_valid_ether_addr(nla_data(tb
[IFLA_ADDRESS
])))
527 return -EADDRNOTAVAIL
;
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
);
539 struct net_device
*joindev
;
540 struct netdevsim
*joinns
;
542 joindev
= __dev_get_by_index(src_net
,
543 nla_get_u32(tb
[IFLA_LINK
]));
546 if (joindev
->netdev_ops
!= &nsim_netdev_ops
)
549 joinns
= netdev_priv(joindev
);
550 if (!joinns
->sdev
|| !joinns
->sdev
->refcnt
)
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
= {
565 .priv_size
= sizeof(struct netdevsim
),
567 .validate
= nsim_validate
,
568 .newlink
= nsim_newlink
,
569 .dellink
= nsim_dellink
,
572 static int __init
nsim_module_init(void)
576 nsim_ddir
= debugfs_create_dir(DRV_NAME
, NULL
);
577 if (IS_ERR_OR_NULL(nsim_ddir
))
580 nsim_sdev_ddir
= debugfs_create_dir(DRV_NAME
"_sdev", NULL
);
581 if (IS_ERR_OR_NULL(nsim_sdev_ddir
)) {
583 goto err_debugfs_destroy
;
586 err
= bus_register(&nsim_bus
);
588 goto err_sdir_destroy
;
590 err
= nsim_devlink_init();
594 err
= rtnl_link_register(&nsim_link_ops
);
603 bus_unregister(&nsim_bus
);
605 debugfs_remove_recursive(nsim_sdev_ddir
);
607 debugfs_remove_recursive(nsim_ddir
);
611 static void __exit
nsim_module_exit(void)
613 rtnl_link_unregister(&nsim_link_ops
);
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
);