Skip to contentSkip to conversation

kvm tools: Handle multiple IPC cmd at a time

PATCH New Asias He 1 participant 6 messages Dec 22, 2011 KVM

lore
Share & tools
lore

Asias He

author
#0 lore raw

PATCH 1/6 kvm tools: Handle multiple IPC cmd at a time

This is useful when client submiting multiple IPC cmd in one socket connection.

---
 tools/kvm/kvm-ipc.c |   15 ++++++++++++---
 1 files changed, 12 insertions(+), 3 deletions(-)
tools/kvm/kvm-ipc.c +12 -3
index 2036b91..dbe2363 100644
@@ -102,7 +102,7 @@ static void kvm_ipc__close_conn(int fd)
 	close(fd);
 }
 
-static void kvm_ipc__receive(int fd)
+static int kvm_ipc__receive(int fd)
 {
 	struct kvm_ipc_head head;
 	u8 *msg = NULL;
@@ -122,8 +122,11 @@ static void kvm_ipc__receive(int fd)
 
 	kvm_ipc__handle(fd, head.type, head.len, msg);
 
+	return 0;
+
 done:
 	free(msg);
+	return -1;
 }
 
 static void *kvm_ipc__thread(void *param)
@@ -140,10 +143,16 @@ static void *kvm_ipc__thread(void *param)
 			if (fd == stop_fd && event.events & EPOLLIN) {
 				break;
 			} else if (fd == server_fd) {
-				int client;
+				int client, r;
 
 				client = kvm_ipc__new_conn(fd);
-				kvm_ipc__receive(client);
+				/*
+				 * Handle multiple IPC cmd at a time
+				 */
+				do {
+					r = kvm_ipc__receive(client);
+				} while	(r == 0);
+
 			} else if (event.events && (EPOLLERR | EPOLLRDHUP | EPOLLHUP)) {
 				kvm_ipc__close_conn(fd);
 			} else {
Signature · 1 line
1.7.7.3
author
#1 lore raw

PATCH 2/6 kvm tools: Introduce KVM_IPC_VMSTATE IPC cmd

This can be used to get vm status information:

vm is running or pasued.

---
 tools/kvm/builtin-run.c                  |   20 +++++++++++++++++---
 tools/kvm/include/kvm/kvm-ipc.h          |    1 +
 tools/kvm/include/kvm/kvm.h              |    5 +++++
 tools/kvm/powerpc/include/kvm/kvm-arch.h |    1 +
 tools/kvm/x86/include/kvm/kvm-arch.h     |    2 ++
 5 files changed, 26 insertions(+), 3 deletions(-)
tools/kvm/builtin-run.c +17 -3
index a94fdbc..80806a1 100644
@@ -510,11 +510,13 @@ static void handle_pause(int fd, u32 type, u32 len, u8 *msg)
 	if (WARN_ON(len))
 		return;
 
-	if (type == KVM_IPC_RESUME && is_paused)
+	if (type == KVM_IPC_RESUME && is_paused) {
+		kvm->vm_state = KVM_VMSTATE_RUNNING;
 		kvm__continue();
-	else if (type == KVM_IPC_PAUSE && !is_paused)
+	} else if (type == KVM_IPC_PAUSE && !is_paused) {
+		kvm->vm_state = KVM_VMSTATE_PAUSED;
 		kvm__pause();
-	else {
+	} else {
 		WARN_ON(1);
 		return;
 	}
@@ -523,6 +525,17 @@ static void handle_pause(int fd, u32 type, u32 len, u8 *msg)
 	pr_info("Guest %s\n", is_paused ? "paused" : "resumed");
 }
 
+static void handle_vmstate(int fd, u32 type, u32 len, u8 *msg)
+{
+	int r = 0;
+
+	if (type == KVM_IPC_VMSTATE)
+		r = write(fd, &kvm->vm_state, sizeof(kvm->vm_state));
+
+	if (r < 0)
+		pr_warning("Failed sending VMSTATE");
+}
+
 static void handle_debug(int fd, u32 type, u32 len, u8 *msg)
 {
 	int i;
@@ -877,6 +890,7 @@ int kvm_cmd_run(int argc, const char **argv, const char *prefix)
 	kvm_ipc__register_handler(KVM_IPC_PAUSE, handle_pause);
 	kvm_ipc__register_handler(KVM_IPC_RESUME, handle_pause);
 	kvm_ipc__register_handler(KVM_IPC_STOP, handle_stop);
+	kvm_ipc__register_handler(KVM_IPC_VMSTATE, handle_vmstate);
 
 	nr_online_cpus = sysconf(_SC_NPROCESSORS_ONLN);
 
tools/kvm/include/kvm/kvm-ipc.h +1 -0
index 5916c40..aefffa4 100644
@@ -11,6 +11,7 @@ enum {
 	KVM_IPC_RESUME	= 5,
 	KVM_IPC_STOP	= 6,
 	KVM_IPC_PID	= 7,
+	KVM_IPC_VMSTATE	= 8,
 };
 
 int kvm_ipc__register_handler(u32 type, void (*cb)(int fd, u32 type, u32 len, u8 *msg));
tools/kvm/include/kvm/kvm.h +5 -0
index a3ea1c9..2e28586 100644
@@ -21,6 +21,11 @@
 	.name = #ext,			\
 	.code = ext
 
+enum {
+	KVM_VMSTATE_RUNNING,
+	KVM_VMSTATE_PAUSED,
+};
+
 struct kvm_ext {
 	const char *name;
 	int code;
tools/kvm/powerpc/include/kvm/kvm-arch.h +1 -0
index 10aa2d9..c4b493c 100644
@@ -65,6 +65,7 @@ struct kvm {
 	unsigned long		initrd_gra;
 	unsigned long		initrd_size;
 	const char		*name;
+	int			vm_state;
 };
 
 #endif /* KVM__KVM_ARCH_H */
tools/kvm/x86/include/kvm/kvm-arch.h +2 -0
index 1ce3d31..244d36c 100644
@@ -48,6 +48,8 @@ struct kvm {
 	int                     nr_disks;
 
 	const char		*name;
+
+	int			vm_state;
 };
 
 static inline void *guest_flat_to_host(struct kvm *kvm, unsigned long offset); /* In kvm.h */
Signature · 1 line
1.7.7.3
author
#2 lore raw

PATCH 3/6 kvm tools: Respect paused status in ./lkvm list

In reply to Asias He

'./lkvm list' does not give the correct vm status if the vm is pasued currently.

This patch fixes this by using KVM_IPC_VMSTATE IPC cmd to query the vm status.

Suppose the guest is paused by ./lkvm pause:

Before:

asias@hj$ ./lkvm list
   PID NAME                 STATE
------------------------------------
 3036 guest-3036           running
      default              shut off

After:

asias@hj$ ./lkvm list
   PID NAME                 STATE
------------------------------------
 3036 guest-3036           paused
      default              shut off
---
 tools/kvm/builtin-list.c |   35 +++++++++++++++++++++++++++++++++--
 1 files changed, 33 insertions(+), 2 deletions(-)
tools/kvm/builtin-list.c +33 -2
index ec1c067..e74b636 100644
@@ -16,6 +16,11 @@ struct pid_cmd {
 	u32 len;
 };
 
+struct vmstate_cmd {
+	u32 type;
+	u32 len;
+};
+
 static bool run;
 static bool rootfs;
 
@@ -32,6 +37,7 @@ static const struct option list_options[] = {
 };
 
 #define KVM_INSTANCE_RUNNING	"running"
+#define KVM_INSTANCE_PAUSED	"paused"
 #define KVM_INSTANCE_SHUTOFF	"shut off"
 
 void kvm_list_help(void)
@@ -56,12 +62,34 @@ static pid_t get_pid(int sock)
 	return pid;
 }
 
+static int get_vmstate(int sock)
+{
+	struct vmstate_cmd cmd = {KVM_IPC_VMSTATE, 0};
+	int r;
+	int vmstate;
+
+	r = write(sock, &cmd, sizeof(cmd));
+	if (r < 0)
+		return r;
+
+	r = read(sock, &vmstate, sizeof(vmstate));
+	if (r < 0)
+		return r;
+
+	return vmstate;
+
+}
+
 static int print_guest(const char *name, int sock)
 {
 	char proc_name[PATH_MAX];
 	char *comm = NULL;
 	FILE *fd;
-	pid_t pid = get_pid(sock);
+	pid_t pid;
+	int vmstate;
+
+	pid = get_pid(sock);
+	vmstate = get_vmstate(sock);
 
 	sprintf(proc_name, "/proc/%d/stat", pid);
 	fd = fopen(proc_name, "r");
@@ -70,7 +98,10 @@ static int print_guest(const char *name, int sock)
 	if (fscanf(fd, "%*u (%as)", &comm) == 0)
 		goto cleanup;
 
-	printf("%5d %-20s %s\n", pid, name, KVM_INSTANCE_RUNNING);
+	if (vmstate == KVM_VMSTATE_PAUSED)
+		printf("%5d %-20s %s\n", pid, name, KVM_INSTANCE_PAUSED);
+	else
+		printf("%5d %-20s %s\n", pid, name, KVM_INSTANCE_RUNNING);
 
 	free(comm);
 
Signature · 1 line
1.7.7.3
author
#3 lore raw

PATCH 4/6 kvm tools: Improve 'lkvm {pause,resume}'s output

In reply to Asias He

lkvm {pause,resume} do not give any feedback to user who uses these commands in the console where the command run.

This patch makes the command output in the command console instead of guest console.

---
 tools/kvm/builtin-pause.c  |   10 +++++++++-
 tools/kvm/builtin-resume.c |   10 +++++++++-
 tools/kvm/builtin-run.c    |    1 -
 3 files changed, 18 insertions(+), 3 deletions(-)
tools/kvm/builtin-pause.c +9 -1
index ee5a5b8..436963f 100644
@@ -41,7 +41,15 @@ void kvm_pause_help(void)
 
 static int do_pause(const char *name, int sock)
 {
-	return kvm_ipc__send(sock, KVM_IPC_PAUSE);
+	int r;
+
+	r = kvm_ipc__send(sock, KVM_IPC_PAUSE);
+	if (r)
+		return r;
+
+	printf("Guest %s paused\n", name);
+
+	return 0;
 }
 
 int kvm_cmd_pause(int argc, const char **argv, const char *prefix)
tools/kvm/builtin-resume.c +9 -1
index 9c47183..033a05b 100644
@@ -41,7 +41,15 @@ void kvm_resume_help(void)
 
 static int do_resume(const char *name, int sock)
 {
-	return kvm_ipc__send(sock, KVM_IPC_RESUME);
+	int r;
+
+	r = kvm_ipc__send(sock, KVM_IPC_RESUME);
+	if (r)
+		return r;
+
+	printf("Guest %s resumed\n", name);
+
+	return 0;
 }
 
 int kvm_cmd_resume(int argc, const char **argv, const char *prefix)
tools/kvm/builtin-run.c +0 -1
index 80806a1..bf39cfb 100644
@@ -522,7 +522,6 @@ static void handle_pause(int fd, u32 type, u32 len, u8 *msg)
 	}
 
 	is_paused = !is_paused;
-	pr_info("Guest %s\n", is_paused ? "paused" : "resumed");
 }
 
 static void handle_vmstate(int fd, u32 type, u32 len, u8 *msg)
Signature · 1 line
1.7.7.3
author
#4 lore raw

PATCH 5/6 kvm tools: Remove resume pause noise.

In reply to Asias He

Remove this annoying message if user resumes or pauses a guest whcih is already resumed or paused.

Warning: (builtin-run.c) handle_pause:520: failed condition: 1

---
 tools/kvm/builtin-run.c |    1 -
 1 files changed, 0 insertions(+), 1 deletions(-)
tools/kvm/builtin-run.c +0 -1
index bf39cfb..4691e1b 100644
@@ -517,7 +517,6 @@ static void handle_pause(int fd, u32 type, u32 len, u8 *msg)
 		kvm->vm_state = KVM_VMSTATE_PAUSED;
 		kvm__pause();
 	} else {
-		WARN_ON(1);
 		return;
 	}
 
