You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 lines
3.3 KiB

  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. package ipv6_test
  5. import (
  6. "bytes"
  7. "net"
  8. "runtime"
  9. "strings"
  10. "sync"
  11. "testing"
  12. "golang.org/x/net/internal/iana"
  13. "golang.org/x/net/internal/nettest"
  14. "golang.org/x/net/ipv6"
  15. )
  16. func BenchmarkReadWriteUnicast(b *testing.B) {
  17. c, err := nettest.NewLocalPacketListener("udp6")
  18. if err != nil {
  19. b.Skipf("not supported on %s/%s: %v", runtime.GOOS, runtime.GOARCH, err)
  20. }
  21. defer c.Close()
  22. dst := c.LocalAddr()
  23. wb, rb := []byte("HELLO-R-U-THERE"), make([]byte, 128)
  24. b.Run("NetUDP", func(b *testing.B) {
  25. for i := 0; i < b.N; i++ {
  26. if _, err := c.WriteTo(wb, dst); err != nil {
  27. b.Fatal(err)
  28. }
  29. if _, _, err := c.ReadFrom(rb); err != nil {
  30. b.Fatal(err)
  31. }
  32. }
  33. })
  34. b.Run("IPv6UDP", func(b *testing.B) {
  35. p := ipv6.NewPacketConn(c)
  36. cf := ipv6.FlagTrafficClass | ipv6.FlagHopLimit | ipv6.FlagSrc | ipv6.FlagDst | ipv6.FlagInterface | ipv6.FlagPathMTU
  37. if err := p.SetControlMessage(cf, true); err != nil {
  38. b.Fatal(err)
  39. }
  40. cm := ipv6.ControlMessage{
  41. TrafficClass: iana.DiffServAF11 | iana.CongestionExperienced,
  42. HopLimit: 1,
  43. }
  44. ifi := nettest.RoutedInterface("ip6", net.FlagUp|net.FlagLoopback)
  45. if ifi != nil {
  46. cm.IfIndex = ifi.Index
  47. }
  48. for i := 0; i < b.N; i++ {
  49. if _, err := p.WriteTo(wb, &cm, dst); err != nil {
  50. b.Fatal(err)
  51. }
  52. if _, _, _, err := p.ReadFrom(rb); err != nil {
  53. b.Fatal(err)
  54. }
  55. }
  56. })
  57. }
  58. func TestPacketConnConcurrentReadWriteUnicastUDP(t *testing.T) {
  59. switch runtime.GOOS {
  60. case "nacl", "plan9", "windows":
  61. t.Skipf("not supported on %s", runtime.GOOS)
  62. }
  63. if !supportsIPv6 {
  64. t.Skip("ipv6 is not supported")
  65. }
  66. c, err := nettest.NewLocalPacketListener("udp6")
  67. if err != nil {
  68. t.Fatal(err)
  69. }
  70. defer c.Close()
  71. p := ipv6.NewPacketConn(c)
  72. defer p.Close()
  73. dst := c.LocalAddr()
  74. ifi := nettest.RoutedInterface("ip6", net.FlagUp|net.FlagLoopback)
  75. cf := ipv6.FlagTrafficClass | ipv6.FlagHopLimit | ipv6.FlagSrc | ipv6.FlagDst | ipv6.FlagInterface | ipv6.FlagPathMTU
  76. wb := []byte("HELLO-R-U-THERE")
  77. if err := p.SetControlMessage(cf, true); err != nil { // probe before test
  78. if nettest.ProtocolNotSupported(err) {
  79. t.Skipf("not supported on %s", runtime.GOOS)
  80. }
  81. t.Fatal(err)
  82. }
  83. var wg sync.WaitGroup
  84. reader := func() {
  85. defer wg.Done()
  86. rb := make([]byte, 128)
  87. if n, cm, _, err := p.ReadFrom(rb); err != nil {
  88. t.Error(err)
  89. return
  90. } else if !bytes.Equal(rb[:n], wb) {
  91. t.Errorf("got %v; want %v", rb[:n], wb)
  92. return
  93. } else {
  94. s := cm.String()
  95. if strings.Contains(s, ",") {
  96. t.Errorf("should be space-separated values: %s", s)
  97. }
  98. }
  99. }
  100. writer := func(toggle bool) {
  101. defer wg.Done()
  102. cm := ipv6.ControlMessage{
  103. TrafficClass: iana.DiffServAF11 | iana.CongestionExperienced,
  104. Src: net.IPv6loopback,
  105. }
  106. if ifi != nil {
  107. cm.IfIndex = ifi.Index
  108. }
  109. if err := p.SetControlMessage(cf, toggle); err != nil {
  110. t.Error(err)
  111. return
  112. }
  113. if n, err := p.WriteTo(wb, &cm, dst); err != nil {
  114. t.Error(err)
  115. return
  116. } else if n != len(wb) {
  117. t.Errorf("got %d; want %d", n, len(wb))
  118. return
  119. }
  120. }
  121. const N = 10
  122. wg.Add(N)
  123. for i := 0; i < N; i++ {
  124. go reader()
  125. }
  126. wg.Add(2 * N)
  127. for i := 0; i < 2*N; i++ {
  128. go writer(i%2 != 0)
  129. }
  130. wg.Add(N)
  131. for i := 0; i < N; i++ {
  132. go reader()
  133. }
  134. wg.Wait()
  135. }