From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from gandalf.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id CD7535A0272 for ; Fri, 8 Sep 2023 03:50:00 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=201602; t=1694137797; bh=b7DbylBg9xReuVjCbE9E9kUwiZpmjtKq4/Zl76AuNyM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ZK4rDxtqj/lxL7x9bGWqJdJ++dhYg3iOGLg878hRKNMcBq3I3I5dHm/FjWpHSICnd O2MjO3TIHYwpsUgr1uuBPpf7EszbThuX42PMfaKM1T3rxx/E4Ymi1fFgkBqGnBniXD JJhmlwAoFvyRxWphIFhMvKj30x74waffamr0eXuA= Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4RhfF10RqXz4xG0; Fri, 8 Sep 2023 11:49:57 +1000 (AEST) From: David Gibson To: passt-dev@passt.top, Stefano Brivio Subject: [PATCH 3/8] tcp: Remove some redundant packet_get() operations Date: Fri, 8 Sep 2023 11:49:48 +1000 Message-ID: <20230908014953.822952-4-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230908014953.822952-1-david@gibson.dropbear.id.au> References: <20230908014953.822952-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: DEAYEWTYSWNXBVUCBHHFDQ7GM7KEHYNO X-Message-ID-Hash: DEAYEWTYSWNXBVUCBHHFDQ7GM7KEHYNO 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: jlesev@gmail.com, 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: Both tcp_data_from_tap() and tcp_tap_handler() call packet_get() to get the entire L4 packet length, then immediately call it again to check that the packet is long enough to include a TCP header. The features of packet_get() let us easily combine these together, we just need to adjust the length slightly, because we want the value to include the TCP header length. Signed-off-by: David Gibson --- tcp.c | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/tcp.c b/tcp.c index d8c2327..6a34f82 100644 --- a/tcp.c +++ b/tcp.c @@ -2320,16 +2320,12 @@ static void tcp_data_from_tap(struct ctx *c, struct tcp_tap_conn *conn, char *data; size_t off; - if (!packet_get(p, i, 0, 0, &len)) { - tcp_rst(c, conn); - return; - } - - th = packet_get(p, i, 0, sizeof(*th), NULL); + th = packet_get(p, i, 0, sizeof(*th), &len); if (!th) { tcp_rst(c, conn); return; } + len += sizeof(*th); off = th->doff * 4UL; if (off < sizeof(*th) || off > len) { @@ -2545,12 +2541,10 @@ int tcp_tap_handler(struct ctx *c, int af, const void *saddr, const void *daddr, int ack_due = 0; char *opts; - if (!packet_get(p, idx, 0, 0, &len)) - return 1; - - th = packet_get(p, idx, 0, sizeof(*th), NULL); + th = packet_get(p, idx, 0, sizeof(*th), &len); if (!th) return 1; + len += sizeof(*th); optlen = th->doff * 4UL - sizeof(*th); /* Static checkers might fail to see this: */ -- 2.41.0