blob: 1042c449e7db554e32389213bb5ca1dcdf353a0c [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef __NET_SCHED_GENERIC_H
3#define __NET_SCHED_GENERIC_H
4
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>
David Brazdil0f672f62019-12-10 10:32:29 +000015#include <linux/mutex.h>
16#include <linux/rwsem.h>
17#include <linux/atomic.h>
18#include <linux/hashtable.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000019#include <net/gen_stats.h>
20#include <net/rtnetlink.h>
David Brazdil0f672f62019-12-10 10:32:29 +000021#include <net/flow_offload.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000022
23struct Qdisc_ops;
24struct qdisc_walker;
25struct tcf_walker;
26struct module;
David Brazdil0f672f62019-12-10 10:32:29 +000027struct bpf_flow_keys;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000028
29struct qdisc_rate_table {
30 struct tc_ratespec rate;
31 u32 data[256];
32 struct qdisc_rate_table *next;
33 int refcnt;
34};
35
36enum qdisc_state_t {
37 __QDISC_STATE_SCHED,
38 __QDISC_STATE_DEACTIVATED,
Olivier Deprez0e641232021-09-23 10:07:05 +020039 __QDISC_STATE_MISSED,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000040};
41
42struct qdisc_size_table {
43 struct rcu_head rcu;
44 struct list_head list;
45 struct tc_sizespec szopts;
46 int refcnt;
47 u16 data[];
48};
49
50/* similar to sk_buff_head, but skb->prev pointer is undefined. */
51struct qdisc_skb_head {
52 struct sk_buff *head;
53 struct sk_buff *tail;
54 __u32 qlen;
55 spinlock_t lock;
56};
57
58struct Qdisc {
59 int (*enqueue)(struct sk_buff *skb,
60 struct Qdisc *sch,
61 struct sk_buff **to_free);
62 struct sk_buff * (*dequeue)(struct Qdisc *sch);
63 unsigned int flags;
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
73 * multiqueue device.
74 */
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.
79 */
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 */
83 u32 limit;
84 const struct Qdisc_ops *ops;
85 struct qdisc_size_table __rcu *stab;
86 struct hlist_node hash;
87 u32 handle;
88 u32 parent;
89
90 struct netdev_queue *dev_queue;
91
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;
Olivier Deprez157378f2022-04-04 15:47:50 +020095 int pad;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000096 refcount_t refcnt;
97
98 /*
99 * For performance sake on SMP, we put highly modified fields at the end
100 */
101 struct sk_buff_head gso_skb ____cacheline_aligned_in_smp;
102 struct qdisc_skb_head q;
103 struct gnet_stats_basic_packed bstats;
104 seqcount_t running;
105 struct gnet_stats_queue qstats;
106 unsigned long state;
107 struct Qdisc *next_sched;
108 struct sk_buff_head skb_bad_txq;
109
110 spinlock_t busylock ____cacheline_aligned_in_smp;
111 spinlock_t seqlock;
David Brazdil0f672f62019-12-10 10:32:29 +0000112
113 /* for NOLOCK qdisc, true if there are no enqueued skbs */
114 bool empty;
115 struct rcu_head rcu;
Olivier Deprez157378f2022-04-04 15:47:50 +0200116
117 /* private data */
118 long privdata[] ____cacheline_aligned;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000119};
120
121static inline void qdisc_refcount_inc(struct Qdisc *qdisc)
122{
123 if (qdisc->flags & TCQ_F_BUILTIN)
124 return;
125 refcount_inc(&qdisc->refcnt);
126}
127
David Brazdil0f672f62019-12-10 10:32:29 +0000128/* Intended to be used by unlocked users, when concurrent qdisc release is
129 * possible.
130 */
131
132static inline struct Qdisc *qdisc_refcount_inc_nz(struct Qdisc *qdisc)
133{
134 if (qdisc->flags & TCQ_F_BUILTIN)
135 return qdisc;
136 if (refcount_inc_not_zero(&qdisc->refcnt))
137 return qdisc;
138 return NULL;
139}
140
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000141static inline bool qdisc_is_running(struct Qdisc *qdisc)
142{
143 if (qdisc->flags & TCQ_F_NOLOCK)
144 return spin_is_locked(&qdisc->seqlock);
145 return (raw_read_seqcount(&qdisc->running) & 1) ? true : false;
146}
147
David Brazdil0f672f62019-12-10 10:32:29 +0000148static inline bool qdisc_is_percpu_stats(const struct Qdisc *q)
149{
150 return q->flags & TCQ_F_CPUSTATS;
151}
152
153static inline bool qdisc_is_empty(const struct Qdisc *qdisc)
154{
155 if (qdisc_is_percpu_stats(qdisc))
Olivier Deprez0e641232021-09-23 10:07:05 +0200156 return READ_ONCE(qdisc->empty);
157 return !READ_ONCE(qdisc->q.qlen);
David Brazdil0f672f62019-12-10 10:32:29 +0000158}
159
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000160static inline bool qdisc_run_begin(struct Qdisc *qdisc)
161{
162 if (qdisc->flags & TCQ_F_NOLOCK) {
Olivier Deprez0e641232021-09-23 10:07:05 +0200163 if (spin_trylock(&qdisc->seqlock))
164 goto nolock_empty;
165
166 /* Paired with smp_mb__after_atomic() to make sure
167 * STATE_MISSED checking is synchronized with clearing
168 * in pfifo_fast_dequeue().
169 */
170 smp_mb__before_atomic();
171
172 /* If the MISSED flag is set, it means other thread has
173 * set the MISSED flag before second spin_trylock(), so
174 * we can return false here to avoid multi cpus doing
175 * the set_bit() and second spin_trylock() concurrently.
176 */
177 if (test_bit(__QDISC_STATE_MISSED, &qdisc->state))
178 return false;
179
180 /* Set the MISSED flag before the second spin_trylock(),
181 * if the second spin_trylock() return false, it means
182 * other cpu holding the lock will do dequeuing for us
183 * or it will see the MISSED flag set after releasing
184 * lock and reschedule the net_tx_action() to do the
185 * dequeuing.
186 */
187 set_bit(__QDISC_STATE_MISSED, &qdisc->state);
188
189 /* spin_trylock() only has load-acquire semantic, so use
190 * smp_mb__after_atomic() to ensure STATE_MISSED is set
191 * before doing the second spin_trylock().
192 */
193 smp_mb__after_atomic();
194
195 /* Retry again in case other CPU may not see the new flag
196 * after it releases the lock at the end of qdisc_run_end().
197 */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000198 if (!spin_trylock(&qdisc->seqlock))
199 return false;
Olivier Deprez0e641232021-09-23 10:07:05 +0200200
201nolock_empty:
202 WRITE_ONCE(qdisc->empty, false);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000203 } else if (qdisc_is_running(qdisc)) {
204 return false;
205 }
206 /* Variant of write_seqcount_begin() telling lockdep a trylock
207 * was attempted.
208 */
209 raw_write_seqcount_begin(&qdisc->running);
210 seqcount_acquire(&qdisc->running.dep_map, 0, 1, _RET_IP_);
211 return true;
212}
213
214static inline void qdisc_run_end(struct Qdisc *qdisc)
215{
216 write_seqcount_end(&qdisc->running);
Olivier Deprez0e641232021-09-23 10:07:05 +0200217 if (qdisc->flags & TCQ_F_NOLOCK) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000218 spin_unlock(&qdisc->seqlock);
Olivier Deprez0e641232021-09-23 10:07:05 +0200219
220 if (unlikely(test_bit(__QDISC_STATE_MISSED,
221 &qdisc->state))) {
222 clear_bit(__QDISC_STATE_MISSED, &qdisc->state);
223 __netif_schedule(qdisc);
224 }
225 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000226}
227
228static inline bool qdisc_may_bulk(const struct Qdisc *qdisc)
229{
230 return qdisc->flags & TCQ_F_ONETXQUEUE;
231}
232
233static inline int qdisc_avail_bulklimit(const struct netdev_queue *txq)
234{
235#ifdef CONFIG_BQL
236 /* Non-BQL migrated drivers will return 0, too. */
237 return dql_avail(&txq->dql);
238#else
239 return 0;
240#endif
241}
242
243struct Qdisc_class_ops {
David Brazdil0f672f62019-12-10 10:32:29 +0000244 unsigned int flags;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000245 /* Child qdisc manipulation */
246 struct netdev_queue * (*select_queue)(struct Qdisc *, struct tcmsg *);
247 int (*graft)(struct Qdisc *, unsigned long cl,
248 struct Qdisc *, struct Qdisc **,
249 struct netlink_ext_ack *extack);
250 struct Qdisc * (*leaf)(struct Qdisc *, unsigned long cl);
251 void (*qlen_notify)(struct Qdisc *, unsigned long);
252
253 /* Class manipulation routines */
254 unsigned long (*find)(struct Qdisc *, u32 classid);
255 int (*change)(struct Qdisc *, u32, u32,
256 struct nlattr **, unsigned long *,
257 struct netlink_ext_ack *);
258 int (*delete)(struct Qdisc *, unsigned long);
259 void (*walk)(struct Qdisc *, struct qdisc_walker * arg);
260
261 /* Filter manipulation */
262 struct tcf_block * (*tcf_block)(struct Qdisc *sch,
263 unsigned long arg,
264 struct netlink_ext_ack *extack);
265 unsigned long (*bind_tcf)(struct Qdisc *, unsigned long,
266 u32 classid);
267 void (*unbind_tcf)(struct Qdisc *, unsigned long);
268
269 /* rtnetlink specific */
270 int (*dump)(struct Qdisc *, unsigned long,
271 struct sk_buff *skb, struct tcmsg*);
272 int (*dump_stats)(struct Qdisc *, unsigned long,
273 struct gnet_dump *);
274};
275
David Brazdil0f672f62019-12-10 10:32:29 +0000276/* Qdisc_class_ops flag values */
277
278/* Implements API that doesn't require rtnl lock */
279enum qdisc_class_ops_flags {
280 QDISC_CLASS_OPS_DOIT_UNLOCKED = 1,
281};
282
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000283struct Qdisc_ops {
284 struct Qdisc_ops *next;
285 const struct Qdisc_class_ops *cl_ops;
286 char id[IFNAMSIZ];
287 int priv_size;
288 unsigned int static_flags;
289
290 int (*enqueue)(struct sk_buff *skb,
291 struct Qdisc *sch,
292 struct sk_buff **to_free);
293 struct sk_buff * (*dequeue)(struct Qdisc *);
294 struct sk_buff * (*peek)(struct Qdisc *);
295
296 int (*init)(struct Qdisc *sch, struct nlattr *arg,
297 struct netlink_ext_ack *extack);
298 void (*reset)(struct Qdisc *);
299 void (*destroy)(struct Qdisc *);
300 int (*change)(struct Qdisc *sch,
301 struct nlattr *arg,
302 struct netlink_ext_ack *extack);
303 void (*attach)(struct Qdisc *sch);
304 int (*change_tx_queue_len)(struct Qdisc *, unsigned int);
Olivier Deprez157378f2022-04-04 15:47:50 +0200305 void (*change_real_num_tx)(struct Qdisc *sch,
306 unsigned int new_real_tx);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000307
308 int (*dump)(struct Qdisc *, struct sk_buff *);
309 int (*dump_stats)(struct Qdisc *, struct gnet_dump *);
310
311 void (*ingress_block_set)(struct Qdisc *sch,
312 u32 block_index);
313 void (*egress_block_set)(struct Qdisc *sch,
314 u32 block_index);
315 u32 (*ingress_block_get)(struct Qdisc *sch);
316 u32 (*egress_block_get)(struct Qdisc *sch);
317
318 struct module *owner;
319};
320
321
322struct tcf_result {
323 union {
324 struct {
325 unsigned long class;
326 u32 classid;
327 };
328 const struct tcf_proto *goto_tp;
329
David Brazdil0f672f62019-12-10 10:32:29 +0000330 /* used in the skb_tc_reinsert function */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000331 struct {
332 bool ingress;
333 struct gnet_stats_queue *qstats;
334 };
335 };
336};
337
338struct tcf_chain;
339
340struct tcf_proto_ops {
341 struct list_head head;
342 char kind[IFNAMSIZ];
343
344 int (*classify)(struct sk_buff *,
345 const struct tcf_proto *,
346 struct tcf_result *);
347 int (*init)(struct tcf_proto*);
David Brazdil0f672f62019-12-10 10:32:29 +0000348 void (*destroy)(struct tcf_proto *tp, bool rtnl_held,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000349 struct netlink_ext_ack *extack);
350
351 void* (*get)(struct tcf_proto*, u32 handle);
David Brazdil0f672f62019-12-10 10:32:29 +0000352 void (*put)(struct tcf_proto *tp, void *f);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000353 int (*change)(struct net *net, struct sk_buff *,
354 struct tcf_proto*, unsigned long,
355 u32 handle, struct nlattr **,
David Brazdil0f672f62019-12-10 10:32:29 +0000356 void **, bool, bool,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000357 struct netlink_ext_ack *);
358 int (*delete)(struct tcf_proto *tp, void *arg,
David Brazdil0f672f62019-12-10 10:32:29 +0000359 bool *last, bool rtnl_held,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000360 struct netlink_ext_ack *);
Olivier Deprez0e641232021-09-23 10:07:05 +0200361 bool (*delete_empty)(struct tcf_proto *tp);
David Brazdil0f672f62019-12-10 10:32:29 +0000362 void (*walk)(struct tcf_proto *tp,
363 struct tcf_walker *arg, bool rtnl_held);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000364 int (*reoffload)(struct tcf_proto *tp, bool add,
David Brazdil0f672f62019-12-10 10:32:29 +0000365 flow_setup_cb_t *cb, void *cb_priv,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000366 struct netlink_ext_ack *extack);
David Brazdil0f672f62019-12-10 10:32:29 +0000367 void (*hw_add)(struct tcf_proto *tp,
368 void *type_data);
369 void (*hw_del)(struct tcf_proto *tp,
370 void *type_data);
Olivier Deprez0e641232021-09-23 10:07:05 +0200371 void (*bind_class)(void *, u32, unsigned long,
372 void *, unsigned long);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000373 void * (*tmplt_create)(struct net *net,
374 struct tcf_chain *chain,
375 struct nlattr **tca,
376 struct netlink_ext_ack *extack);
377 void (*tmplt_destroy)(void *tmplt_priv);
378
379 /* rtnetlink specific */
380 int (*dump)(struct net*, struct tcf_proto*, void *,
David Brazdil0f672f62019-12-10 10:32:29 +0000381 struct sk_buff *skb, struct tcmsg*,
382 bool);
Olivier Deprez157378f2022-04-04 15:47:50 +0200383 int (*terse_dump)(struct net *net,
384 struct tcf_proto *tp, void *fh,
385 struct sk_buff *skb,
386 struct tcmsg *t, bool rtnl_held);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000387 int (*tmplt_dump)(struct sk_buff *skb,
388 struct net *net,
389 void *tmplt_priv);
390
391 struct module *owner;
David Brazdil0f672f62019-12-10 10:32:29 +0000392 int flags;
393};
394
Olivier Deprez0e641232021-09-23 10:07:05 +0200395/* Classifiers setting TCF_PROTO_OPS_DOIT_UNLOCKED in tcf_proto_ops->flags
396 * are expected to implement tcf_proto_ops->delete_empty(), otherwise race
397 * conditions can occur when filters are inserted/deleted simultaneously.
398 */
David Brazdil0f672f62019-12-10 10:32:29 +0000399enum tcf_proto_ops_flags {
400 TCF_PROTO_OPS_DOIT_UNLOCKED = 1,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000401};
402
403struct tcf_proto {
404 /* Fast access part */
405 struct tcf_proto __rcu *next;
406 void __rcu *root;
407
408 /* called under RCU BH lock*/
409 int (*classify)(struct sk_buff *,
410 const struct tcf_proto *,
411 struct tcf_result *);
412 __be16 protocol;
413
414 /* All the rest */
415 u32 prio;
416 void *data;
417 const struct tcf_proto_ops *ops;
418 struct tcf_chain *chain;
David Brazdil0f672f62019-12-10 10:32:29 +0000419 /* Lock protects tcf_proto shared state and can be used by unlocked
420 * classifiers to protect their private data.
421 */
422 spinlock_t lock;
423 bool deleting;
424 refcount_t refcnt;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000425 struct rcu_head rcu;
David Brazdil0f672f62019-12-10 10:32:29 +0000426 struct hlist_node destroy_ht_node;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000427};
428
429struct qdisc_skb_cb {
David Brazdil0f672f62019-12-10 10:32:29 +0000430 struct {
431 unsigned int pkt_len;
432 u16 slave_dev_queue_mapping;
433 u16 tc_classid;
434 };
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000435#define QDISC_CB_PRIV_LEN 20
436 unsigned char data[QDISC_CB_PRIV_LEN];
Olivier Deprez157378f2022-04-04 15:47:50 +0200437 u16 mru;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000438};
439
440typedef void tcf_chain_head_change_t(struct tcf_proto *tp_head, void *priv);
441
442struct tcf_chain {
David Brazdil0f672f62019-12-10 10:32:29 +0000443 /* Protects filter_chain. */
444 struct mutex filter_chain_lock;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000445 struct tcf_proto __rcu *filter_chain;
446 struct list_head list;
447 struct tcf_block *block;
448 u32 index; /* chain index */
449 unsigned int refcnt;
450 unsigned int action_refcnt;
451 bool explicitly_created;
David Brazdil0f672f62019-12-10 10:32:29 +0000452 bool flushing;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000453 const struct tcf_proto_ops *tmplt_ops;
454 void *tmplt_priv;
David Brazdil0f672f62019-12-10 10:32:29 +0000455 struct rcu_head rcu;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000456};
457
458struct tcf_block {
David Brazdil0f672f62019-12-10 10:32:29 +0000459 /* Lock protects tcf_block and lifetime-management data of chains
460 * attached to the block (refcnt, action_refcnt, explicitly_created).
461 */
462 struct mutex lock;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000463 struct list_head chain_list;
464 u32 index; /* block index for shared blocks */
Olivier Deprez0e641232021-09-23 10:07:05 +0200465 u32 classid; /* which class this block belongs to */
David Brazdil0f672f62019-12-10 10:32:29 +0000466 refcount_t refcnt;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000467 struct net *net;
468 struct Qdisc *q;
David Brazdil0f672f62019-12-10 10:32:29 +0000469 struct rw_semaphore cb_lock; /* protects cb_list and offload counters */
470 struct flow_block flow_block;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000471 struct list_head owner_list;
472 bool keep_dst;
David Brazdil0f672f62019-12-10 10:32:29 +0000473 atomic_t offloadcnt; /* Number of oddloaded filters */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000474 unsigned int nooffloaddevcnt; /* Number of devs unable to do offload */
David Brazdil0f672f62019-12-10 10:32:29 +0000475 unsigned int lockeddevcnt; /* Number of devs that require rtnl lock. */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000476 struct {
477 struct tcf_chain *chain;
478 struct list_head filter_chain_list;
479 } chain0;
David Brazdil0f672f62019-12-10 10:32:29 +0000480 struct rcu_head rcu;
481 DECLARE_HASHTABLE(proto_destroy_ht, 7);
482 struct mutex proto_destroy_lock; /* Lock for proto_destroy hashtable. */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000483};
484
David Brazdil0f672f62019-12-10 10:32:29 +0000485#ifdef CONFIG_PROVE_LOCKING
486static inline bool lockdep_tcf_chain_is_locked(struct tcf_chain *chain)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000487{
David Brazdil0f672f62019-12-10 10:32:29 +0000488 return lockdep_is_held(&chain->filter_chain_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000489}
490
David Brazdil0f672f62019-12-10 10:32:29 +0000491static inline bool lockdep_tcf_proto_is_locked(struct tcf_proto *tp)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000492{
David Brazdil0f672f62019-12-10 10:32:29 +0000493 return lockdep_is_held(&tp->lock);
494}
495#else
496static inline bool lockdep_tcf_chain_is_locked(struct tcf_block *chain)
497{
498 return true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000499}
500
David Brazdil0f672f62019-12-10 10:32:29 +0000501static inline bool lockdep_tcf_proto_is_locked(struct tcf_proto *tp)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000502{
David Brazdil0f672f62019-12-10 10:32:29 +0000503 return true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000504}
David Brazdil0f672f62019-12-10 10:32:29 +0000505#endif /* #ifdef CONFIG_PROVE_LOCKING */
506
507#define tcf_chain_dereference(p, chain) \
508 rcu_dereference_protected(p, lockdep_tcf_chain_is_locked(chain))
509
510#define tcf_proto_dereference(p, tp) \
511 rcu_dereference_protected(p, lockdep_tcf_proto_is_locked(tp))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000512
513static inline void qdisc_cb_private_validate(const struct sk_buff *skb, int sz)
514{
515 struct qdisc_skb_cb *qcb;
516
Olivier Deprez157378f2022-04-04 15:47:50 +0200517 BUILD_BUG_ON(sizeof(skb->cb) < sizeof(*qcb));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000518 BUILD_BUG_ON(sizeof(qcb->data) < sz);
519}
520
521static inline int qdisc_qlen_cpu(const struct Qdisc *q)
522{
523 return this_cpu_ptr(q->cpu_qstats)->qlen;
524}
525
526static inline int qdisc_qlen(const struct Qdisc *q)
527{
528 return q->q.qlen;
529}
530
531static inline int qdisc_qlen_sum(const struct Qdisc *q)
532{
533 __u32 qlen = q->qstats.qlen;
534 int i;
535
David Brazdil0f672f62019-12-10 10:32:29 +0000536 if (qdisc_is_percpu_stats(q)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000537 for_each_possible_cpu(i)
538 qlen += per_cpu_ptr(q->cpu_qstats, i)->qlen;
539 } else {
540 qlen += q->q.qlen;
541 }
542
543 return qlen;
544}
545
546static inline struct qdisc_skb_cb *qdisc_skb_cb(const struct sk_buff *skb)
547{
548 return (struct qdisc_skb_cb *)skb->cb;
549}
550
551static inline spinlock_t *qdisc_lock(struct Qdisc *qdisc)
552{
553 return &qdisc->q.lock;
554}
555
556static inline struct Qdisc *qdisc_root(const struct Qdisc *qdisc)
557{
558 struct Qdisc *q = rcu_dereference_rtnl(qdisc->dev_queue->qdisc);
559
560 return q;
561}
562
David Brazdil0f672f62019-12-10 10:32:29 +0000563static inline struct Qdisc *qdisc_root_bh(const struct Qdisc *qdisc)
564{
565 return rcu_dereference_bh(qdisc->dev_queue->qdisc);
566}
567
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000568static inline struct Qdisc *qdisc_root_sleeping(const struct Qdisc *qdisc)
569{
570 return qdisc->dev_queue->qdisc_sleeping;
571}
572
573/* 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 */
584static inline spinlock_t *qdisc_root_lock(const struct Qdisc *qdisc)
585{
586 struct Qdisc *root = qdisc_root(qdisc);
587
588 ASSERT_RTNL();
589 return qdisc_lock(root);
590}
591
592static inline spinlock_t *qdisc_root_sleeping_lock(const struct Qdisc *qdisc)
593{
594 struct Qdisc *root = qdisc_root_sleeping(qdisc);
595
596 ASSERT_RTNL();
597 return qdisc_lock(root);
598}
599
600static inline seqcount_t *qdisc_root_sleeping_running(const struct Qdisc *qdisc)
601{
602 struct Qdisc *root = qdisc_root_sleeping(qdisc);
603
604 ASSERT_RTNL();
605 return &root->running;
606}
607
608static inline struct net_device *qdisc_dev(const struct Qdisc *qdisc)
609{
610 return qdisc->dev_queue->dev;
611}
612
613static inline void sch_tree_lock(const struct Qdisc *q)
614{
615 spin_lock_bh(qdisc_root_sleeping_lock(q));
616}
617
618static inline void sch_tree_unlock(const struct Qdisc *q)
619{
620 spin_unlock_bh(qdisc_root_sleeping_lock(q));
621}
622
623extern struct Qdisc noop_qdisc;
624extern struct Qdisc_ops noop_qdisc_ops;
625extern struct Qdisc_ops pfifo_fast_ops;
626extern struct Qdisc_ops mq_qdisc_ops;
627extern struct Qdisc_ops noqueue_qdisc_ops;
628extern const struct Qdisc_ops *default_qdisc_ops;
629static inline const struct Qdisc_ops *
630get_default_qdisc_ops(const struct net_device *dev, int ntx)
631{
632 return ntx < dev->real_num_tx_queues ?
633 default_qdisc_ops : &pfifo_fast_ops;
634}
635
636struct Qdisc_class_common {
637 u32 classid;
638 struct hlist_node hnode;
639};
640
641struct Qdisc_class_hash {
642 struct hlist_head *hash;
643 unsigned int hashsize;
644 unsigned int hashmask;
645 unsigned int hashelems;
646};
647
648static inline unsigned int qdisc_class_hash(u32 id, u32 mask)
649{
650 id ^= id >> 8;
651 id ^= id >> 4;
652 return id & mask;
653}
654
655static inline struct Qdisc_class_common *
656qdisc_class_find(const struct Qdisc_class_hash *hash, u32 id)
657{
658 struct Qdisc_class_common *cl;
659 unsigned int h;
660
661 if (!id)
662 return NULL;
663
664 h = qdisc_class_hash(id, hash->hashmask);
665 hlist_for_each_entry(cl, &hash->hash[h], hnode) {
666 if (cl->classid == id)
667 return cl;
668 }
669 return NULL;
670}
671
672static inline int tc_classid_to_hwtc(struct net_device *dev, u32 classid)
673{
674 u32 hwtc = TC_H_MIN(classid) - TC_H_MIN_PRIORITY;
675
676 return (hwtc < netdev_get_num_tc(dev)) ? hwtc : -EINVAL;
677}
678
679int qdisc_class_hash_init(struct Qdisc_class_hash *);
680void qdisc_class_hash_insert(struct Qdisc_class_hash *,
681 struct Qdisc_class_common *);
682void qdisc_class_hash_remove(struct Qdisc_class_hash *,
683 struct Qdisc_class_common *);
684void qdisc_class_hash_grow(struct Qdisc *, struct Qdisc_class_hash *);
685void qdisc_class_hash_destroy(struct Qdisc_class_hash *);
686
687int dev_qdisc_change_tx_queue_len(struct net_device *dev);
Olivier Deprez157378f2022-04-04 15:47:50 +0200688void dev_qdisc_change_real_num_tx(struct net_device *dev,
689 unsigned int new_real_tx);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000690void dev_init_scheduler(struct net_device *dev);
691void dev_shutdown(struct net_device *dev);
692void dev_activate(struct net_device *dev);
693void dev_deactivate(struct net_device *dev);
694void dev_deactivate_many(struct list_head *head);
695struct Qdisc *dev_graft_qdisc(struct netdev_queue *dev_queue,
696 struct Qdisc *qdisc);
697void qdisc_reset(struct Qdisc *qdisc);
David Brazdil0f672f62019-12-10 10:32:29 +0000698void qdisc_put(struct Qdisc *qdisc);
699void qdisc_put_unlocked(struct Qdisc *qdisc);
700void qdisc_tree_reduce_backlog(struct Qdisc *qdisc, int n, int len);
701#ifdef CONFIG_NET_SCHED
702int qdisc_offload_dump_helper(struct Qdisc *q, enum tc_setup_type type,
703 void *type_data);
704void qdisc_offload_graft_helper(struct net_device *dev, struct Qdisc *sch,
705 struct Qdisc *new, struct Qdisc *old,
706 enum tc_setup_type type, void *type_data,
707 struct netlink_ext_ack *extack);
708#else
709static inline int
710qdisc_offload_dump_helper(struct Qdisc *q, enum tc_setup_type type,
711 void *type_data)
712{
713 q->flags &= ~TCQ_F_OFFLOADED;
714 return 0;
715}
716
717static inline void
718qdisc_offload_graft_helper(struct net_device *dev, struct Qdisc *sch,
719 struct Qdisc *new, struct Qdisc *old,
720 enum tc_setup_type type, void *type_data,
721 struct netlink_ext_ack *extack)
722{
723}
724#endif
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000725struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue,
726 const struct Qdisc_ops *ops,
727 struct netlink_ext_ack *extack);
728void qdisc_free(struct Qdisc *qdisc);
729struct Qdisc *qdisc_create_dflt(struct netdev_queue *dev_queue,
730 const struct Qdisc_ops *ops, u32 parentid,
731 struct netlink_ext_ack *extack);
732void __qdisc_calculate_pkt_len(struct sk_buff *skb,
733 const struct qdisc_size_table *stab);
734int skb_do_redirect(struct sk_buff *);
735
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000736static inline bool skb_at_tc_ingress(const struct sk_buff *skb)
737{
738#ifdef CONFIG_NET_CLS_ACT
739 return skb->tc_at_ingress;
740#else
741 return false;
742#endif
743}
744
745static inline bool skb_skip_tc_classify(struct sk_buff *skb)
746{
747#ifdef CONFIG_NET_CLS_ACT
748 if (skb->tc_skip_classify) {
749 skb->tc_skip_classify = 0;
750 return true;
751 }
752#endif
753 return false;
754}
755
756/* Reset all TX qdiscs greater than index of a device. */
757static inline void qdisc_reset_all_tx_gt(struct net_device *dev, unsigned int i)
758{
759 struct Qdisc *qdisc;
760
761 for (; i < dev->num_tx_queues; i++) {
762 qdisc = rtnl_dereference(netdev_get_tx_queue(dev, i)->qdisc);
763 if (qdisc) {
764 spin_lock_bh(qdisc_lock(qdisc));
765 qdisc_reset(qdisc);
766 spin_unlock_bh(qdisc_lock(qdisc));
767 }
768 }
769}
770
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000771/* Are all TX queues of the device empty? */
772static inline bool qdisc_all_tx_empty(const struct net_device *dev)
773{
774 unsigned int i;
775
776 rcu_read_lock();
777 for (i = 0; i < dev->num_tx_queues; i++) {
778 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
779 const struct Qdisc *q = rcu_dereference(txq->qdisc);
780
David Brazdil0f672f62019-12-10 10:32:29 +0000781 if (!qdisc_is_empty(q)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000782 rcu_read_unlock();
783 return false;
784 }
785 }
786 rcu_read_unlock();
787 return true;
788}
789
790/* Are any of the TX qdiscs changing? */
791static inline bool qdisc_tx_changing(const struct net_device *dev)
792{
793 unsigned int i;
794
795 for (i = 0; i < dev->num_tx_queues; i++) {
796 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
797 if (rcu_access_pointer(txq->qdisc) != txq->qdisc_sleeping)
798 return true;
799 }
800 return false;
801}
802
803/* Is the device using the noop qdisc on all queues? */
804static inline bool qdisc_tx_is_noop(const struct net_device *dev)
805{
806 unsigned int i;
807
808 for (i = 0; i < dev->num_tx_queues; i++) {
809 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
810 if (rcu_access_pointer(txq->qdisc) != &noop_qdisc)
811 return false;
812 }
813 return true;
814}
815
816static inline unsigned int qdisc_pkt_len(const struct sk_buff *skb)
817{
818 return qdisc_skb_cb(skb)->pkt_len;
819}
820
821/* additional qdisc xmit flags (NET_XMIT_MASK in linux/netdevice.h) */
822enum net_xmit_qdisc_t {
823 __NET_XMIT_STOLEN = 0x00010000,
824 __NET_XMIT_BYPASS = 0x00020000,
825};
826
827#ifdef CONFIG_NET_CLS_ACT
828#define net_xmit_drop_count(e) ((e) & __NET_XMIT_STOLEN ? 0 : 1)
829#else
830#define net_xmit_drop_count(e) (1)
831#endif
832
833static inline void qdisc_calculate_pkt_len(struct sk_buff *skb,
834 const struct Qdisc *sch)
835{
836#ifdef CONFIG_NET_SCHED
837 struct qdisc_size_table *stab = rcu_dereference_bh(sch->stab);
838
839 if (stab)
840 __qdisc_calculate_pkt_len(skb, stab);
841#endif
842}
843
844static inline int qdisc_enqueue(struct sk_buff *skb, struct Qdisc *sch,
845 struct sk_buff **to_free)
846{
847 qdisc_calculate_pkt_len(skb, sch);
848 return sch->enqueue(skb, sch, to_free);
849}
850
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000851static inline void _bstats_update(struct gnet_stats_basic_packed *bstats,
852 __u64 bytes, __u32 packets)
853{
854 bstats->bytes += bytes;
855 bstats->packets += packets;
856}
857
858static inline void bstats_update(struct gnet_stats_basic_packed *bstats,
859 const struct sk_buff *skb)
860{
861 _bstats_update(bstats,
862 qdisc_pkt_len(skb),
863 skb_is_gso(skb) ? skb_shinfo(skb)->gso_segs : 1);
864}
865
866static inline void _bstats_cpu_update(struct gnet_stats_basic_cpu *bstats,
867 __u64 bytes, __u32 packets)
868{
869 u64_stats_update_begin(&bstats->syncp);
870 _bstats_update(&bstats->bstats, bytes, packets);
871 u64_stats_update_end(&bstats->syncp);
872}
873
874static inline void bstats_cpu_update(struct gnet_stats_basic_cpu *bstats,
875 const struct sk_buff *skb)
876{
877 u64_stats_update_begin(&bstats->syncp);
878 bstats_update(&bstats->bstats, skb);
879 u64_stats_update_end(&bstats->syncp);
880}
881
882static inline void qdisc_bstats_cpu_update(struct Qdisc *sch,
883 const struct sk_buff *skb)
884{
885 bstats_cpu_update(this_cpu_ptr(sch->cpu_bstats), skb);
886}
887
888static inline void qdisc_bstats_update(struct Qdisc *sch,
889 const struct sk_buff *skb)
890{
891 bstats_update(&sch->bstats, skb);
892}
893
894static inline void qdisc_qstats_backlog_dec(struct Qdisc *sch,
895 const struct sk_buff *skb)
896{
897 sch->qstats.backlog -= qdisc_pkt_len(skb);
898}
899
900static inline void qdisc_qstats_cpu_backlog_dec(struct Qdisc *sch,
901 const struct sk_buff *skb)
902{
903 this_cpu_sub(sch->cpu_qstats->backlog, qdisc_pkt_len(skb));
904}
905
906static inline void qdisc_qstats_backlog_inc(struct Qdisc *sch,
907 const struct sk_buff *skb)
908{
909 sch->qstats.backlog += qdisc_pkt_len(skb);
910}
911
912static inline void qdisc_qstats_cpu_backlog_inc(struct Qdisc *sch,
913 const struct sk_buff *skb)
914{
915 this_cpu_add(sch->cpu_qstats->backlog, qdisc_pkt_len(skb));
916}
917
918static inline void qdisc_qstats_cpu_qlen_inc(struct Qdisc *sch)
919{
920 this_cpu_inc(sch->cpu_qstats->qlen);
921}
922
923static inline void qdisc_qstats_cpu_qlen_dec(struct Qdisc *sch)
924{
925 this_cpu_dec(sch->cpu_qstats->qlen);
926}
927
928static inline void qdisc_qstats_cpu_requeues_inc(struct Qdisc *sch)
929{
930 this_cpu_inc(sch->cpu_qstats->requeues);
931}
932
933static inline void __qdisc_qstats_drop(struct Qdisc *sch, int count)
934{
935 sch->qstats.drops += count;
936}
937
938static inline void qstats_drop_inc(struct gnet_stats_queue *qstats)
939{
940 qstats->drops++;
941}
942
943static inline void qstats_overlimit_inc(struct gnet_stats_queue *qstats)
944{
945 qstats->overlimits++;
946}
947
948static inline void qdisc_qstats_drop(struct Qdisc *sch)
949{
950 qstats_drop_inc(&sch->qstats);
951}
952
953static inline void qdisc_qstats_cpu_drop(struct Qdisc *sch)
954{
955 this_cpu_inc(sch->cpu_qstats->drops);
956}
957
958static inline void qdisc_qstats_overlimit(struct Qdisc *sch)
959{
960 sch->qstats.overlimits++;
961}
962
David Brazdil0f672f62019-12-10 10:32:29 +0000963static inline int qdisc_qstats_copy(struct gnet_dump *d, struct Qdisc *sch)
964{
965 __u32 qlen = qdisc_qlen_sum(sch);
966
967 return gnet_stats_copy_queue(d, sch->cpu_qstats, &sch->qstats, qlen);
968}
969
970static inline void qdisc_qstats_qlen_backlog(struct Qdisc *sch, __u32 *qlen,
971 __u32 *backlog)
972{
973 struct gnet_stats_queue qstats = { 0 };
974 __u32 len = qdisc_qlen_sum(sch);
975
976 __gnet_stats_copy_queue(&qstats, sch->cpu_qstats, &sch->qstats, len);
977 *qlen = qstats.qlen;
978 *backlog = qstats.backlog;
979}
980
981static inline void qdisc_tree_flush_backlog(struct Qdisc *sch)
982{
983 __u32 qlen, backlog;
984
985 qdisc_qstats_qlen_backlog(sch, &qlen, &backlog);
986 qdisc_tree_reduce_backlog(sch, qlen, backlog);
987}
988
989static inline void qdisc_purge_queue(struct Qdisc *sch)
990{
991 __u32 qlen, backlog;
992
993 qdisc_qstats_qlen_backlog(sch, &qlen, &backlog);
994 qdisc_reset(sch);
995 qdisc_tree_reduce_backlog(sch, qlen, backlog);
996}
997
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000998static inline void qdisc_skb_head_init(struct qdisc_skb_head *qh)
999{
1000 qh->head = NULL;
1001 qh->tail = NULL;
1002 qh->qlen = 0;
1003}
1004
David Brazdil0f672f62019-12-10 10:32:29 +00001005static inline void __qdisc_enqueue_tail(struct sk_buff *skb,
1006 struct qdisc_skb_head *qh)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001007{
1008 struct sk_buff *last = qh->tail;
1009
1010 if (last) {
1011 skb->next = NULL;
1012 last->next = skb;
1013 qh->tail = skb;
1014 } else {
1015 qh->tail = skb;
1016 qh->head = skb;
1017 }
1018 qh->qlen++;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001019}
1020
1021static inline int qdisc_enqueue_tail(struct sk_buff *skb, struct Qdisc *sch)
1022{
David Brazdil0f672f62019-12-10 10:32:29 +00001023 __qdisc_enqueue_tail(skb, &sch->q);
1024 qdisc_qstats_backlog_inc(sch, skb);
1025 return NET_XMIT_SUCCESS;
1026}
1027
1028static inline void __qdisc_enqueue_head(struct sk_buff *skb,
1029 struct qdisc_skb_head *qh)
1030{
1031 skb->next = qh->head;
1032
1033 if (!qh->head)
1034 qh->tail = skb;
1035 qh->head = skb;
1036 qh->qlen++;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001037}
1038
1039static inline struct sk_buff *__qdisc_dequeue_head(struct qdisc_skb_head *qh)
1040{
1041 struct sk_buff *skb = qh->head;
1042
1043 if (likely(skb != NULL)) {
1044 qh->head = skb->next;
1045 qh->qlen--;
1046 if (qh->head == NULL)
1047 qh->tail = NULL;
1048 skb->next = NULL;
1049 }
1050
1051 return skb;
1052}
1053
1054static inline struct sk_buff *qdisc_dequeue_head(struct Qdisc *sch)
1055{
1056 struct sk_buff *skb = __qdisc_dequeue_head(&sch->q);
1057
1058 if (likely(skb != NULL)) {
1059 qdisc_qstats_backlog_dec(sch, skb);
1060 qdisc_bstats_update(sch, skb);
1061 }
1062
1063 return skb;
1064}
1065
1066/* Instead of calling kfree_skb() while root qdisc lock is held,
1067 * queue the skb for future freeing at end of __dev_xmit_skb()
1068 */
1069static inline void __qdisc_drop(struct sk_buff *skb, struct sk_buff **to_free)
1070{
1071 skb->next = *to_free;
1072 *to_free = skb;
1073}
1074
1075static inline void __qdisc_drop_all(struct sk_buff *skb,
1076 struct sk_buff **to_free)
1077{
1078 if (skb->prev)
1079 skb->prev->next = *to_free;
1080 else
1081 skb->next = *to_free;
1082 *to_free = skb;
1083}
1084
1085static inline unsigned int __qdisc_queue_drop_head(struct Qdisc *sch,
1086 struct qdisc_skb_head *qh,
1087 struct sk_buff **to_free)
1088{
1089 struct sk_buff *skb = __qdisc_dequeue_head(qh);
1090
1091 if (likely(skb != NULL)) {
1092 unsigned int len = qdisc_pkt_len(skb);
1093
1094 qdisc_qstats_backlog_dec(sch, skb);
1095 __qdisc_drop(skb, to_free);
1096 return len;
1097 }
1098
1099 return 0;
1100}
1101
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001102static inline struct sk_buff *qdisc_peek_head(struct Qdisc *sch)
1103{
1104 const struct qdisc_skb_head *qh = &sch->q;
1105
1106 return qh->head;
1107}
1108
1109/* generic pseudo peek method for non-work-conserving qdisc */
1110static inline struct sk_buff *qdisc_peek_dequeued(struct Qdisc *sch)
1111{
1112 struct sk_buff *skb = skb_peek(&sch->gso_skb);
1113
1114 /* we can reuse ->gso_skb because peek isn't called for root qdiscs */
1115 if (!skb) {
1116 skb = sch->dequeue(sch);
1117
1118 if (skb) {
1119 __skb_queue_head(&sch->gso_skb, skb);
1120 /* it's still part of the queue */
1121 qdisc_qstats_backlog_inc(sch, skb);
1122 sch->q.qlen++;
1123 }
1124 }
1125
1126 return skb;
1127}
1128
David Brazdil0f672f62019-12-10 10:32:29 +00001129static inline void qdisc_update_stats_at_dequeue(struct Qdisc *sch,
1130 struct sk_buff *skb)
1131{
1132 if (qdisc_is_percpu_stats(sch)) {
1133 qdisc_qstats_cpu_backlog_dec(sch, skb);
1134 qdisc_bstats_cpu_update(sch, skb);
1135 qdisc_qstats_cpu_qlen_dec(sch);
1136 } else {
1137 qdisc_qstats_backlog_dec(sch, skb);
1138 qdisc_bstats_update(sch, skb);
1139 sch->q.qlen--;
1140 }
1141}
1142
1143static inline void qdisc_update_stats_at_enqueue(struct Qdisc *sch,
1144 unsigned int pkt_len)
1145{
1146 if (qdisc_is_percpu_stats(sch)) {
1147 qdisc_qstats_cpu_qlen_inc(sch);
1148 this_cpu_add(sch->cpu_qstats->backlog, pkt_len);
1149 } else {
1150 sch->qstats.backlog += pkt_len;
1151 sch->q.qlen++;
1152 }
1153}
1154
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001155/* use instead of qdisc->dequeue() for all qdiscs queried with ->peek() */
1156static inline struct sk_buff *qdisc_dequeue_peeked(struct Qdisc *sch)
1157{
1158 struct sk_buff *skb = skb_peek(&sch->gso_skb);
1159
1160 if (skb) {
1161 skb = __skb_dequeue(&sch->gso_skb);
David Brazdil0f672f62019-12-10 10:32:29 +00001162 if (qdisc_is_percpu_stats(sch)) {
1163 qdisc_qstats_cpu_backlog_dec(sch, skb);
1164 qdisc_qstats_cpu_qlen_dec(sch);
1165 } else {
1166 qdisc_qstats_backlog_dec(sch, skb);
1167 sch->q.qlen--;
1168 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001169 } else {
1170 skb = sch->dequeue(sch);
1171 }
1172
1173 return skb;
1174}
1175
1176static inline void __qdisc_reset_queue(struct qdisc_skb_head *qh)
1177{
1178 /*
1179 * We do not know the backlog in bytes of this list, it
1180 * is up to the caller to correct it
1181 */
1182 ASSERT_RTNL();
1183 if (qh->qlen) {
1184 rtnl_kfree_skbs(qh->head, qh->tail);
1185
1186 qh->head = NULL;
1187 qh->tail = NULL;
1188 qh->qlen = 0;
1189 }
1190}
1191
1192static inline void qdisc_reset_queue(struct Qdisc *sch)
1193{
1194 __qdisc_reset_queue(&sch->q);
1195 sch->qstats.backlog = 0;
1196}
1197
1198static inline struct Qdisc *qdisc_replace(struct Qdisc *sch, struct Qdisc *new,
1199 struct Qdisc **pold)
1200{
1201 struct Qdisc *old;
1202
1203 sch_tree_lock(sch);
1204 old = *pold;
1205 *pold = new;
David Brazdil0f672f62019-12-10 10:32:29 +00001206 if (old != NULL)
Olivier Deprez0e641232021-09-23 10:07:05 +02001207 qdisc_purge_queue(old);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001208 sch_tree_unlock(sch);
1209
1210 return old;
1211}
1212
1213static inline void rtnl_qdisc_drop(struct sk_buff *skb, struct Qdisc *sch)
1214{
1215 rtnl_kfree_skbs(skb, skb);
1216 qdisc_qstats_drop(sch);
1217}
1218
1219static inline int qdisc_drop_cpu(struct sk_buff *skb, struct Qdisc *sch,
1220 struct sk_buff **to_free)
1221{
1222 __qdisc_drop(skb, to_free);
1223 qdisc_qstats_cpu_drop(sch);
1224
1225 return NET_XMIT_DROP;
1226}
1227
1228static inline int qdisc_drop(struct sk_buff *skb, struct Qdisc *sch,
1229 struct sk_buff **to_free)
1230{
1231 __qdisc_drop(skb, to_free);
1232 qdisc_qstats_drop(sch);
1233
1234 return NET_XMIT_DROP;
1235}
1236
1237static inline int qdisc_drop_all(struct sk_buff *skb, struct Qdisc *sch,
1238 struct sk_buff **to_free)
1239{
1240 __qdisc_drop_all(skb, to_free);
1241 qdisc_qstats_drop(sch);
1242
1243 return NET_XMIT_DROP;
1244}
1245
1246/* Length to Time (L2T) lookup in a qdisc_rate_table, to determine how
1247 long it will take to send a packet given its size.
1248 */
1249static inline u32 qdisc_l2t(struct qdisc_rate_table* rtab, unsigned int pktlen)
1250{
1251 int slot = pktlen + rtab->rate.cell_align + rtab->rate.overhead;
1252 if (slot < 0)
1253 slot = 0;
1254 slot >>= rtab->rate.cell_log;
1255 if (slot > 255)
1256 return rtab->data[255]*(slot >> 8) + rtab->data[slot & 0xFF];
1257 return rtab->data[slot];
1258}
1259
1260struct psched_ratecfg {
1261 u64 rate_bytes_ps; /* bytes per second */
1262 u32 mult;
1263 u16 overhead;
Olivier Deprez157378f2022-04-04 15:47:50 +02001264 u16 mpu;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001265 u8 linklayer;
1266 u8 shift;
1267};
1268
1269static inline u64 psched_l2t_ns(const struct psched_ratecfg *r,
1270 unsigned int len)
1271{
1272 len += r->overhead;
1273
Olivier Deprez157378f2022-04-04 15:47:50 +02001274 if (len < r->mpu)
1275 len = r->mpu;
1276
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001277 if (unlikely(r->linklayer == TC_LINKLAYER_ATM))
1278 return ((u64)(DIV_ROUND_UP(len,48)*53) * r->mult) >> r->shift;
1279
1280 return ((u64)len * r->mult) >> r->shift;
1281}
1282
1283void psched_ratecfg_precompute(struct psched_ratecfg *r,
1284 const struct tc_ratespec *conf,
1285 u64 rate64);
1286
1287static inline void psched_ratecfg_getrate(struct tc_ratespec *res,
1288 const struct psched_ratecfg *r)
1289{
1290 memset(res, 0, sizeof(*res));
1291
1292 /* legacy struct tc_ratespec has a 32bit @rate field
1293 * Qdisc using 64bit rate should add new attributes
1294 * in order to maintain compatibility.
1295 */
1296 res->rate = min_t(u64, r->rate_bytes_ps, ~0U);
1297
1298 res->overhead = r->overhead;
Olivier Deprez157378f2022-04-04 15:47:50 +02001299 res->mpu = r->mpu;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001300 res->linklayer = (r->linklayer & TC_LINKLAYER_MASK);
1301}
1302
1303/* Mini Qdisc serves for specific needs of ingress/clsact Qdisc.
1304 * The fast path only needs to access filter list and to update stats
1305 */
1306struct mini_Qdisc {
1307 struct tcf_proto *filter_list;
Olivier Deprez157378f2022-04-04 15:47:50 +02001308 struct tcf_block *block;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001309 struct gnet_stats_basic_cpu __percpu *cpu_bstats;
1310 struct gnet_stats_queue __percpu *cpu_qstats;
1311 struct rcu_head rcu;
1312};
1313
1314static inline void mini_qdisc_bstats_cpu_update(struct mini_Qdisc *miniq,
1315 const struct sk_buff *skb)
1316{
1317 bstats_cpu_update(this_cpu_ptr(miniq->cpu_bstats), skb);
1318}
1319
1320static inline void mini_qdisc_qstats_cpu_drop(struct mini_Qdisc *miniq)
1321{
1322 this_cpu_inc(miniq->cpu_qstats->drops);
1323}
1324
1325struct mini_Qdisc_pair {
1326 struct mini_Qdisc miniq1;
1327 struct mini_Qdisc miniq2;
1328 struct mini_Qdisc __rcu **p_miniq;
1329};
1330
1331void mini_qdisc_pair_swap(struct mini_Qdisc_pair *miniqp,
1332 struct tcf_proto *tp_head);
1333void mini_qdisc_pair_init(struct mini_Qdisc_pair *miniqp, struct Qdisc *qdisc,
1334 struct mini_Qdisc __rcu **p_miniq);
Olivier Deprez157378f2022-04-04 15:47:50 +02001335void mini_qdisc_pair_block_init(struct mini_Qdisc_pair *miniqp,
1336 struct tcf_block *block);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001337
Olivier Deprez157378f2022-04-04 15:47:50 +02001338static inline int skb_tc_reinsert(struct sk_buff *skb, struct tcf_result *res)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001339{
Olivier Deprez157378f2022-04-04 15:47:50 +02001340 return res->ingress ? netif_receive_skb(skb) : dev_queue_xmit(skb);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001341}
1342
1343#endif