blob: 4c758c6919f5d1cc30e9971b426c4bda2a7b8728 [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 Kicinski79579222017-12-01 15:09:01 -080016#include <linux/device.h>
Jakub Kicinski83c9e132017-12-01 15:08:58 -080017#include <linux/kernel.h>
Jakub Kicinski31d3ad82017-12-01 15:08:59 -080018#include <linux/list.h>
Jakub Kicinski83c9e132017-12-01 15:08:58 -080019#include <linux/netdevice.h>
20#include <linux/u64_stats_sync.h>
Jiri Pirko8320d142019-04-25 15:59:53 +020021#include <net/devlink.h>
Jakub Kicinski05296622018-07-11 20:36:40 -070022#include <net/xdp.h>
Jakub Kicinski83c9e132017-12-01 15:08:58 -080023
24#define DRV_NAME "netdevsim"
25
Jakub Kicinski31d3ad82017-12-01 15:08:59 -080026#define NSIM_XDP_MAX_MTU 4000
27
28#define NSIM_EA(extack, msg) NL_SET_ERR_MSG_MOD((extack), msg)
29
Shannon Nelson76993532018-06-26 10:07:54 -070030#define NSIM_IPSEC_MAX_SA_COUNT 33
31#define NSIM_IPSEC_VALID BIT(31)
32
33struct nsim_sa {
34 struct xfrm_state *xs;
35 __be32 ipaddr[4];
36 u32 key[4];
37 u32 salt;
38 bool used;
39 bool crypt;
40 bool rx;
41};
42
43struct nsim_ipsec {
44 struct nsim_sa sa[NSIM_IPSEC_MAX_SA_COUNT];
45 struct dentry *pfile;
46 u32 count;
47 u32 tx;
48 u32 ok;
49};
50
Jakub Kicinski83c9e132017-12-01 15:08:58 -080051struct netdevsim {
Jakub Kicinski31d3ad82017-12-01 15:08:59 -080052 struct net_device *netdev;
Jiri Pirkoa60f9e42019-04-25 15:59:49 +020053 struct nsim_dev *nsim_dev;
Jiri Pirkoe05b2d12019-04-25 15:59:55 +020054 struct nsim_dev_port *nsim_dev_port;
Jakub Kicinski31d3ad82017-12-01 15:08:59 -080055
Jakub Kicinski83c9e132017-12-01 15:08:58 -080056 u64 tx_packets;
57 u64 tx_bytes;
58 struct u64_stats_sync syncp;
Jakub Kicinski31d3ad82017-12-01 15:08:59 -080059
Jiri Pirko40e4fe42019-04-25 15:59:45 +020060 struct nsim_bus_dev *nsim_bus_dev;
Jakub Kicinski79579222017-12-01 15:09:01 -080061
Jakub Kicinski31d3ad82017-12-01 15:08:59 -080062 struct bpf_prog *bpf_offloaded;
63 u32 bpf_offloaded_id;
64
Jakub Kicinski05296622018-07-11 20:36:40 -070065 struct xdp_attachment_info xdp;
Jakub Kicinski799e1732018-07-11 20:36:42 -070066 struct xdp_attachment_info xdp_hw;
Jakub Kicinski31d3ad82017-12-01 15:08:59 -080067
Jakub Kicinski31d3ad82017-12-01 15:08:59 -080068 bool bpf_tc_accept;
69 bool bpf_tc_non_bound_accept;
70 bool bpf_xdpdrv_accept;
71 bool bpf_xdpoffload_accept;
Jakub Kicinski395cacb2018-01-17 19:13:30 -080072
73 bool bpf_map_accept;
Shannon Nelson76993532018-06-26 10:07:54 -070074 struct nsim_ipsec ipsec;
Jakub Kicinski83c9e132017-12-01 15:08:58 -080075};
Jakub Kicinski31d3ad82017-12-01 15:08:59 -080076
Jiri Pirkoe05b2d12019-04-25 15:59:55 +020077struct netdevsim *
78nsim_create(struct nsim_dev *nsim_dev, struct nsim_dev_port *nsim_dev_port);
79void nsim_destroy(struct netdevsim *ns);
80
Jakub Kicinski7c5db7e2018-01-23 11:22:54 -080081#ifdef CONFIG_BPF_SYSCALL
Jiri Pirkod514f412019-04-25 15:59:50 +020082int nsim_bpf_dev_init(struct nsim_dev *nsim_dev);
83void nsim_bpf_dev_exit(struct nsim_dev *nsim_dev);
Jakub Kicinski31d3ad82017-12-01 15:08:59 -080084int nsim_bpf_init(struct netdevsim *ns);
85void nsim_bpf_uninit(struct netdevsim *ns);
86int nsim_bpf(struct net_device *dev, struct netdev_bpf *bpf);
87int nsim_bpf_disable_tc(struct netdevsim *ns);
88int nsim_bpf_setup_tc_block_cb(enum tc_setup_type type,
89 void *type_data, void *cb_priv);
Jakub Kicinski7c5db7e2018-01-23 11:22:54 -080090#else
Jiri Pirkod514f412019-04-25 15:59:50 +020091
92static inline int nsim_bpf_dev_init(struct nsim_dev *nsim_dev)
93{
94 return 0;
95}
96
97static inline void nsim_bpf_dev_exit(struct nsim_dev *nsim_dev)
98{
99}
Jakub Kicinski7c5db7e2018-01-23 11:22:54 -0800100static inline int nsim_bpf_init(struct netdevsim *ns)
101{
102 return 0;
103}
104
105static inline void nsim_bpf_uninit(struct netdevsim *ns)
106{
107}
108
109static inline int nsim_bpf(struct net_device *dev, struct netdev_bpf *bpf)
110{
111 return bpf->command == XDP_QUERY_PROG ? 0 : -EOPNOTSUPP;
112}
113
114static inline int nsim_bpf_disable_tc(struct netdevsim *ns)
115{
116 return 0;
117}
118
119static inline int
120nsim_bpf_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
121 void *cb_priv)
122{
123 return -EOPNOTSUPP;
124}
125#endif
Jakub Kicinski79579222017-12-01 15:09:01 -0800126
David Ahern37923ed2018-03-27 18:22:00 -0700127enum nsim_resource_id {
128 NSIM_RESOURCE_NONE, /* DEVLINK_RESOURCE_ID_PARENT_TOP */
129 NSIM_RESOURCE_IPV4,
130 NSIM_RESOURCE_IPV4_FIB,
131 NSIM_RESOURCE_IPV4_FIB_RULES,
132 NSIM_RESOURCE_IPV6,
133 NSIM_RESOURCE_IPV6_FIB,
134 NSIM_RESOURCE_IPV6_FIB_RULES,
135};
136
Jiri Pirko8320d142019-04-25 15:59:53 +0200137struct nsim_dev_port {
138 struct list_head list;
139 struct devlink_port devlink_port;
140 unsigned int port_index;
141 struct dentry *ddir;
Jiri Pirkoe05b2d12019-04-25 15:59:55 +0200142 struct netdevsim *ns;
Jiri Pirko8320d142019-04-25 15:59:53 +0200143};
144
Jiri Pirkoa60f9e42019-04-25 15:59:49 +0200145struct nsim_dev {
Jiri Pirkod514f412019-04-25 15:59:50 +0200146 struct nsim_bus_dev *nsim_bus_dev;
Jiri Pirkoa60f9e42019-04-25 15:59:49 +0200147 struct nsim_fib_data *fib_data;
Jiri Pirkod514f412019-04-25 15:59:50 +0200148 struct dentry *ddir;
Jiri Pirkoab1d0cc2019-04-25 15:59:52 +0200149 struct dentry *ports_ddir;
Jiri Pirkod514f412019-04-25 15:59:50 +0200150 struct bpf_offload_dev *bpf_dev;
151 bool bpf_bind_accept;
152 u32 bpf_bind_verifier_delay;
153 struct dentry *ddir_bpf_bound_progs;
154 u32 prog_id_gen;
155 struct list_head bpf_bound_progs;
156 struct list_head bpf_bound_maps;
Jiri Pirko514cf642019-04-25 15:59:51 +0200157 struct netdev_phys_item_id switch_id;
Jiri Pirko8320d142019-04-25 15:59:53 +0200158 struct list_head port_list;
Jiri Pirko794b2c02019-04-25 15:59:54 +0200159 struct mutex port_list_lock; /* protects port list */
Jiri Pirkofa4dfc42019-06-04 15:40:43 +0200160 bool fw_update_status;
Jiri Pirko150e8f82019-08-09 13:05:12 +0200161 u32 max_macs;
162 bool test1;
Jiri Pirko4418f862019-08-15 15:46:33 +0200163 struct devlink_region *dummy_region;
Jiri Pirkoa60f9e42019-04-25 15:59:49 +0200164};
165
Jiri Pirkod514f412019-04-25 15:59:50 +0200166int nsim_dev_init(void);
167void nsim_dev_exit(void);
Jiri Pirko8320d142019-04-25 15:59:53 +0200168int nsim_dev_probe(struct nsim_bus_dev *nsim_bus_dev);
169void nsim_dev_remove(struct nsim_bus_dev *nsim_bus_dev);
Jiri Pirko794b2c02019-04-25 15:59:54 +0200170int nsim_dev_port_add(struct nsim_bus_dev *nsim_bus_dev,
171 unsigned int port_index);
172int nsim_dev_port_del(struct nsim_bus_dev *nsim_bus_dev,
173 unsigned int port_index);
David Ahern37923ed2018-03-27 18:22:00 -0700174
Jiri Pirko5fc49422019-04-25 15:59:42 +0200175struct nsim_fib_data *nsim_fib_create(void);
176void nsim_fib_destroy(struct nsim_fib_data *fib_data);
177u64 nsim_fib_get_val(struct nsim_fib_data *fib_data,
178 enum nsim_resource_id res_id, bool max);
179int nsim_fib_set_max(struct nsim_fib_data *fib_data,
180 enum nsim_resource_id res_id, u64 val,
David Ahern7fa76d72018-06-05 08:14:10 -0700181 struct netlink_ext_ack *extack);
David Ahern37923ed2018-03-27 18:22:00 -0700182
Shannon Nelson76993532018-06-26 10:07:54 -0700183#if IS_ENABLED(CONFIG_XFRM_OFFLOAD)
184void nsim_ipsec_init(struct netdevsim *ns);
185void nsim_ipsec_teardown(struct netdevsim *ns);
186bool nsim_ipsec_tx(struct netdevsim *ns, struct sk_buff *skb);
187#else
188static inline void nsim_ipsec_init(struct netdevsim *ns)
189{
190}
191
192static inline void nsim_ipsec_teardown(struct netdevsim *ns)
193{
194}
195
196static inline bool nsim_ipsec_tx(struct netdevsim *ns, struct sk_buff *skb)
197{
198 return true;
199}
200#endif
201
Jiri Pirko40e4fe42019-04-25 15:59:45 +0200202struct nsim_vf_config {
203 int link_state;
204 u16 min_tx_rate;
205 u16 max_tx_rate;
206 u16 vlan;
207 __be16 vlan_proto;
208 u16 qos;
209 u8 vf_mac[ETH_ALEN];
210 bool spoofchk_enabled;
211 bool trusted;
212 bool rss_query_enabled;
213};
Jiri Pirko925f5af2019-04-25 15:59:44 +0200214
Jiri Pirko40e4fe42019-04-25 15:59:45 +0200215struct nsim_bus_dev {
216 struct device dev;
Jiri Pirkof9d9db42019-04-25 15:59:48 +0200217 struct list_head list;
218 unsigned int port_count;
Jiri Pirko40e4fe42019-04-25 15:59:45 +0200219 unsigned int num_vfs;
220 struct nsim_vf_config *vfconfigs;
221};
Jiri Pirko925f5af2019-04-25 15:59:44 +0200222
Jiri Pirko925f5af2019-04-25 15:59:44 +0200223int nsim_bus_init(void);
224void nsim_bus_exit(void);