Patchwork slirp: add 'cmd:' target for guestfwd

login
register
mail settings
Submitter Alexander Graf
Date June 4, 2011, 6:12 a.m.
Message ID <1307167972-4759-1-git-send-email-agraf@suse.de>
Download mbox | patch
Permalink /patch/98761/
State New
Headers show

Comments

Alexander Graf - June 4, 2011, 6:12 a.m.
When using guestfwd=, Qemu only connects the virtual server's TCP port
to a single chardev. This is useless in most cases, as we usually want
to have more than a single connection from the guest to the outside world.

This patch adds a new cmd: target to guestfwd= that allows for execution
of a command on every TCP connection. This leverages the same code as
the -smb parameter, just that here the command is user defined.

Reported-by: Sascha Wilde <wilde@intevation.de>
Signed-off-by: Alexander Graf <agraf@suse.de>
---
 net/slirp.c     |   44 ++++++++++++++++++++++++++++----------------
 qemu-options.hx |   20 +++++++++++++++++++-
 2 files changed, 47 insertions(+), 17 deletions(-)
Alexander Graf - May 30, 2012, 11:08 p.m.
On 04.06.2011, at 08:12, Alexander Graf wrote:

> When using guestfwd=, Qemu only connects the virtual server's TCP port
> to a single chardev. This is useless in most cases, as we usually want
> to have more than a single connection from the guest to the outside world.
> 
> This patch adds a new cmd: target to guestfwd= that allows for execution
> of a command on every TCP connection. This leverages the same code as
> the -smb parameter, just that here the command is user defined.
> 
> Reported-by: Sascha Wilde <wilde@intevation.de>
> Signed-off-by: Alexander Graf <agraf@suse.de>


Ping?


Alex

