blob: 1f893082a4f67888828291c785365d1c35b69e08 [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{
188 struct Qdisc *q;
189
190 list_for_each_entry(q, &dev->qdisc_list, list) {
191 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);
David S. Miller5ce2d482008-07-08 17:06:30 -0700284 struct net_device *dev = qdisc_dev(wd->qdisc);
Patrick McHardy41794772007-03-16 01:19:15 -0700285
286 wd->qdisc->flags &= ~TCQ_F_THROTTLED;
Stephen Hemminger11274e52007-03-22 12:17:42 -0700287 smp_wmb();
Patrick McHardy0621ed22007-07-14 20:49:26 -0700288 netif_schedule(dev);
Stephen Hemminger19365022007-03-22 12:18:35 -0700289
Patrick McHardy41794772007-03-16 01:19:15 -0700290 return HRTIMER_NORESTART;
291}
292
293void qdisc_watchdog_init(struct qdisc_watchdog *wd, struct Qdisc *qdisc)
294{
295 hrtimer_init(&wd->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
296 wd->timer.function = qdisc_watchdog;
297 wd->qdisc = qdisc;
298}
299EXPORT_SYMBOL(qdisc_watchdog_init);
300
301void qdisc_watchdog_schedule(struct qdisc_watchdog *wd, psched_time_t expires)
302{
303 ktime_t time;
304
305 wd->qdisc->flags |= TCQ_F_THROTTLED;
306 time = ktime_set(0, 0);
307 time = ktime_add_ns(time, PSCHED_US2NS(expires));
308 hrtimer_start(&wd->timer, time, HRTIMER_MODE_ABS);
309}
310EXPORT_SYMBOL(qdisc_watchdog_schedule);
311
312void qdisc_watchdog_cancel(struct qdisc_watchdog *wd)
313{
314 hrtimer_cancel(&wd->timer);
315 wd->qdisc->flags &= ~TCQ_F_THROTTLED;
316}
317EXPORT_SYMBOL(qdisc_watchdog_cancel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Patrick McHardy6fe1c7a2008-07-05 23:21:31 -0700319struct hlist_head *qdisc_class_hash_alloc(unsigned int n)
320{
321 unsigned int size = n * sizeof(struct hlist_head), i;
322 struct hlist_head *h;
323
324 if (size <= PAGE_SIZE)
325 h = kmalloc(size, GFP_KERNEL);
326 else
327 h = (struct hlist_head *)
328 __get_free_pages(GFP_KERNEL, get_order(size));
329
330 if (h != NULL) {
331 for (i = 0; i < n; i++)
332 INIT_HLIST_HEAD(&h[i]);
333 }
334 return h;
335}
336
337static void qdisc_class_hash_free(struct hlist_head *h, unsigned int n)
338{
339 unsigned int size = n * sizeof(struct hlist_head);
340
341 if (size <= PAGE_SIZE)
342 kfree(h);
343 else
344 free_pages((unsigned long)h, get_order(size));
345}
346
347void qdisc_class_hash_grow(struct Qdisc *sch, struct Qdisc_class_hash *clhash)
348{
349 struct Qdisc_class_common *cl;
350 struct hlist_node *n, *next;
351 struct hlist_head *nhash, *ohash;
352 unsigned int nsize, nmask, osize;
353 unsigned int i, h;
354
355 /* Rehash when load factor exceeds 0.75 */
356 if (clhash->hashelems * 4 <= clhash->hashsize * 3)
357 return;
358 nsize = clhash->hashsize * 2;
359 nmask = nsize - 1;
360 nhash = qdisc_class_hash_alloc(nsize);
361 if (nhash == NULL)
362 return;
363
364 ohash = clhash->hash;
365 osize = clhash->hashsize;
366
367 sch_tree_lock(sch);
368 for (i = 0; i < osize; i++) {
369 hlist_for_each_entry_safe(cl, n, next, &ohash[i], hnode) {
370 h = qdisc_class_hash(cl->classid, nmask);
371 hlist_add_head(&cl->hnode, &nhash[h]);
372 }
373 }
374 clhash->hash = nhash;
375 clhash->hashsize = nsize;
376 clhash->hashmask = nmask;
377 sch_tree_unlock(sch);
378
379 qdisc_class_hash_free(ohash, osize);
380}
381EXPORT_SYMBOL(qdisc_class_hash_grow);
382
383int qdisc_class_hash_init(struct Qdisc_class_hash *clhash)
384{
385 unsigned int size = 4;
386
387 clhash->hash = qdisc_class_hash_alloc(size);
388 if (clhash->hash == NULL)
389 return -ENOMEM;
390 clhash->hashsize = size;
391 clhash->hashmask = size - 1;
392 clhash->hashelems = 0;
393 return 0;
394}
395EXPORT_SYMBOL(qdisc_class_hash_init);
396
397void qdisc_class_hash_destroy(struct Qdisc_class_hash *clhash)
398{
399 qdisc_class_hash_free(clhash->hash, clhash->hashsize);
400}
401EXPORT_SYMBOL(qdisc_class_hash_destroy);
402
403void qdisc_class_hash_insert(struct Qdisc_class_hash *clhash,
404 struct Qdisc_class_common *cl)
405{
406 unsigned int h;
407
408 INIT_HLIST_NODE(&cl->hnode);
409 h = qdisc_class_hash(cl->classid, clhash->hashmask);
410 hlist_add_head(&cl->hnode, &clhash->hash[h]);
411 clhash->hashelems++;
412}
413EXPORT_SYMBOL(qdisc_class_hash_insert);
414
415void qdisc_class_hash_remove(struct Qdisc_class_hash *clhash,
416 struct Qdisc_class_common *cl)
417{
418 hlist_del(&cl->hnode);
419 clhash->hashelems--;
420}
421EXPORT_SYMBOL(qdisc_class_hash_remove);
422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423/* Allocate an unique handle from space managed by kernel */
424
425static u32 qdisc_alloc_handle(struct net_device *dev)
426{
427 int i = 0x10000;
428 static u32 autohandle = TC_H_MAKE(0x80000000U, 0);
429
430 do {
431 autohandle += TC_H_MAKE(0x10000U, 0);
432 if (autohandle == TC_H_MAKE(TC_H_ROOT, 0))
433 autohandle = TC_H_MAKE(0x80000000U, 0);
434 } while (qdisc_lookup(dev, autohandle) && --i > 0);
435
436 return i>0 ? autohandle : 0;
437}
438
439/* Attach toplevel qdisc to device dev */
440
441static struct Qdisc *
442dev_graft_qdisc(struct net_device *dev, struct Qdisc *qdisc)
443{
444 struct Qdisc *oqdisc;
445
446 if (dev->flags & IFF_UP)
447 dev_deactivate(dev);
448
449 qdisc_lock_tree(dev);
450 if (qdisc && qdisc->flags&TCQ_F_INGRESS) {
451 oqdisc = dev->qdisc_ingress;
452 /* Prune old scheduler */
453 if (oqdisc && atomic_read(&oqdisc->refcnt) <= 1) {
454 /* delete */
455 qdisc_reset(oqdisc);
456 dev->qdisc_ingress = NULL;
457 } else { /* new */
458 dev->qdisc_ingress = qdisc;
459 }
460
461 } else {
462
463 oqdisc = dev->qdisc_sleeping;
464
465 /* Prune old scheduler */
466 if (oqdisc && atomic_read(&oqdisc->refcnt) <= 1)
467 qdisc_reset(oqdisc);
468
469 /* ... and graft new one */
470 if (qdisc == NULL)
471 qdisc = &noop_qdisc;
472 dev->qdisc_sleeping = qdisc;
473 dev->qdisc = &noop_qdisc;
474 }
475
476 qdisc_unlock_tree(dev);
477
478 if (dev->flags & IFF_UP)
479 dev_activate(dev);
480
481 return oqdisc;
482}
483
Patrick McHardy43effa12006-11-29 17:35:48 -0800484void qdisc_tree_decrease_qlen(struct Qdisc *sch, unsigned int n)
485{
Eric Dumazet20fea082007-11-14 01:44:41 -0800486 const struct Qdisc_class_ops *cops;
Patrick McHardy43effa12006-11-29 17:35:48 -0800487 unsigned long cl;
488 u32 parentid;
489
490 if (n == 0)
491 return;
492 while ((parentid = sch->parent)) {
Jarek Poplawski066a3b52008-04-14 15:10:42 -0700493 if (TC_H_MAJ(parentid) == TC_H_MAJ(TC_H_INGRESS))
494 return;
495
David S. Miller5ce2d482008-07-08 17:06:30 -0700496 sch = qdisc_lookup(qdisc_dev(sch), TC_H_MAJ(parentid));
Patrick McHardyffc8fef2007-07-30 17:11:50 -0700497 if (sch == NULL) {
498 WARN_ON(parentid != TC_H_ROOT);
499 return;
500 }
Patrick McHardy43effa12006-11-29 17:35:48 -0800501 cops = sch->ops->cl_ops;
502 if (cops->qlen_notify) {
503 cl = cops->get(sch, parentid);
504 cops->qlen_notify(sch, cl);
505 cops->put(sch, cl);
506 }
507 sch->q.qlen -= n;
508 }
509}
510EXPORT_SYMBOL(qdisc_tree_decrease_qlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
512/* Graft qdisc "new" to class "classid" of qdisc "parent" or
513 to device "dev".
514
515 Old qdisc is not destroyed but returned in *old.
516 */
517
518static int qdisc_graft(struct net_device *dev, struct Qdisc *parent,
519 u32 classid,
520 struct Qdisc *new, struct Qdisc **old)
521{
522 int err = 0;
523 struct Qdisc *q = *old;
524
525
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900526 if (parent == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 if (q && q->flags&TCQ_F_INGRESS) {
528 *old = dev_graft_qdisc(dev, q);
529 } else {
530 *old = dev_graft_qdisc(dev, new);
531 }
532 } else {
Eric Dumazet20fea082007-11-14 01:44:41 -0800533 const struct Qdisc_class_ops *cops = parent->ops->cl_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
535 err = -EINVAL;
536
537 if (cops) {
538 unsigned long cl = cops->get(parent, classid);
539 if (cl) {
540 err = cops->graft(parent, cl, new, old);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 cops->put(parent, cl);
542 }
543 }
544 }
545 return err;
546}
547
548/*
549 Allocate and initialize new qdisc.
550
551 Parameters are passed via opt.
552 */
553
554static struct Qdisc *
David S. Millerbb949fb2008-07-08 16:55:56 -0700555qdisc_create(struct net_device *dev, struct netdev_queue *dev_queue,
556 u32 parent, u32 handle, struct nlattr **tca, int *errp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
558 int err;
Patrick McHardy1e904742008-01-22 22:11:17 -0800559 struct nlattr *kind = tca[TCA_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 struct Qdisc *sch;
561 struct Qdisc_ops *ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
563 ops = qdisc_lookup_ops(kind);
564#ifdef CONFIG_KMOD
565 if (ops == NULL && kind != NULL) {
566 char name[IFNAMSIZ];
Patrick McHardy1e904742008-01-22 22:11:17 -0800567 if (nla_strlcpy(name, kind, IFNAMSIZ) < IFNAMSIZ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 /* We dropped the RTNL semaphore in order to
569 * perform the module load. So, even if we
570 * succeeded in loading the module we have to
571 * tell the caller to replay the request. We
572 * indicate this using -EAGAIN.
573 * We replay the request because the device may
574 * go away in the mean time.
575 */
576 rtnl_unlock();
577 request_module("sch_%s", name);
578 rtnl_lock();
579 ops = qdisc_lookup_ops(kind);
580 if (ops != NULL) {
581 /* We will try again qdisc_lookup_ops,
582 * so don't keep a reference.
583 */
584 module_put(ops->owner);
585 err = -EAGAIN;
586 goto err_out;
587 }
588 }
589 }
590#endif
591
Jamal Hadi Salimb9e2cc02006-08-03 16:36:51 -0700592 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 if (ops == NULL)
594 goto err_out;
595
David S. Miller5ce2d482008-07-08 17:06:30 -0700596 sch = qdisc_alloc(dev_queue, ops);
Thomas Graf3d54b822005-07-05 14:15:09 -0700597 if (IS_ERR(sch)) {
598 err = PTR_ERR(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 goto err_out2;
Thomas Graf3d54b822005-07-05 14:15:09 -0700600 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Patrick McHardyffc8fef2007-07-30 17:11:50 -0700602 sch->parent = parent;
603
Thomas Graf3d54b822005-07-05 14:15:09 -0700604 if (handle == TC_H_INGRESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 sch->flags |= TCQ_F_INGRESS;
Patrick McHardyfd44de72007-04-16 17:07:08 -0700606 sch->stats_lock = &dev->ingress_lock;
Thomas Graf3d54b822005-07-05 14:15:09 -0700607 handle = TC_H_MAKE(TC_H_INGRESS, 0);
Patrick McHardyfd44de72007-04-16 17:07:08 -0700608 } else {
609 sch->stats_lock = &dev->queue_lock;
610 if (handle == 0) {
611 handle = qdisc_alloc_handle(dev);
612 err = -ENOMEM;
613 if (handle == 0)
614 goto err_out3;
615 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 }
617
Thomas Graf3d54b822005-07-05 14:15:09 -0700618 sch->handle = handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Patrick McHardy1e904742008-01-22 22:11:17 -0800620 if (!ops->init || (err = ops->init(sch, tca[TCA_OPTIONS])) == 0) {
621 if (tca[TCA_RATE]) {
Thomas Graf023e09a2005-07-05 14:15:53 -0700622 err = gen_new_estimator(&sch->bstats, &sch->rate_est,
623 sch->stats_lock,
Patrick McHardy1e904742008-01-22 22:11:17 -0800624 tca[TCA_RATE]);
Thomas Graf023e09a2005-07-05 14:15:53 -0700625 if (err) {
626 /*
627 * Any broken qdiscs that would require
628 * a ops->reset() here? The qdisc was never
629 * in action so it shouldn't be necessary.
630 */
631 if (ops->destroy)
632 ops->destroy(sch);
633 goto err_out3;
634 }
635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 qdisc_lock_tree(dev);
637 list_add_tail(&sch->list, &dev->qdisc_list);
638 qdisc_unlock_tree(dev);
639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 return sch;
641 }
642err_out3:
643 dev_put(dev);
Thomas Graf3d54b822005-07-05 14:15:09 -0700644 kfree((char *) sch - sch->padded);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645err_out2:
646 module_put(ops->owner);
647err_out:
648 *errp = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 return NULL;
650}
651
Patrick McHardy1e904742008-01-22 22:11:17 -0800652static int qdisc_change(struct Qdisc *sch, struct nlattr **tca)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
Patrick McHardy1e904742008-01-22 22:11:17 -0800654 if (tca[TCA_OPTIONS]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 int err;
656
657 if (sch->ops->change == NULL)
658 return -EINVAL;
Patrick McHardy1e904742008-01-22 22:11:17 -0800659 err = sch->ops->change(sch, tca[TCA_OPTIONS]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 if (err)
661 return err;
662 }
Patrick McHardy1e904742008-01-22 22:11:17 -0800663 if (tca[TCA_RATE])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 gen_replace_estimator(&sch->bstats, &sch->rate_est,
Patrick McHardy1e904742008-01-22 22:11:17 -0800665 sch->stats_lock, tca[TCA_RATE]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 return 0;
667}
668
669struct check_loop_arg
670{
671 struct qdisc_walker w;
672 struct Qdisc *p;
673 int depth;
674};
675
676static int check_loop_fn(struct Qdisc *q, unsigned long cl, struct qdisc_walker *w);
677
678static int check_loop(struct Qdisc *q, struct Qdisc *p, int depth)
679{
680 struct check_loop_arg arg;
681
682 if (q->ops->cl_ops == NULL)
683 return 0;
684
685 arg.w.stop = arg.w.skip = arg.w.count = 0;
686 arg.w.fn = check_loop_fn;
687 arg.depth = depth;
688 arg.p = p;
689 q->ops->cl_ops->walk(q, &arg.w);
690 return arg.w.stop ? -ELOOP : 0;
691}
692
693static int
694check_loop_fn(struct Qdisc *q, unsigned long cl, struct qdisc_walker *w)
695{
696 struct Qdisc *leaf;
Eric Dumazet20fea082007-11-14 01:44:41 -0800697 const struct Qdisc_class_ops *cops = q->ops->cl_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 struct check_loop_arg *arg = (struct check_loop_arg *)w;
699
700 leaf = cops->leaf(q, cl);
701 if (leaf) {
702 if (leaf == arg->p || arg->depth > 7)
703 return -ELOOP;
704 return check_loop(leaf, arg->p, arg->depth + 1);
705 }
706 return 0;
707}
708
709/*
710 * Delete/get qdisc.
711 */
712
713static int tc_get_qdisc(struct sk_buff *skb, struct nlmsghdr *n, void *arg)
714{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900715 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 struct tcmsg *tcm = NLMSG_DATA(n);
Patrick McHardy1e904742008-01-22 22:11:17 -0800717 struct nlattr *tca[TCA_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 struct net_device *dev;
719 u32 clid = tcm->tcm_parent;
720 struct Qdisc *q = NULL;
721 struct Qdisc *p = NULL;
722 int err;
723
Denis V. Lunevb8542722007-12-01 00:21:31 +1100724 if (net != &init_net)
725 return -EINVAL;
726
Eric W. Biederman881d9662007-09-17 11:56:21 -0700727 if ((dev = __dev_get_by_index(&init_net, tcm->tcm_ifindex)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 return -ENODEV;
729
Patrick McHardy1e904742008-01-22 22:11:17 -0800730 err = nlmsg_parse(n, sizeof(*tcm), tca, TCA_MAX, NULL);
731 if (err < 0)
732 return err;
733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 if (clid) {
735 if (clid != TC_H_ROOT) {
736 if (TC_H_MAJ(clid) != TC_H_MAJ(TC_H_INGRESS)) {
737 if ((p = qdisc_lookup(dev, TC_H_MAJ(clid))) == NULL)
738 return -ENOENT;
739 q = qdisc_leaf(p, clid);
740 } else { /* ingress */
741 q = dev->qdisc_ingress;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900742 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 } else {
744 q = dev->qdisc_sleeping;
745 }
746 if (!q)
747 return -ENOENT;
748
749 if (tcm->tcm_handle && q->handle != tcm->tcm_handle)
750 return -EINVAL;
751 } else {
752 if ((q = qdisc_lookup(dev, tcm->tcm_handle)) == NULL)
753 return -ENOENT;
754 }
755
Patrick McHardy1e904742008-01-22 22:11:17 -0800756 if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], q->ops->id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 return -EINVAL;
758
759 if (n->nlmsg_type == RTM_DELQDISC) {
760 if (!clid)
761 return -EINVAL;
762 if (q->handle == 0)
763 return -ENOENT;
764 if ((err = qdisc_graft(dev, p, clid, NULL, &q)) != 0)
765 return err;
766 if (q) {
767 qdisc_notify(skb, n, clid, q, NULL);
Patrick McHardyfd44de72007-04-16 17:07:08 -0700768 qdisc_lock_tree(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 qdisc_destroy(q);
Patrick McHardyfd44de72007-04-16 17:07:08 -0700770 qdisc_unlock_tree(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 }
772 } else {
773 qdisc_notify(skb, n, clid, NULL, q);
774 }
775 return 0;
776}
777
778/*
779 Create/change qdisc.
780 */
781
782static int tc_modify_qdisc(struct sk_buff *skb, struct nlmsghdr *n, void *arg)
783{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900784 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 struct tcmsg *tcm;
Patrick McHardy1e904742008-01-22 22:11:17 -0800786 struct nlattr *tca[TCA_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 struct net_device *dev;
788 u32 clid;
789 struct Qdisc *q, *p;
790 int err;
791
Denis V. Lunevb8542722007-12-01 00:21:31 +1100792 if (net != &init_net)
793 return -EINVAL;
794
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795replay:
796 /* Reinit, just in case something touches this. */
797 tcm = NLMSG_DATA(n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 clid = tcm->tcm_parent;
799 q = p = NULL;
800
Eric W. Biederman881d9662007-09-17 11:56:21 -0700801 if ((dev = __dev_get_by_index(&init_net, tcm->tcm_ifindex)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 return -ENODEV;
803
Patrick McHardy1e904742008-01-22 22:11:17 -0800804 err = nlmsg_parse(n, sizeof(*tcm), tca, TCA_MAX, NULL);
805 if (err < 0)
806 return err;
807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 if (clid) {
809 if (clid != TC_H_ROOT) {
810 if (clid != TC_H_INGRESS) {
811 if ((p = qdisc_lookup(dev, TC_H_MAJ(clid))) == NULL)
812 return -ENOENT;
813 q = qdisc_leaf(p, clid);
814 } else { /*ingress */
815 q = dev->qdisc_ingress;
816 }
817 } else {
818 q = dev->qdisc_sleeping;
819 }
820
821 /* It may be default qdisc, ignore it */
822 if (q && q->handle == 0)
823 q = NULL;
824
825 if (!q || !tcm->tcm_handle || q->handle != tcm->tcm_handle) {
826 if (tcm->tcm_handle) {
827 if (q && !(n->nlmsg_flags&NLM_F_REPLACE))
828 return -EEXIST;
829 if (TC_H_MIN(tcm->tcm_handle))
830 return -EINVAL;
831 if ((q = qdisc_lookup(dev, tcm->tcm_handle)) == NULL)
832 goto create_n_graft;
833 if (n->nlmsg_flags&NLM_F_EXCL)
834 return -EEXIST;
Patrick McHardy1e904742008-01-22 22:11:17 -0800835 if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], q->ops->id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 return -EINVAL;
837 if (q == p ||
838 (p && check_loop(q, p, 0)))
839 return -ELOOP;
840 atomic_inc(&q->refcnt);
841 goto graft;
842 } else {
843 if (q == NULL)
844 goto create_n_graft;
845
846 /* This magic test requires explanation.
847 *
848 * We know, that some child q is already
849 * attached to this parent and have choice:
850 * either to change it or to create/graft new one.
851 *
852 * 1. We are allowed to create/graft only
853 * if CREATE and REPLACE flags are set.
854 *
855 * 2. If EXCL is set, requestor wanted to say,
856 * that qdisc tcm_handle is not expected
857 * to exist, so that we choose create/graft too.
858 *
859 * 3. The last case is when no flags are set.
860 * Alas, it is sort of hole in API, we
861 * cannot decide what to do unambiguously.
862 * For now we select create/graft, if
863 * user gave KIND, which does not match existing.
864 */
865 if ((n->nlmsg_flags&NLM_F_CREATE) &&
866 (n->nlmsg_flags&NLM_F_REPLACE) &&
867 ((n->nlmsg_flags&NLM_F_EXCL) ||
Patrick McHardy1e904742008-01-22 22:11:17 -0800868 (tca[TCA_KIND] &&
869 nla_strcmp(tca[TCA_KIND], q->ops->id))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 goto create_n_graft;
871 }
872 }
873 } else {
874 if (!tcm->tcm_handle)
875 return -EINVAL;
876 q = qdisc_lookup(dev, tcm->tcm_handle);
877 }
878
879 /* Change qdisc parameters */
880 if (q == NULL)
881 return -ENOENT;
882 if (n->nlmsg_flags&NLM_F_EXCL)
883 return -EEXIST;
Patrick McHardy1e904742008-01-22 22:11:17 -0800884 if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], q->ops->id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 return -EINVAL;
886 err = qdisc_change(q, tca);
887 if (err == 0)
888 qdisc_notify(skb, n, clid, NULL, q);
889 return err;
890
891create_n_graft:
892 if (!(n->nlmsg_flags&NLM_F_CREATE))
893 return -ENOENT;
894 if (clid == TC_H_INGRESS)
David S. Millerbb949fb2008-07-08 16:55:56 -0700895 q = qdisc_create(dev, &dev->rx_queue,
896 tcm->tcm_parent, tcm->tcm_parent,
Patrick McHardyffc8fef2007-07-30 17:11:50 -0700897 tca, &err);
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900898 else
David S. Millerbb949fb2008-07-08 16:55:56 -0700899 q = qdisc_create(dev, &dev->tx_queue,
900 tcm->tcm_parent, tcm->tcm_handle,
Patrick McHardyffc8fef2007-07-30 17:11:50 -0700901 tca, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 if (q == NULL) {
903 if (err == -EAGAIN)
904 goto replay;
905 return err;
906 }
907
908graft:
909 if (1) {
910 struct Qdisc *old_q = NULL;
911 err = qdisc_graft(dev, p, clid, q, &old_q);
912 if (err) {
913 if (q) {
Patrick McHardyfd44de72007-04-16 17:07:08 -0700914 qdisc_lock_tree(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 qdisc_destroy(q);
Patrick McHardyfd44de72007-04-16 17:07:08 -0700916 qdisc_unlock_tree(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 }
918 return err;
919 }
920 qdisc_notify(skb, n, clid, old_q, q);
921 if (old_q) {
Patrick McHardyfd44de72007-04-16 17:07:08 -0700922 qdisc_lock_tree(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 qdisc_destroy(old_q);
Patrick McHardyfd44de72007-04-16 17:07:08 -0700924 qdisc_unlock_tree(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 }
926 }
927 return 0;
928}
929
930static int tc_fill_qdisc(struct sk_buff *skb, struct Qdisc *q, u32 clid,
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -0700931 u32 pid, u32 seq, u16 flags, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932{
933 struct tcmsg *tcm;
934 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700935 unsigned char *b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 struct gnet_dump d;
937
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -0700938 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*tcm), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 tcm = NLMSG_DATA(nlh);
940 tcm->tcm_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700941 tcm->tcm__pad1 = 0;
942 tcm->tcm__pad2 = 0;
David S. Miller5ce2d482008-07-08 17:06:30 -0700943 tcm->tcm_ifindex = qdisc_dev(q)->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 tcm->tcm_parent = clid;
945 tcm->tcm_handle = q->handle;
946 tcm->tcm_info = atomic_read(&q->refcnt);
Patrick McHardy57e1c482008-01-23 20:34:28 -0800947 NLA_PUT_STRING(skb, TCA_KIND, q->ops->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 if (q->ops->dump && q->ops->dump(q, skb) < 0)
Patrick McHardy1e904742008-01-22 22:11:17 -0800949 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 q->qstats.qlen = q->q.qlen;
951
952 if (gnet_stats_start_copy_compat(skb, TCA_STATS2, TCA_STATS,
953 TCA_XSTATS, q->stats_lock, &d) < 0)
Patrick McHardy1e904742008-01-22 22:11:17 -0800954 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
956 if (q->ops->dump_stats && q->ops->dump_stats(q, &d) < 0)
Patrick McHardy1e904742008-01-22 22:11:17 -0800957 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
959 if (gnet_stats_copy_basic(&d, &q->bstats) < 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 gnet_stats_copy_rate_est(&d, &q->rate_est) < 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 gnet_stats_copy_queue(&d, &q->qstats) < 0)
Patrick McHardy1e904742008-01-22 22:11:17 -0800962 goto nla_put_failure;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900963
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 if (gnet_stats_finish_copy(&d) < 0)
Patrick McHardy1e904742008-01-22 22:11:17 -0800965 goto nla_put_failure;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900966
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700967 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 return skb->len;
969
970nlmsg_failure:
Patrick McHardy1e904742008-01-22 22:11:17 -0800971nla_put_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700972 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 return -1;
974}
975
976static int qdisc_notify(struct sk_buff *oskb, struct nlmsghdr *n,
977 u32 clid, struct Qdisc *old, struct Qdisc *new)
978{
979 struct sk_buff *skb;
980 u32 pid = oskb ? NETLINK_CB(oskb).pid : 0;
981
982 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
983 if (!skb)
984 return -ENOBUFS;
985
986 if (old && old->handle) {
987 if (tc_fill_qdisc(skb, old, clid, pid, n->nlmsg_seq, 0, RTM_DELQDISC) < 0)
988 goto err_out;
989 }
990 if (new) {
991 if (tc_fill_qdisc(skb, new, clid, pid, n->nlmsg_seq, old ? NLM_F_REPLACE : 0, RTM_NEWQDISC) < 0)
992 goto err_out;
993 }
994
995 if (skb->len)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800996 return rtnetlink_send(skb, &init_net, pid, RTNLGRP_TC, n->nlmsg_flags&NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
998err_out:
999 kfree_skb(skb);
1000 return -EINVAL;
1001}
1002
1003static int tc_dump_qdisc(struct sk_buff *skb, struct netlink_callback *cb)
1004{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001005 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 int idx, q_idx;
1007 int s_idx, s_q_idx;
1008 struct net_device *dev;
1009 struct Qdisc *q;
1010
Denis V. Lunevb8542722007-12-01 00:21:31 +11001011 if (net != &init_net)
1012 return 0;
1013
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 s_idx = cb->args[0];
1015 s_q_idx = q_idx = cb->args[1];
1016 read_lock(&dev_base_lock);
Pavel Emelianov7562f872007-05-03 15:13:45 -07001017 idx = 0;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001018 for_each_netdev(&init_net, dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 if (idx < s_idx)
Pavel Emelianov7562f872007-05-03 15:13:45 -07001020 goto cont;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 if (idx > s_idx)
1022 s_q_idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 q_idx = 0;
1024 list_for_each_entry(q, &dev->qdisc_list, list) {
1025 if (q_idx < s_q_idx) {
1026 q_idx++;
1027 continue;
1028 }
1029 if (tc_fill_qdisc(skb, q, q->parent, NETLINK_CB(cb->skb).pid,
Patrick McHardy0463d4a2007-04-16 17:02:10 -07001030 cb->nlh->nlmsg_seq, NLM_F_MULTI, RTM_NEWQDISC) <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 q_idx++;
1033 }
Pavel Emelianov7562f872007-05-03 15:13:45 -07001034cont:
1035 idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 }
1037
1038done:
1039 read_unlock(&dev_base_lock);
1040
1041 cb->args[0] = idx;
1042 cb->args[1] = q_idx;
1043
1044 return skb->len;
1045}
1046
1047
1048
1049/************************************************
1050 * Traffic classes manipulation. *
1051 ************************************************/
1052
1053
1054
1055static int tc_ctl_tclass(struct sk_buff *skb, struct nlmsghdr *n, void *arg)
1056{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001057 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 struct tcmsg *tcm = NLMSG_DATA(n);
Patrick McHardy1e904742008-01-22 22:11:17 -08001059 struct nlattr *tca[TCA_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 struct net_device *dev;
1061 struct Qdisc *q = NULL;
Eric Dumazet20fea082007-11-14 01:44:41 -08001062 const struct Qdisc_class_ops *cops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 unsigned long cl = 0;
1064 unsigned long new_cl;
1065 u32 pid = tcm->tcm_parent;
1066 u32 clid = tcm->tcm_handle;
1067 u32 qid = TC_H_MAJ(clid);
1068 int err;
1069
Denis V. Lunevb8542722007-12-01 00:21:31 +11001070 if (net != &init_net)
1071 return -EINVAL;
1072
Eric W. Biederman881d9662007-09-17 11:56:21 -07001073 if ((dev = __dev_get_by_index(&init_net, tcm->tcm_ifindex)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 return -ENODEV;
1075
Patrick McHardy1e904742008-01-22 22:11:17 -08001076 err = nlmsg_parse(n, sizeof(*tcm), tca, TCA_MAX, NULL);
1077 if (err < 0)
1078 return err;
1079
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 /*
1081 parent == TC_H_UNSPEC - unspecified parent.
1082 parent == TC_H_ROOT - class is root, which has no parent.
1083 parent == X:0 - parent is root class.
1084 parent == X:Y - parent is a node in hierarchy.
1085 parent == 0:Y - parent is X:Y, where X:0 is qdisc.
1086
1087 handle == 0:0 - generate handle from kernel pool.
1088 handle == 0:Y - class is X:Y, where X:0 is qdisc.
1089 handle == X:Y - clear.
1090 handle == X:0 - root class.
1091 */
1092
1093 /* Step 1. Determine qdisc handle X:0 */
1094
1095 if (pid != TC_H_ROOT) {
1096 u32 qid1 = TC_H_MAJ(pid);
1097
1098 if (qid && qid1) {
1099 /* If both majors are known, they must be identical. */
1100 if (qid != qid1)
1101 return -EINVAL;
1102 } else if (qid1) {
1103 qid = qid1;
1104 } else if (qid == 0)
1105 qid = dev->qdisc_sleeping->handle;
1106
1107 /* Now qid is genuine qdisc handle consistent
1108 both with parent and child.
1109
1110 TC_H_MAJ(pid) still may be unspecified, complete it now.
1111 */
1112 if (pid)
1113 pid = TC_H_MAKE(qid, pid);
1114 } else {
1115 if (qid == 0)
1116 qid = dev->qdisc_sleeping->handle;
1117 }
1118
1119 /* OK. Locate qdisc */
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +09001120 if ((q = qdisc_lookup(dev, qid)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 return -ENOENT;
1122
1123 /* An check that it supports classes */
1124 cops = q->ops->cl_ops;
1125 if (cops == NULL)
1126 return -EINVAL;
1127
1128 /* Now try to get class */
1129 if (clid == 0) {
1130 if (pid == TC_H_ROOT)
1131 clid = qid;
1132 } else
1133 clid = TC_H_MAKE(qid, clid);
1134
1135 if (clid)
1136 cl = cops->get(q, clid);
1137
1138 if (cl == 0) {
1139 err = -ENOENT;
1140 if (n->nlmsg_type != RTM_NEWTCLASS || !(n->nlmsg_flags&NLM_F_CREATE))
1141 goto out;
1142 } else {
1143 switch (n->nlmsg_type) {
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +09001144 case RTM_NEWTCLASS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 err = -EEXIST;
1146 if (n->nlmsg_flags&NLM_F_EXCL)
1147 goto out;
1148 break;
1149 case RTM_DELTCLASS:
1150 err = cops->delete(q, cl);
1151 if (err == 0)
1152 tclass_notify(skb, n, q, cl, RTM_DELTCLASS);
1153 goto out;
1154 case RTM_GETTCLASS:
1155 err = tclass_notify(skb, n, q, cl, RTM_NEWTCLASS);
1156 goto out;
1157 default:
1158 err = -EINVAL;
1159 goto out;
1160 }
1161 }
1162
1163 new_cl = cl;
1164 err = cops->change(q, clid, pid, tca, &new_cl);
1165 if (err == 0)
1166 tclass_notify(skb, n, q, new_cl, RTM_NEWTCLASS);
1167
1168out:
1169 if (cl)
1170 cops->put(q, cl);
1171
1172 return err;
1173}
1174
1175
1176static int tc_fill_tclass(struct sk_buff *skb, struct Qdisc *q,
1177 unsigned long cl,
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -07001178 u32 pid, u32 seq, u16 flags, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179{
1180 struct tcmsg *tcm;
1181 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001182 unsigned char *b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 struct gnet_dump d;
Eric Dumazet20fea082007-11-14 01:44:41 -08001184 const struct Qdisc_class_ops *cl_ops = q->ops->cl_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -07001186 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*tcm), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 tcm = NLMSG_DATA(nlh);
1188 tcm->tcm_family = AF_UNSPEC;
David S. Miller5ce2d482008-07-08 17:06:30 -07001189 tcm->tcm_ifindex = qdisc_dev(q)->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 tcm->tcm_parent = q->handle;
1191 tcm->tcm_handle = q->handle;
1192 tcm->tcm_info = 0;
Patrick McHardy57e1c482008-01-23 20:34:28 -08001193 NLA_PUT_STRING(skb, TCA_KIND, q->ops->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 if (cl_ops->dump && cl_ops->dump(q, cl, skb, tcm) < 0)
Patrick McHardy1e904742008-01-22 22:11:17 -08001195 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
1197 if (gnet_stats_start_copy_compat(skb, TCA_STATS2, TCA_STATS,
1198 TCA_XSTATS, q->stats_lock, &d) < 0)
Patrick McHardy1e904742008-01-22 22:11:17 -08001199 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
1201 if (cl_ops->dump_stats && cl_ops->dump_stats(q, cl, &d) < 0)
Patrick McHardy1e904742008-01-22 22:11:17 -08001202 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
1204 if (gnet_stats_finish_copy(&d) < 0)
Patrick McHardy1e904742008-01-22 22:11:17 -08001205 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001207 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 return skb->len;
1209
1210nlmsg_failure:
Patrick McHardy1e904742008-01-22 22:11:17 -08001211nla_put_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001212 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 return -1;
1214}
1215
1216static int tclass_notify(struct sk_buff *oskb, struct nlmsghdr *n,
1217 struct Qdisc *q, unsigned long cl, int event)
1218{
1219 struct sk_buff *skb;
1220 u32 pid = oskb ? NETLINK_CB(oskb).pid : 0;
1221
1222 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1223 if (!skb)
1224 return -ENOBUFS;
1225
1226 if (tc_fill_tclass(skb, q, cl, pid, n->nlmsg_seq, 0, event) < 0) {
1227 kfree_skb(skb);
1228 return -EINVAL;
1229 }
1230
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08001231 return rtnetlink_send(skb, &init_net, pid, RTNLGRP_TC, n->nlmsg_flags&NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232}
1233
1234struct qdisc_dump_args
1235{
1236 struct qdisc_walker w;
1237 struct sk_buff *skb;
1238 struct netlink_callback *cb;
1239};
1240
1241static int qdisc_class_dump(struct Qdisc *q, unsigned long cl, struct qdisc_walker *arg)
1242{
1243 struct qdisc_dump_args *a = (struct qdisc_dump_args *)arg;
1244
1245 return tc_fill_tclass(a->skb, q, cl, NETLINK_CB(a->cb->skb).pid,
1246 a->cb->nlh->nlmsg_seq, NLM_F_MULTI, RTM_NEWTCLASS);
1247}
1248
1249static int tc_dump_tclass(struct sk_buff *skb, struct netlink_callback *cb)
1250{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001251 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 int t;
1253 int s_t;
1254 struct net_device *dev;
1255 struct Qdisc *q;
1256 struct tcmsg *tcm = (struct tcmsg*)NLMSG_DATA(cb->nlh);
1257 struct qdisc_dump_args arg;
1258
Denis V. Lunevb8542722007-12-01 00:21:31 +11001259 if (net != &init_net)
1260 return 0;
1261
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 if (cb->nlh->nlmsg_len < NLMSG_LENGTH(sizeof(*tcm)))
1263 return 0;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001264 if ((dev = dev_get_by_index(&init_net, tcm->tcm_ifindex)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 return 0;
1266
1267 s_t = cb->args[0];
1268 t = 0;
1269
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 list_for_each_entry(q, &dev->qdisc_list, list) {
1271 if (t < s_t || !q->ops->cl_ops ||
1272 (tcm->tcm_parent &&
1273 TC_H_MAJ(tcm->tcm_parent) != q->handle)) {
1274 t++;
1275 continue;
1276 }
1277 if (t > s_t)
1278 memset(&cb->args[1], 0, sizeof(cb->args)-sizeof(cb->args[0]));
1279 arg.w.fn = qdisc_class_dump;
1280 arg.skb = skb;
1281 arg.cb = cb;
1282 arg.w.stop = 0;
1283 arg.w.skip = cb->args[1];
1284 arg.w.count = 0;
1285 q->ops->cl_ops->walk(q, &arg.w);
1286 cb->args[1] = arg.w.count;
1287 if (arg.w.stop)
1288 break;
1289 t++;
1290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
1292 cb->args[0] = t;
1293
1294 dev_put(dev);
1295 return skb->len;
1296}
1297
1298/* Main classifier routine: scans classifier chain attached
1299 to this qdisc, (optionally) tests for protocol and asks
1300 specific classifiers.
1301 */
Patrick McHardy73ca4912007-07-15 00:02:31 -07001302int tc_classify_compat(struct sk_buff *skb, struct tcf_proto *tp,
1303 struct tcf_result *res)
1304{
1305 __be16 protocol = skb->protocol;
1306 int err = 0;
1307
1308 for (; tp; tp = tp->next) {
1309 if ((tp->protocol == protocol ||
1310 tp->protocol == htons(ETH_P_ALL)) &&
1311 (err = tp->classify(skb, tp, res)) >= 0) {
1312#ifdef CONFIG_NET_CLS_ACT
1313 if (err != TC_ACT_RECLASSIFY && skb->tc_verd)
1314 skb->tc_verd = SET_TC_VERD(skb->tc_verd, 0);
1315#endif
1316 return err;
1317 }
1318 }
1319 return -1;
1320}
1321EXPORT_SYMBOL(tc_classify_compat);
1322
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323int tc_classify(struct sk_buff *skb, struct tcf_proto *tp,
Patrick McHardy73ca4912007-07-15 00:02:31 -07001324 struct tcf_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325{
1326 int err = 0;
Patrick McHardy73ca4912007-07-15 00:02:31 -07001327 __be16 protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328#ifdef CONFIG_NET_CLS_ACT
1329 struct tcf_proto *otp = tp;
1330reclassify:
1331#endif
1332 protocol = skb->protocol;
1333
Patrick McHardy73ca4912007-07-15 00:02:31 -07001334 err = tc_classify_compat(skb, tp, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335#ifdef CONFIG_NET_CLS_ACT
Patrick McHardy73ca4912007-07-15 00:02:31 -07001336 if (err == TC_ACT_RECLASSIFY) {
1337 u32 verd = G_TC_VERD(skb->tc_verd);
1338 tp = otp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
Patrick McHardy73ca4912007-07-15 00:02:31 -07001340 if (verd++ >= MAX_REC_LOOP) {
1341 printk("rule prio %u protocol %02x reclassify loop, "
1342 "packet dropped\n",
1343 tp->prio&0xffff, ntohs(tp->protocol));
1344 return TC_ACT_SHOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 }
Patrick McHardy73ca4912007-07-15 00:02:31 -07001346 skb->tc_verd = SET_TC_VERD(skb->tc_verd, verd);
1347 goto reclassify;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 }
Patrick McHardy73ca4912007-07-15 00:02:31 -07001349#endif
1350 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351}
Patrick McHardy73ca4912007-07-15 00:02:31 -07001352EXPORT_SYMBOL(tc_classify);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
Patrick McHardya48b5a62007-03-23 11:29:43 -07001354void tcf_destroy(struct tcf_proto *tp)
1355{
1356 tp->ops->destroy(tp);
1357 module_put(tp->ops->owner);
1358 kfree(tp);
1359}
1360
Patrick McHardyff31ab52008-07-01 19:52:38 -07001361void tcf_destroy_chain(struct tcf_proto **fl)
Patrick McHardya48b5a62007-03-23 11:29:43 -07001362{
1363 struct tcf_proto *tp;
1364
Patrick McHardyff31ab52008-07-01 19:52:38 -07001365 while ((tp = *fl) != NULL) {
1366 *fl = tp->next;
Patrick McHardya48b5a62007-03-23 11:29:43 -07001367 tcf_destroy(tp);
1368 }
1369}
1370EXPORT_SYMBOL(tcf_destroy_chain);
1371
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372#ifdef CONFIG_PROC_FS
1373static int psched_show(struct seq_file *seq, void *v)
1374{
Patrick McHardy3c0cfc12007-10-10 16:32:41 -07001375 struct timespec ts;
1376
1377 hrtimer_get_res(CLOCK_MONOTONIC, &ts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 seq_printf(seq, "%08x %08x %08x %08x\n",
Patrick McHardy641b9e02007-03-16 01:18:42 -07001379 (u32)NSEC_PER_USEC, (u32)PSCHED_US2NS(1),
Patrick McHardy514bca32007-03-16 12:34:52 -07001380 1000000,
Patrick McHardy3c0cfc12007-10-10 16:32:41 -07001381 (u32)NSEC_PER_SEC/(u32)ktime_to_ns(timespec_to_ktime(ts)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
1383 return 0;
1384}
1385
1386static int psched_open(struct inode *inode, struct file *file)
1387{
1388 return single_open(file, psched_show, PDE(inode)->data);
1389}
1390
Arjan van de Venda7071d2007-02-12 00:55:36 -08001391static const struct file_operations psched_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 .owner = THIS_MODULE,
1393 .open = psched_open,
1394 .read = seq_read,
1395 .llseek = seq_lseek,
1396 .release = single_release,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +09001397};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398#endif
1399
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400static int __init pktsched_init(void)
1401{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 register_qdisc(&pfifo_qdisc_ops);
1403 register_qdisc(&bfifo_qdisc_ops);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02001404 proc_net_fops_create(&init_net, "psched", 0, &psched_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
Thomas Grafbe577dd2007-03-22 11:55:50 -07001406 rtnl_register(PF_UNSPEC, RTM_NEWQDISC, tc_modify_qdisc, NULL);
1407 rtnl_register(PF_UNSPEC, RTM_DELQDISC, tc_get_qdisc, NULL);
1408 rtnl_register(PF_UNSPEC, RTM_GETQDISC, tc_get_qdisc, tc_dump_qdisc);
1409 rtnl_register(PF_UNSPEC, RTM_NEWTCLASS, tc_ctl_tclass, NULL);
1410 rtnl_register(PF_UNSPEC, RTM_DELTCLASS, tc_ctl_tclass, NULL);
1411 rtnl_register(PF_UNSPEC, RTM_GETTCLASS, tc_ctl_tclass, tc_dump_tclass);
1412
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 return 0;
1414}
1415
1416subsys_initcall(pktsched_init);