xref: /linux/drivers/net/netdevsim/netdevsim.h (revision 87320be9f0d24fce67631b7eef919f0b79c3e45c)
1 /*
2  * Copyright (C) 2017 Netronome Systems, Inc.
3  *
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.
7  *
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.
14  */
15 
16 #include <linux/debugfs.h>
17 #include <linux/device.h>
18 #include <linux/ethtool.h>
19 #include <linux/ethtool_netlink.h>
20 #include <linux/kernel.h>
21 #include <linux/if_vlan.h>
22 #include <linux/list.h>
23 #include <linux/netdevice.h>
24 #include <linux/ptp_mock.h>
25 #include <net/devlink.h>
26 #include <net/udp_tunnel.h>
27 #include <net/xdp.h>
28 #include <net/macsec.h>
29 
30 #define DRV_NAME	"netdevsim"
31 
32 #define NSIM_XDP_MAX_MTU	4000
33 
34 #define NSIM_EA(extack, msg)	NL_SET_ERR_MSG_MOD((extack), msg)
35 
36 #define NSIM_IPSEC_MAX_SA_COUNT		33
37 #define NSIM_IPSEC_VALID		BIT(31)
38 #define NSIM_UDP_TUNNEL_N_PORTS		4
39 
40 #define NSIM_HDS_THRESHOLD_MAX		1024
41 
42 struct nsim_sa {
43 	struct xfrm_state *xs;
44 	__be32 ipaddr[4];
45 	u32 key[4];
46 	u32 salt;
47 	bool used;
48 	bool crypt;
49 	bool rx;
50 };
51 
52 struct nsim_ipsec {
53 	struct nsim_sa sa[NSIM_IPSEC_MAX_SA_COUNT];
54 	struct dentry *pfile;
55 	u32 count;
56 	u32 tx;
57 };
58 
59 #define NSIM_MACSEC_MAX_SECY_COUNT 3
60 #define NSIM_MACSEC_MAX_RXSC_COUNT 1
61 struct nsim_rxsc {
62 	sci_t sci;
63 	bool used;
64 };
65 
66 struct nsim_secy {
67 	sci_t sci;
68 	struct nsim_rxsc nsim_rxsc[NSIM_MACSEC_MAX_RXSC_COUNT];
69 	u8 nsim_rxsc_count;
70 	bool used;
71 };
72 
73 struct nsim_macsec {
74 	struct nsim_secy nsim_secy[NSIM_MACSEC_MAX_SECY_COUNT];
75 	u8 nsim_secy_count;
76 };
77 
78 struct nsim_vlan {
79 	DECLARE_BITMAP(ctag, VLAN_N_VID);
80 	DECLARE_BITMAP(stag, VLAN_N_VID);
81 };
82 
83 struct nsim_ethtool_pauseparam {
84 	bool rx;
85 	bool tx;
86 	bool report_stats_rx;
87 	bool report_stats_tx;
88 };
89 
90 struct nsim_ethtool {
91 	u32 get_err;
92 	u32 set_err;
93 	u32 channels;
94 	struct nsim_ethtool_pauseparam pauseparam;
95 	struct ethtool_coalesce coalesce;
96 	struct ethtool_ringparam ring;
97 	struct ethtool_fecparam fec;
98 };
99 
100 struct nsim_rq {
101 	struct napi_struct napi;
102 	struct sk_buff_head skb_queue;
103 	struct page_pool *page_pool;
104 	struct hrtimer napi_timer;
105 };
106 
107 struct netdevsim {
108 	struct net_device *netdev;
109 	struct nsim_dev *nsim_dev;
110 	struct nsim_dev_port *nsim_dev_port;
111 	struct mock_phc *phc;
112 	struct nsim_rq **rq;
113 
114 	int rq_reset_mode;
115 
116 	struct {
117 		atomic64_t rx_packets;
118 		atomic64_t rx_bytes;
119 		atomic64_t tx_packets;
120 		atomic64_t tx_bytes;
121 		struct psp_dev __rcu *dev;
122 		struct dentry *rereg;
123 		struct mutex rereg_lock;
124 		u32 spi;
125 		u32 assoc_cnt;
126 	} psp;
127 
128 	struct nsim_bus_dev *nsim_bus_dev;
129 
130 	struct bpf_prog	*bpf_offloaded;
131 	u32 bpf_offloaded_id;
132 
133 	struct xdp_attachment_info xdp;
134 	struct xdp_attachment_info xdp_hw;
135 
136 	bool bpf_tc_accept;
137 	bool bpf_tc_non_bound_accept;
138 	bool bpf_xdpdrv_accept;
139 	bool bpf_xdpoffload_accept;
140 
141 	bool bpf_map_accept;
142 	struct nsim_ipsec ipsec;
143 	struct nsim_macsec macsec;
144 	struct nsim_vlan vlan;
145 	struct {
146 		u32 inject_error;
147 		u32 __ports[2][NSIM_UDP_TUNNEL_N_PORTS];
148 		u32 (*ports)[NSIM_UDP_TUNNEL_N_PORTS];
149 		struct dentry *ddir;
150 		struct debugfs_u32_array dfs_ports[2];
151 	} udp_ports;
152 
153 	struct page *page;
154 	struct dentry *pp_dfs;
155 	struct dentry *qr_dfs;
156 	struct dentry *vlan_dfs;
157 	struct dentry *ethtool_ddir;
158 
159 	struct nsim_ethtool ethtool;
160 	struct netdevsim __rcu *peer;
161 
162 	struct notifier_block nb;
163 	struct netdev_net_notifier nn;
164 };
165 
166 struct netdevsim *nsim_create(struct nsim_dev *nsim_dev,
167 			      struct nsim_dev_port *nsim_dev_port,
168 			      u8 perm_addr[ETH_ALEN]);
169 void nsim_destroy(struct netdevsim *ns);
170 bool netdev_is_nsim(struct net_device *dev);
171 
172 void nsim_ethtool_init(struct netdevsim *ns);
173 void nsim_ethtool_fini(struct netdevsim *ns);
174 
175 void nsim_udp_tunnels_debugfs_create(struct nsim_dev *nsim_dev);
176 int nsim_udp_tunnels_info_create(struct nsim_dev *nsim_dev,
177 				 struct net_device *dev);
178 void nsim_udp_tunnels_info_destroy(struct net_device *dev);
179 
180 #ifdef CONFIG_BPF_SYSCALL
181 int nsim_bpf_dev_init(struct nsim_dev *nsim_dev);
182 void nsim_bpf_dev_exit(struct nsim_dev *nsim_dev);
183 int nsim_bpf_init(struct netdevsim *ns);
184 void nsim_bpf_uninit(struct netdevsim *ns);
185 int nsim_bpf(struct net_device *dev, struct netdev_bpf *bpf);
186 int nsim_bpf_disable_tc(struct netdevsim *ns);
187 int nsim_bpf_setup_tc_block_cb(enum tc_setup_type type,
188 			       void *type_data, void *cb_priv);
189 #else
190 
nsim_bpf_dev_init(struct nsim_dev * nsim_dev)191 static inline int nsim_bpf_dev_init(struct nsim_dev *nsim_dev)
192 {
193 	return 0;
194 }
195 
nsim_bpf_dev_exit(struct nsim_dev * nsim_dev)196 static inline void nsim_bpf_dev_exit(struct nsim_dev *nsim_dev)
197 {
198 }
nsim_bpf_init(struct netdevsim * ns)199 static inline int nsim_bpf_init(struct netdevsim *ns)
200 {
201 	return 0;
202 }
203 
nsim_bpf_uninit(struct netdevsim * ns)204 static inline void nsim_bpf_uninit(struct netdevsim *ns)
205 {
206 }
207 
nsim_bpf(struct net_device * dev,struct netdev_bpf * bpf)208 static inline int nsim_bpf(struct net_device *dev, struct netdev_bpf *bpf)
209 {
210 	return -EOPNOTSUPP;
211 }
212 
nsim_bpf_disable_tc(struct netdevsim * ns)213 static inline int nsim_bpf_disable_tc(struct netdevsim *ns)
214 {
215 	return 0;
216 }
217 
218 static inline int
nsim_bpf_setup_tc_block_cb(enum tc_setup_type type,void * type_data,void * cb_priv)219 nsim_bpf_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
220 			   void *cb_priv)
221 {
222 	return -EOPNOTSUPP;
223 }
224 #endif
225 
226 enum nsim_resource_id {
227 	NSIM_RESOURCE_NONE,   /* DEVLINK_RESOURCE_ID_PARENT_TOP */
228 	NSIM_RESOURCE_IPV4,
229 	NSIM_RESOURCE_IPV4_FIB,
230 	NSIM_RESOURCE_IPV4_FIB_RULES,
231 	NSIM_RESOURCE_IPV6,
232 	NSIM_RESOURCE_IPV6_FIB,
233 	NSIM_RESOURCE_IPV6_FIB_RULES,
234 	NSIM_RESOURCE_NEXTHOPS,
235 };
236 
237 enum nsim_port_resource_id {
238 	NSIM_PORT_RESOURCE_TEST = 1,
239 };
240 
241 struct nsim_dev_health {
242 	struct devlink_health_reporter *empty_reporter;
243 	struct devlink_health_reporter *dummy_reporter;
244 	struct dentry *ddir;
245 	char *recovered_break_msg;
246 	u32 binary_len;
247 	bool fail_recover;
248 };
249 
250 int nsim_dev_health_init(struct nsim_dev *nsim_dev, struct devlink *devlink);
251 void nsim_dev_health_exit(struct nsim_dev *nsim_dev);
252 
253 struct nsim_dev_hwstats_netdev {
254 	struct list_head list;
255 	struct net_device *netdev;
256 	struct rtnl_hw_stats64 stats;
257 	bool enabled;
258 	bool fail_enable;
259 };
260 
261 struct nsim_dev_hwstats {
262 	struct dentry *ddir;
263 	struct dentry *l3_ddir;
264 
265 	struct mutex hwsdev_list_lock; /* protects hwsdev list(s) */
266 	struct list_head l3_list;
267 
268 	struct notifier_block netdevice_nb;
269 	struct delayed_work traffic_dw;
270 };
271 
272 int nsim_dev_hwstats_init(struct nsim_dev *nsim_dev);
273 void nsim_dev_hwstats_exit(struct nsim_dev *nsim_dev);
274 
275 #if IS_ENABLED(CONFIG_PSAMPLE)
276 int nsim_dev_psample_init(struct nsim_dev *nsim_dev);
277 void nsim_dev_psample_exit(struct nsim_dev *nsim_dev);
278 #else
nsim_dev_psample_init(struct nsim_dev * nsim_dev)279 static inline int nsim_dev_psample_init(struct nsim_dev *nsim_dev)
280 {
281 	return 0;
282 }
283 
nsim_dev_psample_exit(struct nsim_dev * nsim_dev)284 static inline void nsim_dev_psample_exit(struct nsim_dev *nsim_dev)
285 {
286 }
287 #endif
288 
289 enum nsim_dev_port_type {
290 	NSIM_DEV_PORT_TYPE_PF,
291 	NSIM_DEV_PORT_TYPE_VF,
292 };
293 
294 #define NSIM_DEV_VF_PORT_INDEX_BASE 128
295 #define NSIM_DEV_VF_PORT_INDEX_MAX UINT_MAX
296 
297 struct nsim_dev_port {
298 	struct list_head list;
299 	struct devlink_port devlink_port;
300 	unsigned int port_index;
301 	enum nsim_dev_port_type port_type;
302 	struct dentry *ddir;
303 	struct dentry *rate_parent;
304 	char *parent_name;
305 	u32 tc_bw[DEVLINK_RATE_TCS_MAX];
306 	struct netdevsim *ns;
307 };
308 
309 struct nsim_vf_config {
310 	int link_state;
311 	u16 min_tx_rate;
312 	u16 max_tx_rate;
313 	u16 vlan;
314 	__be16 vlan_proto;
315 	u16 qos;
316 	u8 vf_mac[ETH_ALEN];
317 	bool spoofchk_enabled;
318 	bool trusted;
319 	bool rss_query_enabled;
320 };
321 
322 struct nsim_dev {
323 	struct nsim_bus_dev *nsim_bus_dev;
324 	struct nsim_fib_data *fib_data;
325 	struct nsim_trap_data *trap_data;
326 	struct dentry *ddir;
327 	struct dentry *ports_ddir;
328 	struct dentry *take_snapshot;
329 	struct dentry *nodes_ddir;
330 
331 	struct nsim_vf_config *vfconfigs;
332 
333 	struct bpf_offload_dev *bpf_dev;
334 	bool bpf_bind_accept;
335 	bool bpf_bind_verifier_accept;
336 	u32 bpf_bind_verifier_delay;
337 	struct dentry *ddir_bpf_bound_progs;
338 	u32 prog_id_gen;
339 	struct list_head bpf_bound_progs;
340 	struct list_head bpf_bound_maps;
341 	struct mutex progs_list_lock;
342 	struct netdev_phys_item_id switch_id;
343 	struct list_head port_list;
344 	bool fw_update_status;
345 	u32 fw_update_overwrite_mask;
346 	u32 fw_update_flash_chunk_time_ms;
347 	u32 max_macs;
348 	bool test1;
349 	u32 test2;
350 	bool dont_allow_reload;
351 	bool fail_reload;
352 	struct devlink_region *dummy_region;
353 	struct nsim_dev_health health;
354 	struct nsim_dev_hwstats hwstats;
355 	struct flow_action_cookie *fa_cookie;
356 	spinlock_t fa_cookie_lock; /* protects fa_cookie */
357 	bool fail_trap_group_set;
358 	bool fail_trap_policer_set;
359 	bool fail_trap_policer_counter_get;
360 	bool fail_trap_drop_counter_get;
361 	struct {
362 		struct udp_tunnel_nic_shared utn_shared;
363 		u32 __ports[2][NSIM_UDP_TUNNEL_N_PORTS];
364 		bool sync_all;
365 		bool open_only;
366 		bool ipv4_only;
367 		bool shared;
368 		bool static_iana_vxlan;
369 	} udp_ports;
370 	struct nsim_dev_psample *psample;
371 	u16 esw_mode;
372 };
373 
nsim_esw_mode_is_legacy(struct nsim_dev * nsim_dev)374 static inline bool nsim_esw_mode_is_legacy(struct nsim_dev *nsim_dev)
375 {
376 	return nsim_dev->esw_mode == DEVLINK_ESWITCH_MODE_LEGACY;
377 }
378 
nsim_esw_mode_is_switchdev(struct nsim_dev * nsim_dev)379 static inline bool nsim_esw_mode_is_switchdev(struct nsim_dev *nsim_dev)
380 {
381 	return nsim_dev->esw_mode == DEVLINK_ESWITCH_MODE_SWITCHDEV;
382 }
383 
nsim_dev_net(struct nsim_dev * nsim_dev)384 static inline struct net *nsim_dev_net(struct nsim_dev *nsim_dev)
385 {
386 	return devlink_net(priv_to_devlink(nsim_dev));
387 }
388 
389 int nsim_dev_init(void);
390 void nsim_dev_exit(void);
391 int nsim_drv_probe(struct nsim_bus_dev *nsim_bus_dev);
392 void nsim_drv_remove(struct nsim_bus_dev *nsim_bus_dev);
393 int nsim_drv_port_add(struct nsim_bus_dev *nsim_bus_dev,
394 		      enum nsim_dev_port_type type, unsigned int port_index,
395 		      u8 perm_addr[ETH_ALEN]);
396 int nsim_drv_port_del(struct nsim_bus_dev *nsim_bus_dev,
397 		      enum nsim_dev_port_type type,
398 		      unsigned int port_index);
399 int nsim_drv_configure_vfs(struct nsim_bus_dev *nsim_bus_dev,
400 			   unsigned int num_vfs);
401 
402 unsigned int nsim_dev_get_vfs(struct nsim_dev *nsim_dev);
403 
404 struct nsim_fib_data *nsim_fib_create(struct devlink *devlink,
405 				      struct netlink_ext_ack *extack);
406 void nsim_fib_destroy(struct devlink *devlink, struct nsim_fib_data *fib_data);
407 u64 nsim_fib_get_val(struct nsim_fib_data *fib_data,
408 		     enum nsim_resource_id res_id, bool max);
409 
nsim_dev_port_is_pf(struct nsim_dev_port * nsim_dev_port)410 static inline bool nsim_dev_port_is_pf(struct nsim_dev_port *nsim_dev_port)
411 {
412 	return nsim_dev_port->port_type == NSIM_DEV_PORT_TYPE_PF;
413 }
414 
nsim_dev_port_is_vf(struct nsim_dev_port * nsim_dev_port)415 static inline bool nsim_dev_port_is_vf(struct nsim_dev_port *nsim_dev_port)
416 {
417 	return nsim_dev_port->port_type == NSIM_DEV_PORT_TYPE_VF;
418 }
419 #if IS_ENABLED(CONFIG_XFRM_OFFLOAD)
420 void nsim_ipsec_init(struct netdevsim *ns);
421 void nsim_ipsec_teardown(struct netdevsim *ns);
422 bool nsim_ipsec_tx(struct netdevsim *ns, struct sk_buff *skb);
423 #else
nsim_ipsec_init(struct netdevsim * ns)424 static inline void nsim_ipsec_init(struct netdevsim *ns)
425 {
426 }
427 
nsim_ipsec_teardown(struct netdevsim * ns)428 static inline void nsim_ipsec_teardown(struct netdevsim *ns)
429 {
430 }
431 
nsim_ipsec_tx(struct netdevsim * ns,struct sk_buff * skb)432 static inline bool nsim_ipsec_tx(struct netdevsim *ns, struct sk_buff *skb)
433 {
434 	return true;
435 }
436 #endif
437 
438 #if IS_ENABLED(CONFIG_MACSEC)
439 void nsim_macsec_init(struct netdevsim *ns);
440 void nsim_macsec_teardown(struct netdevsim *ns);
441 #else
nsim_macsec_init(struct netdevsim * ns)442 static inline void nsim_macsec_init(struct netdevsim *ns)
443 {
444 }
445 
nsim_macsec_teardown(struct netdevsim * ns)446 static inline void nsim_macsec_teardown(struct netdevsim *ns)
447 {
448 }
449 #endif
450 
451 #if IS_ENABLED(CONFIG_INET_PSP)
452 int nsim_psp_init(struct netdevsim *ns);
453 void nsim_psp_uninit(struct netdevsim *ns);
454 void nsim_psp_handle_ext(struct sk_buff *skb, struct skb_ext *psp_ext);
455 enum skb_drop_reason
456 nsim_do_psp(struct sk_buff *skb, struct netdevsim *ns,
457 	    struct netdevsim *peer_ns, struct skb_ext **psp_ext);
458 #else
nsim_psp_init(struct netdevsim * ns)459 static inline int nsim_psp_init(struct netdevsim *ns) { return 0; }
nsim_psp_uninit(struct netdevsim * ns)460 static inline void nsim_psp_uninit(struct netdevsim *ns) {}
461 static inline enum skb_drop_reason
nsim_do_psp(struct sk_buff * skb,struct netdevsim * ns,struct netdevsim * peer_ns,struct skb_ext ** psp_ext)462 nsim_do_psp(struct sk_buff *skb, struct netdevsim *ns,
463 	    struct netdevsim *peer_ns, struct skb_ext **psp_ext)
464 {
465 	return 0;
466 }
467 
468 static inline void
nsim_psp_handle_ext(struct sk_buff * skb,struct skb_ext * psp_ext)469 nsim_psp_handle_ext(struct sk_buff *skb, struct skb_ext *psp_ext) {}
470 #endif
471 
472 int nsim_setup_tc(struct net_device *dev, enum tc_setup_type type,
473 		  void *type_data);
474 
475 struct nsim_bus_dev {
476 	struct device dev;
477 	struct list_head list;
478 	unsigned int port_count;
479 	unsigned int num_queues; /* Number of queues for each port on this bus */
480 	struct net *initial_net; /* Purpose of this is to carry net pointer
481 				  * during the probe time only.
482 				  */
483 	unsigned int max_vfs;
484 	unsigned int num_vfs;
485 	bool init;
486 };
487 
488 int nsim_bus_init(void);
489 void nsim_bus_exit(void);
490