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,
61 /* This field is deprecated, but it is still used by CBQ
62 * and it will live until better solution will be invented.
64 struct Qdisc *__parent;
67 struct Qdisc_class_ops
69 /* Child qdisc manipulation */
70 int (*graft)(struct Qdisc *, unsigned long cl,
71 struct Qdisc *, struct Qdisc **);
72 struct Qdisc * (*leaf)(struct Qdisc *, unsigned long cl);
73 void (*qlen_notify)(struct Qdisc *, unsigned long);
75 /* Class manipulation routines */
76 unsigned long (*get)(struct Qdisc *, u32 classid);
77 void (*put)(struct Qdisc *, unsigned long);
78 int (*change)(struct Qdisc *, u32, u32,
79 struct nlattr **, unsigned long *);
80 int (*delete)(struct Qdisc *, unsigned long);
81 void (*walk)(struct Qdisc *, struct qdisc_walker * arg);
83 /* Filter manipulation */
84 struct tcf_proto ** (*tcf_chain)(struct Qdisc *, unsigned long);
85 unsigned long (*bind_tcf)(struct Qdisc *, unsigned long,
87 void (*unbind_tcf)(struct Qdisc *, unsigned long);
89 /* rtnetlink specific */
90 int (*dump)(struct Qdisc *, unsigned long,
91 struct sk_buff *skb, struct tcmsg*);
92 int (*dump_stats)(struct Qdisc *, unsigned long,
98 struct Qdisc_ops *next;
99 const struct Qdisc_class_ops *cl_ops;
103 int (*enqueue)(struct sk_buff *, struct Qdisc *);
104 struct sk_buff * (*dequeue)(struct Qdisc *);
105 int (*requeue)(struct sk_buff *, struct Qdisc *);
106 unsigned int (*drop)(struct Qdisc *);
108 int (*init)(struct Qdisc *, struct nlattr *arg);
109 void (*reset)(struct Qdisc *);
110 void (*destroy)(struct Qdisc *);
111 int (*change)(struct Qdisc *, struct nlattr *arg);
113 int (*dump)(struct Qdisc *, struct sk_buff *);
114 int (*dump_stats)(struct Qdisc *, struct gnet_dump *);
116 struct module *owner;
128 struct tcf_proto_ops *next;
131 int (*classify)(struct sk_buff*, struct tcf_proto*,
132 struct tcf_result *);
133 int (*init)(struct tcf_proto*);
134 void (*destroy)(struct tcf_proto*);
136 unsigned long (*get)(struct tcf_proto*, u32 handle);
137 void (*put)(struct tcf_proto*, unsigned long);
138 int (*change)(struct tcf_proto*, unsigned long,
139 u32 handle, struct nlattr **,
141 int (*delete)(struct tcf_proto*, unsigned long);
142 void (*walk)(struct tcf_proto*, struct tcf_walker *arg);
144 /* rtnetlink specific */
145 int (*dump)(struct tcf_proto*, unsigned long,
146 struct sk_buff *skb, struct tcmsg*);
148 struct module *owner;
153 /* Fast access part */
154 struct tcf_proto *next;
156 int (*classify)(struct sk_buff*, struct tcf_proto*,
157 struct tcf_result *);
165 struct tcf_proto_ops *ops;
168 static inline spinlock_t *qdisc_lock(struct Qdisc *qdisc)
170 return &qdisc->q.lock;
173 static inline struct Qdisc *qdisc_root(struct Qdisc *qdisc)
175 return qdisc->dev_queue->qdisc;
178 static inline spinlock_t *qdisc_root_lock(struct Qdisc *qdisc)
180 struct Qdisc *root = qdisc_root(qdisc);
182 return qdisc_lock(root);
185 static inline struct net_device *qdisc_dev(struct Qdisc *qdisc)
187 return qdisc->dev_queue->dev;
190 static inline void sch_tree_lock(struct Qdisc *q)
192 spin_lock_bh(qdisc_root_lock(q));
195 static inline void sch_tree_unlock(struct Qdisc *q)
197 spin_unlock_bh(qdisc_root_lock(q));
200 #define tcf_tree_lock(tp) sch_tree_lock((tp)->q)
201 #define tcf_tree_unlock(tp) sch_tree_unlock((tp)->q)
203 extern struct Qdisc noop_qdisc;
204 extern struct Qdisc_ops noop_qdisc_ops;
206 struct Qdisc_class_common
209 struct hlist_node hnode;
212 struct Qdisc_class_hash
214 struct hlist_head *hash;
215 unsigned int hashsize;
216 unsigned int hashmask;
217 unsigned int hashelems;
220 static inline unsigned int qdisc_class_hash(u32 id, u32 mask)
227 static inline struct Qdisc_class_common *
228 qdisc_class_find(struct Qdisc_class_hash *hash, u32 id)
230 struct Qdisc_class_common *cl;
231 struct hlist_node *n;
234 h = qdisc_class_hash(id, hash->hashmask);
235 hlist_for_each_entry(cl, n, &hash->hash[h], hnode) {
236 if (cl->classid == id)
242 extern int qdisc_class_hash_init(struct Qdisc_class_hash *);
243 extern void qdisc_class_hash_insert(struct Qdisc_class_hash *, struct Qdisc_class_common *);
244 extern void qdisc_class_hash_remove(struct Qdisc_class_hash *, struct Qdisc_class_common *);
245 extern void qdisc_class_hash_grow(struct Qdisc *, struct Qdisc_class_hash *);
246 extern void qdisc_class_hash_destroy(struct Qdisc_class_hash *);
248 extern void dev_init_scheduler(struct net_device *dev);
249 extern void dev_shutdown(struct net_device *dev);
250 extern void dev_activate(struct net_device *dev);
251 extern void dev_deactivate(struct net_device *dev);
252 extern void qdisc_reset(struct Qdisc *qdisc);
253 extern void qdisc_destroy(struct Qdisc *qdisc);
254 extern void qdisc_tree_decrease_qlen(struct Qdisc *qdisc, unsigned int n);
255 extern struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue,
256 struct Qdisc_ops *ops);
257 extern struct Qdisc *qdisc_create_dflt(struct net_device *dev,
258 struct netdev_queue *dev_queue,
259 struct Qdisc_ops *ops, u32 parentid);
260 extern void tcf_destroy(struct tcf_proto *tp);
261 extern void tcf_destroy_chain(struct tcf_proto **fl);
263 /* Reset all TX qdiscs of a device. */
264 static inline void qdisc_reset_all_tx(struct net_device *dev)
267 for (i = 0; i < dev->num_tx_queues; i++)
268 qdisc_reset(netdev_get_tx_queue(dev, i)->qdisc);
271 /* Are all TX queues of the device empty? */
272 static inline bool qdisc_all_tx_empty(const struct net_device *dev)
275 for (i = 0; i < dev->num_tx_queues; i++) {
276 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
277 const struct Qdisc *q = txq->qdisc;
285 /* Are any of the TX qdiscs changing? */
286 static inline bool qdisc_tx_changing(struct net_device *dev)
289 for (i = 0; i < dev->num_tx_queues; i++) {
290 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
291 if (txq->qdisc != txq->qdisc_sleeping)
297 /* Is the device using the noop qdisc on all queues? */
298 static inline bool qdisc_tx_is_noop(const struct net_device *dev)
301 for (i = 0; i < dev->num_tx_queues; i++) {
302 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
303 if (txq->qdisc != &noop_qdisc)
309 static inline int __qdisc_enqueue_tail(struct sk_buff *skb, struct Qdisc *sch,
310 struct sk_buff_head *list)
312 __skb_queue_tail(list, skb);
313 sch->qstats.backlog += skb->len;
314 sch->bstats.bytes += skb->len;
315 sch->bstats.packets++;
317 return NET_XMIT_SUCCESS;
320 static inline int qdisc_enqueue_tail(struct sk_buff *skb, struct Qdisc *sch)
322 return __qdisc_enqueue_tail(skb, sch, &sch->q);
325 static inline struct sk_buff *__qdisc_dequeue_head(struct Qdisc *sch,
326 struct sk_buff_head *list)
328 struct sk_buff *skb = __skb_dequeue(list);
330 if (likely(skb != NULL))
331 sch->qstats.backlog -= skb->len;
336 static inline struct sk_buff *qdisc_dequeue_head(struct Qdisc *sch)
338 return __qdisc_dequeue_head(sch, &sch->q);
341 static inline struct sk_buff *__qdisc_dequeue_tail(struct Qdisc *sch,
342 struct sk_buff_head *list)
344 struct sk_buff *skb = __skb_dequeue_tail(list);
346 if (likely(skb != NULL))
347 sch->qstats.backlog -= skb->len;
352 static inline struct sk_buff *qdisc_dequeue_tail(struct Qdisc *sch)
354 return __qdisc_dequeue_tail(sch, &sch->q);
357 static inline int __qdisc_requeue(struct sk_buff *skb, struct Qdisc *sch,
358 struct sk_buff_head *list)
360 __skb_queue_head(list, skb);
361 sch->qstats.backlog += skb->len;
362 sch->qstats.requeues++;
364 return NET_XMIT_SUCCESS;
367 static inline int qdisc_requeue(struct sk_buff *skb, struct Qdisc *sch)
369 return __qdisc_requeue(skb, sch, &sch->q);
372 static inline void __qdisc_reset_queue(struct Qdisc *sch,
373 struct sk_buff_head *list)
376 * We do not know the backlog in bytes of this list, it
377 * is up to the caller to correct it
379 __skb_queue_purge(list);
382 static inline void qdisc_reset_queue(struct Qdisc *sch)
384 __qdisc_reset_queue(sch, &sch->q);
385 sch->qstats.backlog = 0;
388 static inline unsigned int __qdisc_queue_drop(struct Qdisc *sch,
389 struct sk_buff_head *list)
391 struct sk_buff *skb = __qdisc_dequeue_tail(sch, list);
393 if (likely(skb != NULL)) {
394 unsigned int len = skb->len;
402 static inline unsigned int qdisc_queue_drop(struct Qdisc *sch)
404 return __qdisc_queue_drop(sch, &sch->q);
407 static inline int qdisc_drop(struct sk_buff *skb, struct Qdisc *sch)
412 return NET_XMIT_DROP;
415 static inline int qdisc_reshape_fail(struct sk_buff *skb, struct Qdisc *sch)
419 #ifdef CONFIG_NET_CLS_ACT
420 if (sch->reshape_fail == NULL || sch->reshape_fail(skb, sch))
423 return NET_XMIT_SUCCESS;
428 return NET_XMIT_DROP;
431 /* Length to Time (L2T) lookup in a qdisc_rate_table, to determine how
432 long it will take to send a packet given its size.
434 static inline u32 qdisc_l2t(struct qdisc_rate_table* rtab, unsigned int pktlen)
436 int slot = pktlen + rtab->rate.cell_align + rtab->rate.overhead;
439 slot >>= rtab->rate.cell_log;
441 return (rtab->data[255]*(slot >> 8) + rtab->data[slot & 0xFF]);
442 return rtab->data[slot];
445 #ifdef CONFIG_NET_CLS_ACT
446 static inline struct sk_buff *skb_act_clone(struct sk_buff *skb, gfp_t gfp_mask)
448 struct sk_buff *n = skb_clone(skb, gfp_mask);
451 n->tc_verd = SET_TC_VERD(n->tc_verd, 0);
452 n->tc_verd = CLR_TC_OK2MUNGE(n->tc_verd);
453 n->tc_verd = CLR_TC_MUNGED(n->tc_verd);