]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libc/sys/sendfile.2
MFC 359465: Document EINTEGRITY errors for many system calls.
[FreeBSD/FreeBSD.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 March 30, 2020
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 or shared memory object 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 least significant 16 bits of
105 .Fa flags
106 argument is a bitmap of these values:
107 .Bl -tag -offset indent -width "SF_USER_READAHEAD"
108 .It Dv SF_NODISKIO
109 This flag causes
110 .Nm
111 to return
112 .Er EBUSY
113 instead of blocking when a busy page is encountered.
114 This rare situation can happen if some other process is now working
115 with the same region of the file.
116 It is advised to retry the operation after a short period.
117 .Pp
118 Note that in older
119 .Fx
120 versions the
121 .Dv SF_NODISKIO
122 had slightly different notion.
123 The flag prevented
124 .Nm
125 to run I/O operations in case if an invalid (not cached) page is encountered,
126 thus avoiding blocking on I/O.
127 Starting with
128 .Fx 11
129 .Nm
130 sending files off the
131 .Xr ffs 7
132 filesystem does not block on I/O
133 (see
134 .Sx IMPLEMENTATION NOTES
135 ), so the condition no longer applies.
136 However, it is safe if an application utilizes
137 .Dv SF_NODISKIO
138 and on
139 .Er EBUSY
140 performs the same action as it did in
141 older
142 .Fx
143 versions, e.g.,
144 .Xr aio_read 2 ,
145 .Xr read 2
146 or
147 .Nm
148 in a different context.
149 .It Dv SF_NOCACHE
150 The data sent to socket will not be cached by the virtual memory system,
151 and will be freed directly to the pool of free pages.
152 .It Dv SF_SYNC
153 .Nm
154 sleeps until the network stack no longer references the VM pages
155 of the file, making subsequent modifications to it safe.
156 Please note that this is not a guarantee that the data has actually
157 been sent.
158 .It Dv SF_USER_READAHEAD
159 .Nm
160 has some internal heuristics to do readahead when sending data.
161 This flag forces
162 .Nm
163 to override any heuristically calculated readahead and use exactly the
164 application specified readahead.
165 See
166 .Sx SETTING READAHEAD
167 for more details on readahead.
168 .El
169 .Pp
170 When using a socket marked for non-blocking I/O,
171 .Fn sendfile
172 may send fewer bytes than requested.
173 In this case, the number of bytes successfully
174 written is returned in
175 .Fa *sbytes
176 (if specified),
177 and the error
178 .Er EAGAIN
179 is returned.
180 .Sh SETTING READAHEAD
181 .Nm
182 uses internal heuristics based on request size and file system layout
183 to do readahead.
184 Additionally application may request extra readahead.
185 The most significant 16 bits of
186 .Fa flags
187 specify amount of pages that
188 .Nm
189 may read ahead when reading the file.
190 A macro
191 .Fn SF_FLAGS
192 is provided to combine readahead amount and flags.
193 An example showing specifying readahead of 16 pages and
194 .Dv SF_NOCACHE
195 flag:
196 .Pp
197 .Bd -literal -offset indent -compact
198         SF_FLAGS(16, SF_NOCACHE)
199 .Ed
200 .Pp
201 .Nm
202 will use either application specified readahead or internally calculated,
203 whichever is bigger.
204 Setting flag
205 .Dv SF_USER_READAHEAD
206 would turn off any heuristics and set maximum possible readahead length to
207 the number of pages specified via flags.
208 .Sh IMPLEMENTATION NOTES
209 The
210 .Fx
211 implementation of
212 .Fn sendfile
213 does not block on disk I/O when it sends a file off the
214 .Xr ffs 7
215 filesystem.
216 The syscall returns success before the actual I/O completes, and data
217 is put into the socket later unattended.
218 However, the order of data in the socket is preserved, so it is safe
219 to do further writes to the socket.
220 .Pp
221 The
222 .Fx
223 implementation of
224 .Fn sendfile
225 is "zero-copy", meaning that it has been optimized so that copying of the file data is avoided.
226 .Sh TUNING
227 On some architectures, this system call internally uses a special
228 .Fn sendfile
229 buffer
230 .Pq Vt "struct sf_buf"
231 to handle sending file data to the client.
232 If the sending socket is
233 blocking, and there are not enough
234 .Fn sendfile
235 buffers available,
236 .Fn sendfile
237 will block and report a state of
238 .Dq Li sfbufa .
239 If the sending socket is non-blocking and there are not enough
240 .Fn sendfile
241 buffers available, the call will block and wait for the
242 necessary buffers to become available before finishing the call.
243 .Pp
244 The number of
245 .Vt sf_buf Ns 's
246 allocated should be proportional to the number of nmbclusters used to
247 send data to a client via
248 .Fn sendfile .
249 Tune accordingly to avoid blocking!
250 Busy installations that make extensive use of
251 .Fn sendfile
252 may want to increase these values to be inline with their
253 .Va kern.ipc.nmbclusters
254 (see
255 .Xr tuning 7
256 for details).
257 .Pp
258 The number of
259 .Fn sendfile
260 buffers available is determined at boot time by either the
261 .Va kern.ipc.nsfbufs
262 .Xr loader.conf 5
263 variable or the
264 .Dv NSFBUFS
265 kernel configuration tunable.
266 The number of
267 .Fn sendfile
268 buffers scales with
269 .Va kern.maxusers .
270 The
271 .Va kern.ipc.nsfbufsused
272 and
273 .Va kern.ipc.nsfbufspeak
274 read-only
275 .Xr sysctl 8
276 variables show current and peak
277 .Fn sendfile
278 buffers usage respectively.
279 These values may also be viewed through
280 .Nm netstat Fl m .
281 .Pp
282 If a value of zero is reported for
283 .Va kern.ipc.nsfbufs ,
284 your architecture does not need to use
285 .Fn sendfile
286 buffers because their task can be efficiently performed
287 by the generic virtual memory structures.
288 .Sh RETURN VALUES
289 .Rv -std sendfile
290 .Sh ERRORS
291 .Bl -tag -width Er
292 .It Bq Er EAGAIN
293 The socket is marked for non-blocking I/O and not all data was sent due to
294 the socket buffer being filled.
295 If specified, the number of bytes successfully sent will be returned in
296 .Fa *sbytes .
297 .It Bq Er EBADF
298 The
299 .Fa fd
300 argument
301 is not a valid file descriptor.
302 .It Bq Er EBADF
303 The
304 .Fa s
305 argument
306 is not a valid socket descriptor.
307 .It Bq Er EBUSY
308 A busy page was encountered and
309 .Dv SF_NODISKIO
310 had been specified.
311 Partial data may have been sent.
312 .It Bq Er EFAULT
313 An invalid address was specified for an argument.
314 .It Bq Er EINTR
315 A signal interrupted
316 .Fn sendfile
317 before it could be completed.
318 If specified, the number
319 of bytes successfully sent will be returned in
320 .Fa *sbytes .
321 .It Bq Er EINVAL
322 The
323 .Fa fd
324 argument
325 is not a regular file.
326 .It Bq Er EINVAL
327 The
328 .Fa s
329 argument
330 is not a SOCK_STREAM type socket.
331 .It Bq Er EINVAL
332 The
333 .Fa offset
334 argument
335 is negative.
336 .It Bq Er EIO
337 An error occurred while reading from
338 .Fa fd .
339 .It Bq Er EINTEGRITY
340 Corrupted data was detected while reading from
341 .Fa fd .
342 .It Bq Er ENOTCAPABLE
343 The
344 .Fa fd
345 or the
346 .Fa s
347 argument has insufficient rights.
348 .It Bq Er ENOBUFS
349 The system was unable to allocate an internal buffer.
350 .It Bq Er ENOTCONN
351 The
352 .Fa s
353 argument
354 points to an unconnected socket.
355 .It Bq Er ENOTSOCK
356 The
357 .Fa s
358 argument
359 is not a socket.
360 .It Bq Er EOPNOTSUPP
361 The file system for descriptor
362 .Fa fd
363 does not support
364 .Fn sendfile .
365 .It Bq Er EPIPE
366 The socket peer has closed the connection.
367 .El
368 .Sh SEE ALSO
369 .Xr netstat 1 ,
370 .Xr open 2 ,
371 .Xr send 2 ,
372 .Xr socket 2 ,
373 .Xr writev 2 ,
374 .Xr tuning 7
375 .Rs
376 .%A K. Elmeleegy
377 .%A A. Chanda
378 .%A A. L. Cox
379 .%A W. Zwaenepoel
380 .%T A Portable Kernel Abstraction for Low-Overhead Ephemeral Mapping Management
381 .%J The Proceedings of the 2005 USENIX Annual Technical Conference
382 .%P pp 223-236
383 .%D 2005
384 .Re
385 .Sh HISTORY
386 The
387 .Fn sendfile
388 system call
389 first appeared in
390 .Fx 3.0 .
391 This manual page first appeared in
392 .Fx 3.1 .
393 In
394 .Fx 10
395 support for sending shared memory descriptors had been introduced.
396 In
397 .Fx 11
398 a non-blocking implementation had been introduced.
399 .Sh AUTHORS
400 The initial implementation of
401 .Fn sendfile
402 system call
403 and this manual page were written by
404 .An David G. Lawrence Aq Mt dg@dglawrence.com .
405 The
406 .Fx 11
407 implementation was written by
408 .An Gleb Smirnoff Aq Mt glebius@FreeBSD.org .
409 .Sh BUGS
410 The
411 .Fn sendfile
412 system call will not fail, i.e., return
413 .Dv -1
414 and set
415 .Va errno
416 to
417 .Er EFAULT ,
418 if provided an invalid address for
419 .Fa sbytes .
420 The
421 .Fn sendfile
422 system call does not support SCTP sockets,
423 it will return
424 .Dv -1
425 and set
426 .Va errno
427 to
428 .Er EINVAL.