From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 29A1FA0093; Fri, 17 Jun 2022 10:45:28 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 0A8DA40DDD; Fri, 17 Jun 2022 10:45:28 +0200 (CEST) Received: from smartserver.smartsharesystems.com (smartserver.smartsharesystems.com [77.243.40.215]) by mails.dpdk.org (Postfix) with ESMTP id 29BC740698; Fri, 17 Jun 2022 10:45:26 +0200 (CEST) Received: from dkrd2.smartsharesys.local ([192.168.4.12]) by smartserver.smartsharesystems.com with Microsoft SMTPSVC(6.0.3790.4675); Fri, 17 Jun 2022 10:45:25 +0200 From: =?UTF-8?q?Morten=20Br=C3=B8rup?= To: emil.berg@ericsson.com, dev@dpdk.org Cc: stable@dpdk.org, bugzilla@dpdk.org, hofors@lysator.liu.se, olivier.matz@6wind.com, =?UTF-8?q?Morten=20Br=C3=B8rup?= Subject: [PATCH] net: fix checksum with unaligned buffer Date: Fri, 17 Jun 2022 10:45:05 +0200 Message-Id: <20220617084505.62071-1-mb@smartsharesystems.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <98CBD80474FA8B44BF855DF32C47DC35D87139@smartserver.smartshare.dk> References: <98CBD80474FA8B44BF855DF32C47DC35D87139@smartserver.smartshare.dk> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-OriginalArrivalTime: 17 Jun 2022 08:45:25.0698 (UTC) FILETIME=[96C04220:01D88226] X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org With this patch, the checksum can be calculated on an unligned part of a packet buffer. I.e. the buf parameter is no longer required to be 16 bit aligned. The DPDK invariant that packet buffers must be 16 bit aligned remains unchanged. This invariant also defines how to calculate the 16 bit checksum on an unaligned part of a packet buffer. Bugzilla ID: 1035 Cc: stable@dpdk.org Signed-off-by: Morten Brørup --- lib/net/rte_ip.h | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/net/rte_ip.h b/lib/net/rte_ip.h index b502481670..8e301d9c26 100644 --- a/lib/net/rte_ip.h +++ b/lib/net/rte_ip.h @@ -162,9 +162,22 @@ __rte_raw_cksum(const void *buf, size_t len, uint32_t sum) { /* extend strict-aliasing rules */ typedef uint16_t __attribute__((__may_alias__)) u16_p; - const u16_p *u16_buf = (const u16_p *)buf; - const u16_p *end = u16_buf + len / sizeof(*u16_buf); + const u16_p *u16_buf; + const u16_p *end; + + /* if buffer is unaligned, keeping it byte order independent */ + if (unlikely((uintptr_t)buf & 1)) { + uint16_t first = 0; + if (unlikely(len == 0)) + return 0; + ((unsigned char *)&first)[1] = *(const unsigned char *)buf; + sum += first; + buf = (const void *)((uintptr_t)buf + 1); + len--; + } + u16_buf = (const u16_p *)buf; + end = u16_buf + len / sizeof(*u16_buf); for (; u16_buf != end; ++u16_buf) sum += *u16_buf; -- 2.17.1