• 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 
306 	int			(*dump)(struct Qdisc *, struct sk_buff *);
307 	int			(*dump_stats)(struct Qdisc *, struct gnet_dump *);
308 
309 	void			(*ingress_block_set)(struct Qdisc *sch,
310 						     u32 block_index);
311 	void			(*egress_block_set)(struct Qdisc *sch,
312 						    u32 block_index);
313 	u32			(*ingress_block_get)(struct Qdisc *sch);
314 	u32			(*egress_block_get)(struct Qdisc *sch);
315 
316 	struct module		*owner;
317 };
318 
319 
320 struct tcf_result {
321 	union {
322 		struct {
323 			unsigned long	class;
324 			u32		classid;
325 		};
326 		const struct tcf_proto *goto_tp;
327 
328 		/* used in the skb_tc_reinsert function */
329 		struct {
330 			bool		ingress;
331 			struct gnet_stats_queue *qstats;
332 		};
333 	};
334 };
335 
336 struct tcf_chain;
337 
338 struct tcf_proto_ops {
339 	struct list_head	head;
340 	char			kind[IFNAMSIZ];
341 
342 	int			(*classify)(struct sk_buff *,
343 					    const struct tcf_proto *,
344 					    struct tcf_result *);
345 	int			(*init)(struct tcf_proto*);
346 	void			(*destroy)(struct tcf_proto *tp, bool rtnl_held,
347 					   struct netlink_ext_ack *extack);
348 
349 	void*			(*get)(struct tcf_proto*, u32 handle);
350 	void			(*put)(struct tcf_proto *tp, void *f);
351 	int			(*change)(struct net *net, struct sk_buff *,
352 					struct tcf_proto*, unsigned long,
353 					u32 handle, struct nlattr **,
354 					void **, bool, bool,
355 					struct netlink_ext_ack *);
356 	int			(*delete)(struct tcf_proto *tp, void *arg,
357 					  bool *last, bool rtnl_held,
358 					  struct netlink_ext_ack *);
359 	bool			(*delete_empty)(struct tcf_proto *tp);
360 	void			(*walk)(struct tcf_proto *tp,
361 					struct tcf_walker *arg, bool rtnl_held);
362 	int			(*reoffload)(struct tcf_proto *tp, bool add,
363 					     flow_setup_cb_t *cb, void *cb_priv,
364 					     struct netlink_ext_ack *extack);
365 	void			(*hw_add)(struct tcf_proto *tp,
366 					  void *type_data);
367 	void			(*hw_del)(struct tcf_proto *tp,
368 					  void *type_data);
369 	void			(*bind_class)(void *, u32, unsigned long,
370 					      void *, unsigned long);
371 	void *			(*tmplt_create)(struct net *net,
372 						struct tcf_chain *chain,
373 						struct nlattr **tca,
374 						struct netlink_ext_ack *extack);
375 	void			(*tmplt_destroy)(void *tmplt_priv);
376 
377 	/* rtnetlink specific */
378 	int			(*dump)(struct net*, struct tcf_proto*, void *,
379 					struct sk_buff *skb, struct tcmsg*,
380 					bool);
381 	int			(*terse_dump)(struct net *net,
382 					      struct tcf_proto *tp, void *fh,
383 					      struct sk_buff *skb,
384 					      struct tcmsg *t, bool rtnl_held);
385 	int			(*tmplt_dump)(struct sk_buff *skb,
386 					      struct net *net,
387 					      void *tmplt_priv);
388 
389 	struct module		*owner;
390 	int			flags;
391 };
392 
393 /* Classifiers setting TCF_PROTO_OPS_DOIT_UNLOCKED in tcf_proto_ops->flags
394  * are expected to implement tcf_proto_ops->delete_empty(), otherwise race
395  * conditions can occur when filters are inserted/deleted simultaneously.
396  */
397 enum tcf_proto_ops_flags {
398 	TCF_PROTO_OPS_DOIT_UNLOCKED = 1,
399 };
400 
401 struct tcf_proto {
402 	/* Fast access part */
403 	struct tcf_proto __rcu	*next;
404 	void __rcu		*root;
405 
406 	/* called under RCU BH lock*/
407 	int			(*classify)(struct sk_buff *,
408 					    const struct tcf_proto *,
409 					    struct tcf_result *);
410 	__be16			protocol;
411 
412 	/* All the rest */
413 	u32			prio;
414 	void			*data;
415 	const struct tcf_proto_ops	*ops;
416 	struct tcf_chain	*chain;
417 	/* Lock protects tcf_proto shared state and can be used by unlocked
418 	 * classifiers to protect their private data.
419 	 */
420 	spinlock_t		lock;
421 	bool			deleting;
422 	refcount_t		refcnt;
423 	struct rcu_head		rcu;
424 	struct hlist_node	destroy_ht_node;
425 };
426 
427 struct qdisc_skb_cb {
428 	struct {
429 		unsigned int		pkt_len;
430 		u16			slave_dev_queue_mapping;
431 		u16			tc_classid;
432 	};
433 #define QDISC_CB_PRIV_LEN 20
434 	unsigned char		data[QDISC_CB_PRIV_LEN];
435 	u16			mru;
436 };
437 
438 typedef void tcf_chain_head_change_t(struct tcf_proto *tp_head, void *priv);
439 
440 struct tcf_chain {
441 	/* Protects filter_chain. */
442 	struct mutex filter_chain_lock;
443 	struct tcf_proto __rcu *filter_chain;
444 	struct list_head list;
445 	struct tcf_block *block;
446 	u32 index; /* chain index */
447 	unsigned int refcnt;
448 	unsigned int action_refcnt;
449 	bool explicitly_created;
450 	bool flushing;
451 	const struct tcf_proto_ops *tmplt_ops;
452 	void *tmplt_priv;
453 	struct rcu_head rcu;
454 };
455 
456 struct tcf_block {
457 	/* Lock protects tcf_block and lifetime-management data of chains
458 	 * attached to the block (refcnt, action_refcnt, explicitly_created).
459 	 */
460 	struct mutex lock;
461 	struct list_head chain_list;
462 	u32 index; /* block index for shared blocks */
463 	u32 classid; /* which class this block belongs to */
464 	refcount_t refcnt;
465 	struct net *net;
466 	struct Qdisc *q;
467 	struct rw_semaphore cb_lock; /* protects cb_list and offload counters */
468 	struct flow_block flow_block;
469 	struct list_head owner_list;
470 	bool keep_dst;
471 	atomic_t offloadcnt; /* Number of oddloaded filters */
472 	unsigned int nooffloaddevcnt; /* Number of devs unable to do offload */
473 	unsigned int lockeddevcnt; /* Number of devs that require rtnl lock. */
474 	struct {
475 		struct tcf_chain *chain;
476 		struct list_head filter_chain_list;
477 	} chain0;
478 	struct rcu_head rcu;
479 	DECLARE_HASHTABLE(proto_destroy_ht, 7);
480 	struct mutex proto_destroy_lock; /* Lock for proto_destroy hashtable. */
481 };
482 
483 #ifdef CONFIG_PROVE_LOCKING
lockdep_tcf_chain_is_locked(struct tcf_chain * chain)484 static inline bool lockdep_tcf_chain_is_locked(struct tcf_chain *chain)
485 {
486 	return lockdep_is_held(&chain->filter_chain_lock);
487 }
488 
lockdep_tcf_proto_is_locked(struct tcf_proto * tp)489 static inline bool lockdep_tcf_proto_is_locked(struct tcf_proto *tp)
490 {
491 	return lockdep_is_held(&tp->lock);
492 }
493 #else
lockdep_tcf_chain_is_locked(struct tcf_block * chain)494 static inline bool lockdep_tcf_chain_is_locked(struct tcf_block *chain)
495 {
496 	return true;
497 }
498 
lockdep_tcf_proto_is_locked(struct tcf_proto * tp)499 static inline bool lockdep_tcf_proto_is_locked(struct tcf_proto *tp)
500 {
501 	return true;
502 }
503 #endif /* #ifdef CONFIG_PROVE_LOCKING */
504 
505 #define tcf_chain_dereference(p, chain)					\
506 	rcu_dereference_protected(p, lockdep_tcf_chain_is_locked(chain))
507 
508 #define tcf_proto_dereference(p, tp)					\
509 	rcu_dereference_protected(p, lockdep_tcf_proto_is_locked(tp))
510 
qdisc_cb_private_validate(const struct sk_buff * skb,int sz)511 static inline void qdisc_cb_private_validate(const struct sk_buff *skb, int sz)
512 {
513 	struct qdisc_skb_cb *qcb;
514 
515 	BUILD_BUG_ON(sizeof(skb->cb) < sizeof(*qcb));
516 	BUILD_BUG_ON(sizeof(qcb->data) < sz);
517 }
518 
qdisc_qlen_cpu(const struct Qdisc * q)519 static inline int qdisc_qlen_cpu(const struct Qdisc *q)
520 {
521 	return this_cpu_ptr(q->cpu_qstats)->qlen;
522 }
523 
qdisc_qlen(const struct Qdisc * q)524 static inline int qdisc_qlen(const struct Qdisc *q)
525 {
526 	return q->q.qlen;
527 }
528 
qdisc_qlen_sum(const struct Qdisc * q)529 static inline int qdisc_qlen_sum(const struct Qdisc *q)
530 {
531 	__u32 qlen = q->qstats.qlen;
532 	int i;
533 
534 	if (qdisc_is_percpu_stats(q)) {
535 		for_each_possible_cpu(i)
536 			qlen += per_cpu_ptr(q->cpu_qstats, i)->qlen;
537 	} else {
538 		qlen += q->q.qlen;
539 	}
540 
541 	return qlen;
542 }
543 
qdisc_skb_cb(const struct sk_buff * skb)544 static inline struct qdisc_skb_cb *qdisc_skb_cb(const struct sk_buff *skb)
545 {
546 	return (struct qdisc_skb_cb *)skb->cb;
547 }
548 
qdisc_lock(struct Qdisc * qdisc)549 static inline spinlock_t *qdisc_lock(struct Qdisc *qdisc)
550 {
551 	return &qdisc->q.lock;
552 }
553 
qdisc_root(const struct Qdisc * qdisc)554 static inline struct Qdisc *qdisc_root(const struct Qdisc *qdisc)
555 {
556 	struct Qdisc *q = rcu_dereference_rtnl(qdisc->dev_queue->qdisc);
557 
558 	return q;
559 }
560 
qdisc_root_bh(const struct Qdisc * qdisc)561 static inline struct Qdisc *qdisc_root_bh(const struct Qdisc *qdisc)
562 {
563 	return rcu_dereference_bh(qdisc->dev_queue->qdisc);
564 }
565 
qdisc_root_sleeping(const struct Qdisc * qdisc)566 static inline struct Qdisc *qdisc_root_sleeping(const struct Qdisc *qdisc)
567 {
568 	return qdisc->dev_queue->qdisc_sleeping;
569 }
570 
571 /* The qdisc root lock is a mechanism by which to top level
572  * of a qdisc tree can be locked from any qdisc node in the
573  * forest.  This allows changing the configuration of some
574  * aspect of the qdisc tree while blocking out asynchronous
575  * qdisc access in the packet processing paths.
576  *
577  * It is only legal to do this when the root will not change
578  * on us.  Otherwise we'll potentially lock the wrong qdisc
579  * root.  This is enforced by holding the RTNL semaphore, which
580  * all users of this lock accessor must do.
581  */
qdisc_root_lock(const struct Qdisc * qdisc)582 static inline spinlock_t *qdisc_root_lock(const struct Qdisc *qdisc)
583 {
584 	struct Qdisc *root = qdisc_root(qdisc);
585 
586 	ASSERT_RTNL();
587 	return qdisc_lock(root);
588 }
589 
qdisc_root_sleeping_lock(const struct Qdisc * qdisc)590 static inline spinlock_t *qdisc_root_sleeping_lock(const struct Qdisc *qdisc)
591 {
592 	struct Qdisc *root = qdisc_root_sleeping(qdisc);
593 
594 	ASSERT_RTNL();
595 	return qdisc_lock(root);
596 }
597 
qdisc_root_sleeping_running(const struct Qdisc * qdisc)598 static inline seqcount_t *qdisc_root_sleeping_running(const struct Qdisc *qdisc)
599 {
600 	struct Qdisc *root = qdisc_root_sleeping(qdisc);
601 
602 	ASSERT_RTNL();
603 	return &root->running;
604 }
605 
qdisc_dev(const struct Qdisc * qdisc)606 static inline struct net_device *qdisc_dev(const struct Qdisc *qdisc)
607 {
608 	return qdisc->dev_queue->dev;
609 }
610 
sch_tree_lock(const struct Qdisc * q)611 static inline void sch_tree_lock(const struct Qdisc *q)
612 {
613 	spin_lock_bh(qdisc_root_sleeping_lock(q));
614 }
615 
sch_tree_unlock(const struct Qdisc * q)616 static inline void sch_tree_unlock(const struct Qdisc *q)
617 {
618 	spin_unlock_bh(qdisc_root_sleeping_lock(q));
619 }
620 
621 extern struct Qdisc noop_qdisc;
622 extern struct Qdisc_ops noop_qdisc_ops;
623 extern struct Qdisc_ops pfifo_fast_ops;
624 extern struct Qdisc_ops mq_qdisc_ops;
625 extern struct Qdisc_ops noqueue_qdisc_ops;
626 extern const struct Qdisc_ops *default_qdisc_ops;
627 static inline const struct Qdisc_ops *
get_default_qdisc_ops(const struct net_device * dev,int ntx)628 get_default_qdisc_ops(const struct net_device *dev, int ntx)
629 {
630 	return ntx < dev->real_num_tx_queues ?
631 			default_qdisc_ops : &pfifo_fast_ops;
632 }
633 
634 struct Qdisc_class_common {
635 	u32			classid;
636 	struct hlist_node	hnode;
637 };
638 
639 struct Qdisc_class_hash {
640 	struct hlist_head	*hash;
641 	unsigned int		hashsize;
642 	unsigned int		hashmask;
643 	unsigned int		hashelems;
644 };
645 
qdisc_class_hash(u32 id,u32 mask)646 static inline unsigned int qdisc_class_hash(u32 id, u32 mask)
647 {
648 	id ^= id >> 8;
649 	id ^= id >> 4;
650 	return id & mask;
651 }
652 
653 static inline struct Qdisc_class_common *
qdisc_class_find(const struct Qdisc_class_hash * hash,u32 id)654 qdisc_class_find(const struct Qdisc_class_hash *hash, u32 id)
655 {
656 	struct Qdisc_class_common *cl;
657 	unsigned int h;
658 
659 	if (!id)
660 		return NULL;
661 
662 	h = qdisc_class_hash(id, hash->hashmask);
663 	hlist_for_each_entry(cl, &hash->hash[h], hnode) {
664 		if (cl->classid == id)
665 			return cl;
666 	}
667 	return NULL;
668 }
669 
tc_classid_to_hwtc(struct net_device * dev,u32 classid)670 static inline int tc_classid_to_hwtc(struct net_device *dev, u32 classid)
671 {
672 	u32 hwtc = TC_H_MIN(classid) - TC_H_MIN_PRIORITY;
673 
674 	return (hwtc < netdev_get_num_tc(dev)) ? hwtc : -EINVAL;
675 }
676 
677 int qdisc_class_hash_init(struct Qdisc_class_hash *);
678 void qdisc_class_hash_insert(struct Qdisc_class_hash *,
679 			     struct Qdisc_class_common *);
680 void qdisc_class_hash_remove(struct Qdisc_class_hash *,
681 			     struct Qdisc_class_common *);
682 void qdisc_class_hash_grow(struct Qdisc *, struct Qdisc_class_hash *);
683 void qdisc_class_hash_destroy(struct Qdisc_class_hash *);
684 
685 int dev_qdisc_change_tx_queue_len(struct net_device *dev);
686 void dev_init_scheduler(struct net_device *dev);
687 void dev_shutdown(struct net_device *dev);
688 void dev_activate(struct net_device *dev);
689 void dev_deactivate(struct net_device *dev);
690 void dev_deactivate_many(struct list_head *head);
691 struct Qdisc *dev_graft_qdisc(struct netdev_queue *dev_queue,
692 			      struct Qdisc *qdisc);
693 void qdisc_reset(struct Qdisc *qdisc);
694 void qdisc_put(struct Qdisc *qdisc);
695 void qdisc_put_unlocked(struct Qdisc *qdisc);
696 void qdisc_tree_reduce_backlog(struct Qdisc *qdisc, int n, int len);
697 #ifdef CONFIG_NET_SCHED
698 int qdisc_offload_dump_helper(struct Qdisc *q, enum tc_setup_type type,
699 			      void *type_data);
700 void qdisc_offload_graft_helper(struct net_device *dev, struct Qdisc *sch,
701 				struct Qdisc *new, struct Qdisc *old,
702 				enum tc_setup_type type, void *type_data,
703 				struct netlink_ext_ack *extack);
704 #else
705 static inline int
qdisc_offload_dump_helper(struct Qdisc * q,enum tc_setup_type type,void * type_data)706 qdisc_offload_dump_helper(struct Qdisc *q, enum tc_setup_type type,
707 			  void *type_data)
708 {
709 	q->flags &= ~TCQ_F_OFFLOADED;
710 	return 0;
711 }
712 
713 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)714 qdisc_offload_graft_helper(struct net_device *dev, struct Qdisc *sch,
715 			   struct Qdisc *new, struct Qdisc *old,
716 			   enum tc_setup_type type, void *type_data,
717 			   struct netlink_ext_ack *extack)
718 {
719 }
720 #endif
721 struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue,
722 			  const struct Qdisc_ops *ops,
723 			  struct netlink_ext_ack *extack);
724 void qdisc_free(struct Qdisc *qdisc);
725 struct Qdisc *qdisc_create_dflt(struct netdev_queue *dev_queue,
726 				const struct Qdisc_ops *ops, u32 parentid,
727 				struct netlink_ext_ack *extack);
728 void __qdisc_calculate_pkt_len(struct sk_buff *skb,
729 			       const struct qdisc_size_table *stab);
730 int skb_do_redirect(struct sk_buff *);
731 
skb_at_tc_ingress(const struct sk_buff * skb)732 static inline bool skb_at_tc_ingress(const struct sk_buff *skb)
733 {
734 #ifdef CONFIG_NET_CLS_ACT
735 	return skb->tc_at_ingress;
736 #else
737 	return false;
738 #endif
739 }
740 
skb_skip_tc_classify(struct sk_buff * skb)741 static inline bool skb_skip_tc_classify(struct sk_buff *skb)
742 {
743 #ifdef CONFIG_NET_CLS_ACT
744 	if (skb->tc_skip_classify) {
745 		skb->tc_skip_classify = 0;
746 		return true;
747 	}
748 #endif
749 	return false;
750 }
751 
752 /* Reset all TX qdiscs greater than index of a device.  */
qdisc_reset_all_tx_gt(struct net_device * dev,unsigned int i)753 static inline void qdisc_reset_all_tx_gt(struct net_device *dev, unsigned int i)
754 {
755 	struct Qdisc *qdisc;
756 
757 	for (; i < dev->num_tx_queues; i++) {
758 		qdisc = rtnl_dereference(netdev_get_tx_queue(dev, i)->qdisc);
759 		if (qdisc) {
760 			spin_lock_bh(qdisc_lock(qdisc));
761 			qdisc_reset(qdisc);
762 			spin_unlock_bh(qdisc_lock(qdisc));
763 		}
764 	}
765 }
766 
767 /* Are all TX queues of the device empty?  */
qdisc_all_tx_empty(const struct net_device * dev)768 static inline bool qdisc_all_tx_empty(const struct net_device *dev)
769 {
770 	unsigned int i;
771 
772 	rcu_read_lock();
773 	for (i = 0; i < dev->num_tx_queues; i++) {
774 		struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
775 		const struct Qdisc *q = rcu_dereference(txq->qdisc);
776 
777 		if (!qdisc_is_empty(q)) {
778 			rcu_read_unlock();
779 			return false;
780 		}
781 	}
782 	rcu_read_unlock();
783 	return true;
784 }
785 
786 /* Are any of the TX qdiscs changing?  */
qdisc_tx_changing(const struct net_device * dev)787 static inline bool qdisc_tx_changing(const struct net_device *dev)
788 {
789 	unsigned int i;
790 
791 	for (i = 0; i < dev->num_tx_queues; i++) {
792 		struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
793 		if (rcu_access_pointer(txq->qdisc) != txq->qdisc_sleeping)
794 			return true;
795 	}
796 	return false;
797 }
798 
799 /* Is the device using the noop qdisc on all queues?  */
qdisc_tx_is_noop(const struct net_device * dev)800 static inline bool qdisc_tx_is_noop(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 		if (rcu_access_pointer(txq->qdisc) != &noop_qdisc)
807 			return false;
808 	}
809 	return true;
810 }
811 
qdisc_pkt_len(const struct sk_buff * skb)812 static inline unsigned int qdisc_pkt_len(const struct sk_buff *skb)
813 {
814 	return qdisc_skb_cb(skb)->pkt_len;
815 }
816 
817 /* additional qdisc xmit flags (NET_XMIT_MASK in linux/netdevice.h) */
818 enum net_xmit_qdisc_t {
819 	__NET_XMIT_STOLEN = 0x00010000,
820 	__NET_XMIT_BYPASS = 0x00020000,
821 };
822 
823 #ifdef CONFIG_NET_CLS_ACT
824 #define net_xmit_drop_count(e)	((e) & __NET_XMIT_STOLEN ? 0 : 1)
825 #else
826 #define net_xmit_drop_count(e)	(1)
827 #endif
828 
qdisc_calculate_pkt_len(struct sk_buff * skb,const struct Qdisc * sch)829 static inline void qdisc_calculate_pkt_len(struct sk_buff *skb,
830 					   const struct Qdisc *sch)
831 {
832 #ifdef CONFIG_NET_SCHED
833 	struct qdisc_size_table *stab = rcu_dereference_bh(sch->stab);
834 
835 	if (stab)
836 		__qdisc_calculate_pkt_len(skb, stab);
837 #endif
838 }
839 
qdisc_enqueue(struct sk_buff * skb,struct Qdisc * sch,struct sk_buff ** to_free)840 static inline int qdisc_enqueue(struct sk_buff *skb, struct Qdisc *sch,
841 				struct sk_buff **to_free)
842 {
843 	qdisc_calculate_pkt_len(skb, sch);
844 	return sch->enqueue(skb, sch, to_free);
845 }
846 
_bstats_update(struct gnet_stats_basic_packed * bstats,__u64 bytes,__u32 packets)847 static inline void _bstats_update(struct gnet_stats_basic_packed *bstats,
848 				  __u64 bytes, __u32 packets)
849 {
850 	bstats->bytes += bytes;
851 	bstats->packets += packets;
852 }
853 
bstats_update(struct gnet_stats_basic_packed * bstats,const struct sk_buff * skb)854 static inline void bstats_update(struct gnet_stats_basic_packed *bstats,
855 				 const struct sk_buff *skb)
856 {
857 	_bstats_update(bstats,
858 		       qdisc_pkt_len(skb),
859 		       skb_is_gso(skb) ? skb_shinfo(skb)->gso_segs : 1);
860 }
861 
_bstats_cpu_update(struct gnet_stats_basic_cpu * bstats,__u64 bytes,__u32 packets)862 static inline void _bstats_cpu_update(struct gnet_stats_basic_cpu *bstats,
863 				      __u64 bytes, __u32 packets)
864 {
865 	u64_stats_update_begin(&bstats->syncp);
866 	_bstats_update(&bstats->bstats, bytes, packets);
867 	u64_stats_update_end(&bstats->syncp);
868 }
869 
bstats_cpu_update(struct gnet_stats_basic_cpu * bstats,const struct sk_buff * skb)870 static inline void bstats_cpu_update(struct gnet_stats_basic_cpu *bstats,
871 				     const struct sk_buff *skb)
872 {
873 	u64_stats_update_begin(&bstats->syncp);
874 	bstats_update(&bstats->bstats, skb);
875 	u64_stats_update_end(&bstats->syncp);
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_cpu_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 	__u32 len = qdisc_qlen_sum(sch);
971 
972 	__gnet_stats_copy_queue(&qstats, sch->cpu_qstats, &sch->qstats, len);
973 	*qlen = qstats.qlen;
974 	*backlog = qstats.backlog;
975 }
976 
qdisc_tree_flush_backlog(struct Qdisc * sch)977 static inline void qdisc_tree_flush_backlog(struct Qdisc *sch)
978 {
979 	__u32 qlen, backlog;
980 
981 	qdisc_qstats_qlen_backlog(sch, &qlen, &backlog);
982 	qdisc_tree_reduce_backlog(sch, qlen, backlog);
983 }
984 
qdisc_purge_queue(struct Qdisc * sch)985 static inline void qdisc_purge_queue(struct Qdisc *sch)
986 {
987 	__u32 qlen, backlog;
988 
989 	qdisc_qstats_qlen_backlog(sch, &qlen, &backlog);
990 	qdisc_reset(sch);
991 	qdisc_tree_reduce_backlog(sch, qlen, backlog);
992 }
993 
qdisc_skb_head_init(struct qdisc_skb_head * qh)994 static inline void qdisc_skb_head_init(struct qdisc_skb_head *qh)
995 {
996 	qh->head = NULL;
997 	qh->tail = NULL;
998 	qh->qlen = 0;
999 }
1000 
__qdisc_enqueue_tail(struct sk_buff * skb,struct qdisc_skb_head * qh)1001 static inline void __qdisc_enqueue_tail(struct sk_buff *skb,
1002 					struct qdisc_skb_head *qh)
1003 {
1004 	struct sk_buff *last = qh->tail;
1005 
1006 	if (last) {
1007 		skb->next = NULL;
1008 		last->next = skb;
1009 		qh->tail = skb;
1010 	} else {
1011 		qh->tail = skb;
1012 		qh->head = skb;
1013 	}
1014 	qh->qlen++;
1015 }
1016 
qdisc_enqueue_tail(struct sk_buff * skb,struct Qdisc * sch)1017 static inline int qdisc_enqueue_tail(struct sk_buff *skb, struct Qdisc *sch)
1018 {
1019 	__qdisc_enqueue_tail(skb, &sch->q);
1020 	qdisc_qstats_backlog_inc(sch, skb);
1021 	return NET_XMIT_SUCCESS;
1022 }
1023 
__qdisc_enqueue_head(struct sk_buff * skb,struct qdisc_skb_head * qh)1024 static inline void __qdisc_enqueue_head(struct sk_buff *skb,
1025 					struct qdisc_skb_head *qh)
1026 {
1027 	skb->next = qh->head;
1028 
1029 	if (!qh->head)
1030 		qh->tail = skb;
1031 	qh->head = skb;
1032 	qh->qlen++;
1033 }
1034 
__qdisc_dequeue_head(struct qdisc_skb_head * qh)1035 static inline struct sk_buff *__qdisc_dequeue_head(struct qdisc_skb_head *qh)
1036 {
1037 	struct sk_buff *skb = qh->head;
1038 
1039 	if (likely(skb != NULL)) {
1040 		qh->head = skb->next;
1041 		qh->qlen--;
1042 		if (qh->head == NULL)
1043 			qh->tail = NULL;
1044 		skb->next = NULL;
1045 	}
1046 
1047 	return skb;
1048 }
1049 
qdisc_dequeue_head(struct Qdisc * sch)1050 static inline struct sk_buff *qdisc_dequeue_head(struct Qdisc *sch)
1051 {
1052 	struct sk_buff *skb = __qdisc_dequeue_head(&sch->q);
1053 
1054 	if (likely(skb != NULL)) {
1055 		qdisc_qstats_backlog_dec(sch, skb);
1056 		qdisc_bstats_update(sch, skb);
1057 	}
1058 
1059 	return skb;
1060 }
1061 
1062 /* Instead of calling kfree_skb() while root qdisc lock is held,
1063  * queue the skb for future freeing at end of __dev_xmit_skb()
1064  */
__qdisc_drop(struct sk_buff * skb,struct sk_buff ** to_free)1065 static inline void __qdisc_drop(struct sk_buff *skb, struct sk_buff **to_free)
1066 {
1067 	skb->next = *to_free;
1068 	*to_free = skb;
1069 }
1070 
__qdisc_drop_all(struct sk_buff * skb,struct sk_buff ** to_free)1071 static inline void __qdisc_drop_all(struct sk_buff *skb,
1072 				    struct sk_buff **to_free)
1073 {
1074 	if (skb->prev)
1075 		skb->prev->next = *to_free;
1076 	else
1077 		skb->next = *to_free;
1078 	*to_free = skb;
1079 }
1080 
__qdisc_queue_drop_head(struct Qdisc * sch,struct qdisc_skb_head * qh,struct sk_buff ** to_free)1081 static inline unsigned int __qdisc_queue_drop_head(struct Qdisc *sch,
1082 						   struct qdisc_skb_head *qh,
1083 						   struct sk_buff **to_free)
1084 {
1085 	struct sk_buff *skb = __qdisc_dequeue_head(qh);
1086 
1087 	if (likely(skb != NULL)) {
1088 		unsigned int len = qdisc_pkt_len(skb);
1089 
1090 		qdisc_qstats_backlog_dec(sch, skb);
1091 		__qdisc_drop(skb, to_free);
1092 		return len;
1093 	}
1094 
1095 	return 0;
1096 }
1097 
qdisc_peek_head(struct Qdisc * sch)1098 static inline struct sk_buff *qdisc_peek_head(struct Qdisc *sch)
1099 {
1100 	const struct qdisc_skb_head *qh = &sch->q;
1101 
1102 	return qh->head;
1103 }
1104 
1105 /* generic pseudo peek method for non-work-conserving qdisc */
qdisc_peek_dequeued(struct Qdisc * sch)1106 static inline struct sk_buff *qdisc_peek_dequeued(struct Qdisc *sch)
1107 {
1108 	struct sk_buff *skb = skb_peek(&sch->gso_skb);
1109 
1110 	/* we can reuse ->gso_skb because peek isn't called for root qdiscs */
1111 	if (!skb) {
1112 		skb = sch->dequeue(sch);
1113 
1114 		if (skb) {
1115 			__skb_queue_head(&sch->gso_skb, skb);
1116 			/* it's still part of the queue */
1117 			qdisc_qstats_backlog_inc(sch, skb);
1118 			sch->q.qlen++;
1119 		}
1120 	}
1121 
1122 	return skb;
1123 }
1124 
qdisc_update_stats_at_dequeue(struct Qdisc * sch,struct sk_buff * skb)1125 static inline void qdisc_update_stats_at_dequeue(struct Qdisc *sch,
1126 						 struct sk_buff *skb)
1127 {
1128 	if (qdisc_is_percpu_stats(sch)) {
1129 		qdisc_qstats_cpu_backlog_dec(sch, skb);
1130 		qdisc_bstats_cpu_update(sch, skb);
1131 		qdisc_qstats_cpu_qlen_dec(sch);
1132 	} else {
1133 		qdisc_qstats_backlog_dec(sch, skb);
1134 		qdisc_bstats_update(sch, skb);
1135 		sch->q.qlen--;
1136 	}
1137 }
1138 
qdisc_update_stats_at_enqueue(struct Qdisc * sch,unsigned int pkt_len)1139 static inline void qdisc_update_stats_at_enqueue(struct Qdisc *sch,
1140 						 unsigned int pkt_len)
1141 {
1142 	if (qdisc_is_percpu_stats(sch)) {
1143 		qdisc_qstats_cpu_qlen_inc(sch);
1144 		this_cpu_add(sch->cpu_qstats->backlog, pkt_len);
1145 	} else {
1146 		sch->qstats.backlog += pkt_len;
1147 		sch->q.qlen++;
1148 	}
1149 }
1150 
1151 /* use instead of qdisc->dequeue() for all qdiscs queried with ->peek() */
qdisc_dequeue_peeked(struct Qdisc * sch)1152 static inline struct sk_buff *qdisc_dequeue_peeked(struct Qdisc *sch)
1153 {
1154 	struct sk_buff *skb = skb_peek(&sch->gso_skb);
1155 
1156 	if (skb) {
1157 		skb = __skb_dequeue(&sch->gso_skb);
1158 		if (qdisc_is_percpu_stats(sch)) {
1159 			qdisc_qstats_cpu_backlog_dec(sch, skb);
1160 			qdisc_qstats_cpu_qlen_dec(sch);
1161 		} else {
1162 			qdisc_qstats_backlog_dec(sch, skb);
1163 			sch->q.qlen--;
1164 		}
1165 	} else {
1166 		skb = sch->dequeue(sch);
1167 	}
1168 
1169 	return skb;
1170 }
1171 
__qdisc_reset_queue(struct qdisc_skb_head * qh)1172 static inline void __qdisc_reset_queue(struct qdisc_skb_head *qh)
1173 {
1174 	/*
1175 	 * We do not know the backlog in bytes of this list, it
1176 	 * is up to the caller to correct it
1177 	 */
1178 	ASSERT_RTNL();
1179 	if (qh->qlen) {
1180 		rtnl_kfree_skbs(qh->head, qh->tail);
1181 
1182 		qh->head = NULL;
1183 		qh->tail = NULL;
1184 		qh->qlen = 0;
1185 	}
1186 }
1187 
qdisc_reset_queue(struct Qdisc * sch)1188 static inline void qdisc_reset_queue(struct Qdisc *sch)
1189 {
1190 	__qdisc_reset_queue(&sch->q);
1191 	sch->qstats.backlog = 0;
1192 }
1193 
qdisc_replace(struct Qdisc * sch,struct Qdisc * new,struct Qdisc ** pold)1194 static inline struct Qdisc *qdisc_replace(struct Qdisc *sch, struct Qdisc *new,
1195 					  struct Qdisc **pold)
1196 {
1197 	struct Qdisc *old;
1198 
1199 	sch_tree_lock(sch);
1200 	old = *pold;
1201 	*pold = new;
1202 	if (old != NULL)
1203 		qdisc_purge_queue(old);
1204 	sch_tree_unlock(sch);
1205 
1206 	return old;
1207 }
1208 
rtnl_qdisc_drop(struct sk_buff * skb,struct Qdisc * sch)1209 static inline void rtnl_qdisc_drop(struct sk_buff *skb, struct Qdisc *sch)
1210 {
1211 	rtnl_kfree_skbs(skb, skb);
1212 	qdisc_qstats_drop(sch);
1213 }
1214 
qdisc_drop_cpu(struct sk_buff * skb,struct Qdisc * sch,struct sk_buff ** to_free)1215 static inline int qdisc_drop_cpu(struct sk_buff *skb, struct Qdisc *sch,
1216 				 struct sk_buff **to_free)
1217 {
1218 	__qdisc_drop(skb, to_free);
1219 	qdisc_qstats_cpu_drop(sch);
1220 
1221 	return NET_XMIT_DROP;
1222 }
1223 
qdisc_drop(struct sk_buff * skb,struct Qdisc * sch,struct sk_buff ** to_free)1224 static inline int qdisc_drop(struct sk_buff *skb, struct Qdisc *sch,
1225 			     struct sk_buff **to_free)
1226 {
1227 	__qdisc_drop(skb, to_free);
1228 	qdisc_qstats_drop(sch);
1229 
1230 	return NET_XMIT_DROP;
1231 }
1232 
qdisc_drop_all(struct sk_buff * skb,struct Qdisc * sch,struct sk_buff ** to_free)1233 static inline int qdisc_drop_all(struct sk_buff *skb, struct Qdisc *sch,
1234 				 struct sk_buff **to_free)
1235 {
1236 	__qdisc_drop_all(skb, to_free);
1237 	qdisc_qstats_drop(sch);
1238 
1239 	return NET_XMIT_DROP;
1240 }
1241 
1242 /* Length to Time (L2T) lookup in a qdisc_rate_table, to determine how
1243    long it will take to send a packet given its size.
1244  */
qdisc_l2t(struct qdisc_rate_table * rtab,unsigned int pktlen)1245 static inline u32 qdisc_l2t(struct qdisc_rate_table* rtab, unsigned int pktlen)
1246 {
1247 	int slot = pktlen + rtab->rate.cell_align + rtab->rate.overhead;
1248 	if (slot < 0)
1249 		slot = 0;
1250 	slot >>= rtab->rate.cell_log;
1251 	if (slot > 255)
1252 		return rtab->data[255]*(slot >> 8) + rtab->data[slot & 0xFF];
1253 	return rtab->data[slot];
1254 }
1255 
1256 struct psched_ratecfg {
1257 	u64	rate_bytes_ps; /* bytes per second */
1258 	u32	mult;
1259 	u16	overhead;
1260 	u8	linklayer;
1261 	u8	shift;
1262 };
1263 
psched_l2t_ns(const struct psched_ratecfg * r,unsigned int len)1264 static inline u64 psched_l2t_ns(const struct psched_ratecfg *r,
1265 				unsigned int len)
1266 {
1267 	len += r->overhead;
1268 
1269 	if (unlikely(r->linklayer == TC_LINKLAYER_ATM))
1270 		return ((u64)(DIV_ROUND_UP(len,48)*53) * r->mult) >> r->shift;
1271 
1272 	return ((u64)len * r->mult) >> r->shift;
1273 }
1274 
1275 void psched_ratecfg_precompute(struct psched_ratecfg *r,
1276 			       const struct tc_ratespec *conf,
1277 			       u64 rate64);
1278 
psched_ratecfg_getrate(struct tc_ratespec * res,const struct psched_ratecfg * r)1279 static inline void psched_ratecfg_getrate(struct tc_ratespec *res,
1280 					  const struct psched_ratecfg *r)
1281 {
1282 	memset(res, 0, sizeof(*res));
1283 
1284 	/* legacy struct tc_ratespec has a 32bit @rate field
1285 	 * Qdisc using 64bit rate should add new attributes
1286 	 * in order to maintain compatibility.
1287 	 */
1288 	res->rate = min_t(u64, r->rate_bytes_ps, ~0U);
1289 
1290 	res->overhead = r->overhead;
1291 	res->linklayer = (r->linklayer & TC_LINKLAYER_MASK);
1292 }
1293 
1294 /* Mini Qdisc serves for specific needs of ingress/clsact Qdisc.
1295  * The fast path only needs to access filter list and to update stats
1296  */
1297 struct mini_Qdisc {
1298 	struct tcf_proto *filter_list;
1299 	struct tcf_block *block;
1300 	struct gnet_stats_basic_cpu __percpu *cpu_bstats;
1301 	struct gnet_stats_queue	__percpu *cpu_qstats;
1302 	struct rcu_head rcu;
1303 };
1304 
mini_qdisc_bstats_cpu_update(struct mini_Qdisc * miniq,const struct sk_buff * skb)1305 static inline void mini_qdisc_bstats_cpu_update(struct mini_Qdisc *miniq,
1306 						const struct sk_buff *skb)
1307 {
1308 	bstats_cpu_update(this_cpu_ptr(miniq->cpu_bstats), skb);
1309 }
1310 
mini_qdisc_qstats_cpu_drop(struct mini_Qdisc * miniq)1311 static inline void mini_qdisc_qstats_cpu_drop(struct mini_Qdisc *miniq)
1312 {
1313 	this_cpu_inc(miniq->cpu_qstats->drops);
1314 }
1315 
1316 struct mini_Qdisc_pair {
1317 	struct mini_Qdisc miniq1;
1318 	struct mini_Qdisc miniq2;
1319 	struct mini_Qdisc __rcu **p_miniq;
1320 };
1321 
1322 void mini_qdisc_pair_swap(struct mini_Qdisc_pair *miniqp,
1323 			  struct tcf_proto *tp_head);
1324 void mini_qdisc_pair_init(struct mini_Qdisc_pair *miniqp, struct Qdisc *qdisc,
1325 			  struct mini_Qdisc __rcu **p_miniq);
1326 void mini_qdisc_pair_block_init(struct mini_Qdisc_pair *miniqp,
1327 				struct tcf_block *block);
1328 
skb_tc_reinsert(struct sk_buff * skb,struct tcf_result * res)1329 static inline int skb_tc_reinsert(struct sk_buff *skb, struct tcf_result *res)
1330 {
1331 	return res->ingress ? netif_receive_skb(skb) : dev_queue_xmit(skb);
1332 }
1333 
1334 #endif
1335