From patchwork Tue Oct 3 20:29:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Stephen Hemminger X-Patchwork-Id: 132289 X-Patchwork-Delegate: ferruh.yigit@amd.com 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 6DEB7426B2; Tue, 3 Oct 2023 22:29:40 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 96B2940A72; Tue, 3 Oct 2023 22:29:25 +0200 (CEST) Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) by mails.dpdk.org (Postfix) with ESMTP id B8B6F40608 for ; Tue, 3 Oct 2023 22:29:23 +0200 (CEST) Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-6934202b8bdso1083984b3a.1 for ; Tue, 03 Oct 2023 13:29:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=networkplumber-org.20230601.gappssmtp.com; s=20230601; t=1696364962; x=1696969762; darn=dpdk.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=MYx472k3BjwaIoEKo8D2zP3PanhZssBLZLscHcGJ5Ok=; b=lDoW8SRsAvCm2ioCMNFg3CUTUI56kRP8Gl6rwyzozo9SgCtr5EWbVLj3CeLiEXzv+N PmTwJ5zH9Oqjntg8mevyqHGo3I0r7ifLaPU9HpurMtkPCKFYye8EmTRd60uKYuUdpeJJ ReV6JcOLo6oesmee9VjhLspH8shLgMsPgVzeHd8hjoTIj7Tksj7+3onXNetOWFXnKL/k tL5UXsxyBGu63oYUhKmYsHAUET+OiKYTZQ5aQxKnVfYJFXQbmSeeumh9IpJ7C5te29Fx FgUnyiylimu7npdZlVpJcp/hE/NuquRzj5oN52BT0fI2zWWO6oRczfNxXVvB6cll68fS F7lw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696364962; x=1696969762; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=MYx472k3BjwaIoEKo8D2zP3PanhZssBLZLscHcGJ5Ok=; b=hKfZaA4HhOANH7NykARrOVGC4qtrF/iT/ATNDDSpc81uxmEvH1hTTBSO9JytOXpZEQ LYyw8mYVS7NwW2jxFj+GQ5bEdP6dek3pgQ997rX60zVOyh8GL/pij4q7UtEWON8ajoDI lEqLQiA8inHCsqKAoMjGEEhO6cRRH2uoBBKRZuxnfefYat9/1uWbcfdd0yLHo4X31SUh ENB4pV1Q2kOb/H1Qfq2P5EmG3NSItE5kAy6VA+IJl3wg8X7yCeJA1m6bVKSwz8wqXJh1 v286EAyFguUzrq19Zd0vrcx+HmMsA4lRKa17MIXdkv3yXuwnztPe2yZyump5sK/5fuq3 5UUw== X-Gm-Message-State: AOJu0YwnItwvXrItIUIS/iRxKS5ru0OZDw8aGhljr6f6iXfmRMPG6Z0h OpFXpE44kIoYYTVw5tRHHb32vqgAV+olMkriphw= X-Google-Smtp-Source: AGHT+IHu9dOjMX1Bv1Ntye+2VpmSIV4gLmuBtU4dUtqmjOd1PW2T4rYvIl23LyZ5m3FsSWz4recJNA== X-Received: by 2002:a05:6a21:9982:b0:142:952d:3e61 with SMTP id ve2-20020a056a21998200b00142952d3e61mr673971pzb.7.1696364962438; Tue, 03 Oct 2023 13:29:22 -0700 (PDT) Received: from hermes.local (204-195-126-68.wavecable.com. [204.195.126.68]) by smtp.gmail.com with ESMTPSA id a11-20020a1709027d8b00b001c5f62a639asm2014138plm.196.2023.10.03.13.29.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Oct 2023 13:29:22 -0700 (PDT) From: Stephen Hemminger To: dev@dpdk.org Cc: Stephen Hemminger , =?utf-8?q?Morten_Br?= =?utf-8?q?=C3=B8rup?= Subject: [PATCH v3 3/4] test: add tests for rte_ether routines Date: Tue, 3 Oct 2023 13:29:08 -0700 Message-Id: <20231003202909.391330-4-stephen@networkplumber.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231003202909.391330-1-stephen@networkplumber.org> References: <20230929163611.62691-1-stephen@networkplumber.org> <20231003202909.391330-1-stephen@networkplumber.org> MIME-Version: 1.0 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 This add some basic tests for rte_unformat_ether_addr and other functions in rte_ether. Acked-by: Morten Brørup Signed-off-by: Stephen Hemminger --- app/test/meson.build | 1 + app/test/test_net_ether.c | 165 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 166 insertions(+) create mode 100644 app/test/test_net_ether.c diff --git a/app/test/meson.build b/app/test/meson.build index bf9fc906128f..20a9333c726d 100644 --- a/app/test/meson.build +++ b/app/test/meson.build @@ -124,6 +124,7 @@ source_file_deps = { 'test_meter.c': ['meter'], 'test_metrics.c': ['metrics'], 'test_mp_secondary.c': ['hash', 'lpm'], + 'test_net_ether.c': ['net'], 'test_pcapng.c': ['ethdev', 'net', 'pcapng'], 'test_pdcp.c': ['eventdev', 'pdcp', 'net', 'timer', 'security'], 'test_pdump.c': ['pdump'] + sample_packet_forward_deps, diff --git a/app/test/test_net_ether.c b/app/test/test_net_ether.c new file mode 100644 index 000000000000..1cb6845a9c48 --- /dev/null +++ b/app/test/test_net_ether.c @@ -0,0 +1,165 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright (c) 2023 Stephen Hemminger + */ + +#include + +#include +#include "test.h" + +#define N 1000000 + +static const struct rte_ether_addr zero_ea; +static const struct rte_ether_addr bcast_ea = { + .addr_bytes = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, +}; + +static int +test_ether_addr(void) +{ + struct rte_ether_addr rand_ea = { }; + unsigned int i; + + RTE_TEST_ASSERT(rte_is_zero_ether_addr(&zero_ea), "Zero address is not zero"); + RTE_TEST_ASSERT(!rte_is_zero_ether_addr(&bcast_ea), "Broadcast is zero"); + + for (i = 0; i < N; i++) { + rte_eth_random_addr(rand_ea.addr_bytes); + RTE_TEST_ASSERT(!rte_is_zero_ether_addr(&rand_ea), + "Random address is zero"); + RTE_TEST_ASSERT(rte_is_unicast_ether_addr(&rand_ea), + "Random address is not unicast"); + RTE_TEST_ASSERT(rte_is_local_admin_ether_addr(&rand_ea), + "Random address is not local admin"); + } + + return 0; +} + +static int +test_format_addr(void) +{ + struct rte_ether_addr rand_ea = { }; + char buf[RTE_ETHER_ADDR_FMT_SIZE]; + unsigned int i; + + for (i = 0; i < N; i++) { + struct rte_ether_addr result = { }; + int ret; + + rte_eth_random_addr(rand_ea.addr_bytes); + + rte_ether_format_addr(buf, sizeof(buf), &rand_ea); + + ret = rte_ether_unformat_addr(buf, &result); + if (ret != 0) { + fprintf(stderr, "rte_ether_unformat_addr(%s) failed\n", buf); + return -1; + } + RTE_TEST_ASSERT(rte_is_same_ether_addr(&rand_ea, &result), + "rte_ether_format/unformat mismatch"); + } + return 0; + +} + +static int +test_unformat_addr(void) +{ + const struct rte_ether_addr expected = { + .addr_bytes = { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc }, + }; + const struct rte_ether_addr nozero_ea = { + .addr_bytes = { 1, 2, 3, 4, 5, 6 }, + }; + struct rte_ether_addr result; + int ret; + + /* Test IETF format */ + memset(&result, 0, sizeof(result)); + ret = rte_ether_unformat_addr("12:34:56:78:9a:bc", &result); + RTE_TEST_ASSERT(ret == 0, "IETF unformat failed"); + RTE_TEST_ASSERT(rte_is_same_ether_addr(&expected, &result), + "IETF unformat mismatch"); + + /* Test IEEE format */ + memset(&result, 0, sizeof(result)); + ret = rte_ether_unformat_addr("12-34-56-78-9A-BC", &result); + RTE_TEST_ASSERT(ret == 0, "IEEE unformat failed"); + RTE_TEST_ASSERT(rte_is_same_ether_addr(&expected, &result), + "IEEE unformat mismatch"); + + /* Test Cisco format */ + memset(&result, 0, sizeof(result)); + ret = rte_ether_unformat_addr("1234.5678.9ABC", &result); + RTE_TEST_ASSERT(ret == 0, "Cisco unformat failed"); + RTE_TEST_ASSERT(rte_is_same_ether_addr(&expected, &result), + "Cisco unformat mismatch"); + + /* Test no leading zeros - IETF */ + memset(&result, 0, sizeof(result)); + ret = rte_ether_unformat_addr("1:2:3:4:5:6", &result); + RTE_TEST_ASSERT(ret == 0, "IETF leading zero failed"); + RTE_TEST_ASSERT(rte_is_same_ether_addr(&nozero_ea, &result), + "IETF leading zero mismatch"); + + /* Test no-leading zero - IEEE format */ + memset(&result, 0, sizeof(result)); + ret = rte_ether_unformat_addr("1-2-3-4-5-6", &result); + RTE_TEST_ASSERT(ret == 0, "IEEE leading zero failed"); + RTE_TEST_ASSERT(rte_is_same_ether_addr(&nozero_ea, &result), + "IEEE leading zero mismatch"); + + + return 0; +} + +static int +test_invalid_addr(void) +{ + static const char * const invalid[] = { + "123", + "123:456", + "12:34:56:78:9a:gh", + "12:34:56:78:9a", + "100:34:56:78:9a:bc", + "34-56-78-9a-bc", + "12:34:56-78:9a:bc", + "12:34:56.78:9a:bc", + "123:456:789:abc", + "NOT.AN.ADDRESS", + "102.304.506", + "", + }; + struct rte_ether_addr result; + unsigned int i; + + for (i = 0; i < RTE_DIM(invalid); ++i) { + if (!rte_ether_unformat_addr(invalid[i], &result)) { + fprintf(stderr, "rte_ether_unformat_addr(%s) succeeded!\n", + invalid[i]); + return -1; + } + } + return 0; +} + +static int +test_net_ether(void) +{ + if (test_ether_addr()) + return -1; + + if (test_format_addr()) + return -1; + + if (test_unformat_addr()) + return -1; + + if (test_invalid_addr()) + return -1; + + return 0; +} + +REGISTER_FAST_TEST(net_ether_autotest, true, true, test_net_ether);