blob: ea2fa6684646a956ab3647ac3ae40138ca63dda8 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
Willy Tarreaub277d6e2012-05-11 16:59:14 +02002 * Functions used to send/receive data using 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 Tarreaufe598a72010-09-21 21:48:23 +020038#include <proto/protocols.h>
Willy Tarreau75bf2c92012-08-20 17:01:35 +020039#include <proto/raw_sock.h>
Willy Tarreau73b013b2012-05-21 16:31:45 +020040#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020041#include <proto/task.h>
42
Willy Tarreau5bd8c372009-01-19 00:32:22 +010043#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020044
Willy Tarreaub277d6e2012-05-11 16:59:14 +020045
Willy Tarreau96199b12012-08-24 00:46:52 +020046#if defined(CONFIG_HAP_LINUX_SPLICE)
Willy Tarreau43d8fb22011-08-22 17:12:02 +020047#include <common/splice.h>
Willy Tarreau5bd8c372009-01-19 00:32:22 +010048
49/* A pipe contains 16 segments max, and it's common to see segments of 1448 bytes
50 * because of timestamps. Use this as a hint for not looping on splice().
51 */
52#define SPLICE_FULL_HINT 16*1448
53
Willy Tarreaua9de3332009-11-28 07:47:10 +010054/* how many data we attempt to splice at once when the buffer is configured for
55 * infinite forwarding */
56#define MAX_SPLICE_AT_ONCE (1<<30)
57
Willy Tarreau5bd8c372009-01-19 00:32:22 +010058/* Returns :
Willy Tarreau96199b12012-08-24 00:46:52 +020059 * -1 if splice() is not supported
60 * >= 0 to report the amount of spliced bytes.
61 * connection flags are updated (error, read0, wait_room, wait_data).
62 * The caller must have previously allocated the pipe.
Willy Tarreau5bd8c372009-01-19 00:32:22 +010063 */
Willy Tarreau96199b12012-08-24 00:46:52 +020064int raw_sock_to_pipe(struct connection *conn, struct pipe *pipe, unsigned int count)
Willy Tarreau5bd8c372009-01-19 00:32:22 +010065{
Willy Tarreau82a04562011-12-11 22:37:06 +010066 static int splice_detects_close;
Willy Tarreau31971e52009-09-20 12:07:52 +020067 int ret;
Willy Tarreauafad0e02012-08-09 14:45:22 +020068 int retval = 0;
Willy Tarreau5bd8c372009-01-19 00:32:22 +010069
Willy Tarreau96199b12012-08-24 00:46:52 +020070 /* Under Linux, if FD_POLL_HUP is set, we have reached the end.
71 * Since older splice() implementations were buggy and returned
72 * EAGAIN on end of read, let's bypass the call to splice() now.
73 */
74 if ((fdtab[conn->t.sock.fd].ev & (FD_POLL_IN|FD_POLL_HUP)) == FD_POLL_HUP)
75 goto out_read0;
Willy Tarreaua9de3332009-11-28 07:47:10 +010076
Willy Tarreau96199b12012-08-24 00:46:52 +020077 while (count) {
78 if (count > MAX_SPLICE_AT_ONCE)
79 count = MAX_SPLICE_AT_ONCE;
Willy Tarreau5bd8c372009-01-19 00:32:22 +010080
Willy Tarreau96199b12012-08-24 00:46:52 +020081 ret = splice(conn->t.sock.fd, NULL, pipe->prod, NULL, count,
Willy Tarreau5bd8c372009-01-19 00:32:22 +010082 SPLICE_F_MOVE|SPLICE_F_NONBLOCK);
83
84 if (ret <= 0) {
Willy Tarreau5bd8c372009-01-19 00:32:22 +010085 if (ret == 0) {
Willy Tarreau98b306b2009-01-25 11:11:32 +010086 /* connection closed. This is only detected by
Willy Tarreau82a04562011-12-11 22:37:06 +010087 * recent kernels (>= 2.6.27.13). If we notice
88 * it works, we store the info for later use.
Willy Tarreau5bd8c372009-01-19 00:32:22 +010089 */
Willy Tarreau82a04562011-12-11 22:37:06 +010090 splice_detects_close = 1;
Willy Tarreau96199b12012-08-24 00:46:52 +020091 goto out_read0;
Willy Tarreau5bd8c372009-01-19 00:32:22 +010092 }
93
94 if (errno == EAGAIN) {
95 /* there are two reasons for EAGAIN :
96 * - nothing in the socket buffer (standard)
97 * - pipe is full
Willy Tarreau98b306b2009-01-25 11:11:32 +010098 * - the connection is closed (kernel < 2.6.27.13)
Willy Tarreau96199b12012-08-24 00:46:52 +020099 * The last case is annoying but know if we can detect it
100 * and if we can't then we rely on the call to recv() to
101 * get a valid verdict. The difference between the first
102 * two situations is problematic. Since we don't know if
103 * the pipe is full, we'll stop if the pipe is not empty.
104 * Anyway, we will almost always fill/empty the pipe.
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100105 */
Willy Tarreau96199b12012-08-24 00:46:52 +0200106 if (pipe->data) {
107 /* alway stop reading until the pipe is flushed */
108 conn->flags |= CO_FL_WAIT_ROOM;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100109 break;
110 }
111
Willy Tarreau82a04562011-12-11 22:37:06 +0100112 /* We don't know if the connection was closed,
113 * but if we know splice detects close, then we
114 * know it for sure.
Willy Tarreau98b306b2009-01-25 11:11:32 +0100115 * But if we're called upon POLLIN with an empty
Willy Tarreau82a04562011-12-11 22:37:06 +0100116 * pipe and get EAGAIN, it is suspect enough to
Willy Tarreau98b306b2009-01-25 11:11:32 +0100117 * try to fall back to the normal recv scheme
118 * which will be able to deal with the situation.
119 */
Willy Tarreau82a04562011-12-11 22:37:06 +0100120 if (splice_detects_close)
Willy Tarreau56a77e52012-09-02 18:34:44 +0200121 __conn_data_poll_recv(conn); /* we know for sure that it's EAGAIN */
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100122 break;
123 }
Willy Tarreau96199b12012-08-24 00:46:52 +0200124 else if (errno == ENOSYS || errno == EINVAL) {
125 /* splice not supported on this end, disable it.
126 * We can safely return -1 since there is no
127 * chance that any data has been piped yet.
128 */
Willy Tarreaudc340a92009-06-28 23:10:19 +0200129 return -1;
130 }
Willy Tarreau96199b12012-08-24 00:46:52 +0200131 else if (errno == EINTR) {
132 /* try again */
133 continue;
134 }
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100135 /* here we have another error */
Willy Tarreau96199b12012-08-24 00:46:52 +0200136 conn->flags |= CO_FL_ERROR;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100137 break;
138 } /* ret <= 0 */
139
Willy Tarreau96199b12012-08-24 00:46:52 +0200140 retval += ret;
141 pipe->data += ret;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100142
Willy Tarreau96199b12012-08-24 00:46:52 +0200143 if (pipe->data >= SPLICE_FULL_HINT || ret >= global.tune.recv_enough) {
144 /* We've read enough of it for this time, let's stop before
145 * being asked to poll.
146 */
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100147 break;
148 }
149 } /* while */
150
Willy Tarreau96199b12012-08-24 00:46:52 +0200151 return retval;
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100152
Willy Tarreau96199b12012-08-24 00:46:52 +0200153 out_read0:
154 conn_sock_read0(conn);
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100155 return retval;
156}
157
Willy Tarreau96199b12012-08-24 00:46:52 +0200158/* Send as many bytes as possible from the pipe to the connection's socket.
159 */
160int raw_sock_from_pipe(struct connection *conn, struct pipe *pipe)
161{
162 int ret, done;
163
164 done = 0;
165 while (pipe->data) {
166 ret = splice(pipe->cons, NULL, conn->t.sock.fd, NULL, pipe->data,
167 SPLICE_F_MOVE|SPLICE_F_NONBLOCK);
168
169 if (ret <= 0) {
170 if (ret == 0 || errno == EAGAIN) {
Willy Tarreau56a77e52012-09-02 18:34:44 +0200171 __conn_data_poll_send(conn);
Willy Tarreau96199b12012-08-24 00:46:52 +0200172 break;
173 }
174 else if (errno == EINTR)
175 continue;
176
177 /* here we have another error */
178 conn->flags |= CO_FL_ERROR;
179 break;
180 }
181
182 done += ret;
183 pipe->data -= ret;
184 }
185 return done;
186}
187
Willy Tarreau6b4aad42009-01-18 21:59:13 +0100188#endif /* CONFIG_HAP_LINUX_SPLICE */
189
190
Willy Tarreau2ba44652012-08-20 17:30:32 +0200191/* Receive up to <count> bytes from connection <conn>'s socket and store them
192 * into buffer <buf>. The caller must ensure that <count> is always smaller
193 * than the buffer's size. Only one call to recv() is performed, unless the
194 * buffer wraps, in which case a second call may be performed. The connection's
195 * flags are updated with whatever special event is detected (error, read0,
196 * empty). The caller is responsible for taking care of those events and
197 * avoiding the call if inappropriate. The function does not call the
198 * connection's polling update function, so the caller is responsible for this.
199 */
200static int raw_sock_to_buf(struct connection *conn, struct buffer *buf, int count)
201{
202 int ret, done = 0;
203 int try = count;
204
205 /* stop here if we reached the end of data */
206 if ((fdtab[conn->t.sock.fd].ev & (FD_POLL_IN|FD_POLL_HUP)) == FD_POLL_HUP)
207 goto read0;
208
209 /* compute the maximum block size we can read at once. */
210 if (buffer_empty(buf)) {
211 /* let's realign the buffer to optimize I/O */
212 buf->p = buf->data;
213 }
214 else if (buf->data + buf->o < buf->p &&
215 buf->p + buf->i < buf->data + buf->size) {
216 /* remaining space wraps at the end, with a moving limit */
217 if (try > buf->data + buf->size - (buf->p + buf->i))
218 try = buf->data + buf->size - (buf->p + buf->i);
219 }
220
221 /* read the largest possible block. For this, we perform only one call
222 * to recv() unless the buffer wraps and we exactly fill the first hunk,
223 * in which case we accept to do it once again. A new attempt is made on
224 * EINTR too.
225 */
226 while (try) {
227 ret = recv(conn->t.sock.fd, bi_end(buf), try, 0);
228
229 if (ret > 0) {
230 buf->i += ret;
231 done += ret;
232 if (ret < try) {
233 /* unfortunately, on level-triggered events, POLL_HUP
234 * is generally delivered AFTER the system buffer is
235 * empty, so this one might never match.
236 */
237 if (fdtab[conn->t.sock.fd].ev & FD_POLL_HUP)
238 goto read0;
239 break;
240 }
241 count -= ret;
242 try = count;
243 }
244 else if (ret == 0) {
245 goto read0;
246 }
247 else if (errno == EAGAIN) {
Willy Tarreau56a77e52012-09-02 18:34:44 +0200248 __conn_data_poll_recv(conn);
Willy Tarreau2ba44652012-08-20 17:30:32 +0200249 break;
250 }
251 else if (errno != EINTR) {
252 conn->flags |= CO_FL_ERROR;
253 break;
254 }
255 }
256 return done;
257
258 read0:
259 conn_sock_read0(conn);
260 return done;
261}
262
263
Willy Tarreau5368d802012-08-21 18:22:06 +0200264/* Send all pending bytes from buffer <buf> to connection <conn>'s socket.
265 * <flags> may contain MSG_MORE to make the system hold on without sending
266 * data too fast.
267 * Only one call to send() is performed, unless the buffer wraps, in which case
268 * a second call may be performed. The connection's flags are updated with
269 * whatever special event is detected (error, empty). The caller is responsible
270 * for taking care of those events and avoiding the call if inappropriate. The
271 * function does not call the connection's polling update function, so the caller
272 * is responsible for this.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200273 */
Willy Tarreau5368d802012-08-21 18:22:06 +0200274static int raw_sock_from_buf(struct connection *conn, struct buffer *buf, int flags)
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100275{
Willy Tarreau5368d802012-08-21 18:22:06 +0200276 int ret, try, done, send_flag;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200277
Willy Tarreau5368d802012-08-21 18:22:06 +0200278 done = 0;
279 /* send the largest possible block. For this we perform only one call
280 * to send() unless the buffer wraps and we exactly fill the first hunk,
281 * in which case we accept to do it once again.
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100282 */
Willy Tarreau5368d802012-08-21 18:22:06 +0200283 while (buf->o) {
284 try = buf->o;
Willy Tarreau89fa7062012-03-02 16:13:16 +0100285 /* outgoing data may wrap at the end */
Willy Tarreau5368d802012-08-21 18:22:06 +0200286 if (buf->data + try > buf->p)
287 try = buf->data + try - buf->p;
Willy Tarreauf890dc92008-12-13 21:12:26 +0100288
Willy Tarreau5368d802012-08-21 18:22:06 +0200289 send_flag = MSG_DONTWAIT | MSG_NOSIGNAL;
290 if (try < buf->o)
291 send_flag = MSG_MORE;
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200292
Willy Tarreau5368d802012-08-21 18:22:06 +0200293 ret = send(conn->t.sock.fd, bo_ptr(buf), try, send_flag | flags);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200294
295 if (ret > 0) {
Willy Tarreau5368d802012-08-21 18:22:06 +0200296 buf->o -= ret;
297 done += ret;
Willy Tarreaub38903c2008-11-23 21:33:29 +0100298
Willy Tarreau5368d802012-08-21 18:22:06 +0200299 if (likely(!buffer_len(buf)))
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100300 /* optimize data alignment in the buffer */
Willy Tarreau5368d802012-08-21 18:22:06 +0200301 buf->p = buf->data;
Willy Tarreau83749182007-04-15 20:56:27 +0200302
Willy Tarreauab3e1d32007-06-03 14:10:36 +0200303 /* if the system buffer is full, don't insist */
Willy Tarreau5368d802012-08-21 18:22:06 +0200304 if (ret < try)
Willy Tarreau6996e152007-04-30 14:37:43 +0200305 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200306 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200307 else if (ret == 0 || errno == EAGAIN) {
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100308 /* nothing written, we need to poll for write first */
Willy Tarreau56a77e52012-09-02 18:34:44 +0200309 __conn_data_poll_send(conn);
Willy Tarreau5368d802012-08-21 18:22:06 +0200310 break;
Willy Tarreau83749182007-04-15 20:56:27 +0200311 }
Willy Tarreau5368d802012-08-21 18:22:06 +0200312 else if (errno != EINTR) {
313 conn->flags |= CO_FL_ERROR;
314 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200315 }
Willy Tarreau5368d802012-08-21 18:22:06 +0200316 }
317 return done;
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100318}
Willy Tarreau6996e152007-04-30 14:37:43 +0200319
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100320
Willy Tarreauc5788912012-08-24 18:12:41 +0200321/* data-layer operations for RAW sockets */
322struct data_ops raw_sock = {
323 .snd_buf = raw_sock_from_buf,
324 .rcv_buf = raw_sock_to_buf,
Willy Tarreau96199b12012-08-24 00:46:52 +0200325#if defined(CONFIG_HAP_LINUX_SPLICE)
326 .rcv_pipe = raw_sock_to_pipe,
327 .snd_pipe = raw_sock_from_pipe,
328#endif
Willy Tarreauc5788912012-08-24 18:12:41 +0200329 .shutr = NULL,
330 .shutw = NULL,
331 .close = NULL,
Willy Tarreau5c979a92012-05-07 17:15:39 +0200332};
Willy Tarreaubaaee002006-06-26 02:48:02 +0200333
334/*
335 * Local variables:
336 * c-indent-level: 8
337 * c-basic-offset: 8
338 * End:
339 */