device.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package overlay
  2. import (
  3. "io"
  4. "net/netip"
  5. "github.com/slackhq/nebula/routing"
  6. )
  7. type Device interface {
  8. io.ReadWriteCloser
  9. Activate() error
  10. Networks() []netip.Prefix
  11. Name() string
  12. RoutesFor(netip.Addr) routing.Gateways
  13. SupportsMultiqueue() bool
  14. NewMultiQueueReader() (io.ReadWriteCloser, error)
  15. }
  16. // BatchReader is an optional interface that devices can implement
  17. // to support reading multiple packets in a single batch operation.
  18. // This can significantly reduce syscall overhead under high load.
  19. type BatchReader interface {
  20. // ReadBatch reads up to len(packets) packets into the provided buffers.
  21. // Each packet is read into packets[i] and its length is stored in sizes[i].
  22. // Returns the number of packets read, or an error.
  23. // A return of (0, nil) indicates no packets were available (non-blocking).
  24. ReadBatch(packets [][]byte, sizes []int) (int, error)
  25. }
  26. // AsBatchReader returns a BatchReader if the reader supports batch operations,
  27. // otherwise returns nil.
  28. func AsBatchReader(r io.ReadWriteCloser) BatchReader {
  29. if br, ok := r.(BatchReader); ok {
  30. return br
  31. }
  32. return nil
  33. }
  34. // BatchEnabler is an optional interface for devices that need explicit
  35. // enabling of batch read support (e.g., setting non-blocking mode).
  36. type BatchEnabler interface {
  37. EnableBatchReading() error
  38. }
  39. // EnableBatchReading enables batch reading on the device if supported.
  40. // Returns nil if the device doesn't support or need explicit enabling.
  41. func EnableBatchReading(d interface{}) error {
  42. if be, ok := d.(BatchEnabler); ok {
  43. return be.EnableBatchReading()
  44. }
  45. return nil
  46. }