Skip to content

Commit 7e789d6

Browse files
committed
UDP input: Add support for getting max size of UDP receive buffer in BSD compatible way
1 parent c53d576 commit 7e789d6

File tree

1 file changed

+29
-2
lines changed
  • src/plugins/input/udp

1 file changed

+29
-2
lines changed

src/plugins/input/udp/udp.c

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -446,8 +446,33 @@ listener_init(struct udp_data *instance)
446446
const char *err_str;
447447

448448
// Get maximum socket receive buffer size (in bytes)
449-
FILE *f;
450449
int sock_rmax = 0;
450+
451+
#if defined(__FreeBSD__) || defined(__OpenBSD__)
452+
unsigned long space = 0;
453+
size_t n = sizeof(space);
454+
int sock = socket(PF_INET, SOCK_DGRAM, 0);
455+
if (sock < 0) {
456+
ipx_strerror(errno, err_str);
457+
IPX_CTX_WARNING(instance->ctx, "Unable to get the maximum socket receive buffer size "
458+
"(socket() failed: %s). Due to potentially small buffers, some records may be lost!",
459+
err_str);
460+
}
461+
462+
if (getsockopt(sock, SOL_SOCKET, SO_RCVBUF, &space, (socklen_t*)&n) != 0) {
463+
ipx_strerror(errno, err_str);
464+
IPX_CTX_WARNING(instance->ctx, "Unable to get the maximum socket receive buffer size "
465+
"(getsockopt() failed: %s). Due to potentially small buffers, some records may be lost!",
466+
err_str);
467+
} else {
468+
sock_rmax = (int) space;
469+
}
470+
471+
if (sock != 0) {
472+
close(sock);
473+
}
474+
#else
475+
FILE *f;
451476
static const char *sys_cfg = "/proc/sys/net/core/rmem_max";
452477
if ((f = fopen(sys_cfg, "r")) == NULL || fscanf(f, "%d", &sock_rmax) != 1 || sock_rmax < 0) {
453478
ipx_strerror(errno, err_str);
@@ -457,10 +482,12 @@ listener_init(struct udp_data *instance)
457482
sock_rmax = 0;
458483
}
459484

460-
instance->listen.rmem_size = sock_rmax;
461485
if (f != NULL) {
462486
fclose(f);
463487
}
488+
#endif
489+
490+
instance->listen.rmem_size = sock_rmax;
464491

465492
if (sock_rmax != 0 && sock_rmax < UDP_RMEM_REQ) {
466493
IPX_CTX_WARNING(instance->ctx, "The maximum socket receive buffer size is too small "

0 commit comments

Comments
 (0)