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