Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3bbf5f4

Browse files
authoredJun 14, 2023
Use an interface for udp conns (#901)
1 parent 928731a commit 3bbf5f4

17 files changed

+95
-68
lines changed
 

‎connection_manager_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ func Test_NewConnectionManagerTest(t *testing.T) {
5454
ifce := &Interface{
5555
hostMap: hostMap,
5656
inside: &test.NoopTun{},
57-
outside: &udp.Conn{},
57+
outside: &udp.NoopConn{},
5858
firewall: &Firewall{},
5959
lightHouse: lh,
60-
handshakeManager: NewHandshakeManager(l, vpncidr, preferredRanges, hostMap, lh, &udp.Conn{}, defaultHandshakeConfig),
60+
handshakeManager: NewHandshakeManager(l, vpncidr, preferredRanges, hostMap, lh, &udp.NoopConn{}, defaultHandshakeConfig),
6161
l: l,
6262
}
6363
ifce.certState.Store(cs)
@@ -133,10 +133,10 @@ func Test_NewConnectionManagerTest2(t *testing.T) {
133133
ifce := &Interface{
134134
hostMap: hostMap,
135135
inside: &test.NoopTun{},
136-
outside: &udp.Conn{},
136+
outside: &udp.NoopConn{},
137137
firewall: &Firewall{},
138138
lightHouse: lh,
139-
handshakeManager: NewHandshakeManager(l, vpncidr, preferredRanges, hostMap, lh, &udp.Conn{}, defaultHandshakeConfig),
139+
handshakeManager: NewHandshakeManager(l, vpncidr, preferredRanges, hostMap, lh, &udp.NoopConn{}, defaultHandshakeConfig),
140140
l: l,
141141
}
142142
ifce.certState.Store(cs)
@@ -252,10 +252,10 @@ func Test_NewConnectionManagerTest_DisconnectInvalid(t *testing.T) {
252252
ifce := &Interface{
253253
hostMap: hostMap,
254254
inside: &test.NoopTun{},
255-
outside: &udp.Conn{},
255+
outside: &udp.NoopConn{},
256256
firewall: &Firewall{},
257257
lightHouse: lh,
258-
handshakeManager: NewHandshakeManager(l, vpncidr, preferredRanges, hostMap, lh, &udp.Conn{}, defaultHandshakeConfig),
258+
handshakeManager: NewHandshakeManager(l, vpncidr, preferredRanges, hostMap, lh, &udp.NoopConn{}, defaultHandshakeConfig),
259259
l: l,
260260
disconnectInvalid: true,
261261
caPool: ncp,

‎control_tester.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
func (c *Control) WaitForType(msgType header.MessageType, subType header.MessageSubType, pipeTo *Control) {
2222
h := &header.H{}
2323
for {
24-
p := c.f.outside.Get(true)
24+
p := c.f.outside.(*udp.TesterConn).Get(true)
2525
if err := h.Parse(p.Data); err != nil {
2626
panic(err)
2727
}
@@ -37,7 +37,7 @@ func (c *Control) WaitForType(msgType header.MessageType, subType header.Message
3737
func (c *Control) WaitForTypeByIndex(toIndex uint32, msgType header.MessageType, subType header.MessageSubType, pipeTo *Control) {
3838
h := &header.H{}
3939
for {
40-
p := c.f.outside.Get(true)
40+
p := c.f.outside.(*udp.TesterConn).Get(true)
4141
if err := h.Parse(p.Data); err != nil {
4242
panic(err)
4343
}
@@ -90,11 +90,11 @@ func (c *Control) GetFromTun(block bool) []byte {
9090

9191
// GetFromUDP will pull a udp packet off the udp side of nebula
9292
func (c *Control) GetFromUDP(block bool) *udp.Packet {
93-
return c.f.outside.Get(block)
93+
return c.f.outside.(*udp.TesterConn).Get(block)
9494
}
9595

9696
func (c *Control) GetUDPTxChan() <-chan *udp.Packet {
97-
return c.f.outside.TxPackets
97+
return c.f.outside.(*udp.TesterConn).TxPackets
9898
}
9999

100100
func (c *Control) GetTunTxChan() <-chan []byte {
@@ -103,7 +103,7 @@ func (c *Control) GetTunTxChan() <-chan []byte {
103103

104104
// InjectUDPPacket will inject a packet into the udp side of nebula
105105
func (c *Control) InjectUDPPacket(p *udp.Packet) {
106-
c.f.outside.Send(p)
106+
c.f.outside.(*udp.TesterConn).Send(p)
107107
}
108108

109109
// InjectTunUDPPacket puts a udp packet on the tun interface. Using UDP here because it's a simpler protocol
@@ -143,7 +143,7 @@ func (c *Control) GetVpnIp() iputil.VpnIp {
143143
}
144144

145145
func (c *Control) GetUDPAddr() string {
146-
return c.f.outside.Addr.String()
146+
return c.f.outside.(*udp.TesterConn).Addr.String()
147147
}
148148

149149
func (c *Control) KillPendingTunnel(vpnIp net.IP) bool {

‎handshake_manager.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ type HandshakeManager struct {
4545
pendingHostMap *HostMap
4646
mainHostMap *HostMap
4747
lightHouse *LightHouse
48-
outside *udp.Conn
48+
outside udp.Conn
4949
config HandshakeConfig
5050
OutboundHandshakeTimer *LockingTimerWheel[iputil.VpnIp]
5151
messageMetrics *MessageMetrics
@@ -57,7 +57,7 @@ type HandshakeManager struct {
5757
trigger chan iputil.VpnIp
5858
}
5959

60-
func NewHandshakeManager(l *logrus.Logger, tunCidr *net.IPNet, preferredRanges []*net.IPNet, mainHostMap *HostMap, lightHouse *LightHouse, outside *udp.Conn, config HandshakeConfig) *HandshakeManager {
60+
func NewHandshakeManager(l *logrus.Logger, tunCidr *net.IPNet, preferredRanges []*net.IPNet, mainHostMap *HostMap, lightHouse *LightHouse, outside udp.Conn, config HandshakeConfig) *HandshakeManager {
6161
return &HandshakeManager{
6262
pendingHostMap: NewHostMap(l, "pending", tunCidr, preferredRanges),
6363
mainHostMap: mainHostMap,

‎handshake_manager_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func Test_NewHandshakeManagerVpnIp(t *testing.T) {
2323
mainHM := NewHostMap(l, "test", vpncidr, preferredRanges)
2424
lh := newTestLighthouse()
2525

26-
blah := NewHandshakeManager(l, tuncidr, preferredRanges, mainHM, lh, &udp.Conn{}, defaultHandshakeConfig)
26+
blah := NewHandshakeManager(l, tuncidr, preferredRanges, mainHM, lh, &udp.NoopConn{}, defaultHandshakeConfig)
2727

2828
now := time.Now()
2929
blah.NextOutboundHandshakeTimerTick(now, mw)

‎interface.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const mtu = 9001
2626

2727
type InterfaceConfig struct {
2828
HostMap *HostMap
29-
Outside *udp.Conn
29+
Outside udp.Conn
3030
Inside overlay.Device
3131
certState *CertState
3232
Cipher string
@@ -52,7 +52,7 @@ type InterfaceConfig struct {
5252

5353
type Interface struct {
5454
hostMap *HostMap
55-
outside *udp.Conn
55+
outside udp.Conn
5656
inside overlay.Device
5757
certState atomic.Pointer[CertState]
5858
cipher string
@@ -80,7 +80,7 @@ type Interface struct {
8080

8181
conntrackCacheTimeout time.Duration
8282

83-
writers []*udp.Conn
83+
writers []udp.Conn
8484
readers []io.ReadWriteCloser
8585

8686
metricHandshakes metrics.Histogram
@@ -167,7 +167,7 @@ func NewInterface(ctx context.Context, c *InterfaceConfig) (*Interface, error) {
167167
dropMulticast: c.DropMulticast,
168168
routines: c.routines,
169169
version: c.version,
170-
writers: make([]*udp.Conn, c.routines),
170+
writers: make([]udp.Conn, c.routines),
171171
readers: make([]io.ReadWriteCloser, c.routines),
172172
caPool: c.caPool,
173173
disconnectInvalid: c.disconnectInvalid,
@@ -243,7 +243,7 @@ func (f *Interface) run() {
243243
func (f *Interface) listenOut(i int) {
244244
runtime.LockOSThread()
245245

246-
var li *udp.Conn
246+
var li udp.Conn
247247
// TODO clean this up with a coherent interface for each outside connection
248248
if i > 0 {
249249
li = f.writers[i]

‎lighthouse.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ type LightHouse struct {
3939
myVpnIp iputil.VpnIp
4040
myVpnZeros iputil.VpnIp
4141
myVpnNet *net.IPNet
42-
punchConn *udp.Conn
42+
punchConn udp.Conn
4343
punchy *Punchy
4444

4545
// Local cache of answers from light houses
@@ -84,7 +84,7 @@ type LightHouse struct {
8484

8585
// NewLightHouseFromConfig will build a Lighthouse struct from the values provided in the config object
8686
// addrMap should be nil unless this is during a config reload
87-
func NewLightHouseFromConfig(ctx context.Context, l *logrus.Logger, c *config.C, myVpnNet *net.IPNet, pc *udp.Conn, p *Punchy) (*LightHouse, error) {
87+
func NewLightHouseFromConfig(ctx context.Context, l *logrus.Logger, c *config.C, myVpnNet *net.IPNet, pc udp.Conn, p *Punchy) (*LightHouse, error) {
8888
amLighthouse := c.GetBool("lighthouse.am_lighthouse", false)
8989
nebulaPort := uint32(c.GetInt("listen.port", 0))
9090
if amLighthouse && nebulaPort == 0 {

‎main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ func Main(c *config.C, configTest bool, buildVersion string, logger *logrus.Logg
147147
}
148148

149149
// set up our UDP listener
150-
udpConns := make([]*udp.Conn, routines)
150+
udpConns := make([]udp.Conn, routines)
151151
port := c.GetInt("listen.port", 0)
152152

153153
if !configTest {

‎udp/conn.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package udp
22

33
import (
4+
"github.com/slackhq/nebula/config"
45
"github.com/slackhq/nebula/firewall"
56
"github.com/slackhq/nebula/header"
67
)
@@ -18,3 +19,29 @@ type EncReader func(
1819
q int,
1920
localCache firewall.ConntrackCache,
2021
)
22+
23+
type Conn interface {
24+
Rebind() error
25+
LocalAddr() (*Addr, error)
26+
ListenOut(r EncReader, lhf LightHouseHandlerFunc, cache *firewall.ConntrackCacheTicker, q int)
27+
WriteTo(b []byte, addr *Addr) error
28+
ReloadConfig(c *config.C)
29+
}
30+
31+
type NoopConn struct{}
32+
33+
func (NoopConn) Rebind() error {
34+
return nil
35+
}
36+
func (NoopConn) LocalAddr() (*Addr, error) {
37+
return nil, nil
38+
}
39+
func (NoopConn) ListenOut(_ EncReader, _ LightHouseHandlerFunc, _ *firewall.ConntrackCacheTicker, _ int) {
40+
return
41+
}
42+
func (NoopConn) WriteTo(_ []byte, _ *Addr) error {
43+
return nil
44+
}
45+
func (NoopConn) ReloadConfig(_ *config.C) {
46+
return
47+
}

‎udp/udp_android.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@ func NewListenConfig(multi bool) net.ListenConfig {
3434
}
3535
}
3636

37-
func (u *Conn) Rebind() error {
37+
func (u *GenericConn) Rebind() error {
3838
return nil
3939
}

‎udp/udp_darwin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func NewListenConfig(multi bool) net.ListenConfig {
3737
}
3838
}
3939

40-
func (u *Conn) Rebind() error {
40+
func (u *GenericConn) Rebind() error {
4141
file, err := u.File()
4242
if err != nil {
4343
return err

‎udp/udp_freebsd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,6 @@ func NewListenConfig(multi bool) net.ListenConfig {
3636
}
3737
}
3838

39-
func (u *Conn) Rebind() error {
39+
func (u *GenericConn) Rebind() error {
4040
return nil
4141
}

‎udp/udp_generic.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,30 @@ import (
1818
"github.com/slackhq/nebula/header"
1919
)
2020

21-
type Conn struct {
21+
type GenericConn struct {
2222
*net.UDPConn
2323
l *logrus.Logger
2424
}
2525

26-
func NewListener(l *logrus.Logger, ip net.IP, port int, multi bool, batch int) (*Conn, error) {
26+
func NewListener(l *logrus.Logger, ip net.IP, port int, multi bool, batch int) (Conn, error) {
2727
lc := NewListenConfig(multi)
2828
pc, err := lc.ListenPacket(context.TODO(), "udp", net.JoinHostPort(ip.String(), fmt.Sprintf("%v", port)))
2929
if err != nil {
3030
return nil, err
3131
}
3232
if uc, ok := pc.(*net.UDPConn); ok {
33-
return &Conn{UDPConn: uc, l: l}, nil
33+
return &GenericConn{UDPConn: uc, l: l}, nil
3434
}
3535
return nil, fmt.Errorf("Unexpected PacketConn: %T %#v", pc, pc)
3636
}
3737

38-
func (uc *Conn) WriteTo(b []byte, addr *Addr) error {
39-
_, err := uc.UDPConn.WriteToUDP(b, &net.UDPAddr{IP: addr.IP, Port: int(addr.Port)})
38+
func (u *GenericConn) WriteTo(b []byte, addr *Addr) error {
39+
_, err := u.UDPConn.WriteToUDP(b, &net.UDPAddr{IP: addr.IP, Port: int(addr.Port)})
4040
return err
4141
}
4242

43-
func (uc *Conn) LocalAddr() (*Addr, error) {
44-
a := uc.UDPConn.LocalAddr()
43+
func (u *GenericConn) LocalAddr() (*Addr, error) {
44+
a := u.UDPConn.LocalAddr()
4545

4646
switch v := a.(type) {
4747
case *net.UDPAddr:
@@ -55,11 +55,11 @@ func (uc *Conn) LocalAddr() (*Addr, error) {
5555
}
5656
}
5757

58-
func (u *Conn) ReloadConfig(c *config.C) {
58+
func (u *GenericConn) ReloadConfig(c *config.C) {
5959
// TODO
6060
}
6161

62-
func NewUDPStatsEmitter(udpConns []*Conn) func() {
62+
func NewUDPStatsEmitter(udpConns []Conn) func() {
6363
// No UDP stats for non-linux
6464
return func() {}
6565
}
@@ -68,7 +68,7 @@ type rawMessage struct {
6868
Len uint32
6969
}
7070

71-
func (u *Conn) ListenOut(r EncReader, lhf LightHouseHandlerFunc, cache *firewall.ConntrackCacheTicker, q int) {
71+
func (u *GenericConn) ListenOut(r EncReader, lhf LightHouseHandlerFunc, cache *firewall.ConntrackCacheTicker, q int) {
7272
plaintext := make([]byte, MTU)
7373
buffer := make([]byte, MTU)
7474
h := &header.H{}

0 commit comments

Comments
 (0)
Please sign in to comment.