]> git.karo-electronics.de Git - karo-tx-linux.git/blob - include/net/sch_generic.h
netdev: Allocate multiple queues for TX.
[karo-tx-linux.git] / include / net / sch_generic.h
1 #ifndef __NET_SCHED_GENERIC_H
2 #define __NET_SCHED_GENERIC_H
3
4 #include <linux/netdevice.h>
5 #include <linux/types.h>
6 #include <linux/rcupdate.h>
7 #include <linux/module.h>
8 #include <linux/pkt_sched.h>
9 #include <linux/pkt_cls.h>
10 #include <net/gen_stats.h>
11 #include <net/rtnetlink.h>
12
13 struct Qdisc_ops;
14 struct qdisc_walker;
15 struct tcf_walker;
16 struct module;
17
18 struct qdisc_rate_table
19 {
20         struct tc_ratespec rate;
21         u32             data[256];
22         struct qdisc_rate_table *next;
23         int             refcnt;
24 };
25
26 struct 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 netdev_queue     *dev_queue;
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         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
56 struct 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);
62         void                    (*qlen_notify)(struct Qdisc *, unsigned long);
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 nlattr **, 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
85 struct Qdisc_ops
86 {
87         struct Qdisc_ops        *next;
88         const 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 nlattr *arg);
98         void                    (*reset)(struct Qdisc *);
99         void                    (*destroy)(struct Qdisc *);
100         int                     (*change)(struct Qdisc *, struct nlattr *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
109 struct tcf_result
110 {
111         unsigned long   class;
112         u32             classid;
113 };
114
115 struct 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 nlattr **,
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
140 struct 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         __be16                  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 static inline struct net_device *qdisc_dev(struct Qdisc *qdisc)
158 {
159         return qdisc->dev_queue->dev;
160 }
161
162 extern void qdisc_lock_tree(struct net_device *dev);
163 extern void qdisc_unlock_tree(struct net_device *dev);
164
165 #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))
169
170 extern struct Qdisc noop_qdisc;
171 extern struct Qdisc_ops noop_qdisc_ops;
172
173 struct Qdisc_class_common
174 {
175         u32                     classid;
176         struct hlist_node       hnode;
177 };
178
179 struct Qdisc_class_hash
180 {
181         struct hlist_head       *hash;
182         unsigned int            hashsize;
183         unsigned int            hashmask;
184         unsigned int            hashelems;
185 };
186
187 static 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
194 static inline struct Qdisc_class_common *
195 qdisc_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
209 extern int qdisc_class_hash_init(struct Qdisc_class_hash *);
210 extern void qdisc_class_hash_insert(struct Qdisc_class_hash *, struct Qdisc_class_common *);
211 extern void qdisc_class_hash_remove(struct Qdisc_class_hash *, struct Qdisc_class_common *);
212 extern void qdisc_class_hash_grow(struct Qdisc *, struct Qdisc_class_hash *);
213 extern void qdisc_class_hash_destroy(struct Qdisc_class_hash *);
214
215 extern void dev_init_scheduler(struct net_device *dev);
216 extern void dev_shutdown(struct net_device *dev);
217 extern void dev_activate(struct net_device *dev);
218 extern void dev_deactivate(struct net_device *dev);
219 extern void qdisc_reset(struct Qdisc *qdisc);
220 extern void qdisc_destroy(struct Qdisc *qdisc);
221 extern void qdisc_tree_decrease_qlen(struct Qdisc *qdisc, unsigned int n);
222 extern struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue,
223                                  struct Qdisc_ops *ops);
224 extern struct Qdisc *qdisc_create_dflt(struct net_device *dev,
225                                        struct netdev_queue *dev_queue,
226                                        struct Qdisc_ops *ops, u32 parentid);
227 extern void tcf_destroy(struct tcf_proto *tp);
228 extern void tcf_destroy_chain(struct tcf_proto **fl);
229
230 /* Reset all TX qdiscs of a device.  */
231 static inline void qdisc_reset_all_tx(struct net_device *dev)
232 {
233         unsigned int i;
234         for (i = 0; i < dev->num_tx_queues; i++)
235                 qdisc_reset(netdev_get_tx_queue(dev, i)->qdisc);
236 }
237
238 /* Are all TX queues of the device empty?  */
239 static inline bool qdisc_all_tx_empty(const struct net_device *dev)
240 {
241         unsigned int i;
242         for (i = 0; i < dev->num_tx_queues; i++) {
243                 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
244                 const struct Qdisc *q = txq->qdisc;
245
246                 if (q->q.qlen)
247                         return false;
248         }
249         return true;
250 }
251
252 /* Are any of the TX qdiscs changing?  */
253 static inline bool qdisc_tx_changing(struct net_device *dev)
254 {
255         unsigned int i;
256         for (i = 0; i < dev->num_tx_queues; i++) {
257                 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
258                 if (txq->qdisc != txq->qdisc_sleeping)
259                         return true;
260         }
261         return false;
262 }
263
264 /* Is the device using the noop qdisc on all queues?  */
265 static inline bool qdisc_tx_is_noop(const struct net_device *dev)
266 {
267         unsigned int i;
268         for (i = 0; i < dev->num_tx_queues; i++) {
269                 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
270                 if (txq->qdisc != &noop_qdisc)
271                         return false;
272         }
273         return true;
274 }
275
276 static inline int __qdisc_enqueue_tail(struct sk_buff *skb, struct Qdisc *sch,
277                                        struct sk_buff_head *list)
278 {
279         __skb_queue_tail(list, skb);
280         sch->qstats.backlog += skb->len;
281         sch->bstats.bytes += skb->len;
282         sch->bstats.packets++;
283
284         return NET_XMIT_SUCCESS;
285 }
286
287 static inline int qdisc_enqueue_tail(struct sk_buff *skb, struct Qdisc *sch)
288 {
289         return __qdisc_enqueue_tail(skb, sch, &sch->q);
290 }
291
292 static inline struct sk_buff *__qdisc_dequeue_head(struct Qdisc *sch,
293                                                    struct sk_buff_head *list)
294 {
295         struct sk_buff *skb = __skb_dequeue(list);
296
297         if (likely(skb != NULL))
298                 sch->qstats.backlog -= skb->len;
299
300         return skb;
301 }
302
303 static inline struct sk_buff *qdisc_dequeue_head(struct Qdisc *sch)
304 {
305         return __qdisc_dequeue_head(sch, &sch->q);
306 }
307
308 static inline struct sk_buff *__qdisc_dequeue_tail(struct Qdisc *sch,
309                                                    struct sk_buff_head *list)
310 {
311         struct sk_buff *skb = __skb_dequeue_tail(list);
312
313         if (likely(skb != NULL))
314                 sch->qstats.backlog -= skb->len;
315
316         return skb;
317 }
318
319 static inline struct sk_buff *qdisc_dequeue_tail(struct Qdisc *sch)
320 {
321         return __qdisc_dequeue_tail(sch, &sch->q);
322 }
323
324 static inline int __qdisc_requeue(struct sk_buff *skb, struct Qdisc *sch,
325                                   struct sk_buff_head *list)
326 {
327         __skb_queue_head(list, skb);
328         sch->qstats.backlog += skb->len;
329         sch->qstats.requeues++;
330
331         return NET_XMIT_SUCCESS;
332 }
333
334 static inline int qdisc_requeue(struct sk_buff *skb, struct Qdisc *sch)
335 {
336         return __qdisc_requeue(skb, sch, &sch->q);
337 }
338
339 static inline void __qdisc_reset_queue(struct Qdisc *sch,
340                                        struct sk_buff_head *list)
341 {
342         /*
343          * We do not know the backlog in bytes of this list, it
344          * is up to the caller to correct it
345          */
346         skb_queue_purge(list);
347 }
348
349 static inline void qdisc_reset_queue(struct Qdisc *sch)
350 {
351         __qdisc_reset_queue(sch, &sch->q);
352         sch->qstats.backlog = 0;
353 }
354
355 static inline unsigned int __qdisc_queue_drop(struct Qdisc *sch,
356                                               struct sk_buff_head *list)
357 {
358         struct sk_buff *skb = __qdisc_dequeue_tail(sch, list);
359
360         if (likely(skb != NULL)) {
361                 unsigned int len = skb->len;
362                 kfree_skb(skb);
363                 return len;
364         }
365
366         return 0;
367 }
368
369 static inline unsigned int qdisc_queue_drop(struct Qdisc *sch)
370 {
371         return __qdisc_queue_drop(sch, &sch->q);
372 }
373
374 static inline int qdisc_drop(struct sk_buff *skb, struct Qdisc *sch)
375 {
376         kfree_skb(skb);
377         sch->qstats.drops++;
378
379         return NET_XMIT_DROP;
380 }
381
382 static inline int qdisc_reshape_fail(struct sk_buff *skb, struct Qdisc *sch)
383 {
384         sch->qstats.drops++;
385
386 #ifdef CONFIG_NET_CLS_ACT
387         if (sch->reshape_fail == NULL || sch->reshape_fail(skb, sch))
388                 goto drop;
389
390         return NET_XMIT_SUCCESS;
391
392 drop:
393 #endif
394         kfree_skb(skb);
395         return NET_XMIT_DROP;
396 }
397
398 /* Length to Time (L2T) lookup in a qdisc_rate_table, to determine how
399    long it will take to send a packet given its size.
400  */
401 static inline u32 qdisc_l2t(struct qdisc_rate_table* rtab, unsigned int pktlen)
402 {
403         int slot = pktlen + rtab->rate.cell_align + rtab->rate.overhead;
404         if (slot < 0)
405                 slot = 0;
406         slot >>= rtab->rate.cell_log;
407         if (slot > 255)
408                 return (rtab->data[255]*(slot >> 8) + rtab->data[slot & 0xFF]);
409         return rtab->data[slot];
410 }
411
412 #ifdef CONFIG_NET_CLS_ACT
413 static inline struct sk_buff *skb_act_clone(struct sk_buff *skb, gfp_t gfp_mask)
414 {
415         struct sk_buff *n = skb_clone(skb, gfp_mask);
416
417         if (n) {
418                 n->tc_verd = SET_TC_VERD(n->tc_verd, 0);
419                 n->tc_verd = CLR_TC_OK2MUNGE(n->tc_verd);
420                 n->tc_verd = CLR_TC_MUNGED(n->tc_verd);
421         }
422         return n;
423 }
424 #endif
425
426 #endif