diff mbox

[1/2] kvm tools: Set up tun interface using ioctls

Message ID 1302696804-29684-1-git-send-email-levinsasha928@gmail.com (mailing list archive)
State New, archived
Headers show

Commit Message

Sasha Levin April 13, 2011, 12:13 p.m. UTC
Use ioctls to assign IP address and bring interface up instead of using ifconfig.

Signed-off-by: Sasha Levin <levinsasha928@gmail.com>
---
 tools/kvm/virtio-net.c |   25 ++++++++++++++++++++++---
 1 files changed, 22 insertions(+), 3 deletions(-)

Comments

Asias He April 13, 2011, 12:39 p.m. UTC | #1
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 <levinsasha928@gmail.com>
> ---
>  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 <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,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 <asias.hejun@gmail.com>
Pekka Enberg April 13, 2011, 1:49 p.m. UTC | #2
On Wed, 13 Apr 2011, Sasha Levin wrote:
> Use ioctls to assign IP address and bring interface up instead of using ifconfig.
>
> Signed-off-by: Sasha Levin <levinsasha928@gmail.com>

I'm seeing this:

penberg@tiger:~/linux/tools/kvm$ make
   GEN      include/common-cmds.h
   CC       8250-serial.o
   CC       virtio-blk.o
   CC       virtio-net.o
cc1: warnings being treated as errors
virtio-net.c: In function ‘virtio_net__init’:
virtio-net.c:302: error: dereferencing pointer ‘({anonymous})’ does break 
strict-aliasing rules
virtio-net.c:302: note: initialized from here
make: *** [virtio-net.o] Error 1
Asias He April 13, 2011, 1:59 p.m. UTC | #3
On 04/13/2011 09:49 PM, Pekka Enberg wrote:
> On Wed, 13 Apr 2011, Sasha Levin wrote:
>> Use ioctls to assign IP address and bring interface up instead of
>> using ifconfig.
>>
>> Signed-off-by: Sasha Levin <levinsasha928@gmail.com>
> 
> I'm seeing this:
> 
> penberg@tiger:~/linux/tools/kvm$ make
>   GEN      include/common-cmds.h
>   CC       8250-serial.o
>   CC       virtio-blk.o
>   CC       virtio-net.o
> cc1: warnings being treated as errors
> virtio-net.c: In function ‘virtio_net__init’:
> virtio-net.c:302: error: dereferencing pointer ‘({anonymous})’ does
> break strict-aliasing rules
> virtio-net.c:302: note: initialized from here
> make: *** [virtio-net.o] Error 1

Hi, Pekka

I am wondering why is your gcc always stricter than mine?
Pekka Enberg April 13, 2011, 2:03 p.m. UTC | #4
On Wed, 2011-04-13 at 21:59 +0800, Asias He wrote:
> On 04/13/2011 09:49 PM, Pekka Enberg wrote:
> > On Wed, 13 Apr 2011, Sasha Levin wrote:
> >> Use ioctls to assign IP address and bring interface up instead of
> >> using ifconfig.
> >>
> >> Signed-off-by: Sasha Levin <levinsasha928@gmail.com>
> > 
> > I'm seeing this:
> > 
> > penberg@tiger:~/linux/tools/kvm$ make
> >   GEN      include/common-cmds.h
> >   CC       8250-serial.o
> >   CC       virtio-blk.o
> >   CC       virtio-net.o
> > cc1: warnings being treated as errors
> > virtio-net.c: In function ‘virtio_net__init’:
> > virtio-net.c:302: error: dereferencing pointer ‘({anonymous})’ does
> > break strict-aliasing rules
> > virtio-net.c:302: note: initialized from here
> > make: *** [virtio-net.o] Error 1
> 
> Hi, Pekka
> 
> I am wondering why is your gcc always stricter than mine?

I have gcc 4.4.3 here. I wouldn't be surprised if those were actually
gcc bugs but and that we should turn off the aliasing checks. However,
for this particular case, it was pretty good at spotting questionable
code so... :-)

			Pekka

--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

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 <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,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)