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 63B8DA0093; Thu, 23 Jun 2022 14:39:05 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 1E29E40042; Thu, 23 Jun 2022 14:39:05 +0200 (CEST) Received: from smartserver.smartsharesystems.com (smartserver.smartsharesystems.com [77.243.40.215]) by mails.dpdk.org (Postfix) with ESMTP id C2A444003F; Thu, 23 Jun 2022 14:39:03 +0200 (CEST) Received: from dkrd2.smartsharesys.local ([192.168.4.12]) by smartserver.smartsharesystems.com with Microsoft SMTPSVC(6.0.3790.4675); Thu, 23 Jun 2022 14:39:02 +0200 From: =?UTF-8?q?Morten=20Br=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=20Br=C3=B8rup?= Subject: [PATCH v4] net: fix checksum with unaligned buffer Date: Thu, 23 Jun 2022 14:39:00 +0200 Message-Id: <20220623123900.38283-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: 23 Jun 2022 12:39:02.0561 (UTC) FILETIME=[37EE5110:01D886FE] 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 unaligned 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. Furthermore, 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. v4: * Add copyright notice. * Include stdbool.h (Emil Berg). * Use RTE_PTR_ADD (Emil Berg). * Fix one more typo in commit message. Is 'unligned' even a word? v3: * Remove braces from single statement block. * Fix typo in commit message. 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 | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/lib/net/rte_ip.h b/lib/net/rte_ip.h index b502481670..738d643da0 100644 --- a/lib/net/rte_ip.h +++ b/lib/net/rte_ip.h @@ -3,6 +3,7 @@ * The Regents of the University of California. * Copyright(c) 2010-2014 Intel Corporation. * Copyright(c) 2014 6WIND S.A. + * Copyright(c) 2022 SmartShare Systems. * All rights reserved. */ @@ -15,6 +16,7 @@ * IP-related defines */ +#include #include #ifdef RTE_EXEC_ENV_WINDOWS @@ -162,20 +164,40 @@ __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 = RTE_PTR_ADD(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; } - return sum; + /* if buffer is unaligned, swap the checksum bytes */ + if (unlikely(unaligned)) + bsum = (bsum & 0xFF00FF00) >> 8 | (bsum & 0x00FF00FF) << 8; + + return sum + bsum; } /** -- 2.17.1