1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright (c) 2021 Microsoft Corporation
9 #include <rte_ethdev.h>
10 #include <rte_ether.h>
12 #include <rte_mempool.h>
14 #include <rte_pcapng.h>
16 #include <pcap/pcap.h>
20 #define NUM_PACKETS 10
21 #define DUMMY_MBUF_NUM 3
23 static rte_pcapng_t *pcapng;
24 static struct rte_mempool *mp;
25 static const uint32_t pkt_len = 200;
26 static uint16_t port_id;
27 static char file_name[] = "/tmp/pcapng_test_XXXXXX.pcapng";
29 /* first mbuf in the packet, should always be at offset 0 */
31 struct rte_mbuf mb[DUMMY_MBUF_NUM];
32 uint8_t buf[DUMMY_MBUF_NUM][RTE_MBUF_DEFAULT_BUF_SIZE];
36 dummy_mbuf_prep(struct rte_mbuf *mb, uint8_t buf[], uint32_t buf_len,
43 mb->buf_iova = (uintptr_t)buf;
44 mb->buf_len = buf_len;
45 rte_mbuf_refcnt_set(mb, 1);
47 /* set pool pointer to dummy value, test doesn't use it */
48 mb->pool = (void *)buf;
50 rte_pktmbuf_reset(mb);
51 db = (uint8_t *)rte_pktmbuf_append(mb, data_len);
53 for (i = 0; i != data_len; i++)
57 /* Make an IP packet consisting of chain of one packets */
59 mbuf1_prepare(struct dummy_mbuf *dm, uint32_t plen)
62 struct rte_ether_hdr eth;
63 struct rte_ipv4_hdr ip;
66 .dst_addr.addr_bytes = "\xff\xff\xff\xff\xff\xff",
67 .ether_type = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4),
70 .version_ihl = RTE_IPV4_VHL_DEF,
71 .total_length = rte_cpu_to_be_16(plen),
72 .time_to_live = IPDEFTTL,
73 .next_proto_id = IPPROTO_RAW,
74 .src_addr = rte_cpu_to_be_32(RTE_IPV4_LOOPBACK),
75 .dst_addr = rte_cpu_to_be_32(RTE_IPV4_BROADCAST),
79 memset(dm, 0, sizeof(*dm));
80 dummy_mbuf_prep(&dm->mb[0], dm->buf[0], sizeof(dm->buf[0]), plen);
82 rte_eth_random_addr(pkt.eth.src_addr.addr_bytes);
83 memcpy(rte_pktmbuf_mtod(dm->mb, void *), &pkt, RTE_MIN(sizeof(pkt), plen));
91 port_id = rte_eth_find_next(0);
92 if (port_id >= RTE_MAX_ETHPORTS) {
93 fprintf(stderr, "No valid Ether port\n");
97 tmp_fd = mkstemps(file_name, strlen(".pcapng"));
99 perror("mkstemps() failure");
102 printf("pcapng: output file %s\n", file_name);
104 /* open a test capture file */
105 pcapng = rte_pcapng_fdopen(tmp_fd, NULL, NULL, "pcapng_test", NULL);
106 if (pcapng == NULL) {
107 fprintf(stderr, "rte_pcapng_fdopen failed\n");
112 /* Make a pool for cloned packeets */
113 mp = rte_pktmbuf_pool_create_by_ops("pcapng_test_pool", NUM_PACKETS,
115 rte_pcapng_mbuf_size(pkt_len),
116 SOCKET_ID_ANY, "ring_mp_sc");
118 fprintf(stderr, "Cannot create mempool\n");
125 test_write_packets(void)
127 struct rte_mbuf *orig;
128 struct rte_mbuf *clones[NUM_PACKETS] = { };
129 struct dummy_mbuf mbfs;
133 /* make a dummy packet */
134 mbuf1_prepare(&mbfs, pkt_len);
138 for (i = 0; i < NUM_PACKETS; i++) {
141 mc = rte_pcapng_copy(port_id, 0, orig, mp, pkt_len,
142 rte_get_tsc_cycles(), 0);
144 fprintf(stderr, "Cannot copy packet\n");
150 /* write it to capture file */
151 len = rte_pcapng_write_packets(pcapng, clones, NUM_PACKETS);
153 rte_pktmbuf_free_bulk(clones, NUM_PACKETS);
156 fprintf(stderr, "Write of packets failed\n");
164 test_write_stats(void)
168 /* write a statistics block */
169 len = rte_pcapng_write_stats(pcapng, port_id,
173 fprintf(stderr, "Write of statistics failed\n");
180 pkt_print(u_char *user, const struct pcap_pkthdr *h,
183 unsigned int *countp = (unsigned int *)user;
184 const struct rte_ether_hdr *eh;
186 char tbuf[128], src[64], dst[64];
188 tm = localtime(&h->ts.tv_sec);
194 if (strftime(tbuf, sizeof(tbuf), "%X", tm) == 0) {
195 fprintf(stderr, "strftime returned 0!\n");
199 eh = (const struct rte_ether_hdr *)bytes;
200 rte_ether_format_addr(dst, sizeof(dst), &eh->dst_addr);
201 rte_ether_format_addr(src, sizeof(src), &eh->src_addr);
202 printf("%s.%06lu: %s -> %s type %x length %u\n",
203 tbuf, (unsigned long)h->ts.tv_usec,
204 src, dst, rte_be_to_cpu_16(eh->ether_type), h->len);
210 * Open the resulting pcapng file with libpcap
211 * Would be better to use capinfos from wireshark
212 * but that creates an unwanted dependency.
217 char errbuf[PCAP_ERRBUF_SIZE];
218 unsigned int count = 0;
222 pcap = pcap_open_offline(file_name, errbuf);
224 fprintf(stderr, "pcap_open_offline('%s') failed: %s\n",
229 ret = pcap_loop(pcap, 0, pkt_print, (u_char *)&count);
231 printf("Saw %u packets\n", count);
233 fprintf(stderr, "pcap_dispatch: failed: %s\n",
244 rte_mempool_free(mp);
247 rte_pcapng_close(pcapng);
252 unit_test_suite test_pcapng_suite = {
254 .teardown = test_cleanup,
255 .suite_name = "Test Pcapng Unit Test Suite",
257 TEST_CASE(test_write_packets),
258 TEST_CASE(test_write_stats),
259 TEST_CASE(test_validate),
267 return unit_test_suite_runner(&test_pcapng_suite);
270 REGISTER_TEST_COMMAND(pcapng_autotest, test_pcapng);