1b8970f0bSJohn Fastabend /* 2b8970f0bSJohn Fastabend * net/sched/sch_mqprio.c 3b8970f0bSJohn Fastabend * 4b8970f0bSJohn Fastabend * Copyright (c) 2010 John Fastabend <john.r.fastabend@intel.com> 5b8970f0bSJohn Fastabend * 6b8970f0bSJohn Fastabend * This program is free software; you can redistribute it and/or 7b8970f0bSJohn Fastabend * modify it under the terms of the GNU General Public License 8b8970f0bSJohn Fastabend * version 2 as published by the Free Software Foundation. 9b8970f0bSJohn Fastabend */ 10b8970f0bSJohn Fastabend 11b8970f0bSJohn Fastabend #include <linux/types.h> 12b8970f0bSJohn Fastabend #include <linux/slab.h> 13b8970f0bSJohn Fastabend #include <linux/kernel.h> 14b8970f0bSJohn Fastabend #include <linux/string.h> 15b8970f0bSJohn Fastabend #include <linux/errno.h> 16b8970f0bSJohn Fastabend #include <linux/skbuff.h> 173a9a231dSPaul Gortmaker #include <linux/module.h> 18b8970f0bSJohn Fastabend #include <net/netlink.h> 19b8970f0bSJohn Fastabend #include <net/pkt_sched.h> 20b8970f0bSJohn Fastabend #include <net/sch_generic.h> 21b8970f0bSJohn Fastabend 22b8970f0bSJohn Fastabend struct mqprio_sched { 23b8970f0bSJohn Fastabend struct Qdisc **qdiscs; 242026fecfSAlexander Duyck int hw_offload; 25b8970f0bSJohn Fastabend }; 26b8970f0bSJohn Fastabend 27b8970f0bSJohn Fastabend static void mqprio_destroy(struct Qdisc *sch) 28b8970f0bSJohn Fastabend { 29b8970f0bSJohn Fastabend struct net_device *dev = qdisc_dev(sch); 30b8970f0bSJohn Fastabend struct mqprio_sched *priv = qdisc_priv(sch); 31b8970f0bSJohn Fastabend unsigned int ntx; 32b8970f0bSJohn Fastabend 33ac7100baSBen Hutchings if (priv->qdiscs) { 34ac7100baSBen Hutchings for (ntx = 0; 35ac7100baSBen Hutchings ntx < dev->num_tx_queues && priv->qdiscs[ntx]; 36ac7100baSBen Hutchings ntx++) 37b8970f0bSJohn Fastabend qdisc_destroy(priv->qdiscs[ntx]); 38ac7100baSBen Hutchings kfree(priv->qdiscs); 39ac7100baSBen Hutchings } 40b8970f0bSJohn Fastabend 41*56f36acdSAmritha Nambiar if (priv->hw_offload && dev->netdev_ops->ndo_setup_tc) { 42*56f36acdSAmritha Nambiar struct tc_mqprio_qopt offload = { 0 }; 43*56f36acdSAmritha Nambiar struct tc_to_netdev tc = { .type = TC_SETUP_MQPRIO, 44*56f36acdSAmritha Nambiar { .mqprio = &offload } }; 45*56f36acdSAmritha Nambiar 4616e5cc64SJohn Fastabend dev->netdev_ops->ndo_setup_tc(dev, sch->handle, 0, &tc); 47*56f36acdSAmritha Nambiar } else { 48b8970f0bSJohn Fastabend netdev_set_num_tc(dev, 0); 49b8970f0bSJohn Fastabend } 50*56f36acdSAmritha Nambiar } 51b8970f0bSJohn Fastabend 52b8970f0bSJohn Fastabend static int mqprio_parse_opt(struct net_device *dev, struct tc_mqprio_qopt *qopt) 53b8970f0bSJohn Fastabend { 54b8970f0bSJohn Fastabend int i, j; 55b8970f0bSJohn Fastabend 56b8970f0bSJohn Fastabend /* Verify num_tc is not out of max range */ 57b8970f0bSJohn Fastabend if (qopt->num_tc > TC_MAX_QUEUE) 58b8970f0bSJohn Fastabend return -EINVAL; 59b8970f0bSJohn Fastabend 60b8970f0bSJohn Fastabend /* Verify priority mapping uses valid tcs */ 61b8970f0bSJohn Fastabend for (i = 0; i < TC_BITMASK + 1; i++) { 62b8970f0bSJohn Fastabend if (qopt->prio_tc_map[i] >= qopt->num_tc) 63b8970f0bSJohn Fastabend return -EINVAL; 64b8970f0bSJohn Fastabend } 65b8970f0bSJohn Fastabend 662026fecfSAlexander Duyck /* Limit qopt->hw to maximum supported offload value. Drivers have 672026fecfSAlexander Duyck * the option of overriding this later if they don't support the a 682026fecfSAlexander Duyck * given offload type. 692026fecfSAlexander Duyck */ 702026fecfSAlexander Duyck if (qopt->hw > TC_MQPRIO_HW_OFFLOAD_MAX) 712026fecfSAlexander Duyck qopt->hw = TC_MQPRIO_HW_OFFLOAD_MAX; 72b8970f0bSJohn Fastabend 732026fecfSAlexander Duyck /* If hardware offload is requested we will leave it to the device 742026fecfSAlexander Duyck * to either populate the queue counts itself or to validate the 752026fecfSAlexander Duyck * provided queue counts. If ndo_setup_tc is not present then 762026fecfSAlexander Duyck * hardware doesn't support offload and we should return an error. 77b8970f0bSJohn Fastabend */ 78b8970f0bSJohn Fastabend if (qopt->hw) 792026fecfSAlexander Duyck return dev->netdev_ops->ndo_setup_tc ? 0 : -EINVAL; 80b8970f0bSJohn Fastabend 81b8970f0bSJohn Fastabend for (i = 0; i < qopt->num_tc; i++) { 82b8970f0bSJohn Fastabend unsigned int last = qopt->offset[i] + qopt->count[i]; 83b8970f0bSJohn Fastabend 84b8970f0bSJohn Fastabend /* Verify the queue count is in tx range being equal to the 85b8970f0bSJohn Fastabend * real_num_tx_queues indicates the last queue is in use. 86b8970f0bSJohn Fastabend */ 87b8970f0bSJohn Fastabend if (qopt->offset[i] >= dev->real_num_tx_queues || 88b8970f0bSJohn Fastabend !qopt->count[i] || 89b8970f0bSJohn Fastabend last > dev->real_num_tx_queues) 90b8970f0bSJohn Fastabend return -EINVAL; 91b8970f0bSJohn Fastabend 92b8970f0bSJohn Fastabend /* Verify that the offset and counts do not overlap */ 93b8970f0bSJohn Fastabend for (j = i + 1; j < qopt->num_tc; j++) { 94b8970f0bSJohn Fastabend if (last > qopt->offset[j]) 95b8970f0bSJohn Fastabend return -EINVAL; 96b8970f0bSJohn Fastabend } 97b8970f0bSJohn Fastabend } 98b8970f0bSJohn Fastabend 99b8970f0bSJohn Fastabend return 0; 100b8970f0bSJohn Fastabend } 101b8970f0bSJohn Fastabend 102b8970f0bSJohn Fastabend static int mqprio_init(struct Qdisc *sch, struct nlattr *opt) 103b8970f0bSJohn Fastabend { 104b8970f0bSJohn Fastabend struct net_device *dev = qdisc_dev(sch); 105b8970f0bSJohn Fastabend struct mqprio_sched *priv = qdisc_priv(sch); 106b8970f0bSJohn Fastabend struct netdev_queue *dev_queue; 107b8970f0bSJohn Fastabend struct Qdisc *qdisc; 108b8970f0bSJohn Fastabend int i, err = -EOPNOTSUPP; 109b8970f0bSJohn Fastabend struct tc_mqprio_qopt *qopt = NULL; 110b8970f0bSJohn Fastabend 111b8970f0bSJohn Fastabend BUILD_BUG_ON(TC_MAX_QUEUE != TC_QOPT_MAX_QUEUE); 112b8970f0bSJohn Fastabend BUILD_BUG_ON(TC_BITMASK != TC_QOPT_BITMASK); 113b8970f0bSJohn Fastabend 114b8970f0bSJohn Fastabend if (sch->parent != TC_H_ROOT) 115b8970f0bSJohn Fastabend return -EOPNOTSUPP; 116b8970f0bSJohn Fastabend 117b8970f0bSJohn Fastabend if (!netif_is_multiqueue(dev)) 118b8970f0bSJohn Fastabend return -EOPNOTSUPP; 119b8970f0bSJohn Fastabend 1207838f2ceSThomas Graf if (!opt || nla_len(opt) < sizeof(*qopt)) 121b8970f0bSJohn Fastabend return -EINVAL; 122b8970f0bSJohn Fastabend 123b8970f0bSJohn Fastabend qopt = nla_data(opt); 124b8970f0bSJohn Fastabend if (mqprio_parse_opt(dev, qopt)) 125b8970f0bSJohn Fastabend return -EINVAL; 126b8970f0bSJohn Fastabend 127b8970f0bSJohn Fastabend /* pre-allocate qdisc, attachment can't fail */ 128b8970f0bSJohn Fastabend priv->qdiscs = kcalloc(dev->num_tx_queues, sizeof(priv->qdiscs[0]), 129b8970f0bSJohn Fastabend GFP_KERNEL); 13087b60cfaSEric Dumazet if (!priv->qdiscs) 13187b60cfaSEric Dumazet return -ENOMEM; 132b8970f0bSJohn Fastabend 133b8970f0bSJohn Fastabend for (i = 0; i < dev->num_tx_queues; i++) { 134b8970f0bSJohn Fastabend dev_queue = netdev_get_tx_queue(dev, i); 1351f27cde3SEric Dumazet qdisc = qdisc_create_dflt(dev_queue, 1361f27cde3SEric Dumazet get_default_qdisc_ops(dev, i), 137b8970f0bSJohn Fastabend TC_H_MAKE(TC_H_MAJ(sch->handle), 138b8970f0bSJohn Fastabend TC_H_MIN(i + 1))); 13987b60cfaSEric Dumazet if (!qdisc) 14087b60cfaSEric Dumazet return -ENOMEM; 14187b60cfaSEric Dumazet 142b8970f0bSJohn Fastabend priv->qdiscs[i] = qdisc; 1434eaf3b84SEric Dumazet qdisc->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT; 144b8970f0bSJohn Fastabend } 145b8970f0bSJohn Fastabend 146b8970f0bSJohn Fastabend /* If the mqprio options indicate that hardware should own 147b8970f0bSJohn Fastabend * the queue mapping then run ndo_setup_tc otherwise use the 148b8970f0bSJohn Fastabend * supplied and verified mapping 149b8970f0bSJohn Fastabend */ 150b8970f0bSJohn Fastabend if (qopt->hw) { 151*56f36acdSAmritha Nambiar struct tc_mqprio_qopt offload = *qopt; 15216e5cc64SJohn Fastabend struct tc_to_netdev tc = { .type = TC_SETUP_MQPRIO, 153*56f36acdSAmritha Nambiar { .mqprio = &offload } }; 15416e5cc64SJohn Fastabend 15516e5cc64SJohn Fastabend err = dev->netdev_ops->ndo_setup_tc(dev, sch->handle, 0, &tc); 156b8970f0bSJohn Fastabend if (err) 15787b60cfaSEric Dumazet return err; 1582026fecfSAlexander Duyck 159*56f36acdSAmritha Nambiar priv->hw_offload = offload.hw; 160b8970f0bSJohn Fastabend } else { 161b8970f0bSJohn Fastabend netdev_set_num_tc(dev, qopt->num_tc); 162b8970f0bSJohn Fastabend for (i = 0; i < qopt->num_tc; i++) 163b8970f0bSJohn Fastabend netdev_set_tc_queue(dev, i, 164b8970f0bSJohn Fastabend qopt->count[i], qopt->offset[i]); 165b8970f0bSJohn Fastabend } 166b8970f0bSJohn Fastabend 167b8970f0bSJohn Fastabend /* Always use supplied priority mappings */ 168b8970f0bSJohn Fastabend for (i = 0; i < TC_BITMASK + 1; i++) 169b8970f0bSJohn Fastabend netdev_set_prio_tc_map(dev, i, qopt->prio_tc_map[i]); 170b8970f0bSJohn Fastabend 171b8970f0bSJohn Fastabend sch->flags |= TCQ_F_MQROOT; 172b8970f0bSJohn Fastabend return 0; 173b8970f0bSJohn Fastabend } 174b8970f0bSJohn Fastabend 175b8970f0bSJohn Fastabend static void mqprio_attach(struct Qdisc *sch) 176b8970f0bSJohn Fastabend { 177b8970f0bSJohn Fastabend struct net_device *dev = qdisc_dev(sch); 178b8970f0bSJohn Fastabend struct mqprio_sched *priv = qdisc_priv(sch); 17995dc1929SEric Dumazet struct Qdisc *qdisc, *old; 180b8970f0bSJohn Fastabend unsigned int ntx; 181b8970f0bSJohn Fastabend 182b8970f0bSJohn Fastabend /* Attach underlying qdisc */ 183b8970f0bSJohn Fastabend for (ntx = 0; ntx < dev->num_tx_queues; ntx++) { 184b8970f0bSJohn Fastabend qdisc = priv->qdiscs[ntx]; 18595dc1929SEric Dumazet old = dev_graft_qdisc(qdisc->dev_queue, qdisc); 18695dc1929SEric Dumazet if (old) 18795dc1929SEric Dumazet qdisc_destroy(old); 18895dc1929SEric Dumazet if (ntx < dev->real_num_tx_queues) 18949b49971SJiri Kosina qdisc_hash_add(qdisc, false); 190b8970f0bSJohn Fastabend } 191b8970f0bSJohn Fastabend kfree(priv->qdiscs); 192b8970f0bSJohn Fastabend priv->qdiscs = NULL; 193b8970f0bSJohn Fastabend } 194b8970f0bSJohn Fastabend 195b8970f0bSJohn Fastabend static struct netdev_queue *mqprio_queue_get(struct Qdisc *sch, 196b8970f0bSJohn Fastabend unsigned long cl) 197b8970f0bSJohn Fastabend { 198b8970f0bSJohn Fastabend struct net_device *dev = qdisc_dev(sch); 199b8970f0bSJohn Fastabend unsigned long ntx = cl - 1 - netdev_get_num_tc(dev); 200b8970f0bSJohn Fastabend 201b8970f0bSJohn Fastabend if (ntx >= dev->num_tx_queues) 202b8970f0bSJohn Fastabend return NULL; 203b8970f0bSJohn Fastabend return netdev_get_tx_queue(dev, ntx); 204b8970f0bSJohn Fastabend } 205b8970f0bSJohn Fastabend 206b8970f0bSJohn Fastabend static int mqprio_graft(struct Qdisc *sch, unsigned long cl, struct Qdisc *new, 207b8970f0bSJohn Fastabend struct Qdisc **old) 208b8970f0bSJohn Fastabend { 209b8970f0bSJohn Fastabend struct net_device *dev = qdisc_dev(sch); 210b8970f0bSJohn Fastabend struct netdev_queue *dev_queue = mqprio_queue_get(sch, cl); 211b8970f0bSJohn Fastabend 212b8970f0bSJohn Fastabend if (!dev_queue) 213b8970f0bSJohn Fastabend return -EINVAL; 214b8970f0bSJohn Fastabend 215b8970f0bSJohn Fastabend if (dev->flags & IFF_UP) 216b8970f0bSJohn Fastabend dev_deactivate(dev); 217b8970f0bSJohn Fastabend 218b8970f0bSJohn Fastabend *old = dev_graft_qdisc(dev_queue, new); 219b8970f0bSJohn Fastabend 2201abbe139SEric Dumazet if (new) 2214eaf3b84SEric Dumazet new->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT; 2221abbe139SEric Dumazet 223b8970f0bSJohn Fastabend if (dev->flags & IFF_UP) 224b8970f0bSJohn Fastabend dev_activate(dev); 225b8970f0bSJohn Fastabend 226b8970f0bSJohn Fastabend return 0; 227b8970f0bSJohn Fastabend } 228b8970f0bSJohn Fastabend 229b8970f0bSJohn Fastabend static int mqprio_dump(struct Qdisc *sch, struct sk_buff *skb) 230b8970f0bSJohn Fastabend { 231b8970f0bSJohn Fastabend struct net_device *dev = qdisc_dev(sch); 232b8970f0bSJohn Fastabend struct mqprio_sched *priv = qdisc_priv(sch); 233b8970f0bSJohn Fastabend unsigned char *b = skb_tail_pointer(skb); 234144ce879SEric Dumazet struct tc_mqprio_qopt opt = { 0 }; 235b8970f0bSJohn Fastabend struct Qdisc *qdisc; 236b8970f0bSJohn Fastabend unsigned int i; 237b8970f0bSJohn Fastabend 238b8970f0bSJohn Fastabend sch->q.qlen = 0; 239b8970f0bSJohn Fastabend memset(&sch->bstats, 0, sizeof(sch->bstats)); 240b8970f0bSJohn Fastabend memset(&sch->qstats, 0, sizeof(sch->qstats)); 241b8970f0bSJohn Fastabend 242b8970f0bSJohn Fastabend for (i = 0; i < dev->num_tx_queues; i++) { 24346e5da40SJohn Fastabend qdisc = rtnl_dereference(netdev_get_tx_queue(dev, i)->qdisc); 244b8970f0bSJohn Fastabend spin_lock_bh(qdisc_lock(qdisc)); 245b8970f0bSJohn Fastabend sch->q.qlen += qdisc->q.qlen; 246b8970f0bSJohn Fastabend sch->bstats.bytes += qdisc->bstats.bytes; 247b8970f0bSJohn Fastabend sch->bstats.packets += qdisc->bstats.packets; 248b8970f0bSJohn Fastabend sch->qstats.backlog += qdisc->qstats.backlog; 249b8970f0bSJohn Fastabend sch->qstats.drops += qdisc->qstats.drops; 250b8970f0bSJohn Fastabend sch->qstats.requeues += qdisc->qstats.requeues; 251b8970f0bSJohn Fastabend sch->qstats.overlimits += qdisc->qstats.overlimits; 252b8970f0bSJohn Fastabend spin_unlock_bh(qdisc_lock(qdisc)); 253b8970f0bSJohn Fastabend } 254b8970f0bSJohn Fastabend 255b8970f0bSJohn Fastabend opt.num_tc = netdev_get_num_tc(dev); 256b8970f0bSJohn Fastabend memcpy(opt.prio_tc_map, dev->prio_tc_map, sizeof(opt.prio_tc_map)); 2572026fecfSAlexander Duyck opt.hw = priv->hw_offload; 258b8970f0bSJohn Fastabend 259b8970f0bSJohn Fastabend for (i = 0; i < netdev_get_num_tc(dev); i++) { 260b8970f0bSJohn Fastabend opt.count[i] = dev->tc_to_txq[i].count; 261b8970f0bSJohn Fastabend opt.offset[i] = dev->tc_to_txq[i].offset; 262b8970f0bSJohn Fastabend } 263b8970f0bSJohn Fastabend 2641b34ec43SDavid S. Miller if (nla_put(skb, TCA_OPTIONS, sizeof(opt), &opt)) 2651b34ec43SDavid S. Miller goto nla_put_failure; 266b8970f0bSJohn Fastabend 267b8970f0bSJohn Fastabend return skb->len; 268b8970f0bSJohn Fastabend nla_put_failure: 269b8970f0bSJohn Fastabend nlmsg_trim(skb, b); 270b8970f0bSJohn Fastabend return -1; 271b8970f0bSJohn Fastabend } 272b8970f0bSJohn Fastabend 273b8970f0bSJohn Fastabend static struct Qdisc *mqprio_leaf(struct Qdisc *sch, unsigned long cl) 274b8970f0bSJohn Fastabend { 275b8970f0bSJohn Fastabend struct netdev_queue *dev_queue = mqprio_queue_get(sch, cl); 276b8970f0bSJohn Fastabend 277b8970f0bSJohn Fastabend if (!dev_queue) 278b8970f0bSJohn Fastabend return NULL; 279b8970f0bSJohn Fastabend 280b8970f0bSJohn Fastabend return dev_queue->qdisc_sleeping; 281b8970f0bSJohn Fastabend } 282b8970f0bSJohn Fastabend 283b8970f0bSJohn Fastabend static unsigned long mqprio_get(struct Qdisc *sch, u32 classid) 284b8970f0bSJohn Fastabend { 285b8970f0bSJohn Fastabend struct net_device *dev = qdisc_dev(sch); 286b8970f0bSJohn Fastabend unsigned int ntx = TC_H_MIN(classid); 287b8970f0bSJohn Fastabend 288b8970f0bSJohn Fastabend if (ntx > dev->num_tx_queues + netdev_get_num_tc(dev)) 289b8970f0bSJohn Fastabend return 0; 290b8970f0bSJohn Fastabend return ntx; 291b8970f0bSJohn Fastabend } 292b8970f0bSJohn Fastabend 293b8970f0bSJohn Fastabend static void mqprio_put(struct Qdisc *sch, unsigned long cl) 294b8970f0bSJohn Fastabend { 295b8970f0bSJohn Fastabend } 296b8970f0bSJohn Fastabend 297b8970f0bSJohn Fastabend static int mqprio_dump_class(struct Qdisc *sch, unsigned long cl, 298b8970f0bSJohn Fastabend struct sk_buff *skb, struct tcmsg *tcm) 299b8970f0bSJohn Fastabend { 300b8970f0bSJohn Fastabend struct net_device *dev = qdisc_dev(sch); 301b8970f0bSJohn Fastabend 302b8970f0bSJohn Fastabend if (cl <= netdev_get_num_tc(dev)) { 303b8970f0bSJohn Fastabend tcm->tcm_parent = TC_H_ROOT; 304b8970f0bSJohn Fastabend tcm->tcm_info = 0; 305b8970f0bSJohn Fastabend } else { 306b8970f0bSJohn Fastabend int i; 307b8970f0bSJohn Fastabend struct netdev_queue *dev_queue; 308b8970f0bSJohn Fastabend 309b8970f0bSJohn Fastabend dev_queue = mqprio_queue_get(sch, cl); 310b8970f0bSJohn Fastabend tcm->tcm_parent = 0; 311b8970f0bSJohn Fastabend for (i = 0; i < netdev_get_num_tc(dev); i++) { 312b8970f0bSJohn Fastabend struct netdev_tc_txq tc = dev->tc_to_txq[i]; 313b8970f0bSJohn Fastabend int q_idx = cl - netdev_get_num_tc(dev); 314b8970f0bSJohn Fastabend 315b8970f0bSJohn Fastabend if (q_idx > tc.offset && 316b8970f0bSJohn Fastabend q_idx <= tc.offset + tc.count) { 317b8970f0bSJohn Fastabend tcm->tcm_parent = 318b8970f0bSJohn Fastabend TC_H_MAKE(TC_H_MAJ(sch->handle), 319b8970f0bSJohn Fastabend TC_H_MIN(i + 1)); 320b8970f0bSJohn Fastabend break; 321b8970f0bSJohn Fastabend } 322b8970f0bSJohn Fastabend } 323b8970f0bSJohn Fastabend tcm->tcm_info = dev_queue->qdisc_sleeping->handle; 324b8970f0bSJohn Fastabend } 325b8970f0bSJohn Fastabend tcm->tcm_handle |= TC_H_MIN(cl); 326b8970f0bSJohn Fastabend return 0; 327b8970f0bSJohn Fastabend } 328b8970f0bSJohn Fastabend 329b8970f0bSJohn Fastabend static int mqprio_dump_class_stats(struct Qdisc *sch, unsigned long cl, 330b8970f0bSJohn Fastabend struct gnet_dump *d) 331ea18fd95Sstephen hemminger __releases(d->lock) 332ea18fd95Sstephen hemminger __acquires(d->lock) 333b8970f0bSJohn Fastabend { 334b8970f0bSJohn Fastabend struct net_device *dev = qdisc_dev(sch); 335b8970f0bSJohn Fastabend 336b8970f0bSJohn Fastabend if (cl <= netdev_get_num_tc(dev)) { 337b8970f0bSJohn Fastabend int i; 33864015853SJohn Fastabend __u32 qlen = 0; 339b8970f0bSJohn Fastabend struct Qdisc *qdisc; 340b8970f0bSJohn Fastabend struct gnet_stats_queue qstats = {0}; 341b8970f0bSJohn Fastabend struct gnet_stats_basic_packed bstats = {0}; 342b8970f0bSJohn Fastabend struct netdev_tc_txq tc = dev->tc_to_txq[cl - 1]; 343b8970f0bSJohn Fastabend 344b8970f0bSJohn Fastabend /* Drop lock here it will be reclaimed before touching 345b8970f0bSJohn Fastabend * statistics this is required because the d->lock we 346b8970f0bSJohn Fastabend * hold here is the look on dev_queue->qdisc_sleeping 347b8970f0bSJohn Fastabend * also acquired below. 348b8970f0bSJohn Fastabend */ 349edb09eb1SEric Dumazet if (d->lock) 350b8970f0bSJohn Fastabend spin_unlock_bh(d->lock); 351b8970f0bSJohn Fastabend 352b8970f0bSJohn Fastabend for (i = tc.offset; i < tc.offset + tc.count; i++) { 35346e5da40SJohn Fastabend struct netdev_queue *q = netdev_get_tx_queue(dev, i); 35446e5da40SJohn Fastabend 35546e5da40SJohn Fastabend qdisc = rtnl_dereference(q->qdisc); 356b8970f0bSJohn Fastabend spin_lock_bh(qdisc_lock(qdisc)); 35764015853SJohn Fastabend qlen += qdisc->q.qlen; 358b8970f0bSJohn Fastabend bstats.bytes += qdisc->bstats.bytes; 359b8970f0bSJohn Fastabend bstats.packets += qdisc->bstats.packets; 360b8970f0bSJohn Fastabend qstats.backlog += qdisc->qstats.backlog; 361b8970f0bSJohn Fastabend qstats.drops += qdisc->qstats.drops; 362b8970f0bSJohn Fastabend qstats.requeues += qdisc->qstats.requeues; 363b8970f0bSJohn Fastabend qstats.overlimits += qdisc->qstats.overlimits; 364b8970f0bSJohn Fastabend spin_unlock_bh(qdisc_lock(qdisc)); 365b8970f0bSJohn Fastabend } 366b8970f0bSJohn Fastabend /* Reclaim root sleeping lock before completing stats */ 367edb09eb1SEric Dumazet if (d->lock) 368b8970f0bSJohn Fastabend spin_lock_bh(d->lock); 369edb09eb1SEric Dumazet if (gnet_stats_copy_basic(NULL, d, NULL, &bstats) < 0 || 370b0ab6f92SJohn Fastabend gnet_stats_copy_queue(d, NULL, &qstats, qlen) < 0) 371b8970f0bSJohn Fastabend return -1; 372b8970f0bSJohn Fastabend } else { 373b8970f0bSJohn Fastabend struct netdev_queue *dev_queue = mqprio_queue_get(sch, cl); 374b8970f0bSJohn Fastabend 375b8970f0bSJohn Fastabend sch = dev_queue->qdisc_sleeping; 376edb09eb1SEric Dumazet if (gnet_stats_copy_basic(qdisc_root_sleeping_running(sch), 377edb09eb1SEric Dumazet d, NULL, &sch->bstats) < 0 || 378b0ab6f92SJohn Fastabend gnet_stats_copy_queue(d, NULL, 379b0ab6f92SJohn Fastabend &sch->qstats, sch->q.qlen) < 0) 380b8970f0bSJohn Fastabend return -1; 381b8970f0bSJohn Fastabend } 382b8970f0bSJohn Fastabend return 0; 383b8970f0bSJohn Fastabend } 384b8970f0bSJohn Fastabend 385b8970f0bSJohn Fastabend static void mqprio_walk(struct Qdisc *sch, struct qdisc_walker *arg) 386b8970f0bSJohn Fastabend { 387b8970f0bSJohn Fastabend struct net_device *dev = qdisc_dev(sch); 388b8970f0bSJohn Fastabend unsigned long ntx; 389b8970f0bSJohn Fastabend 390b8970f0bSJohn Fastabend if (arg->stop) 391b8970f0bSJohn Fastabend return; 392b8970f0bSJohn Fastabend 393b8970f0bSJohn Fastabend /* Walk hierarchy with a virtual class per tc */ 394b8970f0bSJohn Fastabend arg->count = arg->skip; 395b8970f0bSJohn Fastabend for (ntx = arg->skip; 396b8970f0bSJohn Fastabend ntx < dev->num_tx_queues + netdev_get_num_tc(dev); 397b8970f0bSJohn Fastabend ntx++) { 398b8970f0bSJohn Fastabend if (arg->fn(sch, ntx + 1, arg) < 0) { 399b8970f0bSJohn Fastabend arg->stop = 1; 400b8970f0bSJohn Fastabend break; 401b8970f0bSJohn Fastabend } 402b8970f0bSJohn Fastabend arg->count++; 403b8970f0bSJohn Fastabend } 404b8970f0bSJohn Fastabend } 405b8970f0bSJohn Fastabend 406b8970f0bSJohn Fastabend static const struct Qdisc_class_ops mqprio_class_ops = { 407b8970f0bSJohn Fastabend .graft = mqprio_graft, 408b8970f0bSJohn Fastabend .leaf = mqprio_leaf, 409b8970f0bSJohn Fastabend .get = mqprio_get, 410b8970f0bSJohn Fastabend .put = mqprio_put, 411b8970f0bSJohn Fastabend .walk = mqprio_walk, 412b8970f0bSJohn Fastabend .dump = mqprio_dump_class, 413b8970f0bSJohn Fastabend .dump_stats = mqprio_dump_class_stats, 414b8970f0bSJohn Fastabend }; 415b8970f0bSJohn Fastabend 416ea18fd95Sstephen hemminger static struct Qdisc_ops mqprio_qdisc_ops __read_mostly = { 417b8970f0bSJohn Fastabend .cl_ops = &mqprio_class_ops, 418b8970f0bSJohn Fastabend .id = "mqprio", 419b8970f0bSJohn Fastabend .priv_size = sizeof(struct mqprio_sched), 420b8970f0bSJohn Fastabend .init = mqprio_init, 421b8970f0bSJohn Fastabend .destroy = mqprio_destroy, 422b8970f0bSJohn Fastabend .attach = mqprio_attach, 423b8970f0bSJohn Fastabend .dump = mqprio_dump, 424b8970f0bSJohn Fastabend .owner = THIS_MODULE, 425b8970f0bSJohn Fastabend }; 426b8970f0bSJohn Fastabend 427b8970f0bSJohn Fastabend static int __init mqprio_module_init(void) 428b8970f0bSJohn Fastabend { 429b8970f0bSJohn Fastabend return register_qdisc(&mqprio_qdisc_ops); 430b8970f0bSJohn Fastabend } 431b8970f0bSJohn Fastabend 432b8970f0bSJohn Fastabend static void __exit mqprio_module_exit(void) 433b8970f0bSJohn Fastabend { 434b8970f0bSJohn Fastabend unregister_qdisc(&mqprio_qdisc_ops); 435b8970f0bSJohn Fastabend } 436b8970f0bSJohn Fastabend 437b8970f0bSJohn Fastabend module_init(mqprio_module_init); 438b8970f0bSJohn Fastabend module_exit(mqprio_module_exit); 439b8970f0bSJohn Fastabend 440b8970f0bSJohn Fastabend MODULE_LICENSE("GPL"); 441