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