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