blob: 37bea724b151053c6b1f73a75427c5ef93b53486 [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 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 */
73 if ((fdtab[conn->t.sock.fd].ev & (FD_POLL_IN|FD_POLL_HUP)) == FD_POLL_HUP)
74 goto out_read0;
Willy Tarreaua9de3332009-11-28 07:47:10 +010075
Willy Tarreau96199b12012-08-24 00:46:52 +020076 while (count) {
77 if (count > MAX_SPLICE_AT_ONCE)
78 count = MAX_SPLICE_AT_ONCE;
Willy Tarreau5bd8c372009-01-19 00:32:22 +010079
Willy Tarreau96199b12012-08-24 00:46:52 +020080 ret = splice(conn->t.sock.fd, NULL, pipe->prod, NULL, count,
Willy Tarreau5bd8c372009-01-19 00:32:22 +010081 SPLICE_F_MOVE|SPLICE_F_NONBLOCK);
82
83 if (ret <= 0) {
Willy Tarreau5bd8c372009-01-19 00:32:22 +010084 if (ret == 0) {
Willy Tarreau98b306b2009-01-25 11:11:32 +010085 /* connection closed. This is only detected by
Willy Tarreau82a04562011-12-11 22:37:06 +010086 * recent kernels (>= 2.6.27.13). If we notice
87 * it works, we store the info for later use.
Willy Tarreau5bd8c372009-01-19 00:32:22 +010088 */
Willy Tarreau82a04562011-12-11 22:37:06 +010089 splice_detects_close = 1;
Willy Tarreau96199b12012-08-24 00:46:52 +020090 goto out_read0;
Willy Tarreau5bd8c372009-01-19 00:32:22 +010091 }
92
93 if (errno == EAGAIN) {
94 /* there are two reasons for EAGAIN :
95 * - nothing in the socket buffer (standard)
96 * - pipe is full
Willy Tarreau98b306b2009-01-25 11:11:32 +010097 * - the connection is closed (kernel < 2.6.27.13)
Willy Tarreau96199b12012-08-24 00:46:52 +020098 * The last case is annoying but know if we can detect it
99 * and if we can't then we rely on the call to recv() to
100 * get a valid verdict. The difference between the first
101 * two situations is problematic. Since we don't know if
102 * the pipe is full, we'll stop if the pipe is not empty.
103 * Anyway, we will almost always fill/empty the pipe.
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100104 */
Willy Tarreau96199b12012-08-24 00:46:52 +0200105 if (pipe->data) {
106 /* alway stop reading until the pipe is flushed */
107 conn->flags |= CO_FL_WAIT_ROOM;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100108 break;
109 }
110
Willy Tarreau82a04562011-12-11 22:37:06 +0100111 /* We don't know if the connection was closed,
112 * but if we know splice detects close, then we
113 * know it for sure.
Willy Tarreau98b306b2009-01-25 11:11:32 +0100114 * But if we're called upon POLLIN with an empty
Willy Tarreau82a04562011-12-11 22:37:06 +0100115 * pipe and get EAGAIN, it is suspect enough to
Willy Tarreau98b306b2009-01-25 11:11:32 +0100116 * try to fall back to the normal recv scheme
117 * which will be able to deal with the situation.
118 */
Willy Tarreau82a04562011-12-11 22:37:06 +0100119 if (splice_detects_close)
Willy Tarreau56a77e52012-09-02 18:34:44 +0200120 __conn_data_poll_recv(conn); /* we know for sure that it's EAGAIN */
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100121 break;
122 }
Willy Tarreau96199b12012-08-24 00:46:52 +0200123 else if (errno == ENOSYS || errno == EINVAL) {
124 /* splice not supported on this end, disable it.
125 * We can safely return -1 since there is no
126 * chance that any data has been piped yet.
127 */
Willy Tarreaudc340a92009-06-28 23:10:19 +0200128 return -1;
129 }
Willy Tarreau96199b12012-08-24 00:46:52 +0200130 else if (errno == EINTR) {
131 /* try again */
132 continue;
133 }
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100134 /* here we have another error */
Willy Tarreau96199b12012-08-24 00:46:52 +0200135 conn->flags |= CO_FL_ERROR;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100136 break;
137 } /* ret <= 0 */
138
Willy Tarreau96199b12012-08-24 00:46:52 +0200139 retval += ret;
140 pipe->data += ret;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100141
Willy Tarreau96199b12012-08-24 00:46:52 +0200142 if (pipe->data >= SPLICE_FULL_HINT || ret >= global.tune.recv_enough) {
143 /* We've read enough of it for this time, let's stop before
144 * being asked to poll.
145 */
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100146 break;
147 }
148 } /* while */
149
Willy Tarreau96199b12012-08-24 00:46:52 +0200150 return retval;
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100151
Willy Tarreau96199b12012-08-24 00:46:52 +0200152 out_read0:
153 conn_sock_read0(conn);
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100154 return retval;
155}
156
Willy Tarreau96199b12012-08-24 00:46:52 +0200157/* Send as many bytes as possible from the pipe to the connection's socket.
158 */
159int raw_sock_from_pipe(struct connection *conn, struct pipe *pipe)
160{
161 int ret, done;
162
163 done = 0;
164 while (pipe->data) {
165 ret = splice(pipe->cons, NULL, conn->t.sock.fd, NULL, pipe->data,
166 SPLICE_F_MOVE|SPLICE_F_NONBLOCK);
167
168 if (ret <= 0) {
169 if (ret == 0 || errno == EAGAIN) {
Willy Tarreau56a77e52012-09-02 18:34:44 +0200170 __conn_data_poll_send(conn);
Willy Tarreau96199b12012-08-24 00:46:52 +0200171 break;
172 }
173 else if (errno == EINTR)
174 continue;
175
176 /* here we have another error */
177 conn->flags |= CO_FL_ERROR;
178 break;
179 }
180
181 done += ret;
182 pipe->data -= ret;
183 }
184 return done;
185}
186
Willy Tarreau6b4aad42009-01-18 21:59:13 +0100187#endif /* CONFIG_HAP_LINUX_SPLICE */
188
189
Willy Tarreau2ba44652012-08-20 17:30:32 +0200190/* Receive up to <count> bytes from connection <conn>'s socket and store them
191 * into buffer <buf>. The caller must ensure that <count> is always smaller
192 * than the buffer's size. Only one call to recv() is performed, unless the
193 * buffer wraps, in which case a second call may be performed. The connection's
194 * flags are updated with whatever special event is detected (error, read0,
195 * empty). The caller is responsible for taking care of those events and
196 * avoiding the call if inappropriate. The function does not call the
197 * connection's polling update function, so the caller is responsible for this.
198 */
199static int raw_sock_to_buf(struct connection *conn, struct buffer *buf, int count)
200{
201 int ret, done = 0;
202 int try = count;
203
204 /* stop here if we reached the end of data */
205 if ((fdtab[conn->t.sock.fd].ev & (FD_POLL_IN|FD_POLL_HUP)) == FD_POLL_HUP)
206 goto read0;
207
208 /* compute the maximum block size we can read at once. */
209 if (buffer_empty(buf)) {
210 /* let's realign the buffer to optimize I/O */
211 buf->p = buf->data;
212 }
213 else if (buf->data + buf->o < buf->p &&
214 buf->p + buf->i < buf->data + buf->size) {
215 /* remaining space wraps at the end, with a moving limit */
216 if (try > buf->data + buf->size - (buf->p + buf->i))
217 try = buf->data + buf->size - (buf->p + buf->i);
218 }
219
220 /* read the largest possible block. For this, we perform only one call
221 * to recv() unless the buffer wraps and we exactly fill the first hunk,
222 * in which case we accept to do it once again. A new attempt is made on
223 * EINTR too.
224 */
225 while (try) {
226 ret = recv(conn->t.sock.fd, bi_end(buf), try, 0);
227
228 if (ret > 0) {
229 buf->i += ret;
230 done += ret;
231 if (ret < try) {
232 /* unfortunately, on level-triggered events, POLL_HUP
233 * is generally delivered AFTER the system buffer is
234 * empty, so this one might never match.
235 */
236 if (fdtab[conn->t.sock.fd].ev & FD_POLL_HUP)
237 goto read0;
238 break;
239 }
240 count -= ret;
241 try = count;
242 }
243 else if (ret == 0) {
244 goto read0;
245 }
246 else if (errno == EAGAIN) {
Willy Tarreau56a77e52012-09-02 18:34:44 +0200247 __conn_data_poll_recv(conn);
Willy Tarreau2ba44652012-08-20 17:30:32 +0200248 break;
249 }
250 else if (errno != EINTR) {
251 conn->flags |= CO_FL_ERROR;
252 break;
253 }
254 }
255 return done;
256
257 read0:
258 conn_sock_read0(conn);
259 return done;
260}
261
262
Willy Tarreau5368d802012-08-21 18:22:06 +0200263/* Send all pending bytes from buffer <buf> to connection <conn>'s socket.
264 * <flags> may contain MSG_MORE to make the system hold on without sending
265 * data too fast.
266 * Only one call to send() is performed, unless the buffer wraps, in which case
267 * a second call may be performed. The connection's flags are updated with
268 * whatever special event is detected (error, empty). The caller is responsible
269 * for taking care of those events and avoiding the call if inappropriate. The
270 * function does not call the connection's polling update function, so the caller
271 * is responsible for this.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200272 */
Willy Tarreau5368d802012-08-21 18:22:06 +0200273static int raw_sock_from_buf(struct connection *conn, struct buffer *buf, int flags)
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100274{
Willy Tarreau5368d802012-08-21 18:22:06 +0200275 int ret, try, done, send_flag;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200276
Willy Tarreau5368d802012-08-21 18:22:06 +0200277 done = 0;
278 /* send the largest possible block. For this we perform only one call
279 * to send() unless the buffer wraps and we exactly fill the first hunk,
280 * in which case we accept to do it once again.
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100281 */
Willy Tarreau5368d802012-08-21 18:22:06 +0200282 while (buf->o) {
283 try = buf->o;
Willy Tarreau89fa7062012-03-02 16:13:16 +0100284 /* outgoing data may wrap at the end */
Willy Tarreau5368d802012-08-21 18:22:06 +0200285 if (buf->data + try > buf->p)
286 try = buf->data + try - buf->p;
Willy Tarreauf890dc92008-12-13 21:12:26 +0100287
Willy Tarreau5368d802012-08-21 18:22:06 +0200288 send_flag = MSG_DONTWAIT | MSG_NOSIGNAL;
289 if (try < buf->o)
290 send_flag = MSG_MORE;
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200291
Willy Tarreau5368d802012-08-21 18:22:06 +0200292 ret = send(conn->t.sock.fd, bo_ptr(buf), try, send_flag | flags);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200293
294 if (ret > 0) {
Willy Tarreau5368d802012-08-21 18:22:06 +0200295 buf->o -= ret;
296 done += ret;
Willy Tarreaub38903c2008-11-23 21:33:29 +0100297
Willy Tarreau5368d802012-08-21 18:22:06 +0200298 if (likely(!buffer_len(buf)))
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100299 /* optimize data alignment in the buffer */
Willy Tarreau5368d802012-08-21 18:22:06 +0200300 buf->p = buf->data;
Willy Tarreau83749182007-04-15 20:56:27 +0200301
Willy Tarreauab3e1d32007-06-03 14:10:36 +0200302 /* if the system buffer is full, don't insist */
Willy Tarreau5368d802012-08-21 18:22:06 +0200303 if (ret < try)
Willy Tarreau6996e152007-04-30 14:37:43 +0200304 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200305 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200306 else if (ret == 0 || errno == EAGAIN) {
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100307 /* nothing written, we need to poll for write first */
Willy Tarreau56a77e52012-09-02 18:34:44 +0200308 __conn_data_poll_send(conn);
Willy Tarreau5368d802012-08-21 18:22:06 +0200309 break;
Willy Tarreau83749182007-04-15 20:56:27 +0200310 }
Willy Tarreau5368d802012-08-21 18:22:06 +0200311 else if (errno != EINTR) {
312 conn->flags |= CO_FL_ERROR;
313 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200314 }
Willy Tarreau5368d802012-08-21 18:22:06 +0200315 }
316 return done;
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100317}
Willy Tarreau6996e152007-04-30 14:37:43 +0200318
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100319
Willy Tarreauc5788912012-08-24 18:12:41 +0200320/* data-layer operations for RAW sockets */
321struct data_ops raw_sock = {
322 .snd_buf = raw_sock_from_buf,
323 .rcv_buf = raw_sock_to_buf,
Willy Tarreau96199b12012-08-24 00:46:52 +0200324#if defined(CONFIG_HAP_LINUX_SPLICE)
325 .rcv_pipe = raw_sock_to_pipe,
326 .snd_pipe = raw_sock_from_pipe,
327#endif
Willy Tarreauc5788912012-08-24 18:12:41 +0200328 .shutr = NULL,
329 .shutw = NULL,
330 .close = NULL,
Willy Tarreau5c979a92012-05-07 17:15:39 +0200331};
Willy Tarreaubaaee002006-06-26 02:48:02 +0200332
333/*
334 * Local variables:
335 * c-indent-level: 8
336 * c-basic-offset: 8
337 * End:
338 */