sch_generic.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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. {
  17. struct tc_ratespec rate;
  18. u32 data[256];
  19. struct qdisc_rate_table *next;
  20. int refcnt;
  21. };
  22. enum qdisc_state_t
  23. {
  24. __QDISC_STATE_RUNNING,
  25. __QDISC_STATE_SCHED,
  26. };
  27. struct qdisc_size_table {
  28. struct list_head list;
  29. struct tc_sizespec szopts;
  30. int refcnt;
  31. u16 data[];
  32. };
  33. struct Qdisc
  34. {
  35. int (*enqueue)(struct sk_buff *skb, struct Qdisc *dev);
  36. struct sk_buff * (*dequeue)(struct Qdisc *dev);
  37. unsigned flags;
  38. #define TCQ_F_BUILTIN 1
  39. #define TCQ_F_THROTTLED 2
  40. #define TCQ_F_INGRESS 4
  41. int padded;
  42. struct Qdisc_ops *ops;
  43. struct qdisc_size_table *stab;
  44. u32 handle;
  45. u32 parent;
  46. atomic_t refcnt;
  47. unsigned long state;
  48. struct sk_buff *gso_skb;
  49. struct sk_buff_head q;
  50. struct netdev_queue *dev_queue;
  51. struct Qdisc *next_sched;
  52. struct list_head list;
  53. struct gnet_stats_basic bstats;
  54. struct gnet_stats_queue qstats;
  55. struct gnet_stats_rate_est rate_est;
  56. struct rcu_head q_rcu;
  57. int (*reshape_fail)(struct sk_buff *skb,
  58. struct Qdisc *q);
  59. void *u32_node;
  60. /* This field is deprecated, but it is still used by CBQ
  61. * and it will live until better solution will be invented.
  62. */
  63. struct Qdisc *__parent;
  64. };
  65. struct Qdisc_class_ops
  66. {
  67. /* Child qdisc manipulation */
  68. int (*graft)(struct Qdisc *, unsigned long cl,
  69. struct Qdisc *, struct Qdisc **);
  70. struct Qdisc * (*leaf)(struct Qdisc *, unsigned long cl);
  71. void (*qlen_notify)(struct Qdisc *, unsigned long);
  72. /* Class manipulation routines */
  73. unsigned long (*get)(struct Qdisc *, u32 classid);
  74. void (*put)(struct Qdisc *, unsigned long);
  75. int (*change)(struct Qdisc *, u32, u32,
  76. struct nlattr **, unsigned long *);
  77. int (*delete)(struct Qdisc *, unsigned long);
  78. void (*walk)(struct Qdisc *, struct qdisc_walker * arg);
  79. /* Filter manipulation */
  80. struct tcf_proto ** (*tcf_chain)(struct Qdisc *, unsigned long);
  81. unsigned long (*bind_tcf)(struct Qdisc *, unsigned long,
  82. u32 classid);
  83. void (*unbind_tcf)(struct Qdisc *, unsigned long);
  84. /* rtnetlink specific */
  85. int (*dump)(struct Qdisc *, unsigned long,
  86. struct sk_buff *skb, struct tcmsg*);
  87. int (*dump_stats)(struct Qdisc *, unsigned long,
  88. struct gnet_dump *);
  89. };
  90. struct Qdisc_ops
  91. {
  92. struct Qdisc_ops *next;
  93. const struct Qdisc_class_ops *cl_ops;
  94. char id[IFNAMSIZ];
  95. int priv_size;
  96. int (*enqueue)(struct sk_buff *, struct Qdisc *);
  97. struct sk_buff * (*dequeue)(struct Qdisc *);
  98. int (*requeue)(struct sk_buff *, struct Qdisc *);
  99. unsigned int (*drop)(struct Qdisc *);
  100. int (*init)(struct Qdisc *, struct nlattr *arg);
  101. void (*reset)(struct Qdisc *);
  102. void (*destroy)(struct Qdisc *);
  103. int (*change)(struct Qdisc *, struct nlattr *arg);
  104. int (*dump)(struct Qdisc *, struct sk_buff *);
  105. int (*dump_stats)(struct Qdisc *, struct gnet_dump *);
  106. struct module *owner;
  107. };
  108. struct tcf_result
  109. {
  110. unsigned long class;
  111. u32 classid;
  112. };
  113. struct tcf_proto_ops
  114. {
  115. struct tcf_proto_ops *next;
  116. char kind[IFNAMSIZ];
  117. int (*classify)(struct sk_buff*, struct tcf_proto*,
  118. struct tcf_result *);
  119. int (*init)(struct tcf_proto*);
  120. void (*destroy)(struct tcf_proto*);
  121. unsigned long (*get)(struct tcf_proto*, u32 handle);
  122. void (*put)(struct tcf_proto*, unsigned long);
  123. int (*change)(struct tcf_proto*, unsigned long,
  124. u32 handle, struct nlattr **,
  125. unsigned long *);
  126. int (*delete)(struct tcf_proto*, unsigned long);
  127. void (*walk)(struct tcf_proto*, struct tcf_walker *arg);
  128. /* rtnetlink specific */
  129. int (*dump)(struct tcf_proto*, unsigned long,
  130. struct sk_buff *skb, struct tcmsg*);
  131. struct module *owner;
  132. };
  133. struct tcf_proto
  134. {
  135. /* Fast access part */
  136. struct tcf_proto *next;
  137. void *root;
  138. int (*classify)(struct sk_buff*, struct tcf_proto*,
  139. struct tcf_result *);
  140. __be16 protocol;
  141. /* All the rest */
  142. u32 prio;
  143. u32 classid;
  144. struct Qdisc *q;
  145. void *data;
  146. struct tcf_proto_ops *ops;
  147. };
  148. struct qdisc_skb_cb {
  149. unsigned int pkt_len;
  150. char data[];
  151. };
  152. static inline struct qdisc_skb_cb *qdisc_skb_cb(struct sk_buff *skb)
  153. {
  154. return (struct qdisc_skb_cb *)skb->cb;
  155. }
  156. static inline spinlock_t *qdisc_lock(struct Qdisc *qdisc)
  157. {
  158. return &qdisc->q.lock;
  159. }
  160. static inline struct Qdisc *qdisc_root(struct Qdisc *qdisc)
  161. {
  162. return qdisc->dev_queue->qdisc;
  163. }
  164. static inline spinlock_t *qdisc_root_lock(struct Qdisc *qdisc)
  165. {
  166. struct Qdisc *root = qdisc_root(qdisc);
  167. return qdisc_lock(root);
  168. }
  169. static inline struct net_device *qdisc_dev(struct Qdisc *qdisc)
  170. {
  171. return qdisc->dev_queue->dev;
  172. }
  173. static inline void sch_tree_lock(struct Qdisc *q)
  174. {
  175. spin_lock_bh(qdisc_root_lock(q));
  176. }
  177. static inline void sch_tree_unlock(struct Qdisc *q)
  178. {
  179. spin_unlock_bh(qdisc_root_lock(q));
  180. }
  181. #define tcf_tree_lock(tp) sch_tree_lock((tp)->q)
  182. #define tcf_tree_unlock(tp) sch_tree_unlock((tp)->q)
  183. extern struct Qdisc noop_qdisc;
  184. extern struct Qdisc_ops noop_qdisc_ops;
  185. struct Qdisc_class_common
  186. {
  187. u32 classid;
  188. struct hlist_node hnode;
  189. };
  190. struct Qdisc_class_hash
  191. {
  192. struct hlist_head *hash;
  193. unsigned int hashsize;
  194. unsigned int hashmask;
  195. unsigned int hashelems;
  196. };
  197. static inline unsigned int qdisc_class_hash(u32 id, u32 mask)
  198. {
  199. id ^= id >> 8;
  200. id ^= id >> 4;
  201. return id & mask;
  202. }
  203. static inline struct Qdisc_class_common *
  204. qdisc_class_find(struct Qdisc_class_hash *hash, u32 id)
  205. {
  206. struct Qdisc_class_common *cl;
  207. struct hlist_node *n;
  208. unsigned int h;
  209. h = qdisc_class_hash(id, hash->hashmask);
  210. hlist_for_each_entry(cl, n, &hash->hash[h], hnode) {
  211. if (cl->classid == id)
  212. return cl;
  213. }
  214. return NULL;
  215. }
  216. extern int qdisc_class_hash_init(struct Qdisc_class_hash *);
  217. extern void qdisc_class_hash_insert(struct Qdisc_class_hash *, struct Qdisc_class_common *);
  218. extern void qdisc_class_hash_remove(struct Qdisc_class_hash *, struct Qdisc_class_common *);
  219. extern void qdisc_class_hash_grow(struct Qdisc *, struct Qdisc_class_hash *);
  220. extern void qdisc_class_hash_destroy(struct Qdisc_class_hash *);
  221. extern void dev_init_scheduler(struct net_device *dev);
  222. extern void dev_shutdown(struct net_device *dev);
  223. extern void dev_activate(struct net_device *dev);
  224. extern void dev_deactivate(struct net_device *dev);
  225. extern void qdisc_reset(struct Qdisc *qdisc);
  226. extern void qdisc_destroy(struct Qdisc *qdisc);
  227. extern void qdisc_tree_decrease_qlen(struct Qdisc *qdisc, unsigned int n);
  228. extern struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue,
  229. struct Qdisc_ops *ops);
  230. extern struct Qdisc *qdisc_create_dflt(struct net_device *dev,
  231. struct netdev_queue *dev_queue,
  232. struct Qdisc_ops *ops, u32 parentid);
  233. extern void qdisc_calculate_pkt_len(struct sk_buff *skb,
  234. struct qdisc_size_table *stab);
  235. extern void tcf_destroy(struct tcf_proto *tp);
  236. extern void tcf_destroy_chain(struct tcf_proto **fl);
  237. /* Reset all TX qdiscs of a device. */
  238. static inline void qdisc_reset_all_tx(struct net_device *dev)
  239. {
  240. unsigned int i;
  241. for (i = 0; i < dev->num_tx_queues; i++)
  242. qdisc_reset(netdev_get_tx_queue(dev, i)->qdisc);
  243. }
  244. /* Are all TX queues of the device empty? */
  245. static inline bool qdisc_all_tx_empty(const struct net_device *dev)
  246. {
  247. unsigned int i;
  248. for (i = 0; i < dev->num_tx_queues; i++) {
  249. struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
  250. const struct Qdisc *q = txq->qdisc;
  251. if (q->q.qlen)
  252. return false;
  253. }
  254. return true;
  255. }
  256. /* Are any of the TX qdiscs changing? */
  257. static inline bool qdisc_tx_changing(struct net_device *dev)
  258. {
  259. unsigned int i;
  260. for (i = 0; i < dev->num_tx_queues; i++) {
  261. struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
  262. if (txq->qdisc != txq->qdisc_sleeping)
  263. return true;
  264. }
  265. return false;
  266. }
  267. /* Is the device using the noop qdisc on all queues? */
  268. static inline bool qdisc_tx_is_noop(const struct net_device *dev)
  269. {
  270. unsigned int i;
  271. for (i = 0; i < dev->num_tx_queues; i++) {
  272. struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
  273. if (txq->qdisc != &noop_qdisc)
  274. return false;
  275. }
  276. return true;
  277. }
  278. static inline unsigned int qdisc_pkt_len(struct sk_buff *skb)
  279. {
  280. return qdisc_skb_cb(skb)->pkt_len;
  281. }
  282. static inline int qdisc_enqueue(struct sk_buff *skb, struct Qdisc *sch)
  283. {
  284. #ifdef CONFIG_NET_SCHED
  285. if (sch->stab)
  286. qdisc_calculate_pkt_len(skb, sch->stab);
  287. #endif
  288. return sch->enqueue(skb, sch);
  289. }
  290. static inline int qdisc_enqueue_root(struct sk_buff *skb, struct Qdisc *sch)
  291. {
  292. qdisc_skb_cb(skb)->pkt_len = skb->len;
  293. return qdisc_enqueue(skb, sch);
  294. }
  295. static inline int __qdisc_enqueue_tail(struct sk_buff *skb, struct Qdisc *sch,
  296. struct sk_buff_head *list)
  297. {
  298. __skb_queue_tail(list, skb);
  299. sch->qstats.backlog += qdisc_pkt_len(skb);
  300. sch->bstats.bytes += qdisc_pkt_len(skb);
  301. sch->bstats.packets++;
  302. return NET_XMIT_SUCCESS;
  303. }
  304. static inline int qdisc_enqueue_tail(struct sk_buff *skb, struct Qdisc *sch)
  305. {
  306. return __qdisc_enqueue_tail(skb, sch, &sch->q);
  307. }
  308. static inline struct sk_buff *__qdisc_dequeue_head(struct Qdisc *sch,
  309. struct sk_buff_head *list)
  310. {
  311. struct sk_buff *skb = __skb_dequeue(list);
  312. if (likely(skb != NULL))
  313. sch->qstats.backlog -= qdisc_pkt_len(skb);
  314. return skb;
  315. }
  316. static inline struct sk_buff *qdisc_dequeue_head(struct Qdisc *sch)
  317. {
  318. return __qdisc_dequeue_head(sch, &sch->q);
  319. }
  320. static inline struct sk_buff *__qdisc_dequeue_tail(struct Qdisc *sch,
  321. struct sk_buff_head *list)
  322. {
  323. struct sk_buff *skb = __skb_dequeue_tail(list);
  324. if (likely(skb != NULL))
  325. sch->qstats.backlog -= qdisc_pkt_len(skb);
  326. return skb;
  327. }
  328. static inline struct sk_buff *qdisc_dequeue_tail(struct Qdisc *sch)
  329. {
  330. return __qdisc_dequeue_tail(sch, &sch->q);
  331. }
  332. static inline int __qdisc_requeue(struct sk_buff *skb, struct Qdisc *sch,
  333. struct sk_buff_head *list)
  334. {
  335. __skb_queue_head(list, skb);
  336. sch->qstats.backlog += qdisc_pkt_len(skb);
  337. sch->qstats.requeues++;
  338. return NET_XMIT_SUCCESS;
  339. }
  340. static inline int qdisc_requeue(struct sk_buff *skb, struct Qdisc *sch)
  341. {
  342. return __qdisc_requeue(skb, sch, &sch->q);
  343. }
  344. static inline void __qdisc_reset_queue(struct Qdisc *sch,
  345. struct sk_buff_head *list)
  346. {
  347. /*
  348. * We do not know the backlog in bytes of this list, it
  349. * is up to the caller to correct it
  350. */
  351. __skb_queue_purge(list);
  352. }
  353. static inline void qdisc_reset_queue(struct Qdisc *sch)
  354. {
  355. __qdisc_reset_queue(sch, &sch->q);
  356. sch->qstats.backlog = 0;
  357. }
  358. static inline unsigned int __qdisc_queue_drop(struct Qdisc *sch,
  359. struct sk_buff_head *list)
  360. {
  361. struct sk_buff *skb = __qdisc_dequeue_tail(sch, list);
  362. if (likely(skb != NULL)) {
  363. unsigned int len = qdisc_pkt_len(skb);
  364. kfree_skb(skb);
  365. return len;
  366. }
  367. return 0;
  368. }
  369. static inline unsigned int qdisc_queue_drop(struct Qdisc *sch)
  370. {
  371. return __qdisc_queue_drop(sch, &sch->q);
  372. }
  373. static inline int qdisc_drop(struct sk_buff *skb, struct Qdisc *sch)
  374. {
  375. kfree_skb(skb);
  376. sch->qstats.drops++;
  377. return NET_XMIT_DROP;
  378. }
  379. static inline int qdisc_reshape_fail(struct sk_buff *skb, struct Qdisc *sch)
  380. {
  381. sch->qstats.drops++;
  382. #ifdef CONFIG_NET_CLS_ACT
  383. if (sch->reshape_fail == NULL || sch->reshape_fail(skb, sch))
  384. goto drop;
  385. return NET_XMIT_SUCCESS;
  386. drop:
  387. #endif
  388. kfree_skb(skb);
  389. return NET_XMIT_DROP;
  390. }
  391. /* Length to Time (L2T) lookup in a qdisc_rate_table, to determine how
  392. long it will take to send a packet given its size.
  393. */
  394. static inline u32 qdisc_l2t(struct qdisc_rate_table* rtab, unsigned int pktlen)
  395. {
  396. int slot = pktlen + rtab->rate.cell_align + rtab->rate.overhead;
  397. if (slot < 0)
  398. slot = 0;
  399. slot >>= rtab->rate.cell_log;
  400. if (slot > 255)
  401. return (rtab->data[255]*(slot >> 8) + rtab->data[slot & 0xFF]);
  402. return rtab->data[slot];
  403. }
  404. #ifdef CONFIG_NET_CLS_ACT
  405. static inline struct sk_buff *skb_act_clone(struct sk_buff *skb, gfp_t gfp_mask)
  406. {
  407. struct sk_buff *n = skb_clone(skb, gfp_mask);
  408. if (n) {
  409. n->tc_verd = SET_TC_VERD(n->tc_verd, 0);
  410. n->tc_verd = CLR_TC_OK2MUNGE(n->tc_verd);
  411. n->tc_verd = CLR_TC_MUNGED(n->tc_verd);
  412. }
  413. return n;
  414. }
  415. #endif
  416. #endif