diff mbox

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

Message ID 1437447928-24286-2-git-send-email-fan.du@intel.com (mailing list archive)
State New, archived
Headers show

Commit Message

Du, Fan July 21, 2015, 3:05 a.m. UTC
To detach tap device automatically from bridge when exiting,
just like what the reverse of "script" does.

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

Patch

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