public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
From: David Gibson <david@gibson.dropbear.id.au>
To: Stefano Brivio <sbrivio@redhat.com>,
	Laurent Vivier <lvivier@redhat.com>,
	passt-dev@passt.top
Cc: David Gibson <david@gibson.dropbear.id.au>
Subject: [PATCH 5/6] udp: Avoid unnecessary pointer in udp_update_hdr4()
Date: Thu, 29 Feb 2024 19:42:49 +1100	[thread overview]
Message-ID: <20240229084250.3202450-6-david@gibson.dropbear.id.au> (raw)
In-Reply-To: <20240229084250.3202450-1-david@gibson.dropbear.id.au>

We carry around the source address as a pointer to a constant struct
in_addr.  But it's silly to carry around a 4 or 8 byte pointer to a 4 byte
IPv4 address.  Just copy the IPv4 address around by value.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
---
 udp.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/udp.c b/udp.c
index 0cca9531..a0080b1a 100644
--- a/udp.c
+++ b/udp.c
@@ -588,30 +588,30 @@ static size_t udp_update_hdr4(const struct ctx *c, struct udp4_l2_buf_t *b,
 			      const struct timespec *now)
 {
 	size_t ip_len = datalen + sizeof(b->iph) + sizeof(b->uh);
-	const struct in_addr *src = &b->s_in.sin_addr;
 	in_port_t srcport = ntohs(b->s_in.sin_port);
+	struct in_addr src = b->s_in.sin_addr;
 
 	if (!IN4_IS_ADDR_UNSPECIFIED(&c->ip4.dns_match) &&
-	    IN4_ARE_ADDR_EQUAL(src, &c->ip4.dns_host) && srcport == 53) {
-		src = &c->ip4.dns_match;
-	} else if (IN4_IS_ADDR_LOOPBACK(src) ||
-		   IN4_ARE_ADDR_EQUAL(src, &c->ip4.addr_seen)) {
+	    IN4_ARE_ADDR_EQUAL(&src, &c->ip4.dns_host) && srcport == 53) {
+		src = c->ip4.dns_match;
+	} else if (IN4_IS_ADDR_LOOPBACK(&src) ||
+		   IN4_ARE_ADDR_EQUAL(&src, &c->ip4.addr_seen)) {
 		udp_tap_map[V4][srcport].ts = now->tv_sec;
 		udp_tap_map[V4][srcport].flags |= PORT_LOCAL;
 
-		if (IN4_IS_ADDR_LOOPBACK(src))
+		if (IN4_IS_ADDR_LOOPBACK(&src))
 			udp_tap_map[V4][srcport].flags |= PORT_LOOPBACK;
 		else
 			udp_tap_map[V4][srcport].flags &= ~PORT_LOOPBACK;
 
 		bitmap_set(udp_act[V4][UDP_ACT_TAP], srcport);
 
-		src = &c->ip4.gw;
+		src = c->ip4.gw;
 	}
 
 	b->iph.tot_len = htons(ip_len);
 	b->iph.daddr = c->ip4.addr_seen.s_addr;
-	b->iph.saddr = src->s_addr;
+	b->iph.saddr = src.s_addr;
 	udp_update_check4(b);
 	b->uh.source = htons(srcport);
 	b->uh.dest = htons(dstport);
-- 
@@ -588,30 +588,30 @@ static size_t udp_update_hdr4(const struct ctx *c, struct udp4_l2_buf_t *b,
 			      const struct timespec *now)
 {
 	size_t ip_len = datalen + sizeof(b->iph) + sizeof(b->uh);
-	const struct in_addr *src = &b->s_in.sin_addr;
 	in_port_t srcport = ntohs(b->s_in.sin_port);
+	struct in_addr src = b->s_in.sin_addr;
 
 	if (!IN4_IS_ADDR_UNSPECIFIED(&c->ip4.dns_match) &&
-	    IN4_ARE_ADDR_EQUAL(src, &c->ip4.dns_host) && srcport == 53) {
-		src = &c->ip4.dns_match;
-	} else if (IN4_IS_ADDR_LOOPBACK(src) ||
-		   IN4_ARE_ADDR_EQUAL(src, &c->ip4.addr_seen)) {
+	    IN4_ARE_ADDR_EQUAL(&src, &c->ip4.dns_host) && srcport == 53) {
+		src = c->ip4.dns_match;
+	} else if (IN4_IS_ADDR_LOOPBACK(&src) ||
+		   IN4_ARE_ADDR_EQUAL(&src, &c->ip4.addr_seen)) {
 		udp_tap_map[V4][srcport].ts = now->tv_sec;
 		udp_tap_map[V4][srcport].flags |= PORT_LOCAL;
 
-		if (IN4_IS_ADDR_LOOPBACK(src))
+		if (IN4_IS_ADDR_LOOPBACK(&src))
 			udp_tap_map[V4][srcport].flags |= PORT_LOOPBACK;
 		else
 			udp_tap_map[V4][srcport].flags &= ~PORT_LOOPBACK;
 
 		bitmap_set(udp_act[V4][UDP_ACT_TAP], srcport);
 
-		src = &c->ip4.gw;
+		src = c->ip4.gw;
 	}
 
 	b->iph.tot_len = htons(ip_len);
 	b->iph.daddr = c->ip4.addr_seen.s_addr;
-	b->iph.saddr = src->s_addr;
+	b->iph.saddr = src.s_addr;
 	udp_update_check4(b);
 	b->uh.source = htons(srcport);
 	b->uh.dest = htons(dstport);
-- 
2.44.0


  parent reply	other threads:[~2024-02-29  8:43 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-29  8:42 [PATCH 0/6] udp: Small cleanups David Gibson
2024-02-29  8:42 ` [PATCH 1/6] udp: Refactor udp_sock[46]_iov_init() David Gibson
2024-02-29  8:42 ` [PATCH 2/6] udp: Tweak interface to udp_update_hdr[46] David Gibson
2024-02-29  8:42 ` [PATCH 3/6] udp: Clarify setting of addresses inin udp_update_hdr[46]() David Gibson
2024-02-29  8:42 ` [PATCH 4/6] udp: Consistent port variable names in udp_update_hdr[46] David Gibson
2024-02-29  8:42 ` David Gibson [this message]
2024-02-29  8:42 ` [PATCH 6/6] udp: Clean up UDP checksum generation for inbound IPv6 packets David Gibson

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=20240229084250.3202450-6-david@gibson.dropbear.id.au \
    --to=david@gibson.dropbear.id.au \
    --cc=lvivier@redhat.com \
    --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).