sch_generic.c 14 KB

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