4

Is it possible to change TCP congestion control algorithm from Cubic to Reno or vice versa using setsockopt call from C++ code in linux?
I am looking for an example code of doing so.

TheWaterProgrammer
  • 7,055
  • 12
  • 70
  • 159

1 Answers1

5

You can use the TCP_CONGESTION socket option to get or set the congestion control algorithm for a socket to one of the values listed in /proc/sys/net/ipv4/tcp_allowed_congestion_control or to any one of the values in /proc/sys/net/ipv4/tcp_available_congestion_control if your process is privileged.

C example:

#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

int main(int argc, char **argv)
{
    char buf[256];
    socklen_t len;
    int sock = socket(AF_INET, SOCK_STREAM, 0);

    if (sock == -1)
    {
        perror("socket");
        return -1;
    }

    len = sizeof(buf);

    if (getsockopt(sock, IPPROTO_TCP, TCP_CONGESTION, buf, &len) != 0)
    {
        perror("getsockopt");
        return -1;
    }

    printf("Current: %s\n", buf);

    strcpy(buf, "reno");

    len = strlen(buf);

    if (setsockopt(sock, IPPROTO_TCP, TCP_CONGESTION, buf, len) != 0)
    {
        perror("setsockopt");
        return -1;
    }

    len = sizeof(buf);

    if (getsockopt(sock, IPPROTO_TCP, TCP_CONGESTION, buf, &len) != 0)
    {
        perror("getsockopt");
        return -1;
    }

    printf("New: %s\n", buf);

    close(sock);
    return 0;
}

For me outputs:

Current: cubic
New: reno
Hasturkun
  • 35,395
  • 6
  • 71
  • 104
  • 2
    But, how can I change it using `setsockopt` call from C++ code? That is my question as well. I know that it can be changed command line as well provided that privileges are present. – TheWaterProgrammer Dec 10 '19 at 10:38
  • 3
    @Game_Of_Threads: I've added a small example in C to illustrate. I can add some comments if it isn't self explanatory. – Hasturkun Dec 10 '19 at 11:03