blob: b3ef8307204e8ec95f04fc6e345a8d2046b87b52 [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
David S. Milleread81cc2008-07-17 00:50:32 -0700186struct Qdisc *qdisc_lookup(struct net_device *dev, u32 handle)
Patrick McHardy43effa12006-11-29 17:35:48 -0800187{
188 struct Qdisc *q;
189
David S. Milleread81cc2008-07-17 00:50:32 -0700190 list_for_each_entry(q, &dev->qdisc_list, list) {
Patrick McHardy43effa12006-11-29 17:35:48 -0800191 if (q->handle == handle)
192 return q;
193 }
194 return NULL;
195}
196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197static struct Qdisc *qdisc_leaf(struct Qdisc *p, u32 classid)
198{
199 unsigned long cl;
200 struct Qdisc *leaf;
Eric Dumazet20fea082007-11-14 01:44:41 -0800201 const struct Qdisc_class_ops *cops = p->ops->cl_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203 if (cops == NULL)
204 return NULL;
205 cl = cops->get(p, classid);
206
207 if (cl == 0)
208 return NULL;
209 leaf = cops->leaf(p, cl);
210 cops->put(p, cl);
211 return leaf;
212}
213
214/* Find queueing discipline by name */
215
Patrick McHardy1e904742008-01-22 22:11:17 -0800216static struct Qdisc_ops *qdisc_lookup_ops(struct nlattr *kind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
218 struct Qdisc_ops *q = NULL;
219
220 if (kind) {
221 read_lock(&qdisc_mod_lock);
222 for (q = qdisc_base; q; q = q->next) {
Patrick McHardy1e904742008-01-22 22:11:17 -0800223 if (nla_strcmp(kind, q->id) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 if (!try_module_get(q->owner))
225 q = NULL;
226 break;
227 }
228 }
229 read_unlock(&qdisc_mod_lock);
230 }
231 return q;
232}
233
234static struct qdisc_rate_table *qdisc_rtab_list;
235
Patrick McHardy1e904742008-01-22 22:11:17 -0800236struct qdisc_rate_table *qdisc_get_rtab(struct tc_ratespec *r, struct nlattr *tab)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
238 struct qdisc_rate_table *rtab;
239
240 for (rtab = qdisc_rtab_list; rtab; rtab = rtab->next) {
241 if (memcmp(&rtab->rate, r, sizeof(struct tc_ratespec)) == 0) {
242 rtab->refcnt++;
243 return rtab;
244 }
245 }
246
Patrick McHardy5feb5e12008-01-23 20:35:19 -0800247 if (tab == NULL || r->rate == 0 || r->cell_log == 0 ||
248 nla_len(tab) != TC_RTAB_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 return NULL;
250
251 rtab = kmalloc(sizeof(*rtab), GFP_KERNEL);
252 if (rtab) {
253 rtab->rate = *r;
254 rtab->refcnt = 1;
Patrick McHardy1e904742008-01-22 22:11:17 -0800255 memcpy(rtab->data, nla_data(tab), 1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 rtab->next = qdisc_rtab_list;
257 qdisc_rtab_list = rtab;
258 }
259 return rtab;
260}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800261EXPORT_SYMBOL(qdisc_get_rtab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263void qdisc_put_rtab(struct qdisc_rate_table *tab)
264{
265 struct qdisc_rate_table *rtab, **rtabp;
266
267 if (!tab || --tab->refcnt)
268 return;
269
270 for (rtabp = &qdisc_rtab_list; (rtab=*rtabp) != NULL; rtabp = &rtab->next) {
271 if (rtab == tab) {
272 *rtabp = rtab->next;
273 kfree(rtab);
274 return;
275 }
276 }
277}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800278EXPORT_SYMBOL(qdisc_put_rtab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Patrick McHardy41794772007-03-16 01:19:15 -0700280static enum hrtimer_restart qdisc_watchdog(struct hrtimer *timer)
281{
282 struct qdisc_watchdog *wd = container_of(timer, struct qdisc_watchdog,
283 timer);
284
285 wd->qdisc->flags &= ~TCQ_F_THROTTLED;
Stephen Hemminger11274e52007-03-22 12:17:42 -0700286 smp_wmb();
David S. Miller37437bb2008-07-16 02:15:04 -0700287 __netif_schedule(wd->qdisc);
Stephen Hemminger19365022007-03-22 12:18:35 -0700288
Patrick McHardy41794772007-03-16 01:19:15 -0700289 return HRTIMER_NORESTART;
290}
291
292void qdisc_watchdog_init(struct qdisc_watchdog *wd, struct Qdisc *qdisc)
293{
294 hrtimer_init(&wd->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
295 wd->timer.function = qdisc_watchdog;
296 wd->qdisc = qdisc;
297}
298EXPORT_SYMBOL(qdisc_watchdog_init);
299
300void qdisc_watchdog_schedule(struct qdisc_watchdog *wd, psched_time_t expires)
301{
302 ktime_t time;
303
304 wd->qdisc->flags |= TCQ_F_THROTTLED;
305 time = ktime_set(0, 0);
306 time = ktime_add_ns(time, PSCHED_US2NS(expires));
307 hrtimer_start(&wd->timer, time, HRTIMER_MODE_ABS);
308}
309EXPORT_SYMBOL(qdisc_watchdog_schedule);
310
311void qdisc_watchdog_cancel(struct qdisc_watchdog *wd)
312{
313 hrtimer_cancel(&wd->timer);
314 wd->qdisc->flags &= ~TCQ_F_THROTTLED;
315}
316EXPORT_SYMBOL(qdisc_watchdog_cancel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Patrick McHardy6fe1c7a2008-07-05 23:21:31 -0700318struct hlist_head *qdisc_class_hash_alloc(unsigned int n)
319{
320 unsigned int size = n * sizeof(struct hlist_head), i;
321 struct hlist_head *h;
322
323 if (size <= PAGE_SIZE)
324 h = kmalloc(size, GFP_KERNEL);
325 else
326 h = (struct hlist_head *)
327 __get_free_pages(GFP_KERNEL, get_order(size));
328
329 if (h != NULL) {
330 for (i = 0; i < n; i++)
331 INIT_HLIST_HEAD(&h[i]);
332 }
333 return h;
334}
335
336static void qdisc_class_hash_free(struct hlist_head *h, unsigned int n)
337{
338 unsigned int size = n * sizeof(struct hlist_head);
339
340 if (size <= PAGE_SIZE)
341 kfree(h);
342 else
343 free_pages((unsigned long)h, get_order(size));
344}
345
346void qdisc_class_hash_grow(struct Qdisc *sch, struct Qdisc_class_hash *clhash)
347{
348 struct Qdisc_class_common *cl;
349 struct hlist_node *n, *next;
350 struct hlist_head *nhash, *ohash;
351 unsigned int nsize, nmask, osize;
352 unsigned int i, h;
353
354 /* Rehash when load factor exceeds 0.75 */
355 if (clhash->hashelems * 4 <= clhash->hashsize * 3)
356 return;
357 nsize = clhash->hashsize * 2;
358 nmask = nsize - 1;
359 nhash = qdisc_class_hash_alloc(nsize);
360 if (nhash == NULL)
361 return;
362
363 ohash = clhash->hash;
364 osize = clhash->hashsize;
365
366 sch_tree_lock(sch);
367 for (i = 0; i < osize; i++) {
368 hlist_for_each_entry_safe(cl, n, next, &ohash[i], hnode) {
369 h = qdisc_class_hash(cl->classid, nmask);
370 hlist_add_head(&cl->hnode, &nhash[h]);
371 }
372 }
373 clhash->hash = nhash;
374 clhash->hashsize = nsize;
375 clhash->hashmask = nmask;
376 sch_tree_unlock(sch);
377
378 qdisc_class_hash_free(ohash, osize);
379}
380EXPORT_SYMBOL(qdisc_class_hash_grow);
381
382int qdisc_class_hash_init(struct Qdisc_class_hash *clhash)
383{
384 unsigned int size = 4;
385
386 clhash->hash = qdisc_class_hash_alloc(size);
387 if (clhash->hash == NULL)
388 return -ENOMEM;
389 clhash->hashsize = size;
390 clhash->hashmask = size - 1;
391 clhash->hashelems = 0;
392 return 0;
393}
394EXPORT_SYMBOL(qdisc_class_hash_init);
395
396void qdisc_class_hash_destroy(struct Qdisc_class_hash *clhash)
397{
398 qdisc_class_hash_free(clhash->hash, clhash->hashsize);
399}
400EXPORT_SYMBOL(qdisc_class_hash_destroy);
401
402void qdisc_class_hash_insert(struct Qdisc_class_hash *clhash,
403 struct Qdisc_class_common *cl)
404{
405 unsigned int h;
406
407 INIT_HLIST_NODE(&cl->hnode);
408 h = qdisc_class_hash(cl->classid, clhash->hashmask);
409 hlist_add_head(&cl->hnode, &clhash->hash[h]);
410 clhash->hashelems++;
411}
412EXPORT_SYMBOL(qdisc_class_hash_insert);
413
414void qdisc_class_hash_remove(struct Qdisc_class_hash *clhash,
415 struct Qdisc_class_common *cl)
416{
417 hlist_del(&cl->hnode);
418 clhash->hashelems--;
419}
420EXPORT_SYMBOL(qdisc_class_hash_remove);
421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422/* Allocate an unique handle from space managed by kernel */
423
424static u32 qdisc_alloc_handle(struct net_device *dev)
425{
426 int i = 0x10000;
427 static u32 autohandle = TC_H_MAKE(0x80000000U, 0);
428
429 do {
430 autohandle += TC_H_MAKE(0x10000U, 0);
431 if (autohandle == TC_H_MAKE(TC_H_ROOT, 0))
432 autohandle = TC_H_MAKE(0x80000000U, 0);
433 } while (qdisc_lookup(dev, autohandle) && --i > 0);
434
435 return i>0 ? autohandle : 0;
436}
437
David S. Miller99194cf2008-07-17 04:54:10 -0700438/* Attach toplevel qdisc to device queue. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
David S. Miller99194cf2008-07-17 04:54:10 -0700440static struct Qdisc *dev_graft_qdisc(struct netdev_queue *dev_queue,
441 struct Qdisc *qdisc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442{
David S. Miller53049972008-07-16 03:00:19 -0700443 spinlock_t *root_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 struct Qdisc *oqdisc;
David S. Miller53049972008-07-16 03:00:19 -0700445 int ingress;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
David S. Miller53049972008-07-16 03:00:19 -0700447 ingress = 0;
448 if (qdisc && qdisc->flags&TCQ_F_INGRESS)
449 ingress = 1;
450
451 if (ingress) {
David S. Miller816f3252008-07-08 22:49:00 -0700452 oqdisc = dev_queue->qdisc;
David S. Miller53049972008-07-16 03:00:19 -0700453 } else {
David S. Miller53049972008-07-16 03:00:19 -0700454 oqdisc = dev_queue->qdisc_sleeping;
455 }
456
457 root_lock = qdisc_root_lock(oqdisc);
458 spin_lock_bh(root_lock);
459
460 if (ingress) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 /* Prune old scheduler */
462 if (oqdisc && atomic_read(&oqdisc->refcnt) <= 1) {
463 /* delete */
464 qdisc_reset(oqdisc);
David S. Miller816f3252008-07-08 22:49:00 -0700465 dev_queue->qdisc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 } else { /* new */
David S. Miller816f3252008-07-08 22:49:00 -0700467 dev_queue->qdisc = qdisc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 }
469
470 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 /* Prune old scheduler */
472 if (oqdisc && atomic_read(&oqdisc->refcnt) <= 1)
473 qdisc_reset(oqdisc);
474
475 /* ... and graft new one */
476 if (qdisc == NULL)
477 qdisc = &noop_qdisc;
David S. Millerb0e1e642008-07-08 17:42:10 -0700478 dev_queue->qdisc_sleeping = qdisc;
479 dev_queue->qdisc = &noop_qdisc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 }
481
David S. Miller53049972008-07-16 03:00:19 -0700482 spin_unlock_bh(root_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 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
David S. Miller99194cf2008-07-17 04:54:10 -0700515static void notify_and_destroy(struct sk_buff *skb, struct nlmsghdr *n, u32 clid,
516 struct Qdisc *old, struct Qdisc *new)
517{
518 if (new || old)
519 qdisc_notify(skb, n, clid, old, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
David S. Miller99194cf2008-07-17 04:54:10 -0700521 if (old) {
522 spin_lock_bh(&old->q.lock);
523 qdisc_destroy(old);
524 spin_unlock_bh(&old->q.lock);
525 }
526}
527
528/* Graft qdisc "new" to class "classid" of qdisc "parent" or
529 * to device "dev".
530 *
531 * When appropriate send a netlink notification using 'skb'
532 * and "n".
533 *
534 * On success, destroy old qdisc.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 */
536
537static int qdisc_graft(struct net_device *dev, struct Qdisc *parent,
David S. Miller99194cf2008-07-17 04:54:10 -0700538 struct sk_buff *skb, struct nlmsghdr *n, u32 classid,
539 struct Qdisc *new, struct Qdisc *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
David S. Miller99194cf2008-07-17 04:54:10 -0700541 struct Qdisc *q = old;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900544 if (parent == NULL) {
David S. Miller99194cf2008-07-17 04:54:10 -0700545 unsigned int i, num_q, ingress;
546
547 ingress = 0;
548 num_q = dev->num_tx_queues;
549 if (q && q->flags & TCQ_F_INGRESS) {
550 num_q = 1;
551 ingress = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 }
David S. Miller99194cf2008-07-17 04:54:10 -0700553
554 if (dev->flags & IFF_UP)
555 dev_deactivate(dev);
556
557 for (i = 0; i < num_q; i++) {
558 struct netdev_queue *dev_queue = &dev->rx_queue;
559
560 if (!ingress)
561 dev_queue = netdev_get_tx_queue(dev, i);
562
563 if (ingress) {
564 old = dev_graft_qdisc(dev_queue, q);
565 } else {
566 old = dev_graft_qdisc(dev_queue, new);
567 if (new && i > 0)
568 atomic_inc(&new->refcnt);
569 }
570 notify_and_destroy(skb, n, classid, old, new);
571 }
572
573 if (dev->flags & IFF_UP)
574 dev_activate(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 } else {
Eric Dumazet20fea082007-11-14 01:44:41 -0800576 const struct Qdisc_class_ops *cops = parent->ops->cl_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
578 err = -EINVAL;
579
580 if (cops) {
581 unsigned long cl = cops->get(parent, classid);
582 if (cl) {
David S. Miller99194cf2008-07-17 04:54:10 -0700583 err = cops->graft(parent, cl, new, &old);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 cops->put(parent, cl);
585 }
586 }
David S. Miller99194cf2008-07-17 04:54:10 -0700587 if (!err)
588 notify_and_destroy(skb, n, classid, old, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 }
590 return err;
591}
592
593/*
594 Allocate and initialize new qdisc.
595
596 Parameters are passed via opt.
597 */
598
599static struct Qdisc *
David S. Millerbb949fb2008-07-08 16:55:56 -0700600qdisc_create(struct net_device *dev, struct netdev_queue *dev_queue,
601 u32 parent, u32 handle, struct nlattr **tca, int *errp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
603 int err;
Patrick McHardy1e904742008-01-22 22:11:17 -0800604 struct nlattr *kind = tca[TCA_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 struct Qdisc *sch;
606 struct Qdisc_ops *ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
608 ops = qdisc_lookup_ops(kind);
609#ifdef CONFIG_KMOD
610 if (ops == NULL && kind != NULL) {
611 char name[IFNAMSIZ];
Patrick McHardy1e904742008-01-22 22:11:17 -0800612 if (nla_strlcpy(name, kind, IFNAMSIZ) < IFNAMSIZ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 /* We dropped the RTNL semaphore in order to
614 * perform the module load. So, even if we
615 * succeeded in loading the module we have to
616 * tell the caller to replay the request. We
617 * indicate this using -EAGAIN.
618 * We replay the request because the device may
619 * go away in the mean time.
620 */
621 rtnl_unlock();
622 request_module("sch_%s", name);
623 rtnl_lock();
624 ops = qdisc_lookup_ops(kind);
625 if (ops != NULL) {
626 /* We will try again qdisc_lookup_ops,
627 * so don't keep a reference.
628 */
629 module_put(ops->owner);
630 err = -EAGAIN;
631 goto err_out;
632 }
633 }
634 }
635#endif
636
Jamal Hadi Salimb9e2cc02006-08-03 16:36:51 -0700637 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 if (ops == NULL)
639 goto err_out;
640
David S. Miller5ce2d482008-07-08 17:06:30 -0700641 sch = qdisc_alloc(dev_queue, ops);
Thomas Graf3d54b822005-07-05 14:15:09 -0700642 if (IS_ERR(sch)) {
643 err = PTR_ERR(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 goto err_out2;
Thomas Graf3d54b822005-07-05 14:15:09 -0700645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Patrick McHardyffc8fef2007-07-30 17:11:50 -0700647 sch->parent = parent;
648
Thomas Graf3d54b822005-07-05 14:15:09 -0700649 if (handle == TC_H_INGRESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 sch->flags |= TCQ_F_INGRESS;
Thomas Graf3d54b822005-07-05 14:15:09 -0700651 handle = TC_H_MAKE(TC_H_INGRESS, 0);
Patrick McHardyfd44de72007-04-16 17:07:08 -0700652 } else {
Patrick McHardyfd44de72007-04-16 17:07:08 -0700653 if (handle == 0) {
654 handle = qdisc_alloc_handle(dev);
655 err = -ENOMEM;
656 if (handle == 0)
657 goto err_out3;
658 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 }
660
Thomas Graf3d54b822005-07-05 14:15:09 -0700661 sch->handle = handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Patrick McHardy1e904742008-01-22 22:11:17 -0800663 if (!ops->init || (err = ops->init(sch, tca[TCA_OPTIONS])) == 0) {
664 if (tca[TCA_RATE]) {
Thomas Graf023e09a2005-07-05 14:15:53 -0700665 err = gen_new_estimator(&sch->bstats, &sch->rate_est,
David S. Miller7698b4f2008-07-16 01:42:40 -0700666 qdisc_root_lock(sch),
Patrick McHardy1e904742008-01-22 22:11:17 -0800667 tca[TCA_RATE]);
Thomas Graf023e09a2005-07-05 14:15:53 -0700668 if (err) {
669 /*
670 * Any broken qdiscs that would require
671 * a ops->reset() here? The qdisc was never
672 * in action so it shouldn't be necessary.
673 */
674 if (ops->destroy)
675 ops->destroy(sch);
676 goto err_out3;
677 }
678 }
David S. Milleread81cc2008-07-17 00:50:32 -0700679 spin_lock_bh(&dev->qdisc_list_lock);
680 list_add_tail(&sch->list, &dev->qdisc_list);
681 spin_unlock_bh(&dev->qdisc_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 return sch;
684 }
685err_out3:
686 dev_put(dev);
Thomas Graf3d54b822005-07-05 14:15:09 -0700687 kfree((char *) sch - sch->padded);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688err_out2:
689 module_put(ops->owner);
690err_out:
691 *errp = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 return NULL;
693}
694
Patrick McHardy1e904742008-01-22 22:11:17 -0800695static int qdisc_change(struct Qdisc *sch, struct nlattr **tca)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696{
Patrick McHardy1e904742008-01-22 22:11:17 -0800697 if (tca[TCA_OPTIONS]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 int err;
699
700 if (sch->ops->change == NULL)
701 return -EINVAL;
Patrick McHardy1e904742008-01-22 22:11:17 -0800702 err = sch->ops->change(sch, tca[TCA_OPTIONS]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 if (err)
704 return err;
705 }
Patrick McHardy1e904742008-01-22 22:11:17 -0800706 if (tca[TCA_RATE])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 gen_replace_estimator(&sch->bstats, &sch->rate_est,
David S. Miller7698b4f2008-07-16 01:42:40 -0700708 qdisc_root_lock(sch), tca[TCA_RATE]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 return 0;
710}
711
712struct check_loop_arg
713{
714 struct qdisc_walker w;
715 struct Qdisc *p;
716 int depth;
717};
718
719static int check_loop_fn(struct Qdisc *q, unsigned long cl, struct qdisc_walker *w);
720
721static int check_loop(struct Qdisc *q, struct Qdisc *p, int depth)
722{
723 struct check_loop_arg arg;
724
725 if (q->ops->cl_ops == NULL)
726 return 0;
727
728 arg.w.stop = arg.w.skip = arg.w.count = 0;
729 arg.w.fn = check_loop_fn;
730 arg.depth = depth;
731 arg.p = p;
732 q->ops->cl_ops->walk(q, &arg.w);
733 return arg.w.stop ? -ELOOP : 0;
734}
735
736static int
737check_loop_fn(struct Qdisc *q, unsigned long cl, struct qdisc_walker *w)
738{
739 struct Qdisc *leaf;
Eric Dumazet20fea082007-11-14 01:44:41 -0800740 const struct Qdisc_class_ops *cops = q->ops->cl_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 struct check_loop_arg *arg = (struct check_loop_arg *)w;
742
743 leaf = cops->leaf(q, cl);
744 if (leaf) {
745 if (leaf == arg->p || arg->depth > 7)
746 return -ELOOP;
747 return check_loop(leaf, arg->p, arg->depth + 1);
748 }
749 return 0;
750}
751
752/*
753 * Delete/get qdisc.
754 */
755
756static int tc_get_qdisc(struct sk_buff *skb, struct nlmsghdr *n, void *arg)
757{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900758 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 struct tcmsg *tcm = NLMSG_DATA(n);
Patrick McHardy1e904742008-01-22 22:11:17 -0800760 struct nlattr *tca[TCA_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 struct net_device *dev;
762 u32 clid = tcm->tcm_parent;
763 struct Qdisc *q = NULL;
764 struct Qdisc *p = NULL;
765 int err;
766
Denis V. Lunevb8542722007-12-01 00:21:31 +1100767 if (net != &init_net)
768 return -EINVAL;
769
Eric W. Biederman881d9662007-09-17 11:56:21 -0700770 if ((dev = __dev_get_by_index(&init_net, tcm->tcm_ifindex)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 return -ENODEV;
772
Patrick McHardy1e904742008-01-22 22:11:17 -0800773 err = nlmsg_parse(n, sizeof(*tcm), tca, TCA_MAX, NULL);
774 if (err < 0)
775 return err;
776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 if (clid) {
778 if (clid != TC_H_ROOT) {
779 if (TC_H_MAJ(clid) != TC_H_MAJ(TC_H_INGRESS)) {
780 if ((p = qdisc_lookup(dev, TC_H_MAJ(clid))) == NULL)
781 return -ENOENT;
782 q = qdisc_leaf(p, clid);
783 } else { /* ingress */
David S. Miller816f3252008-07-08 22:49:00 -0700784 q = dev->rx_queue.qdisc;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900785 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 } else {
David S. Millere8a04642008-07-17 00:34:19 -0700787 struct netdev_queue *dev_queue;
788 dev_queue = netdev_get_tx_queue(dev, 0);
David S. Millerb0e1e642008-07-08 17:42:10 -0700789 q = dev_queue->qdisc_sleeping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 }
791 if (!q)
792 return -ENOENT;
793
794 if (tcm->tcm_handle && q->handle != tcm->tcm_handle)
795 return -EINVAL;
796 } else {
797 if ((q = qdisc_lookup(dev, tcm->tcm_handle)) == NULL)
798 return -ENOENT;
799 }
800
Patrick McHardy1e904742008-01-22 22:11:17 -0800801 if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], q->ops->id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 return -EINVAL;
803
804 if (n->nlmsg_type == RTM_DELQDISC) {
805 if (!clid)
806 return -EINVAL;
807 if (q->handle == 0)
808 return -ENOENT;
David S. Miller99194cf2008-07-17 04:54:10 -0700809 if ((err = qdisc_graft(dev, p, skb, n, clid, NULL, q)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 } else {
812 qdisc_notify(skb, n, clid, NULL, q);
813 }
814 return 0;
815}
816
817/*
818 Create/change qdisc.
819 */
820
821static int tc_modify_qdisc(struct sk_buff *skb, struct nlmsghdr *n, void *arg)
822{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900823 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 struct tcmsg *tcm;
Patrick McHardy1e904742008-01-22 22:11:17 -0800825 struct nlattr *tca[TCA_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 struct net_device *dev;
827 u32 clid;
828 struct Qdisc *q, *p;
829 int err;
830
Denis V. Lunevb8542722007-12-01 00:21:31 +1100831 if (net != &init_net)
832 return -EINVAL;
833
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834replay:
835 /* Reinit, just in case something touches this. */
836 tcm = NLMSG_DATA(n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 clid = tcm->tcm_parent;
838 q = p = NULL;
839
Eric W. Biederman881d9662007-09-17 11:56:21 -0700840 if ((dev = __dev_get_by_index(&init_net, tcm->tcm_ifindex)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 return -ENODEV;
842
Patrick McHardy1e904742008-01-22 22:11:17 -0800843 err = nlmsg_parse(n, sizeof(*tcm), tca, TCA_MAX, NULL);
844 if (err < 0)
845 return err;
846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 if (clid) {
848 if (clid != TC_H_ROOT) {
849 if (clid != TC_H_INGRESS) {
850 if ((p = qdisc_lookup(dev, TC_H_MAJ(clid))) == NULL)
851 return -ENOENT;
852 q = qdisc_leaf(p, clid);
853 } else { /*ingress */
David S. Miller816f3252008-07-08 22:49:00 -0700854 q = dev->rx_queue.qdisc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 }
856 } else {
David S. Millere8a04642008-07-17 00:34:19 -0700857 struct netdev_queue *dev_queue;
858 dev_queue = netdev_get_tx_queue(dev, 0);
David S. Millerb0e1e642008-07-08 17:42:10 -0700859 q = dev_queue->qdisc_sleeping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 }
861
862 /* It may be default qdisc, ignore it */
863 if (q && q->handle == 0)
864 q = NULL;
865
866 if (!q || !tcm->tcm_handle || q->handle != tcm->tcm_handle) {
867 if (tcm->tcm_handle) {
868 if (q && !(n->nlmsg_flags&NLM_F_REPLACE))
869 return -EEXIST;
870 if (TC_H_MIN(tcm->tcm_handle))
871 return -EINVAL;
872 if ((q = qdisc_lookup(dev, tcm->tcm_handle)) == NULL)
873 goto create_n_graft;
874 if (n->nlmsg_flags&NLM_F_EXCL)
875 return -EEXIST;
Patrick McHardy1e904742008-01-22 22:11:17 -0800876 if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], q->ops->id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 return -EINVAL;
878 if (q == p ||
879 (p && check_loop(q, p, 0)))
880 return -ELOOP;
881 atomic_inc(&q->refcnt);
882 goto graft;
883 } else {
884 if (q == NULL)
885 goto create_n_graft;
886
887 /* This magic test requires explanation.
888 *
889 * We know, that some child q is already
890 * attached to this parent and have choice:
891 * either to change it or to create/graft new one.
892 *
893 * 1. We are allowed to create/graft only
894 * if CREATE and REPLACE flags are set.
895 *
896 * 2. If EXCL is set, requestor wanted to say,
897 * that qdisc tcm_handle is not expected
898 * to exist, so that we choose create/graft too.
899 *
900 * 3. The last case is when no flags are set.
901 * Alas, it is sort of hole in API, we
902 * cannot decide what to do unambiguously.
903 * For now we select create/graft, if
904 * user gave KIND, which does not match existing.
905 */
906 if ((n->nlmsg_flags&NLM_F_CREATE) &&
907 (n->nlmsg_flags&NLM_F_REPLACE) &&
908 ((n->nlmsg_flags&NLM_F_EXCL) ||
Patrick McHardy1e904742008-01-22 22:11:17 -0800909 (tca[TCA_KIND] &&
910 nla_strcmp(tca[TCA_KIND], q->ops->id))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 goto create_n_graft;
912 }
913 }
914 } else {
915 if (!tcm->tcm_handle)
916 return -EINVAL;
917 q = qdisc_lookup(dev, tcm->tcm_handle);
918 }
919
920 /* Change qdisc parameters */
921 if (q == NULL)
922 return -ENOENT;
923 if (n->nlmsg_flags&NLM_F_EXCL)
924 return -EEXIST;
Patrick McHardy1e904742008-01-22 22:11:17 -0800925 if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], q->ops->id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 return -EINVAL;
927 err = qdisc_change(q, tca);
928 if (err == 0)
929 qdisc_notify(skb, n, clid, NULL, q);
930 return err;
931
932create_n_graft:
933 if (!(n->nlmsg_flags&NLM_F_CREATE))
934 return -ENOENT;
935 if (clid == TC_H_INGRESS)
David S. Millerbb949fb2008-07-08 16:55:56 -0700936 q = qdisc_create(dev, &dev->rx_queue,
937 tcm->tcm_parent, tcm->tcm_parent,
Patrick McHardyffc8fef2007-07-30 17:11:50 -0700938 tca, &err);
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900939 else
David S. Millere8a04642008-07-17 00:34:19 -0700940 q = qdisc_create(dev, netdev_get_tx_queue(dev, 0),
David S. Millerbb949fb2008-07-08 16:55:56 -0700941 tcm->tcm_parent, tcm->tcm_handle,
Patrick McHardyffc8fef2007-07-30 17:11:50 -0700942 tca, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 if (q == NULL) {
944 if (err == -EAGAIN)
945 goto replay;
946 return err;
947 }
948
949graft:
950 if (1) {
David S. Miller53049972008-07-16 03:00:19 -0700951 spinlock_t *root_lock;
952
David S. Miller99194cf2008-07-17 04:54:10 -0700953 err = qdisc_graft(dev, p, skb, n, clid, q, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 if (err) {
955 if (q) {
David S. Miller53049972008-07-16 03:00:19 -0700956 root_lock = qdisc_root_lock(q);
957 spin_lock_bh(root_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 qdisc_destroy(q);
David S. Miller53049972008-07-16 03:00:19 -0700959 spin_unlock_bh(root_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 }
961 return err;
962 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 }
964 return 0;
965}
966
967static int tc_fill_qdisc(struct sk_buff *skb, struct Qdisc *q, u32 clid,
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -0700968 u32 pid, u32 seq, u16 flags, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969{
970 struct tcmsg *tcm;
971 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700972 unsigned char *b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 struct gnet_dump d;
974
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -0700975 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*tcm), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 tcm = NLMSG_DATA(nlh);
977 tcm->tcm_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700978 tcm->tcm__pad1 = 0;
979 tcm->tcm__pad2 = 0;
David S. Miller5ce2d482008-07-08 17:06:30 -0700980 tcm->tcm_ifindex = qdisc_dev(q)->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 tcm->tcm_parent = clid;
982 tcm->tcm_handle = q->handle;
983 tcm->tcm_info = atomic_read(&q->refcnt);
Patrick McHardy57e1c482008-01-23 20:34:28 -0800984 NLA_PUT_STRING(skb, TCA_KIND, q->ops->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 if (q->ops->dump && q->ops->dump(q, skb) < 0)
Patrick McHardy1e904742008-01-22 22:11:17 -0800986 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 q->qstats.qlen = q->q.qlen;
988
989 if (gnet_stats_start_copy_compat(skb, TCA_STATS2, TCA_STATS,
David S. Miller7698b4f2008-07-16 01:42:40 -0700990 TCA_XSTATS, qdisc_root_lock(q), &d) < 0)
Patrick McHardy1e904742008-01-22 22:11:17 -0800991 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
993 if (q->ops->dump_stats && q->ops->dump_stats(q, &d) < 0)
Patrick McHardy1e904742008-01-22 22:11:17 -0800994 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
996 if (gnet_stats_copy_basic(&d, &q->bstats) < 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 gnet_stats_copy_rate_est(&d, &q->rate_est) < 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 gnet_stats_copy_queue(&d, &q->qstats) < 0)
Patrick McHardy1e904742008-01-22 22:11:17 -0800999 goto nla_put_failure;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +09001000
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 if (gnet_stats_finish_copy(&d) < 0)
Patrick McHardy1e904742008-01-22 22:11:17 -08001002 goto nla_put_failure;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +09001003
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001004 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 return skb->len;
1006
1007nlmsg_failure:
Patrick McHardy1e904742008-01-22 22:11:17 -08001008nla_put_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001009 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 return -1;
1011}
1012
1013static int qdisc_notify(struct sk_buff *oskb, struct nlmsghdr *n,
1014 u32 clid, struct Qdisc *old, struct Qdisc *new)
1015{
1016 struct sk_buff *skb;
1017 u32 pid = oskb ? NETLINK_CB(oskb).pid : 0;
1018
1019 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1020 if (!skb)
1021 return -ENOBUFS;
1022
1023 if (old && old->handle) {
1024 if (tc_fill_qdisc(skb, old, clid, pid, n->nlmsg_seq, 0, RTM_DELQDISC) < 0)
1025 goto err_out;
1026 }
1027 if (new) {
1028 if (tc_fill_qdisc(skb, new, clid, pid, n->nlmsg_seq, old ? NLM_F_REPLACE : 0, RTM_NEWQDISC) < 0)
1029 goto err_out;
1030 }
1031
1032 if (skb->len)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08001033 return rtnetlink_send(skb, &init_net, pid, RTNLGRP_TC, n->nlmsg_flags&NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
1035err_out:
1036 kfree_skb(skb);
1037 return -EINVAL;
1038}
1039
1040static int tc_dump_qdisc(struct sk_buff *skb, struct netlink_callback *cb)
1041{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001042 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 int idx, q_idx;
1044 int s_idx, s_q_idx;
1045 struct net_device *dev;
1046 struct Qdisc *q;
1047
Denis V. Lunevb8542722007-12-01 00:21:31 +11001048 if (net != &init_net)
1049 return 0;
1050
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 s_idx = cb->args[0];
1052 s_q_idx = q_idx = cb->args[1];
1053 read_lock(&dev_base_lock);
Pavel Emelianov7562f872007-05-03 15:13:45 -07001054 idx = 0;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001055 for_each_netdev(&init_net, dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 if (idx < s_idx)
Pavel Emelianov7562f872007-05-03 15:13:45 -07001057 goto cont;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 if (idx > s_idx)
1059 s_q_idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 q_idx = 0;
David S. Milleread81cc2008-07-17 00:50:32 -07001061 list_for_each_entry(q, &dev->qdisc_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 if (q_idx < s_q_idx) {
1063 q_idx++;
1064 continue;
1065 }
1066 if (tc_fill_qdisc(skb, q, q->parent, NETLINK_CB(cb->skb).pid,
Patrick McHardy0463d4a2007-04-16 17:02:10 -07001067 cb->nlh->nlmsg_seq, NLM_F_MULTI, RTM_NEWQDISC) <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 q_idx++;
1070 }
Pavel Emelianov7562f872007-05-03 15:13:45 -07001071cont:
1072 idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 }
1074
1075done:
1076 read_unlock(&dev_base_lock);
1077
1078 cb->args[0] = idx;
1079 cb->args[1] = q_idx;
1080
1081 return skb->len;
1082}
1083
1084
1085
1086/************************************************
1087 * Traffic classes manipulation. *
1088 ************************************************/
1089
1090
1091
1092static int tc_ctl_tclass(struct sk_buff *skb, struct nlmsghdr *n, void *arg)
1093{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001094 struct net *net = sock_net(skb->sk);
David S. Millerb0e1e642008-07-08 17:42:10 -07001095 struct netdev_queue *dev_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 struct tcmsg *tcm = NLMSG_DATA(n);
Patrick McHardy1e904742008-01-22 22:11:17 -08001097 struct nlattr *tca[TCA_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 struct net_device *dev;
1099 struct Qdisc *q = NULL;
Eric Dumazet20fea082007-11-14 01:44:41 -08001100 const struct Qdisc_class_ops *cops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 unsigned long cl = 0;
1102 unsigned long new_cl;
1103 u32 pid = tcm->tcm_parent;
1104 u32 clid = tcm->tcm_handle;
1105 u32 qid = TC_H_MAJ(clid);
1106 int err;
1107
Denis V. Lunevb8542722007-12-01 00:21:31 +11001108 if (net != &init_net)
1109 return -EINVAL;
1110
Eric W. Biederman881d9662007-09-17 11:56:21 -07001111 if ((dev = __dev_get_by_index(&init_net, tcm->tcm_ifindex)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 return -ENODEV;
1113
Patrick McHardy1e904742008-01-22 22:11:17 -08001114 err = nlmsg_parse(n, sizeof(*tcm), tca, TCA_MAX, NULL);
1115 if (err < 0)
1116 return err;
1117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 /*
1119 parent == TC_H_UNSPEC - unspecified parent.
1120 parent == TC_H_ROOT - class is root, which has no parent.
1121 parent == X:0 - parent is root class.
1122 parent == X:Y - parent is a node in hierarchy.
1123 parent == 0:Y - parent is X:Y, where X:0 is qdisc.
1124
1125 handle == 0:0 - generate handle from kernel pool.
1126 handle == 0:Y - class is X:Y, where X:0 is qdisc.
1127 handle == X:Y - clear.
1128 handle == X:0 - root class.
1129 */
1130
1131 /* Step 1. Determine qdisc handle X:0 */
1132
David S. Millere8a04642008-07-17 00:34:19 -07001133 dev_queue = netdev_get_tx_queue(dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 if (pid != TC_H_ROOT) {
1135 u32 qid1 = TC_H_MAJ(pid);
1136
1137 if (qid && qid1) {
1138 /* If both majors are known, they must be identical. */
1139 if (qid != qid1)
1140 return -EINVAL;
1141 } else if (qid1) {
1142 qid = qid1;
1143 } else if (qid == 0)
David S. Millerb0e1e642008-07-08 17:42:10 -07001144 qid = dev_queue->qdisc_sleeping->handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
1146 /* Now qid is genuine qdisc handle consistent
1147 both with parent and child.
1148
1149 TC_H_MAJ(pid) still may be unspecified, complete it now.
1150 */
1151 if (pid)
1152 pid = TC_H_MAKE(qid, pid);
1153 } else {
1154 if (qid == 0)
David S. Millerb0e1e642008-07-08 17:42:10 -07001155 qid = dev_queue->qdisc_sleeping->handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 }
1157
1158 /* OK. Locate qdisc */
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +09001159 if ((q = qdisc_lookup(dev, qid)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 return -ENOENT;
1161
1162 /* An check that it supports classes */
1163 cops = q->ops->cl_ops;
1164 if (cops == NULL)
1165 return -EINVAL;
1166
1167 /* Now try to get class */
1168 if (clid == 0) {
1169 if (pid == TC_H_ROOT)
1170 clid = qid;
1171 } else
1172 clid = TC_H_MAKE(qid, clid);
1173
1174 if (clid)
1175 cl = cops->get(q, clid);
1176
1177 if (cl == 0) {
1178 err = -ENOENT;
1179 if (n->nlmsg_type != RTM_NEWTCLASS || !(n->nlmsg_flags&NLM_F_CREATE))
1180 goto out;
1181 } else {
1182 switch (n->nlmsg_type) {
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +09001183 case RTM_NEWTCLASS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 err = -EEXIST;
1185 if (n->nlmsg_flags&NLM_F_EXCL)
1186 goto out;
1187 break;
1188 case RTM_DELTCLASS:
1189 err = cops->delete(q, cl);
1190 if (err == 0)
1191 tclass_notify(skb, n, q, cl, RTM_DELTCLASS);
1192 goto out;
1193 case RTM_GETTCLASS:
1194 err = tclass_notify(skb, n, q, cl, RTM_NEWTCLASS);
1195 goto out;
1196 default:
1197 err = -EINVAL;
1198 goto out;
1199 }
1200 }
1201
1202 new_cl = cl;
1203 err = cops->change(q, clid, pid, tca, &new_cl);
1204 if (err == 0)
1205 tclass_notify(skb, n, q, new_cl, RTM_NEWTCLASS);
1206
1207out:
1208 if (cl)
1209 cops->put(q, cl);
1210
1211 return err;
1212}
1213
1214
1215static int tc_fill_tclass(struct sk_buff *skb, struct Qdisc *q,
1216 unsigned long cl,
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -07001217 u32 pid, u32 seq, u16 flags, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218{
1219 struct tcmsg *tcm;
1220 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001221 unsigned char *b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 struct gnet_dump d;
Eric Dumazet20fea082007-11-14 01:44:41 -08001223 const struct Qdisc_class_ops *cl_ops = q->ops->cl_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -07001225 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*tcm), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 tcm = NLMSG_DATA(nlh);
1227 tcm->tcm_family = AF_UNSPEC;
David S. Miller5ce2d482008-07-08 17:06:30 -07001228 tcm->tcm_ifindex = qdisc_dev(q)->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 tcm->tcm_parent = q->handle;
1230 tcm->tcm_handle = q->handle;
1231 tcm->tcm_info = 0;
Patrick McHardy57e1c482008-01-23 20:34:28 -08001232 NLA_PUT_STRING(skb, TCA_KIND, q->ops->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 if (cl_ops->dump && cl_ops->dump(q, cl, skb, tcm) < 0)
Patrick McHardy1e904742008-01-22 22:11:17 -08001234 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
1236 if (gnet_stats_start_copy_compat(skb, TCA_STATS2, TCA_STATS,
David S. Miller7698b4f2008-07-16 01:42:40 -07001237 TCA_XSTATS, qdisc_root_lock(q), &d) < 0)
Patrick McHardy1e904742008-01-22 22:11:17 -08001238 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
1240 if (cl_ops->dump_stats && cl_ops->dump_stats(q, cl, &d) < 0)
Patrick McHardy1e904742008-01-22 22:11:17 -08001241 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
1243 if (gnet_stats_finish_copy(&d) < 0)
Patrick McHardy1e904742008-01-22 22:11:17 -08001244 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001246 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 return skb->len;
1248
1249nlmsg_failure:
Patrick McHardy1e904742008-01-22 22:11:17 -08001250nla_put_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001251 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 return -1;
1253}
1254
1255static int tclass_notify(struct sk_buff *oskb, struct nlmsghdr *n,
1256 struct Qdisc *q, unsigned long cl, int event)
1257{
1258 struct sk_buff *skb;
1259 u32 pid = oskb ? NETLINK_CB(oskb).pid : 0;
1260
1261 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1262 if (!skb)
1263 return -ENOBUFS;
1264
1265 if (tc_fill_tclass(skb, q, cl, pid, n->nlmsg_seq, 0, event) < 0) {
1266 kfree_skb(skb);
1267 return -EINVAL;
1268 }
1269
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08001270 return rtnetlink_send(skb, &init_net, pid, RTNLGRP_TC, n->nlmsg_flags&NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271}
1272
1273struct qdisc_dump_args
1274{
1275 struct qdisc_walker w;
1276 struct sk_buff *skb;
1277 struct netlink_callback *cb;
1278};
1279
1280static int qdisc_class_dump(struct Qdisc *q, unsigned long cl, struct qdisc_walker *arg)
1281{
1282 struct qdisc_dump_args *a = (struct qdisc_dump_args *)arg;
1283
1284 return tc_fill_tclass(a->skb, q, cl, NETLINK_CB(a->cb->skb).pid,
1285 a->cb->nlh->nlmsg_seq, NLM_F_MULTI, RTM_NEWTCLASS);
1286}
1287
1288static int tc_dump_tclass(struct sk_buff *skb, struct netlink_callback *cb)
1289{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001290 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 int t;
1292 int s_t;
1293 struct net_device *dev;
1294 struct Qdisc *q;
1295 struct tcmsg *tcm = (struct tcmsg*)NLMSG_DATA(cb->nlh);
1296 struct qdisc_dump_args arg;
1297
Denis V. Lunevb8542722007-12-01 00:21:31 +11001298 if (net != &init_net)
1299 return 0;
1300
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 if (cb->nlh->nlmsg_len < NLMSG_LENGTH(sizeof(*tcm)))
1302 return 0;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001303 if ((dev = dev_get_by_index(&init_net, tcm->tcm_ifindex)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 return 0;
1305
1306 s_t = cb->args[0];
1307 t = 0;
1308
David S. Milleread81cc2008-07-17 00:50:32 -07001309 list_for_each_entry(q, &dev->qdisc_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 if (t < s_t || !q->ops->cl_ops ||
1311 (tcm->tcm_parent &&
1312 TC_H_MAJ(tcm->tcm_parent) != q->handle)) {
1313 t++;
1314 continue;
1315 }
1316 if (t > s_t)
1317 memset(&cb->args[1], 0, sizeof(cb->args)-sizeof(cb->args[0]));
1318 arg.w.fn = qdisc_class_dump;
1319 arg.skb = skb;
1320 arg.cb = cb;
1321 arg.w.stop = 0;
1322 arg.w.skip = cb->args[1];
1323 arg.w.count = 0;
1324 q->ops->cl_ops->walk(q, &arg.w);
1325 cb->args[1] = arg.w.count;
1326 if (arg.w.stop)
1327 break;
1328 t++;
1329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
1331 cb->args[0] = t;
1332
1333 dev_put(dev);
1334 return skb->len;
1335}
1336
1337/* Main classifier routine: scans classifier chain attached
1338 to this qdisc, (optionally) tests for protocol and asks
1339 specific classifiers.
1340 */
Patrick McHardy73ca4912007-07-15 00:02:31 -07001341int tc_classify_compat(struct sk_buff *skb, struct tcf_proto *tp,
1342 struct tcf_result *res)
1343{
1344 __be16 protocol = skb->protocol;
1345 int err = 0;
1346
1347 for (; tp; tp = tp->next) {
1348 if ((tp->protocol == protocol ||
1349 tp->protocol == htons(ETH_P_ALL)) &&
1350 (err = tp->classify(skb, tp, res)) >= 0) {
1351#ifdef CONFIG_NET_CLS_ACT
1352 if (err != TC_ACT_RECLASSIFY && skb->tc_verd)
1353 skb->tc_verd = SET_TC_VERD(skb->tc_verd, 0);
1354#endif
1355 return err;
1356 }
1357 }
1358 return -1;
1359}
1360EXPORT_SYMBOL(tc_classify_compat);
1361
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362int tc_classify(struct sk_buff *skb, struct tcf_proto *tp,
Patrick McHardy73ca4912007-07-15 00:02:31 -07001363 struct tcf_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364{
1365 int err = 0;
Patrick McHardy73ca4912007-07-15 00:02:31 -07001366 __be16 protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367#ifdef CONFIG_NET_CLS_ACT
1368 struct tcf_proto *otp = tp;
1369reclassify:
1370#endif
1371 protocol = skb->protocol;
1372
Patrick McHardy73ca4912007-07-15 00:02:31 -07001373 err = tc_classify_compat(skb, tp, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374#ifdef CONFIG_NET_CLS_ACT
Patrick McHardy73ca4912007-07-15 00:02:31 -07001375 if (err == TC_ACT_RECLASSIFY) {
1376 u32 verd = G_TC_VERD(skb->tc_verd);
1377 tp = otp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
Patrick McHardy73ca4912007-07-15 00:02:31 -07001379 if (verd++ >= MAX_REC_LOOP) {
1380 printk("rule prio %u protocol %02x reclassify loop, "
1381 "packet dropped\n",
1382 tp->prio&0xffff, ntohs(tp->protocol));
1383 return TC_ACT_SHOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 }
Patrick McHardy73ca4912007-07-15 00:02:31 -07001385 skb->tc_verd = SET_TC_VERD(skb->tc_verd, verd);
1386 goto reclassify;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 }
Patrick McHardy73ca4912007-07-15 00:02:31 -07001388#endif
1389 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390}
Patrick McHardy73ca4912007-07-15 00:02:31 -07001391EXPORT_SYMBOL(tc_classify);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
Patrick McHardya48b5a62007-03-23 11:29:43 -07001393void tcf_destroy(struct tcf_proto *tp)
1394{
1395 tp->ops->destroy(tp);
1396 module_put(tp->ops->owner);
1397 kfree(tp);
1398}
1399
Patrick McHardyff31ab52008-07-01 19:52:38 -07001400void tcf_destroy_chain(struct tcf_proto **fl)
Patrick McHardya48b5a62007-03-23 11:29:43 -07001401{
1402 struct tcf_proto *tp;
1403
Patrick McHardyff31ab52008-07-01 19:52:38 -07001404 while ((tp = *fl) != NULL) {
1405 *fl = tp->next;
Patrick McHardya48b5a62007-03-23 11:29:43 -07001406 tcf_destroy(tp);
1407 }
1408}
1409EXPORT_SYMBOL(tcf_destroy_chain);
1410
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411#ifdef CONFIG_PROC_FS
1412static int psched_show(struct seq_file *seq, void *v)
1413{
Patrick McHardy3c0cfc12007-10-10 16:32:41 -07001414 struct timespec ts;
1415
1416 hrtimer_get_res(CLOCK_MONOTONIC, &ts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 seq_printf(seq, "%08x %08x %08x %08x\n",
Patrick McHardy641b9e02007-03-16 01:18:42 -07001418 (u32)NSEC_PER_USEC, (u32)PSCHED_US2NS(1),
Patrick McHardy514bca32007-03-16 12:34:52 -07001419 1000000,
Patrick McHardy3c0cfc12007-10-10 16:32:41 -07001420 (u32)NSEC_PER_SEC/(u32)ktime_to_ns(timespec_to_ktime(ts)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
1422 return 0;
1423}
1424
1425static int psched_open(struct inode *inode, struct file *file)
1426{
1427 return single_open(file, psched_show, PDE(inode)->data);
1428}
1429
Arjan van de Venda7071d2007-02-12 00:55:36 -08001430static const struct file_operations psched_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 .owner = THIS_MODULE,
1432 .open = psched_open,
1433 .read = seq_read,
1434 .llseek = seq_lseek,
1435 .release = single_release,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +09001436};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437#endif
1438
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439static int __init pktsched_init(void)
1440{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 register_qdisc(&pfifo_qdisc_ops);
1442 register_qdisc(&bfifo_qdisc_ops);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02001443 proc_net_fops_create(&init_net, "psched", 0, &psched_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
Thomas Grafbe577dd2007-03-22 11:55:50 -07001445 rtnl_register(PF_UNSPEC, RTM_NEWQDISC, tc_modify_qdisc, NULL);
1446 rtnl_register(PF_UNSPEC, RTM_DELQDISC, tc_get_qdisc, NULL);
1447 rtnl_register(PF_UNSPEC, RTM_GETQDISC, tc_get_qdisc, tc_dump_qdisc);
1448 rtnl_register(PF_UNSPEC, RTM_NEWTCLASS, tc_ctl_tclass, NULL);
1449 rtnl_register(PF_UNSPEC, RTM_DELTCLASS, tc_ctl_tclass, NULL);
1450 rtnl_register(PF_UNSPEC, RTM_GETTCLASS, tc_ctl_tclass, tc_dump_tclass);
1451
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 return 0;
1453}
1454
1455subsys_initcall(pktsched_init);