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