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