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