]> asedeno.scripts.mit.edu Git - linux.git/blob - include/net/sch_generic.h
net: sched: prevent insertion of new classifiers during chain flush
[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         bool flushing;
360         const struct tcf_proto_ops *tmplt_ops;
361         void *tmplt_priv;
362 };
363
364 struct tcf_block {
365         /* Lock protects tcf_block and lifetime-management data of chains
366          * attached to the block (refcnt, action_refcnt, explicitly_created).
367          */
368         struct mutex lock;
369         struct list_head chain_list;
370         u32 index; /* block index for shared blocks */
371         refcount_t refcnt;
372         struct net *net;
373         struct Qdisc *q;
374         struct list_head cb_list;
375         struct list_head owner_list;
376         bool keep_dst;
377         unsigned int offloadcnt; /* Number of oddloaded filters */
378         unsigned int nooffloaddevcnt; /* Number of devs unable to do offload */
379         struct {
380                 struct tcf_chain *chain;
381                 struct list_head filter_chain_list;
382         } chain0;
383         struct rcu_head rcu;
384 };
385
386 #ifdef CONFIG_PROVE_LOCKING
387 static inline bool lockdep_tcf_chain_is_locked(struct tcf_chain *chain)
388 {
389         return lockdep_is_held(&chain->filter_chain_lock);
390 }
391
392 static inline bool lockdep_tcf_proto_is_locked(struct tcf_proto *tp)
393 {
394         return lockdep_is_held(&tp->lock);
395 }
396 #else
397 static inline bool lockdep_tcf_chain_is_locked(struct tcf_block *chain)
398 {
399         return true;
400 }
401
402 static inline bool lockdep_tcf_proto_is_locked(struct tcf_proto *tp)
403 {
404         return true;
405 }
406 #endif /* #ifdef CONFIG_PROVE_LOCKING */
407
408 #define tcf_chain_dereference(p, chain)                                 \
409         rcu_dereference_protected(p, lockdep_tcf_chain_is_locked(chain))
410
411 #define tcf_proto_dereference(p, tp)                                    \
412         rcu_dereference_protected(p, lockdep_tcf_proto_is_locked(tp))
413
414 static inline void tcf_block_offload_inc(struct tcf_block *block, u32 *flags)
415 {
416         if (*flags & TCA_CLS_FLAGS_IN_HW)
417                 return;
418         *flags |= TCA_CLS_FLAGS_IN_HW;
419         block->offloadcnt++;
420 }
421
422 static inline void tcf_block_offload_dec(struct tcf_block *block, u32 *flags)
423 {
424         if (!(*flags & TCA_CLS_FLAGS_IN_HW))
425                 return;
426         *flags &= ~TCA_CLS_FLAGS_IN_HW;
427         block->offloadcnt--;
428 }
429
430 static inline void
431 tc_cls_offload_cnt_update(struct tcf_block *block, u32 *cnt,
432                           u32 *flags, bool add)
433 {
434         if (add) {
435                 if (!*cnt)
436                         tcf_block_offload_inc(block, flags);
437                 (*cnt)++;
438         } else {
439                 (*cnt)--;
440                 if (!*cnt)
441                         tcf_block_offload_dec(block, flags);
442         }
443 }
444
445 static inline void qdisc_cb_private_validate(const struct sk_buff *skb, int sz)
446 {
447         struct qdisc_skb_cb *qcb;
448
449         BUILD_BUG_ON(sizeof(skb->cb) < offsetof(struct qdisc_skb_cb, data) + sz);
450         BUILD_BUG_ON(sizeof(qcb->data) < sz);
451 }
452
453 static inline int qdisc_qlen_cpu(const struct Qdisc *q)
454 {
455         return this_cpu_ptr(q->cpu_qstats)->qlen;
456 }
457
458 static inline int qdisc_qlen(const struct Qdisc *q)
459 {
460         return q->q.qlen;
461 }
462
463 static inline int qdisc_qlen_sum(const struct Qdisc *q)
464 {
465         __u32 qlen = q->qstats.qlen;
466         int i;
467
468         if (q->flags & TCQ_F_NOLOCK) {
469                 for_each_possible_cpu(i)
470                         qlen += per_cpu_ptr(q->cpu_qstats, i)->qlen;
471         } else {
472                 qlen += q->q.qlen;
473         }
474
475         return qlen;
476 }
477
478 static inline struct qdisc_skb_cb *qdisc_skb_cb(const struct sk_buff *skb)
479 {
480         return (struct qdisc_skb_cb *)skb->cb;
481 }
482
483 static inline spinlock_t *qdisc_lock(struct Qdisc *qdisc)
484 {
485         return &qdisc->q.lock;
486 }
487
488 static inline struct Qdisc *qdisc_root(const struct Qdisc *qdisc)
489 {
490         struct Qdisc *q = rcu_dereference_rtnl(qdisc->dev_queue->qdisc);
491
492         return q;
493 }
494
495 static inline struct Qdisc *qdisc_root_sleeping(const struct Qdisc *qdisc)
496 {
497         return qdisc->dev_queue->qdisc_sleeping;
498 }
499
500 /* The qdisc root lock is a mechanism by which to top level
501  * of a qdisc tree can be locked from any qdisc node in the
502  * forest.  This allows changing the configuration of some
503  * aspect of the qdisc tree while blocking out asynchronous
504  * qdisc access in the packet processing paths.
505  *
506  * It is only legal to do this when the root will not change
507  * on us.  Otherwise we'll potentially lock the wrong qdisc
508  * root.  This is enforced by holding the RTNL semaphore, which
509  * all users of this lock accessor must do.
510  */
511 static inline spinlock_t *qdisc_root_lock(const struct Qdisc *qdisc)
512 {
513         struct Qdisc *root = qdisc_root(qdisc);
514
515         ASSERT_RTNL();
516         return qdisc_lock(root);
517 }
518
519 static inline spinlock_t *qdisc_root_sleeping_lock(const struct Qdisc *qdisc)
520 {
521         struct Qdisc *root = qdisc_root_sleeping(qdisc);
522
523         ASSERT_RTNL();
524         return qdisc_lock(root);
525 }
526
527 static inline seqcount_t *qdisc_root_sleeping_running(const struct Qdisc *qdisc)
528 {
529         struct Qdisc *root = qdisc_root_sleeping(qdisc);
530
531         ASSERT_RTNL();
532         return &root->running;
533 }
534
535 static inline struct net_device *qdisc_dev(const struct Qdisc *qdisc)
536 {
537         return qdisc->dev_queue->dev;
538 }
539
540 static inline void sch_tree_lock(const struct Qdisc *q)
541 {
542         spin_lock_bh(qdisc_root_sleeping_lock(q));
543 }
544
545 static inline void sch_tree_unlock(const struct Qdisc *q)
546 {
547         spin_unlock_bh(qdisc_root_sleeping_lock(q));
548 }
549
550 extern struct Qdisc noop_qdisc;
551 extern struct Qdisc_ops noop_qdisc_ops;
552 extern struct Qdisc_ops pfifo_fast_ops;
553 extern struct Qdisc_ops mq_qdisc_ops;
554 extern struct Qdisc_ops noqueue_qdisc_ops;
555 extern const struct Qdisc_ops *default_qdisc_ops;
556 static inline const struct Qdisc_ops *
557 get_default_qdisc_ops(const struct net_device *dev, int ntx)
558 {
559         return ntx < dev->real_num_tx_queues ?
560                         default_qdisc_ops : &pfifo_fast_ops;
561 }
562
563 struct Qdisc_class_common {
564         u32                     classid;
565         struct hlist_node       hnode;
566 };
567
568 struct Qdisc_class_hash {
569         struct hlist_head       *hash;
570         unsigned int            hashsize;
571         unsigned int            hashmask;
572         unsigned int            hashelems;
573 };
574
575 static inline unsigned int qdisc_class_hash(u32 id, u32 mask)
576 {
577         id ^= id >> 8;
578         id ^= id >> 4;
579         return id & mask;
580 }
581
582 static inline struct Qdisc_class_common *
583 qdisc_class_find(const struct Qdisc_class_hash *hash, u32 id)
584 {
585         struct Qdisc_class_common *cl;
586         unsigned int h;
587
588         if (!id)
589                 return NULL;
590
591         h = qdisc_class_hash(id, hash->hashmask);
592         hlist_for_each_entry(cl, &hash->hash[h], hnode) {
593                 if (cl->classid == id)
594                         return cl;
595         }
596         return NULL;
597 }
598
599 static inline int tc_classid_to_hwtc(struct net_device *dev, u32 classid)
600 {
601         u32 hwtc = TC_H_MIN(classid) - TC_H_MIN_PRIORITY;
602
603         return (hwtc < netdev_get_num_tc(dev)) ? hwtc : -EINVAL;
604 }
605
606 int qdisc_class_hash_init(struct Qdisc_class_hash *);
607 void qdisc_class_hash_insert(struct Qdisc_class_hash *,
608                              struct Qdisc_class_common *);
609 void qdisc_class_hash_remove(struct Qdisc_class_hash *,
610                              struct Qdisc_class_common *);
611 void qdisc_class_hash_grow(struct Qdisc *, struct Qdisc_class_hash *);
612 void qdisc_class_hash_destroy(struct Qdisc_class_hash *);
613
614 int dev_qdisc_change_tx_queue_len(struct net_device *dev);
615 void dev_init_scheduler(struct net_device *dev);
616 void dev_shutdown(struct net_device *dev);
617 void dev_activate(struct net_device *dev);
618 void dev_deactivate(struct net_device *dev);
619 void dev_deactivate_many(struct list_head *head);
620 struct Qdisc *dev_graft_qdisc(struct netdev_queue *dev_queue,
621                               struct Qdisc *qdisc);
622 void qdisc_reset(struct Qdisc *qdisc);
623 void qdisc_put(struct Qdisc *qdisc);
624 void qdisc_put_unlocked(struct Qdisc *qdisc);
625 void qdisc_tree_reduce_backlog(struct Qdisc *qdisc, int n, int len);
626 #ifdef CONFIG_NET_SCHED
627 int qdisc_offload_dump_helper(struct Qdisc *q, enum tc_setup_type type,
628                               void *type_data);
629 void qdisc_offload_graft_helper(struct net_device *dev, struct Qdisc *sch,
630                                 struct Qdisc *new, struct Qdisc *old,
631                                 enum tc_setup_type type, void *type_data,
632                                 struct netlink_ext_ack *extack);
633 #else
634 static inline int
635 qdisc_offload_dump_helper(struct Qdisc *q, enum tc_setup_type type,
636                           void *type_data)
637 {
638         q->flags &= ~TCQ_F_OFFLOADED;
639         return 0;
640 }
641
642 static inline void
643 qdisc_offload_graft_helper(struct net_device *dev, struct Qdisc *sch,
644                            struct Qdisc *new, struct Qdisc *old,
645                            enum tc_setup_type type, void *type_data,
646                            struct netlink_ext_ack *extack)
647 {
648 }
649 #endif
650 struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue,
651                           const struct Qdisc_ops *ops,
652                           struct netlink_ext_ack *extack);
653 void qdisc_free(struct Qdisc *qdisc);
654 struct Qdisc *qdisc_create_dflt(struct netdev_queue *dev_queue,
655                                 const struct Qdisc_ops *ops, u32 parentid,
656                                 struct netlink_ext_ack *extack);
657 void __qdisc_calculate_pkt_len(struct sk_buff *skb,
658                                const struct qdisc_size_table *stab);
659 int skb_do_redirect(struct sk_buff *);
660
661 static inline void skb_reset_tc(struct sk_buff *skb)
662 {
663 #ifdef CONFIG_NET_CLS_ACT
664         skb->tc_redirected = 0;
665 #endif
666 }
667
668 static inline bool skb_is_tc_redirected(const struct sk_buff *skb)
669 {
670 #ifdef CONFIG_NET_CLS_ACT
671         return skb->tc_redirected;
672 #else
673         return false;
674 #endif
675 }
676
677 static inline bool skb_at_tc_ingress(const struct sk_buff *skb)
678 {
679 #ifdef CONFIG_NET_CLS_ACT
680         return skb->tc_at_ingress;
681 #else
682         return false;
683 #endif
684 }
685
686 static inline bool skb_skip_tc_classify(struct sk_buff *skb)
687 {
688 #ifdef CONFIG_NET_CLS_ACT
689         if (skb->tc_skip_classify) {
690                 skb->tc_skip_classify = 0;
691                 return true;
692         }
693 #endif
694         return false;
695 }
696
697 /* Reset all TX qdiscs greater than index of a device.  */
698 static inline void qdisc_reset_all_tx_gt(struct net_device *dev, unsigned int i)
699 {
700         struct Qdisc *qdisc;
701
702         for (; i < dev->num_tx_queues; i++) {
703                 qdisc = rtnl_dereference(netdev_get_tx_queue(dev, i)->qdisc);
704                 if (qdisc) {
705                         spin_lock_bh(qdisc_lock(qdisc));
706                         qdisc_reset(qdisc);
707                         spin_unlock_bh(qdisc_lock(qdisc));
708                 }
709         }
710 }
711
712 static inline void qdisc_reset_all_tx(struct net_device *dev)
713 {
714         qdisc_reset_all_tx_gt(dev, 0);
715 }
716
717 /* Are all TX queues of the device empty?  */
718 static inline bool qdisc_all_tx_empty(const struct net_device *dev)
719 {
720         unsigned int i;
721
722         rcu_read_lock();
723         for (i = 0; i < dev->num_tx_queues; i++) {
724                 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
725                 const struct Qdisc *q = rcu_dereference(txq->qdisc);
726
727                 if (q->q.qlen) {
728                         rcu_read_unlock();
729                         return false;
730                 }
731         }
732         rcu_read_unlock();
733         return true;
734 }
735
736 /* Are any of the TX qdiscs changing?  */
737 static inline bool qdisc_tx_changing(const struct net_device *dev)
738 {
739         unsigned int i;
740
741         for (i = 0; i < dev->num_tx_queues; i++) {
742                 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
743                 if (rcu_access_pointer(txq->qdisc) != txq->qdisc_sleeping)
744                         return true;
745         }
746         return false;
747 }
748
749 /* Is the device using the noop qdisc on all queues?  */
750 static inline bool qdisc_tx_is_noop(const struct net_device *dev)
751 {
752         unsigned int i;
753
754         for (i = 0; i < dev->num_tx_queues; i++) {
755                 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
756                 if (rcu_access_pointer(txq->qdisc) != &noop_qdisc)
757                         return false;
758         }
759         return true;
760 }
761
762 static inline unsigned int qdisc_pkt_len(const struct sk_buff *skb)
763 {
764         return qdisc_skb_cb(skb)->pkt_len;
765 }
766
767 /* additional qdisc xmit flags (NET_XMIT_MASK in linux/netdevice.h) */
768 enum net_xmit_qdisc_t {
769         __NET_XMIT_STOLEN = 0x00010000,
770         __NET_XMIT_BYPASS = 0x00020000,
771 };
772
773 #ifdef CONFIG_NET_CLS_ACT
774 #define net_xmit_drop_count(e)  ((e) & __NET_XMIT_STOLEN ? 0 : 1)
775 #else
776 #define net_xmit_drop_count(e)  (1)
777 #endif
778
779 static inline void qdisc_calculate_pkt_len(struct sk_buff *skb,
780                                            const struct Qdisc *sch)
781 {
782 #ifdef CONFIG_NET_SCHED
783         struct qdisc_size_table *stab = rcu_dereference_bh(sch->stab);
784
785         if (stab)
786                 __qdisc_calculate_pkt_len(skb, stab);
787 #endif
788 }
789
790 static inline int qdisc_enqueue(struct sk_buff *skb, struct Qdisc *sch,
791                                 struct sk_buff **to_free)
792 {
793         qdisc_calculate_pkt_len(skb, sch);
794         return sch->enqueue(skb, sch, to_free);
795 }
796
797 static inline bool qdisc_is_percpu_stats(const struct Qdisc *q)
798 {
799         return q->flags & TCQ_F_CPUSTATS;
800 }
801
802 static inline void _bstats_update(struct gnet_stats_basic_packed *bstats,
803                                   __u64 bytes, __u32 packets)
804 {
805         bstats->bytes += bytes;
806         bstats->packets += packets;
807 }
808
809 static inline void bstats_update(struct gnet_stats_basic_packed *bstats,
810                                  const struct sk_buff *skb)
811 {
812         _bstats_update(bstats,
813                        qdisc_pkt_len(skb),
814                        skb_is_gso(skb) ? skb_shinfo(skb)->gso_segs : 1);
815 }
816
817 static inline void _bstats_cpu_update(struct gnet_stats_basic_cpu *bstats,
818                                       __u64 bytes, __u32 packets)
819 {
820         u64_stats_update_begin(&bstats->syncp);
821         _bstats_update(&bstats->bstats, bytes, packets);
822         u64_stats_update_end(&bstats->syncp);
823 }
824
825 static inline void bstats_cpu_update(struct gnet_stats_basic_cpu *bstats,
826                                      const struct sk_buff *skb)
827 {
828         u64_stats_update_begin(&bstats->syncp);
829         bstats_update(&bstats->bstats, skb);
830         u64_stats_update_end(&bstats->syncp);
831 }
832
833 static inline void qdisc_bstats_cpu_update(struct Qdisc *sch,
834                                            const struct sk_buff *skb)
835 {
836         bstats_cpu_update(this_cpu_ptr(sch->cpu_bstats), skb);
837 }
838
839 static inline void qdisc_bstats_update(struct Qdisc *sch,
840                                        const struct sk_buff *skb)
841 {
842         bstats_update(&sch->bstats, skb);
843 }
844
845 static inline void qdisc_qstats_backlog_dec(struct Qdisc *sch,
846                                             const struct sk_buff *skb)
847 {
848         sch->qstats.backlog -= qdisc_pkt_len(skb);
849 }
850
851 static inline void qdisc_qstats_cpu_backlog_dec(struct Qdisc *sch,
852                                                 const struct sk_buff *skb)
853 {
854         this_cpu_sub(sch->cpu_qstats->backlog, qdisc_pkt_len(skb));
855 }
856
857 static inline void qdisc_qstats_backlog_inc(struct Qdisc *sch,
858                                             const struct sk_buff *skb)
859 {
860         sch->qstats.backlog += qdisc_pkt_len(skb);
861 }
862
863 static inline void qdisc_qstats_cpu_backlog_inc(struct Qdisc *sch,
864                                                 const struct sk_buff *skb)
865 {
866         this_cpu_add(sch->cpu_qstats->backlog, qdisc_pkt_len(skb));
867 }
868
869 static inline void qdisc_qstats_cpu_qlen_inc(struct Qdisc *sch)
870 {
871         this_cpu_inc(sch->cpu_qstats->qlen);
872 }
873
874 static inline void qdisc_qstats_cpu_qlen_dec(struct Qdisc *sch)
875 {
876         this_cpu_dec(sch->cpu_qstats->qlen);
877 }
878
879 static inline void qdisc_qstats_cpu_requeues_inc(struct Qdisc *sch)
880 {
881         this_cpu_inc(sch->cpu_qstats->requeues);
882 }
883
884 static inline void __qdisc_qstats_drop(struct Qdisc *sch, int count)
885 {
886         sch->qstats.drops += count;
887 }
888
889 static inline void qstats_drop_inc(struct gnet_stats_queue *qstats)
890 {
891         qstats->drops++;
892 }
893
894 static inline void qstats_overlimit_inc(struct gnet_stats_queue *qstats)
895 {
896         qstats->overlimits++;
897 }
898
899 static inline void qdisc_qstats_drop(struct Qdisc *sch)
900 {
901         qstats_drop_inc(&sch->qstats);
902 }
903
904 static inline void qdisc_qstats_cpu_drop(struct Qdisc *sch)
905 {
906         this_cpu_inc(sch->cpu_qstats->drops);
907 }
908
909 static inline void qdisc_qstats_overlimit(struct Qdisc *sch)
910 {
911         sch->qstats.overlimits++;
912 }
913
914 static inline void qdisc_skb_head_init(struct qdisc_skb_head *qh)
915 {
916         qh->head = NULL;
917         qh->tail = NULL;
918         qh->qlen = 0;
919 }
920
921 static inline void __qdisc_enqueue_tail(struct sk_buff *skb,
922                                         struct qdisc_skb_head *qh)
923 {
924         struct sk_buff *last = qh->tail;
925
926         if (last) {
927                 skb->next = NULL;
928                 last->next = skb;
929                 qh->tail = skb;
930         } else {
931                 qh->tail = skb;
932                 qh->head = skb;
933         }
934         qh->qlen++;
935 }
936
937 static inline int qdisc_enqueue_tail(struct sk_buff *skb, struct Qdisc *sch)
938 {
939         __qdisc_enqueue_tail(skb, &sch->q);
940         qdisc_qstats_backlog_inc(sch, skb);
941         return NET_XMIT_SUCCESS;
942 }
943
944 static inline void __qdisc_enqueue_head(struct sk_buff *skb,
945                                         struct qdisc_skb_head *qh)
946 {
947         skb->next = qh->head;
948
949         if (!qh->head)
950                 qh->tail = skb;
951         qh->head = skb;
952         qh->qlen++;
953 }
954
955 static inline struct sk_buff *__qdisc_dequeue_head(struct qdisc_skb_head *qh)
956 {
957         struct sk_buff *skb = qh->head;
958
959         if (likely(skb != NULL)) {
960                 qh->head = skb->next;
961                 qh->qlen--;
962                 if (qh->head == NULL)
963                         qh->tail = NULL;
964                 skb->next = NULL;
965         }
966
967         return skb;
968 }
969
970 static inline struct sk_buff *qdisc_dequeue_head(struct Qdisc *sch)
971 {
972         struct sk_buff *skb = __qdisc_dequeue_head(&sch->q);
973
974         if (likely(skb != NULL)) {
975                 qdisc_qstats_backlog_dec(sch, skb);
976                 qdisc_bstats_update(sch, skb);
977         }
978
979         return skb;
980 }
981
982 /* Instead of calling kfree_skb() while root qdisc lock is held,
983  * queue the skb for future freeing at end of __dev_xmit_skb()
984  */
985 static inline void __qdisc_drop(struct sk_buff *skb, struct sk_buff **to_free)
986 {
987         skb->next = *to_free;
988         *to_free = skb;
989 }
990
991 static inline void __qdisc_drop_all(struct sk_buff *skb,
992                                     struct sk_buff **to_free)
993 {
994         if (skb->prev)
995                 skb->prev->next = *to_free;
996         else
997                 skb->next = *to_free;
998         *to_free = skb;
999 }
1000
1001 static inline unsigned int __qdisc_queue_drop_head(struct Qdisc *sch,
1002                                                    struct qdisc_skb_head *qh,
1003                                                    struct sk_buff **to_free)
1004 {
1005         struct sk_buff *skb = __qdisc_dequeue_head(qh);
1006
1007         if (likely(skb != NULL)) {
1008                 unsigned int len = qdisc_pkt_len(skb);
1009
1010                 qdisc_qstats_backlog_dec(sch, skb);
1011                 __qdisc_drop(skb, to_free);
1012                 return len;
1013         }
1014
1015         return 0;
1016 }
1017
1018 static inline unsigned int qdisc_queue_drop_head(struct Qdisc *sch,
1019                                                  struct sk_buff **to_free)
1020 {
1021         return __qdisc_queue_drop_head(sch, &sch->q, to_free);
1022 }
1023
1024 static inline struct sk_buff *qdisc_peek_head(struct Qdisc *sch)
1025 {
1026         const struct qdisc_skb_head *qh = &sch->q;
1027
1028         return qh->head;
1029 }
1030
1031 /* generic pseudo peek method for non-work-conserving qdisc */
1032 static inline struct sk_buff *qdisc_peek_dequeued(struct Qdisc *sch)
1033 {
1034         struct sk_buff *skb = skb_peek(&sch->gso_skb);
1035
1036         /* we can reuse ->gso_skb because peek isn't called for root qdiscs */
1037         if (!skb) {
1038                 skb = sch->dequeue(sch);
1039
1040                 if (skb) {
1041                         __skb_queue_head(&sch->gso_skb, skb);
1042                         /* it's still part of the queue */
1043                         qdisc_qstats_backlog_inc(sch, skb);
1044                         sch->q.qlen++;
1045                 }
1046         }
1047
1048         return skb;
1049 }
1050
1051 /* use instead of qdisc->dequeue() for all qdiscs queried with ->peek() */
1052 static inline struct sk_buff *qdisc_dequeue_peeked(struct Qdisc *sch)
1053 {
1054         struct sk_buff *skb = skb_peek(&sch->gso_skb);
1055
1056         if (skb) {
1057                 skb = __skb_dequeue(&sch->gso_skb);
1058                 qdisc_qstats_backlog_dec(sch, skb);
1059                 sch->q.qlen--;
1060         } else {
1061                 skb = sch->dequeue(sch);
1062         }
1063
1064         return skb;
1065 }
1066
1067 static inline void __qdisc_reset_queue(struct qdisc_skb_head *qh)
1068 {
1069         /*
1070          * We do not know the backlog in bytes of this list, it
1071          * is up to the caller to correct it
1072          */
1073         ASSERT_RTNL();
1074         if (qh->qlen) {
1075                 rtnl_kfree_skbs(qh->head, qh->tail);
1076
1077                 qh->head = NULL;
1078                 qh->tail = NULL;
1079                 qh->qlen = 0;
1080         }
1081 }
1082
1083 static inline void qdisc_reset_queue(struct Qdisc *sch)
1084 {
1085         __qdisc_reset_queue(&sch->q);
1086         sch->qstats.backlog = 0;
1087 }
1088
1089 static inline struct Qdisc *qdisc_replace(struct Qdisc *sch, struct Qdisc *new,
1090                                           struct Qdisc **pold)
1091 {
1092         struct Qdisc *old;
1093
1094         sch_tree_lock(sch);
1095         old = *pold;
1096         *pold = new;
1097         if (old != NULL) {
1098                 unsigned int qlen = old->q.qlen;
1099                 unsigned int backlog = old->qstats.backlog;
1100
1101                 qdisc_reset(old);
1102                 qdisc_tree_reduce_backlog(old, qlen, backlog);
1103         }
1104         sch_tree_unlock(sch);
1105
1106         return old;
1107 }
1108
1109 static inline void rtnl_qdisc_drop(struct sk_buff *skb, struct Qdisc *sch)
1110 {
1111         rtnl_kfree_skbs(skb, skb);
1112         qdisc_qstats_drop(sch);
1113 }
1114
1115 static inline int qdisc_drop_cpu(struct sk_buff *skb, struct Qdisc *sch,
1116                                  struct sk_buff **to_free)
1117 {
1118         __qdisc_drop(skb, to_free);
1119         qdisc_qstats_cpu_drop(sch);
1120
1121         return NET_XMIT_DROP;
1122 }
1123
1124 static inline int qdisc_drop(struct sk_buff *skb, struct Qdisc *sch,
1125                              struct sk_buff **to_free)
1126 {
1127         __qdisc_drop(skb, to_free);
1128         qdisc_qstats_drop(sch);
1129
1130         return NET_XMIT_DROP;
1131 }
1132
1133 static inline int qdisc_drop_all(struct sk_buff *skb, struct Qdisc *sch,
1134                                  struct sk_buff **to_free)
1135 {
1136         __qdisc_drop_all(skb, to_free);
1137         qdisc_qstats_drop(sch);
1138
1139         return NET_XMIT_DROP;
1140 }
1141
1142 /* Length to Time (L2T) lookup in a qdisc_rate_table, to determine how
1143    long it will take to send a packet given its size.
1144  */
1145 static inline u32 qdisc_l2t(struct qdisc_rate_table* rtab, unsigned int pktlen)
1146 {
1147         int slot = pktlen + rtab->rate.cell_align + rtab->rate.overhead;
1148         if (slot < 0)
1149                 slot = 0;
1150         slot >>= rtab->rate.cell_log;
1151         if (slot > 255)
1152                 return rtab->data[255]*(slot >> 8) + rtab->data[slot & 0xFF];
1153         return rtab->data[slot];
1154 }
1155
1156 struct psched_ratecfg {
1157         u64     rate_bytes_ps; /* bytes per second */
1158         u32     mult;
1159         u16     overhead;
1160         u8      linklayer;
1161         u8      shift;
1162 };
1163
1164 static inline u64 psched_l2t_ns(const struct psched_ratecfg *r,
1165                                 unsigned int len)
1166 {
1167         len += r->overhead;
1168
1169         if (unlikely(r->linklayer == TC_LINKLAYER_ATM))
1170                 return ((u64)(DIV_ROUND_UP(len,48)*53) * r->mult) >> r->shift;
1171
1172         return ((u64)len * r->mult) >> r->shift;
1173 }
1174
1175 void psched_ratecfg_precompute(struct psched_ratecfg *r,
1176                                const struct tc_ratespec *conf,
1177                                u64 rate64);
1178
1179 static inline void psched_ratecfg_getrate(struct tc_ratespec *res,
1180                                           const struct psched_ratecfg *r)
1181 {
1182         memset(res, 0, sizeof(*res));
1183
1184         /* legacy struct tc_ratespec has a 32bit @rate field
1185          * Qdisc using 64bit rate should add new attributes
1186          * in order to maintain compatibility.
1187          */
1188         res->rate = min_t(u64, r->rate_bytes_ps, ~0U);
1189
1190         res->overhead = r->overhead;
1191         res->linklayer = (r->linklayer & TC_LINKLAYER_MASK);
1192 }
1193
1194 /* Mini Qdisc serves for specific needs of ingress/clsact Qdisc.
1195  * The fast path only needs to access filter list and to update stats
1196  */
1197 struct mini_Qdisc {
1198         struct tcf_proto *filter_list;
1199         struct gnet_stats_basic_cpu __percpu *cpu_bstats;
1200         struct gnet_stats_queue __percpu *cpu_qstats;
1201         struct rcu_head rcu;
1202 };
1203
1204 static inline void mini_qdisc_bstats_cpu_update(struct mini_Qdisc *miniq,
1205                                                 const struct sk_buff *skb)
1206 {
1207         bstats_cpu_update(this_cpu_ptr(miniq->cpu_bstats), skb);
1208 }
1209
1210 static inline void mini_qdisc_qstats_cpu_drop(struct mini_Qdisc *miniq)
1211 {
1212         this_cpu_inc(miniq->cpu_qstats->drops);
1213 }
1214
1215 struct mini_Qdisc_pair {
1216         struct mini_Qdisc miniq1;
1217         struct mini_Qdisc miniq2;
1218         struct mini_Qdisc __rcu **p_miniq;
1219 };
1220
1221 void mini_qdisc_pair_swap(struct mini_Qdisc_pair *miniqp,
1222                           struct tcf_proto *tp_head);
1223 void mini_qdisc_pair_init(struct mini_Qdisc_pair *miniqp, struct Qdisc *qdisc,
1224                           struct mini_Qdisc __rcu **p_miniq);
1225
1226 static inline void skb_tc_reinsert(struct sk_buff *skb, struct tcf_result *res)
1227 {
1228         struct gnet_stats_queue *stats = res->qstats;
1229         int ret;
1230
1231         if (res->ingress)
1232                 ret = netif_receive_skb(skb);
1233         else
1234                 ret = dev_queue_xmit(skb);
1235         if (ret && stats)
1236                 qstats_overlimit_inc(res->qstats);
1237 }
1238
1239 #endif