blob: 3d0845ca4214426e3b8ee989247723b77c6c066f [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 Tarreau03d60bb2009-01-09 11:13:00 +010068 if (b->l == 0) {
69 /* let's realign the buffer to optimize I/O */
70 b->r = b->w = b->lr = b->data;
71 max = b->max_len;
Willy Tarreau83749182007-04-15 20:56:27 +020072 }
73 else if (b->r > b->w) {
Willy Tarreau03d60bb2009-01-09 11:13:00 +010074 max = b->data + b->max_len - b->r;
Willy Tarreau83749182007-04-15 20:56:27 +020075 }
76 else {
77 max = b->w - b->r;
Willy Tarreau03d60bb2009-01-09 11:13:00 +010078 if (max > b->max_len)
79 max = b->max_len;
Willy Tarreau83749182007-04-15 20:56:27 +020080 }
Willy Tarreau74ab2ac2008-11-23 17:23:07 +010081
Willy Tarreau68eac132009-01-09 11:38:52 +010082 if (max == 0)
83 goto out_full;
Willy Tarreaubaaee002006-06-26 02:48:02 +020084
Willy Tarreau6996e152007-04-30 14:37:43 +020085 /*
86 * 2. read the largest possible block
87 */
Willy Tarreaubaaee002006-06-26 02:48:02 +020088#ifndef MSG_NOSIGNAL
Willy Tarreau83749182007-04-15 20:56:27 +020089 {
90 int skerr;
91 socklen_t lskerr = sizeof(skerr);
92
93 ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr);
94 if (ret == -1 || skerr)
95 ret = -1;
96 else
97 ret = recv(fd, b->r, max, 0);
98 }
Willy Tarreaubaaee002006-06-26 02:48:02 +020099#else
Willy Tarreau83749182007-04-15 20:56:27 +0200100 ret = recv(fd, b->r, max, MSG_NOSIGNAL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200101#endif
Willy Tarreau83749182007-04-15 20:56:27 +0200102 if (ret > 0) {
103 b->r += ret;
104 b->l += ret;
Willy Tarreau8a7af602008-05-03 23:07:14 +0200105 cur_read += ret;
Willy Tarreaub38903c2008-11-23 21:33:29 +0100106
Willy Tarreau0abebcc2009-01-08 00:09:41 +0100107 /* if we're allowed to directly forward data, we must update send_max */
108 if (b->to_forward > 0) {
109 int fwd = MIN(b->to_forward, ret);
110 b->send_max += fwd;
111 b->to_forward -= fwd;
112 }
Willy Tarreauf890dc92008-12-13 21:12:26 +0100113
Willy Tarreaub38903c2008-11-23 21:33:29 +0100114 if (fdtab[fd].state == FD_STCONN)
115 fdtab[fd].state = FD_STREADY;
116
Willy Tarreau3da77c52008-08-29 09:58:42 +0200117 b->flags |= BF_READ_PARTIAL;
Willy Tarreaue393fe22008-08-16 22:18:07 +0200118 b->flags &= ~BF_EMPTY;
Willy Tarreau74ab2ac2008-11-23 17:23:07 +0100119
Willy Tarreau83749182007-04-15 20:56:27 +0200120 if (b->r == b->data + BUFSIZE) {
121 b->r = b->data; /* wrap around the buffer */
122 }
Willy Tarreau9641e8f2007-03-23 23:02:09 +0100123
Willy Tarreau83749182007-04-15 20:56:27 +0200124 b->total += ret;
Willy Tarreau9641e8f2007-03-23 23:02:09 +0100125
Willy Tarreau03d60bb2009-01-09 11:13:00 +0100126 if (b->l >= b->max_len) {
Willy Tarreau6996e152007-04-30 14:37:43 +0200127 /* The buffer is now full, there's no point in going through
128 * the loop again.
129 */
Willy Tarreau8a7af602008-05-03 23:07:14 +0200130 if (!(b->flags & BF_STREAMER_FAST) && (cur_read == b->l)) {
131 b->xfer_small = 0;
132 b->xfer_large++;
133 if (b->xfer_large >= 3) {
134 /* we call this buffer a fast streamer if it manages
135 * to be filled in one call 3 consecutive times.
136 */
137 b->flags |= (BF_STREAMER | BF_STREAMER_FAST);
138 //fputc('+', stderr);
139 }
140 }
141 else if ((b->flags & (BF_STREAMER | BF_STREAMER_FAST)) &&
142 (cur_read <= BUFSIZE / 2)) {
143 b->xfer_large = 0;
144 b->xfer_small++;
145 if (b->xfer_small >= 2) {
146 /* if the buffer has been at least half full twice,
147 * we receive faster than we send, so at least it
148 * is not a "fast streamer".
149 */
150 b->flags &= ~BF_STREAMER_FAST;
151 //fputc('-', stderr);
152 }
153 }
154 else {
155 b->xfer_small = 0;
156 b->xfer_large = 0;
157 }
Willy Tarreau68eac132009-01-09 11:38:52 +0100158 goto out_full;
Willy Tarreau6996e152007-04-30 14:37:43 +0200159 }
160
Willy Tarreauab3e1d32007-06-03 14:10:36 +0200161 /* if too many bytes were missing from last read, it means that
162 * it's pointless trying to read again because the system does
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100163 * not have them in buffers. BTW, if FD_POLL_HUP was present,
164 * it means that we have reached the end and that the connection
165 * is closed.
Willy Tarreauab3e1d32007-06-03 14:10:36 +0200166 */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100167 if (ret < max) {
Willy Tarreau8a7af602008-05-03 23:07:14 +0200168 if ((b->flags & (BF_STREAMER | BF_STREAMER_FAST)) &&
169 (cur_read <= BUFSIZE / 2)) {
170 b->xfer_large = 0;
171 b->xfer_small++;
172 if (b->xfer_small >= 3) {
173 /* we have read less than half of the buffer in
174 * one pass, and this happened at least 3 times.
175 * This is definitely not a streamer.
176 */
177 b->flags &= ~(BF_STREAMER | BF_STREAMER_FAST);
178 //fputc('!', stderr);
179 }
180 }
Willy Tarreau2bea3a12008-08-28 09:47:43 +0200181 /* unfortunately, on level-triggered events, POLL_HUP
182 * is generally delivered AFTER the system buffer is
183 * empty, so this one might never match.
184 */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100185 if (fdtab[fd].ev & FD_POLL_HUP)
186 goto out_shutdown_r;
Willy Tarreau2bea3a12008-08-28 09:47:43 +0200187
188 /* if a streamer has read few data, it may be because we
189 * have exhausted system buffers. It's not worth trying
190 * again.
191 */
192 if (b->flags & BF_STREAMER)
193 break;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100194 }
Willy Tarreauab3e1d32007-06-03 14:10:36 +0200195
196 /* generally if we read something smaller than 1 or 2 MSS,
Willy Tarreau2bea3a12008-08-28 09:47:43 +0200197 * it means that either we have exhausted the system's
198 * buffers (streamer or question-response protocol) or that
199 * the connection will be closed. Streamers are easily
200 * detected so we return early. For other cases, it's still
201 * better to perform a last read to be sure, because it may
202 * save one complete poll/read/wakeup cycle in case of shutdown.
Willy Tarreau83749182007-04-15 20:56:27 +0200203 */
Willy Tarreau2bea3a12008-08-28 09:47:43 +0200204 if (ret < MIN_RET_FOR_READ_LOOP && b->flags & BF_STREAMER)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200205 break;
Willy Tarreau83749182007-04-15 20:56:27 +0200206
Willy Tarreau6996e152007-04-30 14:37:43 +0200207 if (--read_poll <= 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200208 break;
Willy Tarreau83749182007-04-15 20:56:27 +0200209 }
210 else if (ret == 0) {
Willy Tarreau6996e152007-04-30 14:37:43 +0200211 /* connection closed */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100212 goto out_shutdown_r;
Willy Tarreau83749182007-04-15 20:56:27 +0200213 }
Willy Tarreau9f195292007-04-15 21:26:58 +0200214 else if (errno == EAGAIN) {
215 /* Ignore EAGAIN but inform the poller that there is
Willy Tarreauaf78d0f2009-01-08 10:09:08 +0100216 * nothing to read left if we did not read much, ie
217 * less than what we were still expecting to read.
218 * But we may have done some work justifying to notify
219 * the task.
Willy Tarreau9f195292007-04-15 21:26:58 +0200220 */
Willy Tarreauaf78d0f2009-01-08 10:09:08 +0100221 if (cur_read < MIN_RET_FOR_READ_LOOP)
222 retval = 0;
Willy Tarreau83749182007-04-15 20:56:27 +0200223 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 Tarreau3ffeba12008-12-14 14:42:35 +0100237 if ((b->flags & (BF_READ_PARTIAL|BF_FULL|BF_READ_NOEXP)) == 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 Tarreau3ffeba12008-12-14 14:42:35 +0100243 /* the consumer might be waiting for data */
Willy Tarreauefc612c2009-01-09 12:18:24 +0100244 if (likely((b->flags & (BF_READ_PARTIAL|BF_EMPTY)) == BF_READ_PARTIAL &&
245 (b->cons->flags & SI_FL_WAIT_DATA)))
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100246 b->cons->chk_snd(b->cons);
247
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100248 /* we have to wake up if there is a special event or if we don't have
249 * any more data to forward.
250 */
Willy Tarreauefc612c2009-01-09 12:18:24 +0100251 if (likely((b->flags & (BF_READ_NULL|BF_READ_ERROR|BF_SHUTR)) ||
252 !b->to_forward ||
253 si->state != SI_ST_EST ||
254 b->cons->state != SI_ST_EST))
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100255 task_wakeup(si->owner, TASK_WOKEN_IO);
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200256
257 out_skip_wakeup:
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100258 fdtab[fd].ev &= ~FD_POLL_IN;
Willy Tarreau83749182007-04-15 20:56:27 +0200259 return retval;
Willy Tarreau6996e152007-04-30 14:37:43 +0200260
Willy Tarreau68eac132009-01-09 11:38:52 +0100261 out_full:
262 si->flags |= SI_FL_WAIT_ROOM;
263 b->flags |= BF_FULL;
264 EV_FD_CLR(fd, DIR_RD);
265 b->rex = TICK_ETERNITY;
266 goto out_wakeup;
267
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100268 out_shutdown_r:
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200269 /* we received a shutdown */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100270 fdtab[fd].ev &= ~FD_POLL_HUP;
271 b->flags |= BF_READ_NULL;
Willy Tarreau99126c32008-11-27 10:30:51 +0100272 stream_sock_shutr(si);
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200273 goto out_wakeup;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100274
Willy Tarreau6996e152007-04-30 14:37:43 +0200275 out_error:
Willy Tarreaucff64112008-11-03 06:26:53 +0100276 /* Read error on the file descriptor. We mark the FD as STERROR so
277 * that we don't use it anymore. The error is reported to the stream
278 * interface which will take proper action. We must not perturbate the
279 * buffer because the stream interface wants to ensure transparent
280 * connection retries.
Willy Tarreau6996e152007-04-30 14:37:43 +0200281 */
Willy Tarreaucff64112008-11-03 06:26:53 +0100282
Willy Tarreau6996e152007-04-30 14:37:43 +0200283 fdtab[fd].state = FD_STERROR;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100284 fdtab[fd].ev &= ~FD_POLL_STICKY;
Willy Tarreaucff64112008-11-03 06:26:53 +0100285 si->flags |= SI_FL_ERR;
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200286 task_wakeup(si->owner, TASK_WOKEN_IO);
287 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200288}
289
290
291/*
Willy Tarreauf8306d52006-07-29 19:01:31 +0200292 * this function is called on a write event from a stream socket.
Willy Tarreau83749182007-04-15 20:56:27 +0200293 * It returns 0 if we have a high confidence that we will not be
294 * able to write more data without polling first. Returns non-zero
295 * otherwise.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200296 */
Willy Tarreauf8306d52006-07-29 19:01:31 +0200297int stream_sock_write(int fd) {
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200298 __label__ out_wakeup, out_error;
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200299 struct stream_interface *si = fdtab[fd].owner;
Willy Tarreau48adac52008-08-30 04:58:38 +0200300 struct buffer *b = si->ob;
Willy Tarreau83749182007-04-15 20:56:27 +0200301 int ret, max, retval;
302 int write_poll = MAX_WRITE_POLL_LOOPS;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200303
304#ifdef DEBUG_FULL
Willy Tarreauf8306d52006-07-29 19:01:31 +0200305 fprintf(stderr,"stream_sock_write : fd=%d, owner=%p\n", fd, fdtab[fd].owner);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200306#endif
307
Willy Tarreau83749182007-04-15 20:56:27 +0200308 retval = 1;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100309 if (fdtab[fd].state == FD_STERROR || (fdtab[fd].ev & FD_POLL_ERR))
Willy Tarreau6996e152007-04-30 14:37:43 +0200310 goto out_error;
Willy Tarreau83749182007-04-15 20:56:27 +0200311
Willy Tarreau6996e152007-04-30 14:37:43 +0200312 while (1) {
Willy Tarreau83749182007-04-15 20:56:27 +0200313 if (b->l == 0) { /* let's realign the buffer to optimize I/O */
314 b->r = b->w = b->lr = b->data;
315 max = 0;
316 }
317 else if (b->r > b->w) {
318 max = b->r - b->w;
319 }
320 else {
321 max = b->data + BUFSIZE - b->w;
322 }
323
Willy Tarreauf890dc92008-12-13 21:12:26 +0100324 /* limit the amount of outgoing data if required */
325 if (max > b->send_max)
326 max = b->send_max;
327
Willy Tarreaubaaee002006-06-26 02:48:02 +0200328 if (max == 0) {
Willy Tarreauf8306d52006-07-29 19:01:31 +0200329 /* may be we have received a connection acknowledgement in TCP mode without data */
Willy Tarreau6996e152007-04-30 14:37:43 +0200330 if (likely(fdtab[fd].state == FD_STCONN)) {
Willy Tarreau6996e152007-04-30 14:37:43 +0200331 /* We have no data to send to check the connection, and
332 * getsockopt() will not inform us whether the connection
333 * is still pending. So we'll reuse connect() to check the
334 * state of the socket. This has the advantage of givig us
335 * the following info :
336 * - error
337 * - connecting (EALREADY, EINPROGRESS)
338 * - connected (EISCONN, 0)
339 */
Willy Tarreaue94ebd02007-10-09 17:14:37 +0200340 if ((connect(fd, fdtab[fd].peeraddr, fdtab[fd].peerlen) == 0))
Willy Tarreau6996e152007-04-30 14:37:43 +0200341 errno = 0;
342
343 if (errno == EALREADY || errno == EINPROGRESS) {
344 retval = 0;
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200345 goto out_may_wakeup;
Willy Tarreauf8306d52006-07-29 19:01:31 +0200346 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200347
348 if (errno && errno != EISCONN)
349 goto out_error;
350
351 /* OK we just need to indicate that we got a connection
352 * and that we wrote nothing.
353 */
354 b->flags |= BF_WRITE_NULL;
355 fdtab[fd].state = FD_STREADY;
Willy Tarreauf8306d52006-07-29 19:01:31 +0200356 }
357
Willy Tarreau6996e152007-04-30 14:37:43 +0200358 /* Funny, we were called to write something but there wasn't
Willy Tarreau4d9b1de2009-01-07 20:10:39 +0100359 * anything. We can get there, for example if we were woken up
360 * on a write event to finish the splice, but the send_max is 0
361 * so we cannot write anything from the buffer. Let's disable
362 * the write event and pretend we never came there.
Willy Tarreau6996e152007-04-30 14:37:43 +0200363 */
Willy Tarreau4d9b1de2009-01-07 20:10:39 +0100364 goto write_nothing;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200365 }
366
367#ifndef MSG_NOSIGNAL
368 {
369 int skerr;
370 socklen_t lskerr = sizeof(skerr);
371
Willy Tarreauc6423482006-10-15 14:59:03 +0200372 ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr);
373 if (ret == -1 || skerr)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200374 ret = -1;
375 else
376 ret = send(fd, b->w, max, MSG_DONTWAIT);
377 }
378#else
379 ret = send(fd, b->w, max, MSG_DONTWAIT | MSG_NOSIGNAL);
380#endif
381
382 if (ret > 0) {
383 b->l -= ret;
384 b->w += ret;
Willy Tarreauf890dc92008-12-13 21:12:26 +0100385 b->send_max -= ret;
Willy Tarreau74ab2ac2008-11-23 17:23:07 +0100386
Willy Tarreaub38903c2008-11-23 21:33:29 +0100387 if (fdtab[fd].state == FD_STCONN)
388 fdtab[fd].state = FD_STREADY;
389
Willy Tarreau3da77c52008-08-29 09:58:42 +0200390 b->flags |= BF_WRITE_PARTIAL;
Willy Tarreaue393fe22008-08-16 22:18:07 +0200391
Willy Tarreau03d60bb2009-01-09 11:13:00 +0100392 if (b->l < b->max_len)
Willy Tarreaue393fe22008-08-16 22:18:07 +0200393 b->flags &= ~BF_FULL;
Willy Tarreau74ab2ac2008-11-23 17:23:07 +0100394
Willy Tarreaubaaee002006-06-26 02:48:02 +0200395 if (b->w == b->data + BUFSIZE) {
396 b->w = b->data; /* wrap around the buffer */
397 }
Willy Tarreau83749182007-04-15 20:56:27 +0200398
Willy Tarreaudcef33f2009-01-07 19:33:39 +0100399 if (!b->l && !b->splice_len) {
Willy Tarreaue393fe22008-08-16 22:18:07 +0200400 b->flags |= BF_EMPTY;
Willy Tarreau48adac52008-08-30 04:58:38 +0200401
Willy Tarreau4d9b1de2009-01-07 20:10:39 +0100402 write_nothing:
Willy Tarreau48adac52008-08-30 04:58:38 +0200403 /* Maybe we just wrote the last chunk and need to close ? */
404 if ((b->flags & (BF_SHUTW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) == (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR)) {
405 if (si->state == SI_ST_EST) {
Willy Tarreau99126c32008-11-27 10:30:51 +0100406 stream_sock_shutw(si);
407 b->wex = TICK_ETERNITY;
408 goto out_wakeup;
Willy Tarreau48adac52008-08-30 04:58:38 +0200409 }
410 }
411
Willy Tarreau4d9b1de2009-01-07 20:10:39 +0100412 /* we may either get there when the buffer is empty or when
413 * we refrain from sending due to send_max reached.
414 */
415 if (!b->l && !b->splice_len)
416 si->flags |= SI_FL_WAIT_DATA;
Willy Tarreau6996e152007-04-30 14:37:43 +0200417 EV_FD_CLR(fd, DIR_WR);
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200418 b->wex = TICK_ETERNITY;
419 goto out_wakeup;
Willy Tarreau6996e152007-04-30 14:37:43 +0200420 }
Willy Tarreau83749182007-04-15 20:56:27 +0200421
Willy Tarreauab3e1d32007-06-03 14:10:36 +0200422 /* if the system buffer is full, don't insist */
423 if (ret < max)
424 break;
425
Willy Tarreau6996e152007-04-30 14:37:43 +0200426 if (--write_poll <= 0)
427 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200428 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200429 else if (ret == 0 || errno == EAGAIN) {
430 /* nothing written, just pretend we were never called
431 * and wait for the socket to be ready. But we may have
432 * done some work justifying to notify the task.
433 */
Willy Tarreau83749182007-04-15 20:56:27 +0200434 retval = 0;
435 break;
436 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200437 else {
Willy Tarreau6996e152007-04-30 14:37:43 +0200438 goto out_error;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200439 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200440 } /* while (1) */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200441
Willy Tarreau6996e152007-04-30 14:37:43 +0200442 /*
443 * The only way to get out of this loop is to have stopped writing
444 * without any error, either by limiting the number of loops, or
445 * because of an EAGAIN. We only rearm the timer if we have at least
446 * written something.
447 */
448
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100449 if ((b->flags & (BF_WRITE_PARTIAL|BF_EMPTY|BF_SHUTW)) == BF_WRITE_PARTIAL) {
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200450 b->wex = tick_add_ifset(now_ms, b->wto);
Willy Tarreau86491c32008-12-14 09:04:47 +0100451 if (tick_isset(b->wex) & tick_isset(si->ib->rex)) {
Willy Tarreau83749182007-04-15 20:56:27 +0200452 /* FIXME: to prevent the client from expiring read timeouts during writes,
453 * we refresh it. A solution would be to merge read+write timeouts into a
454 * unique one, although that needs some study particularly on full-duplex
455 * TCP connections. */
Willy Tarreau86491c32008-12-14 09:04:47 +0100456 si->ib->rex = b->wex;
Willy Tarreau83749182007-04-15 20:56:27 +0200457 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200458 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200459
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200460 out_may_wakeup:
Willy Tarreau3da77c52008-08-29 09:58:42 +0200461 if (!(b->flags & BF_WRITE_ACTIVITY))
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200462 goto out_skip_wakeup;
Willy Tarreau6996e152007-04-30 14:37:43 +0200463 out_wakeup:
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100464 /* the producer might be waiting for more room to store data */
Willy Tarreauefc612c2009-01-09 12:18:24 +0100465 if (likely((b->flags & (BF_WRITE_PARTIAL|BF_FULL)) == BF_WRITE_PARTIAL &&
466 (b->prod->flags & SI_FL_WAIT_ROOM)))
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100467 b->prod->chk_rcv(b->prod);
468
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100469 /* we have to wake up if there is a special event or if we don't have
Willy Tarreau0abebcc2009-01-08 00:09:41 +0100470 * any more data to forward and it's not planned to send any more.
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100471 */
Willy Tarreauefc612c2009-01-09 12:18:24 +0100472 if (likely((b->flags & (BF_WRITE_NULL|BF_WRITE_ERROR|BF_SHUTW)) ||
473 (!b->to_forward && !b->send_max && !b->splice_len) ||
474 si->state != SI_ST_EST ||
475 b->prod->state != SI_ST_EST))
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100476 task_wakeup(si->owner, TASK_WOKEN_IO);
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200477
478 out_skip_wakeup:
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100479 fdtab[fd].ev &= ~FD_POLL_OUT;
Willy Tarreau83749182007-04-15 20:56:27 +0200480 return retval;
Willy Tarreau6996e152007-04-30 14:37:43 +0200481
482 out_error:
Willy Tarreaucff64112008-11-03 06:26:53 +0100483 /* Write error on the file descriptor. We mark the FD as STERROR so
484 * that we don't use it anymore. The error is reported to the stream
485 * interface which will take proper action. We must not perturbate the
486 * buffer because the stream interface wants to ensure transparent
487 * connection retries.
Willy Tarreau6996e152007-04-30 14:37:43 +0200488 */
Willy Tarreaucff64112008-11-03 06:26:53 +0100489
Willy Tarreau6996e152007-04-30 14:37:43 +0200490 fdtab[fd].state = FD_STERROR;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100491 fdtab[fd].ev &= ~FD_POLL_STICKY;
Willy Tarreaucff64112008-11-03 06:26:53 +0100492 si->flags |= SI_FL_ERR;
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200493 task_wakeup(si->owner, TASK_WOKEN_IO);
494 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200495}
496
Willy Tarreau48adac52008-08-30 04:58:38 +0200497/*
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200498 * This function performs a shutdown-write on a stream interface in a connected or
499 * init state (it does nothing for other states). It either shuts the write side
Willy Tarreau99126c32008-11-27 10:30:51 +0100500 * or closes the file descriptor and marks itself as closed. The buffer flags are
501 * updated to reflect the new state.
Willy Tarreau48adac52008-08-30 04:58:38 +0200502 */
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100503void stream_sock_shutw(struct stream_interface *si)
Willy Tarreau48adac52008-08-30 04:58:38 +0200504{
Willy Tarreau99126c32008-11-27 10:30:51 +0100505 if (si->ob->flags & BF_SHUTW)
506 return;
507 si->ob->flags |= BF_SHUTW;
508 si->ob->wex = TICK_ETERNITY;
Willy Tarreaub0ef7352008-12-14 13:26:20 +0100509 si->flags &= ~SI_FL_WAIT_DATA;
Willy Tarreau99126c32008-11-27 10:30:51 +0100510
Willy Tarreaub38903c2008-11-23 21:33:29 +0100511 switch (si->state) {
Willy Tarreaub38903c2008-11-23 21:33:29 +0100512 case SI_ST_EST:
513 if (!(si->ib->flags & BF_SHUTR)) {
514 EV_FD_CLR(si->fd, DIR_WR);
515 shutdown(si->fd, SHUT_WR);
516 return;
517 }
518 /* fall through */
519 case SI_ST_CON:
Willy Tarreaub0ef7352008-12-14 13:26:20 +0100520 si->flags &= ~SI_FL_WAIT_ROOM;
Willy Tarreau8bfa4262008-11-27 09:25:45 +0100521 /* we may have to close a pending connection, and mark the
522 * response buffer as shutr
523 */
Willy Tarreau48adac52008-08-30 04:58:38 +0200524 fd_delete(si->fd);
Willy Tarreaufe3718a2008-11-30 18:14:12 +0100525 /* fall through */
526 case SI_ST_CER:
Willy Tarreau7f006512008-12-07 14:04:04 +0100527 si->state = SI_ST_DIS;
528 default:
Willy Tarreau99126c32008-11-27 10:30:51 +0100529 si->ib->flags |= BF_SHUTR;
Willy Tarreaufe3718a2008-11-30 18:14:12 +0100530 si->ib->rex = TICK_ETERNITY;
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100531 return;
Willy Tarreau48adac52008-08-30 04:58:38 +0200532 }
Willy Tarreau48adac52008-08-30 04:58:38 +0200533}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200534
Willy Tarreau2d212792008-08-27 21:41:35 +0200535/*
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200536 * This function performs a shutdown-read on a stream interface in a connected or
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100537 * init state (it does nothing for other states). It either shuts the read side
Willy Tarreau99126c32008-11-27 10:30:51 +0100538 * or closes the file descriptor and marks itself as closed. The buffer flags are
539 * updated to reflect the new state.
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200540 */
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100541void stream_sock_shutr(struct stream_interface *si)
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200542{
Willy Tarreau99126c32008-11-27 10:30:51 +0100543 if (si->ib->flags & BF_SHUTR)
544 return;
545 si->ib->flags |= BF_SHUTR;
546 si->ib->rex = TICK_ETERNITY;
Willy Tarreaub0ef7352008-12-14 13:26:20 +0100547 si->flags &= ~SI_FL_WAIT_ROOM;
Willy Tarreau99126c32008-11-27 10:30:51 +0100548
Willy Tarreau8bfa4262008-11-27 09:25:45 +0100549 if (si->state != SI_ST_EST && si->state != SI_ST_CON)
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100550 return;
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200551
Willy Tarreaucff64112008-11-03 06:26:53 +0100552 if (si->ob->flags & BF_SHUTW) {
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200553 fd_delete(si->fd);
Willy Tarreau74ab2ac2008-11-23 17:23:07 +0100554 si->state = SI_ST_DIS;
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100555 return;
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200556 }
557 EV_FD_CLR(si->fd, DIR_RD);
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100558 return;
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200559}
560
561/*
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200562 * Updates a connected stream_sock file descriptor status and timeouts
563 * according to the buffers' flags. It should only be called once after the
564 * buffer flags have settled down, and before they are cleared. It doesn't
565 * harm to call it as often as desired (it just slightly hurts performance).
566 */
Willy Tarreaub0253252008-11-30 21:37:12 +0100567void stream_sock_data_finish(struct stream_interface *si)
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200568{
Willy Tarreaub0253252008-11-30 21:37:12 +0100569 struct buffer *ib = si->ib;
570 struct buffer *ob = si->ob;
571 int fd = si->fd;
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200572
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200573 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 +0200574 now_ms, __FUNCTION__,
575 fd, fdtab[fd].owner,
576 ib, ob,
577 ib->rex, ob->wex,
578 ib->flags, ob->flags,
Willy Tarreaub0253252008-11-30 21:37:12 +0100579 ib->l, ob->l, si->state);
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200580
581 /* Check if we need to close the read side */
582 if (!(ib->flags & BF_SHUTR)) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200583 /* Read not closed, update FD status and timeout for reads */
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200584 if (ib->flags & (BF_FULL|BF_HIJACK)) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200585 /* stop reading */
Willy Tarreaub0ef7352008-12-14 13:26:20 +0100586 if ((ib->flags & (BF_FULL|BF_HIJACK)) == BF_FULL)
587 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau2d212792008-08-27 21:41:35 +0200588 EV_FD_COND_C(fd, DIR_RD);
589 ib->rex = TICK_ETERNITY;
590 }
591 else {
592 /* (re)start reading and update timeout. Note: we don't recompute the timeout
593 * everytime we get here, otherwise it would risk never to expire. We only
594 * update it if is was not yet set, or if we already got some read status.
595 */
Willy Tarreaub0ef7352008-12-14 13:26:20 +0100596 si->flags &= ~SI_FL_WAIT_ROOM;
Willy Tarreau2d212792008-08-27 21:41:35 +0200597 EV_FD_COND_S(fd, DIR_RD);
Willy Tarreau86491c32008-12-14 09:04:47 +0100598 if (!(ib->flags & BF_READ_NOEXP) &&
599 (!tick_isset(ib->rex) || ib->flags & BF_READ_ACTIVITY))
Willy Tarreau2d212792008-08-27 21:41:35 +0200600 ib->rex = tick_add_ifset(now_ms, ib->rto);
601 }
602 }
603
604 /* Check if we need to close the write side */
605 if (!(ob->flags & BF_SHUTW)) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200606 /* Write not closed, update FD status and timeout for writes */
Willy Tarreaudcef33f2009-01-07 19:33:39 +0100607 if ((ob->send_max == 0 && ob->splice_len == 0) ||
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100608 (ob->flags & BF_EMPTY) ||
Willy Tarreau3da77c52008-08-29 09:58:42 +0200609 (ob->flags & (BF_HIJACK|BF_WRITE_ENA)) == 0) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200610 /* stop writing */
Willy Tarreaub0ef7352008-12-14 13:26:20 +0100611 if ((ob->flags & (BF_EMPTY|BF_HIJACK|BF_WRITE_ENA)) == (BF_EMPTY|BF_WRITE_ENA))
612 si->flags |= SI_FL_WAIT_DATA;
Willy Tarreau2d212792008-08-27 21:41:35 +0200613 EV_FD_COND_C(fd, DIR_WR);
614 ob->wex = TICK_ETERNITY;
615 }
616 else {
617 /* (re)start writing and update timeout. Note: we don't recompute the timeout
618 * everytime we get here, otherwise it would risk never to expire. We only
619 * update it if is was not yet set, or if we already got some write status.
620 */
Willy Tarreaub0ef7352008-12-14 13:26:20 +0100621 si->flags &= ~SI_FL_WAIT_DATA;
Willy Tarreau2d212792008-08-27 21:41:35 +0200622 EV_FD_COND_S(fd, DIR_WR);
Willy Tarreau3da77c52008-08-29 09:58:42 +0200623 if (!tick_isset(ob->wex) || ob->flags & BF_WRITE_ACTIVITY) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200624 ob->wex = tick_add_ifset(now_ms, ob->wto);
Willy Tarreau21e1be82008-08-29 11:30:14 +0200625 if (tick_isset(ob->wex) && tick_isset(ib->rex)) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200626 /* Note: depending on the protocol, we don't know if we're waiting
627 * for incoming data or not. So in order to prevent the socket from
628 * expiring read timeouts during writes, we refresh the read timeout,
629 * except if it was already infinite.
630 */
631 ib->rex = ob->wex;
632 }
633 }
634 }
635 }
Willy Tarreau2d212792008-08-27 21:41:35 +0200636}
637
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100638/* This function is used for inter-stream-interface calls. It is called by the
639 * consumer to inform the producer side that it may be interested in checking
640 * for free space in the buffer. Note that it intentionally does not update
641 * timeouts, so that we can still check them later at wake-up.
642 */
643void stream_sock_chk_rcv(struct stream_interface *si)
644{
645 struct buffer *ib = si->ib;
646
647 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",
648 now_ms, __FUNCTION__,
649 fd, fdtab[fd].owner,
650 ib, ob,
651 ib->rex, ob->wex,
652 ib->flags, ob->flags,
653 ib->l, ob->l, si->state);
654
655 if (unlikely(si->state != SI_ST_EST || (ib->flags & BF_SHUTR)))
656 return;
657
658 if (ib->flags & (BF_FULL|BF_HIJACK)) {
659 /* stop reading */
660 if ((ib->flags & (BF_FULL|BF_HIJACK)) == BF_FULL)
661 si->flags |= SI_FL_WAIT_ROOM;
662 EV_FD_COND_C(si->fd, DIR_RD);
663 }
664 else {
665 /* (re)start reading */
666 si->flags &= ~SI_FL_WAIT_ROOM;
667 EV_FD_COND_S(si->fd, DIR_RD);
668 }
669}
670
671
672/* This function is used for inter-stream-interface calls. It is called by the
673 * producer to inform the consumer side that it may be interested in checking
674 * for data in the buffer. Note that it intentionally does not update timeouts,
675 * so that we can still check them later at wake-up.
676 */
677void stream_sock_chk_snd(struct stream_interface *si)
678{
679 struct buffer *ob = si->ob;
680
681 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",
682 now_ms, __FUNCTION__,
683 fd, fdtab[fd].owner,
684 ib, ob,
685 ib->rex, ob->wex,
686 ib->flags, ob->flags,
687 ib->l, ob->l, si->state);
688
689 if (unlikely(si->state != SI_ST_EST || (ob->flags & BF_SHUTW)))
690 return;
691
Willy Tarreaudcef33f2009-01-07 19:33:39 +0100692 if ((ob->send_max == 0 && ob->splice_len == 0) ||
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100693 (ob->flags & BF_EMPTY) ||
694 (ob->flags & (BF_HIJACK|BF_WRITE_ENA)) == 0) {
695 /* stop writing */
696 if ((ob->flags & (BF_EMPTY|BF_HIJACK|BF_WRITE_ENA)) == (BF_EMPTY|BF_WRITE_ENA))
697 si->flags |= SI_FL_WAIT_DATA;
698 EV_FD_COND_C(si->fd, DIR_WR);
699 }
700 else {
701 /* (re)start writing. */
702 si->flags &= ~SI_FL_WAIT_DATA;
703 EV_FD_COND_S(si->fd, DIR_WR);
704 }
705}
706
Willy Tarreaubaaee002006-06-26 02:48:02 +0200707
708/*
709 * Local variables:
710 * c-indent-level: 8
711 * c-basic-offset: 8
712 * End:
713 */