> ---
> net/slirp.c     |   44 ++++++++++++++++++++++++++++----------------
> qemu-options.hx |   20 +++++++++++++++++++-
> 2 files changed, 47 insertions(+), 17 deletions(-)
> 
> diff --git a/net/slirp.c b/net/slirp.c
> index e057a14..4e4e3c3 100644
> --- a/net/slirp.c
> +++ b/net/slirp.c
> @@ -615,25 +615,37 @@ static int slirp_guestfwd(SlirpState *s, const char *config_str,
> 
>     fwd = qemu_malloc(sizeof(struct GuestFwd));
>     snprintf(buf, sizeof(buf), "guestfwd.tcp.%d", port);
> -    fwd->hd = qemu_chr_open(buf, p, NULL);
> -    if (!fwd->hd) {
> -        error_report("could not open guest forwarding device '%s'", buf);
> -        qemu_free(fwd);
> -        return -1;
> -    }
> 
> -    if (slirp_add_exec(s->slirp, 3, fwd->hd, &server, port) < 0) {
> -        error_report("conflicting/invalid host:port in guest forwarding "
> -                     "rule '%s'", config_str);
> -        qemu_free(fwd);
> -        return -1;
> +    if ((strlen(p) > 4) && !strncmp(p, "cmd:", 4)) {
> +        if (slirp_add_exec(s->slirp, 0, &p[4], &server, port) < 0) {
> +            error_report("conflicting/invalid host:port in guest forwarding "
> +                         "rule '%s'", config_str);
> +            qemu_free(fwd);
> +            return -1;
> +        }
> +    } else {
> +        fwd->hd = qemu_chr_open(buf, p, NULL);
> +        if (!fwd->hd) {
> +            error_report("could not open guest forwarding device '%s'", buf);
> +            qemu_free(fwd);
> +            return -1;
> +        }
> +
> +        if (slirp_add_exec(s->slirp, 3, fwd->hd, &server, port) < 0) {
> +            error_report("conflicting/invalid host:port in guest forwarding "
> +                         "rule '%s'", config_str);
> +            qemu_free(fwd);
> +            return -1;
> +        }
> +
> +        fwd->server = server;
> +        fwd->port = port;
> +        fwd->slirp = s->slirp;
> +
> +        qemu_chr_add_handlers(fwd->hd, guestfwd_can_read, guestfwd_read,
> +                              NULL, fwd);
>     }
> -    fwd->server = server;
> -    fwd->port = port;
> -    fwd->slirp = s->slirp;
> 
> -    qemu_chr_add_handlers(fwd->hd, guestfwd_can_read, guestfwd_read,
> -                          NULL, fwd);
>     return 0;
> 
>  fail_syntax:
> diff --git a/qemu-options.hx b/qemu-options.hx
> index 82e085a..cc62d20 100644
> --- a/qemu-options.hx
> +++ b/qemu-options.hx
> @@ -1242,10 +1242,28 @@ telnet localhost 5555
> Then when you use on the host @code{telnet localhost 5555}, you
> connect to the guest telnet server.
> 
> -@item guestfwd=[tcp]:@var{server}:@var{port}-@var{dev}
> +@item guestfwd=[tcp]:@var{server}:@var{port}-[@var{dev}|@var{cmd:command}]
> Forward guest TCP connections to the IP address @var{server} on port @var{port}
> to the character device @var{dev}. This option can be given multiple times.
> 
> +You can either use a chardev directly and have that one used throughout Qemu's
> +lifetime, like in the following example:
> +
> +@example
> +# open 10.10.1.1:4321 on bootup, connect 10.0.2.100:1234 to it whenever
> +# the guest accesses it
> +qemu -net user,guestfwd=tcp:10.0.2.100:1234-tcp:10.10.1.1:4321 [...]
> +@end example
> +
> +Or you can execute a command on every TCP connection established by the guest,
> +so that Qemu behaves similar to an inetd process for that virtual server:
> +
> +@example
> +# call "netcat 10.10.1.1 4321" on every TCP connection to 10.0.2.100:1234
> +# and connect the TCP stream to its stdin/stdout
> +qemu -net user,guestfwd=tcp:10.0.2.100:1234-cmd:netcat 10.10.1.1 4321
> +@end example
> +
> @end table
> 
> Note: Legacy stand-alone options -tftp, -bootp, -smb and -redir are still
> -- 
> 1.6.0.2
> 
>
Anthony Liguori - May 30, 2012, 11:49 p.m.
On 05/31/2012 07:08 AM, Alexander Graf wrote:
>
> On 04.06.2011, at 08:12, Alexander Graf wrote:
>
>> When using guestfwd=, Qemu only connects the virtual server's TCP port
>> to a single chardev. This is useless in most cases, as we usually want
>> to have more than a single connection from the guest to the outside world.
>>
>> This patch adds a new cmd: target to guestfwd= that allows for execution
>> of a command on every TCP connection. This leverages the same code as
>> the -smb parameter, just that here the command is user defined.
>>
>> Reported-by: Sascha Wilde<wilde@intevation.de>
>> Signed-off-by: Alexander Graf<agraf@suse.de>
>
>
> Ping?

Jan?

Regards,

Anthony Liguori

>
>
> Alex
>
>> ---
>> net/slirp.c     |   44 ++++++++++++++++++++++++++++----------------
>> qemu-options.hx |   20 +++++++++++++++++++-
>> 2 files changed, 47 insertions(+), 17 deletions(-)
>>
>> diff --git a/net/slirp.c b/net/slirp.c
>> index e057a14..4e4e3c3 100644
>> --- a/net/slirp.c
>> +++ b/net/slirp.c
>> @@ -615,25 +615,37 @@ static int slirp_guestfwd(SlirpState *s, const char *config_str,
>>
>>      fwd = qemu_malloc(sizeof(struct GuestFwd));
>>      snprintf(buf, sizeof(buf), "guestfwd.tcp.%d", port);
>> -    fwd->hd = qemu_chr_open(buf, p, NULL);
>> -    if (!fwd->hd) {
>> -        error_report("could not open guest forwarding device '%s'", buf);
>> -        qemu_free(fwd);
>> -        return -1;
>> -    }
>>
>> -    if (slirp_add_exec(s->slirp, 3, fwd->hd,&server, port)<  0) {
>> -        error_report("conflicting/invalid host:port in guest forwarding "
>> -                     "rule '%s'", config_str);
>> -        qemu_free(fwd);
>> -        return -1;
>> +    if ((strlen(p)>  4)&&  !strncmp(p, "cmd:", 4)) {
>> +        if (slirp_add_exec(s->slirp, 0,&p[4],&server, port)<  0) {
>> +            error_report("conflicting/invalid host:port in guest forwarding "
>> +                         "rule '%s'", config_str);
>> +            qemu_free(fwd);
>> +            return -1;
>> +        }
>> +    } else {
>> +        fwd->hd = qemu_chr_open(buf, p, NULL);
>> +        if (!fwd->hd) {
>> +            error_report("could not open guest forwarding device '%s'", buf);
>> +            qemu_free(fwd);
>> +            return -1;
>> +        }
>> +
>> +        if (slirp_add_exec(s->slirp, 3, fwd->hd,&server, port)<  0) {
>> +            error_report("conflicting/invalid host:port in guest forwarding "
>> +                         "rule '%s'", config_str);
>> +            qemu_free(fwd);
>> +            return -1;
>> +        }
>> +
>> +        fwd->server = server;
>> +        fwd->port = port;
>> +        fwd->slirp = s->slirp;
>> +
>> +        qemu_chr_add_handlers(fwd->hd, guestfwd_can_read, guestfwd_read,
>> +                              NULL, fwd);
>>      }
>> -    fwd->server = server;
>> -    fwd->port = port;
>> -    fwd->slirp = s->slirp;
>>
>> -    qemu_chr_add_handlers(fwd->hd, guestfwd_can_read, guestfwd_read,
>> -                          NULL, fwd);
>>      return 0;
>>
>>   fail_syntax:
>> diff --git a/qemu-options.hx b/qemu-options.hx
>> index 82e085a..cc62d20 100644
>> --- a/qemu-options.hx
>> +++ b/qemu-options.hx
>> @@ -1242,10 +1242,28 @@ telnet localhost 5555
>> Then when you use on the host @code{telnet localhost 5555}, you
>> connect to the guest telnet server.
>>
>> -@item guestfwd=[tcp]:@var{server}:@var{port}-@var{dev}
>> +@item guestfwd=[tcp]:@var{server}:@var{port}-[@var{dev}|@var{cmd:command}]
>> Forward guest TCP connections to the IP address @var{server} on port @var{port}
>> to the character device @var{dev}. This option can be given multiple times.
>>
>> +You can either use a chardev directly and have that one used throughout Qemu's
>> +lifetime, like in the following example:
>> +
>> +@example
>> +# open 10.10.1.1:4321 on bootup, connect 10.0.2.100:1234 to it whenever
>> +# the guest accesses it
>> +qemu -net user,guestfwd=tcp:10.0.2.100:1234-tcp:10.10.1.1:4321 [...]
>> +@end example
>> +
>> +Or you can execute a command on every TCP connection established by the guest,
>> +so that Qemu behaves similar to an inetd process for that virtual server:
>> +
>> +@example
>> +# call "netcat 10.10.1.1 4321" on every TCP connection to 10.0.2.100:1234
>> +# and connect the TCP stream to its stdin/stdout
>> +qemu -net user,guestfwd=tcp:10.0.2.100:1234-cmd:netcat 10.10.1.1 4321
>> +@end example
>> +
>> @end table
>>
>> Note: Legacy stand-alone options -tftp, -bootp, -smb and -redir are still
>> --
>> 1.6.0.2
>>
>>
>
Jan Kiszka - May 31, 2012, 11 a.m.
On 2012-05-31 01:08, Alexander Graf wrote:
> 
> On 04.06.2011, at 08:12, Alexander Graf wrote:
> 
>> When using guestfwd=, Qemu only connects the virtual server's TCP port
>> to a single chardev. This is useless in most cases, as we usually want
>> to have more than a single connection from the guest to the outside world.
>>
>> This patch adds a new cmd: target to guestfwd= that allows for execution
>> of a command on every TCP connection. This leverages the same code as
>> the -smb parameter, just that here the command is user defined.
>>
>> Reported-by: Sascha Wilde <wilde@intevation.de>
>> Signed-off-by: Alexander Graf <agraf@suse.de>
> 
> 
> Ping?

No general concerns, but it no longer applies. Some first comments below.

> 
> 
> Alex
> 
>> ---
>> net/slirp.c     |   44 ++++++++++++++++++++++++++++----------------
>> qemu-options.hx |   20 +++++++++++++++++++-
>> 2 files changed, 47 insertions(+), 17 deletions(-)
>>
>> diff --git a/net/slirp.c b/net/slirp.c
>> index e057a14..4e4e3c3 100644
>> --- a/net/slirp.c
>> +++ b/net/slirp.c
>> @@ -615,25 +615,37 @@ static int slirp_guestfwd(SlirpState *s, const char *config_str,
>>
>>     fwd = qemu_malloc(sizeof(struct GuestFwd));
>>     snprintf(buf, sizeof(buf), "guestfwd.tcp.%d", port);
>> -    fwd->hd = qemu_chr_open(buf, p, NULL);
>> -    if (!fwd->hd) {
>> -        error_report("could not open guest forwarding device '%s'", buf);
>> -        qemu_free(fwd);
>> -        return -1;
>> -    }
>>
>> -    if (slirp_add_exec(s->slirp, 3, fwd->hd, &server, port) < 0) {
>> -        error_report("conflicting/invalid host:port in guest forwarding "
>> -                     "rule '%s'", config_str);
>> -        qemu_free(fwd);
>> -        return -1;
>> +    if ((strlen(p) > 4) && !strncmp(p, "cmd:", 4)) {
>> +        if (slirp_add_exec(s->slirp, 0, &p[4], &server, port) < 0) {
>> +            error_report("conflicting/invalid host:port in guest forwarding "
>> +                         "rule '%s'", config_str);
>> +            qemu_free(fwd);
>> +            return -1;
>> +        }
>> +    } else {
>> +        fwd->hd = qemu_chr_open(buf, p, NULL);
>> +        if (!fwd->hd) {
>> +            error_report("could not open guest forwarding device '%s'", buf);
>> +            qemu_free(fwd);
>> +            return -1;
>> +        }
>> +
>> +        if (slirp_add_exec(s->slirp, 3, fwd->hd, &server, port) < 0) {
>> +            error_report("conflicting/invalid host:port in guest forwarding "
>> +                         "rule '%s'", config_str);
>> +            qemu_free(fwd);
>> +            return -1;
>> +        }
>> +
>> +        fwd->server = server;
>> +        fwd->port = port;
>> +        fwd->slirp = s->slirp;
>> +
>> +        qemu_chr_add_handlers(fwd->hd, guestfwd_can_read, guestfwd_read,
>> +                              NULL, fwd);
>>     }
>> -    fwd->server = server;
>> -    fwd->port = port;
>> -    fwd->slirp = s->slirp;
>>
>> -    qemu_chr_add_handlers(fwd->hd, guestfwd_can_read, guestfwd_read,
>> -                          NULL, fwd);
>>     return 0;
>>
>>  fail_syntax:
>> diff --git a/qemu-options.hx b/qemu-options.hx
>> index 82e085a..cc62d20 100644
>> --- a/qemu-options.hx
>> +++ b/qemu-options.hx
>> @@ -1242,10 +1242,28 @@ telnet localhost 5555
>> Then when you use on the host @code{telnet localhost 5555}, you
>> connect to the guest telnet server.
>>
>> -@item guestfwd=[tcp]:@var{server}:@var{port}-@var{dev}
>> +@item guestfwd=[tcp]:@var{server}:@var{port}-[@var{dev}|@var{cmd:command}]

This implies that the destination ("@var{dev}|@var{cmd:command}") is
optional. Probably it's clearer to express this with two lines.

>> Forward guest TCP connections to the IP address @var{server} on port @var{port}
>> to the character device @var{dev}. This option can be given multiple times.

Needs updating (only refers to the chardev variant).

>>
>> +You can either use a chardev directly and have that one used throughout Qemu's
>> +lifetime, like in the following example:
>> +
>> +@example
>> +# open 10.10.1.1:4321 on bootup, connect 10.0.2.100:1234 to it whenever
>> +# the guest accesses it
>> +qemu -net user,guestfwd=tcp:10.0.2.100:1234-tcp:10.10.1.1:4321 [...]
>> +@end example
>> +
>> +Or you can execute a command on every TCP connection established by the guest,
>> +so that Qemu behaves similar to an inetd process for that virtual server:
>> +
>> +@example
>> +# call "netcat 10.10.1.1 4321" on every TCP connection to 10.0.2.100:1234
>> +# and connect the TCP stream to its stdin/stdout
>> +qemu -net user,guestfwd=tcp:10.0.2.100:1234-cmd:netcat 10.10.1.1 4321

Must likely be ...-cmd:'netcat 10.10.1.1 4321'

>> +@end example
>> +
>> @end table
>>
>> Note: Legacy stand-alone options -tftp, -bootp, -smb and -redir are still
>> -- 
>> 1.6.0.2
>>
>>
> 

Jan

Patch

diff --git a/net/slirp.c b/net/slirp.c
index e057a14..4e4e3c3 100644
--- a/net/slirp.c
+++ b/net/slirp.c
@@ -615,25 +615,37 @@  static int slirp_guestfwd(SlirpState *s, const char *config_str,
 
     fwd = qemu_malloc(sizeof(struct GuestFwd));
     snprintf(buf, sizeof(buf), "guestfwd.tcp.%d", port);
-    fwd->hd = qemu_chr_open(buf, p, NULL);
-    if (!fwd->hd) {
-        error_report("could not open guest forwarding device '%s'", buf);
-        qemu_free(fwd);
-        return -1;
-    }
 
-    if (slirp_add_exec(s->slirp, 3, fwd->hd, &server, port) < 0) {
-        error_report("conflicting/invalid host:port in guest forwarding "
-                     "rule '%s'", config_str);
-        qemu_free(fwd);
-        return -1;
+    if ((strlen(p) > 4) && !strncmp(p, "cmd:", 4)) {
+        if (slirp_add_exec(s->slirp, 0, &p[4], &server, port) < 0) {
+            error_report("conflicting/invalid host:port in guest forwarding "
+                         "rule '%s'", config_str);
+            qemu_free(fwd);
+            return -1;
+        }
+    } else {
+        fwd->hd = qemu_chr_open(buf, p, NULL);
+        if (!fwd->hd) {
+            error_report("could not open guest forwarding device '%s'", buf);
+            qemu_free(fwd);
+            return -1;
+        }
+
+        if (slirp_add_exec(s->slirp, 3, fwd->hd, &server, port) < 0) {
+            error_report("conflicting/invalid host:port in guest forwarding "
+                         "rule '%s'", config_str);
+            qemu_free(fwd);
+            return -1;
+        }
+
+        fwd->server = server;
+        fwd->port = port;
+        fwd->slirp = s->slirp;
+
+        qemu_chr_add_handlers(fwd->hd, guestfwd_can_read, guestfwd_read,
+                              NULL, fwd);
     }
-    fwd->server = server;
-    fwd->port = port;
-    fwd->slirp = s->slirp;
 
-    qemu_chr_add_handlers(fwd->hd, guestfwd_can_read, guestfwd_read,
-                          NULL, fwd);
     return 0;
 
  fail_syntax:
diff --git a/qemu-options.hx b/qemu-options.hx
index 82e085a..cc62d20 100644
--- a/qemu-options.hx
+++ b/qemu-options.hx
@@ -1242,10 +1242,28 @@  telnet localhost 5555
 Then when you use on the host @code{telnet localhost 5555}, you
 connect to the guest telnet server.
 
-@item guestfwd=[tcp]:@var{server}:@var{port}-@var{dev}
+@item guestfwd=[tcp]:@var{server}:@var{port}-[@var{dev}|@var{cmd:command}]
 Forward guest TCP connections to the IP address @var{server} on port @var{port}
 to the character device @var{dev}. This option can be given multiple times.
 
+You can either use a chardev directly and have that one used throughout Qemu's
+lifetime, like in the following example:
+
+@example
+# open 10.10.1.1:4321 on bootup, connect 10.0.2.100:1234 to it whenever
+# the guest accesses it
+qemu -net user,guestfwd=tcp:10.0.2.100:1234-tcp:10.10.1.1:4321 [...]
+@end example
+
+Or you can execute a command on every TCP connection established by the guest,
+so that Qemu behaves similar to an inetd process for that virtual server:
+
+@example
+# call "netcat 10.10.1.1 4321" on every TCP connection to 10.0.2.100:1234
+# and connect the TCP stream to its stdin/stdout
+qemu -net user,guestfwd=tcp:10.0.2.100:1234-cmd:netcat 10.10.1.1 4321
+@end example
+
 @end table
 
 Note: Legacy stand-alone options -tftp, -bootp, -smb and -redir are still