Skip to contentSkip to conversation

kvm tools: Switch to using UNIX sockets instead of signals

PATCH Superseded Sasha Levin 1 participant 3 messages Oct 16, 2011 KVM

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/3 kvm tools: Switch to using UNIX sockets instead of signals

This patch changes the IPC method to use UNIX sockets instead of signals.

This allows for more flexibility in moving data between processes, for example it would allow avoid printing output in the terminal of the guest.

---
 tools/kvm/Makefile              |    1 +
 tools/kvm/builtin-balloon.c     |   35 +++++++---
 tools/kvm/builtin-debug.c       |   30 ++++++---
 tools/kvm/builtin-list.c        |   28 +++++++-
 tools/kvm/builtin-pause.c       |   30 ++++++---
 tools/kvm/builtin-resume.c      |   30 ++++++---
 tools/kvm/builtin-run.c         |   22 +++---
 tools/kvm/builtin-stat.c        |   36 ++++++----
 tools/kvm/builtin-stop.c        |   30 ++++++---
 tools/kvm/include/kvm/kvm-ipc.h |   26 +++++++
 tools/kvm/include/kvm/kvm.h     |    7 +--
 tools/kvm/kvm-ipc.c             |  147 +++++++++++++++++++++++++++++++++++++++
 tools/kvm/kvm.c                 |  106 ++++++++++++++++++----------
 tools/kvm/virtio/balloon.c      |   30 ++++-----
 14 files changed, 426 insertions(+), 132 deletions(-)
 create mode 100644 tools/kvm/include/kvm/kvm-ipc.h
 create mode 100644 tools/kvm/kvm-ipc.c
tools/kvm/Makefile +1 -0
index 3939c11..5593417 100644
@@ -82,6 +82,7 @@ OBJS	+= virtio/9p-pdu.o
 OBJS	+= hw/vesa.o
 OBJS	+= hw/i8042.o
 OBJS	+= hw/pci-shmem.o
+OBJS	+= kvm-ipc.o
 
 FLAGS_BFD := $(CFLAGS) -lbfd
 has_bfd := $(call try-cc,$(SOURCE_BFD),$(FLAGS_BFD))
tools/kvm/builtin-balloon.c +25 -10
index 7329063..4a42ee8 100644
@@ -7,12 +7,19 @@
 #include <kvm/builtin-balloon.h>
 #include <kvm/parse-options.h>
 #include <kvm/kvm.h>
+#include <kvm/kvm-ipc.h>
 
-static pid_t instance_pid;
+static int instance;
 static const char *instance_name;
 static u64 inflate;
 static u64 deflate;
 
