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