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>
24 typedef int tc_setup_cb_t(enum tc_setup_type type,
25 void *type_data, void *cb_priv);
27 struct qdisc_rate_table {
28 struct tc_ratespec rate;
30 struct qdisc_rate_table *next;
36 __QDISC_STATE_DEACTIVATED,
39 struct qdisc_size_table {
41 struct list_head list;
42 struct tc_sizespec szopts;
47 /* similar to sk_buff_head, but skb->prev pointer is undefined. */
48 struct qdisc_skb_head {
56 int (*enqueue)(struct sk_buff *skb,
58 struct sk_buff **to_free);
59 struct sk_buff * (*dequeue)(struct Qdisc *sch);
61 #define TCQ_F_BUILTIN 1
62 #define TCQ_F_INGRESS 2
63 #define TCQ_F_CAN_BYPASS 4
64 #define TCQ_F_MQROOT 8
65 #define TCQ_F_ONETXQUEUE 0x10 /* dequeue_skb() can assume all skbs are for
66 * q->dev_queue : It can test
67 * netif_xmit_frozen_or_stopped() before
68 * dequeueing next packet.
69 * Its true for MQ/MQPRIO slaves, or non
72 #define TCQ_F_WARN_NONWC (1 << 16)
73 #define TCQ_F_CPUSTATS 0x20 /* run using percpu statistics */
74 #define TCQ_F_NOPARENT 0x40 /* root of its hierarchy :
75 * qdisc_tree_decrease_qlen() should stop.
77 #define TCQ_F_INVISIBLE 0x80 /* invisible by default in dump */
78 #define TCQ_F_NOLOCK 0x100 /* qdisc does not require locking */
79 #define TCQ_F_OFFLOADED 0x200 /* qdisc is offloaded to HW */
81 const struct Qdisc_ops *ops;
82 struct qdisc_size_table __rcu *stab;
83 struct hlist_node hash;
87 struct netdev_queue *dev_queue;
89 struct net_rate_estimator __rcu *rate_est;
90 struct gnet_stats_basic_cpu __percpu *cpu_bstats;
91 struct gnet_stats_queue __percpu *cpu_qstats;
96 * For performance sake on SMP, we put highly modified fields at the end
98 struct sk_buff_head gso_skb ____cacheline_aligned_in_smp;
99 struct qdisc_skb_head q;
100 struct gnet_stats_basic_packed bstats;
102 struct gnet_stats_queue qstats;
104 struct Qdisc *next_sched;
105 struct sk_buff_head skb_bad_txq;
107 spinlock_t busylock ____cacheline_aligned_in_smp;
112 static inline void qdisc_refcount_inc(struct Qdisc *qdisc)
114 if (qdisc->flags & TCQ_F_BUILTIN)
116 refcount_inc(&qdisc->refcnt);
119 /* Intended to be used by unlocked users, when concurrent qdisc release is
123 static inline struct Qdisc *qdisc_refcount_inc_nz(struct Qdisc *qdisc)
125 if (qdisc->flags & TCQ_F_BUILTIN)
127 if (refcount_inc_not_zero(&qdisc->refcnt))
132 static inline bool qdisc_is_running(struct Qdisc *qdisc)
134 if (qdisc->flags & TCQ_F_NOLOCK)
135 return spin_is_locked(&qdisc->seqlock);
136 return (raw_read_seqcount(&qdisc->running) & 1) ? true : false;
139 static inline bool qdisc_run_begin(struct Qdisc *qdisc)
141 if (qdisc->flags & TCQ_F_NOLOCK) {
142 if (!spin_trylock(&qdisc->seqlock))
144 } else if (qdisc_is_running(qdisc)) {
147 /* Variant of write_seqcount_begin() telling lockdep a trylock
150 raw_write_seqcount_begin(&qdisc->running);
151 seqcount_acquire(&qdisc->running.dep_map, 0, 1, _RET_IP_);
155 static inline void qdisc_run_end(struct Qdisc *qdisc)
157 write_seqcount_end(&qdisc->running);
158 if (qdisc->flags & TCQ_F_NOLOCK)
159 spin_unlock(&qdisc->seqlock);
162 static inline bool qdisc_may_bulk(const struct Qdisc *qdisc)
164 return qdisc->flags & TCQ_F_ONETXQUEUE;
167 static inline int qdisc_avail_bulklimit(const struct netdev_queue *txq)
170 /* Non-BQL migrated drivers will return 0, too. */
171 return dql_avail(&txq->dql);
177 struct Qdisc_class_ops {
178 /* Child qdisc manipulation */
179 struct netdev_queue * (*select_queue)(struct Qdisc *, struct tcmsg *);
180 int (*graft)(struct Qdisc *, unsigned long cl,
181 struct Qdisc *, struct Qdisc **,
182 struct netlink_ext_ack *extack);
183 struct Qdisc * (*leaf)(struct Qdisc *, unsigned long cl);
184 void (*qlen_notify)(struct Qdisc *, unsigned long);
186 /* Class manipulation routines */
187 unsigned long (*find)(struct Qdisc *, u32 classid);
188 int (*change)(struct Qdisc *, u32, u32,
189 struct nlattr **, unsigned long *,
190 struct netlink_ext_ack *);
191 int (*delete)(struct Qdisc *, unsigned long);
192 void (*walk)(struct Qdisc *, struct qdisc_walker * arg);
194 /* Filter manipulation */
195 struct tcf_block * (*tcf_block)(struct Qdisc *sch,
197 struct netlink_ext_ack *extack);
198 unsigned long (*bind_tcf)(struct Qdisc *, unsigned long,
200 void (*unbind_tcf)(struct Qdisc *, unsigned long);
202 /* rtnetlink specific */
203 int (*dump)(struct Qdisc *, unsigned long,
204 struct sk_buff *skb, struct tcmsg*);
205 int (*dump_stats)(struct Qdisc *, unsigned long,
210 struct Qdisc_ops *next;
211 const struct Qdisc_class_ops *cl_ops;
214 unsigned int static_flags;
216 int (*enqueue)(struct sk_buff *skb,
218 struct sk_buff **to_free);
219 struct sk_buff * (*dequeue)(struct Qdisc *);
220 struct sk_buff * (*peek)(struct Qdisc *);
222 int (*init)(struct Qdisc *sch, struct nlattr *arg,
223 struct netlink_ext_ack *extack);
224 void (*reset)(struct Qdisc *);
225 void (*destroy)(struct Qdisc *);
226 int (*change)(struct Qdisc *sch,
228 struct netlink_ext_ack *extack);
229 void (*attach)(struct Qdisc *sch);
230 int (*change_tx_queue_len)(struct Qdisc *, unsigned int);
232 int (*dump)(struct Qdisc *, struct sk_buff *);
233 int (*dump_stats)(struct Qdisc *, struct gnet_dump *);
235 void (*ingress_block_set)(struct Qdisc *sch,
237 void (*egress_block_set)(struct Qdisc *sch,
239 u32 (*ingress_block_get)(struct Qdisc *sch);
240 u32 (*egress_block_get)(struct Qdisc *sch);
242 struct module *owner;
252 const struct tcf_proto *goto_tp;
254 /* used by the TC_ACT_REINSERT action */
257 struct gnet_stats_queue *qstats;
264 struct tcf_proto_ops {
265 struct list_head head;
268 int (*classify)(struct sk_buff *,
269 const struct tcf_proto *,
270 struct tcf_result *);
271 int (*init)(struct tcf_proto*);
272 void (*destroy)(struct tcf_proto *tp,
273 struct netlink_ext_ack *extack);
275 void* (*get)(struct tcf_proto*, u32 handle);
276 int (*change)(struct net *net, struct sk_buff *,
277 struct tcf_proto*, unsigned long,
278 u32 handle, struct nlattr **,
280 struct netlink_ext_ack *);
281 int (*delete)(struct tcf_proto *tp, void *arg,
283 struct netlink_ext_ack *);
284 void (*walk)(struct tcf_proto*, struct tcf_walker *arg);
285 int (*reoffload)(struct tcf_proto *tp, bool add,
286 tc_setup_cb_t *cb, void *cb_priv,
287 struct netlink_ext_ack *extack);
288 void (*bind_class)(void *, u32, unsigned long);
289 void * (*tmplt_create)(struct net *net,
290 struct tcf_chain *chain,
292 struct netlink_ext_ack *extack);
293 void (*tmplt_destroy)(void *tmplt_priv);
295 /* rtnetlink specific */
296 int (*dump)(struct net*, struct tcf_proto*, void *,
297 struct sk_buff *skb, struct tcmsg*);
298 int (*tmplt_dump)(struct sk_buff *skb,
302 struct module *owner;
306 /* Fast access part */
307 struct tcf_proto __rcu *next;
310 /* called under RCU BH lock*/
311 int (*classify)(struct sk_buff *,
312 const struct tcf_proto *,
313 struct tcf_result *);
319 const struct tcf_proto_ops *ops;
320 struct tcf_chain *chain;
324 struct qdisc_skb_cb {
327 unsigned int pkt_len;
328 u16 slave_dev_queue_mapping;
331 struct bpf_flow_keys *flow_keys;
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;
369 static inline void tcf_block_offload_inc(struct tcf_block *block, u32 *flags)
371 if (*flags & TCA_CLS_FLAGS_IN_HW)
373 *flags |= TCA_CLS_FLAGS_IN_HW;
377 static inline void tcf_block_offload_dec(struct tcf_block *block, u32 *flags)
379 if (!(*flags & TCA_CLS_FLAGS_IN_HW))
381 *flags &= ~TCA_CLS_FLAGS_IN_HW;
386 tc_cls_offload_cnt_update(struct tcf_block *block, u32 *cnt,
387 u32 *flags, bool add)
391 tcf_block_offload_inc(block, flags);
396 tcf_block_offload_dec(block, flags);
400 static inline void qdisc_cb_private_validate(const struct sk_buff *skb, int sz)
402 struct qdisc_skb_cb *qcb;
404 BUILD_BUG_ON(sizeof(skb->cb) < offsetof(struct qdisc_skb_cb, data) + sz);
405 BUILD_BUG_ON(sizeof(qcb->data) < sz);
408 static inline int qdisc_qlen_cpu(const struct Qdisc *q)
410 return this_cpu_ptr(q->cpu_qstats)->qlen;
413 static inline int qdisc_qlen(const struct Qdisc *q)
418 static inline int qdisc_qlen_sum(const struct Qdisc *q)
420 __u32 qlen = q->qstats.qlen;
423 if (q->flags & TCQ_F_NOLOCK) {
424 for_each_possible_cpu(i)
425 qlen += per_cpu_ptr(q->cpu_qstats, i)->qlen;
433 static inline struct qdisc_skb_cb *qdisc_skb_cb(const struct sk_buff *skb)
435 return (struct qdisc_skb_cb *)skb->cb;
438 static inline spinlock_t *qdisc_lock(struct Qdisc *qdisc)
440 return &qdisc->q.lock;
443 static inline struct Qdisc *qdisc_root(const struct Qdisc *qdisc)
445 struct Qdisc *q = rcu_dereference_rtnl(qdisc->dev_queue->qdisc);
450 static inline struct Qdisc *qdisc_root_sleeping(const struct Qdisc *qdisc)
452 return qdisc->dev_queue->qdisc_sleeping;
455 /* The qdisc root lock is a mechanism by which to top level
456 * of a qdisc tree can be locked from any qdisc node in the
457 * forest. This allows changing the configuration of some
458 * aspect of the qdisc tree while blocking out asynchronous
459 * qdisc access in the packet processing paths.
461 * It is only legal to do this when the root will not change
462 * on us. Otherwise we'll potentially lock the wrong qdisc
463 * root. This is enforced by holding the RTNL semaphore, which
464 * all users of this lock accessor must do.
466 static inline spinlock_t *qdisc_root_lock(const struct Qdisc *qdisc)
468 struct Qdisc *root = qdisc_root(qdisc);
471 return qdisc_lock(root);
474 static inline spinlock_t *qdisc_root_sleeping_lock(const struct Qdisc *qdisc)
476 struct Qdisc *root = qdisc_root_sleeping(qdisc);
479 return qdisc_lock(root);
482 static inline seqcount_t *qdisc_root_sleeping_running(const struct Qdisc *qdisc)
484 struct Qdisc *root = qdisc_root_sleeping(qdisc);
487 return &root->running;
490 static inline struct net_device *qdisc_dev(const struct Qdisc *qdisc)
492 return qdisc->dev_queue->dev;
495 static inline void sch_tree_lock(const struct Qdisc *q)
497 spin_lock_bh(qdisc_root_sleeping_lock(q));
500 static inline void sch_tree_unlock(const struct Qdisc *q)
502 spin_unlock_bh(qdisc_root_sleeping_lock(q));
505 extern struct Qdisc noop_qdisc;
506 extern struct Qdisc_ops noop_qdisc_ops;
507 extern struct Qdisc_ops pfifo_fast_ops;
508 extern struct Qdisc_ops mq_qdisc_ops;
509 extern struct Qdisc_ops noqueue_qdisc_ops;
510 extern const struct Qdisc_ops *default_qdisc_ops;
511 static inline const struct Qdisc_ops *
512 get_default_qdisc_ops(const struct net_device *dev, int ntx)
514 return ntx < dev->real_num_tx_queues ?
515 default_qdisc_ops : &pfifo_fast_ops;
518 struct Qdisc_class_common {
520 struct hlist_node hnode;
523 struct Qdisc_class_hash {
524 struct hlist_head *hash;
525 unsigned int hashsize;
526 unsigned int hashmask;
527 unsigned int hashelems;
530 static inline unsigned int qdisc_class_hash(u32 id, u32 mask)
537 static inline struct Qdisc_class_common *
538 qdisc_class_find(const struct Qdisc_class_hash *hash, u32 id)
540 struct Qdisc_class_common *cl;
546 h = qdisc_class_hash(id, hash->hashmask);
547 hlist_for_each_entry(cl, &hash->hash[h], hnode) {
548 if (cl->classid == id)
554 static inline int tc_classid_to_hwtc(struct net_device *dev, u32 classid)
556 u32 hwtc = TC_H_MIN(classid) - TC_H_MIN_PRIORITY;
558 return (hwtc < netdev_get_num_tc(dev)) ? hwtc : -EINVAL;
561 int qdisc_class_hash_init(struct Qdisc_class_hash *);
562 void qdisc_class_hash_insert(struct Qdisc_class_hash *,
563 struct Qdisc_class_common *);
564 void qdisc_class_hash_remove(struct Qdisc_class_hash *,
565 struct Qdisc_class_common *);
566 void qdisc_class_hash_grow(struct Qdisc *, struct Qdisc_class_hash *);
567 void qdisc_class_hash_destroy(struct Qdisc_class_hash *);
569 int dev_qdisc_change_tx_queue_len(struct net_device *dev);
570 void dev_init_scheduler(struct net_device *dev);
571 void dev_shutdown(struct net_device *dev);
572 void dev_activate(struct net_device *dev);
573 void dev_deactivate(struct net_device *dev);
574 void dev_deactivate_many(struct list_head *head);
575 struct Qdisc *dev_graft_qdisc(struct netdev_queue *dev_queue,
576 struct Qdisc *qdisc);
577 void qdisc_reset(struct Qdisc *qdisc);
578 void qdisc_put(struct Qdisc *qdisc);
579 void qdisc_put_unlocked(struct Qdisc *qdisc);
580 void qdisc_tree_reduce_backlog(struct Qdisc *qdisc, unsigned int n,
582 struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue,
583 const struct Qdisc_ops *ops,
584 struct netlink_ext_ack *extack);
585 void qdisc_free(struct Qdisc *qdisc);
586 struct Qdisc *qdisc_create_dflt(struct netdev_queue *dev_queue,
587 const struct Qdisc_ops *ops, u32 parentid,
588 struct netlink_ext_ack *extack);
589 void __qdisc_calculate_pkt_len(struct sk_buff *skb,
590 const struct qdisc_size_table *stab);
591 int skb_do_redirect(struct sk_buff *);
593 static inline void skb_reset_tc(struct sk_buff *skb)
595 #ifdef CONFIG_NET_CLS_ACT
596 skb->tc_redirected = 0;
600 static inline bool skb_is_tc_redirected(const struct sk_buff *skb)
602 #ifdef CONFIG_NET_CLS_ACT
603 return skb->tc_redirected;
609 static inline bool skb_at_tc_ingress(const struct sk_buff *skb)
611 #ifdef CONFIG_NET_CLS_ACT
612 return skb->tc_at_ingress;
618 static inline bool skb_skip_tc_classify(struct sk_buff *skb)
620 #ifdef CONFIG_NET_CLS_ACT
621 if (skb->tc_skip_classify) {
622 skb->tc_skip_classify = 0;
629 /* Reset all TX qdiscs greater than index of a device. */
630 static inline void qdisc_reset_all_tx_gt(struct net_device *dev, unsigned int i)
634 for (; i < dev->num_tx_queues; i++) {
635 qdisc = rtnl_dereference(netdev_get_tx_queue(dev, i)->qdisc);
637 spin_lock_bh(qdisc_lock(qdisc));
639 spin_unlock_bh(qdisc_lock(qdisc));
644 static inline void qdisc_reset_all_tx(struct net_device *dev)
646 qdisc_reset_all_tx_gt(dev, 0);
649 /* Are all TX queues of the device empty? */
650 static inline bool qdisc_all_tx_empty(const struct net_device *dev)
655 for (i = 0; i < dev->num_tx_queues; i++) {
656 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
657 const struct Qdisc *q = rcu_dereference(txq->qdisc);
668 /* Are any of the TX qdiscs changing? */
669 static inline bool qdisc_tx_changing(const struct net_device *dev)
673 for (i = 0; i < dev->num_tx_queues; i++) {
674 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
675 if (rcu_access_pointer(txq->qdisc) != txq->qdisc_sleeping)
681 /* Is the device using the noop qdisc on all queues? */
682 static inline bool qdisc_tx_is_noop(const struct net_device *dev)
686 for (i = 0; i < dev->num_tx_queues; i++) {
687 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
688 if (rcu_access_pointer(txq->qdisc) != &noop_qdisc)
694 static inline unsigned int qdisc_pkt_len(const struct sk_buff *skb)
696 return qdisc_skb_cb(skb)->pkt_len;
699 /* additional qdisc xmit flags (NET_XMIT_MASK in linux/netdevice.h) */
700 enum net_xmit_qdisc_t {
701 __NET_XMIT_STOLEN = 0x00010000,
702 __NET_XMIT_BYPASS = 0x00020000,
705 #ifdef CONFIG_NET_CLS_ACT
706 #define net_xmit_drop_count(e) ((e) & __NET_XMIT_STOLEN ? 0 : 1)
708 #define net_xmit_drop_count(e) (1)
711 static inline void qdisc_calculate_pkt_len(struct sk_buff *skb,
712 const struct Qdisc *sch)
714 #ifdef CONFIG_NET_SCHED
715 struct qdisc_size_table *stab = rcu_dereference_bh(sch->stab);
718 __qdisc_calculate_pkt_len(skb, stab);
722 static inline int qdisc_enqueue(struct sk_buff *skb, struct Qdisc *sch,
723 struct sk_buff **to_free)
725 qdisc_calculate_pkt_len(skb, sch);
726 return sch->enqueue(skb, sch, to_free);
729 static inline bool qdisc_is_percpu_stats(const struct Qdisc *q)
731 return q->flags & TCQ_F_CPUSTATS;
734 static inline void _bstats_update(struct gnet_stats_basic_packed *bstats,
735 __u64 bytes, __u32 packets)
737 bstats->bytes += bytes;
738 bstats->packets += packets;
741 static inline void bstats_update(struct gnet_stats_basic_packed *bstats,
742 const struct sk_buff *skb)
744 _bstats_update(bstats,
746 skb_is_gso(skb) ? skb_shinfo(skb)->gso_segs : 1);
749 static inline void _bstats_cpu_update(struct gnet_stats_basic_cpu *bstats,
750 __u64 bytes, __u32 packets)
752 u64_stats_update_begin(&bstats->syncp);
753 _bstats_update(&bstats->bstats, bytes, packets);
754 u64_stats_update_end(&bstats->syncp);
757 static inline void bstats_cpu_update(struct gnet_stats_basic_cpu *bstats,
758 const struct sk_buff *skb)
760 u64_stats_update_begin(&bstats->syncp);
761 bstats_update(&bstats->bstats, skb);
762 u64_stats_update_end(&bstats->syncp);
765 static inline void qdisc_bstats_cpu_update(struct Qdisc *sch,
766 const struct sk_buff *skb)
768 bstats_cpu_update(this_cpu_ptr(sch->cpu_bstats), skb);
771 static inline void qdisc_bstats_update(struct Qdisc *sch,
772 const struct sk_buff *skb)
774 bstats_update(&sch->bstats, skb);
777 static inline void qdisc_qstats_backlog_dec(struct Qdisc *sch,
778 const struct sk_buff *skb)
780 sch->qstats.backlog -= qdisc_pkt_len(skb);
783 static inline void qdisc_qstats_cpu_backlog_dec(struct Qdisc *sch,
784 const struct sk_buff *skb)
786 this_cpu_sub(sch->cpu_qstats->backlog, qdisc_pkt_len(skb));
789 static inline void qdisc_qstats_backlog_inc(struct Qdisc *sch,
790 const struct sk_buff *skb)
792 sch->qstats.backlog += qdisc_pkt_len(skb);
795 static inline void qdisc_qstats_cpu_backlog_inc(struct Qdisc *sch,
796 const struct sk_buff *skb)
798 this_cpu_add(sch->cpu_qstats->backlog, qdisc_pkt_len(skb));
801 static inline void qdisc_qstats_cpu_qlen_inc(struct Qdisc *sch)
803 this_cpu_inc(sch->cpu_qstats->qlen);
806 static inline void qdisc_qstats_cpu_qlen_dec(struct Qdisc *sch)
808 this_cpu_dec(sch->cpu_qstats->qlen);
811 static inline void qdisc_qstats_cpu_requeues_inc(struct Qdisc *sch)
813 this_cpu_inc(sch->cpu_qstats->requeues);
816 static inline void __qdisc_qstats_drop(struct Qdisc *sch, int count)
818 sch->qstats.drops += count;
821 static inline void qstats_drop_inc(struct gnet_stats_queue *qstats)
826 static inline void qstats_overlimit_inc(struct gnet_stats_queue *qstats)
828 qstats->overlimits++;
831 static inline void qdisc_qstats_drop(struct Qdisc *sch)
833 qstats_drop_inc(&sch->qstats);
836 static inline void qdisc_qstats_cpu_drop(struct Qdisc *sch)
838 this_cpu_inc(sch->cpu_qstats->drops);
841 static inline void qdisc_qstats_overlimit(struct Qdisc *sch)
843 sch->qstats.overlimits++;
846 static inline void qdisc_skb_head_init(struct qdisc_skb_head *qh)
853 static inline void __qdisc_enqueue_tail(struct sk_buff *skb,
854 struct qdisc_skb_head *qh)
856 struct sk_buff *last = qh->tail;
869 static inline int qdisc_enqueue_tail(struct sk_buff *skb, struct Qdisc *sch)
871 __qdisc_enqueue_tail(skb, &sch->q);
872 qdisc_qstats_backlog_inc(sch, skb);
873 return NET_XMIT_SUCCESS;
876 static inline void __qdisc_enqueue_head(struct sk_buff *skb,
877 struct qdisc_skb_head *qh)
879 skb->next = qh->head;
887 static inline struct sk_buff *__qdisc_dequeue_head(struct qdisc_skb_head *qh)
889 struct sk_buff *skb = qh->head;
891 if (likely(skb != NULL)) {
892 qh->head = skb->next;
894 if (qh->head == NULL)
902 static inline struct sk_buff *qdisc_dequeue_head(struct Qdisc *sch)
904 struct sk_buff *skb = __qdisc_dequeue_head(&sch->q);
906 if (likely(skb != NULL)) {
907 qdisc_qstats_backlog_dec(sch, skb);
908 qdisc_bstats_update(sch, skb);
914 /* Instead of calling kfree_skb() while root qdisc lock is held,
915 * queue the skb for future freeing at end of __dev_xmit_skb()
917 static inline void __qdisc_drop(struct sk_buff *skb, struct sk_buff **to_free)
919 skb->next = *to_free;
923 static inline void __qdisc_drop_all(struct sk_buff *skb,
924 struct sk_buff **to_free)
927 skb->prev->next = *to_free;
929 skb->next = *to_free;
933 static inline unsigned int __qdisc_queue_drop_head(struct Qdisc *sch,
934 struct qdisc_skb_head *qh,
935 struct sk_buff **to_free)
937 struct sk_buff *skb = __qdisc_dequeue_head(qh);
939 if (likely(skb != NULL)) {
940 unsigned int len = qdisc_pkt_len(skb);
942 qdisc_qstats_backlog_dec(sch, skb);
943 __qdisc_drop(skb, to_free);
950 static inline unsigned int qdisc_queue_drop_head(struct Qdisc *sch,
951 struct sk_buff **to_free)
953 return __qdisc_queue_drop_head(sch, &sch->q, to_free);
956 static inline struct sk_buff *qdisc_peek_head(struct Qdisc *sch)
958 const struct qdisc_skb_head *qh = &sch->q;
963 /* generic pseudo peek method for non-work-conserving qdisc */
964 static inline struct sk_buff *qdisc_peek_dequeued(struct Qdisc *sch)
966 struct sk_buff *skb = skb_peek(&sch->gso_skb);
968 /* we can reuse ->gso_skb because peek isn't called for root qdiscs */
970 skb = sch->dequeue(sch);
973 __skb_queue_head(&sch->gso_skb, skb);
974 /* it's still part of the queue */
975 qdisc_qstats_backlog_inc(sch, skb);
983 /* use instead of qdisc->dequeue() for all qdiscs queried with ->peek() */
984 static inline struct sk_buff *qdisc_dequeue_peeked(struct Qdisc *sch)
986 struct sk_buff *skb = skb_peek(&sch->gso_skb);
989 skb = __skb_dequeue(&sch->gso_skb);
990 qdisc_qstats_backlog_dec(sch, skb);
993 skb = sch->dequeue(sch);
999 static inline void __qdisc_reset_queue(struct qdisc_skb_head *qh)
1002 * We do not know the backlog in bytes of this list, it
1003 * is up to the caller to correct it
1007 rtnl_kfree_skbs(qh->head, qh->tail);
1015 static inline void qdisc_reset_queue(struct Qdisc *sch)
1017 __qdisc_reset_queue(&sch->q);
1018 sch->qstats.backlog = 0;
1021 static inline struct Qdisc *qdisc_replace(struct Qdisc *sch, struct Qdisc *new,
1022 struct Qdisc **pold)
1030 unsigned int qlen = old->q.qlen;
1031 unsigned int backlog = old->qstats.backlog;
1034 qdisc_tree_reduce_backlog(old, qlen, backlog);
1036 sch_tree_unlock(sch);
1041 static inline void rtnl_qdisc_drop(struct sk_buff *skb, struct Qdisc *sch)
1043 rtnl_kfree_skbs(skb, skb);
1044 qdisc_qstats_drop(sch);
1047 static inline int qdisc_drop_cpu(struct sk_buff *skb, struct Qdisc *sch,
1048 struct sk_buff **to_free)
1050 __qdisc_drop(skb, to_free);
1051 qdisc_qstats_cpu_drop(sch);
1053 return NET_XMIT_DROP;
1056 static inline int qdisc_drop(struct sk_buff *skb, struct Qdisc *sch,
1057 struct sk_buff **to_free)
1059 __qdisc_drop(skb, to_free);
1060 qdisc_qstats_drop(sch);
1062 return NET_XMIT_DROP;
1065 static inline int qdisc_drop_all(struct sk_buff *skb, struct Qdisc *sch,
1066 struct sk_buff **to_free)
1068 __qdisc_drop_all(skb, to_free);
1069 qdisc_qstats_drop(sch);
1071 return NET_XMIT_DROP;
1074 /* Length to Time (L2T) lookup in a qdisc_rate_table, to determine how
1075 long it will take to send a packet given its size.
1077 static inline u32 qdisc_l2t(struct qdisc_rate_table* rtab, unsigned int pktlen)
1079 int slot = pktlen + rtab->rate.cell_align + rtab->rate.overhead;
1082 slot >>= rtab->rate.cell_log;
1084 return rtab->data[255]*(slot >> 8) + rtab->data[slot & 0xFF];
1085 return rtab->data[slot];
1088 struct psched_ratecfg {
1089 u64 rate_bytes_ps; /* bytes per second */
1096 static inline u64 psched_l2t_ns(const struct psched_ratecfg *r,
1101 if (unlikely(r->linklayer == TC_LINKLAYER_ATM))
1102 return ((u64)(DIV_ROUND_UP(len,48)*53) * r->mult) >> r->shift;
1104 return ((u64)len * r->mult) >> r->shift;
1107 void psched_ratecfg_precompute(struct psched_ratecfg *r,
1108 const struct tc_ratespec *conf,
1111 static inline void psched_ratecfg_getrate(struct tc_ratespec *res,
1112 const struct psched_ratecfg *r)
1114 memset(res, 0, sizeof(*res));
1116 /* legacy struct tc_ratespec has a 32bit @rate field
1117 * Qdisc using 64bit rate should add new attributes
1118 * in order to maintain compatibility.
1120 res->rate = min_t(u64, r->rate_bytes_ps, ~0U);
1122 res->overhead = r->overhead;
1123 res->linklayer = (r->linklayer & TC_LINKLAYER_MASK);
1126 /* Mini Qdisc serves for specific needs of ingress/clsact Qdisc.
1127 * The fast path only needs to access filter list and to update stats
1130 struct tcf_proto *filter_list;
1131 struct gnet_stats_basic_cpu __percpu *cpu_bstats;
1132 struct gnet_stats_queue __percpu *cpu_qstats;
1133 struct rcu_head rcu;
1136 static inline void mini_qdisc_bstats_cpu_update(struct mini_Qdisc *miniq,
1137 const struct sk_buff *skb)
1139 bstats_cpu_update(this_cpu_ptr(miniq->cpu_bstats), skb);
1142 static inline void mini_qdisc_qstats_cpu_drop(struct mini_Qdisc *miniq)
1144 this_cpu_inc(miniq->cpu_qstats->drops);
1147 struct mini_Qdisc_pair {
1148 struct mini_Qdisc miniq1;
1149 struct mini_Qdisc miniq2;
1150 struct mini_Qdisc __rcu **p_miniq;
1153 void mini_qdisc_pair_swap(struct mini_Qdisc_pair *miniqp,
1154 struct tcf_proto *tp_head);
1155 void mini_qdisc_pair_init(struct mini_Qdisc_pair *miniqp, struct Qdisc *qdisc,
1156 struct mini_Qdisc __rcu **p_miniq);
1158 static inline void skb_tc_reinsert(struct sk_buff *skb, struct tcf_result *res)
1160 struct gnet_stats_queue *stats = res->qstats;
1164 ret = netif_receive_skb(skb);
1166 ret = dev_queue_xmit(skb);
1168 qstats_overlimit_inc(res->qstats);