blob: 08cc65b65058c1262606301175e434e787c76cb2 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Functions operating on SOCK_STREAM and buffers.
3 *
Willy Tarreaue09e0ce2007-03-18 16:31:29 +01004 * Copyright 2000-2007 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 Tarreau2dd0d472006-06-29 17:53:05 +020026#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020027
Willy Tarreaubaaee002006-06-26 02:48:02 +020028#include <types/buffers.h>
29#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020030#include <types/polling.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020031
32#include <proto/client.h>
33#include <proto/fd.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020034#include <proto/stream_sock.h>
35#include <proto/task.h>
36
37
38/*
Willy Tarreaud7971282006-07-29 18:36:34 +020039 * this function is called on a read event from a stream socket.
Willy Tarreau83749182007-04-15 20:56:27 +020040 * It returns 0 if we have a high confidence that we will not be
41 * able to read more data without polling first. Returns non-zero
42 * otherwise.
Willy Tarreaubaaee002006-06-26 02:48:02 +020043 */
Willy Tarreaud7971282006-07-29 18:36:34 +020044int stream_sock_read(int fd) {
Willy Tarreaud6f087e2008-01-18 17:20:13 +010045 __label__ out_eternity, out_wakeup, out_shutdown_r, out_error;
Willy Tarreau54469402006-07-29 16:59:06 +020046 struct buffer *b = fdtab[fd].cb[DIR_RD].b;
Willy Tarreau83749182007-04-15 20:56:27 +020047 int ret, max, retval;
Willy Tarreaub8949f12007-03-23 22:39:59 +010048 int read_poll = MAX_READ_POLL_LOOPS;
Willy Tarreaubaaee002006-06-26 02:48:02 +020049
50#ifdef DEBUG_FULL
Willy Tarreaud6f087e2008-01-18 17:20:13 +010051 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 +020052#endif
53
Willy Tarreau83749182007-04-15 20:56:27 +020054 retval = 1;
55
Willy Tarreaud6f087e2008-01-18 17:20:13 +010056 /* stop immediately on errors */
57 if (fdtab[fd].state == FD_STERROR || (fdtab[fd].ev & FD_POLL_ERR))
Willy Tarreau6996e152007-04-30 14:37:43 +020058 goto out_error;
Willy Tarreaud6f087e2008-01-18 17:20:13 +010059
60 /* stop here if we reached the end of data */
61 if ((fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP)) == FD_POLL_HUP)
62 goto out_shutdown_r;
Willy Tarreau83749182007-04-15 20:56:27 +020063
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 Tarreau83749182007-04-15 20:56:27 +020089 EV_FD_CLR(fd, DIR_RD);
Willy Tarreau6996e152007-04-30 14:37:43 +020090 goto out_eternity;
Willy Tarreau83749182007-04-15 20:56:27 +020091 }
Willy Tarreaubaaee002006-06-26 02:48:02 +020092
Willy Tarreau6996e152007-04-30 14:37:43 +020093 /*
94 * 2. read the largest possible block
95 */
Willy Tarreaubaaee002006-06-26 02:48:02 +020096#ifndef MSG_NOSIGNAL
Willy Tarreau83749182007-04-15 20:56:27 +020097 {
98 int skerr;
99 socklen_t lskerr = sizeof(skerr);
100
101 ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr);
102 if (ret == -1 || skerr)
103 ret = -1;
104 else
105 ret = recv(fd, b->r, max, 0);
106 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200107#else
Willy Tarreau83749182007-04-15 20:56:27 +0200108 ret = recv(fd, b->r, max, MSG_NOSIGNAL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200109#endif
Willy Tarreau83749182007-04-15 20:56:27 +0200110 if (ret > 0) {
111 b->r += ret;
112 b->l += ret;
113 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 */
125 EV_FD_CLR(fd, DIR_RD);
126 goto out_eternity;
127 }
128
Willy Tarreauab3e1d32007-06-03 14:10:36 +0200129 /* if too many bytes were missing from last read, it means that
130 * it's pointless trying to read again because the system does
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100131 * not have them in buffers. BTW, if FD_POLL_HUP was present,
132 * it means that we have reached the end and that the connection
133 * is closed.
Willy Tarreauab3e1d32007-06-03 14:10:36 +0200134 */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100135 if (ret < max) {
136 if (fdtab[fd].ev & FD_POLL_HUP)
137 goto out_shutdown_r;
Willy Tarreauab3e1d32007-06-03 14:10:36 +0200138 break;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100139 }
Willy Tarreauab3e1d32007-06-03 14:10:36 +0200140
141 /* generally if we read something smaller than 1 or 2 MSS,
Willy Tarreau83749182007-04-15 20:56:27 +0200142 * it means that it's not worth trying to read again. It may
143 * also happen on headers, but the application then can stop
144 * reading before we start polling.
145 */
146 if (ret < MIN_RET_FOR_READ_LOOP)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200147 break;
Willy Tarreau83749182007-04-15 20:56:27 +0200148
Willy Tarreau6996e152007-04-30 14:37:43 +0200149 if (--read_poll <= 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200150 break;
Willy Tarreau83749182007-04-15 20:56:27 +0200151
Willy Tarreau83749182007-04-15 20:56:27 +0200152 }
153 else if (ret == 0) {
Willy Tarreau6996e152007-04-30 14:37:43 +0200154 /* connection closed */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100155 goto out_shutdown_r;
Willy Tarreau83749182007-04-15 20:56:27 +0200156 }
Willy Tarreau9f195292007-04-15 21:26:58 +0200157 else if (errno == EAGAIN) {
158 /* Ignore EAGAIN but inform the poller that there is
Willy Tarreau6996e152007-04-30 14:37:43 +0200159 * nothing to read left. But we may have done some work
160 * justifying to notify the task.
Willy Tarreau9f195292007-04-15 21:26:58 +0200161 */
Willy Tarreau83749182007-04-15 20:56:27 +0200162 retval = 0;
163 break;
164 }
165 else {
Willy Tarreau6996e152007-04-30 14:37:43 +0200166 goto out_error;
Willy Tarreau83749182007-04-15 20:56:27 +0200167 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200168 } /* while (1) */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200169
Willy Tarreau6996e152007-04-30 14:37:43 +0200170 /*
171 * The only way to get out of this loop is to have stopped reading
172 * without any error nor close, either by limiting the number of
173 * loops, or because of an EAGAIN. We only rearm the timer if we
174 * have at least read something.
175 */
176
177 if (b->flags & BF_PARTIAL_READ) {
Willy Tarreaua8b55e32007-05-13 16:08:19 +0200178 if (tv_add_ifset(&b->rex, &now, &b->rto))
Willy Tarreau6996e152007-04-30 14:37:43 +0200179 goto out_wakeup;
Willy Tarreau6996e152007-04-30 14:37:43 +0200180 out_eternity:
181 tv_eternity(&b->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200182 }
183
Willy Tarreau6996e152007-04-30 14:37:43 +0200184 out_wakeup:
185 if (b->flags & BF_READ_STATUS)
186 task_wakeup(fdtab[fd].owner);
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100187 fdtab[fd].ev &= ~FD_POLL_IN;
Willy Tarreau83749182007-04-15 20:56:27 +0200188 return retval;
Willy Tarreau6996e152007-04-30 14:37:43 +0200189
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100190 out_shutdown_r:
191 fdtab[fd].ev &= ~FD_POLL_HUP;
192 b->flags |= BF_READ_NULL;
193 goto out_eternity;
194
Willy Tarreau6996e152007-04-30 14:37:43 +0200195 out_error:
196 /* There was an error. we must wakeup the task. No need to clear
197 * the events, the task will do it.
198 */
199 fdtab[fd].state = FD_STERROR;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100200 fdtab[fd].ev &= ~FD_POLL_STICKY;
Willy Tarreau6996e152007-04-30 14:37:43 +0200201 b->flags |= BF_READ_ERROR;
202 goto out_eternity;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200203}
204
205
206/*
Willy Tarreauf8306d52006-07-29 19:01:31 +0200207 * this function is called on a write event from a stream socket.
Willy Tarreau83749182007-04-15 20:56:27 +0200208 * It returns 0 if we have a high confidence that we will not be
209 * able to write more data without polling first. Returns non-zero
210 * otherwise.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200211 */
Willy Tarreauf8306d52006-07-29 19:01:31 +0200212int stream_sock_write(int fd) {
Willy Tarreau6996e152007-04-30 14:37:43 +0200213 __label__ out_eternity, out_wakeup, out_error;
Willy Tarreau54469402006-07-29 16:59:06 +0200214 struct buffer *b = fdtab[fd].cb[DIR_WR].b;
Willy Tarreau83749182007-04-15 20:56:27 +0200215 int ret, max, retval;
216 int write_poll = MAX_WRITE_POLL_LOOPS;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200217
218#ifdef DEBUG_FULL
Willy Tarreauf8306d52006-07-29 19:01:31 +0200219 fprintf(stderr,"stream_sock_write : fd=%d, owner=%p\n", fd, fdtab[fd].owner);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200220#endif
221
Willy Tarreau83749182007-04-15 20:56:27 +0200222 retval = 1;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100223 if (fdtab[fd].state == FD_STERROR || (fdtab[fd].ev & FD_POLL_ERR))
Willy Tarreau6996e152007-04-30 14:37:43 +0200224 goto out_error;
Willy Tarreau83749182007-04-15 20:56:27 +0200225
Willy Tarreau6996e152007-04-30 14:37:43 +0200226 while (1) {
Willy Tarreau83749182007-04-15 20:56:27 +0200227 if (b->l == 0) { /* let's realign the buffer to optimize I/O */
228 b->r = b->w = b->lr = b->data;
229 max = 0;
230 }
231 else if (b->r > b->w) {
232 max = b->r - b->w;
233 }
234 else {
235 max = b->data + BUFSIZE - b->w;
236 }
237
Willy Tarreaubaaee002006-06-26 02:48:02 +0200238 if (max == 0) {
Willy Tarreauf8306d52006-07-29 19:01:31 +0200239 /* may be we have received a connection acknowledgement in TCP mode without data */
Willy Tarreau6996e152007-04-30 14:37:43 +0200240 if (likely(fdtab[fd].state == FD_STCONN)) {
Willy Tarreau6996e152007-04-30 14:37:43 +0200241 /* We have no data to send to check the connection, and
242 * getsockopt() will not inform us whether the connection
243 * is still pending. So we'll reuse connect() to check the
244 * state of the socket. This has the advantage of givig us
245 * the following info :
246 * - error
247 * - connecting (EALREADY, EINPROGRESS)
248 * - connected (EISCONN, 0)
249 */
Willy Tarreaue94ebd02007-10-09 17:14:37 +0200250 if ((connect(fd, fdtab[fd].peeraddr, fdtab[fd].peerlen) == 0))
Willy Tarreau6996e152007-04-30 14:37:43 +0200251 errno = 0;
252
253 if (errno == EALREADY || errno == EINPROGRESS) {
254 retval = 0;
255 goto out_wakeup;
Willy Tarreauf8306d52006-07-29 19:01:31 +0200256 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200257
258 if (errno && errno != EISCONN)
259 goto out_error;
260
261 /* OK we just need to indicate that we got a connection
262 * and that we wrote nothing.
263 */
264 b->flags |= BF_WRITE_NULL;
265 fdtab[fd].state = FD_STREADY;
Willy Tarreauf8306d52006-07-29 19:01:31 +0200266 }
267
Willy Tarreau6996e152007-04-30 14:37:43 +0200268 /* Funny, we were called to write something but there wasn't
269 * anything. Theorically we cannot get there, but just in case,
270 * let's disable the write event and pretend we never came there.
271 */
Willy Tarreauf161a342007-04-08 16:59:42 +0200272 EV_FD_CLR(fd, DIR_WR);
Willy Tarreau83749182007-04-15 20:56:27 +0200273 goto out_eternity;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200274 }
275
276#ifndef MSG_NOSIGNAL
277 {
278 int skerr;
279 socklen_t lskerr = sizeof(skerr);
280
Willy Tarreauc6423482006-10-15 14:59:03 +0200281 ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr);
282 if (ret == -1 || skerr)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200283 ret = -1;
284 else
285 ret = send(fd, b->w, max, MSG_DONTWAIT);
286 }
287#else
288 ret = send(fd, b->w, max, MSG_DONTWAIT | MSG_NOSIGNAL);
289#endif
290
291 if (ret > 0) {
292 b->l -= ret;
293 b->w += ret;
294
Willy Tarreau0f9f5052006-07-29 17:39:25 +0200295 b->flags |= BF_PARTIAL_WRITE;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200296
297 if (b->w == b->data + BUFSIZE) {
298 b->w = b->data; /* wrap around the buffer */
299 }
Willy Tarreau83749182007-04-15 20:56:27 +0200300
Willy Tarreau6996e152007-04-30 14:37:43 +0200301 if (!b->l) {
302 EV_FD_CLR(fd, DIR_WR);
303 goto out_eternity;
304 }
Willy Tarreau83749182007-04-15 20:56:27 +0200305
Willy Tarreauab3e1d32007-06-03 14:10:36 +0200306 /* if the system buffer is full, don't insist */
307 if (ret < max)
308 break;
309
Willy Tarreau6996e152007-04-30 14:37:43 +0200310 if (--write_poll <= 0)
311 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200312 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200313 else if (ret == 0 || errno == EAGAIN) {
314 /* nothing written, just pretend we were never called
315 * and wait for the socket to be ready. But we may have
316 * done some work justifying to notify the task.
317 */
Willy Tarreau83749182007-04-15 20:56:27 +0200318 retval = 0;
319 break;
320 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200321 else {
Willy Tarreau6996e152007-04-30 14:37:43 +0200322 goto out_error;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200323 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200324 } /* while (1) */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200325
Willy Tarreau6996e152007-04-30 14:37:43 +0200326 /*
327 * The only way to get out of this loop is to have stopped writing
328 * without any error, either by limiting the number of loops, or
329 * because of an EAGAIN. We only rearm the timer if we have at least
330 * written something.
331 */
332
333 if (b->flags & BF_PARTIAL_WRITE) {
Willy Tarreaua8b55e32007-05-13 16:08:19 +0200334 if (tv_add_ifset(&b->wex, &now, &b->wto)) {
Willy Tarreau83749182007-04-15 20:56:27 +0200335 /* FIXME: to prevent the client from expiring read timeouts during writes,
336 * we refresh it. A solution would be to merge read+write timeouts into a
337 * unique one, although that needs some study particularly on full-duplex
338 * TCP connections. */
Willy Tarreaufa645582007-06-03 15:59:52 +0200339 if (!(b->flags & BF_SHUTR_STATUS))
340 b->rex = b->wex;
Willy Tarreau6996e152007-04-30 14:37:43 +0200341 goto out_wakeup;
Willy Tarreau83749182007-04-15 20:56:27 +0200342 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200343 out_eternity:
344 tv_eternity(&b->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200345 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200346
Willy Tarreau6996e152007-04-30 14:37:43 +0200347 out_wakeup:
348 if (b->flags & BF_WRITE_STATUS)
349 task_wakeup(fdtab[fd].owner);
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100350 fdtab[fd].ev &= ~FD_POLL_OUT;
Willy Tarreau83749182007-04-15 20:56:27 +0200351 return retval;
Willy Tarreau6996e152007-04-30 14:37:43 +0200352
353 out_error:
354 /* There was an error. we must wakeup the task. No need to clear
355 * the events, the task will do it.
356 */
357 fdtab[fd].state = FD_STERROR;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100358 fdtab[fd].ev &= ~FD_POLL_STICKY;
Willy Tarreau6996e152007-04-30 14:37:43 +0200359 b->flags |= BF_WRITE_ERROR;
360 goto out_eternity;
361
362
Willy Tarreaubaaee002006-06-26 02:48:02 +0200363}
364
Willy Tarreaubaaee002006-06-26 02:48:02 +0200365
366
367/*
368 * Local variables:
369 * c-indent-level: 8
370 * c-basic-offset: 8
371 * End:
372 */