sch_generic.c 14 KB

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