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=202504 header.b=OXQi+J28; dkim-atps=neutral Received: from mail.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id 4B58A5A0275 for ; Wed, 16 Apr 2025 07:55:26 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202504; t=1744782922; bh=+mSGuaRynqJCipp/NIiGV6cgWfbGCw0oDHsCGU3nhNc=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=OXQi+J28HJOgXny8+gqWH0ycZ+OjDQZKgYtTR6dU0mUuUqirf95D7Oc0uMlW9lWej SWiW6903g+ncxPphlfvaKUd6cX+dP6oF+fp1cUv1edB05O30rLrWWkTlj9bnO2I3d9 3GzO/NKBm2x1/trhdqIpH7Rh7b+hIG+2OjV3gocoxDR+MeRP+0Pcv1y5KPORZKB41G ABUPAzI5JA470lIhquhPOEEd1QBPF+e31LVqfyP39Lk3wzWF9poDnfRl+8xUYBRYEs jXL2lLuuk6eP8JfOiXq3878x9XLHNt5sJYdwvaIrqhCNdwmBQ1ohnRdoZK+7fZq8nm ndhV2tn5idj9g== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4Zcqxk343sz4xPl; Wed, 16 Apr 2025 15:55:22 +1000 (AEST) Date: Wed, 16 Apr 2025 13:05:06 +1000 From: David Gibson To: Laurent Vivier Subject: Re: [PATCH v3 03/20] iov: Update IOV_REMOVE_HEADER() and IOV_PEEK_HEADER() Message-ID: References: <20250415094300.65794-1-lvivier@redhat.com> <20250415094300.65794-4-lvivier@redhat.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="pADdU5M1eHEx3lW4" Content-Disposition: inline In-Reply-To: <20250415094300.65794-4-lvivier@redhat.com> Message-ID-Hash: CCDA3HN4MC5TT5KPUHHQYSHMMKH57JG4 X-Message-ID-Hash: CCDA3HN4MC5TT5KPUHHQYSHMMKH57JG4 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: passt-dev@passt.top 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: --pADdU5M1eHEx3lW4 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Tue, Apr 15, 2025 at 11:42:43AM +0200, Laurent Vivier wrote: > Provide a temporary variable of the wanted type to store > the header if the memory in the iovec array is not contiguous. >=20 > Signed-off-by: Laurent Vivier Reviewed-by: David Gibson Although... > --- > iov.c | 53 ++++++++++++++++++++++++++++++++++++++++++++--------- > iov.h | 52 ++++++++++++++++++++++++++++++++++++++-------------- > tcp_buf.c | 2 +- > 3 files changed, 83 insertions(+), 24 deletions(-) >=20 > diff --git a/iov.c b/iov.c > index 60ab1f9af973..6ae792a1bf8f 100644 > --- a/iov.c > +++ b/iov.c > @@ -108,7 +108,7 @@ size_t iov_from_buf(const struct iovec *iov, size_t i= ov_cnt, > * > * Returns: The number of bytes successfully copied. > */ > -/* cppcheck-suppress unusedFunction */ > +/* cppcheck-suppress [staticFunction] */ > size_t iov_to_buf(const struct iovec *iov, size_t iov_cnt, > size_t offset, void *buf, size_t bytes) > { > @@ -126,6 +126,7 @@ size_t iov_to_buf(const struct iovec *iov, size_t iov= _cnt, > /* copying data */ > for (copied =3D 0; copied < bytes && i < iov_cnt; i++) { > size_t len =3D MIN(iov[i].iov_len - offset, bytes - copied); > + /* NOLINTNEXTLINE(clang-analyzer-core.NonNullParamChecker) */ > memcpy((char *)buf + copied, (char *)iov[i].iov_base + offset, =2E.. it would be nice to have a clearer idea what's going on with this lin= t. > len); > copied +=3D len; > @@ -260,7 +261,7 @@ bool iov_tail_drop(struct iov_tail *tail, size_t len) > } > =20 > /** > - * iov_peek_header_() - Get pointer to a header from an IOV tail > + * iov_check_header() - Check if a header can be accessed > * @tail: IOV tail to get header from > * @len: Length of header to get, in bytes > * @align: Required alignment of header, in bytes > @@ -271,8 +272,7 @@ bool iov_tail_drop(struct iov_tail *tail, size_t len) > * overruns the IO vector, is not contiguous or doesn't have the > * requested alignment. > */ > -/* cppcheck-suppress [staticFunction,unmatchedSuppression] */ > -void *iov_peek_header_(struct iov_tail *tail, size_t len, size_t align) > +static void *iov_check_header(struct iov_tail *tail, size_t len, size_t = align) > { > char *p; > =20 > @@ -292,27 +292,62 @@ void *iov_peek_header_(struct iov_tail *tail, size_= t len, size_t align) > return p; > } > =20 > +/** > + * iov_peek_header_() - Get pointer to a header from an IOV tail > + * @tail: IOV tail to get header from > + * @v: Temporary memory to use if the memory in @tail > + * is discontinuous > + * @len: Length of header to get, in bytes > + * @align: Required alignment of header, in bytes > + * > + * @tail may be pruned, but will represent the same bytes as before. > + * > + * Returns: Pointer to the first @len logical bytes of the tail, or to > + * a copy if that overruns the IO vector, is not contiguous or > + * doesn't have the requested alignment. NULL if that overruns = the > + * IO vector. > + */ > +/* cppcheck-suppress [staticFunction,unmatchedSuppression] */ > +void *iov_peek_header_(struct iov_tail *tail, void *v, size_t len, size_= t align) > +{ > + char *p =3D iov_check_header(tail, len, align); > + size_t l; > + > + if (p) > + return p; > + > + l =3D iov_to_buf(tail->iov, tail->cnt, tail->off, v, len); > + if (l !=3D len) > + return NULL; > + > + return v; > +} > + > /** > * iov_remove_header_() - Remove a header from an IOV tail > * @tail: IOV tail to remove header from (modified) > + * @v: Temporary memory to use if the memory in @tail > + * is discontinuous > * @len: Length of header to remove, in bytes > * @align: Required alignment of header, in bytes > * > * On success, @tail is updated so that it longer includes the bytes of = the > * returned header. > * > - * Returns: Pointer to the first @len logical bytes of the tail, NULL if= that > - * overruns the IO vector, is not contiguous or doesn't have the > - * requested alignment. > + * Returns: Pointer to the first @len logical bytes of the tail, or to > + * a copy if that overruns the IO vector, is not contiguous or > + * doesn't have the requested alignment. NULL if that overruns = the > + * IO vector. > */ > -void *iov_remove_header_(struct iov_tail *tail, size_t len, size_t align) > +void *iov_remove_header_(struct iov_tail *tail, void *v, size_t len, siz= e_t align) > { > - char *p =3D iov_peek_header_(tail, len, align); > + char *p =3D iov_peek_header_(tail, v, len, align); > =20 > if (!p) > return NULL; > =20 > tail->off =3D tail->off + len; > + > return p; > } > =20 > diff --git a/iov.h b/iov.h > index 4bbdbf23f27b..03c85bd1a465 100644 > --- a/iov.h > +++ b/iov.h > @@ -73,41 +73,65 @@ struct iov_tail { > #define IOV_TAIL(iov_, cnt_, off_) \ > (struct iov_tail){ .iov =3D (iov_), .cnt =3D (cnt_), .off =3D (off_) } > =20 > +/** > + * IOV_TAIL_FROM_BUF() - Create a new IOV tail from a buffer > + * @buf_: Buffer address to use in the iovec > + * @len_: Buffer size > + * @off_: Byte offset in the buffer where the tail begins > + */ > +#define IOV_TAIL_FROM_BUF(buf_, len_, off_) \ > + IOV_TAIL((&(const struct iovec){ .iov_base =3D (buf_), .iov_len =3D (le= n_) }), 1, (off_)) > + > 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= ); > +void *iov_peek_header_(struct iov_tail *tail, void *v, size_t len, size_= t align); > +void *iov_remove_header_(struct iov_tail *tail, void *v, size_t len, siz= e_t align); > int iov_tail_slice(struct iovec *dst_iov, size_t dst_iov_cnt, > struct iov_tail *tail, size_t *bytes); > =20 > /** > * IOV_PEEK_HEADER() - Get typed pointer to a header from an IOV tail > * @tail_: IOV tail to get header from > - * @type_: Data type of the header > + * @var_: Temporary buffer of the type of the header to use if > + * the memory in the iovec array is not contiguous. > * > * @tail_ may be pruned, but will represent the same bytes as before. > * > - * Returns: Pointer of type (@type_ *) located at the start of @tail_, N= ULL if > - * we can't get a contiguous and aligned pointer. > + * Returns: Pointer of type (@type_ *) located at the start of @tail_ > + * or to @var_ if iovec memory is not contiguous, NULL if > + * that overruns the iovec. > */ > -#define IOV_PEEK_HEADER(tail_, type_) \ > - ((type_ *)(iov_peek_header_((tail_), \ > - sizeof(type_), __alignof__(type_)))) > + > +#define IOV_PEEK_HEADER(tail_, var_) \ > + ((__typeof__(var_) *)(iov_peek_header_((tail_), &(var_), \ > + sizeof(var_), __alignof__(var_)))) > =20 > /** > * IOV_REMOVE_HEADER() - Remove and return typed header from an IOV tail > * @tail_: IOV tail to remove header from (modified) > - * @type_: Data type of the header to remove > + * @var_: Temporary buffer of the type of the header to use if > + * the memory in the iovec array is not contiguous. > * > * On success, @tail_ is updated so that it longer includes the bytes of= the > * returned header. > * > - * Returns: Pointer of type (@type_ *) located at the old start of @tail= _, NULL > - * if we can't get a contiguous and aligned pointer. > + * Returns: Pointer of type (@type_ *) located at the start of @tail_ > + * or to @var_ if iovec memory is not contiguous, NULL if > + * that overruns the iovec. > + */ > + > +#define IOV_REMOVE_HEADER(tail_, var_) \ > + ((__typeof__(var_) *)(iov_remove_header_((tail_), &(var_), \ > + sizeof(var_), __alignof__(var_)))) > + > +/** IOV_DROP_HEADER() -- Remove a typed header from an IOV tail > + * @tail_: IOV tail to remove header from (modified) > + * @type_: Data type of the header to remove > + * > + * Returns: true if the tail still contains any bytes, otherwise false > */ > -#define IOV_REMOVE_HEADER(tail_, type_) \ > - ((type_ *)(iov_remove_header_((tail_), \ > - sizeof(type_), __alignof__(type_)))) > +#define IOV_DROP_HEADER(tail_, type_) \ > + iov_tail_drop((tail_), sizeof(type_)) > =20 > #endif /* IOVEC_H */ > diff --git a/tcp_buf.c b/tcp_buf.c > index 05305636b503..4bcc1acb245a 100644 > --- a/tcp_buf.c > +++ b/tcp_buf.c > @@ -160,7 +160,7 @@ static void tcp_l2_buf_fill_headers(const struct tcp_= tap_conn *conn, > uint32_t seq, bool no_tcp_csum) > { > struct iov_tail tail =3D IOV_TAIL(&iov[TCP_IOV_PAYLOAD], 1, 0); > - struct tcphdr *th =3D IOV_REMOVE_HEADER(&tail, struct tcphdr); > + struct tcphdr thc, *th =3D IOV_REMOVE_HEADER(&tail, thc); > struct tap_hdr *taph =3D iov[TCP_IOV_TAP].iov_base; > const struct flowside *tapside =3D TAPFLOW(conn); > const struct in_addr *a4 =3D inany_v4(&tapside->oaddr); --=20 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 --pADdU5M1eHEx3lW4 Content-Type: application/pgp-signature; name=signature.asc -----BEGIN PGP SIGNATURE----- iQIzBAEBCAAdFiEEO+dNsU4E3yXUXRK2zQJF27ox2GcFAmf/HmIACgkQzQJF27ox 2GfFRQ/+OspBTiDPdL/OKnuwYz4uckRaIzq6mIq4NKAteSxA5/RrvoFWlkB3JjWk kYMADAO3PEuAnNWfzMURCkrOVzBAhEPNRro3KpUpZPlI8X6SJMwrgzR/jz3o+7fo fZr8X5C0o0filIVezQUhcT2+LXMjyv8iuBgdV06dRsU/Je0oTYoosSJxcMPLMDjk LzIMKRf4tdM/QpZHXXf0deVO4xPeqVfXUSOm/zbPD/VS97obxIwDsNACfn7D+E5z ftCVjjXP2fxTy8OY38RpqnQCuivj85poxtNY/eeGlcrp0mgesZQJtxmbUBRjGKuo 9YPfIvck0N8rRz80JFiofUcjC4m7xwFT8wjul1WbLefsQi8q1raiR40P0N9kpQVB RJi/Xp4RKLM+q2i8DxcW9VvWAcguliZ6SHnjQPaqGQ3K2DDGslkVW7CRQw75ZJ3h DtcTUSVYhzm6l+h3E2yzElcSrCUGnTdQvG9XgofWRTw6lWUkWDttjIZ5CAc5kU31 Rnp5Y0zvIPZs+5LSZBc5suWueZ425t0fXDqjRmX/59vkMMeFn3EkkbKHsMU8vOou tlP6hsNd5fpQdaukCr4F3nRMFyks17pa5mZwEgJCJJ0PYPwFMsUZz5EdSE2nxyor 3BAUyIskwg5jOE1R6wGS049uv/VvCDoocdl7t/+Jq+fBYNs7fsU= =b0ec -----END PGP SIGNATURE----- --pADdU5M1eHEx3lW4--