blob: 14a8df272076ecf1deddd4d729f8525b5c011126 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Functions operating on SOCK_STREAM and buffers.
3 *
Willy Tarreau0c303ee2008-07-07 00:09:58 +02004 * Copyright 2000-2008 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
13#include <errno.h>
14#include <fcntl.h>
15#include <stdio.h>
16#include <stdlib.h>
17
18#include <sys/socket.h>
19#include <sys/stat.h>
20#include <sys/types.h>
21
Willy Tarreau2dd0d472006-06-29 17:53:05 +020022#include <common/compat.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020023#include <common/config.h>
Willy Tarreaud6f087e2008-01-18 17:20:13 +010024#include <common/debug.h>
Willy Tarreau83749182007-04-15 20:56:27 +020025#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020026#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020027#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020028
Willy Tarreau2d212792008-08-27 21:41:35 +020029#include <proto/buffers.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020030#include <proto/client.h>
31#include <proto/fd.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020032#include <proto/stream_sock.h>
33#include <proto/task.h>
34
35
36/*
Willy Tarreaud7971282006-07-29 18:36:34 +020037 * this function is called on a read event from a stream socket.
Willy Tarreau83749182007-04-15 20:56:27 +020038 * It returns 0 if we have a high confidence that we will not be
39 * able to read more data without polling first. Returns non-zero
40 * otherwise.
Willy Tarreaubaaee002006-06-26 02:48:02 +020041 */
Willy Tarreaud7971282006-07-29 18:36:34 +020042int stream_sock_read(int fd) {
Willy Tarreau0c303ee2008-07-07 00:09:58 +020043 __label__ out_wakeup, out_shutdown_r, out_error;
Willy Tarreau54469402006-07-29 16:59:06 +020044 struct buffer *b = fdtab[fd].cb[DIR_RD].b;
Willy Tarreaue5ed4062008-08-30 03:17:31 +020045 struct stream_interface *si = fdtab[fd].owner;
Willy Tarreau8a7af602008-05-03 23:07:14 +020046 int ret, max, retval, cur_read;
Willy Tarreaub8949f12007-03-23 22:39:59 +010047 int read_poll = MAX_READ_POLL_LOOPS;
Willy Tarreaubaaee002006-06-26 02:48:02 +020048
49#ifdef DEBUG_FULL
Willy Tarreaud6f087e2008-01-18 17:20:13 +010050 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 +020051#endif
52
Willy Tarreau83749182007-04-15 20:56:27 +020053 retval = 1;
54
Willy Tarreaud6f087e2008-01-18 17:20:13 +010055 /* stop immediately on errors */
56 if (fdtab[fd].state == FD_STERROR || (fdtab[fd].ev & FD_POLL_ERR))
Willy Tarreau6996e152007-04-30 14:37:43 +020057 goto out_error;
Willy Tarreaud6f087e2008-01-18 17:20:13 +010058
59 /* stop here if we reached the end of data */
60 if ((fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP)) == FD_POLL_HUP)
61 goto out_shutdown_r;
Willy Tarreau83749182007-04-15 20:56:27 +020062
Willy Tarreau8a7af602008-05-03 23:07:14 +020063 cur_read = 0;
Willy Tarreau6996e152007-04-30 14:37:43 +020064 while (1) {
65 /*
66 * 1. compute the maximum block size we can read at once.
67 */
Willy Tarreau83749182007-04-15 20:56:27 +020068 if (b->l == 0) { /* let's realign the buffer to optimize I/O */
69 b->r = b->w = b->lr = b->data;
70 max = b->rlim - b->data;
71 }
72 else if (b->r > b->w) {
73 max = b->rlim - b->r;
74 }
75 else {
76 max = b->w - b->r;
77 /* FIXME: theorically, if w>0, we shouldn't have rlim < data+size anymore
78 * since it means that the rewrite protection has been removed. This
79 * implies that the if statement can be removed.
80 */
81 if (max > b->rlim - b->data)
Willy Tarreaubaaee002006-06-26 02:48:02 +020082 max = b->rlim - b->data;
Willy Tarreau83749182007-04-15 20:56:27 +020083 }
Willy Tarreaubaaee002006-06-26 02:48:02 +020084
Willy Tarreau6996e152007-04-30 14:37:43 +020085 if (unlikely(max == 0)) {
86 /* Not anymore room to store data. This should theorically
87 * never happen, but better safe than sorry !
88 */
Willy Tarreaue393fe22008-08-16 22:18:07 +020089 b->flags |= BF_FULL;
Willy Tarreau83749182007-04-15 20:56:27 +020090 EV_FD_CLR(fd, DIR_RD);
Willy Tarreau0c303ee2008-07-07 00:09:58 +020091 b->rex = TICK_ETERNITY;
92 goto out_wakeup;
Willy Tarreau83749182007-04-15 20:56:27 +020093 }
Willy Tarreaubaaee002006-06-26 02:48:02 +020094
Willy Tarreau6996e152007-04-30 14:37:43 +020095 /*
96 * 2. read the largest possible block
97 */
Willy Tarreaubaaee002006-06-26 02:48:02 +020098#ifndef MSG_NOSIGNAL
Willy Tarreau83749182007-04-15 20:56:27 +020099 {
100 int skerr;
101 socklen_t lskerr = sizeof(skerr);
102
103 ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr);
104 if (ret == -1 || skerr)
105 ret = -1;
106 else
107 ret = recv(fd, b->r, max, 0);
108 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200109#else
Willy Tarreau83749182007-04-15 20:56:27 +0200110 ret = recv(fd, b->r, max, MSG_NOSIGNAL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200111#endif
Willy Tarreau83749182007-04-15 20:56:27 +0200112 if (ret > 0) {
113 b->r += ret;
114 b->l += ret;
Willy Tarreau8a7af602008-05-03 23:07:14 +0200115 cur_read += ret;
Willy Tarreau3da77c52008-08-29 09:58:42 +0200116 b->flags |= BF_READ_PARTIAL;
Willy Tarreaue393fe22008-08-16 22:18:07 +0200117 b->flags &= ~BF_EMPTY;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200118
Willy Tarreau83749182007-04-15 20:56:27 +0200119 if (b->r == b->data + BUFSIZE) {
120 b->r = b->data; /* wrap around the buffer */
121 }
Willy Tarreau9641e8f2007-03-23 23:02:09 +0100122
Willy Tarreau83749182007-04-15 20:56:27 +0200123 b->total += ret;
Willy Tarreau9641e8f2007-03-23 23:02:09 +0100124
Willy Tarreaue393fe22008-08-16 22:18:07 +0200125 if (b->l >= b->rlim - b->data) {
Willy Tarreau6996e152007-04-30 14:37:43 +0200126 /* The buffer is now full, there's no point in going through
127 * the loop again.
128 */
Willy Tarreau8a7af602008-05-03 23:07:14 +0200129 if (!(b->flags & BF_STREAMER_FAST) && (cur_read == b->l)) {
130 b->xfer_small = 0;
131 b->xfer_large++;
132 if (b->xfer_large >= 3) {
133 /* we call this buffer a fast streamer if it manages
134 * to be filled in one call 3 consecutive times.
135 */
136 b->flags |= (BF_STREAMER | BF_STREAMER_FAST);
137 //fputc('+', stderr);
138 }
139 }
140 else if ((b->flags & (BF_STREAMER | BF_STREAMER_FAST)) &&
141 (cur_read <= BUFSIZE / 2)) {
142 b->xfer_large = 0;
143 b->xfer_small++;
144 if (b->xfer_small >= 2) {
145 /* if the buffer has been at least half full twice,
146 * we receive faster than we send, so at least it
147 * is not a "fast streamer".
148 */
149 b->flags &= ~BF_STREAMER_FAST;
150 //fputc('-', stderr);
151 }
152 }
153 else {
154 b->xfer_small = 0;
155 b->xfer_large = 0;
156 }
157
Willy Tarreaue393fe22008-08-16 22:18:07 +0200158 b->flags |= BF_FULL;
Willy Tarreau6996e152007-04-30 14:37:43 +0200159 EV_FD_CLR(fd, DIR_RD);
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200160 b->rex = TICK_ETERNITY;
161 goto out_wakeup;
Willy Tarreau6996e152007-04-30 14:37:43 +0200162 }
163
Willy Tarreauab3e1d32007-06-03 14:10:36 +0200164 /* if too many bytes were missing from last read, it means that
165 * it's pointless trying to read again because the system does
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100166 * not have them in buffers. BTW, if FD_POLL_HUP was present,
167 * it means that we have reached the end and that the connection
168 * is closed.
Willy Tarreauab3e1d32007-06-03 14:10:36 +0200169 */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100170 if (ret < max) {
Willy Tarreau8a7af602008-05-03 23:07:14 +0200171 if ((b->flags & (BF_STREAMER | BF_STREAMER_FAST)) &&
172 (cur_read <= BUFSIZE / 2)) {
173 b->xfer_large = 0;
174 b->xfer_small++;
175 if (b->xfer_small >= 3) {
176 /* we have read less than half of the buffer in
177 * one pass, and this happened at least 3 times.
178 * This is definitely not a streamer.
179 */
180 b->flags &= ~(BF_STREAMER | BF_STREAMER_FAST);
181 //fputc('!', stderr);
182 }
183 }
Willy Tarreau2bea3a12008-08-28 09:47:43 +0200184 /* unfortunately, on level-triggered events, POLL_HUP
185 * is generally delivered AFTER the system buffer is
186 * empty, so this one might never match.
187 */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100188 if (fdtab[fd].ev & FD_POLL_HUP)
189 goto out_shutdown_r;
Willy Tarreau2bea3a12008-08-28 09:47:43 +0200190
191 /* if a streamer has read few data, it may be because we
192 * have exhausted system buffers. It's not worth trying
193 * again.
194 */
195 if (b->flags & BF_STREAMER)
196 break;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100197 }
Willy Tarreauab3e1d32007-06-03 14:10:36 +0200198
199 /* generally if we read something smaller than 1 or 2 MSS,
Willy Tarreau2bea3a12008-08-28 09:47:43 +0200200 * it means that either we have exhausted the system's
201 * buffers (streamer or question-response protocol) or that
202 * the connection will be closed. Streamers are easily
203 * detected so we return early. For other cases, it's still
204 * better to perform a last read to be sure, because it may
205 * save one complete poll/read/wakeup cycle in case of shutdown.
Willy Tarreau83749182007-04-15 20:56:27 +0200206 */
Willy Tarreau2bea3a12008-08-28 09:47:43 +0200207 if (ret < MIN_RET_FOR_READ_LOOP && b->flags & BF_STREAMER)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200208 break;
Willy Tarreau83749182007-04-15 20:56:27 +0200209
Willy Tarreau6996e152007-04-30 14:37:43 +0200210 if (--read_poll <= 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200211 break;
Willy Tarreau83749182007-04-15 20:56:27 +0200212 }
213 else if (ret == 0) {
Willy Tarreau6996e152007-04-30 14:37:43 +0200214 /* connection closed */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100215 goto out_shutdown_r;
Willy Tarreau83749182007-04-15 20:56:27 +0200216 }
Willy Tarreau9f195292007-04-15 21:26:58 +0200217 else if (errno == EAGAIN) {
218 /* Ignore EAGAIN but inform the poller that there is
Willy Tarreau6996e152007-04-30 14:37:43 +0200219 * nothing to read left. But we may have done some work
220 * justifying to notify the task.
Willy Tarreau9f195292007-04-15 21:26:58 +0200221 */
Willy Tarreau83749182007-04-15 20:56:27 +0200222 retval = 0;
223 break;
224 }
225 else {
Willy Tarreau6996e152007-04-30 14:37:43 +0200226 goto out_error;
Willy Tarreau83749182007-04-15 20:56:27 +0200227 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200228 } /* while (1) */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200229
Willy Tarreau6996e152007-04-30 14:37:43 +0200230 /*
231 * The only way to get out of this loop is to have stopped reading
232 * without any error nor close, either by limiting the number of
233 * loops, or because of an EAGAIN. We only rearm the timer if we
234 * have at least read something.
235 */
236
Willy Tarreau3da77c52008-08-29 09:58:42 +0200237 if (tick_isset(b->rex) && b->flags & BF_READ_PARTIAL)
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200238 b->rex = tick_add_ifset(now_ms, b->rto);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200239
Willy Tarreau3da77c52008-08-29 09:58:42 +0200240 if (!(b->flags & BF_READ_ACTIVITY))
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200241 goto out_skip_wakeup;
Willy Tarreau6996e152007-04-30 14:37:43 +0200242 out_wakeup:
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200243 task_wakeup(si->owner, TASK_WOKEN_IO);
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200244
245 out_skip_wakeup:
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100246 fdtab[fd].ev &= ~FD_POLL_IN;
Willy Tarreau83749182007-04-15 20:56:27 +0200247 return retval;
Willy Tarreau6996e152007-04-30 14:37:43 +0200248
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100249 out_shutdown_r:
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200250 /* we received a shutdown */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100251 fdtab[fd].ev &= ~FD_POLL_HUP;
252 b->flags |= BF_READ_NULL;
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200253 buffer_shutr(b);
254 /* Maybe we have to completely close the socket */
255 if (fdtab[fd].cb[DIR_WR].b->flags & BF_SHUTW)
256 goto do_close_and_return;
257 EV_FD_CLR(fd, DIR_RD);
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200258 goto out_wakeup;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100259
Willy Tarreau6996e152007-04-30 14:37:43 +0200260 out_error:
261 /* There was an error. we must wakeup the task. No need to clear
262 * the events, the task will do it.
263 */
264 fdtab[fd].state = FD_STERROR;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100265 fdtab[fd].ev &= ~FD_POLL_STICKY;
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200266 b->rex = TICK_ETERNITY;
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200267
268 /* Read error on the file descriptor. We close the FD and set
269 * the error on both buffers.
270 * Note: right now we only support connected sockets.
271 */
272 if (si->state != SI_ST_EST)
273 goto out_wakeup;
274
275 if (!si->err_type)
276 si->err_type = SI_ET_DATA_ERR;
277
278 buffer_shutr(fdtab[fd].cb[DIR_RD].b);
279 fdtab[fd].cb[DIR_RD].b->flags |= BF_READ_ERROR;
280 buffer_shutw(fdtab[fd].cb[DIR_WR].b);
281 fdtab[fd].cb[DIR_WR].b->flags |= BF_WRITE_ERROR;
282
283 do_close_and_return:
284 fd_delete(fd);
285 si->state = SI_ST_CLO;
286 task_wakeup(si->owner, TASK_WOKEN_IO);
287 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200288}
289
290
291/*
Willy Tarreauf8306d52006-07-29 19:01:31 +0200292 * this function is called on a write event from a stream socket.
Willy Tarreau83749182007-04-15 20:56:27 +0200293 * It returns 0 if we have a high confidence that we will not be
294 * able to write more data without polling first. Returns non-zero
295 * otherwise.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200296 */
Willy Tarreauf8306d52006-07-29 19:01:31 +0200297int stream_sock_write(int fd) {
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200298 __label__ out_wakeup, out_error;
Willy Tarreau54469402006-07-29 16:59:06 +0200299 struct buffer *b = fdtab[fd].cb[DIR_WR].b;
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200300 struct stream_interface *si = fdtab[fd].owner;
Willy Tarreau83749182007-04-15 20:56:27 +0200301 int ret, max, retval;
302 int write_poll = MAX_WRITE_POLL_LOOPS;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200303
304#ifdef DEBUG_FULL
Willy Tarreauf8306d52006-07-29 19:01:31 +0200305 fprintf(stderr,"stream_sock_write : fd=%d, owner=%p\n", fd, fdtab[fd].owner);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200306#endif
307
Willy Tarreau83749182007-04-15 20:56:27 +0200308 retval = 1;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100309 if (fdtab[fd].state == FD_STERROR || (fdtab[fd].ev & FD_POLL_ERR))
Willy Tarreau6996e152007-04-30 14:37:43 +0200310 goto out_error;
Willy Tarreau83749182007-04-15 20:56:27 +0200311
Willy Tarreau6996e152007-04-30 14:37:43 +0200312 while (1) {
Willy Tarreau83749182007-04-15 20:56:27 +0200313 if (b->l == 0) { /* let's realign the buffer to optimize I/O */
314 b->r = b->w = b->lr = b->data;
315 max = 0;
316 }
317 else if (b->r > b->w) {
318 max = b->r - b->w;
319 }
320 else {
321 max = b->data + BUFSIZE - b->w;
322 }
323
Willy Tarreaubaaee002006-06-26 02:48:02 +0200324 if (max == 0) {
Willy Tarreauf8306d52006-07-29 19:01:31 +0200325 /* may be we have received a connection acknowledgement in TCP mode without data */
Willy Tarreau6996e152007-04-30 14:37:43 +0200326 if (likely(fdtab[fd].state == FD_STCONN)) {
Willy Tarreau6996e152007-04-30 14:37:43 +0200327 /* We have no data to send to check the connection, and
328 * getsockopt() will not inform us whether the connection
329 * is still pending. So we'll reuse connect() to check the
330 * state of the socket. This has the advantage of givig us
331 * the following info :
332 * - error
333 * - connecting (EALREADY, EINPROGRESS)
334 * - connected (EISCONN, 0)
335 */
Willy Tarreaue94ebd02007-10-09 17:14:37 +0200336 if ((connect(fd, fdtab[fd].peeraddr, fdtab[fd].peerlen) == 0))
Willy Tarreau6996e152007-04-30 14:37:43 +0200337 errno = 0;
338
339 if (errno == EALREADY || errno == EINPROGRESS) {
340 retval = 0;
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200341 goto out_may_wakeup;
Willy Tarreauf8306d52006-07-29 19:01:31 +0200342 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200343
344 if (errno && errno != EISCONN)
345 goto out_error;
346
347 /* OK we just need to indicate that we got a connection
348 * and that we wrote nothing.
349 */
350 b->flags |= BF_WRITE_NULL;
351 fdtab[fd].state = FD_STREADY;
Willy Tarreauf8306d52006-07-29 19:01:31 +0200352 }
353
Willy Tarreau6996e152007-04-30 14:37:43 +0200354 /* Funny, we were called to write something but there wasn't
355 * anything. Theorically we cannot get there, but just in case,
356 * let's disable the write event and pretend we never came there.
357 */
Willy Tarreauf161a342007-04-08 16:59:42 +0200358 EV_FD_CLR(fd, DIR_WR);
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200359 b->wex = TICK_ETERNITY;
360 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200361 }
362
363#ifndef MSG_NOSIGNAL
364 {
365 int skerr;
366 socklen_t lskerr = sizeof(skerr);
367
Willy Tarreauc6423482006-10-15 14:59:03 +0200368 ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr);
369 if (ret == -1 || skerr)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200370 ret = -1;
371 else
372 ret = send(fd, b->w, max, MSG_DONTWAIT);
373 }
374#else
375 ret = send(fd, b->w, max, MSG_DONTWAIT | MSG_NOSIGNAL);
376#endif
377
378 if (ret > 0) {
379 b->l -= ret;
380 b->w += ret;
381
Willy Tarreau3da77c52008-08-29 09:58:42 +0200382 b->flags |= BF_WRITE_PARTIAL;
Willy Tarreaue393fe22008-08-16 22:18:07 +0200383
384 if (b->l < b->rlim - b->data)
385 b->flags &= ~BF_FULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200386
387 if (b->w == b->data + BUFSIZE) {
388 b->w = b->data; /* wrap around the buffer */
389 }
Willy Tarreau83749182007-04-15 20:56:27 +0200390
Willy Tarreau6996e152007-04-30 14:37:43 +0200391 if (!b->l) {
Willy Tarreaue393fe22008-08-16 22:18:07 +0200392 b->flags |= BF_EMPTY;
Willy Tarreau6996e152007-04-30 14:37:43 +0200393 EV_FD_CLR(fd, DIR_WR);
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200394 b->wex = TICK_ETERNITY;
395 goto out_wakeup;
Willy Tarreau6996e152007-04-30 14:37:43 +0200396 }
Willy Tarreau83749182007-04-15 20:56:27 +0200397
Willy Tarreauab3e1d32007-06-03 14:10:36 +0200398 /* if the system buffer is full, don't insist */
399 if (ret < max)
400 break;
401
Willy Tarreau6996e152007-04-30 14:37:43 +0200402 if (--write_poll <= 0)
403 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200404 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200405 else if (ret == 0 || errno == EAGAIN) {
406 /* nothing written, just pretend we were never called
407 * and wait for the socket to be ready. But we may have
408 * done some work justifying to notify the task.
409 */
Willy Tarreau83749182007-04-15 20:56:27 +0200410 retval = 0;
411 break;
412 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200413 else {
Willy Tarreau6996e152007-04-30 14:37:43 +0200414 goto out_error;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200415 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200416 } /* while (1) */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200417
Willy Tarreau6996e152007-04-30 14:37:43 +0200418 /*
419 * The only way to get out of this loop is to have stopped writing
420 * without any error, either by limiting the number of loops, or
421 * because of an EAGAIN. We only rearm the timer if we have at least
422 * written something.
423 */
424
Willy Tarreau3da77c52008-08-29 09:58:42 +0200425 if (tick_isset(b->wex) && b->flags & BF_WRITE_PARTIAL) {
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200426 b->wex = tick_add_ifset(now_ms, b->wto);
Willy Tarreauadfb8562008-08-11 15:24:42 +0200427 if (tick_isset(b->wex)) {
Willy Tarreau83749182007-04-15 20:56:27 +0200428 /* FIXME: to prevent the client from expiring read timeouts during writes,
429 * we refresh it. A solution would be to merge read+write timeouts into a
430 * unique one, although that needs some study particularly on full-duplex
431 * TCP connections. */
Willy Tarreauba392ce2008-08-16 21:13:23 +0200432 if (tick_isset(b->rex) && !(b->flags & BF_SHUTR))
Willy Tarreaufa645582007-06-03 15:59:52 +0200433 b->rex = b->wex;
Willy Tarreau83749182007-04-15 20:56:27 +0200434 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200435 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200436
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200437 out_may_wakeup:
Willy Tarreau3da77c52008-08-29 09:58:42 +0200438 if (!(b->flags & BF_WRITE_ACTIVITY))
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200439 goto out_skip_wakeup;
Willy Tarreau6996e152007-04-30 14:37:43 +0200440 out_wakeup:
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200441 task_wakeup(si->owner, TASK_WOKEN_IO);
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200442
443 out_skip_wakeup:
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100444 fdtab[fd].ev &= ~FD_POLL_OUT;
Willy Tarreau83749182007-04-15 20:56:27 +0200445 return retval;
Willy Tarreau6996e152007-04-30 14:37:43 +0200446
447 out_error:
448 /* There was an error. we must wakeup the task. No need to clear
449 * the events, the task will do it.
450 */
451 fdtab[fd].state = FD_STERROR;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100452 fdtab[fd].ev &= ~FD_POLL_STICKY;
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200453 b->wex = TICK_ETERNITY;
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200454 /* Read error on the file descriptor. We close the FD and set
455 * the error on both buffers.
456 * Note: right now we only support connected sockets.
457 */
458 if (si->state != SI_ST_EST)
459 goto out_wakeup;
460
461 if (!si->err_type)
462 si->err_type = SI_ET_DATA_ERR;
463
464 buffer_shutr(fdtab[fd].cb[DIR_RD].b);
465 fdtab[fd].cb[DIR_RD].b->flags |= BF_READ_ERROR;
466 buffer_shutw(fdtab[fd].cb[DIR_WR].b);
467 fdtab[fd].cb[DIR_WR].b->flags |= BF_WRITE_ERROR;
468
469 fd_delete(fd);
470 si->state = SI_ST_CLO;
471 task_wakeup(si->owner, TASK_WOKEN_IO);
472 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200473}
474
Willy Tarreaubaaee002006-06-26 02:48:02 +0200475
Willy Tarreau2d212792008-08-27 21:41:35 +0200476/*
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200477 * This function only has to be called once after a wakeup event during a data
478 * phase. It controls the file descriptor's status, as well as read and write
479 * timeouts.
Willy Tarreau2d212792008-08-27 21:41:35 +0200480 */
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200481int stream_sock_data_check_timeouts(int fd)
Willy Tarreau2d212792008-08-27 21:41:35 +0200482{
483 struct buffer *ib = fdtab[fd].cb[DIR_RD].b;
484 struct buffer *ob = fdtab[fd].cb[DIR_WR].b;
485
486 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\n",
487 now_ms, __FUNCTION__,
488 fd, fdtab[fd].owner,
489 ib, ob,
490 ib->rex, ob->wex,
491 ib->flags, ob->flags,
492 ib->l, ob->l);
493
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200494 /* Read timeout */
495 if (unlikely(!(ib->flags & (BF_SHUTR|BF_READ_TIMEOUT)) && tick_is_expired(ib->rex, now_ms))) {
496 //trace_term(t, TT_HTTP_SRV_12);
497 ib->flags |= BF_READ_TIMEOUT;
498 if (!ob->cons->err_type) {
499 //ob->cons->err_loc = t->srv;
500 ob->cons->err_type = SI_ET_DATA_TO;
501 }
502 buffer_shutr(ib);
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200503 if (ob->flags & BF_SHUTW) {
504 do_close_and_return:
505 fd_delete(fd);
506 ob->cons->state = SI_ST_CLO;
507 return 0;
508 }
509
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200510 EV_FD_CLR(fd, DIR_RD);
511 }
512
513 /* Write timeout */
514 if (unlikely(!(ob->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) && tick_is_expired(ob->wex, now_ms))) {
515 //trace_term(t, TT_HTTP_SRV_13);
516 ob->flags |= BF_WRITE_TIMEOUT;
517 if (!ob->cons->err_type) {
518 //ob->cons->err_loc = t->srv;
519 ob->cons->err_type = SI_ET_DATA_TO;
520 }
521 buffer_shutw(ob);
522 if (ib->flags & BF_SHUTR)
523 goto do_close_and_return;
524
525 EV_FD_CLR(fd, DIR_WR);
526 shutdown(fd, SHUT_WR);
527 }
528 return 0;
529}
530
531/*
532 * Manages a stream_sock connection during its data phase. The buffers are
533 * examined for various cases of shutdown, then file descriptor and buffers'
534 * flags are updated accordingly.
535 */
536int stream_sock_data_update(int fd)
537{
538 struct buffer *ib = fdtab[fd].cb[DIR_RD].b;
539 struct buffer *ob = fdtab[fd].cb[DIR_WR].b;
540
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200541 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 +0200542 now_ms, __FUNCTION__,
543 fd, fdtab[fd].owner,
544 ib, ob,
545 ib->rex, ob->wex,
546 ib->flags, ob->flags,
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200547 ib->l, ob->l, ob->cons->state);
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200548
Willy Tarreau2d212792008-08-27 21:41:35 +0200549 /* Check if we need to close the read side */
550 if (!(ib->flags & BF_SHUTR)) {
551 /* Last read, forced read-shutdown, or other end closed */
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200552 if (ib->flags & (BF_SHUTR_NOW|BF_SHUTW)) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200553 //trace_term(t, TT_HTTP_SRV_10);
Willy Tarreau2d212792008-08-27 21:41:35 +0200554 buffer_shutr(ib);
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200555 if (ob->flags & BF_SHUTW) {
556 fd_delete(fd);
557 ob->cons->state = SI_ST_CLO;
558 return 0;
559 }
Willy Tarreau2d212792008-08-27 21:41:35 +0200560 EV_FD_CLR(fd, DIR_RD);
561 }
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200562 }
563
564 /* Check if we need to close the write side */
565 if (!(ob->flags & BF_SHUTW)) {
566 /* Forced write-shutdown or other end closed with empty buffer. */
567 if ((ob->flags & BF_SHUTW_NOW) ||
Willy Tarreau3da77c52008-08-29 09:58:42 +0200568 (ob->flags & (BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) == (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR)) {
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200569 //trace_term(t, TT_HTTP_SRV_11);
570 buffer_shutw(ob);
571 if (ib->flags & BF_SHUTR) {
572 fd_delete(fd);
573 ob->cons->state = SI_ST_CLO;
574 return 0;
Willy Tarreau2d212792008-08-27 21:41:35 +0200575 }
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200576 EV_FD_CLR(fd, DIR_WR);
577 shutdown(fd, SHUT_WR);
Willy Tarreau2d212792008-08-27 21:41:35 +0200578 }
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200579 }
580 return 0; /* other cases change nothing */
581}
582
583
584/*
585 * Updates a connected stream_sock file descriptor status and timeouts
586 * according to the buffers' flags. It should only be called once after the
587 * buffer flags have settled down, and before they are cleared. It doesn't
588 * harm to call it as often as desired (it just slightly hurts performance).
589 */
590int stream_sock_data_finish(int fd)
591{
592 struct buffer *ib = fdtab[fd].cb[DIR_RD].b;
593 struct buffer *ob = fdtab[fd].cb[DIR_WR].b;
594
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200595 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 +0200596 now_ms, __FUNCTION__,
597 fd, fdtab[fd].owner,
598 ib, ob,
599 ib->rex, ob->wex,
600 ib->flags, ob->flags,
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200601 ib->l, ob->l, ob->cons->state);
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200602
603 /* Check if we need to close the read side */
604 if (!(ib->flags & BF_SHUTR)) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200605 /* Read not closed, update FD status and timeout for reads */
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200606 if (ib->flags & (BF_FULL|BF_HIJACK)) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200607 /* stop reading */
608 EV_FD_COND_C(fd, DIR_RD);
609 ib->rex = TICK_ETERNITY;
610 }
611 else {
612 /* (re)start reading and update timeout. Note: we don't recompute the timeout
613 * everytime we get here, otherwise it would risk never to expire. We only
614 * update it if is was not yet set, or if we already got some read status.
615 */
616 EV_FD_COND_S(fd, DIR_RD);
Willy Tarreau3da77c52008-08-29 09:58:42 +0200617 if (!tick_isset(ib->rex) || ib->flags & BF_READ_ACTIVITY)
Willy Tarreau2d212792008-08-27 21:41:35 +0200618 ib->rex = tick_add_ifset(now_ms, ib->rto);
619 }
620 }
621
622 /* Check if we need to close the write side */
623 if (!(ob->flags & BF_SHUTW)) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200624 /* Write not closed, update FD status and timeout for writes */
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200625 if ((ob->flags & BF_EMPTY) ||
Willy Tarreau3da77c52008-08-29 09:58:42 +0200626 (ob->flags & (BF_HIJACK|BF_WRITE_ENA)) == 0) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200627 /* stop writing */
628 EV_FD_COND_C(fd, DIR_WR);
629 ob->wex = TICK_ETERNITY;
630 }
631 else {
632 /* (re)start writing and update timeout. Note: we don't recompute the timeout
633 * everytime we get here, otherwise it would risk never to expire. We only
634 * update it if is was not yet set, or if we already got some write status.
635 */
636 EV_FD_COND_S(fd, DIR_WR);
Willy Tarreau3da77c52008-08-29 09:58:42 +0200637 if (!tick_isset(ob->wex) || ob->flags & BF_WRITE_ACTIVITY) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200638 ob->wex = tick_add_ifset(now_ms, ob->wto);
Willy Tarreau21e1be82008-08-29 11:30:14 +0200639 if (tick_isset(ob->wex) && tick_isset(ib->rex)) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200640 /* Note: depending on the protocol, we don't know if we're waiting
641 * for incoming data or not. So in order to prevent the socket from
642 * expiring read timeouts during writes, we refresh the read timeout,
643 * except if it was already infinite.
644 */
645 ib->rex = ob->wex;
646 }
647 }
648 }
649 }
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200650 return 0;
Willy Tarreau2d212792008-08-27 21:41:35 +0200651}
652
Willy Tarreaubaaee002006-06-26 02:48:02 +0200653
654/*
655 * Local variables:
656 * c-indent-level: 8
657 * c-basic-offset: 8
658 * End:
659 */