]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - net/sched/sch_dsmark.c
netlink: allow sending extended ACK with cookie on success
[mirror_ubuntu-artful-kernel.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 15#include <net/pkt_sched.h>
cf1facda 16#include <net/pkt_cls.h>
1da177e4
LT
17#include <net/dsfield.h>
18#include <net/inet_ecn.h>
19#include <asm/byteorder.h>
20
1da177e4
LT
21/*
22 * classid class marking
23 * ------- ----- -------
24 * n/a 0 n/a
25 * x:0 1 use entry [0]
26 * ... ... ...
27 * x:y y>0 y+1 use entry [y]
28 * ... ... ...
29 * x:indices-1 indices use entry [indices-1]
30 * ... ... ...
31 * x:y y+1 use entry [y & (indices-1)]
32 * ... ... ...
33 * 0xffff 0x10000 use entry [indices-1]
34 */
35
36
37#define NO_DEFAULT_INDEX (1 << 16)
38
47bbbb30
ED
39struct mask_value {
40 u8 mask;
41 u8 value;
42};
43
1da177e4
LT
44struct dsmark_qdisc_data {
45 struct Qdisc *q;
25d8c0d5 46 struct tcf_proto __rcu *filter_list;
47bbbb30 47 struct mask_value *mv;
af0d1141 48 u16 indices;
47bbbb30 49 u8 set_tc_index;
af0d1141 50 u32 default_index; /* index range is 0...0xffff */
47bbbb30
ED
51#define DSMARK_EMBEDDED_SZ 16
52 struct mask_value embedded[DSMARK_EMBEDDED_SZ];
1da177e4
LT
53};
54
758cc43c
TG
55static inline int dsmark_valid_index(struct dsmark_qdisc_data *p, u16 index)
56{
17569fae 57 return index <= p->indices && index > 0;
758cc43c 58}
1da177e4
LT
59
60/* ------------------------- Class/flow operations ------------------------- */
61
af0d1141
TG
62static int dsmark_graft(struct Qdisc *sch, unsigned long arg,
63 struct Qdisc *new, struct Qdisc **old)
1da177e4 64{
81da99ed 65 struct dsmark_qdisc_data *p = qdisc_priv(sch);
1da177e4 66
c76f2a2c
YY
67 pr_debug("%s(sch %p,[qdisc %p],new %p,old %p)\n",
68 __func__, sch, p, new, old);
486b53e5
TG
69
70 if (new == NULL) {
3511c913 71 new = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops,
9f9afec4 72 sch->handle);
486b53e5
TG
73 if (new == NULL)
74 new = &noop_qdisc;
75 }
76
86a7996c 77 *old = qdisc_replace(sch, new, &p->q);
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{
c76f2a2c
YY
89 pr_debug("%s(sch %p,[qdisc %p],classid %x)\n",
90 __func__, 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 119 int err = -EINVAL;
1da177e4 120
c76f2a2c
YY
121 pr_debug("%s(sch %p,[qdisc %p],classid %x,parent %x), arg 0x%lx\n",
122 __func__, sch, p, classid, parent, *arg);
758cc43c
TG
123
124 if (!dsmark_valid_index(p, *arg)) {
125 err = -ENOENT;
1e90474c 126 goto errout;
1da177e4 127 }
1da177e4 128
cee63723 129 if (!opt)
1e90474c 130 goto errout;
758cc43c 131
27a3421e 132 err = nla_parse_nested(tb, TCA_DSMARK_MAX, opt, dsmark_policy);
cee63723 133 if (err < 0)
27a3421e 134 goto errout;
cee63723 135
27a3421e 136 if (tb[TCA_DSMARK_VALUE])
47bbbb30 137 p->mv[*arg - 1].value = nla_get_u8(tb[TCA_DSMARK_VALUE]);
10297b99 138
1e90474c 139 if (tb[TCA_DSMARK_MASK])
47bbbb30 140 p->mv[*arg - 1].mask = nla_get_u8(tb[TCA_DSMARK_MASK]);
758cc43c
TG
141
142 err = 0;
143
1e90474c 144errout:
758cc43c
TG
145 return err;
146}
1da177e4 147
af0d1141 148static int dsmark_delete(struct Qdisc *sch, unsigned long arg)
1da177e4 149{
81da99ed 150 struct dsmark_qdisc_data *p = qdisc_priv(sch);
1da177e4 151
af0d1141 152 if (!dsmark_valid_index(p, arg))
1da177e4 153 return -EINVAL;
10297b99 154
47bbbb30
ED
155 p->mv[arg - 1].mask = 0xff;
156 p->mv[arg - 1].value = 0;
af0d1141 157
1da177e4
LT
158 return 0;
159}
160
9d127fbd 161static void dsmark_walk(struct Qdisc *sch, struct qdisc_walker *walker)
1da177e4 162{
81da99ed 163 struct dsmark_qdisc_data *p = qdisc_priv(sch);
1da177e4
LT
164 int i;
165
c76f2a2c
YY
166 pr_debug("%s(sch %p,[qdisc %p],walker %p)\n",
167 __func__, sch, p, walker);
af0d1141 168
1da177e4
LT
169 if (walker->stop)
170 return;
af0d1141 171
1da177e4 172 for (i = 0; i < p->indices; i++) {
47bbbb30 173 if (p->mv[i].mask == 0xff && !p->mv[i].value)
0451eb07 174 goto ignore;
1da177e4 175 if (walker->count >= walker->skip) {
cc7ec456 176 if (walker->fn(sch, i + 1, walker) < 0) {
1da177e4
LT
177 walker->stop = 1;
178 break;
179 }
180 }
10297b99 181ignore:
0451eb07 182 walker->count++;
10297b99 183 }
1da177e4
LT
184}
185
25d8c0d5
JF
186static inline struct tcf_proto __rcu **dsmark_find_tcf(struct Qdisc *sch,
187 unsigned long cl)
1da177e4 188{
81da99ed
SH
189 struct dsmark_qdisc_data *p = qdisc_priv(sch);
190 return &p->filter_list;
1da177e4
LT
191}
192
1da177e4
LT
193/* --------------------------- Qdisc operations ---------------------------- */
194
520ac30f
ED
195static int dsmark_enqueue(struct sk_buff *skb, struct Qdisc *sch,
196 struct sk_buff **to_free)
1da177e4 197{
81da99ed 198 struct dsmark_qdisc_data *p = qdisc_priv(sch);
af0d1141
TG
199 int err;
200
c76f2a2c 201 pr_debug("%s(skb %p,sch %p,[qdisc %p])\n", __func__, skb, sch, p);
1da177e4 202
1da177e4 203 if (p->set_tc_index) {
aea92fb2
ED
204 int wlen = skb_network_offset(skb);
205
d8b9605d 206 switch (tc_skb_protocol(skb)) {
60678040 207 case htons(ETH_P_IP):
aea92fb2
ED
208 wlen += sizeof(struct iphdr);
209 if (!pskb_may_pull(skb, wlen) ||
210 skb_try_make_writable(skb, wlen))
9d127fbd 211 goto drop;
4c30719f 212
9d127fbd
SH
213 skb->tc_index = ipv4_get_dsfield(ip_hdr(skb))
214 & ~INET_ECN_MASK;
215 break;
4c30719f 216
60678040 217 case htons(ETH_P_IPV6):
aea92fb2
ED
218 wlen += sizeof(struct ipv6hdr);
219 if (!pskb_may_pull(skb, wlen) ||
220 skb_try_make_writable(skb, wlen))
9d127fbd 221 goto drop;
4c30719f 222
9d127fbd
SH
223 skb->tc_index = ipv6_get_dsfield(ipv6_hdr(skb))
224 & ~INET_ECN_MASK;
225 break;
226 default:
227 skb->tc_index = 0;
228 break;
3ff50b79 229 }
1da177e4 230 }
af0d1141
TG
231
232 if (TC_H_MAJ(skb->priority) == sch->handle)
1da177e4 233 skb->tc_index = TC_H_MIN(skb->priority);
af0d1141
TG
234 else {
235 struct tcf_result res;
25d8c0d5 236 struct tcf_proto *fl = rcu_dereference_bh(p->filter_list);
3b3ae880 237 int result = tc_classify(skb, fl, &res, false);
af0d1141 238
81da99ed 239 pr_debug("result %d class 0x%04x\n", result, res.classid);
af0d1141 240
1da177e4 241 switch (result) {
f6853e2d
PM
242#ifdef CONFIG_NET_CLS_ACT
243 case TC_ACT_QUEUED:
244 case TC_ACT_STOLEN:
520ac30f 245 __qdisc_drop(skb, to_free);
378a2f09 246 return NET_XMIT_SUCCESS | __NET_XMIT_STOLEN;
4c30719f 247
f6853e2d 248 case TC_ACT_SHOT:
4c30719f 249 goto drop;
1da177e4 250#endif
c3bc7cff 251 case TC_ACT_OK:
f6853e2d
PM
252 skb->tc_index = TC_H_MIN(res.classid);
253 break;
4c30719f 254
f6853e2d
PM
255 default:
256 if (p->default_index != NO_DEFAULT_INDEX)
257 skb->tc_index = p->default_index;
258 break;
3ff50b79 259 }
1da177e4 260 }
1da177e4 261
520ac30f 262 err = qdisc_enqueue(skb, p->q, to_free);
af0d1141 263 if (err != NET_XMIT_SUCCESS) {
378a2f09 264 if (net_xmit_drop_count(err))
25331d6c 265 qdisc_qstats_drop(sch);
af0d1141 266 return err;
1da177e4 267 }
af0d1141 268
bdf17661 269 qdisc_qstats_backlog_inc(sch, skb);
1da177e4 270 sch->q.qlen++;
1da177e4 271
af0d1141 272 return NET_XMIT_SUCCESS;
4c30719f
SH
273
274drop:
520ac30f 275 qdisc_drop(skb, sch, to_free);
c27f339a 276 return NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
af0d1141 277}
1da177e4
LT
278
279static struct sk_buff *dsmark_dequeue(struct Qdisc *sch)
280{
81da99ed 281 struct dsmark_qdisc_data *p = qdisc_priv(sch);
1da177e4 282 struct sk_buff *skb;
af0d1141
TG
283 u32 index;
284
c76f2a2c 285 pr_debug("%s(sch %p,[qdisc %p])\n", __func__, sch, p);
1da177e4 286
f8b33d8e 287 skb = qdisc_dequeue_peeked(p->q);
af0d1141 288 if (skb == NULL)
1da177e4 289 return NULL;
af0d1141 290
9190b3b3 291 qdisc_bstats_update(sch, skb);
bdf17661 292 qdisc_qstats_backlog_dec(sch, skb);
1da177e4 293 sch->q.qlen--;
af0d1141
TG
294
295 index = skb->tc_index & (p->indices - 1);
81da99ed 296 pr_debug("index %d->%d\n", skb->tc_index, index);
af0d1141 297
d8b9605d 298 switch (tc_skb_protocol(skb)) {
60678040 299 case htons(ETH_P_IP):
47bbbb30
ED
300 ipv4_change_dsfield(ip_hdr(skb), p->mv[index].mask,
301 p->mv[index].value);
1da177e4 302 break;
60678040 303 case htons(ETH_P_IPV6):
47bbbb30
ED
304 ipv6_change_dsfield(ipv6_hdr(skb), p->mv[index].mask,
305 p->mv[index].value);
1da177e4 306 break;
9d127fbd
SH
307 default:
308 /*
309 * Only complain if a change was actually attempted.
310 * This way, we can send non-IP traffic through dsmark
311 * and don't need yet another qdisc as a bypass.
312 */
47bbbb30 313 if (p->mv[index].mask != 0xff || p->mv[index].value)
c76f2a2c 314 pr_warn("%s: unsupported protocol %d\n",
d8b9605d 315 __func__, ntohs(tc_skb_protocol(skb)));
9d127fbd 316 break;
3ff50b79 317 }
af0d1141 318
1da177e4
LT
319 return skb;
320}
321
8e3af978
JP
322static struct sk_buff *dsmark_peek(struct Qdisc *sch)
323{
324 struct dsmark_qdisc_data *p = qdisc_priv(sch);
325
c76f2a2c 326 pr_debug("%s(sch %p,[qdisc %p])\n", __func__, sch, p);
8e3af978
JP
327
328 return p->q->ops->peek(p->q);
329}
330
1e90474c 331static int dsmark_init(struct Qdisc *sch, struct nlattr *opt)
1da177e4 332{
81da99ed 333 struct dsmark_qdisc_data *p = qdisc_priv(sch);
1e90474c 334 struct nlattr *tb[TCA_DSMARK_MAX + 1];
758cc43c
TG
335 int err = -EINVAL;
336 u32 default_index = NO_DEFAULT_INDEX;
337 u16 indices;
47bbbb30 338 int i;
758cc43c 339
c76f2a2c 340 pr_debug("%s(sch %p,[qdisc %p],opt %p)\n", __func__, sch, p, opt);
758cc43c 341
cee63723
PM
342 if (!opt)
343 goto errout;
344
27a3421e 345 err = nla_parse_nested(tb, TCA_DSMARK_MAX, opt, dsmark_policy);
cee63723 346 if (err < 0)
758cc43c
TG
347 goto errout;
348
cee63723 349 err = -EINVAL;
1e90474c 350 indices = nla_get_u16(tb[TCA_DSMARK_INDICES]);
5b0ac72b
DM
351
352 if (hweight32(indices) != 1)
758cc43c
TG
353 goto errout;
354
27a3421e 355 if (tb[TCA_DSMARK_DEFAULT_INDEX])
1e90474c 356 default_index = nla_get_u16(tb[TCA_DSMARK_DEFAULT_INDEX]);
758cc43c 357
47bbbb30
ED
358 if (indices <= DSMARK_EMBEDDED_SZ)
359 p->mv = p->embedded;
360 else
361 p->mv = kmalloc_array(indices, sizeof(*p->mv), GFP_KERNEL);
362 if (!p->mv) {
758cc43c
TG
363 err = -ENOMEM;
364 goto errout;
1da177e4 365 }
47bbbb30
ED
366 for (i = 0; i < indices; i++) {
367 p->mv[i].mask = 0xff;
368 p->mv[i].value = 0;
369 }
758cc43c
TG
370 p->indices = indices;
371 p->default_index = default_index;
1e90474c 372 p->set_tc_index = nla_get_flag(tb[TCA_DSMARK_SET_TC_INDEX]);
758cc43c 373
3511c913 374 p->q = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops, sch->handle);
758cc43c 375 if (p->q == NULL)
1da177e4 376 p->q = &noop_qdisc;
49b49971
JK
377 else
378 qdisc_hash_add(p->q, true);
758cc43c 379
c76f2a2c 380 pr_debug("%s: qdisc %p\n", __func__, p->q);
758cc43c
TG
381
382 err = 0;
383errout:
758cc43c 384 return err;
1da177e4
LT
385}
386
1da177e4
LT
387static void dsmark_reset(struct Qdisc *sch)
388{
81da99ed 389 struct dsmark_qdisc_data *p = qdisc_priv(sch);
1da177e4 390
c76f2a2c 391 pr_debug("%s(sch %p,[qdisc %p])\n", __func__, sch, p);
1da177e4 392 qdisc_reset(p->q);
bdf17661 393 sch->qstats.backlog = 0;
1da177e4
LT
394 sch->q.qlen = 0;
395}
396
1da177e4
LT
397static void dsmark_destroy(struct Qdisc *sch)
398{
81da99ed 399 struct dsmark_qdisc_data *p = qdisc_priv(sch);
1da177e4 400
c76f2a2c 401 pr_debug("%s(sch %p,[qdisc %p])\n", __func__, sch, p);
af0d1141 402
ff31ab56 403 tcf_destroy_chain(&p->filter_list);
1da177e4 404 qdisc_destroy(p->q);
47bbbb30
ED
405 if (p->mv != p->embedded)
406 kfree(p->mv);
1da177e4
LT
407}
408
1da177e4 409static int dsmark_dump_class(struct Qdisc *sch, unsigned long cl,
02f23f09 410 struct sk_buff *skb, struct tcmsg *tcm)
1da177e4 411{
81da99ed 412 struct dsmark_qdisc_data *p = qdisc_priv(sch);
1e90474c 413 struct nlattr *opts = NULL;
1da177e4 414
c76f2a2c 415 pr_debug("%s(sch %p,[qdisc %p],class %ld\n", __func__, sch, p, cl);
02f23f09
TG
416
417 if (!dsmark_valid_index(p, cl))
1da177e4 418 return -EINVAL;
02f23f09 419
cc7ec456 420 tcm->tcm_handle = TC_H_MAKE(TC_H_MAJ(sch->handle), cl - 1);
cdc7f8e3 421 tcm->tcm_info = p->q->handle;
02f23f09 422
1e90474c
PM
423 opts = nla_nest_start(skb, TCA_OPTIONS);
424 if (opts == NULL)
425 goto nla_put_failure;
47bbbb30
ED
426 if (nla_put_u8(skb, TCA_DSMARK_MASK, p->mv[cl - 1].mask) ||
427 nla_put_u8(skb, TCA_DSMARK_VALUE, p->mv[cl - 1].value))
1b34ec43 428 goto nla_put_failure;
02f23f09 429
1e90474c 430 return nla_nest_end(skb, opts);
1da177e4 431
1e90474c 432nla_put_failure:
bc3ed28c
TG
433 nla_nest_cancel(skb, opts);
434 return -EMSGSIZE;
1da177e4
LT
435}
436
437static int dsmark_dump(struct Qdisc *sch, struct sk_buff *skb)
438{
81da99ed 439 struct dsmark_qdisc_data *p = qdisc_priv(sch);
1e90474c 440 struct nlattr *opts = NULL;
1da177e4 441
1e90474c
PM
442 opts = nla_nest_start(skb, TCA_OPTIONS);
443 if (opts == NULL)
444 goto nla_put_failure;
1b34ec43
DM
445 if (nla_put_u16(skb, TCA_DSMARK_INDICES, p->indices))
446 goto nla_put_failure;
02f23f09 447
1b34ec43
DM
448 if (p->default_index != NO_DEFAULT_INDEX &&
449 nla_put_u16(skb, TCA_DSMARK_DEFAULT_INDEX, p->default_index))
450 goto nla_put_failure;
1da177e4 451
1b34ec43
DM
452 if (p->set_tc_index &&
453 nla_put_flag(skb, TCA_DSMARK_SET_TC_INDEX))
454 goto nla_put_failure;
02f23f09 455
1e90474c 456 return nla_nest_end(skb, opts);
1da177e4 457
1e90474c 458nla_put_failure:
bc3ed28c
TG
459 nla_nest_cancel(skb, opts);
460 return -EMSGSIZE;
1da177e4
LT
461}
462
20fea08b 463static const struct Qdisc_class_ops dsmark_class_ops = {
1da177e4
LT
464 .graft = dsmark_graft,
465 .leaf = dsmark_leaf,
466 .get = dsmark_get,
467 .put = dsmark_put,
468 .change = dsmark_change,
469 .delete = dsmark_delete,
470 .walk = dsmark_walk,
471 .tcf_chain = dsmark_find_tcf,
472 .bind_tcf = dsmark_bind_filter,
473 .unbind_tcf = dsmark_put,
474 .dump = dsmark_dump_class,
475};
476
20fea08b 477static struct Qdisc_ops dsmark_qdisc_ops __read_mostly = {
1da177e4
LT
478 .next = NULL,
479 .cl_ops = &dsmark_class_ops,
480 .id = "dsmark",
481 .priv_size = sizeof(struct dsmark_qdisc_data),
482 .enqueue = dsmark_enqueue,
483 .dequeue = dsmark_dequeue,
8e3af978 484 .peek = dsmark_peek,
1da177e4
LT
485 .init = dsmark_init,
486 .reset = dsmark_reset,
487 .destroy = dsmark_destroy,
488 .change = NULL,
489 .dump = dsmark_dump,
490 .owner = THIS_MODULE,
491};
492
493static int __init dsmark_module_init(void)
494{
495 return register_qdisc(&dsmark_qdisc_ops);
496}
af0d1141 497
10297b99 498static void __exit dsmark_module_exit(void)
1da177e4
LT
499{
500 unregister_qdisc(&dsmark_qdisc_ops);
501}
af0d1141 502
1da177e4
LT
503module_init(dsmark_module_init)
504module_exit(dsmark_module_exit)
af0d1141 505
1da177e4 506MODULE_LICENSE("GPL");