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