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 {
58 int (*enqueue)(struct sk_buff *skb,
60 struct sk_buff **to_free);
61 struct sk_buff * (*dequeue)(struct Qdisc *sch);
63 #define TCQ_F_BUILTIN 1
64 #define TCQ_F_INGRESS 2
65 #define TCQ_F_CAN_BYPASS 4
66 #define TCQ_F_MQROOT 8
67 #define TCQ_F_ONETXQUEUE 0x10 /* dequeue_skb() can assume all skbs are for
68 * q->dev_queue : It can test
69 * netif_xmit_frozen_or_stopped() before
70 * dequeueing next packet.
71 * Its true for MQ/MQPRIO slaves, or non
74 #define TCQ_F_WARN_NONWC (1 << 16)
75 #define TCQ_F_CPUSTATS 0x20 /* run using percpu statistics */
76 #define TCQ_F_NOPARENT 0x40 /* root of its hierarchy :
77 * qdisc_tree_decrease_qlen() should stop.
79 #define TCQ_F_INVISIBLE 0x80 /* invisible by default in dump */
80 #define TCQ_F_NOLOCK 0x100 /* qdisc does not require locking */
81 #define TCQ_F_OFFLOADED 0x200 /* qdisc is offloaded to HW */
83 const struct Qdisc_ops *ops;
84 struct qdisc_size_table __rcu *stab;
85 struct hlist_node hash;
89 struct netdev_queue *dev_queue;
91 struct net_rate_estimator __rcu *rate_est;
92 struct gnet_stats_basic_cpu __percpu *cpu_bstats;
93 struct gnet_stats_queue __percpu *cpu_qstats;
98 * For performance sake on SMP, we put highly modified fields at the end
100 struct sk_buff_head gso_skb ____cacheline_aligned_in_smp;
101 struct qdisc_skb_head q;
102 struct gnet_stats_basic_packed bstats;
104 struct gnet_stats_queue qstats;
106 struct Qdisc *next_sched;
107 struct sk_buff_head skb_bad_txq;
109 spinlock_t busylock ____cacheline_aligned_in_smp;
114 static inline void qdisc_refcount_inc(struct Qdisc *qdisc)
116 if (qdisc->flags & TCQ_F_BUILTIN)
118 refcount_inc(&qdisc->refcnt);
121 /* Intended to be used by unlocked users, when concurrent qdisc release is
125 static inline struct Qdisc *qdisc_refcount_inc_nz(struct Qdisc *qdisc)
127 if (qdisc->flags & TCQ_F_BUILTIN)
129 if (refcount_inc_not_zero(&qdisc->refcnt))
134 static inline bool qdisc_is_running(struct Qdisc *qdisc)
136 if (qdisc->flags & TCQ_F_NOLOCK)
137 return spin_is_locked(&qdisc->seqlock);
138 return (raw_read_seqcount(&qdisc->running) & 1) ? true : false;
141 static inline bool qdisc_run_begin(struct Qdisc *qdisc)
143 if (qdisc->flags & TCQ_F_NOLOCK) {
144 if (!spin_trylock(&qdisc->seqlock))
146 } else if (qdisc_is_running(qdisc)) {
149 /* Variant of write_seqcount_begin() telling lockdep a trylock
152 raw_write_seqcount_begin(&qdisc->running);
153 seqcount_acquire(&qdisc->running.dep_map, 0, 1, _RET_IP_);
157 static inline void qdisc_run_end(struct Qdisc *qdisc)
159 write_seqcount_end(&qdisc->running);
160 if (qdisc->flags & TCQ_F_NOLOCK)
161 spin_unlock(&qdisc->seqlock);
164 static inline bool qdisc_may_bulk(const struct Qdisc *qdisc)
166 return qdisc->flags & TCQ_F_ONETXQUEUE;
169 static inline int qdisc_avail_bulklimit(const struct netdev_queue *txq)
172 /* Non-BQL migrated drivers will return 0, too. */
173 return dql_avail(&txq->dql);
179 struct Qdisc_class_ops {
180 /* Child qdisc manipulation */
181 struct netdev_queue * (*select_queue)(struct Qdisc *, struct tcmsg *);
182 int (*graft)(struct Qdisc *, unsigned long cl,
183 struct Qdisc *, struct Qdisc **,
184 struct netlink_ext_ack *extack);
185 struct Qdisc * (*leaf)(struct Qdisc *, unsigned long cl);
186 void (*qlen_notify)(struct Qdisc *, unsigned long);
188 /* Class manipulation routines */
189 unsigned long (*find)(struct Qdisc *, u32 classid);
190 int (*change)(struct Qdisc *, u32, u32,
191 struct nlattr **, unsigned long *,
192 struct netlink_ext_ack *);
193 int (*delete)(struct Qdisc *, unsigned long);
194 void (*walk)(struct Qdisc *, struct qdisc_walker * arg);
196 /* Filter manipulation */
197 struct tcf_block * (*tcf_block)(struct Qdisc *sch,
199 struct netlink_ext_ack *extack);
200 unsigned long (*bind_tcf)(struct Qdisc *, unsigned long,
202 void (*unbind_tcf)(struct Qdisc *, unsigned long);
204 /* rtnetlink specific */
205 int (*dump)(struct Qdisc *, unsigned long,
206 struct sk_buff *skb, struct tcmsg*);
207 int (*dump_stats)(struct Qdisc *, unsigned long,
212 struct Qdisc_ops *next;
213 const struct Qdisc_class_ops *cl_ops;
216 unsigned int static_flags;
218 int (*enqueue)(struct sk_buff *skb,
220 struct sk_buff **to_free);
221 struct sk_buff * (*dequeue)(struct Qdisc *);
222 struct sk_buff * (*peek)(struct Qdisc *);
224 int (*init)(struct Qdisc *sch, struct nlattr *arg,
225 struct netlink_ext_ack *extack);
226 void (*reset)(struct Qdisc *);
227 void (*destroy)(struct Qdisc *);
228 int (*change)(struct Qdisc *sch,
230 struct netlink_ext_ack *extack);
231 void (*attach)(struct Qdisc *sch);
232 int (*change_tx_queue_len)(struct Qdisc *, unsigned int);
233 void (*change_real_num_tx)(struct Qdisc *sch,
234 unsigned int new_real_tx);
236 int (*dump)(struct Qdisc *, struct sk_buff *);
237 int (*dump_stats)(struct Qdisc *, struct gnet_dump *);
239 void (*ingress_block_set)(struct Qdisc *sch,
241 void (*egress_block_set)(struct Qdisc *sch,
243 u32 (*ingress_block_get)(struct Qdisc *sch);
244 u32 (*egress_block_get)(struct Qdisc *sch);
246 struct module *owner;
256 const struct tcf_proto *goto_tp;
258 /* used by the TC_ACT_REINSERT action */
261 struct gnet_stats_queue *qstats;
268 struct tcf_proto_ops {
269 struct list_head head;
272 int (*classify)(struct sk_buff *,
273 const struct tcf_proto *,
274 struct tcf_result *);
275 int (*init)(struct tcf_proto*);
276 void (*destroy)(struct tcf_proto *tp,
277 struct netlink_ext_ack *extack);
279 void* (*get)(struct tcf_proto*, u32 handle);
280 int (*change)(struct net *net, struct sk_buff *,
281 struct tcf_proto*, unsigned long,
282 u32 handle, struct nlattr **,
284 struct netlink_ext_ack *);
285 int (*delete)(struct tcf_proto *tp, void *arg,
287 struct netlink_ext_ack *);
288 void (*walk)(struct tcf_proto*, struct tcf_walker *arg);
289 int (*reoffload)(struct tcf_proto *tp, bool add,
290 tc_setup_cb_t *cb, void *cb_priv,
291 struct netlink_ext_ack *extack);
292 void (*bind_class)(void *, u32, unsigned long,
293 void *, unsigned long);
294 void * (*tmplt_create)(struct net *net,
295 struct tcf_chain *chain,
297 struct netlink_ext_ack *extack);
298 void (*tmplt_destroy)(void *tmplt_priv);
300 /* rtnetlink specific */
301 int (*dump)(struct net*, struct tcf_proto*, void *,
302 struct sk_buff *skb, struct tcmsg*);
303 int (*tmplt_dump)(struct sk_buff *skb,
307 struct module *owner;
311 /* Fast access part */
312 struct tcf_proto __rcu *next;
315 /* called under RCU BH lock*/
316 int (*classify)(struct sk_buff *,
317 const struct tcf_proto *,
318 struct tcf_result *);
324 const struct tcf_proto_ops *ops;
325 struct tcf_chain *chain;
329 struct qdisc_skb_cb {
330 unsigned int pkt_len;
331 u16 slave_dev_queue_mapping;
333 #define QDISC_CB_PRIV_LEN 20
334 unsigned char data[QDISC_CB_PRIV_LEN];
337 typedef void tcf_chain_head_change_t(struct tcf_proto *tp_head, void *priv);
340 struct tcf_proto __rcu *filter_chain;
341 struct list_head list;
342 struct tcf_block *block;
343 u32 index; /* chain index */
345 unsigned int action_refcnt;
346 bool explicitly_created;
347 const struct tcf_proto_ops *tmplt_ops;
352 struct list_head chain_list;
353 u32 index; /* block index for shared blocks */
357 struct list_head cb_list;
358 struct list_head owner_list;
360 unsigned int offloadcnt; /* Number of oddloaded filters */
361 unsigned int nooffloaddevcnt; /* Number of devs unable to do offload */
363 struct tcf_chain *chain;
364 struct list_head filter_chain_list;
368 static inline void tcf_block_offload_inc(struct tcf_block *block, u32 *flags)
370 if (*flags & TCA_CLS_FLAGS_IN_HW)
372 *flags |= TCA_CLS_FLAGS_IN_HW;
376 static inline void tcf_block_offload_dec(struct tcf_block *block, u32 *flags)
378 if (!(*flags & TCA_CLS_FLAGS_IN_HW))
380 *flags &= ~TCA_CLS_FLAGS_IN_HW;
385 tc_cls_offload_cnt_update(struct tcf_block *block, unsigned int *cnt,
386 u32 *flags, bool add)
390 tcf_block_offload_inc(block, flags);
395 tcf_block_offload_dec(block, flags);
399 static inline void qdisc_cb_private_validate(const struct sk_buff *skb, int sz)
401 struct qdisc_skb_cb *qcb;
403 BUILD_BUG_ON(sizeof(skb->cb) < offsetof(struct qdisc_skb_cb, data) + sz);
404 BUILD_BUG_ON(sizeof(qcb->data) < sz);
407 static inline int qdisc_qlen(const struct Qdisc *q)
412 static inline u32 qdisc_qlen_sum(const struct Qdisc *q)
414 u32 qlen = q->qstats.qlen;
416 if (q->flags & TCQ_F_NOLOCK)
417 qlen += atomic_read(&q->q.atomic_qlen);
424 static inline struct qdisc_skb_cb *qdisc_skb_cb(const struct sk_buff *skb)
426 return (struct qdisc_skb_cb *)skb->cb;
429 static inline spinlock_t *qdisc_lock(struct Qdisc *qdisc)
431 return &qdisc->q.lock;
434 static inline struct Qdisc *qdisc_root(const struct Qdisc *qdisc)
436 struct Qdisc *q = rcu_dereference_rtnl(qdisc->dev_queue->qdisc);
441 static inline struct Qdisc *qdisc_root_bh(const struct Qdisc *qdisc)
443 return rcu_dereference_bh(qdisc->dev_queue->qdisc);
446 static inline struct Qdisc *qdisc_root_sleeping(const struct Qdisc *qdisc)
448 return qdisc->dev_queue->qdisc_sleeping;
451 /* The qdisc root lock is a mechanism by which to top level
452 * of a qdisc tree can be locked from any qdisc node in the
453 * forest. This allows changing the configuration of some
454 * aspect of the qdisc tree while blocking out asynchronous
455 * qdisc access in the packet processing paths.
457 * It is only legal to do this when the root will not change
458 * on us. Otherwise we'll potentially lock the wrong qdisc
459 * root. This is enforced by holding the RTNL semaphore, which
460 * all users of this lock accessor must do.
462 static inline spinlock_t *qdisc_root_lock(const struct Qdisc *qdisc)
464 struct Qdisc *root = qdisc_root(qdisc);
467 return qdisc_lock(root);
470 static inline spinlock_t *qdisc_root_sleeping_lock(const struct Qdisc *qdisc)
472 struct Qdisc *root = qdisc_root_sleeping(qdisc);
475 return qdisc_lock(root);
478 static inline seqcount_t *qdisc_root_sleeping_running(const struct Qdisc *qdisc)
480 struct Qdisc *root = qdisc_root_sleeping(qdisc);
483 return &root->running;
486 static inline struct net_device *qdisc_dev(const struct Qdisc *qdisc)
488 return qdisc->dev_queue->dev;
491 static inline void sch_tree_lock(const struct Qdisc *q)
493 spin_lock_bh(qdisc_root_sleeping_lock(q));
496 static inline void sch_tree_unlock(const struct Qdisc *q)
498 spin_unlock_bh(qdisc_root_sleeping_lock(q));
501 extern struct Qdisc noop_qdisc;
502 extern struct Qdisc_ops noop_qdisc_ops;
503 extern struct Qdisc_ops pfifo_fast_ops;
504 extern struct Qdisc_ops mq_qdisc_ops;
505 extern struct Qdisc_ops noqueue_qdisc_ops;
506 extern const struct Qdisc_ops *default_qdisc_ops;
507 static inline const struct Qdisc_ops *
508 get_default_qdisc_ops(const struct net_device *dev, int ntx)
510 return ntx < dev->real_num_tx_queues ?
511 default_qdisc_ops : &pfifo_fast_ops;
514 struct Qdisc_class_common {
516 struct hlist_node hnode;
519 struct Qdisc_class_hash {
520 struct hlist_head *hash;
521 unsigned int hashsize;
522 unsigned int hashmask;
523 unsigned int hashelems;
526 static inline unsigned int qdisc_class_hash(u32 id, u32 mask)
533 static inline struct Qdisc_class_common *
534 qdisc_class_find(const struct Qdisc_class_hash *hash, u32 id)
536 struct Qdisc_class_common *cl;
542 h = qdisc_class_hash(id, hash->hashmask);
543 hlist_for_each_entry(cl, &hash->hash[h], hnode) {
544 if (cl->classid == id)
550 static inline int tc_classid_to_hwtc(struct net_device *dev, u32 classid)
552 u32 hwtc = TC_H_MIN(classid) - TC_H_MIN_PRIORITY;
554 return (hwtc < netdev_get_num_tc(dev)) ? hwtc : -EINVAL;
557 int qdisc_class_hash_init(struct Qdisc_class_hash *);
558 void qdisc_class_hash_insert(struct Qdisc_class_hash *,
559 struct Qdisc_class_common *);
560 void qdisc_class_hash_remove(struct Qdisc_class_hash *,
561 struct Qdisc_class_common *);
562 void qdisc_class_hash_grow(struct Qdisc *, struct Qdisc_class_hash *);
563 void qdisc_class_hash_destroy(struct Qdisc_class_hash *);
565 int dev_qdisc_change_tx_queue_len(struct net_device *dev);
566 void dev_qdisc_change_real_num_tx(struct net_device *dev,
567 unsigned int new_real_tx);
568 void dev_init_scheduler(struct net_device *dev);
569 void dev_shutdown(struct net_device *dev);
570 void dev_activate(struct net_device *dev);
571 void dev_deactivate(struct net_device *dev);
572 void dev_deactivate_many(struct list_head *head);
573 struct Qdisc *dev_graft_qdisc(struct netdev_queue *dev_queue,
574 struct Qdisc *qdisc);
575 void qdisc_reset(struct Qdisc *qdisc);
576 void qdisc_put(struct Qdisc *qdisc);
577 void qdisc_put_unlocked(struct Qdisc *qdisc);
578 void qdisc_tree_reduce_backlog(struct Qdisc *qdisc, unsigned int n,
580 struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue,
581 const struct Qdisc_ops *ops,
582 struct netlink_ext_ack *extack);
583 void qdisc_free(struct Qdisc *qdisc);
584 struct Qdisc *qdisc_create_dflt(struct netdev_queue *dev_queue,
585 const struct Qdisc_ops *ops, u32 parentid,
586 struct netlink_ext_ack *extack);
587 void __qdisc_calculate_pkt_len(struct sk_buff *skb,
588 const struct qdisc_size_table *stab);
589 int skb_do_redirect(struct sk_buff *);
591 static inline void skb_reset_tc(struct sk_buff *skb)
593 #ifdef CONFIG_NET_CLS_ACT
594 skb->tc_redirected = 0;
598 static inline bool skb_is_tc_redirected(const struct sk_buff *skb)
600 #ifdef CONFIG_NET_CLS_ACT
601 return skb->tc_redirected;
607 static inline bool skb_at_tc_ingress(const struct sk_buff *skb)
609 #ifdef CONFIG_NET_CLS_ACT
610 return skb->tc_at_ingress;
616 static inline bool skb_skip_tc_classify(struct sk_buff *skb)
618 #ifdef CONFIG_NET_CLS_ACT
619 if (skb->tc_skip_classify) {
620 skb->tc_skip_classify = 0;
627 /* Reset all TX qdiscs greater than index of a device. */
628 static inline void qdisc_reset_all_tx_gt(struct net_device *dev, unsigned int i)
632 for (; i < dev->num_tx_queues; i++) {
633 qdisc = rtnl_dereference(netdev_get_tx_queue(dev, i)->qdisc);
635 spin_lock_bh(qdisc_lock(qdisc));
637 spin_unlock_bh(qdisc_lock(qdisc));
642 static inline void qdisc_reset_all_tx(struct net_device *dev)
644 qdisc_reset_all_tx_gt(dev, 0);
647 /* Are all TX queues of the device empty? */
648 static inline bool qdisc_all_tx_empty(const struct net_device *dev)
653 for (i = 0; i < dev->num_tx_queues; i++) {
654 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
655 const struct Qdisc *q = rcu_dereference(txq->qdisc);
666 /* Are any of the TX qdiscs changing? */
667 static inline bool qdisc_tx_changing(const struct net_device *dev)
671 for (i = 0; i < dev->num_tx_queues; i++) {
672 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
673 if (rcu_access_pointer(txq->qdisc) != txq->qdisc_sleeping)
679 /* Is the device using the noop qdisc on all queues? */
680 static inline bool qdisc_tx_is_noop(const struct net_device *dev)
684 for (i = 0; i < dev->num_tx_queues; i++) {
685 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
686 if (rcu_access_pointer(txq->qdisc) != &noop_qdisc)
692 static inline unsigned int qdisc_pkt_len(const struct sk_buff *skb)
694 return qdisc_skb_cb(skb)->pkt_len;
697 /* additional qdisc xmit flags (NET_XMIT_MASK in linux/netdevice.h) */
698 enum net_xmit_qdisc_t {
699 __NET_XMIT_STOLEN = 0x00010000,
700 __NET_XMIT_BYPASS = 0x00020000,
703 #ifdef CONFIG_NET_CLS_ACT
704 #define net_xmit_drop_count(e) ((e) & __NET_XMIT_STOLEN ? 0 : 1)
706 #define net_xmit_drop_count(e) (1)
709 static inline void qdisc_calculate_pkt_len(struct sk_buff *skb,
710 const struct Qdisc *sch)
712 #ifdef CONFIG_NET_SCHED
713 struct qdisc_size_table *stab = rcu_dereference_bh(sch->stab);
716 __qdisc_calculate_pkt_len(skb, stab);
720 static inline int qdisc_enqueue(struct sk_buff *skb, struct Qdisc *sch,
721 struct sk_buff **to_free)
723 qdisc_calculate_pkt_len(skb, sch);
724 return sch->enqueue(skb, sch, to_free);
727 static inline bool qdisc_is_percpu_stats(const struct Qdisc *q)
729 return q->flags & TCQ_F_CPUSTATS;
732 static inline void _bstats_update(struct gnet_stats_basic_packed *bstats,
733 __u64 bytes, __u32 packets)
735 bstats->bytes += bytes;
736 bstats->packets += packets;
739 static inline void bstats_update(struct gnet_stats_basic_packed *bstats,
740 const struct sk_buff *skb)
742 _bstats_update(bstats,
744 skb_is_gso(skb) ? skb_shinfo(skb)->gso_segs : 1);
747 static inline void _bstats_cpu_update(struct gnet_stats_basic_cpu *bstats,
748 __u64 bytes, __u32 packets)
750 u64_stats_update_begin(&bstats->syncp);
751 _bstats_update(&bstats->bstats, bytes, packets);
752 u64_stats_update_end(&bstats->syncp);
755 static inline void bstats_cpu_update(struct gnet_stats_basic_cpu *bstats,
756 const struct sk_buff *skb)
758 u64_stats_update_begin(&bstats->syncp);
759 bstats_update(&bstats->bstats, skb);
760 u64_stats_update_end(&bstats->syncp);
763 static inline void qdisc_bstats_cpu_update(struct Qdisc *sch,
764 const struct sk_buff *skb)
766 bstats_cpu_update(this_cpu_ptr(sch->cpu_bstats), skb);
769 static inline void qdisc_bstats_update(struct Qdisc *sch,
770 const struct sk_buff *skb)
772 bstats_update(&sch->bstats, skb);
775 static inline void qdisc_qstats_backlog_dec(struct Qdisc *sch,
776 const struct sk_buff *skb)
778 sch->qstats.backlog -= qdisc_pkt_len(skb);
781 static inline void qdisc_qstats_cpu_backlog_dec(struct Qdisc *sch,
782 const struct sk_buff *skb)
784 this_cpu_sub(sch->cpu_qstats->backlog, qdisc_pkt_len(skb));
787 static inline void qdisc_qstats_backlog_inc(struct Qdisc *sch,
788 const struct sk_buff *skb)
790 sch->qstats.backlog += qdisc_pkt_len(skb);
793 static inline void qdisc_qstats_cpu_backlog_inc(struct Qdisc *sch,
794 const struct sk_buff *skb)
796 this_cpu_add(sch->cpu_qstats->backlog, qdisc_pkt_len(skb));
799 static inline void qdisc_qstats_atomic_qlen_inc(struct Qdisc *sch)
801 atomic_inc(&sch->q.atomic_qlen);
804 static inline void qdisc_qstats_atomic_qlen_dec(struct Qdisc *sch)
806 atomic_dec(&sch->q.atomic_qlen);
809 static inline void qdisc_qstats_cpu_requeues_inc(struct Qdisc *sch)
811 this_cpu_inc(sch->cpu_qstats->requeues);
814 static inline void __qdisc_qstats_drop(struct Qdisc *sch, int count)
816 sch->qstats.drops += count;
819 static inline void qstats_drop_inc(struct gnet_stats_queue *qstats)
824 static inline void qstats_overlimit_inc(struct gnet_stats_queue *qstats)
826 qstats->overlimits++;
829 static inline void qdisc_qstats_drop(struct Qdisc *sch)
831 qstats_drop_inc(&sch->qstats);
834 static inline void qdisc_qstats_cpu_drop(struct Qdisc *sch)
836 this_cpu_inc(sch->cpu_qstats->drops);
839 static inline void qdisc_qstats_overlimit(struct Qdisc *sch)
841 sch->qstats.overlimits++;
844 static inline void qdisc_skb_head_init(struct qdisc_skb_head *qh)
851 static inline int __qdisc_enqueue_tail(struct sk_buff *skb, struct Qdisc *sch,
852 struct qdisc_skb_head *qh)
854 struct sk_buff *last = qh->tail;
865 qdisc_qstats_backlog_inc(sch, skb);
867 return NET_XMIT_SUCCESS;
870 static inline int qdisc_enqueue_tail(struct sk_buff *skb, struct Qdisc *sch)
872 return __qdisc_enqueue_tail(skb, sch, &sch->q);
875 static inline struct sk_buff *__qdisc_dequeue_head(struct qdisc_skb_head *qh)
877 struct sk_buff *skb = qh->head;
879 if (likely(skb != NULL)) {
880 qh->head = skb->next;
882 if (qh->head == NULL)
890 static inline struct sk_buff *qdisc_dequeue_head(struct Qdisc *sch)
892 struct sk_buff *skb = __qdisc_dequeue_head(&sch->q);
894 if (likely(skb != NULL)) {
895 qdisc_qstats_backlog_dec(sch, skb);
896 qdisc_bstats_update(sch, skb);
902 /* Instead of calling kfree_skb() while root qdisc lock is held,
903 * queue the skb for future freeing at end of __dev_xmit_skb()
905 static inline void __qdisc_drop(struct sk_buff *skb, struct sk_buff **to_free)
907 skb->next = *to_free;
911 static inline void __qdisc_drop_all(struct sk_buff *skb,
912 struct sk_buff **to_free)
915 skb->prev->next = *to_free;
917 skb->next = *to_free;
921 static inline unsigned int __qdisc_queue_drop_head(struct Qdisc *sch,
922 struct qdisc_skb_head *qh,
923 struct sk_buff **to_free)
925 struct sk_buff *skb = __qdisc_dequeue_head(qh);
927 if (likely(skb != NULL)) {
928 unsigned int len = qdisc_pkt_len(skb);
930 qdisc_qstats_backlog_dec(sch, skb);
931 __qdisc_drop(skb, to_free);
938 static inline unsigned int qdisc_queue_drop_head(struct Qdisc *sch,
939 struct sk_buff **to_free)
941 return __qdisc_queue_drop_head(sch, &sch->q, to_free);
944 static inline struct sk_buff *qdisc_peek_head(struct Qdisc *sch)
946 const struct qdisc_skb_head *qh = &sch->q;
951 /* generic pseudo peek method for non-work-conserving qdisc */
952 static inline struct sk_buff *qdisc_peek_dequeued(struct Qdisc *sch)
954 struct sk_buff *skb = skb_peek(&sch->gso_skb);
956 /* we can reuse ->gso_skb because peek isn't called for root qdiscs */
958 skb = sch->dequeue(sch);
961 __skb_queue_head(&sch->gso_skb, skb);
962 /* it's still part of the queue */
963 qdisc_qstats_backlog_inc(sch, skb);
971 /* use instead of qdisc->dequeue() for all qdiscs queried with ->peek() */
972 static inline struct sk_buff *qdisc_dequeue_peeked(struct Qdisc *sch)
974 struct sk_buff *skb = skb_peek(&sch->gso_skb);
977 skb = __skb_dequeue(&sch->gso_skb);
978 qdisc_qstats_backlog_dec(sch, skb);
981 skb = sch->dequeue(sch);
987 static inline void __qdisc_reset_queue(struct qdisc_skb_head *qh)
990 * We do not know the backlog in bytes of this list, it
991 * is up to the caller to correct it
995 rtnl_kfree_skbs(qh->head, qh->tail);
1003 static inline void qdisc_reset_queue(struct Qdisc *sch)
1005 __qdisc_reset_queue(&sch->q);
1006 sch->qstats.backlog = 0;
1009 static inline struct Qdisc *qdisc_replace(struct Qdisc *sch, struct Qdisc *new,
1010 struct Qdisc **pold)
1018 unsigned int qlen = old->q.qlen;
1019 unsigned int backlog = old->qstats.backlog;
1022 qdisc_tree_reduce_backlog(old, qlen, backlog);
1024 sch_tree_unlock(sch);
1029 static inline void rtnl_qdisc_drop(struct sk_buff *skb, struct Qdisc *sch)
1031 rtnl_kfree_skbs(skb, skb);
1032 qdisc_qstats_drop(sch);
1035 static inline int qdisc_drop_cpu(struct sk_buff *skb, struct Qdisc *sch,
1036 struct sk_buff **to_free)
1038 __qdisc_drop(skb, to_free);
1039 qdisc_qstats_cpu_drop(sch);
1041 return NET_XMIT_DROP;
1044 static inline int qdisc_drop(struct sk_buff *skb, struct Qdisc *sch,
1045 struct sk_buff **to_free)
1047 __qdisc_drop(skb, to_free);
1048 qdisc_qstats_drop(sch);
1050 return NET_XMIT_DROP;
1053 static inline int qdisc_drop_all(struct sk_buff *skb, struct Qdisc *sch,
1054 struct sk_buff **to_free)
1056 __qdisc_drop_all(skb, to_free);
1057 qdisc_qstats_drop(sch);
1059 return NET_XMIT_DROP;
1062 /* Length to Time (L2T) lookup in a qdisc_rate_table, to determine how
1063 long it will take to send a packet given its size.
1065 static inline u32 qdisc_l2t(struct qdisc_rate_table* rtab, unsigned int pktlen)
1067 int slot = pktlen + rtab->rate.cell_align + rtab->rate.overhead;
1070 slot >>= rtab->rate.cell_log;
1072 return rtab->data[255]*(slot >> 8) + rtab->data[slot & 0xFF];
1073 return rtab->data[slot];
1076 struct psched_ratecfg {
1077 u64 rate_bytes_ps; /* bytes per second */
1085 static inline u64 psched_l2t_ns(const struct psched_ratecfg *r,
1093 if (unlikely(r->linklayer == TC_LINKLAYER_ATM))
1094 return ((u64)(DIV_ROUND_UP(len,48)*53) * r->mult) >> r->shift;
1096 return ((u64)len * r->mult) >> r->shift;
1099 void psched_ratecfg_precompute(struct psched_ratecfg *r,
1100 const struct tc_ratespec *conf,
1103 static inline void psched_ratecfg_getrate(struct tc_ratespec *res,
1104 const struct psched_ratecfg *r)
1106 memset(res, 0, sizeof(*res));
1108 /* legacy struct tc_ratespec has a 32bit @rate field
1109 * Qdisc using 64bit rate should add new attributes
1110 * in order to maintain compatibility.
1112 res->rate = min_t(u64, r->rate_bytes_ps, ~0U);
1114 res->overhead = r->overhead;
1116 res->linklayer = (r->linklayer & TC_LINKLAYER_MASK);
1119 /* Mini Qdisc serves for specific needs of ingress/clsact Qdisc.
1120 * The fast path only needs to access filter list and to update stats
1123 struct tcf_proto *filter_list;
1124 struct gnet_stats_basic_cpu __percpu *cpu_bstats;
1125 struct gnet_stats_queue __percpu *cpu_qstats;
1126 struct rcu_head rcu;
1129 static inline void mini_qdisc_bstats_cpu_update(struct mini_Qdisc *miniq,
1130 const struct sk_buff *skb)
1132 bstats_cpu_update(this_cpu_ptr(miniq->cpu_bstats), skb);
1135 static inline void mini_qdisc_qstats_cpu_drop(struct mini_Qdisc *miniq)
1137 this_cpu_inc(miniq->cpu_qstats->drops);
1140 struct mini_Qdisc_pair {
1141 struct mini_Qdisc miniq1;
1142 struct mini_Qdisc miniq2;
1143 struct mini_Qdisc __rcu **p_miniq;
1146 void mini_qdisc_pair_swap(struct mini_Qdisc_pair *miniqp,
1147 struct tcf_proto *tp_head);
1148 void mini_qdisc_pair_init(struct mini_Qdisc_pair *miniqp, struct Qdisc *qdisc,
1149 struct mini_Qdisc __rcu **p_miniq);
1151 static inline void skb_tc_reinsert(struct sk_buff *skb, struct tcf_result *res)
1153 struct gnet_stats_queue *stats = res->qstats;
1157 ret = netif_receive_skb(skb);
1159 ret = dev_queue_xmit(skb);
1161 qstats_overlimit_inc(res->qstats);