conn.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 applies the listen settings that can change, r is nil at startup
  40. ReloadConfig(c *config.C, r *config.Reload)
  41. SupportsMultipleReaders() bool
  42. Close() error
  43. }
  44. type NoopConn struct{}
  45. func (NoopConn) Rebind() error {
  46. return nil
  47. }
  48. func (NoopConn) LocalAddr() (netip.AddrPort, error) {
  49. return netip.AddrPort{}, nil
  50. }
  51. func (NoopConn) ListenOut(_ EncReader, _ func()) error {
  52. return nil
  53. }
  54. func (NoopConn) SupportsMultipleReaders() bool {
  55. return false
  56. }
  57. func (NoopConn) WriteTo(_ []byte, _ netip.AddrPort) error {
  58. return nil
  59. }
  60. func (NoopConn) WriteBatch(bufs [][]byte, _ []netip.AddrPort) (int, error) {
  61. return len(bufs), nil
  62. }
  63. func (NoopConn) ReloadConfig(_ *config.C, _ *config.Reload) {
  64. return
  65. }
  66. func (NoopConn) Close() error {
  67. return nil
  68. }