]> asedeno.scripts.mit.edu Git - linux.git/blob - include/net/pkt_cls.h
7bed674ba29a732f9b8cd221e8d7b7662cb883f8
[linux.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 *, void *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 struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index,
22                                 bool create);
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);
27
28 static inline struct Qdisc *tcf_block_q(struct tcf_block *block)
29 {
30         return block->q;
31 }
32
33 static inline struct net_device *tcf_block_dev(struct tcf_block *block)
34 {
35         return tcf_block_q(block)->dev_queue->dev;
36 }
37
38 int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
39                  struct tcf_result *res, bool compat_mode);
40
41 #else
42 static inline
43 int tcf_block_get(struct tcf_block **p_block,
44                   struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q)
45 {
46         return 0;
47 }
48
49 static inline void tcf_block_put(struct tcf_block *block)
50 {
51 }
52
53 static inline struct Qdisc *tcf_block_q(struct tcf_block *block)
54 {
55         return NULL;
56 }
57
58 static inline struct net_device *tcf_block_dev(struct tcf_block *block)
59 {
60         return NULL;
61 }
62
63 static inline int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
64                                struct tcf_result *res, bool compat_mode)
65 {
66         return TC_ACT_UNSPEC;
67 }
68 #endif
69
70 static inline unsigned long
71 __cls_set_class(unsigned long *clp, unsigned long cl)
72 {
73         return xchg(clp, cl);
74 }
75
76 static inline unsigned long
77 cls_set_class(struct tcf_proto *tp, unsigned long *clp, 
78         unsigned long cl)
79 {
80         unsigned long old_cl;
81         
82         tcf_tree_lock(tp);
83         old_cl = __cls_set_class(clp, cl);
84         tcf_tree_unlock(tp);
85  
86         return old_cl;
87 }
88
89 static inline void
90 tcf_bind_filter(struct tcf_proto *tp, struct tcf_result *r, unsigned long base)
91 {
92         unsigned long cl;
93
94         cl = tp->q->ops->cl_ops->bind_tcf(tp->q, base, r->classid);
95         cl = cls_set_class(tp, &r->class, cl);
96         if (cl)
97                 tp->q->ops->cl_ops->unbind_tcf(tp->q, cl);
98 }
99
100 static inline void
101 tcf_unbind_filter(struct tcf_proto *tp, struct tcf_result *r)
102 {
103         unsigned long cl;
104
105         if ((cl = __cls_set_class(&r->class, 0)) != 0)
106                 tp->q->ops->cl_ops->unbind_tcf(tp->q, cl);
107 }
108
109 struct tcf_exts {
110 #ifdef CONFIG_NET_CLS_ACT
111         __u32   type; /* for backward compat(TCA_OLD_COMPAT) */
112         int nr_actions;
113         struct tc_action **actions;
114 #endif
115         /* Map to export classifier specific extension TLV types to the
116          * generic extensions API. Unsupported extensions must be set to 0.
117          */
118         int action;
119         int police;
120 };
121
122 static inline int tcf_exts_init(struct tcf_exts *exts, int action, int police)
123 {
124 #ifdef CONFIG_NET_CLS_ACT
125         exts->type = 0;
126         exts->nr_actions = 0;
127         exts->actions = kcalloc(TCA_ACT_MAX_PRIO, sizeof(struct tc_action *),
128                                 GFP_KERNEL);
129         if (!exts->actions)
130                 return -ENOMEM;
131 #endif
132         exts->action = action;
133         exts->police = police;
134         return 0;
135 }
136
137 static inline void tcf_exts_to_list(const struct tcf_exts *exts,
138                                     struct list_head *actions)
139 {
140 #ifdef CONFIG_NET_CLS_ACT
141         int i;
142
143         for (i = 0; i < exts->nr_actions; i++) {
144                 struct tc_action *a = exts->actions[i];
145
146                 list_add_tail(&a->list, actions);
147         }
148 #endif
149 }
150
151 static inline void
152 tcf_exts_stats_update(const struct tcf_exts *exts,
153                       u64 bytes, u64 packets, u64 lastuse)
154 {
155 #ifdef CONFIG_NET_CLS_ACT
156         int i;
157
158         preempt_disable();
159
160         for (i = 0; i < exts->nr_actions; i++) {
161                 struct tc_action *a = exts->actions[i];
162
163                 tcf_action_stats_update(a, bytes, packets, lastuse);
164         }
165
166         preempt_enable();
167 #endif
168 }
169
170 /**
171  * tcf_exts_has_actions - check if at least one action is present
172  * @exts: tc filter extensions handle
173  *
174  * Returns true if at least one action is present.
175  */
176 static inline bool tcf_exts_has_actions(struct tcf_exts *exts)
177 {
178 #ifdef CONFIG_NET_CLS_ACT
179         return exts->nr_actions;
180 #else
181         return false;
182 #endif
183 }
184
185 /**
186  * tcf_exts_has_one_action - check if exactly one action is present
187  * @exts: tc filter extensions handle
188  *
189  * Returns true if exactly one action is present.
190  */
191 static inline bool tcf_exts_has_one_action(struct tcf_exts *exts)
192 {
193 #ifdef CONFIG_NET_CLS_ACT
194         return exts->nr_actions == 1;
195 #else
196         return false;
197 #endif
198 }
199
200 /**
201  * tcf_exts_exec - execute tc filter extensions
202  * @skb: socket buffer
203  * @exts: tc filter extensions handle
204  * @res: desired result
205  *
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
209  * underlying layer.
210  */
211 static inline int
212 tcf_exts_exec(struct sk_buff *skb, struct tcf_exts *exts,
213               struct tcf_result *res)
214 {
215 #ifdef CONFIG_NET_CLS_ACT
216         return tcf_action_exec(skb, exts->actions, exts->nr_actions, res);
217 #endif
218         return TC_ACT_OK;
219 }
220
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);
228
229 /**
230  * struct tcf_pkt_info - packet information
231  */
232 struct tcf_pkt_info {
233         unsigned char *         ptr;
234         int                     nexthdr;
235 };
236
237 #ifdef CONFIG_NET_EMATCH
238
239 struct tcf_ematch_ops;
240
241 /**
242  * struct tcf_ematch - extended match (ematch)
243  * 
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
249  */
250 struct tcf_ematch {
251         struct tcf_ematch_ops * ops;
252         unsigned long           data;
253         unsigned int            datalen;
254         u16                     matchid;
255         u16                     flags;
256         struct net              *net;
257 };
258
259 static inline int tcf_em_is_container(struct tcf_ematch *em)
260 {
261         return !em->ops;
262 }
263
264 static inline int tcf_em_is_simple(struct tcf_ematch *em)
265 {
266         return em->flags & TCF_EM_SIMPLE;
267 }
268
269 static inline int tcf_em_is_inverted(struct tcf_ematch *em)
270 {
271         return em->flags & TCF_EM_INVERT;
272 }
273
274 static inline int tcf_em_last_match(struct tcf_ematch *em)
275 {
276         return (em->flags & TCF_EM_REL_MASK) == TCF_EM_REL_END;
277 }
278
279 static inline int tcf_em_early_end(struct tcf_ematch *em, int result)
280 {
281         if (tcf_em_last_match(em))
282                 return 1;
283
284         if (result == 0 && em->flags & TCF_EM_REL_AND)
285                 return 1;
286
287         if (result != 0 && em->flags & TCF_EM_REL_OR)
288                 return 1;
289
290         return 0;
291 }
292         
293 /**
294  * struct tcf_ematch_tree - ematch tree handle
295  *
296  * @hdr: ematch tree header supplied by userspace
297  * @matches: array of ematches
298  */
299 struct tcf_ematch_tree {
300         struct tcf_ematch_tree_hdr hdr;
301         struct tcf_ematch *     matches;
302         
303 };
304
305 /**
306  * struct tcf_ematch_ops - ematch module operations
307  * 
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)
316  */
317 struct tcf_ematch_ops {
318         int                     kind;
319         int                     datalen;
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;
328 };
329
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 *);
338
339 /**
340  * tcf_em_tree_match - evaulate an ematch tree
341  *
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
345  *
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.
349  *
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.
352  */
353 static inline int tcf_em_tree_match(struct sk_buff *skb,
354                                     struct tcf_ematch_tree *tree,
355                                     struct tcf_pkt_info *info)
356 {
357         if (tree->hdr.nmatches)
358                 return __tcf_em_tree_match(skb, tree, info);
359         else
360                 return 1;
361 }
362
363 #define MODULE_ALIAS_TCF_EMATCH(kind)   MODULE_ALIAS("ematch-kind-" __stringify(kind))
364
365 #else /* CONFIG_NET_EMATCH */
366
367 struct tcf_ematch_tree {
368 };
369
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)
374
375 #endif /* CONFIG_NET_EMATCH */
376
377 static inline unsigned char * tcf_get_base_ptr(struct sk_buff *skb, int layer)
378 {
379         switch (layer) {
380                 case TCF_LAYER_LINK:
381                         return skb->data;
382                 case TCF_LAYER_NETWORK:
383                         return skb_network_header(skb);
384                 case TCF_LAYER_TRANSPORT:
385                         return skb_transport_header(skb);
386         }
387
388         return NULL;
389 }
390
391 static inline int tcf_valid_offset(const struct sk_buff *skb,
392                                    const unsigned char *ptr, const int len)
393 {
394         return likely((ptr + len) <= skb_tail_pointer(skb) &&
395                       ptr >= skb->head &&
396                       (ptr <= (ptr + len)));
397 }
398
399 #ifdef CONFIG_NET_CLS_IND
400 #include <net/net_namespace.h>
401
402 static inline int
403 tcf_change_indev(struct net *net, struct nlattr *indev_tlv)
404 {
405         char indev[IFNAMSIZ];
406         struct net_device *dev;
407
408         if (nla_strlcpy(indev, indev_tlv, IFNAMSIZ) >= IFNAMSIZ)
409                 return -EINVAL;
410         dev = __dev_get_by_name(net, indev);
411         if (!dev)
412                 return -ENODEV;
413         return dev->ifindex;
414 }
415
416 static inline bool
417 tcf_match_indev(struct sk_buff *skb, int ifindex)
418 {
419         if (!ifindex)
420                 return true;
421         if  (!skb->skb_iif)
422                 return false;
423         return ifindex == skb->skb_iif;
424 }
425 #endif /* CONFIG_NET_CLS_IND */
426
427 int tc_setup_cb_call(struct tcf_exts *exts, enum tc_setup_type type,
428                      void *type_data, bool err_stop);
429
430 struct tc_cls_common_offload {
431         u32 chain_index;
432         __be16 protocol;
433         u32 prio;
434         u32 classid;
435 };
436
437 static inline void
438 tc_cls_common_offload_init(struct tc_cls_common_offload *cls_common,
439                            const struct tcf_proto *tp)
440 {
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;
445 }
446
447 struct tc_cls_u32_knode {
448         struct tcf_exts *exts;
449         struct tc_u32_sel *sel;
450         u32 handle;
451         u32 val;
452         u32 mask;
453         u32 link_handle;
454         u8 fshift;
455 };
456
457 struct tc_cls_u32_hnode {
458         u32 handle;
459         u32 prio;
460         unsigned int divisor;
461 };
462
463 enum tc_clsu32_command {
464         TC_CLSU32_NEW_KNODE,
465         TC_CLSU32_REPLACE_KNODE,
466         TC_CLSU32_DELETE_KNODE,
467         TC_CLSU32_NEW_HNODE,
468         TC_CLSU32_REPLACE_HNODE,
469         TC_CLSU32_DELETE_HNODE,
470 };
471
472 struct tc_cls_u32_offload {
473         struct tc_cls_common_offload common;
474         /* knode values */
475         enum tc_clsu32_command command;
476         union {
477                 struct tc_cls_u32_knode knode;
478                 struct tc_cls_u32_hnode hnode;
479         };
480 };
481
482 static inline bool tc_can_offload(const struct net_device *dev)
483 {
484         if (!(dev->features & NETIF_F_HW_TC))
485                 return false;
486         if (!dev->netdev_ops->ndo_setup_tc)
487                 return false;
488         return true;
489 }
490
491 static inline bool tc_skip_hw(u32 flags)
492 {
493         return (flags & TCA_CLS_FLAGS_SKIP_HW) ? true : false;
494 }
495
496 static inline bool tc_should_offload(const struct net_device *dev, u32 flags)
497 {
498         if (tc_skip_hw(flags))
499                 return false;
500         return tc_can_offload(dev);
501 }
502
503 static inline bool tc_skip_sw(u32 flags)
504 {
505         return (flags & TCA_CLS_FLAGS_SKIP_SW) ? true : false;
506 }
507
508 /* SKIP_HW and SKIP_SW are mutually exclusive flags. */
509 static inline bool tc_flags_valid(u32 flags)
510 {
511         if (flags & ~(TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW))
512                 return false;
513
514         if (!(flags ^ (TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW)))
515                 return false;
516
517         return true;
518 }
519
520 static inline bool tc_in_hw(u32 flags)
521 {
522         return (flags & TCA_CLS_FLAGS_IN_HW) ? true : false;
523 }
524
525 enum tc_fl_command {
526         TC_CLSFLOWER_REPLACE,
527         TC_CLSFLOWER_DESTROY,
528         TC_CLSFLOWER_STATS,
529 };
530
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;
539 };
540
541 enum tc_matchall_command {
542         TC_CLSMATCHALL_REPLACE,
543         TC_CLSMATCHALL_DESTROY,
544 };
545
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;
551 };
552
553 enum tc_clsbpf_command {
554         TC_CLSBPF_ADD,
555         TC_CLSBPF_REPLACE,
556         TC_CLSBPF_DESTROY,
557         TC_CLSBPF_STATS,
558 };
559
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;
565         const char *name;
566         bool exts_integrated;
567         u32 gen_flags;
568 };
569
570 struct tc_mqprio_qopt_offload {
571         /* struct tc_mqprio_qopt must always be the first element */
572         struct tc_mqprio_qopt qopt;
573         u16 mode;
574         u16 shaper;
575         u32 flags;
576         u64 min_rate[TC_QOPT_MAX_QUEUE];
577         u64 max_rate[TC_QOPT_MAX_QUEUE];
578 };
579
580 /* This structure holds cookie structure that is passed from user
581  * to the kernel for actions and classifiers
582  */
583 struct tc_cookie {
584         u8  *data;
585         u32 len;
586 };
587 #endif