]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - include/net/pkt_cls.h
net: sched: change names of action number helpers to be aligned with the rest
[mirror_ubuntu-hirsute-kernel.git] / include / net / pkt_cls.h
CommitLineData
1da177e4
LT
1#ifndef __NET_PKT_CLS_H
2#define __NET_PKT_CLS_H
3
4#include <linux/pkt_cls.h>
5#include <net/sch_generic.h>
6#include <net/act_api.h>
7
8/* Basic packet classifier frontend definitions. */
9
fd2c3ef7 10struct tcf_walker {
1da177e4
LT
11 int stop;
12 int skip;
13 int count;
14 int (*fn)(struct tcf_proto *, unsigned long node, struct tcf_walker *);
15};
16
5c15257f
JP
17int register_tcf_proto_ops(struct tcf_proto_ops *ops);
18int unregister_tcf_proto_ops(struct tcf_proto_ops *ops);
1da177e4 19
8ae70032 20#ifdef CONFIG_NET_CLS
367a8ce8
WC
21struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index,
22 bool create);
5bc17018 23void tcf_chain_put(struct tcf_chain *chain);
6529eaba
JP
24int tcf_block_get(struct tcf_block **p_block,
25 struct tcf_proto __rcu **p_filter_chain);
26void tcf_block_put(struct tcf_block *block);
87d83093
JP
27int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
28 struct tcf_result *res, bool compat_mode);
29
8ae70032 30#else
6529eaba
JP
31static inline
32int tcf_block_get(struct tcf_block **p_block,
33 struct tcf_proto __rcu **p_filter_chain)
34{
35 return 0;
36}
37
38static inline void tcf_block_put(struct tcf_block *block)
8ae70032
JP
39{
40}
87d83093
JP
41
42static inline int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
43 struct tcf_result *res, bool compat_mode)
44{
45 return TC_ACT_UNSPEC;
46}
8ae70032 47#endif
cf1facda 48
1da177e4
LT
49static inline unsigned long
50__cls_set_class(unsigned long *clp, unsigned long cl)
51{
a0efb80c 52 return xchg(clp, cl);
1da177e4
LT
53}
54
55static inline unsigned long
56cls_set_class(struct tcf_proto *tp, unsigned long *clp,
57 unsigned long cl)
58{
59 unsigned long old_cl;
60
61 tcf_tree_lock(tp);
62 old_cl = __cls_set_class(clp, cl);
63 tcf_tree_unlock(tp);
64
65 return old_cl;
66}
67
68static inline void
69tcf_bind_filter(struct tcf_proto *tp, struct tcf_result *r, unsigned long base)
70{
71 unsigned long cl;
72
73 cl = tp->q->ops->cl_ops->bind_tcf(tp->q, base, r->classid);
74 cl = cls_set_class(tp, &r->class, cl);
75 if (cl)
76 tp->q->ops->cl_ops->unbind_tcf(tp->q, cl);
77}
78
79static inline void
80tcf_unbind_filter(struct tcf_proto *tp, struct tcf_result *r)
81{
82 unsigned long cl;
83
84 if ((cl = __cls_set_class(&r->class, 0)) != 0)
85 tp->q->ops->cl_ops->unbind_tcf(tp->q, cl);
86}
87
fd2c3ef7 88struct tcf_exts {
1da177e4 89#ifdef CONFIG_NET_CLS_ACT
33be6271 90 __u32 type; /* for backward compat(TCA_OLD_COMPAT) */
22dc13c8
WC
91 int nr_actions;
92 struct tc_action **actions;
1da177e4 93#endif
5da57f42
WC
94 /* Map to export classifier specific extension TLV types to the
95 * generic extensions API. Unsupported extensions must be set to 0.
96 */
1da177e4
LT
97 int action;
98 int police;
99};
100
b9a24bb7 101static inline int tcf_exts_init(struct tcf_exts *exts, int action, int police)
33be6271
WC
102{
103#ifdef CONFIG_NET_CLS_ACT
5da57f42 104 exts->type = 0;
22dc13c8
WC
105 exts->nr_actions = 0;
106 exts->actions = kcalloc(TCA_ACT_MAX_PRIO, sizeof(struct tc_action *),
107 GFP_KERNEL);
b9a24bb7
WC
108 if (!exts->actions)
109 return -ENOMEM;
33be6271 110#endif
5da57f42
WC
111 exts->action = action;
112 exts->police = police;
b9a24bb7 113 return 0;
33be6271
WC
114}
115
1da177e4
LT
116/**
117 * tcf_exts_is_predicative - check if a predicative extension is present
118 * @exts: tc filter extensions handle
119 *
120 * Returns 1 if a predicative extension is present, i.e. an extension which
121 * might cause further actions and thus overrule the regular tcf_result.
122 */
123static inline int
124tcf_exts_is_predicative(struct tcf_exts *exts)
125{
126#ifdef CONFIG_NET_CLS_ACT
22dc13c8 127 return exts->nr_actions;
1da177e4
LT
128#else
129 return 0;
130#endif
131}
132
133/**
134 * tcf_exts_is_available - check if at least one extension is present
135 * @exts: tc filter extensions handle
136 *
137 * Returns 1 if at least one extension is present.
138 */
139static inline int
140tcf_exts_is_available(struct tcf_exts *exts)
141{
142 /* All non-predicative extensions must be added here. */
143 return tcf_exts_is_predicative(exts);
144}
145
22dc13c8
WC
146static inline void tcf_exts_to_list(const struct tcf_exts *exts,
147 struct list_head *actions)
148{
149#ifdef CONFIG_NET_CLS_ACT
150 int i;
151
152 for (i = 0; i < exts->nr_actions; i++) {
153 struct tc_action *a = exts->actions[i];
154
fa5effe7 155 list_add_tail(&a->list, actions);
22dc13c8
WC
156 }
157#endif
158}
159
d897a638
JK
160static inline void
161tcf_exts_stats_update(const struct tcf_exts *exts,
162 u64 bytes, u64 packets, u64 lastuse)
163{
164#ifdef CONFIG_NET_CLS_ACT
165 int i;
166
167 preempt_disable();
168
169 for (i = 0; i < exts->nr_actions; i++) {
170 struct tc_action *a = exts->actions[i];
171
172 tcf_action_stats_update(a, bytes, packets, lastuse);
173 }
174
175 preempt_enable();
176#endif
177}
178
1da177e4
LT
179/**
180 * tcf_exts_exec - execute tc filter extensions
181 * @skb: socket buffer
182 * @exts: tc filter extensions handle
183 * @res: desired result
184 *
185 * Executes all configured extensions. Returns 0 on a normal execution,
186 * a negative number if the filter must be considered unmatched or
187 * a positive action code (TC_ACT_*) which must be returned to the
188 * underlying layer.
189 */
190static inline int
191tcf_exts_exec(struct sk_buff *skb, struct tcf_exts *exts,
192 struct tcf_result *res)
193{
194#ifdef CONFIG_NET_CLS_ACT
22dc13c8
WC
195 if (exts->nr_actions)
196 return tcf_action_exec(skb, exts->actions, exts->nr_actions,
197 res);
1da177e4 198#endif
1da177e4
LT
199 return 0;
200}
201
3bcc0cec
JP
202/**
203 * tcf_exts_has_actions - check if at least one action is present
204 * @exts: tc filter extensions handle
205 *
206 * Returns true if at least one action is present.
207 */
208static inline bool tcf_exts_has_actions(struct tcf_exts *exts)
209{
2734437e 210#ifdef CONFIG_NET_CLS_ACT
3bcc0cec
JP
211 return exts->nr_actions;
212#else
213 return false;
214#endif
215}
2734437e 216
3bcc0cec
JP
217/**
218 * tcf_exts_has_one_action - check if exactly one action is present
219 * @exts: tc filter extensions handle
220 *
221 * Returns true if exactly one action is present.
222 */
223static inline bool tcf_exts_has_one_action(struct tcf_exts *exts)
224{
225#ifdef CONFIG_NET_CLS_ACT
226 return exts->nr_actions == 1;
227#else
228 return false;
229#endif
230}
2734437e 231
5c15257f
JP
232int tcf_exts_validate(struct net *net, struct tcf_proto *tp,
233 struct nlattr **tb, struct nlattr *rate_tlv,
2f7ef2f8 234 struct tcf_exts *exts, bool ovr);
18d0264f 235void tcf_exts_destroy(struct tcf_exts *exts);
5c15257f
JP
236void tcf_exts_change(struct tcf_proto *tp, struct tcf_exts *dst,
237 struct tcf_exts *src);
5da57f42
WC
238int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts);
239int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts);
7091d8c7
HHZ
240int tcf_exts_get_dev(struct net_device *dev, struct tcf_exts *exts,
241 struct net_device **hw_dev);
1da177e4
LT
242
243/**
244 * struct tcf_pkt_info - packet information
245 */
fd2c3ef7 246struct tcf_pkt_info {
1da177e4
LT
247 unsigned char * ptr;
248 int nexthdr;
249};
250
251#ifdef CONFIG_NET_EMATCH
252
253struct tcf_ematch_ops;
254
255/**
256 * struct tcf_ematch - extended match (ematch)
257 *
258 * @matchid: identifier to allow userspace to reidentify a match
259 * @flags: flags specifying attributes and the relation to other matches
260 * @ops: the operations lookup table of the corresponding ematch module
261 * @datalen: length of the ematch specific configuration data
262 * @data: ematch specific data
263 */
fd2c3ef7 264struct tcf_ematch {
1da177e4
LT
265 struct tcf_ematch_ops * ops;
266 unsigned long data;
267 unsigned int datalen;
268 u16 matchid;
269 u16 flags;
82a470f1 270 struct net *net;
1da177e4
LT
271};
272
273static inline int tcf_em_is_container(struct tcf_ematch *em)
274{
275 return !em->ops;
276}
277
278static inline int tcf_em_is_simple(struct tcf_ematch *em)
279{
280 return em->flags & TCF_EM_SIMPLE;
281}
282
283static inline int tcf_em_is_inverted(struct tcf_ematch *em)
284{
285 return em->flags & TCF_EM_INVERT;
286}
287
288static inline int tcf_em_last_match(struct tcf_ematch *em)
289{
290 return (em->flags & TCF_EM_REL_MASK) == TCF_EM_REL_END;
291}
292
293static inline int tcf_em_early_end(struct tcf_ematch *em, int result)
294{
295 if (tcf_em_last_match(em))
296 return 1;
297
298 if (result == 0 && em->flags & TCF_EM_REL_AND)
299 return 1;
300
301 if (result != 0 && em->flags & TCF_EM_REL_OR)
302 return 1;
303
304 return 0;
305}
306
307/**
308 * struct tcf_ematch_tree - ematch tree handle
309 *
310 * @hdr: ematch tree header supplied by userspace
311 * @matches: array of ematches
312 */
fd2c3ef7 313struct tcf_ematch_tree {
1da177e4
LT
314 struct tcf_ematch_tree_hdr hdr;
315 struct tcf_ematch * matches;
316
317};
318
319/**
320 * struct tcf_ematch_ops - ematch module operations
321 *
322 * @kind: identifier (kind) of this ematch module
323 * @datalen: length of expected configuration data (optional)
324 * @change: called during validation (optional)
325 * @match: called during ematch tree evaluation, must return 1/0
326 * @destroy: called during destroyage (optional)
327 * @dump: called during dumping process (optional)
328 * @owner: owner, must be set to THIS_MODULE
329 * @link: link to previous/next ematch module (internal use)
330 */
fd2c3ef7 331struct tcf_ematch_ops {
1da177e4
LT
332 int kind;
333 int datalen;
82a470f1 334 int (*change)(struct net *net, void *,
1da177e4
LT
335 int, struct tcf_ematch *);
336 int (*match)(struct sk_buff *, struct tcf_ematch *,
337 struct tcf_pkt_info *);
82a470f1 338 void (*destroy)(struct tcf_ematch *);
1da177e4
LT
339 int (*dump)(struct sk_buff *, struct tcf_ematch *);
340 struct module *owner;
341 struct list_head link;
342};
343
5c15257f
JP
344int tcf_em_register(struct tcf_ematch_ops *);
345void tcf_em_unregister(struct tcf_ematch_ops *);
346int tcf_em_tree_validate(struct tcf_proto *, struct nlattr *,
347 struct tcf_ematch_tree *);
82a470f1 348void tcf_em_tree_destroy(struct tcf_ematch_tree *);
5c15257f
JP
349int tcf_em_tree_dump(struct sk_buff *, struct tcf_ematch_tree *, int);
350int __tcf_em_tree_match(struct sk_buff *, struct tcf_ematch_tree *,
351 struct tcf_pkt_info *);
1da177e4 352
1da177e4
LT
353/**
354 * tcf_em_tree_match - evaulate an ematch tree
355 *
356 * @skb: socket buffer of the packet in question
357 * @tree: ematch tree to be used for evaluation
358 * @info: packet information examined by classifier
359 *
360 * This function matches @skb against the ematch tree in @tree by going
361 * through all ematches respecting their logic relations returning
362 * as soon as the result is obvious.
363 *
364 * Returns 1 if the ematch tree as-one matches, no ematches are configured
365 * or ematch is not enabled in the kernel, otherwise 0 is returned.
366 */
367static inline int tcf_em_tree_match(struct sk_buff *skb,
368 struct tcf_ematch_tree *tree,
369 struct tcf_pkt_info *info)
370{
371 if (tree->hdr.nmatches)
372 return __tcf_em_tree_match(skb, tree, info);
373 else
374 return 1;
375}
376
db3d99c0
PM
377#define MODULE_ALIAS_TCF_EMATCH(kind) MODULE_ALIAS("ematch-kind-" __stringify(kind))
378
1da177e4
LT
379#else /* CONFIG_NET_EMATCH */
380
fd2c3ef7 381struct tcf_ematch_tree {
1da177e4
LT
382};
383
384#define tcf_em_tree_validate(tp, tb, t) ((void)(t), 0)
82a470f1 385#define tcf_em_tree_destroy(t) do { (void)(t); } while(0)
1da177e4 386#define tcf_em_tree_dump(skb, t, tlv) (0)
1da177e4
LT
387#define tcf_em_tree_match(skb, t, info) ((void)(info), 1)
388
389#endif /* CONFIG_NET_EMATCH */
390
391static inline unsigned char * tcf_get_base_ptr(struct sk_buff *skb, int layer)
392{
393 switch (layer) {
394 case TCF_LAYER_LINK:
395 return skb->data;
396 case TCF_LAYER_NETWORK:
d56f90a7 397 return skb_network_header(skb);
1da177e4 398 case TCF_LAYER_TRANSPORT:
9c70220b 399 return skb_transport_header(skb);
1da177e4
LT
400 }
401
402 return NULL;
403}
404
eddc9ec5
ACM
405static inline int tcf_valid_offset(const struct sk_buff *skb,
406 const unsigned char *ptr, const int len)
1da177e4 407{
da521b2c
DM
408 return likely((ptr + len) <= skb_tail_pointer(skb) &&
409 ptr >= skb->head &&
410 (ptr <= (ptr + len)));
1da177e4
LT
411}
412
413#ifdef CONFIG_NET_CLS_IND
0eeb8ffc
DL
414#include <net/net_namespace.h>
415
1da177e4 416static inline int
2519a602 417tcf_change_indev(struct net *net, struct nlattr *indev_tlv)
1da177e4 418{
2519a602
WC
419 char indev[IFNAMSIZ];
420 struct net_device *dev;
421
add93b61 422 if (nla_strlcpy(indev, indev_tlv, IFNAMSIZ) >= IFNAMSIZ)
1da177e4 423 return -EINVAL;
2519a602
WC
424 dev = __dev_get_by_name(net, indev);
425 if (!dev)
426 return -ENODEV;
427 return dev->ifindex;
1da177e4
LT
428}
429
2519a602
WC
430static inline bool
431tcf_match_indev(struct sk_buff *skb, int ifindex)
1da177e4 432{
2519a602
WC
433 if (!ifindex)
434 return true;
435 if (!skb->skb_iif)
436 return false;
437 return ifindex == skb->skb_iif;
1da177e4
LT
438}
439#endif /* CONFIG_NET_CLS_IND */
440
a1b7c5fd
JF
441struct tc_cls_u32_knode {
442 struct tcf_exts *exts;
e014860e 443 struct tc_u32_sel *sel;
a1b7c5fd
JF
444 u32 handle;
445 u32 val;
446 u32 mask;
447 u32 link_handle;
e014860e 448 u8 fshift;
a1b7c5fd
JF
449};
450
451struct tc_cls_u32_hnode {
452 u32 handle;
453 u32 prio;
454 unsigned int divisor;
455};
456
457enum tc_clsu32_command {
458 TC_CLSU32_NEW_KNODE,
459 TC_CLSU32_REPLACE_KNODE,
460 TC_CLSU32_DELETE_KNODE,
461 TC_CLSU32_NEW_HNODE,
462 TC_CLSU32_REPLACE_HNODE,
463 TC_CLSU32_DELETE_HNODE,
464};
465
466struct tc_cls_u32_offload {
467 /* knode values */
468 enum tc_clsu32_command command;
469 union {
470 struct tc_cls_u32_knode knode;
471 struct tc_cls_u32_hnode hnode;
472 };
473};
474
55330f05
HHZ
475static inline bool tc_can_offload(const struct net_device *dev,
476 const struct tcf_proto *tp)
6843e7a2 477{
92c075db
DB
478 const struct Qdisc *sch = tp->q;
479 const struct Qdisc_class_ops *cops = sch->ops->cl_ops;
480
2b6ab0d3
JF
481 if (!(dev->features & NETIF_F_HW_TC))
482 return false;
9e8ce79c
JF
483 if (!dev->netdev_ops->ndo_setup_tc)
484 return false;
92c075db
DB
485 if (cops && cops->tcf_cl_offload)
486 return cops->tcf_cl_offload(tp->classid);
9e8ce79c
JF
487
488 return true;
6843e7a2
JF
489}
490
55330f05
HHZ
491static inline bool tc_skip_hw(u32 flags)
492{
493 return (flags & TCA_CLS_FLAGS_SKIP_HW) ? true : false;
494}
495
496static inline bool tc_should_offload(const struct net_device *dev,
497 const struct tcf_proto *tp, u32 flags)
498{
499 if (tc_skip_hw(flags))
500 return false;
501 return tc_can_offload(dev, tp);
502}
503
d34e3e18
SS
504static inline bool tc_skip_sw(u32 flags)
505{
506 return (flags & TCA_CLS_FLAGS_SKIP_SW) ? true : false;
507}
508
509/* SKIP_HW and SKIP_SW are mutually exclusive flags. */
510static inline bool tc_flags_valid(u32 flags)
511{
512 if (flags & ~(TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW))
513 return false;
514
515 if (!(flags ^ (TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW)))
516 return false;
517
518 return true;
519}
520
e696028a
OG
521static inline bool tc_in_hw(u32 flags)
522{
523 return (flags & TCA_CLS_FLAGS_IN_HW) ? true : false;
524}
525
5b33f488
AV
526enum tc_fl_command {
527 TC_CLSFLOWER_REPLACE,
528 TC_CLSFLOWER_DESTROY,
10cbc684 529 TC_CLSFLOWER_STATS,
5b33f488
AV
530};
531
532struct tc_cls_flower_offload {
533 enum tc_fl_command command;
69ca05ce 534 u32 prio;
8208d21b 535 unsigned long cookie;
5b33f488
AV
536 struct flow_dissector *dissector;
537 struct fl_flow_key *mask;
538 struct fl_flow_key *key;
539 struct tcf_exts *exts;
540};
541
b87f7936
YG
542enum tc_matchall_command {
543 TC_CLSMATCHALL_REPLACE,
544 TC_CLSMATCHALL_DESTROY,
545};
546
547struct tc_cls_matchall_offload {
548 enum tc_matchall_command command;
549 struct tcf_exts *exts;
550 unsigned long cookie;
551};
552
332ae8e2
JK
553enum tc_clsbpf_command {
554 TC_CLSBPF_ADD,
555 TC_CLSBPF_REPLACE,
556 TC_CLSBPF_DESTROY,
68d64063 557 TC_CLSBPF_STATS,
332ae8e2
JK
558};
559
560struct tc_cls_bpf_offload {
561 enum tc_clsbpf_command command;
562 struct tcf_exts *exts;
563 struct bpf_prog *prog;
564 const char *name;
565 bool exts_integrated;
0d01d45f 566 u32 gen_flags;
332ae8e2
JK
567};
568
1045ba77
JHS
569
570/* This structure holds cookie structure that is passed from user
571 * to the kernel for actions and classifiers
572 */
573struct tc_cookie {
574 u8 *data;
575 u32 len;
576};
1da177e4 577#endif