@@ -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',
new file mode 100644
@@ -0,0 +1,61 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2021 Intel Corporation
+ */
+
+#include <stdio.h>
+#include <stdint.h>
+
+#include <rte_ip.h>
+#include <rte_common.h>
+#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);
@@ -26,6 +26,7 @@ headers = files(
sources = files(
'rte_arp.c',
'rte_ether.c',
+ 'rte_ip.c',
'rte_net.c',
'rte_net_crc.c',
)
new file mode 100644
@@ -0,0 +1,43 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2021 Intel Corporation
+ */
+
+#include <string.h>
+#include <rte_ip.h>
+
+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;
+}
@@ -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
*/
@@ -12,3 +12,9 @@ DPDK_22 {
local: *;
};
+
+EXPERIMENTAL {
+ global:
+
+ rte_ip_parse_addr;
+};