blob: fdd0dbdf3a9b46968f1c4741d4e8c3581f5a7ce4 [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 Tarreau74ab2ac2008-11-23 17:23:07 +010084
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 Tarreaub0ef7352008-12-14 13:26:20 +010089 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreaue393fe22008-08-16 22:18:07 +020090 b->flags |= BF_FULL;
Willy Tarreau83749182007-04-15 20:56:27 +020091 EV_FD_CLR(fd, DIR_RD);
Willy Tarreau0c303ee2008-07-07 00:09:58 +020092 b->rex = TICK_ETERNITY;
93 goto out_wakeup;
Willy Tarreau83749182007-04-15 20:56:27 +020094 }
Willy Tarreaubaaee002006-06-26 02:48:02 +020095
Willy Tarreau6996e152007-04-30 14:37:43 +020096 /*
97 * 2. read the largest possible block
98 */
Willy Tarreaubaaee002006-06-26 02:48:02 +020099#ifndef MSG_NOSIGNAL
Willy Tarreau83749182007-04-15 20:56:27 +0200100 {
101 int skerr;
102 socklen_t lskerr = sizeof(skerr);
103
104 ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr);
105 if (ret == -1 || skerr)
106 ret = -1;
107 else
108 ret = recv(fd, b->r, max, 0);
109 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200110#else
Willy Tarreau83749182007-04-15 20:56:27 +0200111 ret = recv(fd, b->r, max, MSG_NOSIGNAL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200112#endif
Willy Tarreau83749182007-04-15 20:56:27 +0200113 if (ret > 0) {
114 b->r += ret;
115 b->l += ret;
Willy Tarreau8a7af602008-05-03 23:07:14 +0200116 cur_read += ret;
Willy Tarreaub38903c2008-11-23 21:33:29 +0100117
Willy Tarreauf890dc92008-12-13 21:12:26 +0100118 /* if noone is interested in analysing data, let's forward everything */
119 if (!b->analysers)
120 b->send_max += ret;
121
Willy Tarreaub38903c2008-11-23 21:33:29 +0100122 if (fdtab[fd].state == FD_STCONN)
123 fdtab[fd].state = FD_STREADY;
124
Willy Tarreau3da77c52008-08-29 09:58:42 +0200125 b->flags |= BF_READ_PARTIAL;
Willy Tarreaue393fe22008-08-16 22:18:07 +0200126 b->flags &= ~BF_EMPTY;
Willy Tarreau74ab2ac2008-11-23 17:23:07 +0100127
Willy Tarreau83749182007-04-15 20:56:27 +0200128 if (b->r == b->data + BUFSIZE) {
129 b->r = b->data; /* wrap around the buffer */
130 }
Willy Tarreau9641e8f2007-03-23 23:02:09 +0100131
Willy Tarreau83749182007-04-15 20:56:27 +0200132 b->total += ret;
Willy Tarreau9641e8f2007-03-23 23:02:09 +0100133
Willy Tarreaue393fe22008-08-16 22:18:07 +0200134 if (b->l >= b->rlim - b->data) {
Willy Tarreau6996e152007-04-30 14:37:43 +0200135 /* The buffer is now full, there's no point in going through
136 * the loop again.
137 */
Willy Tarreau8a7af602008-05-03 23:07:14 +0200138 if (!(b->flags & BF_STREAMER_FAST) && (cur_read == b->l)) {
139 b->xfer_small = 0;
140 b->xfer_large++;
141 if (b->xfer_large >= 3) {
142 /* we call this buffer a fast streamer if it manages
143 * to be filled in one call 3 consecutive times.
144 */
145 b->flags |= (BF_STREAMER | BF_STREAMER_FAST);
146 //fputc('+', stderr);
147 }
148 }
149 else if ((b->flags & (BF_STREAMER | BF_STREAMER_FAST)) &&
150 (cur_read <= BUFSIZE / 2)) {
151 b->xfer_large = 0;
152 b->xfer_small++;
153 if (b->xfer_small >= 2) {
154 /* if the buffer has been at least half full twice,
155 * we receive faster than we send, so at least it
156 * is not a "fast streamer".
157 */
158 b->flags &= ~BF_STREAMER_FAST;
159 //fputc('-', stderr);
160 }
161 }
162 else {
163 b->xfer_small = 0;
164 b->xfer_large = 0;
165 }
166
Willy Tarreaub0ef7352008-12-14 13:26:20 +0100167 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreaue393fe22008-08-16 22:18:07 +0200168 b->flags |= BF_FULL;
Willy Tarreau6996e152007-04-30 14:37:43 +0200169 EV_FD_CLR(fd, DIR_RD);
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200170 b->rex = TICK_ETERNITY;
171 goto out_wakeup;
Willy Tarreau6996e152007-04-30 14:37:43 +0200172 }
173
Willy Tarreauab3e1d32007-06-03 14:10:36 +0200174 /* if too many bytes were missing from last read, it means that
175 * it's pointless trying to read again because the system does
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100176 * not have them in buffers. BTW, if FD_POLL_HUP was present,
177 * it means that we have reached the end and that the connection
178 * is closed.
Willy Tarreauab3e1d32007-06-03 14:10:36 +0200179 */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100180 if (ret < max) {
Willy Tarreau8a7af602008-05-03 23:07:14 +0200181 if ((b->flags & (BF_STREAMER | BF_STREAMER_FAST)) &&
182 (cur_read <= BUFSIZE / 2)) {
183 b->xfer_large = 0;
184 b->xfer_small++;
185 if (b->xfer_small >= 3) {
186 /* we have read less than half of the buffer in
187 * one pass, and this happened at least 3 times.
188 * This is definitely not a streamer.
189 */
190 b->flags &= ~(BF_STREAMER | BF_STREAMER_FAST);
191 //fputc('!', stderr);
192 }
193 }
Willy Tarreau2bea3a12008-08-28 09:47:43 +0200194 /* unfortunately, on level-triggered events, POLL_HUP
195 * is generally delivered AFTER the system buffer is
196 * empty, so this one might never match.
197 */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100198 if (fdtab[fd].ev & FD_POLL_HUP)
199 goto out_shutdown_r;
Willy Tarreau2bea3a12008-08-28 09:47:43 +0200200
201 /* if a streamer has read few data, it may be because we
202 * have exhausted system buffers. It's not worth trying
203 * again.
204 */
205 if (b->flags & BF_STREAMER)
206 break;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100207 }
Willy Tarreauab3e1d32007-06-03 14:10:36 +0200208
209 /* generally if we read something smaller than 1 or 2 MSS,
Willy Tarreau2bea3a12008-08-28 09:47:43 +0200210 * it means that either we have exhausted the system's
211 * buffers (streamer or question-response protocol) or that
212 * the connection will be closed. Streamers are easily
213 * detected so we return early. For other cases, it's still
214 * better to perform a last read to be sure, because it may
215 * save one complete poll/read/wakeup cycle in case of shutdown.
Willy Tarreau83749182007-04-15 20:56:27 +0200216 */
Willy Tarreau2bea3a12008-08-28 09:47:43 +0200217 if (ret < MIN_RET_FOR_READ_LOOP && b->flags & BF_STREAMER)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200218 break;
Willy Tarreau83749182007-04-15 20:56:27 +0200219
Willy Tarreau6996e152007-04-30 14:37:43 +0200220 if (--read_poll <= 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200221 break;
Willy Tarreau83749182007-04-15 20:56:27 +0200222 }
223 else if (ret == 0) {
Willy Tarreau6996e152007-04-30 14:37:43 +0200224 /* connection closed */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100225 goto out_shutdown_r;
Willy Tarreau83749182007-04-15 20:56:27 +0200226 }
Willy Tarreau9f195292007-04-15 21:26:58 +0200227 else if (errno == EAGAIN) {
228 /* Ignore EAGAIN but inform the poller that there is
Willy Tarreau6996e152007-04-30 14:37:43 +0200229 * nothing to read left. But we may have done some work
230 * justifying to notify the task.
Willy Tarreau9f195292007-04-15 21:26:58 +0200231 */
Willy Tarreau83749182007-04-15 20:56:27 +0200232 retval = 0;
233 break;
234 }
235 else {
Willy Tarreau6996e152007-04-30 14:37:43 +0200236 goto out_error;
Willy Tarreau83749182007-04-15 20:56:27 +0200237 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200238 } /* while (1) */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200239
Willy Tarreau6996e152007-04-30 14:37:43 +0200240 /*
241 * The only way to get out of this loop is to have stopped reading
242 * without any error nor close, either by limiting the number of
243 * loops, or because of an EAGAIN. We only rearm the timer if we
244 * have at least read something.
245 */
246
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100247 if ((b->flags & (BF_READ_PARTIAL|BF_FULL|BF_READ_NOEXP)) == BF_READ_PARTIAL)
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200248 b->rex = tick_add_ifset(now_ms, b->rto);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200249
Willy Tarreau3da77c52008-08-29 09:58:42 +0200250 if (!(b->flags & BF_READ_ACTIVITY))
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200251 goto out_skip_wakeup;
Willy Tarreau6996e152007-04-30 14:37:43 +0200252 out_wakeup:
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100253 /* the consumer might be waiting for data */
254 if (b->cons->flags & SI_FL_WAIT_DATA && (b->flags & BF_READ_PARTIAL))
255 b->cons->chk_snd(b->cons);
256
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200257 task_wakeup(si->owner, TASK_WOKEN_IO);
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200258
259 out_skip_wakeup:
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100260 fdtab[fd].ev &= ~FD_POLL_IN;
Willy Tarreau83749182007-04-15 20:56:27 +0200261 return retval;
Willy Tarreau6996e152007-04-30 14:37:43 +0200262
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100263 out_shutdown_r:
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200264 /* we received a shutdown */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100265 fdtab[fd].ev &= ~FD_POLL_HUP;
266 b->flags |= BF_READ_NULL;
Willy Tarreau99126c32008-11-27 10:30:51 +0100267 stream_sock_shutr(si);
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200268 goto out_wakeup;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100269
Willy Tarreau6996e152007-04-30 14:37:43 +0200270 out_error:
Willy Tarreaucff64112008-11-03 06:26:53 +0100271 /* Read error on the file descriptor. We mark the FD as STERROR so
272 * that we don't use it anymore. The error is reported to the stream
273 * interface which will take proper action. We must not perturbate the
274 * buffer because the stream interface wants to ensure transparent
275 * connection retries.
Willy Tarreau6996e152007-04-30 14:37:43 +0200276 */
Willy Tarreaucff64112008-11-03 06:26:53 +0100277
Willy Tarreau6996e152007-04-30 14:37:43 +0200278 fdtab[fd].state = FD_STERROR;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100279 fdtab[fd].ev &= ~FD_POLL_STICKY;
Willy Tarreaucff64112008-11-03 06:26:53 +0100280 si->flags |= SI_FL_ERR;
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200281 task_wakeup(si->owner, TASK_WOKEN_IO);
282 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200283}
284
285
286/*
Willy Tarreauf8306d52006-07-29 19:01:31 +0200287 * this function is called on a write event from a stream socket.
Willy Tarreau83749182007-04-15 20:56:27 +0200288 * It returns 0 if we have a high confidence that we will not be
289 * able to write more data without polling first. Returns non-zero
290 * otherwise.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200291 */
Willy Tarreauf8306d52006-07-29 19:01:31 +0200292int stream_sock_write(int fd) {
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200293 __label__ out_wakeup, out_error;
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200294 struct stream_interface *si = fdtab[fd].owner;
Willy Tarreau48adac52008-08-30 04:58:38 +0200295 struct buffer *b = si->ob;
Willy Tarreau83749182007-04-15 20:56:27 +0200296 int ret, max, retval;
297 int write_poll = MAX_WRITE_POLL_LOOPS;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200298
299#ifdef DEBUG_FULL
Willy Tarreauf8306d52006-07-29 19:01:31 +0200300 fprintf(stderr,"stream_sock_write : fd=%d, owner=%p\n", fd, fdtab[fd].owner);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200301#endif
302
Willy Tarreau83749182007-04-15 20:56:27 +0200303 retval = 1;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100304 if (fdtab[fd].state == FD_STERROR || (fdtab[fd].ev & FD_POLL_ERR))
Willy Tarreau6996e152007-04-30 14:37:43 +0200305 goto out_error;
Willy Tarreau83749182007-04-15 20:56:27 +0200306
Willy Tarreau6996e152007-04-30 14:37:43 +0200307 while (1) {
Willy Tarreau83749182007-04-15 20:56:27 +0200308 if (b->l == 0) { /* let's realign the buffer to optimize I/O */
309 b->r = b->w = b->lr = b->data;
310 max = 0;
311 }
312 else if (b->r > b->w) {
313 max = b->r - b->w;
314 }
315 else {
316 max = b->data + BUFSIZE - b->w;
317 }
318
Willy Tarreauf890dc92008-12-13 21:12:26 +0100319 /* limit the amount of outgoing data if required */
320 if (max > b->send_max)
321 max = b->send_max;
322
Willy Tarreaubaaee002006-06-26 02:48:02 +0200323 if (max == 0) {
Willy Tarreauf8306d52006-07-29 19:01:31 +0200324 /* may be we have received a connection acknowledgement in TCP mode without data */
Willy Tarreau6996e152007-04-30 14:37:43 +0200325 if (likely(fdtab[fd].state == FD_STCONN)) {
Willy Tarreau6996e152007-04-30 14:37:43 +0200326 /* We have no data to send to check the connection, and
327 * getsockopt() will not inform us whether the connection
328 * is still pending. So we'll reuse connect() to check the
329 * state of the socket. This has the advantage of givig us
330 * the following info :
331 * - error
332 * - connecting (EALREADY, EINPROGRESS)
333 * - connected (EISCONN, 0)
334 */
Willy Tarreaue94ebd02007-10-09 17:14:37 +0200335 if ((connect(fd, fdtab[fd].peeraddr, fdtab[fd].peerlen) == 0))
Willy Tarreau6996e152007-04-30 14:37:43 +0200336 errno = 0;
337
338 if (errno == EALREADY || errno == EINPROGRESS) {
339 retval = 0;
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200340 goto out_may_wakeup;
Willy Tarreauf8306d52006-07-29 19:01:31 +0200341 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200342
343 if (errno && errno != EISCONN)
344 goto out_error;
345
346 /* OK we just need to indicate that we got a connection
347 * and that we wrote nothing.
348 */
349 b->flags |= BF_WRITE_NULL;
350 fdtab[fd].state = FD_STREADY;
Willy Tarreauf8306d52006-07-29 19:01:31 +0200351 }
352
Willy Tarreau6996e152007-04-30 14:37:43 +0200353 /* Funny, we were called to write something but there wasn't
354 * anything. Theorically we cannot get there, but just in case,
355 * let's disable the write event and pretend we never came there.
356 */
Willy Tarreaub0ef7352008-12-14 13:26:20 +0100357 si->flags |= SI_FL_WAIT_DATA;
Willy Tarreauf161a342007-04-08 16:59:42 +0200358 EV_FD_CLR(fd, DIR_WR);
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200359 b->wex = TICK_ETERNITY;
360 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200361 }
362
363#ifndef MSG_NOSIGNAL
364 {
365 int skerr;
366 socklen_t lskerr = sizeof(skerr);
367
Willy Tarreauc6423482006-10-15 14:59:03 +0200368 ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr);
369 if (ret == -1 || skerr)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200370 ret = -1;
371 else
372 ret = send(fd, b->w, max, MSG_DONTWAIT);
373 }
374#else
375 ret = send(fd, b->w, max, MSG_DONTWAIT | MSG_NOSIGNAL);
376#endif
377
378 if (ret > 0) {
379 b->l -= ret;
380 b->w += ret;
Willy Tarreauf890dc92008-12-13 21:12:26 +0100381 b->send_max -= ret;
Willy Tarreau74ab2ac2008-11-23 17:23:07 +0100382
Willy Tarreaub38903c2008-11-23 21:33:29 +0100383 if (fdtab[fd].state == FD_STCONN)
384 fdtab[fd].state = FD_STREADY;
385
Willy Tarreau3da77c52008-08-29 09:58:42 +0200386 b->flags |= BF_WRITE_PARTIAL;
Willy Tarreaue393fe22008-08-16 22:18:07 +0200387
388 if (b->l < b->rlim - b->data)
389 b->flags &= ~BF_FULL;
Willy Tarreau74ab2ac2008-11-23 17:23:07 +0100390
Willy Tarreaubaaee002006-06-26 02:48:02 +0200391 if (b->w == b->data + BUFSIZE) {
392 b->w = b->data; /* wrap around the buffer */
393 }
Willy Tarreau83749182007-04-15 20:56:27 +0200394
Willy Tarreau6996e152007-04-30 14:37:43 +0200395 if (!b->l) {
Willy Tarreaue393fe22008-08-16 22:18:07 +0200396 b->flags |= BF_EMPTY;
Willy Tarreau48adac52008-08-30 04:58:38 +0200397
398 /* Maybe we just wrote the last chunk and need to close ? */
399 if ((b->flags & (BF_SHUTW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) == (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR)) {
400 if (si->state == SI_ST_EST) {
Willy Tarreau99126c32008-11-27 10:30:51 +0100401 stream_sock_shutw(si);
402 b->wex = TICK_ETERNITY;
403 goto out_wakeup;
Willy Tarreau48adac52008-08-30 04:58:38 +0200404 }
405 }
406
Willy Tarreaub0ef7352008-12-14 13:26:20 +0100407 si->flags |= SI_FL_WAIT_DATA;
Willy Tarreau6996e152007-04-30 14:37:43 +0200408 EV_FD_CLR(fd, DIR_WR);
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200409 b->wex = TICK_ETERNITY;
410 goto out_wakeup;
Willy Tarreau6996e152007-04-30 14:37:43 +0200411 }
Willy Tarreau83749182007-04-15 20:56:27 +0200412
Willy Tarreauab3e1d32007-06-03 14:10:36 +0200413 /* if the system buffer is full, don't insist */
414 if (ret < max)
415 break;
416
Willy Tarreau6996e152007-04-30 14:37:43 +0200417 if (--write_poll <= 0)
418 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200419 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200420 else if (ret == 0 || errno == EAGAIN) {
421 /* nothing written, just pretend we were never called
422 * and wait for the socket to be ready. But we may have
423 * done some work justifying to notify the task.
424 */
Willy Tarreau83749182007-04-15 20:56:27 +0200425 retval = 0;
426 break;
427 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200428 else {
Willy Tarreau6996e152007-04-30 14:37:43 +0200429 goto out_error;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200430 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200431 } /* while (1) */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200432
Willy Tarreau6996e152007-04-30 14:37:43 +0200433 /*
434 * The only way to get out of this loop is to have stopped writing
435 * without any error, either by limiting the number of loops, or
436 * because of an EAGAIN. We only rearm the timer if we have at least
437 * written something.
438 */
439
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100440 if ((b->flags & (BF_WRITE_PARTIAL|BF_EMPTY|BF_SHUTW)) == BF_WRITE_PARTIAL) {
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200441 b->wex = tick_add_ifset(now_ms, b->wto);
Willy Tarreau86491c32008-12-14 09:04:47 +0100442 if (tick_isset(b->wex) & tick_isset(si->ib->rex)) {
Willy Tarreau83749182007-04-15 20:56:27 +0200443 /* FIXME: to prevent the client from expiring read timeouts during writes,
444 * we refresh it. A solution would be to merge read+write timeouts into a
445 * unique one, although that needs some study particularly on full-duplex
446 * TCP connections. */
Willy Tarreau86491c32008-12-14 09:04:47 +0100447 si->ib->rex = b->wex;
Willy Tarreau83749182007-04-15 20:56:27 +0200448 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200449 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200450
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200451 out_may_wakeup:
Willy Tarreau3da77c52008-08-29 09:58:42 +0200452 if (!(b->flags & BF_WRITE_ACTIVITY))
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200453 goto out_skip_wakeup;
Willy Tarreau6996e152007-04-30 14:37:43 +0200454 out_wakeup:
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100455 /* the producer might be waiting for more room to store data */
456 if ((b->prod->flags & SI_FL_WAIT_ROOM) && (b->flags & BF_WRITE_PARTIAL))
457 b->prod->chk_rcv(b->prod);
458
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200459 task_wakeup(si->owner, TASK_WOKEN_IO);
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200460
461 out_skip_wakeup:
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100462 fdtab[fd].ev &= ~FD_POLL_OUT;
Willy Tarreau83749182007-04-15 20:56:27 +0200463 return retval;
Willy Tarreau6996e152007-04-30 14:37:43 +0200464
465 out_error:
Willy Tarreaucff64112008-11-03 06:26:53 +0100466 /* Write error on the file descriptor. We mark the FD as STERROR so
467 * that we don't use it anymore. The error is reported to the stream
468 * interface which will take proper action. We must not perturbate the
469 * buffer because the stream interface wants to ensure transparent
470 * connection retries.
Willy Tarreau6996e152007-04-30 14:37:43 +0200471 */
Willy Tarreaucff64112008-11-03 06:26:53 +0100472
Willy Tarreau6996e152007-04-30 14:37:43 +0200473 fdtab[fd].state = FD_STERROR;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100474 fdtab[fd].ev &= ~FD_POLL_STICKY;
Willy Tarreaucff64112008-11-03 06:26:53 +0100475 si->flags |= SI_FL_ERR;
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200476 task_wakeup(si->owner, TASK_WOKEN_IO);
477 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200478}
479
Willy Tarreau48adac52008-08-30 04:58:38 +0200480/*
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200481 * This function performs a shutdown-write on a stream interface in a connected or
482 * init state (it does nothing for other states). It either shuts the write side
Willy Tarreau99126c32008-11-27 10:30:51 +0100483 * or closes the file descriptor and marks itself as closed. The buffer flags are
484 * updated to reflect the new state.
Willy Tarreau48adac52008-08-30 04:58:38 +0200485 */
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100486void stream_sock_shutw(struct stream_interface *si)
Willy Tarreau48adac52008-08-30 04:58:38 +0200487{
Willy Tarreau99126c32008-11-27 10:30:51 +0100488 if (si->ob->flags & BF_SHUTW)
489 return;
490 si->ob->flags |= BF_SHUTW;
491 si->ob->wex = TICK_ETERNITY;
Willy Tarreaub0ef7352008-12-14 13:26:20 +0100492 si->flags &= ~SI_FL_WAIT_DATA;
Willy Tarreau99126c32008-11-27 10:30:51 +0100493
Willy Tarreaub38903c2008-11-23 21:33:29 +0100494 switch (si->state) {
Willy Tarreaub38903c2008-11-23 21:33:29 +0100495 case SI_ST_EST:
496 if (!(si->ib->flags & BF_SHUTR)) {
497 EV_FD_CLR(si->fd, DIR_WR);
498 shutdown(si->fd, SHUT_WR);
499 return;
500 }
501 /* fall through */
502 case SI_ST_CON:
Willy Tarreaub0ef7352008-12-14 13:26:20 +0100503 si->flags &= ~SI_FL_WAIT_ROOM;
Willy Tarreau8bfa4262008-11-27 09:25:45 +0100504 /* we may have to close a pending connection, and mark the
505 * response buffer as shutr
506 */
Willy Tarreau48adac52008-08-30 04:58:38 +0200507 fd_delete(si->fd);
Willy Tarreaufe3718a2008-11-30 18:14:12 +0100508 /* fall through */
509 case SI_ST_CER:
Willy Tarreau7f006512008-12-07 14:04:04 +0100510 si->state = SI_ST_DIS;
511 default:
Willy Tarreau99126c32008-11-27 10:30:51 +0100512 si->ib->flags |= BF_SHUTR;
Willy Tarreaufe3718a2008-11-30 18:14:12 +0100513 si->ib->rex = TICK_ETERNITY;
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100514 return;
Willy Tarreau48adac52008-08-30 04:58:38 +0200515 }
Willy Tarreau48adac52008-08-30 04:58:38 +0200516}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200517
Willy Tarreau2d212792008-08-27 21:41:35 +0200518/*
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200519 * This function performs a shutdown-read on a stream interface in a connected or
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100520 * init state (it does nothing for other states). It either shuts the read side
Willy Tarreau99126c32008-11-27 10:30:51 +0100521 * or closes the file descriptor and marks itself as closed. The buffer flags are
522 * updated to reflect the new state.
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200523 */
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100524void stream_sock_shutr(struct stream_interface *si)
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200525{
Willy Tarreau99126c32008-11-27 10:30:51 +0100526 if (si->ib->flags & BF_SHUTR)
527 return;
528 si->ib->flags |= BF_SHUTR;
529 si->ib->rex = TICK_ETERNITY;
Willy Tarreaub0ef7352008-12-14 13:26:20 +0100530 si->flags &= ~SI_FL_WAIT_ROOM;
Willy Tarreau99126c32008-11-27 10:30:51 +0100531
Willy Tarreau8bfa4262008-11-27 09:25:45 +0100532 if (si->state != SI_ST_EST && si->state != SI_ST_CON)
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100533 return;
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200534
Willy Tarreaucff64112008-11-03 06:26:53 +0100535 if (si->ob->flags & BF_SHUTW) {
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200536 fd_delete(si->fd);
Willy Tarreau74ab2ac2008-11-23 17:23:07 +0100537 si->state = SI_ST_DIS;
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100538 return;
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200539 }
540 EV_FD_CLR(si->fd, DIR_RD);
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100541 return;
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200542}
543
544/*
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200545 * Updates a connected stream_sock file descriptor status and timeouts
546 * according to the buffers' flags. It should only be called once after the
547 * buffer flags have settled down, and before they are cleared. It doesn't
548 * harm to call it as often as desired (it just slightly hurts performance).
549 */
Willy Tarreaub0253252008-11-30 21:37:12 +0100550void stream_sock_data_finish(struct stream_interface *si)
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200551{
Willy Tarreaub0253252008-11-30 21:37:12 +0100552 struct buffer *ib = si->ib;
553 struct buffer *ob = si->ob;
554 int fd = si->fd;
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200555
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200556 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 +0200557 now_ms, __FUNCTION__,
558 fd, fdtab[fd].owner,
559 ib, ob,
560 ib->rex, ob->wex,
561 ib->flags, ob->flags,
Willy Tarreaub0253252008-11-30 21:37:12 +0100562 ib->l, ob->l, si->state);
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200563
564 /* Check if we need to close the read side */
565 if (!(ib->flags & BF_SHUTR)) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200566 /* Read not closed, update FD status and timeout for reads */
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200567 if (ib->flags & (BF_FULL|BF_HIJACK)) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200568 /* stop reading */
Willy Tarreaub0ef7352008-12-14 13:26:20 +0100569 if ((ib->flags & (BF_FULL|BF_HIJACK)) == BF_FULL)
570 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau2d212792008-08-27 21:41:35 +0200571 EV_FD_COND_C(fd, DIR_RD);
572 ib->rex = TICK_ETERNITY;
573 }
574 else {
575 /* (re)start reading and update timeout. Note: we don't recompute the timeout
576 * everytime we get here, otherwise it would risk never to expire. We only
577 * update it if is was not yet set, or if we already got some read status.
578 */
Willy Tarreaub0ef7352008-12-14 13:26:20 +0100579 si->flags &= ~SI_FL_WAIT_ROOM;
Willy Tarreau2d212792008-08-27 21:41:35 +0200580 EV_FD_COND_S(fd, DIR_RD);
Willy Tarreau86491c32008-12-14 09:04:47 +0100581 if (!(ib->flags & BF_READ_NOEXP) &&
582 (!tick_isset(ib->rex) || ib->flags & BF_READ_ACTIVITY))
Willy Tarreau2d212792008-08-27 21:41:35 +0200583 ib->rex = tick_add_ifset(now_ms, ib->rto);
584 }
585 }
586
587 /* Check if we need to close the write side */
588 if (!(ob->flags & BF_SHUTW)) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200589 /* Write not closed, update FD status and timeout for writes */
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100590 if ((ob->send_max == 0) ||
591 (ob->flags & BF_EMPTY) ||
Willy Tarreau3da77c52008-08-29 09:58:42 +0200592 (ob->flags & (BF_HIJACK|BF_WRITE_ENA)) == 0) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200593 /* stop writing */
Willy Tarreaub0ef7352008-12-14 13:26:20 +0100594 if ((ob->flags & (BF_EMPTY|BF_HIJACK|BF_WRITE_ENA)) == (BF_EMPTY|BF_WRITE_ENA))
595 si->flags |= SI_FL_WAIT_DATA;
Willy Tarreau2d212792008-08-27 21:41:35 +0200596 EV_FD_COND_C(fd, DIR_WR);
597 ob->wex = TICK_ETERNITY;
598 }
599 else {
600 /* (re)start writing and update timeout. Note: we don't recompute the timeout
601 * everytime we get here, otherwise it would risk never to expire. We only
602 * update it if is was not yet set, or if we already got some write status.
603 */
Willy Tarreaub0ef7352008-12-14 13:26:20 +0100604 si->flags &= ~SI_FL_WAIT_DATA;
Willy Tarreau2d212792008-08-27 21:41:35 +0200605 EV_FD_COND_S(fd, DIR_WR);
Willy Tarreau3da77c52008-08-29 09:58:42 +0200606 if (!tick_isset(ob->wex) || ob->flags & BF_WRITE_ACTIVITY) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200607 ob->wex = tick_add_ifset(now_ms, ob->wto);
Willy Tarreau21e1be82008-08-29 11:30:14 +0200608 if (tick_isset(ob->wex) && tick_isset(ib->rex)) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200609 /* Note: depending on the protocol, we don't know if we're waiting
610 * for incoming data or not. So in order to prevent the socket from
611 * expiring read timeouts during writes, we refresh the read timeout,
612 * except if it was already infinite.
613 */
614 ib->rex = ob->wex;
615 }
616 }
617 }
618 }
Willy Tarreau2d212792008-08-27 21:41:35 +0200619}
620
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100621/* This function is used for inter-stream-interface calls. It is called by the
622 * consumer to inform the producer side that it may be interested in checking
623 * for free space in the buffer. Note that it intentionally does not update
624 * timeouts, so that we can still check them later at wake-up.
625 */
626void stream_sock_chk_rcv(struct stream_interface *si)
627{
628 struct buffer *ib = si->ib;
629
630 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",
631 now_ms, __FUNCTION__,
632 fd, fdtab[fd].owner,
633 ib, ob,
634 ib->rex, ob->wex,
635 ib->flags, ob->flags,
636 ib->l, ob->l, si->state);
637
638 if (unlikely(si->state != SI_ST_EST || (ib->flags & BF_SHUTR)))
639 return;
640
641 if (ib->flags & (BF_FULL|BF_HIJACK)) {
642 /* stop reading */
643 if ((ib->flags & (BF_FULL|BF_HIJACK)) == BF_FULL)
644 si->flags |= SI_FL_WAIT_ROOM;
645 EV_FD_COND_C(si->fd, DIR_RD);
646 }
647 else {
648 /* (re)start reading */
649 si->flags &= ~SI_FL_WAIT_ROOM;
650 EV_FD_COND_S(si->fd, DIR_RD);
651 }
652}
653
654
655/* This function is used for inter-stream-interface calls. It is called by the
656 * producer to inform the consumer side that it may be interested in checking
657 * for data in the buffer. Note that it intentionally does not update timeouts,
658 * so that we can still check them later at wake-up.
659 */
660void stream_sock_chk_snd(struct stream_interface *si)
661{
662 struct buffer *ob = si->ob;
663
664 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",
665 now_ms, __FUNCTION__,
666 fd, fdtab[fd].owner,
667 ib, ob,
668 ib->rex, ob->wex,
669 ib->flags, ob->flags,
670 ib->l, ob->l, si->state);
671
672 if (unlikely(si->state != SI_ST_EST || (ob->flags & BF_SHUTW)))
673 return;
674
675 if ((ob->send_max == 0) ||
676 (ob->flags & BF_EMPTY) ||
677 (ob->flags & (BF_HIJACK|BF_WRITE_ENA)) == 0) {
678 /* stop writing */
679 if ((ob->flags & (BF_EMPTY|BF_HIJACK|BF_WRITE_ENA)) == (BF_EMPTY|BF_WRITE_ENA))
680 si->flags |= SI_FL_WAIT_DATA;
681 EV_FD_COND_C(si->fd, DIR_WR);
682 }
683 else {
684 /* (re)start writing. */
685 si->flags &= ~SI_FL_WAIT_DATA;
686 EV_FD_COND_S(si->fd, DIR_WR);
687 }
688}
689
Willy Tarreaubaaee002006-06-26 02:48:02 +0200690
691/*
692 * Local variables:
693 * c-indent-level: 8
694 * c-basic-offset: 8
695 * End:
696 */