From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E85FA606A322 for ; Wed, 2 Dec 2026 17:51:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=networkplumber-org.20260707.gappssmtp.com header.i=@networkplumber-org.20260707.gappssmtp.com header.a=rsa-sha256 header.s=20260707 header.b=rX7gMwBK Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id DC7C4406FF; Wed, 2 Dec 2026 18:50:39 +0100 (CET) Received: from mail-pg1-f180.google.com (mail-pg1-f180.google.com [209.85.215.180]) by mails.dpdk.org (Postfix) with ESMTPS id 0290D40685 for ; Wed, 2 Dec 2026 18:50:37 +0100 (CET) Received: by mail-pg1-f180.google.com with SMTP id 41be03b00d2f7-cd6acc50b8dso6804973a12.3 for ; Wed, 02 Dec 2026 09:50:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=networkplumber-org.20260707.gappssmtp.com; s=20260707; t=1796233835; x=1796838635; 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:content-type; bh=T/fEUsCIXt6Zps4MEVu/h5HYi6M8pBiwhHRDvT8U3EI=; b=rX7gMwBKPulKj+f/5L4D6mmkt/6JAKZFP/WO8aubsJRHwwFLX8C3PzGNUjDuof6Sic KCaPqDDXjaaXABWFmV13/9KCPjqFzFTAM6V6oP/mZDrlk4d/E6Y7QZdRqWSjuoRghKVv wqxvdvmP9BJhIL4dBGW8CS1qA3TEM2a9SizmJFaZph6fDwgwW+VEaN8DwtK9Z4V28hsR debbpE0qn1LnW+cM79xB3zqLd3mT/E51kuWIt+SKFVlVGJTs8GU7TYwSHP+55L2VJukr 8MeRGDn4Z7DYOjBueLTuASvCv8LvE4Ldw/m7qlwBEMyV9g4JBnzXIF0MxMmm1EoR0Iem f3hQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796233835; x=1796838635; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=T/fEUsCIXt6Zps4MEVu/h5HYi6M8pBiwhHRDvT8U3EI=; b=JUo/aLO+o9Gjh7pIP4dO5JDfu1OujUJpYSvRZtMxahas8Z35h8f27aQYjMkorJkeuE FkQ3GHQfZmnhMiGKho4v2+nrEHUh+kasfbzhW1lCEcaoe65H22a6gw7yZnv5ZUCglEao kQBxq4FcBm2kRlMO14Ri8Kh63H2WGqQtzqTr+2gN000mMN39Whg2ADYSlDDfn8qiqCNi gcazhEv6XeZQ3C65bBZwA3JEncNuzWLVfE5uD1p3kgQdtFGY2PYw/MNfKlitn5vkKEys DUE/JVWSZURm0w5ILF97wjMOO5rQRK9pgJih6C0oRYf+FGQQoQSjrLx3+8q6ojzzXtIa XxVw== X-Gm-Message-State: APuU4Z9E80D0v8YBntkKwIRaKspyBcrkhphTpTv7iCCuQ5OViBSDomvG 98cPhQhk++E5ibVr7ZkZxZ13v+ohVF9bVFkyncdj7yNqQsnjh/PpHHcBbmFTgtC2Xu4pfIWi4l4 YyUGvgcI= X-Gm-Gg: AZFdTZd06zE0zyWMY14eK6A5lzlsLWWyLC4fV2zmWtvGCEnaxWmVZ3uHF3JxnRRqER1 vD6dUasI840VdGbAvga57SFpcHb/KfypsCXL9CdsUVoGrk+Z5hJyIsGRCZVqv9KsjndjiVIAqgV OYqy167m5oqI8OfQou+u3zTz5ZN1USC+Xm0lIna7usHLMo5aDs5LZxJf8oOg85cbuQKwhkLe986 H9yldkTJ2o2ka8/LCciKlE+5MEWzoyIlSEwGzLBHQk0fBMjy0Tcw56LWR9/f+8a4KNYG2RJiI97 R1DiB2HyTzioxyvlCU7cru30JIrtLpDtrNkXwLWDmvM8mopDtWwC7JMMY/NuCgoPoVIGeuOgC/i fXifqYvvjxbacg+rTkk3gyFjsy3qLA4vSfMLZotTJllR9Q0dwqGRbbAs+eAkLXoYt2SL05yZHiV 6vO4zdBXVACKoo5AIofK4Oq34lgHawxbTtOFuu8Y5ZsuXU/QKPlJNSX8RjoPYN115BryAUVOuIF luVla6ZbI7g6gp4L2eJHDnbRoNMDyf3K0b1krOkZlrDvQ/8n5/xKh4IR5js1xgl3foiYgZhCsYu RRFJ4PEHI1jW8UQKV7x/AEYPa5iHRe4auJlQat7s9UlHwsclhtchh1oPNS6cF92pZR8y X-Received: by 2002:a17:90b:5287:b0:3ab:9c73:12e with SMTP id 98e67ed59e1d1-3b41c7c22c0mr42009321a91.6.1796233835273; Wed, 02 Dec 2026 09:50:35 -0800 (PST) Received: from phoenix.lan (204-195-112-43.wavecable.com. [204.195.112.43]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3b5c784e74esm211314a91.6.2026.12.02.09.50.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Dec 2026 09:50:34 -0800 (PST) From: Stephen Hemminger To: dev@dpdk.org Cc: Stephen Hemminger Subject: [PATCH v2 7/7] pcapng: use stdio for output Date: Wed, 2 Dec 2026 09:49:45 -0800 Message-ID: <20261202175028.4071813-8-stephen@networkplumber.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261202175028.4071813-1-stephen@networkplumber.org> References: <20261201224258.1962108-1-stephen@networkplumber.org> <20261202175028.4071813-1-stephen@networkplumber.org> X-Archived-By: 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-Archive: List-Post: List-Help: List-Subscribe: , X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 The pcapng library was using writev to write directly to the file. This optimizes the number of system calls but makes it hard to support other output methods such as compression. Add rte_pcapng_fopen() to open by stdio stream, and reimplement rte_pcapng_fdopen() on top of it. The stream created by rte_pcapng_fdopen() is fully buffered and flushed at the end of each write call, so data is in the file when the call returns as it was before. With rte_pcapng_fopen() the application owns the stream and can call fflush() when needed, for example for live readers like Wireshark. Writing packets with rte_pcapng_write_packets now becomes fully multi-thread safe. Windows has the stdio locking functions under different names, and deprecates the POSIX spelling of dup() and fdopen(), so add shims for those. Signed-off-by: Stephen Hemminger --- app/test/test_pcapng.c | 112 ++++++++++++- doc/guides/rel_notes/release_27_03.rst | 6 + lib/eal/windows/include/rte_os_shim.h | 7 + lib/pcapng/rte_pcapng.c | 213 ++++++++++++++----------- lib/pcapng/rte_pcapng.h | 50 +++++- 5 files changed, 288 insertions(+), 100 deletions(-) diff --git a/app/test/test_pcapng.c b/app/test/test_pcapng.c index 0cf5d469e5..b39c5636f1 100644 --- a/app/test/test_pcapng.c +++ b/app/test/test_pcapng.c @@ -2,7 +2,9 @@ * Copyright (c) 2021 Microsoft Corporation */ +#include #include +#include #include #include #include @@ -551,8 +553,39 @@ test_add_interface(void) return -1; } +/* Open capture file with either rte_pcapng_fdopen() or rte_pcapng_fopen() */ +static rte_pcapng_t * +open_pcapng(int tmp_fd, bool use_stdio, const char *appname) +{ + rte_pcapng_t *pcapng; + FILE *f; + + if (!use_stdio) { + pcapng = rte_pcapng_fdopen(tmp_fd, NULL, NULL, appname, NULL); + if (pcapng == NULL) { + printf("rte_pcapng_fdopen failed: %s\n", rte_strerror(rte_errno)); + close(tmp_fd); + } + return pcapng; + } + + f = fdopen(tmp_fd, "wb"); + if (f == NULL) { + perror("fdopen() failure"); + close(tmp_fd); + return NULL; + } + + pcapng = rte_pcapng_fopen(f, NULL, NULL, appname, NULL); + if (pcapng == NULL) { + printf("rte_pcapng_fopen failed: %s\n", rte_strerror(rte_errno)); + fclose(f); + } + return pcapng; +} + static int -test_write_packets(void) +write_packets(bool use_stdio) { char file_name[PATH_MAX] = "/tmp/pcapng_test_XXXXXX.pcapng"; rte_pcapng_t *pcapng = NULL; @@ -567,12 +600,9 @@ test_write_packets(void) 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) { - printf("rte_pcapng_fdopen failed\n"); - close(tmp_fd); + pcapng = open_pcapng(tmp_fd, use_stdio, "pcapng_test"); + if (pcapng == NULL) goto fail; - } /* Add interface to the file */ ret = rte_pcapng_add_interface(pcapng, port_id, DLT_EN10MB, @@ -615,6 +645,74 @@ test_write_packets(void) return -1; } +static int +test_write_packets(void) +{ + return write_packets(false); +} + +static int +test_write_packets_fopen(void) +{ + return write_packets(true); +} + +/* + * When open fails the caller still owns the file descriptor + * or stdio handle, it must not be closed by the library. + */ +static int +test_open_fail(void) +{ + char file_name[PATH_MAX] = "/tmp/pcapng_test_XXXXXX.pcapng"; + const size_t len = UINT16_MAX + 1; + rte_pcapng_t *pcapng; + char *comment; + FILE *f; + int tmp_fd; + + pcapng = rte_pcapng_fopen(NULL, NULL, NULL, "pcapng_fail", NULL); + TEST_ASSERT_NULL(pcapng, "rte_pcapng_fopen(NULL) should fail"); + TEST_ASSERT_EQUAL(rte_errno, EINVAL, "expected EINVAL got %d", rte_errno); + + /* comment longer than pcapng option allows */ + comment = malloc(len + 1); + TEST_ASSERT_NOT_NULL(comment, "malloc failed"); + memset(comment, 'x', len); + comment[len] = '\0'; + + tmp_fd = mkstemps(file_name, strlen(".pcapng")); + if (tmp_fd == -1) { + free(comment); + TEST_ASSERT(false, "mkstemps() failed"); + } + remove(file_name); + + pcapng = rte_pcapng_fdopen(tmp_fd, NULL, NULL, "pcapng_fail", comment); + if (pcapng != NULL) { + rte_pcapng_close(pcapng); + free(comment); + TEST_ASSERT(false, "rte_pcapng_fdopen with long comment should fail"); + } + + f = fdopen(tmp_fd, "wb"); + if (f == NULL) { + close(tmp_fd); + free(comment); + TEST_ASSERT(false, "fd closed by failed rte_pcapng_fdopen"); + } + + pcapng = rte_pcapng_fopen(f, NULL, NULL, "pcapng_fail", comment); + free(comment); + if (pcapng != NULL) { + rte_pcapng_close(pcapng); + TEST_ASSERT(false, "rte_pcapng_fopen with long comment should fail"); + } + + TEST_ASSERT_SUCCESS(fclose(f), "FILE closed by failed rte_pcapng_fopen"); + return TEST_SUCCESS; +} + static int test_write_before_open(void) { @@ -798,6 +896,8 @@ unit_test_suite test_pcapng_suite = { .unit_test_cases = { TEST_CASE(test_add_interface), TEST_CASE(test_write_packets), + TEST_CASE(test_write_packets_fopen), + TEST_CASE(test_open_fail), TEST_CASE(test_write_before_open), TEST_CASE(test_pcapng_timestamp), TEST_CASES_END() diff --git a/doc/guides/rel_notes/release_27_03.rst b/doc/guides/rel_notes/release_27_03.rst index 726c5b1e3f..479d7bd7f9 100644 --- a/doc/guides/rel_notes/release_27_03.rst +++ b/doc/guides/rel_notes/release_27_03.rst @@ -68,6 +68,12 @@ New Features for example a hardware Rx timestamp. ``rte_pcapng_copy()`` is unchanged and still uses the TSC at copy time. +* **Added pcapng API to capture to a stdio stream.** + + Added the experimental ``rte_pcapng_fopen()`` function, + which takes an already open ``FILE *`` instead of a file descriptor. + This allows capturing to a pipe or a compressed stream. + Removed Items ------------- diff --git a/lib/eal/windows/include/rte_os_shim.h b/lib/eal/windows/include/rte_os_shim.h index 44664a5062..edef8eb283 100644 --- a/lib/eal/windows/include/rte_os_shim.h +++ b/lib/eal/windows/include/rte_os_shim.h @@ -34,6 +34,13 @@ #define fileno(f) _fileno(f) #define isatty(fd) _isatty(fd) #define access(path, mode) _access(path, mode) +#define dup(fd) _dup(fd) +#define fdopen(fd, mode) _fdopen(fd, mode) + +/* Windows spells the stdio locking functions with a leading underscore. */ +#define flockfile(f) _lock_file(f) +#define funlockfile(f) _unlock_file(f) +#define fwrite_unlocked(ptr, size, n, f) _fwrite_nolock(ptr, size, n, f) #define IPVERSION 4 diff --git a/lib/pcapng/rte_pcapng.c b/lib/pcapng/rte_pcapng.c index d6f50c2fc0..271b24cd41 100644 --- a/lib/pcapng/rte_pcapng.c +++ b/lib/pcapng/rte_pcapng.c @@ -12,7 +12,6 @@ #ifndef RTE_EXEC_ENV_WINDOWS #include -#include #endif #include @@ -33,12 +32,17 @@ /* conversion from DPDK speed to PCAPNG */ #define PCAPNG_MBPS_SPEED 1000000ull +/* stream buffer size used by rte_pcapng_fdopen() */ +#define PCAPNG_BUF_SIZE (64 * 1024) + /* upper bound for strings in pcapng option data */ #define PCAPNG_STR_MAX UINT16_MAX /* Format of the capture file handle */ struct rte_pcapng { - int outfd; /* output file */ + FILE *outf; /* output file */ + int fd; /* from rte_pcapng_fdopen(), else -1 */ + bool autoflush; /* flush the stream after each write */ unsigned int ports; /* number of interfaces added */ uint64_t start_time; /* ns since 1/1/1970 when opened */ @@ -47,50 +51,6 @@ struct rte_pcapng { }; #ifdef RTE_EXEC_ENV_WINDOWS -/* - * Windows does not have writev() call. - * Emulate this by copying to a new buffer. - * The copy is necessary since pcapng needs to be thread-safe - * and do atomic write operations. - */ - -#define IOV_MAX 128 -struct iovec { - void *iov_base; - size_t iov_len; -}; - -static ssize_t writev(int fd, const struct iovec *iov, int iovcnt) -{ - size_t bytes = 0; - uint8_t *ptr; - void *tmp_buf; - ssize_t ret; - int i; - - for (i = 0; i < iovcnt; i++) - bytes += iov[i].iov_len; - - if (unlikely(bytes == 0)) - return 0; - - tmp_buf = malloc(bytes); - if (unlikely(tmp_buf == NULL)) { - errno = ENOMEM; - return -1; - } - - ptr = tmp_buf; - for (i = 0; i < iovcnt; i++) { - rte_memcpy(ptr, iov[i].iov_base, iov[i].iov_len); - ptr += iov[i].iov_len; - } - - ret = write(fd, tmp_buf, bytes); - free(tmp_buf); - return ret; -} - #define IF_NAMESIZE 16 /* compatibility wrapper because name is optional */ #define if_indextoname(ifindex, ifname) NULL @@ -116,6 +76,22 @@ pcapng_add_option(struct pcapng_option *popt, uint16_t code, return (struct pcapng_option *)((uint8_t *)popt + pcapng_optlen(len)); } +/* Write a whole block to the file. */ +static int +pcapng_write_block(rte_pcapng_t *self, const void *buf, size_t len) +{ + if (fwrite(buf, len, 1, self->outf) != 1) { + rte_errno = errno; + return -1; + } + + if (self->autoflush && fflush(self->outf) != 0) { + rte_errno = errno; + return -1; + } + return 0; +} + /* * Write required initial section header describing the capture */ @@ -145,8 +121,10 @@ pcapng_section_block(rte_pcapng_t *self, len += sizeof(uint32_t); buf = malloc(len); - if (buf == NULL) - return -ENOMEM; + if (buf == NULL) { + rte_errno = ENOMEM; + return -1; + } hdr = (struct pcapng_section_header *)buf; *hdr = (struct pcapng_section_header) { @@ -179,9 +157,9 @@ pcapng_section_block(rte_pcapng_t *self, /* clone block_length after option */ memcpy(opt, &hdr->block_length, sizeof(uint32_t)); - ret = write(self->outfd, buf, len); + ret = pcapng_write_block(self, buf, len); free(buf); - return ret < 0 ? -errno : 0; + return ret; } /* Write an interface block for a DPDK port */ @@ -306,14 +284,15 @@ rte_pcapng_add_interface(rte_pcapng_t *self, uint16_t port, uint16_t link_type, /* clone block_length after options */ memcpy(opt, &hdr->block_length, sizeof(uint32_t)); - ret = write(self->outfd, buf, len); + ret = pcapng_write_block(self, buf, len); free(buf); + if (ret < 0) + return -1; /* remember the file index only after successful write */ - if (ret > 0) - self->port_index[port] = self->ports++; + self->port_index[port] = self->ports++; - return ret; + return len; } /* @@ -387,9 +366,9 @@ rte_pcapng_write_stats(rte_pcapng_t *self, uint16_t port_id, /* clone block_length after option */ memcpy(opt, &len, sizeof(uint32_t)); - ret = write(self->outfd, buf, len); + ret = pcapng_write_block(self, buf, len); free(buf); - return ret; + return ret < 0 ? -1 : (ssize_t)len; } RTE_EXPORT_SYMBOL(rte_pcapng_mbuf_size) @@ -659,11 +638,12 @@ ssize_t rte_pcapng_write_packets(rte_pcapng_t *self, struct rte_mbuf *pkts[], uint16_t nb_pkts) { - struct iovec iov[IOV_MAX]; - unsigned int i, cnt = 0; - ssize_t ret, total = 0; + FILE *outf = self->outf; + ssize_t total = 0; - for (i = 0; i < nb_pkts; i++) { + flockfile(outf); + + for (unsigned int i = 0; i < nb_pkts; i++) { struct rte_mbuf *m = pkts[i]; struct pcapng_enhance_packet_block *epb; @@ -672,14 +652,14 @@ rte_pcapng_write_packets(rte_pcapng_t *self, if (unlikely(epb->block_type != PCAPNG_ENHANCED_PACKET_BLOCK || epb->block_length != rte_pktmbuf_pkt_len(m))) { rte_errno = EINVAL; - return -1; + goto fail; } /* check that this interface was added. */ epb->interface_id = self->port_index[m->port]; if (unlikely(epb->interface_id > RTE_MAX_ETHPORTS)) { rte_errno = EINVAL; - return -1; + goto fail; } /* @@ -687,45 +667,46 @@ rte_pcapng_write_packets(rte_pcapng_t *self, * Map that to PCAPNG interface in file. */ do { - iov[cnt].iov_base = rte_pktmbuf_mtod(m, void *); - iov[cnt].iov_len = rte_pktmbuf_data_len(m); - - /* If current iov is maxed out then send it */ - if (++cnt == IOV_MAX) { - ret = writev(self->outfd, iov, cnt); - if (unlikely(ret < 0)) { - rte_errno = errno; - return -1; - } - cnt = 0; - total += ret; + size_t len = rte_pktmbuf_data_len(m); + + if (unlikely(fwrite_unlocked(rte_pktmbuf_mtod(m, void *), + 1, len, outf) != len)) { + rte_errno = errno; + goto fail; } + total += len; } while ((m = m->next)); } - if (likely(cnt > 0)) { - ret = writev(self->outfd, iov, cnt); - if (unlikely(ret < 0)) { - rte_errno = errno; - return -1; - } - total += ret; + /* + * Buffered data may still be short of the device, so the flush is + * where a write error such as ENOSPC is seen. + */ + if (self->autoflush && unlikely(fflush(outf) != 0)) { + rte_errno = errno; + goto fail; } + + funlockfile(outf); return total; + +fail: + funlockfile(outf); + return -1; } /* Create new pcapng writer handle */ -RTE_EXPORT_SYMBOL(rte_pcapng_fdopen) +RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_pcapng_fopen, 27.03) rte_pcapng_t * -rte_pcapng_fdopen(int fd, - const char *osname, const char *hardware, - const char *appname, const char *comment) +rte_pcapng_fopen(FILE *f, + const char *osname, const char *hardware, + const char *appname, const char *comment) { unsigned int i; rte_pcapng_t *self; - int ret; - if ((osname && strlen(osname) > PCAPNG_STR_MAX) || + if (f == NULL || + (osname && strlen(osname) > PCAPNG_STR_MAX) || (hardware && strlen(hardware) > PCAPNG_STR_MAX) || (appname && strlen(appname) > PCAPNG_STR_MAX) || (comment && strlen(comment) > PCAPNG_STR_MAX)) { @@ -739,7 +720,9 @@ rte_pcapng_fdopen(int fd, return NULL; } - self->outfd = fd; + self->outf = f; + self->fd = -1; + self->autoflush = false; self->ports = 0; /* Timestamps come from the TSC; if Hz is zero it is unusable. */ @@ -753,11 +736,8 @@ rte_pcapng_fdopen(int fd, for (i = 0; i < RTE_MAX_ETHPORTS; i++) self->port_index[i] = UINT32_MAX; - ret = pcapng_section_block(self, osname, hardware, appname, comment); - if (ret < 0) { - rte_errno = -ret; + if (pcapng_section_block(self, osname, hardware, appname, comment) < 0) goto fail; - } return self; fail: @@ -765,12 +745,59 @@ rte_pcapng_fdopen(int fd, return NULL; } +RTE_EXPORT_SYMBOL(rte_pcapng_fdopen) +rte_pcapng_t * +rte_pcapng_fdopen(int fd, + const char *osname, const char *hardware, + const char *appname, const char *comment) +{ + rte_pcapng_t *self; + FILE *f; + int nfd; + + /* + * On failure the caller still owns fd and will close it, + * so give stdio a duplicate that can be closed here. + */ + nfd = dup(fd); + if (nfd < 0) { + rte_errno = errno; + return NULL; + } + + f = fdopen(nfd, "wb"); + if (f == NULL) { + rte_errno = errno; + close(nfd); + return NULL; + } + + /* + * Buffer a whole burst rather than writing per mbuf segment, and + * keep the previous behavior that data is in the file when the + * write call returns by flushing at the end of each call. + */ + setvbuf(f, NULL, _IOFBF, PCAPNG_BUF_SIZE); + + self = rte_pcapng_fopen(f, osname, hardware, appname, comment); + if (self == NULL) { + fclose(f); + return NULL; + } + + self->fd = fd; + self->autoflush = true; + return self; +} + RTE_EXPORT_SYMBOL(rte_pcapng_close) void rte_pcapng_close(rte_pcapng_t *self) { if (self) { - close(self->outfd); + fclose(self->outf); + if (self->fd >= 0) + close(self->fd); free(self); } } diff --git a/lib/pcapng/rte_pcapng.h b/lib/pcapng/rte_pcapng.h index 647c9d6c2b..2259b3e8ad 100644 --- a/lib/pcapng/rte_pcapng.h +++ b/lib/pcapng/rte_pcapng.h @@ -20,6 +20,7 @@ #define _RTE_PCAPNG_H_ #include +#include #include #include @@ -39,8 +40,12 @@ typedef struct rte_pcapng rte_pcapng_t; /** * Write data to existing open file * + * The stream is flushed after each write, so data is in the file + * when the write call returns. + * * @param fd - * file descriptor + * file descriptor, closed by rte_pcapng_close(). + * On failure it is left open and the caller must close it. * @param osname * Optional description of the operating system. * Examples: "Debian 11", "Windows Server 22" @@ -60,9 +65,43 @@ rte_pcapng_fdopen(int fd, const char *osname, const char *hardware, const char *appname, const char *comment); +/** + * @warning + * @b EXPERIMENTAL: this API may change without prior notice. + * + * Write data to existing open file + * + * @param f + * stdio file handle, closed by rte_pcapng_close(). + * On failure it is left open and the caller must close it. + * @param osname + * Optional description of the operating system. + * Examples: "Debian 11", "Windows Server 22" + * @param hardware + * Optional description of the hardware used to create this file. + * Examples: "x86 Virtual Machine" + * @param appname + * Optional: application name recorded in the pcapng file. + * Example: "dpdk-dumpcap 1.0 (DPDK 20.11)" + * @param comment + * Optional comment to add to file header. + * @return + * handle to library, or NULL in case of error (and rte_errno is set). + */ +__rte_experimental +rte_pcapng_t * +rte_pcapng_fopen(FILE *f, + const char *osname, const char *hardware, + const char *appname, const char *comment); + /** * Close capture file * + * The stream is closed, which flushes anything still buffered. + * Errors from that final flush are not reported, so an application + * that needs to detect a full or failing device should call fflush() + * on its own stream before closing. + * * @param self * handle to library */ @@ -211,6 +250,15 @@ rte_pcapng_mbuf_size(uint32_t length); * Do not pass original mbufs from transmit or receive * or file will be invalid pcapng format. * + * When opened with rte_pcapng_fdopen() the stream is flushed before + * returning, so the data is in the file. + * When opened with rte_pcapng_fopen() the stream's buffering is used, + * and the application can call fflush() when the data needs to + * be in the file, for example for a live reader. + * + * This function uses stdio locking, so multiple threads may write + * packets to the same handle. + * * @param self * The handle to the packet capture file * @param pkts -- 2.53.0