sch_generic.c 15 KB

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