Browse Source

Working 9p virtqueue proxy.

Use with commands like

srv -e '9pvpxy /dev/virtcon/virtio-console-2' vc2
mount -c -n /srv/vc2 /mnt/xxx

Signed-off-by: golubovsky <golubovsky@gmail.com>
golubovsky 7 years ago
parent
commit
ace180bba0
3 changed files with 81 additions and 0 deletions
  1. 3 0
      sys/src/9/port/devvcon.c
  2. 77 0
      sys/src/cmd/9pvpxy.c
  3. 1 0
      sys/src/cmd/build.json

+ 3 - 0
sys/src/9/port/devvcon.c

@@ -53,6 +53,9 @@ rwcommon(Vqctl *d, void *va, int32_t n, int qidx)
 {
 	uint16_t descr[1];
 	Virtq *vq = d->vqs[qidx];
+	if(vq == nil) {
+		error("virtcon: no virtqueue");
+	}
 	int nd = getdescr(vq, 1, descr);
 	if(nd < 1)
 	{

+ 77 - 0
sys/src/cmd/9pvpxy.c

@@ -0,0 +1,77 @@
+/*
+ * This file is part of the Harvey operating system.  It is subject to the
+ * license terms of the GNU GPL v2 in LICENSE.gpl found in the top-level
+ * directory of this distribution and at http://www.gnu.org/licenses/gpl-2.0.txt
+ *
+ * No part of Harvey operating system, including this file, may be copied,
+ * modified, propagated, or distributed except according to the terms
+ * contained in the LICENSE.gpl file.
+ */
+
+// 9pvpxy.c A 9p proxy over virtqueue. Read 9p messages from standard input, send them to the virtqueue.
+// Read responses from the virtqueue, send them to the standard output.
+// The operation is single-threaded: write to virtqueue is expected to complete before the response can be read.
+
+#include <u.h>
+#include <libc.h>
+#include <fcall.h>
+
+#define BUFLEN 256*1024
+
+static uint8_t ibuf[BUFLEN], obuf[BUFLEN];
+
+void
+exerr(char *s)
+{
+	char err[200];
+	err[0] = 0;
+	errstr(err, 199);
+	fprint(2, "%s: %s\n", s, err);
+	exits(err);
+}
+
+void
+exerr2(char *s, char *err)
+{
+	fprint(2, "%s: %s\n", s, err);
+	exits(err);
+}
+
+void
+main(int argc, char *argv[])
+{
+	if(argc == 0) {
+		fprint(2, "usage: %s file\n", argv[0]);
+		exits("usage");
+	}
+	int fd = open(argv[1], ORDWR);
+	if(fd < 0)
+		exerr(argv[0]);
+	while(1) {
+		int rc = read9pmsg(0, obuf, BUFLEN);
+		if(rc < 0)
+		{
+			close(fd);
+			exerr(argv[0]);
+		}
+		int rc2 = write(fd, obuf, rc);
+		if(rc2 < rc)
+		{
+			close(fd);
+			exerr2(argv[0], "short write to vq");
+		}
+		rc = read(fd, ibuf, BUFLEN);
+		if(rc < BUFLEN)
+		{
+			close(fd);
+			exerr(argv[0]);
+		}
+		uint32_t ml = GBIT32(ibuf);
+		rc = write(1, ibuf, ml);
+		if(rc < 0)
+		{
+			close(fd);
+			exerr2(argv[0], "short write to stdout");
+		}
+	}
+}

+ 1 - 0
sys/src/cmd/build.json

@@ -7,6 +7,7 @@
 		],
 		"Install": "/$ARCH/bin/",
 		"SourceFilesCmd": [
+			"9pvpxy.c",
 			"aan.c",
 			"archfs.c",
 			"ascii.c",