sch_generic.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  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. /**
  222. * netif_carrier_on - set carrier
  223. * @dev: network device
  224. *
  225. * Device has detected that carrier.
  226. */
  227. void netif_carrier_on(struct net_device *dev)
  228. {
  229. if (test_and_clear_bit(__LINK_STATE_NOCARRIER, &dev->state))
  230. linkwatch_fire_event(dev);
  231. if (netif_running(dev))
  232. __netdev_watchdog_up(dev);
  233. }
  234. /**
  235. * netif_carrier_off - clear carrier
  236. * @dev: network device
  237. *
  238. * Device has detected loss of carrier.
  239. */
  240. void netif_carrier_off(struct net_device *dev)
  241. {
  242. if (!test_and_set_bit(__LINK_STATE_NOCARRIER, &dev->state))
  243. linkwatch_fire_event(dev);
  244. }
  245. /* "NOOP" scheduler: the best scheduler, recommended for all interfaces
  246. under all circumstances. It is difficult to invent anything faster or
  247. cheaper.
  248. */
  249. static int noop_enqueue(struct sk_buff *skb, struct Qdisc * qdisc)
  250. {
  251. kfree_skb(skb);
  252. return NET_XMIT_CN;
  253. }
  254. static struct sk_buff *noop_dequeue(struct Qdisc * qdisc)
  255. {
  256. return NULL;
  257. }
  258. static int noop_requeue(struct sk_buff *skb, struct Qdisc* qdisc)
  259. {
  260. if (net_ratelimit())
  261. printk(KERN_DEBUG "%s deferred output. It is buggy.\n",
  262. skb->dev->name);
  263. kfree_skb(skb);
  264. return NET_XMIT_CN;
  265. }
  266. struct Qdisc_ops noop_qdisc_ops = {
  267. .id = "noop",
  268. .priv_size = 0,
  269. .enqueue = noop_enqueue,
  270. .dequeue = noop_dequeue,
  271. .requeue = noop_requeue,
  272. .owner = THIS_MODULE,
  273. };
  274. struct Qdisc noop_qdisc = {
  275. .enqueue = noop_enqueue,
  276. .dequeue = noop_dequeue,
  277. .flags = TCQ_F_BUILTIN,
  278. .ops = &noop_qdisc_ops,
  279. .list = LIST_HEAD_INIT(noop_qdisc.list),
  280. };
  281. static struct Qdisc_ops noqueue_qdisc_ops = {
  282. .id = "noqueue",
  283. .priv_size = 0,
  284. .enqueue = noop_enqueue,
  285. .dequeue = noop_dequeue,
  286. .requeue = noop_requeue,
  287. .owner = THIS_MODULE,
  288. };
  289. static struct Qdisc noqueue_qdisc = {
  290. .enqueue = NULL,
  291. .dequeue = noop_dequeue,
  292. .flags = TCQ_F_BUILTIN,
  293. .ops = &noqueue_qdisc_ops,
  294. .list = LIST_HEAD_INIT(noqueue_qdisc.list),
  295. };
  296. static const u8 prio2band[TC_PRIO_MAX+1] =
  297. { 1, 2, 2, 2, 1, 2, 0, 0 , 1, 1, 1, 1, 1, 1, 1, 1 };
  298. /* 3-band FIFO queue: old style, but should be a bit faster than
  299. generic prio+fifo combination.
  300. */
  301. #define PFIFO_FAST_BANDS 3
  302. static inline struct sk_buff_head *prio2list(struct sk_buff *skb,
  303. struct Qdisc *qdisc)
  304. {
  305. struct sk_buff_head *list = qdisc_priv(qdisc);
  306. return list + prio2band[skb->priority & TC_PRIO_MAX];
  307. }
  308. static int pfifo_fast_enqueue(struct sk_buff *skb, struct Qdisc* qdisc)
  309. {
  310. struct sk_buff_head *list = prio2list(skb, qdisc);
  311. if (skb_queue_len(list) < qdisc->dev->tx_queue_len) {
  312. qdisc->q.qlen++;
  313. return __qdisc_enqueue_tail(skb, qdisc, list);
  314. }
  315. return qdisc_drop(skb, qdisc);
  316. }
  317. static struct sk_buff *pfifo_fast_dequeue(struct Qdisc* qdisc)
  318. {
  319. int prio;
  320. struct sk_buff_head *list = qdisc_priv(qdisc);
  321. for (prio = 0; prio < PFIFO_FAST_BANDS; prio++) {
  322. if (!skb_queue_empty(list + prio)) {
  323. qdisc->q.qlen--;
  324. return __qdisc_dequeue_head(qdisc, list + prio);
  325. }
  326. }
  327. return NULL;
  328. }
  329. static int pfifo_fast_requeue(struct sk_buff *skb, struct Qdisc* qdisc)
  330. {
  331. qdisc->q.qlen++;
  332. return __qdisc_requeue(skb, qdisc, prio2list(skb, qdisc));
  333. }
  334. static void pfifo_fast_reset(struct Qdisc* qdisc)
  335. {
  336. int prio;
  337. struct sk_buff_head *list = qdisc_priv(qdisc);
  338. for (prio = 0; prio < PFIFO_FAST_BANDS; prio++)
  339. __qdisc_reset_queue(qdisc, list + prio);
  340. qdisc->qstats.backlog = 0;
  341. qdisc->q.qlen = 0;
  342. }
  343. static int pfifo_fast_dump(struct Qdisc *qdisc, struct sk_buff *skb)
  344. {
  345. struct tc_prio_qopt opt = { .bands = PFIFO_FAST_BANDS };
  346. memcpy(&opt.priomap, prio2band, TC_PRIO_MAX+1);
  347. RTA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
  348. return skb->len;
  349. rtattr_failure:
  350. return -1;
  351. }
  352. static int pfifo_fast_init(struct Qdisc *qdisc, struct rtattr *opt)
  353. {
  354. int prio;
  355. struct sk_buff_head *list = qdisc_priv(qdisc);
  356. for (prio = 0; prio < PFIFO_FAST_BANDS; prio++)
  357. skb_queue_head_init(list + prio);
  358. return 0;
  359. }
  360. static struct Qdisc_ops pfifo_fast_ops = {
  361. .id = "pfifo_fast",
  362. .priv_size = PFIFO_FAST_BANDS * sizeof(struct sk_buff_head),
  363. .enqueue = pfifo_fast_enqueue,
  364. .dequeue = pfifo_fast_dequeue,
  365. .requeue = pfifo_fast_requeue,
  366. .init = pfifo_fast_init,
  367. .reset = pfifo_fast_reset,
  368. .dump = pfifo_fast_dump,
  369. .owner = THIS_MODULE,
  370. };
  371. struct Qdisc *qdisc_alloc(struct net_device *dev, struct Qdisc_ops *ops)
  372. {
  373. void *p;
  374. struct Qdisc *sch;
  375. unsigned int size;
  376. int err = -ENOBUFS;
  377. /* ensure that the Qdisc and the private data are 32-byte aligned */
  378. size = QDISC_ALIGN(sizeof(*sch));
  379. size += ops->priv_size + (QDISC_ALIGNTO - 1);
  380. p = kzalloc(size, GFP_KERNEL);
  381. if (!p)
  382. goto errout;
  383. sch = (struct Qdisc *) QDISC_ALIGN((unsigned long) p);
  384. sch->padded = (char *) sch - (char *) p;
  385. INIT_LIST_HEAD(&sch->list);
  386. skb_queue_head_init(&sch->q);
  387. sch->ops = ops;
  388. sch->enqueue = ops->enqueue;
  389. sch->dequeue = ops->dequeue;
  390. sch->dev = dev;
  391. dev_hold(dev);
  392. atomic_set(&sch->refcnt, 1);
  393. return sch;
  394. errout:
  395. return ERR_PTR(-err);
  396. }
  397. struct Qdisc * qdisc_create_dflt(struct net_device *dev, struct Qdisc_ops *ops,
  398. unsigned int parentid)
  399. {
  400. struct Qdisc *sch;
  401. sch = qdisc_alloc(dev, ops);
  402. if (IS_ERR(sch))
  403. goto errout;
  404. sch->stats_lock = &dev->queue_lock;
  405. sch->parent = parentid;
  406. if (!ops->init || ops->init(sch, NULL) == 0)
  407. return sch;
  408. qdisc_destroy(sch);
  409. errout:
  410. return NULL;
  411. }
  412. /* Under dev->queue_lock and BH! */
  413. void qdisc_reset(struct Qdisc *qdisc)
  414. {
  415. struct Qdisc_ops *ops = qdisc->ops;
  416. if (ops->reset)
  417. ops->reset(qdisc);
  418. }
  419. /* this is the rcu callback function to clean up a qdisc when there
  420. * are no further references to it */
  421. static void __qdisc_destroy(struct rcu_head *head)
  422. {
  423. struct Qdisc *qdisc = container_of(head, struct Qdisc, q_rcu);
  424. kfree((char *) qdisc - qdisc->padded);
  425. }
  426. /* Under dev->queue_lock and BH! */
  427. void qdisc_destroy(struct Qdisc *qdisc)
  428. {
  429. struct Qdisc_ops *ops = qdisc->ops;
  430. if (qdisc->flags & TCQ_F_BUILTIN ||
  431. !atomic_dec_and_test(&qdisc->refcnt))
  432. return;
  433. list_del(&qdisc->list);
  434. gen_kill_estimator(&qdisc->bstats, &qdisc->rate_est);
  435. if (ops->reset)
  436. ops->reset(qdisc);
  437. if (ops->destroy)
  438. ops->destroy(qdisc);
  439. module_put(ops->owner);
  440. dev_put(qdisc->dev);
  441. call_rcu(&qdisc->q_rcu, __qdisc_destroy);
  442. }
  443. void dev_activate(struct net_device *dev)
  444. {
  445. /* No queueing discipline is attached to device;
  446. create default one i.e. pfifo_fast for devices,
  447. which need queueing and noqueue_qdisc for
  448. virtual interfaces
  449. */
  450. if (dev->qdisc_sleeping == &noop_qdisc) {
  451. struct Qdisc *qdisc;
  452. if (dev->tx_queue_len) {
  453. qdisc = qdisc_create_dflt(dev, &pfifo_fast_ops,
  454. TC_H_ROOT);
  455. if (qdisc == NULL) {
  456. printk(KERN_INFO "%s: activation failed\n", dev->name);
  457. return;
  458. }
  459. list_add_tail(&qdisc->list, &dev->qdisc_list);
  460. } else {
  461. qdisc = &noqueue_qdisc;
  462. }
  463. dev->qdisc_sleeping = qdisc;
  464. }
  465. if (!netif_carrier_ok(dev))
  466. /* Delay activation until next carrier-on event */
  467. return;
  468. spin_lock_bh(&dev->queue_lock);
  469. rcu_assign_pointer(dev->qdisc, dev->qdisc_sleeping);
  470. if (dev->qdisc != &noqueue_qdisc) {
  471. dev->trans_start = jiffies;
  472. dev_watchdog_up(dev);
  473. }
  474. spin_unlock_bh(&dev->queue_lock);
  475. }
  476. void dev_deactivate(struct net_device *dev)
  477. {
  478. struct Qdisc *qdisc;
  479. struct sk_buff *skb;
  480. spin_lock_bh(&dev->queue_lock);
  481. qdisc = dev->qdisc;
  482. dev->qdisc = &noop_qdisc;
  483. qdisc_reset(qdisc);
  484. skb = dev->gso_skb;
  485. dev->gso_skb = NULL;
  486. spin_unlock_bh(&dev->queue_lock);
  487. kfree_skb(skb);
  488. dev_watchdog_down(dev);
  489. /* Wait for outstanding dev_queue_xmit calls. */
  490. synchronize_rcu();
  491. /* Wait for outstanding qdisc_run calls. */
  492. while (test_bit(__LINK_STATE_QDISC_RUNNING, &dev->state))
  493. yield();
  494. }
  495. void dev_init_scheduler(struct net_device *dev)
  496. {
  497. qdisc_lock_tree(dev);
  498. dev->qdisc = &noop_qdisc;
  499. dev->qdisc_sleeping = &noop_qdisc;
  500. INIT_LIST_HEAD(&dev->qdisc_list);
  501. qdisc_unlock_tree(dev);
  502. dev_watchdog_init(dev);
  503. }
  504. void dev_shutdown(struct net_device *dev)
  505. {
  506. struct Qdisc *qdisc;
  507. qdisc_lock_tree(dev);
  508. qdisc = dev->qdisc_sleeping;
  509. dev->qdisc = &noop_qdisc;
  510. dev->qdisc_sleeping = &noop_qdisc;
  511. qdisc_destroy(qdisc);
  512. #if defined(CONFIG_NET_SCH_INGRESS) || defined(CONFIG_NET_SCH_INGRESS_MODULE)
  513. if ((qdisc = dev->qdisc_ingress) != NULL) {
  514. dev->qdisc_ingress = NULL;
  515. qdisc_destroy(qdisc);
  516. }
  517. #endif
  518. BUG_TRAP(!timer_pending(&dev->watchdog_timer));
  519. qdisc_unlock_tree(dev);
  520. }
  521. EXPORT_SYMBOL(netif_carrier_on);
  522. EXPORT_SYMBOL(netif_carrier_off);
  523. EXPORT_SYMBOL(noop_qdisc);
  524. EXPORT_SYMBOL(qdisc_create_dflt);
  525. EXPORT_SYMBOL(qdisc_destroy);
  526. EXPORT_SYMBOL(qdisc_reset);
  527. EXPORT_SYMBOL(qdisc_lock_tree);
  528. EXPORT_SYMBOL(qdisc_unlock_tree);