conn: inch BatchSize toward being non-dynamic

There's not really a use at the moment for making this configurable, and
once bind_windows.go behaves like bind_std.go, we'll be able to use
constants everywhere. So begin that simplification now.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld 2023-03-04 15:25:46 +01:00
parent f26efb65f2
commit dbd949307e
8 changed files with 19 additions and 23 deletions

View File

@ -31,21 +31,13 @@ type StdNetBind struct {
blackhole6 bool blackhole6 bool
ipv4PC *ipv4.PacketConn ipv4PC *ipv4.PacketConn
ipv6PC *ipv6.PacketConn ipv6PC *ipv6.PacketConn
batchSize int
udpAddrPool sync.Pool udpAddrPool sync.Pool
ipv4MsgsPool sync.Pool ipv4MsgsPool sync.Pool
ipv6MsgsPool sync.Pool ipv6MsgsPool sync.Pool
} }
func NewStdNetBind() Bind { return NewStdNetBindBatch(DefaultBatchSize) } func NewStdNetBind() Bind {
func NewStdNetBindBatch(maxBatchSize int) Bind {
if maxBatchSize == 0 {
maxBatchSize = DefaultBatchSize
}
return &StdNetBind{ return &StdNetBind{
batchSize: maxBatchSize,
udpAddrPool: sync.Pool{ udpAddrPool: sync.Pool{
New: func() any { New: func() any {
return &net.UDPAddr{ return &net.UDPAddr{
@ -56,7 +48,7 @@ func NewStdNetBindBatch(maxBatchSize int) Bind {
ipv4MsgsPool: sync.Pool{ ipv4MsgsPool: sync.Pool{
New: func() any { New: func() any {
msgs := make([]ipv4.Message, maxBatchSize) msgs := make([]ipv4.Message, IdealBatchSize)
for i := range msgs { for i := range msgs {
msgs[i].Buffers = make(net.Buffers, 1) msgs[i].Buffers = make(net.Buffers, 1)
msgs[i].OOB = make([]byte, srcControlSize) msgs[i].OOB = make([]byte, srcControlSize)
@ -67,7 +59,7 @@ func NewStdNetBindBatch(maxBatchSize int) Bind {
ipv6MsgsPool: sync.Pool{ ipv6MsgsPool: sync.Pool{
New: func() any { New: func() any {
msgs := make([]ipv6.Message, maxBatchSize) msgs := make([]ipv6.Message, IdealBatchSize)
for i := range msgs { for i := range msgs {
msgs[i].Buffers = make(net.Buffers, 1) msgs[i].Buffers = make(net.Buffers, 1)
msgs[i].OOB = make([]byte, srcControlSize) msgs[i].OOB = make([]byte, srcControlSize)
@ -240,8 +232,10 @@ func (s *StdNetBind) receiveIPv6(buffs [][]byte, sizes []int, eps []Endpoint) (n
return numMsgs, nil return numMsgs, nil
} }
// TODO: When all Binds handle IdealBatchSize, remove this dynamic function and
// rename the IdealBatchSize constant to BatchSize.
func (s *StdNetBind) BatchSize() int { func (s *StdNetBind) BatchSize() int {
return s.batchSize return IdealBatchSize
} }
func (s *StdNetBind) Close() error { func (s *StdNetBind) Close() error {

View File

@ -321,6 +321,8 @@ func (bind *WinRingBind) Close() error {
return nil return nil
} }
// TODO: When all Binds handle IdealBatchSize, remove this dynamic function and
// rename the IdealBatchSize constant to BatchSize.
func (bind *WinRingBind) BatchSize() int { func (bind *WinRingBind) BatchSize() int {
// TODO: implement batching in and out of the ring // TODO: implement batching in and out of the ring
return 1 return 1

View File

@ -16,7 +16,7 @@ import (
) )
const ( const (
DefaultBatchSize = 128 // maximum number of packets handled per read and write IdealBatchSize = 128 // maximum number of packets handled per read and write
) )
// A ReceiveFunc receives at least one packet from the network and writes them // A ReceiveFunc receives at least one packet from the network and writes them

View File

@ -10,7 +10,7 @@ import "golang.zx2c4.com/wireguard/conn"
/* Reduce memory consumption for Android */ /* Reduce memory consumption for Android */
const ( const (
QueueStagedSize = conn.DefaultBatchSize QueueStagedSize = conn.IdealBatchSize
QueueOutboundSize = 1024 QueueOutboundSize = 1024
QueueInboundSize = 1024 QueueInboundSize = 1024
QueueHandshakeSize = 1024 QueueHandshakeSize = 1024

View File

@ -10,7 +10,7 @@ package device
import "golang.zx2c4.com/wireguard/conn" import "golang.zx2c4.com/wireguard/conn"
const ( const (
QueueStagedSize = conn.DefaultBatchSize QueueStagedSize = conn.IdealBatchSize
QueueOutboundSize = 1024 QueueOutboundSize = 1024
QueueInboundSize = 1024 QueueInboundSize = 1024
QueueHandshakeSize = 1024 QueueHandshakeSize = 1024

View File

@ -72,11 +72,11 @@ type tcpGROTable struct {
func newTCPGROTable() *tcpGROTable { func newTCPGROTable() *tcpGROTable {
t := &tcpGROTable{ t := &tcpGROTable{
itemsByFlow: make(map[flowKey][]tcpGROItem, conn.DefaultBatchSize), itemsByFlow: make(map[flowKey][]tcpGROItem, conn.IdealBatchSize),
itemsPool: make([][]tcpGROItem, conn.DefaultBatchSize), itemsPool: make([][]tcpGROItem, conn.IdealBatchSize),
} }
for i := range t.itemsPool { for i := range t.itemsPool {
t.itemsPool[i] = make([]tcpGROItem, 0, conn.DefaultBatchSize) t.itemsPool[i] = make([]tcpGROItem, 0, conn.IdealBatchSize)
} }
return t return t
} }

View File

@ -125,8 +125,8 @@ func Test_handleVirtioRead(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
out := make([][]byte, conn.DefaultBatchSize) out := make([][]byte, conn.IdealBatchSize)
sizes := make([]int, conn.DefaultBatchSize) sizes := make([]int, conn.IdealBatchSize)
for i := range out { for i := range out {
out[i] = make([]byte, 65535) out[i] = make([]byte, 65535)
} }

View File

@ -524,7 +524,7 @@ func (tun *NativeTun) initFromFlags(name string) error {
return return
} }
tun.vnetHdr = true tun.vnetHdr = true
tun.batchSize = conn.DefaultBatchSize tun.batchSize = conn.IdealBatchSize
} else { } else {
tun.batchSize = 1 tun.batchSize = 1
} }
@ -577,7 +577,7 @@ func CreateTUNFromFile(file *os.File, mtu int) (Device, error) {
statusListenersShutdown: make(chan struct{}), statusListenersShutdown: make(chan struct{}),
tcp4GROTable: newTCPGROTable(), tcp4GROTable: newTCPGROTable(),
tcp6GROTable: newTCPGROTable(), tcp6GROTable: newTCPGROTable(),
toWrite: make([]int, 0, conn.DefaultBatchSize), toWrite: make([]int, 0, conn.IdealBatchSize),
} }
name, err := tun.Name() name, err := tun.Name()
@ -633,7 +633,7 @@ func CreateUnmonitoredTUNFromFD(fd int) (Device, string, error) {
errors: make(chan error, 5), errors: make(chan error, 5),
tcp4GROTable: newTCPGROTable(), tcp4GROTable: newTCPGROTable(),
tcp6GROTable: newTCPGROTable(), tcp6GROTable: newTCPGROTable(),
toWrite: make([]int, 0, conn.DefaultBatchSize), toWrite: make([]int, 0, conn.IdealBatchSize),
} }
name, err := tun.Name() name, err := tun.Name()
if err != nil { if err != nil {