From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: passt.top; dmarc=none (p=none dis=none) header.from=gibson.dropbear.id.au Authentication-Results: passt.top; dkim=pass (2048-bit key; secure) header.d=gibson.dropbear.id.au header.i=@gibson.dropbear.id.au header.a=rsa-sha256 header.s=202410 header.b=Ob5Lp+rk; dkim-atps=neutral Received: from mail.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id 795265A004C for ; Tue, 05 Nov 2024 03:32:34 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202410; t=1730773941; bh=uRFghWeI6h0NsJK7BYIebFBcsyXGB7bOli8bShF0bto=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Ob5Lp+rk0iwxFiKNISU52qsrH5wzKqmIysElA7RtaVgJQWelwWMC4PV4+L4CCL0/B eWedqGRbEqK/XWuztGxwrEgwOm7OVY6ToY83+/YkSoJQPWD7HcMhvUAqqXaXJV1+ro L57GlZnvCJ0uFV8F0lcGjueJTaXY8jtImA7bN5N/qGeoBboj28JV85FzAlTxy7tfq7 d2GpHcjQ3nteTi3SorGKtK57qU8JpNWoOZSE1lsHiy+qDDX31yZ1fOvBqMwdHzGMgE oFTD3+3L+1BTCL1KeSSiqKkrMl5tGE1kSvyx8iSkt4pGx+suNZb+Qe95ppshMrXlm3 qgmsCpw3C4Qwg== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4XjC6F6tbgz4x8d; Tue, 5 Nov 2024 13:32:21 +1100 (AEDT) From: David Gibson To: Stefano Brivio , passt-dev@passt.top Subject: [PATCH 1/1] iov: iov tail helpers Date: Tue, 5 Nov 2024 13:32:22 +1100 Message-ID: <20241105023222.698658-2-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241105023222.698658-1-david@gibson.dropbear.id.au> References: <20241105023222.698658-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: BNXUW4HD26CQ4JJOPIEDX4BK2EYSQTPJ X-Message-ID-Hash: BNXUW4HD26CQ4JJOPIEDX4BK2EYSQTPJ X-MailFrom: dgibson@gandalf.ozlabs.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: David Gibson X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: In the vhost-user code we have a number of places where we need to locate a particular header within the guest-supplied IO vector. We need to work out which buffer the header is in, and verify that it's contiguous and aligned as we need. At the moment this is open-coded, but introduce a helper to make this more straightforward. We add a new datatype 'struct iov_tail' representing an IO vector from which we've logically consumed some number of headers. The IOV_PULL_HEADER macro consumes a new header from the vector, returning a pointer and updating the iov_tail. Signed-off-by: David Gibson --- iov.c | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ iov.h | 24 +++++++++++++++++ 2 files changed, 107 insertions(+) diff --git a/iov.c b/iov.c index 3f9e229..3d384ae 100644 --- a/iov.c +++ b/iov.c @@ -156,3 +156,86 @@ size_t iov_size(const struct iovec *iov, size_t iov_cnt) return len; } + +/** + * iov_tail_shorten() - Remove any buffers from an IOV tail that are wholly consumed + * @tail: IO vector tail (modified) + * + * Return: true if the tail still contains any bytes, otherwise false + */ +bool iov_tail_shorten(struct iov_tail *tail) +{ + size_t i; + + i = iov_skip_bytes(tail->iov, tail->cnt, tail->off, &tail->off); + tail->iov += i; + tail->cnt -= i; + + return !!tail->cnt; +} + +/** + * iov_tail_size - Calculate the total size of an IO vector tail + * @tail: IO vector tail + * + * Returns: The total size in bytes. + */ +/* cppcheck-suppress unusedFunction */ +size_t iov_tail_size(struct iov_tail *tail) +{ + iov_tail_shorten(tail); + return iov_size(tail->iov, tail->cnt) - tail->off; +} + +/** + * iov_peek_header_() - Get pointer to header from an IOV tail + * @tail: IO vector tail to get header from + * @len: Length of header to remove in bytes + * @align: Required alignment of header in bytes + * + * @tail may be modified, but will be semantically equivalent. + * + * Returns: Pointer to the removed header, NULL if it overruns the IO + * vector, is not contiguous or is misaligned. + */ +void *iov_peek_header_(struct iov_tail *tail, size_t len, size_t align) +{ + char *p; + + if (!iov_tail_shorten(tail)) + return NULL; /* Nothing left */ + + if (tail->off + len < tail->off) + return NULL; /* Overflow */ + + if (tail->off + len > tail->iov[0].iov_len) + return NULL; /* Not contiguous */ + + p = (char *)tail->iov[0].iov_base + tail->off; + if ((uintptr_t)p % align) + return NULL; /* not aligned */ + + return p; +} +/** + * iov_pull_header_() - Remove a header from an IOV tail + * @tail: IO vector tail to remove header from (modified) + * @len: Length of header to remove in bytes + * @align: Required alignment of header in bytes + * + * @tail is updated so that it no longer includes the extracted header + * + * Returns: Pointer to the removed header, NULL if it overruns the IO + * vector, is not contiguous or is misaligned. + */ +/* cppcheck-suppress unusedFunction */ +void *iov_pull_header_(struct iov_tail *tail, size_t len, size_t align) +{ + char *p = iov_peek_header_(tail, len, align); + + if (!p) + return NULL; + + tail->off = tail->off + len; + return p; +} diff --git a/iov.h b/iov.h index a9e1722..a2f449c 100644 --- a/iov.h +++ b/iov.h @@ -28,4 +28,28 @@ size_t iov_from_buf(const struct iovec *iov, size_t iov_cnt, size_t iov_to_buf(const struct iovec *iov, size_t iov_cnt, size_t offset, void *buf, size_t bytes); size_t iov_size(const struct iovec *iov, size_t iov_cnt); + +/** + * struct iov_tail - Represents the fail portion of an IO vector + * @iov: IO vector + * @cnt: Number of entries in @iov + * @off: Current offset in @iov + */ +struct iov_tail { + const struct iovec *iov; + size_t cnt, off; +}; + +#define IOV_TAIL(iov_, cnt_, off_) \ + (struct iov_tail){ .iov = (iov_), .cnt = (cnt_), .off = (off_) } + +bool iov_tail_shorten(struct iov_tail *tail); +size_t iov_tail_size(struct iov_tail *tail); +void *iov_peek_header_(struct iov_tail *tail, size_t len, size_t align); +#define IOV_PEEK_HEADER(tail_, ty_) \ + ((ty_ *)(iov_peek_header_((tail_), sizeof(ty_), __alignof__(ty_)))) +void *iov_pull_header_(struct iov_tail *tail, size_t len, size_t align); +#define IOV_PULL_HEADER(tail_, ty_) \ + ((ty_ *)(iov_pull_header_((tail_), sizeof(ty_), __alignof__(ty_)))) + #endif /* IOVEC_H */ -- 2.47.0