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