Skip to contentSkip to conversation

engines: Add Network Block Device (NBD) support using libnbd.

PATCH Superseded Richard W.M. Jones 3 participants 5 messages Aug 2, 2019 – Aug 3, 2019 Kbuild

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 engines: Add Network Block Device (NBD) support using libnbd.

v3 (the synchronous version) was posted earlier today. It looks like it's not available in the online archives yet.

This is v4 which implements full asynchronous support and in my testing is about 4-5 times faster.

Rich.

#1 lore raw

PATCH v4 1/1 engines: Add Network Block Device (NBD) support using libnbd.

This commit adds a new engine for testing Network Block Devices directly. It requires libnbd (https://github.com/libguestfs/libnbd).

To see how to test nbdkit or qemu-nbd read examples/nbd.fio.

---
 HOWTO            |   2 +
 Makefile         |   6 +
 configure        |  27 ++++
 engines/nbd.c    | 368 +++++++++++++++++++++++++++++++++++++++++++++++
 examples/nbd.fio |  35 +++++
 fio.1            |   3 +
 optgroup.c       |   4 +
 optgroup.h       |   2 +
 options.c        |   3 +
 9 files changed, 450 insertions(+)
HOWTO +2 -0
index 81244064..955ea43c 100644
@@ -1998,6 +1998,8 @@ I/O engine
 			requests for IME. FIO will then decide when to commit these requests.
 		**libiscsi**
 			Read and write iscsi lun with libiscsi.
+		**nbd**
+			Synchronous read and write a Network Block Device (NBD).
 
 I/O engine specific parameters
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Makefile +6 -0
index d7e5fca7..fe02bf1d 100644
@@ -65,6 +65,12 @@ ifdef CONFIG_LIBISCSI
   SOURCE += engines/libiscsi.c
 endif
 
+ifdef CONFIG_LIBNBD
+  CFLAGS += $(LIBNBD_CFLAGS)
+  LIBS += $(LIBNBD_LIBS)
+  SOURCE += engines/nbd.c
+endif
+
 ifdef CONFIG_64BIT
   CFLAGS += -DBITS_PER_LONG=64
 endif
configure +27 -0
index a0692d58..b11b2dce 100755
@@ -149,6 +149,7 @@ disable_pmem="no"
 disable_native="no"
 march_set="no"
 libiscsi="no"
+libnbd="no"
 prefix=/usr/local
 
 # parse options
@@ -207,6 +208,8 @@ for opt do
   ;;
   --enable-libiscsi) libiscsi="yes"
   ;;
+  --enable-libnbd) libnbd="yes"
+  ;;
   --disable-tcmalloc) disable_tcmalloc="yes"
   ;;
   --help)
@@ -245,6 +248,7 @@ if test "$show_help" = "yes" ; then
   echo "--disable-native        Don't build for native host"
   echo "--with-ime=             Install path for DDN's Infinite Memory Engine"
   echo "--enable-libiscsi       Enable iscsi support"
+  echo "--enable-libnbd         Enable libnbd (NBD engine) support"
   echo "--disable-tcmalloc	Disable tcmalloc support"
   exit $exit_val
 fi
@@ -2014,6 +2018,23 @@ if test "$libiscsi" = "yes" ; then
 fi
 print_config "iscsi engine" "$libiscsi"
 
+##########################################
+# Check if we have libnbd (for NBD support).
+minimum_libnbd=0.9.6
+if test "$libnbd" = "yes" ; then
+  if $(pkg-config --atleast-version=$minimum_libnbd libnbd); then
+    libnbd="yes"
+    libnbd_cflags=$(pkg-config --cflags libnbd)
+    libnbd_libs=$(pkg-config --libs libnbd)
+  else
+    if test "$libnbd" = "yes" ; then
+      echo "libnbd" "Install libnbd >= $minimum_libnbd"
+    fi
+    libnbd="no"
+  fi
+fi
+print_config "NBD engine" "$libnbd"
+
 ##########################################
 # Check if we have lex/yacc available
 yacc="no"
