conn.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 additional sendmmsg chunks within a single WriteBatch call.
  11. const MaxWriteBatch = 128
  12. // RxMeta carries per-packet metadata extracted from the RX path (ancillary
  13. // data, kernel offload state, etc.) and passed to EncReader callbacks.
  14. // Backends that do not produce a particular signal leave its zero value.
  15. //
  16. // OuterECN is the 2-bit IP-level ECN codepoint stamped on the carrier
  17. // datagram (extracted from IP_TOS / IPV6_TCLASS cmsg on Linux). Zero
  18. // means Not-ECT, which is also the value backends without ECN RX support
  19. // supply on every packet.
  20. type RxMeta struct {
  21. OuterECN byte
  22. }
  23. type EncReader func(
  24. addr netip.AddrPort,
  25. payload []byte,
  26. meta RxMeta,
  27. )
  28. type Conn interface {
  29. Rebind() error
  30. LocalAddr() (netip.AddrPort, error)
  31. // ListenOut invokes r for each received packet. On batch-capable
  32. // backends (recvmmsg), flush is called after each batch is fully
  33. // delivered — callers use it to flush per-batch accumulators such as
  34. // TUN write coalescers. Single-packet backends call flush after each
  35. // packet. flush must not be nil.
  36. ListenOut(r EncReader, flush func()) error
  37. WriteTo(b []byte, addr netip.AddrPort) error
  38. // WriteBatch sends a contiguous batch of packets, each with its own
  39. // destination. bufs and addrs must have the same length. outerECNs may
  40. // be nil (treated as all-zero / Not-ECT); when non-nil it must have the
  41. // same length as bufs, and outerECNs[i] is the 2-bit IP-level ECN
  42. // codepoint to set on packet i's outer header. Linux uses sendmmsg(2)
  43. // for a single syscall and attaches the value as IP_TOS / IPV6_TCLASS
  44. // cmsg; other backends ignore it. Returns on the first error; callers
  45. // may observe a partial send if some packets went out before the error.
  46. WriteBatch(bufs [][]byte, addrs []netip.AddrPort, outerECNs []byte) error
  47. ReloadConfig(c *config.C)
  48. SupportsMultipleReaders() bool
  49. Close() error
  50. }
  51. type NoopConn struct{}
  52. func (NoopConn) Rebind() error {
  53. return nil
  54. }
  55. func (NoopConn) LocalAddr() (netip.AddrPort, error) {
  56. return netip.AddrPort{}, nil
  57. }
  58. func (NoopConn) ListenOut(_ EncReader, _ func()) error {
  59. return nil
  60. }
  61. func (NoopConn) SupportsMultipleReaders() bool {
  62. return false
  63. }
  64. func (NoopConn) WriteTo(_ []byte, _ netip.AddrPort) error {
  65. return nil
  66. }
  67. func (NoopConn) WriteBatch(_ [][]byte, _ []netip.AddrPort, _ []byte) error {
  68. return nil
  69. }
  70. func (NoopConn) ReloadConfig(_ *config.C) {
  71. return
  72. }
  73. func (NoopConn) Close() error {
  74. return nil
  75. }