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