This filter can add block size constraints to plugins which don't
already support them. It can also enforce an error policy for badly
behaved clients which do not obey the block size constraints.
UNFINISHED: The error policy is not implemented.
---
docs/nbdkit-plugin.pod | 4 +-
.../nbdkit-block-size-constraint-filter.pod | 122 +++++++++++
configure.ac | 2 +
filters/block-size-constraint/Makefile.am | 70 ++++++
tests/Makefile.am | 4 +
.../block-size-constraint.c | 206 ++++++++++++++++++
tests/test-block-size-constraint-filter.sh | 117 ++++++++++
7 files changed, 524 insertions(+), 1 deletion(-)
diff --git a/docs/nbdkit-plugin.pod b/docs/nbdkit-plugin.pod
index 15573f0a..427ca428 100644
--- a/docs/nbdkit-plugin.pod
+++ b/docs/nbdkit-plugin.pod
@@ -890,7 +890,9 @@ that all client requests will obey the constraints. A client could
still ignore the constraints. nbdkit passes all requests through to
the plugin, because what the plugin does depends on the plugin's
policy. It might decide to serve the requests correctly anyway, or
-reject them with an error.
+reject them with an error. Plugins can avoid this complexity by using
+L<nbdkit-block-size-constraint-filter(1)> which allows both
+setting/adjusting the constraints, and selecting an error policy.
The minimum block size must be E<ge> 1. The maximum block size must
be E<le> 0xffff_ffff. minimum E<le> preferred E<le> maximum.
diff --git a/filters/block-size-constraint/nbdkit-block-size-constraint-filter.pod
b/filters/block-size-constraint/nbdkit-block-size-constraint-filter.pod
new file mode 100644
index 00000000..9205e503
--- /dev/null
+++ b/filters/block-size-constraint/nbdkit-block-size-constraint-filter.pod
@@ -0,0 +1,122 @@
+=head1 NAME
+
+nbdkit-block-size-constraint-filter - set minimum, preferred and
+maximum block size, and apply error policy
+
+=head1 SYNOPSIS
+
+ nbdkit --filter=block-size-constraint PLUGIN
+ [block-size-error-policy=allow|error]
+ [block-size-minimum=N]
+ [block-size-preferred=N]
+ [block-size-maximum=N]
+
+=head1 DESCRIPTION
+
+C<nbdkit-block-size-constraint-filter> is an L<nbdkit(1)> filter that
+can add block size constraints to plugins which don't already support
+then. It can also enforce an error policy for badly behaved clients
+which do not obey the block size constraints.
+
+For more information about block size constraints, see section
+"Block size constraints" in
+L<https://github.com/NetworkBlockDevice/nbd/blob/master/doc/proto.md>.
+
+The simplest usage is to place this filter on top of any plugin which
+does not advertise block size constraints, and set the
+C<block-size-minimum>, C<block-size-preferred> and
+C<block-size-maximum> parameters with the desired constraints. For
+example:
+
+ nbdkit --filter=block-size-constraint memory 1G \
+ block-size-preferred=32K
+
+would adjust L<nbdkit-memory-plugin(1)> so that clients should
+prefer 32K requests. You can query the NBD server advertised constraints
+using L<nbdinfo(1)>:
+
+ $ nbdinfo nbd://localhost
+ [...]
+ block_size_minimum: 1
+ block_size_preferred: 32768
+ block_size_maximum: 4294967295
+
+The second part of this filter is adjusting the error policy when
+badly behaved clients do not obey the minimum or maximum request size.
+Normally nbdkit permits these requests, leaving it up to the plugin
+whether it rejects the request with an error or tries to process the
+request (eg. trying to split an over-large request). With this filter
+you can use C<block-size-error-policy=error> to reject these requests
+in the filter with EIO error. The plugin will not see them.
+
+A related filter is L<nbdkit-blocksize-filter(1)>. That filter can
+split and combine requests for plugins that cannot handle requests
+under or over a particular size. The two filters may be used together
+- this filter advertising what the plugin can support to well-behaved
+clients, and the other filter coping with clients that are either not
+well-behaved or not capable of adjusting request size.
+
+=head1 PARAMETERS
+
+=over 4
+
+=item B<block-size-error-policy=allow>
+
+=item B<block-size-error-policy=error>
+
+If a client sends a request which is smaller than the permitted
+minimum size or larger than the permitted maximum size,
+C<block-size-error-policy> chooses what the filter will do. The
+default (and also nbdkit's default) is C<allow> which means pass the
+request through to the plugin. Use C<error> to return an EIO error
+back to the client. The plugin will not see the badly formed request
+in this case.
+
+=item B<block-size-minimum=>N
+
+=item B<block-size-preferred=>N
+
+=item B<block-size-maximum=>N
+
+Advertise minimum, preferred and/or maximum block size to the client.
+Well-behaved clients should obey these constraints.
+
+For each parameter, you can specify it as a size (using the usual
+modifiers like C<4K>).
+
+If the parameter is omitted then either the constraint advertised by
+the plugin itself is used, or a sensible default for plugins which do
+not advertise block size constraints.
+
+=back
+
+=head1 FILES
+
+=over 4
+
+=item F<$filterdir/nbdkit-block-size-constraint-filter.so>
+
+The filter.
+
+Use C<nbdkit --dump-config> to find the location of C<$filterdir>.
+
+=back
+
+=head1 VERSION
+
+C<nbdkit-limit-filter> first appeared in nbdkit 1.30.
+
+=head1 SEE ALSO
+
+L<nbdkit(1)>,
+L<nbdkit-blocksize-filter(1)>,
+L<nbdkit-filter(3)>,
+L<nbdkit-plugin(3)>.
+
+=head1 AUTHORS
+
+Richard W.M. Jones
+
+=head1 COPYRIGHT
+
+Copyright (C) 2022 Red Hat Inc.
diff --git a/configure.ac b/configure.ac
index 1c7200c9..adccd90e 100644
--- a/configure.ac
+++ b/configure.ac
@@ -109,6 +109,7 @@ non_lang_plugins="\
plugins="$(echo $(printf %s\\n $lang_plugins $non_lang_plugins | sort -u))"
filters="\
blocksize \
+ block-size-constraint \
cache \
cacheextents \
checkwrite \
@@ -1335,6 +1336,7 @@ AC_CONFIG_FILES([Makefile
plugins/zero/Makefile
filters/Makefile
filters/blocksize/Makefile
+ filters/block-size-constraint/Makefile
filters/cache/Makefile
filters/cacheextents/Makefile
filters/checkwrite/Makefile
diff --git a/filters/block-size-constraint/Makefile.am
b/filters/block-size-constraint/Makefile.am
new file mode 100644
index 00000000..244f8d66
--- /dev/null
+++ b/filters/block-size-constraint/Makefile.am
@@ -0,0 +1,70 @@
+# nbdkit
+# Copyright (C) 2019-2021 Red Hat Inc.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are
+# met:
+#
+# * Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+#
+# * Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in the
+# documentation and/or other materials provided with the distribution.
+#
+# * Neither the name of Red Hat nor the names of its contributors may be
+# used to endorse or promote products derived from this software without
+# specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
+# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
+# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+# SUCH DAMAGE.
+
+include $(top_srcdir)/common-rules.mk
+
+EXTRA_DIST = nbdkit-block-size-constraint-filter.pod
+
+filter_LTLIBRARIES = nbdkit-block-size-constraint-filter.la
+
+nbdkit_block_size_constraint_filter_la_SOURCES = \
+ block-size-constraint.c \
+ $(top_srcdir)/include/nbdkit-filter.h \
+ $(NULL)
+
+nbdkit_block_size_constraint_filter_la_CPPFLAGS = \
+ -I$(top_srcdir)/common/include \
+ -I$(top_srcdir)/common/utils \
+ -I$(top_srcdir)/include \
+ $(NULL)
+nbdkit_block_size_constraint_filter_la_CFLAGS = $(WARNINGS_CFLAGS)
+nbdkit_block_size_constraint_filter_la_LDFLAGS = \
+ -module -avoid-version -shared $(NO_UNDEFINED_ON_WINDOWS) \
+ -Wl,--version-script=$(top_srcdir)/filters/filters.syms \
+ $(NULL)
+nbdkit_block_size_constraint_filter_la_LIBADD = \
+ $(top_builddir)/common/utils/libutils.la \
+ $(top_builddir)/common/replacements/libcompat.la \
+ $(IMPORT_LIBRARY_ON_WINDOWS) \
+ $(NULL)
+
+if HAVE_POD
+
+man_MANS = nbdkit-block-size-constraint-filter.1
+CLEANFILES += $(man_MANS)
+
+nbdkit-block-size-constraint-filter.1: nbdkit-block-size-constraint-filter.pod \
+ $(top_builddir)/podwrapper.pl
+ $(PODWRAPPER) --section=1 --man $@ \
+ --html $(top_builddir)/html/$@.html \
+ $<
+
+endif HAVE_POD
diff --git a/tests/Makefile.am b/tests/Makefile.am
index e888b1a0..042cd620 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -1409,6 +1409,10 @@ test_layers_filter3_la_LIBADD = $(IMPORT_LIBRARY_ON_WINDOWS)
TESTS += test-blocksize.sh test-blocksize-extents.sh
EXTRA_DIST += test-blocksize.sh test-blocksize-extents.sh
+# block-size-constraint filter test.
+TESTS += test-block-size-constraint-filter.sh
+EXTRA_DIST += test-block-size-constraint-filter.sh
+
# cache filter test.
TESTS += \
test-cache.sh \
diff --git a/filters/block-size-constraint/block-size-constraint.c
b/filters/block-size-constraint/block-size-constraint.c
new file mode 100644
index 00000000..2b4e2e30
--- /dev/null
+++ b/filters/block-size-constraint/block-size-constraint.c
@@ -0,0 +1,206 @@
+/* nbdkit
+ * Copyright (C) 2019-2022 Red Hat Inc.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * * Neither the name of Red Hat nor the names of its contributors may be
+ * used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <config.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <inttypes.h>
+#include <string.h>
+#include <pthread.h>
+
+#include <nbdkit-filter.h>
+
+#include "ispowerof2.h"
+
+/* Block size constraints configured on the command line (0 = unset). */
+static uint32_t config_minimum;
+static uint32_t config_preferred;
+static uint32_t config_maximum;
+
+/* Error policy. */
+static enum { ALLOW, ERROR } error_policy = ALLOW;
+
+static int
+block_size_constraint_config (nbdkit_next_config *next, nbdkit_backend *nxdata,
+ const char *key, const char *value)
+{
+ int64_t r;
+
+ if (strcmp (key, "block-size-error-policy") == 0) {
+ if (strcmp (value, "allow") == 0)
+ error_policy = ALLOW;
+ else if (strcmp (value, "error") == 0)
+ error_policy = ERROR;
+ else {
+ nbdkit_error ("unknown %s: %s", key, value);
+ return -1;
+ }
+ return 0;
+ }
+ else if (strcmp (key, "block-size-minimum") == 0) {
+ r = nbdkit_parse_size (value);
+ if (r == -1 || r > UINT32_MAX) {
+ parse_error:
+ nbdkit_error ("%s: could not parse %s", key, value);
+ return -1;
+ }
+ config_minimum = r;
+ return 0;
+ }
+ else if (strcmp (key, "block-size-preferred") == 0) {
+ r = nbdkit_parse_size (value);
+ if (r == -1 || r > UINT32_MAX) goto parse_error;
+ config_preferred = r;
+ return 0;
+ }
+ else if (strcmp (key, "block-size-maximum") == 0) {
+ r = nbdkit_parse_size (value);
+ if (r == -1 || r > UINT32_MAX) goto parse_error;
+ config_maximum = r;
+ return 0;
+ }
+
+ return next (nxdata, key, value);
+}
+
+static int
+block_size_constraint_config_complete (nbdkit_next_config_complete *next,
+ nbdkit_backend *nxdata)
+{
+ /* Just do some basic checks that the values are sane (if set). */
+ if (config_minimum) {
+ if (! is_power_of_2 (config_minimum)) {
+ nbdkit_error ("block-size-minimum must be a power of 2");
+ return -1;
+ }
+ }
+
+ if (config_minimum && config_preferred) {
+ if (config_minimum > config_preferred) {
+ nbdkit_error ("block-size-minimum must be <= block-size-preferred");
+ return -1;
+ }
+ }
+
+ if (config_preferred && config_maximum) {
+ if (config_preferred > config_maximum) {
+ nbdkit_error ("block-size-preferred must be <= block-size-maximum");
+ return -1;
+ }
+ }
+
+ /* XXX We could also check the preferred and maximum are a
+ * multiple of minimum.
+ */
+
+ return next (nxdata);
+}
+
+static int
+block_size_constraint_block_size (nbdkit_next *next,
+ void *handle,
+ uint32_t *minimum, uint32_t *preferred,
+ uint32_t *maximum)
+{
+ /* If the user has set all of the block size parameters then we
+ * don't need to ask the plugin, we can go ahead and advertise them.
+ */
+ if (config_minimum && config_preferred && config_maximum) {
+ *minimum = config_minimum;
+ *preferred = config_preferred;
+ *maximum = config_maximum;
+ return 0;
+ }
+
+ /* Otherwise, ask the plugin. */
+ if (next->block_size (next, minimum, preferred, maximum) == -1)
+ return -1;
+
+ /* If the user of this filter didn't configure anything, then return
+ * the plugin values (even if unset).
+ */
+ if (!config_minimum && !config_preferred && !config_maximum)
+ return 0;
+
+ /* Now we get to the awkward case where the user configured some
+ * values but not others. There's all kinds of room for things to
+ * go wrong here, so try to check for obvious user errors as best we
+ * can.
+ */
+ if (*minimum == 0) { /* Plugin didn't set anything. */
+ if (config_minimum)
+ *minimum = config_minimum;
+ else
+ *minimum = 1;
+
+ if (config_preferred)
+ *preferred = config_preferred;
+ else
+ *preferred = 4096;
+
+ if (config_maximum)
+ *maximum = config_maximum;
+ else
+ *maximum = 0xffffffff;
+ }
+ else { /* Plugin set some values. */
+ if (config_minimum)
+ *minimum = config_minimum;
+
+ if (config_preferred)
+ *preferred = config_preferred;
+
+ if (config_maximum)
+ *maximum = config_maximum;
+ }
+
+ if (*minimum > *preferred || *preferred > *maximum) {
+ nbdkit_error ("computed block size values are invalid, minimum %" PRIu32
+ " > preferred %" PRIu32
+ " or preferred > maximum %" PRIu32,
+ *minimum, *preferred, *maximum);
+ return -1;
+ }
+ return 0;
+}
+
+static struct nbdkit_filter filter = {
+ .name = "block-size-constraint",
+ .longname = "nbdkit block size constraint filter",
+ .config = block_size_constraint_config,
+ .config_complete = block_size_constraint_config_complete,
+ .block_size = block_size_constraint_block_size,
+};
+
+NBDKIT_REGISTER_FILTER(filter)
diff --git a/tests/test-block-size-constraint-filter.sh
b/tests/test-block-size-constraint-filter.sh
new file mode 100755
index 00000000..5c240e73
--- /dev/null
+++ b/tests/test-block-size-constraint-filter.sh
@@ -0,0 +1,117 @@
+#!/usr/bin/env bash
+# nbdkit
+# Copyright (C) 2019-2022 Red Hat Inc.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are
+# met:
+#
+# * Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+#
+# * Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in the
+# documentation and/or other materials provided with the distribution.
+#
+# * Neither the name of Red Hat nor the names of its contributors may be
+# used to endorse or promote products derived from this software without
+# specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
+# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
+# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+# SUCH DAMAGE.
+
+source ./functions.sh
+set -e
+set -x
+
+requires_plugin eval
+requires nbdsh -c 'print(h.get_block_size)'
+
+# Run nbdkit eval + filter and check resulting block size constraints
+# using some nbdsh.
+
+# No parameters.
+nbdkit -U - eval \
+ block_size="echo 64K 128K 32M" \
+ get_size="echo 0" \
+ --filter=block-size-constraint \
+ --run 'nbdsh \
+ -u "$uri" \
+ -c "assert h.get_block_size(nbd.SIZE_MINIMUM) == 64 * 1024" \
+ -c "assert h.get_block_size(nbd.SIZE_PREFERRED) == 128 * 1024" \
+ -c "assert h.get_block_size(nbd.SIZE_MAXIMUM) == 32 * 1024 * 1024"
\
+ '
+
+# Adjust single values.
+nbdkit -U - eval \
+ block_size="echo 64K 128K 32M" \
+ get_size="echo 0" \
+ --filter=block-size-constraint \
+ block-size-minimum=1 \
+ --run 'nbdsh \
+ -u "$uri" \
+ -c "assert h.get_block_size(nbd.SIZE_MINIMUM) == 1" \
+ -c "assert h.get_block_size(nbd.SIZE_PREFERRED) == 128 * 1024" \
+ -c "assert h.get_block_size(nbd.SIZE_MAXIMUM) == 32 * 1024 * 1024"
\
+ '
+nbdkit -U - eval \
+ block_size="echo 64K 128K 32M" \
+ get_size="echo 0" \
+ --filter=block-size-constraint \
+ block-size-preferred=64K \
+ --run 'nbdsh \
+ -u "$uri" \
+ -c "assert h.get_block_size(nbd.SIZE_MINIMUM) == 64 * 1024" \
+ -c "assert h.get_block_size(nbd.SIZE_PREFERRED) == 64 * 1024" \
+ -c "assert h.get_block_size(nbd.SIZE_MAXIMUM) == 32 * 1024 * 1024"
\
+ '
+nbdkit -U - eval \
+ block_size="echo 64K 128K 32M" \
+ get_size="echo 0" \
+ --filter=block-size-constraint \
+ block-size-maximum=1M \
+ --run 'nbdsh \
+ -u "$uri" \
+ -c "assert h.get_block_size(nbd.SIZE_MINIMUM) == 64 * 1024" \
+ -c "assert h.get_block_size(nbd.SIZE_PREFERRED) == 128 * 1024" \
+ -c "assert h.get_block_size(nbd.SIZE_MAXIMUM) == 1 * 1024 * 1024" \
+ '
+
+# Adjust all values for a plugin which is advertising.
+nbdkit -U - eval \
+ block_size="echo 64K 128K 32M" \
+ get_size="echo 0" \
+ --filter=block-size-constraint \
+ block-size-minimum=1 \
+ block-size-preferred=4K \
+ block-size-maximum=1M \
+ --run 'nbdsh \
+ -u "$uri" \
+ -c "assert h.get_block_size(nbd.SIZE_MINIMUM) == 1" \
+ -c "assert h.get_block_size(nbd.SIZE_PREFERRED) == 4 * 1024" \
+ -c "assert h.get_block_size(nbd.SIZE_MAXIMUM) == 1 * 1024 * 1024" \
+ '
+
+# Set all values for a plugin which is not advertising.
+nbdkit -U - eval \
+ get_size="echo 0" \
+ --filter=block-size-constraint \
+ block-size-minimum=1 \
+ block-size-preferred=4K \
+ block-size-maximum=1M \
+ --run 'nbdsh \
+ -u "$uri" \
+ -c "assert h.get_block_size(nbd.SIZE_MINIMUM) == 1" \
+ -c "assert h.get_block_size(nbd.SIZE_PREFERRED) == 4 * 1024" \
+ -c "assert h.get_block_size(nbd.SIZE_MAXIMUM) == 1 * 1024 * 1024" \
+ '
--
2.35.1