Merge tag 'pci-v6.16-fixes-3' of git://git.kernel.org/pub/scm/linux/kernel/git/pci/pci
[linux-2.6-block.git] / include / net / sch_generic.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
1da177e4
LT
2#ifndef __NET_SCHED_GENERIC_H
3#define __NET_SCHED_GENERIC_H
4
1da177e4
LT
5#include <linux/netdevice.h>
6#include <linux/types.h>
7#include <linux/rcupdate.h>
1da177e4
LT
8#include <linux/pkt_sched.h>
9#include <linux/pkt_cls.h>
22e0f8b9 10#include <linux/percpu.h>
5772e9a3 11#include <linux/dynamic_queue_limits.h>
5bc17018 12#include <linux/list.h>
7b936405 13#include <linux/refcount.h>
7aa0045d 14#include <linux/workqueue.h>
c266f64d 15#include <linux/mutex.h>
4f8116c8 16#include <linux/rwsem.h>
97394bef 17#include <linux/atomic.h>
59eb87cb 18#include <linux/hashtable.h>
1da177e4 19#include <net/gen_stats.h>
be577ddc 20#include <net/rtnetlink.h>
a7323311 21#include <net/flow_offload.h>
913b47d3 22#include <linux/xarray.h>
1da177e4
LT
23
24struct Qdisc_ops;
25struct qdisc_walker;
26struct tcf_walker;
27struct module;
d58e468b 28struct bpf_flow_keys;
1da177e4 29
fd2c3ef7 30struct qdisc_rate_table {
1da177e4
LT
31 struct tc_ratespec rate;
32 u32 data[256];
33 struct qdisc_rate_table *next;
34 int refcnt;
35};
36
fd2c3ef7 37enum qdisc_state_t {
37437bb2 38 __QDISC_STATE_SCHED,
a9312ae8 39 __QDISC_STATE_DEACTIVATED,
a90c57f2 40 __QDISC_STATE_MISSED,
c4fef01b 41 __QDISC_STATE_DRAINING,
97604c65
ED
42};
43
44enum qdisc_state2_t {
29cbcd85
AD
45 /* Only for !TCQ_F_NOLOCK qdisc. Never access it directly.
46 * Use qdisc_run_begin/end() or qdisc_is_running() instead.
47 */
97604c65 48 __QDISC_STATE2_RUNNING,
e2627c8c
DM
49};
50
c4fef01b
YL
51#define QDISC_STATE_MISSED BIT(__QDISC_STATE_MISSED)
52#define QDISC_STATE_DRAINING BIT(__QDISC_STATE_DRAINING)
53
54#define QDISC_STATE_NON_EMPTY (QDISC_STATE_MISSED | \
55 QDISC_STATE_DRAINING)
56
175f9c1b 57struct qdisc_size_table {
a2da570d 58 struct rcu_head rcu;
175f9c1b
JK
59 struct list_head list;
60 struct tc_sizespec szopts;
61 int refcnt;
62 u16 data[];
63};
64
48da34b7
FW
65/* similar to sk_buff_head, but skb->prev pointer is undefined. */
66struct qdisc_skb_head {
67 struct sk_buff *head;
68 struct sk_buff *tail;
73eb628d 69 __u32 qlen;
48da34b7
FW
70 spinlock_t lock;
71};
72
fd2c3ef7 73struct Qdisc {
520ac30f
ED
74 int (*enqueue)(struct sk_buff *skb,
75 struct Qdisc *sch,
76 struct sk_buff **to_free);
77 struct sk_buff * (*dequeue)(struct Qdisc *sch);
05bdd2f1 78 unsigned int flags;
b00355db 79#define TCQ_F_BUILTIN 1
fd245a4a
ED
80#define TCQ_F_INGRESS 2
81#define TCQ_F_CAN_BYPASS 4
82#define TCQ_F_MQROOT 8
1abbe139
ED
83#define TCQ_F_ONETXQUEUE 0x10 /* dequeue_skb() can assume all skbs are for
84 * q->dev_queue : It can test
85 * netif_xmit_frozen_or_stopped() before
86 * dequeueing next packet.
87 * Its true for MQ/MQPRIO slaves, or non
88 * multiqueue device.
89 */
b00355db 90#define TCQ_F_WARN_NONWC (1 << 16)
22e0f8b9 91#define TCQ_F_CPUSTATS 0x20 /* run using percpu statistics */
4eaf3b84
ED
92#define TCQ_F_NOPARENT 0x40 /* root of its hierarchy :
93 * qdisc_tree_decrease_qlen() should stop.
94 */
49b49971 95#define TCQ_F_INVISIBLE 0x80 /* invisible by default in dump */
6b3ba914 96#define TCQ_F_NOLOCK 0x100 /* qdisc does not require locking */
7a4fa291 97#define TCQ_F_OFFLOADED 0x200 /* qdisc is offloaded to HW */
45203a3b 98 u32 limit;
05bdd2f1 99 const struct Qdisc_ops *ops;
a2da570d 100 struct qdisc_size_table __rcu *stab;
59cc1f61 101 struct hlist_node hash;
1da177e4
LT
102 u32 handle;
103 u32 parent;
72b25a91 104
5e140dfc 105 struct netdev_queue *dev_queue;
5e140dfc 106
1c0d32fd 107 struct net_rate_estimator __rcu *rate_est;
50dc9a85 108 struct gnet_stats_basic_sync __percpu *cpu_bstats;
0d32ef8c 109 struct gnet_stats_queue __percpu *cpu_qstats;
846e463a 110 int pad;
e9be0e99 111 refcount_t refcnt;
0d32ef8c 112
5e140dfc
ED
113 /*
114 * For performance sake on SMP, we put highly modified fields at the end
115 */
a53851e2 116 struct sk_buff_head gso_skb ____cacheline_aligned_in_smp;
48da34b7 117 struct qdisc_skb_head q;
50dc9a85 118 struct gnet_stats_basic_sync bstats;
0d32ef8c 119 struct gnet_stats_queue qstats;
0f022d32 120 int owner;
4d202a0d 121 unsigned long state;
97604c65 122 unsigned long state2; /* must be written under qdisc spinlock */
4d202a0d 123 struct Qdisc *next_sched;
70e57d5e 124 struct sk_buff_head skb_bad_txq;
45203a3b
ED
125
126 spinlock_t busylock ____cacheline_aligned_in_smp;
96009c7d 127 spinlock_t seqlock;
28cff537 128
3a7d0d07 129 struct rcu_head rcu;
606509f2 130 netdevice_tracker dev_tracker;
af0cb3fa 131 struct lock_class_key root_lock_key;
846e463a
ED
132 /* private data */
133 long privdata[] ____cacheline_aligned;
1da177e4
LT
134};
135
551143d8
ED
136static inline void qdisc_refcount_inc(struct Qdisc *qdisc)
137{
138 if (qdisc->flags & TCQ_F_BUILTIN)
139 return;
140 refcount_inc(&qdisc->refcnt);
141}
142
84ad0af0
PY
143static inline bool qdisc_refcount_dec_if_one(struct Qdisc *qdisc)
144{
145 if (qdisc->flags & TCQ_F_BUILTIN)
146 return true;
147 return refcount_dec_if_one(&qdisc->refcnt);
148}
149
9d7e82ce
VB
150/* Intended to be used by unlocked users, when concurrent qdisc release is
151 * possible.
152 */
153
154static inline struct Qdisc *qdisc_refcount_inc_nz(struct Qdisc *qdisc)
155{
156 if (qdisc->flags & TCQ_F_BUILTIN)
157 return qdisc;
158 if (refcount_inc_not_zero(&qdisc->refcnt))
159 return qdisc;
160 return NULL;
161}
162
29cbcd85
AD
163/* For !TCQ_F_NOLOCK qdisc: callers must either call this within a qdisc
164 * root_lock section, or provide their own memory barriers -- ordering
165 * against qdisc_run_begin/end() atomic bit operations.
166 */
96009c7d 167static inline bool qdisc_is_running(struct Qdisc *qdisc)
bc135b23 168{
32f7b44d 169 if (qdisc->flags & TCQ_F_NOLOCK)
96009c7d 170 return spin_is_locked(&qdisc->seqlock);
97604c65 171 return test_bit(__QDISC_STATE2_RUNNING, &qdisc->state2);
bc135b23
ED
172}
173
c4fef01b
YL
174static inline bool nolock_qdisc_is_empty(const struct Qdisc *qdisc)
175{
176 return !(READ_ONCE(qdisc->state) & QDISC_STATE_NON_EMPTY);
177}
178
9c01c9f1
PA
179static inline bool qdisc_is_percpu_stats(const struct Qdisc *q)
180{
181 return q->flags & TCQ_F_CPUSTATS;
182}
183
28cff537
PA
184static inline bool qdisc_is_empty(const struct Qdisc *qdisc)
185{
9c01c9f1 186 if (qdisc_is_percpu_stats(qdisc))
d3e0f575 187 return nolock_qdisc_is_empty(qdisc);
90b2be27 188 return !READ_ONCE(qdisc->q.qlen);
28cff537
PA
189}
190
29cbcd85
AD
191/* For !TCQ_F_NOLOCK qdisc, qdisc_run_begin/end() must be invoked with
192 * the qdisc root lock acquired.
193 */
bc135b23
ED
194static inline bool qdisc_run_begin(struct Qdisc *qdisc)
195{
32f7b44d 196 if (qdisc->flags & TCQ_F_NOLOCK) {
a90c57f2 197 if (spin_trylock(&qdisc->seqlock))
d3e0f575 198 return true;
a90c57f2 199
a54ce370
VR
200 /* No need to insist if the MISSED flag was already set.
201 * Note that test_and_set_bit() also gives us memory ordering
202 * guarantees wrt potential earlier enqueue() and below
203 * spin_trylock(), both of which are necessary to prevent races
89837eb4 204 */
a54ce370 205 if (test_and_set_bit(__QDISC_STATE_MISSED, &qdisc->state))
a90c57f2
YL
206 return false;
207
a54ce370
VR
208 /* Try to take the lock again to make sure that we will either
209 * grab it or the CPU that still has it will see MISSED set
210 * when testing it in qdisc_run_end()
a90c57f2 211 */
d3e0f575 212 return spin_trylock(&qdisc->seqlock);
32f7b44d 213 }
97604c65 214 return !__test_and_set_bit(__QDISC_STATE2_RUNNING, &qdisc->state2);
bc135b23
ED
215}
216
217static inline void qdisc_run_end(struct Qdisc *qdisc)
218{
a90c57f2 219 if (qdisc->flags & TCQ_F_NOLOCK) {
96009c7d 220 spin_unlock(&qdisc->seqlock);
a90c57f2 221
2e8728c9
GF
222 /* spin_unlock() only has store-release semantic. The unlock
223 * and test_bit() ordering is a store-load ordering, so a full
224 * memory barrier is needed here.
225 */
226 smp_mb();
227
a90c57f2 228 if (unlikely(test_bit(__QDISC_STATE_MISSED,
c4fef01b 229 &qdisc->state)))
a90c57f2 230 __netif_schedule(qdisc);
dd25296a 231 } else {
97604c65 232 __clear_bit(__QDISC_STATE2_RUNNING, &qdisc->state2);
a90c57f2 233 }
fd245a4a
ED
234}
235
5772e9a3
JDB
236static inline bool qdisc_may_bulk(const struct Qdisc *qdisc)
237{
238 return qdisc->flags & TCQ_F_ONETXQUEUE;
239}
240
241static inline int qdisc_avail_bulklimit(const struct netdev_queue *txq)
242{
eb2c11b2 243 return netdev_queue_dql_avail(txq);
5772e9a3
JDB
244}
245
fd2c3ef7 246struct Qdisc_class_ops {
dfcd2a2b 247 unsigned int flags;
1da177e4 248 /* Child qdisc manipulation */
926e61b7 249 struct netdev_queue * (*select_queue)(struct Qdisc *, struct tcmsg *);
1da177e4 250 int (*graft)(struct Qdisc *, unsigned long cl,
653d6fd6
AA
251 struct Qdisc *, struct Qdisc **,
252 struct netlink_ext_ack *extack);
1da177e4 253 struct Qdisc * (*leaf)(struct Qdisc *, unsigned long cl);
43effa1e 254 void (*qlen_notify)(struct Qdisc *, unsigned long);
1da177e4
LT
255
256 /* Class manipulation routines */
143976ce 257 unsigned long (*find)(struct Qdisc *, u32 classid);
1da177e4 258 int (*change)(struct Qdisc *, u32, u32,
793d81d6
AA
259 struct nlattr **, unsigned long *,
260 struct netlink_ext_ack *);
4dd78a73
MM
261 int (*delete)(struct Qdisc *, unsigned long,
262 struct netlink_ext_ack *);
1da177e4
LT
263 void (*walk)(struct Qdisc *, struct qdisc_walker * arg);
264
265 /* Filter manipulation */
0ac4bd68 266 struct tcf_block * (*tcf_block)(struct Qdisc *sch,
cbaacc4e
AA
267 unsigned long arg,
268 struct netlink_ext_ack *extack);
1da177e4
LT
269 unsigned long (*bind_tcf)(struct Qdisc *, unsigned long,
270 u32 classid);
271 void (*unbind_tcf)(struct Qdisc *, unsigned long);
272
273 /* rtnetlink specific */
274 int (*dump)(struct Qdisc *, unsigned long,
275 struct sk_buff *skb, struct tcmsg*);
276 int (*dump_stats)(struct Qdisc *, unsigned long,
277 struct gnet_dump *);
278};
279
dfcd2a2b
VB
280/* Qdisc_class_ops flag values */
281
282/* Implements API that doesn't require rtnl lock */
283enum qdisc_class_ops_flags {
284 QDISC_CLASS_OPS_DOIT_UNLOCKED = 1,
285};
286
fd2c3ef7 287struct Qdisc_ops {
1da177e4 288 struct Qdisc_ops *next;
20fea08b 289 const struct Qdisc_class_ops *cl_ops;
1da177e4
LT
290 char id[IFNAMSIZ];
291 int priv_size;
d59f5ffa 292 unsigned int static_flags;
1da177e4 293
520ac30f
ED
294 int (*enqueue)(struct sk_buff *skb,
295 struct Qdisc *sch,
296 struct sk_buff **to_free);
1da177e4 297 struct sk_buff * (*dequeue)(struct Qdisc *);
90d841fd 298 struct sk_buff * (*peek)(struct Qdisc *);
1da177e4 299
e63d7dfd
AA
300 int (*init)(struct Qdisc *sch, struct nlattr *arg,
301 struct netlink_ext_ack *extack);
1da177e4
LT
302 void (*reset)(struct Qdisc *);
303 void (*destroy)(struct Qdisc *);
0ac4bd68 304 int (*change)(struct Qdisc *sch,
2030721c
AA
305 struct nlattr *arg,
306 struct netlink_ext_ack *extack);
0ac4bd68 307 void (*attach)(struct Qdisc *sch);
48bfd55e 308 int (*change_tx_queue_len)(struct Qdisc *, unsigned int);
1e080f17
JK
309 void (*change_real_num_tx)(struct Qdisc *sch,
310 unsigned int new_real_tx);
1da177e4
LT
311
312 int (*dump)(struct Qdisc *, struct sk_buff *);
313 int (*dump_stats)(struct Qdisc *, struct gnet_dump *);
314
d47a6b0e
JP
315 void (*ingress_block_set)(struct Qdisc *sch,
316 u32 block_index);
317 void (*egress_block_set)(struct Qdisc *sch,
318 u32 block_index);
319 u32 (*ingress_block_get)(struct Qdisc *sch);
320 u32 (*egress_block_get)(struct Qdisc *sch);
321
1da177e4
LT
322 struct module *owner;
323};
324
fd2c3ef7 325struct tcf_result {
db50514f
JP
326 union {
327 struct {
328 unsigned long class;
329 u32 classid;
330 };
331 const struct tcf_proto *goto_tp;
332 };
1da177e4
LT
333};
334
9f407f17
JP
335struct tcf_chain;
336
fd2c3ef7 337struct tcf_proto_ops {
36272874 338 struct list_head head;
1da177e4
LT
339 char kind[IFNAMSIZ];
340
dc7f9f6e
ED
341 int (*classify)(struct sk_buff *,
342 const struct tcf_proto *,
343 struct tcf_result *);
1da177e4 344 int (*init)(struct tcf_proto*);
12db03b6 345 void (*destroy)(struct tcf_proto *tp, bool rtnl_held,
715df5ec 346 struct netlink_ext_ack *extack);
1da177e4 347
8113c095 348 void* (*get)(struct tcf_proto*, u32 handle);
7d5509fa 349 void (*put)(struct tcf_proto *tp, void *f);
c1b52739 350 int (*change)(struct net *net, struct sk_buff *,
af4c6641 351 struct tcf_proto*, unsigned long,
add93b61 352 u32 handle, struct nlattr **,
695176bf 353 void **, u32,
7306db38 354 struct netlink_ext_ack *);
8865fdd4 355 int (*delete)(struct tcf_proto *tp, void *arg,
12db03b6 356 bool *last, bool rtnl_held,
571acf21 357 struct netlink_ext_ack *);
a5b72a08 358 bool (*delete_empty)(struct tcf_proto *tp);
12db03b6
VB
359 void (*walk)(struct tcf_proto *tp,
360 struct tcf_walker *arg, bool rtnl_held);
e56185c7 361 int (*reoffload)(struct tcf_proto *tp, bool add,
a7323311 362 flow_setup_cb_t *cb, void *cb_priv,
e56185c7 363 struct netlink_ext_ack *extack);
a449a3e7
VB
364 void (*hw_add)(struct tcf_proto *tp,
365 void *type_data);
366 void (*hw_del)(struct tcf_proto *tp,
367 void *type_data);
2e24cd75
CW
368 void (*bind_class)(void *, u32, unsigned long,
369 void *, unsigned long);
9f407f17
JP
370 void * (*tmplt_create)(struct net *net,
371 struct tcf_chain *chain,
372 struct nlattr **tca,
373 struct netlink_ext_ack *extack);
374 void (*tmplt_destroy)(void *tmplt_priv);
32f2a0af
IS
375 void (*tmplt_reoffload)(struct tcf_chain *chain,
376 bool add,
377 flow_setup_cb_t *cb,
378 void *cb_priv);
80cd22c3
PB
379 struct tcf_exts * (*get_exts)(const struct tcf_proto *tp,
380 u32 handle);
1da177e4
LT
381
382 /* rtnetlink specific */
8113c095 383 int (*dump)(struct net*, struct tcf_proto*, void *,
12db03b6
VB
384 struct sk_buff *skb, struct tcmsg*,
385 bool);
f8ab1807
VB
386 int (*terse_dump)(struct net *net,
387 struct tcf_proto *tp, void *fh,
388 struct sk_buff *skb,
389 struct tcmsg *t, bool rtnl_held);
9f407f17
JP
390 int (*tmplt_dump)(struct sk_buff *skb,
391 struct net *net,
392 void *tmplt_priv);
1da177e4
LT
393
394 struct module *owner;
12db03b6
VB
395 int flags;
396};
397
a5b72a08
DC
398/* Classifiers setting TCF_PROTO_OPS_DOIT_UNLOCKED in tcf_proto_ops->flags
399 * are expected to implement tcf_proto_ops->delete_empty(), otherwise race
400 * conditions can occur when filters are inserted/deleted simultaneously.
401 */
12db03b6
VB
402enum tcf_proto_ops_flags {
403 TCF_PROTO_OPS_DOIT_UNLOCKED = 1,
1da177e4
LT
404};
405
fd2c3ef7 406struct tcf_proto {
1da177e4 407 /* Fast access part */
25d8c0d5
JF
408 struct tcf_proto __rcu *next;
409 void __rcu *root;
7fd4b288
PA
410
411 /* called under RCU BH lock*/
dc7f9f6e
ED
412 int (*classify)(struct sk_buff *,
413 const struct tcf_proto *,
414 struct tcf_result *);
66c6f529 415 __be16 protocol;
1da177e4
LT
416
417 /* All the rest */
418 u32 prio;
1da177e4 419 void *data;
dc7f9f6e 420 const struct tcf_proto_ops *ops;
5bc17018 421 struct tcf_chain *chain;
8b64678e
VB
422 /* Lock protects tcf_proto shared state and can be used by unlocked
423 * classifiers to protect their private data.
424 */
425 spinlock_t lock;
426 bool deleting;
2081fd34 427 bool counted;
a12c76a0 428 bool usesw;
4dbfa766 429 refcount_t refcnt;
25d8c0d5 430 struct rcu_head rcu;
59eb87cb 431 struct hlist_node destroy_ht_node;
1da177e4
LT
432};
433
175f9c1b 434struct qdisc_skb_cb {
089b19a9
SF
435 struct {
436 unsigned int pkt_len;
437 u16 slave_dev_queue_mapping;
438 u16 tc_classid;
d58e468b 439 };
25711786
ED
440#define QDISC_CB_PRIV_LEN 20
441 unsigned char data[QDISC_CB_PRIV_LEN];
175f9c1b
JK
442};
443
c7eb7d72
JP
444typedef void tcf_chain_head_change_t(struct tcf_proto *tp_head, void *priv);
445
2190d1d0 446struct tcf_chain {
ed76f5ed
VB
447 /* Protects filter_chain. */
448 struct mutex filter_chain_lock;
2190d1d0 449 struct tcf_proto __rcu *filter_chain;
5bc17018
JP
450 struct list_head list;
451 struct tcf_block *block;
452 u32 index; /* chain index */
453 unsigned int refcnt;
1f3ed383 454 unsigned int action_refcnt;
32a4f5ec 455 bool explicitly_created;
726d0612 456 bool flushing;
9f407f17
JP
457 const struct tcf_proto_ops *tmplt_ops;
458 void *tmplt_priv;
ee3bbfe8 459 struct rcu_head rcu;
6529eaba
JP
460};
461
2190d1d0 462struct tcf_block {
913b47d3 463 struct xarray ports; /* datapath accessible */
c266f64d
VB
464 /* Lock protects tcf_block and lifetime-management data of chains
465 * attached to the block (refcnt, action_refcnt, explicitly_created).
466 */
467 struct mutex lock;
5bc17018 468 struct list_head chain_list;
48617387 469 u32 index; /* block index for shared blocks */
a7df4870 470 u32 classid; /* which class this block belongs to */
cfebd7e2 471 refcount_t refcnt;
855319be 472 struct net *net;
69d78ef2 473 struct Qdisc *q;
4f8116c8 474 struct rw_semaphore cb_lock; /* protects cb_list and offload counters */
14bfb13f 475 struct flow_block flow_block;
f36fe1c4
JP
476 struct list_head owner_list;
477 bool keep_dst;
a12c76a0 478 atomic_t useswcnt;
97394bef 479 atomic_t offloadcnt; /* Number of oddloaded filters */
caa72601 480 unsigned int nooffloaddevcnt; /* Number of devs unable to do offload */
c9f14470 481 unsigned int lockeddevcnt; /* Number of devs that require rtnl lock. */
f71e0ca4
JP
482 struct {
483 struct tcf_chain *chain;
484 struct list_head filter_chain_list;
485 } chain0;
0607e439 486 struct rcu_head rcu;
59eb87cb
JH
487 DECLARE_HASHTABLE(proto_destroy_ht, 7);
488 struct mutex proto_destroy_lock; /* Lock for proto_destroy hashtable. */
2190d1d0
JP
489};
490
a7042cf8
VN
491struct tcf_block *tcf_block_lookup(struct net *net, u32 block_index);
492
ed76f5ed
VB
493static inline bool lockdep_tcf_chain_is_locked(struct tcf_chain *chain)
494{
495 return lockdep_is_held(&chain->filter_chain_lock);
496}
8b64678e
VB
497
498static inline bool lockdep_tcf_proto_is_locked(struct tcf_proto *tp)
499{
500 return lockdep_is_held(&tp->lock);
501}
ed76f5ed
VB
502
503#define tcf_chain_dereference(p, chain) \
504 rcu_dereference_protected(p, lockdep_tcf_chain_is_locked(chain))
505
8b64678e
VB
506#define tcf_proto_dereference(p, tp) \
507 rcu_dereference_protected(p, lockdep_tcf_proto_is_locked(tp))
508
16bda13d
DM
509static inline void qdisc_cb_private_validate(const struct sk_buff *skb, int sz)
510{
511 struct qdisc_skb_cb *qcb;
5ee31c68 512
038ebb1a 513 BUILD_BUG_ON(sizeof(skb->cb) < sizeof(*qcb));
16bda13d
DM
514 BUILD_BUG_ON(sizeof(qcb->data) < sz);
515}
516
05bdd2f1 517static inline int qdisc_qlen(const struct Qdisc *q)
bbd8a0d3
KK
518{
519 return q->q.qlen;
520}
521
73eb628d 522static inline int qdisc_qlen_sum(const struct Qdisc *q)
7e66016f 523{
73eb628d
PA
524 __u32 qlen = q->qstats.qlen;
525 int i;
7e66016f 526
73eb628d
PA
527 if (qdisc_is_percpu_stats(q)) {
528 for_each_possible_cpu(i)
529 qlen += per_cpu_ptr(q->cpu_qstats, i)->qlen;
530 } else {
6172abc1 531 qlen += q->q.qlen;
73eb628d 532 }
7e66016f
JF
533
534 return qlen;
535}
536
bfe0d029 537static inline struct qdisc_skb_cb *qdisc_skb_cb(const struct sk_buff *skb)
175f9c1b
JK
538{
539 return (struct qdisc_skb_cb *)skb->cb;
540}
541
83874000
DM
542static inline spinlock_t *qdisc_lock(struct Qdisc *qdisc)
543{
544 return &qdisc->q.lock;
545}
546
05bdd2f1 547static inline struct Qdisc *qdisc_root(const struct Qdisc *qdisc)
7698b4fc 548{
46e5da40
JF
549 struct Qdisc *q = rcu_dereference_rtnl(qdisc->dev_queue->qdisc);
550
551 return q;
7698b4fc
DM
552}
553
159d2c7d
ED
554static inline struct Qdisc *qdisc_root_bh(const struct Qdisc *qdisc)
555{
556 return rcu_dereference_bh(qdisc->dev_queue->qdisc);
557}
558
05bdd2f1 559static inline struct Qdisc *qdisc_root_sleeping(const struct Qdisc *qdisc)
2540e051 560{
d636fc5d 561 return rcu_dereference_rtnl(qdisc->dev_queue->qdisc_sleeping);
2540e051
JP
562}
563
05bdd2f1 564static inline spinlock_t *qdisc_root_sleeping_lock(const struct Qdisc *qdisc)
f6f9b93f
JP
565{
566 struct Qdisc *root = qdisc_root_sleeping(qdisc);
567
568 ASSERT_RTNL();
569 return qdisc_lock(root);
570}
571
05bdd2f1 572static inline struct net_device *qdisc_dev(const struct Qdisc *qdisc)
5ce2d488
DM
573{
574 return qdisc->dev_queue->dev;
575}
1da177e4 576
ca1e4ab1 577static inline void sch_tree_lock(struct Qdisc *q)
78a5b30b 578{
ca1e4ab1
MM
579 if (q->flags & TCQ_F_MQROOT)
580 spin_lock_bh(qdisc_lock(q));
581 else
582 spin_lock_bh(qdisc_root_sleeping_lock(q));
78a5b30b
DM
583}
584
ca1e4ab1 585static inline void sch_tree_unlock(struct Qdisc *q)
78a5b30b 586{
ca1e4ab1
MM
587 if (q->flags & TCQ_F_MQROOT)
588 spin_unlock_bh(qdisc_lock(q));
589 else
590 spin_unlock_bh(qdisc_root_sleeping_lock(q));
78a5b30b
DM
591}
592
e41a33e6
TG
593extern struct Qdisc noop_qdisc;
594extern struct Qdisc_ops noop_qdisc_ops;
6ec1c69a 595extern struct Qdisc_ops pfifo_fast_ops;
5579ee46 596extern const u8 sch_default_prio2band[TC_PRIO_MAX + 1];
6ec1c69a 597extern struct Qdisc_ops mq_qdisc_ops;
d66d6c31 598extern struct Qdisc_ops noqueue_qdisc_ops;
6da7c8fc 599extern const struct Qdisc_ops *default_qdisc_ops;
1f27cde3
ED
600static inline const struct Qdisc_ops *
601get_default_qdisc_ops(const struct net_device *dev, int ntx)
602{
603 return ntx < dev->real_num_tx_queues ?
604 default_qdisc_ops : &pfifo_fast_ops;
605}
e41a33e6 606
fd2c3ef7 607struct Qdisc_class_common {
6fe1c7a5 608 u32 classid;
8798481b 609 unsigned int filter_cnt;
6fe1c7a5
PM
610 struct hlist_node hnode;
611};
612
fd2c3ef7 613struct Qdisc_class_hash {
6fe1c7a5
PM
614 struct hlist_head *hash;
615 unsigned int hashsize;
616 unsigned int hashmask;
617 unsigned int hashelems;
618};
619
620static inline unsigned int qdisc_class_hash(u32 id, u32 mask)
621{
622 id ^= id >> 8;
623 id ^= id >> 4;
624 return id & mask;
625}
626
627static inline struct Qdisc_class_common *
05bdd2f1 628qdisc_class_find(const struct Qdisc_class_hash *hash, u32 id)
6fe1c7a5
PM
629{
630 struct Qdisc_class_common *cl;
6fe1c7a5
PM
631 unsigned int h;
632
7d3f0cd4
GF
633 if (!id)
634 return NULL;
635
6fe1c7a5 636 h = qdisc_class_hash(id, hash->hashmask);
b67bfe0d 637 hlist_for_each_entry(cl, &hash->hash[h], hnode) {
6fe1c7a5
PM
638 if (cl->classid == id)
639 return cl;
640 }
641 return NULL;
642}
643
8798481b
PT
644static inline bool qdisc_class_in_use(const struct Qdisc_class_common *cl)
645{
646 return cl->filter_cnt > 0;
647}
648
649static inline void qdisc_class_get(struct Qdisc_class_common *cl)
650{
651 unsigned int res;
652
653 if (check_add_overflow(cl->filter_cnt, 1, &res))
654 WARN(1, "Qdisc class overflow");
655
656 cl->filter_cnt = res;
657}
658
659static inline void qdisc_class_put(struct Qdisc_class_common *cl)
660{
661 unsigned int res;
662
663 if (check_sub_overflow(cl->filter_cnt, 1, &res))
664 WARN(1, "Qdisc class underflow");
665
666 cl->filter_cnt = res;
667}
668
384c181e
AN
669static inline int tc_classid_to_hwtc(struct net_device *dev, u32 classid)
670{
671 u32 hwtc = TC_H_MIN(classid) - TC_H_MIN_PRIORITY;
672
673 return (hwtc < netdev_get_num_tc(dev)) ? hwtc : -EINVAL;
674}
675
5c15257f
JP
676int qdisc_class_hash_init(struct Qdisc_class_hash *);
677void qdisc_class_hash_insert(struct Qdisc_class_hash *,
678 struct Qdisc_class_common *);
679void qdisc_class_hash_remove(struct Qdisc_class_hash *,
680 struct Qdisc_class_common *);
681void qdisc_class_hash_grow(struct Qdisc *, struct Qdisc_class_hash *);
682void qdisc_class_hash_destroy(struct Qdisc_class_hash *);
683
48bfd55e 684int dev_qdisc_change_tx_queue_len(struct net_device *dev);
1e080f17
JK
685void dev_qdisc_change_real_num_tx(struct net_device *dev,
686 unsigned int new_real_tx);
5c15257f
JP
687void dev_init_scheduler(struct net_device *dev);
688void dev_shutdown(struct net_device *dev);
689void dev_activate(struct net_device *dev);
690void dev_deactivate(struct net_device *dev);
691void dev_deactivate_many(struct list_head *head);
692struct Qdisc *dev_graft_qdisc(struct netdev_queue *dev_queue,
693 struct Qdisc *qdisc);
694void qdisc_reset(struct Qdisc *qdisc);
84ad0af0 695void qdisc_destroy(struct Qdisc *qdisc);
86bd446b 696void qdisc_put(struct Qdisc *qdisc);
3a7d0d07 697void qdisc_put_unlocked(struct Qdisc *qdisc);
5f2939d9 698void qdisc_tree_reduce_backlog(struct Qdisc *qdisc, int n, int len);
b592843c
JK
699#ifdef CONFIG_NET_SCHED
700int qdisc_offload_dump_helper(struct Qdisc *q, enum tc_setup_type type,
701 void *type_data);
bfaee911
JK
702void 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);
b592843c
JK
706#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}
bfaee911
JK
714
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}
b592843c 722#endif
aac4daa8
VO
723void qdisc_offload_query_caps(struct net_device *dev,
724 enum tc_setup_type type,
725 void *caps, size_t caps_len);
5c15257f 726struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue,
d0bd684d
AA
727 const struct Qdisc_ops *ops,
728 struct netlink_ext_ack *extack);
81d947e2 729void qdisc_free(struct Qdisc *qdisc);
5c15257f 730struct Qdisc *qdisc_create_dflt(struct netdev_queue *dev_queue,
a38a9882
AA
731 const struct Qdisc_ops *ops, u32 parentid,
732 struct netlink_ext_ack *extack);
5c15257f
JP
733void __qdisc_calculate_pkt_len(struct sk_buff *skb,
734 const struct qdisc_size_table *stab);
27b29f63 735int skb_do_redirect(struct sk_buff *);
1da177e4 736
fdc5432a
DB
737static inline bool skb_at_tc_ingress(const struct sk_buff *skb)
738{
e420bed0 739#ifdef CONFIG_NET_XGRESS
8dc07fdb 740 return skb->tc_at_ingress;
fdc5432a
DB
741#else
742 return false;
743#endif
744}
745
e7246e12
WB
746static inline bool skb_skip_tc_classify(struct sk_buff *skb)
747{
748#ifdef CONFIG_NET_CLS_ACT
749 if (skb->tc_skip_classify) {
750 skb->tc_skip_classify = 0;
751 return true;
752 }
753#endif
754 return false;
755}
756
3a053b1a 757/* Reset all TX qdiscs greater than index of a device. */
f0796d5c 758static inline void qdisc_reset_all_tx_gt(struct net_device *dev, unsigned int i)
5aa70995 759{
4ef6acff
JF
760 struct Qdisc *qdisc;
761
f0796d5c 762 for (; i < dev->num_tx_queues; i++) {
46e5da40 763 qdisc = rtnl_dereference(netdev_get_tx_queue(dev, i)->qdisc);
4ef6acff
JF
764 if (qdisc) {
765 spin_lock_bh(qdisc_lock(qdisc));
766 qdisc_reset(qdisc);
767 spin_unlock_bh(qdisc_lock(qdisc));
768 }
769 }
5aa70995
DM
770}
771
3e745dd6
DM
772/* Are all TX queues of the device empty? */
773static inline bool qdisc_all_tx_empty(const struct net_device *dev)
774{
e8a0464c 775 unsigned int i;
46e5da40
JF
776
777 rcu_read_lock();
e8a0464c
DM
778 for (i = 0; i < dev->num_tx_queues; i++) {
779 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
46e5da40 780 const struct Qdisc *q = rcu_dereference(txq->qdisc);
3e745dd6 781
1f5e6fdd 782 if (!qdisc_is_empty(q)) {
46e5da40 783 rcu_read_unlock();
e8a0464c 784 return false;
46e5da40 785 }
e8a0464c 786 }
46e5da40 787 rcu_read_unlock();
e8a0464c 788 return true;
3e745dd6
DM
789}
790
6fa9864b 791/* Are any of the TX qdiscs changing? */
05bdd2f1 792static inline bool qdisc_tx_changing(const struct net_device *dev)
6fa9864b 793{
e8a0464c 794 unsigned int i;
46e5da40 795
e8a0464c
DM
796 for (i = 0; i < dev->num_tx_queues; i++) {
797 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
d636fc5d
ED
798
799 if (rcu_access_pointer(txq->qdisc) !=
800 rcu_access_pointer(txq->qdisc_sleeping))
e8a0464c
DM
801 return true;
802 }
803 return false;
6fa9864b
DM
804}
805
34dd0fec
JDB
806/* "noqueue" qdisc identified by not having any enqueue, see noqueue_init() */
807static inline bool qdisc_txq_has_no_queue(const struct netdev_queue *txq)
808{
809 struct Qdisc *qdisc = rcu_access_pointer(txq->qdisc);
810
811 return qdisc->enqueue == NULL;
812}
813
e8a0464c 814/* Is the device using the noop qdisc on all queues? */
05297949
DM
815static inline bool qdisc_tx_is_noop(const struct net_device *dev)
816{
e8a0464c 817 unsigned int i;
46e5da40 818
e8a0464c
DM
819 for (i = 0; i < dev->num_tx_queues; i++) {
820 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
46e5da40 821 if (rcu_access_pointer(txq->qdisc) != &noop_qdisc)
e8a0464c
DM
822 return false;
823 }
824 return true;
05297949
DM
825}
826
bfe0d029 827static inline unsigned int qdisc_pkt_len(const struct sk_buff *skb)
0abf77e5 828{
175f9c1b 829 return qdisc_skb_cb(skb)->pkt_len;
0abf77e5
JK
830}
831
c27f339a 832/* additional qdisc xmit flags (NET_XMIT_MASK in linux/netdevice.h) */
378a2f09
JP
833enum net_xmit_qdisc_t {
834 __NET_XMIT_STOLEN = 0x00010000,
c27f339a 835 __NET_XMIT_BYPASS = 0x00020000,
378a2f09
JP
836};
837
c27f339a 838#ifdef CONFIG_NET_CLS_ACT
378a2f09 839#define net_xmit_drop_count(e) ((e) & __NET_XMIT_STOLEN ? 0 : 1)
378a2f09
JP
840#else
841#define net_xmit_drop_count(e) (1)
842#endif
843
a2da570d
ED
844static inline void qdisc_calculate_pkt_len(struct sk_buff *skb,
845 const struct Qdisc *sch)
5f86173b 846{
3a682fbd 847#ifdef CONFIG_NET_SCHED
a2da570d
ED
848 struct qdisc_size_table *stab = rcu_dereference_bh(sch->stab);
849
850 if (stab)
851 __qdisc_calculate_pkt_len(skb, stab);
3a682fbd 852#endif
a2da570d
ED
853}
854
ac5c66f2 855static inline int qdisc_enqueue(struct sk_buff *skb, struct Qdisc *sch,
520ac30f 856 struct sk_buff **to_free)
a2da570d 857{
ac5c66f2 858 return sch->enqueue(skb, sch, to_free);
5f86173b
JK
859}
860
50dc9a85 861static inline void _bstats_update(struct gnet_stats_basic_sync *bstats,
811b8f53 862 __u64 bytes, __u64 packets)
38040702 863{
67c9e627 864 u64_stats_update_begin(&bstats->syncp);
50dc9a85
AD
865 u64_stats_add(&bstats->bytes, bytes);
866 u64_stats_add(&bstats->packets, packets);
67c9e627 867 u64_stats_update_end(&bstats->syncp);
38040702
AV
868}
869
50dc9a85 870static inline void bstats_update(struct gnet_stats_basic_sync *bstats,
bfe0d029
ED
871 const struct sk_buff *skb)
872{
38040702
AV
873 _bstats_update(bstats,
874 qdisc_pkt_len(skb),
875 skb_is_gso(skb) ? skb_shinfo(skb)->gso_segs : 1);
876}
877
24ea591d
ED
878static inline void qdisc_bstats_cpu_update(struct Qdisc *sch,
879 const struct sk_buff *skb)
880{
50dc9a85 881 bstats_update(this_cpu_ptr(sch->cpu_bstats), skb);
24ea591d
ED
882}
883
bfe0d029
ED
884static inline void qdisc_bstats_update(struct Qdisc *sch,
885 const struct sk_buff *skb)
bbd8a0d3 886{
bfe0d029 887 bstats_update(&sch->bstats, skb);
bbd8a0d3
KK
888}
889
25331d6c
JF
890static inline void qdisc_qstats_backlog_dec(struct Qdisc *sch,
891 const struct sk_buff *skb)
892{
893 sch->qstats.backlog -= qdisc_pkt_len(skb);
894}
895
40bd0362
JF
896static inline void qdisc_qstats_cpu_backlog_dec(struct Qdisc *sch,
897 const struct sk_buff *skb)
898{
899 this_cpu_sub(sch->cpu_qstats->backlog, qdisc_pkt_len(skb));
900}
901
25331d6c
JF
902static inline void qdisc_qstats_backlog_inc(struct Qdisc *sch,
903 const struct sk_buff *skb)
904{
905 sch->qstats.backlog += qdisc_pkt_len(skb);
906}
907
40bd0362
JF
908static inline void qdisc_qstats_cpu_backlog_inc(struct Qdisc *sch,
909 const struct sk_buff *skb)
910{
911 this_cpu_add(sch->cpu_qstats->backlog, qdisc_pkt_len(skb));
912}
913
73eb628d 914static inline void qdisc_qstats_cpu_qlen_inc(struct Qdisc *sch)
40bd0362 915{
73eb628d 916 this_cpu_inc(sch->cpu_qstats->qlen);
40bd0362
JF
917}
918
73eb628d 919static inline void qdisc_qstats_cpu_qlen_dec(struct Qdisc *sch)
40bd0362 920{
73eb628d 921 this_cpu_dec(sch->cpu_qstats->qlen);
40bd0362
JF
922}
923
924static inline void qdisc_qstats_cpu_requeues_inc(struct Qdisc *sch)
925{
926 this_cpu_inc(sch->cpu_qstats->requeues);
927}
928
25331d6c
JF
929static inline void __qdisc_qstats_drop(struct Qdisc *sch, int count)
930{
931 sch->qstats.drops += count;
932}
933
24ea591d 934static inline void qstats_drop_inc(struct gnet_stats_queue *qstats)
25331d6c 935{
24ea591d 936 qstats->drops++;
25331d6c
JF
937}
938
24ea591d 939static inline void qstats_overlimit_inc(struct gnet_stats_queue *qstats)
b0ab6f92 940{
24ea591d
ED
941 qstats->overlimits++;
942}
b0ab6f92 943
24ea591d
ED
944static inline void qdisc_qstats_drop(struct Qdisc *sch)
945{
946 qstats_drop_inc(&sch->qstats);
947}
948
949static inline void qdisc_qstats_cpu_drop(struct Qdisc *sch)
950{
eb60a8dd 951 this_cpu_inc(sch->cpu_qstats->drops);
b0ab6f92
JF
952}
953
25331d6c
JF
954static inline void qdisc_qstats_overlimit(struct Qdisc *sch)
955{
956 sch->qstats.overlimits++;
957}
958
5dd431b6
PA
959static inline int qdisc_qstats_copy(struct gnet_dump *d, struct Qdisc *sch)
960{
961 __u32 qlen = qdisc_qlen_sum(sch);
962
963 return gnet_stats_copy_queue(d, sch->cpu_qstats, &sch->qstats, qlen);
964}
965
966static inline void qdisc_qstats_qlen_backlog(struct Qdisc *sch, __u32 *qlen,
967 __u32 *backlog)
968{
969 struct gnet_stats_queue qstats = { 0 };
5dd431b6 970
10940eb7
SAS
971 gnet_stats_add_queue(&qstats, sch->cpu_qstats, &sch->qstats);
972 *qlen = qstats.qlen + qdisc_qlen(sch);
5dd431b6
PA
973 *backlog = qstats.backlog;
974}
975
e5f0e8f8
PA
976static inline void qdisc_purge_queue(struct Qdisc *sch)
977{
978 __u32 qlen, backlog;
979
980 qdisc_qstats_qlen_backlog(sch, &qlen, &backlog);
981 qdisc_reset(sch);
982 qdisc_tree_reduce_backlog(sch, qlen, backlog);
983}
984
aea890b8
DM
985static inline void __qdisc_enqueue_tail(struct sk_buff *skb,
986 struct qdisc_skb_head *qh)
9972b25d 987{
48da34b7
FW
988 struct sk_buff *last = qh->tail;
989
990 if (last) {
991 skb->next = NULL;
992 last->next = skb;
993 qh->tail = skb;
994 } else {
995 qh->tail = skb;
996 qh->head = skb;
997 }
998 qh->qlen++;
9972b25d
TG
999}
1000
1001static inline int qdisc_enqueue_tail(struct sk_buff *skb, struct Qdisc *sch)
1002{
aea890b8
DM
1003 __qdisc_enqueue_tail(skb, &sch->q);
1004 qdisc_qstats_backlog_inc(sch, skb);
1005 return NET_XMIT_SUCCESS;
9972b25d
TG
1006}
1007
59697730
DM
1008static inline void __qdisc_enqueue_head(struct sk_buff *skb,
1009 struct qdisc_skb_head *qh)
1010{
1011 skb->next = qh->head;
1012
1013 if (!qh->head)
1014 qh->tail = skb;
1015 qh->head = skb;
1016 qh->qlen++;
1017}
1018
48da34b7 1019static inline struct sk_buff *__qdisc_dequeue_head(struct qdisc_skb_head *qh)
9972b25d 1020{
48da34b7
FW
1021 struct sk_buff *skb = qh->head;
1022
1023 if (likely(skb != NULL)) {
1024 qh->head = skb->next;
1025 qh->qlen--;
1026 if (qh->head == NULL)
1027 qh->tail = NULL;
1028 skb->next = NULL;
1029 }
9972b25d 1030
ec323368
FW
1031 return skb;
1032}
1033
2d3cbfd6
CW
1034static inline struct sk_buff *qdisc_dequeue_internal(struct Qdisc *sch, bool direct)
1035{
1036 struct sk_buff *skb;
1037
1038 skb = __skb_dequeue(&sch->gso_skb);
1039 if (skb) {
1040 sch->q.qlen--;
1041 return skb;
1042 }
1043 if (direct)
1044 return __qdisc_dequeue_head(&sch->q);
1045 else
1046 return sch->dequeue(sch);
1047}
1048
ec323368
FW
1049static inline struct sk_buff *qdisc_dequeue_head(struct Qdisc *sch)
1050{
1051 struct sk_buff *skb = __qdisc_dequeue_head(&sch->q);
1052
9190b3b3 1053 if (likely(skb != NULL)) {
25331d6c 1054 qdisc_qstats_backlog_dec(sch, skb);
9190b3b3
ED
1055 qdisc_bstats_update(sch, skb);
1056 }
9972b25d
TG
1057
1058 return skb;
1059}
1060
b6a3c606
VN
1061struct tc_skb_cb {
1062 struct qdisc_skb_cb qdisc_cb;
1063 u32 drop_reason;
1064
1065 u16 zone; /* Only valid if post_ct = true */
1066 u16 mru;
1067 u8 post_ct:1;
1068 u8 post_ct_snat:1;
1069 u8 post_ct_dnat:1;
1070};
1071
1072static inline struct tc_skb_cb *tc_skb_cb(const struct sk_buff *skb)
1073{
1074 struct tc_skb_cb *cb = (struct tc_skb_cb *)skb->cb;
1075
1076 BUILD_BUG_ON(sizeof(*cb) > sizeof_field(struct sk_buff, cb));
1077 return cb;
1078}
1079
1080static inline enum skb_drop_reason
1081tcf_get_drop_reason(const struct sk_buff *skb)
1082{
1083 return tc_skb_cb(skb)->drop_reason;
1084}
1085
1086static inline void tcf_set_drop_reason(const struct sk_buff *skb,
1087 enum skb_drop_reason reason)
1088{
1089 tc_skb_cb(skb)->drop_reason = reason;
1090}
1091
520ac30f
ED
1092/* Instead of calling kfree_skb() while root qdisc lock is held,
1093 * queue the skb for future freeing at end of __dev_xmit_skb()
1094 */
1095static inline void __qdisc_drop(struct sk_buff *skb, struct sk_buff **to_free)
1096{
1097 skb->next = *to_free;
1098 *to_free = skb;
1099}
1100
35d889d1
AK
1101static inline void __qdisc_drop_all(struct sk_buff *skb,
1102 struct sk_buff **to_free)
1103{
1104 if (skb->prev)
1105 skb->prev->next = *to_free;
1106 else
1107 skb->next = *to_free;
1108 *to_free = skb;
1109}
1110
57dbb2d8 1111static inline unsigned int __qdisc_queue_drop_head(struct Qdisc *sch,
48da34b7 1112 struct qdisc_skb_head *qh,
520ac30f 1113 struct sk_buff **to_free)
57dbb2d8 1114{
48da34b7 1115 struct sk_buff *skb = __qdisc_dequeue_head(qh);
57dbb2d8
HPP
1116
1117 if (likely(skb != NULL)) {
1118 unsigned int len = qdisc_pkt_len(skb);
520ac30f 1119
25331d6c 1120 qdisc_qstats_backlog_dec(sch, skb);
520ac30f 1121 __qdisc_drop(skb, to_free);
57dbb2d8
HPP
1122 return len;
1123 }
1124
1125 return 0;
1126}
1127
48a8f519
PM
1128static inline struct sk_buff *qdisc_peek_head(struct Qdisc *sch)
1129{
48da34b7
FW
1130 const struct qdisc_skb_head *qh = &sch->q;
1131
1132 return qh->head;
48a8f519
PM
1133}
1134
77be155c
JP
1135/* generic pseudo peek method for non-work-conserving qdisc */
1136static inline struct sk_buff *qdisc_peek_dequeued(struct Qdisc *sch)
1137{
a53851e2
JF
1138 struct sk_buff *skb = skb_peek(&sch->gso_skb);
1139
77be155c 1140 /* we can reuse ->gso_skb because peek isn't called for root qdiscs */
a53851e2
JF
1141 if (!skb) {
1142 skb = sch->dequeue(sch);
1143
1144 if (skb) {
1145 __skb_queue_head(&sch->gso_skb, skb);
61c9eaf9 1146 /* it's still part of the queue */
a53851e2 1147 qdisc_qstats_backlog_inc(sch, skb);
61c9eaf9 1148 sch->q.qlen++;
a27758ff 1149 }
61c9eaf9 1150 }
77be155c 1151
a53851e2 1152 return skb;
77be155c
JP
1153}
1154
8a53e616
PA
1155static inline void qdisc_update_stats_at_dequeue(struct Qdisc *sch,
1156 struct sk_buff *skb)
1157{
1158 if (qdisc_is_percpu_stats(sch)) {
1159 qdisc_qstats_cpu_backlog_dec(sch, skb);
1160 qdisc_bstats_cpu_update(sch, skb);
73eb628d 1161 qdisc_qstats_cpu_qlen_dec(sch);
8a53e616
PA
1162 } else {
1163 qdisc_qstats_backlog_dec(sch, skb);
1164 qdisc_bstats_update(sch, skb);
1165 sch->q.qlen--;
1166 }
1167}
1168
1169static inline void qdisc_update_stats_at_enqueue(struct Qdisc *sch,
1170 unsigned int pkt_len)
1171{
1172 if (qdisc_is_percpu_stats(sch)) {
73eb628d 1173 qdisc_qstats_cpu_qlen_inc(sch);
8a53e616
PA
1174 this_cpu_add(sch->cpu_qstats->backlog, pkt_len);
1175 } else {
1176 sch->qstats.backlog += pkt_len;
1177 sch->q.qlen++;
1178 }
1179}
1180
77be155c
JP
1181/* use instead of qdisc->dequeue() for all qdiscs queried with ->peek() */
1182static inline struct sk_buff *qdisc_dequeue_peeked(struct Qdisc *sch)
1183{
a53851e2 1184 struct sk_buff *skb = skb_peek(&sch->gso_skb);
77be155c 1185
61c9eaf9 1186 if (skb) {
a53851e2 1187 skb = __skb_dequeue(&sch->gso_skb);
9c01c9f1
PA
1188 if (qdisc_is_percpu_stats(sch)) {
1189 qdisc_qstats_cpu_backlog_dec(sch, skb);
73eb628d 1190 qdisc_qstats_cpu_qlen_dec(sch);
9c01c9f1
PA
1191 } else {
1192 qdisc_qstats_backlog_dec(sch, skb);
1193 sch->q.qlen--;
1194 }
61c9eaf9 1195 } else {
77be155c 1196 skb = sch->dequeue(sch);
61c9eaf9 1197 }
77be155c
JP
1198
1199 return skb;
1200}
1201
48da34b7 1202static inline void __qdisc_reset_queue(struct qdisc_skb_head *qh)
9972b25d
TG
1203{
1204 /*
1205 * We do not know the backlog in bytes of this list, it
1206 * is up to the caller to correct it
1207 */
48da34b7
FW
1208 ASSERT_RTNL();
1209 if (qh->qlen) {
1210 rtnl_kfree_skbs(qh->head, qh->tail);
1211
1212 qh->head = NULL;
1213 qh->tail = NULL;
1214 qh->qlen = 0;
1b5c5493 1215 }
9972b25d
TG
1216}
1217
1218static inline void qdisc_reset_queue(struct Qdisc *sch)
1219{
1b5c5493 1220 __qdisc_reset_queue(&sch->q);
9972b25d
TG
1221}
1222
86a7996c
WC
1223static inline struct Qdisc *qdisc_replace(struct Qdisc *sch, struct Qdisc *new,
1224 struct Qdisc **pold)
1225{
1226 struct Qdisc *old;
1227
1228 sch_tree_lock(sch);
1229 old = *pold;
1230 *pold = new;
e5f0e8f8 1231 if (old != NULL)
938e0fcd 1232 qdisc_purge_queue(old);
86a7996c
WC
1233 sch_tree_unlock(sch);
1234
1235 return old;
1236}
1237
1b5c5493
ED
1238static inline void rtnl_qdisc_drop(struct sk_buff *skb, struct Qdisc *sch)
1239{
1240 rtnl_kfree_skbs(skb, skb);
1241 qdisc_qstats_drop(sch);
1242}
1243
40bd0362
JF
1244static inline int qdisc_drop_cpu(struct sk_buff *skb, struct Qdisc *sch,
1245 struct sk_buff **to_free)
1246{
1247 __qdisc_drop(skb, to_free);
1248 qdisc_qstats_cpu_drop(sch);
1249
1250 return NET_XMIT_DROP;
1251}
520ac30f
ED
1252
1253static inline int qdisc_drop(struct sk_buff *skb, struct Qdisc *sch,
1254 struct sk_buff **to_free)
9972b25d 1255{
520ac30f 1256 __qdisc_drop(skb, to_free);
25331d6c 1257 qdisc_qstats_drop(sch);
9972b25d
TG
1258
1259 return NET_XMIT_DROP;
1260}
1261
5765c7f6
ED
1262static inline int qdisc_drop_reason(struct sk_buff *skb, struct Qdisc *sch,
1263 struct sk_buff **to_free,
1264 enum skb_drop_reason reason)
1265{
1266 tcf_set_drop_reason(skb, reason);
1267 return qdisc_drop(skb, sch, to_free);
1268}
1269
35d889d1
AK
1270static inline int qdisc_drop_all(struct sk_buff *skb, struct Qdisc *sch,
1271 struct sk_buff **to_free)
1272{
1273 __qdisc_drop_all(skb, to_free);
1274 qdisc_qstats_drop(sch);
1275
1276 return NET_XMIT_DROP;
1277}
1278
292f1c7f 1279struct psched_ratecfg {
130d3d68 1280 u64 rate_bytes_ps; /* bytes per second */
01cb71d2
ED
1281 u32 mult;
1282 u16 overhead;
fb80445c 1283 u16 mpu;
8a8e3d84 1284 u8 linklayer;
01cb71d2 1285 u8 shift;
292f1c7f
JP
1286};
1287
1288static inline u64 psched_l2t_ns(const struct psched_ratecfg *r,
1289 unsigned int len)
1290{
8a8e3d84
JDB
1291 len += r->overhead;
1292
fb80445c
KB
1293 if (len < r->mpu)
1294 len = r->mpu;
1295
8a8e3d84
JDB
1296 if (unlikely(r->linklayer == TC_LINKLAYER_ATM))
1297 return ((u64)(DIV_ROUND_UP(len,48)*53) * r->mult) >> r->shift;
1298
1299 return ((u64)len * r->mult) >> r->shift;
292f1c7f
JP
1300}
1301
5c15257f 1302void psched_ratecfg_precompute(struct psched_ratecfg *r,
3e1e3aae
ED
1303 const struct tc_ratespec *conf,
1304 u64 rate64);
292f1c7f 1305
01cb71d2
ED
1306static inline void psched_ratecfg_getrate(struct tc_ratespec *res,
1307 const struct psched_ratecfg *r)
292f1c7f 1308{
01cb71d2 1309 memset(res, 0, sizeof(*res));
3e1e3aae
ED
1310
1311 /* legacy struct tc_ratespec has a 32bit @rate field
1312 * Qdisc using 64bit rate should add new attributes
1313 * in order to maintain compatibility.
1314 */
1315 res->rate = min_t(u64, r->rate_bytes_ps, ~0U);
1316
01cb71d2 1317 res->overhead = r->overhead;
fb80445c 1318 res->mpu = r->mpu;
8a8e3d84 1319 res->linklayer = (r->linklayer & TC_LINKLAYER_MASK);
292f1c7f
JP
1320}
1321
2ffe0395
BZ
1322struct psched_pktrate {
1323 u64 rate_pkts_ps; /* packets per second */
1324 u32 mult;
1325 u8 shift;
1326};
1327
1328static inline u64 psched_pkt2t_ns(const struct psched_pktrate *r,
1329 unsigned int pkt_num)
1330{
1331 return ((u64)pkt_num * r->mult) >> r->shift;
1332}
1333
1334void psched_ppscfg_precompute(struct psched_pktrate *r, u64 pktrate64);
1335
46209401
JP
1336/* Mini Qdisc serves for specific needs of ingress/clsact Qdisc.
1337 * The fast path only needs to access filter list and to update stats
1338 */
1339struct mini_Qdisc {
1340 struct tcf_proto *filter_list;
7d17c544 1341 struct tcf_block *block;
50dc9a85 1342 struct gnet_stats_basic_sync __percpu *cpu_bstats;
46209401 1343 struct gnet_stats_queue __percpu *cpu_qstats;
26746382 1344 unsigned long rcu_state;
46209401
JP
1345};
1346
1347static inline void mini_qdisc_bstats_cpu_update(struct mini_Qdisc *miniq,
1348 const struct sk_buff *skb)
1349{
50dc9a85 1350 bstats_update(this_cpu_ptr(miniq->cpu_bstats), skb);
46209401
JP
1351}
1352
1353static inline void mini_qdisc_qstats_cpu_drop(struct mini_Qdisc *miniq)
1354{
1355 this_cpu_inc(miniq->cpu_qstats->drops);
1356}
1357
1358struct mini_Qdisc_pair {
1359 struct mini_Qdisc miniq1;
1360 struct mini_Qdisc miniq2;
1361 struct mini_Qdisc __rcu **p_miniq;
1362};
1363
1364void mini_qdisc_pair_swap(struct mini_Qdisc_pair *miniqp,
1365 struct tcf_proto *tp_head);
1366void mini_qdisc_pair_init(struct mini_Qdisc_pair *miniqp, struct Qdisc *qdisc,
1367 struct mini_Qdisc __rcu **p_miniq);
7d17c544
PB
1368void mini_qdisc_pair_block_init(struct mini_Qdisc_pair *miniqp,
1369 struct tcf_block *block);
46209401 1370
f7116fb4
JK
1371void mq_change_real_num_tx(struct Qdisc *sch, unsigned int new_real_tx);
1372
c129412f 1373int sch_frag_xmit_hook(struct sk_buff *skb, int (*xmit)(struct sk_buff *skb));
cd11b164 1374
3a415d59
ED
1375/* Make sure qdisc is no longer in SCHED state. */
1376static inline void qdisc_synchronize(const struct Qdisc *q)
1377{
1378 while (test_bit(__QDISC_STATE_SCHED, &q->state))
1379 msleep(1);
1380}
1381
1da177e4 1382#endif