tun_tester.go 4.8 KB

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