refactor max tcp port config values to uint16 to better represent their valid space

This commit is contained in:
Patricio Whittingslow
2026-04-09 20:13:45 -03:00
parent 548d96da95
commit 568ef2854f
12 changed files with 129 additions and 124 deletions
+15 -11
View File
@@ -62,13 +62,17 @@ type StackAsync struct {
}
type StackConfig struct {
StaticAddress netip.Addr
DNSServer netip.Addr
NTPServer netip.Addr
RandSeed int64
Hostname string
MaxTCPConns int
MaxUDPConns int
StaticAddress netip.Addr
DNSServer netip.Addr
NTPServer netip.Addr
RandSeed int64
Hostname string
// MaxActiveTCPPorts and MaxActiveUDPPorts are a memory guardrail to limit
// number of simultaneous open TCP/UDP ports. The memory impact at the stack level
// of a port corresponds to ~64 bytes excluding the registered StackNode i.e: [tcp.Conn] or [udp.Conn].
MaxActiveTCPPorts, MaxActiveUDPPorts uint16
EthernetTxCRC32Update func(crc uint32, b []byte) uint32
HardwareAddress [6]byte
@@ -170,16 +174,16 @@ func (s *StackAsync) Reset(cfg StackConfig) error {
if err != nil {
return err
}
udpConns := 3 + cfg.MaxUDPConns // DHCP, DNS, NTP + user-registered.
udpConns := 3 + cfg.MaxActiveUDPPorts // DHCP, DNS, NTP + user-registered.
err = s.udps.ResetUDP(udpConns)
if err != nil {
return err
}
internal.SliceReuse(&s.userUDPs, cfg.MaxUDPConns)
internal.SliceReuse(&s.userUDPs, int(cfg.MaxActiveUDPPorts))
// Enable TCP if connections present.
if cfg.MaxTCPConns > 0 {
err = s.tcps.ResetTCP(cfg.MaxTCPConns)
if cfg.MaxActiveTCPPorts > 0 {
err = s.tcps.ResetTCP(cfg.MaxActiveTCPPorts)
if err != nil {
return err
}
+3 -2
View File
@@ -32,7 +32,8 @@ func _() {
}
type TCPPoolConfig struct {
PoolSize int
// PoolSize determines the maximum number of active incoming TCP connections to the pool.
PoolSize uint16
QueueSize int
TxBufSize int
RxBufSize int
@@ -58,7 +59,7 @@ func NewTCPPool(cfg TCPPoolConfig) (*TCPPool, error) {
if cfg.EstablishedTimeout <= 0 || cfg.ClosingTimeout <= 0 {
return nil, lneto.ErrInvalidConfig
}
n := cfg.PoolSize
n := int(cfg.PoolSize)
pool := &TCPPool{
acquiredAt: make([]int64, n),
closingAt: make([]int64, n),
+12 -12
View File
@@ -83,23 +83,23 @@ func BenchmarkTCPHandshake(b *testing.B) {
clconn, svconn := new(tcp.Conn), new(tcp.Conn)
err := sv.Reset(StackConfig{
Hostname: "Server",
RandSeed: 1,
StaticAddress: netip.AddrFrom4([4]byte{10, 0, 0, 1}),
MaxTCPConns: 1,
HardwareAddress: [6]byte{0xbe, 0xef, 0, 0, 0, 1},
MTU: MTU,
Hostname: "Server",
RandSeed: 1,
StaticAddress: netip.AddrFrom4([4]byte{10, 0, 0, 1}),
MaxActiveTCPPorts: 1,
HardwareAddress: [6]byte{0xbe, 0xef, 0, 0, 0, 1},
MTU: MTU,
})
if err != nil {
b.Fatal(err)
}
err = client.Reset(StackConfig{
Hostname: "Client",
RandSeed: 2,
StaticAddress: netip.AddrFrom4([4]byte{10, 0, 0, 2}),
MaxTCPConns: 1,
HardwareAddress: [6]byte{0xbe, 0xef, 0, 0, 0, 2},
MTU: MTU,
Hostname: "Client",
RandSeed: 2,
StaticAddress: netip.AddrFrom4([4]byte{10, 0, 0, 2}),
MaxActiveTCPPorts: 1,
HardwareAddress: [6]byte{0xbe, 0xef, 0, 0, 0, 2},
MTU: MTU,
})
if err != nil {
b.Fatal(err)
+12 -12
View File
@@ -30,12 +30,12 @@ func TestTCPListener_ConcurrentEcho(t *testing.T) {
serverMAC := [6]byte{0xaa, 0xbb, 0xcc, 0x00, 0x00, 0x01}
serverIP := netip.AddrFrom4([4]byte{10, 0, 0, 1})
err := serverStack.Reset(StackConfig{
Hostname: "Server",
RandSeed: seed,
StaticAddress: serverIP,
MaxTCPConns: numClients,
HardwareAddress: serverMAC,
MTU: MTU,
Hostname: "Server",
RandSeed: seed,
StaticAddress: serverIP,
MaxActiveTCPPorts: numClients,
HardwareAddress: serverMAC,
MTU: MTU,
})
if err != nil {
t.Fatal(err)
@@ -72,12 +72,12 @@ func TestTCPListener_ConcurrentEcho(t *testing.T) {
clientMAC := [6]byte{0xaa, 0xbb, 0xcc, 0x00, 0x01, byte(i + 1)}
clientIP := netip.AddrFrom4([4]byte{10, 0, 0, byte(i + 10)})
err := clientStacks[i].Reset(StackConfig{
Hostname: fmt.Sprintf("Client%d", i),
RandSeed: int64(seed + i + 1),
StaticAddress: clientIP,
MaxTCPConns: 1,
HardwareAddress: clientMAC,
MTU: MTU,
Hostname: fmt.Sprintf("Client%d", i),
RandSeed: int64(seed + i + 1),
StaticAddress: clientIP,
MaxActiveTCPPorts: 1,
HardwareAddress: clientMAC,
MTU: MTU,
})
if err != nil {
t.Fatalf("client %d reset: %v", i, err)
+24 -24
View File
@@ -19,23 +19,23 @@ func TestStackAsyncListener_SingleConnection(t *testing.T) {
// Create two stacks.
client, sv := new(StackAsync), new(StackAsync)
err := client.Reset(StackConfig{
Hostname: "Client",
RandSeed: seed,
StaticAddress: netip.AddrFrom4([4]byte{10, 0, 0, 1}),
MaxTCPConns: 1,
HardwareAddress: [6]byte{0xbe, 0xef, 0, 0, 0, 1},
MTU: MTU,
Hostname: "Client",
RandSeed: seed,
StaticAddress: netip.AddrFrom4([4]byte{10, 0, 0, 1}),
MaxActiveTCPPorts: 1,
HardwareAddress: [6]byte{0xbe, 0xef, 0, 0, 0, 1},
MTU: MTU,
})
if err != nil {
t.Fatal(err)
}
err = sv.Reset(StackConfig{
Hostname: "Server",
RandSeed: ^seed,
StaticAddress: netip.AddrFrom4([4]byte{10, 0, 0, 2}),
MaxTCPConns: 1, // Note: We use listener, not direct TCP conn registration.
HardwareAddress: [6]byte{0xbe, 0xef, 0, 0, 0, 2},
MTU: MTU,
Hostname: "Server",
RandSeed: ^seed,
StaticAddress: netip.AddrFrom4([4]byte{10, 0, 0, 2}),
MaxActiveTCPPorts: 1, // Note: We use listener, not direct TCP conn registration.
HardwareAddress: [6]byte{0xbe, 0xef, 0, 0, 0, 2},
MTU: MTU,
})
if err != nil {
t.Fatal(err)
@@ -131,12 +131,12 @@ func TestStackAsyncListener_MultiSequentialConn(t *testing.T) {
// Create two stacks.
sv := new(StackAsync)
err := sv.Reset(StackConfig{
Hostname: "Server",
RandSeed: ^seed,
StaticAddress: netip.AddrFrom4([4]byte{10, 0, 0, 2}),
MaxTCPConns: 1, // Note: We use listener, not direct TCP conn registration.
HardwareAddress: [6]byte{0xbe, 0xef, 0, 0, 0, 2},
MTU: MTU,
Hostname: "Server",
RandSeed: ^seed,
StaticAddress: netip.AddrFrom4([4]byte{10, 0, 0, 2}),
MaxActiveTCPPorts: 1, // Note: We use listener, not direct TCP conn registration.
HardwareAddress: [6]byte{0xbe, 0xef, 0, 0, 0, 2},
MTU: MTU,
})
if err != nil {
t.Fatal(err)
@@ -171,12 +171,12 @@ func TestStackAsyncListener_MultiSequentialConn(t *testing.T) {
doRequest := func(caddrp netip.AddrPort, sleep time.Duration, data []byte) {
var client StackAsync
err := client.Reset(StackConfig{
Hostname: "Client",
RandSeed: seed,
StaticAddress: caddrp.Addr(),
MaxTCPConns: 1,
HardwareAddress: chw,
MTU: MTU,
Hostname: "Client",
RandSeed: seed,
StaticAddress: caddrp.Addr(),
MaxActiveTCPPorts: 1,
HardwareAddress: chw,
MTU: MTU,
})
if err != nil {
panic(err)
+21 -21
View File
@@ -43,13 +43,13 @@ func TestMDNS_QueryResponse(t *testing.T) {
// Setup responder stack with mDNS service.
responderStack := new(StackAsync)
err = responderStack.Reset(StackConfig{
Hostname: "responder",
RandSeed: 1234,
StaticAddress: responderAddr,
HardwareAddress: responderMAC,
MTU: MTU,
MaxUDPConns: 1,
AcceptMulticast: true,
Hostname: "responder",
RandSeed: 1234,
StaticAddress: responderAddr,
HardwareAddress: responderMAC,
MTU: MTU,
MaxActiveUDPPorts: 1,
AcceptMulticast: true,
})
if err != nil {
t.Fatal("responder reset:", err)
@@ -73,13 +73,13 @@ func TestMDNS_QueryResponse(t *testing.T) {
// Setup querier stack.
querierStack := new(StackAsync)
err = querierStack.Reset(StackConfig{
Hostname: "querier",
RandSeed: 5678,
StaticAddress: querierAddr,
HardwareAddress: querierMAC,
MTU: MTU,
MaxUDPConns: 1,
AcceptMulticast: true,
Hostname: "querier",
RandSeed: 5678,
StaticAddress: querierAddr,
HardwareAddress: querierMAC,
MTU: MTU,
MaxActiveUDPPorts: 1,
AcceptMulticast: true,
})
if err != nil {
t.Fatal("querier reset:", err)
@@ -278,13 +278,13 @@ func newMDNSStack(t *testing.T, hostname string, seed int64,
const MTU = 1500
stack := new(StackAsync)
err := stack.Reset(StackConfig{
Hostname: hostname,
RandSeed: seed,
StaticAddress: addr,
HardwareAddress: mac,
MTU: MTU,
MaxUDPConns: 1,
AcceptMulticast: true,
Hostname: hostname,
RandSeed: seed,
StaticAddress: addr,
HardwareAddress: mac,
MTU: MTU,
MaxActiveUDPPorts: 1,
AcceptMulticast: true,
})
if err != nil {
t.Fatal(hostname, "reset:", err)
+12 -12
View File
@@ -175,12 +175,12 @@ func newTCPStacks(t testing.TB, randSeed int64, mtu int) (s1, s2 *StackAsync, c1
c1, c2 = new(tcp.Conn), new(tcp.Conn)
byte1 := byte(randSeed)/4 - 1
err := s1.Reset(StackConfig{
Hostname: "Stack1",
RandSeed: randSeed,
StaticAddress: netip.AddrFrom4([4]byte{10, 0, 0, byte1}),
MaxTCPConns: 1,
HardwareAddress: [6]byte{0xbe, 0xef, 0, 0, 0, byte1},
MTU: uint16(mtu),
Hostname: "Stack1",
RandSeed: randSeed,
StaticAddress: netip.AddrFrom4([4]byte{10, 0, 0, byte1}),
MaxActiveTCPPorts: 1,
HardwareAddress: [6]byte{0xbe, 0xef, 0, 0, 0, byte1},
MTU: uint16(mtu),
})
if err != nil {
t.Fatal(err)
@@ -188,12 +188,12 @@ func newTCPStacks(t testing.TB, randSeed int64, mtu int) (s1, s2 *StackAsync, c1
byte2 := byte1 + 1
err = s2.Reset(StackConfig{
Hostname: "Stack2",
RandSeed: ^randSeed,
StaticAddress: netip.AddrFrom4([4]byte{10, 0, 0, byte2}),
MaxTCPConns: 1,
HardwareAddress: [6]byte{0xbe, 0xef, 0, 0, 0, byte2},
MTU: uint16(mtu),
Hostname: "Stack2",
RandSeed: ^randSeed,
StaticAddress: netip.AddrFrom4([4]byte{10, 0, 0, byte2}),
MaxActiveTCPPorts: 1,
HardwareAddress: [6]byte{0xbe, 0xef, 0, 0, 0, byte2},
MTU: uint16(mtu),
})
if err != nil {
t.Fatal(err)