blob: 479222aee2d66bd0ca7a81f8c09f90d12faabd91 [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
87/* Returns :
88 * -1 if splice is not possible or not possible anymore and we must switch to
89 * user-land copy (eg: to_forward reached)
90 * 0 when we know that polling is required to get more data (EAGAIN)
91 * 1 for all other cases (we can safely try again, or if an activity has been
92 * detected (DATA/NULL/ERR))
93 * Sets :
94 * BF_READ_NULL
95 * BF_READ_PARTIAL
96 * BF_WRITE_PARTIAL (during copy)
Willy Tarreauba0b63d2009-09-20 08:09:44 +020097 * BF_OUT_EMPTY (during copy)
Willy Tarreau5bd8c372009-01-19 00:32:22 +010098 * SI_FL_ERR
99 * SI_FL_WAIT_ROOM
100 * (SI_FL_WAIT_RECV)
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100101 *
102 * This function automatically allocates a pipe from the pipe pool. It also
103 * carefully ensures to clear b->pipe whenever it leaves the pipe empty.
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100104 */
105static int stream_sock_splice_in(struct buffer *b, struct stream_interface *si)
106{
107 int fd = si->fd;
Willy Tarreau31971e52009-09-20 12:07:52 +0200108 int ret;
109 unsigned long max;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100110 int retval = 1;
111
112 if (!b->to_forward)
113 return -1;
114
115 if (!(b->flags & BF_KERN_SPLICING))
116 return -1;
117
118 if (b->l) {
119 /* We're embarrassed, there are already data pending in
120 * the buffer and we don't want to have them at two
121 * locations at a time. Let's indicate we need some
122 * place and ask the consumer to hurry.
123 */
124 si->flags |= SI_FL_WAIT_ROOM;
125 EV_FD_CLR(fd, DIR_RD);
126 b->rex = TICK_ETERNITY;
127 b->cons->chk_snd(b->cons);
128 return 1;
129 }
130
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100131 if (unlikely(b->pipe == NULL)) {
132 if (pipes_used >= global.maxpipes || !(b->pipe = get_pipe())) {
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100133 b->flags &= ~BF_KERN_SPLICING;
134 return -1;
135 }
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100136 }
137
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100138 /* At this point, b->pipe is valid */
139
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100140 while (1) {
141 max = b->to_forward;
Willy Tarreau31971e52009-09-20 12:07:52 +0200142 if (!max) {
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100143 /* It looks like the buffer + the pipe already contain
144 * the maximum amount of data to be transferred. Try to
145 * send those data immediately on the other side if it
146 * is currently waiting.
147 */
148 retval = -1; /* end of forwarding */
149 break;
150 }
151
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100152 ret = splice(fd, NULL, b->pipe->prod, NULL, max,
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100153 SPLICE_F_MOVE|SPLICE_F_NONBLOCK);
154
155 if (ret <= 0) {
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100156 if (ret == 0) {
Willy Tarreau98b306b2009-01-25 11:11:32 +0100157 /* connection closed. This is only detected by
158 * recent kernels (>= 2.6.27.13).
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100159 */
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100160 b->flags |= BF_READ_NULL;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100161 retval = 1; /* no need for further polling */
162 break;
163 }
164
165 if (errno == EAGAIN) {
166 /* there are two reasons for EAGAIN :
167 * - nothing in the socket buffer (standard)
168 * - pipe is full
Willy Tarreau98b306b2009-01-25 11:11:32 +0100169 * - the connection is closed (kernel < 2.6.27.13)
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100170 * Since we don't know if pipe is full, we'll
171 * stop if the pipe is not empty. Anyway, we
172 * will almost always fill/empty the pipe.
173 */
174
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100175 if (b->pipe->data) {
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100176 si->flags |= SI_FL_WAIT_ROOM;
177 retval = 1;
178 break;
179 }
180
Willy Tarreau98b306b2009-01-25 11:11:32 +0100181 /* We don't know if the connection was closed.
182 * But if we're called upon POLLIN with an empty
183 * pipe and get EAGAIN, it is suspect enought to
184 * try to fall back to the normal recv scheme
185 * which will be able to deal with the situation.
186 */
187 retval = -1;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100188 break;
189 }
Willy Tarreaudc340a92009-06-28 23:10:19 +0200190
191 if (errno == ENOSYS) {
192 /* splice not supported on this end, disable it */
193 b->flags &= ~BF_KERN_SPLICING;
194 si->flags &= ~SI_FL_CAP_SPLICE;
195 put_pipe(b->pipe);
196 b->pipe = NULL;
197 return -1;
198 }
199
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100200 /* here we have another error */
201 si->flags |= SI_FL_ERR;
202 retval = 1;
203 break;
204 } /* ret <= 0 */
205
Willy Tarreau31971e52009-09-20 12:07:52 +0200206 if (b->to_forward != BUF_INFINITE_FORWARD)
207 b->to_forward -= ret;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100208 b->total += ret;
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100209 b->pipe->data += ret;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100210 b->flags |= BF_READ_PARTIAL;
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200211 b->flags &= ~BF_OUT_EMPTY;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100212
Willy Tarreau6f4a82c2009-03-21 20:43:57 +0100213 if (b->pipe->data >= SPLICE_FULL_HINT ||
214 ret >= global.tune.recv_enough) {
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100215 /* We've read enough of it for this time. */
216 retval = 1;
217 break;
218 }
219 } /* while */
220
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100221 if (unlikely(!b->pipe->data)) {
222 put_pipe(b->pipe);
223 b->pipe = NULL;
224 }
225
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100226 return retval;
227}
228
Willy Tarreau6b4aad42009-01-18 21:59:13 +0100229#endif /* CONFIG_HAP_LINUX_SPLICE */
230
231
Willy Tarreaubaaee002006-06-26 02:48:02 +0200232/*
Willy Tarreaud7971282006-07-29 18:36:34 +0200233 * this function is called on a read event from a stream socket.
Willy Tarreau83749182007-04-15 20:56:27 +0200234 * It returns 0 if we have a high confidence that we will not be
235 * able to read more data without polling first. Returns non-zero
236 * otherwise.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200237 */
Willy Tarreaud7971282006-07-29 18:36:34 +0200238int stream_sock_read(int fd) {
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200239 struct stream_interface *si = fdtab[fd].owner;
Willy Tarreau48adac52008-08-30 04:58:38 +0200240 struct buffer *b = si->ib;
Willy Tarreau8a7af602008-05-03 23:07:14 +0200241 int ret, max, retval, cur_read;
Willy Tarreaub8949f12007-03-23 22:39:59 +0100242 int read_poll = MAX_READ_POLL_LOOPS;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200243
244#ifdef DEBUG_FULL
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100245 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 +0200246#endif
247
Willy Tarreau83749182007-04-15 20:56:27 +0200248 retval = 1;
249
Willy Tarreau71543652009-07-14 19:55:05 +0200250 /* stop immediately on errors. Note that we DON'T want to stop on
251 * POLL_ERR, as the poller might report a write error while there
252 * are still data available in the recv buffer. This typically
253 * happens when we send too large a request to a backend server
254 * which rejects it before reading it all.
255 */
256 if (fdtab[fd].state == FD_STERROR)
Willy Tarreau6996e152007-04-30 14:37:43 +0200257 goto out_error;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100258
259 /* stop here if we reached the end of data */
260 if ((fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP)) == FD_POLL_HUP)
261 goto out_shutdown_r;
Willy Tarreau83749182007-04-15 20:56:27 +0200262
Willy Tarreaud06e7112009-03-29 10:18:41 +0200263 /* maybe we were called immediately after an asynchronous shutr */
264 if (b->flags & BF_SHUTR)
265 goto out_wakeup;
266
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100267#if defined(CONFIG_HAP_LINUX_SPLICE)
268 if (b->to_forward && b->flags & BF_KERN_SPLICING) {
Willy Tarreau98b306b2009-01-25 11:11:32 +0100269
270 /* Under Linux, if FD_POLL_HUP is set, we have reached the end.
271 * Since older splice() implementations were buggy and returned
272 * EAGAIN on end of read, let's bypass the call to splice() now.
273 */
274 if (fdtab[fd].ev & FD_POLL_HUP)
275 goto out_shutdown_r;
276
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100277 retval = stream_sock_splice_in(b, si);
278
279 if (retval >= 0) {
280 if (si->flags & SI_FL_ERR)
281 goto out_error;
282 if (b->flags & BF_READ_NULL)
283 goto out_shutdown_r;
284 goto out_wakeup;
285 }
286 /* splice not possible (anymore), let's go on on standard copy */
287 }
288#endif
Willy Tarreau8a7af602008-05-03 23:07:14 +0200289 cur_read = 0;
Willy Tarreau6996e152007-04-30 14:37:43 +0200290 while (1) {
291 /*
292 * 1. compute the maximum block size we can read at once.
293 */
Willy Tarreau03d60bb2009-01-09 11:13:00 +0100294 if (b->l == 0) {
295 /* let's realign the buffer to optimize I/O */
296 b->r = b->w = b->lr = b->data;
297 max = b->max_len;
Willy Tarreau83749182007-04-15 20:56:27 +0200298 }
299 else if (b->r > b->w) {
Willy Tarreau03d60bb2009-01-09 11:13:00 +0100300 max = b->data + b->max_len - b->r;
Willy Tarreau83749182007-04-15 20:56:27 +0200301 }
302 else {
303 max = b->w - b->r;
Willy Tarreau03d60bb2009-01-09 11:13:00 +0100304 if (max > b->max_len)
305 max = b->max_len;
Willy Tarreau83749182007-04-15 20:56:27 +0200306 }
Willy Tarreau74ab2ac2008-11-23 17:23:07 +0100307
Willy Tarreau9c0fe592009-01-18 16:25:31 +0100308 if (max == 0) {
309 b->flags |= BF_FULL;
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100310 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau9c0fe592009-01-18 16:25:31 +0100311 break;
312 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200313
Willy Tarreau6996e152007-04-30 14:37:43 +0200314 /*
315 * 2. read the largest possible block
316 */
Willy Tarreaud6d06902009-08-19 11:22:33 +0200317 if (MSG_NOSIGNAL) {
318 ret = recv(fd, b->r, max, MSG_NOSIGNAL);
319 } else {
Willy Tarreau83749182007-04-15 20:56:27 +0200320 int skerr;
321 socklen_t lskerr = sizeof(skerr);
322
323 ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr);
324 if (ret == -1 || skerr)
325 ret = -1;
326 else
327 ret = recv(fd, b->r, max, 0);
328 }
Willy Tarreaud6d06902009-08-19 11:22:33 +0200329
Willy Tarreau83749182007-04-15 20:56:27 +0200330 if (ret > 0) {
331 b->r += ret;
332 b->l += ret;
Willy Tarreau8a7af602008-05-03 23:07:14 +0200333 cur_read += ret;
Willy Tarreaub38903c2008-11-23 21:33:29 +0100334
Willy Tarreau0abebcc2009-01-08 00:09:41 +0100335 /* if we're allowed to directly forward data, we must update send_max */
Willy Tarreau31971e52009-09-20 12:07:52 +0200336 if (b->to_forward && !(b->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
337 unsigned long fwd = ret;
338 if (b->to_forward != BUF_INFINITE_FORWARD) {
339 if (fwd > b->to_forward)
340 fwd = b->to_forward;
341 b->to_forward -= fwd;
342 }
343 b->send_max += fwd;
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200344 b->flags &= ~BF_OUT_EMPTY;
Willy Tarreau0abebcc2009-01-08 00:09:41 +0100345 }
Willy Tarreauf890dc92008-12-13 21:12:26 +0100346
Willy Tarreaub38903c2008-11-23 21:33:29 +0100347 if (fdtab[fd].state == FD_STCONN)
348 fdtab[fd].state = FD_STREADY;
349
Willy Tarreau3da77c52008-08-29 09:58:42 +0200350 b->flags |= BF_READ_PARTIAL;
Willy Tarreau74ab2ac2008-11-23 17:23:07 +0100351
Willy Tarreaua07a34e2009-08-16 23:27:46 +0200352 if (b->r == b->data + b->size) {
Willy Tarreau83749182007-04-15 20:56:27 +0200353 b->r = b->data; /* wrap around the buffer */
354 }
Willy Tarreau9641e8f2007-03-23 23:02:09 +0100355
Willy Tarreau83749182007-04-15 20:56:27 +0200356 b->total += ret;
Willy Tarreau9641e8f2007-03-23 23:02:09 +0100357
Willy Tarreau03d60bb2009-01-09 11:13:00 +0100358 if (b->l >= b->max_len) {
Willy Tarreau6996e152007-04-30 14:37:43 +0200359 /* The buffer is now full, there's no point in going through
360 * the loop again.
361 */
Willy Tarreau8a7af602008-05-03 23:07:14 +0200362 if (!(b->flags & BF_STREAMER_FAST) && (cur_read == b->l)) {
363 b->xfer_small = 0;
364 b->xfer_large++;
365 if (b->xfer_large >= 3) {
366 /* we call this buffer a fast streamer if it manages
367 * to be filled in one call 3 consecutive times.
368 */
369 b->flags |= (BF_STREAMER | BF_STREAMER_FAST);
370 //fputc('+', stderr);
371 }
372 }
373 else if ((b->flags & (BF_STREAMER | BF_STREAMER_FAST)) &&
Willy Tarreaua07a34e2009-08-16 23:27:46 +0200374 (cur_read <= b->size / 2)) {
Willy Tarreau8a7af602008-05-03 23:07:14 +0200375 b->xfer_large = 0;
376 b->xfer_small++;
377 if (b->xfer_small >= 2) {
378 /* if the buffer has been at least half full twice,
379 * we receive faster than we send, so at least it
380 * is not a "fast streamer".
381 */
382 b->flags &= ~BF_STREAMER_FAST;
383 //fputc('-', stderr);
384 }
385 }
386 else {
387 b->xfer_small = 0;
388 b->xfer_large = 0;
389 }
Willy Tarreau9c0fe592009-01-18 16:25:31 +0100390
391 b->flags |= BF_FULL;
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100392 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau9c0fe592009-01-18 16:25:31 +0100393 break;
Willy Tarreau6996e152007-04-30 14:37:43 +0200394 }
395
Willy Tarreauab3e1d32007-06-03 14:10:36 +0200396 /* if too many bytes were missing from last read, it means that
397 * it's pointless trying to read again because the system does
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100398 * not have them in buffers. BTW, if FD_POLL_HUP was present,
399 * it means that we have reached the end and that the connection
400 * is closed.
Willy Tarreauab3e1d32007-06-03 14:10:36 +0200401 */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100402 if (ret < max) {
Willy Tarreau8a7af602008-05-03 23:07:14 +0200403 if ((b->flags & (BF_STREAMER | BF_STREAMER_FAST)) &&
Willy Tarreaua07a34e2009-08-16 23:27:46 +0200404 (cur_read <= b->size / 2)) {
Willy Tarreau8a7af602008-05-03 23:07:14 +0200405 b->xfer_large = 0;
406 b->xfer_small++;
407 if (b->xfer_small >= 3) {
408 /* we have read less than half of the buffer in
409 * one pass, and this happened at least 3 times.
410 * This is definitely not a streamer.
411 */
412 b->flags &= ~(BF_STREAMER | BF_STREAMER_FAST);
413 //fputc('!', stderr);
414 }
415 }
Willy Tarreau2bea3a12008-08-28 09:47:43 +0200416 /* unfortunately, on level-triggered events, POLL_HUP
417 * is generally delivered AFTER the system buffer is
418 * empty, so this one might never match.
419 */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100420 if (fdtab[fd].ev & FD_POLL_HUP)
421 goto out_shutdown_r;
Willy Tarreau2bea3a12008-08-28 09:47:43 +0200422
423 /* if a streamer has read few data, it may be because we
424 * have exhausted system buffers. It's not worth trying
425 * again.
426 */
427 if (b->flags & BF_STREAMER)
428 break;
Willy Tarreauab3e1d32007-06-03 14:10:36 +0200429
Willy Tarreau6f4a82c2009-03-21 20:43:57 +0100430 /* generally if we read something smaller than 1 or 2 MSS,
431 * it means that either we have exhausted the system's
432 * buffers (streamer or question-response protocol) or
433 * that the connection will be closed. Streamers are
434 * easily detected so we return early. For other cases,
435 * it's still better to perform a last read to be sure,
436 * because it may save one complete poll/read/wakeup cycle
437 * in case of shutdown.
438 */
439 if (ret < MIN_RET_FOR_READ_LOOP && b->flags & BF_STREAMER)
440 break;
441
442 /* if we read a large block smaller than what we requested,
443 * it's almost certain we'll never get anything more.
444 */
445 if (ret >= global.tune.recv_enough)
446 break;
447 }
Willy Tarreau83749182007-04-15 20:56:27 +0200448
Willy Tarreau1b194fe2009-03-21 21:10:04 +0100449 if ((b->flags & BF_READ_DONTWAIT) || --read_poll <= 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200450 break;
Willy Tarreau83749182007-04-15 20:56:27 +0200451 }
452 else if (ret == 0) {
Willy Tarreau6996e152007-04-30 14:37:43 +0200453 /* connection closed */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100454 goto out_shutdown_r;
Willy Tarreau83749182007-04-15 20:56:27 +0200455 }
Willy Tarreau9f195292007-04-15 21:26:58 +0200456 else if (errno == EAGAIN) {
457 /* Ignore EAGAIN but inform the poller that there is
Willy Tarreauaf78d0f2009-01-08 10:09:08 +0100458 * nothing to read left if we did not read much, ie
459 * less than what we were still expecting to read.
460 * But we may have done some work justifying to notify
461 * the task.
Willy Tarreau9f195292007-04-15 21:26:58 +0200462 */
Willy Tarreauaf78d0f2009-01-08 10:09:08 +0100463 if (cur_read < MIN_RET_FOR_READ_LOOP)
464 retval = 0;
Willy Tarreau83749182007-04-15 20:56:27 +0200465 break;
466 }
467 else {
Willy Tarreau6996e152007-04-30 14:37:43 +0200468 goto out_error;
Willy Tarreau83749182007-04-15 20:56:27 +0200469 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200470 } /* while (1) */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200471
Willy Tarreau6996e152007-04-30 14:37:43 +0200472 out_wakeup:
Willy Tarreau9c0fe592009-01-18 16:25:31 +0100473 /* We might have some data the consumer is waiting for */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200474 if (!(b->flags & BF_OUT_EMPTY) && (b->cons->flags & SI_FL_WAIT_DATA)) {
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100475 int last_len = b->pipe ? b->pipe->data : 0;
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100476
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100477 b->cons->chk_snd(b->cons);
478
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100479 /* check if the consumer has freed some space */
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100480 if (!(b->flags & BF_FULL) &&
481 (!last_len || !b->pipe || b->pipe->data < last_len))
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100482 si->flags &= ~SI_FL_WAIT_ROOM;
483 }
484
485 if (si->flags & SI_FL_WAIT_ROOM) {
Willy Tarreau9c0fe592009-01-18 16:25:31 +0100486 EV_FD_CLR(fd, DIR_RD);
487 b->rex = TICK_ETERNITY;
488 }
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200489 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 +0100490 b->rex = tick_add_ifset(now_ms, b->rto);
Willy Tarreau9c0fe592009-01-18 16:25:31 +0100491
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100492 /* we have to wake up if there is a special event or if we don't have
493 * any more data to forward.
494 */
Willy Tarreau1b194fe2009-03-21 21:10:04 +0100495 if ((b->flags & (BF_READ_NULL|BF_READ_ERROR|BF_SHUTR|BF_READ_DONTWAIT)) ||
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100496 !b->to_forward ||
497 si->state != SI_ST_EST ||
498 b->cons->state != SI_ST_EST ||
499 (si->flags & SI_FL_ERR))
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100500 task_wakeup(si->owner, TASK_WOKEN_IO);
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100501
Willy Tarreau1b194fe2009-03-21 21:10:04 +0100502 b->flags &= ~BF_READ_DONTWAIT;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100503 fdtab[fd].ev &= ~FD_POLL_IN;
Willy Tarreau83749182007-04-15 20:56:27 +0200504 return retval;
Willy Tarreau6996e152007-04-30 14:37:43 +0200505
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100506 out_shutdown_r:
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200507 /* we received a shutdown */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100508 fdtab[fd].ev &= ~FD_POLL_HUP;
509 b->flags |= BF_READ_NULL;
Willy Tarreau520d95e2009-09-19 21:04:57 +0200510 if (b->flags & BF_AUTO_CLOSE)
Willy Tarreau418fd472009-09-06 21:37:23 +0200511 buffer_shutw_now(b);
Willy Tarreau99126c32008-11-27 10:30:51 +0100512 stream_sock_shutr(si);
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200513 goto out_wakeup;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100514
Willy Tarreau6996e152007-04-30 14:37:43 +0200515 out_error:
Willy Tarreaucff64112008-11-03 06:26:53 +0100516 /* Read error on the file descriptor. We mark the FD as STERROR so
517 * that we don't use it anymore. The error is reported to the stream
518 * interface which will take proper action. We must not perturbate the
519 * buffer because the stream interface wants to ensure transparent
520 * connection retries.
Willy Tarreau6996e152007-04-30 14:37:43 +0200521 */
Willy Tarreaucff64112008-11-03 06:26:53 +0100522
Willy Tarreau6996e152007-04-30 14:37:43 +0200523 fdtab[fd].state = FD_STERROR;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100524 fdtab[fd].ev &= ~FD_POLL_STICKY;
Willy Tarreau1714e0f2009-03-28 20:54:53 +0100525 EV_FD_REM(fd);
Willy Tarreaucff64112008-11-03 06:26:53 +0100526 si->flags |= SI_FL_ERR;
Willy Tarreau9c0fe592009-01-18 16:25:31 +0100527 retval = 1;
528 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200529}
530
531
532/*
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100533 * This function is called to send buffer data to a stream socket.
534 * It returns -1 in case of unrecoverable error, 0 if the caller needs to poll
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100535 * before calling it again, otherwise 1. If a pipe was associated with the
536 * buffer and it empties it, it releases it as well.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200537 */
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100538static int stream_sock_write_loop(struct stream_interface *si, struct buffer *b)
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100539{
Willy Tarreau83749182007-04-15 20:56:27 +0200540 int write_poll = MAX_WRITE_POLL_LOOPS;
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100541 int retval = 1;
542 int ret, max;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200543
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100544#if defined(CONFIG_HAP_LINUX_SPLICE)
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100545 while (b->pipe) {
546 ret = splice(b->pipe->cons, NULL, si->fd, NULL, b->pipe->data,
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100547 SPLICE_F_MOVE|SPLICE_F_NONBLOCK);
548 if (ret <= 0) {
549 if (ret == 0 || errno == EAGAIN) {
550 retval = 0;
551 return retval;
552 }
553 /* here we have another error */
554 retval = -1;
555 return retval;
556 }
557
558 b->flags |= BF_WRITE_PARTIAL;
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100559 b->pipe->data -= ret;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100560
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100561 if (!b->pipe->data) {
562 put_pipe(b->pipe);
563 b->pipe = NULL;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100564 break;
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100565 }
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100566
567 if (--write_poll <= 0)
568 return retval;
569 }
570
571 /* At this point, the pipe is empty, but we may still have data pending
572 * in the normal buffer.
573 */
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100574#endif
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200575 if (!b->send_max) {
576 b->flags |= BF_OUT_EMPTY;
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100577 return retval;
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200578 }
Willy Tarreau83749182007-04-15 20:56:27 +0200579
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100580 /* when we're in this loop, we already know that there is no spliced
581 * data left, and that there are sendable buffered data.
582 */
Willy Tarreau6996e152007-04-30 14:37:43 +0200583 while (1) {
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100584 if (b->r > b->w)
Willy Tarreau83749182007-04-15 20:56:27 +0200585 max = b->r - b->w;
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100586 else
Willy Tarreaua07a34e2009-08-16 23:27:46 +0200587 max = b->data + b->size - b->w;
Willy Tarreau83749182007-04-15 20:56:27 +0200588
Willy Tarreauf890dc92008-12-13 21:12:26 +0100589 /* limit the amount of outgoing data if required */
590 if (max > b->send_max)
591 max = b->send_max;
592
Willy Tarreau6db06d32009-08-19 11:14:11 +0200593 /* check if we want to inform the kernel that we're interested in
594 * sending more data after this call. We want this if :
595 * - we're about to close after this last send and want to merge
596 * the ongoing FIN with the last segment.
597 * - we know we can't send everything at once and must get back
598 * here because of unaligned data
599 * The test is arranged so that the most common case does only 2
600 * tests.
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200601 */
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200602
Willy Tarreaud6d06902009-08-19 11:22:33 +0200603 if (MSG_NOSIGNAL) {
Willy Tarreau6db06d32009-08-19 11:14:11 +0200604 unsigned int send_flag = MSG_DONTWAIT | MSG_NOSIGNAL;
605
606 if (MSG_MORE &&
Willy Tarreau520d95e2009-09-19 21:04:57 +0200607 (((b->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK)) == BF_SHUTW_NOW &&
Willy Tarreau6db06d32009-08-19 11:14:11 +0200608 (max == b->l)) ||
609 (max != b->l && max != b->send_max))
610 && (fdtab[si->fd].flags & FD_FL_TCP)) {
611 send_flag |= MSG_MORE;
612 }
613
614 ret = send(si->fd, b->w, max, send_flag);
Willy Tarreaud6d06902009-08-19 11:22:33 +0200615 } else {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200616 int skerr;
617 socklen_t lskerr = sizeof(skerr);
618
Willy Tarreau87bed622009-03-08 22:25:28 +0100619 ret = getsockopt(si->fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr);
Willy Tarreauc6423482006-10-15 14:59:03 +0200620 if (ret == -1 || skerr)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200621 ret = -1;
622 else
Willy Tarreau87bed622009-03-08 22:25:28 +0100623 ret = send(si->fd, b->w, max, MSG_DONTWAIT);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200624 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200625
626 if (ret > 0) {
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100627 if (fdtab[si->fd].state == FD_STCONN)
628 fdtab[si->fd].state = FD_STREADY;
Willy Tarreaub38903c2008-11-23 21:33:29 +0100629
Willy Tarreau3da77c52008-08-29 09:58:42 +0200630 b->flags |= BF_WRITE_PARTIAL;
Willy Tarreaue393fe22008-08-16 22:18:07 +0200631
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100632 b->w += ret;
Willy Tarreaua07a34e2009-08-16 23:27:46 +0200633 if (b->w == b->data + b->size)
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100634 b->w = b->data; /* wrap around the buffer */
635
636 b->l -= ret;
637 if (likely(b->l < b->max_len))
Willy Tarreaue393fe22008-08-16 22:18:07 +0200638 b->flags &= ~BF_FULL;
Willy Tarreau74ab2ac2008-11-23 17:23:07 +0100639
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200640 if (likely(!b->l))
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100641 /* optimize data alignment in the buffer */
642 b->r = b->w = b->lr = b->data;
Willy Tarreau83749182007-04-15 20:56:27 +0200643
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100644 b->send_max -= ret;
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200645 if (!b->send_max) {
646 if (likely(!b->pipe))
647 b->flags |= BF_OUT_EMPTY;
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100648 break;
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200649 }
Willy Tarreau83749182007-04-15 20:56:27 +0200650
Willy Tarreauab3e1d32007-06-03 14:10:36 +0200651 /* if the system buffer is full, don't insist */
652 if (ret < max)
653 break;
654
Willy Tarreau6996e152007-04-30 14:37:43 +0200655 if (--write_poll <= 0)
656 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200657 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200658 else if (ret == 0 || errno == EAGAIN) {
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100659 /* nothing written, we need to poll for write first */
Willy Tarreau83749182007-04-15 20:56:27 +0200660 retval = 0;
661 break;
662 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200663 else {
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100664 /* bad, we got an error */
665 retval = -1;
666 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200667 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200668 } /* while (1) */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200669
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100670 return retval;
671}
Willy Tarreau6996e152007-04-30 14:37:43 +0200672
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100673
674/*
675 * This function is called on a write event from a stream socket.
676 * It returns 0 if the caller needs to poll before calling it again, otherwise
677 * non-zero.
678 */
679int stream_sock_write(int fd)
680{
681 struct stream_interface *si = fdtab[fd].owner;
682 struct buffer *b = si->ob;
683 int retval = 1;
684
685#ifdef DEBUG_FULL
686 fprintf(stderr,"stream_sock_write : fd=%d, owner=%p\n", fd, fdtab[fd].owner);
687#endif
688
689 retval = 1;
Willy Tarreau71543652009-07-14 19:55:05 +0200690 if (fdtab[fd].state == FD_STERROR)
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100691 goto out_error;
692
Willy Tarreaud06e7112009-03-29 10:18:41 +0200693 /* we might have been called just after an asynchronous shutw */
694 if (b->flags & BF_SHUTW)
695 goto out_wakeup;
696
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200697 if (likely(!(b->flags & BF_OUT_EMPTY))) {
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100698 /* OK there are data waiting to be sent */
699 retval = stream_sock_write_loop(si, b);
700 if (retval < 0)
701 goto out_error;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200702 }
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100703 else {
704 /* may be we have received a connection acknowledgement in TCP mode without data */
705 if (likely(fdtab[fd].state == FD_STCONN)) {
706 /* We have no data to send to check the connection, and
707 * getsockopt() will not inform us whether the connection
708 * is still pending. So we'll reuse connect() to check the
709 * state of the socket. This has the advantage of givig us
710 * the following info :
711 * - error
712 * - connecting (EALREADY, EINPROGRESS)
713 * - connected (EISCONN, 0)
714 */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200715 if ((connect(fd, fdinfo[fd].peeraddr, fdinfo[fd].peerlen) == 0))
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100716 errno = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200717
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100718 if (errno == EALREADY || errno == EINPROGRESS) {
719 retval = 0;
720 goto out_may_wakeup;
721 }
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100722
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100723 if (errno && errno != EISCONN)
724 goto out_error;
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200725
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100726 /* OK we just need to indicate that we got a connection
727 * and that we wrote nothing.
728 */
729 b->flags |= BF_WRITE_NULL;
730 fdtab[fd].state = FD_STREADY;
731 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200732
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100733 /* Funny, we were called to write something but there wasn't
734 * anything. We can get there, for example if we were woken up
735 * on a write event to finish the splice, but the send_max is 0
736 * so we cannot write anything from the buffer. Let's disable
737 * the write event and pretend we never came there.
738 */
739 }
740
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200741 if (b->flags & BF_OUT_EMPTY) {
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100742 /* the connection is established but we can't write. Either the
743 * buffer is empty, or we just refrain from sending because the
744 * send_max limit was reached. Maybe we just wrote the last
745 * chunk and need to close.
746 */
Willy Tarreau520d95e2009-09-19 21:04:57 +0200747 if (((b->flags & (BF_SHUTW|BF_HIJACK|BF_SHUTW_NOW)) == BF_SHUTW_NOW) &&
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100748 (si->state == SI_ST_EST)) {
749 stream_sock_shutw(si);
750 goto out_wakeup;
751 }
752
Willy Tarreau59454bf2009-09-20 11:13:40 +0200753 if ((b->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_FULL|BF_HIJACK)) == 0)
Willy Tarreauac128fe2009-01-09 13:05:19 +0100754 si->flags |= SI_FL_WAIT_DATA;
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100755
Willy Tarreauac128fe2009-01-09 13:05:19 +0100756 EV_FD_CLR(fd, DIR_WR);
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100757 b->wex = TICK_ETERNITY;
Willy Tarreauac128fe2009-01-09 13:05:19 +0100758 }
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100759
760 out_may_wakeup:
761 if (b->flags & BF_WRITE_ACTIVITY) {
762 /* update timeout if we have written something */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200763 if ((b->flags & (BF_OUT_EMPTY|BF_SHUTW|BF_WRITE_PARTIAL)) == BF_WRITE_PARTIAL)
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100764 b->wex = tick_add_ifset(now_ms, b->wto);
765
766 out_wakeup:
Willy Tarreauf27b5ea2009-10-03 22:01:18 +0200767 if (tick_isset(si->ib->rex) && !(si->flags & SI_FL_INDEP_STR)) {
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100768 /* Note: to prevent the client from expiring read timeouts
Willy Tarreauf27b5ea2009-10-03 22:01:18 +0200769 * during writes, we refresh it. We only do this if the
770 * interface is not configured for "independant streams",
771 * because for some applications it's better not to do this,
772 * for instance when continuously exchanging small amounts
773 * of data which can full the socket buffers long before a
774 * write timeout is detected.
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100775 */
776 si->ib->rex = tick_add_ifset(now_ms, si->ib->rto);
777 }
778
779 /* the producer might be waiting for more room to store data */
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200780 if (likely((b->flags & (BF_SHUTW|BF_WRITE_PARTIAL|BF_FULL|BF_DONT_READ)) == BF_WRITE_PARTIAL &&
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100781 (b->prod->flags & SI_FL_WAIT_ROOM)))
782 b->prod->chk_rcv(b->prod);
783
784 /* we have to wake up if there is a special event or if we don't have
785 * any more data to forward and it's not planned to send any more.
786 */
787 if (likely((b->flags & (BF_WRITE_NULL|BF_WRITE_ERROR|BF_SHUTW)) ||
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200788 ((b->flags & BF_OUT_EMPTY) && !b->to_forward) ||
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100789 si->state != SI_ST_EST ||
790 b->prod->state != SI_ST_EST))
791 task_wakeup(si->owner, TASK_WOKEN_IO);
792 }
793
794 fdtab[fd].ev &= ~FD_POLL_OUT;
795 return retval;
Willy Tarreauac128fe2009-01-09 13:05:19 +0100796
Willy Tarreau6996e152007-04-30 14:37:43 +0200797 out_error:
Willy Tarreaucff64112008-11-03 06:26:53 +0100798 /* Write error on the file descriptor. We mark the FD as STERROR so
799 * that we don't use it anymore. The error is reported to the stream
800 * interface which will take proper action. We must not perturbate the
801 * buffer because the stream interface wants to ensure transparent
802 * connection retries.
Willy Tarreau6996e152007-04-30 14:37:43 +0200803 */
Willy Tarreaucff64112008-11-03 06:26:53 +0100804
Willy Tarreau6996e152007-04-30 14:37:43 +0200805 fdtab[fd].state = FD_STERROR;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100806 fdtab[fd].ev &= ~FD_POLL_STICKY;
Willy Tarreau1714e0f2009-03-28 20:54:53 +0100807 EV_FD_REM(fd);
Willy Tarreaucff64112008-11-03 06:26:53 +0100808 si->flags |= SI_FL_ERR;
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200809 task_wakeup(si->owner, TASK_WOKEN_IO);
810 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200811}
812
Willy Tarreau48adac52008-08-30 04:58:38 +0200813/*
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200814 * This function performs a shutdown-write on a stream interface in a connected or
815 * init state (it does nothing for other states). It either shuts the write side
Willy Tarreau99126c32008-11-27 10:30:51 +0100816 * or closes the file descriptor and marks itself as closed. The buffer flags are
817 * updated to reflect the new state.
Willy Tarreau48adac52008-08-30 04:58:38 +0200818 */
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100819void stream_sock_shutw(struct stream_interface *si)
Willy Tarreau48adac52008-08-30 04:58:38 +0200820{
Willy Tarreau418fd472009-09-06 21:37:23 +0200821 si->ob->flags &= ~BF_SHUTW_NOW;
Willy Tarreau99126c32008-11-27 10:30:51 +0100822 if (si->ob->flags & BF_SHUTW)
823 return;
824 si->ob->flags |= BF_SHUTW;
825 si->ob->wex = TICK_ETERNITY;
Willy Tarreaub0ef7352008-12-14 13:26:20 +0100826 si->flags &= ~SI_FL_WAIT_DATA;
Willy Tarreau99126c32008-11-27 10:30:51 +0100827
Willy Tarreaub38903c2008-11-23 21:33:29 +0100828 switch (si->state) {
Willy Tarreaub38903c2008-11-23 21:33:29 +0100829 case SI_ST_EST:
Willy Tarreau720058c2009-07-14 19:21:50 +0200830 /* we have to shut before closing, otherwise some short messages
831 * may never leave the system, especially when there are remaining
832 * unread data in the socket input buffer, or when nolinger is set.
833 */
834 EV_FD_CLR(si->fd, DIR_WR);
835 shutdown(si->fd, SHUT_WR);
836
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200837 if (!(si->ib->flags & (BF_SHUTR|BF_DONT_READ)))
Willy Tarreaub38903c2008-11-23 21:33:29 +0100838 return;
Willy Tarreau5d707e12009-06-28 11:09:07 +0200839
Willy Tarreaub38903c2008-11-23 21:33:29 +0100840 /* fall through */
841 case SI_ST_CON:
Willy Tarreau8bfa4262008-11-27 09:25:45 +0100842 /* we may have to close a pending connection, and mark the
843 * response buffer as shutr
844 */
Willy Tarreau48adac52008-08-30 04:58:38 +0200845 fd_delete(si->fd);
Willy Tarreaufe3718a2008-11-30 18:14:12 +0100846 /* fall through */
847 case SI_ST_CER:
Willy Tarreau7f006512008-12-07 14:04:04 +0100848 si->state = SI_ST_DIS;
849 default:
Willy Tarreaud06e7112009-03-29 10:18:41 +0200850 si->flags &= ~SI_FL_WAIT_ROOM;
Willy Tarreau99126c32008-11-27 10:30:51 +0100851 si->ib->flags |= BF_SHUTR;
Willy Tarreaufe3718a2008-11-30 18:14:12 +0100852 si->ib->rex = TICK_ETERNITY;
Willy Tarreau127334e2009-03-28 10:47:26 +0100853 si->exp = TICK_ETERNITY;
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100854 return;
Willy Tarreau48adac52008-08-30 04:58:38 +0200855 }
Willy Tarreau48adac52008-08-30 04:58:38 +0200856}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200857
Willy Tarreau2d212792008-08-27 21:41:35 +0200858/*
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200859 * This function performs a shutdown-read on a stream interface in a connected or
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100860 * init state (it does nothing for other states). It either shuts the read side
Willy Tarreau99126c32008-11-27 10:30:51 +0100861 * or closes the file descriptor and marks itself as closed. The buffer flags are
862 * updated to reflect the new state.
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200863 */
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100864void stream_sock_shutr(struct stream_interface *si)
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200865{
Willy Tarreau418fd472009-09-06 21:37:23 +0200866 si->ib->flags &= ~BF_SHUTR_NOW;
Willy Tarreau99126c32008-11-27 10:30:51 +0100867 if (si->ib->flags & BF_SHUTR)
868 return;
869 si->ib->flags |= BF_SHUTR;
870 si->ib->rex = TICK_ETERNITY;
Willy Tarreaub0ef7352008-12-14 13:26:20 +0100871 si->flags &= ~SI_FL_WAIT_ROOM;
Willy Tarreau99126c32008-11-27 10:30:51 +0100872
Willy Tarreau8bfa4262008-11-27 09:25:45 +0100873 if (si->state != SI_ST_EST && si->state != SI_ST_CON)
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100874 return;
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200875
Willy Tarreaucff64112008-11-03 06:26:53 +0100876 if (si->ob->flags & BF_SHUTW) {
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200877 fd_delete(si->fd);
Willy Tarreau74ab2ac2008-11-23 17:23:07 +0100878 si->state = SI_ST_DIS;
Willy Tarreau127334e2009-03-28 10:47:26 +0100879 si->exp = TICK_ETERNITY;
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100880 return;
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200881 }
882 EV_FD_CLR(si->fd, DIR_RD);
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100883 return;
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200884}
885
886/*
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200887 * Updates a connected stream_sock file descriptor status and timeouts
888 * according to the buffers' flags. It should only be called once after the
889 * buffer flags have settled down, and before they are cleared. It doesn't
890 * harm to call it as often as desired (it just slightly hurts performance).
891 */
Willy Tarreaub0253252008-11-30 21:37:12 +0100892void stream_sock_data_finish(struct stream_interface *si)
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200893{
Willy Tarreaub0253252008-11-30 21:37:12 +0100894 struct buffer *ib = si->ib;
895 struct buffer *ob = si->ob;
896 int fd = si->fd;
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200897
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200898 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 +0200899 now_ms, __FUNCTION__,
900 fd, fdtab[fd].owner,
901 ib, ob,
902 ib->rex, ob->wex,
903 ib->flags, ob->flags,
Willy Tarreaub0253252008-11-30 21:37:12 +0100904 ib->l, ob->l, si->state);
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200905
906 /* Check if we need to close the read side */
907 if (!(ib->flags & BF_SHUTR)) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200908 /* Read not closed, update FD status and timeout for reads */
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200909 if (ib->flags & (BF_FULL|BF_HIJACK|BF_DONT_READ)) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200910 /* stop reading */
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200911 if ((ib->flags & (BF_FULL|BF_HIJACK|BF_DONT_READ)) == BF_FULL)
Willy Tarreaub0ef7352008-12-14 13:26:20 +0100912 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau2d212792008-08-27 21:41:35 +0200913 EV_FD_COND_C(fd, DIR_RD);
914 ib->rex = TICK_ETERNITY;
915 }
916 else {
917 /* (re)start reading and update timeout. Note: we don't recompute the timeout
918 * everytime we get here, otherwise it would risk never to expire. We only
Willy Tarreaufe8903c2009-10-04 10:56:08 +0200919 * update it if is was not yet set. The stream socket handler will already
920 * have updated it if there has been a completed I/O.
Willy Tarreau2d212792008-08-27 21:41:35 +0200921 */
Willy Tarreaub0ef7352008-12-14 13:26:20 +0100922 si->flags &= ~SI_FL_WAIT_ROOM;
Willy Tarreau2d212792008-08-27 21:41:35 +0200923 EV_FD_COND_S(fd, DIR_RD);
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200924 if (!(ib->flags & (BF_READ_NOEXP|BF_DONT_READ)) && !tick_isset(ib->rex))
Willy Tarreau2d212792008-08-27 21:41:35 +0200925 ib->rex = tick_add_ifset(now_ms, ib->rto);
926 }
927 }
928
929 /* Check if we need to close the write side */
930 if (!(ob->flags & BF_SHUTW)) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200931 /* Write not closed, update FD status and timeout for writes */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200932 if (ob->flags & BF_OUT_EMPTY) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200933 /* stop writing */
Willy Tarreau59454bf2009-09-20 11:13:40 +0200934 if ((ob->flags & (BF_FULL|BF_HIJACK|BF_SHUTW_NOW)) == 0)
Willy Tarreaub0ef7352008-12-14 13:26:20 +0100935 si->flags |= SI_FL_WAIT_DATA;
Willy Tarreau2d212792008-08-27 21:41:35 +0200936 EV_FD_COND_C(fd, DIR_WR);
937 ob->wex = TICK_ETERNITY;
938 }
939 else {
940 /* (re)start writing and update timeout. Note: we don't recompute the timeout
941 * everytime we get here, otherwise it would risk never to expire. We only
Willy Tarreaufe8903c2009-10-04 10:56:08 +0200942 * update it if is was not yet set. The stream socket handler will already
943 * have updated it if there has been a completed I/O.
Willy Tarreau2d212792008-08-27 21:41:35 +0200944 */
Willy Tarreaub0ef7352008-12-14 13:26:20 +0100945 si->flags &= ~SI_FL_WAIT_DATA;
Willy Tarreau2d212792008-08-27 21:41:35 +0200946 EV_FD_COND_S(fd, DIR_WR);
Willy Tarreaufe8903c2009-10-04 10:56:08 +0200947 if (!tick_isset(ob->wex)) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200948 ob->wex = tick_add_ifset(now_ms, ob->wto);
Willy Tarreauf27b5ea2009-10-03 22:01:18 +0200949 if (tick_isset(ib->rex) && !(si->flags & SI_FL_INDEP_STR)) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200950 /* Note: depending on the protocol, we don't know if we're waiting
951 * for incoming data or not. So in order to prevent the socket from
952 * expiring read timeouts during writes, we refresh the read timeout,
Willy Tarreauf27b5ea2009-10-03 22:01:18 +0200953 * except if it was already infinite or if we have explicitly setup
954 * independant streams.
Willy Tarreau2d212792008-08-27 21:41:35 +0200955 */
Willy Tarreaud06e7112009-03-29 10:18:41 +0200956 ib->rex = tick_add_ifset(now_ms, ib->rto);
Willy Tarreau2d212792008-08-27 21:41:35 +0200957 }
958 }
959 }
960 }
Willy Tarreau2d212792008-08-27 21:41:35 +0200961}
962
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100963/* This function is used for inter-stream-interface calls. It is called by the
964 * consumer to inform the producer side that it may be interested in checking
965 * for free space in the buffer. Note that it intentionally does not update
966 * timeouts, so that we can still check them later at wake-up.
967 */
968void stream_sock_chk_rcv(struct stream_interface *si)
969{
970 struct buffer *ib = si->ib;
971
972 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",
973 now_ms, __FUNCTION__,
Vincenzo Farruggia9b97cff2009-01-30 16:49:10 +0000974 si->fd, fdtab[si->fd].owner,
975 ib, si->ob,
976 ib->rex, si->ob->wex,
977 ib->flags, si->ob->flags,
978 ib->l, si->ob->l, si->state);
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100979
980 if (unlikely(si->state != SI_ST_EST || (ib->flags & BF_SHUTR)))
981 return;
982
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200983 if (ib->flags & (BF_FULL|BF_HIJACK|BF_DONT_READ)) {
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100984 /* stop reading */
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200985 if ((ib->flags & (BF_FULL|BF_HIJACK|BF_DONT_READ)) == BF_FULL)
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100986 si->flags |= SI_FL_WAIT_ROOM;
987 EV_FD_COND_C(si->fd, DIR_RD);
988 }
989 else {
990 /* (re)start reading */
991 si->flags &= ~SI_FL_WAIT_ROOM;
992 EV_FD_COND_S(si->fd, DIR_RD);
993 }
994}
995
996
997/* This function is used for inter-stream-interface calls. It is called by the
998 * producer to inform the consumer side that it may be interested in checking
999 * for data in the buffer. Note that it intentionally does not update timeouts,
1000 * so that we can still check them later at wake-up.
1001 */
1002void stream_sock_chk_snd(struct stream_interface *si)
1003{
1004 struct buffer *ob = si->ob;
Willy Tarreaua456f2a2009-01-18 17:38:44 +01001005 int retval;
Willy Tarreau3ffeba12008-12-14 14:42:35 +01001006
1007 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",
1008 now_ms, __FUNCTION__,
Vincenzo Farruggia9b97cff2009-01-30 16:49:10 +00001009 si->fd, fdtab[si->fd].owner,
1010 si->ib, ob,
1011 si->ib->rex, ob->wex,
1012 si->ib->flags, ob->flags,
1013 si->ib->l, ob->l, si->state);
Willy Tarreau3ffeba12008-12-14 14:42:35 +01001014
1015 if (unlikely(si->state != SI_ST_EST || (ob->flags & BF_SHUTW)))
1016 return;
1017
Willy Tarreaua456f2a2009-01-18 17:38:44 +01001018 if (!(si->flags & SI_FL_WAIT_DATA) || /* not waiting for data */
1019 (fdtab[si->fd].ev & FD_POLL_OUT) || /* we'll be called anyway */
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001020 (ob->flags & BF_OUT_EMPTY)) /* called with nothing to send ! */
Willy Tarreaua456f2a2009-01-18 17:38:44 +01001021 return;
1022
1023 retval = stream_sock_write_loop(si, ob);
Willy Tarreauc54aef32009-07-27 20:08:06 +02001024 /* here, we have :
1025 * retval < 0 if an error was encountered during write.
1026 * retval = 0 if we can't write anymore without polling
1027 * retval = 1 if we're invited to come back when desired
1028 */
Willy Tarreaua456f2a2009-01-18 17:38:44 +01001029 if (retval < 0) {
1030 /* Write error on the file descriptor. We mark the FD as STERROR so
1031 * that we don't use it anymore and we notify the task.
1032 */
1033 fdtab[si->fd].state = FD_STERROR;
1034 fdtab[si->fd].ev &= ~FD_POLL_STICKY;
Willy Tarreau1714e0f2009-03-28 20:54:53 +01001035 EV_FD_REM(si->fd);
Willy Tarreaua456f2a2009-01-18 17:38:44 +01001036 si->flags |= SI_FL_ERR;
1037 goto out_wakeup;
1038 }
1039
Willy Tarreauc54aef32009-07-27 20:08:06 +02001040 /* OK, so now we know that retval >= 0 means that some data might have
1041 * been sent, and that we may have to poll first. We have to do that
1042 * too if the buffer is not empty.
1043 */
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001044 if (ob->flags & BF_OUT_EMPTY) {
Willy Tarreaua456f2a2009-01-18 17:38:44 +01001045 /* the connection is established but we can't write. Either the
1046 * buffer is empty, or we just refrain from sending because the
1047 * send_max limit was reached. Maybe we just wrote the last
1048 * chunk and need to close.
1049 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02001050 if (((ob->flags & (BF_SHUTW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTW_NOW)) ==
1051 (BF_AUTO_CLOSE|BF_SHUTW_NOW)) &&
Willy Tarreaua456f2a2009-01-18 17:38:44 +01001052 (si->state == SI_ST_EST)) {
1053 stream_sock_shutw(si);
1054 goto out_wakeup;
1055 }
Willy Tarreaud06e7112009-03-29 10:18:41 +02001056
Willy Tarreau59454bf2009-09-20 11:13:40 +02001057 if ((ob->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_FULL|BF_HIJACK)) == 0)
Willy Tarreau3ffeba12008-12-14 14:42:35 +01001058 si->flags |= SI_FL_WAIT_DATA;
Willy Tarreaua456f2a2009-01-18 17:38:44 +01001059 ob->wex = TICK_ETERNITY;
Willy Tarreau3ffeba12008-12-14 14:42:35 +01001060 }
1061 else {
Willy Tarreauc54aef32009-07-27 20:08:06 +02001062 /* Otherwise there are remaining data to be sent in the buffer,
1063 * which means we have to poll before doing so.
1064 */
Willy Tarreau3ffeba12008-12-14 14:42:35 +01001065 EV_FD_COND_S(si->fd, DIR_WR);
Willy Tarreauc54aef32009-07-27 20:08:06 +02001066 si->flags &= ~SI_FL_WAIT_DATA;
1067 if (!tick_isset(ob->wex))
1068 ob->wex = tick_add_ifset(now_ms, ob->wto);
Willy Tarreau3ffeba12008-12-14 14:42:35 +01001069 }
Willy Tarreaua456f2a2009-01-18 17:38:44 +01001070
Willy Tarreauc9619462009-03-09 22:40:57 +01001071 if (likely(ob->flags & BF_WRITE_ACTIVITY)) {
1072 /* update timeout if we have written something */
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001073 if ((ob->flags & (BF_OUT_EMPTY|BF_SHUTW|BF_WRITE_PARTIAL)) == BF_WRITE_PARTIAL)
Willy Tarreauc9619462009-03-09 22:40:57 +01001074 ob->wex = tick_add_ifset(now_ms, ob->wto);
1075
Willy Tarreauf27b5ea2009-10-03 22:01:18 +02001076 if (tick_isset(si->ib->rex) && !(si->flags & SI_FL_INDEP_STR)) {
Willy Tarreauc9619462009-03-09 22:40:57 +01001077 /* Note: to prevent the client from expiring read timeouts
Willy Tarreauf27b5ea2009-10-03 22:01:18 +02001078 * during writes, we refresh it. We only do this if the
1079 * interface is not configured for "independant streams",
1080 * because for some applications it's better not to do this,
1081 * for instance when continuously exchanging small amounts
1082 * of data which can full the socket buffers long before a
1083 * write timeout is detected.
Willy Tarreauc9619462009-03-09 22:40:57 +01001084 */
1085 si->ib->rex = tick_add_ifset(now_ms, si->ib->rto);
1086 }
1087 }
1088
Willy Tarreaua456f2a2009-01-18 17:38:44 +01001089 /* in case of special condition (error, shutdown, end of write...), we
1090 * have to notify the task.
1091 */
1092 if (likely((ob->flags & (BF_WRITE_NULL|BF_WRITE_ERROR|BF_SHUTW)) ||
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001093 ((ob->flags & BF_OUT_EMPTY) && !ob->to_forward) ||
Willy Tarreaua456f2a2009-01-18 17:38:44 +01001094 si->state != SI_ST_EST)) {
1095 out_wakeup:
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001096 if (!(si->flags & SI_FL_DONT_WAKE) && si->owner)
1097 task_wakeup(si->owner, TASK_WOKEN_IO);
Willy Tarreaua456f2a2009-01-18 17:38:44 +01001098 }
Willy Tarreau3ffeba12008-12-14 14:42:35 +01001099}
1100
Willy Tarreaubaaee002006-06-26 02:48:02 +02001101
1102/*
1103 * Local variables:
1104 * c-indent-level: 8
1105 * c-basic-offset: 8
1106 * End:
1107 */