From mboxrd@z Thu Jan 1 00:00:00 1970 From: Asias He Subject: Re: [PATCH 1/2] kvm tools: Set up tun interface using ioctls Date: Wed, 13 Apr 2011 20:39:48 +0800 Message-ID: <4DA59994.4000400@gmail.com> References: <1302696804-29684-1-git-send-email-levinsasha928@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Cc: penberg@kernel.org, gorcunov@gmail.com, kvm@vger.kernel.org To: Sasha Levin Return-path: Received: from mail-pv0-f174.google.com ([74.125.83.174]:51024 "EHLO mail-pv0-f174.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751126Ab1DMMkv (ORCPT ); Wed, 13 Apr 2011 08:40:51 -0400 Received: by pvg12 with SMTP id 12so198836pvg.19 for ; Wed, 13 Apr 2011 05:40:51 -0700 (PDT) In-Reply-To: <1302696804-29684-1-git-send-email-levinsasha928@gmail.com> Sender: kvm-owner@vger.kernel.org List-ID: On 04/13/2011 08:13 PM, Sasha Levin wrote: > Use ioctls to assign IP address and bring interface up instead of using ifconfig. > > Signed-off-by: Sasha Levin > --- > tools/kvm/virtio-net.c | 25 ++++++++++++++++++++++--- > 1 files changed, 22 insertions(+), 3 deletions(-) > > diff --git a/tools/kvm/virtio-net.c b/tools/kvm/virtio-net.c > index ec70d5c..5f9bf07 100644 > --- a/tools/kvm/virtio-net.c > +++ b/tools/kvm/virtio-net.c > @@ -14,6 +14,9 @@ > #include > #include > #include > +#include > +#include > +#include > > #define VIRTIO_NET_IRQ 14 > #define VIRTIO_NET_QUEUE_SIZE 128 > @@ -276,7 +279,7 @@ static struct pci_device_header virtio_net_pci_device = { > static void virtio_net__tap_init(void) > { > struct ifreq ifr; > - > + int sock = socket(AF_INET, SOCK_STREAM, 0); > net_device.tap_fd = open("/dev/net/tun", O_RDWR); > if (net_device.tap_fd < 0) > die("Unable to open /dev/net/tun\n"); > @@ -291,9 +294,25 @@ static void virtio_net__tap_init(void) > > ioctl(net_device.tap_fd, TUNSETNOCSUM, 1); > > + memset(&ifr, 0, sizeof(ifr)); > + > + strncpy(ifr.ifr_name, net_device.tap_name, sizeof(net_device.tap_name)); > + > /*FIXME: Remove this after user can specify ip address and netmask*/ > - if (system("ifconfig tap0 192.168.33.2") < 0) > - warning("Can not set ip address on tap0"); > + ((struct sockaddr_in *)(&(ifr.ifr_addr)))->sin_addr.s_addr = inet_addr("192.168.33.2"); > + ifr.ifr_addr.sa_family = AF_INET; > + > + if (ioctl(sock, SIOCSIFADDR, &ifr) < 0) > + warning("Can not set ip address on tap device"); > + > + memset(&ifr, 0, sizeof(ifr)); > + strncpy(ifr.ifr_name, net_device.tap_name, sizeof(net_device.tap_name)); > + ioctl(sock, SIOCGIFFLAGS, &ifr); > + ifr.ifr_flags |= IFF_UP | IFF_RUNNING; > + if (ioctl(sock, SIOCSIFFLAGS, &ifr) < 0) > + warning("Could not bring tap device up"); > + > + close(sock); > } > > static void virtio_net__io_thread_init(struct kvm *self) Looks good to me. Reviewed-by: Asias He -- Best Regards, Asias He