tun_linux_offload_test.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. //go:build !android && !e2e_testing
  2. // +build !android,!e2e_testing
  3. package overlay
  4. import (
  5. "encoding/binary"
  6. "testing"
  7. "golang.org/x/sys/unix"
  8. )
  9. // verifyChecksum confirms that the one's-complement sum across `b`, optionally
  10. // seeded with a pseudo-header sum, folds to all-ones (valid).
  11. func verifyChecksum(b []byte, pseudo uint32) bool {
  12. sum := checksumBytes(b, pseudo)
  13. for sum>>16 != 0 {
  14. sum = (sum & 0xffff) + (sum >> 16)
  15. }
  16. return uint16(sum) == 0xffff
  17. }
  18. // buildTSOv4 builds a synthetic IPv4/TCP TSO superpacket with a payload of
  19. // `payLen` bytes split at `mss`.
  20. func buildTSOv4(t *testing.T, payLen, mss int) ([]byte, virtioNetHdr) {
  21. t.Helper()
  22. const ipLen = 20
  23. const tcpLen = 20
  24. pkt := make([]byte, ipLen+tcpLen+payLen)
  25. // IPv4 header
  26. pkt[0] = 0x45 // version 4, IHL 5
  27. // total length is meaningless for TSO but set it anyway
  28. binary.BigEndian.PutUint16(pkt[2:4], uint16(ipLen+tcpLen+payLen))
  29. binary.BigEndian.PutUint16(pkt[4:6], 0x4242) // original ID
  30. pkt[8] = 64 // TTL
  31. pkt[9] = unix.IPPROTO_TCP
  32. copy(pkt[12:16], []byte{10, 0, 0, 1}) // src
  33. copy(pkt[16:20], []byte{10, 0, 0, 2}) // dst
  34. // TCP header
  35. binary.BigEndian.PutUint16(pkt[20:22], 12345) // sport
  36. binary.BigEndian.PutUint16(pkt[22:24], 80) // dport
  37. binary.BigEndian.PutUint32(pkt[24:28], 10000) // seq
  38. binary.BigEndian.PutUint32(pkt[28:32], 20000) // ack
  39. pkt[32] = 0x50 // data offset 5 words
  40. pkt[33] = 0x18 // ACK | PSH
  41. binary.BigEndian.PutUint16(pkt[34:36], 65535) // window
  42. // payload
  43. for i := 0; i < payLen; i++ {
  44. pkt[ipLen+tcpLen+i] = byte(i & 0xff)
  45. }
  46. return pkt, virtioNetHdr{
  47. Flags: unix.VIRTIO_NET_HDR_F_NEEDS_CSUM,
  48. GSOType: unix.VIRTIO_NET_HDR_GSO_TCPV4,
  49. HdrLen: uint16(ipLen + tcpLen),
  50. GSOSize: uint16(mss),
  51. CsumStart: uint16(ipLen),
  52. CsumOffset: 16,
  53. }
  54. }
  55. func TestSegmentTCPv4(t *testing.T) {
  56. const mss = 100
  57. const numSeg = 3
  58. pkt, hdr := buildTSOv4(t, mss*numSeg, mss)
  59. scratch := make([]byte, tunSegBufSize)
  60. var out [][]byte
  61. if err := segmentTCP(pkt, hdr, &out, scratch); err != nil {
  62. t.Fatalf("segmentTCP: %v", err)
  63. }
  64. if len(out) != numSeg {
  65. t.Fatalf("expected %d segments, got %d", numSeg, len(out))
  66. }
  67. for i, seg := range out {
  68. if len(seg) != 40+mss {
  69. t.Errorf("seg %d: unexpected len %d", i, len(seg))
  70. }
  71. totalLen := binary.BigEndian.Uint16(seg[2:4])
  72. if totalLen != uint16(40+mss) {
  73. t.Errorf("seg %d: total_len=%d want %d", i, totalLen, 40+mss)
  74. }
  75. id := binary.BigEndian.Uint16(seg[4:6])
  76. if id != 0x4242+uint16(i) {
  77. t.Errorf("seg %d: ip id=%#x want %#x", i, id, 0x4242+uint16(i))
  78. }
  79. seq := binary.BigEndian.Uint32(seg[24:28])
  80. wantSeq := uint32(10000 + i*mss)
  81. if seq != wantSeq {
  82. t.Errorf("seg %d: seq=%d want %d", i, seq, wantSeq)
  83. }
  84. flags := seg[33]
  85. wantFlags := byte(0x10) // ACK only, PSH cleared
  86. if i == numSeg-1 {
  87. wantFlags = 0x18 // ACK | PSH preserved on last
  88. }
  89. if flags != wantFlags {
  90. t.Errorf("seg %d: flags=%#x want %#x", i, flags, wantFlags)
  91. }
  92. // IPv4 header checksum must verify against itself.
  93. if !verifyChecksum(seg[:20], 0) {
  94. t.Errorf("seg %d: bad IPv4 header checksum", i)
  95. }
  96. // TCP checksum must verify against the pseudo-header.
  97. psum := pseudoHeaderIPv4(seg[12:16], seg[16:20], unix.IPPROTO_TCP, 20+mss)
  98. if !verifyChecksum(seg[20:], psum) {
  99. t.Errorf("seg %d: bad TCP checksum", i)
  100. }
  101. }
  102. }
  103. func TestSegmentTCPv4OddTail(t *testing.T) {
  104. // Payload of 250 bytes with MSS 100 → segments of 100, 100, 50.
  105. pkt, hdr := buildTSOv4(t, 250, 100)
  106. scratch := make([]byte, tunSegBufSize)
  107. var out [][]byte
  108. if err := segmentTCP(pkt, hdr, &out, scratch); err != nil {
  109. t.Fatalf("segmentTCP: %v", err)
  110. }
  111. if len(out) != 3 {
  112. t.Fatalf("want 3 segments, got %d", len(out))
  113. }
  114. wantPayLens := []int{100, 100, 50}
  115. for i, seg := range out {
  116. if len(seg)-40 != wantPayLens[i] {
  117. t.Errorf("seg %d: pay len %d want %d", i, len(seg)-40, wantPayLens[i])
  118. }
  119. if !verifyChecksum(seg[:20], 0) {
  120. t.Errorf("seg %d: bad IPv4 header checksum", i)
  121. }
  122. psum := pseudoHeaderIPv4(seg[12:16], seg[16:20], unix.IPPROTO_TCP, 20+wantPayLens[i])
  123. if !verifyChecksum(seg[20:], psum) {
  124. t.Errorf("seg %d: bad TCP checksum", i)
  125. }
  126. }
  127. }
  128. func TestSegmentTCPv6(t *testing.T) {
  129. const ipLen = 40
  130. const tcpLen = 20
  131. const mss = 120
  132. const numSeg = 2
  133. payLen := mss * numSeg
  134. pkt := make([]byte, ipLen+tcpLen+payLen)
  135. // IPv6 header
  136. pkt[0] = 0x60 // version 6
  137. binary.BigEndian.PutUint16(pkt[4:6], uint16(tcpLen+payLen))
  138. pkt[6] = unix.IPPROTO_TCP
  139. pkt[7] = 64
  140. // src/dst fe80::1 / fe80::2
  141. pkt[8] = 0xfe
  142. pkt[9] = 0x80
  143. pkt[23] = 1
  144. pkt[24] = 0xfe
  145. pkt[25] = 0x80
  146. pkt[39] = 2
  147. // TCP header
  148. binary.BigEndian.PutUint16(pkt[40:42], 12345)
  149. binary.BigEndian.PutUint16(pkt[42:44], 80)
  150. binary.BigEndian.PutUint32(pkt[44:48], 7)
  151. binary.BigEndian.PutUint32(pkt[48:52], 99)
  152. pkt[52] = 0x50
  153. pkt[53] = 0x19 // FIN | ACK | PSH — exercise FIN clearing too
  154. binary.BigEndian.PutUint16(pkt[54:56], 65535)
  155. for i := 0; i < payLen; i++ {
  156. pkt[ipLen+tcpLen+i] = byte(i)
  157. }
  158. hdr := virtioNetHdr{
  159. Flags: unix.VIRTIO_NET_HDR_F_NEEDS_CSUM,
  160. GSOType: unix.VIRTIO_NET_HDR_GSO_TCPV6,
  161. HdrLen: uint16(ipLen + tcpLen),
  162. GSOSize: uint16(mss),
  163. CsumStart: uint16(ipLen),
  164. CsumOffset: 16,
  165. }
  166. scratch := make([]byte, tunSegBufSize)
  167. var out [][]byte
  168. if err := segmentTCP(pkt, hdr, &out, scratch); err != nil {
  169. t.Fatalf("segmentTCP: %v", err)
  170. }
  171. if len(out) != numSeg {
  172. t.Fatalf("want %d segments, got %d", numSeg, len(out))
  173. }
  174. for i, seg := range out {
  175. if len(seg) != ipLen+tcpLen+mss {
  176. t.Errorf("seg %d: len %d want %d", i, len(seg), ipLen+tcpLen+mss)
  177. }
  178. pl := binary.BigEndian.Uint16(seg[4:6])
  179. if pl != uint16(tcpLen+mss) {
  180. t.Errorf("seg %d: payload_length=%d want %d", i, pl, tcpLen+mss)
  181. }
  182. seq := binary.BigEndian.Uint32(seg[44:48])
  183. if seq != uint32(7+i*mss) {
  184. t.Errorf("seg %d: seq=%d want %d", i, seq, 7+i*mss)
  185. }
  186. flags := seg[53]
  187. // Original flags = 0x19 (FIN|ACK|PSH). FIN(0x01)+PSH(0x08) should be
  188. // cleared on all but the last; ACK(0x10) always preserved.
  189. wantFlags := byte(0x10)
  190. if i == numSeg-1 {
  191. wantFlags = 0x19
  192. }
  193. if flags != wantFlags {
  194. t.Errorf("seg %d: flags=%#x want %#x", i, flags, wantFlags)
  195. }
  196. psum := pseudoHeaderIPv6(seg[8:24], seg[24:40], unix.IPPROTO_TCP, tcpLen+mss)
  197. if !verifyChecksum(seg[ipLen:], psum) {
  198. t.Errorf("seg %d: bad TCP checksum", i)
  199. }
  200. }
  201. }
  202. func TestSegmentGSONonePassesThrough(t *testing.T) {
  203. pkt, hdr := buildTSOv4(t, 100, 100)
  204. hdr.GSOType = unix.VIRTIO_NET_HDR_GSO_NONE
  205. hdr.Flags = 0 // no NEEDS_CSUM, leave packet untouched
  206. scratch := make([]byte, tunSegBufSize)
  207. var out [][]byte
  208. if err := segmentInto(pkt, hdr, &out, scratch); err != nil {
  209. t.Fatalf("segmentInto: %v", err)
  210. }
  211. if len(out) != 1 {
  212. t.Fatalf("want 1 segment, got %d", len(out))
  213. }
  214. if len(out[0]) != len(pkt) {
  215. t.Fatalf("unexpected length: %d vs %d", len(out[0]), len(pkt))
  216. }
  217. }
  218. func TestSegmentRejectsUDP(t *testing.T) {
  219. hdr := virtioNetHdr{GSOType: unix.VIRTIO_NET_HDR_GSO_UDP}
  220. var out [][]byte
  221. if err := segmentInto(nil, hdr, &out, nil); err == nil {
  222. t.Fatalf("expected rejection for UDP GSO")
  223. }
  224. }