aboutgitcodebugslistschat
path: root/udp.c
diff options
context:
space:
mode:
authorDavid Gibson <david@gibson.dropbear.id.au>2024-03-06 16:34:23 +1100
committerStefano Brivio <sbrivio@redhat.com>2024-03-13 14:36:59 +0100
commit205b140dec039437f48766427ed05a302d3b3ffa (patch)
tree9c26e15ef80007f955c2732c8cfa721dde3350b1 /udp.c
parent860d2764dd7ce683c0cfeec1ed60dee0c1f2b443 (diff)
downloadpasst-205b140dec039437f48766427ed05a302d3b3ffa.tar
passt-205b140dec039437f48766427ed05a302d3b3ffa.tar.gz
passt-205b140dec039437f48766427ed05a302d3b3ffa.tar.bz2
passt-205b140dec039437f48766427ed05a302d3b3ffa.tar.lz
passt-205b140dec039437f48766427ed05a302d3b3ffa.tar.xz
passt-205b140dec039437f48766427ed05a302d3b3ffa.tar.zst
passt-205b140dec039437f48766427ed05a302d3b3ffa.zip
udp: Refactor udp_sock[46]_iov_init()
Each of these functions have 3 essentially identical loops in a row. Merge the loops into a single common udp_sock_iov_init() function, calling udp_sock[46]_iov_init_one() helpers to initialize each "slot" in the various parallel arrays. This is slightly neater now, and more naturally allows changes we want to make where more initialization will become common between IPv4 and IPv6. Signed-off-by: David Gibson <david@gibson.dropbear.id.au> Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
Diffstat (limited to 'udp.c')
-rw-r--r--udp.c102
1 files changed, 50 insertions, 52 deletions
diff --git a/udp.c b/udp.c
index 1f46afb..df5f3c4 100644
--- a/udp.c
+++ b/udp.c
@@ -294,72 +294,74 @@ void udp_update_l2_buf(const unsigned char *eth_d, const unsigned char *eth_s)
}
/**
- * udp_sock4_iov_init() - Initialise scatter-gather L2 buffers for IPv4 sockets
+ * udp_sock4_iov_init_one() - Initialise a scatter-gather L2 buffer for IPv4
* @c: Execution context
+ * @i: Index of buffer to initialize
*/
-static void udp_sock4_iov_init(const struct ctx *c)
+static void udp_sock4_iov_init_one(const struct ctx *c, size_t i)
{
- struct mmsghdr *h;
- int i;
-
- for (i = 0; i < ARRAY_SIZE(udp4_l2_buf); i++) {
- udp4_l2_buf[i] = (struct udp4_l2_buf_t) {
- .taph = TAP_HDR_INIT(ETH_P_IP),
- .iph = L2_BUF_IP4_INIT(IPPROTO_UDP)
- };
- }
-
- for (i = 0, h = udp4_l2_mh_sock; i < UDP_MAX_FRAMES; i++, h++) {
- struct msghdr *mh = &h->msg_hdr;
+ struct msghdr *mh = &udp4_l2_mh_sock[i].msg_hdr;
+ struct udp4_l2_buf_t *buf = &udp4_l2_buf[i];
+ struct iovec *siov = &udp4_l2_iov_sock[i];
+ struct iovec *tiov = &udp4_l2_iov_tap[i];
- mh->msg_name = &udp4_l2_buf[i].s_in;
- mh->msg_namelen = sizeof(udp4_l2_buf[i].s_in);
+ *buf = (struct udp4_l2_buf_t) {
+ .taph = TAP_HDR_INIT(ETH_P_IP),
+ .iph = L2_BUF_IP4_INIT(IPPROTO_UDP)
+ };
- udp4_l2_iov_sock[i].iov_base = udp4_l2_buf[i].data;
- udp4_l2_iov_sock[i].iov_len = sizeof(udp4_l2_buf[i].data);
- mh->msg_iov = &udp4_l2_iov_sock[i];
- mh->msg_iovlen = 1;
- }
+ siov->iov_base = buf->data;
+ siov->iov_len = sizeof(buf->data);
- for (i = 0; i < UDP_MAX_FRAMES; i++) {
- struct iovec *iov = &udp4_l2_iov_tap[i];
+ mh->msg_name = &buf->s_in;
+ mh->msg_namelen = sizeof(buf->s_in);
+ mh->msg_iov = siov;
+ mh->msg_iovlen = 1;
- iov->iov_base = tap_iov_base(c, &udp4_l2_buf[i].taph);
- }
+ tiov->iov_base = tap_iov_base(c, &buf->taph);
}
/**
- * udp_sock6_iov_init() - Initialise scatter-gather L2 buffers for IPv6 sockets
+ * udp_sock6_iov_init_one() - Initialise a scatter-gather L2 buffer for IPv6
* @c: Execution context
+ * @i: Index of buffer to initialize
*/
-static void udp_sock6_iov_init(const struct ctx *c)
+static void udp_sock6_iov_init_one(const struct ctx *c, size_t i)
{
- struct mmsghdr *h;
- int i;
+ struct msghdr *mh = &udp6_l2_mh_sock[i].msg_hdr;
+ struct udp6_l2_buf_t *buf = &udp6_l2_buf[i];
+ struct iovec *siov = &udp6_l2_iov_sock[i];
+ struct iovec *tiov = &udp6_l2_iov_tap[i];
- for (i = 0; i < ARRAY_SIZE(udp6_l2_buf); i++) {
- udp6_l2_buf[i] = (struct udp6_l2_buf_t) {
- .taph = TAP_HDR_INIT(ETH_P_IPV6),
- .ip6h = L2_BUF_IP6_INIT(IPPROTO_UDP)
- };
- }
+ *buf = (struct udp6_l2_buf_t) {
+ .taph = TAP_HDR_INIT(ETH_P_IPV6),
+ .ip6h = L2_BUF_IP6_INIT(IPPROTO_UDP)
+ };
- for (i = 0, h = udp6_l2_mh_sock; i < UDP_MAX_FRAMES; i++, h++) {
- struct msghdr *mh = &h->msg_hdr;
+ siov->iov_base = buf->data;
+ siov->iov_len = sizeof(buf->data);
- mh->msg_name = &udp6_l2_buf[i].s_in6;
- mh->msg_namelen = sizeof(struct sockaddr_in6);
+ mh->msg_name = &buf->s_in6;
+ mh->msg_namelen = sizeof(buf->s_in6);
+ mh->msg_iov = siov;
+ mh->msg_iovlen = 1;
- udp6_l2_iov_sock[i].iov_base = udp6_l2_buf[i].data;
- udp6_l2_iov_sock[i].iov_len = sizeof(udp6_l2_buf[i].data);
- mh->msg_iov = &udp6_l2_iov_sock[i];
- mh->msg_iovlen = 1;
- }
+ tiov->iov_base = tap_iov_base(c, &buf->taph);
+}
- for (i = 0; i < UDP_MAX_FRAMES; i++) {
- struct iovec *iov = &udp6_l2_iov_tap[i];
+/**
+ * udp_sock_iov_init() - Initialise scatter-gather L2 buffers
+ * @c: Execution context
+ */
+static void udp_sock_iov_init(const struct ctx *c)
+{
+ size_t i;
- iov->iov_base = tap_iov_base(c, &udp6_l2_buf[i].taph);
+ for (i = 0; i < UDP_MAX_FRAMES; i++) {
+ if (c->ifi4)
+ udp_sock4_iov_init_one(c, i);
+ if (c->ifi6)
+ udp_sock6_iov_init_one(c, i);
}
}
@@ -1230,11 +1232,7 @@ v6:
*/
int udp_init(struct ctx *c)
{
- if (c->ifi4)
- udp_sock4_iov_init(c);
-
- if (c->ifi6)
- udp_sock6_iov_init(c);
+ udp_sock_iov_init(c);
udp_invert_portmap(&c->udp.fwd_in);
udp_invert_portmap(&c->udp.fwd_out);