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/device.h>
17 #include <linux/kernel.h>
18 #include <linux/list.h>
19 #include <linux/netdevice.h>
20 #include <linux/u64_stats_sync.h>
21 #include <net/xdp.h>
22 
23 #define DRV_NAME	"netdevsim"
24 
25 #define NSIM_XDP_MAX_MTU	4000
26 
27 #define NSIM_EA(extack, msg)	NL_SET_ERR_MSG_MOD((extack), msg)
28 
29 struct bpf_prog;
30 struct bpf_offload_dev;
31 struct dentry;
32 struct nsim_vf_config;
33 
34 struct netdevsim_shared_dev {
35 	unsigned int refcnt;
36 	u32 switch_id;
37 
38 	struct dentry *ddir;
39 
40 	struct bpf_offload_dev *bpf_dev;
41 
42 	bool bpf_bind_accept;
43 	u32 bpf_bind_verifier_delay;
44 
45 	struct dentry *ddir_bpf_bound_progs;
46 	u32 prog_id_gen;
47 
48 	struct list_head bpf_bound_progs;
49 	struct list_head bpf_bound_maps;
50 };
51 
52 struct netdevsim;
53 
54 struct netdevsim_shared_dev *nsim_sdev_get(struct netdevsim *joinns);
55 void nsim_sdev_put(struct netdevsim_shared_dev *sdev);
56 int nsim_sdev_init(void);
57 void nsim_sdev_exit(void);
58 
59 #define NSIM_IPSEC_MAX_SA_COUNT		33
60 #define NSIM_IPSEC_VALID		BIT(31)
61 
62 struct nsim_sa {
63 	struct xfrm_state *xs;
64 	__be32 ipaddr[4];
65 	u32 key[4];
66 	u32 salt;
67 	bool used;
68 	bool crypt;
69 	bool rx;
70 };
71 
72 struct nsim_ipsec {
73 	struct nsim_sa sa[NSIM_IPSEC_MAX_SA_COUNT];
74 	struct dentry *pfile;
75 	u32 count;
76 	u32 tx;
77 	u32 ok;
78 };
79 
80 struct netdevsim {
81 	struct net_device *netdev;
82 
83 	u64 tx_packets;
84 	u64 tx_bytes;
85 	struct u64_stats_sync syncp;
86 
87 	struct device dev;
88 	struct netdevsim_shared_dev *sdev;
89 
90 	struct dentry *ddir;
91 
92 	unsigned int num_vfs;
93 	struct nsim_vf_config *vfconfigs;
94 
95 	struct bpf_prog	*bpf_offloaded;
96 	u32 bpf_offloaded_id;
97 
98 	struct xdp_attachment_info xdp;
99 	struct xdp_attachment_info xdp_hw;
100 
101 	bool bpf_tc_accept;
102 	bool bpf_tc_non_bound_accept;
103 	bool bpf_xdpdrv_accept;
104 	bool bpf_xdpoffload_accept;
105 
106 	bool bpf_map_accept;
107 	struct devlink *devlink;
108 	struct nsim_ipsec ipsec;
109 };
110 
111 #ifdef CONFIG_BPF_SYSCALL
112 int nsim_bpf_init(struct netdevsim *ns);
113 void nsim_bpf_uninit(struct netdevsim *ns);
114 int nsim_bpf(struct net_device *dev, struct netdev_bpf *bpf);
115 int nsim_bpf_disable_tc(struct netdevsim *ns);
116 int nsim_bpf_setup_tc_block_cb(enum tc_setup_type type,
117 			       void *type_data, void *cb_priv);
118 #else
119 static inline int nsim_bpf_init(struct netdevsim *ns)
120 {
121 	return 0;
122 }
123 
124 static inline void nsim_bpf_uninit(struct netdevsim *ns)
125 {
126 }
127 
128 static inline int nsim_bpf(struct net_device *dev, struct netdev_bpf *bpf)
129 {
130 	return bpf->command == XDP_QUERY_PROG ? 0 : -EOPNOTSUPP;
131 }
132 
133 static inline int nsim_bpf_disable_tc(struct netdevsim *ns)
134 {
135 	return 0;
136 }
137 
138 static inline int
139 nsim_bpf_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
140 			   void *cb_priv)
141 {
142 	return -EOPNOTSUPP;
143 }
144 #endif
145 
146 enum nsim_resource_id {
147 	NSIM_RESOURCE_NONE,   /* DEVLINK_RESOURCE_ID_PARENT_TOP */
148 	NSIM_RESOURCE_IPV4,
149 	NSIM_RESOURCE_IPV4_FIB,
150 	NSIM_RESOURCE_IPV4_FIB_RULES,
151 	NSIM_RESOURCE_IPV6,
152 	NSIM_RESOURCE_IPV6_FIB,
153 	NSIM_RESOURCE_IPV6_FIB_RULES,
154 };
155 
156 int nsim_devlink_setup(struct netdevsim *ns);
157 void nsim_devlink_teardown(struct netdevsim *ns);
158 
159 int nsim_devlink_init(void);
160 void nsim_devlink_exit(void);
161 
162 int nsim_fib_init(void);
163 void nsim_fib_exit(void);
164 u64 nsim_fib_get_val(struct net *net, enum nsim_resource_id res_id, bool max);
165 int nsim_fib_set_max(struct net *net, enum nsim_resource_id res_id, u64 val,
166 		     struct netlink_ext_ack *extack);
167 
168 #if IS_ENABLED(CONFIG_XFRM_OFFLOAD)
169 void nsim_ipsec_init(struct netdevsim *ns);
170 void nsim_ipsec_teardown(struct netdevsim *ns);
171 bool nsim_ipsec_tx(struct netdevsim *ns, struct sk_buff *skb);
172 #else
173 static inline void nsim_ipsec_init(struct netdevsim *ns)
174 {
175 }
176 
177 static inline void nsim_ipsec_teardown(struct netdevsim *ns)
178 {
179 }
180 
181 static inline bool nsim_ipsec_tx(struct netdevsim *ns, struct sk_buff *skb)
182 {
183 	return true;
184 }
185 #endif
186 
187 static inline struct netdevsim *to_nsim(struct device *ptr)
188 {
189 	return container_of(ptr, struct netdevsim, dev);
190 }
191