Search code examples
socketsmemorylinux-kerneldatagram

How to Increase socket memory allocation in Linux kernel


I'm implementing a custom transport layer datagram protocol in the Linux kernel. I've implement send and receive Queues for in-order delivery in lossy environments.

I noticed that with my current implementation, My socket runs out of memory with only 16 socket buffers with BUFSIZ payload in the queue. So I need to increase the value of memory allocated to my socket.

I figured that changing the values of sk->sk_sendbuf and sk->sk_rcvbuf should do the job. What is the correct way to do this?

P.S.- I haven't implemented the sysctl interface for this protocol yet, so can't use that for memory management.

Thanks.


Solution

  • As it turns out, I need not define the sysctl interface manually for my protocol. I just used the following sysctl command on my test machine to increase the amount of memory allocated to each socket

    sysctl -w net.core.wmem_default=<new_value>
    sysctl -w net.core.wmem_max=<new_value>
    

    To select the new_value, I first checked the existing value of these parameters with

    sysctl -n net.core.wmem_default
    sysctl -n net.core.wmem_max
    

    Note that the actual memory assigned to the socket will be twice the new_value, that's just how the implementation is in the kernel.