[PATCH v2 14/20] net/tap: refactor net_tap_init() into net_tap_open_one()

Vladimir Sementsov-Ogievskiy posted 20 patches 2 months, 3 weeks ago
Maintainers: Jason Wang <jasowang@redhat.com>
There is a newer version of this series
[PATCH v2 14/20] net/tap: refactor net_tap_init() into net_tap_open_one()
Posted by Vladimir Sementsov-Ogievskiy 2 months, 3 weeks ago
net_tap_init() is used in one place. Let's move net_init_tap_one()
call to it and simplify outer loop code.

Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>
---
 net/tap.c | 54 +++++++++++++++++++++++++++++++-----------------------
 1 file changed, 31 insertions(+), 23 deletions(-)

diff --git a/net/tap.c b/net/tap.c
index 83a1c9250a..57939ed16f 100644
--- a/net/tap.c
+++ b/net/tap.c
@@ -674,31 +674,37 @@ int net_init_bridge(const Netdev *netdev, const char *name,
     return 0;
 }
 
-static int net_tap_init(const NetdevTapOptions *tap, int *vnet_hdr,
-                        const char *setup_script, char *ifname,
-                        size_t ifname_sz, int mq_required, Error **errp)
+static int net_tap_open_one(const Netdev *netdev,
+                            const char *name, NetClientState *peer,
+                            const char *script, const char *downscript,
+                            char *ifname, size_t ifname_sz,
+                            int mq_required, Error **errp)
 {
+    const NetdevTapOptions *tap = &netdev->u.tap;
     Error *err = NULL;
-    int fd, vnet_hdr_required;
+    int fd, vnet_hdr_required, vnet_hdr;
+    int ret;
+
+    assert(netdev->type == NET_CLIENT_DRIVER_TAP);
 
     if (tap->has_vnet_hdr) {
-        *vnet_hdr = tap->vnet_hdr;
-        vnet_hdr_required = *vnet_hdr;
+        vnet_hdr = tap->vnet_hdr;
+        vnet_hdr_required = vnet_hdr;
     } else {
-        *vnet_hdr = 1;
+        vnet_hdr = 1;
         vnet_hdr_required = 0;
     }
 
-    fd = RETRY_ON_EINTR(tap_open(ifname, ifname_sz, vnet_hdr, vnet_hdr_required,
-                      mq_required, errp));
+    fd = RETRY_ON_EINTR(tap_open(ifname, ifname_sz, &vnet_hdr,
+                                 vnet_hdr_required, mq_required, errp));
     if (fd < 0) {
         return -1;
     }
 
-    if (setup_script &&
-        setup_script[0] != '\0' &&
-        strcmp(setup_script, "no") != 0) {
-        launch_script(setup_script, ifname, fd, &err);
+    if (script &&
+        script[0] != '\0' &&
+        strcmp(script, "no") != 0) {
+        launch_script(script, ifname, fd, &err);
         if (err) {
             error_propagate(errp, err);
             close(fd);
@@ -706,7 +712,15 @@ static int net_tap_init(const NetdevTapOptions *tap, int *vnet_hdr,
         }
     }
 
-    return fd;
+    ret = net_init_tap_one(netdev, peer, "tap", name, ifname,
+                           script, downscript,
+                           tap->vhostfd, vnet_hdr, fd, errp);
+    if (ret < 0) {
+        close(fd);
+        return -1;
+    }
+
+    return 0;
 }
 
 #define MAX_TAP_QUEUES 1024
@@ -948,20 +962,14 @@ int net_init_tap(const Netdev *netdev, const char *name,
         }
 
         for (i = 0; i < queues; i++) {
-            fd = net_tap_init(tap, &vnet_hdr, i >= 1 ? "no" : script,
-                              ifname, sizeof ifname, queues > 1, errp);
-            if (fd == -1) {
-                return -1;
-            }
-
-            ret = net_init_tap_one(netdev, peer, "tap", name, ifname,
+            ret = net_tap_open_one(netdev, name, peer,
                                    i >= 1 ? "no" : script,
                                    i >= 1 ? "no" : downscript,
-                                   tap->vhostfd, vnet_hdr, fd, errp);
+                                   ifname, sizeof ifname, queues > 1, errp);
             if (ret < 0) {
-                close(fd);
                 return -1;
             }
+
         }
     }
 
-- 
2.48.1
Re: [PATCH v2 14/20] net/tap: refactor net_tap_init() into net_tap_open_one()
Posted by Jason Wang 2 months, 1 week ago
On Sun, Aug 24, 2025 at 12:03 AM Vladimir Sementsov-Ogievskiy
<vsementsov@yandex-team.ru> wrote:
>
> net_tap_init() is used in one place. Let's move net_init_tap_one()
> call to it and simplify outer loop code.
>
> Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>
> ---
>  net/tap.c | 54 +++++++++++++++++++++++++++++++-----------------------
>  1 file changed, 31 insertions(+), 23 deletions(-)
>
> diff --git a/net/tap.c b/net/tap.c
> index 83a1c9250a..57939ed16f 100644
> --- a/net/tap.c
> +++ b/net/tap.c
> @@ -674,31 +674,37 @@ int net_init_bridge(const Netdev *netdev, const char *name,
>      return 0;
>  }
>
> -static int net_tap_init(const NetdevTapOptions *tap, int *vnet_hdr,
> -                        const char *setup_script, char *ifname,
> -                        size_t ifname_sz, int mq_required, Error **errp)
> +static int net_tap_open_one(const Netdev *netdev,
> +                            const char *name, NetClientState *peer,
> +                            const char *script, const char *downscript,

I'd stick to "setup_script" as we have "downscript".

And we can save several lines of changes.

The rest looks good.

Thanks
Re: [PATCH v2 14/20] net/tap: refactor net_tap_init() into net_tap_open_one()
Posted by Vladimir Sementsov-Ogievskiy 2 months, 1 week ago
On 03.09.25 08:36, Jason Wang wrote:
> On Sun, Aug 24, 2025 at 12:03 AM Vladimir Sementsov-Ogievskiy
> <vsementsov@yandex-team.ru> wrote:
>>
>> net_tap_init() is used in one place. Let's move net_init_tap_one()
>> call to it and simplify outer loop code.
>>
>> Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>
>> ---
>>   net/tap.c | 54 +++++++++++++++++++++++++++++++-----------------------
>>   1 file changed, 31 insertions(+), 23 deletions(-)
>>
>> diff --git a/net/tap.c b/net/tap.c
>> index 83a1c9250a..57939ed16f 100644
>> --- a/net/tap.c
>> +++ b/net/tap.c
>> @@ -674,31 +674,37 @@ int net_init_bridge(const Netdev *netdev, const char *name,
>>       return 0;
>>   }
>>
>> -static int net_tap_init(const NetdevTapOptions *tap, int *vnet_hdr,
>> -                        const char *setup_script, char *ifname,
>> -                        size_t ifname_sz, int mq_required, Error **errp)
>> +static int net_tap_open_one(const Netdev *netdev,
>> +                            const char *name, NetClientState *peer,
>> +                            const char *script, const char *downscript,
> 
> I'd stick to "setup_script" as we have "downscript".

I decided to rename, because in other places the variable is called script, and the original option is also script=.

It would be more correct to rename in a separate commit. I can do this in v2, or just add a not to commit message here.

> 
> And we can save several lines of changes.
> 
> The rest looks good.
> 
> Thanks
> 

Thank you for reviewing!


-- 
Best regards,
Vladimir