1
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
23struct Qdisc_ops;
24struct qdisc_walker;
25struct tcf_walker;
26struct module;
27struct bpf_flow_keys;
28
29struct qdisc_rate_table {
30 struct tc_ratespec rate;
31 u32 data[256];
32 struct qdisc_rate_table *next;
33 int refcnt;
34};
35
36enum qdisc_state_t {
37 __QDISC_STATE_SCHED,
38 __QDISC_STATE_DEACTIVATED,
39 __QDISC_STATE_MISSED,
40};
41
42struct 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
51struct qdisc_skb_head {
52 struct sk_buff *head;
53 struct sk_buff *tail;
54 __u32 qlen;
55 spinlock_t lock;
56};
57
58struct 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
69
70
71
72
73
74
75#define TCQ_F_WARN_NONWC (1 << 16)
76#define TCQ_F_CPUSTATS 0x20
77#define TCQ_F_NOPARENT 0x40
78
79
80#define TCQ_F_INVISIBLE 0x80
81#define TCQ_F_NOLOCK 0x100
82#define TCQ_F_OFFLOADED 0x200
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
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
114 bool empty;
115 struct rcu_head rcu;
116
117
118 long privdata[] ____cacheline_aligned;
119};
120
121static 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
129
130
131
132static 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
141static 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
148static inline bool qdisc_is_percpu_stats(const struct Qdisc *q)
149{
150 return q->flags & TCQ_F_CPUSTATS;
151}
152
153static 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
160static 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
167
168
169
170
171 if (test_bit(__QDISC_STATE_MISSED, &qdisc->state))
172 return false;
173
174
175
176
177
178
179
180
181 set_bit(__QDISC_STATE_MISSED, &qdisc->state);
182
183
184
185
186 if (!spin_trylock(&qdisc->seqlock))
187 return false;
188
189nolock_empty:
190 WRITE_ONCE(qdisc->empty, false);
191 } else if (qdisc_is_running(qdisc)) {
192 return false;
193 }
194
195
196
197 raw_write_seqcount_begin(&qdisc->running);
198 seqcount_acquire(&qdisc->running.dep_map, 0, 1, _RET_IP_);
199 return true;
200}
201
202static inline void qdisc_run_end(struct Qdisc *qdisc)
203{
204 write_seqcount_end(&qdisc->running);
205 if (qdisc->flags & TCQ_F_NOLOCK) {
206 spin_unlock(&qdisc->seqlock);
207
208 if (unlikely(test_bit(__QDISC_STATE_MISSED,
209 &qdisc->state))) {
210 clear_bit(__QDISC_STATE_MISSED, &qdisc->state);
211 __netif_schedule(qdisc);
212 }
213 }
214}
215
216static inline bool qdisc_may_bulk(const struct Qdisc *qdisc)
217{
218 return qdisc->flags & TCQ_F_ONETXQUEUE;
219}
220
221static inline int qdisc_avail_bulklimit(const struct netdev_queue *txq)
222{
223#ifdef CONFIG_BQL
224
225 return dql_avail(&txq->dql);
226#else
227 return 0;
228#endif
229}
230
231struct Qdisc_class_ops {
232 unsigned int flags;
233
234 struct netdev_queue * (*select_queue)(struct Qdisc *, struct tcmsg *);
235 int (*graft)(struct Qdisc *, unsigned long cl,
236 struct Qdisc *, struct Qdisc **,
237 struct netlink_ext_ack *extack);
238 struct Qdisc * (*leaf)(struct Qdisc *, unsigned long cl);
239 void (*qlen_notify)(struct Qdisc *, unsigned long);
240
241
242 unsigned long (*find)(struct Qdisc *, u32 classid);
243 int (*change)(struct Qdisc *, u32, u32,
244 struct nlattr **, unsigned long *,
245 struct netlink_ext_ack *);
246 int (*delete)(struct Qdisc *, unsigned long,
247 struct netlink_ext_ack *);
248 void (*walk)(struct Qdisc *, struct qdisc_walker * arg);
249
250
251 struct tcf_block * (*tcf_block)(struct Qdisc *sch,
252 unsigned long arg,
253 struct netlink_ext_ack *extack);
254 unsigned long (*bind_tcf)(struct Qdisc *, unsigned long,
255 u32 classid);
256 void (*unbind_tcf)(struct Qdisc *, unsigned long);
257
258
259 int (*dump)(struct Qdisc *, unsigned long,
260 struct sk_buff *skb, struct tcmsg*);
261 int (*dump_stats)(struct Qdisc *, unsigned long,
262 struct gnet_dump *);
263};
264
265
266
267
268enum qdisc_class_ops_flags {
269 QDISC_CLASS_OPS_DOIT_UNLOCKED = 1,
270};
271
272struct Qdisc_ops {
273 struct Qdisc_ops *next;
274 const struct Qdisc_class_ops *cl_ops;
275 char id[IFNAMSIZ];
276 int priv_size;
277 unsigned int static_flags;
278
279 int (*enqueue)(struct sk_buff *skb,
280 struct Qdisc *sch,
281 struct sk_buff **to_free);
282 struct sk_buff * (*dequeue)(struct Qdisc *);
283 struct sk_buff * (*peek)(struct Qdisc *);
284
285 int (*init)(struct Qdisc *sch, struct nlattr *arg,
286 struct netlink_ext_ack *extack);
287 void (*reset)(struct Qdisc *);
288 void (*destroy)(struct Qdisc *);
289 int (*change)(struct Qdisc *sch,
290 struct nlattr *arg,
291 struct netlink_ext_ack *extack);
292 void (*attach)(struct Qdisc *sch);
293 int (*change_tx_queue_len)(struct Qdisc *, unsigned int);
294
295 int (*dump)(struct Qdisc *, struct sk_buff *);
296 int (*dump_stats)(struct Qdisc *, struct gnet_dump *);
297
298 void (*ingress_block_set)(struct Qdisc *sch,
299 u32 block_index);
300 void (*egress_block_set)(struct Qdisc *sch,
301 u32 block_index);
302 u32 (*ingress_block_get)(struct Qdisc *sch);
303 u32 (*egress_block_get)(struct Qdisc *sch);
304
305 struct module *owner;
306};
307
308
309struct tcf_result {
310 union {
311 struct {
312 unsigned long class;
313 u32 classid;
314 };
315 const struct tcf_proto *goto_tp;
316
317
318 struct {
319 bool ingress;
320 struct gnet_stats_queue *qstats;
321 };
322 };
323};
324
325struct tcf_chain;
326
327struct tcf_proto_ops {
328 struct list_head head;
329 char kind[IFNAMSIZ];
330
331 int (*classify)(struct sk_buff *,
332 const struct tcf_proto *,
333 struct tcf_result *);
334 int (*init)(struct tcf_proto*);
335 void (*destroy)(struct tcf_proto *tp, bool rtnl_held,
336 struct netlink_ext_ack *extack);
337
338 void* (*get)(struct tcf_proto*, u32 handle);
339 void (*put)(struct tcf_proto *tp, void *f);
340 int (*change)(struct net *net, struct sk_buff *,
341 struct tcf_proto*, unsigned long,
342 u32 handle, struct nlattr **,
343 void **, bool, bool,
344 struct netlink_ext_ack *);
345 int (*delete)(struct tcf_proto *tp, void *arg,
346 bool *last, bool rtnl_held,
347 struct netlink_ext_ack *);
348 bool (*delete_empty)(struct tcf_proto *tp);
349 void (*walk)(struct tcf_proto *tp,
350 struct tcf_walker *arg, bool rtnl_held);
351 int (*reoffload)(struct tcf_proto *tp, bool add,
352 flow_setup_cb_t *cb, void *cb_priv,
353 struct netlink_ext_ack *extack);
354 void (*hw_add)(struct tcf_proto *tp,
355 void *type_data);
356 void (*hw_del)(struct tcf_proto *tp,
357 void *type_data);
358 void (*bind_class)(void *, u32, unsigned long,
359 void *, unsigned long);
360 void * (*tmplt_create)(struct net *net,
361 struct tcf_chain *chain,
362 struct nlattr **tca,
363 struct netlink_ext_ack *extack);
364 void (*tmplt_destroy)(void *tmplt_priv);
365
366
367 int (*dump)(struct net*, struct tcf_proto*, void *,
368 struct sk_buff *skb, struct tcmsg*,
369 bool);
370 int (*terse_dump)(struct net *net,
371 struct tcf_proto *tp, void *fh,
372 struct sk_buff *skb,
373 struct tcmsg *t, bool rtnl_held);
374 int (*tmplt_dump)(struct sk_buff *skb,
375 struct net *net,
376 void *tmplt_priv);
377
378 struct module *owner;
379 int flags;
380};
381
382
383
384
385
386enum tcf_proto_ops_flags {
387 TCF_PROTO_OPS_DOIT_UNLOCKED = 1,
388};
389
390struct tcf_proto {
391
392 struct tcf_proto __rcu *next;
393 void __rcu *root;
394
395
396 int (*classify)(struct sk_buff *,
397 const struct tcf_proto *,
398 struct tcf_result *);
399 __be16 protocol;
400
401
402 u32 prio;
403 void *data;
404 const struct tcf_proto_ops *ops;
405 struct tcf_chain *chain;
406
407
408
409 spinlock_t lock;
410 bool deleting;
411 refcount_t refcnt;
412 struct rcu_head rcu;
413 struct hlist_node destroy_ht_node;
414};
415
416struct qdisc_skb_cb {
417 struct {
418 unsigned int pkt_len;
419 u16 slave_dev_queue_mapping;
420 u16 tc_classid;
421 };
422#define QDISC_CB_PRIV_LEN 20
423 unsigned char data[QDISC_CB_PRIV_LEN];
424};
425
426typedef void tcf_chain_head_change_t(struct tcf_proto *tp_head, void *priv);
427
428struct tcf_chain {
429
430 struct mutex filter_chain_lock;
431 struct tcf_proto __rcu *filter_chain;
432 struct list_head list;
433 struct tcf_block *block;
434 u32 index;
435 unsigned int refcnt;
436 unsigned int action_refcnt;
437 bool explicitly_created;
438 bool flushing;
439 const struct tcf_proto_ops *tmplt_ops;
440 void *tmplt_priv;
441 struct rcu_head rcu;
442};
443
444struct tcf_block {
445
446
447
448 struct mutex lock;
449 struct list_head chain_list;
450 u32 index;
451 u32 classid;
452 refcount_t refcnt;
453 struct net *net;
454 struct Qdisc *q;
455 struct rw_semaphore cb_lock;
456 struct flow_block flow_block;
457 struct list_head owner_list;
458 bool keep_dst;
459 atomic_t offloadcnt;
460 unsigned int nooffloaddevcnt;
461 unsigned int lockeddevcnt;
462 struct {
463 struct tcf_chain *chain;
464 struct list_head filter_chain_list;
465 } chain0;
466 struct rcu_head rcu;
467 DECLARE_HASHTABLE(proto_destroy_ht, 7);
468 struct mutex proto_destroy_lock;
469};
470
471static inline bool lockdep_tcf_chain_is_locked(struct tcf_chain *chain)
472{
473 return lockdep_is_held(&chain->filter_chain_lock);
474}
475
476static inline bool lockdep_tcf_proto_is_locked(struct tcf_proto *tp)
477{
478 return lockdep_is_held(&tp->lock);
479}
480
481#define tcf_chain_dereference(p, chain) \
482 rcu_dereference_protected(p, lockdep_tcf_chain_is_locked(chain))
483
484#define tcf_proto_dereference(p, tp) \
485 rcu_dereference_protected(p, lockdep_tcf_proto_is_locked(tp))
486
487static inline void qdisc_cb_private_validate(const struct sk_buff *skb, int sz)
488{
489 struct qdisc_skb_cb *qcb;
490
491 BUILD_BUG_ON(sizeof(skb->cb) < sizeof(*qcb));
492 BUILD_BUG_ON(sizeof(qcb->data) < sz);
493}
494
495static inline int qdisc_qlen_cpu(const struct Qdisc *q)
496{
497 return this_cpu_ptr(q->cpu_qstats)->qlen;
498}
499
500static inline int qdisc_qlen(const struct Qdisc *q)
501{
502 return q->q.qlen;
503}
504
505static inline int qdisc_qlen_sum(const struct Qdisc *q)
506{
507 __u32 qlen = q->qstats.qlen;
508 int i;
509
510 if (qdisc_is_percpu_stats(q)) {
511 for_each_possible_cpu(i)
512 qlen += per_cpu_ptr(q->cpu_qstats, i)->qlen;
513 } else {
514 qlen += q->q.qlen;
515 }
516
517 return qlen;
518}
519
520static inline struct qdisc_skb_cb *qdisc_skb_cb(const struct sk_buff *skb)
521{
522 return (struct qdisc_skb_cb *)skb->cb;
523}
524
525static inline spinlock_t *qdisc_lock(struct Qdisc *qdisc)
526{
527 return &qdisc->q.lock;
528}
529
530static inline struct Qdisc *qdisc_root(const struct Qdisc *qdisc)
531{
532 struct Qdisc *q = rcu_dereference_rtnl(qdisc->dev_queue->qdisc);
533
534 return q;
535}
536
537static inline struct Qdisc *qdisc_root_bh(const struct Qdisc *qdisc)
538{
539 return rcu_dereference_bh(qdisc->dev_queue->qdisc);
540}
541
542static inline struct Qdisc *qdisc_root_sleeping(const struct Qdisc *qdisc)
543{
544 return qdisc->dev_queue->qdisc_sleeping;
545}
546
547
548
549
550
551
552
553
554
555
556
557
558static inline spinlock_t *qdisc_root_lock(const struct Qdisc *qdisc)
559{
560 struct Qdisc *root = qdisc_root(qdisc);
561
562 ASSERT_RTNL();
563 return qdisc_lock(root);
564}
565
566static inline spinlock_t *qdisc_root_sleeping_lock(const struct Qdisc *qdisc)
567{
568 struct Qdisc *root = qdisc_root_sleeping(qdisc);
569
570 ASSERT_RTNL();
571 return qdisc_lock(root);
572}
573
574static inline seqcount_t *qdisc_root_sleeping_running(const struct Qdisc *qdisc)
575{
576 struct Qdisc *root = qdisc_root_sleeping(qdisc);
577
578 ASSERT_RTNL();
579 return &root->running;
580}
581
582static inline struct net_device *qdisc_dev(const struct Qdisc *qdisc)
583{
584 return qdisc->dev_queue->dev;
585}
586
587static inline void sch_tree_lock(struct Qdisc *q)
588{
589 if (q->flags & TCQ_F_MQROOT)
590 spin_lock_bh(qdisc_lock(q));
591 else
592 spin_lock_bh(qdisc_root_sleeping_lock(q));
593}
594
595static inline void sch_tree_unlock(struct Qdisc *q)
596{
597 if (q->flags & TCQ_F_MQROOT)
598 spin_unlock_bh(qdisc_lock(q));
599 else
600 spin_unlock_bh(qdisc_root_sleeping_lock(q));
601}
602
603extern struct Qdisc noop_qdisc;
604extern struct Qdisc_ops noop_qdisc_ops;
605extern struct Qdisc_ops pfifo_fast_ops;
606extern struct Qdisc_ops mq_qdisc_ops;
607extern struct Qdisc_ops noqueue_qdisc_ops;
608extern const struct Qdisc_ops *default_qdisc_ops;
609static inline const struct Qdisc_ops *
610get_default_qdisc_ops(const struct net_device *dev, int ntx)
611{
612 return ntx < dev->real_num_tx_queues ?
613 default_qdisc_ops : &pfifo_fast_ops;
614}
615
616struct Qdisc_class_common {
617 u32 classid;
618 struct hlist_node hnode;
619};
620
621struct Qdisc_class_hash {
622 struct hlist_head *hash;
623 unsigned int hashsize;
624 unsigned int hashmask;
625 unsigned int hashelems;
626};
627
628static inline unsigned int qdisc_class_hash(u32 id, u32 mask)
629{
630 id ^= id >> 8;
631 id ^= id >> 4;
632 return id & mask;
633}
634
635static inline struct Qdisc_class_common *
636qdisc_class_find(const struct Qdisc_class_hash *hash, u32 id)
637{
638 struct Qdisc_class_common *cl;
639 unsigned int h;
640
641 if (!id)
642 return NULL;
643
644 h = qdisc_class_hash(id, hash->hashmask);
645 hlist_for_each_entry(cl, &hash->hash[h], hnode) {
646 if (cl->classid == id)
647 return cl;
648 }
649 return NULL;
650}
651
652static inline int tc_classid_to_hwtc(struct net_device *dev, u32 classid)
653{
654 u32 hwtc = TC_H_MIN(classid) - TC_H_MIN_PRIORITY;
655
656 return (hwtc < netdev_get_num_tc(dev)) ? hwtc : -EINVAL;
657}
658
659int qdisc_class_hash_init(struct Qdisc_class_hash *);
660void qdisc_class_hash_insert(struct Qdisc_class_hash *,
661 struct Qdisc_class_common *);
662void qdisc_class_hash_remove(struct Qdisc_class_hash *,
663 struct Qdisc_class_common *);
664void qdisc_class_hash_grow(struct Qdisc *, struct Qdisc_class_hash *);
665void qdisc_class_hash_destroy(struct Qdisc_class_hash *);
666
667int dev_qdisc_change_tx_queue_len(struct net_device *dev);
668void dev_init_scheduler(struct net_device *dev);
669void dev_shutdown(struct net_device *dev);
670void dev_activate(struct net_device *dev);
671void dev_deactivate(struct net_device *dev);
672void dev_deactivate_many(struct list_head *head);
673struct Qdisc *dev_graft_qdisc(struct netdev_queue *dev_queue,
674 struct Qdisc *qdisc);
675void qdisc_reset(struct Qdisc *qdisc);
676void qdisc_put(struct Qdisc *qdisc);
677void qdisc_put_unlocked(struct Qdisc *qdisc);
678void qdisc_tree_reduce_backlog(struct Qdisc *qdisc, int n, int len);
679#ifdef CONFIG_NET_SCHED
680int qdisc_offload_dump_helper(struct Qdisc *q, enum tc_setup_type type,
681 void *type_data);
682void qdisc_offload_graft_helper(struct net_device *dev, struct Qdisc *sch,
683 struct Qdisc *new, struct Qdisc *old,
684 enum tc_setup_type type, void *type_data,
685 struct netlink_ext_ack *extack);
686#else
687static inline int
688qdisc_offload_dump_helper(struct Qdisc *q, enum tc_setup_type type,
689 void *type_data)
690{
691 q->flags &= ~TCQ_F_OFFLOADED;
692 return 0;
693}
694
695static inline void
696qdisc_offload_graft_helper(struct net_device *dev, struct Qdisc *sch,
697 struct Qdisc *new, struct Qdisc *old,
698 enum tc_setup_type type, void *type_data,
699 struct netlink_ext_ack *extack)
700{
701}
702#endif
703struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue,
704 const struct Qdisc_ops *ops,
705 struct netlink_ext_ack *extack);
706void qdisc_free(struct Qdisc *qdisc);
707struct Qdisc *qdisc_create_dflt(struct netdev_queue *dev_queue,
708 const struct Qdisc_ops *ops, u32 parentid,
709 struct netlink_ext_ack *extack);
710void __qdisc_calculate_pkt_len(struct sk_buff *skb,
711 const struct qdisc_size_table *stab);
712int skb_do_redirect(struct sk_buff *);
713
714static inline bool skb_at_tc_ingress(const struct sk_buff *skb)
715{
716#ifdef CONFIG_NET_CLS_ACT
717 return skb->tc_at_ingress;
718#else
719 return false;
720#endif
721}
722
723static inline bool skb_skip_tc_classify(struct sk_buff *skb)
724{
725#ifdef CONFIG_NET_CLS_ACT
726 if (skb->tc_skip_classify) {
727 skb->tc_skip_classify = 0;
728 return true;
729 }
730#endif
731 return false;
732}
733
734
735static inline void qdisc_reset_all_tx_gt(struct net_device *dev, unsigned int i)
736{
737 struct Qdisc *qdisc;
738
739 for (; i < dev->num_tx_queues; i++) {
740 qdisc = rtnl_dereference(netdev_get_tx_queue(dev, i)->qdisc);
741 if (qdisc) {
742 spin_lock_bh(qdisc_lock(qdisc));
743 qdisc_reset(qdisc);
744 spin_unlock_bh(qdisc_lock(qdisc));
745 }
746 }
747}
748
749static inline void qdisc_reset_all_tx(struct net_device *dev)
750{
751 qdisc_reset_all_tx_gt(dev, 0);
752}
753
754
755static inline bool qdisc_all_tx_empty(const struct net_device *dev)
756{
757 unsigned int i;
758
759 rcu_read_lock();
760 for (i = 0; i < dev->num_tx_queues; i++) {
761 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
762 const struct Qdisc *q = rcu_dereference(txq->qdisc);
763
764 if (!qdisc_is_empty(q)) {
765 rcu_read_unlock();
766 return false;
767 }
768 }
769 rcu_read_unlock();
770 return true;
771}
772
773
774static inline bool qdisc_tx_changing(const struct net_device *dev)
775{
776 unsigned int i;
777
778 for (i = 0; i < dev->num_tx_queues; i++) {
779 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
780 if (rcu_access_pointer(txq->qdisc) != txq->qdisc_sleeping)
781 return true;
782 }
783 return false;
784}
785
786
787static inline bool qdisc_tx_is_noop(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) != &noop_qdisc)
794 return false;
795 }
796 return true;
797}
798
799static inline unsigned int qdisc_pkt_len(const struct sk_buff *skb)
800{
801 return qdisc_skb_cb(skb)->pkt_len;
802}
803
804
805enum net_xmit_qdisc_t {
806 __NET_XMIT_STOLEN = 0x00010000,
807 __NET_XMIT_BYPASS = 0x00020000,
808};
809
810#ifdef CONFIG_NET_CLS_ACT
811#define net_xmit_drop_count(e) ((e) & __NET_XMIT_STOLEN ? 0 : 1)
812#else
813#define net_xmit_drop_count(e) (1)
814#endif
815
816static inline void qdisc_calculate_pkt_len(struct sk_buff *skb,
817 const struct Qdisc *sch)
818{
819#ifdef CONFIG_NET_SCHED
820 struct qdisc_size_table *stab = rcu_dereference_bh(sch->stab);
821
822 if (stab)
823 __qdisc_calculate_pkt_len(skb, stab);
824#endif
825}
826
827static inline int qdisc_enqueue(struct sk_buff *skb, struct Qdisc *sch,
828 struct sk_buff **to_free)
829{
830 qdisc_calculate_pkt_len(skb, sch);
831 return sch->enqueue(skb, sch, to_free);
832}
833
834static inline void _bstats_update(struct gnet_stats_basic_packed *bstats,
835 __u64 bytes, __u32 packets)
836{
837 bstats->bytes += bytes;
838 bstats->packets += packets;
839}
840
841static inline void bstats_update(struct gnet_stats_basic_packed *bstats,
842 const struct sk_buff *skb)
843{
844 _bstats_update(bstats,
845 qdisc_pkt_len(skb),
846 skb_is_gso(skb) ? skb_shinfo(skb)->gso_segs : 1);
847}
848
849static inline void _bstats_cpu_update(struct gnet_stats_basic_cpu *bstats,
850 __u64 bytes, __u32 packets)
851{
852 u64_stats_update_begin(&bstats->syncp);
853 _bstats_update(&bstats->bstats, bytes, packets);
854 u64_stats_update_end(&bstats->syncp);
855}
856
857static inline void bstats_cpu_update(struct gnet_stats_basic_cpu *bstats,
858 const struct sk_buff *skb)
859{
860 u64_stats_update_begin(&bstats->syncp);
861 bstats_update(&bstats->bstats, skb);
862 u64_stats_update_end(&bstats->syncp);
863}
864
865static inline void qdisc_bstats_cpu_update(struct Qdisc *sch,
866 const struct sk_buff *skb)
867{
868 bstats_cpu_update(this_cpu_ptr(sch->cpu_bstats), skb);
869}
870
871static inline void qdisc_bstats_update(struct Qdisc *sch,
872 const struct sk_buff *skb)
873{
874 bstats_update(&sch->bstats, skb);
875}
876
877static inline void qdisc_qstats_backlog_dec(struct Qdisc *sch,
878 const struct sk_buff *skb)
879{
880 sch->qstats.backlog -= qdisc_pkt_len(skb);
881}
882
883static inline void qdisc_qstats_cpu_backlog_dec(struct Qdisc *sch,
884 const struct sk_buff *skb)
885{
886 this_cpu_sub(sch->cpu_qstats->backlog, qdisc_pkt_len(skb));
887}
888
889static inline void qdisc_qstats_backlog_inc(struct Qdisc *sch,
890 const struct sk_buff *skb)
891{
892 sch->qstats.backlog += qdisc_pkt_len(skb);
893}
894
895static inline void qdisc_qstats_cpu_backlog_inc(struct Qdisc *sch,
896 const struct sk_buff *skb)
897{
898 this_cpu_add(sch->cpu_qstats->backlog, qdisc_pkt_len(skb));
899}
900
901static inline void qdisc_qstats_cpu_qlen_inc(struct Qdisc *sch)
902{
903 this_cpu_inc(sch->cpu_qstats->qlen);
904}
905
906static inline void qdisc_qstats_cpu_qlen_dec(struct Qdisc *sch)
907{
908 this_cpu_dec(sch->cpu_qstats->qlen);
909}
910
911static inline void qdisc_qstats_cpu_requeues_inc(struct Qdisc *sch)
912{
913 this_cpu_inc(sch->cpu_qstats->requeues);
914}
915
916static inline void __qdisc_qstats_drop(struct Qdisc *sch, int count)
917{
918 sch->qstats.drops += count;
919}
920
921static inline void qstats_drop_inc(struct gnet_stats_queue *qstats)
922{
923 qstats->drops++;
924}
925
926static inline void qstats_overlimit_inc(struct gnet_stats_queue *qstats)
927{
928 qstats->overlimits++;
929}
930
931static inline void qdisc_qstats_drop(struct Qdisc *sch)
932{
933 qstats_drop_inc(&sch->qstats);
934}
935
936static inline void qdisc_qstats_cpu_drop(struct Qdisc *sch)
937{
938 this_cpu_inc(sch->cpu_qstats->drops);
939}
940
941static inline void qdisc_qstats_overlimit(struct Qdisc *sch)
942{
943 sch->qstats.overlimits++;
944}
945
946static inline int qdisc_qstats_copy(struct gnet_dump *d, struct Qdisc *sch)
947{
948 __u32 qlen = qdisc_qlen_sum(sch);
949
950 return gnet_stats_copy_queue(d, sch->cpu_qstats, &sch->qstats, qlen);
951}
952
953static inline void qdisc_qstats_qlen_backlog(struct Qdisc *sch, __u32 *qlen,
954 __u32 *backlog)
955{
956 struct gnet_stats_queue qstats = { 0 };
957 __u32 len = qdisc_qlen_sum(sch);
958
959 __gnet_stats_copy_queue(&qstats, sch->cpu_qstats, &sch->qstats, len);
960 *qlen = qstats.qlen;
961 *backlog = qstats.backlog;
962}
963
964static inline void qdisc_tree_flush_backlog(struct Qdisc *sch)
965{
966 __u32 qlen, backlog;
967
968 qdisc_qstats_qlen_backlog(sch, &qlen, &backlog);
969 qdisc_tree_reduce_backlog(sch, qlen, backlog);
970}
971
972static inline void qdisc_purge_queue(struct Qdisc *sch)
973{
974 __u32 qlen, backlog;
975
976 qdisc_qstats_qlen_backlog(sch, &qlen, &backlog);
977 qdisc_reset(sch);
978 qdisc_tree_reduce_backlog(sch, qlen, backlog);
979}
980
981static inline void qdisc_skb_head_init(struct qdisc_skb_head *qh)
982{
983 qh->head = NULL;
984 qh->tail = NULL;
985 qh->qlen = 0;
986}
987
988static inline void __qdisc_enqueue_tail(struct sk_buff *skb,
989 struct qdisc_skb_head *qh)
990{
991 struct sk_buff *last = qh->tail;
992
993 if (last) {
994 skb->next = NULL;
995 last->next = skb;
996 qh->tail = skb;
997 } else {
998 qh->tail = skb;
999 qh->head = skb;
1000 }
1001 qh->qlen++;
1002}
1003
1004static inline int qdisc_enqueue_tail(struct sk_buff *skb, struct Qdisc *sch)
1005{
1006 __qdisc_enqueue_tail(skb, &sch->q);
1007 qdisc_qstats_backlog_inc(sch, skb);
1008 return NET_XMIT_SUCCESS;
1009}
1010
1011static inline void __qdisc_enqueue_head(struct sk_buff *skb,
1012 struct qdisc_skb_head *qh)
1013{
1014 skb->next = qh->head;
1015
1016 if (!qh->head)
1017 qh->tail = skb;
1018 qh->head = skb;
1019 qh->qlen++;
1020}
1021
1022static inline struct sk_buff *__qdisc_dequeue_head(struct qdisc_skb_head *qh)
1023{
1024 struct sk_buff *skb = qh->head;
1025
1026 if (likely(skb != NULL)) {
1027 qh->head = skb->next;
1028 qh->qlen--;
1029 if (qh->head == NULL)
1030 qh->tail = NULL;
1031 skb->next = NULL;
1032 }
1033
1034 return skb;
1035}
1036
1037static inline struct sk_buff *qdisc_dequeue_head(struct Qdisc *sch)
1038{
1039 struct sk_buff *skb = __qdisc_dequeue_head(&sch->q);
1040
1041 if (likely(skb != NULL)) {
1042 qdisc_qstats_backlog_dec(sch, skb);
1043 qdisc_bstats_update(sch, skb);
1044 }
1045
1046 return skb;
1047}
1048
1049
1050
1051
1052static inline void __qdisc_drop(struct sk_buff *skb, struct sk_buff **to_free)
1053{
1054 skb->next = *to_free;
1055 *to_free = skb;
1056}
1057
1058static inline void __qdisc_drop_all(struct sk_buff *skb,
1059 struct sk_buff **to_free)
1060{
1061 if (skb->prev)
1062 skb->prev->next = *to_free;
1063 else
1064 skb->next = *to_free;
1065 *to_free = skb;
1066}
1067
1068static inline unsigned int __qdisc_queue_drop_head(struct Qdisc *sch,
1069 struct qdisc_skb_head *qh,
1070 struct sk_buff **to_free)
1071{
1072 struct sk_buff *skb = __qdisc_dequeue_head(qh);
1073
1074 if (likely(skb != NULL)) {
1075 unsigned int len = qdisc_pkt_len(skb);
1076
1077 qdisc_qstats_backlog_dec(sch, skb);
1078 __qdisc_drop(skb, to_free);
1079 return len;
1080 }
1081
1082 return 0;
1083}
1084
1085static inline struct sk_buff *qdisc_peek_head(struct Qdisc *sch)
1086{
1087 const struct qdisc_skb_head *qh = &sch->q;
1088
1089 return qh->head;
1090}
1091
1092
1093static inline struct sk_buff *qdisc_peek_dequeued(struct Qdisc *sch)
1094{
1095 struct sk_buff *skb = skb_peek(&sch->gso_skb);
1096
1097
1098 if (!skb) {
1099 skb = sch->dequeue(sch);
1100
1101 if (skb) {
1102 __skb_queue_head(&sch->gso_skb, skb);
1103
1104 qdisc_qstats_backlog_inc(sch, skb);
1105 sch->q.qlen++;
1106 }
1107 }
1108
1109 return skb;
1110}
1111
1112static inline void qdisc_update_stats_at_dequeue(struct Qdisc *sch,
1113 struct sk_buff *skb)
1114{
1115 if (qdisc_is_percpu_stats(sch)) {
1116 qdisc_qstats_cpu_backlog_dec(sch, skb);
1117 qdisc_bstats_cpu_update(sch, skb);
1118 qdisc_qstats_cpu_qlen_dec(sch);
1119 } else {
1120 qdisc_qstats_backlog_dec(sch, skb);
1121 qdisc_bstats_update(sch, skb);
1122 sch->q.qlen--;
1123 }
1124}
1125
1126static inline void qdisc_update_stats_at_enqueue(struct Qdisc *sch,
1127 unsigned int pkt_len)
1128{
1129 if (qdisc_is_percpu_stats(sch)) {
1130 qdisc_qstats_cpu_qlen_inc(sch);
1131 this_cpu_add(sch->cpu_qstats->backlog, pkt_len);
1132 } else {
1133 sch->qstats.backlog += pkt_len;
1134 sch->q.qlen++;
1135 }
1136}
1137
1138
1139static inline struct sk_buff *qdisc_dequeue_peeked(struct Qdisc *sch)
1140{
1141 struct sk_buff *skb = skb_peek(&sch->gso_skb);
1142
1143 if (skb) {
1144 skb = __skb_dequeue(&sch->gso_skb);
1145 if (qdisc_is_percpu_stats(sch)) {
1146 qdisc_qstats_cpu_backlog_dec(sch, skb);
1147 qdisc_qstats_cpu_qlen_dec(sch);
1148 } else {
1149 qdisc_qstats_backlog_dec(sch, skb);
1150 sch->q.qlen--;
1151 }
1152 } else {
1153 skb = sch->dequeue(sch);
1154 }
1155
1156 return skb;
1157}
1158
1159static inline void __qdisc_reset_queue(struct qdisc_skb_head *qh)
1160{
1161
1162
1163
1164
1165 ASSERT_RTNL();
1166 if (qh->qlen) {
1167 rtnl_kfree_skbs(qh->head, qh->tail);
1168
1169 qh->head = NULL;
1170 qh->tail = NULL;
1171 qh->qlen = 0;
1172 }
1173}
1174
1175static inline void qdisc_reset_queue(struct Qdisc *sch)
1176{
1177 __qdisc_reset_queue(&sch->q);
1178 sch->qstats.backlog = 0;
1179}
1180
1181static inline struct Qdisc *qdisc_replace(struct Qdisc *sch, struct Qdisc *new,
1182 struct Qdisc **pold)
1183{
1184 struct Qdisc *old;
1185
1186 sch_tree_lock(sch);
1187 old = *pold;
1188 *pold = new;
1189 if (old != NULL)
1190 qdisc_purge_queue(old);
1191 sch_tree_unlock(sch);
1192
1193 return old;
1194}
1195
1196static inline void rtnl_qdisc_drop(struct sk_buff *skb, struct Qdisc *sch)
1197{
1198 rtnl_kfree_skbs(skb, skb);
1199 qdisc_qstats_drop(sch);
1200}
1201
1202static inline int qdisc_drop_cpu(struct sk_buff *skb, struct Qdisc *sch,
1203 struct sk_buff **to_free)
1204{
1205 __qdisc_drop(skb, to_free);
1206 qdisc_qstats_cpu_drop(sch);
1207
1208 return NET_XMIT_DROP;
1209}
1210
1211static inline int qdisc_drop(struct sk_buff *skb, struct Qdisc *sch,
1212 struct sk_buff **to_free)
1213{
1214 __qdisc_drop(skb, to_free);
1215 qdisc_qstats_drop(sch);
1216
1217 return NET_XMIT_DROP;
1218}
1219
1220static inline int qdisc_drop_all(struct sk_buff *skb, struct Qdisc *sch,
1221 struct sk_buff **to_free)
1222{
1223 __qdisc_drop_all(skb, to_free);
1224 qdisc_qstats_drop(sch);
1225
1226 return NET_XMIT_DROP;
1227}
1228
1229
1230
1231
1232static inline u32 qdisc_l2t(struct qdisc_rate_table* rtab, unsigned int pktlen)
1233{
1234 int slot = pktlen + rtab->rate.cell_align + rtab->rate.overhead;
1235 if (slot < 0)
1236 slot = 0;
1237 slot >>= rtab->rate.cell_log;
1238 if (slot > 255)
1239 return rtab->data[255]*(slot >> 8) + rtab->data[slot & 0xFF];
1240 return rtab->data[slot];
1241}
1242
1243struct psched_ratecfg {
1244 u64 rate_bytes_ps;
1245 u32 mult;
1246 u16 overhead;
1247 u8 linklayer;
1248 u8 shift;
1249};
1250
1251static inline u64 psched_l2t_ns(const struct psched_ratecfg *r,
1252 unsigned int len)
1253{
1254 len += r->overhead;
1255
1256 if (unlikely(r->linklayer == TC_LINKLAYER_ATM))
1257 return ((u64)(DIV_ROUND_UP(len,48)*53) * r->mult) >> r->shift;
1258
1259 return ((u64)len * r->mult) >> r->shift;
1260}
1261
1262void psched_ratecfg_precompute(struct psched_ratecfg *r,
1263 const struct tc_ratespec *conf,
1264 u64 rate64);
1265
1266static inline void psched_ratecfg_getrate(struct tc_ratespec *res,
1267 const struct psched_ratecfg *r)
1268{
1269 memset(res, 0, sizeof(*res));
1270
1271
1272
1273
1274
1275 res->rate = min_t(u64, r->rate_bytes_ps, ~0U);
1276
1277 res->overhead = r->overhead;
1278 res->linklayer = (r->linklayer & TC_LINKLAYER_MASK);
1279}
1280
1281struct psched_pktrate {
1282 u64 rate_pkts_ps;
1283 u32 mult;
1284 u8 shift;
1285};
1286
1287static inline u64 psched_pkt2t_ns(const struct psched_pktrate *r,
1288 unsigned int pkt_num)
1289{
1290 return ((u64)pkt_num * r->mult) >> r->shift;
1291}
1292
1293void psched_ppscfg_precompute(struct psched_pktrate *r, u64 pktrate64);
1294
1295
1296
1297
1298struct mini_Qdisc {
1299 struct tcf_proto *filter_list;
1300 struct tcf_block *block;
1301 struct gnet_stats_basic_cpu __percpu *cpu_bstats;
1302 struct gnet_stats_queue __percpu *cpu_qstats;
1303 struct rcu_head rcu;
1304};
1305
1306static inline void mini_qdisc_bstats_cpu_update(struct mini_Qdisc *miniq,
1307 const struct sk_buff *skb)
1308{
1309 bstats_cpu_update(this_cpu_ptr(miniq->cpu_bstats), skb);
1310}
1311
1312static inline void mini_qdisc_qstats_cpu_drop(struct mini_Qdisc *miniq)
1313{
1314 this_cpu_inc(miniq->cpu_qstats->drops);
1315}
1316
1317struct mini_Qdisc_pair {
1318 struct mini_Qdisc miniq1;
1319 struct mini_Qdisc miniq2;
1320 struct mini_Qdisc __rcu **p_miniq;
1321};
1322
1323void mini_qdisc_pair_swap(struct mini_Qdisc_pair *miniqp,
1324 struct tcf_proto *tp_head);
1325void mini_qdisc_pair_init(struct mini_Qdisc_pair *miniqp, struct Qdisc *qdisc,
1326 struct mini_Qdisc __rcu **p_miniq);
1327void mini_qdisc_pair_block_init(struct mini_Qdisc_pair *miniqp,
1328 struct tcf_block *block);
1329
1330int sch_frag_xmit_hook(struct sk_buff *skb, int (*xmit)(struct sk_buff *skb));
1331
1332#endif
1333