From 38b50dba4704856194ac02b98e492d2349d64058 Mon Sep 17 00:00:00 2001 From: Stefano Brivio Date: Fri, 23 Apr 2021 22:22:37 +0200 Subject: passt: Spare some syscalls, add some optimisations from profiling Avoid a bunch of syscalls on forwarding paths by: - storing minimum and maximum file descriptor numbers for each protocol, fall back to SO_PROTOCOL query only on overlaps - allocating a larger receive buffer -- this can result in more coalesced packets than sendmmsg() can take (UIO_MAXIOV, i.e. 1024), so make sure we don't exceed that within a single call to protocol tap handlers - nesting the handling loop in tap_handler() in the receive loop, so that we have better chances of filling our receive buffer in fewer calls - skipping the recvfrom() in the UDP handler on EPOLLERR -- there's nothing to be done in that case and while at it: - restore the 20ms timer interval for periodic (TCP) events, I accidentally changed that to 100ms in an earlier commit - attempt using SO_ZEROCOPY for UDP -- if it's not available, sendmmsg() will succeed anyway - fix the handling of the status code from sendmmsg(), if it fails, we'll try to discard the first message, hence return 1 from the UDP handler Signed-off-by: Stefano Brivio --- udp.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) (limited to 'udp.c') diff --git a/udp.c b/udp.c index 7be88f6..edb73de 100644 --- a/udp.c +++ b/udp.c @@ -68,7 +68,8 @@ void udp_sock_handler(struct ctx *c, int s, uint32_t events) struct udphdr *uh; ssize_t n; - (void)events; + if (events == EPOLLERR) + return; n = recvfrom(s, buf + sizeof(*uh), sizeof(buf) - sizeof(*uh), MSG_DONTWAIT, (struct sockaddr *)&sr, &slen); @@ -179,7 +180,11 @@ int udp_tap_handler(struct ctx *c, int af, void *addr, return count; } - return sendmmsg(s, mm, count, MSG_DONTWAIT | MSG_NOSIGNAL); + count = sendmmsg(s, mm, count, MSG_DONTWAIT | MSG_NOSIGNAL | MSG_ZEROCOPY); + if (count < 0) + return 1; + + return count; } /** @@ -191,13 +196,19 @@ int udp_tap_handler(struct ctx *c, int af, void *addr, int udp_sock_init(struct ctx *c) { in_port_t port; - int s; + int s, one = 1; + + c->udp.fd_min = INT_MAX; + c->udp.fd_max = 0; for (port = 0; port < USHRT_MAX; port++) { if (c->v4) { if ((s = sock_l4_add(c, 4, IPPROTO_UDP, port)) < 0) return -1; + setsockopt(s, SOL_SOCKET, SO_ZEROCOPY, + &one, sizeof(one)); + udp4_sock_port[port] = s; } @@ -205,6 +216,9 @@ int udp_sock_init(struct ctx *c) if ((s = sock_l4_add(c, 6, IPPROTO_UDP, port)) < 0) return -1; + setsockopt(s, SOL_SOCKET, SO_ZEROCOPY, + &one, sizeof(one)); + udp6_sock_port[port] = s; } } -- cgit v1.2.3