xref: /openbmc/linux/include/net/pkt_cls.h (revision d03b195b)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
21da177e4SLinus Torvalds #ifndef __NET_PKT_CLS_H
31da177e4SLinus Torvalds #define __NET_PKT_CLS_H
41da177e4SLinus Torvalds 
51da177e4SLinus Torvalds #include <linux/pkt_cls.h>
67aa0045dSCong Wang #include <linux/workqueue.h>
71da177e4SLinus Torvalds #include <net/sch_generic.h>
81da177e4SLinus Torvalds #include <net/act_api.h>
9a5148626SJiri Pirko #include <net/net_namespace.h>
101da177e4SLinus Torvalds 
11cd11b164SPaolo Abeni /* TC action not accessible from user space */
12720f22feSJohn Hurley #define TC_ACT_CONSUMED		(TC_ACT_VALUE_MAX + 1)
13cd11b164SPaolo Abeni 
141da177e4SLinus Torvalds /* Basic packet classifier frontend definitions. */
151da177e4SLinus Torvalds 
16fd2c3ef7SEric Dumazet struct tcf_walker {
171da177e4SLinus Torvalds 	int	stop;
181da177e4SLinus Torvalds 	int	skip;
191da177e4SLinus Torvalds 	int	count;
206676d5e4SVlad Buslov 	bool	nonempty;
2101683a14SVlad Buslov 	unsigned long cookie;
228113c095SWANG Cong 	int	(*fn)(struct tcf_proto *, void *node, struct tcf_walker *);
231da177e4SLinus Torvalds };
241da177e4SLinus Torvalds 
255c15257fSJoe Perches int register_tcf_proto_ops(struct tcf_proto_ops *ops);
265c15257fSJoe Perches int unregister_tcf_proto_ops(struct tcf_proto_ops *ops);
271da177e4SLinus Torvalds 
288c4083b3SJiri Pirko struct tcf_block_ext_info {
2932f8c409SPablo Neira Ayuso 	enum flow_block_binder_type binder_type;
30c7eb7d72SJiri Pirko 	tcf_chain_head_change_t *chain_head_change;
31c7eb7d72SJiri Pirko 	void *chain_head_change_priv;
3248617387SJiri Pirko 	u32 block_index;
338c4083b3SJiri Pirko };
348c4083b3SJiri Pirko 
353625750fSPetr Machata struct tcf_qevent {
363625750fSPetr Machata 	struct tcf_block	*block;
373625750fSPetr Machata 	struct tcf_block_ext_info info;
383625750fSPetr Machata 	struct tcf_proto __rcu *filter_chain;
393625750fSPetr Machata };
403625750fSPetr Machata 
41acb67442SJiri Pirko struct tcf_block_cb;
42aaa908ffSCong Wang bool tcf_queue_work(struct rcu_work *rwork, work_func_t func);
43acb67442SJiri Pirko 
448ae70032SJiri Pirko #ifdef CONFIG_NET_CLS
451f3ed383SJiri Pirko struct tcf_chain *tcf_chain_get_by_act(struct tcf_block *block,
461f3ed383SJiri Pirko 				       u32 chain_index);
471f3ed383SJiri Pirko void tcf_chain_put_by_act(struct tcf_chain *chain);
48bbf73830SVlad Buslov struct tcf_chain *tcf_get_next_chain(struct tcf_block *block,
49bbf73830SVlad Buslov 				     struct tcf_chain *chain);
50fe2923afSVlad Buslov struct tcf_proto *tcf_get_next_proto(struct tcf_chain *chain,
510fca55edSVlad Buslov 				     struct tcf_proto *tp);
52f36fe1c4SJiri Pirko void tcf_block_netif_keep_dst(struct tcf_block *block);
536529eabaSJiri Pirko int tcf_block_get(struct tcf_block **p_block,
548d1a77f9SAlexander Aring 		  struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q,
558d1a77f9SAlexander Aring 		  struct netlink_ext_ack *extack);
56c7eb7d72SJiri Pirko int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q,
578d1a77f9SAlexander Aring 		      struct tcf_block_ext_info *ei,
588d1a77f9SAlexander Aring 		      struct netlink_ext_ack *extack);
596529eabaSJiri Pirko void tcf_block_put(struct tcf_block *block);
60c7eb7d72SJiri Pirko void tcf_block_put_ext(struct tcf_block *block, struct Qdisc *q,
618c4083b3SJiri Pirko 		       struct tcf_block_ext_info *ei);
6244186460SJiri Pirko 
6348617387SJiri Pirko static inline bool tcf_block_shared(struct tcf_block *block)
6448617387SJiri Pirko {
6548617387SJiri Pirko 	return block->index;
6648617387SJiri Pirko }
6748617387SJiri Pirko 
68c1a970d0SVlad Buslov static inline bool tcf_block_non_null_shared(struct tcf_block *block)
69c1a970d0SVlad Buslov {
70c1a970d0SVlad Buslov 	return block && block->index;
71c1a970d0SVlad Buslov }
72c1a970d0SVlad Buslov 
7344186460SJiri Pirko static inline struct Qdisc *tcf_block_q(struct tcf_block *block)
7444186460SJiri Pirko {
7548617387SJiri Pirko 	WARN_ON(tcf_block_shared(block));
7644186460SJiri Pirko 	return block->q;
7744186460SJiri Pirko }
7844186460SJiri Pirko 
7987d83093SJiri Pirko int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
8087d83093SJiri Pirko 		 struct tcf_result *res, bool compat_mode);
817d17c544SPaul Blakey int tcf_classify_ingress(struct sk_buff *skb,
827d17c544SPaul Blakey 			 const struct tcf_block *ingress_block,
837d17c544SPaul Blakey 			 const struct tcf_proto *tp, struct tcf_result *res,
847d17c544SPaul Blakey 			 bool compat_mode);
8587d83093SJiri Pirko 
868ae70032SJiri Pirko #else
8788c44a52SPieter Jansen van Vuuren static inline bool tcf_block_shared(struct tcf_block *block)
8888c44a52SPieter Jansen van Vuuren {
8988c44a52SPieter Jansen van Vuuren 	return false;
9088c44a52SPieter Jansen van Vuuren }
9188c44a52SPieter Jansen van Vuuren 
92c1a970d0SVlad Buslov static inline bool tcf_block_non_null_shared(struct tcf_block *block)
93c1a970d0SVlad Buslov {
94c1a970d0SVlad Buslov 	return false;
95c1a970d0SVlad Buslov }
96c1a970d0SVlad Buslov 
976529eabaSJiri Pirko static inline
986529eabaSJiri Pirko int tcf_block_get(struct tcf_block **p_block,
993c149091SSudip Mukherjee 		  struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q,
1003c149091SSudip Mukherjee 		  struct netlink_ext_ack *extack)
1016529eabaSJiri Pirko {
1026529eabaSJiri Pirko 	return 0;
1036529eabaSJiri Pirko }
1046529eabaSJiri Pirko 
1058c4083b3SJiri Pirko static inline
106c7eb7d72SJiri Pirko int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q,
10733c30a8bSQuentin Monnet 		      struct tcf_block_ext_info *ei,
10833c30a8bSQuentin Monnet 		      struct netlink_ext_ack *extack)
1098c4083b3SJiri Pirko {
1108c4083b3SJiri Pirko 	return 0;
1118c4083b3SJiri Pirko }
1128c4083b3SJiri Pirko 
1136529eabaSJiri Pirko static inline void tcf_block_put(struct tcf_block *block)
1148ae70032SJiri Pirko {
1158ae70032SJiri Pirko }
11687d83093SJiri Pirko 
1178c4083b3SJiri Pirko static inline
118c7eb7d72SJiri Pirko void tcf_block_put_ext(struct tcf_block *block, struct Qdisc *q,
1198c4083b3SJiri Pirko 		       struct tcf_block_ext_info *ei)
1208c4083b3SJiri Pirko {
1218c4083b3SJiri Pirko }
1228c4083b3SJiri Pirko 
12344186460SJiri Pirko static inline struct Qdisc *tcf_block_q(struct tcf_block *block)
12444186460SJiri Pirko {
12544186460SJiri Pirko 	return NULL;
12644186460SJiri Pirko }
12744186460SJiri Pirko 
128acb67442SJiri Pirko static inline
129a7323311SPablo Neira Ayuso int tc_setup_cb_block_register(struct tcf_block *block, flow_setup_cb_t *cb,
130acb67442SJiri Pirko 			       void *cb_priv)
131acb67442SJiri Pirko {
132acb67442SJiri Pirko 	return 0;
133acb67442SJiri Pirko }
134acb67442SJiri Pirko 
135acb67442SJiri Pirko static inline
136a7323311SPablo Neira Ayuso void tc_setup_cb_block_unregister(struct tcf_block *block, flow_setup_cb_t *cb,
137acb67442SJiri Pirko 				  void *cb_priv)
138acb67442SJiri Pirko {
139acb67442SJiri Pirko }
140acb67442SJiri Pirko 
14187d83093SJiri Pirko static inline int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
14287d83093SJiri Pirko 			       struct tcf_result *res, bool compat_mode)
14387d83093SJiri Pirko {
14487d83093SJiri Pirko 	return TC_ACT_UNSPEC;
14587d83093SJiri Pirko }
1469410c940SPaul Blakey 
1479410c940SPaul Blakey static inline int tcf_classify_ingress(struct sk_buff *skb,
1487d17c544SPaul Blakey 				       const struct tcf_block *ingress_block,
1499410c940SPaul Blakey 				       const struct tcf_proto *tp,
1509410c940SPaul Blakey 				       struct tcf_result *res, bool compat_mode)
1519410c940SPaul Blakey {
1529410c940SPaul Blakey 	return TC_ACT_UNSPEC;
1539410c940SPaul Blakey }
1549410c940SPaul Blakey 
1558ae70032SJiri Pirko #endif
156cf1facdaSJiri Pirko 
1571da177e4SLinus Torvalds static inline unsigned long
1581da177e4SLinus Torvalds __cls_set_class(unsigned long *clp, unsigned long cl)
1591da177e4SLinus Torvalds {
160a0efb80cSWANG Cong 	return xchg(clp, cl);
1611da177e4SLinus Torvalds }
1621da177e4SLinus Torvalds 
1632e24cd75SCong Wang static inline void
1642e24cd75SCong Wang __tcf_bind_filter(struct Qdisc *q, struct tcf_result *r, unsigned long base)
1651da177e4SLinus Torvalds {
1662e24cd75SCong Wang 	unsigned long cl;
1671da177e4SLinus Torvalds 
1682e24cd75SCong Wang 	cl = q->ops->cl_ops->bind_tcf(q, base, r->classid);
1692e24cd75SCong Wang 	cl = __cls_set_class(&r->class, cl);
1702e24cd75SCong Wang 	if (cl)
1712e24cd75SCong Wang 		q->ops->cl_ops->unbind_tcf(q, cl);
1721da177e4SLinus Torvalds }
1731da177e4SLinus Torvalds 
1741da177e4SLinus Torvalds static inline void
1751da177e4SLinus Torvalds tcf_bind_filter(struct tcf_proto *tp, struct tcf_result *r, unsigned long base)
1761da177e4SLinus Torvalds {
17734e3759cSJiri Pirko 	struct Qdisc *q = tp->chain->block->q;
1781da177e4SLinus Torvalds 
17934e3759cSJiri Pirko 	/* Check q as it is not set for shared blocks. In that case,
18034e3759cSJiri Pirko 	 * setting class is not supported.
18134e3759cSJiri Pirko 	 */
18234e3759cSJiri Pirko 	if (!q)
18334e3759cSJiri Pirko 		return;
1842e24cd75SCong Wang 	sch_tree_lock(q);
1852e24cd75SCong Wang 	__tcf_bind_filter(q, r, base);
1862e24cd75SCong Wang 	sch_tree_unlock(q);
1872e24cd75SCong Wang }
1882e24cd75SCong Wang 
1892e24cd75SCong Wang static inline void
1902e24cd75SCong Wang __tcf_unbind_filter(struct Qdisc *q, struct tcf_result *r)
1912e24cd75SCong Wang {
1922e24cd75SCong Wang 	unsigned long cl;
1932e24cd75SCong Wang 
1942e24cd75SCong Wang 	if ((cl = __cls_set_class(&r->class, 0)) != 0)
19534e3759cSJiri Pirko 		q->ops->cl_ops->unbind_tcf(q, cl);
1961da177e4SLinus Torvalds }
1971da177e4SLinus Torvalds 
1981da177e4SLinus Torvalds static inline void
1991da177e4SLinus Torvalds tcf_unbind_filter(struct tcf_proto *tp, struct tcf_result *r)
2001da177e4SLinus Torvalds {
20134e3759cSJiri Pirko 	struct Qdisc *q = tp->chain->block->q;
2021da177e4SLinus Torvalds 
20334e3759cSJiri Pirko 	if (!q)
20434e3759cSJiri Pirko 		return;
2052e24cd75SCong Wang 	__tcf_unbind_filter(q, r);
2061da177e4SLinus Torvalds }
2071da177e4SLinus Torvalds 
208fd2c3ef7SEric Dumazet struct tcf_exts {
2091da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ACT
21033be6271SWANG Cong 	__u32	type; /* for backward compat(TCA_OLD_COMPAT) */
21122dc13c8SWANG Cong 	int nr_actions;
21222dc13c8SWANG Cong 	struct tc_action **actions;
213e4b95c41SCong Wang 	struct net *net;
2141da177e4SLinus Torvalds #endif
2151da177e4SLinus Torvalds 	/* Map to export classifier specific extension TLV types to the
2161da177e4SLinus Torvalds 	 * generic extensions API. Unsupported extensions must be set to 0.
2171da177e4SLinus Torvalds 	 */
2181da177e4SLinus Torvalds 	int action;
2191da177e4SLinus Torvalds 	int police;
2201da177e4SLinus Torvalds };
2211da177e4SLinus Torvalds 
22214215108SCong Wang static inline int tcf_exts_init(struct tcf_exts *exts, struct net *net,
22314215108SCong Wang 				int action, int police)
22433be6271SWANG Cong {
22533be6271SWANG Cong #ifdef CONFIG_NET_CLS_ACT
2265da57f42SWANG Cong 	exts->type = 0;
22722dc13c8SWANG Cong 	exts->nr_actions = 0;
22814215108SCong Wang 	exts->net = net;
22922dc13c8SWANG Cong 	exts->actions = kcalloc(TCA_ACT_MAX_PRIO, sizeof(struct tc_action *),
23022dc13c8SWANG Cong 				GFP_KERNEL);
231b9a24bb7SWANG Cong 	if (!exts->actions)
232b9a24bb7SWANG Cong 		return -ENOMEM;
23333be6271SWANG Cong #endif
2345da57f42SWANG Cong 	exts->action = action;
2355da57f42SWANG Cong 	exts->police = police;
236b9a24bb7SWANG Cong 	return 0;
23733be6271SWANG Cong }
23833be6271SWANG Cong 
239e4b95c41SCong Wang /* Return false if the netns is being destroyed in cleanup_net(). Callers
240e4b95c41SCong Wang  * need to do cleanup synchronously in this case, otherwise may race with
241e4b95c41SCong Wang  * tc_action_net_exit(). Return true for other cases.
242e4b95c41SCong Wang  */
243e4b95c41SCong Wang static inline bool tcf_exts_get_net(struct tcf_exts *exts)
244e4b95c41SCong Wang {
245e4b95c41SCong Wang #ifdef CONFIG_NET_CLS_ACT
246e4b95c41SCong Wang 	exts->net = maybe_get_net(exts->net);
247e4b95c41SCong Wang 	return exts->net != NULL;
248e4b95c41SCong Wang #else
249e4b95c41SCong Wang 	return true;
250e4b95c41SCong Wang #endif
251e4b95c41SCong Wang }
252e4b95c41SCong Wang 
253e4b95c41SCong Wang static inline void tcf_exts_put_net(struct tcf_exts *exts)
254e4b95c41SCong Wang {
255e4b95c41SCong Wang #ifdef CONFIG_NET_CLS_ACT
256e4b95c41SCong Wang 	if (exts->net)
257e4b95c41SCong Wang 		put_net(exts->net);
258e4b95c41SCong Wang #endif
259e4b95c41SCong Wang }
260e4b95c41SCong Wang 
26122dc13c8SWANG Cong #ifdef CONFIG_NET_CLS_ACT
262244cd96aSCong Wang #define tcf_exts_for_each_action(i, a, exts) \
263244cd96aSCong Wang 	for (i = 0; i < TCA_ACT_MAX_PRIO && ((a) = (exts)->actions[i]); i++)
264244cd96aSCong Wang #else
265244cd96aSCong Wang #define tcf_exts_for_each_action(i, a, exts) \
266191672caSArnd Bergmann 	for (; 0; (void)(i), (void)(a), (void)(exts))
26722dc13c8SWANG Cong #endif
26822dc13c8SWANG Cong 
269d897a638SJakub Kicinski static inline void
270d897a638SJakub Kicinski tcf_exts_stats_update(const struct tcf_exts *exts,
2714b61d3e8SPo Liu 		      u64 bytes, u64 packets, u64 drops, u64 lastuse,
27293a129ebSJiri Pirko 		      u8 used_hw_stats, bool used_hw_stats_valid)
273d897a638SJakub Kicinski {
274d897a638SJakub Kicinski #ifdef CONFIG_NET_CLS_ACT
275d897a638SJakub Kicinski 	int i;
276d897a638SJakub Kicinski 
277d897a638SJakub Kicinski 	preempt_disable();
278d897a638SJakub Kicinski 
279d897a638SJakub Kicinski 	for (i = 0; i < exts->nr_actions; i++) {
280d897a638SJakub Kicinski 		struct tc_action *a = exts->actions[i];
281d897a638SJakub Kicinski 
2824b61d3e8SPo Liu 		tcf_action_stats_update(a, bytes, packets, drops,
2834b61d3e8SPo Liu 					lastuse, true);
28493a129ebSJiri Pirko 		a->used_hw_stats = used_hw_stats;
28593a129ebSJiri Pirko 		a->used_hw_stats_valid = used_hw_stats_valid;
286d897a638SJakub Kicinski 	}
287d897a638SJakub Kicinski 
288d897a638SJakub Kicinski 	preempt_enable();
289d897a638SJakub Kicinski #endif
290d897a638SJakub Kicinski }
291d897a638SJakub Kicinski 
2921da177e4SLinus Torvalds /**
2933bcc0cecSJiri Pirko  * tcf_exts_has_actions - check if at least one action is present
2943bcc0cecSJiri Pirko  * @exts: tc filter extensions handle
2953bcc0cecSJiri Pirko  *
2963bcc0cecSJiri Pirko  * Returns true if at least one action is present.
2973bcc0cecSJiri Pirko  */
2983bcc0cecSJiri Pirko static inline bool tcf_exts_has_actions(struct tcf_exts *exts)
2993bcc0cecSJiri Pirko {
3002734437eSWANG Cong #ifdef CONFIG_NET_CLS_ACT
3013bcc0cecSJiri Pirko 	return exts->nr_actions;
3023bcc0cecSJiri Pirko #else
3033bcc0cecSJiri Pirko 	return false;
3043bcc0cecSJiri Pirko #endif
3053bcc0cecSJiri Pirko }
3062734437eSWANG Cong 
3073bcc0cecSJiri Pirko /**
308af69afc5SJiri Pirko  * tcf_exts_exec - execute tc filter extensions
309af69afc5SJiri Pirko  * @skb: socket buffer
310af69afc5SJiri Pirko  * @exts: tc filter extensions handle
311af69afc5SJiri Pirko  * @res: desired result
312af69afc5SJiri Pirko  *
313af089e70SJiri Pirko  * Executes all configured extensions. Returns TC_ACT_OK on a normal execution,
314af69afc5SJiri Pirko  * a negative number if the filter must be considered unmatched or
315af69afc5SJiri Pirko  * a positive action code (TC_ACT_*) which must be returned to the
316af69afc5SJiri Pirko  * underlying layer.
317af69afc5SJiri Pirko  */
318af69afc5SJiri Pirko static inline int
319af69afc5SJiri Pirko tcf_exts_exec(struct sk_buff *skb, struct tcf_exts *exts,
320af69afc5SJiri Pirko 	      struct tcf_result *res)
321af69afc5SJiri Pirko {
322af69afc5SJiri Pirko #ifdef CONFIG_NET_CLS_ACT
323ec1a9ccaSJiri Pirko 	return tcf_action_exec(skb, exts->actions, exts->nr_actions, res);
324af69afc5SJiri Pirko #endif
325af089e70SJiri Pirko 	return TC_ACT_OK;
326af69afc5SJiri Pirko }
327af69afc5SJiri Pirko 
3285c15257fSJoe Perches int tcf_exts_validate(struct net *net, struct tcf_proto *tp,
329c1b52739SBenjamin LaHaise 		      struct nlattr **tb, struct nlattr *rate_tlv,
330ec6743a1SVlad Buslov 		      struct tcf_exts *exts, bool ovr, bool rtnl_held,
33150a56190SAlexander Aring 		      struct netlink_ext_ack *extack);
33218d0264fSWANG Cong void tcf_exts_destroy(struct tcf_exts *exts);
3339b0d4446SJiri Pirko void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src);
3345da57f42SWANG Cong int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts);
335ca44b738SVlad Buslov int tcf_exts_terse_dump(struct sk_buff *skb, struct tcf_exts *exts);
3365da57f42SWANG Cong int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts);
3371da177e4SLinus Torvalds 
3381da177e4SLinus Torvalds /**
3391da177e4SLinus Torvalds  * struct tcf_pkt_info - packet information
3401da177e4SLinus Torvalds  */
341fd2c3ef7SEric Dumazet struct tcf_pkt_info {
3421da177e4SLinus Torvalds 	unsigned char *		ptr;
3431da177e4SLinus Torvalds 	int			nexthdr;
3441da177e4SLinus Torvalds };
3451da177e4SLinus Torvalds 
3461da177e4SLinus Torvalds #ifdef CONFIG_NET_EMATCH
3471da177e4SLinus Torvalds 
3481da177e4SLinus Torvalds struct tcf_ematch_ops;
3491da177e4SLinus Torvalds 
3501da177e4SLinus Torvalds /**
3511da177e4SLinus Torvalds  * struct tcf_ematch - extended match (ematch)
3521da177e4SLinus Torvalds  *
3531da177e4SLinus Torvalds  * @matchid: identifier to allow userspace to reidentify a match
3541da177e4SLinus Torvalds  * @flags: flags specifying attributes and the relation to other matches
3551da177e4SLinus Torvalds  * @ops: the operations lookup table of the corresponding ematch module
3561da177e4SLinus Torvalds  * @datalen: length of the ematch specific configuration data
3571da177e4SLinus Torvalds  * @data: ematch specific data
3581da177e4SLinus Torvalds  */
359fd2c3ef7SEric Dumazet struct tcf_ematch {
3601da177e4SLinus Torvalds 	struct tcf_ematch_ops * ops;
3611da177e4SLinus Torvalds 	unsigned long		data;
3621da177e4SLinus Torvalds 	unsigned int		datalen;
3631da177e4SLinus Torvalds 	u16			matchid;
3641da177e4SLinus Torvalds 	u16			flags;
36582a470f1SJohn Fastabend 	struct net		*net;
3661da177e4SLinus Torvalds };
3671da177e4SLinus Torvalds 
3681da177e4SLinus Torvalds static inline int tcf_em_is_container(struct tcf_ematch *em)
3691da177e4SLinus Torvalds {
3701da177e4SLinus Torvalds 	return !em->ops;
3711da177e4SLinus Torvalds }
3721da177e4SLinus Torvalds 
3731da177e4SLinus Torvalds static inline int tcf_em_is_simple(struct tcf_ematch *em)
3741da177e4SLinus Torvalds {
3751da177e4SLinus Torvalds 	return em->flags & TCF_EM_SIMPLE;
3761da177e4SLinus Torvalds }
3771da177e4SLinus Torvalds 
3781da177e4SLinus Torvalds static inline int tcf_em_is_inverted(struct tcf_ematch *em)
3791da177e4SLinus Torvalds {
3801da177e4SLinus Torvalds 	return em->flags & TCF_EM_INVERT;
3811da177e4SLinus Torvalds }
3821da177e4SLinus Torvalds 
3831da177e4SLinus Torvalds static inline int tcf_em_last_match(struct tcf_ematch *em)
3841da177e4SLinus Torvalds {
3851da177e4SLinus Torvalds 	return (em->flags & TCF_EM_REL_MASK) == TCF_EM_REL_END;
3861da177e4SLinus Torvalds }
3871da177e4SLinus Torvalds 
3881da177e4SLinus Torvalds static inline int tcf_em_early_end(struct tcf_ematch *em, int result)
3891da177e4SLinus Torvalds {
3901da177e4SLinus Torvalds 	if (tcf_em_last_match(em))
3911da177e4SLinus Torvalds 		return 1;
3921da177e4SLinus Torvalds 
3931da177e4SLinus Torvalds 	if (result == 0 && em->flags & TCF_EM_REL_AND)
3941da177e4SLinus Torvalds 		return 1;
3951da177e4SLinus Torvalds 
3961da177e4SLinus Torvalds 	if (result != 0 && em->flags & TCF_EM_REL_OR)
3971da177e4SLinus Torvalds 		return 1;
3981da177e4SLinus Torvalds 
3991da177e4SLinus Torvalds 	return 0;
4001da177e4SLinus Torvalds }
4011da177e4SLinus Torvalds 
4021da177e4SLinus Torvalds /**
4031da177e4SLinus Torvalds  * struct tcf_ematch_tree - ematch tree handle
4041da177e4SLinus Torvalds  *
4051da177e4SLinus Torvalds  * @hdr: ematch tree header supplied by userspace
4061da177e4SLinus Torvalds  * @matches: array of ematches
4071da177e4SLinus Torvalds  */
408fd2c3ef7SEric Dumazet struct tcf_ematch_tree {
4091da177e4SLinus Torvalds 	struct tcf_ematch_tree_hdr hdr;
4101da177e4SLinus Torvalds 	struct tcf_ematch *	matches;
4111da177e4SLinus Torvalds 
4121da177e4SLinus Torvalds };
4131da177e4SLinus Torvalds 
4141da177e4SLinus Torvalds /**
4151da177e4SLinus Torvalds  * struct tcf_ematch_ops - ematch module operations
4161da177e4SLinus Torvalds  *
4171da177e4SLinus Torvalds  * @kind: identifier (kind) of this ematch module
4181da177e4SLinus Torvalds  * @datalen: length of expected configuration data (optional)
4191da177e4SLinus Torvalds  * @change: called during validation (optional)
4201da177e4SLinus Torvalds  * @match: called during ematch tree evaluation, must return 1/0
4211da177e4SLinus Torvalds  * @destroy: called during destroyage (optional)
4221da177e4SLinus Torvalds  * @dump: called during dumping process (optional)
4231da177e4SLinus Torvalds  * @owner: owner, must be set to THIS_MODULE
4241da177e4SLinus Torvalds  * @link: link to previous/next ematch module (internal use)
4251da177e4SLinus Torvalds  */
426fd2c3ef7SEric Dumazet struct tcf_ematch_ops {
4271da177e4SLinus Torvalds 	int			kind;
4281da177e4SLinus Torvalds 	int			datalen;
42982a470f1SJohn Fastabend 	int			(*change)(struct net *net, void *,
4301da177e4SLinus Torvalds 					  int, struct tcf_ematch *);
4311da177e4SLinus Torvalds 	int			(*match)(struct sk_buff *, struct tcf_ematch *,
4321da177e4SLinus Torvalds 					 struct tcf_pkt_info *);
43382a470f1SJohn Fastabend 	void			(*destroy)(struct tcf_ematch *);
4341da177e4SLinus Torvalds 	int			(*dump)(struct sk_buff *, struct tcf_ematch *);
4351da177e4SLinus Torvalds 	struct module		*owner;
4361da177e4SLinus Torvalds 	struct list_head	link;
4371da177e4SLinus Torvalds };
4381da177e4SLinus Torvalds 
4395c15257fSJoe Perches int tcf_em_register(struct tcf_ematch_ops *);
4405c15257fSJoe Perches void tcf_em_unregister(struct tcf_ematch_ops *);
4415c15257fSJoe Perches int tcf_em_tree_validate(struct tcf_proto *, struct nlattr *,
4421da177e4SLinus Torvalds 			 struct tcf_ematch_tree *);
44382a470f1SJohn Fastabend void tcf_em_tree_destroy(struct tcf_ematch_tree *);
4445c15257fSJoe Perches int tcf_em_tree_dump(struct sk_buff *, struct tcf_ematch_tree *, int);
4455c15257fSJoe Perches int __tcf_em_tree_match(struct sk_buff *, struct tcf_ematch_tree *,
4461da177e4SLinus Torvalds 			struct tcf_pkt_info *);
4471da177e4SLinus Torvalds 
4481da177e4SLinus Torvalds /**
4491da177e4SLinus Torvalds  * tcf_em_tree_match - evaulate an ematch tree
4501da177e4SLinus Torvalds  *
4511da177e4SLinus Torvalds  * @skb: socket buffer of the packet in question
4521da177e4SLinus Torvalds  * @tree: ematch tree to be used for evaluation
4531da177e4SLinus Torvalds  * @info: packet information examined by classifier
4541da177e4SLinus Torvalds  *
4551da177e4SLinus Torvalds  * This function matches @skb against the ematch tree in @tree by going
4561da177e4SLinus Torvalds  * through all ematches respecting their logic relations returning
4571da177e4SLinus Torvalds  * as soon as the result is obvious.
4581da177e4SLinus Torvalds  *
4591da177e4SLinus Torvalds  * Returns 1 if the ematch tree as-one matches, no ematches are configured
4601da177e4SLinus Torvalds  * or ematch is not enabled in the kernel, otherwise 0 is returned.
4611da177e4SLinus Torvalds  */
4621da177e4SLinus Torvalds static inline int tcf_em_tree_match(struct sk_buff *skb,
4631da177e4SLinus Torvalds 				    struct tcf_ematch_tree *tree,
4641da177e4SLinus Torvalds 				    struct tcf_pkt_info *info)
4651da177e4SLinus Torvalds {
4661da177e4SLinus Torvalds 	if (tree->hdr.nmatches)
4671da177e4SLinus Torvalds 		return __tcf_em_tree_match(skb, tree, info);
4681da177e4SLinus Torvalds 	else
4691da177e4SLinus Torvalds 		return 1;
4701da177e4SLinus Torvalds }
4711da177e4SLinus Torvalds 
472db3d99c0SPatrick McHardy #define MODULE_ALIAS_TCF_EMATCH(kind)	MODULE_ALIAS("ematch-kind-" __stringify(kind))
473db3d99c0SPatrick McHardy 
4741da177e4SLinus Torvalds #else /* CONFIG_NET_EMATCH */
4751da177e4SLinus Torvalds 
476fd2c3ef7SEric Dumazet struct tcf_ematch_tree {
4771da177e4SLinus Torvalds };
4781da177e4SLinus Torvalds 
4791da177e4SLinus Torvalds #define tcf_em_tree_validate(tp, tb, t) ((void)(t), 0)
48082a470f1SJohn Fastabend #define tcf_em_tree_destroy(t) do { (void)(t); } while(0)
4811da177e4SLinus Torvalds #define tcf_em_tree_dump(skb, t, tlv) (0)
4821da177e4SLinus Torvalds #define tcf_em_tree_match(skb, t, info) ((void)(info), 1)
4831da177e4SLinus Torvalds 
4841da177e4SLinus Torvalds #endif /* CONFIG_NET_EMATCH */
4851da177e4SLinus Torvalds 
4861da177e4SLinus Torvalds static inline unsigned char * tcf_get_base_ptr(struct sk_buff *skb, int layer)
4871da177e4SLinus Torvalds {
4881da177e4SLinus Torvalds 	switch (layer) {
4891da177e4SLinus Torvalds 		case TCF_LAYER_LINK:
490d3303a65SWolfgang Bumiller 			return skb_mac_header(skb);
4911da177e4SLinus Torvalds 		case TCF_LAYER_NETWORK:
492d56f90a7SArnaldo Carvalho de Melo 			return skb_network_header(skb);
4931da177e4SLinus Torvalds 		case TCF_LAYER_TRANSPORT:
4949c70220bSArnaldo Carvalho de Melo 			return skb_transport_header(skb);
4951da177e4SLinus Torvalds 	}
4961da177e4SLinus Torvalds 
4971da177e4SLinus Torvalds 	return NULL;
4981da177e4SLinus Torvalds }
4991da177e4SLinus Torvalds 
500eddc9ec5SArnaldo Carvalho de Melo static inline int tcf_valid_offset(const struct sk_buff *skb,
501eddc9ec5SArnaldo Carvalho de Melo 				   const unsigned char *ptr, const int len)
5021da177e4SLinus Torvalds {
503da521b2cSDavid S. Miller 	return likely((ptr + len) <= skb_tail_pointer(skb) &&
504da521b2cSDavid S. Miller 		      ptr >= skb->head &&
505da521b2cSDavid S. Miller 		      (ptr <= (ptr + len)));
5061da177e4SLinus Torvalds }
5071da177e4SLinus Torvalds 
5081da177e4SLinus Torvalds static inline int
5091057c55fSAlexander Aring tcf_change_indev(struct net *net, struct nlattr *indev_tlv,
5101057c55fSAlexander Aring 		 struct netlink_ext_ack *extack)
5111da177e4SLinus Torvalds {
5122519a602SWANG Cong 	char indev[IFNAMSIZ];
513c01003c2SPatrick McHardy 	struct net_device *dev;
514c01003c2SPatrick McHardy 
515872f6903SFrancis Laniel 	if (nla_strscpy(indev, indev_tlv, IFNAMSIZ) < 0) {
516e4a58ef3SGuillaume Nault 		NL_SET_ERR_MSG_ATTR(extack, indev_tlv,
517e4a58ef3SGuillaume Nault 				    "Interface name too long");
5182519a602SWANG Cong 		return -EINVAL;
5191057c55fSAlexander Aring 	}
5202519a602SWANG Cong 	dev = __dev_get_by_name(net, indev);
521e4a58ef3SGuillaume Nault 	if (!dev) {
522e4a58ef3SGuillaume Nault 		NL_SET_ERR_MSG_ATTR(extack, indev_tlv,
523e4a58ef3SGuillaume Nault 				    "Network device not found");
5242519a602SWANG Cong 		return -ENODEV;
525e4a58ef3SGuillaume Nault 	}
5262519a602SWANG Cong 	return dev->ifindex;
5271da177e4SLinus Torvalds }
5281da177e4SLinus Torvalds 
5292519a602SWANG Cong static inline bool
5302519a602SWANG Cong tcf_match_indev(struct sk_buff *skb, int ifindex)
5312519a602SWANG Cong {
5322519a602SWANG Cong 	if (!ifindex)
5332519a602SWANG Cong 		return true;
5342519a602SWANG Cong 	if  (!skb->skb_iif)
5352519a602SWANG Cong 		return false;
5362519a602SWANG Cong 	return ifindex == skb->skb_iif;
5371da177e4SLinus Torvalds }
5381da177e4SLinus Torvalds 
5393a7b6861SPablo Neira Ayuso int tc_setup_flow_action(struct flow_action *flow_action,
540b15e7a6eSVlad Buslov 			 const struct tcf_exts *exts);
5415a6ff4b1SVlad Buslov void tc_cleanup_flow_action(struct flow_action *flow_action);
5425a6ff4b1SVlad Buslov 
543aeb3fecdSCong Wang int tc_setup_cb_call(struct tcf_block *block, enum tc_setup_type type,
54440119211SVlad Buslov 		     void *type_data, bool err_stop, bool rtnl_held);
54540119211SVlad Buslov int tc_setup_cb_add(struct tcf_block *block, struct tcf_proto *tp,
54640119211SVlad Buslov 		    enum tc_setup_type type, void *type_data, bool err_stop,
54740119211SVlad Buslov 		    u32 *flags, unsigned int *in_hw_count, bool rtnl_held);
54840119211SVlad Buslov int tc_setup_cb_replace(struct tcf_block *block, struct tcf_proto *tp,
54940119211SVlad Buslov 			enum tc_setup_type type, void *type_data, bool err_stop,
55040119211SVlad Buslov 			u32 *old_flags, unsigned int *old_in_hw_count,
55140119211SVlad Buslov 			u32 *new_flags, unsigned int *new_in_hw_count,
55240119211SVlad Buslov 			bool rtnl_held);
55340119211SVlad Buslov int tc_setup_cb_destroy(struct tcf_block *block, struct tcf_proto *tp,
55440119211SVlad Buslov 			enum tc_setup_type type, void *type_data, bool err_stop,
55540119211SVlad Buslov 			u32 *flags, unsigned int *in_hw_count, bool rtnl_held);
55640119211SVlad Buslov int tc_setup_cb_reoffload(struct tcf_block *block, struct tcf_proto *tp,
55740119211SVlad Buslov 			  bool add, flow_setup_cb_t *cb,
55840119211SVlad Buslov 			  enum tc_setup_type type, void *type_data,
55940119211SVlad Buslov 			  void *cb_priv, u32 *flags, unsigned int *in_hw_count);
560e3ab786bSPablo Neira Ayuso unsigned int tcf_exts_num_actions(struct tcf_exts *exts);
561717503b9SJiri Pirko 
5623625750fSPetr Machata #ifdef CONFIG_NET_CLS_ACT
5633625750fSPetr Machata int tcf_qevent_init(struct tcf_qevent *qe, struct Qdisc *sch,
5643625750fSPetr Machata 		    enum flow_block_binder_type binder_type,
5653625750fSPetr Machata 		    struct nlattr *block_index_attr,
5663625750fSPetr Machata 		    struct netlink_ext_ack *extack);
5673625750fSPetr Machata void tcf_qevent_destroy(struct tcf_qevent *qe, struct Qdisc *sch);
5683625750fSPetr Machata int tcf_qevent_validate_change(struct tcf_qevent *qe, struct nlattr *block_index_attr,
5693625750fSPetr Machata 			       struct netlink_ext_ack *extack);
5703625750fSPetr Machata struct sk_buff *tcf_qevent_handle(struct tcf_qevent *qe, struct Qdisc *sch, struct sk_buff *skb,
57155f656cdSPetr Machata 				  struct sk_buff **to_free, int *ret);
5723625750fSPetr Machata int tcf_qevent_dump(struct sk_buff *skb, int attr_name, struct tcf_qevent *qe);
5733625750fSPetr Machata #else
5743625750fSPetr Machata static inline int tcf_qevent_init(struct tcf_qevent *qe, struct Qdisc *sch,
5753625750fSPetr Machata 				  enum flow_block_binder_type binder_type,
5763625750fSPetr Machata 				  struct nlattr *block_index_attr,
5773625750fSPetr Machata 				  struct netlink_ext_ack *extack)
5783625750fSPetr Machata {
5793625750fSPetr Machata 	return 0;
5803625750fSPetr Machata }
5813625750fSPetr Machata 
5823625750fSPetr Machata static inline void tcf_qevent_destroy(struct tcf_qevent *qe, struct Qdisc *sch)
5833625750fSPetr Machata {
5843625750fSPetr Machata }
5853625750fSPetr Machata 
5863625750fSPetr Machata static inline int tcf_qevent_validate_change(struct tcf_qevent *qe, struct nlattr *block_index_attr,
5873625750fSPetr Machata 					     struct netlink_ext_ack *extack)
5883625750fSPetr Machata {
5893625750fSPetr Machata 	return 0;
5903625750fSPetr Machata }
5913625750fSPetr Machata 
5923625750fSPetr Machata static inline struct sk_buff *
5933625750fSPetr Machata tcf_qevent_handle(struct tcf_qevent *qe, struct Qdisc *sch, struct sk_buff *skb,
59455f656cdSPetr Machata 		  struct sk_buff **to_free, int *ret)
5953625750fSPetr Machata {
5963625750fSPetr Machata 	return skb;
5973625750fSPetr Machata }
5983625750fSPetr Machata 
5993625750fSPetr Machata static inline int tcf_qevent_dump(struct sk_buff *skb, int attr_name, struct tcf_qevent *qe)
6003625750fSPetr Machata {
6013625750fSPetr Machata 	return 0;
6023625750fSPetr Machata }
6033625750fSPetr Machata #endif
6043625750fSPetr Machata 
605a1b7c5fdSJohn Fastabend struct tc_cls_u32_knode {
606a1b7c5fdSJohn Fastabend 	struct tcf_exts *exts;
607068ceb35SJakub Kicinski 	struct tcf_result *res;
608e014860eSJohn Fastabend 	struct tc_u32_sel *sel;
609a1b7c5fdSJohn Fastabend 	u32 handle;
610a1b7c5fdSJohn Fastabend 	u32 val;
611a1b7c5fdSJohn Fastabend 	u32 mask;
612a1b7c5fdSJohn Fastabend 	u32 link_handle;
613e014860eSJohn Fastabend 	u8 fshift;
614a1b7c5fdSJohn Fastabend };
615a1b7c5fdSJohn Fastabend 
616a1b7c5fdSJohn Fastabend struct tc_cls_u32_hnode {
617a1b7c5fdSJohn Fastabend 	u32 handle;
618a1b7c5fdSJohn Fastabend 	u32 prio;
619a1b7c5fdSJohn Fastabend 	unsigned int divisor;
620a1b7c5fdSJohn Fastabend };
621a1b7c5fdSJohn Fastabend 
622a1b7c5fdSJohn Fastabend enum tc_clsu32_command {
623a1b7c5fdSJohn Fastabend 	TC_CLSU32_NEW_KNODE,
624a1b7c5fdSJohn Fastabend 	TC_CLSU32_REPLACE_KNODE,
625a1b7c5fdSJohn Fastabend 	TC_CLSU32_DELETE_KNODE,
626a1b7c5fdSJohn Fastabend 	TC_CLSU32_NEW_HNODE,
627a1b7c5fdSJohn Fastabend 	TC_CLSU32_REPLACE_HNODE,
628a1b7c5fdSJohn Fastabend 	TC_CLSU32_DELETE_HNODE,
629a1b7c5fdSJohn Fastabend };
630a1b7c5fdSJohn Fastabend 
631a1b7c5fdSJohn Fastabend struct tc_cls_u32_offload {
632f9e30088SPablo Neira Ayuso 	struct flow_cls_common_offload common;
633a1b7c5fdSJohn Fastabend 	/* knode values */
634a1b7c5fdSJohn Fastabend 	enum tc_clsu32_command command;
635a1b7c5fdSJohn Fastabend 	union {
636a1b7c5fdSJohn Fastabend 		struct tc_cls_u32_knode knode;
637a1b7c5fdSJohn Fastabend 		struct tc_cls_u32_hnode hnode;
638a1b7c5fdSJohn Fastabend 	};
639a1b7c5fdSJohn Fastabend };
640a1b7c5fdSJohn Fastabend 
6417b06e8aeSJiri Pirko static inline bool tc_can_offload(const struct net_device *dev)
6426843e7a2SJohn Fastabend {
64370b5aee4SJiri Pirko 	return dev->features & NETIF_F_HW_TC;
6446843e7a2SJohn Fastabend }
6456843e7a2SJohn Fastabend 
646f9eda14fSQuentin Monnet static inline bool tc_can_offload_extack(const struct net_device *dev,
647f9eda14fSQuentin Monnet 					 struct netlink_ext_ack *extack)
648f9eda14fSQuentin Monnet {
649f9eda14fSQuentin Monnet 	bool can = tc_can_offload(dev);
650f9eda14fSQuentin Monnet 
651f9eda14fSQuentin Monnet 	if (!can)
652f9eda14fSQuentin Monnet 		NL_SET_ERR_MSG(extack, "TC offload is disabled on net device");
653f9eda14fSQuentin Monnet 
654f9eda14fSQuentin Monnet 	return can;
655f9eda14fSQuentin Monnet }
656f9eda14fSQuentin Monnet 
657878db9f0SJakub Kicinski static inline bool
658878db9f0SJakub Kicinski tc_cls_can_offload_and_chain0(const struct net_device *dev,
659f9e30088SPablo Neira Ayuso 			      struct flow_cls_common_offload *common)
660878db9f0SJakub Kicinski {
661878db9f0SJakub Kicinski 	if (!tc_can_offload_extack(dev, common->extack))
662878db9f0SJakub Kicinski 		return false;
663878db9f0SJakub Kicinski 	if (common->chain_index) {
664878db9f0SJakub Kicinski 		NL_SET_ERR_MSG(common->extack,
665878db9f0SJakub Kicinski 			       "Driver supports only offload of chain 0");
666878db9f0SJakub Kicinski 		return false;
667878db9f0SJakub Kicinski 	}
668878db9f0SJakub Kicinski 	return true;
669878db9f0SJakub Kicinski }
670878db9f0SJakub Kicinski 
67155330f05SHadar Hen Zion static inline bool tc_skip_hw(u32 flags)
67255330f05SHadar Hen Zion {
67355330f05SHadar Hen Zion 	return (flags & TCA_CLS_FLAGS_SKIP_HW) ? true : false;
67455330f05SHadar Hen Zion }
67555330f05SHadar Hen Zion 
676d34e3e18SSamudrala, Sridhar static inline bool tc_skip_sw(u32 flags)
677d34e3e18SSamudrala, Sridhar {
678d34e3e18SSamudrala, Sridhar 	return (flags & TCA_CLS_FLAGS_SKIP_SW) ? true : false;
679d34e3e18SSamudrala, Sridhar }
680d34e3e18SSamudrala, Sridhar 
681d34e3e18SSamudrala, Sridhar /* SKIP_HW and SKIP_SW are mutually exclusive flags. */
682d34e3e18SSamudrala, Sridhar static inline bool tc_flags_valid(u32 flags)
683d34e3e18SSamudrala, Sridhar {
68481c7288bSMarcelo Ricardo Leitner 	if (flags & ~(TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW |
68581c7288bSMarcelo Ricardo Leitner 		      TCA_CLS_FLAGS_VERBOSE))
686d34e3e18SSamudrala, Sridhar 		return false;
687d34e3e18SSamudrala, Sridhar 
68881c7288bSMarcelo Ricardo Leitner 	flags &= TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW;
689d34e3e18SSamudrala, Sridhar 	if (!(flags ^ (TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW)))
690d34e3e18SSamudrala, Sridhar 		return false;
691d34e3e18SSamudrala, Sridhar 
692d34e3e18SSamudrala, Sridhar 	return true;
693d34e3e18SSamudrala, Sridhar }
694d34e3e18SSamudrala, Sridhar 
695e696028aSOr Gerlitz static inline bool tc_in_hw(u32 flags)
696e696028aSOr Gerlitz {
697e696028aSOr Gerlitz 	return (flags & TCA_CLS_FLAGS_IN_HW) ? true : false;
698e696028aSOr Gerlitz }
699e696028aSOr Gerlitz 
70034832e1cSJakub Kicinski static inline void
701f9e30088SPablo Neira Ayuso tc_cls_common_offload_init(struct flow_cls_common_offload *cls_common,
70234832e1cSJakub Kicinski 			   const struct tcf_proto *tp, u32 flags,
70334832e1cSJakub Kicinski 			   struct netlink_ext_ack *extack)
70434832e1cSJakub Kicinski {
70534832e1cSJakub Kicinski 	cls_common->chain_index = tp->chain->index;
70634832e1cSJakub Kicinski 	cls_common->protocol = tp->protocol;
707ef01adaeSPablo Neira Ayuso 	cls_common->prio = tp->prio >> 16;
70881c7288bSMarcelo Ricardo Leitner 	if (tc_skip_sw(flags) || flags & TCA_CLS_FLAGS_VERBOSE)
70934832e1cSJakub Kicinski 		cls_common->extack = extack;
71034832e1cSJakub Kicinski }
71134832e1cSJakub Kicinski 
712b87f7936SYotam Gigi enum tc_matchall_command {
713b87f7936SYotam Gigi 	TC_CLSMATCHALL_REPLACE,
714b87f7936SYotam Gigi 	TC_CLSMATCHALL_DESTROY,
715b7fe4ab8SPieter Jansen van Vuuren 	TC_CLSMATCHALL_STATS,
716b87f7936SYotam Gigi };
717b87f7936SYotam Gigi 
718b87f7936SYotam Gigi struct tc_cls_matchall_offload {
719f9e30088SPablo Neira Ayuso 	struct flow_cls_common_offload common;
720b87f7936SYotam Gigi 	enum tc_matchall_command command;
721f00cbf19SPieter Jansen van Vuuren 	struct flow_rule *rule;
722b7fe4ab8SPieter Jansen van Vuuren 	struct flow_stats stats;
723b87f7936SYotam Gigi 	unsigned long cookie;
724b87f7936SYotam Gigi };
725b87f7936SYotam Gigi 
726332ae8e2SJakub Kicinski enum tc_clsbpf_command {
727102740bdSJakub Kicinski 	TC_CLSBPF_OFFLOAD,
72868d64063SJakub Kicinski 	TC_CLSBPF_STATS,
729332ae8e2SJakub Kicinski };
730332ae8e2SJakub Kicinski 
731332ae8e2SJakub Kicinski struct tc_cls_bpf_offload {
732f9e30088SPablo Neira Ayuso 	struct flow_cls_common_offload common;
733332ae8e2SJakub Kicinski 	enum tc_clsbpf_command command;
734332ae8e2SJakub Kicinski 	struct tcf_exts *exts;
735332ae8e2SJakub Kicinski 	struct bpf_prog *prog;
736102740bdSJakub Kicinski 	struct bpf_prog *oldprog;
737332ae8e2SJakub Kicinski 	const char *name;
738332ae8e2SJakub Kicinski 	bool exts_integrated;
739332ae8e2SJakub Kicinski };
740332ae8e2SJakub Kicinski 
7414e8b86c0SAmritha Nambiar struct tc_mqprio_qopt_offload {
7424e8b86c0SAmritha Nambiar 	/* struct tc_mqprio_qopt must always be the first element */
7434e8b86c0SAmritha Nambiar 	struct tc_mqprio_qopt qopt;
7444e8b86c0SAmritha Nambiar 	u16 mode;
7454e8b86c0SAmritha Nambiar 	u16 shaper;
7464e8b86c0SAmritha Nambiar 	u32 flags;
7474e8b86c0SAmritha Nambiar 	u64 min_rate[TC_QOPT_MAX_QUEUE];
7484e8b86c0SAmritha Nambiar 	u64 max_rate[TC_QOPT_MAX_QUEUE];
7494e8b86c0SAmritha Nambiar };
7501045ba77SJamal Hadi Salim 
7511045ba77SJamal Hadi Salim /* This structure holds cookie structure that is passed from user
7521045ba77SJamal Hadi Salim  * to the kernel for actions and classifiers
7531045ba77SJamal Hadi Salim  */
7541045ba77SJamal Hadi Salim struct tc_cookie {
7551045ba77SJamal Hadi Salim 	u8  *data;
7561045ba77SJamal Hadi Salim 	u32 len;
757eec94fdbSVlad Buslov 	struct rcu_head rcu;
7581045ba77SJamal Hadi Salim };
759602f3bafSNogah Frankel 
760f34b4aacSNogah Frankel struct tc_qopt_offload_stats {
761f34b4aacSNogah Frankel 	struct gnet_stats_basic_packed *bstats;
762f34b4aacSNogah Frankel 	struct gnet_stats_queue *qstats;
763f34b4aacSNogah Frankel };
764f34b4aacSNogah Frankel 
765f971b132SJakub Kicinski enum tc_mq_command {
766f971b132SJakub Kicinski 	TC_MQ_CREATE,
767f971b132SJakub Kicinski 	TC_MQ_DESTROY,
76847c669a4SJakub Kicinski 	TC_MQ_STATS,
769d577a3d2SJakub Kicinski 	TC_MQ_GRAFT,
770d577a3d2SJakub Kicinski };
771d577a3d2SJakub Kicinski 
772d577a3d2SJakub Kicinski struct tc_mq_opt_offload_graft_params {
773d577a3d2SJakub Kicinski 	unsigned long queue;
774d577a3d2SJakub Kicinski 	u32 child_handle;
775f971b132SJakub Kicinski };
776f971b132SJakub Kicinski 
777f971b132SJakub Kicinski struct tc_mq_qopt_offload {
778f971b132SJakub Kicinski 	enum tc_mq_command command;
779f971b132SJakub Kicinski 	u32 handle;
780d577a3d2SJakub Kicinski 	union {
78147c669a4SJakub Kicinski 		struct tc_qopt_offload_stats stats;
782d577a3d2SJakub Kicinski 		struct tc_mq_opt_offload_graft_params graft_params;
783d577a3d2SJakub Kicinski 	};
784f971b132SJakub Kicinski };
785f971b132SJakub Kicinski 
786*d03b195bSMaxim Mikityanskiy enum tc_htb_command {
787*d03b195bSMaxim Mikityanskiy 	/* Root */
788*d03b195bSMaxim Mikityanskiy 	TC_HTB_CREATE, /* Initialize HTB offload. */
789*d03b195bSMaxim Mikityanskiy 	TC_HTB_DESTROY, /* Destroy HTB offload. */
790*d03b195bSMaxim Mikityanskiy 
791*d03b195bSMaxim Mikityanskiy 	/* Classes */
792*d03b195bSMaxim Mikityanskiy 	/* Allocate qid and create leaf. */
793*d03b195bSMaxim Mikityanskiy 	TC_HTB_LEAF_ALLOC_QUEUE,
794*d03b195bSMaxim Mikityanskiy 	/* Convert leaf to inner, preserve and return qid, create new leaf. */
795*d03b195bSMaxim Mikityanskiy 	TC_HTB_LEAF_TO_INNER,
796*d03b195bSMaxim Mikityanskiy 	/* Delete leaf, while siblings remain. */
797*d03b195bSMaxim Mikityanskiy 	TC_HTB_LEAF_DEL,
798*d03b195bSMaxim Mikityanskiy 	/* Delete leaf, convert parent to leaf, preserving qid. */
799*d03b195bSMaxim Mikityanskiy 	TC_HTB_LEAF_DEL_LAST,
800*d03b195bSMaxim Mikityanskiy 	/* TC_HTB_LEAF_DEL_LAST, but delete driver data on hardware errors. */
801*d03b195bSMaxim Mikityanskiy 	TC_HTB_LEAF_DEL_LAST_FORCE,
802*d03b195bSMaxim Mikityanskiy 	/* Modify parameters of a node. */
803*d03b195bSMaxim Mikityanskiy 	TC_HTB_NODE_MODIFY,
804*d03b195bSMaxim Mikityanskiy 
805*d03b195bSMaxim Mikityanskiy 	/* Class qdisc */
806*d03b195bSMaxim Mikityanskiy 	TC_HTB_LEAF_QUERY_QUEUE, /* Query qid by classid. */
807*d03b195bSMaxim Mikityanskiy };
808*d03b195bSMaxim Mikityanskiy 
809*d03b195bSMaxim Mikityanskiy struct tc_htb_qopt_offload {
810*d03b195bSMaxim Mikityanskiy 	struct netlink_ext_ack *extack;
811*d03b195bSMaxim Mikityanskiy 	enum tc_htb_command command;
812*d03b195bSMaxim Mikityanskiy 	u16 classid;
813*d03b195bSMaxim Mikityanskiy 	u32 parent_classid;
814*d03b195bSMaxim Mikityanskiy 	u16 qid;
815*d03b195bSMaxim Mikityanskiy 	u16 moved_qid;
816*d03b195bSMaxim Mikityanskiy 	u64 rate;
817*d03b195bSMaxim Mikityanskiy 	u64 ceil;
818*d03b195bSMaxim Mikityanskiy };
819*d03b195bSMaxim Mikityanskiy 
820*d03b195bSMaxim Mikityanskiy #define TC_HTB_CLASSID_ROOT U32_MAX
821*d03b195bSMaxim Mikityanskiy 
822602f3bafSNogah Frankel enum tc_red_command {
823602f3bafSNogah Frankel 	TC_RED_REPLACE,
824602f3bafSNogah Frankel 	TC_RED_DESTROY,
825602f3bafSNogah Frankel 	TC_RED_STATS,
826602f3bafSNogah Frankel 	TC_RED_XSTATS,
827bf2a752bSJakub Kicinski 	TC_RED_GRAFT,
828602f3bafSNogah Frankel };
829602f3bafSNogah Frankel 
830602f3bafSNogah Frankel struct tc_red_qopt_offload_params {
831602f3bafSNogah Frankel 	u32 min;
832602f3bafSNogah Frankel 	u32 max;
833602f3bafSNogah Frankel 	u32 probability;
834c0b7490bSJakub Kicinski 	u32 limit;
835602f3bafSNogah Frankel 	bool is_ecn;
836190852a5SJakub Kicinski 	bool is_harddrop;
8370a7fad23SPetr Machata 	bool is_nodrop;
838416ef9b1SJakub Kicinski 	struct gnet_stats_queue *qstats;
839602f3bafSNogah Frankel };
840602f3bafSNogah Frankel 
841602f3bafSNogah Frankel struct tc_red_qopt_offload {
842602f3bafSNogah Frankel 	enum tc_red_command command;
843602f3bafSNogah Frankel 	u32 handle;
844602f3bafSNogah Frankel 	u32 parent;
845602f3bafSNogah Frankel 	union {
846602f3bafSNogah Frankel 		struct tc_red_qopt_offload_params set;
847f34b4aacSNogah Frankel 		struct tc_qopt_offload_stats stats;
848602f3bafSNogah Frankel 		struct red_stats *xstats;
849bf2a752bSJakub Kicinski 		u32 child_handle;
850602f3bafSNogah Frankel 	};
851602f3bafSNogah Frankel };
852602f3bafSNogah Frankel 
853890d8d23SJakub Kicinski enum tc_gred_command {
854890d8d23SJakub Kicinski 	TC_GRED_REPLACE,
855890d8d23SJakub Kicinski 	TC_GRED_DESTROY,
856e49efd52SJakub Kicinski 	TC_GRED_STATS,
857890d8d23SJakub Kicinski };
858890d8d23SJakub Kicinski 
859890d8d23SJakub Kicinski struct tc_gred_vq_qopt_offload_params {
860890d8d23SJakub Kicinski 	bool present;
861890d8d23SJakub Kicinski 	u32 limit;
862890d8d23SJakub Kicinski 	u32 prio;
863890d8d23SJakub Kicinski 	u32 min;
864890d8d23SJakub Kicinski 	u32 max;
865890d8d23SJakub Kicinski 	bool is_ecn;
866890d8d23SJakub Kicinski 	bool is_harddrop;
867890d8d23SJakub Kicinski 	u32 probability;
868890d8d23SJakub Kicinski 	/* Only need backlog, see struct tc_prio_qopt_offload_params */
869890d8d23SJakub Kicinski 	u32 *backlog;
870890d8d23SJakub Kicinski };
871890d8d23SJakub Kicinski 
872890d8d23SJakub Kicinski struct tc_gred_qopt_offload_params {
873890d8d23SJakub Kicinski 	bool grio_on;
874890d8d23SJakub Kicinski 	bool wred_on;
875890d8d23SJakub Kicinski 	unsigned int dp_cnt;
876890d8d23SJakub Kicinski 	unsigned int dp_def;
877890d8d23SJakub Kicinski 	struct gnet_stats_queue *qstats;
878890d8d23SJakub Kicinski 	struct tc_gred_vq_qopt_offload_params tab[MAX_DPs];
879890d8d23SJakub Kicinski };
880890d8d23SJakub Kicinski 
881e49efd52SJakub Kicinski struct tc_gred_qopt_offload_stats {
882e49efd52SJakub Kicinski 	struct gnet_stats_basic_packed bstats[MAX_DPs];
883e49efd52SJakub Kicinski 	struct gnet_stats_queue qstats[MAX_DPs];
884e49efd52SJakub Kicinski 	struct red_stats *xstats[MAX_DPs];
885e49efd52SJakub Kicinski };
886e49efd52SJakub Kicinski 
887890d8d23SJakub Kicinski struct tc_gred_qopt_offload {
888890d8d23SJakub Kicinski 	enum tc_gred_command command;
889890d8d23SJakub Kicinski 	u32 handle;
890890d8d23SJakub Kicinski 	u32 parent;
891890d8d23SJakub Kicinski 	union {
892890d8d23SJakub Kicinski 		struct tc_gred_qopt_offload_params set;
893e49efd52SJakub Kicinski 		struct tc_gred_qopt_offload_stats stats;
894890d8d23SJakub Kicinski 	};
895890d8d23SJakub Kicinski };
896890d8d23SJakub Kicinski 
8977fdb61b4SNogah Frankel enum tc_prio_command {
8987fdb61b4SNogah Frankel 	TC_PRIO_REPLACE,
8997fdb61b4SNogah Frankel 	TC_PRIO_DESTROY,
9007fdb61b4SNogah Frankel 	TC_PRIO_STATS,
901b9c7a7acSNogah Frankel 	TC_PRIO_GRAFT,
9027fdb61b4SNogah Frankel };
9037fdb61b4SNogah Frankel 
9047fdb61b4SNogah Frankel struct tc_prio_qopt_offload_params {
9057fdb61b4SNogah Frankel 	int bands;
9067fdb61b4SNogah Frankel 	u8 priomap[TC_PRIO_MAX + 1];
9079586a992SPetr Machata 	/* At the point of un-offloading the Qdisc, the reported backlog and
9089586a992SPetr Machata 	 * qlen need to be reduced by the portion that is in HW.
9097fdb61b4SNogah Frankel 	 */
9107fdb61b4SNogah Frankel 	struct gnet_stats_queue *qstats;
9117fdb61b4SNogah Frankel };
9127fdb61b4SNogah Frankel 
913b9c7a7acSNogah Frankel struct tc_prio_qopt_offload_graft_params {
914b9c7a7acSNogah Frankel 	u8 band;
915b9c7a7acSNogah Frankel 	u32 child_handle;
916b9c7a7acSNogah Frankel };
917b9c7a7acSNogah Frankel 
9187fdb61b4SNogah Frankel struct tc_prio_qopt_offload {
9197fdb61b4SNogah Frankel 	enum tc_prio_command command;
9207fdb61b4SNogah Frankel 	u32 handle;
9217fdb61b4SNogah Frankel 	u32 parent;
9227fdb61b4SNogah Frankel 	union {
9237fdb61b4SNogah Frankel 		struct tc_prio_qopt_offload_params replace_params;
9247fdb61b4SNogah Frankel 		struct tc_qopt_offload_stats stats;
925b9c7a7acSNogah Frankel 		struct tc_prio_qopt_offload_graft_params graft_params;
9267fdb61b4SNogah Frankel 	};
9277fdb61b4SNogah Frankel };
928b9c7a7acSNogah Frankel 
92998b0e5f6SJakub Kicinski enum tc_root_command {
93098b0e5f6SJakub Kicinski 	TC_ROOT_GRAFT,
93198b0e5f6SJakub Kicinski };
93298b0e5f6SJakub Kicinski 
93398b0e5f6SJakub Kicinski struct tc_root_qopt_offload {
93498b0e5f6SJakub Kicinski 	enum tc_root_command command;
93598b0e5f6SJakub Kicinski 	u32 handle;
93698b0e5f6SJakub Kicinski 	bool ingress;
93798b0e5f6SJakub Kicinski };
93898b0e5f6SJakub Kicinski 
939d35eb52bSPetr Machata enum tc_ets_command {
940d35eb52bSPetr Machata 	TC_ETS_REPLACE,
941d35eb52bSPetr Machata 	TC_ETS_DESTROY,
942d35eb52bSPetr Machata 	TC_ETS_STATS,
943d35eb52bSPetr Machata 	TC_ETS_GRAFT,
944d35eb52bSPetr Machata };
945d35eb52bSPetr Machata 
946d35eb52bSPetr Machata struct tc_ets_qopt_offload_replace_params {
947d35eb52bSPetr Machata 	unsigned int bands;
948d35eb52bSPetr Machata 	u8 priomap[TC_PRIO_MAX + 1];
949d35eb52bSPetr Machata 	unsigned int quanta[TCQ_ETS_MAX_BANDS];	/* 0 for strict bands. */
950d35eb52bSPetr Machata 	unsigned int weights[TCQ_ETS_MAX_BANDS];
951d35eb52bSPetr Machata 	struct gnet_stats_queue *qstats;
952d35eb52bSPetr Machata };
953d35eb52bSPetr Machata 
954d35eb52bSPetr Machata struct tc_ets_qopt_offload_graft_params {
955d35eb52bSPetr Machata 	u8 band;
956d35eb52bSPetr Machata 	u32 child_handle;
957d35eb52bSPetr Machata };
958d35eb52bSPetr Machata 
959d35eb52bSPetr Machata struct tc_ets_qopt_offload {
960d35eb52bSPetr Machata 	enum tc_ets_command command;
961d35eb52bSPetr Machata 	u32 handle;
962d35eb52bSPetr Machata 	u32 parent;
963d35eb52bSPetr Machata 	union {
964d35eb52bSPetr Machata 		struct tc_ets_qopt_offload_replace_params replace_params;
965d35eb52bSPetr Machata 		struct tc_qopt_offload_stats stats;
966d35eb52bSPetr Machata 		struct tc_ets_qopt_offload_graft_params graft_params;
967d35eb52bSPetr Machata 	};
968d35eb52bSPetr Machata };
969d35eb52bSPetr Machata 
970ef6aadccSPetr Machata enum tc_tbf_command {
971ef6aadccSPetr Machata 	TC_TBF_REPLACE,
972ef6aadccSPetr Machata 	TC_TBF_DESTROY,
973ef6aadccSPetr Machata 	TC_TBF_STATS,
974ef6aadccSPetr Machata };
975ef6aadccSPetr Machata 
976ef6aadccSPetr Machata struct tc_tbf_qopt_offload_replace_params {
977ef6aadccSPetr Machata 	struct psched_ratecfg rate;
978ef6aadccSPetr Machata 	u32 max_size;
979ef6aadccSPetr Machata 	struct gnet_stats_queue *qstats;
980ef6aadccSPetr Machata };
981ef6aadccSPetr Machata 
982ef6aadccSPetr Machata struct tc_tbf_qopt_offload {
983ef6aadccSPetr Machata 	enum tc_tbf_command command;
984ef6aadccSPetr Machata 	u32 handle;
985ef6aadccSPetr Machata 	u32 parent;
986ef6aadccSPetr Machata 	union {
987ef6aadccSPetr Machata 		struct tc_tbf_qopt_offload_replace_params replace_params;
988ef6aadccSPetr Machata 		struct tc_qopt_offload_stats stats;
989ef6aadccSPetr Machata 	};
990ef6aadccSPetr Machata };
991ef6aadccSPetr Machata 
992aaca9408SPetr Machata enum tc_fifo_command {
993aaca9408SPetr Machata 	TC_FIFO_REPLACE,
994aaca9408SPetr Machata 	TC_FIFO_DESTROY,
995aaca9408SPetr Machata 	TC_FIFO_STATS,
996aaca9408SPetr Machata };
997aaca9408SPetr Machata 
998aaca9408SPetr Machata struct tc_fifo_qopt_offload {
999aaca9408SPetr Machata 	enum tc_fifo_command command;
1000aaca9408SPetr Machata 	u32 handle;
1001aaca9408SPetr Machata 	u32 parent;
1002aaca9408SPetr Machata 	union {
1003aaca9408SPetr Machata 		struct tc_qopt_offload_stats stats;
1004aaca9408SPetr Machata 	};
1005aaca9408SPetr Machata };
1006aaca9408SPetr Machata 
10071da177e4SLinus Torvalds #endif
1008