xref: /openbmc/linux/drivers/net/netdevsim/netdevsim.h (revision 3d40aed862874db14e1dd41fd6f12636dcfdcc3e)
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/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>
25 #include <net/xdp.h>
26 #include <net/macsec.h>
27 
28 #define DRV_NAME	"netdevsim"
29 
30 #define NSIM_XDP_MAX_MTU	4000
31 
32 #define NSIM_EA(extack, msg)	NL_SET_ERR_MSG_MOD((extack), msg)
33 
34 #define NSIM_IPSEC_MAX_SA_COUNT		33
35 #define NSIM_IPSEC_VALID		BIT(31)
36 #define NSIM_UDP_TUNNEL_N_PORTS		4
37 
38 struct nsim_sa {
39 	struct xfrm_state *xs;
40 	__be32 ipaddr[4];
41 	u32 key[4];
42 	u32 salt;
43 	bool used;
44 	bool crypt;
45 	bool rx;
46 };
47 
48 struct nsim_ipsec {
49 	struct nsim_sa sa[NSIM_IPSEC_MAX_SA_COUNT];
50 	struct dentry *pfile;
51 	u32 count;
52 	u32 tx;
53 	u32 ok;
54 };
55 
56 #define NSIM_MACSEC_MAX_SECY_COUNT 3
57 #define NSIM_MACSEC_MAX_RXSC_COUNT 1
58 struct nsim_rxsc {
59 	sci_t sci;
60 	bool used;
61 };
62 
63 struct nsim_secy {
64 	sci_t sci;
65 	struct nsim_rxsc nsim_rxsc[NSIM_MACSEC_MAX_RXSC_COUNT];
66 	u8 nsim_rxsc_count;
67 	bool used;
68 };
69 
70 struct nsim_macsec {
71 	struct nsim_secy nsim_secy[NSIM_MACSEC_MAX_SECY_COUNT];
72 	u8 nsim_secy_count;
73 };
74 
75 struct nsim_ethtool_pauseparam {
76 	bool rx;
77 	bool tx;
78 	bool report_stats_rx;
79 	bool report_stats_tx;
80 };
81 
82 struct nsim_ethtool {
83 	u32 get_err;
84 	u32 set_err;
85 	u32 channels;
86 	struct nsim_ethtool_pauseparam pauseparam;
87 	struct ethtool_coalesce coalesce;
88 	struct ethtool_ringparam ring;
89 	struct ethtool_fecparam fec;
90 };
91 
92 struct netdevsim {
93 	struct net_device *netdev;
94 	struct nsim_dev *nsim_dev;
95 	struct nsim_dev_port *nsim_dev_port;
96 
97 	u64 tx_packets;
98 	u64 tx_bytes;
99 	struct u64_stats_sync syncp;
100 
101 	struct nsim_bus_dev *nsim_bus_dev;
102 
103 	struct bpf_prog	*bpf_offloaded;
104 	u32 bpf_offloaded_id;
105 
106 	struct xdp_attachment_info xdp;
107 	struct xdp_attachment_info xdp_hw;
108 
109 	bool bpf_tc_accept;
110 	bool bpf_tc_non_bound_accept;
111 	bool bpf_xdpdrv_accept;
112 	bool bpf_xdpoffload_accept;
113 
114 	bool bpf_map_accept;
115 	struct nsim_ipsec ipsec;
116 	struct nsim_macsec macsec;
117 	struct {
118 		u32 inject_error;
119 		u32 sleep;
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];
123 	} udp_ports;
124 
125 	struct nsim_ethtool ethtool;
126 };
127 
128 struct netdevsim *
129 nsim_create(struct nsim_dev *nsim_dev, struct nsim_dev_port *nsim_dev_port);
130 void nsim_destroy(struct netdevsim *ns);
131 
132 void nsim_ethtool_init(struct netdevsim *ns);
133 
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);
138 
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);
148 #else
149 
150 static inline int nsim_bpf_dev_init(struct nsim_dev *nsim_dev)
151 {
152 	return 0;
153 }
154 
155 static inline void nsim_bpf_dev_exit(struct nsim_dev *nsim_dev)
156 {
157 }
158 static inline int nsim_bpf_init(struct netdevsim *ns)
159 {
160 	return 0;
161 }
162 
163 static inline void nsim_bpf_uninit(struct netdevsim *ns)
164 {
165 }
166 
167 static inline int nsim_bpf(struct net_device *dev, struct netdev_bpf *bpf)
168 {
169 	return -EOPNOTSUPP;
170 }
171 
172 static inline int nsim_bpf_disable_tc(struct netdevsim *ns)
173 {
174 	return 0;
175 }
176 
177 static inline int
178 nsim_bpf_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
179 			   void *cb_priv)
180 {
181 	return -EOPNOTSUPP;
182 }
183 #endif
184 
185 enum nsim_resource_id {
186 	NSIM_RESOURCE_NONE,   /* DEVLINK_RESOURCE_ID_PARENT_TOP */
187 	NSIM_RESOURCE_IPV4,
188 	NSIM_RESOURCE_IPV4_FIB,
189 	NSIM_RESOURCE_IPV4_FIB_RULES,
190 	NSIM_RESOURCE_IPV6,
191 	NSIM_RESOURCE_IPV6_FIB,
192 	NSIM_RESOURCE_IPV6_FIB_RULES,
193 	NSIM_RESOURCE_NEXTHOPS,
194 };
195 
196 struct nsim_dev_health {
197 	struct devlink_health_reporter *empty_reporter;
198 	struct devlink_health_reporter *dummy_reporter;
199 	struct dentry *ddir;
200 	char *recovered_break_msg;
201 	u32 binary_len;
202 	bool fail_recover;
203 };
204 
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);
207 
208 struct nsim_dev_hwstats_netdev {
209 	struct list_head list;
210 	struct net_device *netdev;
211 	struct rtnl_hw_stats64 stats;
212 	bool enabled;
213 	bool fail_enable;
214 };
215 
216 struct nsim_dev_hwstats {
217 	struct dentry *ddir;
218 	struct dentry *l3_ddir;
219 
220 	struct mutex hwsdev_list_lock; /* protects hwsdev list(s) */
221 	struct list_head l3_list;
222 
223 	struct notifier_block netdevice_nb;
224 	struct delayed_work traffic_dw;
225 };
226 
227 int nsim_dev_hwstats_init(struct nsim_dev *nsim_dev);
228 void nsim_dev_hwstats_exit(struct nsim_dev *nsim_dev);
229 
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);
233 #else
234 static inline int nsim_dev_psample_init(struct nsim_dev *nsim_dev)
235 {
236 	return 0;
237 }
238 
239 static inline void nsim_dev_psample_exit(struct nsim_dev *nsim_dev)
240 {
241 }
242 #endif
243 
244 enum nsim_dev_port_type {
245 	NSIM_DEV_PORT_TYPE_PF,
246 	NSIM_DEV_PORT_TYPE_VF,
247 };
248 
249 #define NSIM_DEV_VF_PORT_INDEX_BASE 128
250 #define NSIM_DEV_VF_PORT_INDEX_MAX UINT_MAX
251 
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;
257 	struct dentry *ddir;
258 	struct dentry *rate_parent;
259 	char *parent_name;
260 	struct netdevsim *ns;
261 };
262 
263 struct nsim_vf_config {
264 	int link_state;
265 	u16 min_tx_rate;
266 	u16 max_tx_rate;
267 	u16 vlan;
268 	__be16 vlan_proto;
269 	u16 qos;
270 	u8 vf_mac[ETH_ALEN];
271 	bool spoofchk_enabled;
272 	bool trusted;
273 	bool rss_query_enabled;
274 };
275 
276 struct nsim_dev {
277 	struct nsim_bus_dev *nsim_bus_dev;
278 	struct nsim_fib_data *fib_data;
279 	struct nsim_trap_data *trap_data;
280 	struct dentry *ddir;
281 	struct dentry *ports_ddir;
282 	struct dentry *take_snapshot;
283 	struct dentry *nodes_ddir;
284 
285 	struct nsim_vf_config *vfconfigs;
286 
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;
292 	u32 prog_id_gen;
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;
299 	u32 max_macs;
300 	bool test1;
301 	bool dont_allow_reload;
302 	bool fail_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;
312 	struct {
313 		struct udp_tunnel_nic_shared utn_shared;
314 		u32 __ports[2][NSIM_UDP_TUNNEL_N_PORTS];
315 		bool sync_all;
316 		bool open_only;
317 		bool ipv4_only;
318 		bool shared;
319 		bool static_iana_vxlan;
320 		u32 sleep;
321 	} udp_ports;
322 	struct nsim_dev_psample *psample;
323 	u16 esw_mode;
324 };
325 
326 static inline bool nsim_esw_mode_is_legacy(struct nsim_dev *nsim_dev)
327 {
328 	return nsim_dev->esw_mode == DEVLINK_ESWITCH_MODE_LEGACY;
329 }
330 
331 static inline bool nsim_esw_mode_is_switchdev(struct nsim_dev *nsim_dev)
332 {
333 	return nsim_dev->esw_mode == DEVLINK_ESWITCH_MODE_SWITCHDEV;
334 }
335 
336 static inline struct net *nsim_dev_net(struct nsim_dev *nsim_dev)
337 {
338 	return devlink_net(priv_to_devlink(nsim_dev));
339 }
340 
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);
353 
354 unsigned int nsim_dev_get_vfs(struct nsim_dev *nsim_dev);
355 
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);
361 
362 static inline bool nsim_dev_port_is_pf(struct nsim_dev_port *nsim_dev_port)
363 {
364 	return nsim_dev_port->port_type == NSIM_DEV_PORT_TYPE_PF;
365 }
366 
367 static inline bool nsim_dev_port_is_vf(struct nsim_dev_port *nsim_dev_port)
368 {
369 	return nsim_dev_port->port_type == NSIM_DEV_PORT_TYPE_VF;
370 }
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);
375 #else
376 static inline void nsim_ipsec_init(struct netdevsim *ns)
377 {
378 }
379 
380 static inline void nsim_ipsec_teardown(struct netdevsim *ns)
381 {
382 }
383 
384 static inline bool nsim_ipsec_tx(struct netdevsim *ns, struct sk_buff *skb)
385 {
386 	return true;
387 }
388 #endif
389 
390 #if IS_ENABLED(CONFIG_MACSEC)
391 void nsim_macsec_init(struct netdevsim *ns);
392 void nsim_macsec_teardown(struct netdevsim *ns);
393 #else
394 static inline void nsim_macsec_init(struct netdevsim *ns)
395 {
396 }
397 
398 static inline void nsim_macsec_teardown(struct netdevsim *ns)
399 {
400 }
401 #endif
402 
403 struct nsim_bus_dev {
404 	struct device 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.
410 				  */
411 	unsigned int max_vfs;
412 	unsigned int num_vfs;
413 	bool init;
414 };
415 
416 int nsim_bus_init(void);
417 void nsim_bus_exit(void);
418