1 #ifndef __NET_PKT_CLS_H
2 #define __NET_PKT_CLS_H
4 #include <linux/pkt_cls.h>
5 #include <net/sch_generic.h>
6 #include <net/act_api.h>
8 /* Basic packet classifier frontend definitions. */
14 int (*fn)(struct tcf_proto *, void *node, struct tcf_walker *);
17 int register_tcf_proto_ops(struct tcf_proto_ops *ops);
18 int unregister_tcf_proto_ops(struct tcf_proto_ops *ops);
21 struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index,
23 void tcf_chain_put(struct tcf_chain *chain);
24 int tcf_block_get(struct tcf_block **p_block,
25 struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q);
26 void tcf_block_put(struct tcf_block *block);
28 static inline struct Qdisc *tcf_block_q(struct tcf_block *block)
33 static inline struct net_device *tcf_block_dev(struct tcf_block *block)
35 return tcf_block_q(block)->dev_queue->dev;
38 int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
39 struct tcf_result *res, bool compat_mode);
43 int tcf_block_get(struct tcf_block **p_block,
44 struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q)
49 static inline void tcf_block_put(struct tcf_block *block)
53 static inline struct Qdisc *tcf_block_q(struct tcf_block *block)
58 static inline struct net_device *tcf_block_dev(struct tcf_block *block)
63 static inline int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
64 struct tcf_result *res, bool compat_mode)
70 static inline unsigned long
71 __cls_set_class(unsigned long *clp, unsigned long cl)
76 static inline unsigned long
77 cls_set_class(struct tcf_proto *tp, unsigned long *clp,
83 old_cl = __cls_set_class(clp, cl);
90 tcf_bind_filter(struct tcf_proto *tp, struct tcf_result *r, unsigned long base)
94 cl = tp->q->ops->cl_ops->bind_tcf(tp->q, base, r->classid);
95 cl = cls_set_class(tp, &r->class, cl);
97 tp->q->ops->cl_ops->unbind_tcf(tp->q, cl);
101 tcf_unbind_filter(struct tcf_proto *tp, struct tcf_result *r)
105 if ((cl = __cls_set_class(&r->class, 0)) != 0)
106 tp->q->ops->cl_ops->unbind_tcf(tp->q, cl);
110 #ifdef CONFIG_NET_CLS_ACT
111 __u32 type; /* for backward compat(TCA_OLD_COMPAT) */
113 struct tc_action **actions;
115 /* Map to export classifier specific extension TLV types to the
116 * generic extensions API. Unsupported extensions must be set to 0.
122 static inline int tcf_exts_init(struct tcf_exts *exts, int action, int police)
124 #ifdef CONFIG_NET_CLS_ACT
126 exts->nr_actions = 0;
127 exts->actions = kcalloc(TCA_ACT_MAX_PRIO, sizeof(struct tc_action *),
132 exts->action = action;
133 exts->police = police;
137 static inline void tcf_exts_to_list(const struct tcf_exts *exts,
138 struct list_head *actions)
140 #ifdef CONFIG_NET_CLS_ACT
143 for (i = 0; i < exts->nr_actions; i++) {
144 struct tc_action *a = exts->actions[i];
146 list_add_tail(&a->list, actions);
152 tcf_exts_stats_update(const struct tcf_exts *exts,
153 u64 bytes, u64 packets, u64 lastuse)
155 #ifdef CONFIG_NET_CLS_ACT
160 for (i = 0; i < exts->nr_actions; i++) {
161 struct tc_action *a = exts->actions[i];
163 tcf_action_stats_update(a, bytes, packets, lastuse);
171 * tcf_exts_has_actions - check if at least one action is present
172 * @exts: tc filter extensions handle
174 * Returns true if at least one action is present.
176 static inline bool tcf_exts_has_actions(struct tcf_exts *exts)
178 #ifdef CONFIG_NET_CLS_ACT
179 return exts->nr_actions;
186 * tcf_exts_has_one_action - check if exactly one action is present
187 * @exts: tc filter extensions handle
189 * Returns true if exactly one action is present.
191 static inline bool tcf_exts_has_one_action(struct tcf_exts *exts)
193 #ifdef CONFIG_NET_CLS_ACT
194 return exts->nr_actions == 1;
201 * tcf_exts_exec - execute tc filter extensions
202 * @skb: socket buffer
203 * @exts: tc filter extensions handle
204 * @res: desired result
206 * Executes all configured extensions. Returns TC_ACT_OK on a normal execution,
207 * a negative number if the filter must be considered unmatched or
208 * a positive action code (TC_ACT_*) which must be returned to the
212 tcf_exts_exec(struct sk_buff *skb, struct tcf_exts *exts,
213 struct tcf_result *res)
215 #ifdef CONFIG_NET_CLS_ACT
216 return tcf_action_exec(skb, exts->actions, exts->nr_actions, res);
221 int tcf_exts_validate(struct net *net, struct tcf_proto *tp,
222 struct nlattr **tb, struct nlattr *rate_tlv,
223 struct tcf_exts *exts, bool ovr);
224 void tcf_exts_destroy(struct tcf_exts *exts);
225 void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src);
226 int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts);
227 int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts);
230 * struct tcf_pkt_info - packet information
232 struct tcf_pkt_info {
237 #ifdef CONFIG_NET_EMATCH
239 struct tcf_ematch_ops;
242 * struct tcf_ematch - extended match (ematch)
244 * @matchid: identifier to allow userspace to reidentify a match
245 * @flags: flags specifying attributes and the relation to other matches
246 * @ops: the operations lookup table of the corresponding ematch module
247 * @datalen: length of the ematch specific configuration data
248 * @data: ematch specific data
251 struct tcf_ematch_ops * ops;
253 unsigned int datalen;
259 static inline int tcf_em_is_container(struct tcf_ematch *em)
264 static inline int tcf_em_is_simple(struct tcf_ematch *em)
266 return em->flags & TCF_EM_SIMPLE;
269 static inline int tcf_em_is_inverted(struct tcf_ematch *em)
271 return em->flags & TCF_EM_INVERT;
274 static inline int tcf_em_last_match(struct tcf_ematch *em)
276 return (em->flags & TCF_EM_REL_MASK) == TCF_EM_REL_END;
279 static inline int tcf_em_early_end(struct tcf_ematch *em, int result)
281 if (tcf_em_last_match(em))
284 if (result == 0 && em->flags & TCF_EM_REL_AND)
287 if (result != 0 && em->flags & TCF_EM_REL_OR)
294 * struct tcf_ematch_tree - ematch tree handle
296 * @hdr: ematch tree header supplied by userspace
297 * @matches: array of ematches
299 struct tcf_ematch_tree {
300 struct tcf_ematch_tree_hdr hdr;
301 struct tcf_ematch * matches;
306 * struct tcf_ematch_ops - ematch module operations
308 * @kind: identifier (kind) of this ematch module
309 * @datalen: length of expected configuration data (optional)
310 * @change: called during validation (optional)
311 * @match: called during ematch tree evaluation, must return 1/0
312 * @destroy: called during destroyage (optional)
313 * @dump: called during dumping process (optional)
314 * @owner: owner, must be set to THIS_MODULE
315 * @link: link to previous/next ematch module (internal use)
317 struct tcf_ematch_ops {
320 int (*change)(struct net *net, void *,
321 int, struct tcf_ematch *);
322 int (*match)(struct sk_buff *, struct tcf_ematch *,
323 struct tcf_pkt_info *);
324 void (*destroy)(struct tcf_ematch *);
325 int (*dump)(struct sk_buff *, struct tcf_ematch *);
326 struct module *owner;
327 struct list_head link;
330 int tcf_em_register(struct tcf_ematch_ops *);
331 void tcf_em_unregister(struct tcf_ematch_ops *);
332 int tcf_em_tree_validate(struct tcf_proto *, struct nlattr *,
333 struct tcf_ematch_tree *);
334 void tcf_em_tree_destroy(struct tcf_ematch_tree *);
335 int tcf_em_tree_dump(struct sk_buff *, struct tcf_ematch_tree *, int);
336 int __tcf_em_tree_match(struct sk_buff *, struct tcf_ematch_tree *,
337 struct tcf_pkt_info *);
340 * tcf_em_tree_match - evaulate an ematch tree
342 * @skb: socket buffer of the packet in question
343 * @tree: ematch tree to be used for evaluation
344 * @info: packet information examined by classifier
346 * This function matches @skb against the ematch tree in @tree by going
347 * through all ematches respecting their logic relations returning
348 * as soon as the result is obvious.
350 * Returns 1 if the ematch tree as-one matches, no ematches are configured
351 * or ematch is not enabled in the kernel, otherwise 0 is returned.
353 static inline int tcf_em_tree_match(struct sk_buff *skb,
354 struct tcf_ematch_tree *tree,
355 struct tcf_pkt_info *info)
357 if (tree->hdr.nmatches)
358 return __tcf_em_tree_match(skb, tree, info);
363 #define MODULE_ALIAS_TCF_EMATCH(kind) MODULE_ALIAS("ematch-kind-" __stringify(kind))
365 #else /* CONFIG_NET_EMATCH */
367 struct tcf_ematch_tree {
370 #define tcf_em_tree_validate(tp, tb, t) ((void)(t), 0)
371 #define tcf_em_tree_destroy(t) do { (void)(t); } while(0)
372 #define tcf_em_tree_dump(skb, t, tlv) (0)
373 #define tcf_em_tree_match(skb, t, info) ((void)(info), 1)
375 #endif /* CONFIG_NET_EMATCH */
377 static inline unsigned char * tcf_get_base_ptr(struct sk_buff *skb, int layer)
382 case TCF_LAYER_NETWORK:
383 return skb_network_header(skb);
384 case TCF_LAYER_TRANSPORT:
385 return skb_transport_header(skb);
391 static inline int tcf_valid_offset(const struct sk_buff *skb,
392 const unsigned char *ptr, const int len)
394 return likely((ptr + len) <= skb_tail_pointer(skb) &&
396 (ptr <= (ptr + len)));
399 #ifdef CONFIG_NET_CLS_IND
400 #include <net/net_namespace.h>
403 tcf_change_indev(struct net *net, struct nlattr *indev_tlv)
405 char indev[IFNAMSIZ];
406 struct net_device *dev;
408 if (nla_strlcpy(indev, indev_tlv, IFNAMSIZ) >= IFNAMSIZ)
410 dev = __dev_get_by_name(net, indev);
417 tcf_match_indev(struct sk_buff *skb, int ifindex)
423 return ifindex == skb->skb_iif;
425 #endif /* CONFIG_NET_CLS_IND */
427 int tc_setup_cb_call(struct tcf_exts *exts, enum tc_setup_type type,
428 void *type_data, bool err_stop);
430 struct tc_cls_common_offload {
438 tc_cls_common_offload_init(struct tc_cls_common_offload *cls_common,
439 const struct tcf_proto *tp)
441 cls_common->chain_index = tp->chain->index;
442 cls_common->protocol = tp->protocol;
443 cls_common->prio = tp->prio;
444 cls_common->classid = tp->classid;
447 struct tc_cls_u32_knode {
448 struct tcf_exts *exts;
449 struct tc_u32_sel *sel;
457 struct tc_cls_u32_hnode {
460 unsigned int divisor;
463 enum tc_clsu32_command {
465 TC_CLSU32_REPLACE_KNODE,
466 TC_CLSU32_DELETE_KNODE,
468 TC_CLSU32_REPLACE_HNODE,
469 TC_CLSU32_DELETE_HNODE,
472 struct tc_cls_u32_offload {
473 struct tc_cls_common_offload common;
475 enum tc_clsu32_command command;
477 struct tc_cls_u32_knode knode;
478 struct tc_cls_u32_hnode hnode;
482 static inline bool tc_can_offload(const struct net_device *dev)
484 if (!(dev->features & NETIF_F_HW_TC))
486 if (!dev->netdev_ops->ndo_setup_tc)
491 static inline bool tc_skip_hw(u32 flags)
493 return (flags & TCA_CLS_FLAGS_SKIP_HW) ? true : false;
496 static inline bool tc_should_offload(const struct net_device *dev, u32 flags)
498 if (tc_skip_hw(flags))
500 return tc_can_offload(dev);
503 static inline bool tc_skip_sw(u32 flags)
505 return (flags & TCA_CLS_FLAGS_SKIP_SW) ? true : false;
508 /* SKIP_HW and SKIP_SW are mutually exclusive flags. */
509 static inline bool tc_flags_valid(u32 flags)
511 if (flags & ~(TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW))
514 if (!(flags ^ (TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW)))
520 static inline bool tc_in_hw(u32 flags)
522 return (flags & TCA_CLS_FLAGS_IN_HW) ? true : false;
526 TC_CLSFLOWER_REPLACE,
527 TC_CLSFLOWER_DESTROY,
531 struct tc_cls_flower_offload {
532 struct tc_cls_common_offload common;
533 enum tc_fl_command command;
534 unsigned long cookie;
535 struct flow_dissector *dissector;
536 struct fl_flow_key *mask;
537 struct fl_flow_key *key;
538 struct tcf_exts *exts;
541 enum tc_matchall_command {
542 TC_CLSMATCHALL_REPLACE,
543 TC_CLSMATCHALL_DESTROY,
546 struct tc_cls_matchall_offload {
547 struct tc_cls_common_offload common;
548 enum tc_matchall_command command;
549 struct tcf_exts *exts;
550 unsigned long cookie;
553 enum tc_clsbpf_command {
560 struct tc_cls_bpf_offload {
561 struct tc_cls_common_offload common;
562 enum tc_clsbpf_command command;
563 struct tcf_exts *exts;
564 struct bpf_prog *prog;
566 bool exts_integrated;
570 struct tc_mqprio_qopt_offload {
571 /* struct tc_mqprio_qopt must always be the first element */
572 struct tc_mqprio_qopt qopt;
576 u64 min_rate[TC_QOPT_MAX_QUEUE];
577 u64 max_rate[TC_QOPT_MAX_QUEUE];
580 /* This structure holds cookie structure that is passed from user
581 * to the kernel for actions and classifiers