From a7e7af18d64164fac42581452f6dc3c07650fcae Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Fri, 30 Jul 2021 10:19:57 +0100 Subject: [PATCH] delay: Add delay-open and delay-close Useful for simulating VDDK which has very slow connection. (cherry picked from commit de8dcd3a34a38b088a0f9a6f8ca754702ad1f598) --- filters/delay/delay.c | 58 ++++++++++++++++++++++++++- filters/delay/nbdkit-delay-filter.pod | 27 +++++++++++-- 2 files changed, 80 insertions(+), 5 deletions(-) diff --git a/filters/delay/delay.c b/filters/delay/delay.c index 5a925aa4..df3729a7 100644 --- a/filters/delay/delay.c +++ b/filters/delay/delay.c @@ -48,6 +48,8 @@ static unsigned delay_zero_ms = 0; /* zero delay (milliseconds) */ static unsigned delay_trim_ms = 0; /* trim delay (milliseconds) */ static unsigned delay_extents_ms = 0;/* extents delay (milliseconds) */ static unsigned delay_cache_ms = 0; /* cache delay (milliseconds) */ +static unsigned delay_open_ms = 0; /* open delay (milliseconds) */ +static unsigned delay_close_ms = 0; /* close delay (milliseconds) */ static int delay_fast_zero = 1; /* whether delaying zero includes fast zero */ @@ -126,6 +128,18 @@ cache_delay (int *err) return delay (delay_cache_ms, err); } +static int +open_delay (int *err) +{ + return delay (delay_open_ms, err); +} + +static int +close_delay (int *err) +{ + return delay (delay_close_ms, err); +} + /* Called for each key=value passed on the command line. */ static int delay_config (nbdkit_next_config *next, nbdkit_backend *nxdata, @@ -182,6 +196,16 @@ delay_config (nbdkit_next_config *next, nbdkit_backend *nxdata, return -1; return 0; } + else if (strcmp (key, "delay-open") == 0) { + if (parse_delay (key, value, &delay_open_ms) == -1) + return -1; + return 0; + } + else if (strcmp (key, "delay-close") == 0) { + if (parse_delay (key, value, &delay_close_ms) == -1) + return -1; + return 0; + } else return next (nxdata, key, value); } @@ -195,7 +219,9 @@ delay_config (nbdkit_next_config *next, nbdkit_backend *nxdata, "delay-extents=[ms] Extents delay in seconds/milliseconds.\n" \ "delay-cache=[ms] Cache delay in seconds/milliseconds.\n" \ "wdelay=[ms] Write, zero and trim delay in secs/msecs.\n" \ - "delay-fast-zero= Delay fast zero requests (default true).\n" + "delay-fast-zero= Delay fast zero requests (default true).\n" \ + "delay-open=[ms] Open delay in seconds/milliseconds.\n" \ + "delay-close=[ms] Close delay in seconds/milliseconds." /* Override the plugin's .can_fast_zero if needed */ static int @@ -208,6 +234,34 @@ delay_can_fast_zero (nbdkit_next *next, return next->can_fast_zero (next); } +/* Open connection. */ +static void * +delay_open (nbdkit_next_open *next, nbdkit_context *nxdata, + int readonly, const char *exportname, int is_tls) +{ + int err; + + if (open_delay (&err) == -1) { + errno = err; + nbdkit_error ("delay: %m"); + return NULL; + } + + if (next (nxdata, readonly, exportname) == -1) + return NULL; + + return NBDKIT_HANDLE_NOT_NEEDED; +} + +/* Close connection. */ +static void +delay_close (void *handle) +{ + int err; + + close_delay (&err); +} + /* Read data. */ static int delay_pread (nbdkit_next *next, @@ -285,6 +339,8 @@ static struct nbdkit_filter filter = { .config = delay_config, .config_help = delay_config_help, .can_fast_zero = delay_can_fast_zero, + .open = delay_open, + .close = delay_close, .pread = delay_pread, .pwrite = delay_pwrite, .zero = delay_zero, diff --git a/filters/delay/nbdkit-delay-filter.pod b/filters/delay/nbdkit-delay-filter.pod index d6961a9e..11ae544b 100644 --- a/filters/delay/nbdkit-delay-filter.pod +++ b/filters/delay/nbdkit-delay-filter.pod @@ -9,10 +9,15 @@ nbdkit-delay-filter - nbdkit delay filter nbdkit --filter=delay plugin rdelay=NNms wdelay=NNms [plugin-args...] nbdkit --filter=delay plugin [plugin-args ...] - delay-read=(SECS|NNms) delay-write=(SECS|NNms) - delay-zero=(SECS|NNms) delay-trim=(SECS|NNms) - delay-extents=(SECS|NNms) delay-cache=(SECS|NNms) + delay-read=(SECS|NNms) + delay-write=(SECS|NNms) + delay-zero=(SECS|NNms) + delay-trim=(SECS|NNms) + delay-extents=(SECS|NNms) + delay-cache=(SECS|NNms) delay-fast-zero=BOOL + delay-open=(SECS|NNms) + delay-close=(SECS|NNms) =head1 DESCRIPTION @@ -108,6 +113,20 @@ delay as any other zero request; but setting this parameter to false instantly fails a fast zero response without waiting for or consulting the plugin. +=item BSECS + +=item BNNB + +=item BSECS + +=item BNNB + +(nbdkit E 1.28) + +Delay open and close operations by C seconds or C +milliseconds. Open corresponds to client connection. Close may not +be visible to clients if they abruptly disconnect. + =back =head1 FILES @@ -140,4 +159,4 @@ Richard W.M. Jones =head1 COPYRIGHT -Copyright (C) 2018 Red Hat Inc. +Copyright (C) 2018-2021 Red Hat Inc. -- 2.31.1