iov: Helper macro to construct iovs covering existing variables or fields

Laurent's recent changes mean we use IO vectors much more heavily in the
TCP code.  In many of those cases, and few others around the code base,
individual iovs of these vectors are constructed to exactly cover existing
variables or fields.  We can make initializing such iovs shorter and
clearer with a macro for the purpose.

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 2024-05-01 16:53:52 +10:00 committed by Stefano Brivio
parent 40f8b2976a
commit 3559899586
4 changed files with 16 additions and 21 deletions

3
iov.h
View file

@ -18,6 +18,9 @@
#include <unistd.h> #include <unistd.h>
#include <string.h> #include <string.h>
#define IOV_OF_LVALUE(lval) \
(struct iovec){ .iov_base = &(lval), .iov_len = sizeof(lval) }
size_t iov_skip_bytes(const struct iovec *iov, size_t n, size_t iov_skip_bytes(const struct iovec *iov, size_t n,
size_t skip, size_t *offset); size_t skip, size_t *offset);
size_t iov_from_buf(const struct iovec *iov, size_t iov_cnt, size_t iov_from_buf(const struct iovec *iov, size_t iov_cnt,

3
tap.c
View file

@ -79,8 +79,7 @@ void tap_send_single(const struct ctx *c, const void *data, size_t l2len)
size_t iovcnt = 0; size_t iovcnt = 0;
if (c->mode == MODE_PASST) { if (c->mode == MODE_PASST) {
iov[iovcnt].iov_base = &vnet_len; iov[iovcnt] = IOV_OF_LVALUE(vnet_len);
iov[iovcnt].iov_len = sizeof(vnet_len);
iovcnt++; iovcnt++;
} }

24
tcp.c
View file

@ -290,6 +290,7 @@
#include "checksum.h" #include "checksum.h"
#include "util.h" #include "util.h"
#include "iov.h"
#include "ip.h" #include "ip.h"
#include "passt.h" #include "passt.h"
#include "tap.h" #include "tap.h"
@ -954,10 +955,8 @@ static void tcp_sock4_iov_init(const struct ctx *c)
iov = tcp4_l2_iov[i]; iov = tcp4_l2_iov[i];
iov[TCP_IOV_TAP] = tap_hdr_iov(c, &tcp4_payload_tap_hdr[i]); iov[TCP_IOV_TAP] = tap_hdr_iov(c, &tcp4_payload_tap_hdr[i]);
iov[TCP_IOV_ETH].iov_base = &tcp4_eth_src; iov[TCP_IOV_ETH] = IOV_OF_LVALUE(tcp4_eth_src);
iov[TCP_IOV_ETH].iov_len = sizeof(tcp4_eth_src); iov[TCP_IOV_IP] = IOV_OF_LVALUE(tcp4_payload_ip[i]);
iov[TCP_IOV_IP].iov_base = &tcp4_payload_ip[i];
iov[TCP_IOV_IP].iov_len = sizeof(tcp4_payload_ip[i]);
iov[TCP_IOV_PAYLOAD].iov_base = &tcp4_payload[i]; iov[TCP_IOV_PAYLOAD].iov_base = &tcp4_payload[i];
} }
@ -966,9 +965,8 @@ static void tcp_sock4_iov_init(const struct ctx *c)
iov[TCP_IOV_TAP] = tap_hdr_iov(c, &tcp4_flags_tap_hdr[i]); iov[TCP_IOV_TAP] = tap_hdr_iov(c, &tcp4_flags_tap_hdr[i]);
iov[TCP_IOV_ETH].iov_base = &tcp4_eth_src; iov[TCP_IOV_ETH].iov_base = &tcp4_eth_src;
iov[TCP_IOV_ETH].iov_len = sizeof(tcp4_eth_src); iov[TCP_IOV_ETH] = IOV_OF_LVALUE(tcp4_eth_src);
iov[TCP_IOV_IP].iov_base = &tcp4_flags_ip[i]; iov[TCP_IOV_IP] = IOV_OF_LVALUE(tcp4_flags_ip[i]);
iov[TCP_IOV_IP].iov_len = sizeof(tcp4_flags_ip[i]);
iov[TCP_IOV_PAYLOAD].iov_base = &tcp4_flags[i]; iov[TCP_IOV_PAYLOAD].iov_base = &tcp4_flags[i];
} }
} }
@ -1001,10 +999,8 @@ static void tcp_sock6_iov_init(const struct ctx *c)
iov = tcp6_l2_iov[i]; iov = tcp6_l2_iov[i];
iov[TCP_IOV_TAP] = tap_hdr_iov(c, &tcp6_payload_tap_hdr[i]); iov[TCP_IOV_TAP] = tap_hdr_iov(c, &tcp6_payload_tap_hdr[i]);
iov[TCP_IOV_ETH].iov_base = &tcp6_eth_src; iov[TCP_IOV_ETH] = IOV_OF_LVALUE(tcp6_eth_src);
iov[TCP_IOV_ETH].iov_len = sizeof(tcp6_eth_src); iov[TCP_IOV_IP] = IOV_OF_LVALUE(tcp6_payload_ip[i]);
iov[TCP_IOV_IP].iov_base = &tcp6_payload_ip[i];
iov[TCP_IOV_IP].iov_len = sizeof(tcp6_payload_ip[i]);
iov[TCP_IOV_PAYLOAD].iov_base = &tcp6_payload[i]; iov[TCP_IOV_PAYLOAD].iov_base = &tcp6_payload[i];
} }
@ -1012,10 +1008,8 @@ static void tcp_sock6_iov_init(const struct ctx *c)
iov = tcp6_l2_flags_iov[i]; iov = tcp6_l2_flags_iov[i];
iov[TCP_IOV_TAP] = tap_hdr_iov(c, &tcp6_flags_tap_hdr[i]); iov[TCP_IOV_TAP] = tap_hdr_iov(c, &tcp6_flags_tap_hdr[i]);
iov[TCP_IOV_ETH].iov_base = &tcp6_eth_src; iov[TCP_IOV_ETH] = IOV_OF_LVALUE(tcp6_eth_src);
iov[TCP_IOV_ETH].iov_len = sizeof(tcp6_eth_src); iov[TCP_IOV_IP] = IOV_OF_LVALUE(tcp6_flags_ip[i]);
iov[TCP_IOV_IP].iov_base = &tcp6_flags_ip[i];
iov[TCP_IOV_IP].iov_len = sizeof(tcp6_flags_ip[i]);
iov[TCP_IOV_PAYLOAD].iov_base = &tcp6_flags[i]; iov[TCP_IOV_PAYLOAD].iov_base = &tcp6_flags[i];
} }
} }

