blob: 0cb0a4bcb5447ddcb02a733d7d1f8a76a932a561 [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;
95 int padded;
96 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;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000116};
117
118static inline void qdisc_refcount_inc(struct Qdisc *qdisc)
119{
120 if (qdisc->flags & TCQ_F_BUILTIN)
121 return;
122 refcount_inc(&qdisc->refcnt);
123}
124
David Brazdil0f672f62019-12-10 10:32:29 +0000125/* Intended to be used by unlocked users, when concurrent qdisc release is
126 * possible.
127 */
128
129static inline struct Qdisc *qdisc_refcount_inc_nz(struct Qdisc *qdisc)
130{
131 if (qdisc->flags & TCQ_F_BUILTIN)
132 return qdisc;
133 if (refcount_inc_not_zero(&qdisc->refcnt))
134 return qdisc;
135 return NULL;
136}
137
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000138static inline bool qdisc_is_running(struct Qdisc *qdisc)
139{
140 if (qdisc->flags & TCQ_F_NOLOCK)
141 return spin_is_locked(&qdisc->seqlock);
142 return (raw_read_seqcount(&qdisc->running) & 1) ? true : false;
143}
144
David Brazdil0f672f62019-12-10 10:32:29 +0000145static inline bool qdisc_is_percpu_stats(const struct Qdisc *q)
146{
147 return q->flags & TCQ_F_CPUSTATS;
148}
149
150static inline bool qdisc_is_empty(const struct Qdisc *qdisc)
151{
152 if (qdisc_is_percpu_stats(qdisc))
Olivier Deprez0e641232021-09-23 10:07:05 +0200153 return READ_ONCE(qdisc->empty);
154 return !READ_ONCE(qdisc->q.qlen);
David Brazdil0f672f62019-12-10 10:32:29 +0000155}
156
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000157static inline bool qdisc_run_begin(struct Qdisc *qdisc)
158{
159 if (qdisc->flags & TCQ_F_NOLOCK) {
Olivier Deprez0e641232021-09-23 10:07:05 +0200160 if (spin_trylock(&qdisc->seqlock))
161 goto nolock_empty;
162
163 /* Paired with smp_mb__after_atomic() to make sure
164 * STATE_MISSED checking is synchronized with clearing
165 * in pfifo_fast_dequeue().
166 */
167 smp_mb__before_atomic();
168
169 /* If the MISSED flag is set, it means other thread has
170 * set the MISSED flag before second spin_trylock(), so
171 * we can return false here to avoid multi cpus doing
172 * the set_bit() and second spin_trylock() concurrently.
173 */
174 if (test_bit(__QDISC_STATE_MISSED, &qdisc->state))
175 return false;
176
177 /* Set the MISSED flag before the second spin_trylock(),
178 * if the second spin_trylock() return false, it means
179 * other cpu holding the lock will do dequeuing for us
180 * or it will see the MISSED flag set after releasing
181 * lock and reschedule the net_tx_action() to do the
182 * dequeuing.
183 */
184 set_bit(__QDISC_STATE_MISSED, &qdisc->state);
185
186 /* spin_trylock() only has load-acquire semantic, so use
187 * smp_mb__after_atomic() to ensure STATE_MISSED is set
188 * before doing the second spin_trylock().
189 */
190 smp_mb__after_atomic();
191
192 /* Retry again in case other CPU may not see the new flag
193 * after it releases the lock at the end of qdisc_run_end().
194 */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000195 if (!spin_trylock(&qdisc->seqlock))
196 return false;
Olivier Deprez0e641232021-09-23 10:07:05 +0200197
198nolock_empty:
199 WRITE_ONCE(qdisc->empty, false);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000200 } else if (qdisc_is_running(qdisc)) {
201 return false;
202 }
203 /* Variant of write_seqcount_begin() telling lockdep a trylock
204 * was attempted.
205 */
206 raw_write_seqcount_begin(&qdisc->running);
207 seqcount_acquire(&qdisc->running.dep_map, 0, 1, _RET_IP_);
208 return true;
209}
210
211static inline void qdisc_run_end(struct Qdisc *qdisc)
212{
213 write_seqcount_end(&qdisc->running);
Olivier Deprez0e641232021-09-23 10:07:05 +0200214 if (qdisc->flags & TCQ_F_NOLOCK) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000215 spin_unlock(&qdisc->seqlock);
Olivier Deprez0e641232021-09-23 10:07:05 +0200216
217 if (unlikely(test_bit(__QDISC_STATE_MISSED,
218 &qdisc->state))) {
219 clear_bit(__QDISC_STATE_MISSED, &qdisc->state);
220 __netif_schedule(qdisc);
221 }
222 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000223}
224
225static inline bool qdisc_may_bulk(const struct Qdisc *qdisc)
226{
227 return qdisc->flags & TCQ_F_ONETXQUEUE;
228}
229
230static inline int qdisc_avail_bulklimit(const struct netdev_queue *txq)
231{
232#ifdef CONFIG_BQL
233 /* Non-BQL migrated drivers will return 0, too. */
234 return dql_avail(&txq->dql);
235#else
236 return 0;
237#endif
238}
239
240struct Qdisc_class_ops {
David Brazdil0f672f62019-12-10 10:32:29 +0000241 unsigned int flags;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000242 /* Child qdisc manipulation */
243 struct netdev_queue * (*select_queue)(struct Qdisc *, struct tcmsg *);
244 int (*graft)(struct Qdisc *, unsigned long cl,
245 struct Qdisc *, struct Qdisc **,
246 struct netlink_ext_ack *extack);
247 struct Qdisc * (*leaf)(struct Qdisc *, unsigned long cl);
248 void (*qlen_notify)(struct Qdisc *, unsigned long);
249
250 /* Class manipulation routines */
251 unsigned long (*find)(struct Qdisc *, u32 classid);
252 int (*change)(struct Qdisc *, u32, u32,
253 struct nlattr **, unsigned long *,
254 struct netlink_ext_ack *);
255 int (*delete)(struct Qdisc *, unsigned long);
256 void (*walk)(struct Qdisc *, struct qdisc_walker * arg);
257
258 /* Filter manipulation */
259 struct tcf_block * (*tcf_block)(struct Qdisc *sch,
260 unsigned long arg,
261 struct netlink_ext_ack *extack);
262 unsigned long (*bind_tcf)(struct Qdisc *, unsigned long,
263 u32 classid);
264 void (*unbind_tcf)(struct Qdisc *, unsigned long);
265
266 /* rtnetlink specific */
267 int (*dump)(struct Qdisc *, unsigned long,
268 struct sk_buff *skb, struct tcmsg*);
269 int (*dump_stats)(struct Qdisc *, unsigned long,
270 struct gnet_dump *);
271};
272
David Brazdil0f672f62019-12-10 10:32:29 +0000273/* Qdisc_class_ops flag values */
274
275/* Implements API that doesn't require rtnl lock */
276enum qdisc_class_ops_flags {
277 QDISC_CLASS_OPS_DOIT_UNLOCKED = 1,
278};
279
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000280struct Qdisc_ops {
281 struct Qdisc_ops *next;
282 const struct Qdisc_class_ops *cl_ops;
283 char id[IFNAMSIZ];
284 int priv_size;
285 unsigned int static_flags;
286
287 int (*enqueue)(struct sk_buff *skb,
288 struct Qdisc *sch,
289 struct sk_buff **to_free);
290 struct sk_buff * (*dequeue)(struct Qdisc *);
291 struct sk_buff * (*peek)(struct Qdisc *);
292
293 int (*init)(struct Qdisc *sch, struct nlattr *arg,
294 struct netlink_ext_ack *extack);
295 void (*reset)(struct Qdisc *);
296 void (*destroy)(struct Qdisc *);
297 int (*change)(struct Qdisc *sch,
298 struct nlattr *arg,
299 struct netlink_ext_ack *extack);
300 void (*attach)(struct Qdisc *sch);
301 int (*change_tx_queue_len)(struct Qdisc *, unsigned int);
302
303 int (*dump)(struct Qdisc *, struct sk_buff *);
304 int (*dump_stats)(struct Qdisc *, struct gnet_dump *);
305
306 void (*ingress_block_set)(struct Qdisc *sch,
307 u32 block_index);
308 void (*egress_block_set)(struct Qdisc *sch,
309 u32 block_index);
310 u32 (*ingress_block_get)(struct Qdisc *sch);
311 u32 (*egress_block_get)(struct Qdisc *sch);
312
313 struct module *owner;
314};
315
316
317struct tcf_result {
318 union {
319 struct {
320 unsigned long class;
321 u32 classid;
322 };
323 const struct tcf_proto *goto_tp;
324
David Brazdil0f672f62019-12-10 10:32:29 +0000325 /* used in the skb_tc_reinsert function */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000326 struct {
327 bool ingress;
328 struct gnet_stats_queue *qstats;
329 };
330 };
331};
332
333struct tcf_chain;
334
335struct tcf_proto_ops {
336 struct list_head head;
337 char kind[IFNAMSIZ];
338
339 int (*classify)(struct sk_buff *,
340 const struct tcf_proto *,
341 struct tcf_result *);
342 int (*init)(struct tcf_proto*);
David Brazdil0f672f62019-12-10 10:32:29 +0000343 void (*destroy)(struct tcf_proto *tp, bool rtnl_held,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000344 struct netlink_ext_ack *extack);
345
346 void* (*get)(struct tcf_proto*, u32 handle);
David Brazdil0f672f62019-12-10 10:32:29 +0000347 void (*put)(struct tcf_proto *tp, void *f);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000348 int (*change)(struct net *net, struct sk_buff *,
349 struct tcf_proto*, unsigned long,
350 u32 handle, struct nlattr **,
David Brazdil0f672f62019-12-10 10:32:29 +0000351 void **, bool, bool,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000352 struct netlink_ext_ack *);
353 int (*delete)(struct tcf_proto *tp, void *arg,
David Brazdil0f672f62019-12-10 10:32:29 +0000354 bool *last, bool rtnl_held,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000355 struct netlink_ext_ack *);
Olivier Deprez0e641232021-09-23 10:07:05 +0200356 bool (*delete_empty)(struct tcf_proto *tp);
David Brazdil0f672f62019-12-10 10:32:29 +0000357 void (*walk)(struct tcf_proto *tp,
358 struct tcf_walker *arg, bool rtnl_held);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000359 int (*reoffload)(struct tcf_proto *tp, bool add,
David Brazdil0f672f62019-12-10 10:32:29 +0000360 flow_setup_cb_t *cb, void *cb_priv,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000361 struct netlink_ext_ack *extack);
David Brazdil0f672f62019-12-10 10:32:29 +0000362 void (*hw_add)(struct tcf_proto *tp,
363 void *type_data);
364 void (*hw_del)(struct tcf_proto *tp,
365 void *type_data);
Olivier Deprez0e641232021-09-23 10:07:05 +0200366 void (*bind_class)(void *, u32, unsigned long,
367 void *, unsigned long);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000368 void * (*tmplt_create)(struct net *net,
369 struct tcf_chain *chain,
370 struct nlattr **tca,
371 struct netlink_ext_ack *extack);
372 void (*tmplt_destroy)(void *tmplt_priv);
373
374 /* rtnetlink specific */
375 int (*dump)(struct net*, struct tcf_proto*, void *,
David Brazdil0f672f62019-12-10 10:32:29 +0000376 struct sk_buff *skb, struct tcmsg*,
377 bool);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000378 int (*tmplt_dump)(struct sk_buff *skb,
379 struct net *net,
380 void *tmplt_priv);
381
382 struct module *owner;
David Brazdil0f672f62019-12-10 10:32:29 +0000383 int flags;
384};
385
Olivier Deprez0e641232021-09-23 10:07:05 +0200386/* Classifiers setting TCF_PROTO_OPS_DOIT_UNLOCKED in tcf_proto_ops->flags
387 * are expected to implement tcf_proto_ops->delete_empty(), otherwise race
388 * conditions can occur when filters are inserted/deleted simultaneously.
389 */
David Brazdil0f672f62019-12-10 10:32:29 +0000390enum tcf_proto_ops_flags {
391 TCF_PROTO_OPS_DOIT_UNLOCKED = 1,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000392};
393
394struct tcf_proto {
395 /* Fast access part */
396 struct tcf_proto __rcu *next;
397 void __rcu *root;
398
399 /* called under RCU BH lock*/
400 int (*classify)(struct sk_buff *,
401 const struct tcf_proto *,
402 struct tcf_result *);
403 __be16 protocol;
404
405 /* All the rest */
406 u32 prio;
407 void *data;
408 const struct tcf_proto_ops *ops;
409 struct tcf_chain *chain;
David Brazdil0f672f62019-12-10 10:32:29 +0000410 /* Lock protects tcf_proto shared state and can be used by unlocked
411 * classifiers to protect their private data.
412 */
413 spinlock_t lock;
414 bool deleting;
415 refcount_t refcnt;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000416 struct rcu_head rcu;
David Brazdil0f672f62019-12-10 10:32:29 +0000417 struct hlist_node destroy_ht_node;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000418};
419
420struct qdisc_skb_cb {
David Brazdil0f672f62019-12-10 10:32:29 +0000421 struct {
422 unsigned int pkt_len;
423 u16 slave_dev_queue_mapping;
424 u16 tc_classid;
425 };
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000426#define QDISC_CB_PRIV_LEN 20
427 unsigned char data[QDISC_CB_PRIV_LEN];
428};
429
430typedef void tcf_chain_head_change_t(struct tcf_proto *tp_head, void *priv);
431
432struct tcf_chain {
David Brazdil0f672f62019-12-10 10:32:29 +0000433 /* Protects filter_chain. */
434 struct mutex filter_chain_lock;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000435 struct tcf_proto __rcu *filter_chain;
436 struct list_head list;
437 struct tcf_block *block;
438 u32 index; /* chain index */
439 unsigned int refcnt;
440 unsigned int action_refcnt;
441 bool explicitly_created;
David Brazdil0f672f62019-12-10 10:32:29 +0000442 bool flushing;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000443 const struct tcf_proto_ops *tmplt_ops;
444 void *tmplt_priv;
David Brazdil0f672f62019-12-10 10:32:29 +0000445 struct rcu_head rcu;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000446};
447
448struct tcf_block {
David Brazdil0f672f62019-12-10 10:32:29 +0000449 /* Lock protects tcf_block and lifetime-management data of chains
450 * attached to the block (refcnt, action_refcnt, explicitly_created).
451 */
452 struct mutex lock;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000453 struct list_head chain_list;
454 u32 index; /* block index for shared blocks */
Olivier Deprez0e641232021-09-23 10:07:05 +0200455 u32 classid; /* which class this block belongs to */
David Brazdil0f672f62019-12-10 10:32:29 +0000456 refcount_t refcnt;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000457 struct net *net;
458 struct Qdisc *q;
David Brazdil0f672f62019-12-10 10:32:29 +0000459 struct rw_semaphore cb_lock; /* protects cb_list and offload counters */
460 struct flow_block flow_block;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000461 struct list_head owner_list;
462 bool keep_dst;
David Brazdil0f672f62019-12-10 10:32:29 +0000463 atomic_t offloadcnt; /* Number of oddloaded filters */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000464 unsigned int nooffloaddevcnt; /* Number of devs unable to do offload */
David Brazdil0f672f62019-12-10 10:32:29 +0000465 unsigned int lockeddevcnt; /* Number of devs that require rtnl lock. */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000466 struct {
467 struct tcf_chain *chain;
468 struct list_head filter_chain_list;
469 } chain0;
David Brazdil0f672f62019-12-10 10:32:29 +0000470 struct rcu_head rcu;
471 DECLARE_HASHTABLE(proto_destroy_ht, 7);
472 struct mutex proto_destroy_lock; /* Lock for proto_destroy hashtable. */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000473};
474
David Brazdil0f672f62019-12-10 10:32:29 +0000475#ifdef CONFIG_PROVE_LOCKING
476static inline bool lockdep_tcf_chain_is_locked(struct tcf_chain *chain)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000477{
David Brazdil0f672f62019-12-10 10:32:29 +0000478 return lockdep_is_held(&chain->filter_chain_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000479}
480
David Brazdil0f672f62019-12-10 10:32:29 +0000481static inline bool lockdep_tcf_proto_is_locked(struct tcf_proto *tp)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000482{
David Brazdil0f672f62019-12-10 10:32:29 +0000483 return lockdep_is_held(&tp->lock);
484}
485#else
486static inline bool lockdep_tcf_chain_is_locked(struct tcf_block *chain)
487{
488 return true;
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 true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000494}
David Brazdil0f672f62019-12-10 10:32:29 +0000495#endif /* #ifdef CONFIG_PROVE_LOCKING */
496
497#define tcf_chain_dereference(p, chain) \
498 rcu_dereference_protected(p, lockdep_tcf_chain_is_locked(chain))
499
500#define tcf_proto_dereference(p, tp) \
501 rcu_dereference_protected(p, lockdep_tcf_proto_is_locked(tp))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000502
503static inline void qdisc_cb_private_validate(const struct sk_buff *skb, int sz)
504{
505 struct qdisc_skb_cb *qcb;
506
507 BUILD_BUG_ON(sizeof(skb->cb) < offsetof(struct qdisc_skb_cb, data) + sz);
508 BUILD_BUG_ON(sizeof(qcb->data) < sz);
509}
510
511static inline int qdisc_qlen_cpu(const struct Qdisc *q)
512{
513 return this_cpu_ptr(q->cpu_qstats)->qlen;
514}
515
516static inline int qdisc_qlen(const struct Qdisc *q)
517{
518 return q->q.qlen;
519}
520
521static inline int qdisc_qlen_sum(const struct Qdisc *q)
522{
523 __u32 qlen = q->qstats.qlen;
524 int i;
525
David Brazdil0f672f62019-12-10 10:32:29 +0000526 if (qdisc_is_percpu_stats(q)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000527 for_each_possible_cpu(i)
528 qlen += per_cpu_ptr(q->cpu_qstats, i)->qlen;
529 } else {
530 qlen += q->q.qlen;
531 }
532
533 return qlen;
534}
535
536static inline struct qdisc_skb_cb *qdisc_skb_cb(const struct sk_buff *skb)
537{
538 return (struct qdisc_skb_cb *)skb->cb;
539}
540
541static inline spinlock_t *qdisc_lock(struct Qdisc *qdisc)
542{
543 return &qdisc->q.lock;
544}
545
546static inline struct Qdisc *qdisc_root(const struct Qdisc *qdisc)
547{
548 struct Qdisc *q = rcu_dereference_rtnl(qdisc->dev_queue->qdisc);
549
550 return q;
551}
552
David Brazdil0f672f62019-12-10 10:32:29 +0000553static inline struct Qdisc *qdisc_root_bh(const struct Qdisc *qdisc)
554{
555 return rcu_dereference_bh(qdisc->dev_queue->qdisc);
556}
557
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000558static inline struct Qdisc *qdisc_root_sleeping(const struct Qdisc *qdisc)
559{
560 return qdisc->dev_queue->qdisc_sleeping;
561}
562
563/* The qdisc root lock is a mechanism by which to top level
564 * of a qdisc tree can be locked from any qdisc node in the
565 * forest. This allows changing the configuration of some
566 * aspect of the qdisc tree while blocking out asynchronous
567 * qdisc access in the packet processing paths.
568 *
569 * It is only legal to do this when the root will not change
570 * on us. Otherwise we'll potentially lock the wrong qdisc
571 * root. This is enforced by holding the RTNL semaphore, which
572 * all users of this lock accessor must do.
573 */
574static inline spinlock_t *qdisc_root_lock(const struct Qdisc *qdisc)
575{
576 struct Qdisc *root = qdisc_root(qdisc);
577
578 ASSERT_RTNL();
579 return qdisc_lock(root);
580}
581
582static inline spinlock_t *qdisc_root_sleeping_lock(const struct Qdisc *qdisc)
583{
584 struct Qdisc *root = qdisc_root_sleeping(qdisc);
585
586 ASSERT_RTNL();
587 return qdisc_lock(root);
588}
589
590static inline seqcount_t *qdisc_root_sleeping_running(const struct Qdisc *qdisc)
591{
592 struct Qdisc *root = qdisc_root_sleeping(qdisc);
593
594 ASSERT_RTNL();
595 return &root->running;
596}
597
598static inline struct net_device *qdisc_dev(const struct Qdisc *qdisc)
599{
600 return qdisc->dev_queue->dev;
601}
602
603static inline void sch_tree_lock(const struct Qdisc *q)
604{
605 spin_lock_bh(qdisc_root_sleeping_lock(q));
606}
607
608static inline void sch_tree_unlock(const struct Qdisc *q)
609{
610 spin_unlock_bh(qdisc_root_sleeping_lock(q));
611}
612
613extern struct Qdisc noop_qdisc;
614extern struct Qdisc_ops noop_qdisc_ops;
615extern struct Qdisc_ops pfifo_fast_ops;
616extern struct Qdisc_ops mq_qdisc_ops;
617extern struct Qdisc_ops noqueue_qdisc_ops;
618extern const struct Qdisc_ops *default_qdisc_ops;
619static inline const struct Qdisc_ops *
620get_default_qdisc_ops(const struct net_device *dev, int ntx)
621{
622 return ntx < dev->real_num_tx_queues ?
623 default_qdisc_ops : &pfifo_fast_ops;
624}
625
626struct Qdisc_class_common {
627 u32 classid;
628 struct hlist_node hnode;
629};
630
631struct Qdisc_class_hash {
632 struct hlist_head *hash;
633 unsigned int hashsize;
634 unsigned int hashmask;
635 unsigned int hashelems;
636};
637
638static inline unsigned int qdisc_class_hash(u32 id, u32 mask)
639{
640 id ^= id >> 8;
641 id ^= id >> 4;
642 return id & mask;
643}
644
645static inline struct Qdisc_class_common *
646qdisc_class_find(const struct Qdisc_class_hash *hash, u32 id)
647{
648 struct Qdisc_class_common *cl;
649 unsigned int h;
650
651 if (!id)
652 return NULL;
653
654 h = qdisc_class_hash(id, hash->hashmask);
655 hlist_for_each_entry(cl, &hash->hash[h], hnode) {
656 if (cl->classid == id)
657 return cl;
658 }
659 return NULL;
660}
661
662static inline int tc_classid_to_hwtc(struct net_device *dev, u32 classid)
663{
664 u32 hwtc = TC_H_MIN(classid) - TC_H_MIN_PRIORITY;
665
666 return (hwtc < netdev_get_num_tc(dev)) ? hwtc : -EINVAL;
667}
668
669int qdisc_class_hash_init(struct Qdisc_class_hash *);
670void qdisc_class_hash_insert(struct Qdisc_class_hash *,
671 struct Qdisc_class_common *);
672void qdisc_class_hash_remove(struct Qdisc_class_hash *,
673 struct Qdisc_class_common *);
674void qdisc_class_hash_grow(struct Qdisc *, struct Qdisc_class_hash *);
675void qdisc_class_hash_destroy(struct Qdisc_class_hash *);
676
677int dev_qdisc_change_tx_queue_len(struct net_device *dev);
678void dev_init_scheduler(struct net_device *dev);
679void dev_shutdown(struct net_device *dev);
680void dev_activate(struct net_device *dev);
681void dev_deactivate(struct net_device *dev);
682void dev_deactivate_many(struct list_head *head);
683struct Qdisc *dev_graft_qdisc(struct netdev_queue *dev_queue,
684 struct Qdisc *qdisc);
685void qdisc_reset(struct Qdisc *qdisc);
David Brazdil0f672f62019-12-10 10:32:29 +0000686void qdisc_put(struct Qdisc *qdisc);
687void qdisc_put_unlocked(struct Qdisc *qdisc);
688void qdisc_tree_reduce_backlog(struct Qdisc *qdisc, int n, int len);
689#ifdef CONFIG_NET_SCHED
690int qdisc_offload_dump_helper(struct Qdisc *q, enum tc_setup_type type,
691 void *type_data);
692void qdisc_offload_graft_helper(struct net_device *dev, struct Qdisc *sch,
693 struct Qdisc *new, struct Qdisc *old,
694 enum tc_setup_type type, void *type_data,
695 struct netlink_ext_ack *extack);
696#else
697static inline int
698qdisc_offload_dump_helper(struct Qdisc *q, enum tc_setup_type type,
699 void *type_data)
700{
701 q->flags &= ~TCQ_F_OFFLOADED;
702 return 0;
703}
704
705static inline void
706qdisc_offload_graft_helper(struct net_device *dev, struct Qdisc *sch,
707 struct Qdisc *new, struct Qdisc *old,
708 enum tc_setup_type type, void *type_data,
709 struct netlink_ext_ack *extack)
710{
711}
712#endif
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000713struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue,
714 const struct Qdisc_ops *ops,
715 struct netlink_ext_ack *extack);
716void qdisc_free(struct Qdisc *qdisc);
717struct Qdisc *qdisc_create_dflt(struct netdev_queue *dev_queue,
718 const struct Qdisc_ops *ops, u32 parentid,
719 struct netlink_ext_ack *extack);
720void __qdisc_calculate_pkt_len(struct sk_buff *skb,
721 const struct qdisc_size_table *stab);
722int skb_do_redirect(struct sk_buff *);
723
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000724static inline bool skb_at_tc_ingress(const struct sk_buff *skb)
725{
726#ifdef CONFIG_NET_CLS_ACT
727 return skb->tc_at_ingress;
728#else
729 return false;
730#endif
731}
732
733static inline bool skb_skip_tc_classify(struct sk_buff *skb)
734{
735#ifdef CONFIG_NET_CLS_ACT
736 if (skb->tc_skip_classify) {
737 skb->tc_skip_classify = 0;
738 return true;
739 }
740#endif
741 return false;
742}
743
744/* Reset all TX qdiscs greater than index of a device. */
745static inline void qdisc_reset_all_tx_gt(struct net_device *dev, unsigned int i)
746{
747 struct Qdisc *qdisc;
748
749 for (; i < dev->num_tx_queues; i++) {
750 qdisc = rtnl_dereference(netdev_get_tx_queue(dev, i)->qdisc);
751 if (qdisc) {
752 spin_lock_bh(qdisc_lock(qdisc));
753 qdisc_reset(qdisc);
754 spin_unlock_bh(qdisc_lock(qdisc));
755 }
756 }
757}
758
759static inline void qdisc_reset_all_tx(struct net_device *dev)
760{
761 qdisc_reset_all_tx_gt(dev, 0);
762}
763
764/* Are all TX queues of the device empty? */
765static inline bool qdisc_all_tx_empty(const struct net_device *dev)
766{
767 unsigned int i;
768
769 rcu_read_lock();
770 for (i = 0; i < dev->num_tx_queues; i++) {
771 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
772 const struct Qdisc *q = rcu_dereference(txq->qdisc);
773
David Brazdil0f672f62019-12-10 10:32:29 +0000774 if (!qdisc_is_empty(q)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000775 rcu_read_unlock();
776 return false;
777 }
778 }
779 rcu_read_unlock();
780 return true;
781}
782
783/* Are any of the TX qdiscs changing? */
784static inline bool qdisc_tx_changing(const struct net_device *dev)
785{
786 unsigned int i;
787
788 for (i = 0; i < dev->num_tx_queues; i++) {
789 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
790 if (rcu_access_pointer(txq->qdisc) != txq->qdisc_sleeping)
791 return true;
792 }
793 return false;
794}
795
796/* Is the device using the noop qdisc on all queues? */
797static inline bool qdisc_tx_is_noop(const struct net_device *dev)
798{
799 unsigned int i;
800
801 for (i = 0; i < dev->num_tx_queues; i++) {
802 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
803 if (rcu_access_pointer(txq->qdisc) != &noop_qdisc)
804 return false;
805 }
806 return true;
807}
808
809static inline unsigned int qdisc_pkt_len(const struct sk_buff *skb)
810{
811 return qdisc_skb_cb(skb)->pkt_len;
812}
813
814/* additional qdisc xmit flags (NET_XMIT_MASK in linux/netdevice.h) */
815enum net_xmit_qdisc_t {
816 __NET_XMIT_STOLEN = 0x00010000,
817 __NET_XMIT_BYPASS = 0x00020000,
818};
819
820#ifdef CONFIG_NET_CLS_ACT
821#define net_xmit_drop_count(e) ((e) & __NET_XMIT_STOLEN ? 0 : 1)
822#else
823#define net_xmit_drop_count(e) (1)
824#endif
825
826static inline void qdisc_calculate_pkt_len(struct sk_buff *skb,
827 const struct Qdisc *sch)
828{
829#ifdef CONFIG_NET_SCHED
830 struct qdisc_size_table *stab = rcu_dereference_bh(sch->stab);
831
832 if (stab)
833 __qdisc_calculate_pkt_len(skb, stab);
834#endif
835}
836
837static inline int qdisc_enqueue(struct sk_buff *skb, struct Qdisc *sch,
838 struct sk_buff **to_free)
839{
840 qdisc_calculate_pkt_len(skb, sch);
841 return sch->enqueue(skb, sch, to_free);
842}
843
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000844static inline void _bstats_update(struct gnet_stats_basic_packed *bstats,
845 __u64 bytes, __u32 packets)
846{
847 bstats->bytes += bytes;
848 bstats->packets += packets;
849}
850
851static inline void bstats_update(struct gnet_stats_basic_packed *bstats,
852 const struct sk_buff *skb)
853{
854 _bstats_update(bstats,
855 qdisc_pkt_len(skb),
856 skb_is_gso(skb) ? skb_shinfo(skb)->gso_segs : 1);
857}
858
859static inline void _bstats_cpu_update(struct gnet_stats_basic_cpu *bstats,
860 __u64 bytes, __u32 packets)
861{
862 u64_stats_update_begin(&bstats->syncp);
863 _bstats_update(&bstats->bstats, bytes, packets);
864 u64_stats_update_end(&bstats->syncp);
865}
866
867static inline void bstats_cpu_update(struct gnet_stats_basic_cpu *bstats,
868 const struct sk_buff *skb)
869{
870 u64_stats_update_begin(&bstats->syncp);
871 bstats_update(&bstats->bstats, skb);
872 u64_stats_update_end(&bstats->syncp);
873}
874
875static inline void qdisc_bstats_cpu_update(struct Qdisc *sch,
876 const struct sk_buff *skb)
877{
878 bstats_cpu_update(this_cpu_ptr(sch->cpu_bstats), skb);
879}
880
881static inline void qdisc_bstats_update(struct Qdisc *sch,
882 const struct sk_buff *skb)
883{
884 bstats_update(&sch->bstats, skb);
885}
886
887static inline void qdisc_qstats_backlog_dec(struct Qdisc *sch,
888 const struct sk_buff *skb)
889{
890 sch->qstats.backlog -= qdisc_pkt_len(skb);
891}
892
893static inline void qdisc_qstats_cpu_backlog_dec(struct Qdisc *sch,
894 const struct sk_buff *skb)
895{
896 this_cpu_sub(sch->cpu_qstats->backlog, qdisc_pkt_len(skb));
897}
898
899static inline void qdisc_qstats_backlog_inc(struct Qdisc *sch,
900 const struct sk_buff *skb)
901{
902 sch->qstats.backlog += qdisc_pkt_len(skb);
903}
904
905static inline void qdisc_qstats_cpu_backlog_inc(struct Qdisc *sch,
906 const struct sk_buff *skb)
907{
908 this_cpu_add(sch->cpu_qstats->backlog, qdisc_pkt_len(skb));
909}
910
911static inline void qdisc_qstats_cpu_qlen_inc(struct Qdisc *sch)
912{
913 this_cpu_inc(sch->cpu_qstats->qlen);
914}
915
916static inline void qdisc_qstats_cpu_qlen_dec(struct Qdisc *sch)
917{
918 this_cpu_dec(sch->cpu_qstats->qlen);
919}
920
921static inline void qdisc_qstats_cpu_requeues_inc(struct Qdisc *sch)
922{
923 this_cpu_inc(sch->cpu_qstats->requeues);
924}
925
926static inline void __qdisc_qstats_drop(struct Qdisc *sch, int count)
927{
928 sch->qstats.drops += count;
929}
930
931static inline void qstats_drop_inc(struct gnet_stats_queue *qstats)
932{
933 qstats->drops++;
934}
935
936static inline void qstats_overlimit_inc(struct gnet_stats_queue *qstats)
937{
938 qstats->overlimits++;
939}
940
941static inline void qdisc_qstats_drop(struct Qdisc *sch)
942{
943 qstats_drop_inc(&sch->qstats);
944}
945
946static inline void qdisc_qstats_cpu_drop(struct Qdisc *sch)
947{
948 this_cpu_inc(sch->cpu_qstats->drops);
949}
950
951static inline void qdisc_qstats_overlimit(struct Qdisc *sch)
952{
953 sch->qstats.overlimits++;
954}
955
David Brazdil0f672f62019-12-10 10:32:29 +0000956static inline int qdisc_qstats_copy(struct gnet_dump *d, struct Qdisc *sch)
957{
958 __u32 qlen = qdisc_qlen_sum(sch);
959
960 return gnet_stats_copy_queue(d, sch->cpu_qstats, &sch->qstats, qlen);
961}
962
963static inline void qdisc_qstats_qlen_backlog(struct Qdisc *sch, __u32 *qlen,
964 __u32 *backlog)
965{
966 struct gnet_stats_queue qstats = { 0 };
967 __u32 len = qdisc_qlen_sum(sch);
968
969 __gnet_stats_copy_queue(&qstats, sch->cpu_qstats, &sch->qstats, len);
970 *qlen = qstats.qlen;
971 *backlog = qstats.backlog;
972}
973
974static inline void qdisc_tree_flush_backlog(struct Qdisc *sch)
975{
976 __u32 qlen, backlog;
977
978 qdisc_qstats_qlen_backlog(sch, &qlen, &backlog);
979 qdisc_tree_reduce_backlog(sch, qlen, backlog);
980}
981
982static inline void qdisc_purge_queue(struct Qdisc *sch)
983{
984 __u32 qlen, backlog;
985
986 qdisc_qstats_qlen_backlog(sch, &qlen, &backlog);
987 qdisc_reset(sch);
988 qdisc_tree_reduce_backlog(sch, qlen, backlog);
989}
990
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000991static inline void qdisc_skb_head_init(struct qdisc_skb_head *qh)
992{
993 qh->head = NULL;
994 qh->tail = NULL;
995 qh->qlen = 0;
996}
997
David Brazdil0f672f62019-12-10 10:32:29 +0000998static inline void __qdisc_enqueue_tail(struct sk_buff *skb,
999 struct qdisc_skb_head *qh)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001000{
1001 struct sk_buff *last = qh->tail;
1002
1003 if (last) {
1004 skb->next = NULL;
1005 last->next = skb;
1006 qh->tail = skb;
1007 } else {
1008 qh->tail = skb;
1009 qh->head = skb;
1010 }
1011 qh->qlen++;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001012}
1013
1014static inline int qdisc_enqueue_tail(struct sk_buff *skb, struct Qdisc *sch)
1015{
David Brazdil0f672f62019-12-10 10:32:29 +00001016 __qdisc_enqueue_tail(skb, &sch->q);
1017 qdisc_qstats_backlog_inc(sch, skb);
1018 return NET_XMIT_SUCCESS;
1019}
1020
1021static inline void __qdisc_enqueue_head(struct sk_buff *skb,
1022 struct qdisc_skb_head *qh)
1023{
1024 skb->next = qh->head;
1025
1026 if (!qh->head)
1027 qh->tail = skb;
1028 qh->head = skb;
1029 qh->qlen++;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001030}
1031
1032static inline struct sk_buff *__qdisc_dequeue_head(struct qdisc_skb_head *qh)
1033{
1034 struct sk_buff *skb = qh->head;
1035
1036 if (likely(skb != NULL)) {
1037 qh->head = skb->next;
1038 qh->qlen--;
1039 if (qh->head == NULL)
1040 qh->tail = NULL;
1041 skb->next = NULL;
1042 }
1043
1044 return skb;
1045}
1046
1047static inline struct sk_buff *qdisc_dequeue_head(struct Qdisc *sch)
1048{
1049 struct sk_buff *skb = __qdisc_dequeue_head(&sch->q);
1050
1051 if (likely(skb != NULL)) {
1052 qdisc_qstats_backlog_dec(sch, skb);
1053 qdisc_bstats_update(sch, skb);
1054 }
1055
1056 return skb;
1057}
1058
1059/* Instead of calling kfree_skb() while root qdisc lock is held,
1060 * queue the skb for future freeing at end of __dev_xmit_skb()
1061 */
1062static inline void __qdisc_drop(struct sk_buff *skb, struct sk_buff **to_free)
1063{
1064 skb->next = *to_free;
1065 *to_free = skb;
1066}
1067
1068static inline void __qdisc_drop_all(struct sk_buff *skb,
1069 struct sk_buff **to_free)
1070{
1071 if (skb->prev)
1072 skb->prev->next = *to_free;
1073 else
1074 skb->next = *to_free;
1075 *to_free = skb;
1076}
1077
1078static inline unsigned int __qdisc_queue_drop_head(struct Qdisc *sch,
1079 struct qdisc_skb_head *qh,
1080 struct sk_buff **to_free)
1081{
1082 struct sk_buff *skb = __qdisc_dequeue_head(qh);
1083
1084 if (likely(skb != NULL)) {
1085 unsigned int len = qdisc_pkt_len(skb);
1086
1087 qdisc_qstats_backlog_dec(sch, skb);
1088 __qdisc_drop(skb, to_free);
1089 return len;
1090 }
1091
1092 return 0;
1093}
1094
1095static inline unsigned int qdisc_queue_drop_head(struct Qdisc *sch,
1096 struct sk_buff **to_free)
1097{
1098 return __qdisc_queue_drop_head(sch, &sch->q, to_free);
1099}
1100
1101static inline struct sk_buff *qdisc_peek_head(struct Qdisc *sch)
1102{
1103 const struct qdisc_skb_head *qh = &sch->q;
1104
1105 return qh->head;
1106}
1107
1108/* generic pseudo peek method for non-work-conserving qdisc */
1109static inline struct sk_buff *qdisc_peek_dequeued(struct Qdisc *sch)
1110{
1111 struct sk_buff *skb = skb_peek(&sch->gso_skb);
1112
1113 /* we can reuse ->gso_skb because peek isn't called for root qdiscs */
1114 if (!skb) {
1115 skb = sch->dequeue(sch);
1116
1117 if (skb) {
1118 __skb_queue_head(&sch->gso_skb, skb);
1119 /* it's still part of the queue */
1120 qdisc_qstats_backlog_inc(sch, skb);
1121 sch->q.qlen++;
1122 }
1123 }
1124
1125 return skb;
1126}
1127
David Brazdil0f672f62019-12-10 10:32:29 +00001128static inline void qdisc_update_stats_at_dequeue(struct Qdisc *sch,
1129 struct sk_buff *skb)
1130{
1131 if (qdisc_is_percpu_stats(sch)) {
1132 qdisc_qstats_cpu_backlog_dec(sch, skb);
1133 qdisc_bstats_cpu_update(sch, skb);
1134 qdisc_qstats_cpu_qlen_dec(sch);
1135 } else {
1136 qdisc_qstats_backlog_dec(sch, skb);
1137 qdisc_bstats_update(sch, skb);
1138 sch->q.qlen--;
1139 }
1140}
1141
1142static inline void qdisc_update_stats_at_enqueue(struct Qdisc *sch,
1143 unsigned int pkt_len)
1144{
1145 if (qdisc_is_percpu_stats(sch)) {
1146 qdisc_qstats_cpu_qlen_inc(sch);
1147 this_cpu_add(sch->cpu_qstats->backlog, pkt_len);
1148 } else {
1149 sch->qstats.backlog += pkt_len;
1150 sch->q.qlen++;
1151 }
1152}
1153
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001154/* use instead of qdisc->dequeue() for all qdiscs queried with ->peek() */
1155static inline struct sk_buff *qdisc_dequeue_peeked(struct Qdisc *sch)
1156{
1157 struct sk_buff *skb = skb_peek(&sch->gso_skb);
1158
1159 if (skb) {
1160 skb = __skb_dequeue(&sch->gso_skb);
David Brazdil0f672f62019-12-10 10:32:29 +00001161 if (qdisc_is_percpu_stats(sch)) {
1162 qdisc_qstats_cpu_backlog_dec(sch, skb);
1163 qdisc_qstats_cpu_qlen_dec(sch);
1164 } else {
1165 qdisc_qstats_backlog_dec(sch, skb);
1166 sch->q.qlen--;
1167 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001168 } else {
1169 skb = sch->dequeue(sch);
1170 }
1171
1172 return skb;
1173}
1174
1175static inline void __qdisc_reset_queue(struct qdisc_skb_head *qh)
1176{
1177 /*
1178 * We do not know the backlog in bytes of this list, it
1179 * is up to the caller to correct it
1180 */
1181 ASSERT_RTNL();
1182 if (qh->qlen) {
1183 rtnl_kfree_skbs(qh->head, qh->tail);
1184
1185 qh->head = NULL;
1186 qh->tail = NULL;
1187 qh->qlen = 0;
1188 }
1189}
1190
1191static inline void qdisc_reset_queue(struct Qdisc *sch)
1192{
1193 __qdisc_reset_queue(&sch->q);
1194 sch->qstats.backlog = 0;
1195}
1196
1197static inline struct Qdisc *qdisc_replace(struct Qdisc *sch, struct Qdisc *new,
1198 struct Qdisc **pold)
1199{
1200 struct Qdisc *old;
1201
1202 sch_tree_lock(sch);
1203 old = *pold;
1204 *pold = new;
David Brazdil0f672f62019-12-10 10:32:29 +00001205 if (old != NULL)
Olivier Deprez0e641232021-09-23 10:07:05 +02001206 qdisc_purge_queue(old);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001207 sch_tree_unlock(sch);
1208
1209 return old;
1210}
1211
1212static inline void rtnl_qdisc_drop(struct sk_buff *skb, struct Qdisc *sch)
1213{
1214 rtnl_kfree_skbs(skb, skb);
1215 qdisc_qstats_drop(sch);
1216}
1217
1218static inline int qdisc_drop_cpu(struct sk_buff *skb, struct Qdisc *sch,
1219 struct sk_buff **to_free)
1220{
1221 __qdisc_drop(skb, to_free);
1222 qdisc_qstats_cpu_drop(sch);
1223
1224 return NET_XMIT_DROP;
1225}
1226
1227static inline int qdisc_drop(struct sk_buff *skb, struct Qdisc *sch,
1228 struct sk_buff **to_free)
1229{
1230 __qdisc_drop(skb, to_free);
1231 qdisc_qstats_drop(sch);
1232
1233 return NET_XMIT_DROP;
1234}
1235
1236static inline int qdisc_drop_all(struct sk_buff *skb, struct Qdisc *sch,
1237 struct sk_buff **to_free)
1238{
1239 __qdisc_drop_all(skb, to_free);
1240 qdisc_qstats_drop(sch);
1241
1242 return NET_XMIT_DROP;
1243}
1244
1245/* Length to Time (L2T) lookup in a qdisc_rate_table, to determine how
1246 long it will take to send a packet given its size.
1247 */
1248static inline u32 qdisc_l2t(struct qdisc_rate_table* rtab, unsigned int pktlen)
1249{
1250 int slot = pktlen + rtab->rate.cell_align + rtab->rate.overhead;
1251 if (slot < 0)
1252 slot = 0;
1253 slot >>= rtab->rate.cell_log;
1254 if (slot > 255)
1255 return rtab->data[255]*(slot >> 8) + rtab->data[slot & 0xFF];
1256 return rtab->data[slot];
1257}
1258
1259struct psched_ratecfg {
1260 u64 rate_bytes_ps; /* bytes per second */
1261 u32 mult;
1262 u16 overhead;
1263 u8 linklayer;
1264 u8 shift;
1265};
1266
1267static inline u64 psched_l2t_ns(const struct psched_ratecfg *r,
1268 unsigned int len)
1269{
1270 len += r->overhead;
1271
1272 if (unlikely(r->linklayer == TC_LINKLAYER_ATM))
1273 return ((u64)(DIV_ROUND_UP(len,48)*53) * r->mult) >> r->shift;
1274
1275 return ((u64)len * r->mult) >> r->shift;
1276}
1277
1278void psched_ratecfg_precompute(struct psched_ratecfg *r,
1279 const struct tc_ratespec *conf,
1280 u64 rate64);
1281
1282static inline void psched_ratecfg_getrate(struct tc_ratespec *res,
1283 const struct psched_ratecfg *r)
1284{
1285 memset(res, 0, sizeof(*res));
1286
1287 /* legacy struct tc_ratespec has a 32bit @rate field
1288 * Qdisc using 64bit rate should add new attributes
1289 * in order to maintain compatibility.
1290 */
1291 res->rate = min_t(u64, r->rate_bytes_ps, ~0U);
1292
1293 res->overhead = r->overhead;
1294 res->linklayer = (r->linklayer & TC_LINKLAYER_MASK);
1295}
1296
1297/* Mini Qdisc serves for specific needs of ingress/clsact Qdisc.
1298 * The fast path only needs to access filter list and to update stats
1299 */
1300struct mini_Qdisc {
1301 struct tcf_proto *filter_list;
1302 struct gnet_stats_basic_cpu __percpu *cpu_bstats;
1303 struct gnet_stats_queue __percpu *cpu_qstats;
1304 struct rcu_head rcu;
1305};
1306
1307static inline void mini_qdisc_bstats_cpu_update(struct mini_Qdisc *miniq,
1308 const struct sk_buff *skb)
1309{
1310 bstats_cpu_update(this_cpu_ptr(miniq->cpu_bstats), skb);
1311}
1312
1313static inline void mini_qdisc_qstats_cpu_drop(struct mini_Qdisc *miniq)
1314{
1315 this_cpu_inc(miniq->cpu_qstats->drops);
1316}
1317
1318struct mini_Qdisc_pair {
1319 struct mini_Qdisc miniq1;
1320 struct mini_Qdisc miniq2;
1321 struct mini_Qdisc __rcu **p_miniq;
1322};
1323
1324void mini_qdisc_pair_swap(struct mini_Qdisc_pair *miniqp,
1325 struct tcf_proto *tp_head);
1326void mini_qdisc_pair_init(struct mini_Qdisc_pair *miniqp, struct Qdisc *qdisc,
1327 struct mini_Qdisc __rcu **p_miniq);
1328
1329static inline void skb_tc_reinsert(struct sk_buff *skb, struct tcf_result *res)
1330{
1331 struct gnet_stats_queue *stats = res->qstats;
1332 int ret;
1333
1334 if (res->ingress)
1335 ret = netif_receive_skb(skb);
1336 else
1337 ret = dev_queue_xmit(skb);
1338 if (ret && stats)
1339 qstats_overlimit_inc(res->qstats);
1340}
1341
1342#endif