blob: ed53ca08af32f76033ece874ec75ff5f45177b7d [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 Tarreau3eba98a2009-01-25 13:56:13 +0100200 if (b->pipe->data >= SPLICE_FULL_HINT) {
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100201 /* We've read enough of it for this time. */
202 retval = 1;
203 break;
204 }
205 } /* while */
206
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100207 if (unlikely(!b->pipe->data)) {
208 put_pipe(b->pipe);
209 b->pipe = NULL;
210 }
211
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100212 return retval;
213}
214
Willy Tarreau6b4aad42009-01-18 21:59:13 +0100215#endif /* CONFIG_HAP_LINUX_SPLICE */
216
217
Willy Tarreaubaaee002006-06-26 02:48:02 +0200218/*
Willy Tarreaud7971282006-07-29 18:36:34 +0200219 * this function is called on a read event from a stream socket.
Willy Tarreau83749182007-04-15 20:56:27 +0200220 * It returns 0 if we have a high confidence that we will not be
221 * able to read more data without polling first. Returns non-zero
222 * otherwise.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200223 */
Willy Tarreaud7971282006-07-29 18:36:34 +0200224int stream_sock_read(int fd) {
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200225 struct stream_interface *si = fdtab[fd].owner;
Willy Tarreau48adac52008-08-30 04:58:38 +0200226 struct buffer *b = si->ib;
Willy Tarreau8a7af602008-05-03 23:07:14 +0200227 int ret, max, retval, cur_read;
Willy Tarreaub8949f12007-03-23 22:39:59 +0100228 int read_poll = MAX_READ_POLL_LOOPS;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200229
230#ifdef DEBUG_FULL
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100231 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 +0200232#endif
233
Willy Tarreau83749182007-04-15 20:56:27 +0200234 retval = 1;
235
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100236 /* stop immediately on errors */
237 if (fdtab[fd].state == FD_STERROR || (fdtab[fd].ev & FD_POLL_ERR))
Willy Tarreau6996e152007-04-30 14:37:43 +0200238 goto out_error;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100239
240 /* stop here if we reached the end of data */
241 if ((fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP)) == FD_POLL_HUP)
242 goto out_shutdown_r;
Willy Tarreau83749182007-04-15 20:56:27 +0200243
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100244#if defined(CONFIG_HAP_LINUX_SPLICE)
245 if (b->to_forward && b->flags & BF_KERN_SPLICING) {
Willy Tarreau98b306b2009-01-25 11:11:32 +0100246
247 /* Under Linux, if FD_POLL_HUP is set, we have reached the end.
248 * Since older splice() implementations were buggy and returned
249 * EAGAIN on end of read, let's bypass the call to splice() now.
250 */
251 if (fdtab[fd].ev & FD_POLL_HUP)
252 goto out_shutdown_r;
253
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100254 retval = stream_sock_splice_in(b, si);
255
256 if (retval >= 0) {
257 if (si->flags & SI_FL_ERR)
258 goto out_error;
259 if (b->flags & BF_READ_NULL)
260 goto out_shutdown_r;
261 goto out_wakeup;
262 }
263 /* splice not possible (anymore), let's go on on standard copy */
264 }
265#endif
Willy Tarreau8a7af602008-05-03 23:07:14 +0200266 cur_read = 0;
Willy Tarreau6996e152007-04-30 14:37:43 +0200267 while (1) {
268 /*
269 * 1. compute the maximum block size we can read at once.
270 */
Willy Tarreau03d60bb2009-01-09 11:13:00 +0100271 if (b->l == 0) {
272 /* let's realign the buffer to optimize I/O */
273 b->r = b->w = b->lr = b->data;
274 max = b->max_len;
Willy Tarreau83749182007-04-15 20:56:27 +0200275 }
276 else if (b->r > b->w) {
Willy Tarreau03d60bb2009-01-09 11:13:00 +0100277 max = b->data + b->max_len - b->r;
Willy Tarreau83749182007-04-15 20:56:27 +0200278 }
279 else {
280 max = b->w - b->r;
Willy Tarreau03d60bb2009-01-09 11:13:00 +0100281 if (max > b->max_len)
282 max = b->max_len;
Willy Tarreau83749182007-04-15 20:56:27 +0200283 }
Willy Tarreau74ab2ac2008-11-23 17:23:07 +0100284
Willy Tarreau9c0fe592009-01-18 16:25:31 +0100285 if (max == 0) {
286 b->flags |= BF_FULL;
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100287 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau9c0fe592009-01-18 16:25:31 +0100288 break;
289 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200290
Willy Tarreau6996e152007-04-30 14:37:43 +0200291 /*
292 * 2. read the largest possible block
293 */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200294#ifndef MSG_NOSIGNAL
Willy Tarreau83749182007-04-15 20:56:27 +0200295 {
296 int skerr;
297 socklen_t lskerr = sizeof(skerr);
298
299 ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr);
300 if (ret == -1 || skerr)
301 ret = -1;
302 else
303 ret = recv(fd, b->r, max, 0);
304 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200305#else
Willy Tarreau83749182007-04-15 20:56:27 +0200306 ret = recv(fd, b->r, max, MSG_NOSIGNAL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200307#endif
Willy Tarreau83749182007-04-15 20:56:27 +0200308 if (ret > 0) {
309 b->r += ret;
310 b->l += ret;
Willy Tarreau8a7af602008-05-03 23:07:14 +0200311 cur_read += ret;
Willy Tarreaub38903c2008-11-23 21:33:29 +0100312
Willy Tarreau0abebcc2009-01-08 00:09:41 +0100313 /* if we're allowed to directly forward data, we must update send_max */
314 if (b->to_forward > 0) {
315 int fwd = MIN(b->to_forward, ret);
316 b->send_max += fwd;
317 b->to_forward -= fwd;
318 }
Willy Tarreauf890dc92008-12-13 21:12:26 +0100319
Willy Tarreaub38903c2008-11-23 21:33:29 +0100320 if (fdtab[fd].state == FD_STCONN)
321 fdtab[fd].state = FD_STREADY;
322
Willy Tarreau3da77c52008-08-29 09:58:42 +0200323 b->flags |= BF_READ_PARTIAL;
Willy Tarreaue393fe22008-08-16 22:18:07 +0200324 b->flags &= ~BF_EMPTY;
Willy Tarreau74ab2ac2008-11-23 17:23:07 +0100325
Willy Tarreau83749182007-04-15 20:56:27 +0200326 if (b->r == b->data + BUFSIZE) {
327 b->r = b->data; /* wrap around the buffer */
328 }
Willy Tarreau9641e8f2007-03-23 23:02:09 +0100329
Willy Tarreau83749182007-04-15 20:56:27 +0200330 b->total += ret;
Willy Tarreau9641e8f2007-03-23 23:02:09 +0100331
Willy Tarreau03d60bb2009-01-09 11:13:00 +0100332 if (b->l >= b->max_len) {
Willy Tarreau6996e152007-04-30 14:37:43 +0200333 /* The buffer is now full, there's no point in going through
334 * the loop again.
335 */
Willy Tarreau8a7af602008-05-03 23:07:14 +0200336 if (!(b->flags & BF_STREAMER_FAST) && (cur_read == b->l)) {
337 b->xfer_small = 0;
338 b->xfer_large++;
339 if (b->xfer_large >= 3) {
340 /* we call this buffer a fast streamer if it manages
341 * to be filled in one call 3 consecutive times.
342 */
343 b->flags |= (BF_STREAMER | BF_STREAMER_FAST);
344 //fputc('+', stderr);
345 }
346 }
347 else if ((b->flags & (BF_STREAMER | BF_STREAMER_FAST)) &&
348 (cur_read <= BUFSIZE / 2)) {
349 b->xfer_large = 0;
350 b->xfer_small++;
351 if (b->xfer_small >= 2) {
352 /* if the buffer has been at least half full twice,
353 * we receive faster than we send, so at least it
354 * is not a "fast streamer".
355 */
356 b->flags &= ~BF_STREAMER_FAST;
357 //fputc('-', stderr);
358 }
359 }
360 else {
361 b->xfer_small = 0;
362 b->xfer_large = 0;
363 }
Willy Tarreau9c0fe592009-01-18 16:25:31 +0100364
365 b->flags |= BF_FULL;
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100366 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau9c0fe592009-01-18 16:25:31 +0100367 break;
Willy Tarreau6996e152007-04-30 14:37:43 +0200368 }
369
Willy Tarreauab3e1d32007-06-03 14:10:36 +0200370 /* if too many bytes were missing from last read, it means that
371 * it's pointless trying to read again because the system does
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100372 * not have them in buffers. BTW, if FD_POLL_HUP was present,
373 * it means that we have reached the end and that the connection
374 * is closed.
Willy Tarreauab3e1d32007-06-03 14:10:36 +0200375 */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100376 if (ret < max) {
Willy Tarreau8a7af602008-05-03 23:07:14 +0200377 if ((b->flags & (BF_STREAMER | BF_STREAMER_FAST)) &&
378 (cur_read <= BUFSIZE / 2)) {
379 b->xfer_large = 0;
380 b->xfer_small++;
381 if (b->xfer_small >= 3) {
382 /* we have read less than half of the buffer in
383 * one pass, and this happened at least 3 times.
384 * This is definitely not a streamer.
385 */
386 b->flags &= ~(BF_STREAMER | BF_STREAMER_FAST);
387 //fputc('!', stderr);
388 }
389 }
Willy Tarreau2bea3a12008-08-28 09:47:43 +0200390 /* unfortunately, on level-triggered events, POLL_HUP
391 * is generally delivered AFTER the system buffer is
392 * empty, so this one might never match.
393 */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100394 if (fdtab[fd].ev & FD_POLL_HUP)
395 goto out_shutdown_r;
Willy Tarreau2bea3a12008-08-28 09:47:43 +0200396
397 /* if a streamer has read few data, it may be because we
398 * have exhausted system buffers. It's not worth trying
399 * again.
400 */
401 if (b->flags & BF_STREAMER)
402 break;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100403 }
Willy Tarreauab3e1d32007-06-03 14:10:36 +0200404
405 /* generally if we read something smaller than 1 or 2 MSS,
Willy Tarreau2bea3a12008-08-28 09:47:43 +0200406 * it means that either we have exhausted the system's
407 * buffers (streamer or question-response protocol) or that
408 * the connection will be closed. Streamers are easily
409 * detected so we return early. For other cases, it's still
410 * better to perform a last read to be sure, because it may
411 * save one complete poll/read/wakeup cycle in case of shutdown.
Willy Tarreau83749182007-04-15 20:56:27 +0200412 */
Willy Tarreau2bea3a12008-08-28 09:47:43 +0200413 if (ret < MIN_RET_FOR_READ_LOOP && b->flags & BF_STREAMER)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200414 break;
Willy Tarreau83749182007-04-15 20:56:27 +0200415
Willy Tarreau6996e152007-04-30 14:37:43 +0200416 if (--read_poll <= 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200417 break;
Willy Tarreau83749182007-04-15 20:56:27 +0200418 }
419 else if (ret == 0) {
Willy Tarreau6996e152007-04-30 14:37:43 +0200420 /* connection closed */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100421 goto out_shutdown_r;
Willy Tarreau83749182007-04-15 20:56:27 +0200422 }
Willy Tarreau9f195292007-04-15 21:26:58 +0200423 else if (errno == EAGAIN) {
424 /* Ignore EAGAIN but inform the poller that there is
Willy Tarreauaf78d0f2009-01-08 10:09:08 +0100425 * nothing to read left if we did not read much, ie
426 * less than what we were still expecting to read.
427 * But we may have done some work justifying to notify
428 * the task.
Willy Tarreau9f195292007-04-15 21:26:58 +0200429 */
Willy Tarreauaf78d0f2009-01-08 10:09:08 +0100430 if (cur_read < MIN_RET_FOR_READ_LOOP)
431 retval = 0;
Willy Tarreau83749182007-04-15 20:56:27 +0200432 break;
433 }
434 else {
Willy Tarreau6996e152007-04-30 14:37:43 +0200435 goto out_error;
Willy Tarreau83749182007-04-15 20:56:27 +0200436 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200437 } /* while (1) */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200438
Willy Tarreau6996e152007-04-30 14:37:43 +0200439 out_wakeup:
Willy Tarreau9c0fe592009-01-18 16:25:31 +0100440 /* We might have some data the consumer is waiting for */
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100441 if ((b->send_max || b->pipe) && (b->cons->flags & SI_FL_WAIT_DATA)) {
442 int last_len = b->pipe ? b->pipe->data : 0;
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100443
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100444 b->cons->chk_snd(b->cons);
445
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100446 /* check if the consumer has freed some space */
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100447 if (!(b->flags & BF_FULL) &&
448 (!last_len || !b->pipe || b->pipe->data < last_len))
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100449 si->flags &= ~SI_FL_WAIT_ROOM;
450 }
451
452 if (si->flags & SI_FL_WAIT_ROOM) {
Willy Tarreau9c0fe592009-01-18 16:25:31 +0100453 EV_FD_CLR(fd, DIR_RD);
454 b->rex = TICK_ETERNITY;
455 }
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100456 else if ((b->flags & (BF_READ_PARTIAL|BF_FULL|BF_READ_NOEXP)) == BF_READ_PARTIAL)
457 b->rex = tick_add_ifset(now_ms, b->rto);
Willy Tarreau9c0fe592009-01-18 16:25:31 +0100458
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100459 /* we have to wake up if there is a special event or if we don't have
460 * any more data to forward.
461 */
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100462 if ((b->flags & (BF_READ_NULL|BF_READ_ERROR|BF_SHUTR)) ||
463 !b->to_forward ||
464 si->state != SI_ST_EST ||
465 b->cons->state != SI_ST_EST ||
466 (si->flags & SI_FL_ERR))
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100467 task_wakeup(si->owner, TASK_WOKEN_IO);
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100468
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100469 fdtab[fd].ev &= ~FD_POLL_IN;
Willy Tarreau83749182007-04-15 20:56:27 +0200470 return retval;
Willy Tarreau6996e152007-04-30 14:37:43 +0200471
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100472 out_shutdown_r:
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200473 /* we received a shutdown */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100474 fdtab[fd].ev &= ~FD_POLL_HUP;
475 b->flags |= BF_READ_NULL;
Willy Tarreau99126c32008-11-27 10:30:51 +0100476 stream_sock_shutr(si);
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200477 goto out_wakeup;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100478
Willy Tarreau6996e152007-04-30 14:37:43 +0200479 out_error:
Willy Tarreaucff64112008-11-03 06:26:53 +0100480 /* Read error on the file descriptor. We mark the FD as STERROR so
481 * that we don't use it anymore. The error is reported to the stream
482 * interface which will take proper action. We must not perturbate the
483 * buffer because the stream interface wants to ensure transparent
484 * connection retries.
Willy Tarreau6996e152007-04-30 14:37:43 +0200485 */
Willy Tarreaucff64112008-11-03 06:26:53 +0100486
Willy Tarreau6996e152007-04-30 14:37:43 +0200487 fdtab[fd].state = FD_STERROR;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100488 fdtab[fd].ev &= ~FD_POLL_STICKY;
Willy Tarreaucff64112008-11-03 06:26:53 +0100489 si->flags |= SI_FL_ERR;
Willy Tarreau9c0fe592009-01-18 16:25:31 +0100490 retval = 1;
491 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200492}
493
494
495/*
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100496 * This function is called to send buffer data to a stream socket.
497 * It returns -1 in case of unrecoverable error, 0 if the caller needs to poll
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100498 * before calling it again, otherwise 1. If a pipe was associated with the
499 * buffer and it empties it, it releases it as well.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200500 */
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100501static int stream_sock_write_loop(struct stream_interface *si, struct buffer *b)
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100502{
Willy Tarreau83749182007-04-15 20:56:27 +0200503 int write_poll = MAX_WRITE_POLL_LOOPS;
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100504 int retval = 1;
505 int ret, max;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200506
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100507#if defined(CONFIG_HAP_LINUX_SPLICE)
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100508 while (b->pipe) {
509 ret = splice(b->pipe->cons, NULL, si->fd, NULL, b->pipe->data,
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100510 SPLICE_F_MOVE|SPLICE_F_NONBLOCK);
511 if (ret <= 0) {
512 if (ret == 0 || errno == EAGAIN) {
513 retval = 0;
514 return retval;
515 }
516 /* here we have another error */
517 retval = -1;
518 return retval;
519 }
520
521 b->flags |= BF_WRITE_PARTIAL;
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100522 b->pipe->data -= ret;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100523
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100524 if (!b->pipe->data) {
525 put_pipe(b->pipe);
526 b->pipe = NULL;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100527 break;
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100528 }
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100529
530 if (--write_poll <= 0)
531 return retval;
532 }
533
534 /* At this point, the pipe is empty, but we may still have data pending
535 * in the normal buffer.
536 */
537 if (!b->l) {
538 b->flags |= BF_EMPTY;
539 return retval;
540 }
541#endif
Willy Tarreaud2def0f2009-01-18 17:37:33 +0100542 if (!b->send_max)
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100543 return retval;
Willy Tarreau83749182007-04-15 20:56:27 +0200544
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100545 /* when we're in this loop, we already know that there is no spliced
546 * data left, and that there are sendable buffered data.
547 */
Willy Tarreau6996e152007-04-30 14:37:43 +0200548 while (1) {
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100549 if (b->r > b->w)
Willy Tarreau83749182007-04-15 20:56:27 +0200550 max = b->r - b->w;
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100551 else
Willy Tarreau83749182007-04-15 20:56:27 +0200552 max = b->data + BUFSIZE - b->w;
Willy Tarreau83749182007-04-15 20:56:27 +0200553
Willy Tarreauf890dc92008-12-13 21:12:26 +0100554 /* limit the amount of outgoing data if required */
555 if (max > b->send_max)
556 max = b->send_max;
557
Willy Tarreaubaaee002006-06-26 02:48:02 +0200558#ifndef MSG_NOSIGNAL
559 {
560 int skerr;
561 socklen_t lskerr = sizeof(skerr);
562
Willy Tarreau87bed622009-03-08 22:25:28 +0100563 ret = getsockopt(si->fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr);
Willy Tarreauc6423482006-10-15 14:59:03 +0200564 if (ret == -1 || skerr)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200565 ret = -1;
566 else
Willy Tarreau87bed622009-03-08 22:25:28 +0100567 ret = send(si->fd, b->w, max, MSG_DONTWAIT);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200568 }
569#else
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100570 ret = send(si->fd, b->w, max, MSG_DONTWAIT | MSG_NOSIGNAL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200571#endif
572
573 if (ret > 0) {
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100574 if (fdtab[si->fd].state == FD_STCONN)
575 fdtab[si->fd].state = FD_STREADY;
Willy Tarreaub38903c2008-11-23 21:33:29 +0100576
Willy Tarreau3da77c52008-08-29 09:58:42 +0200577 b->flags |= BF_WRITE_PARTIAL;
Willy Tarreaue393fe22008-08-16 22:18:07 +0200578
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100579 b->w += ret;
580 if (b->w == b->data + BUFSIZE)
581 b->w = b->data; /* wrap around the buffer */
582
583 b->l -= ret;
584 if (likely(b->l < b->max_len))
Willy Tarreaue393fe22008-08-16 22:18:07 +0200585 b->flags &= ~BF_FULL;
Willy Tarreau74ab2ac2008-11-23 17:23:07 +0100586
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100587 if (likely(!b->l)) {
588 /* optimize data alignment in the buffer */
589 b->r = b->w = b->lr = b->data;
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100590 if (likely(!b->pipe))
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100591 b->flags |= BF_EMPTY;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200592 }
Willy Tarreau83749182007-04-15 20:56:27 +0200593
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100594 b->send_max -= ret;
595 if (!b->send_max || !b->l)
596 break;
Willy Tarreau83749182007-04-15 20:56:27 +0200597
Willy Tarreauab3e1d32007-06-03 14:10:36 +0200598 /* if the system buffer is full, don't insist */
599 if (ret < max)
600 break;
601
Willy Tarreau6996e152007-04-30 14:37:43 +0200602 if (--write_poll <= 0)
603 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200604 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200605 else if (ret == 0 || errno == EAGAIN) {
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100606 /* nothing written, we need to poll for write first */
Willy Tarreau83749182007-04-15 20:56:27 +0200607 retval = 0;
608 break;
609 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200610 else {
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100611 /* bad, we got an error */
612 retval = -1;
613 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200614 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200615 } /* while (1) */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200616
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100617 return retval;
618}
Willy Tarreau6996e152007-04-30 14:37:43 +0200619
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100620
621/*
622 * This function is called on a write event from a stream socket.
623 * It returns 0 if the caller needs to poll before calling it again, otherwise
624 * non-zero.
625 */
626int stream_sock_write(int fd)
627{
628 struct stream_interface *si = fdtab[fd].owner;
629 struct buffer *b = si->ob;
630 int retval = 1;
631
632#ifdef DEBUG_FULL
633 fprintf(stderr,"stream_sock_write : fd=%d, owner=%p\n", fd, fdtab[fd].owner);
634#endif
635
636 retval = 1;
637 if (fdtab[fd].state == FD_STERROR || (fdtab[fd].ev & FD_POLL_ERR))
638 goto out_error;
639
640 if (likely(!(b->flags & BF_EMPTY))) {
641 /* OK there are data waiting to be sent */
642 retval = stream_sock_write_loop(si, b);
643 if (retval < 0)
644 goto out_error;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200645 }
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100646 else {
647 /* may be we have received a connection acknowledgement in TCP mode without data */
648 if (likely(fdtab[fd].state == FD_STCONN)) {
649 /* We have no data to send to check the connection, and
650 * getsockopt() will not inform us whether the connection
651 * is still pending. So we'll reuse connect() to check the
652 * state of the socket. This has the advantage of givig us
653 * the following info :
654 * - error
655 * - connecting (EALREADY, EINPROGRESS)
656 * - connected (EISCONN, 0)
657 */
658 if ((connect(fd, fdtab[fd].peeraddr, fdtab[fd].peerlen) == 0))
659 errno = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200660
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100661 if (errno == EALREADY || errno == EINPROGRESS) {
662 retval = 0;
663 goto out_may_wakeup;
664 }
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100665
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100666 if (errno && errno != EISCONN)
667 goto out_error;
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200668
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100669 /* OK we just need to indicate that we got a connection
670 * and that we wrote nothing.
671 */
672 b->flags |= BF_WRITE_NULL;
673 fdtab[fd].state = FD_STREADY;
674 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200675
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100676 /* Funny, we were called to write something but there wasn't
677 * anything. We can get there, for example if we were woken up
678 * on a write event to finish the splice, but the send_max is 0
679 * so we cannot write anything from the buffer. Let's disable
680 * the write event and pretend we never came there.
681 */
682 }
683
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100684 if (!b->pipe && !b->send_max) {
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100685 /* the connection is established but we can't write. Either the
686 * buffer is empty, or we just refrain from sending because the
687 * send_max limit was reached. Maybe we just wrote the last
688 * chunk and need to close.
689 */
690 if (((b->flags & (BF_SHUTW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) ==
691 (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR)) &&
692 (si->state == SI_ST_EST)) {
693 stream_sock_shutw(si);
694 goto out_wakeup;
695 }
696
697 if (b->flags & BF_EMPTY)
Willy Tarreauac128fe2009-01-09 13:05:19 +0100698 si->flags |= SI_FL_WAIT_DATA;
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100699
Willy Tarreauac128fe2009-01-09 13:05:19 +0100700 EV_FD_CLR(fd, DIR_WR);
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100701 b->wex = TICK_ETERNITY;
Willy Tarreauac128fe2009-01-09 13:05:19 +0100702 }
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100703
704 out_may_wakeup:
705 if (b->flags & BF_WRITE_ACTIVITY) {
706 /* update timeout if we have written something */
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100707 if ((b->send_max || b->pipe) &&
Willy Tarreaud2def0f2009-01-18 17:37:33 +0100708 (b->flags & (BF_SHUTW|BF_WRITE_PARTIAL)) == BF_WRITE_PARTIAL)
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100709 b->wex = tick_add_ifset(now_ms, b->wto);
710
711 out_wakeup:
712 if (tick_isset(si->ib->rex)) {
713 /* Note: to prevent the client from expiring read timeouts
714 * during writes, we refresh it. A better solution would be
715 * to merge read+write timeouts into a unique one, although
716 * that needs some study particularly on full-duplex TCP
717 * connections.
718 */
719 si->ib->rex = tick_add_ifset(now_ms, si->ib->rto);
720 }
721
722 /* the producer might be waiting for more room to store data */
723 if (likely((b->flags & (BF_WRITE_PARTIAL|BF_FULL)) == BF_WRITE_PARTIAL &&
724 (b->prod->flags & SI_FL_WAIT_ROOM)))
725 b->prod->chk_rcv(b->prod);
726
727 /* we have to wake up if there is a special event or if we don't have
728 * any more data to forward and it's not planned to send any more.
729 */
730 if (likely((b->flags & (BF_WRITE_NULL|BF_WRITE_ERROR|BF_SHUTW)) ||
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100731 (!b->to_forward && !b->send_max && !b->pipe) ||
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100732 si->state != SI_ST_EST ||
733 b->prod->state != SI_ST_EST))
734 task_wakeup(si->owner, TASK_WOKEN_IO);
735 }
736
737 fdtab[fd].ev &= ~FD_POLL_OUT;
738 return retval;
Willy Tarreauac128fe2009-01-09 13:05:19 +0100739
Willy Tarreau6996e152007-04-30 14:37:43 +0200740 out_error:
Willy Tarreaucff64112008-11-03 06:26:53 +0100741 /* Write error on the file descriptor. We mark the FD as STERROR so
742 * that we don't use it anymore. The error is reported to the stream
743 * interface which will take proper action. We must not perturbate the
744 * buffer because the stream interface wants to ensure transparent
745 * connection retries.
Willy Tarreau6996e152007-04-30 14:37:43 +0200746 */
Willy Tarreaucff64112008-11-03 06:26:53 +0100747
Willy Tarreau6996e152007-04-30 14:37:43 +0200748 fdtab[fd].state = FD_STERROR;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100749 fdtab[fd].ev &= ~FD_POLL_STICKY;
Willy Tarreaucff64112008-11-03 06:26:53 +0100750 si->flags |= SI_FL_ERR;
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200751 task_wakeup(si->owner, TASK_WOKEN_IO);
752 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200753}
754
Willy Tarreau48adac52008-08-30 04:58:38 +0200755/*
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200756 * This function performs a shutdown-write on a stream interface in a connected or
757 * init state (it does nothing for other states). It either shuts the write side
Willy Tarreau99126c32008-11-27 10:30:51 +0100758 * or closes the file descriptor and marks itself as closed. The buffer flags are
759 * updated to reflect the new state.
Willy Tarreau48adac52008-08-30 04:58:38 +0200760 */
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100761void stream_sock_shutw(struct stream_interface *si)
Willy Tarreau48adac52008-08-30 04:58:38 +0200762{
Willy Tarreau99126c32008-11-27 10:30:51 +0100763 if (si->ob->flags & BF_SHUTW)
764 return;
765 si->ob->flags |= BF_SHUTW;
766 si->ob->wex = TICK_ETERNITY;
Willy Tarreaub0ef7352008-12-14 13:26:20 +0100767 si->flags &= ~SI_FL_WAIT_DATA;
Willy Tarreau99126c32008-11-27 10:30:51 +0100768
Willy Tarreaub38903c2008-11-23 21:33:29 +0100769 switch (si->state) {
Willy Tarreaub38903c2008-11-23 21:33:29 +0100770 case SI_ST_EST:
771 if (!(si->ib->flags & BF_SHUTR)) {
772 EV_FD_CLR(si->fd, DIR_WR);
773 shutdown(si->fd, SHUT_WR);
774 return;
775 }
776 /* fall through */
777 case SI_ST_CON:
Willy Tarreaub0ef7352008-12-14 13:26:20 +0100778 si->flags &= ~SI_FL_WAIT_ROOM;
Willy Tarreau8bfa4262008-11-27 09:25:45 +0100779 /* we may have to close a pending connection, and mark the
780 * response buffer as shutr
781 */
Willy Tarreau48adac52008-08-30 04:58:38 +0200782 fd_delete(si->fd);
Willy Tarreaufe3718a2008-11-30 18:14:12 +0100783 /* fall through */
784 case SI_ST_CER:
Willy Tarreau7f006512008-12-07 14:04:04 +0100785 si->state = SI_ST_DIS;
786 default:
Willy Tarreau99126c32008-11-27 10:30:51 +0100787 si->ib->flags |= BF_SHUTR;
Willy Tarreaufe3718a2008-11-30 18:14:12 +0100788 si->ib->rex = TICK_ETERNITY;
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100789 return;
Willy Tarreau48adac52008-08-30 04:58:38 +0200790 }
Willy Tarreau48adac52008-08-30 04:58:38 +0200791}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200792
Willy Tarreau2d212792008-08-27 21:41:35 +0200793/*
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200794 * This function performs a shutdown-read on a stream interface in a connected or
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100795 * init state (it does nothing for other states). It either shuts the read side
Willy Tarreau99126c32008-11-27 10:30:51 +0100796 * or closes the file descriptor and marks itself as closed. The buffer flags are
797 * updated to reflect the new state.
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200798 */
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100799void stream_sock_shutr(struct stream_interface *si)
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200800{
Willy Tarreau99126c32008-11-27 10:30:51 +0100801 if (si->ib->flags & BF_SHUTR)
802 return;
803 si->ib->flags |= BF_SHUTR;
804 si->ib->rex = TICK_ETERNITY;
Willy Tarreaub0ef7352008-12-14 13:26:20 +0100805 si->flags &= ~SI_FL_WAIT_ROOM;
Willy Tarreau99126c32008-11-27 10:30:51 +0100806
Willy Tarreau8bfa4262008-11-27 09:25:45 +0100807 if (si->state != SI_ST_EST && si->state != SI_ST_CON)
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100808 return;
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200809
Willy Tarreaucff64112008-11-03 06:26:53 +0100810 if (si->ob->flags & BF_SHUTW) {
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200811 fd_delete(si->fd);
Willy Tarreau74ab2ac2008-11-23 17:23:07 +0100812 si->state = SI_ST_DIS;
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100813 return;
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200814 }
815 EV_FD_CLR(si->fd, DIR_RD);
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100816 return;
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200817}
818
819/*
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200820 * Updates a connected stream_sock file descriptor status and timeouts
821 * according to the buffers' flags. It should only be called once after the
822 * buffer flags have settled down, and before they are cleared. It doesn't
823 * harm to call it as often as desired (it just slightly hurts performance).
824 */
Willy Tarreaub0253252008-11-30 21:37:12 +0100825void stream_sock_data_finish(struct stream_interface *si)
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200826{
Willy Tarreaub0253252008-11-30 21:37:12 +0100827 struct buffer *ib = si->ib;
828 struct buffer *ob = si->ob;
829 int fd = si->fd;
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200830
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200831 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 +0200832 now_ms, __FUNCTION__,
833 fd, fdtab[fd].owner,
834 ib, ob,
835 ib->rex, ob->wex,
836 ib->flags, ob->flags,
Willy Tarreaub0253252008-11-30 21:37:12 +0100837 ib->l, ob->l, si->state);
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200838
839 /* Check if we need to close the read side */
840 if (!(ib->flags & BF_SHUTR)) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200841 /* Read not closed, update FD status and timeout for reads */
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200842 if (ib->flags & (BF_FULL|BF_HIJACK)) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200843 /* stop reading */
Willy Tarreaub0ef7352008-12-14 13:26:20 +0100844 if ((ib->flags & (BF_FULL|BF_HIJACK)) == BF_FULL)
845 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau2d212792008-08-27 21:41:35 +0200846 EV_FD_COND_C(fd, DIR_RD);
847 ib->rex = TICK_ETERNITY;
848 }
849 else {
850 /* (re)start reading and update timeout. Note: we don't recompute the timeout
851 * everytime we get here, otherwise it would risk never to expire. We only
852 * update it if is was not yet set, or if we already got some read status.
853 */
Willy Tarreaub0ef7352008-12-14 13:26:20 +0100854 si->flags &= ~SI_FL_WAIT_ROOM;
Willy Tarreau2d212792008-08-27 21:41:35 +0200855 EV_FD_COND_S(fd, DIR_RD);
Willy Tarreau86491c32008-12-14 09:04:47 +0100856 if (!(ib->flags & BF_READ_NOEXP) &&
857 (!tick_isset(ib->rex) || ib->flags & BF_READ_ACTIVITY))
Willy Tarreau2d212792008-08-27 21:41:35 +0200858 ib->rex = tick_add_ifset(now_ms, ib->rto);
859 }
860 }
861
862 /* Check if we need to close the write side */
863 if (!(ob->flags & BF_SHUTW)) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200864 /* Write not closed, update FD status and timeout for writes */
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100865 if ((ob->send_max == 0 && !ob->pipe) ||
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100866 (ob->flags & BF_EMPTY) ||
Willy Tarreau3da77c52008-08-29 09:58:42 +0200867 (ob->flags & (BF_HIJACK|BF_WRITE_ENA)) == 0) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200868 /* stop writing */
Willy Tarreaub0ef7352008-12-14 13:26:20 +0100869 if ((ob->flags & (BF_EMPTY|BF_HIJACK|BF_WRITE_ENA)) == (BF_EMPTY|BF_WRITE_ENA))
870 si->flags |= SI_FL_WAIT_DATA;
Willy Tarreau2d212792008-08-27 21:41:35 +0200871 EV_FD_COND_C(fd, DIR_WR);
872 ob->wex = TICK_ETERNITY;
873 }
874 else {
875 /* (re)start writing and update timeout. Note: we don't recompute the timeout
876 * everytime we get here, otherwise it would risk never to expire. We only
877 * update it if is was not yet set, or if we already got some write status.
878 */
Willy Tarreaub0ef7352008-12-14 13:26:20 +0100879 si->flags &= ~SI_FL_WAIT_DATA;
Willy Tarreau2d212792008-08-27 21:41:35 +0200880 EV_FD_COND_S(fd, DIR_WR);
Willy Tarreau3da77c52008-08-29 09:58:42 +0200881 if (!tick_isset(ob->wex) || ob->flags & BF_WRITE_ACTIVITY) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200882 ob->wex = tick_add_ifset(now_ms, ob->wto);
Willy Tarreau21e1be82008-08-29 11:30:14 +0200883 if (tick_isset(ob->wex) && tick_isset(ib->rex)) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200884 /* Note: depending on the protocol, we don't know if we're waiting
885 * for incoming data or not. So in order to prevent the socket from
886 * expiring read timeouts during writes, we refresh the read timeout,
887 * except if it was already infinite.
888 */
889 ib->rex = ob->wex;
890 }
891 }
892 }
893 }
Willy Tarreau2d212792008-08-27 21:41:35 +0200894}
895
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100896/* This function is used for inter-stream-interface calls. It is called by the
897 * consumer to inform the producer side that it may be interested in checking
898 * for free space in the buffer. Note that it intentionally does not update
899 * timeouts, so that we can still check them later at wake-up.
900 */
901void stream_sock_chk_rcv(struct stream_interface *si)
902{
903 struct buffer *ib = si->ib;
904
905 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",
906 now_ms, __FUNCTION__,
Vincenzo Farruggia9b97cff2009-01-30 16:49:10 +0000907 si->fd, fdtab[si->fd].owner,
908 ib, si->ob,
909 ib->rex, si->ob->wex,
910 ib->flags, si->ob->flags,
911 ib->l, si->ob->l, si->state);
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100912
913 if (unlikely(si->state != SI_ST_EST || (ib->flags & BF_SHUTR)))
914 return;
915
916 if (ib->flags & (BF_FULL|BF_HIJACK)) {
917 /* stop reading */
918 if ((ib->flags & (BF_FULL|BF_HIJACK)) == BF_FULL)
919 si->flags |= SI_FL_WAIT_ROOM;
920 EV_FD_COND_C(si->fd, DIR_RD);
921 }
922 else {
923 /* (re)start reading */
924 si->flags &= ~SI_FL_WAIT_ROOM;
925 EV_FD_COND_S(si->fd, DIR_RD);
926 }
927}
928
929
930/* This function is used for inter-stream-interface calls. It is called by the
931 * producer to inform the consumer side that it may be interested in checking
932 * for data in the buffer. Note that it intentionally does not update timeouts,
933 * so that we can still check them later at wake-up.
934 */
935void stream_sock_chk_snd(struct stream_interface *si)
936{
937 struct buffer *ob = si->ob;
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100938 int retval;
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100939
940 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",
941 now_ms, __FUNCTION__,
Vincenzo Farruggia9b97cff2009-01-30 16:49:10 +0000942 si->fd, fdtab[si->fd].owner,
943 si->ib, ob,
944 si->ib->rex, ob->wex,
945 si->ib->flags, ob->flags,
946 si->ib->l, ob->l, si->state);
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100947
948 if (unlikely(si->state != SI_ST_EST || (ob->flags & BF_SHUTW)))
949 return;
950
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100951 if (!(si->flags & SI_FL_WAIT_DATA) || /* not waiting for data */
952 (fdtab[si->fd].ev & FD_POLL_OUT) || /* we'll be called anyway */
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100953 !(ob->send_max || ob->pipe) || /* called with nothing to send ! */
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100954 !(ob->flags & (BF_HIJACK|BF_WRITE_ENA))) /* we may not write */
955 return;
956
957 retval = stream_sock_write_loop(si, ob);
958 if (retval < 0) {
959 /* Write error on the file descriptor. We mark the FD as STERROR so
960 * that we don't use it anymore and we notify the task.
961 */
962 fdtab[si->fd].state = FD_STERROR;
963 fdtab[si->fd].ev &= ~FD_POLL_STICKY;
964 si->flags |= SI_FL_ERR;
965 goto out_wakeup;
966 }
967
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100968 if (retval > 0 || (ob->send_max == 0 && !ob->pipe)) {
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100969 /* the connection is established but we can't write. Either the
970 * buffer is empty, or we just refrain from sending because the
971 * send_max limit was reached. Maybe we just wrote the last
972 * chunk and need to close.
973 */
974 if (((ob->flags & (BF_SHUTW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) ==
975 (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR)) &&
976 (si->state == SI_ST_EST)) {
977 stream_sock_shutw(si);
978 goto out_wakeup;
979 }
980
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100981 if ((ob->flags & (BF_EMPTY|BF_HIJACK|BF_WRITE_ENA)) == (BF_EMPTY|BF_WRITE_ENA))
982 si->flags |= SI_FL_WAIT_DATA;
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100983 ob->wex = TICK_ETERNITY;
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100984 }
985 else {
986 /* (re)start writing. */
987 si->flags &= ~SI_FL_WAIT_DATA;
988 EV_FD_COND_S(si->fd, DIR_WR);
989 }
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100990
Willy Tarreauc9619462009-03-09 22:40:57 +0100991 if (likely(ob->flags & BF_WRITE_ACTIVITY)) {
992 /* update timeout if we have written something */
993 if ((ob->send_max || ob->pipe) &&
994 (ob->flags & (BF_SHUTW|BF_WRITE_PARTIAL)) == BF_WRITE_PARTIAL)
995 ob->wex = tick_add_ifset(now_ms, ob->wto);
996
997 if (tick_isset(si->ib->rex)) {
998 /* Note: to prevent the client from expiring read timeouts
999 * during writes, we refresh it. A better solution would be
1000 * to merge read+write timeouts into a unique one, although
1001 * that needs some study particularly on full-duplex TCP
1002 * connections.
1003 */
1004 si->ib->rex = tick_add_ifset(now_ms, si->ib->rto);
1005 }
1006 }
1007
Willy Tarreaua456f2a2009-01-18 17:38:44 +01001008 /* in case of special condition (error, shutdown, end of write...), we
1009 * have to notify the task.
1010 */
1011 if (likely((ob->flags & (BF_WRITE_NULL|BF_WRITE_ERROR|BF_SHUTW)) ||
Willy Tarreau3eba98a2009-01-25 13:56:13 +01001012 (!ob->to_forward && !ob->send_max && !ob->pipe) ||
Willy Tarreaua456f2a2009-01-18 17:38:44 +01001013 si->state != SI_ST_EST)) {
1014 out_wakeup:
1015 task_wakeup(si->owner, TASK_WOKEN_IO);
1016 }
Willy Tarreau3ffeba12008-12-14 14:42:35 +01001017}
1018
Willy Tarreaubaaee002006-06-26 02:48:02 +02001019
1020/*
1021 * Local variables:
1022 * c-indent-level: 8
1023 * c-basic-offset: 8
1024 * End:
1025 */