readwrite_go1_8_test.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. // Copyright 2013 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // +build !go1.9
  5. package ipv6_test
  6. import (
  7. "bytes"
  8. "fmt"
  9. "net"
  10. "runtime"
  11. "strings"
  12. "sync"
  13. "testing"
  14. "golang.org/x/net/internal/iana"
  15. "golang.org/x/net/internal/nettest"
  16. "golang.org/x/net/ipv6"
  17. )
  18. func BenchmarkPacketConnReadWriteUnicast(b *testing.B) {
  19. switch runtime.GOOS {
  20. case "nacl", "plan9", "windows":
  21. b.Skipf("not supported on %s", runtime.GOOS)
  22. }
  23. payload := []byte("HELLO-R-U-THERE")
  24. iph := []byte{
  25. 0x69, 0x8b, 0xee, 0xf1, 0xca, 0xfe, 0xff, 0x01,
  26. 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x01, 0x00, 0x00,
  27. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  28. 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x02, 0x00, 0x00,
  29. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  30. }
  31. greh := []byte{0x00, 0x00, 0x86, 0xdd, 0x00, 0x00, 0x00, 0x00}
  32. datagram := append(greh, append(iph, payload...)...)
  33. bb := make([]byte, 128)
  34. cm := ipv6.ControlMessage{
  35. TrafficClass: iana.DiffServAF11 | iana.CongestionExperienced,
  36. HopLimit: 1,
  37. Src: net.IPv6loopback,
  38. }
  39. if ifi := nettest.RoutedInterface("ip6", net.FlagUp|net.FlagLoopback); ifi != nil {
  40. cm.IfIndex = ifi.Index
  41. }
  42. b.Run("UDP", func(b *testing.B) {
  43. c, err := nettest.NewLocalPacketListener("udp6")
  44. if err != nil {
  45. b.Skipf("not supported on %s/%s: %v", runtime.GOOS, runtime.GOARCH, err)
  46. }
  47. defer c.Close()
  48. p := ipv6.NewPacketConn(c)
  49. dst := c.LocalAddr()
  50. cf := ipv6.FlagHopLimit | ipv6.FlagInterface
  51. if err := p.SetControlMessage(cf, true); err != nil {
  52. b.Fatal(err)
  53. }
  54. b.Run("Net", func(b *testing.B) {
  55. for i := 0; i < b.N; i++ {
  56. if _, err := c.WriteTo(payload, dst); err != nil {
  57. b.Fatal(err)
  58. }
  59. if _, _, err := c.ReadFrom(bb); err != nil {
  60. b.Fatal(err)
  61. }
  62. }
  63. })
  64. b.Run("ToFrom", func(b *testing.B) {
  65. for i := 0; i < b.N; i++ {
  66. if _, err := p.WriteTo(payload, &cm, dst); err != nil {
  67. b.Fatal(err)
  68. }
  69. if _, _, _, err := p.ReadFrom(bb); err != nil {
  70. b.Fatal(err)
  71. }
  72. }
  73. })
  74. })
  75. b.Run("IP", func(b *testing.B) {
  76. switch runtime.GOOS {
  77. case "netbsd":
  78. b.Skip("need to configure gre on netbsd")
  79. case "openbsd":
  80. b.Skip("net.inet.gre.allow=0 by default on openbsd")
  81. }
  82. c, err := net.ListenPacket(fmt.Sprintf("ip6:%d", iana.ProtocolGRE), "::1")
  83. if err != nil {
  84. b.Skipf("not supported on %s/%s: %v", runtime.GOOS, runtime.GOARCH, err)
  85. }
  86. defer c.Close()
  87. p := ipv6.NewPacketConn(c)
  88. dst := c.LocalAddr()
  89. cf := ipv6.FlagTrafficClass | ipv6.FlagHopLimit | ipv6.FlagSrc | ipv6.FlagDst | ipv6.FlagInterface | ipv6.FlagPathMTU
  90. if err := p.SetControlMessage(cf, true); err != nil {
  91. b.Fatal(err)
  92. }
  93. b.Run("Net", func(b *testing.B) {
  94. for i := 0; i < b.N; i++ {
  95. if _, err := c.WriteTo(datagram, dst); err != nil {
  96. b.Fatal(err)
  97. }
  98. if _, _, err := c.ReadFrom(bb); err != nil {
  99. b.Fatal(err)
  100. }
  101. }
  102. })
  103. b.Run("ToFrom", func(b *testing.B) {
  104. for i := 0; i < b.N; i++ {
  105. if _, err := p.WriteTo(datagram, &cm, dst); err != nil {
  106. b.Fatal(err)
  107. }
  108. if _, _, _, err := p.ReadFrom(bb); err != nil {
  109. b.Fatal(err)
  110. }
  111. }
  112. })
  113. })
  114. }
  115. func TestPacketConnConcurrentReadWriteUnicast(t *testing.T) {
  116. switch runtime.GOOS {
  117. case "nacl", "plan9", "windows":
  118. t.Skipf("not supported on %s", runtime.GOOS)
  119. }
  120. payload := []byte("HELLO-R-U-THERE")
  121. iph := []byte{
  122. 0x69, 0x8b, 0xee, 0xf1, 0xca, 0xfe, 0xff, 0x01,
  123. 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x01, 0x00, 0x00,
  124. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  125. 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x02, 0x00, 0x00,
  126. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  127. }
  128. greh := []byte{0x00, 0x00, 0x86, 0xdd, 0x00, 0x00, 0x00, 0x00}
  129. datagram := append(greh, append(iph, payload...)...)
  130. t.Run("UDP", func(t *testing.T) {
  131. c, err := nettest.NewLocalPacketListener("udp6")
  132. if err != nil {
  133. t.Skipf("not supported on %s/%s: %v", runtime.GOOS, runtime.GOARCH, err)
  134. }
  135. defer c.Close()
  136. p := ipv6.NewPacketConn(c)
  137. t.Run("ToFrom", func(t *testing.T) {
  138. testPacketConnConcurrentReadWriteUnicast(t, p, payload, c.LocalAddr())
  139. })
  140. })
  141. t.Run("IP", func(t *testing.T) {
  142. switch runtime.GOOS {
  143. case "netbsd":
  144. t.Skip("need to configure gre on netbsd")
  145. case "openbsd":
  146. t.Skip("net.inet.gre.allow=0 by default on openbsd")
  147. }
  148. c, err := net.ListenPacket(fmt.Sprintf("ip6:%d", iana.ProtocolGRE), "::1")
  149. if err != nil {
  150. t.Skipf("not supported on %s/%s: %v", runtime.GOOS, runtime.GOARCH, err)
  151. }
  152. defer c.Close()
  153. p := ipv6.NewPacketConn(c)
  154. t.Run("ToFrom", func(t *testing.T) {
  155. testPacketConnConcurrentReadWriteUnicast(t, p, datagram, c.LocalAddr())
  156. })
  157. })
  158. }
  159. func testPacketConnConcurrentReadWriteUnicast(t *testing.T, p *ipv6.PacketConn, data []byte, dst net.Addr) {
  160. ifi := nettest.RoutedInterface("ip6", net.FlagUp|net.FlagLoopback)
  161. cf := ipv6.FlagTrafficClass | ipv6.FlagHopLimit | ipv6.FlagSrc | ipv6.FlagDst | ipv6.FlagInterface | ipv6.FlagPathMTU
  162. if err := p.SetControlMessage(cf, true); err != nil { // probe before test
  163. if nettest.ProtocolNotSupported(err) {
  164. t.Skipf("not supported on %s", runtime.GOOS)
  165. }
  166. t.Fatal(err)
  167. }
  168. var wg sync.WaitGroup
  169. reader := func() {
  170. defer wg.Done()
  171. b := make([]byte, 128)
  172. n, cm, _, err := p.ReadFrom(b)
  173. if err != nil {
  174. t.Error(err)
  175. return
  176. }
  177. if !bytes.Equal(b[:n], data) {
  178. t.Errorf("got %#v; want %#v", b[:n], data)
  179. return
  180. }
  181. s := cm.String()
  182. if strings.Contains(s, ",") {
  183. t.Errorf("should be space-separated values: %s", s)
  184. return
  185. }
  186. }
  187. writer := func(toggle bool) {
  188. defer wg.Done()
  189. cm := ipv6.ControlMessage{
  190. TrafficClass: iana.DiffServAF11 | iana.CongestionExperienced,
  191. HopLimit: 1,
  192. Src: net.IPv6loopback,
  193. }
  194. if ifi != nil {
  195. cm.IfIndex = ifi.Index
  196. }
  197. if err := p.SetControlMessage(cf, toggle); err != nil {
  198. t.Error(err)
  199. return
  200. }
  201. n, err := p.WriteTo(data, &cm, dst)
  202. if err != nil {
  203. t.Error(err)
  204. return
  205. }
  206. if n != len(data) {
  207. t.Errorf("got %d; want %d", n, len(data))
  208. return
  209. }
  210. }
  211. const N = 10
  212. wg.Add(N)
  213. for i := 0; i < N; i++ {
  214. go reader()
  215. }
  216. wg.Add(2 * N)
  217. for i := 0; i < 2*N; i++ {
  218. go writer(i%2 != 0)
  219. }
  220. wg.Add(N)
  221. for i := 0; i < N; i++ {
  222. go reader()
  223. }
  224. wg.Wait()
  225. }