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 {
62 int (*enqueue)(struct sk_buff *skb,
64 struct sk_buff **to_free);
65 struct sk_buff * (*dequeue)(struct Qdisc *sch);
67 #define TCQ_F_BUILTIN 1
68 #define TCQ_F_INGRESS 2
69 #define TCQ_F_CAN_BYPASS 4
70 #define TCQ_F_MQROOT 8
71 #define TCQ_F_ONETXQUEUE 0x10 /* dequeue_skb() can assume all skbs are for
72 * q->dev_queue : It can test
73 * netif_xmit_frozen_or_stopped() before
74 * dequeueing next packet.
75 * Its true for MQ/MQPRIO slaves, or non
78 #define TCQ_F_WARN_NONWC (1 << 16)
79 #define TCQ_F_CPUSTATS 0x20 /* run using percpu statistics */
80 #define TCQ_F_NOPARENT 0x40 /* root of its hierarchy :
81 * qdisc_tree_decrease_qlen() should stop.
83 #define TCQ_F_INVISIBLE 0x80 /* invisible by default in dump */
84 #define TCQ_F_NOLOCK 0x100 /* qdisc does not require locking */
85 #define TCQ_F_OFFLOADED 0x200 /* qdisc is offloaded to HW */
87 const struct Qdisc_ops *ops;
88 struct qdisc_size_table __rcu *stab;
89 struct hlist_node hash;
93 struct netdev_queue *dev_queue;
95 struct net_rate_estimator __rcu *rate_est;
96 struct gnet_stats_basic_cpu __percpu *cpu_bstats;
97 struct gnet_stats_queue __percpu *cpu_qstats;
102 * For performance sake on SMP, we put highly modified fields at the end
104 struct sk_buff_head gso_skb ____cacheline_aligned_in_smp;
105 struct qdisc_skb_head q;
106 struct gnet_stats_basic_packed bstats;
108 struct gnet_stats_queue qstats;
110 struct Qdisc *next_sched;
111 struct sk_buff_head skb_bad_txq;
113 spinlock_t busylock ____cacheline_aligned_in_smp;
118 static inline void qdisc_refcount_inc(struct Qdisc *qdisc)
120 if (qdisc->flags & TCQ_F_BUILTIN)
122 refcount_inc(&qdisc->refcnt);
125 /* Intended to be used by unlocked users, when concurrent qdisc release is
129 static inline struct Qdisc *qdisc_refcount_inc_nz(struct Qdisc *qdisc)
131 if (qdisc->flags & TCQ_F_BUILTIN)
133 if (refcount_inc_not_zero(&qdisc->refcnt))
138 static inline bool qdisc_is_running(struct Qdisc *qdisc)
140 if (qdisc->flags & TCQ_F_NOLOCK)
141 return spin_is_locked(&qdisc->seqlock);
142 return (raw_read_seqcount(&qdisc->running) & 1) ? true : false;
145 static inline bool qdisc_run_begin(struct Qdisc *qdisc)
147 if (qdisc->flags & TCQ_F_NOLOCK) {
148 if (!spin_trylock(&qdisc->seqlock))
150 } else if (qdisc_is_running(qdisc)) {
153 /* Variant of write_seqcount_begin() telling lockdep a trylock
156 raw_write_seqcount_begin(&qdisc->running);
157 seqcount_acquire(&qdisc->running.dep_map, 0, 1, _RET_IP_);
161 static inline void qdisc_run_end(struct Qdisc *qdisc)
163 write_seqcount_end(&qdisc->running);
164 if (qdisc->flags & TCQ_F_NOLOCK)
165 spin_unlock(&qdisc->seqlock);
168 static inline bool qdisc_may_bulk(const struct Qdisc *qdisc)
170 return qdisc->flags & TCQ_F_ONETXQUEUE;
173 static inline int qdisc_avail_bulklimit(const struct netdev_queue *txq)
176 /* Non-BQL migrated drivers will return 0, too. */
177 return dql_avail(&txq->dql);
183 struct Qdisc_class_ops {
184 /* Child qdisc manipulation */
185 struct netdev_queue * (*select_queue)(struct Qdisc *, struct tcmsg *);
186 int (*graft)(struct Qdisc *, unsigned long cl,
187 struct Qdisc *, struct Qdisc **,
188 struct netlink_ext_ack *extack);
189 struct Qdisc * (*leaf)(struct Qdisc *, unsigned long cl);
190 void (*qlen_notify)(struct Qdisc *, unsigned long);
192 /* Class manipulation routines */
193 unsigned long (*find)(struct Qdisc *, u32 classid);
194 int (*change)(struct Qdisc *, u32, u32,
195 struct nlattr **, unsigned long *,
196 struct netlink_ext_ack *);
197 int (*delete)(struct Qdisc *, unsigned long);
198 void (*walk)(struct Qdisc *, struct qdisc_walker * arg);
200 /* Filter manipulation */
201 struct tcf_block * (*tcf_block)(struct Qdisc *sch,
203 struct netlink_ext_ack *extack);
204 unsigned long (*bind_tcf)(struct Qdisc *, unsigned long,
206 void (*unbind_tcf)(struct Qdisc *, unsigned long);
208 /* rtnetlink specific */
209 int (*dump)(struct Qdisc *, unsigned long,
210 struct sk_buff *skb, struct tcmsg*);
211 int (*dump_stats)(struct Qdisc *, unsigned long,
216 struct Qdisc_ops *next;
217 const struct Qdisc_class_ops *cl_ops;
220 unsigned int static_flags;
222 int (*enqueue)(struct sk_buff *skb,
224 struct sk_buff **to_free);
225 struct sk_buff * (*dequeue)(struct Qdisc *);
226 struct sk_buff * (*peek)(struct Qdisc *);
228 int (*init)(struct Qdisc *sch, struct nlattr *arg,
229 struct netlink_ext_ack *extack);
230 void (*reset)(struct Qdisc *);
231 void (*destroy)(struct Qdisc *);
232 int (*change)(struct Qdisc *sch,
234 struct netlink_ext_ack *extack);
235 void (*attach)(struct Qdisc *sch);
236 int (*change_tx_queue_len)(struct Qdisc *, unsigned int);
238 int (*dump)(struct Qdisc *, struct sk_buff *);
239 int (*dump_stats)(struct Qdisc *, struct gnet_dump *);
241 void (*ingress_block_set)(struct Qdisc *sch,
243 void (*egress_block_set)(struct Qdisc *sch,
245 u32 (*ingress_block_get)(struct Qdisc *sch);
246 u32 (*egress_block_get)(struct Qdisc *sch);
248 struct module *owner;
258 const struct tcf_proto *goto_tp;
260 /* used by the TC_ACT_REINSERT action */
263 struct gnet_stats_queue *qstats;
270 struct tcf_proto_ops {
271 struct list_head head;
274 int (*classify)(struct sk_buff *,
275 const struct tcf_proto *,
276 struct tcf_result *);
277 int (*init)(struct tcf_proto*);
278 void (*destroy)(struct tcf_proto *tp,
279 struct netlink_ext_ack *extack);
281 void* (*get)(struct tcf_proto*, u32 handle);
282 int (*change)(struct net *net, struct sk_buff *,
283 struct tcf_proto*, unsigned long,
284 u32 handle, struct nlattr **,
286 struct netlink_ext_ack *);
287 int (*delete)(struct tcf_proto *tp, void *arg,
289 struct netlink_ext_ack *);
290 void (*walk)(struct tcf_proto*, struct tcf_walker *arg);
291 int (*reoffload)(struct tcf_proto *tp, bool add,
292 tc_setup_cb_t *cb, void *cb_priv,
293 struct netlink_ext_ack *extack);
294 void (*bind_class)(void *, u32, unsigned long);
295 void * (*tmplt_create)(struct net *net,
296 struct tcf_chain *chain,
298 struct netlink_ext_ack *extack);
299 void (*tmplt_destroy)(void *tmplt_priv);
301 /* rtnetlink specific */
302 int (*dump)(struct net*, struct tcf_proto*, void *,
303 struct sk_buff *skb, struct tcmsg*);
304 int (*tmplt_dump)(struct sk_buff *skb,
308 struct module *owner;
312 /* Fast access part */
313 struct tcf_proto __rcu *next;
316 /* called under RCU BH lock*/
317 int (*classify)(struct sk_buff *,
318 const struct tcf_proto *,
319 struct tcf_result *);
325 const struct tcf_proto_ops *ops;
326 struct tcf_chain *chain;
330 struct qdisc_skb_cb {
333 unsigned int pkt_len;
334 u16 slave_dev_queue_mapping;
337 struct bpf_flow_keys *flow_keys;
339 #define QDISC_CB_PRIV_LEN 20
340 unsigned char data[QDISC_CB_PRIV_LEN];
343 typedef void tcf_chain_head_change_t(struct tcf_proto *tp_head, void *priv);
346 struct tcf_proto __rcu *filter_chain;
347 struct list_head list;
348 struct tcf_block *block;
349 u32 index; /* chain index */
351 unsigned int action_refcnt;
352 bool explicitly_created;
353 const struct tcf_proto_ops *tmplt_ops;
358 struct list_head chain_list;
359 u32 index; /* block index for shared blocks */
363 struct list_head cb_list;
364 struct list_head owner_list;
366 unsigned int offloadcnt; /* Number of oddloaded filters */
367 unsigned int nooffloaddevcnt; /* Number of devs unable to do offload */
369 struct tcf_chain *chain;
370 struct list_head filter_chain_list;
375 static inline void tcf_block_offload_inc(struct tcf_block *block, u32 *flags)
377 if (*flags & TCA_CLS_FLAGS_IN_HW)
379 *flags |= TCA_CLS_FLAGS_IN_HW;
383 static inline void tcf_block_offload_dec(struct tcf_block *block, u32 *flags)
385 if (!(*flags & TCA_CLS_FLAGS_IN_HW))
387 *flags &= ~TCA_CLS_FLAGS_IN_HW;
392 tc_cls_offload_cnt_update(struct tcf_block *block, u32 *cnt,
393 u32 *flags, bool add)
397 tcf_block_offload_inc(block, flags);
402 tcf_block_offload_dec(block, flags);
406 static inline void qdisc_cb_private_validate(const struct sk_buff *skb, int sz)
408 struct qdisc_skb_cb *qcb;
410 BUILD_BUG_ON(sizeof(skb->cb) < offsetof(struct qdisc_skb_cb, data) + sz);
411 BUILD_BUG_ON(sizeof(qcb->data) < sz);
414 static inline int qdisc_qlen(const struct Qdisc *q)
419 static inline u32 qdisc_qlen_sum(const struct Qdisc *q)
421 u32 qlen = q->qstats.qlen;
423 if (q->flags & TCQ_F_NOLOCK)
424 qlen += atomic_read(&q->q.atomic_qlen);
431 static inline struct qdisc_skb_cb *qdisc_skb_cb(const struct sk_buff *skb)
433 return (struct qdisc_skb_cb *)skb->cb;
436 static inline spinlock_t *qdisc_lock(struct Qdisc *qdisc)
438 return &qdisc->q.lock;
441 static inline struct Qdisc *qdisc_root(const struct Qdisc *qdisc)
443 struct Qdisc *q = rcu_dereference_rtnl(qdisc->dev_queue->qdisc);
448 static inline struct Qdisc *qdisc_root_sleeping(const struct Qdisc *qdisc)
450 return qdisc->dev_queue->qdisc_sleeping;
453 /* The qdisc root lock is a mechanism by which to top level
454 * of a qdisc tree can be locked from any qdisc node in the
455 * forest. This allows changing the configuration of some
456 * aspect of the qdisc tree while blocking out asynchronous
457 * qdisc access in the packet processing paths.
459 * It is only legal to do this when the root will not change
460 * on us. Otherwise we'll potentially lock the wrong qdisc
461 * root. This is enforced by holding the RTNL semaphore, which
462 * all users of this lock accessor must do.
464 static inline spinlock_t *qdisc_root_lock(const struct Qdisc *qdisc)
466 struct Qdisc *root = qdisc_root(qdisc);
469 return qdisc_lock(root);
472 static inline spinlock_t *qdisc_root_sleeping_lock(const struct Qdisc *qdisc)
474 struct Qdisc *root = qdisc_root_sleeping(qdisc);
477 return qdisc_lock(root);
480 static inline seqcount_t *qdisc_root_sleeping_running(const struct Qdisc *qdisc)
482 struct Qdisc *root = qdisc_root_sleeping(qdisc);
485 return &root->running;
488 static inline struct net_device *qdisc_dev(const struct Qdisc *qdisc)
490 return qdisc->dev_queue->dev;
493 static inline void sch_tree_lock(const struct Qdisc *q)
495 spin_lock_bh(qdisc_root_sleeping_lock(q));
498 static inline void sch_tree_unlock(const struct Qdisc *q)
500 spin_unlock_bh(qdisc_root_sleeping_lock(q));
503 extern struct Qdisc noop_qdisc;
504 extern struct Qdisc_ops noop_qdisc_ops;
505 extern struct Qdisc_ops pfifo_fast_ops;
506 extern struct Qdisc_ops mq_qdisc_ops;
507 extern struct Qdisc_ops noqueue_qdisc_ops;
508 extern const struct Qdisc_ops *default_qdisc_ops;
509 static inline const struct Qdisc_ops *
510 get_default_qdisc_ops(const struct net_device *dev, int ntx)
512 return ntx < dev->real_num_tx_queues ?
513 default_qdisc_ops : &pfifo_fast_ops;
516 struct Qdisc_class_common {
518 struct hlist_node hnode;
521 struct Qdisc_class_hash {
522 struct hlist_head *hash;
523 unsigned int hashsize;
524 unsigned int hashmask;
525 unsigned int hashelems;
528 static inline unsigned int qdisc_class_hash(u32 id, u32 mask)
535 static inline struct Qdisc_class_common *
536 qdisc_class_find(const struct Qdisc_class_hash *hash, u32 id)
538 struct Qdisc_class_common *cl;
544 h = qdisc_class_hash(id, hash->hashmask);
545 hlist_for_each_entry(cl, &hash->hash[h], hnode) {
546 if (cl->classid == id)
552 static inline int tc_classid_to_hwtc(struct net_device *dev, u32 classid)
554 u32 hwtc = TC_H_MIN(classid) - TC_H_MIN_PRIORITY;
556 return (hwtc < netdev_get_num_tc(dev)) ? hwtc : -EINVAL;
559 int qdisc_class_hash_init(struct Qdisc_class_hash *);
560 void qdisc_class_hash_insert(struct Qdisc_class_hash *,
561 struct Qdisc_class_common *);
562 void qdisc_class_hash_remove(struct Qdisc_class_hash *,
563 struct Qdisc_class_common *);
564 void qdisc_class_hash_grow(struct Qdisc *, struct Qdisc_class_hash *);
565 void qdisc_class_hash_destroy(struct Qdisc_class_hash *);
567 int dev_qdisc_change_tx_queue_len(struct net_device *dev);
568 void dev_init_scheduler(struct net_device *dev);
569 void dev_shutdown(struct net_device *dev);
570 void dev_activate(struct net_device *dev);
571 void dev_deactivate(struct net_device *dev);
572 void dev_deactivate_many(struct list_head *head);
573 struct Qdisc *dev_graft_qdisc(struct netdev_queue *dev_queue,
574 struct Qdisc *qdisc);
575 void qdisc_reset(struct Qdisc *qdisc);
576 void qdisc_put(struct Qdisc *qdisc);
577 void qdisc_put_unlocked(struct Qdisc *qdisc);
578 void qdisc_tree_reduce_backlog(struct Qdisc *qdisc, unsigned int n,
580 #ifdef CONFIG_NET_SCHED
581 int qdisc_offload_dump_helper(struct Qdisc *q, enum tc_setup_type type,
583 void qdisc_offload_graft_helper(struct net_device *dev, struct Qdisc *sch,
584 struct Qdisc *new, struct Qdisc *old,
585 enum tc_setup_type type, void *type_data,
586 struct netlink_ext_ack *extack);
589 qdisc_offload_dump_helper(struct Qdisc *q, enum tc_setup_type type,
592 q->flags &= ~TCQ_F_OFFLOADED;
597 qdisc_offload_graft_helper(struct net_device *dev, struct Qdisc *sch,
598 struct Qdisc *new, struct Qdisc *old,
599 enum tc_setup_type type, void *type_data,
600 struct netlink_ext_ack *extack)
604 struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue,
605 const struct Qdisc_ops *ops,
606 struct netlink_ext_ack *extack);
607 void qdisc_free(struct Qdisc *qdisc);
608 struct Qdisc *qdisc_create_dflt(struct netdev_queue *dev_queue,
609 const struct Qdisc_ops *ops, u32 parentid,
610 struct netlink_ext_ack *extack);
611 void __qdisc_calculate_pkt_len(struct sk_buff *skb,
612 const struct qdisc_size_table *stab);
613 int skb_do_redirect(struct sk_buff *);
615 static inline void skb_reset_tc(struct sk_buff *skb)
617 #ifdef CONFIG_NET_CLS_ACT
618 skb->tc_redirected = 0;
622 static inline bool skb_is_tc_redirected(const struct sk_buff *skb)
624 #ifdef CONFIG_NET_CLS_ACT
625 return skb->tc_redirected;
631 static inline bool skb_at_tc_ingress(const struct sk_buff *skb)
633 #ifdef CONFIG_NET_CLS_ACT
634 return skb->tc_at_ingress;
640 static inline bool skb_skip_tc_classify(struct sk_buff *skb)
642 #ifdef CONFIG_NET_CLS_ACT
643 if (skb->tc_skip_classify) {
644 skb->tc_skip_classify = 0;
651 /* Reset all TX qdiscs greater than index of a device. */
652 static inline void qdisc_reset_all_tx_gt(struct net_device *dev, unsigned int i)
656 for (; i < dev->num_tx_queues; i++) {
657 qdisc = rtnl_dereference(netdev_get_tx_queue(dev, i)->qdisc);
659 spin_lock_bh(qdisc_lock(qdisc));
661 spin_unlock_bh(qdisc_lock(qdisc));
666 static inline void qdisc_reset_all_tx(struct net_device *dev)
668 qdisc_reset_all_tx_gt(dev, 0);
671 /* Are all TX queues of the device empty? */
672 static inline bool qdisc_all_tx_empty(const struct net_device *dev)
677 for (i = 0; i < dev->num_tx_queues; i++) {
678 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
679 const struct Qdisc *q = rcu_dereference(txq->qdisc);
690 /* Are any of the TX qdiscs changing? */
691 static inline bool qdisc_tx_changing(const struct net_device *dev)
695 for (i = 0; i < dev->num_tx_queues; i++) {
696 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
697 if (rcu_access_pointer(txq->qdisc) != txq->qdisc_sleeping)
703 /* Is the device using the noop qdisc on all queues? */
704 static inline bool qdisc_tx_is_noop(const struct net_device *dev)
708 for (i = 0; i < dev->num_tx_queues; i++) {
709 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
710 if (rcu_access_pointer(txq->qdisc) != &noop_qdisc)
716 static inline unsigned int qdisc_pkt_len(const struct sk_buff *skb)
718 return qdisc_skb_cb(skb)->pkt_len;
721 /* additional qdisc xmit flags (NET_XMIT_MASK in linux/netdevice.h) */
722 enum net_xmit_qdisc_t {
723 __NET_XMIT_STOLEN = 0x00010000,
724 __NET_XMIT_BYPASS = 0x00020000,
727 #ifdef CONFIG_NET_CLS_ACT
728 #define net_xmit_drop_count(e) ((e) & __NET_XMIT_STOLEN ? 0 : 1)
730 #define net_xmit_drop_count(e) (1)
733 static inline void qdisc_calculate_pkt_len(struct sk_buff *skb,
734 const struct Qdisc *sch)
736 #ifdef CONFIG_NET_SCHED
737 struct qdisc_size_table *stab = rcu_dereference_bh(sch->stab);
740 __qdisc_calculate_pkt_len(skb, stab);
744 static inline int qdisc_enqueue(struct sk_buff *skb, struct Qdisc *sch,
745 struct sk_buff **to_free)
747 qdisc_calculate_pkt_len(skb, sch);
748 return sch->enqueue(skb, sch, to_free);
751 static inline bool qdisc_is_percpu_stats(const struct Qdisc *q)
753 return q->flags & TCQ_F_CPUSTATS;
756 static inline void _bstats_update(struct gnet_stats_basic_packed *bstats,
757 __u64 bytes, __u32 packets)
759 bstats->bytes += bytes;
760 bstats->packets += packets;
763 static inline void bstats_update(struct gnet_stats_basic_packed *bstats,
764 const struct sk_buff *skb)
766 _bstats_update(bstats,
768 skb_is_gso(skb) ? skb_shinfo(skb)->gso_segs : 1);
771 static inline void _bstats_cpu_update(struct gnet_stats_basic_cpu *bstats,
772 __u64 bytes, __u32 packets)
774 u64_stats_update_begin(&bstats->syncp);
775 _bstats_update(&bstats->bstats, bytes, packets);
776 u64_stats_update_end(&bstats->syncp);
779 static inline void bstats_cpu_update(struct gnet_stats_basic_cpu *bstats,
780 const struct sk_buff *skb)
782 u64_stats_update_begin(&bstats->syncp);
783 bstats_update(&bstats->bstats, skb);
784 u64_stats_update_end(&bstats->syncp);
787 static inline void qdisc_bstats_cpu_update(struct Qdisc *sch,
788 const struct sk_buff *skb)
790 bstats_cpu_update(this_cpu_ptr(sch->cpu_bstats), skb);
793 static inline void qdisc_bstats_update(struct Qdisc *sch,
794 const struct sk_buff *skb)
796 bstats_update(&sch->bstats, skb);
799 static inline void qdisc_qstats_backlog_dec(struct Qdisc *sch,
800 const struct sk_buff *skb)
802 sch->qstats.backlog -= qdisc_pkt_len(skb);
805 static inline void qdisc_qstats_cpu_backlog_dec(struct Qdisc *sch,
806 const struct sk_buff *skb)
808 this_cpu_sub(sch->cpu_qstats->backlog, qdisc_pkt_len(skb));
811 static inline void qdisc_qstats_backlog_inc(struct Qdisc *sch,
812 const struct sk_buff *skb)
814 sch->qstats.backlog += qdisc_pkt_len(skb);
817 static inline void qdisc_qstats_cpu_backlog_inc(struct Qdisc *sch,
818 const struct sk_buff *skb)
820 this_cpu_add(sch->cpu_qstats->backlog, qdisc_pkt_len(skb));
823 static inline void qdisc_qstats_atomic_qlen_inc(struct Qdisc *sch)
825 atomic_inc(&sch->q.atomic_qlen);
828 static inline void qdisc_qstats_atomic_qlen_dec(struct Qdisc *sch)
830 atomic_dec(&sch->q.atomic_qlen);
833 static inline void qdisc_qstats_cpu_requeues_inc(struct Qdisc *sch)
835 this_cpu_inc(sch->cpu_qstats->requeues);
838 static inline void __qdisc_qstats_drop(struct Qdisc *sch, int count)
840 sch->qstats.drops += count;
843 static inline void qstats_drop_inc(struct gnet_stats_queue *qstats)
848 static inline void qstats_overlimit_inc(struct gnet_stats_queue *qstats)
850 qstats->overlimits++;
853 static inline void qdisc_qstats_drop(struct Qdisc *sch)
855 qstats_drop_inc(&sch->qstats);
858 static inline void qdisc_qstats_cpu_drop(struct Qdisc *sch)
860 this_cpu_inc(sch->cpu_qstats->drops);
863 static inline void qdisc_qstats_overlimit(struct Qdisc *sch)
865 sch->qstats.overlimits++;
868 static inline void qdisc_skb_head_init(struct qdisc_skb_head *qh)
875 static inline void __qdisc_enqueue_tail(struct sk_buff *skb,
876 struct qdisc_skb_head *qh)
878 struct sk_buff *last = qh->tail;
891 static inline int qdisc_enqueue_tail(struct sk_buff *skb, struct Qdisc *sch)
893 __qdisc_enqueue_tail(skb, &sch->q);
894 qdisc_qstats_backlog_inc(sch, skb);
895 return NET_XMIT_SUCCESS;
898 static inline void __qdisc_enqueue_head(struct sk_buff *skb,
899 struct qdisc_skb_head *qh)
901 skb->next = qh->head;
909 static inline struct sk_buff *__qdisc_dequeue_head(struct qdisc_skb_head *qh)
911 struct sk_buff *skb = qh->head;
913 if (likely(skb != NULL)) {
914 qh->head = skb->next;
916 if (qh->head == NULL)
924 static inline struct sk_buff *qdisc_dequeue_head(struct Qdisc *sch)
926 struct sk_buff *skb = __qdisc_dequeue_head(&sch->q);
928 if (likely(skb != NULL)) {
929 qdisc_qstats_backlog_dec(sch, skb);
930 qdisc_bstats_update(sch, skb);
936 /* Instead of calling kfree_skb() while root qdisc lock is held,
937 * queue the skb for future freeing at end of __dev_xmit_skb()
939 static inline void __qdisc_drop(struct sk_buff *skb, struct sk_buff **to_free)
941 skb->next = *to_free;
945 static inline void __qdisc_drop_all(struct sk_buff *skb,
946 struct sk_buff **to_free)
949 skb->prev->next = *to_free;
951 skb->next = *to_free;
955 static inline unsigned int __qdisc_queue_drop_head(struct Qdisc *sch,
956 struct qdisc_skb_head *qh,
957 struct sk_buff **to_free)
959 struct sk_buff *skb = __qdisc_dequeue_head(qh);
961 if (likely(skb != NULL)) {
962 unsigned int len = qdisc_pkt_len(skb);
964 qdisc_qstats_backlog_dec(sch, skb);
965 __qdisc_drop(skb, to_free);
972 static inline unsigned int qdisc_queue_drop_head(struct Qdisc *sch,
973 struct sk_buff **to_free)
975 return __qdisc_queue_drop_head(sch, &sch->q, to_free);
978 static inline struct sk_buff *qdisc_peek_head(struct Qdisc *sch)
980 const struct qdisc_skb_head *qh = &sch->q;
985 /* generic pseudo peek method for non-work-conserving qdisc */
986 static inline struct sk_buff *qdisc_peek_dequeued(struct Qdisc *sch)
988 struct sk_buff *skb = skb_peek(&sch->gso_skb);
990 /* we can reuse ->gso_skb because peek isn't called for root qdiscs */
992 skb = sch->dequeue(sch);
995 __skb_queue_head(&sch->gso_skb, skb);
996 /* it's still part of the queue */
997 qdisc_qstats_backlog_inc(sch, skb);
1005 /* use instead of qdisc->dequeue() for all qdiscs queried with ->peek() */
1006 static inline struct sk_buff *qdisc_dequeue_peeked(struct Qdisc *sch)
1008 struct sk_buff *skb = skb_peek(&sch->gso_skb);
1011 skb = __skb_dequeue(&sch->gso_skb);
1012 qdisc_qstats_backlog_dec(sch, skb);
1015 skb = sch->dequeue(sch);
1021 static inline void __qdisc_reset_queue(struct qdisc_skb_head *qh)
1024 * We do not know the backlog in bytes of this list, it
1025 * is up to the caller to correct it
1029 rtnl_kfree_skbs(qh->head, qh->tail);
1037 static inline void qdisc_reset_queue(struct Qdisc *sch)
1039 __qdisc_reset_queue(&sch->q);
1040 sch->qstats.backlog = 0;
1043 static inline struct Qdisc *qdisc_replace(struct Qdisc *sch, struct Qdisc *new,
1044 struct Qdisc **pold)
1052 unsigned int qlen = old->q.qlen;
1053 unsigned int backlog = old->qstats.backlog;
1056 qdisc_tree_reduce_backlog(old, qlen, backlog);
1058 sch_tree_unlock(sch);
1063 static inline void rtnl_qdisc_drop(struct sk_buff *skb, struct Qdisc *sch)
1065 rtnl_kfree_skbs(skb, skb);
1066 qdisc_qstats_drop(sch);
1069 static inline int qdisc_drop_cpu(struct sk_buff *skb, struct Qdisc *sch,
1070 struct sk_buff **to_free)
1072 __qdisc_drop(skb, to_free);
1073 qdisc_qstats_cpu_drop(sch);
1075 return NET_XMIT_DROP;
1078 static inline int qdisc_drop(struct sk_buff *skb, struct Qdisc *sch,
1079 struct sk_buff **to_free)
1081 __qdisc_drop(skb, to_free);
1082 qdisc_qstats_drop(sch);
1084 return NET_XMIT_DROP;
1087 static inline int qdisc_drop_all(struct sk_buff *skb, struct Qdisc *sch,
1088 struct sk_buff **to_free)
1090 __qdisc_drop_all(skb, to_free);
1091 qdisc_qstats_drop(sch);
1093 return NET_XMIT_DROP;
1096 /* Length to Time (L2T) lookup in a qdisc_rate_table, to determine how
1097 long it will take to send a packet given its size.
1099 static inline u32 qdisc_l2t(struct qdisc_rate_table* rtab, unsigned int pktlen)
1101 int slot = pktlen + rtab->rate.cell_align + rtab->rate.overhead;
1104 slot >>= rtab->rate.cell_log;
1106 return rtab->data[255]*(slot >> 8) + rtab->data[slot & 0xFF];
1107 return rtab->data[slot];
1110 struct psched_ratecfg {
1111 u64 rate_bytes_ps; /* bytes per second */
1118 static inline u64 psched_l2t_ns(const struct psched_ratecfg *r,
1123 if (unlikely(r->linklayer == TC_LINKLAYER_ATM))
1124 return ((u64)(DIV_ROUND_UP(len,48)*53) * r->mult) >> r->shift;
1126 return ((u64)len * r->mult) >> r->shift;
1129 void psched_ratecfg_precompute(struct psched_ratecfg *r,
1130 const struct tc_ratespec *conf,
1133 static inline void psched_ratecfg_getrate(struct tc_ratespec *res,
1134 const struct psched_ratecfg *r)
1136 memset(res, 0, sizeof(*res));
1138 /* legacy struct tc_ratespec has a 32bit @rate field
1139 * Qdisc using 64bit rate should add new attributes
1140 * in order to maintain compatibility.
1142 res->rate = min_t(u64, r->rate_bytes_ps, ~0U);
1144 res->overhead = r->overhead;
1145 res->linklayer = (r->linklayer & TC_LINKLAYER_MASK);
1148 /* Mini Qdisc serves for specific needs of ingress/clsact Qdisc.
1149 * The fast path only needs to access filter list and to update stats
1152 struct tcf_proto *filter_list;
1153 struct gnet_stats_basic_cpu __percpu *cpu_bstats;
1154 struct gnet_stats_queue __percpu *cpu_qstats;
1155 struct rcu_head rcu;
1158 static inline void mini_qdisc_bstats_cpu_update(struct mini_Qdisc *miniq,
1159 const struct sk_buff *skb)
1161 bstats_cpu_update(this_cpu_ptr(miniq->cpu_bstats), skb);
1164 static inline void mini_qdisc_qstats_cpu_drop(struct mini_Qdisc *miniq)
1166 this_cpu_inc(miniq->cpu_qstats->drops);
1169 struct mini_Qdisc_pair {
1170 struct mini_Qdisc miniq1;
1171 struct mini_Qdisc miniq2;
1172 struct mini_Qdisc __rcu **p_miniq;
1175 void mini_qdisc_pair_swap(struct mini_Qdisc_pair *miniqp,
1176 struct tcf_proto *tp_head);
1177 void mini_qdisc_pair_init(struct mini_Qdisc_pair *miniqp, struct Qdisc *qdisc,
1178 struct mini_Qdisc __rcu **p_miniq);
1180 static inline void skb_tc_reinsert(struct sk_buff *skb, struct tcf_result *res)
1182 struct gnet_stats_queue *stats = res->qstats;
1186 ret = netif_receive_skb(skb);
1188 ret = dev_queue_xmit(skb);
1190 qstats_overlimit_inc(res->qstats);