blob: b0e9108a4e18a93b57b899e0aee858069c9fffb2 [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>
8#include <linux/rtnetlink.h>
9#include <linux/pkt_sched.h>
10#include <linux/pkt_cls.h>
11#include <net/gen_stats.h>
12
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;
40 struct net_device *dev;
41 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;
46 spinlock_t *stats_lock;
47 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);
63
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,
68 struct rtattr **, unsigned long *);
69 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;
88 struct Qdisc_class_ops *cl_ops;
89 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
97 int (*init)(struct Qdisc *, struct rtattr *arg);
98 void (*reset)(struct Qdisc *);
99 void (*destroy)(struct Qdisc *);
100 int (*change)(struct Qdisc *, struct rtattr *arg);
101
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,
128 u32 handle, struct rtattr **,
129 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 *);
147 u32 protocol;
148
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
157
158extern void qdisc_lock_tree(struct net_device *dev);
159extern void qdisc_unlock_tree(struct net_device *dev);
160
161#define sch_tree_lock(q) qdisc_lock_tree((q)->dev)
162#define sch_tree_unlock(q) qdisc_unlock_tree((q)->dev)
163#define tcf_tree_lock(tp) qdisc_lock_tree((tp)->q->dev)
164#define tcf_tree_unlock(tp) qdisc_unlock_tree((tp)->q->dev)
165
Thomas Grafe41a33e2005-07-05 14:14:30 -0700166extern struct Qdisc noop_qdisc;
167extern struct Qdisc_ops noop_qdisc_ops;
168
169extern void dev_init_scheduler(struct net_device *dev);
170extern void dev_shutdown(struct net_device *dev);
171extern void dev_activate(struct net_device *dev);
172extern void dev_deactivate(struct net_device *dev);
173extern void qdisc_reset(struct Qdisc *qdisc);
174extern void qdisc_destroy(struct Qdisc *qdisc);
Thomas Graf3d54b822005-07-05 14:15:09 -0700175extern struct Qdisc *qdisc_alloc(struct net_device *dev, struct Qdisc_ops *ops);
Thomas Grafe41a33e2005-07-05 14:14:30 -0700176extern struct Qdisc *qdisc_create_dflt(struct net_device *dev,
177 struct Qdisc_ops *ops);
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179static inline void
180tcf_destroy(struct tcf_proto *tp)
181{
182 tp->ops->destroy(tp);
183 module_put(tp->ops->owner);
184 kfree(tp);
185}
186
Thomas Graf9972b252005-06-18 22:57:26 -0700187static inline int __qdisc_enqueue_tail(struct sk_buff *skb, struct Qdisc *sch,
188 struct sk_buff_head *list)
189{
190 __skb_queue_tail(list, skb);
191 sch->qstats.backlog += skb->len;
192 sch->bstats.bytes += skb->len;
193 sch->bstats.packets++;
194
195 return NET_XMIT_SUCCESS;
196}
197
198static inline int qdisc_enqueue_tail(struct sk_buff *skb, struct Qdisc *sch)
199{
200 return __qdisc_enqueue_tail(skb, sch, &sch->q);
201}
202
203static inline struct sk_buff *__qdisc_dequeue_head(struct Qdisc *sch,
204 struct sk_buff_head *list)
205{
206 struct sk_buff *skb = __skb_dequeue(list);
207
208 if (likely(skb != NULL))
209 sch->qstats.backlog -= skb->len;
210
211 return skb;
212}
213
214static inline struct sk_buff *qdisc_dequeue_head(struct Qdisc *sch)
215{
216 return __qdisc_dequeue_head(sch, &sch->q);
217}
218
219static inline struct sk_buff *__qdisc_dequeue_tail(struct Qdisc *sch,
220 struct sk_buff_head *list)
221{
222 struct sk_buff *skb = __skb_dequeue_tail(list);
223
224 if (likely(skb != NULL))
225 sch->qstats.backlog -= skb->len;
226
227 return skb;
228}
229
230static inline struct sk_buff *qdisc_dequeue_tail(struct Qdisc *sch)
231{
232 return __qdisc_dequeue_tail(sch, &sch->q);
233}
234
235static inline int __qdisc_requeue(struct sk_buff *skb, struct Qdisc *sch,
236 struct sk_buff_head *list)
237{
238 __skb_queue_head(list, skb);
239 sch->qstats.backlog += skb->len;
240 sch->qstats.requeues++;
241
242 return NET_XMIT_SUCCESS;
243}
244
245static inline int qdisc_requeue(struct sk_buff *skb, struct Qdisc *sch)
246{
247 return __qdisc_requeue(skb, sch, &sch->q);
248}
249
250static inline void __qdisc_reset_queue(struct Qdisc *sch,
251 struct sk_buff_head *list)
252{
253 /*
254 * We do not know the backlog in bytes of this list, it
255 * is up to the caller to correct it
256 */
257 skb_queue_purge(list);
258}
259
260static inline void qdisc_reset_queue(struct Qdisc *sch)
261{
262 __qdisc_reset_queue(sch, &sch->q);
263 sch->qstats.backlog = 0;
264}
265
266static inline unsigned int __qdisc_queue_drop(struct Qdisc *sch,
267 struct sk_buff_head *list)
268{
269 struct sk_buff *skb = __qdisc_dequeue_tail(sch, list);
270
271 if (likely(skb != NULL)) {
272 unsigned int len = skb->len;
273 kfree_skb(skb);
274 return len;
275 }
276
277 return 0;
278}
279
280static inline unsigned int qdisc_queue_drop(struct Qdisc *sch)
281{
282 return __qdisc_queue_drop(sch, &sch->q);
283}
284
285static inline int qdisc_drop(struct sk_buff *skb, struct Qdisc *sch)
286{
287 kfree_skb(skb);
288 sch->qstats.drops++;
289
290 return NET_XMIT_DROP;
291}
292
293static inline int qdisc_reshape_fail(struct sk_buff *skb, struct Qdisc *sch)
294{
295 sch->qstats.drops++;
296
297#ifdef CONFIG_NET_CLS_POLICE
298 if (sch->reshape_fail == NULL || sch->reshape_fail(skb, sch))
299 goto drop;
300
301 return NET_XMIT_SUCCESS;
302
303drop:
304#endif
305 kfree_skb(skb);
306 return NET_XMIT_DROP;
307}
308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309#endif