From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from gandalf.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) by passt.top (Postfix) with ESMTPS id 06F6D5A0271 for ; Fri, 8 Mar 2024 07:53:30 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202312; t=1709880807; bh=sqsFTXkKFtw+NCcVdi2yRhJZaVyIQTljJcnNtzw/nhk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rZGelzfcswrcVvX1blhvao+RWNHBHxdfnhTwIdg/dnDCYufAGulqvKLhJxhQz0+Qy /vlmaU1UGkuVl7xaan74HJftrgXu8f4g6ItPo0sACnda6UsOJGmzzVuPHfes/umIiY ylLSJvkC05DCXbS/epWmPewYsyGDX9imK/GIWICc9HTD39MnPa7a2Xmp7KQUEoxXtD gY8nY/C0Rxpfpv65nmEHx4xo5c3coGhVaSfJvVZkKZ+KESDTa3K5Wu5gQ2Mwxy/auK 94ZAOrV1mk1ddlM3ls1rSjYsQHqUlJlACnYw6BTxzypk4p0a83PHpi1krhYco7ajes FUdNkwK2fHEAg== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4TrcMC55Lzz4wb0; Fri, 8 Mar 2024 17:53:27 +1100 (AEDT) From: David Gibson To: Stefano Brivio , passt-dev@passt.top Subject: [PATCH 2/4] tap: Simplify some casts in the tap "slow path" functions Date: Fri, 8 Mar 2024 17:53:23 +1100 Message-ID: <20240308065325.2181322-3-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240308065325.2181322-1-david@gibson.dropbear.id.au> References: <20240308065325.2181322-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: LB54SFA4KYFUMUFBEHCOXPYUCKASL4FT X-Message-ID-Hash: LB54SFA4KYFUMUFBEHCOXPYUCKASL4FT X-MailFrom: dgibson@gandalf.ozlabs.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: Laurent Vivier , David Gibson X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: We can both remove some variables which differ from others only in type, and slightly improve type safety. Signed-off-by: David Gibson --- tap.c | 41 ++++++++++++++++++----------------------- 1 file changed, 18 insertions(+), 23 deletions(-) diff --git a/tap.c b/tap.c index f9e2a8d9..38965842 100644 --- a/tap.c +++ b/tap.c @@ -146,11 +146,9 @@ static void *tap_push_l2h(const struct ctx *c, void *buf, uint16_t proto) * * Return: pointer at which to write the packet's payload */ -static void *tap_push_ip4h(char *buf, struct in_addr src, struct in_addr dst, - size_t len, uint8_t proto) +static void *tap_push_ip4h(struct iphdr *ip4h, struct in_addr src, + struct in_addr dst, size_t len, uint8_t proto) { - struct iphdr *ip4h = (struct iphdr *)buf; - ip4h->version = 4; ip4h->ihl = sizeof(struct iphdr) / 4; ip4h->tos = 0; @@ -181,9 +179,8 @@ void tap_udp4_send(const struct ctx *c, struct in_addr src, in_port_t sport, { size_t udplen = len + sizeof(struct udphdr); char buf[USHRT_MAX]; - void *ip4h = tap_push_l2h(c, buf, ETH_P_IP); - void *uhp = tap_push_ip4h(ip4h, src, dst, udplen, IPPROTO_UDP); - struct udphdr *uh = (struct udphdr *)uhp; + struct iphdr *ip4h = tap_push_l2h(c, buf, ETH_P_IP); + struct udphdr *uh = tap_push_ip4h(ip4h, src, dst, udplen, IPPROTO_UDP); char *data = (char *)(uh + 1); uh->source = htons(sport); @@ -208,14 +205,14 @@ void tap_icmp4_send(const struct ctx *c, struct in_addr src, struct in_addr dst, const void *in, size_t len) { char buf[USHRT_MAX]; - void *ip4h = tap_push_l2h(c, buf, ETH_P_IP); - char *data = tap_push_ip4h(ip4h, src, dst, len, IPPROTO_ICMP); - struct icmphdr *icmp4h = (struct icmphdr *)data; + struct iphdr *ip4h = tap_push_l2h(c, buf, ETH_P_IP); + struct icmphdr *icmp4h = tap_push_ip4h(ip4h, src, dst, + len, IPPROTO_ICMP); - memcpy(data, in, len); + memcpy(icmp4h, in, len); csum_icmp4(icmp4h, icmp4h + 1, len - sizeof(*icmp4h)); - if (tap_send(c, buf, len + (data - buf)) < 0) + if (tap_send(c, buf, len + ((char *)icmp4h - buf)) < 0) debug("tap: failed to send %zu bytes (IPv4)", len); } @@ -230,13 +227,11 @@ void tap_icmp4_send(const struct ctx *c, struct in_addr src, struct in_addr dst, * * Return: pointer at which to write the packet's payload */ -static void *tap_push_ip6h(char *buf, +static void *tap_push_ip6h(struct ipv6hdr *ip6h, const struct in6_addr *src, const struct in6_addr *dst, size_t len, uint8_t proto, uint32_t flow) { - struct ipv6hdr *ip6h = (struct ipv6hdr *)buf; - ip6h->payload_len = htons(len); ip6h->priority = 0; ip6h->version = 6; @@ -268,9 +263,9 @@ void tap_udp6_send(const struct ctx *c, { size_t udplen = len + sizeof(struct udphdr); char buf[USHRT_MAX]; - void *ip6h = tap_push_l2h(c, buf, ETH_P_IPV6); - void *uhp = tap_push_ip6h(ip6h, src, dst, udplen, IPPROTO_UDP, flow); - struct udphdr *uh = (struct udphdr *)uhp; + struct ipv6hdr *ip6h = tap_push_l2h(c, buf, ETH_P_IPV6); + struct udphdr *uh = tap_push_ip6h(ip6h, src, dst, + udplen, IPPROTO_UDP, flow); char *data = (char *)(uh + 1); uh->source = htons(sport); @@ -296,14 +291,14 @@ void tap_icmp6_send(const struct ctx *c, const void *in, size_t len) { char buf[USHRT_MAX]; - void *ip6h = tap_push_l2h(c, buf, ETH_P_IPV6); - char *data = tap_push_ip6h(ip6h, src, dst, len, IPPROTO_ICMPV6, 0); - struct icmp6hdr *icmp6h = (struct icmp6hdr *)data; + struct ipv6hdr *ip6h = tap_push_l2h(c, buf, ETH_P_IPV6); + struct icmp6hdr *icmp6h = tap_push_ip6h(ip6h, src, dst, len, + IPPROTO_ICMPV6, 0); - memcpy(data, in, len); + memcpy(icmp6h, in, len); csum_icmp6(icmp6h, src, dst, icmp6h + 1, len - sizeof(*icmp6h)); - if (tap_send(c, buf, len + (data - buf)) < 1) + if (tap_send(c, buf, len + ((char *)icmp6h - buf)) < 1) debug("tap: failed to send %zu bytes (IPv6)", len); } -- 2.44.0