2 * Copyright (C) 2017 Netronome Systems, Inc.
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
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.
16 #include <linux/device.h>
17 #include <linux/kernel.h>
18 #include <linux/list.h>
19 #include <linux/netdevice.h>
20 #include <linux/u64_stats_sync.h>
21 #include <net/devlink.h>
24 #define DRV_NAME "netdevsim"
26 #define NSIM_XDP_MAX_MTU 4000
28 #define NSIM_EA(extack, msg) NL_SET_ERR_MSG_MOD((extack), msg)
30 #define NSIM_IPSEC_MAX_SA_COUNT 33
31 #define NSIM_IPSEC_VALID BIT(31)
34 struct xfrm_state
*xs
;
44 struct nsim_sa sa
[NSIM_IPSEC_MAX_SA_COUNT
];
52 struct net_device
*netdev
;
53 struct nsim_dev
*nsim_dev
;
54 struct nsim_dev_port
*nsim_dev_port
;
58 struct u64_stats_sync syncp
;
60 struct nsim_bus_dev
*nsim_bus_dev
;
62 struct bpf_prog
*bpf_offloaded
;
65 struct xdp_attachment_info xdp
;
66 struct xdp_attachment_info xdp_hw
;
69 bool bpf_tc_non_bound_accept
;
70 bool bpf_xdpdrv_accept
;
71 bool bpf_xdpoffload_accept
;
74 struct nsim_ipsec ipsec
;
78 nsim_create(struct nsim_dev
*nsim_dev
, struct nsim_dev_port
*nsim_dev_port
);
79 void nsim_destroy(struct netdevsim
*ns
);
81 #ifdef CONFIG_BPF_SYSCALL
82 int nsim_bpf_dev_init(struct nsim_dev
*nsim_dev
);
83 void nsim_bpf_dev_exit(struct nsim_dev
*nsim_dev
);
84 int nsim_bpf_init(struct netdevsim
*ns
);
85 void nsim_bpf_uninit(struct netdevsim
*ns
);
86 int nsim_bpf(struct net_device
*dev
, struct netdev_bpf
*bpf
);
87 int nsim_bpf_disable_tc(struct netdevsim
*ns
);
88 int nsim_bpf_setup_tc_block_cb(enum tc_setup_type type
,
89 void *type_data
, void *cb_priv
);
92 static inline int nsim_bpf_dev_init(struct nsim_dev
*nsim_dev
)
97 static inline void nsim_bpf_dev_exit(struct nsim_dev
*nsim_dev
)
100 static inline int nsim_bpf_init(struct netdevsim
*ns
)
105 static inline void nsim_bpf_uninit(struct netdevsim
*ns
)
109 static inline int nsim_bpf(struct net_device
*dev
, struct netdev_bpf
*bpf
)
111 return bpf
->command
== XDP_QUERY_PROG
? 0 : -EOPNOTSUPP
;
114 static inline int nsim_bpf_disable_tc(struct netdevsim
*ns
)
120 nsim_bpf_setup_tc_block_cb(enum tc_setup_type type
, void *type_data
,
127 enum nsim_resource_id
{
128 NSIM_RESOURCE_NONE
, /* DEVLINK_RESOURCE_ID_PARENT_TOP */
130 NSIM_RESOURCE_IPV4_FIB
,
131 NSIM_RESOURCE_IPV4_FIB_RULES
,
133 NSIM_RESOURCE_IPV6_FIB
,
134 NSIM_RESOURCE_IPV6_FIB_RULES
,
137 struct nsim_dev_health
{
138 struct devlink_health_reporter
*empty_reporter
;
139 struct devlink_health_reporter
*dummy_reporter
;
141 char *recovered_break_msg
;
146 int nsim_dev_health_init(struct nsim_dev
*nsim_dev
, struct devlink
*devlink
);
147 void nsim_dev_health_exit(struct nsim_dev
*nsim_dev
);
149 struct nsim_dev_port
{
150 struct list_head list
;
151 struct devlink_port devlink_port
;
152 unsigned int port_index
;
154 struct netdevsim
*ns
;
158 struct nsim_bus_dev
*nsim_bus_dev
;
159 struct nsim_fib_data
*fib_data
;
160 struct nsim_trap_data
*trap_data
;
162 struct dentry
*ports_ddir
;
163 struct bpf_offload_dev
*bpf_dev
;
164 bool bpf_bind_accept
;
165 u32 bpf_bind_verifier_delay
;
166 struct dentry
*ddir_bpf_bound_progs
;
168 struct list_head bpf_bound_progs
;
169 struct list_head bpf_bound_maps
;
170 struct netdev_phys_item_id switch_id
;
171 struct list_head port_list
;
172 struct mutex port_list_lock
; /* protects port list */
173 bool fw_update_status
;
176 bool dont_allow_reload
;
178 struct devlink_region
*dummy_region
;
179 struct nsim_dev_health health
;
182 static inline struct net
*nsim_dev_net(struct nsim_dev
*nsim_dev
)
184 return devlink_net(priv_to_devlink(nsim_dev
));
187 int nsim_dev_init(void);
188 void nsim_dev_exit(void);
189 int nsim_dev_probe(struct nsim_bus_dev
*nsim_bus_dev
);
190 void nsim_dev_remove(struct nsim_bus_dev
*nsim_bus_dev
);
191 int nsim_dev_port_add(struct nsim_bus_dev
*nsim_bus_dev
,
192 unsigned int port_index
);
193 int nsim_dev_port_del(struct nsim_bus_dev
*nsim_bus_dev
,
194 unsigned int port_index
);
196 struct nsim_fib_data
*nsim_fib_create(struct devlink
*devlink
,
197 struct netlink_ext_ack
*extack
);
198 void nsim_fib_destroy(struct devlink
*devlink
, struct nsim_fib_data
*fib_data
);
199 u64
nsim_fib_get_val(struct nsim_fib_data
*fib_data
,
200 enum nsim_resource_id res_id
, bool max
);
202 #if IS_ENABLED(CONFIG_XFRM_OFFLOAD)
203 void nsim_ipsec_init(struct netdevsim
*ns
);
204 void nsim_ipsec_teardown(struct netdevsim
*ns
);
205 bool nsim_ipsec_tx(struct netdevsim
*ns
, struct sk_buff
*skb
);
207 static inline void nsim_ipsec_init(struct netdevsim
*ns
)
211 static inline void nsim_ipsec_teardown(struct netdevsim
*ns
)
215 static inline bool nsim_ipsec_tx(struct netdevsim
*ns
, struct sk_buff
*skb
)
221 struct nsim_vf_config
{
229 bool spoofchk_enabled
;
231 bool rss_query_enabled
;
234 struct nsim_bus_dev
{
236 struct list_head list
;
237 unsigned int port_count
;
238 struct net
*initial_net
; /* Purpose of this is to carry net pointer
239 * during the probe time only.
241 unsigned int num_vfs
;
242 struct nsim_vf_config
*vfconfigs
;
245 int nsim_bus_init(void);
246 void nsim_bus_exit(void);