@@ -2698,6 +2719,12 @@ if test "$libiscsi" = "yes" ; then
   echo "LIBISCSI_CFLAGS=$libiscsi_cflags" >> $config_host_mak
   echo "LIBISCSI_LIBS=$libiscsi_libs" >> $config_host_mak
 fi
+if test "$libnbd" = "yes" ; then
+  output_sym "CONFIG_LIBNBD"
+  echo "CONFIG_LIBNBD=m" >> $config_host_mak
+  echo "LIBNBD_CFLAGS=$libnbd_cflags" >> $config_host_mak
+  echo "LIBNBD_LIBS=$libnbd_libs" >> $config_host_mak
+fi
 cat > $TMPC << EOF
 int main(int argc, char **argv)
 {
engines/nbd.c new file +368 -0

368 lines not on this page · raw message

examples/nbd.fio new file +35 -0
new file mode 100644
index 00000000..27e15086
@@ -0,0 +1,35 @@
+# To use fio to test nbdkit:
+#
+# nbdkit -U - memory size=256M --run 'export unixsocket; fio examples/nbd.fio'
+#
+# To use fio to test qemu-nbd:
+#
+# rm -f /tmp/disk.img /tmp/socket
+# truncate -s 256M /tmp/disk.img
+# export unixsocket=/tmp/socket
+# qemu-nbd -t -k $unixsocket -f raw /tmp/disk.img &
+# fio examples/nbd.fio
+# killall qemu-nbd
+
+[global]
+ioengine=nbd
+uri=nbd+unix:///?socket=${unixsocket}
+# Starting from nbdkit 1.14 the following will work:
+#uri=${uri}
+rw=randrw
+time_based
+runtime=60
+group_reporting
+iodepth=64
+
+[job0]
+offset=0
+
+[job1]
+offset=064m
+
+[job2]
+offset=128m
+
+[job3]
+offset=192m
fio.1 +3 -0
index 2966d9d5..30b17f02 100644
@@ -1756,6 +1756,9 @@ FIO will then decide when to commit these requests.
 .TP
 .B libiscsi
 Read and write iscsi lun with libiscsi.
+.TP
+.B nbd
+Synchronous read and write a Network Block Device (NBD).
 .SS "I/O engine specific parameters"
 In addition, there are some parameters which are only valid when a specific
 \fBioengine\fR is in use. These are used identically to normal parameters,
optgroup.c +4 -0
index 04ceec7e..c228ff29 100644
@@ -169,6 +169,10 @@ static const struct opt_group fio_opt_cat_groups[] = {
 		.name	= "libhdfs I/O engine", /* libhdfs */
 		.mask	= FIO_OPT_G_HDFS,
 	},
+	{
+		.name	= "NBD I/O engine", /* NBD */
+		.mask	= FIO_OPT_G_NBD,
+	},
 	{
 		.name	= NULL,
 	},
optgroup.h +2 -0
index 148c8da1..8009bf25 100644
@@ -63,6 +63,7 @@ enum opt_category_group {
 	__FIO_OPT_G_SG,
 	__FIO_OPT_G_MMAP,
 	__FIO_OPT_G_ISCSI,
+	__FIO_OPT_G_NBD,
 	__FIO_OPT_G_NR,
 
 	FIO_OPT_G_RATE		= (1ULL << __FIO_OPT_G_RATE),
@@ -102,6 +103,7 @@ enum opt_category_group {
 	FIO_OPT_G_MMAP		= (1ULL << __FIO_OPT_G_MMAP),
 	FIO_OPT_G_INVALID	= (1ULL << __FIO_OPT_G_NR),
 	FIO_OPT_G_ISCSI         = (1ULL << __FIO_OPT_G_ISCSI),
+	FIO_OPT_G_NBD		= (1ULL << __FIO_OPT_G_NBD),
 };
 
 extern const struct opt_group *opt_group_from_mask(uint64_t *mask);
options.c +3 -0
index 95086074..f4c9bedf 100644
@@ -1899,6 +1899,9 @@ struct fio_option fio_options[FIO_MAX_OPTS] = {
 			    .help = "HTTP (WebDAV/S3) IO engine",
 			  },
 #endif
+			  { .ival = "nbd",
+			    .help = "Network Block Device (NBD) IO engine"
+			  },
 		},
 	},
 	{
Signature · 1 line
2.22.0
#2 lore raw
Quoted 23 lines · Richard W.M. Jones
engines/nbd.c new file +17 -0
new file mode 100644
index 00000000..399da5f3
@@ -0,0 +1,368 @@
+/*
+ * NBD engine
+ *
+ * IO engine that talks to an NBD server.
+ *
+ * Copyright (C) 2019 Red Hat Inc.
+ * Written by Richard W.M. Jones <[email protected]>
+ *
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <errno.h>
+
+#include <libnbd.h>
+

engines/nbd.c line 27

Quoted 10 lines
engines/nbd.c new file +10 -0
+#include "../fio.h"
+#include "../optgroup.h"
+
+/* Actually this differs across servers, but for nbdkit ... */
+#define NBD_MAX_REQUEST_SIZE (64 * 1024 * 1024)
+
+/* Storage for the NBD handle. */
+struct nbd_data {
+	struct nbd_handle *nbd;
+        int debug;

Here (and lots of other places) you have a mix of tabs and spaces. Please make them all tab based.

Signature · 1 line
Jens Axboe
#3 lore raw
In reply to Richard W.M. Jones
Quoted 24 lines · Richard W.M. Jones

This commit adds a new engine for testing Network Block Devices directly. It requires libnbd (https://github.com/libguestfs/libnbd).

To see how to test nbdkit or qemu-nbd read examples/nbd.fio.

---
 HOWTO            |   2 +
 Makefile         |   6 +
 configure        |  27 ++++
 engines/nbd.c    | 368 +++++++++++++++++++++++++++++++++++++++++++++++
 examples/nbd.fio |  35 +++++
 fio.1            |   3 +
 optgroup.c       |   4 +
 optgroup.h       |   2 +
 options.c        |   3 +
 9 files changed, 450 insertions(+)
HOWTO +0 -0
index 81244064..955ea43c 100644

HOWTO I/O engine · line 2002

Quoted 5 lines
HOWTO +2 -0
@@ -1998,6 +1998,8 @@ I/O engine
                        requests for IME. FIO will then decide when to commit these requests.
                **libiscsi**
                        Read and write iscsi lun with libiscsi.
+               **nbd**
+                       Synchronous read and write a Network Block Device (NBD).

I wonder if the engine should be called nbdkit as that's what it uses under hood to talk NBD...

Quoted 33 lines
HOWTO +0 -0
 I/O engine specific parameters
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Makefile +6 -0
index d7e5fca7..fe02bf1d 100644
@@ -65,6 +65,12 @@ ifdef CONFIG_LIBISCSI
   SOURCE += engines/libiscsi.c
 endif
 
+ifdef CONFIG_LIBNBD
+  CFLAGS += $(LIBNBD_CFLAGS)
+  LIBS += $(LIBNBD_LIBS)
+  SOURCE += engines/nbd.c
+endif
+
 ifdef CONFIG_64BIT
   CFLAGS += -DBITS_PER_LONG=64
 endif
configure +1 -0
index a0692d58..b11b2dce 100755
@@ -149,6 +149,7 @@ disable_pmem="no"
 disable_native="no"
 march_set="no"
 libiscsi="no"
+libnbd="no"
 prefix=/usr/local
 
 # parse options

configure for opt do · line 212

Quoted 5 lines
configure +2 -0
@@ -207,6 +208,8 @@ for opt do
   ;;
   --enable-libiscsi) libiscsi="yes"
   ;;
+  --enable-libnbd) libnbd="yes"
+  ;;

Why not enable by default if available and then do a probe to see if it can be enabled?

Quoted 9 lines
configure +9 -0
+
+static struct ioengine_ops ioengine = {
+       .name                   = "nbd",
+       .version                = FIO_IOOPS_VERSION,
+       .options                = options,
+       .option_struct_size     = sizeof(struct nbd_options),
+       .flags                  = FIO_DISKLESSIO | FIO_NOEXTEND,
+
+       .setup                  = nbd_setup,

configure for opt do

Quoted 10 lines
configure +10 -0
+       .init                   = nbd_init,
+       .cleanup                = nbd_cleanup,
+       .queue                  = nbd_queue,
+       .getevents              = nbd_getevents,
+       .event                  = nbd_event,
+       .io_u_init              = nbd_io_u_init,
+       .io_u_free              = nbd_io_u_free,
+
+       .open_file              = nbd_open_file,
+       .invalidate             = nbd_invalidate,

Do you have to register functions for things that you don't do anything (e.g. nbd_io_u_free)?

Quoted 22 lines
examples/nbd.fio new file +19 -0
@@ -0,0 +1,35 @@
+# To use fio to test nbdkit:
+#
+# nbdkit -U - memory size=256M --run 'export unixsocket; fio examples/nbd.fio'
+#
+# To use fio to test qemu-nbd:
+#
+# rm -f /tmp/disk.img /tmp/socket
+# truncate -s 256M /tmp/disk.img
+# export unixsocket=/tmp/socket
+# qemu-nbd -t -k $unixsocket -f raw /tmp/disk.img &
+# fio examples/nbd.fio
+# killall qemu-nbd
+
+[global]
+ioengine=nbd
+uri=nbd+unix:///?socket=${unixsocket}
+# Starting from nbdkit 1.14 the following will work:
+#uri=${uri}
+rw=randrw

examples/nbd.fio line 29

Quoted 10 lines
examples/nbd.fio new file +10 -0
+time_based
+runtime=60
+group_reporting
+iodepth=64
+
+[job0]
+offset=0
+
+[job1]
+offset=064m

I'd drop the leading 0.

Signature · 1 line
Sitsofe | http://sucs.org/~sits/
#4 lore raw
Quoted 6 lines · Sitsofe Wheeler

HOWTO I/O engine · line 2002

Quoted 2 lines · Richard W.M. Jones
HOWTO +2 -0
+               **nbd**
+                       Synchronous read and write a Network Block Device (NBD).

I wonder if the engine should be called nbdkit as that's what it uses under hood to talk NBD...

libnbd (nbdkit is a server). libnbd is supposed to be "the" NBD client library however. I'm not sure what other NBD engines would be useful here.

Quoted 5 lines

configure for opt do · line 212

Quoted 2 lines
configure +2 -0
+  --enable-libnbd) libnbd="yes"
+  ;;

Why not enable by default if available and then do a probe to see if it can be enabled?

Yes, we can do this. I disabled it out of an abundance of caution, so that no one's build could be broken if I got it wrong.

Quoted 7 lines

engines/nbd.c line 357

Quoted 4 lines
engines/nbd.c new file +4 -0
+       .io_u_free              = nbd_io_u_free,
+
+       .open_file              = nbd_open_file,
+       .invalidate             = nbd_invalidate,

Do you have to register functions for things that you don't do anything (e.g. nbd_io_u_free)?

I think io_u_free is the only one which is an actual no-op (but it balances the corresponding init function). I'm not really sure of the purpose of open_file and invalidate except that other engines also did the same thing.

Quoted 3 lines
Quoted 1 line

+offset=064m

I'd drop the leading 0.

This is a bug, I'll send a follow-up patch to fix it.

Rich.

Signature · 4 lines
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
virt-builder quickly builds VMs from scratch
http://libguestfs.org/virt-builder.1.html