| 1 |
//go:build e2e_testing |
| 2 |
// +build e2e_testing |
| 3 |
|
| 4 |
package udp |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"io" |
| 9 |
"log/slog" |
| 10 |
"net/netip" |
| 11 |
"os" |
| 12 |
"sync" |
| 13 |
"sync/atomic" |
| 14 |
|
| 15 |
"github.com/slackhq/nebula/config" |
| 16 |
"github.com/slackhq/nebula/header" |
| 17 |
) |
| 18 |
|
| 19 |
type Packet struct { |
| 20 |
To netip.AddrPort |
| 21 |
From netip.AddrPort |
| 22 |
Data []byte |
| 23 |
} |
| 24 |
|
| 25 |
// Copy returns a fresh *Packet (from the freelist) with a duplicate Data buffer. |
| 26 |
func (u *Packet) Copy() *Packet { |
| 27 |
n := acquirePacket() |
| 28 |
n.To = u.To |
| 29 |
n.From = u.From |
| 30 |
if cap(n.Data) < len(u.Data) { |
| 31 |
n.Data = make([]byte, len(u.Data)) |
| 32 |
} else { |
| 33 |
n.Data = n.Data[:len(u.Data)] |
| 34 |
} |
| 35 |
copy(n.Data, u.Data) |
| 36 |
return n |
| 37 |
} |
| 38 |
|
| 39 |
// Release returns p to the harness packet freelist. |
| 40 |
// Callers that pull a *Packet from Get / TxPackets must Release when done. |
| 41 |
// Channel-backed instead of sync.Pool because sync.Pool's per-P caches drain badly under cross-goroutine Get/Put, |
| 42 |
// and putting a []byte in a Pool escapes the slice header to heap. |
| 43 |
func (p *Packet) Release() { |
| 44 |
if p == nil { |
| 45 |
return |
| 46 |
} |
| 47 |
p.Data = p.Data[:0] |
| 48 |
select { |
| 49 |
case packetFreelist <- p: |
| 50 |
default: |
| 51 |
// Freelist full; drop the *Packet for the GC. |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
// packetFreelist retains *Packet structs (and their backing Data arrays) so steady-state allocation drops to zero. |
| 56 |
var packetFreelist = make(chan *Packet, 64) |
| 57 |
|
| 58 |
func acquirePacket() *Packet { |
| 59 |
select { |
| 60 |
case p := <-packetFreelist: |
| 61 |
return p |
| 62 |
default: |
| 63 |
return &Packet{} |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
type TesterConn struct { |
| 68 |
// addr is read by nebula's own goroutines on every send and by the router's flow renderer, and a test can |
| 69 |
// move it mid-run to simulate roaming, so it is atomic rather than a plain field. |
| 70 |
addr atomic.Pointer[netip.AddrPort] |
| 71 |
|
| 72 |
RxPackets chan *Packet // Packets to receive into nebula |
| 73 |
TxPackets chan *Packet // Packets transmitted outside by nebula |
| 74 |
|
| 75 |
// done is closed exactly once by Close. Senders select on it so they |
| 76 |
// never race with a channel close; readers exit when it fires. The |
| 77 |
// packet channels are intentionally never closed - that was the source |
| 78 |
// of `send on closed channel` panics when a WriteTo/Send from another |
| 79 |
// goroutine passed the close check and reached the send just after |
| 80 |
// Close ran. |
| 81 |
done chan struct{} |
| 82 |
closeOnce sync.Once |
| 83 |
|
| 84 |
l *slog.Logger |
| 85 |
} |
| 86 |
|
| 87 |
func NewListener(l *slog.Logger, ip netip.Addr, port int, _ bool, _ int) (Conn, error) { |
| 88 |
c := &TesterConn{ |
| 89 |
RxPackets: make(chan *Packet, 10), |
| 90 |
TxPackets: make(chan *Packet, 10), |
| 91 |
done: make(chan struct{}), |
| 92 |
l: l, |
| 93 |
} |
| 94 |
c.SetAddr(netip.AddrPortFrom(ip, uint16(port))) |
| 95 |
return c, nil |
| 96 |
} |
| 97 |
|
| 98 |
// GetAddr returns the underlay address this conn currently sends from. |
| 99 |
func (u *TesterConn) GetAddr() netip.AddrPort { |
| 100 |
return *u.addr.Load() |
| 101 |
} |
| 102 |
|
| 103 |
// SetAddr moves this conn to a new underlay address, standing in for a host waking up on a different network. |
| 104 |
func (u *TesterConn) SetAddr(addr netip.AddrPort) { |
| 105 |
u.addr.Store(&addr) |
| 106 |
} |
| 107 |
|
| 108 |
// Send will place a UdpPacket onto the receive queue for nebula to consume |
| 109 |
// this is an encrypted packet or a handshake message in most cases |
| 110 |
// packets were transmitted from another nebula node, you can send them with Tun.Send |
| 111 |
func (u *TesterConn) Send(packet *Packet) { |
| 112 |
if u.l.Enabled(context.Background(), slog.LevelDebug) { |
| 113 |
// Parse the header only under debug logging, otherwise the |
| 114 |
// allocation would show up in every Send call. |
| 115 |
var h header.H |
| 116 |
if err := h.Parse(packet.Data); err != nil { |
| 117 |
panic(err) |
| 118 |
} |
| 119 |
u.l.Debug("UDP receiving injected packet", |
| 120 |
"header", &h, |
| 121 |
"udpAddr", packet.From, |
| 122 |
"dataLen", len(packet.Data), |
| 123 |
) |
| 124 |
} |
| 125 |
select { |
| 126 |
case <-u.done: |
| 127 |
case u.RxPackets <- packet: |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
// Get will pull a UdpPacket from the transmit queue |
| 132 |
// nebula meant to send this message on the network, it will be encrypted |
| 133 |
// packets were ingested from the tun side (in most cases), you can send them with Tun.Send |
| 134 |
func (u *TesterConn) Get(block bool) *Packet { |
| 135 |
if block { |
| 136 |
select { |
| 137 |
case <-u.done: |
| 138 |
return nil |
| 139 |
case p := <-u.TxPackets: |
| 140 |
return p |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
select { |
| 145 |
case p := <-u.TxPackets: |
| 146 |
return p |
| 147 |
default: |
| 148 |
return nil |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
//********************************************************************************************************************// |
| 153 |
// Below this is boilerplate implementation to make nebula actually work |
| 154 |
//********************************************************************************************************************// |
| 155 |
|
| 156 |
func (u *TesterConn) WriteTo(b []byte, addr netip.AddrPort) error { |
| 157 |
p := acquirePacket() |
| 158 |
if cap(p.Data) < len(b) { |
| 159 |
p.Data = make([]byte, len(b)) |
| 160 |
} else { |
| 161 |
p.Data = p.Data[:len(b)] |
| 162 |
} |
| 163 |
copy(p.Data, b) |
| 164 |
p.From = u.GetAddr() |
| 165 |
p.To = addr |
| 166 |
select { |
| 167 |
case <-u.done: |
| 168 |
p.Release() |
| 169 |
return io.ErrClosedPipe |
| 170 |
case u.TxPackets <- p: |
| 171 |
return nil |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
func (u *TesterConn) ListenOut(r EncReader) error { |
| 176 |
for { |
| 177 |
select { |
| 178 |
case <-u.done: |
| 179 |
return os.ErrClosed |
| 180 |
case p := <-u.RxPackets: |
| 181 |
r(p.From, p.Data) |
| 182 |
p.Release() |
| 183 |
} |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
func (u *TesterConn) ReloadConfig(*config.C) {} |
| 188 |
|
| 189 |
func NewUDPStatsEmitter(_ []Conn) func() { |
| 190 |
// No UDP stats for non-linux |
| 191 |
return func() {} |
| 192 |
} |
| 193 |
|
| 194 |
func (u *TesterConn) LocalAddr() (netip.AddrPort, error) { |
| 195 |
return u.GetAddr(), nil |
| 196 |
} |
| 197 |
|
| 198 |
func (u *TesterConn) SupportsMultipleReaders() bool { |
| 199 |
return false |
| 200 |
} |
| 201 |
|
| 202 |
func (u *TesterConn) Rebind() error { |
| 203 |
return nil |
| 204 |
} |
| 205 |
|
| 206 |
func (u *TesterConn) Close() error { |
| 207 |
u.closeOnce.Do(func() { |
| 208 |
close(u.done) |
| 209 |
}) |
| 210 |
return nil |
| 211 |
} |