wgengine/magicsock: introduce virtualNetworkID type (#16021)

This type improves code clarity and reduces the chance of heap alloc as
we pass it as a non-pointer. VNI being a 3-byte value enables us to
track set vs unset via the reserved/unused byte.

Updates tailscale/corp#27502

Signed-off-by: Jordan Whited <jordan@tailscale.com>
This commit is contained in:
Jordan Whited
2025-05-19 19:14:08 -07:00
committed by GitHub
parent 30a89ad378
commit 3cc80cce6a
4 changed files with 100 additions and 14 deletions
+50
View File
@@ -12,6 +12,7 @@ import (
"errors"
"fmt"
"io"
"math"
"math/rand"
"net"
"net/http"
@@ -3317,3 +3318,52 @@ func Test_isDiscoMaybeGeneve(t *testing.T) {
})
}
}
func Test_virtualNetworkID(t *testing.T) {
tests := []struct {
name string
set *uint32
want uint32
}{
{
"don't set",
nil,
0,
},
{
"set 0",
ptr.To(uint32(0)),
0,
},
{
"set 1",
ptr.To(uint32(1)),
1,
},
{
"set math.MaxUint32",
ptr.To(uint32(math.MaxUint32)),
1<<24 - 1,
},
{
"set max 3-byte value",
ptr.To(uint32(1<<24 - 1)),
1<<24 - 1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
v := virtualNetworkID{}
if tt.set != nil {
v.set(*tt.set)
}
if v.isSet() != (tt.set != nil) {
t.Fatalf("isSet: %v != wantIsSet: %v", v.isSet(), tt.set != nil)
}
if v.get() != tt.want {
t.Fatalf("get(): %v != want: %v", v.get(), tt.want)
}
})
}
}