conn.go 2.4 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. The packets alias the receive buffers.
  26. // After each batch is fully delivered (after each packet on single-packet backends), handoff takes the batch's
  27. // buffers and returns the ones to receive the next batch into: the same ones once the caller is done with the
  28. // packets, after flushing per-batch accumulators such as TUN write coalescers, or replacements of the same count
  29. // and size to keep them. handoff must not be nil.
  30. ListenOut(r EncReader, handoff func(bufs [][]byte) [][]byte) error
  31. WriteTo(b []byte, addr netip.AddrPort) error
  32. // WriteBatch sends a contiguous batch of packets, each with its own
  33. // destination. bufs and addrs must have the same length. Linux uses
  34. // sendmmsg(2) for a single syscall.
  35. //
  36. // Returns the number of packets successfully written. A destination the kernel rejects costs only
  37. // its own packet, so a short count means some peers were undeliverable, not that the batch failed.
  38. // Not safe for concurrent use on the same Conn.
  39. WriteBatch(bufs [][]byte, addrs []netip.AddrPort) (int, error)
  40. ReloadConfig(c *config.C)
  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([][]byte) [][]byte) 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) {
  64. return
  65. }
  66. func (NoopConn) Close() error {
  67. return nil
  68. }