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 <linux/mutex.h>
16 #include <net/gen_stats.h>
17 #include <net/rtnetlink.h>
25 typedef int tc_setup_cb_t(enum tc_setup_type type,
26 void *type_data, void *cb_priv);
28 typedef int tc_indr_block_bind_cb_t(struct net_device *dev, void *cb_priv,
29 enum tc_setup_type type, void *type_data);
31 struct qdisc_rate_table {
32 struct tc_ratespec rate;
34 struct qdisc_rate_table *next;
40 __QDISC_STATE_DEACTIVATED,
43 struct qdisc_size_table {
45 struct list_head list;
46 struct tc_sizespec szopts;
51 /* similar to sk_buff_head, but skb->prev pointer is undefined. */
52 struct qdisc_skb_head {
60 int (*enqueue)(struct sk_buff *skb,
62 struct sk_buff **to_free);
63 struct sk_buff * (*dequeue)(struct Qdisc *sch);
65 #define TCQ_F_BUILTIN 1
66 #define TCQ_F_INGRESS 2
67 #define TCQ_F_CAN_BYPASS 4
68 #define TCQ_F_MQROOT 8
69 #define TCQ_F_ONETXQUEUE 0x10 /* dequeue_skb() can assume all skbs are for
70 * q->dev_queue : It can test
71 * netif_xmit_frozen_or_stopped() before
72 * dequeueing next packet.
73 * Its true for MQ/MQPRIO slaves, or non
76 #define TCQ_F_WARN_NONWC (1 << 16)
77 #define TCQ_F_CPUSTATS 0x20 /* run using percpu statistics */
78 #define TCQ_F_NOPARENT 0x40 /* root of its hierarchy :
79 * qdisc_tree_decrease_qlen() should stop.
81 #define TCQ_F_INVISIBLE 0x80 /* invisible by default in dump */
82 #define TCQ_F_NOLOCK 0x100 /* qdisc does not require locking */
83 #define TCQ_F_OFFLOADED 0x200 /* qdisc is offloaded to HW */
85 const struct Qdisc_ops *ops;
86 struct qdisc_size_table __rcu *stab;
87 struct hlist_node hash;
91 struct netdev_queue *dev_queue;
93 struct net_rate_estimator __rcu *rate_est;
94 struct gnet_stats_basic_cpu __percpu *cpu_bstats;
95 struct gnet_stats_queue __percpu *cpu_qstats;
100 * For performance sake on SMP, we put highly modified fields at the end
102 struct sk_buff_head gso_skb ____cacheline_aligned_in_smp;
103 struct qdisc_skb_head q;
104 struct gnet_stats_basic_packed bstats;
106 struct gnet_stats_queue qstats;
108 struct Qdisc *next_sched;
109 struct sk_buff_head skb_bad_txq;
111 spinlock_t busylock ____cacheline_aligned_in_smp;
116 static inline void qdisc_refcount_inc(struct Qdisc *qdisc)
118 if (qdisc->flags & TCQ_F_BUILTIN)
120 refcount_inc(&qdisc->refcnt);
123 /* Intended to be used by unlocked users, when concurrent qdisc release is
127 static inline struct Qdisc *qdisc_refcount_inc_nz(struct Qdisc *qdisc)
129 if (qdisc->flags & TCQ_F_BUILTIN)
131 if (refcount_inc_not_zero(&qdisc->refcnt))
136 static inline bool qdisc_is_running(struct Qdisc *qdisc)
138 if (qdisc->flags & TCQ_F_NOLOCK)
139 return spin_is_locked(&qdisc->seqlock);
140 return (raw_read_seqcount(&qdisc->running) & 1) ? true : false;
143 static inline bool qdisc_run_begin(struct Qdisc *qdisc)
145 if (qdisc->flags & TCQ_F_NOLOCK) {
146 if (!spin_trylock(&qdisc->seqlock))
148 } else if (qdisc_is_running(qdisc)) {
151 /* Variant of write_seqcount_begin() telling lockdep a trylock
154 raw_write_seqcount_begin(&qdisc->running);
155 seqcount_acquire(&qdisc->running.dep_map, 0, 1, _RET_IP_);
159 static inline void qdisc_run_end(struct Qdisc *qdisc)
161 write_seqcount_end(&qdisc->running);
162 if (qdisc->flags & TCQ_F_NOLOCK)
163 spin_unlock(&qdisc->seqlock);
166 static inline bool qdisc_may_bulk(const struct Qdisc *qdisc)
168 return qdisc->flags & TCQ_F_ONETXQUEUE;
171 static inline int qdisc_avail_bulklimit(const struct netdev_queue *txq)
174 /* Non-BQL migrated drivers will return 0, too. */
175 return dql_avail(&txq->dql);
181 struct Qdisc_class_ops {
182 /* Child qdisc manipulation */
183 struct netdev_queue * (*select_queue)(struct Qdisc *, struct tcmsg *);
184 int (*graft)(struct Qdisc *, unsigned long cl,
185 struct Qdisc *, struct Qdisc **,
186 struct netlink_ext_ack *extack);
187 struct Qdisc * (*leaf)(struct Qdisc *, unsigned long cl);
188 void (*qlen_notify)(struct Qdisc *, unsigned long);
190 /* Class manipulation routines */
191 unsigned long (*find)(struct Qdisc *, u32 classid);
192 int (*change)(struct Qdisc *, u32, u32,
193 struct nlattr **, unsigned long *,
194 struct netlink_ext_ack *);
195 int (*delete)(struct Qdisc *, unsigned long);
196 void (*walk)(struct Qdisc *, struct qdisc_walker * arg);
198 /* Filter manipulation */
199 struct tcf_block * (*tcf_block)(struct Qdisc *sch,
201 struct netlink_ext_ack *extack);
202 unsigned long (*bind_tcf)(struct Qdisc *, unsigned long,
204 void (*unbind_tcf)(struct Qdisc *, unsigned long);
206 /* rtnetlink specific */
207 int (*dump)(struct Qdisc *, unsigned long,
208 struct sk_buff *skb, struct tcmsg*);
209 int (*dump_stats)(struct Qdisc *, unsigned long,
214 struct Qdisc_ops *next;
215 const struct Qdisc_class_ops *cl_ops;
218 unsigned int static_flags;
220 int (*enqueue)(struct sk_buff *skb,
222 struct sk_buff **to_free);
223 struct sk_buff * (*dequeue)(struct Qdisc *);
224 struct sk_buff * (*peek)(struct Qdisc *);
226 int (*init)(struct Qdisc *sch, struct nlattr *arg,
227 struct netlink_ext_ack *extack);
228 void (*reset)(struct Qdisc *);
229 void (*destroy)(struct Qdisc *);
230 int (*change)(struct Qdisc *sch,
232 struct netlink_ext_ack *extack);
233 void (*attach)(struct Qdisc *sch);
234 int (*change_tx_queue_len)(struct Qdisc *, unsigned int);
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 * (*tmplt_create)(struct net *net,
294 struct tcf_chain *chain,
296 struct netlink_ext_ack *extack);
297 void (*tmplt_destroy)(void *tmplt_priv);
299 /* rtnetlink specific */
300 int (*dump)(struct net*, struct tcf_proto*, void *,
301 struct sk_buff *skb, struct tcmsg*);
302 int (*tmplt_dump)(struct sk_buff *skb,
306 struct module *owner;
310 /* Fast access part */
311 struct tcf_proto __rcu *next;
314 /* called under RCU BH lock*/
315 int (*classify)(struct sk_buff *,
316 const struct tcf_proto *,
317 struct tcf_result *);
323 const struct tcf_proto_ops *ops;
324 struct tcf_chain *chain;
325 /* Lock protects tcf_proto shared state and can be used by unlocked
326 * classifiers to protect their private data.
334 struct qdisc_skb_cb {
337 unsigned int pkt_len;
338 u16 slave_dev_queue_mapping;
341 struct bpf_flow_keys *flow_keys;
343 #define QDISC_CB_PRIV_LEN 20
344 unsigned char data[QDISC_CB_PRIV_LEN];
347 typedef void tcf_chain_head_change_t(struct tcf_proto *tp_head, void *priv);
350 /* Protects filter_chain. */
351 struct mutex filter_chain_lock;
352 struct tcf_proto __rcu *filter_chain;
353 struct list_head list;
354 struct tcf_block *block;
355 u32 index; /* chain index */
357 unsigned int action_refcnt;
358 bool explicitly_created;
359 const struct tcf_proto_ops *tmplt_ops;
364 /* Lock protects tcf_block and lifetime-management data of chains
365 * attached to the block (refcnt, action_refcnt, explicitly_created).
368 struct list_head chain_list;
369 u32 index; /* block index for shared blocks */
373 struct list_head cb_list;
374 struct list_head owner_list;
376 unsigned int offloadcnt; /* Number of oddloaded filters */
377 unsigned int nooffloaddevcnt; /* Number of devs unable to do offload */
379 struct tcf_chain *chain;
380 struct list_head filter_chain_list;
385 #ifdef CONFIG_PROVE_LOCKING
386 static inline bool lockdep_tcf_chain_is_locked(struct tcf_chain *chain)
388 return lockdep_is_held(&chain->filter_chain_lock);
391 static inline bool lockdep_tcf_proto_is_locked(struct tcf_proto *tp)
393 return lockdep_is_held(&tp->lock);
396 static inline bool lockdep_tcf_chain_is_locked(struct tcf_block *chain)
401 static inline bool lockdep_tcf_proto_is_locked(struct tcf_proto *tp)
405 #endif /* #ifdef CONFIG_PROVE_LOCKING */
407 #define tcf_chain_dereference(p, chain) \
408 rcu_dereference_protected(p, lockdep_tcf_chain_is_locked(chain))
410 #define tcf_proto_dereference(p, tp) \
411 rcu_dereference_protected(p, lockdep_tcf_proto_is_locked(tp))
413 static inline void tcf_block_offload_inc(struct tcf_block *block, u32 *flags)
415 if (*flags & TCA_CLS_FLAGS_IN_HW)
417 *flags |= TCA_CLS_FLAGS_IN_HW;
421 static inline void tcf_block_offload_dec(struct tcf_block *block, u32 *flags)
423 if (!(*flags & TCA_CLS_FLAGS_IN_HW))
425 *flags &= ~TCA_CLS_FLAGS_IN_HW;
430 tc_cls_offload_cnt_update(struct tcf_block *block, u32 *cnt,
431 u32 *flags, bool add)
435 tcf_block_offload_inc(block, flags);
440 tcf_block_offload_dec(block, flags);
444 static inline void qdisc_cb_private_validate(const struct sk_buff *skb, int sz)
446 struct qdisc_skb_cb *qcb;
448 BUILD_BUG_ON(sizeof(skb->cb) < offsetof(struct qdisc_skb_cb, data) + sz);
449 BUILD_BUG_ON(sizeof(qcb->data) < sz);
452 static inline int qdisc_qlen_cpu(const struct Qdisc *q)
454 return this_cpu_ptr(q->cpu_qstats)->qlen;
457 static inline int qdisc_qlen(const struct Qdisc *q)
462 static inline int qdisc_qlen_sum(const struct Qdisc *q)
464 __u32 qlen = q->qstats.qlen;
467 if (q->flags & TCQ_F_NOLOCK) {
468 for_each_possible_cpu(i)
469 qlen += per_cpu_ptr(q->cpu_qstats, i)->qlen;
477 static inline struct qdisc_skb_cb *qdisc_skb_cb(const struct sk_buff *skb)
479 return (struct qdisc_skb_cb *)skb->cb;
482 static inline spinlock_t *qdisc_lock(struct Qdisc *qdisc)
484 return &qdisc->q.lock;
487 static inline struct Qdisc *qdisc_root(const struct Qdisc *qdisc)
489 struct Qdisc *q = rcu_dereference_rtnl(qdisc->dev_queue->qdisc);
494 static inline struct Qdisc *qdisc_root_sleeping(const struct Qdisc *qdisc)
496 return qdisc->dev_queue->qdisc_sleeping;
499 /* The qdisc root lock is a mechanism by which to top level
500 * of a qdisc tree can be locked from any qdisc node in the
501 * forest. This allows changing the configuration of some
502 * aspect of the qdisc tree while blocking out asynchronous
503 * qdisc access in the packet processing paths.
505 * It is only legal to do this when the root will not change
506 * on us. Otherwise we'll potentially lock the wrong qdisc
507 * root. This is enforced by holding the RTNL semaphore, which
508 * all users of this lock accessor must do.
510 static inline spinlock_t *qdisc_root_lock(const struct Qdisc *qdisc)
512 struct Qdisc *root = qdisc_root(qdisc);
515 return qdisc_lock(root);
518 static inline spinlock_t *qdisc_root_sleeping_lock(const struct Qdisc *qdisc)
520 struct Qdisc *root = qdisc_root_sleeping(qdisc);
523 return qdisc_lock(root);
526 static inline seqcount_t *qdisc_root_sleeping_running(const struct Qdisc *qdisc)
528 struct Qdisc *root = qdisc_root_sleeping(qdisc);
531 return &root->running;
534 static inline struct net_device *qdisc_dev(const struct Qdisc *qdisc)
536 return qdisc->dev_queue->dev;
539 static inline void sch_tree_lock(const struct Qdisc *q)
541 spin_lock_bh(qdisc_root_sleeping_lock(q));
544 static inline void sch_tree_unlock(const struct Qdisc *q)
546 spin_unlock_bh(qdisc_root_sleeping_lock(q));
549 extern struct Qdisc noop_qdisc;
550 extern struct Qdisc_ops noop_qdisc_ops;
551 extern struct Qdisc_ops pfifo_fast_ops;
552 extern struct Qdisc_ops mq_qdisc_ops;
553 extern struct Qdisc_ops noqueue_qdisc_ops;
554 extern const struct Qdisc_ops *default_qdisc_ops;
555 static inline const struct Qdisc_ops *
556 get_default_qdisc_ops(const struct net_device *dev, int ntx)
558 return ntx < dev->real_num_tx_queues ?
559 default_qdisc_ops : &pfifo_fast_ops;
562 struct Qdisc_class_common {
564 struct hlist_node hnode;
567 struct Qdisc_class_hash {
568 struct hlist_head *hash;
569 unsigned int hashsize;
570 unsigned int hashmask;
571 unsigned int hashelems;
574 static inline unsigned int qdisc_class_hash(u32 id, u32 mask)
581 static inline struct Qdisc_class_common *
582 qdisc_class_find(const struct Qdisc_class_hash *hash, u32 id)
584 struct Qdisc_class_common *cl;
590 h = qdisc_class_hash(id, hash->hashmask);
591 hlist_for_each_entry(cl, &hash->hash[h], hnode) {
592 if (cl->classid == id)
598 static inline int tc_classid_to_hwtc(struct net_device *dev, u32 classid)
600 u32 hwtc = TC_H_MIN(classid) - TC_H_MIN_PRIORITY;
602 return (hwtc < netdev_get_num_tc(dev)) ? hwtc : -EINVAL;
605 int qdisc_class_hash_init(struct Qdisc_class_hash *);
606 void qdisc_class_hash_insert(struct Qdisc_class_hash *,
607 struct Qdisc_class_common *);
608 void qdisc_class_hash_remove(struct Qdisc_class_hash *,
609 struct Qdisc_class_common *);
610 void qdisc_class_hash_grow(struct Qdisc *, struct Qdisc_class_hash *);
611 void qdisc_class_hash_destroy(struct Qdisc_class_hash *);
613 int dev_qdisc_change_tx_queue_len(struct net_device *dev);
614 void dev_init_scheduler(struct net_device *dev);
615 void dev_shutdown(struct net_device *dev);
616 void dev_activate(struct net_device *dev);
617 void dev_deactivate(struct net_device *dev);
618 void dev_deactivate_many(struct list_head *head);
619 struct Qdisc *dev_graft_qdisc(struct netdev_queue *dev_queue,
620 struct Qdisc *qdisc);
621 void qdisc_reset(struct Qdisc *qdisc);
622 void qdisc_put(struct Qdisc *qdisc);
623 void qdisc_put_unlocked(struct Qdisc *qdisc);
624 void qdisc_tree_reduce_backlog(struct Qdisc *qdisc, int n, int len);
625 #ifdef CONFIG_NET_SCHED
626 int qdisc_offload_dump_helper(struct Qdisc *q, enum tc_setup_type type,
628 void qdisc_offload_graft_helper(struct net_device *dev, struct Qdisc *sch,
629 struct Qdisc *new, struct Qdisc *old,
630 enum tc_setup_type type, void *type_data,
631 struct netlink_ext_ack *extack);
634 qdisc_offload_dump_helper(struct Qdisc *q, enum tc_setup_type type,
637 q->flags &= ~TCQ_F_OFFLOADED;
642 qdisc_offload_graft_helper(struct net_device *dev, struct Qdisc *sch,
643 struct Qdisc *new, struct Qdisc *old,
644 enum tc_setup_type type, void *type_data,
645 struct netlink_ext_ack *extack)
649 struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue,
650 const struct Qdisc_ops *ops,
651 struct netlink_ext_ack *extack);
652 void qdisc_free(struct Qdisc *qdisc);
653 struct Qdisc *qdisc_create_dflt(struct netdev_queue *dev_queue,
654 const struct Qdisc_ops *ops, u32 parentid,
655 struct netlink_ext_ack *extack);
656 void __qdisc_calculate_pkt_len(struct sk_buff *skb,
657 const struct qdisc_size_table *stab);
658 int skb_do_redirect(struct sk_buff *);
660 static inline void skb_reset_tc(struct sk_buff *skb)
662 #ifdef CONFIG_NET_CLS_ACT
663 skb->tc_redirected = 0;
667 static inline bool skb_is_tc_redirected(const struct sk_buff *skb)
669 #ifdef CONFIG_NET_CLS_ACT
670 return skb->tc_redirected;
676 static inline bool skb_at_tc_ingress(const struct sk_buff *skb)
678 #ifdef CONFIG_NET_CLS_ACT
679 return skb->tc_at_ingress;
685 static inline bool skb_skip_tc_classify(struct sk_buff *skb)
687 #ifdef CONFIG_NET_CLS_ACT
688 if (skb->tc_skip_classify) {
689 skb->tc_skip_classify = 0;
696 /* Reset all TX qdiscs greater than index of a device. */
697 static inline void qdisc_reset_all_tx_gt(struct net_device *dev, unsigned int i)
701 for (; i < dev->num_tx_queues; i++) {
702 qdisc = rtnl_dereference(netdev_get_tx_queue(dev, i)->qdisc);
704 spin_lock_bh(qdisc_lock(qdisc));
706 spin_unlock_bh(qdisc_lock(qdisc));
711 static inline void qdisc_reset_all_tx(struct net_device *dev)
713 qdisc_reset_all_tx_gt(dev, 0);
716 /* Are all TX queues of the device empty? */
717 static inline bool qdisc_all_tx_empty(const struct net_device *dev)
722 for (i = 0; i < dev->num_tx_queues; i++) {
723 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
724 const struct Qdisc *q = rcu_dereference(txq->qdisc);
735 /* Are any of the TX qdiscs changing? */
736 static inline bool qdisc_tx_changing(const struct net_device *dev)
740 for (i = 0; i < dev->num_tx_queues; i++) {
741 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
742 if (rcu_access_pointer(txq->qdisc) != txq->qdisc_sleeping)
748 /* Is the device using the noop qdisc on all queues? */
749 static inline bool qdisc_tx_is_noop(const struct net_device *dev)
753 for (i = 0; i < dev->num_tx_queues; i++) {
754 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
755 if (rcu_access_pointer(txq->qdisc) != &noop_qdisc)
761 static inline unsigned int qdisc_pkt_len(const struct sk_buff *skb)
763 return qdisc_skb_cb(skb)->pkt_len;
766 /* additional qdisc xmit flags (NET_XMIT_MASK in linux/netdevice.h) */
767 enum net_xmit_qdisc_t {
768 __NET_XMIT_STOLEN = 0x00010000,
769 __NET_XMIT_BYPASS = 0x00020000,
772 #ifdef CONFIG_NET_CLS_ACT
773 #define net_xmit_drop_count(e) ((e) & __NET_XMIT_STOLEN ? 0 : 1)
775 #define net_xmit_drop_count(e) (1)
778 static inline void qdisc_calculate_pkt_len(struct sk_buff *skb,
779 const struct Qdisc *sch)
781 #ifdef CONFIG_NET_SCHED
782 struct qdisc_size_table *stab = rcu_dereference_bh(sch->stab);
785 __qdisc_calculate_pkt_len(skb, stab);
789 static inline int qdisc_enqueue(struct sk_buff *skb, struct Qdisc *sch,
790 struct sk_buff **to_free)
792 qdisc_calculate_pkt_len(skb, sch);
793 return sch->enqueue(skb, sch, to_free);
796 static inline bool qdisc_is_percpu_stats(const struct Qdisc *q)
798 return q->flags & TCQ_F_CPUSTATS;
801 static inline void _bstats_update(struct gnet_stats_basic_packed *bstats,
802 __u64 bytes, __u32 packets)
804 bstats->bytes += bytes;
805 bstats->packets += packets;
808 static inline void bstats_update(struct gnet_stats_basic_packed *bstats,
809 const struct sk_buff *skb)
811 _bstats_update(bstats,
813 skb_is_gso(skb) ? skb_shinfo(skb)->gso_segs : 1);
816 static inline void _bstats_cpu_update(struct gnet_stats_basic_cpu *bstats,
817 __u64 bytes, __u32 packets)
819 u64_stats_update_begin(&bstats->syncp);
820 _bstats_update(&bstats->bstats, bytes, packets);
821 u64_stats_update_end(&bstats->syncp);
824 static inline void bstats_cpu_update(struct gnet_stats_basic_cpu *bstats,
825 const struct sk_buff *skb)
827 u64_stats_update_begin(&bstats->syncp);
828 bstats_update(&bstats->bstats, skb);
829 u64_stats_update_end(&bstats->syncp);
832 static inline void qdisc_bstats_cpu_update(struct Qdisc *sch,
833 const struct sk_buff *skb)
835 bstats_cpu_update(this_cpu_ptr(sch->cpu_bstats), skb);
838 static inline void qdisc_bstats_update(struct Qdisc *sch,
839 const struct sk_buff *skb)
841 bstats_update(&sch->bstats, skb);
844 static inline void qdisc_qstats_backlog_dec(struct Qdisc *sch,
845 const struct sk_buff *skb)
847 sch->qstats.backlog -= qdisc_pkt_len(skb);
850 static inline void qdisc_qstats_cpu_backlog_dec(struct Qdisc *sch,
851 const struct sk_buff *skb)
853 this_cpu_sub(sch->cpu_qstats->backlog, qdisc_pkt_len(skb));
856 static inline void qdisc_qstats_backlog_inc(struct Qdisc *sch,
857 const struct sk_buff *skb)
859 sch->qstats.backlog += qdisc_pkt_len(skb);
862 static inline void qdisc_qstats_cpu_backlog_inc(struct Qdisc *sch,
863 const struct sk_buff *skb)
865 this_cpu_add(sch->cpu_qstats->backlog, qdisc_pkt_len(skb));
868 static inline void qdisc_qstats_cpu_qlen_inc(struct Qdisc *sch)
870 this_cpu_inc(sch->cpu_qstats->qlen);
873 static inline void qdisc_qstats_cpu_qlen_dec(struct Qdisc *sch)
875 this_cpu_dec(sch->cpu_qstats->qlen);
878 static inline void qdisc_qstats_cpu_requeues_inc(struct Qdisc *sch)
880 this_cpu_inc(sch->cpu_qstats->requeues);
883 static inline void __qdisc_qstats_drop(struct Qdisc *sch, int count)
885 sch->qstats.drops += count;
888 static inline void qstats_drop_inc(struct gnet_stats_queue *qstats)
893 static inline void qstats_overlimit_inc(struct gnet_stats_queue *qstats)
895 qstats->overlimits++;
898 static inline void qdisc_qstats_drop(struct Qdisc *sch)
900 qstats_drop_inc(&sch->qstats);
903 static inline void qdisc_qstats_cpu_drop(struct Qdisc *sch)
905 this_cpu_inc(sch->cpu_qstats->drops);
908 static inline void qdisc_qstats_overlimit(struct Qdisc *sch)
910 sch->qstats.overlimits++;
913 static inline void qdisc_skb_head_init(struct qdisc_skb_head *qh)
920 static inline void __qdisc_enqueue_tail(struct sk_buff *skb,
921 struct qdisc_skb_head *qh)
923 struct sk_buff *last = qh->tail;
936 static inline int qdisc_enqueue_tail(struct sk_buff *skb, struct Qdisc *sch)
938 __qdisc_enqueue_tail(skb, &sch->q);
939 qdisc_qstats_backlog_inc(sch, skb);
940 return NET_XMIT_SUCCESS;
943 static inline void __qdisc_enqueue_head(struct sk_buff *skb,
944 struct qdisc_skb_head *qh)
946 skb->next = qh->head;
954 static inline struct sk_buff *__qdisc_dequeue_head(struct qdisc_skb_head *qh)
956 struct sk_buff *skb = qh->head;
958 if (likely(skb != NULL)) {
959 qh->head = skb->next;
961 if (qh->head == NULL)
969 static inline struct sk_buff *qdisc_dequeue_head(struct Qdisc *sch)
971 struct sk_buff *skb = __qdisc_dequeue_head(&sch->q);
973 if (likely(skb != NULL)) {
974 qdisc_qstats_backlog_dec(sch, skb);
975 qdisc_bstats_update(sch, skb);
981 /* Instead of calling kfree_skb() while root qdisc lock is held,
982 * queue the skb for future freeing at end of __dev_xmit_skb()
984 static inline void __qdisc_drop(struct sk_buff *skb, struct sk_buff **to_free)
986 skb->next = *to_free;
990 static inline void __qdisc_drop_all(struct sk_buff *skb,
991 struct sk_buff **to_free)
994 skb->prev->next = *to_free;
996 skb->next = *to_free;
1000 static inline unsigned int __qdisc_queue_drop_head(struct Qdisc *sch,
1001 struct qdisc_skb_head *qh,
1002 struct sk_buff **to_free)
1004 struct sk_buff *skb = __qdisc_dequeue_head(qh);
1006 if (likely(skb != NULL)) {
1007 unsigned int len = qdisc_pkt_len(skb);
1009 qdisc_qstats_backlog_dec(sch, skb);
1010 __qdisc_drop(skb, to_free);
1017 static inline unsigned int qdisc_queue_drop_head(struct Qdisc *sch,
1018 struct sk_buff **to_free)
1020 return __qdisc_queue_drop_head(sch, &sch->q, to_free);
1023 static inline struct sk_buff *qdisc_peek_head(struct Qdisc *sch)
1025 const struct qdisc_skb_head *qh = &sch->q;
1030 /* generic pseudo peek method for non-work-conserving qdisc */
1031 static inline struct sk_buff *qdisc_peek_dequeued(struct Qdisc *sch)
1033 struct sk_buff *skb = skb_peek(&sch->gso_skb);
1035 /* we can reuse ->gso_skb because peek isn't called for root qdiscs */
1037 skb = sch->dequeue(sch);
1040 __skb_queue_head(&sch->gso_skb, skb);
1041 /* it's still part of the queue */
1042 qdisc_qstats_backlog_inc(sch, skb);
1050 /* use instead of qdisc->dequeue() for all qdiscs queried with ->peek() */
1051 static inline struct sk_buff *qdisc_dequeue_peeked(struct Qdisc *sch)
1053 struct sk_buff *skb = skb_peek(&sch->gso_skb);
1056 skb = __skb_dequeue(&sch->gso_skb);
1057 qdisc_qstats_backlog_dec(sch, skb);
1060 skb = sch->dequeue(sch);
1066 static inline void __qdisc_reset_queue(struct qdisc_skb_head *qh)
1069 * We do not know the backlog in bytes of this list, it
1070 * is up to the caller to correct it
1074 rtnl_kfree_skbs(qh->head, qh->tail);
1082 static inline void qdisc_reset_queue(struct Qdisc *sch)
1084 __qdisc_reset_queue(&sch->q);
1085 sch->qstats.backlog = 0;
1088 static inline struct Qdisc *qdisc_replace(struct Qdisc *sch, struct Qdisc *new,
1089 struct Qdisc **pold)
1097 unsigned int qlen = old->q.qlen;
1098 unsigned int backlog = old->qstats.backlog;
1101 qdisc_tree_reduce_backlog(old, qlen, backlog);
1103 sch_tree_unlock(sch);
1108 static inline void rtnl_qdisc_drop(struct sk_buff *skb, struct Qdisc *sch)
1110 rtnl_kfree_skbs(skb, skb);
1111 qdisc_qstats_drop(sch);
1114 static inline int qdisc_drop_cpu(struct sk_buff *skb, struct Qdisc *sch,
1115 struct sk_buff **to_free)
1117 __qdisc_drop(skb, to_free);
1118 qdisc_qstats_cpu_drop(sch);
1120 return NET_XMIT_DROP;
1123 static inline int qdisc_drop(struct sk_buff *skb, struct Qdisc *sch,
1124 struct sk_buff **to_free)
1126 __qdisc_drop(skb, to_free);
1127 qdisc_qstats_drop(sch);
1129 return NET_XMIT_DROP;
1132 static inline int qdisc_drop_all(struct sk_buff *skb, struct Qdisc *sch,
1133 struct sk_buff **to_free)
1135 __qdisc_drop_all(skb, to_free);
1136 qdisc_qstats_drop(sch);
1138 return NET_XMIT_DROP;
1141 /* Length to Time (L2T) lookup in a qdisc_rate_table, to determine how
1142 long it will take to send a packet given its size.
1144 static inline u32 qdisc_l2t(struct qdisc_rate_table* rtab, unsigned int pktlen)
1146 int slot = pktlen + rtab->rate.cell_align + rtab->rate.overhead;
1149 slot >>= rtab->rate.cell_log;
1151 return rtab->data[255]*(slot >> 8) + rtab->data[slot & 0xFF];
1152 return rtab->data[slot];
1155 struct psched_ratecfg {
1156 u64 rate_bytes_ps; /* bytes per second */
1163 static inline u64 psched_l2t_ns(const struct psched_ratecfg *r,
1168 if (unlikely(r->linklayer == TC_LINKLAYER_ATM))
1169 return ((u64)(DIV_ROUND_UP(len,48)*53) * r->mult) >> r->shift;
1171 return ((u64)len * r->mult) >> r->shift;
1174 void psched_ratecfg_precompute(struct psched_ratecfg *r,
1175 const struct tc_ratespec *conf,
1178 static inline void psched_ratecfg_getrate(struct tc_ratespec *res,
1179 const struct psched_ratecfg *r)
1181 memset(res, 0, sizeof(*res));
1183 /* legacy struct tc_ratespec has a 32bit @rate field
1184 * Qdisc using 64bit rate should add new attributes
1185 * in order to maintain compatibility.
1187 res->rate = min_t(u64, r->rate_bytes_ps, ~0U);
1189 res->overhead = r->overhead;
1190 res->linklayer = (r->linklayer & TC_LINKLAYER_MASK);
1193 /* Mini Qdisc serves for specific needs of ingress/clsact Qdisc.
1194 * The fast path only needs to access filter list and to update stats
1197 struct tcf_proto *filter_list;
1198 struct gnet_stats_basic_cpu __percpu *cpu_bstats;
1199 struct gnet_stats_queue __percpu *cpu_qstats;
1200 struct rcu_head rcu;
1203 static inline void mini_qdisc_bstats_cpu_update(struct mini_Qdisc *miniq,
1204 const struct sk_buff *skb)
1206 bstats_cpu_update(this_cpu_ptr(miniq->cpu_bstats), skb);
1209 static inline void mini_qdisc_qstats_cpu_drop(struct mini_Qdisc *miniq)
1211 this_cpu_inc(miniq->cpu_qstats->drops);
1214 struct mini_Qdisc_pair {
1215 struct mini_Qdisc miniq1;
1216 struct mini_Qdisc miniq2;
1217 struct mini_Qdisc __rcu **p_miniq;
1220 void mini_qdisc_pair_swap(struct mini_Qdisc_pair *miniqp,
1221 struct tcf_proto *tp_head);
1222 void mini_qdisc_pair_init(struct mini_Qdisc_pair *miniqp, struct Qdisc *qdisc,
1223 struct mini_Qdisc __rcu **p_miniq);
1225 static inline void skb_tc_reinsert(struct sk_buff *skb, struct tcf_result *res)
1227 struct gnet_stats_queue *stats = res->qstats;
1231 ret = netif_receive_skb(skb);
1233 ret = dev_queue_xmit(skb);
1235 qstats_overlimit_inc(res->qstats);