sch_generic.c 15 KB

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