blob: 793c86dc5a9c1735b80bc3efb5f079a7ecf21c69 [file] [log] [blame]
Jakub Kicinski83c9e132017-12-01 15:08:58 -08001/*
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
Jakub Kicinski424be632020-07-09 17:42:48 -070016#include <linux/debugfs.h>
Jakub Kicinski79579222017-12-01 15:09:01 -080017#include <linux/device.h>
Antonio Cardacea7fc6db2020-11-18 21:45:19 +010018#include <linux/ethtool.h>
Jakub Kicinski83c9e132017-12-01 15:08:58 -080019#include <linux/kernel.h>
Jakub Kicinski31d3ad82017-12-01 15:08:59 -080020#include <linux/list.h>
Jakub Kicinski83c9e132017-12-01 15:08:58 -080021#include <linux/netdevice.h>
22#include <linux/u64_stats_sync.h>
Jiri Pirko8320d142019-04-25 15:59:53 +020023#include <net/devlink.h>
Jakub Kicinskidc9c0752020-09-25 17:56:42 -070024#include <net/udp_tunnel.h>
Jakub Kicinski05296622018-07-11 20:36:40 -070025#include <net/xdp.h>
Jakub Kicinski83c9e132017-12-01 15:08:58 -080026
27#define DRV_NAME "netdevsim"
28
Jakub Kicinski31d3ad82017-12-01 15:08:59 -080029#define NSIM_XDP_MAX_MTU 4000
30
31#define NSIM_EA(extack, msg) NL_SET_ERR_MSG_MOD((extack), msg)
32
Shannon Nelson76993532018-06-26 10:07:54 -070033#define NSIM_IPSEC_MAX_SA_COUNT 33
34#define NSIM_IPSEC_VALID BIT(31)
Jakub Kicinski424be632020-07-09 17:42:48 -070035#define NSIM_UDP_TUNNEL_N_PORTS 4
Shannon Nelson76993532018-06-26 10:07:54 -070036
37struct 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
47struct 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
Antonio Cardace77f95912020-11-18 21:45:18 +010055struct nsim_ethtool_pauseparam {
Jakub Kicinskiff1f7c12020-09-14 17:11:54 -070056 bool rx;
57 bool tx;
58 bool report_stats_rx;
59 bool report_stats_tx;
60};
61
Antonio Cardace77f95912020-11-18 21:45:18 +010062struct nsim_ethtool {
Jakub Kicinski0d7f76d2021-03-29 20:59:53 -070063 u32 get_err;
64 u32 set_err;
Antonio Cardace77f95912020-11-18 21:45:18 +010065 struct nsim_ethtool_pauseparam pauseparam;
Antonio Cardacea7fc6db2020-11-18 21:45:19 +010066 struct ethtool_coalesce coalesce;
67 struct ethtool_ringparam ring;
Jakub Kicinski0d7f76d2021-03-29 20:59:53 -070068 struct ethtool_fecparam fec;
Antonio Cardace77f95912020-11-18 21:45:18 +010069};
70
Jakub Kicinski83c9e132017-12-01 15:08:58 -080071struct netdevsim {
Jakub Kicinski31d3ad82017-12-01 15:08:59 -080072 struct net_device *netdev;
Jiri Pirkoa60f9e42019-04-25 15:59:49 +020073 struct nsim_dev *nsim_dev;
Jiri Pirkoe05b2d12019-04-25 15:59:55 +020074 struct nsim_dev_port *nsim_dev_port;
Jakub Kicinski31d3ad82017-12-01 15:08:59 -080075
Jakub Kicinski83c9e132017-12-01 15:08:58 -080076 u64 tx_packets;
77 u64 tx_bytes;
78 struct u64_stats_sync syncp;
Jakub Kicinski31d3ad82017-12-01 15:08:59 -080079
Jiri Pirko40e4fe42019-04-25 15:59:45 +020080 struct nsim_bus_dev *nsim_bus_dev;
Jakub Kicinski79579222017-12-01 15:09:01 -080081
Jakub Kicinski31d3ad82017-12-01 15:08:59 -080082 struct bpf_prog *bpf_offloaded;
83 u32 bpf_offloaded_id;
84
Jakub Kicinski05296622018-07-11 20:36:40 -070085 struct xdp_attachment_info xdp;
Jakub Kicinski799e1732018-07-11 20:36:42 -070086 struct xdp_attachment_info xdp_hw;
Jakub Kicinski31d3ad82017-12-01 15:08:59 -080087
Jakub Kicinski31d3ad82017-12-01 15:08:59 -080088 bool bpf_tc_accept;
89 bool bpf_tc_non_bound_accept;
90 bool bpf_xdpdrv_accept;
91 bool bpf_xdpoffload_accept;
Jakub Kicinski395cacb2018-01-17 19:13:30 -080092
93 bool bpf_map_accept;
Shannon Nelson76993532018-06-26 10:07:54 -070094 struct nsim_ipsec ipsec;
Jakub Kicinski424be632020-07-09 17:42:48 -070095 struct {
96 u32 inject_error;
97 u32 sleep;
Jakub Kicinskidc9c0752020-09-25 17:56:42 -070098 u32 __ports[2][NSIM_UDP_TUNNEL_N_PORTS];
99 u32 (*ports)[NSIM_UDP_TUNNEL_N_PORTS];
Jakub Kicinski424be632020-07-09 17:42:48 -0700100 struct debugfs_u32_array dfs_ports[2];
101 } udp_ports;
Jakub Kicinskiff1f7c12020-09-14 17:11:54 -0700102
103 struct nsim_ethtool ethtool;
Jakub Kicinski83c9e132017-12-01 15:08:58 -0800104};
Jakub Kicinski31d3ad82017-12-01 15:08:59 -0800105
Jiri Pirkoe05b2d12019-04-25 15:59:55 +0200106struct netdevsim *
107nsim_create(struct nsim_dev *nsim_dev, struct nsim_dev_port *nsim_dev_port);
108void nsim_destroy(struct netdevsim *ns);
109
Jakub Kicinskiff1f7c12020-09-14 17:11:54 -0700110void nsim_ethtool_init(struct netdevsim *ns);
111
Jakub Kicinski424be632020-07-09 17:42:48 -0700112void nsim_udp_tunnels_debugfs_create(struct nsim_dev *nsim_dev);
113int nsim_udp_tunnels_info_create(struct nsim_dev *nsim_dev,
114 struct net_device *dev);
115void nsim_udp_tunnels_info_destroy(struct net_device *dev);
116
Jakub Kicinski7c5db7e2018-01-23 11:22:54 -0800117#ifdef CONFIG_BPF_SYSCALL
Jiri Pirkod514f412019-04-25 15:59:50 +0200118int nsim_bpf_dev_init(struct nsim_dev *nsim_dev);
119void nsim_bpf_dev_exit(struct nsim_dev *nsim_dev);
Jakub Kicinski31d3ad82017-12-01 15:08:59 -0800120int nsim_bpf_init(struct netdevsim *ns);
121void nsim_bpf_uninit(struct netdevsim *ns);
122int nsim_bpf(struct net_device *dev, struct netdev_bpf *bpf);
123int nsim_bpf_disable_tc(struct netdevsim *ns);
124int nsim_bpf_setup_tc_block_cb(enum tc_setup_type type,
125 void *type_data, void *cb_priv);
Jakub Kicinski7c5db7e2018-01-23 11:22:54 -0800126#else
Jiri Pirkod514f412019-04-25 15:59:50 +0200127
128static inline int nsim_bpf_dev_init(struct nsim_dev *nsim_dev)
129{
130 return 0;
131}
132
133static inline void nsim_bpf_dev_exit(struct nsim_dev *nsim_dev)
134{
135}
Jakub Kicinski7c5db7e2018-01-23 11:22:54 -0800136static inline int nsim_bpf_init(struct netdevsim *ns)
137{
138 return 0;
139}
140
141static inline void nsim_bpf_uninit(struct netdevsim *ns)
142{
143}
144
145static inline int nsim_bpf(struct net_device *dev, struct netdev_bpf *bpf)
146{
Andrii Nakryikoe8407fd2020-07-21 23:46:02 -0700147 return -EOPNOTSUPP;
Jakub Kicinski7c5db7e2018-01-23 11:22:54 -0800148}
149
150static inline int nsim_bpf_disable_tc(struct netdevsim *ns)
151{
152 return 0;
153}
154
155static inline int
156nsim_bpf_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
157 void *cb_priv)
158{
159 return -EOPNOTSUPP;
160}
161#endif
Jakub Kicinski79579222017-12-01 15:09:01 -0800162
David Ahern37923ed2018-03-27 18:22:00 -0700163enum nsim_resource_id {
164 NSIM_RESOURCE_NONE, /* DEVLINK_RESOURCE_ID_PARENT_TOP */
165 NSIM_RESOURCE_IPV4,
166 NSIM_RESOURCE_IPV4_FIB,
167 NSIM_RESOURCE_IPV4_FIB_RULES,
168 NSIM_RESOURCE_IPV6,
169 NSIM_RESOURCE_IPV6_FIB,
170 NSIM_RESOURCE_IPV6_FIB_RULES,
Ido Schimmel35266252020-11-04 15:30:37 +0200171 NSIM_RESOURCE_NEXTHOPS,
David Ahern37923ed2018-03-27 18:22:00 -0700172};
173
Jiri Pirko82c93a82019-10-10 15:18:50 +0200174struct nsim_dev_health {
175 struct devlink_health_reporter *empty_reporter;
176 struct devlink_health_reporter *dummy_reporter;
177 struct dentry *ddir;
178 char *recovered_break_msg;
179 u32 binary_len;
180 bool fail_recover;
181};
182
183int nsim_dev_health_init(struct nsim_dev *nsim_dev, struct devlink *devlink);
184void nsim_dev_health_exit(struct nsim_dev *nsim_dev);
185
Ido Schimmela8700c32021-03-14 14:19:32 +0200186#if IS_ENABLED(CONFIG_PSAMPLE)
187int nsim_dev_psample_init(struct nsim_dev *nsim_dev);
188void nsim_dev_psample_exit(struct nsim_dev *nsim_dev);
189#else
190static inline int nsim_dev_psample_init(struct nsim_dev *nsim_dev)
191{
192 return 0;
193}
194
195static inline void nsim_dev_psample_exit(struct nsim_dev *nsim_dev)
196{
197}
198#endif
199
Dmytro Linkin814b9ce2021-06-02 15:17:16 +0300200enum nsim_dev_port_type {
201 NSIM_DEV_PORT_TYPE_PF,
202 NSIM_DEV_PORT_TYPE_VF,
203};
204
205#define NSIM_DEV_VF_PORT_INDEX_BASE 128
206#define NSIM_DEV_VF_PORT_INDEX_MAX UINT_MAX
207
Jiri Pirko8320d142019-04-25 15:59:53 +0200208struct nsim_dev_port {
209 struct list_head list;
210 struct devlink_port devlink_port;
211 unsigned int port_index;
Dmytro Linkin814b9ce2021-06-02 15:17:16 +0300212 enum nsim_dev_port_type port_type;
Jiri Pirko8320d142019-04-25 15:59:53 +0200213 struct dentry *ddir;
Dmytro Linkinf3d101b2021-06-02 15:17:29 +0300214 struct dentry *rate_parent;
215 char *parent_name;
Jiri Pirkoe05b2d12019-04-25 15:59:55 +0200216 struct netdevsim *ns;
Jiri Pirko8320d142019-04-25 15:59:53 +0200217};
218
Jiri Pirkoa60f9e42019-04-25 15:59:49 +0200219struct nsim_dev {
Jiri Pirkod514f412019-04-25 15:59:50 +0200220 struct nsim_bus_dev *nsim_bus_dev;
Jiri Pirkoa60f9e42019-04-25 15:59:49 +0200221 struct nsim_fib_data *fib_data;
Ido Schimmelda58f902019-08-17 16:28:20 +0300222 struct nsim_trap_data *trap_data;
Jiri Pirkod514f412019-04-25 15:59:50 +0200223 struct dentry *ddir;
Jiri Pirkoab1d0cc2019-04-25 15:59:52 +0200224 struct dentry *ports_ddir;
Taehee Yoo8526ad92020-02-01 16:43:13 +0000225 struct dentry *take_snapshot;
Dmytro Linkind3953812021-06-02 15:17:14 +0300226 struct dentry *max_vfs;
Dmytro Linkin885226f2021-06-02 15:17:26 +0300227 struct dentry *nodes_ddir;
Jiri Pirkod514f412019-04-25 15:59:50 +0200228 struct bpf_offload_dev *bpf_dev;
229 bool bpf_bind_accept;
Toke Høiland-Jørgensene4ff5aa2020-12-09 14:57:39 +0100230 bool bpf_bind_verifier_accept;
Jiri Pirkod514f412019-04-25 15:59:50 +0200231 u32 bpf_bind_verifier_delay;
232 struct dentry *ddir_bpf_bound_progs;
233 u32 prog_id_gen;
234 struct list_head bpf_bound_progs;
235 struct list_head bpf_bound_maps;
Jiri Pirko514cf642019-04-25 15:59:51 +0200236 struct netdev_phys_item_id switch_id;
Jiri Pirko8320d142019-04-25 15:59:53 +0200237 struct list_head port_list;
Jiri Pirko794b2c02019-04-25 15:59:54 +0200238 struct mutex port_list_lock; /* protects port list */
Jiri Pirkofa4dfc42019-06-04 15:40:43 +0200239 bool fw_update_status;
Jacob Kellercbb58362020-09-25 13:46:08 -0700240 u32 fw_update_overwrite_mask;
Jiri Pirko150e8f82019-08-09 13:05:12 +0200241 u32 max_macs;
242 bool test1;
Jiri Pirko155ddfc2019-10-06 08:30:01 +0200243 bool dont_allow_reload;
244 bool fail_reload;
Jiri Pirko4418f862019-08-15 15:46:33 +0200245 struct devlink_region *dummy_region;
Jiri Pirko82c93a82019-10-10 15:18:50 +0200246 struct nsim_dev_health health;
Jiri Pirkod3cbb902020-02-25 11:45:26 +0100247 struct flow_action_cookie *fa_cookie;
248 spinlock_t fa_cookie_lock; /* protects fa_cookie */
Ido Schimmel0dc82492020-03-30 22:38:23 +0300249 bool fail_trap_group_set;
Ido Schimmelad188452020-03-30 22:38:20 +0300250 bool fail_trap_policer_set;
251 bool fail_trap_policer_counter_get;
Oleksandr Mazur275b51c2021-06-17 14:36:32 +0300252 bool fail_trap_drop_counter_get;
Jakub Kicinski424be632020-07-09 17:42:48 -0700253 struct {
Jakub Kicinskidc9c0752020-09-25 17:56:42 -0700254 struct udp_tunnel_nic_shared utn_shared;
255 u32 __ports[2][NSIM_UDP_TUNNEL_N_PORTS];
Jakub Kicinski424be632020-07-09 17:42:48 -0700256 bool sync_all;
257 bool open_only;
258 bool ipv4_only;
Jakub Kicinskidc9c0752020-09-25 17:56:42 -0700259 bool shared;
Jakub Kicinskidda75172020-09-25 17:56:47 -0700260 bool static_iana_vxlan;
Jakub Kicinski424be632020-07-09 17:42:48 -0700261 u32 sleep;
262 } udp_ports;
Ido Schimmela8700c32021-03-14 14:19:32 +0200263 struct nsim_dev_psample *psample;
Dmytro Linkin160dc372021-06-02 15:17:18 +0300264 u16 esw_mode;
Jiri Pirkoa60f9e42019-04-25 15:59:49 +0200265};
266
Dmytro Linkin160dc372021-06-02 15:17:18 +0300267int nsim_esw_legacy_enable(struct nsim_dev *nsim_dev, struct netlink_ext_ack *extack);
268int nsim_esw_switchdev_enable(struct nsim_dev *nsim_dev, struct netlink_ext_ack *extack);
269
270static inline bool nsim_esw_mode_is_legacy(struct nsim_dev *nsim_dev)
271{
272 return nsim_dev->esw_mode == DEVLINK_ESWITCH_MODE_LEGACY;
273}
274
275static inline bool nsim_esw_mode_is_switchdev(struct nsim_dev *nsim_dev)
276{
277 return nsim_dev->esw_mode == DEVLINK_ESWITCH_MODE_SWITCHDEV;
278}
279
Jiri Pirko90d29912019-10-03 11:49:37 +0200280static inline struct net *nsim_dev_net(struct nsim_dev *nsim_dev)
281{
282 return devlink_net(priv_to_devlink(nsim_dev));
283}
284
Jiri Pirkod514f412019-04-25 15:59:50 +0200285int nsim_dev_init(void);
286void nsim_dev_exit(void);
Jiri Pirko8320d142019-04-25 15:59:53 +0200287int nsim_dev_probe(struct nsim_bus_dev *nsim_bus_dev);
288void nsim_dev_remove(struct nsim_bus_dev *nsim_bus_dev);
Jiri Pirko794b2c02019-04-25 15:59:54 +0200289int nsim_dev_port_add(struct nsim_bus_dev *nsim_bus_dev,
Dmytro Linkin814b9ce2021-06-02 15:17:16 +0300290 enum nsim_dev_port_type type,
Jiri Pirko794b2c02019-04-25 15:59:54 +0200291 unsigned int port_index);
292int nsim_dev_port_del(struct nsim_bus_dev *nsim_bus_dev,
Dmytro Linkin814b9ce2021-06-02 15:17:16 +0300293 enum nsim_dev_port_type type,
Jiri Pirko794b2c02019-04-25 15:59:54 +0200294 unsigned int port_index);
David Ahern37923ed2018-03-27 18:22:00 -0700295
Jiri Pirko75ba0292019-10-03 11:49:36 +0200296struct nsim_fib_data *nsim_fib_create(struct devlink *devlink,
297 struct netlink_ext_ack *extack);
298void nsim_fib_destroy(struct devlink *devlink, struct nsim_fib_data *fib_data);
Jiri Pirkoa5facc42019-10-03 11:49:26 +0200299u64 nsim_fib_get_val(struct nsim_fib_data *fib_data,
300 enum nsim_resource_id res_id, bool max);
David Ahern37923ed2018-03-27 18:22:00 -0700301
Dmytro Linkind3953812021-06-02 15:17:14 +0300302ssize_t nsim_bus_dev_max_vfs_read(struct file *file,
303 char __user *data,
304 size_t count, loff_t *ppos);
305ssize_t nsim_bus_dev_max_vfs_write(struct file *file,
306 const char __user *data,
307 size_t count, loff_t *ppos);
Dmytro Linkin32ac15d2021-06-02 15:17:15 +0300308void nsim_bus_dev_vfs_disable(struct nsim_bus_dev *nsim_bus_dev);
Dmytro Linkind3953812021-06-02 15:17:14 +0300309
Dmytro Linkin814b9ce2021-06-02 15:17:16 +0300310static inline bool nsim_dev_port_is_pf(struct nsim_dev_port *nsim_dev_port)
311{
312 return nsim_dev_port->port_type == NSIM_DEV_PORT_TYPE_PF;
313}
314
315static inline bool nsim_dev_port_is_vf(struct nsim_dev_port *nsim_dev_port)
316{
317 return nsim_dev_port->port_type == NSIM_DEV_PORT_TYPE_VF;
318}
Shannon Nelson76993532018-06-26 10:07:54 -0700319#if IS_ENABLED(CONFIG_XFRM_OFFLOAD)
320void nsim_ipsec_init(struct netdevsim *ns);
321void nsim_ipsec_teardown(struct netdevsim *ns);
322bool nsim_ipsec_tx(struct netdevsim *ns, struct sk_buff *skb);
323#else
324static inline void nsim_ipsec_init(struct netdevsim *ns)
325{
326}
327
328static inline void nsim_ipsec_teardown(struct netdevsim *ns)
329{
330}
331
332static inline bool nsim_ipsec_tx(struct netdevsim *ns, struct sk_buff *skb)
333{
334 return true;
335}
336#endif
337
Jiri Pirko40e4fe42019-04-25 15:59:45 +0200338struct nsim_vf_config {
339 int link_state;
340 u16 min_tx_rate;
341 u16 max_tx_rate;
342 u16 vlan;
343 __be16 vlan_proto;
344 u16 qos;
345 u8 vf_mac[ETH_ALEN];
346 bool spoofchk_enabled;
347 bool trusted;
348 bool rss_query_enabled;
349};
Jiri Pirko925f5af2019-04-25 15:59:44 +0200350
Jiri Pirko40e4fe42019-04-25 15:59:45 +0200351struct nsim_bus_dev {
352 struct device dev;
Jiri Pirkof9d9db42019-04-25 15:59:48 +0200353 struct list_head list;
354 unsigned int port_count;
Peilin Yed4861fc2021-07-15 18:52:45 -0700355 unsigned int num_queues; /* Number of queues for each port on this bus */
Jiri Pirko7b600272019-10-05 08:10:32 +0200356 struct net *initial_net; /* Purpose of this is to carry net pointer
357 * during the probe time only.
358 */
Dmytro Linkind3953812021-06-02 15:17:14 +0300359 unsigned int max_vfs;
Jiri Pirko40e4fe42019-04-25 15:59:45 +0200360 unsigned int num_vfs;
Dmytro Linkind3953812021-06-02 15:17:14 +0300361 struct mutex vfs_lock; /* Protects vfconfigs */
Jiri Pirko40e4fe42019-04-25 15:59:45 +0200362 struct nsim_vf_config *vfconfigs;
Taehee Yoo6ab63362020-02-01 16:43:04 +0000363 /* Lock for devlink->reload_enabled in netdevsim module */
364 struct mutex nsim_bus_reload_lock;
Leon Romanovsky5c0418e2021-08-05 17:34:28 +0300365 bool in_reload;
Taehee Yoof5cd2162020-02-01 16:42:54 +0000366 bool init;
Jiri Pirko40e4fe42019-04-25 15:59:45 +0200367};
Jiri Pirko925f5af2019-04-25 15:59:44 +0200368
Jiri Pirko925f5af2019-04-25 15:59:44 +0200369int nsim_bus_init(void);
370void nsim_bus_exit(void);