net: sched: introduce tcf block infractructure
[linux-block.git] / include / net / pkt_cls.h
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
10 struct tcf_walker {
11         int     stop;
12         int     skip;
13         int     count;
14         int     (*fn)(struct tcf_proto *, unsigned long node, struct tcf_walker *);
15 };
16
17 int register_tcf_proto_ops(struct tcf_proto_ops *ops);
18 int unregister_tcf_proto_ops(struct tcf_proto_ops *ops);
19
20 #ifdef CONFIG_NET_CLS
21 int tcf_block_get(struct tcf_block **p_block,
22                   struct tcf_proto __rcu **p_filter_chain);
23 void tcf_block_put(struct tcf_block *block);
24 int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
25                  struct tcf_result *res, bool compat_mode);
26
27 #else
28 static inline
29 int tcf_block_get(struct tcf_block **p_block,
30                   struct tcf_proto __rcu **p_filter_chain)
31 {
32         return 0;
33 }
34
35 static inline void tcf_block_put(struct tcf_block *block)
36 {
37 }
38
39 static 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 }
44 #endif
45
46 static inline unsigned long
47 __cls_set_class(unsigned long *clp, unsigned long cl)
48 {
49         return xchg(clp, cl);
50 }
51
52 static inline unsigned long
53 cls_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
65 static inline void
66 tcf_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
76 static inline void
77 tcf_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
85 struct tcf_exts {
86 #ifdef CONFIG_NET_CLS_ACT
87         __u32   type; /* for backward compat(TCA_OLD_COMPAT) */
88         int nr_actions;
89         struct tc_action **actions;
90 #endif
91         /* Map to export classifier specific extension TLV types to the
92          * generic extensions API. Unsupported extensions must be set to 0.
93          */
94         int action;
95         int police;
96 };
97
98 static inline int tcf_exts_init(struct tcf_exts *exts, int action, int police)
99 {
100 #ifdef CONFIG_NET_CLS_ACT
101         exts->type = 0;
102         exts->nr_actions = 0;
103         exts->actions = kcalloc(TCA_ACT_MAX_PRIO, sizeof(struct tc_action *),
104                                 GFP_KERNEL);
105         if (!exts->actions)
106                 return -ENOMEM;
107 #endif
108         exts->action = action;
109         exts->police = police;
110         return 0;
111 }
112
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  */
120 static inline int
121 tcf_exts_is_predicative(struct tcf_exts *exts)
122 {
123 #ifdef CONFIG_NET_CLS_ACT
124         return exts->nr_actions;
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  */
136 static inline int
137 tcf_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
143 static 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
152                 list_add_tail(&a->list, actions);
153         }
154 #endif
155 }
156
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  */
168 static inline int
169 tcf_exts_exec(struct sk_buff *skb, struct tcf_exts *exts,
170                struct tcf_result *res)
171 {
172 #ifdef CONFIG_NET_CLS_ACT
173         if (exts->nr_actions)
174                 return tcf_action_exec(skb, exts->actions, exts->nr_actions,
175                                        res);
176 #endif
177         return 0;
178 }
179
180 #ifdef CONFIG_NET_CLS_ACT
181
182 #define tc_no_actions(_exts)  ((_exts)->nr_actions == 0)
183 #define tc_single_action(_exts) ((_exts)->nr_actions == 1)
184
185 #else /* CONFIG_NET_CLS_ACT */
186
187 #define tc_no_actions(_exts) true
188 #define tc_single_action(_exts) false
189
190 #endif /* CONFIG_NET_CLS_ACT */
191
192 int tcf_exts_validate(struct net *net, struct tcf_proto *tp,
193                       struct nlattr **tb, struct nlattr *rate_tlv,
194                       struct tcf_exts *exts, bool ovr);
195 void tcf_exts_destroy(struct tcf_exts *exts);
196 void tcf_exts_change(struct tcf_proto *tp, struct tcf_exts *dst,
197                      struct tcf_exts *src);
198 int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts);
199 int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts);
200 int tcf_exts_get_dev(struct net_device *dev, struct tcf_exts *exts,
201                      struct net_device **hw_dev);
202
203 /**
204  * struct tcf_pkt_info - packet information
205  */
206 struct tcf_pkt_info {
207         unsigned char *         ptr;
208         int                     nexthdr;
209 };
210
211 #ifdef CONFIG_NET_EMATCH
212
213 struct 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  */
224 struct tcf_ematch {
225         struct tcf_ematch_ops * ops;
226         unsigned long           data;
227         unsigned int            datalen;
228         u16                     matchid;
229         u16                     flags;
230         struct net              *net;
231 };
232
233 static inline int tcf_em_is_container(struct tcf_ematch *em)
234 {
235         return !em->ops;
236 }
237
238 static inline int tcf_em_is_simple(struct tcf_ematch *em)
239 {
240         return em->flags & TCF_EM_SIMPLE;
241 }
242
243 static inline int tcf_em_is_inverted(struct tcf_ematch *em)
244 {
245         return em->flags & TCF_EM_INVERT;
246 }
247
248 static 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
253 static 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  */
273 struct tcf_ematch_tree {
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  */
291 struct tcf_ematch_ops {
292         int                     kind;
293         int                     datalen;
294         int                     (*change)(struct net *net, void *,
295                                           int, struct tcf_ematch *);
296         int                     (*match)(struct sk_buff *, struct tcf_ematch *,
297                                          struct tcf_pkt_info *);
298         void                    (*destroy)(struct tcf_ematch *);
299         int                     (*dump)(struct sk_buff *, struct tcf_ematch *);
300         struct module           *owner;
301         struct list_head        link;
302 };
303
304 int tcf_em_register(struct tcf_ematch_ops *);
305 void tcf_em_unregister(struct tcf_ematch_ops *);
306 int tcf_em_tree_validate(struct tcf_proto *, struct nlattr *,
307                          struct tcf_ematch_tree *);
308 void tcf_em_tree_destroy(struct tcf_ematch_tree *);
309 int tcf_em_tree_dump(struct sk_buff *, struct tcf_ematch_tree *, int);
310 int __tcf_em_tree_match(struct sk_buff *, struct tcf_ematch_tree *,
311                         struct tcf_pkt_info *);
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  */
324 static 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  */
347 static 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
357 #define MODULE_ALIAS_TCF_EMATCH(kind)   MODULE_ALIAS("ematch-kind-" __stringify(kind))
358
359 #else /* CONFIG_NET_EMATCH */
360
361 struct tcf_ematch_tree {
362 };
363
364 #define tcf_em_tree_validate(tp, tb, t) ((void)(t), 0)
365 #define tcf_em_tree_destroy(t) do { (void)(t); } while(0)
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
372 static 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:
378                         return skb_network_header(skb);
379                 case TCF_LAYER_TRANSPORT:
380                         return skb_transport_header(skb);
381         }
382
383         return NULL;
384 }
385
386 static inline int tcf_valid_offset(const struct sk_buff *skb,
387                                    const unsigned char *ptr, const int len)
388 {
389         return likely((ptr + len) <= skb_tail_pointer(skb) &&
390                       ptr >= skb->head &&
391                       (ptr <= (ptr + len)));
392 }
393
394 #ifdef CONFIG_NET_CLS_IND
395 #include <net/net_namespace.h>
396
397 static inline int
398 tcf_change_indev(struct net *net, struct nlattr *indev_tlv)
399 {
400         char indev[IFNAMSIZ];
401         struct net_device *dev;
402
403         if (nla_strlcpy(indev, indev_tlv, IFNAMSIZ) >= IFNAMSIZ)
404                 return -EINVAL;
405         dev = __dev_get_by_name(net, indev);
406         if (!dev)
407                 return -ENODEV;
408         return dev->ifindex;
409 }
410
411 static inline bool
412 tcf_match_indev(struct sk_buff *skb, int ifindex)
413 {
414         if (!ifindex)
415                 return true;
416         if  (!skb->skb_iif)
417                 return false;
418         return ifindex == skb->skb_iif;
419 }
420 #endif /* CONFIG_NET_CLS_IND */
421
422 struct tc_cls_u32_knode {
423         struct tcf_exts *exts;
424         struct tc_u32_sel *sel;
425         u32 handle;
426         u32 val;
427         u32 mask;
428         u32 link_handle;
429         u8 fshift;
430 };
431
432 struct tc_cls_u32_hnode {
433         u32 handle;
434         u32 prio;
435         unsigned int divisor;
436 };
437
438 enum 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
447 struct 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
456 static inline bool tc_can_offload(const struct net_device *dev,
457                                   const struct tcf_proto *tp)
458 {
459         const struct Qdisc *sch = tp->q;
460         const struct Qdisc_class_ops *cops = sch->ops->cl_ops;
461
462         if (!(dev->features & NETIF_F_HW_TC))
463                 return false;
464         if (!dev->netdev_ops->ndo_setup_tc)
465                 return false;
466         if (cops && cops->tcf_cl_offload)
467                 return cops->tcf_cl_offload(tp->classid);
468
469         return true;
470 }
471
472 static inline bool tc_skip_hw(u32 flags)
473 {
474         return (flags & TCA_CLS_FLAGS_SKIP_HW) ? true : false;
475 }
476
477 static 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
485 static 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. */
491 static 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
502 static inline bool tc_in_hw(u32 flags)
503 {
504         return (flags & TCA_CLS_FLAGS_IN_HW) ? true : false;
505 }
506
507 enum tc_fl_command {
508         TC_CLSFLOWER_REPLACE,
509         TC_CLSFLOWER_DESTROY,
510         TC_CLSFLOWER_STATS,
511 };
512
513 struct tc_cls_flower_offload {
514         enum tc_fl_command command;
515         u32 prio;
516         unsigned long cookie;
517         struct flow_dissector *dissector;
518         struct fl_flow_key *mask;
519         struct fl_flow_key *key;
520         struct tcf_exts *exts;
521 };
522
523 enum tc_matchall_command {
524         TC_CLSMATCHALL_REPLACE,
525         TC_CLSMATCHALL_DESTROY,
526 };
527
528 struct tc_cls_matchall_offload {
529         enum tc_matchall_command command;
530         struct tcf_exts *exts;
531         unsigned long cookie;
532 };
533
534 enum tc_clsbpf_command {
535         TC_CLSBPF_ADD,
536         TC_CLSBPF_REPLACE,
537         TC_CLSBPF_DESTROY,
538         TC_CLSBPF_STATS,
539 };
540
541 struct 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;
547         u32 gen_flags;
548 };
549
550
551 /* This structure holds cookie structure that is passed from user
552  * to the kernel for actions and classifiers
553  */
554 struct tc_cookie {
555         u8  *data;
556         u32 len;
557 };
558 #endif