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>
10 /* TC action not accessible from user space */
11 #define TC_ACT_REINSERT (TC_ACT_VALUE_MAX + 1)
13 /* Basic packet classifier frontend definitions. */
20 int (*fn)(struct tcf_proto *, void *node, struct tcf_walker *);
23 int register_tcf_proto_ops(struct tcf_proto_ops *ops);
24 int unregister_tcf_proto_ops(struct tcf_proto_ops *ops);
26 enum tcf_block_binder_type {
27 TCF_BLOCK_BINDER_TYPE_UNSPEC,
28 TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS,
29 TCF_BLOCK_BINDER_TYPE_CLSACT_EGRESS,
32 struct tcf_block_ext_info {
33 enum tcf_block_binder_type binder_type;
34 tcf_chain_head_change_t *chain_head_change;
35 void *chain_head_change_priv;
40 bool tcf_queue_work(struct rcu_work *rwork, work_func_t func);
43 struct tcf_chain *tcf_chain_get_by_act(struct tcf_block *block,
45 void tcf_chain_put_by_act(struct tcf_chain *chain);
46 void tcf_block_netif_keep_dst(struct tcf_block *block);
47 int tcf_block_get(struct tcf_block **p_block,
48 struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q,
49 struct netlink_ext_ack *extack);
50 int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q,
51 struct tcf_block_ext_info *ei,
52 struct netlink_ext_ack *extack);
53 void tcf_block_put(struct tcf_block *block);
54 void tcf_block_put_ext(struct tcf_block *block, struct Qdisc *q,
55 struct tcf_block_ext_info *ei);
57 static inline bool tcf_block_shared(struct tcf_block *block)
62 static inline struct Qdisc *tcf_block_q(struct tcf_block *block)
64 WARN_ON(tcf_block_shared(block));
68 void *tcf_block_cb_priv(struct tcf_block_cb *block_cb);
69 struct tcf_block_cb *tcf_block_cb_lookup(struct tcf_block *block,
70 tc_setup_cb_t *cb, void *cb_ident);
71 void tcf_block_cb_incref(struct tcf_block_cb *block_cb);
72 unsigned int tcf_block_cb_decref(struct tcf_block_cb *block_cb);
73 struct tcf_block_cb *__tcf_block_cb_register(struct tcf_block *block,
74 tc_setup_cb_t *cb, void *cb_ident,
76 struct netlink_ext_ack *extack);
77 int tcf_block_cb_register(struct tcf_block *block,
78 tc_setup_cb_t *cb, void *cb_ident,
79 void *cb_priv, struct netlink_ext_ack *extack);
80 void __tcf_block_cb_unregister(struct tcf_block *block,
81 struct tcf_block_cb *block_cb);
82 void tcf_block_cb_unregister(struct tcf_block *block,
83 tc_setup_cb_t *cb, void *cb_ident);
84 int __tc_indr_block_cb_register(struct net_device *dev, void *cb_priv,
85 tc_indr_block_bind_cb_t *cb, void *cb_ident);
86 int tc_indr_block_cb_register(struct net_device *dev, void *cb_priv,
87 tc_indr_block_bind_cb_t *cb, void *cb_ident);
88 void __tc_indr_block_cb_unregister(struct net_device *dev,
89 tc_indr_block_bind_cb_t *cb, void *cb_ident);
90 void tc_indr_block_cb_unregister(struct net_device *dev,
91 tc_indr_block_bind_cb_t *cb, void *cb_ident);
93 int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
94 struct tcf_result *res, bool compat_mode);
98 int tcf_block_get(struct tcf_block **p_block,
99 struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q,
100 struct netlink_ext_ack *extack)
106 int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q,
107 struct tcf_block_ext_info *ei,
108 struct netlink_ext_ack *extack)
113 static inline void tcf_block_put(struct tcf_block *block)
118 void tcf_block_put_ext(struct tcf_block *block, struct Qdisc *q,
119 struct tcf_block_ext_info *ei)
123 static inline struct Qdisc *tcf_block_q(struct tcf_block *block)
129 int tc_setup_cb_block_register(struct tcf_block *block, tc_setup_cb_t *cb,
136 void tc_setup_cb_block_unregister(struct tcf_block *block, tc_setup_cb_t *cb,
142 void *tcf_block_cb_priv(struct tcf_block_cb *block_cb)
148 struct tcf_block_cb *tcf_block_cb_lookup(struct tcf_block *block,
149 tc_setup_cb_t *cb, void *cb_ident)
155 void tcf_block_cb_incref(struct tcf_block_cb *block_cb)
160 unsigned int tcf_block_cb_decref(struct tcf_block_cb *block_cb)
166 struct tcf_block_cb *__tcf_block_cb_register(struct tcf_block *block,
167 tc_setup_cb_t *cb, void *cb_ident,
169 struct netlink_ext_ack *extack)
175 int tcf_block_cb_register(struct tcf_block *block,
176 tc_setup_cb_t *cb, void *cb_ident,
177 void *cb_priv, struct netlink_ext_ack *extack)
183 void __tcf_block_cb_unregister(struct tcf_block *block,
184 struct tcf_block_cb *block_cb)
189 void tcf_block_cb_unregister(struct tcf_block *block,
190 tc_setup_cb_t *cb, void *cb_ident)
195 int __tc_indr_block_cb_register(struct net_device *dev, void *cb_priv,
196 tc_indr_block_bind_cb_t *cb, void *cb_ident)
202 int tc_indr_block_cb_register(struct net_device *dev, void *cb_priv,
203 tc_indr_block_bind_cb_t *cb, void *cb_ident)
209 void __tc_indr_block_cb_unregister(struct net_device *dev,
210 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)
220 static inline int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
221 struct tcf_result *res, bool compat_mode)
223 return TC_ACT_UNSPEC;
227 static inline unsigned long
228 __cls_set_class(unsigned long *clp, unsigned long cl)
230 return xchg(clp, cl);
233 static inline unsigned long
234 cls_set_class(struct Qdisc *q, unsigned long *clp, unsigned long cl)
236 unsigned long old_cl;
239 old_cl = __cls_set_class(clp, cl);
245 tcf_bind_filter(struct tcf_proto *tp, struct tcf_result *r, unsigned long base)
247 struct Qdisc *q = tp->chain->block->q;
250 /* Check q as it is not set for shared blocks. In that case,
251 * setting class is not supported.
255 cl = q->ops->cl_ops->bind_tcf(q, base, r->classid);
256 cl = cls_set_class(q, &r->class, cl);
258 q->ops->cl_ops->unbind_tcf(q, cl);
262 tcf_unbind_filter(struct tcf_proto *tp, struct tcf_result *r)
264 struct Qdisc *q = tp->chain->block->q;
269 if ((cl = __cls_set_class(&r->class, 0)) != 0)
270 q->ops->cl_ops->unbind_tcf(q, cl);
274 #ifdef CONFIG_NET_CLS_ACT
275 __u32 type; /* for backward compat(TCA_OLD_COMPAT) */
277 struct tc_action **actions;
280 /* Map to export classifier specific extension TLV types to the
281 * generic extensions API. Unsupported extensions must be set to 0.
287 static inline int tcf_exts_init(struct tcf_exts *exts, int action, int police)
289 #ifdef CONFIG_NET_CLS_ACT
291 exts->nr_actions = 0;
293 exts->actions = kcalloc(TCA_ACT_MAX_PRIO, sizeof(struct tc_action *),
298 exts->action = action;
299 exts->police = police;
303 /* Return false if the netns is being destroyed in cleanup_net(). Callers
304 * need to do cleanup synchronously in this case, otherwise may race with
305 * tc_action_net_exit(). Return true for other cases.
307 static inline bool tcf_exts_get_net(struct tcf_exts *exts)
309 #ifdef CONFIG_NET_CLS_ACT
310 exts->net = maybe_get_net(exts->net);
311 return exts->net != NULL;
317 static inline void tcf_exts_put_net(struct tcf_exts *exts)
319 #ifdef CONFIG_NET_CLS_ACT
325 #ifdef CONFIG_NET_CLS_ACT
326 #define tcf_exts_for_each_action(i, a, exts) \
327 for (i = 0; i < TCA_ACT_MAX_PRIO && ((a) = (exts)->actions[i]); i++)
329 #define tcf_exts_for_each_action(i, a, exts) \
330 for (; 0; (void)(i), (void)(a), (void)(exts))
334 tcf_exts_stats_update(const struct tcf_exts *exts,
335 u64 bytes, u64 packets, u64 lastuse)
337 #ifdef CONFIG_NET_CLS_ACT
342 for (i = 0; i < exts->nr_actions; i++) {
343 struct tc_action *a = exts->actions[i];
345 tcf_action_stats_update(a, bytes, packets, lastuse, true);
353 * tcf_exts_has_actions - check if at least one action is present
354 * @exts: tc filter extensions handle
356 * Returns true if at least one action is present.
358 static inline bool tcf_exts_has_actions(struct tcf_exts *exts)
360 #ifdef CONFIG_NET_CLS_ACT
361 return exts->nr_actions;
368 * tcf_exts_has_one_action - check if exactly one action is present
369 * @exts: tc filter extensions handle
371 * Returns true if exactly one action is present.
373 static inline bool tcf_exts_has_one_action(struct tcf_exts *exts)
375 #ifdef CONFIG_NET_CLS_ACT
376 return exts->nr_actions == 1;
382 static inline struct tc_action *tcf_exts_first_action(struct tcf_exts *exts)
384 #ifdef CONFIG_NET_CLS_ACT
385 return exts->actions[0];
392 * tcf_exts_exec - execute tc filter extensions
393 * @skb: socket buffer
394 * @exts: tc filter extensions handle
395 * @res: desired result
397 * Executes all configured extensions. Returns TC_ACT_OK on a normal execution,
398 * a negative number if the filter must be considered unmatched or
399 * a positive action code (TC_ACT_*) which must be returned to the
403 tcf_exts_exec(struct sk_buff *skb, struct tcf_exts *exts,
404 struct tcf_result *res)
406 #ifdef CONFIG_NET_CLS_ACT
407 return tcf_action_exec(skb, exts->actions, exts->nr_actions, res);
412 int tcf_exts_validate(struct net *net, struct tcf_proto *tp,
413 struct nlattr **tb, struct nlattr *rate_tlv,
414 struct tcf_exts *exts, bool ovr,
415 struct netlink_ext_ack *extack);
416 void tcf_exts_destroy(struct tcf_exts *exts);
417 void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src);
418 int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts);
419 int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts);
422 * struct tcf_pkt_info - packet information
424 struct tcf_pkt_info {
429 #ifdef CONFIG_NET_EMATCH
431 struct tcf_ematch_ops;
434 * struct tcf_ematch - extended match (ematch)
436 * @matchid: identifier to allow userspace to reidentify a match
437 * @flags: flags specifying attributes and the relation to other matches
438 * @ops: the operations lookup table of the corresponding ematch module
439 * @datalen: length of the ematch specific configuration data
440 * @data: ematch specific data
443 struct tcf_ematch_ops * ops;
445 unsigned int datalen;
451 static inline int tcf_em_is_container(struct tcf_ematch *em)
456 static inline int tcf_em_is_simple(struct tcf_ematch *em)
458 return em->flags & TCF_EM_SIMPLE;
461 static inline int tcf_em_is_inverted(struct tcf_ematch *em)
463 return em->flags & TCF_EM_INVERT;
466 static inline int tcf_em_last_match(struct tcf_ematch *em)
468 return (em->flags & TCF_EM_REL_MASK) == TCF_EM_REL_END;
471 static inline int tcf_em_early_end(struct tcf_ematch *em, int result)
473 if (tcf_em_last_match(em))
476 if (result == 0 && em->flags & TCF_EM_REL_AND)
479 if (result != 0 && em->flags & TCF_EM_REL_OR)
486 * struct tcf_ematch_tree - ematch tree handle
488 * @hdr: ematch tree header supplied by userspace
489 * @matches: array of ematches
491 struct tcf_ematch_tree {
492 struct tcf_ematch_tree_hdr hdr;
493 struct tcf_ematch * matches;
498 * struct tcf_ematch_ops - ematch module operations
500 * @kind: identifier (kind) of this ematch module
501 * @datalen: length of expected configuration data (optional)
502 * @change: called during validation (optional)
503 * @match: called during ematch tree evaluation, must return 1/0
504 * @destroy: called during destroyage (optional)
505 * @dump: called during dumping process (optional)
506 * @owner: owner, must be set to THIS_MODULE
507 * @link: link to previous/next ematch module (internal use)
509 struct tcf_ematch_ops {
512 int (*change)(struct net *net, void *,
513 int, struct tcf_ematch *);
514 int (*match)(struct sk_buff *, struct tcf_ematch *,
515 struct tcf_pkt_info *);
516 void (*destroy)(struct tcf_ematch *);
517 int (*dump)(struct sk_buff *, struct tcf_ematch *);
518 struct module *owner;
519 struct list_head link;
522 int tcf_em_register(struct tcf_ematch_ops *);
523 void tcf_em_unregister(struct tcf_ematch_ops *);
524 int tcf_em_tree_validate(struct tcf_proto *, struct nlattr *,
525 struct tcf_ematch_tree *);
526 void tcf_em_tree_destroy(struct tcf_ematch_tree *);
527 int tcf_em_tree_dump(struct sk_buff *, struct tcf_ematch_tree *, int);
528 int __tcf_em_tree_match(struct sk_buff *, struct tcf_ematch_tree *,
529 struct tcf_pkt_info *);
532 * tcf_em_tree_match - evaulate an ematch tree
534 * @skb: socket buffer of the packet in question
535 * @tree: ematch tree to be used for evaluation
536 * @info: packet information examined by classifier
538 * This function matches @skb against the ematch tree in @tree by going
539 * through all ematches respecting their logic relations returning
540 * as soon as the result is obvious.
542 * Returns 1 if the ematch tree as-one matches, no ematches are configured
543 * or ematch is not enabled in the kernel, otherwise 0 is returned.
545 static inline int tcf_em_tree_match(struct sk_buff *skb,
546 struct tcf_ematch_tree *tree,
547 struct tcf_pkt_info *info)
549 if (tree->hdr.nmatches)
550 return __tcf_em_tree_match(skb, tree, info);
555 #define MODULE_ALIAS_TCF_EMATCH(kind) MODULE_ALIAS("ematch-kind-" __stringify(kind))
557 #else /* CONFIG_NET_EMATCH */
559 struct tcf_ematch_tree {
562 #define tcf_em_tree_validate(tp, tb, t) ((void)(t), 0)
563 #define tcf_em_tree_destroy(t) do { (void)(t); } while(0)
564 #define tcf_em_tree_dump(skb, t, tlv) (0)
565 #define tcf_em_tree_match(skb, t, info) ((void)(info), 1)
567 #endif /* CONFIG_NET_EMATCH */
569 static inline unsigned char * tcf_get_base_ptr(struct sk_buff *skb, int layer)
573 return skb_mac_header(skb);
574 case TCF_LAYER_NETWORK:
575 return skb_network_header(skb);
576 case TCF_LAYER_TRANSPORT:
577 return skb_transport_header(skb);
583 static inline int tcf_valid_offset(const struct sk_buff *skb,
584 const unsigned char *ptr, const int len)
586 return likely((ptr + len) <= skb_tail_pointer(skb) &&
588 (ptr <= (ptr + len)));
591 #ifdef CONFIG_NET_CLS_IND
592 #include <net/net_namespace.h>
595 tcf_change_indev(struct net *net, struct nlattr *indev_tlv,
596 struct netlink_ext_ack *extack)
598 char indev[IFNAMSIZ];
599 struct net_device *dev;
601 if (nla_strlcpy(indev, indev_tlv, IFNAMSIZ) >= IFNAMSIZ) {
602 NL_SET_ERR_MSG(extack, "Interface name too long");
605 dev = __dev_get_by_name(net, indev);
612 tcf_match_indev(struct sk_buff *skb, int ifindex)
618 return ifindex == skb->skb_iif;
620 #endif /* CONFIG_NET_CLS_IND */
622 int tc_setup_cb_call(struct tcf_block *block, enum tc_setup_type type,
623 void *type_data, bool err_stop);
625 enum tc_block_command {
630 struct tc_block_offload {
631 enum tc_block_command command;
632 enum tcf_block_binder_type binder_type;
633 struct tcf_block *block;
634 struct netlink_ext_ack *extack;
637 struct tc_cls_common_offload {
641 struct netlink_ext_ack *extack;
644 struct tc_cls_u32_knode {
645 struct tcf_exts *exts;
646 struct tcf_result *res;
647 struct tc_u32_sel *sel;
655 struct tc_cls_u32_hnode {
658 unsigned int divisor;
661 enum tc_clsu32_command {
663 TC_CLSU32_REPLACE_KNODE,
664 TC_CLSU32_DELETE_KNODE,
666 TC_CLSU32_REPLACE_HNODE,
667 TC_CLSU32_DELETE_HNODE,
670 struct tc_cls_u32_offload {
671 struct tc_cls_common_offload common;
673 enum tc_clsu32_command command;
675 struct tc_cls_u32_knode knode;
676 struct tc_cls_u32_hnode hnode;
680 static inline bool tc_can_offload(const struct net_device *dev)
682 return dev->features & NETIF_F_HW_TC;
685 static inline bool tc_can_offload_extack(const struct net_device *dev,
686 struct netlink_ext_ack *extack)
688 bool can = tc_can_offload(dev);
691 NL_SET_ERR_MSG(extack, "TC offload is disabled on net device");
697 tc_cls_can_offload_and_chain0(const struct net_device *dev,
698 struct tc_cls_common_offload *common)
700 if (!tc_can_offload_extack(dev, common->extack))
702 if (common->chain_index) {
703 NL_SET_ERR_MSG(common->extack,
704 "Driver supports only offload of chain 0");
710 static inline bool tc_skip_hw(u32 flags)
712 return (flags & TCA_CLS_FLAGS_SKIP_HW) ? true : false;
715 static inline bool tc_skip_sw(u32 flags)
717 return (flags & TCA_CLS_FLAGS_SKIP_SW) ? true : false;
720 /* SKIP_HW and SKIP_SW are mutually exclusive flags. */
721 static inline bool tc_flags_valid(u32 flags)
723 if (flags & ~(TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW |
724 TCA_CLS_FLAGS_VERBOSE))
727 flags &= TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW;
728 if (!(flags ^ (TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW)))
734 static inline bool tc_in_hw(u32 flags)
736 return (flags & TCA_CLS_FLAGS_IN_HW) ? true : false;
740 tc_cls_common_offload_init(struct tc_cls_common_offload *cls_common,
741 const struct tcf_proto *tp, u32 flags,
742 struct netlink_ext_ack *extack)
744 cls_common->chain_index = tp->chain->index;
745 cls_common->protocol = tp->protocol;
746 cls_common->prio = tp->prio;
747 if (tc_skip_sw(flags) || flags & TCA_CLS_FLAGS_VERBOSE)
748 cls_common->extack = extack;
752 TC_CLSFLOWER_REPLACE,
753 TC_CLSFLOWER_DESTROY,
755 TC_CLSFLOWER_TMPLT_CREATE,
756 TC_CLSFLOWER_TMPLT_DESTROY,
759 struct tc_cls_flower_offload {
760 struct tc_cls_common_offload common;
761 enum tc_fl_command command;
762 unsigned long cookie;
763 struct flow_dissector *dissector;
764 struct fl_flow_key *mask;
765 struct fl_flow_key *key;
766 struct tcf_exts *exts;
770 enum tc_matchall_command {
771 TC_CLSMATCHALL_REPLACE,
772 TC_CLSMATCHALL_DESTROY,
775 struct tc_cls_matchall_offload {
776 struct tc_cls_common_offload common;
777 enum tc_matchall_command command;
778 struct tcf_exts *exts;
779 unsigned long cookie;
782 enum tc_clsbpf_command {
787 struct tc_cls_bpf_offload {
788 struct tc_cls_common_offload common;
789 enum tc_clsbpf_command command;
790 struct tcf_exts *exts;
791 struct bpf_prog *prog;
792 struct bpf_prog *oldprog;
794 bool exts_integrated;
797 struct tc_mqprio_qopt_offload {
798 /* struct tc_mqprio_qopt must always be the first element */
799 struct tc_mqprio_qopt qopt;
803 u64 min_rate[TC_QOPT_MAX_QUEUE];
804 u64 max_rate[TC_QOPT_MAX_QUEUE];
807 /* This structure holds cookie structure that is passed from user
808 * to the kernel for actions and classifiers
816 struct tc_qopt_offload_stats {
817 struct gnet_stats_basic_packed *bstats;
818 struct gnet_stats_queue *qstats;
828 struct tc_mq_opt_offload_graft_params {
833 struct tc_mq_qopt_offload {
834 enum tc_mq_command command;
837 struct tc_qopt_offload_stats stats;
838 struct tc_mq_opt_offload_graft_params graft_params;
842 enum tc_red_command {
850 struct tc_red_qopt_offload_params {
857 struct gnet_stats_queue *qstats;
860 struct tc_red_qopt_offload {
861 enum tc_red_command command;
865 struct tc_red_qopt_offload_params set;
866 struct tc_qopt_offload_stats stats;
867 struct red_stats *xstats;
872 enum tc_gred_command {
878 struct tc_gred_vq_qopt_offload_params {
887 /* Only need backlog, see struct tc_prio_qopt_offload_params */
891 struct tc_gred_qopt_offload_params {
896 struct gnet_stats_queue *qstats;
897 struct tc_gred_vq_qopt_offload_params tab[MAX_DPs];
900 struct tc_gred_qopt_offload_stats {
901 struct gnet_stats_basic_packed bstats[MAX_DPs];
902 struct gnet_stats_queue qstats[MAX_DPs];
903 struct red_stats *xstats[MAX_DPs];
906 struct tc_gred_qopt_offload {
907 enum tc_gred_command command;
911 struct tc_gred_qopt_offload_params set;
912 struct tc_gred_qopt_offload_stats stats;
916 enum tc_prio_command {
923 struct tc_prio_qopt_offload_params {
925 u8 priomap[TC_PRIO_MAX + 1];
926 /* In case that a prio qdisc is offloaded and now is changed to a
927 * non-offloadedable config, it needs to update the backlog & qlen
928 * values to negate the HW backlog & qlen values (and only them).
930 struct gnet_stats_queue *qstats;
933 struct tc_prio_qopt_offload_graft_params {
938 struct tc_prio_qopt_offload {
939 enum tc_prio_command command;
943 struct tc_prio_qopt_offload_params replace_params;
944 struct tc_qopt_offload_stats stats;
945 struct tc_prio_qopt_offload_graft_params graft_params;
949 enum tc_root_command {
953 struct tc_root_qopt_offload {
954 enum tc_root_command command;