conn.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package udp
  2. import (
  3. "net/netip"
  4. "github.com/slackhq/nebula/config"
  5. )
  6. const MTU = 9001
  7. // MaxWriteBatch is the largest batch any Conn.WriteBatch implementation is
  8. // required to accept. Callers SHOULD NOT pass more than this per call; Linux
  9. // backends preallocate sendmmsg scratch sized to this value, so exceeding it
  10. // only costs a chunked retry.
  11. const MaxWriteBatch = 128
  12. type EncReader func(
  13. addr netip.AddrPort,
  14. payload []byte,
  15. )
  16. type Conn interface {
  17. Rebind() error
  18. LocalAddr() (netip.AddrPort, error)
  19. // ListenOut invokes r for each received packet. On batch-capable
  20. // backends (recvmmsg), flush is called after each batch is fully
  21. // delivered — callers use it to flush per-batch accumulators such as
  22. // TUN write coalescers. Single-packet backends call flush after each
  23. // packet. flush must not be nil.
  24. ListenOut(r EncReader, flush func()) error
  25. WriteTo(b []byte, addr netip.AddrPort) error
  26. // WriteBatch sends a contiguous batch of packets, each with its own
  27. // destination. bufs and addrs must have the same length. Linux uses
  28. // sendmmsg(2) for a single syscall; other backends fall back to a
  29. // WriteTo loop. Returns on the first error; callers may observe a
  30. // partial send if some packets went out before the error.
  31. WriteBatch(bufs [][]byte, addrs []netip.AddrPort) error
  32. ReloadConfig(c *config.C)
  33. SupportsMultipleReaders() bool
  34. Close() error
  35. }
  36. type NoopConn struct{}
  37. func (NoopConn) Rebind() error {
  38. return nil
  39. }
  40. func (NoopConn) LocalAddr() (netip.AddrPort, error) {
  41. return netip.AddrPort{}, nil
  42. }
  43. func (NoopConn) ListenOut(_ EncReader, _ func()) error {
  44. return nil
  45. }
  46. func (NoopConn) SupportsMultipleReaders() bool {
  47. return false
  48. }
  49. func (NoopConn) WriteTo(_ []byte, _ netip.AddrPort) error {
  50. return nil
  51. }
  52. func (NoopConn) WriteBatch(_ [][]byte, _ []netip.AddrPort) error {
  53. return nil
  54. }
  55. func (NoopConn) ReloadConfig(_ *config.C) {
  56. return
  57. }
  58. func (NoopConn) Close() error {
  59. return nil
  60. }