Signature · 1 line
1.7.7.3
author
#5 lore raw

PATCH 6/6 kvm tools: Use kvm_ipc__send to send IPC msg

In reply to Asias He

No need to send IPC msg using bare write() any more.

---
 tools/kvm/builtin-list.c |   20 ++++----------------
 1 files changed, 4 insertions(+), 16 deletions(-)
tools/kvm/builtin-list.c +4 -16
index e74b636..eb0ac6c 100644
@@ -11,16 +11,6 @@
 #include <signal.h>
 #include <fcntl.h>
 
-struct pid_cmd {
-	u32 type;
-	u32 len;
-};
-
-struct vmstate_cmd {
-	u32 type;
-	u32 len;
-};
-
 static bool run;
 static bool rootfs;
 
@@ -47,11 +37,10 @@ void kvm_list_help(void)
 
 static pid_t get_pid(int sock)
 {
-	struct pid_cmd cmd = {KVM_IPC_PID, 0};
-	int r;
 	pid_t pid;
+	int r;
 
-	r = write(sock, &cmd, sizeof(cmd));
+	r = kvm_ipc__send(sock, KVM_IPC_PID);
 	if (r < 0)
 		return r;
 
@@ -64,11 +53,10 @@ static pid_t get_pid(int sock)
 
 static int get_vmstate(int sock)
 {
-	struct vmstate_cmd cmd = {KVM_IPC_VMSTATE, 0};
-	int r;
 	int vmstate;
+	int r;
 
-	r = write(sock, &cmd, sizeof(cmd));
+	r = kvm_ipc__send(sock, KVM_IPC_VMSTATE);
 	if (r < 0)
 		return r;
 
Signature · 1 line
1.7.7.3