From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from gandalf.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) by passt.top (Postfix) with ESMTPS id 9E5A05A0275 for ; Mon, 5 Feb 2024 07:13:48 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202312; t=1707113625; bh=3lxgJQML3NDYo+DSQc8BRFisfJBPeUy4UINCowItAjQ=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=A28IW/rSORIKhh9WS2/e8o59V7TCIV8LgveiJYsa6AVp0lE0L6eQAqFaSD14IOmDN 8aLsWcA3PqIN0FVpfFFMyx2dKltDUQLaYKMZmHXSsQkDShUDxDVGd6yjqCf3H/uYeQ KjHQRVSp6nvcOSBiLOStaDqNSCxngHNvNMWtNB6LsNkW3iIaFDe8Q+lD3V7r+XGNYv TcbfbIiN4fVOEhq0KKrcXzCTqZu8NUl/vbAYNnpJPxGnYkMJoetbH8jlI6M8n5Tfrp 4C1/tN9W1sz+M/KzLGIz9YA3QaO5u6aoloihPRm2Q0G+O5BD5XklWwexU2Fo9fRpG2 DpgPuyzrlC6xg== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4TSx091qN2z4wd0; Mon, 5 Feb 2024 17:13:45 +1100 (AEDT) Date: Mon, 5 Feb 2024 16:57:25 +1100 From: David Gibson To: Laurent Vivier Subject: Re: [PATCH 01/24] iov: add some functions to manage iovec Message-ID: References: <20240202141151.3762941-1-lvivier@redhat.com> <20240202141151.3762941-2-lvivier@redhat.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="bxdaJdbW0h0Astzu" Content-Disposition: inline In-Reply-To: <20240202141151.3762941-2-lvivier@redhat.com> Message-ID-Hash: KF76VCDGEPXOTAFBQ3XKFTYBG3IGLPS7 X-Message-ID-Hash: KF76VCDGEPXOTAFBQ3XKFTYBG3IGLPS7 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: --bxdaJdbW0h0Astzu Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Fri, Feb 02, 2024 at 03:11:28PM +0100, Laurent Vivier wrote: > Signed-off-by: Laurent Vivier > --- > Makefile | 4 +-- > iov.c | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ > iov.h | 46 +++++++++++++++++++++++++++++++++ > 3 files changed, 126 insertions(+), 2 deletions(-) > create mode 100644 iov.c > create mode 100644 iov.h >=20 > diff --git a/Makefile b/Makefile > index af4fa87e7e13..c1138fb91d26 100644 > --- a/Makefile > +++ b/Makefile > @@ -47,7 +47,7 @@ FLAGS +=3D -DDUAL_STACK_SOCKETS=3D$(DUAL_STACK_SOCKETS) > PASST_SRCS =3D arch.c arp.c checksum.c conf.c dhcp.c dhcpv6.c flow.c icm= p.c \ > igmp.c isolation.c lineread.c log.c mld.c ndp.c netlink.c packet.c \ > passt.c pasta.c pcap.c pif.c port_fwd.c tap.c tcp.c tcp_splice.c udp.c \ > - util.c > + util.c iov.c I think we've been maintaining these in alphabetical order so far. > QRAP_SRCS =3D qrap.c > SRCS =3D $(PASST_SRCS) $(QRAP_SRCS) > =20 > @@ -56,7 +56,7 @@ MANPAGES =3D passt.1 pasta.1 qrap.1 > PASST_HEADERS =3D arch.h arp.h checksum.h conf.h dhcp.h dhcpv6.h flow.h \ > flow_table.h icmp.h inany.h isolation.h lineread.h log.h ndp.h \ > netlink.h packet.h passt.h pasta.h pcap.h pif.h port_fwd.h siphash.h \ > - tap.h tcp.h tcp_conn.h tcp_splice.h udp.h util.h > + tap.h tcp.h tcp_conn.h tcp_splice.h udp.h util.h iov.h > HEADERS =3D $(PASST_HEADERS) seccomp.h > =20 > C :=3D \#include \nstruct tcp_info x =3D { .tcpi_snd_wnd = =3D 0 }; > diff --git a/iov.c b/iov.c > new file mode 100644 > index 000000000000..38a8e7566021 > --- /dev/null > +++ b/iov.c > @@ -0,0 +1,78 @@ > +// SPDX-License-Identifier: GPL-2.0-or-later I believe we need an actual copyright / authorship notice as well as the SPDX comment. > + > +/* some parts copied from QEMU util/iov.c */ > + > +#include > +#include "util.h" > +#include "iov.h" > + Function comments would be really helpful here. It took me a while to figure out what this does from just the name and implementation. > +size_t iov_from_buf_full(const struct iovec *iov, unsigned int iov_cnt, > + size_t offset, const void *buf, size_t bytes) > +{ > + size_t done; > + unsigned int i; passt style is to order local declarations from longest to shortest. > + for (i =3D 0, done =3D 0; (offset || done < bytes) && i < iov_cnt; i++)= { Not immediately seeing why you need the 'offset ||' part of the condition. > + if (offset < iov[i].iov_len) { > + size_t len =3D MIN(iov[i].iov_len - offset, bytes - done); > + memcpy((char *)iov[i].iov_base + offset, (char *)buf + done, len); > + done +=3D len; > + offset =3D 0; > + } else { > + offset -=3D iov[i].iov_len; > + } > + } > + return done; > +} > + > +size_t iov_to_buf_full(const struct iovec *iov, const unsigned int iov_c= nt, const modifier on an int isn't very useful. > + size_t offset, void *buf, size_t bytes) > +{ > + size_t done; > + unsigned int i; > + for (i =3D 0, done =3D 0; (offset || done < bytes) && i < iov_cnt; i++)= { > + if (offset < iov[i].iov_len) { > + size_t len =3D MIN(iov[i].iov_len - offset, bytes - done); > + memcpy((char *)buf + done, (char *)iov[i].iov_base + offset, len); > + done +=3D len; > + offset =3D 0; > + } else { > + offset -=3D iov[i].iov_len; > + } > + } > + return done; > +} > + > +size_t iov_size(const struct iovec *iov, const unsigned int iov_cnt) > +{ > + size_t len; > + unsigned int i; > + > + len =3D 0; Can be an initialiser. > + for (i =3D 0; i < iov_cnt; i++) { > + len +=3D iov[i].iov_len; > + } > + return len; > +} > + > +unsigned iov_copy(struct iovec *dst_iov, unsigned int dst_iov_cnt, > + const struct iovec *iov, unsigned int iov_cnt, > + size_t offset, size_t bytes) > +{ > + size_t len; > + unsigned int i, j; > + for (i =3D 0, j =3D 0; > + i < iov_cnt && j < dst_iov_cnt && (offset || bytes); i++) { > + if (offset >=3D iov[i].iov_len) { > + offset -=3D iov[i].iov_len; > + continue; > + } > + len =3D MIN(bytes, iov[i].iov_len - offset); > + > + dst_iov[j].iov_base =3D (char *)iov[i].iov_base + offset; > + dst_iov[j].iov_len =3D len; > + j++; > + bytes -=3D len; > + offset =3D 0; > + } > + return j; > +} Small concern about the interface to iov_copy(). If dst_iov_cnt < iov_cnt and the chunk of the input iovec you want doesn't fit in the destination it will silently truncate - you can't tell if this has happened from the return value. If the assumption is that dst_iov_cnt >=3D iov_cnt, then there's not really any need to pass it. > diff --git a/iov.h b/iov.h > new file mode 100644 > index 000000000000..31fbf6d0e1cf > --- /dev/null > +++ b/iov.h > @@ -0,0 +1,46 @@ > +// SPDX-License-Identifier: GPL-2.0-or-later > + > +/* some parts copied from QEMU include/qemu/iov.h */ > + > +#ifndef IOVEC_H > +#define IOVEC_H > + > +#include > +#include > + > +size_t iov_from_buf_full(const struct iovec *iov, unsigned int iov_cnt, > + size_t offset, const void *buf, size_t bytes); > +size_t iov_to_buf_full(const struct iovec *iov, const unsigned int iov_c= nt, > + size_t offset, void *buf, size_t bytes); > + > +static inline size_t iov_from_buf(const struct iovec *iov, > + unsigned int iov_cnt, size_t offset, > + const void *buf, size_t bytes) > +{ > + if (__builtin_constant_p(bytes) && iov_cnt && > + offset <=3D iov[0].iov_len && bytes <=3D iov[0].iov_len - offset) { > + memcpy((char *)iov[0].iov_base + offset, buf, bytes); > + return bytes; Do you have an idea of how much difference this optimized path makes? > + } else { > + return iov_from_buf_full(iov, iov_cnt, offset, buf, bytes); > + } > +} > + > +static inline size_t iov_to_buf(const struct iovec *iov, > + const unsigned int iov_cnt, size_t offset, > + void *buf, size_t bytes) > +{ > + if (__builtin_constant_p(bytes) && iov_cnt && > + offset <=3D iov[0].iov_len && bytes <=3D iov[0].iov_len - offset) { > + memcpy(buf, (char *)iov[0].iov_base + offset, bytes); > + return bytes; > + } else { > + return iov_to_buf_full(iov, iov_cnt, offset, buf, bytes); > + } > +} > + > +size_t iov_size(const struct iovec *iov, const unsigned int iov_cnt); > +unsigned iov_copy(struct iovec *dst_iov, unsigned int dst_iov_cnt, > + const struct iovec *iov, unsigned int iov_cnt, > + size_t offset, size_t bytes); > +#endif Normal to put a /* IOVEC_H */ comment on the final #endif. --=20 David Gibson | 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 --bxdaJdbW0h0Astzu Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCAAdFiEEO+dNsU4E3yXUXRK2zQJF27ox2GcFAmXAeMQACgkQzQJF27ox 2GdObw/8CFRnXhES4ZrBVOHQipSJDvHnVvD4OPmJ3aqtRJJjhVW+BzNBBxjYYBJt VMUjDzT4AUiOkm3V9AmveR4PfSf8ClN+nnRKv/TBhT5zC8Vk/h+pmGu16GMdpnRE jwrApx36gZOtEhbPU9gt5WUP5LWOpz4s2Cqh7Bw+hf49XJ4mk3DYW83QgehX8Up0 709NYaM7/RZosYI/KMiXH8tlUueMDjUo5O99TfGne+lRwcPHS2O2X7tacwcaj35y skM6C7OrkvpVHukFvtx0vUCsJM9aSOpUxGsbKom3Bwz79aNVE9Fbl7Rfv43KmY2M 3Px/CbzQSvcIC1GirsMV6dLM2FOIp3PR03ScsMNVCcsleujLXHOGUF/uXjd9CNiD hp/s+gXFcsk6pR8aJJzG+CWJCfthFRfZ6/xaK4Qh7cn3Ukf+7YSPRDA7RtmuC4TK cRZXif7xGI42NksnEv1RTnvETYrnvRMdQPYyNhXNhV8vKVt2eY6SsYwqeMpwb/4p fHyFpv9ca1+5PLIATZPWl21VrLfVlOIienMwmBvGbkD+qmwSsVPGFvBV/qrX1cju OhGpJMCq1735dPxYleiR/x1WLQUYthC1iy4qomRMCzxhm34eZzpo+U3izlBeeHWq 3I7ib8IG+mFaKGXznh0NdqH8pQ2UoXgfRlCK5nr5+pYWU6AqoRk= =i49x -----END PGP SIGNATURE----- --bxdaJdbW0h0Astzu--