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 5, 1998 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.Fn sendfile "int fd" "int s" "off_t offset" "size_t nbytes" "struct sf_hdtr *hdtr" "off_t *sbytes" "int flags" 42.Sh DESCRIPTION 43The 44.Fn sendfile 45system call 46sends a regular file specified by descriptor 47.Fa fd 48out a stream socket specified by descriptor 49.Fa s . 50.Pp 51The 52.Fa offset 53argument specifies where to begin in the file. 54The 55.Fa nbytes 56argument specifies how many bytes of the file should be sent, with 0 having the special 57meaning of send until the end of file has been reached. 58.Pp 59An optional header and/or trailer can be sent before and after the file data by specifying 60a pointer to a struct sf_hdtr, which has the following structure: 61.Pp 62.Bd -literal -offset indent -compact 63struct sf_hdtr { 64 struct iovec *headers; /* pointer to header iovecs */ 65 int hdr_cnt; /* number of header iovecs */ 66 struct iovec *trailers; /* pointer to trailer iovecs */ 67 int trl_cnt; /* number of trailer iovecs */ 68}; 69.Ed 70.Pp 71The 72.Fa headers 73and 74.Fa trailers 75pointers, if non-NULL, point to arrays of struct iovec structures. 76See the 77.Fn writev 78system call for information on the iovec structure. 79The number of iovecs in these 80arrays is specified by 81.Fa hdr_cnt 82and 83.Fa trl_cnt . 84.Pp 85If non-NULL, the system will write the total number of bytes sent on the socket to the 86variable pointed to by 87.Fa sbytes . 88.Pp 89The 90.Fa flags 91argument has one possible value: 92.Dv SF_NODISKIO . 93This flag causes any 94.Fn sendfile 95call which would block on disk I/O to instead 96return 97.Er EBUSY . 98Busy servers may benefit by transferring requests that would 99block to a separate I/O worker thread. 100.Pp 101When using a socket marked for non-blocking I/O, 102.Fn sendfile 103may send fewer bytes than requested. 104In this case, the number of bytes successfully 105written is returned in 106.Fa *sbytes 107(if specified), 108and the error 109.Er EAGAIN 110is returned. 111.Sh IMPLEMENTATION NOTES 112The 113.Fx 114implementation of 115.Fn sendfile 116is "zero-copy", meaning that it has been optimized so that copying of the file data is avoided. 117.Sh TUNING 118Internally, this system call uses a special 119.Fn sendfile 120buffer 121.Pq Vt "struct sf_buf" 122to handle sending file data to the client. 123If the sending socket is 124blocking, and there are not enough 125.Fn sendfile 126buffers available, 127.Fn sendfile 128will block and report a state of 129.Dq Li sfbufa . 130If the sending socket is non-blocking and there are not enough 131.Fn sendfile 132buffers available, the call will block and wait for the 133necessary buffers to become available before finishing the call. 134.Pp 135The number of 136.Vt sf_buf Ns 's 137allocated should be proportional to the number of nmbclusters used to 138send data to a client via 139.Fn sendfile . 140Tune accordingly to avoid blocking! 141Busy installations that make extensive use of 142.Fn sendfile 143may want to increase these values to be inline with their 144.Va kern.ipc.nmbclusters 145(see 146.Xr tuning 7 147for details). 148.Pp 149The number of 150.Fn sendfile 151buffers available is determined at boot time by either the 152.Va kern.ipc.nsfbufs 153.Xr loader.conf 5 154variable or the 155.Dv NSFBUFS 156kernel configuration tunable. 157The number of 158.Fn sendfile 159buffers scales with 160.Va kern.maxusers . 161The 162.Va kern.ipc.nsfbufsused 163and 164.Va kern.ipc.nsfbufspeak 165read-only 166.Xr sysctl 8 167variables show current and peak 168.Fn sendfile 169buffers usage respectively. 170These values may also be viewed through 171.Nm netstat Fl m . 172.Sh RETURN VALUES 173.Rv -std sendfile 174.Sh ERRORS 175.Bl -tag -width Er 176.It Bq Er EBADF 177The 178.Fa fd 179argument 180is not a valid file descriptor. 181.It Bq Er EBADF 182The 183.Fa s 184argument 185is not a valid socket descriptor. 186.It Bq Er EBUSY 187Completing the entire transfer would have required disk I/O, so 188it was aborted. 189Partial data may have been sent. 190(This error can only occur when 191.Dv SF_NODISKIO 192is specified.) 193.It Bq Er ENOTSOCK 194The 195.Fa s 196argument 197is not a socket. 198.It Bq Er EINVAL 199The 200.Fa fd 201argument 202is not a regular file. 203.It Bq Er EINVAL 204The 205.Fa s 206argument 207is not a SOCK_STREAM type socket. 208.It Bq Er EINVAL 209The 210.Fa offset 211argument 212is negative or out of range. 213.It Bq Er ENOTCONN 214The 215.Fa s 216argument 217points to an unconnected socket. 218.It Bq Er EPIPE 219The socket peer has closed the connection. 220.It Bq Er EIO 221An error occurred while reading from 222.Fa fd . 223.It Bq Er EFAULT 224An invalid address was specified for an argument. 225.It Bq Er EINTR 226A signal interrupted 227.Fn sendfile 228before it could be completed. 229If specified, the number 230of bytes successfully sent will be returned in 231.Fa *sbytes . 232.It Bq Er EAGAIN 233The socket is marked for non-blocking I/O and not all data was sent due to the socket buffer being filled. 234If specified, the number of bytes successfully sent will be returned in 235.Fa *sbytes . 236.El 237.Sh SEE ALSO 238.Xr netstat 1 , 239.Xr open 2 , 240.Xr send 2 , 241.Xr socket 2 , 242.Xr writev 2 , 243.Xr tuning 7 244.Sh HISTORY 245The 246.Fn sendfile 247system call 248first appeared in 249.Fx 3.0 . 250This manual page first appeared in 251.Fx 3.1 . 252.Sh AUTHORS 253The 254.Fn sendfile 255system call 256and this manual page were written by 257.An David G. Lawrence Aq [email protected] . 258