Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * net/sched/sch_mq.c       Classful multiqueue dummy scheduler
0004  *
0005  * Copyright (c) 2009 Patrick McHardy <kaber@trash.net>
0006  */
0007 
0008 #include <linux/types.h>
0009 #include <linux/slab.h>
0010 #include <linux/kernel.h>
0011 #include <linux/export.h>
0012 #include <linux/string.h>
0013 #include <linux/errno.h>
0014 #include <linux/skbuff.h>
0015 #include <net/netlink.h>
0016 #include <net/pkt_cls.h>
0017 #include <net/pkt_sched.h>
0018 #include <net/sch_generic.h>
0019 
0020 struct mq_sched {
0021     struct Qdisc        **qdiscs;
0022 };
0023 
0024 static int mq_offload(struct Qdisc *sch, enum tc_mq_command cmd)
0025 {
0026     struct net_device *dev = qdisc_dev(sch);
0027     struct tc_mq_qopt_offload opt = {
0028         .command = cmd,
0029         .handle = sch->handle,
0030     };
0031 
0032     if (!tc_can_offload(dev) || !dev->netdev_ops->ndo_setup_tc)
0033         return -EOPNOTSUPP;
0034 
0035     return dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_QDISC_MQ, &opt);
0036 }
0037 
0038 static int mq_offload_stats(struct Qdisc *sch)
0039 {
0040     struct tc_mq_qopt_offload opt = {
0041         .command = TC_MQ_STATS,
0042         .handle = sch->handle,
0043         .stats = {
0044             .bstats = &sch->bstats,
0045             .qstats = &sch->qstats,
0046         },
0047     };
0048 
0049     return qdisc_offload_dump_helper(sch, TC_SETUP_QDISC_MQ, &opt);
0050 }
0051 
0052 static void mq_destroy(struct Qdisc *sch)
0053 {
0054     struct net_device *dev = qdisc_dev(sch);
0055     struct mq_sched *priv = qdisc_priv(sch);
0056     unsigned int ntx;
0057 
0058     mq_offload(sch, TC_MQ_DESTROY);
0059 
0060     if (!priv->qdiscs)
0061         return;
0062     for (ntx = 0; ntx < dev->num_tx_queues && priv->qdiscs[ntx]; ntx++)
0063         qdisc_put(priv->qdiscs[ntx]);
0064     kfree(priv->qdiscs);
0065 }
0066 
0067 static int mq_init(struct Qdisc *sch, struct nlattr *opt,
0068            struct netlink_ext_ack *extack)
0069 {
0070     struct net_device *dev = qdisc_dev(sch);
0071     struct mq_sched *priv = qdisc_priv(sch);
0072     struct netdev_queue *dev_queue;
0073     struct Qdisc *qdisc;
0074     unsigned int ntx;
0075 
0076     if (sch->parent != TC_H_ROOT)
0077         return -EOPNOTSUPP;
0078 
0079     if (!netif_is_multiqueue(dev))
0080         return -EOPNOTSUPP;
0081 
0082     /* pre-allocate qdiscs, attachment can't fail */
0083     priv->qdiscs = kcalloc(dev->num_tx_queues, sizeof(priv->qdiscs[0]),
0084                    GFP_KERNEL);
0085     if (!priv->qdiscs)
0086         return -ENOMEM;
0087 
0088     for (ntx = 0; ntx < dev->num_tx_queues; ntx++) {
0089         dev_queue = netdev_get_tx_queue(dev, ntx);
0090         qdisc = qdisc_create_dflt(dev_queue, get_default_qdisc_ops(dev, ntx),
0091                       TC_H_MAKE(TC_H_MAJ(sch->handle),
0092                             TC_H_MIN(ntx + 1)),
0093                       extack);
0094         if (!qdisc)
0095             return -ENOMEM;
0096         priv->qdiscs[ntx] = qdisc;
0097         qdisc->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
0098     }
0099 
0100     sch->flags |= TCQ_F_MQROOT;
0101 
0102     mq_offload(sch, TC_MQ_CREATE);
0103     return 0;
0104 }
0105 
0106 static void mq_attach(struct Qdisc *sch)
0107 {
0108     struct net_device *dev = qdisc_dev(sch);
0109     struct mq_sched *priv = qdisc_priv(sch);
0110     struct Qdisc *qdisc, *old;
0111     unsigned int ntx;
0112 
0113     for (ntx = 0; ntx < dev->num_tx_queues; ntx++) {
0114         qdisc = priv->qdiscs[ntx];
0115         old = dev_graft_qdisc(qdisc->dev_queue, qdisc);
0116         if (old)
0117             qdisc_put(old);
0118 #ifdef CONFIG_NET_SCHED
0119         if (ntx < dev->real_num_tx_queues)
0120             qdisc_hash_add(qdisc, false);
0121 #endif
0122 
0123     }
0124     kfree(priv->qdiscs);
0125     priv->qdiscs = NULL;
0126 }
0127 
0128 static int mq_dump(struct Qdisc *sch, struct sk_buff *skb)
0129 {
0130     struct net_device *dev = qdisc_dev(sch);
0131     struct Qdisc *qdisc;
0132     unsigned int ntx;
0133 
0134     sch->q.qlen = 0;
0135     gnet_stats_basic_sync_init(&sch->bstats);
0136     memset(&sch->qstats, 0, sizeof(sch->qstats));
0137 
0138     /* MQ supports lockless qdiscs. However, statistics accounting needs
0139      * to account for all, none, or a mix of locked and unlocked child
0140      * qdiscs. Percpu stats are added to counters in-band and locking
0141      * qdisc totals are added at end.
0142      */
0143     for (ntx = 0; ntx < dev->num_tx_queues; ntx++) {
0144         qdisc = netdev_get_tx_queue(dev, ntx)->qdisc_sleeping;
0145         spin_lock_bh(qdisc_lock(qdisc));
0146 
0147         gnet_stats_add_basic(&sch->bstats, qdisc->cpu_bstats,
0148                      &qdisc->bstats, false);
0149         gnet_stats_add_queue(&sch->qstats, qdisc->cpu_qstats,
0150                      &qdisc->qstats);
0151         sch->q.qlen += qdisc_qlen(qdisc);
0152 
0153         spin_unlock_bh(qdisc_lock(qdisc));
0154     }
0155 
0156     return mq_offload_stats(sch);
0157 }
0158 
0159 static struct netdev_queue *mq_queue_get(struct Qdisc *sch, unsigned long cl)
0160 {
0161     struct net_device *dev = qdisc_dev(sch);
0162     unsigned long ntx = cl - 1;
0163 
0164     if (ntx >= dev->num_tx_queues)
0165         return NULL;
0166     return netdev_get_tx_queue(dev, ntx);
0167 }
0168 
0169 static struct netdev_queue *mq_select_queue(struct Qdisc *sch,
0170                         struct tcmsg *tcm)
0171 {
0172     return mq_queue_get(sch, TC_H_MIN(tcm->tcm_parent));
0173 }
0174 
0175 static int mq_graft(struct Qdisc *sch, unsigned long cl, struct Qdisc *new,
0176             struct Qdisc **old, struct netlink_ext_ack *extack)
0177 {
0178     struct netdev_queue *dev_queue = mq_queue_get(sch, cl);
0179     struct tc_mq_qopt_offload graft_offload;
0180     struct net_device *dev = qdisc_dev(sch);
0181 
0182     if (dev->flags & IFF_UP)
0183         dev_deactivate(dev);
0184 
0185     *old = dev_graft_qdisc(dev_queue, new);
0186     if (new)
0187         new->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
0188     if (dev->flags & IFF_UP)
0189         dev_activate(dev);
0190 
0191     graft_offload.handle = sch->handle;
0192     graft_offload.graft_params.queue = cl - 1;
0193     graft_offload.graft_params.child_handle = new ? new->handle : 0;
0194     graft_offload.command = TC_MQ_GRAFT;
0195 
0196     qdisc_offload_graft_helper(qdisc_dev(sch), sch, new, *old,
0197                    TC_SETUP_QDISC_MQ, &graft_offload, extack);
0198     return 0;
0199 }
0200 
0201 static struct Qdisc *mq_leaf(struct Qdisc *sch, unsigned long cl)
0202 {
0203     struct netdev_queue *dev_queue = mq_queue_get(sch, cl);
0204 
0205     return dev_queue->qdisc_sleeping;
0206 }
0207 
0208 static unsigned long mq_find(struct Qdisc *sch, u32 classid)
0209 {
0210     unsigned int ntx = TC_H_MIN(classid);
0211 
0212     if (!mq_queue_get(sch, ntx))
0213         return 0;
0214     return ntx;
0215 }
0216 
0217 static int mq_dump_class(struct Qdisc *sch, unsigned long cl,
0218              struct sk_buff *skb, struct tcmsg *tcm)
0219 {
0220     struct netdev_queue *dev_queue = mq_queue_get(sch, cl);
0221 
0222     tcm->tcm_parent = TC_H_ROOT;
0223     tcm->tcm_handle |= TC_H_MIN(cl);
0224     tcm->tcm_info = dev_queue->qdisc_sleeping->handle;
0225     return 0;
0226 }
0227 
0228 static int mq_dump_class_stats(struct Qdisc *sch, unsigned long cl,
0229                    struct gnet_dump *d)
0230 {
0231     struct netdev_queue *dev_queue = mq_queue_get(sch, cl);
0232 
0233     sch = dev_queue->qdisc_sleeping;
0234     if (gnet_stats_copy_basic(d, sch->cpu_bstats, &sch->bstats, true) < 0 ||
0235         qdisc_qstats_copy(d, sch) < 0)
0236         return -1;
0237     return 0;
0238 }
0239 
0240 static void mq_walk(struct Qdisc *sch, struct qdisc_walker *arg)
0241 {
0242     struct net_device *dev = qdisc_dev(sch);
0243     unsigned int ntx;
0244 
0245     if (arg->stop)
0246         return;
0247 
0248     arg->count = arg->skip;
0249     for (ntx = arg->skip; ntx < dev->num_tx_queues; ntx++) {
0250         if (arg->fn(sch, ntx + 1, arg) < 0) {
0251             arg->stop = 1;
0252             break;
0253         }
0254         arg->count++;
0255     }
0256 }
0257 
0258 static const struct Qdisc_class_ops mq_class_ops = {
0259     .select_queue   = mq_select_queue,
0260     .graft      = mq_graft,
0261     .leaf       = mq_leaf,
0262     .find       = mq_find,
0263     .walk       = mq_walk,
0264     .dump       = mq_dump_class,
0265     .dump_stats = mq_dump_class_stats,
0266 };
0267 
0268 struct Qdisc_ops mq_qdisc_ops __read_mostly = {
0269     .cl_ops     = &mq_class_ops,
0270     .id     = "mq",
0271     .priv_size  = sizeof(struct mq_sched),
0272     .init       = mq_init,
0273     .destroy    = mq_destroy,
0274     .attach     = mq_attach,
0275     .change_real_num_tx = mq_change_real_num_tx,
0276     .dump       = mq_dump,
0277     .owner      = THIS_MODULE,
0278 };