xref: /openbmc/linux/include/net/pkt_cls.h (revision 5246c896)
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);
26bc5c8260SZhengchao Shao void 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 
793aa26055SDavide Caratti int tcf_classify(struct sk_buff *skb,
803aa26055SDavide Caratti 		 const struct tcf_block *block,
817d17c544SPaul Blakey 		 const struct tcf_proto *tp, struct tcf_result *res,
827d17c544SPaul Blakey 		 bool compat_mode);
8387d83093SJiri Pirko 
84fe0df81dSZhengchao Shao static inline bool tc_cls_stats_dump(struct tcf_proto *tp,
85fe0df81dSZhengchao Shao 				     struct tcf_walker *arg,
86fe0df81dSZhengchao Shao 				     void *filter)
87fe0df81dSZhengchao Shao {
88fe0df81dSZhengchao Shao 	if (arg->count >= arg->skip && arg->fn(tp, filter, arg) < 0) {
89fe0df81dSZhengchao Shao 		arg->stop = 1;
90fe0df81dSZhengchao Shao 		return false;
91fe0df81dSZhengchao Shao 	}
92fe0df81dSZhengchao Shao 
93fe0df81dSZhengchao Shao 	arg->count++;
94fe0df81dSZhengchao Shao 	return true;
95fe0df81dSZhengchao Shao }
96fe0df81dSZhengchao Shao 
978ae70032SJiri Pirko #else
9888c44a52SPieter Jansen van Vuuren static inline bool tcf_block_shared(struct tcf_block *block)
9988c44a52SPieter Jansen van Vuuren {
10088c44a52SPieter Jansen van Vuuren 	return false;
10188c44a52SPieter Jansen van Vuuren }
10288c44a52SPieter Jansen van Vuuren 
103c1a970d0SVlad Buslov static inline bool tcf_block_non_null_shared(struct tcf_block *block)
104c1a970d0SVlad Buslov {
105c1a970d0SVlad Buslov 	return false;
106c1a970d0SVlad Buslov }
107c1a970d0SVlad Buslov 
1086529eabaSJiri Pirko static inline
1096529eabaSJiri Pirko int tcf_block_get(struct tcf_block **p_block,
1103c149091SSudip Mukherjee 		  struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q,
1113c149091SSudip Mukherjee 		  struct netlink_ext_ack *extack)
1126529eabaSJiri Pirko {
1136529eabaSJiri Pirko 	return 0;
1146529eabaSJiri Pirko }
1156529eabaSJiri Pirko 
1168c4083b3SJiri Pirko static inline
117c7eb7d72SJiri Pirko int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q,
11833c30a8bSQuentin Monnet 		      struct tcf_block_ext_info *ei,
11933c30a8bSQuentin Monnet 		      struct netlink_ext_ack *extack)
1208c4083b3SJiri Pirko {
1218c4083b3SJiri Pirko 	return 0;
1228c4083b3SJiri Pirko }
1238c4083b3SJiri Pirko 
1246529eabaSJiri Pirko static inline void tcf_block_put(struct tcf_block *block)
1258ae70032SJiri Pirko {
1268ae70032SJiri Pirko }
12787d83093SJiri Pirko 
1288c4083b3SJiri Pirko static inline
129c7eb7d72SJiri Pirko void tcf_block_put_ext(struct tcf_block *block, struct Qdisc *q,
1308c4083b3SJiri Pirko 		       struct tcf_block_ext_info *ei)
1318c4083b3SJiri Pirko {
1328c4083b3SJiri Pirko }
1338c4083b3SJiri Pirko 
13444186460SJiri Pirko static inline struct Qdisc *tcf_block_q(struct tcf_block *block)
13544186460SJiri Pirko {
13644186460SJiri Pirko 	return NULL;
13744186460SJiri Pirko }
13844186460SJiri Pirko 
139acb67442SJiri Pirko static inline
140a7323311SPablo Neira Ayuso int tc_setup_cb_block_register(struct tcf_block *block, flow_setup_cb_t *cb,
141acb67442SJiri Pirko 			       void *cb_priv)
142acb67442SJiri Pirko {
143acb67442SJiri Pirko 	return 0;
144acb67442SJiri Pirko }
145acb67442SJiri Pirko 
146acb67442SJiri Pirko static inline
147a7323311SPablo Neira Ayuso void tc_setup_cb_block_unregister(struct tcf_block *block, flow_setup_cb_t *cb,
148acb67442SJiri Pirko 				  void *cb_priv)
149acb67442SJiri Pirko {
150acb67442SJiri Pirko }
151acb67442SJiri Pirko 
1523aa26055SDavide Caratti static inline int tcf_classify(struct sk_buff *skb,
1533aa26055SDavide Caratti 			       const struct tcf_block *block,
1549410c940SPaul Blakey 			       const struct tcf_proto *tp,
1559410c940SPaul Blakey 			       struct tcf_result *res, bool compat_mode)
1569410c940SPaul Blakey {
1579410c940SPaul Blakey 	return TC_ACT_UNSPEC;
1589410c940SPaul Blakey }
1599410c940SPaul Blakey 
1608ae70032SJiri Pirko #endif
161cf1facdaSJiri Pirko 
1621da177e4SLinus Torvalds static inline unsigned long
1631da177e4SLinus Torvalds __cls_set_class(unsigned long *clp, unsigned long cl)
1641da177e4SLinus Torvalds {
165a0efb80cSWANG Cong 	return xchg(clp, cl);
1661da177e4SLinus Torvalds }
1671da177e4SLinus Torvalds 
1682e24cd75SCong Wang static inline void
1692e24cd75SCong Wang __tcf_bind_filter(struct Qdisc *q, struct tcf_result *r, unsigned long base)
1701da177e4SLinus Torvalds {
1712e24cd75SCong Wang 	unsigned long cl;
1721da177e4SLinus Torvalds 
1732e24cd75SCong Wang 	cl = q->ops->cl_ops->bind_tcf(q, base, r->classid);
1742e24cd75SCong Wang 	cl = __cls_set_class(&r->class, cl);
1752e24cd75SCong Wang 	if (cl)
1762e24cd75SCong Wang 		q->ops->cl_ops->unbind_tcf(q, cl);
1771da177e4SLinus Torvalds }
1781da177e4SLinus Torvalds 
1791da177e4SLinus Torvalds static inline void
1801da177e4SLinus Torvalds tcf_bind_filter(struct tcf_proto *tp, struct tcf_result *r, unsigned long base)
1811da177e4SLinus Torvalds {
18234e3759cSJiri Pirko 	struct Qdisc *q = tp->chain->block->q;
1831da177e4SLinus Torvalds 
18434e3759cSJiri Pirko 	/* Check q as it is not set for shared blocks. In that case,
18534e3759cSJiri Pirko 	 * setting class is not supported.
18634e3759cSJiri Pirko 	 */
18734e3759cSJiri Pirko 	if (!q)
18834e3759cSJiri Pirko 		return;
1892e24cd75SCong Wang 	sch_tree_lock(q);
1902e24cd75SCong Wang 	__tcf_bind_filter(q, r, base);
1912e24cd75SCong Wang 	sch_tree_unlock(q);
1922e24cd75SCong Wang }
1932e24cd75SCong Wang 
1942e24cd75SCong Wang static inline void
1952e24cd75SCong Wang __tcf_unbind_filter(struct Qdisc *q, struct tcf_result *r)
1962e24cd75SCong Wang {
1972e24cd75SCong Wang 	unsigned long cl;
1982e24cd75SCong Wang 
1992e24cd75SCong Wang 	if ((cl = __cls_set_class(&r->class, 0)) != 0)
20034e3759cSJiri Pirko 		q->ops->cl_ops->unbind_tcf(q, cl);
2011da177e4SLinus Torvalds }
2021da177e4SLinus Torvalds 
2031da177e4SLinus Torvalds static inline void
2041da177e4SLinus Torvalds tcf_unbind_filter(struct tcf_proto *tp, struct tcf_result *r)
2051da177e4SLinus Torvalds {
20634e3759cSJiri Pirko 	struct Qdisc *q = tp->chain->block->q;
2071da177e4SLinus Torvalds 
20834e3759cSJiri Pirko 	if (!q)
20934e3759cSJiri Pirko 		return;
2102e24cd75SCong Wang 	__tcf_unbind_filter(q, r);
2111da177e4SLinus Torvalds }
2121da177e4SLinus Torvalds 
213402963e3SZhengchao Shao static inline void tc_cls_bind_class(u32 classid, unsigned long cl,
214402963e3SZhengchao Shao 				     void *q, struct tcf_result *res,
215402963e3SZhengchao Shao 				     unsigned long base)
216402963e3SZhengchao Shao {
217402963e3SZhengchao Shao 	if (res->classid == classid) {
218402963e3SZhengchao Shao 		if (cl)
219402963e3SZhengchao Shao 			__tcf_bind_filter(q, res, base);
220402963e3SZhengchao Shao 		else
221402963e3SZhengchao Shao 			__tcf_unbind_filter(q, res);
222402963e3SZhengchao Shao 	}
223402963e3SZhengchao Shao }
224402963e3SZhengchao Shao 
225fd2c3ef7SEric Dumazet struct tcf_exts {
2261da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ACT
22733be6271SWANG Cong 	__u32	type; /* for backward compat(TCA_OLD_COMPAT) */
22822dc13c8SWANG Cong 	int nr_actions;
22922dc13c8SWANG Cong 	struct tc_action **actions;
230e4b95c41SCong Wang 	struct net	*net;
231dbdcda63SEric Dumazet 	netns_tracker	ns_tracker;
2321da177e4SLinus Torvalds #endif
2331da177e4SLinus Torvalds 	/* Map to export classifier specific extension TLV types to the
2341da177e4SLinus Torvalds 	 * generic extensions API. Unsupported extensions must be set to 0.
2351da177e4SLinus Torvalds 	 */
2361da177e4SLinus Torvalds 	int action;
2371da177e4SLinus Torvalds 	int police;
2381da177e4SLinus Torvalds };
2391da177e4SLinus Torvalds 
24014215108SCong Wang static inline int tcf_exts_init(struct tcf_exts *exts, struct net *net,
24114215108SCong Wang 				int action, int police)
24233be6271SWANG Cong {
24333be6271SWANG Cong #ifdef CONFIG_NET_CLS_ACT
2445da57f42SWANG Cong 	exts->type = 0;
24522dc13c8SWANG Cong 	exts->nr_actions = 0;
246cb963a19SEric Dumazet 	/* Note: we do not own yet a reference on net.
247cb963a19SEric Dumazet 	 * This reference might be taken later from tcf_exts_get_net().
248cb963a19SEric Dumazet 	 */
24914215108SCong Wang 	exts->net = net;
25022dc13c8SWANG Cong 	exts->actions = kcalloc(TCA_ACT_MAX_PRIO, sizeof(struct tc_action *),
25122dc13c8SWANG Cong 				GFP_KERNEL);
252b9a24bb7SWANG Cong 	if (!exts->actions)
253b9a24bb7SWANG Cong 		return -ENOMEM;
25433be6271SWANG Cong #endif
2555da57f42SWANG Cong 	exts->action = action;
2565da57f42SWANG Cong 	exts->police = police;
257b9a24bb7SWANG Cong 	return 0;
25833be6271SWANG Cong }
25933be6271SWANG Cong 
260e4b95c41SCong Wang /* Return false if the netns is being destroyed in cleanup_net(). Callers
261e4b95c41SCong Wang  * need to do cleanup synchronously in this case, otherwise may race with
262e4b95c41SCong Wang  * tc_action_net_exit(). Return true for other cases.
263e4b95c41SCong Wang  */
264e4b95c41SCong Wang static inline bool tcf_exts_get_net(struct tcf_exts *exts)
265e4b95c41SCong Wang {
266e4b95c41SCong Wang #ifdef CONFIG_NET_CLS_ACT
267e4b95c41SCong Wang 	exts->net = maybe_get_net(exts->net);
268dbdcda63SEric Dumazet 	if (exts->net)
269dbdcda63SEric Dumazet 		netns_tracker_alloc(exts->net, &exts->ns_tracker, GFP_KERNEL);
270e4b95c41SCong Wang 	return exts->net != NULL;
271e4b95c41SCong Wang #else
272e4b95c41SCong Wang 	return true;
273e4b95c41SCong Wang #endif
274e4b95c41SCong Wang }
275e4b95c41SCong Wang 
276e4b95c41SCong Wang static inline void tcf_exts_put_net(struct tcf_exts *exts)
277e4b95c41SCong Wang {
278e4b95c41SCong Wang #ifdef CONFIG_NET_CLS_ACT
279e4b95c41SCong Wang 	if (exts->net)
280dbdcda63SEric Dumazet 		put_net_track(exts->net, &exts->ns_tracker);
281e4b95c41SCong Wang #endif
282e4b95c41SCong Wang }
283e4b95c41SCong Wang 
28422dc13c8SWANG Cong #ifdef CONFIG_NET_CLS_ACT
285244cd96aSCong Wang #define tcf_exts_for_each_action(i, a, exts) \
286244cd96aSCong Wang 	for (i = 0; i < TCA_ACT_MAX_PRIO && ((a) = (exts)->actions[i]); i++)
287244cd96aSCong Wang #else
288244cd96aSCong Wang #define tcf_exts_for_each_action(i, a, exts) \
289191672caSArnd Bergmann 	for (; 0; (void)(i), (void)(a), (void)(exts))
29022dc13c8SWANG Cong #endif
29122dc13c8SWANG Cong 
2928cbfe939SBaowen Zheng #define tcf_act_for_each_action(i, a, actions) \
2938cbfe939SBaowen Zheng 	for (i = 0; i < TCA_ACT_MAX_PRIO && ((a) = actions[i]); i++)
2948cbfe939SBaowen Zheng 
295*5246c896SOz Shlomo static inline bool tc_act_in_hw(struct tc_action *act)
296*5246c896SOz Shlomo {
297*5246c896SOz Shlomo 	return !!act->in_hw_count;
298*5246c896SOz Shlomo }
299*5246c896SOz Shlomo 
300d897a638SJakub Kicinski static inline void
301bcd64368SBaowen Zheng tcf_exts_hw_stats_update(const struct tcf_exts *exts,
302*5246c896SOz Shlomo 			 struct flow_stats *stats,
303*5246c896SOz Shlomo 			 bool use_act_stats)
304d897a638SJakub Kicinski {
305d897a638SJakub Kicinski #ifdef CONFIG_NET_CLS_ACT
306d897a638SJakub Kicinski 	int i;
307d897a638SJakub Kicinski 
308d897a638SJakub Kicinski 	for (i = 0; i < exts->nr_actions; i++) {
309d897a638SJakub Kicinski 		struct tc_action *a = exts->actions[i];
310d897a638SJakub Kicinski 
311*5246c896SOz Shlomo 		if (use_act_stats || tc_act_in_hw(a)) {
312*5246c896SOz Shlomo 			if (!tcf_action_update_hw_stats(a))
313*5246c896SOz Shlomo 				continue;
314*5246c896SOz Shlomo 		}
315*5246c896SOz Shlomo 
316c7a66f8dSBaowen Zheng 		preempt_disable();
317ac7d2790SOz Shlomo 		tcf_action_stats_update(a, stats->bytes, stats->pkts, stats->drops,
318ac7d2790SOz Shlomo 					stats->lastused, true);
319c7a66f8dSBaowen Zheng 		preempt_enable();
320c7a66f8dSBaowen Zheng 
321ac7d2790SOz Shlomo 		a->used_hw_stats = stats->used_hw_stats;
322ac7d2790SOz Shlomo 		a->used_hw_stats_valid = stats->used_hw_stats_valid;
323d897a638SJakub Kicinski 	}
324d897a638SJakub Kicinski #endif
325d897a638SJakub Kicinski }
326d897a638SJakub Kicinski 
3271da177e4SLinus Torvalds /**
3283bcc0cecSJiri Pirko  * tcf_exts_has_actions - check if at least one action is present
3293bcc0cecSJiri Pirko  * @exts: tc filter extensions handle
3303bcc0cecSJiri Pirko  *
3313bcc0cecSJiri Pirko  * Returns true if at least one action is present.
3323bcc0cecSJiri Pirko  */
3333bcc0cecSJiri Pirko static inline bool tcf_exts_has_actions(struct tcf_exts *exts)
3343bcc0cecSJiri Pirko {
3352734437eSWANG Cong #ifdef CONFIG_NET_CLS_ACT
3363bcc0cecSJiri Pirko 	return exts->nr_actions;
3373bcc0cecSJiri Pirko #else
3383bcc0cecSJiri Pirko 	return false;
3393bcc0cecSJiri Pirko #endif
3403bcc0cecSJiri Pirko }
3412734437eSWANG Cong 
3423bcc0cecSJiri Pirko /**
343af69afc5SJiri Pirko  * tcf_exts_exec - execute tc filter extensions
344af69afc5SJiri Pirko  * @skb: socket buffer
345af69afc5SJiri Pirko  * @exts: tc filter extensions handle
346af69afc5SJiri Pirko  * @res: desired result
347af69afc5SJiri Pirko  *
348af089e70SJiri Pirko  * Executes all configured extensions. Returns TC_ACT_OK on a normal execution,
349af69afc5SJiri Pirko  * a negative number if the filter must be considered unmatched or
350af69afc5SJiri Pirko  * a positive action code (TC_ACT_*) which must be returned to the
351af69afc5SJiri Pirko  * underlying layer.
352af69afc5SJiri Pirko  */
353af69afc5SJiri Pirko static inline int
354af69afc5SJiri Pirko tcf_exts_exec(struct sk_buff *skb, struct tcf_exts *exts,
355af69afc5SJiri Pirko 	      struct tcf_result *res)
356af69afc5SJiri Pirko {
357af69afc5SJiri Pirko #ifdef CONFIG_NET_CLS_ACT
358ec1a9ccaSJiri Pirko 	return tcf_action_exec(skb, exts->actions, exts->nr_actions, res);
359af69afc5SJiri Pirko #endif
360af089e70SJiri Pirko 	return TC_ACT_OK;
361af69afc5SJiri Pirko }
362af69afc5SJiri Pirko 
3635c15257fSJoe Perches int tcf_exts_validate(struct net *net, struct tcf_proto *tp,
364c1b52739SBenjamin LaHaise 		      struct nlattr **tb, struct nlattr *rate_tlv,
365695176bfSCong Wang 		      struct tcf_exts *exts, u32 flags,
36650a56190SAlexander Aring 		      struct netlink_ext_ack *extack);
367c86e0209SBaowen Zheng int tcf_exts_validate_ex(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
368c86e0209SBaowen Zheng 			 struct nlattr *rate_tlv, struct tcf_exts *exts,
369c86e0209SBaowen Zheng 			 u32 flags, u32 fl_flags, struct netlink_ext_ack *extack);
37018d0264fSWANG Cong void tcf_exts_destroy(struct tcf_exts *exts);
3719b0d4446SJiri Pirko void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src);
3725da57f42SWANG Cong int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts);
373ca44b738SVlad Buslov int tcf_exts_terse_dump(struct sk_buff *skb, struct tcf_exts *exts);
3745da57f42SWANG Cong int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts);
3751da177e4SLinus Torvalds 
3761da177e4SLinus Torvalds /**
3771da177e4SLinus Torvalds  * struct tcf_pkt_info - packet information
3780161d151SBijie Xu  *
3790161d151SBijie Xu  * @ptr: start of the pkt data
3800161d151SBijie Xu  * @nexthdr: offset of the next header
3811da177e4SLinus Torvalds  */
382fd2c3ef7SEric Dumazet struct tcf_pkt_info {
3831da177e4SLinus Torvalds 	unsigned char *		ptr;
3841da177e4SLinus Torvalds 	int			nexthdr;
3851da177e4SLinus Torvalds };
3861da177e4SLinus Torvalds 
3871da177e4SLinus Torvalds #ifdef CONFIG_NET_EMATCH
3881da177e4SLinus Torvalds 
3891da177e4SLinus Torvalds struct tcf_ematch_ops;
3901da177e4SLinus Torvalds 
3911da177e4SLinus Torvalds /**
3921da177e4SLinus Torvalds  * struct tcf_ematch - extended match (ematch)
3931da177e4SLinus Torvalds  *
3941da177e4SLinus Torvalds  * @matchid: identifier to allow userspace to reidentify a match
3951da177e4SLinus Torvalds  * @flags: flags specifying attributes and the relation to other matches
3961da177e4SLinus Torvalds  * @ops: the operations lookup table of the corresponding ematch module
3971da177e4SLinus Torvalds  * @datalen: length of the ematch specific configuration data
3981da177e4SLinus Torvalds  * @data: ematch specific data
3990161d151SBijie Xu  * @net: the network namespace
4001da177e4SLinus Torvalds  */
401fd2c3ef7SEric Dumazet struct tcf_ematch {
4021da177e4SLinus Torvalds 	struct tcf_ematch_ops * ops;
4031da177e4SLinus Torvalds 	unsigned long		data;
4041da177e4SLinus Torvalds 	unsigned int		datalen;
4051da177e4SLinus Torvalds 	u16			matchid;
4061da177e4SLinus Torvalds 	u16			flags;
40782a470f1SJohn Fastabend 	struct net		*net;
4081da177e4SLinus Torvalds };
4091da177e4SLinus Torvalds 
4101da177e4SLinus Torvalds static inline int tcf_em_is_container(struct tcf_ematch *em)
4111da177e4SLinus Torvalds {
4121da177e4SLinus Torvalds 	return !em->ops;
4131da177e4SLinus Torvalds }
4141da177e4SLinus Torvalds 
4151da177e4SLinus Torvalds static inline int tcf_em_is_simple(struct tcf_ematch *em)
4161da177e4SLinus Torvalds {
4171da177e4SLinus Torvalds 	return em->flags & TCF_EM_SIMPLE;
4181da177e4SLinus Torvalds }
4191da177e4SLinus Torvalds 
4201da177e4SLinus Torvalds static inline int tcf_em_is_inverted(struct tcf_ematch *em)
4211da177e4SLinus Torvalds {
4221da177e4SLinus Torvalds 	return em->flags & TCF_EM_INVERT;
4231da177e4SLinus Torvalds }
4241da177e4SLinus Torvalds 
4251da177e4SLinus Torvalds static inline int tcf_em_last_match(struct tcf_ematch *em)
4261da177e4SLinus Torvalds {
4271da177e4SLinus Torvalds 	return (em->flags & TCF_EM_REL_MASK) == TCF_EM_REL_END;
4281da177e4SLinus Torvalds }
4291da177e4SLinus Torvalds 
4301da177e4SLinus Torvalds static inline int tcf_em_early_end(struct tcf_ematch *em, int result)
4311da177e4SLinus Torvalds {
4321da177e4SLinus Torvalds 	if (tcf_em_last_match(em))
4331da177e4SLinus Torvalds 		return 1;
4341da177e4SLinus Torvalds 
4351da177e4SLinus Torvalds 	if (result == 0 && em->flags & TCF_EM_REL_AND)
4361da177e4SLinus Torvalds 		return 1;
4371da177e4SLinus Torvalds 
4381da177e4SLinus Torvalds 	if (result != 0 && em->flags & TCF_EM_REL_OR)
4391da177e4SLinus Torvalds 		return 1;
4401da177e4SLinus Torvalds 
4411da177e4SLinus Torvalds 	return 0;
4421da177e4SLinus Torvalds }
4431da177e4SLinus Torvalds 
4441da177e4SLinus Torvalds /**
4451da177e4SLinus Torvalds  * struct tcf_ematch_tree - ematch tree handle
4461da177e4SLinus Torvalds  *
4471da177e4SLinus Torvalds  * @hdr: ematch tree header supplied by userspace
4481da177e4SLinus Torvalds  * @matches: array of ematches
4491da177e4SLinus Torvalds  */
450fd2c3ef7SEric Dumazet struct tcf_ematch_tree {
4511da177e4SLinus Torvalds 	struct tcf_ematch_tree_hdr hdr;
4521da177e4SLinus Torvalds 	struct tcf_ematch *	matches;
4531da177e4SLinus Torvalds 
4541da177e4SLinus Torvalds };
4551da177e4SLinus Torvalds 
4561da177e4SLinus Torvalds /**
4571da177e4SLinus Torvalds  * struct tcf_ematch_ops - ematch module operations
4581da177e4SLinus Torvalds  *
4591da177e4SLinus Torvalds  * @kind: identifier (kind) of this ematch module
4601da177e4SLinus Torvalds  * @datalen: length of expected configuration data (optional)
4611da177e4SLinus Torvalds  * @change: called during validation (optional)
4621da177e4SLinus Torvalds  * @match: called during ematch tree evaluation, must return 1/0
4631da177e4SLinus Torvalds  * @destroy: called during destroyage (optional)
4641da177e4SLinus Torvalds  * @dump: called during dumping process (optional)
4651da177e4SLinus Torvalds  * @owner: owner, must be set to THIS_MODULE
4661da177e4SLinus Torvalds  * @link: link to previous/next ematch module (internal use)
4671da177e4SLinus Torvalds  */
468fd2c3ef7SEric Dumazet struct tcf_ematch_ops {
4691da177e4SLinus Torvalds 	int			kind;
4701da177e4SLinus Torvalds 	int			datalen;
47182a470f1SJohn Fastabend 	int			(*change)(struct net *net, void *,
4721da177e4SLinus Torvalds 					  int, struct tcf_ematch *);
4731da177e4SLinus Torvalds 	int			(*match)(struct sk_buff *, struct tcf_ematch *,
4741da177e4SLinus Torvalds 					 struct tcf_pkt_info *);
47582a470f1SJohn Fastabend 	void			(*destroy)(struct tcf_ematch *);
4761da177e4SLinus Torvalds 	int			(*dump)(struct sk_buff *, struct tcf_ematch *);
4771da177e4SLinus Torvalds 	struct module		*owner;
4781da177e4SLinus Torvalds 	struct list_head	link;
4791da177e4SLinus Torvalds };
4801da177e4SLinus Torvalds 
4815c15257fSJoe Perches int tcf_em_register(struct tcf_ematch_ops *);
4825c15257fSJoe Perches void tcf_em_unregister(struct tcf_ematch_ops *);
4835c15257fSJoe Perches int tcf_em_tree_validate(struct tcf_proto *, struct nlattr *,
4841da177e4SLinus Torvalds 			 struct tcf_ematch_tree *);
48582a470f1SJohn Fastabend void tcf_em_tree_destroy(struct tcf_ematch_tree *);
4865c15257fSJoe Perches int tcf_em_tree_dump(struct sk_buff *, struct tcf_ematch_tree *, int);
4875c15257fSJoe Perches int __tcf_em_tree_match(struct sk_buff *, struct tcf_ematch_tree *,
4881da177e4SLinus Torvalds 			struct tcf_pkt_info *);
4891da177e4SLinus Torvalds 
4901da177e4SLinus Torvalds /**
4911da177e4SLinus Torvalds  * tcf_em_tree_match - evaulate an ematch tree
4921da177e4SLinus Torvalds  *
4931da177e4SLinus Torvalds  * @skb: socket buffer of the packet in question
4941da177e4SLinus Torvalds  * @tree: ematch tree to be used for evaluation
4951da177e4SLinus Torvalds  * @info: packet information examined by classifier
4961da177e4SLinus Torvalds  *
4971da177e4SLinus Torvalds  * This function matches @skb against the ematch tree in @tree by going
4981da177e4SLinus Torvalds  * through all ematches respecting their logic relations returning
4991da177e4SLinus Torvalds  * as soon as the result is obvious.
5001da177e4SLinus Torvalds  *
5011da177e4SLinus Torvalds  * Returns 1 if the ematch tree as-one matches, no ematches are configured
5021da177e4SLinus Torvalds  * or ematch is not enabled in the kernel, otherwise 0 is returned.
5031da177e4SLinus Torvalds  */
5041da177e4SLinus Torvalds static inline int tcf_em_tree_match(struct sk_buff *skb,
5051da177e4SLinus Torvalds 				    struct tcf_ematch_tree *tree,
5061da177e4SLinus Torvalds 				    struct tcf_pkt_info *info)
5071da177e4SLinus Torvalds {
5081da177e4SLinus Torvalds 	if (tree->hdr.nmatches)
5091da177e4SLinus Torvalds 		return __tcf_em_tree_match(skb, tree, info);
5101da177e4SLinus Torvalds 	else
5111da177e4SLinus Torvalds 		return 1;
5121da177e4SLinus Torvalds }
5131da177e4SLinus Torvalds 
514db3d99c0SPatrick McHardy #define MODULE_ALIAS_TCF_EMATCH(kind)	MODULE_ALIAS("ematch-kind-" __stringify(kind))
515db3d99c0SPatrick McHardy 
5161da177e4SLinus Torvalds #else /* CONFIG_NET_EMATCH */
5171da177e4SLinus Torvalds 
518fd2c3ef7SEric Dumazet struct tcf_ematch_tree {
5191da177e4SLinus Torvalds };
5201da177e4SLinus Torvalds 
5211da177e4SLinus Torvalds #define tcf_em_tree_validate(tp, tb, t) ((void)(t), 0)
52282a470f1SJohn Fastabend #define tcf_em_tree_destroy(t) do { (void)(t); } while(0)
5231da177e4SLinus Torvalds #define tcf_em_tree_dump(skb, t, tlv) (0)
5241da177e4SLinus Torvalds #define tcf_em_tree_match(skb, t, info) ((void)(info), 1)
5251da177e4SLinus Torvalds 
5261da177e4SLinus Torvalds #endif /* CONFIG_NET_EMATCH */
5271da177e4SLinus Torvalds 
5281da177e4SLinus Torvalds static inline unsigned char * tcf_get_base_ptr(struct sk_buff *skb, int layer)
5291da177e4SLinus Torvalds {
5301da177e4SLinus Torvalds 	switch (layer) {
5311da177e4SLinus Torvalds 		case TCF_LAYER_LINK:
532d3303a65SWolfgang Bumiller 			return skb_mac_header(skb);
5331da177e4SLinus Torvalds 		case TCF_LAYER_NETWORK:
534d56f90a7SArnaldo Carvalho de Melo 			return skb_network_header(skb);
5351da177e4SLinus Torvalds 		case TCF_LAYER_TRANSPORT:
5369c70220bSArnaldo Carvalho de Melo 			return skb_transport_header(skb);
5371da177e4SLinus Torvalds 	}
5381da177e4SLinus Torvalds 
5391da177e4SLinus Torvalds 	return NULL;
5401da177e4SLinus Torvalds }
5411da177e4SLinus Torvalds 
542eddc9ec5SArnaldo Carvalho de Melo static inline int tcf_valid_offset(const struct sk_buff *skb,
543eddc9ec5SArnaldo Carvalho de Melo 				   const unsigned char *ptr, const int len)
5441da177e4SLinus Torvalds {
545da521b2cSDavid S. Miller 	return likely((ptr + len) <= skb_tail_pointer(skb) &&
546da521b2cSDavid S. Miller 		      ptr >= skb->head &&
547da521b2cSDavid S. Miller 		      (ptr <= (ptr + len)));
5481da177e4SLinus Torvalds }
5491da177e4SLinus Torvalds 
5501da177e4SLinus Torvalds static inline int
5511057c55fSAlexander Aring tcf_change_indev(struct net *net, struct nlattr *indev_tlv,
5521057c55fSAlexander Aring 		 struct netlink_ext_ack *extack)
5531da177e4SLinus Torvalds {
5542519a602SWANG Cong 	char indev[IFNAMSIZ];
555c01003c2SPatrick McHardy 	struct net_device *dev;
556c01003c2SPatrick McHardy 
557872f6903SFrancis Laniel 	if (nla_strscpy(indev, indev_tlv, IFNAMSIZ) < 0) {
558e4a58ef3SGuillaume Nault 		NL_SET_ERR_MSG_ATTR(extack, indev_tlv,
559e4a58ef3SGuillaume Nault 				    "Interface name too long");
5602519a602SWANG Cong 		return -EINVAL;
5611057c55fSAlexander Aring 	}
5622519a602SWANG Cong 	dev = __dev_get_by_name(net, indev);
563e4a58ef3SGuillaume Nault 	if (!dev) {
564e4a58ef3SGuillaume Nault 		NL_SET_ERR_MSG_ATTR(extack, indev_tlv,
565e4a58ef3SGuillaume Nault 				    "Network device not found");
5662519a602SWANG Cong 		return -ENODEV;
567e4a58ef3SGuillaume Nault 	}
5682519a602SWANG Cong 	return dev->ifindex;
5691da177e4SLinus Torvalds }
5701da177e4SLinus Torvalds 
5712519a602SWANG Cong static inline bool
5722519a602SWANG Cong tcf_match_indev(struct sk_buff *skb, int ifindex)
5732519a602SWANG Cong {
5742519a602SWANG Cong 	if (!ifindex)
5752519a602SWANG Cong 		return true;
5762519a602SWANG Cong 	if  (!skb->skb_iif)
5772519a602SWANG Cong 		return false;
5782519a602SWANG Cong 	return ifindex == skb->skb_iif;
5791da177e4SLinus Torvalds }
5801da177e4SLinus Torvalds 
5819c1c0e12SBaowen Zheng int tc_setup_offload_action(struct flow_action *flow_action,
582c2ccf84eSIdo Schimmel 			    const struct tcf_exts *exts,
583c2ccf84eSIdo Schimmel 			    struct netlink_ext_ack *extack);
5849c1c0e12SBaowen Zheng void tc_cleanup_offload_action(struct flow_action *flow_action);
5858cbfe939SBaowen Zheng int tc_setup_action(struct flow_action *flow_action,
586c2ccf84eSIdo Schimmel 		    struct tc_action *actions[],
587c2ccf84eSIdo Schimmel 		    struct netlink_ext_ack *extack);
5885a6ff4b1SVlad Buslov 
589aeb3fecdSCong Wang int tc_setup_cb_call(struct tcf_block *block, enum tc_setup_type type,
59040119211SVlad Buslov 		     void *type_data, bool err_stop, bool rtnl_held);
59140119211SVlad Buslov int tc_setup_cb_add(struct tcf_block *block, struct tcf_proto *tp,
59240119211SVlad Buslov 		    enum tc_setup_type type, void *type_data, bool err_stop,
59340119211SVlad Buslov 		    u32 *flags, unsigned int *in_hw_count, bool rtnl_held);
59440119211SVlad Buslov int tc_setup_cb_replace(struct tcf_block *block, struct tcf_proto *tp,
59540119211SVlad Buslov 			enum tc_setup_type type, void *type_data, bool err_stop,
59640119211SVlad Buslov 			u32 *old_flags, unsigned int *old_in_hw_count,
59740119211SVlad Buslov 			u32 *new_flags, unsigned int *new_in_hw_count,
59840119211SVlad Buslov 			bool rtnl_held);
59940119211SVlad Buslov int tc_setup_cb_destroy(struct tcf_block *block, struct tcf_proto *tp,
60040119211SVlad Buslov 			enum tc_setup_type type, void *type_data, bool err_stop,
60140119211SVlad Buslov 			u32 *flags, unsigned int *in_hw_count, bool rtnl_held);
60240119211SVlad Buslov int tc_setup_cb_reoffload(struct tcf_block *block, struct tcf_proto *tp,
60340119211SVlad Buslov 			  bool add, flow_setup_cb_t *cb,
60440119211SVlad Buslov 			  enum tc_setup_type type, void *type_data,
60540119211SVlad Buslov 			  void *cb_priv, u32 *flags, unsigned int *in_hw_count);
606e3ab786bSPablo Neira Ayuso unsigned int tcf_exts_num_actions(struct tcf_exts *exts);
607717503b9SJiri Pirko 
6083625750fSPetr Machata #ifdef CONFIG_NET_CLS_ACT
6093625750fSPetr Machata int tcf_qevent_init(struct tcf_qevent *qe, struct Qdisc *sch,
6103625750fSPetr Machata 		    enum flow_block_binder_type binder_type,
6113625750fSPetr Machata 		    struct nlattr *block_index_attr,
6123625750fSPetr Machata 		    struct netlink_ext_ack *extack);
6133625750fSPetr Machata void tcf_qevent_destroy(struct tcf_qevent *qe, struct Qdisc *sch);
6143625750fSPetr Machata int tcf_qevent_validate_change(struct tcf_qevent *qe, struct nlattr *block_index_attr,
6153625750fSPetr Machata 			       struct netlink_ext_ack *extack);
6163625750fSPetr Machata struct sk_buff *tcf_qevent_handle(struct tcf_qevent *qe, struct Qdisc *sch, struct sk_buff *skb,
61755f656cdSPetr Machata 				  struct sk_buff **to_free, int *ret);
6183625750fSPetr Machata int tcf_qevent_dump(struct sk_buff *skb, int attr_name, struct tcf_qevent *qe);
6193625750fSPetr Machata #else
6203625750fSPetr Machata static inline int tcf_qevent_init(struct tcf_qevent *qe, struct Qdisc *sch,
6213625750fSPetr Machata 				  enum flow_block_binder_type binder_type,
6223625750fSPetr Machata 				  struct nlattr *block_index_attr,
6233625750fSPetr Machata 				  struct netlink_ext_ack *extack)
6243625750fSPetr Machata {
6253625750fSPetr Machata 	return 0;
6263625750fSPetr Machata }
6273625750fSPetr Machata 
6283625750fSPetr Machata static inline void tcf_qevent_destroy(struct tcf_qevent *qe, struct Qdisc *sch)
6293625750fSPetr Machata {
6303625750fSPetr Machata }
6313625750fSPetr Machata 
6323625750fSPetr Machata static inline int tcf_qevent_validate_change(struct tcf_qevent *qe, struct nlattr *block_index_attr,
6333625750fSPetr Machata 					     struct netlink_ext_ack *extack)
6343625750fSPetr Machata {
6353625750fSPetr Machata 	return 0;
6363625750fSPetr Machata }
6373625750fSPetr Machata 
6383625750fSPetr Machata static inline struct sk_buff *
6393625750fSPetr Machata tcf_qevent_handle(struct tcf_qevent *qe, struct Qdisc *sch, struct sk_buff *skb,
64055f656cdSPetr Machata 		  struct sk_buff **to_free, int *ret)
6413625750fSPetr Machata {
6423625750fSPetr Machata 	return skb;
6433625750fSPetr Machata }
6443625750fSPetr Machata 
6453625750fSPetr Machata static inline int tcf_qevent_dump(struct sk_buff *skb, int attr_name, struct tcf_qevent *qe)
6463625750fSPetr Machata {
6473625750fSPetr Machata 	return 0;
6483625750fSPetr Machata }
6493625750fSPetr Machata #endif
6503625750fSPetr Machata 
651a1b7c5fdSJohn Fastabend struct tc_cls_u32_knode {
652a1b7c5fdSJohn Fastabend 	struct tcf_exts *exts;
653068ceb35SJakub Kicinski 	struct tcf_result *res;
654e014860eSJohn Fastabend 	struct tc_u32_sel *sel;
655a1b7c5fdSJohn Fastabend 	u32 handle;
656a1b7c5fdSJohn Fastabend 	u32 val;
657a1b7c5fdSJohn Fastabend 	u32 mask;
658a1b7c5fdSJohn Fastabend 	u32 link_handle;
659e014860eSJohn Fastabend 	u8 fshift;
660a1b7c5fdSJohn Fastabend };
661a1b7c5fdSJohn Fastabend 
662a1b7c5fdSJohn Fastabend struct tc_cls_u32_hnode {
663a1b7c5fdSJohn Fastabend 	u32 handle;
664a1b7c5fdSJohn Fastabend 	u32 prio;
665a1b7c5fdSJohn Fastabend 	unsigned int divisor;
666a1b7c5fdSJohn Fastabend };
667a1b7c5fdSJohn Fastabend 
668a1b7c5fdSJohn Fastabend enum tc_clsu32_command {
669a1b7c5fdSJohn Fastabend 	TC_CLSU32_NEW_KNODE,
670a1b7c5fdSJohn Fastabend 	TC_CLSU32_REPLACE_KNODE,
671a1b7c5fdSJohn Fastabend 	TC_CLSU32_DELETE_KNODE,
672a1b7c5fdSJohn Fastabend 	TC_CLSU32_NEW_HNODE,
673a1b7c5fdSJohn Fastabend 	TC_CLSU32_REPLACE_HNODE,
674a1b7c5fdSJohn Fastabend 	TC_CLSU32_DELETE_HNODE,
675a1b7c5fdSJohn Fastabend };
676a1b7c5fdSJohn Fastabend 
677a1b7c5fdSJohn Fastabend struct tc_cls_u32_offload {
678f9e30088SPablo Neira Ayuso 	struct flow_cls_common_offload common;
679a1b7c5fdSJohn Fastabend 	/* knode values */
680a1b7c5fdSJohn Fastabend 	enum tc_clsu32_command command;
681a1b7c5fdSJohn Fastabend 	union {
682a1b7c5fdSJohn Fastabend 		struct tc_cls_u32_knode knode;
683a1b7c5fdSJohn Fastabend 		struct tc_cls_u32_hnode hnode;
684a1b7c5fdSJohn Fastabend 	};
685a1b7c5fdSJohn Fastabend };
686a1b7c5fdSJohn Fastabend 
6877b06e8aeSJiri Pirko static inline bool tc_can_offload(const struct net_device *dev)
6886843e7a2SJohn Fastabend {
68970b5aee4SJiri Pirko 	return dev->features & NETIF_F_HW_TC;
6906843e7a2SJohn Fastabend }
6916843e7a2SJohn Fastabend 
692f9eda14fSQuentin Monnet static inline bool tc_can_offload_extack(const struct net_device *dev,
693f9eda14fSQuentin Monnet 					 struct netlink_ext_ack *extack)
694f9eda14fSQuentin Monnet {
695f9eda14fSQuentin Monnet 	bool can = tc_can_offload(dev);
696f9eda14fSQuentin Monnet 
697f9eda14fSQuentin Monnet 	if (!can)
698f9eda14fSQuentin Monnet 		NL_SET_ERR_MSG(extack, "TC offload is disabled on net device");
699f9eda14fSQuentin Monnet 
700f9eda14fSQuentin Monnet 	return can;
701f9eda14fSQuentin Monnet }
702f9eda14fSQuentin Monnet 
703878db9f0SJakub Kicinski static inline bool
704878db9f0SJakub Kicinski tc_cls_can_offload_and_chain0(const struct net_device *dev,
705f9e30088SPablo Neira Ayuso 			      struct flow_cls_common_offload *common)
706878db9f0SJakub Kicinski {
707878db9f0SJakub Kicinski 	if (!tc_can_offload_extack(dev, common->extack))
708878db9f0SJakub Kicinski 		return false;
709878db9f0SJakub Kicinski 	if (common->chain_index) {
710878db9f0SJakub Kicinski 		NL_SET_ERR_MSG(common->extack,
711878db9f0SJakub Kicinski 			       "Driver supports only offload of chain 0");
712878db9f0SJakub Kicinski 		return false;
713878db9f0SJakub Kicinski 	}
714878db9f0SJakub Kicinski 	return true;
715878db9f0SJakub Kicinski }
716878db9f0SJakub Kicinski 
71755330f05SHadar Hen Zion static inline bool tc_skip_hw(u32 flags)
71855330f05SHadar Hen Zion {
71955330f05SHadar Hen Zion 	return (flags & TCA_CLS_FLAGS_SKIP_HW) ? true : false;
72055330f05SHadar Hen Zion }
72155330f05SHadar Hen Zion 
722d34e3e18SSamudrala, Sridhar static inline bool tc_skip_sw(u32 flags)
723d34e3e18SSamudrala, Sridhar {
724d34e3e18SSamudrala, Sridhar 	return (flags & TCA_CLS_FLAGS_SKIP_SW) ? true : false;
725d34e3e18SSamudrala, Sridhar }
726d34e3e18SSamudrala, Sridhar 
727d34e3e18SSamudrala, Sridhar /* SKIP_HW and SKIP_SW are mutually exclusive flags. */
728d34e3e18SSamudrala, Sridhar static inline bool tc_flags_valid(u32 flags)
729d34e3e18SSamudrala, Sridhar {
73081c7288bSMarcelo Ricardo Leitner 	if (flags & ~(TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW |
73181c7288bSMarcelo Ricardo Leitner 		      TCA_CLS_FLAGS_VERBOSE))
732d34e3e18SSamudrala, Sridhar 		return false;
733d34e3e18SSamudrala, Sridhar 
73481c7288bSMarcelo Ricardo Leitner 	flags &= TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW;
735d34e3e18SSamudrala, Sridhar 	if (!(flags ^ (TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW)))
736d34e3e18SSamudrala, Sridhar 		return false;
737d34e3e18SSamudrala, Sridhar 
738d34e3e18SSamudrala, Sridhar 	return true;
739d34e3e18SSamudrala, Sridhar }
740d34e3e18SSamudrala, Sridhar 
741e696028aSOr Gerlitz static inline bool tc_in_hw(u32 flags)
742e696028aSOr Gerlitz {
743e696028aSOr Gerlitz 	return (flags & TCA_CLS_FLAGS_IN_HW) ? true : false;
744e696028aSOr Gerlitz }
745e696028aSOr Gerlitz 
74634832e1cSJakub Kicinski static inline void
747f9e30088SPablo Neira Ayuso tc_cls_common_offload_init(struct flow_cls_common_offload *cls_common,
74834832e1cSJakub Kicinski 			   const struct tcf_proto *tp, u32 flags,
74934832e1cSJakub Kicinski 			   struct netlink_ext_ack *extack)
75034832e1cSJakub Kicinski {
75134832e1cSJakub Kicinski 	cls_common->chain_index = tp->chain->index;
75234832e1cSJakub Kicinski 	cls_common->protocol = tp->protocol;
753ef01adaeSPablo Neira Ayuso 	cls_common->prio = tp->prio >> 16;
75481c7288bSMarcelo Ricardo Leitner 	if (tc_skip_sw(flags) || flags & TCA_CLS_FLAGS_VERBOSE)
75534832e1cSJakub Kicinski 		cls_common->extack = extack;
75634832e1cSJakub Kicinski }
75734832e1cSJakub Kicinski 
7589453d45eSVlad Buslov #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
7599453d45eSVlad Buslov static inline struct tc_skb_ext *tc_skb_ext_alloc(struct sk_buff *skb)
7609453d45eSVlad Buslov {
7619453d45eSVlad Buslov 	struct tc_skb_ext *tc_skb_ext = skb_ext_add(skb, TC_SKB_EXT);
7629453d45eSVlad Buslov 
7639453d45eSVlad Buslov 	if (tc_skb_ext)
7649453d45eSVlad Buslov 		memset(tc_skb_ext, 0, sizeof(*tc_skb_ext));
7659453d45eSVlad Buslov 	return tc_skb_ext;
7669453d45eSVlad Buslov }
7679453d45eSVlad Buslov #endif
7689453d45eSVlad Buslov 
769b87f7936SYotam Gigi enum tc_matchall_command {
770b87f7936SYotam Gigi 	TC_CLSMATCHALL_REPLACE,
771b87f7936SYotam Gigi 	TC_CLSMATCHALL_DESTROY,
772b7fe4ab8SPieter Jansen van Vuuren 	TC_CLSMATCHALL_STATS,
773b87f7936SYotam Gigi };
774b87f7936SYotam Gigi 
775b87f7936SYotam Gigi struct tc_cls_matchall_offload {
776f9e30088SPablo Neira Ayuso 	struct flow_cls_common_offload common;
777b87f7936SYotam Gigi 	enum tc_matchall_command command;
778f00cbf19SPieter Jansen van Vuuren 	struct flow_rule *rule;
779b7fe4ab8SPieter Jansen van Vuuren 	struct flow_stats stats;
780*5246c896SOz Shlomo 	bool use_act_stats;
781b87f7936SYotam Gigi 	unsigned long cookie;
782b87f7936SYotam Gigi };
783b87f7936SYotam Gigi 
784332ae8e2SJakub Kicinski enum tc_clsbpf_command {
785102740bdSJakub Kicinski 	TC_CLSBPF_OFFLOAD,
78668d64063SJakub Kicinski 	TC_CLSBPF_STATS,
787332ae8e2SJakub Kicinski };
788332ae8e2SJakub Kicinski 
789332ae8e2SJakub Kicinski struct tc_cls_bpf_offload {
790f9e30088SPablo Neira Ayuso 	struct flow_cls_common_offload common;
791332ae8e2SJakub Kicinski 	enum tc_clsbpf_command command;
792332ae8e2SJakub Kicinski 	struct tcf_exts *exts;
793332ae8e2SJakub Kicinski 	struct bpf_prog *prog;
794102740bdSJakub Kicinski 	struct bpf_prog *oldprog;
795332ae8e2SJakub Kicinski 	const char *name;
796332ae8e2SJakub Kicinski 	bool exts_integrated;
797332ae8e2SJakub Kicinski };
798332ae8e2SJakub Kicinski 
7991045ba77SJamal Hadi Salim /* This structure holds cookie structure that is passed from user
8001045ba77SJamal Hadi Salim  * to the kernel for actions and classifiers
8011045ba77SJamal Hadi Salim  */
8021045ba77SJamal Hadi Salim struct tc_cookie {
8031045ba77SJamal Hadi Salim 	u8  *data;
8041045ba77SJamal Hadi Salim 	u32 len;
805eec94fdbSVlad Buslov 	struct rcu_head rcu;
8061045ba77SJamal Hadi Salim };
807602f3bafSNogah Frankel 
808f34b4aacSNogah Frankel struct tc_qopt_offload_stats {
80950dc9a85SAhmed S. Darwish 	struct gnet_stats_basic_sync *bstats;
810f34b4aacSNogah Frankel 	struct gnet_stats_queue *qstats;
811f34b4aacSNogah Frankel };
812f34b4aacSNogah Frankel 
813f971b132SJakub Kicinski enum tc_mq_command {
814f971b132SJakub Kicinski 	TC_MQ_CREATE,
815f971b132SJakub Kicinski 	TC_MQ_DESTROY,
81647c669a4SJakub Kicinski 	TC_MQ_STATS,
817d577a3d2SJakub Kicinski 	TC_MQ_GRAFT,
818d577a3d2SJakub Kicinski };
819d577a3d2SJakub Kicinski 
820d577a3d2SJakub Kicinski struct tc_mq_opt_offload_graft_params {
821d577a3d2SJakub Kicinski 	unsigned long queue;
822d577a3d2SJakub Kicinski 	u32 child_handle;
823f971b132SJakub Kicinski };
824f971b132SJakub Kicinski 
825f971b132SJakub Kicinski struct tc_mq_qopt_offload {
826f971b132SJakub Kicinski 	enum tc_mq_command command;
827f971b132SJakub Kicinski 	u32 handle;
828d577a3d2SJakub Kicinski 	union {
82947c669a4SJakub Kicinski 		struct tc_qopt_offload_stats stats;
830d577a3d2SJakub Kicinski 		struct tc_mq_opt_offload_graft_params graft_params;
831d577a3d2SJakub Kicinski 	};
832f971b132SJakub Kicinski };
833f971b132SJakub Kicinski 
834d03b195bSMaxim Mikityanskiy enum tc_htb_command {
835d03b195bSMaxim Mikityanskiy 	/* Root */
836d03b195bSMaxim Mikityanskiy 	TC_HTB_CREATE, /* Initialize HTB offload. */
837d03b195bSMaxim Mikityanskiy 	TC_HTB_DESTROY, /* Destroy HTB offload. */
838d03b195bSMaxim Mikityanskiy 
839d03b195bSMaxim Mikityanskiy 	/* Classes */
840d03b195bSMaxim Mikityanskiy 	/* Allocate qid and create leaf. */
841d03b195bSMaxim Mikityanskiy 	TC_HTB_LEAF_ALLOC_QUEUE,
842d03b195bSMaxim Mikityanskiy 	/* Convert leaf to inner, preserve and return qid, create new leaf. */
843d03b195bSMaxim Mikityanskiy 	TC_HTB_LEAF_TO_INNER,
844d03b195bSMaxim Mikityanskiy 	/* Delete leaf, while siblings remain. */
845d03b195bSMaxim Mikityanskiy 	TC_HTB_LEAF_DEL,
846d03b195bSMaxim Mikityanskiy 	/* Delete leaf, convert parent to leaf, preserving qid. */
847d03b195bSMaxim Mikityanskiy 	TC_HTB_LEAF_DEL_LAST,
848d03b195bSMaxim Mikityanskiy 	/* TC_HTB_LEAF_DEL_LAST, but delete driver data on hardware errors. */
849d03b195bSMaxim Mikityanskiy 	TC_HTB_LEAF_DEL_LAST_FORCE,
850d03b195bSMaxim Mikityanskiy 	/* Modify parameters of a node. */
851d03b195bSMaxim Mikityanskiy 	TC_HTB_NODE_MODIFY,
852d03b195bSMaxim Mikityanskiy 
853d03b195bSMaxim Mikityanskiy 	/* Class qdisc */
854d03b195bSMaxim Mikityanskiy 	TC_HTB_LEAF_QUERY_QUEUE, /* Query qid by classid. */
855d03b195bSMaxim Mikityanskiy };
856d03b195bSMaxim Mikityanskiy 
857d03b195bSMaxim Mikityanskiy struct tc_htb_qopt_offload {
858d03b195bSMaxim Mikityanskiy 	struct netlink_ext_ack *extack;
859d03b195bSMaxim Mikityanskiy 	enum tc_htb_command command;
860d03b195bSMaxim Mikityanskiy 	u32 parent_classid;
861ca49bfd9SMaxim Mikityanskiy 	u16 classid;
862d03b195bSMaxim Mikityanskiy 	u16 qid;
863d03b195bSMaxim Mikityanskiy 	u64 rate;
864d03b195bSMaxim Mikityanskiy 	u64 ceil;
865d03b195bSMaxim Mikityanskiy };
866d03b195bSMaxim Mikityanskiy 
867d03b195bSMaxim Mikityanskiy #define TC_HTB_CLASSID_ROOT U32_MAX
868d03b195bSMaxim Mikityanskiy 
869602f3bafSNogah Frankel enum tc_red_command {
870602f3bafSNogah Frankel 	TC_RED_REPLACE,
871602f3bafSNogah Frankel 	TC_RED_DESTROY,
872602f3bafSNogah Frankel 	TC_RED_STATS,
873602f3bafSNogah Frankel 	TC_RED_XSTATS,
874bf2a752bSJakub Kicinski 	TC_RED_GRAFT,
875602f3bafSNogah Frankel };
876602f3bafSNogah Frankel 
877602f3bafSNogah Frankel struct tc_red_qopt_offload_params {
878602f3bafSNogah Frankel 	u32 min;
879602f3bafSNogah Frankel 	u32 max;
880602f3bafSNogah Frankel 	u32 probability;
881c0b7490bSJakub Kicinski 	u32 limit;
882602f3bafSNogah Frankel 	bool is_ecn;
883190852a5SJakub Kicinski 	bool is_harddrop;
8840a7fad23SPetr Machata 	bool is_nodrop;
885416ef9b1SJakub Kicinski 	struct gnet_stats_queue *qstats;
886602f3bafSNogah Frankel };
887602f3bafSNogah Frankel 
888602f3bafSNogah Frankel struct tc_red_qopt_offload {
889602f3bafSNogah Frankel 	enum tc_red_command command;
890602f3bafSNogah Frankel 	u32 handle;
891602f3bafSNogah Frankel 	u32 parent;
892602f3bafSNogah Frankel 	union {
893602f3bafSNogah Frankel 		struct tc_red_qopt_offload_params set;
894f34b4aacSNogah Frankel 		struct tc_qopt_offload_stats stats;
895602f3bafSNogah Frankel 		struct red_stats *xstats;
896bf2a752bSJakub Kicinski 		u32 child_handle;
897602f3bafSNogah Frankel 	};
898602f3bafSNogah Frankel };
899602f3bafSNogah Frankel 
900890d8d23SJakub Kicinski enum tc_gred_command {
901890d8d23SJakub Kicinski 	TC_GRED_REPLACE,
902890d8d23SJakub Kicinski 	TC_GRED_DESTROY,
903e49efd52SJakub Kicinski 	TC_GRED_STATS,
904890d8d23SJakub Kicinski };
905890d8d23SJakub Kicinski 
906890d8d23SJakub Kicinski struct tc_gred_vq_qopt_offload_params {
907890d8d23SJakub Kicinski 	bool present;
908890d8d23SJakub Kicinski 	u32 limit;
909890d8d23SJakub Kicinski 	u32 prio;
910890d8d23SJakub Kicinski 	u32 min;
911890d8d23SJakub Kicinski 	u32 max;
912890d8d23SJakub Kicinski 	bool is_ecn;
913890d8d23SJakub Kicinski 	bool is_harddrop;
914890d8d23SJakub Kicinski 	u32 probability;
915890d8d23SJakub Kicinski 	/* Only need backlog, see struct tc_prio_qopt_offload_params */
916890d8d23SJakub Kicinski 	u32 *backlog;
917890d8d23SJakub Kicinski };
918890d8d23SJakub Kicinski 
919890d8d23SJakub Kicinski struct tc_gred_qopt_offload_params {
920890d8d23SJakub Kicinski 	bool grio_on;
921890d8d23SJakub Kicinski 	bool wred_on;
922890d8d23SJakub Kicinski 	unsigned int dp_cnt;
923890d8d23SJakub Kicinski 	unsigned int dp_def;
924890d8d23SJakub Kicinski 	struct gnet_stats_queue *qstats;
925890d8d23SJakub Kicinski 	struct tc_gred_vq_qopt_offload_params tab[MAX_DPs];
926890d8d23SJakub Kicinski };
927890d8d23SJakub Kicinski 
928e49efd52SJakub Kicinski struct tc_gred_qopt_offload_stats {
92950dc9a85SAhmed S. Darwish 	struct gnet_stats_basic_sync bstats[MAX_DPs];
930e49efd52SJakub Kicinski 	struct gnet_stats_queue qstats[MAX_DPs];
931e49efd52SJakub Kicinski 	struct red_stats *xstats[MAX_DPs];
932e49efd52SJakub Kicinski };
933e49efd52SJakub Kicinski 
934890d8d23SJakub Kicinski struct tc_gred_qopt_offload {
935890d8d23SJakub Kicinski 	enum tc_gred_command command;
936890d8d23SJakub Kicinski 	u32 handle;
937890d8d23SJakub Kicinski 	u32 parent;
938890d8d23SJakub Kicinski 	union {
939890d8d23SJakub Kicinski 		struct tc_gred_qopt_offload_params set;
940e49efd52SJakub Kicinski 		struct tc_gred_qopt_offload_stats stats;
941890d8d23SJakub Kicinski 	};
942890d8d23SJakub Kicinski };
943890d8d23SJakub Kicinski 
9447fdb61b4SNogah Frankel enum tc_prio_command {
9457fdb61b4SNogah Frankel 	TC_PRIO_REPLACE,
9467fdb61b4SNogah Frankel 	TC_PRIO_DESTROY,
9477fdb61b4SNogah Frankel 	TC_PRIO_STATS,
948b9c7a7acSNogah Frankel 	TC_PRIO_GRAFT,
9497fdb61b4SNogah Frankel };
9507fdb61b4SNogah Frankel 
9517fdb61b4SNogah Frankel struct tc_prio_qopt_offload_params {
9527fdb61b4SNogah Frankel 	int bands;
9537fdb61b4SNogah Frankel 	u8 priomap[TC_PRIO_MAX + 1];
9549586a992SPetr Machata 	/* At the point of un-offloading the Qdisc, the reported backlog and
9559586a992SPetr Machata 	 * qlen need to be reduced by the portion that is in HW.
9567fdb61b4SNogah Frankel 	 */
9577fdb61b4SNogah Frankel 	struct gnet_stats_queue *qstats;
9587fdb61b4SNogah Frankel };
9597fdb61b4SNogah Frankel 
960b9c7a7acSNogah Frankel struct tc_prio_qopt_offload_graft_params {
961b9c7a7acSNogah Frankel 	u8 band;
962b9c7a7acSNogah Frankel 	u32 child_handle;
963b9c7a7acSNogah Frankel };
964b9c7a7acSNogah Frankel 
9657fdb61b4SNogah Frankel struct tc_prio_qopt_offload {
9667fdb61b4SNogah Frankel 	enum tc_prio_command command;
9677fdb61b4SNogah Frankel 	u32 handle;
9687fdb61b4SNogah Frankel 	u32 parent;
9697fdb61b4SNogah Frankel 	union {
9707fdb61b4SNogah Frankel 		struct tc_prio_qopt_offload_params replace_params;
9717fdb61b4SNogah Frankel 		struct tc_qopt_offload_stats stats;
972b9c7a7acSNogah Frankel 		struct tc_prio_qopt_offload_graft_params graft_params;
9737fdb61b4SNogah Frankel 	};
9747fdb61b4SNogah Frankel };
975b9c7a7acSNogah Frankel 
97698b0e5f6SJakub Kicinski enum tc_root_command {
97798b0e5f6SJakub Kicinski 	TC_ROOT_GRAFT,
97898b0e5f6SJakub Kicinski };
97998b0e5f6SJakub Kicinski 
98098b0e5f6SJakub Kicinski struct tc_root_qopt_offload {
98198b0e5f6SJakub Kicinski 	enum tc_root_command command;
98298b0e5f6SJakub Kicinski 	u32 handle;
98398b0e5f6SJakub Kicinski 	bool ingress;
98498b0e5f6SJakub Kicinski };
98598b0e5f6SJakub Kicinski 
986d35eb52bSPetr Machata enum tc_ets_command {
987d35eb52bSPetr Machata 	TC_ETS_REPLACE,
988d35eb52bSPetr Machata 	TC_ETS_DESTROY,
989d35eb52bSPetr Machata 	TC_ETS_STATS,
990d35eb52bSPetr Machata 	TC_ETS_GRAFT,
991d35eb52bSPetr Machata };
992d35eb52bSPetr Machata 
993d35eb52bSPetr Machata struct tc_ets_qopt_offload_replace_params {
994d35eb52bSPetr Machata 	unsigned int bands;
995d35eb52bSPetr Machata 	u8 priomap[TC_PRIO_MAX + 1];
996d35eb52bSPetr Machata 	unsigned int quanta[TCQ_ETS_MAX_BANDS];	/* 0 for strict bands. */
997d35eb52bSPetr Machata 	unsigned int weights[TCQ_ETS_MAX_BANDS];
998d35eb52bSPetr Machata 	struct gnet_stats_queue *qstats;
999d35eb52bSPetr Machata };
1000d35eb52bSPetr Machata 
1001d35eb52bSPetr Machata struct tc_ets_qopt_offload_graft_params {
1002d35eb52bSPetr Machata 	u8 band;
1003d35eb52bSPetr Machata 	u32 child_handle;
1004d35eb52bSPetr Machata };
1005d35eb52bSPetr Machata 
1006d35eb52bSPetr Machata struct tc_ets_qopt_offload {
1007d35eb52bSPetr Machata 	enum tc_ets_command command;
1008d35eb52bSPetr Machata 	u32 handle;
1009d35eb52bSPetr Machata 	u32 parent;
1010d35eb52bSPetr Machata 	union {
1011d35eb52bSPetr Machata 		struct tc_ets_qopt_offload_replace_params replace_params;
1012d35eb52bSPetr Machata 		struct tc_qopt_offload_stats stats;
1013d35eb52bSPetr Machata 		struct tc_ets_qopt_offload_graft_params graft_params;
1014d35eb52bSPetr Machata 	};
1015d35eb52bSPetr Machata };
1016d35eb52bSPetr Machata 
1017ef6aadccSPetr Machata enum tc_tbf_command {
1018ef6aadccSPetr Machata 	TC_TBF_REPLACE,
1019ef6aadccSPetr Machata 	TC_TBF_DESTROY,
1020ef6aadccSPetr Machata 	TC_TBF_STATS,
10216b3efbfaSPetr Machata 	TC_TBF_GRAFT,
1022ef6aadccSPetr Machata };
1023ef6aadccSPetr Machata 
1024ef6aadccSPetr Machata struct tc_tbf_qopt_offload_replace_params {
1025ef6aadccSPetr Machata 	struct psched_ratecfg rate;
1026ef6aadccSPetr Machata 	u32 max_size;
1027ef6aadccSPetr Machata 	struct gnet_stats_queue *qstats;
1028ef6aadccSPetr Machata };
1029ef6aadccSPetr Machata 
1030ef6aadccSPetr Machata struct tc_tbf_qopt_offload {
1031ef6aadccSPetr Machata 	enum tc_tbf_command command;
1032ef6aadccSPetr Machata 	u32 handle;
1033ef6aadccSPetr Machata 	u32 parent;
1034ef6aadccSPetr Machata 	union {
1035ef6aadccSPetr Machata 		struct tc_tbf_qopt_offload_replace_params replace_params;
1036ef6aadccSPetr Machata 		struct tc_qopt_offload_stats stats;
10376b3efbfaSPetr Machata 		u32 child_handle;
1038ef6aadccSPetr Machata 	};
1039ef6aadccSPetr Machata };
1040ef6aadccSPetr Machata 
1041aaca9408SPetr Machata enum tc_fifo_command {
1042aaca9408SPetr Machata 	TC_FIFO_REPLACE,
1043aaca9408SPetr Machata 	TC_FIFO_DESTROY,
1044aaca9408SPetr Machata 	TC_FIFO_STATS,
1045aaca9408SPetr Machata };
1046aaca9408SPetr Machata 
1047aaca9408SPetr Machata struct tc_fifo_qopt_offload {
1048aaca9408SPetr Machata 	enum tc_fifo_command command;
1049aaca9408SPetr Machata 	u32 handle;
1050aaca9408SPetr Machata 	u32 parent;
1051aaca9408SPetr Machata 	union {
1052aaca9408SPetr Machata 		struct tc_qopt_offload_stats stats;
1053aaca9408SPetr Machata 	};
1054aaca9408SPetr Machata };
1055aaca9408SPetr Machata 
105635d39fecSPaul Blakey #ifdef CONFIG_NET_CLS_ACT
105735d39fecSPaul Blakey DECLARE_STATIC_KEY_FALSE(tc_skb_ext_tc);
105835d39fecSPaul Blakey void tc_skb_ext_tc_enable(void);
105935d39fecSPaul Blakey void tc_skb_ext_tc_disable(void);
106035d39fecSPaul Blakey #define tc_skb_ext_tc_enabled() static_branch_unlikely(&tc_skb_ext_tc)
106135d39fecSPaul Blakey #else /* CONFIG_NET_CLS_ACT */
106235d39fecSPaul Blakey static inline void tc_skb_ext_tc_enable(void) { }
106335d39fecSPaul Blakey static inline void tc_skb_ext_tc_disable(void) { }
106435d39fecSPaul Blakey #define tc_skb_ext_tc_enabled() false
106535d39fecSPaul Blakey #endif
106635d39fecSPaul Blakey 
10671da177e4SLinus Torvalds #endif
1068