udp: Better factor IPv4 and IPv6 paths in udp_sock_handler()

Apart from which mh array they're operating on the recvmmsg() calls in
udp_sock_handler() are identical between the IPv4 and IPv6 paths, as are
some of the control structure updates.

By using some local variables to refer to the IP version specific control
arrays, make some more logic common between the IPv4 and IPv6 paths.  As
well as slightly reducing the code size, this makes it less likely that
we'll accidentally use the IPv4 arrays in the IPv6 path or vice versa as we
did in a recently fixed bug.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
This commit is contained in:
David Gibson 2022-11-24 19:54:19 +11:00 committed by Stefano Brivio
parent 6af7ee74cf
commit d533807027

36
udp.c
View file

@ -837,9 +837,10 @@ void udp_sock_handler(const struct ctx *c, union epoll_ref ref, uint32_t events,
const struct timespec *now) const struct timespec *now)
{ {
ssize_t n, msg_len = 0, missing = 0; ssize_t n, msg_len = 0, missing = 0;
struct mmsghdr *tap_mmh, *sock_mmh;
int msg_bufs = 0, msg_i = 0, ret; int msg_bufs = 0, msg_i = 0, ret;
struct mmsghdr *tap_mmh;
struct msghdr *last_mh; struct msghdr *last_mh;
struct iovec *tap_iov;
unsigned int i; unsigned int i;
if (events == EPOLLERR) if (events == EPOLLERR)
@ -851,34 +852,29 @@ void udp_sock_handler(const struct ctx *c, union epoll_ref ref, uint32_t events,
} }
if (ref.r.p.udp.udp.v6) { if (ref.r.p.udp.udp.v6) {
n = recvmmsg(ref.r.s, udp6_l2_mh_sock, UDP_TAP_FRAMES, 0, NULL); tap_mmh = udp6_l2_mh_tap;
if (n <= 0) sock_mmh = udp6_l2_mh_sock;
return; tap_iov = udp6_l2_iov_tap;
} else {
udp6_l2_mh_tap[0].msg_hdr.msg_iov = &udp6_l2_iov_tap[0]; tap_mmh = udp4_l2_mh_tap;
sock_mmh = udp4_l2_mh_sock;
for (i = 0; i < (unsigned)n; i++) { tap_iov = udp4_l2_iov_tap;
udp_sock_fill_data_v6(c, i, ref,
&msg_i, &msg_bufs, &msg_len, now);
} }
udp6_l2_mh_tap[msg_i].msg_hdr.msg_iovlen = msg_bufs; n = recvmmsg(ref.r.s, sock_mmh, UDP_TAP_FRAMES, 0, NULL);
tap_mmh = udp6_l2_mh_tap;
} else {
n = recvmmsg(ref.r.s, udp4_l2_mh_sock, UDP_TAP_FRAMES, 0, NULL);
if (n <= 0) if (n <= 0)
return; return;
udp4_l2_mh_tap[0].msg_hdr.msg_iov = &udp4_l2_iov_tap[0]; tap_mmh[0].msg_hdr.msg_iov = &tap_iov[0];
for (i = 0; i < (unsigned)n; i++) { for (i = 0; i < (unsigned)n; i++) {
if (ref.r.p.udp.udp.v6)
udp_sock_fill_data_v6(c, i, ref,
&msg_i, &msg_bufs, &msg_len, now);
else
udp_sock_fill_data_v4(c, i, ref, udp_sock_fill_data_v4(c, i, ref,
&msg_i, &msg_bufs, &msg_len, now); &msg_i, &msg_bufs, &msg_len, now);
} }
tap_mmh[msg_i].msg_hdr.msg_iovlen = msg_bufs;
udp4_l2_mh_tap[msg_i].msg_hdr.msg_iovlen = msg_bufs;
tap_mmh = udp4_l2_mh_tap;
}
if (c->mode == MODE_PASTA) if (c->mode == MODE_PASTA)
return; return;