sch_generic.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. #ifndef __NET_SCHED_GENERIC_H
  2. #define __NET_SCHED_GENERIC_H
  3. #include <linux/netdevice.h>
  4. #include <linux/types.h>
  5. #include <linux/rcupdate.h>
  6. #include <linux/module.h>
  7. #include <linux/pkt_sched.h>
  8. #include <linux/pkt_cls.h>
  9. #include <net/gen_stats.h>
  10. #include <net/rtnetlink.h>
  11. struct Qdisc_ops;
  12. struct qdisc_walker;
  13. struct tcf_walker;
  14. struct module;
  15. struct qdisc_rate_table {
  16. struct tc_ratespec rate;
  17. u32 data[256];
  18. struct qdisc_rate_table *next;
  19. int refcnt;
  20. };
  21. enum qdisc_state_t {
  22. __QDISC_STATE_SCHED,
  23. __QDISC_STATE_DEACTIVATED,
  24. };
  25. /*
  26. * following bits are only changed while qdisc lock is held
  27. */
  28. enum qdisc___state_t {
  29. __QDISC___STATE_RUNNING,
  30. };
  31. struct qdisc_size_table {
  32. struct list_head list;
  33. struct tc_sizespec szopts;
  34. int refcnt;
  35. u16 data[];
  36. };
  37. struct Qdisc {
  38. int (*enqueue)(struct sk_buff *skb, struct Qdisc *dev);
  39. struct sk_buff * (*dequeue)(struct Qdisc *dev);
  40. unsigned flags;
  41. #define TCQ_F_BUILTIN 1
  42. #define TCQ_F_THROTTLED 2
  43. #define TCQ_F_INGRESS 4
  44. #define TCQ_F_CAN_BYPASS 8
  45. #define TCQ_F_MQROOT 16
  46. #define TCQ_F_WARN_NONWC (1 << 16)
  47. int padded;
  48. struct Qdisc_ops *ops;
  49. struct qdisc_size_table *stab;
  50. struct list_head list;
  51. u32 handle;
  52. u32 parent;
  53. atomic_t refcnt;
  54. struct gnet_stats_rate_est rate_est;
  55. int (*reshape_fail)(struct sk_buff *skb,
  56. struct Qdisc *q);
  57. void *u32_node;
  58. /* This field is deprecated, but it is still used by CBQ
  59. * and it will live until better solution will be invented.
  60. */
  61. struct Qdisc *__parent;
  62. struct netdev_queue *dev_queue;
  63. struct Qdisc *next_sched;
  64. struct sk_buff *gso_skb;
  65. /*
  66. * For performance sake on SMP, we put highly modified fields at the end
  67. */
  68. unsigned long state;
  69. struct sk_buff_head q;
  70. struct gnet_stats_basic_packed bstats;
  71. unsigned long __state;
  72. struct gnet_stats_queue qstats;
  73. struct rcu_head rcu_head;
  74. spinlock_t busylock;
  75. };
  76. static inline bool qdisc_is_running(struct Qdisc *qdisc)
  77. {
  78. return test_bit(__QDISC___STATE_RUNNING, &qdisc->__state);
  79. }
  80. static inline bool qdisc_run_begin(struct Qdisc *qdisc)
  81. {
  82. return !__test_and_set_bit(__QDISC___STATE_RUNNING, &qdisc->__state);
  83. }
  84. static inline void qdisc_run_end(struct Qdisc *qdisc)
  85. {
  86. __clear_bit(__QDISC___STATE_RUNNING, &qdisc->__state);
  87. }
  88. struct Qdisc_class_ops {
  89. /* Child qdisc manipulation */
  90. struct netdev_queue * (*select_queue)(struct Qdisc *, struct tcmsg *);
  91. int (*graft)(struct Qdisc *, unsigned long cl,
  92. struct Qdisc *, struct Qdisc **);
  93. struct Qdisc * (*leaf)(struct Qdisc *, unsigned long cl);
  94. void (*qlen_notify)(struct Qdisc *, unsigned long);
  95. /* Class manipulation routines */
  96. unsigned long (*get)(struct Qdisc *, u32 classid);
  97. void (*put)(struct Qdisc *, unsigned long);
  98. int (*change)(struct Qdisc *, u32, u32,
  99. struct nlattr **, unsigned long *);
  100. int (*delete)(struct Qdisc *, unsigned long);
  101. void (*walk)(struct Qdisc *, struct qdisc_walker * arg);
  102. /* Filter manipulation */
  103. struct tcf_proto ** (*tcf_chain)(struct Qdisc *, unsigned long);
  104. unsigned long (*bind_tcf)(struct Qdisc *, unsigned long,
  105. u32 classid);
  106. void (*unbind_tcf)(struct Qdisc *, unsigned long);
  107. /* rtnetlink specific */
  108. int (*dump)(struct Qdisc *, unsigned long,
  109. struct sk_buff *skb, struct tcmsg*);
  110. int (*dump_stats)(struct Qdisc *, unsigned long,
  111. struct gnet_dump *);
  112. };
  113. struct Qdisc_ops {
  114. struct Qdisc_ops *next;
  115. const struct Qdisc_class_ops *cl_ops;
  116. char id[IFNAMSIZ];
  117. int priv_size;
  118. int (*enqueue)(struct sk_buff *, struct Qdisc *);
  119. struct sk_buff * (*dequeue)(struct Qdisc *);
  120. struct sk_buff * (*peek)(struct Qdisc *);
  121. unsigned int (*drop)(struct Qdisc *);
  122. int (*init)(struct Qdisc *, struct nlattr *arg);
  123. void (*reset)(struct Qdisc *);
  124. void (*destroy)(struct Qdisc *);
  125. int (*change)(struct Qdisc *, struct nlattr *arg);
  126. void (*attach)(struct Qdisc *);
  127. int (*dump)(struct Qdisc *, struct sk_buff *);
  128. int (*dump_stats)(struct Qdisc *, struct gnet_dump *);
  129. struct module *owner;
  130. };
  131. struct tcf_result {
  132. unsigned long class;
  133. u32 classid;
  134. };
  135. struct tcf_proto_ops {
  136. struct tcf_proto_ops *next;
  137. char kind[IFNAMSIZ];
  138. int (*classify)(struct sk_buff*, struct tcf_proto*,
  139. struct tcf_result *);
  140. int (*init)(struct tcf_proto*);
  141. void (*destroy)(struct tcf_proto*);
  142. unsigned long (*get)(struct tcf_proto*, u32 handle);
  143. void (*put)(struct tcf_proto*, unsigned long);
  144. int (*change)(struct tcf_proto*, unsigned long,
  145. u32 handle, struct nlattr **,
  146. unsigned long *);
  147. int (*delete)(struct tcf_proto*, unsigned long);
  148. void (*walk)(struct tcf_proto*, struct tcf_walker *arg);
  149. /* rtnetlink specific */
  150. int (*dump)(struct tcf_proto*, unsigned long,
  151. struct sk_buff *skb, struct tcmsg*);
  152. struct module *owner;
  153. };
  154. struct tcf_proto {
  155. /* Fast access part */
  156. struct tcf_proto *next;
  157. void *root;
  158. int (*classify)(struct sk_buff*, struct tcf_proto*,
  159. struct tcf_result *);
  160. __be16 protocol;
  161. /* All the rest */
  162. u32 prio;
  163. u32 classid;
  164. struct Qdisc *q;
  165. void *data;
  166. struct tcf_proto_ops *ops;
  167. };
  168. struct qdisc_skb_cb {
  169. unsigned int pkt_len;
  170. char data[];
  171. };
  172. static inline int qdisc_qlen(struct Qdisc *q)
  173. {
  174. return q->q.qlen;
  175. }
  176. static inline struct qdisc_skb_cb *qdisc_skb_cb(const struct sk_buff *skb)
  177. {
  178. return (struct qdisc_skb_cb *)skb->cb;
  179. }
  180. static inline spinlock_t *qdisc_lock(struct Qdisc *qdisc)
  181. {
  182. return &qdisc->q.lock;
  183. }
  184. static inline struct Qdisc *qdisc_root(struct Qdisc *qdisc)
  185. {
  186. return qdisc->dev_queue->qdisc;
  187. }
  188. static inline struct Qdisc *qdisc_root_sleeping(struct Qdisc *qdisc)
  189. {
  190. return qdisc->dev_queue->qdisc_sleeping;
  191. }
  192. /* The qdisc root lock is a mechanism by which to top level
  193. * of a qdisc tree can be locked from any qdisc node in the
  194. * forest. This allows changing the configuration of some
  195. * aspect of the qdisc tree while blocking out asynchronous
  196. * qdisc access in the packet processing paths.
  197. *
  198. * It is only legal to do this when the root will not change
  199. * on us. Otherwise we'll potentially lock the wrong qdisc
  200. * root. This is enforced by holding the RTNL semaphore, which
  201. * all users of this lock accessor must do.
  202. */
  203. static inline spinlock_t *qdisc_root_lock(struct Qdisc *qdisc)
  204. {
  205. struct Qdisc *root = qdisc_root(qdisc);
  206. ASSERT_RTNL();
  207. return qdisc_lock(root);
  208. }
  209. static inline spinlock_t *qdisc_root_sleeping_lock(struct Qdisc *qdisc)
  210. {
  211. struct Qdisc *root = qdisc_root_sleeping(qdisc);
  212. ASSERT_RTNL();
  213. return qdisc_lock(root);
  214. }
  215. static inline struct net_device *qdisc_dev(struct Qdisc *qdisc)
  216. {
  217. return qdisc->dev_queue->dev;
  218. }
  219. static inline void sch_tree_lock(struct Qdisc *q)
  220. {
  221. spin_lock_bh(qdisc_root_sleeping_lock(q));
  222. }
  223. static inline void sch_tree_unlock(struct Qdisc *q)
  224. {
  225. spin_unlock_bh(qdisc_root_sleeping_lock(q));
  226. }
  227. #define tcf_tree_lock(tp) sch_tree_lock((tp)->q)
  228. #define tcf_tree_unlock(tp) sch_tree_unlock((tp)->q)
  229. extern struct Qdisc noop_qdisc;
  230. extern struct Qdisc_ops noop_qdisc_ops;
  231. extern struct Qdisc_ops pfifo_fast_ops;
  232. extern struct Qdisc_ops mq_qdisc_ops;
  233. struct Qdisc_class_common {
  234. u32 classid;
  235. struct hlist_node hnode;
  236. };
  237. struct Qdisc_class_hash {
  238. struct hlist_head *hash;
  239. unsigned int hashsize;
  240. unsigned int hashmask;
  241. unsigned int hashelems;
  242. };
  243. static inline unsigned int qdisc_class_hash(u32 id, u32 mask)
  244. {
  245. id ^= id >> 8;
  246. id ^= id >> 4;
  247. return id & mask;
  248. }
  249. static inline struct Qdisc_class_common *
  250. qdisc_class_find(struct Qdisc_class_hash *hash, u32 id)
  251. {
  252. struct Qdisc_class_common *cl;
  253. struct hlist_node *n;
  254. unsigned int h;
  255. h = qdisc_class_hash(id, hash->hashmask);
  256. hlist_for_each_entry(cl, n, &hash->hash[h], hnode) {
  257. if (cl->classid == id)
  258. return cl;
  259. }
  260. return NULL;
  261. }
  262. extern int qdisc_class_hash_init(struct Qdisc_class_hash *);
  263. extern void qdisc_class_hash_insert(struct Qdisc_class_hash *, struct Qdisc_class_common *);
  264. extern void qdisc_class_hash_remove(struct Qdisc_class_hash *, struct Qdisc_class_common *);
  265. extern void qdisc_class_hash_grow(struct Qdisc *, struct Qdisc_class_hash *);
  266. extern void qdisc_class_hash_destroy(struct Qdisc_class_hash *);
  267. extern void dev_init_scheduler(struct net_device *dev);
  268. extern void dev_shutdown(struct net_device *dev);
  269. extern void dev_activate(struct net_device *dev);
  270. extern void dev_deactivate(struct net_device *dev);
  271. extern void dev_deactivate_many(struct list_head *head);
  272. extern struct Qdisc *dev_graft_qdisc(struct netdev_queue *dev_queue,
  273. struct Qdisc *qdisc);
  274. extern void qdisc_reset(struct Qdisc *qdisc);
  275. extern void qdisc_destroy(struct Qdisc *qdisc);
  276. extern void qdisc_tree_decrease_qlen(struct Qdisc *qdisc, unsigned int n);
  277. extern struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue,
  278. struct Qdisc_ops *ops);
  279. extern struct Qdisc *qdisc_create_dflt(struct netdev_queue *dev_queue,
  280. struct Qdisc_ops *ops, u32 parentid);
  281. extern void qdisc_calculate_pkt_len(struct sk_buff *skb,
  282. struct qdisc_size_table *stab);
  283. extern void tcf_destroy(struct tcf_proto *tp);
  284. extern void tcf_destroy_chain(struct tcf_proto **fl);
  285. /* Reset all TX qdiscs greater then index of a device. */
  286. static inline void qdisc_reset_all_tx_gt(struct net_device *dev, unsigned int i)
  287. {
  288. struct Qdisc *qdisc;
  289. for (; i < dev->num_tx_queues; i++) {
  290. qdisc = netdev_get_tx_queue(dev, i)->qdisc;
  291. if (qdisc) {
  292. spin_lock_bh(qdisc_lock(qdisc));
  293. qdisc_reset(qdisc);
  294. spin_unlock_bh(qdisc_lock(qdisc));
  295. }
  296. }
  297. }
  298. static inline void qdisc_reset_all_tx(struct net_device *dev)
  299. {
  300. qdisc_reset_all_tx_gt(dev, 0);
  301. }
  302. /* Are all TX queues of the device empty? */
  303. static inline bool qdisc_all_tx_empty(const struct net_device *dev)
  304. {
  305. unsigned int i;
  306. for (i = 0; i < dev->num_tx_queues; i++) {
  307. struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
  308. const struct Qdisc *q = txq->qdisc;
  309. if (q->q.qlen)
  310. return false;
  311. }
  312. return true;
  313. }
  314. /* Are any of the TX qdiscs changing? */
  315. static inline bool qdisc_tx_changing(struct net_device *dev)
  316. {
  317. unsigned int i;
  318. for (i = 0; i < dev->num_tx_queues; i++) {
  319. struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
  320. if (txq->qdisc != txq->qdisc_sleeping)
  321. return true;
  322. }
  323. return false;
  324. }
  325. /* Is the device using the noop qdisc on all queues? */
  326. static inline bool qdisc_tx_is_noop(const struct net_device *dev)
  327. {
  328. unsigned int i;
  329. for (i = 0; i < dev->num_tx_queues; i++) {
  330. struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
  331. if (txq->qdisc != &noop_qdisc)
  332. return false;
  333. }
  334. return true;
  335. }
  336. static inline unsigned int qdisc_pkt_len(const struct sk_buff *skb)
  337. {
  338. return qdisc_skb_cb(skb)->pkt_len;
  339. }
  340. /* additional qdisc xmit flags (NET_XMIT_MASK in linux/netdevice.h) */
  341. enum net_xmit_qdisc_t {
  342. __NET_XMIT_STOLEN = 0x00010000,
  343. __NET_XMIT_BYPASS = 0x00020000,
  344. };
  345. #ifdef CONFIG_NET_CLS_ACT
  346. #define net_xmit_drop_count(e) ((e) & __NET_XMIT_STOLEN ? 0 : 1)
  347. #else
  348. #define net_xmit_drop_count(e) (1)
  349. #endif
  350. static inline int qdisc_enqueue(struct sk_buff *skb, struct Qdisc *sch)
  351. {
  352. #ifdef CONFIG_NET_SCHED
  353. if (sch->stab)
  354. qdisc_calculate_pkt_len(skb, sch->stab);
  355. #endif
  356. return sch->enqueue(skb, sch);
  357. }
  358. static inline int qdisc_enqueue_root(struct sk_buff *skb, struct Qdisc *sch)
  359. {
  360. qdisc_skb_cb(skb)->pkt_len = skb->len;
  361. return qdisc_enqueue(skb, sch) & NET_XMIT_MASK;
  362. }
  363. static inline void bstats_update(struct gnet_stats_basic_packed *bstats,
  364. const struct sk_buff *skb)
  365. {
  366. bstats->bytes += qdisc_pkt_len(skb);
  367. bstats->packets += skb_is_gso(skb) ? skb_shinfo(skb)->gso_segs : 1;
  368. }
  369. static inline void qdisc_bstats_update(struct Qdisc *sch,
  370. const struct sk_buff *skb)
  371. {
  372. bstats_update(&sch->bstats, skb);
  373. }
  374. static inline int __qdisc_enqueue_tail(struct sk_buff *skb, struct Qdisc *sch,
  375. struct sk_buff_head *list)
  376. {
  377. __skb_queue_tail(list, skb);
  378. sch->qstats.backlog += qdisc_pkt_len(skb);
  379. qdisc_bstats_update(sch, skb);
  380. return NET_XMIT_SUCCESS;
  381. }
  382. static inline int qdisc_enqueue_tail(struct sk_buff *skb, struct Qdisc *sch)
  383. {
  384. return __qdisc_enqueue_tail(skb, sch, &sch->q);
  385. }
  386. static inline struct sk_buff *__qdisc_dequeue_head(struct Qdisc *sch,
  387. struct sk_buff_head *list)
  388. {
  389. struct sk_buff *skb = __skb_dequeue(list);
  390. if (likely(skb != NULL))
  391. sch->qstats.backlog -= qdisc_pkt_len(skb);
  392. return skb;
  393. }
  394. static inline struct sk_buff *qdisc_dequeue_head(struct Qdisc *sch)
  395. {
  396. return __qdisc_dequeue_head(sch, &sch->q);
  397. }
  398. static inline unsigned int __qdisc_queue_drop_head(struct Qdisc *sch,
  399. struct sk_buff_head *list)
  400. {
  401. struct sk_buff *skb = __qdisc_dequeue_head(sch, list);
  402. if (likely(skb != NULL)) {
  403. unsigned int len = qdisc_pkt_len(skb);
  404. kfree_skb(skb);
  405. return len;
  406. }
  407. return 0;
  408. }
  409. static inline unsigned int qdisc_queue_drop_head(struct Qdisc *sch)
  410. {
  411. return __qdisc_queue_drop_head(sch, &sch->q);
  412. }
  413. static inline struct sk_buff *__qdisc_dequeue_tail(struct Qdisc *sch,
  414. struct sk_buff_head *list)
  415. {
  416. struct sk_buff *skb = __skb_dequeue_tail(list);
  417. if (likely(skb != NULL))
  418. sch->qstats.backlog -= qdisc_pkt_len(skb);
  419. return skb;
  420. }
  421. static inline struct sk_buff *qdisc_dequeue_tail(struct Qdisc *sch)
  422. {
  423. return __qdisc_dequeue_tail(sch, &sch->q);
  424. }
  425. static inline struct sk_buff *qdisc_peek_head(struct Qdisc *sch)
  426. {
  427. return skb_peek(&sch->q);
  428. }
  429. /* generic pseudo peek method for non-work-conserving qdisc */
  430. static inline struct sk_buff *qdisc_peek_dequeued(struct Qdisc *sch)
  431. {
  432. /* we can reuse ->gso_skb because peek isn't called for root qdiscs */
  433. if (!sch->gso_skb) {
  434. sch->gso_skb = sch->dequeue(sch);
  435. if (sch->gso_skb)
  436. /* it's still part of the queue */
  437. sch->q.qlen++;
  438. }
  439. return sch->gso_skb;
  440. }
  441. /* use instead of qdisc->dequeue() for all qdiscs queried with ->peek() */
  442. static inline struct sk_buff *qdisc_dequeue_peeked(struct Qdisc *sch)
  443. {
  444. struct sk_buff *skb = sch->gso_skb;
  445. if (skb) {
  446. sch->gso_skb = NULL;
  447. sch->q.qlen--;
  448. } else {
  449. skb = sch->dequeue(sch);
  450. }
  451. return skb;
  452. }
  453. static inline void __qdisc_reset_queue(struct Qdisc *sch,
  454. struct sk_buff_head *list)
  455. {
  456. /*
  457. * We do not know the backlog in bytes of this list, it
  458. * is up to the caller to correct it
  459. */
  460. __skb_queue_purge(list);
  461. }
  462. static inline void qdisc_reset_queue(struct Qdisc *sch)
  463. {
  464. __qdisc_reset_queue(sch, &sch->q);
  465. sch->qstats.backlog = 0;
  466. }
  467. static inline unsigned int __qdisc_queue_drop(struct Qdisc *sch,
  468. struct sk_buff_head *list)
  469. {
  470. struct sk_buff *skb = __qdisc_dequeue_tail(sch, list);
  471. if (likely(skb != NULL)) {
  472. unsigned int len = qdisc_pkt_len(skb);
  473. kfree_skb(skb);
  474. return len;
  475. }
  476. return 0;
  477. }
  478. static inline unsigned int qdisc_queue_drop(struct Qdisc *sch)
  479. {
  480. return __qdisc_queue_drop(sch, &sch->q);
  481. }
  482. static inline int qdisc_drop(struct sk_buff *skb, struct Qdisc *sch)
  483. {
  484. kfree_skb(skb);
  485. sch->qstats.drops++;
  486. return NET_XMIT_DROP;
  487. }
  488. static inline int qdisc_reshape_fail(struct sk_buff *skb, struct Qdisc *sch)
  489. {
  490. sch->qstats.drops++;
  491. #ifdef CONFIG_NET_CLS_ACT
  492. if (sch->reshape_fail == NULL || sch->reshape_fail(skb, sch))
  493. goto drop;
  494. return NET_XMIT_SUCCESS;
  495. drop:
  496. #endif
  497. kfree_skb(skb);
  498. return NET_XMIT_DROP;
  499. }
  500. /* Length to Time (L2T) lookup in a qdisc_rate_table, to determine how
  501. long it will take to send a packet given its size.
  502. */
  503. static inline u32 qdisc_l2t(struct qdisc_rate_table* rtab, unsigned int pktlen)
  504. {
  505. int slot = pktlen + rtab->rate.cell_align + rtab->rate.overhead;
  506. if (slot < 0)
  507. slot = 0;
  508. slot >>= rtab->rate.cell_log;
  509. if (slot > 255)
  510. return rtab->data[255]*(slot >> 8) + rtab->data[slot & 0xFF];
  511. return rtab->data[slot];
  512. }
  513. #ifdef CONFIG_NET_CLS_ACT
  514. static inline struct sk_buff *skb_act_clone(struct sk_buff *skb, gfp_t gfp_mask,
  515. int action)
  516. {
  517. struct sk_buff *n;
  518. n = skb_clone(skb, gfp_mask);
  519. if (n) {
  520. n->tc_verd = SET_TC_VERD(n->tc_verd, 0);
  521. n->tc_verd = CLR_TC_OK2MUNGE(n->tc_verd);
  522. n->tc_verd = CLR_TC_MUNGED(n->tc_verd);
  523. }
  524. return n;
  525. }
  526. #endif
  527. #endif