+struct balloon_cmd {
+	u32 type;
+	u32 len;
+	int amount;
+};
+
 static const char * const balloon_usage[] = {
 	"kvm balloon [-n name] [-p pid] [-i amount] [-d amount]",
 	NULL
@@ -21,7 +28,6 @@ static const char * const balloon_usage[] = {
 static const struct option balloon_options[] = {
 	OPT_GROUP("Instance options:"),
 	OPT_STRING('n', "name", &instance_name, "name", "Instance name"),
-	OPT_INTEGER('p', "pid", &instance_pid, "Instance pid"),
 	OPT_GROUP("Balloon options:"),
 	OPT_U64('i', "inflate", &inflate, "Amount to inflate"),
 	OPT_U64('d', "deflate", &deflate, "Amount to deflate"),
@@ -45,7 +51,8 @@ static void parse_balloon_options(int argc, const char **argv)
 
 int kvm_cmd_balloon(int argc, const char **argv, const char *prefix)
 {
-	u64 i;
+	struct balloon_cmd cmd;
+	int r;
 
 	parse_balloon_options(argc, argv);
 
@@ -53,23 +60,31 @@ int kvm_cmd_balloon(int argc, const char **argv, const char *prefix)
 		kvm_balloon_help();
 
 	if (instance_name == NULL &&
-	    instance_pid == 0)
+	    instance == 0)
 		kvm_balloon_help();
 
 	if (instance_name)
-		instance_pid = kvm__get_pid_by_instance(instance_name);
+		instance = kvm__get_sock_by_instance(instance_name);
 
-	if (instance_pid <= 0)
+	if (instance <= 0)
 		die("Failed locating instance");
 
+	cmd.type = KVM_IPC_BALLOON;
+	cmd.len = sizeof(cmd.amount);
+
 	if (inflate)
-		for (i = 0; i < inflate; i++)
-			kill(instance_pid, SIGKVMADDMEM);
+		cmd.amount = inflate;
 	else if (deflate)
-		for (i = 0; i < deflate; i++)
-			kill(instance_pid, SIGKVMDELMEM);
+		cmd.amount = -deflate;
 	else
 		kvm_balloon_help();
 
+	r = write(instance, &cmd, sizeof(cmd));
+
+	close(instance);
+
+	if (r < 0)
+		return -1;
+
 	return 0;
 }
tools/kvm/builtin-debug.c +21 -9
index 4be12cc..f744a7e 100644
@@ -3,17 +3,23 @@
 #include <kvm/builtin-debug.h>
 #include <kvm/kvm.h>
 #include <kvm/parse-options.h>
+#include <kvm/kvm-ipc.h>
 
 #include <stdio.h>
 #include <string.h>
 #include <signal.h>
 
 static bool all;
-static pid_t instance_pid;
+static int instance;
 static const char *instance_name;
 
+struct debug_cmd {
+	u32 type;
+	u32 len;
+};
+
 static const char * const debug_usage[] = {
-	"kvm debug [--all] [-n name] [-p pid]",
+	"kvm debug [--all] [-n name]",
 	NULL
 };
 
@@ -21,7 +27,6 @@ static const struct option debug_options[] = {
 	OPT_GROUP("General options:"),
 	OPT_BOOLEAN('a', "all", &all, "Debug all instances"),
 	OPT_STRING('n', "name", &instance_name, "name", "Instance name"),
-	OPT_INTEGER('p', "pid", &instance_pid, "Instance pid"),
 	OPT_END()
 };
 
@@ -40,9 +45,16 @@ void kvm_debug_help(void)
 	usage_with_options(debug_usage, debug_options);
 }
 
-static int do_debug(const char *name, int pid)
+static int do_debug(const char *name, int sock)
 {
-	return kill(pid, SIGQUIT);
+	struct debug_cmd cmd = {KVM_IPC_DEBUG, 0};
+	int r;
+
+	r = write(sock, &cmd, sizeof(cmd));
+	if (r < 0)
+		return r;
+
+	return 0;
 }
 
 int kvm_cmd_debug(int argc, const char **argv, const char *prefix)
@@ -53,14 +65,14 @@ int kvm_cmd_debug(int argc, const char **argv, const char *prefix)
 		return kvm__enumerate_instances(do_debug);
 
 	if (instance_name == NULL &&
-	    instance_pid == 0)
+	    instance == 0)
 		kvm_debug_help();
 
 	if (instance_name)
-		instance_pid = kvm__get_pid_by_instance(instance_name);
+		instance = kvm__get_sock_by_instance(instance_name);
 
-	if (instance_pid <= 0)
+	if (instance <= 0)
 		die("Failed locating instance");
 
-	return kill(instance_pid, SIGQUIT);
+	return do_debug(instance_name, instance);
 }
tools/kvm/builtin-list.c +26 -2
index 3e116d6..529e251 100644
@@ -3,6 +3,7 @@
 #include <kvm/builtin-list.h>
 #include <kvm/kvm.h>
 #include <kvm/parse-options.h>
+#include <kvm/kvm-ipc.h>
 
 #include <dirent.h>
 #include <stdio.h>
@@ -12,6 +13,11 @@
 
 #define PROCESS_NAME "kvm"
 
+struct pid_cmd {
+	u32 type;
+	u32 len;
+};
+
 static bool run;
 static bool rootfs;
 
@@ -32,11 +38,29 @@ void kvm_list_help(void)
 	usage_with_options(list_usage, list_options);
 }
 
-static int print_guest(const char *name, int pid)
+static pid_t get_pid(int sock)
+{
+	struct pid_cmd cmd = {KVM_IPC_PID, 0};
+	int r;
+	pid_t pid;
+
+	r = write(sock, &cmd, sizeof(cmd));
+	if (r < 0)
+		return r;
+
+	r = read(sock, &pid, sizeof(pid));
+	if (r < 0)
+		return r;
+
+	return pid;
+}
+
+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);
 
 	sprintf(proc_name, "/proc/%d/stat", pid);
 	fd = fopen(proc_name, "r");
@@ -61,7 +85,7 @@ cleanup:
 	if (comm)
 		free(comm);
 
-	kvm__remove_pidfile(name);
+	kvm__remove_socket(name);
 	return 0;
 }
 
