]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - net/sched/sch_dsmark.c
Merge remote-tracking branch 'ipsec/master'
[karo-tx-linux.git] / net / sched / sch_dsmark.c
1 /* net/sched/sch_dsmark.c - Differentiated Services field marker */
2
3 /* Written 1998-2000 by Werner Almesberger, EPFL ICA */
4
5
6 #include <linux/module.h>
7 #include <linux/init.h>
8 #include <linux/slab.h>
9 #include <linux/types.h>
10 #include <linux/string.h>
11 #include <linux/errno.h>
12 #include <linux/skbuff.h>
13 #include <linux/rtnetlink.h>
14 #include <linux/bitops.h>
15 #include <net/pkt_sched.h>
16 #include <net/dsfield.h>
17 #include <net/inet_ecn.h>
18 #include <asm/byteorder.h>
19
20 /*
21  * classid      class           marking
22  * -------      -----           -------
23  *   n/a          0             n/a
24  *   x:0          1             use entry [0]
25  *   ...         ...            ...
26  *   x:y y>0     y+1            use entry [y]
27  *   ...         ...            ...
28  * x:indices-1  indices         use entry [indices-1]
29  *   ...         ...            ...
30  *   x:y         y+1            use entry [y & (indices-1)]
31  *   ...         ...            ...
32  * 0xffff       0x10000         use entry [indices-1]
33  */
34
35
36 #define NO_DEFAULT_INDEX        (1 << 16)
37
38 struct mask_value {
39         u8                      mask;
40         u8                      value;
41 };
42
43 struct dsmark_qdisc_data {
44         struct Qdisc            *q;
45         struct tcf_proto __rcu  *filter_list;
46         struct mask_value       *mv;
47         u16                     indices;
48         u8                      set_tc_index;
49         u32                     default_index;  /* index range is 0...0xffff */
50 #define DSMARK_EMBEDDED_SZ      16
51         struct mask_value       embedded[DSMARK_EMBEDDED_SZ];
52 };
53
54 static inline int dsmark_valid_index(struct dsmark_qdisc_data *p, u16 index)
55 {
56         return index <= p->indices && index > 0;
57 }
58
59 /* ------------------------- Class/flow operations ------------------------- */
60
61 static int dsmark_graft(struct Qdisc *sch, unsigned long arg,
62                         struct Qdisc *new, struct Qdisc **old)
63 {
64         struct dsmark_qdisc_data *p = qdisc_priv(sch);
65
66         pr_debug("%s(sch %p,[qdisc %p],new %p,old %p)\n",
67                  __func__, sch, p, new, old);
68
69         if (new == NULL) {
70                 new = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops,
71                                         sch->handle);
72                 if (new == NULL)
73                         new = &noop_qdisc;
74         }
75
76         sch_tree_lock(sch);
77         *old = p->q;
78         p->q = new;
79         qdisc_tree_decrease_qlen(*old, (*old)->q.qlen);
80         qdisc_reset(*old);
81         sch_tree_unlock(sch);
82
83         return 0;
84 }
85
86 static struct Qdisc *dsmark_leaf(struct Qdisc *sch, unsigned long arg)
87 {
88         struct dsmark_qdisc_data *p = qdisc_priv(sch);
89         return p->q;
90 }
91
92 static unsigned long dsmark_get(struct Qdisc *sch, u32 classid)
93 {
94         pr_debug("%s(sch %p,[qdisc %p],classid %x)\n",
95                  __func__, sch, qdisc_priv(sch), classid);
96
97         return TC_H_MIN(classid) + 1;
98 }
99
100 static unsigned long dsmark_bind_filter(struct Qdisc *sch,
101                                         unsigned long parent, u32 classid)
102 {
103         return dsmark_get(sch, classid);
104 }
105
106 static void dsmark_put(struct Qdisc *sch, unsigned long cl)
107 {
108 }
109
110 static const struct nla_policy dsmark_policy[TCA_DSMARK_MAX + 1] = {
111         [TCA_DSMARK_INDICES]            = { .type = NLA_U16 },
112         [TCA_DSMARK_DEFAULT_INDEX]      = { .type = NLA_U16 },
113         [TCA_DSMARK_SET_TC_INDEX]       = { .type = NLA_FLAG },
114         [TCA_DSMARK_MASK]               = { .type = NLA_U8 },
115         [TCA_DSMARK_VALUE]              = { .type = NLA_U8 },
116 };
117
118 static int dsmark_change(struct Qdisc *sch, u32 classid, u32 parent,
119                          struct nlattr **tca, unsigned long *arg)
120 {
121         struct dsmark_qdisc_data *p = qdisc_priv(sch);
122         struct nlattr *opt = tca[TCA_OPTIONS];
123         struct nlattr *tb[TCA_DSMARK_MAX + 1];
124         int err = -EINVAL;
125
126         pr_debug("%s(sch %p,[qdisc %p],classid %x,parent %x), arg 0x%lx\n",
127                  __func__, sch, p, classid, parent, *arg);
128
129         if (!dsmark_valid_index(p, *arg)) {
130                 err = -ENOENT;
131                 goto errout;
132         }
133
134         if (!opt)
135                 goto errout;
136
137         err = nla_parse_nested(tb, TCA_DSMARK_MAX, opt, dsmark_policy);
138         if (err < 0)
139                 goto errout;
140
141         if (tb[TCA_DSMARK_VALUE])
142                 p->mv[*arg - 1].value = nla_get_u8(tb[TCA_DSMARK_VALUE]);
143
144         if (tb[TCA_DSMARK_MASK])
145                 p->mv[*arg - 1].mask = nla_get_u8(tb[TCA_DSMARK_MASK]);
146
147         err = 0;
148
149 errout:
150         return err;
151 }
152
153 static int dsmark_delete(struct Qdisc *sch, unsigned long arg)
154 {
155         struct dsmark_qdisc_data *p = qdisc_priv(sch);
156
157         if (!dsmark_valid_index(p, arg))
158                 return -EINVAL;
159
160         p->mv[arg - 1].mask = 0xff;
161         p->mv[arg - 1].value = 0;
162
163         return 0;
164 }
165
166 static void dsmark_walk(struct Qdisc *sch, struct qdisc_walker *walker)
167 {
168         struct dsmark_qdisc_data *p = qdisc_priv(sch);
169         int i;
170
171         pr_debug("%s(sch %p,[qdisc %p],walker %p)\n",
172                  __func__, sch, p, walker);
173
174         if (walker->stop)
175                 return;
176
177         for (i = 0; i < p->indices; i++) {
178                 if (p->mv[i].mask == 0xff && !p->mv[i].value)
179                         goto ignore;
180                 if (walker->count >= walker->skip) {
181                         if (walker->fn(sch, i + 1, walker) < 0) {
182                                 walker->stop = 1;
183                                 break;
184                         }
185                 }
186 ignore:
187                 walker->count++;
188         }
189 }
190
191 static inline struct tcf_proto __rcu **dsmark_find_tcf(struct Qdisc *sch,
192                                                        unsigned long cl)
193 {
194         struct dsmark_qdisc_data *p = qdisc_priv(sch);
195         return &p->filter_list;
196 }
197
198 /* --------------------------- Qdisc operations ---------------------------- */
199
200 static int dsmark_enqueue(struct sk_buff *skb, struct Qdisc *sch)
201 {
202         struct dsmark_qdisc_data *p = qdisc_priv(sch);
203         int err;
204
205         pr_debug("%s(skb %p,sch %p,[qdisc %p])\n", __func__, skb, sch, p);
206
207         if (p->set_tc_index) {
208                 switch (tc_skb_protocol(skb)) {
209                 case htons(ETH_P_IP):
210                         if (skb_cow_head(skb, sizeof(struct iphdr)))
211                                 goto drop;
212
213                         skb->tc_index = ipv4_get_dsfield(ip_hdr(skb))
214                                 & ~INET_ECN_MASK;
215                         break;
216
217                 case htons(ETH_P_IPV6):
218                         if (skb_cow_head(skb, sizeof(struct ipv6hdr)))
219                                 goto drop;
220
221                         skb->tc_index = ipv6_get_dsfield(ipv6_hdr(skb))
222                                 & ~INET_ECN_MASK;
223                         break;
224                 default:
225                         skb->tc_index = 0;
226                         break;
227                 }
228         }
229
230         if (TC_H_MAJ(skb->priority) == sch->handle)
231                 skb->tc_index = TC_H_MIN(skb->priority);
232         else {
233                 struct tcf_result res;
234                 struct tcf_proto *fl = rcu_dereference_bh(p->filter_list);
235                 int result = tc_classify(skb, fl, &res, false);
236
237                 pr_debug("result %d class 0x%04x\n", result, res.classid);
238
239                 switch (result) {
240 #ifdef CONFIG_NET_CLS_ACT
241                 case TC_ACT_QUEUED:
242                 case TC_ACT_STOLEN:
243                         kfree_skb(skb);
244                         return NET_XMIT_SUCCESS | __NET_XMIT_STOLEN;
245
246                 case TC_ACT_SHOT:
247                         goto drop;
248 #endif
249                 case TC_ACT_OK:
250                         skb->tc_index = TC_H_MIN(res.classid);
251                         break;
252
253                 default:
254                         if (p->default_index != NO_DEFAULT_INDEX)
255                                 skb->tc_index = p->default_index;
256                         break;
257                 }
258         }
259
260         err = qdisc_enqueue(skb, p->q);
261         if (err != NET_XMIT_SUCCESS) {
262                 if (net_xmit_drop_count(err))
263                         qdisc_qstats_drop(sch);
264                 return err;
265         }
266
267         sch->q.qlen++;
268
269         return NET_XMIT_SUCCESS;
270
271 drop:
272         qdisc_drop(skb, sch);
273         return NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
274 }
275
276 static struct sk_buff *dsmark_dequeue(struct Qdisc *sch)
277 {
278         struct dsmark_qdisc_data *p = qdisc_priv(sch);
279         struct sk_buff *skb;
280         u32 index;
281
282         pr_debug("%s(sch %p,[qdisc %p])\n", __func__, sch, p);
283
284         skb = p->q->ops->dequeue(p->q);
285         if (skb == NULL)
286                 return NULL;
287
288         qdisc_bstats_update(sch, skb);
289         sch->q.qlen--;
290
291         index = skb->tc_index & (p->indices - 1);
292         pr_debug("index %d->%d\n", skb->tc_index, index);
293
294         switch (tc_skb_protocol(skb)) {
295         case htons(ETH_P_IP):
296                 ipv4_change_dsfield(ip_hdr(skb), p->mv[index].mask,
297                                     p->mv[index].value);
298                         break;
299         case htons(ETH_P_IPV6):
300                 ipv6_change_dsfield(ipv6_hdr(skb), p->mv[index].mask,
301                                     p->mv[index].value);
302                         break;
303         default:
304                 /*
305                  * Only complain if a change was actually attempted.
306                  * This way, we can send non-IP traffic through dsmark
307                  * and don't need yet another qdisc as a bypass.
308                  */
309                 if (p->mv[index].mask != 0xff || p->mv[index].value)
310                         pr_warn("%s: unsupported protocol %d\n",
311                                 __func__, ntohs(tc_skb_protocol(skb)));
312                 break;
313         }
314
315         return skb;
316 }
317
318 static struct sk_buff *dsmark_peek(struct Qdisc *sch)
319 {
320         struct dsmark_qdisc_data *p = qdisc_priv(sch);
321
322         pr_debug("%s(sch %p,[qdisc %p])\n", __func__, sch, p);
323
324         return p->q->ops->peek(p->q);
325 }
326
327 static unsigned int dsmark_drop(struct Qdisc *sch)
328 {
329         struct dsmark_qdisc_data *p = qdisc_priv(sch);
330         unsigned int len;
331
332         pr_debug("%s(sch %p,[qdisc %p])\n", __func__, sch, p);
333
334         if (p->q->ops->drop == NULL)
335                 return 0;
336
337         len = p->q->ops->drop(p->q);
338         if (len)
339                 sch->q.qlen--;
340
341         return len;
342 }
343
344 static int dsmark_init(struct Qdisc *sch, struct nlattr *opt)
345 {
346         struct dsmark_qdisc_data *p = qdisc_priv(sch);
347         struct nlattr *tb[TCA_DSMARK_MAX + 1];
348         int err = -EINVAL;
349         u32 default_index = NO_DEFAULT_INDEX;
350         u16 indices;
351         int i;
352
353         pr_debug("%s(sch %p,[qdisc %p],opt %p)\n", __func__, sch, p, opt);
354
355         if (!opt)
356                 goto errout;
357
358         err = nla_parse_nested(tb, TCA_DSMARK_MAX, opt, dsmark_policy);
359         if (err < 0)
360                 goto errout;
361
362         err = -EINVAL;
363         indices = nla_get_u16(tb[TCA_DSMARK_INDICES]);
364
365         if (hweight32(indices) != 1)
366                 goto errout;
367
368         if (tb[TCA_DSMARK_DEFAULT_INDEX])
369                 default_index = nla_get_u16(tb[TCA_DSMARK_DEFAULT_INDEX]);
370
371         if (indices <= DSMARK_EMBEDDED_SZ)
372                 p->mv = p->embedded;
373         else
374                 p->mv = kmalloc_array(indices, sizeof(*p->mv), GFP_KERNEL);
375         if (!p->mv) {
376                 err = -ENOMEM;
377                 goto errout;
378         }
379         for (i = 0; i < indices; i++) {
380                 p->mv[i].mask = 0xff;
381                 p->mv[i].value = 0;
382         }
383         p->indices = indices;
384         p->default_index = default_index;
385         p->set_tc_index = nla_get_flag(tb[TCA_DSMARK_SET_TC_INDEX]);
386
387         p->q = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops, sch->handle);
388         if (p->q == NULL)
389                 p->q = &noop_qdisc;
390
391         pr_debug("%s: qdisc %p\n", __func__, p->q);
392
393         err = 0;
394 errout:
395         return err;
396 }
397
398 static void dsmark_reset(struct Qdisc *sch)
399 {
400         struct dsmark_qdisc_data *p = qdisc_priv(sch);
401
402         pr_debug("%s(sch %p,[qdisc %p])\n", __func__, sch, p);
403         qdisc_reset(p->q);
404         sch->q.qlen = 0;
405 }
406
407 static void dsmark_destroy(struct Qdisc *sch)
408 {
409         struct dsmark_qdisc_data *p = qdisc_priv(sch);
410
411         pr_debug("%s(sch %p,[qdisc %p])\n", __func__, sch, p);
412
413         tcf_destroy_chain(&p->filter_list);
414         qdisc_destroy(p->q);
415         if (p->mv != p->embedded)
416                 kfree(p->mv);
417 }
418
419 static int dsmark_dump_class(struct Qdisc *sch, unsigned long cl,
420                              struct sk_buff *skb, struct tcmsg *tcm)
421 {
422         struct dsmark_qdisc_data *p = qdisc_priv(sch);
423         struct nlattr *opts = NULL;
424
425         pr_debug("%s(sch %p,[qdisc %p],class %ld\n", __func__, sch, p, cl);
426
427         if (!dsmark_valid_index(p, cl))
428                 return -EINVAL;
429
430         tcm->tcm_handle = TC_H_MAKE(TC_H_MAJ(sch->handle), cl - 1);
431         tcm->tcm_info = p->q->handle;
432
433         opts = nla_nest_start(skb, TCA_OPTIONS);
434         if (opts == NULL)
435                 goto nla_put_failure;
436         if (nla_put_u8(skb, TCA_DSMARK_MASK, p->mv[cl - 1].mask) ||
437             nla_put_u8(skb, TCA_DSMARK_VALUE, p->mv[cl - 1].value))
438                 goto nla_put_failure;
439
440         return nla_nest_end(skb, opts);
441
442 nla_put_failure:
443         nla_nest_cancel(skb, opts);
444         return -EMSGSIZE;
445 }
446
447 static int dsmark_dump(struct Qdisc *sch, struct sk_buff *skb)
448 {
449         struct dsmark_qdisc_data *p = qdisc_priv(sch);
450         struct nlattr *opts = NULL;
451
452         opts = nla_nest_start(skb, TCA_OPTIONS);
453         if (opts == NULL)
454                 goto nla_put_failure;
455         if (nla_put_u16(skb, TCA_DSMARK_INDICES, p->indices))
456                 goto nla_put_failure;
457
458         if (p->default_index != NO_DEFAULT_INDEX &&
459             nla_put_u16(skb, TCA_DSMARK_DEFAULT_INDEX, p->default_index))
460                 goto nla_put_failure;
461
462         if (p->set_tc_index &&
463             nla_put_flag(skb, TCA_DSMARK_SET_TC_INDEX))
464                 goto nla_put_failure;
465
466         return nla_nest_end(skb, opts);
467
468 nla_put_failure:
469         nla_nest_cancel(skb, opts);
470         return -EMSGSIZE;
471 }
472
473 static const struct Qdisc_class_ops dsmark_class_ops = {
474         .graft          =       dsmark_graft,
475         .leaf           =       dsmark_leaf,
476         .get            =       dsmark_get,
477         .put            =       dsmark_put,
478         .change         =       dsmark_change,
479         .delete         =       dsmark_delete,
480         .walk           =       dsmark_walk,
481         .tcf_chain      =       dsmark_find_tcf,
482         .bind_tcf       =       dsmark_bind_filter,
483         .unbind_tcf     =       dsmark_put,
484         .dump           =       dsmark_dump_class,
485 };
486
487 static struct Qdisc_ops dsmark_qdisc_ops __read_mostly = {
488         .next           =       NULL,
489         .cl_ops         =       &dsmark_class_ops,
490         .id             =       "dsmark",
491         .priv_size      =       sizeof(struct dsmark_qdisc_data),
492         .enqueue        =       dsmark_enqueue,
493         .dequeue        =       dsmark_dequeue,
494         .peek           =       dsmark_peek,
495         .drop           =       dsmark_drop,
496         .init           =       dsmark_init,
497         .reset          =       dsmark_reset,
498         .destroy        =       dsmark_destroy,
499         .change         =       NULL,
500         .dump           =       dsmark_dump,
501         .owner          =       THIS_MODULE,
502 };
503
504 static int __init dsmark_module_init(void)
505 {
506         return register_qdisc(&dsmark_qdisc_ops);
507 }
508
509 static void __exit dsmark_module_exit(void)
510 {
511         unregister_qdisc(&dsmark_qdisc_ops);
512 }
513
514 module_init(dsmark_module_init)
515 module_exit(dsmark_module_exit)
516
517 MODULE_LICENSE("GPL");