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