From: Laurent Vivier <lvivier@redhat.com>
To: Stefano Brivio <sbrivio@redhat.com>
Cc: passt-dev@passt.top
Subject: Re: [PATCH v2 3/4] vhost-user: introduce vhost-user API
Date: Wed, 14 Aug 2024 16:44:26 +0200 [thread overview]
Message-ID: <aee18cab-2aab-4493-9da4-0d394bde1dd8@redhat.com> (raw)
In-Reply-To: <20240719232953.07bacbfb@elisabeth>
On 19/07/2024 23:29, Stefano Brivio wrote:
> On Fri, 12 Jul 2024 17:32:43 +0200
> Laurent Vivier <lvivier@redhat.com> wrote:
>
>> Add vhost_user.c and vhost_user.h that define the functions needed
>> to implement vhost-user backend.
>>
>> Signed-off-by: Laurent Vivier <lvivier@redhat.com>
>> ---
>> Makefile | 4 +-
>> iov.c | 1 -
>> vhost_user.c | 1267 ++++++++++++++++++++++++++++++++++++++++++++++++++
>> vhost_user.h | 197 ++++++++
>> virtio.c | 5 -
>> virtio.h | 2 +-
>> 6 files changed, 1467 insertions(+), 9 deletions(-)
>> create mode 100644 vhost_user.c
>> create mode 100644 vhost_user.h
>>
...
>> +/**
>> + * vu_send() - Send a buffer to the front-end using the RX virtqueue
>> + * @vdev: vhost-user device
>> + * @buf: address of the buffer
>> + * @size: size of the buffer
>> + *
>> + * Return: number of bytes sent, -1 if there is an error
>> + */
>> +/* cppcheck-suppress unusedFunction */
>> +int vu_send(struct vu_dev *vdev, const void *buf, size_t size)
>> +{
>> + size_t hdrlen = vdev->hdrlen;
>> + struct vu_virtq *vq = &vdev->vq[VHOST_USER_RX_QUEUE];
>> + struct vu_virtq_element elem[VIRTQUEUE_MAX_SIZE];
>> + struct iovec in_sg[VIRTQUEUE_MAX_SIZE];
>> + size_t lens[VIRTQUEUE_MAX_SIZE];
>> + size_t offset;
>> + int i, j;
>> + __virtio16 *num_buffers_ptr;
>> + int in_sg_count;
>
> Can those be aligned in the usual way (from longest to shortest)?
>
>> +
>> + debug("vu_send size %zu hdrlen %zu", size, hdrlen);
>> +
>> + if (!vu_queue_enabled(vq) || !vu_queue_started(vq)) {
>> + err("Got packet, but no available descriptors on RX virtq.");
>> + return 0;
>> + }
>> +
>> + offset = 0;
>> + i = 0;
>> + num_buffers_ptr = NULL;
>> + in_sg_count = 0;
>
> Could those be initialised when you declare them?
>
>> + while (offset < size) {
>> + size_t len;
>> + int total;
>> + int ret;
>> +
>> + total = 0;
>> +
>> + if (i == ARRAY_SIZE(elem) ||
>> + in_sg_count == ARRAY_SIZE(in_sg)) {
>> + err("virtio-net unexpected long buffer chain");
>> + goto err;
>> + }
>> +
>> + elem[i].out_num = 0;
>> + elem[i].out_sg = NULL;
>> + elem[i].in_num = ARRAY_SIZE(in_sg) - in_sg_count;
>> + elem[i].in_sg = &in_sg[in_sg_count];
>> +
>> + ret = vu_queue_pop(vdev, vq, &elem[i]);
>> + if (ret < 0) {
>> + if (vu_wait_queue(vq) != -1)
>> + continue;
>> + if (i) {
>> + err("virtio-net unexpected empty queue: "
>> + "i %d mergeable %d offset %zd, size %zd, "
>> + "features 0x%" PRIx64,
>> + i, vu_has_feature(vdev,
>> + VIRTIO_NET_F_MRG_RXBUF),
>> + offset, size, vdev->features);
>> + }
>> + offset = -1;
>> + goto err;
>> + }
>> + in_sg_count += elem[i].in_num;
>> +
>> + if (elem[i].in_num < 1) {
>> + err("virtio-net receive queue contains no in buffers");
>> + vu_queue_detach_element(vdev, vq, elem[i].index, 0);
>> + offset = -1;
>> + goto err;
>> + }
>> +
>> + if (i == 0) {
>> + struct virtio_net_hdr hdr = {
>> + .flags = VIRTIO_NET_HDR_F_DATA_VALID,
>> + .gso_type = VIRTIO_NET_HDR_GSO_NONE,
>> + };
>> +
>> + ASSERT(offset == 0);
>> + ASSERT(elem[i].in_sg[0].iov_len >= hdrlen);
>> +
>> + len = iov_from_buf(elem[i].in_sg, elem[i].in_num, 0,
>> + &hdr, sizeof(hdr));
>> +
>> + num_buffers_ptr = (__virtio16 *)((char *)elem[i].in_sg[0].iov_base +
>> + len);
>> +
>> + total += hdrlen;
>
> Shouldn't this be 'total += len' or, alternatively, shouldn't there be
> a check that len == hdrlen?
len is sizeof(virtio_net_hdr) but hdrlen can be either sizeof(struct virtio_net_hdr) or
sizeof(struct virtio_net_hdr_mrg_rxbuf). It depends on VIRTIO_NET_F_MRG_RXBUF.
We actually want to add hdrlen to total.
struct virtio_net_hdr_mrg_rxbuf {
struct virtio_net_hdr hdr;
__virtio16 num_buffers; /* Number of merged rx buffers */
};
At this point we initialize hdr, num_buffers will be set later only if hdrlen is
sizeof(struct virtio_net_hdr_mrg_rxbuf).
Thanks,
Laurent
next prev parent reply other threads:[~2024-08-14 14:44 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-07-12 15:32 [PATCH v2 0/4] Add vhost-user support to passt. (part 3) Laurent Vivier
2024-07-12 15:32 ` [PATCH v2 1/4] packet: replace struct desc by struct iovec Laurent Vivier
2024-07-15 4:59 ` David Gibson
2024-07-19 21:28 ` Stefano Brivio
2024-07-12 15:32 ` [PATCH v2 2/4] vhost-user: introduce virtio API Laurent Vivier
2024-07-17 5:21 ` David Gibson
2024-08-14 12:47 ` Laurent Vivier
2024-08-15 4:52 ` David Gibson
2024-07-19 21:29 ` Stefano Brivio
2024-07-12 15:32 ` [PATCH v2 3/4] vhost-user: introduce vhost-user API Laurent Vivier
2024-07-19 21:29 ` Stefano Brivio
2024-08-14 14:44 ` Laurent Vivier [this message]
2024-07-12 15:32 ` [PATCH v2 4/4] vhost-user: add vhost-user Laurent Vivier
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=aee18cab-2aab-4493-9da4-0d394bde1dd8@redhat.com \
--to=lvivier@redhat.com \
--cc=passt-dev@passt.top \
--cc=sbrivio@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://passt.top/passt
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).