conn.go 2.6 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. type EncReader func(
  13. addr netip.AddrPort,
  14. payload []byte,
  15. )
  16. type Settings struct {
  17. Listen netip.AddrPort
  18. Multi bool
  19. Batch int
  20. Offloads bool
  21. }
  22. type Conn interface {
  23. Rebind() error
  24. LocalAddr() (netip.AddrPort, error)
  25. // ListenOut invokes r for each received packet.
  26. // On batch-capable backends (recvmmsg), flush is called after each batch is fully delivered.
  27. // Callers use it to flush per-batch accumulators such as TUN write coalescers.
  28. // Single-packet backends call flush after each packet. flush must not be nil.
  29. ListenOut(r EncReader, flush func()) error
  30. WriteTo(b []byte, addr netip.AddrPort) error
  31. // WriteBatch sends a contiguous batch of packets, each with its own
  32. // destination. bufs and addrs must have the same length. Linux uses
  33. // sendmmsg(2) for a single syscall.
  34. //
  35. // Returns the number of packets successfully written. A destination the kernel rejects costs only
  36. // its own packet, so a short count means some peers were undeliverable, not that the batch failed.
  37. // Not safe for concurrent use on the same Conn.
  38. WriteBatch(bufs [][]byte, addrs []netip.AddrPort) (int, error)
  39. ReloadConfig(c *config.C)
  40. SupportsMultipleReaders() bool
  41. Close() error
  42. }
  43. // EstablishedPeerConn is a Conn that treats the remotes of established tunnels differently from other
  44. // destinations, as darwin's gives busy ones connected sockets.
  45. type EstablishedPeerConn interface {
  46. // SetEstablishedPeer reports whether dst is the current remote of at least one established tunnel. Calls for
  47. // one dst must not race each other.
  48. SetEstablishedPeer(dst netip.AddrPort, established bool)
  49. }
  50. type NoopConn struct{}
  51. func (NoopConn) Rebind() error {
  52. return nil
  53. }
  54. func (NoopConn) LocalAddr() (netip.AddrPort, error) {
  55. return netip.AddrPort{}, nil
  56. }
  57. func (NoopConn) ListenOut(_ EncReader, _ func()) error {
  58. return nil
  59. }
  60. func (NoopConn) SupportsMultipleReaders() bool {
  61. return false
  62. }
  63. func (NoopConn) WriteTo(_ []byte, _ netip.AddrPort) error {
  64. return nil
  65. }
  66. func (NoopConn) WriteBatch(bufs [][]byte, _ []netip.AddrPort) (int, error) {
  67. return len(bufs), nil
  68. }
  69. func (NoopConn) ReloadConfig(_ *config.C) {
  70. return
  71. }
  72. func (NoopConn) Close() error {
  73. return nil
  74. }