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;
114 /* for NOLOCK qdisc, true if there are no enqueued skbs */
119 static inline void qdisc_refcount_inc(struct Qdisc *qdisc)
121 if (qdisc->flags & TCQ_F_BUILTIN)
123 refcount_inc(&qdisc->refcnt);
126 /* Intended to be used by unlocked users, when concurrent qdisc release is
130 static inline struct Qdisc *qdisc_refcount_inc_nz(struct Qdisc *qdisc)
132 if (qdisc->flags & TCQ_F_BUILTIN)
134 if (refcount_inc_not_zero(&qdisc->refcnt))
139 static inline bool qdisc_is_running(struct Qdisc *qdisc)
141 if (qdisc->flags & TCQ_F_NOLOCK)
142 return spin_is_locked(&qdisc->seqlock);
143 return (raw_read_seqcount(&qdisc->running) & 1) ? true : false;
146 static inline bool qdisc_is_percpu_stats(const struct Qdisc *q)
148 return q->flags & TCQ_F_CPUSTATS;
151 static inline bool qdisc_is_empty(const struct Qdisc *qdisc)
153 if (qdisc_is_percpu_stats(qdisc))
155 return !qdisc->q.qlen;
158 static inline bool qdisc_run_begin(struct Qdisc *qdisc)
160 if (qdisc->flags & TCQ_F_NOLOCK) {
161 if (!spin_trylock(&qdisc->seqlock))
163 qdisc->empty = false;
164 } else if (qdisc_is_running(qdisc)) {
167 /* Variant of write_seqcount_begin() telling lockdep a trylock
170 raw_write_seqcount_begin(&qdisc->running);
171 seqcount_acquire(&qdisc->running.dep_map, 0, 1, _RET_IP_);
175 static inline void qdisc_run_end(struct Qdisc *qdisc)
177 write_seqcount_end(&qdisc->running);
178 if (qdisc->flags & TCQ_F_NOLOCK)
179 spin_unlock(&qdisc->seqlock);
182 static inline bool qdisc_may_bulk(const struct Qdisc *qdisc)
184 return qdisc->flags & TCQ_F_ONETXQUEUE;
187 static inline int qdisc_avail_bulklimit(const struct netdev_queue *txq)
190 /* Non-BQL migrated drivers will return 0, too. */
191 return dql_avail(&txq->dql);
197 struct Qdisc_class_ops {
199 /* Child qdisc manipulation */
200 struct netdev_queue * (*select_queue)(struct Qdisc *, struct tcmsg *);
201 int (*graft)(struct Qdisc *, unsigned long cl,
202 struct Qdisc *, struct Qdisc **,
203 struct netlink_ext_ack *extack);
204 struct Qdisc * (*leaf)(struct Qdisc *, unsigned long cl);
205 void (*qlen_notify)(struct Qdisc *, unsigned long);
207 /* Class manipulation routines */
208 unsigned long (*find)(struct Qdisc *, u32 classid);
209 int (*change)(struct Qdisc *, u32, u32,
210 struct nlattr **, unsigned long *,
211 struct netlink_ext_ack *);
212 int (*delete)(struct Qdisc *, unsigned long);
213 void (*walk)(struct Qdisc *, struct qdisc_walker * arg);
215 /* Filter manipulation */
216 struct tcf_block * (*tcf_block)(struct Qdisc *sch,
218 struct netlink_ext_ack *extack);
219 unsigned long (*bind_tcf)(struct Qdisc *, unsigned long,
221 void (*unbind_tcf)(struct Qdisc *, unsigned long);
223 /* rtnetlink specific */
224 int (*dump)(struct Qdisc *, unsigned long,
225 struct sk_buff *skb, struct tcmsg*);
226 int (*dump_stats)(struct Qdisc *, unsigned long,
230 /* Qdisc_class_ops flag values */
232 /* Implements API that doesn't require rtnl lock */
233 enum qdisc_class_ops_flags {
234 QDISC_CLASS_OPS_DOIT_UNLOCKED = 1,
238 struct Qdisc_ops *next;
239 const struct Qdisc_class_ops *cl_ops;
242 unsigned int static_flags;
244 int (*enqueue)(struct sk_buff *skb,
246 struct sk_buff **to_free);
247 struct sk_buff * (*dequeue)(struct Qdisc *);
248 struct sk_buff * (*peek)(struct Qdisc *);
250 int (*init)(struct Qdisc *sch, struct nlattr *arg,
251 struct netlink_ext_ack *extack);
252 void (*reset)(struct Qdisc *);
253 void (*destroy)(struct Qdisc *);
254 int (*change)(struct Qdisc *sch,
256 struct netlink_ext_ack *extack);
257 void (*attach)(struct Qdisc *sch);
258 int (*change_tx_queue_len)(struct Qdisc *, unsigned int);
260 int (*dump)(struct Qdisc *, struct sk_buff *);
261 int (*dump_stats)(struct Qdisc *, struct gnet_dump *);
263 void (*ingress_block_set)(struct Qdisc *sch,
265 void (*egress_block_set)(struct Qdisc *sch,
267 u32 (*ingress_block_get)(struct Qdisc *sch);
268 u32 (*egress_block_get)(struct Qdisc *sch);
270 struct module *owner;
280 const struct tcf_proto *goto_tp;
282 /* used by the TC_ACT_REINSERT action */
285 struct gnet_stats_queue *qstats;
292 struct tcf_proto_ops {
293 struct list_head head;
296 int (*classify)(struct sk_buff *,
297 const struct tcf_proto *,
298 struct tcf_result *);
299 int (*init)(struct tcf_proto*);
300 void (*destroy)(struct tcf_proto *tp, bool rtnl_held,
301 struct netlink_ext_ack *extack);
303 void* (*get)(struct tcf_proto*, u32 handle);
304 void (*put)(struct tcf_proto *tp, void *f);
305 int (*change)(struct net *net, struct sk_buff *,
306 struct tcf_proto*, unsigned long,
307 u32 handle, struct nlattr **,
309 struct netlink_ext_ack *);
310 int (*delete)(struct tcf_proto *tp, void *arg,
311 bool *last, bool rtnl_held,
312 struct netlink_ext_ack *);
313 void (*walk)(struct tcf_proto *tp,
314 struct tcf_walker *arg, bool rtnl_held);
315 int (*reoffload)(struct tcf_proto *tp, bool add,
316 tc_setup_cb_t *cb, void *cb_priv,
317 struct netlink_ext_ack *extack);
318 void (*bind_class)(void *, u32, unsigned long);
319 void * (*tmplt_create)(struct net *net,
320 struct tcf_chain *chain,
322 struct netlink_ext_ack *extack);
323 void (*tmplt_destroy)(void *tmplt_priv);
325 /* rtnetlink specific */
326 int (*dump)(struct net*, struct tcf_proto*, void *,
327 struct sk_buff *skb, struct tcmsg*,
329 int (*tmplt_dump)(struct sk_buff *skb,
333 struct module *owner;
337 enum tcf_proto_ops_flags {
338 TCF_PROTO_OPS_DOIT_UNLOCKED = 1,
342 /* Fast access part */
343 struct tcf_proto __rcu *next;
346 /* called under RCU BH lock*/
347 int (*classify)(struct sk_buff *,
348 const struct tcf_proto *,
349 struct tcf_result *);
355 const struct tcf_proto_ops *ops;
356 struct tcf_chain *chain;
357 /* Lock protects tcf_proto shared state and can be used by unlocked
358 * classifiers to protect their private data.
366 struct qdisc_skb_cb {
369 unsigned int pkt_len;
370 u16 slave_dev_queue_mapping;
373 struct bpf_flow_keys *flow_keys;
375 #define QDISC_CB_PRIV_LEN 20
376 unsigned char data[QDISC_CB_PRIV_LEN];
379 typedef void tcf_chain_head_change_t(struct tcf_proto *tp_head, void *priv);
382 /* Protects filter_chain. */
383 struct mutex filter_chain_lock;
384 struct tcf_proto __rcu *filter_chain;
385 struct list_head list;
386 struct tcf_block *block;
387 u32 index; /* chain index */
389 unsigned int action_refcnt;
390 bool explicitly_created;
392 const struct tcf_proto_ops *tmplt_ops;
398 /* Lock protects tcf_block and lifetime-management data of chains
399 * attached to the block (refcnt, action_refcnt, explicitly_created).
402 struct list_head chain_list;
403 u32 index; /* block index for shared blocks */
407 struct list_head cb_list;
408 struct list_head owner_list;
410 unsigned int offloadcnt; /* Number of oddloaded filters */
411 unsigned int nooffloaddevcnt; /* Number of devs unable to do offload */
413 struct tcf_chain *chain;
414 struct list_head filter_chain_list;
419 #ifdef CONFIG_PROVE_LOCKING
420 static inline bool lockdep_tcf_chain_is_locked(struct tcf_chain *chain)
422 return lockdep_is_held(&chain->filter_chain_lock);
425 static inline bool lockdep_tcf_proto_is_locked(struct tcf_proto *tp)
427 return lockdep_is_held(&tp->lock);
430 static inline bool lockdep_tcf_chain_is_locked(struct tcf_block *chain)
435 static inline bool lockdep_tcf_proto_is_locked(struct tcf_proto *tp)
439 #endif /* #ifdef CONFIG_PROVE_LOCKING */
441 #define tcf_chain_dereference(p, chain) \
442 rcu_dereference_protected(p, lockdep_tcf_chain_is_locked(chain))
444 #define tcf_proto_dereference(p, tp) \
445 rcu_dereference_protected(p, lockdep_tcf_proto_is_locked(tp))
447 static inline void tcf_block_offload_inc(struct tcf_block *block, u32 *flags)
449 if (*flags & TCA_CLS_FLAGS_IN_HW)
451 *flags |= TCA_CLS_FLAGS_IN_HW;
455 static inline void tcf_block_offload_dec(struct tcf_block *block, u32 *flags)
457 if (!(*flags & TCA_CLS_FLAGS_IN_HW))
459 *flags &= ~TCA_CLS_FLAGS_IN_HW;
464 tc_cls_offload_cnt_update(struct tcf_block *block, u32 *cnt,
465 u32 *flags, bool add)
469 tcf_block_offload_inc(block, flags);
474 tcf_block_offload_dec(block, flags);
478 static inline void qdisc_cb_private_validate(const struct sk_buff *skb, int sz)
480 struct qdisc_skb_cb *qcb;
482 BUILD_BUG_ON(sizeof(skb->cb) < offsetof(struct qdisc_skb_cb, data) + sz);
483 BUILD_BUG_ON(sizeof(qcb->data) < sz);
486 static inline int qdisc_qlen_cpu(const struct Qdisc *q)
488 return this_cpu_ptr(q->cpu_qstats)->qlen;
491 static inline int qdisc_qlen(const struct Qdisc *q)
496 static inline int qdisc_qlen_sum(const struct Qdisc *q)
498 __u32 qlen = q->qstats.qlen;
501 if (qdisc_is_percpu_stats(q)) {
502 for_each_possible_cpu(i)
503 qlen += per_cpu_ptr(q->cpu_qstats, i)->qlen;
511 static inline struct qdisc_skb_cb *qdisc_skb_cb(const struct sk_buff *skb)
513 return (struct qdisc_skb_cb *)skb->cb;
516 static inline spinlock_t *qdisc_lock(struct Qdisc *qdisc)
518 return &qdisc->q.lock;
521 static inline struct Qdisc *qdisc_root(const struct Qdisc *qdisc)
523 struct Qdisc *q = rcu_dereference_rtnl(qdisc->dev_queue->qdisc);
528 static inline struct Qdisc *qdisc_root_sleeping(const struct Qdisc *qdisc)
530 return qdisc->dev_queue->qdisc_sleeping;
533 /* The qdisc root lock is a mechanism by which to top level
534 * of a qdisc tree can be locked from any qdisc node in the
535 * forest. This allows changing the configuration of some
536 * aspect of the qdisc tree while blocking out asynchronous
537 * qdisc access in the packet processing paths.
539 * It is only legal to do this when the root will not change
540 * on us. Otherwise we'll potentially lock the wrong qdisc
541 * root. This is enforced by holding the RTNL semaphore, which
542 * all users of this lock accessor must do.
544 static inline spinlock_t *qdisc_root_lock(const struct Qdisc *qdisc)
546 struct Qdisc *root = qdisc_root(qdisc);
549 return qdisc_lock(root);
552 static inline spinlock_t *qdisc_root_sleeping_lock(const struct Qdisc *qdisc)
554 struct Qdisc *root = qdisc_root_sleeping(qdisc);
557 return qdisc_lock(root);
560 static inline seqcount_t *qdisc_root_sleeping_running(const struct Qdisc *qdisc)
562 struct Qdisc *root = qdisc_root_sleeping(qdisc);
565 return &root->running;
568 static inline struct net_device *qdisc_dev(const struct Qdisc *qdisc)
570 return qdisc->dev_queue->dev;
573 static inline void sch_tree_lock(const struct Qdisc *q)
575 spin_lock_bh(qdisc_root_sleeping_lock(q));
578 static inline void sch_tree_unlock(const struct Qdisc *q)
580 spin_unlock_bh(qdisc_root_sleeping_lock(q));
583 extern struct Qdisc noop_qdisc;
584 extern struct Qdisc_ops noop_qdisc_ops;
585 extern struct Qdisc_ops pfifo_fast_ops;
586 extern struct Qdisc_ops mq_qdisc_ops;
587 extern struct Qdisc_ops noqueue_qdisc_ops;
588 extern const struct Qdisc_ops *default_qdisc_ops;
589 static inline const struct Qdisc_ops *
590 get_default_qdisc_ops(const struct net_device *dev, int ntx)
592 return ntx < dev->real_num_tx_queues ?
593 default_qdisc_ops : &pfifo_fast_ops;
596 struct Qdisc_class_common {
598 struct hlist_node hnode;
601 struct Qdisc_class_hash {
602 struct hlist_head *hash;
603 unsigned int hashsize;
604 unsigned int hashmask;
605 unsigned int hashelems;
608 static inline unsigned int qdisc_class_hash(u32 id, u32 mask)
615 static inline struct Qdisc_class_common *
616 qdisc_class_find(const struct Qdisc_class_hash *hash, u32 id)
618 struct Qdisc_class_common *cl;
624 h = qdisc_class_hash(id, hash->hashmask);
625 hlist_for_each_entry(cl, &hash->hash[h], hnode) {
626 if (cl->classid == id)
632 static inline int tc_classid_to_hwtc(struct net_device *dev, u32 classid)
634 u32 hwtc = TC_H_MIN(classid) - TC_H_MIN_PRIORITY;
636 return (hwtc < netdev_get_num_tc(dev)) ? hwtc : -EINVAL;
639 int qdisc_class_hash_init(struct Qdisc_class_hash *);
640 void qdisc_class_hash_insert(struct Qdisc_class_hash *,
641 struct Qdisc_class_common *);
642 void qdisc_class_hash_remove(struct Qdisc_class_hash *,
643 struct Qdisc_class_common *);
644 void qdisc_class_hash_grow(struct Qdisc *, struct Qdisc_class_hash *);
645 void qdisc_class_hash_destroy(struct Qdisc_class_hash *);
647 int dev_qdisc_change_tx_queue_len(struct net_device *dev);
648 void dev_init_scheduler(struct net_device *dev);
649 void dev_shutdown(struct net_device *dev);
650 void dev_activate(struct net_device *dev);
651 void dev_deactivate(struct net_device *dev);
652 void dev_deactivate_many(struct list_head *head);
653 struct Qdisc *dev_graft_qdisc(struct netdev_queue *dev_queue,
654 struct Qdisc *qdisc);
655 void qdisc_reset(struct Qdisc *qdisc);
656 void qdisc_put(struct Qdisc *qdisc);
657 void qdisc_put_unlocked(struct Qdisc *qdisc);
658 void qdisc_tree_reduce_backlog(struct Qdisc *qdisc, int n, int len);
659 #ifdef CONFIG_NET_SCHED
660 int qdisc_offload_dump_helper(struct Qdisc *q, enum tc_setup_type type,
662 void qdisc_offload_graft_helper(struct net_device *dev, struct Qdisc *sch,
663 struct Qdisc *new, struct Qdisc *old,
664 enum tc_setup_type type, void *type_data,
665 struct netlink_ext_ack *extack);
668 qdisc_offload_dump_helper(struct Qdisc *q, enum tc_setup_type type,
671 q->flags &= ~TCQ_F_OFFLOADED;
676 qdisc_offload_graft_helper(struct net_device *dev, struct Qdisc *sch,
677 struct Qdisc *new, struct Qdisc *old,
678 enum tc_setup_type type, void *type_data,
679 struct netlink_ext_ack *extack)
683 struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue,
684 const struct Qdisc_ops *ops,
685 struct netlink_ext_ack *extack);
686 void qdisc_free(struct Qdisc *qdisc);
687 struct Qdisc *qdisc_create_dflt(struct netdev_queue *dev_queue,
688 const struct Qdisc_ops *ops, u32 parentid,
689 struct netlink_ext_ack *extack);
690 void __qdisc_calculate_pkt_len(struct sk_buff *skb,
691 const struct qdisc_size_table *stab);
692 int skb_do_redirect(struct sk_buff *);
694 static inline void skb_reset_tc(struct sk_buff *skb)
696 #ifdef CONFIG_NET_CLS_ACT
697 skb->tc_redirected = 0;
701 static inline bool skb_is_tc_redirected(const struct sk_buff *skb)
703 #ifdef CONFIG_NET_CLS_ACT
704 return skb->tc_redirected;
710 static inline bool skb_at_tc_ingress(const struct sk_buff *skb)
712 #ifdef CONFIG_NET_CLS_ACT
713 return skb->tc_at_ingress;
719 static inline bool skb_skip_tc_classify(struct sk_buff *skb)
721 #ifdef CONFIG_NET_CLS_ACT
722 if (skb->tc_skip_classify) {
723 skb->tc_skip_classify = 0;
730 /* Reset all TX qdiscs greater than index of a device. */
731 static inline void qdisc_reset_all_tx_gt(struct net_device *dev, unsigned int i)
735 for (; i < dev->num_tx_queues; i++) {
736 qdisc = rtnl_dereference(netdev_get_tx_queue(dev, i)->qdisc);
738 spin_lock_bh(qdisc_lock(qdisc));
740 spin_unlock_bh(qdisc_lock(qdisc));
745 static inline void qdisc_reset_all_tx(struct net_device *dev)
747 qdisc_reset_all_tx_gt(dev, 0);
750 /* Are all TX queues of the device empty? */
751 static inline bool qdisc_all_tx_empty(const struct net_device *dev)
756 for (i = 0; i < dev->num_tx_queues; i++) {
757 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
758 const struct Qdisc *q = rcu_dereference(txq->qdisc);
760 if (!qdisc_is_empty(q)) {
769 /* Are any of the TX qdiscs changing? */
770 static inline bool qdisc_tx_changing(const struct net_device *dev)
774 for (i = 0; i < dev->num_tx_queues; i++) {
775 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
776 if (rcu_access_pointer(txq->qdisc) != txq->qdisc_sleeping)
782 /* Is the device using the noop qdisc on all queues? */
783 static inline bool qdisc_tx_is_noop(const struct net_device *dev)
787 for (i = 0; i < dev->num_tx_queues; i++) {
788 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
789 if (rcu_access_pointer(txq->qdisc) != &noop_qdisc)
795 static inline unsigned int qdisc_pkt_len(const struct sk_buff *skb)
797 return qdisc_skb_cb(skb)->pkt_len;
800 /* additional qdisc xmit flags (NET_XMIT_MASK in linux/netdevice.h) */
801 enum net_xmit_qdisc_t {
802 __NET_XMIT_STOLEN = 0x00010000,
803 __NET_XMIT_BYPASS = 0x00020000,
806 #ifdef CONFIG_NET_CLS_ACT
807 #define net_xmit_drop_count(e) ((e) & __NET_XMIT_STOLEN ? 0 : 1)
809 #define net_xmit_drop_count(e) (1)
812 static inline void qdisc_calculate_pkt_len(struct sk_buff *skb,
813 const struct Qdisc *sch)
815 #ifdef CONFIG_NET_SCHED
816 struct qdisc_size_table *stab = rcu_dereference_bh(sch->stab);
819 __qdisc_calculate_pkt_len(skb, stab);
823 static inline int qdisc_enqueue(struct sk_buff *skb, struct Qdisc *sch,
824 struct sk_buff **to_free)
826 qdisc_calculate_pkt_len(skb, sch);
827 return sch->enqueue(skb, sch, to_free);
830 static inline void _bstats_update(struct gnet_stats_basic_packed *bstats,
831 __u64 bytes, __u32 packets)
833 bstats->bytes += bytes;
834 bstats->packets += packets;
837 static inline void bstats_update(struct gnet_stats_basic_packed *bstats,
838 const struct sk_buff *skb)
840 _bstats_update(bstats,
842 skb_is_gso(skb) ? skb_shinfo(skb)->gso_segs : 1);
845 static inline void _bstats_cpu_update(struct gnet_stats_basic_cpu *bstats,
846 __u64 bytes, __u32 packets)
848 u64_stats_update_begin(&bstats->syncp);
849 _bstats_update(&bstats->bstats, bytes, packets);
850 u64_stats_update_end(&bstats->syncp);
853 static inline void bstats_cpu_update(struct gnet_stats_basic_cpu *bstats,
854 const struct sk_buff *skb)
856 u64_stats_update_begin(&bstats->syncp);
857 bstats_update(&bstats->bstats, skb);
858 u64_stats_update_end(&bstats->syncp);
861 static inline void qdisc_bstats_cpu_update(struct Qdisc *sch,
862 const struct sk_buff *skb)
864 bstats_cpu_update(this_cpu_ptr(sch->cpu_bstats), skb);
867 static inline void qdisc_bstats_update(struct Qdisc *sch,
868 const struct sk_buff *skb)
870 bstats_update(&sch->bstats, skb);
873 static inline void qdisc_qstats_backlog_dec(struct Qdisc *sch,
874 const struct sk_buff *skb)
876 sch->qstats.backlog -= qdisc_pkt_len(skb);
879 static inline void qdisc_qstats_cpu_backlog_dec(struct Qdisc *sch,
880 const struct sk_buff *skb)
882 this_cpu_sub(sch->cpu_qstats->backlog, qdisc_pkt_len(skb));
885 static inline void qdisc_qstats_backlog_inc(struct Qdisc *sch,
886 const struct sk_buff *skb)
888 sch->qstats.backlog += qdisc_pkt_len(skb);
891 static inline void qdisc_qstats_cpu_backlog_inc(struct Qdisc *sch,
892 const struct sk_buff *skb)
894 this_cpu_add(sch->cpu_qstats->backlog, qdisc_pkt_len(skb));
897 static inline void qdisc_qstats_cpu_qlen_inc(struct Qdisc *sch)
899 this_cpu_inc(sch->cpu_qstats->qlen);
902 static inline void qdisc_qstats_cpu_qlen_dec(struct Qdisc *sch)
904 this_cpu_dec(sch->cpu_qstats->qlen);
907 static inline void qdisc_qstats_cpu_requeues_inc(struct Qdisc *sch)
909 this_cpu_inc(sch->cpu_qstats->requeues);
912 static inline void __qdisc_qstats_drop(struct Qdisc *sch, int count)
914 sch->qstats.drops += count;
917 static inline void qstats_drop_inc(struct gnet_stats_queue *qstats)
922 static inline void qstats_overlimit_inc(struct gnet_stats_queue *qstats)
924 qstats->overlimits++;
927 static inline void qdisc_qstats_drop(struct Qdisc *sch)
929 qstats_drop_inc(&sch->qstats);
932 static inline void qdisc_qstats_cpu_drop(struct Qdisc *sch)
934 this_cpu_inc(sch->cpu_qstats->drops);
937 static inline void qdisc_qstats_overlimit(struct Qdisc *sch)
939 sch->qstats.overlimits++;
942 static inline int qdisc_qstats_copy(struct gnet_dump *d, struct Qdisc *sch)
944 __u32 qlen = qdisc_qlen_sum(sch);
946 return gnet_stats_copy_queue(d, sch->cpu_qstats, &sch->qstats, qlen);
949 static inline void qdisc_qstats_qlen_backlog(struct Qdisc *sch, __u32 *qlen,
952 struct gnet_stats_queue qstats = { 0 };
953 __u32 len = qdisc_qlen_sum(sch);
955 __gnet_stats_copy_queue(&qstats, sch->cpu_qstats, &sch->qstats, len);
957 *backlog = qstats.backlog;
960 static inline void qdisc_tree_flush_backlog(struct Qdisc *sch)
964 qdisc_qstats_qlen_backlog(sch, &qlen, &backlog);
965 qdisc_tree_reduce_backlog(sch, qlen, backlog);
968 static inline void qdisc_purge_queue(struct Qdisc *sch)
972 qdisc_qstats_qlen_backlog(sch, &qlen, &backlog);
974 qdisc_tree_reduce_backlog(sch, qlen, backlog);
977 static inline void qdisc_skb_head_init(struct qdisc_skb_head *qh)
984 static inline void __qdisc_enqueue_tail(struct sk_buff *skb,
985 struct qdisc_skb_head *qh)
987 struct sk_buff *last = qh->tail;
1000 static inline int qdisc_enqueue_tail(struct sk_buff *skb, struct Qdisc *sch)
1002 __qdisc_enqueue_tail(skb, &sch->q);
1003 qdisc_qstats_backlog_inc(sch, skb);
1004 return NET_XMIT_SUCCESS;
1007 static inline void __qdisc_enqueue_head(struct sk_buff *skb,
1008 struct qdisc_skb_head *qh)
1010 skb->next = qh->head;
1018 static inline struct sk_buff *__qdisc_dequeue_head(struct qdisc_skb_head *qh)
1020 struct sk_buff *skb = qh->head;
1022 if (likely(skb != NULL)) {
1023 qh->head = skb->next;
1025 if (qh->head == NULL)
1033 static inline struct sk_buff *qdisc_dequeue_head(struct Qdisc *sch)
1035 struct sk_buff *skb = __qdisc_dequeue_head(&sch->q);
1037 if (likely(skb != NULL)) {
1038 qdisc_qstats_backlog_dec(sch, skb);
1039 qdisc_bstats_update(sch, skb);
1045 /* Instead of calling kfree_skb() while root qdisc lock is held,
1046 * queue the skb for future freeing at end of __dev_xmit_skb()
1048 static inline void __qdisc_drop(struct sk_buff *skb, struct sk_buff **to_free)
1050 skb->next = *to_free;
1054 static inline void __qdisc_drop_all(struct sk_buff *skb,
1055 struct sk_buff **to_free)
1058 skb->prev->next = *to_free;
1060 skb->next = *to_free;
1064 static inline unsigned int __qdisc_queue_drop_head(struct Qdisc *sch,
1065 struct qdisc_skb_head *qh,
1066 struct sk_buff **to_free)
1068 struct sk_buff *skb = __qdisc_dequeue_head(qh);
1070 if (likely(skb != NULL)) {
1071 unsigned int len = qdisc_pkt_len(skb);
1073 qdisc_qstats_backlog_dec(sch, skb);
1074 __qdisc_drop(skb, to_free);
1081 static inline unsigned int qdisc_queue_drop_head(struct Qdisc *sch,
1082 struct sk_buff **to_free)
1084 return __qdisc_queue_drop_head(sch, &sch->q, to_free);
1087 static inline struct sk_buff *qdisc_peek_head(struct Qdisc *sch)
1089 const struct qdisc_skb_head *qh = &sch->q;
1094 /* generic pseudo peek method for non-work-conserving qdisc */
1095 static inline struct sk_buff *qdisc_peek_dequeued(struct Qdisc *sch)
1097 struct sk_buff *skb = skb_peek(&sch->gso_skb);
1099 /* we can reuse ->gso_skb because peek isn't called for root qdiscs */
1101 skb = sch->dequeue(sch);
1104 __skb_queue_head(&sch->gso_skb, skb);
1105 /* it's still part of the queue */
1106 qdisc_qstats_backlog_inc(sch, skb);
1114 static inline void qdisc_update_stats_at_dequeue(struct Qdisc *sch,
1115 struct sk_buff *skb)
1117 if (qdisc_is_percpu_stats(sch)) {
1118 qdisc_qstats_cpu_backlog_dec(sch, skb);
1119 qdisc_bstats_cpu_update(sch, skb);
1120 qdisc_qstats_cpu_qlen_dec(sch);
1122 qdisc_qstats_backlog_dec(sch, skb);
1123 qdisc_bstats_update(sch, skb);
1128 static inline void qdisc_update_stats_at_enqueue(struct Qdisc *sch,
1129 unsigned int pkt_len)
1131 if (qdisc_is_percpu_stats(sch)) {
1132 qdisc_qstats_cpu_qlen_inc(sch);
1133 this_cpu_add(sch->cpu_qstats->backlog, pkt_len);
1135 sch->qstats.backlog += pkt_len;
1140 /* use instead of qdisc->dequeue() for all qdiscs queried with ->peek() */
1141 static inline struct sk_buff *qdisc_dequeue_peeked(struct Qdisc *sch)
1143 struct sk_buff *skb = skb_peek(&sch->gso_skb);
1146 skb = __skb_dequeue(&sch->gso_skb);
1147 if (qdisc_is_percpu_stats(sch)) {
1148 qdisc_qstats_cpu_backlog_dec(sch, skb);
1149 qdisc_qstats_cpu_qlen_dec(sch);
1151 qdisc_qstats_backlog_dec(sch, skb);
1155 skb = sch->dequeue(sch);
1161 static inline void __qdisc_reset_queue(struct qdisc_skb_head *qh)
1164 * We do not know the backlog in bytes of this list, it
1165 * is up to the caller to correct it
1169 rtnl_kfree_skbs(qh->head, qh->tail);
1177 static inline void qdisc_reset_queue(struct Qdisc *sch)
1179 __qdisc_reset_queue(&sch->q);
1180 sch->qstats.backlog = 0;
1183 static inline struct Qdisc *qdisc_replace(struct Qdisc *sch, struct Qdisc *new,
1184 struct Qdisc **pold)
1192 qdisc_tree_flush_backlog(old);
1193 sch_tree_unlock(sch);
1198 static inline void rtnl_qdisc_drop(struct sk_buff *skb, struct Qdisc *sch)
1200 rtnl_kfree_skbs(skb, skb);
1201 qdisc_qstats_drop(sch);
1204 static inline int qdisc_drop_cpu(struct sk_buff *skb, struct Qdisc *sch,
1205 struct sk_buff **to_free)
1207 __qdisc_drop(skb, to_free);
1208 qdisc_qstats_cpu_drop(sch);
1210 return NET_XMIT_DROP;
1213 static inline int qdisc_drop(struct sk_buff *skb, struct Qdisc *sch,
1214 struct sk_buff **to_free)
1216 __qdisc_drop(skb, to_free);
1217 qdisc_qstats_drop(sch);
1219 return NET_XMIT_DROP;
1222 static inline int qdisc_drop_all(struct sk_buff *skb, struct Qdisc *sch,
1223 struct sk_buff **to_free)
1225 __qdisc_drop_all(skb, to_free);
1226 qdisc_qstats_drop(sch);
1228 return NET_XMIT_DROP;
1231 /* Length to Time (L2T) lookup in a qdisc_rate_table, to determine how
1232 long it will take to send a packet given its size.
1234 static inline u32 qdisc_l2t(struct qdisc_rate_table* rtab, unsigned int pktlen)
1236 int slot = pktlen + rtab->rate.cell_align + rtab->rate.overhead;
1239 slot >>= rtab->rate.cell_log;
1241 return rtab->data[255]*(slot >> 8) + rtab->data[slot & 0xFF];
1242 return rtab->data[slot];
1245 struct psched_ratecfg {
1246 u64 rate_bytes_ps; /* bytes per second */
1253 static inline u64 psched_l2t_ns(const struct psched_ratecfg *r,
1258 if (unlikely(r->linklayer == TC_LINKLAYER_ATM))
1259 return ((u64)(DIV_ROUND_UP(len,48)*53) * r->mult) >> r->shift;
1261 return ((u64)len * r->mult) >> r->shift;
1264 void psched_ratecfg_precompute(struct psched_ratecfg *r,
1265 const struct tc_ratespec *conf,
1268 static inline void psched_ratecfg_getrate(struct tc_ratespec *res,
1269 const struct psched_ratecfg *r)
1271 memset(res, 0, sizeof(*res));
1273 /* legacy struct tc_ratespec has a 32bit @rate field
1274 * Qdisc using 64bit rate should add new attributes
1275 * in order to maintain compatibility.
1277 res->rate = min_t(u64, r->rate_bytes_ps, ~0U);
1279 res->overhead = r->overhead;
1280 res->linklayer = (r->linklayer & TC_LINKLAYER_MASK);
1283 /* Mini Qdisc serves for specific needs of ingress/clsact Qdisc.
1284 * The fast path only needs to access filter list and to update stats
1287 struct tcf_proto *filter_list;
1288 struct gnet_stats_basic_cpu __percpu *cpu_bstats;
1289 struct gnet_stats_queue __percpu *cpu_qstats;
1290 struct rcu_head rcu;
1293 static inline void mini_qdisc_bstats_cpu_update(struct mini_Qdisc *miniq,
1294 const struct sk_buff *skb)
1296 bstats_cpu_update(this_cpu_ptr(miniq->cpu_bstats), skb);
1299 static inline void mini_qdisc_qstats_cpu_drop(struct mini_Qdisc *miniq)
1301 this_cpu_inc(miniq->cpu_qstats->drops);
1304 struct mini_Qdisc_pair {
1305 struct mini_Qdisc miniq1;
1306 struct mini_Qdisc miniq2;
1307 struct mini_Qdisc __rcu **p_miniq;
1310 void mini_qdisc_pair_swap(struct mini_Qdisc_pair *miniqp,
1311 struct tcf_proto *tp_head);
1312 void mini_qdisc_pair_init(struct mini_Qdisc_pair *miniqp, struct Qdisc *qdisc,
1313 struct mini_Qdisc __rcu **p_miniq);
1315 static inline void skb_tc_reinsert(struct sk_buff *skb, struct tcf_result *res)
1317 struct gnet_stats_queue *stats = res->qstats;
1321 ret = netif_receive_skb(skb);
1323 ret = dev_queue_xmit(skb);
1325 qstats_overlimit_inc(res->qstats);