kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/3] kvm tools: Handle multiple IPC cmd at a time
@ 2011-12-21 12:13 Asias He
  2011-12-21 12:13 ` [PATCH 2/3] kvm tools: Introduce KVM_IPC_VMSTATE IPC cmd Asias He
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Asias He @ 2011-12-21 12:13 UTC (permalink / raw)
  To: Pekka Enberg; +Cc: Sasha Levin, Cyrill Gorcunov, Ingo Molnar, kvm, Asias He

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

Signed-off-by: Asias He <asias.hejun@gmail.com>
---
 tools/kvm/kvm-ipc.c |   15 ++++++++++++---
 1 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/tools/kvm/kvm-ipc.c b/tools/kvm/kvm-ipc.c
index 68c2565..f2c3523 100644
--- a/tools/kvm/kvm-ipc.c
+++ b/tools/kvm/kvm-ipc.c
@@ -74,7 +74,7 @@ static void kvm_ipc__close_conn(int fd)
 	close(fd);
 }
 
-static void kvm_ipc__new_data(int fd)
+static int kvm_ipc__new_data(int fd)
 {
 	struct kvm_ipc_msg *msg;
 	u32 n;
@@ -97,8 +97,11 @@ static void kvm_ipc__new_data(int fd)
 
 	kvm_ipc__handle(fd, msg);
 
+	return 0;
+
 done:
 	free(msg);
+	return -1;
 }
 
 static void *kvm_ipc__thread(void *param)
@@ -115,10 +118,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__new_data(client);
+				/*
+				 * Handle multiple IPC cmd at a time
+				 */
+				do {
+					r = kvm_ipc__new_data(client);
+				} while	(r == 0);
+
 			} else if (event.events && (EPOLLERR | EPOLLRDHUP | EPOLLHUP)) {
 				kvm_ipc__close_conn(fd);
 			} else {
-- 
1.7.7.3


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH 2/3] kvm tools: Introduce KVM_IPC_VMSTATE IPC cmd
  2011-12-21 12:13 [PATCH 1/3] kvm tools: Handle multiple IPC cmd at a time Asias He
@ 2011-12-21 12:13 ` Asias He
  2011-12-21 12:13 ` [PATCH 3/3] kvm tools: Respect paused status in ./lkvm list Asias He
  2011-12-21 20:30 ` [PATCH 1/3] kvm tools: Handle multiple IPC cmd at a time Pekka Enberg
  2 siblings, 0 replies; 5+ messages in thread
From: Asias He @ 2011-12-21 12:13 UTC (permalink / raw)
  To: Pekka Enberg; +Cc: Sasha Levin, Cyrill Gorcunov, Ingo Molnar, kvm, Asias He

This can be used to get vm status information:

vm is running or pasued.

Signed-off-by: Asias He <asias.hejun@gmail.com>
---
 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(-)

diff --git a/tools/kvm/builtin-run.c b/tools/kvm/builtin-run.c
index fac274c..2f48685 100644
--- a/tools/kvm/builtin-run.c
+++ b/tools/kvm/builtin-run.c
@@ -507,17 +507,30 @@ static int is_paused;
 
 static void handle_pause(int fd, u32 type, u32 len, u8 *msg)
 {
-	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
 		return;
 
 	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)
+{
+	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;
@@ -862,6 +875,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);
 
diff --git a/tools/kvm/include/kvm/kvm-ipc.h b/tools/kvm/include/kvm/kvm-ipc.h
index 731767f..c68b89c 100644
--- a/tools/kvm/include/kvm/kvm-ipc.h
+++ b/tools/kvm/include/kvm/kvm-ipc.h
@@ -17,6 +17,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));
diff --git a/tools/kvm/include/kvm/kvm.h b/tools/kvm/include/kvm/kvm.h
index 75adb25..d88ff74 100644
--- a/tools/kvm/include/kvm/kvm.h
+++ b/tools/kvm/include/kvm/kvm.h
@@ -21,6 +21,11 @@
 	.name = #ext,			\
 	.code = ext
 
