]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - net/sched/sch_api.c
netdev: The ingress_lock member is no longer needed.
[mirror_ubuntu-artful-kernel.git] / net / sched / sch_api.c
CommitLineData
1da177e4
LT
1/*
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
1da177e4
LT
18#include <linux/module.h>
19#include <linux/types.h>
20#include <linux/kernel.h>
1da177e4 21#include <linux/string.h>
1da177e4 22#include <linux/errno.h>
1da177e4 23#include <linux/skbuff.h>
1da177e4
LT
24#include <linux/init.h>
25#include <linux/proc_fs.h>
26#include <linux/seq_file.h>
27#include <linux/kmod.h>
28#include <linux/list.h>
4179477f 29#include <linux/hrtimer.h>
1da177e4 30
457c4cbc 31#include <net/net_namespace.h>
b854272b 32#include <net/sock.h>
dc5fc579 33#include <net/netlink.h>
1da177e4
LT
34#include <net/pkt_sched.h>
35
1da177e4
LT
36static 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
e65d22e1 102 just buggy devices, which can defer output even if netif_queue_stopped()=0.
1da177e4
LT
103
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}
62e3ba1b 161EXPORT_SYMBOL(register_qdisc);
1da177e4
LT
162
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}
62e3ba1b 180EXPORT_SYMBOL(unregister_qdisc);
1da177e4
LT
181
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
0463d4ae 186struct Qdisc *qdisc_lookup(struct net_device *dev, u32 handle)
1da177e4
LT
187{
188 struct Qdisc *q;
189
1da177e4 190 list_for_each_entry(q, &dev->qdisc_list, list) {
43effa1e 191 if (q->handle == handle)
1da177e4 192 return q;
1da177e4 193 }
1da177e4
LT
194 return NULL;
195}
196
197static struct Qdisc *qdisc_leaf(struct Qdisc *p, u32 classid)
198{
199 unsigned long cl;
200 struct Qdisc *leaf;
20fea08b 201 const struct Qdisc_class_ops *cops = p->ops->cl_ops;
1da177e4
LT
202
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
1e90474c 216static struct Qdisc_ops *qdisc_lookup_ops(struct nlattr *kind)
1da177e4
LT
217{
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) {
1e90474c 223 if (nla_strcmp(kind, q->id) == 0) {
1da177e4
LT
224 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
1e90474c 236struct qdisc_rate_table *qdisc_get_rtab(struct tc_ratespec *r, struct nlattr *tab)
1da177e4
LT
237{
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
5feb5e1a
PM
247 if (tab == NULL || r->rate == 0 || r->cell_log == 0 ||
248 nla_len(tab) != TC_RTAB_SIZE)
1da177e4
LT
249 return NULL;
250
251 rtab = kmalloc(sizeof(*rtab), GFP_KERNEL);
252 if (rtab) {
253 rtab->rate = *r;
254 rtab->refcnt = 1;
1e90474c 255 memcpy(rtab->data, nla_data(tab), 1024);
1da177e4
LT
256 rtab->next = qdisc_rtab_list;
257 qdisc_rtab_list = rtab;
258 }
259 return rtab;
260}
62e3ba1b 261EXPORT_SYMBOL(qdisc_get_rtab);
1da177e4
LT
262
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}
62e3ba1b 278EXPORT_SYMBOL(qdisc_put_rtab);
1da177e4 279
4179477f
PM
280static enum hrtimer_restart qdisc_watchdog(struct hrtimer *timer)
281{
282 struct qdisc_watchdog *wd = container_of(timer, struct qdisc_watchdog,
283 timer);
5ce2d488 284 struct net_device *dev = qdisc_dev(wd->qdisc);
4179477f
PM
285
286 wd->qdisc->flags &= ~TCQ_F_THROTTLED;
11274e5a 287 smp_wmb();
0621ed2e 288 netif_schedule(dev);
1936502d 289
4179477f
PM
290 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);
1da177e4 318
6fe1c7a5
PM
319struct 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
1da177e4
LT
423/* 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
43effa1e
PM
484void qdisc_tree_decrease_qlen(struct Qdisc *sch, unsigned int n)
485{
20fea08b 486 const struct Qdisc_class_ops *cops;
43effa1e
PM
487 unsigned long cl;
488 u32 parentid;
489
490 if (n == 0)
491 return;
492 while ((parentid = sch->parent)) {
066a3b5b
JP
493 if (TC_H_MAJ(parentid) == TC_H_MAJ(TC_H_INGRESS))
494 return;
495
5ce2d488 496 sch = qdisc_lookup(qdisc_dev(sch), TC_H_MAJ(parentid));
ffc8fefa
PM
497 if (sch == NULL) {
498 WARN_ON(parentid != TC_H_ROOT);
499 return;
500 }
43effa1e
PM
501 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);
1da177e4
LT
511
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
10297b99 526 if (parent == NULL) {
1da177e4
LT
527 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 {
20fea08b 533 const struct Qdisc_class_ops *cops = parent->ops->cl_ops;
1da177e4
LT
534
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);
1da177e4
LT
541 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 *
bb949fbd
DM
555qdisc_create(struct net_device *dev, struct netdev_queue *dev_queue,
556 u32 parent, u32 handle, struct nlattr **tca, int *errp)
1da177e4
LT
557{
558 int err;
1e90474c 559 struct nlattr *kind = tca[TCA_KIND];
1da177e4
LT
560 struct Qdisc *sch;
561 struct Qdisc_ops *ops;
1da177e4
LT
562
563 ops = qdisc_lookup_ops(kind);
564#ifdef CONFIG_KMOD
565 if (ops == NULL && kind != NULL) {
566 char name[IFNAMSIZ];
1e90474c 567 if (nla_strlcpy(name, kind, IFNAMSIZ) < IFNAMSIZ) {
1da177e4
LT
568 /* 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
b9e2cc0f 592 err = -ENOENT;
1da177e4
LT
593 if (ops == NULL)
594 goto err_out;
595
5ce2d488 596 sch = qdisc_alloc(dev_queue, ops);
3d54b82f
TG
597 if (IS_ERR(sch)) {
598 err = PTR_ERR(sch);
1da177e4 599 goto err_out2;
3d54b82f 600 }
1da177e4 601
ffc8fefa
PM
602 sch->parent = parent;
603
555353cf 604 sch->stats_lock = &dev_queue->lock;
3d54b82f 605 if (handle == TC_H_INGRESS) {
1da177e4 606 sch->flags |= TCQ_F_INGRESS;
3d54b82f 607 handle = TC_H_MAKE(TC_H_INGRESS, 0);
fd44de7c 608 } else {
fd44de7c
PM
609 if (handle == 0) {
610 handle = qdisc_alloc_handle(dev);
611 err = -ENOMEM;
612 if (handle == 0)
613 goto err_out3;
614 }
1da177e4
LT
615 }
616
3d54b82f 617 sch->handle = handle;
1da177e4 618
1e90474c
PM
619 if (!ops->init || (err = ops->init(sch, tca[TCA_OPTIONS])) == 0) {
620 if (tca[TCA_RATE]) {
023e09a7
TG
621 err = gen_new_estimator(&sch->bstats, &sch->rate_est,
622 sch->stats_lock,
1e90474c 623 tca[TCA_RATE]);
023e09a7
TG
624 if (err) {
625 /*
626 * Any broken qdiscs that would require
627 * a ops->reset() here? The qdisc was never
628 * in action so it shouldn't be necessary.
629 */
630 if (ops->destroy)
631 ops->destroy(sch);
632 goto err_out3;
633 }
634 }
1da177e4
LT
635 qdisc_lock_tree(dev);
636 list_add_tail(&sch->list, &dev->qdisc_list);
637 qdisc_unlock_tree(dev);
638
1da177e4
LT
639 return sch;
640 }
641err_out3:
642 dev_put(dev);
3d54b82f 643 kfree((char *) sch - sch->padded);
1da177e4
LT
644err_out2:
645 module_put(ops->owner);
646err_out:
647 *errp = err;
1da177e4
LT
648 return NULL;
649}
650
1e90474c 651static int qdisc_change(struct Qdisc *sch, struct nlattr **tca)
1da177e4 652{
1e90474c 653 if (tca[TCA_OPTIONS]) {
1da177e4
LT
654 int err;
655
656 if (sch->ops->change == NULL)
657 return -EINVAL;
1e90474c 658 err = sch->ops->change(sch, tca[TCA_OPTIONS]);
1da177e4
LT
659 if (err)
660 return err;
661 }
1e90474c 662 if (tca[TCA_RATE])
1da177e4 663 gen_replace_estimator(&sch->bstats, &sch->rate_est,
1e90474c 664 sch->stats_lock, tca[TCA_RATE]);
1da177e4
LT
665 return 0;
666}
667
668struct check_loop_arg
669{
670 struct qdisc_walker w;
671 struct Qdisc *p;
672 int depth;
673};
674
675static int check_loop_fn(struct Qdisc *q, unsigned long cl, struct qdisc_walker *w);
676
677static int check_loop(struct Qdisc *q, struct Qdisc *p, int depth)
678{
679 struct check_loop_arg arg;
680
681 if (q->ops->cl_ops == NULL)
682 return 0;
683
684 arg.w.stop = arg.w.skip = arg.w.count = 0;
685 arg.w.fn = check_loop_fn;
686 arg.depth = depth;
687 arg.p = p;
688 q->ops->cl_ops->walk(q, &arg.w);
689 return arg.w.stop ? -ELOOP : 0;
690}
691
692static int
693check_loop_fn(struct Qdisc *q, unsigned long cl, struct qdisc_walker *w)
694{
695 struct Qdisc *leaf;
20fea08b 696 const struct Qdisc_class_ops *cops = q->ops->cl_ops;
1da177e4
LT
697 struct check_loop_arg *arg = (struct check_loop_arg *)w;
698
699 leaf = cops->leaf(q, cl);
700 if (leaf) {
701 if (leaf == arg->p || arg->depth > 7)
702 return -ELOOP;
703 return check_loop(leaf, arg->p, arg->depth + 1);
704 }
705 return 0;
706}
707
708/*
709 * Delete/get qdisc.
710 */
711
712static int tc_get_qdisc(struct sk_buff *skb, struct nlmsghdr *n, void *arg)
713{
3b1e0a65 714 struct net *net = sock_net(skb->sk);
1da177e4 715 struct tcmsg *tcm = NLMSG_DATA(n);
1e90474c 716 struct nlattr *tca[TCA_MAX + 1];
1da177e4
LT
717 struct net_device *dev;
718 u32 clid = tcm->tcm_parent;
719 struct Qdisc *q = NULL;
720 struct Qdisc *p = NULL;
721 int err;
722
b854272b
DL
723 if (net != &init_net)
724 return -EINVAL;
725
881d966b 726 if ((dev = __dev_get_by_index(&init_net, tcm->tcm_ifindex)) == NULL)
1da177e4
LT
727 return -ENODEV;
728
1e90474c
PM
729 err = nlmsg_parse(n, sizeof(*tcm), tca, TCA_MAX, NULL);
730 if (err < 0)
731 return err;
732
1da177e4
LT
733 if (clid) {
734 if (clid != TC_H_ROOT) {
735 if (TC_H_MAJ(clid) != TC_H_MAJ(TC_H_INGRESS)) {
736 if ((p = qdisc_lookup(dev, TC_H_MAJ(clid))) == NULL)
737 return -ENOENT;
738 q = qdisc_leaf(p, clid);
739 } else { /* ingress */
740 q = dev->qdisc_ingress;
10297b99 741 }
1da177e4
LT
742 } else {
743 q = dev->qdisc_sleeping;
744 }
745 if (!q)
746 return -ENOENT;
747
748 if (tcm->tcm_handle && q->handle != tcm->tcm_handle)
749 return -EINVAL;
750 } else {
751 if ((q = qdisc_lookup(dev, tcm->tcm_handle)) == NULL)
752 return -ENOENT;
753 }
754
1e90474c 755 if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], q->ops->id))
1da177e4
LT
756 return -EINVAL;
757
758 if (n->nlmsg_type == RTM_DELQDISC) {
759 if (!clid)
760 return -EINVAL;
761 if (q->handle == 0)
762 return -ENOENT;
763 if ((err = qdisc_graft(dev, p, clid, NULL, &q)) != 0)
764 return err;
765 if (q) {
766 qdisc_notify(skb, n, clid, q, NULL);
fd44de7c 767 qdisc_lock_tree(dev);
1da177e4 768 qdisc_destroy(q);
fd44de7c 769 qdisc_unlock_tree(dev);
1da177e4
LT
770 }
771 } else {
772 qdisc_notify(skb, n, clid, NULL, q);
773 }
774 return 0;
775}
776
777/*
778 Create/change qdisc.
779 */
780
781static int tc_modify_qdisc(struct sk_buff *skb, struct nlmsghdr *n, void *arg)
782{
3b1e0a65 783 struct net *net = sock_net(skb->sk);
1da177e4 784 struct tcmsg *tcm;
1e90474c 785 struct nlattr *tca[TCA_MAX + 1];
1da177e4
LT
786 struct net_device *dev;
787 u32 clid;
788 struct Qdisc *q, *p;
789 int err;
790
b854272b
DL
791 if (net != &init_net)
792 return -EINVAL;
793
1da177e4
LT
794replay:
795 /* Reinit, just in case something touches this. */
796 tcm = NLMSG_DATA(n);
1da177e4
LT
797 clid = tcm->tcm_parent;
798 q = p = NULL;
799
881d966b 800 if ((dev = __dev_get_by_index(&init_net, tcm->tcm_ifindex)) == NULL)
1da177e4
LT
801 return -ENODEV;
802
1e90474c
PM
803 err = nlmsg_parse(n, sizeof(*tcm), tca, TCA_MAX, NULL);
804 if (err < 0)
805 return err;
806
1da177e4
LT
807 if (clid) {
808 if (clid != TC_H_ROOT) {
809 if (clid != TC_H_INGRESS) {
810 if ((p = qdisc_lookup(dev, TC_H_MAJ(clid))) == NULL)
811 return -ENOENT;
812 q = qdisc_leaf(p, clid);
813 } else { /*ingress */
814 q = dev->qdisc_ingress;
815 }
816 } else {
817 q = dev->qdisc_sleeping;
818 }
819
820 /* It may be default qdisc, ignore it */
821 if (q && q->handle == 0)
822 q = NULL;
823
824 if (!q || !tcm->tcm_handle || q->handle != tcm->tcm_handle) {
825 if (tcm->tcm_handle) {
826 if (q && !(n->nlmsg_flags&NLM_F_REPLACE))
827 return -EEXIST;
828 if (TC_H_MIN(tcm->tcm_handle))
829 return -EINVAL;
830 if ((q = qdisc_lookup(dev, tcm->tcm_handle)) == NULL)
831 goto create_n_graft;
832 if (n->nlmsg_flags&NLM_F_EXCL)
833 return -EEXIST;
1e90474c 834 if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], q->ops->id))
1da177e4
LT
835 return -EINVAL;
836 if (q == p ||
837 (p && check_loop(q, p, 0)))
838 return -ELOOP;
839 atomic_inc(&q->refcnt);
840 goto graft;
841 } else {
842 if (q == NULL)
843 goto create_n_graft;
844
845 /* This magic test requires explanation.
846 *
847 * We know, that some child q is already
848 * attached to this parent and have choice:
849 * either to change it or to create/graft new one.
850 *
851 * 1. We are allowed to create/graft only
852 * if CREATE and REPLACE flags are set.
853 *
854 * 2. If EXCL is set, requestor wanted to say,
855 * that qdisc tcm_handle is not expected
856 * to exist, so that we choose create/graft too.
857 *
858 * 3. The last case is when no flags are set.
859 * Alas, it is sort of hole in API, we
860 * cannot decide what to do unambiguously.
861 * For now we select create/graft, if
862 * user gave KIND, which does not match existing.
863 */
864 if ((n->nlmsg_flags&NLM_F_CREATE) &&
865 (n->nlmsg_flags&NLM_F_REPLACE) &&
866 ((n->nlmsg_flags&NLM_F_EXCL) ||
1e90474c
PM
867 (tca[TCA_KIND] &&
868 nla_strcmp(tca[TCA_KIND], q->ops->id))))
1da177e4
LT
869 goto create_n_graft;
870 }
871 }
872 } else {
873 if (!tcm->tcm_handle)
874 return -EINVAL;
875 q = qdisc_lookup(dev, tcm->tcm_handle);
876 }
877
878 /* Change qdisc parameters */
879 if (q == NULL)
880 return -ENOENT;
881 if (n->nlmsg_flags&NLM_F_EXCL)
882 return -EEXIST;
1e90474c 883 if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], q->ops->id))
1da177e4
LT
884 return -EINVAL;
885 err = qdisc_change(q, tca);
886 if (err == 0)
887 qdisc_notify(skb, n, clid, NULL, q);
888 return err;
889
890create_n_graft:
891 if (!(n->nlmsg_flags&NLM_F_CREATE))
892 return -ENOENT;
893 if (clid == TC_H_INGRESS)
bb949fbd
DM
894 q = qdisc_create(dev, &dev->rx_queue,
895 tcm->tcm_parent, tcm->tcm_parent,
ffc8fefa 896 tca, &err);
10297b99 897 else
bb949fbd
DM
898 q = qdisc_create(dev, &dev->tx_queue,
899 tcm->tcm_parent, tcm->tcm_handle,
ffc8fefa 900 tca, &err);
1da177e4
LT
901 if (q == NULL) {
902 if (err == -EAGAIN)
903 goto replay;
904 return err;
905 }
906
907graft:
908 if (1) {
909 struct Qdisc *old_q = NULL;
910 err = qdisc_graft(dev, p, clid, q, &old_q);
911 if (err) {
912 if (q) {
fd44de7c 913 qdisc_lock_tree(dev);
1da177e4 914 qdisc_destroy(q);
fd44de7c 915 qdisc_unlock_tree(dev);
1da177e4
LT
916 }
917 return err;
918 }
919 qdisc_notify(skb, n, clid, old_q, q);
920 if (old_q) {
fd44de7c 921 qdisc_lock_tree(dev);
1da177e4 922 qdisc_destroy(old_q);
fd44de7c 923 qdisc_unlock_tree(dev);
1da177e4
LT
924 }
925 }
926 return 0;
927}
928
929static int tc_fill_qdisc(struct sk_buff *skb, struct Qdisc *q, u32 clid,
e431b8c0 930 u32 pid, u32 seq, u16 flags, int event)
1da177e4
LT
931{
932 struct tcmsg *tcm;
933 struct nlmsghdr *nlh;
27a884dc 934 unsigned char *b = skb_tail_pointer(skb);
1da177e4
LT
935 struct gnet_dump d;
936
e431b8c0 937 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*tcm), flags);
1da177e4
LT
938 tcm = NLMSG_DATA(nlh);
939 tcm->tcm_family = AF_UNSPEC;
9ef1d4c7
PM
940 tcm->tcm__pad1 = 0;
941 tcm->tcm__pad2 = 0;
5ce2d488 942 tcm->tcm_ifindex = qdisc_dev(q)->ifindex;
1da177e4
LT
943 tcm->tcm_parent = clid;
944 tcm->tcm_handle = q->handle;
945 tcm->tcm_info = atomic_read(&q->refcnt);
57e1c487 946 NLA_PUT_STRING(skb, TCA_KIND, q->ops->id);
1da177e4 947 if (q->ops->dump && q->ops->dump(q, skb) < 0)
1e90474c 948 goto nla_put_failure;
1da177e4
LT
949 q->qstats.qlen = q->q.qlen;
950
951 if (gnet_stats_start_copy_compat(skb, TCA_STATS2, TCA_STATS,
952 TCA_XSTATS, q->stats_lock, &d) < 0)
1e90474c 953 goto nla_put_failure;
1da177e4
LT
954
955 if (q->ops->dump_stats && q->ops->dump_stats(q, &d) < 0)
1e90474c 956 goto nla_put_failure;
1da177e4
LT
957
958 if (gnet_stats_copy_basic(&d, &q->bstats) < 0 ||
1da177e4 959 gnet_stats_copy_rate_est(&d, &q->rate_est) < 0 ||
1da177e4 960 gnet_stats_copy_queue(&d, &q->qstats) < 0)
1e90474c 961 goto nla_put_failure;
10297b99 962
1da177e4 963 if (gnet_stats_finish_copy(&d) < 0)
1e90474c 964 goto nla_put_failure;
10297b99 965
27a884dc 966 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1da177e4
LT
967 return skb->len;
968
969nlmsg_failure:
1e90474c 970nla_put_failure:
dc5fc579 971 nlmsg_trim(skb, b);
1da177e4
LT
972 return -1;
973}
974
975static int qdisc_notify(struct sk_buff *oskb, struct nlmsghdr *n,
976 u32 clid, struct Qdisc *old, struct Qdisc *new)
977{
978 struct sk_buff *skb;
979 u32 pid = oskb ? NETLINK_CB(oskb).pid : 0;
980
981 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
982 if (!skb)
983 return -ENOBUFS;
984
985 if (old && old->handle) {
986 if (tc_fill_qdisc(skb, old, clid, pid, n->nlmsg_seq, 0, RTM_DELQDISC) < 0)
987 goto err_out;
988 }
989 if (new) {
990 if (tc_fill_qdisc(skb, new, clid, pid, n->nlmsg_seq, old ? NLM_F_REPLACE : 0, RTM_NEWQDISC) < 0)
991 goto err_out;
992 }
993
994 if (skb->len)
97c53cac 995 return rtnetlink_send(skb, &init_net, pid, RTNLGRP_TC, n->nlmsg_flags&NLM_F_ECHO);
1da177e4
LT
996
997err_out:
998 kfree_skb(skb);
999 return -EINVAL;
1000}
1001
1002static int tc_dump_qdisc(struct sk_buff *skb, struct netlink_callback *cb)
1003{
3b1e0a65 1004 struct net *net = sock_net(skb->sk);
1da177e4
LT
1005 int idx, q_idx;
1006 int s_idx, s_q_idx;
1007 struct net_device *dev;
1008 struct Qdisc *q;
1009
b854272b
DL
1010 if (net != &init_net)
1011 return 0;
1012
1da177e4
LT
1013 s_idx = cb->args[0];
1014 s_q_idx = q_idx = cb->args[1];
1015 read_lock(&dev_base_lock);
7562f876 1016 idx = 0;
881d966b 1017 for_each_netdev(&init_net, dev) {
1da177e4 1018 if (idx < s_idx)
7562f876 1019 goto cont;
1da177e4
LT
1020 if (idx > s_idx)
1021 s_q_idx = 0;
1da177e4
LT
1022 q_idx = 0;
1023 list_for_each_entry(q, &dev->qdisc_list, list) {
1024 if (q_idx < s_q_idx) {
1025 q_idx++;
1026 continue;
1027 }
1028 if (tc_fill_qdisc(skb, q, q->parent, NETLINK_CB(cb->skb).pid,
0463d4ae 1029 cb->nlh->nlmsg_seq, NLM_F_MULTI, RTM_NEWQDISC) <= 0)
1da177e4 1030 goto done;
1da177e4
LT
1031 q_idx++;
1032 }
7562f876
PE
1033cont:
1034 idx++;
1da177e4
LT
1035 }
1036
1037done:
1038 read_unlock(&dev_base_lock);
1039
1040 cb->args[0] = idx;
1041 cb->args[1] = q_idx;
1042
1043 return skb->len;
1044}
1045
1046
1047
1048/************************************************
1049 * Traffic classes manipulation. *
1050 ************************************************/
1051
1052
1053
1054static int tc_ctl_tclass(struct sk_buff *skb, struct nlmsghdr *n, void *arg)
1055{
3b1e0a65 1056 struct net *net = sock_net(skb->sk);
1da177e4 1057 struct tcmsg *tcm = NLMSG_DATA(n);
1e90474c 1058 struct nlattr *tca[TCA_MAX + 1];
1da177e4
LT
1059 struct net_device *dev;
1060 struct Qdisc *q = NULL;
20fea08b 1061 const struct Qdisc_class_ops *cops;
1da177e4
LT
1062 unsigned long cl = 0;
1063 unsigned long new_cl;
1064 u32 pid = tcm->tcm_parent;
1065 u32 clid = tcm->tcm_handle;
1066 u32 qid = TC_H_MAJ(clid);
1067 int err;
1068
b854272b
DL
1069 if (net != &init_net)
1070 return -EINVAL;
1071
881d966b 1072 if ((dev = __dev_get_by_index(&init_net, tcm->tcm_ifindex)) == NULL)
1da177e4
LT
1073 return -ENODEV;
1074
1e90474c
PM
1075 err = nlmsg_parse(n, sizeof(*tcm), tca, TCA_MAX, NULL);
1076 if (err < 0)
1077 return err;
1078
1da177e4
LT
1079 /*
1080 parent == TC_H_UNSPEC - unspecified parent.
1081 parent == TC_H_ROOT - class is root, which has no parent.
1082 parent == X:0 - parent is root class.
1083 parent == X:Y - parent is a node in hierarchy.
1084 parent == 0:Y - parent is X:Y, where X:0 is qdisc.
1085
1086 handle == 0:0 - generate handle from kernel pool.
1087 handle == 0:Y - class is X:Y, where X:0 is qdisc.
1088 handle == X:Y - clear.
1089 handle == X:0 - root class.
1090 */
1091
1092 /* Step 1. Determine qdisc handle X:0 */
1093
1094 if (pid != TC_H_ROOT) {
1095 u32 qid1 = TC_H_MAJ(pid);
1096
1097 if (qid && qid1) {
1098 /* If both majors are known, they must be identical. */
1099 if (qid != qid1)
1100 return -EINVAL;
1101 } else if (qid1) {
1102 qid = qid1;
1103 } else if (qid == 0)
1104 qid = dev->qdisc_sleeping->handle;
1105
1106 /* Now qid is genuine qdisc handle consistent
1107 both with parent and child.
1108
1109 TC_H_MAJ(pid) still may be unspecified, complete it now.
1110 */
1111 if (pid)
1112 pid = TC_H_MAKE(qid, pid);
1113 } else {
1114 if (qid == 0)
1115 qid = dev->qdisc_sleeping->handle;
1116 }
1117
1118 /* OK. Locate qdisc */
10297b99 1119 if ((q = qdisc_lookup(dev, qid)) == NULL)
1da177e4
LT
1120 return -ENOENT;
1121
1122 /* An check that it supports classes */
1123 cops = q->ops->cl_ops;
1124 if (cops == NULL)
1125 return -EINVAL;
1126
1127 /* Now try to get class */
1128 if (clid == 0) {
1129 if (pid == TC_H_ROOT)
1130 clid = qid;
1131 } else
1132 clid = TC_H_MAKE(qid, clid);
1133
1134 if (clid)
1135 cl = cops->get(q, clid);
1136
1137 if (cl == 0) {
1138 err = -ENOENT;
1139 if (n->nlmsg_type != RTM_NEWTCLASS || !(n->nlmsg_flags&NLM_F_CREATE))
1140 goto out;
1141 } else {
1142 switch (n->nlmsg_type) {
10297b99 1143 case RTM_NEWTCLASS:
1da177e4
LT
1144 err = -EEXIST;
1145 if (n->nlmsg_flags&NLM_F_EXCL)
1146 goto out;
1147 break;
1148 case RTM_DELTCLASS:
1149 err = cops->delete(q, cl);
1150 if (err == 0)
1151 tclass_notify(skb, n, q, cl, RTM_DELTCLASS);
1152 goto out;
1153 case RTM_GETTCLASS:
1154 err = tclass_notify(skb, n, q, cl, RTM_NEWTCLASS);
1155 goto out;
1156 default:
1157 err = -EINVAL;
1158 goto out;
1159 }
1160 }
1161
1162 new_cl = cl;
1163 err = cops->change(q, clid, pid, tca, &new_cl);
1164 if (err == 0)
1165 tclass_notify(skb, n, q, new_cl, RTM_NEWTCLASS);
1166
1167out:
1168 if (cl)
1169 cops->put(q, cl);
1170
1171 return err;
1172}
1173
1174
1175static int tc_fill_tclass(struct sk_buff *skb, struct Qdisc *q,
1176 unsigned long cl,
e431b8c0 1177 u32 pid, u32 seq, u16 flags, int event)
1da177e4
LT
1178{
1179 struct tcmsg *tcm;
1180 struct nlmsghdr *nlh;
27a884dc 1181 unsigned char *b = skb_tail_pointer(skb);
1da177e4 1182 struct gnet_dump d;
20fea08b 1183 const struct Qdisc_class_ops *cl_ops = q->ops->cl_ops;
1da177e4 1184
e431b8c0 1185 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*tcm), flags);
1da177e4
LT
1186 tcm = NLMSG_DATA(nlh);
1187 tcm->tcm_family = AF_UNSPEC;
5ce2d488 1188 tcm->tcm_ifindex = qdisc_dev(q)->ifindex;
1da177e4
LT
1189 tcm->tcm_parent = q->handle;
1190 tcm->tcm_handle = q->handle;
1191 tcm->tcm_info = 0;
57e1c487 1192 NLA_PUT_STRING(skb, TCA_KIND, q->ops->id);
1da177e4 1193 if (cl_ops->dump && cl_ops->dump(q, cl, skb, tcm) < 0)
1e90474c 1194 goto nla_put_failure;
1da177e4
LT
1195
1196 if (gnet_stats_start_copy_compat(skb, TCA_STATS2, TCA_STATS,
1197 TCA_XSTATS, q->stats_lock, &d) < 0)
1e90474c 1198 goto nla_put_failure;
1da177e4
LT
1199
1200 if (cl_ops->dump_stats && cl_ops->dump_stats(q, cl, &d) < 0)
1e90474c 1201 goto nla_put_failure;
1da177e4
LT
1202
1203 if (gnet_stats_finish_copy(&d) < 0)
1e90474c 1204 goto nla_put_failure;
1da177e4 1205
27a884dc 1206 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1da177e4
LT
1207 return skb->len;
1208
1209nlmsg_failure:
1e90474c 1210nla_put_failure:
dc5fc579 1211 nlmsg_trim(skb, b);
1da177e4
LT
1212 return -1;
1213}
1214
1215static int tclass_notify(struct sk_buff *oskb, struct nlmsghdr *n,
1216 struct Qdisc *q, unsigned long cl, int event)
1217{
1218 struct sk_buff *skb;
1219 u32 pid = oskb ? NETLINK_CB(oskb).pid : 0;
1220
1221 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1222 if (!skb)
1223 return -ENOBUFS;
1224
1225 if (tc_fill_tclass(skb, q, cl, pid, n->nlmsg_seq, 0, event) < 0) {
1226 kfree_skb(skb);
1227 return -EINVAL;
1228 }
1229
97c53cac 1230 return rtnetlink_send(skb, &init_net, pid, RTNLGRP_TC, n->nlmsg_flags&NLM_F_ECHO);
1da177e4
LT
1231}
1232
1233struct qdisc_dump_args
1234{
1235 struct qdisc_walker w;
1236 struct sk_buff *skb;
1237 struct netlink_callback *cb;
1238};
1239
1240static int qdisc_class_dump(struct Qdisc *q, unsigned long cl, struct qdisc_walker *arg)
1241{
1242 struct qdisc_dump_args *a = (struct qdisc_dump_args *)arg;
1243
1244 return tc_fill_tclass(a->skb, q, cl, NETLINK_CB(a->cb->skb).pid,
1245 a->cb->nlh->nlmsg_seq, NLM_F_MULTI, RTM_NEWTCLASS);
1246}
1247
1248static int tc_dump_tclass(struct sk_buff *skb, struct netlink_callback *cb)
1249{
3b1e0a65 1250 struct net *net = sock_net(skb->sk);
1da177e4
LT
1251 int t;
1252 int s_t;
1253 struct net_device *dev;
1254 struct Qdisc *q;
1255 struct tcmsg *tcm = (struct tcmsg*)NLMSG_DATA(cb->nlh);
1256 struct qdisc_dump_args arg;
1257
b854272b
DL
1258 if (net != &init_net)
1259 return 0;
1260
1da177e4
LT
1261 if (cb->nlh->nlmsg_len < NLMSG_LENGTH(sizeof(*tcm)))
1262 return 0;
881d966b 1263 if ((dev = dev_get_by_index(&init_net, tcm->tcm_ifindex)) == NULL)
1da177e4
LT
1264 return 0;
1265
1266 s_t = cb->args[0];
1267 t = 0;
1268
1da177e4
LT
1269 list_for_each_entry(q, &dev->qdisc_list, list) {
1270 if (t < s_t || !q->ops->cl_ops ||
1271 (tcm->tcm_parent &&
1272 TC_H_MAJ(tcm->tcm_parent) != q->handle)) {
1273 t++;
1274 continue;
1275 }
1276 if (t > s_t)
1277 memset(&cb->args[1], 0, sizeof(cb->args)-sizeof(cb->args[0]));
1278 arg.w.fn = qdisc_class_dump;
1279 arg.skb = skb;
1280 arg.cb = cb;
1281 arg.w.stop = 0;
1282 arg.w.skip = cb->args[1];
1283 arg.w.count = 0;
1284 q->ops->cl_ops->walk(q, &arg.w);
1285 cb->args[1] = arg.w.count;
1286 if (arg.w.stop)
1287 break;
1288 t++;
1289 }
1da177e4
LT
1290
1291 cb->args[0] = t;
1292
1293 dev_put(dev);
1294 return skb->len;
1295}
1296
1297/* Main classifier routine: scans classifier chain attached
1298 to this qdisc, (optionally) tests for protocol and asks
1299 specific classifiers.
1300 */
73ca4918
PM
1301int tc_classify_compat(struct sk_buff *skb, struct tcf_proto *tp,
1302 struct tcf_result *res)
1303{
1304 __be16 protocol = skb->protocol;
1305 int err = 0;
1306
1307 for (; tp; tp = tp->next) {
1308 if ((tp->protocol == protocol ||
1309 tp->protocol == htons(ETH_P_ALL)) &&
1310 (err = tp->classify(skb, tp, res)) >= 0) {
1311#ifdef CONFIG_NET_CLS_ACT
1312 if (err != TC_ACT_RECLASSIFY && skb->tc_verd)
1313 skb->tc_verd = SET_TC_VERD(skb->tc_verd, 0);
1314#endif
1315 return err;
1316 }
1317 }
1318 return -1;
1319}
1320EXPORT_SYMBOL(tc_classify_compat);
1321
1da177e4 1322int tc_classify(struct sk_buff *skb, struct tcf_proto *tp,
73ca4918 1323 struct tcf_result *res)
1da177e4
LT
1324{
1325 int err = 0;
73ca4918 1326 __be16 protocol;
1da177e4
LT
1327#ifdef CONFIG_NET_CLS_ACT
1328 struct tcf_proto *otp = tp;
1329reclassify:
1330#endif
1331 protocol = skb->protocol;
1332
73ca4918 1333 err = tc_classify_compat(skb, tp, res);
1da177e4 1334#ifdef CONFIG_NET_CLS_ACT
73ca4918
PM
1335 if (err == TC_ACT_RECLASSIFY) {
1336 u32 verd = G_TC_VERD(skb->tc_verd);
1337 tp = otp;
1338
1339 if (verd++ >= MAX_REC_LOOP) {
1340 printk("rule prio %u protocol %02x reclassify loop, "
1341 "packet dropped\n",
1342 tp->prio&0xffff, ntohs(tp->protocol));
1343 return TC_ACT_SHOT;
1da177e4 1344 }
73ca4918
PM
1345 skb->tc_verd = SET_TC_VERD(skb->tc_verd, verd);
1346 goto reclassify;
1da177e4 1347 }
73ca4918
PM
1348#endif
1349 return err;
1da177e4 1350}
73ca4918 1351EXPORT_SYMBOL(tc_classify);
1da177e4 1352
a48b5a61
PM
1353void tcf_destroy(struct tcf_proto *tp)
1354{
1355 tp->ops->destroy(tp);
1356 module_put(tp->ops->owner);
1357 kfree(tp);
1358}
1359
ff31ab56 1360void tcf_destroy_chain(struct tcf_proto **fl)
a48b5a61
PM
1361{
1362 struct tcf_proto *tp;
1363
ff31ab56
PM
1364 while ((tp = *fl) != NULL) {
1365 *fl = tp->next;
a48b5a61
PM
1366 tcf_destroy(tp);
1367 }
1368}
1369EXPORT_SYMBOL(tcf_destroy_chain);
1370
1da177e4
LT
1371#ifdef CONFIG_PROC_FS
1372static int psched_show(struct seq_file *seq, void *v)
1373{
3c0cfc13
PM
1374 struct timespec ts;
1375
1376 hrtimer_get_res(CLOCK_MONOTONIC, &ts);
1da177e4 1377 seq_printf(seq, "%08x %08x %08x %08x\n",
641b9e0e 1378 (u32)NSEC_PER_USEC, (u32)PSCHED_US2NS(1),
514bca32 1379 1000000,
3c0cfc13 1380 (u32)NSEC_PER_SEC/(u32)ktime_to_ns(timespec_to_ktime(ts)));
1da177e4
LT
1381
1382 return 0;
1383}
1384
1385static int psched_open(struct inode *inode, struct file *file)
1386{
1387 return single_open(file, psched_show, PDE(inode)->data);
1388}
1389
da7071d7 1390static const struct file_operations psched_fops = {
1da177e4
LT
1391 .owner = THIS_MODULE,
1392 .open = psched_open,
1393 .read = seq_read,
1394 .llseek = seq_lseek,
1395 .release = single_release,
10297b99 1396};
1da177e4
LT
1397#endif
1398
1da177e4
LT
1399static int __init pktsched_init(void)
1400{
1da177e4
LT
1401 register_qdisc(&pfifo_qdisc_ops);
1402 register_qdisc(&bfifo_qdisc_ops);
457c4cbc 1403 proc_net_fops_create(&init_net, "psched", 0, &psched_fops);
1da177e4 1404
be577ddc
TG
1405 rtnl_register(PF_UNSPEC, RTM_NEWQDISC, tc_modify_qdisc, NULL);
1406 rtnl_register(PF_UNSPEC, RTM_DELQDISC, tc_get_qdisc, NULL);
1407 rtnl_register(PF_UNSPEC, RTM_GETQDISC, tc_get_qdisc, tc_dump_qdisc);
1408 rtnl_register(PF_UNSPEC, RTM_NEWTCLASS, tc_ctl_tclass, NULL);
1409 rtnl_register(PF_UNSPEC, RTM_DELTCLASS, tc_ctl_tclass, NULL);
1410 rtnl_register(PF_UNSPEC, RTM_GETTCLASS, tc_ctl_tclass, tc_dump_tclass);
1411
1da177e4
LT
1412 return 0;
1413}
1414
1415subsys_initcall(pktsched_init);