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