1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2b8970f0bSJohn Fastabend /* 3b8970f0bSJohn Fastabend * net/sched/sch_mqprio.c 4b8970f0bSJohn Fastabend * 5b8970f0bSJohn Fastabend * Copyright (c) 2010 John Fastabend <john.r.fastabend@intel.com> 6b8970f0bSJohn Fastabend */ 7b8970f0bSJohn Fastabend 8b8970f0bSJohn Fastabend #include <linux/types.h> 9b8970f0bSJohn Fastabend #include <linux/slab.h> 10b8970f0bSJohn Fastabend #include <linux/kernel.h> 11b8970f0bSJohn Fastabend #include <linux/string.h> 12b8970f0bSJohn Fastabend #include <linux/errno.h> 13b8970f0bSJohn Fastabend #include <linux/skbuff.h> 143a9a231dSPaul Gortmaker #include <linux/module.h> 15b8970f0bSJohn Fastabend #include <net/netlink.h> 16b8970f0bSJohn Fastabend #include <net/pkt_sched.h> 17b8970f0bSJohn Fastabend #include <net/sch_generic.h> 184e8b86c0SAmritha Nambiar #include <net/pkt_cls.h> 19b8970f0bSJohn Fastabend 20b8970f0bSJohn Fastabend struct mqprio_sched { 21b8970f0bSJohn Fastabend struct Qdisc **qdiscs; 224e8b86c0SAmritha Nambiar u16 mode; 234e8b86c0SAmritha Nambiar u16 shaper; 242026fecfSAlexander Duyck int hw_offload; 254e8b86c0SAmritha Nambiar u32 flags; 264e8b86c0SAmritha Nambiar u64 min_rate[TC_QOPT_MAX_QUEUE]; 274e8b86c0SAmritha Nambiar u64 max_rate[TC_QOPT_MAX_QUEUE]; 28b8970f0bSJohn Fastabend }; 29b8970f0bSJohn Fastabend 30b8970f0bSJohn Fastabend static void mqprio_destroy(struct Qdisc *sch) 31b8970f0bSJohn Fastabend { 32b8970f0bSJohn Fastabend struct net_device *dev = qdisc_dev(sch); 33b8970f0bSJohn Fastabend struct mqprio_sched *priv = qdisc_priv(sch); 34b8970f0bSJohn Fastabend unsigned int ntx; 35b8970f0bSJohn Fastabend 36ac7100baSBen Hutchings if (priv->qdiscs) { 37ac7100baSBen Hutchings for (ntx = 0; 38ac7100baSBen Hutchings ntx < dev->num_tx_queues && priv->qdiscs[ntx]; 39ac7100baSBen Hutchings ntx++) 4086bd446bSVlad Buslov qdisc_put(priv->qdiscs[ntx]); 41ac7100baSBen Hutchings kfree(priv->qdiscs); 42ac7100baSBen Hutchings } 43b8970f0bSJohn Fastabend 4456f36acdSAmritha Nambiar if (priv->hw_offload && dev->netdev_ops->ndo_setup_tc) { 454e8b86c0SAmritha Nambiar struct tc_mqprio_qopt_offload mqprio = { { 0 } }; 4656f36acdSAmritha Nambiar 474e8b86c0SAmritha Nambiar switch (priv->mode) { 484e8b86c0SAmritha Nambiar case TC_MQPRIO_MODE_DCB: 494e8b86c0SAmritha Nambiar case TC_MQPRIO_MODE_CHANNEL: 50575ed7d3SNogah Frankel dev->netdev_ops->ndo_setup_tc(dev, 51575ed7d3SNogah Frankel TC_SETUP_QDISC_MQPRIO, 524e8b86c0SAmritha Nambiar &mqprio); 534e8b86c0SAmritha Nambiar break; 544e8b86c0SAmritha Nambiar default: 554e8b86c0SAmritha Nambiar return; 564e8b86c0SAmritha Nambiar } 5756f36acdSAmritha Nambiar } else { 58b8970f0bSJohn Fastabend netdev_set_num_tc(dev, 0); 59b8970f0bSJohn Fastabend } 6056f36acdSAmritha Nambiar } 61b8970f0bSJohn Fastabend 62b8970f0bSJohn Fastabend static int mqprio_parse_opt(struct net_device *dev, struct tc_mqprio_qopt *qopt) 63b8970f0bSJohn Fastabend { 64b8970f0bSJohn Fastabend int i, j; 65b8970f0bSJohn Fastabend 66b8970f0bSJohn Fastabend /* Verify num_tc is not out of max range */ 67b8970f0bSJohn Fastabend if (qopt->num_tc > TC_MAX_QUEUE) 68b8970f0bSJohn Fastabend return -EINVAL; 69b8970f0bSJohn Fastabend 70b8970f0bSJohn Fastabend /* Verify priority mapping uses valid tcs */ 71b8970f0bSJohn Fastabend for (i = 0; i < TC_BITMASK + 1; i++) { 72b8970f0bSJohn Fastabend if (qopt->prio_tc_map[i] >= qopt->num_tc) 73b8970f0bSJohn Fastabend return -EINVAL; 74b8970f0bSJohn Fastabend } 75b8970f0bSJohn Fastabend 762026fecfSAlexander Duyck /* Limit qopt->hw to maximum supported offload value. Drivers have 772026fecfSAlexander Duyck * the option of overriding this later if they don't support the a 782026fecfSAlexander Duyck * given offload type. 792026fecfSAlexander Duyck */ 802026fecfSAlexander Duyck if (qopt->hw > TC_MQPRIO_HW_OFFLOAD_MAX) 812026fecfSAlexander Duyck qopt->hw = TC_MQPRIO_HW_OFFLOAD_MAX; 82b8970f0bSJohn Fastabend 832026fecfSAlexander Duyck /* If hardware offload is requested we will leave it to the device 842026fecfSAlexander Duyck * to either populate the queue counts itself or to validate the 852026fecfSAlexander Duyck * provided queue counts. If ndo_setup_tc is not present then 862026fecfSAlexander Duyck * hardware doesn't support offload and we should return an error. 87b8970f0bSJohn Fastabend */ 88b8970f0bSJohn Fastabend if (qopt->hw) 892026fecfSAlexander Duyck return dev->netdev_ops->ndo_setup_tc ? 0 : -EINVAL; 90b8970f0bSJohn Fastabend 91b8970f0bSJohn Fastabend for (i = 0; i < qopt->num_tc; i++) { 92b8970f0bSJohn Fastabend unsigned int last = qopt->offset[i] + qopt->count[i]; 93b8970f0bSJohn Fastabend 94b8970f0bSJohn Fastabend /* Verify the queue count is in tx range being equal to the 95b8970f0bSJohn Fastabend * real_num_tx_queues indicates the last queue is in use. 96b8970f0bSJohn Fastabend */ 97b8970f0bSJohn Fastabend if (qopt->offset[i] >= dev->real_num_tx_queues || 98b8970f0bSJohn Fastabend !qopt->count[i] || 99b8970f0bSJohn Fastabend last > dev->real_num_tx_queues) 100b8970f0bSJohn Fastabend return -EINVAL; 101b8970f0bSJohn Fastabend 102b8970f0bSJohn Fastabend /* Verify that the offset and counts do not overlap */ 103b8970f0bSJohn Fastabend for (j = i + 1; j < qopt->num_tc; j++) { 104b8970f0bSJohn Fastabend if (last > qopt->offset[j]) 105b8970f0bSJohn Fastabend return -EINVAL; 106b8970f0bSJohn Fastabend } 107b8970f0bSJohn Fastabend } 108b8970f0bSJohn Fastabend 109b8970f0bSJohn Fastabend return 0; 110b8970f0bSJohn Fastabend } 111b8970f0bSJohn Fastabend 1124e8b86c0SAmritha Nambiar static const struct nla_policy mqprio_policy[TCA_MQPRIO_MAX + 1] = { 1134e8b86c0SAmritha Nambiar [TCA_MQPRIO_MODE] = { .len = sizeof(u16) }, 1144e8b86c0SAmritha Nambiar [TCA_MQPRIO_SHAPER] = { .len = sizeof(u16) }, 1154e8b86c0SAmritha Nambiar [TCA_MQPRIO_MIN_RATE64] = { .type = NLA_NESTED }, 1164e8b86c0SAmritha Nambiar [TCA_MQPRIO_MAX_RATE64] = { .type = NLA_NESTED }, 1174e8b86c0SAmritha Nambiar }; 1184e8b86c0SAmritha Nambiar 1194e8b86c0SAmritha Nambiar static int parse_attr(struct nlattr *tb[], int maxtype, struct nlattr *nla, 1204e8b86c0SAmritha Nambiar const struct nla_policy *policy, int len) 1214e8b86c0SAmritha Nambiar { 1224e8b86c0SAmritha Nambiar int nested_len = nla_len(nla) - NLA_ALIGN(len); 1234e8b86c0SAmritha Nambiar 1244e8b86c0SAmritha Nambiar if (nested_len >= nla_attr_size(0)) 1258cb08174SJohannes Berg return nla_parse_deprecated(tb, maxtype, 1268cb08174SJohannes Berg nla_data(nla) + NLA_ALIGN(len), 1274e8b86c0SAmritha Nambiar nested_len, policy, NULL); 1284e8b86c0SAmritha Nambiar 1294e8b86c0SAmritha Nambiar memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1)); 1304e8b86c0SAmritha Nambiar return 0; 1314e8b86c0SAmritha Nambiar } 1324e8b86c0SAmritha Nambiar 133*feb2cf3dSVladimir Oltean static int mqprio_parse_nlattr(struct Qdisc *sch, struct tc_mqprio_qopt *qopt, 134*feb2cf3dSVladimir Oltean struct nlattr *opt) 135b8970f0bSJohn Fastabend { 136b8970f0bSJohn Fastabend struct mqprio_sched *priv = qdisc_priv(sch); 1374e8b86c0SAmritha Nambiar struct nlattr *tb[TCA_MQPRIO_MAX + 1]; 1384e8b86c0SAmritha Nambiar struct nlattr *attr; 139*feb2cf3dSVladimir Oltean int i, rem, err; 140b8970f0bSJohn Fastabend 1414e8b86c0SAmritha Nambiar err = parse_attr(tb, TCA_MQPRIO_MAX, opt, mqprio_policy, 1424e8b86c0SAmritha Nambiar sizeof(*qopt)); 1434e8b86c0SAmritha Nambiar if (err < 0) 1444e8b86c0SAmritha Nambiar return err; 1454e8b86c0SAmritha Nambiar 1464e8b86c0SAmritha Nambiar if (!qopt->hw) 1474e8b86c0SAmritha Nambiar return -EINVAL; 1484e8b86c0SAmritha Nambiar 1494e8b86c0SAmritha Nambiar if (tb[TCA_MQPRIO_MODE]) { 1504e8b86c0SAmritha Nambiar priv->flags |= TC_MQPRIO_F_MODE; 1514e8b86c0SAmritha Nambiar priv->mode = *(u16 *)nla_data(tb[TCA_MQPRIO_MODE]); 1524e8b86c0SAmritha Nambiar } 1534e8b86c0SAmritha Nambiar 1544e8b86c0SAmritha Nambiar if (tb[TCA_MQPRIO_SHAPER]) { 1554e8b86c0SAmritha Nambiar priv->flags |= TC_MQPRIO_F_SHAPER; 1564e8b86c0SAmritha Nambiar priv->shaper = *(u16 *)nla_data(tb[TCA_MQPRIO_SHAPER]); 1574e8b86c0SAmritha Nambiar } 1584e8b86c0SAmritha Nambiar 1594e8b86c0SAmritha Nambiar if (tb[TCA_MQPRIO_MIN_RATE64]) { 1604e8b86c0SAmritha Nambiar if (priv->shaper != TC_MQPRIO_SHAPER_BW_RATE) 1614e8b86c0SAmritha Nambiar return -EINVAL; 1624e8b86c0SAmritha Nambiar i = 0; 1634e8b86c0SAmritha Nambiar nla_for_each_nested(attr, tb[TCA_MQPRIO_MIN_RATE64], 1644e8b86c0SAmritha Nambiar rem) { 1654e8b86c0SAmritha Nambiar if (nla_type(attr) != TCA_MQPRIO_MIN_RATE64) 1664e8b86c0SAmritha Nambiar return -EINVAL; 1674e8b86c0SAmritha Nambiar if (i >= qopt->num_tc) 1684e8b86c0SAmritha Nambiar break; 1694e8b86c0SAmritha Nambiar priv->min_rate[i] = *(u64 *)nla_data(attr); 1704e8b86c0SAmritha Nambiar i++; 1714e8b86c0SAmritha Nambiar } 1724e8b86c0SAmritha Nambiar priv->flags |= TC_MQPRIO_F_MIN_RATE; 1734e8b86c0SAmritha Nambiar } 1744e8b86c0SAmritha Nambiar 1754e8b86c0SAmritha Nambiar if (tb[TCA_MQPRIO_MAX_RATE64]) { 1764e8b86c0SAmritha Nambiar if (priv->shaper != TC_MQPRIO_SHAPER_BW_RATE) 1774e8b86c0SAmritha Nambiar return -EINVAL; 1784e8b86c0SAmritha Nambiar i = 0; 1794e8b86c0SAmritha Nambiar nla_for_each_nested(attr, tb[TCA_MQPRIO_MAX_RATE64], 1804e8b86c0SAmritha Nambiar rem) { 1814e8b86c0SAmritha Nambiar if (nla_type(attr) != TCA_MQPRIO_MAX_RATE64) 1824e8b86c0SAmritha Nambiar return -EINVAL; 1834e8b86c0SAmritha Nambiar if (i >= qopt->num_tc) 1844e8b86c0SAmritha Nambiar break; 1854e8b86c0SAmritha Nambiar priv->max_rate[i] = *(u64 *)nla_data(attr); 1864e8b86c0SAmritha Nambiar i++; 1874e8b86c0SAmritha Nambiar } 1884e8b86c0SAmritha Nambiar priv->flags |= TC_MQPRIO_F_MAX_RATE; 1894e8b86c0SAmritha Nambiar } 190*feb2cf3dSVladimir Oltean 191*feb2cf3dSVladimir Oltean return 0; 192*feb2cf3dSVladimir Oltean } 193*feb2cf3dSVladimir Oltean 194*feb2cf3dSVladimir Oltean static int mqprio_init(struct Qdisc *sch, struct nlattr *opt, 195*feb2cf3dSVladimir Oltean struct netlink_ext_ack *extack) 196*feb2cf3dSVladimir Oltean { 197*feb2cf3dSVladimir Oltean struct net_device *dev = qdisc_dev(sch); 198*feb2cf3dSVladimir Oltean struct mqprio_sched *priv = qdisc_priv(sch); 199*feb2cf3dSVladimir Oltean struct netdev_queue *dev_queue; 200*feb2cf3dSVladimir Oltean struct Qdisc *qdisc; 201*feb2cf3dSVladimir Oltean int i, err = -EOPNOTSUPP; 202*feb2cf3dSVladimir Oltean struct tc_mqprio_qopt *qopt = NULL; 203*feb2cf3dSVladimir Oltean int len; 204*feb2cf3dSVladimir Oltean 205*feb2cf3dSVladimir Oltean BUILD_BUG_ON(TC_MAX_QUEUE != TC_QOPT_MAX_QUEUE); 206*feb2cf3dSVladimir Oltean BUILD_BUG_ON(TC_BITMASK != TC_QOPT_BITMASK); 207*feb2cf3dSVladimir Oltean 208*feb2cf3dSVladimir Oltean if (sch->parent != TC_H_ROOT) 209*feb2cf3dSVladimir Oltean return -EOPNOTSUPP; 210*feb2cf3dSVladimir Oltean 211*feb2cf3dSVladimir Oltean if (!netif_is_multiqueue(dev)) 212*feb2cf3dSVladimir Oltean return -EOPNOTSUPP; 213*feb2cf3dSVladimir Oltean 214*feb2cf3dSVladimir Oltean /* make certain can allocate enough classids to handle queues */ 215*feb2cf3dSVladimir Oltean if (dev->num_tx_queues >= TC_H_MIN_PRIORITY) 216*feb2cf3dSVladimir Oltean return -ENOMEM; 217*feb2cf3dSVladimir Oltean 218*feb2cf3dSVladimir Oltean if (!opt || nla_len(opt) < sizeof(*qopt)) 219*feb2cf3dSVladimir Oltean return -EINVAL; 220*feb2cf3dSVladimir Oltean 221*feb2cf3dSVladimir Oltean qopt = nla_data(opt); 222*feb2cf3dSVladimir Oltean if (mqprio_parse_opt(dev, qopt)) 223*feb2cf3dSVladimir Oltean return -EINVAL; 224*feb2cf3dSVladimir Oltean 225*feb2cf3dSVladimir Oltean len = nla_len(opt) - NLA_ALIGN(sizeof(*qopt)); 226*feb2cf3dSVladimir Oltean if (len > 0) { 227*feb2cf3dSVladimir Oltean err = mqprio_parse_nlattr(sch, qopt, opt); 228*feb2cf3dSVladimir Oltean if (err) 229*feb2cf3dSVladimir Oltean return err; 2304e8b86c0SAmritha Nambiar } 2314e8b86c0SAmritha Nambiar 232b8970f0bSJohn Fastabend /* pre-allocate qdisc, attachment can't fail */ 233b8970f0bSJohn Fastabend priv->qdiscs = kcalloc(dev->num_tx_queues, sizeof(priv->qdiscs[0]), 234b8970f0bSJohn Fastabend GFP_KERNEL); 23587b60cfaSEric Dumazet if (!priv->qdiscs) 23687b60cfaSEric Dumazet return -ENOMEM; 237b8970f0bSJohn Fastabend 238b8970f0bSJohn Fastabend for (i = 0; i < dev->num_tx_queues; i++) { 239b8970f0bSJohn Fastabend dev_queue = netdev_get_tx_queue(dev, i); 2401f27cde3SEric Dumazet qdisc = qdisc_create_dflt(dev_queue, 2411f27cde3SEric Dumazet get_default_qdisc_ops(dev, i), 242b8970f0bSJohn Fastabend TC_H_MAKE(TC_H_MAJ(sch->handle), 243a38a9882SAlexander Aring TC_H_MIN(i + 1)), extack); 24487b60cfaSEric Dumazet if (!qdisc) 24587b60cfaSEric Dumazet return -ENOMEM; 24687b60cfaSEric Dumazet 247b8970f0bSJohn Fastabend priv->qdiscs[i] = qdisc; 2484eaf3b84SEric Dumazet qdisc->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT; 249b8970f0bSJohn Fastabend } 250b8970f0bSJohn Fastabend 251b8970f0bSJohn Fastabend /* If the mqprio options indicate that hardware should own 252b8970f0bSJohn Fastabend * the queue mapping then run ndo_setup_tc otherwise use the 253b8970f0bSJohn Fastabend * supplied and verified mapping 254b8970f0bSJohn Fastabend */ 255b8970f0bSJohn Fastabend if (qopt->hw) { 2564e8b86c0SAmritha Nambiar struct tc_mqprio_qopt_offload mqprio = {.qopt = *qopt}; 25716e5cc64SJohn Fastabend 2584e8b86c0SAmritha Nambiar switch (priv->mode) { 2594e8b86c0SAmritha Nambiar case TC_MQPRIO_MODE_DCB: 2604e8b86c0SAmritha Nambiar if (priv->shaper != TC_MQPRIO_SHAPER_DCB) 2614e8b86c0SAmritha Nambiar return -EINVAL; 2624e8b86c0SAmritha Nambiar break; 2634e8b86c0SAmritha Nambiar case TC_MQPRIO_MODE_CHANNEL: 2644e8b86c0SAmritha Nambiar mqprio.flags = priv->flags; 2654e8b86c0SAmritha Nambiar if (priv->flags & TC_MQPRIO_F_MODE) 2664e8b86c0SAmritha Nambiar mqprio.mode = priv->mode; 2674e8b86c0SAmritha Nambiar if (priv->flags & TC_MQPRIO_F_SHAPER) 2684e8b86c0SAmritha Nambiar mqprio.shaper = priv->shaper; 2694e8b86c0SAmritha Nambiar if (priv->flags & TC_MQPRIO_F_MIN_RATE) 2704e8b86c0SAmritha Nambiar for (i = 0; i < mqprio.qopt.num_tc; i++) 2714e8b86c0SAmritha Nambiar mqprio.min_rate[i] = priv->min_rate[i]; 2724e8b86c0SAmritha Nambiar if (priv->flags & TC_MQPRIO_F_MAX_RATE) 2734e8b86c0SAmritha Nambiar for (i = 0; i < mqprio.qopt.num_tc; i++) 2744e8b86c0SAmritha Nambiar mqprio.max_rate[i] = priv->max_rate[i]; 2754e8b86c0SAmritha Nambiar break; 2764e8b86c0SAmritha Nambiar default: 2774e8b86c0SAmritha Nambiar return -EINVAL; 2784e8b86c0SAmritha Nambiar } 2794e8b86c0SAmritha Nambiar err = dev->netdev_ops->ndo_setup_tc(dev, 280575ed7d3SNogah Frankel TC_SETUP_QDISC_MQPRIO, 281de4784caSJiri Pirko &mqprio); 282b8970f0bSJohn Fastabend if (err) 28387b60cfaSEric Dumazet return err; 2842026fecfSAlexander Duyck 2854e8b86c0SAmritha Nambiar priv->hw_offload = mqprio.qopt.hw; 286b8970f0bSJohn Fastabend } else { 287b8970f0bSJohn Fastabend netdev_set_num_tc(dev, qopt->num_tc); 288b8970f0bSJohn Fastabend for (i = 0; i < qopt->num_tc; i++) 289b8970f0bSJohn Fastabend netdev_set_tc_queue(dev, i, 290b8970f0bSJohn Fastabend qopt->count[i], qopt->offset[i]); 291b8970f0bSJohn Fastabend } 292b8970f0bSJohn Fastabend 293b8970f0bSJohn Fastabend /* Always use supplied priority mappings */ 294b8970f0bSJohn Fastabend for (i = 0; i < TC_BITMASK + 1; i++) 295b8970f0bSJohn Fastabend netdev_set_prio_tc_map(dev, i, qopt->prio_tc_map[i]); 296b8970f0bSJohn Fastabend 297b8970f0bSJohn Fastabend sch->flags |= TCQ_F_MQROOT; 298b8970f0bSJohn Fastabend return 0; 299b8970f0bSJohn Fastabend } 300b8970f0bSJohn Fastabend 301b8970f0bSJohn Fastabend static void mqprio_attach(struct Qdisc *sch) 302b8970f0bSJohn Fastabend { 303b8970f0bSJohn Fastabend struct net_device *dev = qdisc_dev(sch); 304b8970f0bSJohn Fastabend struct mqprio_sched *priv = qdisc_priv(sch); 30595dc1929SEric Dumazet struct Qdisc *qdisc, *old; 306b8970f0bSJohn Fastabend unsigned int ntx; 307b8970f0bSJohn Fastabend 308b8970f0bSJohn Fastabend /* Attach underlying qdisc */ 309b8970f0bSJohn Fastabend for (ntx = 0; ntx < dev->num_tx_queues; ntx++) { 310b8970f0bSJohn Fastabend qdisc = priv->qdiscs[ntx]; 31195dc1929SEric Dumazet old = dev_graft_qdisc(qdisc->dev_queue, qdisc); 31295dc1929SEric Dumazet if (old) 31386bd446bSVlad Buslov qdisc_put(old); 31495dc1929SEric Dumazet if (ntx < dev->real_num_tx_queues) 31549b49971SJiri Kosina qdisc_hash_add(qdisc, false); 316b8970f0bSJohn Fastabend } 317b8970f0bSJohn Fastabend kfree(priv->qdiscs); 318b8970f0bSJohn Fastabend priv->qdiscs = NULL; 319b8970f0bSJohn Fastabend } 320b8970f0bSJohn Fastabend 321b8970f0bSJohn Fastabend static struct netdev_queue *mqprio_queue_get(struct Qdisc *sch, 322b8970f0bSJohn Fastabend unsigned long cl) 323b8970f0bSJohn Fastabend { 324b8970f0bSJohn Fastabend struct net_device *dev = qdisc_dev(sch); 32532302902SAlexander Duyck unsigned long ntx = cl - 1; 326b8970f0bSJohn Fastabend 327b8970f0bSJohn Fastabend if (ntx >= dev->num_tx_queues) 328b8970f0bSJohn Fastabend return NULL; 329b8970f0bSJohn Fastabend return netdev_get_tx_queue(dev, ntx); 330b8970f0bSJohn Fastabend } 331b8970f0bSJohn Fastabend 332b8970f0bSJohn Fastabend static int mqprio_graft(struct Qdisc *sch, unsigned long cl, struct Qdisc *new, 333653d6fd6SAlexander Aring struct Qdisc **old, struct netlink_ext_ack *extack) 334b8970f0bSJohn Fastabend { 335b8970f0bSJohn Fastabend struct net_device *dev = qdisc_dev(sch); 336b8970f0bSJohn Fastabend struct netdev_queue *dev_queue = mqprio_queue_get(sch, cl); 337b8970f0bSJohn Fastabend 338b8970f0bSJohn Fastabend if (!dev_queue) 339b8970f0bSJohn Fastabend return -EINVAL; 340b8970f0bSJohn Fastabend 341b8970f0bSJohn Fastabend if (dev->flags & IFF_UP) 342b8970f0bSJohn Fastabend dev_deactivate(dev); 343b8970f0bSJohn Fastabend 344b8970f0bSJohn Fastabend *old = dev_graft_qdisc(dev_queue, new); 345b8970f0bSJohn Fastabend 3461abbe139SEric Dumazet if (new) 3474eaf3b84SEric Dumazet new->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT; 3481abbe139SEric Dumazet 349b8970f0bSJohn Fastabend if (dev->flags & IFF_UP) 350b8970f0bSJohn Fastabend dev_activate(dev); 351b8970f0bSJohn Fastabend 352b8970f0bSJohn Fastabend return 0; 353b8970f0bSJohn Fastabend } 354b8970f0bSJohn Fastabend 3554e8b86c0SAmritha Nambiar static int dump_rates(struct mqprio_sched *priv, 3564e8b86c0SAmritha Nambiar struct tc_mqprio_qopt *opt, struct sk_buff *skb) 3574e8b86c0SAmritha Nambiar { 3584e8b86c0SAmritha Nambiar struct nlattr *nest; 3594e8b86c0SAmritha Nambiar int i; 3604e8b86c0SAmritha Nambiar 3614e8b86c0SAmritha Nambiar if (priv->flags & TC_MQPRIO_F_MIN_RATE) { 362ae0be8deSMichal Kubecek nest = nla_nest_start_noflag(skb, TCA_MQPRIO_MIN_RATE64); 3634e8b86c0SAmritha Nambiar if (!nest) 3644e8b86c0SAmritha Nambiar goto nla_put_failure; 3654e8b86c0SAmritha Nambiar 3664e8b86c0SAmritha Nambiar for (i = 0; i < opt->num_tc; i++) { 3674e8b86c0SAmritha Nambiar if (nla_put(skb, TCA_MQPRIO_MIN_RATE64, 3684e8b86c0SAmritha Nambiar sizeof(priv->min_rate[i]), 3694e8b86c0SAmritha Nambiar &priv->min_rate[i])) 3704e8b86c0SAmritha Nambiar goto nla_put_failure; 3714e8b86c0SAmritha Nambiar } 3724e8b86c0SAmritha Nambiar nla_nest_end(skb, nest); 3734e8b86c0SAmritha Nambiar } 3744e8b86c0SAmritha Nambiar 3754e8b86c0SAmritha Nambiar if (priv->flags & TC_MQPRIO_F_MAX_RATE) { 376ae0be8deSMichal Kubecek nest = nla_nest_start_noflag(skb, TCA_MQPRIO_MAX_RATE64); 3774e8b86c0SAmritha Nambiar if (!nest) 3784e8b86c0SAmritha Nambiar goto nla_put_failure; 3794e8b86c0SAmritha Nambiar 3804e8b86c0SAmritha Nambiar for (i = 0; i < opt->num_tc; i++) { 3814e8b86c0SAmritha Nambiar if (nla_put(skb, TCA_MQPRIO_MAX_RATE64, 3824e8b86c0SAmritha Nambiar sizeof(priv->max_rate[i]), 3834e8b86c0SAmritha Nambiar &priv->max_rate[i])) 3844e8b86c0SAmritha Nambiar goto nla_put_failure; 3854e8b86c0SAmritha Nambiar } 3864e8b86c0SAmritha Nambiar nla_nest_end(skb, nest); 3874e8b86c0SAmritha Nambiar } 3884e8b86c0SAmritha Nambiar return 0; 3894e8b86c0SAmritha Nambiar 3904e8b86c0SAmritha Nambiar nla_put_failure: 3914e8b86c0SAmritha Nambiar nla_nest_cancel(skb, nest); 3924e8b86c0SAmritha Nambiar return -1; 3934e8b86c0SAmritha Nambiar } 3944e8b86c0SAmritha Nambiar 395b8970f0bSJohn Fastabend static int mqprio_dump(struct Qdisc *sch, struct sk_buff *skb) 396b8970f0bSJohn Fastabend { 397b8970f0bSJohn Fastabend struct net_device *dev = qdisc_dev(sch); 398b8970f0bSJohn Fastabend struct mqprio_sched *priv = qdisc_priv(sch); 3994e8b86c0SAmritha Nambiar struct nlattr *nla = (struct nlattr *)skb_tail_pointer(skb); 400144ce879SEric Dumazet struct tc_mqprio_qopt opt = { 0 }; 401b8970f0bSJohn Fastabend struct Qdisc *qdisc; 402ce679e8dSJohn Fastabend unsigned int ntx, tc; 403b8970f0bSJohn Fastabend 404b8970f0bSJohn Fastabend sch->q.qlen = 0; 40550dc9a85SAhmed S. Darwish gnet_stats_basic_sync_init(&sch->bstats); 406b8970f0bSJohn Fastabend memset(&sch->qstats, 0, sizeof(sch->qstats)); 407b8970f0bSJohn Fastabend 408ce679e8dSJohn Fastabend /* MQ supports lockless qdiscs. However, statistics accounting needs 409ce679e8dSJohn Fastabend * to account for all, none, or a mix of locked and unlocked child 410ce679e8dSJohn Fastabend * qdiscs. Percpu stats are added to counters in-band and locking 411ce679e8dSJohn Fastabend * qdisc totals are added at end. 412ce679e8dSJohn Fastabend */ 413ce679e8dSJohn Fastabend for (ntx = 0; ntx < dev->num_tx_queues; ntx++) { 414ce679e8dSJohn Fastabend qdisc = netdev_get_tx_queue(dev, ntx)->qdisc_sleeping; 415b8970f0bSJohn Fastabend spin_lock_bh(qdisc_lock(qdisc)); 416ce679e8dSJohn Fastabend 41729cbcd85SAhmed S. Darwish gnet_stats_add_basic(&sch->bstats, qdisc->cpu_bstats, 41829cbcd85SAhmed S. Darwish &qdisc->bstats, false); 4197361df46SSebastian Andrzej Siewior gnet_stats_add_queue(&sch->qstats, qdisc->cpu_qstats, 4207361df46SSebastian Andrzej Siewior &qdisc->qstats); 4217361df46SSebastian Andrzej Siewior sch->q.qlen += qdisc_qlen(qdisc); 422ce679e8dSJohn Fastabend 423b8970f0bSJohn Fastabend spin_unlock_bh(qdisc_lock(qdisc)); 424b8970f0bSJohn Fastabend } 425b8970f0bSJohn Fastabend 426b8970f0bSJohn Fastabend opt.num_tc = netdev_get_num_tc(dev); 427b8970f0bSJohn Fastabend memcpy(opt.prio_tc_map, dev->prio_tc_map, sizeof(opt.prio_tc_map)); 4282026fecfSAlexander Duyck opt.hw = priv->hw_offload; 429b8970f0bSJohn Fastabend 430ce679e8dSJohn Fastabend for (tc = 0; tc < netdev_get_num_tc(dev); tc++) { 431ce679e8dSJohn Fastabend opt.count[tc] = dev->tc_to_txq[tc].count; 432ce679e8dSJohn Fastabend opt.offset[tc] = dev->tc_to_txq[tc].offset; 433b8970f0bSJohn Fastabend } 434b8970f0bSJohn Fastabend 4359f104c77SVladyslav Tarasiuk if (nla_put(skb, TCA_OPTIONS, sizeof(opt), &opt)) 4361b34ec43SDavid S. Miller goto nla_put_failure; 437b8970f0bSJohn Fastabend 4384e8b86c0SAmritha Nambiar if ((priv->flags & TC_MQPRIO_F_MODE) && 4394e8b86c0SAmritha Nambiar nla_put_u16(skb, TCA_MQPRIO_MODE, priv->mode)) 4404e8b86c0SAmritha Nambiar goto nla_put_failure; 4414e8b86c0SAmritha Nambiar 4424e8b86c0SAmritha Nambiar if ((priv->flags & TC_MQPRIO_F_SHAPER) && 4434e8b86c0SAmritha Nambiar nla_put_u16(skb, TCA_MQPRIO_SHAPER, priv->shaper)) 4444e8b86c0SAmritha Nambiar goto nla_put_failure; 4454e8b86c0SAmritha Nambiar 4464e8b86c0SAmritha Nambiar if ((priv->flags & TC_MQPRIO_F_MIN_RATE || 4474e8b86c0SAmritha Nambiar priv->flags & TC_MQPRIO_F_MAX_RATE) && 4484e8b86c0SAmritha Nambiar (dump_rates(priv, &opt, skb) != 0)) 4494e8b86c0SAmritha Nambiar goto nla_put_failure; 4504e8b86c0SAmritha Nambiar 4514e8b86c0SAmritha Nambiar return nla_nest_end(skb, nla); 452b8970f0bSJohn Fastabend nla_put_failure: 4534e8b86c0SAmritha Nambiar nlmsg_trim(skb, nla); 454b8970f0bSJohn Fastabend return -1; 455b8970f0bSJohn Fastabend } 456b8970f0bSJohn Fastabend 457b8970f0bSJohn Fastabend static struct Qdisc *mqprio_leaf(struct Qdisc *sch, unsigned long cl) 458b8970f0bSJohn Fastabend { 459b8970f0bSJohn Fastabend struct netdev_queue *dev_queue = mqprio_queue_get(sch, cl); 460b8970f0bSJohn Fastabend 461b8970f0bSJohn Fastabend if (!dev_queue) 462b8970f0bSJohn Fastabend return NULL; 463b8970f0bSJohn Fastabend 464b8970f0bSJohn Fastabend return dev_queue->qdisc_sleeping; 465b8970f0bSJohn Fastabend } 466b8970f0bSJohn Fastabend 467143976ceSWANG Cong static unsigned long mqprio_find(struct Qdisc *sch, u32 classid) 468b8970f0bSJohn Fastabend { 469b8970f0bSJohn Fastabend struct net_device *dev = qdisc_dev(sch); 470b8970f0bSJohn Fastabend unsigned int ntx = TC_H_MIN(classid); 471b8970f0bSJohn Fastabend 47232302902SAlexander Duyck /* There are essentially two regions here that have valid classid 47332302902SAlexander Duyck * values. The first region will have a classid value of 1 through 47432302902SAlexander Duyck * num_tx_queues. All of these are backed by actual Qdiscs. 47532302902SAlexander Duyck */ 47632302902SAlexander Duyck if (ntx < TC_H_MIN_PRIORITY) 47732302902SAlexander Duyck return (ntx <= dev->num_tx_queues) ? ntx : 0; 47832302902SAlexander Duyck 47932302902SAlexander Duyck /* The second region represents the hardware traffic classes. These 48032302902SAlexander Duyck * are represented by classid values of TC_H_MIN_PRIORITY through 48132302902SAlexander Duyck * TC_H_MIN_PRIORITY + netdev_get_num_tc - 1 48232302902SAlexander Duyck */ 48332302902SAlexander Duyck return ((ntx - TC_H_MIN_PRIORITY) < netdev_get_num_tc(dev)) ? ntx : 0; 484b8970f0bSJohn Fastabend } 485b8970f0bSJohn Fastabend 486b8970f0bSJohn Fastabend static int mqprio_dump_class(struct Qdisc *sch, unsigned long cl, 487b8970f0bSJohn Fastabend struct sk_buff *skb, struct tcmsg *tcm) 488b8970f0bSJohn Fastabend { 48932302902SAlexander Duyck if (cl < TC_H_MIN_PRIORITY) { 49032302902SAlexander Duyck struct netdev_queue *dev_queue = mqprio_queue_get(sch, cl); 491b8970f0bSJohn Fastabend struct net_device *dev = qdisc_dev(sch); 49232302902SAlexander Duyck int tc = netdev_txq_to_tc(dev, cl - 1); 493b8970f0bSJohn Fastabend 49432302902SAlexander Duyck tcm->tcm_parent = (tc < 0) ? 0 : 49532302902SAlexander Duyck TC_H_MAKE(TC_H_MAJ(sch->handle), 49632302902SAlexander Duyck TC_H_MIN(tc + TC_H_MIN_PRIORITY)); 49732302902SAlexander Duyck tcm->tcm_info = dev_queue->qdisc_sleeping->handle; 49832302902SAlexander Duyck } else { 499b8970f0bSJohn Fastabend tcm->tcm_parent = TC_H_ROOT; 500b8970f0bSJohn Fastabend tcm->tcm_info = 0; 501b8970f0bSJohn Fastabend } 502b8970f0bSJohn Fastabend tcm->tcm_handle |= TC_H_MIN(cl); 503b8970f0bSJohn Fastabend return 0; 504b8970f0bSJohn Fastabend } 505b8970f0bSJohn Fastabend 506b8970f0bSJohn Fastabend static int mqprio_dump_class_stats(struct Qdisc *sch, unsigned long cl, 507b8970f0bSJohn Fastabend struct gnet_dump *d) 508ea18fd95Sstephen hemminger __releases(d->lock) 509ea18fd95Sstephen hemminger __acquires(d->lock) 510b8970f0bSJohn Fastabend { 51132302902SAlexander Duyck if (cl >= TC_H_MIN_PRIORITY) { 512b8970f0bSJohn Fastabend int i; 5137361df46SSebastian Andrzej Siewior __u32 qlen; 514b8970f0bSJohn Fastabend struct gnet_stats_queue qstats = {0}; 51550dc9a85SAhmed S. Darwish struct gnet_stats_basic_sync bstats; 51632302902SAlexander Duyck struct net_device *dev = qdisc_dev(sch); 51732302902SAlexander Duyck struct netdev_tc_txq tc = dev->tc_to_txq[cl & TC_BITMASK]; 518b8970f0bSJohn Fastabend 51950dc9a85SAhmed S. Darwish gnet_stats_basic_sync_init(&bstats); 520b8970f0bSJohn Fastabend /* Drop lock here it will be reclaimed before touching 521b8970f0bSJohn Fastabend * statistics this is required because the d->lock we 522b8970f0bSJohn Fastabend * hold here is the look on dev_queue->qdisc_sleeping 523b8970f0bSJohn Fastabend * also acquired below. 524b8970f0bSJohn Fastabend */ 525edb09eb1SEric Dumazet if (d->lock) 526b8970f0bSJohn Fastabend spin_unlock_bh(d->lock); 527b8970f0bSJohn Fastabend 528b8970f0bSJohn Fastabend for (i = tc.offset; i < tc.offset + tc.count; i++) { 52946e5da40SJohn Fastabend struct netdev_queue *q = netdev_get_tx_queue(dev, i); 530ce679e8dSJohn Fastabend struct Qdisc *qdisc = rtnl_dereference(q->qdisc); 53146e5da40SJohn Fastabend 532b8970f0bSJohn Fastabend spin_lock_bh(qdisc_lock(qdisc)); 533ce679e8dSJohn Fastabend 53429cbcd85SAhmed S. Darwish gnet_stats_add_basic(&bstats, qdisc->cpu_bstats, 53529cbcd85SAhmed S. Darwish &qdisc->bstats, false); 5367361df46SSebastian Andrzej Siewior gnet_stats_add_queue(&qstats, qdisc->cpu_qstats, 5377361df46SSebastian Andrzej Siewior &qdisc->qstats); 5387361df46SSebastian Andrzej Siewior sch->q.qlen += qdisc_qlen(qdisc); 5397361df46SSebastian Andrzej Siewior 540b8970f0bSJohn Fastabend spin_unlock_bh(qdisc_lock(qdisc)); 541b8970f0bSJohn Fastabend } 5427361df46SSebastian Andrzej Siewior qlen = qdisc_qlen(sch) + qstats.qlen; 543ce679e8dSJohn Fastabend 544b8970f0bSJohn Fastabend /* Reclaim root sleeping lock before completing stats */ 545edb09eb1SEric Dumazet if (d->lock) 546b8970f0bSJohn Fastabend spin_lock_bh(d->lock); 54729cbcd85SAhmed S. Darwish if (gnet_stats_copy_basic(d, NULL, &bstats, false) < 0 || 548b0ab6f92SJohn Fastabend gnet_stats_copy_queue(d, NULL, &qstats, qlen) < 0) 549b8970f0bSJohn Fastabend return -1; 550b8970f0bSJohn Fastabend } else { 551b8970f0bSJohn Fastabend struct netdev_queue *dev_queue = mqprio_queue_get(sch, cl); 552b8970f0bSJohn Fastabend 553b8970f0bSJohn Fastabend sch = dev_queue->qdisc_sleeping; 55429cbcd85SAhmed S. Darwish if (gnet_stats_copy_basic(d, sch->cpu_bstats, 55529cbcd85SAhmed S. Darwish &sch->bstats, true) < 0 || 5565dd431b6SPaolo Abeni qdisc_qstats_copy(d, sch) < 0) 557b8970f0bSJohn Fastabend return -1; 558b8970f0bSJohn Fastabend } 559b8970f0bSJohn Fastabend return 0; 560b8970f0bSJohn Fastabend } 561b8970f0bSJohn Fastabend 562b8970f0bSJohn Fastabend static void mqprio_walk(struct Qdisc *sch, struct qdisc_walker *arg) 563b8970f0bSJohn Fastabend { 564b8970f0bSJohn Fastabend struct net_device *dev = qdisc_dev(sch); 565b8970f0bSJohn Fastabend unsigned long ntx; 566b8970f0bSJohn Fastabend 567b8970f0bSJohn Fastabend if (arg->stop) 568b8970f0bSJohn Fastabend return; 569b8970f0bSJohn Fastabend 570b8970f0bSJohn Fastabend /* Walk hierarchy with a virtual class per tc */ 571b8970f0bSJohn Fastabend arg->count = arg->skip; 57232302902SAlexander Duyck for (ntx = arg->skip; ntx < netdev_get_num_tc(dev); ntx++) { 573e046fa89SZhengchao Shao if (!tc_qdisc_stats_dump(sch, ntx + TC_H_MIN_PRIORITY, arg)) 57432302902SAlexander Duyck return; 57532302902SAlexander Duyck } 57632302902SAlexander Duyck 57732302902SAlexander Duyck /* Pad the values and skip over unused traffic classes */ 57832302902SAlexander Duyck if (ntx < TC_MAX_QUEUE) { 57932302902SAlexander Duyck arg->count = TC_MAX_QUEUE; 58032302902SAlexander Duyck ntx = TC_MAX_QUEUE; 58132302902SAlexander Duyck } 58232302902SAlexander Duyck 58332302902SAlexander Duyck /* Reset offset, sort out remaining per-queue qdiscs */ 58432302902SAlexander Duyck for (ntx -= TC_MAX_QUEUE; ntx < dev->num_tx_queues; ntx++) { 585b8970f0bSJohn Fastabend if (arg->fn(sch, ntx + 1, arg) < 0) { 586b8970f0bSJohn Fastabend arg->stop = 1; 58732302902SAlexander Duyck return; 588b8970f0bSJohn Fastabend } 589b8970f0bSJohn Fastabend arg->count++; 590b8970f0bSJohn Fastabend } 591b8970f0bSJohn Fastabend } 592b8970f0bSJohn Fastabend 5930f7787b4SJesus Sanchez-Palencia static struct netdev_queue *mqprio_select_queue(struct Qdisc *sch, 5940f7787b4SJesus Sanchez-Palencia struct tcmsg *tcm) 5950f7787b4SJesus Sanchez-Palencia { 5960f7787b4SJesus Sanchez-Palencia return mqprio_queue_get(sch, TC_H_MIN(tcm->tcm_parent)); 5970f7787b4SJesus Sanchez-Palencia } 5980f7787b4SJesus Sanchez-Palencia 599b8970f0bSJohn Fastabend static const struct Qdisc_class_ops mqprio_class_ops = { 600b8970f0bSJohn Fastabend .graft = mqprio_graft, 601b8970f0bSJohn Fastabend .leaf = mqprio_leaf, 602143976ceSWANG Cong .find = mqprio_find, 603b8970f0bSJohn Fastabend .walk = mqprio_walk, 604b8970f0bSJohn Fastabend .dump = mqprio_dump_class, 605b8970f0bSJohn Fastabend .dump_stats = mqprio_dump_class_stats, 6060f7787b4SJesus Sanchez-Palencia .select_queue = mqprio_select_queue, 607b8970f0bSJohn Fastabend }; 608b8970f0bSJohn Fastabend 609ea18fd95Sstephen hemminger static struct Qdisc_ops mqprio_qdisc_ops __read_mostly = { 610b8970f0bSJohn Fastabend .cl_ops = &mqprio_class_ops, 611b8970f0bSJohn Fastabend .id = "mqprio", 612b8970f0bSJohn Fastabend .priv_size = sizeof(struct mqprio_sched), 613b8970f0bSJohn Fastabend .init = mqprio_init, 614b8970f0bSJohn Fastabend .destroy = mqprio_destroy, 615b8970f0bSJohn Fastabend .attach = mqprio_attach, 616f7116fb4SJakub Kicinski .change_real_num_tx = mq_change_real_num_tx, 617b8970f0bSJohn Fastabend .dump = mqprio_dump, 618b8970f0bSJohn Fastabend .owner = THIS_MODULE, 619b8970f0bSJohn Fastabend }; 620b8970f0bSJohn Fastabend 621b8970f0bSJohn Fastabend static int __init mqprio_module_init(void) 622b8970f0bSJohn Fastabend { 623b8970f0bSJohn Fastabend return register_qdisc(&mqprio_qdisc_ops); 624b8970f0bSJohn Fastabend } 625b8970f0bSJohn Fastabend 626b8970f0bSJohn Fastabend static void __exit mqprio_module_exit(void) 627b8970f0bSJohn Fastabend { 628b8970f0bSJohn Fastabend unregister_qdisc(&mqprio_qdisc_ops); 629b8970f0bSJohn Fastabend } 630b8970f0bSJohn Fastabend 631b8970f0bSJohn Fastabend module_init(mqprio_module_init); 632b8970f0bSJohn Fastabend module_exit(mqprio_module_exit); 633b8970f0bSJohn Fastabend 634b8970f0bSJohn Fastabend MODULE_LICENSE("GPL"); 635