diff options
author | Laurent Vivier <lvivier@redhat.com> | 2025-09-02 09:52:25 +0200 |
---|---|---|
committer | Stefano Brivio <sbrivio@redhat.com> | 2025-09-03 20:42:15 +0200 |
commit | 066e6b1dc52a5f37feb6915fbda080eb29e042f3 (patch) | |
tree | 345de74783891a572a032e1e5a02e39576a743a4 | |
parent | ea3dd28b546104c6bd4b4a4156d32d4f57b3d0cb (diff) | |
download | passt-066e6b1dc52a5f37feb6915fbda080eb29e042f3.tar passt-066e6b1dc52a5f37feb6915fbda080eb29e042f3.tar.gz passt-066e6b1dc52a5f37feb6915fbda080eb29e042f3.tar.bz2 passt-066e6b1dc52a5f37feb6915fbda080eb29e042f3.tar.lz passt-066e6b1dc52a5f37feb6915fbda080eb29e042f3.tar.xz passt-066e6b1dc52a5f37feb6915fbda080eb29e042f3.tar.zst passt-066e6b1dc52a5f37feb6915fbda080eb29e042f3.zip |
iov: Introduce iov_tail_clone() and iov_drop_header().
These utilities enhance iov_tail manipulation, useful for
efficient packet processing by enabling iovec array cloning and
header stripping without data copies.
- iov_drop_header(): Discards a specified number of bytes from the
beginning of an iov_tail by advancing its internal offset and pruning
consumed elements.
- iov_tail_clone(): Clone an iov_tail into an iovec array, adjusting the
first iovec entry to remove the iov_tail offset.
Signed-off-by: Laurent Vivier <lvivier@redhat.com>
Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
-rw-r--r-- | iov.c | 51 | ||||
-rw-r--r-- | iov.h | 3 |
2 files changed, 54 insertions, 0 deletions
@@ -193,6 +193,21 @@ size_t iov_tail_size(struct iov_tail *tail) } /** + * iov_drop_header() - Discard a header from an IOV tail + * @tail: IO vector tail + * @len: length to move the head of the tail + * + * Return: true if the item still contains any bytes, otherwise false + */ +/* cppcheck-suppress unusedFunction */ +bool iov_drop_header(struct iov_tail *tail, size_t len) +{ + tail->off = tail->off + len; + + return iov_tail_prune(tail); +} + +/** * iov_peek_header_() - Get pointer to a header from an IOV tail * @tail: IOV tail to get header from * @len: Length of header to get, in bytes @@ -248,3 +263,39 @@ void *iov_remove_header_(struct iov_tail *tail, size_t len, size_t align) tail->off = tail->off + len; return p; } + +/** + * iov_tail_clone() - Clone an iov tail into a new iovec array + * + * @dst_iov: Pointer to the destination array of struct iovec describing + * the scatter/gather I/O vector to shallow copy to. + * @dst_iov_cnt: Maximum number of elements in the destination iov array. + * @tail: Pointer to the source iov_tail + * + * Return: the number of elements successfully referenced from the destination + * iov array, a negative value if there is not enough room in the + * destination iov array + */ +/* cppcheck-suppress unusedFunction */ +ssize_t iov_tail_clone(struct iovec *dst_iov, size_t dst_iov_cnt, + struct iov_tail *tail) +{ + const struct iovec *iov = &tail->iov[0]; + size_t iov_cnt = tail->cnt; + size_t offset = tail->off; + unsigned int i, j; + + i = iov_skip_bytes(iov, iov_cnt, offset, &offset); + + /* assign iov references referencing a subset of the source one */ + for (j = 0; i < iov_cnt && j < dst_iov_cnt; i++, j++) { + dst_iov[j].iov_base = (char *)iov[i].iov_base + offset; + dst_iov[j].iov_len = iov[i].iov_len - offset; + offset = 0; + } + + if (j == dst_iov_cnt && i != iov_cnt) + return -1; + + return j; +} @@ -72,8 +72,11 @@ struct iov_tail { bool iov_tail_prune(struct iov_tail *tail); size_t iov_tail_size(struct iov_tail *tail); +bool iov_drop_header(struct iov_tail *tail, size_t len); void *iov_peek_header_(struct iov_tail *tail, size_t len, size_t align); void *iov_remove_header_(struct iov_tail *tail, size_t len, size_t align); +ssize_t iov_tail_clone(struct iovec *dst_iov, size_t dst_iov_cnt, + struct iov_tail *tail); /** * IOV_PEEK_HEADER() - Get typed pointer to a header from an IOV tail |