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