blob: da2004d45e6ca17121b85cce02ea3c293046f9da [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 Tarreaubaaee002006-06-26 02:48:02 +020029#include <proto/client.h>
30#include <proto/fd.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020031#include <proto/stream_sock.h>
32#include <proto/task.h>
33
34
35/*
Willy Tarreaud7971282006-07-29 18:36:34 +020036 * this function is called on a read event from a stream socket.
Willy Tarreau83749182007-04-15 20:56:27 +020037 * It returns 0 if we have a high confidence that we will not be
38 * able to read more data without polling first. Returns non-zero
39 * otherwise.
Willy Tarreaubaaee002006-06-26 02:48:02 +020040 */
Willy Tarreaud7971282006-07-29 18:36:34 +020041int stream_sock_read(int fd) {
Willy Tarreau0c303ee2008-07-07 00:09:58 +020042 __label__ out_wakeup, out_shutdown_r, out_error;
Willy Tarreau54469402006-07-29 16:59:06 +020043 struct buffer *b = fdtab[fd].cb[DIR_RD].b;
Willy Tarreau8a7af602008-05-03 23:07:14 +020044 int ret, max, retval, cur_read;
Willy Tarreaub8949f12007-03-23 22:39:59 +010045 int read_poll = MAX_READ_POLL_LOOPS;
Willy Tarreaubaaee002006-06-26 02:48:02 +020046
47#ifdef DEBUG_FULL
Willy Tarreaud6f087e2008-01-18 17:20:13 +010048 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 +020049#endif
50
Willy Tarreau83749182007-04-15 20:56:27 +020051 retval = 1;
52
Willy Tarreaud6f087e2008-01-18 17:20:13 +010053 /* stop immediately on errors */
54 if (fdtab[fd].state == FD_STERROR || (fdtab[fd].ev & FD_POLL_ERR))
Willy Tarreau6996e152007-04-30 14:37:43 +020055 goto out_error;
Willy Tarreaud6f087e2008-01-18 17:20:13 +010056
57 /* stop here if we reached the end of data */
58 if ((fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP)) == FD_POLL_HUP)
59 goto out_shutdown_r;
Willy Tarreau83749182007-04-15 20:56:27 +020060
Willy Tarreau8a7af602008-05-03 23:07:14 +020061 cur_read = 0;
Willy Tarreau6996e152007-04-30 14:37:43 +020062 while (1) {
63 /*
64 * 1. compute the maximum block size we can read at once.
65 */
Willy Tarreau83749182007-04-15 20:56:27 +020066 if (b->l == 0) { /* let's realign the buffer to optimize I/O */
67 b->r = b->w = b->lr = b->data;
68 max = b->rlim - b->data;
69 }
70 else if (b->r > b->w) {
71 max = b->rlim - b->r;
72 }
73 else {
74 max = b->w - b->r;
75 /* FIXME: theorically, if w>0, we shouldn't have rlim < data+size anymore
76 * since it means that the rewrite protection has been removed. This
77 * implies that the if statement can be removed.
78 */
79 if (max > b->rlim - b->data)
Willy Tarreaubaaee002006-06-26 02:48:02 +020080 max = b->rlim - b->data;
Willy Tarreau83749182007-04-15 20:56:27 +020081 }
Willy Tarreaubaaee002006-06-26 02:48:02 +020082
Willy Tarreau6996e152007-04-30 14:37:43 +020083 if (unlikely(max == 0)) {
84 /* Not anymore room to store data. This should theorically
85 * never happen, but better safe than sorry !
86 */
Willy Tarreau83749182007-04-15 20:56:27 +020087 EV_FD_CLR(fd, DIR_RD);
Willy Tarreau0c303ee2008-07-07 00:09:58 +020088 b->rex = TICK_ETERNITY;
89 goto out_wakeup;
Willy Tarreau83749182007-04-15 20:56:27 +020090 }
Willy Tarreaubaaee002006-06-26 02:48:02 +020091
Willy Tarreau6996e152007-04-30 14:37:43 +020092 /*
93 * 2. read the largest possible block
94 */
Willy Tarreaubaaee002006-06-26 02:48:02 +020095#ifndef MSG_NOSIGNAL
Willy Tarreau83749182007-04-15 20:56:27 +020096 {
97 int skerr;
98 socklen_t lskerr = sizeof(skerr);
99
100 ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr);
101 if (ret == -1 || skerr)
102 ret = -1;
103 else
104 ret = recv(fd, b->r, max, 0);
105 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200106#else
Willy Tarreau83749182007-04-15 20:56:27 +0200107 ret = recv(fd, b->r, max, MSG_NOSIGNAL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200108#endif
Willy Tarreau83749182007-04-15 20:56:27 +0200109 if (ret > 0) {
110 b->r += ret;
111 b->l += ret;
Willy Tarreau8a7af602008-05-03 23:07:14 +0200112 cur_read += ret;
Willy Tarreau83749182007-04-15 20:56:27 +0200113 b->flags |= BF_PARTIAL_READ;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200114
Willy Tarreau83749182007-04-15 20:56:27 +0200115 if (b->r == b->data + BUFSIZE) {
116 b->r = b->data; /* wrap around the buffer */
117 }
Willy Tarreau9641e8f2007-03-23 23:02:09 +0100118
Willy Tarreau83749182007-04-15 20:56:27 +0200119 b->total += ret;
Willy Tarreau9641e8f2007-03-23 23:02:09 +0100120
Willy Tarreau6996e152007-04-30 14:37:43 +0200121 if (b->l == b->rlim - b->data) {
122 /* The buffer is now full, there's no point in going through
123 * the loop again.
124 */
Willy Tarreau8a7af602008-05-03 23:07:14 +0200125 if (!(b->flags & BF_STREAMER_FAST) && (cur_read == b->l)) {
126 b->xfer_small = 0;
127 b->xfer_large++;
128 if (b->xfer_large >= 3) {
129 /* we call this buffer a fast streamer if it manages
130 * to be filled in one call 3 consecutive times.
131 */
132 b->flags |= (BF_STREAMER | BF_STREAMER_FAST);
133 //fputc('+', stderr);
134 }
135 }
136 else if ((b->flags & (BF_STREAMER | BF_STREAMER_FAST)) &&
137 (cur_read <= BUFSIZE / 2)) {
138 b->xfer_large = 0;
139 b->xfer_small++;
140 if (b->xfer_small >= 2) {
141 /* if the buffer has been at least half full twice,
142 * we receive faster than we send, so at least it
143 * is not a "fast streamer".
144 */
145 b->flags &= ~BF_STREAMER_FAST;
146 //fputc('-', stderr);
147 }
148 }
149 else {
150 b->xfer_small = 0;
151 b->xfer_large = 0;
152 }
153
Willy Tarreau6996e152007-04-30 14:37:43 +0200154 EV_FD_CLR(fd, DIR_RD);
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200155 b->rex = TICK_ETERNITY;
156 goto out_wakeup;
Willy Tarreau6996e152007-04-30 14:37:43 +0200157 }
158
Willy Tarreauab3e1d32007-06-03 14:10:36 +0200159 /* if too many bytes were missing from last read, it means that
160 * it's pointless trying to read again because the system does
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100161 * not have them in buffers. BTW, if FD_POLL_HUP was present,
162 * it means that we have reached the end and that the connection
163 * is closed.
Willy Tarreauab3e1d32007-06-03 14:10:36 +0200164 */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100165 if (ret < max) {
Willy Tarreau8a7af602008-05-03 23:07:14 +0200166 if ((b->flags & (BF_STREAMER | BF_STREAMER_FAST)) &&
167 (cur_read <= BUFSIZE / 2)) {
168 b->xfer_large = 0;
169 b->xfer_small++;
170 if (b->xfer_small >= 3) {
171 /* we have read less than half of the buffer in
172 * one pass, and this happened at least 3 times.
173 * This is definitely not a streamer.
174 */
175 b->flags &= ~(BF_STREAMER | BF_STREAMER_FAST);
176 //fputc('!', stderr);
177 }
178 }
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100179 if (fdtab[fd].ev & FD_POLL_HUP)
180 goto out_shutdown_r;
Willy Tarreauab3e1d32007-06-03 14:10:36 +0200181 break;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100182 }
Willy Tarreauab3e1d32007-06-03 14:10:36 +0200183
184 /* generally if we read something smaller than 1 or 2 MSS,
Willy Tarreau83749182007-04-15 20:56:27 +0200185 * it means that it's not worth trying to read again. It may
186 * also happen on headers, but the application then can stop
187 * reading before we start polling.
188 */
189 if (ret < MIN_RET_FOR_READ_LOOP)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200190 break;
Willy Tarreau83749182007-04-15 20:56:27 +0200191
Willy Tarreau6996e152007-04-30 14:37:43 +0200192 if (--read_poll <= 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200193 break;
Willy Tarreau83749182007-04-15 20:56:27 +0200194
Willy Tarreau83749182007-04-15 20:56:27 +0200195 }
196 else if (ret == 0) {
Willy Tarreau6996e152007-04-30 14:37:43 +0200197 /* connection closed */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100198 goto out_shutdown_r;
Willy Tarreau83749182007-04-15 20:56:27 +0200199 }
Willy Tarreau9f195292007-04-15 21:26:58 +0200200 else if (errno == EAGAIN) {
201 /* Ignore EAGAIN but inform the poller that there is
Willy Tarreau6996e152007-04-30 14:37:43 +0200202 * nothing to read left. But we may have done some work
203 * justifying to notify the task.
Willy Tarreau9f195292007-04-15 21:26:58 +0200204 */
Willy Tarreau83749182007-04-15 20:56:27 +0200205 retval = 0;
206 break;
207 }
208 else {
Willy Tarreau6996e152007-04-30 14:37:43 +0200209 goto out_error;
Willy Tarreau83749182007-04-15 20:56:27 +0200210 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200211 } /* while (1) */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200212
Willy Tarreau6996e152007-04-30 14:37:43 +0200213 /*
214 * The only way to get out of this loop is to have stopped reading
215 * without any error nor close, either by limiting the number of
216 * loops, or because of an EAGAIN. We only rearm the timer if we
217 * have at least read something.
218 */
219
Willy Tarreauadfb8562008-08-11 15:24:42 +0200220 if (tick_isset(b->rex) && b->flags & BF_PARTIAL_READ)
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200221 b->rex = tick_add_ifset(now_ms, b->rto);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200222
Willy Tarreau6996e152007-04-30 14:37:43 +0200223 out_wakeup:
224 if (b->flags & BF_READ_STATUS)
225 task_wakeup(fdtab[fd].owner);
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100226 fdtab[fd].ev &= ~FD_POLL_IN;
Willy Tarreau83749182007-04-15 20:56:27 +0200227 return retval;
Willy Tarreau6996e152007-04-30 14:37:43 +0200228
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100229 out_shutdown_r:
230 fdtab[fd].ev &= ~FD_POLL_HUP;
231 b->flags |= BF_READ_NULL;
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200232 b->rex = TICK_ETERNITY;
233 goto out_wakeup;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100234
Willy Tarreau6996e152007-04-30 14:37:43 +0200235 out_error:
236 /* There was an error. we must wakeup the task. No need to clear
237 * the events, the task will do it.
238 */
239 fdtab[fd].state = FD_STERROR;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100240 fdtab[fd].ev &= ~FD_POLL_STICKY;
Willy Tarreau6996e152007-04-30 14:37:43 +0200241 b->flags |= BF_READ_ERROR;
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200242 b->rex = TICK_ETERNITY;
243 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200244}
245
246
247/*
Willy Tarreauf8306d52006-07-29 19:01:31 +0200248 * this function is called on a write event from a stream socket.
Willy Tarreau83749182007-04-15 20:56:27 +0200249 * It returns 0 if we have a high confidence that we will not be
250 * able to write more data without polling first. Returns non-zero
251 * otherwise.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200252 */
Willy Tarreauf8306d52006-07-29 19:01:31 +0200253int stream_sock_write(int fd) {
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200254 __label__ out_wakeup, out_error;
Willy Tarreau54469402006-07-29 16:59:06 +0200255 struct buffer *b = fdtab[fd].cb[DIR_WR].b;
Willy Tarreau83749182007-04-15 20:56:27 +0200256 int ret, max, retval;
257 int write_poll = MAX_WRITE_POLL_LOOPS;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200258
259#ifdef DEBUG_FULL
Willy Tarreauf8306d52006-07-29 19:01:31 +0200260 fprintf(stderr,"stream_sock_write : fd=%d, owner=%p\n", fd, fdtab[fd].owner);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200261#endif
262
Willy Tarreau83749182007-04-15 20:56:27 +0200263 retval = 1;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100264 if (fdtab[fd].state == FD_STERROR || (fdtab[fd].ev & FD_POLL_ERR))
Willy Tarreau6996e152007-04-30 14:37:43 +0200265 goto out_error;
Willy Tarreau83749182007-04-15 20:56:27 +0200266
Willy Tarreau6996e152007-04-30 14:37:43 +0200267 while (1) {
Willy Tarreau83749182007-04-15 20:56:27 +0200268 if (b->l == 0) { /* let's realign the buffer to optimize I/O */
269 b->r = b->w = b->lr = b->data;
270 max = 0;
271 }
272 else if (b->r > b->w) {
273 max = b->r - b->w;
274 }
275 else {
276 max = b->data + BUFSIZE - b->w;
277 }
278
Willy Tarreaubaaee002006-06-26 02:48:02 +0200279 if (max == 0) {
Willy Tarreauf8306d52006-07-29 19:01:31 +0200280 /* may be we have received a connection acknowledgement in TCP mode without data */
Willy Tarreau6996e152007-04-30 14:37:43 +0200281 if (likely(fdtab[fd].state == FD_STCONN)) {
Willy Tarreau6996e152007-04-30 14:37:43 +0200282 /* We have no data to send to check the connection, and
283 * getsockopt() will not inform us whether the connection
284 * is still pending. So we'll reuse connect() to check the
285 * state of the socket. This has the advantage of givig us
286 * the following info :
287 * - error
288 * - connecting (EALREADY, EINPROGRESS)
289 * - connected (EISCONN, 0)
290 */
Willy Tarreaue94ebd02007-10-09 17:14:37 +0200291 if ((connect(fd, fdtab[fd].peeraddr, fdtab[fd].peerlen) == 0))
Willy Tarreau6996e152007-04-30 14:37:43 +0200292 errno = 0;
293
294 if (errno == EALREADY || errno == EINPROGRESS) {
295 retval = 0;
296 goto out_wakeup;
Willy Tarreauf8306d52006-07-29 19:01:31 +0200297 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200298
299 if (errno && errno != EISCONN)
300 goto out_error;
301
302 /* OK we just need to indicate that we got a connection
303 * and that we wrote nothing.
304 */
305 b->flags |= BF_WRITE_NULL;
306 fdtab[fd].state = FD_STREADY;
Willy Tarreauf8306d52006-07-29 19:01:31 +0200307 }
308
Willy Tarreau6996e152007-04-30 14:37:43 +0200309 /* Funny, we were called to write something but there wasn't
310 * anything. Theorically we cannot get there, but just in case,
311 * let's disable the write event and pretend we never came there.
312 */
Willy Tarreauf161a342007-04-08 16:59:42 +0200313 EV_FD_CLR(fd, DIR_WR);
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200314 b->wex = TICK_ETERNITY;
315 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200316 }
317
318#ifndef MSG_NOSIGNAL
319 {
320 int skerr;
321 socklen_t lskerr = sizeof(skerr);
322
Willy Tarreauc6423482006-10-15 14:59:03 +0200323 ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr);
324 if (ret == -1 || skerr)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200325 ret = -1;
326 else
327 ret = send(fd, b->w, max, MSG_DONTWAIT);
328 }
329#else
330 ret = send(fd, b->w, max, MSG_DONTWAIT | MSG_NOSIGNAL);
331#endif
332
333 if (ret > 0) {
334 b->l -= ret;
335 b->w += ret;
336
Willy Tarreau0f9f5052006-07-29 17:39:25 +0200337 b->flags |= BF_PARTIAL_WRITE;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200338
339 if (b->w == b->data + BUFSIZE) {
340 b->w = b->data; /* wrap around the buffer */
341 }
Willy Tarreau83749182007-04-15 20:56:27 +0200342
Willy Tarreau6996e152007-04-30 14:37:43 +0200343 if (!b->l) {
344 EV_FD_CLR(fd, DIR_WR);
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200345 b->wex = TICK_ETERNITY;
346 goto out_wakeup;
Willy Tarreau6996e152007-04-30 14:37:43 +0200347 }
Willy Tarreau83749182007-04-15 20:56:27 +0200348
Willy Tarreauab3e1d32007-06-03 14:10:36 +0200349 /* if the system buffer is full, don't insist */
350 if (ret < max)
351 break;
352
Willy Tarreau6996e152007-04-30 14:37:43 +0200353 if (--write_poll <= 0)
354 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200355 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200356 else if (ret == 0 || errno == EAGAIN) {
357 /* nothing written, just pretend we were never called
358 * and wait for the socket to be ready. But we may have
359 * done some work justifying to notify the task.
360 */
Willy Tarreau83749182007-04-15 20:56:27 +0200361 retval = 0;
362 break;
363 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200364 else {
Willy Tarreau6996e152007-04-30 14:37:43 +0200365 goto out_error;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200366 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200367 } /* while (1) */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200368
Willy Tarreau6996e152007-04-30 14:37:43 +0200369 /*
370 * The only way to get out of this loop is to have stopped writing
371 * without any error, either by limiting the number of loops, or
372 * because of an EAGAIN. We only rearm the timer if we have at least
373 * written something.
374 */
375
Willy Tarreauadfb8562008-08-11 15:24:42 +0200376 if (tick_isset(b->wex) && b->flags & BF_PARTIAL_WRITE) {
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200377 b->wex = tick_add_ifset(now_ms, b->wto);
Willy Tarreauadfb8562008-08-11 15:24:42 +0200378 if (tick_isset(b->wex)) {
Willy Tarreau83749182007-04-15 20:56:27 +0200379 /* FIXME: to prevent the client from expiring read timeouts during writes,
380 * we refresh it. A solution would be to merge read+write timeouts into a
381 * unique one, although that needs some study particularly on full-duplex
382 * TCP connections. */
Willy Tarreauadfb8562008-08-11 15:24:42 +0200383 if (tick_isset(b->rex) && !(b->flags & BF_SHUTR_STATUS))
Willy Tarreaufa645582007-06-03 15:59:52 +0200384 b->rex = b->wex;
Willy Tarreau83749182007-04-15 20:56:27 +0200385 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200386 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200387
Willy Tarreau6996e152007-04-30 14:37:43 +0200388 out_wakeup:
389 if (b->flags & BF_WRITE_STATUS)
390 task_wakeup(fdtab[fd].owner);
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100391 fdtab[fd].ev &= ~FD_POLL_OUT;
Willy Tarreau83749182007-04-15 20:56:27 +0200392 return retval;
Willy Tarreau6996e152007-04-30 14:37:43 +0200393
394 out_error:
395 /* There was an error. we must wakeup the task. No need to clear
396 * the events, the task will do it.
397 */
398 fdtab[fd].state = FD_STERROR;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100399 fdtab[fd].ev &= ~FD_POLL_STICKY;
Willy Tarreau6996e152007-04-30 14:37:43 +0200400 b->flags |= BF_WRITE_ERROR;
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200401 b->wex = TICK_ETERNITY;
402 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200403}
404
Willy Tarreaubaaee002006-06-26 02:48:02 +0200405
406
407/*
408 * Local variables:
409 * c-indent-level: 8
410 * c-basic-offset: 8
411 * End:
412 */