From patchwork Thu Sep 14 15:00:04 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Github ODP bot X-Patchwork-Id: 112563 Delivered-To: patch@linaro.org Received: by 10.140.106.117 with SMTP id d108csp843770qgf; Thu, 14 Sep 2017 08:04:00 -0700 (PDT) X-Google-Smtp-Source: AOwi7QARmm2Desg3ARRXoUInNAPfYGs0bhnNciSgRPUncWkIevPYzN32Zr3zE3aiC8+OaDtCKQP+ X-Received: by 10.200.42.92 with SMTP id l28mr17989654qtl.137.1505401440206; Thu, 14 Sep 2017 08:04:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1505401440; cv=none; d=google.com; s=arc-20160816; b=iuMmQakJi+jU97GuFKty+74a9loW/TLIcKAHf6VQhZq8kCNkvuIdvJG3HaOgQowvl3 t9apmsRl+suBXcdHfwzUw2f/2nnn3jkaBX8VjpjmhxQJt3T+vrIHFx6ts47FQmsNbeY4 SlctSVoJlWCw3xao9CdT6Av22hKvDjDbIitcsURD3f2pTqxFrF1RCWp+NENm/NWIA5KL yOaBhmnSbY6E8K3ulMDBqlX4EVKQwUztaInAMpXvzlz36A+B1wY6kcLnyhcRlrgLUsnO lrvJdtCwkkPq6Gl93KmvB+eBalZ2dI4jHq5oMIKoPTW/mTZOoC2LTEF8/F4NlJEiP8vf z5cg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:subject:github-pr-num :references:in-reply-to:message-id:date:to:from:delivered-to :arc-authentication-results; bh=2AaNB8FlSQ63ZjgUUi+MKUuojRgKk/9JD8ei/9KElYE=; b=wf9LCVAn5rJrzDQF/xLo2DfYRia2SoayKZ6eK6FDIrgq8QAg7rh54PkPEG5QcJCRPX xZ9q0uEvJlEIhlz7woVB+OS3wRKCvuJXiNo7WF26TWSDTAri/cCkiRA1MJvvbBcQalZ7 1sKYIKY2/TdER6SHIJj698RSAolH0lbj72EA1NvGLPxE3MA+J0P85ub2KkmYk0PRfYWo yULWHQYEQL+2UA8EmejXCB30AYdCEJtI7S2f5Y5qsdXE2tb6kuHTt8oGyM34RWhk1ZOi na96neEqKs4cUpaJXNJ787/Zx4h5WlkVQp3hvtupf08cq5f3vlecuOLuMiz5g3tixHbX vX6g== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of lng-odp-bounces@lists.linaro.org designates 54.225.227.206 as permitted sender) smtp.mailfrom=lng-odp-bounces@lists.linaro.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=yandex.ru Return-Path: Received: from lists.linaro.org (lists.linaro.org. [54.225.227.206]) by mx.google.com with ESMTP id l36si3410385qtd.335.2017.09.14.08.03.59; Thu, 14 Sep 2017 08:04:00 -0700 (PDT) Received-SPF: pass (google.com: domain of lng-odp-bounces@lists.linaro.org designates 54.225.227.206 as permitted sender) client-ip=54.225.227.206; Authentication-Results: mx.google.com; spf=pass (google.com: domain of lng-odp-bounces@lists.linaro.org designates 54.225.227.206 as permitted sender) smtp.mailfrom=lng-odp-bounces@lists.linaro.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=yandex.ru Received: by lists.linaro.org (Postfix, from userid 109) id A34DB60C61; Thu, 14 Sep 2017 15:03:59 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on ip-10-142-244-252 X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,URIBL_BLOCKED autolearn=disabled version=3.4.0 Received: from [127.0.0.1] (localhost [127.0.0.1]) by lists.linaro.org (Postfix) with ESMTP id 20EEB609D5; Thu, 14 Sep 2017 15:00:57 +0000 (UTC) X-Original-To: lng-odp@lists.linaro.org Delivered-To: lng-odp@lists.linaro.org Received: by lists.linaro.org (Postfix, from userid 109) id 7B1546097D; Thu, 14 Sep 2017 15:00:26 +0000 (UTC) Received: from forward105o.mail.yandex.net (forward105o.mail.yandex.net [37.140.190.183]) by lists.linaro.org (Postfix) with ESMTPS id E9B1D6088A for ; Thu, 14 Sep 2017 15:00:22 +0000 (UTC) Received: from mxback2o.mail.yandex.net (mxback2o.mail.yandex.net [IPv6:2a02:6b8:0:1a2d::1c]) by forward105o.mail.yandex.net (Yandex) with ESMTP id 74D91444604F for ; Thu, 14 Sep 2017 18:00:21 +0300 (MSK) Received: from smtp3o.mail.yandex.net (smtp3o.mail.yandex.net [2a02:6b8:0:1a2d::27]) by mxback2o.mail.yandex.net (nwsmtp/Yandex) with ESMTP id DfaahRU470-0Lf87Cg3; Thu, 14 Sep 2017 18:00:21 +0300 Received: by smtp3o.mail.yandex.net (nwsmtp/Yandex) with ESMTPSA id gNZkqRzw9k-0K7KjKpa; Thu, 14 Sep 2017 18:00:20 +0300 (using TLSv1.2 with cipher ECDHE-RSA-AES128-SHA256 (128/128 bits)) (Client certificate not present) From: Github ODP bot To: lng-odp@lists.linaro.org Date: Thu, 14 Sep 2017 18:00:04 +0300 Message-Id: <1505401213-7265-4-git-send-email-odpbot@yandex.ru> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1505401213-7265-1-git-send-email-odpbot@yandex.ru> References: <1505401213-7265-1-git-send-email-odpbot@yandex.ru> Github-pr-num: 160 Subject: [lng-odp] [PATCH API-NEXT v11 3/12] helper: ip: correct ipv4 header checksum calculation X-BeenThere: lng-odp@lists.linaro.org X-Mailman-Version: 2.1.16 Precedence: list List-Id: "The OpenDataPlane \(ODP\) List" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: lng-odp-bounces@lists.linaro.org Sender: "lng-odp" From: Dmitry Eremin-Solenikov Current code for IPv4 header checksum calculation assumes that packet data is aligned on 2-byte boundary, that there are no optional headers, etc. Rewrite checksumming code to properly copy & process headers. Signed-off-by: Dmitry Eremin-Solenikov --- /** Email created from pull request 160 (bala-manoharan:api_sched_order_lock) ** https://github.com/Linaro/odp/pull/160 ** Patch: https://github.com/Linaro/odp/pull/160.patch ** Base sha: 6b6253c30f88c80bf632436ff06c1b000860a2f1 ** Merge commit sha: 438a3bab5df9c05dc06f0b4e4b22c0a7db0864fd **/ helper/include/odp/helper/ip.h | 75 +++++++++++++++------- .../api/classification/odp_classification_common.c | 3 +- .../classification/odp_classification_test_pmr.c | 2 +- .../api/classification/odp_classification_tests.c | 9 +-- 4 files changed, 58 insertions(+), 31 deletions(-) diff --git a/helper/include/odp/helper/ip.h b/helper/include/odp/helper/ip.h index 91776fad6..e0d5c3bfb 100644 --- a/helper/include/odp/helper/ip.h +++ b/helper/include/odp/helper/ip.h @@ -74,6 +74,9 @@ extern "C" { /** @internal Returns true if IPv4 packet is a fragment */ #define ODPH_IPV4HDR_IS_FRAGMENT(frag_offset) ((frag_offset) & 0x3fff) +/** @internal Checksum offset in IPv4 header */ +#define ODPH_IPV4HDR_CSUM_OFFSET 10 + /** IPv4 header */ typedef struct ODP_PACKED { uint8_t ver_ihl; /**< Version / Header length */ @@ -92,6 +95,28 @@ typedef struct ODP_PACKED { ODP_STATIC_ASSERT(sizeof(odph_ipv4hdr_t) == ODPH_IPV4HDR_LEN, "ODPH_IPV4HDR_T__SIZE_ERROR"); +static inline int odph_ipv4_csum(odp_packet_t pkt, + uint32_t offset, + odph_ipv4hdr_t *ip, + odp_u16sum_t *chksum) +{ + int nleft = ODPH_IPV4HDR_IHL(ip->ver_ihl) * 4; + uint16_t buf[nleft / 2]; + int res; + + ip->chksum = 0; + memcpy(buf, ip, sizeof(*ip)); + res = odp_packet_copy_to_mem(pkt, offset + sizeof(*ip), + nleft - sizeof(*ip), + buf + sizeof(*ip) / 2); + if (odp_unlikely(res < 0)) + return res; + + *chksum = odph_chksum(buf, nleft); + + return 0; +} + /** * Check if IPv4 checksum is valid * @@ -102,11 +127,9 @@ ODP_STATIC_ASSERT(sizeof(odph_ipv4hdr_t) == ODPH_IPV4HDR_LEN, static inline int odph_ipv4_csum_valid(odp_packet_t pkt) { uint32_t offset; - odp_u16be_t res = 0; - uint16_t *w; - int nleft = sizeof(odph_ipv4hdr_t); + int res; odph_ipv4hdr_t ip; - odp_u16be_t chksum; + odp_u16sum_t chksum, cur_chksum; offset = odp_packet_l3_offset(pkt); if (offset == ODP_PACKET_OFFSET_INVALID) @@ -114,37 +137,45 @@ static inline int odph_ipv4_csum_valid(odp_packet_t pkt) odp_packet_copy_to_mem(pkt, offset, sizeof(odph_ipv4hdr_t), &ip); - w = (uint16_t *)(void *)&ip; chksum = ip.chksum; - ip.chksum = 0x0; - res = odph_chksum(w, nleft); - return (res == chksum) ? 1 : 0; + res = odph_ipv4_csum(pkt, offset, &ip, &cur_chksum); + if (odp_unlikely(res < 0)) + return 0; + + return (cur_chksum == chksum) ? 1 : 0; } /** * Calculate and fill in IPv4 checksum * - * @note when using this api to populate data destined for the wire - * odp_cpu_to_be_16() can be used to remove sparse warnings - * * @param pkt ODP packet * - * @return IPv4 checksum in host cpu order, or 0 on failure + * @retval 0 on success + * @retval <0 on failure */ -static inline odp_u16sum_t odph_ipv4_csum_update(odp_packet_t pkt) +static inline int odph_ipv4_csum_update(odp_packet_t pkt) { - uint16_t *w; - odph_ipv4hdr_t *ip; - int nleft = sizeof(odph_ipv4hdr_t); + uint32_t offset; + odph_ipv4hdr_t ip; + odp_u16sum_t chksum; + int res; - ip = (odph_ipv4hdr_t *)odp_packet_l3_ptr(pkt, NULL); - if (ip == NULL) - return 0; + offset = odp_packet_l3_offset(pkt); + if (offset == ODP_PACKET_OFFSET_INVALID) + return -1; + + res = odp_packet_copy_to_mem(pkt, offset, sizeof(ip), &ip); + if (odp_unlikely(res < 0)) + return res; + + res = odph_ipv4_csum(pkt, offset, &ip, &chksum); + if (odp_unlikely(res < 0)) + return res; - w = (uint16_t *)(void *)ip; - ip->chksum = odph_chksum(w, nleft); - return ip->chksum; + return odp_packet_copy_from_mem(pkt, + offset + ODPH_IPV4HDR_CSUM_OFFSET, + 2, &chksum); } /** IPv6 version */ diff --git a/test/common_plat/validation/api/classification/odp_classification_common.c b/test/common_plat/validation/api/classification/odp_classification_common.c index e0c0808ec..60e20ea87 100644 --- a/test/common_plat/validation/api/classification/odp_classification_common.c +++ b/test/common_plat/validation/api/classification/odp_classification_common.c @@ -317,8 +317,7 @@ odp_packet_t create_packet(cls_packet_info_t pkt_info) ip->src_addr = odp_cpu_to_be_32(addr); ip->ver_ihl = ODPH_IPV4 << 4 | ODPH_IPV4HDR_IHL_MIN; ip->id = odp_cpu_to_be_16(seqno); - ip->chksum = 0; - ip->chksum = odph_ipv4_csum_update(pkt); + odph_ipv4_csum_update(pkt); ip->proto = next_hdr; ip->tot_len = odp_cpu_to_be_16(l3_len); ip->ttl = DEFAULT_TTL; diff --git a/test/common_plat/validation/api/classification/odp_classification_test_pmr.c b/test/common_plat/validation/api/classification/odp_classification_test_pmr.c index 15b76033b..87d04b6be 100644 --- a/test/common_plat/validation/api/classification/odp_classification_test_pmr.c +++ b/test/common_plat/validation/api/classification/odp_classification_test_pmr.c @@ -1678,7 +1678,7 @@ static void classification_test_pmr_term_daddr(void) odp_pktio_mac_addr(pktio, eth->dst.addr, ODPH_ETHADDR_LEN); ip = (odph_ipv4hdr_t *)odp_packet_l3_ptr(pkt, NULL); ip->dst_addr = odp_cpu_to_be_32(addr); - ip->chksum = odph_ipv4_csum_update(pkt); + odph_ipv4_csum_update(pkt); seqno = cls_pkt_get_seq(pkt); CU_ASSERT(seqno != TEST_SEQ_INVALID); diff --git a/test/common_plat/validation/api/classification/odp_classification_tests.c b/test/common_plat/validation/api/classification/odp_classification_tests.c index 1b044509e..c7f5d0d47 100644 --- a/test/common_plat/validation/api/classification/odp_classification_tests.c +++ b/test/common_plat/validation/api/classification/odp_classification_tests.c @@ -239,8 +239,7 @@ void test_cls_pmr_chain(void) ip = (odph_ipv4hdr_t *)odp_packet_l3_ptr(pkt, NULL); parse_ipv4_string(CLS_PMR_CHAIN_SADDR, &addr, &mask); ip->src_addr = odp_cpu_to_be_32(addr); - ip->chksum = 0; - ip->chksum = odph_ipv4_csum_update(pkt); + odph_ipv4_csum_update(pkt); set_first_supported_pmr_port(pkt, CLS_PMR_CHAIN_PORT); @@ -262,8 +261,7 @@ void test_cls_pmr_chain(void) ip = (odph_ipv4hdr_t *)odp_packet_l3_ptr(pkt, NULL); parse_ipv4_string(CLS_PMR_CHAIN_SADDR, &addr, &mask); ip->src_addr = odp_cpu_to_be_32(addr); - ip->chksum = 0; - ip->chksum = odph_ipv4_csum_update(pkt); + odph_ipv4_csum_update(pkt); enqueue_pktio_interface(pkt, pktio_loop); pkt = receive_packet(&queue, ODP_TIME_SEC_IN_NS); @@ -670,8 +668,7 @@ void test_pktio_pmr_composite_cos(void) ip = (odph_ipv4hdr_t *)odp_packet_l3_ptr(pkt, NULL); parse_ipv4_string(CLS_PMR_SET_SADDR, &addr, &mask); ip->src_addr = odp_cpu_to_be_32(addr); - ip->chksum = 0; - ip->chksum = odph_ipv4_csum_update(pkt); + odph_ipv4_csum_update(pkt); set_first_supported_pmr_port(pkt, CLS_PMR_SET_PORT); enqueue_pktio_interface(pkt, pktio_loop);