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>
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>
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);
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.
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.
87 enqueue returns 0, if packet was enqueued successfully.
88 If packet (this one or another one) was dropped, it returns
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.
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.
106 returns qdisc to initial state: purge all buffers, clear all
107 timers, counters (except for statistics) etc.
111 initializes newly created qdisc.
115 destroys resources allocated by init and during lifetime of qdisc.
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;
142 write_lock(&qdisc_mod_lock);
143 for (qp = &qdisc_base; (q = *qp) != NULL; qp = &q->next)
144 if (!strcmp(qops->id, q->id))
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;
158 write_unlock(&qdisc_mod_lock);
161 EXPORT_SYMBOL(register_qdisc);
163 int unregister_qdisc(struct Qdisc_ops *qops)
165 struct Qdisc_ops *q, **qp;
168 write_lock(&qdisc_mod_lock);
169 for (qp = &qdisc_base; (q=*qp)!=NULL; qp = &q->next)
177 write_unlock(&qdisc_mod_lock);
180 EXPORT_SYMBOL(unregister_qdisc);
182 /* We know handle. Find qdisc among all qdisc's attached to device
183 (root qdisc, all its children, children of children etc.)
186 struct Qdisc *qdisc_lookup(struct net_device *dev, u32 handle)
190 list_for_each_entry(q, &dev->qdisc_list, list) {
191 if (q->handle == handle)
197 static struct Qdisc *qdisc_leaf(struct Qdisc *p, u32 classid)
201 const struct Qdisc_class_ops *cops = p->ops->cl_ops;
205 cl = cops->get(p, classid);
209 leaf = cops->leaf(p, cl);
214 /* Find queueing discipline by name */
216 static struct Qdisc_ops *qdisc_lookup_ops(struct rtattr *kind)
218 struct Qdisc_ops *q = NULL;
221 read_lock(&qdisc_mod_lock);
222 for (q = qdisc_base; q; q = q->next) {
223 if (rtattr_strcmp(kind, q->id) == 0) {
224 if (!try_module_get(q->owner))
229 read_unlock(&qdisc_mod_lock);
234 static struct qdisc_rate_table *qdisc_rtab_list;
236 struct qdisc_rate_table *qdisc_get_rtab(struct tc_ratespec *r, struct rtattr *tab)
238 struct qdisc_rate_table *rtab;
240 for (rtab = qdisc_rtab_list; rtab; rtab = rtab->next) {
241 if (memcmp(&rtab->rate, r, sizeof(struct tc_ratespec)) == 0) {
247 if (tab == NULL || r->rate == 0 || r->cell_log == 0 || RTA_PAYLOAD(tab) != 1024)
250 rtab = kmalloc(sizeof(*rtab), GFP_KERNEL);
254 memcpy(rtab->data, RTA_DATA(tab), 1024);
255 rtab->next = qdisc_rtab_list;
256 qdisc_rtab_list = rtab;
260 EXPORT_SYMBOL(qdisc_get_rtab);
262 void qdisc_put_rtab(struct qdisc_rate_table *tab)
264 struct qdisc_rate_table *rtab, **rtabp;
266 if (!tab || --tab->refcnt)
269 for (rtabp = &qdisc_rtab_list; (rtab=*rtabp) != NULL; rtabp = &rtab->next) {
277 EXPORT_SYMBOL(qdisc_put_rtab);
279 static enum hrtimer_restart qdisc_watchdog(struct hrtimer *timer)
281 struct qdisc_watchdog *wd = container_of(timer, struct qdisc_watchdog,
283 struct net_device *dev = wd->qdisc->dev;
285 wd->qdisc->flags &= ~TCQ_F_THROTTLED;
289 return HRTIMER_NORESTART;
292 void qdisc_watchdog_init(struct qdisc_watchdog *wd, struct Qdisc *qdisc)
294 hrtimer_init(&wd->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
295 wd->timer.function = qdisc_watchdog;
298 EXPORT_SYMBOL(qdisc_watchdog_init);
300 void qdisc_watchdog_schedule(struct qdisc_watchdog *wd, psched_time_t expires)
304 wd->qdisc->flags |= TCQ_F_THROTTLED;
305 time = ktime_set(0, 0);
306 time = ktime_add_ns(time, PSCHED_US2NS(expires));
307 hrtimer_start(&wd->timer, time, HRTIMER_MODE_ABS);
309 EXPORT_SYMBOL(qdisc_watchdog_schedule);
311 void qdisc_watchdog_cancel(struct qdisc_watchdog *wd)
313 hrtimer_cancel(&wd->timer);
314 wd->qdisc->flags &= ~TCQ_F_THROTTLED;
316 EXPORT_SYMBOL(qdisc_watchdog_cancel);
318 /* Allocate an unique handle from space managed by kernel */
320 static u32 qdisc_alloc_handle(struct net_device *dev)
323 static u32 autohandle = TC_H_MAKE(0x80000000U, 0);
326 autohandle += TC_H_MAKE(0x10000U, 0);
327 if (autohandle == TC_H_MAKE(TC_H_ROOT, 0))
328 autohandle = TC_H_MAKE(0x80000000U, 0);
329 } while (qdisc_lookup(dev, autohandle) && --i > 0);
331 return i>0 ? autohandle : 0;
334 /* Attach toplevel qdisc to device dev */
336 static struct Qdisc *
337 dev_graft_qdisc(struct net_device *dev, struct Qdisc *qdisc)
339 struct Qdisc *oqdisc;
341 if (dev->flags & IFF_UP)
344 qdisc_lock_tree(dev);
345 if (qdisc && qdisc->flags&TCQ_F_INGRESS) {
346 oqdisc = dev->qdisc_ingress;
347 /* Prune old scheduler */
348 if (oqdisc && atomic_read(&oqdisc->refcnt) <= 1) {
351 dev->qdisc_ingress = NULL;
353 dev->qdisc_ingress = qdisc;
358 oqdisc = dev->qdisc_sleeping;
360 /* Prune old scheduler */
361 if (oqdisc && atomic_read(&oqdisc->refcnt) <= 1)
364 /* ... and graft new one */
367 dev->qdisc_sleeping = qdisc;
368 dev->qdisc = &noop_qdisc;
371 qdisc_unlock_tree(dev);
373 if (dev->flags & IFF_UP)
379 void qdisc_tree_decrease_qlen(struct Qdisc *sch, unsigned int n)
381 const struct Qdisc_class_ops *cops;
387 while ((parentid = sch->parent)) {
388 sch = qdisc_lookup(sch->dev, TC_H_MAJ(parentid));
390 WARN_ON(parentid != TC_H_ROOT);
393 cops = sch->ops->cl_ops;
394 if (cops->qlen_notify) {
395 cl = cops->get(sch, parentid);
396 cops->qlen_notify(sch, cl);
402 EXPORT_SYMBOL(qdisc_tree_decrease_qlen);
404 /* Graft qdisc "new" to class "classid" of qdisc "parent" or
407 Old qdisc is not destroyed but returned in *old.
410 static int qdisc_graft(struct net_device *dev, struct Qdisc *parent,
412 struct Qdisc *new, struct Qdisc **old)
415 struct Qdisc *q = *old;
418 if (parent == NULL) {
419 if (q && q->flags&TCQ_F_INGRESS) {
420 *old = dev_graft_qdisc(dev, q);
422 *old = dev_graft_qdisc(dev, new);
425 const struct Qdisc_class_ops *cops = parent->ops->cl_ops;
430 unsigned long cl = cops->get(parent, classid);
432 err = cops->graft(parent, cl, new, old);
433 cops->put(parent, cl);
441 Allocate and initialize new qdisc.
443 Parameters are passed via opt.
446 static struct Qdisc *
447 qdisc_create(struct net_device *dev, u32 parent, u32 handle,
448 struct rtattr **tca, int *errp)
451 struct rtattr *kind = tca[TCA_KIND-1];
453 struct Qdisc_ops *ops;
455 ops = qdisc_lookup_ops(kind);
457 if (ops == NULL && kind != NULL) {
459 if (rtattr_strlcpy(name, kind, IFNAMSIZ) < IFNAMSIZ) {
460 /* We dropped the RTNL semaphore in order to
461 * perform the module load. So, even if we
462 * succeeded in loading the module we have to
463 * tell the caller to replay the request. We
464 * indicate this using -EAGAIN.
465 * We replay the request because the device may
466 * go away in the mean time.
469 request_module("sch_%s", name);
471 ops = qdisc_lookup_ops(kind);
473 /* We will try again qdisc_lookup_ops,
474 * so don't keep a reference.
476 module_put(ops->owner);
488 sch = qdisc_alloc(dev, ops);
494 sch->parent = parent;
496 if (handle == TC_H_INGRESS) {
497 sch->flags |= TCQ_F_INGRESS;
498 sch->stats_lock = &dev->ingress_lock;
499 handle = TC_H_MAKE(TC_H_INGRESS, 0);
501 sch->stats_lock = &dev->queue_lock;
503 handle = qdisc_alloc_handle(dev);
510 sch->handle = handle;
512 if (!ops->init || (err = ops->init(sch, tca[TCA_OPTIONS-1])) == 0) {
513 if (tca[TCA_RATE-1]) {
514 err = gen_new_estimator(&sch->bstats, &sch->rate_est,
519 * Any broken qdiscs that would require
520 * a ops->reset() here? The qdisc was never
521 * in action so it shouldn't be necessary.
528 qdisc_lock_tree(dev);
529 list_add_tail(&sch->list, &dev->qdisc_list);
530 qdisc_unlock_tree(dev);
536 kfree((char *) sch - sch->padded);
538 module_put(ops->owner);
544 static int qdisc_change(struct Qdisc *sch, struct rtattr **tca)
546 if (tca[TCA_OPTIONS-1]) {
549 if (sch->ops->change == NULL)
551 err = sch->ops->change(sch, tca[TCA_OPTIONS-1]);
556 gen_replace_estimator(&sch->bstats, &sch->rate_est,
557 sch->stats_lock, tca[TCA_RATE-1]);
561 struct check_loop_arg
563 struct qdisc_walker w;
568 static int check_loop_fn(struct Qdisc *q, unsigned long cl, struct qdisc_walker *w);
570 static int check_loop(struct Qdisc *q, struct Qdisc *p, int depth)
572 struct check_loop_arg arg;
574 if (q->ops->cl_ops == NULL)
577 arg.w.stop = arg.w.skip = arg.w.count = 0;
578 arg.w.fn = check_loop_fn;
581 q->ops->cl_ops->walk(q, &arg.w);
582 return arg.w.stop ? -ELOOP : 0;
586 check_loop_fn(struct Qdisc *q, unsigned long cl, struct qdisc_walker *w)
589 const struct Qdisc_class_ops *cops = q->ops->cl_ops;
590 struct check_loop_arg *arg = (struct check_loop_arg *)w;
592 leaf = cops->leaf(q, cl);
594 if (leaf == arg->p || arg->depth > 7)
596 return check_loop(leaf, arg->p, arg->depth + 1);
605 static int tc_get_qdisc(struct sk_buff *skb, struct nlmsghdr *n, void *arg)
607 struct net *net = skb->sk->sk_net;
608 struct tcmsg *tcm = NLMSG_DATA(n);
609 struct rtattr **tca = arg;
610 struct net_device *dev;
611 u32 clid = tcm->tcm_parent;
612 struct Qdisc *q = NULL;
613 struct Qdisc *p = NULL;
616 if (net != &init_net)
619 if ((dev = __dev_get_by_index(&init_net, tcm->tcm_ifindex)) == NULL)
623 if (clid != TC_H_ROOT) {
624 if (TC_H_MAJ(clid) != TC_H_MAJ(TC_H_INGRESS)) {
625 if ((p = qdisc_lookup(dev, TC_H_MAJ(clid))) == NULL)
627 q = qdisc_leaf(p, clid);
628 } else { /* ingress */
629 q = dev->qdisc_ingress;
632 q = dev->qdisc_sleeping;
637 if (tcm->tcm_handle && q->handle != tcm->tcm_handle)
640 if ((q = qdisc_lookup(dev, tcm->tcm_handle)) == NULL)
644 if (tca[TCA_KIND-1] && rtattr_strcmp(tca[TCA_KIND-1], q->ops->id))
647 if (n->nlmsg_type == RTM_DELQDISC) {
652 if ((err = qdisc_graft(dev, p, clid, NULL, &q)) != 0)
655 qdisc_notify(skb, n, clid, q, NULL);
656 qdisc_lock_tree(dev);
658 qdisc_unlock_tree(dev);
661 qdisc_notify(skb, n, clid, NULL, q);
670 static int tc_modify_qdisc(struct sk_buff *skb, struct nlmsghdr *n, void *arg)
672 struct net *net = skb->sk->sk_net;
675 struct net_device *dev;
680 if (net != &init_net)
684 /* Reinit, just in case something touches this. */
687 clid = tcm->tcm_parent;
690 if ((dev = __dev_get_by_index(&init_net, tcm->tcm_ifindex)) == NULL)
694 if (clid != TC_H_ROOT) {
695 if (clid != TC_H_INGRESS) {
696 if ((p = qdisc_lookup(dev, TC_H_MAJ(clid))) == NULL)
698 q = qdisc_leaf(p, clid);
699 } else { /*ingress */
700 q = dev->qdisc_ingress;
703 q = dev->qdisc_sleeping;
706 /* It may be default qdisc, ignore it */
707 if (q && q->handle == 0)
710 if (!q || !tcm->tcm_handle || q->handle != tcm->tcm_handle) {
711 if (tcm->tcm_handle) {
712 if (q && !(n->nlmsg_flags&NLM_F_REPLACE))
714 if (TC_H_MIN(tcm->tcm_handle))
716 if ((q = qdisc_lookup(dev, tcm->tcm_handle)) == NULL)
718 if (n->nlmsg_flags&NLM_F_EXCL)
720 if (tca[TCA_KIND-1] && rtattr_strcmp(tca[TCA_KIND-1], q->ops->id))
723 (p && check_loop(q, p, 0)))
725 atomic_inc(&q->refcnt);
731 /* This magic test requires explanation.
733 * We know, that some child q is already
734 * attached to this parent and have choice:
735 * either to change it or to create/graft new one.
737 * 1. We are allowed to create/graft only
738 * if CREATE and REPLACE flags are set.
740 * 2. If EXCL is set, requestor wanted to say,
741 * that qdisc tcm_handle is not expected
742 * to exist, so that we choose create/graft too.
744 * 3. The last case is when no flags are set.
745 * Alas, it is sort of hole in API, we
746 * cannot decide what to do unambiguously.
747 * For now we select create/graft, if
748 * user gave KIND, which does not match existing.
750 if ((n->nlmsg_flags&NLM_F_CREATE) &&
751 (n->nlmsg_flags&NLM_F_REPLACE) &&
752 ((n->nlmsg_flags&NLM_F_EXCL) ||
754 rtattr_strcmp(tca[TCA_KIND-1], q->ops->id))))
759 if (!tcm->tcm_handle)
761 q = qdisc_lookup(dev, tcm->tcm_handle);
764 /* Change qdisc parameters */
767 if (n->nlmsg_flags&NLM_F_EXCL)
769 if (tca[TCA_KIND-1] && rtattr_strcmp(tca[TCA_KIND-1], q->ops->id))
771 err = qdisc_change(q, tca);
773 qdisc_notify(skb, n, clid, NULL, q);
777 if (!(n->nlmsg_flags&NLM_F_CREATE))
779 if (clid == TC_H_INGRESS)
780 q = qdisc_create(dev, tcm->tcm_parent, tcm->tcm_parent,
783 q = qdisc_create(dev, tcm->tcm_parent, tcm->tcm_handle,
793 struct Qdisc *old_q = NULL;
794 err = qdisc_graft(dev, p, clid, q, &old_q);
797 qdisc_lock_tree(dev);
799 qdisc_unlock_tree(dev);
803 qdisc_notify(skb, n, clid, old_q, q);
805 qdisc_lock_tree(dev);
806 qdisc_destroy(old_q);
807 qdisc_unlock_tree(dev);
813 static int tc_fill_qdisc(struct sk_buff *skb, struct Qdisc *q, u32 clid,
814 u32 pid, u32 seq, u16 flags, int event)
817 struct nlmsghdr *nlh;
818 unsigned char *b = skb_tail_pointer(skb);
821 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*tcm), flags);
822 tcm = NLMSG_DATA(nlh);
823 tcm->tcm_family = AF_UNSPEC;
826 tcm->tcm_ifindex = q->dev->ifindex;
827 tcm->tcm_parent = clid;
828 tcm->tcm_handle = q->handle;
829 tcm->tcm_info = atomic_read(&q->refcnt);
830 RTA_PUT(skb, TCA_KIND, IFNAMSIZ, q->ops->id);
831 if (q->ops->dump && q->ops->dump(q, skb) < 0)
833 q->qstats.qlen = q->q.qlen;
835 if (gnet_stats_start_copy_compat(skb, TCA_STATS2, TCA_STATS,
836 TCA_XSTATS, q->stats_lock, &d) < 0)
839 if (q->ops->dump_stats && q->ops->dump_stats(q, &d) < 0)
842 if (gnet_stats_copy_basic(&d, &q->bstats) < 0 ||
843 gnet_stats_copy_rate_est(&d, &q->rate_est) < 0 ||
844 gnet_stats_copy_queue(&d, &q->qstats) < 0)
847 if (gnet_stats_finish_copy(&d) < 0)
850 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
859 static int qdisc_notify(struct sk_buff *oskb, struct nlmsghdr *n,
860 u32 clid, struct Qdisc *old, struct Qdisc *new)
863 u32 pid = oskb ? NETLINK_CB(oskb).pid : 0;
865 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
869 if (old && old->handle) {
870 if (tc_fill_qdisc(skb, old, clid, pid, n->nlmsg_seq, 0, RTM_DELQDISC) < 0)
874 if (tc_fill_qdisc(skb, new, clid, pid, n->nlmsg_seq, old ? NLM_F_REPLACE : 0, RTM_NEWQDISC) < 0)
879 return rtnetlink_send(skb, &init_net, pid, RTNLGRP_TC, n->nlmsg_flags&NLM_F_ECHO);
886 static int tc_dump_qdisc(struct sk_buff *skb, struct netlink_callback *cb)
888 struct net *net = skb->sk->sk_net;
891 struct net_device *dev;
894 if (net != &init_net)
898 s_q_idx = q_idx = cb->args[1];
899 read_lock(&dev_base_lock);
901 for_each_netdev(&init_net, dev) {
907 list_for_each_entry(q, &dev->qdisc_list, list) {
908 if (q_idx < s_q_idx) {
912 if (tc_fill_qdisc(skb, q, q->parent, NETLINK_CB(cb->skb).pid,
913 cb->nlh->nlmsg_seq, NLM_F_MULTI, RTM_NEWQDISC) <= 0)
922 read_unlock(&dev_base_lock);
932 /************************************************
933 * Traffic classes manipulation. *
934 ************************************************/
938 static int tc_ctl_tclass(struct sk_buff *skb, struct nlmsghdr *n, void *arg)
940 struct net *net = skb->sk->sk_net;
941 struct tcmsg *tcm = NLMSG_DATA(n);
942 struct rtattr **tca = arg;
943 struct net_device *dev;
944 struct Qdisc *q = NULL;
945 const struct Qdisc_class_ops *cops;
946 unsigned long cl = 0;
947 unsigned long new_cl;
948 u32 pid = tcm->tcm_parent;
949 u32 clid = tcm->tcm_handle;
950 u32 qid = TC_H_MAJ(clid);
953 if (net != &init_net)
956 if ((dev = __dev_get_by_index(&init_net, tcm->tcm_ifindex)) == NULL)
960 parent == TC_H_UNSPEC - unspecified parent.
961 parent == TC_H_ROOT - class is root, which has no parent.
962 parent == X:0 - parent is root class.
963 parent == X:Y - parent is a node in hierarchy.
964 parent == 0:Y - parent is X:Y, where X:0 is qdisc.
966 handle == 0:0 - generate handle from kernel pool.
967 handle == 0:Y - class is X:Y, where X:0 is qdisc.
968 handle == X:Y - clear.
969 handle == X:0 - root class.
972 /* Step 1. Determine qdisc handle X:0 */
974 if (pid != TC_H_ROOT) {
975 u32 qid1 = TC_H_MAJ(pid);
978 /* If both majors are known, they must be identical. */
984 qid = dev->qdisc_sleeping->handle;
986 /* Now qid is genuine qdisc handle consistent
987 both with parent and child.
989 TC_H_MAJ(pid) still may be unspecified, complete it now.
992 pid = TC_H_MAKE(qid, pid);
995 qid = dev->qdisc_sleeping->handle;
998 /* OK. Locate qdisc */
999 if ((q = qdisc_lookup(dev, qid)) == NULL)
1002 /* An check that it supports classes */
1003 cops = q->ops->cl_ops;
1007 /* Now try to get class */
1009 if (pid == TC_H_ROOT)
1012 clid = TC_H_MAKE(qid, clid);
1015 cl = cops->get(q, clid);
1019 if (n->nlmsg_type != RTM_NEWTCLASS || !(n->nlmsg_flags&NLM_F_CREATE))
1022 switch (n->nlmsg_type) {
1025 if (n->nlmsg_flags&NLM_F_EXCL)
1029 err = cops->delete(q, cl);
1031 tclass_notify(skb, n, q, cl, RTM_DELTCLASS);
1034 err = tclass_notify(skb, n, q, cl, RTM_NEWTCLASS);
1043 err = cops->change(q, clid, pid, tca, &new_cl);
1045 tclass_notify(skb, n, q, new_cl, RTM_NEWTCLASS);
1055 static int tc_fill_tclass(struct sk_buff *skb, struct Qdisc *q,
1057 u32 pid, u32 seq, u16 flags, int event)
1060 struct nlmsghdr *nlh;
1061 unsigned char *b = skb_tail_pointer(skb);
1063 const struct Qdisc_class_ops *cl_ops = q->ops->cl_ops;
1065 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*tcm), flags);
1066 tcm = NLMSG_DATA(nlh);
1067 tcm->tcm_family = AF_UNSPEC;
1068 tcm->tcm_ifindex = q->dev->ifindex;
1069 tcm->tcm_parent = q->handle;
1070 tcm->tcm_handle = q->handle;
1072 RTA_PUT(skb, TCA_KIND, IFNAMSIZ, q->ops->id);
1073 if (cl_ops->dump && cl_ops->dump(q, cl, skb, tcm) < 0)
1074 goto rtattr_failure;
1076 if (gnet_stats_start_copy_compat(skb, TCA_STATS2, TCA_STATS,
1077 TCA_XSTATS, q->stats_lock, &d) < 0)
1078 goto rtattr_failure;
1080 if (cl_ops->dump_stats && cl_ops->dump_stats(q, cl, &d) < 0)
1081 goto rtattr_failure;
1083 if (gnet_stats_finish_copy(&d) < 0)
1084 goto rtattr_failure;
1086 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1095 static int tclass_notify(struct sk_buff *oskb, struct nlmsghdr *n,
1096 struct Qdisc *q, unsigned long cl, int event)
1098 struct sk_buff *skb;
1099 u32 pid = oskb ? NETLINK_CB(oskb).pid : 0;
1101 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1105 if (tc_fill_tclass(skb, q, cl, pid, n->nlmsg_seq, 0, event) < 0) {
1110 return rtnetlink_send(skb, &init_net, pid, RTNLGRP_TC, n->nlmsg_flags&NLM_F_ECHO);
1113 struct qdisc_dump_args
1115 struct qdisc_walker w;
1116 struct sk_buff *skb;
1117 struct netlink_callback *cb;
1120 static int qdisc_class_dump(struct Qdisc *q, unsigned long cl, struct qdisc_walker *arg)
1122 struct qdisc_dump_args *a = (struct qdisc_dump_args *)arg;
1124 return tc_fill_tclass(a->skb, q, cl, NETLINK_CB(a->cb->skb).pid,
1125 a->cb->nlh->nlmsg_seq, NLM_F_MULTI, RTM_NEWTCLASS);
1128 static int tc_dump_tclass(struct sk_buff *skb, struct netlink_callback *cb)
1130 struct net *net = skb->sk->sk_net;
1133 struct net_device *dev;
1135 struct tcmsg *tcm = (struct tcmsg*)NLMSG_DATA(cb->nlh);
1136 struct qdisc_dump_args arg;
1138 if (net != &init_net)
1141 if (cb->nlh->nlmsg_len < NLMSG_LENGTH(sizeof(*tcm)))
1143 if ((dev = dev_get_by_index(&init_net, tcm->tcm_ifindex)) == NULL)
1149 list_for_each_entry(q, &dev->qdisc_list, list) {
1150 if (t < s_t || !q->ops->cl_ops ||
1152 TC_H_MAJ(tcm->tcm_parent) != q->handle)) {
1157 memset(&cb->args[1], 0, sizeof(cb->args)-sizeof(cb->args[0]));
1158 arg.w.fn = qdisc_class_dump;
1162 arg.w.skip = cb->args[1];
1164 q->ops->cl_ops->walk(q, &arg.w);
1165 cb->args[1] = arg.w.count;
1177 /* Main classifier routine: scans classifier chain attached
1178 to this qdisc, (optionally) tests for protocol and asks
1179 specific classifiers.
1181 int tc_classify_compat(struct sk_buff *skb, struct tcf_proto *tp,
1182 struct tcf_result *res)
1184 __be16 protocol = skb->protocol;
1187 for (; tp; tp = tp->next) {
1188 if ((tp->protocol == protocol ||
1189 tp->protocol == htons(ETH_P_ALL)) &&
1190 (err = tp->classify(skb, tp, res)) >= 0) {
1191 #ifdef CONFIG_NET_CLS_ACT
1192 if (err != TC_ACT_RECLASSIFY && skb->tc_verd)
1193 skb->tc_verd = SET_TC_VERD(skb->tc_verd, 0);
1200 EXPORT_SYMBOL(tc_classify_compat);
1202 int tc_classify(struct sk_buff *skb, struct tcf_proto *tp,
1203 struct tcf_result *res)
1207 #ifdef CONFIG_NET_CLS_ACT
1208 struct tcf_proto *otp = tp;
1211 protocol = skb->protocol;
1213 err = tc_classify_compat(skb, tp, res);
1214 #ifdef CONFIG_NET_CLS_ACT
1215 if (err == TC_ACT_RECLASSIFY) {
1216 u32 verd = G_TC_VERD(skb->tc_verd);
1219 if (verd++ >= MAX_REC_LOOP) {
1220 printk("rule prio %u protocol %02x reclassify loop, "
1222 tp->prio&0xffff, ntohs(tp->protocol));
1225 skb->tc_verd = SET_TC_VERD(skb->tc_verd, verd);
1231 EXPORT_SYMBOL(tc_classify);
1233 void tcf_destroy(struct tcf_proto *tp)
1235 tp->ops->destroy(tp);
1236 module_put(tp->ops->owner);
1240 void tcf_destroy_chain(struct tcf_proto *fl)
1242 struct tcf_proto *tp;
1244 while ((tp = fl) != NULL) {
1249 EXPORT_SYMBOL(tcf_destroy_chain);
1251 #ifdef CONFIG_PROC_FS
1252 static int psched_show(struct seq_file *seq, void *v)
1256 hrtimer_get_res(CLOCK_MONOTONIC, &ts);
1257 seq_printf(seq, "%08x %08x %08x %08x\n",
1258 (u32)NSEC_PER_USEC, (u32)PSCHED_US2NS(1),
1260 (u32)NSEC_PER_SEC/(u32)ktime_to_ns(timespec_to_ktime(ts)));
1265 static int psched_open(struct inode *inode, struct file *file)
1267 return single_open(file, psched_show, PDE(inode)->data);
1270 static const struct file_operations psched_fops = {
1271 .owner = THIS_MODULE,
1272 .open = psched_open,
1274 .llseek = seq_lseek,
1275 .release = single_release,
1279 static int __init pktsched_init(void)
1281 register_qdisc(&pfifo_qdisc_ops);
1282 register_qdisc(&bfifo_qdisc_ops);
1283 proc_net_fops_create(&init_net, "psched", 0, &psched_fops);
1285 rtnl_register(PF_UNSPEC, RTM_NEWQDISC, tc_modify_qdisc, NULL);
1286 rtnl_register(PF_UNSPEC, RTM_DELQDISC, tc_get_qdisc, NULL);
1287 rtnl_register(PF_UNSPEC, RTM_GETQDISC, tc_get_qdisc, tc_dump_qdisc);
1288 rtnl_register(PF_UNSPEC, RTM_NEWTCLASS, tc_ctl_tclass, NULL);
1289 rtnl_register(PF_UNSPEC, RTM_DELTCLASS, tc_ctl_tclass, NULL);
1290 rtnl_register(PF_UNSPEC, RTM_GETTCLASS, tc_ctl_tclass, tc_dump_tclass);
1295 subsys_initcall(pktsched_init);