diff --git a/README.qemu-mcast b/README.qemu-mcast new file mode 100644 index 0000000..8a109d6 --- /dev/null +++ b/README.qemu-mcast @@ -0,0 +1,66 @@ +QEMU multicast support v6 +========================= +Author: JuanJo Ciarlante jjo()um.edu.ar, juanjosec()gmail.com +Date: jue dic 15 13:23:33 ART 2005 +Keywords: qemu, socket, multicast, UDP, mcast, UML + +Attached patch (against qemu CVS 2005-12-10) adds multicast support for +QEMU socket transport, ie: run several stateless possibly non-local QEMUs +on same virtual ethernet "BUS". +User-mode-linux (UML) compatible (tested). + +An excerpt from qemu-doc.texi: + + `-net socket[,vlan=n][,fd=h][,mcast=maddr: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=mcast'), see http://user-mode-linux.sf.net. + Example: + # launch one QEMU instance + qemu linux.img -net nic,macaddr=52:54:00:12:34:56 -net socket,mcast=230.0.0.1:1234 + # launch another QEMU instance on same "bus" + qemu linux.img -net nic,macaddr=52:54:00:12:34:57 -net socket,mcast=230.0.0.1:1234 + # launch yet another QEMU instance on same "bus" + qemu linux.img -net nic,macaddr=52:54:00:12:34:58 -net socket,mcast=230.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=52:54:00:12:34:56 -net socket,mcast=239.192.168.1:1102 + # launch UML + /path/to/linux ubd0=/path/to/root_fs eth0=mcast + +Available under GPLv2 from + http://www.irrigacion.gov.ar/juanjo/qemu/ + + +Changes +------- +v6 (2005-12-15): + * Added tests/qemu-mcast-test-fd.py + +v5 (2005-12-15): + * Reworked code to isolate dgram functions and correctly use IO handlers methods with + specific dgram implementations + * net_socket_fd_init() now detects STREAM or DGRAM and calls: + - net_socket_fd_init_stream(): merely renamed from previous net_socket_fd_init impl + - net_socket_fd_init_dgram(): it also supports fd passing + +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 + + +--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 # diff --git a/qemu-doc.texi b/qemu-doc.texi index 7ec41f5..ed5b57f 100644 --- a/qemu-doc.texi +++ b/qemu-doc.texi @@ -293,6 +293,57 @@ qemu linux.img -net nic,macaddr=52:54:00 qemu linux.img -net nic,macaddr=52:54:00:12:34:57 -net socket,connect=127.0.0.1:1234 @end example address@hidden -net socket[,vlan=n][,fd=h][,mcast=maddr:port] + +Create a VLAN @var{n} shared with another QEMU virtual +machines using a UDP multicast socket, effectively making a bus for +every QEMU with same multicast address @var{maddr} and @var{port}. +NOTES: address@hidden address@hidden +Several QEMU can be running on different hosts and share same bus (assuming +correct multicast setup for these hosts). address@hidden +mcast support is compatible with User Mode Linux (argument @address@hidden), see address@hidden address@hidden enumerate + + +Example: address@hidden +# launch one QEMU instance +qemu linux.img -net nic,macaddr=52:54:00:12:34:56 -net socket,mcast=230.0.0.1:1234 +# launch another QEMU instance on same "bus" +qemu linux.img -net nic,macaddr=52:54:00:12:34:57 -net socket,mcast=230.0.0.1:1234 +# launch yet another QEMU instance on same "bus" +qemu linux.img -net nic,macaddr=52:54:00:12:34:58 -net socket,mcast=230.0.0.1:1234 address@hidden example + +Example (User Mode Linux compat.): address@hidden +# launch QEMU instance (note mcast address selected is UML's default) +qemu linux.img -net nic,macaddr=52:54:00:12:34:56 -net socket,mcast=239.192.168.1:1102 +# launch UML +/path/to/linux ubd0=/path/to/root_fs eth0=mcast address@hidden example + + address@hidden -net socket[,vlan=n][,fd=h][,mcast=maddr:port] + +Create a VLAN @var{n} shared with another QEMU virtual +machines using a UDP multicast socket, effectively making a bus for +every QEMU with same multicast address @var{maddr} and @var{port}. + +Example: address@hidden +# launch one QEMU instance +qemu linux.img -net nic,macaddr=52:54:00:12:34:56 -net socket,mcast=230.0.0.1:1234 +# launch another QEMU instance on same "bus" +qemu linux.img -net nic,macaddr=52:54:00:12:34:57 -net socket,mcast=230.0.0.1:1234 +# launch yet another QEMU instance on same "bus" +qemu linux.img -net nic,macaddr=52:54:00:12:34:58 -net socket,mcast=230.0.0.1:1234 address@hidden 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/tests/qemu-mcast-test-fd.py b/tests/qemu-mcast-test-fd.py new file mode 100755 index 0000000..99083ef --- /dev/null +++ b/tests/qemu-mcast-test-fd.py @@ -0,0 +1,40 @@ +#!/usr/bin/python +# test script: QEMU mcast support for fd's +# Author: JuanJo Ciarlante, under GPLv2 +import os +import sys +import time +import string +import struct +from socket import * + +maddr="239.192.168.1" + +bind_addr=(maddr, 1102) +qemu_bin="./i386-softmmu/qemu" +so_img="-cdrom /local/isos/livecd-mini/damnsmalllinux/dsl-2.0.iso" + +def create_mcast_socket(): + s=socket(AF_INET, SOCK_DGRAM, 0) + s.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) + s.bind(bind_addr) + + bytes = map(int, string.split(maddr, ".")) + grpaddr = 0L + for byte in bytes: grpaddr = (grpaddr << 8) | byte + mreq = struct.pack('ll', htonl(grpaddr), htonl(INADDR_ANY)) + s.setsockopt(IPPROTO_IP, IP_ADD_MEMBERSHIP, mreq) + return s + +s=create_mcast_socket() +# Here goes the show: pass mcast socket as: fd=%d +pids=[] +cmdline="%s %s -net nic,macaddr=52:54:00:12:34:80 -net socket,fd=%d 1>&2 & echo -n $!" % (qemu_bin, so_img, s.fileno()) +pids.append(int(os.popen(cmdline).read())) +cmdline="%s %s -net nic,macaddr=52:54:00:12:34:81 -net socket,fd=%d 1>&2 & echo -n $!" % (qemu_bin, so_img, s.fileno()) +pids.append(int(os.popen(cmdline).read())) + +time.sleep(1) #lets drain QEMU output... +sys.stderr.write("Press [Enter] to kill %s and exit ->" % pids) +sys.stdin.readline() +map(lambda p: os.kill(p, 15), pids) diff --git a/vl.c b/vl.c index 4b1e72e..a1b312e 100644 --- a/vl.c +++ b/vl.c @@ -2131,6 +2131,7 @@ typedef struct NetSocketState { int index; int packet_len; uint8_t buf[4096]; + struct sockaddr_in dgram_dst; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */ } NetSocketState; typedef struct NetSocketListenState { @@ -2148,6 +2149,15 @@ static void net_socket_receive(void *opa unix_write(s->fd, (const uint8_t *)&len, sizeof(len)); unix_write(s->fd, buf, size); } +/* XXX: we consider we can send the whole packet without blocking */ +static void net_socket_receive_dgram(void *opaque, const uint8_t *buf, int size) +{ + NetSocketState *s = opaque; + uint32_t len; + len = htonl(size); + + sendto(s->fd, buf, size, 0, (struct sockaddr *)&s->dgram_dst, sizeof(s->dgram_dst)); +} static void net_socket_send(void *opaque) { @@ -2201,13 +2211,141 @@ static void net_socket_send(void *opaque } } +static void net_socket_send_dgram(void *opaque) +{ + NetSocketState *s = opaque; + int size; + + size = recv(s->fd, s->buf, sizeof(s->buf), 0); + if (size < 0) + return; + if (size == 0) { + /* end of connection */ + qemu_set_fd_handler(s->fd, NULL, NULL, NULL); + return; + } + s->packet_len = size; + qemu_send_packet(s->vc, s->buf, s->packet_len); +} + +static int net_socket_mcast_create(struct sockaddr_in *mcastaddr) +{ + struct ip_mreq imr; + int fd; + int val, ret; + if (!IN_MULTICAST(ntohl(mcastaddr->sin_addr.s_addr))) { + fprintf(stderr, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n", + inet_ntoa(mcastaddr->sin_addr), ntohl(mcastaddr->sin_addr.s_addr)); + return -1; + + } + fd = 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 = mcastaddr->sin_addr; + imr.imr_interface.s_addr = htonl(INADDR_ANY); + + ret = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (void *) &imr, sizeof(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 = 1; + ret=setsockopt(fd, SOL_IP, IP_MULTICAST_LOOP, &val, sizeof(val)); + if (ret < 0) { + perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)"); + goto fail; + } + + ret=setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)); + if (ret < 0) { + perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)"); + goto fail; + } + + ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr)); + if (ret < 0) { + perror("bind"); + goto fail; + } + + fcntl(fd, F_SETFL, O_NONBLOCK); + return fd; +fail: + if (fd>=0) close(fd); + return -1; +} + +static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan, int fd, + int is_connected) +{ + struct sockaddr_in saddr; + int newfd; + socklen_t saddr_len; + NetSocketState *s; + + /* fd passed: multicast: "learn" dgram_dst address from bound address and save it + * Because this may be "shared" socket from a "master" process, datagrams would be recv() + * by ONLY ONE process: we must "clone" this dgram socket --jjo + */ + + if (is_connected) { + if (getsockname(fd, (struct sockaddr *) &saddr, &saddr_len) == 0) { + /* must be bound */ + if (saddr.sin_addr.s_addr==0) { + fprintf(stderr, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n", + fd); + return NULL; + } + /* clone dgram socket */ + newfd = net_socket_mcast_create(&saddr); + if (newfd < 0) { + /* error already reported by net_socket_mcast_create() */ + close(fd); + return NULL; + } + /* clone newfd to fd, close newfd */ + dup2(newfd, fd); + close(newfd); + + } else { + fprintf(stderr, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n", + fd, strerror(errno)); + return NULL; + } + } + + s = qemu_mallocz(sizeof(NetSocketState)); + if (!s) + return NULL; + s->fd = fd; + + s->vc = qemu_new_vlan_client(vlan, net_socket_receive_dgram, s); + qemu_set_fd_handler(s->fd, net_socket_send_dgram, NULL, s); + + /* mcast: save bound address as dst */ + if (is_connected) s->dgram_dst=saddr; + + snprintf(s->vc->info_str, sizeof(s->vc->info_str), + "socket: fd=%d (%s mcast=%s:%d)", + fd, is_connected? "cloned" : "", + inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); + return s; +} + static void net_socket_connect(void *opaque) { NetSocketState *s = opaque; qemu_set_fd_handler(s->fd, net_socket_send, NULL, s); } -static NetSocketState *net_socket_fd_init(VLANState *vlan, int fd, +static NetSocketState *net_socket_fd_init_stream(VLANState *vlan, int fd, int is_connected) { NetSocketState *s; @@ -2227,6 +2365,28 @@ static NetSocketState *net_socket_fd_ini return s; } +static NetSocketState *net_socket_fd_init(VLANState *vlan, int fd, + int is_connected) +{ + int so_type=-1, optlen=sizeof(so_type); + + if(getsockopt(fd, SOL_SOCKET,SO_TYPE, &so_type, &optlen)< 0) { + fprintf(stderr, "qemu: error: setsockopt(SO_TYPE) for fd=%d failed\n", fd); + return NULL; + } + switch(so_type) { + case SOCK_DGRAM: + return net_socket_fd_init_dgram(vlan, fd, is_connected); + case SOCK_STREAM: + return net_socket_fd_init_stream(vlan, fd, is_connected); + default: + /* who knows ... this could be a eg. a pty, do warn and continue as stream */ + fprintf(stderr, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type, fd); + return net_socket_fd_init_stream(vlan, fd, is_connected); + } + return NULL; +} + static void net_socket_accept(void *opaque) { NetSocketListenState *s = opaque; @@ -2336,6 +2496,33 @@ static int net_socket_connect_init(VLANS return 0; } +static int net_socket_mcast_init(VLANState *vlan, const char *host_str) +{ + NetSocketState *s; + int fd; + struct sockaddr_in saddr; + + if (parse_host_port(&saddr, host_str) < 0) + return -1; + + + fd = net_socket_mcast_create(&saddr); + if (fd < 0) + return -1; + + s = net_socket_fd_init(vlan, fd, 0); + if (!s) + return -1; + + s->dgram_dst = saddr; + + snprintf(s->vc->info_str, sizeof(s->vc->info_str), + "socket: mcast=%s:%d", + inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); + return 0; + +} + #endif /* !_WIN32 */ static int get_param_value(char *buf, int buf_size, @@ -2472,6 +2659,8 @@ int net_client_init(const char *str) ret = net_socket_listen_init(vlan, buf); } else if (get_param_value(buf, sizeof(buf), "connect", p) > 0) { ret = net_socket_connect_init(vlan, buf); + } else if (get_param_value(buf, sizeof(buf), "mcast", p) > 0) { + ret = net_socket_mcast_init(vlan, buf); } else { fprintf(stderr, "Unknown socket options: %s\n", p); return -1; @@ -3810,6 +3999,8 @@ void help(void) " use 'fd=h' to connect to an already opened TAP interface\n" "-net socket[,vlan=n][,fd=h][,listen=[host]:port][,connect=host:port]\n" " connect the vlan 'n' to another VLAN using a socket connection\n" + "-net socket[,vlan=n][,fd=h][,mcast=maddr:port]\n" + " create a vlan 'n' with another QEMUs on multicast 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 user'\n"