1 #ifndef __NET_SCHED_GENERIC_H
2 #define __NET_SCHED_GENERIC_H
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>
18 struct qdisc_rate_table
20 struct tc_ratespec rate;
22 struct qdisc_rate_table *next;
28 __QDISC_STATE_RUNNING,
34 int (*enqueue)(struct sk_buff *skb, struct Qdisc *dev);
35 struct sk_buff * (*dequeue)(struct Qdisc *dev);
37 #define TCQ_F_BUILTIN 1
38 #define TCQ_F_THROTTLED 2
39 #define TCQ_F_INGRESS 4
41 struct Qdisc_ops *ops;
46 struct sk_buff *gso_skb;
47 struct sk_buff_head q;
48 struct netdev_queue *dev_queue;
49 struct Qdisc *next_sched;
50 struct list_head list;
52 struct gnet_stats_basic bstats;
53 struct gnet_stats_queue qstats;
54 struct gnet_stats_rate_est rate_est;
55 struct rcu_head q_rcu;
56 int (*reshape_fail)(struct sk_buff *skb,
59 /* This field is deprecated, but it is still used by CBQ
60 * and it will live until better solution will be invented.
62 struct Qdisc *__parent;
65 struct Qdisc_class_ops
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);
71 void (*qlen_notify)(struct Qdisc *, unsigned long);
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,
77 struct nlattr **, unsigned long *);
78 int (*delete)(struct Qdisc *, unsigned long);
79 void (*walk)(struct Qdisc *, struct qdisc_walker * arg);
81 /* Filter manipulation */
82 struct tcf_proto ** (*tcf_chain)(struct Qdisc *, unsigned long);
83 unsigned long (*bind_tcf)(struct Qdisc *, unsigned long,
85 void (*unbind_tcf)(struct Qdisc *, unsigned long);
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,
96 struct Qdisc_ops *next;
97 const struct Qdisc_class_ops *cl_ops;
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 *);
106 int (*init)(struct Qdisc *, struct nlattr *arg);
107 void (*reset)(struct Qdisc *);
108 void (*destroy)(struct Qdisc *);
109 int (*change)(struct Qdisc *, struct nlattr *arg);
111 int (*dump)(struct Qdisc *, struct sk_buff *);
112 int (*dump_stats)(struct Qdisc *, struct gnet_dump *);
114 struct module *owner;
126 struct tcf_proto_ops *next;
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*);
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,
137 u32 handle, struct nlattr **,
139 int (*delete)(struct tcf_proto*, unsigned long);
140 void (*walk)(struct tcf_proto*, struct tcf_walker *arg);
142 /* rtnetlink specific */
143 int (*dump)(struct tcf_proto*, unsigned long,
144 struct sk_buff *skb, struct tcmsg*);
146 struct module *owner;
151 /* Fast access part */
152 struct tcf_proto *next;
154 int (*classify)(struct sk_buff*, struct tcf_proto*,
155 struct tcf_result *);
163 struct tcf_proto_ops *ops;
166 static inline struct Qdisc *qdisc_root(struct Qdisc *qdisc)
168 return qdisc->dev_queue->qdisc;
171 static inline spinlock_t *qdisc_root_lock(struct Qdisc *qdisc)
173 struct Qdisc *root = qdisc_root(qdisc);
175 return &root->dev_queue->lock;
178 static inline struct net_device *qdisc_dev(struct Qdisc *qdisc)
180 return qdisc->dev_queue->dev;
183 extern void qdisc_lock_tree(struct net_device *dev);
184 extern void qdisc_unlock_tree(struct net_device *dev);
186 static inline void sch_tree_lock(struct Qdisc *q)
188 spin_lock_bh(qdisc_root_lock(q));
191 static inline void sch_tree_unlock(struct Qdisc *q)
193 spin_unlock_bh(qdisc_root_lock(q));
196 #define tcf_tree_lock(tp) sch_tree_lock((tp)->q)
197 #define tcf_tree_unlock(tp) sch_tree_unlock((tp)->q)
199 extern struct Qdisc noop_qdisc;
200 extern struct Qdisc_ops noop_qdisc_ops;
202 struct Qdisc_class_common
205 struct hlist_node hnode;
208 struct Qdisc_class_hash
210 struct hlist_head *hash;
211 unsigned int hashsize;
212 unsigned int hashmask;
213 unsigned int hashelems;
216 static inline unsigned int qdisc_class_hash(u32 id, u32 mask)
223 static inline struct Qdisc_class_common *
224 qdisc_class_find(struct Qdisc_class_hash *hash, u32 id)
226 struct Qdisc_class_common *cl;
227 struct hlist_node *n;
230 h = qdisc_class_hash(id, hash->hashmask);
231 hlist_for_each_entry(cl, n, &hash->hash[h], hnode) {
232 if (cl->classid == id)
238 extern int qdisc_class_hash_init(struct Qdisc_class_hash *);
239 extern void qdisc_class_hash_insert(struct Qdisc_class_hash *, struct Qdisc_class_common *);
240 extern void qdisc_class_hash_remove(struct Qdisc_class_hash *, struct Qdisc_class_common *);
241 extern void qdisc_class_hash_grow(struct Qdisc *, struct Qdisc_class_hash *);
242 extern void qdisc_class_hash_destroy(struct Qdisc_class_hash *);
244 extern void dev_init_scheduler(struct net_device *dev);
245 extern void dev_shutdown(struct net_device *dev);
246 extern void dev_activate(struct net_device *dev);
247 extern void dev_deactivate(struct net_device *dev);
248 extern void qdisc_reset(struct Qdisc *qdisc);
249 extern void qdisc_destroy(struct Qdisc *qdisc);
250 extern void qdisc_tree_decrease_qlen(struct Qdisc *qdisc, unsigned int n);
251 extern struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue,
252 struct Qdisc_ops *ops);
253 extern struct Qdisc *qdisc_create_dflt(struct net_device *dev,
254 struct netdev_queue *dev_queue,
255 struct Qdisc_ops *ops, u32 parentid);
256 extern void tcf_destroy(struct tcf_proto *tp);
257 extern void tcf_destroy_chain(struct tcf_proto **fl);
259 /* Reset all TX qdiscs of a device. */
260 static inline void qdisc_reset_all_tx(struct net_device *dev)
263 for (i = 0; i < dev->num_tx_queues; i++)
264 qdisc_reset(netdev_get_tx_queue(dev, i)->qdisc);
267 /* Are all TX queues of the device empty? */
268 static inline bool qdisc_all_tx_empty(const struct net_device *dev)
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;
281 /* Are any of the TX qdiscs changing? */
282 static inline bool qdisc_tx_changing(struct net_device *dev)
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)
293 /* Is the device using the noop qdisc on all queues? */
294 static inline bool qdisc_tx_is_noop(const struct net_device *dev)
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)
305 static inline int __qdisc_enqueue_tail(struct sk_buff *skb, struct Qdisc *sch,
306 struct sk_buff_head *list)
308 __skb_queue_tail(list, skb);
309 sch->qstats.backlog += skb->len;
310 sch->bstats.bytes += skb->len;
311 sch->bstats.packets++;
313 return NET_XMIT_SUCCESS;
316 static inline int qdisc_enqueue_tail(struct sk_buff *skb, struct Qdisc *sch)
318 return __qdisc_enqueue_tail(skb, sch, &sch->q);
321 static inline struct sk_buff *__qdisc_dequeue_head(struct Qdisc *sch,
322 struct sk_buff_head *list)
324 struct sk_buff *skb = __skb_dequeue(list);
326 if (likely(skb != NULL))
327 sch->qstats.backlog -= skb->len;
332 static inline struct sk_buff *qdisc_dequeue_head(struct Qdisc *sch)
334 return __qdisc_dequeue_head(sch, &sch->q);
337 static inline struct sk_buff *__qdisc_dequeue_tail(struct Qdisc *sch,
338 struct sk_buff_head *list)
340 struct sk_buff *skb = __skb_dequeue_tail(list);
342 if (likely(skb != NULL))
343 sch->qstats.backlog -= skb->len;
348 static inline struct sk_buff *qdisc_dequeue_tail(struct Qdisc *sch)
350 return __qdisc_dequeue_tail(sch, &sch->q);
353 static inline int __qdisc_requeue(struct sk_buff *skb, struct Qdisc *sch,
354 struct sk_buff_head *list)
356 __skb_queue_head(list, skb);
357 sch->qstats.backlog += skb->len;
358 sch->qstats.requeues++;
360 return NET_XMIT_SUCCESS;
363 static inline int qdisc_requeue(struct sk_buff *skb, struct Qdisc *sch)
365 return __qdisc_requeue(skb, sch, &sch->q);
368 static inline void __qdisc_reset_queue(struct Qdisc *sch,
369 struct sk_buff_head *list)
372 * We do not know the backlog in bytes of this list, it
373 * is up to the caller to correct it
375 skb_queue_purge(list);
378 static inline void qdisc_reset_queue(struct Qdisc *sch)
380 __qdisc_reset_queue(sch, &sch->q);
381 sch->qstats.backlog = 0;
384 static inline unsigned int __qdisc_queue_drop(struct Qdisc *sch,
385 struct sk_buff_head *list)
387 struct sk_buff *skb = __qdisc_dequeue_tail(sch, list);
389 if (likely(skb != NULL)) {
390 unsigned int len = skb->len;
398 static inline unsigned int qdisc_queue_drop(struct Qdisc *sch)
400 return __qdisc_queue_drop(sch, &sch->q);
403 static inline int qdisc_drop(struct sk_buff *skb, struct Qdisc *sch)
408 return NET_XMIT_DROP;
411 static inline int qdisc_reshape_fail(struct sk_buff *skb, struct Qdisc *sch)
415 #ifdef CONFIG_NET_CLS_ACT
416 if (sch->reshape_fail == NULL || sch->reshape_fail(skb, sch))
419 return NET_XMIT_SUCCESS;
424 return NET_XMIT_DROP;
427 /* 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.
430 static inline u32 qdisc_l2t(struct qdisc_rate_table* rtab, unsigned int pktlen)
432 int slot = pktlen + rtab->rate.cell_align + rtab->rate.overhead;
435 slot >>= rtab->rate.cell_log;
437 return (rtab->data[255]*(slot >> 8) + rtab->data[slot & 0xFF]);
438 return rtab->data[slot];
441 #ifdef CONFIG_NET_CLS_ACT
442 static inline struct sk_buff *skb_act_clone(struct sk_buff *skb, gfp_t gfp_mask)
444 struct sk_buff *n = skb_clone(skb, gfp_mask);
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);