From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: passt.top; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: passt.top; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=abd2l/gd; dkim-atps=neutral Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by passt.top (Postfix) with ESMTP id 5DB6E5A026E for ; Thu, 26 Sep 2024 16:04:29 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1727359466; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=XeC83QDI57Jd9+E+R24+oQjdSMnmyP/laRM29lEfaOE=; b=abd2l/gdfCWpgobeVmrMfvcPMHydMlgkfzTaxQ8f7Btd8DLedrUIRIUD+qp5p4uK8zm5Ce cb0vtREUXrba2150213hqK4w4/hmdRRyHNcKBbbRXsGUzoqd1w/R7134j83yUy604WF5l0 T0l2hQW4xp9e9rj3c4ojIsLRZmzf2pA= Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-153-zsQh8hAvMRSQSwz5is8M-A-1; Thu, 26 Sep 2024 10:04:25 -0400 X-MC-Unique: zsQh8hAvMRSQSwz5is8M-A-1 Received: from mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (unknown [10.30.177.15]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id BD94A1943CC2 for ; Thu, 26 Sep 2024 14:04:24 +0000 (UTC) Received: from lenovo-t14s.redhat.com (unknown [10.39.193.239]) by mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id CDDA419373D7; Thu, 26 Sep 2024 14:04:23 +0000 (UTC) From: Laurent Vivier To: passt-dev@passt.top Subject: [PATCH v4 4/5] tcp: Update TCP checksum using an iovec array Date: Thu, 26 Sep 2024 16:04:14 +0200 Message-ID: <20240926140415.441208-5-lvivier@redhat.com> In-Reply-To: <20240926140415.441208-1-lvivier@redhat.com> References: <20240926140415.441208-1-lvivier@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.15 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true Message-ID-Hash: FXQPYZTVQ5W7II2OBGBGLJCKFMU4HDJZ X-Message-ID-Hash: FXQPYZTVQ5W7II2OBGBGLJCKFMU4HDJZ X-MailFrom: lvivier@redhat.com 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 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: TCP header and payload are supposed to be in the same buffer, and tcp_update_check_tcp4()/tcp_update_check_tcp6() compute the checksum from the base address of the header using the length of the IP payload. In the future (for vhost-user) we need to dispatch the TCP header and the TCP payload through several buffers. To be able to manage that, we provide an iovec array that points to the data of the TCP frame. We provide also an offset to be able to provide an array that contains the TCP frame embedded in an lower level frame, and this offset points to the TCP header inside the iovec array. Signed-off-by: Laurent Vivier --- Notes: v4: - replace die() by err() in tcp_update_check_tcp6() too v3: - replace die() by err() and return - add more information in the error message v2: - s/payload_offset/l4offset/ - check memory address of the checksum (alignment, iovec boundaries) checksum.c | 1 - tcp.c | 138 ++++++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 110 insertions(+), 29 deletions(-) diff --git a/checksum.c b/checksum.c index 16b834641604..19579323cf1e 100644 --- a/checksum.c +++ b/checksum.c @@ -503,7 +503,6 @@ uint16_t csum(const void *buf, size_t len, uint32_t init) * * Return: 16-bit folded, complemented checksum */ -/* cppcheck-suppress unusedFunction */ uint16_t csum_iov(const struct iovec *iov, size_t n, size_t offset, uint32_t init) { diff --git a/tcp.c b/tcp.c index c9472d905520..9db2e283d571 100644 --- a/tcp.c +++ b/tcp.c @@ -755,36 +755,103 @@ static void tcp_sock_set_bufsize(const struct ctx *c, int s) } /** - * tcp_update_check_tcp4() - Update TCP checksum from stored one - * @iph: IPv4 header - * @bp: TCP header followed by TCP payload - */ -static void tcp_update_check_tcp4(const struct iphdr *iph, - struct tcp_payload_t *bp) + * tcp_update_check_tcp4() - Calculate TCP checksum for IPv6 + * @src: IPv4 source address + * @dst: IPv4 destination address + * @iov: Pointer to the array of IO vectors + * @iov_cnt: Length of the array + * @l4offset: IPv4 payload offset in the iovec array + */ +void tcp_update_check_tcp4(struct in_addr src, + struct in_addr dst, + const struct iovec *iov, int iov_cnt, + size_t l4offset) { - uint16_t l4len = ntohs(iph->tot_len) - sizeof(struct iphdr); - struct in_addr saddr = { .s_addr = iph->saddr }; - struct in_addr daddr = { .s_addr = iph->daddr }; - uint32_t sum = proto_ipv4_header_psum(l4len, IPPROTO_TCP, saddr, daddr); + size_t check_ofs; + __sum16 *check; + int check_idx; + uint32_t sum; + + sum = proto_ipv4_header_psum(iov_size(iov, iov_cnt) - l4offset, + IPPROTO_TCP, src, dst); + + check_idx = iov_skip_bytes(iov, iov_cnt, + l4offset + offsetof(struct tcphdr, check), + &check_ofs); + + if (check_idx >= iov_cnt) { + err("TCP4 buffer is too small, iov size %zd, check offset %zd", + iov_size(iov, iov_cnt), + l4offset + offsetof(struct tcphdr, check)); + return; + } + + if (check_ofs + sizeof(*check) > iov[check_idx].iov_len) { + err("TCP4 checksum field memory is not contiguous " + "check_ofs %zd check_idx %d iov_len %zd", + check_ofs, check_idx, iov[check_idx].iov_len); + return; + } + + check = (__sum16 *)((char *)iov[check_idx].iov_base + check_ofs); - bp->th.check = 0; - bp->th.check = csum(bp, l4len, sum); + if ((uintptr_t)check & (__alignof__(*check) - 1)) { + err("TCP4 checksum field is not correctly aligned in memory"); + return; + } + + *check = 0; + *check = csum_iov(iov, iov_cnt, l4offset, sum); } /** * tcp_update_check_tcp6() - Calculate TCP checksum for IPv6 - * @ip6h: IPv6 header - * @bp: TCP header followed by TCP payload - */ -static void tcp_update_check_tcp6(const struct ipv6hdr *ip6h, - struct tcp_payload_t *bp) + * @src: IPv6 source address + * @dst: IPv6 destination address + * @iov: Pointer to the array of IO vectors + * @iov_cnt: Length of the array + * @l4offset: IPv6 payload offset in the iovec array + */ +void tcp_update_check_tcp6(const struct in6_addr *src, + const struct in6_addr *dst, + const struct iovec *iov, int iov_cnt, + size_t l4offset) { - uint16_t l4len = ntohs(ip6h->payload_len); - uint32_t sum = proto_ipv6_header_psum(l4len, IPPROTO_TCP, - &ip6h->saddr, &ip6h->daddr); + size_t check_ofs; + __sum16 *check; + int check_idx; + uint32_t sum; + + sum = proto_ipv6_header_psum(iov_size(iov, iov_cnt) - l4offset, + IPPROTO_TCP, src, dst); + + check_idx = iov_skip_bytes(iov, iov_cnt, + l4offset + offsetof(struct tcphdr, check), + &check_ofs); + + if (check_idx >= iov_cnt) { + err("TCP6 buffer is too small, iov size %zd, check offset %zd", + iov_size(iov, iov_cnt), + l4offset + offsetof(struct tcphdr, check)); + return; + } - bp->th.check = 0; - bp->th.check = csum(bp, l4len, sum); + if (check_ofs + sizeof(*check) > iov[check_idx].iov_len) { + err("TCP6 checksum field memory is not contiguous " + "check_ofs %zd check_idx %d iov_len %zd", + check_ofs, check_idx, iov[check_idx].iov_len); + return; + } + + check = (__sum16 *)((char *)iov[check_idx].iov_base + check_ofs); + + if ((uintptr_t)check & (__alignof__(*check) - 1)) { + err("TCP6 checksum field is not correctly aligned in memory"); + return; + } + + *check = 0; + *check = csum_iov(iov, iov_cnt, l4offset, sum); } /** @@ -935,10 +1002,18 @@ static size_t tcp_fill_headers4(const struct tcp_tap_conn *conn, tcp_fill_header(&bp->th, conn, seq); - if (no_tcp_csum) + if (no_tcp_csum) { bp->th.check = 0; - else - tcp_update_check_tcp4(iph, bp); + } else { + const struct iovec iov = { + .iov_base = bp, + .iov_len = ntohs(iph->tot_len) - sizeof(struct iphdr), + }; + struct in_addr saddr = { .s_addr = iph->saddr }; + struct in_addr daddr = { .s_addr = iph->daddr }; + + tcp_update_check_tcp4(saddr, daddr, &iov, 1, 0); + } tap_hdr_update(taph, l3len + sizeof(struct ethhdr)); @@ -980,10 +1055,17 @@ static size_t tcp_fill_headers6(const struct tcp_tap_conn *conn, tcp_fill_header(&bp->th, conn, seq); - if (no_tcp_csum) + if (no_tcp_csum) { bp->th.check = 0; - else - tcp_update_check_tcp6(ip6h, bp); + } else { + const struct iovec iov = { + .iov_base = bp, + .iov_len = ntohs(ip6h->payload_len) + }; + + tcp_update_check_tcp6(&ip6h->saddr, &ip6h->daddr, + &iov, 1, 0); + } tap_hdr_update(taph, l4len + sizeof(*ip6h) + sizeof(struct ethhdr)); -- 2.46.0