sch_generic.c 15 KB

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