Hi All:
This series implements vendor stats in vdpasim_net. Please review.
Thanks
Changes since V1:
- typo fixes
- move the duplicated get_vendor_vq_stats() in
vdpasim_batch_config_ops to vdpa_sim_config_ops
- use -EOPNOTSUPP instead of -EINVAL in vdpasim_get_vq_stats
- introdce a dedicated variable to record the successful cvq request
and track the number of requests correctly
Jason Wang (4):
vdpa_sim: switch to use __vdpa_alloc_device()
vdpasim: customize allocation size
vdpa_sim: support vendor statistics
vdpa_sim_net: vendor satistics
drivers/vdpa/vdpa_sim/vdpa_sim.c | 33 +++-
drivers/vdpa/vdpa_sim/vdpa_sim.h | 4 +
drivers/vdpa/vdpa_sim/vdpa_sim_blk.c | 1 +
drivers/vdpa/vdpa_sim/vdpa_sim_net.c | 221 ++++++++++++++++++++++++++-
4 files changed, 248 insertions(+), 11 deletions(-)
--
2.25.1
This patch adds support for basic vendor stats that include counters
for tx, rx and cvq.
Signed-off-by: Jason Wang <[email protected]>
---
drivers/vdpa/vdpa_sim/vdpa_sim_net.c | 222 ++++++++++++++++++++++++++-
1 file changed, 216 insertions(+), 6 deletions(-)
diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim_net.c b/drivers/vdpa/vdpa_sim/vdpa_sim_net.c
index 20cd5cdff919..e0fbb739fc4d 100644
--- a/drivers/vdpa/vdpa_sim/vdpa_sim_net.c
+++ b/drivers/vdpa/vdpa_sim/vdpa_sim_net.c
@@ -15,6 +15,7 @@
#include <linux/etherdevice.h>
#include <linux/vringh.h>
#include <linux/vdpa.h>
+#include <net/netlink.h>
#include <uapi/linux/virtio_net.h>
#include <uapi/linux/vdpa.h>
@@ -36,6 +37,34 @@
#define VDPASIM_NET_AS_NUM 2
#define VDPASIM_NET_GROUP_NUM 2
+struct vdpasim_dataq_stats {
+ struct u64_stats_sync syncp;
+ u64 pkts;
+ u64 bytes;
+ u64 drops;
+ u64 errors;
+ u64 overruns;
+};
+
+struct vdpasim_cq_stats {
+ struct u64_stats_sync syncp;
+ u64 requests;
+ u64 successes;
+ u64 errors;
+};
+
+struct vdpasim_net{
+ struct vdpasim vdpasim;
+ struct vdpasim_dataq_stats tx_stats;
+ struct vdpasim_dataq_stats rx_stats;
+ struct vdpasim_cq_stats cq_stats;
+};
+
+static struct vdpasim_net *sim_to_net(struct vdpasim *vdpasim)
+{
+ return container_of(vdpasim, struct vdpasim_net, vdpasim);
+}
+
static void vdpasim_net_complete(struct vdpasim_virtqueue *vq, size_t len)
{
/* Make sure data is wrote before advancing index */
@@ -93,9 +122,14 @@ static virtio_net_ctrl_ack vdpasim_handle_ctrl_mac(struct vdpasim *vdpasim,
static void vdpasim_handle_cvq(struct vdpasim *vdpasim)
{
struct vdpasim_virtqueue *cvq = &vdpasim->vqs[2];
+
+
+
+ struct vdpasim_net *net = sim_to_net(vdpasim);
virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
struct virtio_net_ctrl_hdr ctrl;
size_t read, write;
+ u64 requests = 0, errors = 0, successes = 0;
int err;
if (!(vdpasim->features & (1ULL << VIRTIO_NET_F_CTRL_VQ)))
@@ -111,10 +145,13 @@ static void vdpasim_handle_cvq(struct vdpasim *vdpasim)
if (err <= 0)
break;
+ ++requests;
read = vringh_iov_pull_iotlb(&cvq->vring, &cvq->in_iov, &ctrl,
sizeof(ctrl));
- if (read != sizeof(ctrl))
+ if (read != sizeof(ctrl)) {
+ ++errors;
break;
+ }
switch (ctrl.class) {
case VIRTIO_NET_CTRL_MAC:
@@ -124,6 +161,11 @@ static void vdpasim_handle_cvq(struct vdpasim *vdpasim)
break;
}
+ if (status == VIRTIO_NET_OK)
+ ++successes;
+ else
+ ++errors;
+
/* Make sure data is wrote before advancing index */
smp_wmb();
@@ -141,6 +183,12 @@ static void vdpasim_handle_cvq(struct vdpasim *vdpasim)
cvq->cb(cvq->private);
local_bh_enable();
}
+
+ u64_stats_update_begin(&net->cq_stats.syncp);
+ net->cq_stats.requests += requests;
+ net->cq_stats.errors += errors;
+ net->cq_stats.successes += successes;
+ u64_stats_update_end(&net->cq_stats.syncp);
}
static void vdpasim_net_work(struct work_struct *work)
@@ -148,8 +196,10 @@ static void vdpasim_net_work(struct work_struct *work)
struct vdpasim *vdpasim = container_of(work, struct vdpasim, work);
struct vdpasim_virtqueue *txq = &vdpasim->vqs[1];
struct vdpasim_virtqueue *rxq = &vdpasim->vqs[0];
+ struct vdpasim_net *net = sim_to_net(vdpasim);
ssize_t read, write;
- int pkts = 0;
+ u64 tx_pkts = 0, rx_pkts = 0, tx_bytes = 0, rx_bytes = 0;
+ u64 rx_drops = 0, rx_overruns = 0, rx_errors = 0, tx_errors = 0;
int err;
spin_lock(&vdpasim->lock);
@@ -168,14 +218,21 @@ static void vdpasim_net_work(struct work_struct *work)
while (true) {
err = vringh_getdesc_iotlb(&txq->vring, &txq->out_iov, NULL,
&txq->head, GFP_ATOMIC);
- if (err <= 0)
+ if (err <= 0) {
+ if (err)
+ ++tx_errors;
break;
+ }
+ ++tx_pkts;
read = vringh_iov_pull_iotlb(&txq->vring, &txq->out_iov,
vdpasim->buffer,
PAGE_SIZE);
+ tx_bytes += read;
+
if (!receive_filter(vdpasim, read)) {
+ ++rx_drops;
vdpasim_net_complete(txq, 0);
continue;
}
@@ -183,19 +240,25 @@ static void vdpasim_net_work(struct work_struct *work)
err = vringh_getdesc_iotlb(&rxq->vring, NULL, &rxq->in_iov,
&rxq->head, GFP_ATOMIC);
if (err <= 0) {
+ ++rx_overruns;
vdpasim_net_complete(txq, 0);
break;
}
write = vringh_iov_push_iotlb(&rxq->vring, &rxq->in_iov,
vdpasim->buffer, read);
- if (write <= 0)
+ if (write <= 0) {
+ ++rx_errors;
break;
+ }
+
+ ++rx_pkts;
+ rx_bytes += write;
vdpasim_net_complete(txq, 0);
vdpasim_net_complete(rxq, write);
- if (++pkts > 4) {
+ if (tx_pkts > 4) {
schedule_work(&vdpasim->work);
goto out;
}
@@ -203,6 +266,145 @@ static void vdpasim_net_work(struct work_struct *work)
out:
spin_unlock(&vdpasim->lock);
+
+ u64_stats_update_begin(&net->tx_stats.syncp);
+ net->tx_stats.pkts += tx_pkts;
+ net->tx_stats.bytes += tx_bytes;
+ net->tx_stats.errors += tx_errors;
+ u64_stats_update_end(&net->tx_stats.syncp);
+
+ u64_stats_update_begin(&net->rx_stats.syncp);
+ net->rx_stats.pkts += rx_pkts;
+ net->rx_stats.bytes += rx_bytes;
+ net->rx_stats.drops += rx_drops;
+ net->rx_stats.errors += rx_errors;
+ net->rx_stats.overruns += rx_overruns;
+ u64_stats_update_end(&net->rx_stats.syncp);
+}
+
+static int vdpasim_net_get_stats(struct vdpasim *vdpasim, u16 idx,
+ struct sk_buff *msg,
+ struct netlink_ext_ack *extack)
+{
+ struct vdpasim_net *net = sim_to_net(vdpasim);
+ u64 rx_pkts, rx_bytes, rx_errors, rx_overruns, rx_drops;
+ u64 tx_pkts, tx_bytes, tx_errors, tx_drops;
+ u64 cq_requests, cq_successes, cq_errors;
+ unsigned int start;
+ int err = -EMSGSIZE;
+
+ switch(idx) {
+ case 0:
+ do {
+ start = u64_stats_fetch_begin(&net->rx_stats.syncp);
+ rx_pkts = net->rx_stats.pkts;
+ rx_bytes = net->rx_stats.bytes;
+ rx_errors = net->rx_stats.errors;
+ rx_overruns = net->rx_stats.overruns;
+ rx_drops = net->rx_stats.drops;
+ } while (u64_stats_fetch_retry(&net->rx_stats.syncp, start));
+
+ if (nla_put_string(msg, VDPA_ATTR_DEV_VENDOR_ATTR_NAME,
+ "rx packets"))
+ break;
+ if (nla_put_u64_64bit(msg, VDPA_ATTR_DEV_VENDOR_ATTR_VALUE,
+ rx_pkts, VDPA_ATTR_PAD))
+ break;
+ if (nla_put_string(msg, VDPA_ATTR_DEV_VENDOR_ATTR_NAME,
+ "rx bytes"))
+ break;
+ if (nla_put_u64_64bit(msg, VDPA_ATTR_DEV_VENDOR_ATTR_VALUE,
+ rx_bytes, VDPA_ATTR_PAD))
+ break;
+ if (nla_put_string(msg, VDPA_ATTR_DEV_VENDOR_ATTR_NAME,
+ "rx errors"))
+ break;
+ if (nla_put_u64_64bit(msg, VDPA_ATTR_DEV_VENDOR_ATTR_VALUE,
+ rx_errors, VDPA_ATTR_PAD))
+ break;
+ if (nla_put_string(msg, VDPA_ATTR_DEV_VENDOR_ATTR_NAME,
+ "rx overrunss"))
+ break;
+ if (nla_put_u64_64bit(msg, VDPA_ATTR_DEV_VENDOR_ATTR_VALUE,
+ rx_overruns, VDPA_ATTR_PAD))
+ break;
+ if (nla_put_string(msg, VDPA_ATTR_DEV_VENDOR_ATTR_NAME,
+ "rx drops"))
+ break;
+ if (nla_put_u64_64bit(msg, VDPA_ATTR_DEV_VENDOR_ATTR_VALUE,
+ rx_drops, VDPA_ATTR_PAD))
+ break;
+ err = 0;
+ break;
+ case 1:
+ do {
+ start = u64_stats_fetch_begin(&net->tx_stats.syncp);
+ tx_pkts = net->tx_stats.pkts;
+ tx_bytes = net->tx_stats.bytes;
+ tx_errors = net->tx_stats.errors;
+ tx_drops = net->tx_stats.drops;
+ } while (u64_stats_fetch_retry(&net->tx_stats.syncp, start));
+
+ if (nla_put_string(msg, VDPA_ATTR_DEV_VENDOR_ATTR_NAME,
+ "tx packets"))
+ break;
+ if (nla_put_u64_64bit(msg, VDPA_ATTR_DEV_VENDOR_ATTR_VALUE,
+ tx_pkts, VDPA_ATTR_PAD))
+ break;
+ if (nla_put_string(msg, VDPA_ATTR_DEV_VENDOR_ATTR_NAME,
+ "tx bytes"))
+ break;
+ if (nla_put_u64_64bit(msg, VDPA_ATTR_DEV_VENDOR_ATTR_VALUE,
+ tx_bytes, VDPA_ATTR_PAD))
+ break;
+ if (nla_put_string(msg, VDPA_ATTR_DEV_VENDOR_ATTR_NAME,
+ "tx errors"))
+ break;
+ if (nla_put_u64_64bit(msg, VDPA_ATTR_DEV_VENDOR_ATTR_VALUE,
+ tx_errors, VDPA_ATTR_PAD))
+ break;
+ if (nla_put_string(msg, VDPA_ATTR_DEV_VENDOR_ATTR_NAME,
+ "tx drops"))
+ break;
+ if (nla_put_u64_64bit(msg, VDPA_ATTR_DEV_VENDOR_ATTR_VALUE,
+ tx_drops, VDPA_ATTR_PAD))
+ break;
+ err = 0;
+ break;
+ case 2:
+ do {
+ start = u64_stats_fetch_begin(&net->cq_stats.syncp);
+ cq_requests = net->cq_stats.requests;
+ cq_successes = net->cq_stats.successes;
+ cq_errors = net->cq_stats.errors;
+ } while (u64_stats_fetch_retry(&net->cq_stats.syncp, start));
+
+ if (nla_put_string(msg, VDPA_ATTR_DEV_VENDOR_ATTR_NAME,
+ "cvq requests"))
+ break;
+ if (nla_put_u64_64bit(msg, VDPA_ATTR_DEV_VENDOR_ATTR_VALUE,
+ cq_requests, VDPA_ATTR_PAD))
+ break;
+ if (nla_put_string(msg, VDPA_ATTR_DEV_VENDOR_ATTR_NAME,
+ "cvq successes"))
+ break;
+ if (nla_put_u64_64bit(msg, VDPA_ATTR_DEV_VENDOR_ATTR_VALUE,
+ cq_successes, VDPA_ATTR_PAD))
+ break;
+ if (nla_put_string(msg, VDPA_ATTR_DEV_VENDOR_ATTR_NAME,
+ "cvq errors"))
+ break;
+ if (nla_put_u64_64bit(msg, VDPA_ATTR_DEV_VENDOR_ATTR_VALUE,
+ cq_errors, VDPA_ATTR_PAD))
+ break;
+ err = 0;
+ break;
+ default:
+ err = -EINVAL;
+ break;
+ }
+
+ return err;
}
static void vdpasim_net_get_config(struct vdpasim *vdpasim, void *config)
@@ -239,6 +441,7 @@ static int vdpasim_net_dev_add(struct vdpa_mgmt_dev *mdev, const char *name,
const struct vdpa_dev_set_config *config)
{
struct vdpasim_dev_attr dev_attr = {};
+ struct vdpasim_net *net;
struct vdpasim *simdev;
int ret;
@@ -249,10 +452,11 @@ static int vdpasim_net_dev_add(struct vdpa_mgmt_dev *mdev, const char *name,
dev_attr.nvqs = VDPASIM_NET_VQ_NUM;
dev_attr.ngroups = VDPASIM_NET_GROUP_NUM;
dev_attr.nas = VDPASIM_NET_AS_NUM;
- dev_attr.alloc_size = sizeof(struct vdpasim);
+ dev_attr.alloc_size = sizeof(struct vdpasim_net);
dev_attr.config_size = sizeof(struct virtio_net_config);
dev_attr.get_config = vdpasim_net_get_config;
dev_attr.work_fn = vdpasim_net_work;
+ dev_attr.get_stats = vdpasim_net_get_stats;
dev_attr.buffer_size = PAGE_SIZE;
simdev = vdpasim_create(&dev_attr, config);
@@ -265,6 +469,12 @@ static int vdpasim_net_dev_add(struct vdpa_mgmt_dev *mdev, const char *name,
if (ret)
goto reg_err;
+ net = sim_to_net(simdev);
+
+ u64_stats_init(&net->tx_stats.syncp);
+ u64_stats_init(&net->rx_stats.syncp);
+ u64_stats_init(&net->cq_stats.syncp);
+
return 0;
reg_err:
--
2.25.1
Allow individual simulator to customize the allocation size.
Signed-off-by: Jason Wang <[email protected]>
---
drivers/vdpa/vdpa_sim/vdpa_sim.c | 8 ++++++--
drivers/vdpa/vdpa_sim/vdpa_sim.h | 1 +
drivers/vdpa/vdpa_sim/vdpa_sim_blk.c | 1 +
drivers/vdpa/vdpa_sim/vdpa_sim_net.c | 1 +
4 files changed, 9 insertions(+), 2 deletions(-)
diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.c b/drivers/vdpa/vdpa_sim/vdpa_sim.c
index 757afef86ba0..55aaa023a6e2 100644
--- a/drivers/vdpa/vdpa_sim/vdpa_sim.c
+++ b/drivers/vdpa/vdpa_sim/vdpa_sim.c
@@ -253,7 +253,10 @@ struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr,
struct vdpa_device *vdpa;
struct vdpasim *vdpasim;
struct device *dev;
- int i, ret = -ENOMEM;
+ int i, ret = -EINVAL;
+
+ if (!dev_attr->alloc_size)
+ goto err_alloc;
if (config->mask & BIT_ULL(VDPA_ATTR_DEV_FEATURES)) {
if (config->device_features &
@@ -268,9 +271,10 @@ struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr,
else
ops = &vdpasim_config_ops;
+ ret = -ENOMEM;
vdpa = __vdpa_alloc_device(NULL, ops,
dev_attr->ngroups, dev_attr->nas,
- sizeof(struct vdpasim),
+ dev_attr->alloc_size,
dev_attr->name, false);
if (IS_ERR(vdpa)) {
ret = PTR_ERR(vdpa);
diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.h b/drivers/vdpa/vdpa_sim/vdpa_sim.h
index 0e78737dcc16..51c070a543f1 100644
--- a/drivers/vdpa/vdpa_sim/vdpa_sim.h
+++ b/drivers/vdpa/vdpa_sim/vdpa_sim.h
@@ -37,6 +37,7 @@ struct vdpasim_dev_attr {
struct vdpa_mgmt_dev *mgmt_dev;
const char *name;
u64 supported_features;
+ size_t alloc_size;
size_t config_size;
size_t buffer_size;
int nvqs;
diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim_blk.c b/drivers/vdpa/vdpa_sim/vdpa_sim_blk.c
index c6db1a1baf76..4f7c35f59aa5 100644
--- a/drivers/vdpa/vdpa_sim/vdpa_sim_blk.c
+++ b/drivers/vdpa/vdpa_sim/vdpa_sim_blk.c
@@ -378,6 +378,7 @@ static int vdpasim_blk_dev_add(struct vdpa_mgmt_dev *mdev, const char *name,
dev_attr.nvqs = VDPASIM_BLK_VQ_NUM;
dev_attr.ngroups = VDPASIM_BLK_GROUP_NUM;
dev_attr.nas = VDPASIM_BLK_AS_NUM;
+ dev_attr.alloc_size = sizeof(struct vdpasim);
dev_attr.config_size = sizeof(struct virtio_blk_config);
dev_attr.get_config = vdpasim_blk_get_config;
dev_attr.work_fn = vdpasim_blk_work;
diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim_net.c b/drivers/vdpa/vdpa_sim/vdpa_sim_net.c
index c3cb225ea469..20cd5cdff919 100644
--- a/drivers/vdpa/vdpa_sim/vdpa_sim_net.c
+++ b/drivers/vdpa/vdpa_sim/vdpa_sim_net.c
@@ -249,6 +249,7 @@ static int vdpasim_net_dev_add(struct vdpa_mgmt_dev *mdev, const char *name,
dev_attr.nvqs = VDPASIM_NET_VQ_NUM;
dev_attr.ngroups = VDPASIM_NET_GROUP_NUM;
dev_attr.nas = VDPASIM_NET_AS_NUM;
+ dev_attr.alloc_size = sizeof(struct vdpasim);
dev_attr.config_size = sizeof(struct virtio_net_config);
dev_attr.get_config = vdpasim_net_get_config;
dev_attr.work_fn = vdpasim_net_work;
--
2.25.1
On Thu, Dec 22, 2022 at 6:01 AM Jason Wang <[email protected]> wrote:
>
> Allow individual simulator to customize the allocation size.
>
> Signed-off-by: Jason Wang <[email protected]>
> ---
> drivers/vdpa/vdpa_sim/vdpa_sim.c | 8 ++++++--
> drivers/vdpa/vdpa_sim/vdpa_sim.h | 1 +
> drivers/vdpa/vdpa_sim/vdpa_sim_blk.c | 1 +
> drivers/vdpa/vdpa_sim/vdpa_sim_net.c | 1 +
> 4 files changed, 9 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.c b/drivers/vdpa/vdpa_sim/vdpa_sim.c
> index 757afef86ba0..55aaa023a6e2 100644
> --- a/drivers/vdpa/vdpa_sim/vdpa_sim.c
> +++ b/drivers/vdpa/vdpa_sim/vdpa_sim.c
> @@ -253,7 +253,10 @@ struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr,
> struct vdpa_device *vdpa;
> struct vdpasim *vdpasim;
> struct device *dev;
> - int i, ret = -ENOMEM;
> + int i, ret = -EINVAL;
> +
> + if (!dev_attr->alloc_size)
> + goto err_alloc;
It's weird that we need an error goto here and the next chunk of code
may return ERR_PTR(-EINVAL) directly. It's better to return directly
here in my opinion.
>
> if (config->mask & BIT_ULL(VDPA_ATTR_DEV_FEATURES)) {
> if (config->device_features &
> @@ -268,9 +271,10 @@ struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr,
> else
> ops = &vdpasim_config_ops;
>
> + ret = -ENOMEM;
Nitpicking again,
To set ret = -ENOMEM here is weird to me, since the next failure on
__vdpa_alloc_device will override it. I'd set it right before
dma_set_mask_and_coherent, closer to its actual usage.
Actually, I'd propagate dma_set_mask_and_coherent error and set ret =
-ENOMEM right before *alloc functions, but it wasn't done that way
before this series, so not a reason to NAK to me.
Thanks!
> vdpa = __vdpa_alloc_device(NULL, ops,
> dev_attr->ngroups, dev_attr->nas,
> - sizeof(struct vdpasim),
> + dev_attr->alloc_size,
> dev_attr->name, false);
> if (IS_ERR(vdpa)) {
> ret = PTR_ERR(vdpa);
> diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.h b/drivers/vdpa/vdpa_sim/vdpa_sim.h
> index 0e78737dcc16..51c070a543f1 100644
> --- a/drivers/vdpa/vdpa_sim/vdpa_sim.h
> +++ b/drivers/vdpa/vdpa_sim/vdpa_sim.h
> @@ -37,6 +37,7 @@ struct vdpasim_dev_attr {
> struct vdpa_mgmt_dev *mgmt_dev;
> const char *name;
> u64 supported_features;
> + size_t alloc_size;
> size_t config_size;
> size_t buffer_size;
> int nvqs;
> diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim_blk.c b/drivers/vdpa/vdpa_sim/vdpa_sim_blk.c
> index c6db1a1baf76..4f7c35f59aa5 100644
> --- a/drivers/vdpa/vdpa_sim/vdpa_sim_blk.c
> +++ b/drivers/vdpa/vdpa_sim/vdpa_sim_blk.c
> @@ -378,6 +378,7 @@ static int vdpasim_blk_dev_add(struct vdpa_mgmt_dev *mdev, const char *name,
> dev_attr.nvqs = VDPASIM_BLK_VQ_NUM;
> dev_attr.ngroups = VDPASIM_BLK_GROUP_NUM;
> dev_attr.nas = VDPASIM_BLK_AS_NUM;
> + dev_attr.alloc_size = sizeof(struct vdpasim);
> dev_attr.config_size = sizeof(struct virtio_blk_config);
> dev_attr.get_config = vdpasim_blk_get_config;
> dev_attr.work_fn = vdpasim_blk_work;
> diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim_net.c b/drivers/vdpa/vdpa_sim/vdpa_sim_net.c
> index c3cb225ea469..20cd5cdff919 100644
> --- a/drivers/vdpa/vdpa_sim/vdpa_sim_net.c
> +++ b/drivers/vdpa/vdpa_sim/vdpa_sim_net.c
> @@ -249,6 +249,7 @@ static int vdpasim_net_dev_add(struct vdpa_mgmt_dev *mdev, const char *name,
> dev_attr.nvqs = VDPASIM_NET_VQ_NUM;
> dev_attr.ngroups = VDPASIM_NET_GROUP_NUM;
> dev_attr.nas = VDPASIM_NET_AS_NUM;
> + dev_attr.alloc_size = sizeof(struct vdpasim);
> dev_attr.config_size = sizeof(struct virtio_net_config);
> dev_attr.get_config = vdpasim_net_get_config;
> dev_attr.work_fn = vdpasim_net_work;
> --
> 2.25.1
>
On Thu, Dec 22, 2022 at 4:22 PM Eugenio Perez Martin
<[email protected]> wrote:
>
> On Thu, Dec 22, 2022 at 6:01 AM Jason Wang <[email protected]> wrote:
> >
> > Allow individual simulator to customize the allocation size.
> >
> > Signed-off-by: Jason Wang <[email protected]>
> > ---
> > drivers/vdpa/vdpa_sim/vdpa_sim.c | 8 ++++++--
> > drivers/vdpa/vdpa_sim/vdpa_sim.h | 1 +
> > drivers/vdpa/vdpa_sim/vdpa_sim_blk.c | 1 +
> > drivers/vdpa/vdpa_sim/vdpa_sim_net.c | 1 +
> > 4 files changed, 9 insertions(+), 2 deletions(-)
> >
> > diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.c b/drivers/vdpa/vdpa_sim/vdpa_sim.c
> > index 757afef86ba0..55aaa023a6e2 100644
> > --- a/drivers/vdpa/vdpa_sim/vdpa_sim.c
> > +++ b/drivers/vdpa/vdpa_sim/vdpa_sim.c
> > @@ -253,7 +253,10 @@ struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr,
> > struct vdpa_device *vdpa;
> > struct vdpasim *vdpasim;
> > struct device *dev;
> > - int i, ret = -ENOMEM;
> > + int i, ret = -EINVAL;
> > +
> > + if (!dev_attr->alloc_size)
> > + goto err_alloc;
>
> It's weird that we need an error goto here and the next chunk of code
> may return ERR_PTR(-EINVAL) directly. It's better to return directly
> here in my opinion.
Right, let me fix it.
>
> >
> > if (config->mask & BIT_ULL(VDPA_ATTR_DEV_FEATURES)) {
> > if (config->device_features &
> > @@ -268,9 +271,10 @@ struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr,
> > else
> > ops = &vdpasim_config_ops;
> >
> > + ret = -ENOMEM;
>
> Nitpicking again,
>
> To set ret = -ENOMEM here is weird to me, since the next failure on
> __vdpa_alloc_device will override it. I'd set it right before
> dma_set_mask_and_coherent, closer to its actual usage.
Yes.
>
> Actually, I'd propagate dma_set_mask_and_coherent error and set ret =
> -ENOMEM right before *alloc functions, but it wasn't done that way
> before this series, so not a reason to NAK to me.
Yes and I think this needs a separate fix.
Thanks
>
> Thanks!
>
> > vdpa = __vdpa_alloc_device(NULL, ops,
> > dev_attr->ngroups, dev_attr->nas,
> > - sizeof(struct vdpasim),
> > + dev_attr->alloc_size,
> > dev_attr->name, false);
> > if (IS_ERR(vdpa)) {
> > ret = PTR_ERR(vdpa);
> > diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.h b/drivers/vdpa/vdpa_sim/vdpa_sim.h
> > index 0e78737dcc16..51c070a543f1 100644
> > --- a/drivers/vdpa/vdpa_sim/vdpa_sim.h
> > +++ b/drivers/vdpa/vdpa_sim/vdpa_sim.h
> > @@ -37,6 +37,7 @@ struct vdpasim_dev_attr {
> > struct vdpa_mgmt_dev *mgmt_dev;
> > const char *name;
> > u64 supported_features;
> > + size_t alloc_size;
> > size_t config_size;
> > size_t buffer_size;
> > int nvqs;
> > diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim_blk.c b/drivers/vdpa/vdpa_sim/vdpa_sim_blk.c
> > index c6db1a1baf76..4f7c35f59aa5 100644
> > --- a/drivers/vdpa/vdpa_sim/vdpa_sim_blk.c
> > +++ b/drivers/vdpa/vdpa_sim/vdpa_sim_blk.c
> > @@ -378,6 +378,7 @@ static int vdpasim_blk_dev_add(struct vdpa_mgmt_dev *mdev, const char *name,
> > dev_attr.nvqs = VDPASIM_BLK_VQ_NUM;
> > dev_attr.ngroups = VDPASIM_BLK_GROUP_NUM;
> > dev_attr.nas = VDPASIM_BLK_AS_NUM;
> > + dev_attr.alloc_size = sizeof(struct vdpasim);
> > dev_attr.config_size = sizeof(struct virtio_blk_config);
> > dev_attr.get_config = vdpasim_blk_get_config;
> > dev_attr.work_fn = vdpasim_blk_work;
> > diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim_net.c b/drivers/vdpa/vdpa_sim/vdpa_sim_net.c
> > index c3cb225ea469..20cd5cdff919 100644
> > --- a/drivers/vdpa/vdpa_sim/vdpa_sim_net.c
> > +++ b/drivers/vdpa/vdpa_sim/vdpa_sim_net.c
> > @@ -249,6 +249,7 @@ static int vdpasim_net_dev_add(struct vdpa_mgmt_dev *mdev, const char *name,
> > dev_attr.nvqs = VDPASIM_NET_VQ_NUM;
> > dev_attr.ngroups = VDPASIM_NET_GROUP_NUM;
> > dev_attr.nas = VDPASIM_NET_AS_NUM;
> > + dev_attr.alloc_size = sizeof(struct vdpasim);
> > dev_attr.config_size = sizeof(struct virtio_net_config);
> > dev_attr.get_config = vdpasim_net_get_config;
> > dev_attr.work_fn = vdpasim_net_work;
> > --
> > 2.25.1
> >
>
On Thu, Dec 22, 2022 at 6:01 AM Jason Wang <[email protected]> wrote:
>
> Hi All:
>
> This series implements vendor stats in vdpasim_net. Please review.
>
> Thanks
>
Apart from the commented blanks,
Acked-by: Eugenio PĂ©rez <[email protected]>
Thanks!
> Changes since V1:
> - typo fixes
> - move the duplicated get_vendor_vq_stats() in
> vdpasim_batch_config_ops to vdpa_sim_config_ops
> - use -EOPNOTSUPP instead of -EINVAL in vdpasim_get_vq_stats
> - introdce a dedicated variable to record the successful cvq request
> and track the number of requests correctly
>
> Jason Wang (4):
> vdpa_sim: switch to use __vdpa_alloc_device()
> vdpasim: customize allocation size
> vdpa_sim: support vendor statistics
> vdpa_sim_net: vendor satistics
>
> drivers/vdpa/vdpa_sim/vdpa_sim.c | 33 +++-
> drivers/vdpa/vdpa_sim/vdpa_sim.h | 4 +
> drivers/vdpa/vdpa_sim/vdpa_sim_blk.c | 1 +
> drivers/vdpa/vdpa_sim/vdpa_sim_net.c | 221 ++++++++++++++++++++++++++-
> 4 files changed, 248 insertions(+), 11 deletions(-)
>
> --
> 2.25.1
>
On Thu, Dec 22, 2022 at 6:01 AM Jason Wang <[email protected]> wrote:
>
> This patch adds support for basic vendor stats that include counters
> for tx, rx and cvq.
>
> Signed-off-by: Jason Wang <[email protected]>
> ---
> drivers/vdpa/vdpa_sim/vdpa_sim_net.c | 222 ++++++++++++++++++++++++++-
> 1 file changed, 216 insertions(+), 6 deletions(-)
>
> diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim_net.c b/drivers/vdpa/vdpa_sim/vdpa_sim_net.c
> index 20cd5cdff919..e0fbb739fc4d 100644
> --- a/drivers/vdpa/vdpa_sim/vdpa_sim_net.c
> +++ b/drivers/vdpa/vdpa_sim/vdpa_sim_net.c
> @@ -15,6 +15,7 @@
> #include <linux/etherdevice.h>
> #include <linux/vringh.h>
> #include <linux/vdpa.h>
> +#include <net/netlink.h>
> #include <uapi/linux/virtio_net.h>
> #include <uapi/linux/vdpa.h>
>
> @@ -36,6 +37,34 @@
> #define VDPASIM_NET_AS_NUM 2
> #define VDPASIM_NET_GROUP_NUM 2
>
> +struct vdpasim_dataq_stats {
> + struct u64_stats_sync syncp;
> + u64 pkts;
> + u64 bytes;
> + u64 drops;
> + u64 errors;
> + u64 overruns;
> +};
> +
> +struct vdpasim_cq_stats {
> + struct u64_stats_sync syncp;
> + u64 requests;
> + u64 successes;
> + u64 errors;
> +};
> +
> +struct vdpasim_net{
> + struct vdpasim vdpasim;
> + struct vdpasim_dataq_stats tx_stats;
> + struct vdpasim_dataq_stats rx_stats;
> + struct vdpasim_cq_stats cq_stats;
> +};
> +
> +static struct vdpasim_net *sim_to_net(struct vdpasim *vdpasim)
> +{
> + return container_of(vdpasim, struct vdpasim_net, vdpasim);
> +}
> +
> static void vdpasim_net_complete(struct vdpasim_virtqueue *vq, size_t len)
> {
> /* Make sure data is wrote before advancing index */
> @@ -93,9 +122,14 @@ static virtio_net_ctrl_ack vdpasim_handle_ctrl_mac(struct vdpasim *vdpasim,
> static void vdpasim_handle_cvq(struct vdpasim *vdpasim)
> {
> struct vdpasim_virtqueue *cvq = &vdpasim->vqs[2];
> +
> +
> +
Too many blank lines.
Thanks!
> + struct vdpasim_net *net = sim_to_net(vdpasim);
> virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
> struct virtio_net_ctrl_hdr ctrl;
> size_t read, write;
> + u64 requests = 0, errors = 0, successes = 0;
> int err;
>
> if (!(vdpasim->features & (1ULL << VIRTIO_NET_F_CTRL_VQ)))
> @@ -111,10 +145,13 @@ static void vdpasim_handle_cvq(struct vdpasim *vdpasim)
> if (err <= 0)
> break;
>
> + ++requests;
> read = vringh_iov_pull_iotlb(&cvq->vring, &cvq->in_iov, &ctrl,
> sizeof(ctrl));
> - if (read != sizeof(ctrl))
> + if (read != sizeof(ctrl)) {
> + ++errors;
> break;
> + }
>
> switch (ctrl.class) {
> case VIRTIO_NET_CTRL_MAC:
> @@ -124,6 +161,11 @@ static void vdpasim_handle_cvq(struct vdpasim *vdpasim)
> break;
> }
>
> + if (status == VIRTIO_NET_OK)
> + ++successes;
> + else
> + ++errors;
> +
> /* Make sure data is wrote before advancing index */
> smp_wmb();
>
> @@ -141,6 +183,12 @@ static void vdpasim_handle_cvq(struct vdpasim *vdpasim)
> cvq->cb(cvq->private);
> local_bh_enable();
> }
> +
> + u64_stats_update_begin(&net->cq_stats.syncp);
> + net->cq_stats.requests += requests;
> + net->cq_stats.errors += errors;
> + net->cq_stats.successes += successes;
> + u64_stats_update_end(&net->cq_stats.syncp);
> }
>
> static void vdpasim_net_work(struct work_struct *work)
> @@ -148,8 +196,10 @@ static void vdpasim_net_work(struct work_struct *work)
> struct vdpasim *vdpasim = container_of(work, struct vdpasim, work);
> struct vdpasim_virtqueue *txq = &vdpasim->vqs[1];
> struct vdpasim_virtqueue *rxq = &vdpasim->vqs[0];
> + struct vdpasim_net *net = sim_to_net(vdpasim);
> ssize_t read, write;
> - int pkts = 0;
> + u64 tx_pkts = 0, rx_pkts = 0, tx_bytes = 0, rx_bytes = 0;
> + u64 rx_drops = 0, rx_overruns = 0, rx_errors = 0, tx_errors = 0;
> int err;
>
> spin_lock(&vdpasim->lock);
> @@ -168,14 +218,21 @@ static void vdpasim_net_work(struct work_struct *work)
> while (true) {
> err = vringh_getdesc_iotlb(&txq->vring, &txq->out_iov, NULL,
> &txq->head, GFP_ATOMIC);
> - if (err <= 0)
> + if (err <= 0) {
> + if (err)
> + ++tx_errors;
> break;
> + }
>
> + ++tx_pkts;
> read = vringh_iov_pull_iotlb(&txq->vring, &txq->out_iov,
> vdpasim->buffer,
> PAGE_SIZE);
>
> + tx_bytes += read;
> +
> if (!receive_filter(vdpasim, read)) {
> + ++rx_drops;
> vdpasim_net_complete(txq, 0);
> continue;
> }
> @@ -183,19 +240,25 @@ static void vdpasim_net_work(struct work_struct *work)
> err = vringh_getdesc_iotlb(&rxq->vring, NULL, &rxq->in_iov,
> &rxq->head, GFP_ATOMIC);
> if (err <= 0) {
> + ++rx_overruns;
> vdpasim_net_complete(txq, 0);
> break;
> }
>
> write = vringh_iov_push_iotlb(&rxq->vring, &rxq->in_iov,
> vdpasim->buffer, read);
> - if (write <= 0)
> + if (write <= 0) {
> + ++rx_errors;
> break;
> + }
> +
> + ++rx_pkts;
> + rx_bytes += write;
>
> vdpasim_net_complete(txq, 0);
> vdpasim_net_complete(rxq, write);
>
> - if (++pkts > 4) {
> + if (tx_pkts > 4) {
> schedule_work(&vdpasim->work);
> goto out;
> }
> @@ -203,6 +266,145 @@ static void vdpasim_net_work(struct work_struct *work)
>
> out:
> spin_unlock(&vdpasim->lock);
> +
> + u64_stats_update_begin(&net->tx_stats.syncp);
> + net->tx_stats.pkts += tx_pkts;
> + net->tx_stats.bytes += tx_bytes;
> + net->tx_stats.errors += tx_errors;
> + u64_stats_update_end(&net->tx_stats.syncp);
> +
> + u64_stats_update_begin(&net->rx_stats.syncp);
> + net->rx_stats.pkts += rx_pkts;
> + net->rx_stats.bytes += rx_bytes;
> + net->rx_stats.drops += rx_drops;
> + net->rx_stats.errors += rx_errors;
> + net->rx_stats.overruns += rx_overruns;
> + u64_stats_update_end(&net->rx_stats.syncp);
> +}
> +
> +static int vdpasim_net_get_stats(struct vdpasim *vdpasim, u16 idx,
> + struct sk_buff *msg,
> + struct netlink_ext_ack *extack)
> +{
> + struct vdpasim_net *net = sim_to_net(vdpasim);
> + u64 rx_pkts, rx_bytes, rx_errors, rx_overruns, rx_drops;
> + u64 tx_pkts, tx_bytes, tx_errors, tx_drops;
> + u64 cq_requests, cq_successes, cq_errors;
> + unsigned int start;
> + int err = -EMSGSIZE;
> +
> + switch(idx) {
> + case 0:
> + do {
> + start = u64_stats_fetch_begin(&net->rx_stats.syncp);
> + rx_pkts = net->rx_stats.pkts;
> + rx_bytes = net->rx_stats.bytes;
> + rx_errors = net->rx_stats.errors;
> + rx_overruns = net->rx_stats.overruns;
> + rx_drops = net->rx_stats.drops;
> + } while (u64_stats_fetch_retry(&net->rx_stats.syncp, start));
> +
> + if (nla_put_string(msg, VDPA_ATTR_DEV_VENDOR_ATTR_NAME,
> + "rx packets"))
> + break;
> + if (nla_put_u64_64bit(msg, VDPA_ATTR_DEV_VENDOR_ATTR_VALUE,
> + rx_pkts, VDPA_ATTR_PAD))
> + break;
> + if (nla_put_string(msg, VDPA_ATTR_DEV_VENDOR_ATTR_NAME,
> + "rx bytes"))
> + break;
> + if (nla_put_u64_64bit(msg, VDPA_ATTR_DEV_VENDOR_ATTR_VALUE,
> + rx_bytes, VDPA_ATTR_PAD))
> + break;
> + if (nla_put_string(msg, VDPA_ATTR_DEV_VENDOR_ATTR_NAME,
> + "rx errors"))
> + break;
> + if (nla_put_u64_64bit(msg, VDPA_ATTR_DEV_VENDOR_ATTR_VALUE,
> + rx_errors, VDPA_ATTR_PAD))
> + break;
> + if (nla_put_string(msg, VDPA_ATTR_DEV_VENDOR_ATTR_NAME,
> + "rx overrunss"))
> + break;
> + if (nla_put_u64_64bit(msg, VDPA_ATTR_DEV_VENDOR_ATTR_VALUE,
> + rx_overruns, VDPA_ATTR_PAD))
> + break;
> + if (nla_put_string(msg, VDPA_ATTR_DEV_VENDOR_ATTR_NAME,
> + "rx drops"))
> + break;
> + if (nla_put_u64_64bit(msg, VDPA_ATTR_DEV_VENDOR_ATTR_VALUE,
> + rx_drops, VDPA_ATTR_PAD))
> + break;
> + err = 0;
> + break;
> + case 1:
> + do {
> + start = u64_stats_fetch_begin(&net->tx_stats.syncp);
> + tx_pkts = net->tx_stats.pkts;
> + tx_bytes = net->tx_stats.bytes;
> + tx_errors = net->tx_stats.errors;
> + tx_drops = net->tx_stats.drops;
> + } while (u64_stats_fetch_retry(&net->tx_stats.syncp, start));
> +
> + if (nla_put_string(msg, VDPA_ATTR_DEV_VENDOR_ATTR_NAME,
> + "tx packets"))
> + break;
> + if (nla_put_u64_64bit(msg, VDPA_ATTR_DEV_VENDOR_ATTR_VALUE,
> + tx_pkts, VDPA_ATTR_PAD))
> + break;
> + if (nla_put_string(msg, VDPA_ATTR_DEV_VENDOR_ATTR_NAME,
> + "tx bytes"))
> + break;
> + if (nla_put_u64_64bit(msg, VDPA_ATTR_DEV_VENDOR_ATTR_VALUE,
> + tx_bytes, VDPA_ATTR_PAD))
> + break;
> + if (nla_put_string(msg, VDPA_ATTR_DEV_VENDOR_ATTR_NAME,
> + "tx errors"))
> + break;
> + if (nla_put_u64_64bit(msg, VDPA_ATTR_DEV_VENDOR_ATTR_VALUE,
> + tx_errors, VDPA_ATTR_PAD))
> + break;
> + if (nla_put_string(msg, VDPA_ATTR_DEV_VENDOR_ATTR_NAME,
> + "tx drops"))
> + break;
> + if (nla_put_u64_64bit(msg, VDPA_ATTR_DEV_VENDOR_ATTR_VALUE,
> + tx_drops, VDPA_ATTR_PAD))
> + break;
> + err = 0;
> + break;
> + case 2:
> + do {
> + start = u64_stats_fetch_begin(&net->cq_stats.syncp);
> + cq_requests = net->cq_stats.requests;
> + cq_successes = net->cq_stats.successes;
> + cq_errors = net->cq_stats.errors;
> + } while (u64_stats_fetch_retry(&net->cq_stats.syncp, start));
> +
> + if (nla_put_string(msg, VDPA_ATTR_DEV_VENDOR_ATTR_NAME,
> + "cvq requests"))
> + break;
> + if (nla_put_u64_64bit(msg, VDPA_ATTR_DEV_VENDOR_ATTR_VALUE,
> + cq_requests, VDPA_ATTR_PAD))
> + break;
> + if (nla_put_string(msg, VDPA_ATTR_DEV_VENDOR_ATTR_NAME,
> + "cvq successes"))
> + break;
> + if (nla_put_u64_64bit(msg, VDPA_ATTR_DEV_VENDOR_ATTR_VALUE,
> + cq_successes, VDPA_ATTR_PAD))
> + break;
> + if (nla_put_string(msg, VDPA_ATTR_DEV_VENDOR_ATTR_NAME,
> + "cvq errors"))
> + break;
> + if (nla_put_u64_64bit(msg, VDPA_ATTR_DEV_VENDOR_ATTR_VALUE,
> + cq_errors, VDPA_ATTR_PAD))
> + break;
> + err = 0;
> + break;
> + default:
> + err = -EINVAL;
> + break;
> + }
> +
> + return err;
> }
>
> static void vdpasim_net_get_config(struct vdpasim *vdpasim, void *config)
> @@ -239,6 +441,7 @@ static int vdpasim_net_dev_add(struct vdpa_mgmt_dev *mdev, const char *name,
> const struct vdpa_dev_set_config *config)
> {
> struct vdpasim_dev_attr dev_attr = {};
> + struct vdpasim_net *net;
> struct vdpasim *simdev;
> int ret;
>
> @@ -249,10 +452,11 @@ static int vdpasim_net_dev_add(struct vdpa_mgmt_dev *mdev, const char *name,
> dev_attr.nvqs = VDPASIM_NET_VQ_NUM;
> dev_attr.ngroups = VDPASIM_NET_GROUP_NUM;
> dev_attr.nas = VDPASIM_NET_AS_NUM;
> - dev_attr.alloc_size = sizeof(struct vdpasim);
> + dev_attr.alloc_size = sizeof(struct vdpasim_net);
> dev_attr.config_size = sizeof(struct virtio_net_config);
> dev_attr.get_config = vdpasim_net_get_config;
> dev_attr.work_fn = vdpasim_net_work;
> + dev_attr.get_stats = vdpasim_net_get_stats;
> dev_attr.buffer_size = PAGE_SIZE;
>
> simdev = vdpasim_create(&dev_attr, config);
> @@ -265,6 +469,12 @@ static int vdpasim_net_dev_add(struct vdpa_mgmt_dev *mdev, const char *name,
> if (ret)
> goto reg_err;
>
> + net = sim_to_net(simdev);
> +
> + u64_stats_init(&net->tx_stats.syncp);
> + u64_stats_init(&net->rx_stats.syncp);
> + u64_stats_init(&net->cq_stats.syncp);
> +
> return 0;
>
> reg_err:
> --
> 2.25.1
>