| 1 | <!DOCTYPE html> |
| 2 | <html> |
| 3 | <head> |
| 4 | <title>ProFTPD: Sendfile Support</title> |
| 5 | </head> |
| 6 | |
| 7 | <body bgcolor=white> |
| 8 | |
| 9 | <hr> |
| 10 | <center><h2><b>ProFTPD: Sendfile Support</b></h2></center> |
| 11 | <hr> |
| 12 | |
| 13 | <p> |
| 14 | <b>What is "sendfile"?</b><br> |
| 15 | Many Unix kernels provide a function called <code>sendfile(2)</code>, |
| 16 | a system call which provides a "zero-copy" way of copying data from one |
| 17 | file (or socket) descriptor to another. The phrase "zero-copy" refers to the |
| 18 | fact that all of the copying of data between the two descriptors is done |
| 19 | entirely by the kernel, with no copying of data into userspace buffers. |
| 20 | The normal way of copying data between files involves using the |
| 21 | <code>read(2)</code> system call to read data from the source descriptor |
| 22 | into a userspace buffer, then calling the <code>write(2)</code> system call |
| 23 | to write that buffer to the destination descriptor. This copying of the |
| 24 | data <i>twice</i> (once into the userland buffer, and once out from that |
| 25 | userland buffer) imposes some performance and resource penalties. The |
| 26 | <code>sendfile(2)</code> avoids these penalties by avoiding any use of |
| 27 | userland buffers; it also results in a single system call (and thus only |
| 28 | one context switch), rather than the series of |
| 29 | <code>read(2)</code>/<code>write(2)</code> system calls (each system call |
| 30 | requiring a context switch) usually used for data copying. |
| 31 | |
| 32 | <p> |
| 33 | Unix kernels handle a socket as just another file descriptor; this means |
| 34 | that <code>sendfile(2)</code> can be used to efficiently copy data from |
| 35 | a file on disk to a network socket, <i>e.g.</i> for downloading a file. |
| 36 | |
| 37 | <p> |
| 38 | <b>Use of <code>sendfile(2)</code> in ProFTPD</b><br> |
| 39 | For the above reasons, ProFTPD will, by default, attempt to use the |
| 40 | <code>sendfile(2)</code> function for all downloads. However, there are |
| 41 | a few cases where ProFTPD will specifically avoid the use of |
| 42 | <code>sendfile(2)</code>: |
| 43 | <ul> |
| 44 | <li>When data are transferred in <a href="ASCII.html">ASCII</a> mode; |
| 45 | note that this automatically applies to directory listings (which, |
| 46 | by definition, are ASCII transfers) |
| 47 | <li>When RFC2228 data channel protection is in effect (<i>e.g.</i> |
| 48 | <a href="TLS.html">SSL/TLS</a>) |
| 49 | <li>When transfers are being throttled via the <code>TransferRate</code> |
| 50 | directive |
| 51 | <li>When <code>MODE Z</code> data compression is being used (via the |
| 52 | <code>mod_deflate</code> module) |
| 53 | </ul> |
| 54 | The use of <code>sendfile(2)</code> can also be explicitly configured at |
| 55 | run-time by using the following in your <code>proftpd.conf</code> file: |
| 56 | <pre> |
| 57 | UseSendfile off |
| 58 | </pre> |
| 59 | |
| 60 | <p> |
| 61 | Sendfile support in the compiled <code>proftpd</code> daemon can also be |
| 62 | disabled at compile time, by using the <code>--disable-sendfile</code> |
| 63 | configure option, <i>e.g.</i>: |
| 64 | <pre> |
| 65 | $ ./configure --disable-sendfile ... |
| 66 | </pre> |
| 67 | This is not recommended unless necessary. |
| 68 | |
| 69 | <p> |
| 70 | <b>Known Issues with <code>sendfile(2)</code></b><br> |
| 71 | As useful as the <code>sendfile(2)</code> function can be, there are |
| 72 | unfortunately cases where bad implementations of the function can cause |
| 73 | problems. These problems manifest as broken or aborted downloads, or as |
| 74 | downloaded data being corrupted, or even as downloaded files being larger |
| 75 | than the original file. |
| 76 | |
| 77 | <p> |
| 78 | The <code>sendfile(2)</code> function requires support from both the Unix |
| 79 | kernel <i>and</i> from the filesystem drivers which handle the filesystems |
| 80 | for the file descriptors being used. The following operating systems |
| 81 | have had reports of issues when using <code>sendfile(2)</code>: |
| 82 | <ul> |
| 83 | <li><a href="http://forums.proftpd.org/smf/index.php?topic=2860.0">AIX 5.2</a> |
| 84 | <li><a href="http://forums.proftpd.org/smf/index.php?topic=3605.0">Solaris 10 x86</a> |
| 85 | <li>PA-RISC Linux |
| 86 | </ul> |
| 87 | There have also been some issues reported from users of virtualization |
| 88 | technologies such as OpenVZ and VMware. |
| 89 | |
| 90 | <p> |
| 91 | There have been cases where it was the filesystems, rather than the kernels, |
| 92 | which appeared to have been the culprits in <code>sendfile(2)</code> |
| 93 | problems: |
| 94 | <ul> |
| 95 | <li>Network filesystems (<i>e.g</i> NFS, SMBFS/Samba, CIFS) |
| 96 | <li>Virtualized filesystems (OpenVZ, VMware, and even |
| 97 | <a href="http://bugs.proftpd.org/show_bug.cgi?id=2734">Veritas</a>) |
| 98 | <li>Other filesystems (<i>e.g.</i> NTFS and <code>tmpfs</code> on Linux) |
| 99 | </ul> |
| 100 | Again, if you encounter issues with downloading files from ProFTPD when those |
| 101 | files reside on a networked or virtualized filesystem, try using |
| 102 | "UseSendfile off" in your <code>proftpd.conf</code>. |
| 103 | |
| 104 | <p> |
| 105 | Bugs in certain network cards have been reported on Linux, where the use |
| 106 | of <code>sendfile(2)</code> triggers TCP checksum offloading bugs on these |
| 107 | cards when using IPv6. |
| 108 | |
| 109 | <p> |
| 110 | <a href="http://bugs.proftpd.org/show_bug.cgi?id=3081">Bug 3081</a> also |
| 111 | demonstrates an interesting <code>sendfile(2)</code> issue. It is a special |
| 112 | case where the FTP client and server are on the same machine, |
| 113 | <code>sendfile(2)</code> is used when downloading a file, and the client is |
| 114 | downloading the file to the <i>exact same filesystem path</i> from which the |
| 115 | server is reading the file. When this happens, <code>proftpd</code> will |
| 116 | "hang". The fix for this situation is relatively simple: "Don't do that." |
| 117 | |
| 118 | <p> |
| 119 | If your Unix kernel and your filesystems work together to support |
| 120 | the use of <code>sendfile(2)</code> properly, you may run into one last |
| 121 | potential issue with ProFTPD's <code>ftptop</code> and <code>ftpwho</code> |
| 122 | utilities when <code>sendfile(2)</code> is used (at least for versions of |
| 123 | <code>proftpd</code> prior to 1.3.4rc1). These utilities rely on |
| 124 | data in ProFTPD's <a href="Scoreboard.html"><code>ScoreboardFile</code></a> |
| 125 | for providing their information, including transfer rates. The progress |
| 126 | of a file being downloaded is tracked in the <code>ScoreboardFile</code> |
| 127 | as part of the download process; specifically, as part of the normal |
| 128 | <code>read(2)</code>/<code>write(2)</code> loop. When the |
| 129 | <code>sendfile(2)</code> function is used, the <code>ScoreboardFile</code> is |
| 130 | not updated with the progress (since all of the data copying is being handled |
| 131 | by the kernel, not by <code>proftpd</code> code at that point). Thus |
| 132 | for downloads which use <code>sendfile(2)</code>, <code>ftptop</code> and |
| 133 | <code>ftpwho</code> often report the transfer rate as "Inf". |
| 134 | |
| 135 | <p> |
| 136 | The transfer rates for <i>uploads</i> are still reported properly by |
| 137 | <code>ftptop</code> and <code>ftpwho</code>, however. |
| 138 | |
| 139 | <p> |
| 140 | In <code>proftpd-1.3.4rc1</code> and later, the <code>UseSendfile</code> |
| 141 | directive changed. Now you can use <code>UseSendfile</code> in |
| 142 | <code><Directory></code> sections and <code>.ftpaccess</code> files. |
| 143 | This allows sites to disable use of <code>sendfile(2)</code> just for specific |
| 144 | directories/filesystems where it might be a problem, <i>e.g.</i>: |
| 145 | <pre> |
| 146 | <Directory /path/to/nfs/files> |
| 147 | UseSendfile off |
| 148 | </Directory> |
| 149 | </pre> |
| 150 | |
| 151 | <p> |
| 152 | Instead of the usual <em>on/off</em> parameters, the <code>UseSendfile</code> |
| 153 | directive can also take a byte length, or a file size percentage, as parameters. |
| 154 | These parameters can be used so that <code>sendfile(2)</code> is used for |
| 155 | more optimal downloads <b>while still letting <code>ftptop</code> and |
| 156 | <code>ftpwho</code> display download progress rates</b>. To use this, the |
| 157 | best way to use specify a file size percentage such as 10% or 25%; this means |
| 158 | that <code>sendfile(2)</code> will be used for each 10% or 25% percent of |
| 159 | the size of the file being downloaded: |
| 160 | <pre> |
| 161 | <Directory /> |
| 162 | UseSendfile 10% |
| 163 | </Directory> |
| 164 | </pre> |
| 165 | |
| 166 | <p> |
| 167 | <hr> |
| 168 | <font size=2><b><i> |
| 169 | © Copyright 2017 The ProFTPD Project<br> |
| 170 | All Rights Reserved<br> |
| 171 | </i></b></font> |
| 172 | <hr> |
| 173 | |
| 174 | </body> |
| 175 | </html> |