@samitouri / QOSamiQemu / commits / e37ca0fb0b

net/tap: net_init_tap_one() refactor to get vhostfd param

Get vhostfd instead of vhostfdname: - more symmetry with fd param - prepare to further changes Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru> Reviewed-by: Ben Chaney <bchaney@akamai.com> Signed-off-by: Jason Wang <jasowang@redhat.com>

Vladimir Sementsov-Ogievskiy committed Mar 18, 2026 at 14:31 UTC e37ca0fb0bdbed290663b6551d310b5120444384
1 file changed +31 -17
net/tap.c
+31 -17
@@ -706,11 +706,10 @@ static int net_tap_init(const NetdevTapOptions *tap, int *vnet_hdr,
706 static bool net_init_tap_one(const NetdevTapOptions *tap, NetClientState *peer,
707 const char *model, const char *name,
708 const char *ifname, const char *script,
709 - const char *downscript, const char *vhostfdname,
709 + const char *downscript, int vhostfd,
710 int vnet_hdr, int fd, Error **errp)
711 {
712 TAPState *s = net_tap_fd_init(peer, model, name, fd, vnet_hdr);
713 - int vhostfd;
713 bool sndbuf_required = tap->has_sndbuf;
714 int sndbuf =
715 (tap->has_sndbuf && tap->sndbuf) ? MIN(tap->sndbuf, INT_MAX) : INT_MAX;
@@ -738,7 +737,7 @@ static bool net_init_tap_one(const NetdevTapOptions *tap, NetClientState *peer,
737 }
738
739 if (tap->has_vhost ? tap->vhost :
741 - vhostfdname || (tap->has_vhostforce && tap->vhostforce)) {
740 + (vhostfd != -1) || (tap->has_vhostforce && tap->vhostforce)) {
741 VhostNetOptions options;
742
743 options.backend_type = VHOST_BACKEND_TYPE_KERNEL;
@@ -749,15 +748,7 @@ static bool net_init_tap_one(const NetdevTapOptions *tap, NetClientState *peer,
748 options.busyloop_timeout = 0;
749 }
750
752 - if (vhostfdname) {
753 - vhostfd = monitor_fd_param(monitor_cur(), vhostfdname, errp);
754 - if (vhostfd == -1) {
755 - goto failed;
756 - }
757 - if (!qemu_set_blocking(vhostfd, false, errp)) {
758 - goto failed;
759 - }
760 - } else {
751 + if (vhostfd == -1) {
752 vhostfd = open("/dev/vhost-net", O_RDWR);
753 if (vhostfd < 0) {
754 error_setg_file_open(errp, errno, "/dev/vhost-net");
@@ -820,7 +811,7 @@ int net_init_tap(const Netdev *netdev, const char *name,
811 NetClientState *peer, Error **errp)
812 {
813 const NetdevTapOptions *tap;
823 - int fd = -1, vnet_hdr = 0, i = 0, queues;
814 + int fd = -1, vhostfd = -1, vnet_hdr = 0, i = 0, queues;
815 /* for the no-fd, no-helper case */
816 char ifname[128];
817 char **fds = NULL, **vhost_fds = NULL;
@@ -866,6 +857,17 @@ int net_init_tap(const Netdev *netdev, const char *name,
857 return -1;
858 }
859
860 + if (tap->vhostfd) {
861 + vhostfd = monitor_fd_param(monitor_cur(), tap->vhostfd, errp);
862 + if (vhostfd == -1) {
863 + return -1;
864 + }
865 +
866 + if (!qemu_set_blocking(vhostfd, false, errp)) {
867 + goto fail;
868 + }
869 + }
870 +
871 if (tap->fd) {
872 fd = monitor_fd_param(monitor_cur(), tap->fd, errp);
873 if (fd == -1) {
@@ -883,7 +885,7 @@ int net_init_tap(const Netdev *netdev, const char *name,
885
886 if (!net_init_tap_one(tap, peer, "tap", name, NULL,
887 NULL, NULL,
886 - tap->vhostfd, vnet_hdr, fd, errp)) {
888 + vhostfd, vnet_hdr, fd, errp)) {
889 goto fail;
890 }
891 } else if (tap->fds) {
@@ -910,6 +912,17 @@ int net_init_tap(const Netdev *netdev, const char *name,
912 goto fail;
913 }
914
915 + if (tap->vhostfds) {
916 + vhostfd = monitor_fd_param(monitor_cur(), vhost_fds[i], errp);
917 + if (vhostfd == -1) {
918 + goto fail;
919 + }
920 +
921 + if (!qemu_set_blocking(vhostfd, false, errp)) {
922 + goto fail;
923 + }
924 + }
925 +
926 if (i == 0) {
927 vnet_hdr = tap_probe_vnet_hdr(fd, errp);
928 if (vnet_hdr < 0) {
@@ -923,7 +936,7 @@ int net_init_tap(const Netdev *netdev, const char *name,
936
937 if (!net_init_tap_one(tap, peer, "tap", name, ifname,
938 NULL, NULL,
926 - tap->vhostfds ? vhost_fds[i] : NULL,
939 + vhostfd,
940 vnet_hdr, fd, errp)) {
941 goto fail;
942 }
@@ -945,7 +958,7 @@ int net_init_tap(const Netdev *netdev, const char *name,
958 }
959
960 if (!net_init_tap_one(tap, peer, "bridge", name, ifname,
948 - NULL, NULL, tap->vhostfd,
961 + NULL, NULL, vhostfd,
962 vnet_hdr, fd, errp)) {
963 goto fail;
964 }
@@ -978,7 +991,7 @@ int net_init_tap(const Netdev *netdev, const char *name,
991 if (!net_init_tap_one(tap, peer, "tap", name, ifname,
992 i >= 1 ? NULL : script,
993 i >= 1 ? NULL : downscript,
981 - tap->vhostfd, vnet_hdr, fd, errp)) {
994 + vhostfd, vnet_hdr, fd, errp)) {
995 goto fail;
996 }
997 }
@@ -988,6 +1001,7 @@ int net_init_tap(const Netdev *netdev, const char *name,
1001
1002 fail:
1003 close(fd);
1004 + close(vhostfd);
1005 if (vhost_fds) {
1006 for (i = 0; i < nvhosts; i++) {
1007 g_free(vhost_fds[i]);