blob: 4c83a6f88efe5db6cdea2c2c0a3f80f4e973bc70 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02002 * RAW transport layer over SOCK_STREAM sockets.
Willy Tarreaubaaee002006-06-26 02:48:02 +02003 *
Willy Tarreaub277d6e2012-05-11 16:59:14 +02004 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Willy Tarreau6b4aad42009-01-18 21:59:13 +010013#define _GNU_SOURCE
Willy Tarreaubaaee002006-06-26 02:48:02 +020014#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18
19#include <sys/socket.h>
20#include <sys/stat.h>
21#include <sys/types.h>
22
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +040023#include <netinet/tcp.h>
24
Willy Tarreauc7e42382012-08-24 19:22:53 +020025#include <common/buffer.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020026#include <common/compat.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020027#include <common/config.h>
Willy Tarreaud6f087e2008-01-18 17:20:13 +010028#include <common/debug.h>
Willy Tarreau83749182007-04-15 20:56:27 +020029#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020030#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020031#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020032
Willy Tarreau8b117082012-08-06 15:06:49 +020033#include <proto/connection.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020034#include <proto/fd.h>
Willy Tarreaueb472682010-05-28 18:46:57 +020035#include <proto/freq_ctr.h>
36#include <proto/log.h>
Willy Tarreau3eba98a2009-01-25 13:56:13 +010037#include <proto/pipe.h>
Willy Tarreau75bf2c92012-08-20 17:01:35 +020038#include <proto/raw_sock.h>
Willy Tarreau73b013b2012-05-21 16:31:45 +020039#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020040#include <proto/task.h>
41
Willy Tarreau5bd8c372009-01-19 00:32:22 +010042#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020043
Willy Tarreaub277d6e2012-05-11 16:59:14 +020044
Willy Tarreau96199b12012-08-24 00:46:52 +020045#if defined(CONFIG_HAP_LINUX_SPLICE)
Willy Tarreau43d8fb22011-08-22 17:12:02 +020046#include <common/splice.h>
Willy Tarreau5bd8c372009-01-19 00:32:22 +010047
48/* A pipe contains 16 segments max, and it's common to see segments of 1448 bytes
49 * because of timestamps. Use this as a hint for not looping on splice().
50 */
51#define SPLICE_FULL_HINT 16*1448
52
Willy Tarreaua9de3332009-11-28 07:47:10 +010053/* how many data we attempt to splice at once when the buffer is configured for
54 * infinite forwarding */
55#define MAX_SPLICE_AT_ONCE (1<<30)
56
Willy Tarreau5bd8c372009-01-19 00:32:22 +010057/* Returns :
Willy Tarreau96199b12012-08-24 00:46:52 +020058 * -1 if splice() is not supported
59 * >= 0 to report the amount of spliced bytes.
60 * connection flags are updated (error, read0, wait_room, wait_data).
61 * The caller must have previously allocated the pipe.
Willy Tarreau5bd8c372009-01-19 00:32:22 +010062 */
Willy Tarreau96199b12012-08-24 00:46:52 +020063int raw_sock_to_pipe(struct connection *conn, struct pipe *pipe, unsigned int count)
Willy Tarreau5bd8c372009-01-19 00:32:22 +010064{
Willy Tarreau82a04562011-12-11 22:37:06 +010065 static int splice_detects_close;
Willy Tarreau31971e52009-09-20 12:07:52 +020066 int ret;
Willy Tarreauafad0e02012-08-09 14:45:22 +020067 int retval = 0;
Willy Tarreau5bd8c372009-01-19 00:32:22 +010068
Willy Tarreau96199b12012-08-24 00:46:52 +020069 /* Under Linux, if FD_POLL_HUP is set, we have reached the end.
70 * Since older splice() implementations were buggy and returned
71 * EAGAIN on end of read, let's bypass the call to splice() now.
72 */
Willy Tarreau6f5d1412012-10-04 20:38:49 +020073 if (unlikely(!(fdtab[conn->t.sock.fd].ev & FD_POLL_IN))) {
74 /* stop here if we reached the end of data */
75 if ((fdtab[conn->t.sock.fd].ev & (FD_POLL_ERR|FD_POLL_HUP)) == FD_POLL_HUP)
76 goto out_read0;
77
78 /* report error on POLL_ERR before connection establishment */
79 if ((fdtab[conn->t.sock.fd].ev & FD_POLL_ERR) && (conn->flags & CO_FL_WAIT_L4_CONN)) {
80 conn->flags |= CO_FL_ERROR;
81 return retval;
82 }
83 }
Willy Tarreaua9de3332009-11-28 07:47:10 +010084
Willy Tarreau96199b12012-08-24 00:46:52 +020085 while (count) {
86 if (count > MAX_SPLICE_AT_ONCE)
87 count = MAX_SPLICE_AT_ONCE;
Willy Tarreau5bd8c372009-01-19 00:32:22 +010088
Willy Tarreau96199b12012-08-24 00:46:52 +020089 ret = splice(conn->t.sock.fd, NULL, pipe->prod, NULL, count,
Willy Tarreau5bd8c372009-01-19 00:32:22 +010090 SPLICE_F_MOVE|SPLICE_F_NONBLOCK);
91
92 if (ret <= 0) {
Willy Tarreau5bd8c372009-01-19 00:32:22 +010093 if (ret == 0) {
Willy Tarreau98b306b2009-01-25 11:11:32 +010094 /* connection closed. This is only detected by
Willy Tarreau82a04562011-12-11 22:37:06 +010095 * recent kernels (>= 2.6.27.13). If we notice
96 * it works, we store the info for later use.
Willy Tarreau5bd8c372009-01-19 00:32:22 +010097 */
Willy Tarreau82a04562011-12-11 22:37:06 +010098 splice_detects_close = 1;
Willy Tarreau96199b12012-08-24 00:46:52 +020099 goto out_read0;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100100 }
101
102 if (errno == EAGAIN) {
103 /* there are two reasons for EAGAIN :
104 * - nothing in the socket buffer (standard)
105 * - pipe is full
Willy Tarreau98b306b2009-01-25 11:11:32 +0100106 * - the connection is closed (kernel < 2.6.27.13)
Willy Tarreau96199b12012-08-24 00:46:52 +0200107 * The last case is annoying but know if we can detect it
108 * and if we can't then we rely on the call to recv() to
109 * get a valid verdict. The difference between the first
110 * two situations is problematic. Since we don't know if
111 * the pipe is full, we'll stop if the pipe is not empty.
112 * Anyway, we will almost always fill/empty the pipe.
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100113 */
Willy Tarreau96199b12012-08-24 00:46:52 +0200114 if (pipe->data) {
115 /* alway stop reading until the pipe is flushed */
116 conn->flags |= CO_FL_WAIT_ROOM;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100117 break;
118 }
119
Willy Tarreau82a04562011-12-11 22:37:06 +0100120 /* We don't know if the connection was closed,
121 * but if we know splice detects close, then we
122 * know it for sure.
Willy Tarreau98b306b2009-01-25 11:11:32 +0100123 * But if we're called upon POLLIN with an empty
Willy Tarreau82a04562011-12-11 22:37:06 +0100124 * pipe and get EAGAIN, it is suspect enough to
Willy Tarreau98b306b2009-01-25 11:11:32 +0100125 * try to fall back to the normal recv scheme
126 * which will be able to deal with the situation.
127 */
Willy Tarreau82a04562011-12-11 22:37:06 +0100128 if (splice_detects_close)
Willy Tarreau56a77e52012-09-02 18:34:44 +0200129 __conn_data_poll_recv(conn); /* we know for sure that it's EAGAIN */
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100130 break;
131 }
Willy Tarreau45b88932012-11-12 12:00:09 +0100132 else if (errno == ENOSYS || errno == EINVAL || errno == EBADF) {
Willy Tarreau96199b12012-08-24 00:46:52 +0200133 /* splice not supported on this end, disable it.
134 * We can safely return -1 since there is no
135 * chance that any data has been piped yet.
136 */
Willy Tarreaudc340a92009-06-28 23:10:19 +0200137 return -1;
138 }
Willy Tarreau96199b12012-08-24 00:46:52 +0200139 else if (errno == EINTR) {
140 /* try again */
141 continue;
142 }
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100143 /* here we have another error */
Willy Tarreau96199b12012-08-24 00:46:52 +0200144 conn->flags |= CO_FL_ERROR;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100145 break;
146 } /* ret <= 0 */
147
Willy Tarreau96199b12012-08-24 00:46:52 +0200148 retval += ret;
149 pipe->data += ret;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100150
Willy Tarreau96199b12012-08-24 00:46:52 +0200151 if (pipe->data >= SPLICE_FULL_HINT || ret >= global.tune.recv_enough) {
152 /* We've read enough of it for this time, let's stop before
153 * being asked to poll.
154 */
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100155 break;
156 }
157 } /* while */
158
Willy Tarreau665e6ee2012-10-04 20:20:46 +0200159 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && retval)
160 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau96199b12012-08-24 00:46:52 +0200161 return retval;
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100162
Willy Tarreau96199b12012-08-24 00:46:52 +0200163 out_read0:
164 conn_sock_read0(conn);
Willy Tarreau665e6ee2012-10-04 20:20:46 +0200165 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100166 return retval;
167}
168
Willy Tarreau96199b12012-08-24 00:46:52 +0200169/* Send as many bytes as possible from the pipe to the connection's socket.
170 */
171int raw_sock_from_pipe(struct connection *conn, struct pipe *pipe)
172{
173 int ret, done;
174
175 done = 0;
176 while (pipe->data) {
177 ret = splice(pipe->cons, NULL, conn->t.sock.fd, NULL, pipe->data,
178 SPLICE_F_MOVE|SPLICE_F_NONBLOCK);
179
180 if (ret <= 0) {
181 if (ret == 0 || errno == EAGAIN) {
Willy Tarreau56a77e52012-09-02 18:34:44 +0200182 __conn_data_poll_send(conn);
Willy Tarreau96199b12012-08-24 00:46:52 +0200183 break;
184 }
185 else if (errno == EINTR)
186 continue;
187
188 /* here we have another error */
189 conn->flags |= CO_FL_ERROR;
190 break;
191 }
192
193 done += ret;
194 pipe->data -= ret;
195 }
Willy Tarreau665e6ee2012-10-04 20:20:46 +0200196 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done)
197 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau96199b12012-08-24 00:46:52 +0200198 return done;
199}
200
Willy Tarreau6b4aad42009-01-18 21:59:13 +0100201#endif /* CONFIG_HAP_LINUX_SPLICE */
202
203
Willy Tarreau2ba44652012-08-20 17:30:32 +0200204/* Receive up to <count> bytes from connection <conn>'s socket and store them
205 * into buffer <buf>. The caller must ensure that <count> is always smaller
206 * than the buffer's size. Only one call to recv() is performed, unless the
207 * buffer wraps, in which case a second call may be performed. The connection's
208 * flags are updated with whatever special event is detected (error, read0,
209 * empty). The caller is responsible for taking care of those events and
210 * avoiding the call if inappropriate. The function does not call the
211 * connection's polling update function, so the caller is responsible for this.
212 */
213static int raw_sock_to_buf(struct connection *conn, struct buffer *buf, int count)
214{
215 int ret, done = 0;
216 int try = count;
217
Willy Tarreau6f5d1412012-10-04 20:38:49 +0200218 if (unlikely(!(fdtab[conn->t.sock.fd].ev & FD_POLL_IN))) {
219 /* stop here if we reached the end of data */
220 if ((fdtab[conn->t.sock.fd].ev & (FD_POLL_ERR|FD_POLL_HUP)) == FD_POLL_HUP)
221 goto read0;
222
223 /* report error on POLL_ERR before connection establishment */
224 if ((fdtab[conn->t.sock.fd].ev & FD_POLL_ERR) && (conn->flags & CO_FL_WAIT_L4_CONN)) {
225 conn->flags |= CO_FL_ERROR;
226 return done;
227 }
228 }
Willy Tarreau2ba44652012-08-20 17:30:32 +0200229
230 /* compute the maximum block size we can read at once. */
231 if (buffer_empty(buf)) {
232 /* let's realign the buffer to optimize I/O */
233 buf->p = buf->data;
234 }
235 else if (buf->data + buf->o < buf->p &&
236 buf->p + buf->i < buf->data + buf->size) {
237 /* remaining space wraps at the end, with a moving limit */
238 if (try > buf->data + buf->size - (buf->p + buf->i))
239 try = buf->data + buf->size - (buf->p + buf->i);
240 }
241
242 /* read the largest possible block. For this, we perform only one call
243 * to recv() unless the buffer wraps and we exactly fill the first hunk,
244 * in which case we accept to do it once again. A new attempt is made on
245 * EINTR too.
246 */
247 while (try) {
248 ret = recv(conn->t.sock.fd, bi_end(buf), try, 0);
249
250 if (ret > 0) {
251 buf->i += ret;
252 done += ret;
253 if (ret < try) {
254 /* unfortunately, on level-triggered events, POLL_HUP
255 * is generally delivered AFTER the system buffer is
256 * empty, so this one might never match.
257 */
258 if (fdtab[conn->t.sock.fd].ev & FD_POLL_HUP)
259 goto read0;
260 break;
261 }
262 count -= ret;
263 try = count;
264 }
265 else if (ret == 0) {
266 goto read0;
267 }
268 else if (errno == EAGAIN) {
Willy Tarreau56a77e52012-09-02 18:34:44 +0200269 __conn_data_poll_recv(conn);
Willy Tarreau2ba44652012-08-20 17:30:32 +0200270 break;
271 }
272 else if (errno != EINTR) {
273 conn->flags |= CO_FL_ERROR;
274 break;
275 }
276 }
Willy Tarreau665e6ee2012-10-04 20:20:46 +0200277
278 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done)
279 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau2ba44652012-08-20 17:30:32 +0200280 return done;
281
282 read0:
283 conn_sock_read0(conn);
Willy Tarreau665e6ee2012-10-04 20:20:46 +0200284 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreaudebdc4b2012-12-07 00:01:33 +0100285
286 /* Now a final check for a possible asynchronous low-level error
287 * report. This can happen when a connection receives a reset
288 * after a shutdown, both POLL_HUP and POLL_ERR are queued, and
289 * we might have come from there by just checking POLL_HUP instead
290 * of recv()'s return value 0, so we have no way to tell there was
291 * an error without checking.
292 */
293 if (unlikely(fdtab[conn->t.sock.fd].ev & FD_POLL_ERR))
294 conn->flags |= CO_FL_ERROR;
Willy Tarreau2ba44652012-08-20 17:30:32 +0200295 return done;
296}
297
298
Willy Tarreau5368d802012-08-21 18:22:06 +0200299/* Send all pending bytes from buffer <buf> to connection <conn>'s socket.
300 * <flags> may contain MSG_MORE to make the system hold on without sending
301 * data too fast.
302 * Only one call to send() is performed, unless the buffer wraps, in which case
303 * a second call may be performed. The connection's flags are updated with
304 * whatever special event is detected (error, empty). The caller is responsible
305 * for taking care of those events and avoiding the call if inappropriate. The
306 * function does not call the connection's polling update function, so the caller
307 * is responsible for this.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200308 */
Willy Tarreau5368d802012-08-21 18:22:06 +0200309static int raw_sock_from_buf(struct connection *conn, struct buffer *buf, int flags)
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100310{
Willy Tarreau5368d802012-08-21 18:22:06 +0200311 int ret, try, done, send_flag;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200312
Willy Tarreau5368d802012-08-21 18:22:06 +0200313 done = 0;
314 /* send the largest possible block. For this we perform only one call
315 * to send() unless the buffer wraps and we exactly fill the first hunk,
316 * in which case we accept to do it once again.
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100317 */
Willy Tarreau5368d802012-08-21 18:22:06 +0200318 while (buf->o) {
319 try = buf->o;
Willy Tarreau89fa7062012-03-02 16:13:16 +0100320 /* outgoing data may wrap at the end */
Willy Tarreau5368d802012-08-21 18:22:06 +0200321 if (buf->data + try > buf->p)
322 try = buf->data + try - buf->p;
Willy Tarreauf890dc92008-12-13 21:12:26 +0100323
Willy Tarreau5368d802012-08-21 18:22:06 +0200324 send_flag = MSG_DONTWAIT | MSG_NOSIGNAL;
325 if (try < buf->o)
326 send_flag = MSG_MORE;
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200327
Willy Tarreau5368d802012-08-21 18:22:06 +0200328 ret = send(conn->t.sock.fd, bo_ptr(buf), try, send_flag | flags);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200329
330 if (ret > 0) {
Willy Tarreau5368d802012-08-21 18:22:06 +0200331 buf->o -= ret;
332 done += ret;
Willy Tarreaub38903c2008-11-23 21:33:29 +0100333
Willy Tarreau5368d802012-08-21 18:22:06 +0200334 if (likely(!buffer_len(buf)))
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100335 /* optimize data alignment in the buffer */
Willy Tarreau5368d802012-08-21 18:22:06 +0200336 buf->p = buf->data;
Willy Tarreau83749182007-04-15 20:56:27 +0200337
Willy Tarreauab3e1d32007-06-03 14:10:36 +0200338 /* if the system buffer is full, don't insist */
Willy Tarreau5368d802012-08-21 18:22:06 +0200339 if (ret < try)
Willy Tarreau6996e152007-04-30 14:37:43 +0200340 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200341 }
Willy Tarreau0ea0cf62012-11-11 20:38:30 +0100342 else if (ret == 0 || errno == EAGAIN || errno == ENOTCONN) {
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100343 /* nothing written, we need to poll for write first */
Willy Tarreau56a77e52012-09-02 18:34:44 +0200344 __conn_data_poll_send(conn);
Willy Tarreau5368d802012-08-21 18:22:06 +0200345 break;
Willy Tarreau83749182007-04-15 20:56:27 +0200346 }
Willy Tarreau5368d802012-08-21 18:22:06 +0200347 else if (errno != EINTR) {
348 conn->flags |= CO_FL_ERROR;
349 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200350 }
Willy Tarreau5368d802012-08-21 18:22:06 +0200351 }
Willy Tarreau665e6ee2012-10-04 20:20:46 +0200352 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done)
353 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau5368d802012-08-21 18:22:06 +0200354 return done;
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100355}
Willy Tarreau6996e152007-04-30 14:37:43 +0200356
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100357
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200358/* transport-layer operations for RAW sockets */
359struct xprt_ops raw_sock = {
Willy Tarreauc5788912012-08-24 18:12:41 +0200360 .snd_buf = raw_sock_from_buf,
361 .rcv_buf = raw_sock_to_buf,
Willy Tarreau96199b12012-08-24 00:46:52 +0200362#if defined(CONFIG_HAP_LINUX_SPLICE)
363 .rcv_pipe = raw_sock_to_pipe,
364 .snd_pipe = raw_sock_from_pipe,
365#endif
Willy Tarreauc5788912012-08-24 18:12:41 +0200366 .shutr = NULL,
367 .shutw = NULL,
368 .close = NULL,
Willy Tarreau5c979a92012-05-07 17:15:39 +0200369};
Willy Tarreaubaaee002006-06-26 02:48:02 +0200370
371/*
372 * Local variables:
373 * c-indent-level: 8
374 * c-basic-offset: 8
375 * End:
376 */