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 typedef int tc_indr_block_bind_cb_t(struct net_device *dev, void *cb_priv,
28 enum tc_setup_type type, void *type_data);
30 struct qdisc_rate_table {
31 struct tc_ratespec rate;
33 struct qdisc_rate_table *next;
39 __QDISC_STATE_DEACTIVATED,
42 struct qdisc_size_table {
44 struct list_head list;
45 struct tc_sizespec szopts;
50 /* similar to sk_buff_head, but skb->prev pointer is undefined. */
51 struct qdisc_skb_head {
59 int (*enqueue)(struct sk_buff *skb,
61 struct sk_buff **to_free);
62 struct sk_buff * (*dequeue)(struct Qdisc *sch);
64 #define TCQ_F_BUILTIN 1
65 #define TCQ_F_INGRESS 2
66 #define TCQ_F_CAN_BYPASS 4
67 #define TCQ_F_MQROOT 8
68 #define TCQ_F_ONETXQUEUE 0x10 /* dequeue_skb() can assume all skbs are for
69 * q->dev_queue : It can test
70 * netif_xmit_frozen_or_stopped() before
71 * dequeueing next packet.
72 * Its true for MQ/MQPRIO slaves, or non
75 #define TCQ_F_WARN_NONWC (1 << 16)
76 #define TCQ_F_CPUSTATS 0x20 /* run using percpu statistics */
77 #define TCQ_F_NOPARENT 0x40 /* root of its hierarchy :
78 * qdisc_tree_decrease_qlen() should stop.
80 #define TCQ_F_INVISIBLE 0x80 /* invisible by default in dump */
81 #define TCQ_F_NOLOCK 0x100 /* qdisc does not require locking */
82 #define TCQ_F_OFFLOADED 0x200 /* qdisc is offloaded to HW */
84 const struct Qdisc_ops *ops;
85 struct qdisc_size_table __rcu *stab;
86 struct hlist_node hash;
90 struct netdev_queue *dev_queue;
92 struct net_rate_estimator __rcu *rate_est;
93 struct gnet_stats_basic_cpu __percpu *cpu_bstats;
94 struct gnet_stats_queue __percpu *cpu_qstats;
99 * For performance sake on SMP, we put highly modified fields at the end
101 struct sk_buff_head gso_skb ____cacheline_aligned_in_smp;
102 struct qdisc_skb_head q;
103 struct gnet_stats_basic_packed bstats;
105 struct gnet_stats_queue qstats;
107 struct Qdisc *next_sched;
108 struct sk_buff_head skb_bad_txq;
110 spinlock_t busylock ____cacheline_aligned_in_smp;
115 static inline void qdisc_refcount_inc(struct Qdisc *qdisc)
117 if (qdisc->flags & TCQ_F_BUILTIN)
119 refcount_inc(&qdisc->refcnt);
122 /* Intended to be used by unlocked users, when concurrent qdisc release is
126 static inline struct Qdisc *qdisc_refcount_inc_nz(struct Qdisc *qdisc)
128 if (qdisc->flags & TCQ_F_BUILTIN)
130 if (refcount_inc_not_zero(&qdisc->refcnt))
135 static inline bool qdisc_is_running(struct Qdisc *qdisc)
137 if (qdisc->flags & TCQ_F_NOLOCK)
138 return spin_is_locked(&qdisc->seqlock);
139 return (raw_read_seqcount(&qdisc->running) & 1) ? true : false;
142 static inline bool qdisc_run_begin(struct Qdisc *qdisc)
144 if (qdisc->flags & TCQ_F_NOLOCK) {
145 if (!spin_trylock(&qdisc->seqlock))
147 } else if (qdisc_is_running(qdisc)) {
150 /* Variant of write_seqcount_begin() telling lockdep a trylock
153 raw_write_seqcount_begin(&qdisc->running);
154 seqcount_acquire(&qdisc->running.dep_map, 0, 1, _RET_IP_);
158 static inline void qdisc_run_end(struct Qdisc *qdisc)
160 write_seqcount_end(&qdisc->running);
161 if (qdisc->flags & TCQ_F_NOLOCK)
162 spin_unlock(&qdisc->seqlock);
165 static inline bool qdisc_may_bulk(const struct Qdisc *qdisc)
167 return qdisc->flags & TCQ_F_ONETXQUEUE;
170 static inline int qdisc_avail_bulklimit(const struct netdev_queue *txq)
173 /* Non-BQL migrated drivers will return 0, too. */
174 return dql_avail(&txq->dql);
180 struct Qdisc_class_ops {
181 /* Child qdisc manipulation */
182 struct netdev_queue * (*select_queue)(struct Qdisc *, struct tcmsg *);
183 int (*graft)(struct Qdisc *, unsigned long cl,
184 struct Qdisc *, struct Qdisc **,
185 struct netlink_ext_ack *extack);
186 struct Qdisc * (*leaf)(struct Qdisc *, unsigned long cl);
187 void (*qlen_notify)(struct Qdisc *, unsigned long);
189 /* Class manipulation routines */
190 unsigned long (*find)(struct Qdisc *, u32 classid);
191 int (*change)(struct Qdisc *, u32, u32,
192 struct nlattr **, unsigned long *,
193 struct netlink_ext_ack *);
194 int (*delete)(struct Qdisc *, unsigned long);
195 void (*walk)(struct Qdisc *, struct qdisc_walker * arg);
197 /* Filter manipulation */
198 struct tcf_block * (*tcf_block)(struct Qdisc *sch,
200 struct netlink_ext_ack *extack);
201 unsigned long (*bind_tcf)(struct Qdisc *, unsigned long,
203 void (*unbind_tcf)(struct Qdisc *, unsigned long);
205 /* rtnetlink specific */
206 int (*dump)(struct Qdisc *, unsigned long,
207 struct sk_buff *skb, struct tcmsg*);
208 int (*dump_stats)(struct Qdisc *, unsigned long,
213 struct Qdisc_ops *next;
214 const struct Qdisc_class_ops *cl_ops;
217 unsigned int static_flags;
219 int (*enqueue)(struct sk_buff *skb,
221 struct sk_buff **to_free);
222 struct sk_buff * (*dequeue)(struct Qdisc *);
223 struct sk_buff * (*peek)(struct Qdisc *);
225 int (*init)(struct Qdisc *sch, struct nlattr *arg,
226 struct netlink_ext_ack *extack);
227 void (*reset)(struct Qdisc *);
228 void (*destroy)(struct Qdisc *);
229 int (*change)(struct Qdisc *sch,
231 struct netlink_ext_ack *extack);
232 void (*attach)(struct Qdisc *sch);
233 int (*change_tx_queue_len)(struct Qdisc *, unsigned int);
235 int (*dump)(struct Qdisc *, struct sk_buff *);
236 int (*dump_stats)(struct Qdisc *, struct gnet_dump *);
238 void (*ingress_block_set)(struct Qdisc *sch,
240 void (*egress_block_set)(struct Qdisc *sch,
242 u32 (*ingress_block_get)(struct Qdisc *sch);
243 u32 (*egress_block_get)(struct Qdisc *sch);
245 struct module *owner;
255 const struct tcf_proto *goto_tp;
257 /* used by the TC_ACT_REINSERT action */
260 struct gnet_stats_queue *qstats;
267 struct tcf_proto_ops {
268 struct list_head head;
271 int (*classify)(struct sk_buff *,
272 const struct tcf_proto *,
273 struct tcf_result *);
274 int (*init)(struct tcf_proto*);
275 void (*destroy)(struct tcf_proto *tp,
276 struct netlink_ext_ack *extack);
278 void* (*get)(struct tcf_proto*, u32 handle);
279 int (*change)(struct net *net, struct sk_buff *,
280 struct tcf_proto*, unsigned long,
281 u32 handle, struct nlattr **,
283 struct netlink_ext_ack *);
284 int (*delete)(struct tcf_proto *tp, void *arg,
286 struct netlink_ext_ack *);
287 void (*walk)(struct tcf_proto*, struct tcf_walker *arg);
288 int (*reoffload)(struct tcf_proto *tp, bool add,
289 tc_setup_cb_t *cb, void *cb_priv,
290 struct netlink_ext_ack *extack);
291 void (*bind_class)(void *, u32, unsigned long);
292 void * (*tmplt_create)(struct net *net,
293 struct tcf_chain *chain,
295 struct netlink_ext_ack *extack);
296 void (*tmplt_destroy)(void *tmplt_priv);
298 /* rtnetlink specific */
299 int (*dump)(struct net*, struct tcf_proto*, void *,
300 struct sk_buff *skb, struct tcmsg*);
301 int (*tmplt_dump)(struct sk_buff *skb,
305 struct module *owner;
309 /* Fast access part */
310 struct tcf_proto __rcu *next;
313 /* called under RCU BH lock*/
314 int (*classify)(struct sk_buff *,
315 const struct tcf_proto *,
316 struct tcf_result *);
322 const struct tcf_proto_ops *ops;
323 struct tcf_chain *chain;
327 struct qdisc_skb_cb {
330 unsigned int pkt_len;
331 u16 slave_dev_queue_mapping;
334 struct bpf_flow_keys *flow_keys;
336 #define QDISC_CB_PRIV_LEN 20
337 unsigned char data[QDISC_CB_PRIV_LEN];
340 typedef void tcf_chain_head_change_t(struct tcf_proto *tp_head, void *priv);
343 struct tcf_proto __rcu *filter_chain;
344 struct list_head list;
345 struct tcf_block *block;
346 u32 index; /* chain index */
348 unsigned int action_refcnt;
349 bool explicitly_created;
350 const struct tcf_proto_ops *tmplt_ops;
355 struct list_head chain_list;
356 u32 index; /* block index for shared blocks */
360 struct list_head cb_list;
361 struct list_head owner_list;
363 unsigned int offloadcnt; /* Number of oddloaded filters */
364 unsigned int nooffloaddevcnt; /* Number of devs unable to do offload */
366 struct tcf_chain *chain;
367 struct list_head filter_chain_list;
372 static inline void tcf_block_offload_inc(struct tcf_block *block, u32 *flags)
374 if (*flags & TCA_CLS_FLAGS_IN_HW)
376 *flags |= TCA_CLS_FLAGS_IN_HW;
380 static inline void tcf_block_offload_dec(struct tcf_block *block, u32 *flags)
382 if (!(*flags & TCA_CLS_FLAGS_IN_HW))
384 *flags &= ~TCA_CLS_FLAGS_IN_HW;
389 tc_cls_offload_cnt_update(struct tcf_block *block, u32 *cnt,
390 u32 *flags, bool add)
394 tcf_block_offload_inc(block, flags);
399 tcf_block_offload_dec(block, flags);
403 static inline void qdisc_cb_private_validate(const struct sk_buff *skb, int sz)
405 struct qdisc_skb_cb *qcb;
407 BUILD_BUG_ON(sizeof(skb->cb) < offsetof(struct qdisc_skb_cb, data) + sz);
408 BUILD_BUG_ON(sizeof(qcb->data) < sz);
411 static inline int qdisc_qlen_cpu(const struct Qdisc *q)
413 return this_cpu_ptr(q->cpu_qstats)->qlen;
416 static inline int qdisc_qlen(const struct Qdisc *q)
421 static inline int qdisc_qlen_sum(const struct Qdisc *q)
423 __u32 qlen = q->qstats.qlen;
426 if (q->flags & TCQ_F_NOLOCK) {
427 for_each_possible_cpu(i)
428 qlen += per_cpu_ptr(q->cpu_qstats, i)->qlen;
436 static inline struct qdisc_skb_cb *qdisc_skb_cb(const struct sk_buff *skb)
438 return (struct qdisc_skb_cb *)skb->cb;
441 static inline spinlock_t *qdisc_lock(struct Qdisc *qdisc)
443 return &qdisc->q.lock;
446 static inline struct Qdisc *qdisc_root(const struct Qdisc *qdisc)
448 struct Qdisc *q = rcu_dereference_rtnl(qdisc->dev_queue->qdisc);
453 static inline struct Qdisc *qdisc_root_sleeping(const struct Qdisc *qdisc)
455 return qdisc->dev_queue->qdisc_sleeping;
458 /* The qdisc root lock is a mechanism by which to top level
459 * of a qdisc tree can be locked from any qdisc node in the
460 * forest. This allows changing the configuration of some
461 * aspect of the qdisc tree while blocking out asynchronous
462 * qdisc access in the packet processing paths.
464 * It is only legal to do this when the root will not change
465 * on us. Otherwise we'll potentially lock the wrong qdisc
466 * root. This is enforced by holding the RTNL semaphore, which
467 * all users of this lock accessor must do.
469 static inline spinlock_t *qdisc_root_lock(const struct Qdisc *qdisc)
471 struct Qdisc *root = qdisc_root(qdisc);
474 return qdisc_lock(root);
477 static inline spinlock_t *qdisc_root_sleeping_lock(const struct Qdisc *qdisc)
479 struct Qdisc *root = qdisc_root_sleeping(qdisc);
482 return qdisc_lock(root);
485 static inline seqcount_t *qdisc_root_sleeping_running(const struct Qdisc *qdisc)
487 struct Qdisc *root = qdisc_root_sleeping(qdisc);
490 return &root->running;
493 static inline struct net_device *qdisc_dev(const struct Qdisc *qdisc)
495 return qdisc->dev_queue->dev;
498 static inline void sch_tree_lock(const struct Qdisc *q)
500 spin_lock_bh(qdisc_root_sleeping_lock(q));
503 static inline void sch_tree_unlock(const struct Qdisc *q)
505 spin_unlock_bh(qdisc_root_sleeping_lock(q));
508 extern struct Qdisc noop_qdisc;
509 extern struct Qdisc_ops noop_qdisc_ops;
510 extern struct Qdisc_ops pfifo_fast_ops;
511 extern struct Qdisc_ops mq_qdisc_ops;
512 extern struct Qdisc_ops noqueue_qdisc_ops;
513 extern const struct Qdisc_ops *default_qdisc_ops;
514 static inline const struct Qdisc_ops *
515 get_default_qdisc_ops(const struct net_device *dev, int ntx)
517 return ntx < dev->real_num_tx_queues ?
518 default_qdisc_ops : &pfifo_fast_ops;
521 struct Qdisc_class_common {
523 struct hlist_node hnode;
526 struct Qdisc_class_hash {
527 struct hlist_head *hash;
528 unsigned int hashsize;
529 unsigned int hashmask;
530 unsigned int hashelems;
533 static inline unsigned int qdisc_class_hash(u32 id, u32 mask)
540 static inline struct Qdisc_class_common *
541 qdisc_class_find(const struct Qdisc_class_hash *hash, u32 id)
543 struct Qdisc_class_common *cl;
549 h = qdisc_class_hash(id, hash->hashmask);
550 hlist_for_each_entry(cl, &hash->hash[h], hnode) {
551 if (cl->classid == id)
557 static inline int tc_classid_to_hwtc(struct net_device *dev, u32 classid)
559 u32 hwtc = TC_H_MIN(classid) - TC_H_MIN_PRIORITY;
561 return (hwtc < netdev_get_num_tc(dev)) ? hwtc : -EINVAL;
564 int qdisc_class_hash_init(struct Qdisc_class_hash *);
565 void qdisc_class_hash_insert(struct Qdisc_class_hash *,
566 struct Qdisc_class_common *);
567 void qdisc_class_hash_remove(struct Qdisc_class_hash *,
568 struct Qdisc_class_common *);
569 void qdisc_class_hash_grow(struct Qdisc *, struct Qdisc_class_hash *);
570 void qdisc_class_hash_destroy(struct Qdisc_class_hash *);
572 int dev_qdisc_change_tx_queue_len(struct net_device *dev);
573 void dev_init_scheduler(struct net_device *dev);
574 void dev_shutdown(struct net_device *dev);
575 void dev_activate(struct net_device *dev);
576 void dev_deactivate(struct net_device *dev);
577 void dev_deactivate_many(struct list_head *head);
578 struct Qdisc *dev_graft_qdisc(struct netdev_queue *dev_queue,
579 struct Qdisc *qdisc);
580 void qdisc_reset(struct Qdisc *qdisc);
581 void qdisc_put(struct Qdisc *qdisc);
582 void qdisc_put_unlocked(struct Qdisc *qdisc);
583 void qdisc_tree_reduce_backlog(struct Qdisc *qdisc, unsigned int n,
585 #ifdef CONFIG_NET_SCHED
586 int qdisc_offload_dump_helper(struct Qdisc *q, enum tc_setup_type type,
588 void qdisc_offload_graft_helper(struct net_device *dev, struct Qdisc *sch,
589 struct Qdisc *new, struct Qdisc *old,
590 enum tc_setup_type type, void *type_data,
591 struct netlink_ext_ack *extack);
594 qdisc_offload_dump_helper(struct Qdisc *q, enum tc_setup_type type,
597 q->flags &= ~TCQ_F_OFFLOADED;
602 qdisc_offload_graft_helper(struct net_device *dev, struct Qdisc *sch,
603 struct Qdisc *new, struct Qdisc *old,
604 enum tc_setup_type type, void *type_data,
605 struct netlink_ext_ack *extack)
609 struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue,
610 const struct Qdisc_ops *ops,
611 struct netlink_ext_ack *extack);
612 void qdisc_free(struct Qdisc *qdisc);
613 struct Qdisc *qdisc_create_dflt(struct netdev_queue *dev_queue,
614 const struct Qdisc_ops *ops, u32 parentid,
615 struct netlink_ext_ack *extack);
616 void __qdisc_calculate_pkt_len(struct sk_buff *skb,
617 const struct qdisc_size_table *stab);
618 int skb_do_redirect(struct sk_buff *);
620 static inline void skb_reset_tc(struct sk_buff *skb)
622 #ifdef CONFIG_NET_CLS_ACT
623 skb->tc_redirected = 0;
627 static inline bool skb_is_tc_redirected(const struct sk_buff *skb)
629 #ifdef CONFIG_NET_CLS_ACT
630 return skb->tc_redirected;
636 static inline bool skb_at_tc_ingress(const struct sk_buff *skb)
638 #ifdef CONFIG_NET_CLS_ACT
639 return skb->tc_at_ingress;
645 static inline bool skb_skip_tc_classify(struct sk_buff *skb)
647 #ifdef CONFIG_NET_CLS_ACT
648 if (skb->tc_skip_classify) {
649 skb->tc_skip_classify = 0;
656 /* Reset all TX qdiscs greater than index of a device. */
657 static inline void qdisc_reset_all_tx_gt(struct net_device *dev, unsigned int i)
661 for (; i < dev->num_tx_queues; i++) {
662 qdisc = rtnl_dereference(netdev_get_tx_queue(dev, i)->qdisc);
664 spin_lock_bh(qdisc_lock(qdisc));
666 spin_unlock_bh(qdisc_lock(qdisc));
671 static inline void qdisc_reset_all_tx(struct net_device *dev)
673 qdisc_reset_all_tx_gt(dev, 0);
676 /* Are all TX queues of the device empty? */
677 static inline bool qdisc_all_tx_empty(const struct net_device *dev)
682 for (i = 0; i < dev->num_tx_queues; i++) {
683 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
684 const struct Qdisc *q = rcu_dereference(txq->qdisc);
695 /* Are any of the TX qdiscs changing? */
696 static inline bool qdisc_tx_changing(const struct net_device *dev)
700 for (i = 0; i < dev->num_tx_queues; i++) {
701 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
702 if (rcu_access_pointer(txq->qdisc) != txq->qdisc_sleeping)
708 /* Is the device using the noop qdisc on all queues? */
709 static inline bool qdisc_tx_is_noop(const struct net_device *dev)
713 for (i = 0; i < dev->num_tx_queues; i++) {
714 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
715 if (rcu_access_pointer(txq->qdisc) != &noop_qdisc)
721 static inline unsigned int qdisc_pkt_len(const struct sk_buff *skb)
723 return qdisc_skb_cb(skb)->pkt_len;
726 /* additional qdisc xmit flags (NET_XMIT_MASK in linux/netdevice.h) */
727 enum net_xmit_qdisc_t {
728 __NET_XMIT_STOLEN = 0x00010000,
729 __NET_XMIT_BYPASS = 0x00020000,
732 #ifdef CONFIG_NET_CLS_ACT
733 #define net_xmit_drop_count(e) ((e) & __NET_XMIT_STOLEN ? 0 : 1)
735 #define net_xmit_drop_count(e) (1)
738 static inline void qdisc_calculate_pkt_len(struct sk_buff *skb,
739 const struct Qdisc *sch)
741 #ifdef CONFIG_NET_SCHED
742 struct qdisc_size_table *stab = rcu_dereference_bh(sch->stab);
745 __qdisc_calculate_pkt_len(skb, stab);
749 static inline int qdisc_enqueue(struct sk_buff *skb, struct Qdisc *sch,
750 struct sk_buff **to_free)
752 qdisc_calculate_pkt_len(skb, sch);
753 return sch->enqueue(skb, sch, to_free);
756 static inline bool qdisc_is_percpu_stats(const struct Qdisc *q)
758 return q->flags & TCQ_F_CPUSTATS;
761 static inline void _bstats_update(struct gnet_stats_basic_packed *bstats,
762 __u64 bytes, __u32 packets)
764 bstats->bytes += bytes;
765 bstats->packets += packets;
768 static inline void bstats_update(struct gnet_stats_basic_packed *bstats,
769 const struct sk_buff *skb)
771 _bstats_update(bstats,
773 skb_is_gso(skb) ? skb_shinfo(skb)->gso_segs : 1);
776 static inline void _bstats_cpu_update(struct gnet_stats_basic_cpu *bstats,
777 __u64 bytes, __u32 packets)
779 u64_stats_update_begin(&bstats->syncp);
780 _bstats_update(&bstats->bstats, bytes, packets);
781 u64_stats_update_end(&bstats->syncp);
784 static inline void bstats_cpu_update(struct gnet_stats_basic_cpu *bstats,
785 const struct sk_buff *skb)
787 u64_stats_update_begin(&bstats->syncp);
788 bstats_update(&bstats->bstats, skb);
789 u64_stats_update_end(&bstats->syncp);
792 static inline void qdisc_bstats_cpu_update(struct Qdisc *sch,
793 const struct sk_buff *skb)
795 bstats_cpu_update(this_cpu_ptr(sch->cpu_bstats), skb);
798 static inline void qdisc_bstats_update(struct Qdisc *sch,
799 const struct sk_buff *skb)
801 bstats_update(&sch->bstats, skb);
804 static inline void qdisc_qstats_backlog_dec(struct Qdisc *sch,
805 const struct sk_buff *skb)
807 sch->qstats.backlog -= qdisc_pkt_len(skb);
810 static inline void qdisc_qstats_cpu_backlog_dec(struct Qdisc *sch,
811 const struct sk_buff *skb)
813 this_cpu_sub(sch->cpu_qstats->backlog, qdisc_pkt_len(skb));
816 static inline void qdisc_qstats_backlog_inc(struct Qdisc *sch,
817 const struct sk_buff *skb)
819 sch->qstats.backlog += qdisc_pkt_len(skb);
822 static inline void qdisc_qstats_cpu_backlog_inc(struct Qdisc *sch,
823 const struct sk_buff *skb)
825 this_cpu_add(sch->cpu_qstats->backlog, qdisc_pkt_len(skb));
828 static inline void qdisc_qstats_cpu_qlen_inc(struct Qdisc *sch)
830 this_cpu_inc(sch->cpu_qstats->qlen);
833 static inline void qdisc_qstats_cpu_qlen_dec(struct Qdisc *sch)
835 this_cpu_dec(sch->cpu_qstats->qlen);
838 static inline void qdisc_qstats_cpu_requeues_inc(struct Qdisc *sch)
840 this_cpu_inc(sch->cpu_qstats->requeues);
843 static inline void __qdisc_qstats_drop(struct Qdisc *sch, int count)
845 sch->qstats.drops += count;
848 static inline void qstats_drop_inc(struct gnet_stats_queue *qstats)
853 static inline void qstats_overlimit_inc(struct gnet_stats_queue *qstats)
855 qstats->overlimits++;
858 static inline void qdisc_qstats_drop(struct Qdisc *sch)
860 qstats_drop_inc(&sch->qstats);
863 static inline void qdisc_qstats_cpu_drop(struct Qdisc *sch)
865 this_cpu_inc(sch->cpu_qstats->drops);
868 static inline void qdisc_qstats_overlimit(struct Qdisc *sch)
870 sch->qstats.overlimits++;
873 static inline void qdisc_skb_head_init(struct qdisc_skb_head *qh)
880 static inline void __qdisc_enqueue_tail(struct sk_buff *skb,
881 struct qdisc_skb_head *qh)
883 struct sk_buff *last = qh->tail;
896 static inline int qdisc_enqueue_tail(struct sk_buff *skb, struct Qdisc *sch)
898 __qdisc_enqueue_tail(skb, &sch->q);
899 qdisc_qstats_backlog_inc(sch, skb);
900 return NET_XMIT_SUCCESS;
903 static inline void __qdisc_enqueue_head(struct sk_buff *skb,
904 struct qdisc_skb_head *qh)
906 skb->next = qh->head;
914 static inline struct sk_buff *__qdisc_dequeue_head(struct qdisc_skb_head *qh)
916 struct sk_buff *skb = qh->head;
918 if (likely(skb != NULL)) {
919 qh->head = skb->next;
921 if (qh->head == NULL)
929 static inline struct sk_buff *qdisc_dequeue_head(struct Qdisc *sch)
931 struct sk_buff *skb = __qdisc_dequeue_head(&sch->q);
933 if (likely(skb != NULL)) {
934 qdisc_qstats_backlog_dec(sch, skb);
935 qdisc_bstats_update(sch, skb);
941 /* Instead of calling kfree_skb() while root qdisc lock is held,
942 * queue the skb for future freeing at end of __dev_xmit_skb()
944 static inline void __qdisc_drop(struct sk_buff *skb, struct sk_buff **to_free)
946 skb->next = *to_free;
950 static inline void __qdisc_drop_all(struct sk_buff *skb,
951 struct sk_buff **to_free)
954 skb->prev->next = *to_free;
956 skb->next = *to_free;
960 static inline unsigned int __qdisc_queue_drop_head(struct Qdisc *sch,
961 struct qdisc_skb_head *qh,
962 struct sk_buff **to_free)
964 struct sk_buff *skb = __qdisc_dequeue_head(qh);
966 if (likely(skb != NULL)) {
967 unsigned int len = qdisc_pkt_len(skb);
969 qdisc_qstats_backlog_dec(sch, skb);
970 __qdisc_drop(skb, to_free);
977 static inline unsigned int qdisc_queue_drop_head(struct Qdisc *sch,
978 struct sk_buff **to_free)
980 return __qdisc_queue_drop_head(sch, &sch->q, to_free);
983 static inline struct sk_buff *qdisc_peek_head(struct Qdisc *sch)
985 const struct qdisc_skb_head *qh = &sch->q;
990 /* generic pseudo peek method for non-work-conserving qdisc */
991 static inline struct sk_buff *qdisc_peek_dequeued(struct Qdisc *sch)
993 struct sk_buff *skb = skb_peek(&sch->gso_skb);
995 /* we can reuse ->gso_skb because peek isn't called for root qdiscs */
997 skb = sch->dequeue(sch);
1000 __skb_queue_head(&sch->gso_skb, skb);
1001 /* it's still part of the queue */
1002 qdisc_qstats_backlog_inc(sch, skb);
1010 /* use instead of qdisc->dequeue() for all qdiscs queried with ->peek() */
1011 static inline struct sk_buff *qdisc_dequeue_peeked(struct Qdisc *sch)
1013 struct sk_buff *skb = skb_peek(&sch->gso_skb);
1016 skb = __skb_dequeue(&sch->gso_skb);
1017 qdisc_qstats_backlog_dec(sch, skb);
1020 skb = sch->dequeue(sch);
1026 static inline void __qdisc_reset_queue(struct qdisc_skb_head *qh)
1029 * We do not know the backlog in bytes of this list, it
1030 * is up to the caller to correct it
1034 rtnl_kfree_skbs(qh->head, qh->tail);
1042 static inline void qdisc_reset_queue(struct Qdisc *sch)
1044 __qdisc_reset_queue(&sch->q);
1045 sch->qstats.backlog = 0;
1048 static inline struct Qdisc *qdisc_replace(struct Qdisc *sch, struct Qdisc *new,
1049 struct Qdisc **pold)
1057 unsigned int qlen = old->q.qlen;
1058 unsigned int backlog = old->qstats.backlog;
1061 qdisc_tree_reduce_backlog(old, qlen, backlog);
1063 sch_tree_unlock(sch);
1068 static inline void rtnl_qdisc_drop(struct sk_buff *skb, struct Qdisc *sch)
1070 rtnl_kfree_skbs(skb, skb);
1071 qdisc_qstats_drop(sch);
1074 static inline int qdisc_drop_cpu(struct sk_buff *skb, struct Qdisc *sch,
1075 struct sk_buff **to_free)
1077 __qdisc_drop(skb, to_free);
1078 qdisc_qstats_cpu_drop(sch);
1080 return NET_XMIT_DROP;
1083 static inline int qdisc_drop(struct sk_buff *skb, struct Qdisc *sch,
1084 struct sk_buff **to_free)
1086 __qdisc_drop(skb, to_free);
1087 qdisc_qstats_drop(sch);
1089 return NET_XMIT_DROP;
1092 static inline int qdisc_drop_all(struct sk_buff *skb, struct Qdisc *sch,
1093 struct sk_buff **to_free)
1095 __qdisc_drop_all(skb, to_free);
1096 qdisc_qstats_drop(sch);
1098 return NET_XMIT_DROP;
1101 /* Length to Time (L2T) lookup in a qdisc_rate_table, to determine how
1102 long it will take to send a packet given its size.
1104 static inline u32 qdisc_l2t(struct qdisc_rate_table* rtab, unsigned int pktlen)
1106 int slot = pktlen + rtab->rate.cell_align + rtab->rate.overhead;
1109 slot >>= rtab->rate.cell_log;
1111 return rtab->data[255]*(slot >> 8) + rtab->data[slot & 0xFF];
1112 return rtab->data[slot];
1115 struct psched_ratecfg {
1116 u64 rate_bytes_ps; /* bytes per second */
1123 static inline u64 psched_l2t_ns(const struct psched_ratecfg *r,
1128 if (unlikely(r->linklayer == TC_LINKLAYER_ATM))
1129 return ((u64)(DIV_ROUND_UP(len,48)*53) * r->mult) >> r->shift;
1131 return ((u64)len * r->mult) >> r->shift;
1134 void psched_ratecfg_precompute(struct psched_ratecfg *r,
1135 const struct tc_ratespec *conf,
1138 static inline void psched_ratecfg_getrate(struct tc_ratespec *res,
1139 const struct psched_ratecfg *r)
1141 memset(res, 0, sizeof(*res));
1143 /* legacy struct tc_ratespec has a 32bit @rate field
1144 * Qdisc using 64bit rate should add new attributes
1145 * in order to maintain compatibility.
1147 res->rate = min_t(u64, r->rate_bytes_ps, ~0U);
1149 res->overhead = r->overhead;
1150 res->linklayer = (r->linklayer & TC_LINKLAYER_MASK);
1153 /* Mini Qdisc serves for specific needs of ingress/clsact Qdisc.
1154 * The fast path only needs to access filter list and to update stats
1157 struct tcf_proto *filter_list;
1158 struct gnet_stats_basic_cpu __percpu *cpu_bstats;
1159 struct gnet_stats_queue __percpu *cpu_qstats;
1160 struct rcu_head rcu;
1163 static inline void mini_qdisc_bstats_cpu_update(struct mini_Qdisc *miniq,
1164 const struct sk_buff *skb)
1166 bstats_cpu_update(this_cpu_ptr(miniq->cpu_bstats), skb);
1169 static inline void mini_qdisc_qstats_cpu_drop(struct mini_Qdisc *miniq)
1171 this_cpu_inc(miniq->cpu_qstats->drops);
1174 struct mini_Qdisc_pair {
1175 struct mini_Qdisc miniq1;
1176 struct mini_Qdisc miniq2;
1177 struct mini_Qdisc __rcu **p_miniq;
1180 void mini_qdisc_pair_swap(struct mini_Qdisc_pair *miniqp,
1181 struct tcf_proto *tp_head);
1182 void mini_qdisc_pair_init(struct mini_Qdisc_pair *miniqp, struct Qdisc *qdisc,
1183 struct mini_Qdisc __rcu **p_miniq);
1185 static inline void skb_tc_reinsert(struct sk_buff *skb, struct tcf_result *res)
1187 struct gnet_stats_queue *stats = res->qstats;
1191 ret = netif_receive_skb(skb);
1193 ret = dev_queue_xmit(skb);
1195 qstats_overlimit_inc(res->qstats);