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>
40 #include <net/pkt_sched.h>
42 #include <asm/processor.h>
43 #include <asm/uaccess.h>
44 #include <asm/system.h>
46 static int qdisc_notify(struct sk_buff *oskb, struct nlmsghdr *n, u32 clid,
47 struct Qdisc *old, struct Qdisc *new);
48 static int tclass_notify(struct sk_buff *oskb, struct nlmsghdr *n,
49 struct Qdisc *q, unsigned long cl, int event);
56 This file consists of two interrelated parts:
58 1. queueing disciplines manager frontend.
59 2. traffic classes manager frontend.
61 Generally, queueing discipline ("qdisc") is a black box,
62 which is able to enqueue packets and to dequeue them (when
63 device is ready to send something) in order and at times
64 determined by algorithm hidden in it.
66 qdisc's are divided to two categories:
67 - "queues", which have no internal structure visible from outside.
68 - "schedulers", which split all the packets to "traffic classes",
69 using "packet classifiers" (look at cls_api.c)
71 In turn, classes may have child qdiscs (as rule, queues)
72 attached to them etc. etc. etc.
74 The goal of the routines in this file is to translate
75 information supplied by user in the form of handles
76 to more intelligible for kernel form, to make some sanity
77 checks and part of work, which is common to all qdiscs
78 and to provide rtnetlink notifications.
80 All real intelligent work is done inside qdisc modules.
84 Every discipline has two major routines: enqueue and dequeue.
88 dequeue usually returns a skb to send. It is allowed to return NULL,
89 but it does not mean that queue is empty, it just means that
90 discipline does not want to send anything this time.
91 Queue is really empty if q->q.qlen == 0.
92 For complicated disciplines with multiple queues q->q is not
93 real packet queue, but however q->q.qlen must be valid.
97 enqueue returns 0, if packet was enqueued successfully.
98 If packet (this one or another one) was dropped, it returns
100 NET_XMIT_DROP - this packet dropped
101 Expected action: do not backoff, but wait until queue will clear.
102 NET_XMIT_CN - probably this packet enqueued, but another one dropped.
103 Expected action: backoff or ignore
104 NET_XMIT_POLICED - dropped by police.
105 Expected action: backoff or error to real-time apps.
111 requeues once dequeued packet. It is used for non-standard or
112 just buggy devices, which can defer output even if dev->tbusy=0.
116 returns qdisc to initial state: purge all buffers, clear all
117 timers, counters (except for statistics) etc.
121 initializes newly created qdisc.
125 destroys resources allocated by init and during lifetime of qdisc.
129 changes qdisc parameters.
132 /* Protects list of registered TC modules. It is pure SMP lock. */
133 static DEFINE_RWLOCK(qdisc_mod_lock);
136 /************************************************
137 * Queueing disciplines manipulation. *
138 ************************************************/
141 /* The list of all installed queueing disciplines. */
143 static struct Qdisc_ops *qdisc_base;
145 /* Register/uregister queueing discipline */
147 int register_qdisc(struct Qdisc_ops *qops)
149 struct Qdisc_ops *q, **qp;
152 write_lock(&qdisc_mod_lock);
153 for (qp = &qdisc_base; (q = *qp) != NULL; qp = &q->next)
154 if (!strcmp(qops->id, q->id))
157 if (qops->enqueue == NULL)
158 qops->enqueue = noop_qdisc_ops.enqueue;
159 if (qops->requeue == NULL)
160 qops->requeue = noop_qdisc_ops.requeue;
161 if (qops->dequeue == NULL)
162 qops->dequeue = noop_qdisc_ops.dequeue;
168 write_unlock(&qdisc_mod_lock);
172 int unregister_qdisc(struct Qdisc_ops *qops)
174 struct Qdisc_ops *q, **qp;
177 write_lock(&qdisc_mod_lock);
178 for (qp = &qdisc_base; (q=*qp)!=NULL; qp = &q->next)
186 write_unlock(&qdisc_mod_lock);
190 /* We know handle. Find qdisc among all qdisc's attached to device
191 (root qdisc, all its children, children of children etc.)
194 static struct Qdisc *__qdisc_lookup(struct net_device *dev, u32 handle)
198 list_for_each_entry(q, &dev->qdisc_list, list) {
199 if (q->handle == handle)
205 struct Qdisc *qdisc_lookup(struct net_device *dev, u32 handle)
209 read_lock(&qdisc_tree_lock);
210 q = __qdisc_lookup(dev, handle);
211 read_unlock(&qdisc_tree_lock);
215 static struct Qdisc *qdisc_leaf(struct Qdisc *p, u32 classid)
219 struct Qdisc_class_ops *cops = p->ops->cl_ops;
223 cl = cops->get(p, classid);
227 leaf = cops->leaf(p, cl);
232 /* Find queueing discipline by name */
234 static struct Qdisc_ops *qdisc_lookup_ops(struct rtattr *kind)
236 struct Qdisc_ops *q = NULL;
239 read_lock(&qdisc_mod_lock);
240 for (q = qdisc_base; q; q = q->next) {
241 if (rtattr_strcmp(kind, q->id) == 0) {
242 if (!try_module_get(q->owner))
247 read_unlock(&qdisc_mod_lock);
252 static struct qdisc_rate_table *qdisc_rtab_list;
254 struct qdisc_rate_table *qdisc_get_rtab(struct tc_ratespec *r, struct rtattr *tab)
256 struct qdisc_rate_table *rtab;
258 for (rtab = qdisc_rtab_list; rtab; rtab = rtab->next) {
259 if (memcmp(&rtab->rate, r, sizeof(struct tc_ratespec)) == 0) {
265 if (tab == NULL || r->rate == 0 || r->cell_log == 0 || RTA_PAYLOAD(tab) != 1024)
268 rtab = kmalloc(sizeof(*rtab), GFP_KERNEL);
272 memcpy(rtab->data, RTA_DATA(tab), 1024);
273 rtab->next = qdisc_rtab_list;
274 qdisc_rtab_list = rtab;
279 void qdisc_put_rtab(struct qdisc_rate_table *tab)
281 struct qdisc_rate_table *rtab, **rtabp;
283 if (!tab || --tab->refcnt)
286 for (rtabp = &qdisc_rtab_list; (rtab=*rtabp) != NULL; rtabp = &rtab->next) {
295 static enum hrtimer_restart qdisc_watchdog(struct hrtimer *timer)
297 struct qdisc_watchdog *wd = container_of(timer, struct qdisc_watchdog,
300 wd->qdisc->flags &= ~TCQ_F_THROTTLED;
301 netif_schedule(wd->qdisc->dev);
302 return HRTIMER_NORESTART;
305 void qdisc_watchdog_init(struct qdisc_watchdog *wd, struct Qdisc *qdisc)
307 hrtimer_init(&wd->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
308 wd->timer.function = qdisc_watchdog;
311 EXPORT_SYMBOL(qdisc_watchdog_init);
313 void qdisc_watchdog_schedule(struct qdisc_watchdog *wd, psched_time_t expires)
317 wd->qdisc->flags |= TCQ_F_THROTTLED;
318 time = ktime_set(0, 0);
319 time = ktime_add_ns(time, PSCHED_US2NS(expires));
320 hrtimer_start(&wd->timer, time, HRTIMER_MODE_ABS);
322 EXPORT_SYMBOL(qdisc_watchdog_schedule);
324 void qdisc_watchdog_cancel(struct qdisc_watchdog *wd)
326 hrtimer_cancel(&wd->timer);
327 wd->qdisc->flags &= ~TCQ_F_THROTTLED;
329 EXPORT_SYMBOL(qdisc_watchdog_cancel);
331 /* Allocate an unique handle from space managed by kernel */
333 static u32 qdisc_alloc_handle(struct net_device *dev)
336 static u32 autohandle = TC_H_MAKE(0x80000000U, 0);
339 autohandle += TC_H_MAKE(0x10000U, 0);
340 if (autohandle == TC_H_MAKE(TC_H_ROOT, 0))
341 autohandle = TC_H_MAKE(0x80000000U, 0);
342 } while (qdisc_lookup(dev, autohandle) && --i > 0);
344 return i>0 ? autohandle : 0;
347 /* Attach toplevel qdisc to device dev */
349 static struct Qdisc *
350 dev_graft_qdisc(struct net_device *dev, struct Qdisc *qdisc)
352 struct Qdisc *oqdisc;
354 if (dev->flags & IFF_UP)
357 qdisc_lock_tree(dev);
358 if (qdisc && qdisc->flags&TCQ_F_INGRESS) {
359 oqdisc = dev->qdisc_ingress;
360 /* Prune old scheduler */
361 if (oqdisc && atomic_read(&oqdisc->refcnt) <= 1) {
364 dev->qdisc_ingress = NULL;
366 dev->qdisc_ingress = qdisc;
371 oqdisc = dev->qdisc_sleeping;
373 /* Prune old scheduler */
374 if (oqdisc && atomic_read(&oqdisc->refcnt) <= 1)
377 /* ... and graft new one */
380 dev->qdisc_sleeping = qdisc;
381 dev->qdisc = &noop_qdisc;
384 qdisc_unlock_tree(dev);
386 if (dev->flags & IFF_UP)
392 void qdisc_tree_decrease_qlen(struct Qdisc *sch, unsigned int n)
394 struct Qdisc_class_ops *cops;
400 while ((parentid = sch->parent)) {
401 sch = __qdisc_lookup(sch->dev, TC_H_MAJ(parentid));
402 cops = sch->ops->cl_ops;
403 if (cops->qlen_notify) {
404 cl = cops->get(sch, parentid);
405 cops->qlen_notify(sch, cl);
411 EXPORT_SYMBOL(qdisc_tree_decrease_qlen);
413 /* Graft qdisc "new" to class "classid" of qdisc "parent" or
416 Old qdisc is not destroyed but returned in *old.
419 static int qdisc_graft(struct net_device *dev, struct Qdisc *parent,
421 struct Qdisc *new, struct Qdisc **old)
424 struct Qdisc *q = *old;
427 if (parent == NULL) {
428 if (q && q->flags&TCQ_F_INGRESS) {
429 *old = dev_graft_qdisc(dev, q);
431 *old = dev_graft_qdisc(dev, new);
434 struct Qdisc_class_ops *cops = parent->ops->cl_ops;
439 unsigned long cl = cops->get(parent, classid);
441 err = cops->graft(parent, cl, new, old);
443 new->parent = classid;
444 cops->put(parent, cl);
452 Allocate and initialize new qdisc.
454 Parameters are passed via opt.
457 static struct Qdisc *
458 qdisc_create(struct net_device *dev, u32 handle, struct rtattr **tca, int *errp)
461 struct rtattr *kind = tca[TCA_KIND-1];
463 struct Qdisc_ops *ops;
465 ops = qdisc_lookup_ops(kind);
467 if (ops == NULL && kind != NULL) {
469 if (rtattr_strlcpy(name, kind, IFNAMSIZ) < IFNAMSIZ) {
470 /* We dropped the RTNL semaphore in order to
471 * perform the module load. So, even if we
472 * succeeded in loading the module we have to
473 * tell the caller to replay the request. We
474 * indicate this using -EAGAIN.
475 * We replay the request because the device may
476 * go away in the mean time.
479 request_module("sch_%s", name);
481 ops = qdisc_lookup_ops(kind);
483 /* We will try again qdisc_lookup_ops,
484 * so don't keep a reference.
486 module_put(ops->owner);
498 sch = qdisc_alloc(dev, ops);
504 if (handle == TC_H_INGRESS) {
505 sch->flags |= TCQ_F_INGRESS;
506 handle = TC_H_MAKE(TC_H_INGRESS, 0);
507 } else if (handle == 0) {
508 handle = qdisc_alloc_handle(dev);
514 sch->handle = handle;
516 if (!ops->init || (err = ops->init(sch, tca[TCA_OPTIONS-1])) == 0) {
517 #ifdef CONFIG_NET_ESTIMATOR
518 if (tca[TCA_RATE-1]) {
519 err = gen_new_estimator(&sch->bstats, &sch->rate_est,
524 * Any broken qdiscs that would require
525 * a ops->reset() here? The qdisc was never
526 * in action so it shouldn't be necessary.
534 qdisc_lock_tree(dev);
535 list_add_tail(&sch->list, &dev->qdisc_list);
536 qdisc_unlock_tree(dev);
542 kfree((char *) sch - sch->padded);
544 module_put(ops->owner);
550 static int qdisc_change(struct Qdisc *sch, struct rtattr **tca)
552 if (tca[TCA_OPTIONS-1]) {
555 if (sch->ops->change == NULL)
557 err = sch->ops->change(sch, tca[TCA_OPTIONS-1]);
561 #ifdef CONFIG_NET_ESTIMATOR
563 gen_replace_estimator(&sch->bstats, &sch->rate_est,
564 sch->stats_lock, tca[TCA_RATE-1]);
569 struct check_loop_arg
571 struct qdisc_walker w;
576 static int check_loop_fn(struct Qdisc *q, unsigned long cl, struct qdisc_walker *w);
578 static int check_loop(struct Qdisc *q, struct Qdisc *p, int depth)
580 struct check_loop_arg arg;
582 if (q->ops->cl_ops == NULL)
585 arg.w.stop = arg.w.skip = arg.w.count = 0;
586 arg.w.fn = check_loop_fn;
589 q->ops->cl_ops->walk(q, &arg.w);
590 return arg.w.stop ? -ELOOP : 0;
594 check_loop_fn(struct Qdisc *q, unsigned long cl, struct qdisc_walker *w)
597 struct Qdisc_class_ops *cops = q->ops->cl_ops;
598 struct check_loop_arg *arg = (struct check_loop_arg *)w;
600 leaf = cops->leaf(q, cl);
602 if (leaf == arg->p || arg->depth > 7)
604 return check_loop(leaf, arg->p, arg->depth + 1);
613 static int tc_get_qdisc(struct sk_buff *skb, struct nlmsghdr *n, void *arg)
615 struct tcmsg *tcm = NLMSG_DATA(n);
616 struct rtattr **tca = arg;
617 struct net_device *dev;
618 u32 clid = tcm->tcm_parent;
619 struct Qdisc *q = NULL;
620 struct Qdisc *p = NULL;
623 if ((dev = __dev_get_by_index(tcm->tcm_ifindex)) == NULL)
627 if (clid != TC_H_ROOT) {
628 if (TC_H_MAJ(clid) != TC_H_MAJ(TC_H_INGRESS)) {
629 if ((p = qdisc_lookup(dev, TC_H_MAJ(clid))) == NULL)
631 q = qdisc_leaf(p, clid);
632 } else { /* ingress */
633 q = dev->qdisc_ingress;
636 q = dev->qdisc_sleeping;
641 if (tcm->tcm_handle && q->handle != tcm->tcm_handle)
644 if ((q = qdisc_lookup(dev, tcm->tcm_handle)) == NULL)
648 if (tca[TCA_KIND-1] && rtattr_strcmp(tca[TCA_KIND-1], q->ops->id))
651 if (n->nlmsg_type == RTM_DELQDISC) {
656 if ((err = qdisc_graft(dev, p, clid, NULL, &q)) != 0)
659 qdisc_notify(skb, n, clid, q, NULL);
660 spin_lock_bh(&dev->queue_lock);
662 spin_unlock_bh(&dev->queue_lock);
665 qdisc_notify(skb, n, clid, NULL, q);
674 static int tc_modify_qdisc(struct sk_buff *skb, struct nlmsghdr *n, void *arg)
678 struct net_device *dev;
684 /* Reinit, just in case something touches this. */
687 clid = tcm->tcm_parent;
690 if ((dev = __dev_get_by_index(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, tca, &err);
782 q = qdisc_create(dev, tcm->tcm_handle, tca, &err);
791 struct Qdisc *old_q = NULL;
792 err = qdisc_graft(dev, p, clid, q, &old_q);
795 spin_lock_bh(&dev->queue_lock);
797 spin_unlock_bh(&dev->queue_lock);
801 qdisc_notify(skb, n, clid, old_q, q);
803 spin_lock_bh(&dev->queue_lock);
804 qdisc_destroy(old_q);
805 spin_unlock_bh(&dev->queue_lock);
811 static int tc_fill_qdisc(struct sk_buff *skb, struct Qdisc *q, u32 clid,
812 u32 pid, u32 seq, u16 flags, int event)
815 struct nlmsghdr *nlh;
816 unsigned char *b = skb->tail;
819 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*tcm), flags);
820 tcm = NLMSG_DATA(nlh);
821 tcm->tcm_family = AF_UNSPEC;
824 tcm->tcm_ifindex = q->dev->ifindex;
825 tcm->tcm_parent = clid;
826 tcm->tcm_handle = q->handle;
827 tcm->tcm_info = atomic_read(&q->refcnt);
828 RTA_PUT(skb, TCA_KIND, IFNAMSIZ, q->ops->id);
829 if (q->ops->dump && q->ops->dump(q, skb) < 0)
831 q->qstats.qlen = q->q.qlen;
833 if (gnet_stats_start_copy_compat(skb, TCA_STATS2, TCA_STATS,
834 TCA_XSTATS, q->stats_lock, &d) < 0)
837 if (q->ops->dump_stats && q->ops->dump_stats(q, &d) < 0)
840 if (gnet_stats_copy_basic(&d, &q->bstats) < 0 ||
841 #ifdef CONFIG_NET_ESTIMATOR
842 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 - b;
855 skb_trim(skb, b - skb->data);
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, pid, RTNLGRP_TC, n->nlmsg_flags&NLM_F_ECHO);
886 static int tc_dump_qdisc(struct sk_buff *skb, struct netlink_callback *cb)
890 struct net_device *dev;
894 s_q_idx = q_idx = cb->args[1];
895 read_lock(&dev_base_lock);
896 for (dev=dev_base, idx=0; dev; dev = dev->next, idx++) {
901 read_lock(&qdisc_tree_lock);
903 list_for_each_entry(q, &dev->qdisc_list, list) {
904 if (q_idx < s_q_idx) {
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 read_unlock(&qdisc_tree_lock);
915 read_unlock(&qdisc_tree_lock);
919 read_unlock(&dev_base_lock);
929 /************************************************
930 * Traffic classes manipulation. *
931 ************************************************/
935 static int tc_ctl_tclass(struct sk_buff *skb, struct nlmsghdr *n, void *arg)
937 struct tcmsg *tcm = NLMSG_DATA(n);
938 struct rtattr **tca = arg;
939 struct net_device *dev;
940 struct Qdisc *q = NULL;
941 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);
949 if ((dev = __dev_get_by_index(tcm->tcm_ifindex)) == NULL)
953 parent == TC_H_UNSPEC - unspecified parent.
954 parent == TC_H_ROOT - class is root, which has no parent.
955 parent == X:0 - parent is root class.
956 parent == X:Y - parent is a node in hierarchy.
957 parent == 0:Y - parent is X:Y, where X:0 is qdisc.
959 handle == 0:0 - generate handle from kernel pool.
960 handle == 0:Y - class is X:Y, where X:0 is qdisc.
961 handle == X:Y - clear.
962 handle == X:0 - root class.
965 /* Step 1. Determine qdisc handle X:0 */
967 if (pid != TC_H_ROOT) {
968 u32 qid1 = TC_H_MAJ(pid);
971 /* If both majors are known, they must be identical. */
977 qid = dev->qdisc_sleeping->handle;
979 /* Now qid is genuine qdisc handle consistent
980 both with parent and child.
982 TC_H_MAJ(pid) still may be unspecified, complete it now.
985 pid = TC_H_MAKE(qid, pid);
988 qid = dev->qdisc_sleeping->handle;
991 /* OK. Locate qdisc */
992 if ((q = qdisc_lookup(dev, qid)) == NULL)
995 /* An check that it supports classes */
996 cops = q->ops->cl_ops;
1000 /* Now try to get class */
1002 if (pid == TC_H_ROOT)
1005 clid = TC_H_MAKE(qid, clid);
1008 cl = cops->get(q, clid);
1012 if (n->nlmsg_type != RTM_NEWTCLASS || !(n->nlmsg_flags&NLM_F_CREATE))
1015 switch (n->nlmsg_type) {
1018 if (n->nlmsg_flags&NLM_F_EXCL)
1022 err = cops->delete(q, cl);
1024 tclass_notify(skb, n, q, cl, RTM_DELTCLASS);
1027 err = tclass_notify(skb, n, q, cl, RTM_NEWTCLASS);
1036 err = cops->change(q, clid, pid, tca, &new_cl);
1038 tclass_notify(skb, n, q, new_cl, RTM_NEWTCLASS);
1048 static int tc_fill_tclass(struct sk_buff *skb, struct Qdisc *q,
1050 u32 pid, u32 seq, u16 flags, int event)
1053 struct nlmsghdr *nlh;
1054 unsigned char *b = skb->tail;
1056 struct Qdisc_class_ops *cl_ops = q->ops->cl_ops;
1058 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*tcm), flags);
1059 tcm = NLMSG_DATA(nlh);
1060 tcm->tcm_family = AF_UNSPEC;
1061 tcm->tcm_ifindex = q->dev->ifindex;
1062 tcm->tcm_parent = q->handle;
1063 tcm->tcm_handle = q->handle;
1065 RTA_PUT(skb, TCA_KIND, IFNAMSIZ, q->ops->id);
1066 if (cl_ops->dump && cl_ops->dump(q, cl, skb, tcm) < 0)
1067 goto rtattr_failure;
1069 if (gnet_stats_start_copy_compat(skb, TCA_STATS2, TCA_STATS,
1070 TCA_XSTATS, q->stats_lock, &d) < 0)
1071 goto rtattr_failure;
1073 if (cl_ops->dump_stats && cl_ops->dump_stats(q, cl, &d) < 0)
1074 goto rtattr_failure;
1076 if (gnet_stats_finish_copy(&d) < 0)
1077 goto rtattr_failure;
1079 nlh->nlmsg_len = skb->tail - b;
1084 skb_trim(skb, b - skb->data);
1088 static int tclass_notify(struct sk_buff *oskb, struct nlmsghdr *n,
1089 struct Qdisc *q, unsigned long cl, int event)
1091 struct sk_buff *skb;
1092 u32 pid = oskb ? NETLINK_CB(oskb).pid : 0;
1094 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1098 if (tc_fill_tclass(skb, q, cl, pid, n->nlmsg_seq, 0, event) < 0) {
1103 return rtnetlink_send(skb, pid, RTNLGRP_TC, n->nlmsg_flags&NLM_F_ECHO);
1106 struct qdisc_dump_args
1108 struct qdisc_walker w;
1109 struct sk_buff *skb;
1110 struct netlink_callback *cb;
1113 static int qdisc_class_dump(struct Qdisc *q, unsigned long cl, struct qdisc_walker *arg)
1115 struct qdisc_dump_args *a = (struct qdisc_dump_args *)arg;
1117 return tc_fill_tclass(a->skb, q, cl, NETLINK_CB(a->cb->skb).pid,
1118 a->cb->nlh->nlmsg_seq, NLM_F_MULTI, RTM_NEWTCLASS);
1121 static int tc_dump_tclass(struct sk_buff *skb, struct netlink_callback *cb)
1125 struct net_device *dev;
1127 struct tcmsg *tcm = (struct tcmsg*)NLMSG_DATA(cb->nlh);
1128 struct qdisc_dump_args arg;
1130 if (cb->nlh->nlmsg_len < NLMSG_LENGTH(sizeof(*tcm)))
1132 if ((dev = dev_get_by_index(tcm->tcm_ifindex)) == NULL)
1138 read_lock(&qdisc_tree_lock);
1139 list_for_each_entry(q, &dev->qdisc_list, list) {
1140 if (t < s_t || !q->ops->cl_ops ||
1142 TC_H_MAJ(tcm->tcm_parent) != q->handle)) {
1147 memset(&cb->args[1], 0, sizeof(cb->args)-sizeof(cb->args[0]));
1148 arg.w.fn = qdisc_class_dump;
1152 arg.w.skip = cb->args[1];
1154 q->ops->cl_ops->walk(q, &arg.w);
1155 cb->args[1] = arg.w.count;
1160 read_unlock(&qdisc_tree_lock);
1168 /* Main classifier routine: scans classifier chain attached
1169 to this qdisc, (optionally) tests for protocol and asks
1170 specific classifiers.
1172 int tc_classify(struct sk_buff *skb, struct tcf_proto *tp,
1173 struct tcf_result *res)
1176 __be16 protocol = skb->protocol;
1177 #ifdef CONFIG_NET_CLS_ACT
1178 struct tcf_proto *otp = tp;
1181 protocol = skb->protocol;
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 ( TC_ACT_RECLASSIFY == err) {
1189 __u32 verd = (__u32) G_TC_VERD(skb->tc_verd);
1192 if (MAX_REC_LOOP < verd++) {
1193 printk("rule prio %d protocol %02x reclassify is buggy packet dropped\n",
1194 tp->prio&0xffff, ntohs(tp->protocol));
1197 skb->tc_verd = SET_TC_VERD(skb->tc_verd,verd);
1201 skb->tc_verd = SET_TC_VERD(skb->tc_verd,0);
1214 #ifdef CONFIG_PROC_FS
1215 static int psched_show(struct seq_file *seq, void *v)
1217 seq_printf(seq, "%08x %08x %08x %08x\n",
1218 (u32)NSEC_PER_USEC, (u32)PSCHED_US2NS(1),
1220 (u32)NSEC_PER_SEC/(u32)ktime_to_ns(KTIME_MONOTONIC_RES));
1225 static int psched_open(struct inode *inode, struct file *file)
1227 return single_open(file, psched_show, PDE(inode)->data);
1230 static const struct file_operations psched_fops = {
1231 .owner = THIS_MODULE,
1232 .open = psched_open,
1234 .llseek = seq_lseek,
1235 .release = single_release,
1239 static int __init pktsched_init(void)
1241 struct rtnetlink_link *link_p;
1243 link_p = rtnetlink_links[PF_UNSPEC];
1245 /* Setup rtnetlink links. It is made here to avoid
1246 exporting large number of public symbols.
1250 link_p[RTM_NEWQDISC-RTM_BASE].doit = tc_modify_qdisc;
1251 link_p[RTM_DELQDISC-RTM_BASE].doit = tc_get_qdisc;
1252 link_p[RTM_GETQDISC-RTM_BASE].doit = tc_get_qdisc;
1253 link_p[RTM_GETQDISC-RTM_BASE].dumpit = tc_dump_qdisc;
1254 link_p[RTM_NEWTCLASS-RTM_BASE].doit = tc_ctl_tclass;
1255 link_p[RTM_DELTCLASS-RTM_BASE].doit = tc_ctl_tclass;
1256 link_p[RTM_GETTCLASS-RTM_BASE].doit = tc_ctl_tclass;
1257 link_p[RTM_GETTCLASS-RTM_BASE].dumpit = tc_dump_tclass;
1260 register_qdisc(&pfifo_qdisc_ops);
1261 register_qdisc(&bfifo_qdisc_ops);
1262 proc_net_fops_create("psched", 0, &psched_fops);
1267 subsys_initcall(pktsched_init);
1269 EXPORT_SYMBOL(qdisc_get_rtab);
1270 EXPORT_SYMBOL(qdisc_put_rtab);
1271 EXPORT_SYMBOL(register_qdisc);
1272 EXPORT_SYMBOL(unregister_qdisc);
1273 EXPORT_SYMBOL(tc_classify);