blob: ae5993b347e3b1da0bd1c4773703c981d8d5ea6f [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 Tarreau83749182007-04-15 20:56:27 +020024#include <common/standard.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020025#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020026
Willy Tarreaubaaee002006-06-26 02:48:02 +020027#include <types/buffers.h>
28#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020029#include <types/polling.h>
Willy Tarreau6996e152007-04-30 14:37:43 +020030#include <types/session.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 Tarreau6996e152007-04-30 14:37:43 +020045 __label__ out_eternity, out_wakeup, 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 Tarreaud7971282006-07-29 18:36:34 +020051 fprintf(stderr,"stream_sock_read : fd=%d, owner=%p\n", fd, 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 Tarreau83749182007-04-15 20:56:27 +020056 if (unlikely(fdtab[fd].ev & FD_POLL_HUP)) {
57 /* connection closed */
58 b->flags |= BF_READ_NULL;
Willy Tarreau6996e152007-04-30 14:37:43 +020059 goto out_eternity;
60 }
61 else if (unlikely(fdtab[fd].state == FD_STERROR || (fdtab[fd].ev & FD_POLL_ERR))) {
62 goto out_error;
Willy Tarreau83749182007-04-15 20:56:27 +020063 }
64
Willy Tarreau6996e152007-04-30 14:37:43 +020065 while (1) {
66 /*
67 * 1. compute the maximum block size we can read at once.
68 */
Willy Tarreau83749182007-04-15 20:56:27 +020069 if (b->l == 0) { /* let's realign the buffer to optimize I/O */
70 b->r = b->w = b->lr = b->data;
71 max = b->rlim - b->data;
72 }
73 else if (b->r > b->w) {
74 max = b->rlim - b->r;
75 }
76 else {
77 max = b->w - b->r;
78 /* FIXME: theorically, if w>0, we shouldn't have rlim < data+size anymore
79 * since it means that the rewrite protection has been removed. This
80 * implies that the if statement can be removed.
81 */
82 if (max > b->rlim - b->data)
Willy Tarreaubaaee002006-06-26 02:48:02 +020083 max = b->rlim - b->data;
Willy Tarreau83749182007-04-15 20:56:27 +020084 }
Willy Tarreaubaaee002006-06-26 02:48:02 +020085
Willy Tarreau6996e152007-04-30 14:37:43 +020086 if (unlikely(max == 0)) {
87 /* Not anymore room to store data. This should theorically
88 * never happen, but better safe than sorry !
89 */
Willy Tarreau83749182007-04-15 20:56:27 +020090 EV_FD_CLR(fd, DIR_RD);
Willy Tarreau6996e152007-04-30 14:37:43 +020091 goto out_eternity;
Willy Tarreau83749182007-04-15 20:56:27 +020092 }
Willy Tarreaubaaee002006-06-26 02:48:02 +020093
Willy Tarreau6996e152007-04-30 14:37:43 +020094 /*
95 * 2. read the largest possible block
96 */
Willy Tarreaubaaee002006-06-26 02:48:02 +020097#ifndef MSG_NOSIGNAL
Willy Tarreau83749182007-04-15 20:56:27 +020098 {
99 int skerr;
100 socklen_t lskerr = sizeof(skerr);
101
102 ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr);
103 if (ret == -1 || skerr)
104 ret = -1;
105 else
106 ret = recv(fd, b->r, max, 0);
107 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200108#else
Willy Tarreau83749182007-04-15 20:56:27 +0200109 ret = recv(fd, b->r, max, MSG_NOSIGNAL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200110#endif
Willy Tarreau83749182007-04-15 20:56:27 +0200111 if (ret > 0) {
112 b->r += ret;
113 b->l += ret;
114 b->flags |= BF_PARTIAL_READ;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200115
Willy Tarreau83749182007-04-15 20:56:27 +0200116 if (b->r == b->data + BUFSIZE) {
117 b->r = b->data; /* wrap around the buffer */
118 }
Willy Tarreau9641e8f2007-03-23 23:02:09 +0100119
Willy Tarreau83749182007-04-15 20:56:27 +0200120 b->total += ret;
Willy Tarreau9641e8f2007-03-23 23:02:09 +0100121
Willy Tarreau6996e152007-04-30 14:37:43 +0200122 if (b->l == b->rlim - b->data) {
123 /* The buffer is now full, there's no point in going through
124 * the loop again.
125 */
126 EV_FD_CLR(fd, DIR_RD);
127 goto out_eternity;
128 }
129
Willy Tarreau83749182007-04-15 20:56:27 +0200130 /* generally if we read something smaller than the 1 or 2 MSS,
131 * it means that it's not worth trying to read again. It may
132 * also happen on headers, but the application then can stop
133 * reading before we start polling.
134 */
135 if (ret < MIN_RET_FOR_READ_LOOP)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200136 break;
Willy Tarreau83749182007-04-15 20:56:27 +0200137
Willy Tarreau6996e152007-04-30 14:37:43 +0200138 if (--read_poll <= 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200139 break;
Willy Tarreau83749182007-04-15 20:56:27 +0200140
Willy Tarreau83749182007-04-15 20:56:27 +0200141 }
142 else if (ret == 0) {
Willy Tarreau6996e152007-04-30 14:37:43 +0200143 /* connection closed */
Willy Tarreau83749182007-04-15 20:56:27 +0200144 b->flags |= BF_READ_NULL;
Willy Tarreau6996e152007-04-30 14:37:43 +0200145 goto out_eternity;
Willy Tarreau83749182007-04-15 20:56:27 +0200146 }
Willy Tarreau9f195292007-04-15 21:26:58 +0200147 else if (errno == EAGAIN) {
148 /* Ignore EAGAIN but inform the poller that there is
Willy Tarreau6996e152007-04-30 14:37:43 +0200149 * nothing to read left. But we may have done some work
150 * justifying to notify the task.
Willy Tarreau9f195292007-04-15 21:26:58 +0200151 */
Willy Tarreau83749182007-04-15 20:56:27 +0200152 retval = 0;
153 break;
154 }
155 else {
Willy Tarreau6996e152007-04-30 14:37:43 +0200156 goto out_error;
Willy Tarreau83749182007-04-15 20:56:27 +0200157 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200158 } /* while (1) */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200159
Willy Tarreau6996e152007-04-30 14:37:43 +0200160 /*
161 * The only way to get out of this loop is to have stopped reading
162 * without any error nor close, either by limiting the number of
163 * loops, or because of an EAGAIN. We only rearm the timer if we
164 * have at least read something.
165 */
166
167 if (b->flags & BF_PARTIAL_READ) {
Willy Tarreaua8b55e32007-05-13 16:08:19 +0200168 if (tv_add_ifset(&b->rex, &now, &b->rto))
Willy Tarreau6996e152007-04-30 14:37:43 +0200169 goto out_wakeup;
Willy Tarreau6996e152007-04-30 14:37:43 +0200170 out_eternity:
171 tv_eternity(&b->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200172 }
173
Willy Tarreau6996e152007-04-30 14:37:43 +0200174 out_wakeup:
175 if (b->flags & BF_READ_STATUS)
176 task_wakeup(fdtab[fd].owner);
Willy Tarreau83749182007-04-15 20:56:27 +0200177 fdtab[fd].ev &= ~FD_POLL_RD;
178 return retval;
Willy Tarreau6996e152007-04-30 14:37:43 +0200179
180 out_error:
181 /* There was an error. we must wakeup the task. No need to clear
182 * the events, the task will do it.
183 */
184 fdtab[fd].state = FD_STERROR;
185 b->flags |= BF_READ_ERROR;
186 goto out_eternity;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200187}
188
189
190/*
Willy Tarreauf8306d52006-07-29 19:01:31 +0200191 * this function is called on a write event from a stream socket.
Willy Tarreau83749182007-04-15 20:56:27 +0200192 * It returns 0 if we have a high confidence that we will not be
193 * able to write more data without polling first. Returns non-zero
194 * otherwise.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200195 */
Willy Tarreauf8306d52006-07-29 19:01:31 +0200196int stream_sock_write(int fd) {
Willy Tarreau6996e152007-04-30 14:37:43 +0200197 __label__ out_eternity, out_wakeup, out_error;
Willy Tarreau54469402006-07-29 16:59:06 +0200198 struct buffer *b = fdtab[fd].cb[DIR_WR].b;
Willy Tarreau83749182007-04-15 20:56:27 +0200199 int ret, max, retval;
200 int write_poll = MAX_WRITE_POLL_LOOPS;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200201
202#ifdef DEBUG_FULL
Willy Tarreauf8306d52006-07-29 19:01:31 +0200203 fprintf(stderr,"stream_sock_write : fd=%d, owner=%p\n", fd, fdtab[fd].owner);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200204#endif
205
Willy Tarreau83749182007-04-15 20:56:27 +0200206 retval = 1;
Willy Tarreau6996e152007-04-30 14:37:43 +0200207 if (unlikely(fdtab[fd].state == FD_STERROR || (fdtab[fd].ev & FD_POLL_ERR)))
208 goto out_error;
Willy Tarreau83749182007-04-15 20:56:27 +0200209
Willy Tarreau6996e152007-04-30 14:37:43 +0200210 while (1) {
Willy Tarreau83749182007-04-15 20:56:27 +0200211 if (b->l == 0) { /* let's realign the buffer to optimize I/O */
212 b->r = b->w = b->lr = b->data;
213 max = 0;
214 }
215 else if (b->r > b->w) {
216 max = b->r - b->w;
217 }
218 else {
219 max = b->data + BUFSIZE - b->w;
220 }
221
Willy Tarreaubaaee002006-06-26 02:48:02 +0200222 if (max == 0) {
Willy Tarreauf8306d52006-07-29 19:01:31 +0200223 /* may be we have received a connection acknowledgement in TCP mode without data */
Willy Tarreau6996e152007-04-30 14:37:43 +0200224 if (likely(fdtab[fd].state == FD_STCONN)) {
225 struct session *s = fdtab[fd].owner->context;
226
227 /* We have no data to send to check the connection, and
228 * getsockopt() will not inform us whether the connection
229 * is still pending. So we'll reuse connect() to check the
230 * state of the socket. This has the advantage of givig us
231 * the following info :
232 * - error
233 * - connecting (EALREADY, EINPROGRESS)
234 * - connected (EISCONN, 0)
235 */
236 if ((connect(fd, (struct sockaddr *)&s->srv_addr, sizeof(s->srv_addr)) == 0))
237 errno = 0;
238
239 if (errno == EALREADY || errno == EINPROGRESS) {
240 retval = 0;
241 goto out_wakeup;
Willy Tarreauf8306d52006-07-29 19:01:31 +0200242 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200243
244 if (errno && errno != EISCONN)
245 goto out_error;
246
247 /* OK we just need to indicate that we got a connection
248 * and that we wrote nothing.
249 */
250 b->flags |= BF_WRITE_NULL;
251 fdtab[fd].state = FD_STREADY;
Willy Tarreauf8306d52006-07-29 19:01:31 +0200252 }
253
Willy Tarreau6996e152007-04-30 14:37:43 +0200254 /* Funny, we were called to write something but there wasn't
255 * anything. Theorically we cannot get there, but just in case,
256 * let's disable the write event and pretend we never came there.
257 */
Willy Tarreauf161a342007-04-08 16:59:42 +0200258 EV_FD_CLR(fd, DIR_WR);
Willy Tarreau83749182007-04-15 20:56:27 +0200259 goto out_eternity;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200260 }
261
262#ifndef MSG_NOSIGNAL
263 {
264 int skerr;
265 socklen_t lskerr = sizeof(skerr);
266
Willy Tarreauc6423482006-10-15 14:59:03 +0200267 ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr);
268 if (ret == -1 || skerr)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200269 ret = -1;
270 else
271 ret = send(fd, b->w, max, MSG_DONTWAIT);
272 }
273#else
274 ret = send(fd, b->w, max, MSG_DONTWAIT | MSG_NOSIGNAL);
275#endif
276
277 if (ret > 0) {
278 b->l -= ret;
279 b->w += ret;
280
Willy Tarreau0f9f5052006-07-29 17:39:25 +0200281 b->flags |= BF_PARTIAL_WRITE;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200282
283 if (b->w == b->data + BUFSIZE) {
284 b->w = b->data; /* wrap around the buffer */
285 }
Willy Tarreau83749182007-04-15 20:56:27 +0200286
Willy Tarreau6996e152007-04-30 14:37:43 +0200287 if (!b->l) {
288 EV_FD_CLR(fd, DIR_WR);
289 goto out_eternity;
290 }
Willy Tarreau83749182007-04-15 20:56:27 +0200291
Willy Tarreau6996e152007-04-30 14:37:43 +0200292 if (--write_poll <= 0)
293 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200294 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200295 else if (ret == 0 || errno == EAGAIN) {
296 /* nothing written, just pretend we were never called
297 * and wait for the socket to be ready. But we may have
298 * done some work justifying to notify the task.
299 */
Willy Tarreau83749182007-04-15 20:56:27 +0200300 retval = 0;
301 break;
302 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200303 else {
Willy Tarreau6996e152007-04-30 14:37:43 +0200304 goto out_error;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200305 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200306 } /* while (1) */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200307
Willy Tarreau6996e152007-04-30 14:37:43 +0200308 /*
309 * The only way to get out of this loop is to have stopped writing
310 * without any error, either by limiting the number of loops, or
311 * because of an EAGAIN. We only rearm the timer if we have at least
312 * written something.
313 */
314
315 if (b->flags & BF_PARTIAL_WRITE) {
Willy Tarreaua8b55e32007-05-13 16:08:19 +0200316 if (tv_add_ifset(&b->wex, &now, &b->wto)) {
Willy Tarreau83749182007-04-15 20:56:27 +0200317 /* FIXME: to prevent the client from expiring read timeouts during writes,
318 * we refresh it. A solution would be to merge read+write timeouts into a
319 * unique one, although that needs some study particularly on full-duplex
320 * TCP connections. */
321 b->rex = b->wex;
Willy Tarreau6996e152007-04-30 14:37:43 +0200322 goto out_wakeup;
Willy Tarreau83749182007-04-15 20:56:27 +0200323 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200324 out_eternity:
325 tv_eternity(&b->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200326 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200327
Willy Tarreau6996e152007-04-30 14:37:43 +0200328 out_wakeup:
329 if (b->flags & BF_WRITE_STATUS)
330 task_wakeup(fdtab[fd].owner);
Willy Tarreau83749182007-04-15 20:56:27 +0200331 fdtab[fd].ev &= ~FD_POLL_WR;
332 return retval;
Willy Tarreau6996e152007-04-30 14:37:43 +0200333
334 out_error:
335 /* There was an error. we must wakeup the task. No need to clear
336 * the events, the task will do it.
337 */
338 fdtab[fd].state = FD_STERROR;
339 b->flags |= BF_WRITE_ERROR;
340 goto out_eternity;
341
342
Willy Tarreaubaaee002006-06-26 02:48:02 +0200343}
344
Willy Tarreaubaaee002006-06-26 02:48:02 +0200345
346
347/*
348 * Local variables:
349 * c-indent-level: 8
350 * c-basic-offset: 8
351 * End:
352 */