00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include <linux/bitops.h>
00015 #include <linux/module.h>
00016 #include <linux/types.h>
00017 #include <linux/kernel.h>
00018 #include <linux/sched.h>
00019 #include <linux/string.h>
00020 #include <linux/errno.h>
00021 #include <linux/netdevice.h>
00022 #include <linux/skbuff.h>
00023 #include <linux/rtnetlink.h>
00024 #include <linux/init.h>
00025 #include <linux/rcupdate.h>
00026 #include <linux/list.h>
00027 #include <net/pkt_sched.h>
00028
00029 #ifdef DDE_LINUX
00030 #include "local.h"
00031 #endif
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044 static inline int qdisc_qlen(struct Qdisc *q)
00045 {
00046 return q->q.qlen;
00047 }
00048
00049 static inline int dev_requeue_skb(struct sk_buff *skb, struct Qdisc *q)
00050 {
00051 q->gso_skb = skb;
00052 q->qstats.requeues++;
00053 __netif_schedule(q);
00054
00055 return 0;
00056 }
00057
00058 static inline struct sk_buff *dequeue_skb(struct Qdisc *q)
00059 {
00060 struct sk_buff *skb = q->gso_skb;
00061
00062 if (unlikely(skb)) {
00063 struct net_device *dev = qdisc_dev(q);
00064 struct netdev_queue *txq;
00065
00066
00067 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
00068 if (!netif_tx_queue_stopped(txq) && !netif_tx_queue_frozen(txq))
00069 q->gso_skb = NULL;
00070 else
00071 skb = NULL;
00072 } else {
00073 skb = q->dequeue(q);
00074 }
00075
00076 return skb;
00077 }
00078
00079 static inline int handle_dev_cpu_collision(struct sk_buff *skb,
00080 struct netdev_queue *dev_queue,
00081 struct Qdisc *q)
00082 {
00083 int ret;
00084
00085 if (unlikely(dev_queue->xmit_lock_owner == smp_processor_id())) {
00086
00087
00088
00089
00090
00091
00092 kfree_skb(skb);
00093 if (net_ratelimit())
00094 printk(KERN_WARNING "Dead loop on netdevice %s, "
00095 "fix it urgently!\n", dev_queue->dev->name);
00096 ret = qdisc_qlen(q);
00097 } else {
00098
00099
00100
00101
00102 __get_cpu_var(netdev_rx_stat).cpu_collision++;
00103 ret = dev_requeue_skb(skb, q);
00104 }
00105
00106 return ret;
00107 }
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128 static inline int qdisc_restart(struct Qdisc *q)
00129 {
00130 struct netdev_queue *txq;
00131 int ret = NETDEV_TX_BUSY;
00132 struct net_device *dev;
00133 spinlock_t *root_lock;
00134 struct sk_buff *skb;
00135
00136
00137 if (unlikely((skb = dequeue_skb(q)) == NULL))
00138 return 0;
00139
00140 root_lock = qdisc_lock(q);
00141
00142
00143 spin_unlock(root_lock);
00144
00145 dev = qdisc_dev(q);
00146 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
00147
00148 HARD_TX_LOCK(dev, txq, smp_processor_id());
00149 if (!netif_tx_queue_stopped(txq) &&
00150 !netif_tx_queue_frozen(txq))
00151 ret = dev_hard_start_xmit(skb, dev, txq);
00152 HARD_TX_UNLOCK(dev, txq);
00153
00154 spin_lock(root_lock);
00155
00156 switch (ret) {
00157 case NETDEV_TX_OK:
00158
00159 ret = qdisc_qlen(q);
00160 break;
00161
00162 case NETDEV_TX_LOCKED:
00163
00164 ret = handle_dev_cpu_collision(skb, txq, q);
00165 break;
00166
00167 default:
00168
00169 if (unlikely (ret != NETDEV_TX_BUSY && net_ratelimit()))
00170 printk(KERN_WARNING "BUG %s code %d qlen %d\n",
00171 dev->name, ret, q->q.qlen);
00172
00173 ret = dev_requeue_skb(skb, q);
00174 break;
00175 }
00176
00177 if (ret && (netif_tx_queue_stopped(txq) ||
00178 netif_tx_queue_frozen(txq)))
00179 ret = 0;
00180
00181 return ret;
00182 }
00183
00184 void __qdisc_run(struct Qdisc *q)
00185 {
00186 unsigned long start_time = jiffies;
00187
00188 while (qdisc_restart(q)) {
00189
00190
00191
00192
00193
00194 if (need_resched() || jiffies != start_time) {
00195 __netif_schedule(q);
00196 break;
00197 }
00198 }
00199
00200 clear_bit(__QDISC_STATE_RUNNING, &q->state);
00201 }
00202
00203 static void dev_watchdog(unsigned long arg)
00204 {
00205 struct net_device *dev = (struct net_device *)arg;
00206
00207 netif_tx_lock(dev);
00208 if (!qdisc_tx_is_noop(dev)) {
00209 if (netif_device_present(dev) &&
00210 netif_running(dev) &&
00211 netif_carrier_ok(dev)) {
00212 int some_queue_stopped = 0;
00213 unsigned int i;
00214
00215 for (i = 0; i < dev->num_tx_queues; i++) {
00216 struct netdev_queue *txq;
00217
00218 txq = netdev_get_tx_queue(dev, i);
00219 if (netif_tx_queue_stopped(txq)) {
00220 some_queue_stopped = 1;
00221 break;
00222 }
00223 }
00224
00225 if (some_queue_stopped &&
00226 time_after(jiffies, (dev->trans_start +
00227 dev->watchdog_timeo))) {
00228 char drivername[64];
00229 WARN_ONCE(1, KERN_INFO "NETDEV WATCHDOG: %s (%s): transmit timed out\n",
00230 dev->name, netdev_drivername(dev, drivername, 64));
00231 dev->netdev_ops->ndo_tx_timeout(dev);
00232 }
00233 if (!mod_timer(&dev->watchdog_timer,
00234 round_jiffies(jiffies +
00235 dev->watchdog_timeo)))
00236 dev_hold(dev);
00237 }
00238 }
00239 netif_tx_unlock(dev);
00240
00241 dev_put(dev);
00242 }
00243
00244 void __netdev_watchdog_up(struct net_device *dev)
00245 {
00246 if (dev->netdev_ops->ndo_tx_timeout) {
00247 if (dev->watchdog_timeo <= 0)
00248 dev->watchdog_timeo = 5*HZ;
00249 if (!mod_timer(&dev->watchdog_timer,
00250 round_jiffies(jiffies + dev->watchdog_timeo)))
00251 dev_hold(dev);
00252 }
00253 }
00254
00255 static void dev_watchdog_up(struct net_device *dev)
00256 {
00257 __netdev_watchdog_up(dev);
00258 }
00259
00260 static void dev_watchdog_down(struct net_device *dev)
00261 {
00262 netif_tx_lock_bh(dev);
00263 if (del_timer(&dev->watchdog_timer))
00264 dev_put(dev);
00265 netif_tx_unlock_bh(dev);
00266 }
00267
00268
00269
00270
00271
00272
00273
00274 void netif_carrier_on(struct net_device *dev)
00275 {
00276 if (test_and_clear_bit(__LINK_STATE_NOCARRIER, &dev->state)) {
00277 if (dev->reg_state == NETREG_UNINITIALIZED)
00278 return;
00279 linkwatch_fire_event(dev);
00280 if (netif_running(dev))
00281 __netdev_watchdog_up(dev);
00282 }
00283 }
00284 EXPORT_SYMBOL(netif_carrier_on);
00285
00286
00287
00288
00289
00290
00291
00292 void netif_carrier_off(struct net_device *dev)
00293 {
00294 if (!test_and_set_bit(__LINK_STATE_NOCARRIER, &dev->state)) {
00295 if (dev->reg_state == NETREG_UNINITIALIZED)
00296 return;
00297 linkwatch_fire_event(dev);
00298 }
00299 }
00300 EXPORT_SYMBOL(netif_carrier_off);
00301
00302
00303
00304
00305
00306
00307 static int noop_enqueue(struct sk_buff *skb, struct Qdisc * qdisc)
00308 {
00309 kfree_skb(skb);
00310 return NET_XMIT_CN;
00311 }
00312
00313 static struct sk_buff *noop_dequeue(struct Qdisc * qdisc)
00314 {
00315 return NULL;
00316 }
00317
00318 struct Qdisc_ops noop_qdisc_ops __read_mostly = {
00319 .id = "noop",
00320 .priv_size = 0,
00321 .enqueue = noop_enqueue,
00322 .dequeue = noop_dequeue,
00323 .peek = noop_dequeue,
00324 .owner = THIS_MODULE,
00325 };
00326
00327 static struct netdev_queue noop_netdev_queue = {
00328 .qdisc = &noop_qdisc,
00329 .qdisc_sleeping = &noop_qdisc,
00330 };
00331
00332 struct Qdisc noop_qdisc = {
00333 .enqueue = noop_enqueue,
00334 .dequeue = noop_dequeue,
00335 .flags = TCQ_F_BUILTIN,
00336 .ops = &noop_qdisc_ops,
00337 .list = LIST_HEAD_INIT(noop_qdisc.list),
00338 .q.lock = __SPIN_LOCK_UNLOCKED(noop_qdisc.q.lock),
00339 .dev_queue = &noop_netdev_queue,
00340 };
00341 EXPORT_SYMBOL(noop_qdisc);
00342
00343 static struct Qdisc_ops noqueue_qdisc_ops __read_mostly = {
00344 .id = "noqueue",
00345 .priv_size = 0,
00346 .enqueue = noop_enqueue,
00347 .dequeue = noop_dequeue,
00348 .peek = noop_dequeue,
00349 .owner = THIS_MODULE,
00350 };
00351
00352 static struct Qdisc noqueue_qdisc;
00353 static struct netdev_queue noqueue_netdev_queue = {
00354 .qdisc = &noqueue_qdisc,
00355 .qdisc_sleeping = &noqueue_qdisc,
00356 };
00357
00358 static struct Qdisc noqueue_qdisc = {
00359 .enqueue = NULL,
00360 .dequeue = noop_dequeue,
00361 .flags = TCQ_F_BUILTIN,
00362 .ops = &noqueue_qdisc_ops,
00363 .list = LIST_HEAD_INIT(noqueue_qdisc.list),
00364 .q.lock = __SPIN_LOCK_UNLOCKED(noqueue_qdisc.q.lock),
00365 .dev_queue = &noqueue_netdev_queue,
00366 };
00367
00368
00369 static const u8 prio2band[TC_PRIO_MAX+1] =
00370 { 1, 2, 2, 2, 1, 2, 0, 0 , 1, 1, 1, 1, 1, 1, 1, 1 };
00371
00372
00373
00374
00375
00376 #define PFIFO_FAST_BANDS 3
00377
00378 static inline struct sk_buff_head *prio2list(struct sk_buff *skb,
00379 struct Qdisc *qdisc)
00380 {
00381 struct sk_buff_head *list = qdisc_priv(qdisc);
00382 return list + prio2band[skb->priority & TC_PRIO_MAX];
00383 }
00384
00385 static int pfifo_fast_enqueue(struct sk_buff *skb, struct Qdisc* qdisc)
00386 {
00387 struct sk_buff_head *list = prio2list(skb, qdisc);
00388
00389 if (skb_queue_len(list) < qdisc_dev(qdisc)->tx_queue_len) {
00390 qdisc->q.qlen++;
00391 return __qdisc_enqueue_tail(skb, qdisc, list);
00392 }
00393
00394 return qdisc_drop(skb, qdisc);
00395 }
00396
00397 static struct sk_buff *pfifo_fast_dequeue(struct Qdisc* qdisc)
00398 {
00399 int prio;
00400 struct sk_buff_head *list = qdisc_priv(qdisc);
00401
00402 for (prio = 0; prio < PFIFO_FAST_BANDS; prio++) {
00403 if (!skb_queue_empty(list + prio)) {
00404 qdisc->q.qlen--;
00405 return __qdisc_dequeue_head(qdisc, list + prio);
00406 }
00407 }
00408
00409 return NULL;
00410 }
00411
00412 static struct sk_buff *pfifo_fast_peek(struct Qdisc* qdisc)
00413 {
00414 int prio;
00415 struct sk_buff_head *list = qdisc_priv(qdisc);
00416
00417 for (prio = 0; prio < PFIFO_FAST_BANDS; prio++) {
00418 if (!skb_queue_empty(list + prio))
00419 return skb_peek(list + prio);
00420 }
00421
00422 return NULL;
00423 }
00424
00425 static void pfifo_fast_reset(struct Qdisc* qdisc)
00426 {
00427 int prio;
00428 struct sk_buff_head *list = qdisc_priv(qdisc);
00429
00430 for (prio = 0; prio < PFIFO_FAST_BANDS; prio++)
00431 __qdisc_reset_queue(qdisc, list + prio);
00432
00433 qdisc->qstats.backlog = 0;
00434 qdisc->q.qlen = 0;
00435 }
00436
00437 static int pfifo_fast_dump(struct Qdisc *qdisc, struct sk_buff *skb)
00438 {
00439 struct tc_prio_qopt opt = { .bands = PFIFO_FAST_BANDS };
00440
00441 #ifndef DDE_LINUX
00442 memcpy(&opt.priomap, prio2band, TC_PRIO_MAX+1);
00443 NLA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
00444 #else
00445 WARN_UNIMPL;
00446 #endif
00447 return skb->len;
00448
00449 nla_put_failure:
00450 return -1;
00451 }
00452
00453 static int pfifo_fast_init(struct Qdisc *qdisc, struct nlattr *opt)
00454 {
00455 int prio;
00456 struct sk_buff_head *list = qdisc_priv(qdisc);
00457
00458 for (prio = 0; prio < PFIFO_FAST_BANDS; prio++)
00459 skb_queue_head_init(list + prio);
00460
00461 return 0;
00462 }
00463
00464 static struct Qdisc_ops pfifo_fast_ops __read_mostly = {
00465 .id = "pfifo_fast",
00466 .priv_size = PFIFO_FAST_BANDS * sizeof(struct sk_buff_head),
00467 .enqueue = pfifo_fast_enqueue,
00468 .dequeue = pfifo_fast_dequeue,
00469 .peek = pfifo_fast_peek,
00470 .init = pfifo_fast_init,
00471 .reset = pfifo_fast_reset,
00472 .dump = pfifo_fast_dump,
00473 .owner = THIS_MODULE,
00474 };
00475
00476 struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue,
00477 struct Qdisc_ops *ops)
00478 {
00479 void *p;
00480 struct Qdisc *sch;
00481 unsigned int size;
00482 int err = -ENOBUFS;
00483
00484
00485 size = QDISC_ALIGN(sizeof(*sch));
00486 size += ops->priv_size + (QDISC_ALIGNTO - 1);
00487
00488 p = kzalloc(size, GFP_KERNEL);
00489 if (!p)
00490 goto errout;
00491 sch = (struct Qdisc *) QDISC_ALIGN((unsigned long) p);
00492 sch->padded = (char *) sch - (char *) p;
00493
00494 INIT_LIST_HEAD(&sch->list);
00495 skb_queue_head_init(&sch->q);
00496 sch->ops = ops;
00497 sch->enqueue = ops->enqueue;
00498 sch->dequeue = ops->dequeue;
00499 sch->dev_queue = dev_queue;
00500 dev_hold(qdisc_dev(sch));
00501 atomic_set(&sch->refcnt, 1);
00502
00503 return sch;
00504 errout:
00505 return ERR_PTR(err);
00506 }
00507
00508 struct Qdisc * qdisc_create_dflt(struct net_device *dev,
00509 struct netdev_queue *dev_queue,
00510 struct Qdisc_ops *ops,
00511 unsigned int parentid)
00512 {
00513 struct Qdisc *sch;
00514
00515 sch = qdisc_alloc(dev_queue, ops);
00516 if (IS_ERR(sch))
00517 goto errout;
00518 sch->parent = parentid;
00519
00520 if (!ops->init || ops->init(sch, NULL) == 0)
00521 return sch;
00522
00523 qdisc_destroy(sch);
00524 errout:
00525 return NULL;
00526 }
00527 EXPORT_SYMBOL(qdisc_create_dflt);
00528
00529
00530
00531 void qdisc_reset(struct Qdisc *qdisc)
00532 {
00533 const struct Qdisc_ops *ops = qdisc->ops;
00534
00535 if (ops->reset)
00536 ops->reset(qdisc);
00537
00538 kfree_skb(qdisc->gso_skb);
00539 qdisc->gso_skb = NULL;
00540 }
00541 EXPORT_SYMBOL(qdisc_reset);
00542
00543 void qdisc_destroy(struct Qdisc *qdisc)
00544 {
00545 const struct Qdisc_ops *ops = qdisc->ops;
00546
00547 if (qdisc->flags & TCQ_F_BUILTIN ||
00548 !atomic_dec_and_test(&qdisc->refcnt))
00549 return;
00550
00551 #ifdef CONFIG_NET_SCHED
00552 #ifndef DDE_LINUX
00553 qdisc_list_del(qdisc);
00554
00555 qdisc_put_stab(qdisc->stab);
00556 gen_kill_estimator(&qdisc->bstats, &qdisc->rate_est);
00557 #endif
00558 #endif
00559 if (ops->reset)
00560 ops->reset(qdisc);
00561 if (ops->destroy)
00562 ops->destroy(qdisc);
00563
00564 module_put(ops->owner);
00565 dev_put(qdisc_dev(qdisc));
00566
00567 kfree_skb(qdisc->gso_skb);
00568 kfree((char *) qdisc - qdisc->padded);
00569 }
00570 EXPORT_SYMBOL(qdisc_destroy);
00571
00572 static bool dev_all_qdisc_sleeping_noop(struct net_device *dev)
00573 {
00574 unsigned int i;
00575
00576 for (i = 0; i < dev->num_tx_queues; i++) {
00577 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
00578
00579 if (txq->qdisc_sleeping != &noop_qdisc)
00580 return false;
00581 }
00582 return true;
00583 }
00584
00585 static void attach_one_default_qdisc(struct net_device *dev,
00586 struct netdev_queue *dev_queue,
00587 void *_unused)
00588 {
00589 struct Qdisc *qdisc;
00590
00591 if (dev->tx_queue_len) {
00592 qdisc = qdisc_create_dflt(dev, dev_queue,
00593 &pfifo_fast_ops, TC_H_ROOT);
00594 if (!qdisc) {
00595 printk(KERN_INFO "%s: activation failed\n", dev->name);
00596 return;
00597 }
00598 } else {
00599 qdisc = &noqueue_qdisc;
00600 }
00601 dev_queue->qdisc_sleeping = qdisc;
00602 }
00603
00604 static void transition_one_qdisc(struct net_device *dev,
00605 struct netdev_queue *dev_queue,
00606 void *_need_watchdog)
00607 {
00608 struct Qdisc *new_qdisc = dev_queue->qdisc_sleeping;
00609 int *need_watchdog_p = _need_watchdog;
00610
00611 if (!(new_qdisc->flags & TCQ_F_BUILTIN))
00612 clear_bit(__QDISC_STATE_DEACTIVATED, &new_qdisc->state);
00613
00614 rcu_assign_pointer(dev_queue->qdisc, new_qdisc);
00615 if (need_watchdog_p && new_qdisc != &noqueue_qdisc)
00616 *need_watchdog_p = 1;
00617 }
00618
00619 void dev_activate(struct net_device *dev)
00620 {
00621 int need_watchdog;
00622
00623
00624
00625
00626
00627
00628
00629 if (dev_all_qdisc_sleeping_noop(dev))
00630 netdev_for_each_tx_queue(dev, attach_one_default_qdisc, NULL);
00631
00632 if (!netif_carrier_ok(dev))
00633
00634 return;
00635
00636 need_watchdog = 0;
00637 netdev_for_each_tx_queue(dev, transition_one_qdisc, &need_watchdog);
00638 transition_one_qdisc(dev, &dev->rx_queue, NULL);
00639
00640 if (need_watchdog) {
00641 dev->trans_start = jiffies;
00642 dev_watchdog_up(dev);
00643 }
00644 }
00645
00646 static void dev_deactivate_queue(struct net_device *dev,
00647 struct netdev_queue *dev_queue,
00648 void *_qdisc_default)
00649 {
00650 struct Qdisc *qdisc_default = _qdisc_default;
00651 struct Qdisc *qdisc;
00652
00653 qdisc = dev_queue->qdisc;
00654 if (qdisc) {
00655 spin_lock_bh(qdisc_lock(qdisc));
00656
00657 if (!(qdisc->flags & TCQ_F_BUILTIN))
00658 set_bit(__QDISC_STATE_DEACTIVATED, &qdisc->state);
00659
00660 rcu_assign_pointer(dev_queue->qdisc, qdisc_default);
00661 qdisc_reset(qdisc);
00662
00663 spin_unlock_bh(qdisc_lock(qdisc));
00664 }
00665 }
00666
00667 static bool some_qdisc_is_busy(struct net_device *dev)
00668 {
00669 unsigned int i;
00670
00671 for (i = 0; i < dev->num_tx_queues; i++) {
00672 struct netdev_queue *dev_queue;
00673 spinlock_t *root_lock;
00674 struct Qdisc *q;
00675 int val;
00676
00677 dev_queue = netdev_get_tx_queue(dev, i);
00678 q = dev_queue->qdisc_sleeping;
00679 root_lock = qdisc_lock(q);
00680
00681 spin_lock_bh(root_lock);
00682
00683 val = (test_bit(__QDISC_STATE_RUNNING, &q->state) ||
00684 test_bit(__QDISC_STATE_SCHED, &q->state));
00685
00686 spin_unlock_bh(root_lock);
00687
00688 if (val)
00689 return true;
00690 }
00691 return false;
00692 }
00693
00694 void dev_deactivate(struct net_device *dev)
00695 {
00696 netdev_for_each_tx_queue(dev, dev_deactivate_queue, &noop_qdisc);
00697 dev_deactivate_queue(dev, &dev->rx_queue, &noop_qdisc);
00698
00699 dev_watchdog_down(dev);
00700
00701 #ifndef DDE_LINUX
00702
00703 synchronize_rcu();
00704 #endif
00705
00706
00707 while (some_qdisc_is_busy(dev))
00708 yield();
00709 }
00710
00711 static void dev_init_scheduler_queue(struct net_device *dev,
00712 struct netdev_queue *dev_queue,
00713 void *_qdisc)
00714 {
00715 struct Qdisc *qdisc = _qdisc;
00716
00717 dev_queue->qdisc = qdisc;
00718 dev_queue->qdisc_sleeping = qdisc;
00719 }
00720
00721 void dev_init_scheduler(struct net_device *dev)
00722 {
00723 netdev_for_each_tx_queue(dev, dev_init_scheduler_queue, &noop_qdisc);
00724 dev_init_scheduler_queue(dev, &dev->rx_queue, &noop_qdisc);
00725
00726 setup_timer(&dev->watchdog_timer, dev_watchdog, (unsigned long)dev);
00727 }
00728
00729 static void shutdown_scheduler_queue(struct net_device *dev,
00730 struct netdev_queue *dev_queue,
00731 void *_qdisc_default)
00732 {
00733 struct Qdisc *qdisc = dev_queue->qdisc_sleeping;
00734 struct Qdisc *qdisc_default = _qdisc_default;
00735
00736 if (qdisc) {
00737 rcu_assign_pointer(dev_queue->qdisc, qdisc_default);
00738 dev_queue->qdisc_sleeping = qdisc_default;
00739
00740 qdisc_destroy(qdisc);
00741 }
00742 }
00743
00744 void dev_shutdown(struct net_device *dev)
00745 {
00746 netdev_for_each_tx_queue(dev, shutdown_scheduler_queue, &noop_qdisc);
00747 shutdown_scheduler_queue(dev, &dev->rx_queue, &noop_qdisc);
00748 WARN_ON(timer_pending(&dev->watchdog_timer));
00749 }