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