sch_generic.c 15 KB

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