| 1 |
//go:build e2e_testing |
| 2 |
// +build e2e_testing |
| 3 |
|
| 4 |
package overlay |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"fmt" |
| 9 |
"io" |
| 10 |
"log/slog" |
| 11 |
"net/netip" |
| 12 |
"os" |
| 13 |
"sync/atomic" |
| 14 |
|
| 15 |
"github.com/gaissmai/bart" |
| 16 |
"github.com/slackhq/nebula/config" |
| 17 |
"github.com/slackhq/nebula/routing" |
| 18 |
"github.com/slackhq/nebula/udp" |
| 19 |
) |
| 20 |
|
| 21 |
type TestTun struct { |
| 22 |
Device string |
| 23 |
vpnNetworks []netip.Prefix |
| 24 |
Routes []Route |
| 25 |
routeTree *bart.Table[routing.Gateways] |
| 26 |
l *slog.Logger |
| 27 |
|
| 28 |
closed atomic.Bool |
| 29 |
rxPackets chan []byte // Packets to receive into nebula |
| 30 |
TxPackets chan []byte // Packets transmitted outside by nebula |
| 31 |
} |
| 32 |
|
| 33 |
func newTun(c *config.C, l *slog.Logger, vpnNetworks []netip.Prefix, _ bool) (*TestTun, error) { |
| 34 |
_, routes, err := getAllRoutesFromConfig(c, vpnNetworks, true) |
| 35 |
if err != nil { |
| 36 |
return nil, err |
| 37 |
} |
| 38 |
routeTree, err := makeRouteTree(l, routes, false) |
| 39 |
if err != nil { |
| 40 |
return nil, err |
| 41 |
} |
| 42 |
|
| 43 |
return &TestTun{ |
| 44 |
Device: c.GetString("tun.dev", ""), |
| 45 |
vpnNetworks: vpnNetworks, |
| 46 |
Routes: routes, |
| 47 |
routeTree: routeTree, |
| 48 |
l: l, |
| 49 |
rxPackets: make(chan []byte, 10), |
| 50 |
TxPackets: make(chan []byte, 10), |
| 51 |
}, nil |
| 52 |
} |
| 53 |
|
| 54 |
func newTunFromFd(_ *config.C, _ *slog.Logger, _ int, _ []netip.Prefix) (*TestTun, error) { |
| 55 |
return nil, fmt.Errorf("newTunFromFd not supported") |
| 56 |
} |
| 57 |
|
| 58 |
// Send will place a byte array onto the receive queue for nebula to consume. |
| 59 |
// These are unencrypted ip layer frames destined for another nebula node. |
| 60 |
// packets should exit the udp side, capture them with udpConn.Get. |
| 61 |
// |
| 62 |
// Send copies the input via the freelist, so the caller is free to mutate |
| 63 |
// or reuse it after the call returns. |
| 64 |
func (t *TestTun) Send(packet []byte) { |
| 65 |
if t.closed.Load() { |
| 66 |
return |
| 67 |
} |
| 68 |
|
| 69 |
if t.l.Enabled(context.Background(), slog.LevelDebug) { |
| 70 |
t.l.Debug("Tun receiving injected packet", "dataLen", len(packet)) |
| 71 |
} |
| 72 |
buf := acquireTunBuf(len(packet)) |
| 73 |
copy(buf, packet) |
| 74 |
t.rxPackets <- buf |
| 75 |
} |
| 76 |
|
| 77 |
// Get will pull an unencrypted ip layer frame from the transmit queue |
| 78 |
// nebula meant to send this message to some application on the local system |
| 79 |
// packets were ingested from the udp side, you can send them with udpConn.Send |
| 80 |
func (t *TestTun) Get(block bool) []byte { |
| 81 |
if block { |
| 82 |
return <-t.TxPackets |
| 83 |
} |
| 84 |
|
| 85 |
select { |
| 86 |
case p := <-t.TxPackets: |
| 87 |
return p |
| 88 |
default: |
| 89 |
return nil |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
//********************************************************************************************************************// |
| 94 |
// Below this is boilerplate implementation to make nebula actually work |
| 95 |
//********************************************************************************************************************// |
| 96 |
|
| 97 |
func (t *TestTun) RoutesFor(ip netip.Addr) routing.Gateways { |
| 98 |
r, _ := t.routeTree.Lookup(ip) |
| 99 |
return r |
| 100 |
} |
| 101 |
|
| 102 |
func (t *TestTun) Activate() error { |
| 103 |
return nil |
| 104 |
} |
| 105 |
|
| 106 |
func (t *TestTun) Networks() []netip.Prefix { |
| 107 |
return t.vpnNetworks |
| 108 |
} |
| 109 |
|
| 110 |
func (t *TestTun) Name() string { |
| 111 |
return t.Device |
| 112 |
} |
| 113 |
|
| 114 |
func (t *TestTun) Write(b []byte) (n int, err error) { |
| 115 |
if t.closed.Load() { |
| 116 |
return 0, io.ErrClosedPipe |
| 117 |
} |
| 118 |
|
| 119 |
packet := acquireTunBuf(len(b)) |
| 120 |
copy(packet, b) |
| 121 |
t.TxPackets <- packet |
| 122 |
return len(b), nil |
| 123 |
} |
| 124 |
|
| 125 |
// ReleaseTunBuf returns a slice from TxPackets to the harness freelist, don't use the bytes after the call. |
| 126 |
// Channel-backed instead of sync.Pool because putting a []byte in a sync.Pool escapes the slice header to heap. |
| 127 |
func ReleaseTunBuf(b []byte) { |
| 128 |
if b == nil { |
| 129 |
return |
| 130 |
} |
| 131 |
select { |
| 132 |
case tunBufFreelist <- b: |
| 133 |
default: |
| 134 |
// Freelist full; drop the buffer for the GC. |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
// tunBufFreelist retains the backing arrays for TestTun.Write so steady-state allocation drops to zero once the |
| 139 |
// freelist has saturated for the current MTU. |
| 140 |
var tunBufFreelist = make(chan []byte, 64) |
| 141 |
|
| 142 |
func acquireTunBuf(n int) []byte { |
| 143 |
var b []byte |
| 144 |
select { |
| 145 |
case b = <-tunBufFreelist: |
| 146 |
default: |
| 147 |
b = make([]byte, 0, udp.MTU) |
| 148 |
} |
| 149 |
if cap(b) < n { |
| 150 |
b = make([]byte, n) |
| 151 |
} else { |
| 152 |
b = b[:n] |
| 153 |
} |
| 154 |
return b |
| 155 |
} |
| 156 |
|
| 157 |
func (t *TestTun) Close() error { |
| 158 |
if t.closed.CompareAndSwap(false, true) { |
| 159 |
close(t.rxPackets) |
| 160 |
close(t.TxPackets) |
| 161 |
} |
| 162 |
return nil |
| 163 |
} |
| 164 |
|
| 165 |
func (t *TestTun) Read(b []byte) (int, error) { |
| 166 |
p, ok := <-t.rxPackets |
| 167 |
if !ok { |
| 168 |
return 0, os.ErrClosed |
| 169 |
} |
| 170 |
n := len(p) |
| 171 |
copy(b, p) |
| 172 |
// Send always pushes a freelist-acquired slice, return it once we've copied the bytes into the caller's buffer. |
| 173 |
select { |
| 174 |
case tunBufFreelist <- p: |
| 175 |
default: |
| 176 |
} |
| 177 |
return n, nil |
| 178 |
} |
| 179 |
|
| 180 |
func (t *TestTun) SupportsMultiqueue() bool { |
| 181 |
return false |
| 182 |
} |
| 183 |
|
| 184 |
func (t *TestTun) NewMultiQueueReader() (io.ReadWriteCloser, error) { |
| 185 |
return nil, fmt.Errorf("TODO: multiqueue not implemented") |
| 186 |
} |