Skip to contentSkip to conversation

net: add per-connection guestfwd unix support

PATCH Superseded Gavin Li 3 participants 5 messages Nov 7 – Nov 16

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Gavin Li

author
#0 lore raw

PATCH 1/1 net: add per-connection guestfwd unix support

For -netdev user,guestfwd=..., add support for unix-per-connection: in addition to the currently supported tcp: and cmd:. With unix-per-connection:, a new connection is opened to the given unix socket. This is an improvement over the current unix: support, which does not support 1:1 guest:host connections.

Also update the minimum libslirp version to 4.2.0 (released in 2020) as it adds support for the slirp_add_unix() function.

---
 meson.build     | 2 +-
 net/slirp.c     | 6 ++++++
 qemu-options.hx | 9 +++++++++
 3 files changed, 16 insertions(+), 1 deletion(-)
meson.build +1 -1
index d7bcb581c1..6567ede5d9 100644
@@ -1289,7 +1289,7 @@ endif
 slirp = not_found
 if not get_option('slirp').auto() or have_system
   slirp = dependency('slirp', required: get_option('slirp'),
-                     method: 'pkg-config')
+                     method: 'pkg-config', version: '>=4.2.0')
   # slirp < 4.7 is incompatible with CFI support in QEMU.  This is because
   # it passes function pointers within libslirp as callbacks for timers.
   # When using a system-wide shared libslirp, the type information for the
net/slirp.c +6 -0
index 451c0fd1ba..b0b03b2bf5 100644
@@ -1159,6 +1159,12 @@ static int slirp_guestfwd(SlirpState *s, const char *config_str, Error **errp)
                        "forwarding rule '%s'", config_str);
             return -1;
         }
