]> git.ipfire.org Git - thirdparty/wireguard-go.git/commitdiff
conn: inch BatchSize toward being non-dynamic
authorJason A. Donenfeld <Jason@zx2c4.com>
Sat, 4 Mar 2023 14:25:46 +0000 (15:25 +0100)
committerJason A. Donenfeld <Jason@zx2c4.com>
Fri, 10 Mar 2023 13:52:22 +0000 (14:52 +0100)
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>
conn/bind_std.go
conn/bind_windows.go
conn/conn.go
device/queueconstants_android.go
device/queueconstants_default.go
tun/tcp_offload_linux.go
tun/tcp_offload_linux_test.go
tun/tun_linux.go

index a164f56511af3c7e93e2f69add060a46a65e4afd..b9da4c3afef702bc32964cf9a4e6a35be8e8e25f 100644 (file)
@@ -31,21 +31,13 @@ type StdNetBind struct {
        blackhole6   bool
        ipv4PC       *ipv4.PacketConn
        ipv6PC       *ipv6.PacketConn
-       batchSize    int
        udpAddrPool  sync.Pool
        ipv4MsgsPool sync.Pool
        ipv6MsgsPool sync.Pool
 }
 
-func NewStdNetBind() Bind { return NewStdNetBindBatch(DefaultBatchSize) }
-
-func NewStdNetBindBatch(maxBatchSize int) Bind {
-       if maxBatchSize == 0 {
-               maxBatchSize = DefaultBatchSize
-       }
+func NewStdNetBind() Bind {
        return &StdNetBind{
-               batchSize: maxBatchSize,
-
                udpAddrPool: sync.Pool{
                        New: func() any {
                                return &net.UDPAddr{
@@ -56,7 +48,7 @@ func NewStdNetBindBatch(maxBatchSize int) Bind {
 
                ipv4MsgsPool: sync.Pool{
                        New: func() any {
-                               msgs := make([]ipv4.Message, maxBatchSize)
+                               msgs := make([]ipv4.Message, IdealBatchSize)
                                for i := range msgs {
                                        msgs[i].Buffers = make(net.Buffers, 1)
                                        msgs[i].OOB = make([]byte, srcControlSize)
@@ -67,7 +59,7 @@ func NewStdNetBindBatch(maxBatchSize int) Bind {
 
                ipv6MsgsPool: sync.Pool{
                        New: func() any {
-                               msgs := make([]ipv6.Message, maxBatchSize)
+                               msgs := make([]ipv6.Message, IdealBatchSize)
                                for i := range msgs {
                                        msgs[i].Buffers = make(net.Buffers, 1)
                                        msgs[i].OOB = make([]byte, srcControlSize)
@@ -240,8 +232,10 @@ func (s *StdNetBind) receiveIPv6(buffs [][]byte, sizes []int, eps []Endpoint) (n
        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 {
-       return s.batchSize
+       return IdealBatchSize
 }
 
 func (s *StdNetBind) Close() error {
index 5a0b8c212a5cf14d961a6b785b695f569519e0ee..e44cc7bd3c19256bc539a76c595d327bdd186686 100644 (file)
@@ -321,6 +321,8 @@ func (bind *WinRingBind) Close() error {
        return nil
 }
 
+// TODO: When all Binds handle IdealBatchSize, remove this dynamic function and
+// rename the IdealBatchSize constant to BatchSize.
 func (bind *WinRingBind) BatchSize() int {
        // TODO: implement batching in and out of the ring
        return 1
index 9cbd0afbd963fc096c26e0b5800cb72e9384c869..a9c70b562fdda88d2c8ce87d0d95802cc378b956 100644 (file)
@@ -16,7 +16,7 @@ import (
 )
 
 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
index 115838757f7386d33a47422be6a81f72c4fccc63..3d80eadb0a054605f8a9ca6c50cf8960c616c8bd 100644 (file)
@@ -10,7 +10,7 @@ import "golang.zx2c4.com/wireguard/conn"
 /* Reduce memory consumption for Android */
 
 const (
-       QueueStagedSize            = conn.DefaultBatchSize
+       QueueStagedSize            = conn.IdealBatchSize
        QueueOutboundSize          = 1024
        QueueInboundSize           = 1024
        QueueHandshakeSize         = 1024
index 7ed70a1fd3d5d22f44c53f360cf4613a543fca95..ea763d01c7c6bd60bc1a197c400b5c8b50e73c35 100644 (file)
@@ -10,7 +10,7 @@ package device
 import "golang.zx2c4.com/wireguard/conn"
 
 const (
-       QueueStagedSize            = conn.DefaultBatchSize
+       QueueStagedSize            = conn.IdealBatchSize
        QueueOutboundSize          = 1024
        QueueInboundSize           = 1024
        QueueHandshakeSize         = 1024
index f3ffa757ed52c5d820bea17606d3ca3bcad13ccb..a040e6f5deab11abf61f0feb3cbde50c9793b4ad 100644 (file)
@@ -72,11 +72,11 @@ type tcpGROTable struct {
 
 func newTCPGROTable() *tcpGROTable {
        t := &tcpGROTable{
-               itemsByFlow: make(map[flowKey][]tcpGROItem, conn.DefaultBatchSize),
-               itemsPool:   make([][]tcpGROItem, conn.DefaultBatchSize),
+               itemsByFlow: make(map[flowKey][]tcpGROItem, conn.IdealBatchSize),
+               itemsPool:   make([][]tcpGROItem, conn.IdealBatchSize),
        }
        for i := range t.itemsPool {
-               t.itemsPool[i] = make([]tcpGROItem, 0, conn.DefaultBatchSize)
+               t.itemsPool[i] = make([]tcpGROItem, 0, conn.IdealBatchSize)
        }
        return t
 }
index 7fa07778204acc048889252de839662c34c0eb87..11f9e53b51d7eb11fd62f683bfecf5e6925eb59b 100644 (file)
@@ -125,8 +125,8 @@ func Test_handleVirtioRead(t *testing.T) {
 
        for _, tt := range tests {
                t.Run(tt.name, func(t *testing.T) {
-                       out := make([][]byte, conn.DefaultBatchSize)
-                       sizes := make([]int, conn.DefaultBatchSize)
+                       out := make([][]byte, conn.IdealBatchSize)
+                       sizes := make([]int, conn.IdealBatchSize)
                        for i := range out {
                                out[i] = make([]byte, 65535)
                        }
index d56e3c18c2a246249668d3c5859216b2d390c790..88ed9792aefe7739884ad2a61209b6fa873591f0 100644 (file)
@@ -524,7 +524,7 @@ func (tun *NativeTun) initFromFlags(name string) error {
                                return
                        }
                        tun.vnetHdr = true
-                       tun.batchSize = conn.DefaultBatchSize
+                       tun.batchSize = conn.IdealBatchSize
                } else {
                        tun.batchSize = 1
                }
@@ -577,7 +577,7 @@ func CreateTUNFromFile(file *os.File, mtu int) (Device, error) {
                statusListenersShutdown: make(chan struct{}),
                tcp4GROTable:            newTCPGROTable(),
                tcp6GROTable:            newTCPGROTable(),
-               toWrite:                 make([]int, 0, conn.DefaultBatchSize),
+               toWrite:                 make([]int, 0, conn.IdealBatchSize),
        }
 
        name, err := tun.Name()
@@ -633,7 +633,7 @@ func CreateUnmonitoredTUNFromFD(fd int) (Device, string, error) {
                errors:       make(chan error, 5),
                tcp4GROTable: newTCPGROTable(),
                tcp6GROTable: newTCPGROTable(),
-               toWrite:      make([]int, 0, conn.DefaultBatchSize),
+               toWrite:      make([]int, 0, conn.IdealBatchSize),
        }
        name, err := tun.Name()
        if err != nil {