blob: 7e9dd2c2fca6a61bda3b220aaa280d7003ef93a7 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Functions operating on SOCK_STREAM and buffers.
3 *
Willy Tarreau3eba98a2009-01-25 13:56:13 +01004 * Copyright 2000-2009 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 Tarreaubaaee002006-06-26 02:48:02 +020033#include <proto/client.h>
34#include <proto/fd.h>
Willy Tarreau3eba98a2009-01-25 13:56:13 +010035#include <proto/pipe.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020036#include <proto/stream_sock.h>
37#include <proto/task.h>
38
Willy Tarreau5bd8c372009-01-19 00:32:22 +010039#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020040
Willy Tarreau6b4aad42009-01-18 21:59:13 +010041/* On recent Linux kernels, the splice() syscall may be used for faster data copy.
42 * But it's not always defined on some OS versions, and it even happens that some
43 * definitions are wrong with some glibc due to an offset bug in syscall().
44 */
45
46#if defined(CONFIG_HAP_LINUX_SPLICE)
47#include <unistd.h>
48#include <sys/syscall.h>
49
50#ifndef SPLICE_F_MOVE
51#define SPLICE_F_MOVE 0x1
52#endif
53
54#ifndef SPLICE_F_NONBLOCK
55#define SPLICE_F_NONBLOCK 0x2
56#endif
57
58#ifndef SPLICE_F_MORE
59#define SPLICE_F_MORE 0x4
60#endif
61
62#ifndef __NR_splice
63#if defined(__powerpc__) || defined(__powerpc64__)
64#define __NR_splice 283
65#elif defined(__sparc__) || defined(__sparc64__)
66#define __NR_splice 232
67#elif defined(__x86_64__)
68#define __NR_splice 275
69#elif defined(__alpha__)
70#define __NR_splice 468
71#elif defined (__i386__)
72#define __NR_splice 313
73#else
74#warning unsupported architecture, guessing __NR_splice=313 like x86...
75#define __NR_splice 313
76#endif /* $arch */
77
78_syscall6(int, splice, int, fdin, loff_t *, off_in, int, fdout, loff_t *, off_out, size_t, len, unsigned long, flags)
79
80#endif /* __NR_splice */
Willy Tarreau5bd8c372009-01-19 00:32:22 +010081
82/* A pipe contains 16 segments max, and it's common to see segments of 1448 bytes
83 * because of timestamps. Use this as a hint for not looping on splice().
84 */
85#define SPLICE_FULL_HINT 16*1448
86
Willy Tarreaua9de3332009-11-28 07:47:10 +010087/* how many data we attempt to splice at once when the buffer is configured for
88 * infinite forwarding */
89#define MAX_SPLICE_AT_ONCE (1<<30)
90
Willy Tarreau5bd8c372009-01-19 00:32:22 +010091/* Returns :
92 * -1 if splice is not possible or not possible anymore and we must switch to
93 * user-land copy (eg: to_forward reached)
94 * 0 when we know that polling is required to get more data (EAGAIN)
95 * 1 for all other cases (we can safely try again, or if an activity has been
96 * detected (DATA/NULL/ERR))
97 * Sets :
98 * BF_READ_NULL
99 * BF_READ_PARTIAL
100 * BF_WRITE_PARTIAL (during copy)
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200101 * BF_OUT_EMPTY (during copy)
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100102 * SI_FL_ERR
103 * SI_FL_WAIT_ROOM
104 * (SI_FL_WAIT_RECV)
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100105 *
106 * This function automatically allocates a pipe from the pipe pool. It also
107 * carefully ensures to clear b->pipe whenever it leaves the pipe empty.
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100108 */
109static int stream_sock_splice_in(struct buffer *b, struct stream_interface *si)
110{
111 int fd = si->fd;
Willy Tarreau31971e52009-09-20 12:07:52 +0200112 int ret;
113 unsigned long max;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100114 int retval = 1;
115
116 if (!b->to_forward)
117 return -1;
118
119 if (!(b->flags & BF_KERN_SPLICING))
120 return -1;
121
122 if (b->l) {
123 /* We're embarrassed, there are already data pending in
124 * the buffer and we don't want to have them at two
125 * locations at a time. Let's indicate we need some
126 * place and ask the consumer to hurry.
127 */
128 si->flags |= SI_FL_WAIT_ROOM;
129 EV_FD_CLR(fd, DIR_RD);
130 b->rex = TICK_ETERNITY;
131 b->cons->chk_snd(b->cons);
132 return 1;
133 }
134
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100135 if (unlikely(b->pipe == NULL)) {
136 if (pipes_used >= global.maxpipes || !(b->pipe = get_pipe())) {
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100137 b->flags &= ~BF_KERN_SPLICING;
138 return -1;
139 }
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100140 }
141
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100142 /* At this point, b->pipe is valid */
143
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100144 while (1) {
Willy Tarreaua9de3332009-11-28 07:47:10 +0100145 if (b->to_forward == BUF_INFINITE_FORWARD)
146 max = MAX_SPLICE_AT_ONCE;
147 else
148 max = b->to_forward;
149
Willy Tarreau31971e52009-09-20 12:07:52 +0200150 if (!max) {
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100151 /* It looks like the buffer + the pipe already contain
152 * the maximum amount of data to be transferred. Try to
153 * send those data immediately on the other side if it
154 * is currently waiting.
155 */
156 retval = -1; /* end of forwarding */
157 break;
158 }
159
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100160 ret = splice(fd, NULL, b->pipe->prod, NULL, max,
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100161 SPLICE_F_MOVE|SPLICE_F_NONBLOCK);
162
163 if (ret <= 0) {
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100164 if (ret == 0) {
Willy Tarreau98b306b2009-01-25 11:11:32 +0100165 /* connection closed. This is only detected by
166 * recent kernels (>= 2.6.27.13).
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100167 */
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100168 b->flags |= BF_READ_NULL;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100169 retval = 1; /* no need for further polling */
170 break;
171 }
172
173 if (errno == EAGAIN) {
174 /* there are two reasons for EAGAIN :
175 * - nothing in the socket buffer (standard)
176 * - pipe is full
Willy Tarreau98b306b2009-01-25 11:11:32 +0100177 * - the connection is closed (kernel < 2.6.27.13)
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100178 * Since we don't know if pipe is full, we'll
179 * stop if the pipe is not empty. Anyway, we
180 * will almost always fill/empty the pipe.
181 */
182
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100183 if (b->pipe->data) {
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100184 si->flags |= SI_FL_WAIT_ROOM;
185 retval = 1;
186 break;
187 }
188
Willy Tarreau98b306b2009-01-25 11:11:32 +0100189 /* We don't know if the connection was closed.
190 * But if we're called upon POLLIN with an empty
191 * pipe and get EAGAIN, it is suspect enought to
192 * try to fall back to the normal recv scheme
193 * which will be able to deal with the situation.
194 */
195 retval = -1;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100196 break;
197 }
Willy Tarreaudc340a92009-06-28 23:10:19 +0200198
Willy Tarreaua9de3332009-11-28 07:47:10 +0100199 if (errno == ENOSYS || errno == EINVAL) {
Willy Tarreaudc340a92009-06-28 23:10:19 +0200200 /* splice not supported on this end, disable it */
201 b->flags &= ~BF_KERN_SPLICING;
202 si->flags &= ~SI_FL_CAP_SPLICE;
203 put_pipe(b->pipe);
204 b->pipe = NULL;
205 return -1;
206 }
207
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100208 /* here we have another error */
209 si->flags |= SI_FL_ERR;
210 retval = 1;
211 break;
212 } /* ret <= 0 */
213
Willy Tarreau31971e52009-09-20 12:07:52 +0200214 if (b->to_forward != BUF_INFINITE_FORWARD)
215 b->to_forward -= ret;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100216 b->total += ret;
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100217 b->pipe->data += ret;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100218 b->flags |= BF_READ_PARTIAL;
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200219 b->flags &= ~BF_OUT_EMPTY;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100220
Willy Tarreau6f4a82c2009-03-21 20:43:57 +0100221 if (b->pipe->data >= SPLICE_FULL_HINT ||
222 ret >= global.tune.recv_enough) {
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100223 /* We've read enough of it for this time. */
224 retval = 1;
225 break;
226 }
227 } /* while */
228
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100229 if (unlikely(!b->pipe->data)) {
230 put_pipe(b->pipe);
231 b->pipe = NULL;
232 }
233
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100234 return retval;
235}
236
Willy Tarreau6b4aad42009-01-18 21:59:13 +0100237#endif /* CONFIG_HAP_LINUX_SPLICE */
238
239
Willy Tarreaubaaee002006-06-26 02:48:02 +0200240/*
Willy Tarreaud7971282006-07-29 18:36:34 +0200241 * this function is called on a read event from a stream socket.
Willy Tarreau83749182007-04-15 20:56:27 +0200242 * It returns 0 if we have a high confidence that we will not be
243 * able to read more data without polling first. Returns non-zero
244 * otherwise.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200245 */
Willy Tarreaud7971282006-07-29 18:36:34 +0200246int stream_sock_read(int fd) {
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200247 struct stream_interface *si = fdtab[fd].owner;
Willy Tarreau48adac52008-08-30 04:58:38 +0200248 struct buffer *b = si->ib;
Willy Tarreau8a7af602008-05-03 23:07:14 +0200249 int ret, max, retval, cur_read;
Willy Tarreaub8949f12007-03-23 22:39:59 +0100250 int read_poll = MAX_READ_POLL_LOOPS;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200251
252#ifdef DEBUG_FULL
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100253 fprintf(stderr,"stream_sock_read : fd=%d, ev=0x%02x, owner=%p\n", fd, fdtab[fd].ev, fdtab[fd].owner);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200254#endif
255
Willy Tarreau83749182007-04-15 20:56:27 +0200256 retval = 1;
257
Willy Tarreau71543652009-07-14 19:55:05 +0200258 /* stop immediately on errors. Note that we DON'T want to stop on
259 * POLL_ERR, as the poller might report a write error while there
260 * are still data available in the recv buffer. This typically
261 * happens when we send too large a request to a backend server
262 * which rejects it before reading it all.
263 */
264 if (fdtab[fd].state == FD_STERROR)
Willy Tarreau6996e152007-04-30 14:37:43 +0200265 goto out_error;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100266
267 /* stop here if we reached the end of data */
268 if ((fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP)) == FD_POLL_HUP)
269 goto out_shutdown_r;
Willy Tarreau83749182007-04-15 20:56:27 +0200270
Willy Tarreaud06e7112009-03-29 10:18:41 +0200271 /* maybe we were called immediately after an asynchronous shutr */
272 if (b->flags & BF_SHUTR)
273 goto out_wakeup;
274
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100275#if defined(CONFIG_HAP_LINUX_SPLICE)
276 if (b->to_forward && b->flags & BF_KERN_SPLICING) {
Willy Tarreau98b306b2009-01-25 11:11:32 +0100277
278 /* Under Linux, if FD_POLL_HUP is set, we have reached the end.
279 * Since older splice() implementations were buggy and returned
280 * EAGAIN on end of read, let's bypass the call to splice() now.
281 */
282 if (fdtab[fd].ev & FD_POLL_HUP)
283 goto out_shutdown_r;
284
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100285 retval = stream_sock_splice_in(b, si);
286
287 if (retval >= 0) {
288 if (si->flags & SI_FL_ERR)
289 goto out_error;
290 if (b->flags & BF_READ_NULL)
291 goto out_shutdown_r;
292 goto out_wakeup;
293 }
294 /* splice not possible (anymore), let's go on on standard copy */
295 }
296#endif
Willy Tarreau8a7af602008-05-03 23:07:14 +0200297 cur_read = 0;
Willy Tarreau6996e152007-04-30 14:37:43 +0200298 while (1) {
Willy Tarreau864e8252009-12-28 17:36:37 +0100299 max = buffer_max_len(b) - b->l;
300
301 if (max <= 0) {
302 b->flags |= BF_FULL;
303 si->flags |= SI_FL_WAIT_ROOM;
304 break;
305 }
306
Willy Tarreau6996e152007-04-30 14:37:43 +0200307 /*
308 * 1. compute the maximum block size we can read at once.
309 */
Willy Tarreau03d60bb2009-01-09 11:13:00 +0100310 if (b->l == 0) {
311 /* let's realign the buffer to optimize I/O */
312 b->r = b->w = b->lr = b->data;
Willy Tarreau83749182007-04-15 20:56:27 +0200313 }
314 else if (b->r > b->w) {
Willy Tarreau864e8252009-12-28 17:36:37 +0100315 /* remaining space wraps at the end, with a moving limit */
316 if (max > b->data + b->size - b->r)
317 max = b->data + b->size - b->r;
Willy Tarreau9c0fe592009-01-18 16:25:31 +0100318 }
Willy Tarreau864e8252009-12-28 17:36:37 +0100319 /* else max is already OK */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200320
Willy Tarreau6996e152007-04-30 14:37:43 +0200321 /*
322 * 2. read the largest possible block
323 */
Willy Tarreaud6d06902009-08-19 11:22:33 +0200324 if (MSG_NOSIGNAL) {
325 ret = recv(fd, b->r, max, MSG_NOSIGNAL);
326 } else {
Willy Tarreau83749182007-04-15 20:56:27 +0200327 int skerr;
328 socklen_t lskerr = sizeof(skerr);
329
330 ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr);
331 if (ret == -1 || skerr)
332 ret = -1;
333 else
334 ret = recv(fd, b->r, max, 0);
335 }
Willy Tarreaud6d06902009-08-19 11:22:33 +0200336
Willy Tarreau83749182007-04-15 20:56:27 +0200337 if (ret > 0) {
338 b->r += ret;
339 b->l += ret;
Willy Tarreau8a7af602008-05-03 23:07:14 +0200340 cur_read += ret;
Willy Tarreaub38903c2008-11-23 21:33:29 +0100341
Willy Tarreau0abebcc2009-01-08 00:09:41 +0100342 /* if we're allowed to directly forward data, we must update send_max */
Willy Tarreau31971e52009-09-20 12:07:52 +0200343 if (b->to_forward && !(b->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
344 unsigned long fwd = ret;
345 if (b->to_forward != BUF_INFINITE_FORWARD) {
346 if (fwd > b->to_forward)
347 fwd = b->to_forward;
348 b->to_forward -= fwd;
349 }
350 b->send_max += fwd;
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200351 b->flags &= ~BF_OUT_EMPTY;
Willy Tarreau0abebcc2009-01-08 00:09:41 +0100352 }
Willy Tarreauf890dc92008-12-13 21:12:26 +0100353
Willy Tarreaub38903c2008-11-23 21:33:29 +0100354 if (fdtab[fd].state == FD_STCONN)
355 fdtab[fd].state = FD_STREADY;
356
Willy Tarreau3da77c52008-08-29 09:58:42 +0200357 b->flags |= BF_READ_PARTIAL;
Willy Tarreau74ab2ac2008-11-23 17:23:07 +0100358
Willy Tarreaua07a34e2009-08-16 23:27:46 +0200359 if (b->r == b->data + b->size) {
Willy Tarreau83749182007-04-15 20:56:27 +0200360 b->r = b->data; /* wrap around the buffer */
361 }
Willy Tarreau9641e8f2007-03-23 23:02:09 +0100362
Willy Tarreau83749182007-04-15 20:56:27 +0200363 b->total += ret;
Willy Tarreau9641e8f2007-03-23 23:02:09 +0100364
Willy Tarreau7c3c5412009-12-13 15:53:05 +0100365 if (b->l >= buffer_max_len(b)) {
Willy Tarreau6996e152007-04-30 14:37:43 +0200366 /* The buffer is now full, there's no point in going through
367 * the loop again.
368 */
Willy Tarreau8a7af602008-05-03 23:07:14 +0200369 if (!(b->flags & BF_STREAMER_FAST) && (cur_read == b->l)) {
370 b->xfer_small = 0;
371 b->xfer_large++;
372 if (b->xfer_large >= 3) {
373 /* we call this buffer a fast streamer if it manages
374 * to be filled in one call 3 consecutive times.
375 */
376 b->flags |= (BF_STREAMER | BF_STREAMER_FAST);
377 //fputc('+', stderr);
378 }
379 }
380 else if ((b->flags & (BF_STREAMER | BF_STREAMER_FAST)) &&
Willy Tarreaua07a34e2009-08-16 23:27:46 +0200381 (cur_read <= b->size / 2)) {
Willy Tarreau8a7af602008-05-03 23:07:14 +0200382 b->xfer_large = 0;
383 b->xfer_small++;
384 if (b->xfer_small >= 2) {
385 /* if the buffer has been at least half full twice,
386 * we receive faster than we send, so at least it
387 * is not a "fast streamer".
388 */
389 b->flags &= ~BF_STREAMER_FAST;
390 //fputc('-', stderr);
391 }
392 }
393 else {
394 b->xfer_small = 0;
395 b->xfer_large = 0;
396 }
Willy Tarreau9c0fe592009-01-18 16:25:31 +0100397
398 b->flags |= BF_FULL;
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100399 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau9c0fe592009-01-18 16:25:31 +0100400 break;
Willy Tarreau6996e152007-04-30 14:37:43 +0200401 }
402
Willy Tarreauab3e1d32007-06-03 14:10:36 +0200403 /* if too many bytes were missing from last read, it means that
404 * it's pointless trying to read again because the system does
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100405 * not have them in buffers. BTW, if FD_POLL_HUP was present,
406 * it means that we have reached the end and that the connection
407 * is closed.
Willy Tarreauab3e1d32007-06-03 14:10:36 +0200408 */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100409 if (ret < max) {
Willy Tarreau8a7af602008-05-03 23:07:14 +0200410 if ((b->flags & (BF_STREAMER | BF_STREAMER_FAST)) &&
Willy Tarreaua07a34e2009-08-16 23:27:46 +0200411 (cur_read <= b->size / 2)) {
Willy Tarreau8a7af602008-05-03 23:07:14 +0200412 b->xfer_large = 0;
413 b->xfer_small++;
414 if (b->xfer_small >= 3) {
415 /* we have read less than half of the buffer in
416 * one pass, and this happened at least 3 times.
417 * This is definitely not a streamer.
418 */
419 b->flags &= ~(BF_STREAMER | BF_STREAMER_FAST);
420 //fputc('!', stderr);
421 }
422 }
Willy Tarreau2bea3a12008-08-28 09:47:43 +0200423 /* unfortunately, on level-triggered events, POLL_HUP
424 * is generally delivered AFTER the system buffer is
425 * empty, so this one might never match.
426 */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100427 if (fdtab[fd].ev & FD_POLL_HUP)
428 goto out_shutdown_r;
Willy Tarreau2bea3a12008-08-28 09:47:43 +0200429
430 /* if a streamer has read few data, it may be because we
431 * have exhausted system buffers. It's not worth trying
432 * again.
433 */
434 if (b->flags & BF_STREAMER)
435 break;
Willy Tarreauab3e1d32007-06-03 14:10:36 +0200436
Willy Tarreau6f4a82c2009-03-21 20:43:57 +0100437 /* generally if we read something smaller than 1 or 2 MSS,
438 * it means that either we have exhausted the system's
439 * buffers (streamer or question-response protocol) or
440 * that the connection will be closed. Streamers are
441 * easily detected so we return early. For other cases,
442 * it's still better to perform a last read to be sure,
443 * because it may save one complete poll/read/wakeup cycle
444 * in case of shutdown.
445 */
446 if (ret < MIN_RET_FOR_READ_LOOP && b->flags & BF_STREAMER)
447 break;
448
449 /* if we read a large block smaller than what we requested,
450 * it's almost certain we'll never get anything more.
451 */
452 if (ret >= global.tune.recv_enough)
453 break;
454 }
Willy Tarreau83749182007-04-15 20:56:27 +0200455
Willy Tarreau1b194fe2009-03-21 21:10:04 +0100456 if ((b->flags & BF_READ_DONTWAIT) || --read_poll <= 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200457 break;
Willy Tarreau83749182007-04-15 20:56:27 +0200458 }
459 else if (ret == 0) {
Willy Tarreau6996e152007-04-30 14:37:43 +0200460 /* connection closed */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100461 goto out_shutdown_r;
Willy Tarreau83749182007-04-15 20:56:27 +0200462 }
Willy Tarreau9f195292007-04-15 21:26:58 +0200463 else if (errno == EAGAIN) {
464 /* Ignore EAGAIN but inform the poller that there is
Willy Tarreauaf78d0f2009-01-08 10:09:08 +0100465 * nothing to read left if we did not read much, ie
466 * less than what we were still expecting to read.
467 * But we may have done some work justifying to notify
468 * the task.
Willy Tarreau9f195292007-04-15 21:26:58 +0200469 */
Willy Tarreauaf78d0f2009-01-08 10:09:08 +0100470 if (cur_read < MIN_RET_FOR_READ_LOOP)
471 retval = 0;
Willy Tarreau83749182007-04-15 20:56:27 +0200472 break;
473 }
474 else {
Willy Tarreau6996e152007-04-30 14:37:43 +0200475 goto out_error;
Willy Tarreau83749182007-04-15 20:56:27 +0200476 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200477 } /* while (1) */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200478
Willy Tarreau6996e152007-04-30 14:37:43 +0200479 out_wakeup:
Willy Tarreau9c0fe592009-01-18 16:25:31 +0100480 /* We might have some data the consumer is waiting for */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200481 if (!(b->flags & BF_OUT_EMPTY) && (b->cons->flags & SI_FL_WAIT_DATA)) {
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100482 int last_len = b->pipe ? b->pipe->data : 0;
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100483
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100484 b->cons->chk_snd(b->cons);
485
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100486 /* check if the consumer has freed some space */
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100487 if (!(b->flags & BF_FULL) &&
488 (!last_len || !b->pipe || b->pipe->data < last_len))
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100489 si->flags &= ~SI_FL_WAIT_ROOM;
490 }
491
492 if (si->flags & SI_FL_WAIT_ROOM) {
Willy Tarreau9c0fe592009-01-18 16:25:31 +0100493 EV_FD_CLR(fd, DIR_RD);
494 b->rex = TICK_ETERNITY;
495 }
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200496 else if ((b->flags & (BF_SHUTR|BF_READ_PARTIAL|BF_FULL|BF_DONT_READ|BF_READ_NOEXP)) == BF_READ_PARTIAL)
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100497 b->rex = tick_add_ifset(now_ms, b->rto);
Willy Tarreau9c0fe592009-01-18 16:25:31 +0100498
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100499 /* we have to wake up if there is a special event or if we don't have
500 * any more data to forward.
501 */
Willy Tarreau1b194fe2009-03-21 21:10:04 +0100502 if ((b->flags & (BF_READ_NULL|BF_READ_ERROR|BF_SHUTR|BF_READ_DONTWAIT)) ||
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100503 !b->to_forward ||
504 si->state != SI_ST_EST ||
505 b->cons->state != SI_ST_EST ||
506 (si->flags & SI_FL_ERR))
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100507 task_wakeup(si->owner, TASK_WOKEN_IO);
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100508
Willy Tarreau1b194fe2009-03-21 21:10:04 +0100509 b->flags &= ~BF_READ_DONTWAIT;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100510 fdtab[fd].ev &= ~FD_POLL_IN;
Willy Tarreau83749182007-04-15 20:56:27 +0200511 return retval;
Willy Tarreau6996e152007-04-30 14:37:43 +0200512
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100513 out_shutdown_r:
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200514 /* we received a shutdown */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100515 fdtab[fd].ev &= ~FD_POLL_HUP;
516 b->flags |= BF_READ_NULL;
Willy Tarreau520d95e2009-09-19 21:04:57 +0200517 if (b->flags & BF_AUTO_CLOSE)
Willy Tarreau418fd472009-09-06 21:37:23 +0200518 buffer_shutw_now(b);
Willy Tarreau99126c32008-11-27 10:30:51 +0100519 stream_sock_shutr(si);
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200520 goto out_wakeup;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100521
Willy Tarreau6996e152007-04-30 14:37:43 +0200522 out_error:
Willy Tarreaucff64112008-11-03 06:26:53 +0100523 /* Read error on the file descriptor. We mark the FD as STERROR so
524 * that we don't use it anymore. The error is reported to the stream
525 * interface which will take proper action. We must not perturbate the
526 * buffer because the stream interface wants to ensure transparent
527 * connection retries.
Willy Tarreau6996e152007-04-30 14:37:43 +0200528 */
Willy Tarreaucff64112008-11-03 06:26:53 +0100529
Willy Tarreau6996e152007-04-30 14:37:43 +0200530 fdtab[fd].state = FD_STERROR;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100531 fdtab[fd].ev &= ~FD_POLL_STICKY;
Willy Tarreau1714e0f2009-03-28 20:54:53 +0100532 EV_FD_REM(fd);
Willy Tarreaucff64112008-11-03 06:26:53 +0100533 si->flags |= SI_FL_ERR;
Willy Tarreau9c0fe592009-01-18 16:25:31 +0100534 retval = 1;
535 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200536}
537
538
539/*
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100540 * This function is called to send buffer data to a stream socket.
541 * It returns -1 in case of unrecoverable error, 0 if the caller needs to poll
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100542 * before calling it again, otherwise 1. If a pipe was associated with the
543 * buffer and it empties it, it releases it as well.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200544 */
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100545static int stream_sock_write_loop(struct stream_interface *si, struct buffer *b)
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100546{
Willy Tarreau83749182007-04-15 20:56:27 +0200547 int write_poll = MAX_WRITE_POLL_LOOPS;
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100548 int retval = 1;
549 int ret, max;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200550
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100551#if defined(CONFIG_HAP_LINUX_SPLICE)
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100552 while (b->pipe) {
553 ret = splice(b->pipe->cons, NULL, si->fd, NULL, b->pipe->data,
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100554 SPLICE_F_MOVE|SPLICE_F_NONBLOCK);
555 if (ret <= 0) {
556 if (ret == 0 || errno == EAGAIN) {
557 retval = 0;
558 return retval;
559 }
560 /* here we have another error */
561 retval = -1;
562 return retval;
563 }
564
565 b->flags |= BF_WRITE_PARTIAL;
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100566 b->pipe->data -= ret;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100567
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100568 if (!b->pipe->data) {
569 put_pipe(b->pipe);
570 b->pipe = NULL;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100571 break;
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100572 }
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100573
574 if (--write_poll <= 0)
575 return retval;
576 }
577
578 /* At this point, the pipe is empty, but we may still have data pending
579 * in the normal buffer.
580 */
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100581#endif
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200582 if (!b->send_max) {
583 b->flags |= BF_OUT_EMPTY;
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100584 return retval;
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200585 }
Willy Tarreau83749182007-04-15 20:56:27 +0200586
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100587 /* when we're in this loop, we already know that there is no spliced
588 * data left, and that there are sendable buffered data.
589 */
Willy Tarreau6996e152007-04-30 14:37:43 +0200590 while (1) {
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100591 if (b->r > b->w)
Willy Tarreau83749182007-04-15 20:56:27 +0200592 max = b->r - b->w;
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100593 else
Willy Tarreaua07a34e2009-08-16 23:27:46 +0200594 max = b->data + b->size - b->w;
Willy Tarreau83749182007-04-15 20:56:27 +0200595
Willy Tarreauf890dc92008-12-13 21:12:26 +0100596 /* limit the amount of outgoing data if required */
597 if (max > b->send_max)
598 max = b->send_max;
599
Willy Tarreau6db06d32009-08-19 11:14:11 +0200600 /* check if we want to inform the kernel that we're interested in
601 * sending more data after this call. We want this if :
602 * - we're about to close after this last send and want to merge
603 * the ongoing FIN with the last segment.
604 * - we know we can't send everything at once and must get back
605 * here because of unaligned data
Willy Tarreaud38b53b2010-01-03 11:18:34 +0100606 * - there is still a finite amount of data to forward
Willy Tarreau6db06d32009-08-19 11:14:11 +0200607 * The test is arranged so that the most common case does only 2
608 * tests.
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200609 */
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200610
Willy Tarreaud6d06902009-08-19 11:22:33 +0200611 if (MSG_NOSIGNAL) {
Willy Tarreau6db06d32009-08-19 11:14:11 +0200612 unsigned int send_flag = MSG_DONTWAIT | MSG_NOSIGNAL;
613
614 if (MSG_MORE &&
Willy Tarreaud38b53b2010-01-03 11:18:34 +0100615 ((b->to_forward && b->to_forward != BUF_INFINITE_FORWARD) ||
616 ((b->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK)) == BF_SHUTW_NOW && (max == b->send_max)) ||
Willy Tarreau6db06d32009-08-19 11:14:11 +0200617 (max != b->l && max != b->send_max))
618 && (fdtab[si->fd].flags & FD_FL_TCP)) {
619 send_flag |= MSG_MORE;
620 }
621
622 ret = send(si->fd, b->w, max, send_flag);
Willy Tarreaud6d06902009-08-19 11:22:33 +0200623 } else {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200624 int skerr;
625 socklen_t lskerr = sizeof(skerr);
626
Willy Tarreau87bed622009-03-08 22:25:28 +0100627 ret = getsockopt(si->fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr);
Willy Tarreauc6423482006-10-15 14:59:03 +0200628 if (ret == -1 || skerr)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200629 ret = -1;
630 else
Willy Tarreau87bed622009-03-08 22:25:28 +0100631 ret = send(si->fd, b->w, max, MSG_DONTWAIT);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200632 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200633
634 if (ret > 0) {
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100635 if (fdtab[si->fd].state == FD_STCONN)
636 fdtab[si->fd].state = FD_STREADY;
Willy Tarreaub38903c2008-11-23 21:33:29 +0100637
Willy Tarreau3da77c52008-08-29 09:58:42 +0200638 b->flags |= BF_WRITE_PARTIAL;
Willy Tarreaue393fe22008-08-16 22:18:07 +0200639
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100640 b->w += ret;
Willy Tarreaua07a34e2009-08-16 23:27:46 +0200641 if (b->w == b->data + b->size)
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100642 b->w = b->data; /* wrap around the buffer */
643
644 b->l -= ret;
Willy Tarreau7c3c5412009-12-13 15:53:05 +0100645 if (likely(b->l < buffer_max_len(b)))
Willy Tarreaue393fe22008-08-16 22:18:07 +0200646 b->flags &= ~BF_FULL;
Willy Tarreau74ab2ac2008-11-23 17:23:07 +0100647
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200648 if (likely(!b->l))
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100649 /* optimize data alignment in the buffer */
650 b->r = b->w = b->lr = b->data;
Willy Tarreau83749182007-04-15 20:56:27 +0200651
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100652 b->send_max -= ret;
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200653 if (!b->send_max) {
654 if (likely(!b->pipe))
655 b->flags |= BF_OUT_EMPTY;
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100656 break;
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200657 }
Willy Tarreau83749182007-04-15 20:56:27 +0200658
Willy Tarreauab3e1d32007-06-03 14:10:36 +0200659 /* if the system buffer is full, don't insist */
660 if (ret < max)
661 break;
662
Willy Tarreau6996e152007-04-30 14:37:43 +0200663 if (--write_poll <= 0)
664 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200665 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200666 else if (ret == 0 || errno == EAGAIN) {
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100667 /* nothing written, we need to poll for write first */
Willy Tarreau83749182007-04-15 20:56:27 +0200668 retval = 0;
669 break;
670 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200671 else {
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100672 /* bad, we got an error */
673 retval = -1;
674 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200675 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200676 } /* while (1) */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200677
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100678 return retval;
679}
Willy Tarreau6996e152007-04-30 14:37:43 +0200680
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100681
682/*
683 * This function is called on a write event from a stream socket.
684 * It returns 0 if the caller needs to poll before calling it again, otherwise
685 * non-zero.
686 */
687int stream_sock_write(int fd)
688{
689 struct stream_interface *si = fdtab[fd].owner;
690 struct buffer *b = si->ob;
691 int retval = 1;
692
693#ifdef DEBUG_FULL
694 fprintf(stderr,"stream_sock_write : fd=%d, owner=%p\n", fd, fdtab[fd].owner);
695#endif
696
697 retval = 1;
Willy Tarreau71543652009-07-14 19:55:05 +0200698 if (fdtab[fd].state == FD_STERROR)
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100699 goto out_error;
700
Willy Tarreaud06e7112009-03-29 10:18:41 +0200701 /* we might have been called just after an asynchronous shutw */
702 if (b->flags & BF_SHUTW)
703 goto out_wakeup;
704
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200705 if (likely(!(b->flags & BF_OUT_EMPTY))) {
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100706 /* OK there are data waiting to be sent */
707 retval = stream_sock_write_loop(si, b);
708 if (retval < 0)
709 goto out_error;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200710 }
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100711 else {
712 /* may be we have received a connection acknowledgement in TCP mode without data */
713 if (likely(fdtab[fd].state == FD_STCONN)) {
714 /* We have no data to send to check the connection, and
715 * getsockopt() will not inform us whether the connection
716 * is still pending. So we'll reuse connect() to check the
717 * state of the socket. This has the advantage of givig us
718 * the following info :
719 * - error
720 * - connecting (EALREADY, EINPROGRESS)
721 * - connected (EISCONN, 0)
722 */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200723 if ((connect(fd, fdinfo[fd].peeraddr, fdinfo[fd].peerlen) == 0))
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100724 errno = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200725
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100726 if (errno == EALREADY || errno == EINPROGRESS) {
727 retval = 0;
728 goto out_may_wakeup;
729 }
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100730
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100731 if (errno && errno != EISCONN)
732 goto out_error;
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200733
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100734 /* OK we just need to indicate that we got a connection
735 * and that we wrote nothing.
736 */
737 b->flags |= BF_WRITE_NULL;
738 fdtab[fd].state = FD_STREADY;
739 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200740
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100741 /* Funny, we were called to write something but there wasn't
742 * anything. We can get there, for example if we were woken up
743 * on a write event to finish the splice, but the send_max is 0
744 * so we cannot write anything from the buffer. Let's disable
745 * the write event and pretend we never came there.
746 */
747 }
748
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200749 if (b->flags & BF_OUT_EMPTY) {
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100750 /* the connection is established but we can't write. Either the
751 * buffer is empty, or we just refrain from sending because the
752 * send_max limit was reached. Maybe we just wrote the last
753 * chunk and need to close.
754 */
Willy Tarreau520d95e2009-09-19 21:04:57 +0200755 if (((b->flags & (BF_SHUTW|BF_HIJACK|BF_SHUTW_NOW)) == BF_SHUTW_NOW) &&
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100756 (si->state == SI_ST_EST)) {
757 stream_sock_shutw(si);
758 goto out_wakeup;
759 }
760
Willy Tarreau59454bf2009-09-20 11:13:40 +0200761 if ((b->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_FULL|BF_HIJACK)) == 0)
Willy Tarreauac128fe2009-01-09 13:05:19 +0100762 si->flags |= SI_FL_WAIT_DATA;
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100763
Willy Tarreauac128fe2009-01-09 13:05:19 +0100764 EV_FD_CLR(fd, DIR_WR);
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100765 b->wex = TICK_ETERNITY;
Willy Tarreauac128fe2009-01-09 13:05:19 +0100766 }
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100767
768 out_may_wakeup:
769 if (b->flags & BF_WRITE_ACTIVITY) {
770 /* update timeout if we have written something */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200771 if ((b->flags & (BF_OUT_EMPTY|BF_SHUTW|BF_WRITE_PARTIAL)) == BF_WRITE_PARTIAL)
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100772 b->wex = tick_add_ifset(now_ms, b->wto);
773
774 out_wakeup:
Willy Tarreauf27b5ea2009-10-03 22:01:18 +0200775 if (tick_isset(si->ib->rex) && !(si->flags & SI_FL_INDEP_STR)) {
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100776 /* Note: to prevent the client from expiring read timeouts
Willy Tarreauf27b5ea2009-10-03 22:01:18 +0200777 * during writes, we refresh it. We only do this if the
778 * interface is not configured for "independant streams",
779 * because for some applications it's better not to do this,
780 * for instance when continuously exchanging small amounts
781 * of data which can full the socket buffers long before a
782 * write timeout is detected.
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100783 */
784 si->ib->rex = tick_add_ifset(now_ms, si->ib->rto);
785 }
786
787 /* the producer might be waiting for more room to store data */
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200788 if (likely((b->flags & (BF_SHUTW|BF_WRITE_PARTIAL|BF_FULL|BF_DONT_READ)) == BF_WRITE_PARTIAL &&
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100789 (b->prod->flags & SI_FL_WAIT_ROOM)))
790 b->prod->chk_rcv(b->prod);
791
792 /* we have to wake up if there is a special event or if we don't have
793 * any more data to forward and it's not planned to send any more.
794 */
795 if (likely((b->flags & (BF_WRITE_NULL|BF_WRITE_ERROR|BF_SHUTW)) ||
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200796 ((b->flags & BF_OUT_EMPTY) && !b->to_forward) ||
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100797 si->state != SI_ST_EST ||
798 b->prod->state != SI_ST_EST))
799 task_wakeup(si->owner, TASK_WOKEN_IO);
800 }
801
802 fdtab[fd].ev &= ~FD_POLL_OUT;
803 return retval;
Willy Tarreauac128fe2009-01-09 13:05:19 +0100804
Willy Tarreau6996e152007-04-30 14:37:43 +0200805 out_error:
Willy Tarreaucff64112008-11-03 06:26:53 +0100806 /* Write error on the file descriptor. We mark the FD as STERROR so
807 * that we don't use it anymore. The error is reported to the stream
808 * interface which will take proper action. We must not perturbate the
809 * buffer because the stream interface wants to ensure transparent
810 * connection retries.
Willy Tarreau6996e152007-04-30 14:37:43 +0200811 */
Willy Tarreaucff64112008-11-03 06:26:53 +0100812
Willy Tarreau6996e152007-04-30 14:37:43 +0200813 fdtab[fd].state = FD_STERROR;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100814 fdtab[fd].ev &= ~FD_POLL_STICKY;
Willy Tarreau1714e0f2009-03-28 20:54:53 +0100815 EV_FD_REM(fd);
Willy Tarreaucff64112008-11-03 06:26:53 +0100816 si->flags |= SI_FL_ERR;
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200817 task_wakeup(si->owner, TASK_WOKEN_IO);
818 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200819}
820
Willy Tarreau48adac52008-08-30 04:58:38 +0200821/*
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200822 * This function performs a shutdown-write on a stream interface in a connected or
823 * init state (it does nothing for other states). It either shuts the write side
Willy Tarreau99126c32008-11-27 10:30:51 +0100824 * or closes the file descriptor and marks itself as closed. The buffer flags are
825 * updated to reflect the new state.
Willy Tarreau48adac52008-08-30 04:58:38 +0200826 */
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100827void stream_sock_shutw(struct stream_interface *si)
Willy Tarreau48adac52008-08-30 04:58:38 +0200828{
Willy Tarreau418fd472009-09-06 21:37:23 +0200829 si->ob->flags &= ~BF_SHUTW_NOW;
Willy Tarreau99126c32008-11-27 10:30:51 +0100830 if (si->ob->flags & BF_SHUTW)
831 return;
832 si->ob->flags |= BF_SHUTW;
833 si->ob->wex = TICK_ETERNITY;
Willy Tarreaub0ef7352008-12-14 13:26:20 +0100834 si->flags &= ~SI_FL_WAIT_DATA;
Willy Tarreau99126c32008-11-27 10:30:51 +0100835
Willy Tarreaub38903c2008-11-23 21:33:29 +0100836 switch (si->state) {
Willy Tarreaub38903c2008-11-23 21:33:29 +0100837 case SI_ST_EST:
Willy Tarreau720058c2009-07-14 19:21:50 +0200838 /* we have to shut before closing, otherwise some short messages
839 * may never leave the system, especially when there are remaining
840 * unread data in the socket input buffer, or when nolinger is set.
Willy Tarreau4c283dc2009-12-29 14:36:34 +0100841 * However, if SI_FL_NOLINGER is explicitly set, we know there is
842 * no risk so we close both sides immediately.
Willy Tarreau720058c2009-07-14 19:21:50 +0200843 */
Willy Tarreau4c283dc2009-12-29 14:36:34 +0100844 if (si->flags & SI_FL_NOLINGER) {
845 si->flags &= ~SI_FL_NOLINGER;
846 setsockopt(si->fd, SOL_SOCKET, SO_LINGER,
847 (struct linger *) &nolinger, sizeof(struct linger));
848 } else {
849 EV_FD_CLR(si->fd, DIR_WR);
850 shutdown(si->fd, SHUT_WR);
Willy Tarreau720058c2009-07-14 19:21:50 +0200851
Willy Tarreau4c283dc2009-12-29 14:36:34 +0100852 if (!(si->ib->flags & (BF_SHUTR|BF_DONT_READ)))
853 return;
854 }
Willy Tarreau5d707e12009-06-28 11:09:07 +0200855
Willy Tarreaub38903c2008-11-23 21:33:29 +0100856 /* fall through */
857 case SI_ST_CON:
Willy Tarreau8bfa4262008-11-27 09:25:45 +0100858 /* we may have to close a pending connection, and mark the
859 * response buffer as shutr
860 */
Willy Tarreau48adac52008-08-30 04:58:38 +0200861 fd_delete(si->fd);
Willy Tarreaufe3718a2008-11-30 18:14:12 +0100862 /* fall through */
863 case SI_ST_CER:
Willy Tarreau7f006512008-12-07 14:04:04 +0100864 si->state = SI_ST_DIS;
865 default:
Willy Tarreaud06e7112009-03-29 10:18:41 +0200866 si->flags &= ~SI_FL_WAIT_ROOM;
Willy Tarreau99126c32008-11-27 10:30:51 +0100867 si->ib->flags |= BF_SHUTR;
Willy Tarreaufe3718a2008-11-30 18:14:12 +0100868 si->ib->rex = TICK_ETERNITY;
Willy Tarreau127334e2009-03-28 10:47:26 +0100869 si->exp = TICK_ETERNITY;
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100870 return;
Willy Tarreau48adac52008-08-30 04:58:38 +0200871 }
Willy Tarreau48adac52008-08-30 04:58:38 +0200872}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200873
Willy Tarreau2d212792008-08-27 21:41:35 +0200874/*
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200875 * This function performs a shutdown-read on a stream interface in a connected or
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100876 * init state (it does nothing for other states). It either shuts the read side
Willy Tarreau99126c32008-11-27 10:30:51 +0100877 * or closes the file descriptor and marks itself as closed. The buffer flags are
878 * updated to reflect the new state.
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200879 */
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100880void stream_sock_shutr(struct stream_interface *si)
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200881{
Willy Tarreau418fd472009-09-06 21:37:23 +0200882 si->ib->flags &= ~BF_SHUTR_NOW;
Willy Tarreau99126c32008-11-27 10:30:51 +0100883 if (si->ib->flags & BF_SHUTR)
884 return;
885 si->ib->flags |= BF_SHUTR;
886 si->ib->rex = TICK_ETERNITY;
Willy Tarreaub0ef7352008-12-14 13:26:20 +0100887 si->flags &= ~SI_FL_WAIT_ROOM;
Willy Tarreau99126c32008-11-27 10:30:51 +0100888
Willy Tarreau8bfa4262008-11-27 09:25:45 +0100889 if (si->state != SI_ST_EST && si->state != SI_ST_CON)
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100890 return;
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200891
Willy Tarreaucff64112008-11-03 06:26:53 +0100892 if (si->ob->flags & BF_SHUTW) {
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200893 fd_delete(si->fd);
Willy Tarreau74ab2ac2008-11-23 17:23:07 +0100894 si->state = SI_ST_DIS;
Willy Tarreau127334e2009-03-28 10:47:26 +0100895 si->exp = TICK_ETERNITY;
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100896 return;
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200897 }
898 EV_FD_CLR(si->fd, DIR_RD);
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100899 return;
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200900}
901
902/*
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200903 * Updates a connected stream_sock file descriptor status and timeouts
904 * according to the buffers' flags. It should only be called once after the
905 * buffer flags have settled down, and before they are cleared. It doesn't
906 * harm to call it as often as desired (it just slightly hurts performance).
907 */
Willy Tarreaub0253252008-11-30 21:37:12 +0100908void stream_sock_data_finish(struct stream_interface *si)
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200909{
Willy Tarreaub0253252008-11-30 21:37:12 +0100910 struct buffer *ib = si->ib;
911 struct buffer *ob = si->ob;
912 int fd = si->fd;
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200913
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200914 DPRINTF(stderr,"[%u] %s: fd=%d owner=%p ib=%p, ob=%p, exp(r,w)=%u,%u ibf=%08x obf=%08x ibl=%d obl=%d si=%d\n",
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200915 now_ms, __FUNCTION__,
916 fd, fdtab[fd].owner,
917 ib, ob,
918 ib->rex, ob->wex,
919 ib->flags, ob->flags,
Willy Tarreaub0253252008-11-30 21:37:12 +0100920 ib->l, ob->l, si->state);
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200921
922 /* Check if we need to close the read side */
923 if (!(ib->flags & BF_SHUTR)) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200924 /* Read not closed, update FD status and timeout for reads */
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200925 if (ib->flags & (BF_FULL|BF_HIJACK|BF_DONT_READ)) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200926 /* stop reading */
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200927 if ((ib->flags & (BF_FULL|BF_HIJACK|BF_DONT_READ)) == BF_FULL)
Willy Tarreaub0ef7352008-12-14 13:26:20 +0100928 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau2d212792008-08-27 21:41:35 +0200929 EV_FD_COND_C(fd, DIR_RD);
930 ib->rex = TICK_ETERNITY;
931 }
932 else {
933 /* (re)start reading and update timeout. Note: we don't recompute the timeout
934 * everytime we get here, otherwise it would risk never to expire. We only
Willy Tarreaufe8903c2009-10-04 10:56:08 +0200935 * update it if is was not yet set. The stream socket handler will already
936 * have updated it if there has been a completed I/O.
Willy Tarreau2d212792008-08-27 21:41:35 +0200937 */
Willy Tarreaub0ef7352008-12-14 13:26:20 +0100938 si->flags &= ~SI_FL_WAIT_ROOM;
Willy Tarreau2d212792008-08-27 21:41:35 +0200939 EV_FD_COND_S(fd, DIR_RD);
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200940 if (!(ib->flags & (BF_READ_NOEXP|BF_DONT_READ)) && !tick_isset(ib->rex))
Willy Tarreau2d212792008-08-27 21:41:35 +0200941 ib->rex = tick_add_ifset(now_ms, ib->rto);
942 }
943 }
944
945 /* Check if we need to close the write side */
946 if (!(ob->flags & BF_SHUTW)) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200947 /* Write not closed, update FD status and timeout for writes */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200948 if (ob->flags & BF_OUT_EMPTY) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200949 /* stop writing */
Willy Tarreau59454bf2009-09-20 11:13:40 +0200950 if ((ob->flags & (BF_FULL|BF_HIJACK|BF_SHUTW_NOW)) == 0)
Willy Tarreaub0ef7352008-12-14 13:26:20 +0100951 si->flags |= SI_FL_WAIT_DATA;
Willy Tarreau2d212792008-08-27 21:41:35 +0200952 EV_FD_COND_C(fd, DIR_WR);
953 ob->wex = TICK_ETERNITY;
954 }
955 else {
956 /* (re)start writing and update timeout. Note: we don't recompute the timeout
957 * everytime we get here, otherwise it would risk never to expire. We only
Willy Tarreaufe8903c2009-10-04 10:56:08 +0200958 * update it if is was not yet set. The stream socket handler will already
959 * have updated it if there has been a completed I/O.
Willy Tarreau2d212792008-08-27 21:41:35 +0200960 */
Willy Tarreaub0ef7352008-12-14 13:26:20 +0100961 si->flags &= ~SI_FL_WAIT_DATA;
Willy Tarreau2d212792008-08-27 21:41:35 +0200962 EV_FD_COND_S(fd, DIR_WR);
Willy Tarreaufe8903c2009-10-04 10:56:08 +0200963 if (!tick_isset(ob->wex)) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200964 ob->wex = tick_add_ifset(now_ms, ob->wto);
Willy Tarreauf27b5ea2009-10-03 22:01:18 +0200965 if (tick_isset(ib->rex) && !(si->flags & SI_FL_INDEP_STR)) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200966 /* Note: depending on the protocol, we don't know if we're waiting
967 * for incoming data or not. So in order to prevent the socket from
968 * expiring read timeouts during writes, we refresh the read timeout,
Willy Tarreauf27b5ea2009-10-03 22:01:18 +0200969 * except if it was already infinite or if we have explicitly setup
970 * independant streams.
Willy Tarreau2d212792008-08-27 21:41:35 +0200971 */
Willy Tarreaud06e7112009-03-29 10:18:41 +0200972 ib->rex = tick_add_ifset(now_ms, ib->rto);
Willy Tarreau2d212792008-08-27 21:41:35 +0200973 }
974 }
975 }
976 }
Willy Tarreau2d212792008-08-27 21:41:35 +0200977}
978
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100979/* This function is used for inter-stream-interface calls. It is called by the
980 * consumer to inform the producer side that it may be interested in checking
981 * for free space in the buffer. Note that it intentionally does not update
982 * timeouts, so that we can still check them later at wake-up.
983 */
984void stream_sock_chk_rcv(struct stream_interface *si)
985{
986 struct buffer *ib = si->ib;
987
988 DPRINTF(stderr,"[%u] %s: fd=%d owner=%p ib=%p, ob=%p, exp(r,w)=%u,%u ibf=%08x obf=%08x ibl=%d obl=%d si=%d\n",
989 now_ms, __FUNCTION__,
Vincenzo Farruggia9b97cff2009-01-30 16:49:10 +0000990 si->fd, fdtab[si->fd].owner,
991 ib, si->ob,
992 ib->rex, si->ob->wex,
993 ib->flags, si->ob->flags,
994 ib->l, si->ob->l, si->state);
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100995
996 if (unlikely(si->state != SI_ST_EST || (ib->flags & BF_SHUTR)))
997 return;
998
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200999 if (ib->flags & (BF_FULL|BF_HIJACK|BF_DONT_READ)) {
Willy Tarreau3ffeba12008-12-14 14:42:35 +01001000 /* stop reading */
Willy Tarreauf1ba4b32009-10-17 14:37:52 +02001001 if ((ib->flags & (BF_FULL|BF_HIJACK|BF_DONT_READ)) == BF_FULL)
Willy Tarreau3ffeba12008-12-14 14:42:35 +01001002 si->flags |= SI_FL_WAIT_ROOM;
1003 EV_FD_COND_C(si->fd, DIR_RD);
1004 }
1005 else {
1006 /* (re)start reading */
1007 si->flags &= ~SI_FL_WAIT_ROOM;
1008 EV_FD_COND_S(si->fd, DIR_RD);
1009 }
1010}
1011
1012
1013/* This function is used for inter-stream-interface calls. It is called by the
1014 * producer to inform the consumer side that it may be interested in checking
1015 * for data in the buffer. Note that it intentionally does not update timeouts,
1016 * so that we can still check them later at wake-up.
1017 */
1018void stream_sock_chk_snd(struct stream_interface *si)
1019{
1020 struct buffer *ob = si->ob;
Willy Tarreaua456f2a2009-01-18 17:38:44 +01001021 int retval;
Willy Tarreau3ffeba12008-12-14 14:42:35 +01001022
1023 DPRINTF(stderr,"[%u] %s: fd=%d owner=%p ib=%p, ob=%p, exp(r,w)=%u,%u ibf=%08x obf=%08x ibl=%d obl=%d si=%d\n",
1024 now_ms, __FUNCTION__,
Vincenzo Farruggia9b97cff2009-01-30 16:49:10 +00001025 si->fd, fdtab[si->fd].owner,
1026 si->ib, ob,
1027 si->ib->rex, ob->wex,
1028 si->ib->flags, ob->flags,
1029 si->ib->l, ob->l, si->state);
Willy Tarreau3ffeba12008-12-14 14:42:35 +01001030
1031 if (unlikely(si->state != SI_ST_EST || (ob->flags & BF_SHUTW)))
1032 return;
1033
Willy Tarreaua456f2a2009-01-18 17:38:44 +01001034 if (!(si->flags & SI_FL_WAIT_DATA) || /* not waiting for data */
1035 (fdtab[si->fd].ev & FD_POLL_OUT) || /* we'll be called anyway */
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001036 (ob->flags & BF_OUT_EMPTY)) /* called with nothing to send ! */
Willy Tarreaua456f2a2009-01-18 17:38:44 +01001037 return;
1038
1039 retval = stream_sock_write_loop(si, ob);
Willy Tarreauc54aef32009-07-27 20:08:06 +02001040 /* here, we have :
1041 * retval < 0 if an error was encountered during write.
1042 * retval = 0 if we can't write anymore without polling
1043 * retval = 1 if we're invited to come back when desired
1044 */
Willy Tarreaua456f2a2009-01-18 17:38:44 +01001045 if (retval < 0) {
1046 /* Write error on the file descriptor. We mark the FD as STERROR so
1047 * that we don't use it anymore and we notify the task.
1048 */
1049 fdtab[si->fd].state = FD_STERROR;
1050 fdtab[si->fd].ev &= ~FD_POLL_STICKY;
Willy Tarreau1714e0f2009-03-28 20:54:53 +01001051 EV_FD_REM(si->fd);
Willy Tarreaua456f2a2009-01-18 17:38:44 +01001052 si->flags |= SI_FL_ERR;
1053 goto out_wakeup;
1054 }
1055
Willy Tarreauc54aef32009-07-27 20:08:06 +02001056 /* OK, so now we know that retval >= 0 means that some data might have
1057 * been sent, and that we may have to poll first. We have to do that
1058 * too if the buffer is not empty.
1059 */
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001060 if (ob->flags & BF_OUT_EMPTY) {
Willy Tarreaua456f2a2009-01-18 17:38:44 +01001061 /* the connection is established but we can't write. Either the
1062 * buffer is empty, or we just refrain from sending because the
1063 * send_max limit was reached. Maybe we just wrote the last
1064 * chunk and need to close.
1065 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02001066 if (((ob->flags & (BF_SHUTW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTW_NOW)) ==
1067 (BF_AUTO_CLOSE|BF_SHUTW_NOW)) &&
Willy Tarreaua456f2a2009-01-18 17:38:44 +01001068 (si->state == SI_ST_EST)) {
1069 stream_sock_shutw(si);
1070 goto out_wakeup;
1071 }
Willy Tarreaud06e7112009-03-29 10:18:41 +02001072
Willy Tarreau59454bf2009-09-20 11:13:40 +02001073 if ((ob->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_FULL|BF_HIJACK)) == 0)
Willy Tarreau3ffeba12008-12-14 14:42:35 +01001074 si->flags |= SI_FL_WAIT_DATA;
Willy Tarreaua456f2a2009-01-18 17:38:44 +01001075 ob->wex = TICK_ETERNITY;
Willy Tarreau3ffeba12008-12-14 14:42:35 +01001076 }
1077 else {
Willy Tarreauc54aef32009-07-27 20:08:06 +02001078 /* Otherwise there are remaining data to be sent in the buffer,
1079 * which means we have to poll before doing so.
1080 */
Willy Tarreau3ffeba12008-12-14 14:42:35 +01001081 EV_FD_COND_S(si->fd, DIR_WR);
Willy Tarreauc54aef32009-07-27 20:08:06 +02001082 si->flags &= ~SI_FL_WAIT_DATA;
1083 if (!tick_isset(ob->wex))
1084 ob->wex = tick_add_ifset(now_ms, ob->wto);
Willy Tarreau3ffeba12008-12-14 14:42:35 +01001085 }
Willy Tarreaua456f2a2009-01-18 17:38:44 +01001086
Willy Tarreauc9619462009-03-09 22:40:57 +01001087 if (likely(ob->flags & BF_WRITE_ACTIVITY)) {
1088 /* update timeout if we have written something */
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001089 if ((ob->flags & (BF_OUT_EMPTY|BF_SHUTW|BF_WRITE_PARTIAL)) == BF_WRITE_PARTIAL)
Willy Tarreauc9619462009-03-09 22:40:57 +01001090 ob->wex = tick_add_ifset(now_ms, ob->wto);
1091
Willy Tarreauf27b5ea2009-10-03 22:01:18 +02001092 if (tick_isset(si->ib->rex) && !(si->flags & SI_FL_INDEP_STR)) {
Willy Tarreauc9619462009-03-09 22:40:57 +01001093 /* Note: to prevent the client from expiring read timeouts
Willy Tarreauf27b5ea2009-10-03 22:01:18 +02001094 * during writes, we refresh it. We only do this if the
1095 * interface is not configured for "independant streams",
1096 * because for some applications it's better not to do this,
1097 * for instance when continuously exchanging small amounts
1098 * of data which can full the socket buffers long before a
1099 * write timeout is detected.
Willy Tarreauc9619462009-03-09 22:40:57 +01001100 */
1101 si->ib->rex = tick_add_ifset(now_ms, si->ib->rto);
1102 }
1103 }
1104
Willy Tarreaua456f2a2009-01-18 17:38:44 +01001105 /* in case of special condition (error, shutdown, end of write...), we
1106 * have to notify the task.
1107 */
1108 if (likely((ob->flags & (BF_WRITE_NULL|BF_WRITE_ERROR|BF_SHUTW)) ||
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001109 ((ob->flags & BF_OUT_EMPTY) && !ob->to_forward) ||
Willy Tarreaua456f2a2009-01-18 17:38:44 +01001110 si->state != SI_ST_EST)) {
1111 out_wakeup:
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001112 if (!(si->flags & SI_FL_DONT_WAKE) && si->owner)
1113 task_wakeup(si->owner, TASK_WOKEN_IO);
Willy Tarreaua456f2a2009-01-18 17:38:44 +01001114 }
Willy Tarreau3ffeba12008-12-14 14:42:35 +01001115}
1116
Willy Tarreaubaaee002006-06-26 02:48:02 +02001117
1118/*
1119 * Local variables:
1120 * c-indent-level: 8
1121 * c-basic-offset: 8
1122 * End:
1123 */