[TCP]: Code duplication removal, added tcp_bound_to_half_wnd()
[linux-2.6/mini2440.git] / net / sched / sch_api.c
blob273c628be0540350554f39068489e145eab1b85c
1 /*
2 * net/sched/sch_api.c Packet scheduler API.
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.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
11 * Fixes:
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
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>
31 #include <net/net_namespace.h>
32 #include <net/sock.h>
33 #include <net/netlink.h>
34 #include <net/pkt_sched.h>
36 static int qdisc_notify(struct sk_buff *oskb, struct nlmsghdr *n, u32 clid,
37 struct Qdisc *old, struct Qdisc *new);
38 static int tclass_notify(struct sk_buff *oskb, struct nlmsghdr *n,
39 struct Qdisc *q, unsigned long cl, int event);
43 Short review.
44 -------------
46 This file consists of two interrelated parts:
48 1. queueing disciplines manager frontend.
49 2. traffic classes manager frontend.
51 Generally, queueing discipline ("qdisc") is a black box,
52 which is able to enqueue packets and to dequeue them (when
53 device is ready to send something) in order and at times
54 determined by algorithm hidden in it.
56 qdisc's are divided to two categories:
57 - "queues", which have no internal structure visible from outside.
58 - "schedulers", which split all the packets to "traffic classes",
59 using "packet classifiers" (look at cls_api.c)
61 In turn, classes may have child qdiscs (as rule, queues)
62 attached to them etc. etc. etc.
64 The goal of the routines in this file is to translate
65 information supplied by user in the form of handles
66 to more intelligible for kernel form, to make some sanity
67 checks and part of work, which is common to all qdiscs
68 and to provide rtnetlink notifications.
70 All real intelligent work is done inside qdisc modules.
74 Every discipline has two major routines: enqueue and dequeue.
76 ---dequeue
78 dequeue usually returns a skb to send. It is allowed to return NULL,
79 but it does not mean that queue is empty, it just means that
80 discipline does not want to send anything this time.
81 Queue is really empty if q->q.qlen == 0.
82 For complicated disciplines with multiple queues q->q is not
83 real packet queue, but however q->q.qlen must be valid.
85 ---enqueue
87 enqueue returns 0, if packet was enqueued successfully.
88 If packet (this one or another one) was dropped, it returns
89 not zero error code.
90 NET_XMIT_DROP - this packet dropped
91 Expected action: do not backoff, but wait until queue will clear.
92 NET_XMIT_CN - probably this packet enqueued, but another one dropped.
93 Expected action: backoff or ignore
94 NET_XMIT_POLICED - dropped by police.
95 Expected action: backoff or error to real-time apps.
97 Auxiliary routines:
99 ---requeue
101 requeues once dequeued packet. It is used for non-standard or
102 just buggy devices, which can defer output even if dev->tbusy=0.
104 ---reset
106 returns qdisc to initial state: purge all buffers, clear all
107 timers, counters (except for statistics) etc.
109 ---init
111 initializes newly created qdisc.
113 ---destroy
115 destroys resources allocated by init and during lifetime of qdisc.
117 ---change
119 changes qdisc parameters.
122 /* Protects list of registered TC modules. It is pure SMP lock. */
123 static DEFINE_RWLOCK(qdisc_mod_lock);
126 /************************************************
127 * Queueing disciplines manipulation. *
128 ************************************************/
131 /* The list of all installed queueing disciplines. */
133 static struct Qdisc_ops *qdisc_base;
135 /* Register/uregister queueing discipline */
137 int register_qdisc(struct Qdisc_ops *qops)
139 struct Qdisc_ops *q, **qp;
140 int rc = -EEXIST;
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;
147 if (qops->enqueue == NULL)
148 qops->enqueue = noop_qdisc_ops.enqueue;
149 if (qops->requeue == NULL)
150 qops->requeue = noop_qdisc_ops.requeue;
151 if (qops->dequeue == NULL)
152 qops->dequeue = noop_qdisc_ops.dequeue;
154 qops->next = NULL;
155 *qp = qops;
156 rc = 0;
157 out:
158 write_unlock(&qdisc_mod_lock);
159 return rc;
162 int unregister_qdisc(struct Qdisc_ops *qops)
164 struct Qdisc_ops *q, **qp;
165 int err = -ENOENT;
167 write_lock(&qdisc_mod_lock);
168 for (qp = &qdisc_base; (q=*qp)!=NULL; qp = &q->next)
169 if (q == qops)
170 break;
171 if (q) {
172 *qp = q->next;
173 q->next = NULL;
174 err = 0;
176 write_unlock(&qdisc_mod_lock);
177 return err;
180 /* We know handle. Find qdisc among all qdisc's attached to device
181 (root qdisc, all its children, children of children etc.)
184 struct Qdisc *qdisc_lookup(struct net_device *dev, u32 handle)
186 struct Qdisc *q;
188 list_for_each_entry(q, &dev->qdisc_list, list) {
189 if (q->handle == handle)
190 return q;
192 return NULL;
195 static struct Qdisc *qdisc_leaf(struct Qdisc *p, u32 classid)
197 unsigned long cl;
198 struct Qdisc *leaf;
199 const struct Qdisc_class_ops *cops = p->ops->cl_ops;
201 if (cops == NULL)
202 return NULL;
203 cl = cops->get(p, classid);
205 if (cl == 0)
206 return NULL;
207 leaf = cops->leaf(p, cl);
208 cops->put(p, cl);
209 return leaf;
212 /* Find queueing discipline by name */
214 static struct Qdisc_ops *qdisc_lookup_ops(struct rtattr *kind)
216 struct Qdisc_ops *q = NULL;
218 if (kind) {
219 read_lock(&qdisc_mod_lock);
220 for (q = qdisc_base; q; q = q->next) {
221 if (rtattr_strcmp(kind, q->id) == 0) {
222 if (!try_module_get(q->owner))
223 q = NULL;
224 break;
227 read_unlock(&qdisc_mod_lock);
229 return q;
232 static struct qdisc_rate_table *qdisc_rtab_list;
234 struct qdisc_rate_table *qdisc_get_rtab(struct tc_ratespec *r, struct rtattr *tab)
236 struct qdisc_rate_table *rtab;
238 for (rtab = qdisc_rtab_list; rtab; rtab = rtab->next) {
239 if (memcmp(&rtab->rate, r, sizeof(struct tc_ratespec)) == 0) {
240 rtab->refcnt++;
241 return rtab;
245 if (tab == NULL || r->rate == 0 || r->cell_log == 0 || RTA_PAYLOAD(tab) != 1024)
246 return NULL;
248 rtab = kmalloc(sizeof(*rtab), GFP_KERNEL);
249 if (rtab) {
250 rtab->rate = *r;
251 rtab->refcnt = 1;
252 memcpy(rtab->data, RTA_DATA(tab), 1024);
253 rtab->next = qdisc_rtab_list;
254 qdisc_rtab_list = rtab;
256 return rtab;
259 void qdisc_put_rtab(struct qdisc_rate_table *tab)
261 struct qdisc_rate_table *rtab, **rtabp;
263 if (!tab || --tab->refcnt)
264 return;
266 for (rtabp = &qdisc_rtab_list; (rtab=*rtabp) != NULL; rtabp = &rtab->next) {
267 if (rtab == tab) {
268 *rtabp = rtab->next;
269 kfree(rtab);
270 return;
275 static enum hrtimer_restart qdisc_watchdog(struct hrtimer *timer)
277 struct qdisc_watchdog *wd = container_of(timer, struct qdisc_watchdog,
278 timer);
279 struct net_device *dev = wd->qdisc->dev;
281 wd->qdisc->flags &= ~TCQ_F_THROTTLED;
282 smp_wmb();
283 netif_schedule(dev);
285 return HRTIMER_NORESTART;
288 void qdisc_watchdog_init(struct qdisc_watchdog *wd, struct Qdisc *qdisc)
290 hrtimer_init(&wd->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
291 wd->timer.function = qdisc_watchdog;
292 wd->qdisc = qdisc;
294 EXPORT_SYMBOL(qdisc_watchdog_init);
296 void qdisc_watchdog_schedule(struct qdisc_watchdog *wd, psched_time_t expires)
298 ktime_t time;
300 wd->qdisc->flags |= TCQ_F_THROTTLED;
301 time = ktime_set(0, 0);
302 time = ktime_add_ns(time, PSCHED_US2NS(expires));
303 hrtimer_start(&wd->timer, time, HRTIMER_MODE_ABS);
305 EXPORT_SYMBOL(qdisc_watchdog_schedule);
307 void qdisc_watchdog_cancel(struct qdisc_watchdog *wd)
309 hrtimer_cancel(&wd->timer);
310 wd->qdisc->flags &= ~TCQ_F_THROTTLED;
312 EXPORT_SYMBOL(qdisc_watchdog_cancel);
314 /* Allocate an unique handle from space managed by kernel */
316 static u32 qdisc_alloc_handle(struct net_device *dev)
318 int i = 0x10000;
319 static u32 autohandle = TC_H_MAKE(0x80000000U, 0);
321 do {
322 autohandle += TC_H_MAKE(0x10000U, 0);
323 if (autohandle == TC_H_MAKE(TC_H_ROOT, 0))
324 autohandle = TC_H_MAKE(0x80000000U, 0);
325 } while (qdisc_lookup(dev, autohandle) && --i > 0);
327 return i>0 ? autohandle : 0;
330 /* Attach toplevel qdisc to device dev */
332 static struct Qdisc *
333 dev_graft_qdisc(struct net_device *dev, struct Qdisc *qdisc)
335 struct Qdisc *oqdisc;
337 if (dev->flags & IFF_UP)
338 dev_deactivate(dev);
340 qdisc_lock_tree(dev);
341 if (qdisc && qdisc->flags&TCQ_F_INGRESS) {
342 oqdisc = dev->qdisc_ingress;
343 /* Prune old scheduler */
344 if (oqdisc && atomic_read(&oqdisc->refcnt) <= 1) {
345 /* delete */
346 qdisc_reset(oqdisc);
347 dev->qdisc_ingress = NULL;
348 } else { /* new */
349 dev->qdisc_ingress = qdisc;
352 } else {
354 oqdisc = dev->qdisc_sleeping;
356 /* Prune old scheduler */
357 if (oqdisc && atomic_read(&oqdisc->refcnt) <= 1)
358 qdisc_reset(oqdisc);
360 /* ... and graft new one */
361 if (qdisc == NULL)
362 qdisc = &noop_qdisc;
363 dev->qdisc_sleeping = qdisc;
364 dev->qdisc = &noop_qdisc;
367 qdisc_unlock_tree(dev);
369 if (dev->flags & IFF_UP)
370 dev_activate(dev);
372 return oqdisc;
375 void qdisc_tree_decrease_qlen(struct Qdisc *sch, unsigned int n)
377 const struct Qdisc_class_ops *cops;
378 unsigned long cl;
379 u32 parentid;
381 if (n == 0)
382 return;
383 while ((parentid = sch->parent)) {
384 sch = qdisc_lookup(sch->dev, TC_H_MAJ(parentid));
385 if (sch == NULL) {
386 WARN_ON(parentid != TC_H_ROOT);
387 return;
389 cops = sch->ops->cl_ops;
390 if (cops->qlen_notify) {
391 cl = cops->get(sch, parentid);
392 cops->qlen_notify(sch, cl);
393 cops->put(sch, cl);
395 sch->q.qlen -= n;
398 EXPORT_SYMBOL(qdisc_tree_decrease_qlen);
400 /* Graft qdisc "new" to class "classid" of qdisc "parent" or
401 to device "dev".
403 Old qdisc is not destroyed but returned in *old.
406 static int qdisc_graft(struct net_device *dev, struct Qdisc *parent,
407 u32 classid,
408 struct Qdisc *new, struct Qdisc **old)
410 int err = 0;
411 struct Qdisc *q = *old;
414 if (parent == NULL) {
415 if (q && q->flags&TCQ_F_INGRESS) {
416 *old = dev_graft_qdisc(dev, q);
417 } else {
418 *old = dev_graft_qdisc(dev, new);
420 } else {
421 const struct Qdisc_class_ops *cops = parent->ops->cl_ops;
423 err = -EINVAL;
425 if (cops) {
426 unsigned long cl = cops->get(parent, classid);
427 if (cl) {
428 err = cops->graft(parent, cl, new, old);
429 cops->put(parent, cl);
433 return err;
437 Allocate and initialize new qdisc.
439 Parameters are passed via opt.
442 static struct Qdisc *
443 qdisc_create(struct net_device *dev, u32 parent, u32 handle,
444 struct rtattr **tca, int *errp)
446 int err;
447 struct rtattr *kind = tca[TCA_KIND-1];
448 struct Qdisc *sch;
449 struct Qdisc_ops *ops;
451 ops = qdisc_lookup_ops(kind);
452 #ifdef CONFIG_KMOD
453 if (ops == NULL && kind != NULL) {
454 char name[IFNAMSIZ];
455 if (rtattr_strlcpy(name, kind, IFNAMSIZ) < IFNAMSIZ) {
456 /* We dropped the RTNL semaphore in order to
457 * perform the module load. So, even if we
458 * succeeded in loading the module we have to
459 * tell the caller to replay the request. We
460 * indicate this using -EAGAIN.
461 * We replay the request because the device may
462 * go away in the mean time.
464 rtnl_unlock();
465 request_module("sch_%s", name);
466 rtnl_lock();
467 ops = qdisc_lookup_ops(kind);
468 if (ops != NULL) {
469 /* We will try again qdisc_lookup_ops,
470 * so don't keep a reference.
472 module_put(ops->owner);
473 err = -EAGAIN;
474 goto err_out;
478 #endif
480 err = -ENOENT;
481 if (ops == NULL)
482 goto err_out;
484 sch = qdisc_alloc(dev, ops);
485 if (IS_ERR(sch)) {
486 err = PTR_ERR(sch);
487 goto err_out2;
490 sch->parent = parent;
492 if (handle == TC_H_INGRESS) {
493 sch->flags |= TCQ_F_INGRESS;
494 sch->stats_lock = &dev->ingress_lock;
495 handle = TC_H_MAKE(TC_H_INGRESS, 0);
496 } else {
497 sch->stats_lock = &dev->queue_lock;
498 if (handle == 0) {
499 handle = qdisc_alloc_handle(dev);
500 err = -ENOMEM;
501 if (handle == 0)
502 goto err_out3;
506 sch->handle = handle;
508 if (!ops->init || (err = ops->init(sch, tca[TCA_OPTIONS-1])) == 0) {
509 if (tca[TCA_RATE-1]) {
510 err = gen_new_estimator(&sch->bstats, &sch->rate_est,
511 sch->stats_lock,
512 tca[TCA_RATE-1]);
513 if (err) {
515 * Any broken qdiscs that would require
516 * a ops->reset() here? The qdisc was never
517 * in action so it shouldn't be necessary.
519 if (ops->destroy)
520 ops->destroy(sch);
521 goto err_out3;
524 qdisc_lock_tree(dev);
525 list_add_tail(&sch->list, &dev->qdisc_list);
526 qdisc_unlock_tree(dev);
528 return sch;
530 err_out3:
531 dev_put(dev);
532 kfree((char *) sch - sch->padded);
533 err_out2:
534 module_put(ops->owner);
535 err_out:
536 *errp = err;
537 return NULL;
540 static int qdisc_change(struct Qdisc *sch, struct rtattr **tca)
542 if (tca[TCA_OPTIONS-1]) {
543 int err;
545 if (sch->ops->change == NULL)
546 return -EINVAL;
547 err = sch->ops->change(sch, tca[TCA_OPTIONS-1]);
548 if (err)
549 return err;
551 if (tca[TCA_RATE-1])
552 gen_replace_estimator(&sch->bstats, &sch->rate_est,
553 sch->stats_lock, tca[TCA_RATE-1]);
554 return 0;
557 struct check_loop_arg
559 struct qdisc_walker w;
560 struct Qdisc *p;
561 int depth;
564 static int check_loop_fn(struct Qdisc *q, unsigned long cl, struct qdisc_walker *w);
566 static int check_loop(struct Qdisc *q, struct Qdisc *p, int depth)
568 struct check_loop_arg arg;
570 if (q->ops->cl_ops == NULL)
571 return 0;
573 arg.w.stop = arg.w.skip = arg.w.count = 0;
574 arg.w.fn = check_loop_fn;
575 arg.depth = depth;
576 arg.p = p;
577 q->ops->cl_ops->walk(q, &arg.w);
578 return arg.w.stop ? -ELOOP : 0;
581 static int
582 check_loop_fn(struct Qdisc *q, unsigned long cl, struct qdisc_walker *w)
584 struct Qdisc *leaf;
585 const struct Qdisc_class_ops *cops = q->ops->cl_ops;
586 struct check_loop_arg *arg = (struct check_loop_arg *)w;
588 leaf = cops->leaf(q, cl);
589 if (leaf) {
590 if (leaf == arg->p || arg->depth > 7)
591 return -ELOOP;
592 return check_loop(leaf, arg->p, arg->depth + 1);
594 return 0;
598 * Delete/get qdisc.
601 static int tc_get_qdisc(struct sk_buff *skb, struct nlmsghdr *n, void *arg)
603 struct net *net = skb->sk->sk_net;
604 struct tcmsg *tcm = NLMSG_DATA(n);
605 struct rtattr **tca = arg;
606 struct net_device *dev;
607 u32 clid = tcm->tcm_parent;
608 struct Qdisc *q = NULL;
609 struct Qdisc *p = NULL;
610 int err;
612 if (net != &init_net)
613 return -EINVAL;
615 if ((dev = __dev_get_by_index(&init_net, tcm->tcm_ifindex)) == NULL)
616 return -ENODEV;
618 if (clid) {
619 if (clid != TC_H_ROOT) {
620 if (TC_H_MAJ(clid) != TC_H_MAJ(TC_H_INGRESS)) {
621 if ((p = qdisc_lookup(dev, TC_H_MAJ(clid))) == NULL)
622 return -ENOENT;
623 q = qdisc_leaf(p, clid);
624 } else { /* ingress */
625 q = dev->qdisc_ingress;
627 } else {
628 q = dev->qdisc_sleeping;
630 if (!q)
631 return -ENOENT;
633 if (tcm->tcm_handle && q->handle != tcm->tcm_handle)
634 return -EINVAL;
635 } else {
636 if ((q = qdisc_lookup(dev, tcm->tcm_handle)) == NULL)
637 return -ENOENT;
640 if (tca[TCA_KIND-1] && rtattr_strcmp(tca[TCA_KIND-1], q->ops->id))
641 return -EINVAL;
643 if (n->nlmsg_type == RTM_DELQDISC) {
644 if (!clid)
645 return -EINVAL;
646 if (q->handle == 0)
647 return -ENOENT;
648 if ((err = qdisc_graft(dev, p, clid, NULL, &q)) != 0)
649 return err;
650 if (q) {
651 qdisc_notify(skb, n, clid, q, NULL);
652 qdisc_lock_tree(dev);
653 qdisc_destroy(q);
654 qdisc_unlock_tree(dev);
656 } else {
657 qdisc_notify(skb, n, clid, NULL, q);
659 return 0;
663 Create/change qdisc.
666 static int tc_modify_qdisc(struct sk_buff *skb, struct nlmsghdr *n, void *arg)
668 struct net *net = skb->sk->sk_net;
669 struct tcmsg *tcm;
670 struct rtattr **tca;
671 struct net_device *dev;
672 u32 clid;
673 struct Qdisc *q, *p;
674 int err;
676 if (net != &init_net)
677 return -EINVAL;
679 replay:
680 /* Reinit, just in case something touches this. */
681 tcm = NLMSG_DATA(n);
682 tca = arg;
683 clid = tcm->tcm_parent;
684 q = p = NULL;
686 if ((dev = __dev_get_by_index(&init_net, tcm->tcm_ifindex)) == NULL)
687 return -ENODEV;
689 if (clid) {
690 if (clid != TC_H_ROOT) {
691 if (clid != TC_H_INGRESS) {
692 if ((p = qdisc_lookup(dev, TC_H_MAJ(clid))) == NULL)
693 return -ENOENT;
694 q = qdisc_leaf(p, clid);
695 } else { /*ingress */
696 q = dev->qdisc_ingress;
698 } else {
699 q = dev->qdisc_sleeping;
702 /* It may be default qdisc, ignore it */
703 if (q && q->handle == 0)
704 q = NULL;
706 if (!q || !tcm->tcm_handle || q->handle != tcm->tcm_handle) {
707 if (tcm->tcm_handle) {
708 if (q && !(n->nlmsg_flags&NLM_F_REPLACE))
709 return -EEXIST;
710 if (TC_H_MIN(tcm->tcm_handle))
711 return -EINVAL;
712 if ((q = qdisc_lookup(dev, tcm->tcm_handle)) == NULL)
713 goto create_n_graft;
714 if (n->nlmsg_flags&NLM_F_EXCL)
715 return -EEXIST;
716 if (tca[TCA_KIND-1] && rtattr_strcmp(tca[TCA_KIND-1], q->ops->id))
717 return -EINVAL;
718 if (q == p ||
719 (p && check_loop(q, p, 0)))
720 return -ELOOP;
721 atomic_inc(&q->refcnt);
722 goto graft;
723 } else {
724 if (q == NULL)
725 goto create_n_graft;
727 /* This magic test requires explanation.
729 * We know, that some child q is already
730 * attached to this parent and have choice:
731 * either to change it or to create/graft new one.
733 * 1. We are allowed to create/graft only
734 * if CREATE and REPLACE flags are set.
736 * 2. If EXCL is set, requestor wanted to say,
737 * that qdisc tcm_handle is not expected
738 * to exist, so that we choose create/graft too.
740 * 3. The last case is when no flags are set.
741 * Alas, it is sort of hole in API, we
742 * cannot decide what to do unambiguously.
743 * For now we select create/graft, if
744 * user gave KIND, which does not match existing.
746 if ((n->nlmsg_flags&NLM_F_CREATE) &&
747 (n->nlmsg_flags&NLM_F_REPLACE) &&
748 ((n->nlmsg_flags&NLM_F_EXCL) ||
749 (tca[TCA_KIND-1] &&
750 rtattr_strcmp(tca[TCA_KIND-1], q->ops->id))))
751 goto create_n_graft;
754 } else {
755 if (!tcm->tcm_handle)
756 return -EINVAL;
757 q = qdisc_lookup(dev, tcm->tcm_handle);
760 /* Change qdisc parameters */
761 if (q == NULL)
762 return -ENOENT;
763 if (n->nlmsg_flags&NLM_F_EXCL)
764 return -EEXIST;
765 if (tca[TCA_KIND-1] && rtattr_strcmp(tca[TCA_KIND-1], q->ops->id))
766 return -EINVAL;
767 err = qdisc_change(q, tca);
768 if (err == 0)
769 qdisc_notify(skb, n, clid, NULL, q);
770 return err;
772 create_n_graft:
773 if (!(n->nlmsg_flags&NLM_F_CREATE))
774 return -ENOENT;
775 if (clid == TC_H_INGRESS)
776 q = qdisc_create(dev, tcm->tcm_parent, tcm->tcm_parent,
777 tca, &err);
778 else
779 q = qdisc_create(dev, tcm->tcm_parent, tcm->tcm_handle,
780 tca, &err);
781 if (q == NULL) {
782 if (err == -EAGAIN)
783 goto replay;
784 return err;
787 graft:
788 if (1) {
789 struct Qdisc *old_q = NULL;
790 err = qdisc_graft(dev, p, clid, q, &old_q);
791 if (err) {
792 if (q) {
793 qdisc_lock_tree(dev);
794 qdisc_destroy(q);
795 qdisc_unlock_tree(dev);
797 return err;
799 qdisc_notify(skb, n, clid, old_q, q);
800 if (old_q) {
801 qdisc_lock_tree(dev);
802 qdisc_destroy(old_q);
803 qdisc_unlock_tree(dev);
806 return 0;
809 static int tc_fill_qdisc(struct sk_buff *skb, struct Qdisc *q, u32 clid,
810 u32 pid, u32 seq, u16 flags, int event)
812 struct tcmsg *tcm;
813 struct nlmsghdr *nlh;
814 unsigned char *b = skb_tail_pointer(skb);
815 struct gnet_dump d;
817 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*tcm), flags);
818 tcm = NLMSG_DATA(nlh);
819 tcm->tcm_family = AF_UNSPEC;
820 tcm->tcm__pad1 = 0;
821 tcm->tcm__pad2 = 0;
822 tcm->tcm_ifindex = q->dev->ifindex;
823 tcm->tcm_parent = clid;
824 tcm->tcm_handle = q->handle;
825 tcm->tcm_info = atomic_read(&q->refcnt);
826 RTA_PUT(skb, TCA_KIND, IFNAMSIZ, q->ops->id);
827 if (q->ops->dump && q->ops->dump(q, skb) < 0)
828 goto rtattr_failure;
829 q->qstats.qlen = q->q.qlen;
831 if (gnet_stats_start_copy_compat(skb, TCA_STATS2, TCA_STATS,
832 TCA_XSTATS, q->stats_lock, &d) < 0)
833 goto rtattr_failure;
835 if (q->ops->dump_stats && q->ops->dump_stats(q, &d) < 0)
836 goto rtattr_failure;
838 if (gnet_stats_copy_basic(&d, &q->bstats) < 0 ||
839 gnet_stats_copy_rate_est(&d, &q->rate_est) < 0 ||
840 gnet_stats_copy_queue(&d, &q->qstats) < 0)
841 goto rtattr_failure;
843 if (gnet_stats_finish_copy(&d) < 0)
844 goto rtattr_failure;
846 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
847 return skb->len;
849 nlmsg_failure:
850 rtattr_failure:
851 nlmsg_trim(skb, b);
852 return -1;
855 static int qdisc_notify(struct sk_buff *oskb, struct nlmsghdr *n,
856 u32 clid, struct Qdisc *old, struct Qdisc *new)
858 struct sk_buff *skb;
859 u32 pid = oskb ? NETLINK_CB(oskb).pid : 0;
861 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
862 if (!skb)
863 return -ENOBUFS;
865 if (old && old->handle) {
866 if (tc_fill_qdisc(skb, old, clid, pid, n->nlmsg_seq, 0, RTM_DELQDISC) < 0)
867 goto err_out;
869 if (new) {
870 if (tc_fill_qdisc(skb, new, clid, pid, n->nlmsg_seq, old ? NLM_F_REPLACE : 0, RTM_NEWQDISC) < 0)
871 goto err_out;
874 if (skb->len)
875 return rtnetlink_send(skb, &init_net, pid, RTNLGRP_TC, n->nlmsg_flags&NLM_F_ECHO);
877 err_out:
878 kfree_skb(skb);
879 return -EINVAL;
882 static int tc_dump_qdisc(struct sk_buff *skb, struct netlink_callback *cb)
884 struct net *net = skb->sk->sk_net;
885 int idx, q_idx;
886 int s_idx, s_q_idx;
887 struct net_device *dev;
888 struct Qdisc *q;
890 if (net != &init_net)
891 return 0;
893 s_idx = cb->args[0];
894 s_q_idx = q_idx = cb->args[1];
895 read_lock(&dev_base_lock);
896 idx = 0;
897 for_each_netdev(&init_net, dev) {
898 if (idx < s_idx)
899 goto cont;
900 if (idx > s_idx)
901 s_q_idx = 0;
902 q_idx = 0;
903 list_for_each_entry(q, &dev->qdisc_list, list) {
904 if (q_idx < s_q_idx) {
905 q_idx++;
906 continue;
908 if (tc_fill_qdisc(skb, q, q->parent, NETLINK_CB(cb->skb).pid,
909 cb->nlh->nlmsg_seq, NLM_F_MULTI, RTM_NEWQDISC) <= 0)
910 goto done;
911 q_idx++;
913 cont:
914 idx++;
917 done:
918 read_unlock(&dev_base_lock);
920 cb->args[0] = idx;
921 cb->args[1] = q_idx;
923 return skb->len;
928 /************************************************
929 * Traffic classes manipulation. *
930 ************************************************/
934 static int tc_ctl_tclass(struct sk_buff *skb, struct nlmsghdr *n, void *arg)
936 struct net *net = skb->sk->sk_net;
937 struct tcmsg *tcm = NLMSG_DATA(n);
938 struct rtattr **tca = arg;
939 struct net_device *dev;
940 struct Qdisc *q = NULL;
941 const struct Qdisc_class_ops *cops;
942 unsigned long cl = 0;
943 unsigned long new_cl;
944 u32 pid = tcm->tcm_parent;
945 u32 clid = tcm->tcm_handle;
946 u32 qid = TC_H_MAJ(clid);
947 int err;
949 if (net != &init_net)
950 return -EINVAL;
952 if ((dev = __dev_get_by_index(&init_net, tcm->tcm_ifindex)) == NULL)
953 return -ENODEV;
956 parent == TC_H_UNSPEC - unspecified parent.
957 parent == TC_H_ROOT - class is root, which has no parent.
958 parent == X:0 - parent is root class.
959 parent == X:Y - parent is a node in hierarchy.
960 parent == 0:Y - parent is X:Y, where X:0 is qdisc.
962 handle == 0:0 - generate handle from kernel pool.
963 handle == 0:Y - class is X:Y, where X:0 is qdisc.
964 handle == X:Y - clear.
965 handle == X:0 - root class.
968 /* Step 1. Determine qdisc handle X:0 */
970 if (pid != TC_H_ROOT) {
971 u32 qid1 = TC_H_MAJ(pid);
973 if (qid && qid1) {
974 /* If both majors are known, they must be identical. */
975 if (qid != qid1)
976 return -EINVAL;
977 } else if (qid1) {
978 qid = qid1;
979 } else if (qid == 0)
980 qid = dev->qdisc_sleeping->handle;
982 /* Now qid is genuine qdisc handle consistent
983 both with parent and child.
985 TC_H_MAJ(pid) still may be unspecified, complete it now.
987 if (pid)
988 pid = TC_H_MAKE(qid, pid);
989 } else {
990 if (qid == 0)
991 qid = dev->qdisc_sleeping->handle;
994 /* OK. Locate qdisc */
995 if ((q = qdisc_lookup(dev, qid)) == NULL)
996 return -ENOENT;
998 /* An check that it supports classes */
999 cops = q->ops->cl_ops;
1000 if (cops == NULL)
1001 return -EINVAL;
1003 /* Now try to get class */
1004 if (clid == 0) {
1005 if (pid == TC_H_ROOT)
1006 clid = qid;
1007 } else
1008 clid = TC_H_MAKE(qid, clid);
1010 if (clid)
1011 cl = cops->get(q, clid);
1013 if (cl == 0) {
1014 err = -ENOENT;
1015 if (n->nlmsg_type != RTM_NEWTCLASS || !(n->nlmsg_flags&NLM_F_CREATE))
1016 goto out;
1017 } else {
1018 switch (n->nlmsg_type) {
1019 case RTM_NEWTCLASS:
1020 err = -EEXIST;
1021 if (n->nlmsg_flags&NLM_F_EXCL)
1022 goto out;
1023 break;
1024 case RTM_DELTCLASS:
1025 err = cops->delete(q, cl);
1026 if (err == 0)
1027 tclass_notify(skb, n, q, cl, RTM_DELTCLASS);
1028 goto out;
1029 case RTM_GETTCLASS:
1030 err = tclass_notify(skb, n, q, cl, RTM_NEWTCLASS);
1031 goto out;
1032 default:
1033 err = -EINVAL;
1034 goto out;
1038 new_cl = cl;
1039 err = cops->change(q, clid, pid, tca, &new_cl);
1040 if (err == 0)
1041 tclass_notify(skb, n, q, new_cl, RTM_NEWTCLASS);
1043 out:
1044 if (cl)
1045 cops->put(q, cl);
1047 return err;
1051 static int tc_fill_tclass(struct sk_buff *skb, struct Qdisc *q,
1052 unsigned long cl,
1053 u32 pid, u32 seq, u16 flags, int event)
1055 struct tcmsg *tcm;
1056 struct nlmsghdr *nlh;
1057 unsigned char *b = skb_tail_pointer(skb);
1058 struct gnet_dump d;
1059 const struct Qdisc_class_ops *cl_ops = q->ops->cl_ops;
1061 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*tcm), flags);
1062 tcm = NLMSG_DATA(nlh);
1063 tcm->tcm_family = AF_UNSPEC;
1064 tcm->tcm_ifindex = q->dev->ifindex;
1065 tcm->tcm_parent = q->handle;
1066 tcm->tcm_handle = q->handle;
1067 tcm->tcm_info = 0;
1068 RTA_PUT(skb, TCA_KIND, IFNAMSIZ, q->ops->id);
1069 if (cl_ops->dump && cl_ops->dump(q, cl, skb, tcm) < 0)
1070 goto rtattr_failure;
1072 if (gnet_stats_start_copy_compat(skb, TCA_STATS2, TCA_STATS,
1073 TCA_XSTATS, q->stats_lock, &d) < 0)
1074 goto rtattr_failure;
1076 if (cl_ops->dump_stats && cl_ops->dump_stats(q, cl, &d) < 0)
1077 goto rtattr_failure;
1079 if (gnet_stats_finish_copy(&d) < 0)
1080 goto rtattr_failure;
1082 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1083 return skb->len;
1085 nlmsg_failure:
1086 rtattr_failure:
1087 nlmsg_trim(skb, b);
1088 return -1;
1091 static int tclass_notify(struct sk_buff *oskb, struct nlmsghdr *n,
1092 struct Qdisc *q, unsigned long cl, int event)
1094 struct sk_buff *skb;
1095 u32 pid = oskb ? NETLINK_CB(oskb).pid : 0;
1097 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1098 if (!skb)
1099 return -ENOBUFS;
1101 if (tc_fill_tclass(skb, q, cl, pid, n->nlmsg_seq, 0, event) < 0) {
1102 kfree_skb(skb);
1103 return -EINVAL;
1106 return rtnetlink_send(skb, &init_net, pid, RTNLGRP_TC, n->nlmsg_flags&NLM_F_ECHO);
1109 struct qdisc_dump_args
1111 struct qdisc_walker w;
1112 struct sk_buff *skb;
1113 struct netlink_callback *cb;
1116 static int qdisc_class_dump(struct Qdisc *q, unsigned long cl, struct qdisc_walker *arg)
1118 struct qdisc_dump_args *a = (struct qdisc_dump_args *)arg;
1120 return tc_fill_tclass(a->skb, q, cl, NETLINK_CB(a->cb->skb).pid,
1121 a->cb->nlh->nlmsg_seq, NLM_F_MULTI, RTM_NEWTCLASS);
1124 static int tc_dump_tclass(struct sk_buff *skb, struct netlink_callback *cb)
1126 struct net *net = skb->sk->sk_net;
1127 int t;
1128 int s_t;
1129 struct net_device *dev;
1130 struct Qdisc *q;
1131 struct tcmsg *tcm = (struct tcmsg*)NLMSG_DATA(cb->nlh);
1132 struct qdisc_dump_args arg;
1134 if (net != &init_net)
1135 return 0;
1137 if (cb->nlh->nlmsg_len < NLMSG_LENGTH(sizeof(*tcm)))
1138 return 0;
1139 if ((dev = dev_get_by_index(&init_net, tcm->tcm_ifindex)) == NULL)
1140 return 0;
1142 s_t = cb->args[0];
1143 t = 0;
1145 list_for_each_entry(q, &dev->qdisc_list, list) {
1146 if (t < s_t || !q->ops->cl_ops ||
1147 (tcm->tcm_parent &&
1148 TC_H_MAJ(tcm->tcm_parent) != q->handle)) {
1149 t++;
1150 continue;
1152 if (t > s_t)
1153 memset(&cb->args[1], 0, sizeof(cb->args)-sizeof(cb->args[0]));
1154 arg.w.fn = qdisc_class_dump;
1155 arg.skb = skb;
1156 arg.cb = cb;
1157 arg.w.stop = 0;
1158 arg.w.skip = cb->args[1];
1159 arg.w.count = 0;
1160 q->ops->cl_ops->walk(q, &arg.w);
1161 cb->args[1] = arg.w.count;
1162 if (arg.w.stop)
1163 break;
1164 t++;
1167 cb->args[0] = t;
1169 dev_put(dev);
1170 return skb->len;
1173 /* Main classifier routine: scans classifier chain attached
1174 to this qdisc, (optionally) tests for protocol and asks
1175 specific classifiers.
1177 int tc_classify_compat(struct sk_buff *skb, struct tcf_proto *tp,
1178 struct tcf_result *res)
1180 __be16 protocol = skb->protocol;
1181 int err = 0;
1183 for (; tp; tp = tp->next) {
1184 if ((tp->protocol == protocol ||
1185 tp->protocol == htons(ETH_P_ALL)) &&
1186 (err = tp->classify(skb, tp, res)) >= 0) {
1187 #ifdef CONFIG_NET_CLS_ACT
1188 if (err != TC_ACT_RECLASSIFY && skb->tc_verd)
1189 skb->tc_verd = SET_TC_VERD(skb->tc_verd, 0);
1190 #endif
1191 return err;
1194 return -1;
1196 EXPORT_SYMBOL(tc_classify_compat);
1198 int tc_classify(struct sk_buff *skb, struct tcf_proto *tp,
1199 struct tcf_result *res)
1201 int err = 0;
1202 __be16 protocol;
1203 #ifdef CONFIG_NET_CLS_ACT
1204 struct tcf_proto *otp = tp;
1205 reclassify:
1206 #endif
1207 protocol = skb->protocol;
1209 err = tc_classify_compat(skb, tp, res);
1210 #ifdef CONFIG_NET_CLS_ACT
1211 if (err == TC_ACT_RECLASSIFY) {
1212 u32 verd = G_TC_VERD(skb->tc_verd);
1213 tp = otp;
1215 if (verd++ >= MAX_REC_LOOP) {
1216 printk("rule prio %u protocol %02x reclassify loop, "
1217 "packet dropped\n",
1218 tp->prio&0xffff, ntohs(tp->protocol));
1219 return TC_ACT_SHOT;
1221 skb->tc_verd = SET_TC_VERD(skb->tc_verd, verd);
1222 goto reclassify;
1224 #endif
1225 return err;
1227 EXPORT_SYMBOL(tc_classify);
1229 void tcf_destroy(struct tcf_proto *tp)
1231 tp->ops->destroy(tp);
1232 module_put(tp->ops->owner);
1233 kfree(tp);
1236 void tcf_destroy_chain(struct tcf_proto *fl)
1238 struct tcf_proto *tp;
1240 while ((tp = fl) != NULL) {
1241 fl = tp->next;
1242 tcf_destroy(tp);
1245 EXPORT_SYMBOL(tcf_destroy_chain);
1247 #ifdef CONFIG_PROC_FS
1248 static int psched_show(struct seq_file *seq, void *v)
1250 struct timespec ts;
1252 hrtimer_get_res(CLOCK_MONOTONIC, &ts);
1253 seq_printf(seq, "%08x %08x %08x %08x\n",
1254 (u32)NSEC_PER_USEC, (u32)PSCHED_US2NS(1),
1255 1000000,
1256 (u32)NSEC_PER_SEC/(u32)ktime_to_ns(timespec_to_ktime(ts)));
1258 return 0;
1261 static int psched_open(struct inode *inode, struct file *file)
1263 return single_open(file, psched_show, PDE(inode)->data);
1266 static const struct file_operations psched_fops = {
1267 .owner = THIS_MODULE,
1268 .open = psched_open,
1269 .read = seq_read,
1270 .llseek = seq_lseek,
1271 .release = single_release,
1273 #endif
1275 static int __init pktsched_init(void)
1277 register_qdisc(&pfifo_qdisc_ops);
1278 register_qdisc(&bfifo_qdisc_ops);
1279 proc_net_fops_create(&init_net, "psched", 0, &psched_fops);
1281 rtnl_register(PF_UNSPEC, RTM_NEWQDISC, tc_modify_qdisc, NULL);
1282 rtnl_register(PF_UNSPEC, RTM_DELQDISC, tc_get_qdisc, NULL);
1283 rtnl_register(PF_UNSPEC, RTM_GETQDISC, tc_get_qdisc, tc_dump_qdisc);
1284 rtnl_register(PF_UNSPEC, RTM_NEWTCLASS, tc_ctl_tclass, NULL);
1285 rtnl_register(PF_UNSPEC, RTM_DELTCLASS, tc_ctl_tclass, NULL);
1286 rtnl_register(PF_UNSPEC, RTM_GETTCLASS, tc_ctl_tclass, tc_dump_tclass);
1288 return 0;
1291 subsys_initcall(pktsched_init);
1293 EXPORT_SYMBOL(qdisc_get_rtab);
1294 EXPORT_SYMBOL(qdisc_put_rtab);
1295 EXPORT_SYMBOL(register_qdisc);
1296 EXPORT_SYMBOL(unregister_qdisc);