blob: 7dd90c9468ed078d6a7c36e2b1df51ca61cfe338 [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 Tarreau2dd0d472006-06-29 17:53:05 +020025#include <common/compat.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020026#include <common/config.h>
Willy Tarreaud6f087e2008-01-18 17:20:13 +010027#include <common/debug.h>
Willy Tarreau83749182007-04-15 20:56:27 +020028#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020029#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020030#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020031
Willy Tarreau2d212792008-08-27 21:41:35 +020032#include <proto/buffers.h>
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/* main event functions used to move data between sockets and buffers */
Willy Tarreauafad0e02012-08-09 14:45:22 +020046static void sock_raw_read(struct connection *conn);
Willy Tarreaub277d6e2012-05-11 16:59:14 +020047
48
Willy Tarreauaf978c42012-08-20 20:21:01 +020049#if 0 && defined(CONFIG_HAP_LINUX_SPLICE)
Willy Tarreau43d8fb22011-08-22 17:12:02 +020050#include <common/splice.h>
Willy Tarreau5bd8c372009-01-19 00:32:22 +010051
52/* A pipe contains 16 segments max, and it's common to see segments of 1448 bytes
53 * because of timestamps. Use this as a hint for not looping on splice().
54 */
55#define SPLICE_FULL_HINT 16*1448
56
Willy Tarreaua9de3332009-11-28 07:47:10 +010057/* how many data we attempt to splice at once when the buffer is configured for
58 * infinite forwarding */
59#define MAX_SPLICE_AT_ONCE (1<<30)
60
Willy Tarreau5bd8c372009-01-19 00:32:22 +010061/* Returns :
62 * -1 if splice is not possible or not possible anymore and we must switch to
63 * user-land copy (eg: to_forward reached)
Willy Tarreauafad0e02012-08-09 14:45:22 +020064 * 0 otherwise, including errors and close.
Willy Tarreau5bd8c372009-01-19 00:32:22 +010065 * Sets :
66 * BF_READ_NULL
67 * BF_READ_PARTIAL
68 * BF_WRITE_PARTIAL (during copy)
Willy Tarreauba0b63d2009-09-20 08:09:44 +020069 * BF_OUT_EMPTY (during copy)
Willy Tarreau5bd8c372009-01-19 00:32:22 +010070 * SI_FL_ERR
71 * SI_FL_WAIT_ROOM
72 * (SI_FL_WAIT_RECV)
Willy Tarreau3eba98a2009-01-25 13:56:13 +010073 *
74 * This function automatically allocates a pipe from the pipe pool. It also
75 * carefully ensures to clear b->pipe whenever it leaves the pipe empty.
Willy Tarreau5bd8c372009-01-19 00:32:22 +010076 */
Willy Tarreau7421efb2012-07-02 15:11:27 +020077static int sock_raw_splice_in(struct channel *b, struct stream_interface *si)
Willy Tarreau5bd8c372009-01-19 00:32:22 +010078{
Willy Tarreau82a04562011-12-11 22:37:06 +010079 static int splice_detects_close;
Willy Tarreaufb7508a2012-05-21 16:47:54 +020080 int fd = si_fd(si);
Willy Tarreau31971e52009-09-20 12:07:52 +020081 int ret;
82 unsigned long max;
Willy Tarreauafad0e02012-08-09 14:45:22 +020083 int retval = 0;
Willy Tarreau5bd8c372009-01-19 00:32:22 +010084
85 if (!b->to_forward)
86 return -1;
87
88 if (!(b->flags & BF_KERN_SPLICING))
89 return -1;
90
Willy Tarreau572bf902012-07-02 17:01:20 +020091 if (buffer_not_empty(&b->buf)) {
Willy Tarreau5bd8c372009-01-19 00:32:22 +010092 /* We're embarrassed, there are already data pending in
93 * the buffer and we don't want to have them at two
94 * locations at a time. Let's indicate we need some
95 * place and ask the consumer to hurry.
96 */
97 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreauf9dabec2012-08-17 17:33:53 +020098 conn_data_stop_recv(&si->conn);
Willy Tarreau5bd8c372009-01-19 00:32:22 +010099 b->rex = TICK_ETERNITY;
Willy Tarreau73b013b2012-05-21 16:31:45 +0200100 si_chk_snd(b->cons);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200101 return 0;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100102 }
103
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100104 if (unlikely(b->pipe == NULL)) {
105 if (pipes_used >= global.maxpipes || !(b->pipe = get_pipe())) {
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100106 b->flags &= ~BF_KERN_SPLICING;
107 return -1;
108 }
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100109 }
110
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100111 /* At this point, b->pipe is valid */
112
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100113 while (1) {
Willy Tarreaua9de3332009-11-28 07:47:10 +0100114 if (b->to_forward == BUF_INFINITE_FORWARD)
115 max = MAX_SPLICE_AT_ONCE;
116 else
117 max = b->to_forward;
118
Willy Tarreau31971e52009-09-20 12:07:52 +0200119 if (!max) {
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100120 /* It looks like the buffer + the pipe already contain
121 * the maximum amount of data to be transferred. Try to
122 * send those data immediately on the other side if it
123 * is currently waiting.
124 */
125 retval = -1; /* end of forwarding */
126 break;
127 }
128
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100129 ret = splice(fd, NULL, b->pipe->prod, NULL, max,
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100130 SPLICE_F_MOVE|SPLICE_F_NONBLOCK);
131
132 if (ret <= 0) {
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100133 if (ret == 0) {
Willy Tarreau98b306b2009-01-25 11:11:32 +0100134 /* connection closed. This is only detected by
Willy Tarreau82a04562011-12-11 22:37:06 +0100135 * recent kernels (>= 2.6.27.13). If we notice
136 * it works, we store the info for later use.
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100137 */
Willy Tarreau82a04562011-12-11 22:37:06 +0100138 splice_detects_close = 1;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100139 b->flags |= BF_READ_NULL;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100140 break;
141 }
142
143 if (errno == EAGAIN) {
144 /* there are two reasons for EAGAIN :
145 * - nothing in the socket buffer (standard)
146 * - pipe is full
Willy Tarreau98b306b2009-01-25 11:11:32 +0100147 * - the connection is closed (kernel < 2.6.27.13)
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100148 * Since we don't know if pipe is full, we'll
149 * stop if the pipe is not empty. Anyway, we
150 * will almost always fill/empty the pipe.
151 */
152
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100153 if (b->pipe->data) {
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100154 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100155 break;
156 }
157
Willy Tarreau82a04562011-12-11 22:37:06 +0100158 /* We don't know if the connection was closed,
159 * but if we know splice detects close, then we
160 * know it for sure.
Willy Tarreau98b306b2009-01-25 11:11:32 +0100161 * But if we're called upon POLLIN with an empty
Willy Tarreau82a04562011-12-11 22:37:06 +0100162 * pipe and get EAGAIN, it is suspect enough to
Willy Tarreau98b306b2009-01-25 11:11:32 +0100163 * try to fall back to the normal recv scheme
164 * which will be able to deal with the situation.
165 */
Willy Tarreau82a04562011-12-11 22:37:06 +0100166 if (splice_detects_close)
Willy Tarreauafad0e02012-08-09 14:45:22 +0200167 conn_data_poll_recv(&si->conn); /* we know for sure that it's EAGAIN */
Willy Tarreau82a04562011-12-11 22:37:06 +0100168 else
169 retval = -1;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100170 break;
171 }
Willy Tarreaudc340a92009-06-28 23:10:19 +0200172
Willy Tarreaua9de3332009-11-28 07:47:10 +0100173 if (errno == ENOSYS || errno == EINVAL) {
Willy Tarreaudc340a92009-06-28 23:10:19 +0200174 /* splice not supported on this end, disable it */
175 b->flags &= ~BF_KERN_SPLICING;
176 si->flags &= ~SI_FL_CAP_SPLICE;
177 put_pipe(b->pipe);
178 b->pipe = NULL;
179 return -1;
180 }
181
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100182 /* here we have another error */
183 si->flags |= SI_FL_ERR;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100184 break;
185 } /* ret <= 0 */
186
Willy Tarreau31971e52009-09-20 12:07:52 +0200187 if (b->to_forward != BUF_INFINITE_FORWARD)
188 b->to_forward -= ret;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100189 b->total += ret;
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100190 b->pipe->data += ret;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100191 b->flags |= BF_READ_PARTIAL;
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200192 b->flags &= ~BF_OUT_EMPTY;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100193
Willy Tarreau6f4a82c2009-03-21 20:43:57 +0100194 if (b->pipe->data >= SPLICE_FULL_HINT ||
195 ret >= global.tune.recv_enough) {
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100196 /* We've read enough of it for this time. */
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100197 break;
198 }
199 } /* while */
200
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100201 if (unlikely(!b->pipe->data)) {
202 put_pipe(b->pipe);
203 b->pipe = NULL;
204 }
205
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100206 return retval;
207}
208
Willy Tarreau6b4aad42009-01-18 21:59:13 +0100209#endif /* CONFIG_HAP_LINUX_SPLICE */
210
211
Willy Tarreau2ba44652012-08-20 17:30:32 +0200212/* Receive up to <count> bytes from connection <conn>'s socket and store them
213 * into buffer <buf>. The caller must ensure that <count> is always smaller
214 * than the buffer's size. Only one call to recv() is performed, unless the
215 * buffer wraps, in which case a second call may be performed. The connection's
216 * flags are updated with whatever special event is detected (error, read0,
217 * empty). The caller is responsible for taking care of those events and
218 * avoiding the call if inappropriate. The function does not call the
219 * connection's polling update function, so the caller is responsible for this.
220 */
221static int raw_sock_to_buf(struct connection *conn, struct buffer *buf, int count)
222{
223 int ret, done = 0;
224 int try = count;
225
226 /* stop here if we reached the end of data */
227 if ((fdtab[conn->t.sock.fd].ev & (FD_POLL_IN|FD_POLL_HUP)) == FD_POLL_HUP)
228 goto read0;
229
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) {
269 conn->flags |= CO_FL_WAIT_DATA;
270 break;
271 }
272 else if (errno != EINTR) {
273 conn->flags |= CO_FL_ERROR;
274 break;
275 }
276 }
277 return done;
278
279 read0:
280 conn_sock_read0(conn);
281 return done;
282}
283
284
Willy Tarreaubaaee002006-06-26 02:48:02 +0200285/*
Willy Tarreaud7971282006-07-29 18:36:34 +0200286 * this function is called on a read event from a stream socket.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200287 */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200288static void sock_raw_read(struct connection *conn)
Willy Tarreaub277d6e2012-05-11 16:59:14 +0200289{
Willy Tarreau80184712012-07-06 14:54:49 +0200290 struct stream_interface *si = container_of(conn, struct stream_interface, conn);
Willy Tarreau7421efb2012-07-02 15:11:27 +0200291 struct channel *b = si->ib;
Willy Tarreauafad0e02012-08-09 14:45:22 +0200292 int ret, max, cur_read;
Willy Tarreaub8949f12007-03-23 22:39:59 +0100293 int read_poll = MAX_READ_POLL_LOOPS;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200294
295#ifdef DEBUG_FULL
Willy Tarreau2ba44652012-08-20 17:30:32 +0200296 fprintf(stderr,"sock_raw_read : fd=%d, ev=0x%02x, owner=%p\n", conn->t.sock.fd, fdtab[conn->t.sock.fd].ev, fdtab[conn->t.sock.fd].owner);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200297#endif
Willy Tarreau71543652009-07-14 19:55:05 +0200298 /* stop immediately on errors. Note that we DON'T want to stop on
299 * POLL_ERR, as the poller might report a write error while there
300 * are still data available in the recv buffer. This typically
301 * happens when we send too large a request to a backend server
302 * which rejects it before reading it all.
303 */
Willy Tarreau80184712012-07-06 14:54:49 +0200304 if (conn->flags & CO_FL_ERROR)
Willy Tarreau6996e152007-04-30 14:37:43 +0200305 goto out_error;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100306
307 /* stop here if we reached the end of data */
Willy Tarreau2ba44652012-08-20 17:30:32 +0200308 if (conn_data_read0_pending(conn))
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100309 goto out_shutdown_r;
Willy Tarreau83749182007-04-15 20:56:27 +0200310
Willy Tarreaud06e7112009-03-29 10:18:41 +0200311 /* maybe we were called immediately after an asynchronous shutr */
312 if (b->flags & BF_SHUTR)
Willy Tarreauafad0e02012-08-09 14:45:22 +0200313 return;
Willy Tarreaud06e7112009-03-29 10:18:41 +0200314
Willy Tarreauaf978c42012-08-20 20:21:01 +0200315#if 0 && defined(CONFIG_HAP_LINUX_SPLICE)
Willy Tarreau14acc702011-05-11 20:47:24 +0200316 if (b->to_forward >= MIN_SPLICE_FORWARD && b->flags & BF_KERN_SPLICING) {
Willy Tarreau98b306b2009-01-25 11:11:32 +0100317
318 /* Under Linux, if FD_POLL_HUP is set, we have reached the end.
319 * Since older splice() implementations were buggy and returned
320 * EAGAIN on end of read, let's bypass the call to splice() now.
321 */
Willy Tarreau2ba44652012-08-20 17:30:32 +0200322 if (fdtab[conn->t.sock.fd].ev & FD_POLL_HUP)
Willy Tarreau98b306b2009-01-25 11:11:32 +0100323 goto out_shutdown_r;
324
Willy Tarreauafad0e02012-08-09 14:45:22 +0200325 if (sock_raw_splice_in(b, si) >= 0) {
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100326 if (si->flags & SI_FL_ERR)
327 goto out_error;
328 if (b->flags & BF_READ_NULL)
329 goto out_shutdown_r;
Willy Tarreauafad0e02012-08-09 14:45:22 +0200330 return;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100331 }
332 /* splice not possible (anymore), let's go on on standard copy */
333 }
334#endif
Willy Tarreau8a7af602008-05-03 23:07:14 +0200335 cur_read = 0;
Willy Tarreau2ba44652012-08-20 17:30:32 +0200336 conn->flags &= ~(CO_FL_WAIT_DATA | CO_FL_WAIT_ROOM);
337 while (!(conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_DATA_RD_SH | CO_FL_WAIT_DATA | CO_FL_WAIT_ROOM | CO_FL_HANDSHAKE))) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200338 max = bi_avail(b);
Willy Tarreau864e8252009-12-28 17:36:37 +0100339
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200340 if (!max) {
Willy Tarreau864e8252009-12-28 17:36:37 +0100341 b->flags |= BF_FULL;
342 si->flags |= SI_FL_WAIT_ROOM;
343 break;
344 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200345
Willy Tarreau1fe6bc32012-08-20 20:27:59 +0200346 ret = conn->data->rcv_buf(conn, &b->buf, max);
Willy Tarreau2ba44652012-08-20 17:30:32 +0200347 if (ret <= 0)
348 break;
Willy Tarreaud6d06902009-08-19 11:22:33 +0200349
Willy Tarreau2ba44652012-08-20 17:30:32 +0200350 cur_read += ret;
Willy Tarreaub38903c2008-11-23 21:33:29 +0100351
Willy Tarreau2ba44652012-08-20 17:30:32 +0200352 /* if we're allowed to directly forward data, we must update ->o */
353 if (b->to_forward && !(b->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
354 unsigned long fwd = ret;
355 if (b->to_forward != BUF_INFINITE_FORWARD) {
356 if (fwd > b->to_forward)
357 fwd = b->to_forward;
358 b->to_forward -= fwd;
Willy Tarreau0abebcc2009-01-08 00:09:41 +0100359 }
Willy Tarreau2ba44652012-08-20 17:30:32 +0200360 b_adv(b, fwd);
361 }
Willy Tarreauf890dc92008-12-13 21:12:26 +0100362
Willy Tarreau2ba44652012-08-20 17:30:32 +0200363 if (conn->flags & CO_FL_WAIT_L4_CONN) {
364 conn->flags &= ~CO_FL_WAIT_L4_CONN;
365 si->exp = TICK_ETERNITY;
366 }
Willy Tarreaub38903c2008-11-23 21:33:29 +0100367
Willy Tarreau2ba44652012-08-20 17:30:32 +0200368 b->flags |= BF_READ_PARTIAL;
369 b->total += ret;
Willy Tarreau9641e8f2007-03-23 23:02:09 +0100370
Willy Tarreau2ba44652012-08-20 17:30:32 +0200371 if (bi_full(b)) {
372 /* The buffer is now full, there's no point in going through
373 * the loop again.
374 */
375 if (!(b->flags & BF_STREAMER_FAST) && (cur_read == buffer_len(&b->buf))) {
376 b->xfer_small = 0;
377 b->xfer_large++;
378 if (b->xfer_large >= 3) {
379 /* we call this buffer a fast streamer if it manages
380 * to be filled in one call 3 consecutive times.
381 */
382 b->flags |= (BF_STREAMER | BF_STREAMER_FAST);
383 //fputc('+', stderr);
Willy Tarreau8a7af602008-05-03 23:07:14 +0200384 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200385 }
Willy Tarreau2ba44652012-08-20 17:30:32 +0200386 else if ((b->flags & (BF_STREAMER | BF_STREAMER_FAST)) &&
387 (cur_read <= b->buf.size / 2)) {
388 b->xfer_large = 0;
389 b->xfer_small++;
390 if (b->xfer_small >= 2) {
391 /* if the buffer has been at least half full twice,
392 * we receive faster than we send, so at least it
393 * is not a "fast streamer".
394 */
395 b->flags &= ~BF_STREAMER_FAST;
396 //fputc('-', stderr);
Willy Tarreau8a7af602008-05-03 23:07:14 +0200397 }
Willy Tarreau2ba44652012-08-20 17:30:32 +0200398 }
399 else {
400 b->xfer_small = 0;
401 b->xfer_large = 0;
402 }
Willy Tarreau2bea3a12008-08-28 09:47:43 +0200403
Willy Tarreau2ba44652012-08-20 17:30:32 +0200404 b->flags |= BF_FULL;
405 si->flags |= SI_FL_WAIT_ROOM;
406 break;
407 }
Willy Tarreauab3e1d32007-06-03 14:10:36 +0200408
Willy Tarreau2ba44652012-08-20 17:30:32 +0200409 if ((b->flags & BF_READ_DONTWAIT) || --read_poll <= 0)
410 break;
Willy Tarreau6f4a82c2009-03-21 20:43:57 +0100411
Willy Tarreau2ba44652012-08-20 17:30:32 +0200412 /* if too many bytes were missing from last read, it means that
413 * it's pointless trying to read again because the system does
414 * not have them in buffers.
415 */
416 if (ret < max) {
417 if ((b->flags & (BF_STREAMER | BF_STREAMER_FAST)) &&
418 (cur_read <= b->buf.size / 2)) {
419 b->xfer_large = 0;
420 b->xfer_small++;
421 if (b->xfer_small >= 3) {
422 /* we have read less than half of the buffer in
423 * one pass, and this happened at least 3 times.
424 * This is definitely not a streamer.
425 */
426 b->flags &= ~(BF_STREAMER | BF_STREAMER_FAST);
427 //fputc('!', stderr);
428 }
Willy Tarreau6f4a82c2009-03-21 20:43:57 +0100429 }
Willy Tarreau83749182007-04-15 20:56:27 +0200430
Willy Tarreau2ba44652012-08-20 17:30:32 +0200431 /* if a streamer has read few data, it may be because we
432 * have exhausted system buffers. It's not worth trying
433 * again.
434 */
435 if (b->flags & BF_STREAMER)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200436 break;
Willy Tarreau2ba44652012-08-20 17:30:32 +0200437
438 /* if we read a large block smaller than what we requested,
439 * it's almost certain we'll never get anything more.
Willy Tarreau9f195292007-04-15 21:26:58 +0200440 */
Willy Tarreau2ba44652012-08-20 17:30:32 +0200441 if (ret >= global.tune.recv_enough)
442 break;
Willy Tarreau83749182007-04-15 20:56:27 +0200443 }
Willy Tarreau2ba44652012-08-20 17:30:32 +0200444 } /* while !flags */
445
446 if (conn->flags & CO_FL_ERROR)
447 goto out_error;
448
449 if (conn->flags & CO_FL_WAIT_DATA) {
450 /* we don't automatically ask for polling if we have
451 * read enough data, as it saves some syscalls with
452 * speculative pollers.
453 */
454 if (cur_read < MIN_RET_FOR_READ_LOOP)
455 __conn_data_poll_recv(conn);
456 else
457 __conn_data_want_recv(conn);
458 }
459
460 if (conn_data_read0_pending(conn))
461 /* connection closed */
462 goto out_shutdown_r;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200463
Willy Tarreauafad0e02012-08-09 14:45:22 +0200464 return;
Willy Tarreau6996e152007-04-30 14:37:43 +0200465
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100466 out_shutdown_r:
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200467 /* we received a shutdown */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100468 b->flags |= BF_READ_NULL;
Willy Tarreau520d95e2009-09-19 21:04:57 +0200469 if (b->flags & BF_AUTO_CLOSE)
Willy Tarreau418fd472009-09-06 21:37:23 +0200470 buffer_shutw_now(b);
Willy Tarreau9bf9c142012-08-20 15:38:41 +0200471 stream_sock_read0(si);
Willy Tarreau2ba44652012-08-20 17:30:32 +0200472 conn_data_read0(conn);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200473 return;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100474
Willy Tarreau6996e152007-04-30 14:37:43 +0200475 out_error:
Willy Tarreauafad0e02012-08-09 14:45:22 +0200476 /* Read error on the connection, report the error and stop I/O */
Willy Tarreau80184712012-07-06 14:54:49 +0200477 conn->flags |= CO_FL_ERROR;
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200478 conn_data_stop_both(conn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200479}
480
481
482/*
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100483 * This function is called to send buffer data to a stream socket.
Willy Tarreauafad0e02012-08-09 14:45:22 +0200484 * It returns -1 in case of unrecoverable error, otherwise zero.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200485 */
Willy Tarreaufae44992012-08-20 14:02:10 +0200486static int sock_raw_write_loop(struct connection *conn)
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100487{
Willy Tarreaufae44992012-08-20 14:02:10 +0200488 struct stream_interface *si = container_of(conn, struct stream_interface, conn);
Willy Tarreau7421efb2012-07-02 15:11:27 +0200489 struct channel *b = si->ob;
Willy Tarreau83749182007-04-15 20:56:27 +0200490 int write_poll = MAX_WRITE_POLL_LOOPS;
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100491 int ret, max;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200492
Willy Tarreauaf978c42012-08-20 20:21:01 +0200493#if 0 && defined(CONFIG_HAP_LINUX_SPLICE)
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100494 while (b->pipe) {
Willy Tarreaufb7508a2012-05-21 16:47:54 +0200495 ret = splice(b->pipe->cons, NULL, si_fd(si), NULL, b->pipe->data,
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100496 SPLICE_F_MOVE|SPLICE_F_NONBLOCK);
497 if (ret <= 0) {
498 if (ret == 0 || errno == EAGAIN) {
Willy Tarreauafad0e02012-08-09 14:45:22 +0200499 conn_data_poll_send(&si->conn);
500 return 0;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100501 }
502 /* here we have another error */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200503 return -1;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100504 }
505
506 b->flags |= BF_WRITE_PARTIAL;
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100507 b->pipe->data -= ret;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100508
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100509 if (!b->pipe->data) {
510 put_pipe(b->pipe);
511 b->pipe = NULL;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100512 break;
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100513 }
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100514
515 if (--write_poll <= 0)
Willy Tarreauafad0e02012-08-09 14:45:22 +0200516 return 0;
Willy Tarreaueb9fd512011-12-11 22:11:47 +0100517
518 /* The only reason we did not empty the pipe is that the output
519 * buffer is full.
520 */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200521 conn_data_poll_send(&si->conn);
Willy Tarreaueb9fd512011-12-11 22:11:47 +0100522 return 0;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100523 }
524
525 /* At this point, the pipe is empty, but we may still have data pending
526 * in the normal buffer.
527 */
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100528#endif
Willy Tarreau572bf902012-07-02 17:01:20 +0200529 if (!b->buf.o) {
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200530 b->flags |= BF_OUT_EMPTY;
Willy Tarreauafad0e02012-08-09 14:45:22 +0200531 return 0;
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200532 }
Willy Tarreau83749182007-04-15 20:56:27 +0200533
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100534 /* when we're in this loop, we already know that there is no spliced
535 * data left, and that there are sendable buffered data.
536 */
Willy Tarreau6996e152007-04-30 14:37:43 +0200537 while (1) {
Willy Tarreau572bf902012-07-02 17:01:20 +0200538 max = b->buf.o;
Willy Tarreau83749182007-04-15 20:56:27 +0200539
Willy Tarreau89fa7062012-03-02 16:13:16 +0100540 /* outgoing data may wrap at the end */
Willy Tarreau572bf902012-07-02 17:01:20 +0200541 if (b->buf.data + max > b->buf.p)
542 max = b->buf.data + max - b->buf.p;
Willy Tarreauf890dc92008-12-13 21:12:26 +0100543
Willy Tarreau6db06d32009-08-19 11:14:11 +0200544 /* check if we want to inform the kernel that we're interested in
545 * sending more data after this call. We want this if :
546 * - we're about to close after this last send and want to merge
547 * the ongoing FIN with the last segment.
548 * - we know we can't send everything at once and must get back
549 * here because of unaligned data
Willy Tarreaud38b53b2010-01-03 11:18:34 +0100550 * - there is still a finite amount of data to forward
Willy Tarreau6db06d32009-08-19 11:14:11 +0200551 * The test is arranged so that the most common case does only 2
552 * tests.
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200553 */
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200554
Willy Tarreauface8392010-01-03 11:37:54 +0100555 if (MSG_NOSIGNAL && MSG_MORE) {
Willy Tarreau6db06d32009-08-19 11:14:11 +0200556 unsigned int send_flag = MSG_DONTWAIT | MSG_NOSIGNAL;
557
Willy Tarreau96e31212011-05-30 18:10:30 +0200558 if ((!(b->flags & BF_NEVER_WAIT) &&
559 ((b->to_forward && b->to_forward != BUF_INFINITE_FORWARD) ||
560 (b->flags & BF_EXPECT_MORE))) ||
Willy Tarreau572bf902012-07-02 17:01:20 +0200561 ((b->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK)) == BF_SHUTW_NOW && (max == b->buf.o)) ||
562 (max != b->buf.o)) {
Willy Tarreauface8392010-01-03 11:37:54 +0100563 send_flag |= MSG_MORE;
564 }
Willy Tarreau6db06d32009-08-19 11:14:11 +0200565
Willy Tarreau2be39392010-01-03 17:24:51 +0100566 /* this flag has precedence over the rest */
567 if (b->flags & BF_SEND_DONTWAIT)
568 send_flag &= ~MSG_MORE;
569
Willy Tarreau572bf902012-07-02 17:01:20 +0200570 ret = send(si_fd(si), bo_ptr(&b->buf), max, send_flag);
Willy Tarreaud6d06902009-08-19 11:22:33 +0200571 } else {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200572 int skerr;
573 socklen_t lskerr = sizeof(skerr);
574
Willy Tarreaufb7508a2012-05-21 16:47:54 +0200575 ret = getsockopt(si_fd(si), SOL_SOCKET, SO_ERROR, &skerr, &lskerr);
Willy Tarreauc6423482006-10-15 14:59:03 +0200576 if (ret == -1 || skerr)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200577 ret = -1;
578 else
Willy Tarreau572bf902012-07-02 17:01:20 +0200579 ret = send(si_fd(si), bo_ptr(&b->buf), max, MSG_DONTWAIT);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200580 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200581
582 if (ret > 0) {
Willy Tarreau505e34a2012-07-06 10:17:53 +0200583 if (si->conn.flags & CO_FL_WAIT_L4_CONN) {
584 si->conn.flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau8ae52cb2012-05-20 10:38:46 +0200585 si->exp = TICK_ETERNITY;
586 }
Willy Tarreaub38903c2008-11-23 21:33:29 +0100587
Willy Tarreau3da77c52008-08-29 09:58:42 +0200588 b->flags |= BF_WRITE_PARTIAL;
Willy Tarreaue393fe22008-08-16 22:18:07 +0200589
Willy Tarreau572bf902012-07-02 17:01:20 +0200590 b->buf.o -= ret;
591 if (likely(!buffer_len(&b->buf)))
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100592 /* optimize data alignment in the buffer */
Willy Tarreau572bf902012-07-02 17:01:20 +0200593 b->buf.p = b->buf.data;
Willy Tarreau83749182007-04-15 20:56:27 +0200594
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200595 if (likely(!bi_full(b)))
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100596 b->flags &= ~BF_FULL;
597
Willy Tarreau572bf902012-07-02 17:01:20 +0200598 if (!b->buf.o) {
Willy Tarreauf17810e2012-03-09 18:10:44 +0100599 /* Always clear both flags once everything has been sent, they're one-shot */
600 b->flags &= ~(BF_EXPECT_MORE | BF_SEND_DONTWAIT);
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200601 if (likely(!b->pipe))
602 b->flags |= BF_OUT_EMPTY;
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100603 break;
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200604 }
Willy Tarreau83749182007-04-15 20:56:27 +0200605
Willy Tarreauab3e1d32007-06-03 14:10:36 +0200606 /* if the system buffer is full, don't insist */
607 if (ret < max)
608 break;
609
Willy Tarreau6996e152007-04-30 14:37:43 +0200610 if (--write_poll <= 0)
611 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200612 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200613 else if (ret == 0 || errno == EAGAIN) {
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100614 /* nothing written, we need to poll for write first */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200615 conn_data_poll_send(&si->conn);
616 return 0;
Willy Tarreau83749182007-04-15 20:56:27 +0200617 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200618 else {
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100619 /* bad, we got an error */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200620 return -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200621 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200622 } /* while (1) */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200623 return 0;
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100624}
Willy Tarreau6996e152007-04-30 14:37:43 +0200625
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100626
Willy Tarreau5c979a92012-05-07 17:15:39 +0200627/* stream sock operations */
Willy Tarreau75bf2c92012-08-20 17:01:35 +0200628struct sock_ops raw_sock = {
Willy Tarreau100c4672012-08-20 12:06:26 +0200629 .update = stream_int_update_conn,
Willy Tarreau4a36b562012-08-06 19:31:45 +0200630 .shutr = NULL,
631 .shutw = NULL,
Willy Tarreau46a8d922012-08-20 12:38:36 +0200632 .chk_rcv = stream_int_chk_rcv_conn,
Willy Tarreaude5722c2012-08-20 15:01:10 +0200633 .chk_snd = stream_int_chk_snd_conn,
Willy Tarreaub277d6e2012-05-11 16:59:14 +0200634 .read = sock_raw_read,
Willy Tarreaueecf6ca2012-08-20 15:09:53 +0200635 .write = si_conn_send_cb,
Willy Tarreaufae44992012-08-20 14:02:10 +0200636 .snd_buf = sock_raw_write_loop,
Willy Tarreau1fe6bc32012-08-20 20:27:59 +0200637 .rcv_buf = raw_sock_to_buf,
Willy Tarreau24208272012-05-21 17:28:50 +0200638 .close = NULL,
Willy Tarreau5c979a92012-05-07 17:15:39 +0200639};
Willy Tarreaubaaee002006-06-26 02:48:02 +0200640
641/*
642 * Local variables:
643 * c-indent-level: 8
644 * c-basic-offset: 8
645 * End:
646 */