Skip to contentSkip to conversation

Merge NBD client/server int qemu-nbd

PATCH Superseded Laurent Vivier 3 participants 18 messages Jun 13, 2008 – Jun 19, 2008

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Merge NBD client/server int qemu-nbd

Hi,

this patch allows to connect directly a disk image file to an NBD device. It introduces the use of a unix socket (instead of inet).

- To connect a file to a device:

# qemu-nbd --connect=/dev/nbd0 my_disk.qcow2

Then you can see directly your disk (no need of nbd-client):

# fdisk -l /dev/nbd0

Disk /dev/nbd0: 4294 MB, 4294967296 bytes
255 heads, 63 sectors/track, 522 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

     Device Boot      Start         End      Blocks   Id  System
/dev/nbd0p1   *           1         492     3951958+  83  Linux
/dev/nbd0p2             493         522      240975    5  Extended
/dev/nbd0p5             493         522      240943+  82  Linux swap /

Solaris

- To disconnect the file from the device:

# qemu-nbd --disconnect /dev/nbd0

Regards,
Laurent

Signature · 3 lines
------------- [email protected] ---------------
"The best way to predict the future is to invent it."
- Alan Kay
nbd.c +61 -1
Index: qemu/nbd.c
===================================================================
@@ -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 @@ error:
     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
