# How TCP Buffer Sizes Are Configured in OpenFlux: gVisor Stack Implementation

> Discover how OpenFlux configures TCP buffer sizes within its gVisor stack. Learn about compile-time constants and the SetTCPBuffers function for efficient network performance.

- Repository: [p1neappleXpress/OpenFlux](https://github.com/p1neappleXpress/OpenFlux)
- Tags: internals
- Published: 2026-09-14

---

**OpenFlux hard-codes TCP buffer sizes as compile-time constants (4 MiB minimum, 16 MiB default, and 64 MiB maximum) in [`tunnel/tunnel.go`](https://github.com/p1neappleXpress/OpenFlux/blob/main/tunnel/tunnel.go), applying them to the gVisor network stack via the `SetTCPBuffers` helper function during tunnel initialization.**

OpenFlux leverages the **gVisor** network stack to handle TCP tunnels for its universal bypass capabilities. Understanding how TCP buffer sizes are configured in OpenFlux requires examining the constant definitions and the initialization logic that applies these limits to the underlying virtual network stack. This configuration directly impacts throughput and memory usage for all VPN-style traffic handled by the application.

## Where TCP Buffer Limits Are Defined in OpenFlux

The authoritative source for TCP buffer configuration resides in **[`tunnel/tunnel.go`](https://github.com/p1neappleXpress/OpenFlux/blob/main/tunnel/tunnel.go)**. At lines 62–64, three constants define the allowable range for both send and receive buffers:

```go
const (
    TCPBufMin     = 4 * 1024 * 1024   // 4 MiB
    TCPBufDefault = 16 * 1024 * 1024  // 16 MiB
    TCPBufMax     = 64 * 1024 * 1024  // 64 MiB
)

```

These values establish a hard-coded floor, default, and ceiling for TCP buffer allocations. The 16 MiB default suits high-throughput scenarios typical of VPN traffic, while the 4–64 MiB range prevents pathological memory consumption or starvation.

## How the gVisor Stack Applies Buffer Sizes

When OpenFlux instantiates a new network stack, the `SetTCPBuffers` helper function (defined at line 97 of [`tunnel/tunnel.go`](https://github.com/p1neappleXpress/OpenFlux/blob/main/tunnel/tunnel.go)) configures the transport protocol options. This function creates gVisor option structs that wrap the constants and applies them to both receive and send buffers:

```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)
}

```

The function utilizes `tcpip.TCPReceiveBufferSizeRangeOption` and `tcpip.TCPSendBufferSizeRangeOption` from the gVisor TCP/IP package to enforce these limits at the protocol level.

## Packet Tunnel Initialization

To ensure consistent buffer configuration across all tunnel implementations, `SetTCPBuffers` is invoked from multiple entry points. During packet-level tunnel construction in **[`tunnel/packettunnel.go`](https://github.com/p1neappleXpress/OpenFlux/blob/main/tunnel/packettunnel.go)** (line 53), the helper is called to apply the buffer settings immediately after stack creation. This mirrors the initialization logic in the main tunnel implementation, guaranteeing uniform TCP behavior regardless of the specific tunnel type instantiated.

## Modifying TCP Buffer Sizes for Custom Builds

Because OpenFlux treats these values as compile-time constants, **you cannot adjust buffer sizes via command-line flags or configuration files**. Changing the limits requires editing the source and rebuilding the binary.

For example, to reduce memory footprint for low-resource environments, modify the constants in [`tunnel/tunnel.go`](https://github.com/p1neappleXpress/OpenFlux/blob/main/tunnel/tunnel.go):

```go
const (
    TCPBufMin     = 1 * 1024 * 1024   // 1 MiB
    TCPBufDefault = 4 * 1024 * 1024   // 4 MiB
    TCPBufMax     = 8 * 1024 * 1024   // 8 MiB
)

```

After saving your changes, recompile with:

```bash
go build ./...

```

The new limits take effect immediately for all subsequent tunnel connections instantiated through the p1neappleXpress/OpenFlux codebase.

## Summary

- **Hard-coded constants**: OpenFlux defines `TCPBufMin`, `TCPBufDefault`, and `TCPBufMax` in [`tunnel/tunnel.go`](https://github.com/p1neappleXpress/OpenFlux/blob/main/tunnel/tunnel.go) (lines 62–64) with values of 4 MiB, 16 MiB, and 64 MiB respectively.
- **gVisor integration**: The `SetTCPBuffers` function applies these limits to the gVisor stack using `TCPReceiveBufferSizeRangeOption` and `TCPSendBufferSizeRangeOption`.
- **Consistent application**: Both standard tunnels and packet tunnels ([`tunnel/packettunnel.go`](https://github.com/p1neappleXpress/OpenFlux/blob/main/tunnel/packettunnel.go), line 53) initialize buffers via the same helper to ensure uniform behavior.
- **Compile-time only**: Modifying buffer sizes requires editing source constants and rebuilding with `go build`; no runtime configuration is exposed.

## Frequently Asked Questions

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

OpenFlux uses a default buffer size of **16 MiB**, with a minimum of 4 MiB and a maximum of 64 MiB. These values are defined as constants in [`tunnel/tunnel.go`](https://github.com/p1neappleXpress/OpenFlux/blob/main/tunnel/tunnel.go) and apply equally to both send and receive buffers in the gVisor network stack.

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

No. The buffer sizes are **compile-time constants** defined in the source code. There are no command-line flags or configuration file options to adjust these values at runtime. You must modify the constants in [`tunnel/tunnel.go`](https://github.com/p1neappleXpress/OpenFlux/blob/main/tunnel/tunnel.go) and rebuild the binary using `go build ./...` to apply changes.

### Why does OpenFlux default to 16 MiB TCP buffers?

The 16 MiB default accommodates high-throughput VPN-style traffic typical of OpenFlux use cases, maximizing bandwidth-delay product utilization. The 64 MiB upper bound protects against excessive memory consumption, while the 4 MiB minimum ensures functional performance on constrained paths without risking buffer starvation.

### Where is the SetTCPBuffers function called in the codebase?

According to the OpenFlux source code, `SetTCPBuffers` is called in **[`tunnel/tunnel.go`](https://github.com/p1neappleXpress/OpenFlux/blob/main/tunnel/tunnel.go)** (line 97) during standard stack initialization, and in **[`tunnel/packettunnel.go`](https://github.com/p1neappleXpress/OpenFlux/blob/main/tunnel/packettunnel.go)** (line 53) when constructing packet-level tunnels. This dual invocation ensures consistent TCP buffer configuration across all tunnel implementations.