Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/userguide/capture-hardware/pcap-file.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ This can improve performance, especially for large files.
The size can be specified through the command line option, see
:ref:`--pcap-file-buffer-size <cmdline-option-pcap-file-buffer-size>`

Setting ``buffer-size`` to ``0`` disables ``setvbuf`` buffering. This is the
explicit opt-out for non-seekable sources such as ``/dev/stdin`` or named
pipes, where buffering the underlying file descriptor is not supported.

Directory-related options
-------------------------

Expand Down
3 changes: 2 additions & 1 deletion doc/userguide/partials/options.rst
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@
.. option:: --pcap-file-buffer-size <value>

Set read buffer size using ``setvbuf`` to speed up pcap reading. Valid values
are 4 KiB to 64 MiB. Default value is 128 KiB. Supported on Linux only.
are 0, which disables ``setvbuf`` buffering, or 4 KiB to 64 MiB. Default
value is 128 KiB. Supported on Linux only.

.. option:: -i <interface>

Expand Down
14 changes: 10 additions & 4 deletions src/source-pcap-file-helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -271,10 +271,16 @@ TmEcode InitPcapFile(PcapFileFileVars *pfv)

#if defined(HAVE_SETVBUF) && defined(OS_LINUX)
if (pcap_g.read_buffer_size > 0) {
errno = 0;
if (setvbuf(pcap_file(pfv->pcap_handle), pfv->buffer, _IOFBF, pcap_g.read_buffer_size) <
0) {
SCLogWarning("Failed to setvbuf on PCAP file handle: %s", strerror(errno));
struct stat sb;
int fd = fileno(pcap_file(pfv->pcap_handle));
if (fd >= 0 && fstat(fd, &sb) == 0 && !S_ISREG(sb.st_mode)) {
SCLogInfo("%s: skipping setvbuf, underlying fd is not a regular file", pfv->filename);
} else {
errno = 0;
if (setvbuf(pcap_file(pfv->pcap_handle), pfv->buffer, _IOFBF,
pcap_g.read_buffer_size) != 0) {
SCLogWarning("Failed to setvbuf on PCAP file handle: %s", strerror(errno));
}
}
}
#endif
Expand Down
16 changes: 11 additions & 5 deletions src/source-pcap-file.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,19 @@ void PcapFileGlobalInit(void)
if (SCConfGet("pcap-file.buffer-size", &str) == 1) {
uint32_t value = 0;
if (ParseSizeStringU32(str, &value) < 0) {
SCLogWarning("failed to parse pcap-file.buffer-size %s", str);
}
if (value >= PCAP_FILE_BUFFER_SIZE_MIN && value <= PCAP_FILE_BUFFER_SIZE_MAX) {
SCLogInfo("Pcap-file will use %u buffer size", value);
SCLogWarning("failed to parse pcap-file.buffer-size %s; keeping default %u", str,
PCAP_FILE_BUFFER_SIZE_DEFAULT);
} else if (value == 0 ||
(value >= PCAP_FILE_BUFFER_SIZE_MIN && value <= PCAP_FILE_BUFFER_SIZE_MAX)) {
if (value == 0) {
SCLogInfo("Pcap-file buffering disabled");
} else {
SCLogInfo("Pcap-file will use %u buffer size", value);
}
pcap_g.read_buffer_size = value;
} else {
SCLogWarning("pcap-file.buffer-size value of %u is invalid. Valid range is %u-%u",
SCLogWarning("pcap-file.buffer-size value of %u is invalid. Valid values are 0 to "
"disable buffering, or %u-%u",
value, PCAP_FILE_BUFFER_SIZE_MIN, PCAP_FILE_BUFFER_SIZE_MAX);
}
}
Expand Down
Loading