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