Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * net/sched/sch_generic.c Generic packet scheduler routines. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; either version |
| 7 | * 2 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> |
| 10 | * Jamal Hadi Salim, <hadi@cyberus.ca> 990601 |
| 11 | * - Ingress support |
| 12 | */ |
| 13 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/bitops.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/module.h> |
| 16 | #include <linux/types.h> |
| 17 | #include <linux/kernel.h> |
| 18 | #include <linux/sched.h> |
| 19 | #include <linux/string.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/errno.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/netdevice.h> |
| 22 | #include <linux/skbuff.h> |
| 23 | #include <linux/rtnetlink.h> |
| 24 | #include <linux/init.h> |
| 25 | #include <linux/rcupdate.h> |
| 26 | #include <linux/list.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 27 | #include <linux/slab.h> |
nikolay@redhat.com | 07ce76a | 2013-08-03 22:07:47 +0200 | [diff] [blame] | 28 | #include <linux/if_vlan.h> |
John Fastabend | c5ad119 | 2017-12-07 09:58:19 -0800 | [diff] [blame] | 29 | #include <linux/skb_array.h> |
Chris Dion | 32d3e51 | 2017-12-06 10:50:28 -0500 | [diff] [blame] | 30 | #include <linux/if_macvlan.h> |
Jiri Pirko | 292f1c7 | 2013-02-12 00:12:03 +0000 | [diff] [blame] | 31 | #include <net/sch_generic.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <net/pkt_sched.h> |
Eric Dumazet | 7fee226 | 2010-05-11 23:19:48 +0000 | [diff] [blame] | 33 | #include <net/dst.h> |
Jesper Dangaard Brouer | e543002 | 2017-08-15 21:11:03 +0200 | [diff] [blame] | 34 | #include <trace/events/qdisc.h> |
Steffen Klassert | f53c723 | 2017-12-20 10:41:36 +0100 | [diff] [blame] | 35 | #include <net/xfrm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
stephen hemminger | 34aedd3 | 2013-08-31 10:15:33 -0700 | [diff] [blame] | 37 | /* Qdisc to use by default */ |
| 38 | const struct Qdisc_ops *default_qdisc_ops = &pfifo_fast_ops; |
| 39 | EXPORT_SYMBOL(default_qdisc_ops); |
| 40 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | /* Main transmission queue. */ |
| 42 | |
Patrick McHardy | 0463d4a | 2007-04-16 17:02:10 -0700 | [diff] [blame] | 43 | /* Modifications to data participating in scheduling must be protected with |
David S. Miller | 5fb6622 | 2008-08-02 20:02:43 -0700 | [diff] [blame] | 44 | * qdisc_lock(qdisc) spinlock. |
Patrick McHardy | 0463d4a | 2007-04-16 17:02:10 -0700 | [diff] [blame] | 45 | * |
| 46 | * The idea is the following: |
David S. Miller | c7e4f3b | 2008-07-16 03:22:39 -0700 | [diff] [blame] | 47 | * - enqueue, dequeue are serialized via qdisc root lock |
| 48 | * - ingress filtering is also serialized via qdisc root lock |
Patrick McHardy | 0463d4a | 2007-04-16 17:02:10 -0700 | [diff] [blame] | 49 | * - updates to tree and tree walking are only done under the rtnl mutex. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | */ |
John Fastabend | 70e57d5 | 2017-12-07 09:56:23 -0800 | [diff] [blame] | 51 | |
| 52 | static inline struct sk_buff *__skb_dequeue_bad_txq(struct Qdisc *q) |
| 53 | { |
| 54 | const struct netdev_queue *txq = q->dev_queue; |
| 55 | spinlock_t *lock = NULL; |
| 56 | struct sk_buff *skb; |
| 57 | |
| 58 | if (q->flags & TCQ_F_NOLOCK) { |
| 59 | lock = qdisc_lock(q); |
| 60 | spin_lock(lock); |
| 61 | } |
| 62 | |
| 63 | skb = skb_peek(&q->skb_bad_txq); |
| 64 | if (skb) { |
| 65 | /* check the reason of requeuing without tx lock first */ |
| 66 | txq = skb_get_tx_queue(txq->dev, skb); |
| 67 | if (!netif_xmit_frozen_or_stopped(txq)) { |
| 68 | skb = __skb_dequeue(&q->skb_bad_txq); |
| 69 | if (qdisc_is_percpu_stats(q)) { |
| 70 | qdisc_qstats_cpu_backlog_dec(q, skb); |
Eric Dumazet | 46b1c18 | 2019-02-28 12:55:43 -0800 | [diff] [blame] | 71 | qdisc_qstats_atomic_qlen_dec(q); |
John Fastabend | 70e57d5 | 2017-12-07 09:56:23 -0800 | [diff] [blame] | 72 | } else { |
| 73 | qdisc_qstats_backlog_dec(q, skb); |
| 74 | q->q.qlen--; |
| 75 | } |
| 76 | } else { |
| 77 | skb = NULL; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | if (lock) |
| 82 | spin_unlock(lock); |
| 83 | |
| 84 | return skb; |
| 85 | } |
| 86 | |
| 87 | static inline struct sk_buff *qdisc_dequeue_skb_bad_txq(struct Qdisc *q) |
| 88 | { |
| 89 | struct sk_buff *skb = skb_peek(&q->skb_bad_txq); |
| 90 | |
| 91 | if (unlikely(skb)) |
| 92 | skb = __skb_dequeue_bad_txq(q); |
| 93 | |
| 94 | return skb; |
| 95 | } |
| 96 | |
| 97 | static inline void qdisc_enqueue_skb_bad_txq(struct Qdisc *q, |
| 98 | struct sk_buff *skb) |
| 99 | { |
| 100 | spinlock_t *lock = NULL; |
| 101 | |
| 102 | if (q->flags & TCQ_F_NOLOCK) { |
| 103 | lock = qdisc_lock(q); |
| 104 | spin_lock(lock); |
| 105 | } |
| 106 | |
| 107 | __skb_queue_tail(&q->skb_bad_txq, skb); |
| 108 | |
Eric Dumazet | cce6294c | 2018-03-14 18:53:00 -0700 | [diff] [blame] | 109 | if (qdisc_is_percpu_stats(q)) { |
| 110 | qdisc_qstats_cpu_backlog_inc(q, skb); |
Eric Dumazet | 46b1c18 | 2019-02-28 12:55:43 -0800 | [diff] [blame] | 111 | qdisc_qstats_atomic_qlen_inc(q); |
Eric Dumazet | cce6294c | 2018-03-14 18:53:00 -0700 | [diff] [blame] | 112 | } else { |
| 113 | qdisc_qstats_backlog_inc(q, skb); |
| 114 | q->q.qlen++; |
| 115 | } |
| 116 | |
John Fastabend | 70e57d5 | 2017-12-07 09:56:23 -0800 | [diff] [blame] | 117 | if (lock) |
| 118 | spin_unlock(lock); |
| 119 | } |
| 120 | |
Paolo Abeni | 9c01c9f | 2019-04-10 14:32:39 +0200 | [diff] [blame] | 121 | static inline void dev_requeue_skb(struct sk_buff *skb, struct Qdisc *q) |
Jamal Hadi Salim | c716a81 | 2007-06-10 17:31:24 -0700 | [diff] [blame] | 122 | { |
Paolo Abeni | 9c01c9f | 2019-04-10 14:32:39 +0200 | [diff] [blame] | 123 | spinlock_t *lock = NULL; |
Wei Yongjun | 9540d97 | 2017-12-27 17:05:52 +0800 | [diff] [blame] | 124 | |
Paolo Abeni | 9c01c9f | 2019-04-10 14:32:39 +0200 | [diff] [blame] | 125 | if (q->flags & TCQ_F_NOLOCK) { |
| 126 | lock = qdisc_lock(q); |
| 127 | spin_lock(lock); |
Wei Yongjun | 9540d97 | 2017-12-27 17:05:52 +0800 | [diff] [blame] | 128 | } |
Jarek Poplawski | 6252352 | 2008-10-06 10:41:50 -0700 | [diff] [blame] | 129 | |
Wei Yongjun | 9540d97 | 2017-12-27 17:05:52 +0800 | [diff] [blame] | 130 | while (skb) { |
| 131 | struct sk_buff *next = skb->next; |
| 132 | |
| 133 | __skb_queue_tail(&q->gso_skb, skb); |
| 134 | |
Paolo Abeni | 9c01c9f | 2019-04-10 14:32:39 +0200 | [diff] [blame] | 135 | /* it's still part of the queue */ |
| 136 | if (qdisc_is_percpu_stats(q)) { |
| 137 | qdisc_qstats_cpu_requeues_inc(q); |
| 138 | qdisc_qstats_cpu_backlog_inc(q, skb); |
| 139 | qdisc_qstats_atomic_qlen_inc(q); |
| 140 | } else { |
| 141 | q->qstats.requeues++; |
| 142 | qdisc_qstats_backlog_inc(q, skb); |
| 143 | q->q.qlen++; |
| 144 | } |
Wei Yongjun | 9540d97 | 2017-12-27 17:05:52 +0800 | [diff] [blame] | 145 | |
| 146 | skb = next; |
| 147 | } |
Paolo Abeni | 9c01c9f | 2019-04-10 14:32:39 +0200 | [diff] [blame] | 148 | if (lock) |
| 149 | spin_unlock(lock); |
John Fastabend | a53851e | 2017-12-07 09:55:45 -0800 | [diff] [blame] | 150 | __netif_schedule(q); |
John Fastabend | a53851e | 2017-12-07 09:55:45 -0800 | [diff] [blame] | 151 | } |
| 152 | |
Eric Dumazet | 55a93b3 | 2014-10-03 15:31:07 -0700 | [diff] [blame] | 153 | static void try_bulk_dequeue_skb(struct Qdisc *q, |
| 154 | struct sk_buff *skb, |
Jesper Dangaard Brouer | b8358d7 | 2014-10-09 12:18:10 +0200 | [diff] [blame] | 155 | const struct netdev_queue *txq, |
| 156 | int *packets) |
Jesper Dangaard Brouer | 5772e9a | 2014-10-01 22:35:59 +0200 | [diff] [blame] | 157 | { |
Eric Dumazet | 55a93b3 | 2014-10-03 15:31:07 -0700 | [diff] [blame] | 158 | int bytelimit = qdisc_avail_bulklimit(txq) - skb->len; |
Jesper Dangaard Brouer | 5772e9a | 2014-10-01 22:35:59 +0200 | [diff] [blame] | 159 | |
| 160 | while (bytelimit > 0) { |
Eric Dumazet | 55a93b3 | 2014-10-03 15:31:07 -0700 | [diff] [blame] | 161 | struct sk_buff *nskb = q->dequeue(q); |
| 162 | |
| 163 | if (!nskb) |
Jesper Dangaard Brouer | 5772e9a | 2014-10-01 22:35:59 +0200 | [diff] [blame] | 164 | break; |
| 165 | |
Eric Dumazet | 55a93b3 | 2014-10-03 15:31:07 -0700 | [diff] [blame] | 166 | bytelimit -= nskb->len; /* covers GSO len */ |
| 167 | skb->next = nskb; |
| 168 | skb = nskb; |
Jesper Dangaard Brouer | b8358d7 | 2014-10-09 12:18:10 +0200 | [diff] [blame] | 169 | (*packets)++; /* GSO counts as one pkt */ |
Jesper Dangaard Brouer | 5772e9a | 2014-10-01 22:35:59 +0200 | [diff] [blame] | 170 | } |
David S. Miller | a8305bf | 2018-07-29 20:42:53 -0700 | [diff] [blame] | 171 | skb_mark_not_on_list(skb); |
Jesper Dangaard Brouer | 5772e9a | 2014-10-01 22:35:59 +0200 | [diff] [blame] | 172 | } |
| 173 | |
Eric Dumazet | 4d202a0 | 2016-06-21 23:16:52 -0700 | [diff] [blame] | 174 | /* This variant of try_bulk_dequeue_skb() makes sure |
| 175 | * all skbs in the chain are for the same txq |
| 176 | */ |
| 177 | static void try_bulk_dequeue_skb_slow(struct Qdisc *q, |
| 178 | struct sk_buff *skb, |
| 179 | int *packets) |
| 180 | { |
| 181 | int mapping = skb_get_queue_mapping(skb); |
| 182 | struct sk_buff *nskb; |
| 183 | int cnt = 0; |
| 184 | |
| 185 | do { |
| 186 | nskb = q->dequeue(q); |
| 187 | if (!nskb) |
| 188 | break; |
| 189 | if (unlikely(skb_get_queue_mapping(nskb) != mapping)) { |
John Fastabend | 70e57d5 | 2017-12-07 09:56:23 -0800 | [diff] [blame] | 190 | qdisc_enqueue_skb_bad_txq(q, nskb); |
Eric Dumazet | 4d202a0 | 2016-06-21 23:16:52 -0700 | [diff] [blame] | 191 | break; |
| 192 | } |
| 193 | skb->next = nskb; |
| 194 | skb = nskb; |
| 195 | } while (++cnt < 8); |
| 196 | (*packets) += cnt; |
David S. Miller | a8305bf | 2018-07-29 20:42:53 -0700 | [diff] [blame] | 197 | skb_mark_not_on_list(skb); |
Eric Dumazet | 4d202a0 | 2016-06-21 23:16:52 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Jesper Dangaard Brouer | 5772e9a | 2014-10-01 22:35:59 +0200 | [diff] [blame] | 200 | /* Note that dequeue_skb can possibly return a SKB list (via skb->next). |
| 201 | * A requeued skb (via q->gso_skb) can also be a SKB list. |
| 202 | */ |
Jesper Dangaard Brouer | b8358d7 | 2014-10-09 12:18:10 +0200 | [diff] [blame] | 203 | static struct sk_buff *dequeue_skb(struct Qdisc *q, bool *validate, |
| 204 | int *packets) |
Jamal Hadi Salim | c716a81 | 2007-06-10 17:31:24 -0700 | [diff] [blame] | 205 | { |
Eric Dumazet | 1abbe13 | 2012-12-11 15:54:33 +0000 | [diff] [blame] | 206 | const struct netdev_queue *txq = q->dev_queue; |
John Fastabend | fd8e8d1 | 2017-12-07 09:56:42 -0800 | [diff] [blame] | 207 | struct sk_buff *skb = NULL; |
Jarek Poplawski | 554794d | 2008-10-06 09:54:39 -0700 | [diff] [blame] | 208 | |
Jesper Dangaard Brouer | b8358d7 | 2014-10-09 12:18:10 +0200 | [diff] [blame] | 209 | *packets = 1; |
John Fastabend | a53851e | 2017-12-07 09:55:45 -0800 | [diff] [blame] | 210 | if (unlikely(!skb_queue_empty(&q->gso_skb))) { |
| 211 | spinlock_t *lock = NULL; |
| 212 | |
| 213 | if (q->flags & TCQ_F_NOLOCK) { |
| 214 | lock = qdisc_lock(q); |
| 215 | spin_lock(lock); |
| 216 | } |
| 217 | |
| 218 | skb = skb_peek(&q->gso_skb); |
| 219 | |
| 220 | /* skb may be null if another cpu pulls gso_skb off in between |
| 221 | * empty check and lock. |
| 222 | */ |
| 223 | if (!skb) { |
| 224 | if (lock) |
| 225 | spin_unlock(lock); |
| 226 | goto validate; |
| 227 | } |
| 228 | |
Eric Dumazet | 4d202a0 | 2016-06-21 23:16:52 -0700 | [diff] [blame] | 229 | /* skb in gso_skb were already validated */ |
| 230 | *validate = false; |
Steffen Klassert | f53c723 | 2017-12-20 10:41:36 +0100 | [diff] [blame] | 231 | if (xfrm_offload(skb)) |
| 232 | *validate = true; |
Jarek Poplawski | ebf0598 | 2008-09-22 22:16:23 -0700 | [diff] [blame] | 233 | /* check the reason of requeuing without tx lock first */ |
Daniel Borkmann | 10c51b5623 | 2014-08-27 11:11:27 +0200 | [diff] [blame] | 234 | txq = skb_get_tx_queue(txq->dev, skb); |
Tom Herbert | 73466498 | 2011-11-28 16:32:44 +0000 | [diff] [blame] | 235 | if (!netif_xmit_frozen_or_stopped(txq)) { |
John Fastabend | a53851e | 2017-12-07 09:55:45 -0800 | [diff] [blame] | 236 | skb = __skb_dequeue(&q->gso_skb); |
| 237 | if (qdisc_is_percpu_stats(q)) { |
| 238 | qdisc_qstats_cpu_backlog_dec(q, skb); |
Eric Dumazet | 46b1c18 | 2019-02-28 12:55:43 -0800 | [diff] [blame] | 239 | qdisc_qstats_atomic_qlen_dec(q); |
John Fastabend | a53851e | 2017-12-07 09:55:45 -0800 | [diff] [blame] | 240 | } else { |
| 241 | qdisc_qstats_backlog_dec(q, skb); |
| 242 | q->q.qlen--; |
| 243 | } |
| 244 | } else { |
Jarek Poplawski | ebf0598 | 2008-09-22 22:16:23 -0700 | [diff] [blame] | 245 | skb = NULL; |
John Fastabend | a53851e | 2017-12-07 09:55:45 -0800 | [diff] [blame] | 246 | } |
| 247 | if (lock) |
| 248 | spin_unlock(lock); |
Jesper Dangaard Brouer | e543002 | 2017-08-15 21:11:03 +0200 | [diff] [blame] | 249 | goto trace; |
Eric Dumazet | 4d202a0 | 2016-06-21 23:16:52 -0700 | [diff] [blame] | 250 | } |
John Fastabend | a53851e | 2017-12-07 09:55:45 -0800 | [diff] [blame] | 251 | validate: |
Eric Dumazet | 4d202a0 | 2016-06-21 23:16:52 -0700 | [diff] [blame] | 252 | *validate = true; |
John Fastabend | fd8e8d1 | 2017-12-07 09:56:42 -0800 | [diff] [blame] | 253 | |
| 254 | if ((q->flags & TCQ_F_ONETXQUEUE) && |
| 255 | netif_xmit_frozen_or_stopped(txq)) |
| 256 | return skb; |
| 257 | |
John Fastabend | 70e57d5 | 2017-12-07 09:56:23 -0800 | [diff] [blame] | 258 | skb = qdisc_dequeue_skb_bad_txq(q); |
| 259 | if (unlikely(skb)) |
| 260 | goto bulk; |
John Fastabend | fd8e8d1 | 2017-12-07 09:56:42 -0800 | [diff] [blame] | 261 | skb = q->dequeue(q); |
Eric Dumazet | 4d202a0 | 2016-06-21 23:16:52 -0700 | [diff] [blame] | 262 | if (skb) { |
| 263 | bulk: |
| 264 | if (qdisc_may_bulk(q)) |
| 265 | try_bulk_dequeue_skb(q, skb, txq, packets); |
| 266 | else |
| 267 | try_bulk_dequeue_skb_slow(q, skb, packets); |
Jarek Poplawski | ebf0598 | 2008-09-22 22:16:23 -0700 | [diff] [blame] | 268 | } |
Jesper Dangaard Brouer | e543002 | 2017-08-15 21:11:03 +0200 | [diff] [blame] | 269 | trace: |
| 270 | trace_qdisc_dequeue(q, txq, *packets, skb); |
Jamal Hadi Salim | c716a81 | 2007-06-10 17:31:24 -0700 | [diff] [blame] | 271 | return skb; |
| 272 | } |
| 273 | |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 274 | /* |
Jesper Dangaard Brouer | 10770bc | 2014-09-02 16:35:33 +0200 | [diff] [blame] | 275 | * Transmit possibly several skbs, and handle the return status as |
Eric Dumazet | f9eb8ae | 2016-06-06 09:37:15 -0700 | [diff] [blame] | 276 | * required. Owning running seqcount bit guarantees that |
Jesper Dangaard Brouer | 10770bc | 2014-09-02 16:35:33 +0200 | [diff] [blame] | 277 | * only one CPU can execute this function. |
Krishna Kumar | 6c1361a | 2007-06-24 19:56:09 -0700 | [diff] [blame] | 278 | * |
| 279 | * Returns to the caller: |
John Fastabend | 29b86cd | 2017-12-07 09:54:47 -0800 | [diff] [blame] | 280 | * false - hardware queue frozen backoff |
| 281 | * true - feel free to send more pkts |
Krishna Kumar | 6c1361a | 2007-06-24 19:56:09 -0700 | [diff] [blame] | 282 | */ |
John Fastabend | 29b86cd | 2017-12-07 09:54:47 -0800 | [diff] [blame] | 283 | bool sch_direct_xmit(struct sk_buff *skb, struct Qdisc *q, |
| 284 | struct net_device *dev, struct netdev_queue *txq, |
| 285 | spinlock_t *root_lock, bool validate) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | { |
Peter P Waskiewicz Jr | 5f1a485 | 2007-11-13 20:40:55 -0800 | [diff] [blame] | 287 | int ret = NETDEV_TX_BUSY; |
Steffen Klassert | f53c723 | 2017-12-20 10:41:36 +0100 | [diff] [blame] | 288 | bool again = false; |
David S. Miller | 7698b4f | 2008-07-16 01:42:40 -0700 | [diff] [blame] | 289 | |
| 290 | /* And release qdisc */ |
John Fastabend | 6b3ba91 | 2017-12-07 09:54:25 -0800 | [diff] [blame] | 291 | if (root_lock) |
| 292 | spin_unlock(root_lock); |
Herbert Xu | d90df3a | 2007-05-10 04:55:14 -0700 | [diff] [blame] | 293 | |
Eric Dumazet | 55a93b3 | 2014-10-03 15:31:07 -0700 | [diff] [blame] | 294 | /* Note that we validate skb (GSO, checksum, ...) outside of locks */ |
| 295 | if (validate) |
Steffen Klassert | f53c723 | 2017-12-20 10:41:36 +0100 | [diff] [blame] | 296 | skb = validate_xmit_skb_list(skb, dev, &again); |
| 297 | |
| 298 | #ifdef CONFIG_XFRM_OFFLOAD |
| 299 | if (unlikely(again)) { |
| 300 | if (root_lock) |
| 301 | spin_lock(root_lock); |
| 302 | |
| 303 | dev_requeue_skb(skb, q); |
| 304 | return false; |
| 305 | } |
| 306 | #endif |
Patrick McHardy | 572a9d7 | 2009-11-10 06:14:14 +0000 | [diff] [blame] | 307 | |
Lars Persson | 3dcd493fb | 2016-04-12 08:45:52 +0200 | [diff] [blame] | 308 | if (likely(skb)) { |
Eric Dumazet | 55a93b3 | 2014-10-03 15:31:07 -0700 | [diff] [blame] | 309 | HARD_TX_LOCK(dev, txq, smp_processor_id()); |
| 310 | if (!netif_xmit_frozen_or_stopped(txq)) |
| 311 | skb = dev_hard_start_xmit(skb, dev, txq, &ret); |
Jamal Hadi Salim | c716a81 | 2007-06-10 17:31:24 -0700 | [diff] [blame] | 312 | |
Eric Dumazet | 55a93b3 | 2014-10-03 15:31:07 -0700 | [diff] [blame] | 313 | HARD_TX_UNLOCK(dev, txq); |
Lars Persson | 3dcd493fb | 2016-04-12 08:45:52 +0200 | [diff] [blame] | 314 | } else { |
John Fastabend | 6b3ba91 | 2017-12-07 09:54:25 -0800 | [diff] [blame] | 315 | if (root_lock) |
| 316 | spin_lock(root_lock); |
John Fastabend | 29b86cd | 2017-12-07 09:54:47 -0800 | [diff] [blame] | 317 | return true; |
Eric Dumazet | 55a93b3 | 2014-10-03 15:31:07 -0700 | [diff] [blame] | 318 | } |
John Fastabend | 6b3ba91 | 2017-12-07 09:54:25 -0800 | [diff] [blame] | 319 | |
| 320 | if (root_lock) |
| 321 | spin_lock(root_lock); |
Jamal Hadi Salim | c716a81 | 2007-06-10 17:31:24 -0700 | [diff] [blame] | 322 | |
John Fastabend | 29b86cd | 2017-12-07 09:54:47 -0800 | [diff] [blame] | 323 | if (!dev_xmit_complete(ret)) { |
Krishna Kumar | 6c1361a | 2007-06-24 19:56:09 -0700 | [diff] [blame] | 324 | /* Driver returned NETDEV_TX_BUSY - requeue skb */ |
Joe Perches | e87cc47 | 2012-05-13 21:56:26 +0000 | [diff] [blame] | 325 | if (unlikely(ret != NETDEV_TX_BUSY)) |
| 326 | net_warn_ratelimited("BUG %s code %d qlen %d\n", |
| 327 | dev->name, ret, q->q.qlen); |
Krishna Kumar | 6c1361a | 2007-06-24 19:56:09 -0700 | [diff] [blame] | 328 | |
John Fastabend | 29b86cd | 2017-12-07 09:54:47 -0800 | [diff] [blame] | 329 | dev_requeue_skb(skb, q); |
| 330 | return false; |
Krishna Kumar | 6c1361a | 2007-06-24 19:56:09 -0700 | [diff] [blame] | 331 | } |
| 332 | |
John Fastabend | 29b86cd | 2017-12-07 09:54:47 -0800 | [diff] [blame] | 333 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | } |
| 335 | |
Krishna Kumar | bbd8a0d | 2009-08-06 01:44:21 +0000 | [diff] [blame] | 336 | /* |
| 337 | * NOTE: Called under qdisc_lock(q) with locally disabled BH. |
| 338 | * |
Eric Dumazet | f9eb8ae | 2016-06-06 09:37:15 -0700 | [diff] [blame] | 339 | * running seqcount guarantees only one CPU can process |
Krishna Kumar | bbd8a0d | 2009-08-06 01:44:21 +0000 | [diff] [blame] | 340 | * this qdisc at a time. qdisc_lock(q) serializes queue accesses for |
| 341 | * this queue. |
| 342 | * |
| 343 | * netif_tx_lock serializes accesses to device driver. |
| 344 | * |
| 345 | * qdisc_lock(q) and netif_tx_lock are mutually exclusive, |
| 346 | * if one is grabbed, another must be free. |
| 347 | * |
| 348 | * Note, that this procedure can be called by a watchdog timer |
| 349 | * |
| 350 | * Returns to the caller: |
| 351 | * 0 - queue is empty or throttled. |
| 352 | * >0 - queue is not empty. |
| 353 | * |
| 354 | */ |
John Fastabend | 29b86cd | 2017-12-07 09:54:47 -0800 | [diff] [blame] | 355 | static inline bool qdisc_restart(struct Qdisc *q, int *packets) |
Krishna Kumar | bbd8a0d | 2009-08-06 01:44:21 +0000 | [diff] [blame] | 356 | { |
John Fastabend | 6b3ba91 | 2017-12-07 09:54:25 -0800 | [diff] [blame] | 357 | spinlock_t *root_lock = NULL; |
Krishna Kumar | bbd8a0d | 2009-08-06 01:44:21 +0000 | [diff] [blame] | 358 | struct netdev_queue *txq; |
| 359 | struct net_device *dev; |
Krishna Kumar | bbd8a0d | 2009-08-06 01:44:21 +0000 | [diff] [blame] | 360 | struct sk_buff *skb; |
Paolo Abeni | 32f7b44 | 2018-05-15 10:50:31 +0200 | [diff] [blame] | 361 | bool validate; |
Krishna Kumar | bbd8a0d | 2009-08-06 01:44:21 +0000 | [diff] [blame] | 362 | |
| 363 | /* Dequeue packet */ |
John Fastabend | eb82a99 | 2018-03-24 22:25:06 -0700 | [diff] [blame] | 364 | skb = dequeue_skb(q, &validate, packets); |
Paolo Abeni | 32f7b44 | 2018-05-15 10:50:31 +0200 | [diff] [blame] | 365 | if (unlikely(!skb)) |
John Fastabend | eb82a99 | 2018-03-24 22:25:06 -0700 | [diff] [blame] | 366 | return false; |
John Fastabend | eb82a99 | 2018-03-24 22:25:06 -0700 | [diff] [blame] | 367 | |
Paolo Abeni | 32f7b44 | 2018-05-15 10:50:31 +0200 | [diff] [blame] | 368 | if (!(q->flags & TCQ_F_NOLOCK)) |
John Fastabend | 6b3ba91 | 2017-12-07 09:54:25 -0800 | [diff] [blame] | 369 | root_lock = qdisc_lock(q); |
| 370 | |
Krishna Kumar | bbd8a0d | 2009-08-06 01:44:21 +0000 | [diff] [blame] | 371 | dev = qdisc_dev(q); |
Daniel Borkmann | 10c51b5623 | 2014-08-27 11:11:27 +0200 | [diff] [blame] | 372 | txq = skb_get_tx_queue(dev, skb); |
Krishna Kumar | bbd8a0d | 2009-08-06 01:44:21 +0000 | [diff] [blame] | 373 | |
Paolo Abeni | 32f7b44 | 2018-05-15 10:50:31 +0200 | [diff] [blame] | 374 | return sch_direct_xmit(skb, q, dev, txq, root_lock, validate); |
Krishna Kumar | bbd8a0d | 2009-08-06 01:44:21 +0000 | [diff] [blame] | 375 | } |
| 376 | |
David S. Miller | 37437bb | 2008-07-16 02:15:04 -0700 | [diff] [blame] | 377 | void __qdisc_run(struct Qdisc *q) |
Herbert Xu | 48d8332 | 2006-06-19 23:57:59 -0700 | [diff] [blame] | 378 | { |
Matthias Tafelmeier | 3d48b53 | 2016-12-29 21:37:21 +0100 | [diff] [blame] | 379 | int quota = dev_tx_weight; |
Jesper Dangaard Brouer | b8358d7 | 2014-10-09 12:18:10 +0200 | [diff] [blame] | 380 | int packets; |
Herbert Xu | 2ba2506 | 2008-03-28 16:25:26 -0700 | [diff] [blame] | 381 | |
Jesper Dangaard Brouer | b8358d7 | 2014-10-09 12:18:10 +0200 | [diff] [blame] | 382 | while (qdisc_restart(q, &packets)) { |
Herbert Xu | 2ba2506 | 2008-03-28 16:25:26 -0700 | [diff] [blame] | 383 | /* |
jamal | d5b8aa1 | 2011-06-26 08:13:54 +0000 | [diff] [blame] | 384 | * Ordered by possible occurrence: Postpone processing if |
| 385 | * 1. we've exceeded packet quota |
| 386 | * 2. another process needs the CPU; |
Herbert Xu | 2ba2506 | 2008-03-28 16:25:26 -0700 | [diff] [blame] | 387 | */ |
Jesper Dangaard Brouer | b8358d7 | 2014-10-09 12:18:10 +0200 | [diff] [blame] | 388 | quota -= packets; |
| 389 | if (quota <= 0 || need_resched()) { |
David S. Miller | 37437bb | 2008-07-16 02:15:04 -0700 | [diff] [blame] | 390 | __netif_schedule(q); |
Herbert Xu | 2ba2506 | 2008-03-28 16:25:26 -0700 | [diff] [blame] | 391 | break; |
| 392 | } |
| 393 | } |
Herbert Xu | 48d8332 | 2006-06-19 23:57:59 -0700 | [diff] [blame] | 394 | } |
| 395 | |
Eric Dumazet | 9d21493 | 2009-05-17 20:55:16 -0700 | [diff] [blame] | 396 | unsigned long dev_trans_start(struct net_device *dev) |
| 397 | { |
nikolay@redhat.com | 07ce76a | 2013-08-03 22:07:47 +0200 | [diff] [blame] | 398 | unsigned long val, res; |
Eric Dumazet | 9d21493 | 2009-05-17 20:55:16 -0700 | [diff] [blame] | 399 | unsigned int i; |
| 400 | |
nikolay@redhat.com | 07ce76a | 2013-08-03 22:07:47 +0200 | [diff] [blame] | 401 | if (is_vlan_dev(dev)) |
| 402 | dev = vlan_dev_real_dev(dev); |
Chris Dion | 32d3e51 | 2017-12-06 10:50:28 -0500 | [diff] [blame] | 403 | else if (netif_is_macvlan(dev)) |
| 404 | dev = macvlan_dev_real_dev(dev); |
Florian Westphal | 9b36627 | 2016-05-03 16:33:14 +0200 | [diff] [blame] | 405 | res = netdev_get_tx_queue(dev, 0)->trans_start; |
| 406 | for (i = 1; i < dev->num_tx_queues; i++) { |
Eric Dumazet | 9d21493 | 2009-05-17 20:55:16 -0700 | [diff] [blame] | 407 | val = netdev_get_tx_queue(dev, i)->trans_start; |
| 408 | if (val && time_after(val, res)) |
| 409 | res = val; |
| 410 | } |
nikolay@redhat.com | 07ce76a | 2013-08-03 22:07:47 +0200 | [diff] [blame] | 411 | |
Eric Dumazet | 9d21493 | 2009-05-17 20:55:16 -0700 | [diff] [blame] | 412 | return res; |
| 413 | } |
| 414 | EXPORT_SYMBOL(dev_trans_start); |
| 415 | |
Kees Cook | cdeabbb | 2017-10-16 17:29:17 -0700 | [diff] [blame] | 416 | static void dev_watchdog(struct timer_list *t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | { |
Kees Cook | cdeabbb | 2017-10-16 17:29:17 -0700 | [diff] [blame] | 418 | struct net_device *dev = from_timer(dev, t, watchdog_timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | |
Herbert Xu | 932ff27 | 2006-06-09 12:20:56 -0700 | [diff] [blame] | 420 | netif_tx_lock(dev); |
David S. Miller | e8a0464 | 2008-07-17 00:34:19 -0700 | [diff] [blame] | 421 | if (!qdisc_tx_is_noop(dev)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | if (netif_device_present(dev) && |
| 423 | netif_running(dev) && |
| 424 | netif_carrier_ok(dev)) { |
Eric Dumazet | 9d21493 | 2009-05-17 20:55:16 -0700 | [diff] [blame] | 425 | int some_queue_timedout = 0; |
David S. Miller | e8a0464 | 2008-07-17 00:34:19 -0700 | [diff] [blame] | 426 | unsigned int i; |
Eric Dumazet | 9d21493 | 2009-05-17 20:55:16 -0700 | [diff] [blame] | 427 | unsigned long trans_start; |
Stephen Hemminger | 338f756 | 2006-05-16 15:02:12 -0700 | [diff] [blame] | 428 | |
David S. Miller | e8a0464 | 2008-07-17 00:34:19 -0700 | [diff] [blame] | 429 | for (i = 0; i < dev->num_tx_queues; i++) { |
| 430 | struct netdev_queue *txq; |
| 431 | |
| 432 | txq = netdev_get_tx_queue(dev, i); |
Florian Westphal | 9b36627 | 2016-05-03 16:33:14 +0200 | [diff] [blame] | 433 | trans_start = txq->trans_start; |
Tom Herbert | 73466498 | 2011-11-28 16:32:44 +0000 | [diff] [blame] | 434 | if (netif_xmit_stopped(txq) && |
Eric Dumazet | 9d21493 | 2009-05-17 20:55:16 -0700 | [diff] [blame] | 435 | time_after(jiffies, (trans_start + |
| 436 | dev->watchdog_timeo))) { |
| 437 | some_queue_timedout = 1; |
david decotigny | ccf5ff6 | 2011-11-16 12:15:10 +0000 | [diff] [blame] | 438 | txq->trans_timeout++; |
David S. Miller | e8a0464 | 2008-07-17 00:34:19 -0700 | [diff] [blame] | 439 | break; |
| 440 | } |
| 441 | } |
| 442 | |
Eric Dumazet | 9d21493 | 2009-05-17 20:55:16 -0700 | [diff] [blame] | 443 | if (some_queue_timedout) { |
Eric Dumazet | 9d21493 | 2009-05-17 20:55:16 -0700 | [diff] [blame] | 444 | WARN_ONCE(1, KERN_INFO "NETDEV WATCHDOG: %s (%s): transmit queue %u timed out\n", |
David S. Miller | 3019de1 | 2011-06-06 16:41:33 -0700 | [diff] [blame] | 445 | dev->name, netdev_drivername(dev), i); |
Stephen Hemminger | d314774 | 2008-11-19 21:32:24 -0800 | [diff] [blame] | 446 | dev->netdev_ops->ndo_tx_timeout(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | } |
David S. Miller | e8a0464 | 2008-07-17 00:34:19 -0700 | [diff] [blame] | 448 | if (!mod_timer(&dev->watchdog_timer, |
| 449 | round_jiffies(jiffies + |
| 450 | dev->watchdog_timeo))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | dev_hold(dev); |
| 452 | } |
| 453 | } |
Herbert Xu | 932ff27 | 2006-06-09 12:20:56 -0700 | [diff] [blame] | 454 | netif_tx_unlock(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | |
| 456 | dev_put(dev); |
| 457 | } |
| 458 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | void __netdev_watchdog_up(struct net_device *dev) |
| 460 | { |
Stephen Hemminger | d314774 | 2008-11-19 21:32:24 -0800 | [diff] [blame] | 461 | if (dev->netdev_ops->ndo_tx_timeout) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | if (dev->watchdog_timeo <= 0) |
| 463 | dev->watchdog_timeo = 5*HZ; |
Venkatesh Pallipadi | 60468d5 | 2007-05-31 21:28:44 -0700 | [diff] [blame] | 464 | if (!mod_timer(&dev->watchdog_timer, |
| 465 | round_jiffies(jiffies + dev->watchdog_timeo))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | dev_hold(dev); |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | static void dev_watchdog_up(struct net_device *dev) |
| 471 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | __netdev_watchdog_up(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | static void dev_watchdog_down(struct net_device *dev) |
| 476 | { |
Herbert Xu | 932ff27 | 2006-06-09 12:20:56 -0700 | [diff] [blame] | 477 | netif_tx_lock_bh(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | if (del_timer(&dev->watchdog_timer)) |
Stephen Hemminger | 1533306 | 2006-03-20 22:32:28 -0800 | [diff] [blame] | 479 | dev_put(dev); |
Herbert Xu | 932ff27 | 2006-06-09 12:20:56 -0700 | [diff] [blame] | 480 | netif_tx_unlock_bh(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | } |
| 482 | |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 483 | /** |
| 484 | * netif_carrier_on - set carrier |
| 485 | * @dev: network device |
| 486 | * |
Jouke Witteveen | 989723b | 2019-02-07 17:14:32 +0100 | [diff] [blame] | 487 | * Device has detected acquisition of carrier. |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 488 | */ |
Denis Vlasenko | 0a242ef | 2005-08-11 15:32:53 -0700 | [diff] [blame] | 489 | void netif_carrier_on(struct net_device *dev) |
| 490 | { |
Jeff Garzik | bfaae0f | 2007-10-17 23:26:43 -0700 | [diff] [blame] | 491 | if (test_and_clear_bit(__LINK_STATE_NOCARRIER, &dev->state)) { |
David S. Miller | b473001 | 2008-11-19 15:33:54 -0800 | [diff] [blame] | 492 | if (dev->reg_state == NETREG_UNINITIALIZED) |
| 493 | return; |
David Decotigny | b2d3bcf | 2018-01-18 09:59:13 -0800 | [diff] [blame] | 494 | atomic_inc(&dev->carrier_up_count); |
Denis Vlasenko | 0a242ef | 2005-08-11 15:32:53 -0700 | [diff] [blame] | 495 | linkwatch_fire_event(dev); |
Jeff Garzik | bfaae0f | 2007-10-17 23:26:43 -0700 | [diff] [blame] | 496 | if (netif_running(dev)) |
| 497 | __netdev_watchdog_up(dev); |
| 498 | } |
Denis Vlasenko | 0a242ef | 2005-08-11 15:32:53 -0700 | [diff] [blame] | 499 | } |
Patrick McHardy | 62e3ba1 | 2008-01-22 22:10:23 -0800 | [diff] [blame] | 500 | EXPORT_SYMBOL(netif_carrier_on); |
Denis Vlasenko | 0a242ef | 2005-08-11 15:32:53 -0700 | [diff] [blame] | 501 | |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 502 | /** |
| 503 | * netif_carrier_off - clear carrier |
| 504 | * @dev: network device |
| 505 | * |
| 506 | * Device has detected loss of carrier. |
| 507 | */ |
Denis Vlasenko | 0a242ef | 2005-08-11 15:32:53 -0700 | [diff] [blame] | 508 | void netif_carrier_off(struct net_device *dev) |
| 509 | { |
David S. Miller | b473001 | 2008-11-19 15:33:54 -0800 | [diff] [blame] | 510 | if (!test_and_set_bit(__LINK_STATE_NOCARRIER, &dev->state)) { |
| 511 | if (dev->reg_state == NETREG_UNINITIALIZED) |
| 512 | return; |
David Decotigny | b2d3bcf | 2018-01-18 09:59:13 -0800 | [diff] [blame] | 513 | atomic_inc(&dev->carrier_down_count); |
Denis Vlasenko | 0a242ef | 2005-08-11 15:32:53 -0700 | [diff] [blame] | 514 | linkwatch_fire_event(dev); |
David S. Miller | b473001 | 2008-11-19 15:33:54 -0800 | [diff] [blame] | 515 | } |
Denis Vlasenko | 0a242ef | 2005-08-11 15:32:53 -0700 | [diff] [blame] | 516 | } |
Patrick McHardy | 62e3ba1 | 2008-01-22 22:10:23 -0800 | [diff] [blame] | 517 | EXPORT_SYMBOL(netif_carrier_off); |
Denis Vlasenko | 0a242ef | 2005-08-11 15:32:53 -0700 | [diff] [blame] | 518 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | /* "NOOP" scheduler: the best scheduler, recommended for all interfaces |
| 520 | under all circumstances. It is difficult to invent anything faster or |
| 521 | cheaper. |
| 522 | */ |
| 523 | |
Eric Dumazet | 520ac30 | 2016-06-21 23:16:49 -0700 | [diff] [blame] | 524 | static int noop_enqueue(struct sk_buff *skb, struct Qdisc *qdisc, |
| 525 | struct sk_buff **to_free) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | { |
Eric Dumazet | 520ac30 | 2016-06-21 23:16:49 -0700 | [diff] [blame] | 527 | __qdisc_drop(skb, to_free); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | return NET_XMIT_CN; |
| 529 | } |
| 530 | |
Yang Yingliang | 82d567c | 2013-12-10 20:55:31 +0800 | [diff] [blame] | 531 | static struct sk_buff *noop_dequeue(struct Qdisc *qdisc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | { |
| 533 | return NULL; |
| 534 | } |
| 535 | |
Eric Dumazet | 20fea08 | 2007-11-14 01:44:41 -0800 | [diff] [blame] | 536 | struct Qdisc_ops noop_qdisc_ops __read_mostly = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | .id = "noop", |
| 538 | .priv_size = 0, |
| 539 | .enqueue = noop_enqueue, |
| 540 | .dequeue = noop_dequeue, |
Jarek Poplawski | 99c0db2 | 2008-10-31 00:45:27 -0700 | [diff] [blame] | 541 | .peek = noop_dequeue, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | .owner = THIS_MODULE, |
| 543 | }; |
| 544 | |
David S. Miller | 7698b4f | 2008-07-16 01:42:40 -0700 | [diff] [blame] | 545 | static struct netdev_queue noop_netdev_queue = { |
Li RongQing | 3b40bf4 | 2019-02-25 10:43:06 +0800 | [diff] [blame] | 546 | RCU_POINTER_INITIALIZER(qdisc, &noop_qdisc), |
Jarek Poplawski | 9f3ffae | 2008-10-19 23:37:47 -0700 | [diff] [blame] | 547 | .qdisc_sleeping = &noop_qdisc, |
David S. Miller | 7698b4f | 2008-07-16 01:42:40 -0700 | [diff] [blame] | 548 | }; |
| 549 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | struct Qdisc noop_qdisc = { |
| 551 | .enqueue = noop_enqueue, |
| 552 | .dequeue = noop_dequeue, |
| 553 | .flags = TCQ_F_BUILTIN, |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 554 | .ops = &noop_qdisc_ops, |
David S. Miller | 83874000 | 2008-07-17 00:53:03 -0700 | [diff] [blame] | 555 | .q.lock = __SPIN_LOCK_UNLOCKED(noop_qdisc.q.lock), |
David S. Miller | 7698b4f | 2008-07-16 01:42:40 -0700 | [diff] [blame] | 556 | .dev_queue = &noop_netdev_queue, |
Eric Dumazet | f9eb8ae | 2016-06-06 09:37:15 -0700 | [diff] [blame] | 557 | .running = SEQCNT_ZERO(noop_qdisc.running), |
Eric Dumazet | 7b5edbc | 2010-10-15 19:22:34 +0000 | [diff] [blame] | 558 | .busylock = __SPIN_LOCK_UNLOCKED(noop_qdisc.busylock), |
Eric Dumazet | f98ebd4 | 2018-10-09 15:20:50 -0700 | [diff] [blame] | 559 | .gso_skb = { |
| 560 | .next = (struct sk_buff *)&noop_qdisc.gso_skb, |
| 561 | .prev = (struct sk_buff *)&noop_qdisc.gso_skb, |
| 562 | .qlen = 0, |
| 563 | .lock = __SPIN_LOCK_UNLOCKED(noop_qdisc.gso_skb.lock), |
| 564 | }, |
| 565 | .skb_bad_txq = { |
| 566 | .next = (struct sk_buff *)&noop_qdisc.skb_bad_txq, |
| 567 | .prev = (struct sk_buff *)&noop_qdisc.skb_bad_txq, |
| 568 | .qlen = 0, |
| 569 | .lock = __SPIN_LOCK_UNLOCKED(noop_qdisc.skb_bad_txq.lock), |
| 570 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | }; |
Patrick McHardy | 62e3ba1 | 2008-01-22 22:10:23 -0800 | [diff] [blame] | 572 | EXPORT_SYMBOL(noop_qdisc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | |
Alexander Aring | e63d7df | 2017-12-20 12:35:13 -0500 | [diff] [blame] | 574 | static int noqueue_init(struct Qdisc *qdisc, struct nlattr *opt, |
| 575 | struct netlink_ext_ack *extack) |
Phil Sutter | d66d6c3 | 2015-08-27 21:21:38 +0200 | [diff] [blame] | 576 | { |
| 577 | /* register_qdisc() assigns a default of noop_enqueue if unset, |
| 578 | * but __dev_queue_xmit() treats noqueue only as such |
| 579 | * if this is NULL - so clear it here. */ |
| 580 | qdisc->enqueue = NULL; |
| 581 | return 0; |
| 582 | } |
| 583 | |
| 584 | struct Qdisc_ops noqueue_qdisc_ops __read_mostly = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | .id = "noqueue", |
| 586 | .priv_size = 0, |
Phil Sutter | d66d6c3 | 2015-08-27 21:21:38 +0200 | [diff] [blame] | 587 | .init = noqueue_init, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | .enqueue = noop_enqueue, |
| 589 | .dequeue = noop_dequeue, |
Jarek Poplawski | 99c0db2 | 2008-10-31 00:45:27 -0700 | [diff] [blame] | 590 | .peek = noop_dequeue, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | .owner = THIS_MODULE, |
| 592 | }; |
| 593 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 594 | static const u8 prio2band[TC_PRIO_MAX + 1] = { |
| 595 | 1, 2, 2, 2, 1, 2, 0, 0 , 1, 1, 1, 1, 1, 1, 1, 1 |
| 596 | }; |
Thomas Graf | 321090e | 2005-06-18 22:58:35 -0700 | [diff] [blame] | 597 | |
David S. Miller | d3678b4 | 2008-07-21 09:56:13 -0700 | [diff] [blame] | 598 | /* 3-band FIFO queue: old style, but should be a bit faster than |
| 599 | generic prio+fifo combination. |
| 600 | */ |
| 601 | |
| 602 | #define PFIFO_FAST_BANDS 3 |
| 603 | |
Krishna Kumar | fd3ae5e | 2009-08-18 21:55:59 +0000 | [diff] [blame] | 604 | /* |
| 605 | * Private data for a pfifo_fast scheduler containing: |
John Fastabend | c5ad119 | 2017-12-07 09:58:19 -0800 | [diff] [blame] | 606 | * - rings for priority bands |
Krishna Kumar | fd3ae5e | 2009-08-18 21:55:59 +0000 | [diff] [blame] | 607 | */ |
| 608 | struct pfifo_fast_priv { |
John Fastabend | c5ad119 | 2017-12-07 09:58:19 -0800 | [diff] [blame] | 609 | struct skb_array q[PFIFO_FAST_BANDS]; |
Krishna Kumar | fd3ae5e | 2009-08-18 21:55:59 +0000 | [diff] [blame] | 610 | }; |
| 611 | |
John Fastabend | c5ad119 | 2017-12-07 09:58:19 -0800 | [diff] [blame] | 612 | static inline struct skb_array *band2list(struct pfifo_fast_priv *priv, |
| 613 | int band) |
David S. Miller | d3678b4 | 2008-07-21 09:56:13 -0700 | [diff] [blame] | 614 | { |
John Fastabend | c5ad119 | 2017-12-07 09:58:19 -0800 | [diff] [blame] | 615 | return &priv->q[band]; |
David S. Miller | d3678b4 | 2008-07-21 09:56:13 -0700 | [diff] [blame] | 616 | } |
| 617 | |
Eric Dumazet | 520ac30 | 2016-06-21 23:16:49 -0700 | [diff] [blame] | 618 | static int pfifo_fast_enqueue(struct sk_buff *skb, struct Qdisc *qdisc, |
| 619 | struct sk_buff **to_free) |
David S. Miller | d3678b4 | 2008-07-21 09:56:13 -0700 | [diff] [blame] | 620 | { |
John Fastabend | c5ad119 | 2017-12-07 09:58:19 -0800 | [diff] [blame] | 621 | int band = prio2band[skb->priority & TC_PRIO_MAX]; |
| 622 | struct pfifo_fast_priv *priv = qdisc_priv(qdisc); |
| 623 | struct skb_array *q = band2list(priv, band); |
Eric Dumazet | cce6294c | 2018-03-14 18:53:00 -0700 | [diff] [blame] | 624 | unsigned int pkt_len = qdisc_pkt_len(skb); |
John Fastabend | c5ad119 | 2017-12-07 09:58:19 -0800 | [diff] [blame] | 625 | int err; |
David S. Miller | d3678b4 | 2008-07-21 09:56:13 -0700 | [diff] [blame] | 626 | |
John Fastabend | c5ad119 | 2017-12-07 09:58:19 -0800 | [diff] [blame] | 627 | err = skb_array_produce(q, skb); |
Thomas Graf | 821d24ae | 2005-06-18 22:58:15 -0700 | [diff] [blame] | 628 | |
John Fastabend | c5ad119 | 2017-12-07 09:58:19 -0800 | [diff] [blame] | 629 | if (unlikely(err)) |
| 630 | return qdisc_drop_cpu(skb, qdisc, to_free); |
| 631 | |
Paolo Abeni | 8a53e61 | 2019-04-10 14:32:40 +0200 | [diff] [blame^] | 632 | qdisc_update_stats_at_enqueue(qdisc, pkt_len); |
John Fastabend | c5ad119 | 2017-12-07 09:58:19 -0800 | [diff] [blame] | 633 | return NET_XMIT_SUCCESS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | } |
| 635 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 636 | static struct sk_buff *pfifo_fast_dequeue(struct Qdisc *qdisc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | { |
Krishna Kumar | fd3ae5e | 2009-08-18 21:55:59 +0000 | [diff] [blame] | 638 | struct pfifo_fast_priv *priv = qdisc_priv(qdisc); |
John Fastabend | c5ad119 | 2017-12-07 09:58:19 -0800 | [diff] [blame] | 639 | struct sk_buff *skb = NULL; |
| 640 | int band; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | |
John Fastabend | c5ad119 | 2017-12-07 09:58:19 -0800 | [diff] [blame] | 642 | for (band = 0; band < PFIFO_FAST_BANDS && !skb; band++) { |
| 643 | struct skb_array *q = band2list(priv, band); |
Florian Westphal | ec32336 | 2016-09-18 00:57:32 +0200 | [diff] [blame] | 644 | |
John Fastabend | c5ad119 | 2017-12-07 09:58:19 -0800 | [diff] [blame] | 645 | if (__skb_array_empty(q)) |
| 646 | continue; |
Krishna Kumar | fd3ae5e | 2009-08-18 21:55:59 +0000 | [diff] [blame] | 647 | |
Paolo Abeni | 021a17e | 2018-05-15 16:24:37 +0200 | [diff] [blame] | 648 | skb = __skb_array_consume(q); |
John Fastabend | c5ad119 | 2017-12-07 09:58:19 -0800 | [diff] [blame] | 649 | } |
| 650 | if (likely(skb)) { |
Paolo Abeni | 8a53e61 | 2019-04-10 14:32:40 +0200 | [diff] [blame^] | 651 | qdisc_update_stats_at_dequeue(qdisc, skb); |
Paolo Abeni | 28cff53 | 2019-03-22 16:01:55 +0100 | [diff] [blame] | 652 | } else { |
| 653 | qdisc->empty = true; |
David S. Miller | d3678b4 | 2008-07-21 09:56:13 -0700 | [diff] [blame] | 654 | } |
Thomas Graf | f87a9c3 | 2005-06-18 22:58:53 -0700 | [diff] [blame] | 655 | |
John Fastabend | c5ad119 | 2017-12-07 09:58:19 -0800 | [diff] [blame] | 656 | return skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | } |
| 658 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 659 | static struct sk_buff *pfifo_fast_peek(struct Qdisc *qdisc) |
Jarek Poplawski | 99c0db2 | 2008-10-31 00:45:27 -0700 | [diff] [blame] | 660 | { |
Krishna Kumar | fd3ae5e | 2009-08-18 21:55:59 +0000 | [diff] [blame] | 661 | struct pfifo_fast_priv *priv = qdisc_priv(qdisc); |
John Fastabend | c5ad119 | 2017-12-07 09:58:19 -0800 | [diff] [blame] | 662 | struct sk_buff *skb = NULL; |
| 663 | int band; |
Jarek Poplawski | 99c0db2 | 2008-10-31 00:45:27 -0700 | [diff] [blame] | 664 | |
John Fastabend | c5ad119 | 2017-12-07 09:58:19 -0800 | [diff] [blame] | 665 | for (band = 0; band < PFIFO_FAST_BANDS && !skb; band++) { |
| 666 | struct skb_array *q = band2list(priv, band); |
Krishna Kumar | fd3ae5e | 2009-08-18 21:55:59 +0000 | [diff] [blame] | 667 | |
John Fastabend | c5ad119 | 2017-12-07 09:58:19 -0800 | [diff] [blame] | 668 | skb = __skb_array_peek(q); |
Jarek Poplawski | 99c0db2 | 2008-10-31 00:45:27 -0700 | [diff] [blame] | 669 | } |
| 670 | |
John Fastabend | c5ad119 | 2017-12-07 09:58:19 -0800 | [diff] [blame] | 671 | return skb; |
Jarek Poplawski | 99c0db2 | 2008-10-31 00:45:27 -0700 | [diff] [blame] | 672 | } |
| 673 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 674 | static void pfifo_fast_reset(struct Qdisc *qdisc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | { |
John Fastabend | c5ad119 | 2017-12-07 09:58:19 -0800 | [diff] [blame] | 676 | int i, band; |
Krishna Kumar | fd3ae5e | 2009-08-18 21:55:59 +0000 | [diff] [blame] | 677 | struct pfifo_fast_priv *priv = qdisc_priv(qdisc); |
David S. Miller | d3678b4 | 2008-07-21 09:56:13 -0700 | [diff] [blame] | 678 | |
John Fastabend | c5ad119 | 2017-12-07 09:58:19 -0800 | [diff] [blame] | 679 | for (band = 0; band < PFIFO_FAST_BANDS; band++) { |
| 680 | struct skb_array *q = band2list(priv, band); |
| 681 | struct sk_buff *skb; |
David S. Miller | d3678b4 | 2008-07-21 09:56:13 -0700 | [diff] [blame] | 682 | |
Cong Wang | 1df94c3 | 2017-12-18 14:34:26 -0800 | [diff] [blame] | 683 | /* NULL ring is possible if destroy path is due to a failed |
| 684 | * skb_array_init() in pfifo_fast_init() case. |
| 685 | */ |
| 686 | if (!q->ring.queue) |
| 687 | continue; |
| 688 | |
Paolo Abeni | 021a17e | 2018-05-15 16:24:37 +0200 | [diff] [blame] | 689 | while ((skb = __skb_array_consume(q)) != NULL) |
John Fastabend | c5ad119 | 2017-12-07 09:58:19 -0800 | [diff] [blame] | 690 | kfree_skb(skb); |
| 691 | } |
| 692 | |
| 693 | for_each_possible_cpu(i) { |
| 694 | struct gnet_stats_queue *q = per_cpu_ptr(qdisc->cpu_qstats, i); |
| 695 | |
| 696 | q->backlog = 0; |
John Fastabend | c5ad119 | 2017-12-07 09:58:19 -0800 | [diff] [blame] | 697 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | } |
| 699 | |
David S. Miller | d3678b4 | 2008-07-21 09:56:13 -0700 | [diff] [blame] | 700 | static int pfifo_fast_dump(struct Qdisc *qdisc, struct sk_buff *skb) |
| 701 | { |
| 702 | struct tc_prio_qopt opt = { .bands = PFIFO_FAST_BANDS }; |
| 703 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 704 | memcpy(&opt.priomap, prio2band, TC_PRIO_MAX + 1); |
David S. Miller | 1b34ec4 | 2012-03-29 05:11:39 -0400 | [diff] [blame] | 705 | if (nla_put(skb, TCA_OPTIONS, sizeof(opt), &opt)) |
| 706 | goto nla_put_failure; |
David S. Miller | d3678b4 | 2008-07-21 09:56:13 -0700 | [diff] [blame] | 707 | return skb->len; |
| 708 | |
| 709 | nla_put_failure: |
| 710 | return -1; |
| 711 | } |
| 712 | |
Alexander Aring | e63d7df | 2017-12-20 12:35:13 -0500 | [diff] [blame] | 713 | static int pfifo_fast_init(struct Qdisc *qdisc, struct nlattr *opt, |
| 714 | struct netlink_ext_ack *extack) |
David S. Miller | d3678b4 | 2008-07-21 09:56:13 -0700 | [diff] [blame] | 715 | { |
John Fastabend | c5ad119 | 2017-12-07 09:58:19 -0800 | [diff] [blame] | 716 | unsigned int qlen = qdisc_dev(qdisc)->tx_queue_len; |
Krishna Kumar | fd3ae5e | 2009-08-18 21:55:59 +0000 | [diff] [blame] | 717 | struct pfifo_fast_priv *priv = qdisc_priv(qdisc); |
John Fastabend | c5ad119 | 2017-12-07 09:58:19 -0800 | [diff] [blame] | 718 | int prio; |
David S. Miller | d3678b4 | 2008-07-21 09:56:13 -0700 | [diff] [blame] | 719 | |
John Fastabend | c5ad119 | 2017-12-07 09:58:19 -0800 | [diff] [blame] | 720 | /* guard against zero length rings */ |
| 721 | if (!qlen) |
| 722 | return -EINVAL; |
| 723 | |
| 724 | for (prio = 0; prio < PFIFO_FAST_BANDS; prio++) { |
| 725 | struct skb_array *q = band2list(priv, prio); |
| 726 | int err; |
| 727 | |
| 728 | err = skb_array_init(q, qlen, GFP_KERNEL); |
| 729 | if (err) |
| 730 | return -ENOMEM; |
| 731 | } |
David S. Miller | d3678b4 | 2008-07-21 09:56:13 -0700 | [diff] [blame] | 732 | |
Eric Dumazet | 2362493 | 2011-01-21 16:26:09 -0800 | [diff] [blame] | 733 | /* Can by-pass the queue discipline */ |
| 734 | qdisc->flags |= TCQ_F_CAN_BYPASS; |
David S. Miller | d3678b4 | 2008-07-21 09:56:13 -0700 | [diff] [blame] | 735 | return 0; |
| 736 | } |
| 737 | |
John Fastabend | c5ad119 | 2017-12-07 09:58:19 -0800 | [diff] [blame] | 738 | static void pfifo_fast_destroy(struct Qdisc *sch) |
| 739 | { |
| 740 | struct pfifo_fast_priv *priv = qdisc_priv(sch); |
| 741 | int prio; |
| 742 | |
| 743 | for (prio = 0; prio < PFIFO_FAST_BANDS; prio++) { |
| 744 | struct skb_array *q = band2list(priv, prio); |
| 745 | |
| 746 | /* NULL ring is possible if destroy path is due to a failed |
| 747 | * skb_array_init() in pfifo_fast_init() case. |
| 748 | */ |
Cong Wang | 1df94c3 | 2017-12-18 14:34:26 -0800 | [diff] [blame] | 749 | if (!q->ring.queue) |
John Fastabend | c5ad119 | 2017-12-07 09:58:19 -0800 | [diff] [blame] | 750 | continue; |
| 751 | /* Destroy ring but no need to kfree_skb because a call to |
| 752 | * pfifo_fast_reset() has already done that work. |
| 753 | */ |
| 754 | ptr_ring_cleanup(&q->ring, NULL); |
| 755 | } |
| 756 | } |
| 757 | |
Cong Wang | 7007ba6 | 2018-01-25 18:26:24 -0800 | [diff] [blame] | 758 | static int pfifo_fast_change_tx_queue_len(struct Qdisc *sch, |
| 759 | unsigned int new_len) |
| 760 | { |
| 761 | struct pfifo_fast_priv *priv = qdisc_priv(sch); |
| 762 | struct skb_array *bands[PFIFO_FAST_BANDS]; |
| 763 | int prio; |
| 764 | |
| 765 | for (prio = 0; prio < PFIFO_FAST_BANDS; prio++) { |
| 766 | struct skb_array *q = band2list(priv, prio); |
| 767 | |
| 768 | bands[prio] = q; |
| 769 | } |
| 770 | |
| 771 | return skb_array_resize_multiple(bands, PFIFO_FAST_BANDS, new_len, |
| 772 | GFP_KERNEL); |
| 773 | } |
| 774 | |
David S. Miller | 6ec1c69 | 2009-09-06 01:58:51 -0700 | [diff] [blame] | 775 | struct Qdisc_ops pfifo_fast_ops __read_mostly = { |
David S. Miller | d3678b4 | 2008-07-21 09:56:13 -0700 | [diff] [blame] | 776 | .id = "pfifo_fast", |
Krishna Kumar | fd3ae5e | 2009-08-18 21:55:59 +0000 | [diff] [blame] | 777 | .priv_size = sizeof(struct pfifo_fast_priv), |
David S. Miller | d3678b4 | 2008-07-21 09:56:13 -0700 | [diff] [blame] | 778 | .enqueue = pfifo_fast_enqueue, |
| 779 | .dequeue = pfifo_fast_dequeue, |
Jarek Poplawski | 99c0db2 | 2008-10-31 00:45:27 -0700 | [diff] [blame] | 780 | .peek = pfifo_fast_peek, |
David S. Miller | d3678b4 | 2008-07-21 09:56:13 -0700 | [diff] [blame] | 781 | .init = pfifo_fast_init, |
John Fastabend | c5ad119 | 2017-12-07 09:58:19 -0800 | [diff] [blame] | 782 | .destroy = pfifo_fast_destroy, |
David S. Miller | d3678b4 | 2008-07-21 09:56:13 -0700 | [diff] [blame] | 783 | .reset = pfifo_fast_reset, |
| 784 | .dump = pfifo_fast_dump, |
Cong Wang | 7007ba6 | 2018-01-25 18:26:24 -0800 | [diff] [blame] | 785 | .change_tx_queue_len = pfifo_fast_change_tx_queue_len, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | .owner = THIS_MODULE, |
John Fastabend | c5ad119 | 2017-12-07 09:58:19 -0800 | [diff] [blame] | 787 | .static_flags = TCQ_F_NOLOCK | TCQ_F_CPUSTATS, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | }; |
Eric Dumazet | 1f27cde | 2016-03-02 08:21:43 -0800 | [diff] [blame] | 789 | EXPORT_SYMBOL(pfifo_fast_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | |
Eric Dumazet | 23d3b8b | 2012-09-05 01:02:56 +0000 | [diff] [blame] | 791 | static struct lock_class_key qdisc_tx_busylock; |
Eric Dumazet | f9eb8ae | 2016-06-06 09:37:15 -0700 | [diff] [blame] | 792 | static struct lock_class_key qdisc_running_key; |
Eric Dumazet | 23d3b8b | 2012-09-05 01:02:56 +0000 | [diff] [blame] | 793 | |
David S. Miller | 5ce2d48 | 2008-07-08 17:06:30 -0700 | [diff] [blame] | 794 | struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue, |
Alexander Aring | d0bd684 | 2017-12-20 12:35:20 -0500 | [diff] [blame] | 795 | const struct Qdisc_ops *ops, |
| 796 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | { |
| 798 | void *p; |
| 799 | struct Qdisc *sch; |
Eric Dumazet | d276055 | 2011-03-03 11:10:02 -0800 | [diff] [blame] | 800 | unsigned int size = QDISC_ALIGN(sizeof(*sch)) + ops->priv_size; |
Thomas Graf | 3d54b82 | 2005-07-05 14:15:09 -0700 | [diff] [blame] | 801 | int err = -ENOBUFS; |
Jesus Sanchez-Palencia | 26aa045 | 2017-10-16 18:01:23 -0700 | [diff] [blame] | 802 | struct net_device *dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | |
Jesus Sanchez-Palencia | 26aa045 | 2017-10-16 18:01:23 -0700 | [diff] [blame] | 804 | if (!dev_queue) { |
Alexander Aring | d0bd684 | 2017-12-20 12:35:20 -0500 | [diff] [blame] | 805 | NL_SET_ERR_MSG(extack, "No device queue given"); |
Jesus Sanchez-Palencia | 26aa045 | 2017-10-16 18:01:23 -0700 | [diff] [blame] | 806 | err = -EINVAL; |
| 807 | goto errout; |
| 808 | } |
| 809 | |
| 810 | dev = dev_queue->dev; |
Eric Dumazet | f2cd2d3 | 2010-11-29 08:14:37 +0000 | [diff] [blame] | 811 | p = kzalloc_node(size, GFP_KERNEL, |
| 812 | netdev_queue_numa_node_read(dev_queue)); |
| 813 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | if (!p) |
Thomas Graf | 3d54b82 | 2005-07-05 14:15:09 -0700 | [diff] [blame] | 815 | goto errout; |
Thomas Graf | 3d54b82 | 2005-07-05 14:15:09 -0700 | [diff] [blame] | 816 | sch = (struct Qdisc *) QDISC_ALIGN((unsigned long) p); |
Eric Dumazet | d276055 | 2011-03-03 11:10:02 -0800 | [diff] [blame] | 817 | /* if we got non aligned memory, ask more and do alignment ourself */ |
| 818 | if (sch != p) { |
| 819 | kfree(p); |
| 820 | p = kzalloc_node(size + QDISC_ALIGNTO - 1, GFP_KERNEL, |
| 821 | netdev_queue_numa_node_read(dev_queue)); |
| 822 | if (!p) |
| 823 | goto errout; |
| 824 | sch = (struct Qdisc *) QDISC_ALIGN((unsigned long) p); |
| 825 | sch->padded = (char *) sch - (char *) p; |
| 826 | } |
John Fastabend | a53851e | 2017-12-07 09:55:45 -0800 | [diff] [blame] | 827 | __skb_queue_head_init(&sch->gso_skb); |
John Fastabend | 70e57d5 | 2017-12-07 09:56:23 -0800 | [diff] [blame] | 828 | __skb_queue_head_init(&sch->skb_bad_txq); |
Florian Westphal | 48da34b | 2016-09-18 00:57:34 +0200 | [diff] [blame] | 829 | qdisc_skb_head_init(&sch->q); |
| 830 | spin_lock_init(&sch->q.lock); |
Eric Dumazet | 23d3b8b | 2012-09-05 01:02:56 +0000 | [diff] [blame] | 831 | |
John Fastabend | d59f5ff | 2017-12-07 09:55:26 -0800 | [diff] [blame] | 832 | if (ops->static_flags & TCQ_F_CPUSTATS) { |
| 833 | sch->cpu_bstats = |
| 834 | netdev_alloc_pcpu_stats(struct gnet_stats_basic_cpu); |
| 835 | if (!sch->cpu_bstats) |
| 836 | goto errout1; |
| 837 | |
| 838 | sch->cpu_qstats = alloc_percpu(struct gnet_stats_queue); |
| 839 | if (!sch->cpu_qstats) { |
| 840 | free_percpu(sch->cpu_bstats); |
| 841 | goto errout1; |
| 842 | } |
| 843 | } |
| 844 | |
Eric Dumazet | 79640a4 | 2010-06-02 05:09:29 -0700 | [diff] [blame] | 845 | spin_lock_init(&sch->busylock); |
Eric Dumazet | 23d3b8b | 2012-09-05 01:02:56 +0000 | [diff] [blame] | 846 | lockdep_set_class(&sch->busylock, |
| 847 | dev->qdisc_tx_busylock ?: &qdisc_tx_busylock); |
| 848 | |
Paolo Abeni | 96009c7 | 2018-05-15 16:24:36 +0200 | [diff] [blame] | 849 | /* seqlock has the same scope of busylock, for NOLOCK qdisc */ |
| 850 | spin_lock_init(&sch->seqlock); |
| 851 | lockdep_set_class(&sch->busylock, |
| 852 | dev->qdisc_tx_busylock ?: &qdisc_tx_busylock); |
| 853 | |
Eric Dumazet | f9eb8ae | 2016-06-06 09:37:15 -0700 | [diff] [blame] | 854 | seqcount_init(&sch->running); |
| 855 | lockdep_set_class(&sch->running, |
| 856 | dev->qdisc_running_key ?: &qdisc_running_key); |
| 857 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | sch->ops = ops; |
John Fastabend | d59f5ff | 2017-12-07 09:55:26 -0800 | [diff] [blame] | 859 | sch->flags = ops->static_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | sch->enqueue = ops->enqueue; |
| 861 | sch->dequeue = ops->dequeue; |
David S. Miller | bb949fb | 2008-07-08 16:55:56 -0700 | [diff] [blame] | 862 | sch->dev_queue = dev_queue; |
Paolo Abeni | 28cff53 | 2019-03-22 16:01:55 +0100 | [diff] [blame] | 863 | sch->empty = true; |
Eric Dumazet | 23d3b8b | 2012-09-05 01:02:56 +0000 | [diff] [blame] | 864 | dev_hold(dev); |
Reshetova, Elena | 7b93640 | 2017-07-04 15:53:07 +0300 | [diff] [blame] | 865 | refcount_set(&sch->refcnt, 1); |
Thomas Graf | 3d54b82 | 2005-07-05 14:15:09 -0700 | [diff] [blame] | 866 | |
| 867 | return sch; |
John Fastabend | d59f5ff | 2017-12-07 09:55:26 -0800 | [diff] [blame] | 868 | errout1: |
| 869 | kfree(p); |
Thomas Graf | 3d54b82 | 2005-07-05 14:15:09 -0700 | [diff] [blame] | 870 | errout: |
WANG Cong | 01e123d | 2008-06-27 19:51:35 -0700 | [diff] [blame] | 871 | return ERR_PTR(err); |
Thomas Graf | 3d54b82 | 2005-07-05 14:15:09 -0700 | [diff] [blame] | 872 | } |
| 873 | |
Changli Gao | 3511c91 | 2010-10-16 13:04:08 +0000 | [diff] [blame] | 874 | struct Qdisc *qdisc_create_dflt(struct netdev_queue *dev_queue, |
stephen hemminger | d2a7f26 | 2013-08-31 10:15:50 -0700 | [diff] [blame] | 875 | const struct Qdisc_ops *ops, |
Alexander Aring | a38a9882 | 2017-12-20 12:35:21 -0500 | [diff] [blame] | 876 | unsigned int parentid, |
| 877 | struct netlink_ext_ack *extack) |
Thomas Graf | 3d54b82 | 2005-07-05 14:15:09 -0700 | [diff] [blame] | 878 | { |
| 879 | struct Qdisc *sch; |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 880 | |
Alexander Aring | a38a9882 | 2017-12-20 12:35:21 -0500 | [diff] [blame] | 881 | if (!try_module_get(ops->owner)) { |
| 882 | NL_SET_ERR_MSG(extack, "Failed to increase module reference counter"); |
Eric Dumazet | 166ee5b | 2016-08-24 09:39:02 -0700 | [diff] [blame] | 883 | return NULL; |
Alexander Aring | a38a9882 | 2017-12-20 12:35:21 -0500 | [diff] [blame] | 884 | } |
stephen hemminger | 6da7c8f | 2013-08-27 16:19:08 -0700 | [diff] [blame] | 885 | |
Alexander Aring | a38a9882 | 2017-12-20 12:35:21 -0500 | [diff] [blame] | 886 | sch = qdisc_alloc(dev_queue, ops, extack); |
Eric Dumazet | 166ee5b | 2016-08-24 09:39:02 -0700 | [diff] [blame] | 887 | if (IS_ERR(sch)) { |
| 888 | module_put(ops->owner); |
| 889 | return NULL; |
| 890 | } |
Patrick McHardy | 9f9afec | 2006-11-29 17:35:18 -0800 | [diff] [blame] | 891 | sch->parent = parentid; |
Thomas Graf | 3d54b82 | 2005-07-05 14:15:09 -0700 | [diff] [blame] | 892 | |
Alexander Aring | a38a9882 | 2017-12-20 12:35:21 -0500 | [diff] [blame] | 893 | if (!ops->init || ops->init(sch, NULL, extack) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | return sch; |
| 895 | |
Vlad Buslov | 86bd446 | 2018-09-24 19:22:50 +0300 | [diff] [blame] | 896 | qdisc_put(sch); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 897 | return NULL; |
| 898 | } |
Patrick McHardy | 62e3ba1 | 2008-01-22 22:10:23 -0800 | [diff] [blame] | 899 | EXPORT_SYMBOL(qdisc_create_dflt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | |
David S. Miller | 5fb6622 | 2008-08-02 20:02:43 -0700 | [diff] [blame] | 901 | /* Under qdisc_lock(qdisc) and BH! */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | |
| 903 | void qdisc_reset(struct Qdisc *qdisc) |
| 904 | { |
Eric Dumazet | 20fea08 | 2007-11-14 01:44:41 -0800 | [diff] [blame] | 905 | const struct Qdisc_ops *ops = qdisc->ops; |
John Fastabend | a53851e | 2017-12-07 09:55:45 -0800 | [diff] [blame] | 906 | struct sk_buff *skb, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 907 | |
| 908 | if (ops->reset) |
| 909 | ops->reset(qdisc); |
Jarek Poplawski | 67305eb | 2008-11-03 02:52:50 -0800 | [diff] [blame] | 910 | |
John Fastabend | a53851e | 2017-12-07 09:55:45 -0800 | [diff] [blame] | 911 | skb_queue_walk_safe(&qdisc->gso_skb, skb, tmp) { |
| 912 | __skb_unlink(skb, &qdisc->gso_skb); |
| 913 | kfree_skb_list(skb); |
Krishna Kumar | bbd8a0d | 2009-08-06 01:44:21 +0000 | [diff] [blame] | 914 | } |
John Fastabend | a53851e | 2017-12-07 09:55:45 -0800 | [diff] [blame] | 915 | |
John Fastabend | 70e57d5 | 2017-12-07 09:56:23 -0800 | [diff] [blame] | 916 | skb_queue_walk_safe(&qdisc->skb_bad_txq, skb, tmp) { |
| 917 | __skb_unlink(skb, &qdisc->skb_bad_txq); |
| 918 | kfree_skb_list(skb); |
| 919 | } |
| 920 | |
Eric Dumazet | 4d202a0 | 2016-06-21 23:16:52 -0700 | [diff] [blame] | 921 | qdisc->q.qlen = 0; |
Konstantin Khlebnikov | c8e1812 | 2017-09-20 15:45:36 +0300 | [diff] [blame] | 922 | qdisc->qstats.backlog = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | } |
Patrick McHardy | 62e3ba1 | 2008-01-22 22:10:23 -0800 | [diff] [blame] | 924 | EXPORT_SYMBOL(qdisc_reset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | |
Daniel Borkmann | 81d947e | 2018-01-15 23:12:09 +0100 | [diff] [blame] | 926 | void qdisc_free(struct Qdisc *qdisc) |
Eric Dumazet | 5d944c6 | 2010-03-31 07:06:04 +0000 | [diff] [blame] | 927 | { |
John Fastabend | 73c20a8 | 2016-01-05 09:11:36 -0800 | [diff] [blame] | 928 | if (qdisc_is_percpu_stats(qdisc)) { |
John Fastabend | 22e0f8b | 2014-09-28 11:52:56 -0700 | [diff] [blame] | 929 | free_percpu(qdisc->cpu_bstats); |
John Fastabend | 73c20a8 | 2016-01-05 09:11:36 -0800 | [diff] [blame] | 930 | free_percpu(qdisc->cpu_qstats); |
| 931 | } |
John Fastabend | 22e0f8b | 2014-09-28 11:52:56 -0700 | [diff] [blame] | 932 | |
Eric Dumazet | 5d944c6 | 2010-03-31 07:06:04 +0000 | [diff] [blame] | 933 | kfree((char *) qdisc - qdisc->padded); |
| 934 | } |
| 935 | |
Wei Yongjun | 5362700 | 2018-09-27 14:47:56 +0000 | [diff] [blame] | 936 | static void qdisc_free_cb(struct rcu_head *head) |
Vlad Buslov | 3a7d0d0 | 2018-09-24 19:22:51 +0300 | [diff] [blame] | 937 | { |
| 938 | struct Qdisc *q = container_of(head, struct Qdisc, rcu); |
| 939 | |
| 940 | qdisc_free(q); |
| 941 | } |
| 942 | |
Vlad Buslov | 86bd446 | 2018-09-24 19:22:50 +0300 | [diff] [blame] | 943 | static void qdisc_destroy(struct Qdisc *qdisc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | { |
Eric Dumazet | 20fea08 | 2007-11-14 01:44:41 -0800 | [diff] [blame] | 945 | const struct Qdisc_ops *ops = qdisc->ops; |
John Fastabend | a53851e | 2017-12-07 09:55:45 -0800 | [diff] [blame] | 946 | struct sk_buff *skb, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | |
David S. Miller | 3a682fb | 2008-07-20 18:13:01 -0700 | [diff] [blame] | 948 | #ifdef CONFIG_NET_SCHED |
Jiri Kosina | 59cc1f6 | 2016-08-10 11:05:15 +0200 | [diff] [blame] | 949 | qdisc_hash_del(qdisc); |
Jarek Poplawski | f6e0b23 | 2008-08-22 03:24:05 -0700 | [diff] [blame] | 950 | |
Eric Dumazet | a2da570 | 2011-01-20 03:48:19 +0000 | [diff] [blame] | 951 | qdisc_put_stab(rtnl_dereference(qdisc->stab)); |
David S. Miller | 3a682fb | 2008-07-20 18:13:01 -0700 | [diff] [blame] | 952 | #endif |
Eric Dumazet | 1c0d32f | 2016-12-04 09:48:16 -0800 | [diff] [blame] | 953 | gen_kill_estimator(&qdisc->rate_est); |
Patrick McHardy | 85670cc | 2006-09-27 16:45:45 -0700 | [diff] [blame] | 954 | if (ops->reset) |
| 955 | ops->reset(qdisc); |
| 956 | if (ops->destroy) |
| 957 | ops->destroy(qdisc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | |
Patrick McHardy | 85670cc | 2006-09-27 16:45:45 -0700 | [diff] [blame] | 959 | module_put(ops->owner); |
David S. Miller | 5ce2d48 | 2008-07-08 17:06:30 -0700 | [diff] [blame] | 960 | dev_put(qdisc_dev(qdisc)); |
David S. Miller | 8a34c5d | 2008-07-17 00:47:45 -0700 | [diff] [blame] | 961 | |
John Fastabend | a53851e | 2017-12-07 09:55:45 -0800 | [diff] [blame] | 962 | skb_queue_walk_safe(&qdisc->gso_skb, skb, tmp) { |
| 963 | __skb_unlink(skb, &qdisc->gso_skb); |
| 964 | kfree_skb_list(skb); |
| 965 | } |
| 966 | |
John Fastabend | 70e57d5 | 2017-12-07 09:56:23 -0800 | [diff] [blame] | 967 | skb_queue_walk_safe(&qdisc->skb_bad_txq, skb, tmp) { |
| 968 | __skb_unlink(skb, &qdisc->skb_bad_txq); |
| 969 | kfree_skb_list(skb); |
| 970 | } |
| 971 | |
Vlad Buslov | 3a7d0d0 | 2018-09-24 19:22:51 +0300 | [diff] [blame] | 972 | call_rcu(&qdisc->rcu, qdisc_free_cb); |
David S. Miller | 8a34c5d | 2008-07-17 00:47:45 -0700 | [diff] [blame] | 973 | } |
Vlad Buslov | 86bd446 | 2018-09-24 19:22:50 +0300 | [diff] [blame] | 974 | |
| 975 | void qdisc_put(struct Qdisc *qdisc) |
| 976 | { |
| 977 | if (qdisc->flags & TCQ_F_BUILTIN || |
| 978 | !refcount_dec_and_test(&qdisc->refcnt)) |
| 979 | return; |
| 980 | |
| 981 | qdisc_destroy(qdisc); |
| 982 | } |
| 983 | EXPORT_SYMBOL(qdisc_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 984 | |
Vlad Buslov | 3a7d0d0 | 2018-09-24 19:22:51 +0300 | [diff] [blame] | 985 | /* Version of qdisc_put() that is called with rtnl mutex unlocked. |
| 986 | * Intended to be used as optimization, this function only takes rtnl lock if |
| 987 | * qdisc reference counter reached zero. |
| 988 | */ |
| 989 | |
| 990 | void qdisc_put_unlocked(struct Qdisc *qdisc) |
| 991 | { |
| 992 | if (qdisc->flags & TCQ_F_BUILTIN || |
| 993 | !refcount_dec_and_rtnl_lock(&qdisc->refcnt)) |
| 994 | return; |
| 995 | |
| 996 | qdisc_destroy(qdisc); |
| 997 | rtnl_unlock(); |
| 998 | } |
| 999 | EXPORT_SYMBOL(qdisc_put_unlocked); |
| 1000 | |
Patrick McHardy | 589983c | 2009-09-04 06:41:20 +0000 | [diff] [blame] | 1001 | /* Attach toplevel qdisc to device queue. */ |
| 1002 | struct Qdisc *dev_graft_qdisc(struct netdev_queue *dev_queue, |
| 1003 | struct Qdisc *qdisc) |
| 1004 | { |
| 1005 | struct Qdisc *oqdisc = dev_queue->qdisc_sleeping; |
| 1006 | spinlock_t *root_lock; |
| 1007 | |
| 1008 | root_lock = qdisc_lock(oqdisc); |
| 1009 | spin_lock_bh(root_lock); |
| 1010 | |
Patrick McHardy | 589983c | 2009-09-04 06:41:20 +0000 | [diff] [blame] | 1011 | /* ... and graft new one */ |
| 1012 | if (qdisc == NULL) |
| 1013 | qdisc = &noop_qdisc; |
| 1014 | dev_queue->qdisc_sleeping = qdisc; |
| 1015 | rcu_assign_pointer(dev_queue->qdisc, &noop_qdisc); |
| 1016 | |
| 1017 | spin_unlock_bh(root_lock); |
| 1018 | |
| 1019 | return oqdisc; |
| 1020 | } |
John Fastabend | b8970f0 | 2011-01-17 08:06:09 +0000 | [diff] [blame] | 1021 | EXPORT_SYMBOL(dev_graft_qdisc); |
Patrick McHardy | 589983c | 2009-09-04 06:41:20 +0000 | [diff] [blame] | 1022 | |
David S. Miller | e8a0464 | 2008-07-17 00:34:19 -0700 | [diff] [blame] | 1023 | static void attach_one_default_qdisc(struct net_device *dev, |
| 1024 | struct netdev_queue *dev_queue, |
| 1025 | void *_unused) |
| 1026 | { |
Phil Sutter | 3e692f2 | 2015-08-27 21:21:39 +0200 | [diff] [blame] | 1027 | struct Qdisc *qdisc; |
| 1028 | const struct Qdisc_ops *ops = default_qdisc_ops; |
David S. Miller | e8a0464 | 2008-07-17 00:34:19 -0700 | [diff] [blame] | 1029 | |
Phil Sutter | 3e692f2 | 2015-08-27 21:21:39 +0200 | [diff] [blame] | 1030 | if (dev->priv_flags & IFF_NO_QUEUE) |
| 1031 | ops = &noqueue_qdisc_ops; |
| 1032 | |
Alexander Aring | a38a9882 | 2017-12-20 12:35:21 -0500 | [diff] [blame] | 1033 | qdisc = qdisc_create_dflt(dev_queue, ops, TC_H_ROOT, NULL); |
Phil Sutter | 3e692f2 | 2015-08-27 21:21:39 +0200 | [diff] [blame] | 1034 | if (!qdisc) { |
| 1035 | netdev_info(dev, "activation failed\n"); |
| 1036 | return; |
David S. Miller | e8a0464 | 2008-07-17 00:34:19 -0700 | [diff] [blame] | 1037 | } |
Phil Sutter | 3e692f2 | 2015-08-27 21:21:39 +0200 | [diff] [blame] | 1038 | if (!netif_is_multiqueue(dev)) |
Eric Dumazet | 4eaf3b8 | 2015-12-01 20:08:51 -0800 | [diff] [blame] | 1039 | qdisc->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT; |
David S. Miller | e8a0464 | 2008-07-17 00:34:19 -0700 | [diff] [blame] | 1040 | dev_queue->qdisc_sleeping = qdisc; |
| 1041 | } |
| 1042 | |
David S. Miller | 6ec1c69 | 2009-09-06 01:58:51 -0700 | [diff] [blame] | 1043 | static void attach_default_qdiscs(struct net_device *dev) |
| 1044 | { |
| 1045 | struct netdev_queue *txq; |
| 1046 | struct Qdisc *qdisc; |
| 1047 | |
| 1048 | txq = netdev_get_tx_queue(dev, 0); |
| 1049 | |
Phil Sutter | 4b46995 | 2015-08-13 19:01:07 +0200 | [diff] [blame] | 1050 | if (!netif_is_multiqueue(dev) || |
Phil Sutter | 4b46995 | 2015-08-13 19:01:07 +0200 | [diff] [blame] | 1051 | dev->priv_flags & IFF_NO_QUEUE) { |
David S. Miller | 6ec1c69 | 2009-09-06 01:58:51 -0700 | [diff] [blame] | 1052 | netdev_for_each_tx_queue(dev, attach_one_default_qdisc, NULL); |
| 1053 | dev->qdisc = txq->qdisc_sleeping; |
Eric Dumazet | 551143d | 2017-08-24 21:12:28 -0700 | [diff] [blame] | 1054 | qdisc_refcount_inc(dev->qdisc); |
David S. Miller | 6ec1c69 | 2009-09-06 01:58:51 -0700 | [diff] [blame] | 1055 | } else { |
Alexander Aring | a38a9882 | 2017-12-20 12:35:21 -0500 | [diff] [blame] | 1056 | qdisc = qdisc_create_dflt(txq, &mq_qdisc_ops, TC_H_ROOT, NULL); |
David S. Miller | 6ec1c69 | 2009-09-06 01:58:51 -0700 | [diff] [blame] | 1057 | if (qdisc) { |
David S. Miller | 6ec1c69 | 2009-09-06 01:58:51 -0700 | [diff] [blame] | 1058 | dev->qdisc = qdisc; |
Eric Dumazet | e57a784 | 2013-12-12 15:41:56 -0800 | [diff] [blame] | 1059 | qdisc->ops->attach(qdisc); |
David S. Miller | 6ec1c69 | 2009-09-06 01:58:51 -0700 | [diff] [blame] | 1060 | } |
| 1061 | } |
Jiri Kosina | 59cc1f6 | 2016-08-10 11:05:15 +0200 | [diff] [blame] | 1062 | #ifdef CONFIG_NET_SCHED |
WANG Cong | 92f9170 | 2017-04-04 18:51:30 -0700 | [diff] [blame] | 1063 | if (dev->qdisc != &noop_qdisc) |
Jiri Kosina | 49b4997 | 2017-03-08 16:03:32 +0100 | [diff] [blame] | 1064 | qdisc_hash_add(dev->qdisc, false); |
Jiri Kosina | 59cc1f6 | 2016-08-10 11:05:15 +0200 | [diff] [blame] | 1065 | #endif |
David S. Miller | 6ec1c69 | 2009-09-06 01:58:51 -0700 | [diff] [blame] | 1066 | } |
| 1067 | |
David S. Miller | e8a0464 | 2008-07-17 00:34:19 -0700 | [diff] [blame] | 1068 | static void transition_one_qdisc(struct net_device *dev, |
| 1069 | struct netdev_queue *dev_queue, |
| 1070 | void *_need_watchdog) |
| 1071 | { |
David S. Miller | 83874000 | 2008-07-17 00:53:03 -0700 | [diff] [blame] | 1072 | struct Qdisc *new_qdisc = dev_queue->qdisc_sleeping; |
David S. Miller | e8a0464 | 2008-07-17 00:34:19 -0700 | [diff] [blame] | 1073 | int *need_watchdog_p = _need_watchdog; |
| 1074 | |
David S. Miller | a9312ae | 2008-08-17 21:51:03 -0700 | [diff] [blame] | 1075 | if (!(new_qdisc->flags & TCQ_F_BUILTIN)) |
| 1076 | clear_bit(__QDISC_STATE_DEACTIVATED, &new_qdisc->state); |
| 1077 | |
David S. Miller | 83874000 | 2008-07-17 00:53:03 -0700 | [diff] [blame] | 1078 | rcu_assign_pointer(dev_queue->qdisc, new_qdisc); |
Phil Sutter | 3e692f2 | 2015-08-27 21:21:39 +0200 | [diff] [blame] | 1079 | if (need_watchdog_p) { |
Eric Dumazet | 9d21493 | 2009-05-17 20:55:16 -0700 | [diff] [blame] | 1080 | dev_queue->trans_start = 0; |
David S. Miller | e8a0464 | 2008-07-17 00:34:19 -0700 | [diff] [blame] | 1081 | *need_watchdog_p = 1; |
Eric Dumazet | 9d21493 | 2009-05-17 20:55:16 -0700 | [diff] [blame] | 1082 | } |
David S. Miller | e8a0464 | 2008-07-17 00:34:19 -0700 | [diff] [blame] | 1083 | } |
| 1084 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1085 | void dev_activate(struct net_device *dev) |
| 1086 | { |
David S. Miller | e8a0464 | 2008-07-17 00:34:19 -0700 | [diff] [blame] | 1087 | int need_watchdog; |
David S. Miller | b0e1e64 | 2008-07-08 17:42:10 -0700 | [diff] [blame] | 1088 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1089 | /* No queueing discipline is attached to device; |
stephen hemminger | 6da7c8f | 2013-08-27 16:19:08 -0700 | [diff] [blame] | 1090 | * create default one for devices, which need queueing |
| 1091 | * and noqueue_qdisc for virtual interfaces |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1092 | */ |
| 1093 | |
David S. Miller | 6ec1c69 | 2009-09-06 01:58:51 -0700 | [diff] [blame] | 1094 | if (dev->qdisc == &noop_qdisc) |
| 1095 | attach_default_qdiscs(dev); |
Patrick McHardy | af356af | 2009-09-04 06:41:18 +0000 | [diff] [blame] | 1096 | |
Tommy S. Christensen | cacaddf | 2005-05-03 16:18:52 -0700 | [diff] [blame] | 1097 | if (!netif_carrier_ok(dev)) |
| 1098 | /* Delay activation until next carrier-on event */ |
| 1099 | return; |
| 1100 | |
David S. Miller | e8a0464 | 2008-07-17 00:34:19 -0700 | [diff] [blame] | 1101 | need_watchdog = 0; |
| 1102 | netdev_for_each_tx_queue(dev, transition_one_qdisc, &need_watchdog); |
Eric Dumazet | 24824a0 | 2010-10-02 06:11:55 +0000 | [diff] [blame] | 1103 | if (dev_ingress_queue(dev)) |
| 1104 | transition_one_qdisc(dev, dev_ingress_queue(dev), NULL); |
David S. Miller | e8a0464 | 2008-07-17 00:34:19 -0700 | [diff] [blame] | 1105 | |
| 1106 | if (need_watchdog) { |
Florian Westphal | 860e953 | 2016-05-03 16:33:13 +0200 | [diff] [blame] | 1107 | netif_trans_update(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1108 | dev_watchdog_up(dev); |
| 1109 | } |
David S. Miller | b0e1e64 | 2008-07-08 17:42:10 -0700 | [diff] [blame] | 1110 | } |
John Fastabend | b8970f0 | 2011-01-17 08:06:09 +0000 | [diff] [blame] | 1111 | EXPORT_SYMBOL(dev_activate); |
David S. Miller | b0e1e64 | 2008-07-08 17:42:10 -0700 | [diff] [blame] | 1112 | |
David S. Miller | e8a0464 | 2008-07-17 00:34:19 -0700 | [diff] [blame] | 1113 | static void dev_deactivate_queue(struct net_device *dev, |
| 1114 | struct netdev_queue *dev_queue, |
| 1115 | void *_qdisc_default) |
David S. Miller | b0e1e64 | 2008-07-08 17:42:10 -0700 | [diff] [blame] | 1116 | { |
David S. Miller | e8a0464 | 2008-07-17 00:34:19 -0700 | [diff] [blame] | 1117 | struct Qdisc *qdisc_default = _qdisc_default; |
David S. Miller | 970565b | 2008-07-08 23:10:33 -0700 | [diff] [blame] | 1118 | struct Qdisc *qdisc; |
David S. Miller | b0e1e64 | 2008-07-08 17:42:10 -0700 | [diff] [blame] | 1119 | |
John Fastabend | 46e5da40a | 2014-09-12 20:04:52 -0700 | [diff] [blame] | 1120 | qdisc = rtnl_dereference(dev_queue->qdisc); |
David S. Miller | b0e1e64 | 2008-07-08 17:42:10 -0700 | [diff] [blame] | 1121 | if (qdisc) { |
Paolo Abeni | 96009c7 | 2018-05-15 16:24:36 +0200 | [diff] [blame] | 1122 | bool nolock = qdisc->flags & TCQ_F_NOLOCK; |
| 1123 | |
| 1124 | if (nolock) |
| 1125 | spin_lock_bh(&qdisc->seqlock); |
David S. Miller | 83874000 | 2008-07-17 00:53:03 -0700 | [diff] [blame] | 1126 | spin_lock_bh(qdisc_lock(qdisc)); |
| 1127 | |
David S. Miller | a9312ae | 2008-08-17 21:51:03 -0700 | [diff] [blame] | 1128 | if (!(qdisc->flags & TCQ_F_BUILTIN)) |
| 1129 | set_bit(__QDISC_STATE_DEACTIVATED, &qdisc->state); |
| 1130 | |
Jarek Poplawski | f7a54c1 | 2008-08-27 02:22:07 -0700 | [diff] [blame] | 1131 | rcu_assign_pointer(dev_queue->qdisc, qdisc_default); |
David S. Miller | b0e1e64 | 2008-07-08 17:42:10 -0700 | [diff] [blame] | 1132 | qdisc_reset(qdisc); |
David S. Miller | d3b753d | 2008-07-15 20:14:35 -0700 | [diff] [blame] | 1133 | |
David S. Miller | 83874000 | 2008-07-17 00:53:03 -0700 | [diff] [blame] | 1134 | spin_unlock_bh(qdisc_lock(qdisc)); |
Paolo Abeni | 96009c7 | 2018-05-15 16:24:36 +0200 | [diff] [blame] | 1135 | if (nolock) |
| 1136 | spin_unlock_bh(&qdisc->seqlock); |
David S. Miller | b0e1e64 | 2008-07-08 17:42:10 -0700 | [diff] [blame] | 1137 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1138 | } |
| 1139 | |
David S. Miller | 4335cd2 | 2008-08-17 21:58:07 -0700 | [diff] [blame] | 1140 | static bool some_qdisc_is_busy(struct net_device *dev) |
David S. Miller | e8a0464 | 2008-07-17 00:34:19 -0700 | [diff] [blame] | 1141 | { |
| 1142 | unsigned int i; |
| 1143 | |
| 1144 | for (i = 0; i < dev->num_tx_queues; i++) { |
| 1145 | struct netdev_queue *dev_queue; |
David S. Miller | 7698b4f | 2008-07-16 01:42:40 -0700 | [diff] [blame] | 1146 | spinlock_t *root_lock; |
David S. Miller | e2627c8 | 2008-07-16 00:56:32 -0700 | [diff] [blame] | 1147 | struct Qdisc *q; |
David S. Miller | e8a0464 | 2008-07-17 00:34:19 -0700 | [diff] [blame] | 1148 | int val; |
| 1149 | |
| 1150 | dev_queue = netdev_get_tx_queue(dev, i); |
David S. Miller | b9a3b11 | 2008-08-13 15:18:38 -0700 | [diff] [blame] | 1151 | q = dev_queue->qdisc_sleeping; |
David S. Miller | e8a0464 | 2008-07-17 00:34:19 -0700 | [diff] [blame] | 1152 | |
Paolo Abeni | 32f7b44 | 2018-05-15 10:50:31 +0200 | [diff] [blame] | 1153 | root_lock = qdisc_lock(q); |
| 1154 | spin_lock_bh(root_lock); |
David S. Miller | e8a0464 | 2008-07-17 00:34:19 -0700 | [diff] [blame] | 1155 | |
Paolo Abeni | 32f7b44 | 2018-05-15 10:50:31 +0200 | [diff] [blame] | 1156 | val = (qdisc_is_running(q) || |
| 1157 | test_bit(__QDISC_STATE_SCHED, &q->state)); |
David S. Miller | e8a0464 | 2008-07-17 00:34:19 -0700 | [diff] [blame] | 1158 | |
Paolo Abeni | 32f7b44 | 2018-05-15 10:50:31 +0200 | [diff] [blame] | 1159 | spin_unlock_bh(root_lock); |
David S. Miller | e8a0464 | 2008-07-17 00:34:19 -0700 | [diff] [blame] | 1160 | |
| 1161 | if (val) |
| 1162 | return true; |
| 1163 | } |
| 1164 | return false; |
| 1165 | } |
| 1166 | |
John Fastabend | 7bbde83 | 2017-12-07 09:56:04 -0800 | [diff] [blame] | 1167 | static void dev_qdisc_reset(struct net_device *dev, |
| 1168 | struct netdev_queue *dev_queue, |
| 1169 | void *none) |
| 1170 | { |
| 1171 | struct Qdisc *qdisc = dev_queue->qdisc_sleeping; |
| 1172 | |
| 1173 | if (qdisc) |
| 1174 | qdisc_reset(qdisc); |
| 1175 | } |
| 1176 | |
Eric Dumazet | 3137663 | 2011-05-19 23:42:09 +0000 | [diff] [blame] | 1177 | /** |
| 1178 | * dev_deactivate_many - deactivate transmissions on several devices |
| 1179 | * @head: list of devices to deactivate |
| 1180 | * |
| 1181 | * This function returns only when all outstanding transmissions |
| 1182 | * have completed, unless all devices are in dismantle phase. |
| 1183 | */ |
Octavian Purdila | 4434572 | 2010-12-13 12:44:07 +0000 | [diff] [blame] | 1184 | void dev_deactivate_many(struct list_head *head) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1185 | { |
Octavian Purdila | 4434572 | 2010-12-13 12:44:07 +0000 | [diff] [blame] | 1186 | struct net_device *dev; |
Herbert Xu | 41a23b0 | 2007-05-10 14:12:47 -0700 | [diff] [blame] | 1187 | |
Eric W. Biederman | 5cde282 | 2013-10-05 19:26:05 -0700 | [diff] [blame] | 1188 | list_for_each_entry(dev, head, close_list) { |
Octavian Purdila | 4434572 | 2010-12-13 12:44:07 +0000 | [diff] [blame] | 1189 | netdev_for_each_tx_queue(dev, dev_deactivate_queue, |
| 1190 | &noop_qdisc); |
| 1191 | if (dev_ingress_queue(dev)) |
| 1192 | dev_deactivate_queue(dev, dev_ingress_queue(dev), |
| 1193 | &noop_qdisc); |
| 1194 | |
| 1195 | dev_watchdog_down(dev); |
| 1196 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1197 | |
Eric Dumazet | 3137663 | 2011-05-19 23:42:09 +0000 | [diff] [blame] | 1198 | /* Wait for outstanding qdisc-less dev_queue_xmit calls. |
| 1199 | * This is avoided if all devices are in dismantle phase : |
| 1200 | * Caller will call synchronize_net() for us |
| 1201 | */ |
John Fastabend | 7bbde83 | 2017-12-07 09:56:04 -0800 | [diff] [blame] | 1202 | synchronize_net(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1203 | |
Herbert Xu | d4828d8 | 2006-06-22 02:28:18 -0700 | [diff] [blame] | 1204 | /* Wait for outstanding qdisc_run calls. */ |
John Fastabend | 7bbde83 | 2017-12-07 09:56:04 -0800 | [diff] [blame] | 1205 | list_for_each_entry(dev, head, close_list) { |
Octavian Purdila | 4434572 | 2010-12-13 12:44:07 +0000 | [diff] [blame] | 1206 | while (some_qdisc_is_busy(dev)) |
| 1207 | yield(); |
John Fastabend | 7bbde83 | 2017-12-07 09:56:04 -0800 | [diff] [blame] | 1208 | /* The new qdisc is assigned at this point so we can safely |
| 1209 | * unwind stale skb lists and qdisc statistics |
| 1210 | */ |
| 1211 | netdev_for_each_tx_queue(dev, dev_qdisc_reset, NULL); |
| 1212 | if (dev_ingress_queue(dev)) |
| 1213 | dev_qdisc_reset(dev, dev_ingress_queue(dev), NULL); |
| 1214 | } |
Octavian Purdila | 4434572 | 2010-12-13 12:44:07 +0000 | [diff] [blame] | 1215 | } |
| 1216 | |
| 1217 | void dev_deactivate(struct net_device *dev) |
| 1218 | { |
| 1219 | LIST_HEAD(single); |
| 1220 | |
Eric W. Biederman | 5cde282 | 2013-10-05 19:26:05 -0700 | [diff] [blame] | 1221 | list_add(&dev->close_list, &single); |
Octavian Purdila | 4434572 | 2010-12-13 12:44:07 +0000 | [diff] [blame] | 1222 | dev_deactivate_many(&single); |
Eric W. Biederman | 5f04d50 | 2011-02-20 11:49:45 -0800 | [diff] [blame] | 1223 | list_del(&single); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1224 | } |
John Fastabend | b8970f0 | 2011-01-17 08:06:09 +0000 | [diff] [blame] | 1225 | EXPORT_SYMBOL(dev_deactivate); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1226 | |
Cong Wang | 48bfd55 | 2018-01-25 18:26:23 -0800 | [diff] [blame] | 1227 | static int qdisc_change_tx_queue_len(struct net_device *dev, |
| 1228 | struct netdev_queue *dev_queue) |
| 1229 | { |
| 1230 | struct Qdisc *qdisc = dev_queue->qdisc_sleeping; |
| 1231 | const struct Qdisc_ops *ops = qdisc->ops; |
| 1232 | |
| 1233 | if (ops->change_tx_queue_len) |
| 1234 | return ops->change_tx_queue_len(qdisc, dev->tx_queue_len); |
| 1235 | return 0; |
| 1236 | } |
| 1237 | |
| 1238 | int dev_qdisc_change_tx_queue_len(struct net_device *dev) |
| 1239 | { |
| 1240 | bool up = dev->flags & IFF_UP; |
| 1241 | unsigned int i; |
| 1242 | int ret = 0; |
| 1243 | |
| 1244 | if (up) |
| 1245 | dev_deactivate(dev); |
| 1246 | |
| 1247 | for (i = 0; i < dev->num_tx_queues; i++) { |
| 1248 | ret = qdisc_change_tx_queue_len(dev, &dev->_tx[i]); |
| 1249 | |
| 1250 | /* TODO: revert changes on a partial failure */ |
| 1251 | if (ret) |
| 1252 | break; |
| 1253 | } |
| 1254 | |
| 1255 | if (up) |
| 1256 | dev_activate(dev); |
| 1257 | return ret; |
| 1258 | } |
| 1259 | |
David S. Miller | b0e1e64 | 2008-07-08 17:42:10 -0700 | [diff] [blame] | 1260 | static void dev_init_scheduler_queue(struct net_device *dev, |
| 1261 | struct netdev_queue *dev_queue, |
David S. Miller | e8a0464 | 2008-07-17 00:34:19 -0700 | [diff] [blame] | 1262 | void *_qdisc) |
David S. Miller | b0e1e64 | 2008-07-08 17:42:10 -0700 | [diff] [blame] | 1263 | { |
David S. Miller | e8a0464 | 2008-07-17 00:34:19 -0700 | [diff] [blame] | 1264 | struct Qdisc *qdisc = _qdisc; |
| 1265 | |
John Fastabend | 46e5da40a | 2014-09-12 20:04:52 -0700 | [diff] [blame] | 1266 | rcu_assign_pointer(dev_queue->qdisc, qdisc); |
David S. Miller | b0e1e64 | 2008-07-08 17:42:10 -0700 | [diff] [blame] | 1267 | dev_queue->qdisc_sleeping = qdisc; |
David S. Miller | b0e1e64 | 2008-07-08 17:42:10 -0700 | [diff] [blame] | 1268 | } |
| 1269 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1270 | void dev_init_scheduler(struct net_device *dev) |
| 1271 | { |
Patrick McHardy | af356af | 2009-09-04 06:41:18 +0000 | [diff] [blame] | 1272 | dev->qdisc = &noop_qdisc; |
David S. Miller | e8a0464 | 2008-07-17 00:34:19 -0700 | [diff] [blame] | 1273 | netdev_for_each_tx_queue(dev, dev_init_scheduler_queue, &noop_qdisc); |
Eric Dumazet | 24824a0 | 2010-10-02 06:11:55 +0000 | [diff] [blame] | 1274 | if (dev_ingress_queue(dev)) |
| 1275 | dev_init_scheduler_queue(dev, dev_ingress_queue(dev), &noop_qdisc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1276 | |
Kees Cook | cdeabbb | 2017-10-16 17:29:17 -0700 | [diff] [blame] | 1277 | timer_setup(&dev->watchdog_timer, dev_watchdog, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1278 | } |
| 1279 | |
David S. Miller | e8a0464 | 2008-07-17 00:34:19 -0700 | [diff] [blame] | 1280 | static void shutdown_scheduler_queue(struct net_device *dev, |
| 1281 | struct netdev_queue *dev_queue, |
| 1282 | void *_qdisc_default) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1283 | { |
David S. Miller | b0e1e64 | 2008-07-08 17:42:10 -0700 | [diff] [blame] | 1284 | struct Qdisc *qdisc = dev_queue->qdisc_sleeping; |
David S. Miller | e8a0464 | 2008-07-17 00:34:19 -0700 | [diff] [blame] | 1285 | struct Qdisc *qdisc_default = _qdisc_default; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1286 | |
David S. Miller | b0e1e64 | 2008-07-08 17:42:10 -0700 | [diff] [blame] | 1287 | if (qdisc) { |
Jarek Poplawski | f7a54c1 | 2008-08-27 02:22:07 -0700 | [diff] [blame] | 1288 | rcu_assign_pointer(dev_queue->qdisc, qdisc_default); |
David S. Miller | b0e1e64 | 2008-07-08 17:42:10 -0700 | [diff] [blame] | 1289 | dev_queue->qdisc_sleeping = qdisc_default; |
| 1290 | |
Vlad Buslov | 86bd446 | 2018-09-24 19:22:50 +0300 | [diff] [blame] | 1291 | qdisc_put(qdisc); |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 1292 | } |
David S. Miller | b0e1e64 | 2008-07-08 17:42:10 -0700 | [diff] [blame] | 1293 | } |
| 1294 | |
| 1295 | void dev_shutdown(struct net_device *dev) |
| 1296 | { |
David S. Miller | e8a0464 | 2008-07-17 00:34:19 -0700 | [diff] [blame] | 1297 | netdev_for_each_tx_queue(dev, shutdown_scheduler_queue, &noop_qdisc); |
Eric Dumazet | 24824a0 | 2010-10-02 06:11:55 +0000 | [diff] [blame] | 1298 | if (dev_ingress_queue(dev)) |
| 1299 | shutdown_scheduler_queue(dev, dev_ingress_queue(dev), &noop_qdisc); |
Vlad Buslov | 86bd446 | 2018-09-24 19:22:50 +0300 | [diff] [blame] | 1300 | qdisc_put(dev->qdisc); |
Patrick McHardy | af356af | 2009-09-04 06:41:18 +0000 | [diff] [blame] | 1301 | dev->qdisc = &noop_qdisc; |
| 1302 | |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 1303 | WARN_ON(timer_pending(&dev->watchdog_timer)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1304 | } |
Jiri Pirko | 292f1c7 | 2013-02-12 00:12:03 +0000 | [diff] [blame] | 1305 | |
Eric Dumazet | 01cb71d | 2013-06-02 13:55:05 +0000 | [diff] [blame] | 1306 | void psched_ratecfg_precompute(struct psched_ratecfg *r, |
Eric Dumazet | 3e1e3aa | 2013-09-19 09:10:03 -0700 | [diff] [blame] | 1307 | const struct tc_ratespec *conf, |
| 1308 | u64 rate64) |
Jiri Pirko | 292f1c7 | 2013-02-12 00:12:03 +0000 | [diff] [blame] | 1309 | { |
Eric Dumazet | 01cb71d | 2013-06-02 13:55:05 +0000 | [diff] [blame] | 1310 | memset(r, 0, sizeof(*r)); |
| 1311 | r->overhead = conf->overhead; |
Eric Dumazet | 3e1e3aa | 2013-09-19 09:10:03 -0700 | [diff] [blame] | 1312 | r->rate_bytes_ps = max_t(u64, conf->rate, rate64); |
Jesper Dangaard Brouer | 8a8e3d8 | 2013-08-14 23:47:11 +0200 | [diff] [blame] | 1313 | r->linklayer = (conf->linklayer & TC_LINKLAYER_MASK); |
Jiri Pirko | 292f1c7 | 2013-02-12 00:12:03 +0000 | [diff] [blame] | 1314 | r->mult = 1; |
| 1315 | /* |
Eric Dumazet | 130d3d6 | 2013-06-06 13:56:19 -0700 | [diff] [blame] | 1316 | * The deal here is to replace a divide by a reciprocal one |
| 1317 | * in fast path (a reciprocal divide is a multiply and a shift) |
| 1318 | * |
| 1319 | * Normal formula would be : |
| 1320 | * time_in_ns = (NSEC_PER_SEC * len) / rate_bps |
| 1321 | * |
| 1322 | * We compute mult/shift to use instead : |
| 1323 | * time_in_ns = (len * mult) >> shift; |
| 1324 | * |
| 1325 | * We try to get the highest possible mult value for accuracy, |
| 1326 | * but have to make sure no overflows will ever happen. |
Jiri Pirko | 292f1c7 | 2013-02-12 00:12:03 +0000 | [diff] [blame] | 1327 | */ |
Eric Dumazet | 130d3d6 | 2013-06-06 13:56:19 -0700 | [diff] [blame] | 1328 | if (r->rate_bytes_ps > 0) { |
| 1329 | u64 factor = NSEC_PER_SEC; |
Jiri Pirko | 292f1c7 | 2013-02-12 00:12:03 +0000 | [diff] [blame] | 1330 | |
Eric Dumazet | 130d3d6 | 2013-06-06 13:56:19 -0700 | [diff] [blame] | 1331 | for (;;) { |
| 1332 | r->mult = div64_u64(factor, r->rate_bytes_ps); |
| 1333 | if (r->mult & (1U << 31) || factor & (1ULL << 63)) |
| 1334 | break; |
| 1335 | factor <<= 1; |
| 1336 | r->shift++; |
| 1337 | } |
Jiri Pirko | 292f1c7 | 2013-02-12 00:12:03 +0000 | [diff] [blame] | 1338 | } |
| 1339 | } |
| 1340 | EXPORT_SYMBOL(psched_ratecfg_precompute); |
Jiri Pirko | 4620940 | 2017-11-03 11:46:25 +0100 | [diff] [blame] | 1341 | |
| 1342 | static void mini_qdisc_rcu_func(struct rcu_head *head) |
| 1343 | { |
| 1344 | } |
| 1345 | |
| 1346 | void mini_qdisc_pair_swap(struct mini_Qdisc_pair *miniqp, |
| 1347 | struct tcf_proto *tp_head) |
| 1348 | { |
Vlad Buslov | ed76f5e | 2019-02-11 10:55:38 +0200 | [diff] [blame] | 1349 | /* Protected with chain0->filter_chain_lock. |
| 1350 | * Can't access chain directly because tp_head can be NULL. |
| 1351 | */ |
| 1352 | struct mini_Qdisc *miniq_old = |
| 1353 | rcu_dereference_protected(*miniqp->p_miniq, 1); |
Jiri Pirko | 4620940 | 2017-11-03 11:46:25 +0100 | [diff] [blame] | 1354 | struct mini_Qdisc *miniq; |
| 1355 | |
| 1356 | if (!tp_head) { |
| 1357 | RCU_INIT_POINTER(*miniqp->p_miniq, NULL); |
Cong Wang | b2fb01f | 2017-12-20 23:26:24 -0800 | [diff] [blame] | 1358 | /* Wait for flying RCU callback before it is freed. */ |
Paul E. McKenney | ae0e334 | 2018-11-06 19:40:39 -0800 | [diff] [blame] | 1359 | rcu_barrier(); |
Jiri Pirko | 4620940 | 2017-11-03 11:46:25 +0100 | [diff] [blame] | 1360 | return; |
| 1361 | } |
| 1362 | |
| 1363 | miniq = !miniq_old || miniq_old == &miniqp->miniq2 ? |
| 1364 | &miniqp->miniq1 : &miniqp->miniq2; |
| 1365 | |
| 1366 | /* We need to make sure that readers won't see the miniq |
Paul E. McKenney | ae0e334 | 2018-11-06 19:40:39 -0800 | [diff] [blame] | 1367 | * we are about to modify. So wait until previous call_rcu callback |
Jiri Pirko | 4620940 | 2017-11-03 11:46:25 +0100 | [diff] [blame] | 1368 | * is done. |
| 1369 | */ |
Paul E. McKenney | ae0e334 | 2018-11-06 19:40:39 -0800 | [diff] [blame] | 1370 | rcu_barrier(); |
Jiri Pirko | 4620940 | 2017-11-03 11:46:25 +0100 | [diff] [blame] | 1371 | miniq->filter_list = tp_head; |
| 1372 | rcu_assign_pointer(*miniqp->p_miniq, miniq); |
| 1373 | |
| 1374 | if (miniq_old) |
Cong Wang | b2fb01f | 2017-12-20 23:26:24 -0800 | [diff] [blame] | 1375 | /* This is counterpart of the rcu barriers above. We need to |
Jiri Pirko | 4620940 | 2017-11-03 11:46:25 +0100 | [diff] [blame] | 1376 | * block potential new user of miniq_old until all readers |
| 1377 | * are not seeing it. |
| 1378 | */ |
Paul E. McKenney | ae0e334 | 2018-11-06 19:40:39 -0800 | [diff] [blame] | 1379 | call_rcu(&miniq_old->rcu, mini_qdisc_rcu_func); |
Jiri Pirko | 4620940 | 2017-11-03 11:46:25 +0100 | [diff] [blame] | 1380 | } |
| 1381 | EXPORT_SYMBOL(mini_qdisc_pair_swap); |
| 1382 | |
| 1383 | void mini_qdisc_pair_init(struct mini_Qdisc_pair *miniqp, struct Qdisc *qdisc, |
| 1384 | struct mini_Qdisc __rcu **p_miniq) |
| 1385 | { |
| 1386 | miniqp->miniq1.cpu_bstats = qdisc->cpu_bstats; |
| 1387 | miniqp->miniq1.cpu_qstats = qdisc->cpu_qstats; |
| 1388 | miniqp->miniq2.cpu_bstats = qdisc->cpu_bstats; |
| 1389 | miniqp->miniq2.cpu_qstats = qdisc->cpu_qstats; |
| 1390 | miniqp->p_miniq = p_miniq; |
| 1391 | } |
| 1392 | EXPORT_SYMBOL(mini_qdisc_pair_init); |