xref: /openbmc/linux/drivers/net/netdevsim/netdevsim.h (revision 0deca83ff1118f305fc7fb11e5a4b13e876201e8)
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/kernel.h>
19 #include <linux/list.h>
20 #include <linux/netdevice.h>
21 #include <linux/u64_stats_sync.h>
22 #include <net/devlink.h>
23 #include <net/xdp.h>
24 
25 #define DRV_NAME	"netdevsim"
26 
27 #define NSIM_XDP_MAX_MTU	4000
28 
29 #define NSIM_EA(extack, msg)	NL_SET_ERR_MSG_MOD((extack), msg)
30 
31 #define NSIM_IPSEC_MAX_SA_COUNT		33
32 #define NSIM_IPSEC_VALID		BIT(31)
33 #define NSIM_UDP_TUNNEL_N_PORTS		4
34 
35 struct nsim_sa {
36 	struct xfrm_state *xs;
37 	__be32 ipaddr[4];
38 	u32 key[4];
39 	u32 salt;
40 	bool used;
41 	bool crypt;
42 	bool rx;
43 };
44 
45 struct nsim_ipsec {
46 	struct nsim_sa sa[NSIM_IPSEC_MAX_SA_COUNT];
47 	struct dentry *pfile;
48 	u32 count;
49 	u32 tx;
50 	u32 ok;
51 };
52 
53 struct nsim_ethtool {
54 	bool rx;
55 	bool tx;
56 	bool report_stats_rx;
57 	bool report_stats_tx;
58 };
59 
60 struct netdevsim {
61 	struct net_device *netdev;
62 	struct nsim_dev *nsim_dev;
63 	struct nsim_dev_port *nsim_dev_port;
64 
65 	u64 tx_packets;
66 	u64 tx_bytes;
67 	struct u64_stats_sync syncp;
68 
69 	struct nsim_bus_dev *nsim_bus_dev;
70 
71 	struct bpf_prog	*bpf_offloaded;
72 	u32 bpf_offloaded_id;
73 
74 	struct xdp_attachment_info xdp;
75 	struct xdp_attachment_info xdp_hw;
76 
77 	bool bpf_tc_accept;
78 	bool bpf_tc_non_bound_accept;
79 	bool bpf_xdpdrv_accept;
80 	bool bpf_xdpoffload_accept;
81 
82 	bool bpf_map_accept;
83 	struct nsim_ipsec ipsec;
84 	struct {
85 		u32 inject_error;
86 		u32 sleep;
87 		u32 ports[2][NSIM_UDP_TUNNEL_N_PORTS];
88 		struct debugfs_u32_array dfs_ports[2];
89 	} udp_ports;
90 
91 	struct nsim_ethtool ethtool;
92 };
93 
94 struct netdevsim *
95 nsim_create(struct nsim_dev *nsim_dev, struct nsim_dev_port *nsim_dev_port);
96 void nsim_destroy(struct netdevsim *ns);
97 
98 void nsim_ethtool_init(struct netdevsim *ns);
99 
100 void nsim_udp_tunnels_debugfs_create(struct nsim_dev *nsim_dev);
101 int nsim_udp_tunnels_info_create(struct nsim_dev *nsim_dev,
102 				 struct net_device *dev);
103 void nsim_udp_tunnels_info_destroy(struct net_device *dev);
104 
105 #ifdef CONFIG_BPF_SYSCALL
106 int nsim_bpf_dev_init(struct nsim_dev *nsim_dev);
107 void nsim_bpf_dev_exit(struct nsim_dev *nsim_dev);
108 int nsim_bpf_init(struct netdevsim *ns);
109 void nsim_bpf_uninit(struct netdevsim *ns);
110 int nsim_bpf(struct net_device *dev, struct netdev_bpf *bpf);
111 int nsim_bpf_disable_tc(struct netdevsim *ns);
112 int nsim_bpf_setup_tc_block_cb(enum tc_setup_type type,
113 			       void *type_data, void *cb_priv);
114 #else
115 
116 static inline int nsim_bpf_dev_init(struct nsim_dev *nsim_dev)
117 {
118 	return 0;
119 }
120 
121 static inline void nsim_bpf_dev_exit(struct nsim_dev *nsim_dev)
122 {
123 }
124 static inline int nsim_bpf_init(struct netdevsim *ns)
125 {
126 	return 0;
127 }
128 
129 static inline void nsim_bpf_uninit(struct netdevsim *ns)
130 {
131 }
132 
133 static inline int nsim_bpf(struct net_device *dev, struct netdev_bpf *bpf)
134 {
135 	return -EOPNOTSUPP;
136 }
137 
138 static inline int nsim_bpf_disable_tc(struct netdevsim *ns)
139 {
140 	return 0;
141 }
142 
143 static inline int
144 nsim_bpf_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
145 			   void *cb_priv)
146 {
147 	return -EOPNOTSUPP;
148 }
149 #endif
150 
151 enum nsim_resource_id {
152 	NSIM_RESOURCE_NONE,   /* DEVLINK_RESOURCE_ID_PARENT_TOP */
153 	NSIM_RESOURCE_IPV4,
154 	NSIM_RESOURCE_IPV4_FIB,
155 	NSIM_RESOURCE_IPV4_FIB_RULES,
156 	NSIM_RESOURCE_IPV6,
157 	NSIM_RESOURCE_IPV6_FIB,
158 	NSIM_RESOURCE_IPV6_FIB_RULES,
159 };
160 
161 struct nsim_dev_health {
162 	struct devlink_health_reporter *empty_reporter;
163 	struct devlink_health_reporter *dummy_reporter;
164 	struct dentry *ddir;
165 	char *recovered_break_msg;
166 	u32 binary_len;
167 	bool fail_recover;
168 };
169 
170 int nsim_dev_health_init(struct nsim_dev *nsim_dev, struct devlink *devlink);
171 void nsim_dev_health_exit(struct nsim_dev *nsim_dev);
172 
173 struct nsim_dev_port {
174 	struct list_head list;
175 	struct devlink_port devlink_port;
176 	unsigned int port_index;
177 	struct dentry *ddir;
178 	struct netdevsim *ns;
179 };
180 
181 struct nsim_dev {
182 	struct nsim_bus_dev *nsim_bus_dev;
183 	struct nsim_fib_data *fib_data;
184 	struct nsim_trap_data *trap_data;
185 	struct dentry *ddir;
186 	struct dentry *ports_ddir;
187 	struct dentry *take_snapshot;
188 	struct bpf_offload_dev *bpf_dev;
189 	bool bpf_bind_accept;
190 	u32 bpf_bind_verifier_delay;
191 	struct dentry *ddir_bpf_bound_progs;
192 	u32 prog_id_gen;
193 	struct list_head bpf_bound_progs;
194 	struct list_head bpf_bound_maps;
195 	struct netdev_phys_item_id switch_id;
196 	struct list_head port_list;
197 	struct mutex port_list_lock; /* protects port list */
198 	bool fw_update_status;
199 	u32 max_macs;
200 	bool test1;
201 	bool dont_allow_reload;
202 	bool fail_reload;
203 	struct devlink_region *dummy_region;
204 	struct nsim_dev_health health;
205 	struct flow_action_cookie *fa_cookie;
206 	spinlock_t fa_cookie_lock; /* protects fa_cookie */
207 	bool fail_trap_group_set;
208 	bool fail_trap_policer_set;
209 	bool fail_trap_policer_counter_get;
210 	struct {
211 		bool sync_all;
212 		bool open_only;
213 		bool ipv4_only;
214 		u32 sleep;
215 	} udp_ports;
216 };
217 
218 static inline struct net *nsim_dev_net(struct nsim_dev *nsim_dev)
219 {
220 	return devlink_net(priv_to_devlink(nsim_dev));
221 }
222 
223 int nsim_dev_init(void);
224 void nsim_dev_exit(void);
225 int nsim_dev_probe(struct nsim_bus_dev *nsim_bus_dev);
226 void nsim_dev_remove(struct nsim_bus_dev *nsim_bus_dev);
227 int nsim_dev_port_add(struct nsim_bus_dev *nsim_bus_dev,
228 		      unsigned int port_index);
229 int nsim_dev_port_del(struct nsim_bus_dev *nsim_bus_dev,
230 		      unsigned int port_index);
231 
232 struct nsim_fib_data *nsim_fib_create(struct devlink *devlink,
233 				      struct netlink_ext_ack *extack);
234 void nsim_fib_destroy(struct devlink *devlink, struct nsim_fib_data *fib_data);
235 u64 nsim_fib_get_val(struct nsim_fib_data *fib_data,
236 		     enum nsim_resource_id res_id, bool max);
237 
238 #if IS_ENABLED(CONFIG_XFRM_OFFLOAD)
239 void nsim_ipsec_init(struct netdevsim *ns);
240 void nsim_ipsec_teardown(struct netdevsim *ns);
241 bool nsim_ipsec_tx(struct netdevsim *ns, struct sk_buff *skb);
242 #else
243 static inline void nsim_ipsec_init(struct netdevsim *ns)
244 {
245 }
246 
247 static inline void nsim_ipsec_teardown(struct netdevsim *ns)
248 {
249 }
250 
251 static inline bool nsim_ipsec_tx(struct netdevsim *ns, struct sk_buff *skb)
252 {
253 	return true;
254 }
255 #endif
256 
257 struct nsim_vf_config {
258 	int link_state;
259 	u16 min_tx_rate;
260 	u16 max_tx_rate;
261 	u16 vlan;
262 	__be16 vlan_proto;
263 	u16 qos;
264 	u8 vf_mac[ETH_ALEN];
265 	bool spoofchk_enabled;
266 	bool trusted;
267 	bool rss_query_enabled;
268 };
269 
270 struct nsim_bus_dev {
271 	struct device dev;
272 	struct list_head list;
273 	unsigned int port_count;
274 	struct net *initial_net; /* Purpose of this is to carry net pointer
275 				  * during the probe time only.
276 				  */
277 	unsigned int num_vfs;
278 	struct nsim_vf_config *vfconfigs;
279 	/* Lock for devlink->reload_enabled in netdevsim module */
280 	struct mutex nsim_bus_reload_lock;
281 	bool init;
282 };
283 
284 int nsim_bus_init(void);
285 void nsim_bus_exit(void);
286