tun_linux_batch.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. //go:build linux && !android && !e2e_testing
  2. package overlay
  3. import "fmt"
  4. func (t *tun) batchIO() (*wireguardTunIO, bool) {
  5. io, ok := t.ReadWriteCloser.(*wireguardTunIO)
  6. return io, ok
  7. }
  8. func (t *tun) ReadIntoBatch(pool *PacketPool) ([]*Packet, error) {
  9. io, ok := t.batchIO()
  10. if !ok {
  11. return nil, fmt.Errorf("wireguard batch I/O not enabled")
  12. }
  13. return io.ReadIntoBatch(pool)
  14. }
  15. func (t *tun) WriteBatch(packets []*Packet) (int, error) {
  16. io, ok := t.batchIO()
  17. if ok {
  18. return io.WriteBatch(packets)
  19. }
  20. for _, pkt := range packets {
  21. if pkt == nil {
  22. continue
  23. }
  24. if _, err := t.Write(pkt.Payload()[:pkt.Len]); err != nil {
  25. return 0, err
  26. }
  27. pkt.Release()
  28. }
  29. return len(packets), nil
  30. }
  31. func (t *tun) BatchHeadroom() int {
  32. if io, ok := t.batchIO(); ok {
  33. return io.BatchHeadroom()
  34. }
  35. return 0
  36. }
  37. func (t *tun) BatchPayloadCap() int {
  38. if io, ok := t.batchIO(); ok {
  39. return io.BatchPayloadCap()
  40. }
  41. return 0
  42. }
  43. func (t *tun) BatchSize() int {
  44. if io, ok := t.batchIO(); ok {
  45. return io.BatchSize()
  46. }
  47. return 1
  48. }