aboutsummaryrefslogtreecommitdiffstats
path: root/tools/libvchan/node.c
diff options
context:
space:
mode:
authorDaniel De Graaf <dgdegra@tycho.nsa.gov>2011-10-06 19:44:40 +0100
committerDaniel De Graaf <dgdegra@tycho.nsa.gov>2011-10-06 19:44:40 +0100
commit1a16a3351ff2f2cf9f0cc0a27c89a0652eb8dfb4 (patch)
tree0bdd977ac463467766ee133f140085e0f39b0c93 /tools/libvchan/node.c
parent399a74471de9f7525e84441c59aa9601133ba828 (diff)
downloadxen-1a16a3351ff2f2cf9f0cc0a27c89a0652eb8dfb4.tar.gz
xen-1a16a3351ff2f2cf9f0cc0a27c89a0652eb8dfb4.tar.bz2
xen-1a16a3351ff2f2cf9f0cc0a27c89a0652eb8dfb4.zip
libvchan: interdomain communications library
This library implements a bidirectional communication interface between applications in different domains, similar to unix sockets. Data can be sent using the byte-oriented libvchan_read/libvchan_write or the packet-oriented libvchan_recv/libvchan_send. Channel setup is done using a client-server model; domain IDs and a port number must be negotiated prior to initialization. The server allocates memory for the shared pages and determines the sizes of the communication rings (which may span multiple pages, although the default places rings and control within a single page). With properly sized rings, testing has shown that this interface provides speed comparable to pipes within a single Linux domain; it is significantly faster than network-based communication. Signed-off-by: Daniel De Graaf <dgdegra@tycho.nsa.gov> Acked-by: Ian Campbell <ian.campbell@citrix.com> Committed-by: Ian Jackson <ian.jackson@eu.citrix.com>
Diffstat (limited to 'tools/libvchan/node.c')
-rw-r--r--tools/libvchan/node.c169
1 files changed, 169 insertions, 0 deletions
diff --git a/tools/libvchan/node.c b/tools/libvchan/node.c
new file mode 100644
index 0000000000..cab8368c74
--- /dev/null
+++ b/tools/libvchan/node.c
@@ -0,0 +1,169 @@
+/**
+ * @file
+ * @section AUTHORS
+ *
+ * Copyright (C) 2010 Rafal Wojtczuk <rafal@invisiblethingslab.com>
+ *
+ * Authors:
+ * Rafal Wojtczuk <rafal@invisiblethingslab.com>
+ * Daniel De Graaf <dgdegra@tycho.nsa.gov>
+ *
+ * @section LICENSE
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ * @section DESCRIPTION
+ *
+ * This is a test program for libxenvchan. Communications are in one direction,
+ * either server (grant offeror) to client or vice versa.
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <time.h>
+
+#include <libxenvchan.h>
+
+int libxenvchan_write_all(struct libxenvchan *ctrl, char *buf, int size)
+{
+ int written = 0;
+ int ret;
+ while (written < size) {
+ ret = libxenvchan_write(ctrl, buf + written, size - written);
+ if (ret <= 0) {
+ perror("write");
+ exit(1);
+ }
+ written += ret;
+ }
+ return size;
+}
+
+int write_all(int fd, char *buf, int size)
+{
+ int written = 0;
+ int ret;
+ while (written < size) {
+ ret = write(fd, buf + written, size - written);
+ if (ret <= 0) {
+ perror("write");
+ exit(1);
+ }
+ written += ret;
+ }
+ return size;
+}
+
+void usage(char** argv)
+{
+ fprintf(stderr, "usage:\n"
+ "%s [client|server] [read|write] domid nodepath\n", argv[0]);
+ exit(1);
+}
+
+#define BUFSIZE 5000
+char buf[BUFSIZE];
+void reader(struct libxenvchan *ctrl)
+{
+ int size;
+ for (;;) {
+ size = rand() % (BUFSIZE - 1) + 1;
+ size = libxenvchan_read(ctrl, buf, size);
+ fprintf(stderr, "#");
+ if (size < 0) {
+ perror("read vchan");
+ libxenvchan_close(ctrl);
+ exit(1);
+ }
+ size = write_all(1, buf, size);
+ if (size < 0) {
+ perror("stdout write");
+ exit(1);
+ }
+ if (size == 0) {
+ perror("write size=0?\n");
+ exit(1);
+ }
+ }
+}
+
+void writer(struct libxenvchan *ctrl)
+{
+ int size;
+ for (;;) {
+ size = rand() % (BUFSIZE - 1) + 1;
+ size = read(0, buf, size);
+ if (size < 0) {
+ perror("read stdin");
+ libxenvchan_close(ctrl);
+ exit(1);
+ }
+ if (size == 0)
+ break;
+ size = libxenvchan_write_all(ctrl, buf, size);
+ fprintf(stderr, "#");
+ if (size < 0) {
+ perror("vchan write");
+ exit(1);
+ }
+ if (size == 0) {
+ perror("write size=0?\n");
+ exit(1);
+ }
+ }
+}
+
+
+/**
+ Simple libxenvchan application, both client and server.
+ One side does writing, the other side does reading; both from
+ standard input/output fds.
+*/
+int main(int argc, char **argv)
+{
+ int seed = time(0);
+ struct libxenvchan *ctrl = 0;
+ int wr = 0;
+ if (argc < 4)
+ usage(argv);
+ if (!strcmp(argv[2], "read"))
+ wr = 0;
+ else if (!strcmp(argv[2], "write"))
+ wr = 1;
+ else
+ usage(argv);
+ if (!strcmp(argv[1], "server"))
+ ctrl = libxenvchan_server_init(NULL, atoi(argv[3]), argv[4], 0, 0);
+ else if (!strcmp(argv[1], "client"))
+ ctrl = libxenvchan_client_init(NULL, atoi(argv[3]), argv[4]);
+ else
+ usage(argv);
+ if (!ctrl) {
+ perror("libxenvchan_*_init");
+ exit(1);
+ }
+ ctrl->blocking = 1;
+
+ srand(seed);
+ fprintf(stderr, "seed=%d\n", seed);
+ if (wr)
+ writer(ctrl);
+ else
+ reader(ctrl);
+ libxenvchan_close(ctrl);
+ return 0;
+}