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