From: David Gibson <david@gibson.dropbear.id.au>
To: Stefano Brivio <sbrivio@redhat.com>, passt-dev@passt.top
Cc: David Gibson <david@gibson.dropbear.id.au>
Subject: [PATCH v2 3/6] udp: Pass data length explicitly to to udp_update_hdr[46]
Date: Wed, 6 Mar 2024 16:34:25 +1100 [thread overview]
Message-ID: <20240306053428.1176129-4-david@gibson.dropbear.id.au> (raw)
In-Reply-To: <20240306053428.1176129-1-david@gibson.dropbear.id.au>
These functions take an index to the L2 buffer whose header information to
update. They use that for two things: to locate the buffer pointer itself,
and to retrieve the length of the received message from the paralllel
udp[46]_l2_mh_sock array. The latter is arguably a failure to separate
concerns. Change these functions to explicitly take a buffer pointer and
payload length as parameters.
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
---
udp.c | 28 ++++++++++++++++------------
1 file changed, 16 insertions(+), 12 deletions(-)
diff --git a/udp.c b/udp.c
index 79f0a63c..e20f537a 100644
--- a/udp.c
+++ b/udp.c
@@ -563,21 +563,22 @@ static void udp_splice_sendfrom(const struct ctx *c, unsigned start, unsigned n,
/**
* udp_update_hdr4() - Update headers for one IPv4 datagram
* @c: Execution context
- * @n: Index of buffer in udp4_l2_buf pool
+ * @b: Pointer to udp4_l2_buf to update
* @dstport: Destination port number
+ * @datalen: Length of UDP payload
* @now: Current timestamp
*
* Return: size of tap frame with headers
*/
-static size_t udp_update_hdr4(const struct ctx *c, int n, in_port_t dstport,
+static size_t udp_update_hdr4(const struct ctx *c, struct udp4_l2_buf_t *b,
+ in_port_t dstport, size_t datalen,
const struct timespec *now)
{
- struct udp4_l2_buf_t *b = &udp4_l2_buf[n];
const struct in_addr *src;
in_port_t srcport;
size_t ip_len;
- ip_len = udp4_l2_mh_sock[n].msg_len + sizeof(b->iph) + sizeof(b->uh);
+ ip_len = datalen + sizeof(b->iph) + sizeof(b->uh);
b->iph.tot_len = htons(ip_len);
b->iph.daddr = c->ip4.addr_seen.s_addr;
@@ -608,7 +609,7 @@ static size_t udp_update_hdr4(const struct ctx *c, int n, in_port_t dstport,
*src, c->ip4.addr_seen);
b->uh.source = b->s_in.sin_port;
b->uh.dest = htons(dstport);
- b->uh.len = htons(udp4_l2_mh_sock[n].msg_len + sizeof(b->uh));
+ b->uh.len = htons(datalen + sizeof(b->uh));
return tap_iov_len(c, &b->taph, ip_len);
}
@@ -616,16 +617,17 @@ static size_t udp_update_hdr4(const struct ctx *c, int n, in_port_t dstport,
/**
* udp_update_hdr6() - Update headers for one IPv6 datagram
* @c: Execution context
- * @n: Index of buffer in udp6_l2_buf pool
+ * @b: Pointer to udp6_l2_buf to update
* @dstport: Destination port number
+ * @datalen: Length of UDP payload
* @now: Current timestamp
*
* Return: size of tap frame with headers
*/
-static size_t udp_update_hdr6(const struct ctx *c, int n, in_port_t dstport,
+static size_t udp_update_hdr6(const struct ctx *c, struct udp6_l2_buf_t *b,
+ in_port_t dstport, size_t datalen,
const struct timespec *now)
{
- struct udp6_l2_buf_t *b = &udp6_l2_buf[n];
const struct in6_addr *src, *dst;
uint16_t payload_len;
in_port_t srcport;
@@ -635,9 +637,9 @@ static size_t udp_update_hdr6(const struct ctx *c, int n, in_port_t dstport,
src = &b->s_in6.sin6_addr;
srcport = ntohs(b->s_in6.sin6_port);
- ip_len = udp6_l2_mh_sock[n].msg_len + sizeof(b->ip6h) + sizeof(b->uh);
+ ip_len = datalen + sizeof(b->ip6h) + sizeof(b->uh);
- payload_len = udp6_l2_mh_sock[n].msg_len + sizeof(b->uh);
+ payload_len = datalen + sizeof(b->uh);
b->ip6h.payload_len = htons(payload_len);
if (IN6_IS_ADDR_LINKLOCAL(src)) {
@@ -716,9 +718,11 @@ static void udp_tap_send(const struct ctx *c,
size_t buf_len;
if (v6)
- buf_len = udp_update_hdr6(c, i, dstport, now);
+ buf_len = udp_update_hdr6(c, &udp6_l2_buf[i], dstport,
+ udp6_l2_mh_sock[i].msg_len, now);
else
- buf_len = udp_update_hdr4(c, i, dstport, now);
+ buf_len = udp_update_hdr4(c, &udp4_l2_buf[i], dstport,
+ udp4_l2_mh_sock[i].msg_len, now);
tap_iov[i].iov_len = buf_len;
}
--
@@ -563,21 +563,22 @@ static void udp_splice_sendfrom(const struct ctx *c, unsigned start, unsigned n,
/**
* udp_update_hdr4() - Update headers for one IPv4 datagram
* @c: Execution context
- * @n: Index of buffer in udp4_l2_buf pool
+ * @b: Pointer to udp4_l2_buf to update
* @dstport: Destination port number
+ * @datalen: Length of UDP payload
* @now: Current timestamp
*
* Return: size of tap frame with headers
*/
-static size_t udp_update_hdr4(const struct ctx *c, int n, in_port_t dstport,
+static size_t udp_update_hdr4(const struct ctx *c, struct udp4_l2_buf_t *b,
+ in_port_t dstport, size_t datalen,
const struct timespec *now)
{
- struct udp4_l2_buf_t *b = &udp4_l2_buf[n];
const struct in_addr *src;
in_port_t srcport;
size_t ip_len;
- ip_len = udp4_l2_mh_sock[n].msg_len + sizeof(b->iph) + sizeof(b->uh);
+ ip_len = datalen + sizeof(b->iph) + sizeof(b->uh);
b->iph.tot_len = htons(ip_len);
b->iph.daddr = c->ip4.addr_seen.s_addr;
@@ -608,7 +609,7 @@ static size_t udp_update_hdr4(const struct ctx *c, int n, in_port_t dstport,
*src, c->ip4.addr_seen);
b->uh.source = b->s_in.sin_port;
b->uh.dest = htons(dstport);
- b->uh.len = htons(udp4_l2_mh_sock[n].msg_len + sizeof(b->uh));
+ b->uh.len = htons(datalen + sizeof(b->uh));
return tap_iov_len(c, &b->taph, ip_len);
}
@@ -616,16 +617,17 @@ static size_t udp_update_hdr4(const struct ctx *c, int n, in_port_t dstport,
/**
* udp_update_hdr6() - Update headers for one IPv6 datagram
* @c: Execution context
- * @n: Index of buffer in udp6_l2_buf pool
+ * @b: Pointer to udp6_l2_buf to update
* @dstport: Destination port number
+ * @datalen: Length of UDP payload
* @now: Current timestamp
*
* Return: size of tap frame with headers
*/
-static size_t udp_update_hdr6(const struct ctx *c, int n, in_port_t dstport,
+static size_t udp_update_hdr6(const struct ctx *c, struct udp6_l2_buf_t *b,
+ in_port_t dstport, size_t datalen,
const struct timespec *now)
{
- struct udp6_l2_buf_t *b = &udp6_l2_buf[n];
const struct in6_addr *src, *dst;
uint16_t payload_len;
in_port_t srcport;
@@ -635,9 +637,9 @@ static size_t udp_update_hdr6(const struct ctx *c, int n, in_port_t dstport,
src = &b->s_in6.sin6_addr;
srcport = ntohs(b->s_in6.sin6_port);
- ip_len = udp6_l2_mh_sock[n].msg_len + sizeof(b->ip6h) + sizeof(b->uh);
+ ip_len = datalen + sizeof(b->ip6h) + sizeof(b->uh);
- payload_len = udp6_l2_mh_sock[n].msg_len + sizeof(b->uh);
+ payload_len = datalen + sizeof(b->uh);
b->ip6h.payload_len = htons(payload_len);
if (IN6_IS_ADDR_LINKLOCAL(src)) {
@@ -716,9 +718,11 @@ static void udp_tap_send(const struct ctx *c,
size_t buf_len;
if (v6)
- buf_len = udp_update_hdr6(c, i, dstport, now);
+ buf_len = udp_update_hdr6(c, &udp6_l2_buf[i], dstport,
+ udp6_l2_mh_sock[i].msg_len, now);
else
- buf_len = udp_update_hdr4(c, i, dstport, now);
+ buf_len = udp_update_hdr4(c, &udp4_l2_buf[i], dstport,
+ udp4_l2_mh_sock[i].msg_len, now);
tap_iov[i].iov_len = buf_len;
}
--
2.44.0
next prev parent reply other threads:[~2024-03-06 5:34 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-03-06 5:34 [PATCH v2 0/6] udp: Small cleanups David Gibson
2024-03-06 5:34 ` [PATCH v2 1/6] udp: Refactor udp_sock[46]_iov_init() David Gibson
2024-03-06 5:34 ` [PATCH v2 2/6] udp: Consistent port variable names in udp_update_hdr[46] David Gibson
2024-03-06 5:34 ` David Gibson [this message]
2024-03-06 5:34 ` [PATCH v2 4/6] udp: Re-order udp_update_hdr[46] for clarity and brevity David Gibson
2024-03-06 5:34 ` [PATCH v2 5/6] udp: Avoid unnecessary pointer in udp_update_hdr4() David Gibson
2024-03-06 5:34 ` [PATCH v2 6/6] udp: Use existing helper for UDP checksum on inbound IPv6 packets David Gibson
2024-03-13 14:21 ` [PATCH v2 0/6] udp: Small cleanups Stefano Brivio
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240306053428.1176129-4-david@gibson.dropbear.id.au \
--to=david@gibson.dropbear.id.au \
--cc=passt-dev@passt.top \
--cc=sbrivio@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://passt.top/passt
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).