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
@@ -108,7 +108,7 @@ func run() error {
RandSeed: softRand,
HardwareAddress: nicHW,
MTU: uint16(mtu),
MaxTCPConns: 1024,
MaxActiveTCPPorts: 1024,
}); err != nil {
return err
}
@@ -202,7 +202,7 @@ func run() error {
blocking := stack.StackBlocking(5 * time.Millisecond)
berkeley := blocking.StackGo(xnet.StackGoConfig{
ListenerPoolConfig: xnet.TCPPoolConfig{
PoolSize: flagPoolSize,
PoolSize: uint16(flagPoolSize),
QueueSize: 3,
TxBufSize: mtu,
RxBufSize: mtu,
@@ -329,7 +329,7 @@ func mockClient(stack *xnet.StackAsync, port uint16, subnet netip.Prefix) {
target := netip.AddrPortFrom(stack.Addr(), port)
err := mockStack.Reset(xnet.StackConfig{
StaticAddress: subnet.Addr().Next(),
MaxTCPConns: 1,
MaxActiveTCPPorts: 1,
HardwareAddress: stack.Gateway6(),
Hostname: "the-other",
MTU: uint16(stack.MTU()),
+1 -1
View File
@@ -105,7 +105,7 @@ func run() (err error) {
RandSeed: softRand,
HardwareAddress: nicHW,
MTU: uint16(mtu),
MaxTCPConns: 1000,
MaxActiveTCPPorts: 1000,
})
if err != nil {
return err
+1 -1
View File
@@ -64,7 +64,7 @@ func run(ctx context.Context, stack *xnet.StackAsync) error {
Hostname: "lneto-mwe",
RandSeed: time.Now().UnixNano(),
// A passive TCP listener to many remote ports takes up one spot, active TCP clients to one remote port take up a spot.
MaxTCPConns: 1,
MaxActiveTCPPorts: 1,
// MaxUDPConns: 1 , // For MDNS support.
// AcceptMulticast: true, // For MDNS.
MTU: uint16(framelen - ethernet.MaxOverheadSize),
+1 -1
View File
@@ -125,7 +125,7 @@ func run() (err error) {
RandSeed: softRand,
HardwareAddress: brHW,
MTU: uint16(mtu),
MaxTCPConns: 1,
MaxActiveTCPPorts: 1,
})
if err != nil {
return err
+7 -7
View File
@@ -22,21 +22,21 @@ type StackPorts struct {
rstQueue tcp.RSTQueue
}
func (ps *StackPorts) ResetUDP(maxNodes int) error {
func (ps *StackPorts) ResetUDP(maxNodes uint16) error {
return ps.Reset(uint64(lneto.IPProtoUDP), 2, maxNodes)
}
func (ps *StackPorts) ResetTCP(maxNodes int) error {
func (ps *StackPorts) ResetTCP(maxNodes uint16) error {
return ps.Reset(uint64(lneto.IPProtoTCP), 2, maxNodes)
}
func (ps *StackPorts) Reset(protocol uint64, dstPortOffset uint16, maxNodes int) error {
func (ps *StackPorts) Reset(protocol uint64, dstPortOffset, maxNodes uint16) error {
if protocol > math.MaxUint16 {
return lneto.ErrInvalidConfig
} else if maxNodes <= 0 {
return lneto.ErrInvalidConfig
}
ps.handlers.reset("StackPorts(proto="+strconv.Itoa(int(protocol))+")", maxNodes)
ps.handlers.reset("StackPorts(proto="+strconv.Itoa(int(protocol))+")", int(maxNodes))
*ps = StackPorts{
connID: ps.connID + 1,
handlers: ps.handlers,
@@ -118,15 +118,15 @@ func (mfsp *StackPortsMACFiltered) Register(h lneto.StackNode, addr []byte) erro
return mfsp.sp.handlers.registerByPortProto(nodeFromStackNode(h, port, proto, addr))
}
func (ps *StackPortsMACFiltered) ResetUDP(maxNodes int) error {
func (ps *StackPortsMACFiltered) ResetUDP(maxNodes uint16) error {
return ps.sp.ResetUDP(maxNodes)
}
func (ps *StackPortsMACFiltered) ResetTCP(maxNodes int) error {
func (ps *StackPortsMACFiltered) ResetTCP(maxNodes uint16) error {
return ps.sp.ResetTCP(maxNodes)
}
func (ps *StackPortsMACFiltered) Reset(protocol uint64, dstPortOffset uint16, maxNodes int) error {
func (ps *StackPortsMACFiltered) Reset(protocol uint64, dstPortOffset, maxNodes uint16) error {
return ps.sp.Reset(protocol, dstPortOffset, maxNodes)
}
+10 -6
View File
@@ -67,8 +67,12 @@ type StackConfig struct {
NTPServer netip.Addr
RandSeed int64
Hostname string
MaxTCPConns int
MaxUDPConns int
// 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),
+2 -2
View File
@@ -86,7 +86,7 @@ func BenchmarkTCPHandshake(b *testing.B) {
Hostname: "Server",
RandSeed: 1,
StaticAddress: netip.AddrFrom4([4]byte{10, 0, 0, 1}),
MaxTCPConns: 1,
MaxActiveTCPPorts: 1,
HardwareAddress: [6]byte{0xbe, 0xef, 0, 0, 0, 1},
MTU: MTU,
})
@@ -97,7 +97,7 @@ func BenchmarkTCPHandshake(b *testing.B) {
Hostname: "Client",
RandSeed: 2,
StaticAddress: netip.AddrFrom4([4]byte{10, 0, 0, 2}),
MaxTCPConns: 1,
MaxActiveTCPPorts: 1,
HardwareAddress: [6]byte{0xbe, 0xef, 0, 0, 0, 2},
MTU: MTU,
})
+2 -2
View File
@@ -33,7 +33,7 @@ func TestTCPListener_ConcurrentEcho(t *testing.T) {
Hostname: "Server",
RandSeed: seed,
StaticAddress: serverIP,
MaxTCPConns: numClients,
MaxActiveTCPPorts: numClients,
HardwareAddress: serverMAC,
MTU: MTU,
})
@@ -75,7 +75,7 @@ func TestTCPListener_ConcurrentEcho(t *testing.T) {
Hostname: fmt.Sprintf("Client%d", i),
RandSeed: int64(seed + i + 1),
StaticAddress: clientIP,
MaxTCPConns: 1,
MaxActiveTCPPorts: 1,
HardwareAddress: clientMAC,
MTU: MTU,
})
+4 -4
View File
@@ -22,7 +22,7 @@ func TestStackAsyncListener_SingleConnection(t *testing.T) {
Hostname: "Client",
RandSeed: seed,
StaticAddress: netip.AddrFrom4([4]byte{10, 0, 0, 1}),
MaxTCPConns: 1,
MaxActiveTCPPorts: 1,
HardwareAddress: [6]byte{0xbe, 0xef, 0, 0, 0, 1},
MTU: MTU,
})
@@ -33,7 +33,7 @@ func TestStackAsyncListener_SingleConnection(t *testing.T) {
Hostname: "Server",
RandSeed: ^seed,
StaticAddress: netip.AddrFrom4([4]byte{10, 0, 0, 2}),
MaxTCPConns: 1, // Note: We use listener, not direct TCP conn registration.
MaxActiveTCPPorts: 1, // Note: We use listener, not direct TCP conn registration.
HardwareAddress: [6]byte{0xbe, 0xef, 0, 0, 0, 2},
MTU: MTU,
})
@@ -134,7 +134,7 @@ func TestStackAsyncListener_MultiSequentialConn(t *testing.T) {
Hostname: "Server",
RandSeed: ^seed,
StaticAddress: netip.AddrFrom4([4]byte{10, 0, 0, 2}),
MaxTCPConns: 1, // Note: We use listener, not direct TCP conn registration.
MaxActiveTCPPorts: 1, // Note: We use listener, not direct TCP conn registration.
HardwareAddress: [6]byte{0xbe, 0xef, 0, 0, 0, 2},
MTU: MTU,
})
@@ -174,7 +174,7 @@ func TestStackAsyncListener_MultiSequentialConn(t *testing.T) {
Hostname: "Client",
RandSeed: seed,
StaticAddress: caddrp.Addr(),
MaxTCPConns: 1,
MaxActiveTCPPorts: 1,
HardwareAddress: chw,
MTU: MTU,
})
+3 -3
View File
@@ -48,7 +48,7 @@ func TestMDNS_QueryResponse(t *testing.T) {
StaticAddress: responderAddr,
HardwareAddress: responderMAC,
MTU: MTU,
MaxUDPConns: 1,
MaxActiveUDPPorts: 1,
AcceptMulticast: true,
})
if err != nil {
@@ -78,7 +78,7 @@ func TestMDNS_QueryResponse(t *testing.T) {
StaticAddress: querierAddr,
HardwareAddress: querierMAC,
MTU: MTU,
MaxUDPConns: 1,
MaxActiveUDPPorts: 1,
AcceptMulticast: true,
})
if err != nil {
@@ -283,7 +283,7 @@ func newMDNSStack(t *testing.T, hostname string, seed int64,
StaticAddress: addr,
HardwareAddress: mac,
MTU: MTU,
MaxUDPConns: 1,
MaxActiveUDPPorts: 1,
AcceptMulticast: true,
})
if err != nil {
+2 -2
View File
@@ -178,7 +178,7 @@ func newTCPStacks(t testing.TB, randSeed int64, mtu int) (s1, s2 *StackAsync, c1
Hostname: "Stack1",
RandSeed: randSeed,
StaticAddress: netip.AddrFrom4([4]byte{10, 0, 0, byte1}),
MaxTCPConns: 1,
MaxActiveTCPPorts: 1,
HardwareAddress: [6]byte{0xbe, 0xef, 0, 0, 0, byte1},
MTU: uint16(mtu),
})
@@ -191,7 +191,7 @@ func newTCPStacks(t testing.TB, randSeed int64, mtu int) (s1, s2 *StackAsync, c1
Hostname: "Stack2",
RandSeed: ^randSeed,
StaticAddress: netip.AddrFrom4([4]byte{10, 0, 0, byte2}),
MaxTCPConns: 1,
MaxActiveTCPPorts: 1,
HardwareAddress: [6]byte{0xbe, 0xef, 0, 0, 0, byte2},
MTU: uint16(mtu),
})