]> CyberLeo.Net >> Repos - FreeBSD/releng/8.0.git/blob - lib/libc/sys/sendfile.2
Adjust to reflect 8.0-RELEASE.
[FreeBSD/releng/8.0.git] / lib / libc / sys / sendfile.2
1 .\" Copyright (c) 2003, David G. Lawrence
2 .\" All rights reserved.
3 .\"
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
6 .\" are met:
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\"    notice unmodified, this list of conditions, and the following
9 .\"    disclaimer.
10 .\" 2. Redistributions in binary form must reproduce the above copyright
11 .\"    notice, this list of conditions and the following disclaimer in the
12 .\"    documentation and/or other materials provided with the distribution.
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
18 .\" FOR 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 November 24, 2006
29 .Dt SENDFILE 2
30 .Os
31 .Sh NAME
32 .Nm sendfile
33 .Nd send a file to a socket
34 .Sh LIBRARY
35 .Lb libc
36 .Sh SYNOPSIS
37 .In sys/types.h
38 .In sys/socket.h
39 .In sys/uio.h
40 .Ft int
41 .Fo sendfile
42 .Fa "int fd" "int s" "off_t offset" "size_t nbytes"
43 .Fa "struct sf_hdtr *hdtr" "off_t *sbytes" "int flags"
44 .Fc
45 .Sh DESCRIPTION
46 The
47 .Fn sendfile
48 system call
49 sends a regular file specified by descriptor
50 .Fa fd
51 out a stream socket specified by descriptor
52 .Fa s .
53 .Pp
54 The
55 .Fa offset
56 argument specifies where to begin in the file.
57 Should
58 .Fa offset
59 fall beyond the end of file, the system will return
60 success and report 0 bytes sent as described below.
61 The
62 .Fa nbytes
63 argument specifies how many bytes of the file should be sent, with 0 having the special
64 meaning of send until the end of file has been reached.
65 .Pp
66 An optional header and/or trailer can be sent before and after the file data by specifying
67 a pointer to a
68 .Vt "struct sf_hdtr" ,
69 which has the following structure:
70 .Pp
71 .Bd -literal -offset indent -compact
72 struct sf_hdtr {
73         struct iovec *headers;  /* pointer to header iovecs */
74         int hdr_cnt;            /* number of header iovecs */
75         struct iovec *trailers; /* pointer to trailer iovecs */
76         int trl_cnt;            /* number of trailer iovecs */
77 };
78 .Ed
79 .Pp
80 The
81 .Fa headers
82 and
83 .Fa trailers
84 pointers, if
85 .Pf non- Dv NULL ,
86 point to arrays of
87 .Vt "struct iovec"
88 structures.
89 See the
90 .Fn writev
91 system call for information on the iovec structure.
92 The number of iovecs in these
93 arrays is specified by
94 .Fa hdr_cnt
95 and
96 .Fa trl_cnt .
97 .Pp
98 If
99 .Pf non- Dv NULL ,
100 the system will write the total number of bytes sent on the socket to the
101 variable pointed to by
102 .Fa sbytes .
103 .Pp
104 The
105 .Fa flags
106 argument is a bitmap of these values:
107 .Bl -item -offset indent 
108 .It
109 .Dv SF_NODISKIO .
110 This flag causes any
111 .Fn sendfile
112 call which would block on disk I/O to instead
113 return
114 .Er EBUSY .
115 Busy servers may benefit by transferring requests that would
116 block to a separate I/O worker thread.
117 .It
118 .Dv SF_MNOWAIT .
119 (description missing)
120 .It
121 .Dv SF_SYNC ,
122 .Nm
123 sleeps until the network stack no longer references the VM pages
124 of the file, making subsequent modifications to it safe.
125 Please note that this is not a guarantee that the data has actually
126 been sent.
127 .El
128 .Pp
129 When using a socket marked for non-blocking I/O,
130 .Fn sendfile
131 may send fewer bytes than requested.
132 In this case, the number of bytes successfully
133 written is returned in
134 .Fa *sbytes
135 (if specified),
136 and the error
137 .Er EAGAIN
138 is returned.
139 .Sh IMPLEMENTATION NOTES
140 The
141 .Fx
142 implementation of
143 .Fn sendfile
144 is "zero-copy", meaning that it has been optimized so that copying of the file data is avoided.
145 .Sh TUNING
146 On some architectures, this system call internally uses a special
147 .Fn sendfile
148 buffer
149 .Pq Vt "struct sf_buf"
150 to handle sending file data to the client.
151 If the sending socket is
152 blocking, and there are not enough
153 .Fn sendfile
154 buffers available,
155 .Fn sendfile
156 will block and report a state of
157 .Dq Li sfbufa .
158 If the sending socket is non-blocking and there are not enough
159 .Fn sendfile
160 buffers available, the call will block and wait for the
161 necessary buffers to become available before finishing the call.
162 .Pp
163 The number of
164 .Vt sf_buf Ns 's
165 allocated should be proportional to the number of nmbclusters used to
166 send data to a client via
167 .Fn sendfile .
168 Tune accordingly to avoid blocking!
169 Busy installations that make extensive use of
170 .Fn sendfile
171 may want to increase these values to be inline with their
172 .Va kern.ipc.nmbclusters
173 (see
174 .Xr tuning 7
175 for details).
176 .Pp
177 The number of
178 .Fn sendfile
179 buffers available is determined at boot time by either the
180 .Va kern.ipc.nsfbufs
181 .Xr loader.conf 5
182 variable or the
183 .Dv NSFBUFS
184 kernel configuration tunable.
185 The number of
186 .Fn sendfile
187 buffers scales with
188 .Va kern.maxusers .
189 The
190 .Va kern.ipc.nsfbufsused
191 and
192 .Va kern.ipc.nsfbufspeak
193 read-only
194 .Xr sysctl 8
195 variables show current and peak
196 .Fn sendfile
197 buffers usage respectively.
198 These values may also be viewed through
199 .Nm netstat Fl m .
200 .Pp
201 If a value of zero is reported for
202 .Va kern.ipc.nsfbufs ,
203 your architecture does not need to use
204 .Fn sendfile
205 buffers because their task can be efficiently performed
206 by the generic virtual memory structures.
207 .Sh RETURN VALUES
208 .Rv -std sendfile
209 .Sh ERRORS
210 .Bl -tag -width Er
211 .It Bq Er EAGAIN
212 The socket is marked for non-blocking I/O and not all data was sent due to
213 the socket buffer being filled.
214 If specified, the number of bytes successfully sent will be returned in
215 .Fa *sbytes .
216 .It Bq Er EBADF
217 The
218 .Fa fd
219 argument
220 is not a valid file descriptor.
221 .It Bq Er EBADF
222 The
223 .Fa s
224 argument
225 is not a valid socket descriptor.
226 .It Bq Er EBUSY
227 Completing the entire transfer would have required disk I/O, so
228 it was aborted.
229 Partial data may have been sent.
230 (This error can only occur when
231 .Dv SF_NODISKIO
232 is specified.)
233 .It Bq Er EFAULT
234 An invalid address was specified for an argument.
235 .It Bq Er EINTR
236 A signal interrupted
237 .Fn sendfile
238 before it could be completed.
239 If specified, the number
240 of bytes successfully sent will be returned in
241 .Fa *sbytes .
242 .It Bq Er EINVAL
243 The
244 .Fa fd
245 argument
246 is not a regular file.
247 .It Bq Er EINVAL
248 The
249 .Fa s
250 argument
251 is not a SOCK_STREAM type socket.
252 .It Bq Er EINVAL
253 The
254 .Fa offset
255 argument
256 is negative.
257 .It Bq Er EIO
258 An error occurred while reading from
259 .Fa fd .
260 .It Bq Er ENOTCONN
261 The
262 .Fa s
263 argument
264 points to an unconnected socket.
265 .It Bq Er ENOTSOCK
266 The
267 .Fa s
268 argument
269 is not a socket.
270 .It Bq Er EOPNOTSUPP
271 The file system for descriptor
272 .Fa fd
273 does not support
274 .Fn sendfile .
275 .It Bq Er EPIPE
276 The socket peer has closed the connection.
277 .El
278 .Sh SEE ALSO
279 .Xr netstat 1 ,
280 .Xr open 2 ,
281 .Xr send 2 ,
282 .Xr socket 2 ,
283 .Xr writev 2 ,
284 .Xr tuning 7
285 .Rs
286 .%A K. Elmeleegy
287 .%A A. Chanda
288 .%A A. L. Cox
289 .%A W. Zwaenepoel
290 .%T A Portable Kernel Abstraction for Low-Overhead Ephemeral Mapping Management
291 .%J The Proceedings of the 2005 USENIX Annual Technical Conference
292 .%P pp 223-236
293 .%D 2005
294 .Re
295 .Sh HISTORY
296 The
297 .Fn sendfile
298 system call
299 first appeared in
300 .Fx 3.0 .
301 This manual page first appeared in
302 .Fx 3.1 .
303 .Sh AUTHORS
304 The
305 .Fn sendfile
306 system call
307 and this manual page were written by
308 .An David G. Lawrence Aq dg@dglawrence.com .