sch_generic.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  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 <asm/uaccess.h>
  14. #include <asm/system.h>
  15. #include <linux/bitops.h>
  16. #include <linux/module.h>
  17. #include <linux/types.h>
  18. #include <linux/kernel.h>
  19. #include <linux/sched.h>
  20. #include <linux/string.h>
  21. #include <linux/mm.h>
  22. #include <linux/socket.h>
  23. #include <linux/sockios.h>
  24. #include <linux/in.h>
  25. #include <linux/errno.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/netdevice.h>
  28. #include <linux/skbuff.h>
  29. #include <linux/rtnetlink.h>
  30. #include <linux/init.h>
  31. #include <linux/rcupdate.h>
  32. #include <linux/list.h>
  33. #include <net/sock.h>
  34. #include <net/pkt_sched.h>
  35. /* Main transmission queue. */
  36. /* Main qdisc structure lock.
  37. However, modifications
  38. to data, participating in scheduling must be additionally
  39. protected with dev->queue_lock spinlock.
  40. The idea is the following:
  41. - enqueue, dequeue are serialized via top level device
  42. spinlock dev->queue_lock.
  43. - tree walking is protected by read_lock_bh(qdisc_tree_lock)
  44. and this lock is used only in process context.
  45. - updates to tree are made under rtnl semaphore or
  46. from softirq context (__qdisc_destroy rcu-callback)
  47. hence this lock needs local bh disabling.
  48. qdisc_tree_lock must be grabbed BEFORE dev->queue_lock!
  49. */
  50. DEFINE_RWLOCK(qdisc_tree_lock);
  51. void qdisc_lock_tree(struct net_device *dev)
  52. {
  53. write_lock_bh(&qdisc_tree_lock);
  54. spin_lock_bh(&dev->queue_lock);
  55. }
  56. void qdisc_unlock_tree(struct net_device *dev)
  57. {
  58. spin_unlock_bh(&dev->queue_lock);
  59. write_unlock_bh(&qdisc_tree_lock);
  60. }
  61. /*
  62. dev->queue_lock serializes queue accesses for this device
  63. AND dev->qdisc pointer itself.
  64. netif_tx_lock serializes accesses to device driver.
  65. dev->queue_lock and netif_tx_lock are mutually exclusive,
  66. if one is grabbed, another must be free.
  67. */
  68. /* Kick device.
  69. Note, that this procedure can be called by a watchdog timer, so that
  70. we do not check dev->tbusy flag here.
  71. Returns: 0 - queue is empty.
  72. >0 - queue is not empty, but throttled.
  73. <0 - queue is not empty. Device is throttled, if dev->tbusy != 0.
  74. NOTE: Called under dev->queue_lock with locally disabled BH.
  75. */
  76. static inline int qdisc_restart(struct net_device *dev)
  77. {
  78. struct Qdisc *q = dev->qdisc;
  79. struct sk_buff *skb;
  80. /* Dequeue packet */
  81. if (((skb = dev->gso_skb)) || ((skb = q->dequeue(q)))) {
  82. unsigned nolock = (dev->features & NETIF_F_LLTX);
  83. dev->gso_skb = NULL;
  84. /*
  85. * When the driver has LLTX set it does its own locking
  86. * in start_xmit. No need to add additional overhead by
  87. * locking again. These checks are worth it because
  88. * even uncongested locks can be quite expensive.
  89. * The driver can do trylock like here too, in case
  90. * of lock congestion it should return -1 and the packet
  91. * will be requeued.
  92. */
  93. if (!nolock) {
  94. if (!netif_tx_trylock(dev)) {
  95. collision:
  96. /* So, someone grabbed the driver. */
  97. /* It may be transient configuration error,
  98. when hard_start_xmit() recurses. We detect
  99. it by checking xmit owner and drop the
  100. packet when deadloop is detected.
  101. */
  102. if (dev->xmit_lock_owner == smp_processor_id()) {
  103. kfree_skb(skb);
  104. if (net_ratelimit())
  105. printk(KERN_DEBUG "Dead loop on netdevice %s, fix it urgently!\n", dev->name);
  106. return -1;
  107. }
  108. __get_cpu_var(netdev_rx_stat).cpu_collision++;
  109. goto requeue;
  110. }
  111. }
  112. {
  113. /* And release queue */
  114. spin_unlock(&dev->queue_lock);
  115. if (!netif_queue_stopped(dev)) {
  116. int ret;
  117. ret = dev_hard_start_xmit(skb, dev);
  118. if (ret == NETDEV_TX_OK) {
  119. if (!nolock) {
  120. netif_tx_unlock(dev);
  121. }
  122. spin_lock(&dev->queue_lock);
  123. return -1;
  124. }
  125. if (ret == NETDEV_TX_LOCKED && nolock) {
  126. spin_lock(&dev->queue_lock);
  127. goto collision;
  128. }
  129. }
  130. /* NETDEV_TX_BUSY - we need to requeue */
  131. /* Release the driver */
  132. if (!nolock) {
  133. netif_tx_unlock(dev);
  134. }
  135. spin_lock(&dev->queue_lock);
  136. q = dev->qdisc;
  137. }
  138. /* Device kicked us out :(
  139. This is possible in three cases:
  140. 0. driver is locked
  141. 1. fastroute is enabled
  142. 2. device cannot determine busy state
  143. before start of transmission (f.e. dialout)
  144. 3. device is buggy (ppp)
  145. */
  146. requeue:
  147. if (skb->next)
  148. dev->gso_skb = skb;
  149. else
  150. q->ops->requeue(skb, q);
  151. netif_schedule(dev);
  152. return 1;
  153. }
  154. BUG_ON((int) q->q.qlen < 0);
  155. return q->q.qlen;
  156. }
  157. void __qdisc_run(struct net_device *dev)
  158. {
  159. if (unlikely(dev->qdisc == &noop_qdisc))
  160. goto out;
  161. while (qdisc_restart(dev) < 0 && !netif_queue_stopped(dev))
  162. /* NOTHING */;
  163. out:
  164. clear_bit(__LINK_STATE_QDISC_RUNNING, &dev->state);
  165. }
  166. static void dev_watchdog(unsigned long arg)
  167. {
  168. struct net_device *dev = (struct net_device *)arg;
  169. netif_tx_lock(dev);
  170. if (dev->qdisc != &noop_qdisc) {
  171. if (netif_device_present(dev) &&
  172. netif_running(dev) &&
  173. netif_carrier_ok(dev)) {
  174. if (netif_queue_stopped(dev) &&
  175. time_after(jiffies, dev->trans_start + dev->watchdog_timeo)) {
  176. printk(KERN_INFO "NETDEV WATCHDOG: %s: transmit timed out\n",
  177. dev->name);
  178. dev->tx_timeout(dev);
  179. }
  180. if (!mod_timer(&dev->watchdog_timer, jiffies + dev->watchdog_timeo))
  181. dev_hold(dev);
  182. }
  183. }
  184. netif_tx_unlock(dev);
  185. dev_put(dev);
  186. }
  187. static void dev_watchdog_init(struct net_device *dev)
  188. {
  189. init_timer(&dev->watchdog_timer);
  190. dev->watchdog_timer.data = (unsigned long)dev;
  191. dev->watchdog_timer.function = dev_watchdog;
  192. }
  193. void __netdev_watchdog_up(struct net_device *dev)
  194. {
  195. if (dev->tx_timeout) {
  196. if (dev->watchdog_timeo <= 0)
  197. dev->watchdog_timeo = 5*HZ;
  198. if (!mod_timer(&dev->watchdog_timer, jiffies + dev->watchdog_timeo))
  199. dev_hold(dev);
  200. }
  201. }
  202. static void dev_watchdog_up(struct net_device *dev)
  203. {
  204. __netdev_watchdog_up(dev);
  205. }
  206. static void dev_watchdog_down(struct net_device *dev)
  207. {
  208. netif_tx_lock_bh(dev);
  209. if (del_timer(&dev->watchdog_timer))
  210. dev_put(dev);
  211. netif_tx_unlock_bh(dev);
  212. }
  213. void netif_carrier_on(struct net_device *dev)
  214. {
  215. if (test_and_clear_bit(__LINK_STATE_NOCARRIER, &dev->state))
  216. linkwatch_fire_event(dev);
  217. if (netif_running(dev))
  218. __netdev_watchdog_up(dev);
  219. }
  220. void netif_carrier_off(struct net_device *dev)
  221. {
  222. if (!test_and_set_bit(__LINK_STATE_NOCARRIER, &dev->state))
  223. linkwatch_fire_event(dev);
  224. }
  225. /* "NOOP" scheduler: the best scheduler, recommended for all interfaces
  226. under all circumstances. It is difficult to invent anything faster or
  227. cheaper.
  228. */
  229. static int noop_enqueue(struct sk_buff *skb, struct Qdisc * qdisc)
  230. {
  231. kfree_skb(skb);
  232. return NET_XMIT_CN;
  233. }
  234. static struct sk_buff *noop_dequeue(struct Qdisc * qdisc)
  235. {
  236. return NULL;
  237. }
  238. static int noop_requeue(struct sk_buff *skb, struct Qdisc* qdisc)
  239. {
  240. if (net_ratelimit())
  241. printk(KERN_DEBUG "%s deferred output. It is buggy.\n",
  242. skb->dev->name);
  243. kfree_skb(skb);
  244. return NET_XMIT_CN;
  245. }
  246. struct Qdisc_ops noop_qdisc_ops = {
  247. .id = "noop",
  248. .priv_size = 0,
  249. .enqueue = noop_enqueue,
  250. .dequeue = noop_dequeue,
  251. .requeue = noop_requeue,
  252. .owner = THIS_MODULE,
  253. };
  254. struct Qdisc noop_qdisc = {
  255. .enqueue = noop_enqueue,
  256. .dequeue = noop_dequeue,
  257. .flags = TCQ_F_BUILTIN,
  258. .ops = &noop_qdisc_ops,
  259. .list = LIST_HEAD_INIT(noop_qdisc.list),
  260. };
  261. static struct Qdisc_ops noqueue_qdisc_ops = {
  262. .id = "noqueue",
  263. .priv_size = 0,
  264. .enqueue = noop_enqueue,
  265. .dequeue = noop_dequeue,
  266. .requeue = noop_requeue,
  267. .owner = THIS_MODULE,
  268. };
  269. static struct Qdisc noqueue_qdisc = {
  270. .enqueue = NULL,
  271. .dequeue = noop_dequeue,
  272. .flags = TCQ_F_BUILTIN,
  273. .ops = &noqueue_qdisc_ops,
  274. .list = LIST_HEAD_INIT(noqueue_qdisc.list),
  275. };
  276. static const u8 prio2band[TC_PRIO_MAX+1] =
  277. { 1, 2, 2, 2, 1, 2, 0, 0 , 1, 1, 1, 1, 1, 1, 1, 1 };
  278. /* 3-band FIFO queue: old style, but should be a bit faster than
  279. generic prio+fifo combination.
  280. */
  281. #define PFIFO_FAST_BANDS 3
  282. static inline struct sk_buff_head *prio2list(struct sk_buff *skb,
  283. struct Qdisc *qdisc)
  284. {
  285. struct sk_buff_head *list = qdisc_priv(qdisc);
  286. return list + prio2band[skb->priority & TC_PRIO_MAX];
  287. }
  288. static int pfifo_fast_enqueue(struct sk_buff *skb, struct Qdisc* qdisc)
  289. {
  290. struct sk_buff_head *list = prio2list(skb, qdisc);
  291. if (skb_queue_len(list) < qdisc->dev->tx_queue_len) {
  292. qdisc->q.qlen++;
  293. return __qdisc_enqueue_tail(skb, qdisc, list);
  294. }
  295. return qdisc_drop(skb, qdisc);
  296. }
  297. static struct sk_buff *pfifo_fast_dequeue(struct Qdisc* qdisc)
  298. {
  299. int prio;
  300. struct sk_buff_head *list = qdisc_priv(qdisc);
  301. for (prio = 0; prio < PFIFO_FAST_BANDS; prio++) {
  302. if (!skb_queue_empty(list + prio)) {
  303. qdisc->q.qlen--;
  304. return __qdisc_dequeue_head(qdisc, list + prio);
  305. }
  306. }
  307. return NULL;
  308. }
  309. static int pfifo_fast_requeue(struct sk_buff *skb, struct Qdisc* qdisc)
  310. {
  311. qdisc->q.qlen++;
  312. return __qdisc_requeue(skb, qdisc, prio2list(skb, qdisc));
  313. }
  314. static void pfifo_fast_reset(struct Qdisc* qdisc)
  315. {
  316. int prio;
  317. struct sk_buff_head *list = qdisc_priv(qdisc);
  318. for (prio = 0; prio < PFIFO_FAST_BANDS; prio++)
  319. __qdisc_reset_queue(qdisc, list + prio);
  320. qdisc->qstats.backlog = 0;
  321. qdisc->q.qlen = 0;
  322. }
  323. static int pfifo_fast_dump(struct Qdisc *qdisc, struct sk_buff *skb)
  324. {
  325. struct tc_prio_qopt opt = { .bands = PFIFO_FAST_BANDS };
  326. memcpy(&opt.priomap, prio2band, TC_PRIO_MAX+1);
  327. RTA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
  328. return skb->len;
  329. rtattr_failure:
  330. return -1;
  331. }
  332. static int pfifo_fast_init(struct Qdisc *qdisc, struct rtattr *opt)
  333. {
  334. int prio;
  335. struct sk_buff_head *list = qdisc_priv(qdisc);
  336. for (prio = 0; prio < PFIFO_FAST_BANDS; prio++)
  337. skb_queue_head_init(list + prio);
  338. return 0;
  339. }
  340. static struct Qdisc_ops pfifo_fast_ops = {
  341. .id = "pfifo_fast",
  342. .priv_size = PFIFO_FAST_BANDS * sizeof(struct sk_buff_head),
  343. .enqueue = pfifo_fast_enqueue,
  344. .dequeue = pfifo_fast_dequeue,
  345. .requeue = pfifo_fast_requeue,
  346. .init = pfifo_fast_init,
  347. .reset = pfifo_fast_reset,
  348. .dump = pfifo_fast_dump,
  349. .owner = THIS_MODULE,
  350. };
  351. struct Qdisc *qdisc_alloc(struct net_device *dev, struct Qdisc_ops *ops)
  352. {
  353. void *p;
  354. struct Qdisc *sch;
  355. unsigned int size;
  356. int err = -ENOBUFS;
  357. /* ensure that the Qdisc and the private data are 32-byte aligned */
  358. size = QDISC_ALIGN(sizeof(*sch));
  359. size += ops->priv_size + (QDISC_ALIGNTO - 1);
  360. p = kzalloc(size, GFP_KERNEL);
  361. if (!p)
  362. goto errout;
  363. sch = (struct Qdisc *) QDISC_ALIGN((unsigned long) p);
  364. sch->padded = (char *) sch - (char *) p;
  365. INIT_LIST_HEAD(&sch->list);
  366. skb_queue_head_init(&sch->q);
  367. sch->ops = ops;
  368. sch->enqueue = ops->enqueue;
  369. sch->dequeue = ops->dequeue;
  370. sch->dev = dev;
  371. dev_hold(dev);
  372. sch->stats_lock = &dev->queue_lock;
  373. atomic_set(&sch->refcnt, 1);
  374. return sch;
  375. errout:
  376. return ERR_PTR(-err);
  377. }
  378. struct Qdisc * qdisc_create_dflt(struct net_device *dev, struct Qdisc_ops *ops)
  379. {
  380. struct Qdisc *sch;
  381. sch = qdisc_alloc(dev, ops);
  382. if (IS_ERR(sch))
  383. goto errout;
  384. if (!ops->init || ops->init(sch, NULL) == 0)
  385. return sch;
  386. qdisc_destroy(sch);
  387. errout:
  388. return NULL;
  389. }
  390. /* Under dev->queue_lock and BH! */
  391. void qdisc_reset(struct Qdisc *qdisc)
  392. {
  393. struct Qdisc_ops *ops = qdisc->ops;
  394. if (ops->reset)
  395. ops->reset(qdisc);
  396. }
  397. /* this is the rcu callback function to clean up a qdisc when there
  398. * are no further references to it */
  399. static void __qdisc_destroy(struct rcu_head *head)
  400. {
  401. struct Qdisc *qdisc = container_of(head, struct Qdisc, q_rcu);
  402. struct Qdisc_ops *ops = qdisc->ops;
  403. #ifdef CONFIG_NET_ESTIMATOR
  404. gen_kill_estimator(&qdisc->bstats, &qdisc->rate_est);
  405. #endif
  406. write_lock(&qdisc_tree_lock);
  407. if (ops->reset)
  408. ops->reset(qdisc);
  409. if (ops->destroy)
  410. ops->destroy(qdisc);
  411. write_unlock(&qdisc_tree_lock);
  412. module_put(ops->owner);
  413. dev_put(qdisc->dev);
  414. kfree((char *) qdisc - qdisc->padded);
  415. }
  416. /* Under dev->queue_lock and BH! */
  417. void qdisc_destroy(struct Qdisc *qdisc)
  418. {
  419. struct list_head cql = LIST_HEAD_INIT(cql);
  420. struct Qdisc *cq, *q, *n;
  421. if (qdisc->flags & TCQ_F_BUILTIN ||
  422. !atomic_dec_and_test(&qdisc->refcnt))
  423. return;
  424. if (!list_empty(&qdisc->list)) {
  425. if (qdisc->ops->cl_ops == NULL)
  426. list_del(&qdisc->list);
  427. else
  428. list_move(&qdisc->list, &cql);
  429. }
  430. /* unlink inner qdiscs from dev->qdisc_list immediately */
  431. list_for_each_entry(cq, &cql, list)
  432. list_for_each_entry_safe(q, n, &qdisc->dev->qdisc_list, list)
  433. if (TC_H_MAJ(q->parent) == TC_H_MAJ(cq->handle)) {
  434. if (q->ops->cl_ops == NULL)
  435. list_del_init(&q->list);
  436. else
  437. list_move_tail(&q->list, &cql);
  438. }
  439. list_for_each_entry_safe(cq, n, &cql, list)
  440. list_del_init(&cq->list);
  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. if (qdisc == NULL) {
  455. printk(KERN_INFO "%s: activation failed\n", dev->name);
  456. return;
  457. }
  458. write_lock_bh(&qdisc_tree_lock);
  459. list_add_tail(&qdisc->list, &dev->qdisc_list);
  460. write_unlock_bh(&qdisc_tree_lock);
  461. } else {
  462. qdisc = &noqueue_qdisc;
  463. }
  464. write_lock_bh(&qdisc_tree_lock);
  465. dev->qdisc_sleeping = qdisc;
  466. write_unlock_bh(&qdisc_tree_lock);
  467. }
  468. if (!netif_carrier_ok(dev))
  469. /* Delay activation until next carrier-on event */
  470. return;
  471. spin_lock_bh(&dev->queue_lock);
  472. rcu_assign_pointer(dev->qdisc, dev->qdisc_sleeping);
  473. if (dev->qdisc != &noqueue_qdisc) {
  474. dev->trans_start = jiffies;
  475. dev_watchdog_up(dev);
  476. }
  477. spin_unlock_bh(&dev->queue_lock);
  478. }
  479. void dev_deactivate(struct net_device *dev)
  480. {
  481. struct Qdisc *qdisc;
  482. spin_lock_bh(&dev->queue_lock);
  483. qdisc = dev->qdisc;
  484. dev->qdisc = &noop_qdisc;
  485. qdisc_reset(qdisc);
  486. spin_unlock_bh(&dev->queue_lock);
  487. dev_watchdog_down(dev);
  488. /* Wait for outstanding dev_queue_xmit calls. */
  489. synchronize_rcu();
  490. /* Wait for outstanding qdisc_run calls. */
  491. while (test_bit(__LINK_STATE_QDISC_RUNNING, &dev->state))
  492. yield();
  493. if (dev->gso_skb) {
  494. kfree_skb(dev->gso_skb);
  495. dev->gso_skb = NULL;
  496. }
  497. }
  498. void dev_init_scheduler(struct net_device *dev)
  499. {
  500. qdisc_lock_tree(dev);
  501. dev->qdisc = &noop_qdisc;
  502. dev->qdisc_sleeping = &noop_qdisc;
  503. INIT_LIST_HEAD(&dev->qdisc_list);
  504. qdisc_unlock_tree(dev);
  505. dev_watchdog_init(dev);
  506. }
  507. void dev_shutdown(struct net_device *dev)
  508. {
  509. struct Qdisc *qdisc;
  510. qdisc_lock_tree(dev);
  511. qdisc = dev->qdisc_sleeping;
  512. dev->qdisc = &noop_qdisc;
  513. dev->qdisc_sleeping = &noop_qdisc;
  514. qdisc_destroy(qdisc);
  515. #if defined(CONFIG_NET_SCH_INGRESS) || defined(CONFIG_NET_SCH_INGRESS_MODULE)
  516. if ((qdisc = dev->qdisc_ingress) != NULL) {
  517. dev->qdisc_ingress = NULL;
  518. qdisc_destroy(qdisc);
  519. }
  520. #endif
  521. BUG_TRAP(!timer_pending(&dev->watchdog_timer));
  522. qdisc_unlock_tree(dev);
  523. }
  524. EXPORT_SYMBOL(__netdev_watchdog_up);
  525. EXPORT_SYMBOL(netif_carrier_on);
  526. EXPORT_SYMBOL(netif_carrier_off);
  527. EXPORT_SYMBOL(noop_qdisc);
  528. EXPORT_SYMBOL(noop_qdisc_ops);
  529. EXPORT_SYMBOL(qdisc_create_dflt);
  530. EXPORT_SYMBOL(qdisc_alloc);
  531. EXPORT_SYMBOL(qdisc_destroy);
  532. EXPORT_SYMBOL(qdisc_reset);
  533. EXPORT_SYMBOL(qdisc_lock_tree);
  534. EXPORT_SYMBOL(qdisc_unlock_tree);