xref: /linux/include/net/flow_offload.h (revision 9406b485dea5e25bed7c81cd822747d494cc8bde)
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 <linux/netlink.h>
7 #include <net/flow_dissector.h>
8 #include <linux/rhashtable.h>
9 
10 struct flow_match {
11 	struct flow_dissector	*dissector;
12 	void			*mask;
13 	void			*key;
14 };
15 
16 struct flow_match_meta {
17 	struct flow_dissector_key_meta *key, *mask;
18 };
19 
20 struct flow_match_basic {
21 	struct flow_dissector_key_basic *key, *mask;
22 };
23 
24 struct flow_match_control {
25 	struct flow_dissector_key_control *key, *mask;
26 };
27 
28 struct flow_match_eth_addrs {
29 	struct flow_dissector_key_eth_addrs *key, *mask;
30 };
31 
32 struct flow_match_vlan {
33 	struct flow_dissector_key_vlan *key, *mask;
34 };
35 
36 struct flow_match_ipv4_addrs {
37 	struct flow_dissector_key_ipv4_addrs *key, *mask;
38 };
39 
40 struct flow_match_ipv6_addrs {
41 	struct flow_dissector_key_ipv6_addrs *key, *mask;
42 };
43 
44 struct flow_match_ip {
45 	struct flow_dissector_key_ip *key, *mask;
46 };
47 
48 struct flow_match_ports {
49 	struct flow_dissector_key_ports *key, *mask;
50 };
51 
52 struct flow_match_icmp {
53 	struct flow_dissector_key_icmp *key, *mask;
54 };
55 
56 struct flow_match_tcp {
57 	struct flow_dissector_key_tcp *key, *mask;
58 };
59 
60 struct flow_match_mpls {
61 	struct flow_dissector_key_mpls *key, *mask;
62 };
63 
64 struct flow_match_enc_keyid {
65 	struct flow_dissector_key_keyid *key, *mask;
66 };
67 
68 struct flow_match_enc_opts {
69 	struct flow_dissector_key_enc_opts *key, *mask;
70 };
71 
72 struct flow_match_ct {
73 	struct flow_dissector_key_ct *key, *mask;
74 };
75 
76 struct flow_rule;
77 
78 void flow_rule_match_meta(const struct flow_rule *rule,
79 			  struct flow_match_meta *out);
80 void flow_rule_match_basic(const struct flow_rule *rule,
81 			   struct flow_match_basic *out);
82 void flow_rule_match_control(const struct flow_rule *rule,
83 			     struct flow_match_control *out);
84 void flow_rule_match_eth_addrs(const struct flow_rule *rule,
85 			       struct flow_match_eth_addrs *out);
86 void flow_rule_match_vlan(const struct flow_rule *rule,
87 			  struct flow_match_vlan *out);
88 void flow_rule_match_cvlan(const struct flow_rule *rule,
89 			   struct flow_match_vlan *out);
90 void flow_rule_match_ipv4_addrs(const struct flow_rule *rule,
91 				struct flow_match_ipv4_addrs *out);
92 void flow_rule_match_ipv6_addrs(const struct flow_rule *rule,
93 				struct flow_match_ipv6_addrs *out);
94 void flow_rule_match_ip(const struct flow_rule *rule,
95 			struct flow_match_ip *out);
96 void flow_rule_match_ports(const struct flow_rule *rule,
97 			   struct flow_match_ports *out);
98 void flow_rule_match_tcp(const struct flow_rule *rule,
99 			 struct flow_match_tcp *out);
100 void flow_rule_match_icmp(const struct flow_rule *rule,
101 			  struct flow_match_icmp *out);
102 void flow_rule_match_mpls(const struct flow_rule *rule,
103 			  struct flow_match_mpls *out);
104 void flow_rule_match_enc_control(const struct flow_rule *rule,
105 				 struct flow_match_control *out);
106 void flow_rule_match_enc_ipv4_addrs(const struct flow_rule *rule,
107 				    struct flow_match_ipv4_addrs *out);
108 void flow_rule_match_enc_ipv6_addrs(const struct flow_rule *rule,
109 				    struct flow_match_ipv6_addrs *out);
110 void flow_rule_match_enc_ip(const struct flow_rule *rule,
111 			    struct flow_match_ip *out);
112 void flow_rule_match_enc_ports(const struct flow_rule *rule,
113 			       struct flow_match_ports *out);
114 void flow_rule_match_enc_keyid(const struct flow_rule *rule,
115 			       struct flow_match_enc_keyid *out);
116 void flow_rule_match_enc_opts(const struct flow_rule *rule,
117 			      struct flow_match_enc_opts *out);
118 void flow_rule_match_ct(const struct flow_rule *rule,
119 			struct flow_match_ct *out);
120 
121 enum flow_action_id {
122 	FLOW_ACTION_ACCEPT		= 0,
123 	FLOW_ACTION_DROP,
124 	FLOW_ACTION_TRAP,
125 	FLOW_ACTION_GOTO,
126 	FLOW_ACTION_REDIRECT,
127 	FLOW_ACTION_MIRRED,
128 	FLOW_ACTION_REDIRECT_INGRESS,
129 	FLOW_ACTION_MIRRED_INGRESS,
130 	FLOW_ACTION_VLAN_PUSH,
131 	FLOW_ACTION_VLAN_POP,
132 	FLOW_ACTION_VLAN_MANGLE,
133 	FLOW_ACTION_TUNNEL_ENCAP,
134 	FLOW_ACTION_TUNNEL_DECAP,
135 	FLOW_ACTION_MANGLE,
136 	FLOW_ACTION_ADD,
137 	FLOW_ACTION_CSUM,
138 	FLOW_ACTION_MARK,
139 	FLOW_ACTION_PTYPE,
140 	FLOW_ACTION_PRIORITY,
141 	FLOW_ACTION_WAKE,
142 	FLOW_ACTION_QUEUE,
143 	FLOW_ACTION_SAMPLE,
144 	FLOW_ACTION_POLICE,
145 	FLOW_ACTION_CT,
146 	FLOW_ACTION_CT_METADATA,
147 	FLOW_ACTION_MPLS_PUSH,
148 	FLOW_ACTION_MPLS_POP,
149 	FLOW_ACTION_MPLS_MANGLE,
150 	FLOW_ACTION_GATE,
151 	NUM_FLOW_ACTIONS,
152 };
153 
154 /* This is mirroring enum pedit_header_type definition for easy mapping between
155  * tc pedit action. Legacy TCA_PEDIT_KEY_EX_HDR_TYPE_NETWORK is mapped to
156  * FLOW_ACT_MANGLE_UNSPEC, which is supported by no driver.
157  */
158 enum flow_action_mangle_base {
159 	FLOW_ACT_MANGLE_UNSPEC		= 0,
160 	FLOW_ACT_MANGLE_HDR_TYPE_ETH,
161 	FLOW_ACT_MANGLE_HDR_TYPE_IP4,
162 	FLOW_ACT_MANGLE_HDR_TYPE_IP6,
163 	FLOW_ACT_MANGLE_HDR_TYPE_TCP,
164 	FLOW_ACT_MANGLE_HDR_TYPE_UDP,
165 };
166 
167 enum flow_action_hw_stats_bit {
168 	FLOW_ACTION_HW_STATS_IMMEDIATE_BIT,
169 	FLOW_ACTION_HW_STATS_DELAYED_BIT,
170 };
171 
172 enum flow_action_hw_stats {
173 	FLOW_ACTION_HW_STATS_DISABLED = 0,
174 	FLOW_ACTION_HW_STATS_IMMEDIATE =
175 		BIT(FLOW_ACTION_HW_STATS_IMMEDIATE_BIT),
176 	FLOW_ACTION_HW_STATS_DELAYED = BIT(FLOW_ACTION_HW_STATS_DELAYED_BIT),
177 	FLOW_ACTION_HW_STATS_ANY = FLOW_ACTION_HW_STATS_IMMEDIATE |
178 				   FLOW_ACTION_HW_STATS_DELAYED,
179 };
180 
181 typedef void (*action_destr)(void *priv);
182 
183 struct flow_action_cookie {
184 	u32 cookie_len;
185 	u8 cookie[];
186 };
187 
188 struct flow_action_cookie *flow_action_cookie_create(void *data,
189 						     unsigned int len,
190 						     gfp_t gfp);
191 void flow_action_cookie_destroy(struct flow_action_cookie *cookie);
192 
193 struct flow_action_entry {
194 	enum flow_action_id		id;
195 	enum flow_action_hw_stats	hw_stats;
196 	action_destr			destructor;
197 	void				*destructor_priv;
198 	union {
199 		u32			chain_index;	/* FLOW_ACTION_GOTO */
200 		struct net_device	*dev;		/* FLOW_ACTION_REDIRECT */
201 		struct {				/* FLOW_ACTION_VLAN */
202 			u16		vid;
203 			__be16		proto;
204 			u8		prio;
205 		} vlan;
206 		struct {				/* FLOW_ACTION_MANGLE */
207 							/* FLOW_ACTION_ADD */
208 			enum flow_action_mangle_base htype;
209 			u32		offset;
210 			u32		mask;
211 			u32		val;
212 		} mangle;
213 		struct ip_tunnel_info	*tunnel;	/* FLOW_ACTION_TUNNEL_ENCAP */
214 		u32			csum_flags;	/* FLOW_ACTION_CSUM */
215 		u32			mark;		/* FLOW_ACTION_MARK */
216 		u16                     ptype;          /* FLOW_ACTION_PTYPE */
217 		u32			priority;	/* FLOW_ACTION_PRIORITY */
218 		struct {				/* FLOW_ACTION_QUEUE */
219 			u32		ctx;
220 			u32		index;
221 			u8		vf;
222 		} queue;
223 		struct {				/* FLOW_ACTION_SAMPLE */
224 			struct psample_group	*psample_group;
225 			u32			rate;
226 			u32			trunc_size;
227 			bool			truncate;
228 		} sample;
229 		struct {				/* FLOW_ACTION_POLICE */
230 			s64			burst;
231 			u64			rate_bytes_ps;
232 		} police;
233 		struct {				/* FLOW_ACTION_CT */
234 			int action;
235 			u16 zone;
236 			struct nf_flowtable *flow_table;
237 		} ct;
238 		struct {
239 			unsigned long cookie;
240 			u32 mark;
241 			u32 labels[4];
242 		} ct_metadata;
243 		struct {				/* FLOW_ACTION_MPLS_PUSH */
244 			u32		label;
245 			__be16		proto;
246 			u8		tc;
247 			u8		bos;
248 			u8		ttl;
249 		} mpls_push;
250 		struct {				/* FLOW_ACTION_MPLS_POP */
251 			__be16		proto;
252 		} mpls_pop;
253 		struct {				/* FLOW_ACTION_MPLS_MANGLE */
254 			u32		label;
255 			u8		tc;
256 			u8		bos;
257 			u8		ttl;
258 		} mpls_mangle;
259 		struct {
260 			u32		index;
261 			s32		prio;
262 			u64		basetime;
263 			u64		cycletime;
264 			u64		cycletimeext;
265 			u32		num_entries;
266 			struct action_gate_entry *entries;
267 		} gate;
268 	};
269 	struct flow_action_cookie *cookie; /* user defined action cookie */
270 };
271 
272 struct flow_action {
273 	unsigned int			num_entries;
274 	struct flow_action_entry	entries[];
275 };
276 
277 static inline bool flow_action_has_entries(const struct flow_action *action)
278 {
279 	return action->num_entries;
280 }
281 
282 /**
283  * flow_action_has_one_action() - check if exactly one action is present
284  * @action: tc filter flow offload action
285  *
286  * Returns true if exactly one action is present.
287  */
288 static inline bool flow_offload_has_one_action(const struct flow_action *action)
289 {
290 	return action->num_entries == 1;
291 }
292 
293 #define flow_action_for_each(__i, __act, __actions)			\
294         for (__i = 0, __act = &(__actions)->entries[0];			\
295 	     __i < (__actions)->num_entries;				\
296 	     __act = &(__actions)->entries[++__i])
297 
298 static inline bool
299 flow_action_mixed_hw_stats_check(const struct flow_action *action,
300 				 struct netlink_ext_ack *extack)
301 {
302 	const struct flow_action_entry *action_entry;
303 	u8 uninitialized_var(last_hw_stats);
304 	int i;
305 
306 	if (flow_offload_has_one_action(action))
307 		return true;
308 
309 	flow_action_for_each(i, action_entry, action) {
310 		if (i && action_entry->hw_stats != last_hw_stats) {
311 			NL_SET_ERR_MSG_MOD(extack, "Mixing HW stats types for actions is not supported");
312 			return false;
313 		}
314 		last_hw_stats = action_entry->hw_stats;
315 	}
316 	return true;
317 }
318 
319 static inline const struct flow_action_entry *
320 flow_action_first_entry_get(const struct flow_action *action)
321 {
322 	WARN_ON(!flow_action_has_entries(action));
323 	return &action->entries[0];
324 }
325 
326 static inline bool
327 __flow_action_hw_stats_check(const struct flow_action *action,
328 			     struct netlink_ext_ack *extack,
329 			     bool check_allow_bit,
330 			     enum flow_action_hw_stats_bit allow_bit)
331 {
332 	const struct flow_action_entry *action_entry;
333 
334 	if (!flow_action_has_entries(action))
335 		return true;
336 	if (!flow_action_mixed_hw_stats_check(action, extack))
337 		return false;
338 	action_entry = flow_action_first_entry_get(action);
339 	if (!check_allow_bit &&
340 	    action_entry->hw_stats != FLOW_ACTION_HW_STATS_ANY) {
341 		NL_SET_ERR_MSG_MOD(extack, "Driver supports only default HW stats type \"any\"");
342 		return false;
343 	} else if (check_allow_bit &&
344 		   !(action_entry->hw_stats & BIT(allow_bit))) {
345 		NL_SET_ERR_MSG_MOD(extack, "Driver does not support selected HW stats type");
346 		return false;
347 	}
348 	return true;
349 }
350 
351 static inline bool
352 flow_action_hw_stats_check(const struct flow_action *action,
353 			   struct netlink_ext_ack *extack,
354 			   enum flow_action_hw_stats_bit allow_bit)
355 {
356 	return __flow_action_hw_stats_check(action, extack, true, allow_bit);
357 }
358 
359 static inline bool
360 flow_action_basic_hw_stats_check(const struct flow_action *action,
361 				 struct netlink_ext_ack *extack)
362 {
363 	return __flow_action_hw_stats_check(action, extack, false, 0);
364 }
365 
366 struct flow_rule {
367 	struct flow_match	match;
368 	struct flow_action	action;
369 };
370 
371 struct flow_rule *flow_rule_alloc(unsigned int num_actions);
372 
373 static inline bool flow_rule_match_key(const struct flow_rule *rule,
374 				       enum flow_dissector_key_id key)
375 {
376 	return dissector_uses_key(rule->match.dissector, key);
377 }
378 
379 struct flow_stats {
380 	u64	pkts;
381 	u64	bytes;
382 	u64	lastused;
383 	enum flow_action_hw_stats used_hw_stats;
384 	bool used_hw_stats_valid;
385 };
386 
387 static inline void flow_stats_update(struct flow_stats *flow_stats,
388 				     u64 bytes, u64 pkts, u64 lastused,
389 				     enum flow_action_hw_stats used_hw_stats)
390 {
391 	flow_stats->pkts	+= pkts;
392 	flow_stats->bytes	+= bytes;
393 	flow_stats->lastused	= max_t(u64, flow_stats->lastused, lastused);
394 
395 	/* The driver should pass value with a maximum of one bit set.
396 	 * Passing FLOW_ACTION_HW_STATS_ANY is invalid.
397 	 */
398 	WARN_ON(used_hw_stats == FLOW_ACTION_HW_STATS_ANY);
399 	flow_stats->used_hw_stats |= used_hw_stats;
400 	flow_stats->used_hw_stats_valid = true;
401 }
402 
403 enum flow_block_command {
404 	FLOW_BLOCK_BIND,
405 	FLOW_BLOCK_UNBIND,
406 };
407 
408 enum flow_block_binder_type {
409 	FLOW_BLOCK_BINDER_TYPE_UNSPEC,
410 	FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS,
411 	FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS,
412 };
413 
414 struct flow_block {
415 	struct list_head cb_list;
416 };
417 
418 struct netlink_ext_ack;
419 
420 struct flow_block_offload {
421 	enum flow_block_command command;
422 	enum flow_block_binder_type binder_type;
423 	bool block_shared;
424 	bool unlocked_driver_cb;
425 	struct net *net;
426 	struct flow_block *block;
427 	struct list_head cb_list;
428 	struct list_head *driver_block_list;
429 	struct netlink_ext_ack *extack;
430 };
431 
432 enum tc_setup_type;
433 typedef int flow_setup_cb_t(enum tc_setup_type type, void *type_data,
434 			    void *cb_priv);
435 
436 struct flow_block_cb {
437 	struct list_head	driver_list;
438 	struct list_head	list;
439 	flow_setup_cb_t		*cb;
440 	void			*cb_ident;
441 	void			*cb_priv;
442 	void			(*release)(void *cb_priv);
443 	unsigned int		refcnt;
444 };
445 
446 struct flow_block_cb *flow_block_cb_alloc(flow_setup_cb_t *cb,
447 					  void *cb_ident, void *cb_priv,
448 					  void (*release)(void *cb_priv));
449 void flow_block_cb_free(struct flow_block_cb *block_cb);
450 
451 struct flow_block_cb *flow_block_cb_lookup(struct flow_block *block,
452 					   flow_setup_cb_t *cb, void *cb_ident);
453 
454 void *flow_block_cb_priv(struct flow_block_cb *block_cb);
455 void flow_block_cb_incref(struct flow_block_cb *block_cb);
456 unsigned int flow_block_cb_decref(struct flow_block_cb *block_cb);
457 
458 static inline void flow_block_cb_add(struct flow_block_cb *block_cb,
459 				     struct flow_block_offload *offload)
460 {
461 	list_add_tail(&block_cb->list, &offload->cb_list);
462 }
463 
464 static inline void flow_block_cb_remove(struct flow_block_cb *block_cb,
465 					struct flow_block_offload *offload)
466 {
467 	list_move(&block_cb->list, &offload->cb_list);
468 }
469 
470 bool flow_block_cb_is_busy(flow_setup_cb_t *cb, void *cb_ident,
471 			   struct list_head *driver_block_list);
472 
473 int flow_block_cb_setup_simple(struct flow_block_offload *f,
474 			       struct list_head *driver_list,
475 			       flow_setup_cb_t *cb,
476 			       void *cb_ident, void *cb_priv, bool ingress_only);
477 
478 enum flow_cls_command {
479 	FLOW_CLS_REPLACE,
480 	FLOW_CLS_DESTROY,
481 	FLOW_CLS_STATS,
482 	FLOW_CLS_TMPLT_CREATE,
483 	FLOW_CLS_TMPLT_DESTROY,
484 };
485 
486 struct flow_cls_common_offload {
487 	u32 chain_index;
488 	__be16 protocol;
489 	u32 prio;
490 	struct netlink_ext_ack *extack;
491 };
492 
493 struct flow_cls_offload {
494 	struct flow_cls_common_offload common;
495 	enum flow_cls_command command;
496 	unsigned long cookie;
497 	struct flow_rule *rule;
498 	struct flow_stats stats;
499 	u32 classid;
500 };
501 
502 static inline struct flow_rule *
503 flow_cls_offload_flow_rule(struct flow_cls_offload *flow_cmd)
504 {
505 	return flow_cmd->rule;
506 }
507 
508 static inline void flow_block_init(struct flow_block *flow_block)
509 {
510 	INIT_LIST_HEAD(&flow_block->cb_list);
511 }
512 
513 typedef int flow_indr_block_bind_cb_t(struct net_device *dev, void *cb_priv,
514 				      enum tc_setup_type type, void *type_data);
515 
516 typedef void flow_indr_block_cmd_t(struct net_device *dev,
517 				   flow_indr_block_bind_cb_t *cb, void *cb_priv,
518 				   enum flow_block_command command);
519 
520 struct flow_indr_block_entry {
521 	flow_indr_block_cmd_t *cb;
522 	struct list_head	list;
523 };
524 
525 void flow_indr_add_block_cb(struct flow_indr_block_entry *entry);
526 
527 void flow_indr_del_block_cb(struct flow_indr_block_entry *entry);
528 
529 int __flow_indr_block_cb_register(struct net_device *dev, void *cb_priv,
530 				  flow_indr_block_bind_cb_t *cb,
531 				  void *cb_ident);
532 
533 void __flow_indr_block_cb_unregister(struct net_device *dev,
534 				     flow_indr_block_bind_cb_t *cb,
535 				     void *cb_ident);
536 
537 int flow_indr_block_cb_register(struct net_device *dev, void *cb_priv,
538 				flow_indr_block_bind_cb_t *cb, void *cb_ident);
539 
540 void flow_indr_block_cb_unregister(struct net_device *dev,
541 				   flow_indr_block_bind_cb_t *cb,
542 				   void *cb_ident);
543 
544 void flow_indr_block_call(struct net_device *dev,
545 			  struct flow_block_offload *bo,
546 			  enum flow_block_command command,
547 			  enum tc_setup_type type);
548 
549 #endif /* _NET_FLOW_OFFLOAD_H */
550