1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __NET_SCHED_GENERIC_H
3 #define __NET_SCHED_GENERIC_H
4
5 #include <linux/netdevice.h>
6 #include <linux/types.h>
7 #include <linux/rcupdate.h>
8 #include <linux/pkt_sched.h>
9 #include <linux/pkt_cls.h>
10 #include <linux/percpu.h>
11 #include <linux/dynamic_queue_limits.h>
12 #include <linux/list.h>
13 #include <linux/refcount.h>
14 #include <linux/workqueue.h>
15 #include <linux/mutex.h>
16 #include <linux/rwsem.h>
17 #include <linux/atomic.h>
18 #include <linux/hashtable.h>
19 #include <net/gen_stats.h>
20 #include <net/rtnetlink.h>
21 #include <net/flow_offload.h>
22 #include <linux/xarray.h>
23 #include <net/dropreason-qdisc.h>
24
25 struct Qdisc_ops;
26 struct qdisc_walker;
27 struct tcf_walker;
28 struct module;
29 struct bpf_flow_keys;
30 struct Qdisc;
31 struct netdev_queue;
32
33 struct qdisc_rate_table {
34 struct tc_ratespec rate;
35 u32 data[256];
36 struct qdisc_rate_table *next;
37 int refcnt;
38 };
39
40 enum qdisc_state_t {
41 __QDISC_STATE_SCHED,
42 __QDISC_STATE_DEACTIVATED,
43 __QDISC_STATE_MISSED,
44 __QDISC_STATE_DRAINING,
45 };
46
47 #define QDISC_STATE_MISSED BIT(__QDISC_STATE_MISSED)
48 #define QDISC_STATE_DRAINING BIT(__QDISC_STATE_DRAINING)
49
50 #define QDISC_STATE_NON_EMPTY (QDISC_STATE_MISSED | \
51 QDISC_STATE_DRAINING)
52
53 struct qdisc_size_table {
54 struct rcu_head rcu;
55 struct list_head list;
56 struct tc_sizespec szopts;
57 int refcnt;
58 u16 data[];
59 };
60
61 /* similar to sk_buff_head, but skb->prev pointer is undefined. */
62 struct qdisc_skb_head {
63 struct sk_buff *head;
64 struct sk_buff *tail;
65 __u32 qlen;
66 spinlock_t lock;
67 };
68
69 struct Qdisc {
70 int (*enqueue)(struct sk_buff *skb,
71 struct Qdisc *sch,
72 struct sk_buff **to_free);
73 struct sk_buff * (*dequeue)(struct Qdisc *sch);
74 unsigned int flags;
75 #define TCQ_F_BUILTIN 1
76 #define TCQ_F_INGRESS 2
77 #define TCQ_F_CAN_BYPASS 4
78 #define TCQ_F_MQROOT 8
79 #define TCQ_F_ONETXQUEUE 0x10 /* dequeue_skb() can assume all skbs are for
80 * q->dev_queue : It can test
81 * netif_xmit_frozen_or_stopped() before
82 * dequeueing next packet.
83 * Its true for MQ/MQPRIO slaves, or non
84 * multiqueue device.
85 */
86 #define TCQ_F_WARN_NONWC (1 << 16)
87 #define TCQ_F_CPUSTATS 0x20 /* run using percpu statistics */
88 #define TCQ_F_NOPARENT 0x40 /* root of its hierarchy :
89 * qdisc_tree_reduce_backlog() should stop.
90 */
91 #define TCQ_F_INVISIBLE 0x80 /* invisible by default in dump */
92 #define TCQ_F_NOLOCK 0x100 /* qdisc does not require locking */
93 #define TCQ_F_OFFLOADED 0x200 /* qdisc is offloaded to HW */
94 #define TCQ_F_DEQUEUE_DROPS 0x400 /* ->dequeue() can drop packets in q->to_free */
95
96 u32 limit;
97 const struct Qdisc_ops *ops;
98 struct qdisc_size_table __rcu *stab;
99 struct hlist_node hash;
100 u32 handle;
101 u32 parent;
102 int depth;
103
104 struct netdev_queue *dev_queue;
105
106 struct net_rate_estimator __rcu *rate_est;
107 struct gnet_stats_basic_sync __percpu *cpu_bstats;
108 struct gnet_stats_queue __percpu *cpu_qstats;
109 int pad;
110 refcount_t refcnt;
111
112 /* Cache line potentially dirtied in dequeue() or __netif_reschedule(). */
113 __cacheline_group_begin(Qdisc_read_mostly) ____cacheline_aligned;
114 struct sk_buff_head gso_skb;
115 struct Qdisc *next_sched;
116 struct sk_buff_head skb_bad_txq;
117 __cacheline_group_end(Qdisc_read_mostly);
118
119 /* Fields dirtied in dequeue() fast path. */
120 __cacheline_group_begin(Qdisc_write) ____cacheline_aligned;
121 struct qdisc_skb_head q;
122 unsigned long state;
123 struct gnet_stats_basic_sync bstats;
124 bool running; /* must be written under qdisc spinlock */
125
126 /* Note : we only change qstats.backlog in fast path. */
127 struct gnet_stats_queue qstats;
128
129 struct sk_buff *to_free;
130 __cacheline_group_end(Qdisc_write);
131
132
133 atomic_long_t defer_count ____cacheline_aligned_in_smp;
134 struct llist_head defer_list;
135
136 spinlock_t seqlock;
137
138 struct rcu_head rcu;
139 netdevice_tracker dev_tracker;
140 struct lock_class_key root_lock_key;
141 /* private data */
142 long privdata[] ____cacheline_aligned;
143 };
144
qdisc_refcount_inc(struct Qdisc * qdisc)145 static inline void qdisc_refcount_inc(struct Qdisc *qdisc)
146 {
147 if (qdisc->flags & TCQ_F_BUILTIN)
148 return;
149 refcount_inc(&qdisc->refcnt);
150 }
151
qdisc_refcount_dec_if_one(struct Qdisc * qdisc)152 static inline bool qdisc_refcount_dec_if_one(struct Qdisc *qdisc)
153 {
154 if (qdisc->flags & TCQ_F_BUILTIN)
155 return true;
156 return refcount_dec_if_one(&qdisc->refcnt);
157 }
158
159 /* Intended to be used by unlocked users, when concurrent qdisc release is
160 * possible.
161 */
162
qdisc_refcount_inc_nz(struct Qdisc * qdisc)163 static inline struct Qdisc *qdisc_refcount_inc_nz(struct Qdisc *qdisc)
164 {
165 if (qdisc->flags & TCQ_F_BUILTIN)
166 return qdisc;
167 if (refcount_inc_not_zero(&qdisc->refcnt))
168 return qdisc;
169 return NULL;
170 }
171
172 /* For !TCQ_F_NOLOCK qdisc: callers must either call this within a qdisc
173 * root_lock section, or provide their own memory barriers -- ordering
174 * against qdisc_run_begin/end() atomic bit operations.
175 */
qdisc_is_running(struct Qdisc * qdisc)176 static inline bool qdisc_is_running(struct Qdisc *qdisc)
177 {
178 if (qdisc->flags & TCQ_F_NOLOCK)
179 return spin_is_locked(&qdisc->seqlock);
180 return READ_ONCE(qdisc->running);
181 }
182
nolock_qdisc_is_empty(const struct Qdisc * qdisc)183 static inline bool nolock_qdisc_is_empty(const struct Qdisc *qdisc)
184 {
185 return !(READ_ONCE(qdisc->state) & QDISC_STATE_NON_EMPTY);
186 }
187
qdisc_is_percpu_stats(const struct Qdisc * q)188 static inline bool qdisc_is_percpu_stats(const struct Qdisc *q)
189 {
190 return q->flags & TCQ_F_CPUSTATS;
191 }
192
qdisc_is_empty(const struct Qdisc * qdisc)193 static inline bool qdisc_is_empty(const struct Qdisc *qdisc)
194 {
195 if (qdisc_is_percpu_stats(qdisc))
196 return nolock_qdisc_is_empty(qdisc);
197 return !READ_ONCE(qdisc->q.qlen);
198 }
199
200 /* For !TCQ_F_NOLOCK qdisc, qdisc_run_begin/end() must be invoked with
201 * the qdisc root lock acquired.
202 */
qdisc_run_begin(struct Qdisc * qdisc)203 static inline bool qdisc_run_begin(struct Qdisc *qdisc)
204 {
205 if (qdisc->flags & TCQ_F_NOLOCK) {
206 if (spin_trylock(&qdisc->seqlock))
207 return true;
208
209 /* No need to insist if the MISSED flag was already set.
210 * Note that test_and_set_bit() also gives us memory ordering
211 * guarantees wrt potential earlier enqueue() and below
212 * spin_trylock(), both of which are necessary to prevent races
213 */
214 if (test_and_set_bit(__QDISC_STATE_MISSED, &qdisc->state))
215 return false;
216
217 /* Try to take the lock again to make sure that we will either
218 * grab it or the CPU that still has it will see MISSED set
219 * when testing it in qdisc_run_end()
220 */
221 return spin_trylock(&qdisc->seqlock);
222 }
223 if (READ_ONCE(qdisc->running))
224 return false;
225 WRITE_ONCE(qdisc->running, true);
226 return true;
227 }
228
qdisc_run_end(struct Qdisc * qdisc)229 static inline struct sk_buff *qdisc_run_end(struct Qdisc *qdisc)
230 {
231 struct sk_buff *to_free = NULL;
232
233 if (qdisc->flags & TCQ_F_NOLOCK) {
234 spin_unlock(&qdisc->seqlock);
235
236 /* spin_unlock() only has store-release semantic. The unlock
237 * and test_bit() ordering is a store-load ordering, so a full
238 * memory barrier is needed here.
239 */
240 smp_mb();
241
242 if (unlikely(test_bit(__QDISC_STATE_MISSED,
243 &qdisc->state)))
244 __netif_schedule(qdisc);
245 return NULL;
246 }
247
248 if (qdisc->flags & TCQ_F_DEQUEUE_DROPS) {
249 to_free = qdisc->to_free;
250 if (to_free)
251 qdisc->to_free = NULL;
252 }
253 WRITE_ONCE(qdisc->running, false);
254 return to_free;
255 }
256
qdisc_may_bulk(const struct Qdisc * qdisc)257 static inline bool qdisc_may_bulk(const struct Qdisc *qdisc)
258 {
259 return qdisc->flags & TCQ_F_ONETXQUEUE;
260 }
261
qdisc_avail_bulklimit(const struct netdev_queue * txq)262 static inline int qdisc_avail_bulklimit(const struct netdev_queue *txq)
263 {
264 return netdev_queue_dql_avail(txq);
265 }
266
267 struct Qdisc_class_ops {
268 unsigned int flags;
269 /* Child qdisc manipulation */
270 struct netdev_queue * (*select_queue)(struct Qdisc *, struct tcmsg *);
271 int (*graft)(struct Qdisc *, unsigned long cl,
272 struct Qdisc *, struct Qdisc **,
273 struct netlink_ext_ack *extack);
274 struct Qdisc * (*leaf)(struct Qdisc *, unsigned long cl);
275 void (*qlen_notify)(struct Qdisc *, unsigned long);
276
277 /* Class manipulation routines */
278 unsigned long (*find)(struct Qdisc *, u32 classid);
279 int (*change)(struct Qdisc *, u32, u32,
280 struct nlattr **, unsigned long *,
281 struct netlink_ext_ack *);
282 int (*delete)(struct Qdisc *, unsigned long,
283 struct netlink_ext_ack *);
284 void (*walk)(struct Qdisc *, struct qdisc_walker * arg);
285
286 /* Filter manipulation */
287 struct tcf_block * (*tcf_block)(struct Qdisc *sch,
288 unsigned long arg,
289 struct netlink_ext_ack *extack);
290 unsigned long (*bind_tcf)(struct Qdisc *, unsigned long,
291 u32 classid);
292 void (*unbind_tcf)(struct Qdisc *, unsigned long);
293
294 /* rtnetlink specific */
295 int (*dump)(struct Qdisc *, unsigned long,
296 struct sk_buff *skb, struct tcmsg*);
297 int (*dump_stats)(struct Qdisc *, unsigned long,
298 struct gnet_dump *);
299 };
300
301 /* Qdisc_class_ops flag values */
302
303 /* Implements API that doesn't require rtnl lock */
304 enum qdisc_class_ops_flags {
305 QDISC_CLASS_OPS_DOIT_UNLOCKED = 1,
306 };
307
308 struct Qdisc_ops {
309 struct Qdisc_ops *next;
310 const struct Qdisc_class_ops *cl_ops;
311 char id[IFNAMSIZ];
312 int priv_size;
313 unsigned int static_flags;
314
315 int (*enqueue)(struct sk_buff *skb,
316 struct Qdisc *sch,
317 struct sk_buff **to_free);
318 struct sk_buff * (*dequeue)(struct Qdisc *);
319 struct sk_buff * (*peek)(struct Qdisc *);
320
321 int (*init)(struct Qdisc *sch, struct nlattr *arg,
322 struct netlink_ext_ack *extack);
323 void (*reset)(struct Qdisc *);
324 void (*destroy)(struct Qdisc *);
325 int (*change)(struct Qdisc *sch,
326 struct nlattr *arg,
327 struct netlink_ext_ack *extack);
328 void (*attach)(struct Qdisc *sch);
329 int (*change_tx_queue_len)(struct Qdisc *, unsigned int);
330 void (*change_real_num_tx)(struct Qdisc *sch,
331 unsigned int new_real_tx);
332
333 int (*dump)(struct Qdisc *, struct sk_buff *);
334 int (*dump_stats)(struct Qdisc *, struct gnet_dump *);
335
336 void (*ingress_block_set)(struct Qdisc *sch,
337 u32 block_index);
338 void (*egress_block_set)(struct Qdisc *sch,
339 u32 block_index);
340 u32 (*ingress_block_get)(struct Qdisc *sch);
341 u32 (*egress_block_get)(struct Qdisc *sch);
342
343 struct module *owner;
344 };
345
346 struct tcf_result {
347 union {
348 struct {
349 unsigned long class;
350 u32 classid;
351 };
352 const struct tcf_proto *goto_tp;
353 };
354 };
355
356 struct tcf_chain;
357
358 struct tcf_proto_ops {
359 struct list_head head;
360 char kind[IFNAMSIZ];
361
362 int (*classify)(struct sk_buff *,
363 const struct tcf_proto *,
364 struct tcf_result *);
365 int (*init)(struct tcf_proto*);
366 void (*destroy)(struct tcf_proto *tp, bool rtnl_held,
367 struct netlink_ext_ack *extack);
368
369 void* (*get)(struct tcf_proto*, u32 handle);
370 void (*put)(struct tcf_proto *tp, void *f);
371 int (*change)(struct net *net, struct sk_buff *,
372 struct tcf_proto*, unsigned long,
373 u32 handle, struct nlattr **,
374 void **, u32,
375 struct netlink_ext_ack *);
376 int (*delete)(struct tcf_proto *tp, void *arg,
377 bool *last, bool rtnl_held,
378 struct netlink_ext_ack *);
379 bool (*delete_empty)(struct tcf_proto *tp);
380 void (*walk)(struct tcf_proto *tp,
381 struct tcf_walker *arg, bool rtnl_held);
382 int (*reoffload)(struct tcf_proto *tp, bool add,
383 flow_setup_cb_t *cb, void *cb_priv,
384 struct netlink_ext_ack *extack);
385 void (*hw_add)(struct tcf_proto *tp,
386 void *type_data);
387 void (*hw_del)(struct tcf_proto *tp,
388 void *type_data);
389 void (*bind_class)(void *, u32, unsigned long,
390 void *, unsigned long);
391 void * (*tmplt_create)(struct net *net,
392 struct tcf_chain *chain,
393 struct nlattr **tca,
394 struct netlink_ext_ack *extack);
395 void (*tmplt_destroy)(void *tmplt_priv);
396 void (*tmplt_reoffload)(struct tcf_chain *chain,
397 bool add,
398 flow_setup_cb_t *cb,
399 void *cb_priv);
400 struct tcf_exts * (*get_exts)(const struct tcf_proto *tp,
401 u32 handle);
402
403 /* rtnetlink specific */
404 int (*dump)(struct net*, struct tcf_proto*, void *,
405 struct sk_buff *skb, struct tcmsg*,
406 bool);
407 int (*terse_dump)(struct net *net,
408 struct tcf_proto *tp, void *fh,
409 struct sk_buff *skb,
410 struct tcmsg *t, bool rtnl_held);
411 int (*tmplt_dump)(struct sk_buff *skb,
412 struct net *net,
413 void *tmplt_priv);
414
415 struct module *owner;
416 int flags;
417 };
418
419 /* Classifiers setting TCF_PROTO_OPS_DOIT_UNLOCKED in tcf_proto_ops->flags
420 * are expected to implement tcf_proto_ops->delete_empty(), otherwise race
421 * conditions can occur when filters are inserted/deleted simultaneously.
422 */
423 enum tcf_proto_ops_flags {
424 TCF_PROTO_OPS_DOIT_UNLOCKED = 1,
425 };
426
427 struct tcf_proto {
428 /* Fast access part */
429 struct tcf_proto __rcu *next;
430 void __rcu *root;
431
432 /* called under RCU BH lock*/
433 int (*classify)(struct sk_buff *,
434 const struct tcf_proto *,
435 struct tcf_result *);
436 __be16 protocol;
437
438 /* All the rest */
439 u32 prio;
440 void *data;
441 const struct tcf_proto_ops *ops;
442 struct tcf_chain *chain;
443 /* Lock protects tcf_proto shared state and can be used by unlocked
444 * classifiers to protect their private data.
445 */
446 spinlock_t lock;
447 bool deleting;
448 bool counted;
449 bool usesw;
450 refcount_t refcnt;
451 struct rcu_head rcu;
452 struct hlist_node destroy_ht_node;
453 };
454
455 struct qdisc_skb_cb {
456 unsigned int pkt_len;
457 u16 pkt_segs;
458 u16 tc_classid;
459 #define QDISC_CB_PRIV_LEN 20
460 unsigned char data[QDISC_CB_PRIV_LEN];
461
462 u16 slave_dev_queue_mapping;
463 u8 post_ct:1;
464 u8 post_ct_snat:1;
465 u8 post_ct_dnat:1;
466 };
467
468 typedef void tcf_chain_head_change_t(struct tcf_proto *tp_head, void *priv);
469
470 struct tcf_chain {
471 /* Protects filter_chain. */
472 struct mutex filter_chain_lock;
473 struct tcf_proto __rcu *filter_chain;
474 struct list_head list;
475 struct tcf_block *block;
476 u32 index; /* chain index */
477 unsigned int refcnt;
478 unsigned int action_refcnt;
479 bool explicitly_created;
480 bool flushing;
481 const struct tcf_proto_ops *tmplt_ops;
482 void *tmplt_priv;
483 struct rcu_head rcu;
484 };
485
486 struct tcf_block {
487 struct xarray ports; /* datapath accessible */
488 /* Lock protects tcf_block and lifetime-management data of chains
489 * attached to the block (refcnt, action_refcnt, explicitly_created).
490 */
491 struct mutex lock;
492 struct list_head chain_list;
493 u32 index; /* block index for shared blocks */
494 u32 classid; /* which class this block belongs to */
495 refcount_t refcnt;
496 struct net *net;
497 struct Qdisc *q;
498 struct rw_semaphore cb_lock; /* protects cb_list and offload counters */
499 struct flow_block flow_block;
500 struct list_head owner_list;
501 bool keep_dst;
502 atomic_t useswcnt;
503 atomic_t offloadcnt; /* Number of oddloaded filters */
504 unsigned int nooffloaddevcnt; /* Number of devs unable to do offload */
505 unsigned int lockeddevcnt; /* Number of devs that require rtnl lock. */
506 struct {
507 struct tcf_chain *chain;
508 struct list_head filter_chain_list;
509 } chain0;
510 struct rcu_head rcu;
511 DECLARE_HASHTABLE(proto_destroy_ht, 7);
512 struct mutex proto_destroy_lock; /* Lock for proto_destroy hashtable. */
513 };
514
515 struct tcf_block *tcf_block_lookup(struct net *net, u32 block_index);
516
lockdep_tcf_chain_is_locked(struct tcf_chain * chain)517 static inline bool lockdep_tcf_chain_is_locked(struct tcf_chain *chain)
518 {
519 return lockdep_is_held(&chain->filter_chain_lock);
520 }
521
lockdep_tcf_proto_is_locked(struct tcf_proto * tp)522 static inline bool lockdep_tcf_proto_is_locked(struct tcf_proto *tp)
523 {
524 return lockdep_is_held(&tp->lock);
525 }
526
527 #define tcf_chain_dereference(p, chain) \
528 rcu_dereference_protected(p, lockdep_tcf_chain_is_locked(chain))
529
530 #define tcf_proto_dereference(p, tp) \
531 rcu_dereference_protected(p, lockdep_tcf_proto_is_locked(tp))
532
qdisc_cb_private_validate(const struct sk_buff * skb,int sz)533 static inline void qdisc_cb_private_validate(const struct sk_buff *skb, int sz)
534 {
535 struct qdisc_skb_cb *qcb;
536
537 BUILD_BUG_ON(sizeof(skb->cb) < sizeof(*qcb));
538 BUILD_BUG_ON(sizeof(qcb->data) < sz);
539 }
540
qdisc_qlen(const struct Qdisc * q)541 static inline int qdisc_qlen(const struct Qdisc *q)
542 {
543 return q->q.qlen;
544 }
545
qdisc_qlen_lockless(const struct Qdisc * q)546 static inline int qdisc_qlen_lockless(const struct Qdisc *q)
547 {
548 return READ_ONCE(q->q.qlen);
549 }
550
qdisc_qlen_inc(struct Qdisc * q)551 static inline void qdisc_qlen_inc(struct Qdisc *q)
552 {
553 WRITE_ONCE(q->q.qlen, q->q.qlen + 1);
554 }
555
qdisc_qlen_dec(struct Qdisc * q)556 static inline void qdisc_qlen_dec(struct Qdisc *q)
557 {
558 WRITE_ONCE(q->q.qlen, q->q.qlen - 1);
559 }
560
qdisc_qlen_sum(const struct Qdisc * q)561 static inline int qdisc_qlen_sum(const struct Qdisc *q)
562 {
563 __u32 qlen = q->qstats.qlen;
564 int i;
565
566 if (qdisc_is_percpu_stats(q)) {
567 for_each_possible_cpu(i)
568 qlen += READ_ONCE(per_cpu_ptr(q->cpu_qstats, i)->qlen);
569 } else {
570 qlen += qdisc_qlen_lockless(q);
571 }
572
573 return qlen;
574 }
575
qdisc_skb_cb(const struct sk_buff * skb)576 static inline struct qdisc_skb_cb *qdisc_skb_cb(const struct sk_buff *skb)
577 {
578 return (struct qdisc_skb_cb *)skb->cb;
579 }
580
qdisc_lock(struct Qdisc * qdisc)581 static inline spinlock_t *qdisc_lock(struct Qdisc *qdisc)
582 {
583 return &qdisc->q.lock;
584 }
585
qdisc_root(const struct Qdisc * qdisc)586 static inline struct Qdisc *qdisc_root(const struct Qdisc *qdisc)
587 {
588 struct Qdisc *q = rcu_dereference_rtnl(qdisc->dev_queue->qdisc);
589
590 return q;
591 }
592
qdisc_root_bh(const struct Qdisc * qdisc)593 static inline struct Qdisc *qdisc_root_bh(const struct Qdisc *qdisc)
594 {
595 return rcu_dereference_bh(qdisc->dev_queue->qdisc);
596 }
597
qdisc_root_sleeping(const struct Qdisc * qdisc)598 static inline struct Qdisc *qdisc_root_sleeping(const struct Qdisc *qdisc)
599 {
600 return rcu_dereference_rtnl(qdisc->dev_queue->qdisc_sleeping);
601 }
602
qdisc_root_sleeping_lock(const struct Qdisc * qdisc)603 static inline spinlock_t *qdisc_root_sleeping_lock(const struct Qdisc *qdisc)
604 {
605 struct Qdisc *root = qdisc_root_sleeping(qdisc);
606
607 ASSERT_RTNL();
608 return qdisc_lock(root);
609 }
610
qdisc_dev(const struct Qdisc * qdisc)611 static inline struct net_device *qdisc_dev(const struct Qdisc *qdisc)
612 {
613 return qdisc->dev_queue->dev;
614 }
615
sch_tree_lock(struct Qdisc * q)616 static inline void sch_tree_lock(struct Qdisc *q)
617 {
618 if (q->flags & TCQ_F_MQROOT)
619 spin_lock_bh(qdisc_lock(q));
620 else
621 spin_lock_bh(qdisc_root_sleeping_lock(q));
622 }
623
sch_tree_unlock(struct Qdisc * q)624 static inline void sch_tree_unlock(struct Qdisc *q)
625 {
626 if (q->flags & TCQ_F_MQROOT)
627 spin_unlock_bh(qdisc_lock(q));
628 else
629 spin_unlock_bh(qdisc_root_sleeping_lock(q));
630 }
631
632 extern struct Qdisc noop_qdisc;
633 extern struct Qdisc_ops noop_qdisc_ops;
634 extern struct Qdisc_ops pfifo_fast_ops;
635 extern const u8 sch_default_prio2band[TC_PRIO_MAX + 1];
636 extern struct Qdisc_ops mq_qdisc_ops;
637 extern struct Qdisc_ops noqueue_qdisc_ops;
638 extern const struct Qdisc_ops *default_qdisc_ops;
639 static inline const struct Qdisc_ops *
get_default_qdisc_ops(const struct net_device * dev,int ntx)640 get_default_qdisc_ops(const struct net_device *dev, int ntx)
641 {
642 return ntx < dev->real_num_tx_queues ?
643 default_qdisc_ops : &pfifo_fast_ops;
644 }
645
646 struct Qdisc_class_common {
647 u32 classid;
648 unsigned int filter_cnt;
649 struct hlist_node hnode;
650 };
651
652 struct Qdisc_class_hash {
653 struct hlist_head *hash;
654 unsigned int hashsize;
655 unsigned int hashmask;
656 unsigned int hashelems;
657 };
658
qdisc_class_hash(u32 id,u32 mask)659 static inline unsigned int qdisc_class_hash(u32 id, u32 mask)
660 {
661 id ^= id >> 8;
662 id ^= id >> 4;
663 return id & mask;
664 }
665
666 static inline struct Qdisc_class_common *
qdisc_class_find(const struct Qdisc_class_hash * hash,u32 id)667 qdisc_class_find(const struct Qdisc_class_hash *hash, u32 id)
668 {
669 struct Qdisc_class_common *cl;
670 unsigned int h;
671
672 if (!id)
673 return NULL;
674
675 h = qdisc_class_hash(id, hash->hashmask);
676 hlist_for_each_entry(cl, &hash->hash[h], hnode) {
677 if (cl->classid == id)
678 return cl;
679 }
680 return NULL;
681 }
682
qdisc_class_in_use(const struct Qdisc_class_common * cl)683 static inline bool qdisc_class_in_use(const struct Qdisc_class_common *cl)
684 {
685 return cl->filter_cnt > 0;
686 }
687
qdisc_class_get(struct Qdisc_class_common * cl)688 static inline void qdisc_class_get(struct Qdisc_class_common *cl)
689 {
690 unsigned int res;
691
692 if (check_add_overflow(cl->filter_cnt, 1, &res))
693 WARN(1, "Qdisc class overflow");
694
695 cl->filter_cnt = res;
696 }
697
qdisc_class_put(struct Qdisc_class_common * cl)698 static inline void qdisc_class_put(struct Qdisc_class_common *cl)
699 {
700 unsigned int res;
701
702 if (check_sub_overflow(cl->filter_cnt, 1, &res))
703 WARN(1, "Qdisc class underflow");
704
705 cl->filter_cnt = res;
706 }
707
tc_classid_to_hwtc(struct net_device * dev,u32 classid)708 static inline int tc_classid_to_hwtc(struct net_device *dev, u32 classid)
709 {
710 u32 hwtc = TC_H_MIN(classid) - TC_H_MIN_PRIORITY;
711
712 return (hwtc < netdev_get_num_tc(dev)) ? hwtc : -EINVAL;
713 }
714
715 int qdisc_class_hash_init(struct Qdisc_class_hash *);
716 void qdisc_class_hash_insert(struct Qdisc_class_hash *,
717 struct Qdisc_class_common *);
718 void qdisc_class_hash_remove(struct Qdisc_class_hash *,
719 struct Qdisc_class_common *);
720 void qdisc_class_hash_grow(struct Qdisc *, struct Qdisc_class_hash *);
721 void qdisc_class_hash_destroy(struct Qdisc_class_hash *);
722
723 int dev_qdisc_change_tx_queue_len(struct net_device *dev);
724 void dev_qdisc_change_real_num_tx(struct net_device *dev,
725 unsigned int new_real_tx);
726 void dev_init_scheduler(struct net_device *dev);
727 void dev_shutdown(struct net_device *dev);
728 void dev_activate(struct net_device *dev);
729 void dev_deactivate(struct net_device *dev, bool reset_needed);
730 void dev_deactivate_many(struct list_head *head, bool reset_needed);
731 struct Qdisc *dev_graft_qdisc(struct netdev_queue *dev_queue,
732 struct Qdisc *qdisc);
733 void qdisc_reset(struct Qdisc *qdisc);
734 void qdisc_destroy(struct Qdisc *qdisc);
735 void qdisc_put(struct Qdisc *qdisc);
736 void qdisc_put_unlocked(struct Qdisc *qdisc);
737 void qdisc_tree_reduce_backlog(struct Qdisc *qdisc, int n, int len);
738
dev_reset_queue(struct net_device * dev,struct netdev_queue * dev_queue,void * _unused)739 static inline void dev_reset_queue(struct net_device *dev,
740 struct netdev_queue *dev_queue,
741 void *_unused)
742 {
743 struct Qdisc *qdisc;
744 bool nolock;
745
746 qdisc = rtnl_dereference(dev_queue->qdisc_sleeping);
747 if (!qdisc)
748 return;
749
750 nolock = qdisc->flags & TCQ_F_NOLOCK;
751
752 if (nolock)
753 spin_lock_bh(&qdisc->seqlock);
754 spin_lock_bh(qdisc_lock(qdisc));
755
756 qdisc_reset(qdisc);
757
758 spin_unlock_bh(qdisc_lock(qdisc));
759 if (nolock) {
760 clear_bit(__QDISC_STATE_MISSED, &qdisc->state);
761 clear_bit(__QDISC_STATE_DRAINING, &qdisc->state);
762 spin_unlock_bh(&qdisc->seqlock);
763 }
764 }
765
766 #ifdef CONFIG_NET_SCHED
767 int qdisc_offload_dump_helper(struct Qdisc *q, enum tc_setup_type type,
768 void *type_data);
769 void qdisc_offload_graft_helper(struct net_device *dev, struct Qdisc *sch,
770 struct Qdisc *new, struct Qdisc *old,
771 enum tc_setup_type type, void *type_data,
772 struct netlink_ext_ack *extack);
773 #else
774 static inline int
qdisc_offload_dump_helper(struct Qdisc * q,enum tc_setup_type type,void * type_data)775 qdisc_offload_dump_helper(struct Qdisc *q, enum tc_setup_type type,
776 void *type_data)
777 {
778 q->flags &= ~TCQ_F_OFFLOADED;
779 return 0;
780 }
781
782 static inline void
qdisc_offload_graft_helper(struct net_device * dev,struct Qdisc * sch,struct Qdisc * new,struct Qdisc * old,enum tc_setup_type type,void * type_data,struct netlink_ext_ack * extack)783 qdisc_offload_graft_helper(struct net_device *dev, struct Qdisc *sch,
784 struct Qdisc *new, struct Qdisc *old,
785 enum tc_setup_type type, void *type_data,
786 struct netlink_ext_ack *extack)
787 {
788 }
789 #endif
790 void qdisc_offload_query_caps(struct net_device *dev,
791 enum tc_setup_type type,
792 void *caps, size_t caps_len);
793 struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue,
794 const struct Qdisc_ops *ops,
795 struct netlink_ext_ack *extack);
796 void qdisc_free_rcu(struct Qdisc *qdisc);
797 struct Qdisc *qdisc_create_dflt(struct netdev_queue *dev_queue,
798 const struct Qdisc_ops *ops, u32 parentid,
799 struct netlink_ext_ack *extack);
800 void __qdisc_calculate_pkt_len(struct sk_buff *skb,
801 const struct qdisc_size_table *stab);
802 int skb_do_redirect(struct sk_buff *);
803
skb_at_tc_ingress(const struct sk_buff * skb)804 static inline bool skb_at_tc_ingress(const struct sk_buff *skb)
805 {
806 #ifdef CONFIG_NET_XGRESS
807 return skb->tc_at_ingress;
808 #else
809 return false;
810 #endif
811 }
812
skb_at_tc_egress(const struct sk_buff * skb)813 static inline bool skb_at_tc_egress(const struct sk_buff *skb)
814 {
815 #ifdef CONFIG_NET_EGRESS
816 return skb->nf_skip_egress && !skb_at_tc_ingress(skb);
817 #else
818 return false;
819 #endif
820 }
821
skb_skip_tc_classify(struct sk_buff * skb)822 static inline bool skb_skip_tc_classify(struct sk_buff *skb)
823 {
824 #ifdef CONFIG_NET_CLS_ACT
825 if (skb->tc_skip_classify) {
826 skb->tc_skip_classify = 0;
827 return true;
828 }
829 #endif
830 return false;
831 }
832
833 /* Reset all TX qdiscs greater than index of a device. */
qdisc_reset_all_tx_gt(struct net_device * dev,unsigned int i)834 static inline void qdisc_reset_all_tx_gt(struct net_device *dev, unsigned int i)
835 {
836 struct Qdisc *qdisc;
837 bool nolock;
838
839 for (; i < dev->num_tx_queues; i++) {
840 qdisc = rtnl_dereference(netdev_get_tx_queue(dev, i)->qdisc);
841 if (qdisc) {
842 nolock = qdisc->flags & TCQ_F_NOLOCK;
843
844 if (nolock)
845 spin_lock_bh(&qdisc->seqlock);
846 spin_lock_bh(qdisc_lock(qdisc));
847 qdisc_reset(qdisc);
848 spin_unlock_bh(qdisc_lock(qdisc));
849 if (nolock) {
850 clear_bit(__QDISC_STATE_MISSED, &qdisc->state);
851 clear_bit(__QDISC_STATE_DRAINING, &qdisc->state);
852 spin_unlock_bh(&qdisc->seqlock);
853 }
854 }
855 }
856 }
857
858 /* Are all TX queues of the device empty? */
qdisc_all_tx_empty(const struct net_device * dev)859 static inline bool qdisc_all_tx_empty(const struct net_device *dev)
860 {
861 unsigned int i;
862
863 rcu_read_lock();
864 for (i = 0; i < dev->num_tx_queues; i++) {
865 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
866 const struct Qdisc *q = rcu_dereference(txq->qdisc);
867
868 if (!qdisc_is_empty(q)) {
869 rcu_read_unlock();
870 return false;
871 }
872 }
873 rcu_read_unlock();
874 return true;
875 }
876
877 /* Are any of the TX qdiscs changing? */
qdisc_tx_changing(const struct net_device * dev)878 static inline bool qdisc_tx_changing(const struct net_device *dev)
879 {
880 unsigned int i;
881
882 for (i = 0; i < dev->num_tx_queues; i++) {
883 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
884
885 if (rcu_access_pointer(txq->qdisc) !=
886 rcu_access_pointer(txq->qdisc_sleeping))
887 return true;
888 }
889 return false;
890 }
891
892 /* "noqueue" qdisc identified by not having any enqueue, see noqueue_init() */
qdisc_txq_has_no_queue(const struct netdev_queue * txq)893 static inline bool qdisc_txq_has_no_queue(const struct netdev_queue *txq)
894 {
895 struct Qdisc *qdisc = rcu_access_pointer(txq->qdisc);
896
897 return qdisc->enqueue == NULL;
898 }
899
900 /* Is the device using the noop qdisc on all queues? */
qdisc_tx_is_noop(const struct net_device * dev)901 static inline bool qdisc_tx_is_noop(const struct net_device *dev)
902 {
903 unsigned int i;
904
905 for (i = 0; i < dev->num_tx_queues; i++) {
906 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
907 if (rcu_access_pointer(txq->qdisc) != &noop_qdisc)
908 return false;
909 }
910 return true;
911 }
912
qdisc_pkt_len(const struct sk_buff * skb)913 static inline unsigned int qdisc_pkt_len(const struct sk_buff *skb)
914 {
915 return qdisc_skb_cb(skb)->pkt_len;
916 }
917
qdisc_pkt_segs(const struct sk_buff * skb)918 static inline unsigned int qdisc_pkt_segs(const struct sk_buff *skb)
919 {
920 u32 pkt_segs = qdisc_skb_cb(skb)->pkt_segs;
921
922 DEBUG_NET_WARN_ON_ONCE(pkt_segs !=
923 (skb_is_gso(skb) ? skb_shinfo(skb)->gso_segs : 1));
924 return pkt_segs;
925 }
926
927 /* additional qdisc xmit flags (NET_XMIT_MASK in linux/netdevice.h) */
928 enum net_xmit_qdisc_t {
929 __NET_XMIT_STOLEN = 0x00010000,
930 __NET_XMIT_BYPASS = 0x00020000,
931 };
932
933 #ifdef CONFIG_NET_CLS_ACT
934 #define net_xmit_drop_count(e) ((e) & __NET_XMIT_STOLEN ? 0 : 1)
935 #else
936 #define net_xmit_drop_count(e) (1)
937 #endif
938
qdisc_calculate_pkt_len(struct sk_buff * skb,const struct Qdisc * sch)939 static inline void qdisc_calculate_pkt_len(struct sk_buff *skb,
940 const struct Qdisc *sch)
941 {
942 #ifdef CONFIG_NET_SCHED
943 struct qdisc_size_table *stab = rcu_dereference_bh(sch->stab);
944
945 if (stab)
946 __qdisc_calculate_pkt_len(skb, stab);
947 #endif
948 }
949
qdisc_enqueue(struct sk_buff * skb,struct Qdisc * sch,struct sk_buff ** to_free)950 static inline int qdisc_enqueue(struct sk_buff *skb, struct Qdisc *sch,
951 struct sk_buff **to_free)
952 {
953 return sch->enqueue(skb, sch, to_free);
954 }
955
_bstats_update(struct gnet_stats_basic_sync * bstats,__u64 bytes,__u64 packets)956 static inline void _bstats_update(struct gnet_stats_basic_sync *bstats,
957 __u64 bytes, __u64 packets)
958 {
959 u64_stats_update_begin(&bstats->syncp);
960 u64_stats_add(&bstats->bytes, bytes);
961 u64_stats_add(&bstats->packets, packets);
962 u64_stats_update_end(&bstats->syncp);
963 }
964
_bstats_set(struct gnet_stats_basic_sync * bstats,u64 bytes,u64 packets)965 static inline void _bstats_set(struct gnet_stats_basic_sync *bstats,
966 u64 bytes, u64 packets)
967 {
968 u64_stats_update_begin(&bstats->syncp);
969 u64_stats_set(&bstats->bytes, bytes);
970 u64_stats_set(&bstats->packets, packets);
971 u64_stats_update_end(&bstats->syncp);
972 }
973
bstats_update(struct gnet_stats_basic_sync * bstats,const struct sk_buff * skb)974 static inline void bstats_update(struct gnet_stats_basic_sync *bstats,
975 const struct sk_buff *skb)
976 {
977 _bstats_update(bstats, qdisc_pkt_len(skb), qdisc_pkt_segs(skb));
978 }
979
qdisc_bstats_cpu_update(struct Qdisc * sch,const struct sk_buff * skb)980 static inline void qdisc_bstats_cpu_update(struct Qdisc *sch,
981 const struct sk_buff *skb)
982 {
983 bstats_update(this_cpu_ptr(sch->cpu_bstats), skb);
984 }
985
qdisc_bstats_update(struct Qdisc * sch,const struct sk_buff * skb)986 static inline void qdisc_bstats_update(struct Qdisc *sch,
987 const struct sk_buff *skb)
988 {
989 bstats_update(&sch->bstats, skb);
990 }
991
qstats_backlog_sub(struct Qdisc * sch,u32 val)992 static inline void qstats_backlog_sub(struct Qdisc *sch, u32 val)
993 {
994 WRITE_ONCE(sch->qstats.backlog, sch->qstats.backlog - val);
995 }
996
qdisc_qstats_backlog_dec(struct Qdisc * sch,const struct sk_buff * skb)997 static inline void qdisc_qstats_backlog_dec(struct Qdisc *sch,
998 const struct sk_buff *skb)
999 {
1000 qstats_backlog_sub(sch, qdisc_pkt_len(skb));
1001 }
1002
qdisc_qstats_cpu_backlog_dec(struct Qdisc * sch,const struct sk_buff * skb)1003 static inline void qdisc_qstats_cpu_backlog_dec(struct Qdisc *sch,
1004 const struct sk_buff *skb)
1005 {
1006 this_cpu_sub(sch->cpu_qstats->backlog, qdisc_pkt_len(skb));
1007 }
1008
qstats_backlog_add(struct Qdisc * sch,u32 val)1009 static inline void qstats_backlog_add(struct Qdisc *sch, u32 val)
1010 {
1011 WRITE_ONCE(sch->qstats.backlog, sch->qstats.backlog + val);
1012 }
1013
qdisc_qstats_backlog_inc(struct Qdisc * sch,const struct sk_buff * skb)1014 static inline void qdisc_qstats_backlog_inc(struct Qdisc *sch,
1015 const struct sk_buff *skb)
1016 {
1017 qstats_backlog_add(sch, qdisc_pkt_len(skb));
1018 }
1019
qdisc_qstats_cpu_backlog_inc(struct Qdisc * sch,const struct sk_buff * skb)1020 static inline void qdisc_qstats_cpu_backlog_inc(struct Qdisc *sch,
1021 const struct sk_buff *skb)
1022 {
1023 this_cpu_add(sch->cpu_qstats->backlog, qdisc_pkt_len(skb));
1024 }
1025
qdisc_qstats_cpu_qlen_inc(struct Qdisc * sch)1026 static inline void qdisc_qstats_cpu_qlen_inc(struct Qdisc *sch)
1027 {
1028 this_cpu_inc(sch->cpu_qstats->qlen);
1029 }
1030
qdisc_qstats_cpu_qlen_dec(struct Qdisc * sch)1031 static inline void qdisc_qstats_cpu_qlen_dec(struct Qdisc *sch)
1032 {
1033 this_cpu_dec(sch->cpu_qstats->qlen);
1034 }
1035
qdisc_qstats_cpu_requeues_inc(struct Qdisc * sch)1036 static inline void qdisc_qstats_cpu_requeues_inc(struct Qdisc *sch)
1037 {
1038 this_cpu_inc(sch->cpu_qstats->requeues);
1039 }
1040
__qdisc_qstats_drop(struct Qdisc * sch,int count)1041 static inline void __qdisc_qstats_drop(struct Qdisc *sch, int count)
1042 {
1043 WRITE_ONCE(sch->qstats.drops, sch->qstats.drops + count);
1044 }
1045
qstats_drop_inc(struct gnet_stats_queue * qstats)1046 static inline void qstats_drop_inc(struct gnet_stats_queue *qstats)
1047 {
1048 WRITE_ONCE(qstats->drops, qstats->drops + 1);
1049 }
1050
qstats_cpu_drop_inc(struct gnet_stats_queue __percpu * qstats)1051 static inline void qstats_cpu_drop_inc(struct gnet_stats_queue __percpu *qstats)
1052 {
1053 this_cpu_inc(qstats->drops);
1054 }
1055
qstats_cpu_overlimit_inc(struct gnet_stats_queue __percpu * qstats)1056 static inline void qstats_cpu_overlimit_inc(struct gnet_stats_queue __percpu *qstats)
1057 {
1058 this_cpu_inc(qstats->overlimits);
1059 }
1060
qdisc_qstats_drop(struct Qdisc * sch)1061 static inline void qdisc_qstats_drop(struct Qdisc *sch)
1062 {
1063 qstats_drop_inc(&sch->qstats);
1064 }
1065
qdisc_qstats_cpu_drop(struct Qdisc * sch)1066 static inline void qdisc_qstats_cpu_drop(struct Qdisc *sch)
1067 {
1068 this_cpu_inc(sch->cpu_qstats->drops);
1069 }
1070
qdisc_qstats_overlimit(struct Qdisc * sch)1071 static inline void qdisc_qstats_overlimit(struct Qdisc *sch)
1072 {
1073 WRITE_ONCE(sch->qstats.overlimits, sch->qstats.overlimits + 1);
1074 }
1075
qdisc_qstats_copy(struct gnet_dump * d,const struct Qdisc * sch)1076 static inline int qdisc_qstats_copy(struct gnet_dump *d, const struct Qdisc *sch)
1077 {
1078 __u32 qlen = qdisc_qlen_sum(sch);
1079
1080 return gnet_stats_copy_queue(d, sch->cpu_qstats, &sch->qstats, qlen);
1081 }
1082
qdisc_qstats_qlen_backlog(const struct Qdisc * sch,u32 * qlen,u32 * backlog)1083 static inline void qdisc_qstats_qlen_backlog(const struct Qdisc *sch,
1084 u32 *qlen, u32 *backlog)
1085 {
1086 struct gnet_stats_queue qstats = { 0 };
1087
1088 gnet_stats_add_queue(&qstats, sch->cpu_qstats, &sch->qstats);
1089 *qlen = qstats.qlen + qdisc_qlen_lockless(sch);
1090 *backlog = qstats.backlog;
1091 }
1092
qdisc_purge_queue(struct Qdisc * sch)1093 static inline void qdisc_purge_queue(struct Qdisc *sch)
1094 {
1095 __u32 qlen, backlog;
1096
1097 qdisc_qstats_qlen_backlog(sch, &qlen, &backlog);
1098 qdisc_reset(sch);
1099 qdisc_tree_reduce_backlog(sch, qlen, backlog);
1100 }
1101
__qdisc_enqueue_tail(struct sk_buff * skb,struct qdisc_skb_head * qh)1102 static inline void __qdisc_enqueue_tail(struct sk_buff *skb,
1103 struct qdisc_skb_head *qh)
1104 {
1105 struct sk_buff *last = qh->tail;
1106
1107 if (last) {
1108 skb->next = NULL;
1109 last->next = skb;
1110 qh->tail = skb;
1111 } else {
1112 qh->tail = skb;
1113 qh->head = skb;
1114 }
1115 WRITE_ONCE(qh->qlen, qh->qlen + 1);
1116 }
1117
qdisc_enqueue_tail(struct sk_buff * skb,struct Qdisc * sch)1118 static inline int qdisc_enqueue_tail(struct sk_buff *skb, struct Qdisc *sch)
1119 {
1120 __qdisc_enqueue_tail(skb, &sch->q);
1121 qdisc_qstats_backlog_inc(sch, skb);
1122 return NET_XMIT_SUCCESS;
1123 }
1124
__qdisc_enqueue_head(struct sk_buff * skb,struct qdisc_skb_head * qh)1125 static inline void __qdisc_enqueue_head(struct sk_buff *skb,
1126 struct qdisc_skb_head *qh)
1127 {
1128 skb->next = qh->head;
1129
1130 if (!qh->head)
1131 qh->tail = skb;
1132 qh->head = skb;
1133 WRITE_ONCE(qh->qlen, qh->qlen + 1);
1134 }
1135
__qdisc_dequeue_head(struct qdisc_skb_head * qh)1136 static inline struct sk_buff *__qdisc_dequeue_head(struct qdisc_skb_head *qh)
1137 {
1138 struct sk_buff *skb = qh->head;
1139
1140 if (likely(skb != NULL)) {
1141 qh->head = skb->next;
1142 WRITE_ONCE(qh->qlen, qh->qlen - 1);
1143 if (qh->head == NULL)
1144 qh->tail = NULL;
1145 skb->next = NULL;
1146 }
1147
1148 return skb;
1149 }
1150
qdisc_dequeue_internal(struct Qdisc * sch,bool direct)1151 static inline struct sk_buff *qdisc_dequeue_internal(struct Qdisc *sch, bool direct)
1152 {
1153 struct sk_buff *skb;
1154
1155 skb = __skb_dequeue(&sch->gso_skb);
1156 if (skb) {
1157 qdisc_qlen_dec(sch);
1158 qdisc_qstats_backlog_dec(sch, skb);
1159 return skb;
1160 }
1161 if (direct) {
1162 skb = __qdisc_dequeue_head(&sch->q);
1163 if (skb)
1164 qdisc_qstats_backlog_dec(sch, skb);
1165 return skb;
1166 } else {
1167 return sch->dequeue(sch);
1168 }
1169 }
1170
qdisc_dequeue_head(struct Qdisc * sch)1171 static inline struct sk_buff *qdisc_dequeue_head(struct Qdisc *sch)
1172 {
1173 struct sk_buff *skb = __qdisc_dequeue_head(&sch->q);
1174
1175 if (likely(skb != NULL)) {
1176 qdisc_qstats_backlog_dec(sch, skb);
1177 qdisc_bstats_update(sch, skb);
1178 }
1179
1180 return skb;
1181 }
1182
1183 struct tc_skb_cb {
1184 struct qdisc_skb_cb qdisc_cb;
1185 u32 drop_reason;
1186
1187 u16 zone; /* Only valid if qdisc_skb_cb(skb)->post_ct = true */
1188 u16 mru;
1189 };
1190
tc_skb_cb(const struct sk_buff * skb)1191 static inline struct tc_skb_cb *tc_skb_cb(const struct sk_buff *skb)
1192 {
1193 struct tc_skb_cb *cb = (struct tc_skb_cb *)skb->cb;
1194
1195 BUILD_BUG_ON(sizeof(*cb) > sizeof_field(struct sk_buff, cb));
1196 return cb;
1197 }
1198
1199 /* TC classifier accessors - use enum skb_drop_reason */
1200 static inline enum skb_drop_reason
tcf_get_drop_reason(const struct sk_buff * skb)1201 tcf_get_drop_reason(const struct sk_buff *skb)
1202 {
1203 return (enum skb_drop_reason)tc_skb_cb(skb)->drop_reason;
1204 }
1205
tcf_set_drop_reason(const struct sk_buff * skb,enum skb_drop_reason reason)1206 static inline void tcf_set_drop_reason(const struct sk_buff *skb,
1207 enum skb_drop_reason reason)
1208 {
1209 tc_skb_cb(skb)->drop_reason = (enum qdisc_drop_reason)reason;
1210 }
1211
1212 /* Qdisc accessors - use enum qdisc_drop_reason */
1213 static inline enum qdisc_drop_reason
tcf_get_qdisc_drop_reason(const struct sk_buff * skb)1214 tcf_get_qdisc_drop_reason(const struct sk_buff *skb)
1215 {
1216 return tc_skb_cb(skb)->drop_reason;
1217 }
1218
tcf_set_qdisc_drop_reason(const struct sk_buff * skb,enum qdisc_drop_reason reason)1219 static inline void tcf_set_qdisc_drop_reason(const struct sk_buff *skb,
1220 enum qdisc_drop_reason reason)
1221 {
1222 tc_skb_cb(skb)->drop_reason = reason;
1223 }
1224
1225 void __tcf_kfree_skb_list(struct sk_buff *skb, struct Qdisc *q,
1226 struct netdev_queue *txq, struct net_device *dev);
1227
tcf_kfree_skb_list(struct sk_buff * skb,struct Qdisc * q,struct netdev_queue * txq,struct net_device * dev)1228 static inline void tcf_kfree_skb_list(struct sk_buff *skb, struct Qdisc *q,
1229 struct netdev_queue *txq,
1230 struct net_device *dev)
1231 {
1232 if (unlikely(skb))
1233 __tcf_kfree_skb_list(skb, q, txq, dev);
1234 }
1235
qdisc_dequeue_drop(struct Qdisc * q,struct sk_buff * skb,enum qdisc_drop_reason reason)1236 static inline void qdisc_dequeue_drop(struct Qdisc *q, struct sk_buff *skb,
1237 enum qdisc_drop_reason reason)
1238 {
1239 struct Qdisc *root;
1240
1241 DEBUG_NET_WARN_ON_ONCE(!(q->flags & TCQ_F_DEQUEUE_DROPS));
1242 DEBUG_NET_WARN_ON_ONCE(q->flags & TCQ_F_NOLOCK);
1243
1244 rcu_read_lock();
1245 root = qdisc_root_sleeping(q);
1246
1247 if (root->flags & TCQ_F_DEQUEUE_DROPS) {
1248 tcf_set_qdisc_drop_reason(skb, reason);
1249 skb->next = root->to_free;
1250 root->to_free = skb;
1251 } else {
1252 kfree_skb_reason(skb, (enum skb_drop_reason)reason);
1253 }
1254 rcu_read_unlock();
1255 }
1256
1257 /* Instead of calling kfree_skb() while root qdisc lock is held,
1258 * queue the skb for future freeing at end of __dev_xmit_skb()
1259 */
__qdisc_drop(struct sk_buff * skb,struct sk_buff ** to_free)1260 static inline void __qdisc_drop(struct sk_buff *skb, struct sk_buff **to_free)
1261 {
1262 skb->next = *to_free;
1263 *to_free = skb;
1264 }
1265
__qdisc_drop_all(struct sk_buff * skb,struct sk_buff ** to_free)1266 static inline void __qdisc_drop_all(struct sk_buff *skb,
1267 struct sk_buff **to_free)
1268 {
1269 if (skb->prev)
1270 skb->prev->next = *to_free;
1271 else
1272 skb->next = *to_free;
1273 *to_free = skb;
1274 }
1275
__qdisc_queue_drop_head(struct Qdisc * sch,struct qdisc_skb_head * qh,struct sk_buff ** to_free)1276 static inline unsigned int __qdisc_queue_drop_head(struct Qdisc *sch,
1277 struct qdisc_skb_head *qh,
1278 struct sk_buff **to_free)
1279 {
1280 struct sk_buff *skb = __qdisc_dequeue_head(qh);
1281
1282 if (likely(skb != NULL)) {
1283 unsigned int len = qdisc_pkt_len(skb);
1284
1285 qdisc_qstats_backlog_dec(sch, skb);
1286 __qdisc_drop(skb, to_free);
1287 return len;
1288 }
1289
1290 return 0;
1291 }
1292
qdisc_peek_head(struct Qdisc * sch)1293 static inline struct sk_buff *qdisc_peek_head(struct Qdisc *sch)
1294 {
1295 const struct qdisc_skb_head *qh = &sch->q;
1296
1297 return qh->head;
1298 }
1299
1300 /* generic pseudo peek method for non-work-conserving qdisc */
qdisc_peek_dequeued(struct Qdisc * sch)1301 static inline struct sk_buff *qdisc_peek_dequeued(struct Qdisc *sch)
1302 {
1303 struct sk_buff *skb = skb_peek(&sch->gso_skb);
1304
1305 /* we can reuse ->gso_skb because peek isn't called for root qdiscs */
1306 if (!skb) {
1307 skb = sch->dequeue(sch);
1308
1309 if (skb) {
1310 __skb_queue_head(&sch->gso_skb, skb);
1311 /* it's still part of the queue */
1312 qdisc_qstats_backlog_inc(sch, skb);
1313 qdisc_qlen_inc(sch);
1314 }
1315 }
1316
1317 return skb;
1318 }
1319
qdisc_update_stats_at_dequeue(struct Qdisc * sch,struct sk_buff * skb)1320 static inline void qdisc_update_stats_at_dequeue(struct Qdisc *sch,
1321 struct sk_buff *skb)
1322 {
1323 if (qdisc_is_percpu_stats(sch)) {
1324 qdisc_qstats_cpu_backlog_dec(sch, skb);
1325 qdisc_bstats_cpu_update(sch, skb);
1326 qdisc_qstats_cpu_qlen_dec(sch);
1327 } else {
1328 qdisc_qstats_backlog_dec(sch, skb);
1329 qdisc_bstats_update(sch, skb);
1330 qdisc_qlen_dec(sch);
1331 }
1332 }
1333
qdisc_update_stats_at_enqueue(struct Qdisc * sch,unsigned int pkt_len)1334 static inline void qdisc_update_stats_at_enqueue(struct Qdisc *sch,
1335 unsigned int pkt_len)
1336 {
1337 if (qdisc_is_percpu_stats(sch)) {
1338 qdisc_qstats_cpu_qlen_inc(sch);
1339 this_cpu_add(sch->cpu_qstats->backlog, pkt_len);
1340 } else {
1341 qstats_backlog_add(sch, pkt_len);
1342 qdisc_qlen_inc(sch);
1343 }
1344 }
1345
1346 /* use instead of qdisc->dequeue() for all qdiscs queried with ->peek() */
qdisc_dequeue_peeked(struct Qdisc * sch)1347 static inline struct sk_buff *qdisc_dequeue_peeked(struct Qdisc *sch)
1348 {
1349 struct sk_buff *skb = skb_peek(&sch->gso_skb);
1350
1351 if (skb) {
1352 skb = __skb_dequeue(&sch->gso_skb);
1353 if (qdisc_is_percpu_stats(sch)) {
1354 qdisc_qstats_cpu_backlog_dec(sch, skb);
1355 qdisc_qstats_cpu_qlen_dec(sch);
1356 } else {
1357 qdisc_qstats_backlog_dec(sch, skb);
1358 qdisc_qlen_dec(sch);
1359 }
1360 } else {
1361 skb = sch->dequeue(sch);
1362 }
1363
1364 return skb;
1365 }
1366
__qdisc_reset_queue(struct qdisc_skb_head * qh)1367 static inline void __qdisc_reset_queue(struct qdisc_skb_head *qh)
1368 {
1369 /*
1370 * We do not know the backlog in bytes of this list, it
1371 * is up to the caller to correct it
1372 */
1373 ASSERT_RTNL();
1374 if (qh->qlen) {
1375 rtnl_kfree_skbs(qh->head, qh->tail);
1376
1377 qh->head = NULL;
1378 qh->tail = NULL;
1379 WRITE_ONCE(qh->qlen, 0);
1380 }
1381 }
1382
qdisc_reset_queue(struct Qdisc * sch)1383 static inline void qdisc_reset_queue(struct Qdisc *sch)
1384 {
1385 __qdisc_reset_queue(&sch->q);
1386 }
1387
qdisc_replace(struct Qdisc * sch,struct Qdisc * new,struct Qdisc ** pold)1388 static inline struct Qdisc *qdisc_replace(struct Qdisc *sch, struct Qdisc *new,
1389 struct Qdisc **pold)
1390 {
1391 struct Qdisc *old;
1392
1393 sch_tree_lock(sch);
1394 old = *pold;
1395 *pold = new;
1396 if (old != NULL)
1397 qdisc_purge_queue(old);
1398 sch_tree_unlock(sch);
1399
1400 return old;
1401 }
1402
rtnl_qdisc_drop(struct sk_buff * skb,struct Qdisc * sch)1403 static inline void rtnl_qdisc_drop(struct sk_buff *skb, struct Qdisc *sch)
1404 {
1405 rtnl_kfree_skbs(skb, skb);
1406 qdisc_qstats_drop(sch);
1407 }
1408
qdisc_drop_cpu(struct sk_buff * skb,struct Qdisc * sch,struct sk_buff ** to_free)1409 static inline int qdisc_drop_cpu(struct sk_buff *skb, struct Qdisc *sch,
1410 struct sk_buff **to_free)
1411 {
1412 __qdisc_drop(skb, to_free);
1413 qdisc_qstats_cpu_drop(sch);
1414
1415 return NET_XMIT_DROP;
1416 }
1417
qdisc_drop(struct sk_buff * skb,struct Qdisc * sch,struct sk_buff ** to_free)1418 static inline int qdisc_drop(struct sk_buff *skb, struct Qdisc *sch,
1419 struct sk_buff **to_free)
1420 {
1421 __qdisc_drop(skb, to_free);
1422 qdisc_qstats_drop(sch);
1423
1424 return NET_XMIT_DROP;
1425 }
1426
qdisc_drop_reason(struct sk_buff * skb,struct Qdisc * sch,struct sk_buff ** to_free,enum qdisc_drop_reason reason)1427 static inline int qdisc_drop_reason(struct sk_buff *skb, struct Qdisc *sch,
1428 struct sk_buff **to_free,
1429 enum qdisc_drop_reason reason)
1430 {
1431 tcf_set_qdisc_drop_reason(skb, reason);
1432 return qdisc_drop(skb, sch, to_free);
1433 }
1434
qdisc_drop_all(struct sk_buff * skb,struct Qdisc * sch,struct sk_buff ** to_free)1435 static inline int qdisc_drop_all(struct sk_buff *skb, struct Qdisc *sch,
1436 struct sk_buff **to_free)
1437 {
1438 __qdisc_drop_all(skb, to_free);
1439 qdisc_qstats_drop(sch);
1440
1441 return NET_XMIT_DROP;
1442 }
1443
1444 struct psched_ratecfg {
1445 u64 rate_bytes_ps; /* bytes per second */
1446 u32 mult;
1447 u16 overhead;
1448 u16 mpu;
1449 u8 linklayer;
1450 u8 shift;
1451 };
1452
psched_l2t_ns(const struct psched_ratecfg * r,unsigned int len)1453 static inline u64 psched_l2t_ns(const struct psched_ratecfg *r,
1454 unsigned int len)
1455 {
1456 len += r->overhead;
1457
1458 if (len < r->mpu)
1459 len = r->mpu;
1460
1461 if (unlikely(r->linklayer == TC_LINKLAYER_ATM))
1462 return ((u64)(DIV_ROUND_UP(len,48)*53) * r->mult) >> r->shift;
1463
1464 return ((u64)len * r->mult) >> r->shift;
1465 }
1466
1467 void psched_ratecfg_precompute(struct psched_ratecfg *r,
1468 const struct tc_ratespec *conf,
1469 u64 rate64);
1470
psched_ratecfg_getrate(struct tc_ratespec * res,const struct psched_ratecfg * r)1471 static inline void psched_ratecfg_getrate(struct tc_ratespec *res,
1472 const struct psched_ratecfg *r)
1473 {
1474 memset(res, 0, sizeof(*res));
1475
1476 /* legacy struct tc_ratespec has a 32bit @rate field
1477 * Qdisc using 64bit rate should add new attributes
1478 * in order to maintain compatibility.
1479 */
1480 res->rate = min_t(u64, r->rate_bytes_ps, ~0U);
1481
1482 res->overhead = r->overhead;
1483 res->mpu = r->mpu;
1484 res->linklayer = (r->linklayer & TC_LINKLAYER_MASK);
1485 }
1486
1487 struct psched_pktrate {
1488 u64 rate_pkts_ps; /* packets per second */
1489 u32 mult;
1490 u8 shift;
1491 };
1492
psched_pkt2t_ns(const struct psched_pktrate * r,unsigned int pkt_num)1493 static inline u64 psched_pkt2t_ns(const struct psched_pktrate *r,
1494 unsigned int pkt_num)
1495 {
1496 return ((u64)pkt_num * r->mult) >> r->shift;
1497 }
1498
1499 void psched_ppscfg_precompute(struct psched_pktrate *r, u64 pktrate64);
1500
1501 /* Mini Qdisc serves for specific needs of ingress/clsact Qdisc.
1502 * The fast path only needs to access filter list and to update stats
1503 */
1504 struct mini_Qdisc {
1505 struct tcf_proto *filter_list;
1506 struct tcf_block *block;
1507 struct gnet_stats_basic_sync __percpu *cpu_bstats;
1508 struct gnet_stats_queue __percpu *cpu_qstats;
1509 unsigned long rcu_state;
1510 };
1511
mini_qdisc_bstats_cpu_update(struct mini_Qdisc * miniq,const struct sk_buff * skb)1512 static inline void mini_qdisc_bstats_cpu_update(struct mini_Qdisc *miniq,
1513 const struct sk_buff *skb)
1514 {
1515 bstats_update(this_cpu_ptr(miniq->cpu_bstats), skb);
1516 }
1517
mini_qdisc_qstats_cpu_drop(struct mini_Qdisc * miniq)1518 static inline void mini_qdisc_qstats_cpu_drop(struct mini_Qdisc *miniq)
1519 {
1520 this_cpu_inc(miniq->cpu_qstats->drops);
1521 }
1522
1523 struct mini_Qdisc_pair {
1524 struct mini_Qdisc miniq1;
1525 struct mini_Qdisc miniq2;
1526 struct mini_Qdisc __rcu **p_miniq;
1527 };
1528
1529 void mini_qdisc_pair_swap(struct mini_Qdisc_pair *miniqp,
1530 struct tcf_proto *tp_head);
1531 void mini_qdisc_pair_init(struct mini_Qdisc_pair *miniqp, struct Qdisc *qdisc,
1532 struct mini_Qdisc __rcu **p_miniq);
1533 void mini_qdisc_pair_block_init(struct mini_Qdisc_pair *miniqp,
1534 struct tcf_block *block);
1535
mini_qdisc_pair_inited(struct mini_Qdisc_pair * miniqp)1536 static inline bool mini_qdisc_pair_inited(struct mini_Qdisc_pair *miniqp)
1537 {
1538 return !!miniqp->p_miniq;
1539 }
1540
1541 void mq_change_real_num_tx(struct Qdisc *sch, unsigned int new_real_tx);
1542
1543 int sch_frag_xmit_hook(struct sk_buff *skb, int (*xmit)(struct sk_buff *skb));
1544
1545 /* Make sure qdisc is no longer in SCHED state. */
qdisc_synchronize(const struct Qdisc * q)1546 static inline void qdisc_synchronize(const struct Qdisc *q)
1547 {
1548 while (test_bit(__QDISC_STATE_SCHED, &q->state))
1549 msleep(1);
1550 }
1551
1552 #endif
1553