Linux Audio

Check our new training course

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