blob: 472843eedbaee80c47673e13c8580f197c00b801 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002#ifndef __NET_SCHED_GENERIC_H
3#define __NET_SCHED_GENERIC_H
4
Linus Torvalds1da177e2005-04-16 15:20:36 -07005#include <linux/netdevice.h>
6#include <linux/types.h>
7#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/pkt_sched.h>
9#include <linux/pkt_cls.h>
John Fastabend22e0f8b2014-09-28 11:52:56 -070010#include <linux/percpu.h>
Jesper Dangaard Brouer5772e9a2014-10-01 22:35:59 +020011#include <linux/dynamic_queue_limits.h>
Jiri Pirko5bc17012017-05-17 11:08:01 +020012#include <linux/list.h>
Reshetova, Elena7b936402017-07-04 15:53:07 +030013#include <linux/refcount.h>
Cong Wang7aa00452017-10-26 18:24:28 -070014#include <linux/workqueue.h>
Vlad Buslovc266f642019-02-11 10:55:32 +020015#include <linux/mutex.h>
Vlad Buslov4f8116c2019-08-26 16:44:57 +030016#include <linux/rwsem.h>
Vlad Buslov97394be2019-08-26 16:44:58 +030017#include <linux/atomic.h>
John Hurley59eb87c2019-11-02 14:17:47 +000018#include <linux/hashtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <net/gen_stats.h>
Thomas Grafbe577dd2007-03-22 11:55:50 -070020#include <net/rtnetlink.h>
Pablo Neira Ayusoa7323312019-07-19 18:20:15 +020021#include <net/flow_offload.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23struct Qdisc_ops;
24struct qdisc_walker;
25struct tcf_walker;
26struct module;
Petar Penkovd58e4682018-09-14 07:46:18 -070027struct bpf_flow_keys;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Eric Dumazetfd2c3ef2009-11-03 03:26:03 +000029struct qdisc_rate_table {
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 struct tc_ratespec rate;
31 u32 data[256];
32 struct qdisc_rate_table *next;
33 int refcnt;
34};
35
Eric Dumazetfd2c3ef2009-11-03 03:26:03 +000036enum qdisc_state_t {
David S. Miller37437bb2008-07-16 02:15:04 -070037 __QDISC_STATE_SCHED,
David S. Millera9312ae2008-08-17 21:51:03 -070038 __QDISC_STATE_DEACTIVATED,
Yunsheng Lina90c57f2021-05-14 11:16:59 +080039 __QDISC_STATE_MISSED,
Yunsheng Linc4fef012021-06-22 14:49:56 +080040 __QDISC_STATE_DRAINING,
Eric Dumazet97604c62021-10-18 17:34:02 -070041};
42
43enum qdisc_state2_t {
Ahmed S. Darwish29cbcd82021-10-16 10:49:10 +020044 /* Only for !TCQ_F_NOLOCK qdisc. Never access it directly.
45 * Use qdisc_run_begin/end() or qdisc_is_running() instead.
46 */
Eric Dumazet97604c62021-10-18 17:34:02 -070047 __QDISC_STATE2_RUNNING,
David S. Millere2627c82008-07-16 00:56:32 -070048};
49
Yunsheng Linc4fef012021-06-22 14:49:56 +080050#define QDISC_STATE_MISSED BIT(__QDISC_STATE_MISSED)
51#define QDISC_STATE_DRAINING BIT(__QDISC_STATE_DRAINING)
52
53#define QDISC_STATE_NON_EMPTY (QDISC_STATE_MISSED | \
54 QDISC_STATE_DRAINING)
55
Jussi Kivilinna175f9c12008-07-20 00:08:47 -070056struct qdisc_size_table {
Eric Dumazeta2da5702011-01-20 03:48:19 +000057 struct rcu_head rcu;
Jussi Kivilinna175f9c12008-07-20 00:08:47 -070058 struct list_head list;
59 struct tc_sizespec szopts;
60 int refcnt;
61 u16 data[];
62};
63
Florian Westphal48da34b2016-09-18 00:57:34 +020064/* similar to sk_buff_head, but skb->prev pointer is undefined. */
65struct qdisc_skb_head {
66 struct sk_buff *head;
67 struct sk_buff *tail;
Paolo Abeni73eb6282019-04-10 14:32:41 +020068 __u32 qlen;
Florian Westphal48da34b2016-09-18 00:57:34 +020069 spinlock_t lock;
70};
71
Eric Dumazetfd2c3ef2009-11-03 03:26:03 +000072struct Qdisc {
Eric Dumazet520ac302016-06-21 23:16:49 -070073 int (*enqueue)(struct sk_buff *skb,
74 struct Qdisc *sch,
75 struct sk_buff **to_free);
76 struct sk_buff * (*dequeue)(struct Qdisc *sch);
Eric Dumazet05bdd2f2011-10-20 17:45:43 -040077 unsigned int flags;
Jarek Poplawskib00355d2009-02-01 01:12:42 -080078#define TCQ_F_BUILTIN 1
Eric Dumazetfd245a42011-01-20 05:27:16 +000079#define TCQ_F_INGRESS 2
80#define TCQ_F_CAN_BYPASS 4
81#define TCQ_F_MQROOT 8
Eric Dumazet1abbe132012-12-11 15:54:33 +000082#define TCQ_F_ONETXQUEUE 0x10 /* dequeue_skb() can assume all skbs are for
83 * q->dev_queue : It can test
84 * netif_xmit_frozen_or_stopped() before
85 * dequeueing next packet.
86 * Its true for MQ/MQPRIO slaves, or non
87 * multiqueue device.
88 */
Jarek Poplawskib00355d2009-02-01 01:12:42 -080089#define TCQ_F_WARN_NONWC (1 << 16)
John Fastabend22e0f8b2014-09-28 11:52:56 -070090#define TCQ_F_CPUSTATS 0x20 /* run using percpu statistics */
Eric Dumazet4eaf3b82015-12-01 20:08:51 -080091#define TCQ_F_NOPARENT 0x40 /* root of its hierarchy :
92 * qdisc_tree_decrease_qlen() should stop.
93 */
Jiri Kosina49b49972017-03-08 16:03:32 +010094#define TCQ_F_INVISIBLE 0x80 /* invisible by default in dump */
John Fastabend6b3ba912017-12-07 09:54:25 -080095#define TCQ_F_NOLOCK 0x100 /* qdisc does not require locking */
Yuval Mintz7a4fa292017-12-14 15:54:29 +020096#define TCQ_F_OFFLOADED 0x200 /* qdisc is offloaded to HW */
Eric Dumazet45203a32013-06-06 08:43:22 -070097 u32 limit;
Eric Dumazet05bdd2f2011-10-20 17:45:43 -040098 const struct Qdisc_ops *ops;
Eric Dumazeta2da5702011-01-20 03:48:19 +000099 struct qdisc_size_table __rcu *stab;
Jiri Kosina59cc1f62016-08-10 11:05:15 +0200100 struct hlist_node hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 u32 handle;
102 u32 parent;
David S. Miller72b25a92008-07-18 20:54:17 -0700103
Eric Dumazet5e140df2009-03-20 01:33:32 -0700104 struct netdev_queue *dev_queue;
Eric Dumazet5e140df2009-03-20 01:33:32 -0700105
Eric Dumazet1c0d32f2016-12-04 09:48:16 -0800106 struct net_rate_estimator __rcu *rate_est;
Ahmed S. Darwish50dc9a82021-10-16 10:49:09 +0200107 struct gnet_stats_basic_sync __percpu *cpu_bstats;
Eric Dumazet0d32ef82015-01-29 17:30:12 -0800108 struct gnet_stats_queue __percpu *cpu_qstats;
Eric Dumazet846e4632020-10-07 09:51:11 -0700109 int pad;
Paolo Abenie9be0e92018-05-25 16:28:44 +0200110 refcount_t refcnt;
Eric Dumazet0d32ef82015-01-29 17:30:12 -0800111
Eric Dumazet5e140df2009-03-20 01:33:32 -0700112 /*
113 * For performance sake on SMP, we put highly modified fields at the end
114 */
John Fastabenda53851e2017-12-07 09:55:45 -0800115 struct sk_buff_head gso_skb ____cacheline_aligned_in_smp;
Florian Westphal48da34b2016-09-18 00:57:34 +0200116 struct qdisc_skb_head q;
Ahmed S. Darwish50dc9a82021-10-16 10:49:09 +0200117 struct gnet_stats_basic_sync bstats;
Eric Dumazet0d32ef82015-01-29 17:30:12 -0800118 struct gnet_stats_queue qstats;
Eric Dumazet4d202a02016-06-21 23:16:52 -0700119 unsigned long state;
Eric Dumazet97604c62021-10-18 17:34:02 -0700120 unsigned long state2; /* must be written under qdisc spinlock */
Eric Dumazet4d202a02016-06-21 23:16:52 -0700121 struct Qdisc *next_sched;
John Fastabend70e57d52017-12-07 09:56:23 -0800122 struct sk_buff_head skb_bad_txq;
Eric Dumazet45203a32013-06-06 08:43:22 -0700123
124 spinlock_t busylock ____cacheline_aligned_in_smp;
Paolo Abeni96009c72018-05-15 16:24:36 +0200125 spinlock_t seqlock;
Paolo Abeni28cff532019-03-22 16:01:55 +0100126
Vlad Buslov3a7d0d02018-09-24 19:22:51 +0300127 struct rcu_head rcu;
Eric Dumazet606509f2021-12-04 20:22:13 -0800128 netdevice_tracker dev_tracker;
Eric Dumazet846e4632020-10-07 09:51:11 -0700129 /* private data */
130 long privdata[] ____cacheline_aligned;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131};
132
Eric Dumazet551143d2017-08-24 21:12:28 -0700133static inline void qdisc_refcount_inc(struct Qdisc *qdisc)
134{
135 if (qdisc->flags & TCQ_F_BUILTIN)
136 return;
137 refcount_inc(&qdisc->refcnt);
138}
139
Vlad Buslov9d7e82c2018-09-24 19:22:52 +0300140/* Intended to be used by unlocked users, when concurrent qdisc release is
141 * possible.
142 */
143
144static inline struct Qdisc *qdisc_refcount_inc_nz(struct Qdisc *qdisc)
145{
146 if (qdisc->flags & TCQ_F_BUILTIN)
147 return qdisc;
148 if (refcount_inc_not_zero(&qdisc->refcnt))
149 return qdisc;
150 return NULL;
151}
152
Ahmed S. Darwish29cbcd82021-10-16 10:49:10 +0200153/* For !TCQ_F_NOLOCK qdisc: callers must either call this within a qdisc
154 * root_lock section, or provide their own memory barriers -- ordering
155 * against qdisc_run_begin/end() atomic bit operations.
156 */
Paolo Abeni96009c72018-05-15 16:24:36 +0200157static inline bool qdisc_is_running(struct Qdisc *qdisc)
Eric Dumazetbc135b22010-06-02 03:23:51 -0700158{
Paolo Abeni32f7b442018-05-15 10:50:31 +0200159 if (qdisc->flags & TCQ_F_NOLOCK)
Paolo Abeni96009c72018-05-15 16:24:36 +0200160 return spin_is_locked(&qdisc->seqlock);
Eric Dumazet97604c62021-10-18 17:34:02 -0700161 return test_bit(__QDISC_STATE2_RUNNING, &qdisc->state2);
Eric Dumazetbc135b22010-06-02 03:23:51 -0700162}
163
Yunsheng Linc4fef012021-06-22 14:49:56 +0800164static inline bool nolock_qdisc_is_empty(const struct Qdisc *qdisc)
165{
166 return !(READ_ONCE(qdisc->state) & QDISC_STATE_NON_EMPTY);
167}
168
Paolo Abeni9c01c9f2019-04-10 14:32:39 +0200169static inline bool qdisc_is_percpu_stats(const struct Qdisc *q)
170{
171 return q->flags & TCQ_F_CPUSTATS;
172}
173
Paolo Abeni28cff532019-03-22 16:01:55 +0100174static inline bool qdisc_is_empty(const struct Qdisc *qdisc)
175{
Paolo Abeni9c01c9f2019-04-10 14:32:39 +0200176 if (qdisc_is_percpu_stats(qdisc))
Yunsheng Lind3e0f572021-06-22 14:49:57 +0800177 return nolock_qdisc_is_empty(qdisc);
Eric Dumazet90b2be22019-11-08 08:45:23 -0800178 return !READ_ONCE(qdisc->q.qlen);
Paolo Abeni28cff532019-03-22 16:01:55 +0100179}
180
Ahmed S. Darwish29cbcd82021-10-16 10:49:10 +0200181/* For !TCQ_F_NOLOCK qdisc, qdisc_run_begin/end() must be invoked with
182 * the qdisc root lock acquired.
183 */
Eric Dumazetbc135b22010-06-02 03:23:51 -0700184static inline bool qdisc_run_begin(struct Qdisc *qdisc)
185{
Paolo Abeni32f7b442018-05-15 10:50:31 +0200186 if (qdisc->flags & TCQ_F_NOLOCK) {
Yunsheng Lina90c57f2021-05-14 11:16:59 +0800187 if (spin_trylock(&qdisc->seqlock))
Yunsheng Lind3e0f572021-06-22 14:49:57 +0800188 return true;
Yunsheng Lina90c57f2021-05-14 11:16:59 +0800189
Yunsheng Lin89837eb2021-06-17 09:04:14 +0800190 /* Paired with smp_mb__after_atomic() to make sure
191 * STATE_MISSED checking is synchronized with clearing
192 * in pfifo_fast_dequeue().
193 */
194 smp_mb__before_atomic();
195
Yunsheng Lina90c57f2021-05-14 11:16:59 +0800196 /* If the MISSED flag is set, it means other thread has
197 * set the MISSED flag before second spin_trylock(), so
198 * we can return false here to avoid multi cpus doing
199 * the set_bit() and second spin_trylock() concurrently.
200 */
201 if (test_bit(__QDISC_STATE_MISSED, &qdisc->state))
202 return false;
203
204 /* Set the MISSED flag before the second spin_trylock(),
205 * if the second spin_trylock() return false, it means
206 * other cpu holding the lock will do dequeuing for us
207 * or it will see the MISSED flag set after releasing
208 * lock and reschedule the net_tx_action() to do the
209 * dequeuing.
210 */
211 set_bit(__QDISC_STATE_MISSED, &qdisc->state);
212
Yunsheng Lin89837eb2021-06-17 09:04:14 +0800213 /* spin_trylock() only has load-acquire semantic, so use
214 * smp_mb__after_atomic() to ensure STATE_MISSED is set
215 * before doing the second spin_trylock().
216 */
217 smp_mb__after_atomic();
218
Yunsheng Lina90c57f2021-05-14 11:16:59 +0800219 /* Retry again in case other CPU may not see the new flag
220 * after it releases the lock at the end of qdisc_run_end().
221 */
Yunsheng Lind3e0f572021-06-22 14:49:57 +0800222 return spin_trylock(&qdisc->seqlock);
Paolo Abeni32f7b442018-05-15 10:50:31 +0200223 }
Eric Dumazet97604c62021-10-18 17:34:02 -0700224 return !__test_and_set_bit(__QDISC_STATE2_RUNNING, &qdisc->state2);
Eric Dumazetbc135b22010-06-02 03:23:51 -0700225}
226
227static inline void qdisc_run_end(struct Qdisc *qdisc)
228{
Yunsheng Lina90c57f2021-05-14 11:16:59 +0800229 if (qdisc->flags & TCQ_F_NOLOCK) {
Paolo Abeni96009c72018-05-15 16:24:36 +0200230 spin_unlock(&qdisc->seqlock);
Yunsheng Lina90c57f2021-05-14 11:16:59 +0800231
232 if (unlikely(test_bit(__QDISC_STATE_MISSED,
Yunsheng Linc4fef012021-06-22 14:49:56 +0800233 &qdisc->state)))
Yunsheng Lina90c57f2021-05-14 11:16:59 +0800234 __netif_schedule(qdisc);
Yunsheng Lindd252962021-06-22 14:49:55 +0800235 } else {
Eric Dumazet97604c62021-10-18 17:34:02 -0700236 __clear_bit(__QDISC_STATE2_RUNNING, &qdisc->state2);
Yunsheng Lina90c57f2021-05-14 11:16:59 +0800237 }
Eric Dumazetfd245a42011-01-20 05:27:16 +0000238}
239
Jesper Dangaard Brouer5772e9a2014-10-01 22:35:59 +0200240static inline bool qdisc_may_bulk(const struct Qdisc *qdisc)
241{
242 return qdisc->flags & TCQ_F_ONETXQUEUE;
243}
244
245static inline int qdisc_avail_bulklimit(const struct netdev_queue *txq)
246{
247#ifdef CONFIG_BQL
248 /* Non-BQL migrated drivers will return 0, too. */
249 return dql_avail(&txq->dql);
250#else
251 return 0;
252#endif
253}
254
Eric Dumazetfd2c3ef2009-11-03 03:26:03 +0000255struct Qdisc_class_ops {
Vlad Buslovdfcd2a22019-02-11 10:55:46 +0200256 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 /* Child qdisc manipulation */
Jarek Poplawski926e61b2009-09-15 02:53:07 -0700258 struct netdev_queue * (*select_queue)(struct Qdisc *, struct tcmsg *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 int (*graft)(struct Qdisc *, unsigned long cl,
Alexander Aring653d6fd2017-12-20 12:35:17 -0500260 struct Qdisc *, struct Qdisc **,
261 struct netlink_ext_ack *extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 struct Qdisc * (*leaf)(struct Qdisc *, unsigned long cl);
Patrick McHardy43effa12006-11-29 17:35:48 -0800263 void (*qlen_notify)(struct Qdisc *, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265 /* Class manipulation routines */
WANG Cong143976c2017-08-24 16:51:29 -0700266 unsigned long (*find)(struct Qdisc *, u32 classid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 int (*change)(struct Qdisc *, u32, u32,
Alexander Aring793d81d2017-12-20 12:35:15 -0500268 struct nlattr **, unsigned long *,
269 struct netlink_ext_ack *);
Maxim Mikityanskiy4dd78a72021-01-19 14:08:12 +0200270 int (*delete)(struct Qdisc *, unsigned long,
271 struct netlink_ext_ack *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 void (*walk)(struct Qdisc *, struct qdisc_walker * arg);
273
274 /* Filter manipulation */
Alexander Aring0ac4bd62017-12-04 18:39:59 -0500275 struct tcf_block * (*tcf_block)(struct Qdisc *sch,
Alexander Aringcbaacc42017-12-20 12:35:16 -0500276 unsigned long arg,
277 struct netlink_ext_ack *extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 unsigned long (*bind_tcf)(struct Qdisc *, unsigned long,
279 u32 classid);
280 void (*unbind_tcf)(struct Qdisc *, unsigned long);
281
282 /* rtnetlink specific */
283 int (*dump)(struct Qdisc *, unsigned long,
284 struct sk_buff *skb, struct tcmsg*);
285 int (*dump_stats)(struct Qdisc *, unsigned long,
286 struct gnet_dump *);
287};
288
Vlad Buslovdfcd2a22019-02-11 10:55:46 +0200289/* Qdisc_class_ops flag values */
290
291/* Implements API that doesn't require rtnl lock */
292enum qdisc_class_ops_flags {
293 QDISC_CLASS_OPS_DOIT_UNLOCKED = 1,
294};
295
Eric Dumazetfd2c3ef2009-11-03 03:26:03 +0000296struct Qdisc_ops {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 struct Qdisc_ops *next;
Eric Dumazet20fea082007-11-14 01:44:41 -0800298 const struct Qdisc_class_ops *cl_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 char id[IFNAMSIZ];
300 int priv_size;
John Fastabendd59f5ff2017-12-07 09:55:26 -0800301 unsigned int static_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Eric Dumazet520ac302016-06-21 23:16:49 -0700303 int (*enqueue)(struct sk_buff *skb,
304 struct Qdisc *sch,
305 struct sk_buff **to_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 struct sk_buff * (*dequeue)(struct Qdisc *);
Jarek Poplawski90d841fd2008-10-31 00:43:45 -0700307 struct sk_buff * (*peek)(struct Qdisc *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Alexander Aringe63d7df2017-12-20 12:35:13 -0500309 int (*init)(struct Qdisc *sch, struct nlattr *arg,
310 struct netlink_ext_ack *extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 void (*reset)(struct Qdisc *);
312 void (*destroy)(struct Qdisc *);
Alexander Aring0ac4bd62017-12-04 18:39:59 -0500313 int (*change)(struct Qdisc *sch,
Alexander Aring20307212017-12-20 12:35:14 -0500314 struct nlattr *arg,
315 struct netlink_ext_ack *extack);
Alexander Aring0ac4bd62017-12-04 18:39:59 -0500316 void (*attach)(struct Qdisc *sch);
Cong Wang48bfd552018-01-25 18:26:23 -0800317 int (*change_tx_queue_len)(struct Qdisc *, unsigned int);
Jakub Kicinski1e080f12021-09-13 15:53:30 -0700318 void (*change_real_num_tx)(struct Qdisc *sch,
319 unsigned int new_real_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
321 int (*dump)(struct Qdisc *, struct sk_buff *);
322 int (*dump_stats)(struct Qdisc *, struct gnet_dump *);
323
Jiri Pirkod47a6b02018-01-17 11:46:52 +0100324 void (*ingress_block_set)(struct Qdisc *sch,
325 u32 block_index);
326 void (*egress_block_set)(struct Qdisc *sch,
327 u32 block_index);
328 u32 (*ingress_block_get)(struct Qdisc *sch);
329 u32 (*egress_block_get)(struct Qdisc *sch);
330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 struct module *owner;
332};
333
334
Eric Dumazetfd2c3ef2009-11-03 03:26:03 +0000335struct tcf_result {
Jiri Pirkodb505142017-05-17 11:08:03 +0200336 union {
337 struct {
338 unsigned long class;
339 u32 classid;
340 };
341 const struct tcf_proto *goto_tp;
Paolo Abenicd11b1642018-07-30 14:30:44 +0200342
John Hurley720f22f2019-06-24 23:13:35 +0100343 /* used in the skb_tc_reinsert function */
Paolo Abenicd11b1642018-07-30 14:30:44 +0200344 struct {
345 bool ingress;
346 struct gnet_stats_queue *qstats;
347 };
Jiri Pirkodb505142017-05-17 11:08:03 +0200348 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349};
350
Jiri Pirko9f407f12018-07-23 09:23:07 +0200351struct tcf_chain;
352
Eric Dumazetfd2c3ef2009-11-03 03:26:03 +0000353struct tcf_proto_ops {
WANG Cong36272872013-12-15 20:15:11 -0800354 struct list_head head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 char kind[IFNAMSIZ];
356
Eric Dumazetdc7f9f62011-07-05 23:25:42 +0000357 int (*classify)(struct sk_buff *,
358 const struct tcf_proto *,
359 struct tcf_result *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 int (*init)(struct tcf_proto*);
Vlad Buslov12db03b2019-02-11 10:55:45 +0200361 void (*destroy)(struct tcf_proto *tp, bool rtnl_held,
Jakub Kicinski715df5e2018-01-24 12:54:13 -0800362 struct netlink_ext_ack *extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
WANG Cong8113c092017-08-04 21:31:43 -0700364 void* (*get)(struct tcf_proto*, u32 handle);
Vlad Buslov7d5509f2019-02-11 10:55:44 +0200365 void (*put)(struct tcf_proto *tp, void *f);
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000366 int (*change)(struct net *net, struct sk_buff *,
Eric W. Biedermanaf4c6642012-05-25 13:42:45 -0600367 struct tcf_proto*, unsigned long,
Patrick McHardyadd93b62008-01-22 22:11:33 -0800368 u32 handle, struct nlattr **,
Cong Wang695176b2021-07-29 16:12:14 -0700369 void **, u32,
Alexander Aring7306db32018-01-18 11:20:51 -0500370 struct netlink_ext_ack *);
Alexander Aring8865fdd2018-01-18 11:20:49 -0500371 int (*delete)(struct tcf_proto *tp, void *arg,
Vlad Buslov12db03b2019-02-11 10:55:45 +0200372 bool *last, bool rtnl_held,
Alexander Aring571acf22018-01-18 11:20:53 -0500373 struct netlink_ext_ack *);
Davide Carattia5b72a02019-12-28 16:36:58 +0100374 bool (*delete_empty)(struct tcf_proto *tp);
Vlad Buslov12db03b2019-02-11 10:55:45 +0200375 void (*walk)(struct tcf_proto *tp,
376 struct tcf_walker *arg, bool rtnl_held);
John Hurleye56185c2018-06-25 14:30:05 -0700377 int (*reoffload)(struct tcf_proto *tp, bool add,
Pablo Neira Ayusoa7323312019-07-19 18:20:15 +0200378 flow_setup_cb_t *cb, void *cb_priv,
John Hurleye56185c2018-06-25 14:30:05 -0700379 struct netlink_ext_ack *extack);
Vlad Buslova449a3e2019-08-26 16:45:00 +0300380 void (*hw_add)(struct tcf_proto *tp,
381 void *type_data);
382 void (*hw_del)(struct tcf_proto *tp,
383 void *type_data);
Cong Wang2e24cd72020-01-23 16:26:18 -0800384 void (*bind_class)(void *, u32, unsigned long,
385 void *, unsigned long);
Jiri Pirko9f407f12018-07-23 09:23:07 +0200386 void * (*tmplt_create)(struct net *net,
387 struct tcf_chain *chain,
388 struct nlattr **tca,
389 struct netlink_ext_ack *extack);
390 void (*tmplt_destroy)(void *tmplt_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
392 /* rtnetlink specific */
WANG Cong8113c092017-08-04 21:31:43 -0700393 int (*dump)(struct net*, struct tcf_proto*, void *,
Vlad Buslov12db03b2019-02-11 10:55:45 +0200394 struct sk_buff *skb, struct tcmsg*,
395 bool);
Vlad Buslovf8ab1802020-05-15 14:40:11 +0300396 int (*terse_dump)(struct net *net,
397 struct tcf_proto *tp, void *fh,
398 struct sk_buff *skb,
399 struct tcmsg *t, bool rtnl_held);
Jiri Pirko9f407f12018-07-23 09:23:07 +0200400 int (*tmplt_dump)(struct sk_buff *skb,
401 struct net *net,
402 void *tmplt_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404 struct module *owner;
Vlad Buslov12db03b2019-02-11 10:55:45 +0200405 int flags;
406};
407
Davide Carattia5b72a02019-12-28 16:36:58 +0100408/* Classifiers setting TCF_PROTO_OPS_DOIT_UNLOCKED in tcf_proto_ops->flags
409 * are expected to implement tcf_proto_ops->delete_empty(), otherwise race
410 * conditions can occur when filters are inserted/deleted simultaneously.
411 */
Vlad Buslov12db03b2019-02-11 10:55:45 +0200412enum tcf_proto_ops_flags {
413 TCF_PROTO_OPS_DOIT_UNLOCKED = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414};
415
Eric Dumazetfd2c3ef2009-11-03 03:26:03 +0000416struct tcf_proto {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 /* Fast access part */
John Fastabend25d8c0d2014-09-12 20:05:27 -0700418 struct tcf_proto __rcu *next;
419 void __rcu *root;
Paolo Abeni7fd4b282018-07-30 14:30:43 +0200420
421 /* called under RCU BH lock*/
Eric Dumazetdc7f9f62011-07-05 23:25:42 +0000422 int (*classify)(struct sk_buff *,
423 const struct tcf_proto *,
424 struct tcf_result *);
Al Viro66c6f522006-11-20 18:07:51 -0800425 __be16 protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427 /* All the rest */
428 u32 prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 void *data;
Eric Dumazetdc7f9f62011-07-05 23:25:42 +0000430 const struct tcf_proto_ops *ops;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200431 struct tcf_chain *chain;
Vlad Buslov8b646782019-02-11 10:55:41 +0200432 /* Lock protects tcf_proto shared state and can be used by unlocked
433 * classifiers to protect their private data.
434 */
435 spinlock_t lock;
436 bool deleting;
Vlad Buslov4dbfa762019-02-11 10:55:39 +0200437 refcount_t refcnt;
John Fastabend25d8c0d2014-09-12 20:05:27 -0700438 struct rcu_head rcu;
John Hurley59eb87c2019-11-02 14:17:47 +0000439 struct hlist_node destroy_ht_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440};
441
Jussi Kivilinna175f9c12008-07-20 00:08:47 -0700442struct qdisc_skb_cb {
Stanislav Fomichev089b19a2019-04-22 08:55:44 -0700443 struct {
444 unsigned int pkt_len;
445 u16 slave_dev_queue_mapping;
446 u16 tc_classid;
Petar Penkovd58e4682018-09-14 07:46:18 -0700447 };
Eric Dumazet25711782014-09-18 08:02:05 -0700448#define QDISC_CB_PRIV_LEN 20
449 unsigned char data[QDISC_CB_PRIV_LEN];
Jussi Kivilinna175f9c12008-07-20 00:08:47 -0700450};
451
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100452typedef void tcf_chain_head_change_t(struct tcf_proto *tp_head, void *priv);
453
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200454struct tcf_chain {
Vlad Busloved76f5e2019-02-11 10:55:38 +0200455 /* Protects filter_chain. */
456 struct mutex filter_chain_lock;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200457 struct tcf_proto __rcu *filter_chain;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200458 struct list_head list;
459 struct tcf_block *block;
460 u32 index; /* chain index */
461 unsigned int refcnt;
Jiri Pirko1f3ed382018-07-27 09:45:05 +0200462 unsigned int action_refcnt;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +0200463 bool explicitly_created;
Vlad Buslov726d06122019-02-11 10:55:42 +0200464 bool flushing;
Jiri Pirko9f407f12018-07-23 09:23:07 +0200465 const struct tcf_proto_ops *tmplt_ops;
466 void *tmplt_priv;
Davide Carattiee3bbfe2019-03-20 15:00:16 +0100467 struct rcu_head rcu;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200468};
469
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200470struct tcf_block {
Vlad Buslovc266f642019-02-11 10:55:32 +0200471 /* Lock protects tcf_block and lifetime-management data of chains
472 * attached to the block (refcnt, action_refcnt, explicitly_created).
473 */
474 struct mutex lock;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200475 struct list_head chain_list;
Jiri Pirko48617382018-01-17 11:46:46 +0100476 u32 index; /* block index for shared blocks */
Cong Wanga7df4872020-04-30 20:53:49 -0700477 u32 classid; /* which class this block belongs to */
Vlad Buslovcfebd7e2018-09-24 19:22:54 +0300478 refcount_t refcnt;
Jiri Pirko855319b2017-10-13 14:00:58 +0200479 struct net *net;
Jiri Pirko69d78ef2017-10-13 14:00:57 +0200480 struct Qdisc *q;
Vlad Buslov4f8116c2019-08-26 16:44:57 +0300481 struct rw_semaphore cb_lock; /* protects cb_list and offload counters */
Pablo Neira Ayuso14bfb132019-07-19 18:20:16 +0200482 struct flow_block flow_block;
Jiri Pirkof36fe1c2018-01-17 11:46:48 +0100483 struct list_head owner_list;
484 bool keep_dst;
Vlad Buslov97394be2019-08-26 16:44:58 +0300485 atomic_t offloadcnt; /* Number of oddloaded filters */
Jiri Pirkocaa72602018-01-17 11:46:50 +0100486 unsigned int nooffloaddevcnt; /* Number of devs unable to do offload */
Vlad Buslovc9f14472019-08-26 16:45:01 +0300487 unsigned int lockeddevcnt; /* Number of devs that require rtnl lock. */
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200488 struct {
489 struct tcf_chain *chain;
490 struct list_head filter_chain_list;
491 } chain0;
Vlad Buslov0607e432018-09-24 19:22:57 +0300492 struct rcu_head rcu;
John Hurley59eb87c2019-11-02 14:17:47 +0000493 DECLARE_HASHTABLE(proto_destroy_ht, 7);
494 struct mutex proto_destroy_lock; /* Lock for proto_destroy hashtable. */
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200495};
496
Vlad Busloved76f5e2019-02-11 10:55:38 +0200497static inline bool lockdep_tcf_chain_is_locked(struct tcf_chain *chain)
498{
499 return lockdep_is_held(&chain->filter_chain_lock);
500}
Vlad Buslov8b646782019-02-11 10:55:41 +0200501
502static inline bool lockdep_tcf_proto_is_locked(struct tcf_proto *tp)
503{
504 return lockdep_is_held(&tp->lock);
505}
Vlad Busloved76f5e2019-02-11 10:55:38 +0200506
507#define tcf_chain_dereference(p, chain) \
508 rcu_dereference_protected(p, lockdep_tcf_chain_is_locked(chain))
509
Vlad Buslov8b646782019-02-11 10:55:41 +0200510#define tcf_proto_dereference(p, tp) \
511 rcu_dereference_protected(p, lockdep_tcf_proto_is_locked(tp))
512
David S. Miller16bda132012-02-06 15:14:37 -0500513static inline void qdisc_cb_private_validate(const struct sk_buff *skb, int sz)
514{
515 struct qdisc_skb_cb *qcb;
Eric Dumazet5ee31c682012-06-12 06:03:51 +0000516
wenxu038ebb12020-07-31 10:45:01 +0800517 BUILD_BUG_ON(sizeof(skb->cb) < sizeof(*qcb));
David S. Miller16bda132012-02-06 15:14:37 -0500518 BUILD_BUG_ON(sizeof(qcb->data) < sz);
519}
520
Paolo Abeni73eb6282019-04-10 14:32:41 +0200521static inline int qdisc_qlen_cpu(const struct Qdisc *q)
522{
523 return this_cpu_ptr(q->cpu_qstats)->qlen;
524}
525
Eric Dumazet05bdd2f2011-10-20 17:45:43 -0400526static inline int qdisc_qlen(const struct Qdisc *q)
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +0000527{
528 return q->q.qlen;
529}
530
Paolo Abeni73eb6282019-04-10 14:32:41 +0200531static inline int qdisc_qlen_sum(const struct Qdisc *q)
John Fastabend7e660162017-12-07 09:57:00 -0800532{
Paolo Abeni73eb6282019-04-10 14:32:41 +0200533 __u32 qlen = q->qstats.qlen;
534 int i;
John Fastabend7e660162017-12-07 09:57:00 -0800535
Paolo Abeni73eb6282019-04-10 14:32:41 +0200536 if (qdisc_is_percpu_stats(q)) {
537 for_each_possible_cpu(i)
538 qlen += per_cpu_ptr(q->cpu_qstats, i)->qlen;
539 } else {
Jakub Kicinski6172abc2018-05-25 21:53:30 -0700540 qlen += q->q.qlen;
Paolo Abeni73eb6282019-04-10 14:32:41 +0200541 }
John Fastabend7e660162017-12-07 09:57:00 -0800542
543 return qlen;
544}
545
Eric Dumazetbfe0d022011-01-09 08:30:54 +0000546static inline struct qdisc_skb_cb *qdisc_skb_cb(const struct sk_buff *skb)
Jussi Kivilinna175f9c12008-07-20 00:08:47 -0700547{
548 return (struct qdisc_skb_cb *)skb->cb;
549}
550
David S. Miller838740002008-07-17 00:53:03 -0700551static inline spinlock_t *qdisc_lock(struct Qdisc *qdisc)
552{
553 return &qdisc->q.lock;
554}
555
Eric Dumazet05bdd2f2011-10-20 17:45:43 -0400556static inline struct Qdisc *qdisc_root(const struct Qdisc *qdisc)
David S. Miller7698b4f2008-07-16 01:42:40 -0700557{
John Fastabend46e5da40a2014-09-12 20:04:52 -0700558 struct Qdisc *q = rcu_dereference_rtnl(qdisc->dev_queue->qdisc);
559
560 return q;
David S. Miller7698b4f2008-07-16 01:42:40 -0700561}
562
Eric Dumazet159d2c72019-09-24 13:11:26 -0700563static inline struct Qdisc *qdisc_root_bh(const struct Qdisc *qdisc)
564{
565 return rcu_dereference_bh(qdisc->dev_queue->qdisc);
566}
567
Eric Dumazet05bdd2f2011-10-20 17:45:43 -0400568static inline struct Qdisc *qdisc_root_sleeping(const struct Qdisc *qdisc)
Jarek Poplawski2540e052008-08-21 05:11:14 -0700569{
570 return qdisc->dev_queue->qdisc_sleeping;
571}
572
David S. Miller7e43f112008-08-02 23:27:37 -0700573/* The qdisc root lock is a mechanism by which to top level
574 * of a qdisc tree can be locked from any qdisc node in the
575 * forest. This allows changing the configuration of some
576 * aspect of the qdisc tree while blocking out asynchronous
577 * qdisc access in the packet processing paths.
578 *
579 * It is only legal to do this when the root will not change
580 * on us. Otherwise we'll potentially lock the wrong qdisc
581 * root. This is enforced by holding the RTNL semaphore, which
582 * all users of this lock accessor must do.
583 */
Eric Dumazet05bdd2f2011-10-20 17:45:43 -0400584static inline spinlock_t *qdisc_root_lock(const struct Qdisc *qdisc)
David S. Miller7698b4f2008-07-16 01:42:40 -0700585{
586 struct Qdisc *root = qdisc_root(qdisc);
587
David S. Miller7e43f112008-08-02 23:27:37 -0700588 ASSERT_RTNL();
David S. Miller838740002008-07-17 00:53:03 -0700589 return qdisc_lock(root);
David S. Miller7698b4f2008-07-16 01:42:40 -0700590}
591
Eric Dumazet05bdd2f2011-10-20 17:45:43 -0400592static inline spinlock_t *qdisc_root_sleeping_lock(const struct Qdisc *qdisc)
Jarek Poplawskif6f9b932008-08-27 02:25:17 -0700593{
594 struct Qdisc *root = qdisc_root_sleeping(qdisc);
595
596 ASSERT_RTNL();
597 return qdisc_lock(root);
598}
599
Eric Dumazet05bdd2f2011-10-20 17:45:43 -0400600static inline struct net_device *qdisc_dev(const struct Qdisc *qdisc)
David S. Miller5ce2d482008-07-08 17:06:30 -0700601{
602 return qdisc->dev_queue->dev;
603}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Maxim Mikityanskiyca1e4ab2021-01-19 14:08:11 +0200605static inline void sch_tree_lock(struct Qdisc *q)
David S. Miller78a5b302008-07-16 03:12:24 -0700606{
Maxim Mikityanskiyca1e4ab2021-01-19 14:08:11 +0200607 if (q->flags & TCQ_F_MQROOT)
608 spin_lock_bh(qdisc_lock(q));
609 else
610 spin_lock_bh(qdisc_root_sleeping_lock(q));
David S. Miller78a5b302008-07-16 03:12:24 -0700611}
612
Maxim Mikityanskiyca1e4ab2021-01-19 14:08:11 +0200613static inline void sch_tree_unlock(struct Qdisc *q)
David S. Miller78a5b302008-07-16 03:12:24 -0700614{
Maxim Mikityanskiyca1e4ab2021-01-19 14:08:11 +0200615 if (q->flags & TCQ_F_MQROOT)
616 spin_unlock_bh(qdisc_lock(q));
617 else
618 spin_unlock_bh(qdisc_root_sleeping_lock(q));
David S. Miller78a5b302008-07-16 03:12:24 -0700619}
620
Thomas Grafe41a33e2005-07-05 14:14:30 -0700621extern struct Qdisc noop_qdisc;
622extern struct Qdisc_ops noop_qdisc_ops;
David S. Miller6ec1c692009-09-06 01:58:51 -0700623extern struct Qdisc_ops pfifo_fast_ops;
624extern struct Qdisc_ops mq_qdisc_ops;
Phil Sutterd66d6c32015-08-27 21:21:38 +0200625extern struct Qdisc_ops noqueue_qdisc_ops;
stephen hemminger6da7c8f2013-08-27 16:19:08 -0700626extern const struct Qdisc_ops *default_qdisc_ops;
Eric Dumazet1f27cde2016-03-02 08:21:43 -0800627static inline const struct Qdisc_ops *
628get_default_qdisc_ops(const struct net_device *dev, int ntx)
629{
630 return ntx < dev->real_num_tx_queues ?
631 default_qdisc_ops : &pfifo_fast_ops;
632}
Thomas Grafe41a33e2005-07-05 14:14:30 -0700633
Eric Dumazetfd2c3ef2009-11-03 03:26:03 +0000634struct Qdisc_class_common {
Patrick McHardy6fe1c7a2008-07-05 23:21:31 -0700635 u32 classid;
636 struct hlist_node hnode;
637};
638
Eric Dumazetfd2c3ef2009-11-03 03:26:03 +0000639struct Qdisc_class_hash {
Patrick McHardy6fe1c7a2008-07-05 23:21:31 -0700640 struct hlist_head *hash;
641 unsigned int hashsize;
642 unsigned int hashmask;
643 unsigned int hashelems;
644};
645
646static inline unsigned int qdisc_class_hash(u32 id, u32 mask)
647{
648 id ^= id >> 8;
649 id ^= id >> 4;
650 return id & mask;
651}
652
653static inline struct Qdisc_class_common *
Eric Dumazet05bdd2f2011-10-20 17:45:43 -0400654qdisc_class_find(const struct Qdisc_class_hash *hash, u32 id)
Patrick McHardy6fe1c7a2008-07-05 23:21:31 -0700655{
656 struct Qdisc_class_common *cl;
Patrick McHardy6fe1c7a2008-07-05 23:21:31 -0700657 unsigned int h;
658
Gao Feng7d3f0cd2017-08-18 15:23:24 +0800659 if (!id)
660 return NULL;
661
Patrick McHardy6fe1c7a2008-07-05 23:21:31 -0700662 h = qdisc_class_hash(id, hash->hashmask);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800663 hlist_for_each_entry(cl, &hash->hash[h], hnode) {
Patrick McHardy6fe1c7a2008-07-05 23:21:31 -0700664 if (cl->classid == id)
665 return cl;
666 }
667 return NULL;
668}
669
Amritha Nambiar384c1812017-10-27 02:35:34 -0700670static inline int tc_classid_to_hwtc(struct net_device *dev, u32 classid)
671{
672 u32 hwtc = TC_H_MIN(classid) - TC_H_MIN_PRIORITY;
673
674 return (hwtc < netdev_get_num_tc(dev)) ? hwtc : -EINVAL;
675}
676
Joe Perches5c152572013-07-30 22:47:13 -0700677int qdisc_class_hash_init(struct Qdisc_class_hash *);
678void qdisc_class_hash_insert(struct Qdisc_class_hash *,
679 struct Qdisc_class_common *);
680void qdisc_class_hash_remove(struct Qdisc_class_hash *,
681 struct Qdisc_class_common *);
682void qdisc_class_hash_grow(struct Qdisc *, struct Qdisc_class_hash *);
683void qdisc_class_hash_destroy(struct Qdisc_class_hash *);
Patrick McHardy6fe1c7a2008-07-05 23:21:31 -0700684
Cong Wang48bfd552018-01-25 18:26:23 -0800685int dev_qdisc_change_tx_queue_len(struct net_device *dev);
Jakub Kicinski1e080f12021-09-13 15:53:30 -0700686void dev_qdisc_change_real_num_tx(struct net_device *dev,
687 unsigned int new_real_tx);
Joe Perches5c152572013-07-30 22:47:13 -0700688void dev_init_scheduler(struct net_device *dev);
689void dev_shutdown(struct net_device *dev);
690void dev_activate(struct net_device *dev);
691void dev_deactivate(struct net_device *dev);
692void dev_deactivate_many(struct list_head *head);
693struct Qdisc *dev_graft_qdisc(struct netdev_queue *dev_queue,
694 struct Qdisc *qdisc);
695void qdisc_reset(struct Qdisc *qdisc);
Vlad Buslov86bd4462018-09-24 19:22:50 +0300696void qdisc_put(struct Qdisc *qdisc);
Vlad Buslov3a7d0d02018-09-24 19:22:51 +0300697void qdisc_put_unlocked(struct Qdisc *qdisc);
Toke Høiland-Jørgensen5f2939d2019-01-09 17:10:57 +0100698void qdisc_tree_reduce_backlog(struct Qdisc *qdisc, int n, int len);
Jakub Kicinskib5928432018-11-07 17:33:34 -0800699#ifdef CONFIG_NET_SCHED
700int qdisc_offload_dump_helper(struct Qdisc *q, enum tc_setup_type type,
701 void *type_data);
Jakub Kicinskibfaee912018-11-07 17:33:37 -0800702void qdisc_offload_graft_helper(struct net_device *dev, struct Qdisc *sch,
703 struct Qdisc *new, struct Qdisc *old,
704 enum tc_setup_type type, void *type_data,
705 struct netlink_ext_ack *extack);
Jakub Kicinskib5928432018-11-07 17:33:34 -0800706#else
707static inline int
708qdisc_offload_dump_helper(struct Qdisc *q, enum tc_setup_type type,
709 void *type_data)
710{
711 q->flags &= ~TCQ_F_OFFLOADED;
712 return 0;
713}
Jakub Kicinskibfaee912018-11-07 17:33:37 -0800714
715static inline void
716qdisc_offload_graft_helper(struct net_device *dev, struct Qdisc *sch,
717 struct Qdisc *new, struct Qdisc *old,
718 enum tc_setup_type type, void *type_data,
719 struct netlink_ext_ack *extack)
720{
721}
Jakub Kicinskib5928432018-11-07 17:33:34 -0800722#endif
Joe Perches5c152572013-07-30 22:47:13 -0700723struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue,
Alexander Aringd0bd6842017-12-20 12:35:20 -0500724 const struct Qdisc_ops *ops,
725 struct netlink_ext_ack *extack);
Daniel Borkmann81d947e2018-01-15 23:12:09 +0100726void qdisc_free(struct Qdisc *qdisc);
Joe Perches5c152572013-07-30 22:47:13 -0700727struct Qdisc *qdisc_create_dflt(struct netdev_queue *dev_queue,
Alexander Aringa38a98822017-12-20 12:35:21 -0500728 const struct Qdisc_ops *ops, u32 parentid,
729 struct netlink_ext_ack *extack);
Joe Perches5c152572013-07-30 22:47:13 -0700730void __qdisc_calculate_pkt_len(struct sk_buff *skb,
731 const struct qdisc_size_table *stab);
Alexei Starovoitov27b29f62015-09-15 23:05:43 -0700732int skb_do_redirect(struct sk_buff *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Daniel Borkmannfdc54322016-01-07 15:50:22 +0100734static inline bool skb_at_tc_ingress(const struct sk_buff *skb)
735{
736#ifdef CONFIG_NET_CLS_ACT
Willem de Bruijn8dc07fd2017-01-07 17:06:37 -0500737 return skb->tc_at_ingress;
Daniel Borkmannfdc54322016-01-07 15:50:22 +0100738#else
739 return false;
740#endif
741}
742
Willem de Bruijne7246e12017-01-07 17:06:35 -0500743static inline bool skb_skip_tc_classify(struct sk_buff *skb)
744{
745#ifdef CONFIG_NET_CLS_ACT
746 if (skb->tc_skip_classify) {
747 skb->tc_skip_classify = 0;
748 return true;
749 }
750#endif
751 return false;
752}
753
Gal Pressman3a053b12018-02-28 15:59:15 +0200754/* Reset all TX qdiscs greater than index of a device. */
John Fastabendf0796d52010-07-01 13:21:57 +0000755static inline void qdisc_reset_all_tx_gt(struct net_device *dev, unsigned int i)
David S. Miller5aa70992008-07-08 22:59:10 -0700756{
John Fastabend4ef6acf2010-07-01 13:21:35 +0000757 struct Qdisc *qdisc;
758
John Fastabendf0796d52010-07-01 13:21:57 +0000759 for (; i < dev->num_tx_queues; i++) {
John Fastabend46e5da40a2014-09-12 20:04:52 -0700760 qdisc = rtnl_dereference(netdev_get_tx_queue(dev, i)->qdisc);
John Fastabend4ef6acf2010-07-01 13:21:35 +0000761 if (qdisc) {
762 spin_lock_bh(qdisc_lock(qdisc));
763 qdisc_reset(qdisc);
764 spin_unlock_bh(qdisc_lock(qdisc));
765 }
766 }
David S. Miller5aa70992008-07-08 22:59:10 -0700767}
768
David S. Miller3e745dd2008-07-08 23:00:25 -0700769/* Are all TX queues of the device empty? */
770static inline bool qdisc_all_tx_empty(const struct net_device *dev)
771{
David S. Millere8a04642008-07-17 00:34:19 -0700772 unsigned int i;
John Fastabend46e5da40a2014-09-12 20:04:52 -0700773
774 rcu_read_lock();
David S. Millere8a04642008-07-17 00:34:19 -0700775 for (i = 0; i < dev->num_tx_queues; i++) {
776 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
John Fastabend46e5da40a2014-09-12 20:04:52 -0700777 const struct Qdisc *q = rcu_dereference(txq->qdisc);
David S. Miller3e745dd2008-07-08 23:00:25 -0700778
Paolo Abeni1f5e6fd2019-04-10 14:32:38 +0200779 if (!qdisc_is_empty(q)) {
John Fastabend46e5da40a2014-09-12 20:04:52 -0700780 rcu_read_unlock();
David S. Millere8a04642008-07-17 00:34:19 -0700781 return false;
John Fastabend46e5da40a2014-09-12 20:04:52 -0700782 }
David S. Millere8a04642008-07-17 00:34:19 -0700783 }
John Fastabend46e5da40a2014-09-12 20:04:52 -0700784 rcu_read_unlock();
David S. Millere8a04642008-07-17 00:34:19 -0700785 return true;
David S. Miller3e745dd2008-07-08 23:00:25 -0700786}
787
David S. Miller6fa98642008-07-08 23:01:06 -0700788/* Are any of the TX qdiscs changing? */
Eric Dumazet05bdd2f2011-10-20 17:45:43 -0400789static inline bool qdisc_tx_changing(const struct net_device *dev)
David S. Miller6fa98642008-07-08 23:01:06 -0700790{
David S. Millere8a04642008-07-17 00:34:19 -0700791 unsigned int i;
John Fastabend46e5da40a2014-09-12 20:04:52 -0700792
David S. Millere8a04642008-07-17 00:34:19 -0700793 for (i = 0; i < dev->num_tx_queues; i++) {
794 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
John Fastabend46e5da40a2014-09-12 20:04:52 -0700795 if (rcu_access_pointer(txq->qdisc) != txq->qdisc_sleeping)
David S. Millere8a04642008-07-17 00:34:19 -0700796 return true;
797 }
798 return false;
David S. Miller6fa98642008-07-08 23:01:06 -0700799}
800
David S. Millere8a04642008-07-17 00:34:19 -0700801/* Is the device using the noop qdisc on all queues? */
David S. Miller05297942008-07-08 23:01:27 -0700802static inline bool qdisc_tx_is_noop(const struct net_device *dev)
803{
David S. Millere8a04642008-07-17 00:34:19 -0700804 unsigned int i;
John Fastabend46e5da40a2014-09-12 20:04:52 -0700805
David S. Millere8a04642008-07-17 00:34:19 -0700806 for (i = 0; i < dev->num_tx_queues; i++) {
807 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
John Fastabend46e5da40a2014-09-12 20:04:52 -0700808 if (rcu_access_pointer(txq->qdisc) != &noop_qdisc)
David S. Millere8a04642008-07-17 00:34:19 -0700809 return false;
810 }
811 return true;
David S. Miller05297942008-07-08 23:01:27 -0700812}
813
Eric Dumazetbfe0d022011-01-09 08:30:54 +0000814static inline unsigned int qdisc_pkt_len(const struct sk_buff *skb)
Jussi Kivilinna0abf77e2008-07-20 00:08:27 -0700815{
Jussi Kivilinna175f9c12008-07-20 00:08:47 -0700816 return qdisc_skb_cb(skb)->pkt_len;
Jussi Kivilinna0abf77e2008-07-20 00:08:27 -0700817}
818
Jarek Poplawskic27f3392008-08-04 22:39:11 -0700819/* additional qdisc xmit flags (NET_XMIT_MASK in linux/netdevice.h) */
Jarek Poplawski378a2f02008-08-04 22:31:03 -0700820enum net_xmit_qdisc_t {
821 __NET_XMIT_STOLEN = 0x00010000,
Jarek Poplawskic27f3392008-08-04 22:39:11 -0700822 __NET_XMIT_BYPASS = 0x00020000,
Jarek Poplawski378a2f02008-08-04 22:31:03 -0700823};
824
Jarek Poplawskic27f3392008-08-04 22:39:11 -0700825#ifdef CONFIG_NET_CLS_ACT
Jarek Poplawski378a2f02008-08-04 22:31:03 -0700826#define net_xmit_drop_count(e) ((e) & __NET_XMIT_STOLEN ? 0 : 1)
Jarek Poplawski378a2f02008-08-04 22:31:03 -0700827#else
828#define net_xmit_drop_count(e) (1)
829#endif
830
Eric Dumazeta2da5702011-01-20 03:48:19 +0000831static inline void qdisc_calculate_pkt_len(struct sk_buff *skb,
832 const struct Qdisc *sch)
Jussi Kivilinna5f861732008-07-20 00:08:04 -0700833{
David S. Miller3a682fb2008-07-20 18:13:01 -0700834#ifdef CONFIG_NET_SCHED
Eric Dumazeta2da5702011-01-20 03:48:19 +0000835 struct qdisc_size_table *stab = rcu_dereference_bh(sch->stab);
836
837 if (stab)
838 __qdisc_calculate_pkt_len(skb, stab);
David S. Miller3a682fb2008-07-20 18:13:01 -0700839#endif
Eric Dumazeta2da5702011-01-20 03:48:19 +0000840}
841
Petr Machataac5c66f2020-07-14 20:03:08 +0300842static inline int qdisc_enqueue(struct sk_buff *skb, struct Qdisc *sch,
Eric Dumazet520ac302016-06-21 23:16:49 -0700843 struct sk_buff **to_free)
Eric Dumazeta2da5702011-01-20 03:48:19 +0000844{
845 qdisc_calculate_pkt_len(skb, sch);
Petr Machataac5c66f2020-07-14 20:03:08 +0300846 return sch->enqueue(skb, sch, to_free);
Jussi Kivilinna5f861732008-07-20 00:08:04 -0700847}
848
Ahmed S. Darwish50dc9a82021-10-16 10:49:09 +0200849static inline void _bstats_update(struct gnet_stats_basic_sync *bstats,
Amir Vadai38040702016-05-13 12:55:35 +0000850 __u64 bytes, __u32 packets)
851{
Ahmed S. Darwish67c9e62702021-10-16 10:49:07 +0200852 u64_stats_update_begin(&bstats->syncp);
Ahmed S. Darwish50dc9a82021-10-16 10:49:09 +0200853 u64_stats_add(&bstats->bytes, bytes);
854 u64_stats_add(&bstats->packets, packets);
Ahmed S. Darwish67c9e62702021-10-16 10:49:07 +0200855 u64_stats_update_end(&bstats->syncp);
Amir Vadai38040702016-05-13 12:55:35 +0000856}
857
Ahmed S. Darwish50dc9a82021-10-16 10:49:09 +0200858static inline void bstats_update(struct gnet_stats_basic_sync *bstats,
Eric Dumazetbfe0d022011-01-09 08:30:54 +0000859 const struct sk_buff *skb)
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +0000860{
Amir Vadai38040702016-05-13 12:55:35 +0000861 _bstats_update(bstats,
862 qdisc_pkt_len(skb),
863 skb_is_gso(skb) ? skb_shinfo(skb)->gso_segs : 1);
864}
865
Eric Dumazet24ea5912015-07-06 05:18:03 -0700866static inline void qdisc_bstats_cpu_update(struct Qdisc *sch,
867 const struct sk_buff *skb)
868{
Ahmed S. Darwish50dc9a82021-10-16 10:49:09 +0200869 bstats_update(this_cpu_ptr(sch->cpu_bstats), skb);
Eric Dumazet24ea5912015-07-06 05:18:03 -0700870}
871
Eric Dumazetbfe0d022011-01-09 08:30:54 +0000872static inline void qdisc_bstats_update(struct Qdisc *sch,
873 const struct sk_buff *skb)
874{
875 bstats_update(&sch->bstats, skb);
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +0000876}
877
John Fastabend25331d62014-09-28 11:53:29 -0700878static inline void qdisc_qstats_backlog_dec(struct Qdisc *sch,
879 const struct sk_buff *skb)
880{
881 sch->qstats.backlog -= qdisc_pkt_len(skb);
882}
883
John Fastabend40bd0362017-12-07 09:55:07 -0800884static inline void qdisc_qstats_cpu_backlog_dec(struct Qdisc *sch,
885 const struct sk_buff *skb)
886{
887 this_cpu_sub(sch->cpu_qstats->backlog, qdisc_pkt_len(skb));
888}
889
John Fastabend25331d62014-09-28 11:53:29 -0700890static inline void qdisc_qstats_backlog_inc(struct Qdisc *sch,
891 const struct sk_buff *skb)
892{
893 sch->qstats.backlog += qdisc_pkt_len(skb);
894}
895
John Fastabend40bd0362017-12-07 09:55:07 -0800896static inline void qdisc_qstats_cpu_backlog_inc(struct Qdisc *sch,
897 const struct sk_buff *skb)
898{
899 this_cpu_add(sch->cpu_qstats->backlog, qdisc_pkt_len(skb));
900}
901
Paolo Abeni73eb6282019-04-10 14:32:41 +0200902static inline void qdisc_qstats_cpu_qlen_inc(struct Qdisc *sch)
John Fastabend40bd0362017-12-07 09:55:07 -0800903{
Paolo Abeni73eb6282019-04-10 14:32:41 +0200904 this_cpu_inc(sch->cpu_qstats->qlen);
John Fastabend40bd0362017-12-07 09:55:07 -0800905}
906
Paolo Abeni73eb6282019-04-10 14:32:41 +0200907static inline void qdisc_qstats_cpu_qlen_dec(struct Qdisc *sch)
John Fastabend40bd0362017-12-07 09:55:07 -0800908{
Paolo Abeni73eb6282019-04-10 14:32:41 +0200909 this_cpu_dec(sch->cpu_qstats->qlen);
John Fastabend40bd0362017-12-07 09:55:07 -0800910}
911
912static inline void qdisc_qstats_cpu_requeues_inc(struct Qdisc *sch)
913{
914 this_cpu_inc(sch->cpu_qstats->requeues);
915}
916
John Fastabend25331d62014-09-28 11:53:29 -0700917static inline void __qdisc_qstats_drop(struct Qdisc *sch, int count)
918{
919 sch->qstats.drops += count;
920}
921
Eric Dumazet24ea5912015-07-06 05:18:03 -0700922static inline void qstats_drop_inc(struct gnet_stats_queue *qstats)
John Fastabend25331d62014-09-28 11:53:29 -0700923{
Eric Dumazet24ea5912015-07-06 05:18:03 -0700924 qstats->drops++;
John Fastabend25331d62014-09-28 11:53:29 -0700925}
926
Eric Dumazet24ea5912015-07-06 05:18:03 -0700927static inline void qstats_overlimit_inc(struct gnet_stats_queue *qstats)
John Fastabendb0ab6f92014-09-28 11:54:24 -0700928{
Eric Dumazet24ea5912015-07-06 05:18:03 -0700929 qstats->overlimits++;
930}
John Fastabendb0ab6f92014-09-28 11:54:24 -0700931
Eric Dumazet24ea5912015-07-06 05:18:03 -0700932static inline void qdisc_qstats_drop(struct Qdisc *sch)
933{
934 qstats_drop_inc(&sch->qstats);
935}
936
937static inline void qdisc_qstats_cpu_drop(struct Qdisc *sch)
938{
Eric Dumazeteb60a8d2016-08-24 10:23:34 -0700939 this_cpu_inc(sch->cpu_qstats->drops);
John Fastabendb0ab6f92014-09-28 11:54:24 -0700940}
941
John Fastabend25331d62014-09-28 11:53:29 -0700942static inline void qdisc_qstats_overlimit(struct Qdisc *sch)
943{
944 sch->qstats.overlimits++;
945}
946
Paolo Abeni5dd431b2019-03-28 16:53:12 +0100947static inline int qdisc_qstats_copy(struct gnet_dump *d, struct Qdisc *sch)
948{
949 __u32 qlen = qdisc_qlen_sum(sch);
950
951 return gnet_stats_copy_queue(d, sch->cpu_qstats, &sch->qstats, qlen);
952}
953
954static inline void qdisc_qstats_qlen_backlog(struct Qdisc *sch, __u32 *qlen,
955 __u32 *backlog)
956{
957 struct gnet_stats_queue qstats = { 0 };
Paolo Abeni5dd431b2019-03-28 16:53:12 +0100958
Sebastian Andrzej Siewior10940eb2021-10-16 10:49:05 +0200959 gnet_stats_add_queue(&qstats, sch->cpu_qstats, &sch->qstats);
960 *qlen = qstats.qlen + qdisc_qlen(sch);
Paolo Abeni5dd431b2019-03-28 16:53:12 +0100961 *backlog = qstats.backlog;
962}
963
Paolo Abenie5f0e8f2019-03-28 16:53:13 +0100964static inline void qdisc_tree_flush_backlog(struct Qdisc *sch)
965{
966 __u32 qlen, backlog;
967
968 qdisc_qstats_qlen_backlog(sch, &qlen, &backlog);
969 qdisc_tree_reduce_backlog(sch, qlen, backlog);
970}
971
972static inline void qdisc_purge_queue(struct Qdisc *sch)
973{
974 __u32 qlen, backlog;
975
976 qdisc_qstats_qlen_backlog(sch, &qlen, &backlog);
977 qdisc_reset(sch);
978 qdisc_tree_reduce_backlog(sch, qlen, backlog);
979}
980
Florian Westphal48da34b2016-09-18 00:57:34 +0200981static inline void qdisc_skb_head_init(struct qdisc_skb_head *qh)
Thomas Graf9972b252005-06-18 22:57:26 -0700982{
Florian Westphal48da34b2016-09-18 00:57:34 +0200983 qh->head = NULL;
984 qh->tail = NULL;
985 qh->qlen = 0;
986}
987
David S. Milleraea890b2018-07-29 16:22:13 -0700988static inline void __qdisc_enqueue_tail(struct sk_buff *skb,
989 struct qdisc_skb_head *qh)
Florian Westphal48da34b2016-09-18 00:57:34 +0200990{
991 struct sk_buff *last = qh->tail;
992
993 if (last) {
994 skb->next = NULL;
995 last->next = skb;
996 qh->tail = skb;
997 } else {
998 qh->tail = skb;
999 qh->head = skb;
1000 }
1001 qh->qlen++;
Thomas Graf9972b252005-06-18 22:57:26 -07001002}
1003
1004static inline int qdisc_enqueue_tail(struct sk_buff *skb, struct Qdisc *sch)
1005{
David S. Milleraea890b2018-07-29 16:22:13 -07001006 __qdisc_enqueue_tail(skb, &sch->q);
1007 qdisc_qstats_backlog_inc(sch, skb);
1008 return NET_XMIT_SUCCESS;
Thomas Graf9972b252005-06-18 22:57:26 -07001009}
1010
David S. Miller59697732018-07-29 16:33:28 -07001011static inline void __qdisc_enqueue_head(struct sk_buff *skb,
1012 struct qdisc_skb_head *qh)
1013{
1014 skb->next = qh->head;
1015
1016 if (!qh->head)
1017 qh->tail = skb;
1018 qh->head = skb;
1019 qh->qlen++;
1020}
1021
Florian Westphal48da34b2016-09-18 00:57:34 +02001022static inline struct sk_buff *__qdisc_dequeue_head(struct qdisc_skb_head *qh)
Thomas Graf9972b252005-06-18 22:57:26 -07001023{
Florian Westphal48da34b2016-09-18 00:57:34 +02001024 struct sk_buff *skb = qh->head;
1025
1026 if (likely(skb != NULL)) {
1027 qh->head = skb->next;
1028 qh->qlen--;
1029 if (qh->head == NULL)
1030 qh->tail = NULL;
1031 skb->next = NULL;
1032 }
Thomas Graf9972b252005-06-18 22:57:26 -07001033
Florian Westphalec323362016-09-18 00:57:32 +02001034 return skb;
1035}
1036
1037static inline struct sk_buff *qdisc_dequeue_head(struct Qdisc *sch)
1038{
1039 struct sk_buff *skb = __qdisc_dequeue_head(&sch->q);
1040
Eric Dumazet9190b3b2011-01-20 23:31:33 -08001041 if (likely(skb != NULL)) {
John Fastabend25331d62014-09-28 11:53:29 -07001042 qdisc_qstats_backlog_dec(sch, skb);
Eric Dumazet9190b3b2011-01-20 23:31:33 -08001043 qdisc_bstats_update(sch, skb);
1044 }
Thomas Graf9972b252005-06-18 22:57:26 -07001045
1046 return skb;
1047}
1048
Eric Dumazet520ac302016-06-21 23:16:49 -07001049/* Instead of calling kfree_skb() while root qdisc lock is held,
1050 * queue the skb for future freeing at end of __dev_xmit_skb()
1051 */
1052static inline void __qdisc_drop(struct sk_buff *skb, struct sk_buff **to_free)
1053{
1054 skb->next = *to_free;
1055 *to_free = skb;
1056}
1057
Alexey Kodanev35d889d2018-03-05 20:52:54 +03001058static inline void __qdisc_drop_all(struct sk_buff *skb,
1059 struct sk_buff **to_free)
1060{
1061 if (skb->prev)
1062 skb->prev->next = *to_free;
1063 else
1064 skb->next = *to_free;
1065 *to_free = skb;
1066}
1067
Hagen Paul Pfeifer57dbb2d2010-01-24 12:30:59 +00001068static inline unsigned int __qdisc_queue_drop_head(struct Qdisc *sch,
Florian Westphal48da34b2016-09-18 00:57:34 +02001069 struct qdisc_skb_head *qh,
Eric Dumazet520ac302016-06-21 23:16:49 -07001070 struct sk_buff **to_free)
Hagen Paul Pfeifer57dbb2d2010-01-24 12:30:59 +00001071{
Florian Westphal48da34b2016-09-18 00:57:34 +02001072 struct sk_buff *skb = __qdisc_dequeue_head(qh);
Hagen Paul Pfeifer57dbb2d2010-01-24 12:30:59 +00001073
1074 if (likely(skb != NULL)) {
1075 unsigned int len = qdisc_pkt_len(skb);
Eric Dumazet520ac302016-06-21 23:16:49 -07001076
John Fastabend25331d62014-09-28 11:53:29 -07001077 qdisc_qstats_backlog_dec(sch, skb);
Eric Dumazet520ac302016-06-21 23:16:49 -07001078 __qdisc_drop(skb, to_free);
Hagen Paul Pfeifer57dbb2d2010-01-24 12:30:59 +00001079 return len;
1080 }
1081
1082 return 0;
1083}
1084
Patrick McHardy48a8f512008-10-31 00:44:18 -07001085static inline struct sk_buff *qdisc_peek_head(struct Qdisc *sch)
1086{
Florian Westphal48da34b2016-09-18 00:57:34 +02001087 const struct qdisc_skb_head *qh = &sch->q;
1088
1089 return qh->head;
Patrick McHardy48a8f512008-10-31 00:44:18 -07001090}
1091
Jarek Poplawski77be1552008-10-31 00:47:01 -07001092/* generic pseudo peek method for non-work-conserving qdisc */
1093static inline struct sk_buff *qdisc_peek_dequeued(struct Qdisc *sch)
1094{
John Fastabenda53851e2017-12-07 09:55:45 -08001095 struct sk_buff *skb = skb_peek(&sch->gso_skb);
1096
Jarek Poplawski77be1552008-10-31 00:47:01 -07001097 /* we can reuse ->gso_skb because peek isn't called for root qdiscs */
John Fastabenda53851e2017-12-07 09:55:45 -08001098 if (!skb) {
1099 skb = sch->dequeue(sch);
1100
1101 if (skb) {
1102 __skb_queue_head(&sch->gso_skb, skb);
Jarek Poplawski61c9eaf2008-11-05 16:02:34 -08001103 /* it's still part of the queue */
John Fastabenda53851e2017-12-07 09:55:45 -08001104 qdisc_qstats_backlog_inc(sch, skb);
Jarek Poplawski61c9eaf2008-11-05 16:02:34 -08001105 sch->q.qlen++;
WANG Conga27758f2016-06-03 15:05:57 -07001106 }
Jarek Poplawski61c9eaf2008-11-05 16:02:34 -08001107 }
Jarek Poplawski77be1552008-10-31 00:47:01 -07001108
John Fastabenda53851e2017-12-07 09:55:45 -08001109 return skb;
Jarek Poplawski77be1552008-10-31 00:47:01 -07001110}
1111
Paolo Abeni8a53e612019-04-10 14:32:40 +02001112static inline void qdisc_update_stats_at_dequeue(struct Qdisc *sch,
1113 struct sk_buff *skb)
1114{
1115 if (qdisc_is_percpu_stats(sch)) {
1116 qdisc_qstats_cpu_backlog_dec(sch, skb);
1117 qdisc_bstats_cpu_update(sch, skb);
Paolo Abeni73eb6282019-04-10 14:32:41 +02001118 qdisc_qstats_cpu_qlen_dec(sch);
Paolo Abeni8a53e612019-04-10 14:32:40 +02001119 } else {
1120 qdisc_qstats_backlog_dec(sch, skb);
1121 qdisc_bstats_update(sch, skb);
1122 sch->q.qlen--;
1123 }
1124}
1125
1126static inline void qdisc_update_stats_at_enqueue(struct Qdisc *sch,
1127 unsigned int pkt_len)
1128{
1129 if (qdisc_is_percpu_stats(sch)) {
Paolo Abeni73eb6282019-04-10 14:32:41 +02001130 qdisc_qstats_cpu_qlen_inc(sch);
Paolo Abeni8a53e612019-04-10 14:32:40 +02001131 this_cpu_add(sch->cpu_qstats->backlog, pkt_len);
1132 } else {
1133 sch->qstats.backlog += pkt_len;
1134 sch->q.qlen++;
1135 }
1136}
1137
Jarek Poplawski77be1552008-10-31 00:47:01 -07001138/* use instead of qdisc->dequeue() for all qdiscs queried with ->peek() */
1139static inline struct sk_buff *qdisc_dequeue_peeked(struct Qdisc *sch)
1140{
John Fastabenda53851e2017-12-07 09:55:45 -08001141 struct sk_buff *skb = skb_peek(&sch->gso_skb);
Jarek Poplawski77be1552008-10-31 00:47:01 -07001142
Jarek Poplawski61c9eaf2008-11-05 16:02:34 -08001143 if (skb) {
John Fastabenda53851e2017-12-07 09:55:45 -08001144 skb = __skb_dequeue(&sch->gso_skb);
Paolo Abeni9c01c9f2019-04-10 14:32:39 +02001145 if (qdisc_is_percpu_stats(sch)) {
1146 qdisc_qstats_cpu_backlog_dec(sch, skb);
Paolo Abeni73eb6282019-04-10 14:32:41 +02001147 qdisc_qstats_cpu_qlen_dec(sch);
Paolo Abeni9c01c9f2019-04-10 14:32:39 +02001148 } else {
1149 qdisc_qstats_backlog_dec(sch, skb);
1150 sch->q.qlen--;
1151 }
Jarek Poplawski61c9eaf2008-11-05 16:02:34 -08001152 } else {
Jarek Poplawski77be1552008-10-31 00:47:01 -07001153 skb = sch->dequeue(sch);
Jarek Poplawski61c9eaf2008-11-05 16:02:34 -08001154 }
Jarek Poplawski77be1552008-10-31 00:47:01 -07001155
1156 return skb;
1157}
1158
Florian Westphal48da34b2016-09-18 00:57:34 +02001159static inline void __qdisc_reset_queue(struct qdisc_skb_head *qh)
Thomas Graf9972b252005-06-18 22:57:26 -07001160{
1161 /*
1162 * We do not know the backlog in bytes of this list, it
1163 * is up to the caller to correct it
1164 */
Florian Westphal48da34b2016-09-18 00:57:34 +02001165 ASSERT_RTNL();
1166 if (qh->qlen) {
1167 rtnl_kfree_skbs(qh->head, qh->tail);
1168
1169 qh->head = NULL;
1170 qh->tail = NULL;
1171 qh->qlen = 0;
Eric Dumazet1b5c5492016-06-13 20:21:50 -07001172 }
Thomas Graf9972b252005-06-18 22:57:26 -07001173}
1174
1175static inline void qdisc_reset_queue(struct Qdisc *sch)
1176{
Eric Dumazet1b5c5492016-06-13 20:21:50 -07001177 __qdisc_reset_queue(&sch->q);
Thomas Graf9972b252005-06-18 22:57:26 -07001178 sch->qstats.backlog = 0;
1179}
1180
WANG Cong86a79962016-02-25 14:55:00 -08001181static inline struct Qdisc *qdisc_replace(struct Qdisc *sch, struct Qdisc *new,
1182 struct Qdisc **pold)
1183{
1184 struct Qdisc *old;
1185
1186 sch_tree_lock(sch);
1187 old = *pold;
1188 *pold = new;
Paolo Abenie5f0e8f2019-03-28 16:53:13 +01001189 if (old != NULL)
Alexander Ovechkin938e0fc2021-02-01 23:00:49 +03001190 qdisc_purge_queue(old);
WANG Cong86a79962016-02-25 14:55:00 -08001191 sch_tree_unlock(sch);
1192
1193 return old;
1194}
1195
Eric Dumazet1b5c5492016-06-13 20:21:50 -07001196static inline void rtnl_qdisc_drop(struct sk_buff *skb, struct Qdisc *sch)
1197{
1198 rtnl_kfree_skbs(skb, skb);
1199 qdisc_qstats_drop(sch);
1200}
1201
John Fastabend40bd0362017-12-07 09:55:07 -08001202static inline int qdisc_drop_cpu(struct sk_buff *skb, struct Qdisc *sch,
1203 struct sk_buff **to_free)
1204{
1205 __qdisc_drop(skb, to_free);
1206 qdisc_qstats_cpu_drop(sch);
1207
1208 return NET_XMIT_DROP;
1209}
Eric Dumazet520ac302016-06-21 23:16:49 -07001210
1211static inline int qdisc_drop(struct sk_buff *skb, struct Qdisc *sch,
1212 struct sk_buff **to_free)
Thomas Graf9972b252005-06-18 22:57:26 -07001213{
Eric Dumazet520ac302016-06-21 23:16:49 -07001214 __qdisc_drop(skb, to_free);
John Fastabend25331d62014-09-28 11:53:29 -07001215 qdisc_qstats_drop(sch);
Thomas Graf9972b252005-06-18 22:57:26 -07001216
1217 return NET_XMIT_DROP;
1218}
1219
Alexey Kodanev35d889d2018-03-05 20:52:54 +03001220static inline int qdisc_drop_all(struct sk_buff *skb, struct Qdisc *sch,
1221 struct sk_buff **to_free)
1222{
1223 __qdisc_drop_all(skb, to_free);
1224 qdisc_qstats_drop(sch);
1225
1226 return NET_XMIT_DROP;
1227}
1228
Jesper Dangaard Brouere9bef552007-09-12 16:35:24 +02001229/* Length to Time (L2T) lookup in a qdisc_rate_table, to determine how
1230 long it will take to send a packet given its size.
1231 */
1232static inline u32 qdisc_l2t(struct qdisc_rate_table* rtab, unsigned int pktlen)
1233{
Jesper Dangaard Brouere08b0992007-09-12 16:36:28 +02001234 int slot = pktlen + rtab->rate.cell_align + rtab->rate.overhead;
1235 if (slot < 0)
1236 slot = 0;
Jesper Dangaard Brouere9bef552007-09-12 16:35:24 +02001237 slot >>= rtab->rate.cell_log;
1238 if (slot > 255)
Eric Dumazeta02cec22010-09-22 20:43:57 +00001239 return rtab->data[255]*(slot >> 8) + rtab->data[slot & 0xFF];
Jesper Dangaard Brouere9bef552007-09-12 16:35:24 +02001240 return rtab->data[slot];
1241}
1242
Jiri Pirko292f1c72013-02-12 00:12:03 +00001243struct psched_ratecfg {
Eric Dumazet130d3d62013-06-06 13:56:19 -07001244 u64 rate_bytes_ps; /* bytes per second */
Eric Dumazet01cb71d2013-06-02 13:55:05 +00001245 u32 mult;
1246 u16 overhead;
Kevin Braceyfb804452022-01-12 19:02:10 +02001247 u16 mpu;
Jesper Dangaard Brouer8a8e3d82013-08-14 23:47:11 +02001248 u8 linklayer;
Eric Dumazet01cb71d2013-06-02 13:55:05 +00001249 u8 shift;
Jiri Pirko292f1c72013-02-12 00:12:03 +00001250};
1251
1252static inline u64 psched_l2t_ns(const struct psched_ratecfg *r,
1253 unsigned int len)
1254{
Jesper Dangaard Brouer8a8e3d82013-08-14 23:47:11 +02001255 len += r->overhead;
1256
Kevin Braceyfb804452022-01-12 19:02:10 +02001257 if (len < r->mpu)
1258 len = r->mpu;
1259
Jesper Dangaard Brouer8a8e3d82013-08-14 23:47:11 +02001260 if (unlikely(r->linklayer == TC_LINKLAYER_ATM))
1261 return ((u64)(DIV_ROUND_UP(len,48)*53) * r->mult) >> r->shift;
1262
1263 return ((u64)len * r->mult) >> r->shift;
Jiri Pirko292f1c72013-02-12 00:12:03 +00001264}
1265
Joe Perches5c152572013-07-30 22:47:13 -07001266void psched_ratecfg_precompute(struct psched_ratecfg *r,
Eric Dumazet3e1e3aa2013-09-19 09:10:03 -07001267 const struct tc_ratespec *conf,
1268 u64 rate64);
Jiri Pirko292f1c72013-02-12 00:12:03 +00001269
Eric Dumazet01cb71d2013-06-02 13:55:05 +00001270static inline void psched_ratecfg_getrate(struct tc_ratespec *res,
1271 const struct psched_ratecfg *r)
Jiri Pirko292f1c72013-02-12 00:12:03 +00001272{
Eric Dumazet01cb71d2013-06-02 13:55:05 +00001273 memset(res, 0, sizeof(*res));
Eric Dumazet3e1e3aa2013-09-19 09:10:03 -07001274
1275 /* legacy struct tc_ratespec has a 32bit @rate field
1276 * Qdisc using 64bit rate should add new attributes
1277 * in order to maintain compatibility.
1278 */
1279 res->rate = min_t(u64, r->rate_bytes_ps, ~0U);
1280
Eric Dumazet01cb71d2013-06-02 13:55:05 +00001281 res->overhead = r->overhead;
Kevin Braceyfb804452022-01-12 19:02:10 +02001282 res->mpu = r->mpu;
Jesper Dangaard Brouer8a8e3d82013-08-14 23:47:11 +02001283 res->linklayer = (r->linklayer & TC_LINKLAYER_MASK);
Jiri Pirko292f1c72013-02-12 00:12:03 +00001284}
1285
Baowen Zheng2ffe0392021-03-12 15:08:31 +01001286struct psched_pktrate {
1287 u64 rate_pkts_ps; /* packets per second */
1288 u32 mult;
1289 u8 shift;
1290};
1291
1292static inline u64 psched_pkt2t_ns(const struct psched_pktrate *r,
1293 unsigned int pkt_num)
1294{
1295 return ((u64)pkt_num * r->mult) >> r->shift;
1296}
1297
1298void psched_ppscfg_precompute(struct psched_pktrate *r, u64 pktrate64);
1299
Jiri Pirko46209402017-11-03 11:46:25 +01001300/* Mini Qdisc serves for specific needs of ingress/clsact Qdisc.
1301 * The fast path only needs to access filter list and to update stats
1302 */
1303struct mini_Qdisc {
1304 struct tcf_proto *filter_list;
Paul Blakey7d17c542020-02-16 12:01:22 +02001305 struct tcf_block *block;
Ahmed S. Darwish50dc9a82021-10-16 10:49:09 +02001306 struct gnet_stats_basic_sync __percpu *cpu_bstats;
Jiri Pirko46209402017-11-03 11:46:25 +01001307 struct gnet_stats_queue __percpu *cpu_qstats;
Seth Forshee26746382021-10-26 08:06:59 -05001308 unsigned long rcu_state;
Jiri Pirko46209402017-11-03 11:46:25 +01001309};
1310
1311static inline void mini_qdisc_bstats_cpu_update(struct mini_Qdisc *miniq,
1312 const struct sk_buff *skb)
1313{
Ahmed S. Darwish50dc9a82021-10-16 10:49:09 +02001314 bstats_update(this_cpu_ptr(miniq->cpu_bstats), skb);
Jiri Pirko46209402017-11-03 11:46:25 +01001315}
1316
1317static inline void mini_qdisc_qstats_cpu_drop(struct mini_Qdisc *miniq)
1318{
1319 this_cpu_inc(miniq->cpu_qstats->drops);
1320}
1321
1322struct mini_Qdisc_pair {
1323 struct mini_Qdisc miniq1;
1324 struct mini_Qdisc miniq2;
1325 struct mini_Qdisc __rcu **p_miniq;
1326};
1327
1328void mini_qdisc_pair_swap(struct mini_Qdisc_pair *miniqp,
1329 struct tcf_proto *tp_head);
1330void mini_qdisc_pair_init(struct mini_Qdisc_pair *miniqp, struct Qdisc *qdisc,
1331 struct mini_Qdisc __rcu **p_miniq);
Paul Blakey7d17c542020-02-16 12:01:22 +02001332void mini_qdisc_pair_block_init(struct mini_Qdisc_pair *miniqp,
1333 struct tcf_block *block);
Jiri Pirko46209402017-11-03 11:46:25 +01001334
Jakub Kicinskif7116fb2021-09-17 06:55:06 -07001335void mq_change_real_num_tx(struct Qdisc *sch, unsigned int new_real_tx);
1336
wenxuc1294122020-11-25 12:01:23 +08001337int sch_frag_xmit_hook(struct sk_buff *skb, int (*xmit)(struct sk_buff *skb));
Paolo Abenicd11b1642018-07-30 14:30:44 +02001338
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339#endif