c - Does Linux raw socket buffer size have upper limit of 256 K? -
i using following code in centos change raw socket buffer size 400 kb, got same result set buffer size 256 kb. wrong? or limitation of socket layer? kernel version 2.6.34. thanks!
int rawsock; socklen_t socklen; int optval; int bufsize = 400 * 1024; rawsock = socket(pf_packet, sock_raw, htons(eth_p_all)); if (rawsock < 0) { my_log(log_err, "error creating raw socket"); return rawsock; } optval = 0; socklen = 4; err = getsockopt(rawsock, sol_socket, so_rcvbuf, &optval, &socklen); bail_error(err); my_log("socket rx original buffer size = %d", optval); optval = 0; socklen = 4; err = getsockopt(rawsock, sol_socket, so_sndbuf, &optval, &socklen); bail_error(err); my_log("socket tx original buffer size = %d", optval); err = setsockopt(rawsock, sol_socket, so_rcvbuf, &bufsize, sizeof(bufsize)); bail_error(err); err = setsockopt(rawsock, sol_socket, so_sndbuf, &bufsize, sizeof(bufsize)); bail_error(err); optval = 0; socklen = 4; err = getsockopt(rawsock, sol_socket, so_rcvbuf, &optval, &socklen); bail_error(err); my_log("socket rx new buffer size = %d", optval); optval = 0; socklen = 4; err = getsockopt(rawsock, sol_socket, so_sndbuf, &optval, &socklen); bail_error(err); my_log("socket tx new buffer size = %d", optval);
after running, result is:
socket rx original buffer size = 110592 socket tx original buffer size = 110592 socket rx new buffer size = 524288 socket tx new buffer size = 524288
you're hitting system's current sysctl limits net.core.wmem_max
, net.core.rmem_max
.
if process has superuser privileges, can use so_sndbufforce
, so_rcvbufforce
ioctls override limits. if there real reason why service require larger buffers -- is, other reason besides poor development or design choices --, recommend way. there no such reason, in case recommend fix application/service code instead.
you can modify limits system-wide, they'll affect processes. defaults work fine, in specialized cases (embedded servers wide long-latency network connections, perhaps?) might wish modify them.
to temporarily (until next boot), run sysctl -w net.core.wmem_max=bytes
, sysctl -w net.core.rmem_max=bytes
root (where bytes
new limit decimal number, in bytes).
to make changes permanent, add
net.core.rmem_max=bytes net.core.wmem_max=bytes
to /etc/sysctl.conf
file, or new file in /etc/sysctl.d/
directory if linux distribution provides one. latter better approach, because won't stop updates default configuration files.
if want delve deeper these , other socket ioctls, can take @ kernel net/core/sock.c
file, , sock_setsockopt()
function therein.
Comments
Post a Comment