xref: /linux/include/net/sch_generic.h (revision 0d240e7811c4ec1965760ee4643b5bbc9cfacbb3)
1 #ifndef __NET_SCHED_GENERIC_H
2 #define __NET_SCHED_GENERIC_H
3 
4 #include <linux/netdevice.h>
5 #include <linux/types.h>
6 #include <linux/rcupdate.h>
7 #include <linux/pkt_sched.h>
8 #include <linux/pkt_cls.h>
9 #include <linux/percpu.h>
10 #include <linux/dynamic_queue_limits.h>
11 #include <net/gen_stats.h>
12 #include <net/rtnetlink.h>
13 
14 struct Qdisc_ops;
15 struct qdisc_walker;
16 struct tcf_walker;
17 struct module;
18 
19 struct qdisc_rate_table {
20 	struct tc_ratespec rate;
21 	u32		data[256];
22 	struct qdisc_rate_table *next;
23 	int		refcnt;
24 };
25 
26 enum qdisc_state_t {
27 	__QDISC_STATE_SCHED,
28 	__QDISC_STATE_DEACTIVATED,
29 };
30 
31 struct qdisc_size_table {
32 	struct rcu_head		rcu;
33 	struct list_head	list;
34 	struct tc_sizespec	szopts;
35 	int			refcnt;
36 	u16			data[];
37 };
38 
39 struct Qdisc {
40 	int 			(*enqueue)(struct sk_buff *skb, struct Qdisc *dev);
41 	struct sk_buff *	(*dequeue)(struct Qdisc *dev);
42 	unsigned int		flags;
43 #define TCQ_F_BUILTIN		1
44 #define TCQ_F_INGRESS		2
45 #define TCQ_F_CAN_BYPASS	4
46 #define TCQ_F_MQROOT		8
47 #define TCQ_F_ONETXQUEUE	0x10 /* dequeue_skb() can assume all skbs are for
48 				      * q->dev_queue : It can test
49 				      * netif_xmit_frozen_or_stopped() before
50 				      * dequeueing next packet.
51 				      * Its true for MQ/MQPRIO slaves, or non
52 				      * multiqueue device.
53 				      */
54 #define TCQ_F_WARN_NONWC	(1 << 16)
55 #define TCQ_F_CPUSTATS		0x20 /* run using percpu statistics */
56 #define TCQ_F_NOPARENT		0x40 /* root of its hierarchy :
57 				      * qdisc_tree_decrease_qlen() should stop.
58 				      */
59 	u32			limit;
60 	const struct Qdisc_ops	*ops;
61 	struct qdisc_size_table	__rcu *stab;
62 	struct list_head	list;
63 	u32			handle;
64 	u32			parent;
65 	void			*u32_node;
66 
67 	struct netdev_queue	*dev_queue;
68 
69 	struct gnet_stats_rate_est64	rate_est;
70 	struct gnet_stats_basic_cpu __percpu *cpu_bstats;
71 	struct gnet_stats_queue	__percpu *cpu_qstats;
72 
73 	/*
74 	 * For performance sake on SMP, we put highly modified fields at the end
75 	 */
76 	struct Qdisc		*next_sched ____cacheline_aligned_in_smp;
77 	struct sk_buff		*gso_skb;
78 	unsigned long		state;
79 	struct sk_buff_head	q;
80 	struct gnet_stats_basic_packed bstats;
81 	seqcount_t		running;
82 	struct gnet_stats_queue	qstats;
83 	struct rcu_head		rcu_head;
84 	int			padded;
85 	atomic_t		refcnt;
86 
87 	spinlock_t		busylock ____cacheline_aligned_in_smp;
88 };
89 
90 static inline bool qdisc_is_running(const struct Qdisc *qdisc)
91 {
92 	return (raw_read_seqcount(&qdisc->running) & 1) ? true : false;
93 }
94 
95 static inline bool qdisc_run_begin(struct Qdisc *qdisc)
96 {
97 	if (qdisc_is_running(qdisc))
98 		return false;
99 	/* Variant of write_seqcount_begin() telling lockdep a trylock
100 	 * was attempted.
101 	 */
102 	raw_write_seqcount_begin(&qdisc->running);
103 	seqcount_acquire(&qdisc->running.dep_map, 0, 1, _RET_IP_);
104 	return true;
105 }
106 
107 static inline void qdisc_run_end(struct Qdisc *qdisc)
108 {
109 	write_seqcount_end(&qdisc->running);
110 }
111 
112 static inline bool qdisc_may_bulk(const struct Qdisc *qdisc)
113 {
114 	return qdisc->flags & TCQ_F_ONETXQUEUE;
115 }
116 
117 static inline int qdisc_avail_bulklimit(const struct netdev_queue *txq)
118 {
119 #ifdef CONFIG_BQL
120 	/* Non-BQL migrated drivers will return 0, too. */
121 	return dql_avail(&txq->dql);
122 #else
123 	return 0;
124 #endif
125 }
126 
127 struct Qdisc_class_ops {
128 	/* Child qdisc manipulation */
129 	struct netdev_queue *	(*select_queue)(struct Qdisc *, struct tcmsg *);
130 	int			(*graft)(struct Qdisc *, unsigned long cl,
131 					struct Qdisc *, struct Qdisc **);
132 	struct Qdisc *		(*leaf)(struct Qdisc *, unsigned long cl);
133 	void			(*qlen_notify)(struct Qdisc *, unsigned long);
134 
135 	/* Class manipulation routines */
136 	unsigned long		(*get)(struct Qdisc *, u32 classid);
137 	void			(*put)(struct Qdisc *, unsigned long);
138 	int			(*change)(struct Qdisc *, u32, u32,
139 					struct nlattr **, unsigned long *);
140 	int			(*delete)(struct Qdisc *, unsigned long);
141 	void			(*walk)(struct Qdisc *, struct qdisc_walker * arg);
142 
143 	/* Filter manipulation */
144 	struct tcf_proto __rcu ** (*tcf_chain)(struct Qdisc *, unsigned long);
145 	bool			(*tcf_cl_offload)(u32 classid);
146 	unsigned long		(*bind_tcf)(struct Qdisc *, unsigned long,
147 					u32 classid);
148 	void			(*unbind_tcf)(struct Qdisc *, unsigned long);
149 
150 	/* rtnetlink specific */
151 	int			(*dump)(struct Qdisc *, unsigned long,
152 					struct sk_buff *skb, struct tcmsg*);
153 	int			(*dump_stats)(struct Qdisc *, unsigned long,
154 					struct gnet_dump *);
155 };
156 
157 struct Qdisc_ops {
158 	struct Qdisc_ops	*next;
159 	const struct Qdisc_class_ops	*cl_ops;
160 	char			id[IFNAMSIZ];
161 	int			priv_size;
162 
163 	int 			(*enqueue)(struct sk_buff *, struct Qdisc *);
164 	struct sk_buff *	(*dequeue)(struct Qdisc *);
165 	struct sk_buff *	(*peek)(struct Qdisc *);
166 
167 	int			(*init)(struct Qdisc *, struct nlattr *arg);
168 	void			(*reset)(struct Qdisc *);
169 	void			(*destroy)(struct Qdisc *);
170 	int			(*change)(struct Qdisc *, struct nlattr *arg);
171 	void			(*attach)(struct Qdisc *);
172 
173 	int			(*dump)(struct Qdisc *, struct sk_buff *);
174 	int			(*dump_stats)(struct Qdisc *, struct gnet_dump *);
175 
176 	struct module		*owner;
177 };
178 
179 
180 struct tcf_result {
181 	unsigned long	class;
182 	u32		classid;
183 };
184 
185 struct tcf_proto_ops {
186 	struct list_head	head;
187 	char			kind[IFNAMSIZ];
188 
189 	int			(*classify)(struct sk_buff *,
190 					    const struct tcf_proto *,
191 					    struct tcf_result *);
192 	int			(*init)(struct tcf_proto*);
193 	bool			(*destroy)(struct tcf_proto*, bool);
194 
195 	unsigned long		(*get)(struct tcf_proto*, u32 handle);
196 	int			(*change)(struct net *net, struct sk_buff *,
197 					struct tcf_proto*, unsigned long,
198 					u32 handle, struct nlattr **,
199 					unsigned long *, bool);
200 	int			(*delete)(struct tcf_proto*, unsigned long);
201 	void			(*walk)(struct tcf_proto*, struct tcf_walker *arg);
202 
203 	/* rtnetlink specific */
204 	int			(*dump)(struct net*, struct tcf_proto*, unsigned long,
205 					struct sk_buff *skb, struct tcmsg*);
206 
207 	struct module		*owner;
208 };
209 
210 struct tcf_proto {
211 	/* Fast access part */
212 	struct tcf_proto __rcu	*next;
213 	void __rcu		*root;
214 	int			(*classify)(struct sk_buff *,
215 					    const struct tcf_proto *,
216 					    struct tcf_result *);
217 	__be16			protocol;
218 
219 	/* All the rest */
220 	u32			prio;
221 	u32			classid;
222 	struct Qdisc		*q;
223 	void			*data;
224 	const struct tcf_proto_ops	*ops;
225 	struct rcu_head		rcu;
226 };
227 
228 struct qdisc_skb_cb {
229 	unsigned int		pkt_len;
230 	u16			slave_dev_queue_mapping;
231 	u16			tc_classid;
232 #define QDISC_CB_PRIV_LEN 20
233 	unsigned char		data[QDISC_CB_PRIV_LEN];
234 };
235 
236 static inline void qdisc_cb_private_validate(const struct sk_buff *skb, int sz)
237 {
238 	struct qdisc_skb_cb *qcb;
239 
240 	BUILD_BUG_ON(sizeof(skb->cb) < offsetof(struct qdisc_skb_cb, data) + sz);
241 	BUILD_BUG_ON(sizeof(qcb->data) < sz);
242 }
243 
244 static inline int qdisc_qlen(const struct Qdisc *q)
245 {
246 	return q->q.qlen;
247 }
248 
249 static inline struct qdisc_skb_cb *qdisc_skb_cb(const struct sk_buff *skb)
250 {
251 	return (struct qdisc_skb_cb *)skb->cb;
252 }
253 
254 static inline spinlock_t *qdisc_lock(struct Qdisc *qdisc)
255 {
256 	return &qdisc->q.lock;
257 }
258 
259 static inline struct Qdisc *qdisc_root(const struct Qdisc *qdisc)
260 {
261 	struct Qdisc *q = rcu_dereference_rtnl(qdisc->dev_queue->qdisc);
262 
263 	return q;
264 }
265 
266 static inline struct Qdisc *qdisc_root_sleeping(const struct Qdisc *qdisc)
267 {
268 	return qdisc->dev_queue->qdisc_sleeping;
269 }
270 
271 /* The qdisc root lock is a mechanism by which to top level
272  * of a qdisc tree can be locked from any qdisc node in the
273  * forest.  This allows changing the configuration of some
274  * aspect of the qdisc tree while blocking out asynchronous
275  * qdisc access in the packet processing paths.
276  *
277  * It is only legal to do this when the root will not change
278  * on us.  Otherwise we'll potentially lock the wrong qdisc
279  * root.  This is enforced by holding the RTNL semaphore, which
280  * all users of this lock accessor must do.
281  */
282 static inline spinlock_t *qdisc_root_lock(const struct Qdisc *qdisc)
283 {
284 	struct Qdisc *root = qdisc_root(qdisc);
285 
286 	ASSERT_RTNL();
287 	return qdisc_lock(root);
288 }
289 
290 static inline spinlock_t *qdisc_root_sleeping_lock(const struct Qdisc *qdisc)
291 {
292 	struct Qdisc *root = qdisc_root_sleeping(qdisc);
293 
294 	ASSERT_RTNL();
295 	return qdisc_lock(root);
296 }
297 
298 static inline seqcount_t *qdisc_root_sleeping_running(const struct Qdisc *qdisc)
299 {
300 	struct Qdisc *root = qdisc_root_sleeping(qdisc);
301 
302 	ASSERT_RTNL();
303 	return &root->running;
304 }
305 
306 static inline struct net_device *qdisc_dev(const struct Qdisc *qdisc)
307 {
308 	return qdisc->dev_queue->dev;
309 }
310 
311 static inline void sch_tree_lock(const struct Qdisc *q)
312 {
313 	spin_lock_bh(qdisc_root_sleeping_lock(q));
314 }
315 
316 static inline void sch_tree_unlock(const struct Qdisc *q)
317 {
318 	spin_unlock_bh(qdisc_root_sleeping_lock(q));
319 }
320 
321 #define tcf_tree_lock(tp)	sch_tree_lock((tp)->q)
322 #define tcf_tree_unlock(tp)	sch_tree_unlock((tp)->q)
323 
324 extern struct Qdisc noop_qdisc;
325 extern struct Qdisc_ops noop_qdisc_ops;
326 extern struct Qdisc_ops pfifo_fast_ops;
327 extern struct Qdisc_ops mq_qdisc_ops;
328 extern struct Qdisc_ops noqueue_qdisc_ops;
329 extern const struct Qdisc_ops *default_qdisc_ops;
330 static inline const struct Qdisc_ops *
331 get_default_qdisc_ops(const struct net_device *dev, int ntx)
332 {
333 	return ntx < dev->real_num_tx_queues ?
334 			default_qdisc_ops : &pfifo_fast_ops;
335 }
336 
337 struct Qdisc_class_common {
338 	u32			classid;
339 	struct hlist_node	hnode;
340 };
341 
342 struct Qdisc_class_hash {
343 	struct hlist_head	*hash;
344 	unsigned int		hashsize;
345 	unsigned int		hashmask;
346 	unsigned int		hashelems;
347 };
348 
349 static inline unsigned int qdisc_class_hash(u32 id, u32 mask)
350 {
351 	id ^= id >> 8;
352 	id ^= id >> 4;
353 	return id & mask;
354 }
355 
356 static inline struct Qdisc_class_common *
357 qdisc_class_find(const struct Qdisc_class_hash *hash, u32 id)
358 {
359 	struct Qdisc_class_common *cl;
360 	unsigned int h;
361 
362 	h = qdisc_class_hash(id, hash->hashmask);
363 	hlist_for_each_entry(cl, &hash->hash[h], hnode) {
364 		if (cl->classid == id)
365 			return cl;
366 	}
367 	return NULL;
368 }
369 
370 int qdisc_class_hash_init(struct Qdisc_class_hash *);
371 void qdisc_class_hash_insert(struct Qdisc_class_hash *,
372 			     struct Qdisc_class_common *);
373 void qdisc_class_hash_remove(struct Qdisc_class_hash *,
374 			     struct Qdisc_class_common *);
375 void qdisc_class_hash_grow(struct Qdisc *, struct Qdisc_class_hash *);
376 void qdisc_class_hash_destroy(struct Qdisc_class_hash *);
377 
378 void dev_init_scheduler(struct net_device *dev);
379 void dev_shutdown(struct net_device *dev);
380 void dev_activate(struct net_device *dev);
381 void dev_deactivate(struct net_device *dev);
382 void dev_deactivate_many(struct list_head *head);
383 struct Qdisc *dev_graft_qdisc(struct netdev_queue *dev_queue,
384 			      struct Qdisc *qdisc);
385 void qdisc_reset(struct Qdisc *qdisc);
386 void qdisc_destroy(struct Qdisc *qdisc);
387 void qdisc_tree_reduce_backlog(struct Qdisc *qdisc, unsigned int n,
388 			       unsigned int len);
389 struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue,
390 			  const struct Qdisc_ops *ops);
391 struct Qdisc *qdisc_create_dflt(struct netdev_queue *dev_queue,
392 				const struct Qdisc_ops *ops, u32 parentid);
393 void __qdisc_calculate_pkt_len(struct sk_buff *skb,
394 			       const struct qdisc_size_table *stab);
395 bool tcf_destroy(struct tcf_proto *tp, bool force);
396 void tcf_destroy_chain(struct tcf_proto __rcu **fl);
397 int skb_do_redirect(struct sk_buff *);
398 
399 static inline bool skb_at_tc_ingress(const struct sk_buff *skb)
400 {
401 #ifdef CONFIG_NET_CLS_ACT
402 	return G_TC_AT(skb->tc_verd) & AT_INGRESS;
403 #else
404 	return false;
405 #endif
406 }
407 
408 /* Reset all TX qdiscs greater then index of a device.  */
409 static inline void qdisc_reset_all_tx_gt(struct net_device *dev, unsigned int i)
410 {
411 	struct Qdisc *qdisc;
412 
413 	for (; i < dev->num_tx_queues; i++) {
414 		qdisc = rtnl_dereference(netdev_get_tx_queue(dev, i)->qdisc);
415 		if (qdisc) {
416 			spin_lock_bh(qdisc_lock(qdisc));
417 			qdisc_reset(qdisc);
418 			spin_unlock_bh(qdisc_lock(qdisc));
419 		}
420 	}
421 }
422 
423 static inline void qdisc_reset_all_tx(struct net_device *dev)
424 {
425 	qdisc_reset_all_tx_gt(dev, 0);
426 }
427 
428 /* Are all TX queues of the device empty?  */
429 static inline bool qdisc_all_tx_empty(const struct net_device *dev)
430 {
431 	unsigned int i;
432 
433 	rcu_read_lock();
434 	for (i = 0; i < dev->num_tx_queues; i++) {
435 		struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
436 		const struct Qdisc *q = rcu_dereference(txq->qdisc);
437 
438 		if (q->q.qlen) {
439 			rcu_read_unlock();
440 			return false;
441 		}
442 	}
443 	rcu_read_unlock();
444 	return true;
445 }
446 
447 /* Are any of the TX qdiscs changing?  */
448 static inline bool qdisc_tx_changing(const struct net_device *dev)
449 {
450 	unsigned int i;
451 
452 	for (i = 0; i < dev->num_tx_queues; i++) {
453 		struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
454 		if (rcu_access_pointer(txq->qdisc) != txq->qdisc_sleeping)
455 			return true;
456 	}
457 	return false;
458 }
459 
460 /* Is the device using the noop qdisc on all queues?  */
461 static inline bool qdisc_tx_is_noop(const struct net_device *dev)
462 {
463 	unsigned int i;
464 
465 	for (i = 0; i < dev->num_tx_queues; i++) {
466 		struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
467 		if (rcu_access_pointer(txq->qdisc) != &noop_qdisc)
468 			return false;
469 	}
470 	return true;
471 }
472 
473 static inline unsigned int qdisc_pkt_len(const struct sk_buff *skb)
474 {
475 	return qdisc_skb_cb(skb)->pkt_len;
476 }
477 
478 /* additional qdisc xmit flags (NET_XMIT_MASK in linux/netdevice.h) */
479 enum net_xmit_qdisc_t {
480 	__NET_XMIT_STOLEN = 0x00010000,
481 	__NET_XMIT_BYPASS = 0x00020000,
482 };
483 
484 #ifdef CONFIG_NET_CLS_ACT
485 #define net_xmit_drop_count(e)	((e) & __NET_XMIT_STOLEN ? 0 : 1)
486 #else
487 #define net_xmit_drop_count(e)	(1)
488 #endif
489 
490 static inline void qdisc_calculate_pkt_len(struct sk_buff *skb,
491 					   const struct Qdisc *sch)
492 {
493 #ifdef CONFIG_NET_SCHED
494 	struct qdisc_size_table *stab = rcu_dereference_bh(sch->stab);
495 
496 	if (stab)
497 		__qdisc_calculate_pkt_len(skb, stab);
498 #endif
499 }
500 
501 static inline int qdisc_enqueue(struct sk_buff *skb, struct Qdisc *sch)
502 {
503 	qdisc_calculate_pkt_len(skb, sch);
504 	return sch->enqueue(skb, sch);
505 }
506 
507 static inline bool qdisc_is_percpu_stats(const struct Qdisc *q)
508 {
509 	return q->flags & TCQ_F_CPUSTATS;
510 }
511 
512 static inline void _bstats_update(struct gnet_stats_basic_packed *bstats,
513 				  __u64 bytes, __u32 packets)
514 {
515 	bstats->bytes += bytes;
516 	bstats->packets += packets;
517 }
518 
519 static inline void bstats_update(struct gnet_stats_basic_packed *bstats,
520 				 const struct sk_buff *skb)
521 {
522 	_bstats_update(bstats,
523 		       qdisc_pkt_len(skb),
524 		       skb_is_gso(skb) ? skb_shinfo(skb)->gso_segs : 1);
525 }
526 
527 static inline void _bstats_cpu_update(struct gnet_stats_basic_cpu *bstats,
528 				      __u64 bytes, __u32 packets)
529 {
530 	u64_stats_update_begin(&bstats->syncp);
531 	_bstats_update(&bstats->bstats, bytes, packets);
532 	u64_stats_update_end(&bstats->syncp);
533 }
534 
535 static inline void bstats_cpu_update(struct gnet_stats_basic_cpu *bstats,
536 				     const struct sk_buff *skb)
537 {
538 	u64_stats_update_begin(&bstats->syncp);
539 	bstats_update(&bstats->bstats, skb);
540 	u64_stats_update_end(&bstats->syncp);
541 }
542 
543 static inline void qdisc_bstats_cpu_update(struct Qdisc *sch,
544 					   const struct sk_buff *skb)
545 {
546 	bstats_cpu_update(this_cpu_ptr(sch->cpu_bstats), skb);
547 }
548 
549 static inline void qdisc_bstats_update(struct Qdisc *sch,
550 				       const struct sk_buff *skb)
551 {
552 	bstats_update(&sch->bstats, skb);
553 }
554 
555 static inline void qdisc_qstats_backlog_dec(struct Qdisc *sch,
556 					    const struct sk_buff *skb)
557 {
558 	sch->qstats.backlog -= qdisc_pkt_len(skb);
559 }
560 
561 static inline void qdisc_qstats_backlog_inc(struct Qdisc *sch,
562 					    const struct sk_buff *skb)
563 {
564 	sch->qstats.backlog += qdisc_pkt_len(skb);
565 }
566 
567 static inline void __qdisc_qstats_drop(struct Qdisc *sch, int count)
568 {
569 	sch->qstats.drops += count;
570 }
571 
572 static inline void qstats_drop_inc(struct gnet_stats_queue *qstats)
573 {
574 	qstats->drops++;
575 }
576 
577 static inline void qstats_overlimit_inc(struct gnet_stats_queue *qstats)
578 {
579 	qstats->overlimits++;
580 }
581 
582 static inline void qdisc_qstats_drop(struct Qdisc *sch)
583 {
584 	qstats_drop_inc(&sch->qstats);
585 }
586 
587 static inline void qdisc_qstats_cpu_drop(struct Qdisc *sch)
588 {
589 	qstats_drop_inc(this_cpu_ptr(sch->cpu_qstats));
590 }
591 
592 static inline void qdisc_qstats_overlimit(struct Qdisc *sch)
593 {
594 	sch->qstats.overlimits++;
595 }
596 
597 static inline int __qdisc_enqueue_tail(struct sk_buff *skb, struct Qdisc *sch,
598 				       struct sk_buff_head *list)
599 {
600 	__skb_queue_tail(list, skb);
601 	qdisc_qstats_backlog_inc(sch, skb);
602 
603 	return NET_XMIT_SUCCESS;
604 }
605 
606 static inline int qdisc_enqueue_tail(struct sk_buff *skb, struct Qdisc *sch)
607 {
608 	return __qdisc_enqueue_tail(skb, sch, &sch->q);
609 }
610 
611 static inline struct sk_buff *__qdisc_dequeue_head(struct Qdisc *sch,
612 						   struct sk_buff_head *list)
613 {
614 	struct sk_buff *skb = __skb_dequeue(list);
615 
616 	if (likely(skb != NULL)) {
617 		qdisc_qstats_backlog_dec(sch, skb);
618 		qdisc_bstats_update(sch, skb);
619 	}
620 
621 	return skb;
622 }
623 
624 static inline struct sk_buff *qdisc_dequeue_head(struct Qdisc *sch)
625 {
626 	return __qdisc_dequeue_head(sch, &sch->q);
627 }
628 
629 static inline unsigned int __qdisc_queue_drop_head(struct Qdisc *sch,
630 					      struct sk_buff_head *list)
631 {
632 	struct sk_buff *skb = __skb_dequeue(list);
633 
634 	if (likely(skb != NULL)) {
635 		unsigned int len = qdisc_pkt_len(skb);
636 		qdisc_qstats_backlog_dec(sch, skb);
637 		kfree_skb(skb);
638 		return len;
639 	}
640 
641 	return 0;
642 }
643 
644 static inline unsigned int qdisc_queue_drop_head(struct Qdisc *sch)
645 {
646 	return __qdisc_queue_drop_head(sch, &sch->q);
647 }
648 
649 static inline struct sk_buff *qdisc_peek_head(struct Qdisc *sch)
650 {
651 	return skb_peek(&sch->q);
652 }
653 
654 /* generic pseudo peek method for non-work-conserving qdisc */
655 static inline struct sk_buff *qdisc_peek_dequeued(struct Qdisc *sch)
656 {
657 	/* we can reuse ->gso_skb because peek isn't called for root qdiscs */
658 	if (!sch->gso_skb) {
659 		sch->gso_skb = sch->dequeue(sch);
660 		if (sch->gso_skb) {
661 			/* it's still part of the queue */
662 			qdisc_qstats_backlog_inc(sch, sch->gso_skb);
663 			sch->q.qlen++;
664 		}
665 	}
666 
667 	return sch->gso_skb;
668 }
669 
670 /* use instead of qdisc->dequeue() for all qdiscs queried with ->peek() */
671 static inline struct sk_buff *qdisc_dequeue_peeked(struct Qdisc *sch)
672 {
673 	struct sk_buff *skb = sch->gso_skb;
674 
675 	if (skb) {
676 		sch->gso_skb = NULL;
677 		qdisc_qstats_backlog_dec(sch, skb);
678 		sch->q.qlen--;
679 	} else {
680 		skb = sch->dequeue(sch);
681 	}
682 
683 	return skb;
684 }
685 
686 static inline void __qdisc_reset_queue(struct sk_buff_head *list)
687 {
688 	/*
689 	 * We do not know the backlog in bytes of this list, it
690 	 * is up to the caller to correct it
691 	 */
692 	if (!skb_queue_empty(list)) {
693 		rtnl_kfree_skbs(list->next, list->prev);
694 		__skb_queue_head_init(list);
695 	}
696 }
697 
698 static inline void qdisc_reset_queue(struct Qdisc *sch)
699 {
700 	__qdisc_reset_queue(&sch->q);
701 	sch->qstats.backlog = 0;
702 }
703 
704 static inline struct Qdisc *qdisc_replace(struct Qdisc *sch, struct Qdisc *new,
705 					  struct Qdisc **pold)
706 {
707 	struct Qdisc *old;
708 
709 	sch_tree_lock(sch);
710 	old = *pold;
711 	*pold = new;
712 	if (old != NULL) {
713 		qdisc_tree_reduce_backlog(old, old->q.qlen, old->qstats.backlog);
714 		qdisc_reset(old);
715 	}
716 	sch_tree_unlock(sch);
717 
718 	return old;
719 }
720 
721 static inline void rtnl_qdisc_drop(struct sk_buff *skb, struct Qdisc *sch)
722 {
723 	rtnl_kfree_skbs(skb, skb);
724 	qdisc_qstats_drop(sch);
725 }
726 
727 static inline int qdisc_drop(struct sk_buff *skb, struct Qdisc *sch)
728 {
729 	kfree_skb(skb);
730 	qdisc_qstats_drop(sch);
731 
732 	return NET_XMIT_DROP;
733 }
734 
735 /* Length to Time (L2T) lookup in a qdisc_rate_table, to determine how
736    long it will take to send a packet given its size.
737  */
738 static inline u32 qdisc_l2t(struct qdisc_rate_table* rtab, unsigned int pktlen)
739 {
740 	int slot = pktlen + rtab->rate.cell_align + rtab->rate.overhead;
741 	if (slot < 0)
742 		slot = 0;
743 	slot >>= rtab->rate.cell_log;
744 	if (slot > 255)
745 		return rtab->data[255]*(slot >> 8) + rtab->data[slot & 0xFF];
746 	return rtab->data[slot];
747 }
748 
749 struct psched_ratecfg {
750 	u64	rate_bytes_ps; /* bytes per second */
751 	u32	mult;
752 	u16	overhead;
753 	u8	linklayer;
754 	u8	shift;
755 };
756 
757 static inline u64 psched_l2t_ns(const struct psched_ratecfg *r,
758 				unsigned int len)
759 {
760 	len += r->overhead;
761 
762 	if (unlikely(r->linklayer == TC_LINKLAYER_ATM))
763 		return ((u64)(DIV_ROUND_UP(len,48)*53) * r->mult) >> r->shift;
764 
765 	return ((u64)len * r->mult) >> r->shift;
766 }
767 
768 void psched_ratecfg_precompute(struct psched_ratecfg *r,
769 			       const struct tc_ratespec *conf,
770 			       u64 rate64);
771 
772 static inline void psched_ratecfg_getrate(struct tc_ratespec *res,
773 					  const struct psched_ratecfg *r)
774 {
775 	memset(res, 0, sizeof(*res));
776 
777 	/* legacy struct tc_ratespec has a 32bit @rate field
778 	 * Qdisc using 64bit rate should add new attributes
779 	 * in order to maintain compatibility.
780 	 */
781 	res->rate = min_t(u64, r->rate_bytes_ps, ~0U);
782 
783 	res->overhead = r->overhead;
784 	res->linklayer = (r->linklayer & TC_LINKLAYER_MASK);
785 }
786 
787 #endif
788