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 AD260A0C45; Mon, 13 Sep 2021 20:16:09 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id E61D14113F; Mon, 13 Sep 2021 20:15:30 +0200 (CEST) Received: from mail-pj1-f44.google.com (mail-pj1-f44.google.com [209.85.216.44]) by mails.dpdk.org (Postfix) with ESMTP id D3AF640E28 for ; Mon, 13 Sep 2021 20:15:23 +0200 (CEST) Received: by mail-pj1-f44.google.com with SMTP id gp20-20020a17090adf1400b00196b761920aso664759pjb.3 for ; Mon, 13 Sep 2021 11:15:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=networkplumber-org.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=S0wgL5DgJ+cdmzD3DfnVIW8B/xg3YdbgKzAOs1++iHM=; b=kq/OwsVSRvYvqemhNN//Zf4H7AgI1xgvs9cjVopFQHfm42UAOOv4B9XqMfh+iO+gzG NfER9rIQg0YtJUVXtdTp9CqYzzJ4I2Yy/V/cGzTyWdHW8ZPV5rLcfS+VUcQFU4z2D5Mp cjKEPyNnvwdjbW/p/xMkrA0YCyVKXc6zlpr1/tbWCijZRESTt1E5m+8v9PnYf9bgCuvY DKUVygkQASqsf97MITKyTWq+1UABqfsv4p/TLdfHMQN5/A6GixmH6ff4zIb1MootUIWN CkZTbEbbAO6Zyl5hfW4/jWtQTZKJIuV+aXtDUjblg5e+BQrlYZPBxgjQX81NhSGKFkgv Chlw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=S0wgL5DgJ+cdmzD3DfnVIW8B/xg3YdbgKzAOs1++iHM=; b=3ex8MA2smZPsz21fzHTRBVWsaYmbpKhOL+9NuS7y/RsHyQxG/kLOMheOlFon3fF0+C l14FoyVU+34RqtIqgrXhuoZGCA2XpELo0y3ZdyQ7wfONY7hjA+I1T7KzsRnKr/HjLhNw 6I2n4wLEUN29q2+YiUqNPaNwa90jzVLY1cJcQrQnGb7D8S+vkd8OF9VXjAG7mo9DUhJO gVADElcZNXkW4nvAy6Xk8mdzU50Gouw6AsDzr/NQx8ejrtmFKA1FqkTefr1AtHjxlCCE jkZWt+hX6nT0QTbT/WGblbNWP5/OxVt3TvffRQcbiRplK2gvpZmHg/iCiX3stOOEPOrm SbnA== X-Gm-Message-State: AOAM530UUJUj5DDVCCpKkQkgKIwwJadObLjjZIoa5WPvtwY+OfGgMVw0 GTBAGLd5edofOFVldivhoa4aSv56GWJiRg== X-Google-Smtp-Source: ABdhPJy06BFr71ssS3GfqJjj3ZvyTl08aAP7BnX/zM9/Uhvu/yo0hVRtPH2Tv/fKHA+9E7wueMrYjQ== X-Received: by 2002:a17:90a:29a4:: with SMTP id h33mr853970pjd.88.1631556922665; Mon, 13 Sep 2021 11:15:22 -0700 (PDT) Received: from hermes.local (204-195-33-123.wavecable.com. [204.195.33.123]) by smtp.gmail.com with ESMTPSA id b10sm7616888pfl.220.2021.09.13.11.15.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Sep 2021 11:15:21 -0700 (PDT) From: Stephen Hemminger To: dev@dpdk.org Cc: Stephen Hemminger Date: Mon, 13 Sep 2021 11:15:07 -0700 Message-Id: <20210913181510.46058-10-stephen@networkplumber.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210913181510.46058-1-stephen@networkplumber.org> References: <20210903004732.109023-1-stephen@networkplumber.org> <20210913181510.46058-1-stephen@networkplumber.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [dpdk-dev] [PATCH v8 09/12] test: add a test for pcapng library 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 Sender: "dev" Simple unit test that created pcapng file using API. To run this test you need to have at least one device. For example: DPDK_TEST=pcapng_autotest ./build/app/test/dpdk-test -l 0-15 \ --no-huge -m 2048 --vdev=net_tap,iface=dummy Signed-off-by: Stephen Hemminger --- app/test/meson.build | 1 + app/test/test_pcapng.c | 190 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 191 insertions(+) create mode 100644 app/test/test_pcapng.c diff --git a/app/test/meson.build b/app/test/meson.build index a7611686adcb..0d551ac9c2b2 100644 --- a/app/test/meson.build +++ b/app/test/meson.build @@ -97,6 +97,7 @@ test_sources = files( 'test_metrics.c', 'test_mcslock.c', 'test_mp_secondary.c', + 'test_pcapng.c', 'test_per_lcore.c', 'test_pflock.c', 'test_pmd_perf.c', diff --git a/app/test/test_pcapng.c b/app/test/test_pcapng.c new file mode 100644 index 000000000000..6bf993ad30f6 --- /dev/null +++ b/app/test/test_pcapng.c @@ -0,0 +1,190 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright (c) 2021 Microsoft Corporation + */ + +#include +#include +#include + +#include +#include +#include +#include +#include + +#include "test.h" + +#define DUMMY_MBUF_NUM 3 + +static rte_pcapng_t *pcapng; +static struct rte_mempool *mp; +static const uint32_t pkt_len = 200; +static uint64_t ifrecv, ifdrop; +static uint16_t port_id; + +/* first mbuf in the packet, should always be at offset 0 */ +struct dummy_mbuf { + struct rte_mbuf mb[DUMMY_MBUF_NUM]; + uint8_t buf[DUMMY_MBUF_NUM][RTE_MBUF_DEFAULT_BUF_SIZE]; +}; + +static void +dummy_mbuf_prep(struct rte_mbuf *mb, uint8_t buf[], uint32_t buf_len, + uint32_t data_len) +{ + uint32_t i; + uint8_t *db; + + mb->buf_addr = buf; + mb->buf_iova = (uintptr_t)buf; + mb->buf_len = buf_len; + rte_mbuf_refcnt_set(mb, 1); + + /* set pool pointer to dummy value, test doesn't use it */ + mb->pool = (void *)buf; + + rte_pktmbuf_reset(mb); + db = (uint8_t *)rte_pktmbuf_append(mb, data_len); + + for (i = 0; i != data_len; i++) + db[i] = i; +} + +/* Make an IP packet consisting of chain of two packets */ +static void +mbuf1_prepare(struct dummy_mbuf *dm, uint32_t plen) +{ + struct rte_ipv4_hdr *ph; + const struct rte_ipv4_hdr iph = { + .version_ihl = RTE_IPV4_VHL_DEF, + .total_length = rte_cpu_to_be_16(plen), + .time_to_live = IPDEFTTL, + .next_proto_id = IPPROTO_RAW, + .src_addr = rte_cpu_to_be_32(RTE_IPV4_LOOPBACK), + .dst_addr = rte_cpu_to_be_32(RTE_IPV4_BROADCAST), + }; + + memset(dm, 0, sizeof(*dm)); + dummy_mbuf_prep(&dm->mb[0], dm->buf[0], sizeof(dm->buf[0]), plen); + + ph = rte_pktmbuf_mtod(dm->mb, typeof(ph)); + memcpy(ph, &iph, sizeof(iph)); +} + +static int +test_setup(void) +{ + char file_template[] = "/tmp/pcapng_test_XXXXXX.pcapng"; + int tmp_fd; + + port_id = rte_eth_find_next(0); + if (port_id >= RTE_MAX_ETHPORTS) { + fprintf(stderr, "No valid Ether port\n"); + return -1; + } + + tmp_fd = mkstemps(file_template, strlen(".pcapng")); + if (tmp_fd == -1) { + perror("mkstemps() failure"); + return -1; + } + + /* open a test capture file */ + pcapng = rte_pcapng_fdopen(tmp_fd, NULL, NULL, "pcapng_test", NULL); + if (pcapng == NULL) { + fprintf(stderr, "rte_pcapng_fdopen failed\n"); + close(tmp_fd); + return -1; + } + + /* Make a pool for cloned packeets */ + mp = rte_pktmbuf_pool_create_by_ops("pcapng_test_pool", DUMMY_MBUF_NUM, + 0, 0, + rte_pcapng_mbuf_size(pkt_len), + SOCKET_ID_ANY, "ring_mp_sc"); + if (mp == NULL) { + fprintf(stderr, "Cannot create mempool\n"); + return -1; + } + return 0; + +} + +static int +test_basic_packets(void) +{ + struct rte_mbuf *orig, *clone = NULL; + struct dummy_mbuf mbfs; + ssize_t len; + + /* make a dummy packet */ + mbuf1_prepare(&mbfs, pkt_len); + + /* clone them */ + orig = &mbfs.mb[0]; + clone = rte_pcapng_copy(port_id, 0, orig, mp, pkt_len, + rte_get_tsc_cycles(), 0); + if (clone == NULL) { + fprintf(stderr, "Cannot copy packet\n"); + return -1; + } + + /* write it to capture file */ + len = rte_pcapng_write_packets(pcapng, &clone, 1); + rte_pktmbuf_free(clone); + + if (len <= 0) { + fprintf(stderr, "Write of packets failed\n"); + return -1; + } + + ++ifrecv; + return 0; +} + +static int +test_write_stats(void) +{ + ssize_t len; + + /* write a statistics block */ + len = rte_pcapng_write_stats(pcapng, port_id, + NULL, 0, 0, + ifrecv, ifdrop); + if (len <= 0) { + fprintf(stderr, "Write of statistics failed\n"); + return -1; + } + return 0; +} + +static void +test_cleanup(void) +{ + if (mp) + rte_mempool_free(mp); + + if (pcapng) + rte_pcapng_close(pcapng); + +} + +static struct +unit_test_suite test_pcapng_suite = { + .setup = test_setup, + .teardown = test_cleanup, + .suite_name = "Test Pcapng Unit Test Suite", + .unit_test_cases = { + TEST_CASE(test_basic_packets), + TEST_CASE(test_write_stats), + TEST_CASES_END() + } +}; + +static int +test_pcapng(void) +{ + return unit_test_suite_runner(&test_pcapng_suite); +} + +REGISTER_TEST_COMMAND(pcapng_autotest, test_pcapng); -- 2.30.2