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