sch_generic.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. struct Qdisc
  23. {
  24. int (*enqueue)(struct sk_buff *skb, struct Qdisc *dev);
  25. struct sk_buff * (*dequeue)(struct Qdisc *dev);
  26. unsigned flags;
  27. #define TCQ_F_BUILTIN 1
  28. #define TCQ_F_THROTTLED 2
  29. #define TCQ_F_INGRESS 4
  30. int padded;
  31. struct Qdisc_ops *ops;
  32. u32 handle;
  33. u32 parent;
  34. atomic_t refcnt;
  35. struct sk_buff_head q;
  36. struct net_device *dev;
  37. struct list_head list;
  38. struct gnet_stats_basic bstats;
  39. struct gnet_stats_queue qstats;
  40. struct gnet_stats_rate_est rate_est;
  41. spinlock_t *stats_lock;
  42. struct rcu_head q_rcu;
  43. int (*reshape_fail)(struct sk_buff *skb,
  44. struct Qdisc *q);
  45. /* This field is deprecated, but it is still used by CBQ
  46. * and it will live until better solution will be invented.
  47. */
  48. struct Qdisc *__parent;
  49. };
  50. struct Qdisc_class_ops
  51. {
  52. /* Child qdisc manipulation */
  53. int (*graft)(struct Qdisc *, unsigned long cl,
  54. struct Qdisc *, struct Qdisc **);
  55. struct Qdisc * (*leaf)(struct Qdisc *, unsigned long cl);
  56. void (*qlen_notify)(struct Qdisc *, unsigned long);
  57. /* Class manipulation routines */
  58. unsigned long (*get)(struct Qdisc *, u32 classid);
  59. void (*put)(struct Qdisc *, unsigned long);
  60. int (*change)(struct Qdisc *, u32, u32,
  61. struct rtattr **, unsigned long *);
  62. int (*delete)(struct Qdisc *, unsigned long);
  63. void (*walk)(struct Qdisc *, struct qdisc_walker * arg);
  64. /* Filter manipulation */
  65. struct tcf_proto ** (*tcf_chain)(struct Qdisc *, unsigned long);
  66. unsigned long (*bind_tcf)(struct Qdisc *, unsigned long,
  67. u32 classid);
  68. void (*unbind_tcf)(struct Qdisc *, unsigned long);
  69. /* rtnetlink specific */
  70. int (*dump)(struct Qdisc *, unsigned long,
  71. struct sk_buff *skb, struct tcmsg*);
  72. int (*dump_stats)(struct Qdisc *, unsigned long,
  73. struct gnet_dump *);
  74. };
  75. struct Qdisc_ops
  76. {
  77. struct Qdisc_ops *next;
  78. struct Qdisc_class_ops *cl_ops;
  79. char id[IFNAMSIZ];
  80. int priv_size;
  81. int (*enqueue)(struct sk_buff *, struct Qdisc *);
  82. struct sk_buff * (*dequeue)(struct Qdisc *);
  83. int (*requeue)(struct sk_buff *, struct Qdisc *);
  84. unsigned int (*drop)(struct Qdisc *);
  85. int (*init)(struct Qdisc *, struct rtattr *arg);
  86. void (*reset)(struct Qdisc *);
  87. void (*destroy)(struct Qdisc *);
  88. int (*change)(struct Qdisc *, struct rtattr *arg);
  89. int (*dump)(struct Qdisc *, struct sk_buff *);
  90. int (*dump_stats)(struct Qdisc *, struct gnet_dump *);
  91. struct module *owner;
  92. };
  93. struct tcf_result
  94. {
  95. unsigned long class;
  96. u32 classid;
  97. };
  98. struct tcf_proto_ops
  99. {
  100. struct tcf_proto_ops *next;
  101. char kind[IFNAMSIZ];
  102. int (*classify)(struct sk_buff*, struct tcf_proto*,
  103. struct tcf_result *);
  104. int (*init)(struct tcf_proto*);
  105. void (*destroy)(struct tcf_proto*);
  106. unsigned long (*get)(struct tcf_proto*, u32 handle);
  107. void (*put)(struct tcf_proto*, unsigned long);
  108. int (*change)(struct tcf_proto*, unsigned long,
  109. u32 handle, struct rtattr **,
  110. unsigned long *);
  111. int (*delete)(struct tcf_proto*, unsigned long);
  112. void (*walk)(struct tcf_proto*, struct tcf_walker *arg);
  113. /* rtnetlink specific */
  114. int (*dump)(struct tcf_proto*, unsigned long,
  115. struct sk_buff *skb, struct tcmsg*);
  116. struct module *owner;
  117. };
  118. struct tcf_proto
  119. {
  120. /* Fast access part */
  121. struct tcf_proto *next;
  122. void *root;
  123. int (*classify)(struct sk_buff*, struct tcf_proto*,
  124. struct tcf_result *);
  125. __be16 protocol;
  126. /* All the rest */
  127. u32 prio;
  128. u32 classid;
  129. struct Qdisc *q;
  130. void *data;
  131. struct tcf_proto_ops *ops;
  132. };
  133. extern void qdisc_lock_tree(struct net_device *dev);
  134. extern void qdisc_unlock_tree(struct net_device *dev);
  135. #define sch_tree_lock(q) qdisc_lock_tree((q)->dev)
  136. #define sch_tree_unlock(q) qdisc_unlock_tree((q)->dev)
  137. #define tcf_tree_lock(tp) qdisc_lock_tree((tp)->q->dev)
  138. #define tcf_tree_unlock(tp) qdisc_unlock_tree((tp)->q->dev)
  139. extern struct Qdisc noop_qdisc;
  140. extern struct Qdisc_ops noop_qdisc_ops;
  141. extern void dev_init_scheduler(struct net_device *dev);
  142. extern void dev_shutdown(struct net_device *dev);
  143. extern void dev_activate(struct net_device *dev);
  144. extern void dev_deactivate(struct net_device *dev);
  145. extern void qdisc_reset(struct Qdisc *qdisc);
  146. extern void qdisc_destroy(struct Qdisc *qdisc);
  147. extern void qdisc_tree_decrease_qlen(struct Qdisc *qdisc, unsigned int n);
  148. extern struct Qdisc *qdisc_alloc(struct net_device *dev, struct Qdisc_ops *ops);
  149. extern struct Qdisc *qdisc_create_dflt(struct net_device *dev,
  150. struct Qdisc_ops *ops, u32 parentid);
  151. extern void tcf_destroy(struct tcf_proto *tp);
  152. extern void tcf_destroy_chain(struct tcf_proto *fl);
  153. static inline int __qdisc_enqueue_tail(struct sk_buff *skb, struct Qdisc *sch,
  154. struct sk_buff_head *list)
  155. {
  156. __skb_queue_tail(list, skb);
  157. sch->qstats.backlog += skb->len;
  158. sch->bstats.bytes += skb->len;
  159. sch->bstats.packets++;
  160. return NET_XMIT_SUCCESS;
  161. }
  162. static inline int qdisc_enqueue_tail(struct sk_buff *skb, struct Qdisc *sch)
  163. {
  164. return __qdisc_enqueue_tail(skb, sch, &sch->q);
  165. }
  166. static inline struct sk_buff *__qdisc_dequeue_head(struct Qdisc *sch,
  167. struct sk_buff_head *list)
  168. {
  169. struct sk_buff *skb = __skb_dequeue(list);
  170. if (likely(skb != NULL))
  171. sch->qstats.backlog -= skb->len;
  172. return skb;
  173. }
  174. static inline struct sk_buff *qdisc_dequeue_head(struct Qdisc *sch)
  175. {
  176. return __qdisc_dequeue_head(sch, &sch->q);
  177. }
  178. static inline struct sk_buff *__qdisc_dequeue_tail(struct Qdisc *sch,
  179. struct sk_buff_head *list)
  180. {
  181. struct sk_buff *skb = __skb_dequeue_tail(list);
  182. if (likely(skb != NULL))
  183. sch->qstats.backlog -= skb->len;
  184. return skb;
  185. }
  186. static inline struct sk_buff *qdisc_dequeue_tail(struct Qdisc *sch)
  187. {
  188. return __qdisc_dequeue_tail(sch, &sch->q);
  189. }
  190. static inline int __qdisc_requeue(struct sk_buff *skb, struct Qdisc *sch,
  191. struct sk_buff_head *list)
  192. {
  193. __skb_queue_head(list, skb);
  194. sch->qstats.backlog += skb->len;
  195. sch->qstats.requeues++;
  196. return NET_XMIT_SUCCESS;
  197. }
  198. static inline int qdisc_requeue(struct sk_buff *skb, struct Qdisc *sch)
  199. {
  200. return __qdisc_requeue(skb, sch, &sch->q);
  201. }
  202. static inline void __qdisc_reset_queue(struct Qdisc *sch,
  203. struct sk_buff_head *list)
  204. {
  205. /*
  206. * We do not know the backlog in bytes of this list, it
  207. * is up to the caller to correct it
  208. */
  209. skb_queue_purge(list);
  210. }
  211. static inline void qdisc_reset_queue(struct Qdisc *sch)
  212. {
  213. __qdisc_reset_queue(sch, &sch->q);
  214. sch->qstats.backlog = 0;
  215. }
  216. static inline unsigned int __qdisc_queue_drop(struct Qdisc *sch,
  217. struct sk_buff_head *list)
  218. {
  219. struct sk_buff *skb = __qdisc_dequeue_tail(sch, list);
  220. if (likely(skb != NULL)) {
  221. unsigned int len = skb->len;
  222. kfree_skb(skb);
  223. return len;
  224. }
  225. return 0;
  226. }
  227. static inline unsigned int qdisc_queue_drop(struct Qdisc *sch)
  228. {
  229. return __qdisc_queue_drop(sch, &sch->q);
  230. }
  231. static inline int qdisc_drop(struct sk_buff *skb, struct Qdisc *sch)
  232. {
  233. kfree_skb(skb);
  234. sch->qstats.drops++;
  235. return NET_XMIT_DROP;
  236. }
  237. static inline int qdisc_reshape_fail(struct sk_buff *skb, struct Qdisc *sch)
  238. {
  239. sch->qstats.drops++;
  240. #ifdef CONFIG_NET_CLS_ACT
  241. if (sch->reshape_fail == NULL || sch->reshape_fail(skb, sch))
  242. goto drop;
  243. return NET_XMIT_SUCCESS;
  244. drop:
  245. #endif
  246. kfree_skb(skb);
  247. return NET_XMIT_DROP;
  248. }
  249. /* Length to Time (L2T) lookup in a qdisc_rate_table, to determine how
  250. long it will take to send a packet given its size.
  251. */
  252. static inline u32 qdisc_l2t(struct qdisc_rate_table* rtab, unsigned int pktlen)
  253. {
  254. int slot = pktlen + rtab->rate.cell_align + rtab->rate.overhead;
  255. if (slot < 0)
  256. slot = 0;
  257. slot >>= rtab->rate.cell_log;
  258. if (slot > 255)
  259. return (rtab->data[255]*(slot >> 8) + rtab->data[slot & 0xFF]);
  260. return rtab->data[slot];
  261. }
  262. #endif