tun_tester.go 4.4 KB

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