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