blob: 6fb3d41c0e41174d35d4d96612c493effcb1da4c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * net/sched/sch_api.c Packet scheduler API.
3 *
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.
8 *
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 *
11 * Fixes:
12 *
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
16 */
17
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/module.h>
19#include <linux/types.h>
20#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#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>
Patrick McHardy41794772007-03-16 01:19:15 -070029#include <linux/hrtimer.h>
Jarek Poplawski25bfcd52008-08-18 20:53:34 -070030#include <linux/lockdep.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020033#include <net/net_namespace.h>
Denis V. Lunevb8542722007-12-01 00:21:31 +110034#include <net/sock.h>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070035#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <net/pkt_sched.h>
37
Tom Goff7316ae82010-03-19 15:40:13 +000038static int qdisc_notify(struct net *net, struct sk_buff *oskb,
39 struct nlmsghdr *n, u32 clid,
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 struct Qdisc *old, struct Qdisc *new);
Tom Goff7316ae82010-03-19 15:40:13 +000041static int tclass_notify(struct net *net, struct sk_buff *oskb,
42 struct nlmsghdr *n, struct Qdisc *q,
43 unsigned long cl, int event);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45/*
46
47 Short review.
48 -------------
49
50 This file consists of two interrelated parts:
51
52 1. queueing disciplines manager frontend.
53 2. traffic classes manager frontend.
54
55 Generally, queueing discipline ("qdisc") is a black box,
56 which is able to enqueue packets and to dequeue them (when
57 device is ready to send something) in order and at times
58 determined by algorithm hidden in it.
59
60 qdisc's are divided to two categories:
61 - "queues", which have no internal structure visible from outside.
62 - "schedulers", which split all the packets to "traffic classes",
63 using "packet classifiers" (look at cls_api.c)
64
65 In turn, classes may have child qdiscs (as rule, queues)
66 attached to them etc. etc. etc.
67
68 The goal of the routines in this file is to translate
69 information supplied by user in the form of handles
70 to more intelligible for kernel form, to make some sanity
71 checks and part of work, which is common to all qdiscs
72 and to provide rtnetlink notifications.
73
74 All real intelligent work is done inside qdisc modules.
75
76
77
78 Every discipline has two major routines: enqueue and dequeue.
79
80 ---dequeue
81
82 dequeue usually returns a skb to send. It is allowed to return NULL,
83 but it does not mean that queue is empty, it just means that
84 discipline does not want to send anything this time.
85 Queue is really empty if q->q.qlen == 0.
86 For complicated disciplines with multiple queues q->q is not
87 real packet queue, but however q->q.qlen must be valid.
88
89 ---enqueue
90
91 enqueue returns 0, if packet was enqueued successfully.
92 If packet (this one or another one) was dropped, it returns
93 not zero error code.
94 NET_XMIT_DROP - this packet dropped
95 Expected action: do not backoff, but wait until queue will clear.
96 NET_XMIT_CN - probably this packet enqueued, but another one dropped.
97 Expected action: backoff or ignore
98 NET_XMIT_POLICED - dropped by police.
99 Expected action: backoff or error to real-time apps.
100
101 Auxiliary routines:
102
Jarek Poplawski99c0db22008-10-31 00:45:27 -0700103 ---peek
104
105 like dequeue but without removing a packet from the queue
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 ---reset
108
109 returns qdisc to initial state: purge all buffers, clear all
110 timers, counters (except for statistics) etc.
111
112 ---init
113
114 initializes newly created qdisc.
115
116 ---destroy
117
118 destroys resources allocated by init and during lifetime of qdisc.
119
120 ---change
121
122 changes qdisc parameters.
123 */
124
125/* Protects list of registered TC modules. It is pure SMP lock. */
126static DEFINE_RWLOCK(qdisc_mod_lock);
127
128
129/************************************************
130 * Queueing disciplines manipulation. *
131 ************************************************/
132
133
134/* The list of all installed queueing disciplines. */
135
136static struct Qdisc_ops *qdisc_base;
137
138/* Register/uregister queueing discipline */
139
140int register_qdisc(struct Qdisc_ops *qops)
141{
142 struct Qdisc_ops *q, **qp;
143 int rc = -EEXIST;
144
145 write_lock(&qdisc_mod_lock);
146 for (qp = &qdisc_base; (q = *qp) != NULL; qp = &q->next)
147 if (!strcmp(qops->id, q->id))
148 goto out;
149
150 if (qops->enqueue == NULL)
151 qops->enqueue = noop_qdisc_ops.enqueue;
Jarek Poplawski99c0db22008-10-31 00:45:27 -0700152 if (qops->peek == NULL) {
Jarek Poplawski68fd26b2010-08-09 12:18:48 +0000153 if (qops->dequeue == NULL)
Jarek Poplawski99c0db22008-10-31 00:45:27 -0700154 qops->peek = noop_qdisc_ops.peek;
Jarek Poplawski68fd26b2010-08-09 12:18:48 +0000155 else
156 goto out_einval;
Jarek Poplawski99c0db22008-10-31 00:45:27 -0700157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 if (qops->dequeue == NULL)
159 qops->dequeue = noop_qdisc_ops.dequeue;
160
Jarek Poplawski68fd26b2010-08-09 12:18:48 +0000161 if (qops->cl_ops) {
162 const struct Qdisc_class_ops *cops = qops->cl_ops;
163
Jarek Poplawski3e9e5a52010-08-10 22:31:20 +0000164 if (!(cops->get && cops->put && cops->walk && cops->leaf))
Jarek Poplawski68fd26b2010-08-09 12:18:48 +0000165 goto out_einval;
166
167 if (cops->tcf_chain && !(cops->bind_tcf && cops->unbind_tcf))
168 goto out_einval;
169 }
170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 qops->next = NULL;
172 *qp = qops;
173 rc = 0;
174out:
175 write_unlock(&qdisc_mod_lock);
176 return rc;
Jarek Poplawski68fd26b2010-08-09 12:18:48 +0000177
178out_einval:
179 rc = -EINVAL;
180 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800182EXPORT_SYMBOL(register_qdisc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184int unregister_qdisc(struct Qdisc_ops *qops)
185{
186 struct Qdisc_ops *q, **qp;
187 int err = -ENOENT;
188
189 write_lock(&qdisc_mod_lock);
190 for (qp = &qdisc_base; (q=*qp)!=NULL; qp = &q->next)
191 if (q == qops)
192 break;
193 if (q) {
194 *qp = q->next;
195 q->next = NULL;
196 err = 0;
197 }
198 write_unlock(&qdisc_mod_lock);
199 return err;
200}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800201EXPORT_SYMBOL(unregister_qdisc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203/* We know handle. Find qdisc among all qdisc's attached to device
204 (root qdisc, all its children, children of children etc.)
205 */
206
Hannes Eder6113b742008-11-28 03:06:46 -0800207static struct Qdisc *qdisc_match_from_root(struct Qdisc *root, u32 handle)
David S. Miller8123b422008-08-08 23:23:39 -0700208{
209 struct Qdisc *q;
210
211 if (!(root->flags & TCQ_F_BUILTIN) &&
212 root->handle == handle)
213 return root;
214
215 list_for_each_entry(q, &root->list, list) {
216 if (q->handle == handle)
217 return q;
218 }
219 return NULL;
220}
221
Jarek Poplawskif6e0b232008-08-22 03:24:05 -0700222static void qdisc_list_add(struct Qdisc *q)
223{
Jarek Poplawskif6486d42008-11-25 13:56:06 -0800224 if ((q->parent != TC_H_ROOT) && !(q->flags & TCQ_F_INGRESS))
Patrick McHardyaf356af2009-09-04 06:41:18 +0000225 list_add_tail(&q->list, &qdisc_dev(q)->qdisc->list);
Jarek Poplawskif6e0b232008-08-22 03:24:05 -0700226}
227
228void qdisc_list_del(struct Qdisc *q)
229{
Jarek Poplawskif6486d42008-11-25 13:56:06 -0800230 if ((q->parent != TC_H_ROOT) && !(q->flags & TCQ_F_INGRESS))
Jarek Poplawskif6e0b232008-08-22 03:24:05 -0700231 list_del(&q->list);
Jarek Poplawskif6e0b232008-08-22 03:24:05 -0700232}
233EXPORT_SYMBOL(qdisc_list_del);
234
David S. Milleread81cc2008-07-17 00:50:32 -0700235struct Qdisc *qdisc_lookup(struct net_device *dev, u32 handle)
Patrick McHardy43effa12006-11-29 17:35:48 -0800236{
Jarek Poplawskif6e0b232008-08-22 03:24:05 -0700237 struct Qdisc *q;
238
Patrick McHardyaf356af2009-09-04 06:41:18 +0000239 q = qdisc_match_from_root(dev->qdisc, handle);
240 if (q)
241 goto out;
Jarek Poplawskif6e0b232008-08-22 03:24:05 -0700242
243 q = qdisc_match_from_root(dev->rx_queue.qdisc_sleeping, handle);
Jarek Poplawskif6486d42008-11-25 13:56:06 -0800244out:
Jarek Poplawskif6e0b232008-08-22 03:24:05 -0700245 return q;
Patrick McHardy43effa12006-11-29 17:35:48 -0800246}
247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248static struct Qdisc *qdisc_leaf(struct Qdisc *p, u32 classid)
249{
250 unsigned long cl;
251 struct Qdisc *leaf;
Eric Dumazet20fea082007-11-14 01:44:41 -0800252 const struct Qdisc_class_ops *cops = p->ops->cl_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254 if (cops == NULL)
255 return NULL;
256 cl = cops->get(p, classid);
257
258 if (cl == 0)
259 return NULL;
260 leaf = cops->leaf(p, cl);
261 cops->put(p, cl);
262 return leaf;
263}
264
265/* Find queueing discipline by name */
266
Patrick McHardy1e904742008-01-22 22:11:17 -0800267static struct Qdisc_ops *qdisc_lookup_ops(struct nlattr *kind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
269 struct Qdisc_ops *q = NULL;
270
271 if (kind) {
272 read_lock(&qdisc_mod_lock);
273 for (q = qdisc_base; q; q = q->next) {
Patrick McHardy1e904742008-01-22 22:11:17 -0800274 if (nla_strcmp(kind, q->id) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 if (!try_module_get(q->owner))
276 q = NULL;
277 break;
278 }
279 }
280 read_unlock(&qdisc_mod_lock);
281 }
282 return q;
283}
284
285static struct qdisc_rate_table *qdisc_rtab_list;
286
Patrick McHardy1e904742008-01-22 22:11:17 -0800287struct qdisc_rate_table *qdisc_get_rtab(struct tc_ratespec *r, struct nlattr *tab)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
289 struct qdisc_rate_table *rtab;
290
291 for (rtab = qdisc_rtab_list; rtab; rtab = rtab->next) {
292 if (memcmp(&rtab->rate, r, sizeof(struct tc_ratespec)) == 0) {
293 rtab->refcnt++;
294 return rtab;
295 }
296 }
297
Patrick McHardy5feb5e12008-01-23 20:35:19 -0800298 if (tab == NULL || r->rate == 0 || r->cell_log == 0 ||
299 nla_len(tab) != TC_RTAB_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 return NULL;
301
302 rtab = kmalloc(sizeof(*rtab), GFP_KERNEL);
303 if (rtab) {
304 rtab->rate = *r;
305 rtab->refcnt = 1;
Patrick McHardy1e904742008-01-22 22:11:17 -0800306 memcpy(rtab->data, nla_data(tab), 1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 rtab->next = qdisc_rtab_list;
308 qdisc_rtab_list = rtab;
309 }
310 return rtab;
311}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800312EXPORT_SYMBOL(qdisc_get_rtab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314void qdisc_put_rtab(struct qdisc_rate_table *tab)
315{
316 struct qdisc_rate_table *rtab, **rtabp;
317
318 if (!tab || --tab->refcnt)
319 return;
320
321 for (rtabp = &qdisc_rtab_list; (rtab=*rtabp) != NULL; rtabp = &rtab->next) {
322 if (rtab == tab) {
323 *rtabp = rtab->next;
324 kfree(rtab);
325 return;
326 }
327 }
328}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800329EXPORT_SYMBOL(qdisc_put_rtab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Jussi Kivilinna175f9c12008-07-20 00:08:47 -0700331static LIST_HEAD(qdisc_stab_list);
332static DEFINE_SPINLOCK(qdisc_stab_lock);
333
334static const struct nla_policy stab_policy[TCA_STAB_MAX + 1] = {
335 [TCA_STAB_BASE] = { .len = sizeof(struct tc_sizespec) },
336 [TCA_STAB_DATA] = { .type = NLA_BINARY },
337};
338
339static struct qdisc_size_table *qdisc_get_stab(struct nlattr *opt)
340{
341 struct nlattr *tb[TCA_STAB_MAX + 1];
342 struct qdisc_size_table *stab;
343 struct tc_sizespec *s;
344 unsigned int tsize = 0;
345 u16 *tab = NULL;
346 int err;
347
348 err = nla_parse_nested(tb, TCA_STAB_MAX, opt, stab_policy);
349 if (err < 0)
350 return ERR_PTR(err);
351 if (!tb[TCA_STAB_BASE])
352 return ERR_PTR(-EINVAL);
353
354 s = nla_data(tb[TCA_STAB_BASE]);
355
356 if (s->tsize > 0) {
357 if (!tb[TCA_STAB_DATA])
358 return ERR_PTR(-EINVAL);
359 tab = nla_data(tb[TCA_STAB_DATA]);
360 tsize = nla_len(tb[TCA_STAB_DATA]) / sizeof(u16);
361 }
362
Dan Carpenter00093fa2010-08-14 11:09:49 +0000363 if (tsize != s->tsize || (!tab && tsize > 0))
Jussi Kivilinna175f9c12008-07-20 00:08:47 -0700364 return ERR_PTR(-EINVAL);
365
David S. Millerf3b96052008-08-18 22:33:05 -0700366 spin_lock(&qdisc_stab_lock);
Jussi Kivilinna175f9c12008-07-20 00:08:47 -0700367
368 list_for_each_entry(stab, &qdisc_stab_list, list) {
369 if (memcmp(&stab->szopts, s, sizeof(*s)))
370 continue;
371 if (tsize > 0 && memcmp(stab->data, tab, tsize * sizeof(u16)))
372 continue;
373 stab->refcnt++;
David S. Millerf3b96052008-08-18 22:33:05 -0700374 spin_unlock(&qdisc_stab_lock);
Jussi Kivilinna175f9c12008-07-20 00:08:47 -0700375 return stab;
376 }
377
David S. Millerf3b96052008-08-18 22:33:05 -0700378 spin_unlock(&qdisc_stab_lock);
Jussi Kivilinna175f9c12008-07-20 00:08:47 -0700379
380 stab = kmalloc(sizeof(*stab) + tsize * sizeof(u16), GFP_KERNEL);
381 if (!stab)
382 return ERR_PTR(-ENOMEM);
383
384 stab->refcnt = 1;
385 stab->szopts = *s;
386 if (tsize > 0)
387 memcpy(stab->data, tab, tsize * sizeof(u16));
388
David S. Millerf3b96052008-08-18 22:33:05 -0700389 spin_lock(&qdisc_stab_lock);
Jussi Kivilinna175f9c12008-07-20 00:08:47 -0700390 list_add_tail(&stab->list, &qdisc_stab_list);
David S. Millerf3b96052008-08-18 22:33:05 -0700391 spin_unlock(&qdisc_stab_lock);
Jussi Kivilinna175f9c12008-07-20 00:08:47 -0700392
393 return stab;
394}
395
396void qdisc_put_stab(struct qdisc_size_table *tab)
397{
398 if (!tab)
399 return;
400
David S. Millerf3b96052008-08-18 22:33:05 -0700401 spin_lock(&qdisc_stab_lock);
Jussi Kivilinna175f9c12008-07-20 00:08:47 -0700402
403 if (--tab->refcnt == 0) {
404 list_del(&tab->list);
405 kfree(tab);
406 }
407
David S. Millerf3b96052008-08-18 22:33:05 -0700408 spin_unlock(&qdisc_stab_lock);
Jussi Kivilinna175f9c12008-07-20 00:08:47 -0700409}
410EXPORT_SYMBOL(qdisc_put_stab);
411
412static int qdisc_dump_stab(struct sk_buff *skb, struct qdisc_size_table *stab)
413{
414 struct nlattr *nest;
415
416 nest = nla_nest_start(skb, TCA_STAB);
Patrick McHardy3aa46142008-11-20 04:07:14 -0800417 if (nest == NULL)
418 goto nla_put_failure;
Jussi Kivilinna175f9c12008-07-20 00:08:47 -0700419 NLA_PUT(skb, TCA_STAB_BASE, sizeof(stab->szopts), &stab->szopts);
420 nla_nest_end(skb, nest);
421
422 return skb->len;
423
424nla_put_failure:
425 return -1;
426}
427
428void qdisc_calculate_pkt_len(struct sk_buff *skb, struct qdisc_size_table *stab)
429{
430 int pkt_len, slot;
431
432 pkt_len = skb->len + stab->szopts.overhead;
433 if (unlikely(!stab->szopts.tsize))
434 goto out;
435
436 slot = pkt_len + stab->szopts.cell_align;
437 if (unlikely(slot < 0))
438 slot = 0;
439
440 slot >>= stab->szopts.cell_log;
441 if (likely(slot < stab->szopts.tsize))
442 pkt_len = stab->data[slot];
443 else
444 pkt_len = stab->data[stab->szopts.tsize - 1] *
445 (slot / stab->szopts.tsize) +
446 stab->data[slot % stab->szopts.tsize];
447
448 pkt_len <<= stab->szopts.size_log;
449out:
450 if (unlikely(pkt_len < 1))
451 pkt_len = 1;
452 qdisc_skb_cb(skb)->pkt_len = pkt_len;
453}
454EXPORT_SYMBOL(qdisc_calculate_pkt_len);
455
Jarek Poplawskib00355d2009-02-01 01:12:42 -0800456void qdisc_warn_nonwc(char *txt, struct Qdisc *qdisc)
457{
458 if (!(qdisc->flags & TCQ_F_WARN_NONWC)) {
459 printk(KERN_WARNING
460 "%s: %s qdisc %X: is non-work-conserving?\n",
461 txt, qdisc->ops->id, qdisc->handle >> 16);
462 qdisc->flags |= TCQ_F_WARN_NONWC;
463 }
464}
465EXPORT_SYMBOL(qdisc_warn_nonwc);
466
Patrick McHardy41794772007-03-16 01:19:15 -0700467static enum hrtimer_restart qdisc_watchdog(struct hrtimer *timer)
468{
469 struct qdisc_watchdog *wd = container_of(timer, struct qdisc_watchdog,
David S. Miller2fbd3da2009-09-01 17:59:25 -0700470 timer);
Patrick McHardy41794772007-03-16 01:19:15 -0700471
472 wd->qdisc->flags &= ~TCQ_F_THROTTLED;
David S. Miller8608db02008-08-18 20:51:18 -0700473 __netif_schedule(qdisc_root(wd->qdisc));
Stephen Hemminger19365022007-03-22 12:18:35 -0700474
Patrick McHardy41794772007-03-16 01:19:15 -0700475 return HRTIMER_NORESTART;
476}
477
478void qdisc_watchdog_init(struct qdisc_watchdog *wd, struct Qdisc *qdisc)
479{
David S. Miller2fbd3da2009-09-01 17:59:25 -0700480 hrtimer_init(&wd->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
481 wd->timer.function = qdisc_watchdog;
Patrick McHardy41794772007-03-16 01:19:15 -0700482 wd->qdisc = qdisc;
483}
484EXPORT_SYMBOL(qdisc_watchdog_init);
485
486void qdisc_watchdog_schedule(struct qdisc_watchdog *wd, psched_time_t expires)
487{
488 ktime_t time;
489
Jarek Poplawski2540e052008-08-21 05:11:14 -0700490 if (test_bit(__QDISC_STATE_DEACTIVATED,
491 &qdisc_root_sleeping(wd->qdisc)->state))
492 return;
493
Patrick McHardy41794772007-03-16 01:19:15 -0700494 wd->qdisc->flags |= TCQ_F_THROTTLED;
495 time = ktime_set(0, 0);
Jarek Poplawskica44d6e2009-06-15 02:31:47 -0700496 time = ktime_add_ns(time, PSCHED_TICKS2NS(expires));
David S. Miller2fbd3da2009-09-01 17:59:25 -0700497 hrtimer_start(&wd->timer, time, HRTIMER_MODE_ABS);
Patrick McHardy41794772007-03-16 01:19:15 -0700498}
499EXPORT_SYMBOL(qdisc_watchdog_schedule);
500
501void qdisc_watchdog_cancel(struct qdisc_watchdog *wd)
502{
David S. Miller2fbd3da2009-09-01 17:59:25 -0700503 hrtimer_cancel(&wd->timer);
Patrick McHardy41794772007-03-16 01:19:15 -0700504 wd->qdisc->flags &= ~TCQ_F_THROTTLED;
505}
506EXPORT_SYMBOL(qdisc_watchdog_cancel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Adrian Bunka94f7792008-07-22 14:20:11 -0700508static struct hlist_head *qdisc_class_hash_alloc(unsigned int n)
Patrick McHardy6fe1c7a2008-07-05 23:21:31 -0700509{
510 unsigned int size = n * sizeof(struct hlist_head), i;
511 struct hlist_head *h;
512
513 if (size <= PAGE_SIZE)
514 h = kmalloc(size, GFP_KERNEL);
515 else
516 h = (struct hlist_head *)
517 __get_free_pages(GFP_KERNEL, get_order(size));
518
519 if (h != NULL) {
520 for (i = 0; i < n; i++)
521 INIT_HLIST_HEAD(&h[i]);
522 }
523 return h;
524}
525
526static void qdisc_class_hash_free(struct hlist_head *h, unsigned int n)
527{
528 unsigned int size = n * sizeof(struct hlist_head);
529
530 if (size <= PAGE_SIZE)
531 kfree(h);
532 else
533 free_pages((unsigned long)h, get_order(size));
534}
535
536void qdisc_class_hash_grow(struct Qdisc *sch, struct Qdisc_class_hash *clhash)
537{
538 struct Qdisc_class_common *cl;
539 struct hlist_node *n, *next;
540 struct hlist_head *nhash, *ohash;
541 unsigned int nsize, nmask, osize;
542 unsigned int i, h;
543
544 /* Rehash when load factor exceeds 0.75 */
545 if (clhash->hashelems * 4 <= clhash->hashsize * 3)
546 return;
547 nsize = clhash->hashsize * 2;
548 nmask = nsize - 1;
549 nhash = qdisc_class_hash_alloc(nsize);
550 if (nhash == NULL)
551 return;
552
553 ohash = clhash->hash;
554 osize = clhash->hashsize;
555
556 sch_tree_lock(sch);
557 for (i = 0; i < osize; i++) {
558 hlist_for_each_entry_safe(cl, n, next, &ohash[i], hnode) {
559 h = qdisc_class_hash(cl->classid, nmask);
560 hlist_add_head(&cl->hnode, &nhash[h]);
561 }
562 }
563 clhash->hash = nhash;
564 clhash->hashsize = nsize;
565 clhash->hashmask = nmask;
566 sch_tree_unlock(sch);
567
568 qdisc_class_hash_free(ohash, osize);
569}
570EXPORT_SYMBOL(qdisc_class_hash_grow);
571
572int qdisc_class_hash_init(struct Qdisc_class_hash *clhash)
573{
574 unsigned int size = 4;
575
576 clhash->hash = qdisc_class_hash_alloc(size);
577 if (clhash->hash == NULL)
578 return -ENOMEM;
579 clhash->hashsize = size;
580 clhash->hashmask = size - 1;
581 clhash->hashelems = 0;
582 return 0;
583}
584EXPORT_SYMBOL(qdisc_class_hash_init);
585
586void qdisc_class_hash_destroy(struct Qdisc_class_hash *clhash)
587{
588 qdisc_class_hash_free(clhash->hash, clhash->hashsize);
589}
590EXPORT_SYMBOL(qdisc_class_hash_destroy);
591
592void qdisc_class_hash_insert(struct Qdisc_class_hash *clhash,
593 struct Qdisc_class_common *cl)
594{
595 unsigned int h;
596
597 INIT_HLIST_NODE(&cl->hnode);
598 h = qdisc_class_hash(cl->classid, clhash->hashmask);
599 hlist_add_head(&cl->hnode, &clhash->hash[h]);
600 clhash->hashelems++;
601}
602EXPORT_SYMBOL(qdisc_class_hash_insert);
603
604void qdisc_class_hash_remove(struct Qdisc_class_hash *clhash,
605 struct Qdisc_class_common *cl)
606{
607 hlist_del(&cl->hnode);
608 clhash->hashelems--;
609}
610EXPORT_SYMBOL(qdisc_class_hash_remove);
611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612/* Allocate an unique handle from space managed by kernel */
613
614static u32 qdisc_alloc_handle(struct net_device *dev)
615{
616 int i = 0x10000;
617 static u32 autohandle = TC_H_MAKE(0x80000000U, 0);
618
619 do {
620 autohandle += TC_H_MAKE(0x10000U, 0);
621 if (autohandle == TC_H_MAKE(TC_H_ROOT, 0))
622 autohandle = TC_H_MAKE(0x80000000U, 0);
623 } while (qdisc_lookup(dev, autohandle) && --i > 0);
624
625 return i>0 ? autohandle : 0;
626}
627
Patrick McHardy43effa12006-11-29 17:35:48 -0800628void qdisc_tree_decrease_qlen(struct Qdisc *sch, unsigned int n)
629{
Eric Dumazet20fea082007-11-14 01:44:41 -0800630 const struct Qdisc_class_ops *cops;
Patrick McHardy43effa12006-11-29 17:35:48 -0800631 unsigned long cl;
632 u32 parentid;
633
634 if (n == 0)
635 return;
636 while ((parentid = sch->parent)) {
Jarek Poplawski066a3b52008-04-14 15:10:42 -0700637 if (TC_H_MAJ(parentid) == TC_H_MAJ(TC_H_INGRESS))
638 return;
639
David S. Miller5ce2d482008-07-08 17:06:30 -0700640 sch = qdisc_lookup(qdisc_dev(sch), TC_H_MAJ(parentid));
Patrick McHardyffc8fef2007-07-30 17:11:50 -0700641 if (sch == NULL) {
642 WARN_ON(parentid != TC_H_ROOT);
643 return;
644 }
Patrick McHardy43effa12006-11-29 17:35:48 -0800645 cops = sch->ops->cl_ops;
646 if (cops->qlen_notify) {
647 cl = cops->get(sch, parentid);
648 cops->qlen_notify(sch, cl);
649 cops->put(sch, cl);
650 }
651 sch->q.qlen -= n;
652 }
653}
654EXPORT_SYMBOL(qdisc_tree_decrease_qlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
Tom Goff7316ae82010-03-19 15:40:13 +0000656static void notify_and_destroy(struct net *net, struct sk_buff *skb,
657 struct nlmsghdr *n, u32 clid,
David S. Miller99194cf2008-07-17 04:54:10 -0700658 struct Qdisc *old, struct Qdisc *new)
659{
660 if (new || old)
Tom Goff7316ae82010-03-19 15:40:13 +0000661 qdisc_notify(net, skb, n, clid, old, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
David S. Miller4d8863a2008-08-18 21:03:15 -0700663 if (old)
David S. Miller99194cf2008-07-17 04:54:10 -0700664 qdisc_destroy(old);
David S. Miller99194cf2008-07-17 04:54:10 -0700665}
666
667/* Graft qdisc "new" to class "classid" of qdisc "parent" or
668 * to device "dev".
669 *
670 * When appropriate send a netlink notification using 'skb'
671 * and "n".
672 *
673 * On success, destroy old qdisc.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 */
675
676static int qdisc_graft(struct net_device *dev, struct Qdisc *parent,
David S. Miller99194cf2008-07-17 04:54:10 -0700677 struct sk_buff *skb, struct nlmsghdr *n, u32 classid,
678 struct Qdisc *new, struct Qdisc *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
David S. Miller99194cf2008-07-17 04:54:10 -0700680 struct Qdisc *q = old;
Tom Goff7316ae82010-03-19 15:40:13 +0000681 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900684 if (parent == NULL) {
David S. Miller99194cf2008-07-17 04:54:10 -0700685 unsigned int i, num_q, ingress;
686
687 ingress = 0;
688 num_q = dev->num_tx_queues;
David S. Miller8d50b532008-07-30 02:37:46 -0700689 if ((q && q->flags & TCQ_F_INGRESS) ||
690 (new && new->flags & TCQ_F_INGRESS)) {
David S. Miller99194cf2008-07-17 04:54:10 -0700691 num_q = 1;
692 ingress = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 }
David S. Miller99194cf2008-07-17 04:54:10 -0700694
695 if (dev->flags & IFF_UP)
696 dev_deactivate(dev);
697
David S. Miller6ec1c692009-09-06 01:58:51 -0700698 if (new && new->ops->attach) {
699 new->ops->attach(new);
700 num_q = 0;
701 }
702
David S. Miller99194cf2008-07-17 04:54:10 -0700703 for (i = 0; i < num_q; i++) {
704 struct netdev_queue *dev_queue = &dev->rx_queue;
705
706 if (!ingress)
707 dev_queue = netdev_get_tx_queue(dev, i);
708
David S. Miller8d50b532008-07-30 02:37:46 -0700709 old = dev_graft_qdisc(dev_queue, new);
710 if (new && i > 0)
711 atomic_inc(&new->refcnt);
712
Jarek Poplawski036d6a62009-09-13 22:35:44 +0000713 if (!ingress)
714 qdisc_destroy(old);
David S. Miller99194cf2008-07-17 04:54:10 -0700715 }
716
Jarek Poplawski036d6a62009-09-13 22:35:44 +0000717 if (!ingress) {
Tom Goff7316ae82010-03-19 15:40:13 +0000718 notify_and_destroy(net, skb, n, classid,
719 dev->qdisc, new);
Jarek Poplawski036d6a62009-09-13 22:35:44 +0000720 if (new && !new->ops->attach)
721 atomic_inc(&new->refcnt);
722 dev->qdisc = new ? : &noop_qdisc;
723 } else {
Tom Goff7316ae82010-03-19 15:40:13 +0000724 notify_and_destroy(net, skb, n, classid, old, new);
Jarek Poplawski036d6a62009-09-13 22:35:44 +0000725 }
Patrick McHardyaf356af2009-09-04 06:41:18 +0000726
David S. Miller99194cf2008-07-17 04:54:10 -0700727 if (dev->flags & IFF_UP)
728 dev_activate(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 } else {
Eric Dumazet20fea082007-11-14 01:44:41 -0800730 const struct Qdisc_class_ops *cops = parent->ops->cl_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Patrick McHardyc9f1d032009-09-04 06:41:13 +0000732 err = -EOPNOTSUPP;
733 if (cops && cops->graft) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 unsigned long cl = cops->get(parent, classid);
735 if (cl) {
David S. Miller99194cf2008-07-17 04:54:10 -0700736 err = cops->graft(parent, cl, new, &old);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 cops->put(parent, cl);
Patrick McHardyc9f1d032009-09-04 06:41:13 +0000738 } else
739 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 }
David S. Miller99194cf2008-07-17 04:54:10 -0700741 if (!err)
Tom Goff7316ae82010-03-19 15:40:13 +0000742 notify_and_destroy(net, skb, n, classid, old, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 }
744 return err;
745}
746
Jarek Poplawski25bfcd52008-08-18 20:53:34 -0700747/* lockdep annotation is needed for ingress; egress gets it only for name */
748static struct lock_class_key qdisc_tx_lock;
749static struct lock_class_key qdisc_rx_lock;
750
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751/*
752 Allocate and initialize new qdisc.
753
754 Parameters are passed via opt.
755 */
756
757static struct Qdisc *
David S. Millerbb949fb2008-07-08 16:55:56 -0700758qdisc_create(struct net_device *dev, struct netdev_queue *dev_queue,
Patrick McHardy23bcf632009-09-09 18:11:23 -0700759 struct Qdisc *p, u32 parent, u32 handle,
760 struct nlattr **tca, int *errp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761{
762 int err;
Patrick McHardy1e904742008-01-22 22:11:17 -0800763 struct nlattr *kind = tca[TCA_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 struct Qdisc *sch;
765 struct Qdisc_ops *ops;
Jussi Kivilinna175f9c12008-07-20 00:08:47 -0700766 struct qdisc_size_table *stab;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
768 ops = qdisc_lookup_ops(kind);
Johannes Berg95a5afc2008-10-16 15:24:51 -0700769#ifdef CONFIG_MODULES
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 if (ops == NULL && kind != NULL) {
771 char name[IFNAMSIZ];
Patrick McHardy1e904742008-01-22 22:11:17 -0800772 if (nla_strlcpy(name, kind, IFNAMSIZ) < IFNAMSIZ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 /* We dropped the RTNL semaphore in order to
774 * perform the module load. So, even if we
775 * succeeded in loading the module we have to
776 * tell the caller to replay the request. We
777 * indicate this using -EAGAIN.
778 * We replay the request because the device may
779 * go away in the mean time.
780 */
781 rtnl_unlock();
782 request_module("sch_%s", name);
783 rtnl_lock();
784 ops = qdisc_lookup_ops(kind);
785 if (ops != NULL) {
786 /* We will try again qdisc_lookup_ops,
787 * so don't keep a reference.
788 */
789 module_put(ops->owner);
790 err = -EAGAIN;
791 goto err_out;
792 }
793 }
794 }
795#endif
796
Jamal Hadi Salimb9e2cc02006-08-03 16:36:51 -0700797 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 if (ops == NULL)
799 goto err_out;
800
David S. Miller5ce2d482008-07-08 17:06:30 -0700801 sch = qdisc_alloc(dev_queue, ops);
Thomas Graf3d54b822005-07-05 14:15:09 -0700802 if (IS_ERR(sch)) {
803 err = PTR_ERR(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 goto err_out2;
Thomas Graf3d54b822005-07-05 14:15:09 -0700805 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Patrick McHardyffc8fef2007-07-30 17:11:50 -0700807 sch->parent = parent;
808
Thomas Graf3d54b822005-07-05 14:15:09 -0700809 if (handle == TC_H_INGRESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 sch->flags |= TCQ_F_INGRESS;
Thomas Graf3d54b822005-07-05 14:15:09 -0700811 handle = TC_H_MAKE(TC_H_INGRESS, 0);
Jarek Poplawski25bfcd52008-08-18 20:53:34 -0700812 lockdep_set_class(qdisc_lock(sch), &qdisc_rx_lock);
Patrick McHardyfd44de72007-04-16 17:07:08 -0700813 } else {
Patrick McHardyfd44de72007-04-16 17:07:08 -0700814 if (handle == 0) {
815 handle = qdisc_alloc_handle(dev);
816 err = -ENOMEM;
817 if (handle == 0)
818 goto err_out3;
819 }
Jarek Poplawski25bfcd52008-08-18 20:53:34 -0700820 lockdep_set_class(qdisc_lock(sch), &qdisc_tx_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 }
822
Thomas Graf3d54b822005-07-05 14:15:09 -0700823 sch->handle = handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Patrick McHardy1e904742008-01-22 22:11:17 -0800825 if (!ops->init || (err = ops->init(sch, tca[TCA_OPTIONS])) == 0) {
Jussi Kivilinna175f9c12008-07-20 00:08:47 -0700826 if (tca[TCA_STAB]) {
827 stab = qdisc_get_stab(tca[TCA_STAB]);
828 if (IS_ERR(stab)) {
829 err = PTR_ERR(stab);
Jarek Poplawski7c64b9f2009-09-15 23:42:05 -0700830 goto err_out4;
Jussi Kivilinna175f9c12008-07-20 00:08:47 -0700831 }
832 sch->stab = stab;
833 }
Patrick McHardy1e904742008-01-22 22:11:17 -0800834 if (tca[TCA_RATE]) {
Jarek Poplawskif6f9b932008-08-27 02:25:17 -0700835 spinlock_t *root_lock;
836
Patrick McHardy23bcf632009-09-09 18:11:23 -0700837 err = -EOPNOTSUPP;
838 if (sch->flags & TCQ_F_MQROOT)
839 goto err_out4;
840
Jarek Poplawskif6f9b932008-08-27 02:25:17 -0700841 if ((sch->parent != TC_H_ROOT) &&
Patrick McHardy23bcf632009-09-09 18:11:23 -0700842 !(sch->flags & TCQ_F_INGRESS) &&
843 (!p || !(p->flags & TCQ_F_MQROOT)))
Jarek Poplawskif6f9b932008-08-27 02:25:17 -0700844 root_lock = qdisc_root_sleeping_lock(sch);
845 else
846 root_lock = qdisc_lock(sch);
847
Thomas Graf023e09a2005-07-05 14:15:53 -0700848 err = gen_new_estimator(&sch->bstats, &sch->rate_est,
Jarek Poplawskif6f9b932008-08-27 02:25:17 -0700849 root_lock, tca[TCA_RATE]);
Patrick McHardy23bcf632009-09-09 18:11:23 -0700850 if (err)
851 goto err_out4;
Thomas Graf023e09a2005-07-05 14:15:53 -0700852 }
Jarek Poplawskif6e0b232008-08-22 03:24:05 -0700853
854 qdisc_list_add(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 return sch;
857 }
858err_out3:
859 dev_put(dev);
Thomas Graf3d54b822005-07-05 14:15:09 -0700860 kfree((char *) sch - sch->padded);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861err_out2:
862 module_put(ops->owner);
863err_out:
864 *errp = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 return NULL;
Patrick McHardy23bcf632009-09-09 18:11:23 -0700866
867err_out4:
868 /*
869 * Any broken qdiscs that would require a ops->reset() here?
870 * The qdisc was never in action so it shouldn't be necessary.
871 */
Jarek Poplawski7c64b9f2009-09-15 23:42:05 -0700872 qdisc_put_stab(sch->stab);
Patrick McHardy23bcf632009-09-09 18:11:23 -0700873 if (ops->destroy)
874 ops->destroy(sch);
875 goto err_out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876}
877
Patrick McHardy1e904742008-01-22 22:11:17 -0800878static int qdisc_change(struct Qdisc *sch, struct nlattr **tca)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879{
Jussi Kivilinna175f9c12008-07-20 00:08:47 -0700880 struct qdisc_size_table *stab = NULL;
881 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Jussi Kivilinna175f9c12008-07-20 00:08:47 -0700883 if (tca[TCA_OPTIONS]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 if (sch->ops->change == NULL)
885 return -EINVAL;
Patrick McHardy1e904742008-01-22 22:11:17 -0800886 err = sch->ops->change(sch, tca[TCA_OPTIONS]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 if (err)
888 return err;
889 }
Jussi Kivilinna175f9c12008-07-20 00:08:47 -0700890
891 if (tca[TCA_STAB]) {
892 stab = qdisc_get_stab(tca[TCA_STAB]);
893 if (IS_ERR(stab))
894 return PTR_ERR(stab);
895 }
896
897 qdisc_put_stab(sch->stab);
898 sch->stab = stab;
899
Patrick McHardy23bcf632009-09-09 18:11:23 -0700900 if (tca[TCA_RATE]) {
Stephen Hemminger71bcb092008-11-25 21:13:31 -0800901 /* NB: ignores errors from replace_estimator
902 because change can't be undone. */
Patrick McHardy23bcf632009-09-09 18:11:23 -0700903 if (sch->flags & TCQ_F_MQROOT)
904 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 gen_replace_estimator(&sch->bstats, &sch->rate_est,
Stephen Hemminger71bcb092008-11-25 21:13:31 -0800906 qdisc_root_sleeping_lock(sch),
907 tca[TCA_RATE]);
Patrick McHardy23bcf632009-09-09 18:11:23 -0700908 }
909out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 return 0;
911}
912
913struct check_loop_arg
914{
915 struct qdisc_walker w;
916 struct Qdisc *p;
917 int depth;
918};
919
920static int check_loop_fn(struct Qdisc *q, unsigned long cl, struct qdisc_walker *w);
921
922static int check_loop(struct Qdisc *q, struct Qdisc *p, int depth)
923{
924 struct check_loop_arg arg;
925
926 if (q->ops->cl_ops == NULL)
927 return 0;
928
929 arg.w.stop = arg.w.skip = arg.w.count = 0;
930 arg.w.fn = check_loop_fn;
931 arg.depth = depth;
932 arg.p = p;
933 q->ops->cl_ops->walk(q, &arg.w);
934 return arg.w.stop ? -ELOOP : 0;
935}
936
937static int
938check_loop_fn(struct Qdisc *q, unsigned long cl, struct qdisc_walker *w)
939{
940 struct Qdisc *leaf;
Eric Dumazet20fea082007-11-14 01:44:41 -0800941 const struct Qdisc_class_ops *cops = q->ops->cl_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 struct check_loop_arg *arg = (struct check_loop_arg *)w;
943
944 leaf = cops->leaf(q, cl);
945 if (leaf) {
946 if (leaf == arg->p || arg->depth > 7)
947 return -ELOOP;
948 return check_loop(leaf, arg->p, arg->depth + 1);
949 }
950 return 0;
951}
952
953/*
954 * Delete/get qdisc.
955 */
956
957static int tc_get_qdisc(struct sk_buff *skb, struct nlmsghdr *n, void *arg)
958{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900959 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 struct tcmsg *tcm = NLMSG_DATA(n);
Patrick McHardy1e904742008-01-22 22:11:17 -0800961 struct nlattr *tca[TCA_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 struct net_device *dev;
963 u32 clid = tcm->tcm_parent;
964 struct Qdisc *q = NULL;
965 struct Qdisc *p = NULL;
966 int err;
967
Tom Goff7316ae82010-03-19 15:40:13 +0000968 if ((dev = __dev_get_by_index(net, tcm->tcm_ifindex)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 return -ENODEV;
970
Patrick McHardy1e904742008-01-22 22:11:17 -0800971 err = nlmsg_parse(n, sizeof(*tcm), tca, TCA_MAX, NULL);
972 if (err < 0)
973 return err;
974
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 if (clid) {
976 if (clid != TC_H_ROOT) {
977 if (TC_H_MAJ(clid) != TC_H_MAJ(TC_H_INGRESS)) {
978 if ((p = qdisc_lookup(dev, TC_H_MAJ(clid))) == NULL)
979 return -ENOENT;
980 q = qdisc_leaf(p, clid);
981 } else { /* ingress */
David S. Miller8123b422008-08-08 23:23:39 -0700982 q = dev->rx_queue.qdisc_sleeping;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900983 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 } else {
Patrick McHardyaf356af2009-09-04 06:41:18 +0000985 q = dev->qdisc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 }
987 if (!q)
988 return -ENOENT;
989
990 if (tcm->tcm_handle && q->handle != tcm->tcm_handle)
991 return -EINVAL;
992 } else {
993 if ((q = qdisc_lookup(dev, tcm->tcm_handle)) == NULL)
994 return -ENOENT;
995 }
996
Patrick McHardy1e904742008-01-22 22:11:17 -0800997 if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], q->ops->id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 return -EINVAL;
999
1000 if (n->nlmsg_type == RTM_DELQDISC) {
1001 if (!clid)
1002 return -EINVAL;
1003 if (q->handle == 0)
1004 return -ENOENT;
David S. Miller99194cf2008-07-17 04:54:10 -07001005 if ((err = qdisc_graft(dev, p, skb, n, clid, NULL, q)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 } else {
Tom Goff7316ae82010-03-19 15:40:13 +00001008 qdisc_notify(net, skb, n, clid, NULL, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 }
1010 return 0;
1011}
1012
1013/*
1014 Create/change qdisc.
1015 */
1016
1017static int tc_modify_qdisc(struct sk_buff *skb, struct nlmsghdr *n, void *arg)
1018{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001019 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 struct tcmsg *tcm;
Patrick McHardy1e904742008-01-22 22:11:17 -08001021 struct nlattr *tca[TCA_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 struct net_device *dev;
1023 u32 clid;
1024 struct Qdisc *q, *p;
1025 int err;
1026
1027replay:
1028 /* Reinit, just in case something touches this. */
1029 tcm = NLMSG_DATA(n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 clid = tcm->tcm_parent;
1031 q = p = NULL;
1032
Tom Goff7316ae82010-03-19 15:40:13 +00001033 if ((dev = __dev_get_by_index(net, tcm->tcm_ifindex)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 return -ENODEV;
1035
Patrick McHardy1e904742008-01-22 22:11:17 -08001036 err = nlmsg_parse(n, sizeof(*tcm), tca, TCA_MAX, NULL);
1037 if (err < 0)
1038 return err;
1039
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 if (clid) {
1041 if (clid != TC_H_ROOT) {
1042 if (clid != TC_H_INGRESS) {
1043 if ((p = qdisc_lookup(dev, TC_H_MAJ(clid))) == NULL)
1044 return -ENOENT;
1045 q = qdisc_leaf(p, clid);
1046 } else { /*ingress */
David S. Miller8123b422008-08-08 23:23:39 -07001047 q = dev->rx_queue.qdisc_sleeping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 }
1049 } else {
Patrick McHardyaf356af2009-09-04 06:41:18 +00001050 q = dev->qdisc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 }
1052
1053 /* It may be default qdisc, ignore it */
1054 if (q && q->handle == 0)
1055 q = NULL;
1056
1057 if (!q || !tcm->tcm_handle || q->handle != tcm->tcm_handle) {
1058 if (tcm->tcm_handle) {
1059 if (q && !(n->nlmsg_flags&NLM_F_REPLACE))
1060 return -EEXIST;
1061 if (TC_H_MIN(tcm->tcm_handle))
1062 return -EINVAL;
1063 if ((q = qdisc_lookup(dev, tcm->tcm_handle)) == NULL)
1064 goto create_n_graft;
1065 if (n->nlmsg_flags&NLM_F_EXCL)
1066 return -EEXIST;
Patrick McHardy1e904742008-01-22 22:11:17 -08001067 if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], q->ops->id))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 return -EINVAL;
1069 if (q == p ||
1070 (p && check_loop(q, p, 0)))
1071 return -ELOOP;
1072 atomic_inc(&q->refcnt);
1073 goto graft;
1074 } else {
1075 if (q == NULL)
1076 goto create_n_graft;
1077
1078 /* This magic test requires explanation.
1079 *
1080 * We know, that some child q is already
1081 * attached to this parent and have choice:
1082 * either to change it or to create/graft new one.
1083 *
1084 * 1. We are allowed to create/graft only
1085 * if CREATE and REPLACE flags are set.
1086 *
1087 * 2. If EXCL is set, requestor wanted to say,
1088 * that qdisc tcm_handle is not expected
1089 * to exist, so that we choose create/graft too.
1090 *
1091 * 3. The last case is when no flags are set.
1092 * Alas, it is sort of hole in API, we
1093 * cannot decide what to do unambiguously.
1094 * For now we select create/graft, if
1095 * user gave KIND, which does not match existing.
1096 */
1097 if ((n->nlmsg_flags&NLM_F_CREATE) &&
1098 (n->nlmsg_flags&NLM_F_REPLACE) &&
1099 ((n->nlmsg_flags&NLM_F_EXCL) ||
Patrick McHardy1e904742008-01-22 22:11:17 -08001100 (tca[TCA_KIND] &&
1101 nla_strcmp(tca[TCA_KIND], q->ops->id))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 goto create_n_graft;
1103 }
1104 }
1105 } else {
1106 if (!tcm->tcm_handle)
1107 return -EINVAL;
1108 q = qdisc_lookup(dev, tcm->tcm_handle);
1109 }
1110
1111 /* Change qdisc parameters */
1112 if (q == NULL)
1113 return -ENOENT;
1114 if (n->nlmsg_flags&NLM_F_EXCL)
1115 return -EEXIST;
Patrick McHardy1e904742008-01-22 22:11:17 -08001116 if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], q->ops->id))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 return -EINVAL;
1118 err = qdisc_change(q, tca);
1119 if (err == 0)
Tom Goff7316ae82010-03-19 15:40:13 +00001120 qdisc_notify(net, skb, n, clid, NULL, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 return err;
1122
1123create_n_graft:
1124 if (!(n->nlmsg_flags&NLM_F_CREATE))
1125 return -ENOENT;
1126 if (clid == TC_H_INGRESS)
Patrick McHardy23bcf632009-09-09 18:11:23 -07001127 q = qdisc_create(dev, &dev->rx_queue, p,
David S. Millerbb949fb2008-07-08 16:55:56 -07001128 tcm->tcm_parent, tcm->tcm_parent,
Patrick McHardyffc8fef2007-07-30 17:11:50 -07001129 tca, &err);
David S. Miller6ec1c692009-09-06 01:58:51 -07001130 else {
Jarek Poplawski926e61b2009-09-15 02:53:07 -07001131 struct netdev_queue *dev_queue;
David S. Miller6ec1c692009-09-06 01:58:51 -07001132
1133 if (p && p->ops->cl_ops && p->ops->cl_ops->select_queue)
Jarek Poplawski926e61b2009-09-15 02:53:07 -07001134 dev_queue = p->ops->cl_ops->select_queue(p, tcm);
1135 else if (p)
1136 dev_queue = p->dev_queue;
1137 else
1138 dev_queue = netdev_get_tx_queue(dev, 0);
David S. Miller6ec1c692009-09-06 01:58:51 -07001139
Jarek Poplawski926e61b2009-09-15 02:53:07 -07001140 q = qdisc_create(dev, dev_queue, p,
David S. Millerbb949fb2008-07-08 16:55:56 -07001141 tcm->tcm_parent, tcm->tcm_handle,
Patrick McHardyffc8fef2007-07-30 17:11:50 -07001142 tca, &err);
David S. Miller6ec1c692009-09-06 01:58:51 -07001143 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 if (q == NULL) {
1145 if (err == -EAGAIN)
1146 goto replay;
1147 return err;
1148 }
1149
1150graft:
Ilpo Järvinene5befbd2008-08-18 22:30:01 -07001151 err = qdisc_graft(dev, p, skb, n, clid, q, NULL);
1152 if (err) {
1153 if (q)
1154 qdisc_destroy(q);
1155 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 }
Ilpo Järvinene5befbd2008-08-18 22:30:01 -07001157
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 return 0;
1159}
1160
1161static int tc_fill_qdisc(struct sk_buff *skb, struct Qdisc *q, u32 clid,
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -07001162 u32 pid, u32 seq, u16 flags, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163{
1164 struct tcmsg *tcm;
1165 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001166 unsigned char *b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 struct gnet_dump d;
1168
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -07001169 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*tcm), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 tcm = NLMSG_DATA(nlh);
1171 tcm->tcm_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001172 tcm->tcm__pad1 = 0;
1173 tcm->tcm__pad2 = 0;
David S. Miller5ce2d482008-07-08 17:06:30 -07001174 tcm->tcm_ifindex = qdisc_dev(q)->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 tcm->tcm_parent = clid;
1176 tcm->tcm_handle = q->handle;
1177 tcm->tcm_info = atomic_read(&q->refcnt);
Patrick McHardy57e1c482008-01-23 20:34:28 -08001178 NLA_PUT_STRING(skb, TCA_KIND, q->ops->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 if (q->ops->dump && q->ops->dump(q, skb) < 0)
Patrick McHardy1e904742008-01-22 22:11:17 -08001180 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 q->qstats.qlen = q->q.qlen;
1182
Jussi Kivilinna175f9c12008-07-20 00:08:47 -07001183 if (q->stab && qdisc_dump_stab(skb, q->stab) < 0)
1184 goto nla_put_failure;
1185
Jarek Poplawski102396a2008-08-29 14:21:52 -07001186 if (gnet_stats_start_copy_compat(skb, TCA_STATS2, TCA_STATS, TCA_XSTATS,
1187 qdisc_root_sleeping_lock(q), &d) < 0)
Patrick McHardy1e904742008-01-22 22:11:17 -08001188 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
1190 if (q->ops->dump_stats && q->ops->dump_stats(q, &d) < 0)
Patrick McHardy1e904742008-01-22 22:11:17 -08001191 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
1193 if (gnet_stats_copy_basic(&d, &q->bstats) < 0 ||
Eric Dumazetd250a5f2009-10-02 10:32:18 +00001194 gnet_stats_copy_rate_est(&d, &q->bstats, &q->rate_est) < 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 gnet_stats_copy_queue(&d, &q->qstats) < 0)
Patrick McHardy1e904742008-01-22 22:11:17 -08001196 goto nla_put_failure;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +09001197
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 if (gnet_stats_finish_copy(&d) < 0)
Patrick McHardy1e904742008-01-22 22:11:17 -08001199 goto nla_put_failure;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +09001200
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001201 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 return skb->len;
1203
1204nlmsg_failure:
Patrick McHardy1e904742008-01-22 22:11:17 -08001205nla_put_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001206 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 return -1;
1208}
1209
Eric Dumazet53b0f082010-05-22 20:37:44 +00001210static bool tc_qdisc_dump_ignore(struct Qdisc *q)
1211{
1212 return (q->flags & TCQ_F_BUILTIN) ? true : false;
1213}
1214
Tom Goff7316ae82010-03-19 15:40:13 +00001215static int qdisc_notify(struct net *net, struct sk_buff *oskb,
1216 struct nlmsghdr *n, u32 clid,
1217 struct Qdisc *old, struct Qdisc *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218{
1219 struct sk_buff *skb;
1220 u32 pid = oskb ? NETLINK_CB(oskb).pid : 0;
1221
1222 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1223 if (!skb)
1224 return -ENOBUFS;
1225
Eric Dumazet53b0f082010-05-22 20:37:44 +00001226 if (old && !tc_qdisc_dump_ignore(old)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 if (tc_fill_qdisc(skb, old, clid, pid, n->nlmsg_seq, 0, RTM_DELQDISC) < 0)
1228 goto err_out;
1229 }
Eric Dumazet53b0f082010-05-22 20:37:44 +00001230 if (new && !tc_qdisc_dump_ignore(new)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 if (tc_fill_qdisc(skb, new, clid, pid, n->nlmsg_seq, old ? NLM_F_REPLACE : 0, RTM_NEWQDISC) < 0)
1232 goto err_out;
1233 }
1234
1235 if (skb->len)
Tom Goff7316ae82010-03-19 15:40:13 +00001236 return rtnetlink_send(skb, net, pid, RTNLGRP_TC, n->nlmsg_flags&NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
1238err_out:
1239 kfree_skb(skb);
1240 return -EINVAL;
1241}
1242
David S. Miller30723672008-07-18 22:50:15 -07001243static int tc_dump_qdisc_root(struct Qdisc *root, struct sk_buff *skb,
1244 struct netlink_callback *cb,
1245 int *q_idx_p, int s_q_idx)
1246{
1247 int ret = 0, q_idx = *q_idx_p;
1248 struct Qdisc *q;
1249
1250 if (!root)
1251 return 0;
1252
1253 q = root;
1254 if (q_idx < s_q_idx) {
1255 q_idx++;
1256 } else {
1257 if (!tc_qdisc_dump_ignore(q) &&
1258 tc_fill_qdisc(skb, q, q->parent, NETLINK_CB(cb->skb).pid,
1259 cb->nlh->nlmsg_seq, NLM_F_MULTI, RTM_NEWQDISC) <= 0)
1260 goto done;
1261 q_idx++;
1262 }
1263 list_for_each_entry(q, &root->list, list) {
1264 if (q_idx < s_q_idx) {
1265 q_idx++;
1266 continue;
1267 }
1268 if (!tc_qdisc_dump_ignore(q) &&
1269 tc_fill_qdisc(skb, q, q->parent, NETLINK_CB(cb->skb).pid,
1270 cb->nlh->nlmsg_seq, NLM_F_MULTI, RTM_NEWQDISC) <= 0)
1271 goto done;
1272 q_idx++;
1273 }
1274
1275out:
1276 *q_idx_p = q_idx;
1277 return ret;
1278done:
1279 ret = -1;
1280 goto out;
1281}
1282
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283static int tc_dump_qdisc(struct sk_buff *skb, struct netlink_callback *cb)
1284{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001285 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 int idx, q_idx;
1287 int s_idx, s_q_idx;
1288 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
1290 s_idx = cb->args[0];
1291 s_q_idx = q_idx = cb->args[1];
stephen hemmingerf1e90162009-11-10 07:54:49 +00001292
1293 rcu_read_lock();
Pavel Emelianov7562f872007-05-03 15:13:45 -07001294 idx = 0;
Tom Goff7316ae82010-03-19 15:40:13 +00001295 for_each_netdev_rcu(net, dev) {
David S. Miller30723672008-07-18 22:50:15 -07001296 struct netdev_queue *dev_queue;
1297
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 if (idx < s_idx)
Pavel Emelianov7562f872007-05-03 15:13:45 -07001299 goto cont;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 if (idx > s_idx)
1301 s_q_idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 q_idx = 0;
David S. Miller30723672008-07-18 22:50:15 -07001303
Patrick McHardyaf356af2009-09-04 06:41:18 +00001304 if (tc_dump_qdisc_root(dev->qdisc, skb, cb, &q_idx, s_q_idx) < 0)
David S. Miller30723672008-07-18 22:50:15 -07001305 goto done;
1306
1307 dev_queue = &dev->rx_queue;
David S. Miller827ebd62008-08-07 20:26:40 -07001308 if (tc_dump_qdisc_root(dev_queue->qdisc_sleeping, skb, cb, &q_idx, s_q_idx) < 0)
David S. Miller30723672008-07-18 22:50:15 -07001309 goto done;
1310
Pavel Emelianov7562f872007-05-03 15:13:45 -07001311cont:
1312 idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 }
1314
1315done:
stephen hemmingerf1e90162009-11-10 07:54:49 +00001316 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
1318 cb->args[0] = idx;
1319 cb->args[1] = q_idx;
1320
1321 return skb->len;
1322}
1323
1324
1325
1326/************************************************
1327 * Traffic classes manipulation. *
1328 ************************************************/
1329
1330
1331
1332static int tc_ctl_tclass(struct sk_buff *skb, struct nlmsghdr *n, void *arg)
1333{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001334 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 struct tcmsg *tcm = NLMSG_DATA(n);
Patrick McHardy1e904742008-01-22 22:11:17 -08001336 struct nlattr *tca[TCA_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 struct net_device *dev;
1338 struct Qdisc *q = NULL;
Eric Dumazet20fea082007-11-14 01:44:41 -08001339 const struct Qdisc_class_ops *cops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 unsigned long cl = 0;
1341 unsigned long new_cl;
1342 u32 pid = tcm->tcm_parent;
1343 u32 clid = tcm->tcm_handle;
1344 u32 qid = TC_H_MAJ(clid);
1345 int err;
1346
Tom Goff7316ae82010-03-19 15:40:13 +00001347 if ((dev = __dev_get_by_index(net, tcm->tcm_ifindex)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 return -ENODEV;
1349
Patrick McHardy1e904742008-01-22 22:11:17 -08001350 err = nlmsg_parse(n, sizeof(*tcm), tca, TCA_MAX, NULL);
1351 if (err < 0)
1352 return err;
1353
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 /*
1355 parent == TC_H_UNSPEC - unspecified parent.
1356 parent == TC_H_ROOT - class is root, which has no parent.
1357 parent == X:0 - parent is root class.
1358 parent == X:Y - parent is a node in hierarchy.
1359 parent == 0:Y - parent is X:Y, where X:0 is qdisc.
1360
1361 handle == 0:0 - generate handle from kernel pool.
1362 handle == 0:Y - class is X:Y, where X:0 is qdisc.
1363 handle == X:Y - clear.
1364 handle == X:0 - root class.
1365 */
1366
1367 /* Step 1. Determine qdisc handle X:0 */
1368
1369 if (pid != TC_H_ROOT) {
1370 u32 qid1 = TC_H_MAJ(pid);
1371
1372 if (qid && qid1) {
1373 /* If both majors are known, they must be identical. */
1374 if (qid != qid1)
1375 return -EINVAL;
1376 } else if (qid1) {
1377 qid = qid1;
1378 } else if (qid == 0)
Patrick McHardyaf356af2009-09-04 06:41:18 +00001379 qid = dev->qdisc->handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
1381 /* Now qid is genuine qdisc handle consistent
1382 both with parent and child.
1383
1384 TC_H_MAJ(pid) still may be unspecified, complete it now.
1385 */
1386 if (pid)
1387 pid = TC_H_MAKE(qid, pid);
1388 } else {
1389 if (qid == 0)
Patrick McHardyaf356af2009-09-04 06:41:18 +00001390 qid = dev->qdisc->handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 }
1392
1393 /* OK. Locate qdisc */
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +09001394 if ((q = qdisc_lookup(dev, qid)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 return -ENOENT;
1396
1397 /* An check that it supports classes */
1398 cops = q->ops->cl_ops;
1399 if (cops == NULL)
1400 return -EINVAL;
1401
1402 /* Now try to get class */
1403 if (clid == 0) {
1404 if (pid == TC_H_ROOT)
1405 clid = qid;
1406 } else
1407 clid = TC_H_MAKE(qid, clid);
1408
1409 if (clid)
1410 cl = cops->get(q, clid);
1411
1412 if (cl == 0) {
1413 err = -ENOENT;
1414 if (n->nlmsg_type != RTM_NEWTCLASS || !(n->nlmsg_flags&NLM_F_CREATE))
1415 goto out;
1416 } else {
1417 switch (n->nlmsg_type) {
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +09001418 case RTM_NEWTCLASS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 err = -EEXIST;
1420 if (n->nlmsg_flags&NLM_F_EXCL)
1421 goto out;
1422 break;
1423 case RTM_DELTCLASS:
Patrick McHardyde6d5cd2009-09-04 06:41:16 +00001424 err = -EOPNOTSUPP;
1425 if (cops->delete)
1426 err = cops->delete(q, cl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 if (err == 0)
Tom Goff7316ae82010-03-19 15:40:13 +00001428 tclass_notify(net, skb, n, q, cl, RTM_DELTCLASS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 goto out;
1430 case RTM_GETTCLASS:
Tom Goff7316ae82010-03-19 15:40:13 +00001431 err = tclass_notify(net, skb, n, q, cl, RTM_NEWTCLASS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 goto out;
1433 default:
1434 err = -EINVAL;
1435 goto out;
1436 }
1437 }
1438
1439 new_cl = cl;
Patrick McHardyde6d5cd2009-09-04 06:41:16 +00001440 err = -EOPNOTSUPP;
1441 if (cops->change)
1442 err = cops->change(q, clid, pid, tca, &new_cl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 if (err == 0)
Tom Goff7316ae82010-03-19 15:40:13 +00001444 tclass_notify(net, skb, n, q, new_cl, RTM_NEWTCLASS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
1446out:
1447 if (cl)
1448 cops->put(q, cl);
1449
1450 return err;
1451}
1452
1453
1454static int tc_fill_tclass(struct sk_buff *skb, struct Qdisc *q,
1455 unsigned long cl,
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -07001456 u32 pid, u32 seq, u16 flags, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457{
1458 struct tcmsg *tcm;
1459 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001460 unsigned char *b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 struct gnet_dump d;
Eric Dumazet20fea082007-11-14 01:44:41 -08001462 const struct Qdisc_class_ops *cl_ops = q->ops->cl_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -07001464 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*tcm), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 tcm = NLMSG_DATA(nlh);
1466 tcm->tcm_family = AF_UNSPEC;
Eric Dumazet16ebb5e2009-09-02 02:40:09 +00001467 tcm->tcm__pad1 = 0;
1468 tcm->tcm__pad2 = 0;
David S. Miller5ce2d482008-07-08 17:06:30 -07001469 tcm->tcm_ifindex = qdisc_dev(q)->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 tcm->tcm_parent = q->handle;
1471 tcm->tcm_handle = q->handle;
1472 tcm->tcm_info = 0;
Patrick McHardy57e1c482008-01-23 20:34:28 -08001473 NLA_PUT_STRING(skb, TCA_KIND, q->ops->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 if (cl_ops->dump && cl_ops->dump(q, cl, skb, tcm) < 0)
Patrick McHardy1e904742008-01-22 22:11:17 -08001475 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
Jarek Poplawski102396a2008-08-29 14:21:52 -07001477 if (gnet_stats_start_copy_compat(skb, TCA_STATS2, TCA_STATS, TCA_XSTATS,
1478 qdisc_root_sleeping_lock(q), &d) < 0)
Patrick McHardy1e904742008-01-22 22:11:17 -08001479 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
1481 if (cl_ops->dump_stats && cl_ops->dump_stats(q, cl, &d) < 0)
Patrick McHardy1e904742008-01-22 22:11:17 -08001482 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
1484 if (gnet_stats_finish_copy(&d) < 0)
Patrick McHardy1e904742008-01-22 22:11:17 -08001485 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001487 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 return skb->len;
1489
1490nlmsg_failure:
Patrick McHardy1e904742008-01-22 22:11:17 -08001491nla_put_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001492 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 return -1;
1494}
1495
Tom Goff7316ae82010-03-19 15:40:13 +00001496static int tclass_notify(struct net *net, struct sk_buff *oskb,
1497 struct nlmsghdr *n, struct Qdisc *q,
1498 unsigned long cl, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499{
1500 struct sk_buff *skb;
1501 u32 pid = oskb ? NETLINK_CB(oskb).pid : 0;
1502
1503 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1504 if (!skb)
1505 return -ENOBUFS;
1506
1507 if (tc_fill_tclass(skb, q, cl, pid, n->nlmsg_seq, 0, event) < 0) {
1508 kfree_skb(skb);
1509 return -EINVAL;
1510 }
1511
Tom Goff7316ae82010-03-19 15:40:13 +00001512 return rtnetlink_send(skb, net, pid, RTNLGRP_TC, n->nlmsg_flags&NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513}
1514
1515struct qdisc_dump_args
1516{
1517 struct qdisc_walker w;
1518 struct sk_buff *skb;
1519 struct netlink_callback *cb;
1520};
1521
1522static int qdisc_class_dump(struct Qdisc *q, unsigned long cl, struct qdisc_walker *arg)
1523{
1524 struct qdisc_dump_args *a = (struct qdisc_dump_args *)arg;
1525
1526 return tc_fill_tclass(a->skb, q, cl, NETLINK_CB(a->cb->skb).pid,
1527 a->cb->nlh->nlmsg_seq, NLM_F_MULTI, RTM_NEWTCLASS);
1528}
1529
David S. Miller30723672008-07-18 22:50:15 -07001530static int tc_dump_tclass_qdisc(struct Qdisc *q, struct sk_buff *skb,
1531 struct tcmsg *tcm, struct netlink_callback *cb,
1532 int *t_p, int s_t)
1533{
1534 struct qdisc_dump_args arg;
1535
1536 if (tc_qdisc_dump_ignore(q) ||
1537 *t_p < s_t || !q->ops->cl_ops ||
1538 (tcm->tcm_parent &&
1539 TC_H_MAJ(tcm->tcm_parent) != q->handle)) {
1540 (*t_p)++;
1541 return 0;
1542 }
1543 if (*t_p > s_t)
1544 memset(&cb->args[1], 0, sizeof(cb->args)-sizeof(cb->args[0]));
1545 arg.w.fn = qdisc_class_dump;
1546 arg.skb = skb;
1547 arg.cb = cb;
1548 arg.w.stop = 0;
1549 arg.w.skip = cb->args[1];
1550 arg.w.count = 0;
1551 q->ops->cl_ops->walk(q, &arg.w);
1552 cb->args[1] = arg.w.count;
1553 if (arg.w.stop)
1554 return -1;
1555 (*t_p)++;
1556 return 0;
1557}
1558
1559static int tc_dump_tclass_root(struct Qdisc *root, struct sk_buff *skb,
1560 struct tcmsg *tcm, struct netlink_callback *cb,
1561 int *t_p, int s_t)
1562{
1563 struct Qdisc *q;
1564
1565 if (!root)
1566 return 0;
1567
1568 if (tc_dump_tclass_qdisc(root, skb, tcm, cb, t_p, s_t) < 0)
1569 return -1;
1570
1571 list_for_each_entry(q, &root->list, list) {
1572 if (tc_dump_tclass_qdisc(q, skb, tcm, cb, t_p, s_t) < 0)
1573 return -1;
1574 }
1575
1576 return 0;
1577}
1578
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579static int tc_dump_tclass(struct sk_buff *skb, struct netlink_callback *cb)
1580{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 struct tcmsg *tcm = (struct tcmsg*)NLMSG_DATA(cb->nlh);
David S. Miller30723672008-07-18 22:50:15 -07001582 struct net *net = sock_net(skb->sk);
1583 struct netdev_queue *dev_queue;
1584 struct net_device *dev;
1585 int t, s_t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
1587 if (cb->nlh->nlmsg_len < NLMSG_LENGTH(sizeof(*tcm)))
1588 return 0;
Tom Goff7316ae82010-03-19 15:40:13 +00001589 if ((dev = dev_get_by_index(net, tcm->tcm_ifindex)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 return 0;
1591
1592 s_t = cb->args[0];
1593 t = 0;
1594
Patrick McHardyaf356af2009-09-04 06:41:18 +00001595 if (tc_dump_tclass_root(dev->qdisc, skb, tcm, cb, &t, s_t) < 0)
David S. Miller30723672008-07-18 22:50:15 -07001596 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
David S. Miller30723672008-07-18 22:50:15 -07001598 dev_queue = &dev->rx_queue;
David S. Miller8123b422008-08-08 23:23:39 -07001599 if (tc_dump_tclass_root(dev_queue->qdisc_sleeping, skb, tcm, cb, &t, s_t) < 0)
David S. Miller30723672008-07-18 22:50:15 -07001600 goto done;
1601
1602done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 cb->args[0] = t;
1604
1605 dev_put(dev);
1606 return skb->len;
1607}
1608
1609/* Main classifier routine: scans classifier chain attached
1610 to this qdisc, (optionally) tests for protocol and asks
1611 specific classifiers.
1612 */
Patrick McHardy73ca4912007-07-15 00:02:31 -07001613int tc_classify_compat(struct sk_buff *skb, struct tcf_proto *tp,
1614 struct tcf_result *res)
1615{
1616 __be16 protocol = skb->protocol;
1617 int err = 0;
1618
1619 for (; tp; tp = tp->next) {
1620 if ((tp->protocol == protocol ||
1621 tp->protocol == htons(ETH_P_ALL)) &&
1622 (err = tp->classify(skb, tp, res)) >= 0) {
1623#ifdef CONFIG_NET_CLS_ACT
1624 if (err != TC_ACT_RECLASSIFY && skb->tc_verd)
1625 skb->tc_verd = SET_TC_VERD(skb->tc_verd, 0);
1626#endif
1627 return err;
1628 }
1629 }
1630 return -1;
1631}
1632EXPORT_SYMBOL(tc_classify_compat);
1633
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634int tc_classify(struct sk_buff *skb, struct tcf_proto *tp,
Patrick McHardy73ca4912007-07-15 00:02:31 -07001635 struct tcf_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636{
1637 int err = 0;
Patrick McHardy73ca4912007-07-15 00:02:31 -07001638 __be16 protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639#ifdef CONFIG_NET_CLS_ACT
1640 struct tcf_proto *otp = tp;
1641reclassify:
1642#endif
1643 protocol = skb->protocol;
1644
Patrick McHardy73ca4912007-07-15 00:02:31 -07001645 err = tc_classify_compat(skb, tp, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646#ifdef CONFIG_NET_CLS_ACT
Patrick McHardy73ca4912007-07-15 00:02:31 -07001647 if (err == TC_ACT_RECLASSIFY) {
1648 u32 verd = G_TC_VERD(skb->tc_verd);
1649 tp = otp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
Patrick McHardy73ca4912007-07-15 00:02:31 -07001651 if (verd++ >= MAX_REC_LOOP) {
stephen hemmingerb60b6592010-05-11 14:24:12 +00001652 if (net_ratelimit())
1653 printk(KERN_NOTICE
1654 "%s: packet reclassify loop"
1655 " rule prio %u protocol %02x\n",
1656 tp->q->ops->id,
1657 tp->prio & 0xffff, ntohs(tp->protocol));
Patrick McHardy73ca4912007-07-15 00:02:31 -07001658 return TC_ACT_SHOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 }
Patrick McHardy73ca4912007-07-15 00:02:31 -07001660 skb->tc_verd = SET_TC_VERD(skb->tc_verd, verd);
1661 goto reclassify;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 }
Patrick McHardy73ca4912007-07-15 00:02:31 -07001663#endif
1664 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665}
Patrick McHardy73ca4912007-07-15 00:02:31 -07001666EXPORT_SYMBOL(tc_classify);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
Patrick McHardya48b5a62007-03-23 11:29:43 -07001668void tcf_destroy(struct tcf_proto *tp)
1669{
1670 tp->ops->destroy(tp);
1671 module_put(tp->ops->owner);
1672 kfree(tp);
1673}
1674
Patrick McHardyff31ab52008-07-01 19:52:38 -07001675void tcf_destroy_chain(struct tcf_proto **fl)
Patrick McHardya48b5a62007-03-23 11:29:43 -07001676{
1677 struct tcf_proto *tp;
1678
Patrick McHardyff31ab52008-07-01 19:52:38 -07001679 while ((tp = *fl) != NULL) {
1680 *fl = tp->next;
Patrick McHardya48b5a62007-03-23 11:29:43 -07001681 tcf_destroy(tp);
1682 }
1683}
1684EXPORT_SYMBOL(tcf_destroy_chain);
1685
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686#ifdef CONFIG_PROC_FS
1687static int psched_show(struct seq_file *seq, void *v)
1688{
Patrick McHardy3c0cfc12007-10-10 16:32:41 -07001689 struct timespec ts;
1690
1691 hrtimer_get_res(CLOCK_MONOTONIC, &ts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 seq_printf(seq, "%08x %08x %08x %08x\n",
Jarek Poplawskica44d6e2009-06-15 02:31:47 -07001693 (u32)NSEC_PER_USEC, (u32)PSCHED_TICKS2NS(1),
Patrick McHardy514bca32007-03-16 12:34:52 -07001694 1000000,
Patrick McHardy3c0cfc12007-10-10 16:32:41 -07001695 (u32)NSEC_PER_SEC/(u32)ktime_to_ns(timespec_to_ktime(ts)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
1697 return 0;
1698}
1699
1700static int psched_open(struct inode *inode, struct file *file)
1701{
Tom Goff7e5ab152010-03-30 19:44:56 -07001702 return single_open(file, psched_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703}
1704
Arjan van de Venda7071d2007-02-12 00:55:36 -08001705static const struct file_operations psched_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 .owner = THIS_MODULE,
1707 .open = psched_open,
1708 .read = seq_read,
1709 .llseek = seq_lseek,
1710 .release = single_release,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +09001711};
Tom Goff7316ae82010-03-19 15:40:13 +00001712
1713static int __net_init psched_net_init(struct net *net)
1714{
1715 struct proc_dir_entry *e;
1716
1717 e = proc_net_fops_create(net, "psched", 0, &psched_fops);
1718 if (e == NULL)
1719 return -ENOMEM;
1720
1721 return 0;
1722}
1723
1724static void __net_exit psched_net_exit(struct net *net)
1725{
1726 proc_net_remove(net, "psched");
Tom Goff7316ae82010-03-19 15:40:13 +00001727}
1728#else
1729static int __net_init psched_net_init(struct net *net)
1730{
1731 return 0;
1732}
1733
1734static void __net_exit psched_net_exit(struct net *net)
1735{
1736}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737#endif
1738
Tom Goff7316ae82010-03-19 15:40:13 +00001739static struct pernet_operations psched_net_ops = {
1740 .init = psched_net_init,
1741 .exit = psched_net_exit,
1742};
1743
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744static int __init pktsched_init(void)
1745{
Tom Goff7316ae82010-03-19 15:40:13 +00001746 int err;
1747
1748 err = register_pernet_subsys(&psched_net_ops);
1749 if (err) {
1750 printk(KERN_ERR "pktsched_init: "
1751 "cannot initialize per netns operations\n");
1752 return err;
1753 }
1754
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 register_qdisc(&pfifo_qdisc_ops);
1756 register_qdisc(&bfifo_qdisc_ops);
Hagen Paul Pfeifer57dbb2d2010-01-24 12:30:59 +00001757 register_qdisc(&pfifo_head_drop_qdisc_ops);
David S. Miller6ec1c692009-09-06 01:58:51 -07001758 register_qdisc(&mq_qdisc_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759
Thomas Grafbe577dd2007-03-22 11:55:50 -07001760 rtnl_register(PF_UNSPEC, RTM_NEWQDISC, tc_modify_qdisc, NULL);
1761 rtnl_register(PF_UNSPEC, RTM_DELQDISC, tc_get_qdisc, NULL);
1762 rtnl_register(PF_UNSPEC, RTM_GETQDISC, tc_get_qdisc, tc_dump_qdisc);
1763 rtnl_register(PF_UNSPEC, RTM_NEWTCLASS, tc_ctl_tclass, NULL);
1764 rtnl_register(PF_UNSPEC, RTM_DELTCLASS, tc_ctl_tclass, NULL);
1765 rtnl_register(PF_UNSPEC, RTM_GETTCLASS, tc_ctl_tclass, tc_dump_tclass);
1766
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 return 0;
1768}
1769
1770subsys_initcall(pktsched_init);