Store them next to the mnl_socket pointer. While being at it, add a comment to mnl_set_rcvbuffer() explaining why the buffer size is changed. Signed-off-by: Phil Sutter <phil@xxxxxx> --- iptables/nft.c | 17 +++++++---------- iptables/nft.h | 2 ++ 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/iptables/nft.c b/iptables/nft.c index 4a5280916e3b1..e927d1db2b426 100644 --- a/iptables/nft.c +++ b/iptables/nft.c @@ -186,13 +186,11 @@ static void mnl_err_list_free(struct mnl_err *err) free(err); } -static int nlbuffsiz; - static void mnl_set_sndbuffer(struct nft_handle *h) { int newbuffsiz = nftnl_batch_iovec_len(h->batch) * BATCH_PAGE_SIZE; - if (newbuffsiz <= nlbuffsiz) + if (newbuffsiz <= h->nlsndbuffsiz) return; /* Rise sender buffer length to avoid hitting -EMSGSIZE */ @@ -200,23 +198,22 @@ static void mnl_set_sndbuffer(struct nft_handle *h) &newbuffsiz, sizeof(socklen_t)) < 0) return; - nlbuffsiz = newbuffsiz; + h->nlsndbuffsiz = newbuffsiz; } -static int nlrcvbuffsiz; - static void mnl_set_rcvbuffer(struct nft_handle *h, int numcmds) { int newbuffsiz = getpagesize() * numcmds; - if (newbuffsiz <= nlrcvbuffsiz) + if (newbuffsiz <= h->nlrcvbuffsiz) return; + /* Rise receiver buffer length to avoid hitting -ENOBUFS */ if (setsockopt(mnl_socket_get_fd(h->nl), SOL_SOCKET, SO_RCVBUFFORCE, &newbuffsiz, sizeof(socklen_t)) < 0) return; - nlrcvbuffsiz = newbuffsiz; + h->nlrcvbuffsiz = newbuffsiz; } static ssize_t mnl_nft_socket_sendmsg(struct nft_handle *h, int numcmds) @@ -807,8 +804,8 @@ static int nft_restart(struct nft_handle *h) return -1; h->portid = mnl_socket_get_portid(h->nl); - nlbuffsiz = 0; - nlrcvbuffsiz = 0; + h->nlsndbuffsiz = 0; + h->nlrcvbuffsiz = 0; return 0; } diff --git a/iptables/nft.h b/iptables/nft.h index 43eb8a39dd9c1..dc1161840a38c 100644 --- a/iptables/nft.h +++ b/iptables/nft.h @@ -38,6 +38,8 @@ struct nft_cache { struct nft_handle { int family; struct mnl_socket *nl; + int nlsndbuffsiz; + int nlrcvbuffsiz; uint32_t portid; uint32_t seq; uint32_t nft_genid; -- 2.21.0