xref: /linux/include/net/flow_offload.h (revision 397692eab35cbbd83681880c6a2dbcdb9fd84386)
1 #ifndef _NET_FLOW_OFFLOAD_H
2 #define _NET_FLOW_OFFLOAD_H
3 
4 #include <linux/kernel.h>
5 #include <linux/list.h>
6 #include <net/flow_dissector.h>
7 #include <linux/rhashtable.h>
8 
9 struct flow_match {
10 	struct flow_dissector	*dissector;
11 	void			*mask;
12 	void			*key;
13 };
14 
15 struct flow_match_meta {
16 	struct flow_dissector_key_meta *key, *mask;
17 };
18 
19 struct flow_match_basic {
20 	struct flow_dissector_key_basic *key, *mask;
21 };
22 
23 struct flow_match_control {
24 	struct flow_dissector_key_control *key, *mask;
25 };
26 
27 struct flow_match_eth_addrs {
28 	struct flow_dissector_key_eth_addrs *key, *mask;
29 };
30 
31 struct flow_match_vlan {
32 	struct flow_dissector_key_vlan *key, *mask;
33 };
34 
35 struct flow_match_ipv4_addrs {
36 	struct flow_dissector_key_ipv4_addrs *key, *mask;
37 };
38 
39 struct flow_match_ipv6_addrs {
40 	struct flow_dissector_key_ipv6_addrs *key, *mask;
41 };
42 
43 struct flow_match_ip {
44 	struct flow_dissector_key_ip *key, *mask;
45 };
46 
47 struct flow_match_ports {
48 	struct flow_dissector_key_ports *key, *mask;
49 };
50 
51 struct flow_match_icmp {
52 	struct flow_dissector_key_icmp *key, *mask;
53 };
54 
55 struct flow_match_tcp {
56 	struct flow_dissector_key_tcp *key, *mask;
57 };
58 
59 struct flow_match_mpls {
60 	struct flow_dissector_key_mpls *key, *mask;
61 };
62 
63 struct flow_match_enc_keyid {
64 	struct flow_dissector_key_keyid *key, *mask;
65 };
66 
67 struct flow_match_enc_opts {
68 	struct flow_dissector_key_enc_opts *key, *mask;
69 };
70 
71 struct flow_rule;
72 
73 void flow_rule_match_meta(const struct flow_rule *rule,
74 			  struct flow_match_meta *out);
75 void flow_rule_match_basic(const struct flow_rule *rule,
76 			   struct flow_match_basic *out);
77 void flow_rule_match_control(const struct flow_rule *rule,
78 			     struct flow_match_control *out);
79 void flow_rule_match_eth_addrs(const struct flow_rule *rule,
80 			       struct flow_match_eth_addrs *out);
81 void flow_rule_match_vlan(const struct flow_rule *rule,
82 			  struct flow_match_vlan *out);
83 void flow_rule_match_cvlan(const struct flow_rule *rule,
84 			   struct flow_match_vlan *out);
85 void flow_rule_match_ipv4_addrs(const struct flow_rule *rule,
86 				struct flow_match_ipv4_addrs *out);
87 void flow_rule_match_ipv6_addrs(const struct flow_rule *rule,
88 				struct flow_match_ipv6_addrs *out);
89 void flow_rule_match_ip(const struct flow_rule *rule,
90 			struct flow_match_ip *out);
91 void flow_rule_match_ports(const struct flow_rule *rule,
92 			   struct flow_match_ports *out);
93 void flow_rule_match_tcp(const struct flow_rule *rule,
94 			 struct flow_match_tcp *out);
95 void flow_rule_match_icmp(const struct flow_rule *rule,
96 			  struct flow_match_icmp *out);
97 void flow_rule_match_mpls(const struct flow_rule *rule,
98 			  struct flow_match_mpls *out);
99 void flow_rule_match_enc_control(const struct flow_rule *rule,
100 				 struct flow_match_control *out);
101 void flow_rule_match_enc_ipv4_addrs(const struct flow_rule *rule,
102 				    struct flow_match_ipv4_addrs *out);
103 void flow_rule_match_enc_ipv6_addrs(const struct flow_rule *rule,
104 				    struct flow_match_ipv6_addrs *out);
105 void flow_rule_match_enc_ip(const struct flow_rule *rule,
106 			    struct flow_match_ip *out);
107 void flow_rule_match_enc_ports(const struct flow_rule *rule,
108 			       struct flow_match_ports *out);
109 void flow_rule_match_enc_keyid(const struct flow_rule *rule,
110 			       struct flow_match_enc_keyid *out);
111 void flow_rule_match_enc_opts(const struct flow_rule *rule,
112 			      struct flow_match_enc_opts *out);
113 
114 enum flow_action_id {
115 	FLOW_ACTION_ACCEPT		= 0,
116 	FLOW_ACTION_DROP,
117 	FLOW_ACTION_TRAP,
118 	FLOW_ACTION_GOTO,
119 	FLOW_ACTION_REDIRECT,
120 	FLOW_ACTION_MIRRED,
121 	FLOW_ACTION_REDIRECT_INGRESS,
122 	FLOW_ACTION_MIRRED_INGRESS,
123 	FLOW_ACTION_VLAN_PUSH,
124 	FLOW_ACTION_VLAN_POP,
125 	FLOW_ACTION_VLAN_MANGLE,
126 	FLOW_ACTION_TUNNEL_ENCAP,
127 	FLOW_ACTION_TUNNEL_DECAP,
128 	FLOW_ACTION_MANGLE,
129 	FLOW_ACTION_ADD,
130 	FLOW_ACTION_CSUM,
131 	FLOW_ACTION_MARK,
132 	FLOW_ACTION_PTYPE,
133 	FLOW_ACTION_WAKE,
134 	FLOW_ACTION_QUEUE,
135 	FLOW_ACTION_SAMPLE,
136 	FLOW_ACTION_POLICE,
137 	FLOW_ACTION_CT,
138 	FLOW_ACTION_MPLS_PUSH,
139 	FLOW_ACTION_MPLS_POP,
140 	FLOW_ACTION_MPLS_MANGLE,
141 	NUM_FLOW_ACTIONS,
142 };
143 
144 /* This is mirroring enum pedit_header_type definition for easy mapping between
145  * tc pedit action. Legacy TCA_PEDIT_KEY_EX_HDR_TYPE_NETWORK is mapped to
146  * FLOW_ACT_MANGLE_UNSPEC, which is supported by no driver.
147  */
148 enum flow_action_mangle_base {
149 	FLOW_ACT_MANGLE_UNSPEC		= 0,
150 	FLOW_ACT_MANGLE_HDR_TYPE_ETH,
151 	FLOW_ACT_MANGLE_HDR_TYPE_IP4,
152 	FLOW_ACT_MANGLE_HDR_TYPE_IP6,
153 	FLOW_ACT_MANGLE_HDR_TYPE_TCP,
154 	FLOW_ACT_MANGLE_HDR_TYPE_UDP,
155 };
156 
157 typedef void (*action_destr)(void *priv);
158 
159 struct flow_action_cookie {
160 	u32 cookie_len;
161 	u8 cookie[];
162 };
163 
164 struct flow_action_cookie *flow_action_cookie_create(void *data,
165 						     unsigned int len,
166 						     gfp_t gfp);
167 void flow_action_cookie_destroy(struct flow_action_cookie *cookie);
168 
169 struct flow_action_entry {
170 	enum flow_action_id		id;
171 	action_destr			destructor;
172 	void				*destructor_priv;
173 	union {
174 		u32			chain_index;	/* FLOW_ACTION_GOTO */
175 		struct net_device	*dev;		/* FLOW_ACTION_REDIRECT */
176 		struct {				/* FLOW_ACTION_VLAN */
177 			u16		vid;
178 			__be16		proto;
179 			u8		prio;
180 		} vlan;
181 		struct {				/* FLOW_ACTION_PACKET_EDIT */
182 			enum flow_action_mangle_base htype;
183 			u32		offset;
184 			u32		mask;
185 			u32		val;
186 		} mangle;
187 		struct ip_tunnel_info	*tunnel;	/* FLOW_ACTION_TUNNEL_ENCAP */
188 		u32			csum_flags;	/* FLOW_ACTION_CSUM */
189 		u32			mark;		/* FLOW_ACTION_MARK */
190 		u16                     ptype;          /* FLOW_ACTION_PTYPE */
191 		struct {				/* FLOW_ACTION_QUEUE */
192 			u32		ctx;
193 			u32		index;
194 			u8		vf;
195 		} queue;
196 		struct {				/* FLOW_ACTION_SAMPLE */
197 			struct psample_group	*psample_group;
198 			u32			rate;
199 			u32			trunc_size;
200 			bool			truncate;
201 		} sample;
202 		struct {				/* FLOW_ACTION_POLICE */
203 			s64			burst;
204 			u64			rate_bytes_ps;
205 		} police;
206 		struct {				/* FLOW_ACTION_CT */
207 			int action;
208 			u16 zone;
209 		} ct;
210 		struct {				/* FLOW_ACTION_MPLS_PUSH */
211 			u32		label;
212 			__be16		proto;
213 			u8		tc;
214 			u8		bos;
215 			u8		ttl;
216 		} mpls_push;
217 		struct {				/* FLOW_ACTION_MPLS_POP */
218 			__be16		proto;
219 		} mpls_pop;
220 		struct {				/* FLOW_ACTION_MPLS_MANGLE */
221 			u32		label;
222 			u8		tc;
223 			u8		bos;
224 			u8		ttl;
225 		} mpls_mangle;
226 	};
227 	struct flow_action_cookie *cookie; /* user defined action cookie */
228 };
229 
230 struct flow_action {
231 	unsigned int			num_entries;
232 	struct flow_action_entry	entries[];
233 };
234 
235 static inline bool flow_action_has_entries(const struct flow_action *action)
236 {
237 	return action->num_entries;
238 }
239 
240 /**
241  * flow_action_has_one_action() - check if exactly one action is present
242  * @action: tc filter flow offload action
243  *
244  * Returns true if exactly one action is present.
245  */
246 static inline bool flow_offload_has_one_action(const struct flow_action *action)
247 {
248 	return action->num_entries == 1;
249 }
250 
251 #define flow_action_for_each(__i, __act, __actions)			\
252         for (__i = 0, __act = &(__actions)->entries[0]; __i < (__actions)->num_entries; __act = &(__actions)->entries[++__i])
253 
254 struct flow_rule {
255 	struct flow_match	match;
256 	struct flow_action	action;
257 };
258 
259 struct flow_rule *flow_rule_alloc(unsigned int num_actions);
260 
261 static inline bool flow_rule_match_key(const struct flow_rule *rule,
262 				       enum flow_dissector_key_id key)
263 {
264 	return dissector_uses_key(rule->match.dissector, key);
265 }
266 
267 struct flow_stats {
268 	u64	pkts;
269 	u64	bytes;
270 	u64	lastused;
271 };
272 
273 static inline void flow_stats_update(struct flow_stats *flow_stats,
274 				     u64 bytes, u64 pkts, u64 lastused)
275 {
276 	flow_stats->pkts	+= pkts;
277 	flow_stats->bytes	+= bytes;
278 	flow_stats->lastused	= max_t(u64, flow_stats->lastused, lastused);
279 }
280 
281 enum flow_block_command {
282 	FLOW_BLOCK_BIND,
283 	FLOW_BLOCK_UNBIND,
284 };
285 
286 enum flow_block_binder_type {
287 	FLOW_BLOCK_BINDER_TYPE_UNSPEC,
288 	FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS,
289 	FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS,
290 };
291 
292 struct flow_block {
293 	struct list_head cb_list;
294 };
295 
296 struct netlink_ext_ack;
297 
298 struct flow_block_offload {
299 	enum flow_block_command command;
300 	enum flow_block_binder_type binder_type;
301 	bool block_shared;
302 	bool unlocked_driver_cb;
303 	struct net *net;
304 	struct flow_block *block;
305 	struct list_head cb_list;
306 	struct list_head *driver_block_list;
307 	struct netlink_ext_ack *extack;
308 };
309 
310 enum tc_setup_type;
311 typedef int flow_setup_cb_t(enum tc_setup_type type, void *type_data,
312 			    void *cb_priv);
313 
314 struct flow_block_cb {
315 	struct list_head	driver_list;
316 	struct list_head	list;
317 	flow_setup_cb_t		*cb;
318 	void			*cb_ident;
319 	void			*cb_priv;
320 	void			(*release)(void *cb_priv);
321 	unsigned int		refcnt;
322 };
323 
324 struct flow_block_cb *flow_block_cb_alloc(flow_setup_cb_t *cb,
325 					  void *cb_ident, void *cb_priv,
326 					  void (*release)(void *cb_priv));
327 void flow_block_cb_free(struct flow_block_cb *block_cb);
328 
329 struct flow_block_cb *flow_block_cb_lookup(struct flow_block *block,
330 					   flow_setup_cb_t *cb, void *cb_ident);
331 
332 void *flow_block_cb_priv(struct flow_block_cb *block_cb);
333 void flow_block_cb_incref(struct flow_block_cb *block_cb);
334 unsigned int flow_block_cb_decref(struct flow_block_cb *block_cb);
335 
336 static inline void flow_block_cb_add(struct flow_block_cb *block_cb,
337 				     struct flow_block_offload *offload)
338 {
339 	list_add_tail(&block_cb->list, &offload->cb_list);
340 }
341 
342 static inline void flow_block_cb_remove(struct flow_block_cb *block_cb,
343 					struct flow_block_offload *offload)
344 {
345 	list_move(&block_cb->list, &offload->cb_list);
346 }
347 
348 bool flow_block_cb_is_busy(flow_setup_cb_t *cb, void *cb_ident,
349 			   struct list_head *driver_block_list);
350 
351 int flow_block_cb_setup_simple(struct flow_block_offload *f,
352 			       struct list_head *driver_list,
353 			       flow_setup_cb_t *cb,
354 			       void *cb_ident, void *cb_priv, bool ingress_only);
355 
356 enum flow_cls_command {
357 	FLOW_CLS_REPLACE,
358 	FLOW_CLS_DESTROY,
359 	FLOW_CLS_STATS,
360 	FLOW_CLS_TMPLT_CREATE,
361 	FLOW_CLS_TMPLT_DESTROY,
362 };
363 
364 struct flow_cls_common_offload {
365 	u32 chain_index;
366 	__be16 protocol;
367 	u32 prio;
368 	struct netlink_ext_ack *extack;
369 };
370 
371 struct flow_cls_offload {
372 	struct flow_cls_common_offload common;
373 	enum flow_cls_command command;
374 	unsigned long cookie;
375 	struct flow_rule *rule;
376 	struct flow_stats stats;
377 	u32 classid;
378 };
379 
380 static inline struct flow_rule *
381 flow_cls_offload_flow_rule(struct flow_cls_offload *flow_cmd)
382 {
383 	return flow_cmd->rule;
384 }
385 
386 static inline void flow_block_init(struct flow_block *flow_block)
387 {
388 	INIT_LIST_HEAD(&flow_block->cb_list);
389 }
390 
391 typedef int flow_indr_block_bind_cb_t(struct net_device *dev, void *cb_priv,
392 				      enum tc_setup_type type, void *type_data);
393 
394 typedef void flow_indr_block_cmd_t(struct net_device *dev,
395 				   flow_indr_block_bind_cb_t *cb, void *cb_priv,
396 				   enum flow_block_command command);
397 
398 struct flow_indr_block_entry {
399 	flow_indr_block_cmd_t *cb;
400 	struct list_head	list;
401 };
402 
403 void flow_indr_add_block_cb(struct flow_indr_block_entry *entry);
404 
405 void flow_indr_del_block_cb(struct flow_indr_block_entry *entry);
406 
407 int __flow_indr_block_cb_register(struct net_device *dev, void *cb_priv,
408 				  flow_indr_block_bind_cb_t *cb,
409 				  void *cb_ident);
410 
411 void __flow_indr_block_cb_unregister(struct net_device *dev,
412 				     flow_indr_block_bind_cb_t *cb,
413 				     void *cb_ident);
414 
415 int flow_indr_block_cb_register(struct net_device *dev, void *cb_priv,
416 				flow_indr_block_bind_cb_t *cb, void *cb_ident);
417 
418 void flow_indr_block_cb_unregister(struct net_device *dev,
419 				   flow_indr_block_bind_cb_t *cb,
420 				   void *cb_ident);
421 
422 void flow_indr_block_call(struct net_device *dev,
423 			  struct flow_block_offload *bo,
424 			  enum flow_block_command command);
425 
426 #endif /* _NET_FLOW_OFFLOAD_H */
427