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 * ------- ----- -------
26 * x:y y>0 y+1 use entry [y]
28 * x:indices-1 indices use entry [indices-1]
30 * x:y y+1 use entry [y & (indices-1)]
32 * 0xffff 0x10000 use entry [indices-1]
36 #define NO_DEFAULT_INDEX (1 << 16)
43 struct dsmark_qdisc_data {
45 struct tcf_proto __rcu *filter_list;
46 struct mask_value *mv;
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);
70 new = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops,
79 qdisc_tree_decrease_qlen(*old, (*old)->q.qlen);
86 static struct Qdisc *dsmark_leaf(struct Qdisc *sch, unsigned long arg)
88 struct dsmark_qdisc_data *p = qdisc_priv(sch);
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];
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)) {
137 err = nla_parse_nested(tb, TCA_DSMARK_MAX, opt, dsmark_policy);
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]);
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))
160 p->mv[arg - 1].mask = 0xff;
161 p->mv[arg - 1].value = 0;
166 static void dsmark_walk(struct Qdisc *sch, struct qdisc_walker *walker)
168 struct dsmark_qdisc_data *p = qdisc_priv(sch);
171 pr_debug("%s(sch %p,[qdisc %p],walker %p)\n",
172 __func__, sch, p, walker);
177 for (i = 0; i < p->indices; i++) {
178 if (p->mv[i].mask == 0xff && !p->mv[i].value)
180 if (walker->count >= walker->skip) {
181 if (walker->fn(sch, i + 1, walker) < 0) {
191 static inline struct tcf_proto __rcu **dsmark_find_tcf(struct Qdisc *sch,
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);
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)))
213 skb->tc_index = ipv4_get_dsfield(ip_hdr(skb))
217 case htons(ETH_P_IPV6):
218 if (skb_cow_head(skb, sizeof(struct ipv6hdr)))
221 skb->tc_index = ipv6_get_dsfield(ipv6_hdr(skb))
230 if (TC_H_MAJ(skb->priority) == sch->handle)
231 skb->tc_index = TC_H_MIN(skb->priority);
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);
240 #ifdef CONFIG_NET_CLS_ACT
244 return NET_XMIT_SUCCESS | __NET_XMIT_STOLEN;
250 skb->tc_index = TC_H_MIN(res.classid);
254 if (p->default_index != NO_DEFAULT_INDEX)
255 skb->tc_index = p->default_index;
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);
269 return NET_XMIT_SUCCESS;
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);
282 pr_debug("%s(sch %p,[qdisc %p])\n", __func__, sch, p);
284 skb = p->q->ops->dequeue(p->q);
288 qdisc_bstats_update(sch, skb);
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,
299 case htons(ETH_P_IPV6):
300 ipv6_change_dsfield(ipv6_hdr(skb), p->mv[index].mask,
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)));
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);
332 pr_debug("%s(sch %p,[qdisc %p])\n", __func__, sch, p);
334 if (p->q->ops->drop == NULL)
337 len = p->q->ops->drop(p->q);
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];
349 u32 default_index = NO_DEFAULT_INDEX;
353 pr_debug("%s(sch %p,[qdisc %p],opt %p)\n", __func__, sch, p, opt);
358 err = nla_parse_nested(tb, TCA_DSMARK_MAX, opt, dsmark_policy);
363 indices = nla_get_u16(tb[TCA_DSMARK_INDICES]);
365 if (hweight32(indices) != 1)
368 if (tb[TCA_DSMARK_DEFAULT_INDEX])
369 default_index = nla_get_u16(tb[TCA_DSMARK_DEFAULT_INDEX]);
371 if (indices <= DSMARK_EMBEDDED_SZ)
374 p->mv = kmalloc_array(indices, sizeof(*p->mv), GFP_KERNEL);
379 for (i = 0; i < indices; i++) {
380 p->mv[i].mask = 0xff;
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);
391 pr_debug("%s: qdisc %p\n", __func__, p->q);
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);
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);
415 if (p->mv != p->embedded)
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))
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);
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);
443 nla_nest_cancel(skb, opts);
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);
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);
469 nla_nest_cancel(skb, opts);
473 static const struct Qdisc_class_ops dsmark_class_ops = {
474 .graft = dsmark_graft,
478 .change = dsmark_change,
479 .delete = dsmark_delete,
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 = {
489 .cl_ops = &dsmark_class_ops,
491 .priv_size = sizeof(struct dsmark_qdisc_data),
492 .enqueue = dsmark_enqueue,
493 .dequeue = dsmark_dequeue,
497 .reset = dsmark_reset,
498 .destroy = dsmark_destroy,
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");