xref: /openbmc/linux/include/net/flow_offload.h (revision add48ba4)
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 	FLOW_ACTION_HW_STATS_DISABLED_BIT,
171 
172 	FLOW_ACTION_HW_STATS_NUM_BITS
173 };
174 
175 enum flow_action_hw_stats {
176 	FLOW_ACTION_HW_STATS_IMMEDIATE =
177 		BIT(FLOW_ACTION_HW_STATS_IMMEDIATE_BIT),
178 	FLOW_ACTION_HW_STATS_DELAYED = BIT(FLOW_ACTION_HW_STATS_DELAYED_BIT),
179 	FLOW_ACTION_HW_STATS_ANY = FLOW_ACTION_HW_STATS_IMMEDIATE |
180 				   FLOW_ACTION_HW_STATS_DELAYED,
181 	FLOW_ACTION_HW_STATS_DISABLED =
182 		BIT(FLOW_ACTION_HW_STATS_DISABLED_BIT),
183 	FLOW_ACTION_HW_STATS_DONT_CARE = BIT(FLOW_ACTION_HW_STATS_NUM_BITS) - 1,
184 };
185 
186 typedef void (*action_destr)(void *priv);
187 
188 struct flow_action_cookie {
189 	u32 cookie_len;
190 	u8 cookie[];
191 };
192 
193 struct flow_action_cookie *flow_action_cookie_create(void *data,
194 						     unsigned int len,
195 						     gfp_t gfp);
196 void flow_action_cookie_destroy(struct flow_action_cookie *cookie);
197 
198 struct flow_action_entry {
199 	enum flow_action_id		id;
200 	enum flow_action_hw_stats	hw_stats;
201 	action_destr			destructor;
202 	void				*destructor_priv;
203 	union {
204 		u32			chain_index;	/* FLOW_ACTION_GOTO */
205 		struct net_device	*dev;		/* FLOW_ACTION_REDIRECT */
206 		struct {				/* FLOW_ACTION_VLAN */
207 			u16		vid;
208 			__be16		proto;
209 			u8		prio;
210 		} vlan;
211 		struct {				/* FLOW_ACTION_MANGLE */
212 							/* FLOW_ACTION_ADD */
213 			enum flow_action_mangle_base htype;
214 			u32		offset;
215 			u32		mask;
216 			u32		val;
217 		} mangle;
218 		struct ip_tunnel_info	*tunnel;	/* FLOW_ACTION_TUNNEL_ENCAP */
219 		u32			csum_flags;	/* FLOW_ACTION_CSUM */
220 		u32			mark;		/* FLOW_ACTION_MARK */
221 		u16                     ptype;          /* FLOW_ACTION_PTYPE */
222 		u32			priority;	/* FLOW_ACTION_PRIORITY */
223 		struct {				/* FLOW_ACTION_QUEUE */
224 			u32		ctx;
225 			u32		index;
226 			u8		vf;
227 		} queue;
228 		struct {				/* FLOW_ACTION_SAMPLE */
229 			struct psample_group	*psample_group;
230 			u32			rate;
231 			u32			trunc_size;
232 			bool			truncate;
233 		} sample;
234 		struct {				/* FLOW_ACTION_POLICE */
235 			s64			burst;
236 			u64			rate_bytes_ps;
237 		} police;
238 		struct {				/* FLOW_ACTION_CT */
239 			int action;
240 			u16 zone;
241 			struct nf_flowtable *flow_table;
242 		} ct;
243 		struct {
244 			unsigned long cookie;
245 			u32 mark;
246 			u32 labels[4];
247 		} ct_metadata;
248 		struct {				/* FLOW_ACTION_MPLS_PUSH */
249 			u32		label;
250 			__be16		proto;
251 			u8		tc;
252 			u8		bos;
253 			u8		ttl;
254 		} mpls_push;
255 		struct {				/* FLOW_ACTION_MPLS_POP */
256 			__be16		proto;
257 		} mpls_pop;
258 		struct {				/* FLOW_ACTION_MPLS_MANGLE */
259 			u32		label;
260 			u8		tc;
261 			u8		bos;
262 			u8		ttl;
263 		} mpls_mangle;
264 		struct {
265 			u32		index;
266 			s32		prio;
267 			u64		basetime;
268 			u64		cycletime;
269 			u64		cycletimeext;
270 			u32		num_entries;
271 			struct action_gate_entry *entries;
272 		} gate;
273 	};
274 	struct flow_action_cookie *cookie; /* user defined action cookie */
275 };
276 
277 struct flow_action {
278 	unsigned int			num_entries;
279 	struct flow_action_entry	entries[];
280 };
281 
282 static inline bool flow_action_has_entries(const struct flow_action *action)
283 {
284 	return action->num_entries;
285 }
286 
287 /**
288  * flow_action_has_one_action() - check if exactly one action is present
289  * @action: tc filter flow offload action
290  *
291  * Returns true if exactly one action is present.
292  */
293 static inline bool flow_offload_has_one_action(const struct flow_action *action)
294 {
295 	return action->num_entries == 1;
296 }
297 
298 #define flow_action_for_each(__i, __act, __actions)			\
299         for (__i = 0, __act = &(__actions)->entries[0];			\
300 	     __i < (__actions)->num_entries;				\
301 	     __act = &(__actions)->entries[++__i])
302 
303 static inline bool
304 flow_action_mixed_hw_stats_check(const struct flow_action *action,
305 				 struct netlink_ext_ack *extack)
306 {
307 	const struct flow_action_entry *action_entry;
308 	u8 uninitialized_var(last_hw_stats);
309 	int i;
310 
311 	if (flow_offload_has_one_action(action))
312 		return true;
313 
314 	flow_action_for_each(i, action_entry, action) {
315 		if (i && action_entry->hw_stats != last_hw_stats) {
316 			NL_SET_ERR_MSG_MOD(extack, "Mixing HW stats types for actions is not supported");
317 			return false;
318 		}
319 		last_hw_stats = action_entry->hw_stats;
320 	}
321 	return true;
322 }
323 
324 static inline const struct flow_action_entry *
325 flow_action_first_entry_get(const struct flow_action *action)
326 {
327 	WARN_ON(!flow_action_has_entries(action));
328 	return &action->entries[0];
329 }
330 
331 static inline bool
332 __flow_action_hw_stats_check(const struct flow_action *action,
333 			     struct netlink_ext_ack *extack,
334 			     bool check_allow_bit,
335 			     enum flow_action_hw_stats_bit allow_bit)
336 {
337 	const struct flow_action_entry *action_entry;
338 
339 	if (!flow_action_has_entries(action))
340 		return true;
341 	if (!flow_action_mixed_hw_stats_check(action, extack))
342 		return false;
343 
344 	action_entry = flow_action_first_entry_get(action);
345 
346 	/* Zero is not a legal value for hw_stats, catch anyone passing it */
347 	WARN_ON_ONCE(!action_entry->hw_stats);
348 
349 	if (!check_allow_bit &&
350 	    ~action_entry->hw_stats & FLOW_ACTION_HW_STATS_ANY) {
351 		NL_SET_ERR_MSG_MOD(extack, "Driver supports only default HW stats type \"any\"");
352 		return false;
353 	} else if (check_allow_bit &&
354 		   !(action_entry->hw_stats & BIT(allow_bit))) {
355 		NL_SET_ERR_MSG_MOD(extack, "Driver does not support selected HW stats type");
356 		return false;
357 	}
358 	return true;
359 }
360 
361 static inline bool
362 flow_action_hw_stats_check(const struct flow_action *action,
363 			   struct netlink_ext_ack *extack,
364 			   enum flow_action_hw_stats_bit allow_bit)
365 {
366 	return __flow_action_hw_stats_check(action, extack, true, allow_bit);
367 }
368 
369 static inline bool
370 flow_action_basic_hw_stats_check(const struct flow_action *action,
371 				 struct netlink_ext_ack *extack)
372 {
373 	return __flow_action_hw_stats_check(action, extack, false, 0);
374 }
375 
376 struct flow_rule {
377 	struct flow_match	match;
378 	struct flow_action	action;
379 };
380 
381 struct flow_rule *flow_rule_alloc(unsigned int num_actions);
382 
383 static inline bool flow_rule_match_key(const struct flow_rule *rule,
384 				       enum flow_dissector_key_id key)
385 {
386 	return dissector_uses_key(rule->match.dissector, key);
387 }
388 
389 struct flow_stats {
390 	u64	pkts;
391 	u64	bytes;
392 	u64	lastused;
393 	enum flow_action_hw_stats used_hw_stats;
394 	bool used_hw_stats_valid;
395 };
396 
397 static inline void flow_stats_update(struct flow_stats *flow_stats,
398 				     u64 bytes, u64 pkts, u64 lastused,
399 				     enum flow_action_hw_stats used_hw_stats)
400 {
401 	flow_stats->pkts	+= pkts;
402 	flow_stats->bytes	+= bytes;
403 	flow_stats->lastused	= max_t(u64, flow_stats->lastused, lastused);
404 
405 	/* The driver should pass value with a maximum of one bit set.
406 	 * Passing FLOW_ACTION_HW_STATS_ANY is invalid.
407 	 */
408 	WARN_ON(used_hw_stats == FLOW_ACTION_HW_STATS_ANY);
409 	flow_stats->used_hw_stats |= used_hw_stats;
410 	flow_stats->used_hw_stats_valid = true;
411 }
412 
413 enum flow_block_command {
414 	FLOW_BLOCK_BIND,
415 	FLOW_BLOCK_UNBIND,
416 };
417 
418 enum flow_block_binder_type {
419 	FLOW_BLOCK_BINDER_TYPE_UNSPEC,
420 	FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS,
421 	FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS,
422 };
423 
424 struct flow_block {
425 	struct list_head cb_list;
426 };
427 
428 struct netlink_ext_ack;
429 
430 struct flow_block_offload {
431 	enum flow_block_command command;
432 	enum flow_block_binder_type binder_type;
433 	bool block_shared;
434 	bool unlocked_driver_cb;
435 	struct net *net;
436 	struct flow_block *block;
437 	struct list_head cb_list;
438 	struct list_head *driver_block_list;
439 	struct netlink_ext_ack *extack;
440 };
441 
442 enum tc_setup_type;
443 typedef int flow_setup_cb_t(enum tc_setup_type type, void *type_data,
444 			    void *cb_priv);
445 
446 struct flow_block_cb;
447 
448 struct flow_block_indr {
449 	struct list_head		list;
450 	struct net_device		*dev;
451 	enum flow_block_binder_type	binder_type;
452 	void				*data;
453 	void				(*cleanup)(struct flow_block_cb *block_cb);
454 };
455 
456 struct flow_block_cb {
457 	struct list_head	driver_list;
458 	struct list_head	list;
459 	flow_setup_cb_t		*cb;
460 	void			*cb_ident;
461 	void			*cb_priv;
462 	void			(*release)(void *cb_priv);
463 	struct flow_block_indr	indr;
464 	unsigned int		refcnt;
465 };
466 
467 struct flow_block_cb *flow_block_cb_alloc(flow_setup_cb_t *cb,
468 					  void *cb_ident, void *cb_priv,
469 					  void (*release)(void *cb_priv));
470 void flow_block_cb_free(struct flow_block_cb *block_cb);
471 
472 struct flow_block_cb *flow_block_cb_lookup(struct flow_block *block,
473 					   flow_setup_cb_t *cb, void *cb_ident);
474 
475 void *flow_block_cb_priv(struct flow_block_cb *block_cb);
476 void flow_block_cb_incref(struct flow_block_cb *block_cb);
477 unsigned int flow_block_cb_decref(struct flow_block_cb *block_cb);
478 
479 static inline void flow_block_cb_add(struct flow_block_cb *block_cb,
480 				     struct flow_block_offload *offload)
481 {
482 	list_add_tail(&block_cb->list, &offload->cb_list);
483 }
484 
485 static inline void flow_block_cb_remove(struct flow_block_cb *block_cb,
486 					struct flow_block_offload *offload)
487 {
488 	list_move(&block_cb->list, &offload->cb_list);
489 }
490 
491 bool flow_block_cb_is_busy(flow_setup_cb_t *cb, void *cb_ident,
492 			   struct list_head *driver_block_list);
493 
494 int flow_block_cb_setup_simple(struct flow_block_offload *f,
495 			       struct list_head *driver_list,
496 			       flow_setup_cb_t *cb,
497 			       void *cb_ident, void *cb_priv, bool ingress_only);
498 
499 enum flow_cls_command {
500 	FLOW_CLS_REPLACE,
501 	FLOW_CLS_DESTROY,
502 	FLOW_CLS_STATS,
503 	FLOW_CLS_TMPLT_CREATE,
504 	FLOW_CLS_TMPLT_DESTROY,
505 };
506 
507 struct flow_cls_common_offload {
508 	u32 chain_index;
509 	__be16 protocol;
510 	u32 prio;
511 	struct netlink_ext_ack *extack;
512 };
513 
514 struct flow_cls_offload {
515 	struct flow_cls_common_offload common;
516 	enum flow_cls_command command;
517 	unsigned long cookie;
518 	struct flow_rule *rule;
519 	struct flow_stats stats;
520 	u32 classid;
521 };
522 
523 static inline struct flow_rule *
524 flow_cls_offload_flow_rule(struct flow_cls_offload *flow_cmd)
525 {
526 	return flow_cmd->rule;
527 }
528 
529 static inline void flow_block_init(struct flow_block *flow_block)
530 {
531 	INIT_LIST_HEAD(&flow_block->cb_list);
532 }
533 
534 typedef int flow_indr_block_bind_cb_t(struct net_device *dev, void *cb_priv,
535 				      enum tc_setup_type type, void *type_data);
536 
537 int flow_indr_dev_register(flow_indr_block_bind_cb_t *cb, void *cb_priv);
538 void flow_indr_dev_unregister(flow_indr_block_bind_cb_t *cb, void *cb_priv,
539 			      flow_setup_cb_t *setup_cb);
540 int flow_indr_dev_setup_offload(struct net_device *dev,
541 				enum tc_setup_type type, void *data,
542 				struct flow_block_offload *bo,
543 				void (*cleanup)(struct flow_block_cb *block_cb));
544 
545 #endif /* _NET_FLOW_OFFLOAD_H */
546