Skip to contentSkip to conversation

[4834] Merge NBD client/server, by Laurent Vivier.

Qemu-devel Thiemo Seufer 1 participant 1 message Jul 3, 2008

lore
Share & tools
lore
#0 lore raw

Revision: 4834

          http://svn.sv.gnu.org/viewvc/?view=rev&root=qemu&revision=4834
Author:   ths
Date:     2008-07-03 10:23:51 +0000 (Thu, 03 Jul 2008)

Log Message:
-----------
Merge NBD client/server, by Laurent Vivier.

Modified Paths:
--------------
trunk/nbd.c
trunk/nbd.h
trunk/qemu-nbd.c
trunk/qemu-nbd.texi

Modified: trunk/nbd.c
===================================================================

nbd.c +68 -6
@@ -25,6 +25,7 @@
 #include <ctype.h>
 #include <inttypes.h>
 #include <sys/socket.h>
+#include <sys/un.h>
 #include <netinet/in.h>
 #include <netinet/tcp.h>
 #include <arpa/inet.h>
@@ -183,6 +184,65 @@
     return -1;
 }
 
+int unix_socket_incoming(const char *path)
+{
+    int s;
+    struct sockaddr_un addr;
+    int serrno;
+
+    s = socket(PF_UNIX, SOCK_STREAM, 0);
+    if (s == -1) {
+        return -1;
+    }
+
+    memset(&addr, 0, sizeof(addr));
+    addr.sun_family = AF_UNIX;
+    pstrcpy(addr.sun_path, sizeof(addr.sun_path), path);
+
+    if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
+        goto error;
+    }
+
+    if (listen(s, 128) == -1) {
+        goto error;
+    }
+
+    return s;
+error:
+    serrno = errno;
+    close(s);
+    errno = serrno;
+    return -1;
+}
+
+int unix_socket_outgoing(const char *path)
+{
+    int s;
+    struct sockaddr_un addr;
+    int serrno;
+
+    s = socket(PF_UNIX, SOCK_STREAM, 0);
+    if (s == -1) {
+        return -1;
+    }
+
+    memset(&addr, 0, sizeof(addr));
+    addr.sun_family = AF_UNIX;
+    pstrcpy(addr.sun_path, sizeof(addr.sun_path), path);
+
+    if (connect(s, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
+        goto error;
+    }
+
+    return s;
+error:
+    serrno = errno;
+    close(s);
+    errno = serrno;
+    return -1;
+}
+
+
 /* Basic flow
 
    Server         Client
@@ -225,12 +285,10 @@
 	return 0;
 }
 
-int nbd_receive_negotiate(int fd, int csock)
+int nbd_receive_negotiate(int csock, off_t *size, size_t *blocksize)
 {
 	char buf[8 + 8 + 8 + 128];
 	uint64_t magic;
-	off_t size;
-	size_t blocksize;
 
 	TRACE("Receiving negotation.");
 
@@ -241,8 +299,8 @@
 	}
 
 	magic = be64_to_cpup((uint64_t*)(buf + 8));
-	size = be64_to_cpup((uint64_t*)(buf + 16));
-	blocksize = 1024;
+	*size = be64_to_cpup((uint64_t*)(buf + 16));
+	*blocksize = 1024;
 
 	TRACE("Magic is %c%c%c%c%c%c%c%c",
 	      isprint(buf[0]) ? buf[0] : '.',
@@ -254,7 +312,7 @@
 	      isprint(buf[6]) ? buf[6] : '.',
 	      isprint(buf[7]) ? buf[7] : '.');
 	TRACE("Magic is 0x%" PRIx64, magic);
-	TRACE("Size is %" PRIu64, size);
+	TRACE("Size is %" PRIu64, *size);
 
 	if (memcmp(buf, "NBDMAGIC", 8) != 0) {
 		LOG("Invalid magic received");
@@ -269,7 +327,11 @@
 		errno = EINVAL;
 		return -1;
 	}
+        return 0;
+}
 
+int nbd_init(int fd, int csock, off_t size, size_t blocksize)
+{
 	TRACE("Setting block size to %lu", (unsigned long)blocksize);
 
 	if (ioctl(fd, NBD_SET_BLKSIZE, blocksize) == -1) {

Modified: trunk/nbd.h
===================================================================

nbd.h +4 -1
@@ -27,9 +27,12 @@
 #include "block_int.h"
 
 int tcp_socket_incoming(const char *address, uint16_t port);
+int unix_socket_outgoing(const char *path);
+int unix_socket_incoming(const char *path);
 
 int nbd_negotiate(BlockDriverState *bs, int csock, off_t size);
-int nbd_receive_negotiate(int fd, int csock);
+int nbd_receive_negotiate(int csock, off_t *size, size_t *blocksize);
+int nbd_init(int fd, int csock, off_t size, size_t blocksize);
 int nbd_trip(BlockDriverState *bs, int csock, off_t size, uint64_t dev_offset, off_t *offset, bool readonly);
 int nbd_client(int fd, int csock);
 int nbd_disconnect(int fd);

Modified: trunk/qemu-nbd.c
===================================================================

qemu-nbd.c +135 -4

222 lines not on this page · raw message

Modified: trunk/qemu-nbd.texi
===================================================================

qemu-nbd.texi +6 -0
@@ -20,10 +20,16 @@
   offset into the image
 @item -b, --bind=IFACE
   interface to bind to (default `0.0.0.0')
+@item -k, --socket=PATH
+  Use a unix socket with path PATH
 @item -r, --read-only
   export read-only
 @item -P, --partition=NUM
   only expose partition NUM
+@item -c, --connect
+  connect FILE to NBD device DEV
+@item -d, --disconnect
+  disconnect the specified device
 @item -v, --verbose
   display extra debugging information
 @item -h, --help