tools/kvm/builtin-pause.c +21 -9
index 7b644ff..d16da28 100644
@@ -3,17 +3,23 @@
 #include <kvm/builtin-pause.h>
 #include <kvm/kvm.h>
 #include <kvm/parse-options.h>
+#include <kvm/kvm-ipc.h>
 
 #include <stdio.h>
 #include <string.h>
 #include <signal.h>
 
+struct pause_cmd {
+	u32 type;
+	u32 len;
+};
+
 static bool all;
-static pid_t instance_pid;
+static int instance;
 static const char *instance_name;
 
 static const char * const pause_usage[] = {
-	"kvm pause [--all] [-n name] [-p pid]",
+	"kvm pause [--all] [-n name]",
 	NULL
 };
 
@@ -21,7 +27,6 @@ static const struct option pause_options[] = {
 	OPT_GROUP("General options:"),
 	OPT_BOOLEAN('a', "all", &all, "Pause all instances"),
 	OPT_STRING('n', "name", &instance_name, "name", "Instance name"),
-	OPT_INTEGER('p', "pid", &instance_pid, "Instance pid"),
 	OPT_END()
 };
 
@@ -40,9 +45,16 @@ void kvm_pause_help(void)
 	usage_with_options(pause_usage, pause_options);
 }
 
-static int do_pause(const char *name, int pid)
+static int do_pause(const char *name, int sock)
 {
-	return kill(pid, SIGUSR2);
+	struct pause_cmd cmd = {KVM_IPC_PAUSE, 0};
+	int r;
+
+	r = write(sock, &cmd, sizeof(cmd));
+	if (r < 0)
+		return r;
+
+	return 0;
 }
 
 int kvm_cmd_pause(int argc, const char **argv, const char *prefix)
@@ -53,14 +65,14 @@ int kvm_cmd_pause(int argc, const char **argv, const char *prefix)
 		return kvm__enumerate_instances(do_pause);
 
 	if (instance_name == NULL &&
-	    instance_pid == 0)
+	    instance == 0)
 		kvm_pause_help();
 
 	if (instance_name)
-		instance_pid = kvm__get_pid_by_instance(instance_name);
+		instance = kvm__get_sock_by_instance(instance_name);
 
-	if (instance_pid <= 0)
+	if (instance <= 0)
 		die("Failed locating instance");
 
-	return kill(instance_pid, SIGUSR2);
+	return do_pause(instance_name, instance);
 }
tools/kvm/builtin-resume.c +21 -9
index 70de0fc..1e76c47 100644
@@ -3,17 +3,23 @@
 #include <kvm/builtin-resume.h>
 #include <kvm/kvm.h>
 #include <kvm/parse-options.h>
+#include <kvm/kvm-ipc.h>
 
 #include <stdio.h>
 #include <string.h>
 #include <signal.h>
 
+struct resume_cmd {
+	u32 type;
+	u32 len;
+};
+
 static bool all;
-static pid_t instance_pid;
+static int instance;
 static const char *instance_name;
 
 static const char * const resume_usage[] = {
-	"kvm resume [--all] [-n name] [-p pid]",
+	"kvm resume [--all] [-n name]",
 	NULL
 };
 
@@ -21,7 +27,6 @@ static const struct option resume_options[] = {
 	OPT_GROUP("General options:"),
 	OPT_BOOLEAN('a', "all", &all, "Resume all instances"),
 	OPT_STRING('n', "name", &instance_name, "name", "Instance name"),
-	OPT_INTEGER('p', "pid", &instance_pid, "Instance pid"),
 	OPT_END()
 };
 
@@ -40,9 +45,16 @@ void kvm_resume_help(void)
 	usage_with_options(resume_usage, resume_options);
 }
 
-static int do_resume(const char *name, int pid)
+static int do_resume(const char *name, int sock)
 {
-	return kill(pid, SIGKVMRESUME);
+	struct resume_cmd cmd = {KVM_IPC_RESUME, 0};
+	int r;
+
+	r = write(sock, &cmd, sizeof(cmd));
+	if (r < 0)
+		return r;
+
+	return 0;
 }
 
 int kvm_cmd_resume(int argc, const char **argv, const char *prefix)
@@ -53,14 +65,14 @@ int kvm_cmd_resume(int argc, const char **argv, const char *prefix)
 		return kvm__enumerate_instances(do_resume);
 
 	if (instance_name == NULL &&
-	    instance_pid == 0)
+	    instance == 0)
 		kvm_resume_help();
 
 	if (instance_name)
