Linux Audio

Check our new training course

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