Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | #ifndef __NET_SCHED_GENERIC_H |
| 3 | #define __NET_SCHED_GENERIC_H |
| 4 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | #include <linux/netdevice.h> |
| 6 | #include <linux/types.h> |
| 7 | #include <linux/rcupdate.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | #include <linux/pkt_sched.h> |
| 9 | #include <linux/pkt_cls.h> |
John Fastabend | 22e0f8b | 2014-09-28 11:52:56 -0700 | [diff] [blame] | 10 | #include <linux/percpu.h> |
Jesper Dangaard Brouer | 5772e9a | 2014-10-01 22:35:59 +0200 | [diff] [blame] | 11 | #include <linux/dynamic_queue_limits.h> |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 12 | #include <linux/list.h> |
Reshetova, Elena | 7b93640 | 2017-07-04 15:53:07 +0300 | [diff] [blame] | 13 | #include <linux/refcount.h> |
Cong Wang | 7aa0045 | 2017-10-26 18:24:28 -0700 | [diff] [blame] | 14 | #include <linux/workqueue.h> |
Vlad Buslov | c266f64 | 2019-02-11 10:55:32 +0200 | [diff] [blame] | 15 | #include <linux/mutex.h> |
Vlad Buslov | 4f8116c | 2019-08-26 16:44:57 +0300 | [diff] [blame] | 16 | #include <linux/rwsem.h> |
Vlad Buslov | 97394be | 2019-08-26 16:44:58 +0300 | [diff] [blame] | 17 | #include <linux/atomic.h> |
John Hurley | 59eb87c | 2019-11-02 14:17:47 +0000 | [diff] [blame] | 18 | #include <linux/hashtable.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <net/gen_stats.h> |
Thomas Graf | be577dd | 2007-03-22 11:55:50 -0700 | [diff] [blame] | 20 | #include <net/rtnetlink.h> |
Pablo Neira Ayuso | a732331 | 2019-07-19 18:20:15 +0200 | [diff] [blame] | 21 | #include <net/flow_offload.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
| 23 | struct Qdisc_ops; |
| 24 | struct qdisc_walker; |
| 25 | struct tcf_walker; |
| 26 | struct module; |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 27 | struct bpf_flow_keys; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
Eric Dumazet | fd2c3ef | 2009-11-03 03:26:03 +0000 | [diff] [blame] | 29 | struct qdisc_rate_table { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | struct tc_ratespec rate; |
| 31 | u32 data[256]; |
| 32 | struct qdisc_rate_table *next; |
| 33 | int refcnt; |
| 34 | }; |
| 35 | |
Eric Dumazet | fd2c3ef | 2009-11-03 03:26:03 +0000 | [diff] [blame] | 36 | enum qdisc_state_t { |
David S. Miller | 37437bb | 2008-07-16 02:15:04 -0700 | [diff] [blame] | 37 | __QDISC_STATE_SCHED, |
David S. Miller | a9312ae | 2008-08-17 21:51:03 -0700 | [diff] [blame] | 38 | __QDISC_STATE_DEACTIVATED, |
Yunsheng Lin | a90c57f | 2021-05-14 11:16:59 +0800 | [diff] [blame] | 39 | __QDISC_STATE_MISSED, |
Yunsheng Lin | c4fef01 | 2021-06-22 14:49:56 +0800 | [diff] [blame] | 40 | __QDISC_STATE_DRAINING, |
Eric Dumazet | 97604c6 | 2021-10-18 17:34:02 -0700 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | enum qdisc_state2_t { |
Ahmed S. Darwish | 29cbcd8 | 2021-10-16 10:49:10 +0200 | [diff] [blame] | 44 | /* Only for !TCQ_F_NOLOCK qdisc. Never access it directly. |
| 45 | * Use qdisc_run_begin/end() or qdisc_is_running() instead. |
| 46 | */ |
Eric Dumazet | 97604c6 | 2021-10-18 17:34:02 -0700 | [diff] [blame] | 47 | __QDISC_STATE2_RUNNING, |
David S. Miller | e2627c8 | 2008-07-16 00:56:32 -0700 | [diff] [blame] | 48 | }; |
| 49 | |
Yunsheng Lin | c4fef01 | 2021-06-22 14:49:56 +0800 | [diff] [blame] | 50 | #define QDISC_STATE_MISSED BIT(__QDISC_STATE_MISSED) |
| 51 | #define QDISC_STATE_DRAINING BIT(__QDISC_STATE_DRAINING) |
| 52 | |
| 53 | #define QDISC_STATE_NON_EMPTY (QDISC_STATE_MISSED | \ |
| 54 | QDISC_STATE_DRAINING) |
| 55 | |
Jussi Kivilinna | 175f9c1 | 2008-07-20 00:08:47 -0700 | [diff] [blame] | 56 | struct qdisc_size_table { |
Eric Dumazet | a2da570 | 2011-01-20 03:48:19 +0000 | [diff] [blame] | 57 | struct rcu_head rcu; |
Jussi Kivilinna | 175f9c1 | 2008-07-20 00:08:47 -0700 | [diff] [blame] | 58 | struct list_head list; |
| 59 | struct tc_sizespec szopts; |
| 60 | int refcnt; |
| 61 | u16 data[]; |
| 62 | }; |
| 63 | |
Florian Westphal | 48da34b | 2016-09-18 00:57:34 +0200 | [diff] [blame] | 64 | /* similar to sk_buff_head, but skb->prev pointer is undefined. */ |
| 65 | struct qdisc_skb_head { |
| 66 | struct sk_buff *head; |
| 67 | struct sk_buff *tail; |
Paolo Abeni | 73eb628 | 2019-04-10 14:32:41 +0200 | [diff] [blame] | 68 | __u32 qlen; |
Florian Westphal | 48da34b | 2016-09-18 00:57:34 +0200 | [diff] [blame] | 69 | spinlock_t lock; |
| 70 | }; |
| 71 | |
Eric Dumazet | fd2c3ef | 2009-11-03 03:26:03 +0000 | [diff] [blame] | 72 | struct Qdisc { |
Eric Dumazet | 520ac30 | 2016-06-21 23:16:49 -0700 | [diff] [blame] | 73 | int (*enqueue)(struct sk_buff *skb, |
| 74 | struct Qdisc *sch, |
| 75 | struct sk_buff **to_free); |
| 76 | struct sk_buff * (*dequeue)(struct Qdisc *sch); |
Eric Dumazet | 05bdd2f | 2011-10-20 17:45:43 -0400 | [diff] [blame] | 77 | unsigned int flags; |
Jarek Poplawski | b00355d | 2009-02-01 01:12:42 -0800 | [diff] [blame] | 78 | #define TCQ_F_BUILTIN 1 |
Eric Dumazet | fd245a4 | 2011-01-20 05:27:16 +0000 | [diff] [blame] | 79 | #define TCQ_F_INGRESS 2 |
| 80 | #define TCQ_F_CAN_BYPASS 4 |
| 81 | #define TCQ_F_MQROOT 8 |
Eric Dumazet | 1abbe13 | 2012-12-11 15:54:33 +0000 | [diff] [blame] | 82 | #define TCQ_F_ONETXQUEUE 0x10 /* dequeue_skb() can assume all skbs are for |
| 83 | * q->dev_queue : It can test |
| 84 | * netif_xmit_frozen_or_stopped() before |
| 85 | * dequeueing next packet. |
| 86 | * Its true for MQ/MQPRIO slaves, or non |
| 87 | * multiqueue device. |
| 88 | */ |
Jarek Poplawski | b00355d | 2009-02-01 01:12:42 -0800 | [diff] [blame] | 89 | #define TCQ_F_WARN_NONWC (1 << 16) |
John Fastabend | 22e0f8b | 2014-09-28 11:52:56 -0700 | [diff] [blame] | 90 | #define TCQ_F_CPUSTATS 0x20 /* run using percpu statistics */ |
Eric Dumazet | 4eaf3b8 | 2015-12-01 20:08:51 -0800 | [diff] [blame] | 91 | #define TCQ_F_NOPARENT 0x40 /* root of its hierarchy : |
| 92 | * qdisc_tree_decrease_qlen() should stop. |
| 93 | */ |
Jiri Kosina | 49b4997 | 2017-03-08 16:03:32 +0100 | [diff] [blame] | 94 | #define TCQ_F_INVISIBLE 0x80 /* invisible by default in dump */ |
John Fastabend | 6b3ba91 | 2017-12-07 09:54:25 -0800 | [diff] [blame] | 95 | #define TCQ_F_NOLOCK 0x100 /* qdisc does not require locking */ |
Yuval Mintz | 7a4fa29 | 2017-12-14 15:54:29 +0200 | [diff] [blame] | 96 | #define TCQ_F_OFFLOADED 0x200 /* qdisc is offloaded to HW */ |
Eric Dumazet | 45203a3 | 2013-06-06 08:43:22 -0700 | [diff] [blame] | 97 | u32 limit; |
Eric Dumazet | 05bdd2f | 2011-10-20 17:45:43 -0400 | [diff] [blame] | 98 | const struct Qdisc_ops *ops; |
Eric Dumazet | a2da570 | 2011-01-20 03:48:19 +0000 | [diff] [blame] | 99 | struct qdisc_size_table __rcu *stab; |
Jiri Kosina | 59cc1f6 | 2016-08-10 11:05:15 +0200 | [diff] [blame] | 100 | struct hlist_node hash; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | u32 handle; |
| 102 | u32 parent; |
David S. Miller | 72b25a9 | 2008-07-18 20:54:17 -0700 | [diff] [blame] | 103 | |
Eric Dumazet | 5e140df | 2009-03-20 01:33:32 -0700 | [diff] [blame] | 104 | struct netdev_queue *dev_queue; |
Eric Dumazet | 5e140df | 2009-03-20 01:33:32 -0700 | [diff] [blame] | 105 | |
Eric Dumazet | 1c0d32f | 2016-12-04 09:48:16 -0800 | [diff] [blame] | 106 | struct net_rate_estimator __rcu *rate_est; |
Ahmed S. Darwish | 50dc9a8 | 2021-10-16 10:49:09 +0200 | [diff] [blame] | 107 | struct gnet_stats_basic_sync __percpu *cpu_bstats; |
Eric Dumazet | 0d32ef8 | 2015-01-29 17:30:12 -0800 | [diff] [blame] | 108 | struct gnet_stats_queue __percpu *cpu_qstats; |
Eric Dumazet | 846e463 | 2020-10-07 09:51:11 -0700 | [diff] [blame] | 109 | int pad; |
Paolo Abeni | e9be0e9 | 2018-05-25 16:28:44 +0200 | [diff] [blame] | 110 | refcount_t refcnt; |
Eric Dumazet | 0d32ef8 | 2015-01-29 17:30:12 -0800 | [diff] [blame] | 111 | |
Eric Dumazet | 5e140df | 2009-03-20 01:33:32 -0700 | [diff] [blame] | 112 | /* |
| 113 | * For performance sake on SMP, we put highly modified fields at the end |
| 114 | */ |
John Fastabend | a53851e | 2017-12-07 09:55:45 -0800 | [diff] [blame] | 115 | struct sk_buff_head gso_skb ____cacheline_aligned_in_smp; |
Florian Westphal | 48da34b | 2016-09-18 00:57:34 +0200 | [diff] [blame] | 116 | struct qdisc_skb_head q; |
Ahmed S. Darwish | 50dc9a8 | 2021-10-16 10:49:09 +0200 | [diff] [blame] | 117 | struct gnet_stats_basic_sync bstats; |
Eric Dumazet | 0d32ef8 | 2015-01-29 17:30:12 -0800 | [diff] [blame] | 118 | struct gnet_stats_queue qstats; |
Eric Dumazet | 4d202a0 | 2016-06-21 23:16:52 -0700 | [diff] [blame] | 119 | unsigned long state; |
Eric Dumazet | 97604c6 | 2021-10-18 17:34:02 -0700 | [diff] [blame] | 120 | unsigned long state2; /* must be written under qdisc spinlock */ |
Eric Dumazet | 4d202a0 | 2016-06-21 23:16:52 -0700 | [diff] [blame] | 121 | struct Qdisc *next_sched; |
John Fastabend | 70e57d5 | 2017-12-07 09:56:23 -0800 | [diff] [blame] | 122 | struct sk_buff_head skb_bad_txq; |
Eric Dumazet | 45203a3 | 2013-06-06 08:43:22 -0700 | [diff] [blame] | 123 | |
| 124 | spinlock_t busylock ____cacheline_aligned_in_smp; |
Paolo Abeni | 96009c7 | 2018-05-15 16:24:36 +0200 | [diff] [blame] | 125 | spinlock_t seqlock; |
Paolo Abeni | 28cff53 | 2019-03-22 16:01:55 +0100 | [diff] [blame] | 126 | |
Vlad Buslov | 3a7d0d0 | 2018-09-24 19:22:51 +0300 | [diff] [blame] | 127 | struct rcu_head rcu; |
Eric Dumazet | 606509f | 2021-12-04 20:22:13 -0800 | [diff] [blame] | 128 | netdevice_tracker dev_tracker; |
Eric Dumazet | 846e463 | 2020-10-07 09:51:11 -0700 | [diff] [blame] | 129 | /* private data */ |
| 130 | long privdata[] ____cacheline_aligned; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | }; |
| 132 | |
Eric Dumazet | 551143d | 2017-08-24 21:12:28 -0700 | [diff] [blame] | 133 | static inline void qdisc_refcount_inc(struct Qdisc *qdisc) |
| 134 | { |
| 135 | if (qdisc->flags & TCQ_F_BUILTIN) |
| 136 | return; |
| 137 | refcount_inc(&qdisc->refcnt); |
| 138 | } |
| 139 | |
Vlad Buslov | 9d7e82c | 2018-09-24 19:22:52 +0300 | [diff] [blame] | 140 | /* Intended to be used by unlocked users, when concurrent qdisc release is |
| 141 | * possible. |
| 142 | */ |
| 143 | |
| 144 | static inline struct Qdisc *qdisc_refcount_inc_nz(struct Qdisc *qdisc) |
| 145 | { |
| 146 | if (qdisc->flags & TCQ_F_BUILTIN) |
| 147 | return qdisc; |
| 148 | if (refcount_inc_not_zero(&qdisc->refcnt)) |
| 149 | return qdisc; |
| 150 | return NULL; |
| 151 | } |
| 152 | |
Ahmed S. Darwish | 29cbcd8 | 2021-10-16 10:49:10 +0200 | [diff] [blame] | 153 | /* For !TCQ_F_NOLOCK qdisc: callers must either call this within a qdisc |
| 154 | * root_lock section, or provide their own memory barriers -- ordering |
| 155 | * against qdisc_run_begin/end() atomic bit operations. |
| 156 | */ |
Paolo Abeni | 96009c7 | 2018-05-15 16:24:36 +0200 | [diff] [blame] | 157 | static inline bool qdisc_is_running(struct Qdisc *qdisc) |
Eric Dumazet | bc135b2 | 2010-06-02 03:23:51 -0700 | [diff] [blame] | 158 | { |
Paolo Abeni | 32f7b44 | 2018-05-15 10:50:31 +0200 | [diff] [blame] | 159 | if (qdisc->flags & TCQ_F_NOLOCK) |
Paolo Abeni | 96009c7 | 2018-05-15 16:24:36 +0200 | [diff] [blame] | 160 | return spin_is_locked(&qdisc->seqlock); |
Eric Dumazet | 97604c6 | 2021-10-18 17:34:02 -0700 | [diff] [blame] | 161 | return test_bit(__QDISC_STATE2_RUNNING, &qdisc->state2); |
Eric Dumazet | bc135b2 | 2010-06-02 03:23:51 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Yunsheng Lin | c4fef01 | 2021-06-22 14:49:56 +0800 | [diff] [blame] | 164 | static inline bool nolock_qdisc_is_empty(const struct Qdisc *qdisc) |
| 165 | { |
| 166 | return !(READ_ONCE(qdisc->state) & QDISC_STATE_NON_EMPTY); |
| 167 | } |
| 168 | |
Paolo Abeni | 9c01c9f | 2019-04-10 14:32:39 +0200 | [diff] [blame] | 169 | static inline bool qdisc_is_percpu_stats(const struct Qdisc *q) |
| 170 | { |
| 171 | return q->flags & TCQ_F_CPUSTATS; |
| 172 | } |
| 173 | |
Paolo Abeni | 28cff53 | 2019-03-22 16:01:55 +0100 | [diff] [blame] | 174 | static inline bool qdisc_is_empty(const struct Qdisc *qdisc) |
| 175 | { |
Paolo Abeni | 9c01c9f | 2019-04-10 14:32:39 +0200 | [diff] [blame] | 176 | if (qdisc_is_percpu_stats(qdisc)) |
Yunsheng Lin | d3e0f57 | 2021-06-22 14:49:57 +0800 | [diff] [blame] | 177 | return nolock_qdisc_is_empty(qdisc); |
Eric Dumazet | 90b2be2 | 2019-11-08 08:45:23 -0800 | [diff] [blame] | 178 | return !READ_ONCE(qdisc->q.qlen); |
Paolo Abeni | 28cff53 | 2019-03-22 16:01:55 +0100 | [diff] [blame] | 179 | } |
| 180 | |
Ahmed S. Darwish | 29cbcd8 | 2021-10-16 10:49:10 +0200 | [diff] [blame] | 181 | /* For !TCQ_F_NOLOCK qdisc, qdisc_run_begin/end() must be invoked with |
| 182 | * the qdisc root lock acquired. |
| 183 | */ |
Eric Dumazet | bc135b2 | 2010-06-02 03:23:51 -0700 | [diff] [blame] | 184 | static inline bool qdisc_run_begin(struct Qdisc *qdisc) |
| 185 | { |
Paolo Abeni | 32f7b44 | 2018-05-15 10:50:31 +0200 | [diff] [blame] | 186 | if (qdisc->flags & TCQ_F_NOLOCK) { |
Yunsheng Lin | a90c57f | 2021-05-14 11:16:59 +0800 | [diff] [blame] | 187 | if (spin_trylock(&qdisc->seqlock)) |
Yunsheng Lin | d3e0f57 | 2021-06-22 14:49:57 +0800 | [diff] [blame] | 188 | return true; |
Yunsheng Lin | a90c57f | 2021-05-14 11:16:59 +0800 | [diff] [blame] | 189 | |
Yunsheng Lin | 89837eb | 2021-06-17 09:04:14 +0800 | [diff] [blame] | 190 | /* Paired with smp_mb__after_atomic() to make sure |
| 191 | * STATE_MISSED checking is synchronized with clearing |
| 192 | * in pfifo_fast_dequeue(). |
| 193 | */ |
| 194 | smp_mb__before_atomic(); |
| 195 | |
Yunsheng Lin | a90c57f | 2021-05-14 11:16:59 +0800 | [diff] [blame] | 196 | /* If the MISSED flag is set, it means other thread has |
| 197 | * set the MISSED flag before second spin_trylock(), so |
| 198 | * we can return false here to avoid multi cpus doing |
| 199 | * the set_bit() and second spin_trylock() concurrently. |
| 200 | */ |
| 201 | if (test_bit(__QDISC_STATE_MISSED, &qdisc->state)) |
| 202 | return false; |
| 203 | |
| 204 | /* Set the MISSED flag before the second spin_trylock(), |
| 205 | * if the second spin_trylock() return false, it means |
| 206 | * other cpu holding the lock will do dequeuing for us |
| 207 | * or it will see the MISSED flag set after releasing |
| 208 | * lock and reschedule the net_tx_action() to do the |
| 209 | * dequeuing. |
| 210 | */ |
| 211 | set_bit(__QDISC_STATE_MISSED, &qdisc->state); |
| 212 | |
Yunsheng Lin | 89837eb | 2021-06-17 09:04:14 +0800 | [diff] [blame] | 213 | /* spin_trylock() only has load-acquire semantic, so use |
| 214 | * smp_mb__after_atomic() to ensure STATE_MISSED is set |
| 215 | * before doing the second spin_trylock(). |
| 216 | */ |
| 217 | smp_mb__after_atomic(); |
| 218 | |
Yunsheng Lin | a90c57f | 2021-05-14 11:16:59 +0800 | [diff] [blame] | 219 | /* Retry again in case other CPU may not see the new flag |
| 220 | * after it releases the lock at the end of qdisc_run_end(). |
| 221 | */ |
Yunsheng Lin | d3e0f57 | 2021-06-22 14:49:57 +0800 | [diff] [blame] | 222 | return spin_trylock(&qdisc->seqlock); |
Paolo Abeni | 32f7b44 | 2018-05-15 10:50:31 +0200 | [diff] [blame] | 223 | } |
Eric Dumazet | 97604c6 | 2021-10-18 17:34:02 -0700 | [diff] [blame] | 224 | return !__test_and_set_bit(__QDISC_STATE2_RUNNING, &qdisc->state2); |
Eric Dumazet | bc135b2 | 2010-06-02 03:23:51 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | static inline void qdisc_run_end(struct Qdisc *qdisc) |
| 228 | { |
Yunsheng Lin | a90c57f | 2021-05-14 11:16:59 +0800 | [diff] [blame] | 229 | if (qdisc->flags & TCQ_F_NOLOCK) { |
Paolo Abeni | 96009c7 | 2018-05-15 16:24:36 +0200 | [diff] [blame] | 230 | spin_unlock(&qdisc->seqlock); |
Yunsheng Lin | a90c57f | 2021-05-14 11:16:59 +0800 | [diff] [blame] | 231 | |
| 232 | if (unlikely(test_bit(__QDISC_STATE_MISSED, |
Yunsheng Lin | c4fef01 | 2021-06-22 14:49:56 +0800 | [diff] [blame] | 233 | &qdisc->state))) |
Yunsheng Lin | a90c57f | 2021-05-14 11:16:59 +0800 | [diff] [blame] | 234 | __netif_schedule(qdisc); |
Yunsheng Lin | dd25296 | 2021-06-22 14:49:55 +0800 | [diff] [blame] | 235 | } else { |
Eric Dumazet | 97604c6 | 2021-10-18 17:34:02 -0700 | [diff] [blame] | 236 | __clear_bit(__QDISC_STATE2_RUNNING, &qdisc->state2); |
Yunsheng Lin | a90c57f | 2021-05-14 11:16:59 +0800 | [diff] [blame] | 237 | } |
Eric Dumazet | fd245a4 | 2011-01-20 05:27:16 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Jesper Dangaard Brouer | 5772e9a | 2014-10-01 22:35:59 +0200 | [diff] [blame] | 240 | static inline bool qdisc_may_bulk(const struct Qdisc *qdisc) |
| 241 | { |
| 242 | return qdisc->flags & TCQ_F_ONETXQUEUE; |
| 243 | } |
| 244 | |
| 245 | static inline int qdisc_avail_bulklimit(const struct netdev_queue *txq) |
| 246 | { |
| 247 | #ifdef CONFIG_BQL |
| 248 | /* Non-BQL migrated drivers will return 0, too. */ |
| 249 | return dql_avail(&txq->dql); |
| 250 | #else |
| 251 | return 0; |
| 252 | #endif |
| 253 | } |
| 254 | |
Eric Dumazet | fd2c3ef | 2009-11-03 03:26:03 +0000 | [diff] [blame] | 255 | struct Qdisc_class_ops { |
Vlad Buslov | dfcd2a2 | 2019-02-11 10:55:46 +0200 | [diff] [blame] | 256 | unsigned int flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | /* Child qdisc manipulation */ |
Jarek Poplawski | 926e61b | 2009-09-15 02:53:07 -0700 | [diff] [blame] | 258 | struct netdev_queue * (*select_queue)(struct Qdisc *, struct tcmsg *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | int (*graft)(struct Qdisc *, unsigned long cl, |
Alexander Aring | 653d6fd | 2017-12-20 12:35:17 -0500 | [diff] [blame] | 260 | struct Qdisc *, struct Qdisc **, |
| 261 | struct netlink_ext_ack *extack); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | struct Qdisc * (*leaf)(struct Qdisc *, unsigned long cl); |
Patrick McHardy | 43effa1 | 2006-11-29 17:35:48 -0800 | [diff] [blame] | 263 | void (*qlen_notify)(struct Qdisc *, unsigned long); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | |
| 265 | /* Class manipulation routines */ |
WANG Cong | 143976c | 2017-08-24 16:51:29 -0700 | [diff] [blame] | 266 | unsigned long (*find)(struct Qdisc *, u32 classid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | int (*change)(struct Qdisc *, u32, u32, |
Alexander Aring | 793d81d | 2017-12-20 12:35:15 -0500 | [diff] [blame] | 268 | struct nlattr **, unsigned long *, |
| 269 | struct netlink_ext_ack *); |
Maxim Mikityanskiy | 4dd78a7 | 2021-01-19 14:08:12 +0200 | [diff] [blame] | 270 | int (*delete)(struct Qdisc *, unsigned long, |
| 271 | struct netlink_ext_ack *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | void (*walk)(struct Qdisc *, struct qdisc_walker * arg); |
| 273 | |
| 274 | /* Filter manipulation */ |
Alexander Aring | 0ac4bd6 | 2017-12-04 18:39:59 -0500 | [diff] [blame] | 275 | struct tcf_block * (*tcf_block)(struct Qdisc *sch, |
Alexander Aring | cbaacc4 | 2017-12-20 12:35:16 -0500 | [diff] [blame] | 276 | unsigned long arg, |
| 277 | struct netlink_ext_ack *extack); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | unsigned long (*bind_tcf)(struct Qdisc *, unsigned long, |
| 279 | u32 classid); |
| 280 | void (*unbind_tcf)(struct Qdisc *, unsigned long); |
| 281 | |
| 282 | /* rtnetlink specific */ |
| 283 | int (*dump)(struct Qdisc *, unsigned long, |
| 284 | struct sk_buff *skb, struct tcmsg*); |
| 285 | int (*dump_stats)(struct Qdisc *, unsigned long, |
| 286 | struct gnet_dump *); |
| 287 | }; |
| 288 | |
Vlad Buslov | dfcd2a2 | 2019-02-11 10:55:46 +0200 | [diff] [blame] | 289 | /* Qdisc_class_ops flag values */ |
| 290 | |
| 291 | /* Implements API that doesn't require rtnl lock */ |
| 292 | enum qdisc_class_ops_flags { |
| 293 | QDISC_CLASS_OPS_DOIT_UNLOCKED = 1, |
| 294 | }; |
| 295 | |
Eric Dumazet | fd2c3ef | 2009-11-03 03:26:03 +0000 | [diff] [blame] | 296 | struct Qdisc_ops { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | struct Qdisc_ops *next; |
Eric Dumazet | 20fea08 | 2007-11-14 01:44:41 -0800 | [diff] [blame] | 298 | const struct Qdisc_class_ops *cl_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | char id[IFNAMSIZ]; |
| 300 | int priv_size; |
John Fastabend | d59f5ff | 2017-12-07 09:55:26 -0800 | [diff] [blame] | 301 | unsigned int static_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | |
Eric Dumazet | 520ac30 | 2016-06-21 23:16:49 -0700 | [diff] [blame] | 303 | int (*enqueue)(struct sk_buff *skb, |
| 304 | struct Qdisc *sch, |
| 305 | struct sk_buff **to_free); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | struct sk_buff * (*dequeue)(struct Qdisc *); |
Jarek Poplawski | 90d841fd | 2008-10-31 00:43:45 -0700 | [diff] [blame] | 307 | struct sk_buff * (*peek)(struct Qdisc *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | |
Alexander Aring | e63d7df | 2017-12-20 12:35:13 -0500 | [diff] [blame] | 309 | int (*init)(struct Qdisc *sch, struct nlattr *arg, |
| 310 | struct netlink_ext_ack *extack); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | void (*reset)(struct Qdisc *); |
| 312 | void (*destroy)(struct Qdisc *); |
Alexander Aring | 0ac4bd6 | 2017-12-04 18:39:59 -0500 | [diff] [blame] | 313 | int (*change)(struct Qdisc *sch, |
Alexander Aring | 2030721 | 2017-12-20 12:35:14 -0500 | [diff] [blame] | 314 | struct nlattr *arg, |
| 315 | struct netlink_ext_ack *extack); |
Alexander Aring | 0ac4bd6 | 2017-12-04 18:39:59 -0500 | [diff] [blame] | 316 | void (*attach)(struct Qdisc *sch); |
Cong Wang | 48bfd55 | 2018-01-25 18:26:23 -0800 | [diff] [blame] | 317 | int (*change_tx_queue_len)(struct Qdisc *, unsigned int); |
Jakub Kicinski | 1e080f1 | 2021-09-13 15:53:30 -0700 | [diff] [blame] | 318 | void (*change_real_num_tx)(struct Qdisc *sch, |
| 319 | unsigned int new_real_tx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | |
| 321 | int (*dump)(struct Qdisc *, struct sk_buff *); |
| 322 | int (*dump_stats)(struct Qdisc *, struct gnet_dump *); |
| 323 | |
Jiri Pirko | d47a6b0 | 2018-01-17 11:46:52 +0100 | [diff] [blame] | 324 | void (*ingress_block_set)(struct Qdisc *sch, |
| 325 | u32 block_index); |
| 326 | void (*egress_block_set)(struct Qdisc *sch, |
| 327 | u32 block_index); |
| 328 | u32 (*ingress_block_get)(struct Qdisc *sch); |
| 329 | u32 (*egress_block_get)(struct Qdisc *sch); |
| 330 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | struct module *owner; |
| 332 | }; |
| 333 | |
| 334 | |
Eric Dumazet | fd2c3ef | 2009-11-03 03:26:03 +0000 | [diff] [blame] | 335 | struct tcf_result { |
Jiri Pirko | db50514 | 2017-05-17 11:08:03 +0200 | [diff] [blame] | 336 | union { |
| 337 | struct { |
| 338 | unsigned long class; |
| 339 | u32 classid; |
| 340 | }; |
| 341 | const struct tcf_proto *goto_tp; |
Paolo Abeni | cd11b164 | 2018-07-30 14:30:44 +0200 | [diff] [blame] | 342 | |
John Hurley | 720f22f | 2019-06-24 23:13:35 +0100 | [diff] [blame] | 343 | /* used in the skb_tc_reinsert function */ |
Paolo Abeni | cd11b164 | 2018-07-30 14:30:44 +0200 | [diff] [blame] | 344 | struct { |
| 345 | bool ingress; |
| 346 | struct gnet_stats_queue *qstats; |
| 347 | }; |
Jiri Pirko | db50514 | 2017-05-17 11:08:03 +0200 | [diff] [blame] | 348 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | }; |
| 350 | |
Jiri Pirko | 9f407f1 | 2018-07-23 09:23:07 +0200 | [diff] [blame] | 351 | struct tcf_chain; |
| 352 | |
Eric Dumazet | fd2c3ef | 2009-11-03 03:26:03 +0000 | [diff] [blame] | 353 | struct tcf_proto_ops { |
WANG Cong | 3627287 | 2013-12-15 20:15:11 -0800 | [diff] [blame] | 354 | struct list_head head; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | char kind[IFNAMSIZ]; |
| 356 | |
Eric Dumazet | dc7f9f6 | 2011-07-05 23:25:42 +0000 | [diff] [blame] | 357 | int (*classify)(struct sk_buff *, |
| 358 | const struct tcf_proto *, |
| 359 | struct tcf_result *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | int (*init)(struct tcf_proto*); |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 361 | void (*destroy)(struct tcf_proto *tp, bool rtnl_held, |
Jakub Kicinski | 715df5e | 2018-01-24 12:54:13 -0800 | [diff] [blame] | 362 | struct netlink_ext_ack *extack); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 364 | void* (*get)(struct tcf_proto*, u32 handle); |
Vlad Buslov | 7d5509f | 2019-02-11 10:55:44 +0200 | [diff] [blame] | 365 | void (*put)(struct tcf_proto *tp, void *f); |
Benjamin LaHaise | c1b5273 | 2013-01-14 05:15:39 +0000 | [diff] [blame] | 366 | int (*change)(struct net *net, struct sk_buff *, |
Eric W. Biederman | af4c664 | 2012-05-25 13:42:45 -0600 | [diff] [blame] | 367 | struct tcf_proto*, unsigned long, |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame] | 368 | u32 handle, struct nlattr **, |
Cong Wang | 695176b | 2021-07-29 16:12:14 -0700 | [diff] [blame] | 369 | void **, u32, |
Alexander Aring | 7306db3 | 2018-01-18 11:20:51 -0500 | [diff] [blame] | 370 | struct netlink_ext_ack *); |
Alexander Aring | 8865fdd | 2018-01-18 11:20:49 -0500 | [diff] [blame] | 371 | int (*delete)(struct tcf_proto *tp, void *arg, |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 372 | bool *last, bool rtnl_held, |
Alexander Aring | 571acf2 | 2018-01-18 11:20:53 -0500 | [diff] [blame] | 373 | struct netlink_ext_ack *); |
Davide Caratti | a5b72a0 | 2019-12-28 16:36:58 +0100 | [diff] [blame] | 374 | bool (*delete_empty)(struct tcf_proto *tp); |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 375 | void (*walk)(struct tcf_proto *tp, |
| 376 | struct tcf_walker *arg, bool rtnl_held); |
John Hurley | e56185c | 2018-06-25 14:30:05 -0700 | [diff] [blame] | 377 | int (*reoffload)(struct tcf_proto *tp, bool add, |
Pablo Neira Ayuso | a732331 | 2019-07-19 18:20:15 +0200 | [diff] [blame] | 378 | flow_setup_cb_t *cb, void *cb_priv, |
John Hurley | e56185c | 2018-06-25 14:30:05 -0700 | [diff] [blame] | 379 | struct netlink_ext_ack *extack); |
Vlad Buslov | a449a3e | 2019-08-26 16:45:00 +0300 | [diff] [blame] | 380 | void (*hw_add)(struct tcf_proto *tp, |
| 381 | void *type_data); |
| 382 | void (*hw_del)(struct tcf_proto *tp, |
| 383 | void *type_data); |
Cong Wang | 2e24cd7 | 2020-01-23 16:26:18 -0800 | [diff] [blame] | 384 | void (*bind_class)(void *, u32, unsigned long, |
| 385 | void *, unsigned long); |
Jiri Pirko | 9f407f1 | 2018-07-23 09:23:07 +0200 | [diff] [blame] | 386 | void * (*tmplt_create)(struct net *net, |
| 387 | struct tcf_chain *chain, |
| 388 | struct nlattr **tca, |
| 389 | struct netlink_ext_ack *extack); |
| 390 | void (*tmplt_destroy)(void *tmplt_priv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | |
| 392 | /* rtnetlink specific */ |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 393 | int (*dump)(struct net*, struct tcf_proto*, void *, |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 394 | struct sk_buff *skb, struct tcmsg*, |
| 395 | bool); |
Vlad Buslov | f8ab180 | 2020-05-15 14:40:11 +0300 | [diff] [blame] | 396 | int (*terse_dump)(struct net *net, |
| 397 | struct tcf_proto *tp, void *fh, |
| 398 | struct sk_buff *skb, |
| 399 | struct tcmsg *t, bool rtnl_held); |
Jiri Pirko | 9f407f1 | 2018-07-23 09:23:07 +0200 | [diff] [blame] | 400 | int (*tmplt_dump)(struct sk_buff *skb, |
| 401 | struct net *net, |
| 402 | void *tmplt_priv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | |
| 404 | struct module *owner; |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 405 | int flags; |
| 406 | }; |
| 407 | |
Davide Caratti | a5b72a0 | 2019-12-28 16:36:58 +0100 | [diff] [blame] | 408 | /* Classifiers setting TCF_PROTO_OPS_DOIT_UNLOCKED in tcf_proto_ops->flags |
| 409 | * are expected to implement tcf_proto_ops->delete_empty(), otherwise race |
| 410 | * conditions can occur when filters are inserted/deleted simultaneously. |
| 411 | */ |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 412 | enum tcf_proto_ops_flags { |
| 413 | TCF_PROTO_OPS_DOIT_UNLOCKED = 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | }; |
| 415 | |
Eric Dumazet | fd2c3ef | 2009-11-03 03:26:03 +0000 | [diff] [blame] | 416 | struct tcf_proto { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | /* Fast access part */ |
John Fastabend | 25d8c0d | 2014-09-12 20:05:27 -0700 | [diff] [blame] | 418 | struct tcf_proto __rcu *next; |
| 419 | void __rcu *root; |
Paolo Abeni | 7fd4b28 | 2018-07-30 14:30:43 +0200 | [diff] [blame] | 420 | |
| 421 | /* called under RCU BH lock*/ |
Eric Dumazet | dc7f9f6 | 2011-07-05 23:25:42 +0000 | [diff] [blame] | 422 | int (*classify)(struct sk_buff *, |
| 423 | const struct tcf_proto *, |
| 424 | struct tcf_result *); |
Al Viro | 66c6f52 | 2006-11-20 18:07:51 -0800 | [diff] [blame] | 425 | __be16 protocol; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | |
| 427 | /* All the rest */ |
| 428 | u32 prio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | void *data; |
Eric Dumazet | dc7f9f6 | 2011-07-05 23:25:42 +0000 | [diff] [blame] | 430 | const struct tcf_proto_ops *ops; |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 431 | struct tcf_chain *chain; |
Vlad Buslov | 8b64678 | 2019-02-11 10:55:41 +0200 | [diff] [blame] | 432 | /* Lock protects tcf_proto shared state and can be used by unlocked |
| 433 | * classifiers to protect their private data. |
| 434 | */ |
| 435 | spinlock_t lock; |
| 436 | bool deleting; |
Vlad Buslov | 4dbfa76 | 2019-02-11 10:55:39 +0200 | [diff] [blame] | 437 | refcount_t refcnt; |
John Fastabend | 25d8c0d | 2014-09-12 20:05:27 -0700 | [diff] [blame] | 438 | struct rcu_head rcu; |
John Hurley | 59eb87c | 2019-11-02 14:17:47 +0000 | [diff] [blame] | 439 | struct hlist_node destroy_ht_node; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | }; |
| 441 | |
Jussi Kivilinna | 175f9c1 | 2008-07-20 00:08:47 -0700 | [diff] [blame] | 442 | struct qdisc_skb_cb { |
Stanislav Fomichev | 089b19a | 2019-04-22 08:55:44 -0700 | [diff] [blame] | 443 | struct { |
| 444 | unsigned int pkt_len; |
| 445 | u16 slave_dev_queue_mapping; |
| 446 | u16 tc_classid; |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 447 | }; |
Eric Dumazet | 2571178 | 2014-09-18 08:02:05 -0700 | [diff] [blame] | 448 | #define QDISC_CB_PRIV_LEN 20 |
| 449 | unsigned char data[QDISC_CB_PRIV_LEN]; |
Jussi Kivilinna | 175f9c1 | 2008-07-20 00:08:47 -0700 | [diff] [blame] | 450 | }; |
| 451 | |
Jiri Pirko | c7eb7d7 | 2017-11-03 11:46:24 +0100 | [diff] [blame] | 452 | typedef void tcf_chain_head_change_t(struct tcf_proto *tp_head, void *priv); |
| 453 | |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 454 | struct tcf_chain { |
Vlad Buslov | ed76f5e | 2019-02-11 10:55:38 +0200 | [diff] [blame] | 455 | /* Protects filter_chain. */ |
| 456 | struct mutex filter_chain_lock; |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 457 | struct tcf_proto __rcu *filter_chain; |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 458 | struct list_head list; |
| 459 | struct tcf_block *block; |
| 460 | u32 index; /* chain index */ |
| 461 | unsigned int refcnt; |
Jiri Pirko | 1f3ed38 | 2018-07-27 09:45:05 +0200 | [diff] [blame] | 462 | unsigned int action_refcnt; |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 463 | bool explicitly_created; |
Vlad Buslov | 726d0612 | 2019-02-11 10:55:42 +0200 | [diff] [blame] | 464 | bool flushing; |
Jiri Pirko | 9f407f1 | 2018-07-23 09:23:07 +0200 | [diff] [blame] | 465 | const struct tcf_proto_ops *tmplt_ops; |
| 466 | void *tmplt_priv; |
Davide Caratti | ee3bbfe | 2019-03-20 15:00:16 +0100 | [diff] [blame] | 467 | struct rcu_head rcu; |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 468 | }; |
| 469 | |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 470 | struct tcf_block { |
Vlad Buslov | c266f64 | 2019-02-11 10:55:32 +0200 | [diff] [blame] | 471 | /* Lock protects tcf_block and lifetime-management data of chains |
| 472 | * attached to the block (refcnt, action_refcnt, explicitly_created). |
| 473 | */ |
| 474 | struct mutex lock; |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 475 | struct list_head chain_list; |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 476 | u32 index; /* block index for shared blocks */ |
Cong Wang | a7df487 | 2020-04-30 20:53:49 -0700 | [diff] [blame] | 477 | u32 classid; /* which class this block belongs to */ |
Vlad Buslov | cfebd7e | 2018-09-24 19:22:54 +0300 | [diff] [blame] | 478 | refcount_t refcnt; |
Jiri Pirko | 855319b | 2017-10-13 14:00:58 +0200 | [diff] [blame] | 479 | struct net *net; |
Jiri Pirko | 69d78ef | 2017-10-13 14:00:57 +0200 | [diff] [blame] | 480 | struct Qdisc *q; |
Vlad Buslov | 4f8116c | 2019-08-26 16:44:57 +0300 | [diff] [blame] | 481 | struct rw_semaphore cb_lock; /* protects cb_list and offload counters */ |
Pablo Neira Ayuso | 14bfb13 | 2019-07-19 18:20:16 +0200 | [diff] [blame] | 482 | struct flow_block flow_block; |
Jiri Pirko | f36fe1c | 2018-01-17 11:46:48 +0100 | [diff] [blame] | 483 | struct list_head owner_list; |
| 484 | bool keep_dst; |
Vlad Buslov | 97394be | 2019-08-26 16:44:58 +0300 | [diff] [blame] | 485 | atomic_t offloadcnt; /* Number of oddloaded filters */ |
Jiri Pirko | caa7260 | 2018-01-17 11:46:50 +0100 | [diff] [blame] | 486 | unsigned int nooffloaddevcnt; /* Number of devs unable to do offload */ |
Vlad Buslov | c9f1447 | 2019-08-26 16:45:01 +0300 | [diff] [blame] | 487 | unsigned int lockeddevcnt; /* Number of devs that require rtnl lock. */ |
Jiri Pirko | f71e0ca4 | 2018-07-23 09:23:05 +0200 | [diff] [blame] | 488 | struct { |
| 489 | struct tcf_chain *chain; |
| 490 | struct list_head filter_chain_list; |
| 491 | } chain0; |
Vlad Buslov | 0607e43 | 2018-09-24 19:22:57 +0300 | [diff] [blame] | 492 | struct rcu_head rcu; |
John Hurley | 59eb87c | 2019-11-02 14:17:47 +0000 | [diff] [blame] | 493 | DECLARE_HASHTABLE(proto_destroy_ht, 7); |
| 494 | struct mutex proto_destroy_lock; /* Lock for proto_destroy hashtable. */ |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 495 | }; |
| 496 | |
Vlad Buslov | ed76f5e | 2019-02-11 10:55:38 +0200 | [diff] [blame] | 497 | static inline bool lockdep_tcf_chain_is_locked(struct tcf_chain *chain) |
| 498 | { |
| 499 | return lockdep_is_held(&chain->filter_chain_lock); |
| 500 | } |
Vlad Buslov | 8b64678 | 2019-02-11 10:55:41 +0200 | [diff] [blame] | 501 | |
| 502 | static inline bool lockdep_tcf_proto_is_locked(struct tcf_proto *tp) |
| 503 | { |
| 504 | return lockdep_is_held(&tp->lock); |
| 505 | } |
Vlad Buslov | ed76f5e | 2019-02-11 10:55:38 +0200 | [diff] [blame] | 506 | |
| 507 | #define tcf_chain_dereference(p, chain) \ |
| 508 | rcu_dereference_protected(p, lockdep_tcf_chain_is_locked(chain)) |
| 509 | |
Vlad Buslov | 8b64678 | 2019-02-11 10:55:41 +0200 | [diff] [blame] | 510 | #define tcf_proto_dereference(p, tp) \ |
| 511 | rcu_dereference_protected(p, lockdep_tcf_proto_is_locked(tp)) |
| 512 | |
David S. Miller | 16bda13 | 2012-02-06 15:14:37 -0500 | [diff] [blame] | 513 | static inline void qdisc_cb_private_validate(const struct sk_buff *skb, int sz) |
| 514 | { |
| 515 | struct qdisc_skb_cb *qcb; |
Eric Dumazet | 5ee31c68 | 2012-06-12 06:03:51 +0000 | [diff] [blame] | 516 | |
wenxu | 038ebb1 | 2020-07-31 10:45:01 +0800 | [diff] [blame] | 517 | BUILD_BUG_ON(sizeof(skb->cb) < sizeof(*qcb)); |
David S. Miller | 16bda13 | 2012-02-06 15:14:37 -0500 | [diff] [blame] | 518 | BUILD_BUG_ON(sizeof(qcb->data) < sz); |
| 519 | } |
| 520 | |
Paolo Abeni | 73eb628 | 2019-04-10 14:32:41 +0200 | [diff] [blame] | 521 | static inline int qdisc_qlen_cpu(const struct Qdisc *q) |
| 522 | { |
| 523 | return this_cpu_ptr(q->cpu_qstats)->qlen; |
| 524 | } |
| 525 | |
Eric Dumazet | 05bdd2f | 2011-10-20 17:45:43 -0400 | [diff] [blame] | 526 | static inline int qdisc_qlen(const struct Qdisc *q) |
Krishna Kumar | bbd8a0d | 2009-08-06 01:44:21 +0000 | [diff] [blame] | 527 | { |
| 528 | return q->q.qlen; |
| 529 | } |
| 530 | |
Paolo Abeni | 73eb628 | 2019-04-10 14:32:41 +0200 | [diff] [blame] | 531 | static inline int qdisc_qlen_sum(const struct Qdisc *q) |
John Fastabend | 7e66016 | 2017-12-07 09:57:00 -0800 | [diff] [blame] | 532 | { |
Paolo Abeni | 73eb628 | 2019-04-10 14:32:41 +0200 | [diff] [blame] | 533 | __u32 qlen = q->qstats.qlen; |
| 534 | int i; |
John Fastabend | 7e66016 | 2017-12-07 09:57:00 -0800 | [diff] [blame] | 535 | |
Paolo Abeni | 73eb628 | 2019-04-10 14:32:41 +0200 | [diff] [blame] | 536 | if (qdisc_is_percpu_stats(q)) { |
| 537 | for_each_possible_cpu(i) |
| 538 | qlen += per_cpu_ptr(q->cpu_qstats, i)->qlen; |
| 539 | } else { |
Jakub Kicinski | 6172abc | 2018-05-25 21:53:30 -0700 | [diff] [blame] | 540 | qlen += q->q.qlen; |
Paolo Abeni | 73eb628 | 2019-04-10 14:32:41 +0200 | [diff] [blame] | 541 | } |
John Fastabend | 7e66016 | 2017-12-07 09:57:00 -0800 | [diff] [blame] | 542 | |
| 543 | return qlen; |
| 544 | } |
| 545 | |
Eric Dumazet | bfe0d02 | 2011-01-09 08:30:54 +0000 | [diff] [blame] | 546 | static inline struct qdisc_skb_cb *qdisc_skb_cb(const struct sk_buff *skb) |
Jussi Kivilinna | 175f9c1 | 2008-07-20 00:08:47 -0700 | [diff] [blame] | 547 | { |
| 548 | return (struct qdisc_skb_cb *)skb->cb; |
| 549 | } |
| 550 | |
David S. Miller | 83874000 | 2008-07-17 00:53:03 -0700 | [diff] [blame] | 551 | static inline spinlock_t *qdisc_lock(struct Qdisc *qdisc) |
| 552 | { |
| 553 | return &qdisc->q.lock; |
| 554 | } |
| 555 | |
Eric Dumazet | 05bdd2f | 2011-10-20 17:45:43 -0400 | [diff] [blame] | 556 | static inline struct Qdisc *qdisc_root(const struct Qdisc *qdisc) |
David S. Miller | 7698b4f | 2008-07-16 01:42:40 -0700 | [diff] [blame] | 557 | { |
John Fastabend | 46e5da40a | 2014-09-12 20:04:52 -0700 | [diff] [blame] | 558 | struct Qdisc *q = rcu_dereference_rtnl(qdisc->dev_queue->qdisc); |
| 559 | |
| 560 | return q; |
David S. Miller | 7698b4f | 2008-07-16 01:42:40 -0700 | [diff] [blame] | 561 | } |
| 562 | |
Eric Dumazet | 159d2c7 | 2019-09-24 13:11:26 -0700 | [diff] [blame] | 563 | static inline struct Qdisc *qdisc_root_bh(const struct Qdisc *qdisc) |
| 564 | { |
| 565 | return rcu_dereference_bh(qdisc->dev_queue->qdisc); |
| 566 | } |
| 567 | |
Eric Dumazet | 05bdd2f | 2011-10-20 17:45:43 -0400 | [diff] [blame] | 568 | static inline struct Qdisc *qdisc_root_sleeping(const struct Qdisc *qdisc) |
Jarek Poplawski | 2540e05 | 2008-08-21 05:11:14 -0700 | [diff] [blame] | 569 | { |
| 570 | return qdisc->dev_queue->qdisc_sleeping; |
| 571 | } |
| 572 | |
David S. Miller | 7e43f11 | 2008-08-02 23:27:37 -0700 | [diff] [blame] | 573 | /* The qdisc root lock is a mechanism by which to top level |
| 574 | * of a qdisc tree can be locked from any qdisc node in the |
| 575 | * forest. This allows changing the configuration of some |
| 576 | * aspect of the qdisc tree while blocking out asynchronous |
| 577 | * qdisc access in the packet processing paths. |
| 578 | * |
| 579 | * It is only legal to do this when the root will not change |
| 580 | * on us. Otherwise we'll potentially lock the wrong qdisc |
| 581 | * root. This is enforced by holding the RTNL semaphore, which |
| 582 | * all users of this lock accessor must do. |
| 583 | */ |
Eric Dumazet | 05bdd2f | 2011-10-20 17:45:43 -0400 | [diff] [blame] | 584 | static inline spinlock_t *qdisc_root_lock(const struct Qdisc *qdisc) |
David S. Miller | 7698b4f | 2008-07-16 01:42:40 -0700 | [diff] [blame] | 585 | { |
| 586 | struct Qdisc *root = qdisc_root(qdisc); |
| 587 | |
David S. Miller | 7e43f11 | 2008-08-02 23:27:37 -0700 | [diff] [blame] | 588 | ASSERT_RTNL(); |
David S. Miller | 83874000 | 2008-07-17 00:53:03 -0700 | [diff] [blame] | 589 | return qdisc_lock(root); |
David S. Miller | 7698b4f | 2008-07-16 01:42:40 -0700 | [diff] [blame] | 590 | } |
| 591 | |
Eric Dumazet | 05bdd2f | 2011-10-20 17:45:43 -0400 | [diff] [blame] | 592 | static inline spinlock_t *qdisc_root_sleeping_lock(const struct Qdisc *qdisc) |
Jarek Poplawski | f6f9b93 | 2008-08-27 02:25:17 -0700 | [diff] [blame] | 593 | { |
| 594 | struct Qdisc *root = qdisc_root_sleeping(qdisc); |
| 595 | |
| 596 | ASSERT_RTNL(); |
| 597 | return qdisc_lock(root); |
| 598 | } |
| 599 | |
Eric Dumazet | 05bdd2f | 2011-10-20 17:45:43 -0400 | [diff] [blame] | 600 | static inline struct net_device *qdisc_dev(const struct Qdisc *qdisc) |
David S. Miller | 5ce2d48 | 2008-07-08 17:06:30 -0700 | [diff] [blame] | 601 | { |
| 602 | return qdisc->dev_queue->dev; |
| 603 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | |
Maxim Mikityanskiy | ca1e4ab | 2021-01-19 14:08:11 +0200 | [diff] [blame] | 605 | static inline void sch_tree_lock(struct Qdisc *q) |
David S. Miller | 78a5b30 | 2008-07-16 03:12:24 -0700 | [diff] [blame] | 606 | { |
Maxim Mikityanskiy | ca1e4ab | 2021-01-19 14:08:11 +0200 | [diff] [blame] | 607 | if (q->flags & TCQ_F_MQROOT) |
| 608 | spin_lock_bh(qdisc_lock(q)); |
| 609 | else |
| 610 | spin_lock_bh(qdisc_root_sleeping_lock(q)); |
David S. Miller | 78a5b30 | 2008-07-16 03:12:24 -0700 | [diff] [blame] | 611 | } |
| 612 | |
Maxim Mikityanskiy | ca1e4ab | 2021-01-19 14:08:11 +0200 | [diff] [blame] | 613 | static inline void sch_tree_unlock(struct Qdisc *q) |
David S. Miller | 78a5b30 | 2008-07-16 03:12:24 -0700 | [diff] [blame] | 614 | { |
Maxim Mikityanskiy | ca1e4ab | 2021-01-19 14:08:11 +0200 | [diff] [blame] | 615 | if (q->flags & TCQ_F_MQROOT) |
| 616 | spin_unlock_bh(qdisc_lock(q)); |
| 617 | else |
| 618 | spin_unlock_bh(qdisc_root_sleeping_lock(q)); |
David S. Miller | 78a5b30 | 2008-07-16 03:12:24 -0700 | [diff] [blame] | 619 | } |
| 620 | |
Thomas Graf | e41a33e | 2005-07-05 14:14:30 -0700 | [diff] [blame] | 621 | extern struct Qdisc noop_qdisc; |
| 622 | extern struct Qdisc_ops noop_qdisc_ops; |
David S. Miller | 6ec1c69 | 2009-09-06 01:58:51 -0700 | [diff] [blame] | 623 | extern struct Qdisc_ops pfifo_fast_ops; |
| 624 | extern struct Qdisc_ops mq_qdisc_ops; |
Phil Sutter | d66d6c3 | 2015-08-27 21:21:38 +0200 | [diff] [blame] | 625 | extern struct Qdisc_ops noqueue_qdisc_ops; |
stephen hemminger | 6da7c8f | 2013-08-27 16:19:08 -0700 | [diff] [blame] | 626 | extern const struct Qdisc_ops *default_qdisc_ops; |
Eric Dumazet | 1f27cde | 2016-03-02 08:21:43 -0800 | [diff] [blame] | 627 | static inline const struct Qdisc_ops * |
| 628 | get_default_qdisc_ops(const struct net_device *dev, int ntx) |
| 629 | { |
| 630 | return ntx < dev->real_num_tx_queues ? |
| 631 | default_qdisc_ops : &pfifo_fast_ops; |
| 632 | } |
Thomas Graf | e41a33e | 2005-07-05 14:14:30 -0700 | [diff] [blame] | 633 | |
Eric Dumazet | fd2c3ef | 2009-11-03 03:26:03 +0000 | [diff] [blame] | 634 | struct Qdisc_class_common { |
Patrick McHardy | 6fe1c7a | 2008-07-05 23:21:31 -0700 | [diff] [blame] | 635 | u32 classid; |
| 636 | struct hlist_node hnode; |
| 637 | }; |
| 638 | |
Eric Dumazet | fd2c3ef | 2009-11-03 03:26:03 +0000 | [diff] [blame] | 639 | struct Qdisc_class_hash { |
Patrick McHardy | 6fe1c7a | 2008-07-05 23:21:31 -0700 | [diff] [blame] | 640 | struct hlist_head *hash; |
| 641 | unsigned int hashsize; |
| 642 | unsigned int hashmask; |
| 643 | unsigned int hashelems; |
| 644 | }; |
| 645 | |
| 646 | static inline unsigned int qdisc_class_hash(u32 id, u32 mask) |
| 647 | { |
| 648 | id ^= id >> 8; |
| 649 | id ^= id >> 4; |
| 650 | return id & mask; |
| 651 | } |
| 652 | |
| 653 | static inline struct Qdisc_class_common * |
Eric Dumazet | 05bdd2f | 2011-10-20 17:45:43 -0400 | [diff] [blame] | 654 | qdisc_class_find(const struct Qdisc_class_hash *hash, u32 id) |
Patrick McHardy | 6fe1c7a | 2008-07-05 23:21:31 -0700 | [diff] [blame] | 655 | { |
| 656 | struct Qdisc_class_common *cl; |
Patrick McHardy | 6fe1c7a | 2008-07-05 23:21:31 -0700 | [diff] [blame] | 657 | unsigned int h; |
| 658 | |
Gao Feng | 7d3f0cd | 2017-08-18 15:23:24 +0800 | [diff] [blame] | 659 | if (!id) |
| 660 | return NULL; |
| 661 | |
Patrick McHardy | 6fe1c7a | 2008-07-05 23:21:31 -0700 | [diff] [blame] | 662 | h = qdisc_class_hash(id, hash->hashmask); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 663 | hlist_for_each_entry(cl, &hash->hash[h], hnode) { |
Patrick McHardy | 6fe1c7a | 2008-07-05 23:21:31 -0700 | [diff] [blame] | 664 | if (cl->classid == id) |
| 665 | return cl; |
| 666 | } |
| 667 | return NULL; |
| 668 | } |
| 669 | |
Amritha Nambiar | 384c181 | 2017-10-27 02:35:34 -0700 | [diff] [blame] | 670 | static inline int tc_classid_to_hwtc(struct net_device *dev, u32 classid) |
| 671 | { |
| 672 | u32 hwtc = TC_H_MIN(classid) - TC_H_MIN_PRIORITY; |
| 673 | |
| 674 | return (hwtc < netdev_get_num_tc(dev)) ? hwtc : -EINVAL; |
| 675 | } |
| 676 | |
Joe Perches | 5c15257 | 2013-07-30 22:47:13 -0700 | [diff] [blame] | 677 | int qdisc_class_hash_init(struct Qdisc_class_hash *); |
| 678 | void qdisc_class_hash_insert(struct Qdisc_class_hash *, |
| 679 | struct Qdisc_class_common *); |
| 680 | void qdisc_class_hash_remove(struct Qdisc_class_hash *, |
| 681 | struct Qdisc_class_common *); |
| 682 | void qdisc_class_hash_grow(struct Qdisc *, struct Qdisc_class_hash *); |
| 683 | void qdisc_class_hash_destroy(struct Qdisc_class_hash *); |
Patrick McHardy | 6fe1c7a | 2008-07-05 23:21:31 -0700 | [diff] [blame] | 684 | |
Cong Wang | 48bfd55 | 2018-01-25 18:26:23 -0800 | [diff] [blame] | 685 | int dev_qdisc_change_tx_queue_len(struct net_device *dev); |
Jakub Kicinski | 1e080f1 | 2021-09-13 15:53:30 -0700 | [diff] [blame] | 686 | void dev_qdisc_change_real_num_tx(struct net_device *dev, |
| 687 | unsigned int new_real_tx); |
Joe Perches | 5c15257 | 2013-07-30 22:47:13 -0700 | [diff] [blame] | 688 | void dev_init_scheduler(struct net_device *dev); |
| 689 | void dev_shutdown(struct net_device *dev); |
| 690 | void dev_activate(struct net_device *dev); |
| 691 | void dev_deactivate(struct net_device *dev); |
| 692 | void dev_deactivate_many(struct list_head *head); |
| 693 | struct Qdisc *dev_graft_qdisc(struct netdev_queue *dev_queue, |
| 694 | struct Qdisc *qdisc); |
| 695 | void qdisc_reset(struct Qdisc *qdisc); |
Vlad Buslov | 86bd446 | 2018-09-24 19:22:50 +0300 | [diff] [blame] | 696 | void qdisc_put(struct Qdisc *qdisc); |
Vlad Buslov | 3a7d0d0 | 2018-09-24 19:22:51 +0300 | [diff] [blame] | 697 | void qdisc_put_unlocked(struct Qdisc *qdisc); |
Toke Høiland-Jørgensen | 5f2939d | 2019-01-09 17:10:57 +0100 | [diff] [blame] | 698 | void qdisc_tree_reduce_backlog(struct Qdisc *qdisc, int n, int len); |
Jakub Kicinski | b592843 | 2018-11-07 17:33:34 -0800 | [diff] [blame] | 699 | #ifdef CONFIG_NET_SCHED |
| 700 | int qdisc_offload_dump_helper(struct Qdisc *q, enum tc_setup_type type, |
| 701 | void *type_data); |
Jakub Kicinski | bfaee91 | 2018-11-07 17:33:37 -0800 | [diff] [blame] | 702 | void qdisc_offload_graft_helper(struct net_device *dev, struct Qdisc *sch, |
| 703 | struct Qdisc *new, struct Qdisc *old, |
| 704 | enum tc_setup_type type, void *type_data, |
| 705 | struct netlink_ext_ack *extack); |
Jakub Kicinski | b592843 | 2018-11-07 17:33:34 -0800 | [diff] [blame] | 706 | #else |
| 707 | static inline int |
| 708 | qdisc_offload_dump_helper(struct Qdisc *q, enum tc_setup_type type, |
| 709 | void *type_data) |
| 710 | { |
| 711 | q->flags &= ~TCQ_F_OFFLOADED; |
| 712 | return 0; |
| 713 | } |
Jakub Kicinski | bfaee91 | 2018-11-07 17:33:37 -0800 | [diff] [blame] | 714 | |
| 715 | static inline void |
| 716 | qdisc_offload_graft_helper(struct net_device *dev, struct Qdisc *sch, |
| 717 | struct Qdisc *new, struct Qdisc *old, |
| 718 | enum tc_setup_type type, void *type_data, |
| 719 | struct netlink_ext_ack *extack) |
| 720 | { |
| 721 | } |
Jakub Kicinski | b592843 | 2018-11-07 17:33:34 -0800 | [diff] [blame] | 722 | #endif |
Joe Perches | 5c15257 | 2013-07-30 22:47:13 -0700 | [diff] [blame] | 723 | struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue, |
Alexander Aring | d0bd684 | 2017-12-20 12:35:20 -0500 | [diff] [blame] | 724 | const struct Qdisc_ops *ops, |
| 725 | struct netlink_ext_ack *extack); |
Daniel Borkmann | 81d947e | 2018-01-15 23:12:09 +0100 | [diff] [blame] | 726 | void qdisc_free(struct Qdisc *qdisc); |
Joe Perches | 5c15257 | 2013-07-30 22:47:13 -0700 | [diff] [blame] | 727 | struct Qdisc *qdisc_create_dflt(struct netdev_queue *dev_queue, |
Alexander Aring | a38a9882 | 2017-12-20 12:35:21 -0500 | [diff] [blame] | 728 | const struct Qdisc_ops *ops, u32 parentid, |
| 729 | struct netlink_ext_ack *extack); |
Joe Perches | 5c15257 | 2013-07-30 22:47:13 -0700 | [diff] [blame] | 730 | void __qdisc_calculate_pkt_len(struct sk_buff *skb, |
| 731 | const struct qdisc_size_table *stab); |
Alexei Starovoitov | 27b29f6 | 2015-09-15 23:05:43 -0700 | [diff] [blame] | 732 | int skb_do_redirect(struct sk_buff *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | |
Daniel Borkmann | fdc5432 | 2016-01-07 15:50:22 +0100 | [diff] [blame] | 734 | static inline bool skb_at_tc_ingress(const struct sk_buff *skb) |
| 735 | { |
| 736 | #ifdef CONFIG_NET_CLS_ACT |
Willem de Bruijn | 8dc07fd | 2017-01-07 17:06:37 -0500 | [diff] [blame] | 737 | return skb->tc_at_ingress; |
Daniel Borkmann | fdc5432 | 2016-01-07 15:50:22 +0100 | [diff] [blame] | 738 | #else |
| 739 | return false; |
| 740 | #endif |
| 741 | } |
| 742 | |
Willem de Bruijn | e7246e1 | 2017-01-07 17:06:35 -0500 | [diff] [blame] | 743 | static inline bool skb_skip_tc_classify(struct sk_buff *skb) |
| 744 | { |
| 745 | #ifdef CONFIG_NET_CLS_ACT |
| 746 | if (skb->tc_skip_classify) { |
| 747 | skb->tc_skip_classify = 0; |
| 748 | return true; |
| 749 | } |
| 750 | #endif |
| 751 | return false; |
| 752 | } |
| 753 | |
Gal Pressman | 3a053b1 | 2018-02-28 15:59:15 +0200 | [diff] [blame] | 754 | /* Reset all TX qdiscs greater than index of a device. */ |
John Fastabend | f0796d5 | 2010-07-01 13:21:57 +0000 | [diff] [blame] | 755 | static inline void qdisc_reset_all_tx_gt(struct net_device *dev, unsigned int i) |
David S. Miller | 5aa7099 | 2008-07-08 22:59:10 -0700 | [diff] [blame] | 756 | { |
John Fastabend | 4ef6acf | 2010-07-01 13:21:35 +0000 | [diff] [blame] | 757 | struct Qdisc *qdisc; |
| 758 | |
John Fastabend | f0796d5 | 2010-07-01 13:21:57 +0000 | [diff] [blame] | 759 | for (; i < dev->num_tx_queues; i++) { |
John Fastabend | 46e5da40a | 2014-09-12 20:04:52 -0700 | [diff] [blame] | 760 | qdisc = rtnl_dereference(netdev_get_tx_queue(dev, i)->qdisc); |
John Fastabend | 4ef6acf | 2010-07-01 13:21:35 +0000 | [diff] [blame] | 761 | if (qdisc) { |
| 762 | spin_lock_bh(qdisc_lock(qdisc)); |
| 763 | qdisc_reset(qdisc); |
| 764 | spin_unlock_bh(qdisc_lock(qdisc)); |
| 765 | } |
| 766 | } |
David S. Miller | 5aa7099 | 2008-07-08 22:59:10 -0700 | [diff] [blame] | 767 | } |
| 768 | |
David S. Miller | 3e745dd | 2008-07-08 23:00:25 -0700 | [diff] [blame] | 769 | /* Are all TX queues of the device empty? */ |
| 770 | static inline bool qdisc_all_tx_empty(const struct net_device *dev) |
| 771 | { |
David S. Miller | e8a0464 | 2008-07-17 00:34:19 -0700 | [diff] [blame] | 772 | unsigned int i; |
John Fastabend | 46e5da40a | 2014-09-12 20:04:52 -0700 | [diff] [blame] | 773 | |
| 774 | rcu_read_lock(); |
David S. Miller | e8a0464 | 2008-07-17 00:34:19 -0700 | [diff] [blame] | 775 | for (i = 0; i < dev->num_tx_queues; i++) { |
| 776 | struct netdev_queue *txq = netdev_get_tx_queue(dev, i); |
John Fastabend | 46e5da40a | 2014-09-12 20:04:52 -0700 | [diff] [blame] | 777 | const struct Qdisc *q = rcu_dereference(txq->qdisc); |
David S. Miller | 3e745dd | 2008-07-08 23:00:25 -0700 | [diff] [blame] | 778 | |
Paolo Abeni | 1f5e6fd | 2019-04-10 14:32:38 +0200 | [diff] [blame] | 779 | if (!qdisc_is_empty(q)) { |
John Fastabend | 46e5da40a | 2014-09-12 20:04:52 -0700 | [diff] [blame] | 780 | rcu_read_unlock(); |
David S. Miller | e8a0464 | 2008-07-17 00:34:19 -0700 | [diff] [blame] | 781 | return false; |
John Fastabend | 46e5da40a | 2014-09-12 20:04:52 -0700 | [diff] [blame] | 782 | } |
David S. Miller | e8a0464 | 2008-07-17 00:34:19 -0700 | [diff] [blame] | 783 | } |
John Fastabend | 46e5da40a | 2014-09-12 20:04:52 -0700 | [diff] [blame] | 784 | rcu_read_unlock(); |
David S. Miller | e8a0464 | 2008-07-17 00:34:19 -0700 | [diff] [blame] | 785 | return true; |
David S. Miller | 3e745dd | 2008-07-08 23:00:25 -0700 | [diff] [blame] | 786 | } |
| 787 | |
David S. Miller | 6fa9864 | 2008-07-08 23:01:06 -0700 | [diff] [blame] | 788 | /* Are any of the TX qdiscs changing? */ |
Eric Dumazet | 05bdd2f | 2011-10-20 17:45:43 -0400 | [diff] [blame] | 789 | static inline bool qdisc_tx_changing(const struct net_device *dev) |
David S. Miller | 6fa9864 | 2008-07-08 23:01:06 -0700 | [diff] [blame] | 790 | { |
David S. Miller | e8a0464 | 2008-07-17 00:34:19 -0700 | [diff] [blame] | 791 | unsigned int i; |
John Fastabend | 46e5da40a | 2014-09-12 20:04:52 -0700 | [diff] [blame] | 792 | |
David S. Miller | e8a0464 | 2008-07-17 00:34:19 -0700 | [diff] [blame] | 793 | for (i = 0; i < dev->num_tx_queues; i++) { |
| 794 | struct netdev_queue *txq = netdev_get_tx_queue(dev, i); |
John Fastabend | 46e5da40a | 2014-09-12 20:04:52 -0700 | [diff] [blame] | 795 | if (rcu_access_pointer(txq->qdisc) != txq->qdisc_sleeping) |
David S. Miller | e8a0464 | 2008-07-17 00:34:19 -0700 | [diff] [blame] | 796 | return true; |
| 797 | } |
| 798 | return false; |
David S. Miller | 6fa9864 | 2008-07-08 23:01:06 -0700 | [diff] [blame] | 799 | } |
| 800 | |
David S. Miller | e8a0464 | 2008-07-17 00:34:19 -0700 | [diff] [blame] | 801 | /* Is the device using the noop qdisc on all queues? */ |
David S. Miller | 0529794 | 2008-07-08 23:01:27 -0700 | [diff] [blame] | 802 | static inline bool qdisc_tx_is_noop(const struct net_device *dev) |
| 803 | { |
David S. Miller | e8a0464 | 2008-07-17 00:34:19 -0700 | [diff] [blame] | 804 | unsigned int i; |
John Fastabend | 46e5da40a | 2014-09-12 20:04:52 -0700 | [diff] [blame] | 805 | |
David S. Miller | e8a0464 | 2008-07-17 00:34:19 -0700 | [diff] [blame] | 806 | for (i = 0; i < dev->num_tx_queues; i++) { |
| 807 | struct netdev_queue *txq = netdev_get_tx_queue(dev, i); |
John Fastabend | 46e5da40a | 2014-09-12 20:04:52 -0700 | [diff] [blame] | 808 | if (rcu_access_pointer(txq->qdisc) != &noop_qdisc) |
David S. Miller | e8a0464 | 2008-07-17 00:34:19 -0700 | [diff] [blame] | 809 | return false; |
| 810 | } |
| 811 | return true; |
David S. Miller | 0529794 | 2008-07-08 23:01:27 -0700 | [diff] [blame] | 812 | } |
| 813 | |
Eric Dumazet | bfe0d02 | 2011-01-09 08:30:54 +0000 | [diff] [blame] | 814 | static inline unsigned int qdisc_pkt_len(const struct sk_buff *skb) |
Jussi Kivilinna | 0abf77e | 2008-07-20 00:08:27 -0700 | [diff] [blame] | 815 | { |
Jussi Kivilinna | 175f9c1 | 2008-07-20 00:08:47 -0700 | [diff] [blame] | 816 | return qdisc_skb_cb(skb)->pkt_len; |
Jussi Kivilinna | 0abf77e | 2008-07-20 00:08:27 -0700 | [diff] [blame] | 817 | } |
| 818 | |
Jarek Poplawski | c27f339 | 2008-08-04 22:39:11 -0700 | [diff] [blame] | 819 | /* additional qdisc xmit flags (NET_XMIT_MASK in linux/netdevice.h) */ |
Jarek Poplawski | 378a2f0 | 2008-08-04 22:31:03 -0700 | [diff] [blame] | 820 | enum net_xmit_qdisc_t { |
| 821 | __NET_XMIT_STOLEN = 0x00010000, |
Jarek Poplawski | c27f339 | 2008-08-04 22:39:11 -0700 | [diff] [blame] | 822 | __NET_XMIT_BYPASS = 0x00020000, |
Jarek Poplawski | 378a2f0 | 2008-08-04 22:31:03 -0700 | [diff] [blame] | 823 | }; |
| 824 | |
Jarek Poplawski | c27f339 | 2008-08-04 22:39:11 -0700 | [diff] [blame] | 825 | #ifdef CONFIG_NET_CLS_ACT |
Jarek Poplawski | 378a2f0 | 2008-08-04 22:31:03 -0700 | [diff] [blame] | 826 | #define net_xmit_drop_count(e) ((e) & __NET_XMIT_STOLEN ? 0 : 1) |
Jarek Poplawski | 378a2f0 | 2008-08-04 22:31:03 -0700 | [diff] [blame] | 827 | #else |
| 828 | #define net_xmit_drop_count(e) (1) |
| 829 | #endif |
| 830 | |
Eric Dumazet | a2da570 | 2011-01-20 03:48:19 +0000 | [diff] [blame] | 831 | static inline void qdisc_calculate_pkt_len(struct sk_buff *skb, |
| 832 | const struct Qdisc *sch) |
Jussi Kivilinna | 5f86173 | 2008-07-20 00:08:04 -0700 | [diff] [blame] | 833 | { |
David S. Miller | 3a682fb | 2008-07-20 18:13:01 -0700 | [diff] [blame] | 834 | #ifdef CONFIG_NET_SCHED |
Eric Dumazet | a2da570 | 2011-01-20 03:48:19 +0000 | [diff] [blame] | 835 | struct qdisc_size_table *stab = rcu_dereference_bh(sch->stab); |
| 836 | |
| 837 | if (stab) |
| 838 | __qdisc_calculate_pkt_len(skb, stab); |
David S. Miller | 3a682fb | 2008-07-20 18:13:01 -0700 | [diff] [blame] | 839 | #endif |
Eric Dumazet | a2da570 | 2011-01-20 03:48:19 +0000 | [diff] [blame] | 840 | } |
| 841 | |
Petr Machata | ac5c66f | 2020-07-14 20:03:08 +0300 | [diff] [blame] | 842 | static inline int qdisc_enqueue(struct sk_buff *skb, struct Qdisc *sch, |
Eric Dumazet | 520ac30 | 2016-06-21 23:16:49 -0700 | [diff] [blame] | 843 | struct sk_buff **to_free) |
Eric Dumazet | a2da570 | 2011-01-20 03:48:19 +0000 | [diff] [blame] | 844 | { |
| 845 | qdisc_calculate_pkt_len(skb, sch); |
Petr Machata | ac5c66f | 2020-07-14 20:03:08 +0300 | [diff] [blame] | 846 | return sch->enqueue(skb, sch, to_free); |
Jussi Kivilinna | 5f86173 | 2008-07-20 00:08:04 -0700 | [diff] [blame] | 847 | } |
| 848 | |
Ahmed S. Darwish | 50dc9a8 | 2021-10-16 10:49:09 +0200 | [diff] [blame] | 849 | static inline void _bstats_update(struct gnet_stats_basic_sync *bstats, |
Amir Vadai | 3804070 | 2016-05-13 12:55:35 +0000 | [diff] [blame] | 850 | __u64 bytes, __u32 packets) |
| 851 | { |
Ahmed S. Darwish | 67c9e6270 | 2021-10-16 10:49:07 +0200 | [diff] [blame] | 852 | u64_stats_update_begin(&bstats->syncp); |
Ahmed S. Darwish | 50dc9a8 | 2021-10-16 10:49:09 +0200 | [diff] [blame] | 853 | u64_stats_add(&bstats->bytes, bytes); |
| 854 | u64_stats_add(&bstats->packets, packets); |
Ahmed S. Darwish | 67c9e6270 | 2021-10-16 10:49:07 +0200 | [diff] [blame] | 855 | u64_stats_update_end(&bstats->syncp); |
Amir Vadai | 3804070 | 2016-05-13 12:55:35 +0000 | [diff] [blame] | 856 | } |
| 857 | |
Ahmed S. Darwish | 50dc9a8 | 2021-10-16 10:49:09 +0200 | [diff] [blame] | 858 | static inline void bstats_update(struct gnet_stats_basic_sync *bstats, |
Eric Dumazet | bfe0d02 | 2011-01-09 08:30:54 +0000 | [diff] [blame] | 859 | const struct sk_buff *skb) |
Krishna Kumar | bbd8a0d | 2009-08-06 01:44:21 +0000 | [diff] [blame] | 860 | { |
Amir Vadai | 3804070 | 2016-05-13 12:55:35 +0000 | [diff] [blame] | 861 | _bstats_update(bstats, |
| 862 | qdisc_pkt_len(skb), |
| 863 | skb_is_gso(skb) ? skb_shinfo(skb)->gso_segs : 1); |
| 864 | } |
| 865 | |
Eric Dumazet | 24ea591 | 2015-07-06 05:18:03 -0700 | [diff] [blame] | 866 | static inline void qdisc_bstats_cpu_update(struct Qdisc *sch, |
| 867 | const struct sk_buff *skb) |
| 868 | { |
Ahmed S. Darwish | 50dc9a8 | 2021-10-16 10:49:09 +0200 | [diff] [blame] | 869 | bstats_update(this_cpu_ptr(sch->cpu_bstats), skb); |
Eric Dumazet | 24ea591 | 2015-07-06 05:18:03 -0700 | [diff] [blame] | 870 | } |
| 871 | |
Eric Dumazet | bfe0d02 | 2011-01-09 08:30:54 +0000 | [diff] [blame] | 872 | static inline void qdisc_bstats_update(struct Qdisc *sch, |
| 873 | const struct sk_buff *skb) |
| 874 | { |
| 875 | bstats_update(&sch->bstats, skb); |
Krishna Kumar | bbd8a0d | 2009-08-06 01:44:21 +0000 | [diff] [blame] | 876 | } |
| 877 | |
John Fastabend | 25331d6 | 2014-09-28 11:53:29 -0700 | [diff] [blame] | 878 | static inline void qdisc_qstats_backlog_dec(struct Qdisc *sch, |
| 879 | const struct sk_buff *skb) |
| 880 | { |
| 881 | sch->qstats.backlog -= qdisc_pkt_len(skb); |
| 882 | } |
| 883 | |
John Fastabend | 40bd036 | 2017-12-07 09:55:07 -0800 | [diff] [blame] | 884 | static inline void qdisc_qstats_cpu_backlog_dec(struct Qdisc *sch, |
| 885 | const struct sk_buff *skb) |
| 886 | { |
| 887 | this_cpu_sub(sch->cpu_qstats->backlog, qdisc_pkt_len(skb)); |
| 888 | } |
| 889 | |
John Fastabend | 25331d6 | 2014-09-28 11:53:29 -0700 | [diff] [blame] | 890 | static inline void qdisc_qstats_backlog_inc(struct Qdisc *sch, |
| 891 | const struct sk_buff *skb) |
| 892 | { |
| 893 | sch->qstats.backlog += qdisc_pkt_len(skb); |
| 894 | } |
| 895 | |
John Fastabend | 40bd036 | 2017-12-07 09:55:07 -0800 | [diff] [blame] | 896 | static inline void qdisc_qstats_cpu_backlog_inc(struct Qdisc *sch, |
| 897 | const struct sk_buff *skb) |
| 898 | { |
| 899 | this_cpu_add(sch->cpu_qstats->backlog, qdisc_pkt_len(skb)); |
| 900 | } |
| 901 | |
Paolo Abeni | 73eb628 | 2019-04-10 14:32:41 +0200 | [diff] [blame] | 902 | static inline void qdisc_qstats_cpu_qlen_inc(struct Qdisc *sch) |
John Fastabend | 40bd036 | 2017-12-07 09:55:07 -0800 | [diff] [blame] | 903 | { |
Paolo Abeni | 73eb628 | 2019-04-10 14:32:41 +0200 | [diff] [blame] | 904 | this_cpu_inc(sch->cpu_qstats->qlen); |
John Fastabend | 40bd036 | 2017-12-07 09:55:07 -0800 | [diff] [blame] | 905 | } |
| 906 | |
Paolo Abeni | 73eb628 | 2019-04-10 14:32:41 +0200 | [diff] [blame] | 907 | static inline void qdisc_qstats_cpu_qlen_dec(struct Qdisc *sch) |
John Fastabend | 40bd036 | 2017-12-07 09:55:07 -0800 | [diff] [blame] | 908 | { |
Paolo Abeni | 73eb628 | 2019-04-10 14:32:41 +0200 | [diff] [blame] | 909 | this_cpu_dec(sch->cpu_qstats->qlen); |
John Fastabend | 40bd036 | 2017-12-07 09:55:07 -0800 | [diff] [blame] | 910 | } |
| 911 | |
| 912 | static inline void qdisc_qstats_cpu_requeues_inc(struct Qdisc *sch) |
| 913 | { |
| 914 | this_cpu_inc(sch->cpu_qstats->requeues); |
| 915 | } |
| 916 | |
John Fastabend | 25331d6 | 2014-09-28 11:53:29 -0700 | [diff] [blame] | 917 | static inline void __qdisc_qstats_drop(struct Qdisc *sch, int count) |
| 918 | { |
| 919 | sch->qstats.drops += count; |
| 920 | } |
| 921 | |
Eric Dumazet | 24ea591 | 2015-07-06 05:18:03 -0700 | [diff] [blame] | 922 | static inline void qstats_drop_inc(struct gnet_stats_queue *qstats) |
John Fastabend | 25331d6 | 2014-09-28 11:53:29 -0700 | [diff] [blame] | 923 | { |
Eric Dumazet | 24ea591 | 2015-07-06 05:18:03 -0700 | [diff] [blame] | 924 | qstats->drops++; |
John Fastabend | 25331d6 | 2014-09-28 11:53:29 -0700 | [diff] [blame] | 925 | } |
| 926 | |
Eric Dumazet | 24ea591 | 2015-07-06 05:18:03 -0700 | [diff] [blame] | 927 | static inline void qstats_overlimit_inc(struct gnet_stats_queue *qstats) |
John Fastabend | b0ab6f9 | 2014-09-28 11:54:24 -0700 | [diff] [blame] | 928 | { |
Eric Dumazet | 24ea591 | 2015-07-06 05:18:03 -0700 | [diff] [blame] | 929 | qstats->overlimits++; |
| 930 | } |
John Fastabend | b0ab6f9 | 2014-09-28 11:54:24 -0700 | [diff] [blame] | 931 | |
Eric Dumazet | 24ea591 | 2015-07-06 05:18:03 -0700 | [diff] [blame] | 932 | static inline void qdisc_qstats_drop(struct Qdisc *sch) |
| 933 | { |
| 934 | qstats_drop_inc(&sch->qstats); |
| 935 | } |
| 936 | |
| 937 | static inline void qdisc_qstats_cpu_drop(struct Qdisc *sch) |
| 938 | { |
Eric Dumazet | eb60a8d | 2016-08-24 10:23:34 -0700 | [diff] [blame] | 939 | this_cpu_inc(sch->cpu_qstats->drops); |
John Fastabend | b0ab6f9 | 2014-09-28 11:54:24 -0700 | [diff] [blame] | 940 | } |
| 941 | |
John Fastabend | 25331d6 | 2014-09-28 11:53:29 -0700 | [diff] [blame] | 942 | static inline void qdisc_qstats_overlimit(struct Qdisc *sch) |
| 943 | { |
| 944 | sch->qstats.overlimits++; |
| 945 | } |
| 946 | |
Paolo Abeni | 5dd431b | 2019-03-28 16:53:12 +0100 | [diff] [blame] | 947 | static inline int qdisc_qstats_copy(struct gnet_dump *d, struct Qdisc *sch) |
| 948 | { |
| 949 | __u32 qlen = qdisc_qlen_sum(sch); |
| 950 | |
| 951 | return gnet_stats_copy_queue(d, sch->cpu_qstats, &sch->qstats, qlen); |
| 952 | } |
| 953 | |
| 954 | static inline void qdisc_qstats_qlen_backlog(struct Qdisc *sch, __u32 *qlen, |
| 955 | __u32 *backlog) |
| 956 | { |
| 957 | struct gnet_stats_queue qstats = { 0 }; |
Paolo Abeni | 5dd431b | 2019-03-28 16:53:12 +0100 | [diff] [blame] | 958 | |
Sebastian Andrzej Siewior | 10940eb | 2021-10-16 10:49:05 +0200 | [diff] [blame] | 959 | gnet_stats_add_queue(&qstats, sch->cpu_qstats, &sch->qstats); |
| 960 | *qlen = qstats.qlen + qdisc_qlen(sch); |
Paolo Abeni | 5dd431b | 2019-03-28 16:53:12 +0100 | [diff] [blame] | 961 | *backlog = qstats.backlog; |
| 962 | } |
| 963 | |
Paolo Abeni | e5f0e8f | 2019-03-28 16:53:13 +0100 | [diff] [blame] | 964 | static inline void qdisc_tree_flush_backlog(struct Qdisc *sch) |
| 965 | { |
| 966 | __u32 qlen, backlog; |
| 967 | |
| 968 | qdisc_qstats_qlen_backlog(sch, &qlen, &backlog); |
| 969 | qdisc_tree_reduce_backlog(sch, qlen, backlog); |
| 970 | } |
| 971 | |
| 972 | static inline void qdisc_purge_queue(struct Qdisc *sch) |
| 973 | { |
| 974 | __u32 qlen, backlog; |
| 975 | |
| 976 | qdisc_qstats_qlen_backlog(sch, &qlen, &backlog); |
| 977 | qdisc_reset(sch); |
| 978 | qdisc_tree_reduce_backlog(sch, qlen, backlog); |
| 979 | } |
| 980 | |
Florian Westphal | 48da34b | 2016-09-18 00:57:34 +0200 | [diff] [blame] | 981 | static inline void qdisc_skb_head_init(struct qdisc_skb_head *qh) |
Thomas Graf | 9972b25 | 2005-06-18 22:57:26 -0700 | [diff] [blame] | 982 | { |
Florian Westphal | 48da34b | 2016-09-18 00:57:34 +0200 | [diff] [blame] | 983 | qh->head = NULL; |
| 984 | qh->tail = NULL; |
| 985 | qh->qlen = 0; |
| 986 | } |
| 987 | |
David S. Miller | aea890b | 2018-07-29 16:22:13 -0700 | [diff] [blame] | 988 | static inline void __qdisc_enqueue_tail(struct sk_buff *skb, |
| 989 | struct qdisc_skb_head *qh) |
Florian Westphal | 48da34b | 2016-09-18 00:57:34 +0200 | [diff] [blame] | 990 | { |
| 991 | struct sk_buff *last = qh->tail; |
| 992 | |
| 993 | if (last) { |
| 994 | skb->next = NULL; |
| 995 | last->next = skb; |
| 996 | qh->tail = skb; |
| 997 | } else { |
| 998 | qh->tail = skb; |
| 999 | qh->head = skb; |
| 1000 | } |
| 1001 | qh->qlen++; |
Thomas Graf | 9972b25 | 2005-06-18 22:57:26 -0700 | [diff] [blame] | 1002 | } |
| 1003 | |
| 1004 | static inline int qdisc_enqueue_tail(struct sk_buff *skb, struct Qdisc *sch) |
| 1005 | { |
David S. Miller | aea890b | 2018-07-29 16:22:13 -0700 | [diff] [blame] | 1006 | __qdisc_enqueue_tail(skb, &sch->q); |
| 1007 | qdisc_qstats_backlog_inc(sch, skb); |
| 1008 | return NET_XMIT_SUCCESS; |
Thomas Graf | 9972b25 | 2005-06-18 22:57:26 -0700 | [diff] [blame] | 1009 | } |
| 1010 | |
David S. Miller | 5969773 | 2018-07-29 16:33:28 -0700 | [diff] [blame] | 1011 | static inline void __qdisc_enqueue_head(struct sk_buff *skb, |
| 1012 | struct qdisc_skb_head *qh) |
| 1013 | { |
| 1014 | skb->next = qh->head; |
| 1015 | |
| 1016 | if (!qh->head) |
| 1017 | qh->tail = skb; |
| 1018 | qh->head = skb; |
| 1019 | qh->qlen++; |
| 1020 | } |
| 1021 | |
Florian Westphal | 48da34b | 2016-09-18 00:57:34 +0200 | [diff] [blame] | 1022 | static inline struct sk_buff *__qdisc_dequeue_head(struct qdisc_skb_head *qh) |
Thomas Graf | 9972b25 | 2005-06-18 22:57:26 -0700 | [diff] [blame] | 1023 | { |
Florian Westphal | 48da34b | 2016-09-18 00:57:34 +0200 | [diff] [blame] | 1024 | struct sk_buff *skb = qh->head; |
| 1025 | |
| 1026 | if (likely(skb != NULL)) { |
| 1027 | qh->head = skb->next; |
| 1028 | qh->qlen--; |
| 1029 | if (qh->head == NULL) |
| 1030 | qh->tail = NULL; |
| 1031 | skb->next = NULL; |
| 1032 | } |
Thomas Graf | 9972b25 | 2005-06-18 22:57:26 -0700 | [diff] [blame] | 1033 | |
Florian Westphal | ec32336 | 2016-09-18 00:57:32 +0200 | [diff] [blame] | 1034 | return skb; |
| 1035 | } |
| 1036 | |
| 1037 | static inline struct sk_buff *qdisc_dequeue_head(struct Qdisc *sch) |
| 1038 | { |
| 1039 | struct sk_buff *skb = __qdisc_dequeue_head(&sch->q); |
| 1040 | |
Eric Dumazet | 9190b3b | 2011-01-20 23:31:33 -0800 | [diff] [blame] | 1041 | if (likely(skb != NULL)) { |
John Fastabend | 25331d6 | 2014-09-28 11:53:29 -0700 | [diff] [blame] | 1042 | qdisc_qstats_backlog_dec(sch, skb); |
Eric Dumazet | 9190b3b | 2011-01-20 23:31:33 -0800 | [diff] [blame] | 1043 | qdisc_bstats_update(sch, skb); |
| 1044 | } |
Thomas Graf | 9972b25 | 2005-06-18 22:57:26 -0700 | [diff] [blame] | 1045 | |
| 1046 | return skb; |
| 1047 | } |
| 1048 | |
Eric Dumazet | 520ac30 | 2016-06-21 23:16:49 -0700 | [diff] [blame] | 1049 | /* Instead of calling kfree_skb() while root qdisc lock is held, |
| 1050 | * queue the skb for future freeing at end of __dev_xmit_skb() |
| 1051 | */ |
| 1052 | static inline void __qdisc_drop(struct sk_buff *skb, struct sk_buff **to_free) |
| 1053 | { |
| 1054 | skb->next = *to_free; |
| 1055 | *to_free = skb; |
| 1056 | } |
| 1057 | |
Alexey Kodanev | 35d889d | 2018-03-05 20:52:54 +0300 | [diff] [blame] | 1058 | static inline void __qdisc_drop_all(struct sk_buff *skb, |
| 1059 | struct sk_buff **to_free) |
| 1060 | { |
| 1061 | if (skb->prev) |
| 1062 | skb->prev->next = *to_free; |
| 1063 | else |
| 1064 | skb->next = *to_free; |
| 1065 | *to_free = skb; |
| 1066 | } |
| 1067 | |
Hagen Paul Pfeifer | 57dbb2d | 2010-01-24 12:30:59 +0000 | [diff] [blame] | 1068 | static inline unsigned int __qdisc_queue_drop_head(struct Qdisc *sch, |
Florian Westphal | 48da34b | 2016-09-18 00:57:34 +0200 | [diff] [blame] | 1069 | struct qdisc_skb_head *qh, |
Eric Dumazet | 520ac30 | 2016-06-21 23:16:49 -0700 | [diff] [blame] | 1070 | struct sk_buff **to_free) |
Hagen Paul Pfeifer | 57dbb2d | 2010-01-24 12:30:59 +0000 | [diff] [blame] | 1071 | { |
Florian Westphal | 48da34b | 2016-09-18 00:57:34 +0200 | [diff] [blame] | 1072 | struct sk_buff *skb = __qdisc_dequeue_head(qh); |
Hagen Paul Pfeifer | 57dbb2d | 2010-01-24 12:30:59 +0000 | [diff] [blame] | 1073 | |
| 1074 | if (likely(skb != NULL)) { |
| 1075 | unsigned int len = qdisc_pkt_len(skb); |
Eric Dumazet | 520ac30 | 2016-06-21 23:16:49 -0700 | [diff] [blame] | 1076 | |
John Fastabend | 25331d6 | 2014-09-28 11:53:29 -0700 | [diff] [blame] | 1077 | qdisc_qstats_backlog_dec(sch, skb); |
Eric Dumazet | 520ac30 | 2016-06-21 23:16:49 -0700 | [diff] [blame] | 1078 | __qdisc_drop(skb, to_free); |
Hagen Paul Pfeifer | 57dbb2d | 2010-01-24 12:30:59 +0000 | [diff] [blame] | 1079 | return len; |
| 1080 | } |
| 1081 | |
| 1082 | return 0; |
| 1083 | } |
| 1084 | |
Patrick McHardy | 48a8f51 | 2008-10-31 00:44:18 -0700 | [diff] [blame] | 1085 | static inline struct sk_buff *qdisc_peek_head(struct Qdisc *sch) |
| 1086 | { |
Florian Westphal | 48da34b | 2016-09-18 00:57:34 +0200 | [diff] [blame] | 1087 | const struct qdisc_skb_head *qh = &sch->q; |
| 1088 | |
| 1089 | return qh->head; |
Patrick McHardy | 48a8f51 | 2008-10-31 00:44:18 -0700 | [diff] [blame] | 1090 | } |
| 1091 | |
Jarek Poplawski | 77be155 | 2008-10-31 00:47:01 -0700 | [diff] [blame] | 1092 | /* generic pseudo peek method for non-work-conserving qdisc */ |
| 1093 | static inline struct sk_buff *qdisc_peek_dequeued(struct Qdisc *sch) |
| 1094 | { |
John Fastabend | a53851e | 2017-12-07 09:55:45 -0800 | [diff] [blame] | 1095 | struct sk_buff *skb = skb_peek(&sch->gso_skb); |
| 1096 | |
Jarek Poplawski | 77be155 | 2008-10-31 00:47:01 -0700 | [diff] [blame] | 1097 | /* we can reuse ->gso_skb because peek isn't called for root qdiscs */ |
John Fastabend | a53851e | 2017-12-07 09:55:45 -0800 | [diff] [blame] | 1098 | if (!skb) { |
| 1099 | skb = sch->dequeue(sch); |
| 1100 | |
| 1101 | if (skb) { |
| 1102 | __skb_queue_head(&sch->gso_skb, skb); |
Jarek Poplawski | 61c9eaf | 2008-11-05 16:02:34 -0800 | [diff] [blame] | 1103 | /* it's still part of the queue */ |
John Fastabend | a53851e | 2017-12-07 09:55:45 -0800 | [diff] [blame] | 1104 | qdisc_qstats_backlog_inc(sch, skb); |
Jarek Poplawski | 61c9eaf | 2008-11-05 16:02:34 -0800 | [diff] [blame] | 1105 | sch->q.qlen++; |
WANG Cong | a27758f | 2016-06-03 15:05:57 -0700 | [diff] [blame] | 1106 | } |
Jarek Poplawski | 61c9eaf | 2008-11-05 16:02:34 -0800 | [diff] [blame] | 1107 | } |
Jarek Poplawski | 77be155 | 2008-10-31 00:47:01 -0700 | [diff] [blame] | 1108 | |
John Fastabend | a53851e | 2017-12-07 09:55:45 -0800 | [diff] [blame] | 1109 | return skb; |
Jarek Poplawski | 77be155 | 2008-10-31 00:47:01 -0700 | [diff] [blame] | 1110 | } |
| 1111 | |
Paolo Abeni | 8a53e61 | 2019-04-10 14:32:40 +0200 | [diff] [blame] | 1112 | static inline void qdisc_update_stats_at_dequeue(struct Qdisc *sch, |
| 1113 | struct sk_buff *skb) |
| 1114 | { |
| 1115 | if (qdisc_is_percpu_stats(sch)) { |
| 1116 | qdisc_qstats_cpu_backlog_dec(sch, skb); |
| 1117 | qdisc_bstats_cpu_update(sch, skb); |
Paolo Abeni | 73eb628 | 2019-04-10 14:32:41 +0200 | [diff] [blame] | 1118 | qdisc_qstats_cpu_qlen_dec(sch); |
Paolo Abeni | 8a53e61 | 2019-04-10 14:32:40 +0200 | [diff] [blame] | 1119 | } else { |
| 1120 | qdisc_qstats_backlog_dec(sch, skb); |
| 1121 | qdisc_bstats_update(sch, skb); |
| 1122 | sch->q.qlen--; |
| 1123 | } |
| 1124 | } |
| 1125 | |
| 1126 | static inline void qdisc_update_stats_at_enqueue(struct Qdisc *sch, |
| 1127 | unsigned int pkt_len) |
| 1128 | { |
| 1129 | if (qdisc_is_percpu_stats(sch)) { |
Paolo Abeni | 73eb628 | 2019-04-10 14:32:41 +0200 | [diff] [blame] | 1130 | qdisc_qstats_cpu_qlen_inc(sch); |
Paolo Abeni | 8a53e61 | 2019-04-10 14:32:40 +0200 | [diff] [blame] | 1131 | this_cpu_add(sch->cpu_qstats->backlog, pkt_len); |
| 1132 | } else { |
| 1133 | sch->qstats.backlog += pkt_len; |
| 1134 | sch->q.qlen++; |
| 1135 | } |
| 1136 | } |
| 1137 | |
Jarek Poplawski | 77be155 | 2008-10-31 00:47:01 -0700 | [diff] [blame] | 1138 | /* use instead of qdisc->dequeue() for all qdiscs queried with ->peek() */ |
| 1139 | static inline struct sk_buff *qdisc_dequeue_peeked(struct Qdisc *sch) |
| 1140 | { |
John Fastabend | a53851e | 2017-12-07 09:55:45 -0800 | [diff] [blame] | 1141 | struct sk_buff *skb = skb_peek(&sch->gso_skb); |
Jarek Poplawski | 77be155 | 2008-10-31 00:47:01 -0700 | [diff] [blame] | 1142 | |
Jarek Poplawski | 61c9eaf | 2008-11-05 16:02:34 -0800 | [diff] [blame] | 1143 | if (skb) { |
John Fastabend | a53851e | 2017-12-07 09:55:45 -0800 | [diff] [blame] | 1144 | skb = __skb_dequeue(&sch->gso_skb); |
Paolo Abeni | 9c01c9f | 2019-04-10 14:32:39 +0200 | [diff] [blame] | 1145 | if (qdisc_is_percpu_stats(sch)) { |
| 1146 | qdisc_qstats_cpu_backlog_dec(sch, skb); |
Paolo Abeni | 73eb628 | 2019-04-10 14:32:41 +0200 | [diff] [blame] | 1147 | qdisc_qstats_cpu_qlen_dec(sch); |
Paolo Abeni | 9c01c9f | 2019-04-10 14:32:39 +0200 | [diff] [blame] | 1148 | } else { |
| 1149 | qdisc_qstats_backlog_dec(sch, skb); |
| 1150 | sch->q.qlen--; |
| 1151 | } |
Jarek Poplawski | 61c9eaf | 2008-11-05 16:02:34 -0800 | [diff] [blame] | 1152 | } else { |
Jarek Poplawski | 77be155 | 2008-10-31 00:47:01 -0700 | [diff] [blame] | 1153 | skb = sch->dequeue(sch); |
Jarek Poplawski | 61c9eaf | 2008-11-05 16:02:34 -0800 | [diff] [blame] | 1154 | } |
Jarek Poplawski | 77be155 | 2008-10-31 00:47:01 -0700 | [diff] [blame] | 1155 | |
| 1156 | return skb; |
| 1157 | } |
| 1158 | |
Florian Westphal | 48da34b | 2016-09-18 00:57:34 +0200 | [diff] [blame] | 1159 | static inline void __qdisc_reset_queue(struct qdisc_skb_head *qh) |
Thomas Graf | 9972b25 | 2005-06-18 22:57:26 -0700 | [diff] [blame] | 1160 | { |
| 1161 | /* |
| 1162 | * We do not know the backlog in bytes of this list, it |
| 1163 | * is up to the caller to correct it |
| 1164 | */ |
Florian Westphal | 48da34b | 2016-09-18 00:57:34 +0200 | [diff] [blame] | 1165 | ASSERT_RTNL(); |
| 1166 | if (qh->qlen) { |
| 1167 | rtnl_kfree_skbs(qh->head, qh->tail); |
| 1168 | |
| 1169 | qh->head = NULL; |
| 1170 | qh->tail = NULL; |
| 1171 | qh->qlen = 0; |
Eric Dumazet | 1b5c549 | 2016-06-13 20:21:50 -0700 | [diff] [blame] | 1172 | } |
Thomas Graf | 9972b25 | 2005-06-18 22:57:26 -0700 | [diff] [blame] | 1173 | } |
| 1174 | |
| 1175 | static inline void qdisc_reset_queue(struct Qdisc *sch) |
| 1176 | { |
Eric Dumazet | 1b5c549 | 2016-06-13 20:21:50 -0700 | [diff] [blame] | 1177 | __qdisc_reset_queue(&sch->q); |
Thomas Graf | 9972b25 | 2005-06-18 22:57:26 -0700 | [diff] [blame] | 1178 | sch->qstats.backlog = 0; |
| 1179 | } |
| 1180 | |
WANG Cong | 86a7996 | 2016-02-25 14:55:00 -0800 | [diff] [blame] | 1181 | static inline struct Qdisc *qdisc_replace(struct Qdisc *sch, struct Qdisc *new, |
| 1182 | struct Qdisc **pold) |
| 1183 | { |
| 1184 | struct Qdisc *old; |
| 1185 | |
| 1186 | sch_tree_lock(sch); |
| 1187 | old = *pold; |
| 1188 | *pold = new; |
Paolo Abeni | e5f0e8f | 2019-03-28 16:53:13 +0100 | [diff] [blame] | 1189 | if (old != NULL) |
Alexander Ovechkin | 938e0fc | 2021-02-01 23:00:49 +0300 | [diff] [blame] | 1190 | qdisc_purge_queue(old); |
WANG Cong | 86a7996 | 2016-02-25 14:55:00 -0800 | [diff] [blame] | 1191 | sch_tree_unlock(sch); |
| 1192 | |
| 1193 | return old; |
| 1194 | } |
| 1195 | |
Eric Dumazet | 1b5c549 | 2016-06-13 20:21:50 -0700 | [diff] [blame] | 1196 | static inline void rtnl_qdisc_drop(struct sk_buff *skb, struct Qdisc *sch) |
| 1197 | { |
| 1198 | rtnl_kfree_skbs(skb, skb); |
| 1199 | qdisc_qstats_drop(sch); |
| 1200 | } |
| 1201 | |
John Fastabend | 40bd036 | 2017-12-07 09:55:07 -0800 | [diff] [blame] | 1202 | static inline int qdisc_drop_cpu(struct sk_buff *skb, struct Qdisc *sch, |
| 1203 | struct sk_buff **to_free) |
| 1204 | { |
| 1205 | __qdisc_drop(skb, to_free); |
| 1206 | qdisc_qstats_cpu_drop(sch); |
| 1207 | |
| 1208 | return NET_XMIT_DROP; |
| 1209 | } |
Eric Dumazet | 520ac30 | 2016-06-21 23:16:49 -0700 | [diff] [blame] | 1210 | |
| 1211 | static inline int qdisc_drop(struct sk_buff *skb, struct Qdisc *sch, |
| 1212 | struct sk_buff **to_free) |
Thomas Graf | 9972b25 | 2005-06-18 22:57:26 -0700 | [diff] [blame] | 1213 | { |
Eric Dumazet | 520ac30 | 2016-06-21 23:16:49 -0700 | [diff] [blame] | 1214 | __qdisc_drop(skb, to_free); |
John Fastabend | 25331d6 | 2014-09-28 11:53:29 -0700 | [diff] [blame] | 1215 | qdisc_qstats_drop(sch); |
Thomas Graf | 9972b25 | 2005-06-18 22:57:26 -0700 | [diff] [blame] | 1216 | |
| 1217 | return NET_XMIT_DROP; |
| 1218 | } |
| 1219 | |
Alexey Kodanev | 35d889d | 2018-03-05 20:52:54 +0300 | [diff] [blame] | 1220 | static inline int qdisc_drop_all(struct sk_buff *skb, struct Qdisc *sch, |
| 1221 | struct sk_buff **to_free) |
| 1222 | { |
| 1223 | __qdisc_drop_all(skb, to_free); |
| 1224 | qdisc_qstats_drop(sch); |
| 1225 | |
| 1226 | return NET_XMIT_DROP; |
| 1227 | } |
| 1228 | |
Jesper Dangaard Brouer | e9bef55 | 2007-09-12 16:35:24 +0200 | [diff] [blame] | 1229 | /* Length to Time (L2T) lookup in a qdisc_rate_table, to determine how |
| 1230 | long it will take to send a packet given its size. |
| 1231 | */ |
| 1232 | static inline u32 qdisc_l2t(struct qdisc_rate_table* rtab, unsigned int pktlen) |
| 1233 | { |
Jesper Dangaard Brouer | e08b099 | 2007-09-12 16:36:28 +0200 | [diff] [blame] | 1234 | int slot = pktlen + rtab->rate.cell_align + rtab->rate.overhead; |
| 1235 | if (slot < 0) |
| 1236 | slot = 0; |
Jesper Dangaard Brouer | e9bef55 | 2007-09-12 16:35:24 +0200 | [diff] [blame] | 1237 | slot >>= rtab->rate.cell_log; |
| 1238 | if (slot > 255) |
Eric Dumazet | a02cec2 | 2010-09-22 20:43:57 +0000 | [diff] [blame] | 1239 | return rtab->data[255]*(slot >> 8) + rtab->data[slot & 0xFF]; |
Jesper Dangaard Brouer | e9bef55 | 2007-09-12 16:35:24 +0200 | [diff] [blame] | 1240 | return rtab->data[slot]; |
| 1241 | } |
| 1242 | |
Jiri Pirko | 292f1c7 | 2013-02-12 00:12:03 +0000 | [diff] [blame] | 1243 | struct psched_ratecfg { |
Eric Dumazet | 130d3d6 | 2013-06-06 13:56:19 -0700 | [diff] [blame] | 1244 | u64 rate_bytes_ps; /* bytes per second */ |
Eric Dumazet | 01cb71d | 2013-06-02 13:55:05 +0000 | [diff] [blame] | 1245 | u32 mult; |
| 1246 | u16 overhead; |
Kevin Bracey | fb80445 | 2022-01-12 19:02:10 +0200 | [diff] [blame] | 1247 | u16 mpu; |
Jesper Dangaard Brouer | 8a8e3d8 | 2013-08-14 23:47:11 +0200 | [diff] [blame] | 1248 | u8 linklayer; |
Eric Dumazet | 01cb71d | 2013-06-02 13:55:05 +0000 | [diff] [blame] | 1249 | u8 shift; |
Jiri Pirko | 292f1c7 | 2013-02-12 00:12:03 +0000 | [diff] [blame] | 1250 | }; |
| 1251 | |
| 1252 | static inline u64 psched_l2t_ns(const struct psched_ratecfg *r, |
| 1253 | unsigned int len) |
| 1254 | { |
Jesper Dangaard Brouer | 8a8e3d8 | 2013-08-14 23:47:11 +0200 | [diff] [blame] | 1255 | len += r->overhead; |
| 1256 | |
Kevin Bracey | fb80445 | 2022-01-12 19:02:10 +0200 | [diff] [blame] | 1257 | if (len < r->mpu) |
| 1258 | len = r->mpu; |
| 1259 | |
Jesper Dangaard Brouer | 8a8e3d8 | 2013-08-14 23:47:11 +0200 | [diff] [blame] | 1260 | if (unlikely(r->linklayer == TC_LINKLAYER_ATM)) |
| 1261 | return ((u64)(DIV_ROUND_UP(len,48)*53) * r->mult) >> r->shift; |
| 1262 | |
| 1263 | return ((u64)len * r->mult) >> r->shift; |
Jiri Pirko | 292f1c7 | 2013-02-12 00:12:03 +0000 | [diff] [blame] | 1264 | } |
| 1265 | |
Joe Perches | 5c15257 | 2013-07-30 22:47:13 -0700 | [diff] [blame] | 1266 | void psched_ratecfg_precompute(struct psched_ratecfg *r, |
Eric Dumazet | 3e1e3aa | 2013-09-19 09:10:03 -0700 | [diff] [blame] | 1267 | const struct tc_ratespec *conf, |
| 1268 | u64 rate64); |
Jiri Pirko | 292f1c7 | 2013-02-12 00:12:03 +0000 | [diff] [blame] | 1269 | |
Eric Dumazet | 01cb71d | 2013-06-02 13:55:05 +0000 | [diff] [blame] | 1270 | static inline void psched_ratecfg_getrate(struct tc_ratespec *res, |
| 1271 | const struct psched_ratecfg *r) |
Jiri Pirko | 292f1c7 | 2013-02-12 00:12:03 +0000 | [diff] [blame] | 1272 | { |
Eric Dumazet | 01cb71d | 2013-06-02 13:55:05 +0000 | [diff] [blame] | 1273 | memset(res, 0, sizeof(*res)); |
Eric Dumazet | 3e1e3aa | 2013-09-19 09:10:03 -0700 | [diff] [blame] | 1274 | |
| 1275 | /* legacy struct tc_ratespec has a 32bit @rate field |
| 1276 | * Qdisc using 64bit rate should add new attributes |
| 1277 | * in order to maintain compatibility. |
| 1278 | */ |
| 1279 | res->rate = min_t(u64, r->rate_bytes_ps, ~0U); |
| 1280 | |
Eric Dumazet | 01cb71d | 2013-06-02 13:55:05 +0000 | [diff] [blame] | 1281 | res->overhead = r->overhead; |
Kevin Bracey | fb80445 | 2022-01-12 19:02:10 +0200 | [diff] [blame] | 1282 | res->mpu = r->mpu; |
Jesper Dangaard Brouer | 8a8e3d8 | 2013-08-14 23:47:11 +0200 | [diff] [blame] | 1283 | res->linklayer = (r->linklayer & TC_LINKLAYER_MASK); |
Jiri Pirko | 292f1c7 | 2013-02-12 00:12:03 +0000 | [diff] [blame] | 1284 | } |
| 1285 | |
Baowen Zheng | 2ffe039 | 2021-03-12 15:08:31 +0100 | [diff] [blame] | 1286 | struct psched_pktrate { |
| 1287 | u64 rate_pkts_ps; /* packets per second */ |
| 1288 | u32 mult; |
| 1289 | u8 shift; |
| 1290 | }; |
| 1291 | |
| 1292 | static inline u64 psched_pkt2t_ns(const struct psched_pktrate *r, |
| 1293 | unsigned int pkt_num) |
| 1294 | { |
| 1295 | return ((u64)pkt_num * r->mult) >> r->shift; |
| 1296 | } |
| 1297 | |
| 1298 | void psched_ppscfg_precompute(struct psched_pktrate *r, u64 pktrate64); |
| 1299 | |
Jiri Pirko | 4620940 | 2017-11-03 11:46:25 +0100 | [diff] [blame] | 1300 | /* Mini Qdisc serves for specific needs of ingress/clsact Qdisc. |
| 1301 | * The fast path only needs to access filter list and to update stats |
| 1302 | */ |
| 1303 | struct mini_Qdisc { |
| 1304 | struct tcf_proto *filter_list; |
Paul Blakey | 7d17c54 | 2020-02-16 12:01:22 +0200 | [diff] [blame] | 1305 | struct tcf_block *block; |
Ahmed S. Darwish | 50dc9a8 | 2021-10-16 10:49:09 +0200 | [diff] [blame] | 1306 | struct gnet_stats_basic_sync __percpu *cpu_bstats; |
Jiri Pirko | 4620940 | 2017-11-03 11:46:25 +0100 | [diff] [blame] | 1307 | struct gnet_stats_queue __percpu *cpu_qstats; |
Seth Forshee | 2674638 | 2021-10-26 08:06:59 -0500 | [diff] [blame] | 1308 | unsigned long rcu_state; |
Jiri Pirko | 4620940 | 2017-11-03 11:46:25 +0100 | [diff] [blame] | 1309 | }; |
| 1310 | |
| 1311 | static inline void mini_qdisc_bstats_cpu_update(struct mini_Qdisc *miniq, |
| 1312 | const struct sk_buff *skb) |
| 1313 | { |
Ahmed S. Darwish | 50dc9a8 | 2021-10-16 10:49:09 +0200 | [diff] [blame] | 1314 | bstats_update(this_cpu_ptr(miniq->cpu_bstats), skb); |
Jiri Pirko | 4620940 | 2017-11-03 11:46:25 +0100 | [diff] [blame] | 1315 | } |
| 1316 | |
| 1317 | static inline void mini_qdisc_qstats_cpu_drop(struct mini_Qdisc *miniq) |
| 1318 | { |
| 1319 | this_cpu_inc(miniq->cpu_qstats->drops); |
| 1320 | } |
| 1321 | |
| 1322 | struct mini_Qdisc_pair { |
| 1323 | struct mini_Qdisc miniq1; |
| 1324 | struct mini_Qdisc miniq2; |
| 1325 | struct mini_Qdisc __rcu **p_miniq; |
| 1326 | }; |
| 1327 | |
| 1328 | void mini_qdisc_pair_swap(struct mini_Qdisc_pair *miniqp, |
| 1329 | struct tcf_proto *tp_head); |
| 1330 | void mini_qdisc_pair_init(struct mini_Qdisc_pair *miniqp, struct Qdisc *qdisc, |
| 1331 | struct mini_Qdisc __rcu **p_miniq); |
Paul Blakey | 7d17c54 | 2020-02-16 12:01:22 +0200 | [diff] [blame] | 1332 | void mini_qdisc_pair_block_init(struct mini_Qdisc_pair *miniqp, |
| 1333 | struct tcf_block *block); |
Jiri Pirko | 4620940 | 2017-11-03 11:46:25 +0100 | [diff] [blame] | 1334 | |
Jakub Kicinski | f7116fb | 2021-09-17 06:55:06 -0700 | [diff] [blame] | 1335 | void mq_change_real_num_tx(struct Qdisc *sch, unsigned int new_real_tx); |
| 1336 | |
wenxu | c129412 | 2020-11-25 12:01:23 +0800 | [diff] [blame] | 1337 | int sch_frag_xmit_hook(struct sk_buff *skb, int (*xmit)(struct sk_buff *skb)); |
Paolo Abeni | cd11b164 | 2018-07-30 14:30:44 +0200 | [diff] [blame] | 1338 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1339 | #endif |