blob: 92417825d387d0bd539f8fbb8e32ad3759da4aba [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
David S. Millere2627c82008-07-16 00:56:32 -070026enum qdisc_state_t
27{
28 __QDISC_STATE_RUNNING,
29};
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031struct Qdisc
32{
33 int (*enqueue)(struct sk_buff *skb, struct Qdisc *dev);
34 struct sk_buff * (*dequeue)(struct Qdisc *dev);
35 unsigned flags;
36#define TCQ_F_BUILTIN 1
37#define TCQ_F_THROTTLED 2
38#define TCQ_F_INGRESS 4
39 int padded;
40 struct Qdisc_ops *ops;
41 u32 handle;
42 u32 parent;
43 atomic_t refcnt;
David S. Millere2627c82008-07-16 00:56:32 -070044 unsigned long state;
David S. Millerd3b753d2008-07-15 20:14:35 -070045 struct sk_buff *gso_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 struct sk_buff_head q;
David S. Millerbb949fb2008-07-08 16:55:56 -070047 struct netdev_queue *dev_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 struct list_head list;
49
50 struct gnet_stats_basic bstats;
51 struct gnet_stats_queue qstats;
52 struct gnet_stats_rate_est rate_est;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 struct rcu_head q_rcu;
54 int (*reshape_fail)(struct sk_buff *skb,
55 struct Qdisc *q);
56
57 /* This field is deprecated, but it is still used by CBQ
58 * and it will live until better solution will be invented.
59 */
60 struct Qdisc *__parent;
61};
62
63struct Qdisc_class_ops
64{
65 /* Child qdisc manipulation */
66 int (*graft)(struct Qdisc *, unsigned long cl,
67 struct Qdisc *, struct Qdisc **);
68 struct Qdisc * (*leaf)(struct Qdisc *, unsigned long cl);
Patrick McHardy43effa12006-11-29 17:35:48 -080069 void (*qlen_notify)(struct Qdisc *, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71 /* Class manipulation routines */
72 unsigned long (*get)(struct Qdisc *, u32 classid);
73 void (*put)(struct Qdisc *, unsigned long);
74 int (*change)(struct Qdisc *, u32, u32,
Patrick McHardy1e904742008-01-22 22:11:17 -080075 struct nlattr **, unsigned long *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 int (*delete)(struct Qdisc *, unsigned long);
77 void (*walk)(struct Qdisc *, struct qdisc_walker * arg);
78
79 /* Filter manipulation */
80 struct tcf_proto ** (*tcf_chain)(struct Qdisc *, unsigned long);
81 unsigned long (*bind_tcf)(struct Qdisc *, unsigned long,
82 u32 classid);
83 void (*unbind_tcf)(struct Qdisc *, unsigned long);
84
85 /* rtnetlink specific */
86 int (*dump)(struct Qdisc *, unsigned long,
87 struct sk_buff *skb, struct tcmsg*);
88 int (*dump_stats)(struct Qdisc *, unsigned long,
89 struct gnet_dump *);
90};
91
92struct Qdisc_ops
93{
94 struct Qdisc_ops *next;
Eric Dumazet20fea082007-11-14 01:44:41 -080095 const struct Qdisc_class_ops *cl_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 char id[IFNAMSIZ];
97 int priv_size;
98
99 int (*enqueue)(struct sk_buff *, struct Qdisc *);
100 struct sk_buff * (*dequeue)(struct Qdisc *);
101 int (*requeue)(struct sk_buff *, struct Qdisc *);
102 unsigned int (*drop)(struct Qdisc *);
103
Patrick McHardy1e904742008-01-22 22:11:17 -0800104 int (*init)(struct Qdisc *, struct nlattr *arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 void (*reset)(struct Qdisc *);
106 void (*destroy)(struct Qdisc *);
Patrick McHardy1e904742008-01-22 22:11:17 -0800107 int (*change)(struct Qdisc *, struct nlattr *arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109 int (*dump)(struct Qdisc *, struct sk_buff *);
110 int (*dump_stats)(struct Qdisc *, struct gnet_dump *);
111
112 struct module *owner;
113};
114
115
116struct tcf_result
117{
118 unsigned long class;
119 u32 classid;
120};
121
122struct tcf_proto_ops
123{
124 struct tcf_proto_ops *next;
125 char kind[IFNAMSIZ];
126
127 int (*classify)(struct sk_buff*, struct tcf_proto*,
128 struct tcf_result *);
129 int (*init)(struct tcf_proto*);
130 void (*destroy)(struct tcf_proto*);
131
132 unsigned long (*get)(struct tcf_proto*, u32 handle);
133 void (*put)(struct tcf_proto*, unsigned long);
134 int (*change)(struct tcf_proto*, unsigned long,
Patrick McHardyadd93b62008-01-22 22:11:33 -0800135 u32 handle, struct nlattr **,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 unsigned long *);
137 int (*delete)(struct tcf_proto*, unsigned long);
138 void (*walk)(struct tcf_proto*, struct tcf_walker *arg);
139
140 /* rtnetlink specific */
141 int (*dump)(struct tcf_proto*, unsigned long,
142 struct sk_buff *skb, struct tcmsg*);
143
144 struct module *owner;
145};
146
147struct tcf_proto
148{
149 /* Fast access part */
150 struct tcf_proto *next;
151 void *root;
152 int (*classify)(struct sk_buff*, struct tcf_proto*,
153 struct tcf_result *);
Al Viro66c6f522006-11-20 18:07:51 -0800154 __be16 protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 /* All the rest */
157 u32 prio;
158 u32 classid;
159 struct Qdisc *q;
160 void *data;
161 struct tcf_proto_ops *ops;
162};
163
David S. Miller7698b4f2008-07-16 01:42:40 -0700164static inline struct Qdisc *qdisc_root(struct Qdisc *qdisc)
165{
166 return qdisc->dev_queue->qdisc;
167}
168
169static inline spinlock_t *qdisc_root_lock(struct Qdisc *qdisc)
170{
171 struct Qdisc *root = qdisc_root(qdisc);
172
173 return &root->dev_queue->lock;
174}
175
David S. Miller5ce2d482008-07-08 17:06:30 -0700176static inline struct net_device *qdisc_dev(struct Qdisc *qdisc)
177{
178 return qdisc->dev_queue->dev;
179}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181extern void qdisc_lock_tree(struct net_device *dev);
182extern void qdisc_unlock_tree(struct net_device *dev);
183
David S. Miller5ce2d482008-07-08 17:06:30 -0700184#define sch_tree_lock(q) qdisc_lock_tree(qdisc_dev(q))
185#define sch_tree_unlock(q) qdisc_unlock_tree(qdisc_dev(q))
186#define tcf_tree_lock(tp) qdisc_lock_tree(qdisc_dev((tp)->q))
187#define tcf_tree_unlock(tp) qdisc_unlock_tree(qdisc_dev((tp)->q))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Thomas Grafe41a33e2005-07-05 14:14:30 -0700189extern struct Qdisc noop_qdisc;
190extern struct Qdisc_ops noop_qdisc_ops;
191
Patrick McHardy6fe1c7a2008-07-05 23:21:31 -0700192struct Qdisc_class_common
193{
194 u32 classid;
195 struct hlist_node hnode;
196};
197
198struct Qdisc_class_hash
199{
200 struct hlist_head *hash;
201 unsigned int hashsize;
202 unsigned int hashmask;
203 unsigned int hashelems;
204};
205
206static inline unsigned int qdisc_class_hash(u32 id, u32 mask)
207{
208 id ^= id >> 8;
209 id ^= id >> 4;
210 return id & mask;
211}
212
213static inline struct Qdisc_class_common *
214qdisc_class_find(struct Qdisc_class_hash *hash, u32 id)
215{
216 struct Qdisc_class_common *cl;
217 struct hlist_node *n;
218 unsigned int h;
219
220 h = qdisc_class_hash(id, hash->hashmask);
221 hlist_for_each_entry(cl, n, &hash->hash[h], hnode) {
222 if (cl->classid == id)
223 return cl;
224 }
225 return NULL;
226}
227
228extern int qdisc_class_hash_init(struct Qdisc_class_hash *);
229extern void qdisc_class_hash_insert(struct Qdisc_class_hash *, struct Qdisc_class_common *);
230extern void qdisc_class_hash_remove(struct Qdisc_class_hash *, struct Qdisc_class_common *);
231extern void qdisc_class_hash_grow(struct Qdisc *, struct Qdisc_class_hash *);
232extern void qdisc_class_hash_destroy(struct Qdisc_class_hash *);
233
Thomas Grafe41a33e2005-07-05 14:14:30 -0700234extern void dev_init_scheduler(struct net_device *dev);
235extern void dev_shutdown(struct net_device *dev);
236extern void dev_activate(struct net_device *dev);
237extern void dev_deactivate(struct net_device *dev);
238extern void qdisc_reset(struct Qdisc *qdisc);
239extern void qdisc_destroy(struct Qdisc *qdisc);
Patrick McHardy43effa12006-11-29 17:35:48 -0800240extern void qdisc_tree_decrease_qlen(struct Qdisc *qdisc, unsigned int n);
David S. Miller5ce2d482008-07-08 17:06:30 -0700241extern struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue,
David S. Millerbb949fb2008-07-08 16:55:56 -0700242 struct Qdisc_ops *ops);
Thomas Grafe41a33e2005-07-05 14:14:30 -0700243extern struct Qdisc *qdisc_create_dflt(struct net_device *dev,
David S. Millerbb949fb2008-07-08 16:55:56 -0700244 struct netdev_queue *dev_queue,
Patrick McHardy9f9afec2006-11-29 17:35:18 -0800245 struct Qdisc_ops *ops, u32 parentid);
Patrick McHardya48b5a62007-03-23 11:29:43 -0700246extern void tcf_destroy(struct tcf_proto *tp);
Patrick McHardyff31ab52008-07-01 19:52:38 -0700247extern void tcf_destroy_chain(struct tcf_proto **fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
David S. Miller5aa70992008-07-08 22:59:10 -0700249/* Reset all TX qdiscs of a device. */
250static inline void qdisc_reset_all_tx(struct net_device *dev)
251{
David S. Millere8a04642008-07-17 00:34:19 -0700252 unsigned int i;
253 for (i = 0; i < dev->num_tx_queues; i++)
254 qdisc_reset(netdev_get_tx_queue(dev, i)->qdisc);
David S. Miller5aa70992008-07-08 22:59:10 -0700255}
256
David S. Miller3e745dd2008-07-08 23:00:25 -0700257/* Are all TX queues of the device empty? */
258static inline bool qdisc_all_tx_empty(const struct net_device *dev)
259{
David S. Millere8a04642008-07-17 00:34:19 -0700260 unsigned int i;
261 for (i = 0; i < dev->num_tx_queues; i++) {
262 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
263 const struct Qdisc *q = txq->qdisc;
David S. Miller3e745dd2008-07-08 23:00:25 -0700264
David S. Millere8a04642008-07-17 00:34:19 -0700265 if (q->q.qlen)
266 return false;
267 }
268 return true;
David S. Miller3e745dd2008-07-08 23:00:25 -0700269}
270
David S. Miller6fa98642008-07-08 23:01:06 -0700271/* Are any of the TX qdiscs changing? */
272static inline bool qdisc_tx_changing(struct net_device *dev)
273{
David S. Millere8a04642008-07-17 00:34:19 -0700274 unsigned int i;
275 for (i = 0; i < dev->num_tx_queues; i++) {
276 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
277 if (txq->qdisc != txq->qdisc_sleeping)
278 return true;
279 }
280 return false;
David S. Miller6fa98642008-07-08 23:01:06 -0700281}
282
David S. Millere8a04642008-07-17 00:34:19 -0700283/* Is the device using the noop qdisc on all queues? */
David S. Miller05297942008-07-08 23:01:27 -0700284static inline bool qdisc_tx_is_noop(const struct net_device *dev)
285{
David S. Millere8a04642008-07-17 00:34:19 -0700286 unsigned int i;
287 for (i = 0; i < dev->num_tx_queues; i++) {
288 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
289 if (txq->qdisc != &noop_qdisc)
290 return false;
291 }
292 return true;
David S. Miller05297942008-07-08 23:01:27 -0700293}
294
Thomas Graf9972b252005-06-18 22:57:26 -0700295static inline int __qdisc_enqueue_tail(struct sk_buff *skb, struct Qdisc *sch,
296 struct sk_buff_head *list)
297{
298 __skb_queue_tail(list, skb);
299 sch->qstats.backlog += skb->len;
300 sch->bstats.bytes += skb->len;
301 sch->bstats.packets++;
302
303 return NET_XMIT_SUCCESS;
304}
305
306static inline int qdisc_enqueue_tail(struct sk_buff *skb, struct Qdisc *sch)
307{
308 return __qdisc_enqueue_tail(skb, sch, &sch->q);
309}
310
311static inline struct sk_buff *__qdisc_dequeue_head(struct Qdisc *sch,
312 struct sk_buff_head *list)
313{
314 struct sk_buff *skb = __skb_dequeue(list);
315
316 if (likely(skb != NULL))
317 sch->qstats.backlog -= skb->len;
318
319 return skb;
320}
321
322static inline struct sk_buff *qdisc_dequeue_head(struct Qdisc *sch)
323{
324 return __qdisc_dequeue_head(sch, &sch->q);
325}
326
327static inline struct sk_buff *__qdisc_dequeue_tail(struct Qdisc *sch,
328 struct sk_buff_head *list)
329{
330 struct sk_buff *skb = __skb_dequeue_tail(list);
331
332 if (likely(skb != NULL))
333 sch->qstats.backlog -= skb->len;
334
335 return skb;
336}
337
338static inline struct sk_buff *qdisc_dequeue_tail(struct Qdisc *sch)
339{
340 return __qdisc_dequeue_tail(sch, &sch->q);
341}
342
343static inline int __qdisc_requeue(struct sk_buff *skb, struct Qdisc *sch,
344 struct sk_buff_head *list)
345{
346 __skb_queue_head(list, skb);
347 sch->qstats.backlog += skb->len;
348 sch->qstats.requeues++;
349
350 return NET_XMIT_SUCCESS;
351}
352
353static inline int qdisc_requeue(struct sk_buff *skb, struct Qdisc *sch)
354{
355 return __qdisc_requeue(skb, sch, &sch->q);
356}
357
358static inline void __qdisc_reset_queue(struct Qdisc *sch,
359 struct sk_buff_head *list)
360{
361 /*
362 * We do not know the backlog in bytes of this list, it
363 * is up to the caller to correct it
364 */
365 skb_queue_purge(list);
366}
367
368static inline void qdisc_reset_queue(struct Qdisc *sch)
369{
370 __qdisc_reset_queue(sch, &sch->q);
371 sch->qstats.backlog = 0;
372}
373
374static inline unsigned int __qdisc_queue_drop(struct Qdisc *sch,
375 struct sk_buff_head *list)
376{
377 struct sk_buff *skb = __qdisc_dequeue_tail(sch, list);
378
379 if (likely(skb != NULL)) {
380 unsigned int len = skb->len;
381 kfree_skb(skb);
382 return len;
383 }
384
385 return 0;
386}
387
388static inline unsigned int qdisc_queue_drop(struct Qdisc *sch)
389{
390 return __qdisc_queue_drop(sch, &sch->q);
391}
392
393static inline int qdisc_drop(struct sk_buff *skb, struct Qdisc *sch)
394{
395 kfree_skb(skb);
396 sch->qstats.drops++;
397
398 return NET_XMIT_DROP;
399}
400
401static inline int qdisc_reshape_fail(struct sk_buff *skb, struct Qdisc *sch)
402{
403 sch->qstats.drops++;
404
Patrick McHardyc3bc7cf2007-07-15 00:03:05 -0700405#ifdef CONFIG_NET_CLS_ACT
Thomas Graf9972b252005-06-18 22:57:26 -0700406 if (sch->reshape_fail == NULL || sch->reshape_fail(skb, sch))
407 goto drop;
408
409 return NET_XMIT_SUCCESS;
410
411drop:
412#endif
413 kfree_skb(skb);
414 return NET_XMIT_DROP;
415}
416
Jesper Dangaard Brouere9bef552007-09-12 16:35:24 +0200417/* Length to Time (L2T) lookup in a qdisc_rate_table, to determine how
418 long it will take to send a packet given its size.
419 */
420static inline u32 qdisc_l2t(struct qdisc_rate_table* rtab, unsigned int pktlen)
421{
Jesper Dangaard Brouere08b0992007-09-12 16:36:28 +0200422 int slot = pktlen + rtab->rate.cell_align + rtab->rate.overhead;
423 if (slot < 0)
424 slot = 0;
Jesper Dangaard Brouere9bef552007-09-12 16:35:24 +0200425 slot >>= rtab->rate.cell_log;
426 if (slot > 255)
427 return (rtab->data[255]*(slot >> 8) + rtab->data[slot & 0xFF]);
428 return rtab->data[slot];
429}
430
Jamal Hadi Salim12da81d2007-10-26 02:47:23 -0700431#ifdef CONFIG_NET_CLS_ACT
432static inline struct sk_buff *skb_act_clone(struct sk_buff *skb, gfp_t gfp_mask)
433{
434 struct sk_buff *n = skb_clone(skb, gfp_mask);
435
436 if (n) {
437 n->tc_verd = SET_TC_VERD(n->tc_verd, 0);
438 n->tc_verd = CLR_TC_OK2MUNGE(n->tc_verd);
439 n->tc_verd = CLR_TC_MUNGED(n->tc_verd);
Jamal Hadi Salim12da81d2007-10-26 02:47:23 -0700440 }
441 return n;
442}
443#endif
444
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445#endif