blob: 60e1e9f9e453e4de31a9642f4b57081322a5c726 [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,
David S. Miller37437bb2008-07-16 02:15:04 -070029 __QDISC_STATE_SCHED,
David S. Millere2627c82008-07-16 00:56:32 -070030};
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032struct 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;
David S. Millere2627c82008-07-16 00:56:32 -070045 unsigned long state;
David S. Millerd3b753d2008-07-15 20:14:35 -070046 struct sk_buff *gso_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 struct sk_buff_head q;
David S. Millerbb949fb2008-07-08 16:55:56 -070048 struct netdev_queue *dev_queue;
David S. Miller37437bb2008-07-16 02:15:04 -070049 struct Qdisc *next_sched;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 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
65struct 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);
Patrick McHardy43effa12006-11-29 17:35:48 -080071 void (*qlen_notify)(struct Qdisc *, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
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,
Patrick McHardy1e904742008-01-22 22:11:17 -080077 struct nlattr **, unsigned long *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 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
94struct Qdisc_ops
95{
96 struct Qdisc_ops *next;
Eric Dumazet20fea082007-11-14 01:44:41 -080097 const struct Qdisc_class_ops *cl_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 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
Patrick McHardy1e904742008-01-22 22:11:17 -0800106 int (*init)(struct Qdisc *, struct nlattr *arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 void (*reset)(struct Qdisc *);
108 void (*destroy)(struct Qdisc *);
Patrick McHardy1e904742008-01-22 22:11:17 -0800109 int (*change)(struct Qdisc *, struct nlattr *arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
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
118struct tcf_result
119{
120 unsigned long class;
121 u32 classid;
122};
123
124struct 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,
Patrick McHardyadd93b62008-01-22 22:11:33 -0800137 u32 handle, struct nlattr **,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 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
149struct 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 *);
Al Viro66c6f522006-11-20 18:07:51 -0800156 __be16 protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
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
David S. Miller7698b4f2008-07-16 01:42:40 -0700166static inline struct Qdisc *qdisc_root(struct Qdisc *qdisc)
167{
168 return qdisc->dev_queue->qdisc;
169}
170
171static 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
David S. Miller5ce2d482008-07-08 17:06:30 -0700178static inline struct net_device *qdisc_dev(struct Qdisc *qdisc)
179{
180 return qdisc->dev_queue->dev;
181}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183extern void qdisc_lock_tree(struct net_device *dev);
184extern void qdisc_unlock_tree(struct net_device *dev);
185
David S. Miller78a5b302008-07-16 03:12:24 -0700186static inline void sch_tree_lock(struct Qdisc *q)
187{
188 spin_lock_bh(qdisc_root_lock(q));
189}
190
191static inline void sch_tree_unlock(struct Qdisc *q)
192{
193 spin_unlock_bh(qdisc_root_lock(q));
194}
195
196#define tcf_tree_lock(tp) sch_tree_lock((tp)->q)
197#define tcf_tree_unlock(tp) sch_tree_unlock((tp)->q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Thomas Grafe41a33e2005-07-05 14:14:30 -0700199extern struct Qdisc noop_qdisc;
200extern struct Qdisc_ops noop_qdisc_ops;
201
Patrick McHardy6fe1c7a2008-07-05 23:21:31 -0700202struct Qdisc_class_common
203{
204 u32 classid;
205 struct hlist_node hnode;
206};
207
208struct Qdisc_class_hash
209{
210 struct hlist_head *hash;
211 unsigned int hashsize;
212 unsigned int hashmask;
213 unsigned int hashelems;
214};
215
216static inline unsigned int qdisc_class_hash(u32 id, u32 mask)
217{
218 id ^= id >> 8;
219 id ^= id >> 4;
220 return id & mask;
221}
222
223static inline struct Qdisc_class_common *
224qdisc_class_find(struct Qdisc_class_hash *hash, u32 id)
225{
226 struct Qdisc_class_common *cl;
227 struct hlist_node *n;
228 unsigned int h;
229
230 h = qdisc_class_hash(id, hash->hashmask);
231 hlist_for_each_entry(cl, n, &hash->hash[h], hnode) {
232 if (cl->classid == id)
233 return cl;
234 }
235 return NULL;
236}
237
238extern int qdisc_class_hash_init(struct Qdisc_class_hash *);
239extern void qdisc_class_hash_insert(struct Qdisc_class_hash *, struct Qdisc_class_common *);
240extern void qdisc_class_hash_remove(struct Qdisc_class_hash *, struct Qdisc_class_common *);
241extern void qdisc_class_hash_grow(struct Qdisc *, struct Qdisc_class_hash *);
242extern void qdisc_class_hash_destroy(struct Qdisc_class_hash *);
243
Thomas Grafe41a33e2005-07-05 14:14:30 -0700244extern void dev_init_scheduler(struct net_device *dev);
245extern void dev_shutdown(struct net_device *dev);
246extern void dev_activate(struct net_device *dev);
247extern void dev_deactivate(struct net_device *dev);
248extern void qdisc_reset(struct Qdisc *qdisc);
249extern void qdisc_destroy(struct Qdisc *qdisc);
Patrick McHardy43effa12006-11-29 17:35:48 -0800250extern void qdisc_tree_decrease_qlen(struct Qdisc *qdisc, unsigned int n);
David S. Miller5ce2d482008-07-08 17:06:30 -0700251extern struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue,
David S. Millerbb949fb2008-07-08 16:55:56 -0700252 struct Qdisc_ops *ops);
Thomas Grafe41a33e2005-07-05 14:14:30 -0700253extern struct Qdisc *qdisc_create_dflt(struct net_device *dev,
David S. Millerbb949fb2008-07-08 16:55:56 -0700254 struct netdev_queue *dev_queue,
Patrick McHardy9f9afec2006-11-29 17:35:18 -0800255 struct Qdisc_ops *ops, u32 parentid);
Patrick McHardya48b5a62007-03-23 11:29:43 -0700256extern void tcf_destroy(struct tcf_proto *tp);
Patrick McHardyff31ab52008-07-01 19:52:38 -0700257extern void tcf_destroy_chain(struct tcf_proto **fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
David S. Miller5aa70992008-07-08 22:59:10 -0700259/* Reset all TX qdiscs of a device. */
260static inline void qdisc_reset_all_tx(struct net_device *dev)
261{
David S. Millere8a04642008-07-17 00:34:19 -0700262 unsigned int i;
263 for (i = 0; i < dev->num_tx_queues; i++)
264 qdisc_reset(netdev_get_tx_queue(dev, i)->qdisc);
David S. Miller5aa70992008-07-08 22:59:10 -0700265}
266
David S. Miller3e745dd2008-07-08 23:00:25 -0700267/* Are all TX queues of the device empty? */
268static inline bool qdisc_all_tx_empty(const struct net_device *dev)
269{
David S. Millere8a04642008-07-17 00:34:19 -0700270 unsigned int i;
271 for (i = 0; i < dev->num_tx_queues; i++) {
272 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
273 const struct Qdisc *q = txq->qdisc;
David S. Miller3e745dd2008-07-08 23:00:25 -0700274
David S. Millere8a04642008-07-17 00:34:19 -0700275 if (q->q.qlen)
276 return false;
277 }
278 return true;
David S. Miller3e745dd2008-07-08 23:00:25 -0700279}
280
David S. Miller6fa98642008-07-08 23:01:06 -0700281/* Are any of the TX qdiscs changing? */
282static inline bool qdisc_tx_changing(struct net_device *dev)
283{
David S. Millere8a04642008-07-17 00:34:19 -0700284 unsigned int i;
285 for (i = 0; i < dev->num_tx_queues; i++) {
286 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
287 if (txq->qdisc != txq->qdisc_sleeping)
288 return true;
289 }
290 return false;
David S. Miller6fa98642008-07-08 23:01:06 -0700291}
292
David S. Millere8a04642008-07-17 00:34:19 -0700293/* Is the device using the noop qdisc on all queues? */
David S. Miller05297942008-07-08 23:01:27 -0700294static inline bool qdisc_tx_is_noop(const struct net_device *dev)
295{
David S. Millere8a04642008-07-17 00:34:19 -0700296 unsigned int i;
297 for (i = 0; i < dev->num_tx_queues; i++) {
298 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
299 if (txq->qdisc != &noop_qdisc)
300 return false;
301 }
302 return true;
David S. Miller05297942008-07-08 23:01:27 -0700303}
304
Thomas Graf9972b252005-06-18 22:57:26 -0700305static inline int __qdisc_enqueue_tail(struct sk_buff *skb, struct Qdisc *sch,
306 struct sk_buff_head *list)
307{
308 __skb_queue_tail(list, skb);
309 sch->qstats.backlog += skb->len;
310 sch->bstats.bytes += skb->len;
311 sch->bstats.packets++;
312
313 return NET_XMIT_SUCCESS;
314}
315
316static inline int qdisc_enqueue_tail(struct sk_buff *skb, struct Qdisc *sch)
317{
318 return __qdisc_enqueue_tail(skb, sch, &sch->q);
319}
320
321static inline struct sk_buff *__qdisc_dequeue_head(struct Qdisc *sch,
322 struct sk_buff_head *list)
323{
324 struct sk_buff *skb = __skb_dequeue(list);
325
326 if (likely(skb != NULL))
327 sch->qstats.backlog -= skb->len;
328
329 return skb;
330}
331
332static inline struct sk_buff *qdisc_dequeue_head(struct Qdisc *sch)
333{
334 return __qdisc_dequeue_head(sch, &sch->q);
335}
336
337static inline struct sk_buff *__qdisc_dequeue_tail(struct Qdisc *sch,
338 struct sk_buff_head *list)
339{
340 struct sk_buff *skb = __skb_dequeue_tail(list);
341
342 if (likely(skb != NULL))
343 sch->qstats.backlog -= skb->len;
344
345 return skb;
346}
347
348static inline struct sk_buff *qdisc_dequeue_tail(struct Qdisc *sch)
349{
350 return __qdisc_dequeue_tail(sch, &sch->q);
351}
352
353static inline int __qdisc_requeue(struct sk_buff *skb, struct Qdisc *sch,
354 struct sk_buff_head *list)
355{
356 __skb_queue_head(list, skb);
357 sch->qstats.backlog += skb->len;
358 sch->qstats.requeues++;
359
360 return NET_XMIT_SUCCESS;
361}
362
363static inline int qdisc_requeue(struct sk_buff *skb, struct Qdisc *sch)
364{
365 return __qdisc_requeue(skb, sch, &sch->q);
366}
367
368static inline void __qdisc_reset_queue(struct Qdisc *sch,
369 struct sk_buff_head *list)
370{
371 /*
372 * We do not know the backlog in bytes of this list, it
373 * is up to the caller to correct it
374 */
375 skb_queue_purge(list);
376}
377
378static inline void qdisc_reset_queue(struct Qdisc *sch)
379{
380 __qdisc_reset_queue(sch, &sch->q);
381 sch->qstats.backlog = 0;
382}
383
384static inline unsigned int __qdisc_queue_drop(struct Qdisc *sch,
385 struct sk_buff_head *list)
386{
387 struct sk_buff *skb = __qdisc_dequeue_tail(sch, list);
388
389 if (likely(skb != NULL)) {
390 unsigned int len = skb->len;
391 kfree_skb(skb);
392 return len;
393 }
394
395 return 0;
396}
397
398static inline unsigned int qdisc_queue_drop(struct Qdisc *sch)
399{
400 return __qdisc_queue_drop(sch, &sch->q);
401}
402
403static inline int qdisc_drop(struct sk_buff *skb, struct Qdisc *sch)
404{
405 kfree_skb(skb);
406 sch->qstats.drops++;
407
408 return NET_XMIT_DROP;
409}
410
411static inline int qdisc_reshape_fail(struct sk_buff *skb, struct Qdisc *sch)
412{
413 sch->qstats.drops++;
414
Patrick McHardyc3bc7cf2007-07-15 00:03:05 -0700415#ifdef CONFIG_NET_CLS_ACT
Thomas Graf9972b252005-06-18 22:57:26 -0700416 if (sch->reshape_fail == NULL || sch->reshape_fail(skb, sch))
417 goto drop;
418
419 return NET_XMIT_SUCCESS;
420
421drop:
422#endif
423 kfree_skb(skb);
424 return NET_XMIT_DROP;
425}
426
Jesper Dangaard Brouere9bef552007-09-12 16:35:24 +0200427/* Length to Time (L2T) lookup in a qdisc_rate_table, to determine how
428 long it will take to send a packet given its size.
429 */
430static inline u32 qdisc_l2t(struct qdisc_rate_table* rtab, unsigned int pktlen)
431{
Jesper Dangaard Brouere08b0992007-09-12 16:36:28 +0200432 int slot = pktlen + rtab->rate.cell_align + rtab->rate.overhead;
433 if (slot < 0)
434 slot = 0;
Jesper Dangaard Brouere9bef552007-09-12 16:35:24 +0200435 slot >>= rtab->rate.cell_log;
436 if (slot > 255)
437 return (rtab->data[255]*(slot >> 8) + rtab->data[slot & 0xFF]);
438 return rtab->data[slot];
439}
440
Jamal Hadi Salim12da81d2007-10-26 02:47:23 -0700441#ifdef CONFIG_NET_CLS_ACT
442static inline struct sk_buff *skb_act_clone(struct sk_buff *skb, gfp_t gfp_mask)
443{
444 struct sk_buff *n = skb_clone(skb, gfp_mask);
445
446 if (n) {
447 n->tc_verd = SET_TC_VERD(n->tc_verd, 0);
448 n->tc_verd = CLR_TC_OK2MUNGE(n->tc_verd);
449 n->tc_verd = CLR_TC_MUNGED(n->tc_verd);
Jamal Hadi Salim12da81d2007-10-26 02:47:23 -0700450 }
451 return n;
452}
453#endif
454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455#endif