stack_gvisor_endpoint.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package tun
  2. import (
  3. "context"
  4. "errors"
  5. "gvisor.dev/gvisor/pkg/tcpip"
  6. "gvisor.dev/gvisor/pkg/tcpip/header"
  7. "gvisor.dev/gvisor/pkg/tcpip/stack"
  8. )
  9. var ErrQueueEmpty = errors.New("queue is empty")
  10. type GVisorDevice interface {
  11. WritePacket(packet *stack.PacketBuffer) tcpip.Error
  12. ReadPacket() (byte, *stack.PacketBuffer, error)
  13. Wait()
  14. }
  15. // LinkEndpoint implements GVisor stack.LinkEndpoint
  16. var _ stack.LinkEndpoint = (*LinkEndpoint)(nil)
  17. type LinkEndpoint struct {
  18. deviceMTU uint32
  19. device GVisorDevice
  20. dispatcherCancel context.CancelFunc
  21. }
  22. func (e *LinkEndpoint) MTU() uint32 {
  23. return e.deviceMTU
  24. }
  25. func (e *LinkEndpoint) SetMTU(_ uint32) {
  26. // not Implemented, as it is not expected GVisor will be asking tun device to be modified
  27. }
  28. func (e *LinkEndpoint) MaxHeaderLength() uint16 {
  29. return 0
  30. }
  31. func (e *LinkEndpoint) LinkAddress() tcpip.LinkAddress {
  32. return ""
  33. }
  34. func (e *LinkEndpoint) SetLinkAddress(_ tcpip.LinkAddress) {
  35. // not Implemented, as it is not expected GVisor will be asking tun device to be modified
  36. }
  37. func (e *LinkEndpoint) Capabilities() stack.LinkEndpointCapabilities {
  38. return stack.CapabilityRXChecksumOffload
  39. }
  40. func (e *LinkEndpoint) Attach(dispatcher stack.NetworkDispatcher) {
  41. if e.dispatcherCancel != nil {
  42. e.dispatcherCancel()
  43. e.dispatcherCancel = nil
  44. }
  45. if dispatcher != nil {
  46. ctx, cancel := context.WithCancel(context.Background())
  47. go e.dispatchLoop(ctx, dispatcher)
  48. e.dispatcherCancel = cancel
  49. }
  50. }
  51. func (e *LinkEndpoint) IsAttached() bool {
  52. return e.dispatcherCancel != nil
  53. }
  54. func (e *LinkEndpoint) Wait() {
  55. }
  56. func (e *LinkEndpoint) ARPHardwareType() header.ARPHardwareType {
  57. return header.ARPHardwareNone
  58. }
  59. func (e *LinkEndpoint) AddHeader(buffer *stack.PacketBuffer) {
  60. // tun interface doesn't have link layer header, it will be added by the OS
  61. }
  62. func (e *LinkEndpoint) ParseHeader(ptr *stack.PacketBuffer) bool {
  63. return true
  64. }
  65. func (e *LinkEndpoint) Close() {
  66. if e.dispatcherCancel != nil {
  67. e.dispatcherCancel()
  68. e.dispatcherCancel = nil
  69. }
  70. }
  71. func (e *LinkEndpoint) SetOnCloseAction(_ func()) {
  72. }
  73. func (e *LinkEndpoint) WritePackets(packetBufferList stack.PacketBufferList) (int, tcpip.Error) {
  74. var n int
  75. var err tcpip.Error
  76. for _, packetBuffer := range packetBufferList.AsSlice() {
  77. err = e.device.WritePacket(packetBuffer)
  78. if err != nil {
  79. return n, &tcpip.ErrAborted{}
  80. }
  81. n++
  82. }
  83. return n, nil
  84. }
  85. func (e *LinkEndpoint) dispatchLoop(ctx context.Context, dispatcher stack.NetworkDispatcher) {
  86. var networkProtocolNumber tcpip.NetworkProtocolNumber
  87. var version byte
  88. var packet *stack.PacketBuffer
  89. var err error
  90. for {
  91. select {
  92. case <-ctx.Done():
  93. return
  94. default:
  95. version, packet, err = e.device.ReadPacket()
  96. // on "queue empty", ask device to yield slightly and continue
  97. if errors.Is(err, ErrQueueEmpty) {
  98. e.device.Wait()
  99. continue
  100. }
  101. // stop dispatcher loop on any other interface failure
  102. if err != nil {
  103. e.Attach(nil)
  104. return
  105. }
  106. // extract network protocol number from the packet first byte
  107. // (which is returned separately, since it is so incredibly hard to extract one byte from
  108. // stack.PacketBuffer without additional memory allocation and full copying it back and forth)
  109. switch version {
  110. case 4:
  111. networkProtocolNumber = header.IPv4ProtocolNumber
  112. case 6:
  113. networkProtocolNumber = header.IPv6ProtocolNumber
  114. default:
  115. // discard unknown network protocol packet
  116. packet.DecRef()
  117. continue
  118. }
  119. // dispatch the buffer to the stack
  120. dispatcher.DeliverNetworkPacket(networkProtocolNumber, packet)
  121. // signal the buffer that it can be released
  122. packet.DecRef()
  123. }
  124. }
  125. }