]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - net/sched/sch_dsmark.c
tun: fix inability to set offloads after disabling them via ethtool
[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) {
d8b9605d 204 switch (tc_skb_protocol(skb)) {
60678040 205 case htons(ETH_P_IP):
9d127fbd
SH
206 if (skb_cow_head(skb, sizeof(struct iphdr)))
207 goto drop;
4c30719f 208
9d127fbd
SH
209 skb->tc_index = ipv4_get_dsfield(ip_hdr(skb))
210 & ~INET_ECN_MASK;
211 break;
4c30719f 212
60678040 213 case htons(ETH_P_IPV6):
9d127fbd
SH
214 if (skb_cow_head(skb, sizeof(struct ipv6hdr)))
215 goto drop;
4c30719f 216
9d127fbd
SH
217 skb->tc_index = ipv6_get_dsfield(ipv6_hdr(skb))
218 & ~INET_ECN_MASK;
219 break;
220 default:
221 skb->tc_index = 0;
222 break;
3ff50b79 223 }
1da177e4 224 }
af0d1141
TG
225
226 if (TC_H_MAJ(skb->priority) == sch->handle)
1da177e4 227 skb->tc_index = TC_H_MIN(skb->priority);
af0d1141
TG
228 else {
229 struct tcf_result res;
25d8c0d5 230 struct tcf_proto *fl = rcu_dereference_bh(p->filter_list);
3b3ae880 231 int result = tc_classify(skb, fl, &res, false);
af0d1141 232
81da99ed 233 pr_debug("result %d class 0x%04x\n", result, res.classid);
af0d1141 234
1da177e4 235 switch (result) {
f6853e2d
PM
236#ifdef CONFIG_NET_CLS_ACT
237 case TC_ACT_QUEUED:
238 case TC_ACT_STOLEN:
520ac30f 239 __qdisc_drop(skb, to_free);
378a2f09 240 return NET_XMIT_SUCCESS | __NET_XMIT_STOLEN;
4c30719f 241
f6853e2d 242 case TC_ACT_SHOT:
4c30719f 243 goto drop;
1da177e4 244#endif
c3bc7cff 245 case TC_ACT_OK:
f6853e2d
PM
246 skb->tc_index = TC_H_MIN(res.classid);
247 break;
4c30719f 248
f6853e2d
PM
249 default:
250 if (p->default_index != NO_DEFAULT_INDEX)
251 skb->tc_index = p->default_index;
252 break;
3ff50b79 253 }
1da177e4 254 }
1da177e4 255
520ac30f 256 err = qdisc_enqueue(skb, p->q, to_free);
af0d1141 257 if (err != NET_XMIT_SUCCESS) {
378a2f09 258 if (net_xmit_drop_count(err))
25331d6c 259 qdisc_qstats_drop(sch);
af0d1141 260 return err;
1da177e4 261 }
af0d1141 262
bdf17661 263 qdisc_qstats_backlog_inc(sch, skb);
1da177e4 264 sch->q.qlen++;
1da177e4 265
af0d1141 266 return NET_XMIT_SUCCESS;
4c30719f
SH
267
268drop:
520ac30f 269 qdisc_drop(skb, sch, to_free);
c27f339a 270 return NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
af0d1141 271}
1da177e4
LT
272
273static struct sk_buff *dsmark_dequeue(struct Qdisc *sch)
274{
81da99ed 275 struct dsmark_qdisc_data *p = qdisc_priv(sch);
1da177e4 276 struct sk_buff *skb;
af0d1141
TG
277 u32 index;
278
c76f2a2c 279 pr_debug("%s(sch %p,[qdisc %p])\n", __func__, sch, p);
1da177e4 280
f8b33d8e 281 skb = qdisc_dequeue_peeked(p->q);
af0d1141 282 if (skb == NULL)
1da177e4 283 return NULL;
af0d1141 284
9190b3b3 285 qdisc_bstats_update(sch, skb);
bdf17661 286 qdisc_qstats_backlog_dec(sch, skb);
1da177e4 287 sch->q.qlen--;
af0d1141
TG
288
289 index = skb->tc_index & (p->indices - 1);
81da99ed 290 pr_debug("index %d->%d\n", skb->tc_index, index);
af0d1141 291
d8b9605d 292 switch (tc_skb_protocol(skb)) {
60678040 293 case htons(ETH_P_IP):
47bbbb30
ED
294 ipv4_change_dsfield(ip_hdr(skb), p->mv[index].mask,
295 p->mv[index].value);
1da177e4 296 break;
60678040 297 case htons(ETH_P_IPV6):
47bbbb30
ED
298 ipv6_change_dsfield(ipv6_hdr(skb), p->mv[index].mask,
299 p->mv[index].value);
1da177e4 300 break;
9d127fbd
SH
301 default:
302 /*
303 * Only complain if a change was actually attempted.
304 * This way, we can send non-IP traffic through dsmark
305 * and don't need yet another qdisc as a bypass.
306 */
47bbbb30 307 if (p->mv[index].mask != 0xff || p->mv[index].value)
c76f2a2c 308 pr_warn("%s: unsupported protocol %d\n",
d8b9605d 309 __func__, ntohs(tc_skb_protocol(skb)));
9d127fbd 310 break;
3ff50b79 311 }
af0d1141 312
1da177e4
LT
313 return skb;
314}
315
8e3af978
JP
316static struct sk_buff *dsmark_peek(struct Qdisc *sch)
317{
318 struct dsmark_qdisc_data *p = qdisc_priv(sch);
319
c76f2a2c 320 pr_debug("%s(sch %p,[qdisc %p])\n", __func__, sch, p);
8e3af978
JP
321
322 return p->q->ops->peek(p->q);
323}
324
1e90474c 325static int dsmark_init(struct Qdisc *sch, struct nlattr *opt)
1da177e4 326{
81da99ed 327 struct dsmark_qdisc_data *p = qdisc_priv(sch);
1e90474c 328 struct nlattr *tb[TCA_DSMARK_MAX + 1];
758cc43c
TG
329 int err = -EINVAL;
330 u32 default_index = NO_DEFAULT_INDEX;
331 u16 indices;
47bbbb30 332 int i;
758cc43c 333
c76f2a2c 334 pr_debug("%s(sch %p,[qdisc %p],opt %p)\n", __func__, sch, p, opt);
758cc43c 335
cee63723
PM
336 if (!opt)
337 goto errout;
338
27a3421e 339 err = nla_parse_nested(tb, TCA_DSMARK_MAX, opt, dsmark_policy);
cee63723 340 if (err < 0)
758cc43c
TG
341 goto errout;
342
cee63723 343 err = -EINVAL;
1e90474c 344 indices = nla_get_u16(tb[TCA_DSMARK_INDICES]);
5b0ac72b
DM
345
346 if (hweight32(indices) != 1)
758cc43c
TG
347 goto errout;
348
27a3421e 349 if (tb[TCA_DSMARK_DEFAULT_INDEX])
1e90474c 350 default_index = nla_get_u16(tb[TCA_DSMARK_DEFAULT_INDEX]);
758cc43c 351
47bbbb30
ED
352 if (indices <= DSMARK_EMBEDDED_SZ)
353 p->mv = p->embedded;
354 else
355 p->mv = kmalloc_array(indices, sizeof(*p->mv), GFP_KERNEL);
356 if (!p->mv) {
758cc43c
TG
357 err = -ENOMEM;
358 goto errout;
1da177e4 359 }
47bbbb30
ED
360 for (i = 0; i < indices; i++) {
361 p->mv[i].mask = 0xff;
362 p->mv[i].value = 0;
363 }
758cc43c
TG
364 p->indices = indices;
365 p->default_index = default_index;
1e90474c 366 p->set_tc_index = nla_get_flag(tb[TCA_DSMARK_SET_TC_INDEX]);
758cc43c 367
3511c913 368 p->q = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops, sch->handle);
758cc43c 369 if (p->q == NULL)
1da177e4 370 p->q = &noop_qdisc;
758cc43c 371
c76f2a2c 372 pr_debug("%s: qdisc %p\n", __func__, p->q);
758cc43c
TG
373
374 err = 0;
375errout:
758cc43c 376 return err;
1da177e4
LT
377}
378
1da177e4
LT
379static void dsmark_reset(struct Qdisc *sch)
380{
81da99ed 381 struct dsmark_qdisc_data *p = qdisc_priv(sch);
1da177e4 382
c76f2a2c 383 pr_debug("%s(sch %p,[qdisc %p])\n", __func__, sch, p);
1da177e4 384 qdisc_reset(p->q);
bdf17661 385 sch->qstats.backlog = 0;
1da177e4
LT
386 sch->q.qlen = 0;
387}
388
1da177e4
LT
389static void dsmark_destroy(struct Qdisc *sch)
390{
81da99ed 391 struct dsmark_qdisc_data *p = qdisc_priv(sch);
1da177e4 392
c76f2a2c 393 pr_debug("%s(sch %p,[qdisc %p])\n", __func__, sch, p);
af0d1141 394
ff31ab56 395 tcf_destroy_chain(&p->filter_list);
1da177e4 396 qdisc_destroy(p->q);
47bbbb30
ED
397 if (p->mv != p->embedded)
398 kfree(p->mv);
1da177e4
LT
399}
400
1da177e4 401static int dsmark_dump_class(struct Qdisc *sch, unsigned long cl,
02f23f09 402 struct sk_buff *skb, struct tcmsg *tcm)
1da177e4 403{
81da99ed 404 struct dsmark_qdisc_data *p = qdisc_priv(sch);
1e90474c 405 struct nlattr *opts = NULL;
1da177e4 406
c76f2a2c 407 pr_debug("%s(sch %p,[qdisc %p],class %ld\n", __func__, sch, p, cl);
02f23f09
TG
408
409 if (!dsmark_valid_index(p, cl))
1da177e4 410 return -EINVAL;
02f23f09 411
cc7ec456 412 tcm->tcm_handle = TC_H_MAKE(TC_H_MAJ(sch->handle), cl - 1);
cdc7f8e3 413 tcm->tcm_info = p->q->handle;
02f23f09 414
1e90474c
PM
415 opts = nla_nest_start(skb, TCA_OPTIONS);
416 if (opts == NULL)
417 goto nla_put_failure;
47bbbb30
ED
418 if (nla_put_u8(skb, TCA_DSMARK_MASK, p->mv[cl - 1].mask) ||
419 nla_put_u8(skb, TCA_DSMARK_VALUE, p->mv[cl - 1].value))
1b34ec43 420 goto nla_put_failure;
02f23f09 421
1e90474c 422 return nla_nest_end(skb, opts);
1da177e4 423
1e90474c 424nla_put_failure:
bc3ed28c
TG
425 nla_nest_cancel(skb, opts);
426 return -EMSGSIZE;
1da177e4
LT
427}
428
429static int dsmark_dump(struct Qdisc *sch, struct sk_buff *skb)
430{
81da99ed 431 struct dsmark_qdisc_data *p = qdisc_priv(sch);
1e90474c 432 struct nlattr *opts = NULL;
1da177e4 433
1e90474c
PM
434 opts = nla_nest_start(skb, TCA_OPTIONS);
435 if (opts == NULL)
436 goto nla_put_failure;
1b34ec43
DM
437 if (nla_put_u16(skb, TCA_DSMARK_INDICES, p->indices))
438 goto nla_put_failure;
02f23f09 439
1b34ec43
DM
440 if (p->default_index != NO_DEFAULT_INDEX &&
441 nla_put_u16(skb, TCA_DSMARK_DEFAULT_INDEX, p->default_index))
442 goto nla_put_failure;
1da177e4 443
1b34ec43
DM
444 if (p->set_tc_index &&
445 nla_put_flag(skb, TCA_DSMARK_SET_TC_INDEX))
446 goto nla_put_failure;
02f23f09 447
1e90474c 448 return nla_nest_end(skb, opts);
1da177e4 449
1e90474c 450nla_put_failure:
bc3ed28c
TG
451 nla_nest_cancel(skb, opts);
452 return -EMSGSIZE;
1da177e4
LT
453}
454
20fea08b 455static const struct Qdisc_class_ops dsmark_class_ops = {
1da177e4
LT
456 .graft = dsmark_graft,
457 .leaf = dsmark_leaf,
458 .get = dsmark_get,
459 .put = dsmark_put,
460 .change = dsmark_change,
461 .delete = dsmark_delete,
462 .walk = dsmark_walk,
463 .tcf_chain = dsmark_find_tcf,
464 .bind_tcf = dsmark_bind_filter,
465 .unbind_tcf = dsmark_put,
466 .dump = dsmark_dump_class,
467};
468
20fea08b 469static struct Qdisc_ops dsmark_qdisc_ops __read_mostly = {
1da177e4
LT
470 .next = NULL,
471 .cl_ops = &dsmark_class_ops,
472 .id = "dsmark",
473 .priv_size = sizeof(struct dsmark_qdisc_data),
474 .enqueue = dsmark_enqueue,
475 .dequeue = dsmark_dequeue,
8e3af978 476 .peek = dsmark_peek,
1da177e4
LT
477 .init = dsmark_init,
478 .reset = dsmark_reset,
479 .destroy = dsmark_destroy,
480 .change = NULL,
481 .dump = dsmark_dump,
482 .owner = THIS_MODULE,
483};
484
485static int __init dsmark_module_init(void)
486{
487 return register_qdisc(&dsmark_qdisc_ops);
488}
af0d1141 489
10297b99 490static void __exit dsmark_module_exit(void)
1da177e4
LT
491{
492 unregister_qdisc(&dsmark_qdisc_ops);
493}
af0d1141 494
1da177e4
LT
495module_init(dsmark_module_init)
496module_exit(dsmark_module_exit)
af0d1141 497
1da177e4 498MODULE_LICENSE("GPL");