-		instance_pid = kvm__get_pid_by_instance(instance_name);
+		instance = kvm__get_sock_by_instance(instance_name);
 
-	if (instance_pid <= 0)
+	if (instance <= 0)
 		die("Failed locating instance");
 
-	return kill(instance_pid, SIGKVMRESUME);
+	return do_resume(instance_name, instance);
 }
tools/kvm/builtin-run.c +10 -12
index a88503f..bc4f07b 100644
@@ -30,6 +30,7 @@
 #include "kvm/vnc.h"
 #include "kvm/guest_compat.h"
 #include "kvm/pci-shmem.h"
+#include "kvm/kvm-ipc.h"
 
 #include <linux/types.h>
 
@@ -471,11 +472,11 @@ static void handle_sigusr1(int sig)
 /* Pause/resume the guest using SIGUSR2 */
 static int is_paused;
 
-static void handle_sigusr2(int sig)
+static void handle_pause(int fd, u32 type, u32 len, u8 *msg)
 {
-	if (sig == SIGKVMRESUME && is_paused)
+	if (type == KVM_IPC_RESUME && is_paused)
 		kvm__continue();
-	else if (sig == SIGUSR2 && !is_paused)
+	else if (type == KVM_IPC_PAUSE && !is_paused)
 		kvm__pause();
 	else
 		return;
@@ -484,7 +485,7 @@ static void handle_sigusr2(int sig)
 	pr_info("Guest %s\n", is_paused ? "paused" : "resumed");
 }
 
-static void handle_sigquit(int sig)
+static void handle_debug(int fd, u32 type, u32 len, u8 *msg)
 {
 	int i;
 
@@ -514,7 +515,7 @@ static void handle_sigalrm(int sig)
 	virtio_console__inject_interrupt(kvm);
 }
 
-static void handle_sigstop(int sig)
+static void handle_stop(int fd, u32 type, u32 len, u8 *msg)
 {
 	kvm_cpu__reboot();
 }
@@ -702,14 +703,11 @@ int kvm_cmd_run(int argc, const char **argv, const char *prefix)
 	void *ret;
 
 	signal(SIGALRM, handle_sigalrm);
-	signal(SIGQUIT, handle_sigquit);
+	kvm_ipc__register_handler(KVM_IPC_DEBUG, handle_debug);
 	signal(SIGUSR1, handle_sigusr1);
-	signal(SIGUSR2, handle_sigusr2);
-	signal(SIGKVMSTOP, handle_sigstop);
-	signal(SIGKVMRESUME, handle_sigusr2);
-	/* ignore balloon signal by default if not enable balloon optiion */
-	signal(SIGKVMADDMEM, SIG_IGN);
-	signal(SIGKVMDELMEM, SIG_IGN);
+	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);
 
 	nr_online_cpus = sysconf(_SC_NPROCESSORS_ONLN);
 
tools/kvm/builtin-stat.c +23 -13
index be12d15..609a6e5 100644
@@ -3,18 +3,24 @@
 #include <kvm/builtin-stat.h>
 #include <kvm/kvm.h>
 #include <kvm/parse-options.h>
+#include <kvm/kvm-ipc.h>
 
 #include <stdio.h>
 #include <string.h>
 #include <signal.h>
 
+struct stat_cmd {
+	u32 type;
+	u32 len;
+};
+
 static bool mem;
 static bool all;
-static pid_t instance_pid;
+static int instance;
 static const char *instance_name;
 
 static const char * const stat_usage[] = {
-	"kvm stat [command] [--all] [-n name] [-p pid]",
+	"kvm stat [command] [--all] [-n name]",
 	NULL
 };
 
@@ -24,7 +30,6 @@ static const struct option stat_options[] = {
 	OPT_GROUP("Instance options:"),
 	OPT_BOOLEAN('a', "all", &all, "All instances"),
 	OPT_STRING('n', "name", &instance_name, "name", "Instance name"),
-	OPT_INTEGER('p', "pid", &instance_pid, "Instance pid"),
 	OPT_END()
 };
 
@@ -43,10 +48,18 @@ void kvm_stat_help(void)
 	usage_with_options(stat_usage, stat_options);
 }
 
-static int do_memstat(const char *name, int pid)
+static int do_memstat(const char *name, int sock)
 {
+	struct stat_cmd cmd = {KVM_IPC_STAT, 0};
+	int r;
+
 	printf("Sending memstat command to %s, output should be on the targets' terminal.\n", name);
-	return kill(pid, SIGKVMMEMSTAT);
+
+	r = write(sock, &cmd, sizeof(cmd));
+	if (r < 0)
+		return r;
+
+	return 0;
 }
 
 int kvm_cmd_stat(int argc, const char **argv, const char *prefix)
