sch_generic.c 15 KB

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