sch_generic.h 11 KB

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