xref: /openbmc/linux/include/net/pkt_cls.h (revision 55330f05)
11da177e4SLinus Torvalds #ifndef __NET_PKT_CLS_H
21da177e4SLinus Torvalds #define __NET_PKT_CLS_H
31da177e4SLinus Torvalds 
41da177e4SLinus Torvalds #include <linux/pkt_cls.h>
51da177e4SLinus Torvalds #include <net/sch_generic.h>
61da177e4SLinus Torvalds #include <net/act_api.h>
71da177e4SLinus Torvalds 
81da177e4SLinus Torvalds /* Basic packet classifier frontend definitions. */
91da177e4SLinus Torvalds 
10fd2c3ef7SEric Dumazet struct tcf_walker {
111da177e4SLinus Torvalds 	int	stop;
121da177e4SLinus Torvalds 	int	skip;
131da177e4SLinus Torvalds 	int	count;
141da177e4SLinus Torvalds 	int	(*fn)(struct tcf_proto *, unsigned long node, struct tcf_walker *);
151da177e4SLinus Torvalds };
161da177e4SLinus Torvalds 
175c15257fSJoe Perches int register_tcf_proto_ops(struct tcf_proto_ops *ops);
185c15257fSJoe Perches int unregister_tcf_proto_ops(struct tcf_proto_ops *ops);
191da177e4SLinus Torvalds 
201da177e4SLinus Torvalds static inline unsigned long
211da177e4SLinus Torvalds __cls_set_class(unsigned long *clp, unsigned long cl)
221da177e4SLinus Torvalds {
23a0efb80cSWANG Cong 	return xchg(clp, cl);
241da177e4SLinus Torvalds }
251da177e4SLinus Torvalds 
261da177e4SLinus Torvalds static inline unsigned long
271da177e4SLinus Torvalds cls_set_class(struct tcf_proto *tp, unsigned long *clp,
281da177e4SLinus Torvalds 	unsigned long cl)
291da177e4SLinus Torvalds {
301da177e4SLinus Torvalds 	unsigned long old_cl;
311da177e4SLinus Torvalds 
321da177e4SLinus Torvalds 	tcf_tree_lock(tp);
331da177e4SLinus Torvalds 	old_cl = __cls_set_class(clp, cl);
341da177e4SLinus Torvalds 	tcf_tree_unlock(tp);
351da177e4SLinus Torvalds 
361da177e4SLinus Torvalds 	return old_cl;
371da177e4SLinus Torvalds }
381da177e4SLinus Torvalds 
391da177e4SLinus Torvalds static inline void
401da177e4SLinus Torvalds tcf_bind_filter(struct tcf_proto *tp, struct tcf_result *r, unsigned long base)
411da177e4SLinus Torvalds {
421da177e4SLinus Torvalds 	unsigned long cl;
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds 	cl = tp->q->ops->cl_ops->bind_tcf(tp->q, base, r->classid);
451da177e4SLinus Torvalds 	cl = cls_set_class(tp, &r->class, cl);
461da177e4SLinus Torvalds 	if (cl)
471da177e4SLinus Torvalds 		tp->q->ops->cl_ops->unbind_tcf(tp->q, cl);
481da177e4SLinus Torvalds }
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds static inline void
511da177e4SLinus Torvalds tcf_unbind_filter(struct tcf_proto *tp, struct tcf_result *r)
521da177e4SLinus Torvalds {
531da177e4SLinus Torvalds 	unsigned long cl;
541da177e4SLinus Torvalds 
551da177e4SLinus Torvalds 	if ((cl = __cls_set_class(&r->class, 0)) != 0)
561da177e4SLinus Torvalds 		tp->q->ops->cl_ops->unbind_tcf(tp->q, cl);
571da177e4SLinus Torvalds }
581da177e4SLinus Torvalds 
59fd2c3ef7SEric Dumazet struct tcf_exts {
601da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ACT
6133be6271SWANG Cong 	__u32	type; /* for backward compat(TCA_OLD_COMPAT) */
6222dc13c8SWANG Cong 	int nr_actions;
6322dc13c8SWANG Cong 	struct tc_action **actions;
641da177e4SLinus Torvalds #endif
651da177e4SLinus Torvalds 	/* Map to export classifier specific extension TLV types to the
661da177e4SLinus Torvalds 	 * generic extensions API. Unsupported extensions must be set to 0.
671da177e4SLinus Torvalds 	 */
681da177e4SLinus Torvalds 	int action;
691da177e4SLinus Torvalds 	int police;
701da177e4SLinus Torvalds };
711da177e4SLinus Torvalds 
72b9a24bb7SWANG Cong static inline int tcf_exts_init(struct tcf_exts *exts, int action, int police)
7333be6271SWANG Cong {
7433be6271SWANG Cong #ifdef CONFIG_NET_CLS_ACT
755da57f42SWANG Cong 	exts->type = 0;
7622dc13c8SWANG Cong 	exts->nr_actions = 0;
7722dc13c8SWANG Cong 	exts->actions = kcalloc(TCA_ACT_MAX_PRIO, sizeof(struct tc_action *),
7822dc13c8SWANG Cong 				GFP_KERNEL);
79b9a24bb7SWANG Cong 	if (!exts->actions)
80b9a24bb7SWANG Cong 		return -ENOMEM;
8133be6271SWANG Cong #endif
825da57f42SWANG Cong 	exts->action = action;
835da57f42SWANG Cong 	exts->police = police;
84b9a24bb7SWANG Cong 	return 0;
8533be6271SWANG Cong }
8633be6271SWANG Cong 
871da177e4SLinus Torvalds /**
881da177e4SLinus Torvalds  * tcf_exts_is_predicative - check if a predicative extension is present
891da177e4SLinus Torvalds  * @exts: tc filter extensions handle
901da177e4SLinus Torvalds  *
911da177e4SLinus Torvalds  * Returns 1 if a predicative extension is present, i.e. an extension which
921da177e4SLinus Torvalds  * might cause further actions and thus overrule the regular tcf_result.
931da177e4SLinus Torvalds  */
941da177e4SLinus Torvalds static inline int
951da177e4SLinus Torvalds tcf_exts_is_predicative(struct tcf_exts *exts)
961da177e4SLinus Torvalds {
971da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ACT
9822dc13c8SWANG Cong 	return exts->nr_actions;
991da177e4SLinus Torvalds #else
1001da177e4SLinus Torvalds 	return 0;
1011da177e4SLinus Torvalds #endif
1021da177e4SLinus Torvalds }
1031da177e4SLinus Torvalds 
1041da177e4SLinus Torvalds /**
1051da177e4SLinus Torvalds  * tcf_exts_is_available - check if at least one extension is present
1061da177e4SLinus Torvalds  * @exts: tc filter extensions handle
1071da177e4SLinus Torvalds  *
1081da177e4SLinus Torvalds  * Returns 1 if at least one extension is present.
1091da177e4SLinus Torvalds  */
1101da177e4SLinus Torvalds static inline int
1111da177e4SLinus Torvalds tcf_exts_is_available(struct tcf_exts *exts)
1121da177e4SLinus Torvalds {
1131da177e4SLinus Torvalds 	/* All non-predicative extensions must be added here. */
1141da177e4SLinus Torvalds 	return tcf_exts_is_predicative(exts);
1151da177e4SLinus Torvalds }
1161da177e4SLinus Torvalds 
11722dc13c8SWANG Cong static inline void tcf_exts_to_list(const struct tcf_exts *exts,
11822dc13c8SWANG Cong 				    struct list_head *actions)
11922dc13c8SWANG Cong {
12022dc13c8SWANG Cong #ifdef CONFIG_NET_CLS_ACT
12122dc13c8SWANG Cong 	int i;
12222dc13c8SWANG Cong 
12322dc13c8SWANG Cong 	for (i = 0; i < exts->nr_actions; i++) {
12422dc13c8SWANG Cong 		struct tc_action *a = exts->actions[i];
12522dc13c8SWANG Cong 
126fa5effe7SHadar Hen Zion 		list_add_tail(&a->list, actions);
12722dc13c8SWANG Cong 	}
12822dc13c8SWANG Cong #endif
12922dc13c8SWANG Cong }
13022dc13c8SWANG Cong 
1311da177e4SLinus Torvalds /**
1321da177e4SLinus Torvalds  * tcf_exts_exec - execute tc filter extensions
1331da177e4SLinus Torvalds  * @skb: socket buffer
1341da177e4SLinus Torvalds  * @exts: tc filter extensions handle
1351da177e4SLinus Torvalds  * @res: desired result
1361da177e4SLinus Torvalds  *
1371da177e4SLinus Torvalds  * Executes all configured extensions. Returns 0 on a normal execution,
1381da177e4SLinus Torvalds  * a negative number if the filter must be considered unmatched or
1391da177e4SLinus Torvalds  * a positive action code (TC_ACT_*) which must be returned to the
1401da177e4SLinus Torvalds  * underlying layer.
1411da177e4SLinus Torvalds  */
1421da177e4SLinus Torvalds static inline int
1431da177e4SLinus Torvalds tcf_exts_exec(struct sk_buff *skb, struct tcf_exts *exts,
1441da177e4SLinus Torvalds 	       struct tcf_result *res)
1451da177e4SLinus Torvalds {
1461da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ACT
14722dc13c8SWANG Cong 	if (exts->nr_actions)
14822dc13c8SWANG Cong 		return tcf_action_exec(skb, exts->actions, exts->nr_actions,
14922dc13c8SWANG Cong 				       res);
1501da177e4SLinus Torvalds #endif
1511da177e4SLinus Torvalds 	return 0;
1521da177e4SLinus Torvalds }
1531da177e4SLinus Torvalds 
1542734437eSWANG Cong #ifdef CONFIG_NET_CLS_ACT
1552734437eSWANG Cong 
15622dc13c8SWANG Cong #define tc_no_actions(_exts)  ((_exts)->nr_actions == 0)
15722dc13c8SWANG Cong #define tc_single_action(_exts) ((_exts)->nr_actions == 1)
1582734437eSWANG Cong 
1592734437eSWANG Cong #else /* CONFIG_NET_CLS_ACT */
1602734437eSWANG Cong 
1612734437eSWANG Cong #define tc_no_actions(_exts) true
1622734437eSWANG Cong #define tc_single_action(_exts) false
1632734437eSWANG Cong 
1642734437eSWANG Cong #endif /* CONFIG_NET_CLS_ACT */
1652734437eSWANG Cong 
1665c15257fSJoe Perches int tcf_exts_validate(struct net *net, struct tcf_proto *tp,
167c1b52739SBenjamin LaHaise 		      struct nlattr **tb, struct nlattr *rate_tlv,
1682f7ef2f8SCong Wang 		      struct tcf_exts *exts, bool ovr);
16918d0264fSWANG Cong void tcf_exts_destroy(struct tcf_exts *exts);
1705c15257fSJoe Perches void tcf_exts_change(struct tcf_proto *tp, struct tcf_exts *dst,
1711da177e4SLinus Torvalds 		     struct tcf_exts *src);
1725da57f42SWANG Cong int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts);
1735da57f42SWANG Cong int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts);
1741da177e4SLinus Torvalds 
1751da177e4SLinus Torvalds /**
1761da177e4SLinus Torvalds  * struct tcf_pkt_info - packet information
1771da177e4SLinus Torvalds  */
178fd2c3ef7SEric Dumazet struct tcf_pkt_info {
1791da177e4SLinus Torvalds 	unsigned char *		ptr;
1801da177e4SLinus Torvalds 	int			nexthdr;
1811da177e4SLinus Torvalds };
1821da177e4SLinus Torvalds 
1831da177e4SLinus Torvalds #ifdef CONFIG_NET_EMATCH
1841da177e4SLinus Torvalds 
1851da177e4SLinus Torvalds struct tcf_ematch_ops;
1861da177e4SLinus Torvalds 
1871da177e4SLinus Torvalds /**
1881da177e4SLinus Torvalds  * struct tcf_ematch - extended match (ematch)
1891da177e4SLinus Torvalds  *
1901da177e4SLinus Torvalds  * @matchid: identifier to allow userspace to reidentify a match
1911da177e4SLinus Torvalds  * @flags: flags specifying attributes and the relation to other matches
1921da177e4SLinus Torvalds  * @ops: the operations lookup table of the corresponding ematch module
1931da177e4SLinus Torvalds  * @datalen: length of the ematch specific configuration data
1941da177e4SLinus Torvalds  * @data: ematch specific data
1951da177e4SLinus Torvalds  */
196fd2c3ef7SEric Dumazet struct tcf_ematch {
1971da177e4SLinus Torvalds 	struct tcf_ematch_ops * ops;
1981da177e4SLinus Torvalds 	unsigned long		data;
1991da177e4SLinus Torvalds 	unsigned int		datalen;
2001da177e4SLinus Torvalds 	u16			matchid;
2011da177e4SLinus Torvalds 	u16			flags;
20282a470f1SJohn Fastabend 	struct net		*net;
2031da177e4SLinus Torvalds };
2041da177e4SLinus Torvalds 
2051da177e4SLinus Torvalds static inline int tcf_em_is_container(struct tcf_ematch *em)
2061da177e4SLinus Torvalds {
2071da177e4SLinus Torvalds 	return !em->ops;
2081da177e4SLinus Torvalds }
2091da177e4SLinus Torvalds 
2101da177e4SLinus Torvalds static inline int tcf_em_is_simple(struct tcf_ematch *em)
2111da177e4SLinus Torvalds {
2121da177e4SLinus Torvalds 	return em->flags & TCF_EM_SIMPLE;
2131da177e4SLinus Torvalds }
2141da177e4SLinus Torvalds 
2151da177e4SLinus Torvalds static inline int tcf_em_is_inverted(struct tcf_ematch *em)
2161da177e4SLinus Torvalds {
2171da177e4SLinus Torvalds 	return em->flags & TCF_EM_INVERT;
2181da177e4SLinus Torvalds }
2191da177e4SLinus Torvalds 
2201da177e4SLinus Torvalds static inline int tcf_em_last_match(struct tcf_ematch *em)
2211da177e4SLinus Torvalds {
2221da177e4SLinus Torvalds 	return (em->flags & TCF_EM_REL_MASK) == TCF_EM_REL_END;
2231da177e4SLinus Torvalds }
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds static inline int tcf_em_early_end(struct tcf_ematch *em, int result)
2261da177e4SLinus Torvalds {
2271da177e4SLinus Torvalds 	if (tcf_em_last_match(em))
2281da177e4SLinus Torvalds 		return 1;
2291da177e4SLinus Torvalds 
2301da177e4SLinus Torvalds 	if (result == 0 && em->flags & TCF_EM_REL_AND)
2311da177e4SLinus Torvalds 		return 1;
2321da177e4SLinus Torvalds 
2331da177e4SLinus Torvalds 	if (result != 0 && em->flags & TCF_EM_REL_OR)
2341da177e4SLinus Torvalds 		return 1;
2351da177e4SLinus Torvalds 
2361da177e4SLinus Torvalds 	return 0;
2371da177e4SLinus Torvalds }
2381da177e4SLinus Torvalds 
2391da177e4SLinus Torvalds /**
2401da177e4SLinus Torvalds  * struct tcf_ematch_tree - ematch tree handle
2411da177e4SLinus Torvalds  *
2421da177e4SLinus Torvalds  * @hdr: ematch tree header supplied by userspace
2431da177e4SLinus Torvalds  * @matches: array of ematches
2441da177e4SLinus Torvalds  */
245fd2c3ef7SEric Dumazet struct tcf_ematch_tree {
2461da177e4SLinus Torvalds 	struct tcf_ematch_tree_hdr hdr;
2471da177e4SLinus Torvalds 	struct tcf_ematch *	matches;
2481da177e4SLinus Torvalds 
2491da177e4SLinus Torvalds };
2501da177e4SLinus Torvalds 
2511da177e4SLinus Torvalds /**
2521da177e4SLinus Torvalds  * struct tcf_ematch_ops - ematch module operations
2531da177e4SLinus Torvalds  *
2541da177e4SLinus Torvalds  * @kind: identifier (kind) of this ematch module
2551da177e4SLinus Torvalds  * @datalen: length of expected configuration data (optional)
2561da177e4SLinus Torvalds  * @change: called during validation (optional)
2571da177e4SLinus Torvalds  * @match: called during ematch tree evaluation, must return 1/0
2581da177e4SLinus Torvalds  * @destroy: called during destroyage (optional)
2591da177e4SLinus Torvalds  * @dump: called during dumping process (optional)
2601da177e4SLinus Torvalds  * @owner: owner, must be set to THIS_MODULE
2611da177e4SLinus Torvalds  * @link: link to previous/next ematch module (internal use)
2621da177e4SLinus Torvalds  */
263fd2c3ef7SEric Dumazet struct tcf_ematch_ops {
2641da177e4SLinus Torvalds 	int			kind;
2651da177e4SLinus Torvalds 	int			datalen;
26682a470f1SJohn Fastabend 	int			(*change)(struct net *net, void *,
2671da177e4SLinus Torvalds 					  int, struct tcf_ematch *);
2681da177e4SLinus Torvalds 	int			(*match)(struct sk_buff *, struct tcf_ematch *,
2691da177e4SLinus Torvalds 					 struct tcf_pkt_info *);
27082a470f1SJohn Fastabend 	void			(*destroy)(struct tcf_ematch *);
2711da177e4SLinus Torvalds 	int			(*dump)(struct sk_buff *, struct tcf_ematch *);
2721da177e4SLinus Torvalds 	struct module		*owner;
2731da177e4SLinus Torvalds 	struct list_head	link;
2741da177e4SLinus Torvalds };
2751da177e4SLinus Torvalds 
2765c15257fSJoe Perches int tcf_em_register(struct tcf_ematch_ops *);
2775c15257fSJoe Perches void tcf_em_unregister(struct tcf_ematch_ops *);
2785c15257fSJoe Perches int tcf_em_tree_validate(struct tcf_proto *, struct nlattr *,
2791da177e4SLinus Torvalds 			 struct tcf_ematch_tree *);
28082a470f1SJohn Fastabend void tcf_em_tree_destroy(struct tcf_ematch_tree *);
2815c15257fSJoe Perches int tcf_em_tree_dump(struct sk_buff *, struct tcf_ematch_tree *, int);
2825c15257fSJoe Perches int __tcf_em_tree_match(struct sk_buff *, struct tcf_ematch_tree *,
2831da177e4SLinus Torvalds 			struct tcf_pkt_info *);
2841da177e4SLinus Torvalds 
2851da177e4SLinus Torvalds /**
2861da177e4SLinus Torvalds  * tcf_em_tree_change - replace ematch tree of a running classifier
2871da177e4SLinus Torvalds  *
2881da177e4SLinus Torvalds  * @tp: classifier kind handle
2891da177e4SLinus Torvalds  * @dst: destination ematch tree variable
2901da177e4SLinus Torvalds  * @src: source ematch tree (temporary tree from tcf_em_tree_validate)
2911da177e4SLinus Torvalds  *
2921da177e4SLinus Torvalds  * This functions replaces the ematch tree in @dst with the ematch
2931da177e4SLinus Torvalds  * tree in @src. The classifier in charge of the ematch tree may be
2941da177e4SLinus Torvalds  * running.
2951da177e4SLinus Torvalds  */
2961da177e4SLinus Torvalds static inline void tcf_em_tree_change(struct tcf_proto *tp,
2971da177e4SLinus Torvalds 				      struct tcf_ematch_tree *dst,
2981da177e4SLinus Torvalds 				      struct tcf_ematch_tree *src)
2991da177e4SLinus Torvalds {
3001da177e4SLinus Torvalds 	tcf_tree_lock(tp);
3011da177e4SLinus Torvalds 	memcpy(dst, src, sizeof(*dst));
3021da177e4SLinus Torvalds 	tcf_tree_unlock(tp);
3031da177e4SLinus Torvalds }
3041da177e4SLinus Torvalds 
3051da177e4SLinus Torvalds /**
3061da177e4SLinus Torvalds  * tcf_em_tree_match - evaulate an ematch tree
3071da177e4SLinus Torvalds  *
3081da177e4SLinus Torvalds  * @skb: socket buffer of the packet in question
3091da177e4SLinus Torvalds  * @tree: ematch tree to be used for evaluation
3101da177e4SLinus Torvalds  * @info: packet information examined by classifier
3111da177e4SLinus Torvalds  *
3121da177e4SLinus Torvalds  * This function matches @skb against the ematch tree in @tree by going
3131da177e4SLinus Torvalds  * through all ematches respecting their logic relations returning
3141da177e4SLinus Torvalds  * as soon as the result is obvious.
3151da177e4SLinus Torvalds  *
3161da177e4SLinus Torvalds  * Returns 1 if the ematch tree as-one matches, no ematches are configured
3171da177e4SLinus Torvalds  * or ematch is not enabled in the kernel, otherwise 0 is returned.
3181da177e4SLinus Torvalds  */
3191da177e4SLinus Torvalds static inline int tcf_em_tree_match(struct sk_buff *skb,
3201da177e4SLinus Torvalds 				    struct tcf_ematch_tree *tree,
3211da177e4SLinus Torvalds 				    struct tcf_pkt_info *info)
3221da177e4SLinus Torvalds {
3231da177e4SLinus Torvalds 	if (tree->hdr.nmatches)
3241da177e4SLinus Torvalds 		return __tcf_em_tree_match(skb, tree, info);
3251da177e4SLinus Torvalds 	else
3261da177e4SLinus Torvalds 		return 1;
3271da177e4SLinus Torvalds }
3281da177e4SLinus Torvalds 
329db3d99c0SPatrick McHardy #define MODULE_ALIAS_TCF_EMATCH(kind)	MODULE_ALIAS("ematch-kind-" __stringify(kind))
330db3d99c0SPatrick McHardy 
3311da177e4SLinus Torvalds #else /* CONFIG_NET_EMATCH */
3321da177e4SLinus Torvalds 
333fd2c3ef7SEric Dumazet struct tcf_ematch_tree {
3341da177e4SLinus Torvalds };
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds #define tcf_em_tree_validate(tp, tb, t) ((void)(t), 0)
33782a470f1SJohn Fastabend #define tcf_em_tree_destroy(t) do { (void)(t); } while(0)
3381da177e4SLinus Torvalds #define tcf_em_tree_dump(skb, t, tlv) (0)
3391da177e4SLinus Torvalds #define tcf_em_tree_change(tp, dst, src) do { } while(0)
3401da177e4SLinus Torvalds #define tcf_em_tree_match(skb, t, info) ((void)(info), 1)
3411da177e4SLinus Torvalds 
3421da177e4SLinus Torvalds #endif /* CONFIG_NET_EMATCH */
3431da177e4SLinus Torvalds 
3441da177e4SLinus Torvalds static inline unsigned char * tcf_get_base_ptr(struct sk_buff *skb, int layer)
3451da177e4SLinus Torvalds {
3461da177e4SLinus Torvalds 	switch (layer) {
3471da177e4SLinus Torvalds 		case TCF_LAYER_LINK:
3481da177e4SLinus Torvalds 			return skb->data;
3491da177e4SLinus Torvalds 		case TCF_LAYER_NETWORK:
350d56f90a7SArnaldo Carvalho de Melo 			return skb_network_header(skb);
3511da177e4SLinus Torvalds 		case TCF_LAYER_TRANSPORT:
3529c70220bSArnaldo Carvalho de Melo 			return skb_transport_header(skb);
3531da177e4SLinus Torvalds 	}
3541da177e4SLinus Torvalds 
3551da177e4SLinus Torvalds 	return NULL;
3561da177e4SLinus Torvalds }
3571da177e4SLinus Torvalds 
358eddc9ec5SArnaldo Carvalho de Melo static inline int tcf_valid_offset(const struct sk_buff *skb,
359eddc9ec5SArnaldo Carvalho de Melo 				   const unsigned char *ptr, const int len)
3601da177e4SLinus Torvalds {
361da521b2cSDavid S. Miller 	return likely((ptr + len) <= skb_tail_pointer(skb) &&
362da521b2cSDavid S. Miller 		      ptr >= skb->head &&
363da521b2cSDavid S. Miller 		      (ptr <= (ptr + len)));
3641da177e4SLinus Torvalds }
3651da177e4SLinus Torvalds 
3661da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_IND
3670eeb8ffcSDenis V. Lunev #include <net/net_namespace.h>
3680eeb8ffcSDenis V. Lunev 
3691da177e4SLinus Torvalds static inline int
3702519a602SWANG Cong tcf_change_indev(struct net *net, struct nlattr *indev_tlv)
3711da177e4SLinus Torvalds {
3722519a602SWANG Cong 	char indev[IFNAMSIZ];
373c01003c2SPatrick McHardy 	struct net_device *dev;
374c01003c2SPatrick McHardy 
3752519a602SWANG Cong 	if (nla_strlcpy(indev, indev_tlv, IFNAMSIZ) >= IFNAMSIZ)
3762519a602SWANG Cong 		return -EINVAL;
3772519a602SWANG Cong 	dev = __dev_get_by_name(net, indev);
3782519a602SWANG Cong 	if (!dev)
3792519a602SWANG Cong 		return -ENODEV;
3802519a602SWANG Cong 	return dev->ifindex;
3811da177e4SLinus Torvalds }
3821da177e4SLinus Torvalds 
3832519a602SWANG Cong static inline bool
3842519a602SWANG Cong tcf_match_indev(struct sk_buff *skb, int ifindex)
3852519a602SWANG Cong {
3862519a602SWANG Cong 	if (!ifindex)
3872519a602SWANG Cong 		return true;
3882519a602SWANG Cong 	if  (!skb->skb_iif)
3892519a602SWANG Cong 		return false;
3902519a602SWANG Cong 	return ifindex == skb->skb_iif;
3911da177e4SLinus Torvalds }
3921da177e4SLinus Torvalds #endif /* CONFIG_NET_CLS_IND */
3931da177e4SLinus Torvalds 
394a1b7c5fdSJohn Fastabend struct tc_cls_u32_knode {
395a1b7c5fdSJohn Fastabend 	struct tcf_exts *exts;
396e014860eSJohn Fastabend 	struct tc_u32_sel *sel;
397a1b7c5fdSJohn Fastabend 	u32 handle;
398a1b7c5fdSJohn Fastabend 	u32 val;
399a1b7c5fdSJohn Fastabend 	u32 mask;
400a1b7c5fdSJohn Fastabend 	u32 link_handle;
401e014860eSJohn Fastabend 	u8 fshift;
402a1b7c5fdSJohn Fastabend };
403a1b7c5fdSJohn Fastabend 
404a1b7c5fdSJohn Fastabend struct tc_cls_u32_hnode {
405a1b7c5fdSJohn Fastabend 	u32 handle;
406a1b7c5fdSJohn Fastabend 	u32 prio;
407a1b7c5fdSJohn Fastabend 	unsigned int divisor;
408a1b7c5fdSJohn Fastabend };
409a1b7c5fdSJohn Fastabend 
410a1b7c5fdSJohn Fastabend enum tc_clsu32_command {
411a1b7c5fdSJohn Fastabend 	TC_CLSU32_NEW_KNODE,
412a1b7c5fdSJohn Fastabend 	TC_CLSU32_REPLACE_KNODE,
413a1b7c5fdSJohn Fastabend 	TC_CLSU32_DELETE_KNODE,
414a1b7c5fdSJohn Fastabend 	TC_CLSU32_NEW_HNODE,
415a1b7c5fdSJohn Fastabend 	TC_CLSU32_REPLACE_HNODE,
416a1b7c5fdSJohn Fastabend 	TC_CLSU32_DELETE_HNODE,
417a1b7c5fdSJohn Fastabend };
418a1b7c5fdSJohn Fastabend 
419a1b7c5fdSJohn Fastabend struct tc_cls_u32_offload {
420a1b7c5fdSJohn Fastabend 	/* knode values */
421a1b7c5fdSJohn Fastabend 	enum tc_clsu32_command command;
422a1b7c5fdSJohn Fastabend 	union {
423a1b7c5fdSJohn Fastabend 		struct tc_cls_u32_knode knode;
424a1b7c5fdSJohn Fastabend 		struct tc_cls_u32_hnode hnode;
425a1b7c5fdSJohn Fastabend 	};
426a1b7c5fdSJohn Fastabend };
427a1b7c5fdSJohn Fastabend 
42855330f05SHadar Hen Zion static inline bool tc_can_offload(const struct net_device *dev,
42955330f05SHadar Hen Zion 				  const struct tcf_proto *tp)
4306843e7a2SJohn Fastabend {
43192c075dbSDaniel Borkmann 	const struct Qdisc *sch = tp->q;
43292c075dbSDaniel Borkmann 	const struct Qdisc_class_ops *cops = sch->ops->cl_ops;
43392c075dbSDaniel Borkmann 
4342b6ab0d3SJohn Fastabend 	if (!(dev->features & NETIF_F_HW_TC))
4352b6ab0d3SJohn Fastabend 		return false;
4369e8ce79cSJohn Fastabend 	if (!dev->netdev_ops->ndo_setup_tc)
4379e8ce79cSJohn Fastabend 		return false;
43892c075dbSDaniel Borkmann 	if (cops && cops->tcf_cl_offload)
43992c075dbSDaniel Borkmann 		return cops->tcf_cl_offload(tp->classid);
4409e8ce79cSJohn Fastabend 
4419e8ce79cSJohn Fastabend 	return true;
4426843e7a2SJohn Fastabend }
4436843e7a2SJohn Fastabend 
44455330f05SHadar Hen Zion static inline bool tc_skip_hw(u32 flags)
44555330f05SHadar Hen Zion {
44655330f05SHadar Hen Zion 	return (flags & TCA_CLS_FLAGS_SKIP_HW) ? true : false;
44755330f05SHadar Hen Zion }
44855330f05SHadar Hen Zion 
44955330f05SHadar Hen Zion static inline bool tc_should_offload(const struct net_device *dev,
45055330f05SHadar Hen Zion 				     const struct tcf_proto *tp, u32 flags)
45155330f05SHadar Hen Zion {
45255330f05SHadar Hen Zion 	if (tc_skip_hw(flags))
45355330f05SHadar Hen Zion 		return false;
45455330f05SHadar Hen Zion 	return tc_can_offload(dev, tp);
45555330f05SHadar Hen Zion }
45655330f05SHadar Hen Zion 
457d34e3e18SSamudrala, Sridhar static inline bool tc_skip_sw(u32 flags)
458d34e3e18SSamudrala, Sridhar {
459d34e3e18SSamudrala, Sridhar 	return (flags & TCA_CLS_FLAGS_SKIP_SW) ? true : false;
460d34e3e18SSamudrala, Sridhar }
461d34e3e18SSamudrala, Sridhar 
462d34e3e18SSamudrala, Sridhar /* SKIP_HW and SKIP_SW are mutually exclusive flags. */
463d34e3e18SSamudrala, Sridhar static inline bool tc_flags_valid(u32 flags)
464d34e3e18SSamudrala, Sridhar {
465d34e3e18SSamudrala, Sridhar 	if (flags & ~(TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW))
466d34e3e18SSamudrala, Sridhar 		return false;
467d34e3e18SSamudrala, Sridhar 
468d34e3e18SSamudrala, Sridhar 	if (!(flags ^ (TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW)))
469d34e3e18SSamudrala, Sridhar 		return false;
470d34e3e18SSamudrala, Sridhar 
471d34e3e18SSamudrala, Sridhar 	return true;
472d34e3e18SSamudrala, Sridhar }
473d34e3e18SSamudrala, Sridhar 
4745b33f488SAmir Vadai enum tc_fl_command {
4755b33f488SAmir Vadai 	TC_CLSFLOWER_REPLACE,
4765b33f488SAmir Vadai 	TC_CLSFLOWER_DESTROY,
47710cbc684SAmir Vadai 	TC_CLSFLOWER_STATS,
4785b33f488SAmir Vadai };
4795b33f488SAmir Vadai 
4805b33f488SAmir Vadai struct tc_cls_flower_offload {
4815b33f488SAmir Vadai 	enum tc_fl_command command;
4828208d21bSAmir Vadai 	unsigned long cookie;
4835b33f488SAmir Vadai 	struct flow_dissector *dissector;
4845b33f488SAmir Vadai 	struct fl_flow_key *mask;
4855b33f488SAmir Vadai 	struct fl_flow_key *key;
4865b33f488SAmir Vadai 	struct tcf_exts *exts;
4875b33f488SAmir Vadai };
4885b33f488SAmir Vadai 
489b87f7936SYotam Gigi enum tc_matchall_command {
490b87f7936SYotam Gigi 	TC_CLSMATCHALL_REPLACE,
491b87f7936SYotam Gigi 	TC_CLSMATCHALL_DESTROY,
492b87f7936SYotam Gigi };
493b87f7936SYotam Gigi 
494b87f7936SYotam Gigi struct tc_cls_matchall_offload {
495b87f7936SYotam Gigi 	enum tc_matchall_command command;
496b87f7936SYotam Gigi 	struct tcf_exts *exts;
497b87f7936SYotam Gigi 	unsigned long cookie;
498b87f7936SYotam Gigi };
499b87f7936SYotam Gigi 
500332ae8e2SJakub Kicinski enum tc_clsbpf_command {
501332ae8e2SJakub Kicinski 	TC_CLSBPF_ADD,
502332ae8e2SJakub Kicinski 	TC_CLSBPF_REPLACE,
503332ae8e2SJakub Kicinski 	TC_CLSBPF_DESTROY,
50468d64063SJakub Kicinski 	TC_CLSBPF_STATS,
505332ae8e2SJakub Kicinski };
506332ae8e2SJakub Kicinski 
507332ae8e2SJakub Kicinski struct tc_cls_bpf_offload {
508332ae8e2SJakub Kicinski 	enum tc_clsbpf_command command;
509332ae8e2SJakub Kicinski 	struct tcf_exts *exts;
510332ae8e2SJakub Kicinski 	struct bpf_prog *prog;
511332ae8e2SJakub Kicinski 	const char *name;
512332ae8e2SJakub Kicinski 	bool exts_integrated;
5130d01d45fSJakub Kicinski 	u32 gen_flags;
514332ae8e2SJakub Kicinski };
515332ae8e2SJakub Kicinski 
5161da177e4SLinus Torvalds #endif
517