readwrite_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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. "fmt"
  8. "net"
  9. "runtime"
  10. "strings"
  11. "sync"
  12. "testing"
  13. "golang.org/x/net/internal/iana"
  14. "golang.org/x/net/ipv6"
  15. "golang.org/x/net/nettest"
  16. )
  17. func BenchmarkReadWriteUnicast(b *testing.B) {
  18. switch runtime.GOOS {
  19. case "fuchsia", "hurd", "js", "nacl", "plan9", "windows":
  20. b.Skipf("not supported on %s", runtime.GOOS)
  21. }
  22. c, err := nettest.NewLocalPacketListener("udp6")
  23. if err != nil {
  24. b.Skipf("not supported on %s/%s: %v", runtime.GOOS, runtime.GOARCH, err)
  25. }
  26. defer c.Close()
  27. dst := c.LocalAddr()
  28. wb, rb := []byte("HELLO-R-U-THERE"), make([]byte, 128)
  29. b.Run("NetUDP", func(b *testing.B) {
  30. for i := 0; i < b.N; i++ {
  31. if _, err := c.WriteTo(wb, dst); err != nil {
  32. b.Fatal(err)
  33. }
  34. if _, _, err := c.ReadFrom(rb); err != nil {
  35. b.Fatal(err)
  36. }
  37. }
  38. })
  39. b.Run("IPv6UDP", func(b *testing.B) {
  40. p := ipv6.NewPacketConn(c)
  41. cf := ipv6.FlagTrafficClass | ipv6.FlagHopLimit | ipv6.FlagSrc | ipv6.FlagDst | ipv6.FlagInterface | ipv6.FlagPathMTU
  42. if err := p.SetControlMessage(cf, true); err != nil {
  43. b.Fatal(err)
  44. }
  45. cm := ipv6.ControlMessage{
  46. TrafficClass: iana.DiffServAF11 | iana.CongestionExperienced,
  47. HopLimit: 1,
  48. }
  49. ifi, _ := nettest.RoutedInterface("ip6", net.FlagUp|net.FlagLoopback)
  50. if ifi != nil {
  51. cm.IfIndex = ifi.Index
  52. }
  53. for i := 0; i < b.N; i++ {
  54. if _, err := p.WriteTo(wb, &cm, dst); err != nil {
  55. b.Fatal(err)
  56. }
  57. if _, _, _, err := p.ReadFrom(rb); err != nil {
  58. b.Fatal(err)
  59. }
  60. }
  61. })
  62. }
  63. func BenchmarkPacketConnReadWriteUnicast(b *testing.B) {
  64. switch runtime.GOOS {
  65. case "fuchsia", "hurd", "js", "nacl", "plan9", "windows":
  66. b.Skipf("not supported on %s", runtime.GOOS)
  67. }
  68. payload := []byte("HELLO-R-U-THERE")
  69. iph := []byte{
  70. 0x69, 0x8b, 0xee, 0xf1, 0xca, 0xfe, 0xff, 0x01,
  71. 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x01, 0x00, 0x00,
  72. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  73. 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x02, 0x00, 0x00,
  74. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  75. }
  76. greh := []byte{0x00, 0x00, 0x86, 0xdd, 0x00, 0x00, 0x00, 0x00}
  77. datagram := append(greh, append(iph, payload...)...)
  78. bb := make([]byte, 128)
  79. cm := ipv6.ControlMessage{
  80. TrafficClass: iana.DiffServAF11 | iana.CongestionExperienced,
  81. HopLimit: 1,
  82. Src: net.IPv6loopback,
  83. }
  84. ifi, _ := nettest.RoutedInterface("ip6", net.FlagUp|net.FlagLoopback)
  85. if ifi != nil {
  86. cm.IfIndex = ifi.Index
  87. }
  88. b.Run("UDP", func(b *testing.B) {
  89. c, err := nettest.NewLocalPacketListener("udp6")
  90. if err != nil {
  91. b.Skipf("not supported on %s/%s: %v", runtime.GOOS, runtime.GOARCH, err)
  92. }
  93. defer c.Close()
  94. p := ipv6.NewPacketConn(c)
  95. dst := c.LocalAddr()
  96. cf := ipv6.FlagHopLimit | ipv6.FlagInterface
  97. if err := p.SetControlMessage(cf, true); err != nil {
  98. b.Fatal(err)
  99. }
  100. wms := []ipv6.Message{
  101. {
  102. Buffers: [][]byte{payload},
  103. Addr: dst,
  104. OOB: cm.Marshal(),
  105. },
  106. }
  107. rms := []ipv6.Message{
  108. {
  109. Buffers: [][]byte{bb},
  110. OOB: ipv6.NewControlMessage(cf),
  111. },
  112. }
  113. b.Run("Net", func(b *testing.B) {
  114. for i := 0; i < b.N; i++ {
  115. if _, err := c.WriteTo(payload, dst); err != nil {
  116. b.Fatal(err)
  117. }
  118. if _, _, err := c.ReadFrom(bb); err != nil {
  119. b.Fatal(err)
  120. }
  121. }
  122. })
  123. b.Run("ToFrom", func(b *testing.B) {
  124. for i := 0; i < b.N; i++ {
  125. if _, err := p.WriteTo(payload, &cm, dst); err != nil {
  126. b.Fatal(err)
  127. }
  128. if _, _, _, err := p.ReadFrom(bb); err != nil {
  129. b.Fatal(err)
  130. }
  131. }
  132. })
  133. b.Run("Batch", func(b *testing.B) {
  134. for i := 0; i < b.N; i++ {
  135. if _, err := p.WriteBatch(wms, 0); err != nil {
  136. b.Fatal(err)
  137. }
  138. if _, err := p.ReadBatch(rms, 0); err != nil {
  139. b.Fatal(err)
  140. }
  141. }
  142. })
  143. })
  144. b.Run("IP", func(b *testing.B) {
  145. switch runtime.GOOS {
  146. case "netbsd":
  147. b.Skip("need to configure gre on netbsd")
  148. case "openbsd":
  149. b.Skip("net.inet.gre.allow=0 by default on openbsd")
  150. }
  151. c, err := net.ListenPacket(fmt.Sprintf("ip6:%d", iana.ProtocolGRE), "::1")
  152. if err != nil {
  153. b.Skipf("not supported on %s/%s: %v", runtime.GOOS, runtime.GOARCH, err)
  154. }
  155. defer c.Close()
  156. p := ipv6.NewPacketConn(c)
  157. dst := c.LocalAddr()
  158. cf := ipv6.FlagTrafficClass | ipv6.FlagHopLimit | ipv6.FlagSrc | ipv6.FlagDst | ipv6.FlagInterface | ipv6.FlagPathMTU
  159. if err := p.SetControlMessage(cf, true); err != nil {
  160. b.Fatal(err)
  161. }
  162. wms := []ipv6.Message{
  163. {
  164. Buffers: [][]byte{datagram},
  165. Addr: dst,
  166. OOB: cm.Marshal(),
  167. },
  168. }
  169. rms := []ipv6.Message{
  170. {
  171. Buffers: [][]byte{bb},
  172. OOB: ipv6.NewControlMessage(cf),
  173. },
  174. }
  175. b.Run("Net", func(b *testing.B) {
  176. for i := 0; i < b.N; i++ {
  177. if _, err := c.WriteTo(datagram, dst); err != nil {
  178. b.Fatal(err)
  179. }
  180. if _, _, err := c.ReadFrom(bb); err != nil {
  181. b.Fatal(err)
  182. }
  183. }
  184. })
  185. b.Run("ToFrom", func(b *testing.B) {
  186. for i := 0; i < b.N; i++ {
  187. if _, err := p.WriteTo(datagram, &cm, dst); err != nil {
  188. b.Fatal(err)
  189. }
  190. if _, _, _, err := p.ReadFrom(bb); err != nil {
  191. b.Fatal(err)
  192. }
  193. }
  194. })
  195. b.Run("Batch", func(b *testing.B) {
  196. for i := 0; i < b.N; i++ {
  197. if _, err := p.WriteBatch(wms, 0); err != nil {
  198. b.Fatal(err)
  199. }
  200. if _, err := p.ReadBatch(rms, 0); err != nil {
  201. b.Fatal(err)
  202. }
  203. }
  204. })
  205. })
  206. }
  207. func TestPacketConnConcurrentReadWriteUnicastUDP(t *testing.T) {
  208. switch runtime.GOOS {
  209. case "fuchsia", "hurd", "js", "nacl", "plan9", "windows":
  210. t.Skipf("not supported on %s", runtime.GOOS)
  211. }
  212. if !nettest.SupportsIPv6() {
  213. t.Skip("ipv6 is not supported")
  214. }
  215. c, err := nettest.NewLocalPacketListener("udp6")
  216. if err != nil {
  217. t.Fatal(err)
  218. }
  219. defer c.Close()
  220. p := ipv6.NewPacketConn(c)
  221. defer p.Close()
  222. dst := c.LocalAddr()
  223. ifi, _ := nettest.RoutedInterface("ip6", net.FlagUp|net.FlagLoopback)
  224. cf := ipv6.FlagTrafficClass | ipv6.FlagHopLimit | ipv6.FlagSrc | ipv6.FlagDst | ipv6.FlagInterface | ipv6.FlagPathMTU
  225. wb := []byte("HELLO-R-U-THERE")
  226. if err := p.SetControlMessage(cf, true); err != nil { // probe before test
  227. if protocolNotSupported(err) {
  228. t.Skipf("not supported on %s", runtime.GOOS)
  229. }
  230. t.Fatal(err)
  231. }
  232. var wg sync.WaitGroup
  233. reader := func() {
  234. defer wg.Done()
  235. rb := make([]byte, 128)
  236. if n, cm, _, err := p.ReadFrom(rb); err != nil {
  237. t.Error(err)
  238. return
  239. } else if !bytes.Equal(rb[:n], wb) {
  240. t.Errorf("got %v; want %v", rb[:n], wb)
  241. return
  242. } else {
  243. s := cm.String()
  244. if strings.Contains(s, ",") {
  245. t.Errorf("should be space-separated values: %s", s)
  246. }
  247. }
  248. }
  249. writer := func(toggle bool) {
  250. defer wg.Done()
  251. cm := ipv6.ControlMessage{
  252. TrafficClass: iana.DiffServAF11 | iana.CongestionExperienced,
  253. Src: net.IPv6loopback,
  254. }
  255. if ifi != nil {
  256. cm.IfIndex = ifi.Index
  257. }
  258. if err := p.SetControlMessage(cf, toggle); err != nil {
  259. t.Error(err)
  260. return
  261. }
  262. if n, err := p.WriteTo(wb, &cm, dst); err != nil {
  263. t.Error(err)
  264. return
  265. } else if n != len(wb) {
  266. t.Errorf("got %d; want %d", n, len(wb))
  267. return
  268. }
  269. }
  270. const N = 10
  271. wg.Add(N)
  272. for i := 0; i < N; i++ {
  273. go reader()
  274. }
  275. wg.Add(2 * N)
  276. for i := 0; i < 2*N; i++ {
  277. go writer(i%2 != 0)
  278. }
  279. wg.Add(N)
  280. for i := 0; i < N; i++ {
  281. go reader()
  282. }
  283. wg.Wait()
  284. }
  285. func TestPacketConnConcurrentReadWriteUnicast(t *testing.T) {
  286. switch runtime.GOOS {
  287. case "fuchsia", "hurd", "js", "nacl", "plan9", "windows":
  288. t.Skipf("not supported on %s", runtime.GOOS)
  289. }
  290. payload := []byte("HELLO-R-U-THERE")
  291. iph := []byte{
  292. 0x69, 0x8b, 0xee, 0xf1, 0xca, 0xfe, 0xff, 0x01,
  293. 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x01, 0x00, 0x00,
  294. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  295. 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x02, 0x00, 0x00,
  296. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  297. }
  298. greh := []byte{0x00, 0x00, 0x86, 0xdd, 0x00, 0x00, 0x00, 0x00}
  299. datagram := append(greh, append(iph, payload...)...)
  300. t.Run("UDP", func(t *testing.T) {
  301. c, err := nettest.NewLocalPacketListener("udp6")
  302. if err != nil {
  303. t.Skipf("not supported on %s/%s: %v", runtime.GOOS, runtime.GOARCH, err)
  304. }
  305. defer c.Close()
  306. p := ipv6.NewPacketConn(c)
  307. t.Run("ToFrom", func(t *testing.T) {
  308. testPacketConnConcurrentReadWriteUnicast(t, p, payload, c.LocalAddr(), false)
  309. })
  310. t.Run("Batch", func(t *testing.T) {
  311. testPacketConnConcurrentReadWriteUnicast(t, p, payload, c.LocalAddr(), true)
  312. })
  313. })
  314. t.Run("IP", func(t *testing.T) {
  315. switch runtime.GOOS {
  316. case "netbsd":
  317. t.Skip("need to configure gre on netbsd")
  318. case "openbsd":
  319. t.Skip("net.inet.gre.allow=0 by default on openbsd")
  320. }
  321. c, err := net.ListenPacket(fmt.Sprintf("ip6:%d", iana.ProtocolGRE), "::1")
  322. if err != nil {
  323. t.Skipf("not supported on %s/%s: %v", runtime.GOOS, runtime.GOARCH, err)
  324. }
  325. defer c.Close()
  326. p := ipv6.NewPacketConn(c)
  327. t.Run("ToFrom", func(t *testing.T) {
  328. testPacketConnConcurrentReadWriteUnicast(t, p, datagram, c.LocalAddr(), false)
  329. })
  330. t.Run("Batch", func(t *testing.T) {
  331. testPacketConnConcurrentReadWriteUnicast(t, p, datagram, c.LocalAddr(), true)
  332. })
  333. })
  334. }
  335. func testPacketConnConcurrentReadWriteUnicast(t *testing.T, p *ipv6.PacketConn, data []byte, dst net.Addr, batch bool) {
  336. t.Helper()
  337. ifi, _ := nettest.RoutedInterface("ip6", net.FlagUp|net.FlagLoopback)
  338. cf := ipv6.FlagTrafficClass | ipv6.FlagHopLimit | ipv6.FlagSrc | ipv6.FlagDst | ipv6.FlagInterface | ipv6.FlagPathMTU
  339. if err := p.SetControlMessage(cf, true); err != nil { // probe before test
  340. if protocolNotSupported(err) {
  341. t.Skipf("not supported on %s", runtime.GOOS)
  342. }
  343. t.Fatal(err)
  344. }
  345. var wg sync.WaitGroup
  346. reader := func() {
  347. defer wg.Done()
  348. b := make([]byte, 128)
  349. n, cm, _, err := p.ReadFrom(b)
  350. if err != nil {
  351. t.Error(err)
  352. return
  353. }
  354. if !bytes.Equal(b[:n], data) {
  355. t.Errorf("got %#v; want %#v", b[:n], data)
  356. return
  357. }
  358. s := cm.String()
  359. if strings.Contains(s, ",") {
  360. t.Errorf("should be space-separated values: %s", s)
  361. return
  362. }
  363. }
  364. batchReader := func() {
  365. defer wg.Done()
  366. ms := []ipv6.Message{
  367. {
  368. Buffers: [][]byte{make([]byte, 128)},
  369. OOB: ipv6.NewControlMessage(cf),
  370. },
  371. }
  372. n, err := p.ReadBatch(ms, 0)
  373. if err != nil {
  374. t.Error(err)
  375. return
  376. }
  377. if n != len(ms) {
  378. t.Errorf("got %d; want %d", n, len(ms))
  379. return
  380. }
  381. var cm ipv6.ControlMessage
  382. if err := cm.Parse(ms[0].OOB[:ms[0].NN]); err != nil {
  383. t.Error(err)
  384. return
  385. }
  386. b := ms[0].Buffers[0][:ms[0].N]
  387. if !bytes.Equal(b, data) {
  388. t.Errorf("got %#v; want %#v", b, data)
  389. return
  390. }
  391. s := cm.String()
  392. if strings.Contains(s, ",") {
  393. t.Errorf("should be space-separated values: %s", s)
  394. return
  395. }
  396. }
  397. writer := func(toggle bool) {
  398. defer wg.Done()
  399. cm := ipv6.ControlMessage{
  400. TrafficClass: iana.DiffServAF11 | iana.CongestionExperienced,
  401. HopLimit: 1,
  402. Src: net.IPv6loopback,
  403. }
  404. if ifi != nil {
  405. cm.IfIndex = ifi.Index
  406. }
  407. if err := p.SetControlMessage(cf, toggle); err != nil {
  408. t.Error(err)
  409. return
  410. }
  411. n, err := p.WriteTo(data, &cm, dst)
  412. if err != nil {
  413. t.Error(err)
  414. return
  415. }
  416. if n != len(data) {
  417. t.Errorf("got %d; want %d", n, len(data))
  418. return
  419. }
  420. }
  421. batchWriter := func(toggle bool) {
  422. defer wg.Done()
  423. cm := ipv6.ControlMessage{
  424. TrafficClass: iana.DiffServAF11 | iana.CongestionExperienced,
  425. HopLimit: 1,
  426. Src: net.IPv6loopback,
  427. }
  428. if ifi != nil {
  429. cm.IfIndex = ifi.Index
  430. }
  431. if err := p.SetControlMessage(cf, toggle); err != nil {
  432. t.Error(err)
  433. return
  434. }
  435. ms := []ipv6.Message{
  436. {
  437. Buffers: [][]byte{data},
  438. OOB: cm.Marshal(),
  439. Addr: dst,
  440. },
  441. }
  442. n, err := p.WriteBatch(ms, 0)
  443. if err != nil {
  444. t.Error(err)
  445. return
  446. }
  447. if n != len(ms) {
  448. t.Errorf("got %d; want %d", n, len(ms))
  449. return
  450. }
  451. if ms[0].N != len(data) {
  452. t.Errorf("got %d; want %d", ms[0].N, len(data))
  453. return
  454. }
  455. }
  456. const N = 10
  457. wg.Add(N)
  458. for i := 0; i < N; i++ {
  459. if batch {
  460. go batchReader()
  461. } else {
  462. go reader()
  463. }
  464. }
  465. wg.Add(2 * N)
  466. for i := 0; i < 2*N; i++ {
  467. if batch {
  468. go batchWriter(i%2 != 0)
  469. } else {
  470. go writer(i%2 != 0)
  471. }
  472. }
  473. wg.Add(N)
  474. for i := 0; i < N; i++ {
  475. if batch {
  476. go batchReader()
  477. } else {
  478. go reader()
  479. }
  480. }
  481. wg.Wait()
  482. }