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 8*f62af20bSVladimir Oltean #include <linux/ethtool_netlink.h> 9b8970f0bSJohn Fastabend #include <linux/types.h> 10b8970f0bSJohn Fastabend #include <linux/slab.h> 11b8970f0bSJohn Fastabend #include <linux/kernel.h> 12b8970f0bSJohn Fastabend #include <linux/string.h> 13b8970f0bSJohn Fastabend #include <linux/errno.h> 14b8970f0bSJohn Fastabend #include <linux/skbuff.h> 153a9a231dSPaul Gortmaker #include <linux/module.h> 16b8970f0bSJohn Fastabend #include <net/netlink.h> 17b8970f0bSJohn Fastabend #include <net/pkt_sched.h> 18b8970f0bSJohn Fastabend #include <net/sch_generic.h> 194e8b86c0SAmritha Nambiar #include <net/pkt_cls.h> 20b8970f0bSJohn Fastabend 211dfe086dSVladimir Oltean #include "sch_mqprio_lib.h" 221dfe086dSVladimir Oltean 23b8970f0bSJohn Fastabend struct mqprio_sched { 24b8970f0bSJohn Fastabend struct Qdisc **qdiscs; 254e8b86c0SAmritha Nambiar u16 mode; 264e8b86c0SAmritha Nambiar u16 shaper; 272026fecfSAlexander Duyck int hw_offload; 284e8b86c0SAmritha Nambiar u32 flags; 294e8b86c0SAmritha Nambiar u64 min_rate[TC_QOPT_MAX_QUEUE]; 304e8b86c0SAmritha Nambiar u64 max_rate[TC_QOPT_MAX_QUEUE]; 31*f62af20bSVladimir Oltean u32 fp[TC_QOPT_MAX_QUEUE]; 32b8970f0bSJohn Fastabend }; 33b8970f0bSJohn Fastabend 345cfb45e2SVladimir Oltean static int mqprio_enable_offload(struct Qdisc *sch, 35d404959fSVladimir Oltean const struct tc_mqprio_qopt *qopt, 36d404959fSVladimir Oltean struct netlink_ext_ack *extack) 375cfb45e2SVladimir Oltean { 385cfb45e2SVladimir Oltean struct mqprio_sched *priv = qdisc_priv(sch); 395cfb45e2SVladimir Oltean struct net_device *dev = qdisc_dev(sch); 40c54876cdSVladimir Oltean struct tc_mqprio_qopt_offload mqprio = { 41c54876cdSVladimir Oltean .qopt = *qopt, 42c54876cdSVladimir Oltean .extack = extack, 43c54876cdSVladimir Oltean }; 445cfb45e2SVladimir Oltean int err, i; 455cfb45e2SVladimir Oltean 465cfb45e2SVladimir Oltean switch (priv->mode) { 475cfb45e2SVladimir Oltean case TC_MQPRIO_MODE_DCB: 485cfb45e2SVladimir Oltean if (priv->shaper != TC_MQPRIO_SHAPER_DCB) 495cfb45e2SVladimir Oltean return -EINVAL; 505cfb45e2SVladimir Oltean break; 515cfb45e2SVladimir Oltean case TC_MQPRIO_MODE_CHANNEL: 525cfb45e2SVladimir Oltean mqprio.flags = priv->flags; 535cfb45e2SVladimir Oltean if (priv->flags & TC_MQPRIO_F_MODE) 545cfb45e2SVladimir Oltean mqprio.mode = priv->mode; 555cfb45e2SVladimir Oltean if (priv->flags & TC_MQPRIO_F_SHAPER) 565cfb45e2SVladimir Oltean mqprio.shaper = priv->shaper; 575cfb45e2SVladimir Oltean if (priv->flags & TC_MQPRIO_F_MIN_RATE) 585cfb45e2SVladimir Oltean for (i = 0; i < mqprio.qopt.num_tc; i++) 595cfb45e2SVladimir Oltean mqprio.min_rate[i] = priv->min_rate[i]; 605cfb45e2SVladimir Oltean if (priv->flags & TC_MQPRIO_F_MAX_RATE) 615cfb45e2SVladimir Oltean for (i = 0; i < mqprio.qopt.num_tc; i++) 625cfb45e2SVladimir Oltean mqprio.max_rate[i] = priv->max_rate[i]; 635cfb45e2SVladimir Oltean break; 645cfb45e2SVladimir Oltean default: 655cfb45e2SVladimir Oltean return -EINVAL; 665cfb45e2SVladimir Oltean } 675cfb45e2SVladimir Oltean 68*f62af20bSVladimir Oltean mqprio_fp_to_offload(priv->fp, &mqprio); 69*f62af20bSVladimir Oltean 705cfb45e2SVladimir Oltean err = dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_QDISC_MQPRIO, 715cfb45e2SVladimir Oltean &mqprio); 725cfb45e2SVladimir Oltean if (err) 735cfb45e2SVladimir Oltean return err; 745cfb45e2SVladimir Oltean 755cfb45e2SVladimir Oltean priv->hw_offload = mqprio.qopt.hw; 765cfb45e2SVladimir Oltean 775cfb45e2SVladimir Oltean return 0; 785cfb45e2SVladimir Oltean } 795cfb45e2SVladimir Oltean 805cfb45e2SVladimir Oltean static void mqprio_disable_offload(struct Qdisc *sch) 815cfb45e2SVladimir Oltean { 825cfb45e2SVladimir Oltean struct tc_mqprio_qopt_offload mqprio = { { 0 } }; 835cfb45e2SVladimir Oltean struct mqprio_sched *priv = qdisc_priv(sch); 845cfb45e2SVladimir Oltean struct net_device *dev = qdisc_dev(sch); 855cfb45e2SVladimir Oltean 865cfb45e2SVladimir Oltean switch (priv->mode) { 875cfb45e2SVladimir Oltean case TC_MQPRIO_MODE_DCB: 885cfb45e2SVladimir Oltean case TC_MQPRIO_MODE_CHANNEL: 895cfb45e2SVladimir Oltean dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_QDISC_MQPRIO, 905cfb45e2SVladimir Oltean &mqprio); 915cfb45e2SVladimir Oltean break; 925cfb45e2SVladimir Oltean } 935cfb45e2SVladimir Oltean } 945cfb45e2SVladimir Oltean 95b8970f0bSJohn Fastabend static void mqprio_destroy(struct Qdisc *sch) 96b8970f0bSJohn Fastabend { 97b8970f0bSJohn Fastabend struct net_device *dev = qdisc_dev(sch); 98b8970f0bSJohn Fastabend struct mqprio_sched *priv = qdisc_priv(sch); 99b8970f0bSJohn Fastabend unsigned int ntx; 100b8970f0bSJohn Fastabend 101ac7100baSBen Hutchings if (priv->qdiscs) { 102ac7100baSBen Hutchings for (ntx = 0; 103ac7100baSBen Hutchings ntx < dev->num_tx_queues && priv->qdiscs[ntx]; 104ac7100baSBen Hutchings ntx++) 10586bd446bSVlad Buslov qdisc_put(priv->qdiscs[ntx]); 106ac7100baSBen Hutchings kfree(priv->qdiscs); 107ac7100baSBen Hutchings } 108b8970f0bSJohn Fastabend 1095cfb45e2SVladimir Oltean if (priv->hw_offload && dev->netdev_ops->ndo_setup_tc) 1105cfb45e2SVladimir Oltean mqprio_disable_offload(sch); 1115cfb45e2SVladimir Oltean else 112b8970f0bSJohn Fastabend netdev_set_num_tc(dev, 0); 113b8970f0bSJohn Fastabend } 114b8970f0bSJohn Fastabend 11519278d76SVladimir Oltean static int mqprio_parse_opt(struct net_device *dev, struct tc_mqprio_qopt *qopt, 116d404959fSVladimir Oltean const struct tc_mqprio_caps *caps, 117d404959fSVladimir Oltean struct netlink_ext_ack *extack) 118b8970f0bSJohn Fastabend { 1191dfe086dSVladimir Oltean int err; 120b8970f0bSJohn Fastabend 1212026fecfSAlexander Duyck /* Limit qopt->hw to maximum supported offload value. Drivers have 1222026fecfSAlexander Duyck * the option of overriding this later if they don't support the a 1232026fecfSAlexander Duyck * given offload type. 1242026fecfSAlexander Duyck */ 1252026fecfSAlexander Duyck if (qopt->hw > TC_MQPRIO_HW_OFFLOAD_MAX) 1262026fecfSAlexander Duyck qopt->hw = TC_MQPRIO_HW_OFFLOAD_MAX; 127b8970f0bSJohn Fastabend 12819278d76SVladimir Oltean /* If hardware offload is requested, we will leave 3 options to the 12919278d76SVladimir Oltean * device driver: 13019278d76SVladimir Oltean * - populate the queue counts itself (and ignore what was requested) 13119278d76SVladimir Oltean * - validate the provided queue counts by itself (and apply them) 13219278d76SVladimir Oltean * - request queue count validation here (and apply them) 133b8970f0bSJohn Fastabend */ 1341dfe086dSVladimir Oltean err = mqprio_validate_qopt(dev, qopt, 1351dfe086dSVladimir Oltean !qopt->hw || caps->validate_queue_counts, 1361dfe086dSVladimir Oltean false, extack); 13719278d76SVladimir Oltean if (err) 13819278d76SVladimir Oltean return err; 139b8970f0bSJohn Fastabend 14019278d76SVladimir Oltean /* If ndo_setup_tc is not present then hardware doesn't support offload 14119278d76SVladimir Oltean * and we should return an error. 142b8970f0bSJohn Fastabend */ 143ab277d20SVladimir Oltean if (qopt->hw && !dev->netdev_ops->ndo_setup_tc) { 144ab277d20SVladimir Oltean NL_SET_ERR_MSG(extack, 145ab277d20SVladimir Oltean "Device does not support hardware offload"); 146b8970f0bSJohn Fastabend return -EINVAL; 147ab277d20SVladimir Oltean } 148b8970f0bSJohn Fastabend 149b8970f0bSJohn Fastabend return 0; 150b8970f0bSJohn Fastabend } 151b8970f0bSJohn Fastabend 152*f62af20bSVladimir Oltean static const struct 153*f62af20bSVladimir Oltean nla_policy mqprio_tc_entry_policy[TCA_MQPRIO_TC_ENTRY_MAX + 1] = { 154*f62af20bSVladimir Oltean [TCA_MQPRIO_TC_ENTRY_INDEX] = NLA_POLICY_MAX(NLA_U32, 155*f62af20bSVladimir Oltean TC_QOPT_MAX_QUEUE), 156*f62af20bSVladimir Oltean [TCA_MQPRIO_TC_ENTRY_FP] = NLA_POLICY_RANGE(NLA_U32, 157*f62af20bSVladimir Oltean TC_FP_EXPRESS, 158*f62af20bSVladimir Oltean TC_FP_PREEMPTIBLE), 159*f62af20bSVladimir Oltean }; 160*f62af20bSVladimir Oltean 1614e8b86c0SAmritha Nambiar static const struct nla_policy mqprio_policy[TCA_MQPRIO_MAX + 1] = { 1624e8b86c0SAmritha Nambiar [TCA_MQPRIO_MODE] = { .len = sizeof(u16) }, 1634e8b86c0SAmritha Nambiar [TCA_MQPRIO_SHAPER] = { .len = sizeof(u16) }, 1644e8b86c0SAmritha Nambiar [TCA_MQPRIO_MIN_RATE64] = { .type = NLA_NESTED }, 1654e8b86c0SAmritha Nambiar [TCA_MQPRIO_MAX_RATE64] = { .type = NLA_NESTED }, 166*f62af20bSVladimir Oltean [TCA_MQPRIO_TC_ENTRY] = { .type = NLA_NESTED }, 1674e8b86c0SAmritha Nambiar }; 1684e8b86c0SAmritha Nambiar 169*f62af20bSVladimir Oltean static int mqprio_parse_tc_entry(u32 fp[TC_QOPT_MAX_QUEUE], 170*f62af20bSVladimir Oltean struct nlattr *opt, 171*f62af20bSVladimir Oltean unsigned long *seen_tcs, 172*f62af20bSVladimir Oltean struct netlink_ext_ack *extack) 173*f62af20bSVladimir Oltean { 174*f62af20bSVladimir Oltean struct nlattr *tb[TCA_MQPRIO_TC_ENTRY_MAX + 1]; 175*f62af20bSVladimir Oltean int err, tc; 176*f62af20bSVladimir Oltean 177*f62af20bSVladimir Oltean err = nla_parse_nested(tb, TCA_MQPRIO_TC_ENTRY_MAX, opt, 178*f62af20bSVladimir Oltean mqprio_tc_entry_policy, extack); 179*f62af20bSVladimir Oltean if (err < 0) 180*f62af20bSVladimir Oltean return err; 181*f62af20bSVladimir Oltean 182*f62af20bSVladimir Oltean if (NL_REQ_ATTR_CHECK(extack, opt, tb, TCA_MQPRIO_TC_ENTRY_INDEX)) { 183*f62af20bSVladimir Oltean NL_SET_ERR_MSG(extack, "TC entry index missing"); 184*f62af20bSVladimir Oltean return -EINVAL; 185*f62af20bSVladimir Oltean } 186*f62af20bSVladimir Oltean 187*f62af20bSVladimir Oltean tc = nla_get_u32(tb[TCA_MQPRIO_TC_ENTRY_INDEX]); 188*f62af20bSVladimir Oltean if (*seen_tcs & BIT(tc)) { 189*f62af20bSVladimir Oltean NL_SET_ERR_MSG_ATTR(extack, tb[TCA_MQPRIO_TC_ENTRY_INDEX], 190*f62af20bSVladimir Oltean "Duplicate tc entry"); 191*f62af20bSVladimir Oltean return -EINVAL; 192*f62af20bSVladimir Oltean } 193*f62af20bSVladimir Oltean 194*f62af20bSVladimir Oltean *seen_tcs |= BIT(tc); 195*f62af20bSVladimir Oltean 196*f62af20bSVladimir Oltean if (tb[TCA_MQPRIO_TC_ENTRY_FP]) 197*f62af20bSVladimir Oltean fp[tc] = nla_get_u32(tb[TCA_MQPRIO_TC_ENTRY_FP]); 198*f62af20bSVladimir Oltean 199*f62af20bSVladimir Oltean return 0; 200*f62af20bSVladimir Oltean } 201*f62af20bSVladimir Oltean 202*f62af20bSVladimir Oltean static int mqprio_parse_tc_entries(struct Qdisc *sch, struct nlattr *nlattr_opt, 203*f62af20bSVladimir Oltean int nlattr_opt_len, 204*f62af20bSVladimir Oltean struct netlink_ext_ack *extack) 205*f62af20bSVladimir Oltean { 206*f62af20bSVladimir Oltean struct mqprio_sched *priv = qdisc_priv(sch); 207*f62af20bSVladimir Oltean struct net_device *dev = qdisc_dev(sch); 208*f62af20bSVladimir Oltean bool have_preemption = false; 209*f62af20bSVladimir Oltean unsigned long seen_tcs = 0; 210*f62af20bSVladimir Oltean u32 fp[TC_QOPT_MAX_QUEUE]; 211*f62af20bSVladimir Oltean struct nlattr *n; 212*f62af20bSVladimir Oltean int tc, rem; 213*f62af20bSVladimir Oltean int err = 0; 214*f62af20bSVladimir Oltean 215*f62af20bSVladimir Oltean for (tc = 0; tc < TC_QOPT_MAX_QUEUE; tc++) 216*f62af20bSVladimir Oltean fp[tc] = priv->fp[tc]; 217*f62af20bSVladimir Oltean 218*f62af20bSVladimir Oltean nla_for_each_attr(n, nlattr_opt, nlattr_opt_len, rem) { 219*f62af20bSVladimir Oltean if (nla_type(n) != TCA_MQPRIO_TC_ENTRY) 220*f62af20bSVladimir Oltean continue; 221*f62af20bSVladimir Oltean 222*f62af20bSVladimir Oltean err = mqprio_parse_tc_entry(fp, n, &seen_tcs, extack); 223*f62af20bSVladimir Oltean if (err) 224*f62af20bSVladimir Oltean goto out; 225*f62af20bSVladimir Oltean } 226*f62af20bSVladimir Oltean 227*f62af20bSVladimir Oltean for (tc = 0; tc < TC_QOPT_MAX_QUEUE; tc++) { 228*f62af20bSVladimir Oltean priv->fp[tc] = fp[tc]; 229*f62af20bSVladimir Oltean if (fp[tc] == TC_FP_PREEMPTIBLE) 230*f62af20bSVladimir Oltean have_preemption = true; 231*f62af20bSVladimir Oltean } 232*f62af20bSVladimir Oltean 233*f62af20bSVladimir Oltean if (have_preemption && !ethtool_dev_mm_supported(dev)) { 234*f62af20bSVladimir Oltean NL_SET_ERR_MSG(extack, "Device does not support preemption"); 235*f62af20bSVladimir Oltean return -EOPNOTSUPP; 236*f62af20bSVladimir Oltean } 237*f62af20bSVladimir Oltean out: 238*f62af20bSVladimir Oltean return err; 239*f62af20bSVladimir Oltean } 240*f62af20bSVladimir Oltean 2413dd0c16eSVladimir Oltean /* Parse the other netlink attributes that represent the payload of 2423dd0c16eSVladimir Oltean * TCA_OPTIONS, which are appended right after struct tc_mqprio_qopt. 2433dd0c16eSVladimir Oltean */ 244feb2cf3dSVladimir Oltean static int mqprio_parse_nlattr(struct Qdisc *sch, struct tc_mqprio_qopt *qopt, 24557f21bf8SVladimir Oltean struct nlattr *opt, 24657f21bf8SVladimir Oltean struct netlink_ext_ack *extack) 247b8970f0bSJohn Fastabend { 2483dd0c16eSVladimir Oltean struct nlattr *nlattr_opt = nla_data(opt) + NLA_ALIGN(sizeof(*qopt)); 2493dd0c16eSVladimir Oltean int nlattr_opt_len = nla_len(opt) - NLA_ALIGN(sizeof(*qopt)); 250b8970f0bSJohn Fastabend struct mqprio_sched *priv = qdisc_priv(sch); 2513dd0c16eSVladimir Oltean struct nlattr *tb[TCA_MQPRIO_MAX + 1] = {}; 2524e8b86c0SAmritha Nambiar struct nlattr *attr; 253feb2cf3dSVladimir Oltean int i, rem, err; 254b8970f0bSJohn Fastabend 2553dd0c16eSVladimir Oltean if (nlattr_opt_len >= nla_attr_size(0)) { 2563dd0c16eSVladimir Oltean err = nla_parse_deprecated(tb, TCA_MQPRIO_MAX, nlattr_opt, 2573dd0c16eSVladimir Oltean nlattr_opt_len, mqprio_policy, 2583dd0c16eSVladimir Oltean NULL); 2594e8b86c0SAmritha Nambiar if (err < 0) 2604e8b86c0SAmritha Nambiar return err; 2613dd0c16eSVladimir Oltean } 2624e8b86c0SAmritha Nambiar 26357f21bf8SVladimir Oltean if (!qopt->hw) { 26457f21bf8SVladimir Oltean NL_SET_ERR_MSG(extack, 26557f21bf8SVladimir Oltean "mqprio TCA_OPTIONS can only contain netlink attributes in hardware mode"); 2664e8b86c0SAmritha Nambiar return -EINVAL; 26757f21bf8SVladimir Oltean } 2684e8b86c0SAmritha Nambiar 2694e8b86c0SAmritha Nambiar if (tb[TCA_MQPRIO_MODE]) { 2704e8b86c0SAmritha Nambiar priv->flags |= TC_MQPRIO_F_MODE; 2718b0f2565SPedro Tammela priv->mode = nla_get_u16(tb[TCA_MQPRIO_MODE]); 2724e8b86c0SAmritha Nambiar } 2734e8b86c0SAmritha Nambiar 2744e8b86c0SAmritha Nambiar if (tb[TCA_MQPRIO_SHAPER]) { 2754e8b86c0SAmritha Nambiar priv->flags |= TC_MQPRIO_F_SHAPER; 2768b0f2565SPedro Tammela priv->shaper = nla_get_u16(tb[TCA_MQPRIO_SHAPER]); 2774e8b86c0SAmritha Nambiar } 2784e8b86c0SAmritha Nambiar 2794e8b86c0SAmritha Nambiar if (tb[TCA_MQPRIO_MIN_RATE64]) { 28057f21bf8SVladimir Oltean if (priv->shaper != TC_MQPRIO_SHAPER_BW_RATE) { 28157f21bf8SVladimir Oltean NL_SET_ERR_MSG_ATTR(extack, tb[TCA_MQPRIO_MIN_RATE64], 28257f21bf8SVladimir Oltean "min_rate accepted only when shaper is in bw_rlimit mode"); 2834e8b86c0SAmritha Nambiar return -EINVAL; 28457f21bf8SVladimir Oltean } 2854e8b86c0SAmritha Nambiar i = 0; 2864e8b86c0SAmritha Nambiar nla_for_each_nested(attr, tb[TCA_MQPRIO_MIN_RATE64], 2874e8b86c0SAmritha Nambiar rem) { 28857f21bf8SVladimir Oltean if (nla_type(attr) != TCA_MQPRIO_MIN_RATE64) { 28957f21bf8SVladimir Oltean NL_SET_ERR_MSG_ATTR(extack, attr, 29057f21bf8SVladimir Oltean "Attribute type expected to be TCA_MQPRIO_MIN_RATE64"); 2914e8b86c0SAmritha Nambiar return -EINVAL; 29257f21bf8SVladimir Oltean } 2934e8b86c0SAmritha Nambiar if (i >= qopt->num_tc) 2944e8b86c0SAmritha Nambiar break; 2958b0f2565SPedro Tammela priv->min_rate[i] = nla_get_u64(attr); 2964e8b86c0SAmritha Nambiar i++; 2974e8b86c0SAmritha Nambiar } 2984e8b86c0SAmritha Nambiar priv->flags |= TC_MQPRIO_F_MIN_RATE; 2994e8b86c0SAmritha Nambiar } 3004e8b86c0SAmritha Nambiar 3014e8b86c0SAmritha Nambiar if (tb[TCA_MQPRIO_MAX_RATE64]) { 30257f21bf8SVladimir Oltean if (priv->shaper != TC_MQPRIO_SHAPER_BW_RATE) { 30357f21bf8SVladimir Oltean NL_SET_ERR_MSG_ATTR(extack, tb[TCA_MQPRIO_MAX_RATE64], 30457f21bf8SVladimir Oltean "max_rate accepted only when shaper is in bw_rlimit mode"); 3054e8b86c0SAmritha Nambiar return -EINVAL; 30657f21bf8SVladimir Oltean } 3074e8b86c0SAmritha Nambiar i = 0; 3084e8b86c0SAmritha Nambiar nla_for_each_nested(attr, tb[TCA_MQPRIO_MAX_RATE64], 3094e8b86c0SAmritha Nambiar rem) { 31057f21bf8SVladimir Oltean if (nla_type(attr) != TCA_MQPRIO_MAX_RATE64) { 31157f21bf8SVladimir Oltean NL_SET_ERR_MSG_ATTR(extack, attr, 31257f21bf8SVladimir Oltean "Attribute type expected to be TCA_MQPRIO_MAX_RATE64"); 3134e8b86c0SAmritha Nambiar return -EINVAL; 31457f21bf8SVladimir Oltean } 3154e8b86c0SAmritha Nambiar if (i >= qopt->num_tc) 3164e8b86c0SAmritha Nambiar break; 3178b0f2565SPedro Tammela priv->max_rate[i] = nla_get_u64(attr); 3184e8b86c0SAmritha Nambiar i++; 3194e8b86c0SAmritha Nambiar } 3204e8b86c0SAmritha Nambiar priv->flags |= TC_MQPRIO_F_MAX_RATE; 3214e8b86c0SAmritha Nambiar } 322feb2cf3dSVladimir Oltean 323*f62af20bSVladimir Oltean if (tb[TCA_MQPRIO_TC_ENTRY]) { 324*f62af20bSVladimir Oltean err = mqprio_parse_tc_entries(sch, nlattr_opt, nlattr_opt_len, 325*f62af20bSVladimir Oltean extack); 326*f62af20bSVladimir Oltean if (err) 327*f62af20bSVladimir Oltean return err; 328*f62af20bSVladimir Oltean } 329*f62af20bSVladimir Oltean 330feb2cf3dSVladimir Oltean return 0; 331feb2cf3dSVladimir Oltean } 332feb2cf3dSVladimir Oltean 333feb2cf3dSVladimir Oltean static int mqprio_init(struct Qdisc *sch, struct nlattr *opt, 334feb2cf3dSVladimir Oltean struct netlink_ext_ack *extack) 335feb2cf3dSVladimir Oltean { 336feb2cf3dSVladimir Oltean struct net_device *dev = qdisc_dev(sch); 337feb2cf3dSVladimir Oltean struct mqprio_sched *priv = qdisc_priv(sch); 338feb2cf3dSVladimir Oltean struct netdev_queue *dev_queue; 339feb2cf3dSVladimir Oltean struct Qdisc *qdisc; 340feb2cf3dSVladimir Oltean int i, err = -EOPNOTSUPP; 341feb2cf3dSVladimir Oltean struct tc_mqprio_qopt *qopt = NULL; 34219278d76SVladimir Oltean struct tc_mqprio_caps caps; 343*f62af20bSVladimir Oltean int len, tc; 344feb2cf3dSVladimir Oltean 345feb2cf3dSVladimir Oltean BUILD_BUG_ON(TC_MAX_QUEUE != TC_QOPT_MAX_QUEUE); 346feb2cf3dSVladimir Oltean BUILD_BUG_ON(TC_BITMASK != TC_QOPT_BITMASK); 347feb2cf3dSVladimir Oltean 348feb2cf3dSVladimir Oltean if (sch->parent != TC_H_ROOT) 349feb2cf3dSVladimir Oltean return -EOPNOTSUPP; 350feb2cf3dSVladimir Oltean 351feb2cf3dSVladimir Oltean if (!netif_is_multiqueue(dev)) 352feb2cf3dSVladimir Oltean return -EOPNOTSUPP; 353feb2cf3dSVladimir Oltean 354feb2cf3dSVladimir Oltean /* make certain can allocate enough classids to handle queues */ 355feb2cf3dSVladimir Oltean if (dev->num_tx_queues >= TC_H_MIN_PRIORITY) 356feb2cf3dSVladimir Oltean return -ENOMEM; 357feb2cf3dSVladimir Oltean 358feb2cf3dSVladimir Oltean if (!opt || nla_len(opt) < sizeof(*qopt)) 359feb2cf3dSVladimir Oltean return -EINVAL; 360feb2cf3dSVladimir Oltean 361*f62af20bSVladimir Oltean for (tc = 0; tc < TC_QOPT_MAX_QUEUE; tc++) 362*f62af20bSVladimir Oltean priv->fp[tc] = TC_FP_EXPRESS; 363*f62af20bSVladimir Oltean 36419278d76SVladimir Oltean qdisc_offload_query_caps(dev, TC_SETUP_QDISC_MQPRIO, 36519278d76SVladimir Oltean &caps, sizeof(caps)); 36619278d76SVladimir Oltean 367feb2cf3dSVladimir Oltean qopt = nla_data(opt); 368d404959fSVladimir Oltean if (mqprio_parse_opt(dev, qopt, &caps, extack)) 369feb2cf3dSVladimir Oltean return -EINVAL; 370feb2cf3dSVladimir Oltean 371feb2cf3dSVladimir Oltean len = nla_len(opt) - NLA_ALIGN(sizeof(*qopt)); 372feb2cf3dSVladimir Oltean if (len > 0) { 37357f21bf8SVladimir Oltean err = mqprio_parse_nlattr(sch, qopt, opt, extack); 374feb2cf3dSVladimir Oltean if (err) 375feb2cf3dSVladimir Oltean return err; 3764e8b86c0SAmritha Nambiar } 3774e8b86c0SAmritha Nambiar 378b8970f0bSJohn Fastabend /* pre-allocate qdisc, attachment can't fail */ 379b8970f0bSJohn Fastabend priv->qdiscs = kcalloc(dev->num_tx_queues, sizeof(priv->qdiscs[0]), 380b8970f0bSJohn Fastabend GFP_KERNEL); 38187b60cfaSEric Dumazet if (!priv->qdiscs) 38287b60cfaSEric Dumazet return -ENOMEM; 383b8970f0bSJohn Fastabend 384b8970f0bSJohn Fastabend for (i = 0; i < dev->num_tx_queues; i++) { 385b8970f0bSJohn Fastabend dev_queue = netdev_get_tx_queue(dev, i); 3861f27cde3SEric Dumazet qdisc = qdisc_create_dflt(dev_queue, 3871f27cde3SEric Dumazet get_default_qdisc_ops(dev, i), 388b8970f0bSJohn Fastabend TC_H_MAKE(TC_H_MAJ(sch->handle), 389a38a9882SAlexander Aring TC_H_MIN(i + 1)), extack); 39087b60cfaSEric Dumazet if (!qdisc) 39187b60cfaSEric Dumazet return -ENOMEM; 39287b60cfaSEric Dumazet 393b8970f0bSJohn Fastabend priv->qdiscs[i] = qdisc; 3944eaf3b84SEric Dumazet qdisc->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT; 395b8970f0bSJohn Fastabend } 396b8970f0bSJohn Fastabend 397b8970f0bSJohn Fastabend /* If the mqprio options indicate that hardware should own 398b8970f0bSJohn Fastabend * the queue mapping then run ndo_setup_tc otherwise use the 399b8970f0bSJohn Fastabend * supplied and verified mapping 400b8970f0bSJohn Fastabend */ 401b8970f0bSJohn Fastabend if (qopt->hw) { 402d404959fSVladimir Oltean err = mqprio_enable_offload(sch, qopt, extack); 403b8970f0bSJohn Fastabend if (err) 40487b60cfaSEric Dumazet return err; 405b8970f0bSJohn Fastabend } else { 406b8970f0bSJohn Fastabend netdev_set_num_tc(dev, qopt->num_tc); 407b8970f0bSJohn Fastabend for (i = 0; i < qopt->num_tc; i++) 408b8970f0bSJohn Fastabend netdev_set_tc_queue(dev, i, 409b8970f0bSJohn Fastabend qopt->count[i], qopt->offset[i]); 410b8970f0bSJohn Fastabend } 411b8970f0bSJohn Fastabend 412b8970f0bSJohn Fastabend /* Always use supplied priority mappings */ 413b8970f0bSJohn Fastabend for (i = 0; i < TC_BITMASK + 1; i++) 414b8970f0bSJohn Fastabend netdev_set_prio_tc_map(dev, i, qopt->prio_tc_map[i]); 415b8970f0bSJohn Fastabend 416b8970f0bSJohn Fastabend sch->flags |= TCQ_F_MQROOT; 417b8970f0bSJohn Fastabend return 0; 418b8970f0bSJohn Fastabend } 419b8970f0bSJohn Fastabend 420b8970f0bSJohn Fastabend static void mqprio_attach(struct Qdisc *sch) 421b8970f0bSJohn Fastabend { 422b8970f0bSJohn Fastabend struct net_device *dev = qdisc_dev(sch); 423b8970f0bSJohn Fastabend struct mqprio_sched *priv = qdisc_priv(sch); 42495dc1929SEric Dumazet struct Qdisc *qdisc, *old; 425b8970f0bSJohn Fastabend unsigned int ntx; 426b8970f0bSJohn Fastabend 427b8970f0bSJohn Fastabend /* Attach underlying qdisc */ 428b8970f0bSJohn Fastabend for (ntx = 0; ntx < dev->num_tx_queues; ntx++) { 429b8970f0bSJohn Fastabend qdisc = priv->qdiscs[ntx]; 43095dc1929SEric Dumazet old = dev_graft_qdisc(qdisc->dev_queue, qdisc); 43195dc1929SEric Dumazet if (old) 43286bd446bSVlad Buslov qdisc_put(old); 43395dc1929SEric Dumazet if (ntx < dev->real_num_tx_queues) 43449b49971SJiri Kosina qdisc_hash_add(qdisc, false); 435b8970f0bSJohn Fastabend } 436b8970f0bSJohn Fastabend kfree(priv->qdiscs); 437b8970f0bSJohn Fastabend priv->qdiscs = NULL; 438b8970f0bSJohn Fastabend } 439b8970f0bSJohn Fastabend 440b8970f0bSJohn Fastabend static struct netdev_queue *mqprio_queue_get(struct Qdisc *sch, 441b8970f0bSJohn Fastabend unsigned long cl) 442b8970f0bSJohn Fastabend { 443b8970f0bSJohn Fastabend struct net_device *dev = qdisc_dev(sch); 44432302902SAlexander Duyck unsigned long ntx = cl - 1; 445b8970f0bSJohn Fastabend 446b8970f0bSJohn Fastabend if (ntx >= dev->num_tx_queues) 447b8970f0bSJohn Fastabend return NULL; 448b8970f0bSJohn Fastabend return netdev_get_tx_queue(dev, ntx); 449b8970f0bSJohn Fastabend } 450b8970f0bSJohn Fastabend 451b8970f0bSJohn Fastabend static int mqprio_graft(struct Qdisc *sch, unsigned long cl, struct Qdisc *new, 452653d6fd6SAlexander Aring struct Qdisc **old, struct netlink_ext_ack *extack) 453b8970f0bSJohn Fastabend { 454b8970f0bSJohn Fastabend struct net_device *dev = qdisc_dev(sch); 455b8970f0bSJohn Fastabend struct netdev_queue *dev_queue = mqprio_queue_get(sch, cl); 456b8970f0bSJohn Fastabend 457b8970f0bSJohn Fastabend if (!dev_queue) 458b8970f0bSJohn Fastabend return -EINVAL; 459b8970f0bSJohn Fastabend 460b8970f0bSJohn Fastabend if (dev->flags & IFF_UP) 461b8970f0bSJohn Fastabend dev_deactivate(dev); 462b8970f0bSJohn Fastabend 463b8970f0bSJohn Fastabend *old = dev_graft_qdisc(dev_queue, new); 464b8970f0bSJohn Fastabend 4651abbe139SEric Dumazet if (new) 4664eaf3b84SEric Dumazet new->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT; 4671abbe139SEric Dumazet 468b8970f0bSJohn Fastabend if (dev->flags & IFF_UP) 469b8970f0bSJohn Fastabend dev_activate(dev); 470b8970f0bSJohn Fastabend 471b8970f0bSJohn Fastabend return 0; 472b8970f0bSJohn Fastabend } 473b8970f0bSJohn Fastabend 4744e8b86c0SAmritha Nambiar static int dump_rates(struct mqprio_sched *priv, 4754e8b86c0SAmritha Nambiar struct tc_mqprio_qopt *opt, struct sk_buff *skb) 4764e8b86c0SAmritha Nambiar { 4774e8b86c0SAmritha Nambiar struct nlattr *nest; 4784e8b86c0SAmritha Nambiar int i; 4794e8b86c0SAmritha Nambiar 4804e8b86c0SAmritha Nambiar if (priv->flags & TC_MQPRIO_F_MIN_RATE) { 481ae0be8deSMichal Kubecek nest = nla_nest_start_noflag(skb, TCA_MQPRIO_MIN_RATE64); 4824e8b86c0SAmritha Nambiar if (!nest) 4834e8b86c0SAmritha Nambiar goto nla_put_failure; 4844e8b86c0SAmritha Nambiar 4854e8b86c0SAmritha Nambiar for (i = 0; i < opt->num_tc; i++) { 4864e8b86c0SAmritha Nambiar if (nla_put(skb, TCA_MQPRIO_MIN_RATE64, 4874e8b86c0SAmritha Nambiar sizeof(priv->min_rate[i]), 4884e8b86c0SAmritha Nambiar &priv->min_rate[i])) 4894e8b86c0SAmritha Nambiar goto nla_put_failure; 4904e8b86c0SAmritha Nambiar } 4914e8b86c0SAmritha Nambiar nla_nest_end(skb, nest); 4924e8b86c0SAmritha Nambiar } 4934e8b86c0SAmritha Nambiar 4944e8b86c0SAmritha Nambiar if (priv->flags & TC_MQPRIO_F_MAX_RATE) { 495ae0be8deSMichal Kubecek nest = nla_nest_start_noflag(skb, TCA_MQPRIO_MAX_RATE64); 4964e8b86c0SAmritha Nambiar if (!nest) 4974e8b86c0SAmritha Nambiar goto nla_put_failure; 4984e8b86c0SAmritha Nambiar 4994e8b86c0SAmritha Nambiar for (i = 0; i < opt->num_tc; i++) { 5004e8b86c0SAmritha Nambiar if (nla_put(skb, TCA_MQPRIO_MAX_RATE64, 5014e8b86c0SAmritha Nambiar sizeof(priv->max_rate[i]), 5024e8b86c0SAmritha Nambiar &priv->max_rate[i])) 5034e8b86c0SAmritha Nambiar goto nla_put_failure; 5044e8b86c0SAmritha Nambiar } 5054e8b86c0SAmritha Nambiar nla_nest_end(skb, nest); 5064e8b86c0SAmritha Nambiar } 5074e8b86c0SAmritha Nambiar return 0; 5084e8b86c0SAmritha Nambiar 5094e8b86c0SAmritha Nambiar nla_put_failure: 5104e8b86c0SAmritha Nambiar nla_nest_cancel(skb, nest); 5114e8b86c0SAmritha Nambiar return -1; 5124e8b86c0SAmritha Nambiar } 5134e8b86c0SAmritha Nambiar 514*f62af20bSVladimir Oltean static int mqprio_dump_tc_entries(struct mqprio_sched *priv, 515*f62af20bSVladimir Oltean struct sk_buff *skb) 516*f62af20bSVladimir Oltean { 517*f62af20bSVladimir Oltean struct nlattr *n; 518*f62af20bSVladimir Oltean int tc; 519*f62af20bSVladimir Oltean 520*f62af20bSVladimir Oltean for (tc = 0; tc < TC_QOPT_MAX_QUEUE; tc++) { 521*f62af20bSVladimir Oltean n = nla_nest_start(skb, TCA_MQPRIO_TC_ENTRY); 522*f62af20bSVladimir Oltean if (!n) 523*f62af20bSVladimir Oltean return -EMSGSIZE; 524*f62af20bSVladimir Oltean 525*f62af20bSVladimir Oltean if (nla_put_u32(skb, TCA_MQPRIO_TC_ENTRY_INDEX, tc)) 526*f62af20bSVladimir Oltean goto nla_put_failure; 527*f62af20bSVladimir Oltean 528*f62af20bSVladimir Oltean if (nla_put_u32(skb, TCA_MQPRIO_TC_ENTRY_FP, priv->fp[tc])) 529*f62af20bSVladimir Oltean goto nla_put_failure; 530*f62af20bSVladimir Oltean 531*f62af20bSVladimir Oltean nla_nest_end(skb, n); 532*f62af20bSVladimir Oltean } 533*f62af20bSVladimir Oltean 534*f62af20bSVladimir Oltean return 0; 535*f62af20bSVladimir Oltean 536*f62af20bSVladimir Oltean nla_put_failure: 537*f62af20bSVladimir Oltean nla_nest_cancel(skb, n); 538*f62af20bSVladimir Oltean return -EMSGSIZE; 539*f62af20bSVladimir Oltean } 540*f62af20bSVladimir Oltean 541b8970f0bSJohn Fastabend static int mqprio_dump(struct Qdisc *sch, struct sk_buff *skb) 542b8970f0bSJohn Fastabend { 543b8970f0bSJohn Fastabend struct net_device *dev = qdisc_dev(sch); 544b8970f0bSJohn Fastabend struct mqprio_sched *priv = qdisc_priv(sch); 5454e8b86c0SAmritha Nambiar struct nlattr *nla = (struct nlattr *)skb_tail_pointer(skb); 546144ce879SEric Dumazet struct tc_mqprio_qopt opt = { 0 }; 547b8970f0bSJohn Fastabend struct Qdisc *qdisc; 5489dd6ad67SVladimir Oltean unsigned int ntx; 549b8970f0bSJohn Fastabend 550b8970f0bSJohn Fastabend sch->q.qlen = 0; 55150dc9a85SAhmed S. Darwish gnet_stats_basic_sync_init(&sch->bstats); 552b8970f0bSJohn Fastabend memset(&sch->qstats, 0, sizeof(sch->qstats)); 553b8970f0bSJohn Fastabend 554ce679e8dSJohn Fastabend /* MQ supports lockless qdiscs. However, statistics accounting needs 555ce679e8dSJohn Fastabend * to account for all, none, or a mix of locked and unlocked child 556ce679e8dSJohn Fastabend * qdiscs. Percpu stats are added to counters in-band and locking 557ce679e8dSJohn Fastabend * qdisc totals are added at end. 558ce679e8dSJohn Fastabend */ 559ce679e8dSJohn Fastabend for (ntx = 0; ntx < dev->num_tx_queues; ntx++) { 560ce679e8dSJohn Fastabend qdisc = netdev_get_tx_queue(dev, ntx)->qdisc_sleeping; 561b8970f0bSJohn Fastabend spin_lock_bh(qdisc_lock(qdisc)); 562ce679e8dSJohn Fastabend 56329cbcd85SAhmed S. Darwish gnet_stats_add_basic(&sch->bstats, qdisc->cpu_bstats, 56429cbcd85SAhmed S. Darwish &qdisc->bstats, false); 5657361df46SSebastian Andrzej Siewior gnet_stats_add_queue(&sch->qstats, qdisc->cpu_qstats, 5667361df46SSebastian Andrzej Siewior &qdisc->qstats); 5677361df46SSebastian Andrzej Siewior sch->q.qlen += qdisc_qlen(qdisc); 568ce679e8dSJohn Fastabend 569b8970f0bSJohn Fastabend spin_unlock_bh(qdisc_lock(qdisc)); 570b8970f0bSJohn Fastabend } 571b8970f0bSJohn Fastabend 5729dd6ad67SVladimir Oltean mqprio_qopt_reconstruct(dev, &opt); 5732026fecfSAlexander Duyck opt.hw = priv->hw_offload; 574b8970f0bSJohn Fastabend 5759f104c77SVladyslav Tarasiuk if (nla_put(skb, TCA_OPTIONS, sizeof(opt), &opt)) 5761b34ec43SDavid S. Miller goto nla_put_failure; 577b8970f0bSJohn Fastabend 5784e8b86c0SAmritha Nambiar if ((priv->flags & TC_MQPRIO_F_MODE) && 5794e8b86c0SAmritha Nambiar nla_put_u16(skb, TCA_MQPRIO_MODE, priv->mode)) 5804e8b86c0SAmritha Nambiar goto nla_put_failure; 5814e8b86c0SAmritha Nambiar 5824e8b86c0SAmritha Nambiar if ((priv->flags & TC_MQPRIO_F_SHAPER) && 5834e8b86c0SAmritha Nambiar nla_put_u16(skb, TCA_MQPRIO_SHAPER, priv->shaper)) 5844e8b86c0SAmritha Nambiar goto nla_put_failure; 5854e8b86c0SAmritha Nambiar 5864e8b86c0SAmritha Nambiar if ((priv->flags & TC_MQPRIO_F_MIN_RATE || 5874e8b86c0SAmritha Nambiar priv->flags & TC_MQPRIO_F_MAX_RATE) && 5884e8b86c0SAmritha Nambiar (dump_rates(priv, &opt, skb) != 0)) 5894e8b86c0SAmritha Nambiar goto nla_put_failure; 5904e8b86c0SAmritha Nambiar 591*f62af20bSVladimir Oltean if (mqprio_dump_tc_entries(priv, skb)) 592*f62af20bSVladimir Oltean goto nla_put_failure; 593*f62af20bSVladimir Oltean 5944e8b86c0SAmritha Nambiar return nla_nest_end(skb, nla); 595b8970f0bSJohn Fastabend nla_put_failure: 5964e8b86c0SAmritha Nambiar nlmsg_trim(skb, nla); 597b8970f0bSJohn Fastabend return -1; 598b8970f0bSJohn Fastabend } 599b8970f0bSJohn Fastabend 600b8970f0bSJohn Fastabend static struct Qdisc *mqprio_leaf(struct Qdisc *sch, unsigned long cl) 601b8970f0bSJohn Fastabend { 602b8970f0bSJohn Fastabend struct netdev_queue *dev_queue = mqprio_queue_get(sch, cl); 603b8970f0bSJohn Fastabend 604b8970f0bSJohn Fastabend if (!dev_queue) 605b8970f0bSJohn Fastabend return NULL; 606b8970f0bSJohn Fastabend 607b8970f0bSJohn Fastabend return dev_queue->qdisc_sleeping; 608b8970f0bSJohn Fastabend } 609b8970f0bSJohn Fastabend 610143976ceSWANG Cong static unsigned long mqprio_find(struct Qdisc *sch, u32 classid) 611b8970f0bSJohn Fastabend { 612b8970f0bSJohn Fastabend struct net_device *dev = qdisc_dev(sch); 613b8970f0bSJohn Fastabend unsigned int ntx = TC_H_MIN(classid); 614b8970f0bSJohn Fastabend 61532302902SAlexander Duyck /* There are essentially two regions here that have valid classid 61632302902SAlexander Duyck * values. The first region will have a classid value of 1 through 61732302902SAlexander Duyck * num_tx_queues. All of these are backed by actual Qdiscs. 61832302902SAlexander Duyck */ 61932302902SAlexander Duyck if (ntx < TC_H_MIN_PRIORITY) 62032302902SAlexander Duyck return (ntx <= dev->num_tx_queues) ? ntx : 0; 62132302902SAlexander Duyck 62232302902SAlexander Duyck /* The second region represents the hardware traffic classes. These 62332302902SAlexander Duyck * are represented by classid values of TC_H_MIN_PRIORITY through 62432302902SAlexander Duyck * TC_H_MIN_PRIORITY + netdev_get_num_tc - 1 62532302902SAlexander Duyck */ 62632302902SAlexander Duyck return ((ntx - TC_H_MIN_PRIORITY) < netdev_get_num_tc(dev)) ? ntx : 0; 627b8970f0bSJohn Fastabend } 628b8970f0bSJohn Fastabend 629b8970f0bSJohn Fastabend static int mqprio_dump_class(struct Qdisc *sch, unsigned long cl, 630b8970f0bSJohn Fastabend struct sk_buff *skb, struct tcmsg *tcm) 631b8970f0bSJohn Fastabend { 63232302902SAlexander Duyck if (cl < TC_H_MIN_PRIORITY) { 63332302902SAlexander Duyck struct netdev_queue *dev_queue = mqprio_queue_get(sch, cl); 634b8970f0bSJohn Fastabend struct net_device *dev = qdisc_dev(sch); 63532302902SAlexander Duyck int tc = netdev_txq_to_tc(dev, cl - 1); 636b8970f0bSJohn Fastabend 63732302902SAlexander Duyck tcm->tcm_parent = (tc < 0) ? 0 : 63832302902SAlexander Duyck TC_H_MAKE(TC_H_MAJ(sch->handle), 63932302902SAlexander Duyck TC_H_MIN(tc + TC_H_MIN_PRIORITY)); 64032302902SAlexander Duyck tcm->tcm_info = dev_queue->qdisc_sleeping->handle; 64132302902SAlexander Duyck } else { 642b8970f0bSJohn Fastabend tcm->tcm_parent = TC_H_ROOT; 643b8970f0bSJohn Fastabend tcm->tcm_info = 0; 644b8970f0bSJohn Fastabend } 645b8970f0bSJohn Fastabend tcm->tcm_handle |= TC_H_MIN(cl); 646b8970f0bSJohn Fastabend return 0; 647b8970f0bSJohn Fastabend } 648b8970f0bSJohn Fastabend 649b8970f0bSJohn Fastabend static int mqprio_dump_class_stats(struct Qdisc *sch, unsigned long cl, 650b8970f0bSJohn Fastabend struct gnet_dump *d) 651ea18fd95Sstephen hemminger __releases(d->lock) 652ea18fd95Sstephen hemminger __acquires(d->lock) 653b8970f0bSJohn Fastabend { 65432302902SAlexander Duyck if (cl >= TC_H_MIN_PRIORITY) { 655b8970f0bSJohn Fastabend int i; 6567361df46SSebastian Andrzej Siewior __u32 qlen; 657b8970f0bSJohn Fastabend struct gnet_stats_queue qstats = {0}; 65850dc9a85SAhmed S. Darwish struct gnet_stats_basic_sync bstats; 65932302902SAlexander Duyck struct net_device *dev = qdisc_dev(sch); 66032302902SAlexander Duyck struct netdev_tc_txq tc = dev->tc_to_txq[cl & TC_BITMASK]; 661b8970f0bSJohn Fastabend 66250dc9a85SAhmed S. Darwish gnet_stats_basic_sync_init(&bstats); 663b8970f0bSJohn Fastabend /* Drop lock here it will be reclaimed before touching 664b8970f0bSJohn Fastabend * statistics this is required because the d->lock we 665b8970f0bSJohn Fastabend * hold here is the look on dev_queue->qdisc_sleeping 666b8970f0bSJohn Fastabend * also acquired below. 667b8970f0bSJohn Fastabend */ 668edb09eb1SEric Dumazet if (d->lock) 669b8970f0bSJohn Fastabend spin_unlock_bh(d->lock); 670b8970f0bSJohn Fastabend 671b8970f0bSJohn Fastabend for (i = tc.offset; i < tc.offset + tc.count; i++) { 67246e5da40SJohn Fastabend struct netdev_queue *q = netdev_get_tx_queue(dev, i); 673ce679e8dSJohn Fastabend struct Qdisc *qdisc = rtnl_dereference(q->qdisc); 67446e5da40SJohn Fastabend 675b8970f0bSJohn Fastabend spin_lock_bh(qdisc_lock(qdisc)); 676ce679e8dSJohn Fastabend 67729cbcd85SAhmed S. Darwish gnet_stats_add_basic(&bstats, qdisc->cpu_bstats, 67829cbcd85SAhmed S. Darwish &qdisc->bstats, false); 6797361df46SSebastian Andrzej Siewior gnet_stats_add_queue(&qstats, qdisc->cpu_qstats, 6807361df46SSebastian Andrzej Siewior &qdisc->qstats); 6817361df46SSebastian Andrzej Siewior sch->q.qlen += qdisc_qlen(qdisc); 6827361df46SSebastian Andrzej Siewior 683b8970f0bSJohn Fastabend spin_unlock_bh(qdisc_lock(qdisc)); 684b8970f0bSJohn Fastabend } 6857361df46SSebastian Andrzej Siewior qlen = qdisc_qlen(sch) + qstats.qlen; 686ce679e8dSJohn Fastabend 687b8970f0bSJohn Fastabend /* Reclaim root sleeping lock before completing stats */ 688edb09eb1SEric Dumazet if (d->lock) 689b8970f0bSJohn Fastabend spin_lock_bh(d->lock); 69029cbcd85SAhmed S. Darwish if (gnet_stats_copy_basic(d, NULL, &bstats, false) < 0 || 691b0ab6f92SJohn Fastabend gnet_stats_copy_queue(d, NULL, &qstats, qlen) < 0) 692b8970f0bSJohn Fastabend return -1; 693b8970f0bSJohn Fastabend } else { 694b8970f0bSJohn Fastabend struct netdev_queue *dev_queue = mqprio_queue_get(sch, cl); 695b8970f0bSJohn Fastabend 696b8970f0bSJohn Fastabend sch = dev_queue->qdisc_sleeping; 69729cbcd85SAhmed S. Darwish if (gnet_stats_copy_basic(d, sch->cpu_bstats, 69829cbcd85SAhmed S. Darwish &sch->bstats, true) < 0 || 6995dd431b6SPaolo Abeni qdisc_qstats_copy(d, sch) < 0) 700b8970f0bSJohn Fastabend return -1; 701b8970f0bSJohn Fastabend } 702b8970f0bSJohn Fastabend return 0; 703b8970f0bSJohn Fastabend } 704b8970f0bSJohn Fastabend 705b8970f0bSJohn Fastabend static void mqprio_walk(struct Qdisc *sch, struct qdisc_walker *arg) 706b8970f0bSJohn Fastabend { 707b8970f0bSJohn Fastabend struct net_device *dev = qdisc_dev(sch); 708b8970f0bSJohn Fastabend unsigned long ntx; 709b8970f0bSJohn Fastabend 710b8970f0bSJohn Fastabend if (arg->stop) 711b8970f0bSJohn Fastabend return; 712b8970f0bSJohn Fastabend 713b8970f0bSJohn Fastabend /* Walk hierarchy with a virtual class per tc */ 714b8970f0bSJohn Fastabend arg->count = arg->skip; 71532302902SAlexander Duyck for (ntx = arg->skip; ntx < netdev_get_num_tc(dev); ntx++) { 716e046fa89SZhengchao Shao if (!tc_qdisc_stats_dump(sch, ntx + TC_H_MIN_PRIORITY, arg)) 71732302902SAlexander Duyck return; 71832302902SAlexander Duyck } 71932302902SAlexander Duyck 72032302902SAlexander Duyck /* Pad the values and skip over unused traffic classes */ 72132302902SAlexander Duyck if (ntx < TC_MAX_QUEUE) { 72232302902SAlexander Duyck arg->count = TC_MAX_QUEUE; 72332302902SAlexander Duyck ntx = TC_MAX_QUEUE; 72432302902SAlexander Duyck } 72532302902SAlexander Duyck 72632302902SAlexander Duyck /* Reset offset, sort out remaining per-queue qdiscs */ 72732302902SAlexander Duyck for (ntx -= TC_MAX_QUEUE; ntx < dev->num_tx_queues; ntx++) { 728b8970f0bSJohn Fastabend if (arg->fn(sch, ntx + 1, arg) < 0) { 729b8970f0bSJohn Fastabend arg->stop = 1; 73032302902SAlexander Duyck return; 731b8970f0bSJohn Fastabend } 732b8970f0bSJohn Fastabend arg->count++; 733b8970f0bSJohn Fastabend } 734b8970f0bSJohn Fastabend } 735b8970f0bSJohn Fastabend 7360f7787b4SJesus Sanchez-Palencia static struct netdev_queue *mqprio_select_queue(struct Qdisc *sch, 7370f7787b4SJesus Sanchez-Palencia struct tcmsg *tcm) 7380f7787b4SJesus Sanchez-Palencia { 7390f7787b4SJesus Sanchez-Palencia return mqprio_queue_get(sch, TC_H_MIN(tcm->tcm_parent)); 7400f7787b4SJesus Sanchez-Palencia } 7410f7787b4SJesus Sanchez-Palencia 742b8970f0bSJohn Fastabend static const struct Qdisc_class_ops mqprio_class_ops = { 743b8970f0bSJohn Fastabend .graft = mqprio_graft, 744b8970f0bSJohn Fastabend .leaf = mqprio_leaf, 745143976ceSWANG Cong .find = mqprio_find, 746b8970f0bSJohn Fastabend .walk = mqprio_walk, 747b8970f0bSJohn Fastabend .dump = mqprio_dump_class, 748b8970f0bSJohn Fastabend .dump_stats = mqprio_dump_class_stats, 7490f7787b4SJesus Sanchez-Palencia .select_queue = mqprio_select_queue, 750b8970f0bSJohn Fastabend }; 751b8970f0bSJohn Fastabend 752ea18fd95Sstephen hemminger static struct Qdisc_ops mqprio_qdisc_ops __read_mostly = { 753b8970f0bSJohn Fastabend .cl_ops = &mqprio_class_ops, 754b8970f0bSJohn Fastabend .id = "mqprio", 755b8970f0bSJohn Fastabend .priv_size = sizeof(struct mqprio_sched), 756b8970f0bSJohn Fastabend .init = mqprio_init, 757b8970f0bSJohn Fastabend .destroy = mqprio_destroy, 758b8970f0bSJohn Fastabend .attach = mqprio_attach, 759f7116fb4SJakub Kicinski .change_real_num_tx = mq_change_real_num_tx, 760b8970f0bSJohn Fastabend .dump = mqprio_dump, 761b8970f0bSJohn Fastabend .owner = THIS_MODULE, 762b8970f0bSJohn Fastabend }; 763b8970f0bSJohn Fastabend 764b8970f0bSJohn Fastabend static int __init mqprio_module_init(void) 765b8970f0bSJohn Fastabend { 766b8970f0bSJohn Fastabend return register_qdisc(&mqprio_qdisc_ops); 767b8970f0bSJohn Fastabend } 768b8970f0bSJohn Fastabend 769b8970f0bSJohn Fastabend static void __exit mqprio_module_exit(void) 770b8970f0bSJohn Fastabend { 771b8970f0bSJohn Fastabend unregister_qdisc(&mqprio_qdisc_ops); 772b8970f0bSJohn Fastabend } 773b8970f0bSJohn Fastabend 774b8970f0bSJohn Fastabend module_init(mqprio_module_init); 775b8970f0bSJohn Fastabend module_exit(mqprio_module_exit); 776b8970f0bSJohn Fastabend 777b8970f0bSJohn Fastabend MODULE_LICENSE("GPL"); 778