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/device.h>
18 #include <linux/ethtool.h>
19 #include <linux/kernel.h>
20 #include <linux/list.h>
21 #include <linux/netdevice.h>
22 #include <linux/ptp_mock.h>
23 #include <linux/u64_stats_sync.h>
24 #include <net/devlink.h>
25 #include <net/udp_tunnel.h>
27 #include <net/macsec.h>
29 #define DRV_NAME "netdevsim"
31 #define NSIM_XDP_MAX_MTU 4000
33 #define NSIM_EA(extack, msg) NL_SET_ERR_MSG_MOD((extack), msg)
35 #define NSIM_IPSEC_MAX_SA_COUNT 33
36 #define NSIM_IPSEC_VALID BIT(31)
37 #define NSIM_UDP_TUNNEL_N_PORTS 4
40 struct xfrm_state *xs;
50 struct nsim_sa sa[NSIM_IPSEC_MAX_SA_COUNT];
57 #define NSIM_MACSEC_MAX_SECY_COUNT 3
58 #define NSIM_MACSEC_MAX_RXSC_COUNT 1
66 struct nsim_rxsc nsim_rxsc[NSIM_MACSEC_MAX_RXSC_COUNT];
72 struct nsim_secy nsim_secy[NSIM_MACSEC_MAX_SECY_COUNT];
76 struct nsim_ethtool_pauseparam {
87 struct nsim_ethtool_pauseparam pauseparam;
88 struct ethtool_coalesce coalesce;
89 struct ethtool_ringparam ring;
90 struct ethtool_fecparam fec;
94 struct napi_struct napi;
95 struct sk_buff_head skb_queue;
96 struct page_pool *page_pool;
100 struct net_device *netdev;
101 struct nsim_dev *nsim_dev;
102 struct nsim_dev_port *nsim_dev_port;
103 struct mock_phc *phc;
109 struct u64_stats_sync syncp;
111 struct nsim_bus_dev *nsim_bus_dev;
113 struct bpf_prog *bpf_offloaded;
114 u32 bpf_offloaded_id;
116 struct xdp_attachment_info xdp;
117 struct xdp_attachment_info xdp_hw;
120 bool bpf_tc_non_bound_accept;
121 bool bpf_xdpdrv_accept;
122 bool bpf_xdpoffload_accept;
125 struct nsim_ipsec ipsec;
126 struct nsim_macsec macsec;
130 u32 __ports[2][NSIM_UDP_TUNNEL_N_PORTS];
131 u32 (*ports)[NSIM_UDP_TUNNEL_N_PORTS];
132 struct debugfs_u32_array dfs_ports[2];
136 struct dentry *pp_dfs;
138 struct nsim_ethtool ethtool;
139 struct netdevsim __rcu *peer;
143 nsim_create(struct nsim_dev *nsim_dev, struct nsim_dev_port *nsim_dev_port);
144 void nsim_destroy(struct netdevsim *ns);
145 bool netdev_is_nsim(struct net_device *dev);
147 void nsim_ethtool_init(struct netdevsim *ns);
149 void nsim_udp_tunnels_debugfs_create(struct nsim_dev *nsim_dev);
150 int nsim_udp_tunnels_info_create(struct nsim_dev *nsim_dev,
151 struct net_device *dev);
152 void nsim_udp_tunnels_info_destroy(struct net_device *dev);
154 #ifdef CONFIG_BPF_SYSCALL
155 int nsim_bpf_dev_init(struct nsim_dev *nsim_dev);
156 void nsim_bpf_dev_exit(struct nsim_dev *nsim_dev);
157 int nsim_bpf_init(struct netdevsim *ns);
158 void nsim_bpf_uninit(struct netdevsim *ns);
159 int nsim_bpf(struct net_device *dev, struct netdev_bpf *bpf);
160 int nsim_bpf_disable_tc(struct netdevsim *ns);
161 int nsim_bpf_setup_tc_block_cb(enum tc_setup_type type,
162 void *type_data, void *cb_priv);
165 static inline int nsim_bpf_dev_init(struct nsim_dev *nsim_dev)
170 static inline void nsim_bpf_dev_exit(struct nsim_dev *nsim_dev)
173 static inline int nsim_bpf_init(struct netdevsim *ns)
178 static inline void nsim_bpf_uninit(struct netdevsim *ns)
182 static inline int nsim_bpf(struct net_device *dev, struct netdev_bpf *bpf)
187 static inline int nsim_bpf_disable_tc(struct netdevsim *ns)
193 nsim_bpf_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
200 enum nsim_resource_id {
201 NSIM_RESOURCE_NONE, /* DEVLINK_RESOURCE_ID_PARENT_TOP */
203 NSIM_RESOURCE_IPV4_FIB,
204 NSIM_RESOURCE_IPV4_FIB_RULES,
206 NSIM_RESOURCE_IPV6_FIB,
207 NSIM_RESOURCE_IPV6_FIB_RULES,
208 NSIM_RESOURCE_NEXTHOPS,
211 struct nsim_dev_health {
212 struct devlink_health_reporter *empty_reporter;
213 struct devlink_health_reporter *dummy_reporter;
215 char *recovered_break_msg;
220 int nsim_dev_health_init(struct nsim_dev *nsim_dev, struct devlink *devlink);
221 void nsim_dev_health_exit(struct nsim_dev *nsim_dev);
223 struct nsim_dev_hwstats_netdev {
224 struct list_head list;
225 struct net_device *netdev;
226 struct rtnl_hw_stats64 stats;
231 struct nsim_dev_hwstats {
233 struct dentry *l3_ddir;
235 struct mutex hwsdev_list_lock; /* protects hwsdev list(s) */
236 struct list_head l3_list;
238 struct notifier_block netdevice_nb;
239 struct delayed_work traffic_dw;
242 int nsim_dev_hwstats_init(struct nsim_dev *nsim_dev);
243 void nsim_dev_hwstats_exit(struct nsim_dev *nsim_dev);
245 #if IS_ENABLED(CONFIG_PSAMPLE)
246 int nsim_dev_psample_init(struct nsim_dev *nsim_dev);
247 void nsim_dev_psample_exit(struct nsim_dev *nsim_dev);
249 static inline int nsim_dev_psample_init(struct nsim_dev *nsim_dev)
254 static inline void nsim_dev_psample_exit(struct nsim_dev *nsim_dev)
259 enum nsim_dev_port_type {
260 NSIM_DEV_PORT_TYPE_PF,
261 NSIM_DEV_PORT_TYPE_VF,
264 #define NSIM_DEV_VF_PORT_INDEX_BASE 128
265 #define NSIM_DEV_VF_PORT_INDEX_MAX UINT_MAX
267 struct nsim_dev_port {
268 struct list_head list;
269 struct devlink_port devlink_port;
270 unsigned int port_index;
271 enum nsim_dev_port_type port_type;
273 struct dentry *rate_parent;
275 struct netdevsim *ns;
278 struct nsim_vf_config {
286 bool spoofchk_enabled;
288 bool rss_query_enabled;
292 struct nsim_bus_dev *nsim_bus_dev;
293 struct nsim_fib_data *fib_data;
294 struct nsim_trap_data *trap_data;
296 struct dentry *ports_ddir;
297 struct dentry *take_snapshot;
298 struct dentry *nodes_ddir;
300 struct nsim_vf_config *vfconfigs;
302 struct bpf_offload_dev *bpf_dev;
303 bool bpf_bind_accept;
304 bool bpf_bind_verifier_accept;
305 u32 bpf_bind_verifier_delay;
306 struct dentry *ddir_bpf_bound_progs;
308 struct list_head bpf_bound_progs;
309 struct list_head bpf_bound_maps;
310 struct netdev_phys_item_id switch_id;
311 struct list_head port_list;
312 bool fw_update_status;
313 u32 fw_update_overwrite_mask;
316 bool dont_allow_reload;
318 struct devlink_region *dummy_region;
319 struct nsim_dev_health health;
320 struct nsim_dev_hwstats hwstats;
321 struct flow_action_cookie *fa_cookie;
322 spinlock_t fa_cookie_lock; /* protects fa_cookie */
323 bool fail_trap_group_set;
324 bool fail_trap_policer_set;
325 bool fail_trap_policer_counter_get;
326 bool fail_trap_drop_counter_get;
328 struct udp_tunnel_nic_shared utn_shared;
329 u32 __ports[2][NSIM_UDP_TUNNEL_N_PORTS];
334 bool static_iana_vxlan;
337 struct nsim_dev_psample *psample;
341 static inline bool nsim_esw_mode_is_legacy(struct nsim_dev *nsim_dev)
343 return nsim_dev->esw_mode == DEVLINK_ESWITCH_MODE_LEGACY;
346 static inline bool nsim_esw_mode_is_switchdev(struct nsim_dev *nsim_dev)
348 return nsim_dev->esw_mode == DEVLINK_ESWITCH_MODE_SWITCHDEV;
351 static inline struct net *nsim_dev_net(struct nsim_dev *nsim_dev)
353 return devlink_net(priv_to_devlink(nsim_dev));
356 int nsim_dev_init(void);
357 void nsim_dev_exit(void);
358 int nsim_drv_probe(struct nsim_bus_dev *nsim_bus_dev);
359 void nsim_drv_remove(struct nsim_bus_dev *nsim_bus_dev);
360 int nsim_drv_port_add(struct nsim_bus_dev *nsim_bus_dev,
361 enum nsim_dev_port_type type,
362 unsigned int port_index);
363 int nsim_drv_port_del(struct nsim_bus_dev *nsim_bus_dev,
364 enum nsim_dev_port_type type,
365 unsigned int port_index);
366 int nsim_drv_configure_vfs(struct nsim_bus_dev *nsim_bus_dev,
367 unsigned int num_vfs);
369 unsigned int nsim_dev_get_vfs(struct nsim_dev *nsim_dev);
371 struct nsim_fib_data *nsim_fib_create(struct devlink *devlink,
372 struct netlink_ext_ack *extack);
373 void nsim_fib_destroy(struct devlink *devlink, struct nsim_fib_data *fib_data);
374 u64 nsim_fib_get_val(struct nsim_fib_data *fib_data,
375 enum nsim_resource_id res_id, bool max);
377 static inline bool nsim_dev_port_is_pf(struct nsim_dev_port *nsim_dev_port)
379 return nsim_dev_port->port_type == NSIM_DEV_PORT_TYPE_PF;
382 static inline bool nsim_dev_port_is_vf(struct nsim_dev_port *nsim_dev_port)
384 return nsim_dev_port->port_type == NSIM_DEV_PORT_TYPE_VF;
386 #if IS_ENABLED(CONFIG_XFRM_OFFLOAD)
387 void nsim_ipsec_init(struct netdevsim *ns);
388 void nsim_ipsec_teardown(struct netdevsim *ns);
389 bool nsim_ipsec_tx(struct netdevsim *ns, struct sk_buff *skb);
391 static inline void nsim_ipsec_init(struct netdevsim *ns)
395 static inline void nsim_ipsec_teardown(struct netdevsim *ns)
399 static inline bool nsim_ipsec_tx(struct netdevsim *ns, struct sk_buff *skb)
405 #if IS_ENABLED(CONFIG_MACSEC)
406 void nsim_macsec_init(struct netdevsim *ns);
407 void nsim_macsec_teardown(struct netdevsim *ns);
409 static inline void nsim_macsec_init(struct netdevsim *ns)
413 static inline void nsim_macsec_teardown(struct netdevsim *ns)
418 struct nsim_bus_dev {
420 struct list_head list;
421 unsigned int port_count;
422 unsigned int num_queues; /* Number of queues for each port on this bus */
423 struct net *initial_net; /* Purpose of this is to carry net pointer
424 * during the probe time only.
426 unsigned int max_vfs;
427 unsigned int num_vfs;
431 int nsim_bus_init(void);
432 void nsim_bus_exit(void);