Skip to content

Commit dbd9493

Browse files
committed
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 <[email protected]>
1 parent f26efb6 commit dbd9493

File tree

8 files changed

+19
-23
lines changed

8 files changed

+19
-23
lines changed

conn/bind_std.go

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,13 @@ type StdNetBind struct {
3131
blackhole6 bool
3232
ipv4PC *ipv4.PacketConn
3333
ipv6PC *ipv6.PacketConn
34-
batchSize int
3534
udpAddrPool sync.Pool
3635
ipv4MsgsPool sync.Pool
3736
ipv6MsgsPool sync.Pool
3837
}
3938

40-
func NewStdNetBind() Bind { return NewStdNetBindBatch(DefaultBatchSize) }
41-
42-
func NewStdNetBindBatch(maxBatchSize int) Bind {
43-
if maxBatchSize == 0 {
44-
maxBatchSize = DefaultBatchSize
45-
}
39+
func NewStdNetBind() Bind {
4640
return &StdNetBind{
47-
batchSize: maxBatchSize,
48-
4941
udpAddrPool: sync.Pool{
5042
New: func() any {
5143
return &net.UDPAddr{
@@ -56,7 +48,7 @@ func NewStdNetBindBatch(maxBatchSize int) Bind {
5648

5749
ipv4MsgsPool: sync.Pool{
5850
New: func() any {
59-
msgs := make([]ipv4.Message, maxBatchSize)
51+
msgs := make([]ipv4.Message, IdealBatchSize)
6052
for i := range msgs {
6153
msgs[i].Buffers = make(net.Buffers, 1)
6254
msgs[i].OOB = make([]byte, srcControlSize)
@@ -67,7 +59,7 @@ func NewStdNetBindBatch(maxBatchSize int) Bind {
6759

6860
ipv6MsgsPool: sync.Pool{
6961
New: func() any {
70-
msgs := make([]ipv6.Message, maxBatchSize)
62+
msgs := make([]ipv6.Message, IdealBatchSize)
7163
for i := range msgs {
7264
msgs[i].Buffers = make(net.Buffers, 1)
7365
msgs[i].OOB = make([]byte, srcControlSize)
@@ -240,8 +232,10 @@ func (s *StdNetBind) receiveIPv6(buffs [][]byte, sizes []int, eps []Endpoint) (n
240232
return numMsgs, nil
241233
}
242234

235+
// TODO: When all Binds handle IdealBatchSize, remove this dynamic function and
236+
// rename the IdealBatchSize constant to BatchSize.
243237
func (s *StdNetBind) BatchSize() int {
244-
return s.batchSize
238+
return IdealBatchSize
245239
}
246240

247241
func (s *StdNetBind) Close() error {

conn/bind_windows.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,8 @@ func (bind *WinRingBind) Close() error {
321321
return nil
322322
}
323323

324+
// TODO: When all Binds handle IdealBatchSize, remove this dynamic function and
325+
// rename the IdealBatchSize constant to BatchSize.
324326
func (bind *WinRingBind) BatchSize() int {
325327
// TODO: implement batching in and out of the ring
326328
return 1

conn/conn.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
)
1717

1818
const (
19-
DefaultBatchSize = 128 // maximum number of packets handled per read and write
19+
IdealBatchSize = 128 // maximum number of packets handled per read and write
2020
)
2121

2222
// A ReceiveFunc receives at least one packet from the network and writes them

device/queueconstants_android.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import "golang.zx2c4.com/wireguard/conn"
1010
/* Reduce memory consumption for Android */
1111

1212
const (
13-
QueueStagedSize = conn.DefaultBatchSize
13+
QueueStagedSize = conn.IdealBatchSize
1414
QueueOutboundSize = 1024
1515
QueueInboundSize = 1024
1616
QueueHandshakeSize = 1024

device/queueconstants_default.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ package device
1010
import "golang.zx2c4.com/wireguard/conn"
1111

1212
const (
13-
QueueStagedSize = conn.DefaultBatchSize
13+
QueueStagedSize = conn.IdealBatchSize
1414
QueueOutboundSize = 1024
1515
QueueInboundSize = 1024
1616
QueueHandshakeSize = 1024

tun/tcp_offload_linux.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,11 @@ type tcpGROTable struct {
7272

7373
func newTCPGROTable() *tcpGROTable {
7474
t := &tcpGROTable{
75-
itemsByFlow: make(map[flowKey][]tcpGROItem, conn.DefaultBatchSize),
76-
itemsPool: make([][]tcpGROItem, conn.DefaultBatchSize),
75+
itemsByFlow: make(map[flowKey][]tcpGROItem, conn.IdealBatchSize),
76+
itemsPool: make([][]tcpGROItem, conn.IdealBatchSize),
7777
}
7878
for i := range t.itemsPool {
79-
t.itemsPool[i] = make([]tcpGROItem, 0, conn.DefaultBatchSize)
79+
t.itemsPool[i] = make([]tcpGROItem, 0, conn.IdealBatchSize)
8080
}
8181
return t
8282
}

tun/tcp_offload_linux_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ func Test_handleVirtioRead(t *testing.T) {
125125

126126
for _, tt := range tests {
127127
t.Run(tt.name, func(t *testing.T) {
128-
out := make([][]byte, conn.DefaultBatchSize)
129-
sizes := make([]int, conn.DefaultBatchSize)
128+
out := make([][]byte, conn.IdealBatchSize)
129+
sizes := make([]int, conn.IdealBatchSize)
130130
for i := range out {
131131
out[i] = make([]byte, 65535)
132132
}

tun/tun_linux.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ func (tun *NativeTun) initFromFlags(name string) error {
524524
return
525525
}
526526
tun.vnetHdr = true
527-
tun.batchSize = conn.DefaultBatchSize
527+
tun.batchSize = conn.IdealBatchSize
528528
} else {
529529
tun.batchSize = 1
530530
}
@@ -577,7 +577,7 @@ func CreateTUNFromFile(file *os.File, mtu int) (Device, error) {
577577
statusListenersShutdown: make(chan struct{}),
578578
tcp4GROTable: newTCPGROTable(),
579579
tcp6GROTable: newTCPGROTable(),
580-
toWrite: make([]int, 0, conn.DefaultBatchSize),
580+
toWrite: make([]int, 0, conn.IdealBatchSize),
581581
}
582582

583583
name, err := tun.Name()
@@ -633,7 +633,7 @@ func CreateUnmonitoredTUNFromFD(fd int) (Device, string, error) {
633633
errors: make(chan error, 5),
634634
tcp4GROTable: newTCPGROTable(),
635635
tcp6GROTable: newTCPGROTable(),
636-
toWrite: make([]int, 0, conn.DefaultBatchSize),
636+
toWrite: make([]int, 0, conn.IdealBatchSize),
637637
}
638638
name, err := tun.Name()
639639
if err != nil {

0 commit comments

Comments
 (0)