ARM: dts: kirkwood: gpio pin fixes for linkstation ls-wxl/wsxl
[linux/fpc-iii.git] / net / sched / sch_dsmark.c
blobf357f34d02d2a5e9f4ead0ebe77ec44dca79f89a
1 /* net/sched/sch_dsmark.c - Differentiated Services field marker */
3 /* Written 1998-2000 by Werner Almesberger, EPFL ICA */
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>
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]
36 #define NO_DEFAULT_INDEX (1 << 16)
38 struct mask_value {
39 u8 mask;
40 u8 value;
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];
54 static inline int dsmark_valid_index(struct dsmark_qdisc_data *p, u16 index)
56 return index <= p->indices && index > 0;
59 /* ------------------------- Class/flow operations ------------------------- */
61 static int dsmark_graft(struct Qdisc *sch, unsigned long arg,
62 struct Qdisc *new, struct Qdisc **old)
64 struct dsmark_qdisc_data *p = qdisc_priv(sch);
66 pr_debug("%s(sch %p,[qdisc %p],new %p,old %p)\n",
67 __func__, sch, p, new, old);
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;
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);
83 return 0;
86 static struct Qdisc *dsmark_leaf(struct Qdisc *sch, unsigned long arg)
88 struct dsmark_qdisc_data *p = qdisc_priv(sch);
89 return p->q;
92 static unsigned long dsmark_get(struct Qdisc *sch, u32 classid)
94 pr_debug("%s(sch %p,[qdisc %p],classid %x)\n",
95 __func__, sch, qdisc_priv(sch), classid);
97 return TC_H_MIN(classid) + 1;
100 static unsigned long dsmark_bind_filter(struct Qdisc *sch,
101 unsigned long parent, u32 classid)
103 return dsmark_get(sch, classid);
106 static void dsmark_put(struct Qdisc *sch, unsigned long cl)
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 },
118 static int dsmark_change(struct Qdisc *sch, u32 classid, u32 parent,
119 struct nlattr **tca, unsigned long *arg)
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;
126 pr_debug("%s(sch %p,[qdisc %p],classid %x,parent %x), arg 0x%lx\n",
127 __func__, sch, p, classid, parent, *arg);
129 if (!dsmark_valid_index(p, *arg)) {
130 err = -ENOENT;
131 goto errout;
134 if (!opt)
135 goto errout;
137 err = nla_parse_nested(tb, TCA_DSMARK_MAX, opt, dsmark_policy);
138 if (err < 0)
139 goto errout;
141 if (tb[TCA_DSMARK_VALUE])
142 p->mv[*arg - 1].value = nla_get_u8(tb[TCA_DSMARK_VALUE]);
144 if (tb[TCA_DSMARK_MASK])
145 p->mv[*arg - 1].mask = nla_get_u8(tb[TCA_DSMARK_MASK]);
147 err = 0;
149 errout:
150 return err;
153 static int dsmark_delete(struct Qdisc *sch, unsigned long arg)
155 struct dsmark_qdisc_data *p = qdisc_priv(sch);
157 if (!dsmark_valid_index(p, arg))
158 return -EINVAL;
160 p->mv[arg - 1].mask = 0xff;
161 p->mv[arg - 1].value = 0;
163 return 0;
166 static void dsmark_walk(struct Qdisc *sch, struct qdisc_walker *walker)
168 struct dsmark_qdisc_data *p = qdisc_priv(sch);
169 int i;
171 pr_debug("%s(sch %p,[qdisc %p],walker %p)\n",
172 __func__, sch, p, walker);
174 if (walker->stop)
175 return;
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;
186 ignore:
187 walker->count++;
191 static inline struct tcf_proto __rcu **dsmark_find_tcf(struct Qdisc *sch,
192 unsigned long cl)
194 struct dsmark_qdisc_data *p = qdisc_priv(sch);
195 return &p->filter_list;
198 /* --------------------------- Qdisc operations ---------------------------- */
200 static int dsmark_enqueue(struct sk_buff *skb, struct Qdisc *sch)
202 struct dsmark_qdisc_data *p = qdisc_priv(sch);
203 int err;
205 pr_debug("%s(skb %p,sch %p,[qdisc %p])\n", __func__, skb, sch, p);
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;
213 skb->tc_index = ipv4_get_dsfield(ip_hdr(skb))
214 & ~INET_ECN_MASK;
215 break;
217 case htons(ETH_P_IPV6):
218 if (skb_cow_head(skb, sizeof(struct ipv6hdr)))
219 goto drop;
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;
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);
237 pr_debug("result %d class 0x%04x\n", result, res.classid);
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;
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;
253 default:
254 if (p->default_index != NO_DEFAULT_INDEX)
255 skb->tc_index = p->default_index;
256 break;
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;
267 sch->q.qlen++;
269 return NET_XMIT_SUCCESS;
271 drop:
272 qdisc_drop(skb, sch);
273 return NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
276 static struct sk_buff *dsmark_dequeue(struct Qdisc *sch)
278 struct dsmark_qdisc_data *p = qdisc_priv(sch);
279 struct sk_buff *skb;
280 u32 index;
282 pr_debug("%s(sch %p,[qdisc %p])\n", __func__, sch, p);
284 skb = p->q->ops->dequeue(p->q);
285 if (skb == NULL)
286 return NULL;
288 qdisc_bstats_update(sch, skb);
289 sch->q.qlen--;
291 index = skb->tc_index & (p->indices - 1);
292 pr_debug("index %d->%d\n", skb->tc_index, index);
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:
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.
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;
315 return skb;
318 static struct sk_buff *dsmark_peek(struct Qdisc *sch)
320 struct dsmark_qdisc_data *p = qdisc_priv(sch);
322 pr_debug("%s(sch %p,[qdisc %p])\n", __func__, sch, p);
324 return p->q->ops->peek(p->q);
327 static unsigned int dsmark_drop(struct Qdisc *sch)
329 struct dsmark_qdisc_data *p = qdisc_priv(sch);
330 unsigned int len;
332 pr_debug("%s(sch %p,[qdisc %p])\n", __func__, sch, p);
334 if (p->q->ops->drop == NULL)
335 return 0;
337 len = p->q->ops->drop(p->q);
338 if (len)
339 sch->q.qlen--;
341 return len;
344 static int dsmark_init(struct Qdisc *sch, struct nlattr *opt)
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;
353 pr_debug("%s(sch %p,[qdisc %p],opt %p)\n", __func__, sch, p, opt);
355 if (!opt)
356 goto errout;
358 err = nla_parse_nested(tb, TCA_DSMARK_MAX, opt, dsmark_policy);
359 if (err < 0)
360 goto errout;
362 err = -EINVAL;
363 indices = nla_get_u16(tb[TCA_DSMARK_INDICES]);
365 if (hweight32(indices) != 1)
366 goto errout;
368 if (tb[TCA_DSMARK_DEFAULT_INDEX])
369 default_index = nla_get_u16(tb[TCA_DSMARK_DEFAULT_INDEX]);
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;
379 for (i = 0; i < indices; i++) {
380 p->mv[i].mask = 0xff;
381 p->mv[i].value = 0;
383 p->indices = indices;
384 p->default_index = default_index;
385 p->set_tc_index = nla_get_flag(tb[TCA_DSMARK_SET_TC_INDEX]);
387 p->q = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops, sch->handle);
388 if (p->q == NULL)
389 p->q = &noop_qdisc;
391 pr_debug("%s: qdisc %p\n", __func__, p->q);
393 err = 0;
394 errout:
395 return err;
398 static void dsmark_reset(struct Qdisc *sch)
400 struct dsmark_qdisc_data *p = qdisc_priv(sch);
402 pr_debug("%s(sch %p,[qdisc %p])\n", __func__, sch, p);
403 qdisc_reset(p->q);
404 sch->q.qlen = 0;
407 static void dsmark_destroy(struct Qdisc *sch)
409 struct dsmark_qdisc_data *p = qdisc_priv(sch);
411 pr_debug("%s(sch %p,[qdisc %p])\n", __func__, sch, p);
413 tcf_destroy_chain(&p->filter_list);
414 qdisc_destroy(p->q);
415 if (p->mv != p->embedded)
416 kfree(p->mv);
419 static int dsmark_dump_class(struct Qdisc *sch, unsigned long cl,
420 struct sk_buff *skb, struct tcmsg *tcm)
422 struct dsmark_qdisc_data *p = qdisc_priv(sch);
423 struct nlattr *opts = NULL;
425 pr_debug("%s(sch %p,[qdisc %p],class %ld\n", __func__, sch, p, cl);
427 if (!dsmark_valid_index(p, cl))
428 return -EINVAL;
430 tcm->tcm_handle = TC_H_MAKE(TC_H_MAJ(sch->handle), cl - 1);
431 tcm->tcm_info = p->q->handle;
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;
440 return nla_nest_end(skb, opts);
442 nla_put_failure:
443 nla_nest_cancel(skb, opts);
444 return -EMSGSIZE;
447 static int dsmark_dump(struct Qdisc *sch, struct sk_buff *skb)
449 struct dsmark_qdisc_data *p = qdisc_priv(sch);
450 struct nlattr *opts = NULL;
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;
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;
462 if (p->set_tc_index &&
463 nla_put_flag(skb, TCA_DSMARK_SET_TC_INDEX))
464 goto nla_put_failure;
466 return nla_nest_end(skb, opts);
468 nla_put_failure:
469 nla_nest_cancel(skb, opts);
470 return -EMSGSIZE;
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,
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,
504 static int __init dsmark_module_init(void)
506 return register_qdisc(&dsmark_qdisc_ops);
509 static void __exit dsmark_module_exit(void)
511 unregister_qdisc(&dsmark_qdisc_ops);
514 module_init(dsmark_module_init)
515 module_exit(dsmark_module_exit)
517 MODULE_LICENSE("GPL");