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 23F08426A5; Mon, 2 Oct 2023 20:38:01 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 8D637402EA; Mon, 2 Oct 2023 20:37:47 +0200 (CEST) Received: from mail-pl1-f181.google.com (mail-pl1-f181.google.com [209.85.214.181]) by mails.dpdk.org (Postfix) with ESMTP id 07460402D5 for ; Mon, 2 Oct 2023 20:37:45 +0200 (CEST) Received: by mail-pl1-f181.google.com with SMTP id d9443c01a7336-1bf55a81eeaso1081875ad.0 for ; Mon, 02 Oct 2023 11:37:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=networkplumber-org.20230601.gappssmtp.com; s=20230601; t=1696271864; x=1696876664; 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=h5MS8SMAbT7v9isRW24gI7PqH+NVxcBjHc33IbSMo/0=; b=CHyV/eFAb2udc+ISX8M9v7DmsL4VuvLUZgBDlo+cPT70D0PSzZqzwi209ix6M4yUQl 3JeSNm9nFAPEd0jJ8tHE2mkyMOp5OUJgJg2NGxDg5R1S7PrbdqA0SUPFxLzXhg/ilAMa mhSiKR1PeeaPRTB2ljxccrjyn1ehNfJ3hUk3jmESfNBBrLmle6UByuJKgay5DrUh0UPU OiyYUNIu+e3fMM1YbJiHGrN9Ny39KBY/ookR8wTj8qmp+smjUPnsn2jL6oXqPTrsi42u mUO3Uz705DycRmkWT5eVqL3HKEnj9zTHStDnrDCVdwD66wO7PjRiqKdviRVqVBXldEkr yA5Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696271864; x=1696876664; 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=h5MS8SMAbT7v9isRW24gI7PqH+NVxcBjHc33IbSMo/0=; b=TTFsiHj/izvgpDqh4JivZCfnUD3TwArx6IHD7tlimiesVJzhATT/FilOC7x79DpYza h7KEP96JX7zZqgnSmKYaRd6JJ7hBohMvLd7k9pUWYHMYBIbKj+xcYVflwXerkLHrcUpM MSB2TLjbJPldekwKyCL3uY/8hFIMlsjRO+y0rATl+IE7y3k3BhOucxAABGdr5zujAmPG B23nAsZdRdCkvZPeGmA4VIo8mmoFfhE0MZv/S5DJaZOB6NbyHDKgSNtwtcPtHqQUEQ1k 4IdDxGHn3C1rbdqi1FKzbz+M+rZlZVLu3lYMn2J+O4ZnwCuRRltdAeWz10sqlGuPvva2 1RRg== X-Gm-Message-State: AOJu0YwJYgMhVnJWlodyWm6I6z/sZzxWowFCyEwfye+eSrAnfAifNQ3l iCnkCi9CBh6nCgLixiR6zl12v4ICp6D2vbkKgAg= X-Google-Smtp-Source: AGHT+IGL1fDFGE+Gav50bXoCgUMfHWEkvE80IOmSFngRi8ujjVS73HOJB64Uk9JGtub7qNSkzF6/qw== X-Received: by 2002:a17:902:f812:b0:1c6:1ddf:5c3b with SMTP id ix18-20020a170902f81200b001c61ddf5c3bmr9725036plb.28.1696271863814; Mon, 02 Oct 2023 11:37:43 -0700 (PDT) Received: from hermes.local (204-195-126-68.wavecable.com. [204.195.126.68]) by smtp.gmail.com with ESMTPSA id iz7-20020a170902ef8700b001b9f7bc3e77sm12546595plb.189.2023.10.02.11.37.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Oct 2023 11:37:43 -0700 (PDT) From: Stephen Hemminger To: dev@dpdk.org Cc: Stephen Hemminger Subject: [PATCH v2 3/3] test: add tests for rte_ether routines Date: Mon, 2 Oct 2023 11:37:30 -0700 Message-Id: <20231002183730.301163-4-stephen@networkplumber.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231002183730.301163-1-stephen@networkplumber.org> References: <20230929163611.62691-1-stephen@networkplumber.org> <20231002183730.301163-1-stephen@networkplumber.org> 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 This add some basic tests for rte_unformat_ether_addr and other functions in rte_ether. Signed-off-by: Stephen Hemminger --- app/test/meson.build | 1 + app/test/test_net_ether.c | 169 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 170 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..a6d38cc2ffa0 --- /dev/null +++ b/app/test/test_net_ether.c @@ -0,0 +1,169 @@ +/* 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 addrss 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"); + + /* Test no-leading zero - Cisco format */ + memset(&result, 0, sizeof(result)); + ret = rte_ether_unformat_addr("102.304.506", &result); + RTE_TEST_ASSERT(ret == 0, "Cisco leading zero failed"); + RTE_TEST_ASSERT(rte_is_same_ether_addr(&nozero_ea, &result), + "Cisco leading zero mismatch"); + + return 0; +} + +static int +test_invalid_addr(void) +{ + static const char * const invalid[] = { + "NO:ADDRESS", + "123", + "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", + "", + }; + 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); -- 2.39.2