netlink: don't pretend that sysconf isn't a function

We can cache the value of this instead of evaluating every time.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld 2020-02-05 23:59:55 +01:00
parent ef117a91d1
commit 0dc32bbeaf
2 changed files with 15 additions and 4 deletions

View file

@ -40,7 +40,7 @@
#define SOCK_PATH RUNSTATEDIR "/wireguard/" #define SOCK_PATH RUNSTATEDIR "/wireguard/"
#define SOCK_SUFFIX ".sock" #define SOCK_SUFFIX ".sock"
#ifdef __linux__ #ifdef __linux__
#define SOCKET_BUFFER_SIZE MNL_SOCKET_BUFFER_SIZE #define SOCKET_BUFFER_SIZE (mnl_ideal_socket_buffer_size())
#else #else
#define SOCKET_BUFFER_SIZE 8192 #define SOCKET_BUFFER_SIZE 8192
#endif #endif

View file

@ -20,7 +20,6 @@
#include <linux/genetlink.h> #include <linux/genetlink.h>
#define MNL_SOCKET_AUTOPID 0 #define MNL_SOCKET_AUTOPID 0
#define MNL_SOCKET_BUFFER_SIZE (sysconf(_SC_PAGESIZE) < 8192L ? sysconf(_SC_PAGESIZE) : 8192L)
#define MNL_ALIGNTO 4 #define MNL_ALIGNTO 4
#define MNL_ALIGN(len) (((len)+MNL_ALIGNTO-1) & ~(MNL_ALIGNTO-1)) #define MNL_ALIGN(len) (((len)+MNL_ALIGNTO-1) & ~(MNL_ALIGNTO-1))
#define MNL_NLMSG_HDRLEN MNL_ALIGN(sizeof(struct nlmsghdr)) #define MNL_NLMSG_HDRLEN MNL_ALIGN(sizeof(struct nlmsghdr))
@ -68,6 +67,18 @@ typedef int (*mnl_cb_t)(const struct nlmsghdr *nlh, void *data);
#define MNL_ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0])) #define MNL_ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
#endif #endif
static size_t mnl_ideal_socket_buffer_size(void)
{
static size_t size = 0;
if (size)
return size;
size = (size_t)sysconf(_SC_PAGESIZE);
if (size > 8192)
size = 8192;
return size;
}
static size_t mnl_nlmsg_size(size_t len) static size_t mnl_nlmsg_size(size_t len)
{ {
return len + MNL_NLMSG_HDRLEN; return len + MNL_NLMSG_HDRLEN;
@ -680,7 +691,7 @@ static int mnlg_socket_recv_run(struct mnlg_socket *nlg, mnl_cb_t data_cb, void
do { do {
err = mnl_socket_recvfrom(nlg->nl, nlg->buf, err = mnl_socket_recvfrom(nlg->nl, nlg->buf,
MNL_SOCKET_BUFFER_SIZE); mnl_ideal_socket_buffer_size());
if (err <= 0) if (err <= 0)
break; break;
err = mnl_cb_run2(nlg->buf, err, nlg->seq, nlg->portid, err = mnl_cb_run2(nlg->buf, err, nlg->seq, nlg->portid,
@ -728,7 +739,7 @@ static struct mnlg_socket *mnlg_socket_open(const char *family_name, uint8_t ver
return NULL; return NULL;
err = -ENOMEM; err = -ENOMEM;
nlg->buf = malloc(MNL_SOCKET_BUFFER_SIZE); nlg->buf = malloc(mnl_ideal_socket_buffer_size());
if (!nlg->buf) if (!nlg->buf)
goto err_buf_alloc; goto err_buf_alloc;