+enum {
+	KVM_VMSTATE_RUNNING,
+	KVM_VMSTATE_PAUSED,
+};
+
 struct kvm_ext {
 	const char *name;
 	int code;
diff --git a/tools/kvm/powerpc/include/kvm/kvm-arch.h b/tools/kvm/powerpc/include/kvm/kvm-arch.h
index 10aa2d9..c4b493c 100644
--- a/tools/kvm/powerpc/include/kvm/kvm-arch.h
+++ b/tools/kvm/powerpc/include/kvm/kvm-arch.h
@@ -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 */
diff --git a/tools/kvm/x86/include/kvm/kvm-arch.h b/tools/kvm/x86/include/kvm/kvm-arch.h
index 1ce3d31..244d36c 100644
--- a/tools/kvm/x86/include/kvm/kvm-arch.h
+++ b/tools/kvm/x86/include/kvm/kvm-arch.h
@@ -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 */
-- 
1.7.7.3


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH 3/3] kvm tools: Respect paused status in ./lkvm list
  2011-12-21 12:13 [PATCH 1/3] kvm tools: Handle multiple IPC cmd at a time Asias He
  2011-12-21 12:13 ` [PATCH 2/3] kvm tools: Introduce KVM_IPC_VMSTATE IPC cmd Asias He
@ 2011-12-21 12:13 ` Asias He
  2011-12-21 20:30 ` [PATCH 1/3] kvm tools: Handle multiple IPC cmd at a time Pekka Enberg
  2 siblings, 0 replies; 5+ messages in thread
From: Asias He @ 2011-12-21 12:13 UTC (permalink / raw)
  To: Pekka Enberg; +Cc: Sasha Levin, Cyrill Gorcunov, Ingo Molnar, kvm, 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

Signed-off-by: Asias He <asias.hejun@gmail.com>
---
 tools/kvm/builtin-list.c |   35 +++++++++++++++++++++++++++++++++--
 1 files changed, 33 insertions(+), 2 deletions(-)

diff --git a/tools/kvm/builtin-list.c b/tools/kvm/builtin-list.c
index ec1c067..e74b636 100644
--- a/tools/kvm/builtin-list.c
+++ b/tools/kvm/builtin-list.c
@@ -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);
 
-- 
1.7.7.3


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH 1/3] kvm tools: Handle multiple IPC cmd at a time
  2011-12-21 12:13 [PATCH 1/3] kvm tools: Handle multiple IPC cmd at a time Asias He
  2011-12-21 12:13 ` [PATCH 2/3] kvm tools: Introduce KVM_IPC_VMSTATE IPC cmd Asias He
  2011-12-21 12:13 ` [PATCH 3/3] kvm tools: Respect paused status in ./lkvm list Asias He
@ 2011-12-21 20:30 ` Pekka Enberg
  2011-12-22  2:11   ` Asias He
  2 siblings, 1 reply; 5+ messages in thread
From: Pekka Enberg @ 2011-12-21 20:30 UTC (permalink / raw)
  To: Asias He; +Cc: Sasha Levin, Cyrill Gorcunov, Ingo Molnar, kvm

On Wed, 21 Dec 2011, Asias He wrote:
> This is useful when client submiting multiple IPC cmd in one socket
> connection.
>
> Signed-off-by: Asias He <asias.hejun@gmail.com>

This patch doesn't apply on top of master after Lai's patches. Can you 
please resend?

 			Pekka

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 1/3] kvm tools: Handle multiple IPC cmd at a time
  2011-12-21 20:30 ` [PATCH 1/3] kvm tools: Handle multiple IPC cmd at a time Pekka Enberg
@ 2011-12-22  2:11   ` Asias He
  0 siblings, 0 replies; 5+ messages in thread
From: Asias He @ 2011-12-22  2:11 UTC (permalink / raw)
  To: Pekka Enberg; +Cc: Sasha Levin, Cyrill Gorcunov, Ingo Molnar, kvm

On 12/22/2011 04:30 AM, Pekka Enberg wrote:
> On Wed, 21 Dec 2011, Asias He wrote:
>> This is useful when client submiting multiple IPC cmd in one socket
>> connection.
>>
>> Signed-off-by: Asias He <asias.hejun@gmail.com>
> 
> This patch doesn't apply on top of master after Lai's patches. Can you
> please resend?

Sure. Done.


-- 
Asias He

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2011-12-22  2:13 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-12-21 12:13 [PATCH 1/3] kvm tools: Handle multiple IPC cmd at a time Asias He
2011-12-21 12:13 ` [PATCH 2/3] kvm tools: Introduce KVM_IPC_VMSTATE IPC cmd Asias He
2011-12-21 12:13 ` [PATCH 3/3] kvm tools: Respect paused status in ./lkvm list Asias He
2011-12-21 20:30 ` [PATCH 1/3] kvm tools: Handle multiple IPC cmd at a time Pekka Enberg
2011-12-22  2:11   ` Asias He

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).