sch_generic.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  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. int ret = NETDEV_TX_BUSY;
  123. /* Dequeue packet */
  124. if (unlikely((skb = dev_dequeue_skb(dev, q)) == NULL))
  125. return 0;
  126. /* And release queue */
  127. spin_unlock(&dev->queue_lock);
  128. HARD_TX_LOCK(dev, smp_processor_id());
  129. if (!netif_subqueue_stopped(dev, skb))
  130. ret = dev_hard_start_xmit(skb, dev);
  131. HARD_TX_UNLOCK(dev);
  132. spin_lock(&dev->queue_lock);
  133. q = dev->qdisc;
  134. switch (ret) {
  135. case NETDEV_TX_OK:
  136. /* Driver sent out skb successfully */
  137. ret = qdisc_qlen(q);
  138. break;
  139. case NETDEV_TX_LOCKED:
  140. /* Driver try lock failed */
  141. ret = handle_dev_cpu_collision(skb, dev, q);
  142. break;
  143. default:
  144. /* Driver returned NETDEV_TX_BUSY - requeue skb */
  145. if (unlikely (ret != NETDEV_TX_BUSY && net_ratelimit()))
  146. printk(KERN_WARNING "BUG %s code %d qlen %d\n",
  147. dev->name, ret, q->q.qlen);
  148. ret = dev_requeue_skb(skb, dev, q);
  149. break;
  150. }
  151. return ret;
  152. }
  153. void __qdisc_run(struct net_device *dev)
  154. {
  155. do {
  156. if (!qdisc_restart(dev))
  157. break;
  158. } while (!netif_queue_stopped(dev));
  159. clear_bit(__LINK_STATE_QDISC_RUNNING, &dev->state);
  160. }
  161. static void dev_watchdog(unsigned long arg)
  162. {
  163. struct net_device *dev = (struct net_device *)arg;
  164. netif_tx_lock(dev);
  165. if (dev->qdisc != &noop_qdisc) {
  166. if (netif_device_present(dev) &&
  167. netif_running(dev) &&
  168. netif_carrier_ok(dev)) {
  169. if (netif_queue_stopped(dev) &&
  170. time_after(jiffies, dev->trans_start + dev->watchdog_timeo)) {
  171. printk(KERN_INFO "NETDEV WATCHDOG: %s: transmit timed out\n",
  172. dev->name);
  173. dev->tx_timeout(dev);
  174. }
  175. if (!mod_timer(&dev->watchdog_timer, round_jiffies(jiffies + dev->watchdog_timeo)))
  176. dev_hold(dev);
  177. }
  178. }
  179. netif_tx_unlock(dev);
  180. dev_put(dev);
  181. }
  182. static void dev_watchdog_init(struct net_device *dev)
  183. {
  184. init_timer(&dev->watchdog_timer);
  185. dev->watchdog_timer.data = (unsigned long)dev;
  186. dev->watchdog_timer.function = dev_watchdog;
  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. /**
  224. * netif_carrier_off - clear carrier
  225. * @dev: network device
  226. *
  227. * Device has detected loss of carrier.
  228. */
  229. void netif_carrier_off(struct net_device *dev)
  230. {
  231. if (!test_and_set_bit(__LINK_STATE_NOCARRIER, &dev->state))
  232. linkwatch_fire_event(dev);
  233. }
  234. /* "NOOP" scheduler: the best scheduler, recommended for all interfaces
  235. under all circumstances. It is difficult to invent anything faster or
  236. cheaper.
  237. */
  238. static int noop_enqueue(struct sk_buff *skb, struct Qdisc * qdisc)
  239. {
  240. kfree_skb(skb);
  241. return NET_XMIT_CN;
  242. }
  243. static struct sk_buff *noop_dequeue(struct Qdisc * qdisc)
  244. {
  245. return NULL;
  246. }
  247. static int noop_requeue(struct sk_buff *skb, struct Qdisc* qdisc)
  248. {
  249. if (net_ratelimit())
  250. printk(KERN_DEBUG "%s deferred output. It is buggy.\n",
  251. skb->dev->name);
  252. kfree_skb(skb);
  253. return NET_XMIT_CN;
  254. }
  255. struct Qdisc_ops noop_qdisc_ops = {
  256. .id = "noop",
  257. .priv_size = 0,
  258. .enqueue = noop_enqueue,
  259. .dequeue = noop_dequeue,
  260. .requeue = noop_requeue,
  261. .owner = THIS_MODULE,
  262. };
  263. struct Qdisc noop_qdisc = {
  264. .enqueue = noop_enqueue,
  265. .dequeue = noop_dequeue,
  266. .flags = TCQ_F_BUILTIN,
  267. .ops = &noop_qdisc_ops,
  268. .list = LIST_HEAD_INIT(noop_qdisc.list),
  269. };
  270. static struct Qdisc_ops noqueue_qdisc_ops = {
  271. .id = "noqueue",
  272. .priv_size = 0,
  273. .enqueue = noop_enqueue,
  274. .dequeue = noop_dequeue,
  275. .requeue = noop_requeue,
  276. .owner = THIS_MODULE,
  277. };
  278. static struct Qdisc noqueue_qdisc = {
  279. .enqueue = NULL,
  280. .dequeue = noop_dequeue,
  281. .flags = TCQ_F_BUILTIN,
  282. .ops = &noqueue_qdisc_ops,
  283. .list = LIST_HEAD_INIT(noqueue_qdisc.list),
  284. };
  285. static const u8 prio2band[TC_PRIO_MAX+1] =
  286. { 1, 2, 2, 2, 1, 2, 0, 0 , 1, 1, 1, 1, 1, 1, 1, 1 };
  287. /* 3-band FIFO queue: old style, but should be a bit faster than
  288. generic prio+fifo combination.
  289. */
  290. #define PFIFO_FAST_BANDS 3
  291. static inline struct sk_buff_head *prio2list(struct sk_buff *skb,
  292. struct Qdisc *qdisc)
  293. {
  294. struct sk_buff_head *list = qdisc_priv(qdisc);
  295. return list + prio2band[skb->priority & TC_PRIO_MAX];
  296. }
  297. static int pfifo_fast_enqueue(struct sk_buff *skb, struct Qdisc* qdisc)
  298. {
  299. struct sk_buff_head *list = prio2list(skb, qdisc);
  300. if (skb_queue_len(list) < qdisc->dev->tx_queue_len) {
  301. qdisc->q.qlen++;
  302. return __qdisc_enqueue_tail(skb, qdisc, list);
  303. }
  304. return qdisc_drop(skb, qdisc);
  305. }
  306. static struct sk_buff *pfifo_fast_dequeue(struct Qdisc* qdisc)
  307. {
  308. int prio;
  309. struct sk_buff_head *list = qdisc_priv(qdisc);
  310. for (prio = 0; prio < PFIFO_FAST_BANDS; prio++) {
  311. if (!skb_queue_empty(list + prio)) {
  312. qdisc->q.qlen--;
  313. return __qdisc_dequeue_head(qdisc, list + prio);
  314. }
  315. }
  316. return NULL;
  317. }
  318. static int pfifo_fast_requeue(struct sk_buff *skb, struct Qdisc* qdisc)
  319. {
  320. qdisc->q.qlen++;
  321. return __qdisc_requeue(skb, qdisc, prio2list(skb, qdisc));
  322. }
  323. static void pfifo_fast_reset(struct Qdisc* qdisc)
  324. {
  325. int prio;
  326. struct sk_buff_head *list = qdisc_priv(qdisc);
  327. for (prio = 0; prio < PFIFO_FAST_BANDS; prio++)
  328. __qdisc_reset_queue(qdisc, list + prio);
  329. qdisc->qstats.backlog = 0;
  330. qdisc->q.qlen = 0;
  331. }
  332. static int pfifo_fast_dump(struct Qdisc *qdisc, struct sk_buff *skb)
  333. {
  334. struct tc_prio_qopt opt = { .bands = PFIFO_FAST_BANDS };
  335. memcpy(&opt.priomap, prio2band, TC_PRIO_MAX+1);
  336. RTA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
  337. return skb->len;
  338. rtattr_failure:
  339. return -1;
  340. }
  341. static int pfifo_fast_init(struct Qdisc *qdisc, struct rtattr *opt)
  342. {
  343. int prio;
  344. struct sk_buff_head *list = qdisc_priv(qdisc);
  345. for (prio = 0; prio < PFIFO_FAST_BANDS; prio++)
  346. skb_queue_head_init(list + prio);
  347. return 0;
  348. }
  349. static struct Qdisc_ops pfifo_fast_ops = {
  350. .id = "pfifo_fast",
  351. .priv_size = PFIFO_FAST_BANDS * sizeof(struct sk_buff_head),
  352. .enqueue = pfifo_fast_enqueue,
  353. .dequeue = pfifo_fast_dequeue,
  354. .requeue = pfifo_fast_requeue,
  355. .init = pfifo_fast_init,
  356. .reset = pfifo_fast_reset,
  357. .dump = pfifo_fast_dump,
  358. .owner = THIS_MODULE,
  359. };
  360. struct Qdisc *qdisc_alloc(struct net_device *dev, struct Qdisc_ops *ops)
  361. {
  362. void *p;
  363. struct Qdisc *sch;
  364. unsigned int size;
  365. int err = -ENOBUFS;
  366. /* ensure that the Qdisc and the private data are 32-byte aligned */
  367. size = QDISC_ALIGN(sizeof(*sch));
  368. size += ops->priv_size + (QDISC_ALIGNTO - 1);
  369. p = kzalloc(size, GFP_KERNEL);
  370. if (!p)
  371. goto errout;
  372. sch = (struct Qdisc *) QDISC_ALIGN((unsigned long) p);
  373. sch->padded = (char *) sch - (char *) p;
  374. INIT_LIST_HEAD(&sch->list);
  375. skb_queue_head_init(&sch->q);
  376. sch->ops = ops;
  377. sch->enqueue = ops->enqueue;
  378. sch->dequeue = ops->dequeue;
  379. sch->dev = dev;
  380. dev_hold(dev);
  381. atomic_set(&sch->refcnt, 1);
  382. return sch;
  383. errout:
  384. return ERR_PTR(-err);
  385. }
  386. struct Qdisc * qdisc_create_dflt(struct net_device *dev, struct Qdisc_ops *ops,
  387. unsigned int parentid)
  388. {
  389. struct Qdisc *sch;
  390. sch = qdisc_alloc(dev, ops);
  391. if (IS_ERR(sch))
  392. goto errout;
  393. sch->stats_lock = &dev->queue_lock;
  394. sch->parent = parentid;
  395. if (!ops->init || ops->init(sch, NULL) == 0)
  396. return sch;
  397. qdisc_destroy(sch);
  398. errout:
  399. return NULL;
  400. }
  401. /* Under dev->queue_lock and BH! */
  402. void qdisc_reset(struct Qdisc *qdisc)
  403. {
  404. struct Qdisc_ops *ops = qdisc->ops;
  405. if (ops->reset)
  406. ops->reset(qdisc);
  407. }
  408. /* this is the rcu callback function to clean up a qdisc when there
  409. * are no further references to it */
  410. static void __qdisc_destroy(struct rcu_head *head)
  411. {
  412. struct Qdisc *qdisc = container_of(head, struct Qdisc, q_rcu);
  413. kfree((char *) qdisc - qdisc->padded);
  414. }
  415. /* Under dev->queue_lock and BH! */
  416. void qdisc_destroy(struct Qdisc *qdisc)
  417. {
  418. struct Qdisc_ops *ops = qdisc->ops;
  419. if (qdisc->flags & TCQ_F_BUILTIN ||
  420. !atomic_dec_and_test(&qdisc->refcnt))
  421. return;
  422. list_del(&qdisc->list);
  423. gen_kill_estimator(&qdisc->bstats, &qdisc->rate_est);
  424. if (ops->reset)
  425. ops->reset(qdisc);
  426. if (ops->destroy)
  427. ops->destroy(qdisc);
  428. module_put(ops->owner);
  429. dev_put(qdisc->dev);
  430. call_rcu(&qdisc->q_rcu, __qdisc_destroy);
  431. }
  432. void dev_activate(struct net_device *dev)
  433. {
  434. /* No queueing discipline is attached to device;
  435. create default one i.e. pfifo_fast for devices,
  436. which need queueing and noqueue_qdisc for
  437. virtual interfaces
  438. */
  439. if (dev->qdisc_sleeping == &noop_qdisc) {
  440. struct Qdisc *qdisc;
  441. if (dev->tx_queue_len) {
  442. qdisc = qdisc_create_dflt(dev, &pfifo_fast_ops,
  443. TC_H_ROOT);
  444. if (qdisc == NULL) {
  445. printk(KERN_INFO "%s: activation failed\n", dev->name);
  446. return;
  447. }
  448. list_add_tail(&qdisc->list, &dev->qdisc_list);
  449. } else {
  450. qdisc = &noqueue_qdisc;
  451. }
  452. dev->qdisc_sleeping = qdisc;
  453. }
  454. if (!netif_carrier_ok(dev))
  455. /* Delay activation until next carrier-on event */
  456. return;
  457. spin_lock_bh(&dev->queue_lock);
  458. rcu_assign_pointer(dev->qdisc, dev->qdisc_sleeping);
  459. if (dev->qdisc != &noqueue_qdisc) {
  460. dev->trans_start = jiffies;
  461. dev_watchdog_up(dev);
  462. }
  463. spin_unlock_bh(&dev->queue_lock);
  464. }
  465. void dev_deactivate(struct net_device *dev)
  466. {
  467. struct Qdisc *qdisc;
  468. struct sk_buff *skb;
  469. int running;
  470. spin_lock_bh(&dev->queue_lock);
  471. qdisc = dev->qdisc;
  472. dev->qdisc = &noop_qdisc;
  473. qdisc_reset(qdisc);
  474. skb = dev->gso_skb;
  475. dev->gso_skb = NULL;
  476. spin_unlock_bh(&dev->queue_lock);
  477. kfree_skb(skb);
  478. dev_watchdog_down(dev);
  479. /* Wait for outstanding qdisc-less dev_queue_xmit calls. */
  480. synchronize_rcu();
  481. /* Wait for outstanding qdisc_run calls. */
  482. do {
  483. while (test_bit(__LINK_STATE_QDISC_RUNNING, &dev->state))
  484. yield();
  485. /*
  486. * Double-check inside queue lock to ensure that all effects
  487. * of the queue run are visible when we return.
  488. */
  489. spin_lock_bh(&dev->queue_lock);
  490. running = test_bit(__LINK_STATE_QDISC_RUNNING, &dev->state);
  491. spin_unlock_bh(&dev->queue_lock);
  492. /*
  493. * The running flag should never be set at this point because
  494. * we've already set dev->qdisc to noop_qdisc *inside* the same
  495. * pair of spin locks. That is, if any qdisc_run starts after
  496. * our initial test it should see the noop_qdisc and then
  497. * clear the RUNNING bit before dropping the queue lock. So
  498. * if it is set here then we've found a bug.
  499. */
  500. } while (WARN_ON_ONCE(running));
  501. }
  502. void dev_init_scheduler(struct net_device *dev)
  503. {
  504. qdisc_lock_tree(dev);
  505. dev->qdisc = &noop_qdisc;
  506. dev->qdisc_sleeping = &noop_qdisc;
  507. INIT_LIST_HEAD(&dev->qdisc_list);
  508. qdisc_unlock_tree(dev);
  509. dev_watchdog_init(dev);
  510. }
  511. void dev_shutdown(struct net_device *dev)
  512. {
  513. struct Qdisc *qdisc;
  514. qdisc_lock_tree(dev);
  515. qdisc = dev->qdisc_sleeping;
  516. dev->qdisc = &noop_qdisc;
  517. dev->qdisc_sleeping = &noop_qdisc;
  518. qdisc_destroy(qdisc);
  519. #if defined(CONFIG_NET_SCH_INGRESS) || defined(CONFIG_NET_SCH_INGRESS_MODULE)
  520. if ((qdisc = dev->qdisc_ingress) != NULL) {
  521. dev->qdisc_ingress = NULL;
  522. qdisc_destroy(qdisc);
  523. }
  524. #endif
  525. BUG_TRAP(!timer_pending(&dev->watchdog_timer));
  526. qdisc_unlock_tree(dev);
  527. }
  528. EXPORT_SYMBOL(netif_carrier_on);
  529. EXPORT_SYMBOL(netif_carrier_off);
  530. EXPORT_SYMBOL(noop_qdisc);
  531. EXPORT_SYMBOL(qdisc_create_dflt);
  532. EXPORT_SYMBOL(qdisc_destroy);
  533. EXPORT_SYMBOL(qdisc_reset);
  534. EXPORT_SYMBOL(qdisc_lock_tree);
  535. EXPORT_SYMBOL(qdisc_unlock_tree);