xref: /linux/drivers/net/netdevsim/netdevsim.h (revision 35c2c39832e569449b9192fa1afbbc4c66227af7)
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 *dev;
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 
158 	struct nsim_ethtool ethtool;
159 	struct netdevsim __rcu *peer;
160 
161 	struct notifier_block nb;
162 	struct netdev_net_notifier nn;
163 };
164 
165 struct netdevsim *nsim_create(struct nsim_dev *nsim_dev,
166 			      struct nsim_dev_port *nsim_dev_port,
167 			      u8 perm_addr[ETH_ALEN]);
168 void nsim_destroy(struct netdevsim *ns);
169 bool netdev_is_nsim(struct net_device *dev);
170 
171 void nsim_ethtool_init(struct netdevsim *ns);
172 
173 void nsim_udp_tunnels_debugfs_create(struct nsim_dev *nsim_dev);
174 int nsim_udp_tunnels_info_create(struct nsim_dev *nsim_dev,
175 				 struct net_device *dev);
176 void nsim_udp_tunnels_info_destroy(struct net_device *dev);
177 
178 #ifdef CONFIG_BPF_SYSCALL
179 int nsim_bpf_dev_init(struct nsim_dev *nsim_dev);
180 void nsim_bpf_dev_exit(struct nsim_dev *nsim_dev);
181 int nsim_bpf_init(struct netdevsim *ns);
182 void nsim_bpf_uninit(struct netdevsim *ns);
183 int nsim_bpf(struct net_device *dev, struct netdev_bpf *bpf);
184 int nsim_bpf_disable_tc(struct netdevsim *ns);
185 int nsim_bpf_setup_tc_block_cb(enum tc_setup_type type,
186 			       void *type_data, void *cb_priv);
187 #else
188 
189 static inline int nsim_bpf_dev_init(struct nsim_dev *nsim_dev)
190 {
191 	return 0;
192 }
193 
194 static inline void nsim_bpf_dev_exit(struct nsim_dev *nsim_dev)
195 {
196 }
197 static inline int nsim_bpf_init(struct netdevsim *ns)
198 {
199 	return 0;
200 }
201 
202 static inline void nsim_bpf_uninit(struct netdevsim *ns)
203 {
204 }
205 
206 static inline int nsim_bpf(struct net_device *dev, struct netdev_bpf *bpf)
207 {
208 	return -EOPNOTSUPP;
209 }
210 
211 static inline int nsim_bpf_disable_tc(struct netdevsim *ns)
212 {
213 	return 0;
214 }
215 
216 static inline int
217 nsim_bpf_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
218 			   void *cb_priv)
219 {
220 	return -EOPNOTSUPP;
221 }
222 #endif
223 
224 enum nsim_resource_id {
225 	NSIM_RESOURCE_NONE,   /* DEVLINK_RESOURCE_ID_PARENT_TOP */
226 	NSIM_RESOURCE_IPV4,
227 	NSIM_RESOURCE_IPV4_FIB,
228 	NSIM_RESOURCE_IPV4_FIB_RULES,
229 	NSIM_RESOURCE_IPV6,
230 	NSIM_RESOURCE_IPV6_FIB,
231 	NSIM_RESOURCE_IPV6_FIB_RULES,
232 	NSIM_RESOURCE_NEXTHOPS,
233 };
234 
235 enum nsim_port_resource_id {
236 	NSIM_PORT_RESOURCE_TEST = 1,
237 };
238 
239 struct nsim_dev_health {
240 	struct devlink_health_reporter *empty_reporter;
241 	struct devlink_health_reporter *dummy_reporter;
242 	struct dentry *ddir;
243 	char *recovered_break_msg;
244 	u32 binary_len;
245 	bool fail_recover;
246 };
247 
248 int nsim_dev_health_init(struct nsim_dev *nsim_dev, struct devlink *devlink);
249 void nsim_dev_health_exit(struct nsim_dev *nsim_dev);
250 
251 struct nsim_dev_hwstats_netdev {
252 	struct list_head list;
253 	struct net_device *netdev;
254 	struct rtnl_hw_stats64 stats;
255 	bool enabled;
256 	bool fail_enable;
257 };
258 
259 struct nsim_dev_hwstats {
260 	struct dentry *ddir;
261 	struct dentry *l3_ddir;
262 
263 	struct mutex hwsdev_list_lock; /* protects hwsdev list(s) */
264 	struct list_head l3_list;
265 
266 	struct notifier_block netdevice_nb;
267 	struct delayed_work traffic_dw;
268 };
269 
270 int nsim_dev_hwstats_init(struct nsim_dev *nsim_dev);
271 void nsim_dev_hwstats_exit(struct nsim_dev *nsim_dev);
272 
273 #if IS_ENABLED(CONFIG_PSAMPLE)
274 int nsim_dev_psample_init(struct nsim_dev *nsim_dev);
275 void nsim_dev_psample_exit(struct nsim_dev *nsim_dev);
276 #else
277 static inline int nsim_dev_psample_init(struct nsim_dev *nsim_dev)
278 {
279 	return 0;
280 }
281 
282 static inline void nsim_dev_psample_exit(struct nsim_dev *nsim_dev)
283 {
284 }
285 #endif
286 
287 enum nsim_dev_port_type {
288 	NSIM_DEV_PORT_TYPE_PF,
289 	NSIM_DEV_PORT_TYPE_VF,
290 };
291 
292 #define NSIM_DEV_VF_PORT_INDEX_BASE 128
293 #define NSIM_DEV_VF_PORT_INDEX_MAX UINT_MAX
294 
295 struct nsim_dev_port {
296 	struct list_head list;
297 	struct devlink_port devlink_port;
298 	unsigned int port_index;
299 	enum nsim_dev_port_type port_type;
300 	struct dentry *ddir;
301 	struct dentry *rate_parent;
302 	char *parent_name;
303 	u32 tc_bw[DEVLINK_RATE_TCS_MAX];
304 	struct netdevsim *ns;
305 };
306 
307 struct nsim_vf_config {
308 	int link_state;
309 	u16 min_tx_rate;
310 	u16 max_tx_rate;
311 	u16 vlan;
312 	__be16 vlan_proto;
313 	u16 qos;
314 	u8 vf_mac[ETH_ALEN];
315 	bool spoofchk_enabled;
316 	bool trusted;
317 	bool rss_query_enabled;
318 };
319 
320 struct nsim_dev {
321 	struct nsim_bus_dev *nsim_bus_dev;
322 	struct nsim_fib_data *fib_data;
323 	struct nsim_trap_data *trap_data;
324 	struct dentry *ddir;
325 	struct dentry *ports_ddir;
326 	struct dentry *take_snapshot;
327 	struct dentry *nodes_ddir;
328 
329 	struct nsim_vf_config *vfconfigs;
330 
331 	struct bpf_offload_dev *bpf_dev;
332 	bool bpf_bind_accept;
333 	bool bpf_bind_verifier_accept;
334 	u32 bpf_bind_verifier_delay;
335 	struct dentry *ddir_bpf_bound_progs;
336 	u32 prog_id_gen;
337 	struct list_head bpf_bound_progs;
338 	struct list_head bpf_bound_maps;
339 	struct mutex progs_list_lock;
340 	struct netdev_phys_item_id switch_id;
341 	struct list_head port_list;
342 	bool fw_update_status;
343 	u32 fw_update_overwrite_mask;
344 	u32 fw_update_flash_chunk_time_ms;
345 	u32 max_macs;
346 	bool test1;
347 	u32 test2;
348 	bool dont_allow_reload;
349 	bool fail_reload;
350 	struct devlink_region *dummy_region;
351 	struct nsim_dev_health health;
352 	struct nsim_dev_hwstats hwstats;
353 	struct flow_action_cookie *fa_cookie;
354 	spinlock_t fa_cookie_lock; /* protects fa_cookie */
355 	bool fail_trap_group_set;
356 	bool fail_trap_policer_set;
357 	bool fail_trap_policer_counter_get;
358 	bool fail_trap_drop_counter_get;
359 	struct {
360 		struct udp_tunnel_nic_shared utn_shared;
361 		u32 __ports[2][NSIM_UDP_TUNNEL_N_PORTS];
362 		bool sync_all;
363 		bool open_only;
364 		bool ipv4_only;
365 		bool shared;
366 		bool static_iana_vxlan;
367 	} udp_ports;
368 	struct nsim_dev_psample *psample;
369 	u16 esw_mode;
370 };
371 
372 static inline bool nsim_esw_mode_is_legacy(struct nsim_dev *nsim_dev)
373 {
374 	return nsim_dev->esw_mode == DEVLINK_ESWITCH_MODE_LEGACY;
375 }
376 
377 static inline bool nsim_esw_mode_is_switchdev(struct nsim_dev *nsim_dev)
378 {
379 	return nsim_dev->esw_mode == DEVLINK_ESWITCH_MODE_SWITCHDEV;
380 }
381 
382 static inline struct net *nsim_dev_net(struct nsim_dev *nsim_dev)
383 {
384 	return devlink_net(priv_to_devlink(nsim_dev));
385 }
386 
387 int nsim_dev_init(void);
388 void nsim_dev_exit(void);
389 int nsim_drv_probe(struct nsim_bus_dev *nsim_bus_dev);
390 void nsim_drv_remove(struct nsim_bus_dev *nsim_bus_dev);
391 int nsim_drv_port_add(struct nsim_bus_dev *nsim_bus_dev,
392 		      enum nsim_dev_port_type type, unsigned int port_index,
393 		      u8 perm_addr[ETH_ALEN]);
394 int nsim_drv_port_del(struct nsim_bus_dev *nsim_bus_dev,
395 		      enum nsim_dev_port_type type,
396 		      unsigned int port_index);
397 int nsim_drv_configure_vfs(struct nsim_bus_dev *nsim_bus_dev,
398 			   unsigned int num_vfs);
399 
400 unsigned int nsim_dev_get_vfs(struct nsim_dev *nsim_dev);
401 
402 struct nsim_fib_data *nsim_fib_create(struct devlink *devlink,
403 				      struct netlink_ext_ack *extack);
404 void nsim_fib_destroy(struct devlink *devlink, struct nsim_fib_data *fib_data);
405 u64 nsim_fib_get_val(struct nsim_fib_data *fib_data,
406 		     enum nsim_resource_id res_id, bool max);
407 
408 static inline bool nsim_dev_port_is_pf(struct nsim_dev_port *nsim_dev_port)
409 {
410 	return nsim_dev_port->port_type == NSIM_DEV_PORT_TYPE_PF;
411 }
412 
413 static inline bool nsim_dev_port_is_vf(struct nsim_dev_port *nsim_dev_port)
414 {
415 	return nsim_dev_port->port_type == NSIM_DEV_PORT_TYPE_VF;
416 }
417 #if IS_ENABLED(CONFIG_XFRM_OFFLOAD)
418 void nsim_ipsec_init(struct netdevsim *ns);
419 void nsim_ipsec_teardown(struct netdevsim *ns);
420 bool nsim_ipsec_tx(struct netdevsim *ns, struct sk_buff *skb);
421 #else
422 static inline void nsim_ipsec_init(struct netdevsim *ns)
423 {
424 }
425 
426 static inline void nsim_ipsec_teardown(struct netdevsim *ns)
427 {
428 }
429 
430 static inline bool nsim_ipsec_tx(struct netdevsim *ns, struct sk_buff *skb)
431 {
432 	return true;
433 }
434 #endif
435 
436 #if IS_ENABLED(CONFIG_MACSEC)
437 void nsim_macsec_init(struct netdevsim *ns);
438 void nsim_macsec_teardown(struct netdevsim *ns);
439 #else
440 static inline void nsim_macsec_init(struct netdevsim *ns)
441 {
442 }
443 
444 static inline void nsim_macsec_teardown(struct netdevsim *ns)
445 {
446 }
447 #endif
448 
449 #if IS_ENABLED(CONFIG_INET_PSP)
450 int nsim_psp_init(struct netdevsim *ns);
451 void nsim_psp_uninit(struct netdevsim *ns);
452 void nsim_psp_handle_ext(struct sk_buff *skb, struct skb_ext *psp_ext);
453 enum skb_drop_reason
454 nsim_do_psp(struct sk_buff *skb, struct netdevsim *ns,
455 	    struct netdevsim *peer_ns, struct skb_ext **psp_ext);
456 #else
457 static inline int nsim_psp_init(struct netdevsim *ns) { return 0; }
458 static inline void nsim_psp_uninit(struct netdevsim *ns) {}
459 static inline enum skb_drop_reason
460 nsim_do_psp(struct sk_buff *skb, struct netdevsim *ns,
461 	    struct netdevsim *peer_ns, struct skb_ext **psp_ext)
462 {
463 	return 0;
464 }
465 
466 static inline void
467 nsim_psp_handle_ext(struct sk_buff *skb, struct skb_ext *psp_ext) {}
468 #endif
469 
470 int nsim_setup_tc(struct net_device *dev, enum tc_setup_type type,
471 		  void *type_data);
472 
473 struct nsim_bus_dev {
474 	struct device dev;
475 	struct list_head list;
476 	unsigned int port_count;
477 	unsigned int num_queues; /* Number of queues for each port on this bus */
478 	struct net *initial_net; /* Purpose of this is to carry net pointer
479 				  * during the probe time only.
480 				  */
481 	unsigned int max_vfs;
482 	unsigned int num_vfs;
483 	bool init;
484 };
485 
486 int nsim_bus_init(void);
487 void nsim_bus_exit(void);
488