Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1EmJ3W-0004pR-G1 for qemu-devel@nongnu.org; Tue, 13 Dec 2005 17:55:34 -0500 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1EmJ3U-0004n3-Lc for qemu-devel@nongnu.org; Tue, 13 Dec 2005 17:55:34 -0500 Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1EmJ3U-0004mZ-CA for qemu-devel@nongnu.org; Tue, 13 Dec 2005 17:55:32 -0500 Received: from [200.41.44.92] (helo=install.gobernac.mendoza.gov.ar) by monty-python.gnu.org with esmtp (TLS-1.0:DHE_RSA_3DES_EDE_CBC_SHA:24) (Exim 4.34) id 1EmJ5R-0003ej-KF for qemu-devel@nongnu.org; Tue, 13 Dec 2005 17:57:34 -0500 Received: from install.gobernac.mendoza.gov.ar (localhost.localdomain [127.0.0.1]) by install.gobernac.mendoza.gov.ar (8.12.8/8.12.8) with ESMTP id jBDMspld005905 for ; Tue, 13 Dec 2005 19:54:51 -0300 Received: from mail.mendoza.gov.ar ([192.168.64.3])by install.gobernac.mendoza.gov.ar (8.12.8/8.12.8) with SMTP id jBDMso3i005900for ; Tue, 13 Dec 2005 19:54:50 -0300 Date: Tue, 13 Dec 2005 19:57:29 -0300 From: JuanJo Ciarlante Message-ID: <20051213195729.B3046@mendoza.gov.ar> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="i7F3eY7HS/tUJxUd" Content-Disposition: inline Subject: [Qemu-devel] [PATCH] QEMU multicast socket support v4 (UML mcast compatible) Reply-To: qemu-devel@nongnu.org List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org --i7F3eY7HS/tUJxUd Content-Type: multipart/mixed; boundary="qcHopEYAB45HaUaB" Content-Disposition: inline --qcHopEYAB45HaUaB Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Hi! Attached patch (against qemu CVS 2005-12-10) adds multicast support for=20 QEMU socket transport, ie: run several stateless possibly non-local QEMUs= =20 on same virtual ethernet "BUS".=20 User-mode-linux (UML) compatible (tested). An excerpt from qemu-doc.texi: `-net socket[,vlan=3Dn][,fd=3Dh][,mcast=3Dmaddr:port]' Create a VLAN n shared with another QEMU virtual machines using a UDP multicast socket, effectively making a bus for every QEMU with same multicast address maddr and port. NOTES: 1. Several QEMU can be running on different hosts and share same bus (assuming correct multicast setup for these hosts). 2. mcast support is compatible with User Mode Linux (argument `ethN=3Dmcast'), see http://user-mode-linux.sf.net. Example: # launch one QEMU instance qemu linux.img -net nic,macaddr=3D52:54:00:12:34:56 -net socket,mcast=3D23= 0.0.0.1:1234 # launch another QEMU instance on same "bus" qemu linux.img -net nic,macaddr=3D52:54:00:12:34:57 -net socket,mcast=3D23= 0.0.0.1:1234 # launch yet another QEMU instance on same "bus" qemu linux.img -net nic,macaddr=3D52:54:00:12:34:58 -net socket,mcast=3D23= 0.0.0.1:1234 Example (User Mode Linux compat.): # launch QEMU instance (note mcast address selected is UML's default) qemu linux.img -net nic,macaddr=3D52:54:00:12:34:56 -net socket,mcast=3D23= 9.192.168.1:1102 # launch UML /path/to/linux ubd0=3D/path/to/root_fs eth0=3Dmcast Available under GPLv2 from http://www.irrigacion.gov.ar/juanjo/qemu/ Changes ------- v4 (2005-12-12): * Tested mcast user-mode-linux compatibility * Minor tweaks v3 (2005-12-07): * correctly close fds if any socket syscall fails (ie. close'em don't leak= 'em ;) v2 (2005-12-07): * 1st public release =09 EnjoY=09 --=20 --Juanjo # Juan Jose Ciarlante (JuanJo) jjo ;at; mendoza.gov.ar = # # GnuPG Public Key: gpg --keyserver wwwkeys.eu.pgp.net --recv-key 66727177= # # Key fingerprint: 0D2F 3E5D 8B5C 729E 0560 F453 A3F7 E249 6672 7177 = # --qcHopEYAB45HaUaB Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="qemu-socket_mcast.jjo.v4.diff" Content-Transfer-Encoding: quoted-printable diff --git a/README.qemu-mcast b/README.qemu-mcast new file mode 100644 index 0000000..4021be5 --- /dev/null +++ b/README.qemu-mcast @@ -0,0 +1,56 @@ +QEMU multicast support +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D +Author: JuanJo Ciarlante jjo()um.edu.ar, juanjosec()gmail.com +Date: Mon Dec 12 12:38:11 ART 2005 +Keywords: qemu, socket, multicast, UDP, mcast, UML + +Attached patch (against qemu CVS 2005-12-10) adds multicast support for=20 +QEMU socket transport, ie: run several stateless possibly non-local QEMUs= =20 +on same virtual ethernet "BUS".=20 +User-mode-linux (UML) compatible (tested). + +An excerpt from qemu-doc.texi: + + `-net socket[,vlan=3Dn][,fd=3Dh][,mcast=3Dmaddr:port]' + Create a VLAN n shared with another QEMU virtual machines using + a UDP multicast socket, effectively making a bus for every QEMU + with same multicast address maddr and port. NOTES: + 1. Several QEMU can be running on different hosts and share same + bus (assuming correct multicast setup for these hosts). + 2. mcast support is compatible with User Mode Linux (argument + `ethN=3Dmcast'), see http://user-mode-linux.sf.net. + Example: + # launch one QEMU instance + qemu linux.img -net nic,macaddr=3D52:54:00:12:34:56 -net socket,mcast=3D2= 30.0.0.1:1234 + # launch another QEMU instance on same "bus" + qemu linux.img -net nic,macaddr=3D52:54:00:12:34:57 -net socket,mcast=3D2= 30.0.0.1:1234 + # launch yet another QEMU instance on same "bus" + qemu linux.img -net nic,macaddr=3D52:54:00:12:34:58 -net socket,mcast=3D2= 30.0.0.1:1234 + Example (User Mode Linux compat.): + # launch QEMU instance (note mcast address selected is UML's default) + qemu linux.img -net nic,macaddr=3D52:54:00:12:34:56 -net socket,mcast=3D2= 39.192.168.1:1102 + # launch UML + /path/to/linux ubd0=3D/path/to/root_fs eth0=3Dmcast + +Available under GPLv2 from + http://www.irrigacion.gov.ar/juanjo/qemu/ + + +Changes +------- +v4 (2005-12-12): + * Tested mcast user-mode-linux compatibility + * Minor tweaks + +v3 (2005-12-07): + * correctly close fds if any socket syscall fails (ie. close'em don't lea= k'em ;) + +v2 (2005-12-07): + * 1st public release + + +--Juanjo + +# Juan Jose Ciarlante (JuanJo) jjo ;at; mendoza.gov.ar = # +# GnuPG Public Key: gpg --keyserver wwwkeys.eu.pgp.net --recv-key 6672717= 7 # +# Key fingerprint: 0D2F 3E5D 8B5C 729E 0560 F453 A3F7 E249 6672 7177 = # diff --git a/qemu-doc.texi b/qemu-doc.texi index 7ec41f5..d630adf 100644 --- a/qemu-doc.texi +++ b/qemu-doc.texi @@ -293,6 +293,41 @@ qemu linux.img -net nic,macaddr=3D52:54:00 qemu linux.img -net nic,macaddr=3D52:54:00:12:34:57 -net socket,connect=3D= 127.0.0.1:1234 @end example =20 +@item -net socket[,vlan=3Dn][,fd=3Dh][,mcast=3Dmaddr:port] + +Create a VLAN @var{n} shared with another QEMU virtual +machines using a UDP multicast socket, effectively making a bus for=20 +every QEMU with same multicast address @var{maddr} and @var{port}. +NOTES: +@enumerate +@item=20 +Several QEMU can be running on different hosts and share same bus (assumin= g=20 +correct multicast setup for these hosts). +@item +mcast support is compatible with User Mode Linux (argument @option{eth@var= {N}=3Dmcast}), see +@url{user-mode-linux.sf.net}. +@end enumerate + + +Example: +@example +# launch one QEMU instance +qemu linux.img -net nic,macaddr=3D52:54:00:12:34:56 -net socket,mcast=3D23= 0.0.0.1:1234 +# launch another QEMU instance on same "bus" +qemu linux.img -net nic,macaddr=3D52:54:00:12:34:57 -net socket,mcast=3D23= 0.0.0.1:1234 +# launch yet another QEMU instance on same "bus" +qemu linux.img -net nic,macaddr=3D52:54:00:12:34:58 -net socket,mcast=3D23= 0.0.0.1:1234 +@end example + +Example (User Mode Linux compat.): +@example +# launch QEMU instance (note mcast address selected is UML's default) +qemu linux.img -net nic,macaddr=3D52:54:00:12:34:56 -net socket,mcast=3D23= 9.192.168.1:1102 +# launch UML +/path/to/linux ubd0=3D/path/to/root_fs eth0=3Dmcast +@end example + + @item -net none Indicate that no network devices should be configured. It is used to override the default configuration which is activated if no diff --git a/vl.c b/vl.c index 4b1e72e..4492418 100644 --- a/vl.c +++ b/vl.c @@ -2131,8 +2131,17 @@ typedef struct NetSocketState { int index; int packet_len; uint8_t buf[4096]; + struct sockaddr_in dgram_dst; /* contains inet host and port destinati= on iff connectionless (SOCK_DGRAM) */ } NetSocketState; =20 +static int qemu_socket_is_dgram(const struct NetSocketState *s) +{ + /* This could also be: getsockopt(s->fd, SOL_SOCKET, SO_TYPE, &opt, &o= ptlen) + * but I would cost an extra syscall per write (DGRAM writes don't req= uire sending pkt length) + */ + return (s->dgram_dst.sin_addr.s_addr!=3D0); +} + typedef struct NetSocketListenState { VLANState *vlan; int fd; @@ -2145,8 +2154,12 @@ static void net_socket_receive(void *opa uint32_t len; len =3D htonl(size); =20 - unix_write(s->fd, (const uint8_t *)&len, sizeof(len)); - unix_write(s->fd, buf, size); + if (qemu_socket_is_dgram(s)) { + sendto(s->fd, buf, size, 0, (struct sockaddr *)&s->dgram_dst, sizeof(s->d= gram_dst)); + } else { + unix_write(s->fd, (const uint8_t *)&len, sizeof(len)); + unix_write(s->fd, buf, size); + } } =20 static void net_socket_send(void *opaque) @@ -2164,6 +2177,11 @@ static void net_socket_send(void *opaque qemu_set_fd_handler(s->fd, NULL, NULL, NULL); return; } + if (qemu_socket_is_dgram(s)) { + s->index =3D 0; + s->state =3D 1; + s->packet_len =3D size; + } buf =3D buf1; while (size > 0) { /* reassemble a packet from the network */ @@ -2336,6 +2354,75 @@ static int net_socket_connect_init(VLANS return 0; } =20 +static int net_socket_mcast_init(VLANState *vlan, const char *host_str) +{ + NetSocketState *s; + int fd=3D-1, ret, val; + struct sockaddr_in saddr; + struct ip_mreq imr; + + if (parse_host_port(&saddr, host_str) < 0) + return -1; + + if (!IN_MULTICAST(ntohl(saddr.sin_addr.s_addr))) { + fprintf(stderr, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does no= t contain a multicast address\n", + inet_ntoa(saddr.sin_addr), ntohl(saddr.sin_addr.s_addr)); + return -1; + + } + fd =3D socket(PF_INET, SOCK_DGRAM, 0); + if (fd < 0) { + perror("socket(PF_INET, SOCK_DGRAM)"); + return -1; + } + + /* Add host to multicast group */ + imr.imr_multiaddr =3D saddr.sin_addr; + imr.imr_interface.s_addr =3D htonl(INADDR_ANY); + + ret =3D setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (void *) &imr, s= izeof(struct ip_mreq)); + if (ret < 0) { + perror("setsockopt(IP_ADD_MEMBERSHIP)"); + goto fail; + } + + /* Force mcast msgs to loopback (eg. several QEMUs in same host */ + val =3D 1; + ret=3Dsetsockopt(fd, SOL_IP, IP_MULTICAST_LOOP, &val, sizeof(val)); + if (ret < 0) { + perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)"); + goto fail; + } + ret=3Dsetsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)); + if (ret < 0) { + perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)"); + goto fail; + } + =20 + ret =3D bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)); + if (ret < 0) { + perror("bind"); + goto fail; + } + =20 + fcntl(fd, F_SETFL, O_NONBLOCK); + + s =3D net_socket_fd_init(vlan, fd, 1); + if (!s) + goto fail; + =20 + /* Connectionless: save mcast address for sendto() */ + s->dgram_dst =3D saddr; + + snprintf(s->vc->info_str, sizeof(s->vc->info_str), + "socket: mcast at %s:%d",=20 + inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); + return 0; +fail: + if (fd>=3D0) close(fd); + return -1; +} + #endif /* !_WIN32 */ =20 static int get_param_value(char *buf, int buf_size, @@ -2472,6 +2559,8 @@ int net_client_init(const char *str) ret =3D net_socket_listen_init(vlan, buf); } else if (get_param_value(buf, sizeof(buf), "connect", p) > 0) { ret =3D net_socket_connect_init(vlan, buf); + } else if (get_param_value(buf, sizeof(buf), "mcast", p) > 0) { + ret =3D net_socket_mcast_init(vlan, buf); } else { fprintf(stderr, "Unknown socket options: %s\n", p); return -1; @@ -3810,6 +3899,8 @@ void help(void) " use 'fd=3Dh' to connect to an already opened T= AP interface\n" "-net socket[,vlan=3Dn][,fd=3Dh][,listen=3D[host]:port][,connec= t=3Dhost:port]\n" " connect the vlan 'n' to another VLAN using a s= ocket connection\n" + "-net socket[,vlan=3Dn][,fd=3Dh][,mcast=3Dmaddr:port]\n" + " create a vlan 'n' with another QEMUs on multic= ast maddr and port\n" #endif "-net none use it alone to have zero network devices; if = no -net option\n" " is provided, the default is '-net nic -net use= r'\n" --qcHopEYAB45HaUaB-- --i7F3eY7HS/tUJxUd--