blob: d4a603c98fcffb1505d7220e2964c0118458bc21 [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 Tarreaue393fe22008-08-16 22:18:07 +020089 b->flags |= BF_FULL;
Willy Tarreau83749182007-04-15 20:56:27 +020090 EV_FD_CLR(fd, DIR_RD);
Willy Tarreau0c303ee2008-07-07 00:09:58 +020091 b->rex = TICK_ETERNITY;
92 goto out_wakeup;
Willy Tarreau83749182007-04-15 20:56:27 +020093 }
Willy Tarreaubaaee002006-06-26 02:48:02 +020094
Willy Tarreau6996e152007-04-30 14:37:43 +020095 /*
96 * 2. read the largest possible block
97 */
Willy Tarreaubaaee002006-06-26 02:48:02 +020098#ifndef MSG_NOSIGNAL
Willy Tarreau83749182007-04-15 20:56:27 +020099 {
100 int skerr;
101 socklen_t lskerr = sizeof(skerr);
102
103 ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr);
104 if (ret == -1 || skerr)
105 ret = -1;
106 else
107 ret = recv(fd, b->r, max, 0);
108 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200109#else
Willy Tarreau83749182007-04-15 20:56:27 +0200110 ret = recv(fd, b->r, max, MSG_NOSIGNAL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200111#endif
Willy Tarreau83749182007-04-15 20:56:27 +0200112 if (ret > 0) {
113 b->r += ret;
114 b->l += ret;
Willy Tarreau8a7af602008-05-03 23:07:14 +0200115 cur_read += ret;
Willy Tarreaub38903c2008-11-23 21:33:29 +0100116
117 if (fdtab[fd].state == FD_STCONN)
118 fdtab[fd].state = FD_STREADY;
119
Willy Tarreau3da77c52008-08-29 09:58:42 +0200120 b->flags |= BF_READ_PARTIAL;
Willy Tarreaue393fe22008-08-16 22:18:07 +0200121 b->flags &= ~BF_EMPTY;
Willy Tarreau74ab2ac2008-11-23 17:23:07 +0100122
Willy Tarreau83749182007-04-15 20:56:27 +0200123 if (b->r == b->data + BUFSIZE) {
124 b->r = b->data; /* wrap around the buffer */
125 }
Willy Tarreau9641e8f2007-03-23 23:02:09 +0100126
Willy Tarreau83749182007-04-15 20:56:27 +0200127 b->total += ret;
Willy Tarreau9641e8f2007-03-23 23:02:09 +0100128
Willy Tarreaue393fe22008-08-16 22:18:07 +0200129 if (b->l >= b->rlim - b->data) {
Willy Tarreau6996e152007-04-30 14:37:43 +0200130 /* The buffer is now full, there's no point in going through
131 * the loop again.
132 */
Willy Tarreau8a7af602008-05-03 23:07:14 +0200133 if (!(b->flags & BF_STREAMER_FAST) && (cur_read == b->l)) {
134 b->xfer_small = 0;
135 b->xfer_large++;
136 if (b->xfer_large >= 3) {
137 /* we call this buffer a fast streamer if it manages
138 * to be filled in one call 3 consecutive times.
139 */
140 b->flags |= (BF_STREAMER | BF_STREAMER_FAST);
141 //fputc('+', stderr);
142 }
143 }
144 else if ((b->flags & (BF_STREAMER | BF_STREAMER_FAST)) &&
145 (cur_read <= BUFSIZE / 2)) {
146 b->xfer_large = 0;
147 b->xfer_small++;
148 if (b->xfer_small >= 2) {
149 /* if the buffer has been at least half full twice,
150 * we receive faster than we send, so at least it
151 * is not a "fast streamer".
152 */
153 b->flags &= ~BF_STREAMER_FAST;
154 //fputc('-', stderr);
155 }
156 }
157 else {
158 b->xfer_small = 0;
159 b->xfer_large = 0;
160 }
161
Willy Tarreaue393fe22008-08-16 22:18:07 +0200162 b->flags |= BF_FULL;
Willy Tarreau6996e152007-04-30 14:37:43 +0200163 EV_FD_CLR(fd, DIR_RD);
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200164 b->rex = TICK_ETERNITY;
165 goto out_wakeup;
Willy Tarreau6996e152007-04-30 14:37:43 +0200166 }
167
Willy Tarreauab3e1d32007-06-03 14:10:36 +0200168 /* if too many bytes were missing from last read, it means that
169 * it's pointless trying to read again because the system does
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100170 * not have them in buffers. BTW, if FD_POLL_HUP was present,
171 * it means that we have reached the end and that the connection
172 * is closed.
Willy Tarreauab3e1d32007-06-03 14:10:36 +0200173 */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100174 if (ret < max) {
Willy Tarreau8a7af602008-05-03 23:07:14 +0200175 if ((b->flags & (BF_STREAMER | BF_STREAMER_FAST)) &&
176 (cur_read <= BUFSIZE / 2)) {
177 b->xfer_large = 0;
178 b->xfer_small++;
179 if (b->xfer_small >= 3) {
180 /* we have read less than half of the buffer in
181 * one pass, and this happened at least 3 times.
182 * This is definitely not a streamer.
183 */
184 b->flags &= ~(BF_STREAMER | BF_STREAMER_FAST);
185 //fputc('!', stderr);
186 }
187 }
Willy Tarreau2bea3a12008-08-28 09:47:43 +0200188 /* unfortunately, on level-triggered events, POLL_HUP
189 * is generally delivered AFTER the system buffer is
190 * empty, so this one might never match.
191 */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100192 if (fdtab[fd].ev & FD_POLL_HUP)
193 goto out_shutdown_r;
Willy Tarreau2bea3a12008-08-28 09:47:43 +0200194
195 /* if a streamer has read few data, it may be because we
196 * have exhausted system buffers. It's not worth trying
197 * again.
198 */
199 if (b->flags & BF_STREAMER)
200 break;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100201 }
Willy Tarreauab3e1d32007-06-03 14:10:36 +0200202
203 /* generally if we read something smaller than 1 or 2 MSS,
Willy Tarreau2bea3a12008-08-28 09:47:43 +0200204 * it means that either we have exhausted the system's
205 * buffers (streamer or question-response protocol) or that
206 * the connection will be closed. Streamers are easily
207 * detected so we return early. For other cases, it's still
208 * better to perform a last read to be sure, because it may
209 * save one complete poll/read/wakeup cycle in case of shutdown.
Willy Tarreau83749182007-04-15 20:56:27 +0200210 */
Willy Tarreau2bea3a12008-08-28 09:47:43 +0200211 if (ret < MIN_RET_FOR_READ_LOOP && b->flags & BF_STREAMER)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200212 break;
Willy Tarreau83749182007-04-15 20:56:27 +0200213
Willy Tarreau6996e152007-04-30 14:37:43 +0200214 if (--read_poll <= 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200215 break;
Willy Tarreau83749182007-04-15 20:56:27 +0200216 }
217 else if (ret == 0) {
Willy Tarreau6996e152007-04-30 14:37:43 +0200218 /* connection closed */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100219 goto out_shutdown_r;
Willy Tarreau83749182007-04-15 20:56:27 +0200220 }
Willy Tarreau9f195292007-04-15 21:26:58 +0200221 else if (errno == EAGAIN) {
222 /* Ignore EAGAIN but inform the poller that there is
Willy Tarreau6996e152007-04-30 14:37:43 +0200223 * nothing to read left. But we may have done some work
224 * justifying to notify the task.
Willy Tarreau9f195292007-04-15 21:26:58 +0200225 */
Willy Tarreau83749182007-04-15 20:56:27 +0200226 retval = 0;
227 break;
228 }
229 else {
Willy Tarreau6996e152007-04-30 14:37:43 +0200230 goto out_error;
Willy Tarreau83749182007-04-15 20:56:27 +0200231 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200232 } /* while (1) */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200233
Willy Tarreau6996e152007-04-30 14:37:43 +0200234 /*
235 * The only way to get out of this loop is to have stopped reading
236 * without any error nor close, either by limiting the number of
237 * loops, or because of an EAGAIN. We only rearm the timer if we
238 * have at least read something.
239 */
240
Willy Tarreau3da77c52008-08-29 09:58:42 +0200241 if (tick_isset(b->rex) && b->flags & BF_READ_PARTIAL)
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200242 b->rex = tick_add_ifset(now_ms, b->rto);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200243
Willy Tarreau3da77c52008-08-29 09:58:42 +0200244 if (!(b->flags & BF_READ_ACTIVITY))
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200245 goto out_skip_wakeup;
Willy Tarreau6996e152007-04-30 14:37:43 +0200246 out_wakeup:
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200247 task_wakeup(si->owner, TASK_WOKEN_IO);
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200248
249 out_skip_wakeup:
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100250 fdtab[fd].ev &= ~FD_POLL_IN;
Willy Tarreau83749182007-04-15 20:56:27 +0200251 return retval;
Willy Tarreau6996e152007-04-30 14:37:43 +0200252
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100253 out_shutdown_r:
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200254 /* we received a shutdown */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100255 fdtab[fd].ev &= ~FD_POLL_HUP;
256 b->flags |= BF_READ_NULL;
Willy Tarreau99126c32008-11-27 10:30:51 +0100257 stream_sock_shutr(si);
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200258 goto out_wakeup;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100259
Willy Tarreau6996e152007-04-30 14:37:43 +0200260 out_error:
Willy Tarreaucff64112008-11-03 06:26:53 +0100261 /* Read error on the file descriptor. We mark the FD as STERROR so
262 * that we don't use it anymore. The error is reported to the stream
263 * interface which will take proper action. We must not perturbate the
264 * buffer because the stream interface wants to ensure transparent
265 * connection retries.
Willy Tarreau6996e152007-04-30 14:37:43 +0200266 */
Willy Tarreaucff64112008-11-03 06:26:53 +0100267
Willy Tarreau6996e152007-04-30 14:37:43 +0200268 fdtab[fd].state = FD_STERROR;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100269 fdtab[fd].ev &= ~FD_POLL_STICKY;
Willy Tarreaucff64112008-11-03 06:26:53 +0100270 si->flags |= SI_FL_ERR;
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200271 task_wakeup(si->owner, TASK_WOKEN_IO);
272 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200273}
274
275
276/*
Willy Tarreauf8306d52006-07-29 19:01:31 +0200277 * this function is called on a write event from a stream socket.
Willy Tarreau83749182007-04-15 20:56:27 +0200278 * It returns 0 if we have a high confidence that we will not be
279 * able to write more data without polling first. Returns non-zero
280 * otherwise.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200281 */
Willy Tarreauf8306d52006-07-29 19:01:31 +0200282int stream_sock_write(int fd) {
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200283 __label__ out_wakeup, out_error;
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200284 struct stream_interface *si = fdtab[fd].owner;
Willy Tarreau48adac52008-08-30 04:58:38 +0200285 struct buffer *b = si->ob;
Willy Tarreau83749182007-04-15 20:56:27 +0200286 int ret, max, retval;
287 int write_poll = MAX_WRITE_POLL_LOOPS;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200288
289#ifdef DEBUG_FULL
Willy Tarreauf8306d52006-07-29 19:01:31 +0200290 fprintf(stderr,"stream_sock_write : fd=%d, owner=%p\n", fd, fdtab[fd].owner);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200291#endif
292
Willy Tarreau83749182007-04-15 20:56:27 +0200293 retval = 1;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100294 if (fdtab[fd].state == FD_STERROR || (fdtab[fd].ev & FD_POLL_ERR))
Willy Tarreau6996e152007-04-30 14:37:43 +0200295 goto out_error;
Willy Tarreau83749182007-04-15 20:56:27 +0200296
Willy Tarreau6996e152007-04-30 14:37:43 +0200297 while (1) {
Willy Tarreau83749182007-04-15 20:56:27 +0200298 if (b->l == 0) { /* let's realign the buffer to optimize I/O */
299 b->r = b->w = b->lr = b->data;
300 max = 0;
301 }
302 else if (b->r > b->w) {
303 max = b->r - b->w;
304 }
305 else {
306 max = b->data + BUFSIZE - b->w;
307 }
308
Willy Tarreaubaaee002006-06-26 02:48:02 +0200309 if (max == 0) {
Willy Tarreauf8306d52006-07-29 19:01:31 +0200310 /* may be we have received a connection acknowledgement in TCP mode without data */
Willy Tarreau6996e152007-04-30 14:37:43 +0200311 if (likely(fdtab[fd].state == FD_STCONN)) {
Willy Tarreau6996e152007-04-30 14:37:43 +0200312 /* We have no data to send to check the connection, and
313 * getsockopt() will not inform us whether the connection
314 * is still pending. So we'll reuse connect() to check the
315 * state of the socket. This has the advantage of givig us
316 * the following info :
317 * - error
318 * - connecting (EALREADY, EINPROGRESS)
319 * - connected (EISCONN, 0)
320 */
Willy Tarreaue94ebd02007-10-09 17:14:37 +0200321 if ((connect(fd, fdtab[fd].peeraddr, fdtab[fd].peerlen) == 0))
Willy Tarreau6996e152007-04-30 14:37:43 +0200322 errno = 0;
323
324 if (errno == EALREADY || errno == EINPROGRESS) {
325 retval = 0;
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200326 goto out_may_wakeup;
Willy Tarreauf8306d52006-07-29 19:01:31 +0200327 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200328
329 if (errno && errno != EISCONN)
330 goto out_error;
331
332 /* OK we just need to indicate that we got a connection
333 * and that we wrote nothing.
334 */
335 b->flags |= BF_WRITE_NULL;
336 fdtab[fd].state = FD_STREADY;
Willy Tarreauf8306d52006-07-29 19:01:31 +0200337 }
338
Willy Tarreau6996e152007-04-30 14:37:43 +0200339 /* Funny, we were called to write something but there wasn't
340 * anything. Theorically we cannot get there, but just in case,
341 * let's disable the write event and pretend we never came there.
342 */
Willy Tarreauf161a342007-04-08 16:59:42 +0200343 EV_FD_CLR(fd, DIR_WR);
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200344 b->wex = TICK_ETERNITY;
345 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200346 }
347
348#ifndef MSG_NOSIGNAL
349 {
350 int skerr;
351 socklen_t lskerr = sizeof(skerr);
352
Willy Tarreauc6423482006-10-15 14:59:03 +0200353 ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr);
354 if (ret == -1 || skerr)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200355 ret = -1;
356 else
357 ret = send(fd, b->w, max, MSG_DONTWAIT);
358 }
359#else
360 ret = send(fd, b->w, max, MSG_DONTWAIT | MSG_NOSIGNAL);
361#endif
362
363 if (ret > 0) {
364 b->l -= ret;
365 b->w += ret;
Willy Tarreau74ab2ac2008-11-23 17:23:07 +0100366
Willy Tarreaub38903c2008-11-23 21:33:29 +0100367 if (fdtab[fd].state == FD_STCONN)
368 fdtab[fd].state = FD_STREADY;
369
Willy Tarreau3da77c52008-08-29 09:58:42 +0200370 b->flags |= BF_WRITE_PARTIAL;
Willy Tarreaue393fe22008-08-16 22:18:07 +0200371
372 if (b->l < b->rlim - b->data)
373 b->flags &= ~BF_FULL;
Willy Tarreau74ab2ac2008-11-23 17:23:07 +0100374
Willy Tarreaubaaee002006-06-26 02:48:02 +0200375 if (b->w == b->data + BUFSIZE) {
376 b->w = b->data; /* wrap around the buffer */
377 }
Willy Tarreau83749182007-04-15 20:56:27 +0200378
Willy Tarreau6996e152007-04-30 14:37:43 +0200379 if (!b->l) {
Willy Tarreaue393fe22008-08-16 22:18:07 +0200380 b->flags |= BF_EMPTY;
Willy Tarreau48adac52008-08-30 04:58:38 +0200381
382 /* Maybe we just wrote the last chunk and need to close ? */
383 if ((b->flags & (BF_SHUTW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) == (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR)) {
384 if (si->state == SI_ST_EST) {
Willy Tarreau99126c32008-11-27 10:30:51 +0100385 stream_sock_shutw(si);
386 b->wex = TICK_ETERNITY;
387 goto out_wakeup;
Willy Tarreau48adac52008-08-30 04:58:38 +0200388 }
389 }
390
Willy Tarreau6996e152007-04-30 14:37:43 +0200391 EV_FD_CLR(fd, DIR_WR);
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200392 b->wex = TICK_ETERNITY;
393 goto out_wakeup;
Willy Tarreau6996e152007-04-30 14:37:43 +0200394 }
Willy Tarreau83749182007-04-15 20:56:27 +0200395
Willy Tarreauab3e1d32007-06-03 14:10:36 +0200396 /* if the system buffer is full, don't insist */
397 if (ret < max)
398 break;
399
Willy Tarreau6996e152007-04-30 14:37:43 +0200400 if (--write_poll <= 0)
401 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200402 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200403 else if (ret == 0 || errno == EAGAIN) {
404 /* nothing written, just pretend we were never called
405 * and wait for the socket to be ready. But we may have
406 * done some work justifying to notify the task.
407 */
Willy Tarreau83749182007-04-15 20:56:27 +0200408 retval = 0;
409 break;
410 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200411 else {
Willy Tarreau6996e152007-04-30 14:37:43 +0200412 goto out_error;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200413 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200414 } /* while (1) */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200415
Willy Tarreau6996e152007-04-30 14:37:43 +0200416 /*
417 * The only way to get out of this loop is to have stopped writing
418 * without any error, either by limiting the number of loops, or
419 * because of an EAGAIN. We only rearm the timer if we have at least
420 * written something.
421 */
422
Willy Tarreau3da77c52008-08-29 09:58:42 +0200423 if (tick_isset(b->wex) && b->flags & BF_WRITE_PARTIAL) {
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200424 b->wex = tick_add_ifset(now_ms, b->wto);
Willy Tarreauadfb8562008-08-11 15:24:42 +0200425 if (tick_isset(b->wex)) {
Willy Tarreau83749182007-04-15 20:56:27 +0200426 /* FIXME: to prevent the client from expiring read timeouts during writes,
427 * we refresh it. A solution would be to merge read+write timeouts into a
428 * unique one, although that needs some study particularly on full-duplex
429 * TCP connections. */
Willy Tarreauba392ce2008-08-16 21:13:23 +0200430 if (tick_isset(b->rex) && !(b->flags & BF_SHUTR))
Willy Tarreaufa645582007-06-03 15:59:52 +0200431 b->rex = b->wex;
Willy Tarreau83749182007-04-15 20:56:27 +0200432 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200433 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200434
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200435 out_may_wakeup:
Willy Tarreau3da77c52008-08-29 09:58:42 +0200436 if (!(b->flags & BF_WRITE_ACTIVITY))
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200437 goto out_skip_wakeup;
Willy Tarreau6996e152007-04-30 14:37:43 +0200438 out_wakeup:
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200439 task_wakeup(si->owner, TASK_WOKEN_IO);
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200440
441 out_skip_wakeup:
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100442 fdtab[fd].ev &= ~FD_POLL_OUT;
Willy Tarreau83749182007-04-15 20:56:27 +0200443 return retval;
Willy Tarreau6996e152007-04-30 14:37:43 +0200444
445 out_error:
Willy Tarreaucff64112008-11-03 06:26:53 +0100446 /* Write error on the file descriptor. We mark the FD as STERROR so
447 * that we don't use it anymore. The error is reported to the stream
448 * interface which will take proper action. We must not perturbate the
449 * buffer because the stream interface wants to ensure transparent
450 * connection retries.
Willy Tarreau6996e152007-04-30 14:37:43 +0200451 */
Willy Tarreaucff64112008-11-03 06:26:53 +0100452
Willy Tarreau6996e152007-04-30 14:37:43 +0200453 fdtab[fd].state = FD_STERROR;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100454 fdtab[fd].ev &= ~FD_POLL_STICKY;
Willy Tarreaucff64112008-11-03 06:26:53 +0100455 si->flags |= SI_FL_ERR;
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200456 task_wakeup(si->owner, TASK_WOKEN_IO);
457 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200458}
459
Willy Tarreau48adac52008-08-30 04:58:38 +0200460/*
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200461 * This function performs a shutdown-write on a stream interface in a connected or
462 * init state (it does nothing for other states). It either shuts the write side
Willy Tarreau99126c32008-11-27 10:30:51 +0100463 * or closes the file descriptor and marks itself as closed. The buffer flags are
464 * updated to reflect the new state.
Willy Tarreau48adac52008-08-30 04:58:38 +0200465 */
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100466void stream_sock_shutw(struct stream_interface *si)
Willy Tarreau48adac52008-08-30 04:58:38 +0200467{
Willy Tarreau99126c32008-11-27 10:30:51 +0100468 if (si->ob->flags & BF_SHUTW)
469 return;
470 si->ob->flags |= BF_SHUTW;
471 si->ob->wex = TICK_ETERNITY;
472
Willy Tarreaub38903c2008-11-23 21:33:29 +0100473 switch (si->state) {
Willy Tarreaub38903c2008-11-23 21:33:29 +0100474 case SI_ST_EST:
475 if (!(si->ib->flags & BF_SHUTR)) {
476 EV_FD_CLR(si->fd, DIR_WR);
477 shutdown(si->fd, SHUT_WR);
478 return;
479 }
480 /* fall through */
481 case SI_ST_CON:
Willy Tarreau8bfa4262008-11-27 09:25:45 +0100482 /* we may have to close a pending connection, and mark the
483 * response buffer as shutr
484 */
Willy Tarreau48adac52008-08-30 04:58:38 +0200485 fd_delete(si->fd);
Willy Tarreaufe3718a2008-11-30 18:14:12 +0100486 /* fall through */
487 case SI_ST_CER:
Willy Tarreau7f006512008-12-07 14:04:04 +0100488 si->state = SI_ST_DIS;
489 default:
Willy Tarreau99126c32008-11-27 10:30:51 +0100490 si->ib->flags |= BF_SHUTR;
Willy Tarreaufe3718a2008-11-30 18:14:12 +0100491 si->ib->rex = TICK_ETERNITY;
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100492 return;
Willy Tarreau48adac52008-08-30 04:58:38 +0200493 }
Willy Tarreau48adac52008-08-30 04:58:38 +0200494}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200495
Willy Tarreau2d212792008-08-27 21:41:35 +0200496/*
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200497 * This function performs a shutdown-read on a stream interface in a connected or
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100498 * init state (it does nothing for other states). It either shuts the read side
Willy Tarreau99126c32008-11-27 10:30:51 +0100499 * or closes the file descriptor and marks itself as closed. The buffer flags are
500 * updated to reflect the new state.
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200501 */
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100502void stream_sock_shutr(struct stream_interface *si)
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200503{
Willy Tarreau99126c32008-11-27 10:30:51 +0100504 if (si->ib->flags & BF_SHUTR)
505 return;
506 si->ib->flags |= BF_SHUTR;
507 si->ib->rex = TICK_ETERNITY;
508
Willy Tarreau8bfa4262008-11-27 09:25:45 +0100509 if (si->state != SI_ST_EST && si->state != SI_ST_CON)
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100510 return;
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200511
Willy Tarreaucff64112008-11-03 06:26:53 +0100512 if (si->ob->flags & BF_SHUTW) {
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200513 fd_delete(si->fd);
Willy Tarreau74ab2ac2008-11-23 17:23:07 +0100514 si->state = SI_ST_DIS;
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100515 return;
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200516 }
517 EV_FD_CLR(si->fd, DIR_RD);
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100518 return;
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200519}
520
521/*
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200522 * Updates a connected stream_sock file descriptor status and timeouts
523 * according to the buffers' flags. It should only be called once after the
524 * buffer flags have settled down, and before they are cleared. It doesn't
525 * harm to call it as often as desired (it just slightly hurts performance).
526 */
Willy Tarreaub0253252008-11-30 21:37:12 +0100527void stream_sock_data_finish(struct stream_interface *si)
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200528{
Willy Tarreaub0253252008-11-30 21:37:12 +0100529 struct buffer *ib = si->ib;
530 struct buffer *ob = si->ob;
531 int fd = si->fd;
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200532
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200533 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 +0200534 now_ms, __FUNCTION__,
535 fd, fdtab[fd].owner,
536 ib, ob,
537 ib->rex, ob->wex,
538 ib->flags, ob->flags,
Willy Tarreaub0253252008-11-30 21:37:12 +0100539 ib->l, ob->l, si->state);
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200540
541 /* Check if we need to close the read side */
542 if (!(ib->flags & BF_SHUTR)) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200543 /* Read not closed, update FD status and timeout for reads */
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200544 if (ib->flags & (BF_FULL|BF_HIJACK)) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200545 /* stop reading */
546 EV_FD_COND_C(fd, DIR_RD);
547 ib->rex = TICK_ETERNITY;
548 }
549 else {
550 /* (re)start reading and update timeout. Note: we don't recompute the timeout
551 * everytime we get here, otherwise it would risk never to expire. We only
552 * update it if is was not yet set, or if we already got some read status.
553 */
554 EV_FD_COND_S(fd, DIR_RD);
Willy Tarreau3da77c52008-08-29 09:58:42 +0200555 if (!tick_isset(ib->rex) || ib->flags & BF_READ_ACTIVITY)
Willy Tarreau2d212792008-08-27 21:41:35 +0200556 ib->rex = tick_add_ifset(now_ms, ib->rto);
557 }
558 }
559
560 /* Check if we need to close the write side */
561 if (!(ob->flags & BF_SHUTW)) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200562 /* Write not closed, update FD status and timeout for writes */
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200563 if ((ob->flags & BF_EMPTY) ||
Willy Tarreau3da77c52008-08-29 09:58:42 +0200564 (ob->flags & (BF_HIJACK|BF_WRITE_ENA)) == 0) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200565 /* stop writing */
566 EV_FD_COND_C(fd, DIR_WR);
567 ob->wex = TICK_ETERNITY;
568 }
569 else {
570 /* (re)start writing and update timeout. Note: we don't recompute the timeout
571 * everytime we get here, otherwise it would risk never to expire. We only
572 * update it if is was not yet set, or if we already got some write status.
573 */
574 EV_FD_COND_S(fd, DIR_WR);
Willy Tarreau3da77c52008-08-29 09:58:42 +0200575 if (!tick_isset(ob->wex) || ob->flags & BF_WRITE_ACTIVITY) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200576 ob->wex = tick_add_ifset(now_ms, ob->wto);
Willy Tarreau21e1be82008-08-29 11:30:14 +0200577 if (tick_isset(ob->wex) && tick_isset(ib->rex)) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200578 /* Note: depending on the protocol, we don't know if we're waiting
579 * for incoming data or not. So in order to prevent the socket from
580 * expiring read timeouts during writes, we refresh the read timeout,
581 * except if it was already infinite.
582 */
583 ib->rex = ob->wex;
584 }
585 }
586 }
587 }
Willy Tarreau2d212792008-08-27 21:41:35 +0200588}
589
Willy Tarreaubaaee002006-06-26 02:48:02 +0200590
591/*
592 * Local variables:
593 * c-indent-level: 8
594 * c-basic-offset: 8
595 * End:
596 */