+    } else if (g_str_has_prefix(p, "unix-per-connection:")) {
+        if (slirp_add_unix(s->slirp, &p[20], &server, port) < 0) {
+            error_setg(errp, "Conflicting/invalid host:port in guest "
+                       "forwarding rule '%s'", config_str);
+            return -1;
+        }
     } else {
         Error *err = NULL;
         /*
qemu-options.hx +9 -0
index 5fe9d73eaa..062cd39aa3 100644
@@ -3553,6 +3553,15 @@ SRST
             # and connect the TCP stream to its stdin/stdout
             |qemu_system| -nic  'user,id=n1,guestfwd=tcp:10.0.2.100:1234-cmd:netcat 10.10.1.1 4321'
 
+    ``guestfwd=[tcp]:server:port-unix-per-connection:path``
+        With ``unix-per-connection:``, a new connection is opened to the
+        given unix socket per TCP connection made by the guest. The unix socket
+        need not exist at startup time.
+
+        .. parsed-literal::
+
+            |qemu_system| -nic user,guestfwd=tcp:10.0.2.100:1234-unix-per-connection:/tmp/socket
+
 ``-netdev tap,id=id[,fd=h][,ifname=name][,script=file][,downscript=dfile][,br=bridge][,helper=helper]``
     Configure a host TAP network backend with ID id.
 

base-commit: 71f25c75417d31c214ba2cd26b273c114fc99987

Signature · 1 line
2.55.0
Acked-by on v1 1/1
#1 lore raw

Gavin Li, le ven. 06 nov. 2026 23:42:25 -0500, a ecrit:

Quoted 10 lines

For -netdev user,guestfwd=..., add support for unix-per-connection: in addition to the currently supported tcp: and cmd:. With unix-per-connection:, a new connection is opened to the given unix socket. This is an improvement over the current unix: support, which does not support 1:1 guest:host connections.

Also update the minimum libslirp version to 4.2.0 (released in 2020) as it adds support for the slirp_add_unix() function.

Samuel

Quoted 61 lines
---
 meson.build     | 2 +-
 net/slirp.c     | 6 ++++++
 qemu-options.hx | 9 +++++++++
 3 files changed, 16 insertions(+), 1 deletion(-)
meson.build +1 -1
index d7bcb581c1..6567ede5d9 100644
@@ -1289,7 +1289,7 @@ endif
 slirp = not_found
 if not get_option('slirp').auto() or have_system
   slirp = dependency('slirp', required: get_option('slirp'),
-                     method: 'pkg-config')
+                     method: 'pkg-config', version: '>=4.2.0')
   # slirp < 4.7 is incompatible with CFI support in QEMU.  This is because
   # it passes function pointers within libslirp as callbacks for timers.
   # When using a system-wide shared libslirp, the type information for the
net/slirp.c +6 -0
index 451c0fd1ba..b0b03b2bf5 100644
@@ -1159,6 +1159,12 @@ static int slirp_guestfwd(SlirpState *s, const char *config_str, Error **errp)
                        "forwarding rule '%s'", config_str);
             return -1;
         }
+    } else if (g_str_has_prefix(p, "unix-per-connection:")) {
+        if (slirp_add_unix(s->slirp, &p[20], &server, port) < 0) {
+            error_setg(errp, "Conflicting/invalid host:port in guest "
+                       "forwarding rule '%s'", config_str);
+            return -1;
+        }
     } else {
         Error *err = NULL;
         /*
qemu-options.hx +9 -0
index 5fe9d73eaa..062cd39aa3 100644
@@ -3553,6 +3553,15 @@ SRST
             # and connect the TCP stream to its stdin/stdout
             |qemu_system| -nic  'user,id=n1,guestfwd=tcp:10.0.2.100:1234-cmd:netcat 10.10.1.1 4321'
 
+    ``guestfwd=[tcp]:server:port-unix-per-connection:path``
+        With ``unix-per-connection:``, a new connection is opened to the
+        given unix socket per TCP connection made by the guest. The unix socket
+        need not exist at startup time.
+
+        .. parsed-literal::
+
+            |qemu_system| -nic user,guestfwd=tcp:10.0.2.100:1234-unix-per-connection:/tmp/socket
+
 ``-netdev tap,id=id[,fd=h][,ifname=name][,script=file][,downscript=dfile][,br=bridge][,helper=helper]``
     Configure a host TAP network backend with ID id.
 

base-commit: 71f25c75417d31c214ba2cd26b273c114fc99987

Signature · 1 line
2.55.0
Signature · 5 lines
Samuel
 PS> Salut ! J'ai un sujet de philo à vous soumettre : "Suffit-il
 PS> d'observer pour connaître" Idées + plan Merçi
 Oui, ya qu'a t'observer pour connaître le fait que tu es une feignasse.
 -+- FF in: Guide du Neuneu d'Usenet - Neuneu fait de la philo -+-
#2 lore raw
In reply to Gavin Li
Quoted 5 lines · Gavin Li

For -netdev user,guestfwd=..., add support for unix-per-connection: in addition to the currently supported tcp: and cmd:. With unix-per-connection:, a new connection is opened to the given unix socket. This is an improvement over the current unix: support, which does not support 1:1 guest:host connections.

I'm not familiar with guestfwd, but from the manpage, I would rather assume that it always opens a new connection to the host socket for each guest connection. What are its current semantics when two guest connections are initiated on the same port, if that's not happening ?

Quoted 29 lines

Also update the minimum libslirp version to 4.2.0 (released in 2020) as it adds support for the slirp_add_unix() function.

---
 meson.build     | 2 +-
 net/slirp.c     | 6 ++++++
 qemu-options.hx | 9 +++++++++
 3 files changed, 16 insertions(+), 1 deletion(-)
meson.build +1 -1
index d7bcb581c1..6567ede5d9 100644
@@ -1289,7 +1289,7 @@ endif
 slirp = not_found
 if not get_option('slirp').auto() or have_system
   slirp = dependency('slirp', required: get_option('slirp'),
-                     method: 'pkg-config')
+                     method: 'pkg-config', version: '>=4.2.0')
   # slirp < 4.7 is incompatible with CFI support in QEMU.  This is because
   # it passes function pointers within libslirp as callbacks for timers.
   # When using a system-wide shared libslirp, the type information for the
net/slirp.c +0 -0
index 451c0fd1ba..b0b03b2bf5 100644

net/slirp.c static int slirp_guestfwd(SlirpState *s, const char *config_str, Error **errp) · line 1162

Quoted 4 lines
net/slirp.c +1 -0
@@ -1159,6 +1159,12 @@ static int slirp_guestfwd(SlirpState *s, const char *config_str, Error **errp)
                        "forwarding rule '%s'", config_str);
             return -1;
         }
+    } else if (g_str_has_prefix(p, "unix-per-connection:")) {

This is rather a mouthful as a parameter name, and including '-' in the name is undesirable, since we're already using '-' as a general separator. Could it be just 'unix' to match the use of 'unix' in hostfwd ?

Quoted 13 lines
net/slirp.c +5 -0
+        if (slirp_add_unix(s->slirp, &p[20], &server, port) < 0) {
+            error_setg(errp, "Conflicting/invalid host:port in guest "
+                       "forwarding rule '%s'", config_str);
+            return -1;
+        }
     } else {
         Error *err = NULL;
         /*
qemu-options.hx +0 -0
index 5fe9d73eaa..062cd39aa3 100644

qemu-options.hx SRST · line 3559

Quoted 7 lines
qemu-options.hx +4 -0
@@ -3553,6 +3553,15 @@ SRST
             # and connect the TCP stream to its stdin/stdout
             |qemu_system| -nic  'user,id=n1,guestfwd=tcp:10.0.2.100:1234-cmd:netcat 10.10.1.1 4321'
 
+    ``guestfwd=[tcp]:server:port-unix-per-connection:path``
+        With ``unix-per-connection:``, a new connection is opened to the
+        given unix socket per TCP connection made by the guest. The unix socket
+        need not exist at startup time.

I think the existing docs probably benefit from an update to better show how the new parmeter is different from what I would think was already the semantics.

Quoted 9 lines
qemu-options.hx +5 -0
+
+        .. parsed-literal::
+
+            |qemu_system| -nic user,guestfwd=tcp:10.0.2.100:1234-unix-per-connection:/tmp/socket
+
 ``-netdev tap,id=id[,fd=h][,ifname=name][,script=file][,downscript=dfile][,br=bridge][,helper=helper]``
     Configure a host TAP network backend with ID id.
 
Quoted 4 lines

base-commit: 71f25c75417d31c214ba2cd26b273c114fc99987

Signature · 1 line
2.55.0

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com       ~~        https://hachyderm.io/@berrange :|
|: https://libvirt.org          ~~          https://entangle-photo.org :|
|: https://pixelfed.art/berrange   ~~    https://fstop138.berrange.com :|
#3 lore raw

Hello,

Daniel P. Berrangé, le jeu. 12 nov. 2026 15:21:20 +0000, a ecrit:

Quoted 10 lines · Gavin Li
Quoted 5 lines

For -netdev user,guestfwd=..., add support for unix-per-connection: in addition to the currently supported tcp: and cmd:. With unix-per-connection:, a new connection is opened to the given unix socket. This is an improvement over the current unix: support, which does not support 1:1 guest:host connections.

I'm not familiar with guestfwd, but from the manpage, I would rather assume that it always opens a new connection to the host socket for each guest connection.

No, qemu opens the unix socket at initialization and just forwards data to it.

Quoted 2 lines

What are its current semantics when two guest connections are initiated on the same port, if that's not happening ?

It's just mixing the data coming from the connections into the unix connection, and sending all data from the unix connection to one of the tcp connection.

Quoted 11 lines

net/slirp.c static int slirp_guestfwd(SlirpState *s, const char *config_str, Error **errp) · line 1162

Quoted 9 lines
net/slirp.c +1 -0
index 451c0fd1ba..b0b03b2bf5 100644
@@ -1159,6 +1159,12 @@ static int slirp_guestfwd(SlirpState *s, const char *config_str, Error **errp)
                        "forwarding rule '%s'", config_str);
             return -1;
         }
+    } else if (g_str_has_prefix(p, "unix-per-connection:")) {

This is rather a mouthful as a parameter name,

It seemed a bit long to me too.

Quoted 2 lines

and including '-' in the name is undesirable, since we're already using '-' as a general separator.

That's indeed a no-go.

Quoted 1 line

Could it be just 'unix' to match the use of 'unix' in hostfwd ?

That would prevent from being able to get the one-unix-connection behavior.

Samuel

Quoted 38 lines
Quoted 13 lines
net/slirp.c +5 -0
+        if (slirp_add_unix(s->slirp, &p[20], &server, port) < 0) {
+            error_setg(errp, "Conflicting/invalid host:port in guest "
+                       "forwarding rule '%s'", config_str);
+            return -1;
+        }
     } else {
         Error *err = NULL;
         /*
qemu-options.hx +0 -0
index 5fe9d73eaa..062cd39aa3 100644

qemu-options.hx SRST · line 3559

Quoted 7 lines
qemu-options.hx +4 -0
@@ -3553,6 +3553,15 @@ SRST
             # and connect the TCP stream to its stdin/stdout
             |qemu_system| -nic  'user,id=n1,guestfwd=tcp:10.0.2.100:1234-cmd:netcat 10.10.1.1 4321'
 
+    ``guestfwd=[tcp]:server:port-unix-per-connection:path``
+        With ``unix-per-connection:``, a new connection is opened to the
+        given unix socket per TCP connection made by the guest. The unix socket
+        need not exist at startup time.

I think the existing docs probably benefit from an update to better show how the new parmeter is different from what I would think was already the semantics.

Quoted 13 lines
qemu-options.hx +5 -0
+
+        .. parsed-literal::
+
+            |qemu_system| -nic user,guestfwd=tcp:10.0.2.100:1234-unix-per-connection:/tmp/socket
+
 ``-netdev tap,id=id[,fd=h][,ifname=name][,script=file][,downscript=dfile][,br=bridge][,helper=helper]``
     Configure a host TAP network backend with ID id.
 

base-commit: 71f25c75417d31c214ba2cd26b273c114fc99987

Signature · 1 line
2.55.0
author
#4 lore raw

Thanks for the review all. I've posted a v2 patch that shortens "unix-per-connection" to "unix2" and updates the commit message for clarity.

Gavin

Quoted 89 lines · Samuel Thibault

Hello,

Daniel P. Berrangé, le jeu. 12 nov. 2026 15:21:20 +0000, a ecrit:

Quoted 10 lines · Gavin Li
Quoted 5 lines

For -netdev user,guestfwd=..., add support for unix-per-connection: in addition to the currently supported tcp: and cmd:. With unix-per-connection:, a new connection is opened to the given unix socket. This is an improvement over the current unix: support, which does not support 1:1 guest:host connections.

I'm not familiar with guestfwd, but from the manpage, I would rather assume that it always opens a new connection to the host socket for each guest connection.

No, qemu opens the unix socket at initialization and just forwards data to it.

Quoted 2 lines

What are its current semantics when two guest connections are initiated on the same port, if that's not happening ?

It's just mixing the data coming from the connections into the unix connection, and sending all data from the unix connection to one of the tcp connection.

Quoted 11 lines

net/slirp.c static int slirp_guestfwd(SlirpState *s, const char *config_str, Error **errp) · line 1162

Quoted 9 lines
net/slirp.c +1 -0
index 451c0fd1ba..b0b03b2bf5 100644
@@ -1159,6 +1159,12 @@ static int slirp_guestfwd(SlirpState *s, const char *config_str, Error **errp)
                        "forwarding rule '%s'", config_str);
             return -1;
         }
+    } else if (g_str_has_prefix(p, "unix-per-connection:")) {

This is rather a mouthful as a parameter name,

It seemed a bit long to me too.

Quoted 2 lines

and including '-' in the name is undesirable, since we're already using '-' as a general separator.

That's indeed a no-go.

Quoted 1 line

Could it be just 'unix' to match the use of 'unix' in hostfwd ?

That would prevent from being able to get the one-unix-connection behavior.

Samuel

Quoted 38 lines
Quoted 13 lines
net/slirp.c +5 -0
+        if (slirp_add_unix(s->slirp, &p[20], &server, port) < 0) {
+            error_setg(errp, "Conflicting/invalid host:port in guest "
+                       "forwarding rule '%s'", config_str);
+            return -1;
+        }
     } else {
         Error *err = NULL;
         /*
qemu-options.hx +0 -0
index 5fe9d73eaa..062cd39aa3 100644

qemu-options.hx SRST · line 3559

Quoted 7 lines
qemu-options.hx +4 -0
@@ -3553,6 +3553,15 @@ SRST
             # and connect the TCP stream to its stdin/stdout
             |qemu_system| -nic  'user,id=n1,guestfwd=tcp:10.0.2.100:1234-cmd:netcat 10.10.1.1 4321'
 
+    ``guestfwd=[tcp]:server:port-unix-per-connection:path``
+        With ``unix-per-connection:``, a new connection is opened to the
+        given unix socket per TCP connection made by the guest. The unix socket
+        need not exist at startup time.

I think the existing docs probably benefit from an update to better show how the new parmeter is different from what I would think was already the semantics.

Quoted 13 lines
qemu-options.hx +5 -0
+
+        .. parsed-literal::
+
+            |qemu_system| -nic user,guestfwd=tcp:10.0.2.100:1234-unix-per-connection:/tmp/socket
+
 ``-netdev tap,id=id[,fd=h][,ifname=name][,script=file][,downscript=dfile][,br=bridge][,helper=helper]``
     Configure a host TAP network backend with ID id.
 

base-commit: 71f25c75417d31c214ba2cd26b273c114fc99987 --
2.55.0