@@ -60,20 +73,17 @@ int kvm_cmd_stat(int argc, const char **argv, const char *prefix)
 		return kvm__enumerate_instances(do_memstat);
 
 	if (instance_name == NULL &&
-	    instance_pid == 0)
+	    instance == 0)
 		kvm_stat_help();
 
 	if (instance_name)
-		instance_pid = kvm__get_pid_by_instance(instance_name);
+		instance = kvm__get_sock_by_instance(instance_name);
 
-	if (instance_pid <= 0)
+	if (instance <= 0)
 		die("Failed locating instance");
 
-	if (mem) {
-		printf("Sending memstat command to designated instance, output should be on the targets' terminal.\n");
-
-		return kill(instance_pid, SIGKVMMEMSTAT);
-	}
+	if (mem)
+		return do_memstat(instance_name, instance);
 
 	return 0;
 }
tools/kvm/builtin-stop.c +21 -9
index fd0500e..26a9214 100644
@@ -3,17 +3,23 @@
 #include <kvm/builtin-stop.h>
 #include <kvm/kvm.h>
 #include <kvm/parse-options.h>
+#include <kvm/kvm-ipc.h>
 
 #include <stdio.h>
 #include <string.h>
 #include <signal.h>
 
+struct stop_cmd {
+	u32 type;
+	u32 len;
+};
+
 static bool all;
-static pid_t instance_pid;
+static int instance;
 static const char *instance_name;
 
 static const char * const stop_usage[] = {
-	"kvm stop [--all] [-n name] [-p pid]",
+	"kvm stop [--all] [-n name]",
 	NULL
 };
 
@@ -21,7 +27,6 @@ static const struct option stop_options[] = {
 	OPT_GROUP("General options:"),
 	OPT_BOOLEAN('a', "all", &all, "Stop all instances"),
 	OPT_STRING('n', "name", &instance_name, "name", "Instance name"),
-	OPT_INTEGER('p', "pid", &instance_pid, "Instance pid"),
 	OPT_END()
 };
 
@@ -40,9 +45,16 @@ void kvm_stop_help(void)
 	usage_with_options(stop_usage, stop_options);
 }
 
-static int do_stop(const char *name, int pid)
+static int do_stop(const char *name, int sock)
 {
-	return kill(pid, SIGKVMSTOP);
+	struct stop_cmd cmd = {KVM_IPC_STOP, 0};
+	int r;
+
+	r = write(sock, &cmd, sizeof(cmd));
+	if (r < 0)
+		return r;
+
+	return 0;
 }
 
 int kvm_cmd_stop(int argc, const char **argv, const char *prefix)
@@ -53,14 +65,14 @@ int kvm_cmd_stop(int argc, const char **argv, const char *prefix)
 		return kvm__enumerate_instances(do_stop);
 
 	if (instance_name == NULL &&
-	    instance_pid == 0)
+	    instance == 0)
 		kvm_stop_help();
 
 	if (instance_name)
-		instance_pid = kvm__get_pid_by_instance(instance_name);
+		instance = kvm__get_sock_by_instance(instance_name);
 
-	if (instance_pid <= 0)
+	if (instance <= 0)
 		die("Failed locating instance");
 
-	return kill(instance_pid, SIGKVMSTOP);
+	return do_stop(instance_name, instance);
 }
tools/kvm/include/kvm/kvm-ipc.h new file +26 -0
new file mode 100644
index 0000000..c932052
@@ -0,0 +1,26 @@
+#ifndef KVM__IPC_H_
+#define KVM__IPC_H_
+
+#include <linux/types.h>
+
+struct kvm_ipc_msg {
+	u32 type;
+	u32 len;
+	u8 data[];
+};
+
+enum {
+	KVM_IPC_BALLOON	= 1,
+	KVM_IPC_DEBUG	= 2,
+	KVM_IPC_STAT	= 3,
+	KVM_IPC_PAUSE	= 4,
+	KVM_IPC_RESUME	= 5,
+	KVM_IPC_STOP	= 6,
+	KVM_IPC_PID	= 7,
+};
+
+int kvm_ipc__register_handler(u32 type, void (*cb)(int fd, u32 type, u32 len, u8 *msg));
+int kvm_ipc__handle(int fd, struct kvm_ipc_msg *msg);
+int kvm_ipc__start(int sock);
+
+#endif
tools/kvm/include/kvm/kvm.h +2 -5
index 79fa97d..a4c60d6 100644
@@ -19,11 +19,8 @@
 
 #define SIGKVMEXIT		(SIGRTMIN + 0)
 #define SIGKVMPAUSE		(SIGRTMIN + 1)