@@ -388,7 +448,7 @@ int nbd_trip(BlockDriverState *bs, int c
 	}
 
 	if (len > sizeof(data)) {
-		LOG("len (%u) is larger than max len (%u)",
+		LOG("len (%u) is larger than max len (%lu)",
 		    len, sizeof(data));
 		errno = EINVAL;
 		return -1;
nbd.h +2 -0
Index: qemu/nbd.h
===================================================================
@@ -27,6 +27,8 @@
 #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);
qemu-nbd.c +110 -3

189 lines not on this page · raw message

#1 lore raw
Quoted 31 lines · Laurent Vivier

Hi,

this patch allows to connect directly a disk image file to an NBD device. It introduces the use of a unix socket (instead of inet).

- To connect a file to a device:

# qemu-nbd --connect=/dev/nbd0 my_disk.qcow2

Then you can see directly your disk (no need of nbd-client):

# fdisk -l /dev/nbd0

Disk /dev/nbd0: 4294 MB, 4294967296 bytes
255 heads, 63 sectors/track, 522 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

     Device Boot      Start         End      Blocks   Id  System
/dev/nbd0p1   *           1         492     3951958+  83  Linux
/dev/nbd0p2             493         522      240975    5  Extended
/dev/nbd0p5             493         522      240943+  82  Linux swap /

Solaris

- To disconnect the file from the device:

# qemu-nbd --disconnect /dev/nbd0

Regards,
Laurent

qemu-nbd.c static int find_partition(BlockDriverSta · line 159

Quoted 7 lines
qemu-nbd.c +7 -0
+static void show_parts(const char *device)
+{
+    if (fork() == 0) {
+        int nbd;
+
+	/* wait device */
+        sleep(1);

This looks like a big red-herring. What is this sleep waiting for any can it be possibly made into something less racy?

Regards,

Anthony Liguori

#2 lore raw
Quoted 15 lines · Anthony Liguori

qemu-nbd.c static int find_partition(BlockDriverSta · line 159

Quoted 13 lines
# qemu-nbd --disconnect /dev/nbd0

Regards,
Laurent
+static void show_parts(const char *device)

qemu-nbd.c +6 -0
+{
+    if (fork() == 0) {
+        int nbd;
+
+    /* wait device */
+        sleep(1);

This looks like a big red-herring. What is this sleep waiting for any can it be possibly made into something less racy?

It's called udevsettle, if the host is running udev.

Signature · 2 lines
I have a truly marvellous patch that fixes the bug which this
signature is too narrow to contain.
#3 lore raw
In reply to Anthony Liguori
Quoted 1 line · Anthony Liguori

Laurent Vivier wrote:

[...]

Quoted 11 lines

qemu-nbd.c static int find_partition(BlockDriverSta · line 159

Quoted 8 lines
qemu-nbd.c +7 -0
+static void show_parts(const char *device)
+{
+    if (fork() == 0) {
+        int nbd;
+
+	/* wait device */
+        sleep(1);

This looks like a big red-herring. What is this sleep waiting for any can it be possibly made into something less racy?

Yes, I know, it's BAD (and it can failed sometime...)

But show_parts() must wait its parent has entered in nbd_trip() loop.

Because the open() calls /dev/nbd0 which calls through the socket its parent and IMHO there is no easy way to know if the server is ready to process the request.

Perhaps something like:

int timeout = 5;
while ((fd = open(device, O_RDWR)) == -1 && timeout--)

    sleep(1);

Any suggestion ???

Regards,
Laurent

Signature · 3 lines
------------- [email protected] ---------------
"The best way to predict the future is to invent it."
- Alan Kay
#4 lore raw
Quoted 18 lines · Laurent Vivier
Quoted 2 lines · Anthony Liguori

Laurent Vivier wrote:

[...]

Quoted 11 lines

qemu-nbd.c static int find_partition(BlockDriverSta · line 159

Quoted 9 lines
qemu-nbd.c +7 -0
+static void show_parts(const char *device)
+{
+    if (fork() == 0) {
+        int nbd;
+
+	/* wait device */
+        sleep(1);

This looks like a big red-herring. What is this sleep waiting for any can it be possibly made into something less racy?

Quoted 4 lines

Yes, I know, it's BAD (and it can failed sometime...)

But show_parts() must wait its parent has entered in nbd_trip() loop.

You mean, listen() has to be called on the fd in the server? You have two choices I think. You could wait to fork the child until after you've listen()'d which is probably the most elegant solution. If that's difficult to do, you could have the child inherit one end of a pipe() and use that in the child to wait for the server to be ready.

Regards,

Anthony Liguori

Quoted 15 lines

Because the open() calls /dev/nbd0 which calls through the socket its parent and IMHO there is no easy way to know if the server is ready to process the request.

Perhaps something like:

int timeout = 5;
while ((fd = open(device, O_RDWR)) == -1 && timeout--)

    sleep(1);

Any suggestion ???

Regards,
Laurent

#6 lore raw
In reply to Laurent Vivier
Quoted 7 lines · Laurent Vivier

int timeout = 5;
while ((fd = open(device, O_RDWR)) == -1 && timeout--)

    sleep(1);

Any suggestion ???

/sbin/udevsettle

Signature · 2 lines
I have a truly marvellous patch that fixes the bug which this
signature is too narrow to contain.
#5 lore raw
In reply to Anthony Liguori
Quoted 20 lines · Anthony Liguori
Quoted 19 lines · Laurent Vivier
Quoted 2 lines · Anthony Liguori

Laurent Vivier wrote:

[...]

Quoted 12 lines

qemu-nbd.c static int find_partition(BlockDriverSta · line 159

Quoted 9 lines
qemu-nbd.c +7 -0
+static void show_parts(const char *device)
+{
+    if (fork() == 0) {
+        int nbd;
+
+	/* wait device */
+        sleep(1);

This looks like a big red-herring. What is this sleep waiting for any can it be possibly made into something less racy?

Quoted 8 lines
Quoted 4 lines

Yes, I know, it's BAD (and it can failed sometime...)

But show_parts() must wait its parent has entered in nbd_trip() loop.

You mean, listen() has to be called on the fd in the server? You have two choices I think. You could wait to fork the child until after you've listen()'d which is probably the most elegant solution. If

It is already after the listen (even after the accept...). It is why I call show_parts() from the server and not from client.

Quoted 2 lines

that's difficult to do, you could have the child inherit one end of a pipe() and use that in the child to wait for the server to be ready.

perhaps I'm wrong, but it seems the server must be waiting on the read() to allow to have a success on the open(), so I don't see how the server can launch show_parts() whereas it is already waiting on the read().

Regards,
Laurent

Signature · 3 lines
------------- [email protected] ---------------
"The best way to predict the future is to invent it."
- Alan Kay
#7 lore raw
In reply to Avi Kivity
Quoted 10 lines · Avi Kivity
Quoted 7 lines · Laurent Vivier

int timeout = 5;
while ((fd = open(device, O_RDWR)) == -1 && timeout--)

    sleep(1);

Any suggestion ???

/sbin/udevsettle

Why do you think udev is involved at this moment ?

/dev/nbd0 is created when the module is loaded (thus before) and the partitions when the partition table is read (from within the open).

Regards,
Laurent

Signature · 3 lines
------------- [email protected] ---------------
"The best way to predict the future is to invent it."
- Alan Kay
#8 lore raw
Quoted 12 lines · Laurent Vivier
Quoted 10 lines · Avi Kivity
Quoted 8 lines · Laurent Vivier

int timeout = 5;
while ((fd = open(device, O_RDWR)) == -1 && timeout--)

    sleep(1);

Any suggestion ???

Quoted 8 lines
Quoted 2 lines

/sbin/udevsettle

Why do you think udev is involved at this moment ?

/dev/nbd0 is created when the module is loaded (thus before) and the partitions when the partition table is read (from within the open).

It is udev that creates the devices, based on events it receives from the kernel. Calling udevsettle after the kernel instructs udev to create the device files will wait until they are actually created.

Signature · 2 lines
I have a truly marvellous patch that fixes the bug which this
signature is too narrow to contain.
#9 lore raw
Quoted 18 lines · Avi Kivity
Quoted 17 lines · Laurent Vivier
Quoted 6 lines · Avi Kivity
Quoted 4 lines · Laurent Vivier

int timeout = 5;
while ((fd = open(device, O_RDWR)) == -1 && timeout--)

    sleep(1);
Quoted 6 lines
Quoted 4 lines

Any suggestion ???

/sbin/udevsettle

Why do you think udev is involved at this moment ?

Quoted 7 lines
Quoted 3 lines

/dev/nbd0 is created when the module is loaded (thus before) and the partitions when the partition table is read (from within the open).

It is udev that creates the devices, based on events it receives from the kernel. Calling udevsettle after the kernel instructs udev to create the device files will wait until they are actually created.

Yes, I agree but the kernel events are generated by the open(), so I think I can't use this to know if I can use open().

I've used udevmonitor to see what happen:
- without open() -> no events
- whith open() whithout sleep() -> no events
-> open() + sleep() -> events generated by the partition creation.

So, what do I miss ?

Laurent

Signature · 3 lines
------------- [email protected] ---------------
"The best way to predict the future is to invent it."
- Alan Kay
#10 lore raw
Quoted 6 lines · Laurent Vivier
Quoted 2 lines

It is udev that creates the devices, based on events it receives from the kernel. Calling udevsettle after the kernel instructs udev to create the device files will wait until they are actually created.

Yes, I agree but the kernel events are generated by the open(), so I think I can't use this to know if I can use open().

Quoted 7 lines

I've used udevmonitor to see what happen:
- without open() -> no events
- whith open() whithout sleep() -> no events
-> open() + sleep() -> events generated by the partition creation.

So, what do I miss ?

For this, it seems like sleep() generates the events, which is a little unlikely. I suggest re-checking this.

There is also some ioctl which can be used to force re-reading the partition table, perhaps invoking that and then udevsettle will suffice.

Signature · 2 lines
I have a truly marvellous patch that fixes the bug which this
signature is too narrow to contain.
#11 lore raw
Quoted 17 lines · Avi Kivity
Quoted 13 lines · Laurent Vivier
Quoted 4 lines

It is udev that creates the devices, based on events it receives from the kernel. Calling udevsettle after the kernel instructs udev to create the device files will wait until they are actually created.

Yes, I agree but the kernel events are generated by the open(), so I think I can't use this to know if I can use open().

I've used udevmonitor to see what happen:
- without open() -> no events
- whith open() whithout sleep() -> no events
-> open() + sleep() -> events generated by the partition creation.

Quoted 5 lines
Quoted 2 lines

So, what do I miss ?

For this, it seems like sleep() generates the events, which is a little unlikely. I suggest re-checking this.

re-checked, no events with sleep() only.

events generated with sleep() + open() are:

UEVENT[1213473088.354639] change@/block/nbd0
ACTION=change
DEVPATH=/block/nbd0
SUBSYSTEM=block
MAJOR=43
MINOR=0
DEVTYPE=disk
SEQNUM=2921

UEVENT[1213473088.354721] add@/block/nbd0/nbd0p1
ACTION=add
DEVPATH=/block/nbd0/nbd0p1
SUBSYSTEM=block
MAJOR=43
MINOR=1
DEVTYPE=partition
SEQNUM=2922

UEVENT[1213473088.354781] add@/block/nbd0/nbd0p2
ACTION=add
DEVPATH=/block/nbd0/nbd0p2
SUBSYSTEM=block
MAJOR=43
MINOR=2
DEVTYPE=partition
SEQNUM=2923

UEVENT[1213473088.354842] add@/block/nbd0/nbd0p5
ACTION=add
DEVPATH=/block/nbd0/nbd0p5
SUBSYSTEM=block
MAJOR=43
MINOR=5
DEVTYPE=partition
SEQNUM=2924

UDEV [1213473088.355617] change@/block/nbd0
UDEV_LOG=3
ACTION=change
DEVPATH=/block/nbd0
SUBSYSTEM=block
MAJOR=43
MINOR=0
DEVTYPE=disk
SEQNUM=2921
UDEVD_EVENT=1
DEVNAME=/dev/nbd0

UDEV [1213473088.356818] add@/block/nbd0/nbd0p1
UDEV_LOG=3
ACTION=add
DEVPATH=/block/nbd0/nbd0p1
SUBSYSTEM=block
MAJOR=43
MINOR=1
DEVTYPE=partition
SEQNUM=2922
UDEVD_EVENT=1
DEVNAME=/dev/nbd0p1

UDEV [1213473088.358335] add@/block/nbd0/nbd0p5
UDEV_LOG=3
ACTION=add
DEVPATH=/block/nbd0/nbd0p5
SUBSYSTEM=block
MAJOR=43
MINOR=5
DEVTYPE=partition
SEQNUM=2924
UDEVD_EVENT=1
DEVNAME=/dev/nbd0p5

UDEV [1213473088.358390] add@/block/nbd0/nbd0p2
UDEV_LOG=3
ACTION=add
DEVPATH=/block/nbd0/nbd0p2
SUBSYSTEM=block
MAJOR=43
MINOR=2
DEVTYPE=partition
SEQNUM=2923
UDEVD_EVENT=1
DEVNAME=/dev/nbd0p2

Quoted 2 lines

There is also some ioctl which can be used to force re-reading the partition table, perhaps invoking that and then udevsettle will suffice.

Yes, I know that, but to make the ioctl() you need an open()... and the open() is enough.

Regards,
Laurent

Signature · 3 lines
------------- [email protected] ---------------
"The best way to predict the future is to invent it."
- Alan Kay
#12 lore raw
Quoted 21 lines · Laurent Vivier
Quoted 19 lines · Avi Kivity
Quoted 14 lines · Laurent Vivier
Quoted 3 lines

It is udev that creates the devices, based on events it receives from the kernel. Calling udevsettle after the kernel instructs udev to create the device files will wait until they are actually created.

Yes, I agree but the kernel events are generated by the open(), so I think I can't use this to know if I can use open().

I've used udevmonitor to see what happen:
- without open() -> no events
- whith open() whithout sleep() -> no events
-> open() + sleep() -> events generated by the partition creation.

So, what do I miss ?

Quoted 7 lines
Quoted 2 lines

For this, it seems like sleep() generates the events, which is a little unlikely. I suggest re-checking this.

re-checked, no events with sleep() only.

events generated with sleep() + open() are:

I imagine the sleep() is causing another task to be scheduled (probably the server) which is what ends up generating the event.

Regards,

Anthony Liguori

#13 lore raw
Quoted 28 lines · Anthony Liguori
Quoted 27 lines · Laurent Vivier
Quoted 24 lines · Avi Kivity
Quoted 8 lines · Laurent Vivier
Quoted 5 lines

It is udev that creates the devices, based on events it receives from the kernel. Calling udevsettle after the kernel instructs udev to create the device files will wait until they are actually created.

Yes, I agree but the kernel events are generated by the open(), so I think I can't use this to know if I can use open().

Quoted 8 lines

I've used udevmonitor to see what happen:
- without open() -> no events
- whith open() whithout sleep() -> no events
-> open() + sleep() -> events generated by the partition creation.

So, what do I miss ?

For this, it seems like sleep() generates the events, which is a little unlikely. I suggest re-checking this.

Quoted 7 lines
Quoted 4 lines

re-checked, no events with sleep() only.

events generated with sleep() + open() are:

I imagine the sleep() is causing another task to be scheduled (probably the server) which is what ends up generating the event.

No, for me, the open() generates the events:

open() -> blkdev_open() -> do_open() -> rescan_partitions() -> add_partitions() -> device_add() -> kobject_uevent()

If you remove show_parts() from the main(), the partitions appear only on the first open on the device, for instance you can run manually "cmp /dev/nbd0 /dev/zero" after "qemu-nbd --connect=/dev/nbd0 ...".

Laurent

Signature · 3 lines
------------- [email protected] ---------------
"The best way to predict the future is to invent it."
- Alan Kay
#14 lore raw
Quoted 10 lines · Laurent Vivier

No, for me, the open() generates the events:

open() -> blkdev_open() -> do_open() -> rescan_partitions() -> add_partitions() -> device_add() -> kobject_uevent()

If you remove show_parts() from the main(), the partitions appear only on the first open on the device, for instance you can run manually "cmp /dev/nbd0 /dev/zero" after "qemu-nbd --connect=/dev/nbd0 ...".

Well then, simply call udevsettle after the open of the block device, after which you're guaranteed to have the paritions.

Signature · 2 lines
I have a truly marvellous patch that fixes the bug which this
signature is too narrow to contain.
#15 lore raw
Quoted 6 lines · Avi Kivity
Quoted 5 lines · Laurent Vivier

No, for me, the open() generates the events:

open() -> blkdev_open() -> do_open() -> rescan_partitions() -> add_partitions() -> device_add() -> kobject_uevent()

Quoted 8 lines
Quoted 5 lines

If you remove show_parts() from the main(), the partitions appear only on the first open on the device, for instance you can run manually "cmp /dev/nbd0 /dev/zero" after "qemu-nbd --connect=/dev/nbd0 ...".

Well then, simply call udevsettle after the open of the block device, after which you're guaranteed to have the paritions.

Not possible: if I call the open too early the partitions are not updated and thus udevsettle is useless (remember: this open() is here only to update the partition table).

Laurent

Signature · 3 lines
------------- [email protected] ---------------
"The best way to predict the future is to invent it."
- Alan Kay
#16 lore raw

PATCH v2 1/1 Merge NBD client/server int qemu-nbd

Hi,

this patch allows to connect directly a disk image file to an NBD device. It introduces the use of a unix socket (instead of inet).

- To connect a file to a device:

# qemu-nbd --connect=/dev/nbd0 my_disk.qcow2

Then you can see directly your disk (no need of nbd-client):

# fdisk -l /dev/nbd0

Disk /dev/nbd0: 4294 MB, 4294967296 bytes
255 heads, 63 sectors/track, 522 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

     Device Boot      Start         End      Blocks   Id  System
/dev/nbd0p1   *           1         492     3951958+  83  Linux
/dev/nbd0p2             493         522      240975    5  Extended
/dev/nbd0p5             493         522      240943+  82  Linux swap /

Solaris

- To disconnect the file from the device:

# qemu-nbd --disconnect /dev/nbd0

Changelog:
- v2: call show_parts() from client and avoid the sleep(1). Thank you to Avi and Anthony. Include my cleanup patch and comments from Carlo Marcelo Arenas Belon.

Regards,
Laurent

Signature · 3 lines
------------- [email protected] ---------------
"The best way to predict the future is to invent it."
- Alan Kay
nbd.c +62 -2
Index: qemu/nbd.c
===================================================================
@@ -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 @@ error:
     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
@@ -388,8 +448,8 @@ int nbd_trip(BlockDriverState *bs, int c
 	}
 
 	if (len > sizeof(data)) {
-		LOG("len (%u) is larger than max len (%u)",
-		    len, sizeof(data));
+		LOG("len (%u) is larger than max len (%lu)",
+		    len, (unsigned long)sizeof(data));
 		errno = EINVAL;
 		return -1;
 	}
nbd.h +2 -0
Index: qemu/nbd.h
===================================================================
@@ -27,6 +27,8 @@
 #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);
qemu-nbd.c +106 -3

179 lines not on this page · raw message

#17 lore raw

Oops, this one is up-to-date.

Signature · 3 lines
------------- [email protected] ---------------
"The best way to predict the future is to invent it."
- Alan Kay
nbd.c +62 -2
Index: qemu/nbd.c
===================================================================
@@ -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 @@ error:
     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
@@ -388,8 +448,8 @@ int nbd_trip(BlockDriverState *bs, int c
 	}
 
 	if (len > sizeof(data)) {
-		LOG("len (%u) is larger than max len (%u)",
-		    len, sizeof(data));
+		LOG("len (%u) is larger than max len (%lu)",
+		    len, (unsigned long)sizeof(data));
 		errno = EINVAL;
 		return -1;
 	}
nbd.h +2 -0
Index: qemu/nbd.h
===================================================================
@@ -27,6 +27,8 @@
 #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);
qemu-nbd.c +104 -3

177 lines not on this page · raw message