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>
11 /* TC action not accessible from user space */
12 #define TC_ACT_REINSERT (TC_ACT_VALUE_MAX + 1)
14 /* Basic packet classifier frontend definitions. */
22 int (*fn)(struct tcf_proto *, void *node, struct tcf_walker *);
25 int register_tcf_proto_ops(struct tcf_proto_ops *ops);
26 int unregister_tcf_proto_ops(struct tcf_proto_ops *ops);
28 enum tcf_block_binder_type {
29 TCF_BLOCK_BINDER_TYPE_UNSPEC,
30 TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS,
31 TCF_BLOCK_BINDER_TYPE_CLSACT_EGRESS,
34 struct tcf_block_ext_info {
35 enum tcf_block_binder_type binder_type;
36 tcf_chain_head_change_t *chain_head_change;
37 void *chain_head_change_priv;
42 bool tcf_queue_work(struct rcu_work *rwork, work_func_t func);
45 struct tcf_chain *tcf_chain_get_by_act(struct tcf_block *block,
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, bool rtnl_held);
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);
63 static inline bool tcf_block_shared(struct tcf_block *block)
68 static inline struct Qdisc *tcf_block_q(struct tcf_block *block)
70 WARN_ON(tcf_block_shared(block));
74 void *tcf_block_cb_priv(struct tcf_block_cb *block_cb);
75 struct tcf_block_cb *tcf_block_cb_lookup(struct tcf_block *block,
76 tc_setup_cb_t *cb, void *cb_ident);
77 void tcf_block_cb_incref(struct tcf_block_cb *block_cb);
78 unsigned int tcf_block_cb_decref(struct tcf_block_cb *block_cb);
79 struct tcf_block_cb *__tcf_block_cb_register(struct tcf_block *block,
80 tc_setup_cb_t *cb, void *cb_ident,
82 struct netlink_ext_ack *extack);
83 int tcf_block_cb_register(struct tcf_block *block,
84 tc_setup_cb_t *cb, void *cb_ident,
85 void *cb_priv, struct netlink_ext_ack *extack);
86 void __tcf_block_cb_unregister(struct tcf_block *block,
87 struct tcf_block_cb *block_cb);
88 void tcf_block_cb_unregister(struct tcf_block *block,
89 tc_setup_cb_t *cb, void *cb_ident);
90 int __tc_indr_block_cb_register(struct net_device *dev, void *cb_priv,
91 tc_indr_block_bind_cb_t *cb, void *cb_ident);
92 int tc_indr_block_cb_register(struct net_device *dev, void *cb_priv,
93 tc_indr_block_bind_cb_t *cb, void *cb_ident);
94 void __tc_indr_block_cb_unregister(struct net_device *dev,
95 tc_indr_block_bind_cb_t *cb, void *cb_ident);
96 void tc_indr_block_cb_unregister(struct net_device *dev,
97 tc_indr_block_bind_cb_t *cb, void *cb_ident);
99 int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
100 struct tcf_result *res, bool compat_mode);
104 int tcf_block_get(struct tcf_block **p_block,
105 struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q,
106 struct netlink_ext_ack *extack)
112 int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q,
113 struct tcf_block_ext_info *ei,
114 struct netlink_ext_ack *extack)
119 static inline void tcf_block_put(struct tcf_block *block)
124 void tcf_block_put_ext(struct tcf_block *block, struct Qdisc *q,
125 struct tcf_block_ext_info *ei)
129 static inline struct Qdisc *tcf_block_q(struct tcf_block *block)
135 int tc_setup_cb_block_register(struct tcf_block *block, tc_setup_cb_t *cb,
142 void tc_setup_cb_block_unregister(struct tcf_block *block, tc_setup_cb_t *cb,
148 void *tcf_block_cb_priv(struct tcf_block_cb *block_cb)
154 struct tcf_block_cb *tcf_block_cb_lookup(struct tcf_block *block,
155 tc_setup_cb_t *cb, void *cb_ident)
161 void tcf_block_cb_incref(struct tcf_block_cb *block_cb)
166 unsigned int tcf_block_cb_decref(struct tcf_block_cb *block_cb)
172 struct tcf_block_cb *__tcf_block_cb_register(struct tcf_block *block,
173 tc_setup_cb_t *cb, void *cb_ident,
175 struct netlink_ext_ack *extack)
181 int tcf_block_cb_register(struct tcf_block *block,
182 tc_setup_cb_t *cb, void *cb_ident,
183 void *cb_priv, struct netlink_ext_ack *extack)
189 void __tcf_block_cb_unregister(struct tcf_block *block,
190 struct tcf_block_cb *block_cb)
195 void tcf_block_cb_unregister(struct tcf_block *block,
196 tc_setup_cb_t *cb, void *cb_ident)
201 int __tc_indr_block_cb_register(struct net_device *dev, void *cb_priv,
202 tc_indr_block_bind_cb_t *cb, void *cb_ident)
208 int tc_indr_block_cb_register(struct net_device *dev, void *cb_priv,
209 tc_indr_block_bind_cb_t *cb, void *cb_ident)
215 void __tc_indr_block_cb_unregister(struct net_device *dev,
216 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)
226 static inline int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
227 struct tcf_result *res, bool compat_mode)
229 return TC_ACT_UNSPEC;
233 static inline unsigned long
234 __cls_set_class(unsigned long *clp, unsigned long cl)
236 return xchg(clp, cl);
239 static inline unsigned long
240 cls_set_class(struct Qdisc *q, unsigned long *clp, unsigned long cl)
242 unsigned long old_cl;
245 old_cl = __cls_set_class(clp, cl);
251 tcf_bind_filter(struct tcf_proto *tp, struct tcf_result *r, unsigned long base)
253 struct Qdisc *q = tp->chain->block->q;
256 /* Check q as it is not set for shared blocks. In that case,
257 * setting class is not supported.
261 cl = q->ops->cl_ops->bind_tcf(q, base, r->classid);
262 cl = cls_set_class(q, &r->class, cl);
264 q->ops->cl_ops->unbind_tcf(q, cl);
268 tcf_unbind_filter(struct tcf_proto *tp, struct tcf_result *r)
270 struct Qdisc *q = tp->chain->block->q;
275 if ((cl = __cls_set_class(&r->class, 0)) != 0)
276 q->ops->cl_ops->unbind_tcf(q, cl);
280 #ifdef CONFIG_NET_CLS_ACT
281 __u32 type; /* for backward compat(TCA_OLD_COMPAT) */
283 struct tc_action **actions;
286 /* Map to export classifier specific extension TLV types to the
287 * generic extensions API. Unsupported extensions must be set to 0.
293 static inline int tcf_exts_init(struct tcf_exts *exts, struct net *net,
294 int action, int police)
296 #ifdef CONFIG_NET_CLS_ACT
298 exts->nr_actions = 0;
300 exts->actions = kcalloc(TCA_ACT_MAX_PRIO, sizeof(struct tc_action *),
305 exts->action = action;
306 exts->police = police;
310 /* Return false if the netns is being destroyed in cleanup_net(). Callers
311 * need to do cleanup synchronously in this case, otherwise may race with
312 * tc_action_net_exit(). Return true for other cases.
314 static inline bool tcf_exts_get_net(struct tcf_exts *exts)
316 #ifdef CONFIG_NET_CLS_ACT
317 exts->net = maybe_get_net(exts->net);
318 return exts->net != NULL;
324 static inline void tcf_exts_put_net(struct tcf_exts *exts)
326 #ifdef CONFIG_NET_CLS_ACT
332 #ifdef CONFIG_NET_CLS_ACT
333 #define tcf_exts_for_each_action(i, a, exts) \
334 for (i = 0; i < TCA_ACT_MAX_PRIO && ((a) = (exts)->actions[i]); i++)
336 #define tcf_exts_for_each_action(i, a, exts) \
337 for (; 0; (void)(i), (void)(a), (void)(exts))
341 tcf_exts_stats_update(const struct tcf_exts *exts,
342 u64 bytes, u64 packets, u64 lastuse)
344 #ifdef CONFIG_NET_CLS_ACT
349 for (i = 0; i < exts->nr_actions; i++) {
350 struct tc_action *a = exts->actions[i];
352 tcf_action_stats_update(a, bytes, packets, lastuse, true);
360 * tcf_exts_has_actions - check if at least one action is present
361 * @exts: tc filter extensions handle
363 * Returns true if at least one action is present.
365 static inline bool tcf_exts_has_actions(struct tcf_exts *exts)
367 #ifdef CONFIG_NET_CLS_ACT
368 return exts->nr_actions;
375 * tcf_exts_has_one_action - check if exactly one action is present
376 * @exts: tc filter extensions handle
378 * Returns true if exactly one action is present.
380 static inline bool tcf_exts_has_one_action(struct tcf_exts *exts)
382 #ifdef CONFIG_NET_CLS_ACT
383 return exts->nr_actions == 1;
389 static inline struct tc_action *tcf_exts_first_action(struct tcf_exts *exts)
391 #ifdef CONFIG_NET_CLS_ACT
392 return exts->actions[0];
399 * tcf_exts_exec - execute tc filter extensions
400 * @skb: socket buffer
401 * @exts: tc filter extensions handle
402 * @res: desired result
404 * Executes all configured extensions. Returns TC_ACT_OK on a normal execution,
405 * a negative number if the filter must be considered unmatched or
406 * a positive action code (TC_ACT_*) which must be returned to the
410 tcf_exts_exec(struct sk_buff *skb, struct tcf_exts *exts,
411 struct tcf_result *res)
413 #ifdef CONFIG_NET_CLS_ACT
414 return tcf_action_exec(skb, exts->actions, exts->nr_actions, res);
419 int tcf_exts_validate(struct net *net, struct tcf_proto *tp,
420 struct nlattr **tb, struct nlattr *rate_tlv,
421 struct tcf_exts *exts, bool ovr, bool rtnl_held,
422 struct netlink_ext_ack *extack);
423 void tcf_exts_destroy(struct tcf_exts *exts);
424 void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src);
425 int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts);
426 int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts);
429 * struct tcf_pkt_info - packet information
431 struct tcf_pkt_info {
436 #ifdef CONFIG_NET_EMATCH
438 struct tcf_ematch_ops;
441 * struct tcf_ematch - extended match (ematch)
443 * @matchid: identifier to allow userspace to reidentify a match
444 * @flags: flags specifying attributes and the relation to other matches
445 * @ops: the operations lookup table of the corresponding ematch module
446 * @datalen: length of the ematch specific configuration data
447 * @data: ematch specific data
450 struct tcf_ematch_ops * ops;
452 unsigned int datalen;
458 static inline int tcf_em_is_container(struct tcf_ematch *em)
463 static inline int tcf_em_is_simple(struct tcf_ematch *em)
465 return em->flags & TCF_EM_SIMPLE;
468 static inline int tcf_em_is_inverted(struct tcf_ematch *em)
470 return em->flags & TCF_EM_INVERT;
473 static inline int tcf_em_last_match(struct tcf_ematch *em)
475 return (em->flags & TCF_EM_REL_MASK) == TCF_EM_REL_END;
478 static inline int tcf_em_early_end(struct tcf_ematch *em, int result)
480 if (tcf_em_last_match(em))
483 if (result == 0 && em->flags & TCF_EM_REL_AND)
486 if (result != 0 && em->flags & TCF_EM_REL_OR)
493 * struct tcf_ematch_tree - ematch tree handle
495 * @hdr: ematch tree header supplied by userspace
496 * @matches: array of ematches
498 struct tcf_ematch_tree {
499 struct tcf_ematch_tree_hdr hdr;
500 struct tcf_ematch * matches;
505 * struct tcf_ematch_ops - ematch module operations
507 * @kind: identifier (kind) of this ematch module
508 * @datalen: length of expected configuration data (optional)
509 * @change: called during validation (optional)
510 * @match: called during ematch tree evaluation, must return 1/0
511 * @destroy: called during destroyage (optional)
512 * @dump: called during dumping process (optional)
513 * @owner: owner, must be set to THIS_MODULE
514 * @link: link to previous/next ematch module (internal use)
516 struct tcf_ematch_ops {
519 int (*change)(struct net *net, void *,
520 int, struct tcf_ematch *);
521 int (*match)(struct sk_buff *, struct tcf_ematch *,
522 struct tcf_pkt_info *);
523 void (*destroy)(struct tcf_ematch *);
524 int (*dump)(struct sk_buff *, struct tcf_ematch *);
525 struct module *owner;
526 struct list_head link;
529 int tcf_em_register(struct tcf_ematch_ops *);
530 void tcf_em_unregister(struct tcf_ematch_ops *);
531 int tcf_em_tree_validate(struct tcf_proto *, struct nlattr *,
532 struct tcf_ematch_tree *);
533 void tcf_em_tree_destroy(struct tcf_ematch_tree *);
534 int tcf_em_tree_dump(struct sk_buff *, struct tcf_ematch_tree *, int);
535 int __tcf_em_tree_match(struct sk_buff *, struct tcf_ematch_tree *,
536 struct tcf_pkt_info *);
539 * tcf_em_tree_match - evaulate an ematch tree
541 * @skb: socket buffer of the packet in question
542 * @tree: ematch tree to be used for evaluation
543 * @info: packet information examined by classifier
545 * This function matches @skb against the ematch tree in @tree by going
546 * through all ematches respecting their logic relations returning
547 * as soon as the result is obvious.
549 * Returns 1 if the ematch tree as-one matches, no ematches are configured
550 * or ematch is not enabled in the kernel, otherwise 0 is returned.
552 static inline int tcf_em_tree_match(struct sk_buff *skb,
553 struct tcf_ematch_tree *tree,
554 struct tcf_pkt_info *info)
556 if (tree->hdr.nmatches)
557 return __tcf_em_tree_match(skb, tree, info);
562 #define MODULE_ALIAS_TCF_EMATCH(kind) MODULE_ALIAS("ematch-kind-" __stringify(kind))
564 #else /* CONFIG_NET_EMATCH */
566 struct tcf_ematch_tree {
569 #define tcf_em_tree_validate(tp, tb, t) ((void)(t), 0)
570 #define tcf_em_tree_destroy(t) do { (void)(t); } while(0)
571 #define tcf_em_tree_dump(skb, t, tlv) (0)
572 #define tcf_em_tree_match(skb, t, info) ((void)(info), 1)
574 #endif /* CONFIG_NET_EMATCH */
576 static inline unsigned char * tcf_get_base_ptr(struct sk_buff *skb, int layer)
580 return skb_mac_header(skb);
581 case TCF_LAYER_NETWORK:
582 return skb_network_header(skb);
583 case TCF_LAYER_TRANSPORT:
584 return skb_transport_header(skb);
590 static inline int tcf_valid_offset(const struct sk_buff *skb,
591 const unsigned char *ptr, const int len)
593 return likely((ptr + len) <= skb_tail_pointer(skb) &&
595 (ptr <= (ptr + len)));
598 #ifdef CONFIG_NET_CLS_IND
599 #include <net/net_namespace.h>
602 tcf_change_indev(struct net *net, struct nlattr *indev_tlv,
603 struct netlink_ext_ack *extack)
605 char indev[IFNAMSIZ];
606 struct net_device *dev;
608 if (nla_strlcpy(indev, indev_tlv, IFNAMSIZ) >= IFNAMSIZ) {
609 NL_SET_ERR_MSG(extack, "Interface name too long");
612 dev = __dev_get_by_name(net, indev);
619 tcf_match_indev(struct sk_buff *skb, int ifindex)
625 return ifindex == skb->skb_iif;
627 #endif /* CONFIG_NET_CLS_IND */
629 int tc_setup_flow_action(struct flow_action *flow_action,
630 const struct tcf_exts *exts);
631 int tc_setup_cb_call(struct tcf_block *block, enum tc_setup_type type,
632 void *type_data, bool err_stop);
633 unsigned int tcf_exts_num_actions(struct tcf_exts *exts);
635 enum tc_block_command {
640 struct tc_block_offload {
641 enum tc_block_command command;
642 enum tcf_block_binder_type binder_type;
643 struct tcf_block *block;
644 struct netlink_ext_ack *extack;
647 struct tc_cls_common_offload {
651 struct netlink_ext_ack *extack;
654 struct tc_cls_u32_knode {
655 struct tcf_exts *exts;
656 struct tcf_result *res;
657 struct tc_u32_sel *sel;
665 struct tc_cls_u32_hnode {
668 unsigned int divisor;
671 enum tc_clsu32_command {
673 TC_CLSU32_REPLACE_KNODE,
674 TC_CLSU32_DELETE_KNODE,
676 TC_CLSU32_REPLACE_HNODE,
677 TC_CLSU32_DELETE_HNODE,
680 struct tc_cls_u32_offload {
681 struct tc_cls_common_offload common;
683 enum tc_clsu32_command command;
685 struct tc_cls_u32_knode knode;
686 struct tc_cls_u32_hnode hnode;
690 static inline bool tc_can_offload(const struct net_device *dev)
692 return dev->features & NETIF_F_HW_TC;
695 static inline bool tc_can_offload_extack(const struct net_device *dev,
696 struct netlink_ext_ack *extack)
698 bool can = tc_can_offload(dev);
701 NL_SET_ERR_MSG(extack, "TC offload is disabled on net device");
707 tc_cls_can_offload_and_chain0(const struct net_device *dev,
708 struct tc_cls_common_offload *common)
710 if (!tc_can_offload_extack(dev, common->extack))
712 if (common->chain_index) {
713 NL_SET_ERR_MSG(common->extack,
714 "Driver supports only offload of chain 0");
720 static inline bool tc_skip_hw(u32 flags)
722 return (flags & TCA_CLS_FLAGS_SKIP_HW) ? true : false;
725 static inline bool tc_skip_sw(u32 flags)
727 return (flags & TCA_CLS_FLAGS_SKIP_SW) ? true : false;
730 /* SKIP_HW and SKIP_SW are mutually exclusive flags. */
731 static inline bool tc_flags_valid(u32 flags)
733 if (flags & ~(TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW |
734 TCA_CLS_FLAGS_VERBOSE))
737 flags &= TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW;
738 if (!(flags ^ (TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW)))
744 static inline bool tc_in_hw(u32 flags)
746 return (flags & TCA_CLS_FLAGS_IN_HW) ? true : false;
750 tc_cls_common_offload_init(struct tc_cls_common_offload *cls_common,
751 const struct tcf_proto *tp, u32 flags,
752 struct netlink_ext_ack *extack)
754 cls_common->chain_index = tp->chain->index;
755 cls_common->protocol = tp->protocol;
756 cls_common->prio = tp->prio;
757 if (tc_skip_sw(flags) || flags & TCA_CLS_FLAGS_VERBOSE)
758 cls_common->extack = extack;
762 TC_CLSFLOWER_REPLACE,
763 TC_CLSFLOWER_DESTROY,
765 TC_CLSFLOWER_TMPLT_CREATE,
766 TC_CLSFLOWER_TMPLT_DESTROY,
769 struct tc_cls_flower_offload {
770 struct tc_cls_common_offload common;
771 enum tc_fl_command command;
772 unsigned long cookie;
773 struct flow_rule *rule;
774 struct flow_stats stats;
778 static inline struct flow_rule *
779 tc_cls_flower_offload_flow_rule(struct tc_cls_flower_offload *tc_flow_cmd)
781 return tc_flow_cmd->rule;
784 enum tc_matchall_command {
785 TC_CLSMATCHALL_REPLACE,
786 TC_CLSMATCHALL_DESTROY,
789 struct tc_cls_matchall_offload {
790 struct tc_cls_common_offload common;
791 enum tc_matchall_command command;
792 struct tcf_exts *exts;
793 unsigned long cookie;
796 enum tc_clsbpf_command {
801 struct tc_cls_bpf_offload {
802 struct tc_cls_common_offload common;
803 enum tc_clsbpf_command command;
804 struct tcf_exts *exts;
805 struct bpf_prog *prog;
806 struct bpf_prog *oldprog;
808 bool exts_integrated;
811 struct tc_mqprio_qopt_offload {
812 /* struct tc_mqprio_qopt must always be the first element */
813 struct tc_mqprio_qopt qopt;
817 u64 min_rate[TC_QOPT_MAX_QUEUE];
818 u64 max_rate[TC_QOPT_MAX_QUEUE];
821 /* This structure holds cookie structure that is passed from user
822 * to the kernel for actions and classifiers
830 struct tc_qopt_offload_stats {
831 struct gnet_stats_basic_packed *bstats;
832 struct gnet_stats_queue *qstats;
842 struct tc_mq_opt_offload_graft_params {
847 struct tc_mq_qopt_offload {
848 enum tc_mq_command command;
851 struct tc_qopt_offload_stats stats;
852 struct tc_mq_opt_offload_graft_params graft_params;
856 enum tc_red_command {
864 struct tc_red_qopt_offload_params {
871 struct gnet_stats_queue *qstats;
874 struct tc_red_qopt_offload {
875 enum tc_red_command command;
879 struct tc_red_qopt_offload_params set;
880 struct tc_qopt_offload_stats stats;
881 struct red_stats *xstats;
886 enum tc_gred_command {
892 struct tc_gred_vq_qopt_offload_params {
901 /* Only need backlog, see struct tc_prio_qopt_offload_params */
905 struct tc_gred_qopt_offload_params {
910 struct gnet_stats_queue *qstats;
911 struct tc_gred_vq_qopt_offload_params tab[MAX_DPs];
914 struct tc_gred_qopt_offload_stats {
915 struct gnet_stats_basic_packed bstats[MAX_DPs];
916 struct gnet_stats_queue qstats[MAX_DPs];
917 struct red_stats *xstats[MAX_DPs];
920 struct tc_gred_qopt_offload {
921 enum tc_gred_command command;
925 struct tc_gred_qopt_offload_params set;
926 struct tc_gred_qopt_offload_stats stats;
930 enum tc_prio_command {
937 struct tc_prio_qopt_offload_params {
939 u8 priomap[TC_PRIO_MAX + 1];
940 /* In case that a prio qdisc is offloaded and now is changed to a
941 * non-offloadedable config, it needs to update the backlog & qlen
942 * values to negate the HW backlog & qlen values (and only them).
944 struct gnet_stats_queue *qstats;
947 struct tc_prio_qopt_offload_graft_params {
952 struct tc_prio_qopt_offload {
953 enum tc_prio_command command;
957 struct tc_prio_qopt_offload_params replace_params;
958 struct tc_qopt_offload_stats stats;
959 struct tc_prio_qopt_offload_graft_params graft_params;
963 enum tc_root_command {
967 struct tc_root_qopt_offload {
968 enum tc_root_command command;