[PATCHv4 kvmtool] kvmtool: Introduce downscript option for virtio-net

2015-08-04 Thread Fan Du
To detach tap device automatically from bridge when exiting,
just like what the reverse of script does.

Signed-off-by: Fan Du fan...@intel.com
---
 include/kvm/virtio-net.h |  1 +
 virtio/net.c | 49 
 2 files changed, 38 insertions(+), 12 deletions(-)

diff --git a/include/kvm/virtio-net.h b/include/kvm/virtio-net.h
index f435cc3..d136a09 100644
--- a/include/kvm/virtio-net.h
+++ b/include/kvm/virtio-net.h
@@ -9,6 +9,7 @@ struct virtio_net_params {
const char *guest_ip;
const char *host_ip;
const char *script;
+   const char *downscript;
const char *trans;
const char *tapif;
char guest_mac[6];
diff --git a/virtio/net.c b/virtio/net.c
index 4a6a855..a6e58db 100644
--- a/virtio/net.c
+++ b/virtio/net.c
@@ -294,10 +294,29 @@ static int virtio_net_request_tap(struct net_dev *ndev, 
struct ifreq *ifr,
return ret;
 }
 
+static int virtio_net_exec_script(const char* script, const char *tap_name)
+{
+   pid_t pid;
+   int status;
+
+   pid = fork();
+   if (pid == 0) {
+   execl(script, script, tap_name, NULL);
+   _exit(1);
+   } else {
+   waitpid(pid, status, 0);
+   if (WIFEXITED(status)  WEXITSTATUS(status) != 0) {
+   pr_warning(Fail to setup tap by %s, script);
+   return -1;
+   }
+   }
+   return 0;
+}
+
 static bool virtio_net__tap_init(struct net_dev *ndev)
 {
int sock = socket(AF_INET, SOCK_STREAM, 0);
-   int pid, status, offload, hdr_len;
+   int offload, hdr_len;
struct sockaddr_in sin = {0};
struct ifreq ifr;
const struct virtio_net_params *params = ndev-params;
@@ -339,17 +358,8 @@ static bool virtio_net__tap_init(struct net_dev *ndev)
}
 
if (strcmp(params-script, none)) {
-   pid = fork();
-   if (pid == 0) {
-   execl(params-script, params-script, ndev-tap_name, 
NULL);
-   _exit(1);
-   } else {
-   waitpid(pid, status, 0);
-   if (WIFEXITED(status)  WEXITSTATUS(status) != 0) {
-   pr_warning(Fail to setup tap by %s, 
params-script);
-   goto fail;
-   }
-   }
+   if(virtio_net_exec_script(params-script, ndev-tap_name)  0)
+   goto fail;
} else if (!skipconf) {
memset(ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, ndev-tap_name, sizeof(ndev-tap_name));
@@ -702,6 +712,8 @@ static int set_net_param(struct kvm *kvm, struct 
virtio_net_params *p,
die(Unknown network mode %s, please use user, tap or 
none, kvm-cfg.network);
} else if (strcmp(param, script) == 0) {
p-script = strdup(val);
+   } else if (strcmp(param, downscript) == 0) {
+   p-downscript = strdup(val);
} else if (strcmp(param, guest_ip) == 0) {
p-guest_ip = strdup(val);
} else if (strcmp(param, host_ip) == 0) {
@@ -740,6 +752,7 @@ int netdev_parser(const struct option *opt, const char 
*arg, int unset)
.guest_ip   = DEFAULT_GUEST_ADDR,
.host_ip= DEFAULT_HOST_ADDR,
.script = DEFAULT_SCRIPT,
+   .downscript = DEFAULT_SCRIPT,
.mode   = NET_MODE_TAP,
};
 
@@ -877,6 +890,18 @@ virtio_dev_init(virtio_net__init);
 
 int virtio_net__exit(struct kvm *kvm)
 {
+   struct virtio_net_params *params;
+   struct net_dev *ndev;
+   struct list_head *ptr;
+
+   list_for_each(ptr, ndevs) {
+   ndev = list_entry(ptr, struct net_dev, list);
+   params = ndev-params;
+   /* Cleanup any tap device which attached to bridge */
+   if (ndev-mode == NET_MODE_TAP 
+   strcmp(params-downscript, none))
+   virtio_net_exec_script(params-downscript, 
ndev-tap_name);
+   }
return 0;
 }
 virtio_dev_exit(virtio_net__exit);
-- 
1.8.3.1

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


[PATCHv3 1/2] kvmtool: Introduce downscript option for virtio-net

2015-07-21 Thread Fan Du
To detach tap device automatically from bridge when exiting,
just like what the reverse of script does.

Signed-off-by: Fan Du fan...@intel.com
---
 include/kvm/virtio-net.h |  1 +
 virtio/net.c | 49 
 2 files changed, 38 insertions(+), 12 deletions(-)

diff --git a/include/kvm/virtio-net.h b/include/kvm/virtio-net.h
index f435cc3..d136a09 100644
--- a/include/kvm/virtio-net.h
+++ b/include/kvm/virtio-net.h
@@ -9,6 +9,7 @@ struct virtio_net_params {
const char *guest_ip;
const char *host_ip;
const char *script;
+   const char *downscript;
const char *trans;
const char *tapif;
char guest_mac[6];
diff --git a/virtio/net.c b/virtio/net.c
index 4a6a855..d343615 100644
--- a/virtio/net.c
+++ b/virtio/net.c
@@ -294,10 +294,29 @@ static int virtio_net_request_tap(struct net_dev *ndev, 
struct ifreq *ifr,
return ret;
 }
 
+static int virtio_net_exec_script(const char* script, char *tap_name)
+{
+   int pid;
+   int status;
+
+   pid = fork();
+   if (pid == 0) {
+   execl(script, script, tap_name, NULL);
+   _exit(1);
+   } else {
+   waitpid(pid, status, 0);
+   if (WIFEXITED(status)  WEXITSTATUS(status) != 0) {
+   pr_warning(Fail to setup tap by %s, script);
+   return -1;
+   }
+   }
+   return 0;
+}
+
 static bool virtio_net__tap_init(struct net_dev *ndev)
 {
int sock = socket(AF_INET, SOCK_STREAM, 0);
-   int pid, status, offload, hdr_len;
+   int offload, hdr_len;
struct sockaddr_in sin = {0};
struct ifreq ifr;
const struct virtio_net_params *params = ndev-params;
@@ -339,17 +358,8 @@ static bool virtio_net__tap_init(struct net_dev *ndev)
}
 
if (strcmp(params-script, none)) {
-   pid = fork();
-   if (pid == 0) {
-   execl(params-script, params-script, ndev-tap_name, 
NULL);
-   _exit(1);
-   } else {
-   waitpid(pid, status, 0);
-   if (WIFEXITED(status)  WEXITSTATUS(status) != 0) {
-   pr_warning(Fail to setup tap by %s, 
params-script);
-   goto fail;
-   }
-   }
+   if(virtio_net_exec_script(params-script, ndev-tap_name)  0)
+   goto fail;
} else if (!skipconf) {
memset(ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, ndev-tap_name, sizeof(ndev-tap_name));
@@ -702,6 +712,8 @@ static int set_net_param(struct kvm *kvm, struct 
virtio_net_params *p,
die(Unknown network mode %s, please use user, tap or 
none, kvm-cfg.network);
} else if (strcmp(param, script) == 0) {
p-script = strdup(val);
+   } else if (strcmp(param, downscript) == 0) {
+   p-downscript = strdup(val);
} else if (strcmp(param, guest_ip) == 0) {
p-guest_ip = strdup(val);
} else if (strcmp(param, host_ip) == 0) {
@@ -877,6 +889,19 @@ virtio_dev_init(virtio_net__init);
 
 int virtio_net__exit(struct kvm *kvm)
 {
+   struct virtio_net_params *params;
+   struct net_dev *ndev;
+   struct list_head *ptr;
+
+   list_for_each(ptr, ndevs) {
+   ndev = list_entry(ptr, struct net_dev, list);
+   params = ndev-params;
+   /* Cleanup any tap device which attached to bridge */
+   if (ndev-mode == NET_MODE_TAP 
+   strcmp(params-downscript, none)) {
+   virtio_net_exec_script(params-downscript, 
ndev-tap_name);
+   }
+   }
return 0;
 }
 virtio_dev_exit(virtio_net__exit);
-- 
1.8.3.1

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


[PATCHv3 0/2] Fixes for kvmtool virtio-net part

2015-07-21 Thread Fan Du
Changelog:
v2:
  - Rebase with Will kvmtool.git tree[1]
  - Drop patch2 from v1, as commit f83dc816a9c7 has already fix it
v3:
  - Remove -Wunused-variable warnings

Fan Du (2):
  kvmtool: Introduce downscript option for virtio-net
  kvmtool: Restrict virtio queue number to 1 when vhost on

 include/kvm/virtio-net.h |  1 +
 virtio/net.c | 53 +---
 2 files changed, 42 insertions(+), 12 deletions(-)

-- 
1.8.3.1

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


[PATCHv3 2/2] kvmtool: Restrict virtio queue number to 1 when vhost on

2015-07-21 Thread Fan Du
vhost kernel driver does not support mutiple queue yet,
Tweak queue number will fail with --net mode=tap,vhost=1,mq=2
as below when lkvm trying to set ring kick fd for queue 2:

VHOST_SET_VRING_KICK failed: No buffer space available

Error on this scenario, and overide with the default one queue
configuration.

Signed-off-by: Fan Du fan...@intel.com
---
 virtio/net.c | 4 
 1 file changed, 4 insertions(+)

diff --git a/virtio/net.c b/virtio/net.c
index d343615..21a80f3 100644
--- a/virtio/net.c
+++ b/virtio/net.c
@@ -730,6 +730,10 @@ static int set_net_param(struct kvm *kvm, struct 
virtio_net_params *p,
p-mq = atoi(val);
} else
die(Unknown network parameter %s, param);
+   if (p-vhost  p-mq  1) {
+   p-mq = 1;
+   pr_err(vhost does not support mq yet, overide mq to 1.);
+   }
 
return 0;
 }
-- 
1.8.3.1

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


[PATCH 1/3] kvm-tool: Introduce downscript option

2015-07-20 Thread Fan Du
To detach tap device automatically from bridge when exiting,
just like the reverse of script does.

Signed-off-by: Fan Du fan...@intel.com
---
 tools/kvm/include/kvm/virtio-net.h |  1 +
 tools/kvm/virtio/net.c | 28 
 2 files changed, 29 insertions(+)

diff --git a/tools/kvm/include/kvm/virtio-net.h 
b/tools/kvm/include/kvm/virtio-net.h
index f435cc3..d136a09 100644
--- a/tools/kvm/include/kvm/virtio-net.h
+++ b/tools/kvm/include/kvm/virtio-net.h
@@ -9,6 +9,7 @@ struct virtio_net_params {
const char *guest_ip;
const char *host_ip;
const char *script;
+   const char *downscript;
const char *trans;
const char *tapif;
char guest_mac[6];
diff --git a/tools/kvm/virtio/net.c b/tools/kvm/virtio/net.c
index 25b9496..f3f7200 100644
--- a/tools/kvm/virtio/net.c
+++ b/tools/kvm/virtio/net.c
@@ -696,6 +696,8 @@ static int set_net_param(struct kvm *kvm, struct 
virtio_net_params *p,
die(Unknown network mode %s, please use user, tap or 
none, kvm-cfg.network);
} else if (strcmp(param, script) == 0) {
p-script = strdup(val);
+   } else if (strcmp(param, downscript) == 0) {
+   p-downscript = strdup(val);
} else if (strcmp(param, guest_ip) == 0) {
p-guest_ip = strdup(val);
} else if (strcmp(param, host_ip) == 0) {
@@ -871,6 +873,32 @@ virtio_dev_init(virtio_net__init);
 
 int virtio_net__exit(struct kvm *kvm)
 {
+   int pid;
+   int status;
+   struct virtio_net_params *params;
+   struct net_dev *ndev;
+   struct list_head *ptr;
+
+   list_for_each(ptr, ndevs) {
+   ndev = list_entry(ptr, struct net_dev, list);
+   params = ndev-params;
+   /* Cleanup any tap device which attached to bridge */
+   if (ndev-mode == NET_MODE_TAP ||
+   strcmp(params-downscript, none)) {
+   pid = fork();
+   if (pid == 0) {
+   execl(ndev-params-downscript,
+ params-downscript, ndev-tap_name, NULL);
+   _exit(1);
+   } else {
+   waitpid(pid, status, 0);
+   if (WIFEXITED(status)  WEXITSTATUS(status) != 
0) {
+   pr_warning(Fail to cleanup tap by %s,
+  params-script);
+   }
+   }
+   }
+   }
return 0;
 }
 virtio_dev_exit(virtio_net__exit);
-- 
1.8.3.1

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


[PATCH 3/3] kvm-tool: Restrict queue number to 1 when vhost on

2015-07-20 Thread Fan Du
vhost kernel driver does not support mutiple queue yet,
Tweak queue number will fail with --net mode=tap,vhost=1,mq=2
as below when lkvm trying to set ring kick fd for queue 2:

VHOST_SET_VRING_KICK failed: No buffer space available

Error on this scenario, and overide with the default one queue
configuration.

Signed-off-by: Fan Du fan...@intel.com
---
 tools/kvm/virtio/net.c | 4 
 1 file changed, 4 insertions(+)

diff --git a/tools/kvm/virtio/net.c b/tools/kvm/virtio/net.c
index 5678ff2..32159f1 100644
--- a/tools/kvm/virtio/net.c
+++ b/tools/kvm/virtio/net.c
@@ -714,6 +714,10 @@ static int set_net_param(struct kvm *kvm, struct 
virtio_net_params *p,
p-mq = atoi(val);
} else
die(Unknown network parameter %s, param);
+   if (p-vhost  p-mq  1) {
+   p-mq = 1;
+   pr_err(Virtio vhost does not support mq yet, overide mq to 
1.);
+   }
 
return 0;
 }
-- 
1.8.3.1

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


[PATCH 2/3] kvm-tool: Pass address of pointer to ioctl for tap

2015-07-20 Thread Fan Du
When enable virtio tap mode as root previliege by:
lkvm run --disk linux-0.2.img  --kernel bzImage --network virtio

lkvm spits:
[1.981352] loop: module loaded
[1.986039]  vda:
  Warning: Config tap device error. Are you root?
You have requested a TAP device, but creation of one has failed because: 
Invalid argument

The last param of ioctl should be a pointer address

Signed-off-by: Fan Du fan...@intel.com
Fixes: e325f3e77e78 (kvmtool: Add minimal support for macvtap)
---
 tools/kvm/virtio/net.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/kvm/virtio/net.c b/tools/kvm/virtio/net.c
index f3f7200..5678ff2 100644
--- a/tools/kvm/virtio/net.c
+++ b/tools/kvm/virtio/net.c
@@ -286,7 +286,7 @@ static int virtio_net_request_tap(struct net_dev *ndev, 
struct ifreq *ifr,
if (tapname)
strncpy(ifr-ifr_name, tapname, sizeof(ifr-ifr_name));
 
-   ret = ioctl(ndev-tap_fd, TUNSETIFF, ifr);
+   ret = ioctl(ndev-tap_fd, TUNSETIFF, ifr);
 
if (ret = 0)
strncpy(ndev-tap_name, ifr-ifr_name, sizeof(ndev-tap_name));
-- 
1.8.3.1

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


[PATCH 0/3] Fixes for kvmtool virtio-net part

2015-07-20 Thread Fan Du
Hallo

This changset provide some fix and small enhancement
for kvm tool when I play it with virtio part.

Fan Du (3):
  kvm-tool: Introduce downscript option
  kvm-tool: Pass address of pointer to ioctl for tap
  kvm-tool: Restrict queue number to 1 when vhost on

 tools/kvm/include/kvm/virtio-net.h |  1 +
 tools/kvm/virtio/net.c | 34 +-
 2 files changed, 34 insertions(+), 1 deletion(-)

-- 
1.8.3.1

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


[PATCH 0/2] Fixes for kvmtool virtio-net part

2015-07-20 Thread Fan Du
This changset provide some fix and small enhancement
for kvm tool when I play it for the firt time.

Changelog:
v2:
  - Rebase with Will kvmtool.git tree[1]
  - Drop patch2 from v1, as commit f83dc816a9c7 has already fix it

[1]: https://git.kernel.org/pub/scm/linux/kernel/git/will/kvmtool.git

Fan Du (2):
  kvmtool: Introduce downscript option for virtio-net
  kvmtool: Restrict virtio queue number to 1 when vhost on

 include/kvm/virtio-net.h |  1 +
 virtio/net.c | 54 ++--
 2 files changed, 44 insertions(+), 11 deletions(-)

-- 
1.8.3.1

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


[PATCH 1/2] kvmtool: Introduce downscript option for virtio-net

2015-07-20 Thread Fan Du
To detach tap device automatically from bridge when exiting,
just like what the reverse of script does.

Signed-off-by: Fan Du fan...@intel.com
---
 include/kvm/virtio-net.h |  1 +
 virtio/net.c | 50 +---
 2 files changed, 40 insertions(+), 11 deletions(-)

diff --git a/include/kvm/virtio-net.h b/include/kvm/virtio-net.h
index f435cc3..d136a09 100644
--- a/include/kvm/virtio-net.h
+++ b/include/kvm/virtio-net.h
@@ -9,6 +9,7 @@ struct virtio_net_params {
const char *guest_ip;
const char *host_ip;
const char *script;
+   const char *downscript;
const char *trans;
const char *tapif;
char guest_mac[6];
diff --git a/virtio/net.c b/virtio/net.c
index 4a6a855..55dd9d9 100644
--- a/virtio/net.c
+++ b/virtio/net.c
@@ -294,6 +294,26 @@ static int virtio_net_request_tap(struct net_dev *ndev, 
struct ifreq *ifr,
return ret;
 }
 
+static int virtio_net_exec_script(const char* script, char *tap_name)
+{
+   int ret;
+   int pid;
+   int status;
+
+   pid = fork();
+   if (pid == 0) {
+   execl(script, script, tap_name, NULL);
+   _exit(1);
+   } else {
+   waitpid(pid, status, 0);
+   if (WIFEXITED(status)  WEXITSTATUS(status) != 0) {
+   pr_warning(Fail to setup tap by %s, script);
+   return -1;
+   }
+   }
+   return 0;
+}
+
 static bool virtio_net__tap_init(struct net_dev *ndev)
 {
int sock = socket(AF_INET, SOCK_STREAM, 0);
@@ -339,17 +359,8 @@ static bool virtio_net__tap_init(struct net_dev *ndev)
}
 
if (strcmp(params-script, none)) {
-   pid = fork();
-   if (pid == 0) {
-   execl(params-script, params-script, ndev-tap_name, 
NULL);
-   _exit(1);
-   } else {
-   waitpid(pid, status, 0);
-   if (WIFEXITED(status)  WEXITSTATUS(status) != 0) {
-   pr_warning(Fail to setup tap by %s, 
params-script);
-   goto fail;
-   }
-   }
+   if(virtio_net_exec_script(params-script, ndev-tap_name)  0)
+   goto fail;
} else if (!skipconf) {
memset(ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, ndev-tap_name, sizeof(ndev-tap_name));
@@ -702,6 +713,8 @@ static int set_net_param(struct kvm *kvm, struct 
virtio_net_params *p,
die(Unknown network mode %s, please use user, tap or 
none, kvm-cfg.network);
} else if (strcmp(param, script) == 0) {
p-script = strdup(val);
+   } else if (strcmp(param, downscript) == 0) {
+   p-downscript = strdup(val);
} else if (strcmp(param, guest_ip) == 0) {
p-guest_ip = strdup(val);
} else if (strcmp(param, host_ip) == 0) {
@@ -877,6 +890,21 @@ virtio_dev_init(virtio_net__init);
 
 int virtio_net__exit(struct kvm *kvm)
 {
+   int pid;
+   int status;
+   struct virtio_net_params *params;
+   struct net_dev *ndev;
+   struct list_head *ptr;
+
+   list_for_each(ptr, ndevs) {
+   ndev = list_entry(ptr, struct net_dev, list);
+   params = ndev-params;
+   /* Cleanup any tap device which attached to bridge */
+   if (ndev-mode == NET_MODE_TAP 
+   strcmp(params-downscript, none)) {
+   virtio_net_exec_script(params-downscript, 
ndev-tap_name);
+   }
+   }
return 0;
 }
 virtio_dev_exit(virtio_net__exit);
-- 
1.8.3.1

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


[PATCH 2/2] kvmtool: Restrict virtio queue number to 1 when vhost on

2015-07-20 Thread Fan Du
vhost kernel driver does not support mutiple queue yet,
Tweak queue number will fail with --net mode=tap,vhost=1,mq=2
as below when lkvm trying to set ring kick fd for queue 2:

VHOST_SET_VRING_KICK failed: No buffer space available

Error on this scenario, and overide with the default one queue
configuration.

Signed-off-by: Fan Du fan...@intel.com
---
 virtio/net.c | 4 
 1 file changed, 4 insertions(+)

diff --git a/virtio/net.c b/virtio/net.c
index 55dd9d9..78199b9 100644
--- a/virtio/net.c
+++ b/virtio/net.c
@@ -731,6 +731,10 @@ static int set_net_param(struct kvm *kvm, struct 
virtio_net_params *p,
p-mq = atoi(val);
} else
die(Unknown network parameter %s, param);
+   if (p-vhost  p-mq  1) {
+   p-mq = 1;
+   pr_err(vhost does not support mq yet, overide mq to 1.);
+   }
 
return 0;
 }
-- 
1.8.3.1

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