diff mbox series

[9/9] vhost-vdpa: allow pre-opend file descriptor

Message ID 20200831082737.10983-10-jasowang@redhat.com
State New
Headers show
Series refine vhost-vdpa initialization | expand

Commit Message

Jason Wang Aug. 31, 2020, 8:27 a.m. UTC
This patch allows to initialize vhost-vdpa network backend with pre
opened vhost-vdpa file descriptor. This is useful for running
unprivileged qemu through libvirt.

Cc: Eric Blake <eblake@redhat.com>
Cc: Markus Armbruster <armbru@redhat.com>
Signed-off-by: Jason Wang <jasowang@redhat.com>
---
 net/vhost-vdpa.c | 24 +++++++++++++++++++-----
 qapi/net.json    |  5 ++++-
 2 files changed, 23 insertions(+), 6 deletions(-)

Comments

Cindy Lu Aug. 31, 2020, 11:16 a.m. UTC | #1
On Mon, 31 Aug 2020 at 16:30, Jason Wang <jasowang@redhat.com> wrote:
>
> This patch allows to initialize vhost-vdpa network backend with pre
> opened vhost-vdpa file descriptor. This is useful for running
> unprivileged qemu through libvirt.
>
> Cc: Eric Blake <eblake@redhat.com>
> Cc: Markus Armbruster <armbru@redhat.com>
> Signed-off-by: Jason Wang <jasowang@redhat.com>
> ---
>  net/vhost-vdpa.c | 24 +++++++++++++++++++-----
>  qapi/net.json    |  5 ++++-
>  2 files changed, 23 insertions(+), 6 deletions(-)
>
> diff --git a/net/vhost-vdpa.c b/net/vhost-vdpa.c
> index 9a6f0b63d3..f6385cd264 100644
> --- a/net/vhost-vdpa.c
> +++ b/net/vhost-vdpa.c
> @@ -206,20 +206,34 @@ int net_init_vhost_vdpa(const Netdev *netdev, const char *name,
>                            (char *)name, errp)) {
>          return -1;
>      }
> -    if (!opts->has_vhostdev) {
> -        error_setg(errp, "vhost-vdpa requires vhostdev to be set");
> +    if (!(opts->has_vhostdev ^ opts->has_fd)) {
> +        error_setg(errp, "Vhost-vdpa requires either vhostdev or fd to be set");
>          return -1;
>      }
>
>      assert(name);
>
>      nc = qemu_new_net_client(&net_vhost_vdpa_info, peer, TYPE_VHOST_VDPA, name);
> -    snprintf(nc->info_str, sizeof(nc->info_str), "vhostdev=%s", opts->vhostdev);
> +    if (opts->has_vhostdev) {
> +        snprintf(nc->info_str, sizeof(nc->info_str),
> +                 "vhostdev=%s", opts->vhostdev);
> +        vdpa_device_fd = qemu_open(opts->vhostdev, O_RDWR);
> +        if (vdpa_device_fd == -1) {
> +            error_setg(errp, "Fail to open vhost-vdpa device %s",
> +                       opts->vhostdev);
> +            return -errno;
> +        }
> +    } else {
> +        snprintf(nc->info_str, sizeof(nc->info_str), "fd=%s", opts->fd);
> +        vdpa_device_fd = monitor_fd_param(cur_mon, opts->fd, errp);
> +        if (vdpa_device_fd == -1) {
> +            return -1;
> +        }
> +    }
>
>      s = DO_UPCAST(VhostVDPAState, nc, nc);
> -    vdpa_device_fd = qemu_open(opts->vhostdev, O_RDWR);
>      if (vdpa_device_fd == -1) {
> -        error_setg(errp, "Fail to open vhost-vdpa device %s", opts->vhostdev);
> +
>          return -errno;
>      }
>      s->vhost_vdpa.device_fd = vdpa_device_fd;
> diff --git a/qapi/net.json b/qapi/net.json
> index a2a94fad3e..5ad60c3045 100644
> --- a/qapi/net.json
> +++ b/qapi/net.json
> @@ -442,12 +442,15 @@
>  # @queues: number of queues to be created for multiqueue vhost-vdpa
>  #          (default: 1)
>  #
> +# @fd: file descriptor of an already opened vhost-vdpa (since 5.2)
> +#
>  # Since: 5.1
>  ##
>  { 'struct': 'NetdevVhostVDPAOptions',
>    'data': {
>      '*vhostdev':     'str',
> -    '*queues':       'int' } }
> +    '*queues':       'int',
> +    '*fd':           'str' } }
>
>  ##
>  # @NetClientDriver:
> --
> 2.20.1
>
I think the latest  code supported this part.
you can pass a pre open file descriptor to it via the add-fd QMP
command to /dev/fdset/NNN, and then pass the string
"/dev/fdset/NNN" as vhostdev.  so we don't need a special fd parameter here.

