blob: 4de51ee2cbb968b9e7bd2aac782f57859442efdb [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 Tarreaue5ed4062008-08-30 03:17:31 +020044 struct stream_interface *si = fdtab[fd].owner;
Willy Tarreau48adac52008-08-30 04:58:38 +020045 struct buffer *b = si->ib;
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);
Willy Tarreau48adac52008-08-30 04:58:38 +0200254
255 /* Maybe we have to completely close the local socket */
256 if (si->ob->flags & BF_SHUTW)
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200257 goto do_close_and_return;
258 EV_FD_CLR(fd, DIR_RD);
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200259 goto out_wakeup;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100260
Willy Tarreau6996e152007-04-30 14:37:43 +0200261 out_error:
Willy Tarreaucff64112008-11-03 06:26:53 +0100262 /* Read error on the file descriptor. We mark the FD as STERROR so
263 * that we don't use it anymore. The error is reported to the stream
264 * interface which will take proper action. We must not perturbate the
265 * buffer because the stream interface wants to ensure transparent
266 * connection retries.
Willy Tarreau6996e152007-04-30 14:37:43 +0200267 */
Willy Tarreaucff64112008-11-03 06:26:53 +0100268
Willy Tarreau6996e152007-04-30 14:37:43 +0200269 fdtab[fd].state = FD_STERROR;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100270 fdtab[fd].ev &= ~FD_POLL_STICKY;
Willy Tarreaucff64112008-11-03 06:26:53 +0100271 si->flags |= SI_FL_ERR;
272 goto wakeup_return;
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200273
274 do_close_and_return:
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200275 si->state = SI_ST_CLO;
Willy Tarreaucff64112008-11-03 06:26:53 +0100276 fd_delete(fd);
277 wakeup_return:
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200278 task_wakeup(si->owner, TASK_WOKEN_IO);
279 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200280}
281
282
283/*
Willy Tarreauf8306d52006-07-29 19:01:31 +0200284 * this function is called on a write event from a stream socket.
Willy Tarreau83749182007-04-15 20:56:27 +0200285 * It returns 0 if we have a high confidence that we will not be
286 * able to write more data without polling first. Returns non-zero
287 * otherwise.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200288 */
Willy Tarreauf8306d52006-07-29 19:01:31 +0200289int stream_sock_write(int fd) {
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200290 __label__ out_wakeup, out_error;
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200291 struct stream_interface *si = fdtab[fd].owner;
Willy Tarreau48adac52008-08-30 04:58:38 +0200292 struct buffer *b = si->ob;
Willy Tarreau83749182007-04-15 20:56:27 +0200293 int ret, max, retval;
294 int write_poll = MAX_WRITE_POLL_LOOPS;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200295
296#ifdef DEBUG_FULL
Willy Tarreauf8306d52006-07-29 19:01:31 +0200297 fprintf(stderr,"stream_sock_write : fd=%d, owner=%p\n", fd, fdtab[fd].owner);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200298#endif
299
Willy Tarreau83749182007-04-15 20:56:27 +0200300 retval = 1;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100301 if (fdtab[fd].state == FD_STERROR || (fdtab[fd].ev & FD_POLL_ERR))
Willy Tarreau6996e152007-04-30 14:37:43 +0200302 goto out_error;
Willy Tarreau83749182007-04-15 20:56:27 +0200303
Willy Tarreau6996e152007-04-30 14:37:43 +0200304 while (1) {
Willy Tarreau83749182007-04-15 20:56:27 +0200305 if (b->l == 0) { /* let's realign the buffer to optimize I/O */
306 b->r = b->w = b->lr = b->data;
307 max = 0;
308 }
309 else if (b->r > b->w) {
310 max = b->r - b->w;
311 }
312 else {
313 max = b->data + BUFSIZE - b->w;
314 }
315
Willy Tarreaubaaee002006-06-26 02:48:02 +0200316 if (max == 0) {
Willy Tarreauf8306d52006-07-29 19:01:31 +0200317 /* may be we have received a connection acknowledgement in TCP mode without data */
Willy Tarreau6996e152007-04-30 14:37:43 +0200318 if (likely(fdtab[fd].state == FD_STCONN)) {
Willy Tarreau6996e152007-04-30 14:37:43 +0200319 /* We have no data to send to check the connection, and
320 * getsockopt() will not inform us whether the connection
321 * is still pending. So we'll reuse connect() to check the
322 * state of the socket. This has the advantage of givig us
323 * the following info :
324 * - error
325 * - connecting (EALREADY, EINPROGRESS)
326 * - connected (EISCONN, 0)
327 */
Willy Tarreaue94ebd02007-10-09 17:14:37 +0200328 if ((connect(fd, fdtab[fd].peeraddr, fdtab[fd].peerlen) == 0))
Willy Tarreau6996e152007-04-30 14:37:43 +0200329 errno = 0;
330
331 if (errno == EALREADY || errno == EINPROGRESS) {
332 retval = 0;
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200333 goto out_may_wakeup;
Willy Tarreauf8306d52006-07-29 19:01:31 +0200334 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200335
336 if (errno && errno != EISCONN)
337 goto out_error;
338
339 /* OK we just need to indicate that we got a connection
340 * and that we wrote nothing.
341 */
342 b->flags |= BF_WRITE_NULL;
343 fdtab[fd].state = FD_STREADY;
Willy Tarreauf8306d52006-07-29 19:01:31 +0200344 }
345
Willy Tarreau6996e152007-04-30 14:37:43 +0200346 /* Funny, we were called to write something but there wasn't
347 * anything. Theorically we cannot get there, but just in case,
348 * let's disable the write event and pretend we never came there.
349 */
Willy Tarreauf161a342007-04-08 16:59:42 +0200350 EV_FD_CLR(fd, DIR_WR);
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200351 b->wex = TICK_ETERNITY;
352 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200353 }
354
355#ifndef MSG_NOSIGNAL
356 {
357 int skerr;
358 socklen_t lskerr = sizeof(skerr);
359
Willy Tarreauc6423482006-10-15 14:59:03 +0200360 ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr);
361 if (ret == -1 || skerr)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200362 ret = -1;
363 else
364 ret = send(fd, b->w, max, MSG_DONTWAIT);
365 }
366#else
367 ret = send(fd, b->w, max, MSG_DONTWAIT | MSG_NOSIGNAL);
368#endif
369
370 if (ret > 0) {
371 b->l -= ret;
372 b->w += ret;
373
Willy Tarreau3da77c52008-08-29 09:58:42 +0200374 b->flags |= BF_WRITE_PARTIAL;
Willy Tarreaue393fe22008-08-16 22:18:07 +0200375
376 if (b->l < b->rlim - b->data)
377 b->flags &= ~BF_FULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200378
379 if (b->w == b->data + BUFSIZE) {
380 b->w = b->data; /* wrap around the buffer */
381 }
Willy Tarreau83749182007-04-15 20:56:27 +0200382
Willy Tarreau6996e152007-04-30 14:37:43 +0200383 if (!b->l) {
Willy Tarreaue393fe22008-08-16 22:18:07 +0200384 b->flags |= BF_EMPTY;
Willy Tarreau48adac52008-08-30 04:58:38 +0200385
386 /* Maybe we just wrote the last chunk and need to close ? */
387 if ((b->flags & (BF_SHUTW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) == (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR)) {
388 if (si->state == SI_ST_EST) {
389 buffer_shutw(b);
390 if (si->ib->flags & BF_SHUTR)
391 goto do_close_and_return;
392 shutdown(fd, SHUT_WR);
393 }
394 }
395
Willy Tarreau6996e152007-04-30 14:37:43 +0200396 EV_FD_CLR(fd, DIR_WR);
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200397 b->wex = TICK_ETERNITY;
398 goto out_wakeup;
Willy Tarreau6996e152007-04-30 14:37:43 +0200399 }
Willy Tarreau83749182007-04-15 20:56:27 +0200400
Willy Tarreauab3e1d32007-06-03 14:10:36 +0200401 /* if the system buffer is full, don't insist */
402 if (ret < max)
403 break;
404
Willy Tarreau6996e152007-04-30 14:37:43 +0200405 if (--write_poll <= 0)
406 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200407 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200408 else if (ret == 0 || errno == EAGAIN) {
409 /* nothing written, just pretend we were never called
410 * and wait for the socket to be ready. But we may have
411 * done some work justifying to notify the task.
412 */
Willy Tarreau83749182007-04-15 20:56:27 +0200413 retval = 0;
414 break;
415 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200416 else {
Willy Tarreau6996e152007-04-30 14:37:43 +0200417 goto out_error;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200418 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200419 } /* while (1) */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200420
Willy Tarreau6996e152007-04-30 14:37:43 +0200421 /*
422 * The only way to get out of this loop is to have stopped writing
423 * without any error, either by limiting the number of loops, or
424 * because of an EAGAIN. We only rearm the timer if we have at least
425 * written something.
426 */
427
Willy Tarreau3da77c52008-08-29 09:58:42 +0200428 if (tick_isset(b->wex) && b->flags & BF_WRITE_PARTIAL) {
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200429 b->wex = tick_add_ifset(now_ms, b->wto);
Willy Tarreauadfb8562008-08-11 15:24:42 +0200430 if (tick_isset(b->wex)) {
Willy Tarreau83749182007-04-15 20:56:27 +0200431 /* FIXME: to prevent the client from expiring read timeouts during writes,
432 * we refresh it. A solution would be to merge read+write timeouts into a
433 * unique one, although that needs some study particularly on full-duplex
434 * TCP connections. */
Willy Tarreauba392ce2008-08-16 21:13:23 +0200435 if (tick_isset(b->rex) && !(b->flags & BF_SHUTR))
Willy Tarreaufa645582007-06-03 15:59:52 +0200436 b->rex = b->wex;
Willy Tarreau83749182007-04-15 20:56:27 +0200437 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200438 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200439
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200440 out_may_wakeup:
Willy Tarreau3da77c52008-08-29 09:58:42 +0200441 if (!(b->flags & BF_WRITE_ACTIVITY))
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200442 goto out_skip_wakeup;
Willy Tarreau6996e152007-04-30 14:37:43 +0200443 out_wakeup:
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200444 task_wakeup(si->owner, TASK_WOKEN_IO);
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200445
446 out_skip_wakeup:
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100447 fdtab[fd].ev &= ~FD_POLL_OUT;
Willy Tarreau83749182007-04-15 20:56:27 +0200448 return retval;
Willy Tarreau6996e152007-04-30 14:37:43 +0200449
450 out_error:
Willy Tarreaucff64112008-11-03 06:26:53 +0100451 /* Write error on the file descriptor. We mark the FD as STERROR so
452 * that we don't use it anymore. The error is reported to the stream
453 * interface which will take proper action. We must not perturbate the
454 * buffer because the stream interface wants to ensure transparent
455 * connection retries.
Willy Tarreau6996e152007-04-30 14:37:43 +0200456 */
Willy Tarreaucff64112008-11-03 06:26:53 +0100457
Willy Tarreau6996e152007-04-30 14:37:43 +0200458 fdtab[fd].state = FD_STERROR;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100459 fdtab[fd].ev &= ~FD_POLL_STICKY;
Willy Tarreaucff64112008-11-03 06:26:53 +0100460 si->flags |= SI_FL_ERR;
461 goto wakeup_return;
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200462
Willy Tarreau48adac52008-08-30 04:58:38 +0200463 do_close_and_return:
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200464 si->state = SI_ST_CLO;
Willy Tarreaucff64112008-11-03 06:26:53 +0100465 fd_delete(fd);
466 wakeup_return:
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200467 task_wakeup(si->owner, TASK_WOKEN_IO);
468 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200469}
470
Willy Tarreau48adac52008-08-30 04:58:38 +0200471/*
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200472 * This function performs a shutdown-write on a stream interface in a connected or
473 * init state (it does nothing for other states). It either shuts the write side
Willy Tarreau48adac52008-08-30 04:58:38 +0200474 * closes the file descriptor and marks itself as closed. No buffer flags are
475 * changed, it's up to the caller to adjust them. The sole purpose of this
476 * function is to be called from the other stream interface to notify of a
477 * close_read, or by itself upon a full write leading to an empty buffer.
478 * It normally returns zero, unless it has completely closed the socket, in
479 * which case it returns 1.
480 */
481int stream_sock_shutw(struct stream_interface *si)
482{
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200483 if (si->state != SI_ST_EST && si->state != SI_ST_CON)
Willy Tarreau48adac52008-08-30 04:58:38 +0200484 return 0;
485
486 if (si->ib->flags & BF_SHUTR) {
487 fd_delete(si->fd);
488 si->state = SI_ST_CLO;
489 return 1;
490 }
491 EV_FD_CLR(si->fd, DIR_WR);
492 shutdown(si->fd, SHUT_WR);
493 return 0;
494}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200495
Willy Tarreau2d212792008-08-27 21:41:35 +0200496/*
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200497 * This function performs a shutdown-read on a stream interface in a connected or
498 * init state (it does nothing for other states). It either shuts the read side or
499 * closes the file descriptor and marks itself as closed. No buffer flags are
500 * changed, it's up to the caller to adjust them. The sole purpose of this
501 * function is to be called from the other stream interface to notify of a
502 * close_read, or by itself upon a full write leading to an empty buffer.
503 * It normally returns zero, unless it has completely closed the socket, in
504 * which case it returns 1.
505 */
506int stream_sock_shutr(struct stream_interface *si)
507{
508 if (si->state != SI_ST_EST && si->state != SI_ST_CON)
509 return 0;
510
Willy Tarreaucff64112008-11-03 06:26:53 +0100511 if (si->ob->flags & BF_SHUTW) {
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200512 fd_delete(si->fd);
513 si->state = SI_ST_CLO;
514 return 1;
515 }
516 EV_FD_CLR(si->fd, DIR_RD);
517 return 0;
518}
519
520/*
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200521 * Manages a stream_sock connection during its data phase. The buffers are
522 * examined for various cases of shutdown, then file descriptor and buffers'
523 * flags are updated accordingly.
524 */
525int stream_sock_data_update(int fd)
526{
527 struct buffer *ib = fdtab[fd].cb[DIR_RD].b;
528 struct buffer *ob = fdtab[fd].cb[DIR_WR].b;
529
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200530 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 +0200531 now_ms, __FUNCTION__,
532 fd, fdtab[fd].owner,
533 ib, ob,
534 ib->rex, ob->wex,
535 ib->flags, ob->flags,
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200536 ib->l, ob->l, ob->cons->state);
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200537
Willy Tarreau2d212792008-08-27 21:41:35 +0200538 /* Check if we need to close the read side */
539 if (!(ib->flags & BF_SHUTR)) {
540 /* Last read, forced read-shutdown, or other end closed */
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200541 if (ib->flags & (BF_SHUTR_NOW|BF_SHUTW)) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200542 //trace_term(t, TT_HTTP_SRV_10);
Willy Tarreau2d212792008-08-27 21:41:35 +0200543 buffer_shutr(ib);
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200544 if (ob->flags & BF_SHUTW) {
545 fd_delete(fd);
546 ob->cons->state = SI_ST_CLO;
547 return 0;
548 }
Willy Tarreau2d212792008-08-27 21:41:35 +0200549 EV_FD_CLR(fd, DIR_RD);
550 }
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200551 }
552
553 /* Check if we need to close the write side */
554 if (!(ob->flags & BF_SHUTW)) {
555 /* Forced write-shutdown or other end closed with empty buffer. */
556 if ((ob->flags & BF_SHUTW_NOW) ||
Willy Tarreau3da77c52008-08-29 09:58:42 +0200557 (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 +0200558 //trace_term(t, TT_HTTP_SRV_11);
559 buffer_shutw(ob);
560 if (ib->flags & BF_SHUTR) {
561 fd_delete(fd);
562 ob->cons->state = SI_ST_CLO;
563 return 0;
Willy Tarreau2d212792008-08-27 21:41:35 +0200564 }
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200565 EV_FD_CLR(fd, DIR_WR);
566 shutdown(fd, SHUT_WR);
Willy Tarreau2d212792008-08-27 21:41:35 +0200567 }
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200568 }
569 return 0; /* other cases change nothing */
570}
571
572
573/*
574 * Updates a connected stream_sock file descriptor status and timeouts
575 * according to the buffers' flags. It should only be called once after the
576 * buffer flags have settled down, and before they are cleared. It doesn't
577 * harm to call it as often as desired (it just slightly hurts performance).
578 */
579int stream_sock_data_finish(int fd)
580{
581 struct buffer *ib = fdtab[fd].cb[DIR_RD].b;
582 struct buffer *ob = fdtab[fd].cb[DIR_WR].b;
583
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200584 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 +0200585 now_ms, __FUNCTION__,
586 fd, fdtab[fd].owner,
587 ib, ob,
588 ib->rex, ob->wex,
589 ib->flags, ob->flags,
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200590 ib->l, ob->l, ob->cons->state);
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200591
592 /* Check if we need to close the read side */
593 if (!(ib->flags & BF_SHUTR)) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200594 /* Read not closed, update FD status and timeout for reads */
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200595 if (ib->flags & (BF_FULL|BF_HIJACK)) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200596 /* stop reading */
597 EV_FD_COND_C(fd, DIR_RD);
598 ib->rex = TICK_ETERNITY;
599 }
600 else {
601 /* (re)start reading and update timeout. Note: we don't recompute the timeout
602 * everytime we get here, otherwise it would risk never to expire. We only
603 * update it if is was not yet set, or if we already got some read status.
604 */
605 EV_FD_COND_S(fd, DIR_RD);
Willy Tarreau3da77c52008-08-29 09:58:42 +0200606 if (!tick_isset(ib->rex) || ib->flags & BF_READ_ACTIVITY)
Willy Tarreau2d212792008-08-27 21:41:35 +0200607 ib->rex = tick_add_ifset(now_ms, ib->rto);
608 }
609 }
610
611 /* Check if we need to close the write side */
612 if (!(ob->flags & BF_SHUTW)) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200613 /* Write not closed, update FD status and timeout for writes */
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200614 if ((ob->flags & BF_EMPTY) ||
Willy Tarreau3da77c52008-08-29 09:58:42 +0200615 (ob->flags & (BF_HIJACK|BF_WRITE_ENA)) == 0) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200616 /* stop writing */
617 EV_FD_COND_C(fd, DIR_WR);
618 ob->wex = TICK_ETERNITY;
619 }
620 else {
621 /* (re)start writing and update timeout. Note: we don't recompute the timeout
622 * everytime we get here, otherwise it would risk never to expire. We only
623 * update it if is was not yet set, or if we already got some write status.
624 */
625 EV_FD_COND_S(fd, DIR_WR);
Willy Tarreau3da77c52008-08-29 09:58:42 +0200626 if (!tick_isset(ob->wex) || ob->flags & BF_WRITE_ACTIVITY) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200627 ob->wex = tick_add_ifset(now_ms, ob->wto);
Willy Tarreau21e1be82008-08-29 11:30:14 +0200628 if (tick_isset(ob->wex) && tick_isset(ib->rex)) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200629 /* Note: depending on the protocol, we don't know if we're waiting
630 * for incoming data or not. So in order to prevent the socket from
631 * expiring read timeouts during writes, we refresh the read timeout,
632 * except if it was already infinite.
633 */
634 ib->rex = ob->wex;
635 }
636 }
637 }
638 }
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200639 return 0;
Willy Tarreau2d212792008-08-27 21:41:35 +0200640}
641
Willy Tarreaubaaee002006-06-26 02:48:02 +0200642
643/*
644 * Local variables:
645 * c-indent-level: 8
646 * c-basic-offset: 8
647 * End:
648 */