-#define SIGKVMADDMEM		(SIGRTMIN + 2)
-#define SIGKVMDELMEM		(SIGRTMIN + 3)
 #define SIGKVMSTOP		(SIGRTMIN + 4)
 #define SIGKVMRESUME		(SIGRTMIN + 5)
-#define SIGKVMMEMSTAT		(SIGRTMIN + 6)
 
 #define KVM_PID_FILE_PATH	"/.kvm-tools/"
 #define HOME_DIR		getenv("HOME")
@@ -77,9 +74,9 @@ bool kvm__deregister_mmio(struct kvm *kvm, u64 phys_addr);
 void kvm__pause(void);
 void kvm__continue(void);
 void kvm__notify_paused(void);
-pid_t kvm__get_pid_by_instance(const char *name);
+int kvm__get_sock_by_instance(const char *name);
 int kvm__enumerate_instances(int (*callback)(const char *name, int pid));
-void kvm__remove_pidfile(const char *name);
+void kvm__remove_socket(const char *name);
 
 /*
  * Debugging
tools/kvm/kvm-ipc.c new file +147 -0

147 lines not on this page · raw message

tools/kvm/kvm.c +69 -37

170 lines not on this page · raw message

tools/kvm/virtio/balloon.c +13 -17
index 1691b79..991610f 100644
@@ -9,6 +9,7 @@
 #include "kvm/threadpool.h"
 #include "kvm/guest_compat.h"
 #include "kvm/virtio-pci.h"
+#include "kvm/kvm-ipc.h"
 
 #include <linux/virtio_ring.h>
 #include <linux/virtio_balloon.h>
@@ -139,12 +140,12 @@ static int virtio_bln__collect_stats(void)
 	return 0;
 }
 
-static int virtio_bln__print_stats(void)
+static void virtio_bln__print_stats(int fd, u32 type, u32 len, u8 *msg)
 {
 	u16 i;
 
 	if (virtio_bln__collect_stats() < 0)
-		return -EFAULT;
+		return;
 
 	printf("\n\n\t*** Guest memory statistics ***\n\n");
 	for (i = 0; i < bdev.stat_count; i++) {
@@ -171,23 +172,19 @@ static int virtio_bln__print_stats(void)
 		printf("%llu\n", bdev.stats[i].val);
 	}
 	printf("\n");
-
-	return 0;
 }
 
-static void handle_sigmem(int sig)
+static void handle_mem(int fd, u32 type, u32 len, u8 *msg)
 {
-	if (sig == SIGKVMADDMEM) {
-		bdev.config.num_pages += 256;
-	} else if (sig == SIGKVMDELMEM) {
-		if (bdev.config.num_pages < 256)
-			return;
+	int mem = *(int *)msg;
 
-		bdev.config.num_pages -= 256;
-	} else if (sig == SIGKVMMEMSTAT) {
-		virtio_bln__print_stats();
+	if (mem > 0) {
+		bdev.config.num_pages += 256 * mem;
+	} else if (mem < 0) {
+		if (bdev.config.num_pages < (u32)(256 * (-mem)))
+			return;
 
-		return;
+		bdev.config.num_pages += 256 * mem;
 	}
 
 	/* Notify that the configuration space has changed */
