2 .\" Copyright (c) 2002 Kenneth D. Merry.
3 .\" All rights reserved.
5 .\" Redistribution and use in source and binary forms, with or without
6 .\" modification, are permitted provided that the following conditions
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.
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
34 .Nd "zero copy sockets code"
36 .Cd "options ZERO_COPY_SOCKETS"
40 kernel includes a facility for eliminating data copies on
41 socket reads and writes.
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.
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).
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
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
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.
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.
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
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.
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
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.
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.
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
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.
110 driver includes modified firmware for Tigon 2 boards only.
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
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
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.
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.
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.
140 There are other approaches, such as RDMA and TCP Offload, that may
141 potentially help alleviate the CPU overhead associated with copying data
143 Most known techniques require some sort of support at
144 the NIC level to work, and describing such techniques is beyond the scope
147 The zero copy send and zero copy receive code can be individually turned
149 .Va kern.ipc.zero_copy.send
151 .Va kern.ipc.zero_copy.receive
153 variables respectively.
159 The zero copy sockets code first appeared in
162 been in existence in patch form since at least mid-1999.
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 .