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>
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>
23 #include <linux/socket.h>
24 #include <linux/sockios.h>
26 #include <linux/errno.h>
27 #include <linux/interrupt.h>
28 #include <linux/netdevice.h>
29 #include <linux/skbuff.h>
30 #include <linux/rtnetlink.h>
31 #include <linux/init.h>
32 #include <linux/proc_fs.h>
33 #include <linux/seq_file.h>
34 #include <linux/kmod.h>
35 #include <linux/list.h>
36 #include <linux/bitops.h>
37 #include <linux/hrtimer.h>
39 #include <net/netlink.h>
41 #include <net/pkt_sched.h>
43 #include <asm/processor.h>
44 #include <asm/uaccess.h>
45 #include <asm/system.h>
47 static int qdisc_notify(struct sk_buff *oskb, struct nlmsghdr *n, u32 clid,
48 struct Qdisc *old, struct Qdisc *new);
49 static int tclass_notify(struct sk_buff *oskb, struct nlmsghdr *n,
50 struct Qdisc *q, unsigned long cl, int event);
57 This file consists of two interrelated parts:
59 1. queueing disciplines manager frontend.
60 2. traffic classes manager frontend.
62 Generally, queueing discipline ("qdisc") is a black box,
63 which is able to enqueue packets and to dequeue them (when
64 device is ready to send something) in order and at times
65 determined by algorithm hidden in it.
67 qdisc's are divided to two categories:
68 - "queues", which have no internal structure visible from outside.
69 - "schedulers", which split all the packets to "traffic classes",
70 using "packet classifiers" (look at cls_api.c)
72 In turn, classes may have child qdiscs (as rule, queues)
73 attached to them etc. etc. etc.
75 The goal of the routines in this file is to translate
76 information supplied by user in the form of handles
77 to more intelligible for kernel form, to make some sanity
78 checks and part of work, which is common to all qdiscs
79 and to provide rtnetlink notifications.
81 All real intelligent work is done inside qdisc modules.
85 Every discipline has two major routines: enqueue and dequeue.
89 dequeue usually returns a skb to send. It is allowed to return NULL,
90 but it does not mean that queue is empty, it just means that
91 discipline does not want to send anything this time.
92 Queue is really empty if q->q.qlen == 0.
93 For complicated disciplines with multiple queues q->q is not
94 real packet queue, but however q->q.qlen must be valid.
98 enqueue returns 0, if packet was enqueued successfully.
99 If packet (this one or another one) was dropped, it returns
101 NET_XMIT_DROP - this packet dropped
102 Expected action: do not backoff, but wait until queue will clear.
103 NET_XMIT_CN - probably this packet enqueued, but another one dropped.
104 Expected action: backoff or ignore
105 NET_XMIT_POLICED - dropped by police.
106 Expected action: backoff or error to real-time apps.
112 requeues once dequeued packet. It is used for non-standard or
113 just buggy devices, which can defer output even if dev->tbusy=0.
117 returns qdisc to initial state: purge all buffers, clear all
118 timers, counters (except for statistics) etc.
122 initializes newly created qdisc.
126 destroys resources allocated by init and during lifetime of qdisc.
130 changes qdisc parameters.
133 /* Protects list of registered TC modules. It is pure SMP lock. */
134 static DEFINE_RWLOCK(qdisc_mod_lock);
137 /************************************************
138 * Queueing disciplines manipulation. *
139 ************************************************/
142 /* The list of all installed queueing disciplines. */
144 static struct Qdisc_ops *qdisc_base;
146 /* Register/uregister queueing discipline */
148 int register_qdisc(struct Qdisc_ops *qops)
150 struct Qdisc_ops *q, **qp;
153 write_lock(&qdisc_mod_lock);
154 for (qp = &qdisc_base; (q = *qp) != NULL; qp = &q->next)
155 if (!strcmp(qops->id, q->id))
158 if (qops->enqueue == NULL)
159 qops->enqueue = noop_qdisc_ops.enqueue;
160 if (qops->requeue == NULL)
161 qops->requeue = noop_qdisc_ops.requeue;
162 if (qops->dequeue == NULL)
163 qops->dequeue = noop_qdisc_ops.dequeue;
169 write_unlock(&qdisc_mod_lock);
173 int unregister_qdisc(struct Qdisc_ops *qops)
175 struct Qdisc_ops *q, **qp;
178 write_lock(&qdisc_mod_lock);
179 for (qp = &qdisc_base; (q=*qp)!=NULL; qp = &q->next)
187 write_unlock(&qdisc_mod_lock);
191 /* We know handle. Find qdisc among all qdisc's attached to device
192 (root qdisc, all its children, children of children etc.)
195 static struct Qdisc *__qdisc_lookup(struct net_device *dev, u32 handle)
199 list_for_each_entry(q, &dev->qdisc_list, list) {
200 if (q->handle == handle)
206 struct Qdisc *qdisc_lookup(struct net_device *dev, u32 handle)
210 read_lock(&qdisc_tree_lock);
211 q = __qdisc_lookup(dev, handle);
212 read_unlock(&qdisc_tree_lock);
216 static struct Qdisc *qdisc_leaf(struct Qdisc *p, u32 classid)
220 struct Qdisc_class_ops *cops = p->ops->cl_ops;
224 cl = cops->get(p, classid);
228 leaf = cops->leaf(p, cl);
233 /* Find queueing discipline by name */
235 static struct Qdisc_ops *qdisc_lookup_ops(struct rtattr *kind)
237 struct Qdisc_ops *q = NULL;
240 read_lock(&qdisc_mod_lock);
241 for (q = qdisc_base; q; q = q->next) {
242 if (rtattr_strcmp(kind, q->id) == 0) {
243 if (!try_module_get(q->owner))
248 read_unlock(&qdisc_mod_lock);
253 static struct qdisc_rate_table *qdisc_rtab_list;
255 struct qdisc_rate_table *qdisc_get_rtab(struct tc_ratespec *r, struct rtattr *tab)
257 struct qdisc_rate_table *rtab;
259 for (rtab = qdisc_rtab_list; rtab; rtab = rtab->next) {
260 if (memcmp(&rtab->rate, r, sizeof(struct tc_ratespec)) == 0) {
266 if (tab == NULL || r->rate == 0 || r->cell_log == 0 || RTA_PAYLOAD(tab) != 1024)
269 rtab = kmalloc(sizeof(*rtab), GFP_KERNEL);
273 memcpy(rtab->data, RTA_DATA(tab), 1024);
274 rtab->next = qdisc_rtab_list;
275 qdisc_rtab_list = rtab;
280 void qdisc_put_rtab(struct qdisc_rate_table *tab)
282 struct qdisc_rate_table *rtab, **rtabp;
284 if (!tab || --tab->refcnt)
287 for (rtabp = &qdisc_rtab_list; (rtab=*rtabp) != NULL; rtabp = &rtab->next) {
296 static enum hrtimer_restart qdisc_watchdog(struct hrtimer *timer)
298 struct qdisc_watchdog *wd = container_of(timer, struct qdisc_watchdog,
301 wd->qdisc->flags &= ~TCQ_F_THROTTLED;
302 netif_schedule(wd->qdisc->dev);
303 return HRTIMER_NORESTART;
306 void qdisc_watchdog_init(struct qdisc_watchdog *wd, struct Qdisc *qdisc)
308 hrtimer_init(&wd->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
309 wd->timer.function = qdisc_watchdog;
312 EXPORT_SYMBOL(qdisc_watchdog_init);
314 void qdisc_watchdog_schedule(struct qdisc_watchdog *wd, psched_time_t expires)
318 wd->qdisc->flags |= TCQ_F_THROTTLED;
319 time = ktime_set(0, 0);
320 time = ktime_add_ns(time, PSCHED_US2NS(expires));
321 hrtimer_start(&wd->timer, time, HRTIMER_MODE_ABS);
323 EXPORT_SYMBOL(qdisc_watchdog_schedule);
325 void qdisc_watchdog_cancel(struct qdisc_watchdog *wd)
327 hrtimer_cancel(&wd->timer);
328 wd->qdisc->flags &= ~TCQ_F_THROTTLED;
330 EXPORT_SYMBOL(qdisc_watchdog_cancel);
332 /* Allocate an unique handle from space managed by kernel */
334 static u32 qdisc_alloc_handle(struct net_device *dev)
337 static u32 autohandle = TC_H_MAKE(0x80000000U, 0);
340 autohandle += TC_H_MAKE(0x10000U, 0);
341 if (autohandle == TC_H_MAKE(TC_H_ROOT, 0))
342 autohandle = TC_H_MAKE(0x80000000U, 0);
343 } while (qdisc_lookup(dev, autohandle) && --i > 0);
345 return i>0 ? autohandle : 0;
348 /* Attach toplevel qdisc to device dev */
350 static struct Qdisc *
351 dev_graft_qdisc(struct net_device *dev, struct Qdisc *qdisc)
353 struct Qdisc *oqdisc;
355 if (dev->flags & IFF_UP)
358 qdisc_lock_tree(dev);
359 if (qdisc && qdisc->flags&TCQ_F_INGRESS) {
360 oqdisc = dev->qdisc_ingress;
361 /* Prune old scheduler */
362 if (oqdisc && atomic_read(&oqdisc->refcnt) <= 1) {
365 dev->qdisc_ingress = NULL;
367 dev->qdisc_ingress = qdisc;
372 oqdisc = dev->qdisc_sleeping;
374 /* Prune old scheduler */
375 if (oqdisc && atomic_read(&oqdisc->refcnt) <= 1)
378 /* ... and graft new one */
381 dev->qdisc_sleeping = qdisc;
382 dev->qdisc = &noop_qdisc;
385 qdisc_unlock_tree(dev);
387 if (dev->flags & IFF_UP)
393 void qdisc_tree_decrease_qlen(struct Qdisc *sch, unsigned int n)
395 struct Qdisc_class_ops *cops;
401 while ((parentid = sch->parent)) {
402 sch = __qdisc_lookup(sch->dev, TC_H_MAJ(parentid));
403 cops = sch->ops->cl_ops;
404 if (cops->qlen_notify) {
405 cl = cops->get(sch, parentid);
406 cops->qlen_notify(sch, cl);
412 EXPORT_SYMBOL(qdisc_tree_decrease_qlen);
414 /* Graft qdisc "new" to class "classid" of qdisc "parent" or
417 Old qdisc is not destroyed but returned in *old.
420 static int qdisc_graft(struct net_device *dev, struct Qdisc *parent,
422 struct Qdisc *new, struct Qdisc **old)
425 struct Qdisc *q = *old;
428 if (parent == NULL) {
429 if (q && q->flags&TCQ_F_INGRESS) {
430 *old = dev_graft_qdisc(dev, q);
432 *old = dev_graft_qdisc(dev, new);
435 struct Qdisc_class_ops *cops = parent->ops->cl_ops;
440 unsigned long cl = cops->get(parent, classid);
442 err = cops->graft(parent, cl, new, old);
444 new->parent = classid;
445 cops->put(parent, cl);
453 Allocate and initialize new qdisc.
455 Parameters are passed via opt.
458 static struct Qdisc *
459 qdisc_create(struct net_device *dev, u32 handle, struct rtattr **tca, int *errp)
462 struct rtattr *kind = tca[TCA_KIND-1];
464 struct Qdisc_ops *ops;
466 ops = qdisc_lookup_ops(kind);
468 if (ops == NULL && kind != NULL) {
470 if (rtattr_strlcpy(name, kind, IFNAMSIZ) < IFNAMSIZ) {
471 /* We dropped the RTNL semaphore in order to
472 * perform the module load. So, even if we
473 * succeeded in loading the module we have to
474 * tell the caller to replay the request. We
475 * indicate this using -EAGAIN.
476 * We replay the request because the device may
477 * go away in the mean time.
480 request_module("sch_%s", name);
482 ops = qdisc_lookup_ops(kind);
484 /* We will try again qdisc_lookup_ops,
485 * so don't keep a reference.
487 module_put(ops->owner);
499 sch = qdisc_alloc(dev, ops);
505 if (handle == TC_H_INGRESS) {
506 sch->flags |= TCQ_F_INGRESS;
507 handle = TC_H_MAKE(TC_H_INGRESS, 0);
508 } else if (handle == 0) {
509 handle = qdisc_alloc_handle(dev);
515 sch->handle = handle;
517 if (!ops->init || (err = ops->init(sch, tca[TCA_OPTIONS-1])) == 0) {
518 #ifdef CONFIG_NET_ESTIMATOR
519 if (tca[TCA_RATE-1]) {
520 err = gen_new_estimator(&sch->bstats, &sch->rate_est,
525 * Any broken qdiscs that would require
526 * a ops->reset() here? The qdisc was never
527 * in action so it shouldn't be necessary.
535 qdisc_lock_tree(dev);
536 list_add_tail(&sch->list, &dev->qdisc_list);
537 qdisc_unlock_tree(dev);
543 kfree((char *) sch - sch->padded);
545 module_put(ops->owner);
551 static int qdisc_change(struct Qdisc *sch, struct rtattr **tca)
553 if (tca[TCA_OPTIONS-1]) {
556 if (sch->ops->change == NULL)
558 err = sch->ops->change(sch, tca[TCA_OPTIONS-1]);
562 #ifdef CONFIG_NET_ESTIMATOR
564 gen_replace_estimator(&sch->bstats, &sch->rate_est,
565 sch->stats_lock, tca[TCA_RATE-1]);
570 struct check_loop_arg
572 struct qdisc_walker w;
577 static int check_loop_fn(struct Qdisc *q, unsigned long cl, struct qdisc_walker *w);
579 static int check_loop(struct Qdisc *q, struct Qdisc *p, int depth)
581 struct check_loop_arg arg;
583 if (q->ops->cl_ops == NULL)
586 arg.w.stop = arg.w.skip = arg.w.count = 0;
587 arg.w.fn = check_loop_fn;
590 q->ops->cl_ops->walk(q, &arg.w);
591 return arg.w.stop ? -ELOOP : 0;
595 check_loop_fn(struct Qdisc *q, unsigned long cl, struct qdisc_walker *w)
598 struct Qdisc_class_ops *cops = q->ops->cl_ops;
599 struct check_loop_arg *arg = (struct check_loop_arg *)w;
601 leaf = cops->leaf(q, cl);
603 if (leaf == arg->p || arg->depth > 7)
605 return check_loop(leaf, arg->p, arg->depth + 1);
614 static int tc_get_qdisc(struct sk_buff *skb, struct nlmsghdr *n, void *arg)
616 struct tcmsg *tcm = NLMSG_DATA(n);
617 struct rtattr **tca = arg;
618 struct net_device *dev;
619 u32 clid = tcm->tcm_parent;
620 struct Qdisc *q = NULL;
621 struct Qdisc *p = NULL;
624 if ((dev = __dev_get_by_index(tcm->tcm_ifindex)) == NULL)
628 if (clid != TC_H_ROOT) {
629 if (TC_H_MAJ(clid) != TC_H_MAJ(TC_H_INGRESS)) {
630 if ((p = qdisc_lookup(dev, TC_H_MAJ(clid))) == NULL)
632 q = qdisc_leaf(p, clid);
633 } else { /* ingress */
634 q = dev->qdisc_ingress;
637 q = dev->qdisc_sleeping;
642 if (tcm->tcm_handle && q->handle != tcm->tcm_handle)
645 if ((q = qdisc_lookup(dev, tcm->tcm_handle)) == NULL)
649 if (tca[TCA_KIND-1] && rtattr_strcmp(tca[TCA_KIND-1], q->ops->id))
652 if (n->nlmsg_type == RTM_DELQDISC) {
657 if ((err = qdisc_graft(dev, p, clid, NULL, &q)) != 0)
660 qdisc_notify(skb, n, clid, q, NULL);
661 spin_lock_bh(&dev->queue_lock);
663 spin_unlock_bh(&dev->queue_lock);
666 qdisc_notify(skb, n, clid, NULL, q);
675 static int tc_modify_qdisc(struct sk_buff *skb, struct nlmsghdr *n, void *arg)
679 struct net_device *dev;
685 /* Reinit, just in case something touches this. */
688 clid = tcm->tcm_parent;
691 if ((dev = __dev_get_by_index(tcm->tcm_ifindex)) == NULL)
695 if (clid != TC_H_ROOT) {
696 if (clid != TC_H_INGRESS) {
697 if ((p = qdisc_lookup(dev, TC_H_MAJ(clid))) == NULL)
699 q = qdisc_leaf(p, clid);
700 } else { /*ingress */
701 q = dev->qdisc_ingress;
704 q = dev->qdisc_sleeping;
707 /* It may be default qdisc, ignore it */
708 if (q && q->handle == 0)
711 if (!q || !tcm->tcm_handle || q->handle != tcm->tcm_handle) {
712 if (tcm->tcm_handle) {
713 if (q && !(n->nlmsg_flags&NLM_F_REPLACE))
715 if (TC_H_MIN(tcm->tcm_handle))
717 if ((q = qdisc_lookup(dev, tcm->tcm_handle)) == NULL)
719 if (n->nlmsg_flags&NLM_F_EXCL)
721 if (tca[TCA_KIND-1] && rtattr_strcmp(tca[TCA_KIND-1], q->ops->id))
724 (p && check_loop(q, p, 0)))
726 atomic_inc(&q->refcnt);
732 /* This magic test requires explanation.
734 * We know, that some child q is already
735 * attached to this parent and have choice:
736 * either to change it or to create/graft new one.
738 * 1. We are allowed to create/graft only
739 * if CREATE and REPLACE flags are set.
741 * 2. If EXCL is set, requestor wanted to say,
742 * that qdisc tcm_handle is not expected
743 * to exist, so that we choose create/graft too.
745 * 3. The last case is when no flags are set.
746 * Alas, it is sort of hole in API, we
747 * cannot decide what to do unambiguously.
748 * For now we select create/graft, if
749 * user gave KIND, which does not match existing.
751 if ((n->nlmsg_flags&NLM_F_CREATE) &&
752 (n->nlmsg_flags&NLM_F_REPLACE) &&
753 ((n->nlmsg_flags&NLM_F_EXCL) ||
755 rtattr_strcmp(tca[TCA_KIND-1], q->ops->id))))
760 if (!tcm->tcm_handle)
762 q = qdisc_lookup(dev, tcm->tcm_handle);
765 /* Change qdisc parameters */
768 if (n->nlmsg_flags&NLM_F_EXCL)
770 if (tca[TCA_KIND-1] && rtattr_strcmp(tca[TCA_KIND-1], q->ops->id))
772 err = qdisc_change(q, tca);
774 qdisc_notify(skb, n, clid, NULL, q);
778 if (!(n->nlmsg_flags&NLM_F_CREATE))
780 if (clid == TC_H_INGRESS)
781 q = qdisc_create(dev, tcm->tcm_parent, tca, &err);
783 q = qdisc_create(dev, tcm->tcm_handle, tca, &err);
792 struct Qdisc *old_q = NULL;
793 err = qdisc_graft(dev, p, clid, q, &old_q);
796 spin_lock_bh(&dev->queue_lock);
798 spin_unlock_bh(&dev->queue_lock);
802 qdisc_notify(skb, n, clid, old_q, q);
804 spin_lock_bh(&dev->queue_lock);
805 qdisc_destroy(old_q);
806 spin_unlock_bh(&dev->queue_lock);
812 static int tc_fill_qdisc(struct sk_buff *skb, struct Qdisc *q, u32 clid,
813 u32 pid, u32 seq, u16 flags, int event)
816 struct nlmsghdr *nlh;
817 unsigned char *b = skb_tail_pointer(skb);
820 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*tcm), flags);
821 tcm = NLMSG_DATA(nlh);
822 tcm->tcm_family = AF_UNSPEC;
825 tcm->tcm_ifindex = q->dev->ifindex;
826 tcm->tcm_parent = clid;
827 tcm->tcm_handle = q->handle;
828 tcm->tcm_info = atomic_read(&q->refcnt);
829 RTA_PUT(skb, TCA_KIND, IFNAMSIZ, q->ops->id);
830 if (q->ops->dump && q->ops->dump(q, skb) < 0)
832 q->qstats.qlen = q->q.qlen;
834 if (gnet_stats_start_copy_compat(skb, TCA_STATS2, TCA_STATS,
835 TCA_XSTATS, q->stats_lock, &d) < 0)
838 if (q->ops->dump_stats && q->ops->dump_stats(q, &d) < 0)
841 if (gnet_stats_copy_basic(&d, &q->bstats) < 0 ||
842 #ifdef CONFIG_NET_ESTIMATOR
843 gnet_stats_copy_rate_est(&d, &q->rate_est) < 0 ||
845 gnet_stats_copy_queue(&d, &q->qstats) < 0)
848 if (gnet_stats_finish_copy(&d) < 0)
851 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
860 static int qdisc_notify(struct sk_buff *oskb, struct nlmsghdr *n,
861 u32 clid, struct Qdisc *old, struct Qdisc *new)
864 u32 pid = oskb ? NETLINK_CB(oskb).pid : 0;
866 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
870 if (old && old->handle) {
871 if (tc_fill_qdisc(skb, old, clid, pid, n->nlmsg_seq, 0, RTM_DELQDISC) < 0)
875 if (tc_fill_qdisc(skb, new, clid, pid, n->nlmsg_seq, old ? NLM_F_REPLACE : 0, RTM_NEWQDISC) < 0)
880 return rtnetlink_send(skb, pid, RTNLGRP_TC, n->nlmsg_flags&NLM_F_ECHO);
887 static int tc_dump_qdisc(struct sk_buff *skb, struct netlink_callback *cb)
891 struct net_device *dev;
895 s_q_idx = q_idx = cb->args[1];
896 read_lock(&dev_base_lock);
897 for (dev=dev_base, idx=0; dev; dev = dev->next, idx++) {
902 read_lock(&qdisc_tree_lock);
904 list_for_each_entry(q, &dev->qdisc_list, list) {
905 if (q_idx < s_q_idx) {
909 if (tc_fill_qdisc(skb, q, q->parent, NETLINK_CB(cb->skb).pid,
910 cb->nlh->nlmsg_seq, NLM_F_MULTI, RTM_NEWQDISC) <= 0) {
911 read_unlock(&qdisc_tree_lock);
916 read_unlock(&qdisc_tree_lock);
920 read_unlock(&dev_base_lock);
930 /************************************************
931 * Traffic classes manipulation. *
932 ************************************************/
936 static int tc_ctl_tclass(struct sk_buff *skb, struct nlmsghdr *n, void *arg)
938 struct tcmsg *tcm = NLMSG_DATA(n);
939 struct rtattr **tca = arg;
940 struct net_device *dev;
941 struct Qdisc *q = NULL;
942 struct Qdisc_class_ops *cops;
943 unsigned long cl = 0;
944 unsigned long new_cl;
945 u32 pid = tcm->tcm_parent;
946 u32 clid = tcm->tcm_handle;
947 u32 qid = TC_H_MAJ(clid);
950 if ((dev = __dev_get_by_index(tcm->tcm_ifindex)) == NULL)
954 parent == TC_H_UNSPEC - unspecified parent.
955 parent == TC_H_ROOT - class is root, which has no parent.
956 parent == X:0 - parent is root class.
957 parent == X:Y - parent is a node in hierarchy.
958 parent == 0:Y - parent is X:Y, where X:0 is qdisc.
960 handle == 0:0 - generate handle from kernel pool.
961 handle == 0:Y - class is X:Y, where X:0 is qdisc.
962 handle == X:Y - clear.
963 handle == X:0 - root class.
966 /* Step 1. Determine qdisc handle X:0 */
968 if (pid != TC_H_ROOT) {
969 u32 qid1 = TC_H_MAJ(pid);
972 /* If both majors are known, they must be identical. */
978 qid = dev->qdisc_sleeping->handle;
980 /* Now qid is genuine qdisc handle consistent
981 both with parent and child.
983 TC_H_MAJ(pid) still may be unspecified, complete it now.
986 pid = TC_H_MAKE(qid, pid);
989 qid = dev->qdisc_sleeping->handle;
992 /* OK. Locate qdisc */
993 if ((q = qdisc_lookup(dev, qid)) == NULL)
996 /* An check that it supports classes */
997 cops = q->ops->cl_ops;
1001 /* Now try to get class */
1003 if (pid == TC_H_ROOT)
1006 clid = TC_H_MAKE(qid, clid);
1009 cl = cops->get(q, clid);
1013 if (n->nlmsg_type != RTM_NEWTCLASS || !(n->nlmsg_flags&NLM_F_CREATE))
1016 switch (n->nlmsg_type) {
1019 if (n->nlmsg_flags&NLM_F_EXCL)
1023 err = cops->delete(q, cl);
1025 tclass_notify(skb, n, q, cl, RTM_DELTCLASS);
1028 err = tclass_notify(skb, n, q, cl, RTM_NEWTCLASS);
1037 err = cops->change(q, clid, pid, tca, &new_cl);
1039 tclass_notify(skb, n, q, new_cl, RTM_NEWTCLASS);
1049 static int tc_fill_tclass(struct sk_buff *skb, struct Qdisc *q,
1051 u32 pid, u32 seq, u16 flags, int event)
1054 struct nlmsghdr *nlh;
1055 unsigned char *b = skb_tail_pointer(skb);
1057 struct Qdisc_class_ops *cl_ops = q->ops->cl_ops;
1059 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*tcm), flags);
1060 tcm = NLMSG_DATA(nlh);
1061 tcm->tcm_family = AF_UNSPEC;
1062 tcm->tcm_ifindex = q->dev->ifindex;
1063 tcm->tcm_parent = q->handle;
1064 tcm->tcm_handle = q->handle;
1066 RTA_PUT(skb, TCA_KIND, IFNAMSIZ, q->ops->id);
1067 if (cl_ops->dump && cl_ops->dump(q, cl, skb, tcm) < 0)
1068 goto rtattr_failure;
1070 if (gnet_stats_start_copy_compat(skb, TCA_STATS2, TCA_STATS,
1071 TCA_XSTATS, q->stats_lock, &d) < 0)
1072 goto rtattr_failure;
1074 if (cl_ops->dump_stats && cl_ops->dump_stats(q, cl, &d) < 0)
1075 goto rtattr_failure;
1077 if (gnet_stats_finish_copy(&d) < 0)
1078 goto rtattr_failure;
1080 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1089 static int tclass_notify(struct sk_buff *oskb, struct nlmsghdr *n,
1090 struct Qdisc *q, unsigned long cl, int event)
1092 struct sk_buff *skb;
1093 u32 pid = oskb ? NETLINK_CB(oskb).pid : 0;
1095 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1099 if (tc_fill_tclass(skb, q, cl, pid, n->nlmsg_seq, 0, event) < 0) {
1104 return rtnetlink_send(skb, pid, RTNLGRP_TC, n->nlmsg_flags&NLM_F_ECHO);
1107 struct qdisc_dump_args
1109 struct qdisc_walker w;
1110 struct sk_buff *skb;
1111 struct netlink_callback *cb;
1114 static int qdisc_class_dump(struct Qdisc *q, unsigned long cl, struct qdisc_walker *arg)
1116 struct qdisc_dump_args *a = (struct qdisc_dump_args *)arg;
1118 return tc_fill_tclass(a->skb, q, cl, NETLINK_CB(a->cb->skb).pid,
1119 a->cb->nlh->nlmsg_seq, NLM_F_MULTI, RTM_NEWTCLASS);
1122 static int tc_dump_tclass(struct sk_buff *skb, struct netlink_callback *cb)
1126 struct net_device *dev;
1128 struct tcmsg *tcm = (struct tcmsg*)NLMSG_DATA(cb->nlh);
1129 struct qdisc_dump_args arg;
1131 if (cb->nlh->nlmsg_len < NLMSG_LENGTH(sizeof(*tcm)))
1133 if ((dev = dev_get_by_index(tcm->tcm_ifindex)) == NULL)
1139 read_lock(&qdisc_tree_lock);
1140 list_for_each_entry(q, &dev->qdisc_list, list) {
1141 if (t < s_t || !q->ops->cl_ops ||
1143 TC_H_MAJ(tcm->tcm_parent) != q->handle)) {
1148 memset(&cb->args[1], 0, sizeof(cb->args)-sizeof(cb->args[0]));
1149 arg.w.fn = qdisc_class_dump;
1153 arg.w.skip = cb->args[1];
1155 q->ops->cl_ops->walk(q, &arg.w);
1156 cb->args[1] = arg.w.count;
1161 read_unlock(&qdisc_tree_lock);
1169 /* Main classifier routine: scans classifier chain attached
1170 to this qdisc, (optionally) tests for protocol and asks
1171 specific classifiers.
1173 int tc_classify(struct sk_buff *skb, struct tcf_proto *tp,
1174 struct tcf_result *res)
1177 __be16 protocol = skb->protocol;
1178 #ifdef CONFIG_NET_CLS_ACT
1179 struct tcf_proto *otp = tp;
1182 protocol = skb->protocol;
1184 for ( ; tp; tp = tp->next) {
1185 if ((tp->protocol == protocol ||
1186 tp->protocol == htons(ETH_P_ALL)) &&
1187 (err = tp->classify(skb, tp, res)) >= 0) {
1188 #ifdef CONFIG_NET_CLS_ACT
1189 if ( TC_ACT_RECLASSIFY == err) {
1190 __u32 verd = (__u32) G_TC_VERD(skb->tc_verd);
1193 if (MAX_REC_LOOP < verd++) {
1194 printk("rule prio %d protocol %02x reclassify is buggy packet dropped\n",
1195 tp->prio&0xffff, ntohs(tp->protocol));
1198 skb->tc_verd = SET_TC_VERD(skb->tc_verd,verd);
1202 skb->tc_verd = SET_TC_VERD(skb->tc_verd,0);
1215 #ifdef CONFIG_PROC_FS
1216 static int psched_show(struct seq_file *seq, void *v)
1218 seq_printf(seq, "%08x %08x %08x %08x\n",
1219 (u32)NSEC_PER_USEC, (u32)PSCHED_US2NS(1),
1221 (u32)NSEC_PER_SEC/(u32)ktime_to_ns(KTIME_MONOTONIC_RES));
1226 static int psched_open(struct inode *inode, struct file *file)
1228 return single_open(file, psched_show, PDE(inode)->data);
1231 static const struct file_operations psched_fops = {
1232 .owner = THIS_MODULE,
1233 .open = psched_open,
1235 .llseek = seq_lseek,
1236 .release = single_release,
1240 static int __init pktsched_init(void)
1242 struct rtnetlink_link *link_p;
1244 link_p = rtnetlink_links[PF_UNSPEC];
1246 /* Setup rtnetlink links. It is made here to avoid
1247 exporting large number of public symbols.
1251 link_p[RTM_NEWQDISC-RTM_BASE].doit = tc_modify_qdisc;
1252 link_p[RTM_DELQDISC-RTM_BASE].doit = tc_get_qdisc;
1253 link_p[RTM_GETQDISC-RTM_BASE].doit = tc_get_qdisc;
1254 link_p[RTM_GETQDISC-RTM_BASE].dumpit = tc_dump_qdisc;
1255 link_p[RTM_NEWTCLASS-RTM_BASE].doit = tc_ctl_tclass;
1256 link_p[RTM_DELTCLASS-RTM_BASE].doit = tc_ctl_tclass;
1257 link_p[RTM_GETTCLASS-RTM_BASE].doit = tc_ctl_tclass;
1258 link_p[RTM_GETTCLASS-RTM_BASE].dumpit = tc_dump_tclass;
1261 register_qdisc(&pfifo_qdisc_ops);
1262 register_qdisc(&bfifo_qdisc_ops);
1263 proc_net_fops_create("psched", 0, &psched_fops);
1268 subsys_initcall(pktsched_init);
1270 EXPORT_SYMBOL(qdisc_get_rtab);
1271 EXPORT_SYMBOL(qdisc_put_rtab);
1272 EXPORT_SYMBOL(register_qdisc);
1273 EXPORT_SYMBOL(unregister_qdisc);
1274 EXPORT_SYMBOL(tc_classify);