blob: b96c3d9e10a8af48e3cce4080585055594f2e357 [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;
David S. Millerd3b753d2008-07-15 20:14:35 -070039 struct sk_buff *gso_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 struct sk_buff_head q;
David S. Millerbb949fb2008-07-08 16:55:56 -070041 struct netdev_queue *dev_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 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
57struct 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);
Patrick McHardy43effa12006-11-29 17:35:48 -080063 void (*qlen_notify)(struct Qdisc *, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
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,
Patrick McHardy1e904742008-01-22 22:11:17 -080069 struct nlattr **, unsigned long *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 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
86struct Qdisc_ops
87{
88 struct Qdisc_ops *next;
Eric Dumazet20fea082007-11-14 01:44:41 -080089 const struct Qdisc_class_ops *cl_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 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
Patrick McHardy1e904742008-01-22 22:11:17 -080098 int (*init)(struct Qdisc *, struct nlattr *arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 void (*reset)(struct Qdisc *);
100 void (*destroy)(struct Qdisc *);
Patrick McHardy1e904742008-01-22 22:11:17 -0800101 int (*change)(struct Qdisc *, struct nlattr *arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
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
110struct tcf_result
111{
112 unsigned long class;
113 u32 classid;
114};
115
116struct 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,
Patrick McHardyadd93b62008-01-22 22:11:33 -0800129 u32 handle, struct nlattr **,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 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
141struct 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 *);
Al Viro66c6f522006-11-20 18:07:51 -0800148 __be16 protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
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
David S. Miller5ce2d482008-07-08 17:06:30 -0700158static inline struct net_device *qdisc_dev(struct Qdisc *qdisc)
159{
160 return qdisc->dev_queue->dev;
161}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163extern void qdisc_lock_tree(struct net_device *dev);
164extern void qdisc_unlock_tree(struct net_device *dev);
165
David S. Miller5ce2d482008-07-08 17:06:30 -0700166#define sch_tree_lock(q) qdisc_lock_tree(qdisc_dev(q))
167#define sch_tree_unlock(q) qdisc_unlock_tree(qdisc_dev(q))
168#define tcf_tree_lock(tp) qdisc_lock_tree(qdisc_dev((tp)->q))
169#define tcf_tree_unlock(tp) qdisc_unlock_tree(qdisc_dev((tp)->q))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Thomas Grafe41a33e2005-07-05 14:14:30 -0700171extern struct Qdisc noop_qdisc;
172extern struct Qdisc_ops noop_qdisc_ops;
173
Patrick McHardy6fe1c7a2008-07-05 23:21:31 -0700174struct Qdisc_class_common
175{
176 u32 classid;
177 struct hlist_node hnode;
178};
179
180struct Qdisc_class_hash
181{
182 struct hlist_head *hash;
183 unsigned int hashsize;
184 unsigned int hashmask;
185 unsigned int hashelems;
186};
187
188static inline unsigned int qdisc_class_hash(u32 id, u32 mask)
189{
190 id ^= id >> 8;
191 id ^= id >> 4;
192 return id & mask;
193}
194
195static inline struct Qdisc_class_common *
196qdisc_class_find(struct Qdisc_class_hash *hash, u32 id)
197{
198 struct Qdisc_class_common *cl;
199 struct hlist_node *n;
200 unsigned int h;
201
202 h = qdisc_class_hash(id, hash->hashmask);
203 hlist_for_each_entry(cl, n, &hash->hash[h], hnode) {
204 if (cl->classid == id)
205 return cl;
206 }
207 return NULL;
208}
209
210extern int qdisc_class_hash_init(struct Qdisc_class_hash *);
211extern void qdisc_class_hash_insert(struct Qdisc_class_hash *, struct Qdisc_class_common *);
212extern void qdisc_class_hash_remove(struct Qdisc_class_hash *, struct Qdisc_class_common *);
213extern void qdisc_class_hash_grow(struct Qdisc *, struct Qdisc_class_hash *);
214extern void qdisc_class_hash_destroy(struct Qdisc_class_hash *);
215
Thomas Grafe41a33e2005-07-05 14:14:30 -0700216extern void dev_init_scheduler(struct net_device *dev);
217extern void dev_shutdown(struct net_device *dev);
218extern void dev_activate(struct net_device *dev);
219extern void dev_deactivate(struct net_device *dev);
220extern void qdisc_reset(struct Qdisc *qdisc);
221extern void qdisc_destroy(struct Qdisc *qdisc);
Patrick McHardy43effa12006-11-29 17:35:48 -0800222extern void qdisc_tree_decrease_qlen(struct Qdisc *qdisc, unsigned int n);
David S. Miller5ce2d482008-07-08 17:06:30 -0700223extern struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue,
David S. Millerbb949fb2008-07-08 16:55:56 -0700224 struct Qdisc_ops *ops);
Thomas Grafe41a33e2005-07-05 14:14:30 -0700225extern struct Qdisc *qdisc_create_dflt(struct net_device *dev,
David S. Millerbb949fb2008-07-08 16:55:56 -0700226 struct netdev_queue *dev_queue,
Patrick McHardy9f9afec2006-11-29 17:35:18 -0800227 struct Qdisc_ops *ops, u32 parentid);
Patrick McHardya48b5a62007-03-23 11:29:43 -0700228extern void tcf_destroy(struct tcf_proto *tp);
Patrick McHardyff31ab52008-07-01 19:52:38 -0700229extern void tcf_destroy_chain(struct tcf_proto **fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
David S. Miller5aa70992008-07-08 22:59:10 -0700231/* Reset all TX qdiscs of a device. */
232static inline void qdisc_reset_all_tx(struct net_device *dev)
233{
David S. Millere8a04642008-07-17 00:34:19 -0700234 unsigned int i;
235 for (i = 0; i < dev->num_tx_queues; i++)
236 qdisc_reset(netdev_get_tx_queue(dev, i)->qdisc);
David S. Miller5aa70992008-07-08 22:59:10 -0700237}
238
David S. Miller3e745dd2008-07-08 23:00:25 -0700239/* Are all TX queues of the device empty? */
240static inline bool qdisc_all_tx_empty(const struct net_device *dev)
241{
David S. Millere8a04642008-07-17 00:34:19 -0700242 unsigned int i;
243 for (i = 0; i < dev->num_tx_queues; i++) {
244 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
245 const struct Qdisc *q = txq->qdisc;
David S. Miller3e745dd2008-07-08 23:00:25 -0700246
David S. Millere8a04642008-07-17 00:34:19 -0700247 if (q->q.qlen)
248 return false;
249 }
250 return true;
David S. Miller3e745dd2008-07-08 23:00:25 -0700251}
252
David S. Miller6fa98642008-07-08 23:01:06 -0700253/* Are any of the TX qdiscs changing? */
254static inline bool qdisc_tx_changing(struct net_device *dev)
255{
David S. Millere8a04642008-07-17 00:34:19 -0700256 unsigned int i;
257 for (i = 0; i < dev->num_tx_queues; i++) {
258 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
259 if (txq->qdisc != txq->qdisc_sleeping)
260 return true;
261 }
262 return false;
David S. Miller6fa98642008-07-08 23:01:06 -0700263}
264
David S. Millere8a04642008-07-17 00:34:19 -0700265/* Is the device using the noop qdisc on all queues? */
David S. Miller05297942008-07-08 23:01:27 -0700266static inline bool qdisc_tx_is_noop(const struct net_device *dev)
267{
David S. Millere8a04642008-07-17 00:34:19 -0700268 unsigned int i;
269 for (i = 0; i < dev->num_tx_queues; i++) {
270 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
271 if (txq->qdisc != &noop_qdisc)
272 return false;
273 }
274 return true;
David S. Miller05297942008-07-08 23:01:27 -0700275}
276
Thomas Graf9972b252005-06-18 22:57:26 -0700277static inline int __qdisc_enqueue_tail(struct sk_buff *skb, struct Qdisc *sch,
278 struct sk_buff_head *list)
279{
280 __skb_queue_tail(list, skb);
281 sch->qstats.backlog += skb->len;
282 sch->bstats.bytes += skb->len;
283 sch->bstats.packets++;
284
285 return NET_XMIT_SUCCESS;
286}
287
288static inline int qdisc_enqueue_tail(struct sk_buff *skb, struct Qdisc *sch)
289{
290 return __qdisc_enqueue_tail(skb, sch, &sch->q);
291}
292
293static inline struct sk_buff *__qdisc_dequeue_head(struct Qdisc *sch,
294 struct sk_buff_head *list)
295{
296 struct sk_buff *skb = __skb_dequeue(list);
297
298 if (likely(skb != NULL))
299 sch->qstats.backlog -= skb->len;
300
301 return skb;
302}
303
304static inline struct sk_buff *qdisc_dequeue_head(struct Qdisc *sch)
305{
306 return __qdisc_dequeue_head(sch, &sch->q);
307}
308
309static inline struct sk_buff *__qdisc_dequeue_tail(struct Qdisc *sch,
310 struct sk_buff_head *list)
311{
312 struct sk_buff *skb = __skb_dequeue_tail(list);
313
314 if (likely(skb != NULL))
315 sch->qstats.backlog -= skb->len;
316
317 return skb;
318}
319
320static inline struct sk_buff *qdisc_dequeue_tail(struct Qdisc *sch)
321{
322 return __qdisc_dequeue_tail(sch, &sch->q);
323}
324
325static inline int __qdisc_requeue(struct sk_buff *skb, struct Qdisc *sch,
326 struct sk_buff_head *list)
327{
328 __skb_queue_head(list, skb);
329 sch->qstats.backlog += skb->len;
330 sch->qstats.requeues++;
331
332 return NET_XMIT_SUCCESS;
333}
334
335static inline int qdisc_requeue(struct sk_buff *skb, struct Qdisc *sch)
336{
337 return __qdisc_requeue(skb, sch, &sch->q);
338}
339
340static inline void __qdisc_reset_queue(struct Qdisc *sch,
341 struct sk_buff_head *list)
342{
343 /*
344 * We do not know the backlog in bytes of this list, it
345 * is up to the caller to correct it
346 */
347 skb_queue_purge(list);
348}
349
350static inline void qdisc_reset_queue(struct Qdisc *sch)
351{
352 __qdisc_reset_queue(sch, &sch->q);
353 sch->qstats.backlog = 0;
354}
355
356static inline unsigned int __qdisc_queue_drop(struct Qdisc *sch,
357 struct sk_buff_head *list)
358{
359 struct sk_buff *skb = __qdisc_dequeue_tail(sch, list);
360
361 if (likely(skb != NULL)) {
362 unsigned int len = skb->len;
363 kfree_skb(skb);
364 return len;
365 }
366
367 return 0;
368}
369
370static inline unsigned int qdisc_queue_drop(struct Qdisc *sch)
371{
372 return __qdisc_queue_drop(sch, &sch->q);
373}
374
375static inline int qdisc_drop(struct sk_buff *skb, struct Qdisc *sch)
376{
377 kfree_skb(skb);
378 sch->qstats.drops++;
379
380 return NET_XMIT_DROP;
381}
382
383static inline int qdisc_reshape_fail(struct sk_buff *skb, struct Qdisc *sch)
384{
385 sch->qstats.drops++;
386
Patrick McHardyc3bc7cf2007-07-15 00:03:05 -0700387#ifdef CONFIG_NET_CLS_ACT
Thomas Graf9972b252005-06-18 22:57:26 -0700388 if (sch->reshape_fail == NULL || sch->reshape_fail(skb, sch))
389 goto drop;
390
391 return NET_XMIT_SUCCESS;
392
393drop:
394#endif
395 kfree_skb(skb);
396 return NET_XMIT_DROP;
397}
398
Jesper Dangaard Brouere9bef552007-09-12 16:35:24 +0200399/* Length to Time (L2T) lookup in a qdisc_rate_table, to determine how
400 long it will take to send a packet given its size.
401 */
402static inline u32 qdisc_l2t(struct qdisc_rate_table* rtab, unsigned int pktlen)
403{
Jesper Dangaard Brouere08b0992007-09-12 16:36:28 +0200404 int slot = pktlen + rtab->rate.cell_align + rtab->rate.overhead;
405 if (slot < 0)
406 slot = 0;
Jesper Dangaard Brouere9bef552007-09-12 16:35:24 +0200407 slot >>= rtab->rate.cell_log;
408 if (slot > 255)
409 return (rtab->data[255]*(slot >> 8) + rtab->data[slot & 0xFF]);
410 return rtab->data[slot];
411}
412
Jamal Hadi Salim12da81d2007-10-26 02:47:23 -0700413#ifdef CONFIG_NET_CLS_ACT
414static inline struct sk_buff *skb_act_clone(struct sk_buff *skb, gfp_t gfp_mask)
415{
416 struct sk_buff *n = skb_clone(skb, gfp_mask);
417
418 if (n) {
419 n->tc_verd = SET_TC_VERD(n->tc_verd, 0);
420 n->tc_verd = CLR_TC_OK2MUNGE(n->tc_verd);
421 n->tc_verd = CLR_TC_MUNGED(n->tc_verd);
Jamal Hadi Salim12da81d2007-10-26 02:47:23 -0700422 }
423 return n;
424}
425#endif
426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427#endif