7
udp.c
View file

@ -113,6 +113,7 @@
#include "checksum.h" #include "checksum.h"
#include "util.h" #include "util.h"
#include "iov.h"
#include "ip.h" #include "ip.h"
#include "siphash.h" #include "siphash.h"
#include "inany.h" #include "inany.h"
@ -315,8 +316,7 @@ static void udp_sock4_iov_init_one(const struct ctx *c, size_t i)
.iph = L2_BUF_IP4_INIT(IPPROTO_UDP) .iph = L2_BUF_IP4_INIT(IPPROTO_UDP)
}; };
siov->iov_base = buf->data; *siov = IOV_OF_LVALUE(buf->data);
siov->iov_len = sizeof(buf->data);
mh->msg_name = &buf->s_in; mh->msg_name = &buf->s_in;
mh->msg_namelen = sizeof(buf->s_in); mh->msg_namelen = sizeof(buf->s_in);
@ -343,8 +343,7 @@ static void udp_sock6_iov_init_one(const struct ctx *c, size_t i)
.ip6h = L2_BUF_IP6_INIT(IPPROTO_UDP) .ip6h = L2_BUF_IP6_INIT(IPPROTO_UDP)
}; };
siov->iov_base = buf->data; *siov = IOV_OF_LVALUE(buf->data);
siov->iov_len = sizeof(buf->data);
mh->msg_name = &buf->s_in6; mh->msg_name = &buf->s_in6;
mh->msg_namelen = sizeof(buf->s_in6); mh->msg_namelen = sizeof(buf->s_in6);