1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __NET_PKT_CLS_H
3 #define __NET_PKT_CLS_H
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/flow_offload.h>
10 #include <net/net_namespace.h>
12 /* TC action not accessible from user space */
13 #define TC_ACT_REINSERT (TC_ACT_VALUE_MAX + 1)
15 /* Basic packet classifier frontend definitions. */
23 int (*fn)(struct tcf_proto *, void *node, struct tcf_walker *);
26 int register_tcf_proto_ops(struct tcf_proto_ops *ops);
27 int unregister_tcf_proto_ops(struct tcf_proto_ops *ops);
29 enum tcf_block_binder_type {
30 TCF_BLOCK_BINDER_TYPE_UNSPEC,
31 TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS,
32 TCF_BLOCK_BINDER_TYPE_CLSACT_EGRESS,
35 struct tcf_block_ext_info {
36 enum tcf_block_binder_type binder_type;
37 tcf_chain_head_change_t *chain_head_change;
38 void *chain_head_change_priv;
43 bool tcf_queue_work(struct rcu_work *rwork, work_func_t func);
46 struct tcf_chain *tcf_chain_get_by_act(struct tcf_block *block,
48 void tcf_chain_put_by_act(struct tcf_chain *chain);
49 struct tcf_chain *tcf_get_next_chain(struct tcf_block *block,
50 struct tcf_chain *chain);
51 struct tcf_proto *tcf_get_next_proto(struct tcf_chain *chain,
52 struct tcf_proto *tp, bool rtnl_held);
53 void tcf_block_netif_keep_dst(struct tcf_block *block);
54 int tcf_block_get(struct tcf_block **p_block,
55 struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q,
56 struct netlink_ext_ack *extack);
57 int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q,
58 struct tcf_block_ext_info *ei,
59 struct netlink_ext_ack *extack);
60 void tcf_block_put(struct tcf_block *block);
61 void tcf_block_put_ext(struct tcf_block *block, struct Qdisc *q,
62 struct tcf_block_ext_info *ei);
64 static inline bool tcf_block_shared(struct tcf_block *block)
69 static inline struct Qdisc *tcf_block_q(struct tcf_block *block)
71 WARN_ON(tcf_block_shared(block));
75 void *tcf_block_cb_priv(struct tcf_block_cb *block_cb);
76 struct tcf_block_cb *tcf_block_cb_lookup(struct tcf_block *block,
77 tc_setup_cb_t *cb, void *cb_ident);
78 void tcf_block_cb_incref(struct tcf_block_cb *block_cb);
79 unsigned int tcf_block_cb_decref(struct tcf_block_cb *block_cb);
80 struct tcf_block_cb *__tcf_block_cb_register(struct tcf_block *block,
81 tc_setup_cb_t *cb, void *cb_ident,
83 struct netlink_ext_ack *extack);
84 int tcf_block_cb_register(struct tcf_block *block,
85 tc_setup_cb_t *cb, void *cb_ident,
86 void *cb_priv, struct netlink_ext_ack *extack);
87 void __tcf_block_cb_unregister(struct tcf_block *block,
88 struct tcf_block_cb *block_cb);
89 void tcf_block_cb_unregister(struct tcf_block *block,
90 tc_setup_cb_t *cb, void *cb_ident);
91 int __tc_indr_block_cb_register(struct net_device *dev, void *cb_priv,
92 tc_indr_block_bind_cb_t *cb, void *cb_ident);
93 int tc_indr_block_cb_register(struct net_device *dev, void *cb_priv,
94 tc_indr_block_bind_cb_t *cb, void *cb_ident);
95 void __tc_indr_block_cb_unregister(struct net_device *dev,
96 tc_indr_block_bind_cb_t *cb, void *cb_ident);
97 void tc_indr_block_cb_unregister(struct net_device *dev,
98 tc_indr_block_bind_cb_t *cb, void *cb_ident);
100 int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
101 struct tcf_result *res, bool compat_mode);
104 static inline bool tcf_block_shared(struct tcf_block *block)
110 int tcf_block_get(struct tcf_block **p_block,
111 struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q,
112 struct netlink_ext_ack *extack)
118 int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q,
119 struct tcf_block_ext_info *ei,
120 struct netlink_ext_ack *extack)
125 static inline void tcf_block_put(struct tcf_block *block)
130 void tcf_block_put_ext(struct tcf_block *block, struct Qdisc *q,
131 struct tcf_block_ext_info *ei)
135 static inline struct Qdisc *tcf_block_q(struct tcf_block *block)
141 int tc_setup_cb_block_register(struct tcf_block *block, tc_setup_cb_t *cb,
148 void tc_setup_cb_block_unregister(struct tcf_block *block, tc_setup_cb_t *cb,
154 void *tcf_block_cb_priv(struct tcf_block_cb *block_cb)
160 struct tcf_block_cb *tcf_block_cb_lookup(struct tcf_block *block,
161 tc_setup_cb_t *cb, void *cb_ident)
167 void tcf_block_cb_incref(struct tcf_block_cb *block_cb)
172 unsigned int tcf_block_cb_decref(struct tcf_block_cb *block_cb)
178 struct tcf_block_cb *__tcf_block_cb_register(struct tcf_block *block,
179 tc_setup_cb_t *cb, void *cb_ident,
181 struct netlink_ext_ack *extack)
187 int tcf_block_cb_register(struct tcf_block *block,
188 tc_setup_cb_t *cb, void *cb_ident,
189 void *cb_priv, struct netlink_ext_ack *extack)
195 void __tcf_block_cb_unregister(struct tcf_block *block,
196 struct tcf_block_cb *block_cb)
201 void tcf_block_cb_unregister(struct tcf_block *block,
202 tc_setup_cb_t *cb, void *cb_ident)
207 int __tc_indr_block_cb_register(struct net_device *dev, void *cb_priv,
208 tc_indr_block_bind_cb_t *cb, void *cb_ident)
214 int tc_indr_block_cb_register(struct net_device *dev, void *cb_priv,
215 tc_indr_block_bind_cb_t *cb, void *cb_ident)
221 void __tc_indr_block_cb_unregister(struct net_device *dev,
222 tc_indr_block_bind_cb_t *cb, void *cb_ident)
227 void tc_indr_block_cb_unregister(struct net_device *dev,
228 tc_indr_block_bind_cb_t *cb, void *cb_ident)
232 static inline int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
233 struct tcf_result *res, bool compat_mode)
235 return TC_ACT_UNSPEC;
239 static inline unsigned long
240 __cls_set_class(unsigned long *clp, unsigned long cl)
242 return xchg(clp, cl);
245 static inline unsigned long
246 cls_set_class(struct Qdisc *q, unsigned long *clp, unsigned long cl)
248 unsigned long old_cl;
251 old_cl = __cls_set_class(clp, cl);
257 tcf_bind_filter(struct tcf_proto *tp, struct tcf_result *r, unsigned long base)
259 struct Qdisc *q = tp->chain->block->q;
262 /* Check q as it is not set for shared blocks. In that case,
263 * setting class is not supported.
267 cl = q->ops->cl_ops->bind_tcf(q, base, r->classid);
268 cl = cls_set_class(q, &r->class, cl);
270 q->ops->cl_ops->unbind_tcf(q, cl);
274 tcf_unbind_filter(struct tcf_proto *tp, struct tcf_result *r)
276 struct Qdisc *q = tp->chain->block->q;
281 if ((cl = __cls_set_class(&r->class, 0)) != 0)
282 q->ops->cl_ops->unbind_tcf(q, cl);
286 #ifdef CONFIG_NET_CLS_ACT
287 __u32 type; /* for backward compat(TCA_OLD_COMPAT) */
289 struct tc_action **actions;
292 /* Map to export classifier specific extension TLV types to the
293 * generic extensions API. Unsupported extensions must be set to 0.
299 static inline int tcf_exts_init(struct tcf_exts *exts, struct net *net,
300 int action, int police)
302 #ifdef CONFIG_NET_CLS_ACT
304 exts->nr_actions = 0;
306 exts->actions = kcalloc(TCA_ACT_MAX_PRIO, sizeof(struct tc_action *),
311 exts->action = action;
312 exts->police = police;
316 /* Return false if the netns is being destroyed in cleanup_net(). Callers
317 * need to do cleanup synchronously in this case, otherwise may race with
318 * tc_action_net_exit(). Return true for other cases.
320 static inline bool tcf_exts_get_net(struct tcf_exts *exts)
322 #ifdef CONFIG_NET_CLS_ACT
323 exts->net = maybe_get_net(exts->net);
324 return exts->net != NULL;
330 static inline void tcf_exts_put_net(struct tcf_exts *exts)
332 #ifdef CONFIG_NET_CLS_ACT
338 #ifdef CONFIG_NET_CLS_ACT
339 #define tcf_exts_for_each_action(i, a, exts) \
340 for (i = 0; i < TCA_ACT_MAX_PRIO && ((a) = (exts)->actions[i]); i++)
342 #define tcf_exts_for_each_action(i, a, exts) \
343 for (; 0; (void)(i), (void)(a), (void)(exts))
347 tcf_exts_stats_update(const struct tcf_exts *exts,
348 u64 bytes, u64 packets, u64 lastuse)
350 #ifdef CONFIG_NET_CLS_ACT
355 for (i = 0; i < exts->nr_actions; i++) {
356 struct tc_action *a = exts->actions[i];
358 tcf_action_stats_update(a, bytes, packets, lastuse, true);
366 * tcf_exts_has_actions - check if at least one action is present
367 * @exts: tc filter extensions handle
369 * Returns true if at least one action is present.
371 static inline bool tcf_exts_has_actions(struct tcf_exts *exts)
373 #ifdef CONFIG_NET_CLS_ACT
374 return exts->nr_actions;
381 * tcf_exts_exec - execute tc filter extensions
382 * @skb: socket buffer
383 * @exts: tc filter extensions handle
384 * @res: desired result
386 * Executes all configured extensions. Returns TC_ACT_OK on a normal execution,
387 * a negative number if the filter must be considered unmatched or
388 * a positive action code (TC_ACT_*) which must be returned to the
392 tcf_exts_exec(struct sk_buff *skb, struct tcf_exts *exts,
393 struct tcf_result *res)
395 #ifdef CONFIG_NET_CLS_ACT
396 return tcf_action_exec(skb, exts->actions, exts->nr_actions, res);
401 int tcf_exts_validate(struct net *net, struct tcf_proto *tp,
402 struct nlattr **tb, struct nlattr *rate_tlv,
403 struct tcf_exts *exts, bool ovr, bool rtnl_held,
404 struct netlink_ext_ack *extack);
405 void tcf_exts_destroy(struct tcf_exts *exts);
406 void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src);
407 int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts);
408 int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts);
411 * struct tcf_pkt_info - packet information
413 struct tcf_pkt_info {
418 #ifdef CONFIG_NET_EMATCH
420 struct tcf_ematch_ops;
423 * struct tcf_ematch - extended match (ematch)
425 * @matchid: identifier to allow userspace to reidentify a match
426 * @flags: flags specifying attributes and the relation to other matches
427 * @ops: the operations lookup table of the corresponding ematch module
428 * @datalen: length of the ematch specific configuration data
429 * @data: ematch specific data
432 struct tcf_ematch_ops * ops;
434 unsigned int datalen;
440 static inline int tcf_em_is_container(struct tcf_ematch *em)
445 static inline int tcf_em_is_simple(struct tcf_ematch *em)
447 return em->flags & TCF_EM_SIMPLE;
450 static inline int tcf_em_is_inverted(struct tcf_ematch *em)
452 return em->flags & TCF_EM_INVERT;
455 static inline int tcf_em_last_match(struct tcf_ematch *em)
457 return (em->flags & TCF_EM_REL_MASK) == TCF_EM_REL_END;
460 static inline int tcf_em_early_end(struct tcf_ematch *em, int result)
462 if (tcf_em_last_match(em))
465 if (result == 0 && em->flags & TCF_EM_REL_AND)
468 if (result != 0 && em->flags & TCF_EM_REL_OR)
475 * struct tcf_ematch_tree - ematch tree handle
477 * @hdr: ematch tree header supplied by userspace
478 * @matches: array of ematches
480 struct tcf_ematch_tree {
481 struct tcf_ematch_tree_hdr hdr;
482 struct tcf_ematch * matches;
487 * struct tcf_ematch_ops - ematch module operations
489 * @kind: identifier (kind) of this ematch module
490 * @datalen: length of expected configuration data (optional)
491 * @change: called during validation (optional)
492 * @match: called during ematch tree evaluation, must return 1/0
493 * @destroy: called during destroyage (optional)
494 * @dump: called during dumping process (optional)
495 * @owner: owner, must be set to THIS_MODULE
496 * @link: link to previous/next ematch module (internal use)
498 struct tcf_ematch_ops {
501 int (*change)(struct net *net, void *,
502 int, struct tcf_ematch *);
503 int (*match)(struct sk_buff *, struct tcf_ematch *,
504 struct tcf_pkt_info *);
505 void (*destroy)(struct tcf_ematch *);
506 int (*dump)(struct sk_buff *, struct tcf_ematch *);
507 struct module *owner;
508 struct list_head link;
511 int tcf_em_register(struct tcf_ematch_ops *);
512 void tcf_em_unregister(struct tcf_ematch_ops *);
513 int tcf_em_tree_validate(struct tcf_proto *, struct nlattr *,
514 struct tcf_ematch_tree *);
515 void tcf_em_tree_destroy(struct tcf_ematch_tree *);
516 int tcf_em_tree_dump(struct sk_buff *, struct tcf_ematch_tree *, int);
517 int __tcf_em_tree_match(struct sk_buff *, struct tcf_ematch_tree *,
518 struct tcf_pkt_info *);
521 * tcf_em_tree_match - evaulate an ematch tree
523 * @skb: socket buffer of the packet in question
524 * @tree: ematch tree to be used for evaluation
525 * @info: packet information examined by classifier
527 * This function matches @skb against the ematch tree in @tree by going
528 * through all ematches respecting their logic relations returning
529 * as soon as the result is obvious.
531 * Returns 1 if the ematch tree as-one matches, no ematches are configured
532 * or ematch is not enabled in the kernel, otherwise 0 is returned.
534 static inline int tcf_em_tree_match(struct sk_buff *skb,
535 struct tcf_ematch_tree *tree,
536 struct tcf_pkt_info *info)
538 if (tree->hdr.nmatches)
539 return __tcf_em_tree_match(skb, tree, info);
544 #define MODULE_ALIAS_TCF_EMATCH(kind) MODULE_ALIAS("ematch-kind-" __stringify(kind))
546 #else /* CONFIG_NET_EMATCH */
548 struct tcf_ematch_tree {
551 #define tcf_em_tree_validate(tp, tb, t) ((void)(t), 0)
552 #define tcf_em_tree_destroy(t) do { (void)(t); } while(0)
553 #define tcf_em_tree_dump(skb, t, tlv) (0)
554 #define tcf_em_tree_match(skb, t, info) ((void)(info), 1)
556 #endif /* CONFIG_NET_EMATCH */
558 static inline unsigned char * tcf_get_base_ptr(struct sk_buff *skb, int layer)
562 return skb_mac_header(skb);
563 case TCF_LAYER_NETWORK:
564 return skb_network_header(skb);
565 case TCF_LAYER_TRANSPORT:
566 return skb_transport_header(skb);
572 static inline int tcf_valid_offset(const struct sk_buff *skb,
573 const unsigned char *ptr, const int len)
575 return likely((ptr + len) <= skb_tail_pointer(skb) &&
577 (ptr <= (ptr + len)));
581 tcf_change_indev(struct net *net, struct nlattr *indev_tlv,
582 struct netlink_ext_ack *extack)
584 char indev[IFNAMSIZ];
585 struct net_device *dev;
587 if (nla_strlcpy(indev, indev_tlv, IFNAMSIZ) >= IFNAMSIZ) {
588 NL_SET_ERR_MSG(extack, "Interface name too long");
591 dev = __dev_get_by_name(net, indev);
598 tcf_match_indev(struct sk_buff *skb, int ifindex)
604 return ifindex == skb->skb_iif;
607 int tc_setup_flow_action(struct flow_action *flow_action,
608 const struct tcf_exts *exts);
609 int tc_setup_cb_call(struct tcf_block *block, enum tc_setup_type type,
610 void *type_data, bool err_stop);
611 unsigned int tcf_exts_num_actions(struct tcf_exts *exts);
613 enum tc_block_command {
618 struct tc_block_offload {
619 enum tc_block_command command;
620 enum tcf_block_binder_type binder_type;
621 struct tcf_block *block;
622 struct netlink_ext_ack *extack;
625 struct tc_cls_common_offload {
629 struct netlink_ext_ack *extack;
632 struct tc_cls_u32_knode {
633 struct tcf_exts *exts;
634 struct tcf_result *res;
635 struct tc_u32_sel *sel;
643 struct tc_cls_u32_hnode {
646 unsigned int divisor;
649 enum tc_clsu32_command {
651 TC_CLSU32_REPLACE_KNODE,
652 TC_CLSU32_DELETE_KNODE,
654 TC_CLSU32_REPLACE_HNODE,
655 TC_CLSU32_DELETE_HNODE,
658 struct tc_cls_u32_offload {
659 struct tc_cls_common_offload common;
661 enum tc_clsu32_command command;
663 struct tc_cls_u32_knode knode;
664 struct tc_cls_u32_hnode hnode;
668 static inline bool tc_can_offload(const struct net_device *dev)
670 return dev->features & NETIF_F_HW_TC;
673 static inline bool tc_can_offload_extack(const struct net_device *dev,
674 struct netlink_ext_ack *extack)
676 bool can = tc_can_offload(dev);
679 NL_SET_ERR_MSG(extack, "TC offload is disabled on net device");
685 tc_cls_can_offload_and_chain0(const struct net_device *dev,
686 struct tc_cls_common_offload *common)
688 if (!tc_can_offload_extack(dev, common->extack))
690 if (common->chain_index) {
691 NL_SET_ERR_MSG(common->extack,
692 "Driver supports only offload of chain 0");
698 static inline bool tc_skip_hw(u32 flags)
700 return (flags & TCA_CLS_FLAGS_SKIP_HW) ? true : false;
703 static inline bool tc_skip_sw(u32 flags)
705 return (flags & TCA_CLS_FLAGS_SKIP_SW) ? true : false;
708 /* SKIP_HW and SKIP_SW are mutually exclusive flags. */
709 static inline bool tc_flags_valid(u32 flags)
711 if (flags & ~(TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW |
712 TCA_CLS_FLAGS_VERBOSE))
715 flags &= TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW;
716 if (!(flags ^ (TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW)))
722 static inline bool tc_in_hw(u32 flags)
724 return (flags & TCA_CLS_FLAGS_IN_HW) ? true : false;
728 tc_cls_common_offload_init(struct tc_cls_common_offload *cls_common,
729 const struct tcf_proto *tp, u32 flags,
730 struct netlink_ext_ack *extack)
732 cls_common->chain_index = tp->chain->index;
733 cls_common->protocol = tp->protocol;
734 cls_common->prio = tp->prio;
735 if (tc_skip_sw(flags) || flags & TCA_CLS_FLAGS_VERBOSE)
736 cls_common->extack = extack;
740 TC_CLSFLOWER_REPLACE,
741 TC_CLSFLOWER_DESTROY,
743 TC_CLSFLOWER_TMPLT_CREATE,
744 TC_CLSFLOWER_TMPLT_DESTROY,
747 struct tc_cls_flower_offload {
748 struct tc_cls_common_offload common;
749 enum tc_fl_command command;
750 unsigned long cookie;
751 struct flow_rule *rule;
752 struct flow_stats stats;
756 static inline struct flow_rule *
757 tc_cls_flower_offload_flow_rule(struct tc_cls_flower_offload *tc_flow_cmd)
759 return tc_flow_cmd->rule;
762 enum tc_matchall_command {
763 TC_CLSMATCHALL_REPLACE,
764 TC_CLSMATCHALL_DESTROY,
765 TC_CLSMATCHALL_STATS,
768 struct tc_cls_matchall_offload {
769 struct tc_cls_common_offload common;
770 enum tc_matchall_command command;
771 struct flow_rule *rule;
772 struct flow_stats stats;
773 unsigned long cookie;
776 enum tc_clsbpf_command {
781 struct tc_cls_bpf_offload {
782 struct tc_cls_common_offload common;
783 enum tc_clsbpf_command command;
784 struct tcf_exts *exts;
785 struct bpf_prog *prog;
786 struct bpf_prog *oldprog;
788 bool exts_integrated;
791 struct tc_mqprio_qopt_offload {
792 /* struct tc_mqprio_qopt must always be the first element */
793 struct tc_mqprio_qopt qopt;
797 u64 min_rate[TC_QOPT_MAX_QUEUE];
798 u64 max_rate[TC_QOPT_MAX_QUEUE];
801 /* This structure holds cookie structure that is passed from user
802 * to the kernel for actions and classifiers
810 struct tc_qopt_offload_stats {
811 struct gnet_stats_basic_packed *bstats;
812 struct gnet_stats_queue *qstats;
822 struct tc_mq_opt_offload_graft_params {
827 struct tc_mq_qopt_offload {
828 enum tc_mq_command command;
831 struct tc_qopt_offload_stats stats;
832 struct tc_mq_opt_offload_graft_params graft_params;
836 enum tc_red_command {
844 struct tc_red_qopt_offload_params {
851 struct gnet_stats_queue *qstats;
854 struct tc_red_qopt_offload {
855 enum tc_red_command command;
859 struct tc_red_qopt_offload_params set;
860 struct tc_qopt_offload_stats stats;
861 struct red_stats *xstats;
866 enum tc_gred_command {
872 struct tc_gred_vq_qopt_offload_params {
881 /* Only need backlog, see struct tc_prio_qopt_offload_params */
885 struct tc_gred_qopt_offload_params {
890 struct gnet_stats_queue *qstats;
891 struct tc_gred_vq_qopt_offload_params tab[MAX_DPs];
894 struct tc_gred_qopt_offload_stats {
895 struct gnet_stats_basic_packed bstats[MAX_DPs];
896 struct gnet_stats_queue qstats[MAX_DPs];
897 struct red_stats *xstats[MAX_DPs];
900 struct tc_gred_qopt_offload {
901 enum tc_gred_command command;
905 struct tc_gred_qopt_offload_params set;
906 struct tc_gred_qopt_offload_stats stats;
910 enum tc_prio_command {
917 struct tc_prio_qopt_offload_params {
919 u8 priomap[TC_PRIO_MAX + 1];
920 /* In case that a prio qdisc is offloaded and now is changed to a
921 * non-offloadedable config, it needs to update the backlog & qlen
922 * values to negate the HW backlog & qlen values (and only them).
924 struct gnet_stats_queue *qstats;
927 struct tc_prio_qopt_offload_graft_params {
932 struct tc_prio_qopt_offload {
933 enum tc_prio_command command;
937 struct tc_prio_qopt_offload_params replace_params;
938 struct tc_qopt_offload_stats stats;
939 struct tc_prio_qopt_offload_graft_params graft_params;
943 enum tc_root_command {
947 struct tc_root_qopt_offload {
948 enum tc_root_command command;