From patchwork Thu Apr 14 05:24:40 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sasha Levin X-Patchwork-Id: 706211 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 p3E5PQH2019906 for ; Thu, 14 Apr 2011 05:25:27 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752931Ab1DNFZA (ORCPT ); Thu, 14 Apr 2011 01:25:00 -0400 Received: from mail-ww0-f44.google.com ([74.125.82.44]:36683 "EHLO mail-ww0-f44.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752800Ab1DNFZA (ORCPT ); Thu, 14 Apr 2011 01:25:00 -0400 Received: by wwa36 with SMTP id 36so1562871wwa.1 for ; Wed, 13 Apr 2011 22:24:58 -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=kk7OR/KJ3MNLCFB23KDLQOBiKVVshnSZkPXLjAbqkK4=; b=dx0gz5cvZCZnp12v4kdfn0Yhryg+QtEQaNiICHHANglcQ/M9dHw73Gi4KyOBexFU7x KBJdodhcQw0fI48ztsaKCRZz9b/guGTAJ+9AHm2+H3pLMEVUG4gPnBnL47fpRKUuzcec hMD2IxAAvVx3yTAi8olurhM0YlC5SIV58f3R0= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=from:to:cc:subject:date:message-id:x-mailer; b=HGgyKgagOk4o4mHWe3ELXSON/kfLnfhVLu5j45poSlptsNyWs2UxhKnBUGOz/ZcN5f ssesIwlhUpsoAJvQC+wqAhosTZ+sFTPqJxCREQAxeA7xKudnor8mxxc+yVitMrWcwdRs pdzshaYE7pN1jTas9qvSV4NQM8Qw+cfrQZXiA= Received: by 10.227.57.83 with SMTP id b19mr352345wbh.15.1302758698781; Wed, 13 Apr 2011 22:24:58 -0700 (PDT) Received: from localhost.localdomain (bzq-79-177-209-247.red.bezeqint.net [79.177.209.247]) by mx.google.com with ESMTPS id bd8sm747766wbb.14.2011.04.13.22.24.57 (version=TLSv1/SSLv3 cipher=OTHER); Wed, 13 Apr 2011 22:24:58 -0700 (PDT) From: Sasha Levin To: penberg@kernel.org, asias.hejun@gmail.com, gorcunov@gmail.com Cc: kvm@vger.kernel.org, Sasha Levin Subject: [PATCH 1/2 V2] kvm tools: Set up tun interface using ioctls Date: Thu, 14 Apr 2011 08:24:40 +0300 Message-Id: <1302758681-8829-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]); Thu, 14 Apr 2011 05:25:27 +0000 (UTC) Use ioctls to assign IP address and bring interface up instead of using ifconfig. Not breaking aliasing rules this time. Signed-off-by: Sasha Levin Reviewed-by: Asias He --- tools/kvm/virtio-net.c | 29 ++++++++++++++++++++++++++--- 1 files changed, 26 insertions(+), 3 deletions(-) diff --git a/tools/kvm/virtio-net.c b/tools/kvm/virtio-net.c index ec70d5c..622cfc6 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,9 @@ 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); + struct sockaddr_in sin = {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 +296,27 @@ 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"); + sin.sin_addr.s_addr = inet_addr("192.168.33.2"); + memcpy(&(ifr.ifr_addr), &sin, sizeof(ifr.ifr_addr)); + 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)