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