udp_tester.go 5.2 KB

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