blob: 30d6b6227523cd36994fb8a42d491350f814035d [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
Willy Tarreau2dd0d472006-06-29 17:53:05 +020023#include <common/compat.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020024#include <common/config.h>
Willy Tarreaud6f087e2008-01-18 17:20:13 +010025#include <common/debug.h>
Willy Tarreau83749182007-04-15 20:56:27 +020026#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020027#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020028#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020029
Willy Tarreau2d212792008-08-27 21:41:35 +020030#include <proto/buffers.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020031#include <proto/client.h>
32#include <proto/fd.h>
Willy Tarreau3eba98a2009-01-25 13:56:13 +010033#include <proto/pipe.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020034#include <proto/stream_sock.h>
35#include <proto/task.h>
36
Willy Tarreau5bd8c372009-01-19 00:32:22 +010037#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020038
Willy Tarreau6b4aad42009-01-18 21:59:13 +010039/* On recent Linux kernels, the splice() syscall may be used for faster data copy.
40 * But it's not always defined on some OS versions, and it even happens that some
41 * definitions are wrong with some glibc due to an offset bug in syscall().
42 */
43
44#if defined(CONFIG_HAP_LINUX_SPLICE)
45#include <unistd.h>
46#include <sys/syscall.h>
47
48#ifndef SPLICE_F_MOVE
49#define SPLICE_F_MOVE 0x1
50#endif
51
52#ifndef SPLICE_F_NONBLOCK
53#define SPLICE_F_NONBLOCK 0x2
54#endif
55
56#ifndef SPLICE_F_MORE
57#define SPLICE_F_MORE 0x4
58#endif
59
60#ifndef __NR_splice
61#if defined(__powerpc__) || defined(__powerpc64__)
62#define __NR_splice 283
63#elif defined(__sparc__) || defined(__sparc64__)
64#define __NR_splice 232
65#elif defined(__x86_64__)
66#define __NR_splice 275
67#elif defined(__alpha__)
68#define __NR_splice 468
69#elif defined (__i386__)
70#define __NR_splice 313
71#else
72#warning unsupported architecture, guessing __NR_splice=313 like x86...
73#define __NR_splice 313
74#endif /* $arch */
75
76_syscall6(int, splice, int, fdin, loff_t *, off_in, int, fdout, loff_t *, off_out, size_t, len, unsigned long, flags)
77
78#endif /* __NR_splice */
Willy Tarreau5bd8c372009-01-19 00:32:22 +010079
80/* A pipe contains 16 segments max, and it's common to see segments of 1448 bytes
81 * because of timestamps. Use this as a hint for not looping on splice().
82 */
83#define SPLICE_FULL_HINT 16*1448
84
85/* Returns :
86 * -1 if splice is not possible or not possible anymore and we must switch to
87 * user-land copy (eg: to_forward reached)
88 * 0 when we know that polling is required to get more data (EAGAIN)
89 * 1 for all other cases (we can safely try again, or if an activity has been
90 * detected (DATA/NULL/ERR))
91 * Sets :
92 * BF_READ_NULL
93 * BF_READ_PARTIAL
94 * BF_WRITE_PARTIAL (during copy)
95 * BF_EMPTY (during copy)
96 * SI_FL_ERR
97 * SI_FL_WAIT_ROOM
98 * (SI_FL_WAIT_RECV)
Willy Tarreau3eba98a2009-01-25 13:56:13 +010099 *
100 * This function automatically allocates a pipe from the pipe pool. It also
101 * carefully ensures to clear b->pipe whenever it leaves the pipe empty.
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100102 */
103static int stream_sock_splice_in(struct buffer *b, struct stream_interface *si)
104{
105 int fd = si->fd;
106 int ret, max, total = 0;
107 int retval = 1;
108
109 if (!b->to_forward)
110 return -1;
111
112 if (!(b->flags & BF_KERN_SPLICING))
113 return -1;
114
115 if (b->l) {
116 /* We're embarrassed, there are already data pending in
117 * the buffer and we don't want to have them at two
118 * locations at a time. Let's indicate we need some
119 * place and ask the consumer to hurry.
120 */
121 si->flags |= SI_FL_WAIT_ROOM;
122 EV_FD_CLR(fd, DIR_RD);
123 b->rex = TICK_ETERNITY;
124 b->cons->chk_snd(b->cons);
125 return 1;
126 }
127
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100128 if (unlikely(b->pipe == NULL)) {
129 if (pipes_used >= global.maxpipes || !(b->pipe = get_pipe())) {
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100130 b->flags &= ~BF_KERN_SPLICING;
131 return -1;
132 }
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100133 }
134
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100135 /* At this point, b->pipe is valid */
136
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100137 while (1) {
138 max = b->to_forward;
139 if (max <= 0) {
140 /* It looks like the buffer + the pipe already contain
141 * the maximum amount of data to be transferred. Try to
142 * send those data immediately on the other side if it
143 * is currently waiting.
144 */
145 retval = -1; /* end of forwarding */
146 break;
147 }
148
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100149 ret = splice(fd, NULL, b->pipe->prod, NULL, max,
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100150 SPLICE_F_MOVE|SPLICE_F_NONBLOCK);
151
152 if (ret <= 0) {
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100153 if (ret == 0) {
Willy Tarreau98b306b2009-01-25 11:11:32 +0100154 /* connection closed. This is only detected by
155 * recent kernels (>= 2.6.27.13).
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100156 */
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100157 b->flags |= BF_READ_NULL;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100158 retval = 1; /* no need for further polling */
159 break;
160 }
161
162 if (errno == EAGAIN) {
163 /* there are two reasons for EAGAIN :
164 * - nothing in the socket buffer (standard)
165 * - pipe is full
Willy Tarreau98b306b2009-01-25 11:11:32 +0100166 * - the connection is closed (kernel < 2.6.27.13)
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100167 * Since we don't know if pipe is full, we'll
168 * stop if the pipe is not empty. Anyway, we
169 * will almost always fill/empty the pipe.
170 */
171
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100172 if (b->pipe->data) {
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100173 si->flags |= SI_FL_WAIT_ROOM;
174 retval = 1;
175 break;
176 }
177
Willy Tarreau98b306b2009-01-25 11:11:32 +0100178 /* We don't know if the connection was closed.
179 * But if we're called upon POLLIN with an empty
180 * pipe and get EAGAIN, it is suspect enought to
181 * try to fall back to the normal recv scheme
182 * which will be able to deal with the situation.
183 */
184 retval = -1;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100185 break;
186 }
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100187 /* here we have another error */
188 si->flags |= SI_FL_ERR;
189 retval = 1;
190 break;
191 } /* ret <= 0 */
192
193 b->to_forward -= ret;
194 total += ret;
195 b->total += ret;
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100196 b->pipe->data += ret;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100197 b->flags |= BF_READ_PARTIAL;
198 b->flags &= ~BF_EMPTY; /* to prevent shutdowns */
199
Willy Tarreau6f4a82c2009-03-21 20:43:57 +0100200 if (b->pipe->data >= SPLICE_FULL_HINT ||
201 ret >= global.tune.recv_enough) {
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100202 /* We've read enough of it for this time. */
203 retval = 1;
204 break;
205 }
206 } /* while */
207
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100208 if (unlikely(!b->pipe->data)) {
209 put_pipe(b->pipe);
210 b->pipe = NULL;
211 }
212
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100213 return retval;
214}
215
Willy Tarreau6b4aad42009-01-18 21:59:13 +0100216#endif /* CONFIG_HAP_LINUX_SPLICE */
217
218
Willy Tarreaubaaee002006-06-26 02:48:02 +0200219/*
Willy Tarreaud7971282006-07-29 18:36:34 +0200220 * this function is called on a read event from a stream socket.
Willy Tarreau83749182007-04-15 20:56:27 +0200221 * It returns 0 if we have a high confidence that we will not be
222 * able to read more data without polling first. Returns non-zero
223 * otherwise.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200224 */
Willy Tarreaud7971282006-07-29 18:36:34 +0200225int stream_sock_read(int fd) {
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200226 struct stream_interface *si = fdtab[fd].owner;
Willy Tarreau48adac52008-08-30 04:58:38 +0200227 struct buffer *b = si->ib;
Willy Tarreau8a7af602008-05-03 23:07:14 +0200228 int ret, max, retval, cur_read;
Willy Tarreaub8949f12007-03-23 22:39:59 +0100229 int read_poll = MAX_READ_POLL_LOOPS;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200230
231#ifdef DEBUG_FULL
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100232 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 +0200233#endif
234
Willy Tarreau83749182007-04-15 20:56:27 +0200235 retval = 1;
236
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100237 /* stop immediately on errors */
238 if (fdtab[fd].state == FD_STERROR || (fdtab[fd].ev & FD_POLL_ERR))
Willy Tarreau6996e152007-04-30 14:37:43 +0200239 goto out_error;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100240
241 /* stop here if we reached the end of data */
242 if ((fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP)) == FD_POLL_HUP)
243 goto out_shutdown_r;
Willy Tarreau83749182007-04-15 20:56:27 +0200244
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100245#if defined(CONFIG_HAP_LINUX_SPLICE)
246 if (b->to_forward && b->flags & BF_KERN_SPLICING) {
Willy Tarreau98b306b2009-01-25 11:11:32 +0100247
248 /* Under Linux, if FD_POLL_HUP is set, we have reached the end.
249 * Since older splice() implementations were buggy and returned
250 * EAGAIN on end of read, let's bypass the call to splice() now.
251 */
252 if (fdtab[fd].ev & FD_POLL_HUP)
253 goto out_shutdown_r;
254
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100255 retval = stream_sock_splice_in(b, si);
256
257 if (retval >= 0) {
258 if (si->flags & SI_FL_ERR)
259 goto out_error;
260 if (b->flags & BF_READ_NULL)
261 goto out_shutdown_r;
262 goto out_wakeup;
263 }
264 /* splice not possible (anymore), let's go on on standard copy */
265 }
266#endif
Willy Tarreau8a7af602008-05-03 23:07:14 +0200267 cur_read = 0;
Willy Tarreau6996e152007-04-30 14:37:43 +0200268 while (1) {
269 /*
270 * 1. compute the maximum block size we can read at once.
271 */
Willy Tarreau03d60bb2009-01-09 11:13:00 +0100272 if (b->l == 0) {
273 /* let's realign the buffer to optimize I/O */
274 b->r = b->w = b->lr = b->data;
275 max = b->max_len;
Willy Tarreau83749182007-04-15 20:56:27 +0200276 }
277 else if (b->r > b->w) {
Willy Tarreau03d60bb2009-01-09 11:13:00 +0100278 max = b->data + b->max_len - b->r;
Willy Tarreau83749182007-04-15 20:56:27 +0200279 }
280 else {
281 max = b->w - b->r;
Willy Tarreau03d60bb2009-01-09 11:13:00 +0100282 if (max > b->max_len)
283 max = b->max_len;
Willy Tarreau83749182007-04-15 20:56:27 +0200284 }
Willy Tarreau74ab2ac2008-11-23 17:23:07 +0100285
Willy Tarreau9c0fe592009-01-18 16:25:31 +0100286 if (max == 0) {
287 b->flags |= BF_FULL;
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100288 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau9c0fe592009-01-18 16:25:31 +0100289 break;
290 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200291
Willy Tarreau6996e152007-04-30 14:37:43 +0200292 /*
293 * 2. read the largest possible block
294 */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200295#ifndef MSG_NOSIGNAL
Willy Tarreau83749182007-04-15 20:56:27 +0200296 {
297 int skerr;
298 socklen_t lskerr = sizeof(skerr);
299
300 ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr);
301 if (ret == -1 || skerr)
302 ret = -1;
303 else
304 ret = recv(fd, b->r, max, 0);
305 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200306#else
Willy Tarreau83749182007-04-15 20:56:27 +0200307 ret = recv(fd, b->r, max, MSG_NOSIGNAL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200308#endif
Willy Tarreau83749182007-04-15 20:56:27 +0200309 if (ret > 0) {
310 b->r += ret;
311 b->l += ret;
Willy Tarreau8a7af602008-05-03 23:07:14 +0200312 cur_read += ret;
Willy Tarreaub38903c2008-11-23 21:33:29 +0100313
Willy Tarreau0abebcc2009-01-08 00:09:41 +0100314 /* if we're allowed to directly forward data, we must update send_max */
315 if (b->to_forward > 0) {
316 int fwd = MIN(b->to_forward, ret);
317 b->send_max += fwd;
318 b->to_forward -= fwd;
319 }
Willy Tarreauf890dc92008-12-13 21:12:26 +0100320
Willy Tarreaub38903c2008-11-23 21:33:29 +0100321 if (fdtab[fd].state == FD_STCONN)
322 fdtab[fd].state = FD_STREADY;
323
Willy Tarreau3da77c52008-08-29 09:58:42 +0200324 b->flags |= BF_READ_PARTIAL;
Willy Tarreaue393fe22008-08-16 22:18:07 +0200325 b->flags &= ~BF_EMPTY;
Willy Tarreau74ab2ac2008-11-23 17:23:07 +0100326
Willy Tarreau83749182007-04-15 20:56:27 +0200327 if (b->r == b->data + BUFSIZE) {
328 b->r = b->data; /* wrap around the buffer */
329 }
Willy Tarreau9641e8f2007-03-23 23:02:09 +0100330
Willy Tarreau83749182007-04-15 20:56:27 +0200331 b->total += ret;
Willy Tarreau9641e8f2007-03-23 23:02:09 +0100332
Willy Tarreau03d60bb2009-01-09 11:13:00 +0100333 if (b->l >= b->max_len) {
Willy Tarreau6996e152007-04-30 14:37:43 +0200334 /* The buffer is now full, there's no point in going through
335 * the loop again.
336 */
Willy Tarreau8a7af602008-05-03 23:07:14 +0200337 if (!(b->flags & BF_STREAMER_FAST) && (cur_read == b->l)) {
338 b->xfer_small = 0;
339 b->xfer_large++;
340 if (b->xfer_large >= 3) {
341 /* we call this buffer a fast streamer if it manages
342 * to be filled in one call 3 consecutive times.
343 */
344 b->flags |= (BF_STREAMER | BF_STREAMER_FAST);
345 //fputc('+', stderr);
346 }
347 }
348 else if ((b->flags & (BF_STREAMER | BF_STREAMER_FAST)) &&
349 (cur_read <= BUFSIZE / 2)) {
350 b->xfer_large = 0;
351 b->xfer_small++;
352 if (b->xfer_small >= 2) {
353 /* if the buffer has been at least half full twice,
354 * we receive faster than we send, so at least it
355 * is not a "fast streamer".
356 */
357 b->flags &= ~BF_STREAMER_FAST;
358 //fputc('-', stderr);
359 }
360 }
361 else {
362 b->xfer_small = 0;
363 b->xfer_large = 0;
364 }
Willy Tarreau9c0fe592009-01-18 16:25:31 +0100365
366 b->flags |= BF_FULL;
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100367 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau9c0fe592009-01-18 16:25:31 +0100368 break;
Willy Tarreau6996e152007-04-30 14:37:43 +0200369 }
370
Willy Tarreauab3e1d32007-06-03 14:10:36 +0200371 /* if too many bytes were missing from last read, it means that
372 * it's pointless trying to read again because the system does
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100373 * not have them in buffers. BTW, if FD_POLL_HUP was present,
374 * it means that we have reached the end and that the connection
375 * is closed.
Willy Tarreauab3e1d32007-06-03 14:10:36 +0200376 */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100377 if (ret < max) {
Willy Tarreau8a7af602008-05-03 23:07:14 +0200378 if ((b->flags & (BF_STREAMER | BF_STREAMER_FAST)) &&
379 (cur_read <= BUFSIZE / 2)) {
380 b->xfer_large = 0;
381 b->xfer_small++;
382 if (b->xfer_small >= 3) {
383 /* we have read less than half of the buffer in
384 * one pass, and this happened at least 3 times.
385 * This is definitely not a streamer.
386 */
387 b->flags &= ~(BF_STREAMER | BF_STREAMER_FAST);
388 //fputc('!', stderr);
389 }
390 }
Willy Tarreau2bea3a12008-08-28 09:47:43 +0200391 /* unfortunately, on level-triggered events, POLL_HUP
392 * is generally delivered AFTER the system buffer is
393 * empty, so this one might never match.
394 */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100395 if (fdtab[fd].ev & FD_POLL_HUP)
396 goto out_shutdown_r;
Willy Tarreau2bea3a12008-08-28 09:47:43 +0200397
398 /* if a streamer has read few data, it may be because we
399 * have exhausted system buffers. It's not worth trying
400 * again.
401 */
402 if (b->flags & BF_STREAMER)
403 break;
Willy Tarreauab3e1d32007-06-03 14:10:36 +0200404
Willy Tarreau6f4a82c2009-03-21 20:43:57 +0100405 /* generally if we read something smaller than 1 or 2 MSS,
406 * it means that either we have exhausted the system's
407 * buffers (streamer or question-response protocol) or
408 * that the connection will be closed. Streamers are
409 * easily detected so we return early. For other cases,
410 * it's still better to perform a last read to be sure,
411 * because it may save one complete poll/read/wakeup cycle
412 * in case of shutdown.
413 */
414 if (ret < MIN_RET_FOR_READ_LOOP && b->flags & BF_STREAMER)
415 break;
416
417 /* if we read a large block smaller than what we requested,
418 * it's almost certain we'll never get anything more.
419 */
420 if (ret >= global.tune.recv_enough)
421 break;
422 }
Willy Tarreau83749182007-04-15 20:56:27 +0200423
Willy Tarreau1b194fe2009-03-21 21:10:04 +0100424 if ((b->flags & BF_READ_DONTWAIT) || --read_poll <= 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200425 break;
Willy Tarreau83749182007-04-15 20:56:27 +0200426 }
427 else if (ret == 0) {
Willy Tarreau6996e152007-04-30 14:37:43 +0200428 /* connection closed */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100429 goto out_shutdown_r;
Willy Tarreau83749182007-04-15 20:56:27 +0200430 }
Willy Tarreau9f195292007-04-15 21:26:58 +0200431 else if (errno == EAGAIN) {
432 /* Ignore EAGAIN but inform the poller that there is
Willy Tarreauaf78d0f2009-01-08 10:09:08 +0100433 * nothing to read left if we did not read much, ie
434 * less than what we were still expecting to read.
435 * But we may have done some work justifying to notify
436 * the task.
Willy Tarreau9f195292007-04-15 21:26:58 +0200437 */
Willy Tarreauaf78d0f2009-01-08 10:09:08 +0100438 if (cur_read < MIN_RET_FOR_READ_LOOP)
439 retval = 0;
Willy Tarreau83749182007-04-15 20:56:27 +0200440 break;
441 }
442 else {
Willy Tarreau6996e152007-04-30 14:37:43 +0200443 goto out_error;
Willy Tarreau83749182007-04-15 20:56:27 +0200444 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200445 } /* while (1) */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200446
Willy Tarreau6996e152007-04-30 14:37:43 +0200447 out_wakeup:
Willy Tarreau9c0fe592009-01-18 16:25:31 +0100448 /* We might have some data the consumer is waiting for */
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100449 if ((b->send_max || b->pipe) && (b->cons->flags & SI_FL_WAIT_DATA)) {
450 int last_len = b->pipe ? b->pipe->data : 0;
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100451
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100452 b->cons->chk_snd(b->cons);
453
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100454 /* check if the consumer has freed some space */
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100455 if (!(b->flags & BF_FULL) &&
456 (!last_len || !b->pipe || b->pipe->data < last_len))
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100457 si->flags &= ~SI_FL_WAIT_ROOM;
458 }
459
460 if (si->flags & SI_FL_WAIT_ROOM) {
Willy Tarreau9c0fe592009-01-18 16:25:31 +0100461 EV_FD_CLR(fd, DIR_RD);
462 b->rex = TICK_ETERNITY;
463 }
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100464 else if ((b->flags & (BF_READ_PARTIAL|BF_FULL|BF_READ_NOEXP)) == BF_READ_PARTIAL)
465 b->rex = tick_add_ifset(now_ms, b->rto);
Willy Tarreau9c0fe592009-01-18 16:25:31 +0100466
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100467 /* we have to wake up if there is a special event or if we don't have
468 * any more data to forward.
469 */
Willy Tarreau1b194fe2009-03-21 21:10:04 +0100470 if ((b->flags & (BF_READ_NULL|BF_READ_ERROR|BF_SHUTR|BF_READ_DONTWAIT)) ||
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100471 !b->to_forward ||
472 si->state != SI_ST_EST ||
473 b->cons->state != SI_ST_EST ||
474 (si->flags & SI_FL_ERR))
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100475 task_wakeup(si->owner, TASK_WOKEN_IO);
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100476
Willy Tarreau1b194fe2009-03-21 21:10:04 +0100477 b->flags &= ~BF_READ_DONTWAIT;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100478 fdtab[fd].ev &= ~FD_POLL_IN;
Willy Tarreau83749182007-04-15 20:56:27 +0200479 return retval;
Willy Tarreau6996e152007-04-30 14:37:43 +0200480
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100481 out_shutdown_r:
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200482 /* we received a shutdown */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100483 fdtab[fd].ev &= ~FD_POLL_HUP;
484 b->flags |= BF_READ_NULL;
Willy Tarreau99126c32008-11-27 10:30:51 +0100485 stream_sock_shutr(si);
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200486 goto out_wakeup;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100487
Willy Tarreau6996e152007-04-30 14:37:43 +0200488 out_error:
Willy Tarreaucff64112008-11-03 06:26:53 +0100489 /* Read error on the file descriptor. We mark the FD as STERROR so
490 * that we don't use it anymore. The error is reported to the stream
491 * interface which will take proper action. We must not perturbate the
492 * buffer because the stream interface wants to ensure transparent
493 * connection retries.
Willy Tarreau6996e152007-04-30 14:37:43 +0200494 */
Willy Tarreaucff64112008-11-03 06:26:53 +0100495
Willy Tarreau6996e152007-04-30 14:37:43 +0200496 fdtab[fd].state = FD_STERROR;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100497 fdtab[fd].ev &= ~FD_POLL_STICKY;
Willy Tarreaucff64112008-11-03 06:26:53 +0100498 si->flags |= SI_FL_ERR;
Willy Tarreau9c0fe592009-01-18 16:25:31 +0100499 retval = 1;
500 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200501}
502
503
504/*
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100505 * This function is called to send buffer data to a stream socket.
506 * It returns -1 in case of unrecoverable error, 0 if the caller needs to poll
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100507 * before calling it again, otherwise 1. If a pipe was associated with the
508 * buffer and it empties it, it releases it as well.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200509 */
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100510static int stream_sock_write_loop(struct stream_interface *si, struct buffer *b)
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100511{
Willy Tarreau83749182007-04-15 20:56:27 +0200512 int write_poll = MAX_WRITE_POLL_LOOPS;
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100513 int retval = 1;
514 int ret, max;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200515
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100516#if defined(CONFIG_HAP_LINUX_SPLICE)
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100517 while (b->pipe) {
518 ret = splice(b->pipe->cons, NULL, si->fd, NULL, b->pipe->data,
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100519 SPLICE_F_MOVE|SPLICE_F_NONBLOCK);
520 if (ret <= 0) {
521 if (ret == 0 || errno == EAGAIN) {
522 retval = 0;
523 return retval;
524 }
525 /* here we have another error */
526 retval = -1;
527 return retval;
528 }
529
530 b->flags |= BF_WRITE_PARTIAL;
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100531 b->pipe->data -= ret;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100532
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100533 if (!b->pipe->data) {
534 put_pipe(b->pipe);
535 b->pipe = NULL;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100536 break;
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100537 }
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100538
539 if (--write_poll <= 0)
540 return retval;
541 }
542
543 /* At this point, the pipe is empty, but we may still have data pending
544 * in the normal buffer.
545 */
546 if (!b->l) {
547 b->flags |= BF_EMPTY;
548 return retval;
549 }
550#endif
Willy Tarreaud2def0f2009-01-18 17:37:33 +0100551 if (!b->send_max)
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100552 return retval;
Willy Tarreau83749182007-04-15 20:56:27 +0200553
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100554 /* when we're in this loop, we already know that there is no spliced
555 * data left, and that there are sendable buffered data.
556 */
Willy Tarreau6996e152007-04-30 14:37:43 +0200557 while (1) {
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100558 if (b->r > b->w)
Willy Tarreau83749182007-04-15 20:56:27 +0200559 max = b->r - b->w;
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100560 else
Willy Tarreau83749182007-04-15 20:56:27 +0200561 max = b->data + BUFSIZE - b->w;
Willy Tarreau83749182007-04-15 20:56:27 +0200562
Willy Tarreauf890dc92008-12-13 21:12:26 +0100563 /* limit the amount of outgoing data if required */
564 if (max > b->send_max)
565 max = b->send_max;
566
Willy Tarreaubaaee002006-06-26 02:48:02 +0200567#ifndef MSG_NOSIGNAL
568 {
569 int skerr;
570 socklen_t lskerr = sizeof(skerr);
571
Willy Tarreau87bed622009-03-08 22:25:28 +0100572 ret = getsockopt(si->fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr);
Willy Tarreauc6423482006-10-15 14:59:03 +0200573 if (ret == -1 || skerr)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200574 ret = -1;
575 else
Willy Tarreau87bed622009-03-08 22:25:28 +0100576 ret = send(si->fd, b->w, max, MSG_DONTWAIT);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200577 }
578#else
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100579 ret = send(si->fd, b->w, max, MSG_DONTWAIT | MSG_NOSIGNAL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200580#endif
581
582 if (ret > 0) {
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100583 if (fdtab[si->fd].state == FD_STCONN)
584 fdtab[si->fd].state = FD_STREADY;
Willy Tarreaub38903c2008-11-23 21:33:29 +0100585
Willy Tarreau3da77c52008-08-29 09:58:42 +0200586 b->flags |= BF_WRITE_PARTIAL;
Willy Tarreaue393fe22008-08-16 22:18:07 +0200587
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100588 b->w += ret;
589 if (b->w == b->data + BUFSIZE)
590 b->w = b->data; /* wrap around the buffer */
591
592 b->l -= ret;
593 if (likely(b->l < b->max_len))
Willy Tarreaue393fe22008-08-16 22:18:07 +0200594 b->flags &= ~BF_FULL;
Willy Tarreau74ab2ac2008-11-23 17:23:07 +0100595
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100596 if (likely(!b->l)) {
597 /* optimize data alignment in the buffer */
598 b->r = b->w = b->lr = b->data;
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100599 if (likely(!b->pipe))
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100600 b->flags |= BF_EMPTY;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200601 }
Willy Tarreau83749182007-04-15 20:56:27 +0200602
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100603 b->send_max -= ret;
604 if (!b->send_max || !b->l)
605 break;
Willy Tarreau83749182007-04-15 20:56:27 +0200606
Willy Tarreauab3e1d32007-06-03 14:10:36 +0200607 /* if the system buffer is full, don't insist */
608 if (ret < max)
609 break;
610
Willy Tarreau6996e152007-04-30 14:37:43 +0200611 if (--write_poll <= 0)
612 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200613 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200614 else if (ret == 0 || errno == EAGAIN) {
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100615 /* nothing written, we need to poll for write first */
Willy Tarreau83749182007-04-15 20:56:27 +0200616 retval = 0;
617 break;
618 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200619 else {
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100620 /* bad, we got an error */
621 retval = -1;
622 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200623 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200624 } /* while (1) */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200625
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100626 return retval;
627}
Willy Tarreau6996e152007-04-30 14:37:43 +0200628
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100629
630/*
631 * This function is called on a write event from a stream socket.
632 * It returns 0 if the caller needs to poll before calling it again, otherwise
633 * non-zero.
634 */
635int stream_sock_write(int fd)
636{
637 struct stream_interface *si = fdtab[fd].owner;
638 struct buffer *b = si->ob;
639 int retval = 1;
640
641#ifdef DEBUG_FULL
642 fprintf(stderr,"stream_sock_write : fd=%d, owner=%p\n", fd, fdtab[fd].owner);
643#endif
644
645 retval = 1;
646 if (fdtab[fd].state == FD_STERROR || (fdtab[fd].ev & FD_POLL_ERR))
647 goto out_error;
648
649 if (likely(!(b->flags & BF_EMPTY))) {
650 /* OK there are data waiting to be sent */
651 retval = stream_sock_write_loop(si, b);
652 if (retval < 0)
653 goto out_error;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200654 }
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100655 else {
656 /* may be we have received a connection acknowledgement in TCP mode without data */
657 if (likely(fdtab[fd].state == FD_STCONN)) {
658 /* We have no data to send to check the connection, and
659 * getsockopt() will not inform us whether the connection
660 * is still pending. So we'll reuse connect() to check the
661 * state of the socket. This has the advantage of givig us
662 * the following info :
663 * - error
664 * - connecting (EALREADY, EINPROGRESS)
665 * - connected (EISCONN, 0)
666 */
667 if ((connect(fd, fdtab[fd].peeraddr, fdtab[fd].peerlen) == 0))
668 errno = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200669
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100670 if (errno == EALREADY || errno == EINPROGRESS) {
671 retval = 0;
672 goto out_may_wakeup;
673 }
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100674
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100675 if (errno && errno != EISCONN)
676 goto out_error;
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200677
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100678 /* OK we just need to indicate that we got a connection
679 * and that we wrote nothing.
680 */
681 b->flags |= BF_WRITE_NULL;
682 fdtab[fd].state = FD_STREADY;
683 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200684
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100685 /* Funny, we were called to write something but there wasn't
686 * anything. We can get there, for example if we were woken up
687 * on a write event to finish the splice, but the send_max is 0
688 * so we cannot write anything from the buffer. Let's disable
689 * the write event and pretend we never came there.
690 */
691 }
692
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100693 if (!b->pipe && !b->send_max) {
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100694 /* the connection is established but we can't write. Either the
695 * buffer is empty, or we just refrain from sending because the
696 * send_max limit was reached. Maybe we just wrote the last
697 * chunk and need to close.
698 */
699 if (((b->flags & (BF_SHUTW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) ==
700 (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR)) &&
701 (si->state == SI_ST_EST)) {
702 stream_sock_shutw(si);
703 goto out_wakeup;
704 }
705
706 if (b->flags & BF_EMPTY)
Willy Tarreauac128fe2009-01-09 13:05:19 +0100707 si->flags |= SI_FL_WAIT_DATA;
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100708
Willy Tarreauac128fe2009-01-09 13:05:19 +0100709 EV_FD_CLR(fd, DIR_WR);
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100710 b->wex = TICK_ETERNITY;
Willy Tarreauac128fe2009-01-09 13:05:19 +0100711 }
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100712
713 out_may_wakeup:
714 if (b->flags & BF_WRITE_ACTIVITY) {
715 /* update timeout if we have written something */
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100716 if ((b->send_max || b->pipe) &&
Willy Tarreaud2def0f2009-01-18 17:37:33 +0100717 (b->flags & (BF_SHUTW|BF_WRITE_PARTIAL)) == BF_WRITE_PARTIAL)
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100718 b->wex = tick_add_ifset(now_ms, b->wto);
719
720 out_wakeup:
721 if (tick_isset(si->ib->rex)) {
722 /* Note: to prevent the client from expiring read timeouts
723 * during writes, we refresh it. A better solution would be
724 * to merge read+write timeouts into a unique one, although
725 * that needs some study particularly on full-duplex TCP
726 * connections.
727 */
728 si->ib->rex = tick_add_ifset(now_ms, si->ib->rto);
729 }
730
731 /* the producer might be waiting for more room to store data */
732 if (likely((b->flags & (BF_WRITE_PARTIAL|BF_FULL)) == BF_WRITE_PARTIAL &&
733 (b->prod->flags & SI_FL_WAIT_ROOM)))
734 b->prod->chk_rcv(b->prod);
735
736 /* we have to wake up if there is a special event or if we don't have
737 * any more data to forward and it's not planned to send any more.
738 */
739 if (likely((b->flags & (BF_WRITE_NULL|BF_WRITE_ERROR|BF_SHUTW)) ||
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100740 (!b->to_forward && !b->send_max && !b->pipe) ||
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100741 si->state != SI_ST_EST ||
742 b->prod->state != SI_ST_EST))
743 task_wakeup(si->owner, TASK_WOKEN_IO);
744 }
745
746 fdtab[fd].ev &= ~FD_POLL_OUT;
747 return retval;
Willy Tarreauac128fe2009-01-09 13:05:19 +0100748
Willy Tarreau6996e152007-04-30 14:37:43 +0200749 out_error:
Willy Tarreaucff64112008-11-03 06:26:53 +0100750 /* Write error on the file descriptor. We mark the FD as STERROR so
751 * that we don't use it anymore. The error is reported to the stream
752 * interface which will take proper action. We must not perturbate the
753 * buffer because the stream interface wants to ensure transparent
754 * connection retries.
Willy Tarreau6996e152007-04-30 14:37:43 +0200755 */
Willy Tarreaucff64112008-11-03 06:26:53 +0100756
Willy Tarreau6996e152007-04-30 14:37:43 +0200757 fdtab[fd].state = FD_STERROR;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100758 fdtab[fd].ev &= ~FD_POLL_STICKY;
Willy Tarreaucff64112008-11-03 06:26:53 +0100759 si->flags |= SI_FL_ERR;
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200760 task_wakeup(si->owner, TASK_WOKEN_IO);
761 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200762}
763
Willy Tarreau48adac52008-08-30 04:58:38 +0200764/*
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200765 * This function performs a shutdown-write on a stream interface in a connected or
766 * init state (it does nothing for other states). It either shuts the write side
Willy Tarreau99126c32008-11-27 10:30:51 +0100767 * or closes the file descriptor and marks itself as closed. The buffer flags are
768 * updated to reflect the new state.
Willy Tarreau48adac52008-08-30 04:58:38 +0200769 */
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100770void stream_sock_shutw(struct stream_interface *si)
Willy Tarreau48adac52008-08-30 04:58:38 +0200771{
Willy Tarreau99126c32008-11-27 10:30:51 +0100772 if (si->ob->flags & BF_SHUTW)
773 return;
774 si->ob->flags |= BF_SHUTW;
775 si->ob->wex = TICK_ETERNITY;
Willy Tarreaub0ef7352008-12-14 13:26:20 +0100776 si->flags &= ~SI_FL_WAIT_DATA;
Willy Tarreau99126c32008-11-27 10:30:51 +0100777
Willy Tarreaub38903c2008-11-23 21:33:29 +0100778 switch (si->state) {
Willy Tarreaub38903c2008-11-23 21:33:29 +0100779 case SI_ST_EST:
780 if (!(si->ib->flags & BF_SHUTR)) {
781 EV_FD_CLR(si->fd, DIR_WR);
782 shutdown(si->fd, SHUT_WR);
783 return;
784 }
785 /* fall through */
786 case SI_ST_CON:
Willy Tarreaub0ef7352008-12-14 13:26:20 +0100787 si->flags &= ~SI_FL_WAIT_ROOM;
Willy Tarreau8bfa4262008-11-27 09:25:45 +0100788 /* we may have to close a pending connection, and mark the
789 * response buffer as shutr
790 */
Willy Tarreau48adac52008-08-30 04:58:38 +0200791 fd_delete(si->fd);
Willy Tarreaufe3718a2008-11-30 18:14:12 +0100792 /* fall through */
793 case SI_ST_CER:
Willy Tarreau7f006512008-12-07 14:04:04 +0100794 si->state = SI_ST_DIS;
795 default:
Willy Tarreau99126c32008-11-27 10:30:51 +0100796 si->ib->flags |= BF_SHUTR;
Willy Tarreaufe3718a2008-11-30 18:14:12 +0100797 si->ib->rex = TICK_ETERNITY;
Willy Tarreau127334e2009-03-28 10:47:26 +0100798 si->exp = TICK_ETERNITY;
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100799 return;
Willy Tarreau48adac52008-08-30 04:58:38 +0200800 }
Willy Tarreau48adac52008-08-30 04:58:38 +0200801}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200802
Willy Tarreau2d212792008-08-27 21:41:35 +0200803/*
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200804 * This function performs a shutdown-read on a stream interface in a connected or
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100805 * init state (it does nothing for other states). It either shuts the read side
Willy Tarreau99126c32008-11-27 10:30:51 +0100806 * or closes the file descriptor and marks itself as closed. The buffer flags are
807 * updated to reflect the new state.
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200808 */
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100809void stream_sock_shutr(struct stream_interface *si)
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200810{
Willy Tarreau99126c32008-11-27 10:30:51 +0100811 if (si->ib->flags & BF_SHUTR)
812 return;
813 si->ib->flags |= BF_SHUTR;
814 si->ib->rex = TICK_ETERNITY;
Willy Tarreaub0ef7352008-12-14 13:26:20 +0100815 si->flags &= ~SI_FL_WAIT_ROOM;
Willy Tarreau99126c32008-11-27 10:30:51 +0100816
Willy Tarreau8bfa4262008-11-27 09:25:45 +0100817 if (si->state != SI_ST_EST && si->state != SI_ST_CON)
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100818 return;
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200819
Willy Tarreaucff64112008-11-03 06:26:53 +0100820 if (si->ob->flags & BF_SHUTW) {
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200821 fd_delete(si->fd);
Willy Tarreau74ab2ac2008-11-23 17:23:07 +0100822 si->state = SI_ST_DIS;
Willy Tarreau127334e2009-03-28 10:47:26 +0100823 si->exp = TICK_ETERNITY;
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100824 return;
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200825 }
826 EV_FD_CLR(si->fd, DIR_RD);
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100827 return;
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200828}
829
830/*
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200831 * Updates a connected stream_sock file descriptor status and timeouts
832 * according to the buffers' flags. It should only be called once after the
833 * buffer flags have settled down, and before they are cleared. It doesn't
834 * harm to call it as often as desired (it just slightly hurts performance).
835 */
Willy Tarreaub0253252008-11-30 21:37:12 +0100836void stream_sock_data_finish(struct stream_interface *si)
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200837{
Willy Tarreaub0253252008-11-30 21:37:12 +0100838 struct buffer *ib = si->ib;
839 struct buffer *ob = si->ob;
840 int fd = si->fd;
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200841
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200842 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 +0200843 now_ms, __FUNCTION__,
844 fd, fdtab[fd].owner,
845 ib, ob,
846 ib->rex, ob->wex,
847 ib->flags, ob->flags,
Willy Tarreaub0253252008-11-30 21:37:12 +0100848 ib->l, ob->l, si->state);
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200849
850 /* Check if we need to close the read side */
851 if (!(ib->flags & BF_SHUTR)) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200852 /* Read not closed, update FD status and timeout for reads */
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200853 if (ib->flags & (BF_FULL|BF_HIJACK)) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200854 /* stop reading */
Willy Tarreaub0ef7352008-12-14 13:26:20 +0100855 if ((ib->flags & (BF_FULL|BF_HIJACK)) == BF_FULL)
856 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau2d212792008-08-27 21:41:35 +0200857 EV_FD_COND_C(fd, DIR_RD);
858 ib->rex = TICK_ETERNITY;
859 }
860 else {
861 /* (re)start reading and update timeout. Note: we don't recompute the timeout
862 * everytime we get here, otherwise it would risk never to expire. We only
863 * update it if is was not yet set, or if we already got some read status.
864 */
Willy Tarreaub0ef7352008-12-14 13:26:20 +0100865 si->flags &= ~SI_FL_WAIT_ROOM;
Willy Tarreau2d212792008-08-27 21:41:35 +0200866 EV_FD_COND_S(fd, DIR_RD);
Willy Tarreau86491c32008-12-14 09:04:47 +0100867 if (!(ib->flags & BF_READ_NOEXP) &&
868 (!tick_isset(ib->rex) || ib->flags & BF_READ_ACTIVITY))
Willy Tarreau2d212792008-08-27 21:41:35 +0200869 ib->rex = tick_add_ifset(now_ms, ib->rto);
870 }
871 }
872
873 /* Check if we need to close the write side */
874 if (!(ob->flags & BF_SHUTW)) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200875 /* Write not closed, update FD status and timeout for writes */
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100876 if ((ob->send_max == 0 && !ob->pipe) ||
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100877 (ob->flags & BF_EMPTY) ||
Willy Tarreau3da77c52008-08-29 09:58:42 +0200878 (ob->flags & (BF_HIJACK|BF_WRITE_ENA)) == 0) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200879 /* stop writing */
Willy Tarreaub0ef7352008-12-14 13:26:20 +0100880 if ((ob->flags & (BF_EMPTY|BF_HIJACK|BF_WRITE_ENA)) == (BF_EMPTY|BF_WRITE_ENA))
881 si->flags |= SI_FL_WAIT_DATA;
Willy Tarreau2d212792008-08-27 21:41:35 +0200882 EV_FD_COND_C(fd, DIR_WR);
883 ob->wex = TICK_ETERNITY;
884 }
885 else {
886 /* (re)start writing and update timeout. Note: we don't recompute the timeout
887 * everytime we get here, otherwise it would risk never to expire. We only
888 * update it if is was not yet set, or if we already got some write status.
889 */
Willy Tarreaub0ef7352008-12-14 13:26:20 +0100890 si->flags &= ~SI_FL_WAIT_DATA;
Willy Tarreau2d212792008-08-27 21:41:35 +0200891 EV_FD_COND_S(fd, DIR_WR);
Willy Tarreau3da77c52008-08-29 09:58:42 +0200892 if (!tick_isset(ob->wex) || ob->flags & BF_WRITE_ACTIVITY) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200893 ob->wex = tick_add_ifset(now_ms, ob->wto);
Willy Tarreau21e1be82008-08-29 11:30:14 +0200894 if (tick_isset(ob->wex) && tick_isset(ib->rex)) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200895 /* Note: depending on the protocol, we don't know if we're waiting
896 * for incoming data or not. So in order to prevent the socket from
897 * expiring read timeouts during writes, we refresh the read timeout,
898 * except if it was already infinite.
899 */
900 ib->rex = ob->wex;
901 }
902 }
903 }
904 }
Willy Tarreau2d212792008-08-27 21:41:35 +0200905}
906
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100907/* This function is used for inter-stream-interface calls. It is called by the
908 * consumer to inform the producer side that it may be interested in checking
909 * for free space in the buffer. Note that it intentionally does not update
910 * timeouts, so that we can still check them later at wake-up.
911 */
912void stream_sock_chk_rcv(struct stream_interface *si)
913{
914 struct buffer *ib = si->ib;
915
916 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",
917 now_ms, __FUNCTION__,
Vincenzo Farruggia9b97cff2009-01-30 16:49:10 +0000918 si->fd, fdtab[si->fd].owner,
919 ib, si->ob,
920 ib->rex, si->ob->wex,
921 ib->flags, si->ob->flags,
922 ib->l, si->ob->l, si->state);
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100923
924 if (unlikely(si->state != SI_ST_EST || (ib->flags & BF_SHUTR)))
925 return;
926
927 if (ib->flags & (BF_FULL|BF_HIJACK)) {
928 /* stop reading */
929 if ((ib->flags & (BF_FULL|BF_HIJACK)) == BF_FULL)
930 si->flags |= SI_FL_WAIT_ROOM;
931 EV_FD_COND_C(si->fd, DIR_RD);
932 }
933 else {
934 /* (re)start reading */
935 si->flags &= ~SI_FL_WAIT_ROOM;
936 EV_FD_COND_S(si->fd, DIR_RD);
937 }
938}
939
940
941/* This function is used for inter-stream-interface calls. It is called by the
942 * producer to inform the consumer side that it may be interested in checking
943 * for data in the buffer. Note that it intentionally does not update timeouts,
944 * so that we can still check them later at wake-up.
945 */
946void stream_sock_chk_snd(struct stream_interface *si)
947{
948 struct buffer *ob = si->ob;
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100949 int retval;
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100950
951 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",
952 now_ms, __FUNCTION__,
Vincenzo Farruggia9b97cff2009-01-30 16:49:10 +0000953 si->fd, fdtab[si->fd].owner,
954 si->ib, ob,
955 si->ib->rex, ob->wex,
956 si->ib->flags, ob->flags,
957 si->ib->l, ob->l, si->state);
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100958
959 if (unlikely(si->state != SI_ST_EST || (ob->flags & BF_SHUTW)))
960 return;
961
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100962 if (!(si->flags & SI_FL_WAIT_DATA) || /* not waiting for data */
963 (fdtab[si->fd].ev & FD_POLL_OUT) || /* we'll be called anyway */
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100964 !(ob->send_max || ob->pipe) || /* called with nothing to send ! */
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100965 !(ob->flags & (BF_HIJACK|BF_WRITE_ENA))) /* we may not write */
966 return;
967
968 retval = stream_sock_write_loop(si, ob);
969 if (retval < 0) {
970 /* Write error on the file descriptor. We mark the FD as STERROR so
971 * that we don't use it anymore and we notify the task.
972 */
973 fdtab[si->fd].state = FD_STERROR;
974 fdtab[si->fd].ev &= ~FD_POLL_STICKY;
975 si->flags |= SI_FL_ERR;
976 goto out_wakeup;
977 }
978
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100979 if (retval > 0 || (ob->send_max == 0 && !ob->pipe)) {
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100980 /* the connection is established but we can't write. Either the
981 * buffer is empty, or we just refrain from sending because the
982 * send_max limit was reached. Maybe we just wrote the last
983 * chunk and need to close.
984 */
985 if (((ob->flags & (BF_SHUTW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) ==
986 (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR)) &&
987 (si->state == SI_ST_EST)) {
988 stream_sock_shutw(si);
989 goto out_wakeup;
990 }
991
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100992 if ((ob->flags & (BF_EMPTY|BF_HIJACK|BF_WRITE_ENA)) == (BF_EMPTY|BF_WRITE_ENA))
993 si->flags |= SI_FL_WAIT_DATA;
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100994 ob->wex = TICK_ETERNITY;
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100995 }
996 else {
997 /* (re)start writing. */
998 si->flags &= ~SI_FL_WAIT_DATA;
999 EV_FD_COND_S(si->fd, DIR_WR);
1000 }
Willy Tarreaua456f2a2009-01-18 17:38:44 +01001001
Willy Tarreauc9619462009-03-09 22:40:57 +01001002 if (likely(ob->flags & BF_WRITE_ACTIVITY)) {
1003 /* update timeout if we have written something */
1004 if ((ob->send_max || ob->pipe) &&
1005 (ob->flags & (BF_SHUTW|BF_WRITE_PARTIAL)) == BF_WRITE_PARTIAL)
1006 ob->wex = tick_add_ifset(now_ms, ob->wto);
1007
1008 if (tick_isset(si->ib->rex)) {
1009 /* Note: to prevent the client from expiring read timeouts
1010 * during writes, we refresh it. A better solution would be
1011 * to merge read+write timeouts into a unique one, although
1012 * that needs some study particularly on full-duplex TCP
1013 * connections.
1014 */
1015 si->ib->rex = tick_add_ifset(now_ms, si->ib->rto);
1016 }
1017 }
1018
Willy Tarreaua456f2a2009-01-18 17:38:44 +01001019 /* in case of special condition (error, shutdown, end of write...), we
1020 * have to notify the task.
1021 */
1022 if (likely((ob->flags & (BF_WRITE_NULL|BF_WRITE_ERROR|BF_SHUTW)) ||
Willy Tarreau3eba98a2009-01-25 13:56:13 +01001023 (!ob->to_forward && !ob->send_max && !ob->pipe) ||
Willy Tarreaua456f2a2009-01-18 17:38:44 +01001024 si->state != SI_ST_EST)) {
1025 out_wakeup:
1026 task_wakeup(si->owner, TASK_WOKEN_IO);
1027 }
Willy Tarreau3ffeba12008-12-14 14:42:35 +01001028}
1029
Willy Tarreaubaaee002006-06-26 02:48:02 +02001030
1031/*
1032 * Local variables:
1033 * c-indent-level: 8
1034 * c-basic-offset: 8
1035 * End:
1036 */