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>
32 #include <net/netlink.h>
33 #include <net/pkt_sched.h>
35 static int qdisc_notify(struct sk_buff
*oskb
, struct nlmsghdr
*n
, u32 clid
,
36 struct Qdisc
*old
, struct Qdisc
*new);
37 static int tclass_notify(struct sk_buff
*oskb
, struct nlmsghdr
*n
,
38 struct Qdisc
*q
, unsigned long cl
, int event
);
45 This file consists of two interrelated parts:
47 1. queueing disciplines manager frontend.
48 2. traffic classes manager frontend.
50 Generally, queueing discipline ("qdisc") is a black box,
51 which is able to enqueue packets and to dequeue them (when
52 device is ready to send something) in order and at times
53 determined by algorithm hidden in it.
55 qdisc's are divided to two categories:
56 - "queues", which have no internal structure visible from outside.
57 - "schedulers", which split all the packets to "traffic classes",
58 using "packet classifiers" (look at cls_api.c)
60 In turn, classes may have child qdiscs (as rule, queues)
61 attached to them etc. etc. etc.
63 The goal of the routines in this file is to translate
64 information supplied by user in the form of handles
65 to more intelligible for kernel form, to make some sanity
66 checks and part of work, which is common to all qdiscs
67 and to provide rtnetlink notifications.
69 All real intelligent work is done inside qdisc modules.
73 Every discipline has two major routines: enqueue and dequeue.
77 dequeue usually returns a skb to send. It is allowed to return NULL,
78 but it does not mean that queue is empty, it just means that
79 discipline does not want to send anything this time.
80 Queue is really empty if q->q.qlen == 0.
81 For complicated disciplines with multiple queues q->q is not
82 real packet queue, but however q->q.qlen must be valid.
86 enqueue returns 0, if packet was enqueued successfully.
87 If packet (this one or another one) was dropped, it returns
89 NET_XMIT_DROP - this packet dropped
90 Expected action: do not backoff, but wait until queue will clear.
91 NET_XMIT_CN - probably this packet enqueued, but another one dropped.
92 Expected action: backoff or ignore
93 NET_XMIT_POLICED - dropped by police.
94 Expected action: backoff or error to real-time apps.
100 requeues once dequeued packet. It is used for non-standard or
101 just buggy devices, which can defer output even if dev->tbusy=0.
105 returns qdisc to initial state: purge all buffers, clear all
106 timers, counters (except for statistics) etc.
110 initializes newly created qdisc.
114 destroys resources allocated by init and during lifetime of qdisc.
118 changes qdisc parameters.
121 /* Protects list of registered TC modules. It is pure SMP lock. */
122 static DEFINE_RWLOCK(qdisc_mod_lock
);
125 /************************************************
126 * Queueing disciplines manipulation. *
127 ************************************************/
130 /* The list of all installed queueing disciplines. */
132 static struct Qdisc_ops
*qdisc_base
;
134 /* Register/uregister queueing discipline */
136 int register_qdisc(struct Qdisc_ops
*qops
)
138 struct Qdisc_ops
*q
, **qp
;
141 write_lock(&qdisc_mod_lock
);
142 for (qp
= &qdisc_base
; (q
= *qp
) != NULL
; qp
= &q
->next
)
143 if (!strcmp(qops
->id
, q
->id
))
146 if (qops
->enqueue
== NULL
)
147 qops
->enqueue
= noop_qdisc_ops
.enqueue
;
148 if (qops
->requeue
== NULL
)
149 qops
->requeue
= noop_qdisc_ops
.requeue
;
150 if (qops
->dequeue
== NULL
)
151 qops
->dequeue
= noop_qdisc_ops
.dequeue
;
157 write_unlock(&qdisc_mod_lock
);
161 int unregister_qdisc(struct Qdisc_ops
*qops
)
163 struct Qdisc_ops
*q
, **qp
;
166 write_lock(&qdisc_mod_lock
);
167 for (qp
= &qdisc_base
; (q
=*qp
)!=NULL
; qp
= &q
->next
)
175 write_unlock(&qdisc_mod_lock
);
179 /* We know handle. Find qdisc among all qdisc's attached to device
180 (root qdisc, all its children, children of children etc.)
183 struct Qdisc
*qdisc_lookup(struct net_device
*dev
, u32 handle
)
187 list_for_each_entry(q
, &dev
->qdisc_list
, list
) {
188 if (q
->handle
== handle
)
194 static struct Qdisc
*qdisc_leaf(struct Qdisc
*p
, u32 classid
)
198 struct Qdisc_class_ops
*cops
= p
->ops
->cl_ops
;
202 cl
= cops
->get(p
, classid
);
206 leaf
= cops
->leaf(p
, cl
);
211 /* Find queueing discipline by name */
213 static struct Qdisc_ops
*qdisc_lookup_ops(struct rtattr
*kind
)
215 struct Qdisc_ops
*q
= NULL
;
218 read_lock(&qdisc_mod_lock
);
219 for (q
= qdisc_base
; q
; q
= q
->next
) {
220 if (rtattr_strcmp(kind
, q
->id
) == 0) {
221 if (!try_module_get(q
->owner
))
226 read_unlock(&qdisc_mod_lock
);
231 static struct qdisc_rate_table
*qdisc_rtab_list
;
233 struct qdisc_rate_table
*qdisc_get_rtab(struct tc_ratespec
*r
, struct rtattr
*tab
)
235 struct qdisc_rate_table
*rtab
;
237 for (rtab
= qdisc_rtab_list
; rtab
; rtab
= rtab
->next
) {
238 if (memcmp(&rtab
->rate
, r
, sizeof(struct tc_ratespec
)) == 0) {
244 if (tab
== NULL
|| r
->rate
== 0 || r
->cell_log
== 0 || RTA_PAYLOAD(tab
) != 1024)
247 rtab
= kmalloc(sizeof(*rtab
), GFP_KERNEL
);
251 memcpy(rtab
->data
, RTA_DATA(tab
), 1024);
252 rtab
->next
= qdisc_rtab_list
;
253 qdisc_rtab_list
= rtab
;
258 void qdisc_put_rtab(struct qdisc_rate_table
*tab
)
260 struct qdisc_rate_table
*rtab
, **rtabp
;
262 if (!tab
|| --tab
->refcnt
)
265 for (rtabp
= &qdisc_rtab_list
; (rtab
=*rtabp
) != NULL
; rtabp
= &rtab
->next
) {
274 static enum hrtimer_restart
qdisc_watchdog(struct hrtimer
*timer
)
276 struct qdisc_watchdog
*wd
= container_of(timer
, struct qdisc_watchdog
,
278 struct net_device
*dev
= wd
->qdisc
->dev
;
280 wd
->qdisc
->flags
&= ~TCQ_F_THROTTLED
;
284 return HRTIMER_NORESTART
;
287 void qdisc_watchdog_init(struct qdisc_watchdog
*wd
, struct Qdisc
*qdisc
)
289 hrtimer_init(&wd
->timer
, CLOCK_MONOTONIC
, HRTIMER_MODE_ABS
);
290 wd
->timer
.function
= qdisc_watchdog
;
293 EXPORT_SYMBOL(qdisc_watchdog_init
);
295 void qdisc_watchdog_schedule(struct qdisc_watchdog
*wd
, psched_time_t expires
)
299 wd
->qdisc
->flags
|= TCQ_F_THROTTLED
;
300 time
= ktime_set(0, 0);
301 time
= ktime_add_ns(time
, PSCHED_US2NS(expires
));
302 hrtimer_start(&wd
->timer
, time
, HRTIMER_MODE_ABS
);
304 EXPORT_SYMBOL(qdisc_watchdog_schedule
);
306 void qdisc_watchdog_cancel(struct qdisc_watchdog
*wd
)
308 hrtimer_cancel(&wd
->timer
);
309 wd
->qdisc
->flags
&= ~TCQ_F_THROTTLED
;
311 EXPORT_SYMBOL(qdisc_watchdog_cancel
);
313 /* Allocate an unique handle from space managed by kernel */
315 static u32
qdisc_alloc_handle(struct net_device
*dev
)
318 static u32 autohandle
= TC_H_MAKE(0x80000000U
, 0);
321 autohandle
+= TC_H_MAKE(0x10000U
, 0);
322 if (autohandle
== TC_H_MAKE(TC_H_ROOT
, 0))
323 autohandle
= TC_H_MAKE(0x80000000U
, 0);
324 } while (qdisc_lookup(dev
, autohandle
) && --i
> 0);
326 return i
>0 ? autohandle
: 0;
329 /* Attach toplevel qdisc to device dev */
331 static struct Qdisc
*
332 dev_graft_qdisc(struct net_device
*dev
, struct Qdisc
*qdisc
)
334 struct Qdisc
*oqdisc
;
336 if (dev
->flags
& IFF_UP
)
339 qdisc_lock_tree(dev
);
340 if (qdisc
&& qdisc
->flags
&TCQ_F_INGRESS
) {
341 oqdisc
= dev
->qdisc_ingress
;
342 /* Prune old scheduler */
343 if (oqdisc
&& atomic_read(&oqdisc
->refcnt
) <= 1) {
346 dev
->qdisc_ingress
= NULL
;
348 dev
->qdisc_ingress
= qdisc
;
353 oqdisc
= dev
->qdisc_sleeping
;
355 /* Prune old scheduler */
356 if (oqdisc
&& atomic_read(&oqdisc
->refcnt
) <= 1)
359 /* ... and graft new one */
362 dev
->qdisc_sleeping
= qdisc
;
363 dev
->qdisc
= &noop_qdisc
;
366 qdisc_unlock_tree(dev
);
368 if (dev
->flags
& IFF_UP
)
374 void qdisc_tree_decrease_qlen(struct Qdisc
*sch
, unsigned int n
)
376 struct Qdisc_class_ops
*cops
;
382 while ((parentid
= sch
->parent
)) {
383 sch
= qdisc_lookup(sch
->dev
, TC_H_MAJ(parentid
));
385 WARN_ON(parentid
!= TC_H_ROOT
);
388 cops
= sch
->ops
->cl_ops
;
389 if (cops
->qlen_notify
) {
390 cl
= cops
->get(sch
, parentid
);
391 cops
->qlen_notify(sch
, cl
);
397 EXPORT_SYMBOL(qdisc_tree_decrease_qlen
);
399 /* Graft qdisc "new" to class "classid" of qdisc "parent" or
402 Old qdisc is not destroyed but returned in *old.
405 static int qdisc_graft(struct net_device
*dev
, struct Qdisc
*parent
,
407 struct Qdisc
*new, struct Qdisc
**old
)
410 struct Qdisc
*q
= *old
;
413 if (parent
== NULL
) {
414 if (q
&& q
->flags
&TCQ_F_INGRESS
) {
415 *old
= dev_graft_qdisc(dev
, q
);
417 *old
= dev_graft_qdisc(dev
, new);
420 struct Qdisc_class_ops
*cops
= parent
->ops
->cl_ops
;
425 unsigned long cl
= cops
->get(parent
, classid
);
427 err
= cops
->graft(parent
, cl
, new, old
);
428 cops
->put(parent
, cl
);
436 Allocate and initialize new qdisc.
438 Parameters are passed via opt.
441 static struct Qdisc
*
442 qdisc_create(struct net_device
*dev
, u32 parent
, u32 handle
,
443 struct rtattr
**tca
, int *errp
)
446 struct rtattr
*kind
= tca
[TCA_KIND
-1];
448 struct Qdisc_ops
*ops
;
450 ops
= qdisc_lookup_ops(kind
);
452 if (ops
== NULL
&& kind
!= NULL
) {
454 if (rtattr_strlcpy(name
, kind
, IFNAMSIZ
) < IFNAMSIZ
) {
455 /* We dropped the RTNL semaphore in order to
456 * perform the module load. So, even if we
457 * succeeded in loading the module we have to
458 * tell the caller to replay the request. We
459 * indicate this using -EAGAIN.
460 * We replay the request because the device may
461 * go away in the mean time.
464 request_module("sch_%s", name
);
466 ops
= qdisc_lookup_ops(kind
);
468 /* We will try again qdisc_lookup_ops,
469 * so don't keep a reference.
471 module_put(ops
->owner
);
483 sch
= qdisc_alloc(dev
, ops
);
489 sch
->parent
= parent
;
491 if (handle
== TC_H_INGRESS
) {
492 sch
->flags
|= TCQ_F_INGRESS
;
493 sch
->stats_lock
= &dev
->ingress_lock
;
494 handle
= TC_H_MAKE(TC_H_INGRESS
, 0);
496 sch
->stats_lock
= &dev
->queue_lock
;
498 handle
= qdisc_alloc_handle(dev
);
505 sch
->handle
= handle
;
507 if (!ops
->init
|| (err
= ops
->init(sch
, tca
[TCA_OPTIONS
-1])) == 0) {
508 if (tca
[TCA_RATE
-1]) {
509 err
= gen_new_estimator(&sch
->bstats
, &sch
->rate_est
,
514 * Any broken qdiscs that would require
515 * a ops->reset() here? The qdisc was never
516 * in action so it shouldn't be necessary.
523 qdisc_lock_tree(dev
);
524 list_add_tail(&sch
->list
, &dev
->qdisc_list
);
525 qdisc_unlock_tree(dev
);
531 kfree((char *) sch
- sch
->padded
);
533 module_put(ops
->owner
);
539 static int qdisc_change(struct Qdisc
*sch
, struct rtattr
**tca
)
541 if (tca
[TCA_OPTIONS
-1]) {
544 if (sch
->ops
->change
== NULL
)
546 err
= sch
->ops
->change(sch
, tca
[TCA_OPTIONS
-1]);
551 gen_replace_estimator(&sch
->bstats
, &sch
->rate_est
,
552 sch
->stats_lock
, tca
[TCA_RATE
-1]);
556 struct check_loop_arg
558 struct qdisc_walker w
;
563 static int check_loop_fn(struct Qdisc
*q
, unsigned long cl
, struct qdisc_walker
*w
);
565 static int check_loop(struct Qdisc
*q
, struct Qdisc
*p
, int depth
)
567 struct check_loop_arg arg
;
569 if (q
->ops
->cl_ops
== NULL
)
572 arg
.w
.stop
= arg
.w
.skip
= arg
.w
.count
= 0;
573 arg
.w
.fn
= check_loop_fn
;
576 q
->ops
->cl_ops
->walk(q
, &arg
.w
);
577 return arg
.w
.stop
? -ELOOP
: 0;
581 check_loop_fn(struct Qdisc
*q
, unsigned long cl
, struct qdisc_walker
*w
)
584 struct Qdisc_class_ops
*cops
= q
->ops
->cl_ops
;
585 struct check_loop_arg
*arg
= (struct check_loop_arg
*)w
;
587 leaf
= cops
->leaf(q
, cl
);
589 if (leaf
== arg
->p
|| arg
->depth
> 7)
591 return check_loop(leaf
, arg
->p
, arg
->depth
+ 1);
600 static int tc_get_qdisc(struct sk_buff
*skb
, struct nlmsghdr
*n
, void *arg
)
602 struct tcmsg
*tcm
= NLMSG_DATA(n
);
603 struct rtattr
**tca
= arg
;
604 struct net_device
*dev
;
605 u32 clid
= tcm
->tcm_parent
;
606 struct Qdisc
*q
= NULL
;
607 struct Qdisc
*p
= NULL
;
610 if ((dev
= __dev_get_by_index(&init_net
, tcm
->tcm_ifindex
)) == NULL
)
614 if (clid
!= TC_H_ROOT
) {
615 if (TC_H_MAJ(clid
) != TC_H_MAJ(TC_H_INGRESS
)) {
616 if ((p
= qdisc_lookup(dev
, TC_H_MAJ(clid
))) == NULL
)
618 q
= qdisc_leaf(p
, clid
);
619 } else { /* ingress */
620 q
= dev
->qdisc_ingress
;
623 q
= dev
->qdisc_sleeping
;
628 if (tcm
->tcm_handle
&& q
->handle
!= tcm
->tcm_handle
)
631 if ((q
= qdisc_lookup(dev
, tcm
->tcm_handle
)) == NULL
)
635 if (tca
[TCA_KIND
-1] && rtattr_strcmp(tca
[TCA_KIND
-1], q
->ops
->id
))
638 if (n
->nlmsg_type
== RTM_DELQDISC
) {
643 if ((err
= qdisc_graft(dev
, p
, clid
, NULL
, &q
)) != 0)
646 qdisc_notify(skb
, n
, clid
, q
, NULL
);
647 qdisc_lock_tree(dev
);
649 qdisc_unlock_tree(dev
);
652 qdisc_notify(skb
, n
, clid
, NULL
, q
);
661 static int tc_modify_qdisc(struct sk_buff
*skb
, struct nlmsghdr
*n
, void *arg
)
665 struct net_device
*dev
;
671 /* Reinit, just in case something touches this. */
674 clid
= tcm
->tcm_parent
;
677 if ((dev
= __dev_get_by_index(&init_net
, tcm
->tcm_ifindex
)) == NULL
)
681 if (clid
!= TC_H_ROOT
) {
682 if (clid
!= TC_H_INGRESS
) {
683 if ((p
= qdisc_lookup(dev
, TC_H_MAJ(clid
))) == NULL
)
685 q
= qdisc_leaf(p
, clid
);
686 } else { /*ingress */
687 q
= dev
->qdisc_ingress
;
690 q
= dev
->qdisc_sleeping
;
693 /* It may be default qdisc, ignore it */
694 if (q
&& q
->handle
== 0)
697 if (!q
|| !tcm
->tcm_handle
|| q
->handle
!= tcm
->tcm_handle
) {
698 if (tcm
->tcm_handle
) {
699 if (q
&& !(n
->nlmsg_flags
&NLM_F_REPLACE
))
701 if (TC_H_MIN(tcm
->tcm_handle
))
703 if ((q
= qdisc_lookup(dev
, tcm
->tcm_handle
)) == NULL
)
705 if (n
->nlmsg_flags
&NLM_F_EXCL
)
707 if (tca
[TCA_KIND
-1] && rtattr_strcmp(tca
[TCA_KIND
-1], q
->ops
->id
))
710 (p
&& check_loop(q
, p
, 0)))
712 atomic_inc(&q
->refcnt
);
718 /* This magic test requires explanation.
720 * We know, that some child q is already
721 * attached to this parent and have choice:
722 * either to change it or to create/graft new one.
724 * 1. We are allowed to create/graft only
725 * if CREATE and REPLACE flags are set.
727 * 2. If EXCL is set, requestor wanted to say,
728 * that qdisc tcm_handle is not expected
729 * to exist, so that we choose create/graft too.
731 * 3. The last case is when no flags are set.
732 * Alas, it is sort of hole in API, we
733 * cannot decide what to do unambiguously.
734 * For now we select create/graft, if
735 * user gave KIND, which does not match existing.
737 if ((n
->nlmsg_flags
&NLM_F_CREATE
) &&
738 (n
->nlmsg_flags
&NLM_F_REPLACE
) &&
739 ((n
->nlmsg_flags
&NLM_F_EXCL
) ||
741 rtattr_strcmp(tca
[TCA_KIND
-1], q
->ops
->id
))))
746 if (!tcm
->tcm_handle
)
748 q
= qdisc_lookup(dev
, tcm
->tcm_handle
);
751 /* Change qdisc parameters */
754 if (n
->nlmsg_flags
&NLM_F_EXCL
)
756 if (tca
[TCA_KIND
-1] && rtattr_strcmp(tca
[TCA_KIND
-1], q
->ops
->id
))
758 err
= qdisc_change(q
, tca
);
760 qdisc_notify(skb
, n
, clid
, NULL
, q
);
764 if (!(n
->nlmsg_flags
&NLM_F_CREATE
))
766 if (clid
== TC_H_INGRESS
)
767 q
= qdisc_create(dev
, tcm
->tcm_parent
, tcm
->tcm_parent
,
770 q
= qdisc_create(dev
, tcm
->tcm_parent
, tcm
->tcm_handle
,
780 struct Qdisc
*old_q
= NULL
;
781 err
= qdisc_graft(dev
, p
, clid
, q
, &old_q
);
784 qdisc_lock_tree(dev
);
786 qdisc_unlock_tree(dev
);
790 qdisc_notify(skb
, n
, clid
, old_q
, q
);
792 qdisc_lock_tree(dev
);
793 qdisc_destroy(old_q
);
794 qdisc_unlock_tree(dev
);
800 static int tc_fill_qdisc(struct sk_buff
*skb
, struct Qdisc
*q
, u32 clid
,
801 u32 pid
, u32 seq
, u16 flags
, int event
)
804 struct nlmsghdr
*nlh
;
805 unsigned char *b
= skb_tail_pointer(skb
);
808 nlh
= NLMSG_NEW(skb
, pid
, seq
, event
, sizeof(*tcm
), flags
);
809 tcm
= NLMSG_DATA(nlh
);
810 tcm
->tcm_family
= AF_UNSPEC
;
813 tcm
->tcm_ifindex
= q
->dev
->ifindex
;
814 tcm
->tcm_parent
= clid
;
815 tcm
->tcm_handle
= q
->handle
;
816 tcm
->tcm_info
= atomic_read(&q
->refcnt
);
817 RTA_PUT(skb
, TCA_KIND
, IFNAMSIZ
, q
->ops
->id
);
818 if (q
->ops
->dump
&& q
->ops
->dump(q
, skb
) < 0)
820 q
->qstats
.qlen
= q
->q
.qlen
;
822 if (gnet_stats_start_copy_compat(skb
, TCA_STATS2
, TCA_STATS
,
823 TCA_XSTATS
, q
->stats_lock
, &d
) < 0)
826 if (q
->ops
->dump_stats
&& q
->ops
->dump_stats(q
, &d
) < 0)
829 if (gnet_stats_copy_basic(&d
, &q
->bstats
) < 0 ||
830 gnet_stats_copy_rate_est(&d
, &q
->rate_est
) < 0 ||
831 gnet_stats_copy_queue(&d
, &q
->qstats
) < 0)
834 if (gnet_stats_finish_copy(&d
) < 0)
837 nlh
->nlmsg_len
= skb_tail_pointer(skb
) - b
;
846 static int qdisc_notify(struct sk_buff
*oskb
, struct nlmsghdr
*n
,
847 u32 clid
, struct Qdisc
*old
, struct Qdisc
*new)
850 u32 pid
= oskb
? NETLINK_CB(oskb
).pid
: 0;
852 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
856 if (old
&& old
->handle
) {
857 if (tc_fill_qdisc(skb
, old
, clid
, pid
, n
->nlmsg_seq
, 0, RTM_DELQDISC
) < 0)
861 if (tc_fill_qdisc(skb
, new, clid
, pid
, n
->nlmsg_seq
, old
? NLM_F_REPLACE
: 0, RTM_NEWQDISC
) < 0)
866 return rtnetlink_send(skb
, pid
, RTNLGRP_TC
, n
->nlmsg_flags
&NLM_F_ECHO
);
873 static int tc_dump_qdisc(struct sk_buff
*skb
, struct netlink_callback
*cb
)
877 struct net_device
*dev
;
881 s_q_idx
= q_idx
= cb
->args
[1];
882 read_lock(&dev_base_lock
);
884 for_each_netdev(&init_net
, dev
) {
890 list_for_each_entry(q
, &dev
->qdisc_list
, list
) {
891 if (q_idx
< s_q_idx
) {
895 if (tc_fill_qdisc(skb
, q
, q
->parent
, NETLINK_CB(cb
->skb
).pid
,
896 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
, RTM_NEWQDISC
) <= 0)
905 read_unlock(&dev_base_lock
);
915 /************************************************
916 * Traffic classes manipulation. *
917 ************************************************/
921 static int tc_ctl_tclass(struct sk_buff
*skb
, struct nlmsghdr
*n
, void *arg
)
923 struct tcmsg
*tcm
= NLMSG_DATA(n
);
924 struct rtattr
**tca
= arg
;
925 struct net_device
*dev
;
926 struct Qdisc
*q
= NULL
;
927 struct Qdisc_class_ops
*cops
;
928 unsigned long cl
= 0;
929 unsigned long new_cl
;
930 u32 pid
= tcm
->tcm_parent
;
931 u32 clid
= tcm
->tcm_handle
;
932 u32 qid
= TC_H_MAJ(clid
);
935 if ((dev
= __dev_get_by_index(&init_net
, tcm
->tcm_ifindex
)) == NULL
)
939 parent == TC_H_UNSPEC - unspecified parent.
940 parent == TC_H_ROOT - class is root, which has no parent.
941 parent == X:0 - parent is root class.
942 parent == X:Y - parent is a node in hierarchy.
943 parent == 0:Y - parent is X:Y, where X:0 is qdisc.
945 handle == 0:0 - generate handle from kernel pool.
946 handle == 0:Y - class is X:Y, where X:0 is qdisc.
947 handle == X:Y - clear.
948 handle == X:0 - root class.
951 /* Step 1. Determine qdisc handle X:0 */
953 if (pid
!= TC_H_ROOT
) {
954 u32 qid1
= TC_H_MAJ(pid
);
957 /* If both majors are known, they must be identical. */
963 qid
= dev
->qdisc_sleeping
->handle
;
965 /* Now qid is genuine qdisc handle consistent
966 both with parent and child.
968 TC_H_MAJ(pid) still may be unspecified, complete it now.
971 pid
= TC_H_MAKE(qid
, pid
);
974 qid
= dev
->qdisc_sleeping
->handle
;
977 /* OK. Locate qdisc */
978 if ((q
= qdisc_lookup(dev
, qid
)) == NULL
)
981 /* An check that it supports classes */
982 cops
= q
->ops
->cl_ops
;
986 /* Now try to get class */
988 if (pid
== TC_H_ROOT
)
991 clid
= TC_H_MAKE(qid
, clid
);
994 cl
= cops
->get(q
, clid
);
998 if (n
->nlmsg_type
!= RTM_NEWTCLASS
|| !(n
->nlmsg_flags
&NLM_F_CREATE
))
1001 switch (n
->nlmsg_type
) {
1004 if (n
->nlmsg_flags
&NLM_F_EXCL
)
1008 err
= cops
->delete(q
, cl
);
1010 tclass_notify(skb
, n
, q
, cl
, RTM_DELTCLASS
);
1013 err
= tclass_notify(skb
, n
, q
, cl
, RTM_NEWTCLASS
);
1022 err
= cops
->change(q
, clid
, pid
, tca
, &new_cl
);
1024 tclass_notify(skb
, n
, q
, new_cl
, RTM_NEWTCLASS
);
1034 static int tc_fill_tclass(struct sk_buff
*skb
, struct Qdisc
*q
,
1036 u32 pid
, u32 seq
, u16 flags
, int event
)
1039 struct nlmsghdr
*nlh
;
1040 unsigned char *b
= skb_tail_pointer(skb
);
1042 struct Qdisc_class_ops
*cl_ops
= q
->ops
->cl_ops
;
1044 nlh
= NLMSG_NEW(skb
, pid
, seq
, event
, sizeof(*tcm
), flags
);
1045 tcm
= NLMSG_DATA(nlh
);
1046 tcm
->tcm_family
= AF_UNSPEC
;
1047 tcm
->tcm_ifindex
= q
->dev
->ifindex
;
1048 tcm
->tcm_parent
= q
->handle
;
1049 tcm
->tcm_handle
= q
->handle
;
1051 RTA_PUT(skb
, TCA_KIND
, IFNAMSIZ
, q
->ops
->id
);
1052 if (cl_ops
->dump
&& cl_ops
->dump(q
, cl
, skb
, tcm
) < 0)
1053 goto rtattr_failure
;
1055 if (gnet_stats_start_copy_compat(skb
, TCA_STATS2
, TCA_STATS
,
1056 TCA_XSTATS
, q
->stats_lock
, &d
) < 0)
1057 goto rtattr_failure
;
1059 if (cl_ops
->dump_stats
&& cl_ops
->dump_stats(q
, cl
, &d
) < 0)
1060 goto rtattr_failure
;
1062 if (gnet_stats_finish_copy(&d
) < 0)
1063 goto rtattr_failure
;
1065 nlh
->nlmsg_len
= skb_tail_pointer(skb
) - b
;
1074 static int tclass_notify(struct sk_buff
*oskb
, struct nlmsghdr
*n
,
1075 struct Qdisc
*q
, unsigned long cl
, int event
)
1077 struct sk_buff
*skb
;
1078 u32 pid
= oskb
? NETLINK_CB(oskb
).pid
: 0;
1080 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
1084 if (tc_fill_tclass(skb
, q
, cl
, pid
, n
->nlmsg_seq
, 0, event
) < 0) {
1089 return rtnetlink_send(skb
, pid
, RTNLGRP_TC
, n
->nlmsg_flags
&NLM_F_ECHO
);
1092 struct qdisc_dump_args
1094 struct qdisc_walker w
;
1095 struct sk_buff
*skb
;
1096 struct netlink_callback
*cb
;
1099 static int qdisc_class_dump(struct Qdisc
*q
, unsigned long cl
, struct qdisc_walker
*arg
)
1101 struct qdisc_dump_args
*a
= (struct qdisc_dump_args
*)arg
;
1103 return tc_fill_tclass(a
->skb
, q
, cl
, NETLINK_CB(a
->cb
->skb
).pid
,
1104 a
->cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
, RTM_NEWTCLASS
);
1107 static int tc_dump_tclass(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1111 struct net_device
*dev
;
1113 struct tcmsg
*tcm
= (struct tcmsg
*)NLMSG_DATA(cb
->nlh
);
1114 struct qdisc_dump_args arg
;
1116 if (cb
->nlh
->nlmsg_len
< NLMSG_LENGTH(sizeof(*tcm
)))
1118 if ((dev
= dev_get_by_index(&init_net
, tcm
->tcm_ifindex
)) == NULL
)
1124 list_for_each_entry(q
, &dev
->qdisc_list
, list
) {
1125 if (t
< s_t
|| !q
->ops
->cl_ops
||
1127 TC_H_MAJ(tcm
->tcm_parent
) != q
->handle
)) {
1132 memset(&cb
->args
[1], 0, sizeof(cb
->args
)-sizeof(cb
->args
[0]));
1133 arg
.w
.fn
= qdisc_class_dump
;
1137 arg
.w
.skip
= cb
->args
[1];
1139 q
->ops
->cl_ops
->walk(q
, &arg
.w
);
1140 cb
->args
[1] = arg
.w
.count
;
1152 /* Main classifier routine: scans classifier chain attached
1153 to this qdisc, (optionally) tests for protocol and asks
1154 specific classifiers.
1156 int tc_classify_compat(struct sk_buff
*skb
, struct tcf_proto
*tp
,
1157 struct tcf_result
*res
)
1159 __be16 protocol
= skb
->protocol
;
1162 for (; tp
; tp
= tp
->next
) {
1163 if ((tp
->protocol
== protocol
||
1164 tp
->protocol
== htons(ETH_P_ALL
)) &&
1165 (err
= tp
->classify(skb
, tp
, res
)) >= 0) {
1166 #ifdef CONFIG_NET_CLS_ACT
1167 if (err
!= TC_ACT_RECLASSIFY
&& skb
->tc_verd
)
1168 skb
->tc_verd
= SET_TC_VERD(skb
->tc_verd
, 0);
1175 EXPORT_SYMBOL(tc_classify_compat
);
1177 int tc_classify(struct sk_buff
*skb
, struct tcf_proto
*tp
,
1178 struct tcf_result
*res
)
1182 #ifdef CONFIG_NET_CLS_ACT
1183 struct tcf_proto
*otp
= tp
;
1186 protocol
= skb
->protocol
;
1188 err
= tc_classify_compat(skb
, tp
, res
);
1189 #ifdef CONFIG_NET_CLS_ACT
1190 if (err
== TC_ACT_RECLASSIFY
) {
1191 u32 verd
= G_TC_VERD(skb
->tc_verd
);
1194 if (verd
++ >= MAX_REC_LOOP
) {
1195 printk("rule prio %u protocol %02x reclassify loop, "
1197 tp
->prio
&0xffff, ntohs(tp
->protocol
));
1200 skb
->tc_verd
= SET_TC_VERD(skb
->tc_verd
, verd
);
1206 EXPORT_SYMBOL(tc_classify
);
1208 void tcf_destroy(struct tcf_proto
*tp
)
1210 tp
->ops
->destroy(tp
);
1211 module_put(tp
->ops
->owner
);
1215 void tcf_destroy_chain(struct tcf_proto
*fl
)
1217 struct tcf_proto
*tp
;
1219 while ((tp
= fl
) != NULL
) {
1224 EXPORT_SYMBOL(tcf_destroy_chain
);
1226 #ifdef CONFIG_PROC_FS
1227 static int psched_show(struct seq_file
*seq
, void *v
)
1229 seq_printf(seq
, "%08x %08x %08x %08x\n",
1230 (u32
)NSEC_PER_USEC
, (u32
)PSCHED_US2NS(1),
1232 (u32
)NSEC_PER_SEC
/(u32
)ktime_to_ns(KTIME_MONOTONIC_RES
));
1237 static int psched_open(struct inode
*inode
, struct file
*file
)
1239 return single_open(file
, psched_show
, PDE(inode
)->data
);
1242 static const struct file_operations psched_fops
= {
1243 .owner
= THIS_MODULE
,
1244 .open
= psched_open
,
1246 .llseek
= seq_lseek
,
1247 .release
= single_release
,
1251 static int __init
pktsched_init(void)
1253 register_qdisc(&pfifo_qdisc_ops
);
1254 register_qdisc(&bfifo_qdisc_ops
);
1255 proc_net_fops_create(&init_net
, "psched", 0, &psched_fops
);
1257 rtnl_register(PF_UNSPEC
, RTM_NEWQDISC
, tc_modify_qdisc
, NULL
);
1258 rtnl_register(PF_UNSPEC
, RTM_DELQDISC
, tc_get_qdisc
, NULL
);
1259 rtnl_register(PF_UNSPEC
, RTM_GETQDISC
, tc_get_qdisc
, tc_dump_qdisc
);
1260 rtnl_register(PF_UNSPEC
, RTM_NEWTCLASS
, tc_ctl_tclass
, NULL
);
1261 rtnl_register(PF_UNSPEC
, RTM_DELTCLASS
, tc_ctl_tclass
, NULL
);
1262 rtnl_register(PF_UNSPEC
, RTM_GETTCLASS
, tc_ctl_tclass
, tc_dump_tclass
);
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
);