Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1MCzTp-0002qs-FW for qemu-devel@nongnu.org; Sat, 06 Jun 2009 13:14:53 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1MCzTl-0002oW-UL for qemu-devel@nongnu.org; Sat, 06 Jun 2009 13:14:53 -0400 Received: from [199.232.76.173] (port=58711 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MCzTl-0002oT-OS for qemu-devel@nongnu.org; Sat, 06 Jun 2009 13:14:49 -0400 Received: from mail.gmx.net ([213.165.64.20]:33289) by monty-python.gnu.org with smtp (Exim 4.60) (envelope-from ) id 1MCzTk-00084L-Vn for qemu-devel@nongnu.org; Sat, 06 Jun 2009 13:14:49 -0400 Message-ID: From: "Sebastian Herbszt" References: <49C0BFCD.1040304@siemens.com> <49C6F2AA.40408@codemonkey.ws><49C91829.6000606@siemens.com> <49CE5DD1.1010302@codemonkey.ws> <7A9475ADF255438F86B48879B553733E@FSCPC> <4A09320F.1000705@siemens.com><5F412034D5F7477A95980A3554CCD5DC@FSCPC> <22F2AA9D186E4B179211713E01CD2FDE@FSCPC> In-Reply-To: Date: Sat, 6 Jun 2009 19:13:26 +0200 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----=_NextPart_000_0A59_01C9E6DA.DE47B470" Subject: [Qemu-devel] Re: [PATCH] Add pcap-based host network bridge List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Consul , qemu-devel@nongnu.org This is a multi-part message in MIME format. ------=_NextPart_000_0A59_01C9E6DA.DE47B470 Content-Type: text/plain; format=flowed; charset="iso-8859-15"; reply-type=response Content-Transfer-Encoding: 7bit Consul wrote: > Sebastian Herbszt wrote: >> Consul wrote: >>> Sebastian Herbszt wrote: >>>> That's really too bad. pcap-based networking works flawless on windows. >>>> Guest <-> host and guest <-> world communication works just fine. >>>> According to the previous threads about pcap-based networking it also >>>> should work fine on FreeBSD. >>>> >>> >>> Maybe make it compile conditionally for Windows only? Would you >>> consider such patch? >>> It would really be a valuable feature. >> >> I did rebase the patch against git master and it still seems to work. I >> can post it if there >> is any interest. >> >> - Sebastian >> > > Yes, please post. Find jan-pcap-updated-qdev3.diff attached. > I also have it rebased in my tree and it still works great. Is your tree available somewhere? > Easier to configure than TAP for sure. Maybe if we'll bug the list often > enough and loud enough, it finally gets through. - Sebastian ------=_NextPart_000_0A59_01C9E6DA.DE47B470 Content-Type: application/octet-stream; name="jan-pcap-updated-qdev3.diff" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="jan-pcap-updated-qdev3.diff" diff --git a/Makefile.target b/Makefile.target=0A= index 27de4b9..7eb84ad 100644=0A= --- a/Makefile.target=0A= +++ b/Makefile.target=0A= @@ -734,7 +734,7 @@ vl.o: qemu-options.h=0A= =0A= monitor.o: qemu-monitor.h=0A= =0A= -$(QEMU_PROG): LIBS +=3D $(SDL_LIBS) $(COCOA_LIBS) $(CURSES_LIBS) = $(BRLAPI_LIBS) $(VDE_LIBS) $(CURL_LIBS)=0A= +$(QEMU_PROG): LIBS +=3D $(SDL_LIBS) $(COCOA_LIBS) $(CURSES_LIBS) = $(BRLAPI_LIBS) $(VDE_LIBS) $(CURL_LIBS) $(PCAP_LIBS)=0A= $(QEMU_PROG): ARLIBS=3D../libqemu_common.a libqemu.a $(HWLIB)=0A= $(QEMU_PROG): $(OBJS) ../libqemu_common.a libqemu.a $(HWLIB)=0A= $(call LINK,$(OBJS))=0A= diff --git a/configure b/configure=0A= index 42d46f2..169f8bc 100755=0A= --- a/configure=0A= +++ b/configure=0A= @@ -165,6 +165,7 @@ mingw32=3D"no"=0A= EXESUF=3D""=0A= slirp=3D"yes"=0A= vde=3D"yes"=0A= +pcap=3D"yes"=0A= fmod_lib=3D""=0A= fmod_inc=3D""=0A= oss_lib=3D""=0A= @@ -432,6 +433,8 @@ for opt do=0A= ;;=0A= --disable-vde) vde=3D"no"=0A= ;;=0A= + --disable-pcap) pcap=3D"no"=0A= + ;;=0A= --disable-kqemu) kqemu=3D"no"=0A= ;;=0A= --disable-xen) xen=3D"no"=0A= @@ -634,6 +637,7 @@ echo " --oss-lib path to OSS library"=0A= echo " --enable-uname-release=3DR Return R for uname -r in usermode = emulation"=0A= echo " --sparc_cpu=3DV Build qemu for Sparc architecture = v7, v8, v8plus, v8plusa, v9"=0A= echo " --disable-vde disable support for vde network"=0A= +echo " --disable-pcap disable support for pcap-based network"=0A= echo " --disable-pthread disable pthread support"=0A= echo " --disable-aio disable AIO support"=0A= echo " --enable-io-thread enable IO thread"=0A= @@ -981,6 +985,31 @@ EOF=0A= fi=0A= =0A= ##########################################=0A= +# pcap libraries probe=0A= +if test "$pcap" =3D "yes" ; then=0A= + cat > $TMPC << EOF=0A= +#include =0A= +int main(void)=0A= +{=0A= + char errbuf[PCAP_ERRBUF_SIZE];=0A= +=0A= + pcap_lookupdev(errbuf);=0A= + return 0;=0A= +}=0A= +EOF=0A= + if test "$mingw32" =3D "yes" ; then=0A= + PCAP_LIBS=3D-lwpcap=0A= + else=0A= + PCAP_LIBS=3D-lpcap=0A= + fi=0A= + if $cc $ARCH_CFLAGS -o $TMPE $TMPC $PCAP_LIBS > /dev/null 2> = /dev/null ; then=0A= + :=0A= + else=0A= + pcap=3D"no"=0A= + fi=0A= +fi=0A= +=0A= +##########################################=0A= # Sound support libraries probe=0A= =0A= audio_drv_probe()=0A= @@ -1374,6 +1403,7 @@ echo "Documentation $build_docs"=0A= echo "uname -r $uname_release"=0A= echo "NPTL support $nptl"=0A= echo "vde support $vde"=0A= +echo "pcap net support $pcap"=0A= echo "AIO support $aio"=0A= echo "IO thread $io_thread"=0A= echo "Install blobs $blobs"=0A= @@ -1587,6 +1617,11 @@ if test "$vde" =3D "yes" ; then=0A= echo "#define CONFIG_VDE 1" >> $config_h=0A= echo "VDE_LIBS=3D-lvdeplug" >> $config_mak=0A= fi=0A= +if test "$pcap" =3D "yes" ; then=0A= + echo "CONFIG_PCAP=3Dyes" >> $config_mak=0A= + echo "#define CONFIG_PCAP 1" >> $config_h=0A= + echo "PCAP_LIBS=3D"$PCAP_LIBS >> $config_mak=0A= +fi=0A= for card in $audio_card_list; do=0A= def=3DCONFIG_`echo $card | tr '[:lower:]' '[:upper:]'`=0A= echo "$def=3Dyes" >> $config_mak=0A= diff --git a/net.c b/net.c=0A= index 2d24a7c..e4b3740 100644=0A= --- a/net.c=0A= +++ b/net.c=0A= @@ -93,6 +93,13 @@=0A= #endif=0A= #endif=0A= =0A= +#if defined(CONFIG_PCAP)=0A= +#include =0A= +#ifdef _WIN32=0A= +#include =0A= +#endif /* _WIN32 */=0A= +#endif /* CONFIG_PCAP */=0A= +=0A= #if defined(__OpenBSD__)=0A= #include =0A= #endif=0A= @@ -1176,6 +1183,268 @@ static int net_tap_init(VLANState *vlan, const = char *model,=0A= =0A= #endif /* !_WIN32 */=0A= =0A= +#if defined(CONFIG_PCAP)=0A= +=0A= +#ifdef _WIN32=0A= +#define ADAPTER_KEY = "SYSTEM\\CurrentControlSet\\Control\\Class" \=0A= + = "\\{4D36E972-E325-11CE-BFC1-08002BE10318}"=0A= +#define NETWORK_CONNECTIONS_KEY = "SYSTEM\\CurrentControlSet\\Control\\Network" \=0A= + = "\\{4D36E972-E325-11CE-BFC1-08002BE10318}"=0A= +#endif=0A= +=0A= +typedef struct PCAPState {=0A= + VLANClientState *vc;=0A= + pcap_t *handle;=0A= +} PCAPState;=0A= +=0A= +static void pcap_receive(void *opaque, const uint8_t *buf, int size)=0A= +{=0A= + PCAPState *s =3D (PCAPState *)opaque;=0A= +=0A= + pcap_sendpacket(s->handle, (u_char*)buf, size);=0A= +}=0A= +=0A= +static void pcap_callback(u_char *user, struct pcap_pkthdr *phdr, = u_char *pdata)=0A= +{=0A= + VLANClientState *vc =3D (VLANClientState *)user;=0A= +=0A= + qemu_send_packet(vc, pdata, phdr->len);=0A= +}=0A= +=0A= +static void pcap_send(void *opaque)=0A= +{=0A= + PCAPState *s =3D (PCAPState *)opaque;=0A= +=0A= + pcap_dispatch(s->handle, 1, (pcap_handler)&pcap_callback, (u_char = *)s->vc);=0A= +}=0A= +=0A= +static void pcap_cleanup(VLANClientState *vc)=0A= +{=0A= + PCAPState *s =3D vc->opaque;=0A= +=0A= +#ifndef _WIN32=0A= + int fd;=0A= +=0A= + if ((fd =3D pcap_get_selectable_fd(s->handle)) < 0) {=0A= + fprintf(stderr, "qemu: pcap_get_selectable_fd failed\n");=0A= + return;=0A= + }=0A= + qemu_set_fd_handler(fd, NULL, NULL, NULL);=0A= +#else=0A= + HANDLE hand;=0A= +=0A= + hand =3D pcap_getevent(s->handle);=0A= + if ((hand =3D pcap_getevent(s->handle)) =3D=3D 0) {=0A= + fprintf(stderr, "qemu: pcap_getevent failed\n");=0A= + return;=0A= + }=0A= + qemu_del_wait_object(hand, NULL, NULL);=0A= +#endif=0A= +=0A= + pcap_close(s->handle);=0A= + qemu_free(s);=0A= +}=0A= +=0A= +#ifdef _WIN32=0A= +static int get_windows_device_name(char *name, int name_size,=0A= + char *actual_name, int = actual_name_size)=0A= +{=0A= + const char name_string[] =3D "Name";=0A= + char connection_string[256];=0A= + HKEY connection_key;=0A= + char name_data[256];=0A= + DWORD name_type;=0A= + LONG status;=0A= + DWORD len;=0A= +=0A= + snprintf(connection_string, sizeof(connection_string),=0A= + "%s\\%s\\Connection", NETWORK_CONNECTIONS_KEY, name);=0A= +=0A= + status =3D RegOpenKeyEx(HKEY_LOCAL_MACHINE, connection_string, 0, = KEY_READ,=0A= + &connection_key);=0A= +=0A= + if (status =3D=3D ERROR_SUCCESS) {=0A= + len =3D sizeof(name_data);=0A= + status =3D RegQueryValueEx(connection_key, name_string, NULL,=0A= + &name_type, (PBYTE)name_data, &len);=0A= + if (status !=3D ERROR_SUCCESS || name_type !=3D REG_SZ) {=0A= + fprintf(stderr, "qemu: error opening registry key: = %s\\%s\\%s",=0A= + NETWORK_CONNECTIONS_KEY, connection_string, = name_string);=0A= + return -1;=0A= + } else {=0A= + snprintf(actual_name, actual_name_size, "%s", name_data);=0A= + }=0A= +=0A= + RegCloseKey(connection_key);=0A= +=0A= + }=0A= + return 0;=0A= +}=0A= +#endif=0A= +=0A= +static int net_pcap_init(VLANState *vlan, const char *model, const char = *name,=0A= + char *ifname)=0A= +{=0A= + PCAPState *s;=0A= + char errbuf[PCAP_ERRBUF_SIZE];=0A= + int fd;=0A= +=0A= + s =3D qemu_mallocz(sizeof(PCAPState));=0A= + if (!s)=0A= + return -1;=0A= +=0A= +#ifndef _WIN32=0A= + if (!ifname && !(ifname =3D pcap_lookupdev(errbuf))) {=0A= + fprintf(stderr, "qemu: pcap_lookupdev: %s\n", errbuf);=0A= + goto fail;=0A= + }=0A= +=0A= + s->handle =3D (void*)pcap_open_live(ifname, 65535, 1, 0, errbuf);=0A= +#else=0A= + {=0A= + pcap_if_t *found_device =3D NULL;=0A= + pcap_if_t *alldevs =3D NULL;=0A= + pcap_if_t *device;=0A= + char errbuf[PCAP_ERRBUF_SIZE];=0A= + char connection_name_data[256];=0A= + char name_data[256];=0A= + char *code_strt;=0A= +=0A= + /* Retrieve the device list */=0A= + if (pcap_findalldevs(&alldevs, errbuf) =3D=3D -1) {=0A= + fprintf(stderr, "qemu: error in pcap_findalldevs: %s\n", = errbuf);=0A= + goto fail;=0A= + }=0A= +=0A= + device =3D alldevs;=0A= +=0A= + while (device) {=0A= + memset(connection_name_data, 0, = sizeof(connection_name_data));=0A= +=0A= + code_strt =3D strchr(device->name, '{');=0A= + if (code_strt) {=0A= + snprintf(name_data, sizeof(name_data), "%s", code_strt);=0A= +=0A= + get_windows_device_name(name_data, sizeof(name_data),=0A= + connection_name_data,=0A= + sizeof(connection_name_data));=0A= + }=0A= +=0A= + if (*connection_name_data !=3D 0) {=0A= + if (ifname) {=0A= + /*=0A= + * If an exact match exists, over-ride any found = device,=0A= + * setting exact match device to it.=0A= + */=0A= + if (strcmp(connection_name_data, ifname) =3D=3D 0) {=0A= + found_device =3D device;=0A= + break;=0A= + }=0A= +=0A= + /*=0A= + * Do a partial search, if successful, set this = device as=0A= + * the found one, but continue looping through = devices.=0A= + */=0A= + if (found_device && strstr(connection_name_data, = ifname))=0A= + found_device =3D device;=0A= + } else {=0A= + /*=0A= + * If no name specified and network has an address,=0A= + * autoselect the first device.=0A= + */=0A= + if (device->addresses) {=0A= + found_device =3D device;=0A= + break;=0A= + }=0A= + }=0A= + }=0A= +=0A= + device =3D device->next;=0A= + }=0A= +=0A= + if (!found_device)=0A= + goto fail;=0A= +=0A= + ifname =3D found_device->name;=0A= +=0A= + s->handle =3D pcap_open(ifname, 65536,=0A= + PCAP_OPENFLAG_PROMISCUOUS |=0A= + PCAP_OPENFLAG_NOCAPTURE_LOCAL |=0A= + PCAP_OPENFLAG_MAX_RESPONSIVENESS,=0A= + 0, NULL, errbuf);=0A= + }=0A= +#endif=0A= + if (!s->handle) {=0A= + fprintf(stderr, "qemu: pcap_open_live: %s\n", errbuf);=0A= + goto fail;=0A= + }=0A= +=0A= + if (pcap_setnonblock(s->handle, 1, errbuf) < 0) {=0A= + fprintf(stderr, "qemu: pcap_setnonblock: %s\n", errbuf);=0A= + goto fail;=0A= + }=0A= +=0A= +#ifndef _WIN32=0A= +#if defined(BIOCSHDRCMPLT)=0A= + /*=0A= + * Tell the kernel that the header is fully-formed when it gets it.=0A= + * This is required in order to fake the src address.=0A= + */=0A= + {=0A= + unsigned int one =3D 1;=0A= + ioctl(pcap_fileno(s->handle), BIOCSHDRCMPLT, &one);=0A= + }=0A= +#endif /* BIOCSHDRCMPLT */=0A= +=0A= +#if defined(BIOCIMMEDIATE)=0A= + /*=0A= + * Tell the kernel that the packet has to be processed immediately.=0A= + */=0A= + {=0A= + unsigned int one =3D 1;=0A= + ioctl(pcap_fileno(s->handle), BIOCIMMEDIATE, &one);=0A= + }=0A= +#endif /* BIOCIMMEDIATE */=0A= +#endif=0A= +=0A= + s->vc =3D qemu_new_vlan_client(vlan, model, name, pcap_receive, = NULL, pcap_cleanup, s);=0A= + snprintf(s->vc->info_str, sizeof(s->vc->info_str), "pcap = redirector");=0A= +#ifndef _WIN32=0A= + if ((fd =3D pcap_get_selectable_fd(s->handle)) < 0) {=0A= + fprintf(stderr, "qemu: pcap_get_selectable_fd failed\n");=0A= + goto fail;=0A= + }=0A= + qemu_set_fd_handler(fd, pcap_send, NULL, s);=0A= +#else=0A= + {=0A= + HANDLE hand;=0A= +=0A= + hand =3D pcap_getevent(s->handle);=0A= + if ((hand =3D pcap_getevent(s->handle)) =3D=3D 0) {=0A= + fprintf(stderr, "qemu: pcap_getevent failed\n");=0A= + goto fail;=0A= + }=0A= + if (qemu_add_wait_object(hand, pcap_send, s) < 0) {=0A= + fprintf(stderr,=0A= + "qemu: qemu_add_wait_object for pcap_send = failed\n");=0A= + goto fail;=0A= + }=0A= + }=0A= +#endif=0A= +=0A= + return 0;=0A= +=0A= +fail:=0A= + if (s) {=0A= + if (s->handle)=0A= + pcap_close(s->handle);=0A= + qemu_free(s);=0A= + }=0A= +=0A= + return -1;=0A= +}=0A= +#endif /* CONFIG_PCAP */=0A= +=0A= #if defined(CONFIG_VDE)=0A= typedef struct VDEState {=0A= VLANClientState *vc;=0A= @@ -2025,6 +2294,16 @@ int net_client_init(const char *device, const = char *p)=0A= }=0A= } else=0A= #endif=0A= +#ifdef CONFIG_PCAP=0A= + if (!strcmp(device, "pcap")) {=0A= + char ifname[64];=0A= + vlan->nb_host_devs++;=0A= + if (get_param_value(ifname, sizeof(ifname), "ifname", p) <=3D = 0) {=0A= + ifname[0] =3D '\0';=0A= + }=0A= + ret =3D net_pcap_init(vlan, device, name, ifname);=0A= + } else=0A= +#endif=0A= if (!strcmp(device, "socket")) {=0A= if (get_param_value(buf, sizeof(buf), "fd", p) > 0) {=0A= int fd;=0A= diff --git a/qemu-options.hx b/qemu-options.hx=0A= index 87af798..9d24473 100644=0A= --- a/qemu-options.hx=0A= +++ b/qemu-options.hx=0A= @@ -753,6 +753,10 @@ DEF("net", HAS_ARG, QEMU_OPTION_net, \=0A= " Use group 'groupname' and mode 'octalmode' to = change default\n"=0A= " ownership and permissions for communication = port.\n"=0A= #endif=0A= +#ifdef CONFIG_PCAP=0A= + "-net pcap[,vlan=3Dn][,name=3Dstr][,ifname=3Dname]\n"=0A= + " connect the host network interface to VLAN 'n' = using PCAP\n"=0A= +#endif=0A= "-net dump[,vlan=3Dn][,file=3Df][,len=3Dn]\n"=0A= " dump traffic on vlan 'n' to file 'f' (max n bytes = per packet)\n"=0A= "-net none use it alone to have zero network devices; if no = -net option\n"=0A= @@ -875,6 +879,18 @@ vde_switch -F -sock /tmp/myswitch=0A= qemu linux.img -net nic -net vde,sock=3D/tmp/myswitch=0A= @end example=0A= =0A= +@item -net = pcap[,vlan=3D@var{n}][,name=3D@var{name}][,ifname=3D@var{name}]=0A= +Connect VLAN @var{n} to the host network interface @var{name} using the = pcap=0A= +interface. This will switch the host's interface to promisc mode, = enabling the=0A= +guest to receive all packets on this VLAN that the host sees, too. The = pcap=0A= +host interface is a handy alternative to a TAP interface, attached via a=0A= +bridge to a host network interface.=0A= +=0A= +Example:=0A= +@example=0A= +qemu linux.img -net nic -net pcap,ifname=3Deth0=0A= +@end example=0A= +=0A= @item -net dump[,vlan=3D@var{n}][,file=3D@var{file}][,len=3D@var{len}]=0A= Dump network traffic on VLAN @var{n} to file @var{file} = (@file{qemu-vlan0.pcap} by default).=0A= At most @var{len} bytes (64k by default) per packet are stored. The = file format is=0A= ------=_NextPart_000_0A59_01C9E6DA.DE47B470--