kvm tools: Set up tun interface using ioctls
PATCH v2 1/2 kvm tools: Set up tun interface using ioctls
Use ioctls to assign IP address and bring interface up instead of using ifconfig. Not breaking aliasing rules this time.
--- tools/kvm/virtio-net.c | 29 ++++++++++++++++++++++++++--- 1 files changed, 26 insertions(+), 3 deletions(-)
tools/kvm/virtio-net.c +26 -3
@@ -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)
Signature · 1 line
1.7.5.rc1
PATCH v2 2/2 kvm tools: Make host side IP configurable
Add --host-ip-addr parameter to allow changing the host-side IP address. Add a networking group to the cmdline menu.
--- tools/kvm/include/kvm/virtio-net.h | 2 +- tools/kvm/kvm-run.c | 15 ++++++++++++--- tools/kvm/virtio-net.c | 9 ++++----- 3 files changed, 17 insertions(+), 9 deletions(-)
tools/kvm/include/kvm/virtio-net.h +1 -1
@@ -2,6 +2,6 @@#define KVM__VIRTIO_NET_H struct kvm ; -void virtio_net__init (struct kvm *self); +void virtio_net__init (struct kvm *self,const char *host_ip_addr);#endif /* KVM__VIRTIO_NET_H */
tools/kvm/kvm-run.c +12 -3
@@ -31,6 +31,7 @@#define DEFAULT_KVM_DEV "/dev/kvm"#define DEFAULT_CONSOLE "serial"#define DEFAULT_NETWORK "none" +#define DEFAULT_HOST_ADDR "192.168.33.2"#define MB_SHIFT (20)#define MIN_RAM_SIZE_MB (64ULL)
@@ -66,6 +67,7 @@ static const char *image_filename;static const char *console;static const char *kvm_dev;static const char *network; +static const char *host_ip_addr;static bool single_step;static bool readonly_image;extern bool ioport_debug;
@@ -87,8 +89,6 @@ static const struct option options[] = { "Don't write changes back to disk image"),OPT_STRING ('c' ,"console" , &console,"serial or virtio" ,"Console to use" ), -OPT_STRING ('n' ,"network" , &network,"virtio" , -"Network to use" ),OPT_GROUP ("Kernel options:" ), OPT_STRING('k', "kernel", &kernel_filename, "kernel",
@@ -98,6 +98,12 @@ static const struct option options[] = {OPT_STRING ('p' ,"params" , &kernel_cmdline,"params" ,"Kernel command line arguments" ), +OPT_GROUP ("Networking options:" ), +OPT_STRING ('n' ,"network" , &network,"virtio" , +"Network to use" ), +OPT_STRING ('\0' ,"host-ip-addr" , &host_ip_addr,"a.b.c.d" , +"Assign this address to the host side networking" ), +OPT_GROUP ("Debug options:" ),OPT_STRING ('d' ,"kvm-dev" , &kvm_dev,"kvm-dev" ,"KVM device file" ), OPT_BOOLEAN('s', "single-step", &single_step,
@@ -218,6 +224,9 @@ int kvm_cmd_run(int argc, const char **argv, const char *prefix)else active_console =CONSOLE_8250 ; +if (!host_ip_addr) + host_ip_addr =DEFAULT_HOST_ADDR ; +term_init (); kvm =kvm__init (kvm_dev, ram_size);
@@ -259,7 +268,7 @@ int kvm_cmd_run(int argc, const char **argv, const char *prefix) network =DEFAULT_NETWORK ;if (!strncmp (network,"virtio" ,6 )) -virtio_net__init (kvm); +virtio_net__init (kvm, host_ip_addr);kvm__start_timer (kvm);
tools/kvm/virtio-net.c +4 -5
@@ -276,7 +276,7 @@ static struct pci_device_header virtio_net_pci_device = { .irq_line = VIRTIO_NET_IRQ, }; -static void virtio_net__tap_init (void ) +static void virtio_net__tap_init (const char *host_ip_addr) {struct ifreq ifr;int sock =socket (AF_INET ,SOCK_STREAM ,0 );
@@ -301,8 +301,7 @@ static void virtio_net__tap_init(void)strncpy (ifr.ifr_name, net_device.tap_name,sizeof (net_device.tap_name)); -/*FIXME: Remove this after user can specify ip address and netmask*/ - sin.sin_addr.s_addr =inet_addr ("192.168.33.2" ); + sin.sin_addr.s_addr =inet_addr (host_ip_addr);memcpy (&(ifr.ifr_addr), &sin,sizeof (ifr.ifr_addr)); ifr.ifr_addr.sa_family =AF_INET ;
@@ -331,11 +330,11 @@ static void virtio_net__io_thread_init(struct kvm *self)pthread_create (&net_device.io_tx_thread,NULL , virtio_net_tx_thread, (void *)self); } -void virtio_net__init (struct kvm *self) +void virtio_net__init (struct kvm *self,const char *host_ip_addr) {pci__register (&virtio_net_pci_device,PCI_VIRTIO_NET_DEVNUM );ioport__register (IOPORT_VIRTIO_NET , &virtio_net_io_ops,IOPORT_VIRTIO_NET_SIZE ); -virtio_net__tap_init (); +virtio_net__tap_init (host_ip_addr);virtio_net__io_thread_init (self); }
Signature · 1 line
1.7.5.rc1
Re: [PATCH 2/2 V2] kvm tools: Make host side IP configurable
Quoted 110 lines · Sasha Levin
Add --host-ip-addr parameter to allow changing the host-side IP address. Add a networking group to the cmdline menu.
Signed-off-by Sasha Levin <[email protected]>--- tools/kvm/include/kvm/virtio-net.h | 2 +- tools/kvm/kvm-run.c | 15 ++++++++++++--- tools/kvm/virtio-net.c | 9 ++++----- 3 files changed, 17 insertions(+), 9 deletions(-)tools/kvm/include/kvm/virtio-net.h +1
-1@@ -2,6 +2,6 @@#define KVM__VIRTIO_NET_H struct kvm ; -void virtio_net__init (struct kvm *self); +void virtio_net__init (struct kvm *self,const char *host_ip_addr);#endif /* KVM__VIRTIO_NET_H */ tools/kvm/kvm-run.c +12
-3@@ -31,6 +31,7 @@#define DEFAULT_KVM_DEV "/dev/kvm"#define DEFAULT_CONSOLE "serial"#define DEFAULT_NETWORK "none" +#define DEFAULT_HOST_ADDR "192.168.33.2"#define MB_SHIFT (20)#define MIN_RAM_SIZE_MB (64ULL)@@ -66,6 +67,7 @@ static const char *image_filename;static const char *console;static const char *kvm_dev;static const char *network; +static const char *host_ip_addr;static bool single_step;static bool readonly_image;extern bool ioport_debug;@@ -87,8 +89,6 @@ static const struct option options[] = { "Don't write changes back to disk image"),OPT_STRING ('c' ,"console" , &console,"serial or virtio" ,"Console to use" ), -OPT_STRING ('n' ,"network" , &network,"virtio" , -"Network to use" ),OPT_GROUP ("Kernel options:" ), OPT_STRING('k', "kernel", &kernel_filename, "kernel",@@ -98,6 +98,12 @@ static const struct option options[] = {OPT_STRING ('p' ,"params" , &kernel_cmdline,"params" ,"Kernel command line arguments" ), +OPT_GROUP ("Networking options:" ), +OPT_STRING ('n' ,"network" , &network,"virtio" , +"Network to use" ), +OPT_STRING ('\0' ,"host-ip-addr" , &host_ip_addr,"a.b.c.d" , +"Assign this address to the host side networking" ), +OPT_GROUP ("Debug options:" ),OPT_STRING ('d' ,"kvm-dev" , &kvm_dev,"kvm-dev" ,"KVM device file" ), OPT_BOOLEAN('s', "single-step", &single_step,@@ -218,6 +224,9 @@ int kvm_cmd_run(int argc, const char **argv, const char *prefix)else active_console =CONSOLE_8250 ; +if (!host_ip_addr) + host_ip_addr =DEFAULT_HOST_ADDR ; +term_init (); kvm =kvm__init (kvm_dev, ram_size);@@ -259,7 +268,7 @@ int kvm_cmd_run(int argc, const char **argv, const char *prefix) network =DEFAULT_NETWORK ;if (!strncmp (network,"virtio" ,6 )) -virtio_net__init (kvm); +virtio_net__init (kvm, host_ip_addr);kvm__start_timer (kvm);tools/kvm/virtio-net.c +2
-3@@ -276,7 +276,7 @@ static struct pci_device_header virtio_net_pci_device = { .irq_line = VIRTIO_NET_IRQ, }; -static void virtio_net__tap_init (void ) +static void virtio_net__tap_init (const char *host_ip_addr) {struct ifreq ifr;int sock =socket (AF_INET ,SOCK_STREAM ,0 );@@ -301,8 +301,7 @@ static void virtio_net__tap_init(void)strncpy (ifr.ifr_name, net_device.tap_name,sizeof (net_device.tap_name)); -/*FIXME: Remove this after user can specify ip address and netmask*/ - sin.sin_addr.s_addr =inet_addr ("192.168.33.2" ); + sin.sin_addr.s_addr =inet_addr (host_ip_addr);memcpy (&(ifr.ifr_addr), &sin,sizeof (ifr.ifr_addr)); ifr.ifr_addr.sa_family =AF_INET ;@@ -331,11 +330,11 @@ static void virtio_net__io_thread_init(struct kvm *self)pthread_create (&net_device.io_tx_thread,NULL , virtio_net_tx_thread, (void *)self); }
tools/kvm/virtio-net.c static void virtio_net__io_thread_init(struct kvm *self) · line 340
Quoted 10 lines
tools/kvm/virtio-net.c +2
-2-void virtio_net__init (struct kvm *self) +void virtio_net__init (struct kvm *self,const char *host_ip_addr) {pci__register (&virtio_net_pci_device,PCI_VIRTIO_NET_DEVNUM );ioport__register (IOPORT_VIRTIO_NET , &virtio_net_io_ops,IOPORT_VIRTIO_NET_SIZE ); -virtio_net__tap_init (); +virtio_net__tap_init (host_ip_addr);virtio_net__io_thread_init (self); }
Looks good to me.
Signature · 2 lines
Best Regards, Asias He
Quoted 53 lines · 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 <[email protected]>--- tools/kvm/virtio-net.c | 29 ++++++++++++++++++++++++++--- 1 files changed, 26 insertions(+), 3 deletions(-)tools/kvm/virtio-net.c +19
-3@@ -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));
tools/kvm/virtio-net.c static void virtio_net__tap_init(void) · line 322
Quoted 10 lines
tools/kvm/virtio-net.c +7
-0+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.
Signature · 2 lines
Best Regards, Asias He