ipv6 and autoport patches.
v2 cover ipv6 and autoport patches.
Hi,
This patch series starts adding support for auto-allocating tcp ports and for ipv6 to qemu. It adds a new "info chardev" command (needed to query the auto-allocated ports), adds socket helper functiond with ipv6 support and finally switches chardevs and vnc over to the new socket helpers. Individual patches have more vebose descriptions.
New in v2:
* address review comments from Daniel P. Berrange and Blue Swirl.
* fixed windows build (mingw32 crosscompiler).
* added -ipv4 and -ipv6 switches to the help text.
cheers,
Gerd
PATCH v2 1/4 Implement "info chardev" command.
This patch makes qemu keep track of the character devices in use and implements a "info chardev" monitor command to print a list.
qemu_chr_open() sticks the devices into a linked list now. It got a new argument (label), so there is a name for each device. It also assigns a filename to each character device. By default it just copyes the filename passed in. Individual drivers can fill in something else though. qemu_chr_open_pty() sets the filename to name of the pseudo tty allocated.
Output looks like this:
(qemu) info chardev
monitor: filename=unix:/tmp/run.sh-26827/monitor,server,nowait
serial0: filename=unix:/tmp/run.sh-26827/console,server
serial1: filename=pty:/dev/pts/5
parallel0: filename=vc:640x480
--- gdbstub.c | 2 +- hw/usb-serial.c | 5 ++- monitor.c | 2 + qemu-char.h | 7 +++- vl.c | 98 ++++++++++++++++++++++++++++++++++++------------------ 5 files changed, 78 insertions(+), 36 deletions(-)
gdbstub.c +1 -1
@@ -1852,7 +1852,7 @@ int gdbserver_start(const char *port) port = gdbstub_port_name; } - chr =qemu_chr_open (port); + chr =qemu_chr_open ("gdb" , port);if (!chr)return -1 ;
hw/usb-serial.c +4 -1
@@ -521,6 +521,8 @@ USBDevice *usb_serial_init(const char *filename)USBSerialState *s;CharDriverState *cdrv;unsigned short vendorid =0x0403 , productid =0x6001 ; +char label[32 ]; +static int index;while (*filename && *filename !=':' ) {const char *p;
@@ -555,7 +557,8 @@ USBDevice *usb_serial_init(const char *filename)if (!s)return NULL ; - cdrv =qemu_chr_open (filename); +snprintf (label,sizeof (label),"usbserial%d" , index++); + cdrv =qemu_chr_open (label, filename);if (!cdrv) goto fail; s->cs = cdrv;
monitor.c +2 -0
@@ -1470,6 +1470,8 @@ static const term_cmd_t info_cmds[] = { "", "show the version of qemu" }, {"network" ,"" , do_info_network, "", "show the network state" }, + { "chardev", "", qemu_chr_info, + "", "show the character devices" }, { "block", "", do_info_block, "", "show the block devices" }, { "blockstats", "", do_info_blockstats,
qemu-char.h +6 -1
@@ -1,6 +1,7 @@#ifndef QEMU_CHAR_H #define QEMU_CHAR_H +#include "sys-queue.h" /* character device */ #define CHR_EVENT_BREAK 0/* serial break char */
@@ -55,9 +56,12 @@ struct CharDriverState {void *opaque;int focus;QEMUBH *bh; +char *label; +char *filename; +TAILQ_ENTRY (CharDriverState) next; }; -CharDriverState *qemu_chr_open (const char *filename); +CharDriverState *qemu_chr_open (const char *label,const char *filename);void qemu_chr_close (CharDriverState *chr);void qemu_chr_printf (CharDriverState *s,const char *fmt, ...);int qemu_chr_write (CharDriverState *s,const uint8_t *buf,int len);
@@ -72,6 +76,7 @@ void qemu_chr_reset(CharDriverState *s);int qemu_chr_can_read (CharDriverState *s);void qemu_chr_read (CharDriverState *s,uint8_t *buf,int len);void qemu_chr_accept_input (CharDriverState *s); +void qemu_chr_info (void );/* async I/O support */
vl.c +65 -33
189 lines not on this page · raw message
Signature · 1 line
1.5.6.5
Anthony Liguori
AppliedRe: [Qemu-devel] [PATCH 1/4] Implement "info chardev" command.
Quoted 12 lines · Gerd Hoffmann
This patch makes qemu keep track of the character devices in use and implements a "info chardev" monitor command to print a list.
qemu_chr_open()sticks the devices into a linked list now. It got a new argument (label), so there is a name for each device. It also assigns a filename to each character device. By default it just copyes the filename passed in. Individual drivers can fill in something else though.qemu_chr_open_pty()sets the filename to name of the pseudo tty allocated.Output looks like this:
Quoted 8 lines
(qemu) info chardev
monitor: filename=unix:/tmp/run.sh-26827/monitor,server,nowait
serial0: filename=unix:/tmp/run.sh-26827/console,server
serial1: filename=pty:/dev/pts/5
parallel0: filename=vc:640x480Signed-off-by Gerd Hoffmann <[email protected]>
Applied. Thanks.
Regards,
Anthony Liguori
PATCH v2 2/4 sockets: helper functions for qemu.
In reply to Gerd HoffmannThis patch creates a new source and header file qemu-sockets.[ch] with a bunch of helper functions to create listening and connected sockets.
New features of this code are (a) support for searching for a free port in a given range and (b) support for IPv6.
The following patches put that code into use.
--- Makefile.target | 1 + qemu-sockets.c | 409 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ qemu-sockets.h | 8 + vl.c | 16 ++ 4 files changed, 434 insertions(+), 0 deletions(-) create mode 100644 qemu-sockets.c create mode 100644 qemu-sockets.h
Makefile.target +1 -0
@@ -580,6 +580,7 @@ ifndef CONFIG_USER_ONLY OBJS=vl.o osdep.o monitor.o pci.o loader.o isa_mmio.o machine.o net-checksum.o OBJS+=fw_cfg.o aio.o buffered_file.o migration.o migration-tcp.o +OBJS+=qemu-sockets.o ifdef CONFIG_WIN32 OBJS+=block-raw-win32.o else
qemu-sockets.c new file +409 -0
409 lines not on this page · raw message
qemu-sockets.h new file +8 -0
@@ -0,0 +1,8 @@ +int inet_listen (const char *str,char *ostr,int olen, +int socktype,int port_offset); +int inet_connect (const char *str,int socktype); +void inet_set_ipv4 (void ); +void inet_set_ipv6 (void ); + +int unix_listen (const char *path,char *ostr,int olen); +int unix_connect (const char *path);
vl.c +16 -0
@@ -37,6 +37,7 @@#include "gdbstub.h" #include "qemu-timer.h" #include "qemu-char.h" +#include "qemu-sockets.h" #include "block.h" #include "audio/audio.h" #include "migration.h"
@@ -8280,6 +8281,9 @@ static void help(int exitcode)"-redir [tcp|udp]:host-port:[guest-host]:guest-port\n" " redirect TCP or UDP connections from host to guest [-net user]\n"#endif +"-ipv4 Use IPv4 only.\n" +"-ipv6 Use UPv6 only.\n" +" By default qemu uses what is available on your machine.\n" "\n" "Linux boot specific:\n" "-kernel bzImage use 'bzImage' as kernel image\n"
@@ -8386,6 +8390,8 @@ enum { QEMU_OPTION_bootp, QEMU_OPTION_smb, QEMU_OPTION_redir, + QEMU_OPTION_ipv4, + QEMU_OPTION_ipv6, QEMU_OPTION_kernel, QEMU_OPTION_append,
@@ -8489,6 +8495,10 @@ static const QEMUOption qemu_options[] = { #endif { "redir", HAS_ARG, QEMU_OPTION_redir }, #endif + { "4", 0, QEMU_OPTION_ipv4 }, + { "6", 0, QEMU_OPTION_ipv6 }, + { "ipv4", 0, QEMU_OPTION_ipv4 }, + { "ipv6", 0, QEMU_OPTION_ipv6 }, { "kernel", HAS_ARG, QEMU_OPTION_kernel }, { "append", HAS_ARG, QEMU_OPTION_append },
@@ -9151,6 +9161,12 @@ int main(int argc, char **argv)net_slirp_redir (optarg);break ;#endif +case QEMU_OPTION_ipv4: +inet_set_ipv4 (); +break ; +case QEMU_OPTION_ipv6: +inet_set_ipv6 (); +break ;#ifdef HAS_AUDIO case QEMU_OPTION_audio_help:AUD_help ();
Signature · 1 line
1.5.6.5
Re: [Qemu-devel] [PATCH 2/4] sockets: helper functions for qemu.
Quoted 8 lines · Gerd Hoffmann
This patch creates a new source and header file qemu-sockets.[ch] with a bunch of helper functions to create listening and connected sockets.
New features of this code are (a) support for searching for a free port in a given range and (b) support for IPv6.
The following patches put that code into use.
I like this patch, but we already have a qemu_socket.h. Please remove qemu_socket.h if you're going to introduce qemu-sockets.h.
qemu-sockets.c line 211
Quoted 4 lines
qemu-sockets.c new file +3
-0+ +listen: +if (0 !=listen (slisten,1 )) {
Please try to avoid this style of if().
Quoted 32 lines
vl.c +6
-0@@ -37,6 +37,7 @@#include "gdbstub.h" #include "qemu-timer.h" #include "qemu-char.h" +#include "qemu-sockets.h" #include "block.h" #include "audio/audio.h" #include "migration.h" @@ -8280,6 +8281,9 @@ static void help(int exitcode)"-redir [tcp|udp]:host-port:[guest-host]:guest-port\n" " redirect TCP or UDP connections from host to guest [-net user]\n"#endif +"-ipv4 Use IPv4 only.\n" +"-ipv6 Use UPv6 only.\n" +" By default qemu uses what is available on your machine.\n" "\n" "Linux boot specific:\n" "-kernel bzImage use 'bzImage' as kernel image\n" @@ -8386,6 +8390,8 @@ enum { QEMU_OPTION_bootp, QEMU_OPTION_smb, QEMU_OPTION_redir, + QEMU_OPTION_ipv4, + QEMU_OPTION_ipv6, QEMU_OPTION_kernel, QEMU_OPTION_append,
vl.c static const QEMUOption qemu_options[] = { · line 8501
Quoted 7 lines
vl.c +4
-0@@ -8489,6 +8495,10 @@ static const QEMUOption qemu_options[] = { #endif { "redir", HAS_ARG, QEMU_OPTION_redir }, #endif + { "4", 0, QEMU_OPTION_ipv4 }, + { "6", 0, QEMU_OPTION_ipv6 }, + { "ipv4", 0, QEMU_OPTION_ipv4 }, + { "ipv6", 0, QEMU_OPTION_ipv6 },
I don't like the idea of aliasing these options. Please just stick with one set of options.
And do we really need to have options for this? Can't we just do the right thing? I can't believe that every application has to have an ipv6 switch to be ipv6 enabled.
Regards,
Anthony Liguori
Re: [Qemu-devel] [PATCH 2/4] sockets: helper functions for qemu.
Quoted 34 lines · Anthony Liguori
Quoted 22 lines · Gerd Hoffmann
vl.c +3
-0@@ -8280,6 +8281,9 @@ static void help(int exitcode) "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n" " redirect TCP or UDP connections from host to guest [-net user]\n"#endif +"-ipv4 Use IPv4 only.\n" +"-ipv6 Use UPv6 only.\n" + " By default qemu uses what is available onyour machine.\n"
"\n"
"Linux boot specific:\n"
"-kernel bzImage use 'bzImage' as kernel image\n"vl.c +2
-0@@ -8386,6 +8390,8 @@ enum { QEMU_OPTION_bootp, QEMU_OPTION_smb, QEMU_OPTION_redir, + QEMU_OPTION_ipv4, + QEMU_OPTION_ipv6, QEMU_OPTION_kernel, QEMU_OPTION_append,vl.c static const QEMUOption qemu_options[] = { · line 8501
Quoted 7 lines
vl.c +4
-0@@ -8489,6 +8495,10 @@ static const QEMUOption qemu_options[] = { #endif { "redir", HAS_ARG, QEMU_OPTION_redir }, #endif + { "4", 0, QEMU_OPTION_ipv4 }, + { "6", 0, QEMU_OPTION_ipv6 }, + { "ipv4", 0, QEMU_OPTION_ipv4 }, + { "ipv6", 0, QEMU_OPTION_ipv6 },I don't like the idea of aliasing these options. Please just stick with one set of options.
Quoted 3 lines
And do we really need to have options for this? Can't we just do the right thing? I can't believe that every application has to have an ipv6 switch to be ipv6 enabled.
Gerd's patch does 'do the right thing' for 90% of people - it uses the AI_ADDRCONFIG flag. This means that the getaddrinfo() name lookup will only return an IPv6 address if there is at least one NIC with IPv6 configured, likewise only return an IPv4 address if there is at least one NIC with IPv4 configured. There are still times when it is desirable to restrict it to just IPv4 or just IPv6, regardless of your host NIC config - mostly when needing to interact with other broken apps which are not correctly IPv6 aware. Maybe it would be sufficient to just have it added as a flag to the arg in question though, eg -serial tcp:localhost:123,ipv4 rather than a global -ipv4 flag
Daniel
Signature · 4 lines
|: Red Hat, Engineering, London -o- http://people.redhat.com/berrange/ :| |: http://libvirt.org -o- http://virt-manager.org -o- http://ovirt.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: GnuPG: 7D3B9505 -o- F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :|
Re: [Qemu-devel] [PATCH 2/4] sockets: helper functions for qemu.
Quoted 13 lines · Daniel P. Berrange
On Fri, Oct 31, 2008 at 12:37:16PM -0500, Anthony Liguori wrote:
Gerd's patch does 'do the right thing' for 90% of people - it uses the
AI_ADDRCONFIGflag. This means that thegetaddrinfo()name lookup will only return an IPv6 address if there is at least one NIC with IPv6 configured, likewise only return an IPv4 address if there is at least one NIC with IPv4 configured. There are still times when it is desirable to restrict it to just IPv4 or just IPv6, regardless of your host NIC config - mostly when needing to interact with other broken apps which are not correctly IPv6 aware. Maybe it would be sufficient to just have it added as a flag to the arg in question though, eg -serial tcp:localhost:123,ipv4 rather than a global -ipv4 flag
Yes, this would be much nicer. I think having the options as a global is likely to confuse users.
Regards,
Anthony Liguori
Quoted 2 lines
Daniel
Re: [Qemu-devel] [PATCH 2/4] sockets: helper functions for qemu.
Quoted 7 lines · Anthony Liguori
Quoted 4 lines
Maybe it would be sufficient to just have it added as a flag to the arg in question though, eg -serial tcp:localhost:123,ipv4 rather than a global -ipv4 flag
Yes, this would be much nicer. I think having the options as a global is likely to confuse users.
Yes. In those few cases where you need to restrict it, a global option would be wrong too: if you have multiple interfaces, and you need to override the automatic default for some reason, there's a fair chance you don't need that on every interface.
-- Jamie
Re: [Qemu-devel] [PATCH 2/4] sockets: helper functions for qemu.
In reply to Anthony LiguoriQuoted 2 lines · Anthony Liguori
I like this patch, but we already have a
qemu_socket.h. Please removeqemu_socket.hif you're going to introduceqemu-sockets.h.
Will fix.
Quoted 5 lines
qemu-sockets.c line 211
Quoted 3 lines
qemu-sockets.c new file +3
-0+ +listen: +if (0 !=listen (slisten,1 )) {Please try to avoid this style of
if().
Yea, old habit from the days where gcc didn't warn on "if (a = -1)". Pointless these days. Trying to get rid of it ...
Quoted 7 lines
vl.c static const QEMUOption qemu_options[] = { · line 8501
Quoted 2 lines
vl.c +2
-0+ { "ipv4", 0, QEMU_OPTION_ipv4 }, + { "ipv6", 0, QEMU_OPTION_ipv6 },I don't like the idea of aliasing these options. Please just stick with one set of options.
And do we really need to have options for this?
Yes, we do, unfortunaly.
Quoted 3 lines
Can't we just do the
right thing? I can't believe that every application has to have an ipv6 switch to be ipv6 enabled.
98% of the users should never ever need that. Unfortunaly there are always corner cases where you can't get it right automatically. You can easily check the ipv6 setup on the local machine, but you still don't know how the network is setup and whenever reaching machine $foo via ipv6 actually works. Of course you can just try and in case it doesn't work fallback to ipv4. Which is what the code does btw. But that involves a noticeable delay, waiting for the ipv6 connect attempt time out.
Will make that a per-connection option as discussed further down this thread.
Respin of the patches will follow later today or tomorrow.
cheers,
Gerd
PATCH v2 3/4 sockets: switch vnc to new code, support vnc port auto-allocation.
In reply to Gerd HoffmannThis patch switches the vnc code ofer to the new socket helper functions.
It adds support IPv6 support and for automatically allocating an unused vnc display port. The latter is handled ising a to= option, specifying the upper limit for the display number to try. Scanning is started at the display number given in the display specification, i.e. this command line:
-vnc localhost:7,to=11
will try displays 7 to 11 (inclusive).
The display actually allocated can be queried using the "info vnc" monitor command.
--- vl.c | 1 + vnc.c | 114 +++++++++++++++------------------------------------------------- 2 files changed, 28 insertions(+), 87 deletions(-)
vl.c +1 -0
@@ -8284,6 +8284,7 @@ static void help(int exitcode)"-ipv4 Use IPv4 only.\n" "-ipv6 Use UPv6 only.\n" " By default qemu uses what is available on your machine.\n" +" Applies to -vnc.\n" "\n" "Linux boot specific:\n" "-kernel bzImage use 'bzImage' as kernel image\n"
vnc.c +27 -87
158 lines not on this page · raw message
Signature · 1 line
1.5.6.5
PATCH v2 4/4 sockets: switch over tcp/telnet/unix serial line to new helper functions.
In reply to Gerd HoffmannThis switches the tcp, telnet and unix socket support for character devices (serial/parallel, ...) to the new socket helpers. Thereby they gain IPv6 support and also get ability to search for a free tcp port. Syntax is the same as for vnc, using a to= option, like this:
-serial tcp:localhost:5000,to=5099,server
This will check the 5000 -> 5099 port range (inclusive) for a free tcp port. Likewise you can get auto-allocated unix sockets by specifying an empty path:
-serial unix:,server
qemu will create a randomly named socket in $TMPDIR then. You can use the "info chardev" command added by the first patch in this series to figure the tcp port / unix socket actually allocated.
--- vl.c | 131 +++++++++++++++++------------------------------------------------- 1 files changed, 33 insertions(+), 98 deletions(-)
vl.c +33 -98
@@ -3370,9 +3370,6 @@ static void udp_chr_update_read_handler(CharDriverState *chr) } } -#ifndef _WIN32 -static int parse_unix_path (struct sockaddr_un *uaddr,const char *str); -#endif int parse_host_src_port (struct sockaddr_in *haddr,struct sockaddr_in *saddr,const char *str);
@@ -3629,32 +3626,11 @@ static CharDriverState *qemu_chr_open_tcp(const char *host_str, {CharDriverState *chr =NULL ;TCPCharDriver *s =NULL ; -int fd =-1 , ret, err, val; +int fd =-1 , offset =0 ;int is_listen =0 ;int is_waitconnect =1 ;int do_nodelay =0 ;const char *ptr; -struct sockaddr_in saddr; -#ifndef _WIN32 -struct sockaddr_un uaddr; -#endif -struct sockaddr *addr; -socklen_t addrlen; - -#ifndef _WIN32 -if (is_unix) { - addr = (struct sockaddr *)&uaddr; - addrlen =sizeof (uaddr); -if (parse_unix_path (&uaddr, host_str) <0 ) - goto fail; - } else -#endif - { - addr = (struct sockaddr *)&saddr; - addrlen =sizeof (saddr); -if (parse_host_port (&saddr, host_str) <0 ) - goto fail; - } ptr = host_str;while ((ptr =strchr (ptr,',' ))) {
@@ -3665,6 +3641,8 @@ static CharDriverState *qemu_chr_open_tcp(const char *host_str, is_waitconnect =0 ; }else if (!strncmp (ptr,"nodelay" ,6 )) { do_nodelay =1 ; + }else if (!strncmp (ptr,"to=" ,3 )) { +/* nothing, inet_listen() parses this one */ ; }else {printf ("Unknown option: %s\n" , ptr); goto fail;
@@ -3680,13 +3658,31 @@ static CharDriverState *qemu_chr_open_tcp(const char *host_str,if (!s) goto fail; -#ifndef _WIN32 -if (is_unix) - fd =socket (PF_UNIX ,SOCK_STREAM ,0 ); - else -#endif - fd =socket (PF_INET ,SOCK_STREAM ,0 ); - +if (is_listen) { + chr->filename =qemu_malloc (256 ); +if (is_unix) { +strcpy (chr->filename,"unix:" ); + }else if (is_telnet) { +strcpy (chr->filename,"telnet:" ); + }else { +strcpy (chr->filename,"tcp:" ); + } + offset =strlen (chr->filename); + } +if (is_unix) { +if (is_listen) { + fd =unix_listen (host_str, chr->filename + offset,256 - offset); + }else { + fd =unix_connect (host_str); + } + }else { +if (is_listen) { + fd =inet_listen (host_str, chr->filename + offset,256 - offset, +SOCK_STREAM ,0 ); + }else { + fd =inet_connect (host_str,SOCK_STREAM ); + } + }if (fd <0 ) goto fail;
@@ -3704,61 +3700,20 @@ static CharDriverState *qemu_chr_open_tcp(const char *host_str, chr->chr_close = tcp_chr_close;if (is_listen) { -/* allow fast reuse */ -#ifndef _WIN32 -if (is_unix) { -char path[109 ]; -pstrcpy (path,sizeof (path), uaddr.sun_path); -unlink (path); - } else -#endif - { - val =1 ; -setsockopt (fd,SOL_SOCKET ,SO_REUSEADDR , (const char *)&val,sizeof (val)); - } - - ret =bind (fd, addr, addrlen); -if (ret <0 ) - goto fail; - - ret =listen (fd,0 ); -if (ret <0 ) - goto fail; - s->listen_fd = fd;qemu_set_fd_handler (s->listen_fd, tcp_chr_accept,NULL , chr);if (is_telnet) s->do_telnetopt =1 ; }else { -for (;;) { - ret =connect (fd, addr, addrlen); -if (ret <0 ) { - err =socket_error (); -if (err ==EINTR || err ==EWOULDBLOCK ) { - }else if (err ==EINPROGRESS ) { -break ; -#ifdef _WIN32 - }else if (err ==WSAEALREADY ) { -break ; -#endif - }else { - goto fail; - } - }else { - s->connected =1 ; -break ; - } - } + s->connected =1 ; s->fd = fd;socket_set_nodelay (fd); -if (s->connected) -tcp_chr_connect (chr); -else -qemu_set_fd_handler (s->fd,NULL , tcp_chr_connect, chr); +tcp_chr_connect (chr); }if (is_listen && is_waitconnect) { -printf ("QEMU waiting for connection on: %s\n" , host_str); +printf ("QEMU waiting for connection on: %s\n" , + chr->filename ? chr->filename : host_str);tcp_chr_accept (chr);socket_set_nonblock (s->listen_fd); }
@@ -4035,26 +3990,6 @@ int parse_host_port(struct sockaddr_in *saddr, const char *str)return 0 ; } -#ifndef _WIN32 -static int parse_unix_path (struct sockaddr_un *uaddr,const char *str) -{ -const char *p; -int len; - - len =MIN (108 ,strlen (str)); - p =strchr (str,',' ); -if (p) - len =MIN (len, p - str); - -memset (uaddr,0 ,sizeof (*uaddr)); - - uaddr->sun_family =AF_UNIX ; -memcpy (uaddr->sun_path, str, len); - -return 0 ; -} -#endif -/* find or alloc a new VLAN */ VLANState *qemu_find_vlan (int id) {
@@ -8284,7 +8219,7 @@ static void help(int exitcode)"-ipv4 Use IPv4 only.\n" "-ipv6 Use UPv6 only.\n" " By default qemu uses what is available on your machine.\n" -" Applies to -vnc.\n" +" Applies to -vnc and -serial tcp.\n" "\n" "Linux boot specific:\n" "-kernel bzImage use 'bzImage' as kernel image\n"
Signature · 1 line
1.5.6.5