John Fastabend | b8970f0 | 2011-01-17 08:06:09 +0000 | [diff] [blame] | 1 | /* |
| 2 | * net/sched/sch_mqprio.c |
| 3 | * |
| 4 | * Copyright (c) 2010 John Fastabend <john.r.fastabend@intel.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * version 2 as published by the Free Software Foundation. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/types.h> |
| 12 | #include <linux/slab.h> |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/string.h> |
| 15 | #include <linux/errno.h> |
| 16 | #include <linux/skbuff.h> |
Paul Gortmaker | 3a9a231 | 2011-05-27 09:12:25 -0400 | [diff] [blame] | 17 | #include <linux/module.h> |
John Fastabend | b8970f0 | 2011-01-17 08:06:09 +0000 | [diff] [blame] | 18 | #include <net/netlink.h> |
| 19 | #include <net/pkt_sched.h> |
| 20 | #include <net/sch_generic.h> |
Amritha Nambiar | 4e8b86c | 2017-09-07 04:00:06 -0700 | [diff] [blame] | 21 | #include <net/pkt_cls.h> |
John Fastabend | b8970f0 | 2011-01-17 08:06:09 +0000 | [diff] [blame] | 22 | |
| 23 | struct mqprio_sched { |
| 24 | struct Qdisc **qdiscs; |
Amritha Nambiar | 4e8b86c | 2017-09-07 04:00:06 -0700 | [diff] [blame] | 25 | u16 mode; |
| 26 | u16 shaper; |
Alexander Duyck | 2026fec | 2017-03-15 10:39:18 -0700 | [diff] [blame] | 27 | int hw_offload; |
Amritha Nambiar | 4e8b86c | 2017-09-07 04:00:06 -0700 | [diff] [blame] | 28 | u32 flags; |
| 29 | u64 min_rate[TC_QOPT_MAX_QUEUE]; |
| 30 | u64 max_rate[TC_QOPT_MAX_QUEUE]; |
John Fastabend | b8970f0 | 2011-01-17 08:06:09 +0000 | [diff] [blame] | 31 | }; |
| 32 | |
| 33 | static void mqprio_destroy(struct Qdisc *sch) |
| 34 | { |
| 35 | struct net_device *dev = qdisc_dev(sch); |
| 36 | struct mqprio_sched *priv = qdisc_priv(sch); |
| 37 | unsigned int ntx; |
| 38 | |
Ben Hutchings | ac7100b | 2011-02-14 19:02:23 +0000 | [diff] [blame] | 39 | if (priv->qdiscs) { |
| 40 | for (ntx = 0; |
| 41 | ntx < dev->num_tx_queues && priv->qdiscs[ntx]; |
| 42 | ntx++) |
Vlad Buslov | 86bd446 | 2018-09-24 19:22:50 +0300 | [diff] [blame] | 43 | qdisc_put(priv->qdiscs[ntx]); |
Ben Hutchings | ac7100b | 2011-02-14 19:02:23 +0000 | [diff] [blame] | 44 | kfree(priv->qdiscs); |
| 45 | } |
John Fastabend | b8970f0 | 2011-01-17 08:06:09 +0000 | [diff] [blame] | 46 | |
Amritha Nambiar | 56f36ac | 2017-03-15 10:39:25 -0700 | [diff] [blame] | 47 | if (priv->hw_offload && dev->netdev_ops->ndo_setup_tc) { |
Amritha Nambiar | 4e8b86c | 2017-09-07 04:00:06 -0700 | [diff] [blame] | 48 | struct tc_mqprio_qopt_offload mqprio = { { 0 } }; |
Amritha Nambiar | 56f36ac | 2017-03-15 10:39:25 -0700 | [diff] [blame] | 49 | |
Amritha Nambiar | 4e8b86c | 2017-09-07 04:00:06 -0700 | [diff] [blame] | 50 | switch (priv->mode) { |
| 51 | case TC_MQPRIO_MODE_DCB: |
| 52 | case TC_MQPRIO_MODE_CHANNEL: |
Nogah Frankel | 575ed7d | 2017-11-06 07:23:42 +0100 | [diff] [blame] | 53 | dev->netdev_ops->ndo_setup_tc(dev, |
| 54 | TC_SETUP_QDISC_MQPRIO, |
Amritha Nambiar | 4e8b86c | 2017-09-07 04:00:06 -0700 | [diff] [blame] | 55 | &mqprio); |
| 56 | break; |
| 57 | default: |
| 58 | return; |
| 59 | } |
Amritha Nambiar | 56f36ac | 2017-03-15 10:39:25 -0700 | [diff] [blame] | 60 | } else { |
John Fastabend | b8970f0 | 2011-01-17 08:06:09 +0000 | [diff] [blame] | 61 | netdev_set_num_tc(dev, 0); |
Amritha Nambiar | 56f36ac | 2017-03-15 10:39:25 -0700 | [diff] [blame] | 62 | } |
John Fastabend | b8970f0 | 2011-01-17 08:06:09 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | static int mqprio_parse_opt(struct net_device *dev, struct tc_mqprio_qopt *qopt) |
| 66 | { |
| 67 | int i, j; |
| 68 | |
| 69 | /* Verify num_tc is not out of max range */ |
| 70 | if (qopt->num_tc > TC_MAX_QUEUE) |
| 71 | return -EINVAL; |
| 72 | |
| 73 | /* Verify priority mapping uses valid tcs */ |
| 74 | for (i = 0; i < TC_BITMASK + 1; i++) { |
| 75 | if (qopt->prio_tc_map[i] >= qopt->num_tc) |
| 76 | return -EINVAL; |
| 77 | } |
| 78 | |
Alexander Duyck | 2026fec | 2017-03-15 10:39:18 -0700 | [diff] [blame] | 79 | /* Limit qopt->hw to maximum supported offload value. Drivers have |
| 80 | * the option of overriding this later if they don't support the a |
| 81 | * given offload type. |
| 82 | */ |
| 83 | if (qopt->hw > TC_MQPRIO_HW_OFFLOAD_MAX) |
| 84 | qopt->hw = TC_MQPRIO_HW_OFFLOAD_MAX; |
John Fastabend | b8970f0 | 2011-01-17 08:06:09 +0000 | [diff] [blame] | 85 | |
Alexander Duyck | 2026fec | 2017-03-15 10:39:18 -0700 | [diff] [blame] | 86 | /* If hardware offload is requested we will leave it to the device |
| 87 | * to either populate the queue counts itself or to validate the |
| 88 | * provided queue counts. If ndo_setup_tc is not present then |
| 89 | * hardware doesn't support offload and we should return an error. |
John Fastabend | b8970f0 | 2011-01-17 08:06:09 +0000 | [diff] [blame] | 90 | */ |
| 91 | if (qopt->hw) |
Alexander Duyck | 2026fec | 2017-03-15 10:39:18 -0700 | [diff] [blame] | 92 | return dev->netdev_ops->ndo_setup_tc ? 0 : -EINVAL; |
John Fastabend | b8970f0 | 2011-01-17 08:06:09 +0000 | [diff] [blame] | 93 | |
| 94 | for (i = 0; i < qopt->num_tc; i++) { |
| 95 | unsigned int last = qopt->offset[i] + qopt->count[i]; |
| 96 | |
| 97 | /* Verify the queue count is in tx range being equal to the |
| 98 | * real_num_tx_queues indicates the last queue is in use. |
| 99 | */ |
| 100 | if (qopt->offset[i] >= dev->real_num_tx_queues || |
| 101 | !qopt->count[i] || |
| 102 | last > dev->real_num_tx_queues) |
| 103 | return -EINVAL; |
| 104 | |
| 105 | /* Verify that the offset and counts do not overlap */ |
| 106 | for (j = i + 1; j < qopt->num_tc; j++) { |
| 107 | if (last > qopt->offset[j]) |
| 108 | return -EINVAL; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | return 0; |
| 113 | } |
| 114 | |
Amritha Nambiar | 4e8b86c | 2017-09-07 04:00:06 -0700 | [diff] [blame] | 115 | static const struct nla_policy mqprio_policy[TCA_MQPRIO_MAX + 1] = { |
| 116 | [TCA_MQPRIO_MODE] = { .len = sizeof(u16) }, |
| 117 | [TCA_MQPRIO_SHAPER] = { .len = sizeof(u16) }, |
| 118 | [TCA_MQPRIO_MIN_RATE64] = { .type = NLA_NESTED }, |
| 119 | [TCA_MQPRIO_MAX_RATE64] = { .type = NLA_NESTED }, |
| 120 | }; |
| 121 | |
| 122 | static int parse_attr(struct nlattr *tb[], int maxtype, struct nlattr *nla, |
| 123 | const struct nla_policy *policy, int len) |
| 124 | { |
| 125 | int nested_len = nla_len(nla) - NLA_ALIGN(len); |
| 126 | |
| 127 | if (nested_len >= nla_attr_size(0)) |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame^] | 128 | return nla_parse_deprecated(tb, maxtype, |
| 129 | nla_data(nla) + NLA_ALIGN(len), |
| 130 | nested_len, policy, NULL); |
Amritha Nambiar | 4e8b86c | 2017-09-07 04:00:06 -0700 | [diff] [blame] | 131 | |
| 132 | memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1)); |
| 133 | return 0; |
| 134 | } |
| 135 | |
Alexander Aring | e63d7df | 2017-12-20 12:35:13 -0500 | [diff] [blame] | 136 | static int mqprio_init(struct Qdisc *sch, struct nlattr *opt, |
| 137 | struct netlink_ext_ack *extack) |
John Fastabend | b8970f0 | 2011-01-17 08:06:09 +0000 | [diff] [blame] | 138 | { |
| 139 | struct net_device *dev = qdisc_dev(sch); |
| 140 | struct mqprio_sched *priv = qdisc_priv(sch); |
| 141 | struct netdev_queue *dev_queue; |
| 142 | struct Qdisc *qdisc; |
| 143 | int i, err = -EOPNOTSUPP; |
| 144 | struct tc_mqprio_qopt *qopt = NULL; |
Amritha Nambiar | 4e8b86c | 2017-09-07 04:00:06 -0700 | [diff] [blame] | 145 | struct nlattr *tb[TCA_MQPRIO_MAX + 1]; |
| 146 | struct nlattr *attr; |
| 147 | int rem; |
Colin Ian King | 22ce97f | 2017-10-17 16:01:30 +0100 | [diff] [blame] | 148 | int len; |
John Fastabend | b8970f0 | 2011-01-17 08:06:09 +0000 | [diff] [blame] | 149 | |
| 150 | BUILD_BUG_ON(TC_MAX_QUEUE != TC_QOPT_MAX_QUEUE); |
| 151 | BUILD_BUG_ON(TC_BITMASK != TC_QOPT_BITMASK); |
| 152 | |
| 153 | if (sch->parent != TC_H_ROOT) |
| 154 | return -EOPNOTSUPP; |
| 155 | |
| 156 | if (!netif_is_multiqueue(dev)) |
| 157 | return -EOPNOTSUPP; |
| 158 | |
Alexander Duyck | 3230290 | 2017-10-12 11:38:45 -0700 | [diff] [blame] | 159 | /* make certain can allocate enough classids to handle queues */ |
| 160 | if (dev->num_tx_queues >= TC_H_MIN_PRIORITY) |
| 161 | return -ENOMEM; |
| 162 | |
Thomas Graf | 7838f2c | 2011-12-22 02:05:07 +0000 | [diff] [blame] | 163 | if (!opt || nla_len(opt) < sizeof(*qopt)) |
John Fastabend | b8970f0 | 2011-01-17 08:06:09 +0000 | [diff] [blame] | 164 | return -EINVAL; |
| 165 | |
| 166 | qopt = nla_data(opt); |
| 167 | if (mqprio_parse_opt(dev, qopt)) |
| 168 | return -EINVAL; |
| 169 | |
Colin Ian King | 22ce97f | 2017-10-17 16:01:30 +0100 | [diff] [blame] | 170 | len = nla_len(opt) - NLA_ALIGN(sizeof(*qopt)); |
Amritha Nambiar | 4e8b86c | 2017-09-07 04:00:06 -0700 | [diff] [blame] | 171 | if (len > 0) { |
| 172 | err = parse_attr(tb, TCA_MQPRIO_MAX, opt, mqprio_policy, |
| 173 | sizeof(*qopt)); |
| 174 | if (err < 0) |
| 175 | return err; |
| 176 | |
| 177 | if (!qopt->hw) |
| 178 | return -EINVAL; |
| 179 | |
| 180 | if (tb[TCA_MQPRIO_MODE]) { |
| 181 | priv->flags |= TC_MQPRIO_F_MODE; |
| 182 | priv->mode = *(u16 *)nla_data(tb[TCA_MQPRIO_MODE]); |
| 183 | } |
| 184 | |
| 185 | if (tb[TCA_MQPRIO_SHAPER]) { |
| 186 | priv->flags |= TC_MQPRIO_F_SHAPER; |
| 187 | priv->shaper = *(u16 *)nla_data(tb[TCA_MQPRIO_SHAPER]); |
| 188 | } |
| 189 | |
| 190 | if (tb[TCA_MQPRIO_MIN_RATE64]) { |
| 191 | if (priv->shaper != TC_MQPRIO_SHAPER_BW_RATE) |
| 192 | return -EINVAL; |
| 193 | i = 0; |
| 194 | nla_for_each_nested(attr, tb[TCA_MQPRIO_MIN_RATE64], |
| 195 | rem) { |
| 196 | if (nla_type(attr) != TCA_MQPRIO_MIN_RATE64) |
| 197 | return -EINVAL; |
| 198 | if (i >= qopt->num_tc) |
| 199 | break; |
| 200 | priv->min_rate[i] = *(u64 *)nla_data(attr); |
| 201 | i++; |
| 202 | } |
| 203 | priv->flags |= TC_MQPRIO_F_MIN_RATE; |
| 204 | } |
| 205 | |
| 206 | if (tb[TCA_MQPRIO_MAX_RATE64]) { |
| 207 | if (priv->shaper != TC_MQPRIO_SHAPER_BW_RATE) |
| 208 | return -EINVAL; |
| 209 | i = 0; |
| 210 | nla_for_each_nested(attr, tb[TCA_MQPRIO_MAX_RATE64], |
| 211 | rem) { |
| 212 | if (nla_type(attr) != TCA_MQPRIO_MAX_RATE64) |
| 213 | return -EINVAL; |
| 214 | if (i >= qopt->num_tc) |
| 215 | break; |
| 216 | priv->max_rate[i] = *(u64 *)nla_data(attr); |
| 217 | i++; |
| 218 | } |
| 219 | priv->flags |= TC_MQPRIO_F_MAX_RATE; |
| 220 | } |
| 221 | } |
| 222 | |
John Fastabend | b8970f0 | 2011-01-17 08:06:09 +0000 | [diff] [blame] | 223 | /* pre-allocate qdisc, attachment can't fail */ |
| 224 | priv->qdiscs = kcalloc(dev->num_tx_queues, sizeof(priv->qdiscs[0]), |
| 225 | GFP_KERNEL); |
Eric Dumazet | 87b60cf | 2017-02-10 10:31:49 -0800 | [diff] [blame] | 226 | if (!priv->qdiscs) |
| 227 | return -ENOMEM; |
John Fastabend | b8970f0 | 2011-01-17 08:06:09 +0000 | [diff] [blame] | 228 | |
| 229 | for (i = 0; i < dev->num_tx_queues; i++) { |
| 230 | dev_queue = netdev_get_tx_queue(dev, i); |
Eric Dumazet | 1f27cde | 2016-03-02 08:21:43 -0800 | [diff] [blame] | 231 | qdisc = qdisc_create_dflt(dev_queue, |
| 232 | get_default_qdisc_ops(dev, i), |
John Fastabend | b8970f0 | 2011-01-17 08:06:09 +0000 | [diff] [blame] | 233 | TC_H_MAKE(TC_H_MAJ(sch->handle), |
Alexander Aring | a38a9882 | 2017-12-20 12:35:21 -0500 | [diff] [blame] | 234 | TC_H_MIN(i + 1)), extack); |
Eric Dumazet | 87b60cf | 2017-02-10 10:31:49 -0800 | [diff] [blame] | 235 | if (!qdisc) |
| 236 | return -ENOMEM; |
| 237 | |
John Fastabend | b8970f0 | 2011-01-17 08:06:09 +0000 | [diff] [blame] | 238 | priv->qdiscs[i] = qdisc; |
Eric Dumazet | 4eaf3b8 | 2015-12-01 20:08:51 -0800 | [diff] [blame] | 239 | qdisc->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT; |
John Fastabend | b8970f0 | 2011-01-17 08:06:09 +0000 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | /* If the mqprio options indicate that hardware should own |
| 243 | * the queue mapping then run ndo_setup_tc otherwise use the |
| 244 | * supplied and verified mapping |
| 245 | */ |
| 246 | if (qopt->hw) { |
Amritha Nambiar | 4e8b86c | 2017-09-07 04:00:06 -0700 | [diff] [blame] | 247 | struct tc_mqprio_qopt_offload mqprio = {.qopt = *qopt}; |
John Fastabend | 16e5cc6 | 2016-02-16 21:16:43 -0800 | [diff] [blame] | 248 | |
Amritha Nambiar | 4e8b86c | 2017-09-07 04:00:06 -0700 | [diff] [blame] | 249 | switch (priv->mode) { |
| 250 | case TC_MQPRIO_MODE_DCB: |
| 251 | if (priv->shaper != TC_MQPRIO_SHAPER_DCB) |
| 252 | return -EINVAL; |
| 253 | break; |
| 254 | case TC_MQPRIO_MODE_CHANNEL: |
| 255 | mqprio.flags = priv->flags; |
| 256 | if (priv->flags & TC_MQPRIO_F_MODE) |
| 257 | mqprio.mode = priv->mode; |
| 258 | if (priv->flags & TC_MQPRIO_F_SHAPER) |
| 259 | mqprio.shaper = priv->shaper; |
| 260 | if (priv->flags & TC_MQPRIO_F_MIN_RATE) |
| 261 | for (i = 0; i < mqprio.qopt.num_tc; i++) |
| 262 | mqprio.min_rate[i] = priv->min_rate[i]; |
| 263 | if (priv->flags & TC_MQPRIO_F_MAX_RATE) |
| 264 | for (i = 0; i < mqprio.qopt.num_tc; i++) |
| 265 | mqprio.max_rate[i] = priv->max_rate[i]; |
| 266 | break; |
| 267 | default: |
| 268 | return -EINVAL; |
| 269 | } |
| 270 | err = dev->netdev_ops->ndo_setup_tc(dev, |
Nogah Frankel | 575ed7d | 2017-11-06 07:23:42 +0100 | [diff] [blame] | 271 | TC_SETUP_QDISC_MQPRIO, |
Jiri Pirko | de4784c | 2017-08-07 10:15:32 +0200 | [diff] [blame] | 272 | &mqprio); |
John Fastabend | b8970f0 | 2011-01-17 08:06:09 +0000 | [diff] [blame] | 273 | if (err) |
Eric Dumazet | 87b60cf | 2017-02-10 10:31:49 -0800 | [diff] [blame] | 274 | return err; |
Alexander Duyck | 2026fec | 2017-03-15 10:39:18 -0700 | [diff] [blame] | 275 | |
Amritha Nambiar | 4e8b86c | 2017-09-07 04:00:06 -0700 | [diff] [blame] | 276 | priv->hw_offload = mqprio.qopt.hw; |
John Fastabend | b8970f0 | 2011-01-17 08:06:09 +0000 | [diff] [blame] | 277 | } else { |
| 278 | netdev_set_num_tc(dev, qopt->num_tc); |
| 279 | for (i = 0; i < qopt->num_tc; i++) |
| 280 | netdev_set_tc_queue(dev, i, |
| 281 | qopt->count[i], qopt->offset[i]); |
| 282 | } |
| 283 | |
| 284 | /* Always use supplied priority mappings */ |
| 285 | for (i = 0; i < TC_BITMASK + 1; i++) |
| 286 | netdev_set_prio_tc_map(dev, i, qopt->prio_tc_map[i]); |
| 287 | |
| 288 | sch->flags |= TCQ_F_MQROOT; |
| 289 | return 0; |
John Fastabend | b8970f0 | 2011-01-17 08:06:09 +0000 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | static void mqprio_attach(struct Qdisc *sch) |
| 293 | { |
| 294 | struct net_device *dev = qdisc_dev(sch); |
| 295 | struct mqprio_sched *priv = qdisc_priv(sch); |
Eric Dumazet | 95dc192 | 2013-12-05 11:12:02 -0800 | [diff] [blame] | 296 | struct Qdisc *qdisc, *old; |
John Fastabend | b8970f0 | 2011-01-17 08:06:09 +0000 | [diff] [blame] | 297 | unsigned int ntx; |
| 298 | |
| 299 | /* Attach underlying qdisc */ |
| 300 | for (ntx = 0; ntx < dev->num_tx_queues; ntx++) { |
| 301 | qdisc = priv->qdiscs[ntx]; |
Eric Dumazet | 95dc192 | 2013-12-05 11:12:02 -0800 | [diff] [blame] | 302 | old = dev_graft_qdisc(qdisc->dev_queue, qdisc); |
| 303 | if (old) |
Vlad Buslov | 86bd446 | 2018-09-24 19:22:50 +0300 | [diff] [blame] | 304 | qdisc_put(old); |
Eric Dumazet | 95dc192 | 2013-12-05 11:12:02 -0800 | [diff] [blame] | 305 | if (ntx < dev->real_num_tx_queues) |
Jiri Kosina | 49b4997 | 2017-03-08 16:03:32 +0100 | [diff] [blame] | 306 | qdisc_hash_add(qdisc, false); |
John Fastabend | b8970f0 | 2011-01-17 08:06:09 +0000 | [diff] [blame] | 307 | } |
| 308 | kfree(priv->qdiscs); |
| 309 | priv->qdiscs = NULL; |
| 310 | } |
| 311 | |
| 312 | static struct netdev_queue *mqprio_queue_get(struct Qdisc *sch, |
| 313 | unsigned long cl) |
| 314 | { |
| 315 | struct net_device *dev = qdisc_dev(sch); |
Alexander Duyck | 3230290 | 2017-10-12 11:38:45 -0700 | [diff] [blame] | 316 | unsigned long ntx = cl - 1; |
John Fastabend | b8970f0 | 2011-01-17 08:06:09 +0000 | [diff] [blame] | 317 | |
| 318 | if (ntx >= dev->num_tx_queues) |
| 319 | return NULL; |
| 320 | return netdev_get_tx_queue(dev, ntx); |
| 321 | } |
| 322 | |
| 323 | static int mqprio_graft(struct Qdisc *sch, unsigned long cl, struct Qdisc *new, |
Alexander Aring | 653d6fd | 2017-12-20 12:35:17 -0500 | [diff] [blame] | 324 | struct Qdisc **old, struct netlink_ext_ack *extack) |
John Fastabend | b8970f0 | 2011-01-17 08:06:09 +0000 | [diff] [blame] | 325 | { |
| 326 | struct net_device *dev = qdisc_dev(sch); |
| 327 | struct netdev_queue *dev_queue = mqprio_queue_get(sch, cl); |
| 328 | |
| 329 | if (!dev_queue) |
| 330 | return -EINVAL; |
| 331 | |
| 332 | if (dev->flags & IFF_UP) |
| 333 | dev_deactivate(dev); |
| 334 | |
| 335 | *old = dev_graft_qdisc(dev_queue, new); |
| 336 | |
Eric Dumazet | 1abbe13 | 2012-12-11 15:54:33 +0000 | [diff] [blame] | 337 | if (new) |
Eric Dumazet | 4eaf3b8 | 2015-12-01 20:08:51 -0800 | [diff] [blame] | 338 | new->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT; |
Eric Dumazet | 1abbe13 | 2012-12-11 15:54:33 +0000 | [diff] [blame] | 339 | |
John Fastabend | b8970f0 | 2011-01-17 08:06:09 +0000 | [diff] [blame] | 340 | if (dev->flags & IFF_UP) |
| 341 | dev_activate(dev); |
| 342 | |
| 343 | return 0; |
| 344 | } |
| 345 | |
Amritha Nambiar | 4e8b86c | 2017-09-07 04:00:06 -0700 | [diff] [blame] | 346 | static int dump_rates(struct mqprio_sched *priv, |
| 347 | struct tc_mqprio_qopt *opt, struct sk_buff *skb) |
| 348 | { |
| 349 | struct nlattr *nest; |
| 350 | int i; |
| 351 | |
| 352 | if (priv->flags & TC_MQPRIO_F_MIN_RATE) { |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 353 | nest = nla_nest_start_noflag(skb, TCA_MQPRIO_MIN_RATE64); |
Amritha Nambiar | 4e8b86c | 2017-09-07 04:00:06 -0700 | [diff] [blame] | 354 | if (!nest) |
| 355 | goto nla_put_failure; |
| 356 | |
| 357 | for (i = 0; i < opt->num_tc; i++) { |
| 358 | if (nla_put(skb, TCA_MQPRIO_MIN_RATE64, |
| 359 | sizeof(priv->min_rate[i]), |
| 360 | &priv->min_rate[i])) |
| 361 | goto nla_put_failure; |
| 362 | } |
| 363 | nla_nest_end(skb, nest); |
| 364 | } |
| 365 | |
| 366 | if (priv->flags & TC_MQPRIO_F_MAX_RATE) { |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 367 | nest = nla_nest_start_noflag(skb, TCA_MQPRIO_MAX_RATE64); |
Amritha Nambiar | 4e8b86c | 2017-09-07 04:00:06 -0700 | [diff] [blame] | 368 | if (!nest) |
| 369 | goto nla_put_failure; |
| 370 | |
| 371 | for (i = 0; i < opt->num_tc; i++) { |
| 372 | if (nla_put(skb, TCA_MQPRIO_MAX_RATE64, |
| 373 | sizeof(priv->max_rate[i]), |
| 374 | &priv->max_rate[i])) |
| 375 | goto nla_put_failure; |
| 376 | } |
| 377 | nla_nest_end(skb, nest); |
| 378 | } |
| 379 | return 0; |
| 380 | |
| 381 | nla_put_failure: |
| 382 | nla_nest_cancel(skb, nest); |
| 383 | return -1; |
| 384 | } |
| 385 | |
John Fastabend | b8970f0 | 2011-01-17 08:06:09 +0000 | [diff] [blame] | 386 | static int mqprio_dump(struct Qdisc *sch, struct sk_buff *skb) |
| 387 | { |
| 388 | struct net_device *dev = qdisc_dev(sch); |
| 389 | struct mqprio_sched *priv = qdisc_priv(sch); |
Amritha Nambiar | 4e8b86c | 2017-09-07 04:00:06 -0700 | [diff] [blame] | 390 | struct nlattr *nla = (struct nlattr *)skb_tail_pointer(skb); |
Eric Dumazet | 144ce87 | 2011-01-26 07:21:57 +0000 | [diff] [blame] | 391 | struct tc_mqprio_qopt opt = { 0 }; |
John Fastabend | b8970f0 | 2011-01-17 08:06:09 +0000 | [diff] [blame] | 392 | struct Qdisc *qdisc; |
John Fastabend | ce679e8 | 2017-12-07 09:57:39 -0800 | [diff] [blame] | 393 | unsigned int ntx, tc; |
John Fastabend | b8970f0 | 2011-01-17 08:06:09 +0000 | [diff] [blame] | 394 | |
| 395 | sch->q.qlen = 0; |
| 396 | memset(&sch->bstats, 0, sizeof(sch->bstats)); |
| 397 | memset(&sch->qstats, 0, sizeof(sch->qstats)); |
| 398 | |
John Fastabend | ce679e8 | 2017-12-07 09:57:39 -0800 | [diff] [blame] | 399 | /* MQ supports lockless qdiscs. However, statistics accounting needs |
| 400 | * to account for all, none, or a mix of locked and unlocked child |
| 401 | * qdiscs. Percpu stats are added to counters in-band and locking |
| 402 | * qdisc totals are added at end. |
| 403 | */ |
| 404 | for (ntx = 0; ntx < dev->num_tx_queues; ntx++) { |
| 405 | qdisc = netdev_get_tx_queue(dev, ntx)->qdisc_sleeping; |
John Fastabend | b8970f0 | 2011-01-17 08:06:09 +0000 | [diff] [blame] | 406 | spin_lock_bh(qdisc_lock(qdisc)); |
John Fastabend | ce679e8 | 2017-12-07 09:57:39 -0800 | [diff] [blame] | 407 | |
| 408 | if (qdisc_is_percpu_stats(qdisc)) { |
| 409 | __u32 qlen = qdisc_qlen_sum(qdisc); |
| 410 | |
| 411 | __gnet_stats_copy_basic(NULL, &sch->bstats, |
| 412 | qdisc->cpu_bstats, |
| 413 | &qdisc->bstats); |
| 414 | __gnet_stats_copy_queue(&sch->qstats, |
| 415 | qdisc->cpu_qstats, |
| 416 | &qdisc->qstats, qlen); |
| 417 | } else { |
| 418 | sch->q.qlen += qdisc->q.qlen; |
| 419 | sch->bstats.bytes += qdisc->bstats.bytes; |
| 420 | sch->bstats.packets += qdisc->bstats.packets; |
| 421 | sch->qstats.backlog += qdisc->qstats.backlog; |
| 422 | sch->qstats.drops += qdisc->qstats.drops; |
| 423 | sch->qstats.requeues += qdisc->qstats.requeues; |
| 424 | sch->qstats.overlimits += qdisc->qstats.overlimits; |
| 425 | } |
| 426 | |
John Fastabend | b8970f0 | 2011-01-17 08:06:09 +0000 | [diff] [blame] | 427 | spin_unlock_bh(qdisc_lock(qdisc)); |
| 428 | } |
| 429 | |
| 430 | opt.num_tc = netdev_get_num_tc(dev); |
| 431 | memcpy(opt.prio_tc_map, dev->prio_tc_map, sizeof(opt.prio_tc_map)); |
Alexander Duyck | 2026fec | 2017-03-15 10:39:18 -0700 | [diff] [blame] | 432 | opt.hw = priv->hw_offload; |
John Fastabend | b8970f0 | 2011-01-17 08:06:09 +0000 | [diff] [blame] | 433 | |
John Fastabend | ce679e8 | 2017-12-07 09:57:39 -0800 | [diff] [blame] | 434 | for (tc = 0; tc < netdev_get_num_tc(dev); tc++) { |
| 435 | opt.count[tc] = dev->tc_to_txq[tc].count; |
| 436 | opt.offset[tc] = dev->tc_to_txq[tc].offset; |
John Fastabend | b8970f0 | 2011-01-17 08:06:09 +0000 | [diff] [blame] | 437 | } |
| 438 | |
Amritha Nambiar | 4e8b86c | 2017-09-07 04:00:06 -0700 | [diff] [blame] | 439 | if (nla_put(skb, TCA_OPTIONS, NLA_ALIGN(sizeof(opt)), &opt)) |
David S. Miller | 1b34ec4 | 2012-03-29 05:11:39 -0400 | [diff] [blame] | 440 | goto nla_put_failure; |
John Fastabend | b8970f0 | 2011-01-17 08:06:09 +0000 | [diff] [blame] | 441 | |
Amritha Nambiar | 4e8b86c | 2017-09-07 04:00:06 -0700 | [diff] [blame] | 442 | if ((priv->flags & TC_MQPRIO_F_MODE) && |
| 443 | nla_put_u16(skb, TCA_MQPRIO_MODE, priv->mode)) |
| 444 | goto nla_put_failure; |
| 445 | |
| 446 | if ((priv->flags & TC_MQPRIO_F_SHAPER) && |
| 447 | nla_put_u16(skb, TCA_MQPRIO_SHAPER, priv->shaper)) |
| 448 | goto nla_put_failure; |
| 449 | |
| 450 | if ((priv->flags & TC_MQPRIO_F_MIN_RATE || |
| 451 | priv->flags & TC_MQPRIO_F_MAX_RATE) && |
| 452 | (dump_rates(priv, &opt, skb) != 0)) |
| 453 | goto nla_put_failure; |
| 454 | |
| 455 | return nla_nest_end(skb, nla); |
John Fastabend | b8970f0 | 2011-01-17 08:06:09 +0000 | [diff] [blame] | 456 | nla_put_failure: |
Amritha Nambiar | 4e8b86c | 2017-09-07 04:00:06 -0700 | [diff] [blame] | 457 | nlmsg_trim(skb, nla); |
John Fastabend | b8970f0 | 2011-01-17 08:06:09 +0000 | [diff] [blame] | 458 | return -1; |
| 459 | } |
| 460 | |
| 461 | static struct Qdisc *mqprio_leaf(struct Qdisc *sch, unsigned long cl) |
| 462 | { |
| 463 | struct netdev_queue *dev_queue = mqprio_queue_get(sch, cl); |
| 464 | |
| 465 | if (!dev_queue) |
| 466 | return NULL; |
| 467 | |
| 468 | return dev_queue->qdisc_sleeping; |
| 469 | } |
| 470 | |
WANG Cong | 143976c | 2017-08-24 16:51:29 -0700 | [diff] [blame] | 471 | static unsigned long mqprio_find(struct Qdisc *sch, u32 classid) |
John Fastabend | b8970f0 | 2011-01-17 08:06:09 +0000 | [diff] [blame] | 472 | { |
| 473 | struct net_device *dev = qdisc_dev(sch); |
| 474 | unsigned int ntx = TC_H_MIN(classid); |
| 475 | |
Alexander Duyck | 3230290 | 2017-10-12 11:38:45 -0700 | [diff] [blame] | 476 | /* There are essentially two regions here that have valid classid |
| 477 | * values. The first region will have a classid value of 1 through |
| 478 | * num_tx_queues. All of these are backed by actual Qdiscs. |
| 479 | */ |
| 480 | if (ntx < TC_H_MIN_PRIORITY) |
| 481 | return (ntx <= dev->num_tx_queues) ? ntx : 0; |
| 482 | |
| 483 | /* The second region represents the hardware traffic classes. These |
| 484 | * are represented by classid values of TC_H_MIN_PRIORITY through |
| 485 | * TC_H_MIN_PRIORITY + netdev_get_num_tc - 1 |
| 486 | */ |
| 487 | return ((ntx - TC_H_MIN_PRIORITY) < netdev_get_num_tc(dev)) ? ntx : 0; |
John Fastabend | b8970f0 | 2011-01-17 08:06:09 +0000 | [diff] [blame] | 488 | } |
| 489 | |
John Fastabend | b8970f0 | 2011-01-17 08:06:09 +0000 | [diff] [blame] | 490 | static int mqprio_dump_class(struct Qdisc *sch, unsigned long cl, |
| 491 | struct sk_buff *skb, struct tcmsg *tcm) |
| 492 | { |
Alexander Duyck | 3230290 | 2017-10-12 11:38:45 -0700 | [diff] [blame] | 493 | if (cl < TC_H_MIN_PRIORITY) { |
| 494 | struct netdev_queue *dev_queue = mqprio_queue_get(sch, cl); |
| 495 | struct net_device *dev = qdisc_dev(sch); |
| 496 | int tc = netdev_txq_to_tc(dev, cl - 1); |
John Fastabend | b8970f0 | 2011-01-17 08:06:09 +0000 | [diff] [blame] | 497 | |
Alexander Duyck | 3230290 | 2017-10-12 11:38:45 -0700 | [diff] [blame] | 498 | tcm->tcm_parent = (tc < 0) ? 0 : |
| 499 | TC_H_MAKE(TC_H_MAJ(sch->handle), |
| 500 | TC_H_MIN(tc + TC_H_MIN_PRIORITY)); |
| 501 | tcm->tcm_info = dev_queue->qdisc_sleeping->handle; |
| 502 | } else { |
John Fastabend | b8970f0 | 2011-01-17 08:06:09 +0000 | [diff] [blame] | 503 | tcm->tcm_parent = TC_H_ROOT; |
| 504 | tcm->tcm_info = 0; |
John Fastabend | b8970f0 | 2011-01-17 08:06:09 +0000 | [diff] [blame] | 505 | } |
| 506 | tcm->tcm_handle |= TC_H_MIN(cl); |
| 507 | return 0; |
| 508 | } |
| 509 | |
| 510 | static int mqprio_dump_class_stats(struct Qdisc *sch, unsigned long cl, |
stephen hemminger | ea18fd9 | 2011-02-23 09:06:51 +0000 | [diff] [blame] | 511 | struct gnet_dump *d) |
| 512 | __releases(d->lock) |
| 513 | __acquires(d->lock) |
John Fastabend | b8970f0 | 2011-01-17 08:06:09 +0000 | [diff] [blame] | 514 | { |
Alexander Duyck | 3230290 | 2017-10-12 11:38:45 -0700 | [diff] [blame] | 515 | if (cl >= TC_H_MIN_PRIORITY) { |
John Fastabend | b8970f0 | 2011-01-17 08:06:09 +0000 | [diff] [blame] | 516 | int i; |
John Fastabend | 6401585 | 2014-09-28 11:53:57 -0700 | [diff] [blame] | 517 | __u32 qlen = 0; |
John Fastabend | b8970f0 | 2011-01-17 08:06:09 +0000 | [diff] [blame] | 518 | struct gnet_stats_queue qstats = {0}; |
| 519 | struct gnet_stats_basic_packed bstats = {0}; |
Alexander Duyck | 3230290 | 2017-10-12 11:38:45 -0700 | [diff] [blame] | 520 | struct net_device *dev = qdisc_dev(sch); |
| 521 | struct netdev_tc_txq tc = dev->tc_to_txq[cl & TC_BITMASK]; |
John Fastabend | b8970f0 | 2011-01-17 08:06:09 +0000 | [diff] [blame] | 522 | |
| 523 | /* Drop lock here it will be reclaimed before touching |
| 524 | * statistics this is required because the d->lock we |
| 525 | * hold here is the look on dev_queue->qdisc_sleeping |
| 526 | * also acquired below. |
| 527 | */ |
Eric Dumazet | edb09eb | 2016-06-06 09:37:16 -0700 | [diff] [blame] | 528 | if (d->lock) |
| 529 | spin_unlock_bh(d->lock); |
John Fastabend | b8970f0 | 2011-01-17 08:06:09 +0000 | [diff] [blame] | 530 | |
| 531 | for (i = tc.offset; i < tc.offset + tc.count; i++) { |
John Fastabend | 46e5da40a | 2014-09-12 20:04:52 -0700 | [diff] [blame] | 532 | struct netdev_queue *q = netdev_get_tx_queue(dev, i); |
John Fastabend | ce679e8 | 2017-12-07 09:57:39 -0800 | [diff] [blame] | 533 | struct Qdisc *qdisc = rtnl_dereference(q->qdisc); |
| 534 | struct gnet_stats_basic_cpu __percpu *cpu_bstats = NULL; |
| 535 | struct gnet_stats_queue __percpu *cpu_qstats = NULL; |
John Fastabend | 46e5da40a | 2014-09-12 20:04:52 -0700 | [diff] [blame] | 536 | |
John Fastabend | b8970f0 | 2011-01-17 08:06:09 +0000 | [diff] [blame] | 537 | spin_lock_bh(qdisc_lock(qdisc)); |
John Fastabend | ce679e8 | 2017-12-07 09:57:39 -0800 | [diff] [blame] | 538 | if (qdisc_is_percpu_stats(qdisc)) { |
| 539 | cpu_bstats = qdisc->cpu_bstats; |
| 540 | cpu_qstats = qdisc->cpu_qstats; |
| 541 | } |
| 542 | |
| 543 | qlen = qdisc_qlen_sum(qdisc); |
| 544 | __gnet_stats_copy_basic(NULL, &sch->bstats, |
| 545 | cpu_bstats, &qdisc->bstats); |
| 546 | __gnet_stats_copy_queue(&sch->qstats, |
| 547 | cpu_qstats, |
| 548 | &qdisc->qstats, |
| 549 | qlen); |
John Fastabend | b8970f0 | 2011-01-17 08:06:09 +0000 | [diff] [blame] | 550 | spin_unlock_bh(qdisc_lock(qdisc)); |
| 551 | } |
John Fastabend | ce679e8 | 2017-12-07 09:57:39 -0800 | [diff] [blame] | 552 | |
John Fastabend | b8970f0 | 2011-01-17 08:06:09 +0000 | [diff] [blame] | 553 | /* Reclaim root sleeping lock before completing stats */ |
Eric Dumazet | edb09eb | 2016-06-06 09:37:16 -0700 | [diff] [blame] | 554 | if (d->lock) |
| 555 | spin_lock_bh(d->lock); |
| 556 | if (gnet_stats_copy_basic(NULL, d, NULL, &bstats) < 0 || |
John Fastabend | b0ab6f9 | 2014-09-28 11:54:24 -0700 | [diff] [blame] | 557 | gnet_stats_copy_queue(d, NULL, &qstats, qlen) < 0) |
John Fastabend | b8970f0 | 2011-01-17 08:06:09 +0000 | [diff] [blame] | 558 | return -1; |
| 559 | } else { |
| 560 | struct netdev_queue *dev_queue = mqprio_queue_get(sch, cl); |
| 561 | |
| 562 | sch = dev_queue->qdisc_sleeping; |
Eric Dumazet | edb09eb | 2016-06-06 09:37:16 -0700 | [diff] [blame] | 563 | if (gnet_stats_copy_basic(qdisc_root_sleeping_running(sch), |
| 564 | d, NULL, &sch->bstats) < 0 || |
Paolo Abeni | 5dd431b | 2019-03-28 16:53:12 +0100 | [diff] [blame] | 565 | qdisc_qstats_copy(d, sch) < 0) |
John Fastabend | b8970f0 | 2011-01-17 08:06:09 +0000 | [diff] [blame] | 566 | return -1; |
| 567 | } |
| 568 | return 0; |
| 569 | } |
| 570 | |
| 571 | static void mqprio_walk(struct Qdisc *sch, struct qdisc_walker *arg) |
| 572 | { |
| 573 | struct net_device *dev = qdisc_dev(sch); |
| 574 | unsigned long ntx; |
| 575 | |
| 576 | if (arg->stop) |
| 577 | return; |
| 578 | |
| 579 | /* Walk hierarchy with a virtual class per tc */ |
| 580 | arg->count = arg->skip; |
Alexander Duyck | 3230290 | 2017-10-12 11:38:45 -0700 | [diff] [blame] | 581 | for (ntx = arg->skip; ntx < netdev_get_num_tc(dev); ntx++) { |
| 582 | if (arg->fn(sch, ntx + TC_H_MIN_PRIORITY, arg) < 0) { |
| 583 | arg->stop = 1; |
| 584 | return; |
| 585 | } |
| 586 | arg->count++; |
| 587 | } |
| 588 | |
| 589 | /* Pad the values and skip over unused traffic classes */ |
| 590 | if (ntx < TC_MAX_QUEUE) { |
| 591 | arg->count = TC_MAX_QUEUE; |
| 592 | ntx = TC_MAX_QUEUE; |
| 593 | } |
| 594 | |
| 595 | /* Reset offset, sort out remaining per-queue qdiscs */ |
| 596 | for (ntx -= TC_MAX_QUEUE; ntx < dev->num_tx_queues; ntx++) { |
John Fastabend | b8970f0 | 2011-01-17 08:06:09 +0000 | [diff] [blame] | 597 | if (arg->fn(sch, ntx + 1, arg) < 0) { |
| 598 | arg->stop = 1; |
Alexander Duyck | 3230290 | 2017-10-12 11:38:45 -0700 | [diff] [blame] | 599 | return; |
John Fastabend | b8970f0 | 2011-01-17 08:06:09 +0000 | [diff] [blame] | 600 | } |
| 601 | arg->count++; |
| 602 | } |
| 603 | } |
| 604 | |
Jesus Sanchez-Palencia | 0f7787b | 2017-10-16 18:01:25 -0700 | [diff] [blame] | 605 | static struct netdev_queue *mqprio_select_queue(struct Qdisc *sch, |
| 606 | struct tcmsg *tcm) |
| 607 | { |
| 608 | return mqprio_queue_get(sch, TC_H_MIN(tcm->tcm_parent)); |
| 609 | } |
| 610 | |
John Fastabend | b8970f0 | 2011-01-17 08:06:09 +0000 | [diff] [blame] | 611 | static const struct Qdisc_class_ops mqprio_class_ops = { |
| 612 | .graft = mqprio_graft, |
| 613 | .leaf = mqprio_leaf, |
WANG Cong | 143976c | 2017-08-24 16:51:29 -0700 | [diff] [blame] | 614 | .find = mqprio_find, |
John Fastabend | b8970f0 | 2011-01-17 08:06:09 +0000 | [diff] [blame] | 615 | .walk = mqprio_walk, |
| 616 | .dump = mqprio_dump_class, |
| 617 | .dump_stats = mqprio_dump_class_stats, |
Jesus Sanchez-Palencia | 0f7787b | 2017-10-16 18:01:25 -0700 | [diff] [blame] | 618 | .select_queue = mqprio_select_queue, |
John Fastabend | b8970f0 | 2011-01-17 08:06:09 +0000 | [diff] [blame] | 619 | }; |
| 620 | |
stephen hemminger | ea18fd9 | 2011-02-23 09:06:51 +0000 | [diff] [blame] | 621 | static struct Qdisc_ops mqprio_qdisc_ops __read_mostly = { |
John Fastabend | b8970f0 | 2011-01-17 08:06:09 +0000 | [diff] [blame] | 622 | .cl_ops = &mqprio_class_ops, |
| 623 | .id = "mqprio", |
| 624 | .priv_size = sizeof(struct mqprio_sched), |
| 625 | .init = mqprio_init, |
| 626 | .destroy = mqprio_destroy, |
| 627 | .attach = mqprio_attach, |
| 628 | .dump = mqprio_dump, |
| 629 | .owner = THIS_MODULE, |
| 630 | }; |
| 631 | |
| 632 | static int __init mqprio_module_init(void) |
| 633 | { |
| 634 | return register_qdisc(&mqprio_qdisc_ops); |
| 635 | } |
| 636 | |
| 637 | static void __exit mqprio_module_exit(void) |
| 638 | { |
| 639 | unregister_qdisc(&mqprio_qdisc_ops); |
| 640 | } |
| 641 | |
| 642 | module_init(mqprio_module_init); |
| 643 | module_exit(mqprio_module_exit); |
| 644 | |
| 645 | MODULE_LICENSE("GPL"); |