1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __NET_SCHED_GENERIC_H
3 #define __NET_SCHED_GENERIC_H
5 #include <linux/netdevice.h>
6 #include <linux/types.h>
7 #include <linux/rcupdate.h>
8 #include <linux/pkt_sched.h>
9 #include <linux/pkt_cls.h>
10 #include <linux/percpu.h>
11 #include <linux/dynamic_queue_limits.h>
12 #include <linux/list.h>
13 #include <linux/refcount.h>
14 #include <linux/workqueue.h>
15 #include <net/gen_stats.h>
16 #include <net/rtnetlink.h>
23 typedef int tc_setup_cb_t(enum tc_setup_type type,
24 void *type_data, void *cb_priv);
26 struct qdisc_rate_table {
27 struct tc_ratespec rate;
29 struct qdisc_rate_table *next;
35 __QDISC_STATE_DEACTIVATED,
38 struct qdisc_size_table {
40 struct list_head list;
41 struct tc_sizespec szopts;
46 /* similar to sk_buff_head, but skb->prev pointer is undefined. */
47 struct qdisc_skb_head {
55 int (*enqueue)(struct sk_buff *skb,
57 struct sk_buff **to_free);
58 struct sk_buff * (*dequeue)(struct Qdisc *sch);
60 #define TCQ_F_BUILTIN 1
61 #define TCQ_F_INGRESS 2
62 #define TCQ_F_CAN_BYPASS 4
63 #define TCQ_F_MQROOT 8
64 #define TCQ_F_ONETXQUEUE 0x10 /* dequeue_skb() can assume all skbs are for
65 * q->dev_queue : It can test
66 * netif_xmit_frozen_or_stopped() before
67 * dequeueing next packet.
68 * Its true for MQ/MQPRIO slaves, or non
71 #define TCQ_F_WARN_NONWC (1 << 16)
72 #define TCQ_F_CPUSTATS 0x20 /* run using percpu statistics */
73 #define TCQ_F_NOPARENT 0x40 /* root of its hierarchy :
74 * qdisc_tree_decrease_qlen() should stop.
76 #define TCQ_F_INVISIBLE 0x80 /* invisible by default in dump */
77 #define TCQ_F_NOLOCK 0x100 /* qdisc does not require locking */
78 #define TCQ_F_OFFLOADED 0x200 /* qdisc is offloaded to HW */
80 const struct Qdisc_ops *ops;
81 struct qdisc_size_table __rcu *stab;
82 struct hlist_node hash;
86 struct netdev_queue *dev_queue;
88 struct net_rate_estimator __rcu *rate_est;
89 struct gnet_stats_basic_cpu __percpu *cpu_bstats;
90 struct gnet_stats_queue __percpu *cpu_qstats;
95 * For performance sake on SMP, we put highly modified fields at the end
97 struct sk_buff_head gso_skb ____cacheline_aligned_in_smp;
98 struct qdisc_skb_head q;
99 struct gnet_stats_basic_packed bstats;
101 struct gnet_stats_queue qstats;
103 struct Qdisc *next_sched;
104 struct sk_buff_head skb_bad_txq;
106 spinlock_t busylock ____cacheline_aligned_in_smp;
110 static inline void qdisc_refcount_inc(struct Qdisc *qdisc)
112 if (qdisc->flags & TCQ_F_BUILTIN)
114 refcount_inc(&qdisc->refcnt);
117 static inline bool qdisc_is_running(struct Qdisc *qdisc)
119 if (qdisc->flags & TCQ_F_NOLOCK)
120 return spin_is_locked(&qdisc->seqlock);
121 return (raw_read_seqcount(&qdisc->running) & 1) ? true : false;
124 static inline bool qdisc_run_begin(struct Qdisc *qdisc)
126 if (qdisc->flags & TCQ_F_NOLOCK) {
127 if (!spin_trylock(&qdisc->seqlock))
129 } else if (qdisc_is_running(qdisc)) {
132 /* Variant of write_seqcount_begin() telling lockdep a trylock
135 raw_write_seqcount_begin(&qdisc->running);
136 seqcount_acquire(&qdisc->running.dep_map, 0, 1, _RET_IP_);
140 static inline void qdisc_run_end(struct Qdisc *qdisc)
142 write_seqcount_end(&qdisc->running);
143 if (qdisc->flags & TCQ_F_NOLOCK)
144 spin_unlock(&qdisc->seqlock);
147 static inline bool qdisc_may_bulk(const struct Qdisc *qdisc)
149 return qdisc->flags & TCQ_F_ONETXQUEUE;
152 static inline int qdisc_avail_bulklimit(const struct netdev_queue *txq)
155 /* Non-BQL migrated drivers will return 0, too. */
156 return dql_avail(&txq->dql);
162 struct Qdisc_class_ops {
163 /* Child qdisc manipulation */
164 struct netdev_queue * (*select_queue)(struct Qdisc *, struct tcmsg *);
165 int (*graft)(struct Qdisc *, unsigned long cl,
166 struct Qdisc *, struct Qdisc **,
167 struct netlink_ext_ack *extack);
168 struct Qdisc * (*leaf)(struct Qdisc *, unsigned long cl);
169 void (*qlen_notify)(struct Qdisc *, unsigned long);
171 /* Class manipulation routines */
172 unsigned long (*find)(struct Qdisc *, u32 classid);
173 int (*change)(struct Qdisc *, u32, u32,
174 struct nlattr **, unsigned long *,
175 struct netlink_ext_ack *);
176 int (*delete)(struct Qdisc *, unsigned long);
177 void (*walk)(struct Qdisc *, struct qdisc_walker * arg);
179 /* Filter manipulation */
180 struct tcf_block * (*tcf_block)(struct Qdisc *sch,
182 struct netlink_ext_ack *extack);
183 unsigned long (*bind_tcf)(struct Qdisc *, unsigned long,
185 void (*unbind_tcf)(struct Qdisc *, unsigned long);
187 /* rtnetlink specific */
188 int (*dump)(struct Qdisc *, unsigned long,
189 struct sk_buff *skb, struct tcmsg*);
190 int (*dump_stats)(struct Qdisc *, unsigned long,
195 struct Qdisc_ops *next;
196 const struct Qdisc_class_ops *cl_ops;
199 unsigned int static_flags;
201 int (*enqueue)(struct sk_buff *skb,
203 struct sk_buff **to_free);
204 struct sk_buff * (*dequeue)(struct Qdisc *);
205 struct sk_buff * (*peek)(struct Qdisc *);
207 int (*init)(struct Qdisc *sch, struct nlattr *arg,
208 struct netlink_ext_ack *extack);
209 void (*reset)(struct Qdisc *);
210 void (*destroy)(struct Qdisc *);
211 int (*change)(struct Qdisc *sch,
213 struct netlink_ext_ack *extack);
214 void (*attach)(struct Qdisc *sch);
215 int (*change_tx_queue_len)(struct Qdisc *, unsigned int);
217 int (*dump)(struct Qdisc *, struct sk_buff *);
218 int (*dump_stats)(struct Qdisc *, struct gnet_dump *);
220 void (*ingress_block_set)(struct Qdisc *sch,
222 void (*egress_block_set)(struct Qdisc *sch,
224 u32 (*ingress_block_get)(struct Qdisc *sch);
225 u32 (*egress_block_get)(struct Qdisc *sch);
227 struct module *owner;
237 const struct tcf_proto *goto_tp;
241 struct tcf_proto_ops {
242 struct list_head head;
245 int (*classify)(struct sk_buff *,
246 const struct tcf_proto *,
247 struct tcf_result *);
248 int (*init)(struct tcf_proto*);
249 void (*destroy)(struct tcf_proto *tp,
250 struct netlink_ext_ack *extack);
252 void* (*get)(struct tcf_proto*, u32 handle);
253 int (*change)(struct net *net, struct sk_buff *,
254 struct tcf_proto*, unsigned long,
255 u32 handle, struct nlattr **,
257 struct netlink_ext_ack *);
258 int (*delete)(struct tcf_proto *tp, void *arg,
260 struct netlink_ext_ack *);
261 void (*walk)(struct tcf_proto*, struct tcf_walker *arg);
262 int (*reoffload)(struct tcf_proto *tp, bool add,
263 tc_setup_cb_t *cb, void *cb_priv,
264 struct netlink_ext_ack *extack);
265 void (*bind_class)(void *, u32, unsigned long);
267 /* rtnetlink specific */
268 int (*dump)(struct net*, struct tcf_proto*, void *,
269 struct sk_buff *skb, struct tcmsg*);
271 struct module *owner;
275 /* Fast access part */
276 struct tcf_proto __rcu *next;
278 int (*classify)(struct sk_buff *,
279 const struct tcf_proto *,
280 struct tcf_result *);
286 const struct tcf_proto_ops *ops;
287 struct tcf_chain *chain;
291 struct qdisc_skb_cb {
292 unsigned int pkt_len;
293 u16 slave_dev_queue_mapping;
295 #define QDISC_CB_PRIV_LEN 20
296 unsigned char data[QDISC_CB_PRIV_LEN];
299 typedef void tcf_chain_head_change_t(struct tcf_proto *tp_head, void *priv);
302 struct tcf_proto __rcu *filter_chain;
303 struct list_head list;
304 struct tcf_block *block;
305 u32 index; /* chain index */
307 bool explicitly_created;
311 struct list_head chain_list;
312 u32 index; /* block index for shared blocks */
316 struct list_head cb_list;
317 struct list_head owner_list;
319 unsigned int offloadcnt; /* Number of oddloaded filters */
320 unsigned int nooffloaddevcnt; /* Number of devs unable to do offload */
322 struct tcf_chain *chain;
323 struct list_head filter_chain_list;
327 static inline void tcf_block_offload_inc(struct tcf_block *block, u32 *flags)
329 if (*flags & TCA_CLS_FLAGS_IN_HW)
331 *flags |= TCA_CLS_FLAGS_IN_HW;
335 static inline void tcf_block_offload_dec(struct tcf_block *block, u32 *flags)
337 if (!(*flags & TCA_CLS_FLAGS_IN_HW))
339 *flags &= ~TCA_CLS_FLAGS_IN_HW;
344 tc_cls_offload_cnt_update(struct tcf_block *block, unsigned int *cnt,
345 u32 *flags, bool add)
349 tcf_block_offload_inc(block, flags);
354 tcf_block_offload_dec(block, flags);
358 static inline void qdisc_cb_private_validate(const struct sk_buff *skb, int sz)
360 struct qdisc_skb_cb *qcb;
362 BUILD_BUG_ON(sizeof(skb->cb) < offsetof(struct qdisc_skb_cb, data) + sz);
363 BUILD_BUG_ON(sizeof(qcb->data) < sz);
366 static inline int qdisc_qlen_cpu(const struct Qdisc *q)
368 return this_cpu_ptr(q->cpu_qstats)->qlen;
371 static inline int qdisc_qlen(const struct Qdisc *q)
376 static inline int qdisc_qlen_sum(const struct Qdisc *q)
378 __u32 qlen = q->qstats.qlen;
381 if (q->flags & TCQ_F_NOLOCK) {
382 for_each_possible_cpu(i)
383 qlen += per_cpu_ptr(q->cpu_qstats, i)->qlen;
391 static inline struct qdisc_skb_cb *qdisc_skb_cb(const struct sk_buff *skb)
393 return (struct qdisc_skb_cb *)skb->cb;
396 static inline spinlock_t *qdisc_lock(struct Qdisc *qdisc)
398 return &qdisc->q.lock;
401 static inline struct Qdisc *qdisc_root(const struct Qdisc *qdisc)
403 struct Qdisc *q = rcu_dereference_rtnl(qdisc->dev_queue->qdisc);
408 static inline struct Qdisc *qdisc_root_sleeping(const struct Qdisc *qdisc)
410 return qdisc->dev_queue->qdisc_sleeping;
413 /* The qdisc root lock is a mechanism by which to top level
414 * of a qdisc tree can be locked from any qdisc node in the
415 * forest. This allows changing the configuration of some
416 * aspect of the qdisc tree while blocking out asynchronous
417 * qdisc access in the packet processing paths.
419 * It is only legal to do this when the root will not change
420 * on us. Otherwise we'll potentially lock the wrong qdisc
421 * root. This is enforced by holding the RTNL semaphore, which
422 * all users of this lock accessor must do.
424 static inline spinlock_t *qdisc_root_lock(const struct Qdisc *qdisc)
426 struct Qdisc *root = qdisc_root(qdisc);
429 return qdisc_lock(root);
432 static inline spinlock_t *qdisc_root_sleeping_lock(const struct Qdisc *qdisc)
434 struct Qdisc *root = qdisc_root_sleeping(qdisc);
437 return qdisc_lock(root);
440 static inline seqcount_t *qdisc_root_sleeping_running(const struct Qdisc *qdisc)
442 struct Qdisc *root = qdisc_root_sleeping(qdisc);
445 return &root->running;
448 static inline struct net_device *qdisc_dev(const struct Qdisc *qdisc)
450 return qdisc->dev_queue->dev;
453 static inline void sch_tree_lock(const struct Qdisc *q)
455 spin_lock_bh(qdisc_root_sleeping_lock(q));
458 static inline void sch_tree_unlock(const struct Qdisc *q)
460 spin_unlock_bh(qdisc_root_sleeping_lock(q));
463 extern struct Qdisc noop_qdisc;
464 extern struct Qdisc_ops noop_qdisc_ops;
465 extern struct Qdisc_ops pfifo_fast_ops;
466 extern struct Qdisc_ops mq_qdisc_ops;
467 extern struct Qdisc_ops noqueue_qdisc_ops;
468 extern const struct Qdisc_ops *default_qdisc_ops;
469 static inline const struct Qdisc_ops *
470 get_default_qdisc_ops(const struct net_device *dev, int ntx)
472 return ntx < dev->real_num_tx_queues ?
473 default_qdisc_ops : &pfifo_fast_ops;
476 struct Qdisc_class_common {
478 struct hlist_node hnode;
481 struct Qdisc_class_hash {
482 struct hlist_head *hash;
483 unsigned int hashsize;
484 unsigned int hashmask;
485 unsigned int hashelems;
488 static inline unsigned int qdisc_class_hash(u32 id, u32 mask)
495 static inline struct Qdisc_class_common *
496 qdisc_class_find(const struct Qdisc_class_hash *hash, u32 id)
498 struct Qdisc_class_common *cl;
504 h = qdisc_class_hash(id, hash->hashmask);
505 hlist_for_each_entry(cl, &hash->hash[h], hnode) {
506 if (cl->classid == id)
512 static inline int tc_classid_to_hwtc(struct net_device *dev, u32 classid)
514 u32 hwtc = TC_H_MIN(classid) - TC_H_MIN_PRIORITY;
516 return (hwtc < netdev_get_num_tc(dev)) ? hwtc : -EINVAL;
519 int qdisc_class_hash_init(struct Qdisc_class_hash *);
520 void qdisc_class_hash_insert(struct Qdisc_class_hash *,
521 struct Qdisc_class_common *);
522 void qdisc_class_hash_remove(struct Qdisc_class_hash *,
523 struct Qdisc_class_common *);
524 void qdisc_class_hash_grow(struct Qdisc *, struct Qdisc_class_hash *);
525 void qdisc_class_hash_destroy(struct Qdisc_class_hash *);
527 int dev_qdisc_change_tx_queue_len(struct net_device *dev);
528 void dev_init_scheduler(struct net_device *dev);
529 void dev_shutdown(struct net_device *dev);
530 void dev_activate(struct net_device *dev);
531 void dev_deactivate(struct net_device *dev);
532 void dev_deactivate_many(struct list_head *head);
533 struct Qdisc *dev_graft_qdisc(struct netdev_queue *dev_queue,
534 struct Qdisc *qdisc);
535 void qdisc_reset(struct Qdisc *qdisc);
536 void qdisc_destroy(struct Qdisc *qdisc);
537 void qdisc_tree_reduce_backlog(struct Qdisc *qdisc, unsigned int n,
539 struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue,
540 const struct Qdisc_ops *ops,
541 struct netlink_ext_ack *extack);
542 void qdisc_free(struct Qdisc *qdisc);
543 struct Qdisc *qdisc_create_dflt(struct netdev_queue *dev_queue,
544 const struct Qdisc_ops *ops, u32 parentid,
545 struct netlink_ext_ack *extack);
546 void __qdisc_calculate_pkt_len(struct sk_buff *skb,
547 const struct qdisc_size_table *stab);
548 int skb_do_redirect(struct sk_buff *);
550 static inline void skb_reset_tc(struct sk_buff *skb)
552 #ifdef CONFIG_NET_CLS_ACT
553 skb->tc_redirected = 0;
557 static inline bool skb_at_tc_ingress(const struct sk_buff *skb)
559 #ifdef CONFIG_NET_CLS_ACT
560 return skb->tc_at_ingress;
566 static inline bool skb_skip_tc_classify(struct sk_buff *skb)
568 #ifdef CONFIG_NET_CLS_ACT
569 if (skb->tc_skip_classify) {
570 skb->tc_skip_classify = 0;
577 /* Reset all TX qdiscs greater than index of a device. */
578 static inline void qdisc_reset_all_tx_gt(struct net_device *dev, unsigned int i)
582 for (; i < dev->num_tx_queues; i++) {
583 qdisc = rtnl_dereference(netdev_get_tx_queue(dev, i)->qdisc);
585 spin_lock_bh(qdisc_lock(qdisc));
587 spin_unlock_bh(qdisc_lock(qdisc));
592 static inline void qdisc_reset_all_tx(struct net_device *dev)
594 qdisc_reset_all_tx_gt(dev, 0);
597 /* Are all TX queues of the device empty? */
598 static inline bool qdisc_all_tx_empty(const struct net_device *dev)
603 for (i = 0; i < dev->num_tx_queues; i++) {
604 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
605 const struct Qdisc *q = rcu_dereference(txq->qdisc);
616 /* Are any of the TX qdiscs changing? */
617 static inline bool qdisc_tx_changing(const struct net_device *dev)
621 for (i = 0; i < dev->num_tx_queues; i++) {
622 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
623 if (rcu_access_pointer(txq->qdisc) != txq->qdisc_sleeping)
629 /* Is the device using the noop qdisc on all queues? */
630 static inline bool qdisc_tx_is_noop(const struct net_device *dev)
634 for (i = 0; i < dev->num_tx_queues; i++) {
635 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
636 if (rcu_access_pointer(txq->qdisc) != &noop_qdisc)
642 static inline unsigned int qdisc_pkt_len(const struct sk_buff *skb)
644 return qdisc_skb_cb(skb)->pkt_len;
647 /* additional qdisc xmit flags (NET_XMIT_MASK in linux/netdevice.h) */
648 enum net_xmit_qdisc_t {
649 __NET_XMIT_STOLEN = 0x00010000,
650 __NET_XMIT_BYPASS = 0x00020000,
653 #ifdef CONFIG_NET_CLS_ACT
654 #define net_xmit_drop_count(e) ((e) & __NET_XMIT_STOLEN ? 0 : 1)
656 #define net_xmit_drop_count(e) (1)
659 static inline void qdisc_calculate_pkt_len(struct sk_buff *skb,
660 const struct Qdisc *sch)
662 #ifdef CONFIG_NET_SCHED
663 struct qdisc_size_table *stab = rcu_dereference_bh(sch->stab);
666 __qdisc_calculate_pkt_len(skb, stab);
670 static inline int qdisc_enqueue(struct sk_buff *skb, struct Qdisc *sch,
671 struct sk_buff **to_free)
673 qdisc_calculate_pkt_len(skb, sch);
674 return sch->enqueue(skb, sch, to_free);
677 static inline bool qdisc_is_percpu_stats(const struct Qdisc *q)
679 return q->flags & TCQ_F_CPUSTATS;
682 static inline void _bstats_update(struct gnet_stats_basic_packed *bstats,
683 __u64 bytes, __u32 packets)
685 bstats->bytes += bytes;
686 bstats->packets += packets;
689 static inline void bstats_update(struct gnet_stats_basic_packed *bstats,
690 const struct sk_buff *skb)
692 _bstats_update(bstats,
694 skb_is_gso(skb) ? skb_shinfo(skb)->gso_segs : 1);
697 static inline void _bstats_cpu_update(struct gnet_stats_basic_cpu *bstats,
698 __u64 bytes, __u32 packets)
700 u64_stats_update_begin(&bstats->syncp);
701 _bstats_update(&bstats->bstats, bytes, packets);
702 u64_stats_update_end(&bstats->syncp);
705 static inline void bstats_cpu_update(struct gnet_stats_basic_cpu *bstats,
706 const struct sk_buff *skb)
708 u64_stats_update_begin(&bstats->syncp);
709 bstats_update(&bstats->bstats, skb);
710 u64_stats_update_end(&bstats->syncp);
713 static inline void qdisc_bstats_cpu_update(struct Qdisc *sch,
714 const struct sk_buff *skb)
716 bstats_cpu_update(this_cpu_ptr(sch->cpu_bstats), skb);
719 static inline void qdisc_bstats_update(struct Qdisc *sch,
720 const struct sk_buff *skb)
722 bstats_update(&sch->bstats, skb);
725 static inline void qdisc_qstats_backlog_dec(struct Qdisc *sch,
726 const struct sk_buff *skb)
728 sch->qstats.backlog -= qdisc_pkt_len(skb);
731 static inline void qdisc_qstats_cpu_backlog_dec(struct Qdisc *sch,
732 const struct sk_buff *skb)
734 this_cpu_sub(sch->cpu_qstats->backlog, qdisc_pkt_len(skb));
737 static inline void qdisc_qstats_backlog_inc(struct Qdisc *sch,
738 const struct sk_buff *skb)
740 sch->qstats.backlog += qdisc_pkt_len(skb);
743 static inline void qdisc_qstats_cpu_backlog_inc(struct Qdisc *sch,
744 const struct sk_buff *skb)
746 this_cpu_add(sch->cpu_qstats->backlog, qdisc_pkt_len(skb));
749 static inline void qdisc_qstats_cpu_qlen_inc(struct Qdisc *sch)
751 this_cpu_inc(sch->cpu_qstats->qlen);
754 static inline void qdisc_qstats_cpu_qlen_dec(struct Qdisc *sch)
756 this_cpu_dec(sch->cpu_qstats->qlen);
759 static inline void qdisc_qstats_cpu_requeues_inc(struct Qdisc *sch)
761 this_cpu_inc(sch->cpu_qstats->requeues);
764 static inline void __qdisc_qstats_drop(struct Qdisc *sch, int count)
766 sch->qstats.drops += count;
769 static inline void qstats_drop_inc(struct gnet_stats_queue *qstats)
774 static inline void qstats_overlimit_inc(struct gnet_stats_queue *qstats)
776 qstats->overlimits++;
779 static inline void qdisc_qstats_drop(struct Qdisc *sch)
781 qstats_drop_inc(&sch->qstats);
784 static inline void qdisc_qstats_cpu_drop(struct Qdisc *sch)
786 this_cpu_inc(sch->cpu_qstats->drops);
789 static inline void qdisc_qstats_overlimit(struct Qdisc *sch)
791 sch->qstats.overlimits++;
794 static inline void qdisc_skb_head_init(struct qdisc_skb_head *qh)
801 static inline int __qdisc_enqueue_tail(struct sk_buff *skb, struct Qdisc *sch,
802 struct qdisc_skb_head *qh)
804 struct sk_buff *last = qh->tail;
815 qdisc_qstats_backlog_inc(sch, skb);
817 return NET_XMIT_SUCCESS;
820 static inline int qdisc_enqueue_tail(struct sk_buff *skb, struct Qdisc *sch)
822 return __qdisc_enqueue_tail(skb, sch, &sch->q);
825 static inline struct sk_buff *__qdisc_dequeue_head(struct qdisc_skb_head *qh)
827 struct sk_buff *skb = qh->head;
829 if (likely(skb != NULL)) {
830 qh->head = skb->next;
832 if (qh->head == NULL)
840 static inline struct sk_buff *qdisc_dequeue_head(struct Qdisc *sch)
842 struct sk_buff *skb = __qdisc_dequeue_head(&sch->q);
844 if (likely(skb != NULL)) {
845 qdisc_qstats_backlog_dec(sch, skb);
846 qdisc_bstats_update(sch, skb);
852 /* Instead of calling kfree_skb() while root qdisc lock is held,
853 * queue the skb for future freeing at end of __dev_xmit_skb()
855 static inline void __qdisc_drop(struct sk_buff *skb, struct sk_buff **to_free)
857 skb->next = *to_free;
861 static inline void __qdisc_drop_all(struct sk_buff *skb,
862 struct sk_buff **to_free)
865 skb->prev->next = *to_free;
867 skb->next = *to_free;
871 static inline unsigned int __qdisc_queue_drop_head(struct Qdisc *sch,
872 struct qdisc_skb_head *qh,
873 struct sk_buff **to_free)
875 struct sk_buff *skb = __qdisc_dequeue_head(qh);
877 if (likely(skb != NULL)) {
878 unsigned int len = qdisc_pkt_len(skb);
880 qdisc_qstats_backlog_dec(sch, skb);
881 __qdisc_drop(skb, to_free);
888 static inline unsigned int qdisc_queue_drop_head(struct Qdisc *sch,
889 struct sk_buff **to_free)
891 return __qdisc_queue_drop_head(sch, &sch->q, to_free);
894 static inline struct sk_buff *qdisc_peek_head(struct Qdisc *sch)
896 const struct qdisc_skb_head *qh = &sch->q;
901 /* generic pseudo peek method for non-work-conserving qdisc */
902 static inline struct sk_buff *qdisc_peek_dequeued(struct Qdisc *sch)
904 struct sk_buff *skb = skb_peek(&sch->gso_skb);
906 /* we can reuse ->gso_skb because peek isn't called for root qdiscs */
908 skb = sch->dequeue(sch);
911 __skb_queue_head(&sch->gso_skb, skb);
912 /* it's still part of the queue */
913 qdisc_qstats_backlog_inc(sch, skb);
921 /* use instead of qdisc->dequeue() for all qdiscs queried with ->peek() */
922 static inline struct sk_buff *qdisc_dequeue_peeked(struct Qdisc *sch)
924 struct sk_buff *skb = skb_peek(&sch->gso_skb);
927 skb = __skb_dequeue(&sch->gso_skb);
928 qdisc_qstats_backlog_dec(sch, skb);
931 skb = sch->dequeue(sch);
937 static inline void __qdisc_reset_queue(struct qdisc_skb_head *qh)
940 * We do not know the backlog in bytes of this list, it
941 * is up to the caller to correct it
945 rtnl_kfree_skbs(qh->head, qh->tail);
953 static inline void qdisc_reset_queue(struct Qdisc *sch)
955 __qdisc_reset_queue(&sch->q);
956 sch->qstats.backlog = 0;
959 static inline struct Qdisc *qdisc_replace(struct Qdisc *sch, struct Qdisc *new,
968 unsigned int qlen = old->q.qlen;
969 unsigned int backlog = old->qstats.backlog;
972 qdisc_tree_reduce_backlog(old, qlen, backlog);
974 sch_tree_unlock(sch);
979 static inline void rtnl_qdisc_drop(struct sk_buff *skb, struct Qdisc *sch)
981 rtnl_kfree_skbs(skb, skb);
982 qdisc_qstats_drop(sch);
985 static inline int qdisc_drop_cpu(struct sk_buff *skb, struct Qdisc *sch,
986 struct sk_buff **to_free)
988 __qdisc_drop(skb, to_free);
989 qdisc_qstats_cpu_drop(sch);
991 return NET_XMIT_DROP;
994 static inline int qdisc_drop(struct sk_buff *skb, struct Qdisc *sch,
995 struct sk_buff **to_free)
997 __qdisc_drop(skb, to_free);
998 qdisc_qstats_drop(sch);
1000 return NET_XMIT_DROP;
1003 static inline int qdisc_drop_all(struct sk_buff *skb, struct Qdisc *sch,
1004 struct sk_buff **to_free)
1006 __qdisc_drop_all(skb, to_free);
1007 qdisc_qstats_drop(sch);
1009 return NET_XMIT_DROP;
1012 /* Length to Time (L2T) lookup in a qdisc_rate_table, to determine how
1013 long it will take to send a packet given its size.
1015 static inline u32 qdisc_l2t(struct qdisc_rate_table* rtab, unsigned int pktlen)
1017 int slot = pktlen + rtab->rate.cell_align + rtab->rate.overhead;
1020 slot >>= rtab->rate.cell_log;
1022 return rtab->data[255]*(slot >> 8) + rtab->data[slot & 0xFF];
1023 return rtab->data[slot];
1026 struct psched_ratecfg {
1027 u64 rate_bytes_ps; /* bytes per second */
1034 static inline u64 psched_l2t_ns(const struct psched_ratecfg *r,
1039 if (unlikely(r->linklayer == TC_LINKLAYER_ATM))
1040 return ((u64)(DIV_ROUND_UP(len,48)*53) * r->mult) >> r->shift;
1042 return ((u64)len * r->mult) >> r->shift;
1045 void psched_ratecfg_precompute(struct psched_ratecfg *r,
1046 const struct tc_ratespec *conf,
1049 static inline void psched_ratecfg_getrate(struct tc_ratespec *res,
1050 const struct psched_ratecfg *r)
1052 memset(res, 0, sizeof(*res));
1054 /* legacy struct tc_ratespec has a 32bit @rate field
1055 * Qdisc using 64bit rate should add new attributes
1056 * in order to maintain compatibility.
1058 res->rate = min_t(u64, r->rate_bytes_ps, ~0U);
1060 res->overhead = r->overhead;
1061 res->linklayer = (r->linklayer & TC_LINKLAYER_MASK);
1064 /* Mini Qdisc serves for specific needs of ingress/clsact Qdisc.
1065 * The fast path only needs to access filter list and to update stats
1068 struct tcf_proto *filter_list;
1069 struct gnet_stats_basic_cpu __percpu *cpu_bstats;
1070 struct gnet_stats_queue __percpu *cpu_qstats;
1071 struct rcu_head rcu;
1074 static inline void mini_qdisc_bstats_cpu_update(struct mini_Qdisc *miniq,
1075 const struct sk_buff *skb)
1077 bstats_cpu_update(this_cpu_ptr(miniq->cpu_bstats), skb);
1080 static inline void mini_qdisc_qstats_cpu_drop(struct mini_Qdisc *miniq)
1082 this_cpu_inc(miniq->cpu_qstats->drops);
1085 struct mini_Qdisc_pair {
1086 struct mini_Qdisc miniq1;
1087 struct mini_Qdisc miniq2;
1088 struct mini_Qdisc __rcu **p_miniq;
1091 void mini_qdisc_pair_swap(struct mini_Qdisc_pair *miniqp,
1092 struct tcf_proto *tp_head);
1093 void mini_qdisc_pair_init(struct mini_Qdisc_pair *miniqp, struct Qdisc *qdisc,
1094 struct mini_Qdisc __rcu **p_miniq);