blob: 16d7c5404ee41a03db04168d10af6826c754ae8f [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 Tarreau6996e152007-04-30 14:37:43 +020082 if (unlikely(max == 0)) {
83 /* Not anymore room to store data. This should theorically
84 * never happen, but better safe than sorry !
85 */
Willy Tarreaub0ef7352008-12-14 13:26:20 +010086 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreaue393fe22008-08-16 22:18:07 +020087 b->flags |= BF_FULL;
Willy Tarreau83749182007-04-15 20:56:27 +020088 EV_FD_CLR(fd, DIR_RD);
Willy Tarreau0c303ee2008-07-07 00:09:58 +020089 b->rex = TICK_ETERNITY;
90 goto out_wakeup;
Willy Tarreau83749182007-04-15 20:56:27 +020091 }
Willy Tarreaubaaee002006-06-26 02:48:02 +020092
Willy Tarreau6996e152007-04-30 14:37:43 +020093 /*
94 * 2. read the largest possible block
95 */
Willy Tarreaubaaee002006-06-26 02:48:02 +020096#ifndef MSG_NOSIGNAL
Willy Tarreau83749182007-04-15 20:56:27 +020097 {
98 int skerr;
99 socklen_t lskerr = sizeof(skerr);
100
101 ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr);
102 if (ret == -1 || skerr)
103 ret = -1;
104 else
105 ret = recv(fd, b->r, max, 0);
106 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200107#else
Willy Tarreau83749182007-04-15 20:56:27 +0200108 ret = recv(fd, b->r, max, MSG_NOSIGNAL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200109#endif
Willy Tarreau83749182007-04-15 20:56:27 +0200110 if (ret > 0) {
111 b->r += ret;
112 b->l += ret;
Willy Tarreau8a7af602008-05-03 23:07:14 +0200113 cur_read += ret;
Willy Tarreaub38903c2008-11-23 21:33:29 +0100114
Willy Tarreau0abebcc2009-01-08 00:09:41 +0100115 /* if we're allowed to directly forward data, we must update send_max */
116 if (b->to_forward > 0) {
117 int fwd = MIN(b->to_forward, ret);
118 b->send_max += fwd;
119 b->to_forward -= fwd;
120 }
Willy Tarreauf890dc92008-12-13 21:12:26 +0100121
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 Tarreau03d60bb2009-01-09 11:13:00 +0100134 if (b->l >= b->max_len) {
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 Tarreauaf78d0f2009-01-08 10:09:08 +0100229 * nothing to read left if we did not read much, ie
230 * less than what we were still expecting to read.
231 * But we may have done some work justifying to notify
232 * the task.
Willy Tarreau9f195292007-04-15 21:26:58 +0200233 */
Willy Tarreauaf78d0f2009-01-08 10:09:08 +0100234 if (cur_read < MIN_RET_FOR_READ_LOOP)
235 retval = 0;
Willy Tarreau83749182007-04-15 20:56:27 +0200236 break;
237 }
238 else {
Willy Tarreau6996e152007-04-30 14:37:43 +0200239 goto out_error;
Willy Tarreau83749182007-04-15 20:56:27 +0200240 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200241 } /* while (1) */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200242
Willy Tarreau6996e152007-04-30 14:37:43 +0200243 /*
244 * The only way to get out of this loop is to have stopped reading
245 * without any error nor close, either by limiting the number of
246 * loops, or because of an EAGAIN. We only rearm the timer if we
247 * have at least read something.
248 */
249
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100250 if ((b->flags & (BF_READ_PARTIAL|BF_FULL|BF_READ_NOEXP)) == BF_READ_PARTIAL)
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200251 b->rex = tick_add_ifset(now_ms, b->rto);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200252
Willy Tarreau3da77c52008-08-29 09:58:42 +0200253 if (!(b->flags & BF_READ_ACTIVITY))
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200254 goto out_skip_wakeup;
Willy Tarreau6996e152007-04-30 14:37:43 +0200255 out_wakeup:
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100256 /* the consumer might be waiting for data */
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100257 if (b->cons->flags & SI_FL_WAIT_DATA && (b->flags & BF_READ_PARTIAL) && !(b->flags & BF_EMPTY))
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100258 b->cons->chk_snd(b->cons);
259
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100260 /* we have to wake up if there is a special event or if we don't have
261 * any more data to forward.
262 */
263 if ((b->flags & (BF_READ_NULL|BF_READ_ERROR|BF_SHUTR)) ||
264 !b->to_forward ||
265 si->state != SI_ST_EST ||
266 b->cons->state != SI_ST_EST)
267 task_wakeup(si->owner, TASK_WOKEN_IO);
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200268
269 out_skip_wakeup:
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100270 fdtab[fd].ev &= ~FD_POLL_IN;
Willy Tarreau83749182007-04-15 20:56:27 +0200271 return retval;
Willy Tarreau6996e152007-04-30 14:37:43 +0200272
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100273 out_shutdown_r:
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200274 /* we received a shutdown */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100275 fdtab[fd].ev &= ~FD_POLL_HUP;
276 b->flags |= BF_READ_NULL;
Willy Tarreau99126c32008-11-27 10:30:51 +0100277 stream_sock_shutr(si);
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200278 goto out_wakeup;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100279
Willy Tarreau6996e152007-04-30 14:37:43 +0200280 out_error:
Willy Tarreaucff64112008-11-03 06:26:53 +0100281 /* Read error on the file descriptor. We mark the FD as STERROR so
282 * that we don't use it anymore. The error is reported to the stream
283 * interface which will take proper action. We must not perturbate the
284 * buffer because the stream interface wants to ensure transparent
285 * connection retries.
Willy Tarreau6996e152007-04-30 14:37:43 +0200286 */
Willy Tarreaucff64112008-11-03 06:26:53 +0100287
Willy Tarreau6996e152007-04-30 14:37:43 +0200288 fdtab[fd].state = FD_STERROR;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100289 fdtab[fd].ev &= ~FD_POLL_STICKY;
Willy Tarreaucff64112008-11-03 06:26:53 +0100290 si->flags |= SI_FL_ERR;
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200291 task_wakeup(si->owner, TASK_WOKEN_IO);
292 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200293}
294
295
296/*
Willy Tarreauf8306d52006-07-29 19:01:31 +0200297 * this function is called on a write event from a stream socket.
Willy Tarreau83749182007-04-15 20:56:27 +0200298 * It returns 0 if we have a high confidence that we will not be
299 * able to write more data without polling first. Returns non-zero
300 * otherwise.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200301 */
Willy Tarreauf8306d52006-07-29 19:01:31 +0200302int stream_sock_write(int fd) {
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200303 __label__ out_wakeup, out_error;
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200304 struct stream_interface *si = fdtab[fd].owner;
Willy Tarreau48adac52008-08-30 04:58:38 +0200305 struct buffer *b = si->ob;
Willy Tarreau83749182007-04-15 20:56:27 +0200306 int ret, max, retval;
307 int write_poll = MAX_WRITE_POLL_LOOPS;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200308
309#ifdef DEBUG_FULL
Willy Tarreauf8306d52006-07-29 19:01:31 +0200310 fprintf(stderr,"stream_sock_write : fd=%d, owner=%p\n", fd, fdtab[fd].owner);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200311#endif
312
Willy Tarreau83749182007-04-15 20:56:27 +0200313 retval = 1;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100314 if (fdtab[fd].state == FD_STERROR || (fdtab[fd].ev & FD_POLL_ERR))
Willy Tarreau6996e152007-04-30 14:37:43 +0200315 goto out_error;
Willy Tarreau83749182007-04-15 20:56:27 +0200316
Willy Tarreau6996e152007-04-30 14:37:43 +0200317 while (1) {
Willy Tarreau83749182007-04-15 20:56:27 +0200318 if (b->l == 0) { /* let's realign the buffer to optimize I/O */
319 b->r = b->w = b->lr = b->data;
320 max = 0;
321 }
322 else if (b->r > b->w) {
323 max = b->r - b->w;
324 }
325 else {
326 max = b->data + BUFSIZE - b->w;
327 }
328
Willy Tarreauf890dc92008-12-13 21:12:26 +0100329 /* limit the amount of outgoing data if required */
330 if (max > b->send_max)
331 max = b->send_max;
332
Willy Tarreaubaaee002006-06-26 02:48:02 +0200333 if (max == 0) {
Willy Tarreauf8306d52006-07-29 19:01:31 +0200334 /* may be we have received a connection acknowledgement in TCP mode without data */
Willy Tarreau6996e152007-04-30 14:37:43 +0200335 if (likely(fdtab[fd].state == FD_STCONN)) {
Willy Tarreau6996e152007-04-30 14:37:43 +0200336 /* We have no data to send to check the connection, and
337 * getsockopt() will not inform us whether the connection
338 * is still pending. So we'll reuse connect() to check the
339 * state of the socket. This has the advantage of givig us
340 * the following info :
341 * - error
342 * - connecting (EALREADY, EINPROGRESS)
343 * - connected (EISCONN, 0)
344 */
Willy Tarreaue94ebd02007-10-09 17:14:37 +0200345 if ((connect(fd, fdtab[fd].peeraddr, fdtab[fd].peerlen) == 0))
Willy Tarreau6996e152007-04-30 14:37:43 +0200346 errno = 0;
347
348 if (errno == EALREADY || errno == EINPROGRESS) {
349 retval = 0;
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200350 goto out_may_wakeup;
Willy Tarreauf8306d52006-07-29 19:01:31 +0200351 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200352
353 if (errno && errno != EISCONN)
354 goto out_error;
355
356 /* OK we just need to indicate that we got a connection
357 * and that we wrote nothing.
358 */
359 b->flags |= BF_WRITE_NULL;
360 fdtab[fd].state = FD_STREADY;
Willy Tarreauf8306d52006-07-29 19:01:31 +0200361 }
362
Willy Tarreau6996e152007-04-30 14:37:43 +0200363 /* Funny, we were called to write something but there wasn't
Willy Tarreau4d9b1de2009-01-07 20:10:39 +0100364 * anything. We can get there, for example if we were woken up
365 * on a write event to finish the splice, but the send_max is 0
366 * so we cannot write anything from the buffer. Let's disable
367 * the write event and pretend we never came there.
Willy Tarreau6996e152007-04-30 14:37:43 +0200368 */
Willy Tarreau4d9b1de2009-01-07 20:10:39 +0100369 goto write_nothing;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200370 }
371
372#ifndef MSG_NOSIGNAL
373 {
374 int skerr;
375 socklen_t lskerr = sizeof(skerr);
376
Willy Tarreauc6423482006-10-15 14:59:03 +0200377 ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr);
378 if (ret == -1 || skerr)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200379 ret = -1;
380 else
381 ret = send(fd, b->w, max, MSG_DONTWAIT);
382 }
383#else
384 ret = send(fd, b->w, max, MSG_DONTWAIT | MSG_NOSIGNAL);
385#endif
386
387 if (ret > 0) {
388 b->l -= ret;
389 b->w += ret;
Willy Tarreauf890dc92008-12-13 21:12:26 +0100390 b->send_max -= ret;
Willy Tarreau74ab2ac2008-11-23 17:23:07 +0100391
Willy Tarreaub38903c2008-11-23 21:33:29 +0100392 if (fdtab[fd].state == FD_STCONN)
393 fdtab[fd].state = FD_STREADY;
394
Willy Tarreau3da77c52008-08-29 09:58:42 +0200395 b->flags |= BF_WRITE_PARTIAL;
Willy Tarreaue393fe22008-08-16 22:18:07 +0200396
Willy Tarreau03d60bb2009-01-09 11:13:00 +0100397 if (b->l < b->max_len)
Willy Tarreaue393fe22008-08-16 22:18:07 +0200398 b->flags &= ~BF_FULL;
Willy Tarreau74ab2ac2008-11-23 17:23:07 +0100399
Willy Tarreaubaaee002006-06-26 02:48:02 +0200400 if (b->w == b->data + BUFSIZE) {
401 b->w = b->data; /* wrap around the buffer */
402 }
Willy Tarreau83749182007-04-15 20:56:27 +0200403
Willy Tarreaudcef33f2009-01-07 19:33:39 +0100404 if (!b->l && !b->splice_len) {
Willy Tarreaue393fe22008-08-16 22:18:07 +0200405 b->flags |= BF_EMPTY;
Willy Tarreau48adac52008-08-30 04:58:38 +0200406
Willy Tarreau4d9b1de2009-01-07 20:10:39 +0100407 write_nothing:
Willy Tarreau48adac52008-08-30 04:58:38 +0200408 /* Maybe we just wrote the last chunk and need to close ? */
409 if ((b->flags & (BF_SHUTW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) == (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR)) {
410 if (si->state == SI_ST_EST) {
Willy Tarreau99126c32008-11-27 10:30:51 +0100411 stream_sock_shutw(si);
412 b->wex = TICK_ETERNITY;
413 goto out_wakeup;
Willy Tarreau48adac52008-08-30 04:58:38 +0200414 }
415 }
416
Willy Tarreau4d9b1de2009-01-07 20:10:39 +0100417 /* we may either get there when the buffer is empty or when
418 * we refrain from sending due to send_max reached.
419 */
420 if (!b->l && !b->splice_len)
421 si->flags |= SI_FL_WAIT_DATA;
Willy Tarreau6996e152007-04-30 14:37:43 +0200422 EV_FD_CLR(fd, DIR_WR);
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200423 b->wex = TICK_ETERNITY;
424 goto out_wakeup;
Willy Tarreau6996e152007-04-30 14:37:43 +0200425 }
Willy Tarreau83749182007-04-15 20:56:27 +0200426
Willy Tarreauab3e1d32007-06-03 14:10:36 +0200427 /* if the system buffer is full, don't insist */
428 if (ret < max)
429 break;
430
Willy Tarreau6996e152007-04-30 14:37:43 +0200431 if (--write_poll <= 0)
432 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200433 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200434 else if (ret == 0 || errno == EAGAIN) {
435 /* nothing written, just pretend we were never called
436 * and wait for the socket to be ready. But we may have
437 * done some work justifying to notify the task.
438 */
Willy Tarreau83749182007-04-15 20:56:27 +0200439 retval = 0;
440 break;
441 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200442 else {
Willy Tarreau6996e152007-04-30 14:37:43 +0200443 goto out_error;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200444 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200445 } /* while (1) */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200446
Willy Tarreau6996e152007-04-30 14:37:43 +0200447 /*
448 * The only way to get out of this loop is to have stopped writing
449 * without any error, either by limiting the number of loops, or
450 * because of an EAGAIN. We only rearm the timer if we have at least
451 * written something.
452 */
453
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100454 if ((b->flags & (BF_WRITE_PARTIAL|BF_EMPTY|BF_SHUTW)) == BF_WRITE_PARTIAL) {
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200455 b->wex = tick_add_ifset(now_ms, b->wto);
Willy Tarreau86491c32008-12-14 09:04:47 +0100456 if (tick_isset(b->wex) & tick_isset(si->ib->rex)) {
Willy Tarreau83749182007-04-15 20:56:27 +0200457 /* FIXME: to prevent the client from expiring read timeouts during writes,
458 * we refresh it. A solution would be to merge read+write timeouts into a
459 * unique one, although that needs some study particularly on full-duplex
460 * TCP connections. */
Willy Tarreau86491c32008-12-14 09:04:47 +0100461 si->ib->rex = b->wex;
Willy Tarreau83749182007-04-15 20:56:27 +0200462 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200463 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200464
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200465 out_may_wakeup:
Willy Tarreau3da77c52008-08-29 09:58:42 +0200466 if (!(b->flags & BF_WRITE_ACTIVITY))
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200467 goto out_skip_wakeup;
Willy Tarreau6996e152007-04-30 14:37:43 +0200468 out_wakeup:
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100469 /* the producer might be waiting for more room to store data */
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100470 if ((b->prod->flags & SI_FL_WAIT_ROOM) && (b->flags & BF_WRITE_PARTIAL) && !(b->flags & BF_FULL))
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100471 b->prod->chk_rcv(b->prod);
472
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100473 /* we have to wake up if there is a special event or if we don't have
Willy Tarreau0abebcc2009-01-08 00:09:41 +0100474 * any more data to forward and it's not planned to send any more.
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100475 */
476 if ((b->flags & (BF_WRITE_NULL|BF_WRITE_ERROR|BF_SHUTW)) ||
Willy Tarreau0abebcc2009-01-08 00:09:41 +0100477 (!b->to_forward && !b->send_max && !b->splice_len) ||
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100478 si->state != SI_ST_EST ||
479 b->prod->state != SI_ST_EST)
480 task_wakeup(si->owner, TASK_WOKEN_IO);
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200481
482 out_skip_wakeup:
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100483 fdtab[fd].ev &= ~FD_POLL_OUT;
Willy Tarreau83749182007-04-15 20:56:27 +0200484 return retval;
Willy Tarreau6996e152007-04-30 14:37:43 +0200485
486 out_error:
Willy Tarreaucff64112008-11-03 06:26:53 +0100487 /* Write error on the file descriptor. We mark the FD as STERROR so
488 * that we don't use it anymore. The error is reported to the stream
489 * interface which will take proper action. We must not perturbate the
490 * buffer because the stream interface wants to ensure transparent
491 * connection retries.
Willy Tarreau6996e152007-04-30 14:37:43 +0200492 */
Willy Tarreaucff64112008-11-03 06:26:53 +0100493
Willy Tarreau6996e152007-04-30 14:37:43 +0200494 fdtab[fd].state = FD_STERROR;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100495 fdtab[fd].ev &= ~FD_POLL_STICKY;
Willy Tarreaucff64112008-11-03 06:26:53 +0100496 si->flags |= SI_FL_ERR;
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200497 task_wakeup(si->owner, TASK_WOKEN_IO);
498 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200499}
500
Willy Tarreau48adac52008-08-30 04:58:38 +0200501/*
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200502 * This function performs a shutdown-write on a stream interface in a connected or
503 * init state (it does nothing for other states). It either shuts the write side
Willy Tarreau99126c32008-11-27 10:30:51 +0100504 * or closes the file descriptor and marks itself as closed. The buffer flags are
505 * updated to reflect the new state.
Willy Tarreau48adac52008-08-30 04:58:38 +0200506 */
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100507void stream_sock_shutw(struct stream_interface *si)
Willy Tarreau48adac52008-08-30 04:58:38 +0200508{
Willy Tarreau99126c32008-11-27 10:30:51 +0100509 if (si->ob->flags & BF_SHUTW)
510 return;
511 si->ob->flags |= BF_SHUTW;
512 si->ob->wex = TICK_ETERNITY;
Willy Tarreaub0ef7352008-12-14 13:26:20 +0100513 si->flags &= ~SI_FL_WAIT_DATA;
Willy Tarreau99126c32008-11-27 10:30:51 +0100514
Willy Tarreaub38903c2008-11-23 21:33:29 +0100515 switch (si->state) {
Willy Tarreaub38903c2008-11-23 21:33:29 +0100516 case SI_ST_EST:
517 if (!(si->ib->flags & BF_SHUTR)) {
518 EV_FD_CLR(si->fd, DIR_WR);
519 shutdown(si->fd, SHUT_WR);
520 return;
521 }
522 /* fall through */
523 case SI_ST_CON:
Willy Tarreaub0ef7352008-12-14 13:26:20 +0100524 si->flags &= ~SI_FL_WAIT_ROOM;
Willy Tarreau8bfa4262008-11-27 09:25:45 +0100525 /* we may have to close a pending connection, and mark the
526 * response buffer as shutr
527 */
Willy Tarreau48adac52008-08-30 04:58:38 +0200528 fd_delete(si->fd);
Willy Tarreaufe3718a2008-11-30 18:14:12 +0100529 /* fall through */
530 case SI_ST_CER:
Willy Tarreau7f006512008-12-07 14:04:04 +0100531 si->state = SI_ST_DIS;
532 default:
Willy Tarreau99126c32008-11-27 10:30:51 +0100533 si->ib->flags |= BF_SHUTR;
Willy Tarreaufe3718a2008-11-30 18:14:12 +0100534 si->ib->rex = TICK_ETERNITY;
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100535 return;
Willy Tarreau48adac52008-08-30 04:58:38 +0200536 }
Willy Tarreau48adac52008-08-30 04:58:38 +0200537}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200538
Willy Tarreau2d212792008-08-27 21:41:35 +0200539/*
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200540 * This function performs a shutdown-read on a stream interface in a connected or
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100541 * init state (it does nothing for other states). It either shuts the read side
Willy Tarreau99126c32008-11-27 10:30:51 +0100542 * or closes the file descriptor and marks itself as closed. The buffer flags are
543 * updated to reflect the new state.
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200544 */
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100545void stream_sock_shutr(struct stream_interface *si)
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200546{
Willy Tarreau99126c32008-11-27 10:30:51 +0100547 if (si->ib->flags & BF_SHUTR)
548 return;
549 si->ib->flags |= BF_SHUTR;
550 si->ib->rex = TICK_ETERNITY;
Willy Tarreaub0ef7352008-12-14 13:26:20 +0100551 si->flags &= ~SI_FL_WAIT_ROOM;
Willy Tarreau99126c32008-11-27 10:30:51 +0100552
Willy Tarreau8bfa4262008-11-27 09:25:45 +0100553 if (si->state != SI_ST_EST && si->state != SI_ST_CON)
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100554 return;
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200555
Willy Tarreaucff64112008-11-03 06:26:53 +0100556 if (si->ob->flags & BF_SHUTW) {
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200557 fd_delete(si->fd);
Willy Tarreau74ab2ac2008-11-23 17:23:07 +0100558 si->state = SI_ST_DIS;
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100559 return;
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200560 }
561 EV_FD_CLR(si->fd, DIR_RD);
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100562 return;
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200563}
564
565/*
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200566 * Updates a connected stream_sock file descriptor status and timeouts
567 * according to the buffers' flags. It should only be called once after the
568 * buffer flags have settled down, and before they are cleared. It doesn't
569 * harm to call it as often as desired (it just slightly hurts performance).
570 */
Willy Tarreaub0253252008-11-30 21:37:12 +0100571void stream_sock_data_finish(struct stream_interface *si)
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200572{
Willy Tarreaub0253252008-11-30 21:37:12 +0100573 struct buffer *ib = si->ib;
574 struct buffer *ob = si->ob;
575 int fd = si->fd;
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200576
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200577 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 +0200578 now_ms, __FUNCTION__,
579 fd, fdtab[fd].owner,
580 ib, ob,
581 ib->rex, ob->wex,
582 ib->flags, ob->flags,
Willy Tarreaub0253252008-11-30 21:37:12 +0100583 ib->l, ob->l, si->state);
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200584
585 /* Check if we need to close the read side */
586 if (!(ib->flags & BF_SHUTR)) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200587 /* Read not closed, update FD status and timeout for reads */
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200588 if (ib->flags & (BF_FULL|BF_HIJACK)) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200589 /* stop reading */
Willy Tarreaub0ef7352008-12-14 13:26:20 +0100590 if ((ib->flags & (BF_FULL|BF_HIJACK)) == BF_FULL)
591 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau2d212792008-08-27 21:41:35 +0200592 EV_FD_COND_C(fd, DIR_RD);
593 ib->rex = TICK_ETERNITY;
594 }
595 else {
596 /* (re)start reading and update timeout. Note: we don't recompute the timeout
597 * everytime we get here, otherwise it would risk never to expire. We only
598 * update it if is was not yet set, or if we already got some read status.
599 */
Willy Tarreaub0ef7352008-12-14 13:26:20 +0100600 si->flags &= ~SI_FL_WAIT_ROOM;
Willy Tarreau2d212792008-08-27 21:41:35 +0200601 EV_FD_COND_S(fd, DIR_RD);
Willy Tarreau86491c32008-12-14 09:04:47 +0100602 if (!(ib->flags & BF_READ_NOEXP) &&
603 (!tick_isset(ib->rex) || ib->flags & BF_READ_ACTIVITY))
Willy Tarreau2d212792008-08-27 21:41:35 +0200604 ib->rex = tick_add_ifset(now_ms, ib->rto);
605 }
606 }
607
608 /* Check if we need to close the write side */
609 if (!(ob->flags & BF_SHUTW)) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200610 /* Write not closed, update FD status and timeout for writes */
Willy Tarreaudcef33f2009-01-07 19:33:39 +0100611 if ((ob->send_max == 0 && ob->splice_len == 0) ||
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100612 (ob->flags & BF_EMPTY) ||
Willy Tarreau3da77c52008-08-29 09:58:42 +0200613 (ob->flags & (BF_HIJACK|BF_WRITE_ENA)) == 0) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200614 /* stop writing */
Willy Tarreaub0ef7352008-12-14 13:26:20 +0100615 if ((ob->flags & (BF_EMPTY|BF_HIJACK|BF_WRITE_ENA)) == (BF_EMPTY|BF_WRITE_ENA))
616 si->flags |= SI_FL_WAIT_DATA;
Willy Tarreau2d212792008-08-27 21:41:35 +0200617 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 */
Willy Tarreaub0ef7352008-12-14 13:26:20 +0100625 si->flags &= ~SI_FL_WAIT_DATA;
Willy Tarreau2d212792008-08-27 21:41:35 +0200626 EV_FD_COND_S(fd, DIR_WR);
Willy Tarreau3da77c52008-08-29 09:58:42 +0200627 if (!tick_isset(ob->wex) || ob->flags & BF_WRITE_ACTIVITY) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200628 ob->wex = tick_add_ifset(now_ms, ob->wto);
Willy Tarreau21e1be82008-08-29 11:30:14 +0200629 if (tick_isset(ob->wex) && tick_isset(ib->rex)) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200630 /* Note: depending on the protocol, we don't know if we're waiting
631 * for incoming data or not. So in order to prevent the socket from
632 * expiring read timeouts during writes, we refresh the read timeout,
633 * except if it was already infinite.
634 */
635 ib->rex = ob->wex;
636 }
637 }
638 }
639 }
Willy Tarreau2d212792008-08-27 21:41:35 +0200640}
641
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100642/* This function is used for inter-stream-interface calls. It is called by the
643 * consumer to inform the producer side that it may be interested in checking
644 * for free space in the buffer. Note that it intentionally does not update
645 * timeouts, so that we can still check them later at wake-up.
646 */
647void stream_sock_chk_rcv(struct stream_interface *si)
648{
649 struct buffer *ib = si->ib;
650
651 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",
652 now_ms, __FUNCTION__,
653 fd, fdtab[fd].owner,
654 ib, ob,
655 ib->rex, ob->wex,
656 ib->flags, ob->flags,
657 ib->l, ob->l, si->state);
658
659 if (unlikely(si->state != SI_ST_EST || (ib->flags & BF_SHUTR)))
660 return;
661
662 if (ib->flags & (BF_FULL|BF_HIJACK)) {
663 /* stop reading */
664 if ((ib->flags & (BF_FULL|BF_HIJACK)) == BF_FULL)
665 si->flags |= SI_FL_WAIT_ROOM;
666 EV_FD_COND_C(si->fd, DIR_RD);
667 }
668 else {
669 /* (re)start reading */
670 si->flags &= ~SI_FL_WAIT_ROOM;
671 EV_FD_COND_S(si->fd, DIR_RD);
672 }
673}
674
675
676/* This function is used for inter-stream-interface calls. It is called by the
677 * producer to inform the consumer side that it may be interested in checking
678 * for data in the buffer. Note that it intentionally does not update timeouts,
679 * so that we can still check them later at wake-up.
680 */
681void stream_sock_chk_snd(struct stream_interface *si)
682{
683 struct buffer *ob = si->ob;
684
685 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",
686 now_ms, __FUNCTION__,
687 fd, fdtab[fd].owner,
688 ib, ob,
689 ib->rex, ob->wex,
690 ib->flags, ob->flags,
691 ib->l, ob->l, si->state);
692
693 if (unlikely(si->state != SI_ST_EST || (ob->flags & BF_SHUTW)))
694 return;
695
Willy Tarreaudcef33f2009-01-07 19:33:39 +0100696 if ((ob->send_max == 0 && ob->splice_len == 0) ||
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100697 (ob->flags & BF_EMPTY) ||
698 (ob->flags & (BF_HIJACK|BF_WRITE_ENA)) == 0) {
699 /* stop writing */
700 if ((ob->flags & (BF_EMPTY|BF_HIJACK|BF_WRITE_ENA)) == (BF_EMPTY|BF_WRITE_ENA))
701 si->flags |= SI_FL_WAIT_DATA;
702 EV_FD_COND_C(si->fd, DIR_WR);
703 }
704 else {
705 /* (re)start writing. */
706 si->flags &= ~SI_FL_WAIT_DATA;
707 EV_FD_COND_S(si->fd, DIR_WR);
708 }
709}
710
Willy Tarreaubaaee002006-06-26 02:48:02 +0200711
712/*
713 * Local variables:
714 * c-indent-level: 8
715 * c-basic-offset: 8
716 * End:
717 */