# How to Configure TCP Send/Receive Buffer Sizes in OpenFlux

> Learn to configure TCP send/receive buffer sizes in OpenFlux. Optimize network throughput by modifying source constants or using Go build flags for gVisor TCP/IP stack settings.

- Repository: [p1neappleXpress/OpenFlux](https://github.com/p1neappleXpress/OpenFlux)
- Tags: how-to-guide
- Published: 2026-09-13

---

**OpenFlux utilizes the gVisor TCP/IP stack with buffer sizes controlled by the `SetTCPBuffers` function in [`tunnel/tunnel.go`](https://github.com/p1neappleXpress/OpenFlux/blob/main/tunnel/tunnel.go), allowing customization through source constant modification or Go build flags to optimize network throughput.**

OpenFlux is a tunneling application built on the gVisor userspace networking stack that routes TCP traffic through encrypted tunnels. Configuring TCP send/receive buffer sizes in OpenFlux requires modifying three package-level constants that define the minimum, default, and maximum buffer ranges applied to every tunnel instance at initialization. This article explains the implementation details found in the `p1neappleXpress/OpenFlux` source code and provides practical methods for tuning these values to match your deployment environment.

## TCP Buffer Constants in tunnel/tunnel.go

According to the OpenFlux source code, TCP buffer sizes are not hard-coded at runtime but are defined as exported variables in [`tunnel/tunnel.go`](https://github.com/p1neappleXpress/OpenFlux/blob/main/tunnel/tunnel.go). The stack configuration relies on three constants that control both send and receive buffers:

| Constant | Default Value | Purpose |
|----------|---------------|---------|
| `TCPBufMin` | `65536` (64 KB) | Minimum buffer allocation per connection |
| `TCPBufDefault` | `262144` (256 KB) | Initial buffer size for new TCP sockets |
| `TCPBufMax` | `1048576` (1 MB) | Upper limit for buffer auto-tuning |

The **gVisor** stack applies these values through the `SetTCPBuffers` helper function, which wraps the `tcpip` transport protocol options:

```go
func SetTCPBuffers(s *stack.Stack) {
    rcv := tcpip.TCPReceiveBufferSizeRangeOption{
        Min: TCPBufMin, Default: TCPBufDefault, Max: TCPBufMax,
    }
    _ = s.SetTransportProtocolOption(tcp.ProtocolNumber, &rcv)

    snd := tcpip.TCPSendBufferSizeRangeOption{
        Min: TCPBufMin, Default: TCPBufDefault, Max: TCPBufMax,
    }
    _ = s.SetTransportProtocolOption(tcp.ProtocolNumber, &snd)
}

```

`SetTransportProtocolOption` registers these ranges with the gVisor `tcp` protocol implementation, ensuring all subsequent sockets use the specified limits for **TCP receive buffer** and **TCP send buffer** operations.

## Methods to Configure TCP Buffer Sizes

OpenFlux offers two approaches to override the default buffer sizes. Both require recompiling the binary, as these values are baked into the stack initialization phase.

### Method 1: Modify Source Constants Directly

Edit the variable declarations in [`tunnel/tunnel.go`](https://github.com/p1neappleXpress/OpenFlux/blob/main/tunnel/tunnel.go) to adjust the global defaults. This approach is straightforward for maintaining a custom fork:

```go
// tunnel/tunnel.go
var (
    TCPBufMin     = 131072   // 128 KB
    TCPBufDefault = 524288   // 512 KB
    TCPBufMax     = 2097152  // 2 MB
)

```

After modifying the constants, rebuild the binary:

```bash
go build -o openflux .

```

`SetTCPBuffers` is invoked from `NewTCPTunnel` immediately after the gVisor stack is instantiated, ensuring your new defaults apply to every tunnel created thereafter.

### Method 2: Override with Go Build Flags

For temporary builds or CI pipelines, use `-ldflags` to inject values at link time without editing source files. The syntax requires the full package path followed by the variable name:

```bash
go build -ldflags="-X github.com/p1neappleXpress/OpenFlux/tunnel.TCPBufMin=131072 \
                   -X github.com/p1neappleXpress/OpenFlux/tunnel.TCPBufDefault=524288 \
                   -X github.com/p1neappleXpress/OpenFlux/tunnel.TCPBufMax=2097152" \
         -o openflux .

```

**Note:** Verify the module path in `go.mod` matches the package import path. If the constants are defined in the `main` package instead of the `tunnel` package, substitute `tunnel` with `main` in the flags above.

## How Buffer Configuration Applies to the Stack

The gVisor stack initialization sequence ensures buffer limits are locked before any TCP connections are accepted. When `NewTCPTunnel` creates a new tunnel instance, it executes the following sequence:

1. Instantiates the gVisor `stack.Stack` object
2. Calls `SetTCPBuffers(s)` to register the **TCPReceiveBufferSizeRangeOption** and **TCPSendBufferSizeRangeOption**
3. Configures the network endpoints with these protocol-wide defaults

Because `SetTransportProtocolOption` applies settings at the protocol level (using `tcp.ProtocolNumber`), the configured ranges affect all TCP sockets within that stack instance. This design choice prioritizes consistency and memory predictability over per-connection flexibility.

## Summary

- OpenFlux defines TCP buffer limits through three variables in [`tunnel/tunnel.go`](https://github.com/p1neappleXpress/OpenFlux/blob/main/tunnel/tunnel.go): `TCPBufMin`, `TCPBufDefault`, and `TCPBufMax`.
- The `SetTCPBuffers` function applies these ranges to the gVisor stack by setting `TCPReceiveBufferSizeRangeOption` and `TCPSendBufferSizeRangeOption` during tunnel initialization.
- You can modify buffer sizes by either editing the source constants or using `go build -ldflags` to override values at compile time.
- Changes require a full rebuild, as buffer configuration happens during stack setup in `NewTCPTunnel`, not at runtime.

## Frequently Asked Questions

### What are the default TCP buffer sizes in OpenFlux?

The default values set in [`tunnel/tunnel.go`](https://github.com/p1neappleXpress/OpenFlux/blob/main/tunnel/tunnel.go) are 64 KB minimum, 256 KB default, and 1 MB maximum. These values balance memory usage and throughput for general-purpose tunneling.

### Can I change TCP buffer sizes without recompiling OpenFlux?

No. Because `SetTCPBuffers` is called during stack initialization in `NewTCPTunnel`, the buffer ranges are fixed when the binary starts. You must recompile using either source edits or `-ldflags` to apply new limits.

### When should I increase TCP buffer sizes?

Increase the **TCP send buffer** and **TCP receive buffer** maximums when deploying on high-bandwidth, high-latency networks (such as cross-continent VPS links) where the bandwidth-delay product exceeds the default 1 MB limit. For memory-constrained environments like iOS Network Extensions, consider reducing the minimum and default values.

### Where is the buffer configuration function located?

The `SetTCPBuffers` function is defined in [`tunnel/tunnel.go`](https://github.com/p1neappleXpress/OpenFlux/blob/main/tunnel/tunnel.go) alongside the buffer constants. It is invoked by `NewTCPTunnel` in the same file immediately after the gVisor stack creation, as evidenced by the source code in the `p1neappleXpress/OpenFlux` repository.