readwrite_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. // Copyright 2012 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 ipv4_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/ipv4"
  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("udp4")
  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("IPv4UDP", func(b *testing.B) {
  40. p := ipv4.NewPacketConn(c)
  41. cf := ipv4.FlagTTL | ipv4.FlagInterface
  42. if err := p.SetControlMessage(cf, true); err != nil {
  43. b.Fatal(err)
  44. }
  45. cm := ipv4.ControlMessage{TTL: 1}
  46. ifi, _ := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagLoopback)
  47. if ifi != nil {
  48. cm.IfIndex = ifi.Index
  49. }
  50. for i := 0; i < b.N; i++ {
  51. if _, err := p.WriteTo(wb, &cm, dst); err != nil {
  52. b.Fatal(err)
  53. }
  54. if _, _, _, err := p.ReadFrom(rb); err != nil {
  55. b.Fatal(err)
  56. }
  57. }
  58. })
  59. }
  60. func BenchmarkPacketConnReadWriteUnicast(b *testing.B) {
  61. switch runtime.GOOS {
  62. case "fuchsia", "hurd", "js", "nacl", "plan9", "windows":
  63. b.Skipf("not supported on %s", runtime.GOOS)
  64. }
  65. payload := []byte("HELLO-R-U-THERE")
  66. iph, err := (&ipv4.Header{
  67. Version: ipv4.Version,
  68. Len: ipv4.HeaderLen,
  69. TotalLen: ipv4.HeaderLen + len(payload),
  70. TTL: 1,
  71. Protocol: iana.ProtocolReserved,
  72. Src: net.IPv4(192, 0, 2, 1),
  73. Dst: net.IPv4(192, 0, 2, 254),
  74. }).Marshal()
  75. if err != nil {
  76. b.Fatal(err)
  77. }
  78. greh := []byte{0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00}
  79. datagram := append(greh, append(iph, payload...)...)
  80. bb := make([]byte, 128)
  81. cm := ipv4.ControlMessage{
  82. Src: net.IPv4(127, 0, 0, 1),
  83. }
  84. ifi, _ := nettest.RoutedInterface("ip4", 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("udp4")
  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 := ipv4.NewPacketConn(c)
  95. dst := c.LocalAddr()
  96. cf := ipv4.FlagTTL | ipv4.FlagInterface
  97. if err := p.SetControlMessage(cf, true); err != nil {
  98. b.Fatal(err)
  99. }
  100. wms := []ipv4.Message{
  101. {
  102. Buffers: [][]byte{payload},
  103. Addr: dst,
  104. OOB: cm.Marshal(),
  105. },
  106. }
  107. rms := []ipv4.Message{
  108. {
  109. Buffers: [][]byte{bb},
  110. OOB: ipv4.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("ip4:%d", iana.ProtocolGRE), "127.0.0.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 := ipv4.NewPacketConn(c)
  157. dst := c.LocalAddr()
  158. cf := ipv4.FlagTTL | ipv4.FlagInterface
  159. if err := p.SetControlMessage(cf, true); err != nil {
  160. b.Fatal(err)
  161. }
  162. wms := []ipv4.Message{
  163. {
  164. Buffers: [][]byte{datagram},
  165. Addr: dst,
  166. OOB: cm.Marshal(),
  167. },
  168. }
  169. rms := []ipv4.Message{
  170. {
  171. Buffers: [][]byte{bb},
  172. OOB: ipv4.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. c, err := nettest.NewLocalPacketListener("udp4")
  213. if err != nil {
  214. t.Fatal(err)
  215. }
  216. defer c.Close()
  217. p := ipv4.NewPacketConn(c)
  218. defer p.Close()
  219. dst := c.LocalAddr()
  220. ifi, _ := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagLoopback)
  221. cf := ipv4.FlagTTL | ipv4.FlagSrc | ipv4.FlagDst | ipv4.FlagInterface
  222. wb := []byte("HELLO-R-U-THERE")
  223. if err := p.SetControlMessage(cf, true); err != nil { // probe before test
  224. if protocolNotSupported(err) {
  225. t.Skipf("not supported on %s", runtime.GOOS)
  226. }
  227. t.Fatal(err)
  228. }
  229. var wg sync.WaitGroup
  230. reader := func() {
  231. defer wg.Done()
  232. rb := make([]byte, 128)
  233. if n, cm, _, err := p.ReadFrom(rb); err != nil {
  234. t.Error(err)
  235. return
  236. } else if !bytes.Equal(rb[:n], wb) {
  237. t.Errorf("got %v; want %v", rb[:n], wb)
  238. return
  239. } else {
  240. s := cm.String()
  241. if strings.Contains(s, ",") {
  242. t.Errorf("should be space-separated values: %s", s)
  243. }
  244. }
  245. }
  246. writer := func(toggle bool) {
  247. defer wg.Done()
  248. cm := ipv4.ControlMessage{
  249. Src: net.IPv4(127, 0, 0, 1),
  250. }
  251. if ifi != nil {
  252. cm.IfIndex = ifi.Index
  253. }
  254. if err := p.SetControlMessage(cf, toggle); err != nil {
  255. t.Error(err)
  256. return
  257. }
  258. if n, err := p.WriteTo(wb, &cm, dst); err != nil {
  259. t.Error(err)
  260. return
  261. } else if n != len(wb) {
  262. t.Errorf("got %d; want %d", n, len(wb))
  263. return
  264. }
  265. }
  266. const N = 10
  267. wg.Add(N)
  268. for i := 0; i < N; i++ {
  269. go reader()
  270. }
  271. wg.Add(2 * N)
  272. for i := 0; i < 2*N; i++ {
  273. go writer(i%2 != 0)
  274. }
  275. wg.Add(N)
  276. for i := 0; i < N; i++ {
  277. go reader()
  278. }
  279. wg.Wait()
  280. }
  281. func TestPacketConnConcurrentReadWriteUnicast(t *testing.T) {
  282. switch runtime.GOOS {
  283. case "fuchsia", "hurd", "js", "nacl", "plan9", "windows":
  284. t.Skipf("not supported on %s", runtime.GOOS)
  285. }
  286. payload := []byte("HELLO-R-U-THERE")
  287. iph, err := (&ipv4.Header{
  288. Version: ipv4.Version,
  289. Len: ipv4.HeaderLen,
  290. TotalLen: ipv4.HeaderLen + len(payload),
  291. TTL: 1,
  292. Protocol: iana.ProtocolReserved,
  293. Src: net.IPv4(192, 0, 2, 1),
  294. Dst: net.IPv4(192, 0, 2, 254),
  295. }).Marshal()
  296. if err != nil {
  297. t.Fatal(err)
  298. }
  299. greh := []byte{0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00}
  300. datagram := append(greh, append(iph, payload...)...)
  301. t.Run("UDP", func(t *testing.T) {
  302. c, err := nettest.NewLocalPacketListener("udp4")
  303. if err != nil {
  304. t.Skipf("not supported on %s/%s: %v", runtime.GOOS, runtime.GOARCH, err)
  305. }
  306. defer c.Close()
  307. p := ipv4.NewPacketConn(c)
  308. t.Run("ToFrom", func(t *testing.T) {
  309. testPacketConnConcurrentReadWriteUnicast(t, p, payload, c.LocalAddr(), false)
  310. })
  311. t.Run("Batch", func(t *testing.T) {
  312. testPacketConnConcurrentReadWriteUnicast(t, p, payload, c.LocalAddr(), true)
  313. })
  314. })
  315. t.Run("IP", func(t *testing.T) {
  316. switch runtime.GOOS {
  317. case "netbsd":
  318. t.Skip("need to configure gre on netbsd")
  319. case "openbsd":
  320. t.Skip("net.inet.gre.allow=0 by default on openbsd")
  321. }
  322. c, err := net.ListenPacket(fmt.Sprintf("ip4:%d", iana.ProtocolGRE), "127.0.0.1")
  323. if err != nil {
  324. t.Skipf("not supported on %s/%s: %v", runtime.GOOS, runtime.GOARCH, err)
  325. }
  326. defer c.Close()
  327. p := ipv4.NewPacketConn(c)
  328. t.Run("ToFrom", func(t *testing.T) {
  329. testPacketConnConcurrentReadWriteUnicast(t, p, datagram, c.LocalAddr(), false)
  330. })
  331. t.Run("Batch", func(t *testing.T) {
  332. testPacketConnConcurrentReadWriteUnicast(t, p, datagram, c.LocalAddr(), true)
  333. })
  334. })
  335. }
  336. func testPacketConnConcurrentReadWriteUnicast(t *testing.T, p *ipv4.PacketConn, data []byte, dst net.Addr, batch bool) {
  337. t.Helper()
  338. ifi, _ := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagLoopback)
  339. cf := ipv4.FlagTTL | ipv4.FlagSrc | ipv4.FlagDst | ipv4.FlagInterface
  340. if err := p.SetControlMessage(cf, true); err != nil { // probe before test
  341. if protocolNotSupported(err) {
  342. t.Skipf("not supported on %s", runtime.GOOS)
  343. }
  344. t.Fatal(err)
  345. }
  346. var wg sync.WaitGroup
  347. reader := func() {
  348. defer wg.Done()
  349. b := make([]byte, 128)
  350. n, cm, _, err := p.ReadFrom(b)
  351. if err != nil {
  352. t.Error(err)
  353. return
  354. }
  355. if !bytes.Equal(b[:n], data) {
  356. t.Errorf("got %#v; want %#v", b[:n], data)
  357. return
  358. }
  359. s := cm.String()
  360. if strings.Contains(s, ",") {
  361. t.Errorf("should be space-separated values: %s", s)
  362. return
  363. }
  364. }
  365. batchReader := func() {
  366. defer wg.Done()
  367. ms := []ipv4.Message{
  368. {
  369. Buffers: [][]byte{make([]byte, 128)},
  370. OOB: ipv4.NewControlMessage(cf),
  371. },
  372. }
  373. n, err := p.ReadBatch(ms, 0)
  374. if err != nil {
  375. t.Error(err)
  376. return
  377. }
  378. if n != len(ms) {
  379. t.Errorf("got %d; want %d", n, len(ms))
  380. return
  381. }
  382. var cm ipv4.ControlMessage
  383. if err := cm.Parse(ms[0].OOB[:ms[0].NN]); err != nil {
  384. t.Error(err)
  385. return
  386. }
  387. var b []byte
  388. if _, ok := dst.(*net.IPAddr); ok {
  389. var h ipv4.Header
  390. if err := h.Parse(ms[0].Buffers[0][:ms[0].N]); err != nil {
  391. t.Error(err)
  392. return
  393. }
  394. b = ms[0].Buffers[0][h.Len:ms[0].N]
  395. } else {
  396. b = ms[0].Buffers[0][:ms[0].N]
  397. }
  398. if !bytes.Equal(b, data) {
  399. t.Errorf("got %#v; want %#v", b, data)
  400. return
  401. }
  402. s := cm.String()
  403. if strings.Contains(s, ",") {
  404. t.Errorf("should be space-separated values: %s", s)
  405. return
  406. }
  407. }
  408. writer := func(toggle bool) {
  409. defer wg.Done()
  410. cm := ipv4.ControlMessage{
  411. Src: net.IPv4(127, 0, 0, 1),
  412. }
  413. if ifi != nil {
  414. cm.IfIndex = ifi.Index
  415. }
  416. if err := p.SetControlMessage(cf, toggle); err != nil {
  417. t.Error(err)
  418. return
  419. }
  420. n, err := p.WriteTo(data, &cm, dst)
  421. if err != nil {
  422. t.Error(err)
  423. return
  424. }
  425. if n != len(data) {
  426. t.Errorf("got %d; want %d", n, len(data))
  427. return
  428. }
  429. }
  430. batchWriter := func(toggle bool) {
  431. defer wg.Done()
  432. cm := ipv4.ControlMessage{
  433. Src: net.IPv4(127, 0, 0, 1),
  434. }
  435. if ifi != nil {
  436. cm.IfIndex = ifi.Index
  437. }
  438. if err := p.SetControlMessage(cf, toggle); err != nil {
  439. t.Error(err)
  440. return
  441. }
  442. ms := []ipv4.Message{
  443. {
  444. Buffers: [][]byte{data},
  445. OOB: cm.Marshal(),
  446. Addr: dst,
  447. },
  448. }
  449. n, err := p.WriteBatch(ms, 0)
  450. if err != nil {
  451. t.Error(err)
  452. return
  453. }
  454. if n != len(ms) {
  455. t.Errorf("got %d; want %d", n, len(ms))
  456. return
  457. }
  458. if ms[0].N != len(data) {
  459. t.Errorf("got %d; want %d", ms[0].N, len(data))
  460. return
  461. }
  462. }
  463. const N = 10
  464. wg.Add(N)
  465. for i := 0; i < N; i++ {
  466. if batch {
  467. go batchReader()
  468. } else {
  469. go reader()
  470. }
  471. }
  472. wg.Add(2 * N)
  473. for i := 0; i < 2*N; i++ {
  474. if batch {
  475. go batchWriter(i%2 != 0)
  476. } else {
  477. go writer(i%2 != 0)
  478. }
  479. }
  480. wg.Add(N)
  481. for i := 0; i < N; i++ {
  482. if batch {
  483. go batchReader()
  484. } else {
  485. go reader()
  486. }
  487. }
  488. wg.Wait()
  489. }