From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id D35545A004F for ; Mon, 24 Jun 2024 07:05:32 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202312; t=1719205528; bh=LCOwDcN8NUyDBgfm7zKmprDih0dQ5+KjvFpq5lw9kw0=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=NG28L+Wel0hhc7vZRDg892njpitA+ahtP0+KYKby2kc4ogdGlIgmXIQXShRGKTkqM 8C8nGwLFM4LQFby8l/AKqwe7ZBMxt3d1/hLOwBLqQTHxkXW8yDz2jiW7GAz21zFPCc JDBAiEBgFZmqXywIGSCCcKAoGL2wxegl7h8u4hRiIV5P2+p3Y8fNIYtiCR83Scvspd MSd7zcfPNeq5x3kHqwi4xKiQ5IivvDsIdet+VWYxRikM9W9LHwSozKDl1hFvzDSKT0 xvCf/iPROOAlW3B9Bw9UWkvIEhqqtGHDeGr2Sq8r94JfiO1qmExoZM3mfGXXnCr6oM lvlqRxASwVkBw== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4W6wrm2FSFz4wqM; Mon, 24 Jun 2024 15:05:28 +1000 (AEST) Date: Mon, 24 Jun 2024 15:05:23 +1000 From: David Gibson To: Laurent Vivier Subject: Re: [PATCH 5/5] vhost-user: add vhost-user Message-ID: References: <20240621145640.1914287-1-lvivier@redhat.com> <20240621145640.1914287-6-lvivier@redhat.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="wJBjJjxf1Cnk7fLP" Content-Disposition: inline In-Reply-To: <20240621145640.1914287-6-lvivier@redhat.com> Message-ID-Hash: NCQFH2FVGKLGLZQLE7VZF34AOOEER65G X-Message-ID-Hash: NCQFH2FVGKLGLZQLE7VZF34AOOEER65G 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: --wJBjJjxf1Cnk7fLP Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Fri, Jun 21, 2024 at 04:56:40PM +0200, Laurent Vivier wrote: > add virtio and vhost-user functions to connect with QEMU. >=20 > $ ./passt --vhost-user >=20 > and >=20 > # qemu-system-x86_64 ... -m 4G \ > -object memory-backend-memfd,id=3Dmemfd0,share=3Don,size=3D4G \ > -numa node,memdev=3Dmemfd0 \ > -chardev socket,id=3Dchr0,path=3D/tmp/passt_1.socket \ > -netdev vhost-user,id=3Dnetdev0,chardev=3Dchr0 \ > -device virtio-net,mac=3D9a:2b:2c:2d:2e:2f,netdev=3Dnetdev0 \ > ... >=20 > Signed-off-by: Laurent Vivier > --- > Makefile | 4 +- > checksum.c | 1 - > conf.c | 18 +- > iov.c | 1 - > packet.c | 6 + > packet.h | 2 + > passt.c | 12 +- > passt.h | 2 + > pcap.c | 1 - > tap.c | 87 ++++++-- > tap.h | 3 +- > tcp.c | 17 +- > tcp_vu.c | 547 +++++++++++++++++++++++++++++++++++++++++++++++++ > tcp_vu.h | 9 + > udp.c | 54 +++-- > udp_internal.h | 39 ++++ > udp_vu.c | 237 +++++++++++++++++++++ > udp_vu.h | 8 + > vhost_user.c | 6 - > virtio.c | 1 - > 20 files changed, 988 insertions(+), 67 deletions(-) > create mode 100644 tcp_vu.c > create mode 100644 tcp_vu.h > create mode 100644 udp_internal.h > create mode 100644 udp_vu.c > create mode 100644 udp_vu.h >=20 > diff --git a/Makefile b/Makefile > index b2da6ad62103..d22388726099 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 fwd= =2Ec \ > icmp.c igmp.c inany.c iov.c ip.c isolation.c lineread.c log.c mld.c \ > ndp.c netlink.c packet.c passt.c pasta.c pcap.c pif.c tap.c tcp.c \ > - tcp_buf.c tcp_splice.c udp.c util.c vhost_user.c virtio.c > + tcp_buf.c tcp_splice.c tcp_vu.c udp.c udp_vu.c util.c vhost_user.c virt= io.c > QRAP_SRCS =3D qrap.c > SRCS =3D $(PASST_SRCS) $(QRAP_SRCS) > =20 > @@ -57,7 +57,7 @@ PASST_HEADERS =3D arch.h arp.h checksum.h conf.h dhcp.h= dhcpv6.h flow.h fwd.h \ > flow_table.h icmp.h icmp_flow.h inany.h iov.h ip.h isolation.h \ > lineread.h log.h ndp.h netlink.h packet.h passt.h pasta.h pcap.h pif.h \ > siphash.h tap.h tcp.h tcp_buf.h tcp_conn.h tcp_internal.h tcp_splice.h \ > - udp.h util.h vhost_user.h virtio.h > + tcp_vu.h udp.h udp_internal.h udp_vu.h util.h vhost_user.h virtio.h > HEADERS =3D $(PASST_HEADERS) seccomp.h > =20 > C :=3D \#include \nstruct tcp_info x =3D { .tcpi_snd_wnd = =3D 0 }; > diff --git a/checksum.c b/checksum.c > index 006614fcbb28..aa5b7ae1cb66 100644 > --- a/checksum.c > +++ b/checksum.c > @@ -501,7 +501,6 @@ uint16_t csum(const void *buf, size_t len, uint32_t i= nit) > * > * Return: 16-bit folded, complemented checksum > */ > -/* cppcheck-suppress unusedFunction */ > uint16_t csum_iov(const struct iovec *iov, size_t n, uint32_t init) > { > unsigned int i; > diff --git a/conf.c b/conf.c > index 94b3ed6fa659..2c9a6da05666 100644 > --- a/conf.c > +++ b/conf.c > @@ -45,6 +45,7 @@ > #include "lineread.h" > #include "isolation.h" > #include "log.h" > +#include "vhost_user.h" > =20 > /** > * next_chunk - Return the next piece of a string delimited by a charact= er > @@ -751,6 +752,9 @@ static void usage(const char *name, FILE *f, int stat= us) > " -s, --socket PATH UNIX domain socket path\n" > " default: probe free path starting from " > UNIX_SOCK_PATH "\n", 1); > + info( " --vhost-user Enable vhost-user mode"); > + info( " UNIX domain socket is provided by -s option"); > + info( " --print-capabilities print back-end capabilities in JSON fo= rmat"); Probably worth nothing this is only meaningful for vhost-user mode. > } > =20 > fprintf(f, > @@ -1175,6 +1179,7 @@ void conf(struct ctx *c, int argc, char **argv) > {"help", no_argument, NULL, 'h' }, > {"socket", required_argument, NULL, 's' }, > {"fd", required_argument, NULL, 'F' }, > + {"socket-path", required_argument, NULL, 's' }, /* vhost-user mandato= ry */ Maybe put this next to the --socket option to make it clearer it's an alias for it. "vhost-user mandatory" isn't that clear to me - initially I though it meant the user had to supply this option for vhost-user mode, rather than that the vhost-user interface mandates this option exists. > {"ns-ifname", required_argument, NULL, 'I' }, > {"pcap", required_argument, NULL, 'p' }, > {"pid", required_argument, NULL, 'P' }, > @@ -1221,6 +1226,8 @@ void conf(struct ctx *c, int argc, char **argv) > {"config-net", no_argument, NULL, 17 }, > {"no-copy-routes", no_argument, NULL, 18 }, > {"no-copy-addrs", no_argument, NULL, 19 }, > + {"vhost-user", no_argument, NULL, 20 }, > + {"print-capabilities", no_argument, NULL, 21 }, /* vhost-user mandato= ry */ > { 0 }, > }; > char userns[PATH_MAX] =3D { 0 }, netns[PATH_MAX] =3D { 0 }; > @@ -1373,7 +1380,6 @@ void conf(struct ctx *c, int argc, char **argv) > sizeof(c->ip6.ifname_out), "%s", optarg); > if (ret <=3D 0 || ret >=3D (int)sizeof(c->ip6.ifname_out)) > die("Invalid interface name: %s", optarg); > - Unrelated whitespace change. > break; > case 17: > if (c->mode !=3D MODE_PASTA) > @@ -1395,6 +1401,16 @@ void conf(struct ctx *c, int argc, char **argv) > warn("--no-copy-addrs will be dropped soon"); > c->no_copy_addrs =3D copy_addrs_opt =3D true; > break; > + case 20: > + if (c->mode =3D=3D MODE_PASTA) { > + err("--vhost-user is for passt mode only"); > + usage(argv[0], stdout, EXIT_SUCCESS); > + } > + c->mode =3D MODE_VU; > + break; > + case 21: > + vu_print_capabilities(); > + break; > case 'd': > if (c->debug) > die("Multiple --debug options given"); > diff --git a/iov.c b/iov.c > index 793788b5d2bc..4215baf7c3b9 100644 > --- a/iov.c > +++ b/iov.c > @@ -170,7 +170,6 @@ size_t iov_size(const struct iovec *iov, size_t iov_c= nt) > * > * Returns: The number of iovec needed to store @size bytes. > */ > -/* cppcheck-suppress unusedFunction */ > size_t iov_count(const struct iovec *iov, size_t iov_cnt, > size_t size, size_t *last_iov_length) > { > diff --git a/packet.c b/packet.c > index af2a539a1794..3c5fc39df6d7 100644 > --- a/packet.c > +++ b/packet.c > @@ -25,6 +25,12 @@ > static int packet_check_range(const struct pool *p, size_t offset, size_= t len, > const char *start, const char *func, int line) > { > + ASSERT(p->buf); > + > + if (p->buf_size =3D=3D 0) > + return vu_packet_check_range((void *)p->buf, offset, len, start, > + func, line); > + > if (start < p->buf) { > if (func) { > trace("add packet start %p before buffer start %p, " > diff --git a/packet.h b/packet.h > index 8377dcf678bb..0aec6d9410aa 100644 > --- a/packet.h > +++ b/packet.h > @@ -22,6 +22,8 @@ struct pool { > struct iovec pkt[1]; > }; > =20 > +int vu_packet_check_range(void *buf, size_t offset, size_t len, > + const char *start, const char *func, int line); > void packet_add_do(struct pool *p, size_t len, const char *start, > const char *func, int line); > void *packet_get_do(const struct pool *p, const size_t idx, > diff --git a/passt.c b/passt.c > index 9d21c545b9cf..8c0490782a7f 100644 > --- a/passt.c > +++ b/passt.c > @@ -274,6 +274,7 @@ int main(int argc, char **argv) > pasta_netns_quit_init(&c); > =20 > tap_sock_init(&c); > + vu_init(&c); > =20 > secret_init(&c); > =20 > @@ -367,11 +368,20 @@ loop: > tcp_timer_handler(&c, ref); > break; > case EPOLL_TYPE_UDP: > - udp_buf_sock_handler(&c, ref, eventmask, &now); > + if (c.mode =3D=3D MODE_VU) > + udp_vu_sock_handler(&c, ref, eventmask, &now); > + else > + udp_buf_sock_handler(&c, ref, eventmask, &now); > break; > case EPOLL_TYPE_PING: > icmp_sock_handler(&c, ref); > break; > + case EPOLL_TYPE_VHOST_CMD: > + tap_handler_vu(&c, eventmask); > + break; > + case EPOLL_TYPE_VHOST_KICK: > + vu_kick_cb(&c, ref); > + break; > default: > /* Can't happen */ > ASSERT(0); > diff --git a/passt.h b/passt.h > index af10d0bfe4ef..f15f28c89d39 100644 > --- a/passt.h > +++ b/passt.h > @@ -22,6 +22,7 @@ union epoll_ref; > #include "fwd.h" > #include "tcp.h" > #include "udp.h" > +#include "udp_vu.h" > #include "vhost_user.h" > =20 > /** > @@ -122,6 +123,7 @@ struct fqdn { > enum passt_modes { > MODE_PASST, > MODE_PASTA, > + MODE_VU, > }; > =20 > /** > diff --git a/pcap.c b/pcap.c > index 507be2ac1edf..d4d0ec62b944 100644 > --- a/pcap.c > +++ b/pcap.c > @@ -142,7 +142,6 @@ void pcap_multiple(const struct iovec *iov, size_t fr= ame_parts, unsigned int n, > * containing packet data to write, including L2 header > * @iovcnt: Number of buffers (@iov entries) > */ > -/* cppcheck-suppress unusedFunction */ > void pcap_iov(const struct iovec *iov, size_t iovcnt) > { > struct timespec now; > diff --git a/tap.c b/tap.c > index be272d25b642..e3274d39131a 100644 > --- a/tap.c > +++ b/tap.c > @@ -58,6 +58,7 @@ > #include "packet.h" > #include "tap.h" > #include "log.h" > +#include "vhost_user.h" > =20 > /* IPv4 (plus ARP) and IPv6 message batches from tap/guest to IP handler= s */ > static PACKET_POOL_NOINIT(pool_tap4, TAP_MSGS, pkt_buf); > @@ -78,16 +79,22 @@ void tap_send_single(const struct ctx *c, const void = *data, size_t l2len) > struct iovec iov[2]; > size_t iovcnt =3D 0; > =20 > - if (c->mode =3D=3D MODE_PASST) { > + switch (c->mode) { > + case MODE_PASST: > iov[iovcnt] =3D IOV_OF_LVALUE(vnet_len); > iovcnt++; > - } > - > - iov[iovcnt].iov_base =3D (void *)data; > - iov[iovcnt].iov_len =3D l2len; > - iovcnt++; > + /* fall through */ > + case MODE_PASTA: > + iov[iovcnt].iov_base =3D (void *)data; > + iov[iovcnt].iov_len =3D l2len; > + iovcnt++; > =20 > - tap_send_frames(c, iov, iovcnt, 1); > + tap_send_frames(c, iov, iovcnt, 1); > + break; > + case MODE_VU: > + vu_send(c, data, l2len); > + break; > + } > } > =20 > /** > @@ -416,10 +423,19 @@ size_t tap_send_frames(const struct ctx *c, const s= truct iovec *iov, > if (!nframes) > return 0; > =20 > - if (c->mode =3D=3D MODE_PASTA) > + switch (c->mode) { > + case MODE_PASTA: > m =3D tap_send_frames_pasta(c, iov, bufs_per_frame, nframes); > - else > + break; > + case MODE_PASST: > m =3D tap_send_frames_passt(c, iov, bufs_per_frame, nframes); > + break; > + case MODE_VU: > + ASSERT(0); > + default: This should be an ASSERT(0) as well, yes? We shouldn't be able to get here without a mode being set. > + m =3D 0; > + break; > + } > =20 > if (m < nframes) > debug("tap: failed to send %zu frames of %zu", > @@ -1180,11 +1196,17 @@ static void tap_sock_unix_init(struct ctx *c) > ev.data.u64 =3D ref.u64; > epoll_ctl(c->epollfd, EPOLL_CTL_ADD, c->fd_tap_listen, &ev); > =20 > - info("\nYou can now start qemu (>=3D 7.2, with commit 13c6be96618c):"); > - info(" kvm ... -device virtio-net-pci,netdev=3Ds -netdev stream,id= =3Ds,server=3Doff,addr.type=3Dunix,addr.path=3D%s", > - c->sock_path); > - info("or qrap, for earlier qemu versions:"); > - info(" ./qrap 5 kvm ... -net socket,fd=3D5 -net nic,model=3Dvirtio"); > + if (c->mode =3D=3D MODE_VU) { > + info("You can start qemu with:"); > + info(" kvm ... -chardev socket,id=3Dchr0,path=3D%s -netdev vhost-us= er,id=3Dnetdev0,chardev=3Dchr0 -device virtio-net,netdev=3Dnetdev0 -object = memory-backend-memfd,id=3Dmemfd0,share=3Don,size=3D$RAMSIZE -numa node,memd= ev=3Dmemfd0\n", > + c->sock_path); > + } else { > + info("\nYou can now start qemu (>=3D 7.2, with commit 13c6be96618c):"); > + info(" kvm ... -device virtio-net-pci,netdev=3Ds -netdev stream,id= =3Ds,server=3Doff,addr.type=3Dunix,addr.path=3D%s", > + c->sock_path); > + info("or qrap, for earlier qemu versions:"); > + info(" ./qrap 5 kvm ... -net socket,fd=3D5 -net nic,model=3Dvirtio"= ); > + } > } > =20 > /** > @@ -1194,8 +1216,8 @@ static void tap_sock_unix_init(struct ctx *c) > */ > void tap_listen_handler(struct ctx *c, uint32_t events) > { > - union epoll_ref ref =3D { .type =3D EPOLL_TYPE_TAP_PASST }; > struct epoll_event ev =3D { 0 }; > + union epoll_ref ref; > int v =3D INT_MAX / 2; > struct ucred ucred; > socklen_t len; > @@ -1235,7 +1257,13 @@ void tap_listen_handler(struct ctx *c, uint32_t ev= ents) > trace("tap: failed to set SO_SNDBUF to %i", v); > =20 > ref.fd =3D c->fd_tap; > - ev.events =3D EPOLLIN | EPOLLET | EPOLLRDHUP; > + if (c->mode =3D=3D MODE_VU) { > + ref.type =3D EPOLL_TYPE_VHOST_CMD; > + ev.events =3D EPOLLIN | EPOLLRDHUP; > + } else { > + ref.type =3D EPOLL_TYPE_TAP_PASST; > + ev.events =3D EPOLLIN | EPOLLRDHUP | EPOLLET; > + } > ev.data.u64 =3D ref.u64; > epoll_ctl(c->epollfd, EPOLL_CTL_ADD, c->fd_tap, &ev); > } > @@ -1324,10 +1352,22 @@ void tap_sock_init(struct ctx *c) > =20 > pool_tap4_storage =3D PACKET_INIT(pool_tap4, TAP_MSGS, pkt_buf, sz); > pool_tap6_storage =3D PACKET_INIT(pool_tap6, TAP_MSGS, pkt_buf, sz); > + if (c->mode =3D=3D MODE_VU) { > + pool_tap4_storage.buf =3D NULL; > + pool_tap4_storage.buf_size =3D 0; > + pool_tap6_storage.buf =3D NULL; > + pool_tap6_storage.buf_size =3D 0; It seems a bit of a layering violation to initialize the pool with PACKET_INIT() then mangle its internals in the vhost-user case. Could we use a different PACKET_INIT invocation for the VU case instead? > + } > =20 > for (i =3D 0; i < TAP_SEQS; i++) { > tap4_l4[i].p =3D PACKET_INIT(pool_l4, UIO_MAXIOV, pkt_buf, sz); > tap6_l4[i].p =3D PACKET_INIT(pool_l4, UIO_MAXIOV, pkt_buf, sz); > + if (c->mode =3D=3D MODE_VU) { > + tap4_l4[i].p.buf =3D NULL; > + tap4_l4[i].p.buf_size =3D 0; > + tap6_l4[i].p.buf =3D NULL; > + tap6_l4[i].p.buf_size =3D 0; Same here, of course. > + } > } > =20 > if (c->fd_tap !=3D -1) { /* Passed as --fd */ > @@ -1336,12 +1376,21 @@ void tap_sock_init(struct ctx *c) > =20 > ASSERT(c->one_off); > ref.fd =3D c->fd_tap; > - if (c->mode =3D=3D MODE_PASST) > + switch (c->mode) { > + case MODE_PASST: > ref.type =3D EPOLL_TYPE_TAP_PASST; > - else > + ev.events =3D EPOLLIN | EPOLLET | EPOLLRDHUP; > + break; > + case MODE_PASTA: > ref.type =3D EPOLL_TYPE_TAP_PASTA; > + ev.events =3D EPOLLIN | EPOLLET | EPOLLRDHUP; > + break; > + case MODE_VU: > + ref.type =3D EPOLL_TYPE_VHOST_CMD; > + ev.events =3D EPOLLIN | EPOLLRDHUP; > + break; I suspect one of our static checkers will complain at some point if we don't put a default case with an ASSERT here. > + } > =20 > - ev.events =3D EPOLLIN | EPOLLET | EPOLLRDHUP; > ev.data.u64 =3D ref.u64; > epoll_ctl(c->epollfd, EPOLL_CTL_ADD, c->fd_tap, &ev); > return; > diff --git a/tap.h b/tap.h > index 3b2dde41ae8d..d9c6d4f57093 100644 > --- a/tap.h > +++ b/tap.h > @@ -40,7 +40,8 @@ static inline struct iovec tap_hdr_iov(const struct ctx= *c, > */ > static inline void tap_hdr_update(struct tap_hdr *thdr, size_t l2len) > { > - thdr->vnet_len =3D htonl(l2len); > + if (thdr) > + thdr->vnet_len =3D htonl(l2len); > } > =20 > struct in_addr tap_ip4_daddr(const struct ctx *c); > diff --git a/tcp.c b/tcp.c > index 68524235347c..8709dd6d97bb 100644 > --- a/tcp.c > +++ b/tcp.c > @@ -304,6 +304,7 @@ > #include "flow_table.h" > #include "tcp_internal.h" > #include "tcp_buf.h" > +#include "tcp_vu.h" > =20 > #define TCP_HASH_TABLE_LOAD 70 /* % */ > #define TCP_HASH_TABLE_SIZE (FLOW_MAX * 100 / TCP_HASH_TABLE_LOAD) > @@ -1049,7 +1050,10 @@ static size_t tcp_fill_headers4(const struct ctx *= c, > =20 > tcp_fill_header(th, conn, seq); > =20 > - tcp_update_check_tcp4(iph, th); > + if (c->mode !=3D MODE_VU) > + tcp_update_check_tcp4(iph, th); > + else > + th->check =3D 0; > =20 > tap_hdr_update(taph, l3len + sizeof(struct ethhdr)); > =20 > @@ -1094,7 +1098,10 @@ static size_t tcp_fill_headers6(const struct ctx *= c, > =20 > tcp_fill_header(th, conn, seq); > =20 > - tcp_update_check_tcp6(ip6h, th); > + if (c->mode !=3D MODE_VU) > + tcp_update_check_tcp6(ip6h, th); > + else > + th->check =3D 0; > =20 > tap_hdr_update(taph, l4len + sizeof(*ip6h) + sizeof(struct ethhdr)); > =20 > @@ -1362,6 +1369,9 @@ int tcp_prepare_flags(struct ctx *c, struct tcp_tap= _conn *conn, > */ > int tcp_send_flag(struct ctx *c, struct tcp_tap_conn *conn, int flags) > { > + if (c->mode =3D=3D MODE_VU) > + return tcp_vu_send_flag(c, conn, flags); > + > return tcp_buf_send_flag(c, conn, flags); > } > =20 > @@ -1808,6 +1818,9 @@ static int tcp_sock_consume(const struct tcp_tap_co= nn *conn, uint32_t ack_seq) > */ > static int tcp_data_from_sock(struct ctx *c, struct tcp_tap_conn *conn) > { > + if (c->mode =3D=3D MODE_VU) > + return tcp_vu_data_from_sock(c, conn); > + > return tcp_buf_data_from_sock(c, conn); > } > =20 > diff --git a/tcp_vu.c b/tcp_vu.c > new file mode 100644 > index 000000000000..f27890f63c0e > --- /dev/null > +++ b/tcp_vu.c > @@ -0,0 +1,547 @@ > +// SPDX-License-Identifier: GPL-2.0-or-later Needs Copyright notice, author information and general description here. > + > +#include > +#include > +#include > + > +#include > + > +#include > + > +#include > +#include > + > +#include "util.h" > +#include "ip.h" > +#include "passt.h" > +#include "siphash.h" > +#include "inany.h" > +#include "vhost_user.h" > +#include "tcp.h" > +#include "pcap.h" > +#include "flow.h" > +#include "tcp_conn.h" > +#include "flow_table.h" > +#include "tcp_vu.h" > +#include "tcp_internal.h" > +#include "checksum.h" > + > +#define CONN_V4(conn) (!!inany_v4(&(conn)->faddr)) > +#define CONN_V6(conn) (!CONN_V4(conn)) > + > +/** > + * struct tcp_payload_t - TCP header and data to send segments with payl= oad > + * @th: TCP header > + * @data: TCP data > + */ > +struct tcp_payload_t { > + struct tcphdr th; > + uint8_t data[IP_MAX_MTU - sizeof(struct tcphdr)]; > +}; This could be common with tcp_buf.c, couldn't it? > + > +/** > + * struct tcp_flags_t - TCP header and data to send zero-length > + * segments (flags) > + * @th: TCP header > + * @opts TCP options > + */ > +struct tcp_flags_t { > + struct tcphdr th; > + char opts[OPT_MSS_LEN + OPT_WS_LEN + 1]; > +}; Likewise here. > + > +/* vhost-user */ > +static const struct virtio_net_hdr vu_header =3D { > + .flags =3D VIRTIO_NET_HDR_F_DATA_VALID, > + .gso_type =3D VIRTIO_NET_HDR_GSO_NONE, > +}; > + > +int tcp_vu_send_flag(struct ctx *c, struct tcp_tap_conn *conn, int flags) > +{ > + VuDev *vdev =3D (VuDev *)&c->vdev; > + VuVirtq *vq =3D &vdev->vq[VHOST_USER_RX_QUEUE]; > + size_t tlen, vnet_hdrlen, l4len, optlen; > + struct virtio_net_hdr_mrg_rxbuf *vh; > + struct iovec l2_iov[TCP_NUM_IOVS]; > + VuVirtqElement elem; > + struct iovec in_sg; > + struct ethhdr *eh; > + int nb_ack; > + int ret; > + > + elem.out_num =3D 0; > + elem.out_sg =3D NULL; > + elem.in_num =3D 1; > + elem.in_sg =3D &in_sg; > + ret =3D vu_queue_pop(vdev, vq, &elem); > + if (ret < 0) > + return 0; > + > + if (elem.in_num < 1) { > + err("virtio-net receive queue contains no in buffers"); > + vu_queue_rewind(vdev, vq, 1); > + return 0; > + } > + > + vh =3D elem.in_sg[0].iov_base; AFAICT, the code below requires that in_sg[0] be large enough to contain the frame, plus a virtio_net_hdr_mrg_rxbuf. Seems like that we should ASSERT() that somewhere. If I'm understanding correctly that the virtio_net_hdr_mrg_rxbuf is a kind of pseudo-header you need for each frame, I'm wondering if it could be integrated into the tap_hdr mechanisms. > + > + vh->hdr =3D vu_header; > + if (vu_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF)) { > + vnet_hdrlen =3D sizeof(struct virtio_net_hdr_mrg_rxbuf); sizeof(*vh) maybe? > + vh->num_buffers =3D htole16(1); > + } else { > + vnet_hdrlen =3D sizeof(struct virtio_net_hdr); > + } > + > + l2_iov[TCP_IOV_TAP].iov_base =3D NULL; > + l2_iov[TCP_IOV_TAP].iov_len =3D 0; > + l2_iov[TCP_IOV_ETH].iov_base =3D (char *)elem.in_sg[0].iov_base + vnet_= hdrlen; > + l2_iov[TCP_IOV_ETH].iov_len =3D sizeof(struct ethhdr); > + > + eh =3D l2_iov[TCP_IOV_ETH].iov_base; You could initialise eh first, then set l2_iov[TCP_IOV_ETH] using IOV_OF_LVALUE(). > + > + memcpy(eh->h_dest, c->mac_guest, sizeof(eh->h_dest)); > + memcpy(eh->h_source, c->mac, sizeof(eh->h_source)); > + > + if (CONN_V4(conn)) { > + struct tcp_flags_t *payload; > + struct iphdr *iph; > + uint32_t seq; > + > + l2_iov[TCP_IOV_IP].iov_base =3D (char *)l2_iov[TCP_IOV_ETH].iov_base + > + l2_iov[TCP_IOV_ETH].iov_len; > + l2_iov[TCP_IOV_IP].iov_len =3D sizeof(struct iphdr); > + l2_iov[TCP_IOV_PAYLOAD].iov_base =3D (char *)l2_iov[TCP_IOV_IP].iov_ba= se + > + l2_iov[TCP_IOV_IP].iov_len; > + Similar thing for iph and TCP_IOV_IP. > + eh->h_proto =3D htons(ETH_P_IP); > + > + iph =3D l2_iov[TCP_IOV_IP].iov_base; > + *iph =3D (struct iphdr)L2_BUF_IP4_INIT(IPPROTO_TCP); Hrm... once l2_iov[TCP_IOV_IP] is set up like this, couldn't you share the actual initialisation of the header fields with the tcp_buf code? > + payload =3D l2_iov[TCP_IOV_PAYLOAD].iov_base; > + payload->th =3D (struct tcphdr){ > + .doff =3D offsetof(struct tcp_flags_t, opts) / 4, > + .ack =3D 1 > + }; Similarly the TCP header. > + seq =3D conn->seq_to_tap; > + ret =3D tcp_prepare_flags(c, conn, flags, &payload->th, payload->opts,= &optlen); > + if (ret <=3D 0) { > + vu_queue_rewind(vdev, vq, 1); > + return ret; > + } > + > + l4len =3D tcp_l2_buf_fill_headers(c, conn, l2_iov, optlen, NULL, > + seq); Hrm, I guess you sort of are, but I wonder if we can make a longer stretch of code common here. > + /* cppcheck-suppress unreadVariable */ > + l2_iov[TCP_IOV_PAYLOAD].iov_len =3D l4len; > + > + tlen =3D l4len + sizeof(*iph) + sizeof(struct ethhdr); 'l2len' would be the preferred name for this quantity now. > + > + if (*c->pcap) { > + uint32_t sum =3D proto_ipv4_header_psum(l4len, > + IPPROTO_TCP, > + /* cppcheck-suppress unknownEvaluationOrder */ > + (struct in_addr){ .s_addr =3D iph->saddr }, I think using locals of type struct in_addr would be cleaner here, and avoid the cppcheck warning more elegantly. > + (struct in_addr){ .s_addr =3D iph->daddr }); > + > + payload->th.check =3D 0; > + payload->th.check =3D csum(&payload->th, optlen + sizeof(struct tcphd= r), sum); > + } > + } else { > + struct tcp_flags_t *payload; > + struct ipv6hdr *ip6h; > + uint32_t seq; > + > + l2_iov[TCP_IOV_IP].iov_base =3D (char *)l2_iov[TCP_IOV_ETH].iov_base + > + l2_iov[TCP_IOV_ETH].iov_len; > + l2_iov[TCP_IOV_IP].iov_len =3D sizeof(struct ipv6hdr); > + l2_iov[TCP_IOV_PAYLOAD].iov_base =3D (char *)l2_iov[TCP_IOV_IP].iov_ba= se + > + l2_iov[TCP_IOV_IP].iov_len; > + > + eh->h_proto =3D htons(ETH_P_IPV6); > + > + ip6h =3D l2_iov[TCP_IOV_IP].iov_base; > + *ip6h =3D (struct ipv6hdr)L2_BUF_IP6_INIT(IPPROTO_TCP); > + > + payload =3D l2_iov[TCP_IOV_PAYLOAD].iov_base; > + payload->th =3D (struct tcphdr){ > + .doff =3D offsetof(struct tcp_flags_t, opts) / 4, > + .ack =3D 1 > + }; > + > + seq =3D conn->seq_to_tap; > + ret =3D tcp_prepare_flags(c, conn, flags, &payload->th, payload->opts,= &optlen); > + if (ret <=3D 0) { > + vu_queue_rewind(vdev, vq, 1); > + return ret; > + } > + > + l4len =3D tcp_l2_buf_fill_headers(c, conn, l2_iov, optlen, NULL, > + seq); > + /* cppcheck-suppress unreadVariable */ > + l2_iov[TCP_IOV_PAYLOAD].iov_len =3D l4len; > + > + tlen =3D l4len + sizeof(*ip6h) + sizeof(struct ethhdr); > + > + if (*c->pcap) { > + uint32_t sum =3D proto_ipv6_header_psum(l4len, > + IPPROTO_TCP, > + &ip6h->saddr, > + &ip6h->daddr); > + > + payload->th.check =3D 0; > + payload->th.check =3D csum(&payload->th, optlen + sizeof(struct tcphd= r), sum); > + } > + } > + > + pcap((void *)eh, tlen); > + > + tlen +=3D vnet_hdrlen; > + vu_queue_fill(vdev, vq, &elem, tlen, 0); > + nb_ack =3D 1; > + > + if (flags & DUP_ACK) { > + VuVirtqElement elem_dup; > + struct iovec in_sg_dup; > + > + elem_dup.out_num =3D 0; > + elem_dup.out_sg =3D NULL; > + elem_dup.in_num =3D 1; > + elem_dup.in_sg =3D &in_sg_dup; > + ret =3D vu_queue_pop(vdev, vq, &elem_dup); > + if (ret =3D=3D 0) { > + if (elem_dup.in_num < 1 || elem_dup.in_sg[0].iov_len < tlen) { > + vu_queue_rewind(vdev, vq, 1); > + } else { > + memcpy(elem_dup.in_sg[0].iov_base, vh, tlen); > + nb_ack++; > + } > + } > + } > + > + vu_queue_flush(vdev, vq, nb_ack); > + vu_queue_notify(vdev, vq); > + > + return 0; > +} > + > +int tcp_vu_data_from_sock(struct ctx *c, struct tcp_tap_conn *conn) > +{ > + uint32_t wnd_scaled =3D conn->wnd_from_tap << conn->ws_from_tap; > + static struct iovec iov_vu[VIRTQUEUE_MAX_SIZE]; > + static VuVirtqElement elem[VIRTQUEUE_MAX_SIZE]; > + static struct iovec in_sg[VIRTQUEUE_MAX_SIZE]; > + VuDev *vdev =3D (VuDev *)&c->vdev; > + VuVirtq *vq =3D &vdev->vq[VHOST_USER_RX_QUEUE]; > + size_t l2_hdrlen, vnet_hdrlen, fillsize; > + int s =3D conn->sock, v4 =3D CONN_V4(conn); > + struct iovec l2_iov[TCP_NUM_IOVS]; > + int i, ret, iov_cnt, iov_used; > + struct msghdr mh_sock =3D { 0 }; > + uint16_t mss =3D MSS_GET(conn); > + static int in_sg_count; > + uint32_t already_sent; > + const uint16_t *check; > + struct iovec *first; > + bool has_mrg_rxbuf; > + int segment_size; > + int num_buffers; > + ssize_t len; > + > + if (!vu_queue_enabled(vq) || !vu_queue_started(vq)) { > + err("Got packet, but no available descriptors on RX virtq."); flow_err() to give more useful information here. > + return 0; > + } > + > + already_sent =3D conn->seq_to_tap - conn->seq_ack_from_tap; > + > + if (SEQ_LT(already_sent, 0)) { > + /* RFC 761, section 2.1. */ > + flow_trace(conn, "ACK sequence gap: ACK for %u, sent: %u", > + conn->seq_ack_from_tap, conn->seq_to_tap); > + conn->seq_to_tap =3D conn->seq_ack_from_tap; > + already_sent =3D 0; > + } > + > + if (!wnd_scaled || already_sent >=3D wnd_scaled) { > + conn_flag(c, conn, STALLED); > + conn_flag(c, conn, ACK_FROM_TAP_DUE); > + return 0; > + } Duplicating some of this subtle TCP core logic between the buf and vu paths worries me quite a bit :/. > + > + /* Set up buffer descriptors we'll fill completely and partially. */ > + > + fillsize =3D wnd_scaled; > + > + iov_vu[0].iov_base =3D tcp_buf_discard; > + iov_vu[0].iov_len =3D already_sent; > + fillsize -=3D already_sent; > + > + has_mrg_rxbuf =3D vu_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF); > + if (has_mrg_rxbuf) > + vnet_hdrlen =3D sizeof(struct virtio_net_hdr_mrg_rxbuf); > + else > + vnet_hdrlen =3D sizeof(struct virtio_net_hdr); > + l2_hdrlen =3D vnet_hdrlen + sizeof(struct ethhdr) + sizeof(struct tcphd= r); "l2_hdrlen" is a bad name, since this is including headers both "above" L2 (tcp, IP) and "below" L2 (vnet_hdrlen). > + if (v4) > + l2_hdrlen +=3D sizeof(struct iphdr); > + else > + l2_hdrlen +=3D sizeof(struct ipv6hdr); > + > + iov_cnt =3D 0; > + in_sg_count =3D 0; > + segment_size =3D 0; > + while (fillsize > 0 && iov_cnt < VIRTQUEUE_MAX_SIZE - 1 && > + in_sg_count < ARRAY_SIZE(in_sg)) { > + > + elem[iov_cnt].out_num =3D 0; > + elem[iov_cnt].out_sg =3D NULL; > + elem[iov_cnt].in_num =3D ARRAY_SIZE(in_sg) - in_sg_count; > + elem[iov_cnt].in_sg =3D &in_sg[in_sg_count]; > + ret =3D vu_queue_pop(vdev, vq, &elem[iov_cnt]); > + if (ret < 0) > + break; > + > + if (elem[iov_cnt].in_num < 1) { > + err("virtio-net receive queue contains no in buffers"); > + goto err; > + } > + in_sg_count +=3D elem[iov_cnt].in_num; > + > + ASSERT(elem[iov_cnt].in_num =3D=3D 1); > + ASSERT(elem[iov_cnt].in_sg[0].iov_len >=3D l2_hdrlen); > + > + if (segment_size =3D=3D 0) { > + iov_vu[iov_cnt + 1].iov_base =3D > + (char *)elem[iov_cnt].in_sg[0].iov_base + l2_hdrlen; > + iov_vu[iov_cnt + 1].iov_len =3D > + elem[iov_cnt].in_sg[0].iov_len - l2_hdrlen; Do we need to verify somehwere that this buffer is large enough for all the headers? > + } else { > + iov_vu[iov_cnt + 1].iov_base =3D elem[iov_cnt].in_sg[0].iov_base; > + iov_vu[iov_cnt + 1].iov_len =3D elem[iov_cnt].in_sg[0].iov_len; > + } > + > + if (iov_vu[iov_cnt + 1].iov_len > fillsize) > + iov_vu[iov_cnt + 1].iov_len =3D fillsize; > + > + segment_size +=3D iov_vu[iov_cnt + 1].iov_len; > + if (!has_mrg_rxbuf) { > + segment_size =3D 0; > + } else if (segment_size >=3D mss) { > + iov_vu[iov_cnt + 1].iov_len -=3D segment_size - mss; > + segment_size =3D 0; > + } If I'm understanding this correctly, we're adjusting the size of each TCP packet we generate to match the size of the buffer that the guest provides. Is that correct? In practice, how large are these buffers going to me - could this dramatically our typical segment size in practice? > + fillsize -=3D iov_vu[iov_cnt + 1].iov_len; > + > + iov_cnt++; > + } > + if (iov_cnt =3D=3D 0) > + return 0; > + > + ret =3D 0; > + mh_sock.msg_iov =3D iov_vu; > + mh_sock.msg_iovlen =3D iov_cnt + 1; > + > + do > + len =3D recvmsg(s, &mh_sock, MSG_PEEK); > + while (len < 0 && errno =3D=3D EINTR); > + > + if (len < 0) > + goto err; > + > + if (!len) { > + vu_queue_rewind(vdev, vq, iov_cnt); > + if ((conn->events & (SOCK_FIN_RCVD | TAP_FIN_SENT)) =3D=3D SOCK_FIN_RC= VD) { > + ret =3D tcp_vu_send_flag(c, conn, FIN | ACK); > + if (ret) { > + tcp_rst(c, conn); > + return ret; > + } > + > + conn_event(c, conn, TAP_FIN_SENT); > + } > + > + return 0; > + } > + > + len -=3D already_sent; > + if (len <=3D 0) { > + conn_flag(c, conn, STALLED); > + vu_queue_rewind(vdev, vq, iov_cnt); > + return 0; > + } > + > + conn_flag(c, conn, ~STALLED); > + > + /* Likely, some new data was acked too. */ > + tcp_update_seqack_wnd(c, conn, 0, NULL); > + > + /* initialize headers */ > + iov_used =3D 0; > + num_buffers =3D 0; > + check =3D NULL; > + segment_size =3D 0; > + for (i =3D 0; i < iov_cnt && len; i++) { > + > + if (segment_size =3D=3D 0) > + first =3D &iov_vu[i + 1]; > + > + if (iov_vu[i + 1].iov_len > (size_t)len) > + iov_vu[i + 1].iov_len =3D len; > + > + len -=3D iov_vu[i + 1].iov_len; > + iov_used++; > + > + segment_size +=3D iov_vu[i + 1].iov_len; > + num_buffers++; > + > + if (segment_size >=3D mss || len =3D=3D 0 || > + i + 1 =3D=3D iov_cnt || !has_mrg_rxbuf) { > + char *base =3D (char *)first->iov_base - l2_hdrlen; > + size_t size =3D first->iov_len + l2_hdrlen; > + struct virtio_net_hdr_mrg_rxbuf *vh; > + struct ethhdr *eh; > + size_t l4len; > + > + vh =3D (struct virtio_net_hdr_mrg_rxbuf *)base; > + > + vh->hdr =3D vu_header; > + if (has_mrg_rxbuf) > + vh->num_buffers =3D htole16(num_buffers); > + > + l2_iov[TCP_IOV_TAP].iov_base =3D NULL; > + l2_iov[TCP_IOV_TAP].iov_len =3D 0; > + l2_iov[TCP_IOV_ETH].iov_base =3D base + vnet_hdrlen; > + l2_iov[TCP_IOV_ETH].iov_len =3D sizeof(struct ethhdr); > + > + eh =3D l2_iov[TCP_IOV_ETH].iov_base; Again, this could be done a bit more neatly using IOV_OF_LVALUE(). Also... IIUC, the only purpose of l2_iov[] here is to communicate the various pieces of the packet to the header initiailizing functions. That seems like a poor choice compared to specifically typed pointers for each piece (which, yes, I realise would mean a bunch of changes in the existing code). > + memcpy(eh->h_dest, c->mac_guest, sizeof(eh->h_dest)); > + memcpy(eh->h_source, c->mac, sizeof(eh->h_source)); > + > + /* initialize header */ > + if (v4) { > + struct tcp_payload_t *payload; > + struct iphdr *iph; > + > + l2_iov[TCP_IOV_IP].iov_base =3D (char *)l2_iov[TCP_IOV_ETH].iov_base= + > + l2_iov[TCP_IOV_ETH].iov_len; > + l2_iov[TCP_IOV_IP].iov_len =3D sizeof(struct iphdr); > + l2_iov[TCP_IOV_PAYLOAD].iov_base =3D (char *)l2_iov[TCP_IOV_IP].iov_= base + > + l2_iov[TCP_IOV_IP].iov_len; > + > + > + eh->h_proto =3D htons(ETH_P_IP); > + > + iph =3D l2_iov[TCP_IOV_IP].iov_base; > + *iph =3D (struct iphdr)L2_BUF_IP4_INIT(IPPROTO_TCP); > + payload =3D l2_iov[TCP_IOV_PAYLOAD].iov_base; > + payload->th =3D (struct tcphdr){ > + .doff =3D offsetof(struct tcp_payload_t, data) / 4, > + .ack =3D 1 > + }; > + > + l4len =3D tcp_l2_buf_fill_headers(c, conn, l2_iov, > + segment_size, > + len ? check : NULL, > + conn->seq_to_tap); > + l2_iov[TCP_IOV_PAYLOAD].iov_len =3D l4len; > + > + if (*c->pcap) { > + uint32_t sum =3D proto_ipv4_header_psum(l4len, > + IPPROTO_TCP, > + /* cppcheck-suppress unknownEvaluationOrder */ > + (struct in_addr){ .s_addr =3D iph->saddr }, > + (struct in_addr){ .s_addr =3D iph->daddr }); > + > + first->iov_base =3D &payload->th; > + first->iov_len =3D size - l2_hdrlen + sizeof(struct tcphdr); > + payload->th.check =3D 0; > + payload->th.check =3D csum_iov(first, num_buffers, sum); > + } > + > + check =3D &iph->check; > + } else { > + struct tcp_payload_t *payload; > + struct ipv6hdr *ip6h; > + > + l2_iov[TCP_IOV_IP].iov_base =3D (char *)l2_iov[TCP_IOV_ETH].iov_base= + > + l2_iov[TCP_IOV_ETH].iov_len; > + l2_iov[TCP_IOV_IP].iov_len =3D sizeof(struct ipv6hdr); > + l2_iov[TCP_IOV_PAYLOAD].iov_base =3D (char *)l2_iov[TCP_IOV_IP].iov_= base + > + l2_iov[TCP_IOV_IP].iov_len; > + > + > + eh->h_proto =3D htons(ETH_P_IPV6); > + > + ip6h =3D l2_iov[TCP_IOV_IP].iov_base; > + *ip6h =3D (struct ipv6hdr)L2_BUF_IP6_INIT(IPPROTO_TCP); > + > + payload =3D l2_iov[TCP_IOV_PAYLOAD].iov_base; > + payload->th =3D (struct tcphdr){ > + .doff =3D offsetof(struct tcp_payload_t, data) / 4, > + .ack =3D 1 > + }; > +; > + l4len =3D tcp_l2_buf_fill_headers(c, conn, l2_iov, > + segment_size, > + NULL, conn->seq_to_tap); > + l2_iov[TCP_IOV_PAYLOAD].iov_len =3D l4len; > + > + if (*c->pcap) { > + uint32_t sum =3D proto_ipv6_header_psum(l4len, > + IPPROTO_TCP, > + &ip6h->saddr, > + &ip6h->daddr); > + > + first->iov_base =3D &payload->th; > + first->iov_len =3D size - l2_hdrlen + sizeof(struct tcphdr); > + > + payload->th.check =3D 0; > + payload->th.check =3D csum_iov(first, num_buffers, sum); > + } > + } > + > + /* set iov for pcap logging */ > + first->iov_base =3D eh; > + first->iov_len =3D size - vnet_hdrlen; > + > + pcap_iov(first, num_buffers); > + > + /* set iov_len for vu_queue_fill_by_index(); */ > + > + first->iov_base =3D base; > + first->iov_len =3D size; > + > + conn->seq_to_tap +=3D segment_size; > + > + segment_size =3D 0; > + num_buffers =3D 0; > + } > + } > + > + /* release unused buffers */ > + vu_queue_rewind(vdev, vq, iov_cnt - iov_used); > + > + /* send packets */ > + for (i =3D 0; i < iov_used; i++) > + vu_queue_fill(vdev, vq, &elem[i], iov_vu[i + 1].iov_len, i); > + > + vu_queue_flush(vdev, vq, iov_used); > + vu_queue_notify(vdev, vq); > + > + conn_flag(c, conn, ACK_FROM_TAP_DUE); > + > + return 0; > +err: > + vu_queue_rewind(vdev, vq, iov_cnt); > + > + if (errno !=3D EAGAIN && errno !=3D EWOULDBLOCK) { > + ret =3D -errno; > + tcp_rst(c, conn); > + } > + > + return ret; > +} > diff --git a/tcp_vu.h b/tcp_vu.h > new file mode 100644 > index 000000000000..b8c57a543ed5 > --- /dev/null > +++ b/tcp_vu.h > @@ -0,0 +1,9 @@ > +// SPDX-License-Identifier: GPL-2.0-or-later > + > +#ifndef TCP_VU_H > +#define TCP_VU_H > + > +int tcp_vu_send_flag(struct ctx *c, struct tcp_tap_conn *conn, int flags= ); > +int tcp_vu_data_from_sock(struct ctx *c, struct tcp_tap_conn *conn); > + > +#endif /*TCP_VU_H */ > diff --git a/udp.c b/udp.c > index dba75d7fecbd..90d58b691c83 100644 > --- a/udp.c > +++ b/udp.c > @@ -121,9 +121,7 @@ > #include "tap.h" > #include "pcap.h" > #include "log.h" > - > -#define UDP_CONN_TIMEOUT 180 /* s, timeout for ephemeral or local bind */ > -#define UDP_MAX_FRAMES 32 /* max # of frames to receive at once */ > +#include "udp_internal.h" > =20 > /** > * struct udp_tap_port - Port tracking based on tap-facing source port > @@ -171,20 +169,8 @@ static uint8_t udp_act[IP_VERSIONS][UDP_ACT_TYPE_MAX= ][DIV_ROUND_UP(NUM_PORTS, 8) > =20 > /* Static buffers */ > =20 > -/** > - * struct udp_payload_t - UDP header and data for inbound messages > - * @uh: UDP header > - * @data: UDP data > - */ > -static struct udp_payload_t { > - struct udphdr uh; > - char data[USHRT_MAX - sizeof(struct udphdr)]; > -#ifdef __AVX2__ > -} __attribute__ ((packed, aligned(32))) > -#else > -} __attribute__ ((packed, aligned(__alignof__(unsigned int)))) > -#endif > -udp_payload[UDP_MAX_FRAMES]; > +/* UDP header and data for inbound messages */ > +static struct udp_payload_t udp_payload[UDP_MAX_FRAMES]; > =20 > /* Ethernet header for IPv4 frames */ > static struct ethhdr udp4_eth_hdr; > @@ -239,11 +225,11 @@ static struct mmsghdr udp6_l2_mh_sock [UDP_MAX_FRA= MES]; > /* recvmmsg()/sendmmsg() data for "spliced" connections */ > static struct iovec udp_iov_splice [UDP_MAX_FRAMES]; > =20 > -static struct sockaddr_in udp4_localname =3D { > +struct sockaddr_in udp4_localname =3D { > .sin_family =3D AF_INET, > .sin_addr =3D IN4ADDR_LOOPBACK_INIT, > }; > -static struct sockaddr_in6 udp6_localname =3D { > +struct sockaddr_in6 udp6_localname =3D { > .sin6_family =3D AF_INET6, > .sin6_addr =3D IN6ADDR_LOOPBACK_INIT, > }; > @@ -564,11 +550,11 @@ static void udp_splice_sendfrom(const struct ctx *c= , unsigned start, unsigned n, > * > * Return: size of IPv4 payload (UDP header + data) > */ > -static size_t udp_update_hdr4(const struct ctx *c, > - struct iphdr *ip4h, const struct sockaddr_in *s_in, > - struct udp_payload_t *bp, > - in_port_t dstport, size_t dlen, > - const struct timespec *now) > +size_t udp_update_hdr4(const struct ctx *c, > + struct iphdr *ip4h, const struct sockaddr_in *s_in, > + struct udp_payload_t *bp, > + in_port_t dstport, size_t dlen, > + const struct timespec *now) > { > const struct in_addr dst =3D c->ip4.addr_seen; > in_port_t srcport =3D ntohs(s_in->sin_port); > @@ -603,7 +589,10 @@ static size_t udp_update_hdr4(const struct ctx *c, > bp->uh.source =3D s_in->sin_port; > bp->uh.dest =3D htons(dstport); > bp->uh.len =3D htons(l4len); > - csum_udp4(&bp->uh, src, dst, bp->data, dlen); > + if (c->mode !=3D MODE_VU) > + csum_udp4(&bp->uh, src, dst, bp->data, dlen); > + else > + bp->uh.check =3D 0; > =20 > return l4len; > } > @@ -620,11 +609,11 @@ static size_t udp_update_hdr4(const struct ctx *c, > * > * Return: size of IPv6 payload (UDP header + data) > */ > -static size_t udp_update_hdr6(const struct ctx *c, > - struct ipv6hdr *ip6h, struct sockaddr_in6 *s_in6, > - struct udp_payload_t *bp, > - in_port_t dstport, size_t dlen, > - const struct timespec *now) > +size_t udp_update_hdr6(const struct ctx *c, > + struct ipv6hdr *ip6h, struct sockaddr_in6 *s_in6, > + struct udp_payload_t *bp, > + in_port_t dstport, size_t dlen, > + const struct timespec *now) > { > const struct in6_addr *src =3D &s_in6->sin6_addr; > const struct in6_addr *dst =3D &c->ip6.addr_seen; > @@ -675,7 +664,10 @@ static size_t udp_update_hdr6(const struct ctx *c, > bp->uh.source =3D s_in6->sin6_port; > bp->uh.dest =3D htons(dstport); > bp->uh.len =3D ip6h->payload_len; > - csum_udp6(&bp->uh, src, dst, bp->data, dlen); > + if (c->mode !=3D MODE_VU) > + csum_udp6(&bp->uh, src, dst, bp->data, dlen); > + else > + bp->uh.check =3D 0xffff; /* zero checksum is invalid with IPv6 */ > =20 > return l4len; > } > diff --git a/udp_internal.h b/udp_internal.h > new file mode 100644 > index 000000000000..898d1e103cb8 > --- /dev/null > +++ b/udp_internal.h > @@ -0,0 +1,39 @@ > +/* SPDX-License-Identifier: GPL-2.0-or-later > + * Copyright (c) 2021 Red Hat GmbH > + * Author: Stefano Brivio > + */ > + > +#ifndef UDP_INTERNAL_H > +#define UDP_INTERNAL_H > + > +#define UDP_CONN_TIMEOUT 180 /* s, timeout for ephemeral or local bind */ > +#define UDP_MAX_FRAMES 32 /* max # of frames to receive at once */ > + > +extern struct sockaddr_in udp4_localname; > +extern struct sockaddr_in6 udp6_localname; > + > +/** > + * struct udp_payload_t - UDP header and data for inbound messages > + * @uh: UDP header > + * @data: UDP data > + */ > +struct udp_payload_t { > + struct udphdr uh; > + char data[USHRT_MAX - sizeof(struct udphdr)]; > +#ifdef __AVX2__ > +} __attribute__ ((packed, aligned(32))); > +#else > +} __attribute__ ((packed, aligned(__alignof__(unsigned int)))); > +#endif > + > +size_t udp_update_hdr4(const struct ctx *c, > + struct iphdr *ip4h, const struct sockaddr_in *s_in, > + struct udp_payload_t *bp, > + in_port_t dstport, size_t dlen, > + const struct timespec *now); > +size_t udp_update_hdr6(const struct ctx *c, > + struct ipv6hdr *ip6h, struct sockaddr_in6 *s_in6, > + struct udp_payload_t *bp, > + in_port_t dstport, size_t dlen, > + const struct timespec *now); > +#endif /* UDP_INTERNAL_H */ > diff --git a/udp_vu.c b/udp_vu.c > new file mode 100644 > index 000000000000..deb649028153 > --- /dev/null > +++ b/udp_vu.c > @@ -0,0 +1,237 @@ > +// SPDX-License-Identifier: GPL-2.0-or-later > + > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > + > +#include "checksum.h" > +#include "util.h" > +#include "ip.h" > +#include "passt.h" > +#include "pcap.h" > +#include "log.h" > +#include "vhost_user.h" > +#include "udp_internal.h" > +#include "udp_vu.h" > + > +/* vhost-user */ > +static const struct virtio_net_hdr vu_header =3D { > + .flags =3D VIRTIO_NET_HDR_F_DATA_VALID, > + .gso_type =3D VIRTIO_NET_HDR_GSO_NONE, > +}; > + > +static struct iovec iov_vu [VIRTQUEUE_MAX_SIZE]; > +static VuVirtqElement elem [VIRTQUEUE_MAX_SIZE]; > +static struct iovec in_sg[VIRTQUEUE_MAX_SIZE]; > +static int in_sg_count; > + > +void udp_vu_sock_handler(const struct ctx *c, union epoll_ref ref, > + uint32_t events, const struct timespec *now) > +{ > + VuDev *vdev =3D (VuDev *)&c->vdev; > + VuVirtq *vq =3D &vdev->vq[VHOST_USER_RX_QUEUE]; > + bool has_mrg_rxbuf, v6 =3D ref.udp.v6; > + in_port_t dstport =3D ref.udp.port; > + size_t l2_hdrlen, vnet_hdrlen; > + struct msghdr msg; > + int i, virtqueue_max; As with TCP, it kind of feels like we should be able to share more of the skeleton of this path. I'm worried about the amount of logic duplication we have, in terms of maintainability. > + > + if (c->no_udp || !(events & EPOLLIN)) > + return; > + > + has_mrg_rxbuf =3D vu_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF); > + if (has_mrg_rxbuf) { > + vnet_hdrlen =3D sizeof(struct virtio_net_hdr_mrg_rxbuf); > + virtqueue_max =3D VIRTQUEUE_MAX_SIZE; > + } else { > + vnet_hdrlen =3D sizeof(struct virtio_net_hdr); > + virtqueue_max =3D 1; > + } > + l2_hdrlen =3D vnet_hdrlen + sizeof(struct ethhdr) + sizeof(struct udphd= r); > + > + if (v6) { > + l2_hdrlen +=3D sizeof(struct ipv6hdr); > + > + udp6_localname.sin6_port =3D htons(dstport); > + msg.msg_name =3D &udp6_localname; > + msg.msg_namelen =3D sizeof(udp6_localname); > + } else { > + l2_hdrlen +=3D sizeof(struct iphdr); > + > + udp4_localname.sin_port =3D htons(dstport); > + msg.msg_name =3D &udp4_localname; > + msg.msg_namelen =3D sizeof(udp4_localname); > + } > + > + msg.msg_control =3D NULL; > + msg.msg_controllen =3D 0; > + msg.msg_flags =3D 0; > + > + for (i =3D 0; i < UDP_MAX_FRAMES; i++) { > + struct virtio_net_hdr_mrg_rxbuf *vh; > + size_t size, fillsize, remaining; > + int iov_cnt, iov_used; > + struct ethhdr *eh; > + ssize_t data_len; > + size_t l4len; > + char *base; > + > + fillsize =3D USHRT_MAX; > + iov_cnt =3D 0; > + in_sg_count =3D 0; > + while (fillsize && iov_cnt < virtqueue_max && > + in_sg_count < ARRAY_SIZE(in_sg)) { > + int ret; > + > + elem[iov_cnt].out_num =3D 0; > + elem[iov_cnt].out_sg =3D NULL; > + elem[iov_cnt].in_num =3D ARRAY_SIZE(in_sg) - in_sg_count; > + elem[iov_cnt].in_sg =3D &in_sg[in_sg_count]; > + ret =3D vu_queue_pop(vdev, vq, &elem[iov_cnt]); > + if (ret < 0) > + break; > + in_sg_count +=3D elem[iov_cnt].in_num; > + > + if (elem[iov_cnt].in_num < 1) { > + err("virtio-net receive queue contains no in buffers"); > + vu_queue_rewind(vdev, vq, iov_cnt); > + return; > + } > + ASSERT(elem[iov_cnt].in_num =3D=3D 1); > + ASSERT(elem[iov_cnt].in_sg[0].iov_len >=3D l2_hdrlen); > + > + if (iov_cnt =3D=3D 0) { > + base =3D elem[iov_cnt].in_sg[0].iov_base; > + size =3D elem[iov_cnt].in_sg[0].iov_len; > + > + /* keep space for the headers */ > + iov_vu[0].iov_base =3D base + l2_hdrlen; > + iov_vu[0].iov_len =3D size - l2_hdrlen; > + } else { > + iov_vu[iov_cnt].iov_base =3D elem[iov_cnt].in_sg[0].iov_base; > + iov_vu[iov_cnt].iov_len =3D elem[iov_cnt].in_sg[0].iov_len; > + } > + > + if (iov_vu[iov_cnt].iov_len > fillsize) > + iov_vu[iov_cnt].iov_len =3D fillsize; > + > + fillsize -=3D iov_vu[iov_cnt].iov_len; > + > + iov_cnt++; > + } > + if (iov_cnt =3D=3D 0) > + break; > + > + msg.msg_iov =3D iov_vu; > + msg.msg_iovlen =3D iov_cnt; > + > + data_len =3D recvmsg(ref.fd, &msg, 0); > + if (data_len < 0) { > + vu_queue_rewind(vdev, vq, iov_cnt); > + return; > + } > + > + /* restore original values */ > + iov_vu[0].iov_base =3D base; > + iov_vu[0].iov_len =3D size; > + > + /* count the numbers of buffer filled by recvmsg() */ > + iov_used =3D iov_count(iov_vu, iov_cnt, l2_hdrlen + data_len, > + &remaining); > + ASSERT(iov_used <=3D iov_cnt); > + if (iov_used > 0) { > + ASSERT(iov_vu[iov_used - 1].iov_len >=3D remaining); > + iov_vu[iov_used - 1].iov_len =3D remaining; > + /* update size */ > + if (iov_used - 1 =3D=3D 0) > + size =3D iov_vu[0].iov_len; > + } > + > + /* release unused buffers */ > + vu_queue_rewind(vdev, vq, iov_cnt - iov_used); > + > + /* vnet_header */ > + vh =3D (struct virtio_net_hdr_mrg_rxbuf *)base; > + vh->hdr =3D vu_header; > + if (has_mrg_rxbuf) > + vh->num_buffers =3D htole16(iov_used); > + > + /* ethernet header */ > + eh =3D (struct ethhdr *)(base + vnet_hdrlen); > + > + memcpy(eh->h_dest, c->mac_guest, sizeof(eh->h_dest)); > + memcpy(eh->h_source, c->mac, sizeof(eh->h_source)); > + > + /* initialize header */ > + if (v6) { > + struct ipv6hdr *ip6h =3D (struct ipv6hdr *)(eh + 1); > + struct udp_payload_t *bp =3D (struct udp_payload_t *)(ip6h + 1); > + > + eh->h_proto =3D htons(ETH_P_IPV6); > + > + *ip6h =3D (struct ipv6hdr)L2_BUF_IP6_INIT(IPPROTO_UDP); > + > + l4len =3D udp_update_hdr6(c, ip6h, &udp6_localname, bp, > + dstport, data_len, now); > + if (*c->pcap) { > + uint32_t sum; > + > + sum =3D proto_ipv6_header_psum(l4len, IPPROTO_UDP, > + &ip6h->saddr, > + &ip6h->daddr); > + > + iov_vu[0].iov_base =3D &bp->uh; > + iov_vu[0].iov_len =3D size - l2_hdrlen + > + sizeof(bp->uh); > + bp->uh.check =3D 0; /* by default, set to 0xffff */ > + bp->uh.check =3D csum_iov(iov_vu, iov_used, sum); > + } > + } else { > + struct iphdr *iph =3D (struct iphdr *)(eh + 1); > + struct udp_payload_t *bp =3D (struct udp_payload_t *)(iph + 1); > + > + eh->h_proto =3D htons(ETH_P_IP); > + > + *iph =3D (struct iphdr)L2_BUF_IP4_INIT(IPPROTO_UDP); > + > + l4len =3D udp_update_hdr4(c, iph, &udp4_localname, bp, > + dstport, data_len, now); > + if (*c->pcap) { > + uint32_t sum; > + > + sum =3D proto_ipv4_header_psum(l4len, IPPROTO_UDP, > + /* cppcheck-suppress unknownEvaluationOrder */ > + (struct in_addr){ .s_addr =3D iph->saddr }, > + (struct in_addr){ .s_addr =3D iph->daddr }); > + > + iov_vu[0].iov_base =3D &bp->uh; > + iov_vu[0].iov_len =3D size - l2_hdrlen + > + sizeof(bp->uh); > + bp->uh.check =3D csum_iov(iov_vu, iov_used, sum); > + } > + } > + > + /* set iov for pcap logging */ > + iov_vu[0].iov_base =3D base + vnet_hdrlen; > + iov_vu[0].iov_len =3D size - vnet_hdrlen; > + pcap_iov(iov_vu, iov_used); > + > + /* set iov_len for vu_queue_fill_by_index(); */ > + iov_vu[0].iov_base =3D base; > + iov_vu[0].iov_len =3D size; > + > + /* send packets */ > + for (i =3D 0; i < iov_used; i++) > + vu_queue_fill(vdev, vq, &elem[i], iov_vu[i].iov_len, i); > + > + vu_queue_flush(vdev, vq, iov_used); > + vu_queue_notify(vdev, vq); > + } > +} > diff --git a/udp_vu.h b/udp_vu.h > new file mode 100644 > index 000000000000..e01ce047ee0a > --- /dev/null > +++ b/udp_vu.h > @@ -0,0 +1,8 @@ > +// SPDX-License-Identifier: GPL-2.0-or-later > + > +#ifndef UDP_VU_H > +#define UDP_VU_H > + > +void udp_vu_sock_handler(const struct ctx *c, union epoll_ref ref, > + uint32_t events, const struct timespec *now); > +#endif /* UDP_VU_H */ > diff --git a/vhost_user.c b/vhost_user.c > index 4ac0a3e53499..a3d156558359 100644 > --- a/vhost_user.c > +++ b/vhost_user.c > @@ -28,7 +28,6 @@ > =20 > #define VHOST_USER_VERSION 1 > =20 > -/* cppcheck-suppress unusedFunction */ > void vu_print_capabilities(void) > { > printf("{\n"); > @@ -332,7 +331,6 @@ static bool map_ring(VuDev *vdev, VuVirtq *vq) > return !(vq->vring.desc && vq->vring.used && vq->vring.avail); > } > =20 > -/* cppcheck-suppress unusedFunction */ > int vu_packet_check_range(void *buf, size_t offset, size_t len, const ch= ar *start, > const char *func, int line) > { > @@ -545,7 +543,6 @@ static int vu_wait_queue(const VuVirtq *vq) > return 0; > } > =20 > -/* cppcheck-suppress unusedFunction */ > int vu_send(const struct ctx *c, const void *buf, size_t size) > { > VuDev *vdev =3D (VuDev *)&c->vdev; > @@ -730,7 +727,6 @@ static void vu_handle_tx(VuDev *vdev, int index) > } > } > =20 > -/* cppcheck-suppress unusedFunction */ > void vu_kick_cb(struct ctx *c, union epoll_ref ref) > { > VuDev *vdev =3D &c->vdev; > @@ -927,7 +923,6 @@ static bool vu_set_vring_enable_exec(VuDev *vdev, str= uct VhostUserMsg *msg) > return false; > } > =20 > -/* cppcheck-suppress unusedFunction */ > void vu_init(struct ctx *c) > { > int i; > @@ -988,7 +983,6 @@ static void vu_cleanup(VuDev *vdev) > * @c: Execution context > * @events: epoll events > */ > -/* cppcheck-suppress unusedFunction */ > void tap_handler_vu(struct ctx *c, uint32_t events) > { > VuDev *dev =3D &c->vdev; > diff --git a/virtio.c b/virtio.c > index 5d58e56204b3..8c651070bba5 100644 > --- a/virtio.c > +++ b/virtio.c > @@ -367,7 +367,6 @@ void vu_queue_unpop(VuDev *dev, VuVirtq *vq, unsigned= int index, size_t len) > vu_queue_detach_element(dev, vq, index, len); > } > =20 > -/* cppcheck-suppress unusedFunction */ > bool vu_queue_rewind(VuDev *dev, VuVirtq *vq, unsigned int num) > { > (void)dev; --=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 --wJBjJjxf1Cnk7fLP Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCAAdFiEEO+dNsU4E3yXUXRK2zQJF27ox2GcFAmZ4/oMACgkQzQJF27ox 2Gec0RAAp41E5bJOatFd5gGiSwKCCXjG3Mghze4XX/gy5fYriJmAZrSU4tOK+nmO 6HeeGiM5J55vZecNi6J0TDkdPf57noZOD67V3SvzIa57ZBuqo+vggHHRQO5K7AwO Jyb31u8gaU4cuYfwTU0zhy2bodYa7B9dCxDbGFn8ru6OIPYA7Qb+oo2Mdroc/ZUO yqmcoU6CB9Btov53KjGwtdFmnwEd1/O3iNfTjiJyVrFEqCSwZiH4vTblij+Q3dxq gkz9aTtUKTvn0DsLNOON9TELK1vNQM+13vuhLH7pIITeEcYTRPU+6hFUg7zCqOFA vdjDI7Og6VzQLbjM5oU6RlL22vd0RK2Pt9FaJabiQOm2ERB7jd1ZUVnz0b0vqlSS Rzm9myZcAbIl/jPnIDXaKTNHTl5cPMxPOC6YmjC1kymMAnPF1OXT3bCEIbddTnZS 8F7G4xQrMZW30+Ji9Eo1VGznpvrRLnsz5TxRUZNeC1QwSaNsKpCbS0WTOE4gPd/r X+z7Ak1U5uKIgyduUTbWRDk5k2A98nMtE+cgRh5Dw5RQj1lJBurswnKiweRR0sx2 WoSGoz7m80tE1BxTnvIbVLxBf08sCu6bH9BiDCXyyEyN+hJeDD0E1uqWS5zuox4q DGrMDUfw6pNHRxV4cc9ImIwXjZKrUGZ60+wqKoZNzbVag3GYoC8= =jkyw -----END PGP SIGNATURE----- --wJBjJjxf1Cnk7fLP--