All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/2 V2] kvm tools: Set up tun interface using ioctls
@ 2011-04-14  5:24 Sasha Levin
  2011-04-14  5:24 ` [PATCH 2/2 V2] kvm tools: Make host side IP configurable Sasha Levin
  2011-04-14 12:35 ` [PATCH 1/2 V2] kvm tools: Set up tun interface using ioctls Asias He
  0 siblings, 2 replies; 4+ messages in thread
From: Sasha Levin @ 2011-04-14  5:24 UTC (permalink / raw)
  To: penberg, asias.hejun, gorcunov; +Cc: kvm, Sasha Levin

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 <levinsasha928@gmail.com>
---
 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 <sys/ioctl.h>
 #include <assert.h>
 #include <fcntl.h>
+#include <arpa/inet.h>
+#include <sys/types.h>
+#include <sys/socket.h>
 
 #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)
-- 
1.7.5.rc1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2011-04-14 12:36 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-04-14  5:24 [PATCH 1/2 V2] kvm tools: Set up tun interface using ioctls Sasha Levin
2011-04-14  5:24 ` [PATCH 2/2 V2] kvm tools: Make host side IP configurable Sasha Levin
2011-04-14 12:35   ` Asias He
2011-04-14 12:35 ` [PATCH 1/2 V2] kvm tools: Set up tun interface using ioctls Asias He

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.