Thanks
Cindy
Jason Wang Sept. 1, 2020, 6:53 a.m. UTC | #2
On 2020/8/31 下午7:16, Cindy Lu wrote:
> On Mon, 31 Aug 2020 at 16:30, Jason Wang <jasowang@redhat.com> wrote:
>> This patch allows to initialize vhost-vdpa network backend with pre
>> opened vhost-vdpa file descriptor. This is useful for running
>> unprivileged qemu through libvirt.
>>
>> Cc: Eric Blake <eblake@redhat.com>
>> Cc: Markus Armbruster <armbru@redhat.com>
>> Signed-off-by: Jason Wang <jasowang@redhat.com>
>> ---
>>   net/vhost-vdpa.c | 24 +++++++++++++++++++-----
>>   qapi/net.json    |  5 ++++-
>>   2 files changed, 23 insertions(+), 6 deletions(-)
>>
>> diff --git a/net/vhost-vdpa.c b/net/vhost-vdpa.c
>> index 9a6f0b63d3..f6385cd264 100644
>> --- a/net/vhost-vdpa.c
>> +++ b/net/vhost-vdpa.c
>> @@ -206,20 +206,34 @@ int net_init_vhost_vdpa(const Netdev *netdev, const char *name,
>>                             (char *)name, errp)) {
>>           return -1;
>>       }
>> -    if (!opts->has_vhostdev) {
>> -        error_setg(errp, "vhost-vdpa requires vhostdev to be set");
>> +    if (!(opts->has_vhostdev ^ opts->has_fd)) {
>> +        error_setg(errp, "Vhost-vdpa requires either vhostdev or fd to be set");
>>           return -1;
>>       }
>>
>>       assert(name);
>>
>>       nc = qemu_new_net_client(&net_vhost_vdpa_info, peer, TYPE_VHOST_VDPA, name);
>> -    snprintf(nc->info_str, sizeof(nc->info_str), "vhostdev=%s", opts->vhostdev);
>> +    if (opts->has_vhostdev) {
>> +        snprintf(nc->info_str, sizeof(nc->info_str),
>> +                 "vhostdev=%s", opts->vhostdev);
>> +        vdpa_device_fd = qemu_open(opts->vhostdev, O_RDWR);
>> +        if (vdpa_device_fd == -1) {
>> +            error_setg(errp, "Fail to open vhost-vdpa device %s",
>> +                       opts->vhostdev);
>> +            return -errno;
>> +        }
>> +    } else {
>> +        snprintf(nc->info_str, sizeof(nc->info_str), "fd=%s", opts->fd);
>> +        vdpa_device_fd = monitor_fd_param(cur_mon, opts->fd, errp);
>> +        if (vdpa_device_fd == -1) {
>> +            return -1;
>> +        }
>> +    }
>>
>>       s = DO_UPCAST(VhostVDPAState, nc, nc);
>> -    vdpa_device_fd = qemu_open(opts->vhostdev, O_RDWR);
>>       if (vdpa_device_fd == -1) {
>> -        error_setg(errp, "Fail to open vhost-vdpa device %s", opts->vhostdev);
>> +
>>           return -errno;
>>       }
>>       s->vhost_vdpa.device_fd = vdpa_device_fd;
>> diff --git a/qapi/net.json b/qapi/net.json
>> index a2a94fad3e..5ad60c3045 100644
>> --- a/qapi/net.json
>> +++ b/qapi/net.json
>> @@ -442,12 +442,15 @@
>>   # @queues: number of queues to be created for multiqueue vhost-vdpa
>>   #          (default: 1)
>>   #
>> +# @fd: file descriptor of an already opened vhost-vdpa (since 5.2)
>> +#
>>   # Since: 5.1
>>   ##
>>   { 'struct': 'NetdevVhostVDPAOptions',
>>     'data': {
>>       '*vhostdev':     'str',
>> -    '*queues':       'int' } }
>> +    '*queues':       'int',
>> +    '*fd':           'str' } }
>>
>>   ##
>>   # @NetClientDriver:
>> --
>> 2.20.1
>>
> I think the latest  code supported this part.
> you can pass a pre open file descriptor to it via the add-fd QMP
> command to /dev/fdset/NNN, and then pass the string
> "/dev/fdset/NNN" as vhostdev.  so we don't need a special fd parameter here.


Right, I forgot the fdset tricks.


Thanks



