blob: 0ab53c575f87b39d72fce0df01bbbef61ae96ecb [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef __NET_SCHED_GENERIC_H
2#define __NET_SCHED_GENERIC_H
3
Linus Torvalds1da177e2005-04-16 15:20:36 -07004#include <linux/netdevice.h>
5#include <linux/types.h>
6#include <linux/rcupdate.h>
7#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/pkt_sched.h>
9#include <linux/pkt_cls.h>
10#include <net/gen_stats.h>
Thomas Grafbe577dd2007-03-22 11:55:50 -070011#include <net/rtnetlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012
13struct Qdisc_ops;
14struct qdisc_walker;
15struct tcf_walker;
16struct module;
17
18struct qdisc_rate_table
19{
20 struct tc_ratespec rate;
21 u32 data[256];
22 struct qdisc_rate_table *next;
23 int refcnt;
24};
25
26struct 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 struct sk_buff_head q;
David S. Millerbb949fb2008-07-08 16:55:56 -070040 struct netdev_queue *dev_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 struct net_device *dev;
42 struct list_head list;
43
44 struct gnet_stats_basic bstats;
45 struct gnet_stats_queue qstats;
46 struct gnet_stats_rate_est rate_est;
47 spinlock_t *stats_lock;
48 struct rcu_head q_rcu;
49 int (*reshape_fail)(struct sk_buff *skb,
50 struct Qdisc *q);
51
52 /* This field is deprecated, but it is still used by CBQ
53 * and it will live until better solution will be invented.
54 */
55 struct Qdisc *__parent;
56};
57
58struct Qdisc_class_ops
59{
60 /* Child qdisc manipulation */
61 int (*graft)(struct Qdisc *, unsigned long cl,
62 struct Qdisc *, struct Qdisc **);
63 struct Qdisc * (*leaf)(struct Qdisc *, unsigned long cl);
Patrick McHardy43effa12006-11-29 17:35:48 -080064 void (*qlen_notify)(struct Qdisc *, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66 /* Class manipulation routines */
67 unsigned long (*get)(struct Qdisc *, u32 classid);
68 void (*put)(struct Qdisc *, unsigned long);
69 int (*change)(struct Qdisc *, u32, u32,
Patrick McHardy1e904742008-01-22 22:11:17 -080070 struct nlattr **, unsigned long *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 int (*delete)(struct Qdisc *, unsigned long);
72 void (*walk)(struct Qdisc *, struct qdisc_walker * arg);
73
74 /* Filter manipulation */
75 struct tcf_proto ** (*tcf_chain)(struct Qdisc *, unsigned long);
76 unsigned long (*bind_tcf)(struct Qdisc *, unsigned long,
77 u32 classid);
78 void (*unbind_tcf)(struct Qdisc *, unsigned long);
79
80 /* rtnetlink specific */
81 int (*dump)(struct Qdisc *, unsigned long,
82 struct sk_buff *skb, struct tcmsg*);
83 int (*dump_stats)(struct Qdisc *, unsigned long,
84 struct gnet_dump *);
85};
86
87struct Qdisc_ops
88{
89 struct Qdisc_ops *next;
Eric Dumazet20fea082007-11-14 01:44:41 -080090 const struct Qdisc_class_ops *cl_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 char id[IFNAMSIZ];
92 int priv_size;
93
94 int (*enqueue)(struct sk_buff *, struct Qdisc *);
95 struct sk_buff * (*dequeue)(struct Qdisc *);
96 int (*requeue)(struct sk_buff *, struct Qdisc *);
97 unsigned int (*drop)(struct Qdisc *);
98
Patrick McHardy1e904742008-01-22 22:11:17 -080099 int (*init)(struct Qdisc *, struct nlattr *arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 void (*reset)(struct Qdisc *);
101 void (*destroy)(struct Qdisc *);
Patrick McHardy1e904742008-01-22 22:11:17 -0800102 int (*change)(struct Qdisc *, struct nlattr *arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104 int (*dump)(struct Qdisc *, struct sk_buff *);
105 int (*dump_stats)(struct Qdisc *, struct gnet_dump *);
106
107 struct module *owner;
108};
109
110
111struct tcf_result
112{
113 unsigned long class;
114 u32 classid;
115};
116
117struct tcf_proto_ops
118{
119 struct tcf_proto_ops *next;
120 char kind[IFNAMSIZ];
121
122 int (*classify)(struct sk_buff*, struct tcf_proto*,
123 struct tcf_result *);
124 int (*init)(struct tcf_proto*);
125 void (*destroy)(struct tcf_proto*);
126
127 unsigned long (*get)(struct tcf_proto*, u32 handle);
128 void (*put)(struct tcf_proto*, unsigned long);
129 int (*change)(struct tcf_proto*, unsigned long,
Patrick McHardyadd93b62008-01-22 22:11:33 -0800130 u32 handle, struct nlattr **,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 unsigned long *);
132 int (*delete)(struct tcf_proto*, unsigned long);
133 void (*walk)(struct tcf_proto*, struct tcf_walker *arg);
134
135 /* rtnetlink specific */
136 int (*dump)(struct tcf_proto*, unsigned long,
137 struct sk_buff *skb, struct tcmsg*);
138
139 struct module *owner;
140};
141
142struct tcf_proto
143{
144 /* Fast access part */
145 struct tcf_proto *next;
146 void *root;
147 int (*classify)(struct sk_buff*, struct tcf_proto*,
148 struct tcf_result *);
Al Viro66c6f522006-11-20 18:07:51 -0800149 __be16 protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151 /* All the rest */
152 u32 prio;
153 u32 classid;
154 struct Qdisc *q;
155 void *data;
156 struct tcf_proto_ops *ops;
157};
158
159
160extern void qdisc_lock_tree(struct net_device *dev);
161extern void qdisc_unlock_tree(struct net_device *dev);
162
163#define sch_tree_lock(q) qdisc_lock_tree((q)->dev)
164#define sch_tree_unlock(q) qdisc_unlock_tree((q)->dev)
165#define tcf_tree_lock(tp) qdisc_lock_tree((tp)->q->dev)
166#define tcf_tree_unlock(tp) qdisc_unlock_tree((tp)->q->dev)
167
Thomas Grafe41a33e2005-07-05 14:14:30 -0700168extern struct Qdisc noop_qdisc;
169extern struct Qdisc_ops noop_qdisc_ops;
170
Patrick McHardy6fe1c7a2008-07-05 23:21:31 -0700171struct Qdisc_class_common
172{
173 u32 classid;
174 struct hlist_node hnode;
175};
176
177struct Qdisc_class_hash
178{
179 struct hlist_head *hash;
180 unsigned int hashsize;
181 unsigned int hashmask;
182 unsigned int hashelems;
183};
184
185static inline unsigned int qdisc_class_hash(u32 id, u32 mask)
186{
187 id ^= id >> 8;
188 id ^= id >> 4;
189 return id & mask;
190}
191
192static inline struct Qdisc_class_common *
193qdisc_class_find(struct Qdisc_class_hash *hash, u32 id)
194{
195 struct Qdisc_class_common *cl;
196 struct hlist_node *n;
197 unsigned int h;
198
199 h = qdisc_class_hash(id, hash->hashmask);
200 hlist_for_each_entry(cl, n, &hash->hash[h], hnode) {
201 if (cl->classid == id)
202 return cl;
203 }
204 return NULL;
205}
206
207extern int qdisc_class_hash_init(struct Qdisc_class_hash *);
208extern void qdisc_class_hash_insert(struct Qdisc_class_hash *, struct Qdisc_class_common *);
209extern void qdisc_class_hash_remove(struct Qdisc_class_hash *, struct Qdisc_class_common *);
210extern void qdisc_class_hash_grow(struct Qdisc *, struct Qdisc_class_hash *);
211extern void qdisc_class_hash_destroy(struct Qdisc_class_hash *);
212
Thomas Grafe41a33e2005-07-05 14:14:30 -0700213extern void dev_init_scheduler(struct net_device *dev);
214extern void dev_shutdown(struct net_device *dev);
215extern void dev_activate(struct net_device *dev);
216extern void dev_deactivate(struct net_device *dev);
217extern void qdisc_reset(struct Qdisc *qdisc);
218extern void qdisc_destroy(struct Qdisc *qdisc);
Patrick McHardy43effa12006-11-29 17:35:48 -0800219extern void qdisc_tree_decrease_qlen(struct Qdisc *qdisc, unsigned int n);
David S. Millerbb949fb2008-07-08 16:55:56 -0700220extern struct Qdisc *qdisc_alloc(struct net_device *dev,
221 struct netdev_queue *dev_queue,
222 struct Qdisc_ops *ops);
Thomas Grafe41a33e2005-07-05 14:14:30 -0700223extern struct Qdisc *qdisc_create_dflt(struct net_device *dev,
David S. Millerbb949fb2008-07-08 16:55:56 -0700224 struct netdev_queue *dev_queue,
Patrick McHardy9f9afec2006-11-29 17:35:18 -0800225 struct Qdisc_ops *ops, u32 parentid);
Patrick McHardya48b5a62007-03-23 11:29:43 -0700226extern void tcf_destroy(struct tcf_proto *tp);
Patrick McHardyff31ab52008-07-01 19:52:38 -0700227extern void tcf_destroy_chain(struct tcf_proto **fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Thomas Graf9972b252005-06-18 22:57:26 -0700229static inline int __qdisc_enqueue_tail(struct sk_buff *skb, struct Qdisc *sch,
230 struct sk_buff_head *list)
231{
232 __skb_queue_tail(list, skb);
233 sch->qstats.backlog += skb->len;
234 sch->bstats.bytes += skb->len;
235 sch->bstats.packets++;
236
237 return NET_XMIT_SUCCESS;
238}
239
240static inline int qdisc_enqueue_tail(struct sk_buff *skb, struct Qdisc *sch)
241{
242 return __qdisc_enqueue_tail(skb, sch, &sch->q);
243}
244
245static inline struct sk_buff *__qdisc_dequeue_head(struct Qdisc *sch,
246 struct sk_buff_head *list)
247{
248 struct sk_buff *skb = __skb_dequeue(list);
249
250 if (likely(skb != NULL))
251 sch->qstats.backlog -= skb->len;
252
253 return skb;
254}
255
256static inline struct sk_buff *qdisc_dequeue_head(struct Qdisc *sch)
257{
258 return __qdisc_dequeue_head(sch, &sch->q);
259}
260
261static inline struct sk_buff *__qdisc_dequeue_tail(struct Qdisc *sch,
262 struct sk_buff_head *list)
263{
264 struct sk_buff *skb = __skb_dequeue_tail(list);
265
266 if (likely(skb != NULL))
267 sch->qstats.backlog -= skb->len;
268
269 return skb;
270}
271
272static inline struct sk_buff *qdisc_dequeue_tail(struct Qdisc *sch)
273{
274 return __qdisc_dequeue_tail(sch, &sch->q);
275}
276
277static inline int __qdisc_requeue(struct sk_buff *skb, struct Qdisc *sch,
278 struct sk_buff_head *list)
279{
280 __skb_queue_head(list, skb);
281 sch->qstats.backlog += skb->len;
282 sch->qstats.requeues++;
283
284 return NET_XMIT_SUCCESS;
285}
286
287static inline int qdisc_requeue(struct sk_buff *skb, struct Qdisc *sch)
288{
289 return __qdisc_requeue(skb, sch, &sch->q);
290}
291
292static inline void __qdisc_reset_queue(struct Qdisc *sch,
293 struct sk_buff_head *list)
294{
295 /*
296 * We do not know the backlog in bytes of this list, it
297 * is up to the caller to correct it
298 */
299 skb_queue_purge(list);
300}
301
302static inline void qdisc_reset_queue(struct Qdisc *sch)
303{
304 __qdisc_reset_queue(sch, &sch->q);
305 sch->qstats.backlog = 0;
306}
307
308static inline unsigned int __qdisc_queue_drop(struct Qdisc *sch,
309 struct sk_buff_head *list)
310{
311 struct sk_buff *skb = __qdisc_dequeue_tail(sch, list);
312
313 if (likely(skb != NULL)) {
314 unsigned int len = skb->len;
315 kfree_skb(skb);
316 return len;
317 }
318
319 return 0;
320}
321
322static inline unsigned int qdisc_queue_drop(struct Qdisc *sch)
323{
324 return __qdisc_queue_drop(sch, &sch->q);
325}
326
327static inline int qdisc_drop(struct sk_buff *skb, struct Qdisc *sch)
328{
329 kfree_skb(skb);
330 sch->qstats.drops++;
331
332 return NET_XMIT_DROP;
333}
334
335static inline int qdisc_reshape_fail(struct sk_buff *skb, struct Qdisc *sch)
336{
337 sch->qstats.drops++;
338
Patrick McHardyc3bc7cf2007-07-15 00:03:05 -0700339#ifdef CONFIG_NET_CLS_ACT
Thomas Graf9972b252005-06-18 22:57:26 -0700340 if (sch->reshape_fail == NULL || sch->reshape_fail(skb, sch))
341 goto drop;
342
343 return NET_XMIT_SUCCESS;
344
345drop:
346#endif
347 kfree_skb(skb);
348 return NET_XMIT_DROP;
349}
350
Jesper Dangaard Brouere9bef552007-09-12 16:35:24 +0200351/* Length to Time (L2T) lookup in a qdisc_rate_table, to determine how
352 long it will take to send a packet given its size.
353 */
354static inline u32 qdisc_l2t(struct qdisc_rate_table* rtab, unsigned int pktlen)
355{
Jesper Dangaard Brouere08b0992007-09-12 16:36:28 +0200356 int slot = pktlen + rtab->rate.cell_align + rtab->rate.overhead;
357 if (slot < 0)
358 slot = 0;
Jesper Dangaard Brouere9bef552007-09-12 16:35:24 +0200359 slot >>= rtab->rate.cell_log;
360 if (slot > 255)
361 return (rtab->data[255]*(slot >> 8) + rtab->data[slot & 0xFF]);
362 return rtab->data[slot];
363}
364
Jamal Hadi Salim12da81d2007-10-26 02:47:23 -0700365#ifdef CONFIG_NET_CLS_ACT
366static inline struct sk_buff *skb_act_clone(struct sk_buff *skb, gfp_t gfp_mask)
367{
368 struct sk_buff *n = skb_clone(skb, gfp_mask);
369
370 if (n) {
371 n->tc_verd = SET_TC_VERD(n->tc_verd, 0);
372 n->tc_verd = CLR_TC_OK2MUNGE(n->tc_verd);
373 n->tc_verd = CLR_TC_MUNGED(n->tc_verd);
Jamal Hadi Salim12da81d2007-10-26 02:47:23 -0700374 }
375 return n;
376}
377#endif
378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379#endif