sch_generic.c 14 KB

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