blob: 8c47d318cb48a42c16f116e32ec1b2f9de2f88f4 [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 Tarreaubaaee002006-06-26 02:48:02 +020030
31#include <proto/client.h>
32#include <proto/fd.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020033#include <proto/stream_sock.h>
34#include <proto/task.h>
35
36
37/*
Willy Tarreaud7971282006-07-29 18:36:34 +020038 * this function is called on a read event from a stream socket.
Willy Tarreau83749182007-04-15 20:56:27 +020039 * It returns 0 if we have a high confidence that we will not be
40 * able to read more data without polling first. Returns non-zero
41 * otherwise.
Willy Tarreaubaaee002006-06-26 02:48:02 +020042 */
Willy Tarreaud7971282006-07-29 18:36:34 +020043int stream_sock_read(int fd) {
Willy Tarreau6996e152007-04-30 14:37:43 +020044 __label__ out_eternity, out_wakeup, out_error;
Willy Tarreau54469402006-07-29 16:59:06 +020045 struct buffer *b = fdtab[fd].cb[DIR_RD].b;
Willy Tarreau83749182007-04-15 20:56:27 +020046 int ret, max, retval;
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 Tarreaud7971282006-07-29 18:36:34 +020050 fprintf(stderr,"stream_sock_read : fd=%d, owner=%p\n", fd, 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 Tarreau83749182007-04-15 20:56:27 +020055 if (unlikely(fdtab[fd].ev & FD_POLL_HUP)) {
56 /* connection closed */
57 b->flags |= BF_READ_NULL;
Willy Tarreau6996e152007-04-30 14:37:43 +020058 goto out_eternity;
59 }
60 else if (unlikely(fdtab[fd].state == FD_STERROR || (fdtab[fd].ev & FD_POLL_ERR))) {
61 goto out_error;
Willy Tarreau83749182007-04-15 20:56:27 +020062 }
63
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
131 * not have them in buffers.
132 */
133 if (ret < max)
134 break;
135
136 /* generally if we read something smaller than 1 or 2 MSS,
Willy Tarreau83749182007-04-15 20:56:27 +0200137 * it means that it's not worth trying to read again. It may
138 * also happen on headers, but the application then can stop
139 * reading before we start polling.
140 */
141 if (ret < MIN_RET_FOR_READ_LOOP)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200142 break;
Willy Tarreau83749182007-04-15 20:56:27 +0200143
Willy Tarreau6996e152007-04-30 14:37:43 +0200144 if (--read_poll <= 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200145 break;
Willy Tarreau83749182007-04-15 20:56:27 +0200146
Willy Tarreau83749182007-04-15 20:56:27 +0200147 }
148 else if (ret == 0) {
Willy Tarreau6996e152007-04-30 14:37:43 +0200149 /* connection closed */
Willy Tarreau83749182007-04-15 20:56:27 +0200150 b->flags |= BF_READ_NULL;
Willy Tarreau6996e152007-04-30 14:37:43 +0200151 goto out_eternity;
Willy Tarreau83749182007-04-15 20:56:27 +0200152 }
Willy Tarreau9f195292007-04-15 21:26:58 +0200153 else if (errno == EAGAIN) {
154 /* Ignore EAGAIN but inform the poller that there is
Willy Tarreau6996e152007-04-30 14:37:43 +0200155 * nothing to read left. But we may have done some work
156 * justifying to notify the task.
Willy Tarreau9f195292007-04-15 21:26:58 +0200157 */
Willy Tarreau83749182007-04-15 20:56:27 +0200158 retval = 0;
159 break;
160 }
161 else {
Willy Tarreau6996e152007-04-30 14:37:43 +0200162 goto out_error;
Willy Tarreau83749182007-04-15 20:56:27 +0200163 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200164 } /* while (1) */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200165
Willy Tarreau6996e152007-04-30 14:37:43 +0200166 /*
167 * The only way to get out of this loop is to have stopped reading
168 * without any error nor close, either by limiting the number of
169 * loops, or because of an EAGAIN. We only rearm the timer if we
170 * have at least read something.
171 */
172
173 if (b->flags & BF_PARTIAL_READ) {
Willy Tarreaua8b55e32007-05-13 16:08:19 +0200174 if (tv_add_ifset(&b->rex, &now, &b->rto))
Willy Tarreau6996e152007-04-30 14:37:43 +0200175 goto out_wakeup;
Willy Tarreau6996e152007-04-30 14:37:43 +0200176 out_eternity:
177 tv_eternity(&b->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200178 }
179
Willy Tarreau6996e152007-04-30 14:37:43 +0200180 out_wakeup:
181 if (b->flags & BF_READ_STATUS)
182 task_wakeup(fdtab[fd].owner);
Willy Tarreau83749182007-04-15 20:56:27 +0200183 fdtab[fd].ev &= ~FD_POLL_RD;
184 return retval;
Willy Tarreau6996e152007-04-30 14:37:43 +0200185
186 out_error:
187 /* There was an error. we must wakeup the task. No need to clear
188 * the events, the task will do it.
189 */
190 fdtab[fd].state = FD_STERROR;
191 b->flags |= BF_READ_ERROR;
192 goto out_eternity;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200193}
194
195
196/*
Willy Tarreauf8306d52006-07-29 19:01:31 +0200197 * this function is called on a write event from a stream socket.
Willy Tarreau83749182007-04-15 20:56:27 +0200198 * It returns 0 if we have a high confidence that we will not be
199 * able to write more data without polling first. Returns non-zero
200 * otherwise.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200201 */
Willy Tarreauf8306d52006-07-29 19:01:31 +0200202int stream_sock_write(int fd) {
Willy Tarreau6996e152007-04-30 14:37:43 +0200203 __label__ out_eternity, out_wakeup, out_error;
Willy Tarreau54469402006-07-29 16:59:06 +0200204 struct buffer *b = fdtab[fd].cb[DIR_WR].b;
Willy Tarreau83749182007-04-15 20:56:27 +0200205 int ret, max, retval;
206 int write_poll = MAX_WRITE_POLL_LOOPS;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200207
208#ifdef DEBUG_FULL
Willy Tarreauf8306d52006-07-29 19:01:31 +0200209 fprintf(stderr,"stream_sock_write : fd=%d, owner=%p\n", fd, fdtab[fd].owner);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200210#endif
211
Willy Tarreau83749182007-04-15 20:56:27 +0200212 retval = 1;
Willy Tarreau6996e152007-04-30 14:37:43 +0200213 if (unlikely(fdtab[fd].state == FD_STERROR || (fdtab[fd].ev & FD_POLL_ERR)))
214 goto out_error;
Willy Tarreau83749182007-04-15 20:56:27 +0200215
Willy Tarreau6996e152007-04-30 14:37:43 +0200216 while (1) {
Willy Tarreau83749182007-04-15 20:56:27 +0200217 if (b->l == 0) { /* let's realign the buffer to optimize I/O */
218 b->r = b->w = b->lr = b->data;
219 max = 0;
220 }
221 else if (b->r > b->w) {
222 max = b->r - b->w;
223 }
224 else {
225 max = b->data + BUFSIZE - b->w;
226 }
227
Willy Tarreaubaaee002006-06-26 02:48:02 +0200228 if (max == 0) {
Willy Tarreauf8306d52006-07-29 19:01:31 +0200229 /* may be we have received a connection acknowledgement in TCP mode without data */
Willy Tarreau6996e152007-04-30 14:37:43 +0200230 if (likely(fdtab[fd].state == FD_STCONN)) {
Willy Tarreau6996e152007-04-30 14:37:43 +0200231 /* We have no data to send to check the connection, and
232 * getsockopt() will not inform us whether the connection
233 * is still pending. So we'll reuse connect() to check the
234 * state of the socket. This has the advantage of givig us
235 * the following info :
236 * - error
237 * - connecting (EALREADY, EINPROGRESS)
238 * - connected (EISCONN, 0)
239 */
Willy Tarreaue94ebd02007-10-09 17:14:37 +0200240 if ((connect(fd, fdtab[fd].peeraddr, fdtab[fd].peerlen) == 0))
Willy Tarreau6996e152007-04-30 14:37:43 +0200241 errno = 0;
242
243 if (errno == EALREADY || errno == EINPROGRESS) {
244 retval = 0;
245 goto out_wakeup;
Willy Tarreauf8306d52006-07-29 19:01:31 +0200246 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200247
248 if (errno && errno != EISCONN)
249 goto out_error;
250
251 /* OK we just need to indicate that we got a connection
252 * and that we wrote nothing.
253 */
254 b->flags |= BF_WRITE_NULL;
255 fdtab[fd].state = FD_STREADY;
Willy Tarreauf8306d52006-07-29 19:01:31 +0200256 }
257
Willy Tarreau6996e152007-04-30 14:37:43 +0200258 /* Funny, we were called to write something but there wasn't
259 * anything. Theorically we cannot get there, but just in case,
260 * let's disable the write event and pretend we never came there.
261 */
Willy Tarreauf161a342007-04-08 16:59:42 +0200262 EV_FD_CLR(fd, DIR_WR);
Willy Tarreau83749182007-04-15 20:56:27 +0200263 goto out_eternity;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200264 }
265
266#ifndef MSG_NOSIGNAL
267 {
268 int skerr;
269 socklen_t lskerr = sizeof(skerr);
270
Willy Tarreauc6423482006-10-15 14:59:03 +0200271 ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr);
272 if (ret == -1 || skerr)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200273 ret = -1;
274 else
275 ret = send(fd, b->w, max, MSG_DONTWAIT);
276 }
277#else
278 ret = send(fd, b->w, max, MSG_DONTWAIT | MSG_NOSIGNAL);
279#endif
280
281 if (ret > 0) {
282 b->l -= ret;
283 b->w += ret;
284
Willy Tarreau0f9f5052006-07-29 17:39:25 +0200285 b->flags |= BF_PARTIAL_WRITE;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200286
287 if (b->w == b->data + BUFSIZE) {
288 b->w = b->data; /* wrap around the buffer */
289 }
Willy Tarreau83749182007-04-15 20:56:27 +0200290
Willy Tarreau6996e152007-04-30 14:37:43 +0200291 if (!b->l) {
292 EV_FD_CLR(fd, DIR_WR);
293 goto out_eternity;
294 }
Willy Tarreau83749182007-04-15 20:56:27 +0200295
Willy Tarreauab3e1d32007-06-03 14:10:36 +0200296 /* if the system buffer is full, don't insist */
297 if (ret < max)
298 break;
299
Willy Tarreau6996e152007-04-30 14:37:43 +0200300 if (--write_poll <= 0)
301 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200302 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200303 else if (ret == 0 || errno == EAGAIN) {
304 /* nothing written, just pretend we were never called
305 * and wait for the socket to be ready. But we may have
306 * done some work justifying to notify the task.
307 */
Willy Tarreau83749182007-04-15 20:56:27 +0200308 retval = 0;
309 break;
310 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200311 else {
Willy Tarreau6996e152007-04-30 14:37:43 +0200312 goto out_error;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200313 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200314 } /* while (1) */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200315
Willy Tarreau6996e152007-04-30 14:37:43 +0200316 /*
317 * The only way to get out of this loop is to have stopped writing
318 * without any error, either by limiting the number of loops, or
319 * because of an EAGAIN. We only rearm the timer if we have at least
320 * written something.
321 */
322
323 if (b->flags & BF_PARTIAL_WRITE) {
Willy Tarreaua8b55e32007-05-13 16:08:19 +0200324 if (tv_add_ifset(&b->wex, &now, &b->wto)) {
Willy Tarreau83749182007-04-15 20:56:27 +0200325 /* FIXME: to prevent the client from expiring read timeouts during writes,
326 * we refresh it. A solution would be to merge read+write timeouts into a
327 * unique one, although that needs some study particularly on full-duplex
328 * TCP connections. */
Willy Tarreaufa645582007-06-03 15:59:52 +0200329 if (!(b->flags & BF_SHUTR_STATUS))
330 b->rex = b->wex;
Willy Tarreau6996e152007-04-30 14:37:43 +0200331 goto out_wakeup;
Willy Tarreau83749182007-04-15 20:56:27 +0200332 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200333 out_eternity:
334 tv_eternity(&b->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200335 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200336
Willy Tarreau6996e152007-04-30 14:37:43 +0200337 out_wakeup:
338 if (b->flags & BF_WRITE_STATUS)
339 task_wakeup(fdtab[fd].owner);
Willy Tarreau83749182007-04-15 20:56:27 +0200340 fdtab[fd].ev &= ~FD_POLL_WR;
341 return retval;
Willy Tarreau6996e152007-04-30 14:37:43 +0200342
343 out_error:
344 /* There was an error. we must wakeup the task. No need to clear
345 * the events, the task will do it.
346 */
347 fdtab[fd].state = FD_STERROR;
348 b->flags |= BF_WRITE_ERROR;
349 goto out_eternity;
350
351
Willy Tarreaubaaee002006-06-26 02:48:02 +0200352}
353
Willy Tarreaubaaee002006-06-26 02:48:02 +0200354
355
356/*
357 * Local variables:
358 * c-indent-level: 8
359 * c-basic-offset: 8
360 * End:
361 */