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