Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright (c) 2021 Microsoft Corporation
3 : : */
4 : :
5 : : #include <inttypes.h>
6 : : #include <stdio.h>
7 : : #include <stdlib.h>
8 : : #include <string.h>
9 : : #include <time.h>
10 : :
11 : : #ifdef RTE_EXEC_ENV_WINDOWS
12 : : #include <winsock2.h>
13 : : #include <io.h>
14 : : #include <fcntl.h>
15 : : #include <windows.h>
16 : : #else
17 : : #include <unistd.h>
18 : : #endif
19 : :
20 : : #include <rte_bus_vdev.h>
21 : : #include <rte_ethdev.h>
22 : : #include <rte_ether.h>
23 : : #include <rte_ip.h>
24 : : #include <rte_mbuf.h>
25 : : #include <rte_mempool.h>
26 : : #include <rte_net.h>
27 : : #include <rte_pcapng.h>
28 : : #include <rte_random.h>
29 : : #include <rte_reciprocal.h>
30 : : #include <rte_time.h>
31 : : #include <rte_udp.h>
32 : :
33 : : #include <pcap/pcap.h>
34 : :
35 : : #include "test.h"
36 : :
37 : : #ifdef RTE_EXEC_ENV_WINDOWS
38 : : static uint64_t
39 : : current_timestamp(void)
40 : : {
41 : : FILETIME ft;
42 : : ULARGE_INTEGER ul;
43 : :
44 : : GetSystemTimeAsFileTime(&ft);
45 : : ul.LowPart = ft.dwLowDateTime;
46 : : ul.HighPart = ft.dwHighDateTime;
47 : : /* FILETIME is 100ns intervals since 1601-01-01, convert to ns since Unix epoch */
48 : : return (ul.QuadPart - 116444736000000000ULL) * 100;
49 : : }
50 : :
51 : : /*
52 : : * Create temporary file with suffix for Windows.
53 : : * Returns file descriptor or -1 on failure.
54 : : */
55 : : static int
56 : : mkstemps(char *tmpl, int suffixlen)
57 : : {
58 : : char temp_dir[MAX_PATH];
59 : : char temp_file[MAX_PATH];
60 : : DWORD ret;
61 : :
62 : : ret = GetTempPathA(sizeof(temp_dir), temp_dir);
63 : : if (ret == 0 || ret > sizeof(temp_dir))
64 : : return -1;
65 : :
66 : : if (GetTempFileNameA(temp_dir, "pcap", 0, temp_file) == 0)
67 : : return -1;
68 : :
69 : : /*
70 : : * GetTempFileNameA with uUnique=0 creates the file to reserve the name.
71 : : * Remove it since we open a different name with the original suffix appended.
72 : : */
73 : : DeleteFileA(temp_file);
74 : :
75 : : /* Append the original suffix (e.g. ".pcapng") to the temp file */
76 : : strlcat(temp_file, tmpl + strlen(tmpl) - suffixlen, sizeof(temp_file));
77 : : strlcpy(tmpl, temp_file, PATH_MAX);
78 : :
79 : : return _open(tmpl, _O_RDWR | _O_BINARY | _O_CREAT | _O_EXCL, 0666);
80 : : }
81 : : #endif /* RTE_EXEC_ENV_WINDOWS */
82 : :
83 : : #define PCAPNG_TEST_DEBUG 0
84 : :
85 : : /*
86 : : * Want to write enough packets to exercise timestamp logic.
87 : : * On fast CPU's TSC wraps around 32 bits in 4 seconds.
88 : : */
89 : : #define TOTAL_PACKETS 10000
90 : : #define MAX_BURST 32
91 : : #define NUM_BURSTS (TOTAL_PACKETS / MAX_BURST)
92 : : #define TEST_TIME_SEC 4
93 : : #define GAP_US ((TEST_TIME_SEC * US_PER_S) / NUM_BURSTS)
94 : :
95 : : #define DUMMY_MBUF_NUM 2
96 : :
97 : : static struct rte_mempool *mp;
98 : : static uint16_t port_id;
99 : : static const char null_dev[] = "net_null0";
100 : :
101 : : /* first mbuf in the packet, should always be at offset 0 */
102 : : struct dummy_mbuf {
103 : : struct rte_mbuf mb[DUMMY_MBUF_NUM];
104 : : uint8_t buf[DUMMY_MBUF_NUM][RTE_MBUF_DEFAULT_BUF_SIZE];
105 : : };
106 : :
107 : : #define MAX_DATA_SIZE (RTE_MBUF_DEFAULT_BUF_SIZE - RTE_PKTMBUF_HEADROOM)
108 : :
109 : : /* RFC 864 chargen pattern used for comment testing */
110 : : #define FILL_LINE_LENGTH 72
111 : : #define FILL_START 0x21 /* ! */
112 : : #define FILL_END 0x7e /* ~ */
113 : : #define FILL_RANGE (FILL_END - FILL_START)
114 : :
115 : : static void
116 : 3 : fill_mbuf(struct rte_mbuf *mb)
117 : : {
118 : 3 : unsigned int len = rte_pktmbuf_tailroom(mb);
119 : : char *buf = rte_pktmbuf_append(mb, len);
120 : : unsigned int n = 0;
121 : : unsigned int line = 0;
122 : :
123 [ + - ]: 3 : if (len == 0)
124 : : return;
125 : :
126 [ + + ]: 90 : while (n < len - 1) {
127 : 87 : char ch = FILL_START + (line % FILL_RANGE);
128 : : unsigned int i;
129 : :
130 [ + + + + ]: 6144 : for (i = 0; i < FILL_LINE_LENGTH && n < len - 1; i++) {
131 : 6057 : buf[n++] = ch;
132 [ + + ]: 6057 : if (++ch > FILL_END)
133 : : ch = FILL_START;
134 : : }
135 [ + + ]: 87 : if (n < len - 1)
136 : 84 : buf[n++] = '\n';
137 : 87 : line++;
138 : : }
139 : : }
140 : :
141 : : static void
142 : 6 : dummy_mbuf_prep(struct rte_mbuf *mb, uint8_t buf[], uint32_t buf_len)
143 : : {
144 : 6 : mb->buf_addr = buf;
145 : 6 : rte_mbuf_iova_set(mb, (uintptr_t)buf);
146 : 6 : mb->buf_len = buf_len;
147 : : rte_mbuf_refcnt_set(mb, 1);
148 : :
149 : : /* set pool pointer to dummy value, test doesn't use it */
150 : 6 : mb->pool = (void *)buf;
151 : :
152 : : rte_pktmbuf_reset(mb);
153 : 6 : }
154 : :
155 : : /* Make an IP packet consisting of chain of one packets */
156 : : static void
157 : 3 : mbuf1_prepare(struct dummy_mbuf *dm)
158 : : {
159 : : struct {
160 : : struct rte_ether_hdr eth;
161 : : struct rte_ipv4_hdr ip;
162 : : struct rte_udp_hdr udp;
163 : 3 : } pkt = {
164 : : .eth = {
165 : : .dst_addr.addr_bytes = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
166 : : .ether_type = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4),
167 : : },
168 : : .ip = {
169 : : .version_ihl = RTE_IPV4_VHL_DEF,
170 : : .time_to_live = 1,
171 : : .next_proto_id = IPPROTO_UDP,
172 : : .src_addr = rte_cpu_to_be_32(RTE_IPV4_LOOPBACK),
173 : : .dst_addr = rte_cpu_to_be_32(RTE_IPV4_BROADCAST),
174 : : },
175 : : .udp = {
176 : : .src_port = rte_cpu_to_be_16(19), /* Chargen port */
177 : : .dst_port = rte_cpu_to_be_16(9), /* Discard port */
178 : : },
179 : : };
180 : :
181 : : memset(dm, 0, sizeof(*dm));
182 : 3 : dummy_mbuf_prep(&dm->mb[0], dm->buf[0], sizeof(dm->buf[0]));
183 : 3 : dummy_mbuf_prep(&dm->mb[1], dm->buf[1], sizeof(dm->buf[1]));
184 : :
185 : 3 : rte_eth_random_addr(pkt.eth.src_addr.addr_bytes);
186 : : memcpy(rte_pktmbuf_append(&dm->mb[0], sizeof(pkt)), &pkt, sizeof(pkt));
187 : :
188 : 3 : fill_mbuf(&dm->mb[1]);
189 : : rte_pktmbuf_chain(&dm->mb[0], &dm->mb[1]);
190 : :
191 : 3 : rte_mbuf_sanity_check(&dm->mb[0], 1);
192 : 3 : rte_mbuf_sanity_check(&dm->mb[1], 0);
193 : 3 : }
194 : :
195 : : static void
196 : 2636 : mbuf1_resize(struct dummy_mbuf *dm, uint16_t len)
197 : : {
198 : : struct {
199 : : struct rte_ether_hdr eth;
200 : : struct rte_ipv4_hdr ip;
201 : : struct rte_udp_hdr udp;
202 : 2636 : } *pkt = rte_pktmbuf_mtod(&dm->mb[0], void *);
203 : :
204 : 2636 : dm->mb[1].data_len = len;
205 : 2636 : dm->mb[0].pkt_len = dm->mb[0].data_len + dm->mb[1].data_len;
206 : :
207 : 2636 : len += sizeof(struct rte_udp_hdr);
208 [ - + ]: 2636 : pkt->udp.dgram_len = rte_cpu_to_be_16(len);
209 : :
210 : 2636 : len += sizeof(struct rte_ipv4_hdr);
211 [ - + ]: 2636 : pkt->ip.total_length = rte_cpu_to_be_16(len);
212 : 2636 : pkt->ip.hdr_checksum = 0;
213 : 2636 : pkt->ip.hdr_checksum = rte_ipv4_cksum(&pkt->ip);
214 : :
215 : 2636 : rte_mbuf_sanity_check(&dm->mb[0], 1);
216 : 2636 : rte_mbuf_sanity_check(&dm->mb[1], 0);
217 : 2636 : }
218 : :
219 : : static int
220 : 1 : test_setup(void)
221 : : {
222 : 1 : port_id = rte_eth_dev_count_avail();
223 : :
224 : : /* Make a dummy null device to snoop on */
225 [ - + ]: 1 : if (rte_vdev_init(null_dev, NULL) != 0) {
226 : : printf("Failed to create vdev '%s'\n", null_dev);
227 : 0 : goto fail;
228 : : }
229 : :
230 : : /* Make a pool for cloned packets */
231 : 1 : mp = rte_pktmbuf_pool_create_by_ops("pcapng_test_pool",
232 : : MAX_BURST * 32, 0, 0,
233 : 1 : rte_pcapng_mbuf_size(MAX_DATA_SIZE),
234 : : SOCKET_ID_ANY, "ring_mp_sc");
235 [ - + ]: 1 : if (mp == NULL) {
236 : : printf("Cannot create mempool\n");
237 : 0 : goto fail;
238 : : }
239 : :
240 : : return 0;
241 : :
242 : 0 : fail:
243 : 0 : rte_vdev_uninit(null_dev);
244 : 0 : rte_mempool_free(mp);
245 : 0 : return -1;
246 : : }
247 : :
248 : : static int
249 : 1 : fill_pcapng_file(rte_pcapng_t *pcapng)
250 : : {
251 : : struct dummy_mbuf mbfs;
252 : : struct rte_mbuf *orig;
253 : : unsigned int burst_size;
254 : : unsigned int count;
255 : : struct timespec start_time;
256 : : ssize_t len;
257 : : /*
258 : : * These are some silly comments to test various lengths and alignments sprinkle
259 : : * into the file. You can see these comments by using the dumpcap program on the file
260 : : */
261 : : static const char * const examples[] = {
262 : : "Lockless and fearless - that’s how we roll in userspace.",
263 : : "Memory pool deep / Mbufs swim in lockless rings / Zero copy dreams,",
264 : : "Poll mode driver waits / No interrupts disturb its zen / Busy loop finds peace,",
265 : : "Memory barriers / rte_atomic_thread_fence() / Guards our shared state",
266 : : "Hugepages so vast / Two megabytes of glory / TLB misses weep",
267 : : "Packets flow like streams / Through the graph node pipeline / Iterate in place",
268 : :
269 : : /* Long one to make sure we can do > 256 characters */
270 : : ("Dear future maintainer: I am sorry. This packet was captured at 3 AM while "
271 : : "debugging a priority flow control issue that turned out to be a loose cable. "
272 : : "The rte_eth_tx_burst() call you see here has been cargo-culted through four "
273 : : "generations of example code. The magic number 32 is not documented because "
274 : : "nobody remembers why. Trust the process."),
275 : : };
276 : :
277 : 1 : mbuf1_prepare(&mbfs);
278 : : orig = &mbfs.mb[0];
279 : :
280 : 1 : clock_gettime(CLOCK_MONOTONIC, &start_time);
281 : :
282 [ + - ]: 151 : for (count = 0; count < TOTAL_PACKETS; count += burst_size) {
283 : : struct rte_mbuf *clones[MAX_BURST];
284 : : struct timespec now;
285 : : unsigned int i;
286 : :
287 : : /* break off writing if test is taking too long */
288 : 151 : clock_gettime(CLOCK_MONOTONIC, &now);
289 [ + + ]: 151 : if (now.tv_sec >= start_time.tv_sec + TEST_TIME_SEC)
290 : : break;
291 : :
292 : : /* put 1 .. MAX_BURST packets in one write call */
293 : 150 : burst_size = rte_rand_max(MAX_BURST) + 1;
294 [ + + ]: 2784 : for (i = 0; i < burst_size; i++) {
295 : : struct rte_mbuf *mc;
296 : : const char *comment = NULL;
297 : :
298 : : /* Put randomized comment on every 100th packet (1%) */
299 [ + + ]: 2634 : if (count % 100 == 0)
300 : 29 : comment = examples[rte_rand_max(RTE_DIM(examples))];
301 : :
302 : : /* Vary the size of the packets, okay to allow 0 sized packet */
303 : 2634 : mbuf1_resize(&mbfs, rte_rand_max(MAX_DATA_SIZE));
304 : :
305 : 2634 : mc = rte_pcapng_copy(port_id, 0, orig, mp, rte_pktmbuf_pkt_len(orig),
306 : : RTE_PCAPNG_DIRECTION_IN, comment);
307 [ - + ]: 2634 : if (mc == NULL) {
308 : : printf("Cannot copy packet\n");
309 : 0 : return -1;
310 : : }
311 : 2634 : clones[i] = mc;
312 : : }
313 : :
314 : : /* write it to capture file */
315 : 150 : len = rte_pcapng_write_packets(pcapng, clones, burst_size);
316 : 150 : rte_pktmbuf_free_bulk(clones, burst_size);
317 : :
318 [ - + ]: 150 : if (len <= 0) {
319 : 0 : printf("Write of packets failed: %s\n",
320 : : rte_strerror(rte_errno));
321 : 0 : return -1;
322 : : }
323 : :
324 : 150 : rte_delay_us_block(rte_rand_max(2 * GAP_US));
325 : : }
326 : :
327 : 1 : return count;
328 : : }
329 : :
330 : : /* Convert time in nanoseconds since 1/1/1970 to UTC time string */
331 : : static char *
332 : 0 : fmt_time(char *buf, size_t size, uint64_t ts_ns)
333 : : {
334 : : time_t sec;
335 : : size_t len;
336 : :
337 : 0 : sec = ts_ns / NS_PER_S;
338 : :
339 : 0 : len = strftime(buf, size, "%T", gmtime(&sec));
340 : 0 : snprintf(buf + len, size - len, ".%09lu",
341 : 0 : (unsigned long)(ts_ns % NS_PER_S));
342 : :
343 : 0 : return buf;
344 : : }
345 : :
346 : : /* Context for the pcap_loop callback */
347 : : struct pkt_print_ctx {
348 : : pcap_t *pcap;
349 : : unsigned int count;
350 : : uint64_t start_ns;
351 : : uint64_t end_ns;
352 : : };
353 : :
354 : : static void
355 : 0 : print_packet(uint64_t ts_ns, const struct rte_ether_hdr *eh, size_t len)
356 : : {
357 : : char tbuf[128], src[64], dst[64];
358 : :
359 : 0 : fmt_time(tbuf, sizeof(tbuf), ts_ns);
360 : 0 : rte_ether_format_addr(dst, sizeof(dst), &eh->dst_addr);
361 : 0 : rte_ether_format_addr(src, sizeof(src), &eh->src_addr);
362 : 0 : printf("%s: %s -> %s type %x length %zu\n",
363 [ # # ]: 0 : tbuf, src, dst, rte_be_to_cpu_16(eh->ether_type), len);
364 : 0 : fflush(stdout);
365 : 0 : }
366 : :
367 : : /* Callback from pcap_loop used to validate packets in the file */
368 : : static void
369 : 2642 : parse_pcap_packet(u_char *user, const struct pcap_pkthdr *h,
370 : : const u_char *bytes)
371 : : {
372 : : struct pkt_print_ctx *ctx = (struct pkt_print_ctx *)user;
373 : : const struct rte_ether_hdr *eh;
374 : : const struct rte_ipv4_hdr *ip;
375 : : static unsigned int total_errors;
376 : : uint64_t ns;
377 : :
378 : : eh = (const struct rte_ether_hdr *)bytes;
379 : 2642 : ip = (const struct rte_ipv4_hdr *)(eh + 1);
380 : :
381 : 2642 : ctx->count += 1;
382 : :
383 : : /* The pcap library is misleading in reporting timestamp.
384 : : * packet header struct gives timestamp as a timeval (ie. usec);
385 : : * but the file is open in nanonsecond mode therefore
386 : : * the timestamp is really in timespec (ie. nanoseconds).
387 : : */
388 : 2642 : ns = (uint64_t)h->ts.tv_sec * NS_PER_S + h->ts.tv_usec;
389 [ + - - + ]: 2642 : if (ns < ctx->start_ns || ns > ctx->end_ns) {
390 : : char tstart[128], tend[128];
391 : :
392 : 0 : fmt_time(tstart, sizeof(tstart), ctx->start_ns);
393 : 0 : fmt_time(tend, sizeof(tend), ctx->end_ns);
394 : :
395 : : printf("Timestamp out of range [%s .. %s]\n",
396 : : tstart, tend);
397 : 0 : goto error;
398 : : }
399 : :
400 [ - + ]: 2642 : if (!rte_is_broadcast_ether_addr(&eh->dst_addr)) {
401 : : printf("Destination is not broadcast\n");
402 : 0 : goto error;
403 : : }
404 : :
405 [ - + ]: 2642 : if (rte_ipv4_cksum(ip) != 0) {
406 : : printf("Bad IPv4 checksum\n");
407 : 0 : goto error;
408 : : }
409 : :
410 : : return; /* packet is normal */
411 : :
412 : 0 : error:
413 : 0 : print_packet(ns, eh, h->len);
414 : :
415 : : /* Stop parsing at tenth error */
416 [ # # ]: 0 : if (++total_errors >= 10)
417 : 0 : pcap_breakloop(ctx->pcap);
418 : : }
419 : :
420 : : #ifndef RTE_EXEC_ENV_WINDOWS
421 : : static uint64_t
422 : : current_timestamp(void)
423 : : {
424 : : struct timespec ts;
425 : :
426 : 5 : clock_gettime(CLOCK_REALTIME, &ts);
427 : : return rte_timespec_to_ns(&ts);
428 : : }
429 : : #endif
430 : :
431 : : /*
432 : : * Open the resulting pcapng file with libpcap
433 : : * Would be better to use capinfos from wireshark
434 : : * but that creates an unwanted dependency.
435 : : */
436 : : static int
437 : 3 : valid_pcapng_file(const char *file_name, uint64_t started, unsigned int expected)
438 : : {
439 : : char errbuf[PCAP_ERRBUF_SIZE];
440 : 3 : struct pkt_print_ctx ctx = { };
441 : : int ret;
442 : :
443 : 3 : ctx.start_ns = started;
444 : 3 : ctx.end_ns = current_timestamp() + NS_PER_S;
445 : :
446 : 3 : ctx.pcap = pcap_open_offline_with_tstamp_precision(file_name,
447 : : PCAP_TSTAMP_PRECISION_NANO,
448 : : errbuf);
449 [ - + ]: 3 : if (ctx.pcap == NULL) {
450 : : printf("pcap_open_offline('%s') failed: %s\n",
451 : : file_name, errbuf);
452 : 0 : return -1;
453 : : }
454 : :
455 : 3 : ret = pcap_loop(ctx.pcap, 0, parse_pcap_packet, (u_char *)&ctx);
456 [ - + ]: 3 : if (ret != 0) {
457 : 0 : printf("pcap_dispatch: failed: %s\n",
458 : : pcap_geterr(ctx.pcap));
459 [ - + ]: 3 : } else if (ctx.count != expected) {
460 : : printf("Only %u packets, expected %u\n",
461 : : ctx.count, expected);
462 : : ret = -1;
463 : : }
464 : :
465 : 3 : pcap_close(ctx.pcap);
466 : :
467 : 3 : return ret;
468 : : }
469 : :
470 : : /* Record the timestamp of the first packet in the file. */
471 : : static void
472 : 4 : first_timestamp_cb(u_char *user, const struct pcap_pkthdr *h,
473 : : const u_char *bytes __rte_unused)
474 : : {
475 : : uint64_t *ts_ns = (uint64_t *)user;
476 : :
477 : : /* File has nanosecond precision, so tv_usec holds ns. */
478 [ + - ]: 4 : if (*ts_ns == 0)
479 : 4 : *ts_ns = (uint64_t)h->ts.tv_sec * NS_PER_S + h->ts.tv_usec;
480 : 4 : }
481 : :
482 : : static int
483 : 4 : read_one_timestamp(const char *file_name, uint64_t *ts_ns)
484 : : {
485 : : char errbuf[PCAP_ERRBUF_SIZE];
486 : : pcap_t *pcap;
487 : : int ret;
488 : :
489 : 4 : *ts_ns = 0;
490 : 4 : pcap = pcap_open_offline_with_tstamp_precision(file_name,
491 : : PCAP_TSTAMP_PRECISION_NANO,
492 : : errbuf);
493 [ - + ]: 4 : if (pcap == NULL) {
494 : : printf("pcap_open_offline('%s') failed: %s\n",
495 : : file_name, errbuf);
496 : 0 : return -1;
497 : : }
498 : :
499 : 4 : ret = pcap_loop(pcap, 0, first_timestamp_cb, (u_char *)ts_ns);
500 [ - + ]: 4 : if (ret != 0)
501 : 0 : printf("pcap_loop: failed: %s\n", pcap_geterr(pcap));
502 : 4 : pcap_close(pcap);
503 : :
504 [ + - - + ]: 4 : if (ret == 0 && *ts_ns == 0) {
505 : : printf("no packet found in %s\n", file_name);
506 : 0 : return -1;
507 : : }
508 : : return ret;
509 : : }
510 : :
511 : : static int
512 : 1 : test_add_interface(void)
513 : : {
514 : 1 : char file_name[PATH_MAX] = "/tmp/pcapng_test_XXXXXX.pcapng";
515 : : static rte_pcapng_t *pcapng;
516 : : int ret, tmp_fd;
517 : : uint64_t now = current_timestamp();
518 : :
519 : 1 : tmp_fd = mkstemps(file_name, strlen(".pcapng"));
520 [ - + ]: 1 : if (tmp_fd == -1) {
521 : 0 : perror("mkstemps() failure");
522 : 0 : goto fail;
523 : : }
524 : : printf("pcapng: output file %s\n", file_name);
525 : :
526 : : /* open a test capture file */
527 : 1 : pcapng = rte_pcapng_fdopen(tmp_fd, NULL, NULL, "pcapng_addif", NULL);
528 [ - + ]: 1 : if (pcapng == NULL) {
529 : : printf("rte_pcapng_fdopen failed\n");
530 : 0 : close(tmp_fd);
531 : 0 : goto fail;
532 : : }
533 : :
534 : : /* Add interface to the file */
535 : 1 : ret = rte_pcapng_add_interface(pcapng, port_id, DLT_EN10MB,
536 : : NULL, NULL, NULL);
537 [ - + ]: 1 : if (ret < 0) {
538 : 0 : printf("can not add port %u\n", port_id);
539 : 0 : goto fail;
540 : : }
541 : :
542 : : /* Add interface with ifname and ifdescr */
543 : 1 : ret = rte_pcapng_add_interface(pcapng, port_id, DLT_EN10MB,
544 : : "myeth", "Some long description", NULL);
545 [ - + ]: 1 : if (ret < 0) {
546 : 0 : printf("can not add port %u with ifname\n", port_id);
547 : 0 : goto fail;
548 : : }
549 : :
550 : : /* Add interface with filter */
551 : 1 : ret = rte_pcapng_add_interface(pcapng, port_id, DLT_EN10MB,
552 : : NULL, NULL, "tcp port 8080");
553 [ - + ]: 1 : if (ret < 0) {
554 : 0 : printf("can not add port %u with filter\n", port_id);
555 : 0 : goto fail;
556 : : }
557 : :
558 : 1 : rte_pcapng_close(pcapng);
559 : :
560 : 1 : ret = valid_pcapng_file(file_name, now, 0);
561 : : /* if test fails want to investigate the file */
562 [ + - ]: 1 : if (ret == 0)
563 : 1 : remove(file_name);
564 : :
565 : : return ret;
566 : :
567 : 0 : fail:
568 : 0 : rte_pcapng_close(pcapng);
569 : 0 : return -1;
570 : : }
571 : :
572 : : static int
573 : 1 : test_write_packets(void)
574 : : {
575 : 1 : char file_name[PATH_MAX] = "/tmp/pcapng_test_XXXXXX.pcapng";
576 : : rte_pcapng_t *pcapng = NULL;
577 : : int ret, tmp_fd, count;
578 : : uint64_t now = current_timestamp();
579 : :
580 : 1 : tmp_fd = mkstemps(file_name, strlen(".pcapng"));
581 [ - + ]: 1 : if (tmp_fd == -1) {
582 : 0 : perror("mkstemps() failure");
583 : 0 : goto fail;
584 : : }
585 : : printf("pcapng: output file %s\n", file_name);
586 : :
587 : : /* open a test capture file */
588 : 1 : pcapng = rte_pcapng_fdopen(tmp_fd, NULL, NULL, "pcapng_test", NULL);
589 [ - + ]: 1 : if (pcapng == NULL) {
590 : : printf("rte_pcapng_fdopen failed\n");
591 : 0 : close(tmp_fd);
592 : 0 : goto fail;
593 : : }
594 : :
595 : : /* Add interface to the file */
596 : 1 : ret = rte_pcapng_add_interface(pcapng, port_id, DLT_EN10MB,
597 : : NULL, NULL, NULL);
598 [ - + ]: 1 : if (ret < 0) {
599 : 0 : printf("can not add port %u\n", port_id);
600 : 0 : goto fail;
601 : : }
602 : :
603 : : /* write a statistics block */
604 : 1 : ret = rte_pcapng_write_stats(pcapng, port_id, 0, 0, NULL);
605 [ - + ]: 1 : if (ret <= 0) {
606 : : printf("Write of statistics failed\n");
607 : 0 : goto fail;
608 : : }
609 : :
610 : 1 : count = fill_pcapng_file(pcapng);
611 [ - + ]: 1 : if (count < 0)
612 : 0 : goto fail;
613 : :
614 : : /* write a statistics block */
615 : 1 : ret = rte_pcapng_write_stats(pcapng, port_id,
616 : : count, 0, "end of test");
617 [ - + ]: 1 : if (ret <= 0) {
618 : : printf("Write of statistics failed\n");
619 : 0 : goto fail;
620 : : }
621 : :
622 : 1 : rte_pcapng_close(pcapng);
623 : :
624 : 1 : ret = valid_pcapng_file(file_name, now, count);
625 : : /* if test fails want to investigate the file */
626 [ + - ]: 1 : if (ret == 0)
627 : 1 : remove(file_name);
628 : :
629 : : return ret;
630 : :
631 : 0 : fail:
632 : 0 : rte_pcapng_close(pcapng);
633 : 0 : return -1;
634 : : }
635 : :
636 : : static int
637 : 1 : test_write_before_open(void)
638 : : {
639 : 1 : char file_name[PATH_MAX] = "/tmp/pcapng_test_XXXXXX.pcapng";
640 : : struct dummy_mbuf mbfs;
641 : : struct rte_mbuf *clones[MAX_BURST];
642 : : rte_pcapng_t *pcapng = NULL;
643 : : int ret, tmp_fd, i;
644 : : unsigned int count = 8;
645 : : uint64_t now;
646 : : ssize_t len;
647 : :
648 : 1 : mbuf1_prepare(&mbfs);
649 : 1 : mbuf1_resize(&mbfs, rte_rand_max(MAX_DATA_SIZE));
650 : :
651 : : /* Copy packets BEFORE opening the pcapng file.
652 : : * This exercises the negative TSC delta path.
653 : : */
654 [ + + ]: 9 : for (i = 0; i < (int)count; i++) {
655 : 8 : clones[i] = rte_pcapng_copy(port_id, 0, &mbfs.mb[0], mp,
656 : : rte_pktmbuf_pkt_len(&mbfs.mb[0]),
657 : : RTE_PCAPNG_DIRECTION_IN, NULL);
658 [ - + ]: 8 : if (clones[i] == NULL) {
659 : 0 : fprintf(stderr, "Cannot copy packet before open\n");
660 : 0 : rte_pktmbuf_free_bulk(clones, i);
661 : 0 : return -1;
662 : : }
663 : : }
664 : :
665 : : /* Small delay so fdopen's tsc_base is measurably after the copies */
666 : 1 : rte_delay_us_block(100);
667 : :
668 : : now = current_timestamp();
669 : :
670 : 1 : tmp_fd = mkstemps(file_name, strlen(".pcapng"));
671 [ - + ]: 1 : if (tmp_fd == -1) {
672 : 0 : perror("mkstemps() failure");
673 : 0 : rte_pktmbuf_free_bulk(clones, count);
674 : 0 : return -1;
675 : : }
676 : :
677 : 1 : pcapng = rte_pcapng_fdopen(tmp_fd, NULL, NULL, "pcapng_preopen", NULL);
678 [ - + ]: 1 : if (pcapng == NULL) {
679 : 0 : fprintf(stderr, "rte_pcapng_fdopen failed\n");
680 : 0 : close(tmp_fd);
681 : 0 : rte_pktmbuf_free_bulk(clones, count);
682 : 0 : return -1;
683 : : }
684 : :
685 : 1 : ret = rte_pcapng_add_interface(pcapng, port_id, DLT_EN10MB,
686 : : NULL, NULL, NULL);
687 [ - + ]: 1 : if (ret < 0) {
688 : 0 : fprintf(stderr, "can not add port %u\n", port_id);
689 : 0 : goto fail;
690 : : }
691 : :
692 : : /* Write the pre-captured packets — timestamps precede tsc_base */
693 : 1 : len = rte_pcapng_write_packets(pcapng, clones, count);
694 : 1 : rte_pktmbuf_free_bulk(clones, count);
695 [ - + ]: 1 : if (len <= 0) {
696 : 0 : fprintf(stderr, "Write of pre-open packets failed: %s\n",
697 : : rte_strerror(rte_errno));
698 : 0 : goto fail;
699 : : }
700 : :
701 : 1 : rte_pcapng_close(pcapng);
702 : :
703 : : /* Validate the file is parseable — timestamps should be
704 : : * slightly before 'now' but still reasonable.
705 : : */
706 : 1 : ret = valid_pcapng_file(file_name, now - NS_PER_S, count);
707 [ + - ]: 1 : if (ret == 0)
708 : 1 : remove(file_name);
709 : :
710 : : return ret;
711 : :
712 : 0 : fail:
713 : 0 : rte_pcapng_close(pcapng);
714 : 0 : return -1;
715 : : }
716 : :
717 : : static void
718 : 1 : test_cleanup(void)
719 : : {
720 : 1 : rte_mempool_free(mp);
721 : 1 : rte_vdev_uninit(null_dev);
722 : 1 : }
723 : :
724 : : /*
725 : : * Converting a cycle count to nanoseconds overflows past
726 : : * UINT64_MAX / NS_PER_S cycles, which is only a few seconds of real
727 : : * capture. Forge the timestamp in the block header to reach large
728 : : * deltas without waiting.
729 : : */
730 : : static int
731 : 1 : test_long_timestamp(void)
732 : : {
733 : : /* seconds into the future */
734 : : static const unsigned int offsets[] = { 1, 8, 3600, 10 * 86400 };
735 : : struct pcapng_test_hdr {
736 : : uint32_t block_type;
737 : : uint32_t block_length;
738 : : uint32_t interface_id;
739 : : uint32_t timestamp_hi;
740 : : uint32_t timestamp_lo;
741 : : } *epb;
742 : : struct dummy_mbuf mbfs;
743 : 1 : uint64_t hz = rte_get_tsc_hz();
744 : : unsigned int i;
745 : :
746 [ - + ]: 1 : TEST_ASSERT(hz != 0, "TSC frequency is zero");
747 : :
748 : 1 : mbuf1_prepare(&mbfs);
749 : 1 : mbuf1_resize(&mbfs, 512);
750 : :
751 [ + + ]: 5 : for (i = 0; i < RTE_DIM(offsets); i++) {
752 : 4 : char file_name[PATH_MAX] = "/tmp/pcapng_test_XXXXXX.pcapng";
753 : : uint64_t cycles, base_ns, want_ns, got_ns, diff;
754 : : struct rte_mbuf *mc;
755 : : rte_pcapng_t *pcapng;
756 : : int ret, tmp_fd;
757 : : ssize_t len;
758 : :
759 : 4 : mc = rte_pcapng_copy(port_id, 0, &mbfs.mb[0], mp,
760 : : rte_pktmbuf_pkt_len(&mbfs.mb[0]),
761 : : RTE_PCAPNG_DIRECTION_IN, NULL);
762 [ - + ]: 4 : TEST_ASSERT(mc != NULL, "rte_pcapng_copy failed");
763 : :
764 : 4 : tmp_fd = mkstemps(file_name, strlen(".pcapng"));
765 [ - + ]: 4 : if (tmp_fd == -1) {
766 : 0 : rte_pktmbuf_free(mc);
767 : 0 : TEST_ASSERT(false, "mkstemps() failed");
768 : : }
769 : :
770 : : base_ns = current_timestamp();
771 : 4 : pcapng = rte_pcapng_fdopen(tmp_fd, NULL, NULL, "longts", NULL);
772 [ - + ]: 4 : if (pcapng == NULL) {
773 : 0 : close(tmp_fd);
774 : 0 : rte_pktmbuf_free(mc);
775 : 0 : TEST_ASSERT(false, "rte_pcapng_fdopen failed");
776 : : }
777 : :
778 : 4 : ret = rte_pcapng_add_interface(pcapng, port_id, DLT_EN10MB,
779 : : NULL, NULL, NULL);
780 [ - + ]: 4 : if (ret < 0) {
781 : 0 : rte_pcapng_close(pcapng);
782 : 0 : rte_pktmbuf_free(mc);
783 : 0 : TEST_ASSERT(false, "can not add port %u", port_id);
784 : : }
785 : :
786 : : /* Conversion happens on write, so move the capture time. */
787 : 4 : epb = rte_pktmbuf_mtod(mc, struct pcapng_test_hdr *);
788 : 4 : cycles = (uint64_t)epb->timestamp_hi << 32;
789 : 4 : cycles += epb->timestamp_lo;
790 : 4 : cycles += (uint64_t)offsets[i] * hz;
791 : 4 : epb->timestamp_hi = cycles >> 32;
792 : 4 : epb->timestamp_lo = (uint32_t)cycles;
793 : :
794 : 4 : len = rte_pcapng_write_packets(pcapng, &mc, 1);
795 : 4 : rte_pktmbuf_free(mc);
796 : 4 : rte_pcapng_close(pcapng);
797 [ - + ]: 4 : TEST_ASSERT(len > 0, "write failed at +%u s", offsets[i]);
798 : :
799 : 4 : ret = read_one_timestamp(file_name, &got_ns);
800 [ - + ]: 4 : TEST_ASSERT(ret == 0, "can not read back +%u s", offsets[i]);
801 : :
802 : : /* An overflow wraps and misses by the whole offset. */
803 : 4 : want_ns = base_ns + (uint64_t)offsets[i] * NS_PER_S;
804 [ - + ]: 4 : diff = (got_ns > want_ns) ? got_ns - want_ns : want_ns - got_ns;
805 [ - + ]: 4 : if (diff > 2 * NS_PER_S)
806 : : printf("at +%u s: got %"PRIu64" want %"PRIu64"\n",
807 : : offsets[i], got_ns, want_ns);
808 : : else
809 : 4 : remove(file_name);
810 : :
811 [ - + ]: 4 : TEST_ASSERT(diff <= 2 * NS_PER_S,
812 : : "timestamp off by %"PRIu64" ns at +%u s",
813 : : diff, offsets[i]);
814 : : }
815 : :
816 : : return 0;
817 : : }
818 : :
819 : : static struct
820 : : unit_test_suite test_pcapng_suite = {
821 : : .setup = test_setup,
822 : : .teardown = test_cleanup,
823 : : .suite_name = "Test Pcapng Unit Test Suite",
824 : : .unit_test_cases = {
825 : : TEST_CASE(test_add_interface),
826 : : TEST_CASE(test_write_packets),
827 : : TEST_CASE(test_write_before_open),
828 : : TEST_CASE(test_long_timestamp),
829 : : TEST_CASES_END()
830 : : }
831 : : };
832 : :
833 : : static int
834 : 1 : test_pcapng(void)
835 : : {
836 : 1 : return unit_test_suite_runner(&test_pcapng_suite);
837 : : }
838 : :
839 : 305 : REGISTER_FAST_TEST(pcapng_autotest, NOHUGE_OK, ASAN_OK, test_pcapng);
|