sch_generic.c 15 KB

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