xref: /openbmc/linux/include/net/pkt_cls.h (revision 8cbfe939)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __NET_PKT_CLS_H
3 #define __NET_PKT_CLS_H
4 
5 #include <linux/pkt_cls.h>
6 #include <linux/workqueue.h>
7 #include <net/sch_generic.h>
8 #include <net/act_api.h>
9 #include <net/net_namespace.h>
10 
11 /* TC action not accessible from user space */
12 #define TC_ACT_CONSUMED		(TC_ACT_VALUE_MAX + 1)
13 
14 /* Basic packet classifier frontend definitions. */
15 
16 struct tcf_walker {
17 	int	stop;
18 	int	skip;
19 	int	count;
20 	bool	nonempty;
21 	unsigned long cookie;
22 	int	(*fn)(struct tcf_proto *, void *node, struct tcf_walker *);
23 };
24 
25 int register_tcf_proto_ops(struct tcf_proto_ops *ops);
26 int unregister_tcf_proto_ops(struct tcf_proto_ops *ops);
27 
28 struct tcf_block_ext_info {
29 	enum flow_block_binder_type binder_type;
30 	tcf_chain_head_change_t *chain_head_change;
31 	void *chain_head_change_priv;
32 	u32 block_index;
33 };
34 
35 struct tcf_qevent {
36 	struct tcf_block	*block;
37 	struct tcf_block_ext_info info;
38 	struct tcf_proto __rcu *filter_chain;
39 };
40 
41 struct tcf_block_cb;
42 bool tcf_queue_work(struct rcu_work *rwork, work_func_t func);
43 
44 #ifdef CONFIG_NET_CLS
45 struct tcf_chain *tcf_chain_get_by_act(struct tcf_block *block,
46 				       u32 chain_index);
47 void tcf_chain_put_by_act(struct tcf_chain *chain);
48 struct tcf_chain *tcf_get_next_chain(struct tcf_block *block,
49 				     struct tcf_chain *chain);
50 struct tcf_proto *tcf_get_next_proto(struct tcf_chain *chain,
51 				     struct tcf_proto *tp);
52 void tcf_block_netif_keep_dst(struct tcf_block *block);
53 int tcf_block_get(struct tcf_block **p_block,
54 		  struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q,
55 		  struct netlink_ext_ack *extack);
56 int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q,
57 		      struct tcf_block_ext_info *ei,
58 		      struct netlink_ext_ack *extack);
59 void tcf_block_put(struct tcf_block *block);
60 void tcf_block_put_ext(struct tcf_block *block, struct Qdisc *q,
61 		       struct tcf_block_ext_info *ei);
62 
63 static inline bool tcf_block_shared(struct tcf_block *block)
64 {
65 	return block->index;
66 }
67 
68 static inline bool tcf_block_non_null_shared(struct tcf_block *block)
69 {
70 	return block && block->index;
71 }
72 
73 static inline struct Qdisc *tcf_block_q(struct tcf_block *block)
74 {
75 	WARN_ON(tcf_block_shared(block));
76 	return block->q;
77 }
78 
79 int tcf_classify(struct sk_buff *skb,
80 		 const struct tcf_block *block,
81 		 const struct tcf_proto *tp, struct tcf_result *res,
82 		 bool compat_mode);
83 
84 #else
85 static inline bool tcf_block_shared(struct tcf_block *block)
86 {
87 	return false;
88 }
89 
90 static inline bool tcf_block_non_null_shared(struct tcf_block *block)
91 {
92 	return false;
93 }
94 
95 static inline
96 int tcf_block_get(struct tcf_block **p_block,
97 		  struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q,
98 		  struct netlink_ext_ack *extack)
99 {
100 	return 0;
101 }
102 
103 static inline
104 int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q,
105 		      struct tcf_block_ext_info *ei,
106 		      struct netlink_ext_ack *extack)
107 {
108 	return 0;
109 }
110 
111 static inline void tcf_block_put(struct tcf_block *block)
112 {
113 }
114 
115 static inline
116 void tcf_block_put_ext(struct tcf_block *block, struct Qdisc *q,
117 		       struct tcf_block_ext_info *ei)
118 {
119 }
120 
121 static inline struct Qdisc *tcf_block_q(struct tcf_block *block)
122 {
123 	return NULL;
124 }
125 
126 static inline
127 int tc_setup_cb_block_register(struct tcf_block *block, flow_setup_cb_t *cb,
128 			       void *cb_priv)
129 {
130 	return 0;
131 }
132 
133 static inline
134 void tc_setup_cb_block_unregister(struct tcf_block *block, flow_setup_cb_t *cb,
135 				  void *cb_priv)
136 {
137 }
138 
139 static inline int tcf_classify(struct sk_buff *skb,
140 			       const struct tcf_block *block,
141 			       const struct tcf_proto *tp,
142 			       struct tcf_result *res, bool compat_mode)
143 {
144 	return TC_ACT_UNSPEC;
145 }
146 
147 #endif
148 
149 static inline unsigned long
150 __cls_set_class(unsigned long *clp, unsigned long cl)
151 {
152 	return xchg(clp, cl);
153 }
154 
155 static inline void
156 __tcf_bind_filter(struct Qdisc *q, struct tcf_result *r, unsigned long base)
157 {
158 	unsigned long cl;
159 
160 	cl = q->ops->cl_ops->bind_tcf(q, base, r->classid);
161 	cl = __cls_set_class(&r->class, cl);
162 	if (cl)
163 		q->ops->cl_ops->unbind_tcf(q, cl);
164 }
165 
166 static inline void
167 tcf_bind_filter(struct tcf_proto *tp, struct tcf_result *r, unsigned long base)
168 {
169 	struct Qdisc *q = tp->chain->block->q;
170 
171 	/* Check q as it is not set for shared blocks. In that case,
172 	 * setting class is not supported.
173 	 */
174 	if (!q)
175 		return;
176 	sch_tree_lock(q);
177 	__tcf_bind_filter(q, r, base);
178 	sch_tree_unlock(q);
179 }
180 
181 static inline void
182 __tcf_unbind_filter(struct Qdisc *q, struct tcf_result *r)
183 {
184 	unsigned long cl;
185 
186 	if ((cl = __cls_set_class(&r->class, 0)) != 0)
187 		q->ops->cl_ops->unbind_tcf(q, cl);
188 }
189 
190 static inline void
191 tcf_unbind_filter(struct tcf_proto *tp, struct tcf_result *r)
192 {
193 	struct Qdisc *q = tp->chain->block->q;
194 
195 	if (!q)
196 		return;
197 	__tcf_unbind_filter(q, r);
198 }
199 
200 struct tcf_exts {
201 #ifdef CONFIG_NET_CLS_ACT
202 	__u32	type; /* for backward compat(TCA_OLD_COMPAT) */
203 	int nr_actions;
204 	struct tc_action **actions;
205 	struct net	*net;
206 	netns_tracker	ns_tracker;
207 #endif
208 	/* Map to export classifier specific extension TLV types to the
209 	 * generic extensions API. Unsupported extensions must be set to 0.
210 	 */
211 	int action;
212 	int police;
213 };
214 
215 static inline int tcf_exts_init(struct tcf_exts *exts, struct net *net,
216 				int action, int police)
217 {
218 #ifdef CONFIG_NET_CLS_ACT
219 	exts->type = 0;
220 	exts->nr_actions = 0;
221 	exts->net = net;
222 	netns_tracker_alloc(net, &exts->ns_tracker, GFP_KERNEL);
223 	exts->actions = kcalloc(TCA_ACT_MAX_PRIO, sizeof(struct tc_action *),
224 				GFP_KERNEL);
225 	if (!exts->actions)
226 		return -ENOMEM;
227 #endif
228 	exts->action = action;
229 	exts->police = police;
230 	return 0;
231 }
232 
233 /* Return false if the netns is being destroyed in cleanup_net(). Callers
234  * need to do cleanup synchronously in this case, otherwise may race with
235  * tc_action_net_exit(). Return true for other cases.
236  */
237 static inline bool tcf_exts_get_net(struct tcf_exts *exts)
238 {
239 #ifdef CONFIG_NET_CLS_ACT
240 	exts->net = maybe_get_net(exts->net);
241 	if (exts->net)
242 		netns_tracker_alloc(exts->net, &exts->ns_tracker, GFP_KERNEL);
243 	return exts->net != NULL;
244 #else
245 	return true;
246 #endif
247 }
248 
249 static inline void tcf_exts_put_net(struct tcf_exts *exts)
250 {
251 #ifdef CONFIG_NET_CLS_ACT
252 	if (exts->net)
253 		put_net_track(exts->net, &exts->ns_tracker);
254 #endif
255 }
256 
257 #ifdef CONFIG_NET_CLS_ACT
258 #define tcf_exts_for_each_action(i, a, exts) \
259 	for (i = 0; i < TCA_ACT_MAX_PRIO && ((a) = (exts)->actions[i]); i++)
260 #else
261 #define tcf_exts_for_each_action(i, a, exts) \
262 	for (; 0; (void)(i), (void)(a), (void)(exts))
263 #endif
264 
265 #define tcf_act_for_each_action(i, a, actions) \
266 	for (i = 0; i < TCA_ACT_MAX_PRIO && ((a) = actions[i]); i++)
267 
268 static inline void
269 tcf_exts_stats_update(const struct tcf_exts *exts,
270 		      u64 bytes, u64 packets, u64 drops, u64 lastuse,
271 		      u8 used_hw_stats, bool used_hw_stats_valid)
272 {
273 #ifdef CONFIG_NET_CLS_ACT
274 	int i;
275 
276 	preempt_disable();
277 
278 	for (i = 0; i < exts->nr_actions; i++) {
279 		struct tc_action *a = exts->actions[i];
280 
281 		tcf_action_stats_update(a, bytes, packets, drops,
282 					lastuse, true);
283 		a->used_hw_stats = used_hw_stats;
284 		a->used_hw_stats_valid = used_hw_stats_valid;
285 	}
286 
287 	preempt_enable();
288 #endif
289 }
290 
291 /**
292  * tcf_exts_has_actions - check if at least one action is present
293  * @exts: tc filter extensions handle
294  *
295  * Returns true if at least one action is present.
296  */
297 static inline bool tcf_exts_has_actions(struct tcf_exts *exts)
298 {
299 #ifdef CONFIG_NET_CLS_ACT
300 	return exts->nr_actions;
301 #else
302 	return false;
303 #endif
304 }
305 
306 /**
307  * tcf_exts_exec - execute tc filter extensions
308  * @skb: socket buffer
309  * @exts: tc filter extensions handle
310  * @res: desired result
311  *
312  * Executes all configured extensions. Returns TC_ACT_OK on a normal execution,
313  * a negative number if the filter must be considered unmatched or
314  * a positive action code (TC_ACT_*) which must be returned to the
315  * underlying layer.
316  */
317 static inline int
318 tcf_exts_exec(struct sk_buff *skb, struct tcf_exts *exts,
319 	      struct tcf_result *res)
320 {
321 #ifdef CONFIG_NET_CLS_ACT
322 	return tcf_action_exec(skb, exts->actions, exts->nr_actions, res);
323 #endif
324 	return TC_ACT_OK;
325 }
326 
327 int tcf_exts_validate(struct net *net, struct tcf_proto *tp,
328 		      struct nlattr **tb, struct nlattr *rate_tlv,
329 		      struct tcf_exts *exts, u32 flags,
330 		      struct netlink_ext_ack *extack);
331 void tcf_exts_destroy(struct tcf_exts *exts);
332 void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src);
333 int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts);
334 int tcf_exts_terse_dump(struct sk_buff *skb, struct tcf_exts *exts);
335 int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts);
336 
337 /**
338  * struct tcf_pkt_info - packet information
339  *
340  * @ptr: start of the pkt data
341  * @nexthdr: offset of the next header
342  */
343 struct tcf_pkt_info {
344 	unsigned char *		ptr;
345 	int			nexthdr;
346 };
347 
348 #ifdef CONFIG_NET_EMATCH
349 
350 struct tcf_ematch_ops;
351 
352 /**
353  * struct tcf_ematch - extended match (ematch)
354  *
355  * @matchid: identifier to allow userspace to reidentify a match
356  * @flags: flags specifying attributes and the relation to other matches
357  * @ops: the operations lookup table of the corresponding ematch module
358  * @datalen: length of the ematch specific configuration data
359  * @data: ematch specific data
360  * @net: the network namespace
361  */
362 struct tcf_ematch {
363 	struct tcf_ematch_ops * ops;
364 	unsigned long		data;
365 	unsigned int		datalen;
366 	u16			matchid;
367 	u16			flags;
368 	struct net		*net;
369 };
370 
371 static inline int tcf_em_is_container(struct tcf_ematch *em)
372 {
373 	return !em->ops;
374 }
375 
376 static inline int tcf_em_is_simple(struct tcf_ematch *em)
377 {
378 	return em->flags & TCF_EM_SIMPLE;
379 }
380 
381 static inline int tcf_em_is_inverted(struct tcf_ematch *em)
382 {
383 	return em->flags & TCF_EM_INVERT;
384 }
385 
386 static inline int tcf_em_last_match(struct tcf_ematch *em)
387 {
388 	return (em->flags & TCF_EM_REL_MASK) == TCF_EM_REL_END;
389 }
390 
391 static inline int tcf_em_early_end(struct tcf_ematch *em, int result)
392 {
393 	if (tcf_em_last_match(em))
394 		return 1;
395 
396 	if (result == 0 && em->flags & TCF_EM_REL_AND)
397 		return 1;
398 
399 	if (result != 0 && em->flags & TCF_EM_REL_OR)
400 		return 1;
401 
402 	return 0;
403 }
404 
405 /**
406  * struct tcf_ematch_tree - ematch tree handle
407  *
408  * @hdr: ematch tree header supplied by userspace
409  * @matches: array of ematches
410  */
411 struct tcf_ematch_tree {
412 	struct tcf_ematch_tree_hdr hdr;
413 	struct tcf_ematch *	matches;
414 
415 };
416 
417 /**
418  * struct tcf_ematch_ops - ematch module operations
419  *
420  * @kind: identifier (kind) of this ematch module
421  * @datalen: length of expected configuration data (optional)
422  * @change: called during validation (optional)
423  * @match: called during ematch tree evaluation, must return 1/0
424  * @destroy: called during destroyage (optional)
425  * @dump: called during dumping process (optional)
426  * @owner: owner, must be set to THIS_MODULE
427  * @link: link to previous/next ematch module (internal use)
428  */
429 struct tcf_ematch_ops {
430 	int			kind;
431 	int			datalen;
432 	int			(*change)(struct net *net, void *,
433 					  int, struct tcf_ematch *);
434 	int			(*match)(struct sk_buff *, struct tcf_ematch *,
435 					 struct tcf_pkt_info *);
436 	void			(*destroy)(struct tcf_ematch *);
437 	int			(*dump)(struct sk_buff *, struct tcf_ematch *);
438 	struct module		*owner;
439 	struct list_head	link;
440 };
441 
442 int tcf_em_register(struct tcf_ematch_ops *);
443 void tcf_em_unregister(struct tcf_ematch_ops *);
444 int tcf_em_tree_validate(struct tcf_proto *, struct nlattr *,
445 			 struct tcf_ematch_tree *);
446 void tcf_em_tree_destroy(struct tcf_ematch_tree *);
447 int tcf_em_tree_dump(struct sk_buff *, struct tcf_ematch_tree *, int);
448 int __tcf_em_tree_match(struct sk_buff *, struct tcf_ematch_tree *,
449 			struct tcf_pkt_info *);
450 
451 /**
452  * tcf_em_tree_match - evaulate an ematch tree
453  *
454  * @skb: socket buffer of the packet in question
455  * @tree: ematch tree to be used for evaluation
456  * @info: packet information examined by classifier
457  *
458  * This function matches @skb against the ematch tree in @tree by going
459  * through all ematches respecting their logic relations returning
460  * as soon as the result is obvious.
461  *
462  * Returns 1 if the ematch tree as-one matches, no ematches are configured
463  * or ematch is not enabled in the kernel, otherwise 0 is returned.
464  */
465 static inline int tcf_em_tree_match(struct sk_buff *skb,
466 				    struct tcf_ematch_tree *tree,
467 				    struct tcf_pkt_info *info)
468 {
469 	if (tree->hdr.nmatches)
470 		return __tcf_em_tree_match(skb, tree, info);
471 	else
472 		return 1;
473 }
474 
475 #define MODULE_ALIAS_TCF_EMATCH(kind)	MODULE_ALIAS("ematch-kind-" __stringify(kind))
476 
477 #else /* CONFIG_NET_EMATCH */
478 
479 struct tcf_ematch_tree {
480 };
481 
482 #define tcf_em_tree_validate(tp, tb, t) ((void)(t), 0)
483 #define tcf_em_tree_destroy(t) do { (void)(t); } while(0)
484 #define tcf_em_tree_dump(skb, t, tlv) (0)
485 #define tcf_em_tree_match(skb, t, info) ((void)(info), 1)
486 
487 #endif /* CONFIG_NET_EMATCH */
488 
489 static inline unsigned char * tcf_get_base_ptr(struct sk_buff *skb, int layer)
490 {
491 	switch (layer) {
492 		case TCF_LAYER_LINK:
493 			return skb_mac_header(skb);
494 		case TCF_LAYER_NETWORK:
495 			return skb_network_header(skb);
496 		case TCF_LAYER_TRANSPORT:
497 			return skb_transport_header(skb);
498 	}
499 
500 	return NULL;
501 }
502 
503 static inline int tcf_valid_offset(const struct sk_buff *skb,
504 				   const unsigned char *ptr, const int len)
505 {
506 	return likely((ptr + len) <= skb_tail_pointer(skb) &&
507 		      ptr >= skb->head &&
508 		      (ptr <= (ptr + len)));
509 }
510 
511 static inline int
512 tcf_change_indev(struct net *net, struct nlattr *indev_tlv,
513 		 struct netlink_ext_ack *extack)
514 {
515 	char indev[IFNAMSIZ];
516 	struct net_device *dev;
517 
518 	if (nla_strscpy(indev, indev_tlv, IFNAMSIZ) < 0) {
519 		NL_SET_ERR_MSG_ATTR(extack, indev_tlv,
520 				    "Interface name too long");
521 		return -EINVAL;
522 	}
523 	dev = __dev_get_by_name(net, indev);
524 	if (!dev) {
525 		NL_SET_ERR_MSG_ATTR(extack, indev_tlv,
526 				    "Network device not found");
527 		return -ENODEV;
528 	}
529 	return dev->ifindex;
530 }
531 
532 static inline bool
533 tcf_match_indev(struct sk_buff *skb, int ifindex)
534 {
535 	if (!ifindex)
536 		return true;
537 	if  (!skb->skb_iif)
538 		return false;
539 	return ifindex == skb->skb_iif;
540 }
541 
542 int tc_setup_offload_action(struct flow_action *flow_action,
543 			    const struct tcf_exts *exts);
544 void tc_cleanup_offload_action(struct flow_action *flow_action);
545 int tc_setup_action(struct flow_action *flow_action,
546 		    struct tc_action *actions[]);
547 
548 int tc_setup_cb_call(struct tcf_block *block, enum tc_setup_type type,
549 		     void *type_data, bool err_stop, bool rtnl_held);
550 int tc_setup_cb_add(struct tcf_block *block, struct tcf_proto *tp,
551 		    enum tc_setup_type type, void *type_data, bool err_stop,
552 		    u32 *flags, unsigned int *in_hw_count, bool rtnl_held);
553 int tc_setup_cb_replace(struct tcf_block *block, struct tcf_proto *tp,
554 			enum tc_setup_type type, void *type_data, bool err_stop,
555 			u32 *old_flags, unsigned int *old_in_hw_count,
556 			u32 *new_flags, unsigned int *new_in_hw_count,
557 			bool rtnl_held);
558 int tc_setup_cb_destroy(struct tcf_block *block, struct tcf_proto *tp,
559 			enum tc_setup_type type, void *type_data, bool err_stop,
560 			u32 *flags, unsigned int *in_hw_count, bool rtnl_held);
561 int tc_setup_cb_reoffload(struct tcf_block *block, struct tcf_proto *tp,
562 			  bool add, flow_setup_cb_t *cb,
563 			  enum tc_setup_type type, void *type_data,
564 			  void *cb_priv, u32 *flags, unsigned int *in_hw_count);
565 unsigned int tcf_exts_num_actions(struct tcf_exts *exts);
566 
567 #ifdef CONFIG_NET_CLS_ACT
568 int tcf_qevent_init(struct tcf_qevent *qe, struct Qdisc *sch,
569 		    enum flow_block_binder_type binder_type,
570 		    struct nlattr *block_index_attr,
571 		    struct netlink_ext_ack *extack);
572 void tcf_qevent_destroy(struct tcf_qevent *qe, struct Qdisc *sch);
573 int tcf_qevent_validate_change(struct tcf_qevent *qe, struct nlattr *block_index_attr,
574 			       struct netlink_ext_ack *extack);
575 struct sk_buff *tcf_qevent_handle(struct tcf_qevent *qe, struct Qdisc *sch, struct sk_buff *skb,
576 				  struct sk_buff **to_free, int *ret);
577 int tcf_qevent_dump(struct sk_buff *skb, int attr_name, struct tcf_qevent *qe);
578 #else
579 static inline int tcf_qevent_init(struct tcf_qevent *qe, struct Qdisc *sch,
580 				  enum flow_block_binder_type binder_type,
581 				  struct nlattr *block_index_attr,
582 				  struct netlink_ext_ack *extack)
583 {
584 	return 0;
585 }
586 
587 static inline void tcf_qevent_destroy(struct tcf_qevent *qe, struct Qdisc *sch)
588 {
589 }
590 
591 static inline int tcf_qevent_validate_change(struct tcf_qevent *qe, struct nlattr *block_index_attr,
592 					     struct netlink_ext_ack *extack)
593 {
594 	return 0;
595 }
596 
597 static inline struct sk_buff *
598 tcf_qevent_handle(struct tcf_qevent *qe, struct Qdisc *sch, struct sk_buff *skb,
599 		  struct sk_buff **to_free, int *ret)
600 {
601 	return skb;
602 }
603 
604 static inline int tcf_qevent_dump(struct sk_buff *skb, int attr_name, struct tcf_qevent *qe)
605 {
606 	return 0;
607 }
608 #endif
609 
610 struct tc_cls_u32_knode {
611 	struct tcf_exts *exts;
612 	struct tcf_result *res;
613 	struct tc_u32_sel *sel;
614 	u32 handle;
615 	u32 val;
616 	u32 mask;
617 	u32 link_handle;
618 	u8 fshift;
619 };
620 
621 struct tc_cls_u32_hnode {
622 	u32 handle;
623 	u32 prio;
624 	unsigned int divisor;
625 };
626 
627 enum tc_clsu32_command {
628 	TC_CLSU32_NEW_KNODE,
629 	TC_CLSU32_REPLACE_KNODE,
630 	TC_CLSU32_DELETE_KNODE,
631 	TC_CLSU32_NEW_HNODE,
632 	TC_CLSU32_REPLACE_HNODE,
633 	TC_CLSU32_DELETE_HNODE,
634 };
635 
636 struct tc_cls_u32_offload {
637 	struct flow_cls_common_offload common;
638 	/* knode values */
639 	enum tc_clsu32_command command;
640 	union {
641 		struct tc_cls_u32_knode knode;
642 		struct tc_cls_u32_hnode hnode;
643 	};
644 };
645 
646 static inline bool tc_can_offload(const struct net_device *dev)
647 {
648 	return dev->features & NETIF_F_HW_TC;
649 }
650 
651 static inline bool tc_can_offload_extack(const struct net_device *dev,
652 					 struct netlink_ext_ack *extack)
653 {
654 	bool can = tc_can_offload(dev);
655 
656 	if (!can)
657 		NL_SET_ERR_MSG(extack, "TC offload is disabled on net device");
658 
659 	return can;
660 }
661 
662 static inline bool
663 tc_cls_can_offload_and_chain0(const struct net_device *dev,
664 			      struct flow_cls_common_offload *common)
665 {
666 	if (!tc_can_offload_extack(dev, common->extack))
667 		return false;
668 	if (common->chain_index) {
669 		NL_SET_ERR_MSG(common->extack,
670 			       "Driver supports only offload of chain 0");
671 		return false;
672 	}
673 	return true;
674 }
675 
676 static inline bool tc_skip_hw(u32 flags)
677 {
678 	return (flags & TCA_CLS_FLAGS_SKIP_HW) ? true : false;
679 }
680 
681 static inline bool tc_skip_sw(u32 flags)
682 {
683 	return (flags & TCA_CLS_FLAGS_SKIP_SW) ? true : false;
684 }
685 
686 /* SKIP_HW and SKIP_SW are mutually exclusive flags. */
687 static inline bool tc_flags_valid(u32 flags)
688 {
689 	if (flags & ~(TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW |
690 		      TCA_CLS_FLAGS_VERBOSE))
691 		return false;
692 
693 	flags &= TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW;
694 	if (!(flags ^ (TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW)))
695 		return false;
696 
697 	return true;
698 }
699 
700 static inline bool tc_in_hw(u32 flags)
701 {
702 	return (flags & TCA_CLS_FLAGS_IN_HW) ? true : false;
703 }
704 
705 static inline void
706 tc_cls_common_offload_init(struct flow_cls_common_offload *cls_common,
707 			   const struct tcf_proto *tp, u32 flags,
708 			   struct netlink_ext_ack *extack)
709 {
710 	cls_common->chain_index = tp->chain->index;
711 	cls_common->protocol = tp->protocol;
712 	cls_common->prio = tp->prio >> 16;
713 	if (tc_skip_sw(flags) || flags & TCA_CLS_FLAGS_VERBOSE)
714 		cls_common->extack = extack;
715 }
716 
717 #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
718 static inline struct tc_skb_ext *tc_skb_ext_alloc(struct sk_buff *skb)
719 {
720 	struct tc_skb_ext *tc_skb_ext = skb_ext_add(skb, TC_SKB_EXT);
721 
722 	if (tc_skb_ext)
723 		memset(tc_skb_ext, 0, sizeof(*tc_skb_ext));
724 	return tc_skb_ext;
725 }
726 #endif
727 
728 enum tc_matchall_command {
729 	TC_CLSMATCHALL_REPLACE,
730 	TC_CLSMATCHALL_DESTROY,
731 	TC_CLSMATCHALL_STATS,
732 };
733 
734 struct tc_cls_matchall_offload {
735 	struct flow_cls_common_offload common;
736 	enum tc_matchall_command command;
737 	struct flow_rule *rule;
738 	struct flow_stats stats;
739 	unsigned long cookie;
740 };
741 
742 enum tc_clsbpf_command {
743 	TC_CLSBPF_OFFLOAD,
744 	TC_CLSBPF_STATS,
745 };
746 
747 struct tc_cls_bpf_offload {
748 	struct flow_cls_common_offload common;
749 	enum tc_clsbpf_command command;
750 	struct tcf_exts *exts;
751 	struct bpf_prog *prog;
752 	struct bpf_prog *oldprog;
753 	const char *name;
754 	bool exts_integrated;
755 };
756 
757 struct tc_mqprio_qopt_offload {
758 	/* struct tc_mqprio_qopt must always be the first element */
759 	struct tc_mqprio_qopt qopt;
760 	u16 mode;
761 	u16 shaper;
762 	u32 flags;
763 	u64 min_rate[TC_QOPT_MAX_QUEUE];
764 	u64 max_rate[TC_QOPT_MAX_QUEUE];
765 };
766 
767 /* This structure holds cookie structure that is passed from user
768  * to the kernel for actions and classifiers
769  */
770 struct tc_cookie {
771 	u8  *data;
772 	u32 len;
773 	struct rcu_head rcu;
774 };
775 
776 struct tc_qopt_offload_stats {
777 	struct gnet_stats_basic_sync *bstats;
778 	struct gnet_stats_queue *qstats;
779 };
780 
781 enum tc_mq_command {
782 	TC_MQ_CREATE,
783 	TC_MQ_DESTROY,
784 	TC_MQ_STATS,
785 	TC_MQ_GRAFT,
786 };
787 
788 struct tc_mq_opt_offload_graft_params {
789 	unsigned long queue;
790 	u32 child_handle;
791 };
792 
793 struct tc_mq_qopt_offload {
794 	enum tc_mq_command command;
795 	u32 handle;
796 	union {
797 		struct tc_qopt_offload_stats stats;
798 		struct tc_mq_opt_offload_graft_params graft_params;
799 	};
800 };
801 
802 enum tc_htb_command {
803 	/* Root */
804 	TC_HTB_CREATE, /* Initialize HTB offload. */
805 	TC_HTB_DESTROY, /* Destroy HTB offload. */
806 
807 	/* Classes */
808 	/* Allocate qid and create leaf. */
809 	TC_HTB_LEAF_ALLOC_QUEUE,
810 	/* Convert leaf to inner, preserve and return qid, create new leaf. */
811 	TC_HTB_LEAF_TO_INNER,
812 	/* Delete leaf, while siblings remain. */
813 	TC_HTB_LEAF_DEL,
814 	/* Delete leaf, convert parent to leaf, preserving qid. */
815 	TC_HTB_LEAF_DEL_LAST,
816 	/* TC_HTB_LEAF_DEL_LAST, but delete driver data on hardware errors. */
817 	TC_HTB_LEAF_DEL_LAST_FORCE,
818 	/* Modify parameters of a node. */
819 	TC_HTB_NODE_MODIFY,
820 
821 	/* Class qdisc */
822 	TC_HTB_LEAF_QUERY_QUEUE, /* Query qid by classid. */
823 };
824 
825 struct tc_htb_qopt_offload {
826 	struct netlink_ext_ack *extack;
827 	enum tc_htb_command command;
828 	u32 parent_classid;
829 	u16 classid;
830 	u16 qid;
831 	u64 rate;
832 	u64 ceil;
833 };
834 
835 #define TC_HTB_CLASSID_ROOT U32_MAX
836 
837 enum tc_red_command {
838 	TC_RED_REPLACE,
839 	TC_RED_DESTROY,
840 	TC_RED_STATS,
841 	TC_RED_XSTATS,
842 	TC_RED_GRAFT,
843 };
844 
845 struct tc_red_qopt_offload_params {
846 	u32 min;
847 	u32 max;
848 	u32 probability;
849 	u32 limit;
850 	bool is_ecn;
851 	bool is_harddrop;
852 	bool is_nodrop;
853 	struct gnet_stats_queue *qstats;
854 };
855 
856 struct tc_red_qopt_offload {
857 	enum tc_red_command command;
858 	u32 handle;
859 	u32 parent;
860 	union {
861 		struct tc_red_qopt_offload_params set;
862 		struct tc_qopt_offload_stats stats;
863 		struct red_stats *xstats;
864 		u32 child_handle;
865 	};
866 };
867 
868 enum tc_gred_command {
869 	TC_GRED_REPLACE,
870 	TC_GRED_DESTROY,
871 	TC_GRED_STATS,
872 };
873 
874 struct tc_gred_vq_qopt_offload_params {
875 	bool present;
876 	u32 limit;
877 	u32 prio;
878 	u32 min;
879 	u32 max;
880 	bool is_ecn;
881 	bool is_harddrop;
882 	u32 probability;
883 	/* Only need backlog, see struct tc_prio_qopt_offload_params */
884 	u32 *backlog;
885 };
886 
887 struct tc_gred_qopt_offload_params {
888 	bool grio_on;
889 	bool wred_on;
890 	unsigned int dp_cnt;
891 	unsigned int dp_def;
892 	struct gnet_stats_queue *qstats;
893 	struct tc_gred_vq_qopt_offload_params tab[MAX_DPs];
894 };
895 
896 struct tc_gred_qopt_offload_stats {
897 	struct gnet_stats_basic_sync bstats[MAX_DPs];
898 	struct gnet_stats_queue qstats[MAX_DPs];
899 	struct red_stats *xstats[MAX_DPs];
900 };
901 
902 struct tc_gred_qopt_offload {
903 	enum tc_gred_command command;
904 	u32 handle;
905 	u32 parent;
906 	union {
907 		struct tc_gred_qopt_offload_params set;
908 		struct tc_gred_qopt_offload_stats stats;
909 	};
910 };
911 
912 enum tc_prio_command {
913 	TC_PRIO_REPLACE,
914 	TC_PRIO_DESTROY,
915 	TC_PRIO_STATS,
916 	TC_PRIO_GRAFT,
917 };
918 
919 struct tc_prio_qopt_offload_params {
920 	int bands;
921 	u8 priomap[TC_PRIO_MAX + 1];
922 	/* At the point of un-offloading the Qdisc, the reported backlog and
923 	 * qlen need to be reduced by the portion that is in HW.
924 	 */
925 	struct gnet_stats_queue *qstats;
926 };
927 
928 struct tc_prio_qopt_offload_graft_params {
929 	u8 band;
930 	u32 child_handle;
931 };
932 
933 struct tc_prio_qopt_offload {
934 	enum tc_prio_command command;
935 	u32 handle;
936 	u32 parent;
937 	union {
938 		struct tc_prio_qopt_offload_params replace_params;
939 		struct tc_qopt_offload_stats stats;
940 		struct tc_prio_qopt_offload_graft_params graft_params;
941 	};
942 };
943 
944 enum tc_root_command {
945 	TC_ROOT_GRAFT,
946 };
947 
948 struct tc_root_qopt_offload {
949 	enum tc_root_command command;
950 	u32 handle;
951 	bool ingress;
952 };
953 
954 enum tc_ets_command {
955 	TC_ETS_REPLACE,
956 	TC_ETS_DESTROY,
957 	TC_ETS_STATS,
958 	TC_ETS_GRAFT,
959 };
960 
961 struct tc_ets_qopt_offload_replace_params {
962 	unsigned int bands;
963 	u8 priomap[TC_PRIO_MAX + 1];
964 	unsigned int quanta[TCQ_ETS_MAX_BANDS];	/* 0 for strict bands. */
965 	unsigned int weights[TCQ_ETS_MAX_BANDS];
966 	struct gnet_stats_queue *qstats;
967 };
968 
969 struct tc_ets_qopt_offload_graft_params {
970 	u8 band;
971 	u32 child_handle;
972 };
973 
974 struct tc_ets_qopt_offload {
975 	enum tc_ets_command command;
976 	u32 handle;
977 	u32 parent;
978 	union {
979 		struct tc_ets_qopt_offload_replace_params replace_params;
980 		struct tc_qopt_offload_stats stats;
981 		struct tc_ets_qopt_offload_graft_params graft_params;
982 	};
983 };
984 
985 enum tc_tbf_command {
986 	TC_TBF_REPLACE,
987 	TC_TBF_DESTROY,
988 	TC_TBF_STATS,
989 	TC_TBF_GRAFT,
990 };
991 
992 struct tc_tbf_qopt_offload_replace_params {
993 	struct psched_ratecfg rate;
994 	u32 max_size;
995 	struct gnet_stats_queue *qstats;
996 };
997 
998 struct tc_tbf_qopt_offload {
999 	enum tc_tbf_command command;
1000 	u32 handle;
1001 	u32 parent;
1002 	union {
1003 		struct tc_tbf_qopt_offload_replace_params replace_params;
1004 		struct tc_qopt_offload_stats stats;
1005 		u32 child_handle;
1006 	};
1007 };
1008 
1009 enum tc_fifo_command {
1010 	TC_FIFO_REPLACE,
1011 	TC_FIFO_DESTROY,
1012 	TC_FIFO_STATS,
1013 };
1014 
1015 struct tc_fifo_qopt_offload {
1016 	enum tc_fifo_command command;
1017 	u32 handle;
1018 	u32 parent;
1019 	union {
1020 		struct tc_qopt_offload_stats stats;
1021 	};
1022 };
1023 
1024 #endif
1025