blob: bf8f7264a778be4414d647797f73701ab7924f8c [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 list_head list;
42
43 struct gnet_stats_basic bstats;
44 struct gnet_stats_queue qstats;
45 struct gnet_stats_rate_est rate_est;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 struct rcu_head q_rcu;
47 int (*reshape_fail)(struct sk_buff *skb,
48 struct Qdisc *q);
49
50 /* This field is deprecated, but it is still used by CBQ
51 * and it will live until better solution will be invented.
52 */
53 struct Qdisc *__parent;
54};
55
56struct Qdisc_class_ops
57{
58 /* Child qdisc manipulation */
59 int (*graft)(struct Qdisc *, unsigned long cl,
60 struct Qdisc *, struct Qdisc **);
61 struct Qdisc * (*leaf)(struct Qdisc *, unsigned long cl);
Patrick McHardy43effa12006-11-29 17:35:48 -080062 void (*qlen_notify)(struct Qdisc *, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64 /* Class manipulation routines */
65 unsigned long (*get)(struct Qdisc *, u32 classid);
66 void (*put)(struct Qdisc *, unsigned long);
67 int (*change)(struct Qdisc *, u32, u32,
Patrick McHardy1e904742008-01-22 22:11:17 -080068 struct nlattr **, unsigned long *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 int (*delete)(struct Qdisc *, unsigned long);
70 void (*walk)(struct Qdisc *, struct qdisc_walker * arg);
71
72 /* Filter manipulation */
73 struct tcf_proto ** (*tcf_chain)(struct Qdisc *, unsigned long);
74 unsigned long (*bind_tcf)(struct Qdisc *, unsigned long,
75 u32 classid);
76 void (*unbind_tcf)(struct Qdisc *, unsigned long);
77
78 /* rtnetlink specific */
79 int (*dump)(struct Qdisc *, unsigned long,
80 struct sk_buff *skb, struct tcmsg*);
81 int (*dump_stats)(struct Qdisc *, unsigned long,
82 struct gnet_dump *);
83};
84
85struct Qdisc_ops
86{
87 struct Qdisc_ops *next;
Eric Dumazet20fea082007-11-14 01:44:41 -080088 const struct Qdisc_class_ops *cl_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 char id[IFNAMSIZ];
90 int priv_size;
91
92 int (*enqueue)(struct sk_buff *, struct Qdisc *);
93 struct sk_buff * (*dequeue)(struct Qdisc *);
94 int (*requeue)(struct sk_buff *, struct Qdisc *);
95 unsigned int (*drop)(struct Qdisc *);
96
Patrick McHardy1e904742008-01-22 22:11:17 -080097 int (*init)(struct Qdisc *, struct nlattr *arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 void (*reset)(struct Qdisc *);
99 void (*destroy)(struct Qdisc *);
Patrick McHardy1e904742008-01-22 22:11:17 -0800100 int (*change)(struct Qdisc *, struct nlattr *arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102 int (*dump)(struct Qdisc *, struct sk_buff *);
103 int (*dump_stats)(struct Qdisc *, struct gnet_dump *);
104
105 struct module *owner;
106};
107
108
109struct tcf_result
110{
111 unsigned long class;
112 u32 classid;
113};
114
115struct tcf_proto_ops
116{
117 struct tcf_proto_ops *next;
118 char kind[IFNAMSIZ];
119
120 int (*classify)(struct sk_buff*, struct tcf_proto*,
121 struct tcf_result *);
122 int (*init)(struct tcf_proto*);
123 void (*destroy)(struct tcf_proto*);
124
125 unsigned long (*get)(struct tcf_proto*, u32 handle);
126 void (*put)(struct tcf_proto*, unsigned long);
127 int (*change)(struct tcf_proto*, unsigned long,
Patrick McHardyadd93b62008-01-22 22:11:33 -0800128 u32 handle, struct nlattr **,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 unsigned long *);
130 int (*delete)(struct tcf_proto*, unsigned long);
131 void (*walk)(struct tcf_proto*, struct tcf_walker *arg);
132
133 /* rtnetlink specific */
134 int (*dump)(struct tcf_proto*, unsigned long,
135 struct sk_buff *skb, struct tcmsg*);
136
137 struct module *owner;
138};
139
140struct tcf_proto
141{
142 /* Fast access part */
143 struct tcf_proto *next;
144 void *root;
145 int (*classify)(struct sk_buff*, struct tcf_proto*,
146 struct tcf_result *);
Al Viro66c6f522006-11-20 18:07:51 -0800147 __be16 protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149 /* All the rest */
150 u32 prio;
151 u32 classid;
152 struct Qdisc *q;
153 void *data;
154 struct tcf_proto_ops *ops;
155};
156
David S. Miller5ce2d482008-07-08 17:06:30 -0700157static inline struct net_device *qdisc_dev(struct Qdisc *qdisc)
158{
159 return qdisc->dev_queue->dev;
160}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162extern void qdisc_lock_tree(struct net_device *dev);
163extern void qdisc_unlock_tree(struct net_device *dev);
164
David S. Miller5ce2d482008-07-08 17:06:30 -0700165#define sch_tree_lock(q) qdisc_lock_tree(qdisc_dev(q))
166#define sch_tree_unlock(q) qdisc_unlock_tree(qdisc_dev(q))
167#define tcf_tree_lock(tp) qdisc_lock_tree(qdisc_dev((tp)->q))
168#define tcf_tree_unlock(tp) qdisc_unlock_tree(qdisc_dev((tp)->q))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Thomas Grafe41a33e2005-07-05 14:14:30 -0700170extern struct Qdisc noop_qdisc;
171extern struct Qdisc_ops noop_qdisc_ops;
172
Patrick McHardy6fe1c7a2008-07-05 23:21:31 -0700173struct Qdisc_class_common
174{
175 u32 classid;
176 struct hlist_node hnode;
177};
178
179struct Qdisc_class_hash
180{
181 struct hlist_head *hash;
182 unsigned int hashsize;
183 unsigned int hashmask;
184 unsigned int hashelems;
185};
186
187static inline unsigned int qdisc_class_hash(u32 id, u32 mask)
188{
189 id ^= id >> 8;
190 id ^= id >> 4;
191 return id & mask;
192}
193
194static inline struct Qdisc_class_common *
195qdisc_class_find(struct Qdisc_class_hash *hash, u32 id)
196{
197 struct Qdisc_class_common *cl;
198 struct hlist_node *n;
199 unsigned int h;
200
201 h = qdisc_class_hash(id, hash->hashmask);
202 hlist_for_each_entry(cl, n, &hash->hash[h], hnode) {
203 if (cl->classid == id)
204 return cl;
205 }
206 return NULL;
207}
208
209extern int qdisc_class_hash_init(struct Qdisc_class_hash *);
210extern void qdisc_class_hash_insert(struct Qdisc_class_hash *, struct Qdisc_class_common *);
211extern void qdisc_class_hash_remove(struct Qdisc_class_hash *, struct Qdisc_class_common *);
212extern void qdisc_class_hash_grow(struct Qdisc *, struct Qdisc_class_hash *);
213extern void qdisc_class_hash_destroy(struct Qdisc_class_hash *);
214
Thomas Grafe41a33e2005-07-05 14:14:30 -0700215extern void dev_init_scheduler(struct net_device *dev);
216extern void dev_shutdown(struct net_device *dev);
217extern void dev_activate(struct net_device *dev);
218extern void dev_deactivate(struct net_device *dev);
219extern void qdisc_reset(struct Qdisc *qdisc);
220extern void qdisc_destroy(struct Qdisc *qdisc);
Patrick McHardy43effa12006-11-29 17:35:48 -0800221extern void qdisc_tree_decrease_qlen(struct Qdisc *qdisc, unsigned int n);
David S. Miller5ce2d482008-07-08 17:06:30 -0700222extern struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue,
David S. Millerbb949fb2008-07-08 16:55:56 -0700223 struct Qdisc_ops *ops);
Thomas Grafe41a33e2005-07-05 14:14:30 -0700224extern struct Qdisc *qdisc_create_dflt(struct net_device *dev,
David S. Millerbb949fb2008-07-08 16:55:56 -0700225 struct netdev_queue *dev_queue,
Patrick McHardy9f9afec2006-11-29 17:35:18 -0800226 struct Qdisc_ops *ops, u32 parentid);
Patrick McHardya48b5a62007-03-23 11:29:43 -0700227extern void tcf_destroy(struct tcf_proto *tp);
Patrick McHardyff31ab52008-07-01 19:52:38 -0700228extern void tcf_destroy_chain(struct tcf_proto **fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
David S. Miller5aa70992008-07-08 22:59:10 -0700230/* Reset all TX qdiscs of a device. */
231static inline void qdisc_reset_all_tx(struct net_device *dev)
232{
233 qdisc_reset(dev->tx_queue.qdisc);
234}
235
David S. Miller3e745dd2008-07-08 23:00:25 -0700236/* Are all TX queues of the device empty? */
237static inline bool qdisc_all_tx_empty(const struct net_device *dev)
238{
239 const struct netdev_queue *txq = &dev->tx_queue;
240 const struct Qdisc *q = txq->qdisc;
241
242 return (q->q.qlen == 0);
243}
244
David S. Miller6fa98642008-07-08 23:01:06 -0700245/* Are any of the TX qdiscs changing? */
246static inline bool qdisc_tx_changing(struct net_device *dev)
247{
248 struct netdev_queue *txq = &dev->tx_queue;
249
250 return (txq->qdisc != txq->qdisc_sleeping);
251}
252
Thomas Graf9972b252005-06-18 22:57:26 -0700253static inline int __qdisc_enqueue_tail(struct sk_buff *skb, struct Qdisc *sch,
254 struct sk_buff_head *list)
255{
256 __skb_queue_tail(list, skb);
257 sch->qstats.backlog += skb->len;
258 sch->bstats.bytes += skb->len;
259 sch->bstats.packets++;
260
261 return NET_XMIT_SUCCESS;
262}
263
264static inline int qdisc_enqueue_tail(struct sk_buff *skb, struct Qdisc *sch)
265{
266 return __qdisc_enqueue_tail(skb, sch, &sch->q);
267}
268
269static inline struct sk_buff *__qdisc_dequeue_head(struct Qdisc *sch,
270 struct sk_buff_head *list)
271{
272 struct sk_buff *skb = __skb_dequeue(list);
273
274 if (likely(skb != NULL))
275 sch->qstats.backlog -= skb->len;
276
277 return skb;
278}
279
280static inline struct sk_buff *qdisc_dequeue_head(struct Qdisc *sch)
281{
282 return __qdisc_dequeue_head(sch, &sch->q);
283}
284
285static inline struct sk_buff *__qdisc_dequeue_tail(struct Qdisc *sch,
286 struct sk_buff_head *list)
287{
288 struct sk_buff *skb = __skb_dequeue_tail(list);
289
290 if (likely(skb != NULL))
291 sch->qstats.backlog -= skb->len;
292
293 return skb;
294}
295
296static inline struct sk_buff *qdisc_dequeue_tail(struct Qdisc *sch)
297{
298 return __qdisc_dequeue_tail(sch, &sch->q);
299}
300
301static inline int __qdisc_requeue(struct sk_buff *skb, struct Qdisc *sch,
302 struct sk_buff_head *list)
303{
304 __skb_queue_head(list, skb);
305 sch->qstats.backlog += skb->len;
306 sch->qstats.requeues++;
307
308 return NET_XMIT_SUCCESS;
309}
310
311static inline int qdisc_requeue(struct sk_buff *skb, struct Qdisc *sch)
312{
313 return __qdisc_requeue(skb, sch, &sch->q);
314}
315
316static inline void __qdisc_reset_queue(struct Qdisc *sch,
317 struct sk_buff_head *list)
318{
319 /*
320 * We do not know the backlog in bytes of this list, it
321 * is up to the caller to correct it
322 */
323 skb_queue_purge(list);
324}
325
326static inline void qdisc_reset_queue(struct Qdisc *sch)
327{
328 __qdisc_reset_queue(sch, &sch->q);
329 sch->qstats.backlog = 0;
330}
331
332static inline unsigned int __qdisc_queue_drop(struct Qdisc *sch,
333 struct sk_buff_head *list)
334{
335 struct sk_buff *skb = __qdisc_dequeue_tail(sch, list);
336
337 if (likely(skb != NULL)) {
338 unsigned int len = skb->len;
339 kfree_skb(skb);
340 return len;
341 }
342
343 return 0;
344}
345
346static inline unsigned int qdisc_queue_drop(struct Qdisc *sch)
347{
348 return __qdisc_queue_drop(sch, &sch->q);
349}
350
351static inline int qdisc_drop(struct sk_buff *skb, struct Qdisc *sch)
352{
353 kfree_skb(skb);
354 sch->qstats.drops++;
355
356 return NET_XMIT_DROP;
357}
358
359static inline int qdisc_reshape_fail(struct sk_buff *skb, struct Qdisc *sch)
360{
361 sch->qstats.drops++;
362
Patrick McHardyc3bc7cf2007-07-15 00:03:05 -0700363#ifdef CONFIG_NET_CLS_ACT
Thomas Graf9972b252005-06-18 22:57:26 -0700364 if (sch->reshape_fail == NULL || sch->reshape_fail(skb, sch))
365 goto drop;
366
367 return NET_XMIT_SUCCESS;
368
369drop:
370#endif
371 kfree_skb(skb);
372 return NET_XMIT_DROP;
373}
374
Jesper Dangaard Brouere9bef552007-09-12 16:35:24 +0200375/* Length to Time (L2T) lookup in a qdisc_rate_table, to determine how
376 long it will take to send a packet given its size.
377 */
378static inline u32 qdisc_l2t(struct qdisc_rate_table* rtab, unsigned int pktlen)
379{
Jesper Dangaard Brouere08b0992007-09-12 16:36:28 +0200380 int slot = pktlen + rtab->rate.cell_align + rtab->rate.overhead;
381 if (slot < 0)
382 slot = 0;
Jesper Dangaard Brouere9bef552007-09-12 16:35:24 +0200383 slot >>= rtab->rate.cell_log;
384 if (slot > 255)
385 return (rtab->data[255]*(slot >> 8) + rtab->data[slot & 0xFF]);
386 return rtab->data[slot];
387}
388
Jamal Hadi Salim12da81d2007-10-26 02:47:23 -0700389#ifdef CONFIG_NET_CLS_ACT
390static inline struct sk_buff *skb_act_clone(struct sk_buff *skb, gfp_t gfp_mask)
391{
392 struct sk_buff *n = skb_clone(skb, gfp_mask);
393
394 if (n) {
395 n->tc_verd = SET_TC_VERD(n->tc_verd, 0);
396 n->tc_verd = CLR_TC_OK2MUNGE(n->tc_verd);
397 n->tc_verd = CLR_TC_MUNGED(n->tc_verd);
Jamal Hadi Salim12da81d2007-10-26 02:47:23 -0700398 }
399 return n;
400}
401#endif
402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403#endif