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 01E65A0C43; Fri, 15 Oct 2021 20:30:14 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 4361541174; Fri, 15 Oct 2021 20:29:24 +0200 (CEST) Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) by mails.dpdk.org (Postfix) with ESMTP id 5014F41135 for ; Fri, 15 Oct 2021 20:29:21 +0200 (CEST) Received: by mail-pl1-f182.google.com with SMTP id l6so6897433plh.9 for ; Fri, 15 Oct 2021 11:29:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=networkplumber-org.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=hsd4hyRm+yLhXhwT3QlCLF/JFJL7lO4nWjD+1h+uxVY=; b=qS8b6Yl5vDEj7P4SPhriahELnuBVUstRgXguv/hdts20p+bPb/Zo9/ZXSjCWZnDLhS iV1GPbN0nZYHsAVVa4QTRaLyje9xECFILFVxJq3TZE11b/XXvJD3vJ5kGfobPwaTi0zx UMPIDnYzMFEMc3i1clUNUNyf0BdlhHMMsL8XJRcsxX6nRyJkdivp4981JbnCKuFQvSDp bfwCYmpWqs82uUHC0CL8yM7HiLn8Wrpjrm1Ib/Zr/V0regXI+CwxHPETGlpD6yt3pNjI /8pZdvD2NSbTSNuVlM4WsjhIGUr2sJ5DHH3Hi4DG63968bxEqznCjVRUbhch2lF+pWrN 4zrw== 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=hsd4hyRm+yLhXhwT3QlCLF/JFJL7lO4nWjD+1h+uxVY=; b=4BRAwOK0bjoW9ci/C57neYZRjGW0XBgbsKF0+I7t3iBttkJtoGSGgCk7dDigy7MIfV /xrCDtIgYov5YsPUcU8gNPOVComo/ovjg9MdqiqIfyI1FddDmf36iafHYzl6uIlUkLis KbrTqObu+DNlTl3tlShQZ8YFsASH4U9n9xlgoOy8Kn7DXwgNejBYBg/4iRBtj8b5vvBI MSKbbyc+dBw7IjVnZi9EBqES9T1w3/ZJe1SRxlcCUg6NWPwWT11bQto4eQpAqTmUvScE CD1oFAqEBfz+OLX/2bp/i/9Ss8Kw509kC7rAVlJGJ8LXfBguLfLZAusbxjDyOLQUHfeH w7Hg== X-Gm-Message-State: AOAM533VBP11oDejpfFyxU3s2dSSVsH99faBE6VefaudXIpwKA4cAedl eMrSeq77x1so8rXHpjitz+UbI3ndK+6DCA== X-Google-Smtp-Source: ABdhPJygkOqrR9oB7SfC3a+a+EJA/8UMbEI2fz9icizx/cmroqrIwnaYQeTwEjcneAezCzmnGjq8Lg== X-Received: by 2002:a17:902:ab8c:b0:13a:22d1:88d with SMTP id f12-20020a170902ab8c00b0013a22d1088dmr12385570plr.33.1634322560040; Fri, 15 Oct 2021 11:29:20 -0700 (PDT) Received: from hermes.local (204-195-33-123.wavecable.com. [204.195.33.123]) by smtp.gmail.com with ESMTPSA id p189sm5526545pfp.167.2021.10.15.11.29.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Oct 2021 11:29:19 -0700 (PDT) From: Stephen Hemminger To: dev@dpdk.org Cc: Stephen Hemminger , Reshma Pattan Date: Fri, 15 Oct 2021 11:29:00 -0700 Message-Id: <20211015182903.52615-10-stephen@networkplumber.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20211015182903.52615-1-stephen@networkplumber.org> References: <20210903004732.109023-1-stephen@networkplumber.org> <20211015182903.52615-1-stephen@networkplumber.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [dpdk-dev] [PATCH v13 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 | 4 + app/test/test_pcapng.c | 272 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 276 insertions(+) create mode 100644 app/test/test_pcapng.c diff --git a/app/test/meson.build b/app/test/meson.build index f144d8b8ed68..56863f97acbb 100644 --- a/app/test/meson.build +++ b/app/test/meson.build @@ -396,6 +396,10 @@ if dpdk_conf.has('RTE_NET_RING') fast_tests += [['pdump_autotest', true]] endif +if dpdk_conf.has('RTE_PORT_PCAP') + test_sources += 'test_pcapng.c' +endif + if dpdk_conf.has('RTE_LIB_POWER') test_deps += 'power' endif diff --git a/app/test/test_pcapng.c b/app/test/test_pcapng.c new file mode 100644 index 000000000000..ed1e87f9445d --- /dev/null +++ b/app/test/test_pcapng.c @@ -0,0 +1,272 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright (c) 2021 Microsoft Corporation + */ + +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include + +#include "test.h" + +#define NUM_PACKETS 10 +#define DUMMY_MBUF_NUM 3 + +static rte_pcapng_t *pcapng; +static struct rte_mempool *mp; +static const uint32_t pkt_len = 200; +static uint16_t port_id; +static char file_name[] = "/tmp/pcapng_test_XXXXXX.pcapng"; + +/* 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 one packets */ +static void +mbuf1_prepare(struct dummy_mbuf *dm, uint32_t plen) +{ + struct { + struct rte_ether_hdr eth; + struct rte_ipv4_hdr ip; + } pkt = { + .eth = { + .dst_addr.addr_bytes = "\xff\xff\xff\xff\xff\xff", + .ether_type = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4), + }, + .ip = { + .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); + + rte_eth_random_addr(pkt.eth.src_addr.addr_bytes); + memcpy(rte_pktmbuf_mtod(dm->mb, void *), &pkt, RTE_MIN(sizeof(pkt), plen)); +} + +static int +test_setup(void) +{ + 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_name, strlen(".pcapng")); + if (tmp_fd == -1) { + perror("mkstemps() failure"); + return -1; + } + printf("pcapng: output file %s\n", file_name); + + /* 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", NUM_PACKETS, + 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_write_packets(void) +{ + struct rte_mbuf *orig; + struct rte_mbuf *clones[NUM_PACKETS] = { }; + struct dummy_mbuf mbfs; + unsigned int i; + ssize_t len; + + /* make a dummy packet */ + mbuf1_prepare(&mbfs, pkt_len); + + /* clone them */ + orig = &mbfs.mb[0]; + for (i = 0; i < NUM_PACKETS; i++) { + struct rte_mbuf *mc; + + mc = rte_pcapng_copy(port_id, 0, orig, mp, pkt_len, + rte_get_tsc_cycles(), 0); + if (mc == NULL) { + fprintf(stderr, "Cannot copy packet\n"); + return -1; + } + clones[i] = mc; + } + + /* write it to capture file */ + len = rte_pcapng_write_packets(pcapng, clones, NUM_PACKETS); + + rte_pktmbuf_free_bulk(clones, NUM_PACKETS); + + if (len <= 0) { + fprintf(stderr, "Write of packets failed\n"); + return -1; + } + + 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, + NUM_PACKETS, 0); + if (len <= 0) { + fprintf(stderr, "Write of statistics failed\n"); + return -1; + } + return 0; +} + +static void +pkt_print(u_char *user, const struct pcap_pkthdr *h, + const u_char *bytes) +{ + unsigned int *countp = (unsigned int *)user; + const struct rte_ether_hdr *eh; + struct tm *tm; + char tbuf[128], src[64], dst[64]; + + tm = localtime(&h->ts.tv_sec); + if (tm == NULL) { + perror("localtime"); + return; + } + + if (strftime(tbuf, sizeof(tbuf), "%X", tm) == 0) { + fprintf(stderr, "strftime returned 0!\n"); + return; + } + + eh = (const struct rte_ether_hdr *)bytes; + rte_ether_format_addr(dst, sizeof(dst), &eh->dst_addr); + rte_ether_format_addr(src, sizeof(src), &eh->src_addr); + printf("%s.%06lu: %s -> %s type %x length %u\n", + tbuf, (unsigned long)h->ts.tv_usec, + src, dst, rte_be_to_cpu_16(eh->ether_type), h->len); + + *countp += 1; +} + +/* + * Open the resulting pcapng file with libpcap + * Would be better to use capinfos from wireshark + * but that creates an unwanted dependency. + */ +static int +test_validate(void) +{ + char errbuf[PCAP_ERRBUF_SIZE]; + unsigned int count = 0; + pcap_t *pcap; + int ret; + + pcap = pcap_open_offline(file_name, errbuf); + if (pcap == NULL) { + fprintf(stderr, "pcap_open_offline('%s') failed: %s\n", + file_name, errbuf); + return -1; + } + + ret = pcap_loop(pcap, 0, pkt_print, (u_char *)&count); + if (ret == 0) + printf("Saw %u packets\n", count); + else + fprintf(stderr, "pcap_dispatch: failed: %s\n", + pcap_geterr(pcap)); + pcap_close(pcap); + + return ret; +} + +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_write_packets), + TEST_CASE(test_write_stats), + TEST_CASE(test_validate), + 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