sch_generic.c 15 KB

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