xref: /linux/drivers/net/netdevsim/netdevsim.h (revision fcee7d82f27d6a8b1ddc5bbefda59b4e441e9bc0)
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 <linux/u64_stats_sync.h>
26 #include <net/devlink.h>
27 #include <net/udp_tunnel.h>
28 #include <net/xdp.h>
29 #include <net/macsec.h>
30 
31 #define DRV_NAME	"netdevsim"
32 
33 #define NSIM_XDP_MAX_MTU	4000
34 
35 #define NSIM_EA(extack, msg)	NL_SET_ERR_MSG_MOD((extack), msg)
36 
37 #define NSIM_IPSEC_MAX_SA_COUNT		33
38 #define NSIM_IPSEC_VALID		BIT(31)
39 #define NSIM_UDP_TUNNEL_N_PORTS		4
40 
41 #define NSIM_HDS_THRESHOLD_MAX		1024
42 
43 struct nsim_sa {
44 	struct xfrm_state *xs;
45 	__be32 ipaddr[4];
46 	u32 key[4];
47 	u32 salt;
48 	bool used;
49 	bool crypt;
50 	bool rx;
51 };
52 
53 struct nsim_ipsec {
54 	struct nsim_sa sa[NSIM_IPSEC_MAX_SA_COUNT];
55 	struct dentry *pfile;
56 	u32 count;
57 	u32 tx;
58 };
59 
60 #define NSIM_MACSEC_MAX_SECY_COUNT 3
61 #define NSIM_MACSEC_MAX_RXSC_COUNT 1
62 struct nsim_rxsc {
63 	sci_t sci;
64 	bool used;
65 };
66 
67 struct nsim_secy {
68 	sci_t sci;
69 	struct nsim_rxsc nsim_rxsc[NSIM_MACSEC_MAX_RXSC_COUNT];
70 	u8 nsim_rxsc_count;
71 	bool used;
72 };
73 
74 struct nsim_macsec {
75 	struct nsim_secy nsim_secy[NSIM_MACSEC_MAX_SECY_COUNT];
76 	u8 nsim_secy_count;
77 };
78 
79 struct nsim_vlan {
80 	DECLARE_BITMAP(ctag, VLAN_N_VID);
81 	DECLARE_BITMAP(stag, VLAN_N_VID);
82 };
83 
84 struct nsim_ethtool_pauseparam {
85 	bool rx;
86 	bool tx;
87 	bool report_stats_rx;
88 	bool report_stats_tx;
89 };
90 
91 struct nsim_ethtool {
92 	u32 get_err;
93 	u32 set_err;
94 	u32 channels;
95 	struct nsim_ethtool_pauseparam pauseparam;
96 	struct ethtool_coalesce coalesce;
97 	struct ethtool_ringparam ring;
98 	struct ethtool_fecparam fec;
99 };
100 
101 struct nsim_rq {
102 	struct napi_struct napi;
103 	struct sk_buff_head skb_queue;
104 	struct page_pool *page_pool;
105 	struct hrtimer napi_timer;
106 };
107 
108 struct netdevsim {
109 	struct net_device *netdev;
110 	struct nsim_dev *nsim_dev;
111 	struct nsim_dev_port *nsim_dev_port;
112 	struct mock_phc *phc;
113 	struct nsim_rq **rq;
114 
115 	int rq_reset_mode;
116 
117 	struct {
118 		u64_stats_t rx_packets;
119 		u64_stats_t rx_bytes;
120 		u64_stats_t tx_packets;
121 		u64_stats_t tx_bytes;
122 		struct u64_stats_sync syncp;
123 		struct psp_dev __rcu *dev;
124 		struct dentry *rereg;
125 		struct mutex rereg_lock;
126 		u32 spi;
127 		u32 assoc_cnt;
128 	} psp;
129 
130 	struct nsim_bus_dev *nsim_bus_dev;
131 
132 	struct bpf_prog	*bpf_offloaded;
133 	u32 bpf_offloaded_id;
134 
135 	struct xdp_attachment_info xdp;
136 	struct xdp_attachment_info xdp_hw;
137 
138 	bool bpf_tc_accept;
139 	bool bpf_tc_non_bound_accept;
140 	bool bpf_xdpdrv_accept;
141 	bool bpf_xdpoffload_accept;
142 
143 	bool bpf_map_accept;
144 	struct nsim_ipsec ipsec;
145 	struct nsim_macsec macsec;
146 	struct nsim_vlan vlan;
147 	struct {
148 		u32 inject_error;
149 		u32 __ports[2][NSIM_UDP_TUNNEL_N_PORTS];
150 		u32 (*ports)[NSIM_UDP_TUNNEL_N_PORTS];
151 		struct dentry *ddir;
152 		struct debugfs_u32_array dfs_ports[2];
153 	} udp_ports;
154 
155 	struct page *page;
156 	struct dentry *pp_dfs;
157 	struct dentry *qr_dfs;
158 	struct dentry *vlan_dfs;
159 
160 	struct nsim_ethtool ethtool;
161 	struct netdevsim __rcu *peer;
162 
163 	struct notifier_block nb;
164 	struct netdev_net_notifier nn;
165 };
166 
167 struct netdevsim *nsim_create(struct nsim_dev *nsim_dev,
168 			      struct nsim_dev_port *nsim_dev_port,
169 			      u8 perm_addr[ETH_ALEN]);
170 void nsim_destroy(struct netdevsim *ns);
171 bool netdev_is_nsim(struct net_device *dev);
172 
173 void nsim_ethtool_init(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