[PATCH v2 02/15] net/vhost-user: Fix device compatibility check

Kevin Wolf posted 15 patches 1 month, 2 weeks ago

[PATCH v2 02/15] net/vhost-user: Fix device compatibility check

Posted by Kevin Wolf 1 month, 2 weeks ago
vhost-user works only with specific devices. At startup, it second
guesses what the command line option handling will do and error out if
it thinks a non-virtio device will attach to them.

This second guessing is not only ugly, it can lead to wrong error
messages ('-device floppy,netdev=foo' should complain about an unknown
property, not about the wrong kind of network device being attached) and
completely ignores hotplugging.

Drop the old checks and implement .check_peer_type() instead to fix
this. As a nice side effect, it also removes one more dependency on the
legacy QemuOpts infrastructure and even reduces the code size.

Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
 net/vhost-user.c | 41 ++++++++++++++---------------------------
 1 file changed, 14 insertions(+), 27 deletions(-)

diff --git a/net/vhost-user.c b/net/vhost-user.c
index 4a939124d2..b1a0247b59 100644
--- a/net/vhost-user.c
+++ b/net/vhost-user.c
@@ -198,6 +198,19 @@ static bool vhost_user_has_ufo(NetClientState *nc)
     return true;
 }
 
+static bool vhost_user_check_peer_type(NetClientState *nc, ObjectClass *oc,
+                                       Error **errp)
+{
+    const char *driver = object_class_get_name(oc);
+
+    if (!g_str_has_prefix(driver, "virtio-net-")) {
+        error_setg(errp, "vhost-user requires frontend driver virtio-net-*");
+        return false;
+    }
+
+    return true;
+}
+
 static NetClientInfo net_vhost_user_info = {
         .type = NET_CLIENT_DRIVER_VHOST_USER,
         .size = sizeof(NetVhostUserState),
@@ -207,6 +220,7 @@ static NetClientInfo net_vhost_user_info = {
         .has_ufo = vhost_user_has_ufo,
         .set_vnet_be = vhost_user_set_vnet_endianness,
         .set_vnet_le = vhost_user_set_vnet_endianness,
+        .check_peer_type = vhost_user_check_peer_type,
 };
 
 static gboolean net_vhost_user_watch(void *do_not_use, GIOCondition cond,
@@ -397,27 +411,6 @@ static Chardev *net_vhost_claim_chardev(
     return chr;
 }
 
-static int net_vhost_check_net(void *opaque, QemuOpts *opts, Error **errp)
-{
-    const char *name = opaque;
-    const char *driver, *netdev;
-
-    driver = qemu_opt_get(opts, "driver");
-    netdev = qemu_opt_get(opts, "netdev");
-
-    if (!driver || !netdev) {
-        return 0;
-    }
-
-    if (strcmp(netdev, name) == 0 &&
-        !g_str_has_prefix(driver, "virtio-net-")) {
-        error_setg(errp, "vhost-user requires frontend driver virtio-net-*");
-        return -1;
-    }
-
-    return 0;
-}
-
 int net_init_vhost_user(const Netdev *netdev, const char *name,
                         NetClientState *peer, Error **errp)
 {
@@ -433,12 +426,6 @@ int net_init_vhost_user(const Netdev *netdev, const char *name,
         return -1;
     }
 
-    /* verify net frontend */
-    if (qemu_opts_foreach(qemu_find_opts("device"), net_vhost_check_net,
-                          (char *)name, errp)) {
-        return -1;
-    }
-
     queues = vhost_user_opts->has_queues ? vhost_user_opts->queues : 1;
     if (queues < 1 || queues > MAX_QUEUE_NUM) {
         error_setg(errp,
-- 
2.31.1

Re: [PATCH v2 02/15] net/vhost-user: Fix device compatibility check

Posted by Jason Wang 1 month, 1 week ago
在 2021/10/8 下午9:34, Kevin Wolf 写道:
> vhost-user works only with specific devices. At startup, it second
> guesses what the command line option handling will do and error out if
> it thinks a non-virtio device will attach to them.
>
> This second guessing is not only ugly, it can lead to wrong error
> messages ('-device floppy,netdev=foo' should complain about an unknown
> property, not about the wrong kind of network device being attached) and
> completely ignores hotplugging.
>
> Drop the old checks and implement .check_peer_type() instead to fix
> this. As a nice side effect, it also removes one more dependency on the
> legacy QemuOpts infrastructure and even reduces the code size.
>
> Signed-off-by: Kevin Wolf <kwolf@redhat.com>


Acked-by: Jason Wang <jasowang@redhat.com>


> ---
>   net/vhost-user.c | 41 ++++++++++++++---------------------------
>   1 file changed, 14 insertions(+), 27 deletions(-)
>
> diff --git a/net/vhost-user.c b/net/vhost-user.c
> index 4a939124d2..b1a0247b59 100644
> --- a/net/vhost-user.c
> +++ b/net/vhost-user.c
> @@ -198,6 +198,19 @@ static bool vhost_user_has_ufo(NetClientState *nc)
>       return true;
>   }
>   
> +static bool vhost_user_check_peer_type(NetClientState *nc, ObjectClass *oc,
> +                                       Error **errp)
> +{
> +    const char *driver = object_class_get_name(oc);
> +
> +    if (!g_str_has_prefix(driver, "virtio-net-")) {
> +        error_setg(errp, "vhost-user requires frontend driver virtio-net-*");
> +        return false;
> +    }
> +
> +    return true;
> +}
> +
>   static NetClientInfo net_vhost_user_info = {
>           .type = NET_CLIENT_DRIVER_VHOST_USER,
>           .size = sizeof(NetVhostUserState),
> @@ -207,6 +220,7 @@ static NetClientInfo net_vhost_user_info = {
>           .has_ufo = vhost_user_has_ufo,
>           .set_vnet_be = vhost_user_set_vnet_endianness,
>           .set_vnet_le = vhost_user_set_vnet_endianness,
> +        .check_peer_type = vhost_user_check_peer_type,
>   };
>   
>   static gboolean net_vhost_user_watch(void *do_not_use, GIOCondition cond,
> @@ -397,27 +411,6 @@ static Chardev *net_vhost_claim_chardev(
>       return chr;
>   }
>   
> -static int net_vhost_check_net(void *opaque, QemuOpts *opts, Error **errp)
> -{
> -    const char *name = opaque;
> -    const char *driver, *netdev;
> -
> -    driver = qemu_opt_get(opts, "driver");
> -    netdev = qemu_opt_get(opts, "netdev");
> -
> -    if (!driver || !netdev) {
> -        return 0;
> -    }
> -
> -    if (strcmp(netdev, name) == 0 &&
> -        !g_str_has_prefix(driver, "virtio-net-")) {
> -        error_setg(errp, "vhost-user requires frontend driver virtio-net-*");
> -        return -1;
> -    }
> -
> -    return 0;
> -}
> -
>   int net_init_vhost_user(const Netdev *netdev, const char *name,
>                           NetClientState *peer, Error **errp)
>   {
> @@ -433,12 +426,6 @@ int net_init_vhost_user(const Netdev *netdev, const char *name,
>           return -1;
>       }
>   
> -    /* verify net frontend */
> -    if (qemu_opts_foreach(qemu_find_opts("device"), net_vhost_check_net,
> -                          (char *)name, errp)) {
> -        return -1;
> -    }
> -
>       queues = vhost_user_opts->has_queues ? vhost_user_opts->queues : 1;
>       if (queues < 1 || queues > MAX_QUEUE_NUM) {
>           error_setg(errp,


Re: [PATCH v2 02/15] net/vhost-user: Fix device compatibility check

Posted by Damien Hedde 1 month, 2 weeks ago

On 10/8/21 15:34, Kevin Wolf wrote:
> vhost-user works only with specific devices. At startup, it second
> guesses what the command line option handling will do and error out if
> it thinks a non-virtio device will attach to them.
> 
> This second guessing is not only ugly, it can lead to wrong error
> messages ('-device floppy,netdev=foo' should complain about an unknown
> property, not about the wrong kind of network device being attached) and
> completely ignores hotplugging.
> 
> Drop the old checks and implement .check_peer_type() instead to fix
> this. As a nice side effect, it also removes one more dependency on the
> legacy QemuOpts infrastructure and even reduces the code size.
> 
> Signed-off-by: Kevin Wolf <kwolf@redhat.com>

Reviewed-by: Damien Hedde <damien.hedde@greensocs.com>