From patchwork Wed Jun 22 13:44:51 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Morten_Br=C3=B8rup?= X-Patchwork-Id: 113253 X-Patchwork-Delegate: thomas@monjalon.net Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 54A2EA04FD; Wed, 22 Jun 2022 15:45:03 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 1914640A84; Wed, 22 Jun 2022 15:45:03 +0200 (CEST) Received: from smartserver.smartsharesystems.com (smartserver.smartsharesystems.com [77.243.40.215]) by mails.dpdk.org (Postfix) with ESMTP id BE18B4069F; Wed, 22 Jun 2022 15:45:01 +0200 (CEST) Received: from dkrd2.smartsharesys.local ([192.168.4.12]) by smartserver.smartsharesystems.com with Microsoft SMTPSVC(6.0.3790.4675); Wed, 22 Jun 2022 15:45:01 +0200 From: =?utf-8?q?Morten_Br=C3=B8rup?= To: emil.berg@ericsson.com, bruce.richardson@intel.com, dev@dpdk.org Cc: stephen@networkplumber.org, stable@dpdk.org, bugzilla@dpdk.org, hofors@lysator.liu.se, olivier.matz@6wind.com, =?utf-8?q?Morten_Br=C3=B8rup?= Subject: [PATCH v2] net: fix checksum with unaligned buffer Date: Wed, 22 Jun 2022 15:44:51 +0200 Message-Id: <20220622134451.20100-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 X-OriginalArrivalTime: 22 Jun 2022 13:45:01.0366 (UTC) FILETIME=[45264560:01D8863E] 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 buffer. I.e. the buf parameter is no longer required to be 16 bit aligned. The checksum is still calculated using a 16 bit aligned pointer, so the compiler can auto-vectorize the function's inner loop. When the buffer is unaligned, the first byte of the buffer is handled separately. Futhermore, the calculated checksum of the buffer is byte shifted before being added to the initial checksum, to compensate for the checksum having been calculated on the buffer shifted by one byte. v2: * Do not assume that the buffer is part of an aligned packet buffer. Bugzilla ID: 1035 Cc: stable@dpdk.org Signed-off-by: Morten Brørup --- lib/net/rte_ip.h | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/lib/net/rte_ip.h b/lib/net/rte_ip.h index b502481670..ad46bdb443 100644 --- a/lib/net/rte_ip.h +++ b/lib/net/rte_ip.h @@ -162,20 +162,41 @@ __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; + uint32_t bsum = 0; + const bool unaligned = (uintptr_t)buf & 1; + + /* if buffer is unaligned, keeping it byte order independent */ + if (unlikely(unaligned)) { + uint16_t first = 0; + if (unlikely(len == 0)) + return 0; + ((unsigned char *)&first)[1] = *(const unsigned char *)buf; + bsum += first; + buf = (const void *)((uintptr_t)buf + 1); + len--; + } + /* aligned access for compiler auto-vectorization */ + u16_buf = (const u16_p *)buf; + end = u16_buf + len / sizeof(*u16_buf); for (; u16_buf != end; ++u16_buf) - sum += *u16_buf; + bsum += *u16_buf; /* if length is odd, keeping it byte order independent */ if (unlikely(len % 2)) { uint16_t left = 0; *(unsigned char *)&left = *(const unsigned char *)end; - sum += left; + bsum += left; + } + + /* if buffer is unaligned, swap the checksum bytes */ + if (unlikely(unaligned)) { + bsum = (bsum & 0xFF00FF00) >> 8 | (bsum & 0x00FF00FF) << 8; } - return sum; + return sum + bsum; } /**