From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 7DB6BCA0FE1 for ; Fri, 1 Sep 2023 10:02:10 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qc0xj-0000M8-TF; Fri, 01 Sep 2023 06:01:11 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qc0xZ-0000K8-PF for qemu-devel@nongnu.org; Fri, 01 Sep 2023 06:01:02 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qc0xX-0007Vw-51 for qemu-devel@nongnu.org; Fri, 01 Sep 2023 06:01:01 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1693562458; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=PbE8PxWlx8vaWh2V1FewM6gI98FrvJqOBJjYo2Ev7i4=; b=MGdtyMVGL99v6PpEhjuuyRc+jRuvv/pHyo76/gKsHnh+GLjulCtzWLYjGmpnG6kYZToTDp FQerUrgkaUY9iw77Vxd19fTCYoQ3fOtv18rUxzfPI21X02FwpT56QdGquEzzXj8Q/UpJCK ZP9EFSzrEdaFlEUwtk9rlH1Q3AaGp4c= Received: from mail-pj1-f69.google.com (mail-pj1-f69.google.com [209.85.216.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-402-eypvN_F_OuWQ_sBQyd4yPw-1; Fri, 01 Sep 2023 06:00:56 -0400 X-MC-Unique: eypvN_F_OuWQ_sBQyd4yPw-1 Received: by mail-pj1-f69.google.com with SMTP id 98e67ed59e1d1-26f6ed09f59so2000979a91.1 for ; Fri, 01 Sep 2023 03:00:56 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693562455; x=1694167255; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=PbE8PxWlx8vaWh2V1FewM6gI98FrvJqOBJjYo2Ev7i4=; b=N441S/DUAfajiLfiqby9KF62pmePqccW9AmOg914dwdhVWKF8ZKxs/0MgAadsAvZT7 xR0NKqBvcl956pyp3UsOlc065EzXwPiXXCm32j/bHiUUwT8IqCg6m8mn85pJTS3BS2Os dPUUfsA2G2gBXbJJFxgeXF29+kxYxb4t0pxe2vBVrHFMCM66BUqWYgdv7izNqYVbVm2o JbgZBEOjuaCNRyL0HeBZ60Q1PTA7Zjfn41I+5GBYQOEn3NlFW/Gz0/riOVU2XLqvvgzT ReYI3JK6QPgcHoAcDaDcrwTgV2nTQW/gMt7XHhjhF7kXUJmocVXSxGS4zxjWo9e4F4ie T1ZQ== X-Gm-Message-State: AOJu0YxnuJ4Ol9n0uc0SyCC8DxxwLpL8pW8Km6ak1zxtYAt67iXD1HHo eEE+B6g9jcJ33R1qyD3zfTaGlTrW9qccDee1FTSXv+zd3GowiS7kek6eCvTemHhX9KurzUjuXKq iPDUNTPuFm5ykyMv3Fvk/Zs8IcxXoIxQ= X-Received: by 2002:a17:90a:6304:b0:26d:49c5:52cf with SMTP id e4-20020a17090a630400b0026d49c552cfmr1506845pjj.49.1693562455448; Fri, 01 Sep 2023 03:00:55 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGfNEbQAJplW/orJLWC1vqlD98QKHlIZL4kQIlSiq1a6XwnLAwOpEHx4/K+j+lEQtTXH3bsHu9zUeGwCuwt9pA= X-Received: by 2002:a17:90a:6304:b0:26d:49c5:52cf with SMTP id e4-20020a17090a630400b0026d49c552cfmr1506810pjj.49.1693562455078; Fri, 01 Sep 2023 03:00:55 -0700 (PDT) MIME-Version: 1.0 References: <20230710153522.3469097-1-alex.bennee@linaro.org> <20230710153522.3469097-21-alex.bennee@linaro.org> In-Reply-To: <20230710153522.3469097-21-alex.bennee@linaro.org> From: Albert Esteve Date: Fri, 1 Sep 2023 12:00:44 +0200 Message-ID: Subject: Re: [RFC PATCH v3 20/20] hw/virtio: allow vhost-user-device to be driven by backend To: =?UTF-8?B?QWxleCBCZW5uw6ll?= Cc: qemu-devel@nongnu.org, =?UTF-8?B?TWFyYy1BbmRyw6kgTHVyZWF1?= , Jason Wang , Viresh Kumar , Raphael Norwitz , Paolo Bonzini , =?UTF-8?Q?Daniel_P=2E_Berrang=C3=A9?= , qemu-block@nongnu.org, Eric Blake , "Michael S. Tsirkin" , Mark Cave-Ayland , Kevin Wolf , Markus Armbruster , Gerd Hoffmann , Mathieu Poirier , Hanna Reitz , Erik Schilling , "Gonglei (Arei)" , Eduardo Habkost , Fam Zheng , virtio-fs@redhat.com, Stefan Hajnoczi Content-Type: multipart/alternative; boundary="0000000000008a51730604493c18" Received-SPF: pass client-ip=170.10.133.124; envelope-from=aesteve@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, HTML_MESSAGE=0.001, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org --0000000000008a51730604493c18 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Mon, Jul 10, 2023 at 6:44=E2=80=AFPM Alex Benn=C3=A9e wrote: > Instead of requiring all the information up front allow the > vhost_dev_init to complete and then see what information we have from > the backend. > > This does change the order around somewhat. > > Signed-off-by: Alex Benn=C3=A9e > --- > hw/virtio/vhost-user-device.c | 45 +++++++++++++++++++++++++---------- > 1 file changed, 32 insertions(+), 13 deletions(-) > > diff --git a/hw/virtio/vhost-user-device.c b/hw/virtio/vhost-user-device.= c > index 0109d4829d..b30b6265fb 100644 > --- a/hw/virtio/vhost-user-device.c > +++ b/hw/virtio/vhost-user-device.c > @@ -243,7 +243,6 @@ static void vub_device_realize(DeviceState *dev, Erro= r > **errp) > { > VirtIODevice *vdev =3D VIRTIO_DEVICE(dev); > VHostUserBase *vub =3D VHOST_USER_BASE(dev); > - int ret; > > if (!vub->chardev.chr) { > error_setg(errp, "vhost-user-device: missing chardev"); > @@ -254,13 +253,43 @@ static void vub_device_realize(DeviceState *dev, > Error **errp) > return; > } > > + if (vhost_dev_init(&vub->vhost_dev, &vub->vhost_user, > + VHOST_BACKEND_TYPE_USER, 0, errp)!=3D0) { > + error_setg(errp, "vhost-user-device: unable to start connection"= ); > + return; > + } > + > + if (vub->vhost_dev.specs.device_id) { > + if (vub->virtio_id && vub->virtio_id !=3D > vub->vhost_dev.specs.device_id) { > + error_setg(errp, "vhost-user-device: backend id %d doesn't > match cli %d", > + vub->vhost_dev.specs.device_id, vub->virtio_id); > + return; > + } > + vub->virtio_id =3D vub->vhost_dev.specs.device_id; > + } > + > if (!vub->virtio_id) { > - error_setg(errp, "vhost-user-device: need to define device id"); > + error_setg(errp, "vhost-user-device: need to define or be told > device id"); > return; > } > > + if (vub->vhost_dev.specs.min_vqs) { > + if (vub->num_vqs) { > + if (vub->num_vqs < vub->vhost_dev.specs.min_vqs || > + vub->num_vqs > vub->vhost_dev.specs.max_vqs) { > + error_setg(errp, > + "vhost-user-device: selected nvqs (%d) out of > bounds (%d->%d)", > + vub->num_vqs, > + vub->vhost_dev.specs.min_vqs, > vub->vhost_dev.specs.max_vqs); > + return; > + } > + } else { > + vub->num_vqs =3D vub->vhost_dev.specs.min_vqs; > + } > + } > + > if (!vub->num_vqs) { > - vub->num_vqs =3D 1; /* reasonable default? */ > + error_setg(errp, "vhost-user-device: need to define number of > vqs"); > } > > /* > @@ -287,16 +316,6 @@ static void vub_device_realize(DeviceState *dev, > Error **errp) > virtio_add_queue(vdev, 4, vub_handle_output)); > } > > - vub->vhost_dev.nvqs =3D vub->num_vqs; > Who sets `vub->vhost_dev.nvqs` after removing this line? Why having `vub->num_vqs` in the first place? In vub_start for example we still use `vub->vhost_dev.nvqs`, and we pass `vhost_dev` to other functions that use its `nvqs`, so `num_vqs` is redundant and requires a logic to copy/initialise `vhost_dev.nvqs`. Maybe it would be better to initialse `nvqs` through a function, in the device file, instead of doing: `vub->num_vqs =3D 2;` We could have: `vub_set_nvqs(vub, 2);` Or something along those lines. And the function will have all the internal logic in this commit, i.e., checking the boundaries, setting the `vhost_dev.nvqs` value, printing the error, etc. So we can save the extra variable, and the logic to copy the value to the device. > - > - /* connect to backend */ > - ret =3D vhost_dev_init(&vub->vhost_dev, &vub->vhost_user, > - VHOST_BACKEND_TYPE_USER, 0, errp); > - > - if (ret < 0) { > - do_vhost_user_cleanup(vdev, vub); > - } > - > qemu_chr_fe_set_handlers(&vub->chardev, NULL, NULL, vub_event, NULL, > dev, NULL, true); > } > -- > 2.39.2 > > > --0000000000008a51730604493c18 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable


On Mon, Jul 10, 2023 at 6:44= =E2=80=AFPM Alex Benn=C3=A9e <= alex.bennee@linaro.org> wrote:
Instead of requiring all the information up front all= ow the
vhost_dev_init to complete and then see what information we have from
the backend.

This does change the order around somewhat.

Signed-off-by: Alex Benn=C3=A9e <alex.bennee@linaro.org>
---
=C2=A0hw/virtio/vhost-user-device.c | 45 +++++++++++++++++++++++++---------= -
=C2=A01 file changed, 32 insertions(+), 13 deletions(-)

diff --git a/hw/virtio/vhost-user-device.c b/hw/virtio/vhost-user-device.c<= br> index 0109d4829d..b30b6265fb 100644
--- a/hw/virtio/vhost-user-device.c
+++ b/hw/virtio/vhost-user-device.c
@@ -243,7 +243,6 @@ static void vub_device_realize(DeviceState *dev, Error = **errp)
=C2=A0{
=C2=A0 =C2=A0 =C2=A0VirtIODevice *vdev =3D VIRTIO_DEVICE(dev);
=C2=A0 =C2=A0 =C2=A0VHostUserBase *vub =3D VHOST_USER_BASE(dev);
-=C2=A0 =C2=A0 int ret;

=C2=A0 =C2=A0 =C2=A0if (!vub->chardev.chr) {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0error_setg(errp, "vhost-user-device:= missing chardev");
@@ -254,13 +253,43 @@ static void vub_device_realize(DeviceState *dev, Erro= r **errp)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0return;
=C2=A0 =C2=A0 =C2=A0}

+=C2=A0 =C2=A0 if (vhost_dev_init(&vub->vhost_dev, &vub->vhos= t_user,
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0VHOST_BACKEND_TYPE_USER, 0, errp)!=3D0) {
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 error_setg(errp, "vhost-user-device: unab= le to start connection");
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 return;
+=C2=A0 =C2=A0 }
+
+=C2=A0 =C2=A0 if (vub->vhost_dev.specs.device_id) {
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 if (vub->virtio_id && vub->virti= o_id !=3D vub->vhost_dev.specs.device_id) {
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 error_setg(errp, "vhost-use= r-device: backend id %d doesn't match cli %d",
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0vub->vhost_dev.specs.device_id, vub->virtio_id);
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 return;
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 }
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 vub->virtio_id =3D vub->vhost_dev.specs.= device_id;
+=C2=A0 =C2=A0 }
+
=C2=A0 =C2=A0 =C2=A0if (!vub->virtio_id) {
-=C2=A0 =C2=A0 =C2=A0 =C2=A0 error_setg(errp, "vhost-user-device: need= to define device id");
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 error_setg(errp, "vhost-user-device: need= to define or be told device id");
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0return;
=C2=A0 =C2=A0 =C2=A0}

+=C2=A0 =C2=A0 if (vub->vhost_dev.specs.min_vqs) {
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 if (vub->num_vqs) {
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 if (vub->num_vqs < vub->= ;vhost_dev.specs.min_vqs ||
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 vub->num_vqs &g= t; vub->vhost_dev.specs.max_vqs) {
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 error_setg(errp, +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0"vhost-user-device: selected nvqs (%d) out of = bounds (%d->%d)",
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0vub->num_vqs,
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0vub->vhost_dev.specs.min_vqs, vub->vhost_dev.= specs.max_vqs);
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 return;
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 }
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 } else {
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 vub->num_vqs =3D vub->vhos= t_dev.specs.min_vqs;
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 }
+=C2=A0 =C2=A0 }
+
=C2=A0 =C2=A0 =C2=A0if (!vub->num_vqs) {
-=C2=A0 =C2=A0 =C2=A0 =C2=A0 vub->num_vqs =3D 1; /* reasonable default? = */
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 error_setg(errp, "vhost-user-device: need= to define number of vqs");
=C2=A0 =C2=A0 =C2=A0}

=C2=A0 =C2=A0 =C2=A0/*
@@ -287,16 +316,6 @@ static void vub_device_realize(DeviceState *dev, Error= **errp)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0virtio_add_queue(vdev, 4, vub_handle_output));
=C2=A0 =C2=A0 =C2=A0}

-=C2=A0 =C2=A0 vub->vhost_dev.nvqs =3D vub->num_vqs;
=

Who sets `vub->vhost_dev.nvqs` after removing this l= ine?
Why having `vub->num_vqs` in the first place? In vub_star= t for example we still
use `vub->vhost_dev.nvqs`, and we pass = `vhost_dev` to other functions that
use its `nvqs`, so `num_vqs` = is redundant and requires a logic to copy/initialise `vhost_dev.nvqs`.

Maybe it would be better to initialse=C2=A0`nvqs` thro= ugh a function, in the device file, instead of doing:
`vub->nu= m_vqs =3D 2;`
We could have:
`vub_set_nvqs(vub, 2);= `
Or something along those lines. And the function will have all = the internal logic in this commit, i.e.,
checking the boundar= ies, setting the `vhost_dev.nvqs` value, printing the error, etc.
So we can save the extra variable, and the logic to copy the value to the = device.
=C2=A0
-
-=C2=A0 =C2=A0 /* connect to backend */
-=C2=A0 =C2=A0 ret =3D vhost_dev_init(&vub->vhost_dev, &vub->= vhost_user,
-=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0VHOST_BACKEND_TYPE_USER, 0, errp);
-
-=C2=A0 =C2=A0 if (ret < 0) {
-=C2=A0 =C2=A0 =C2=A0 =C2=A0 do_vhost_user_cleanup(vdev, vub);
-=C2=A0 =C2=A0 }
-
=C2=A0 =C2=A0 =C2=A0qemu_chr_fe_set_handlers(&vub->chardev, NULL, NU= LL, vub_event, NULL,
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 dev, NULL, true);
=C2=A0}
--
2.39.2


--0000000000008a51730604493c18--