]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - lib/libc/sys/sendfile.2
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.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 January 7, 2010
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 Do not wait for some kernel resource to become available,
120 in particular,
121 .Vt mbuf
122 and
123 .Vt sf_buf .
124 The flag does not make the
125 .Fn sendfile
126 syscall truly non-blocking, since other resources are still allocated
127 in a blocking fashion.
128 .It
129 .Dv SF_SYNC .
130 .Nm
131 sleeps until the network stack no longer references the VM pages
132 of the file, making subsequent modifications to it safe.
133 Please note that this is not a guarantee that the data has actually
134 been sent.
135 .El
136 .Pp
137 When using a socket marked for non-blocking I/O,
138 .Fn sendfile
139 may send fewer bytes than requested.
140 In this case, the number of bytes successfully
141 written is returned in
142 .Fa *sbytes
143 (if specified),
144 and the error
145 .Er EAGAIN
146 is returned.
147 .Sh IMPLEMENTATION NOTES
148 The
149 .Fx
150 implementation of
151 .Fn sendfile
152 is "zero-copy", meaning that it has been optimized so that copying of the file data is avoided.
153 .Sh TUNING
154 On some architectures, this system call internally uses a special
155 .Fn sendfile
156 buffer
157 .Pq Vt "struct sf_buf"
158 to handle sending file data to the client.
159 If the sending socket is
160 blocking, and there are not enough
161 .Fn sendfile
162 buffers available,
163 .Fn sendfile
164 will block and report a state of
165 .Dq Li sfbufa .
166 If the sending socket is non-blocking and there are not enough
167 .Fn sendfile
168 buffers available, the call will block and wait for the
169 necessary buffers to become available before finishing the call.
170 .Pp
171 The number of
172 .Vt sf_buf Ns 's
173 allocated should be proportional to the number of nmbclusters used to
174 send data to a client via
175 .Fn sendfile .
176 Tune accordingly to avoid blocking!
177 Busy installations that make extensive use of
178 .Fn sendfile
179 may want to increase these values to be inline with their
180 .Va kern.ipc.nmbclusters
181 (see
182 .Xr tuning 7
183 for details).
184 .Pp
185 The number of
186 .Fn sendfile
187 buffers available is determined at boot time by either the
188 .Va kern.ipc.nsfbufs
189 .Xr loader.conf 5
190 variable or the
191 .Dv NSFBUFS
192 kernel configuration tunable.
193 The number of
194 .Fn sendfile
195 buffers scales with
196 .Va kern.maxusers .
197 The
198 .Va kern.ipc.nsfbufsused
199 and
200 .Va kern.ipc.nsfbufspeak
201 read-only
202 .Xr sysctl 8
203 variables show current and peak
204 .Fn sendfile
205 buffers usage respectively.
206 These values may also be viewed through
207 .Nm netstat Fl m .
208 .Pp
209 If a value of zero is reported for
210 .Va kern.ipc.nsfbufs ,
211 your architecture does not need to use
212 .Fn sendfile
213 buffers because their task can be efficiently performed
214 by the generic virtual memory structures.
215 .Sh RETURN VALUES
216 .Rv -std sendfile
217 .Sh ERRORS
218 .Bl -tag -width Er
219 .It Bq Er EAGAIN
220 The socket is marked for non-blocking I/O and not all data was sent due to
221 the socket buffer being filled.
222 If specified, the number of bytes successfully sent will be returned in
223 .Fa *sbytes .
224 .It Bq Er EBADF
225 The
226 .Fa fd
227 argument
228 is not a valid file descriptor.
229 .It Bq Er EBADF
230 The
231 .Fa s
232 argument
233 is not a valid socket descriptor.
234 .It Bq Er EBUSY
235 Completing the entire transfer would have required disk I/O, so
236 it was aborted.
237 Partial data may have been sent.
238 (This error can only occur when
239 .Dv SF_NODISKIO
240 is specified.)
241 .It Bq Er EFAULT
242 An invalid address was specified for an argument.
243 .It Bq Er EINTR
244 A signal interrupted
245 .Fn sendfile
246 before it could be completed.
247 If specified, the number
248 of bytes successfully sent will be returned in
249 .Fa *sbytes .
250 .It Bq Er EINVAL
251 The
252 .Fa fd
253 argument
254 is not a regular file.
255 .It Bq Er EINVAL
256 The
257 .Fa s
258 argument
259 is not a SOCK_STREAM type socket.
260 .It Bq Er EINVAL
261 The
262 .Fa offset
263 argument
264 is negative.
265 .It Bq Er EIO
266 An error occurred while reading from
267 .Fa fd .
268 .It Bq Er ENOTCONN
269 The
270 .Fa s
271 argument
272 points to an unconnected socket.
273 .It Bq Er ENOTSOCK
274 The
275 .Fa s
276 argument
277 is not a socket.
278 .It Bq Er EOPNOTSUPP
279 The file system for descriptor
280 .Fa fd
281 does not support
282 .Fn sendfile .
283 .It Bq Er EPIPE
284 The socket peer has closed the connection.
285 .El
286 .Sh SEE ALSO
287 .Xr netstat 1 ,
288 .Xr open 2 ,
289 .Xr send 2 ,
290 .Xr socket 2 ,
291 .Xr writev 2 ,
292 .Xr tuning 7
293 .Rs
294 .%A K. Elmeleegy
295 .%A A. Chanda
296 .%A A. L. Cox
297 .%A W. Zwaenepoel
298 .%T A Portable Kernel Abstraction for Low-Overhead Ephemeral Mapping Management
299 .%J The Proceedings of the 2005 USENIX Annual Technical Conference
300 .%P pp 223-236
301 .%D 2005
302 .Re
303 .Sh HISTORY
304 The
305 .Fn sendfile
306 system call
307 first appeared in
308 .Fx 3.0 .
309 This manual page first appeared in
310 .Fx 3.1 .
311 .Sh AUTHORS
312 The
313 .Fn sendfile
314 system call
315 and this manual page were written by
316 .An David G. Lawrence Aq dg@dglawrence.com .