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