pipe: Add a utility for buffering data from sockets

When dealing with sockets most code in SPDK buffers
data into large chunks to minimize the number of
syscalls made. The pipe utility is designed to make
that easy.

Change-Id: Ie29966712bbfb43fb49457e042903cf45864e6c6
Signed-off-by: Ben Walker <benjamin.walker@intel.com>
Reviewed-on: https://review.gerrithub.io/c/spdk/spdk/+/465707
Tested-by: SPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: Jim Harris <james.r.harris@intel.com>
Reviewed-by: Shuhei Matsumoto <shuhei.matsumoto.xt@hitachi.com>
This commit is contained in:
Ben Walker 2019-08-19 12:44:17 -07:00 committed by Tomasz Zawadzki
parent aa4856632e
commit 063252fa66
9 changed files with 1132 additions and 2 deletions

View File

@ -20,6 +20,11 @@ Added boolean return value for function spdk_fs_set_cache_size to indicate its o
Added `blobfs_set_cache_size` RPC method to set cache size for blobstore filesystem.
### util
`spdk_pipe`, a new utility for buffering data from sockets or files for parsing
has been added. The public API is available at `include/spdk/pipe.h`.
## v19.10:
### rpc

149
include/spdk/pipe.h Normal file
View File

@ -0,0 +1,149 @@
/*-
* BSD LICENSE
*
* Copyright (c) Intel Corporation. All rights reserved.
*
* 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 Intel Corporation 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 THE COPYRIGHT HOLDERS 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 THE COPYRIGHT
* OWNER 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.
*/
/** \file
* A pipe that is intended for buffering data between a source, such as
* a socket, and a sink, such as a parser, or vice versa. Any time data
* is received in units that differ from the the units it is consumed
* in may benefit from using a pipe.
*
* The pipe is not thread safe. Only a single thread can act as both
* the producer (called the writer) and the consumer (called the reader).
*/
#ifndef SPDK_PIPE_H
#define SPDK_PIPE_H
#include "spdk/stdinc.h"
struct spdk_pipe;
/**
* Construct a pipe around the given memory buffer. The pipe treats the memory
* buffer as a circular ring of bytes.
*
* The available size for writing will be one less byte than provided. A single
* byte must be reserved to distinguish queue full from queue empty conditions.
*
* \param buf The data buffer that backs this pipe.
* \param sz The size of the data buffer.
*
* \return spdk_pipe. The new pipe.
*/
struct spdk_pipe *spdk_pipe_create(void *buf, uint32_t sz);
/**
* Destroys the pipe. This does not release the buffer, but does
* make it safe for the user to release the buffer.
*
* \param pipe The pipe to operate on.
*/
void spdk_pipe_destroy(struct spdk_pipe *pipe);
/**
* Acquire memory from the pipe for writing.
*
* This function will acquire up to sz bytes from the pipe to be used for
* writing. It may return fewer total bytes.
*
* The memory is only marked as consumed upon a call to spdk_pipe_writer_advance().
* Multiple calls to this function without calling advance return the same region
* of memory.
*
* \param pipe The pipe to operate on.
* \param sz The size requested.
* \param iovs A two element iovec array that will be populated with the requested memory.
*
* \return The total bytes obtained. May be 0.
*/
int spdk_pipe_writer_get_buffer(struct spdk_pipe *pipe, uint32_t sz, struct iovec *iovs);
/**
* Advance the write pointer by the given number of bytes
*
* The user can obtain memory from the pipe using spdk_pipe_writer_get_buffer(),
* but only calling this function marks it as consumed. The user is not required
* to advance the same number of bytes as was obtained from spdk_pipe_writer_get_buffer().
* However, upon calling this function, the previous memory region is considered
* invalid and the user must call spdk_pipe_writer_get_buffer() again to obtain
* additional memory.
*
* The user cannot advance past the current read location.
*
* \param pipe The pipe to operate on.
* \param count The number of bytes to advance.
*
* \return On error, a negated errno. On success, 0.
*/
int spdk_pipe_writer_advance(struct spdk_pipe *pipe, uint32_t count);
/**
* Get the number of bytes available to read from the pipe.
*
* \param pipe The pipe to operate on.
*
* \return The number of bytes available for reading.
*/
uint32_t spdk_pipe_reader_bytes_available(struct spdk_pipe *pipe);
/**
* Obtain previously written memory from the pipe for reading.
*
* This call populates the two element iovec provided with a region
* of memory containing the next available data in the pipe. The size
* will be up to sz bytes, but may be less.
*
* Calling this function does not mark the memory as consumed. Calling this function
* twice without a call to spdk_pipe_reader_advance in between will return the same
* region of memory.
*
* \param pipe The pipe to operate on.
* \param sz The size requested.
* \param iovs A two element iovec array that will be populated with the requested memory.
*
* \return On error, a negated errno. On success, the total number of bytes available.
*/
int spdk_pipe_reader_get_buffer(struct spdk_pipe *pipe, uint32_t sz, struct iovec *iovs);
/**
* Mark memory as read, making it available for writing. The user is not required
* to advance the same number of byte as was obtained by a previous call to
* spdk_pipe_reader_get_buffer().
*
* \param pipe The pipe to operate on.
* \param count The number of bytes to advance.
*
* \return On error, a negated errno. On success, 0.
*/
int spdk_pipe_reader_advance(struct spdk_pipe *pipe, uint32_t count);
#endif

