xref: /linux/include/net/sch_generic.h (revision 8f7aa3d3c7323f4ca2768a9e74ebbe359c4f8f88)
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 
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 
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 
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  */
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 
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 
184 static inline bool qdisc_is_percpu_stats(const struct Qdisc *q)
185 {
186 	return q->flags & TCQ_F_CPUSTATS;
187 }
188 
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  */
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 
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 
253 static inline bool qdisc_may_bulk(const struct Qdisc *qdisc)
254 {
255 	return qdisc->flags & TCQ_F_ONETXQUEUE;
256 }
257 
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 
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 
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 
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 
537 static inline int qdisc_qlen(const struct Qdisc *q)
538 {
539 	return q->q.qlen;
540 }
541 
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 
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 
562 static inline spinlock_t *qdisc_lock(struct Qdisc *qdisc)
563 {
564 	return &qdisc->q.lock;
565 }
566 
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 
574 static inline struct Qdisc *qdisc_root_bh(const struct Qdisc *qdisc)
575 {
576 	return rcu_dereference_bh(qdisc->dev_queue->qdisc);
577 }
578 
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 
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 
592 static inline struct net_device *qdisc_dev(const struct Qdisc *qdisc)
593 {
594 	return qdisc->dev_queue->dev;
595 }
596 
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 
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 *
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 
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 *
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 
664 static inline bool qdisc_class_in_use(const struct Qdisc_class_common *cl)
665 {
666 	return cl->filter_cnt > 0;
667 }
668 
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 
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 
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
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
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 
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 
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.  */
778 static inline void qdisc_reset_all_tx_gt(struct net_device *dev, unsigned int i)
779 {
780 	struct Qdisc *qdisc;
781 
782 	for (; i < dev->num_tx_queues; i++) {
783 		qdisc = rtnl_dereference(netdev_get_tx_queue(dev, i)->qdisc);
784 		if (qdisc) {
785 			spin_lock_bh(qdisc_lock(qdisc));
786 			qdisc_reset(qdisc);
787 			spin_unlock_bh(qdisc_lock(qdisc));
788 		}
789 	}
790 }
791 
792 /* Are all TX queues of the device empty?  */
793 static inline bool qdisc_all_tx_empty(const struct net_device *dev)
794 {
795 	unsigned int i;
796 
797 	rcu_read_lock();
798 	for (i = 0; i < dev->num_tx_queues; i++) {
799 		struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
800 		const struct Qdisc *q = rcu_dereference(txq->qdisc);
801 
802 		if (!qdisc_is_empty(q)) {
803 			rcu_read_unlock();
804 			return false;
805 		}
806 	}
807 	rcu_read_unlock();
808 	return true;
809 }
810 
811 /* Are any of the TX qdiscs changing?  */
812 static inline bool qdisc_tx_changing(const struct net_device *dev)
813 {
814 	unsigned int i;
815 
816 	for (i = 0; i < dev->num_tx_queues; i++) {
817 		struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
818 
819 		if (rcu_access_pointer(txq->qdisc) !=
820 		    rcu_access_pointer(txq->qdisc_sleeping))
821 			return true;
822 	}
823 	return false;
824 }
825 
826 /* "noqueue" qdisc identified by not having any enqueue, see noqueue_init() */
827 static inline bool qdisc_txq_has_no_queue(const struct netdev_queue *txq)
828 {
829 	struct Qdisc *qdisc = rcu_access_pointer(txq->qdisc);
830 
831 	return qdisc->enqueue == NULL;
832 }
833 
834 /* Is the device using the noop qdisc on all queues?  */
835 static inline bool qdisc_tx_is_noop(const struct net_device *dev)
836 {
837 	unsigned int i;
838 
839 	for (i = 0; i < dev->num_tx_queues; i++) {
840 		struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
841 		if (rcu_access_pointer(txq->qdisc) != &noop_qdisc)
842 			return false;
843 	}
844 	return true;
845 }
846 
847 static inline unsigned int qdisc_pkt_len(const struct sk_buff *skb)
848 {
849 	return qdisc_skb_cb(skb)->pkt_len;
850 }
851 
852 static inline unsigned int qdisc_pkt_segs(const struct sk_buff *skb)
853 {
854 	u32 pkt_segs = qdisc_skb_cb(skb)->pkt_segs;
855 
856 	DEBUG_NET_WARN_ON_ONCE(pkt_segs !=
857 			(skb_is_gso(skb) ? skb_shinfo(skb)->gso_segs : 1));
858 	return pkt_segs;
859 }
860 
861 /* additional qdisc xmit flags (NET_XMIT_MASK in linux/netdevice.h) */
862 enum net_xmit_qdisc_t {
863 	__NET_XMIT_STOLEN = 0x00010000,
864 	__NET_XMIT_BYPASS = 0x00020000,
865 };
866 
867 #ifdef CONFIG_NET_CLS_ACT
868 #define net_xmit_drop_count(e)	((e) & __NET_XMIT_STOLEN ? 0 : 1)
869 #else
870 #define net_xmit_drop_count(e)	(1)
871 #endif
872 
873 static inline void qdisc_calculate_pkt_len(struct sk_buff *skb,
874 					   const struct Qdisc *sch)
875 {
876 #ifdef CONFIG_NET_SCHED
877 	struct qdisc_size_table *stab = rcu_dereference_bh(sch->stab);
878 
879 	if (stab)
880 		__qdisc_calculate_pkt_len(skb, stab);
881 #endif
882 }
883 
884 static inline int qdisc_enqueue(struct sk_buff *skb, struct Qdisc *sch,
885 				struct sk_buff **to_free)
886 {
887 	return sch->enqueue(skb, sch, to_free);
888 }
889 
890 static inline void _bstats_update(struct gnet_stats_basic_sync *bstats,
891 				  __u64 bytes, __u64 packets)
892 {
893 	u64_stats_update_begin(&bstats->syncp);
894 	u64_stats_add(&bstats->bytes, bytes);
895 	u64_stats_add(&bstats->packets, packets);
896 	u64_stats_update_end(&bstats->syncp);
897 }
898 
899 static inline void bstats_update(struct gnet_stats_basic_sync *bstats,
900 				 const struct sk_buff *skb)
901 {
902 	_bstats_update(bstats, qdisc_pkt_len(skb), qdisc_pkt_segs(skb));
903 }
904 
905 static inline void qdisc_bstats_cpu_update(struct Qdisc *sch,
906 					   const struct sk_buff *skb)
907 {
908 	bstats_update(this_cpu_ptr(sch->cpu_bstats), skb);
909 }
910 
911 static inline void qdisc_bstats_update(struct Qdisc *sch,
912 				       const struct sk_buff *skb)
913 {
914 	bstats_update(&sch->bstats, skb);
915 }
916 
917 static inline void qdisc_qstats_backlog_dec(struct Qdisc *sch,
918 					    const struct sk_buff *skb)
919 {
920 	sch->qstats.backlog -= qdisc_pkt_len(skb);
921 }
922 
923 static inline void qdisc_qstats_cpu_backlog_dec(struct Qdisc *sch,
924 						const struct sk_buff *skb)
925 {
926 	this_cpu_sub(sch->cpu_qstats->backlog, qdisc_pkt_len(skb));
927 }
928 
929 static inline void qdisc_qstats_backlog_inc(struct Qdisc *sch,
930 					    const struct sk_buff *skb)
931 {
932 	sch->qstats.backlog += qdisc_pkt_len(skb);
933 }
934 
935 static inline void qdisc_qstats_cpu_backlog_inc(struct Qdisc *sch,
936 						const struct sk_buff *skb)
937 {
938 	this_cpu_add(sch->cpu_qstats->backlog, qdisc_pkt_len(skb));
939 }
940 
941 static inline void qdisc_qstats_cpu_qlen_inc(struct Qdisc *sch)
942 {
943 	this_cpu_inc(sch->cpu_qstats->qlen);
944 }
945 
946 static inline void qdisc_qstats_cpu_qlen_dec(struct Qdisc *sch)
947 {
948 	this_cpu_dec(sch->cpu_qstats->qlen);
949 }
950 
951 static inline void qdisc_qstats_cpu_requeues_inc(struct Qdisc *sch)
952 {
953 	this_cpu_inc(sch->cpu_qstats->requeues);
954 }
955 
956 static inline void __qdisc_qstats_drop(struct Qdisc *sch, int count)
957 {
958 	sch->qstats.drops += count;
959 }
960 
961 static inline void qstats_drop_inc(struct gnet_stats_queue *qstats)
962 {
963 	qstats->drops++;
964 }
965 
966 static inline void qstats_overlimit_inc(struct gnet_stats_queue *qstats)
967 {
968 	qstats->overlimits++;
969 }
970 
971 static inline void qdisc_qstats_drop(struct Qdisc *sch)
972 {
973 	qstats_drop_inc(&sch->qstats);
974 }
975 
976 static inline void qdisc_qstats_cpu_drop(struct Qdisc *sch)
977 {
978 	this_cpu_inc(sch->cpu_qstats->drops);
979 }
980 
981 static inline void qdisc_qstats_overlimit(struct Qdisc *sch)
982 {
983 	sch->qstats.overlimits++;
984 }
985 
986 static inline int qdisc_qstats_copy(struct gnet_dump *d, struct Qdisc *sch)
987 {
988 	__u32 qlen = qdisc_qlen_sum(sch);
989 
990 	return gnet_stats_copy_queue(d, sch->cpu_qstats, &sch->qstats, qlen);
991 }
992 
993 static inline void qdisc_qstats_qlen_backlog(struct Qdisc *sch,  __u32 *qlen,
994 					     __u32 *backlog)
995 {
996 	struct gnet_stats_queue qstats = { 0 };
997 
998 	gnet_stats_add_queue(&qstats, sch->cpu_qstats, &sch->qstats);
999 	*qlen = qstats.qlen + qdisc_qlen(sch);
1000 	*backlog = qstats.backlog;
1001 }
1002 
1003 static inline void qdisc_purge_queue(struct Qdisc *sch)
1004 {
1005 	__u32 qlen, backlog;
1006 
1007 	qdisc_qstats_qlen_backlog(sch, &qlen, &backlog);
1008 	qdisc_reset(sch);
1009 	qdisc_tree_reduce_backlog(sch, qlen, backlog);
1010 }
1011 
1012 static inline void __qdisc_enqueue_tail(struct sk_buff *skb,
1013 					struct qdisc_skb_head *qh)
1014 {
1015 	struct sk_buff *last = qh->tail;
1016 
1017 	if (last) {
1018 		skb->next = NULL;
1019 		last->next = skb;
1020 		qh->tail = skb;
1021 	} else {
1022 		qh->tail = skb;
1023 		qh->head = skb;
1024 	}
1025 	qh->qlen++;
1026 }
1027 
1028 static inline int qdisc_enqueue_tail(struct sk_buff *skb, struct Qdisc *sch)
1029 {
1030 	__qdisc_enqueue_tail(skb, &sch->q);
1031 	qdisc_qstats_backlog_inc(sch, skb);
1032 	return NET_XMIT_SUCCESS;
1033 }
1034 
1035 static inline void __qdisc_enqueue_head(struct sk_buff *skb,
1036 					struct qdisc_skb_head *qh)
1037 {
1038 	skb->next = qh->head;
1039 
1040 	if (!qh->head)
1041 		qh->tail = skb;
1042 	qh->head = skb;
1043 	qh->qlen++;
1044 }
1045 
1046 static inline struct sk_buff *__qdisc_dequeue_head(struct qdisc_skb_head *qh)
1047 {
1048 	struct sk_buff *skb = qh->head;
1049 
1050 	if (likely(skb != NULL)) {
1051 		qh->head = skb->next;
1052 		qh->qlen--;
1053 		if (qh->head == NULL)
1054 			qh->tail = NULL;
1055 		skb->next = NULL;
1056 	}
1057 
1058 	return skb;
1059 }
1060 
1061 static inline struct sk_buff *qdisc_dequeue_internal(struct Qdisc *sch, bool direct)
1062 {
1063 	struct sk_buff *skb;
1064 
1065 	skb = __skb_dequeue(&sch->gso_skb);
1066 	if (skb) {
1067 		sch->q.qlen--;
1068 		qdisc_qstats_backlog_dec(sch, skb);
1069 		return skb;
1070 	}
1071 	if (direct) {
1072 		skb = __qdisc_dequeue_head(&sch->q);
1073 		if (skb)
1074 			qdisc_qstats_backlog_dec(sch, skb);
1075 		return skb;
1076 	} else {
1077 		return sch->dequeue(sch);
1078 	}
1079 }
1080 
1081 static inline struct sk_buff *qdisc_dequeue_head(struct Qdisc *sch)
1082 {
1083 	struct sk_buff *skb = __qdisc_dequeue_head(&sch->q);
1084 
1085 	if (likely(skb != NULL)) {
1086 		qdisc_qstats_backlog_dec(sch, skb);
1087 		qdisc_bstats_update(sch, skb);
1088 	}
1089 
1090 	return skb;
1091 }
1092 
1093 struct tc_skb_cb {
1094 	struct qdisc_skb_cb qdisc_cb;
1095 	u32 drop_reason;
1096 
1097 	u16 zone; /* Only valid if qdisc_skb_cb(skb)->post_ct = true */
1098 	u16 mru;
1099 };
1100 
1101 static inline struct tc_skb_cb *tc_skb_cb(const struct sk_buff *skb)
1102 {
1103 	struct tc_skb_cb *cb = (struct tc_skb_cb *)skb->cb;
1104 
1105 	BUILD_BUG_ON(sizeof(*cb) > sizeof_field(struct sk_buff, cb));
1106 	return cb;
1107 }
1108 
1109 static inline enum skb_drop_reason
1110 tcf_get_drop_reason(const struct sk_buff *skb)
1111 {
1112 	return tc_skb_cb(skb)->drop_reason;
1113 }
1114 
1115 static inline void tcf_set_drop_reason(const struct sk_buff *skb,
1116 				       enum skb_drop_reason reason)
1117 {
1118 	tc_skb_cb(skb)->drop_reason = reason;
1119 }
1120 
1121 static inline void tcf_kfree_skb_list(struct sk_buff *skb)
1122 {
1123 	while (unlikely(skb)) {
1124 		struct sk_buff *next = skb->next;
1125 
1126 		prefetch(next);
1127 		kfree_skb_reason(skb, tcf_get_drop_reason(skb));
1128 		skb = next;
1129 	}
1130 }
1131 
1132 static inline void qdisc_dequeue_drop(struct Qdisc *q, struct sk_buff *skb,
1133 				      enum skb_drop_reason reason)
1134 {
1135 	DEBUG_NET_WARN_ON_ONCE(!(q->flags & TCQ_F_DEQUEUE_DROPS));
1136 	DEBUG_NET_WARN_ON_ONCE(q->flags & TCQ_F_NOLOCK);
1137 
1138 	tcf_set_drop_reason(skb, reason);
1139 	skb->next = q->to_free;
1140 	q->to_free = skb;
1141 }
1142 
1143 /* Instead of calling kfree_skb() while root qdisc lock is held,
1144  * queue the skb for future freeing at end of __dev_xmit_skb()
1145  */
1146 static inline void __qdisc_drop(struct sk_buff *skb, struct sk_buff **to_free)
1147 {
1148 	skb->next = *to_free;
1149 	*to_free = skb;
1150 }
1151 
1152 static inline void __qdisc_drop_all(struct sk_buff *skb,
1153 				    struct sk_buff **to_free)
1154 {
1155 	if (skb->prev)
1156 		skb->prev->next = *to_free;
1157 	else
1158 		skb->next = *to_free;
1159 	*to_free = skb;
1160 }
1161 
1162 static inline unsigned int __qdisc_queue_drop_head(struct Qdisc *sch,
1163 						   struct qdisc_skb_head *qh,
1164 						   struct sk_buff **to_free)
1165 {
1166 	struct sk_buff *skb = __qdisc_dequeue_head(qh);
1167 
1168 	if (likely(skb != NULL)) {
1169 		unsigned int len = qdisc_pkt_len(skb);
1170 
1171 		qdisc_qstats_backlog_dec(sch, skb);
1172 		__qdisc_drop(skb, to_free);
1173 		return len;
1174 	}
1175 
1176 	return 0;
1177 }
1178 
1179 static inline struct sk_buff *qdisc_peek_head(struct Qdisc *sch)
1180 {
1181 	const struct qdisc_skb_head *qh = &sch->q;
1182 
1183 	return qh->head;
1184 }
1185 
1186 /* generic pseudo peek method for non-work-conserving qdisc */
1187 static inline struct sk_buff *qdisc_peek_dequeued(struct Qdisc *sch)
1188 {
1189 	struct sk_buff *skb = skb_peek(&sch->gso_skb);
1190 
1191 	/* we can reuse ->gso_skb because peek isn't called for root qdiscs */
1192 	if (!skb) {
1193 		skb = sch->dequeue(sch);
1194 
1195 		if (skb) {
1196 			__skb_queue_head(&sch->gso_skb, skb);
1197 			/* it's still part of the queue */
1198 			qdisc_qstats_backlog_inc(sch, skb);
1199 			sch->q.qlen++;
1200 		}
1201 	}
1202 
1203 	return skb;
1204 }
1205 
1206 static inline void qdisc_update_stats_at_dequeue(struct Qdisc *sch,
1207 						 struct sk_buff *skb)
1208 {
1209 	if (qdisc_is_percpu_stats(sch)) {
1210 		qdisc_qstats_cpu_backlog_dec(sch, skb);
1211 		qdisc_bstats_cpu_update(sch, skb);
1212 		qdisc_qstats_cpu_qlen_dec(sch);
1213 	} else {
1214 		qdisc_qstats_backlog_dec(sch, skb);
1215 		qdisc_bstats_update(sch, skb);
1216 		sch->q.qlen--;
1217 	}
1218 }
1219 
1220 static inline void qdisc_update_stats_at_enqueue(struct Qdisc *sch,
1221 						 unsigned int pkt_len)
1222 {
1223 	if (qdisc_is_percpu_stats(sch)) {
1224 		qdisc_qstats_cpu_qlen_inc(sch);
1225 		this_cpu_add(sch->cpu_qstats->backlog, pkt_len);
1226 	} else {
1227 		sch->qstats.backlog += pkt_len;
1228 		sch->q.qlen++;
1229 	}
1230 }
1231 
1232 /* use instead of qdisc->dequeue() for all qdiscs queried with ->peek() */
1233 static inline struct sk_buff *qdisc_dequeue_peeked(struct Qdisc *sch)
1234 {
1235 	struct sk_buff *skb = skb_peek(&sch->gso_skb);
1236 
1237 	if (skb) {
1238 		skb = __skb_dequeue(&sch->gso_skb);
1239 		if (qdisc_is_percpu_stats(sch)) {
1240 			qdisc_qstats_cpu_backlog_dec(sch, skb);
1241 			qdisc_qstats_cpu_qlen_dec(sch);
1242 		} else {
1243 			qdisc_qstats_backlog_dec(sch, skb);
1244 			sch->q.qlen--;
1245 		}
1246 	} else {
1247 		skb = sch->dequeue(sch);
1248 	}
1249 
1250 	return skb;
1251 }
1252 
1253 static inline void __qdisc_reset_queue(struct qdisc_skb_head *qh)
1254 {
1255 	/*
1256 	 * We do not know the backlog in bytes of this list, it
1257 	 * is up to the caller to correct it
1258 	 */
1259 	ASSERT_RTNL();
1260 	if (qh->qlen) {
1261 		rtnl_kfree_skbs(qh->head, qh->tail);
1262 
1263 		qh->head = NULL;
1264 		qh->tail = NULL;
1265 		qh->qlen = 0;
1266 	}
1267 }
1268 
1269 static inline void qdisc_reset_queue(struct Qdisc *sch)
1270 {
1271 	__qdisc_reset_queue(&sch->q);
1272 }
1273 
1274 static inline struct Qdisc *qdisc_replace(struct Qdisc *sch, struct Qdisc *new,
1275 					  struct Qdisc **pold)
1276 {
1277 	struct Qdisc *old;
1278 
1279 	sch_tree_lock(sch);
1280 	old = *pold;
1281 	*pold = new;
1282 	if (old != NULL)
1283 		qdisc_purge_queue(old);
1284 	sch_tree_unlock(sch);
1285 
1286 	return old;
1287 }
1288 
1289 static inline void rtnl_qdisc_drop(struct sk_buff *skb, struct Qdisc *sch)
1290 {
1291 	rtnl_kfree_skbs(skb, skb);
1292 	qdisc_qstats_drop(sch);
1293 }
1294 
1295 static inline int qdisc_drop_cpu(struct sk_buff *skb, struct Qdisc *sch,
1296 				 struct sk_buff **to_free)
1297 {
1298 	__qdisc_drop(skb, to_free);
1299 	qdisc_qstats_cpu_drop(sch);
1300 
1301 	return NET_XMIT_DROP;
1302 }
1303 
1304 static inline int qdisc_drop(struct sk_buff *skb, struct Qdisc *sch,
1305 			     struct sk_buff **to_free)
1306 {
1307 	__qdisc_drop(skb, to_free);
1308 	qdisc_qstats_drop(sch);
1309 
1310 	return NET_XMIT_DROP;
1311 }
1312 
1313 static inline int qdisc_drop_reason(struct sk_buff *skb, struct Qdisc *sch,
1314 				    struct sk_buff **to_free,
1315 				    enum skb_drop_reason reason)
1316 {
1317 	tcf_set_drop_reason(skb, reason);
1318 	return qdisc_drop(skb, sch, to_free);
1319 }
1320 
1321 static inline int qdisc_drop_all(struct sk_buff *skb, struct Qdisc *sch,
1322 				 struct sk_buff **to_free)
1323 {
1324 	__qdisc_drop_all(skb, to_free);
1325 	qdisc_qstats_drop(sch);
1326 
1327 	return NET_XMIT_DROP;
1328 }
1329 
1330 struct psched_ratecfg {
1331 	u64	rate_bytes_ps; /* bytes per second */
1332 	u32	mult;
1333 	u16	overhead;
1334 	u16	mpu;
1335 	u8	linklayer;
1336 	u8	shift;
1337 };
1338 
1339 static inline u64 psched_l2t_ns(const struct psched_ratecfg *r,
1340 				unsigned int len)
1341 {
1342 	len += r->overhead;
1343 
1344 	if (len < r->mpu)
1345 		len = r->mpu;
1346 
1347 	if (unlikely(r->linklayer == TC_LINKLAYER_ATM))
1348 		return ((u64)(DIV_ROUND_UP(len,48)*53) * r->mult) >> r->shift;
1349 
1350 	return ((u64)len * r->mult) >> r->shift;
1351 }
1352 
1353 void psched_ratecfg_precompute(struct psched_ratecfg *r,
1354 			       const struct tc_ratespec *conf,
1355 			       u64 rate64);
1356 
1357 static inline void psched_ratecfg_getrate(struct tc_ratespec *res,
1358 					  const struct psched_ratecfg *r)
1359 {
1360 	memset(res, 0, sizeof(*res));
1361 
1362 	/* legacy struct tc_ratespec has a 32bit @rate field
1363 	 * Qdisc using 64bit rate should add new attributes
1364 	 * in order to maintain compatibility.
1365 	 */
1366 	res->rate = min_t(u64, r->rate_bytes_ps, ~0U);
1367 
1368 	res->overhead = r->overhead;
1369 	res->mpu = r->mpu;
1370 	res->linklayer = (r->linklayer & TC_LINKLAYER_MASK);
1371 }
1372 
1373 struct psched_pktrate {
1374 	u64	rate_pkts_ps; /* packets per second */
1375 	u32	mult;
1376 	u8	shift;
1377 };
1378 
1379 static inline u64 psched_pkt2t_ns(const struct psched_pktrate *r,
1380 				  unsigned int pkt_num)
1381 {
1382 	return ((u64)pkt_num * r->mult) >> r->shift;
1383 }
1384 
1385 void psched_ppscfg_precompute(struct psched_pktrate *r, u64 pktrate64);
1386 
1387 /* Mini Qdisc serves for specific needs of ingress/clsact Qdisc.
1388  * The fast path only needs to access filter list and to update stats
1389  */
1390 struct mini_Qdisc {
1391 	struct tcf_proto *filter_list;
1392 	struct tcf_block *block;
1393 	struct gnet_stats_basic_sync __percpu *cpu_bstats;
1394 	struct gnet_stats_queue	__percpu *cpu_qstats;
1395 	unsigned long rcu_state;
1396 };
1397 
1398 static inline void mini_qdisc_bstats_cpu_update(struct mini_Qdisc *miniq,
1399 						const struct sk_buff *skb)
1400 {
1401 	bstats_update(this_cpu_ptr(miniq->cpu_bstats), skb);
1402 }
1403 
1404 static inline void mini_qdisc_qstats_cpu_drop(struct mini_Qdisc *miniq)
1405 {
1406 	this_cpu_inc(miniq->cpu_qstats->drops);
1407 }
1408 
1409 struct mini_Qdisc_pair {
1410 	struct mini_Qdisc miniq1;
1411 	struct mini_Qdisc miniq2;
1412 	struct mini_Qdisc __rcu **p_miniq;
1413 };
1414 
1415 void mini_qdisc_pair_swap(struct mini_Qdisc_pair *miniqp,
1416 			  struct tcf_proto *tp_head);
1417 void mini_qdisc_pair_init(struct mini_Qdisc_pair *miniqp, struct Qdisc *qdisc,
1418 			  struct mini_Qdisc __rcu **p_miniq);
1419 void mini_qdisc_pair_block_init(struct mini_Qdisc_pair *miniqp,
1420 				struct tcf_block *block);
1421 
1422 void mq_change_real_num_tx(struct Qdisc *sch, unsigned int new_real_tx);
1423 
1424 int sch_frag_xmit_hook(struct sk_buff *skb, int (*xmit)(struct sk_buff *skb));
1425 
1426 /* Make sure qdisc is no longer in SCHED state. */
1427 static inline void qdisc_synchronize(const struct Qdisc *q)
1428 {
1429 	while (test_bit(__QDISC_STATE_SCHED, &q->state))
1430 		msleep(1);
1431 }
1432 
1433 #endif
1434