123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509 |
- // Copyright 2013 The Go Authors. All rights reserved.
- // Use of this source code is governed by a BSD-style
- // license that can be found in the LICENSE file.
- package ipv6_test
- import (
- "bytes"
- "fmt"
- "net"
- "runtime"
- "strings"
- "sync"
- "testing"
- "golang.org/x/net/internal/iana"
- "golang.org/x/net/ipv6"
- "golang.org/x/net/nettest"
- )
- func BenchmarkReadWriteUnicast(b *testing.B) {
- switch runtime.GOOS {
- case "fuchsia", "hurd", "js", "nacl", "plan9", "windows":
- b.Skipf("not supported on %s", runtime.GOOS)
- }
- c, err := nettest.NewLocalPacketListener("udp6")
- if err != nil {
- b.Skipf("not supported on %s/%s: %v", runtime.GOOS, runtime.GOARCH, err)
- }
- defer c.Close()
- dst := c.LocalAddr()
- wb, rb := []byte("HELLO-R-U-THERE"), make([]byte, 128)
- b.Run("NetUDP", func(b *testing.B) {
- for i := 0; i < b.N; i++ {
- if _, err := c.WriteTo(wb, dst); err != nil {
- b.Fatal(err)
- }
- if _, _, err := c.ReadFrom(rb); err != nil {
- b.Fatal(err)
- }
- }
- })
- b.Run("IPv6UDP", func(b *testing.B) {
- p := ipv6.NewPacketConn(c)
- cf := ipv6.FlagTrafficClass | ipv6.FlagHopLimit | ipv6.FlagSrc | ipv6.FlagDst | ipv6.FlagInterface | ipv6.FlagPathMTU
- if err := p.SetControlMessage(cf, true); err != nil {
- b.Fatal(err)
- }
- cm := ipv6.ControlMessage{
- TrafficClass: iana.DiffServAF11 | iana.CongestionExperienced,
- HopLimit: 1,
- }
- ifi, _ := nettest.RoutedInterface("ip6", net.FlagUp|net.FlagLoopback)
- if ifi != nil {
- cm.IfIndex = ifi.Index
- }
- for i := 0; i < b.N; i++ {
- if _, err := p.WriteTo(wb, &cm, dst); err != nil {
- b.Fatal(err)
- }
- if _, _, _, err := p.ReadFrom(rb); err != nil {
- b.Fatal(err)
- }
- }
- })
- }
- func BenchmarkPacketConnReadWriteUnicast(b *testing.B) {
- switch runtime.GOOS {
- case "fuchsia", "hurd", "js", "nacl", "plan9", "windows":
- b.Skipf("not supported on %s", runtime.GOOS)
- }
- payload := []byte("HELLO-R-U-THERE")
- iph := []byte{
- 0x69, 0x8b, 0xee, 0xf1, 0xca, 0xfe, 0xff, 0x01,
- 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x01, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
- 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x02, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
- }
- greh := []byte{0x00, 0x00, 0x86, 0xdd, 0x00, 0x00, 0x00, 0x00}
- datagram := append(greh, append(iph, payload...)...)
- bb := make([]byte, 128)
- cm := ipv6.ControlMessage{
- TrafficClass: iana.DiffServAF11 | iana.CongestionExperienced,
- HopLimit: 1,
- Src: net.IPv6loopback,
- }
- ifi, _ := nettest.RoutedInterface("ip6", net.FlagUp|net.FlagLoopback)
- if ifi != nil {
- cm.IfIndex = ifi.Index
- }
- b.Run("UDP", func(b *testing.B) {
- c, err := nettest.NewLocalPacketListener("udp6")
- if err != nil {
- b.Skipf("not supported on %s/%s: %v", runtime.GOOS, runtime.GOARCH, err)
- }
- defer c.Close()
- p := ipv6.NewPacketConn(c)
- dst := c.LocalAddr()
- cf := ipv6.FlagHopLimit | ipv6.FlagInterface
- if err := p.SetControlMessage(cf, true); err != nil {
- b.Fatal(err)
- }
- wms := []ipv6.Message{
- {
- Buffers: [][]byte{payload},
- Addr: dst,
- OOB: cm.Marshal(),
- },
- }
- rms := []ipv6.Message{
- {
- Buffers: [][]byte{bb},
- OOB: ipv6.NewControlMessage(cf),
- },
- }
- b.Run("Net", func(b *testing.B) {
- for i := 0; i < b.N; i++ {
- if _, err := c.WriteTo(payload, dst); err != nil {
- b.Fatal(err)
- }
- if _, _, err := c.ReadFrom(bb); err != nil {
- b.Fatal(err)
- }
- }
- })
- b.Run("ToFrom", func(b *testing.B) {
- for i := 0; i < b.N; i++ {
- if _, err := p.WriteTo(payload, &cm, dst); err != nil {
- b.Fatal(err)
- }
- if _, _, _, err := p.ReadFrom(bb); err != nil {
- b.Fatal(err)
- }
- }
- })
- b.Run("Batch", func(b *testing.B) {
- for i := 0; i < b.N; i++ {
- if _, err := p.WriteBatch(wms, 0); err != nil {
- b.Fatal(err)
- }
- if _, err := p.ReadBatch(rms, 0); err != nil {
- b.Fatal(err)
- }
- }
- })
- })
- b.Run("IP", func(b *testing.B) {
- switch runtime.GOOS {
- case "netbsd":
- b.Skip("need to configure gre on netbsd")
- case "openbsd":
- b.Skip("net.inet.gre.allow=0 by default on openbsd")
- }
- c, err := net.ListenPacket(fmt.Sprintf("ip6:%d", iana.ProtocolGRE), "::1")
- if err != nil {
- b.Skipf("not supported on %s/%s: %v", runtime.GOOS, runtime.GOARCH, err)
- }
- defer c.Close()
- p := ipv6.NewPacketConn(c)
- dst := c.LocalAddr()
- cf := ipv6.FlagTrafficClass | ipv6.FlagHopLimit | ipv6.FlagSrc | ipv6.FlagDst | ipv6.FlagInterface | ipv6.FlagPathMTU
- if err := p.SetControlMessage(cf, true); err != nil {
- b.Fatal(err)
- }
- wms := []ipv6.Message{
- {
- Buffers: [][]byte{datagram},
- Addr: dst,
- OOB: cm.Marshal(),
- },
- }
- rms := []ipv6.Message{
- {
- Buffers: [][]byte{bb},
- OOB: ipv6.NewControlMessage(cf),
- },
- }
- b.Run("Net", func(b *testing.B) {
- for i := 0; i < b.N; i++ {
- if _, err := c.WriteTo(datagram, dst); err != nil {
- b.Fatal(err)
- }
- if _, _, err := c.ReadFrom(bb); err != nil {
- b.Fatal(err)
- }
- }
- })
- b.Run("ToFrom", func(b *testing.B) {
- for i := 0; i < b.N; i++ {
- if _, err := p.WriteTo(datagram, &cm, dst); err != nil {
- b.Fatal(err)
- }
- if _, _, _, err := p.ReadFrom(bb); err != nil {
- b.Fatal(err)
- }
- }
- })
- b.Run("Batch", func(b *testing.B) {
- for i := 0; i < b.N; i++ {
- if _, err := p.WriteBatch(wms, 0); err != nil {
- b.Fatal(err)
- }
- if _, err := p.ReadBatch(rms, 0); err != nil {
- b.Fatal(err)
- }
- }
- })
- })
- }
- func TestPacketConnConcurrentReadWriteUnicastUDP(t *testing.T) {
- switch runtime.GOOS {
- case "fuchsia", "hurd", "js", "nacl", "plan9", "windows":
- t.Skipf("not supported on %s", runtime.GOOS)
- }
- if !nettest.SupportsIPv6() {
- t.Skip("ipv6 is not supported")
- }
- c, err := nettest.NewLocalPacketListener("udp6")
- if err != nil {
- t.Fatal(err)
- }
- defer c.Close()
- p := ipv6.NewPacketConn(c)
- defer p.Close()
- dst := c.LocalAddr()
- ifi, _ := nettest.RoutedInterface("ip6", net.FlagUp|net.FlagLoopback)
- cf := ipv6.FlagTrafficClass | ipv6.FlagHopLimit | ipv6.FlagSrc | ipv6.FlagDst | ipv6.FlagInterface | ipv6.FlagPathMTU
- wb := []byte("HELLO-R-U-THERE")
- if err := p.SetControlMessage(cf, true); err != nil { // probe before test
- if protocolNotSupported(err) {
- t.Skipf("not supported on %s", runtime.GOOS)
- }
- t.Fatal(err)
- }
- var wg sync.WaitGroup
- reader := func() {
- defer wg.Done()
- rb := make([]byte, 128)
- if n, cm, _, err := p.ReadFrom(rb); err != nil {
- t.Error(err)
- return
- } else if !bytes.Equal(rb[:n], wb) {
- t.Errorf("got %v; want %v", rb[:n], wb)
- return
- } else {
- s := cm.String()
- if strings.Contains(s, ",") {
- t.Errorf("should be space-separated values: %s", s)
- }
- }
- }
- writer := func(toggle bool) {
- defer wg.Done()
- cm := ipv6.ControlMessage{
- TrafficClass: iana.DiffServAF11 | iana.CongestionExperienced,
- Src: net.IPv6loopback,
- }
- if ifi != nil {
- cm.IfIndex = ifi.Index
- }
- if err := p.SetControlMessage(cf, toggle); err != nil {
- t.Error(err)
- return
- }
- if n, err := p.WriteTo(wb, &cm, dst); err != nil {
- t.Error(err)
- return
- } else if n != len(wb) {
- t.Errorf("got %d; want %d", n, len(wb))
- return
- }
- }
- const N = 10
- wg.Add(N)
- for i := 0; i < N; i++ {
- go reader()
- }
- wg.Add(2 * N)
- for i := 0; i < 2*N; i++ {
- go writer(i%2 != 0)
- }
- wg.Add(N)
- for i := 0; i < N; i++ {
- go reader()
- }
- wg.Wait()
- }
- func TestPacketConnConcurrentReadWriteUnicast(t *testing.T) {
- switch runtime.GOOS {
- case "fuchsia", "hurd", "js", "nacl", "plan9", "windows":
- t.Skipf("not supported on %s", runtime.GOOS)
- }
- payload := []byte("HELLO-R-U-THERE")
- iph := []byte{
- 0x69, 0x8b, 0xee, 0xf1, 0xca, 0xfe, 0xff, 0x01,
- 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x01, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
- 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x02, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
- }
- greh := []byte{0x00, 0x00, 0x86, 0xdd, 0x00, 0x00, 0x00, 0x00}
- datagram := append(greh, append(iph, payload...)...)
- t.Run("UDP", func(t *testing.T) {
- c, err := nettest.NewLocalPacketListener("udp6")
- if err != nil {
- t.Skipf("not supported on %s/%s: %v", runtime.GOOS, runtime.GOARCH, err)
- }
- defer c.Close()
- p := ipv6.NewPacketConn(c)
- t.Run("ToFrom", func(t *testing.T) {
- testPacketConnConcurrentReadWriteUnicast(t, p, payload, c.LocalAddr(), false)
- })
- t.Run("Batch", func(t *testing.T) {
- testPacketConnConcurrentReadWriteUnicast(t, p, payload, c.LocalAddr(), true)
- })
- })
- t.Run("IP", func(t *testing.T) {
- switch runtime.GOOS {
- case "netbsd":
- t.Skip("need to configure gre on netbsd")
- case "openbsd":
- t.Skip("net.inet.gre.allow=0 by default on openbsd")
- }
- c, err := net.ListenPacket(fmt.Sprintf("ip6:%d", iana.ProtocolGRE), "::1")
- if err != nil {
- t.Skipf("not supported on %s/%s: %v", runtime.GOOS, runtime.GOARCH, err)
- }
- defer c.Close()
- p := ipv6.NewPacketConn(c)
- t.Run("ToFrom", func(t *testing.T) {
- testPacketConnConcurrentReadWriteUnicast(t, p, datagram, c.LocalAddr(), false)
- })
- t.Run("Batch", func(t *testing.T) {
- testPacketConnConcurrentReadWriteUnicast(t, p, datagram, c.LocalAddr(), true)
- })
- })
- }
- func testPacketConnConcurrentReadWriteUnicast(t *testing.T, p *ipv6.PacketConn, data []byte, dst net.Addr, batch bool) {
- t.Helper()
- ifi, _ := nettest.RoutedInterface("ip6", net.FlagUp|net.FlagLoopback)
- cf := ipv6.FlagTrafficClass | ipv6.FlagHopLimit | ipv6.FlagSrc | ipv6.FlagDst | ipv6.FlagInterface | ipv6.FlagPathMTU
- if err := p.SetControlMessage(cf, true); err != nil { // probe before test
- if protocolNotSupported(err) {
- t.Skipf("not supported on %s", runtime.GOOS)
- }
- t.Fatal(err)
- }
- var wg sync.WaitGroup
- reader := func() {
- defer wg.Done()
- b := make([]byte, 128)
- n, cm, _, err := p.ReadFrom(b)
- if err != nil {
- t.Error(err)
- return
- }
- if !bytes.Equal(b[:n], data) {
- t.Errorf("got %#v; want %#v", b[:n], data)
- return
- }
- s := cm.String()
- if strings.Contains(s, ",") {
- t.Errorf("should be space-separated values: %s", s)
- return
- }
- }
- batchReader := func() {
- defer wg.Done()
- ms := []ipv6.Message{
- {
- Buffers: [][]byte{make([]byte, 128)},
- OOB: ipv6.NewControlMessage(cf),
- },
- }
- n, err := p.ReadBatch(ms, 0)
- if err != nil {
- t.Error(err)
- return
- }
- if n != len(ms) {
- t.Errorf("got %d; want %d", n, len(ms))
- return
- }
- var cm ipv6.ControlMessage
- if err := cm.Parse(ms[0].OOB[:ms[0].NN]); err != nil {
- t.Error(err)
- return
- }
- b := ms[0].Buffers[0][:ms[0].N]
- if !bytes.Equal(b, data) {
- t.Errorf("got %#v; want %#v", b, data)
- return
- }
- s := cm.String()
- if strings.Contains(s, ",") {
- t.Errorf("should be space-separated values: %s", s)
- return
- }
- }
- writer := func(toggle bool) {
- defer wg.Done()
- cm := ipv6.ControlMessage{
- TrafficClass: iana.DiffServAF11 | iana.CongestionExperienced,
- HopLimit: 1,
- Src: net.IPv6loopback,
- }
- if ifi != nil {
- cm.IfIndex = ifi.Index
- }
- if err := p.SetControlMessage(cf, toggle); err != nil {
- t.Error(err)
- return
- }
- n, err := p.WriteTo(data, &cm, dst)
- if err != nil {
- t.Error(err)
- return
- }
- if n != len(data) {
- t.Errorf("got %d; want %d", n, len(data))
- return
- }
- }
- batchWriter := func(toggle bool) {
- defer wg.Done()
- cm := ipv6.ControlMessage{
- TrafficClass: iana.DiffServAF11 | iana.CongestionExperienced,
- HopLimit: 1,
- Src: net.IPv6loopback,
- }
- if ifi != nil {
- cm.IfIndex = ifi.Index
- }
- if err := p.SetControlMessage(cf, toggle); err != nil {
- t.Error(err)
- return
- }
- ms := []ipv6.Message{
- {
- Buffers: [][]byte{data},
- OOB: cm.Marshal(),
- Addr: dst,
- },
- }
- n, err := p.WriteBatch(ms, 0)
- if err != nil {
- t.Error(err)
- return
- }
- if n != len(ms) {
- t.Errorf("got %d; want %d", n, len(ms))
- return
- }
- if ms[0].N != len(data) {
- t.Errorf("got %d; want %d", ms[0].N, len(data))
- return
- }
- }
- const N = 10
- wg.Add(N)
- for i := 0; i < N; i++ {
- if batch {
- go batchReader()
- } else {
- go reader()
- }
- }
- wg.Add(2 * N)
- for i := 0; i < 2*N; i++ {
- if batch {
- go batchWriter(i%2 != 0)
- } else {
- go writer(i%2 != 0)
- }
- }
- wg.Add(N)
- for i := 0; i < N; i++ {
- if batch {
- go batchReader()
- } else {
- go reader()
- }
- }
- wg.Wait()
- }
|