Debian Codegraph @

doc/howto/KeepAlives.html

raw symbols SCIP
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <title>ProFTPD: KeepAlives</title>
5 </head>
6
7 <body bgcolor=white>
8
9 <hr>
10 <center><h2><b>ProFTPD: KeepAlives</b></h2></center>
11 <hr>
12
13 <p>
14 <b>Overview of KeepAlive</b><br>
15 Any discussion of so-called "keep-alive" functionality must start by answering
16 the question: "What is does 'keep-alive' mean?" As one specification
17 succintly states:
18 <pre>
19 A "keep-alive" mechanism periodically probes the other end of a connection
20 when the connection is otherwise idle, even when there is no data to be sent.
21 </pre>
22 Keepalive mechanisms appear in many different protocols, under various names.
23 Protocols may be layered over the top of another protocol; for example, HTTPS
24 consists of HTTP layered over SSL/TLS, itself layered over TCP/IP. Each
25 protocol layer may have its own form of such keepalive functionality.
26
27 <p><a name="TCPKeepAlive"></a>
28 <b>TCP KeepAlive</b><br>
29 For TCP, the definitive specification for keepalive functionality is
30 <a href="http://www.faqs.org/rfcs/rfc1122.html">RFC 1122</a>, Section
31 4.2.3.6. While RFC 1122 includes a good discussion of why TCP keepalives are
32 meant to be off by default, in practice TCP keepalives <b>do</b> have value,
33 especially when dealing with network equipment (such as routers, firewalls,
34 NATs) between the client and the server; such equipment might terminate the
35 connection when the connection has been idle for too long. The use of TCP
36 keepalives can help to prevent such network equipment from breaking the
37 connection needlessly.
38
39 <p>
40 <b><i>How TCP KeepAlives Works</i></b><br>
41 OK, so you want to use the TCP keepalive functionality in your program. The
42 question is "How exactly does the TCP keepalive feature work?" Good question.
43 Answering this requires three different numeric values: the <i>idle time</i>,
44 the number of probes to send (the probe <i>count</i>), and the <i>interval
45 time</i> between each probe. Remember, though, that the
46 <code>SO_KEEPALIVE</code> TCP socket option <b>must</b> be enabled on the socket
47 in order for the TCP keepalive feature to be used.
48
49 <p>
50 First, let's assume that you have created a TCP connection, and have transferred
51 data back and forth on that connection. All of the data have been transferred,
52 but you have not <i>closed</i> the connection, so now it is idle. How long
53 does that connection sit idle, with no data transferred <i>at the TCP layer</i>,
54 before one end of the connection or the other starts to wonder whether the
55 connection is still alive? This amount of time where the connection sits idle
56 is the TCP keepalive <i>idle time</i>; the default <i>idle time</i> is two
57 hours (per RFC 1122).
58
59 <p>
60 Our TCP connection has been sitting idle now for amount of time given by the
61 <i>idle time</i> value; what happens then? At this point, the end of the TCP
62 connection with TCP keepalive enabled sends out a "probe". This probe is
63 just a small TCP packet which requires a response from the other side. Once
64 the probe has been sent, the amount of time given by the <i>interval time</i>
65 value passes. If we hear nothing back from the remote peer within the
66 <i>interval time</i>, we send another probe. This process repeats until either
67 <i>a)</i> we receive a response back from the peer, or <i>b)</i> the probe
68 <i>count</i> value has been reached.
69
70 <p>
71 Let us assume that our TCP connection was idle for so long that TCP keepalive
72 probes were sent, and still no response was received. What happens then? At
73 this point, the connection is broken. When the programs at either end of
74 the connection next try to read or write data on that connection, the read/write
75 attempts will fail.
76
77 <p>
78 <b><i>When To Use TCP KeepAlive</i></b><br>
79 When the TCP client is connected directly to the TCP server, it usually does
80 not matter whether one end or the other uses TCP keepalive. As long as
81 <i>one</i> of them does, a broken connection can be detected.
82 <pre>
83 client <-------------------------------> server
84 </pre>
85 However, if the TCP client connects to the TCP server via
86 proxies/routers/firewalls/NAT, the picture changes. When this happens (and
87 it is the common scenario), then <b>both</b> sides may need to use TCP
88 keepalive to learn when their side of the proxied connection is broken:
89 <pre>
90 client <-----------> NAT <-------------> server
91 </pre>
92 In this situation, there are actually <em>two</em> different TCP connections
93 involved: between the client and the NAT, and between the NAT and the server.
94 Each TCP connection may break independently of the other, which is why both
95 ends of the connection (client and server) may need to use TCP keepalives.
96 Use of TCP keepalives <i>also</i> helps here because when the
97 router/firewall/NAT receives the TCP keepalive probe, it <i>may</i> (depending
98 on the network equipment in question) cause the router to reset any timers that
99 were about to close the TCP connections on either side.
100
101 <p>
102 <b><i>Why are TCP KeepAlives Useful for FTP?</i></b><br>
103 "This is all very fascinating", you say, "but what does it have to do with
104 <code>proftpd</code> and FTP?" If you have ever had an FTP download (or upload)
105 take a <i>very long time</i>, only to have that transfer timed out in the
106 middle, then TCP keepalives may prevent the timeout.
107
108 <p>
109 Consider what happens for FTP transfers which take a long time (either due to
110 very large file(s) being transferred, or a slow connection): you have one TCP
111 connection for the control connection, and a <i>separate</i> TCP connection for
112 the data transfer connection. All of the bytes are being transferred over the
113 data connection, so that data connection is certainly not idle -- but while the
114 data transfer is occurring, the <i>control</i> connection is idle! And let's
115 assume that your FTP connections are going through some NAT device in between
116 the client and the server. That NAT may not be very smart; it may not know
117 that the two different TCP connections of your FTP session are related to each
118 other; it only sees one idle TCP connection, and one busy TCP connection. If
119 that FTP control connection is idle for too long, then the NAT may close it
120 (in order to keep valuable space in its state tables available for TCP
121 connections that actually need to transfer bytes). (Some NATs have been known
122 to close TCP connections that have been idle for only 5 minutes.) The FTP
123 server sees that the FTP control connection is closed, and aborts the data
124 transfer. What a mess!
125
126 <p>
127 If either the FTP server or the FTP client had used TCP keepalives on the
128 control connection, then maybe that NAT would have seen the TCP keepalive
129 probes, and <b>not</b> closed the idle control connection. So how can we
130 make sure that either the client or the server has TCP keepalives enabled?
131
132 <p>
133 In <code>proftpd-1.3.5rc1</code> and later, ProFTPD's <a href="../modules/mod_core.html#SocketOptions"><code>SocketOptions</code></a> directive supports a
134 <em>keepalive</em> parameter for controlling whether the server uses TCP
135 keepalives, <i>e.g.</i>:
136 <pre>
137 # Disable use of TCP keepalives
138 SocketOptions keepalive off
139
140 # Enable use of TCP keepalives (this is the default)
141 SocketOptions keepalive on
142 </pre>
143 In addition, on some Unix platforms, the <code>SocketOptions</code>
144 directive's <em>keepalive</em> parameter can do finer-grained tuning of the
145 TCP keepalive values:
146 <pre>
147 # Enable use of TCP keepalives, with the given idle/count/interval values
148 SocketOptions keepalive 7200:9:75
149 </pre>
150 In general, though, you should use the system-wide defaults unless you are
151 running into data transfer timeout issues. If you <b>are</b> seeing timeouts,
152 try using the <em>keepalive</em> parameter of <code>SocketOptions</code> to
153 gradually reduce the idle timeout by small increments (<i>e.g.</i> 10-15
154 seconds), <i>then</i> if that does not help, increment the count by 1 at a
155 time (remember that each probe is more extra data transfer), <i>then</i> if that
156 still does not help, <b>increase</b> the interval time. Do <b>not reduce the
157 interval time</b>, since that is the amount of time that you should wait to
158 see if the other end responds, before sending another probe. Waiting less
159 time before the other end responds means a greater chance of killing your TCP
160 connection unnecessarily.
161
162 <p>
163 Not all TCP stacks let the application control the TCP keepalive timeout after
164 which the first probe will be sent, or the total number of probes sent, or how
165 much time between probes will be used. That is, many TCP stacks <i>only</i>
166 allow enabling/disabling of TCP keepalive. If TCP keepalive <i>is</i> enabled,
167 then the standard values of 2 hours for the idle timeout, a count of 9 probes,
168 with 75 seconds between probes, will be used.
169
170 <p>
171 Since many platforms do not allow fine-grained tuning of TCP keepalive values,
172 especially on a per-service basis, other means for checking whether the
173 connection is still alive must be used. And that leads us to application-level
174 keepalive mechanisms.
175
176 <p><a name="FTPKeepAlive">
177 <b>FTP KeepAlive</b><br>
178 If tuning TCP keepalives does not work to keep your long-lasting data transfers
179 from timing out, what can be done? Answer: use keepalive features at other
180 layers in the protocol stack. FTP has its own ideas for doing keepalive
181 checks, but they are not as elegant as that of TCP.
182
183 <p>
184 The main issue with FTP keepalives is that they are all initiated by the client.
185 FTP is a request/response model, and it does not allow for the FTP server to
186 send arbitrary unrequested data to the FTP client via the control connection.
187 Fortunately, there are many FTP clients which implement some sort of FTP
188 keepalive feature.
189
190 <p>
191 <b><i>How FTP KeepAlive Works</i></b><br>
192 Since the FTP server cannot do anything to test whether the FTP session is
193 alive, the FTP client must do the tests. The easiest way to test whether an
194 FTP session is alive is to send an FTP command. And fortunately,
195 <a href="http://www.faqs.org/rfcs/rfc959.html">RFC 959</a>, Section 4.1.3
196 defines the <code>NOOP</code> ("No Operation") command whose sole purpose is
197 to elicit the "OK" response from the FTP server. This makes the
198 <code>NOOP</code> command the ideal way to test whether the FTP server is
199 still alive and listening to the FTP client.
200
201 <p>
202 Some firewalls/routers know about this <code>NOOP</code> trick, though, and
203 may filter out/drop that FTP command. FTP clients, then, have been known to
204 resort to a number of other FTP commands for use as FTP keepalives, including:
205 <ul>
206 <li><code>NOOP</code>
207 <li><code>LIST</code>
208 <li><code>STAT</code>
209 <li><code>CWD</code>
210 <li><code>REST 0</code>
211 <li><code>PWD</code>
212 <li><code>TYPE A</code>
213 </ul>
214 Some FTP clients even choose a command at random from the above list, just to
215 keep any interfering router/firewall/NAT guessing!
216
217 <p>
218 Sadly, some FTP servers cannot handle receiving an FTP command on the control
219 connection while they are in the middle of transferring data on the data
220 connection (<code>proftpd</code> <b>can</b> handle this). But that may not
221 matter, for the purposes of FTP keepalives; all that matters is that at the
222 TCP level, the bytes were sent by the client and acknowledged by the server's
223 TCP stack.
224
225 <p>
226 <b><i>FTP Client-Specific KeepAlive Settings</i></b><br>
227 Not every FTP client supports the FTP keepalive functionality. If you want
228 to try out FTP clients which <i>do</i> support FTP keepalives, you might look
229 into the <code>ftp:nop-interval</code> setting for
230 <a href="http://lftp.yar.ru/lftp-man.html"><code>lftp</code></a>, or the
231 <code>control-timeout</code> setting for
232 <a href="http://www.ncftp.com/ncftp/doc/ncftp.html#sect5"><code>ncftp</code></a>.
233
234 <p><a name="SSHKeepAlive">
235 <b>SSH KeepAlive</b><br>
236 The SSH2 protocol (and SFTP, which runs over SSH2) is more complex than FTP,
237 and thus has <i>much</i> better support for application-level keepalive
238 functionality. Either end of an SSH2 connection can send messages at any time.
239
240 <p>
241 <b><i>How SSH KeepAlive Works</i></b><br>
242 The usual mechanism used by SSH2 implementations for implementing an SSH2-level
243 keepalive check is send either a <code>CHANNEL_REQUEST</code> or a
244 <code>GLOBAL_REQUEST</code> message for a known unsupported command, and to
245 request a response from the other side. It does not matter that the requested
246 command is unsupported; all that matters is that the response comes back,
247 signifying that the other end is still alive and listening. Both clients and
248 servers use this technique.
249
250 <p>
251 In the case of ProFTPD's <code>mod_sftp</code> module, the way to configure
252 SSH2 keepalives is the <a href="../contrib/mod_sftp.html#SFTPClientAlive"><code>SFTPClientAlive</code></a> directive. When configured, the
253 <code>mod_sftp</code> module sends <code>CHANNEL_REQUEST</code>/<code>GLOBAL_REQUEST</code> messages for "[email protected]" in order to solicit a response
254 from the connected client.
255
256 <p>
257 <b>KeepAlive in Other Protocols</b><br>
258 Many application protocols end up reinventing the keepalive feature in some
259 way, usually as a "ping/pong" mechanism where a "ping" is sent every so often
260 by one side, with a "pong" response expected from the other end of the
261 connection.
262
263 <p>
264 <b><i>HTTP</i></b><br>
265 Most HTTP connections have no need of a keepalive mechanism since HTTP
266 connections are usually short-lived, and since there are usually data flowing
267 in one direction or the other on the HTTP connection (thus an HTTP connection
268 is usually not idle for long enough time to warrant a keepalive feature).
269 HTTP long polling (<i>i.e.</i>
270 <a href="http://www.faqs.org/rfcs/rfc6202.html">RFC 6202</a>) is an exception;
271 and for HTTP long polling connections, use of TCP keepalives may be needed.
272 But the HTTP protocol itself does not specifically define a way for either
273 end to arbitrarily send data across the connection for the purpose of
274 determining whether the connection is still alive. (HTTP keepalive refers
275 to a different concept, <i>i.e.</i> that of telling the server to <b>not</b>
276 close the connection after sending its response so that the connection can
277 be reused, thus "kept alive".)
278
279 <p>
280 <b><i>LDAP</i></b><br>
281 For long-lived LDAP connections, keepalive functionality can be implemented
282 by using the Abandon operation, as described
283 <a href="http://stackoverflow.com/questions/313575/ldap-socket-keep-alive?rq=1">here</a>. The idea is to have the client send a request that it knows the
284 server will ignore/discard; the act of transmitting the request over the
285 connection acts to keep any intermediaries on the network (router/NAT/firewall)
286 from closing an "idle" connection prematurely.
287
288 <p>
289 <b><i>WebSocket</i></b><br>
290 The WebSocket protocol, defined in
291 <a href="http://www.faqs.org/rfcs/rfc6455.html">RFC 6455</a>, does have need
292 of a keepalive mechanism, since it establishes a long-lived connection.
293 Thus does the RFC define ping/pong messages; see Section 5.5.2
294 (<code>PING</code>), and Section 5.5.3 (<code>PONG</code>).
295
296 <p>
297 <b>Additional Reading</b></br>
298 <ul>
299 <li><a name="[1]"><a href="http://www.gnugk.org/keepalive.html">http://www.gnugk.org/keepalive.html</a>
300 <li><a name="[2]"><a href="http://www.starquest.com/Supportdocs/techStarLicense/SL002_TCPKeepAlive.shtml">http://www.starquest.com/Supportdocs/techStarLicense/SL002_TCPKeepAlive.shtml</a>
301 <li><a name="[3]"><a href="http://rolande.wordpress.com/2010/12/30/performance-tuning-the-network-stack-on-mac-osx-10-6/">http://rolande.wordpress.com/2010/12/30/performance-tuning-the-network-stack-on-mac-osx-10-6/</a>
302 <li><a name="[4]"><a href="http://stackoverflow.com/questions/1480236/does-a-tcp-socket-connection-have-a-keep-alive">http://stackoverflow.com/questions/1480236/does-a-tcp-socket-connection-have-a-keep-alive</a>
303 <li><a name="[5]"><a href="http://www.wftpserver.com/kb_keepalive.htm">http://www.wftpserver.com/kb_keepalive.htm</a>
304 <li><a name="[6]"><a href="http://serverfault.com/questions/84310/how-to-prevent-tcp-connection-timeout-when-ftping-large-file">http://serverfault.com/questions/84310/how-to-prevent-tcp-connection-timeout-when-ftping-large-file</a>
305 <li><a name="[7]"><a href="http://stackoverflow.com/questions/5533185/keeping-ftp-control-connection-alive">http://stackoverflow.com/questions/5533185/keeping-ftp-control-connection-alive</a>
306 </ul>
307
308 <p><a name="FAQ">
309 <b>Frequently Asked Questions</b><br>
310 <p>
311 <font color=red>Question</font>: When should I use application-level keepalives like FTP or SSH2 keepalives instead of TCP keepalives? Or can I use them all
312 at the same time?<br>
313 <font color=blue>Answer</font>: There is no issue with using the different
314 types of keepalives at the same time; remember that each type of keepalive
315 functions at a different protocol layer.
316
317 <p>
318 That said, if you have to choose, I would recommend using the application-level
319 keepalive feature when you can, as opposed to TCP keepalives. Why? First,
320 you probably have more control over when the application-level keepalive
321 feature is used than if you used TCP keepalives (<i>e.g.</i> you can use the
322 application-level keepalive check more frequently than the TCP keepalive
323 feature would detect a dead connection).
324
325 <p>
326 Second, TCP keepalives may <b>not</b> cause a NAT to keep the TCP connection
327 on each side open, but an application-level keepalive should do so, since
328 the application-level keepalive data <b>must</b> traverse both connections:
329 <pre>
330 <------------ SSH2 REQUEST ----------->
331 -------------- FTP NOOP -------------->
332 client <---------------> NAT <---------------> server
333 <-- TCP probe --> <-- TCP probe -->
334 </pre>
335
336 <p>
337 <font color=red>Question</font>: Why should I use <code>SocketOptions</code> to
338 configure <code>proftpd</code>'s TCP keepalive settings, as opposed to
339 changing some of the sysctls on my machine that apply to TCP KeepAlives?<br>
340 <font color=blue>Answer</font>: Using the <code>SocketOptions</code> directive
341 means that your TCP keepalive tunings will only affect the FTP/SFTP connections
342 to <code>proftpd</code>, instead of applying to <b>all</b> TCP connections to
343 your machine. FTP/SFTP sessions, being long-lived, probably have different
344 keepalive timing needs than from other TCP connections, so it is best to tune
345 their settings separately, without impacting all connections to that machine.
346
347 <p>
348 <font color=red>Question</font>: If TCP keepalives are so useful, why not
349 tune them to be quite short? Why does RFC 1122 recommend a default of
350 <em>two</em> hours before checking if the peer is still there?<br>
351 <font color=blue>Answer</font>: There are a couple of reasons why you might
352 <b>not</b> want to tune your TCP keepalive settings to be shorter.
353
354 <p>
355 First, keep in mind that TCP was designed to keep the TCP level connection
356 "alive" even though various parts of the underlying hardware, such as
357 routers, firewalls, etc might crash and be rebooted in the middle of things.
358 This is why TCP retransmits lost packets, routes around unresponsive
359 hops, etc. If, then, your TCP keepalive settings are aggressively short,
360 your TCP connection may be shut down (due to not responding to TCP keepalive
361 probes in time) because of a crashed network component. The recommended
362 default of two hours allows for such network route changes <i>without losing
363 the TCP connection</i>.
364
365 <p>
366 Second, every TCP keepalive probe counts as more data transferred over your
367 network link. Some links may have exorbitantly high rates for transferred
368 bytes (think satellite links), so on such links, keeping the number of bytes
369 transferred down amounts to cost savings. Tuning TCP keepalive to use shorter
370 times on such links means <i>more</i> data transferred, thus higher costs.
371 On other links, where data transfer rates are cheap, the additional bytes
372 transferred due to shorter TCP keepalive settings may be negligible.
373
374 <p>
375 Last, many people argue that use of TCP keepalives may consume unnecessary
376 bandwidth. The question becomes "If no one is using the connection, who
377 cares if the connection is still good?" (To be fair, this argument makes more
378 sense when the connected peers are <b>not</b> separated by proxies, gateways,
379 and NATs.)
380
381 <p>
382 <hr>
383 <font size=2><b><i>
384 &copy; Copyright 2017 The ProFTPD Project<br>
385 All Rights Reserved<br>
386 </i></b></font>
387 <hr>
388
389 </body>
390 </html>