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