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 <linux/rwsem.h>
17 #include <linux/atomic.h>
18 #include <linux/hashtable.h>
19 #include <net/gen_stats.h>
20 #include <net/rtnetlink.h>
21 #include <net/flow_offload.h>
29 struct qdisc_rate_table {
30 struct tc_ratespec rate;
32 struct qdisc_rate_table *next;
38 __QDISC_STATE_DEACTIVATED,
41 struct qdisc_size_table {
43 struct list_head list;
44 struct tc_sizespec szopts;
49 /* similar to sk_buff_head, but skb->prev pointer is undefined. */
50 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;
112 /* for NOLOCK qdisc, true if there are no enqueued skbs */
117 long privdata[] ____cacheline_aligned;
120 static inline void qdisc_refcount_inc(struct Qdisc *qdisc)
122 if (qdisc->flags & TCQ_F_BUILTIN)
124 refcount_inc(&qdisc->refcnt);
127 /* Intended to be used by unlocked users, when concurrent qdisc release is
131 static inline struct Qdisc *qdisc_refcount_inc_nz(struct Qdisc *qdisc)
133 if (qdisc->flags & TCQ_F_BUILTIN)
135 if (refcount_inc_not_zero(&qdisc->refcnt))
140 static inline bool qdisc_is_running(struct Qdisc *qdisc)
142 if (qdisc->flags & TCQ_F_NOLOCK)
143 return spin_is_locked(&qdisc->seqlock);
144 return (raw_read_seqcount(&qdisc->running) & 1) ? true : false;
147 static inline bool qdisc_is_percpu_stats(const struct Qdisc *q)
149 return q->flags & TCQ_F_CPUSTATS;
152 static inline bool qdisc_is_empty(const struct Qdisc *qdisc)
154 if (qdisc_is_percpu_stats(qdisc))
155 return READ_ONCE(qdisc->empty);
156 return !READ_ONCE(qdisc->q.qlen);
159 static inline bool qdisc_run_begin(struct Qdisc *qdisc)
161 if (qdisc->flags & TCQ_F_NOLOCK) {
162 if (!spin_trylock(&qdisc->seqlock))
164 WRITE_ONCE(qdisc->empty, false);
165 } else if (qdisc_is_running(qdisc)) {
168 /* Variant of write_seqcount_begin() telling lockdep a trylock
171 raw_write_seqcount_begin(&qdisc->running);
172 seqcount_acquire(&qdisc->running.dep_map, 0, 1, _RET_IP_);
176 static inline void qdisc_run_end(struct Qdisc *qdisc)
178 write_seqcount_end(&qdisc->running);
179 if (qdisc->flags & TCQ_F_NOLOCK)
180 spin_unlock(&qdisc->seqlock);
183 static inline bool qdisc_may_bulk(const struct Qdisc *qdisc)
185 return qdisc->flags & TCQ_F_ONETXQUEUE;
188 static inline int qdisc_avail_bulklimit(const struct netdev_queue *txq)
191 /* Non-BQL migrated drivers will return 0, too. */
192 return dql_avail(&txq->dql);
198 struct Qdisc_class_ops {
200 /* Child qdisc manipulation */
201 struct netdev_queue * (*select_queue)(struct Qdisc *, struct tcmsg *);
202 int (*graft)(struct Qdisc *, unsigned long cl,
203 struct Qdisc *, struct Qdisc **,
204 struct netlink_ext_ack *extack);
205 struct Qdisc * (*leaf)(struct Qdisc *, unsigned long cl);
206 void (*qlen_notify)(struct Qdisc *, unsigned long);
208 /* Class manipulation routines */
209 unsigned long (*find)(struct Qdisc *, u32 classid);
210 int (*change)(struct Qdisc *, u32, u32,
211 struct nlattr **, unsigned long *,
212 struct netlink_ext_ack *);
213 int (*delete)(struct Qdisc *, unsigned long);
214 void (*walk)(struct Qdisc *, struct qdisc_walker * arg);
216 /* Filter manipulation */
217 struct tcf_block * (*tcf_block)(struct Qdisc *sch,
219 struct netlink_ext_ack *extack);
220 unsigned long (*bind_tcf)(struct Qdisc *, unsigned long,
222 void (*unbind_tcf)(struct Qdisc *, unsigned long);
224 /* rtnetlink specific */
225 int (*dump)(struct Qdisc *, unsigned long,
226 struct sk_buff *skb, struct tcmsg*);
227 int (*dump_stats)(struct Qdisc *, unsigned long,
231 /* Qdisc_class_ops flag values */
233 /* Implements API that doesn't require rtnl lock */
234 enum qdisc_class_ops_flags {
235 QDISC_CLASS_OPS_DOIT_UNLOCKED = 1,
239 struct Qdisc_ops *next;
240 const struct Qdisc_class_ops *cl_ops;
243 unsigned int static_flags;
245 int (*enqueue)(struct sk_buff *skb,
247 struct sk_buff **to_free);
248 struct sk_buff * (*dequeue)(struct Qdisc *);
249 struct sk_buff * (*peek)(struct Qdisc *);
251 int (*init)(struct Qdisc *sch, struct nlattr *arg,
252 struct netlink_ext_ack *extack);
253 void (*reset)(struct Qdisc *);
254 void (*destroy)(struct Qdisc *);
255 int (*change)(struct Qdisc *sch,
257 struct netlink_ext_ack *extack);
258 void (*attach)(struct Qdisc *sch);
259 int (*change_tx_queue_len)(struct Qdisc *, unsigned int);
261 int (*dump)(struct Qdisc *, struct sk_buff *);
262 int (*dump_stats)(struct Qdisc *, struct gnet_dump *);
264 void (*ingress_block_set)(struct Qdisc *sch,
266 void (*egress_block_set)(struct Qdisc *sch,
268 u32 (*ingress_block_get)(struct Qdisc *sch);
269 u32 (*egress_block_get)(struct Qdisc *sch);
271 struct module *owner;
281 const struct tcf_proto *goto_tp;
283 /* used in the skb_tc_reinsert function */
286 struct gnet_stats_queue *qstats;
293 struct tcf_proto_ops {
294 struct list_head head;
297 int (*classify)(struct sk_buff *,
298 const struct tcf_proto *,
299 struct tcf_result *);
300 int (*init)(struct tcf_proto*);
301 void (*destroy)(struct tcf_proto *tp, bool rtnl_held,
302 struct netlink_ext_ack *extack);
304 void* (*get)(struct tcf_proto*, u32 handle);
305 void (*put)(struct tcf_proto *tp, void *f);
306 int (*change)(struct net *net, struct sk_buff *,
307 struct tcf_proto*, unsigned long,
308 u32 handle, struct nlattr **,
310 struct netlink_ext_ack *);
311 int (*delete)(struct tcf_proto *tp, void *arg,
312 bool *last, bool rtnl_held,
313 struct netlink_ext_ack *);
314 bool (*delete_empty)(struct tcf_proto *tp);
315 void (*walk)(struct tcf_proto *tp,
316 struct tcf_walker *arg, bool rtnl_held);
317 int (*reoffload)(struct tcf_proto *tp, bool add,
318 flow_setup_cb_t *cb, void *cb_priv,
319 struct netlink_ext_ack *extack);
320 void (*hw_add)(struct tcf_proto *tp,
322 void (*hw_del)(struct tcf_proto *tp,
324 void (*bind_class)(void *, u32, unsigned long,
325 void *, unsigned long);
326 void * (*tmplt_create)(struct net *net,
327 struct tcf_chain *chain,
329 struct netlink_ext_ack *extack);
330 void (*tmplt_destroy)(void *tmplt_priv);
332 /* rtnetlink specific */
333 int (*dump)(struct net*, struct tcf_proto*, void *,
334 struct sk_buff *skb, struct tcmsg*,
336 int (*terse_dump)(struct net *net,
337 struct tcf_proto *tp, void *fh,
339 struct tcmsg *t, bool rtnl_held);
340 int (*tmplt_dump)(struct sk_buff *skb,
344 struct module *owner;
348 /* Classifiers setting TCF_PROTO_OPS_DOIT_UNLOCKED in tcf_proto_ops->flags
349 * are expected to implement tcf_proto_ops->delete_empty(), otherwise race
350 * conditions can occur when filters are inserted/deleted simultaneously.
352 enum tcf_proto_ops_flags {
353 TCF_PROTO_OPS_DOIT_UNLOCKED = 1,
357 /* Fast access part */
358 struct tcf_proto __rcu *next;
361 /* called under RCU BH lock*/
362 int (*classify)(struct sk_buff *,
363 const struct tcf_proto *,
364 struct tcf_result *);
370 const struct tcf_proto_ops *ops;
371 struct tcf_chain *chain;
372 /* Lock protects tcf_proto shared state and can be used by unlocked
373 * classifiers to protect their private data.
379 struct hlist_node destroy_ht_node;
382 struct qdisc_skb_cb {
384 unsigned int pkt_len;
385 u16 slave_dev_queue_mapping;
388 #define QDISC_CB_PRIV_LEN 20
389 unsigned char data[QDISC_CB_PRIV_LEN];
393 typedef void tcf_chain_head_change_t(struct tcf_proto *tp_head, void *priv);
396 /* Protects filter_chain. */
397 struct mutex filter_chain_lock;
398 struct tcf_proto __rcu *filter_chain;
399 struct list_head list;
400 struct tcf_block *block;
401 u32 index; /* chain index */
403 unsigned int action_refcnt;
404 bool explicitly_created;
406 const struct tcf_proto_ops *tmplt_ops;
412 /* Lock protects tcf_block and lifetime-management data of chains
413 * attached to the block (refcnt, action_refcnt, explicitly_created).
416 struct list_head chain_list;
417 u32 index; /* block index for shared blocks */
418 u32 classid; /* which class this block belongs to */
422 struct rw_semaphore cb_lock; /* protects cb_list and offload counters */
423 struct flow_block flow_block;
424 struct list_head owner_list;
426 atomic_t offloadcnt; /* Number of oddloaded filters */
427 unsigned int nooffloaddevcnt; /* Number of devs unable to do offload */
428 unsigned int lockeddevcnt; /* Number of devs that require rtnl lock. */
430 struct tcf_chain *chain;
431 struct list_head filter_chain_list;
434 DECLARE_HASHTABLE(proto_destroy_ht, 7);
435 struct mutex proto_destroy_lock; /* Lock for proto_destroy hashtable. */
438 #ifdef CONFIG_PROVE_LOCKING
439 static inline bool lockdep_tcf_chain_is_locked(struct tcf_chain *chain)
441 return lockdep_is_held(&chain->filter_chain_lock);
444 static inline bool lockdep_tcf_proto_is_locked(struct tcf_proto *tp)
446 return lockdep_is_held(&tp->lock);
449 static inline bool lockdep_tcf_chain_is_locked(struct tcf_block *chain)
454 static inline bool lockdep_tcf_proto_is_locked(struct tcf_proto *tp)
458 #endif /* #ifdef CONFIG_PROVE_LOCKING */
460 #define tcf_chain_dereference(p, chain) \
461 rcu_dereference_protected(p, lockdep_tcf_chain_is_locked(chain))
463 #define tcf_proto_dereference(p, tp) \
464 rcu_dereference_protected(p, lockdep_tcf_proto_is_locked(tp))
466 static inline void qdisc_cb_private_validate(const struct sk_buff *skb, int sz)
468 struct qdisc_skb_cb *qcb;
470 BUILD_BUG_ON(sizeof(skb->cb) < sizeof(*qcb));
471 BUILD_BUG_ON(sizeof(qcb->data) < sz);
474 static inline int qdisc_qlen_cpu(const struct Qdisc *q)
476 return this_cpu_ptr(q->cpu_qstats)->qlen;
479 static inline int qdisc_qlen(const struct Qdisc *q)
484 static inline int qdisc_qlen_sum(const struct Qdisc *q)
486 __u32 qlen = q->qstats.qlen;
489 if (qdisc_is_percpu_stats(q)) {
490 for_each_possible_cpu(i)
491 qlen += per_cpu_ptr(q->cpu_qstats, i)->qlen;
499 static inline struct qdisc_skb_cb *qdisc_skb_cb(const struct sk_buff *skb)
501 return (struct qdisc_skb_cb *)skb->cb;
504 static inline spinlock_t *qdisc_lock(struct Qdisc *qdisc)
506 return &qdisc->q.lock;
509 static inline struct Qdisc *qdisc_root(const struct Qdisc *qdisc)
511 struct Qdisc *q = rcu_dereference_rtnl(qdisc->dev_queue->qdisc);
516 static inline struct Qdisc *qdisc_root_bh(const struct Qdisc *qdisc)
518 return rcu_dereference_bh(qdisc->dev_queue->qdisc);
521 static inline struct Qdisc *qdisc_root_sleeping(const struct Qdisc *qdisc)
523 return qdisc->dev_queue->qdisc_sleeping;
526 /* The qdisc root lock is a mechanism by which to top level
527 * of a qdisc tree can be locked from any qdisc node in the
528 * forest. This allows changing the configuration of some
529 * aspect of the qdisc tree while blocking out asynchronous
530 * qdisc access in the packet processing paths.
532 * It is only legal to do this when the root will not change
533 * on us. Otherwise we'll potentially lock the wrong qdisc
534 * root. This is enforced by holding the RTNL semaphore, which
535 * all users of this lock accessor must do.
537 static inline spinlock_t *qdisc_root_lock(const struct Qdisc *qdisc)
539 struct Qdisc *root = qdisc_root(qdisc);
542 return qdisc_lock(root);
545 static inline spinlock_t *qdisc_root_sleeping_lock(const struct Qdisc *qdisc)
547 struct Qdisc *root = qdisc_root_sleeping(qdisc);
550 return qdisc_lock(root);
553 static inline seqcount_t *qdisc_root_sleeping_running(const struct Qdisc *qdisc)
555 struct Qdisc *root = qdisc_root_sleeping(qdisc);
558 return &root->running;
561 static inline struct net_device *qdisc_dev(const struct Qdisc *qdisc)
563 return qdisc->dev_queue->dev;
566 static inline void sch_tree_lock(const struct Qdisc *q)
568 spin_lock_bh(qdisc_root_sleeping_lock(q));
571 static inline void sch_tree_unlock(const struct Qdisc *q)
573 spin_unlock_bh(qdisc_root_sleeping_lock(q));
576 extern struct Qdisc noop_qdisc;
577 extern struct Qdisc_ops noop_qdisc_ops;
578 extern struct Qdisc_ops pfifo_fast_ops;
579 extern struct Qdisc_ops mq_qdisc_ops;
580 extern struct Qdisc_ops noqueue_qdisc_ops;
581 extern const struct Qdisc_ops *default_qdisc_ops;
582 static inline const struct Qdisc_ops *
583 get_default_qdisc_ops(const struct net_device *dev, int ntx)
585 return ntx < dev->real_num_tx_queues ?
586 default_qdisc_ops : &pfifo_fast_ops;
589 struct Qdisc_class_common {
591 struct hlist_node hnode;
594 struct Qdisc_class_hash {
595 struct hlist_head *hash;
596 unsigned int hashsize;
597 unsigned int hashmask;
598 unsigned int hashelems;
601 static inline unsigned int qdisc_class_hash(u32 id, u32 mask)
608 static inline struct Qdisc_class_common *
609 qdisc_class_find(const struct Qdisc_class_hash *hash, u32 id)
611 struct Qdisc_class_common *cl;
617 h = qdisc_class_hash(id, hash->hashmask);
618 hlist_for_each_entry(cl, &hash->hash[h], hnode) {
619 if (cl->classid == id)
625 static inline int tc_classid_to_hwtc(struct net_device *dev, u32 classid)
627 u32 hwtc = TC_H_MIN(classid) - TC_H_MIN_PRIORITY;
629 return (hwtc < netdev_get_num_tc(dev)) ? hwtc : -EINVAL;
632 int qdisc_class_hash_init(struct Qdisc_class_hash *);
633 void qdisc_class_hash_insert(struct Qdisc_class_hash *,
634 struct Qdisc_class_common *);
635 void qdisc_class_hash_remove(struct Qdisc_class_hash *,
636 struct Qdisc_class_common *);
637 void qdisc_class_hash_grow(struct Qdisc *, struct Qdisc_class_hash *);
638 void qdisc_class_hash_destroy(struct Qdisc_class_hash *);
640 int dev_qdisc_change_tx_queue_len(struct net_device *dev);
641 void dev_init_scheduler(struct net_device *dev);
642 void dev_shutdown(struct net_device *dev);
643 void dev_activate(struct net_device *dev);
644 void dev_deactivate(struct net_device *dev);
645 void dev_deactivate_many(struct list_head *head);
646 struct Qdisc *dev_graft_qdisc(struct netdev_queue *dev_queue,
647 struct Qdisc *qdisc);
648 void qdisc_reset(struct Qdisc *qdisc);
649 void qdisc_put(struct Qdisc *qdisc);
650 void qdisc_put_unlocked(struct Qdisc *qdisc);
651 void qdisc_tree_reduce_backlog(struct Qdisc *qdisc, int n, int len);
652 #ifdef CONFIG_NET_SCHED
653 int qdisc_offload_dump_helper(struct Qdisc *q, enum tc_setup_type type,
655 void qdisc_offload_graft_helper(struct net_device *dev, struct Qdisc *sch,
656 struct Qdisc *new, struct Qdisc *old,
657 enum tc_setup_type type, void *type_data,
658 struct netlink_ext_ack *extack);
661 qdisc_offload_dump_helper(struct Qdisc *q, enum tc_setup_type type,
664 q->flags &= ~TCQ_F_OFFLOADED;
669 qdisc_offload_graft_helper(struct net_device *dev, struct Qdisc *sch,
670 struct Qdisc *new, struct Qdisc *old,
671 enum tc_setup_type type, void *type_data,
672 struct netlink_ext_ack *extack)
676 struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue,
677 const struct Qdisc_ops *ops,
678 struct netlink_ext_ack *extack);
679 void qdisc_free(struct Qdisc *qdisc);
680 struct Qdisc *qdisc_create_dflt(struct netdev_queue *dev_queue,
681 const struct Qdisc_ops *ops, u32 parentid,
682 struct netlink_ext_ack *extack);
683 void __qdisc_calculate_pkt_len(struct sk_buff *skb,
684 const struct qdisc_size_table *stab);
685 int skb_do_redirect(struct sk_buff *);
687 static inline bool skb_at_tc_ingress(const struct sk_buff *skb)
689 #ifdef CONFIG_NET_CLS_ACT
690 return skb->tc_at_ingress;
696 static inline bool skb_skip_tc_classify(struct sk_buff *skb)
698 #ifdef CONFIG_NET_CLS_ACT
699 if (skb->tc_skip_classify) {
700 skb->tc_skip_classify = 0;
707 /* Reset all TX qdiscs greater than index of a device. */
708 static inline void qdisc_reset_all_tx_gt(struct net_device *dev, unsigned int i)
712 for (; i < dev->num_tx_queues; i++) {
713 qdisc = rtnl_dereference(netdev_get_tx_queue(dev, i)->qdisc);
715 spin_lock_bh(qdisc_lock(qdisc));
717 spin_unlock_bh(qdisc_lock(qdisc));
722 /* Are all TX queues of the device empty? */
723 static inline bool qdisc_all_tx_empty(const struct net_device *dev)
728 for (i = 0; i < dev->num_tx_queues; i++) {
729 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
730 const struct Qdisc *q = rcu_dereference(txq->qdisc);
732 if (!qdisc_is_empty(q)) {
741 /* Are any of the TX qdiscs changing? */
742 static inline bool qdisc_tx_changing(const struct net_device *dev)
746 for (i = 0; i < dev->num_tx_queues; i++) {
747 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
748 if (rcu_access_pointer(txq->qdisc) != txq->qdisc_sleeping)
754 /* Is the device using the noop qdisc on all queues? */
755 static inline bool qdisc_tx_is_noop(const struct net_device *dev)
759 for (i = 0; i < dev->num_tx_queues; i++) {
760 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
761 if (rcu_access_pointer(txq->qdisc) != &noop_qdisc)
767 static inline unsigned int qdisc_pkt_len(const struct sk_buff *skb)
769 return qdisc_skb_cb(skb)->pkt_len;
772 /* additional qdisc xmit flags (NET_XMIT_MASK in linux/netdevice.h) */
773 enum net_xmit_qdisc_t {
774 __NET_XMIT_STOLEN = 0x00010000,
775 __NET_XMIT_BYPASS = 0x00020000,
778 #ifdef CONFIG_NET_CLS_ACT
779 #define net_xmit_drop_count(e) ((e) & __NET_XMIT_STOLEN ? 0 : 1)
781 #define net_xmit_drop_count(e) (1)
784 static inline void qdisc_calculate_pkt_len(struct sk_buff *skb,
785 const struct Qdisc *sch)
787 #ifdef CONFIG_NET_SCHED
788 struct qdisc_size_table *stab = rcu_dereference_bh(sch->stab);
791 __qdisc_calculate_pkt_len(skb, stab);
795 static inline int qdisc_enqueue(struct sk_buff *skb, struct Qdisc *sch,
796 struct sk_buff **to_free)
798 qdisc_calculate_pkt_len(skb, sch);
799 return sch->enqueue(skb, sch, to_free);
802 static inline void _bstats_update(struct gnet_stats_basic_packed *bstats,
803 __u64 bytes, __u32 packets)
805 bstats->bytes += bytes;
806 bstats->packets += packets;
809 static inline void bstats_update(struct gnet_stats_basic_packed *bstats,
810 const struct sk_buff *skb)
812 _bstats_update(bstats,
814 skb_is_gso(skb) ? skb_shinfo(skb)->gso_segs : 1);
817 static inline void _bstats_cpu_update(struct gnet_stats_basic_cpu *bstats,
818 __u64 bytes, __u32 packets)
820 u64_stats_update_begin(&bstats->syncp);
821 _bstats_update(&bstats->bstats, bytes, packets);
822 u64_stats_update_end(&bstats->syncp);
825 static inline void bstats_cpu_update(struct gnet_stats_basic_cpu *bstats,
826 const struct sk_buff *skb)
828 u64_stats_update_begin(&bstats->syncp);
829 bstats_update(&bstats->bstats, skb);
830 u64_stats_update_end(&bstats->syncp);
833 static inline void qdisc_bstats_cpu_update(struct Qdisc *sch,
834 const struct sk_buff *skb)
836 bstats_cpu_update(this_cpu_ptr(sch->cpu_bstats), skb);
839 static inline void qdisc_bstats_update(struct Qdisc *sch,
840 const struct sk_buff *skb)
842 bstats_update(&sch->bstats, skb);
845 static inline void qdisc_qstats_backlog_dec(struct Qdisc *sch,
846 const struct sk_buff *skb)
848 sch->qstats.backlog -= qdisc_pkt_len(skb);
851 static inline void qdisc_qstats_cpu_backlog_dec(struct Qdisc *sch,
852 const struct sk_buff *skb)
854 this_cpu_sub(sch->cpu_qstats->backlog, qdisc_pkt_len(skb));
857 static inline void qdisc_qstats_backlog_inc(struct Qdisc *sch,
858 const struct sk_buff *skb)
860 sch->qstats.backlog += qdisc_pkt_len(skb);
863 static inline void qdisc_qstats_cpu_backlog_inc(struct Qdisc *sch,
864 const struct sk_buff *skb)
866 this_cpu_add(sch->cpu_qstats->backlog, qdisc_pkt_len(skb));
869 static inline void qdisc_qstats_cpu_qlen_inc(struct Qdisc *sch)
871 this_cpu_inc(sch->cpu_qstats->qlen);
874 static inline void qdisc_qstats_cpu_qlen_dec(struct Qdisc *sch)
876 this_cpu_dec(sch->cpu_qstats->qlen);
879 static inline void qdisc_qstats_cpu_requeues_inc(struct Qdisc *sch)
881 this_cpu_inc(sch->cpu_qstats->requeues);
884 static inline void __qdisc_qstats_drop(struct Qdisc *sch, int count)
886 sch->qstats.drops += count;
889 static inline void qstats_drop_inc(struct gnet_stats_queue *qstats)
894 static inline void qstats_overlimit_inc(struct gnet_stats_queue *qstats)
896 qstats->overlimits++;
899 static inline void qdisc_qstats_drop(struct Qdisc *sch)
901 qstats_drop_inc(&sch->qstats);
904 static inline void qdisc_qstats_cpu_drop(struct Qdisc *sch)
906 this_cpu_inc(sch->cpu_qstats->drops);
909 static inline void qdisc_qstats_overlimit(struct Qdisc *sch)
911 sch->qstats.overlimits++;
914 static inline int qdisc_qstats_copy(struct gnet_dump *d, struct Qdisc *sch)
916 __u32 qlen = qdisc_qlen_sum(sch);
918 return gnet_stats_copy_queue(d, sch->cpu_qstats, &sch->qstats, qlen);
921 static inline void qdisc_qstats_qlen_backlog(struct Qdisc *sch, __u32 *qlen,
924 struct gnet_stats_queue qstats = { 0 };
925 __u32 len = qdisc_qlen_sum(sch);
927 __gnet_stats_copy_queue(&qstats, sch->cpu_qstats, &sch->qstats, len);
929 *backlog = qstats.backlog;
932 static inline void qdisc_tree_flush_backlog(struct Qdisc *sch)
936 qdisc_qstats_qlen_backlog(sch, &qlen, &backlog);
937 qdisc_tree_reduce_backlog(sch, qlen, backlog);
940 static inline void qdisc_purge_queue(struct Qdisc *sch)
944 qdisc_qstats_qlen_backlog(sch, &qlen, &backlog);
946 qdisc_tree_reduce_backlog(sch, qlen, backlog);
949 static inline void qdisc_skb_head_init(struct qdisc_skb_head *qh)
956 static inline void __qdisc_enqueue_tail(struct sk_buff *skb,
957 struct qdisc_skb_head *qh)
959 struct sk_buff *last = qh->tail;
972 static inline int qdisc_enqueue_tail(struct sk_buff *skb, struct Qdisc *sch)
974 __qdisc_enqueue_tail(skb, &sch->q);
975 qdisc_qstats_backlog_inc(sch, skb);
976 return NET_XMIT_SUCCESS;
979 static inline void __qdisc_enqueue_head(struct sk_buff *skb,
980 struct qdisc_skb_head *qh)
982 skb->next = qh->head;
990 static inline struct sk_buff *__qdisc_dequeue_head(struct qdisc_skb_head *qh)
992 struct sk_buff *skb = qh->head;
994 if (likely(skb != NULL)) {
995 qh->head = skb->next;
997 if (qh->head == NULL)
1005 static inline struct sk_buff *qdisc_dequeue_head(struct Qdisc *sch)
1007 struct sk_buff *skb = __qdisc_dequeue_head(&sch->q);
1009 if (likely(skb != NULL)) {
1010 qdisc_qstats_backlog_dec(sch, skb);
1011 qdisc_bstats_update(sch, skb);
1017 /* Instead of calling kfree_skb() while root qdisc lock is held,
1018 * queue the skb for future freeing at end of __dev_xmit_skb()
1020 static inline void __qdisc_drop(struct sk_buff *skb, struct sk_buff **to_free)
1022 skb->next = *to_free;
1026 static inline void __qdisc_drop_all(struct sk_buff *skb,
1027 struct sk_buff **to_free)
1030 skb->prev->next = *to_free;
1032 skb->next = *to_free;
1036 static inline unsigned int __qdisc_queue_drop_head(struct Qdisc *sch,
1037 struct qdisc_skb_head *qh,
1038 struct sk_buff **to_free)
1040 struct sk_buff *skb = __qdisc_dequeue_head(qh);
1042 if (likely(skb != NULL)) {
1043 unsigned int len = qdisc_pkt_len(skb);
1045 qdisc_qstats_backlog_dec(sch, skb);
1046 __qdisc_drop(skb, to_free);
1053 static inline struct sk_buff *qdisc_peek_head(struct Qdisc *sch)
1055 const struct qdisc_skb_head *qh = &sch->q;
1060 /* generic pseudo peek method for non-work-conserving qdisc */
1061 static inline struct sk_buff *qdisc_peek_dequeued(struct Qdisc *sch)
1063 struct sk_buff *skb = skb_peek(&sch->gso_skb);
1065 /* we can reuse ->gso_skb because peek isn't called for root qdiscs */
1067 skb = sch->dequeue(sch);
1070 __skb_queue_head(&sch->gso_skb, skb);
1071 /* it's still part of the queue */
1072 qdisc_qstats_backlog_inc(sch, skb);
1080 static inline void qdisc_update_stats_at_dequeue(struct Qdisc *sch,
1081 struct sk_buff *skb)
1083 if (qdisc_is_percpu_stats(sch)) {
1084 qdisc_qstats_cpu_backlog_dec(sch, skb);
1085 qdisc_bstats_cpu_update(sch, skb);
1086 qdisc_qstats_cpu_qlen_dec(sch);
1088 qdisc_qstats_backlog_dec(sch, skb);
1089 qdisc_bstats_update(sch, skb);
1094 static inline void qdisc_update_stats_at_enqueue(struct Qdisc *sch,
1095 unsigned int pkt_len)
1097 if (qdisc_is_percpu_stats(sch)) {
1098 qdisc_qstats_cpu_qlen_inc(sch);
1099 this_cpu_add(sch->cpu_qstats->backlog, pkt_len);
1101 sch->qstats.backlog += pkt_len;
1106 /* use instead of qdisc->dequeue() for all qdiscs queried with ->peek() */
1107 static inline struct sk_buff *qdisc_dequeue_peeked(struct Qdisc *sch)
1109 struct sk_buff *skb = skb_peek(&sch->gso_skb);
1112 skb = __skb_dequeue(&sch->gso_skb);
1113 if (qdisc_is_percpu_stats(sch)) {
1114 qdisc_qstats_cpu_backlog_dec(sch, skb);
1115 qdisc_qstats_cpu_qlen_dec(sch);
1117 qdisc_qstats_backlog_dec(sch, skb);
1121 skb = sch->dequeue(sch);
1127 static inline void __qdisc_reset_queue(struct qdisc_skb_head *qh)
1130 * We do not know the backlog in bytes of this list, it
1131 * is up to the caller to correct it
1135 rtnl_kfree_skbs(qh->head, qh->tail);
1143 static inline void qdisc_reset_queue(struct Qdisc *sch)
1145 __qdisc_reset_queue(&sch->q);
1146 sch->qstats.backlog = 0;
1149 static inline struct Qdisc *qdisc_replace(struct Qdisc *sch, struct Qdisc *new,
1150 struct Qdisc **pold)
1158 qdisc_tree_flush_backlog(old);
1159 sch_tree_unlock(sch);
1164 static inline void rtnl_qdisc_drop(struct sk_buff *skb, struct Qdisc *sch)
1166 rtnl_kfree_skbs(skb, skb);
1167 qdisc_qstats_drop(sch);
1170 static inline int qdisc_drop_cpu(struct sk_buff *skb, struct Qdisc *sch,
1171 struct sk_buff **to_free)
1173 __qdisc_drop(skb, to_free);
1174 qdisc_qstats_cpu_drop(sch);
1176 return NET_XMIT_DROP;
1179 static inline int qdisc_drop(struct sk_buff *skb, struct Qdisc *sch,
1180 struct sk_buff **to_free)
1182 __qdisc_drop(skb, to_free);
1183 qdisc_qstats_drop(sch);
1185 return NET_XMIT_DROP;
1188 static inline int qdisc_drop_all(struct sk_buff *skb, struct Qdisc *sch,
1189 struct sk_buff **to_free)
1191 __qdisc_drop_all(skb, to_free);
1192 qdisc_qstats_drop(sch);
1194 return NET_XMIT_DROP;
1197 /* Length to Time (L2T) lookup in a qdisc_rate_table, to determine how
1198 long it will take to send a packet given its size.
1200 static inline u32 qdisc_l2t(struct qdisc_rate_table* rtab, unsigned int pktlen)
1202 int slot = pktlen + rtab->rate.cell_align + rtab->rate.overhead;
1205 slot >>= rtab->rate.cell_log;
1207 return rtab->data[255]*(slot >> 8) + rtab->data[slot & 0xFF];
1208 return rtab->data[slot];
1211 struct psched_ratecfg {
1212 u64 rate_bytes_ps; /* bytes per second */
1219 static inline u64 psched_l2t_ns(const struct psched_ratecfg *r,
1224 if (unlikely(r->linklayer == TC_LINKLAYER_ATM))
1225 return ((u64)(DIV_ROUND_UP(len,48)*53) * r->mult) >> r->shift;
1227 return ((u64)len * r->mult) >> r->shift;
1230 void psched_ratecfg_precompute(struct psched_ratecfg *r,
1231 const struct tc_ratespec *conf,
1234 static inline void psched_ratecfg_getrate(struct tc_ratespec *res,
1235 const struct psched_ratecfg *r)
1237 memset(res, 0, sizeof(*res));
1239 /* legacy struct tc_ratespec has a 32bit @rate field
1240 * Qdisc using 64bit rate should add new attributes
1241 * in order to maintain compatibility.
1243 res->rate = min_t(u64, r->rate_bytes_ps, ~0U);
1245 res->overhead = r->overhead;
1246 res->linklayer = (r->linklayer & TC_LINKLAYER_MASK);
1249 /* Mini Qdisc serves for specific needs of ingress/clsact Qdisc.
1250 * The fast path only needs to access filter list and to update stats
1253 struct tcf_proto *filter_list;
1254 struct tcf_block *block;
1255 struct gnet_stats_basic_cpu __percpu *cpu_bstats;
1256 struct gnet_stats_queue __percpu *cpu_qstats;
1257 struct rcu_head rcu;
1260 static inline void mini_qdisc_bstats_cpu_update(struct mini_Qdisc *miniq,
1261 const struct sk_buff *skb)
1263 bstats_cpu_update(this_cpu_ptr(miniq->cpu_bstats), skb);
1266 static inline void mini_qdisc_qstats_cpu_drop(struct mini_Qdisc *miniq)
1268 this_cpu_inc(miniq->cpu_qstats->drops);
1271 struct mini_Qdisc_pair {
1272 struct mini_Qdisc miniq1;
1273 struct mini_Qdisc miniq2;
1274 struct mini_Qdisc __rcu **p_miniq;
1277 void mini_qdisc_pair_swap(struct mini_Qdisc_pair *miniqp,
1278 struct tcf_proto *tp_head);
1279 void mini_qdisc_pair_init(struct mini_Qdisc_pair *miniqp, struct Qdisc *qdisc,
1280 struct mini_Qdisc __rcu **p_miniq);
1281 void mini_qdisc_pair_block_init(struct mini_Qdisc_pair *miniqp,
1282 struct tcf_block *block);