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 56A06A034E; Fri, 21 Jan 2022 11:31:38 +0100 (CET) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id E326A4274B; Fri, 21 Jan 2022 11:31:33 +0100 (CET) Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by mails.dpdk.org (Postfix) with ESMTP id CD49540042 for ; Fri, 21 Jan 2022 11:31:31 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1642761092; x=1674297092; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=91BuGmLS8tjq2kdFkJkABz4t618UqoaKsJyobTy/HtE=; b=XfaKcIPwGXPQbGwmYlzkSFhXE1/z3cuvl/NdLrhz6BtzN0ldts+LAdDF fgcrokvpqS1HO7QSOnf5Tzq5tYZ4zPe56Sd5kXUbQfokwaa0EZXKZyaKR hg1WCKD1KiTxOjfK1L7T4kklCmxZeB2xSiQA5g+Wk2JxLTkZat2no5XDF 99PbFdyruGxz88a0VczqhJCgU5Z0xh65DvlaVDh8SdQ3aMGRzMssRE4ej D/3AlrRKmCPOsDBJwk2nfY+lCQgbmINRM+X3e2vPyQQZkI8DKyt4GkOGe 80MT8PoEpi3/SUSnUhn4dkBoy0L0Y88avdbb/LmP1jADVK5lKEnfrsYrm Q==; X-IronPort-AV: E=McAfee;i="6200,9189,10233"; a="270045062" X-IronPort-AV: E=Sophos;i="5.88,304,1635231600"; d="scan'208";a="270045062" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Jan 2022 02:31:31 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.88,304,1635231600"; d="scan'208";a="533222699" Received: from silpixa00401120.ir.intel.com ([10.55.128.255]) by orsmga008.jf.intel.com with ESMTP; 21 Jan 2022 02:31:30 -0800 From: Ronan Randles To: dev@dpdk.org Cc: Ronan Randles , Harry van Haaren Subject: [PATCH v2 01/15] net: add string to IPv4 parse function Date: Fri, 21 Jan 2022 10:31:08 +0000 Message-Id: <20220121103122.2926856-2-ronan.randles@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220121103122.2926856-1-ronan.randles@intel.com> References: <20211214141242.3383831-1-ronan.randles@intel.com> <20220121103122.2926856-1-ronan.randles@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 Added function that accepts ip string as a parameter and returns an ip address represented by a uint32_t. Relevant unit test for this function is also included. Signed-off-by: Harry van Haaren Signed-off-by: Ronan Randles --- app/test/meson.build | 2 ++ app/test/test_net.c | 61 ++++++++++++++++++++++++++++++++++++++++++++ lib/net/meson.build | 1 + lib/net/rte_ip.c | 43 +++++++++++++++++++++++++++++++ lib/net/rte_ip.h | 18 +++++++++++++ lib/net/version.map | 6 +++++ 6 files changed, 131 insertions(+) create mode 100644 app/test/test_net.c create mode 100644 lib/net/rte_ip.c diff --git a/app/test/meson.build b/app/test/meson.build index 344a609a4d..8cdfb783a9 100644 --- a/app/test/meson.build +++ b/app/test/meson.build @@ -100,6 +100,7 @@ test_sources = files( 'test_meter.c', 'test_mcslock.c', 'test_mp_secondary.c', + 'test_net.c', 'test_per_lcore.c', 'test_pflock.c', 'test_pmd_perf.c', @@ -177,6 +178,7 @@ test_deps = [ 'ipsec', 'lpm', 'member', + 'net', 'node', 'pipeline', 'port', diff --git a/app/test/test_net.c b/app/test/test_net.c new file mode 100644 index 0000000000..2cb7d3e1c9 --- /dev/null +++ b/app/test/test_net.c @@ -0,0 +1,61 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright(c) 2021 Intel Corporation + */ + +#include +#include + +#include +#include +#include "test.h" + +static int +test_rte_ip_parse_addr(void) +{ + printf("Running IP parsing tests...\n"); + + struct str_ip_t { + const char *str; + uint32_t exp_output; + uint32_t expected_to_fail; + } str_ip_tests[] = { + { .str = "1.2.3.4", .exp_output = RTE_IPV4(1, 2, 3, 4)}, + { .str = "192.168.255.255", .exp_output = + RTE_IPV4(192, 168, 255, 255)}, + { .str = "172.16.0.9", .exp_output = + RTE_IPV4(172, 16, 0, 9)}, + { .str = "1.2.3", .expected_to_fail = 1}, + { .str = "1.2.3.4.5", .expected_to_fail = 1}, + { .str = "fail.1.2.3", .expected_to_fail = 1}, + { .str = "", .expected_to_fail = 1}, + { .str = "1.2.3.fail", .expected_to_fail = 1} + }; + + uint32_t i; + for (i = 0; i < RTE_DIM(str_ip_tests); i++) { + uint32_t test_addr; + int32_t err = rte_ip_parse_addr(str_ip_tests[i].str, + &test_addr); + if (!test_addr) { + if (str_ip_tests[i].expected_to_fail != 1) + return -1; + } + + if (err || test_addr != str_ip_tests[i].exp_output) { + if (str_ip_tests[i].expected_to_fail != 1) + return -1; + } + } + + + return 0; +} + +static int +test_net_tests(void) +{ + int ret = test_rte_ip_parse_addr(); + return ret; +} + +REGISTER_TEST_COMMAND(net_autotest, test_net_tests); diff --git a/lib/net/meson.build b/lib/net/meson.build index e899846578..b2577a7592 100644 --- a/lib/net/meson.build +++ b/lib/net/meson.build @@ -26,6 +26,7 @@ headers = files( sources = files( 'rte_arp.c', 'rte_ether.c', + 'rte_ip.c', 'rte_net.c', 'rte_net_crc.c', ) diff --git a/lib/net/rte_ip.c b/lib/net/rte_ip.c new file mode 100644 index 0000000000..b859dfb640 --- /dev/null +++ b/lib/net/rte_ip.c @@ -0,0 +1,43 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright(c) 2021 Intel Corporation + */ + +#include +#include + +int32_t +rte_ip_parse_addr(const char *src_ip, uint32_t *output_addr) +{ + int32_t ret = 0; + char *current_position; + + if (src_ip == NULL) + return -1; + + char *tok = strdup(src_ip); + if (tok == NULL) + return -1; + + char *current_digit = strtok_r(tok, ".", ¤t_position); + + *output_addr = 0; + uint32_t i = 0; + while (current_digit) { + uint32_t shift = ((3 - i) * 8); + unsigned long parsed_value = strtoul(current_digit, NULL, 0) + << shift; + + if (parsed_value == 0 && strcmp(current_digit, "0")) + break; + + *output_addr |= parsed_value; + current_digit = strtok_r(NULL, ".", ¤t_position); + i++; + + } + if (i != 4) + return -1; + + free(tok); + return ret; +} diff --git a/lib/net/rte_ip.h b/lib/net/rte_ip.h index c575250852..188054fda4 100644 --- a/lib/net/rte_ip.h +++ b/lib/net/rte_ip.h @@ -426,6 +426,24 @@ rte_ipv4_udptcp_cksum_verify(const struct rte_ipv4_hdr *ipv4_hdr, return 0; } +/** + * IP address parser. + * + * @param src_ip + * The IP address to be parsed. + * @param output_addr + * The array in which the parsed digits will be saved. + * + * @retval 0 + * Success. + * @retval -1 + * Failure due to invalid input arguments. + */ + +__rte_experimental +int32_t +rte_ip_parse_addr(const char *src_ip, uint32_t *output_addr); + /** * IPv6 Header */ diff --git a/lib/net/version.map b/lib/net/version.map index 4f4330d1c4..c530d1a4e4 100644 --- a/lib/net/version.map +++ b/lib/net/version.map @@ -12,3 +12,9 @@ DPDK_22 { local: *; }; + +EXPERIMENTAL { + global: + + rte_ip_parse_addr; +}; -- 2.25.1