blob: 4003c280b69f997f3da0e4cf8d134f13c58dfab9 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020031#include <net/net_namespace.h>
Denis V. Lunevb8542722007-12-01 00:21:31 +110032#include <net/sock.h>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070033#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <net/pkt_sched.h>
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036static int qdisc_notify(struct sk_buff *oskb, struct nlmsghdr *n, u32 clid,
37 struct Qdisc *old, struct Qdisc *new);
38static int tclass_notify(struct sk_buff *oskb, struct nlmsghdr *n,
39 struct Qdisc *q, unsigned long cl, int event);
40
41/*
42
43 Short review.
44 -------------
45
46 This file consists of two interrelated parts:
47
48 1. queueing disciplines manager frontend.
49 2. traffic classes manager frontend.
50
51 Generally, queueing discipline ("qdisc") is a black box,
52 which is able to enqueue packets and to dequeue them (when
53 device is ready to send something) in order and at times
54 determined by algorithm hidden in it.
55
56 qdisc's are divided to two categories:
57 - "queues", which have no internal structure visible from outside.
58 - "schedulers", which split all the packets to "traffic classes",
59 using "packet classifiers" (look at cls_api.c)
60
61 In turn, classes may have child qdiscs (as rule, queues)
62 attached to them etc. etc. etc.
63
64 The goal of the routines in this file is to translate
65 information supplied by user in the form of handles
66 to more intelligible for kernel form, to make some sanity
67 checks and part of work, which is common to all qdiscs
68 and to provide rtnetlink notifications.
69
70 All real intelligent work is done inside qdisc modules.
71
72
73
74 Every discipline has two major routines: enqueue and dequeue.
75
76 ---dequeue
77
78 dequeue usually returns a skb to send. It is allowed to return NULL,
79 but it does not mean that queue is empty, it just means that
80 discipline does not want to send anything this time.
81 Queue is really empty if q->q.qlen == 0.
82 For complicated disciplines with multiple queues q->q is not
83 real packet queue, but however q->q.qlen must be valid.
84
85 ---enqueue
86
87 enqueue returns 0, if packet was enqueued successfully.
88 If packet (this one or another one) was dropped, it returns
89 not zero error code.
90 NET_XMIT_DROP - this packet dropped
91 Expected action: do not backoff, but wait until queue will clear.
92 NET_XMIT_CN - probably this packet enqueued, but another one dropped.
93 Expected action: backoff or ignore
94 NET_XMIT_POLICED - dropped by police.
95 Expected action: backoff or error to real-time apps.
96
97 Auxiliary routines:
98
99 ---requeue
100
101 requeues once dequeued packet. It is used for non-standard or
David S. Millere65d22e2008-07-08 16:46:01 -0700102 just buggy devices, which can defer output even if netif_queue_stopped()=0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104 ---reset
105
106 returns qdisc to initial state: purge all buffers, clear all
107 timers, counters (except for statistics) etc.
108
109 ---init
110
111 initializes newly created qdisc.
112
113 ---destroy
114
115 destroys resources allocated by init and during lifetime of qdisc.
116
117 ---change
118
119 changes qdisc parameters.
120 */
121
122/* Protects list of registered TC modules. It is pure SMP lock. */
123static DEFINE_RWLOCK(qdisc_mod_lock);
124
125
126/************************************************
127 * Queueing disciplines manipulation. *
128 ************************************************/
129
130
131/* The list of all installed queueing disciplines. */
132
133static struct Qdisc_ops *qdisc_base;
134
135/* Register/uregister queueing discipline */
136
137int register_qdisc(struct Qdisc_ops *qops)
138{
139 struct Qdisc_ops *q, **qp;
140 int rc = -EEXIST;
141
142 write_lock(&qdisc_mod_lock);
143 for (qp = &qdisc_base; (q = *qp) != NULL; qp = &q->next)
144 if (!strcmp(qops->id, q->id))
145 goto out;
146
147 if (qops->enqueue == NULL)
148 qops->enqueue = noop_qdisc_ops.enqueue;
149 if (qops->requeue == NULL)
150 qops->requeue = noop_qdisc_ops.requeue;
151 if (qops->dequeue == NULL)
152 qops->dequeue = noop_qdisc_ops.dequeue;
153
154 qops->next = NULL;
155 *qp = qops;
156 rc = 0;
157out:
158 write_unlock(&qdisc_mod_lock);
159 return rc;
160}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800161EXPORT_SYMBOL(register_qdisc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163int unregister_qdisc(struct Qdisc_ops *qops)
164{
165 struct Qdisc_ops *q, **qp;
166 int err = -ENOENT;
167
168 write_lock(&qdisc_mod_lock);
169 for (qp = &qdisc_base; (q=*qp)!=NULL; qp = &q->next)
170 if (q == qops)
171 break;
172 if (q) {
173 *qp = q->next;
174 q->next = NULL;
175 err = 0;
176 }
177 write_unlock(&qdisc_mod_lock);
178 return err;
179}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800180EXPORT_SYMBOL(unregister_qdisc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182/* We know handle. Find qdisc among all qdisc's attached to device
183 (root qdisc, all its children, children of children etc.)
184 */
185
Patrick McHardy0463d4a2007-04-16 17:02:10 -0700186struct Qdisc *qdisc_lookup(struct net_device *dev, u32 handle)
Patrick McHardy43effa12006-11-29 17:35:48 -0800187{
David S. Millerb0e1e642008-07-08 17:42:10 -0700188 struct netdev_queue *dev_queue = &dev->tx_queue;
Patrick McHardy43effa12006-11-29 17:35:48 -0800189 struct Qdisc *q;
190
David S. Millerb0e1e642008-07-08 17:42:10 -0700191 list_for_each_entry(q, &dev_queue->qdisc_list, list) {
Patrick McHardy43effa12006-11-29 17:35:48 -0800192 if (q->handle == handle)
193 return q;
194 }
195 return NULL;
196}
197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198static struct Qdisc *qdisc_leaf(struct Qdisc *p, u32 classid)
199{
200 unsigned long cl;
201 struct Qdisc *leaf;
Eric Dumazet20fea082007-11-14 01:44:41 -0800202 const struct Qdisc_class_ops *cops = p->ops->cl_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204 if (cops == NULL)
205 return NULL;
206 cl = cops->get(p, classid);
207
208 if (cl == 0)
209 return NULL;
210 leaf = cops->leaf(p, cl);
211 cops->put(p, cl);
212 return leaf;
213}
214
215/* Find queueing discipline by name */
216
Patrick McHardy1e904742008-01-22 22:11:17 -0800217static struct Qdisc_ops *qdisc_lookup_ops(struct nlattr *kind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
219 struct Qdisc_ops *q = NULL;
220
221 if (kind) {
222 read_lock(&qdisc_mod_lock);
223 for (q = qdisc_base; q; q = q->next) {
Patrick McHardy1e904742008-01-22 22:11:17 -0800224 if (nla_strcmp(kind, q->id) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 if (!try_module_get(q->owner))
226 q = NULL;
227 break;
228 }
229 }
230 read_unlock(&qdisc_mod_lock);
231 }
232 return q;
233}
234
235static struct qdisc_rate_table *qdisc_rtab_list;
236
Patrick McHardy1e904742008-01-22 22:11:17 -0800237struct qdisc_rate_table *qdisc_get_rtab(struct tc_ratespec *r, struct nlattr *tab)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
239 struct qdisc_rate_table *rtab;
240
241 for (rtab = qdisc_rtab_list; rtab; rtab = rtab->next) {
242 if (memcmp(&rtab->rate, r, sizeof(struct tc_ratespec)) == 0) {
243 rtab->refcnt++;
244 return rtab;
245 }
246 }
247
Patrick McHardy5feb5e12008-01-23 20:35:19 -0800248 if (tab == NULL || r->rate == 0 || r->cell_log == 0 ||
249 nla_len(tab) != TC_RTAB_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 return NULL;
251
252 rtab = kmalloc(sizeof(*rtab), GFP_KERNEL);
253 if (rtab) {
254 rtab->rate = *r;
255 rtab->refcnt = 1;
Patrick McHardy1e904742008-01-22 22:11:17 -0800256 memcpy(rtab->data, nla_data(tab), 1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 rtab->next = qdisc_rtab_list;
258 qdisc_rtab_list = rtab;
259 }
260 return rtab;
261}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800262EXPORT_SYMBOL(qdisc_get_rtab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264void qdisc_put_rtab(struct qdisc_rate_table *tab)
265{
266 struct qdisc_rate_table *rtab, **rtabp;
267
268 if (!tab || --tab->refcnt)
269 return;
270
271 for (rtabp = &qdisc_rtab_list; (rtab=*rtabp) != NULL; rtabp = &rtab->next) {
272 if (rtab == tab) {
273 *rtabp = rtab->next;
274 kfree(rtab);
275 return;
276 }
277 }
278}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800279EXPORT_SYMBOL(qdisc_put_rtab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Patrick McHardy41794772007-03-16 01:19:15 -0700281static enum hrtimer_restart qdisc_watchdog(struct hrtimer *timer)
282{
283 struct qdisc_watchdog *wd = container_of(timer, struct qdisc_watchdog,
284 timer);
David S. Miller5ce2d482008-07-08 17:06:30 -0700285 struct net_device *dev = qdisc_dev(wd->qdisc);
Patrick McHardy41794772007-03-16 01:19:15 -0700286
287 wd->qdisc->flags &= ~TCQ_F_THROTTLED;
Stephen Hemminger11274e52007-03-22 12:17:42 -0700288 smp_wmb();
Patrick McHardy0621ed22007-07-14 20:49:26 -0700289 netif_schedule(dev);
Stephen Hemminger19365022007-03-22 12:18:35 -0700290
Patrick McHardy41794772007-03-16 01:19:15 -0700291 return HRTIMER_NORESTART;
292}
293
294void qdisc_watchdog_init(struct qdisc_watchdog *wd, struct Qdisc *qdisc)
295{
296 hrtimer_init(&wd->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
297 wd->timer.function = qdisc_watchdog;
298 wd->qdisc = qdisc;
299}
300EXPORT_SYMBOL(qdisc_watchdog_init);
301
302void qdisc_watchdog_schedule(struct qdisc_watchdog *wd, psched_time_t expires)
303{
304 ktime_t time;
305
306 wd->qdisc->flags |= TCQ_F_THROTTLED;
307 time = ktime_set(0, 0);
308 time = ktime_add_ns(time, PSCHED_US2NS(expires));
309 hrtimer_start(&wd->timer, time, HRTIMER_MODE_ABS);
310}
311EXPORT_SYMBOL(qdisc_watchdog_schedule);
312
313void qdisc_watchdog_cancel(struct qdisc_watchdog *wd)
314{
315 hrtimer_cancel(&wd->timer);
316 wd->qdisc->flags &= ~TCQ_F_THROTTLED;
317}
318EXPORT_SYMBOL(qdisc_watchdog_cancel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Patrick McHardy6fe1c7a2008-07-05 23:21:31 -0700320struct hlist_head *qdisc_class_hash_alloc(unsigned int n)
321{
322 unsigned int size = n * sizeof(struct hlist_head), i;
323 struct hlist_head *h;
324
325 if (size <= PAGE_SIZE)
326 h = kmalloc(size, GFP_KERNEL);
327 else
328 h = (struct hlist_head *)
329 __get_free_pages(GFP_KERNEL, get_order(size));
330
331 if (h != NULL) {
332 for (i = 0; i < n; i++)
333 INIT_HLIST_HEAD(&h[i]);
334 }
335 return h;
336}
337
338static void qdisc_class_hash_free(struct hlist_head *h, unsigned int n)
339{
340 unsigned int size = n * sizeof(struct hlist_head);
341
342 if (size <= PAGE_SIZE)
343 kfree(h);
344 else
345 free_pages((unsigned long)h, get_order(size));
346}
347
348void qdisc_class_hash_grow(struct Qdisc *sch, struct Qdisc_class_hash *clhash)
349{
350 struct Qdisc_class_common *cl;
351 struct hlist_node *n, *next;
352 struct hlist_head *nhash, *ohash;
353 unsigned int nsize, nmask, osize;
354 unsigned int i, h;
355
356 /* Rehash when load factor exceeds 0.75 */
357 if (clhash->hashelems * 4 <= clhash->hashsize * 3)
358 return;
359 nsize = clhash->hashsize * 2;
360 nmask = nsize - 1;
361 nhash = qdisc_class_hash_alloc(nsize);
362 if (nhash == NULL)
363 return;
364
365 ohash = clhash->hash;
366 osize = clhash->hashsize;
367
368 sch_tree_lock(sch);
369 for (i = 0; i < osize; i++) {
370 hlist_for_each_entry_safe(cl, n, next, &ohash[i], hnode) {
371 h = qdisc_class_hash(cl->classid, nmask);
372 hlist_add_head(&cl->hnode, &nhash[h]);
373 }
374 }
375 clhash->hash = nhash;
376 clhash->hashsize = nsize;
377 clhash->hashmask = nmask;
378 sch_tree_unlock(sch);
379
380 qdisc_class_hash_free(ohash, osize);
381}
382EXPORT_SYMBOL(qdisc_class_hash_grow);
383
384int qdisc_class_hash_init(struct Qdisc_class_hash *clhash)
385{
386 unsigned int size = 4;
387
388 clhash->hash = qdisc_class_hash_alloc(size);
389 if (clhash->hash == NULL)
390 return -ENOMEM;
391 clhash->hashsize = size;
392 clhash->hashmask = size - 1;
393 clhash->hashelems = 0;
394 return 0;
395}
396EXPORT_SYMBOL(qdisc_class_hash_init);
397
398void qdisc_class_hash_destroy(struct Qdisc_class_hash *clhash)
399{
400 qdisc_class_hash_free(clhash->hash, clhash->hashsize);
401}
402EXPORT_SYMBOL(qdisc_class_hash_destroy);
403
404void qdisc_class_hash_insert(struct Qdisc_class_hash *clhash,
405 struct Qdisc_class_common *cl)
406{
407 unsigned int h;
408
409 INIT_HLIST_NODE(&cl->hnode);
410 h = qdisc_class_hash(cl->classid, clhash->hashmask);
411 hlist_add_head(&cl->hnode, &clhash->hash[h]);
412 clhash->hashelems++;
413}
414EXPORT_SYMBOL(qdisc_class_hash_insert);
415
416void qdisc_class_hash_remove(struct Qdisc_class_hash *clhash,
417 struct Qdisc_class_common *cl)
418{
419 hlist_del(&cl->hnode);
420 clhash->hashelems--;
421}
422EXPORT_SYMBOL(qdisc_class_hash_remove);
423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424/* Allocate an unique handle from space managed by kernel */
425
426static u32 qdisc_alloc_handle(struct net_device *dev)
427{
428 int i = 0x10000;
429 static u32 autohandle = TC_H_MAKE(0x80000000U, 0);
430
431 do {
432 autohandle += TC_H_MAKE(0x10000U, 0);
433 if (autohandle == TC_H_MAKE(TC_H_ROOT, 0))
434 autohandle = TC_H_MAKE(0x80000000U, 0);
435 } while (qdisc_lookup(dev, autohandle) && --i > 0);
436
437 return i>0 ? autohandle : 0;
438}
439
440/* Attach toplevel qdisc to device dev */
441
442static struct Qdisc *
443dev_graft_qdisc(struct net_device *dev, struct Qdisc *qdisc)
444{
David S. Millerb0e1e642008-07-08 17:42:10 -0700445 struct netdev_queue *dev_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 struct Qdisc *oqdisc;
447
448 if (dev->flags & IFF_UP)
449 dev_deactivate(dev);
450
451 qdisc_lock_tree(dev);
452 if (qdisc && qdisc->flags&TCQ_F_INGRESS) {
David S. Miller816f3252008-07-08 22:49:00 -0700453 dev_queue = &dev->rx_queue;
454 oqdisc = dev_queue->qdisc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 /* Prune old scheduler */
456 if (oqdisc && atomic_read(&oqdisc->refcnt) <= 1) {
457 /* delete */
458 qdisc_reset(oqdisc);
David S. Miller816f3252008-07-08 22:49:00 -0700459 dev_queue->qdisc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 } else { /* new */
David S. Miller816f3252008-07-08 22:49:00 -0700461 dev_queue->qdisc = qdisc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 }
463
464 } else {
David S. Millerb0e1e642008-07-08 17:42:10 -0700465 dev_queue = &dev->tx_queue;
466 oqdisc = dev_queue->qdisc_sleeping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
468 /* Prune old scheduler */
469 if (oqdisc && atomic_read(&oqdisc->refcnt) <= 1)
470 qdisc_reset(oqdisc);
471
472 /* ... and graft new one */
473 if (qdisc == NULL)
474 qdisc = &noop_qdisc;
David S. Millerb0e1e642008-07-08 17:42:10 -0700475 dev_queue->qdisc_sleeping = qdisc;
476 dev_queue->qdisc = &noop_qdisc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 }
478
479 qdisc_unlock_tree(dev);
480
481 if (dev->flags & IFF_UP)
482 dev_activate(dev);
483
484 return oqdisc;
485}
486
Patrick McHardy43effa12006-11-29 17:35:48 -0800487void qdisc_tree_decrease_qlen(struct Qdisc *sch, unsigned int n)
488{
Eric Dumazet20fea082007-11-14 01:44:41 -0800489 const struct Qdisc_class_ops *cops;
Patrick McHardy43effa12006-11-29 17:35:48 -0800490 unsigned long cl;
491 u32 parentid;
492
493 if (n == 0)
494 return;
495 while ((parentid = sch->parent)) {
Jarek Poplawski066a3b52008-04-14 15:10:42 -0700496 if (TC_H_MAJ(parentid) == TC_H_MAJ(TC_H_INGRESS))
497 return;
498
David S. Miller5ce2d482008-07-08 17:06:30 -0700499 sch = qdisc_lookup(qdisc_dev(sch), TC_H_MAJ(parentid));
Patrick McHardyffc8fef2007-07-30 17:11:50 -0700500 if (sch == NULL) {
501 WARN_ON(parentid != TC_H_ROOT);
502 return;
503 }
Patrick McHardy43effa12006-11-29 17:35:48 -0800504 cops = sch->ops->cl_ops;
505 if (cops->qlen_notify) {
506 cl = cops->get(sch, parentid);
507 cops->qlen_notify(sch, cl);
508 cops->put(sch, cl);
509 }
510 sch->q.qlen -= n;
511 }
512}
513EXPORT_SYMBOL(qdisc_tree_decrease_qlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515/* Graft qdisc "new" to class "classid" of qdisc "parent" or
516 to device "dev".
517
518 Old qdisc is not destroyed but returned in *old.
519 */
520
521static int qdisc_graft(struct net_device *dev, struct Qdisc *parent,
522 u32 classid,
523 struct Qdisc *new, struct Qdisc **old)
524{
525 int err = 0;
526 struct Qdisc *q = *old;
527
528
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900529 if (parent == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 if (q && q->flags&TCQ_F_INGRESS) {
531 *old = dev_graft_qdisc(dev, q);
532 } else {
533 *old = dev_graft_qdisc(dev, new);
534 }
535 } else {
Eric Dumazet20fea082007-11-14 01:44:41 -0800536 const struct Qdisc_class_ops *cops = parent->ops->cl_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538 err = -EINVAL;
539
540 if (cops) {
541 unsigned long cl = cops->get(parent, classid);
542 if (cl) {
543 err = cops->graft(parent, cl, new, old);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 cops->put(parent, cl);
545 }
546 }
547 }
548 return err;
549}
550
551/*
552 Allocate and initialize new qdisc.
553
554 Parameters are passed via opt.
555 */
556
557static struct Qdisc *
David S. Millerbb949fb2008-07-08 16:55:56 -0700558qdisc_create(struct net_device *dev, struct netdev_queue *dev_queue,
559 u32 parent, u32 handle, struct nlattr **tca, int *errp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
561 int err;
Patrick McHardy1e904742008-01-22 22:11:17 -0800562 struct nlattr *kind = tca[TCA_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 struct Qdisc *sch;
564 struct Qdisc_ops *ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566 ops = qdisc_lookup_ops(kind);
567#ifdef CONFIG_KMOD
568 if (ops == NULL && kind != NULL) {
569 char name[IFNAMSIZ];
Patrick McHardy1e904742008-01-22 22:11:17 -0800570 if (nla_strlcpy(name, kind, IFNAMSIZ) < IFNAMSIZ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 /* We dropped the RTNL semaphore in order to
572 * perform the module load. So, even if we
573 * succeeded in loading the module we have to
574 * tell the caller to replay the request. We
575 * indicate this using -EAGAIN.
576 * We replay the request because the device may
577 * go away in the mean time.
578 */
579 rtnl_unlock();
580 request_module("sch_%s", name);
581 rtnl_lock();
582 ops = qdisc_lookup_ops(kind);
583 if (ops != NULL) {
584 /* We will try again qdisc_lookup_ops,
585 * so don't keep a reference.
586 */
587 module_put(ops->owner);
588 err = -EAGAIN;
589 goto err_out;
590 }
591 }
592 }
593#endif
594
Jamal Hadi Salimb9e2cc02006-08-03 16:36:51 -0700595 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 if (ops == NULL)
597 goto err_out;
598
David S. Miller5ce2d482008-07-08 17:06:30 -0700599 sch = qdisc_alloc(dev_queue, ops);
Thomas Graf3d54b822005-07-05 14:15:09 -0700600 if (IS_ERR(sch)) {
601 err = PTR_ERR(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 goto err_out2;
Thomas Graf3d54b822005-07-05 14:15:09 -0700603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Patrick McHardyffc8fef2007-07-30 17:11:50 -0700605 sch->parent = parent;
606
David S. Miller555353c2008-07-08 17:33:13 -0700607 sch->stats_lock = &dev_queue->lock;
Thomas Graf3d54b822005-07-05 14:15:09 -0700608 if (handle == TC_H_INGRESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 sch->flags |= TCQ_F_INGRESS;
Thomas Graf3d54b822005-07-05 14:15:09 -0700610 handle = TC_H_MAKE(TC_H_INGRESS, 0);
Patrick McHardyfd44de72007-04-16 17:07:08 -0700611 } else {
Patrick McHardyfd44de72007-04-16 17:07:08 -0700612 if (handle == 0) {
613 handle = qdisc_alloc_handle(dev);
614 err = -ENOMEM;
615 if (handle == 0)
616 goto err_out3;
617 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 }
619
Thomas Graf3d54b822005-07-05 14:15:09 -0700620 sch->handle = handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Patrick McHardy1e904742008-01-22 22:11:17 -0800622 if (!ops->init || (err = ops->init(sch, tca[TCA_OPTIONS])) == 0) {
623 if (tca[TCA_RATE]) {
Thomas Graf023e09a2005-07-05 14:15:53 -0700624 err = gen_new_estimator(&sch->bstats, &sch->rate_est,
625 sch->stats_lock,
Patrick McHardy1e904742008-01-22 22:11:17 -0800626 tca[TCA_RATE]);
Thomas Graf023e09a2005-07-05 14:15:53 -0700627 if (err) {
628 /*
629 * Any broken qdiscs that would require
630 * a ops->reset() here? The qdisc was never
631 * in action so it shouldn't be necessary.
632 */
633 if (ops->destroy)
634 ops->destroy(sch);
635 goto err_out3;
636 }
637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 qdisc_lock_tree(dev);
David S. Millerb0e1e642008-07-08 17:42:10 -0700639 list_add_tail(&sch->list, &dev_queue->qdisc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 qdisc_unlock_tree(dev);
641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 return sch;
643 }
644err_out3:
645 dev_put(dev);
Thomas Graf3d54b822005-07-05 14:15:09 -0700646 kfree((char *) sch - sch->padded);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647err_out2:
648 module_put(ops->owner);
649err_out:
650 *errp = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 return NULL;
652}
653
Patrick McHardy1e904742008-01-22 22:11:17 -0800654static int qdisc_change(struct Qdisc *sch, struct nlattr **tca)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655{
Patrick McHardy1e904742008-01-22 22:11:17 -0800656 if (tca[TCA_OPTIONS]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 int err;
658
659 if (sch->ops->change == NULL)
660 return -EINVAL;
Patrick McHardy1e904742008-01-22 22:11:17 -0800661 err = sch->ops->change(sch, tca[TCA_OPTIONS]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 if (err)
663 return err;
664 }
Patrick McHardy1e904742008-01-22 22:11:17 -0800665 if (tca[TCA_RATE])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 gen_replace_estimator(&sch->bstats, &sch->rate_est,
Patrick McHardy1e904742008-01-22 22:11:17 -0800667 sch->stats_lock, tca[TCA_RATE]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 return 0;
669}
670
671struct check_loop_arg
672{
673 struct qdisc_walker w;
674 struct Qdisc *p;
675 int depth;
676};
677
678static int check_loop_fn(struct Qdisc *q, unsigned long cl, struct qdisc_walker *w);
679
680static int check_loop(struct Qdisc *q, struct Qdisc *p, int depth)
681{
682 struct check_loop_arg arg;
683
684 if (q->ops->cl_ops == NULL)
685 return 0;
686
687 arg.w.stop = arg.w.skip = arg.w.count = 0;
688 arg.w.fn = check_loop_fn;
689 arg.depth = depth;
690 arg.p = p;
691 q->ops->cl_ops->walk(q, &arg.w);
692 return arg.w.stop ? -ELOOP : 0;
693}
694
695static int
696check_loop_fn(struct Qdisc *q, unsigned long cl, struct qdisc_walker *w)
697{
698 struct Qdisc *leaf;
Eric Dumazet20fea082007-11-14 01:44:41 -0800699 const struct Qdisc_class_ops *cops = q->ops->cl_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 struct check_loop_arg *arg = (struct check_loop_arg *)w;
701
702 leaf = cops->leaf(q, cl);
703 if (leaf) {
704 if (leaf == arg->p || arg->depth > 7)
705 return -ELOOP;
706 return check_loop(leaf, arg->p, arg->depth + 1);
707 }
708 return 0;
709}
710
711/*
712 * Delete/get qdisc.
713 */
714
715static int tc_get_qdisc(struct sk_buff *skb, struct nlmsghdr *n, void *arg)
716{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900717 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 struct tcmsg *tcm = NLMSG_DATA(n);
Patrick McHardy1e904742008-01-22 22:11:17 -0800719 struct nlattr *tca[TCA_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 struct net_device *dev;
721 u32 clid = tcm->tcm_parent;
722 struct Qdisc *q = NULL;
723 struct Qdisc *p = NULL;
724 int err;
725
Denis V. Lunevb8542722007-12-01 00:21:31 +1100726 if (net != &init_net)
727 return -EINVAL;
728
Eric W. Biederman881d9662007-09-17 11:56:21 -0700729 if ((dev = __dev_get_by_index(&init_net, tcm->tcm_ifindex)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 return -ENODEV;
731
Patrick McHardy1e904742008-01-22 22:11:17 -0800732 err = nlmsg_parse(n, sizeof(*tcm), tca, TCA_MAX, NULL);
733 if (err < 0)
734 return err;
735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 if (clid) {
737 if (clid != TC_H_ROOT) {
738 if (TC_H_MAJ(clid) != TC_H_MAJ(TC_H_INGRESS)) {
739 if ((p = qdisc_lookup(dev, TC_H_MAJ(clid))) == NULL)
740 return -ENOENT;
741 q = qdisc_leaf(p, clid);
742 } else { /* ingress */
David S. Miller816f3252008-07-08 22:49:00 -0700743 q = dev->rx_queue.qdisc;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900744 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 } else {
David S. Millerb0e1e642008-07-08 17:42:10 -0700746 struct netdev_queue *dev_queue = &dev->tx_queue;
747 q = dev_queue->qdisc_sleeping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 }
749 if (!q)
750 return -ENOENT;
751
752 if (tcm->tcm_handle && q->handle != tcm->tcm_handle)
753 return -EINVAL;
754 } else {
755 if ((q = qdisc_lookup(dev, tcm->tcm_handle)) == NULL)
756 return -ENOENT;
757 }
758
Patrick McHardy1e904742008-01-22 22:11:17 -0800759 if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], q->ops->id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 return -EINVAL;
761
762 if (n->nlmsg_type == RTM_DELQDISC) {
763 if (!clid)
764 return -EINVAL;
765 if (q->handle == 0)
766 return -ENOENT;
767 if ((err = qdisc_graft(dev, p, clid, NULL, &q)) != 0)
768 return err;
769 if (q) {
770 qdisc_notify(skb, n, clid, q, NULL);
Patrick McHardyfd44de72007-04-16 17:07:08 -0700771 qdisc_lock_tree(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 qdisc_destroy(q);
Patrick McHardyfd44de72007-04-16 17:07:08 -0700773 qdisc_unlock_tree(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 }
775 } else {
776 qdisc_notify(skb, n, clid, NULL, q);
777 }
778 return 0;
779}
780
781/*
782 Create/change qdisc.
783 */
784
785static int tc_modify_qdisc(struct sk_buff *skb, struct nlmsghdr *n, void *arg)
786{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900787 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 struct tcmsg *tcm;
Patrick McHardy1e904742008-01-22 22:11:17 -0800789 struct nlattr *tca[TCA_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 struct net_device *dev;
791 u32 clid;
792 struct Qdisc *q, *p;
793 int err;
794
Denis V. Lunevb8542722007-12-01 00:21:31 +1100795 if (net != &init_net)
796 return -EINVAL;
797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798replay:
799 /* Reinit, just in case something touches this. */
800 tcm = NLMSG_DATA(n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 clid = tcm->tcm_parent;
802 q = p = NULL;
803
Eric W. Biederman881d9662007-09-17 11:56:21 -0700804 if ((dev = __dev_get_by_index(&init_net, tcm->tcm_ifindex)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 return -ENODEV;
806
Patrick McHardy1e904742008-01-22 22:11:17 -0800807 err = nlmsg_parse(n, sizeof(*tcm), tca, TCA_MAX, NULL);
808 if (err < 0)
809 return err;
810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 if (clid) {
812 if (clid != TC_H_ROOT) {
813 if (clid != TC_H_INGRESS) {
814 if ((p = qdisc_lookup(dev, TC_H_MAJ(clid))) == NULL)
815 return -ENOENT;
816 q = qdisc_leaf(p, clid);
817 } else { /*ingress */
David S. Miller816f3252008-07-08 22:49:00 -0700818 q = dev->rx_queue.qdisc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 }
820 } else {
David S. Millerb0e1e642008-07-08 17:42:10 -0700821 struct netdev_queue *dev_queue = &dev->tx_queue;
822 q = dev_queue->qdisc_sleeping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 }
824
825 /* It may be default qdisc, ignore it */
826 if (q && q->handle == 0)
827 q = NULL;
828
829 if (!q || !tcm->tcm_handle || q->handle != tcm->tcm_handle) {
830 if (tcm->tcm_handle) {
831 if (q && !(n->nlmsg_flags&NLM_F_REPLACE))
832 return -EEXIST;
833 if (TC_H_MIN(tcm->tcm_handle))
834 return -EINVAL;
835 if ((q = qdisc_lookup(dev, tcm->tcm_handle)) == NULL)
836 goto create_n_graft;
837 if (n->nlmsg_flags&NLM_F_EXCL)
838 return -EEXIST;
Patrick McHardy1e904742008-01-22 22:11:17 -0800839 if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], q->ops->id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 return -EINVAL;
841 if (q == p ||
842 (p && check_loop(q, p, 0)))
843 return -ELOOP;
844 atomic_inc(&q->refcnt);
845 goto graft;
846 } else {
847 if (q == NULL)
848 goto create_n_graft;
849
850 /* This magic test requires explanation.
851 *
852 * We know, that some child q is already
853 * attached to this parent and have choice:
854 * either to change it or to create/graft new one.
855 *
856 * 1. We are allowed to create/graft only
857 * if CREATE and REPLACE flags are set.
858 *
859 * 2. If EXCL is set, requestor wanted to say,
860 * that qdisc tcm_handle is not expected
861 * to exist, so that we choose create/graft too.
862 *
863 * 3. The last case is when no flags are set.
864 * Alas, it is sort of hole in API, we
865 * cannot decide what to do unambiguously.
866 * For now we select create/graft, if
867 * user gave KIND, which does not match existing.
868 */
869 if ((n->nlmsg_flags&NLM_F_CREATE) &&
870 (n->nlmsg_flags&NLM_F_REPLACE) &&
871 ((n->nlmsg_flags&NLM_F_EXCL) ||
Patrick McHardy1e904742008-01-22 22:11:17 -0800872 (tca[TCA_KIND] &&
873 nla_strcmp(tca[TCA_KIND], q->ops->id))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 goto create_n_graft;
875 }
876 }
877 } else {
878 if (!tcm->tcm_handle)
879 return -EINVAL;
880 q = qdisc_lookup(dev, tcm->tcm_handle);
881 }
882
883 /* Change qdisc parameters */
884 if (q == NULL)
885 return -ENOENT;
886 if (n->nlmsg_flags&NLM_F_EXCL)
887 return -EEXIST;
Patrick McHardy1e904742008-01-22 22:11:17 -0800888 if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], q->ops->id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 return -EINVAL;
890 err = qdisc_change(q, tca);
891 if (err == 0)
892 qdisc_notify(skb, n, clid, NULL, q);
893 return err;
894
895create_n_graft:
896 if (!(n->nlmsg_flags&NLM_F_CREATE))
897 return -ENOENT;
898 if (clid == TC_H_INGRESS)
David S. Millerbb949fb2008-07-08 16:55:56 -0700899 q = qdisc_create(dev, &dev->rx_queue,
900 tcm->tcm_parent, tcm->tcm_parent,
Patrick McHardyffc8fef2007-07-30 17:11:50 -0700901 tca, &err);
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900902 else
David S. Millerbb949fb2008-07-08 16:55:56 -0700903 q = qdisc_create(dev, &dev->tx_queue,
904 tcm->tcm_parent, tcm->tcm_handle,
Patrick McHardyffc8fef2007-07-30 17:11:50 -0700905 tca, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 if (q == NULL) {
907 if (err == -EAGAIN)
908 goto replay;
909 return err;
910 }
911
912graft:
913 if (1) {
914 struct Qdisc *old_q = NULL;
915 err = qdisc_graft(dev, p, clid, q, &old_q);
916 if (err) {
917 if (q) {
Patrick McHardyfd44de72007-04-16 17:07:08 -0700918 qdisc_lock_tree(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 qdisc_destroy(q);
Patrick McHardyfd44de72007-04-16 17:07:08 -0700920 qdisc_unlock_tree(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 }
922 return err;
923 }
924 qdisc_notify(skb, n, clid, old_q, q);
925 if (old_q) {
Patrick McHardyfd44de72007-04-16 17:07:08 -0700926 qdisc_lock_tree(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 qdisc_destroy(old_q);
Patrick McHardyfd44de72007-04-16 17:07:08 -0700928 qdisc_unlock_tree(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 }
930 }
931 return 0;
932}
933
934static int tc_fill_qdisc(struct sk_buff *skb, struct Qdisc *q, u32 clid,
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -0700935 u32 pid, u32 seq, u16 flags, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936{
937 struct tcmsg *tcm;
938 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700939 unsigned char *b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 struct gnet_dump d;
941
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -0700942 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*tcm), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 tcm = NLMSG_DATA(nlh);
944 tcm->tcm_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700945 tcm->tcm__pad1 = 0;
946 tcm->tcm__pad2 = 0;
David S. Miller5ce2d482008-07-08 17:06:30 -0700947 tcm->tcm_ifindex = qdisc_dev(q)->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 tcm->tcm_parent = clid;
949 tcm->tcm_handle = q->handle;
950 tcm->tcm_info = atomic_read(&q->refcnt);
Patrick McHardy57e1c482008-01-23 20:34:28 -0800951 NLA_PUT_STRING(skb, TCA_KIND, q->ops->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 if (q->ops->dump && q->ops->dump(q, skb) < 0)
Patrick McHardy1e904742008-01-22 22:11:17 -0800953 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 q->qstats.qlen = q->q.qlen;
955
956 if (gnet_stats_start_copy_compat(skb, TCA_STATS2, TCA_STATS,
957 TCA_XSTATS, q->stats_lock, &d) < 0)
Patrick McHardy1e904742008-01-22 22:11:17 -0800958 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
960 if (q->ops->dump_stats && q->ops->dump_stats(q, &d) < 0)
Patrick McHardy1e904742008-01-22 22:11:17 -0800961 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
963 if (gnet_stats_copy_basic(&d, &q->bstats) < 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 gnet_stats_copy_rate_est(&d, &q->rate_est) < 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 gnet_stats_copy_queue(&d, &q->qstats) < 0)
Patrick McHardy1e904742008-01-22 22:11:17 -0800966 goto nla_put_failure;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900967
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 if (gnet_stats_finish_copy(&d) < 0)
Patrick McHardy1e904742008-01-22 22:11:17 -0800969 goto nla_put_failure;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900970
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700971 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 return skb->len;
973
974nlmsg_failure:
Patrick McHardy1e904742008-01-22 22:11:17 -0800975nla_put_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700976 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 return -1;
978}
979
980static int qdisc_notify(struct sk_buff *oskb, struct nlmsghdr *n,
981 u32 clid, struct Qdisc *old, struct Qdisc *new)
982{
983 struct sk_buff *skb;
984 u32 pid = oskb ? NETLINK_CB(oskb).pid : 0;
985
986 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
987 if (!skb)
988 return -ENOBUFS;
989
990 if (old && old->handle) {
991 if (tc_fill_qdisc(skb, old, clid, pid, n->nlmsg_seq, 0, RTM_DELQDISC) < 0)
992 goto err_out;
993 }
994 if (new) {
995 if (tc_fill_qdisc(skb, new, clid, pid, n->nlmsg_seq, old ? NLM_F_REPLACE : 0, RTM_NEWQDISC) < 0)
996 goto err_out;
997 }
998
999 if (skb->len)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08001000 return rtnetlink_send(skb, &init_net, pid, RTNLGRP_TC, n->nlmsg_flags&NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
1002err_out:
1003 kfree_skb(skb);
1004 return -EINVAL;
1005}
1006
1007static int tc_dump_qdisc(struct sk_buff *skb, struct netlink_callback *cb)
1008{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001009 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 int idx, q_idx;
1011 int s_idx, s_q_idx;
1012 struct net_device *dev;
1013 struct Qdisc *q;
1014
Denis V. Lunevb8542722007-12-01 00:21:31 +11001015 if (net != &init_net)
1016 return 0;
1017
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 s_idx = cb->args[0];
1019 s_q_idx = q_idx = cb->args[1];
1020 read_lock(&dev_base_lock);
Pavel Emelianov7562f872007-05-03 15:13:45 -07001021 idx = 0;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001022 for_each_netdev(&init_net, dev) {
David S. Millerb0e1e642008-07-08 17:42:10 -07001023 struct netdev_queue *dev_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 if (idx < s_idx)
Pavel Emelianov7562f872007-05-03 15:13:45 -07001025 goto cont;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 if (idx > s_idx)
1027 s_q_idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 q_idx = 0;
David S. Millerb0e1e642008-07-08 17:42:10 -07001029 dev_queue = &dev->tx_queue;
1030 list_for_each_entry(q, &dev_queue->qdisc_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 if (q_idx < s_q_idx) {
1032 q_idx++;
1033 continue;
1034 }
1035 if (tc_fill_qdisc(skb, q, q->parent, NETLINK_CB(cb->skb).pid,
Patrick McHardy0463d4a2007-04-16 17:02:10 -07001036 cb->nlh->nlmsg_seq, NLM_F_MULTI, RTM_NEWQDISC) <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 q_idx++;
1039 }
Pavel Emelianov7562f872007-05-03 15:13:45 -07001040cont:
1041 idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 }
1043
1044done:
1045 read_unlock(&dev_base_lock);
1046
1047 cb->args[0] = idx;
1048 cb->args[1] = q_idx;
1049
1050 return skb->len;
1051}
1052
1053
1054
1055/************************************************
1056 * Traffic classes manipulation. *
1057 ************************************************/
1058
1059
1060
1061static int tc_ctl_tclass(struct sk_buff *skb, struct nlmsghdr *n, void *arg)
1062{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001063 struct net *net = sock_net(skb->sk);
David S. Millerb0e1e642008-07-08 17:42:10 -07001064 struct netdev_queue *dev_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 struct tcmsg *tcm = NLMSG_DATA(n);
Patrick McHardy1e904742008-01-22 22:11:17 -08001066 struct nlattr *tca[TCA_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 struct net_device *dev;
1068 struct Qdisc *q = NULL;
Eric Dumazet20fea082007-11-14 01:44:41 -08001069 const struct Qdisc_class_ops *cops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 unsigned long cl = 0;
1071 unsigned long new_cl;
1072 u32 pid = tcm->tcm_parent;
1073 u32 clid = tcm->tcm_handle;
1074 u32 qid = TC_H_MAJ(clid);
1075 int err;
1076
Denis V. Lunevb8542722007-12-01 00:21:31 +11001077 if (net != &init_net)
1078 return -EINVAL;
1079
Eric W. Biederman881d9662007-09-17 11:56:21 -07001080 if ((dev = __dev_get_by_index(&init_net, tcm->tcm_ifindex)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 return -ENODEV;
1082
Patrick McHardy1e904742008-01-22 22:11:17 -08001083 err = nlmsg_parse(n, sizeof(*tcm), tca, TCA_MAX, NULL);
1084 if (err < 0)
1085 return err;
1086
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 /*
1088 parent == TC_H_UNSPEC - unspecified parent.
1089 parent == TC_H_ROOT - class is root, which has no parent.
1090 parent == X:0 - parent is root class.
1091 parent == X:Y - parent is a node in hierarchy.
1092 parent == 0:Y - parent is X:Y, where X:0 is qdisc.
1093
1094 handle == 0:0 - generate handle from kernel pool.
1095 handle == 0:Y - class is X:Y, where X:0 is qdisc.
1096 handle == X:Y - clear.
1097 handle == X:0 - root class.
1098 */
1099
1100 /* Step 1. Determine qdisc handle X:0 */
1101
David S. Millerb0e1e642008-07-08 17:42:10 -07001102 dev_queue = &dev->tx_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 if (pid != TC_H_ROOT) {
1104 u32 qid1 = TC_H_MAJ(pid);
1105
1106 if (qid && qid1) {
1107 /* If both majors are known, they must be identical. */
1108 if (qid != qid1)
1109 return -EINVAL;
1110 } else if (qid1) {
1111 qid = qid1;
1112 } else if (qid == 0)
David S. Millerb0e1e642008-07-08 17:42:10 -07001113 qid = dev_queue->qdisc_sleeping->handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
1115 /* Now qid is genuine qdisc handle consistent
1116 both with parent and child.
1117
1118 TC_H_MAJ(pid) still may be unspecified, complete it now.
1119 */
1120 if (pid)
1121 pid = TC_H_MAKE(qid, pid);
1122 } else {
1123 if (qid == 0)
David S. Millerb0e1e642008-07-08 17:42:10 -07001124 qid = dev_queue->qdisc_sleeping->handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 }
1126
1127 /* OK. Locate qdisc */
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +09001128 if ((q = qdisc_lookup(dev, qid)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 return -ENOENT;
1130
1131 /* An check that it supports classes */
1132 cops = q->ops->cl_ops;
1133 if (cops == NULL)
1134 return -EINVAL;
1135
1136 /* Now try to get class */
1137 if (clid == 0) {
1138 if (pid == TC_H_ROOT)
1139 clid = qid;
1140 } else
1141 clid = TC_H_MAKE(qid, clid);
1142
1143 if (clid)
1144 cl = cops->get(q, clid);
1145
1146 if (cl == 0) {
1147 err = -ENOENT;
1148 if (n->nlmsg_type != RTM_NEWTCLASS || !(n->nlmsg_flags&NLM_F_CREATE))
1149 goto out;
1150 } else {
1151 switch (n->nlmsg_type) {
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +09001152 case RTM_NEWTCLASS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 err = -EEXIST;
1154 if (n->nlmsg_flags&NLM_F_EXCL)
1155 goto out;
1156 break;
1157 case RTM_DELTCLASS:
1158 err = cops->delete(q, cl);
1159 if (err == 0)
1160 tclass_notify(skb, n, q, cl, RTM_DELTCLASS);
1161 goto out;
1162 case RTM_GETTCLASS:
1163 err = tclass_notify(skb, n, q, cl, RTM_NEWTCLASS);
1164 goto out;
1165 default:
1166 err = -EINVAL;
1167 goto out;
1168 }
1169 }
1170
1171 new_cl = cl;
1172 err = cops->change(q, clid, pid, tca, &new_cl);
1173 if (err == 0)
1174 tclass_notify(skb, n, q, new_cl, RTM_NEWTCLASS);
1175
1176out:
1177 if (cl)
1178 cops->put(q, cl);
1179
1180 return err;
1181}
1182
1183
1184static int tc_fill_tclass(struct sk_buff *skb, struct Qdisc *q,
1185 unsigned long cl,
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -07001186 u32 pid, u32 seq, u16 flags, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187{
1188 struct tcmsg *tcm;
1189 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001190 unsigned char *b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 struct gnet_dump d;
Eric Dumazet20fea082007-11-14 01:44:41 -08001192 const struct Qdisc_class_ops *cl_ops = q->ops->cl_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -07001194 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*tcm), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 tcm = NLMSG_DATA(nlh);
1196 tcm->tcm_family = AF_UNSPEC;
David S. Miller5ce2d482008-07-08 17:06:30 -07001197 tcm->tcm_ifindex = qdisc_dev(q)->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 tcm->tcm_parent = q->handle;
1199 tcm->tcm_handle = q->handle;
1200 tcm->tcm_info = 0;
Patrick McHardy57e1c482008-01-23 20:34:28 -08001201 NLA_PUT_STRING(skb, TCA_KIND, q->ops->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 if (cl_ops->dump && cl_ops->dump(q, cl, skb, tcm) < 0)
Patrick McHardy1e904742008-01-22 22:11:17 -08001203 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
1205 if (gnet_stats_start_copy_compat(skb, TCA_STATS2, TCA_STATS,
1206 TCA_XSTATS, q->stats_lock, &d) < 0)
Patrick McHardy1e904742008-01-22 22:11:17 -08001207 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
1209 if (cl_ops->dump_stats && cl_ops->dump_stats(q, cl, &d) < 0)
Patrick McHardy1e904742008-01-22 22:11:17 -08001210 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
1212 if (gnet_stats_finish_copy(&d) < 0)
Patrick McHardy1e904742008-01-22 22:11:17 -08001213 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001215 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 return skb->len;
1217
1218nlmsg_failure:
Patrick McHardy1e904742008-01-22 22:11:17 -08001219nla_put_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001220 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 return -1;
1222}
1223
1224static int tclass_notify(struct sk_buff *oskb, struct nlmsghdr *n,
1225 struct Qdisc *q, unsigned long cl, int event)
1226{
1227 struct sk_buff *skb;
1228 u32 pid = oskb ? NETLINK_CB(oskb).pid : 0;
1229
1230 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1231 if (!skb)
1232 return -ENOBUFS;
1233
1234 if (tc_fill_tclass(skb, q, cl, pid, n->nlmsg_seq, 0, event) < 0) {
1235 kfree_skb(skb);
1236 return -EINVAL;
1237 }
1238
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08001239 return rtnetlink_send(skb, &init_net, pid, RTNLGRP_TC, n->nlmsg_flags&NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240}
1241
1242struct qdisc_dump_args
1243{
1244 struct qdisc_walker w;
1245 struct sk_buff *skb;
1246 struct netlink_callback *cb;
1247};
1248
1249static int qdisc_class_dump(struct Qdisc *q, unsigned long cl, struct qdisc_walker *arg)
1250{
1251 struct qdisc_dump_args *a = (struct qdisc_dump_args *)arg;
1252
1253 return tc_fill_tclass(a->skb, q, cl, NETLINK_CB(a->cb->skb).pid,
1254 a->cb->nlh->nlmsg_seq, NLM_F_MULTI, RTM_NEWTCLASS);
1255}
1256
1257static int tc_dump_tclass(struct sk_buff *skb, struct netlink_callback *cb)
1258{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001259 struct net *net = sock_net(skb->sk);
David S. Millerb0e1e642008-07-08 17:42:10 -07001260 struct netdev_queue *dev_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 int t;
1262 int s_t;
1263 struct net_device *dev;
1264 struct Qdisc *q;
1265 struct tcmsg *tcm = (struct tcmsg*)NLMSG_DATA(cb->nlh);
1266 struct qdisc_dump_args arg;
1267
Denis V. Lunevb8542722007-12-01 00:21:31 +11001268 if (net != &init_net)
1269 return 0;
1270
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 if (cb->nlh->nlmsg_len < NLMSG_LENGTH(sizeof(*tcm)))
1272 return 0;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001273 if ((dev = dev_get_by_index(&init_net, tcm->tcm_ifindex)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 return 0;
1275
1276 s_t = cb->args[0];
1277 t = 0;
1278
David S. Millerb0e1e642008-07-08 17:42:10 -07001279 dev_queue = &dev->tx_queue;
1280 list_for_each_entry(q, &dev_queue->qdisc_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 if (t < s_t || !q->ops->cl_ops ||
1282 (tcm->tcm_parent &&
1283 TC_H_MAJ(tcm->tcm_parent) != q->handle)) {
1284 t++;
1285 continue;
1286 }
1287 if (t > s_t)
1288 memset(&cb->args[1], 0, sizeof(cb->args)-sizeof(cb->args[0]));
1289 arg.w.fn = qdisc_class_dump;
1290 arg.skb = skb;
1291 arg.cb = cb;
1292 arg.w.stop = 0;
1293 arg.w.skip = cb->args[1];
1294 arg.w.count = 0;
1295 q->ops->cl_ops->walk(q, &arg.w);
1296 cb->args[1] = arg.w.count;
1297 if (arg.w.stop)
1298 break;
1299 t++;
1300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
1302 cb->args[0] = t;
1303
1304 dev_put(dev);
1305 return skb->len;
1306}
1307
1308/* Main classifier routine: scans classifier chain attached
1309 to this qdisc, (optionally) tests for protocol and asks
1310 specific classifiers.
1311 */
Patrick McHardy73ca4912007-07-15 00:02:31 -07001312int tc_classify_compat(struct sk_buff *skb, struct tcf_proto *tp,
1313 struct tcf_result *res)
1314{
1315 __be16 protocol = skb->protocol;
1316 int err = 0;
1317
1318 for (; tp; tp = tp->next) {
1319 if ((tp->protocol == protocol ||
1320 tp->protocol == htons(ETH_P_ALL)) &&
1321 (err = tp->classify(skb, tp, res)) >= 0) {
1322#ifdef CONFIG_NET_CLS_ACT
1323 if (err != TC_ACT_RECLASSIFY && skb->tc_verd)
1324 skb->tc_verd = SET_TC_VERD(skb->tc_verd, 0);
1325#endif
1326 return err;
1327 }
1328 }
1329 return -1;
1330}
1331EXPORT_SYMBOL(tc_classify_compat);
1332
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333int tc_classify(struct sk_buff *skb, struct tcf_proto *tp,
Patrick McHardy73ca4912007-07-15 00:02:31 -07001334 struct tcf_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335{
1336 int err = 0;
Patrick McHardy73ca4912007-07-15 00:02:31 -07001337 __be16 protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338#ifdef CONFIG_NET_CLS_ACT
1339 struct tcf_proto *otp = tp;
1340reclassify:
1341#endif
1342 protocol = skb->protocol;
1343
Patrick McHardy73ca4912007-07-15 00:02:31 -07001344 err = tc_classify_compat(skb, tp, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345#ifdef CONFIG_NET_CLS_ACT
Patrick McHardy73ca4912007-07-15 00:02:31 -07001346 if (err == TC_ACT_RECLASSIFY) {
1347 u32 verd = G_TC_VERD(skb->tc_verd);
1348 tp = otp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
Patrick McHardy73ca4912007-07-15 00:02:31 -07001350 if (verd++ >= MAX_REC_LOOP) {
1351 printk("rule prio %u protocol %02x reclassify loop, "
1352 "packet dropped\n",
1353 tp->prio&0xffff, ntohs(tp->protocol));
1354 return TC_ACT_SHOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 }
Patrick McHardy73ca4912007-07-15 00:02:31 -07001356 skb->tc_verd = SET_TC_VERD(skb->tc_verd, verd);
1357 goto reclassify;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 }
Patrick McHardy73ca4912007-07-15 00:02:31 -07001359#endif
1360 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361}
Patrick McHardy73ca4912007-07-15 00:02:31 -07001362EXPORT_SYMBOL(tc_classify);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
Patrick McHardya48b5a62007-03-23 11:29:43 -07001364void tcf_destroy(struct tcf_proto *tp)
1365{
1366 tp->ops->destroy(tp);
1367 module_put(tp->ops->owner);
1368 kfree(tp);
1369}
1370
Patrick McHardyff31ab52008-07-01 19:52:38 -07001371void tcf_destroy_chain(struct tcf_proto **fl)
Patrick McHardya48b5a62007-03-23 11:29:43 -07001372{
1373 struct tcf_proto *tp;
1374
Patrick McHardyff31ab52008-07-01 19:52:38 -07001375 while ((tp = *fl) != NULL) {
1376 *fl = tp->next;
Patrick McHardya48b5a62007-03-23 11:29:43 -07001377 tcf_destroy(tp);
1378 }
1379}
1380EXPORT_SYMBOL(tcf_destroy_chain);
1381
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382#ifdef CONFIG_PROC_FS
1383static int psched_show(struct seq_file *seq, void *v)
1384{
Patrick McHardy3c0cfc12007-10-10 16:32:41 -07001385 struct timespec ts;
1386
1387 hrtimer_get_res(CLOCK_MONOTONIC, &ts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 seq_printf(seq, "%08x %08x %08x %08x\n",
Patrick McHardy641b9e02007-03-16 01:18:42 -07001389 (u32)NSEC_PER_USEC, (u32)PSCHED_US2NS(1),
Patrick McHardy514bca32007-03-16 12:34:52 -07001390 1000000,
Patrick McHardy3c0cfc12007-10-10 16:32:41 -07001391 (u32)NSEC_PER_SEC/(u32)ktime_to_ns(timespec_to_ktime(ts)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
1393 return 0;
1394}
1395
1396static int psched_open(struct inode *inode, struct file *file)
1397{
1398 return single_open(file, psched_show, PDE(inode)->data);
1399}
1400
Arjan van de Venda7071d2007-02-12 00:55:36 -08001401static const struct file_operations psched_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 .owner = THIS_MODULE,
1403 .open = psched_open,
1404 .read = seq_read,
1405 .llseek = seq_lseek,
1406 .release = single_release,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +09001407};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408#endif
1409
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410static int __init pktsched_init(void)
1411{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 register_qdisc(&pfifo_qdisc_ops);
1413 register_qdisc(&bfifo_qdisc_ops);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02001414 proc_net_fops_create(&init_net, "psched", 0, &psched_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
Thomas Grafbe577dd2007-03-22 11:55:50 -07001416 rtnl_register(PF_UNSPEC, RTM_NEWQDISC, tc_modify_qdisc, NULL);
1417 rtnl_register(PF_UNSPEC, RTM_DELQDISC, tc_get_qdisc, NULL);
1418 rtnl_register(PF_UNSPEC, RTM_GETQDISC, tc_get_qdisc, tc_dump_qdisc);
1419 rtnl_register(PF_UNSPEC, RTM_NEWTCLASS, tc_ctl_tclass, NULL);
1420 rtnl_register(PF_UNSPEC, RTM_DELTCLASS, tc_ctl_tclass, NULL);
1421 rtnl_register(PF_UNSPEC, RTM_GETTCLASS, tc_ctl_tclass, tc_dump_tclass);
1422
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 return 0;
1424}
1425
1426subsys_initcall(pktsched_init);