View File

@ -35,7 +35,7 @@ SPDK_ROOT_DIR := $(abspath $(CURDIR)/../..)
include $(SPDK_ROOT_DIR)/mk/spdk.common.mk
C_SRCS = base64.c bit_array.c cpuset.c crc16.c crc32.c crc32c.c crc32_ieee.c \
dif.c fd.c file.c math.c strerror_tls.c string.c uuid.c
dif.c fd.c file.c math.c pipe.c strerror_tls.c string.c uuid.c
LIBNAME = util
LOCAL_SYS_LIBS = -luuid

273
lib/util/pipe.c Normal file
View File

@ -0,0 +1,273 @@
/*-
* BSD LICENSE
*
* Copyright (c) Intel Corporation.
* All rights reserved.
*
* 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 Intel Corporation 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 THE COPYRIGHT HOLDERS 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 THE COPYRIGHT
* OWNER 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 "spdk/pipe.h"
#include "spdk/util.h"
struct spdk_pipe {
uint8_t *buf;
uint32_t sz;
uint32_t write;
uint32_t read;
};
struct spdk_pipe *
spdk_pipe_create(void *buf, uint32_t sz)
{
struct spdk_pipe *pipe;
pipe = calloc(1, sizeof(*pipe));
if (pipe == NULL) {
return NULL;
}
pipe->buf = buf;
pipe->sz = sz;
return pipe;
}
void
spdk_pipe_destroy(struct spdk_pipe *pipe)
{
free(pipe);
}
int
spdk_pipe_writer_get_buffer(struct spdk_pipe *pipe, uint32_t requested_sz, struct iovec *iovs)
{
uint32_t sz;
uint32_t read;
uint32_t write;
read = pipe->read;
write = pipe->write;
if (read <= write) {
requested_sz = spdk_min(requested_sz, ((read + pipe->sz) - write - 1));
sz = spdk_min(requested_sz, pipe->sz - write);
if (sz == 0) {
iovs[0].iov_base = NULL;
} else {
iovs[0].iov_base = pipe->buf + write;
}
iovs[0].iov_len = sz;
requested_sz -= sz;
if (requested_sz > 0) {
sz = spdk_min(requested_sz, read);
if (sz == 0) {
iovs[1].iov_base = NULL;
} else {
iovs[1].iov_base = pipe->buf;
}
iovs[1].iov_len = sz;
} else {
iovs[1].iov_base = NULL;
iovs[1].iov_len = 0;
}
} else {
sz = spdk_min(requested_sz, read - write - 1);
if (sz == 0) {
iovs[0].iov_base = NULL;
} else {
iovs[0].iov_base = pipe->buf + write;
}
iovs[0].iov_len = sz;
iovs[1].iov_base = NULL;
iovs[1].iov_len = 0;
}
return iovs[0].iov_len + iovs[1].iov_len;
}
int
spdk_pipe_writer_advance(struct spdk_pipe *pipe, uint32_t requested_sz)
{
uint32_t sz;
uint32_t read;
uint32_t write;
read = pipe->read;
write = pipe->write;
if (requested_sz > pipe->sz - 1) {
return -EINVAL;
}
if (read <= write) {
if (requested_sz > (read + pipe->sz) - write) {
return -EINVAL;
}
sz = spdk_min(requested_sz, pipe->sz - write);
write += sz;
if (write > pipe->sz - 1) {
write = 0;
}
requested_sz -= sz;
if (requested_sz > 0) {
if (requested_sz >= read) {
return -EINVAL;
}
write = requested_sz;
}
} else {
if (requested_sz > (read - write - 1)) {
return -EINVAL;
}
write += requested_sz;
}
pipe->write = write;
return 0;
}
uint32_t
spdk_pipe_reader_bytes_available(struct spdk_pipe *pipe)
{
uint32_t read;
uint32_t write;
read = pipe->read;
write = pipe->write;
if (read <= write) {
return write - read;
}
return (write + pipe->sz) - read;
}
int
spdk_pipe_reader_get_buffer(struct spdk_pipe *pipe, uint32_t requested_sz, struct iovec *iovs)
{
uint32_t sz;
uint32_t read;
uint32_t write;
read = pipe->read;
write = pipe->write;
if (read <= write) {
sz = spdk_min(requested_sz, write - read);
if (sz == 0) {
iovs[0].iov_base = NULL;
} else {
iovs[0].iov_base = pipe->buf + read;
}
iovs[0].iov_len = sz;
iovs[1].iov_base = NULL;
iovs[1].iov_len = 0;
} else {
sz = spdk_min(requested_sz, pipe->sz - read);
if (sz == 0) {
iovs[0].iov_base = NULL;
} else {
iovs[0].iov_base = pipe->buf + read;
}
iovs[0].iov_len = sz;
requested_sz -= sz;
if (requested_sz > 0) {
sz = spdk_min(requested_sz, write);
if (sz == 0) {
iovs[1].iov_base = NULL;
} else {
iovs[1].iov_base = pipe->buf;
}
iovs[1].iov_len = sz;
} else {
iovs[1].iov_base = NULL;
iovs[1].iov_len = 0;
}
}
return iovs[0].iov_len + iovs[1].iov_len;
}
int
spdk_pipe_reader_advance(struct spdk_pipe *pipe, uint32_t requested_sz)
{
uint32_t sz;
uint32_t read;
uint32_t write;
read = pipe->read;
write = pipe->write;
if (read <= write) {
if (requested_sz > (write - read)) {
return -EINVAL;
}
read += requested_sz;
} else {
sz = spdk_min(requested_sz, pipe->sz - read);
read += sz;
if (read > pipe->sz - 1) {
read = 0;
}
requested_sz -= sz;
if (requested_sz > 0) {
if (requested_sz > write) {
return -EINVAL;
}
read = requested_sz;
}
}
pipe->read = read;
return 0;
}

View File

@ -34,7 +34,8 @@
SPDK_ROOT_DIR := $(abspath $(CURDIR)/../../../..)
include $(SPDK_ROOT_DIR)/mk/spdk.common.mk
DIRS-y = base64.c bit_array.c cpuset.c crc16.c crc32_ieee.c crc32c.c dif.c string.c
DIRS-y = base64.c bit_array.c cpuset.c crc16.c crc32_ieee.c crc32c.c dif.c \
pipe.c string.c
.PHONY: all clean $(DIRS-y)

1
test/unit/lib/util/pipe.c/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
pipe_ut

View File

@ -0,0 +1,38 @@
#
# BSD LICENSE
#
# Copyright (c) Intel Corporation.
# All rights reserved.
#
# 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 Intel Corporation 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 THE COPYRIGHT HOLDERS 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 THE COPYRIGHT
# OWNER 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.
#
SPDK_ROOT_DIR := $(abspath $(CURDIR)/../../../../..)
TEST_FILE = pipe_ut.c
include $(SPDK_ROOT_DIR)/mk/spdk.unittest.mk

View File

@ -0,0 +1,662 @@
/*-
* BSD LICENSE
*
* Copyright (c) Intel Corporation.
* All rights reserved.
*
* 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 Intel Corporation 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 THE COPYRIGHT HOLDERS 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 THE COPYRIGHT
* OWNER 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 "spdk/stdinc.h"
#include "spdk_cunit.h"
#include "util/pipe.c"
#include "common/lib/test_env.c"
static void
test_create_destroy(void)
{
struct spdk_pipe *pipe;
uint8_t mem[10];
pipe = spdk_pipe_create(mem, sizeof(mem));
SPDK_CU_ASSERT_FATAL(pipe != NULL);
spdk_pipe_destroy(pipe);
}
static void
test_write_get_buffer(void)
{
struct spdk_pipe *pipe;
uint8_t mem[10];
struct iovec iovs[2];
int rc;
pipe = spdk_pipe_create(mem, sizeof(mem));
SPDK_CU_ASSERT_FATAL(pipe != NULL);
/* Get some available memory. */
rc = spdk_pipe_writer_get_buffer(pipe, 5, iovs);
CU_ASSERT(rc == 5);
CU_ASSERT(iovs[0].iov_base == mem);
CU_ASSERT(iovs[0].iov_len == 5);
CU_ASSERT(iovs[1].iov_base == NULL);
CU_ASSERT(iovs[1].iov_len == 0);
CU_ASSERT(pipe->write == 0);
CU_ASSERT(pipe->read == 0);
memset(iovs, 0, sizeof(iovs));
/* Get 0 bytes. */
rc = spdk_pipe_writer_get_buffer(pipe, 0, iovs);
CU_ASSERT(rc == 0);
CU_ASSERT(iovs[0].iov_base == NULL);
CU_ASSERT(iovs[0].iov_len == 0);
CU_ASSERT(iovs[1].iov_base == NULL);
CU_ASSERT(iovs[1].iov_len == 0);
CU_ASSERT(pipe->write == 0);
CU_ASSERT(pipe->read == 0);
memset(iovs, 0, sizeof(iovs));
/* Get all available memory */
rc = spdk_pipe_writer_get_buffer(pipe, 9, iovs);
CU_ASSERT(rc == 9);
CU_ASSERT(iovs[0].iov_base == mem);
CU_ASSERT(iovs[0].iov_len == 9);
CU_ASSERT(iovs[1].iov_base == NULL);
CU_ASSERT(iovs[1].iov_len == 0);
CU_ASSERT(pipe->write == 0);
CU_ASSERT(pipe->read == 0);
memset(iovs, 0, sizeof(iovs));
/* Get the full size of the data buffer backing the pipe, which isn't allowed */
rc = spdk_pipe_writer_get_buffer(pipe, 10, iovs);
CU_ASSERT(rc == 9);
CU_ASSERT(iovs[0].iov_base == mem);
CU_ASSERT(iovs[0].iov_len == 9);
CU_ASSERT(iovs[1].iov_base == NULL);
CU_ASSERT(iovs[1].iov_len == 0);
CU_ASSERT(pipe->write == 0);
CU_ASSERT(pipe->read == 0);
memset(iovs, 0, sizeof(iovs));
/* Advance the write pointer 7 bytes in. */
pipe->write = 7;
/* Get all of the available memory. */
rc = spdk_pipe_writer_get_buffer(pipe, 2, iovs);
CU_ASSERT(rc == 2);
CU_ASSERT(iovs[0].iov_base == (mem + 7));
CU_ASSERT(iovs[0].iov_len == 2);
CU_ASSERT(iovs[1].iov_base == NULL);
CU_ASSERT(iovs[1].iov_len == 0);
CU_ASSERT(pipe->write == 7);
CU_ASSERT(pipe->read == 0);
memset(iovs, 0, sizeof(iovs));
/* Get more than the available memory */
rc = spdk_pipe_writer_get_buffer(pipe, 3, iovs);
CU_ASSERT(rc == 2);
CU_ASSERT(iovs[0].iov_base == (mem + 7));
CU_ASSERT(iovs[0].iov_len == 2);
CU_ASSERT(iovs[1].iov_base == NULL);
CU_ASSERT(iovs[1].iov_len == 0);
CU_ASSERT(pipe->write == 7);
CU_ASSERT(pipe->read == 0);
memset(iovs, 0, sizeof(iovs));
/* Advance the read pointer 3 bytes in. */
pipe->read = 3;
/* Get all of the available memory. */
rc = spdk_pipe_writer_get_buffer(pipe, 5, iovs);
CU_ASSERT(rc == 5);
CU_ASSERT(iovs[0].iov_base == (mem + 7));
CU_ASSERT(iovs[0].iov_len == 3);
CU_ASSERT(iovs[1].iov_base == mem);
CU_ASSERT(iovs[1].iov_len == 2);
CU_ASSERT(pipe->write == 7);
CU_ASSERT(pipe->read == 3);
memset(iovs, 0, sizeof(iovs));
/* Get more than the available memory */
rc = spdk_pipe_writer_get_buffer(pipe, 6, iovs);
CU_ASSERT(rc == 5);
CU_ASSERT(iovs[0].iov_base == (mem + 7));
CU_ASSERT(iovs[0].iov_len == 3);
CU_ASSERT(iovs[1].iov_base == mem);
CU_ASSERT(iovs[1].iov_len == 2);
CU_ASSERT(pipe->write == 7);
CU_ASSERT(pipe->read == 3);
memset(iovs, 0, sizeof(iovs));
/* Advance the read pointer past the write pointer */
pipe->read = 9;
/* Get all of the available memory. */
rc = spdk_pipe_writer_get_buffer(pipe, 1, iovs);
CU_ASSERT(rc == 1);
CU_ASSERT(iovs[0].iov_base == (mem + 7));
CU_ASSERT(iovs[0].iov_len == 1);
CU_ASSERT(iovs[1].iov_base == NULL);
CU_ASSERT(iovs[1].iov_len == 0);
CU_ASSERT(pipe->write == 7);
CU_ASSERT(pipe->read == 9);
memset(iovs, 0, sizeof(iovs));
/* Get more than the available memory */
rc = spdk_pipe_writer_get_buffer(pipe, 2, iovs);
CU_ASSERT(rc == 1);
CU_ASSERT(iovs[0].iov_base == (mem + 7));
CU_ASSERT(iovs[0].iov_len == 1);
CU_ASSERT(iovs[1].iov_base == NULL);
CU_ASSERT(iovs[1].iov_len == 0);
CU_ASSERT(pipe->write == 7);
CU_ASSERT(pipe->read == 9);
memset(iovs, 0, sizeof(iovs));
/* Fill the pipe */
pipe->write = 8;
/* Get data while the pipe is full */
rc = spdk_pipe_writer_get_buffer(pipe, 1, iovs);
CU_ASSERT(rc == 0);
CU_ASSERT(iovs[0].iov_base == NULL);
CU_ASSERT(iovs[0].iov_len == 0);
CU_ASSERT(iovs[1].iov_base == NULL);
CU_ASSERT(iovs[1].iov_len == 0);
CU_ASSERT(pipe->write == 8);
CU_ASSERT(pipe->read == 9);
spdk_pipe_destroy(pipe);
}
static void
test_write_advance(void)
{
struct spdk_pipe *pipe;
uint8_t mem[10];
int rc;
pipe = spdk_pipe_create(mem, sizeof(mem));
SPDK_CU_ASSERT_FATAL(pipe != NULL);
/* Advance half way through the pipe */
rc = spdk_pipe_writer_advance(pipe, 5);
CU_ASSERT(rc == 0);
CU_ASSERT(pipe->write == 5);
CU_ASSERT(pipe->read == 0);
pipe->write = 0;
/* Advance to the end of the pipe */
rc = spdk_pipe_writer_advance(pipe, 9);
CU_ASSERT(rc == 0);
CU_ASSERT(pipe->write == 9);
CU_ASSERT(pipe->read == 0);
pipe->write = 0;
/* Advance beyond the end */
rc = spdk_pipe_writer_advance(pipe, 10);
CU_ASSERT(rc == -EINVAL);
CU_ASSERT(pipe->write == 0);
CU_ASSERT(pipe->read == 0);
/* Move the read pointer forward */
pipe->write = 0;
pipe->read = 5;
/* Advance to the end of the pipe */
rc = spdk_pipe_writer_advance(pipe, 4);
CU_ASSERT(rc == 0);
CU_ASSERT(pipe->write == 4);
CU_ASSERT(pipe->read == 5);
pipe->write = 0;
pipe->read = 5;
/* Advance beyond the end */
rc = spdk_pipe_writer_advance(pipe, 5);
CU_ASSERT(rc == -EINVAL);
CU_ASSERT(pipe->write == 0);
CU_ASSERT(pipe->read == 5);
/* Test wrap around */
pipe->write = 7;
pipe->read = 3;
/* Advance to the end of the pipe */
rc = spdk_pipe_writer_advance(pipe, 5);
CU_ASSERT(rc == 0);
CU_ASSERT(pipe->write == 2);
CU_ASSERT(pipe->read == 3);
pipe->write = 7;
pipe->read = 3;
/* Advance beyond the end */
rc = spdk_pipe_writer_advance(pipe, 6);
CU_ASSERT(rc == -EINVAL);
CU_ASSERT(pipe->write == 7);
CU_ASSERT(pipe->read == 3);
spdk_pipe_destroy(pipe);
}
static void
test_read_get_buffer(void)
{
struct spdk_pipe *pipe;
uint8_t mem[10];
struct iovec iovs[2];
int rc;
pipe = spdk_pipe_create(mem, sizeof(mem));
SPDK_CU_ASSERT_FATAL(pipe != NULL);
/* Set the write pointer to the end, making all data available. */
pipe->write = 9;
/* Get half the available memory. */
rc = spdk_pipe_reader_get_buffer(pipe, 5, iovs);
CU_ASSERT(rc == 5);
CU_ASSERT(iovs[0].iov_base == mem);
CU_ASSERT(iovs[0].iov_len == 5);
CU_ASSERT(iovs[1].iov_base == NULL);
CU_ASSERT(iovs[1].iov_len == 0);
CU_ASSERT(pipe->write == 9);
CU_ASSERT(pipe->read == 0);
memset(iovs, 0, sizeof(iovs));
/* Get 0 bytes. */
rc = spdk_pipe_reader_get_buffer(pipe, 0, iovs);
CU_ASSERT(rc == 0);
CU_ASSERT(iovs[0].iov_base == NULL);
CU_ASSERT(iovs[0].iov_len == 0);
CU_ASSERT(iovs[1].iov_base == NULL);
CU_ASSERT(iovs[1].iov_len == 0);
CU_ASSERT(pipe->write == 9);
CU_ASSERT(pipe->read == 0);
memset(iovs, 0, sizeof(iovs));
/* Get all available memory */
rc = spdk_pipe_reader_get_buffer(pipe, 9, iovs);
CU_ASSERT(rc == 9);
CU_ASSERT(iovs[0].iov_base == mem);
CU_ASSERT(iovs[0].iov_len == 9);
CU_ASSERT(iovs[1].iov_base == NULL);
CU_ASSERT(iovs[1].iov_len == 0);
CU_ASSERT(pipe->write == 9);
CU_ASSERT(pipe->read == 0);
memset(iovs, 0, sizeof(iovs));
/* Get more bytes than exist */
rc = spdk_pipe_reader_get_buffer(pipe, 10, iovs);
CU_ASSERT(rc == 9);
CU_ASSERT(iovs[0].iov_base == mem);
CU_ASSERT(iovs[0].iov_len == 9);
CU_ASSERT(iovs[1].iov_base == NULL);
CU_ASSERT(iovs[1].iov_len == 0);
CU_ASSERT(pipe->write == 9);
CU_ASSERT(pipe->read == 0);
memset(iovs, 0, sizeof(iovs));
/* Advance the read pointer 5 bytes in. */
pipe->read = 5;
pipe->write = 0;
/* Get all of the available memory. */
rc = spdk_pipe_reader_get_buffer(pipe, 5, iovs);
CU_ASSERT(rc == 5);
CU_ASSERT(iovs[0].iov_base == (mem + 5));
CU_ASSERT(iovs[0].iov_len == 5);
CU_ASSERT(iovs[1].iov_base == NULL);
CU_ASSERT(iovs[1].iov_len == 0);
CU_ASSERT(pipe->write == 0);
CU_ASSERT(pipe->read == 5);
memset(iovs, 0, sizeof(iovs));
/* Get more than the available memory */
rc = spdk_pipe_reader_get_buffer(pipe, 6, iovs);
CU_ASSERT(rc == 5);
CU_ASSERT(iovs[0].iov_base == (mem + 5));
CU_ASSERT(iovs[0].iov_len == 5);
CU_ASSERT(iovs[1].iov_base == NULL);
CU_ASSERT(iovs[1].iov_len == 0);
CU_ASSERT(pipe->write == 0);
CU_ASSERT(pipe->read == 5);
memset(iovs, 0, sizeof(iovs));
/* Invert the write and read pointers */
pipe->read = 7;
pipe->write = 3;
/* Get all of the available memory. */
rc = spdk_pipe_reader_get_buffer(pipe, 6, iovs);
CU_ASSERT(rc == 6);
CU_ASSERT(iovs[0].iov_base == (mem + 7));
CU_ASSERT(iovs[0].iov_len == 3);
CU_ASSERT(iovs[1].iov_base == mem);
CU_ASSERT(iovs[1].iov_len == 3);
CU_ASSERT(pipe->write == 3);
CU_ASSERT(pipe->read == 7);
memset(iovs, 0, sizeof(iovs));
/* Get more than the available memory */
rc = spdk_pipe_reader_get_buffer(pipe, 7, iovs);
CU_ASSERT(rc == 6);
CU_ASSERT(iovs[0].iov_base == (mem + 7));
CU_ASSERT(iovs[0].iov_len == 3);
CU_ASSERT(iovs[1].iov_base == mem);
CU_ASSERT(iovs[1].iov_len == 3);
CU_ASSERT(pipe->write == 3);
CU_ASSERT(pipe->read == 7);
memset(iovs, 0, sizeof(iovs));
/* Empty the pipe */
pipe->read = 8;
pipe->write = 8;
/* Get data while the pipe is empty */
rc = spdk_pipe_reader_get_buffer(pipe, 1, iovs);
CU_ASSERT(rc == 0);
CU_ASSERT(iovs[0].iov_base == NULL);
CU_ASSERT(iovs[0].iov_len == 0);
CU_ASSERT(iovs[1].iov_base == NULL);
CU_ASSERT(iovs[1].iov_len == 0);
CU_ASSERT(pipe->write == 8);
CU_ASSERT(pipe->read == 8);
spdk_pipe_destroy(pipe);
}
static void
test_read_advance(void)
{
struct spdk_pipe *pipe;
uint8_t mem[10];
int rc;
pipe = spdk_pipe_create(mem, sizeof(mem));
SPDK_CU_ASSERT_FATAL(pipe != NULL);
pipe->read = 0;
pipe->write = 9;
/* Advance half way through the pipe */
rc = spdk_pipe_reader_advance(pipe, 5);
CU_ASSERT(rc == 0);
CU_ASSERT(pipe->read == 5);
CU_ASSERT(pipe->write == 9);
pipe->read = 0;
pipe->write = 9;
/* Advance to the end of the pipe */
rc = spdk_pipe_reader_advance(pipe, 9);
CU_ASSERT(rc == 0);
CU_ASSERT(pipe->read == 9);
CU_ASSERT(pipe->write == 9);
pipe->read = 0;
pipe->write = 9;
/* Advance beyond the end */
rc = spdk_pipe_reader_advance(pipe, 10);
CU_ASSERT(rc == -EINVAL);
CU_ASSERT(pipe->read == 0);
CU_ASSERT(pipe->write == 9);
/* Move the write pointer forward */
pipe->read = 0;
pipe->write = 5;
/* Advance to the end of the pipe */
rc = spdk_pipe_reader_advance(pipe, 5);
CU_ASSERT(rc == 0);
CU_ASSERT(pipe->write == 5);
CU_ASSERT(pipe->read == 5);
pipe->read = 0;
pipe->write = 5;
/* Advance beyond the end */
rc = spdk_pipe_reader_advance(pipe, 6);
CU_ASSERT(rc == -EINVAL);
CU_ASSERT(pipe->read == 0);
CU_ASSERT(pipe->write == 5);
/* Test wrap around */
pipe->read = 7;
pipe->write = 3;
/* Advance to the end of the pipe */
rc = spdk_pipe_reader_advance(pipe, 6);
CU_ASSERT(rc == 0);
CU_ASSERT(pipe->read == 3);
CU_ASSERT(pipe->write == 3);
pipe->read = 7;
pipe->write = 3;
/* Advance beyond the end */
rc = spdk_pipe_writer_advance(pipe, 7);
CU_ASSERT(rc == -EINVAL);
CU_ASSERT(pipe->read == 7);
CU_ASSERT(pipe->write == 3);
spdk_pipe_destroy(pipe);
}
static void
test_data(void)
{
struct spdk_pipe *pipe;
uint8_t mem[10];
struct iovec iovs[2];
uint8_t *data;
int rc;
size_t i;
memset(mem, 0, sizeof(mem));
memset(iovs, 0, sizeof(iovs));
pipe = spdk_pipe_create(mem, sizeof(mem));
SPDK_CU_ASSERT_FATAL(pipe != NULL);
/* Place 1 byte in the pipe */
rc = spdk_pipe_writer_get_buffer(pipe, 1, iovs);
CU_ASSERT(rc == 1);
CU_ASSERT(iovs[0].iov_base != NULL);
CU_ASSERT(iovs[0].iov_len == 1);
memset(iovs[0].iov_base, 'A', 1);
rc = spdk_pipe_writer_advance(pipe, 1);
CU_ASSERT(rc == 0);
CU_ASSERT(mem[0] == 'A');
CU_ASSERT(mem[1] == 0);
CU_ASSERT(mem[2] == 0);
CU_ASSERT(mem[3] == 0);
CU_ASSERT(mem[4] == 0);
CU_ASSERT(mem[5] == 0);
CU_ASSERT(mem[6] == 0);
CU_ASSERT(mem[7] == 0);
CU_ASSERT(mem[8] == 0);
CU_ASSERT(mem[9] == 0);
memset(iovs, 0, sizeof(iovs));
/* Get 1 byte from the pipe */
CU_ASSERT(spdk_pipe_reader_bytes_available(pipe) == 1);
rc = spdk_pipe_reader_get_buffer(pipe, 10, iovs);
CU_ASSERT(rc == 1);
data = iovs[0].iov_base;
CU_ASSERT(*data = 'A');
spdk_pipe_reader_advance(pipe, 1);
/* Put 9 more bytes in the pipe, so every byte has
* been written */
rc = spdk_pipe_writer_get_buffer(pipe, 9, iovs);
CU_ASSERT(rc == 9);
CU_ASSERT(iovs[0].iov_len == 9);
CU_ASSERT(iovs[1].iov_len == 0);
memset(iovs[0].iov_base, 'B', iovs[0].iov_len);
rc = spdk_pipe_writer_advance(pipe, 9);
CU_ASSERT(rc == 0);
CU_ASSERT(mem[0] == 'A');
CU_ASSERT(mem[1] == 'B');
CU_ASSERT(mem[2] == 'B');
CU_ASSERT(mem[3] == 'B');
CU_ASSERT(mem[4] == 'B');
CU_ASSERT(mem[5] == 'B');
CU_ASSERT(mem[6] == 'B');
CU_ASSERT(mem[7] == 'B');
CU_ASSERT(mem[8] == 'B');
CU_ASSERT(mem[9] == 'B');
memset(iovs, 0, sizeof(iovs));
/* Get 7 bytes of the previously written 9. */
CU_ASSERT(spdk_pipe_reader_bytes_available(pipe) == 9);
rc = spdk_pipe_reader_get_buffer(pipe, 7, iovs);
CU_ASSERT(rc == 7);
CU_ASSERT(iovs[0].iov_len == 7);
data = iovs[0].iov_base;
for (i = 0; i < iovs[0].iov_len; i++) {
CU_ASSERT(data[i] == 'B');
}
spdk_pipe_reader_advance(pipe, 7);
memset(iovs, 0, sizeof(iovs));
/* Put 1 more byte in the pipe, overwriting the original 'A' */
rc = spdk_pipe_writer_get_buffer(pipe, 1, iovs);
CU_ASSERT(rc == 1);
CU_ASSERT(iovs[0].iov_len == 1);
CU_ASSERT(iovs[1].iov_len == 0);
memset(iovs[0].iov_base, 'C', iovs[0].iov_len);
rc = spdk_pipe_writer_advance(pipe, 1);
CU_ASSERT(rc == 0);
CU_ASSERT(mem[0] == 'C');
CU_ASSERT(mem[1] == 'B');
CU_ASSERT(mem[2] == 'B');
CU_ASSERT(mem[3] == 'B');
CU_ASSERT(mem[4] == 'B');
CU_ASSERT(mem[5] == 'B');
CU_ASSERT(mem[6] == 'B');
CU_ASSERT(mem[7] == 'B');
CU_ASSERT(mem[8] == 'B');
CU_ASSERT(mem[9] == 'B');
memset(iovs, 0, sizeof(iovs));
/* Get all of the data out of the pipe */
CU_ASSERT(spdk_pipe_reader_bytes_available(pipe) == 3);
rc = spdk_pipe_reader_get_buffer(pipe, 3, iovs);
CU_ASSERT(rc == 3);
CU_ASSERT(iovs[0].iov_len == 2);
CU_ASSERT(iovs[1].iov_len == 1);
data = iovs[0].iov_base;
CU_ASSERT(data[0] == 'B');
CU_ASSERT(data[1] == 'B');
data = iovs[1].iov_base;
CU_ASSERT(data[0] == 'C');
spdk_pipe_reader_advance(pipe, 3);
spdk_pipe_destroy(pipe);
}
int
main(int argc, char **argv)
{
CU_pSuite suite = NULL;
unsigned int num_failures;
if (CU_initialize_registry() != CUE_SUCCESS) {
return CU_get_error();
}
suite = CU_add_suite("pipe", NULL, NULL);
if (suite == NULL) {
CU_cleanup_registry();
return CU_get_error();
}
if (
CU_add_test(suite, "test_create_destroy", test_create_destroy) == NULL ||
CU_add_test(suite, "test_write_get_buffer", test_write_get_buffer) == NULL ||
CU_add_test(suite, "test_write_advance", test_write_advance) == NULL ||
CU_add_test(suite, "test_read_get_buffer", test_read_get_buffer) == NULL ||
CU_add_test(suite, "test_read_advance", test_read_advance) == NULL ||
CU_add_test(suite, "test_data", test_data) == NULL) {
CU_cleanup_registry();
return CU_get_error();
}
CU_basic_set_mode(CU_BRM_VERBOSE);
CU_basic_run_tests();
num_failures = CU_get_number_of_failures();
CU_cleanup_registry();
return num_failures;
}

View File

@ -155,6 +155,7 @@ $valgrind $testdir/lib/util/crc32_ieee.c/crc32_ieee_ut
$valgrind $testdir/lib/util/crc32c.c/crc32c_ut
$valgrind $testdir/lib/util/string.c/string_ut
$valgrind $testdir/lib/util/dif.c/dif_ut
$valgrind $testdir/lib/util/pipe.c/pipe_ut
if [ $(uname -s) = Linux ]; then
$valgrind $testdir/lib/vhost/vhost.c/vhost_ut