net: sched: remove classid and q fields from tcf_proto
[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>
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;
1da177e4 258 void *data;
dc7f9f6e 259 const struct tcf_proto_ops *ops;
5bc17018 260 struct tcf_chain *chain;
25d8c0d5 261 struct rcu_head rcu;
1da177e4
LT
262};
263
175f9c1b
JK
264struct qdisc_skb_cb {
265 unsigned int pkt_len;
df4ab5b3 266 u16 slave_dev_queue_mapping;
045efa82 267 u16 tc_classid;
25711786
ED
268#define QDISC_CB_PRIV_LEN 20
269 unsigned char data[QDISC_CB_PRIV_LEN];
175f9c1b
JK
270};
271
c7eb7d72
JP
272typedef void tcf_chain_head_change_t(struct tcf_proto *tp_head, void *priv);
273
2190d1d0
JP
274struct tcf_chain {
275 struct tcf_proto __rcu *filter_chain;
a9b19443 276 struct list_head filter_chain_list;
5bc17018
JP
277 struct list_head list;
278 struct tcf_block *block;
279 u32 index; /* chain index */
280 unsigned int refcnt;
6529eaba
JP
281};
282
2190d1d0 283struct tcf_block {
5bc17018 284 struct list_head chain_list;
48617387
JP
285 u32 index; /* block index for shared blocks */
286 unsigned int refcnt;
855319be 287 struct net *net;
69d78ef2 288 struct Qdisc *q;
acb67442 289 struct list_head cb_list;
f36fe1c4
JP
290 struct list_head owner_list;
291 bool keep_dst;
2190d1d0
JP
292};
293
16bda13d
DM
294static inline void qdisc_cb_private_validate(const struct sk_buff *skb, int sz)
295{
296 struct qdisc_skb_cb *qcb;
5ee31c68
ED
297
298 BUILD_BUG_ON(sizeof(skb->cb) < offsetof(struct qdisc_skb_cb, data) + sz);
16bda13d
DM
299 BUILD_BUG_ON(sizeof(qcb->data) < sz);
300}
301
7e66016f
JF
302static inline int qdisc_qlen_cpu(const struct Qdisc *q)
303{
304 return this_cpu_ptr(q->cpu_qstats)->qlen;
305}
306
05bdd2f1 307static inline int qdisc_qlen(const struct Qdisc *q)
bbd8a0d3
KK
308{
309 return q->q.qlen;
310}
311
7e66016f
JF
312static inline int qdisc_qlen_sum(const struct Qdisc *q)
313{
314 __u32 qlen = 0;
315 int i;
316
317 if (q->flags & TCQ_F_NOLOCK) {
318 for_each_possible_cpu(i)
319 qlen += per_cpu_ptr(q->cpu_qstats, i)->qlen;
320 } else {
321 qlen = q->q.qlen;
322 }
323
324 return qlen;
325}
326
bfe0d029 327static inline struct qdisc_skb_cb *qdisc_skb_cb(const struct sk_buff *skb)
175f9c1b
JK
328{
329 return (struct qdisc_skb_cb *)skb->cb;
330}
331
83874000
DM
332static inline spinlock_t *qdisc_lock(struct Qdisc *qdisc)
333{
334 return &qdisc->q.lock;
335}
336
05bdd2f1 337static inline struct Qdisc *qdisc_root(const struct Qdisc *qdisc)
7698b4fc 338{
46e5da40
JF
339 struct Qdisc *q = rcu_dereference_rtnl(qdisc->dev_queue->qdisc);
340
341 return q;
7698b4fc
DM
342}
343
05bdd2f1 344static inline struct Qdisc *qdisc_root_sleeping(const struct Qdisc *qdisc)
2540e051
JP
345{
346 return qdisc->dev_queue->qdisc_sleeping;
347}
348
7e43f112
DM
349/* The qdisc root lock is a mechanism by which to top level
350 * of a qdisc tree can be locked from any qdisc node in the
351 * forest. This allows changing the configuration of some
352 * aspect of the qdisc tree while blocking out asynchronous
353 * qdisc access in the packet processing paths.
354 *
355 * It is only legal to do this when the root will not change
356 * on us. Otherwise we'll potentially lock the wrong qdisc
357 * root. This is enforced by holding the RTNL semaphore, which
358 * all users of this lock accessor must do.
359 */
05bdd2f1 360static inline spinlock_t *qdisc_root_lock(const struct Qdisc *qdisc)
7698b4fc
DM
361{
362 struct Qdisc *root = qdisc_root(qdisc);
363
7e43f112 364 ASSERT_RTNL();
83874000 365 return qdisc_lock(root);
7698b4fc
DM
366}
367
05bdd2f1 368static inline spinlock_t *qdisc_root_sleeping_lock(const struct Qdisc *qdisc)
f6f9b93f
JP
369{
370 struct Qdisc *root = qdisc_root_sleeping(qdisc);
371
372 ASSERT_RTNL();
373 return qdisc_lock(root);
374}
375
edb09eb1
ED
376static inline seqcount_t *qdisc_root_sleeping_running(const struct Qdisc *qdisc)
377{
378 struct Qdisc *root = qdisc_root_sleeping(qdisc);
379
380 ASSERT_RTNL();
381 return &root->running;
382}
383
05bdd2f1 384static inline struct net_device *qdisc_dev(const struct Qdisc *qdisc)
5ce2d488
DM
385{
386 return qdisc->dev_queue->dev;
387}
1da177e4 388
05bdd2f1 389static inline void sch_tree_lock(const struct Qdisc *q)
78a5b30b 390{
fe439dd0 391 spin_lock_bh(qdisc_root_sleeping_lock(q));
78a5b30b
DM
392}
393
05bdd2f1 394static inline void sch_tree_unlock(const struct Qdisc *q)
78a5b30b 395{
fe439dd0 396 spin_unlock_bh(qdisc_root_sleeping_lock(q));
78a5b30b
DM
397}
398
e41a33e6
TG
399extern struct Qdisc noop_qdisc;
400extern struct Qdisc_ops noop_qdisc_ops;
6ec1c69a
DM
401extern struct Qdisc_ops pfifo_fast_ops;
402extern struct Qdisc_ops mq_qdisc_ops;
d66d6c31 403extern struct Qdisc_ops noqueue_qdisc_ops;
6da7c8fc 404extern const struct Qdisc_ops *default_qdisc_ops;
1f27cde3
ED
405static inline const struct Qdisc_ops *
406get_default_qdisc_ops(const struct net_device *dev, int ntx)
407{
408 return ntx < dev->real_num_tx_queues ?
409 default_qdisc_ops : &pfifo_fast_ops;
410}
e41a33e6 411
fd2c3ef7 412struct Qdisc_class_common {
6fe1c7a5
PM
413 u32 classid;
414 struct hlist_node hnode;
415};
416
fd2c3ef7 417struct Qdisc_class_hash {
6fe1c7a5
PM
418 struct hlist_head *hash;
419 unsigned int hashsize;
420 unsigned int hashmask;
421 unsigned int hashelems;
422};
423
424static inline unsigned int qdisc_class_hash(u32 id, u32 mask)
425{
426 id ^= id >> 8;
427 id ^= id >> 4;
428 return id & mask;
429}
430
431static inline struct Qdisc_class_common *
05bdd2f1 432qdisc_class_find(const struct Qdisc_class_hash *hash, u32 id)
6fe1c7a5
PM
433{
434 struct Qdisc_class_common *cl;
6fe1c7a5
PM
435 unsigned int h;
436
7d3f0cd4
GF
437 if (!id)
438 return NULL;
439
6fe1c7a5 440 h = qdisc_class_hash(id, hash->hashmask);
b67bfe0d 441 hlist_for_each_entry(cl, &hash->hash[h], hnode) {
6fe1c7a5
PM
442 if (cl->classid == id)
443 return cl;
444 }
445 return NULL;
446}
447
384c181e
AN
448static inline int tc_classid_to_hwtc(struct net_device *dev, u32 classid)
449{
450 u32 hwtc = TC_H_MIN(classid) - TC_H_MIN_PRIORITY;
451
452 return (hwtc < netdev_get_num_tc(dev)) ? hwtc : -EINVAL;
453}
454
5c15257f
JP
455int qdisc_class_hash_init(struct Qdisc_class_hash *);
456void qdisc_class_hash_insert(struct Qdisc_class_hash *,
457 struct Qdisc_class_common *);
458void qdisc_class_hash_remove(struct Qdisc_class_hash *,
459 struct Qdisc_class_common *);
460void qdisc_class_hash_grow(struct Qdisc *, struct Qdisc_class_hash *);
461void qdisc_class_hash_destroy(struct Qdisc_class_hash *);
462
463void dev_init_scheduler(struct net_device *dev);
464void dev_shutdown(struct net_device *dev);
465void dev_activate(struct net_device *dev);
466void dev_deactivate(struct net_device *dev);
467void dev_deactivate_many(struct list_head *head);
468struct Qdisc *dev_graft_qdisc(struct netdev_queue *dev_queue,
469 struct Qdisc *qdisc);
470void qdisc_reset(struct Qdisc *qdisc);
471void qdisc_destroy(struct Qdisc *qdisc);
2ccccf5f
WC
472void qdisc_tree_reduce_backlog(struct Qdisc *qdisc, unsigned int n,
473 unsigned int len);
5c15257f 474struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue,
d0bd684d
AA
475 const struct Qdisc_ops *ops,
476 struct netlink_ext_ack *extack);
81d947e2 477void qdisc_free(struct Qdisc *qdisc);
5c15257f 478struct Qdisc *qdisc_create_dflt(struct netdev_queue *dev_queue,
a38a9882
AA
479 const struct Qdisc_ops *ops, u32 parentid,
480 struct netlink_ext_ack *extack);
5c15257f
JP
481void __qdisc_calculate_pkt_len(struct sk_buff *skb,
482 const struct qdisc_size_table *stab);
27b29f63 483int skb_do_redirect(struct sk_buff *);
1da177e4 484
a5135bcf
WB
485static inline void skb_reset_tc(struct sk_buff *skb)
486{
487#ifdef CONFIG_NET_CLS_ACT
bc31c905 488 skb->tc_redirected = 0;
a5135bcf
WB
489#endif
490}
491
fdc5432a
DB
492static inline bool skb_at_tc_ingress(const struct sk_buff *skb)
493{
494#ifdef CONFIG_NET_CLS_ACT
8dc07fdb 495 return skb->tc_at_ingress;
fdc5432a
DB
496#else
497 return false;
498#endif
499}
500
e7246e12
WB
501static inline bool skb_skip_tc_classify(struct sk_buff *skb)
502{
503#ifdef CONFIG_NET_CLS_ACT
504 if (skb->tc_skip_classify) {
505 skb->tc_skip_classify = 0;
506 return true;
507 }
508#endif
509 return false;
510}
511
f0796d5c
JF
512/* Reset all TX qdiscs greater then index of a device. */
513static inline void qdisc_reset_all_tx_gt(struct net_device *dev, unsigned int i)
5aa70995 514{
4ef6acff
JF
515 struct Qdisc *qdisc;
516
f0796d5c 517 for (; i < dev->num_tx_queues; i++) {
46e5da40 518 qdisc = rtnl_dereference(netdev_get_tx_queue(dev, i)->qdisc);
4ef6acff
JF
519 if (qdisc) {
520 spin_lock_bh(qdisc_lock(qdisc));
521 qdisc_reset(qdisc);
522 spin_unlock_bh(qdisc_lock(qdisc));
523 }
524 }
5aa70995
DM
525}
526
5aa70995
DM
527static inline void qdisc_reset_all_tx(struct net_device *dev)
528{
f0796d5c 529 qdisc_reset_all_tx_gt(dev, 0);
5aa70995
DM
530}
531
3e745dd6
DM
532/* Are all TX queues of the device empty? */
533static inline bool qdisc_all_tx_empty(const struct net_device *dev)
534{
e8a0464c 535 unsigned int i;
46e5da40
JF
536
537 rcu_read_lock();
e8a0464c
DM
538 for (i = 0; i < dev->num_tx_queues; i++) {
539 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
46e5da40 540 const struct Qdisc *q = rcu_dereference(txq->qdisc);
3e745dd6 541
46e5da40
JF
542 if (q->q.qlen) {
543 rcu_read_unlock();
e8a0464c 544 return false;
46e5da40 545 }
e8a0464c 546 }
46e5da40 547 rcu_read_unlock();
e8a0464c 548 return true;
3e745dd6
DM
549}
550
6fa9864b 551/* Are any of the TX qdiscs changing? */
05bdd2f1 552static inline bool qdisc_tx_changing(const struct net_device *dev)
6fa9864b 553{
e8a0464c 554 unsigned int i;
46e5da40 555
e8a0464c
DM
556 for (i = 0; i < dev->num_tx_queues; i++) {
557 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
46e5da40 558 if (rcu_access_pointer(txq->qdisc) != txq->qdisc_sleeping)
e8a0464c
DM
559 return true;
560 }
561 return false;
6fa9864b
DM
562}
563
e8a0464c 564/* Is the device using the noop qdisc on all queues? */
05297949
DM
565static inline bool qdisc_tx_is_noop(const struct net_device *dev)
566{
e8a0464c 567 unsigned int i;
46e5da40 568
e8a0464c
DM
569 for (i = 0; i < dev->num_tx_queues; i++) {
570 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
46e5da40 571 if (rcu_access_pointer(txq->qdisc) != &noop_qdisc)
e8a0464c
DM
572 return false;
573 }
574 return true;
05297949
DM
575}
576
bfe0d029 577static inline unsigned int qdisc_pkt_len(const struct sk_buff *skb)
0abf77e5 578{
175f9c1b 579 return qdisc_skb_cb(skb)->pkt_len;
0abf77e5
JK
580}
581
c27f339a 582/* additional qdisc xmit flags (NET_XMIT_MASK in linux/netdevice.h) */
378a2f09
JP
583enum net_xmit_qdisc_t {
584 __NET_XMIT_STOLEN = 0x00010000,
c27f339a 585 __NET_XMIT_BYPASS = 0x00020000,
378a2f09
JP
586};
587
c27f339a 588#ifdef CONFIG_NET_CLS_ACT
378a2f09 589#define net_xmit_drop_count(e) ((e) & __NET_XMIT_STOLEN ? 0 : 1)
378a2f09
JP
590#else
591#define net_xmit_drop_count(e) (1)
592#endif
593
a2da570d
ED
594static inline void qdisc_calculate_pkt_len(struct sk_buff *skb,
595 const struct Qdisc *sch)
5f86173b 596{
3a682fbd 597#ifdef CONFIG_NET_SCHED
a2da570d
ED
598 struct qdisc_size_table *stab = rcu_dereference_bh(sch->stab);
599
600 if (stab)
601 __qdisc_calculate_pkt_len(skb, stab);
3a682fbd 602#endif
a2da570d
ED
603}
604
520ac30f
ED
605static inline int qdisc_enqueue(struct sk_buff *skb, struct Qdisc *sch,
606 struct sk_buff **to_free)
a2da570d
ED
607{
608 qdisc_calculate_pkt_len(skb, sch);
520ac30f 609 return sch->enqueue(skb, sch, to_free);
5f86173b
JK
610}
611
22e0f8b9
JF
612static inline bool qdisc_is_percpu_stats(const struct Qdisc *q)
613{
614 return q->flags & TCQ_F_CPUSTATS;
615}
bfe0d029 616
38040702
AV
617static inline void _bstats_update(struct gnet_stats_basic_packed *bstats,
618 __u64 bytes, __u32 packets)
619{
620 bstats->bytes += bytes;
621 bstats->packets += packets;
622}
623
bfe0d029
ED
624static inline void bstats_update(struct gnet_stats_basic_packed *bstats,
625 const struct sk_buff *skb)
626{
38040702
AV
627 _bstats_update(bstats,
628 qdisc_pkt_len(skb),
629 skb_is_gso(skb) ? skb_shinfo(skb)->gso_segs : 1);
630}
631
632static inline void _bstats_cpu_update(struct gnet_stats_basic_cpu *bstats,
633 __u64 bytes, __u32 packets)
634{
635 u64_stats_update_begin(&bstats->syncp);
636 _bstats_update(&bstats->bstats, bytes, packets);
637 u64_stats_update_end(&bstats->syncp);
bfe0d029
ED
638}
639
24ea591d
ED
640static inline void bstats_cpu_update(struct gnet_stats_basic_cpu *bstats,
641 const struct sk_buff *skb)
22e0f8b9 642{
22e0f8b9
JF
643 u64_stats_update_begin(&bstats->syncp);
644 bstats_update(&bstats->bstats, skb);
645 u64_stats_update_end(&bstats->syncp);
646}
647
24ea591d
ED
648static inline void qdisc_bstats_cpu_update(struct Qdisc *sch,
649 const struct sk_buff *skb)
650{
651 bstats_cpu_update(this_cpu_ptr(sch->cpu_bstats), skb);
652}
653
bfe0d029
ED
654static inline void qdisc_bstats_update(struct Qdisc *sch,
655 const struct sk_buff *skb)
bbd8a0d3 656{
bfe0d029 657 bstats_update(&sch->bstats, skb);
bbd8a0d3
KK
658}
659
25331d6c
JF
660static inline void qdisc_qstats_backlog_dec(struct Qdisc *sch,
661 const struct sk_buff *skb)
662{
663 sch->qstats.backlog -= qdisc_pkt_len(skb);
664}
665
40bd0362
JF
666static inline void qdisc_qstats_cpu_backlog_dec(struct Qdisc *sch,
667 const struct sk_buff *skb)
668{
669 this_cpu_sub(sch->cpu_qstats->backlog, qdisc_pkt_len(skb));
670}
671
25331d6c
JF
672static inline void qdisc_qstats_backlog_inc(struct Qdisc *sch,
673 const struct sk_buff *skb)
674{
675 sch->qstats.backlog += qdisc_pkt_len(skb);
676}
677
40bd0362
JF
678static inline void qdisc_qstats_cpu_backlog_inc(struct Qdisc *sch,
679 const struct sk_buff *skb)
680{
681 this_cpu_add(sch->cpu_qstats->backlog, qdisc_pkt_len(skb));
682}
683
684static inline void qdisc_qstats_cpu_qlen_inc(struct Qdisc *sch)
685{
686 this_cpu_inc(sch->cpu_qstats->qlen);
687}
688
689static inline void qdisc_qstats_cpu_qlen_dec(struct Qdisc *sch)
690{
691 this_cpu_dec(sch->cpu_qstats->qlen);
692}
693
694static inline void qdisc_qstats_cpu_requeues_inc(struct Qdisc *sch)
695{
696 this_cpu_inc(sch->cpu_qstats->requeues);
697}
698
25331d6c
JF
699static inline void __qdisc_qstats_drop(struct Qdisc *sch, int count)
700{
701 sch->qstats.drops += count;
702}
703
24ea591d 704static inline void qstats_drop_inc(struct gnet_stats_queue *qstats)
25331d6c 705{
24ea591d 706 qstats->drops++;
25331d6c
JF
707}
708
24ea591d 709static inline void qstats_overlimit_inc(struct gnet_stats_queue *qstats)
b0ab6f92 710{
24ea591d
ED
711 qstats->overlimits++;
712}
b0ab6f92 713
24ea591d
ED
714static inline void qdisc_qstats_drop(struct Qdisc *sch)
715{
716 qstats_drop_inc(&sch->qstats);
717}
718
719static inline void qdisc_qstats_cpu_drop(struct Qdisc *sch)
720{
eb60a8dd 721 this_cpu_inc(sch->cpu_qstats->drops);
b0ab6f92
JF
722}
723
25331d6c
JF
724static inline void qdisc_qstats_overlimit(struct Qdisc *sch)
725{
726 sch->qstats.overlimits++;
727}
728
48da34b7
FW
729static inline void qdisc_skb_head_init(struct qdisc_skb_head *qh)
730{
731 qh->head = NULL;
732 qh->tail = NULL;
733 qh->qlen = 0;
734}
735
9972b25d 736static inline int __qdisc_enqueue_tail(struct sk_buff *skb, struct Qdisc *sch,
48da34b7 737 struct qdisc_skb_head *qh)
9972b25d 738{
48da34b7
FW
739 struct sk_buff *last = qh->tail;
740
741 if (last) {
742 skb->next = NULL;
743 last->next = skb;
744 qh->tail = skb;
745 } else {
746 qh->tail = skb;
747 qh->head = skb;
748 }
749 qh->qlen++;
25331d6c 750 qdisc_qstats_backlog_inc(sch, skb);
9972b25d
TG
751
752 return NET_XMIT_SUCCESS;
753}
754
755static inline int qdisc_enqueue_tail(struct sk_buff *skb, struct Qdisc *sch)
756{
757 return __qdisc_enqueue_tail(skb, sch, &sch->q);
758}
759
48da34b7 760static inline struct sk_buff *__qdisc_dequeue_head(struct qdisc_skb_head *qh)
9972b25d 761{
48da34b7
FW
762 struct sk_buff *skb = qh->head;
763
764 if (likely(skb != NULL)) {
765 qh->head = skb->next;
766 qh->qlen--;
767 if (qh->head == NULL)
768 qh->tail = NULL;
769 skb->next = NULL;
770 }
9972b25d 771
ec323368
FW
772 return skb;
773}
774
775static inline struct sk_buff *qdisc_dequeue_head(struct Qdisc *sch)
776{
777 struct sk_buff *skb = __qdisc_dequeue_head(&sch->q);
778
9190b3b3 779 if (likely(skb != NULL)) {
25331d6c 780 qdisc_qstats_backlog_dec(sch, skb);
9190b3b3
ED
781 qdisc_bstats_update(sch, skb);
782 }
9972b25d
TG
783
784 return skb;
785}
786
520ac30f
ED
787/* Instead of calling kfree_skb() while root qdisc lock is held,
788 * queue the skb for future freeing at end of __dev_xmit_skb()
789 */
790static inline void __qdisc_drop(struct sk_buff *skb, struct sk_buff **to_free)
791{
792 skb->next = *to_free;
793 *to_free = skb;
794}
795
57dbb2d8 796static inline unsigned int __qdisc_queue_drop_head(struct Qdisc *sch,
48da34b7 797 struct qdisc_skb_head *qh,
520ac30f 798 struct sk_buff **to_free)
57dbb2d8 799{
48da34b7 800 struct sk_buff *skb = __qdisc_dequeue_head(qh);
57dbb2d8
HPP
801
802 if (likely(skb != NULL)) {
803 unsigned int len = qdisc_pkt_len(skb);
520ac30f 804
25331d6c 805 qdisc_qstats_backlog_dec(sch, skb);
520ac30f 806 __qdisc_drop(skb, to_free);
57dbb2d8
HPP
807 return len;
808 }
809
810 return 0;
811}
812
520ac30f
ED
813static inline unsigned int qdisc_queue_drop_head(struct Qdisc *sch,
814 struct sk_buff **to_free)
57dbb2d8 815{
520ac30f 816 return __qdisc_queue_drop_head(sch, &sch->q, to_free);
9972b25d
TG
817}
818
48a8f519
PM
819static inline struct sk_buff *qdisc_peek_head(struct Qdisc *sch)
820{
48da34b7
FW
821 const struct qdisc_skb_head *qh = &sch->q;
822
823 return qh->head;
48a8f519
PM
824}
825
77be155c
JP
826/* generic pseudo peek method for non-work-conserving qdisc */
827static inline struct sk_buff *qdisc_peek_dequeued(struct Qdisc *sch)
828{
a53851e2
JF
829 struct sk_buff *skb = skb_peek(&sch->gso_skb);
830
77be155c 831 /* we can reuse ->gso_skb because peek isn't called for root qdiscs */
a53851e2
JF
832 if (!skb) {
833 skb = sch->dequeue(sch);
834
835 if (skb) {
836 __skb_queue_head(&sch->gso_skb, skb);
61c9eaf9 837 /* it's still part of the queue */
a53851e2 838 qdisc_qstats_backlog_inc(sch, skb);
61c9eaf9 839 sch->q.qlen++;
a27758ff 840 }
61c9eaf9 841 }
77be155c 842
a53851e2 843 return skb;
77be155c
JP
844}
845
846/* use instead of qdisc->dequeue() for all qdiscs queried with ->peek() */
847static inline struct sk_buff *qdisc_dequeue_peeked(struct Qdisc *sch)
848{
a53851e2 849 struct sk_buff *skb = skb_peek(&sch->gso_skb);
77be155c 850
61c9eaf9 851 if (skb) {
a53851e2 852 skb = __skb_dequeue(&sch->gso_skb);
a27758ff 853 qdisc_qstats_backlog_dec(sch, skb);
61c9eaf9
JP
854 sch->q.qlen--;
855 } else {
77be155c 856 skb = sch->dequeue(sch);
61c9eaf9 857 }
77be155c
JP
858
859 return skb;
860}
861
48da34b7 862static inline void __qdisc_reset_queue(struct qdisc_skb_head *qh)
9972b25d
TG
863{
864 /*
865 * We do not know the backlog in bytes of this list, it
866 * is up to the caller to correct it
867 */
48da34b7
FW
868 ASSERT_RTNL();
869 if (qh->qlen) {
870 rtnl_kfree_skbs(qh->head, qh->tail);
871
872 qh->head = NULL;
873 qh->tail = NULL;
874 qh->qlen = 0;
1b5c5493 875 }
9972b25d
TG
876}
877
878static inline void qdisc_reset_queue(struct Qdisc *sch)
879{
1b5c5493 880 __qdisc_reset_queue(&sch->q);
9972b25d
TG
881 sch->qstats.backlog = 0;
882}
883
86a7996c
WC
884static inline struct Qdisc *qdisc_replace(struct Qdisc *sch, struct Qdisc *new,
885 struct Qdisc **pold)
886{
887 struct Qdisc *old;
888
889 sch_tree_lock(sch);
890 old = *pold;
891 *pold = new;
892 if (old != NULL) {
68a66d14
KK
893 unsigned int qlen = old->q.qlen;
894 unsigned int backlog = old->qstats.backlog;
895
86a7996c 896 qdisc_reset(old);
68a66d14 897 qdisc_tree_reduce_backlog(old, qlen, backlog);
86a7996c
WC
898 }
899 sch_tree_unlock(sch);
900
901 return old;
902}
903
1b5c5493
ED
904static inline void rtnl_qdisc_drop(struct sk_buff *skb, struct Qdisc *sch)
905{
906 rtnl_kfree_skbs(skb, skb);
907 qdisc_qstats_drop(sch);
908}
909
40bd0362
JF
910static inline int qdisc_drop_cpu(struct sk_buff *skb, struct Qdisc *sch,
911 struct sk_buff **to_free)
912{
913 __qdisc_drop(skb, to_free);
914 qdisc_qstats_cpu_drop(sch);
915
916 return NET_XMIT_DROP;
917}
520ac30f
ED
918
919static inline int qdisc_drop(struct sk_buff *skb, struct Qdisc *sch,
920 struct sk_buff **to_free)
9972b25d 921{
520ac30f 922 __qdisc_drop(skb, to_free);
25331d6c 923 qdisc_qstats_drop(sch);
9972b25d
TG
924
925 return NET_XMIT_DROP;
926}
927
e9bef55d
JDB
928/* Length to Time (L2T) lookup in a qdisc_rate_table, to determine how
929 long it will take to send a packet given its size.
930 */
931static inline u32 qdisc_l2t(struct qdisc_rate_table* rtab, unsigned int pktlen)
932{
e08b0998
JDB
933 int slot = pktlen + rtab->rate.cell_align + rtab->rate.overhead;
934 if (slot < 0)
935 slot = 0;
e9bef55d
JDB
936 slot >>= rtab->rate.cell_log;
937 if (slot > 255)
a02cec21 938 return rtab->data[255]*(slot >> 8) + rtab->data[slot & 0xFF];
e9bef55d
JDB
939 return rtab->data[slot];
940}
941
292f1c7f 942struct psched_ratecfg {
130d3d68 943 u64 rate_bytes_ps; /* bytes per second */
01cb71d2
ED
944 u32 mult;
945 u16 overhead;
8a8e3d84 946 u8 linklayer;
01cb71d2 947 u8 shift;
292f1c7f
JP
948};
949
950static inline u64 psched_l2t_ns(const struct psched_ratecfg *r,
951 unsigned int len)
952{
8a8e3d84
JDB
953 len += r->overhead;
954
955 if (unlikely(r->linklayer == TC_LINKLAYER_ATM))
956 return ((u64)(DIV_ROUND_UP(len,48)*53) * r->mult) >> r->shift;
957
958 return ((u64)len * r->mult) >> r->shift;
292f1c7f
JP
959}
960
5c15257f 961void psched_ratecfg_precompute(struct psched_ratecfg *r,
3e1e3aae
ED
962 const struct tc_ratespec *conf,
963 u64 rate64);
292f1c7f 964
01cb71d2
ED
965static inline void psched_ratecfg_getrate(struct tc_ratespec *res,
966 const struct psched_ratecfg *r)
292f1c7f 967{
01cb71d2 968 memset(res, 0, sizeof(*res));
3e1e3aae
ED
969
970 /* legacy struct tc_ratespec has a 32bit @rate field
971 * Qdisc using 64bit rate should add new attributes
972 * in order to maintain compatibility.
973 */
974 res->rate = min_t(u64, r->rate_bytes_ps, ~0U);
975
01cb71d2 976 res->overhead = r->overhead;
8a8e3d84 977 res->linklayer = (r->linklayer & TC_LINKLAYER_MASK);
292f1c7f
JP
978}
979
46209401
JP
980/* Mini Qdisc serves for specific needs of ingress/clsact Qdisc.
981 * The fast path only needs to access filter list and to update stats
982 */
983struct mini_Qdisc {
984 struct tcf_proto *filter_list;
985 struct gnet_stats_basic_cpu __percpu *cpu_bstats;
986 struct gnet_stats_queue __percpu *cpu_qstats;
987 struct rcu_head rcu;
988};
989
990static inline void mini_qdisc_bstats_cpu_update(struct mini_Qdisc *miniq,
991 const struct sk_buff *skb)
992{
993 bstats_cpu_update(this_cpu_ptr(miniq->cpu_bstats), skb);
994}
995
996static inline void mini_qdisc_qstats_cpu_drop(struct mini_Qdisc *miniq)
997{
998 this_cpu_inc(miniq->cpu_qstats->drops);
999}
1000
1001struct mini_Qdisc_pair {
1002 struct mini_Qdisc miniq1;
1003 struct mini_Qdisc miniq2;
1004 struct mini_Qdisc __rcu **p_miniq;
1005};
1006
1007void mini_qdisc_pair_swap(struct mini_Qdisc_pair *miniqp,
1008 struct tcf_proto *tp_head);
1009void mini_qdisc_pair_init(struct mini_Qdisc_pair *miniqp, struct Qdisc *qdisc,
1010 struct mini_Qdisc __rcu **p_miniq);
1011
1da177e4 1012#endif