> Thanks
> Cindy
>
>
Eric Blake Sept. 16, 2020, 4:04 p.m. UTC | #3
On 8/31/20 6:16 AM, Cindy Lu wrote:
> On Mon, 31 Aug 2020 at 16:30, Jason Wang <jasowang@redhat.com> wrote:
>>
>> This patch allows to initialize vhost-vdpa network backend with pre
>> opened vhost-vdpa file descriptor. This is useful for running
>> unprivileged qemu through libvirt.
>>
>> Cc: Eric Blake <eblake@redhat.com>
>> Cc: Markus Armbruster <armbru@redhat.com>
>> Signed-off-by: Jason Wang <jasowang@redhat.com>
>> ---

>> +++ b/qapi/net.json
>> @@ -442,12 +442,15 @@
>>   # @queues: number of queues to be created for multiqueue vhost-vdpa
>>   #          (default: 1)
>>   #
>> +# @fd: file descriptor of an already opened vhost-vdpa (since 5.2)
>> +#
>>   # Since: 5.1
>>   ##
>>   { 'struct': 'NetdevVhostVDPAOptions',
>>     'data': {
>>       '*vhostdev':     'str',
>> -    '*queues':       'int' } }
>> +    '*queues':       'int',
>> +    '*fd':           'str' } }
>>
>>   ##
>>   # @NetClientDriver:
>> --
>> 2.20.1
>>
> I think the latest  code supported this part.
> you can pass a pre open file descriptor to it via the add-fd QMP
> command to /dev/fdset/NNN, and then pass the string
> "/dev/fdset/NNN" as vhostdev.  so we don't need a special fd parameter here.

Correct - the 'vhostdev' parameter + magic filename is all the more that 
is needed to access a pre-opened fd, so no 'fd' parameter should be added.
diff mbox series

Patch

diff --git a/net/vhost-vdpa.c b/net/vhost-vdpa.c
index 9a6f0b63d3..f6385cd264 100644
--- a/net/vhost-vdpa.c
+++ b/net/vhost-vdpa.c
@@ -206,20 +206,34 @@  int net_init_vhost_vdpa(const Netdev *netdev, const char *name,
                           (char *)name, errp)) {
         return -1;
     }
-    if (!opts->has_vhostdev) {
-        error_setg(errp, "vhost-vdpa requires vhostdev to be set");
+    if (!(opts->has_vhostdev ^ opts->has_fd)) {
+        error_setg(errp, "Vhost-vdpa requires either vhostdev or fd to be set");
         return -1;
     }
 
     assert(name);
 
     nc = qemu_new_net_client(&net_vhost_vdpa_info, peer, TYPE_VHOST_VDPA, name);
-    snprintf(nc->info_str, sizeof(nc->info_str), "vhostdev=%s", opts->vhostdev);
+    if (opts->has_vhostdev) {
+        snprintf(nc->info_str, sizeof(nc->info_str),
+                 "vhostdev=%s", opts->vhostdev);
+        vdpa_device_fd = qemu_open(opts->vhostdev, O_RDWR);
+        if (vdpa_device_fd == -1) {
+            error_setg(errp, "Fail to open vhost-vdpa device %s",
+                       opts->vhostdev);
+            return -errno;
+        }
+    } else {
+        snprintf(nc->info_str, sizeof(nc->info_str), "fd=%s", opts->fd);
+        vdpa_device_fd = monitor_fd_param(cur_mon, opts->fd, errp);
+        if (vdpa_device_fd == -1) {
+            return -1;
+        }
+    }
 
     s = DO_UPCAST(VhostVDPAState, nc, nc);
-    vdpa_device_fd = qemu_open(opts->vhostdev, O_RDWR);
     if (vdpa_device_fd == -1) {
-        error_setg(errp, "Fail to open vhost-vdpa device %s", opts->vhostdev);
+
         return -errno;
     }
     s->vhost_vdpa.device_fd = vdpa_device_fd;
diff --git a/qapi/net.json b/qapi/net.json
index a2a94fad3e..5ad60c3045 100644
--- a/qapi/net.json
+++ b/qapi/net.json
@@ -442,12 +442,15 @@ 
 # @queues: number of queues to be created for multiqueue vhost-vdpa
 #          (default: 1)
 #
+# @fd: file descriptor of an already opened vhost-vdpa (since 5.2)
+#
 # Since: 5.1
 ##
 { 'struct': 'NetdevVhostVDPAOptions',
   'data': {
     '*vhostdev':     'str',
-    '*queues':       'int' } }
+    '*queues':       'int',
+    '*fd':           'str' } }
 
 ##
 # @NetClientDriver: