1 /*
2 * net/sched/sch_generic.c Generic packet scheduler routines.
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 * Jamal Hadi Salim, <hadi@cyberus.ca> 990601
11 * - Ingress support
12 */
13
14 #include <linux/bitops.h>
15 #include <linux/kallsyms.h>
16 #include <linux/module.h>
17 #include <linux/types.h>
18 #include <linux/kernel.h>
19 #include <linux/sched.h>
20 #include <linux/string.h>
21 #include <linux/errno.h>
22 #include <linux/netdevice.h>
23 #include <linux/skbuff.h>
24 #include <linux/rtnetlink.h>
25 #include <linux/init.h>
26 #include <linux/rcupdate.h>
27 #include <linux/list.h>
28 #include <linux/delay.h>
29 #include <net/pkt_sched.h>
30
31 /* Main transmission queue. */
32
33 /* Modifications to data participating in scheduling must be protected with
34 * dev->queue_lock spinlock.
35 *
36 * The idea is the following:
37 * - enqueue, dequeue are serialized via top level device
38 * spinlock dev->queue_lock.
39 * - ingress filtering is serialized via top level device
40 * spinlock dev->ingress_lock.
41 * - updates to tree and tree walking are only done under the rtnl mutex.
42 */
43
44 void qdisc_lock_tree(struct net_device *dev)
45 __acquires(dev->queue_lock)
46 __acquires(dev->ingress_lock)
47 {
48 spin_lock_bh(&dev->queue_lock);
49 spin_lock(&dev->ingress_lock);
50 }
51 EXPORT_SYMBOL(qdisc_lock_tree);
52
53 void qdisc_unlock_tree(struct net_device *dev)
54 __releases(dev->ingress_lock)
55 __releases(dev->queue_lock)
56 {
57 spin_unlock(&dev->ingress_lock);
58 spin_unlock_bh(&dev->queue_lock);
59 }
60 EXPORT_SYMBOL(qdisc_unlock_tree);
61
62 static inline int qdisc_qlen(struct Qdisc *q)
63 {
64 return q->q.qlen;
65 }
66
67 static inline int dev_requeue_skb(struct sk_buff *skb, struct net_device *dev,
68 struct Qdisc *q)
69 {
70 if (unlikely(skb->next))
71 dev->gso_skb = skb;
72 else
73 q->ops->requeue(skb, q);
74
75 netif_schedule(dev);
76 return 0;
77 }
78
79 static inline struct sk_buff *dev_dequeue_skb(struct net_device *dev,
80 struct Qdisc *q)
81 {
82 struct sk_buff *skb;
83
84 if ((skb = dev->gso_skb))
85 dev->gso_skb = NULL;
86 else
87 skb = q->dequeue(q);
88
89 return skb;
90 }
91
92 static inline int handle_dev_cpu_collision(struct sk_buff *skb,
93 struct net_device *dev,
94 struct Qdisc *q)
95 {
96 int ret;
97
98 if (unlikely(dev->xmit_lock_owner == (void *)current)) {
99 /*
100 * Same CPU holding the lock. It may be a transient
101 * configuration error, when hard_start_xmit() recurses. We
102 * detect it by checking xmit owner and drop the packet when
103 * deadloop is detected. Return OK to try the next skb.
104 */
105 kfree_skb(skb);
106 if (net_ratelimit())
107 printk(KERN_WARNING "Dead loop on netdevice %s, "
108 "fix it urgently!\n", dev->name);
109 ret = qdisc_qlen(q);
110 } else {
111 /*
112 * Another cpu is holding lock, requeue & delay xmits for
113 * some time.
114 */
115 __get_cpu_var(netdev_rx_stat).cpu_collision++;
116 ret = dev_requeue_skb(skb, dev, q);
117 }
118
119 return ret;
120 }
121
122 /*
123 * NOTE: Called under dev->queue_lock with locally disabled BH.
124 *
125 * __LINK_STATE_QDISC_RUNNING guarantees only one CPU can process this
126 * device at a time. dev->queue_lock serializes queue accesses for
127 * this device AND dev->qdisc pointer itself.
128 *
129 * netif_tx_lock serializes accesses to device driver.
130 *
131 * dev->queue_lock and netif_tx_lock are mutually exclusive,
132 * if one is grabbed, another must be free.
133 *
134 * Note, that this procedure can be called by a watchdog timer
135 *
136 * Returns to the caller:
137 * 0 - queue is empty or throttled.
138 * >0 - queue is not empty.
139 *
140 */
141 static inline int qdisc_restart(struct net_device *dev)
142 {
143 struct Qdisc *q = dev->qdisc;
144 struct sk_buff *skb;
145 int ret = NETDEV_TX_BUSY;
146
147 /* Dequeue packet */
148 if (unlikely((skb = dev_dequeue_skb(dev, q)) == NULL))
149 return 0;
150
151
152 /* And release queue */
153 spin_unlock(&dev->queue_lock);
154
155 HARD_TX_LOCK(dev);
156 if (!netif_subqueue_stopped(dev, skb))
157 ret = dev_hard_start_xmit(skb, dev);
158 HARD_TX_UNLOCK(dev);
159
160 spin_lock(&dev->queue_lock);
161 q = dev->qdisc;
162
163 switch (ret) {
164 case NETDEV_TX_OK:
165 /* Driver sent out skb successfully */
166 ret = qdisc_qlen(q);
167 break;
168
169 case NETDEV_TX_LOCKED:
170 /* Driver try lock failed */
171 ret = handle_dev_cpu_collision(skb, dev, q);
172 break;
173
174 default:
175 /* Driver returned NETDEV_TX_BUSY - requeue skb */
176 if (unlikely (ret != NETDEV_TX_BUSY && net_ratelimit()))
177 printk(KERN_WARNING "BUG %s code %d qlen %d\n",
178 dev->name, ret, q->q.qlen);
179
180 ret = dev_requeue_skb(skb, dev, q);
181 break;
182 }
183
184 return ret;
185 }
186
187 void __qdisc_run(struct net_device *dev)
188 {
189 unsigned long start_time = jiffies;
190
191 while (qdisc_restart(dev)) {
192 if (netif_queue_stopped(dev))
193 break;
194
195 /*
196 * Postpone processing if
197 * 1. another process needs the CPU;
198 * 2. we've been doing it for too long.
199 */
200 if (need_resched() || jiffies != start_time) {
201 netif_schedule(dev);
202 break;
203 }
204 }
205
206 clear_bit(__LINK_STATE_QDISC_RUNNING, &dev->state);
207 }
208
209 static void dev_watchdog(unsigned long arg)
210 {
211 struct net_device *dev = (struct net_device *)arg;
212
213 netif_tx_lock(dev);
214 if (dev->qdisc != &noop_qdisc) {
215 if (netif_device_present(dev) &&
216 netif_running(dev) &&
217 netif_carrier_ok(dev)) {
218 if (netif_queue_stopped(dev) &&
219 time_after(jiffies, dev->trans_start + dev->watchdog_timeo)) {
220
221 printk(KERN_INFO "NETDEV WATCHDOG: %s: transmit timed out\n",
222 dev->name);
223 dev->tx_timeout(dev);
224 }
225 if (!mod_timer(&dev->watchdog_timer, round_jiffies(jiffies + dev->watchdog_timeo)))
226 dev_hold(dev);
227 }
228 }
229 netif_tx_unlock(dev);
230
231 dev_put(dev);
232 }
233
234 void __netdev_watchdog_up(struct net_device *dev)
235 {
236 if (dev->tx_timeout) {
237 if (dev->watchdog_timeo <= 0)
238 dev->watchdog_timeo = 5*HZ;
239 if (!mod_timer(&dev->watchdog_timer,
240 round_jiffies(jiffies + dev->watchdog_timeo)))
241 dev_hold(dev);
242 }
243 }
244
245 static void dev_watchdog_up(struct net_device *dev)
246 {
247 __netdev_watchdog_up(dev);
248 }
249
250 static void dev_watchdog_down(struct net_device *dev)
251 {
252 netif_tx_lock_bh(dev);
253 if (del_timer(&dev->watchdog_timer))
254 dev_put(dev);
255 netif_tx_unlock_bh(dev);
256 }
257
258 /**
259 * netif_carrier_on - set carrier
260 * @dev: network device
261 *
262 * Device has detected that carrier.
263 */
264 void netif_carrier_on(struct net_device *dev)
265 {
266 if (test_and_clear_bit(__LINK_STATE_NOCARRIER, &dev->state)) {
267 linkwatch_fire_event(dev);
268 if (netif_running(dev))
269 __netdev_watchdog_up(dev);
270 }
271 }
272 EXPORT_SYMBOL(netif_carrier_on);
273
274 /**
275 * netif_carrier_off - clear carrier
276 * @dev: network device
277 *
278 * Device has detected loss of carrier.
279 */
280 void netif_carrier_off(struct net_device *dev)
281 {
282 if (!test_and_set_bit(__LINK_STATE_NOCARRIER, &dev->state))
283 linkwatch_fire_event(dev);
284 }
285 EXPORT_SYMBOL(netif_carrier_off);
286
287 /* "NOOP" scheduler: the best scheduler, recommended for all interfaces
288 under all circumstances. It is difficult to invent anything faster or
289 cheaper.
290 */
291
292 static int noop_enqueue(struct sk_buff *skb, struct Qdisc * qdisc)
293 {
294 kfree_skb(skb);
295 return NET_XMIT_CN;
296 }
297
298 static struct sk_buff *noop_dequeue(struct Qdisc * qdisc)
299 {
300 return NULL;
301 }
302
303 static int noop_requeue(struct sk_buff *skb, struct Qdisc* qdisc)
304 {
305 if (net_ratelimit())
306 printk(KERN_DEBUG "%s deferred output. It is buggy.\n",
307 skb->dev->name);
308 kfree_skb(skb);
309 return NET_XMIT_CN;
310 }
311
312 struct Qdisc_ops noop_qdisc_ops __read_mostly = {
313 .id = "noop",
314 .priv_size = 0,
315 .enqueue = noop_enqueue,
316 .dequeue = noop_dequeue,
317 .requeue = noop_requeue,
318 .owner = THIS_MODULE,
319 };
320
321 struct Qdisc noop_qdisc = {
322 .enqueue = noop_enqueue,
323 .dequeue = noop_dequeue,
324 .flags = TCQ_F_BUILTIN,
325 .ops = &noop_qdisc_ops,
326 .list = LIST_HEAD_INIT(noop_qdisc.list),
327 };
328 EXPORT_SYMBOL(noop_qdisc);
329
330 static struct Qdisc_ops noqueue_qdisc_ops __read_mostly = {
331 .id = "noqueue",
332 .priv_size = 0,
333 .enqueue = noop_enqueue,
334 .dequeue = noop_dequeue,
335 .requeue = noop_requeue,
336 .owner = THIS_MODULE,
337 };
338
339 static struct Qdisc noqueue_qdisc = {
340 .enqueue = NULL,
341 .dequeue = noop_dequeue,
342 .flags = TCQ_F_BUILTIN,
343 .ops = &noqueue_qdisc_ops,
344 .list = LIST_HEAD_INIT(noqueue_qdisc.list),
345 };
346
347
348 static const u8 prio2band[TC_PRIO_MAX+1] =
349 { 1, 2, 2, 2, 1, 2, 0, 0 , 1, 1, 1, 1, 1, 1, 1, 1 };
350
351 /* 3-band FIFO queue: old style, but should be a bit faster than
352 generic prio+fifo combination.
353 */
354
355 #define PFIFO_FAST_BANDS 3
356
357 static inline struct sk_buff_head *prio2list(struct sk_buff *skb,
358 struct Qdisc *qdisc)
359 {
360 struct sk_buff_head *list = qdisc_priv(qdisc);
361 return list + prio2band[skb->priority & TC_PRIO_MAX];
362 }
363
364 static int pfifo_fast_enqueue(struct sk_buff *skb, struct Qdisc* qdisc)
365 {
366 struct sk_buff_head *list = prio2list(skb, qdisc);
367
368 if (skb_queue_len(list) < qdisc->dev->tx_queue_len) {
369 qdisc->q.qlen++;
370 return __qdisc_enqueue_tail(skb, qdisc, list);
371 }
372
373 return qdisc_drop(skb, qdisc);
374 }
375
376 static struct sk_buff *pfifo_fast_dequeue(struct Qdisc* qdisc)
377 {
378 int prio;
379 struct sk_buff_head *list = qdisc_priv(qdisc);
380
381 for (prio = 0; prio < PFIFO_FAST_BANDS; prio++) {
382 if (!skb_queue_empty(list + prio)) {
383 qdisc->q.qlen--;
384 return __qdisc_dequeue_head(qdisc, list + prio);
385 }
386 }
387
388 return NULL;
389 }
390
391 static int pfifo_fast_requeue(struct sk_buff *skb, struct Qdisc* qdisc)
392 {
393 qdisc->q.qlen++;
394 return __qdisc_requeue(skb, qdisc, prio2list(skb, qdisc));
395 }
396
397 static void pfifo_fast_reset(struct Qdisc* qdisc)
398 {
399 int prio;
400 struct sk_buff_head *list = qdisc_priv(qdisc);
401
402 for (prio = 0; prio < PFIFO_FAST_BANDS; prio++)
403 __qdisc_reset_queue(qdisc, list + prio);
404
405 qdisc->qstats.backlog = 0;
406 qdisc->q.qlen = 0;
407 }
408
409 static int pfifo_fast_dump(struct Qdisc *qdisc, struct sk_buff *skb)
410 {
411 struct tc_prio_qopt opt = { .bands = PFIFO_FAST_BANDS };
412
413 memcpy(&opt.priomap, prio2band, TC_PRIO_MAX+1);
414 NLA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
415 return skb->len;
416
417 nla_put_failure:
418 return -1;
419 }
420
421 static int pfifo_fast_init(struct Qdisc *qdisc, struct nlattr *opt)
422 {
423 int prio;
424 struct sk_buff_head *list = qdisc_priv(qdisc);
425
426 for (prio = 0; prio < PFIFO_FAST_BANDS; prio++)
427 skb_queue_head_init(list + prio);
428
429 return 0;
430 }
431
432 static struct Qdisc_ops pfifo_fast_ops __read_mostly = {
433 .id = "pfifo_fast",
434 .priv_size = PFIFO_FAST_BANDS * sizeof(struct sk_buff_head),
435 .enqueue = pfifo_fast_enqueue,
436 .dequeue = pfifo_fast_dequeue,
437 .requeue = pfifo_fast_requeue,
438 .init = pfifo_fast_init,
439 .reset = pfifo_fast_reset,
440 .dump = pfifo_fast_dump,
441 .owner = THIS_MODULE,
442 };
443
444 struct Qdisc *qdisc_alloc(struct net_device *dev, struct Qdisc_ops *ops)
445 {
446 void *p;
447 struct Qdisc *sch;
448 unsigned int size;
449 int err = -ENOBUFS;
450
451 /* ensure that the Qdisc and the private data are 32-byte aligned */
452 size = QDISC_ALIGN(sizeof(*sch));
453 size += ops->priv_size + (QDISC_ALIGNTO - 1);
454
455 p = kzalloc(size, GFP_KERNEL);
456 if (!p)
457 goto errout;
458 sch = (struct Qdisc *) QDISC_ALIGN((unsigned long) p);
459 sch->padded = (char *) sch - (char *) p;
460
461 INIT_LIST_HEAD(&sch->list);
462 skb_queue_head_init(&sch->q);
463 sch->ops = ops;
464 sch->enqueue = ops->enqueue;
465 sch->dequeue = ops->dequeue;
466 sch->dev = dev;
467 dev_hold(dev);
468 atomic_set(&sch->refcnt, 1);
469
470 return sch;
471 errout:
472 return ERR_PTR(-err);
473 }
474
475 struct Qdisc * qdisc_create_dflt(struct net_device *dev, struct Qdisc_ops *ops,
476 unsigned int parentid)
477 {
478 struct Qdisc *sch;
479
480 sch = qdisc_alloc(dev, ops);
481 if (IS_ERR(sch))
482 goto errout;
483 sch->stats_lock = &dev->queue_lock;
484 sch->parent = parentid;
485
486 if (!ops->init || ops->init(sch, NULL) == 0)
487 return sch;
488
489 qdisc_destroy(sch);
490 errout:
491 return NULL;
492 }
493 EXPORT_SYMBOL(qdisc_create_dflt);
494
495 /* Under dev->queue_lock and BH! */
496
497 void qdisc_reset(struct Qdisc *qdisc)
498 {
499 const struct Qdisc_ops *ops = qdisc->ops;
500
501 if (ops->reset)
502 ops->reset(qdisc);
503 }
504 EXPORT_SYMBOL(qdisc_reset);
505
506 /* this is the rcu callback function to clean up a qdisc when there
507 * are no further references to it */
508
509 static void __qdisc_destroy(struct rcu_head *head)
510 {
511 struct Qdisc *qdisc = container_of(head, struct Qdisc, q_rcu);
512 kfree((char *) qdisc - qdisc->padded);
513 }
514
515 /* Under dev->queue_lock and BH! */
516
517 void qdisc_destroy(struct Qdisc *qdisc)
518 {
519 const struct Qdisc_ops *ops = qdisc->ops;
520
521 if (qdisc->flags & TCQ_F_BUILTIN ||
522 !atomic_dec_and_test(&qdisc->refcnt))
523 return;
524
525 list_del(&qdisc->list);
526 gen_kill_estimator(&qdisc->bstats, &qdisc->rate_est);
527 if (ops->reset)
528 ops->reset(qdisc);
529 if (ops->destroy)
530 ops->destroy(qdisc);
531
532 module_put(ops->owner);
533 dev_put(qdisc->dev);
534 call_rcu(&qdisc->q_rcu, __qdisc_destroy);
535 }
536 EXPORT_SYMBOL(qdisc_destroy);
537
538 void dev_activate(struct net_device *dev)
539 {
540 /* No queueing discipline is attached to device;
541 create default one i.e. pfifo_fast for devices,
542 which need queueing and noqueue_qdisc for
543 virtual interfaces
544 */
545
546 if (dev->qdisc_sleeping == &noop_qdisc) {
547 struct Qdisc *qdisc;
548 if (dev->tx_queue_len) {
549 qdisc = qdisc_create_dflt(dev, &pfifo_fast_ops,
550 TC_H_ROOT);
551 if (qdisc == NULL) {
552 printk(KERN_INFO "%s: activation failed\n", dev->name);
553 return;
554 }
555 list_add_tail(&qdisc->list, &dev->qdisc_list);
556 } else {
557 qdisc = &noqueue_qdisc;
558 }
559 dev->qdisc_sleeping = qdisc;
560 }
561
562 if (!netif_carrier_ok(dev))
563 /* Delay activation until next carrier-on event */
564 return;
565
566 spin_lock_bh(&dev->queue_lock);
567 rcu_assign_pointer(dev->qdisc, dev->qdisc_sleeping);
568 if (dev->qdisc != &noqueue_qdisc) {
569 dev->trans_start = jiffies;
570 dev_watchdog_up(dev);
571 }
572 spin_unlock_bh(&dev->queue_lock);
573 }
574
575 void dev_deactivate(struct net_device *dev)
576 {
577 struct Qdisc *qdisc;
578 struct sk_buff *skb;
579 int running;
580
581 spin_lock_bh(&dev->queue_lock);
582 qdisc = dev->qdisc;
583 dev->qdisc = &noop_qdisc;
584
585 qdisc_reset(qdisc);
586
587 skb = dev->gso_skb;
588 dev->gso_skb = NULL;
589 spin_unlock_bh(&dev->queue_lock);
590
591 kfree_skb(skb);
592
593 dev_watchdog_down(dev);
594
595 /* Wait for outstanding qdisc-less dev_queue_xmit calls. */
596 synchronize_rcu();
597
598 /* Wait for outstanding qdisc_run calls. */
599 do {
600 /*
601 * Wait for outstanding qdisc_run calls.
602 * TODO: shouldnt this be wakeup-based, instead of polling it?
603 */
604 while (test_bit(__LINK_STATE_QDISC_RUNNING, &dev->state))
605 msleep(1);
606
607 /*
608 * Double-check inside queue lock to ensure that all effects
609 * of the queue run are visible when we return.
610 */
611 spin_lock_bh(&dev->queue_lock);
612 running = test_bit(__LINK_STATE_QDISC_RUNNING, &dev->state);
613 spin_unlock_bh(&dev->queue_lock);
614
615 /*
616 * The running flag should never be set at this point because
617 * we've already set dev->qdisc to noop_qdisc *inside* the same
618 * pair of spin locks. That is, if any qdisc_run starts after
619 * our initial test it should see the noop_qdisc and then
620 * clear the RUNNING bit before dropping the queue lock. So
621 * if it is set here then we've found a bug.
622 */
623 } while (WARN_ON_ONCE(running));
624 }
625
626 void dev_init_scheduler(struct net_device *dev)
627 {
628 qdisc_lock_tree(dev);
629 dev->qdisc = &noop_qdisc;
630 dev->qdisc_sleeping = &noop_qdisc;
631 INIT_LIST_HEAD(&dev->qdisc_list);
632 qdisc_unlock_tree(dev);
633
634 setup_timer(&dev->watchdog_timer, dev_watchdog, (unsigned long)dev);
635 }
636
637 void dev_shutdown(struct net_device *dev)
638 {
639 struct Qdisc *qdisc;
640
641 qdisc_lock_tree(dev);
642 qdisc = dev->qdisc_sleeping;
643 dev->qdisc = &noop_qdisc;
644 dev->qdisc_sleeping = &noop_qdisc;
645 qdisc_destroy(qdisc);
646 #if defined(CONFIG_NET_SCH_INGRESS) || defined(CONFIG_NET_SCH_INGRESS_MODULE)
647 if ((qdisc = dev->qdisc_ingress) != NULL) {
648 dev->qdisc_ingress = NULL;
649 qdisc_destroy(qdisc);
650 }
651 #endif
652 BUG_TRAP(!timer_pending(&dev->watchdog_timer));
653 qdisc_unlock_tree(dev);
654 }
655
|
This page was automatically generated by the
LXR engine.
|