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