blob: 0e6ca85e548772f529aca754f87fd4f5d050706e [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;
Jakub Kicinski31d3ad82017-12-01 15:08:59 -080054
Jakub Kicinski83c9e132017-12-01 15:08:58 -080055 u64 tx_packets;
56 u64 tx_bytes;
57 struct u64_stats_sync syncp;
Jakub Kicinski31d3ad82017-12-01 15:08:59 -080058
Jiri Pirko40e4fe42019-04-25 15:59:45 +020059 struct nsim_bus_dev *nsim_bus_dev;
Jakub Kicinski79579222017-12-01 15:09:01 -080060
Jakub Kicinski31d3ad82017-12-01 15:08:59 -080061 struct dentry *ddir;
62
63 struct bpf_prog *bpf_offloaded;
64 u32 bpf_offloaded_id;
65
Jakub Kicinski05296622018-07-11 20:36:40 -070066 struct xdp_attachment_info xdp;
Jakub Kicinski799e1732018-07-11 20:36:42 -070067 struct xdp_attachment_info xdp_hw;
Jakub Kicinski31d3ad82017-12-01 15:08:59 -080068
Jakub Kicinski31d3ad82017-12-01 15:08:59 -080069 bool bpf_tc_accept;
70 bool bpf_tc_non_bound_accept;
71 bool bpf_xdpdrv_accept;
72 bool bpf_xdpoffload_accept;
Jakub Kicinski395cacb2018-01-17 19:13:30 -080073
74 bool bpf_map_accept;
Shannon Nelson76993532018-06-26 10:07:54 -070075 struct nsim_ipsec ipsec;
Jakub Kicinski83c9e132017-12-01 15:08:58 -080076};
Jakub Kicinski31d3ad82017-12-01 15:08:59 -080077
Jakub Kicinski7c5db7e2018-01-23 11:22:54 -080078#ifdef CONFIG_BPF_SYSCALL
Jiri Pirkod514f412019-04-25 15:59:50 +020079int nsim_bpf_dev_init(struct nsim_dev *nsim_dev);
80void nsim_bpf_dev_exit(struct nsim_dev *nsim_dev);
Jakub Kicinski31d3ad82017-12-01 15:08:59 -080081int nsim_bpf_init(struct netdevsim *ns);
82void nsim_bpf_uninit(struct netdevsim *ns);
83int nsim_bpf(struct net_device *dev, struct netdev_bpf *bpf);
84int nsim_bpf_disable_tc(struct netdevsim *ns);
85int nsim_bpf_setup_tc_block_cb(enum tc_setup_type type,
86 void *type_data, void *cb_priv);
Jakub Kicinski7c5db7e2018-01-23 11:22:54 -080087#else
Jiri Pirkod514f412019-04-25 15:59:50 +020088
89static inline int nsim_bpf_dev_init(struct nsim_dev *nsim_dev)
90{
91 return 0;
92}
93
94static inline void nsim_bpf_dev_exit(struct nsim_dev *nsim_dev)
95{
96}
Jakub Kicinski7c5db7e2018-01-23 11:22:54 -080097static inline int nsim_bpf_init(struct netdevsim *ns)
98{
99 return 0;
100}
101
102static inline void nsim_bpf_uninit(struct netdevsim *ns)
103{
104}
105
106static inline int nsim_bpf(struct net_device *dev, struct netdev_bpf *bpf)
107{
108 return bpf->command == XDP_QUERY_PROG ? 0 : -EOPNOTSUPP;
109}
110
111static inline int nsim_bpf_disable_tc(struct netdevsim *ns)
112{
113 return 0;
114}
115
116static inline int
117nsim_bpf_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
118 void *cb_priv)
119{
120 return -EOPNOTSUPP;
121}
122#endif
Jakub Kicinski79579222017-12-01 15:09:01 -0800123
David Ahern37923ed2018-03-27 18:22:00 -0700124enum nsim_resource_id {
125 NSIM_RESOURCE_NONE, /* DEVLINK_RESOURCE_ID_PARENT_TOP */
126 NSIM_RESOURCE_IPV4,
127 NSIM_RESOURCE_IPV4_FIB,
128 NSIM_RESOURCE_IPV4_FIB_RULES,
129 NSIM_RESOURCE_IPV6,
130 NSIM_RESOURCE_IPV6_FIB,
131 NSIM_RESOURCE_IPV6_FIB_RULES,
132};
133
Jiri Pirko8320d142019-04-25 15:59:53 +0200134struct nsim_dev_port {
135 struct list_head list;
136 struct devlink_port devlink_port;
137 unsigned int port_index;
138 struct dentry *ddir;
139};
140
Jiri Pirkoa60f9e42019-04-25 15:59:49 +0200141struct nsim_dev {
Jiri Pirkod514f412019-04-25 15:59:50 +0200142 struct nsim_bus_dev *nsim_bus_dev;
Jiri Pirkoa60f9e42019-04-25 15:59:49 +0200143 struct nsim_fib_data *fib_data;
Jiri Pirkod514f412019-04-25 15:59:50 +0200144 struct dentry *ddir;
Jiri Pirkoab1d0cc2019-04-25 15:59:52 +0200145 struct dentry *ports_ddir;
Jiri Pirkod514f412019-04-25 15:59:50 +0200146 struct bpf_offload_dev *bpf_dev;
147 bool bpf_bind_accept;
148 u32 bpf_bind_verifier_delay;
149 struct dentry *ddir_bpf_bound_progs;
150 u32 prog_id_gen;
151 struct list_head bpf_bound_progs;
152 struct list_head bpf_bound_maps;
Jiri Pirko514cf642019-04-25 15:59:51 +0200153 struct netdev_phys_item_id switch_id;
Jiri Pirko8320d142019-04-25 15:59:53 +0200154 struct list_head port_list;
Jiri Pirkoa60f9e42019-04-25 15:59:49 +0200155};
156
Jiri Pirkod514f412019-04-25 15:59:50 +0200157int nsim_dev_init(void);
158void nsim_dev_exit(void);
Jiri Pirko8320d142019-04-25 15:59:53 +0200159int nsim_dev_probe(struct nsim_bus_dev *nsim_bus_dev);
160void nsim_dev_remove(struct nsim_bus_dev *nsim_bus_dev);
David Ahern37923ed2018-03-27 18:22:00 -0700161
Jiri Pirko5fc49422019-04-25 15:59:42 +0200162struct nsim_fib_data *nsim_fib_create(void);
163void nsim_fib_destroy(struct nsim_fib_data *fib_data);
164u64 nsim_fib_get_val(struct nsim_fib_data *fib_data,
165 enum nsim_resource_id res_id, bool max);
166int nsim_fib_set_max(struct nsim_fib_data *fib_data,
167 enum nsim_resource_id res_id, u64 val,
David Ahern7fa76d72018-06-05 08:14:10 -0700168 struct netlink_ext_ack *extack);
David Ahern37923ed2018-03-27 18:22:00 -0700169
Shannon Nelson76993532018-06-26 10:07:54 -0700170#if IS_ENABLED(CONFIG_XFRM_OFFLOAD)
171void nsim_ipsec_init(struct netdevsim *ns);
172void nsim_ipsec_teardown(struct netdevsim *ns);
173bool nsim_ipsec_tx(struct netdevsim *ns, struct sk_buff *skb);
174#else
175static inline void nsim_ipsec_init(struct netdevsim *ns)
176{
177}
178
179static inline void nsim_ipsec_teardown(struct netdevsim *ns)
180{
181}
182
183static inline bool nsim_ipsec_tx(struct netdevsim *ns, struct sk_buff *skb)
184{
185 return true;
186}
187#endif
188
Jiri Pirko40e4fe42019-04-25 15:59:45 +0200189struct nsim_vf_config {
190 int link_state;
191 u16 min_tx_rate;
192 u16 max_tx_rate;
193 u16 vlan;
194 __be16 vlan_proto;
195 u16 qos;
196 u8 vf_mac[ETH_ALEN];
197 bool spoofchk_enabled;
198 bool trusted;
199 bool rss_query_enabled;
200};
Jiri Pirko925f5af2019-04-25 15:59:44 +0200201
Jiri Pirko40e4fe42019-04-25 15:59:45 +0200202struct nsim_bus_dev {
203 struct device dev;
Jiri Pirkof9d9db42019-04-25 15:59:48 +0200204 struct list_head list;
205 unsigned int port_count;
Jiri Pirko40e4fe42019-04-25 15:59:45 +0200206 unsigned int num_vfs;
207 struct nsim_vf_config *vfconfigs;
208};
Jiri Pirko925f5af2019-04-25 15:59:44 +0200209
Jiri Pirkof9d9db42019-04-25 15:59:48 +0200210struct nsim_bus_dev *nsim_bus_dev_new(unsigned int id, unsigned int port_count);
Jiri Pirko8320d142019-04-25 15:59:53 +0200211struct nsim_bus_dev *nsim_bus_dev_new_with_ns(struct netdevsim *ns);
Jiri Pirko40e4fe42019-04-25 15:59:45 +0200212void nsim_bus_dev_del(struct nsim_bus_dev *nsim_bus_dev);
Jiri Pirko925f5af2019-04-25 15:59:44 +0200213int nsim_bus_init(void);
214void nsim_bus_exit(void);