From patchwork Wed Apr 13 12:13:23 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sasha Levin X-Patchwork-Id: 703661 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter1.kernel.org (8.14.4/8.14.3) with ESMTP id p3DCDrWQ029344 for ; Wed, 13 Apr 2011 12:13:53 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755442Ab1DMMNv (ORCPT ); Wed, 13 Apr 2011 08:13:51 -0400 Received: from mail-wy0-f174.google.com ([74.125.82.174]:33937 "EHLO mail-wy0-f174.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755089Ab1DMMNu (ORCPT ); Wed, 13 Apr 2011 08:13:50 -0400 Received: by mail-wy0-f174.google.com with SMTP id 21so431810wya.19 for ; Wed, 13 Apr 2011 05:13:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:from:to:cc:subject:date:message-id:x-mailer; bh=O9XqLUHWsjIvFOyjmb8DQCgl9+4TJLk8BjxhuIO5LZo=; b=O2lS+M8JObZ1bJx4Pjlpn/XP9iHE2/eqf73pg8uUYy0Zz55yQhXXDA2BJC8tWtuzPl LVo52Wu9wqkPCh4xJcG/mx3cscpUK7uBLxiVH4jxqmp9cz2SSONHNw1Kz/qpPMS/gL0N gTcSXtxiQ/NEP8eRmxiDVDt78HboBGZ1tU9C8= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=from:to:cc:subject:date:message-id:x-mailer; b=segvG6U++/BwwqFFVOttDhnfwnnXW0AJZ33sR1pxeOQESmUYzc4SRhJSCGpTlCItZF JGz6xzqEIGIR+/R3UH6Z1C+0cjovybtahvdzIvi9hJ5mmfgU+Xb9MLyR7JexmowoD4SY VbeS7WUSrtcQOnjSXBksPRlc5EhU65cIDqcuc= Received: by 10.216.143.134 with SMTP id l6mr1014373wej.2.1302696811675; Wed, 13 Apr 2011 05:13:31 -0700 (PDT) Received: from localhost.localdomain ([94.230.81.151]) by mx.google.com with ESMTPS id s40sm235855weq.28.2011.04.13.05.13.29 (version=TLSv1/SSLv3 cipher=OTHER); Wed, 13 Apr 2011 05:13:31 -0700 (PDT) From: Sasha Levin To: penberg@kernel.org, gorcunov@gmail.com, asias.hejun@gmail.com Cc: kvm@vger.kernel.org, Sasha Levin Subject: [PATCH 1/2] kvm tools: Set up tun interface using ioctls Date: Wed, 13 Apr 2011 15:13:23 +0300 Message-Id: <1302696804-29684-1-git-send-email-levinsasha928@gmail.com> X-Mailer: git-send-email 1.7.5.rc1 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.6 (demeter1.kernel.org [140.211.167.41]); Wed, 13 Apr 2011 12:13:54 +0000 (UTC) Use ioctls to assign IP address and bring interface up instead of using ifconfig. Signed-off-by: Sasha Levin Reviewed-by: Asias He --- 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)