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