@@ -261,9 +258,8 @@ static int get_size_vq(struct kvm *kvm, void *dev, u32 vq)
 
 void virtio_bln__init(struct kvm *kvm)
 {
-	signal(SIGKVMADDMEM, handle_sigmem);
-	signal(SIGKVMDELMEM, handle_sigmem);
-	signal(SIGKVMMEMSTAT, handle_sigmem);
+	kvm_ipc__register_handler(KVM_IPC_BALLOON, handle_mem);
+	kvm_ipc__register_handler(KVM_IPC_STAT, virtio_bln__print_stats);
 
 	bdev.stat_waitfd	= eventfd(0, 0);
 	memset(&bdev.config, 0, sizeof(struct virtio_balloon_config));
Signature · 1 line
1.7.7
#1 lore raw

PATCH v2 2/3 kvm tools: Move memory stat print to client

So far we were printing memory statistics in the terminal of the guest, this was annoying, confusing and inneficient.

This patch moves the print to the terminal which ran 'kvm stat'

---
 tools/kvm/builtin-stat.c   |   46 ++++++++++++++++++++++++++++++++++++++++++-
 tools/kvm/virtio/balloon.c |   30 +++------------------------
 2 files changed, 48 insertions(+), 28 deletions(-)
tools/kvm/builtin-stat.c +44 -2
index 609a6e5..e28eb5b 100644
@@ -5,10 +5,13 @@
 #include <kvm/parse-options.h>
 #include <kvm/kvm-ipc.h>
 
+#include <sys/select.h>
 #include <stdio.h>
 #include <string.h>
 #include <signal.h>
 
+#include <linux/virtio_balloon.h>
+
 struct stat_cmd {
 	u32 type;
 	u32 len;
@@ -51,14 +54,53 @@ void kvm_stat_help(void)
 static int do_memstat(const char *name, int sock)
 {
 	struct stat_cmd cmd = {KVM_IPC_STAT, 0};
+	struct virtio_balloon_stat stats[VIRTIO_BALLOON_S_NR];
+	fd_set fdset;
+	struct timeval t = { .tv_sec = 1 };
 	int r;
+	u8 i;
 
-	printf("Sending memstat command to %s, output should be on the targets' terminal.\n", name);
-
+	FD_ZERO(&fdset);
+	FD_SET(sock, &fdset);
 	r = write(sock, &cmd, sizeof(cmd));
 	if (r < 0)
 		return r;
 
+	r = select(1, &fdset, NULL, NULL, &t);
+	if (r < 0) {
+		pr_error("Could not retrieve mem stats from %s", name);
+		return r;
+	}
+	r = read(sock, &stats, sizeof(stats));
+	if (r < 0)
+		return r;
+
+	printf("\n\n\t*** Guest memory statistics ***\n\n");
+	for (i = 0; i < VIRTIO_BALLOON_S_NR; i++) {
+		switch (stats[i].tag) {
+		case VIRTIO_BALLOON_S_SWAP_IN:
+			printf("The amount of memory that has been swapped in (in bytes):");
+			break;
+		case VIRTIO_BALLOON_S_SWAP_OUT:
+			printf("The amount of memory that has been swapped out to disk (in bytes):");
+			break;
+		case VIRTIO_BALLOON_S_MAJFLT:
+			printf("The number of major page faults that have occurred:");
+			break;
+		case VIRTIO_BALLOON_S_MINFLT:
+			printf("The number of minor page faults that have occurred:");
+			break;
+		case VIRTIO_BALLOON_S_MEMFREE:
+			printf("The amount of memory not being used for any purpose (in bytes):");
+			break;
+		case VIRTIO_BALLOON_S_MEMTOT:
+			printf("The total amount of memory available (in bytes):");
+			break;
+		}
+		printf("%llu\n", stats[i].val);
+	}
+	printf("\n");
+
 	return 0;
 }
 
tools/kvm/virtio/balloon.c +4 -26
index 991610f..0228686 100644
@@ -142,36 +142,14 @@ static int virtio_bln__collect_stats(void)
 
 static void virtio_bln__print_stats(int fd, u32 type, u32 len, u8 *msg)
 {
-	u16 i;
+	int r;
 
 	if (virtio_bln__collect_stats() < 0)
 		return;
 
-	printf("\n\n\t*** Guest memory statistics ***\n\n");
-	for (i = 0; i < bdev.stat_count; i++) {
-		switch (bdev.stats[i].tag) {
-		case VIRTIO_BALLOON_S_SWAP_IN:
-			printf("The amount of memory that has been swapped in (in bytes):");
-			break;
-		case VIRTIO_BALLOON_S_SWAP_OUT:
-			printf("The amount of memory that has been swapped out to disk (in bytes):");
-			break;
-		case VIRTIO_BALLOON_S_MAJFLT:
-			printf("The number of major page faults that have occurred:");
-			break;
-		case VIRTIO_BALLOON_S_MINFLT:
-			printf("The number of minor page faults that have occurred:");
-			break;
-		case VIRTIO_BALLOON_S_MEMFREE:
-			printf("The amount of memory not being used for any purpose (in bytes):");
-			break;
-		case VIRTIO_BALLOON_S_MEMTOT:
-			printf("The total amount of memory available (in bytes):");
-			break;
-		}
-		printf("%llu\n", bdev.stats[i].val);
-	}
-	printf("\n");
+	r = write(fd, bdev.stats, sizeof(bdev.stats));
+	if (r < 0)
+		pr_warning("Failed sending memory stats");
 }
 
 static void handle_mem(int fd, u32 type, u32 len, u8 *msg)
Signature · 1 line
1.7.7
#2 lore raw

PATCH v2 3/3 kvm tools: Allow piping debug output to file descriptor

In reply to Sasha Levin

This patch makes debug output go to a 'debug_fd' instead of stdout.

Doing so allows us to send the output to a different console when required.

This patch also changes the behaviour of 'kvm debug' to show the debug output in the console that executed the debug command instead of in the console of the guest.

---
 tools/kvm/builtin-debug.c       |    9 ++++
 tools/kvm/builtin-run.c         |    8 +++-
 tools/kvm/include/kvm/kvm-cpu.h |    2 +
 tools/kvm/kvm-cpu.c             |   78 ++++++++++++++++++++++----------------
 4 files changed, 63 insertions(+), 34 deletions(-)
tools/kvm/builtin-debug.c +9 -0
index f744a7e..63804fd 100644
@@ -47,6 +47,7 @@ void kvm_debug_help(void)
 
 static int do_debug(const char *name, int sock)
 {
+	char buff[100];
 	struct debug_cmd cmd = {KVM_IPC_DEBUG, 0};
 	int r;
 
@@ -54,6 +55,14 @@ static int do_debug(const char *name, int sock)
 	if (r < 0)
 		return r;
 
+	do {
+		r = read(sock, buff, 100);
+		if (r < 0)
+			return 0;
+		printf("%s", buff);
+		memset(buff, 0, 100);
+	} while (r > 0);
+
 	return 0;
 }
 
tools/kvm/builtin-run.c +7 -1
index bc4f07b..bfade41 100644
@@ -456,11 +456,12 @@ static int printout_done;
 static void handle_sigusr1(int sig)
 {
 	struct kvm_cpu *cpu = current_kvm_cpu;
+	int fd = kvm_cpu__get_debug_fd();
 
 	if (!cpu)
 		return;
 
-	printf("\n #\n # vCPU #%ld's dump:\n #\n", cpu->cpu_id);
+	dprintf(fd, "\n #\n # vCPU #%ld's dump:\n #\n", cpu->cpu_id);
 	kvm_cpu__show_registers(cpu);
 	kvm_cpu__show_code(cpu);
 	kvm_cpu__show_page_tables(cpu);
@@ -496,6 +497,8 @@ static void handle_debug(int fd, u32 type, u32 len, u8 *msg)
 			continue;
 
 		printout_done = 0;
+
+		kvm_cpu__set_debug_fd(fd);
 		pthread_kill(cpu->thread, SIGUSR1);
 		/*
 		 * Wait for the vCPU to dump state before signalling
@@ -506,6 +509,8 @@ static void handle_debug(int fd, u32 type, u32 len, u8 *msg)
 			mb();
 	}
 
+	close(fd);
+
 	serial8250__inject_sysrq(kvm);
 }
 
@@ -539,6 +544,7 @@ panic_kvm:
 		fprintf(stderr, "KVM exit code: 0x%Lu\n",
 			current_kvm_cpu->kvm_run->hw.hardware_exit_reason);
 
+	kvm_cpu__set_debug_fd(STDOUT_FILENO);
 	kvm_cpu__show_registers(current_kvm_cpu);
 	kvm_cpu__show_code(current_kvm_cpu);
 	kvm_cpu__show_page_tables(current_kvm_cpu);
tools/kvm/include/kvm/kvm-cpu.h +2 -0
index 95f3f9d..01540ac 100644
@@ -37,6 +37,8 @@ void kvm_cpu__run(struct kvm_cpu *vcpu);
 void kvm_cpu__reboot(void);
 int kvm_cpu__start(struct kvm_cpu *cpu);
 
+int kvm_cpu__get_debug_fd(void);
+void kvm_cpu__set_debug_fd(int fd);
 void kvm_cpu__show_code(struct kvm_cpu *vcpu);
 void kvm_cpu__show_registers(struct kvm_cpu *vcpu);
 void kvm_cpu__show_page_tables(struct kvm_cpu *vcpu);
tools/kvm/kvm-cpu.c +45 -33

146 lines not on this page · raw message

Signature · 1 line
1.7.7