udp_linux_32.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //go:build linux && (386 || amd64p32 || arm || mips || mipsle) && !android && !e2e_testing
  2. // +build linux
  3. // +build 386 amd64p32 arm mips mipsle
  4. // +build !android
  5. // +build !e2e_testing
  6. package udp
  7. import (
  8. "golang.org/x/sys/unix"
  9. )
  10. type iovec struct {
  11. Base *byte
  12. Len uint32
  13. }
  14. type msghdr struct {
  15. Name *byte
  16. Namelen uint32
  17. Iov *iovec
  18. Iovlen uint32
  19. Control *byte
  20. Controllen uint32
  21. Flags int32
  22. }
  23. type rawMessage struct {
  24. Hdr msghdr
  25. Len uint32
  26. }
  27. func (u *StdConn) PrepareRawMessages(n int) ([]rawMessage, [][]byte, [][]byte, [][]byte) {
  28. msgs := make([]rawMessage, n)
  29. buffers := make([][]byte, n)
  30. names := make([][]byte, n)
  31. controls := make([][]byte, n)
  32. // Use larger buffers if GRO is enabled to hold coalesced packets
  33. bufSize := MTU
  34. if u.groSupported {
  35. bufSize = groMaxPacketSize
  36. }
  37. // Control buffer size for receiving UDP_GRO segment size
  38. controlSize := 0
  39. if u.groSupported {
  40. controlSize = unix.CmsgSpace(2) // space for uint16 segment size
  41. }
  42. for i := range msgs {
  43. buffers[i] = make([]byte, bufSize)
  44. names[i] = make([]byte, unix.SizeofSockaddrInet6)
  45. vs := []iovec{
  46. {Base: &buffers[i][0], Len: uint32(len(buffers[i]))},
  47. }
  48. msgs[i].Hdr.Iov = &vs[0]
  49. msgs[i].Hdr.Iovlen = uint32(len(vs))
  50. msgs[i].Hdr.Name = &names[i][0]
  51. msgs[i].Hdr.Namelen = uint32(len(names[i]))
  52. // Set up control message buffer for GRO
  53. if controlSize > 0 {
  54. controls[i] = make([]byte, controlSize)
  55. msgs[i].Hdr.Control = &controls[i][0]
  56. msgs[i].Hdr.Controllen = uint32(controlSize)
  57. }
  58. }
  59. return msgs, buffers, names, controls
  60. }
  61. func setIovecBase(iov *iovec, base *byte) {
  62. iov.Base = base
  63. }
  64. func setIovecLen(iov *iovec, l int) {
  65. iov.Len = uint32(l)
  66. }
  67. func setMsghdrIovlen(hdr *msghdr, l int) {
  68. hdr.Iovlen = uint32(l)
  69. }
  70. func setMsghdrControllen(hdr *msghdr, l int) {
  71. hdr.Controllen = uint32(l)
  72. }
  73. func getMsghdrControllen(hdr *msghdr) int {
  74. return int(hdr.Controllen)
  75. }