1
0

conn.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. type NoopConn struct{}
  44. func (NoopConn) Rebind() error {
  45. return nil
  46. }
  47. func (NoopConn) LocalAddr() (netip.AddrPort, error) {
  48. return netip.AddrPort{}, nil
  49. }
  50. func (NoopConn) ListenOut(_ EncReader, _ func()) error {
  51. return nil
  52. }
  53. func (NoopConn) SupportsMultipleReaders() bool {
  54. return false
  55. }
  56. func (NoopConn) WriteTo(_ []byte, _ netip.AddrPort) error {
  57. return nil
  58. }
  59. func (NoopConn) WriteBatch(bufs [][]byte, _ []netip.AddrPort) (int, error) {
  60. return len(bufs), nil
  61. }
  62. func (NoopConn) ReloadConfig(_ *config.C) {
  63. return
  64. }
  65. func (NoopConn) Close() error {
  66. return nil
  67. }