/share/man/man9/zero_copy.9

https://bitbucket.org/freebsd/freebsd-head/ · Unknown · 168 lines · 168 code · 0 blank · 0 comment · 0 complexity · d2024edc8d67dcfff7ce335976c488d5 MD5 · raw file

  1. .\"
  2. .\" Copyright (c) 2002 Kenneth D. Merry.
  3. .\" All rights reserved.
  4. .\"
  5. .\" Redistribution and use in source and binary forms, with or without
  6. .\" modification, are permitted provided that the following conditions
  7. .\" are met:
  8. .\" 1. Redistributions of source code must retain the above copyright
  9. .\" notice, this list of conditions, and the following disclaimer,
  10. .\" without modification, immediately at the beginning of the file.
  11. .\" 2. The name of the author may not be used to endorse or promote products
  12. .\" derived from this software without specific prior written permission.
  13. .\"
  14. .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  15. .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. .\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
  18. .\" ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19. .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  20. .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  21. .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  22. .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  23. .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  24. .\" SUCH DAMAGE.
  25. .\"
  26. .\" $FreeBSD$
  27. .\"
  28. .Dd December 5, 2004
  29. .Dt ZERO_COPY 9
  30. .Os
  31. .Sh NAME
  32. .Nm zero_copy ,
  33. .Nm zero_copy_sockets
  34. .Nd "zero copy sockets code"
  35. .Sh SYNOPSIS
  36. .Cd "options ZERO_COPY_SOCKETS"
  37. .Sh DESCRIPTION
  38. The
  39. .Fx
  40. kernel includes a facility for eliminating data copies on
  41. socket reads and writes.
  42. .Pp
  43. This code is collectively known as the zero copy sockets code, because during
  44. normal network I/O, data will not be copied by the CPU at all.
  45. Rather it
  46. will be DMAed from the user's buffer to the NIC (for sends), or DMAed from
  47. the NIC to a buffer that will then be given to the user (receives).
  48. .Pp
  49. The zero copy sockets code uses the standard socket read and write
  50. semantics, and therefore has some limitations and restrictions that
  51. programmers should be aware of when trying to take advantage of this
  52. functionality.
  53. .Pp
  54. For sending data, there are no special requirements or capabilities that
  55. the sending NIC must have.
  56. The data written to the socket, though, must be
  57. at least a page in size and page aligned in order to be mapped into the
  58. kernel.
  59. If it does not meet the page size and alignment constraints, it
  60. will be copied into the kernel, as is normally the case with socket I/O.
  61. .Pp
  62. The user should be careful not to overwrite buffers that have been written
  63. to the socket before the data has been freed by the kernel, and the
  64. copy-on-write mapping cleared.
  65. If a buffer is overwritten before it has
  66. been given up by the kernel, the data will be copied, and no savings in CPU
  67. utilization and memory bandwidth utilization will be realized.
  68. .Pp
  69. The
  70. .Xr socket 2
  71. API does not really give the user any indication of when his data has
  72. actually been sent over the wire, or when the data has been freed from
  73. kernel buffers.
  74. For protocols like TCP, the data will be kept around in
  75. the kernel until it has been acknowledged by the other side; it must be
  76. kept until the acknowledgement is received in case retransmission is required.
  77. .Pp
  78. From an application standpoint, the best way to guarantee that the data has
  79. been sent out over the wire and freed by the kernel (for TCP-based sockets)
  80. is to set a socket buffer size (see the
  81. .Dv SO_SNDBUF
  82. socket option in the
  83. .Xr setsockopt 2
  84. manual page) appropriate for the application and network environment and then
  85. make sure you have sent out twice as much data as the socket buffer size
  86. before reusing a buffer.
  87. For TCP, the send and receive socket buffer sizes
  88. generally directly correspond to the TCP window size.
  89. .Pp
  90. For receiving data, in order to take advantage of the zero copy receive
  91. code, the user must have a NIC that is configured for an MTU greater than
  92. the architecture page size.
  93. (E.g., for i386 it would be 4KB.)
  94. Additionally, in order for zero copy receive to work,
  95. packet payloads must be at least a page in size and page aligned.
  96. .Pp
  97. Achieving page aligned payloads requires a NIC that can split an incoming
  98. packet into multiple buffers.
  99. It also generally requires some sort of
  100. intelligence on the NIC to make sure that the payload starts in its own
  101. buffer.
  102. This is called
  103. .Dq "header splitting" .
  104. Currently the only NICs with
  105. support for header splitting are Alteon Tigon 2 based boards running
  106. slightly modified firmware.
  107. The
  108. .Fx
  109. .Xr ti 4
  110. driver includes modified firmware for Tigon 2 boards only.
  111. Header
  112. splitting code can be written, however, for any NIC that allows putting
  113. received packets into multiple buffers and that has enough programmability
  114. to determine that the header should go into one buffer and the payload into
  115. another.
  116. .Pp
  117. You can also do a form of header splitting that does not require any NIC
  118. modifications if your NIC is at least capable of splitting packets into
  119. multiple buffers.
  120. This requires that you optimize the NIC driver for your
  121. most common packet header size.
  122. If that size (ethernet + IP + TCP headers)
  123. is generally 66 bytes, for instance, you would set the first buffer in a
  124. set for a particular packet to be 66 bytes long, and then subsequent
  125. buffers would be a page in size.
  126. For packets that have headers that are
  127. exactly 66 bytes long, your payload will be page aligned.
  128. .Pp
  129. The other requirement for zero copy receive to work is that the buffer that
  130. is the destination for the data read from a socket must be at least a page
  131. in size and page aligned.
  132. .Pp
  133. Obviously the requirements for receive side zero copy are impossible to
  134. meet without NIC hardware that is programmable enough to do header
  135. splitting of some sort.
  136. Since most NICs are not that programmable, or their
  137. manufacturers will not share the source code to their firmware, this approach
  138. to zero copy receive is not widely useful.
  139. .Pp
  140. There are other approaches, such as RDMA and TCP Offload, that may
  141. potentially help alleviate the CPU overhead associated with copying data
  142. out of the kernel.
  143. Most known techniques require some sort of support at
  144. the NIC level to work, and describing such techniques is beyond the scope
  145. of this manual page.
  146. .Pp
  147. The zero copy send and zero copy receive code can be individually turned
  148. off via the
  149. .Va kern.ipc.zero_copy.send
  150. and
  151. .Va kern.ipc.zero_copy.receive
  152. .Nm sysctl
  153. variables respectively.
  154. .Sh SEE ALSO
  155. .Xr sendfile 2 ,
  156. .Xr socket 2 ,
  157. .Xr ti 4
  158. .Sh HISTORY
  159. The zero copy sockets code first appeared in
  160. .Fx 5.0 ,
  161. although it has
  162. been in existence in patch form since at least mid-1999.
  163. .Sh AUTHORS
  164. .An -nosplit
  165. The zero copy sockets code was originally written by
  166. .An Andrew Gallatin Aq gallatin@FreeBSD.org
  167. and substantially modified and updated by
  168. .An Kenneth Merry Aq ken@FreeBSD.org .