blob: 0aeabbe81cc6fbe616188238a2ec3244766c4ccd [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>
Jakub Kicinski05296622018-07-11 20:36:40 -070021#include <net/xdp.h>
Jakub Kicinski83c9e132017-12-01 15:08:58 -080022
23#define DRV_NAME "netdevsim"
24
Jakub Kicinski31d3ad82017-12-01 15:08:59 -080025#define NSIM_XDP_MAX_MTU 4000
26
27#define NSIM_EA(extack, msg) NL_SET_ERR_MSG_MOD((extack), msg)
28
29struct bpf_prog;
30struct dentry;
Jakub Kicinski79579222017-12-01 15:09:01 -080031struct nsim_vf_config;
Jakub Kicinski31d3ad82017-12-01 15:08:59 -080032
Shannon Nelson76993532018-06-26 10:07:54 -070033#define NSIM_IPSEC_MAX_SA_COUNT 33
34#define NSIM_IPSEC_VALID BIT(31)
35
36struct nsim_sa {
37 struct xfrm_state *xs;
38 __be32 ipaddr[4];
39 u32 key[4];
40 u32 salt;
41 bool used;
42 bool crypt;
43 bool rx;
44};
45
46struct nsim_ipsec {
47 struct nsim_sa sa[NSIM_IPSEC_MAX_SA_COUNT];
48 struct dentry *pfile;
49 u32 count;
50 u32 tx;
51 u32 ok;
52};
53
Jakub Kicinski83c9e132017-12-01 15:08:58 -080054struct netdevsim {
Jakub Kicinski31d3ad82017-12-01 15:08:59 -080055 struct net_device *netdev;
56
Jakub Kicinski83c9e132017-12-01 15:08:58 -080057 u64 tx_packets;
58 u64 tx_bytes;
59 struct u64_stats_sync syncp;
Jakub Kicinski31d3ad82017-12-01 15:08:59 -080060
Jakub Kicinski79579222017-12-01 15:09:01 -080061 struct device dev;
62
Jakub Kicinski31d3ad82017-12-01 15:08:59 -080063 struct dentry *ddir;
64
Jakub Kicinski79579222017-12-01 15:09:01 -080065 unsigned int num_vfs;
66 struct nsim_vf_config *vfconfigs;
67
Jakub Kicinski31d3ad82017-12-01 15:08:59 -080068 struct bpf_prog *bpf_offloaded;
69 u32 bpf_offloaded_id;
70
Jakub Kicinski05296622018-07-11 20:36:40 -070071 struct xdp_attachment_info xdp;
Jakub Kicinski799e1732018-07-11 20:36:42 -070072 struct xdp_attachment_info xdp_hw;
Jakub Kicinski31d3ad82017-12-01 15:08:59 -080073
74 u32 prog_id_gen;
75
76 bool bpf_bind_accept;
77 u32 bpf_bind_verifier_delay;
78 struct dentry *ddir_bpf_bound_progs;
79 struct list_head bpf_bound_progs;
80
81 bool bpf_tc_accept;
82 bool bpf_tc_non_bound_accept;
83 bool bpf_xdpdrv_accept;
84 bool bpf_xdpoffload_accept;
Jakub Kicinski395cacb2018-01-17 19:13:30 -080085
86 bool bpf_map_accept;
87 struct list_head bpf_bound_maps;
David Ahern37923ed2018-03-27 18:22:00 -070088#if IS_ENABLED(CONFIG_NET_DEVLINK)
89 struct devlink *devlink;
90#endif
Shannon Nelson76993532018-06-26 10:07:54 -070091 struct nsim_ipsec ipsec;
Jakub Kicinski83c9e132017-12-01 15:08:58 -080092};
Jakub Kicinski31d3ad82017-12-01 15:08:59 -080093
94extern struct dentry *nsim_ddir;
95
Jakub Kicinski7c5db7e2018-01-23 11:22:54 -080096#ifdef CONFIG_BPF_SYSCALL
Jakub Kicinski31d3ad82017-12-01 15:08:59 -080097int nsim_bpf_init(struct netdevsim *ns);
98void nsim_bpf_uninit(struct netdevsim *ns);
99int nsim_bpf(struct net_device *dev, struct netdev_bpf *bpf);
100int nsim_bpf_disable_tc(struct netdevsim *ns);
101int nsim_bpf_setup_tc_block_cb(enum tc_setup_type type,
102 void *type_data, void *cb_priv);
Jakub Kicinski7c5db7e2018-01-23 11:22:54 -0800103#else
104static inline int nsim_bpf_init(struct netdevsim *ns)
105{
106 return 0;
107}
108
109static inline void nsim_bpf_uninit(struct netdevsim *ns)
110{
111}
112
113static inline int nsim_bpf(struct net_device *dev, struct netdev_bpf *bpf)
114{
115 return bpf->command == XDP_QUERY_PROG ? 0 : -EOPNOTSUPP;
116}
117
118static inline int nsim_bpf_disable_tc(struct netdevsim *ns)
119{
120 return 0;
121}
122
123static inline int
124nsim_bpf_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
125 void *cb_priv)
126{
127 return -EOPNOTSUPP;
128}
129#endif
Jakub Kicinski79579222017-12-01 15:09:01 -0800130
David Ahern37923ed2018-03-27 18:22:00 -0700131#if IS_ENABLED(CONFIG_NET_DEVLINK)
132enum nsim_resource_id {
133 NSIM_RESOURCE_NONE, /* DEVLINK_RESOURCE_ID_PARENT_TOP */
134 NSIM_RESOURCE_IPV4,
135 NSIM_RESOURCE_IPV4_FIB,
136 NSIM_RESOURCE_IPV4_FIB_RULES,
137 NSIM_RESOURCE_IPV6,
138 NSIM_RESOURCE_IPV6_FIB,
139 NSIM_RESOURCE_IPV6_FIB_RULES,
140};
141
David Ahernef817102018-03-30 09:28:51 -0700142int nsim_devlink_setup(struct netdevsim *ns);
David Ahern37923ed2018-03-27 18:22:00 -0700143void nsim_devlink_teardown(struct netdevsim *ns);
144
145int nsim_devlink_init(void);
146void nsim_devlink_exit(void);
147
148int nsim_fib_init(void);
149void nsim_fib_exit(void);
150u64 nsim_fib_get_val(struct net *net, enum nsim_resource_id res_id, bool max);
David Ahern7fa76d72018-06-05 08:14:10 -0700151int nsim_fib_set_max(struct net *net, enum nsim_resource_id res_id, u64 val,
152 struct netlink_ext_ack *extack);
David Ahern37923ed2018-03-27 18:22:00 -0700153#else
David Ahernef817102018-03-30 09:28:51 -0700154static inline int nsim_devlink_setup(struct netdevsim *ns)
David Ahern37923ed2018-03-27 18:22:00 -0700155{
David Ahernef817102018-03-30 09:28:51 -0700156 return 0;
David Ahern37923ed2018-03-27 18:22:00 -0700157}
158
159static inline void nsim_devlink_teardown(struct netdevsim *ns)
160{
161}
162
163static inline int nsim_devlink_init(void)
164{
165 return 0;
166}
167
168static inline void nsim_devlink_exit(void)
169{
170}
171#endif
172
Shannon Nelson76993532018-06-26 10:07:54 -0700173#if IS_ENABLED(CONFIG_XFRM_OFFLOAD)
174void nsim_ipsec_init(struct netdevsim *ns);
175void nsim_ipsec_teardown(struct netdevsim *ns);
176bool nsim_ipsec_tx(struct netdevsim *ns, struct sk_buff *skb);
177#else
178static inline void nsim_ipsec_init(struct netdevsim *ns)
179{
180}
181
182static inline void nsim_ipsec_teardown(struct netdevsim *ns)
183{
184}
185
186static inline bool nsim_ipsec_tx(struct netdevsim *ns, struct sk_buff *skb)
187{
188 return true;
189}
190#endif
191
Jakub Kicinski79579222017-12-01 15:09:01 -0800192static inline struct netdevsim *to_nsim(struct device *ptr)
193{
194 return container_of(ptr, struct netdevsim, dev);
195}