On Fri, Aug 08, 2025 at 04:01:14PM +0200, Laurent Vivier wrote:
These utilities enhance iov_tail manipulation, useful for efficient packet processing by enabling iovec array cloning and header stripping without data copies.
- iov_tail_drop(): 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
Reviewed-by: David Gibson
--- iov.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ iov.h | 3 +++ 2 files changed, 55 insertions(+)
diff --git a/iov.c b/iov.c index edf0444d1955..9d282d4af461 100644 --- a/iov.c +++ b/iov.c @@ -192,6 +192,21 @@ size_t iov_tail_size(struct iov_tail *tail) return iov_size(tail->iov, tail->cnt) - tail->off; }
+/** + * iov_tail_drop() - 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_tail_drop(struct iov_tail *tail, size_t len)
I'd prefer the name iov_drop_header(), to match iov_peek_header() and iov_remove_header().
+{ + 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 @@ -248,3 +263,40 @@ void *iov_remove_header_(struct iov_tail *tail, size_t len, size_t align) tail->off = tail->off + len; return p; } + +/** + * iov_tail_clone() - Assign iov references referencing a subset of the data + * in an iov_tail
I find that short description very hard to parse. Maybe 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);
As noted several times previously we can clean up this and a number of other places if we clarify that always being pruned is part of an iov_tail's invariant.
+
At this point we can already test if dst_iov_cnt is long enough, which I think would be a little cleaner than counting through it and only then finding it's not enough.
+ /* 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; +} diff --git a/iov.h b/iov.h index 3fc96ab9755a..bf9820ac52ab 100644 --- a/iov.h +++ b/iov.h @@ -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_tail_drop(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
-- David Gibson (he or they) | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you, not the other way | around. http://www.ozlabs.org/~dgibson