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/u64_stats_sync.h>
23 #include <net/devlink.h>
24 #include <net/udp_tunnel.h>
26 #include <net/macsec.h>
28 #define DRV_NAME "netdevsim"
30 #define NSIM_XDP_MAX_MTU 4000
32 #define NSIM_EA(extack, msg) NL_SET_ERR_MSG_MOD((extack), msg)
34 #define NSIM_IPSEC_MAX_SA_COUNT 33
35 #define NSIM_IPSEC_VALID BIT(31)
36 #define NSIM_UDP_TUNNEL_N_PORTS 4
39 struct xfrm_state *xs;
49 struct nsim_sa sa[NSIM_IPSEC_MAX_SA_COUNT];
56 #define NSIM_MACSEC_MAX_SECY_COUNT 3
57 #define NSIM_MACSEC_MAX_RXSC_COUNT 1
65 struct nsim_rxsc nsim_rxsc[NSIM_MACSEC_MAX_RXSC_COUNT];
71 struct nsim_secy nsim_secy[NSIM_MACSEC_MAX_SECY_COUNT];
75 struct nsim_ethtool_pauseparam {
86 struct nsim_ethtool_pauseparam pauseparam;
87 struct ethtool_coalesce coalesce;
88 struct ethtool_ringparam ring;
89 struct ethtool_fecparam fec;
93 struct net_device *netdev;
94 struct nsim_dev *nsim_dev;
95 struct nsim_dev_port *nsim_dev_port;
99 struct u64_stats_sync syncp;
101 struct nsim_bus_dev *nsim_bus_dev;
103 struct bpf_prog *bpf_offloaded;
104 u32 bpf_offloaded_id;
106 struct xdp_attachment_info xdp;
107 struct xdp_attachment_info xdp_hw;
110 bool bpf_tc_non_bound_accept;
111 bool bpf_xdpdrv_accept;
112 bool bpf_xdpoffload_accept;
115 struct nsim_ipsec ipsec;
116 struct nsim_macsec macsec;
120 u32 __ports[2][NSIM_UDP_TUNNEL_N_PORTS];
121 u32 (*ports)[NSIM_UDP_TUNNEL_N_PORTS];
122 struct debugfs_u32_array dfs_ports[2];
125 struct nsim_ethtool ethtool;
129 nsim_create(struct nsim_dev *nsim_dev, struct nsim_dev_port *nsim_dev_port);
130 void nsim_destroy(struct netdevsim *ns);
132 void nsim_ethtool_init(struct netdevsim *ns);
134 void nsim_udp_tunnels_debugfs_create(struct nsim_dev *nsim_dev);
135 int nsim_udp_tunnels_info_create(struct nsim_dev *nsim_dev,
136 struct net_device *dev);
137 void nsim_udp_tunnels_info_destroy(struct net_device *dev);
139 #ifdef CONFIG_BPF_SYSCALL
140 int nsim_bpf_dev_init(struct nsim_dev *nsim_dev);
141 void nsim_bpf_dev_exit(struct nsim_dev *nsim_dev);
142 int nsim_bpf_init(struct netdevsim *ns);
143 void nsim_bpf_uninit(struct netdevsim *ns);
144 int nsim_bpf(struct net_device *dev, struct netdev_bpf *bpf);
145 int nsim_bpf_disable_tc(struct netdevsim *ns);
146 int nsim_bpf_setup_tc_block_cb(enum tc_setup_type type,
147 void *type_data, void *cb_priv);
150 static inline int nsim_bpf_dev_init(struct nsim_dev *nsim_dev)
155 static inline void nsim_bpf_dev_exit(struct nsim_dev *nsim_dev)
158 static inline int nsim_bpf_init(struct netdevsim *ns)
163 static inline void nsim_bpf_uninit(struct netdevsim *ns)
167 static inline int nsim_bpf(struct net_device *dev, struct netdev_bpf *bpf)
172 static inline int nsim_bpf_disable_tc(struct netdevsim *ns)
178 nsim_bpf_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
185 enum nsim_resource_id {
186 NSIM_RESOURCE_NONE, /* DEVLINK_RESOURCE_ID_PARENT_TOP */
188 NSIM_RESOURCE_IPV4_FIB,
189 NSIM_RESOURCE_IPV4_FIB_RULES,
191 NSIM_RESOURCE_IPV6_FIB,
192 NSIM_RESOURCE_IPV6_FIB_RULES,
193 NSIM_RESOURCE_NEXTHOPS,
196 struct nsim_dev_health {
197 struct devlink_health_reporter *empty_reporter;
198 struct devlink_health_reporter *dummy_reporter;
200 char *recovered_break_msg;
205 int nsim_dev_health_init(struct nsim_dev *nsim_dev, struct devlink *devlink);
206 void nsim_dev_health_exit(struct nsim_dev *nsim_dev);
208 struct nsim_dev_hwstats_netdev {
209 struct list_head list;
210 struct net_device *netdev;
211 struct rtnl_hw_stats64 stats;
216 struct nsim_dev_hwstats {
218 struct dentry *l3_ddir;
220 struct mutex hwsdev_list_lock; /* protects hwsdev list(s) */
221 struct list_head l3_list;
223 struct notifier_block netdevice_nb;
224 struct delayed_work traffic_dw;
227 int nsim_dev_hwstats_init(struct nsim_dev *nsim_dev);
228 void nsim_dev_hwstats_exit(struct nsim_dev *nsim_dev);
230 #if IS_ENABLED(CONFIG_PSAMPLE)
231 int nsim_dev_psample_init(struct nsim_dev *nsim_dev);
232 void nsim_dev_psample_exit(struct nsim_dev *nsim_dev);
234 static inline int nsim_dev_psample_init(struct nsim_dev *nsim_dev)
239 static inline void nsim_dev_psample_exit(struct nsim_dev *nsim_dev)
244 enum nsim_dev_port_type {
245 NSIM_DEV_PORT_TYPE_PF,
246 NSIM_DEV_PORT_TYPE_VF,
249 #define NSIM_DEV_VF_PORT_INDEX_BASE 128
250 #define NSIM_DEV_VF_PORT_INDEX_MAX UINT_MAX
252 struct nsim_dev_port {
253 struct list_head list;
254 struct devlink_port devlink_port;
255 unsigned int port_index;
256 enum nsim_dev_port_type port_type;
258 struct dentry *rate_parent;
260 struct netdevsim *ns;
263 struct nsim_vf_config {
271 bool spoofchk_enabled;
273 bool rss_query_enabled;
277 struct nsim_bus_dev *nsim_bus_dev;
278 struct nsim_fib_data *fib_data;
279 struct nsim_trap_data *trap_data;
281 struct dentry *ports_ddir;
282 struct dentry *take_snapshot;
283 struct dentry *nodes_ddir;
285 struct nsim_vf_config *vfconfigs;
287 struct bpf_offload_dev *bpf_dev;
288 bool bpf_bind_accept;
289 bool bpf_bind_verifier_accept;
290 u32 bpf_bind_verifier_delay;
291 struct dentry *ddir_bpf_bound_progs;
293 struct list_head bpf_bound_progs;
294 struct list_head bpf_bound_maps;
295 struct netdev_phys_item_id switch_id;
296 struct list_head port_list;
297 bool fw_update_status;
298 u32 fw_update_overwrite_mask;
301 bool dont_allow_reload;
303 struct devlink_region *dummy_region;
304 struct nsim_dev_health health;
305 struct nsim_dev_hwstats hwstats;
306 struct flow_action_cookie *fa_cookie;
307 spinlock_t fa_cookie_lock; /* protects fa_cookie */
308 bool fail_trap_group_set;
309 bool fail_trap_policer_set;
310 bool fail_trap_policer_counter_get;
311 bool fail_trap_drop_counter_get;
313 struct udp_tunnel_nic_shared utn_shared;
314 u32 __ports[2][NSIM_UDP_TUNNEL_N_PORTS];
319 bool static_iana_vxlan;
322 struct nsim_dev_psample *psample;
326 static inline bool nsim_esw_mode_is_legacy(struct nsim_dev *nsim_dev)
328 return nsim_dev->esw_mode == DEVLINK_ESWITCH_MODE_LEGACY;
331 static inline bool nsim_esw_mode_is_switchdev(struct nsim_dev *nsim_dev)
333 return nsim_dev->esw_mode == DEVLINK_ESWITCH_MODE_SWITCHDEV;
336 static inline struct net *nsim_dev_net(struct nsim_dev *nsim_dev)
338 return devlink_net(priv_to_devlink(nsim_dev));
341 int nsim_dev_init(void);
342 void nsim_dev_exit(void);
343 int nsim_drv_probe(struct nsim_bus_dev *nsim_bus_dev);
344 void nsim_drv_remove(struct nsim_bus_dev *nsim_bus_dev);
345 int nsim_drv_port_add(struct nsim_bus_dev *nsim_bus_dev,
346 enum nsim_dev_port_type type,
347 unsigned int port_index);
348 int nsim_drv_port_del(struct nsim_bus_dev *nsim_bus_dev,
349 enum nsim_dev_port_type type,
350 unsigned int port_index);
351 int nsim_drv_configure_vfs(struct nsim_bus_dev *nsim_bus_dev,
352 unsigned int num_vfs);
354 unsigned int nsim_dev_get_vfs(struct nsim_dev *nsim_dev);
356 struct nsim_fib_data *nsim_fib_create(struct devlink *devlink,
357 struct netlink_ext_ack *extack);
358 void nsim_fib_destroy(struct devlink *devlink, struct nsim_fib_data *fib_data);
359 u64 nsim_fib_get_val(struct nsim_fib_data *fib_data,
360 enum nsim_resource_id res_id, bool max);
362 static inline bool nsim_dev_port_is_pf(struct nsim_dev_port *nsim_dev_port)
364 return nsim_dev_port->port_type == NSIM_DEV_PORT_TYPE_PF;
367 static inline bool nsim_dev_port_is_vf(struct nsim_dev_port *nsim_dev_port)
369 return nsim_dev_port->port_type == NSIM_DEV_PORT_TYPE_VF;
371 #if IS_ENABLED(CONFIG_XFRM_OFFLOAD)
372 void nsim_ipsec_init(struct netdevsim *ns);
373 void nsim_ipsec_teardown(struct netdevsim *ns);
374 bool nsim_ipsec_tx(struct netdevsim *ns, struct sk_buff *skb);
376 static inline void nsim_ipsec_init(struct netdevsim *ns)
380 static inline void nsim_ipsec_teardown(struct netdevsim *ns)
384 static inline bool nsim_ipsec_tx(struct netdevsim *ns, struct sk_buff *skb)
390 #if IS_ENABLED(CONFIG_MACSEC)
391 void nsim_macsec_init(struct netdevsim *ns);
392 void nsim_macsec_teardown(struct netdevsim *ns);
394 static inline void nsim_macsec_init(struct netdevsim *ns)
398 static inline void nsim_macsec_teardown(struct netdevsim *ns)
403 struct nsim_bus_dev {
405 struct list_head list;
406 unsigned int port_count;
407 unsigned int num_queues; /* Number of queues for each port on this bus */
408 struct net *initial_net; /* Purpose of this is to carry net pointer
409 * during the probe time only.
411 unsigned int max_vfs;
412 unsigned int num_vfs;
416 int nsim_bus_init(void);
417 void nsim_bus_exit(void);