]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame_incremental - net/sched/sch_api.c
net_sched: move dev_graft_qdisc() to sch_generic.c
[mirror_ubuntu-artful-kernel.git] / net / sched / sch_api.c
... / ...
CommitLineData
1/*
2 * net/sched/sch_api.c Packet scheduler API.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 *
11 * Fixes:
12 *
13 * Rani Assaf <rani@magic.metawire.com> :980802: JIFFIES and CPU clock sources are repaired.
14 * Eduardo J. Blanco <ejbs@netlabs.com.uy> :990222: kmod support
15 * Jamal Hadi Salim <hadi@nortelnetworks.com>: 990601: ingress support
16 */
17
18#include <linux/module.h>
19#include <linux/types.h>
20#include <linux/kernel.h>
21#include <linux/string.h>
22#include <linux/errno.h>
23#include <linux/skbuff.h>
24#include <linux/init.h>
25#include <linux/proc_fs.h>
26#include <linux/seq_file.h>
27#include <linux/kmod.h>
28#include <linux/list.h>
29#include <linux/hrtimer.h>
30#include <linux/lockdep.h>
31
32#include <net/net_namespace.h>
33#include <net/sock.h>
34#include <net/netlink.h>
35#include <net/pkt_sched.h>
36
37static int qdisc_notify(struct sk_buff *oskb, struct nlmsghdr *n, u32 clid,
38 struct Qdisc *old, struct Qdisc *new);
39static int tclass_notify(struct sk_buff *oskb, struct nlmsghdr *n,
40 struct Qdisc *q, unsigned long cl, int event);
41
42/*
43
44 Short review.
45 -------------
46
47 This file consists of two interrelated parts:
48
49 1. queueing disciplines manager frontend.
50 2. traffic classes manager frontend.
51
52 Generally, queueing discipline ("qdisc") is a black box,
53 which is able to enqueue packets and to dequeue them (when
54 device is ready to send something) in order and at times
55 determined by algorithm hidden in it.
56
57 qdisc's are divided to two categories:
58 - "queues", which have no internal structure visible from outside.
59 - "schedulers", which split all the packets to "traffic classes",
60 using "packet classifiers" (look at cls_api.c)
61
62 In turn, classes may have child qdiscs (as rule, queues)
63 attached to them etc. etc. etc.
64
65 The goal of the routines in this file is to translate
66 information supplied by user in the form of handles
67 to more intelligible for kernel form, to make some sanity
68 checks and part of work, which is common to all qdiscs
69 and to provide rtnetlink notifications.
70
71 All real intelligent work is done inside qdisc modules.
72
73
74
75 Every discipline has two major routines: enqueue and dequeue.
76
77 ---dequeue
78
79 dequeue usually returns a skb to send. It is allowed to return NULL,
80 but it does not mean that queue is empty, it just means that
81 discipline does not want to send anything this time.
82 Queue is really empty if q->q.qlen == 0.
83 For complicated disciplines with multiple queues q->q is not
84 real packet queue, but however q->q.qlen must be valid.
85
86 ---enqueue
87
88 enqueue returns 0, if packet was enqueued successfully.
89 If packet (this one or another one) was dropped, it returns
90 not zero error code.
91 NET_XMIT_DROP - this packet dropped
92 Expected action: do not backoff, but wait until queue will clear.
93 NET_XMIT_CN - probably this packet enqueued, but another one dropped.
94 Expected action: backoff or ignore
95 NET_XMIT_POLICED - dropped by police.
96 Expected action: backoff or error to real-time apps.
97
98 Auxiliary routines:
99
100 ---peek
101
102 like dequeue but without removing a packet from the queue
103
104 ---reset
105
106 returns qdisc to initial state: purge all buffers, clear all
107 timers, counters (except for statistics) etc.
108
109 ---init
110
111 initializes newly created qdisc.
112
113 ---destroy
114
115 destroys resources allocated by init and during lifetime of qdisc.
116
117 ---change
118
119 changes qdisc parameters.
120 */
121
122/* Protects list of registered TC modules. It is pure SMP lock. */
123static DEFINE_RWLOCK(qdisc_mod_lock);
124
125
126/************************************************
127 * Queueing disciplines manipulation. *
128 ************************************************/
129
130
131/* The list of all installed queueing disciplines. */
132
133static struct Qdisc_ops *qdisc_base;
134
135/* Register/uregister queueing discipline */
136
137int register_qdisc(struct Qdisc_ops *qops)
138{
139 struct Qdisc_ops *q, **qp;
140 int rc = -EEXIST;
141
142 write_lock(&qdisc_mod_lock);
143 for (qp = &qdisc_base; (q = *qp) != NULL; qp = &q->next)
144 if (!strcmp(qops->id, q->id))
145 goto out;
146
147 if (qops->enqueue == NULL)
148 qops->enqueue = noop_qdisc_ops.enqueue;
149 if (qops->peek == NULL) {
150 if (qops->dequeue == NULL) {
151 qops->peek = noop_qdisc_ops.peek;
152 } else {
153 rc = -EINVAL;
154 goto out;
155 }
156 }
157 if (qops->dequeue == NULL)
158 qops->dequeue = noop_qdisc_ops.dequeue;
159
160 qops->next = NULL;
161 *qp = qops;
162 rc = 0;
163out:
164 write_unlock(&qdisc_mod_lock);
165 return rc;
166}
167EXPORT_SYMBOL(register_qdisc);
168
169int unregister_qdisc(struct Qdisc_ops *qops)
170{
171 struct Qdisc_ops *q, **qp;
172 int err = -ENOENT;
173
174 write_lock(&qdisc_mod_lock);
175 for (qp = &qdisc_base; (q=*qp)!=NULL; qp = &q->next)
176 if (q == qops)
177 break;
178 if (q) {
179 *qp = q->next;
180 q->next = NULL;
181 err = 0;
182 }
183 write_unlock(&qdisc_mod_lock);
184 return err;
185}
186EXPORT_SYMBOL(unregister_qdisc);
187
188/* We know handle. Find qdisc among all qdisc's attached to device
189 (root qdisc, all its children, children of children etc.)
190 */
191
192static struct Qdisc *qdisc_match_from_root(struct Qdisc *root, u32 handle)
193{
194 struct Qdisc *q;
195
196 if (!(root->flags & TCQ_F_BUILTIN) &&
197 root->handle == handle)
198 return root;
199
200 list_for_each_entry(q, &root->list, list) {
201 if (q->handle == handle)
202 return q;
203 }
204 return NULL;
205}
206
207static void qdisc_list_add(struct Qdisc *q)
208{
209 if ((q->parent != TC_H_ROOT) && !(q->flags & TCQ_F_INGRESS))
210 list_add_tail(&q->list, &qdisc_dev(q)->qdisc->list);
211}
212
213void qdisc_list_del(struct Qdisc *q)
214{
215 if ((q->parent != TC_H_ROOT) && !(q->flags & TCQ_F_INGRESS))
216 list_del(&q->list);
217}
218EXPORT_SYMBOL(qdisc_list_del);
219
220struct Qdisc *qdisc_lookup(struct net_device *dev, u32 handle)
221{
222 struct Qdisc *q;
223
224 q = qdisc_match_from_root(dev->qdisc, handle);
225 if (q)
226 goto out;
227
228 q = qdisc_match_from_root(dev->rx_queue.qdisc_sleeping, handle);
229out:
230 return q;
231}
232
233static struct Qdisc *qdisc_leaf(struct Qdisc *p, u32 classid)
234{
235 unsigned long cl;
236 struct Qdisc *leaf;
237 const struct Qdisc_class_ops *cops = p->ops->cl_ops;
238
239 if (cops == NULL)
240 return NULL;
241 cl = cops->get(p, classid);
242
243 if (cl == 0)
244 return NULL;
245 leaf = cops->leaf(p, cl);
246 cops->put(p, cl);
247 return leaf;
248}
249
250/* Find queueing discipline by name */
251
252static struct Qdisc_ops *qdisc_lookup_ops(struct nlattr *kind)
253{
254 struct Qdisc_ops *q = NULL;
255
256 if (kind) {
257 read_lock(&qdisc_mod_lock);
258 for (q = qdisc_base; q; q = q->next) {
259 if (nla_strcmp(kind, q->id) == 0) {
260 if (!try_module_get(q->owner))
261 q = NULL;
262 break;
263 }
264 }
265 read_unlock(&qdisc_mod_lock);
266 }
267 return q;
268}
269
270static struct qdisc_rate_table *qdisc_rtab_list;
271
272struct qdisc_rate_table *qdisc_get_rtab(struct tc_ratespec *r, struct nlattr *tab)
273{
274 struct qdisc_rate_table *rtab;
275
276 for (rtab = qdisc_rtab_list; rtab; rtab = rtab->next) {
277 if (memcmp(&rtab->rate, r, sizeof(struct tc_ratespec)) == 0) {
278 rtab->refcnt++;
279 return rtab;
280 }
281 }
282
283 if (tab == NULL || r->rate == 0 || r->cell_log == 0 ||
284 nla_len(tab) != TC_RTAB_SIZE)
285 return NULL;
286
287 rtab = kmalloc(sizeof(*rtab), GFP_KERNEL);
288 if (rtab) {
289 rtab->rate = *r;
290 rtab->refcnt = 1;
291 memcpy(rtab->data, nla_data(tab), 1024);
292 rtab->next = qdisc_rtab_list;
293 qdisc_rtab_list = rtab;
294 }
295 return rtab;
296}
297EXPORT_SYMBOL(qdisc_get_rtab);
298
299void qdisc_put_rtab(struct qdisc_rate_table *tab)
300{
301 struct qdisc_rate_table *rtab, **rtabp;
302
303 if (!tab || --tab->refcnt)
304 return;
305
306 for (rtabp = &qdisc_rtab_list; (rtab=*rtabp) != NULL; rtabp = &rtab->next) {
307 if (rtab == tab) {
308 *rtabp = rtab->next;
309 kfree(rtab);
310 return;
311 }
312 }
313}
314EXPORT_SYMBOL(qdisc_put_rtab);
315
316static LIST_HEAD(qdisc_stab_list);
317static DEFINE_SPINLOCK(qdisc_stab_lock);
318
319static const struct nla_policy stab_policy[TCA_STAB_MAX + 1] = {
320 [TCA_STAB_BASE] = { .len = sizeof(struct tc_sizespec) },
321 [TCA_STAB_DATA] = { .type = NLA_BINARY },
322};
323
324static struct qdisc_size_table *qdisc_get_stab(struct nlattr *opt)
325{
326 struct nlattr *tb[TCA_STAB_MAX + 1];
327 struct qdisc_size_table *stab;
328 struct tc_sizespec *s;
329 unsigned int tsize = 0;
330 u16 *tab = NULL;
331 int err;
332
333 err = nla_parse_nested(tb, TCA_STAB_MAX, opt, stab_policy);
334 if (err < 0)
335 return ERR_PTR(err);
336 if (!tb[TCA_STAB_BASE])
337 return ERR_PTR(-EINVAL);
338
339 s = nla_data(tb[TCA_STAB_BASE]);
340
341 if (s->tsize > 0) {
342 if (!tb[TCA_STAB_DATA])
343 return ERR_PTR(-EINVAL);
344 tab = nla_data(tb[TCA_STAB_DATA]);
345 tsize = nla_len(tb[TCA_STAB_DATA]) / sizeof(u16);
346 }
347
348 if (!s || tsize != s->tsize || (!tab && tsize > 0))
349 return ERR_PTR(-EINVAL);
350
351 spin_lock(&qdisc_stab_lock);
352
353 list_for_each_entry(stab, &qdisc_stab_list, list) {
354 if (memcmp(&stab->szopts, s, sizeof(*s)))
355 continue;
356 if (tsize > 0 && memcmp(stab->data, tab, tsize * sizeof(u16)))
357 continue;
358 stab->refcnt++;
359 spin_unlock(&qdisc_stab_lock);
360 return stab;
361 }
362
363 spin_unlock(&qdisc_stab_lock);
364
365 stab = kmalloc(sizeof(*stab) + tsize * sizeof(u16), GFP_KERNEL);
366 if (!stab)
367 return ERR_PTR(-ENOMEM);
368
369 stab->refcnt = 1;
370 stab->szopts = *s;
371 if (tsize > 0)
372 memcpy(stab->data, tab, tsize * sizeof(u16));
373
374 spin_lock(&qdisc_stab_lock);
375 list_add_tail(&stab->list, &qdisc_stab_list);
376 spin_unlock(&qdisc_stab_lock);
377
378 return stab;
379}
380
381void qdisc_put_stab(struct qdisc_size_table *tab)
382{
383 if (!tab)
384 return;
385
386 spin_lock(&qdisc_stab_lock);
387
388 if (--tab->refcnt == 0) {
389 list_del(&tab->list);
390 kfree(tab);
391 }
392
393 spin_unlock(&qdisc_stab_lock);
394}
395EXPORT_SYMBOL(qdisc_put_stab);
396
397static int qdisc_dump_stab(struct sk_buff *skb, struct qdisc_size_table *stab)
398{
399 struct nlattr *nest;
400
401 nest = nla_nest_start(skb, TCA_STAB);
402 if (nest == NULL)
403 goto nla_put_failure;
404 NLA_PUT(skb, TCA_STAB_BASE, sizeof(stab->szopts), &stab->szopts);
405 nla_nest_end(skb, nest);
406
407 return skb->len;
408
409nla_put_failure:
410 return -1;
411}
412
413void qdisc_calculate_pkt_len(struct sk_buff *skb, struct qdisc_size_table *stab)
414{
415 int pkt_len, slot;
416
417 pkt_len = skb->len + stab->szopts.overhead;
418 if (unlikely(!stab->szopts.tsize))
419 goto out;
420
421 slot = pkt_len + stab->szopts.cell_align;
422 if (unlikely(slot < 0))
423 slot = 0;
424
425 slot >>= stab->szopts.cell_log;
426 if (likely(slot < stab->szopts.tsize))
427 pkt_len = stab->data[slot];
428 else
429 pkt_len = stab->data[stab->szopts.tsize - 1] *
430 (slot / stab->szopts.tsize) +
431 stab->data[slot % stab->szopts.tsize];
432
433 pkt_len <<= stab->szopts.size_log;
434out:
435 if (unlikely(pkt_len < 1))
436 pkt_len = 1;
437 qdisc_skb_cb(skb)->pkt_len = pkt_len;
438}
439EXPORT_SYMBOL(qdisc_calculate_pkt_len);
440
441void qdisc_warn_nonwc(char *txt, struct Qdisc *qdisc)
442{
443 if (!(qdisc->flags & TCQ_F_WARN_NONWC)) {
444 printk(KERN_WARNING
445 "%s: %s qdisc %X: is non-work-conserving?\n",
446 txt, qdisc->ops->id, qdisc->handle >> 16);
447 qdisc->flags |= TCQ_F_WARN_NONWC;
448 }
449}
450EXPORT_SYMBOL(qdisc_warn_nonwc);
451
452static enum hrtimer_restart qdisc_watchdog(struct hrtimer *timer)
453{
454 struct qdisc_watchdog *wd = container_of(timer, struct qdisc_watchdog,
455 timer);
456
457 wd->qdisc->flags &= ~TCQ_F_THROTTLED;
458 __netif_schedule(qdisc_root(wd->qdisc));
459
460 return HRTIMER_NORESTART;
461}
462
463void qdisc_watchdog_init(struct qdisc_watchdog *wd, struct Qdisc *qdisc)
464{
465 hrtimer_init(&wd->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
466 wd->timer.function = qdisc_watchdog;
467 wd->qdisc = qdisc;
468}
469EXPORT_SYMBOL(qdisc_watchdog_init);
470
471void qdisc_watchdog_schedule(struct qdisc_watchdog *wd, psched_time_t expires)
472{
473 ktime_t time;
474
475 if (test_bit(__QDISC_STATE_DEACTIVATED,
476 &qdisc_root_sleeping(wd->qdisc)->state))
477 return;
478
479 wd->qdisc->flags |= TCQ_F_THROTTLED;
480 time = ktime_set(0, 0);
481 time = ktime_add_ns(time, PSCHED_TICKS2NS(expires));
482 hrtimer_start(&wd->timer, time, HRTIMER_MODE_ABS);
483}
484EXPORT_SYMBOL(qdisc_watchdog_schedule);
485
486void qdisc_watchdog_cancel(struct qdisc_watchdog *wd)
487{
488 hrtimer_cancel(&wd->timer);
489 wd->qdisc->flags &= ~TCQ_F_THROTTLED;
490}
491EXPORT_SYMBOL(qdisc_watchdog_cancel);
492
493static struct hlist_head *qdisc_class_hash_alloc(unsigned int n)
494{
495 unsigned int size = n * sizeof(struct hlist_head), i;
496 struct hlist_head *h;
497
498 if (size <= PAGE_SIZE)
499 h = kmalloc(size, GFP_KERNEL);
500 else
501 h = (struct hlist_head *)
502 __get_free_pages(GFP_KERNEL, get_order(size));
503
504 if (h != NULL) {
505 for (i = 0; i < n; i++)
506 INIT_HLIST_HEAD(&h[i]);
507 }
508 return h;
509}
510
511static void qdisc_class_hash_free(struct hlist_head *h, unsigned int n)
512{
513 unsigned int size = n * sizeof(struct hlist_head);
514
515 if (size <= PAGE_SIZE)
516 kfree(h);
517 else
518 free_pages((unsigned long)h, get_order(size));
519}
520
521void qdisc_class_hash_grow(struct Qdisc *sch, struct Qdisc_class_hash *clhash)
522{
523 struct Qdisc_class_common *cl;
524 struct hlist_node *n, *next;
525 struct hlist_head *nhash, *ohash;
526 unsigned int nsize, nmask, osize;
527 unsigned int i, h;
528
529 /* Rehash when load factor exceeds 0.75 */
530 if (clhash->hashelems * 4 <= clhash->hashsize * 3)
531 return;
532 nsize = clhash->hashsize * 2;
533 nmask = nsize - 1;
534 nhash = qdisc_class_hash_alloc(nsize);
535 if (nhash == NULL)
536 return;
537
538 ohash = clhash->hash;
539 osize = clhash->hashsize;
540
541 sch_tree_lock(sch);
542 for (i = 0; i < osize; i++) {
543 hlist_for_each_entry_safe(cl, n, next, &ohash[i], hnode) {
544 h = qdisc_class_hash(cl->classid, nmask);
545 hlist_add_head(&cl->hnode, &nhash[h]);
546 }
547 }
548 clhash->hash = nhash;
549 clhash->hashsize = nsize;
550 clhash->hashmask = nmask;
551 sch_tree_unlock(sch);
552
553 qdisc_class_hash_free(ohash, osize);
554}
555EXPORT_SYMBOL(qdisc_class_hash_grow);
556
557int qdisc_class_hash_init(struct Qdisc_class_hash *clhash)
558{
559 unsigned int size = 4;
560
561 clhash->hash = qdisc_class_hash_alloc(size);
562 if (clhash->hash == NULL)
563 return -ENOMEM;
564 clhash->hashsize = size;
565 clhash->hashmask = size - 1;
566 clhash->hashelems = 0;
567 return 0;
568}
569EXPORT_SYMBOL(qdisc_class_hash_init);
570
571void qdisc_class_hash_destroy(struct Qdisc_class_hash *clhash)
572{
573 qdisc_class_hash_free(clhash->hash, clhash->hashsize);
574}
575EXPORT_SYMBOL(qdisc_class_hash_destroy);
576
577void qdisc_class_hash_insert(struct Qdisc_class_hash *clhash,
578 struct Qdisc_class_common *cl)
579{
580 unsigned int h;
581
582 INIT_HLIST_NODE(&cl->hnode);
583 h = qdisc_class_hash(cl->classid, clhash->hashmask);
584 hlist_add_head(&cl->hnode, &clhash->hash[h]);
585 clhash->hashelems++;
586}
587EXPORT_SYMBOL(qdisc_class_hash_insert);
588
589void qdisc_class_hash_remove(struct Qdisc_class_hash *clhash,
590 struct Qdisc_class_common *cl)
591{
592 hlist_del(&cl->hnode);
593 clhash->hashelems--;
594}
595EXPORT_SYMBOL(qdisc_class_hash_remove);
596
597/* Allocate an unique handle from space managed by kernel */
598
599static u32 qdisc_alloc_handle(struct net_device *dev)
600{
601 int i = 0x10000;
602 static u32 autohandle = TC_H_MAKE(0x80000000U, 0);
603
604 do {
605 autohandle += TC_H_MAKE(0x10000U, 0);
606 if (autohandle == TC_H_MAKE(TC_H_ROOT, 0))
607 autohandle = TC_H_MAKE(0x80000000U, 0);
608 } while (qdisc_lookup(dev, autohandle) && --i > 0);
609
610 return i>0 ? autohandle : 0;
611}
612
613void qdisc_tree_decrease_qlen(struct Qdisc *sch, unsigned int n)
614{
615 const struct Qdisc_class_ops *cops;
616 unsigned long cl;
617 u32 parentid;
618
619 if (n == 0)
620 return;
621 while ((parentid = sch->parent)) {
622 if (TC_H_MAJ(parentid) == TC_H_MAJ(TC_H_INGRESS))
623 return;
624
625 sch = qdisc_lookup(qdisc_dev(sch), TC_H_MAJ(parentid));
626 if (sch == NULL) {
627 WARN_ON(parentid != TC_H_ROOT);
628 return;
629 }
630 cops = sch->ops->cl_ops;
631 if (cops->qlen_notify) {
632 cl = cops->get(sch, parentid);
633 cops->qlen_notify(sch, cl);
634 cops->put(sch, cl);
635 }
636 sch->q.qlen -= n;
637 }
638}
639EXPORT_SYMBOL(qdisc_tree_decrease_qlen);
640
641static void notify_and_destroy(struct sk_buff *skb, struct nlmsghdr *n, u32 clid,
642 struct Qdisc *old, struct Qdisc *new)
643{
644 if (new || old)
645 qdisc_notify(skb, n, clid, old, new);
646
647 if (old)
648 qdisc_destroy(old);
649}
650
651/* Graft qdisc "new" to class "classid" of qdisc "parent" or
652 * to device "dev".
653 *
654 * When appropriate send a netlink notification using 'skb'
655 * and "n".
656 *
657 * On success, destroy old qdisc.
658 */
659
660static int qdisc_graft(struct net_device *dev, struct Qdisc *parent,
661 struct sk_buff *skb, struct nlmsghdr *n, u32 classid,
662 struct Qdisc *new, struct Qdisc *old)
663{
664 struct Qdisc *q = old;
665 int err = 0;
666
667 if (parent == NULL) {
668 unsigned int i, num_q, ingress;
669
670 ingress = 0;
671 num_q = dev->num_tx_queues;
672 if ((q && q->flags & TCQ_F_INGRESS) ||
673 (new && new->flags & TCQ_F_INGRESS)) {
674 num_q = 1;
675 ingress = 1;
676 }
677
678 if (dev->flags & IFF_UP)
679 dev_deactivate(dev);
680
681 for (i = 0; i < num_q; i++) {
682 struct netdev_queue *dev_queue = &dev->rx_queue;
683
684 if (!ingress)
685 dev_queue = netdev_get_tx_queue(dev, i);
686
687 old = dev_graft_qdisc(dev_queue, new);
688 if (new && i > 0)
689 atomic_inc(&new->refcnt);
690
691 qdisc_destroy(old);
692 }
693
694 notify_and_destroy(skb, n, classid, dev->qdisc, new);
695 if (new)
696 atomic_inc(&new->refcnt);
697 dev->qdisc = new ? : &noop_qdisc;
698
699 if (dev->flags & IFF_UP)
700 dev_activate(dev);
701 } else {
702 const struct Qdisc_class_ops *cops = parent->ops->cl_ops;
703
704 err = -EOPNOTSUPP;
705 if (cops && cops->graft) {
706 unsigned long cl = cops->get(parent, classid);
707 if (cl) {
708 err = cops->graft(parent, cl, new, &old);
709 cops->put(parent, cl);
710 } else
711 err = -ENOENT;
712 }
713 if (!err)
714 notify_and_destroy(skb, n, classid, old, new);
715 }
716 return err;
717}
718
719/* lockdep annotation is needed for ingress; egress gets it only for name */
720static struct lock_class_key qdisc_tx_lock;
721static struct lock_class_key qdisc_rx_lock;
722
723/*
724 Allocate and initialize new qdisc.
725
726 Parameters are passed via opt.
727 */
728
729static struct Qdisc *
730qdisc_create(struct net_device *dev, struct netdev_queue *dev_queue,
731 u32 parent, u32 handle, struct nlattr **tca, int *errp)
732{
733 int err;
734 struct nlattr *kind = tca[TCA_KIND];
735 struct Qdisc *sch;
736 struct Qdisc_ops *ops;
737 struct qdisc_size_table *stab;
738
739 ops = qdisc_lookup_ops(kind);
740#ifdef CONFIG_MODULES
741 if (ops == NULL && kind != NULL) {
742 char name[IFNAMSIZ];
743 if (nla_strlcpy(name, kind, IFNAMSIZ) < IFNAMSIZ) {
744 /* We dropped the RTNL semaphore in order to
745 * perform the module load. So, even if we
746 * succeeded in loading the module we have to
747 * tell the caller to replay the request. We
748 * indicate this using -EAGAIN.
749 * We replay the request because the device may
750 * go away in the mean time.
751 */
752 rtnl_unlock();
753 request_module("sch_%s", name);
754 rtnl_lock();
755 ops = qdisc_lookup_ops(kind);
756 if (ops != NULL) {
757 /* We will try again qdisc_lookup_ops,
758 * so don't keep a reference.
759 */
760 module_put(ops->owner);
761 err = -EAGAIN;
762 goto err_out;
763 }
764 }
765 }
766#endif
767
768 err = -ENOENT;
769 if (ops == NULL)
770 goto err_out;
771
772 sch = qdisc_alloc(dev_queue, ops);
773 if (IS_ERR(sch)) {
774 err = PTR_ERR(sch);
775 goto err_out2;
776 }
777
778 sch->parent = parent;
779
780 if (handle == TC_H_INGRESS) {
781 sch->flags |= TCQ_F_INGRESS;
782 handle = TC_H_MAKE(TC_H_INGRESS, 0);
783 lockdep_set_class(qdisc_lock(sch), &qdisc_rx_lock);
784 } else {
785 if (handle == 0) {
786 handle = qdisc_alloc_handle(dev);
787 err = -ENOMEM;
788 if (handle == 0)
789 goto err_out3;
790 }
791 lockdep_set_class(qdisc_lock(sch), &qdisc_tx_lock);
792 }
793
794 sch->handle = handle;
795
796 if (!ops->init || (err = ops->init(sch, tca[TCA_OPTIONS])) == 0) {
797 if (tca[TCA_STAB]) {
798 stab = qdisc_get_stab(tca[TCA_STAB]);
799 if (IS_ERR(stab)) {
800 err = PTR_ERR(stab);
801 goto err_out3;
802 }
803 sch->stab = stab;
804 }
805 if (tca[TCA_RATE]) {
806 spinlock_t *root_lock;
807
808 if ((sch->parent != TC_H_ROOT) &&
809 !(sch->flags & TCQ_F_INGRESS))
810 root_lock = qdisc_root_sleeping_lock(sch);
811 else
812 root_lock = qdisc_lock(sch);
813
814 err = gen_new_estimator(&sch->bstats, &sch->rate_est,
815 root_lock, tca[TCA_RATE]);
816 if (err) {
817 /*
818 * Any broken qdiscs that would require
819 * a ops->reset() here? The qdisc was never
820 * in action so it shouldn't be necessary.
821 */
822 if (ops->destroy)
823 ops->destroy(sch);
824 goto err_out3;
825 }
826 }
827
828 qdisc_list_add(sch);
829
830 return sch;
831 }
832err_out3:
833 qdisc_put_stab(sch->stab);
834 dev_put(dev);
835 kfree((char *) sch - sch->padded);
836err_out2:
837 module_put(ops->owner);
838err_out:
839 *errp = err;
840 return NULL;
841}
842
843static int qdisc_change(struct Qdisc *sch, struct nlattr **tca)
844{
845 struct qdisc_size_table *stab = NULL;
846 int err = 0;
847
848 if (tca[TCA_OPTIONS]) {
849 if (sch->ops->change == NULL)
850 return -EINVAL;
851 err = sch->ops->change(sch, tca[TCA_OPTIONS]);
852 if (err)
853 return err;
854 }
855
856 if (tca[TCA_STAB]) {
857 stab = qdisc_get_stab(tca[TCA_STAB]);
858 if (IS_ERR(stab))
859 return PTR_ERR(stab);
860 }
861
862 qdisc_put_stab(sch->stab);
863 sch->stab = stab;
864
865 if (tca[TCA_RATE])
866 /* NB: ignores errors from replace_estimator
867 because change can't be undone. */
868 gen_replace_estimator(&sch->bstats, &sch->rate_est,
869 qdisc_root_sleeping_lock(sch),
870 tca[TCA_RATE]);
871
872 return 0;
873}
874
875struct check_loop_arg
876{
877 struct qdisc_walker w;
878 struct Qdisc *p;
879 int depth;
880};
881
882static int check_loop_fn(struct Qdisc *q, unsigned long cl, struct qdisc_walker *w);
883
884static int check_loop(struct Qdisc *q, struct Qdisc *p, int depth)
885{
886 struct check_loop_arg arg;
887
888 if (q->ops->cl_ops == NULL)
889 return 0;
890
891 arg.w.stop = arg.w.skip = arg.w.count = 0;
892 arg.w.fn = check_loop_fn;
893 arg.depth = depth;
894 arg.p = p;
895 q->ops->cl_ops->walk(q, &arg.w);
896 return arg.w.stop ? -ELOOP : 0;
897}
898
899static int
900check_loop_fn(struct Qdisc *q, unsigned long cl, struct qdisc_walker *w)
901{
902 struct Qdisc *leaf;
903 const struct Qdisc_class_ops *cops = q->ops->cl_ops;
904 struct check_loop_arg *arg = (struct check_loop_arg *)w;
905
906 leaf = cops->leaf(q, cl);
907 if (leaf) {
908 if (leaf == arg->p || arg->depth > 7)
909 return -ELOOP;
910 return check_loop(leaf, arg->p, arg->depth + 1);
911 }
912 return 0;
913}
914
915/*
916 * Delete/get qdisc.
917 */
918
919static int tc_get_qdisc(struct sk_buff *skb, struct nlmsghdr *n, void *arg)
920{
921 struct net *net = sock_net(skb->sk);
922 struct tcmsg *tcm = NLMSG_DATA(n);
923 struct nlattr *tca[TCA_MAX + 1];
924 struct net_device *dev;
925 u32 clid = tcm->tcm_parent;
926 struct Qdisc *q = NULL;
927 struct Qdisc *p = NULL;
928 int err;
929
930 if (net != &init_net)
931 return -EINVAL;
932
933 if ((dev = __dev_get_by_index(&init_net, tcm->tcm_ifindex)) == NULL)
934 return -ENODEV;
935
936 err = nlmsg_parse(n, sizeof(*tcm), tca, TCA_MAX, NULL);
937 if (err < 0)
938 return err;
939
940 if (clid) {
941 if (clid != TC_H_ROOT) {
942 if (TC_H_MAJ(clid) != TC_H_MAJ(TC_H_INGRESS)) {
943 if ((p = qdisc_lookup(dev, TC_H_MAJ(clid))) == NULL)
944 return -ENOENT;
945 q = qdisc_leaf(p, clid);
946 } else { /* ingress */
947 q = dev->rx_queue.qdisc_sleeping;
948 }
949 } else {
950 q = dev->qdisc;
951 }
952 if (!q)
953 return -ENOENT;
954
955 if (tcm->tcm_handle && q->handle != tcm->tcm_handle)
956 return -EINVAL;
957 } else {
958 if ((q = qdisc_lookup(dev, tcm->tcm_handle)) == NULL)
959 return -ENOENT;
960 }
961
962 if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], q->ops->id))
963 return -EINVAL;
964
965 if (n->nlmsg_type == RTM_DELQDISC) {
966 if (!clid)
967 return -EINVAL;
968 if (q->handle == 0)
969 return -ENOENT;
970 if ((err = qdisc_graft(dev, p, skb, n, clid, NULL, q)) != 0)
971 return err;
972 } else {
973 qdisc_notify(skb, n, clid, NULL, q);
974 }
975 return 0;
976}
977
978/*
979 Create/change qdisc.
980 */
981
982static int tc_modify_qdisc(struct sk_buff *skb, struct nlmsghdr *n, void *arg)
983{
984 struct net *net = sock_net(skb->sk);
985 struct tcmsg *tcm;
986 struct nlattr *tca[TCA_MAX + 1];
987 struct net_device *dev;
988 u32 clid;
989 struct Qdisc *q, *p;
990 int err;
991
992 if (net != &init_net)
993 return -EINVAL;
994
995replay:
996 /* Reinit, just in case something touches this. */
997 tcm = NLMSG_DATA(n);
998 clid = tcm->tcm_parent;
999 q = p = NULL;
1000
1001 if ((dev = __dev_get_by_index(&init_net, tcm->tcm_ifindex)) == NULL)
1002 return -ENODEV;
1003
1004 err = nlmsg_parse(n, sizeof(*tcm), tca, TCA_MAX, NULL);
1005 if (err < 0)
1006 return err;
1007
1008 if (clid) {
1009 if (clid != TC_H_ROOT) {
1010 if (clid != TC_H_INGRESS) {
1011 if ((p = qdisc_lookup(dev, TC_H_MAJ(clid))) == NULL)
1012 return -ENOENT;
1013 q = qdisc_leaf(p, clid);
1014 } else { /*ingress */
1015 q = dev->rx_queue.qdisc_sleeping;
1016 }
1017 } else {
1018 q = dev->qdisc;
1019 }
1020
1021 /* It may be default qdisc, ignore it */
1022 if (q && q->handle == 0)
1023 q = NULL;
1024
1025 if (!q || !tcm->tcm_handle || q->handle != tcm->tcm_handle) {
1026 if (tcm->tcm_handle) {
1027 if (q && !(n->nlmsg_flags&NLM_F_REPLACE))
1028 return -EEXIST;
1029 if (TC_H_MIN(tcm->tcm_handle))
1030 return -EINVAL;
1031 if ((q = qdisc_lookup(dev, tcm->tcm_handle)) == NULL)
1032 goto create_n_graft;
1033 if (n->nlmsg_flags&NLM_F_EXCL)
1034 return -EEXIST;
1035 if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], q->ops->id))
1036 return -EINVAL;
1037 if (q == p ||
1038 (p && check_loop(q, p, 0)))
1039 return -ELOOP;
1040 atomic_inc(&q->refcnt);
1041 goto graft;
1042 } else {
1043 if (q == NULL)
1044 goto create_n_graft;
1045
1046 /* This magic test requires explanation.
1047 *
1048 * We know, that some child q is already
1049 * attached to this parent and have choice:
1050 * either to change it or to create/graft new one.
1051 *
1052 * 1. We are allowed to create/graft only
1053 * if CREATE and REPLACE flags are set.
1054 *
1055 * 2. If EXCL is set, requestor wanted to say,
1056 * that qdisc tcm_handle is not expected
1057 * to exist, so that we choose create/graft too.
1058 *
1059 * 3. The last case is when no flags are set.
1060 * Alas, it is sort of hole in API, we
1061 * cannot decide what to do unambiguously.
1062 * For now we select create/graft, if
1063 * user gave KIND, which does not match existing.
1064 */
1065 if ((n->nlmsg_flags&NLM_F_CREATE) &&
1066 (n->nlmsg_flags&NLM_F_REPLACE) &&
1067 ((n->nlmsg_flags&NLM_F_EXCL) ||
1068 (tca[TCA_KIND] &&
1069 nla_strcmp(tca[TCA_KIND], q->ops->id))))
1070 goto create_n_graft;
1071 }
1072 }
1073 } else {
1074 if (!tcm->tcm_handle)
1075 return -EINVAL;
1076 q = qdisc_lookup(dev, tcm->tcm_handle);
1077 }
1078
1079 /* Change qdisc parameters */
1080 if (q == NULL)
1081 return -ENOENT;
1082 if (n->nlmsg_flags&NLM_F_EXCL)
1083 return -EEXIST;
1084 if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], q->ops->id))
1085 return -EINVAL;
1086 err = qdisc_change(q, tca);
1087 if (err == 0)
1088 qdisc_notify(skb, n, clid, NULL, q);
1089 return err;
1090
1091create_n_graft:
1092 if (!(n->nlmsg_flags&NLM_F_CREATE))
1093 return -ENOENT;
1094 if (clid == TC_H_INGRESS)
1095 q = qdisc_create(dev, &dev->rx_queue,
1096 tcm->tcm_parent, tcm->tcm_parent,
1097 tca, &err);
1098 else
1099 q = qdisc_create(dev, netdev_get_tx_queue(dev, 0),
1100 tcm->tcm_parent, tcm->tcm_handle,
1101 tca, &err);
1102 if (q == NULL) {
1103 if (err == -EAGAIN)
1104 goto replay;
1105 return err;
1106 }
1107
1108graft:
1109 err = qdisc_graft(dev, p, skb, n, clid, q, NULL);
1110 if (err) {
1111 if (q)
1112 qdisc_destroy(q);
1113 return err;
1114 }
1115
1116 return 0;
1117}
1118
1119static int tc_fill_qdisc(struct sk_buff *skb, struct Qdisc *q, u32 clid,
1120 u32 pid, u32 seq, u16 flags, int event)
1121{
1122 struct tcmsg *tcm;
1123 struct nlmsghdr *nlh;
1124 unsigned char *b = skb_tail_pointer(skb);
1125 struct gnet_dump d;
1126
1127 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*tcm), flags);
1128 tcm = NLMSG_DATA(nlh);
1129 tcm->tcm_family = AF_UNSPEC;
1130 tcm->tcm__pad1 = 0;
1131 tcm->tcm__pad2 = 0;
1132 tcm->tcm_ifindex = qdisc_dev(q)->ifindex;
1133 tcm->tcm_parent = clid;
1134 tcm->tcm_handle = q->handle;
1135 tcm->tcm_info = atomic_read(&q->refcnt);
1136 NLA_PUT_STRING(skb, TCA_KIND, q->ops->id);
1137 if (q->ops->dump && q->ops->dump(q, skb) < 0)
1138 goto nla_put_failure;
1139 q->qstats.qlen = q->q.qlen;
1140
1141 if (q->stab && qdisc_dump_stab(skb, q->stab) < 0)
1142 goto nla_put_failure;
1143
1144 if (gnet_stats_start_copy_compat(skb, TCA_STATS2, TCA_STATS, TCA_XSTATS,
1145 qdisc_root_sleeping_lock(q), &d) < 0)
1146 goto nla_put_failure;
1147
1148 if (q->ops->dump_stats && q->ops->dump_stats(q, &d) < 0)
1149 goto nla_put_failure;
1150
1151 if (gnet_stats_copy_basic(&d, &q->bstats) < 0 ||
1152 gnet_stats_copy_rate_est(&d, &q->rate_est) < 0 ||
1153 gnet_stats_copy_queue(&d, &q->qstats) < 0)
1154 goto nla_put_failure;
1155
1156 if (gnet_stats_finish_copy(&d) < 0)
1157 goto nla_put_failure;
1158
1159 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1160 return skb->len;
1161
1162nlmsg_failure:
1163nla_put_failure:
1164 nlmsg_trim(skb, b);
1165 return -1;
1166}
1167
1168static int qdisc_notify(struct sk_buff *oskb, struct nlmsghdr *n,
1169 u32 clid, struct Qdisc *old, struct Qdisc *new)
1170{
1171 struct sk_buff *skb;
1172 u32 pid = oskb ? NETLINK_CB(oskb).pid : 0;
1173
1174 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1175 if (!skb)
1176 return -ENOBUFS;
1177
1178 if (old && old->handle) {
1179 if (tc_fill_qdisc(skb, old, clid, pid, n->nlmsg_seq, 0, RTM_DELQDISC) < 0)
1180 goto err_out;
1181 }
1182 if (new) {
1183 if (tc_fill_qdisc(skb, new, clid, pid, n->nlmsg_seq, old ? NLM_F_REPLACE : 0, RTM_NEWQDISC) < 0)
1184 goto err_out;
1185 }
1186
1187 if (skb->len)
1188 return rtnetlink_send(skb, &init_net, pid, RTNLGRP_TC, n->nlmsg_flags&NLM_F_ECHO);
1189
1190err_out:
1191 kfree_skb(skb);
1192 return -EINVAL;
1193}
1194
1195static bool tc_qdisc_dump_ignore(struct Qdisc *q)
1196{
1197 return (q->flags & TCQ_F_BUILTIN) ? true : false;
1198}
1199
1200static int tc_dump_qdisc_root(struct Qdisc *root, struct sk_buff *skb,
1201 struct netlink_callback *cb,
1202 int *q_idx_p, int s_q_idx)
1203{
1204 int ret = 0, q_idx = *q_idx_p;
1205 struct Qdisc *q;
1206
1207 if (!root)
1208 return 0;
1209
1210 q = root;
1211 if (q_idx < s_q_idx) {
1212 q_idx++;
1213 } else {
1214 if (!tc_qdisc_dump_ignore(q) &&
1215 tc_fill_qdisc(skb, q, q->parent, NETLINK_CB(cb->skb).pid,
1216 cb->nlh->nlmsg_seq, NLM_F_MULTI, RTM_NEWQDISC) <= 0)
1217 goto done;
1218 q_idx++;
1219 }
1220 list_for_each_entry(q, &root->list, list) {
1221 if (q_idx < s_q_idx) {
1222 q_idx++;
1223 continue;
1224 }
1225 if (!tc_qdisc_dump_ignore(q) &&
1226 tc_fill_qdisc(skb, q, q->parent, NETLINK_CB(cb->skb).pid,
1227 cb->nlh->nlmsg_seq, NLM_F_MULTI, RTM_NEWQDISC) <= 0)
1228 goto done;
1229 q_idx++;
1230 }
1231
1232out:
1233 *q_idx_p = q_idx;
1234 return ret;
1235done:
1236 ret = -1;
1237 goto out;
1238}
1239
1240static int tc_dump_qdisc(struct sk_buff *skb, struct netlink_callback *cb)
1241{
1242 struct net *net = sock_net(skb->sk);
1243 int idx, q_idx;
1244 int s_idx, s_q_idx;
1245 struct net_device *dev;
1246
1247 if (net != &init_net)
1248 return 0;
1249
1250 s_idx = cb->args[0];
1251 s_q_idx = q_idx = cb->args[1];
1252 read_lock(&dev_base_lock);
1253 idx = 0;
1254 for_each_netdev(&init_net, dev) {
1255 struct netdev_queue *dev_queue;
1256
1257 if (idx < s_idx)
1258 goto cont;
1259 if (idx > s_idx)
1260 s_q_idx = 0;
1261 q_idx = 0;
1262
1263 if (tc_dump_qdisc_root(dev->qdisc, skb, cb, &q_idx, s_q_idx) < 0)
1264 goto done;
1265
1266 dev_queue = &dev->rx_queue;
1267 if (tc_dump_qdisc_root(dev_queue->qdisc_sleeping, skb, cb, &q_idx, s_q_idx) < 0)
1268 goto done;
1269
1270cont:
1271 idx++;
1272 }
1273
1274done:
1275 read_unlock(&dev_base_lock);
1276
1277 cb->args[0] = idx;
1278 cb->args[1] = q_idx;
1279
1280 return skb->len;
1281}
1282
1283
1284
1285/************************************************
1286 * Traffic classes manipulation. *
1287 ************************************************/
1288
1289
1290
1291static int tc_ctl_tclass(struct sk_buff *skb, struct nlmsghdr *n, void *arg)
1292{
1293 struct net *net = sock_net(skb->sk);
1294 struct tcmsg *tcm = NLMSG_DATA(n);
1295 struct nlattr *tca[TCA_MAX + 1];
1296 struct net_device *dev;
1297 struct Qdisc *q = NULL;
1298 const struct Qdisc_class_ops *cops;
1299 unsigned long cl = 0;
1300 unsigned long new_cl;
1301 u32 pid = tcm->tcm_parent;
1302 u32 clid = tcm->tcm_handle;
1303 u32 qid = TC_H_MAJ(clid);
1304 int err;
1305
1306 if (net != &init_net)
1307 return -EINVAL;
1308
1309 if ((dev = __dev_get_by_index(&init_net, tcm->tcm_ifindex)) == NULL)
1310 return -ENODEV;
1311
1312 err = nlmsg_parse(n, sizeof(*tcm), tca, TCA_MAX, NULL);
1313 if (err < 0)
1314 return err;
1315
1316 /*
1317 parent == TC_H_UNSPEC - unspecified parent.
1318 parent == TC_H_ROOT - class is root, which has no parent.
1319 parent == X:0 - parent is root class.
1320 parent == X:Y - parent is a node in hierarchy.
1321 parent == 0:Y - parent is X:Y, where X:0 is qdisc.
1322
1323 handle == 0:0 - generate handle from kernel pool.
1324 handle == 0:Y - class is X:Y, where X:0 is qdisc.
1325 handle == X:Y - clear.
1326 handle == X:0 - root class.
1327 */
1328
1329 /* Step 1. Determine qdisc handle X:0 */
1330
1331 if (pid != TC_H_ROOT) {
1332 u32 qid1 = TC_H_MAJ(pid);
1333
1334 if (qid && qid1) {
1335 /* If both majors are known, they must be identical. */
1336 if (qid != qid1)
1337 return -EINVAL;
1338 } else if (qid1) {
1339 qid = qid1;
1340 } else if (qid == 0)
1341 qid = dev->qdisc->handle;
1342
1343 /* Now qid is genuine qdisc handle consistent
1344 both with parent and child.
1345
1346 TC_H_MAJ(pid) still may be unspecified, complete it now.
1347 */
1348 if (pid)
1349 pid = TC_H_MAKE(qid, pid);
1350 } else {
1351 if (qid == 0)
1352 qid = dev->qdisc->handle;
1353 }
1354
1355 /* OK. Locate qdisc */
1356 if ((q = qdisc_lookup(dev, qid)) == NULL)
1357 return -ENOENT;
1358
1359 /* An check that it supports classes */
1360 cops = q->ops->cl_ops;
1361 if (cops == NULL)
1362 return -EINVAL;
1363
1364 /* Now try to get class */
1365 if (clid == 0) {
1366 if (pid == TC_H_ROOT)
1367 clid = qid;
1368 } else
1369 clid = TC_H_MAKE(qid, clid);
1370
1371 if (clid)
1372 cl = cops->get(q, clid);
1373
1374 if (cl == 0) {
1375 err = -ENOENT;
1376 if (n->nlmsg_type != RTM_NEWTCLASS || !(n->nlmsg_flags&NLM_F_CREATE))
1377 goto out;
1378 } else {
1379 switch (n->nlmsg_type) {
1380 case RTM_NEWTCLASS:
1381 err = -EEXIST;
1382 if (n->nlmsg_flags&NLM_F_EXCL)
1383 goto out;
1384 break;
1385 case RTM_DELTCLASS:
1386 err = -EOPNOTSUPP;
1387 if (cops->delete)
1388 err = cops->delete(q, cl);
1389 if (err == 0)
1390 tclass_notify(skb, n, q, cl, RTM_DELTCLASS);
1391 goto out;
1392 case RTM_GETTCLASS:
1393 err = tclass_notify(skb, n, q, cl, RTM_NEWTCLASS);
1394 goto out;
1395 default:
1396 err = -EINVAL;
1397 goto out;
1398 }
1399 }
1400
1401 new_cl = cl;
1402 err = -EOPNOTSUPP;
1403 if (cops->change)
1404 err = cops->change(q, clid, pid, tca, &new_cl);
1405 if (err == 0)
1406 tclass_notify(skb, n, q, new_cl, RTM_NEWTCLASS);
1407
1408out:
1409 if (cl)
1410 cops->put(q, cl);
1411
1412 return err;
1413}
1414
1415
1416static int tc_fill_tclass(struct sk_buff *skb, struct Qdisc *q,
1417 unsigned long cl,
1418 u32 pid, u32 seq, u16 flags, int event)
1419{
1420 struct tcmsg *tcm;
1421 struct nlmsghdr *nlh;
1422 unsigned char *b = skb_tail_pointer(skb);
1423 struct gnet_dump d;
1424 const struct Qdisc_class_ops *cl_ops = q->ops->cl_ops;
1425
1426 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*tcm), flags);
1427 tcm = NLMSG_DATA(nlh);
1428 tcm->tcm_family = AF_UNSPEC;
1429 tcm->tcm_ifindex = qdisc_dev(q)->ifindex;
1430 tcm->tcm_parent = q->handle;
1431 tcm->tcm_handle = q->handle;
1432 tcm->tcm_info = 0;
1433 NLA_PUT_STRING(skb, TCA_KIND, q->ops->id);
1434 if (cl_ops->dump && cl_ops->dump(q, cl, skb, tcm) < 0)
1435 goto nla_put_failure;
1436
1437 if (gnet_stats_start_copy_compat(skb, TCA_STATS2, TCA_STATS, TCA_XSTATS,
1438 qdisc_root_sleeping_lock(q), &d) < 0)
1439 goto nla_put_failure;
1440
1441 if (cl_ops->dump_stats && cl_ops->dump_stats(q, cl, &d) < 0)
1442 goto nla_put_failure;
1443
1444 if (gnet_stats_finish_copy(&d) < 0)
1445 goto nla_put_failure;
1446
1447 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1448 return skb->len;
1449
1450nlmsg_failure:
1451nla_put_failure:
1452 nlmsg_trim(skb, b);
1453 return -1;
1454}
1455
1456static int tclass_notify(struct sk_buff *oskb, struct nlmsghdr *n,
1457 struct Qdisc *q, unsigned long cl, int event)
1458{
1459 struct sk_buff *skb;
1460 u32 pid = oskb ? NETLINK_CB(oskb).pid : 0;
1461
1462 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1463 if (!skb)
1464 return -ENOBUFS;
1465
1466 if (tc_fill_tclass(skb, q, cl, pid, n->nlmsg_seq, 0, event) < 0) {
1467 kfree_skb(skb);
1468 return -EINVAL;
1469 }
1470
1471 return rtnetlink_send(skb, &init_net, pid, RTNLGRP_TC, n->nlmsg_flags&NLM_F_ECHO);
1472}
1473
1474struct qdisc_dump_args
1475{
1476 struct qdisc_walker w;
1477 struct sk_buff *skb;
1478 struct netlink_callback *cb;
1479};
1480
1481static int qdisc_class_dump(struct Qdisc *q, unsigned long cl, struct qdisc_walker *arg)
1482{
1483 struct qdisc_dump_args *a = (struct qdisc_dump_args *)arg;
1484
1485 return tc_fill_tclass(a->skb, q, cl, NETLINK_CB(a->cb->skb).pid,
1486 a->cb->nlh->nlmsg_seq, NLM_F_MULTI, RTM_NEWTCLASS);
1487}
1488
1489static int tc_dump_tclass_qdisc(struct Qdisc *q, struct sk_buff *skb,
1490 struct tcmsg *tcm, struct netlink_callback *cb,
1491 int *t_p, int s_t)
1492{
1493 struct qdisc_dump_args arg;
1494
1495 if (tc_qdisc_dump_ignore(q) ||
1496 *t_p < s_t || !q->ops->cl_ops ||
1497 (tcm->tcm_parent &&
1498 TC_H_MAJ(tcm->tcm_parent) != q->handle)) {
1499 (*t_p)++;
1500 return 0;
1501 }
1502 if (*t_p > s_t)
1503 memset(&cb->args[1], 0, sizeof(cb->args)-sizeof(cb->args[0]));
1504 arg.w.fn = qdisc_class_dump;
1505 arg.skb = skb;
1506 arg.cb = cb;
1507 arg.w.stop = 0;
1508 arg.w.skip = cb->args[1];
1509 arg.w.count = 0;
1510 q->ops->cl_ops->walk(q, &arg.w);
1511 cb->args[1] = arg.w.count;
1512 if (arg.w.stop)
1513 return -1;
1514 (*t_p)++;
1515 return 0;
1516}
1517
1518static int tc_dump_tclass_root(struct Qdisc *root, struct sk_buff *skb,
1519 struct tcmsg *tcm, struct netlink_callback *cb,
1520 int *t_p, int s_t)
1521{
1522 struct Qdisc *q;
1523
1524 if (!root)
1525 return 0;
1526
1527 if (tc_dump_tclass_qdisc(root, skb, tcm, cb, t_p, s_t) < 0)
1528 return -1;
1529
1530 list_for_each_entry(q, &root->list, list) {
1531 if (tc_dump_tclass_qdisc(q, skb, tcm, cb, t_p, s_t) < 0)
1532 return -1;
1533 }
1534
1535 return 0;
1536}
1537
1538static int tc_dump_tclass(struct sk_buff *skb, struct netlink_callback *cb)
1539{
1540 struct tcmsg *tcm = (struct tcmsg*)NLMSG_DATA(cb->nlh);
1541 struct net *net = sock_net(skb->sk);
1542 struct netdev_queue *dev_queue;
1543 struct net_device *dev;
1544 int t, s_t;
1545
1546 if (net != &init_net)
1547 return 0;
1548
1549 if (cb->nlh->nlmsg_len < NLMSG_LENGTH(sizeof(*tcm)))
1550 return 0;
1551 if ((dev = dev_get_by_index(&init_net, tcm->tcm_ifindex)) == NULL)
1552 return 0;
1553
1554 s_t = cb->args[0];
1555 t = 0;
1556
1557 if (tc_dump_tclass_root(dev->qdisc, skb, tcm, cb, &t, s_t) < 0)
1558 goto done;
1559
1560 dev_queue = &dev->rx_queue;
1561 if (tc_dump_tclass_root(dev_queue->qdisc_sleeping, skb, tcm, cb, &t, s_t) < 0)
1562 goto done;
1563
1564done:
1565 cb->args[0] = t;
1566
1567 dev_put(dev);
1568 return skb->len;
1569}
1570
1571/* Main classifier routine: scans classifier chain attached
1572 to this qdisc, (optionally) tests for protocol and asks
1573 specific classifiers.
1574 */
1575int tc_classify_compat(struct sk_buff *skb, struct tcf_proto *tp,
1576 struct tcf_result *res)
1577{
1578 __be16 protocol = skb->protocol;
1579 int err = 0;
1580
1581 for (; tp; tp = tp->next) {
1582 if ((tp->protocol == protocol ||
1583 tp->protocol == htons(ETH_P_ALL)) &&
1584 (err = tp->classify(skb, tp, res)) >= 0) {
1585#ifdef CONFIG_NET_CLS_ACT
1586 if (err != TC_ACT_RECLASSIFY && skb->tc_verd)
1587 skb->tc_verd = SET_TC_VERD(skb->tc_verd, 0);
1588#endif
1589 return err;
1590 }
1591 }
1592 return -1;
1593}
1594EXPORT_SYMBOL(tc_classify_compat);
1595
1596int tc_classify(struct sk_buff *skb, struct tcf_proto *tp,
1597 struct tcf_result *res)
1598{
1599 int err = 0;
1600 __be16 protocol;
1601#ifdef CONFIG_NET_CLS_ACT
1602 struct tcf_proto *otp = tp;
1603reclassify:
1604#endif
1605 protocol = skb->protocol;
1606
1607 err = tc_classify_compat(skb, tp, res);
1608#ifdef CONFIG_NET_CLS_ACT
1609 if (err == TC_ACT_RECLASSIFY) {
1610 u32 verd = G_TC_VERD(skb->tc_verd);
1611 tp = otp;
1612
1613 if (verd++ >= MAX_REC_LOOP) {
1614 printk("rule prio %u protocol %02x reclassify loop, "
1615 "packet dropped\n",
1616 tp->prio&0xffff, ntohs(tp->protocol));
1617 return TC_ACT_SHOT;
1618 }
1619 skb->tc_verd = SET_TC_VERD(skb->tc_verd, verd);
1620 goto reclassify;
1621 }
1622#endif
1623 return err;
1624}
1625EXPORT_SYMBOL(tc_classify);
1626
1627void tcf_destroy(struct tcf_proto *tp)
1628{
1629 tp->ops->destroy(tp);
1630 module_put(tp->ops->owner);
1631 kfree(tp);
1632}
1633
1634void tcf_destroy_chain(struct tcf_proto **fl)
1635{
1636 struct tcf_proto *tp;
1637
1638 while ((tp = *fl) != NULL) {
1639 *fl = tp->next;
1640 tcf_destroy(tp);
1641 }
1642}
1643EXPORT_SYMBOL(tcf_destroy_chain);
1644
1645#ifdef CONFIG_PROC_FS
1646static int psched_show(struct seq_file *seq, void *v)
1647{
1648 struct timespec ts;
1649
1650 hrtimer_get_res(CLOCK_MONOTONIC, &ts);
1651 seq_printf(seq, "%08x %08x %08x %08x\n",
1652 (u32)NSEC_PER_USEC, (u32)PSCHED_TICKS2NS(1),
1653 1000000,
1654 (u32)NSEC_PER_SEC/(u32)ktime_to_ns(timespec_to_ktime(ts)));
1655
1656 return 0;
1657}
1658
1659static int psched_open(struct inode *inode, struct file *file)
1660{
1661 return single_open(file, psched_show, PDE(inode)->data);
1662}
1663
1664static const struct file_operations psched_fops = {
1665 .owner = THIS_MODULE,
1666 .open = psched_open,
1667 .read = seq_read,
1668 .llseek = seq_lseek,
1669 .release = single_release,
1670};
1671#endif
1672
1673static int __init pktsched_init(void)
1674{
1675 register_qdisc(&pfifo_qdisc_ops);
1676 register_qdisc(&bfifo_qdisc_ops);
1677 proc_net_fops_create(&init_net, "psched", 0, &psched_fops);
1678
1679 rtnl_register(PF_UNSPEC, RTM_NEWQDISC, tc_modify_qdisc, NULL);
1680 rtnl_register(PF_UNSPEC, RTM_DELQDISC, tc_get_qdisc, NULL);
1681 rtnl_register(PF_UNSPEC, RTM_GETQDISC, tc_get_qdisc, tc_dump_qdisc);
1682 rtnl_register(PF_UNSPEC, RTM_NEWTCLASS, tc_ctl_tclass, NULL);
1683 rtnl_register(PF_UNSPEC, RTM_DELTCLASS, tc_ctl_tclass, NULL);
1684 rtnl_register(PF_UNSPEC, RTM_GETTCLASS, tc_ctl_tclass, tc_dump_tclass);
1685
1686 return 0;
1687}
1688
1689subsys_initcall(pktsched_init);