Wednesday, May 14, 2025

How to switch TCP Stack and Congestion Algorithms in FreeBSD

The TCP stack and congestion control algorithms are core components of any modern operating system's networking infrastructure. They directly influence the performance, reliability, and efficiency of data communication over networks, especially over the internet or WANs.

Role of the TCP Stack

The TCP (Transmission Control Protocol) stack is part of the OS kernel that
  1. Manages Reliable Transport
    • Handles packet ordering, retransmission, and acknowledgment (ACKs).
    • Ensures no data is lost, duplicated, or delivered out of order.
  2. Implements Flow Control
    • Uses the sliding window mechanism to prevent overwhelming the receiver.
  3. Implements Congestion Control
    • Reacts to network conditions (e.g., packet loss or delay) to adjust transmission rates.
  4. Integrates with the OS Networking Subsystem
    • Interacts with the IP layer, NIC drivers, and user-space sockets (bind(), send(), etc.).
    • Supports features like NAT traversal, QoS, TCP Fast Open, and ECN (Explicit Congestion Notification).

Role of Congestion Algorithms

Congestion Algorithms determine how fast TCP can send data, especially under varying network conditions. Modern algorithms:
  • Adjust the congestion window (cwnd) dynamically.
  • Try to avoid congestion (proactively) and recover quickly if it happens.

Let's deep dive into options we have in FreeBSD 14 and how we can use them ...

TCP Stack

FreeBSD 14.2 currently supports three TCP stacks.

  1. freebsd (default FreeBSD TCP stack)
  2. rack (RACK-TLP Loss Detection Algorithm for TCP aka Recent ACKnowledgment is modern loss recovery stack)
  3. bbr (Bottleneck Bandwidth and Round-Trip Time Algorithm)

We can check current TCP stack 

root@FBSD-01:~ # sysctl -a | grep net.inet.tcp.functions_default
net.inet.tcp.functions_default: freebsd

Now we can change TCP stack to TCP stack rack

kldload tcp_rack
sysctl net.inet.tcp.functions_default=rack

and check current TCP stack again

root@FBSD-01:~ # sysctl -a | grep net.inet.tcp.functions_default
net.inet.tcp.functions_default: rack

Now we can change TCP stack to TCP stack bbr

kldload tcp_bbr
sysctl net.inet.tcp.functions_default=bbr

and check current TCP stack again

root@FBSD-01:~ # sysctl -a | grep net.inet.tcp.functions_default
net.inet.tcp.functions_default: bbr

If we want to switch it back to default FreeBSD TCP stack, we can use command below

sysctl net.inet.tcp.functions_default=freebsd  

and can check current TCP stack 

root@FBSD-01:~ # sysctl -a | grep net.inet.tcp.functions_default
net.inet.tcp.functions_default: freebsd

Congestion Algorithms

FreeBSD 14.2 supports following congestion algorithms

  1. cubic (default)
  2. newreno
  3. htcp
  4. vegas
  5. cdg
  6. chd

To change congestion algorithm, we have to load cc module and switch algorithm as shown below.

root@FBSD-01:~ # kldload cc_newreno
root@FBSD-01:~ # sysctl net.inet.tcp.cc.algorithm=newreno
net.inet.tcp.cc.algorithm: cubic -> newreno

We can check current CC algoritm 

root@FBSD-01:~ # sysctl -a | grep net.inet.tcp.cc.algorithm
net.inet.tcp.cc.algorithm: newreno

The procedure to change other algorithms is the same, the only change is algorithm name.


No comments:

Post a Comment