blob: 62ba15b8682dd30d3f68a40d99825299edb70f67 [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 Tarreaue5ed4062008-08-30 03:17:31 +020043 struct stream_interface *si = fdtab[fd].owner;
Willy Tarreau48adac52008-08-30 04:58:38 +020044 struct buffer *b = si->ib;
Willy Tarreau8a7af602008-05-03 23:07:14 +020045 int ret, max, retval, cur_read;
Willy Tarreaub8949f12007-03-23 22:39:59 +010046 int read_poll = MAX_READ_POLL_LOOPS;
Willy Tarreaubaaee002006-06-26 02:48:02 +020047
48#ifdef DEBUG_FULL
Willy Tarreaud6f087e2008-01-18 17:20:13 +010049 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 +020050#endif
51
Willy Tarreau83749182007-04-15 20:56:27 +020052 retval = 1;
53
Willy Tarreaud6f087e2008-01-18 17:20:13 +010054 /* stop immediately on errors */
55 if (fdtab[fd].state == FD_STERROR || (fdtab[fd].ev & FD_POLL_ERR))
Willy Tarreau6996e152007-04-30 14:37:43 +020056 goto out_error;
Willy Tarreaud6f087e2008-01-18 17:20:13 +010057
58 /* stop here if we reached the end of data */
59 if ((fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP)) == FD_POLL_HUP)
60 goto out_shutdown_r;
Willy Tarreau83749182007-04-15 20:56:27 +020061
Willy Tarreau8a7af602008-05-03 23:07:14 +020062 cur_read = 0;
Willy Tarreau6996e152007-04-30 14:37:43 +020063 while (1) {
64 /*
65 * 1. compute the maximum block size we can read at once.
66 */
Willy Tarreau03d60bb2009-01-09 11:13:00 +010067 if (b->l == 0) {
68 /* let's realign the buffer to optimize I/O */
69 b->r = b->w = b->lr = b->data;
70 max = b->max_len;
Willy Tarreau83749182007-04-15 20:56:27 +020071 }
72 else if (b->r > b->w) {
Willy Tarreau03d60bb2009-01-09 11:13:00 +010073 max = b->data + b->max_len - b->r;
Willy Tarreau83749182007-04-15 20:56:27 +020074 }
75 else {
76 max = b->w - b->r;
Willy Tarreau03d60bb2009-01-09 11:13:00 +010077 if (max > b->max_len)
78 max = b->max_len;
Willy Tarreau83749182007-04-15 20:56:27 +020079 }
Willy Tarreau74ab2ac2008-11-23 17:23:07 +010080
Willy Tarreau9c0fe592009-01-18 16:25:31 +010081 if (max == 0) {
82 b->flags |= BF_FULL;
83 break;
84 }
Willy Tarreaubaaee002006-06-26 02:48:02 +020085
Willy Tarreau6996e152007-04-30 14:37:43 +020086 /*
87 * 2. read the largest possible block
88 */
Willy Tarreaubaaee002006-06-26 02:48:02 +020089#ifndef MSG_NOSIGNAL
Willy Tarreau83749182007-04-15 20:56:27 +020090 {
91 int skerr;
92 socklen_t lskerr = sizeof(skerr);
93
94 ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr);
95 if (ret == -1 || skerr)
96 ret = -1;
97 else
98 ret = recv(fd, b->r, max, 0);
99 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200100#else
Willy Tarreau83749182007-04-15 20:56:27 +0200101 ret = recv(fd, b->r, max, MSG_NOSIGNAL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200102#endif
Willy Tarreau83749182007-04-15 20:56:27 +0200103 if (ret > 0) {
104 b->r += ret;
105 b->l += ret;
Willy Tarreau8a7af602008-05-03 23:07:14 +0200106 cur_read += ret;
Willy Tarreaub38903c2008-11-23 21:33:29 +0100107
Willy Tarreau0abebcc2009-01-08 00:09:41 +0100108 /* if we're allowed to directly forward data, we must update send_max */
109 if (b->to_forward > 0) {
110 int fwd = MIN(b->to_forward, ret);
111 b->send_max += fwd;
112 b->to_forward -= fwd;
113 }
Willy Tarreauf890dc92008-12-13 21:12:26 +0100114
Willy Tarreaub38903c2008-11-23 21:33:29 +0100115 if (fdtab[fd].state == FD_STCONN)
116 fdtab[fd].state = FD_STREADY;
117
Willy Tarreau3da77c52008-08-29 09:58:42 +0200118 b->flags |= BF_READ_PARTIAL;
Willy Tarreaue393fe22008-08-16 22:18:07 +0200119 b->flags &= ~BF_EMPTY;
Willy Tarreau74ab2ac2008-11-23 17:23:07 +0100120
Willy Tarreau83749182007-04-15 20:56:27 +0200121 if (b->r == b->data + BUFSIZE) {
122 b->r = b->data; /* wrap around the buffer */
123 }
Willy Tarreau9641e8f2007-03-23 23:02:09 +0100124
Willy Tarreau83749182007-04-15 20:56:27 +0200125 b->total += ret;
Willy Tarreau9641e8f2007-03-23 23:02:09 +0100126
Willy Tarreau03d60bb2009-01-09 11:13:00 +0100127 if (b->l >= b->max_len) {
Willy Tarreau6996e152007-04-30 14:37:43 +0200128 /* The buffer is now full, there's no point in going through
129 * the loop again.
130 */
Willy Tarreau8a7af602008-05-03 23:07:14 +0200131 if (!(b->flags & BF_STREAMER_FAST) && (cur_read == b->l)) {
132 b->xfer_small = 0;
133 b->xfer_large++;
134 if (b->xfer_large >= 3) {
135 /* we call this buffer a fast streamer if it manages
136 * to be filled in one call 3 consecutive times.
137 */
138 b->flags |= (BF_STREAMER | BF_STREAMER_FAST);
139 //fputc('+', stderr);
140 }
141 }
142 else if ((b->flags & (BF_STREAMER | BF_STREAMER_FAST)) &&
143 (cur_read <= BUFSIZE / 2)) {
144 b->xfer_large = 0;
145 b->xfer_small++;
146 if (b->xfer_small >= 2) {
147 /* if the buffer has been at least half full twice,
148 * we receive faster than we send, so at least it
149 * is not a "fast streamer".
150 */
151 b->flags &= ~BF_STREAMER_FAST;
152 //fputc('-', stderr);
153 }
154 }
155 else {
156 b->xfer_small = 0;
157 b->xfer_large = 0;
158 }
Willy Tarreau9c0fe592009-01-18 16:25:31 +0100159
160 b->flags |= BF_FULL;
161 break;
Willy Tarreau6996e152007-04-30 14:37:43 +0200162 }
163
Willy Tarreauab3e1d32007-06-03 14:10:36 +0200164 /* if too many bytes were missing from last read, it means that
165 * it's pointless trying to read again because the system does
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100166 * not have them in buffers. BTW, if FD_POLL_HUP was present,
167 * it means that we have reached the end and that the connection
168 * is closed.
Willy Tarreauab3e1d32007-06-03 14:10:36 +0200169 */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100170 if (ret < max) {
Willy Tarreau8a7af602008-05-03 23:07:14 +0200171 if ((b->flags & (BF_STREAMER | BF_STREAMER_FAST)) &&
172 (cur_read <= BUFSIZE / 2)) {
173 b->xfer_large = 0;
174 b->xfer_small++;
175 if (b->xfer_small >= 3) {
176 /* we have read less than half of the buffer in
177 * one pass, and this happened at least 3 times.
178 * This is definitely not a streamer.
179 */
180 b->flags &= ~(BF_STREAMER | BF_STREAMER_FAST);
181 //fputc('!', stderr);
182 }
183 }
Willy Tarreau2bea3a12008-08-28 09:47:43 +0200184 /* unfortunately, on level-triggered events, POLL_HUP
185 * is generally delivered AFTER the system buffer is
186 * empty, so this one might never match.
187 */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100188 if (fdtab[fd].ev & FD_POLL_HUP)
189 goto out_shutdown_r;
Willy Tarreau2bea3a12008-08-28 09:47:43 +0200190
191 /* if a streamer has read few data, it may be because we
192 * have exhausted system buffers. It's not worth trying
193 * again.
194 */
195 if (b->flags & BF_STREAMER)
196 break;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100197 }
Willy Tarreauab3e1d32007-06-03 14:10:36 +0200198
199 /* generally if we read something smaller than 1 or 2 MSS,
Willy Tarreau2bea3a12008-08-28 09:47:43 +0200200 * it means that either we have exhausted the system's
201 * buffers (streamer or question-response protocol) or that
202 * the connection will be closed. Streamers are easily
203 * detected so we return early. For other cases, it's still
204 * better to perform a last read to be sure, because it may
205 * save one complete poll/read/wakeup cycle in case of shutdown.
Willy Tarreau83749182007-04-15 20:56:27 +0200206 */
Willy Tarreau2bea3a12008-08-28 09:47:43 +0200207 if (ret < MIN_RET_FOR_READ_LOOP && b->flags & BF_STREAMER)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200208 break;
Willy Tarreau83749182007-04-15 20:56:27 +0200209
Willy Tarreau6996e152007-04-30 14:37:43 +0200210 if (--read_poll <= 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200211 break;
Willy Tarreau83749182007-04-15 20:56:27 +0200212 }
213 else if (ret == 0) {
Willy Tarreau6996e152007-04-30 14:37:43 +0200214 /* connection closed */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100215 goto out_shutdown_r;
Willy Tarreau83749182007-04-15 20:56:27 +0200216 }
Willy Tarreau9f195292007-04-15 21:26:58 +0200217 else if (errno == EAGAIN) {
218 /* Ignore EAGAIN but inform the poller that there is
Willy Tarreauaf78d0f2009-01-08 10:09:08 +0100219 * nothing to read left if we did not read much, ie
220 * less than what we were still expecting to read.
221 * But we may have done some work justifying to notify
222 * the task.
Willy Tarreau9f195292007-04-15 21:26:58 +0200223 */
Willy Tarreauaf78d0f2009-01-08 10:09:08 +0100224 if (cur_read < MIN_RET_FOR_READ_LOOP)
225 retval = 0;
Willy Tarreau83749182007-04-15 20:56:27 +0200226 break;
227 }
228 else {
Willy Tarreau6996e152007-04-30 14:37:43 +0200229 goto out_error;
Willy Tarreau83749182007-04-15 20:56:27 +0200230 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200231 } /* while (1) */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200232
Willy Tarreau6996e152007-04-30 14:37:43 +0200233 out_wakeup:
Willy Tarreau9c0fe592009-01-18 16:25:31 +0100234 /* We might have some data the consumer is waiting for */
235 if (likely((b->send_max || b->splice_len) &&
Willy Tarreauefc612c2009-01-09 12:18:24 +0100236 (b->cons->flags & SI_FL_WAIT_DATA)))
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100237 b->cons->chk_snd(b->cons);
238
Willy Tarreau9c0fe592009-01-18 16:25:31 +0100239 /* note that the consumer might have cleared BF_FULL */
240 if ((b->flags & (BF_READ_PARTIAL|BF_FULL|BF_READ_NOEXP)) == BF_READ_PARTIAL)
241 b->rex = tick_add_ifset(now_ms, b->rto);
242 else if (b->flags & BF_FULL) {
243 si->flags |= SI_FL_WAIT_ROOM;
244 EV_FD_CLR(fd, DIR_RD);
245 b->rex = TICK_ETERNITY;
246 }
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 ||
Willy Tarreau9c0fe592009-01-18 16:25:31 +0100254 b->cons->state != SI_ST_EST ||
255 (si->flags & SI_FL_ERR)))
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100256 task_wakeup(si->owner, TASK_WOKEN_IO);
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200257
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 Tarreaud6f087e2008-01-18 17:20:13 +0100261 out_shutdown_r:
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200262 /* we received a shutdown */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100263 fdtab[fd].ev &= ~FD_POLL_HUP;
264 b->flags |= BF_READ_NULL;
Willy Tarreau99126c32008-11-27 10:30:51 +0100265 stream_sock_shutr(si);
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200266 goto out_wakeup;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100267
Willy Tarreau6996e152007-04-30 14:37:43 +0200268 out_error:
Willy Tarreaucff64112008-11-03 06:26:53 +0100269 /* Read error on the file descriptor. We mark the FD as STERROR so
270 * that we don't use it anymore. The error is reported to the stream
271 * interface which will take proper action. We must not perturbate the
272 * buffer because the stream interface wants to ensure transparent
273 * connection retries.
Willy Tarreau6996e152007-04-30 14:37:43 +0200274 */
Willy Tarreaucff64112008-11-03 06:26:53 +0100275
Willy Tarreau6996e152007-04-30 14:37:43 +0200276 fdtab[fd].state = FD_STERROR;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100277 fdtab[fd].ev &= ~FD_POLL_STICKY;
Willy Tarreaucff64112008-11-03 06:26:53 +0100278 si->flags |= SI_FL_ERR;
Willy Tarreau9c0fe592009-01-18 16:25:31 +0100279 retval = 1;
280 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200281}
282
283
284/*
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100285 * This function is called to send buffer data to a stream socket.
286 * It returns -1 in case of unrecoverable error, 0 if the caller needs to poll
287 * before calling it again, otherwise 1.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200288 */
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100289int stream_sock_write_loop(struct stream_interface *si, struct buffer *b)
290{
Willy Tarreau83749182007-04-15 20:56:27 +0200291 int write_poll = MAX_WRITE_POLL_LOOPS;
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100292 int retval = 1;
293 int ret, max;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200294
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100295 if (!b->l || !b->send_max)
296 return retval;
Willy Tarreau83749182007-04-15 20:56:27 +0200297
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100298 /* when we're in this loop, we already know that there is no spliced
299 * data left, and that there are sendable buffered data.
300 */
Willy Tarreau6996e152007-04-30 14:37:43 +0200301 while (1) {
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100302 if (b->r > b->w)
Willy Tarreau83749182007-04-15 20:56:27 +0200303 max = b->r - b->w;
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100304 else
Willy Tarreau83749182007-04-15 20:56:27 +0200305 max = b->data + BUFSIZE - b->w;
Willy Tarreau83749182007-04-15 20:56:27 +0200306
Willy Tarreauf890dc92008-12-13 21:12:26 +0100307 /* limit the amount of outgoing data if required */
308 if (max > b->send_max)
309 max = b->send_max;
310
Willy Tarreaubaaee002006-06-26 02:48:02 +0200311#ifndef MSG_NOSIGNAL
312 {
313 int skerr;
314 socklen_t lskerr = sizeof(skerr);
315
Willy Tarreauc6423482006-10-15 14:59:03 +0200316 ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr);
317 if (ret == -1 || skerr)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200318 ret = -1;
319 else
320 ret = send(fd, b->w, max, MSG_DONTWAIT);
321 }
322#else
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100323 ret = send(si->fd, b->w, max, MSG_DONTWAIT | MSG_NOSIGNAL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200324#endif
325
326 if (ret > 0) {
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100327 if (fdtab[si->fd].state == FD_STCONN)
328 fdtab[si->fd].state = FD_STREADY;
Willy Tarreaub38903c2008-11-23 21:33:29 +0100329
Willy Tarreau3da77c52008-08-29 09:58:42 +0200330 b->flags |= BF_WRITE_PARTIAL;
Willy Tarreaue393fe22008-08-16 22:18:07 +0200331
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100332 b->w += ret;
333 if (b->w == b->data + BUFSIZE)
334 b->w = b->data; /* wrap around the buffer */
335
336 b->l -= ret;
337 if (likely(b->l < b->max_len))
Willy Tarreaue393fe22008-08-16 22:18:07 +0200338 b->flags &= ~BF_FULL;
Willy Tarreau74ab2ac2008-11-23 17:23:07 +0100339
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100340 if (likely(!b->l)) {
341 /* optimize data alignment in the buffer */
342 b->r = b->w = b->lr = b->data;
343 if (likely(!b->splice_len))
344 b->flags |= BF_EMPTY;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200345 }
Willy Tarreau83749182007-04-15 20:56:27 +0200346
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100347 b->send_max -= ret;
348 if (!b->send_max || !b->l)
349 break;
Willy Tarreau83749182007-04-15 20:56:27 +0200350
Willy Tarreauab3e1d32007-06-03 14:10:36 +0200351 /* if the system buffer is full, don't insist */
352 if (ret < max)
353 break;
354
Willy Tarreau6996e152007-04-30 14:37:43 +0200355 if (--write_poll <= 0)
356 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200357 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200358 else if (ret == 0 || errno == EAGAIN) {
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100359 /* nothing written, we need to poll for write first */
Willy Tarreau83749182007-04-15 20:56:27 +0200360 retval = 0;
361 break;
362 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200363 else {
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100364 /* bad, we got an error */
365 retval = -1;
366 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200367 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200368 } /* while (1) */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200369
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100370 return retval;
371}
Willy Tarreau6996e152007-04-30 14:37:43 +0200372
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100373
374/*
375 * This function is called on a write event from a stream socket.
376 * It returns 0 if the caller needs to poll before calling it again, otherwise
377 * non-zero.
378 */
379int stream_sock_write(int fd)
380{
381 struct stream_interface *si = fdtab[fd].owner;
382 struct buffer *b = si->ob;
383 int retval = 1;
384
385#ifdef DEBUG_FULL
386 fprintf(stderr,"stream_sock_write : fd=%d, owner=%p\n", fd, fdtab[fd].owner);
387#endif
388
389 retval = 1;
390 if (fdtab[fd].state == FD_STERROR || (fdtab[fd].ev & FD_POLL_ERR))
391 goto out_error;
392
393 if (likely(!(b->flags & BF_EMPTY))) {
394 /* OK there are data waiting to be sent */
395 retval = stream_sock_write_loop(si, b);
396 if (retval < 0)
397 goto out_error;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200398 }
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100399 else {
400 /* may be we have received a connection acknowledgement in TCP mode without data */
401 if (likely(fdtab[fd].state == FD_STCONN)) {
402 /* We have no data to send to check the connection, and
403 * getsockopt() will not inform us whether the connection
404 * is still pending. So we'll reuse connect() to check the
405 * state of the socket. This has the advantage of givig us
406 * the following info :
407 * - error
408 * - connecting (EALREADY, EINPROGRESS)
409 * - connected (EISCONN, 0)
410 */
411 if ((connect(fd, fdtab[fd].peeraddr, fdtab[fd].peerlen) == 0))
412 errno = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200413
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100414 if (errno == EALREADY || errno == EINPROGRESS) {
415 retval = 0;
416 goto out_may_wakeup;
417 }
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100418
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100419 if (errno && errno != EISCONN)
420 goto out_error;
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200421
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100422 /* OK we just need to indicate that we got a connection
423 * and that we wrote nothing.
424 */
425 b->flags |= BF_WRITE_NULL;
426 fdtab[fd].state = FD_STREADY;
427 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200428
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100429 /* Funny, we were called to write something but there wasn't
430 * anything. We can get there, for example if we were woken up
431 * on a write event to finish the splice, but the send_max is 0
432 * so we cannot write anything from the buffer. Let's disable
433 * the write event and pretend we never came there.
434 */
435 }
436
437 if ((b->flags & BF_EMPTY) || !b->send_max) {
438 /* the connection is established but we can't write. Either the
439 * buffer is empty, or we just refrain from sending because the
440 * send_max limit was reached. Maybe we just wrote the last
441 * chunk and need to close.
442 */
443 if (((b->flags & (BF_SHUTW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) ==
444 (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR)) &&
445 (si->state == SI_ST_EST)) {
446 stream_sock_shutw(si);
447 goto out_wakeup;
448 }
449
450 if (b->flags & BF_EMPTY)
Willy Tarreauac128fe2009-01-09 13:05:19 +0100451 si->flags |= SI_FL_WAIT_DATA;
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100452
Willy Tarreauac128fe2009-01-09 13:05:19 +0100453 EV_FD_CLR(fd, DIR_WR);
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100454 b->wex = TICK_ETERNITY;
Willy Tarreauac128fe2009-01-09 13:05:19 +0100455 }
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100456
457 out_may_wakeup:
458 if (b->flags & BF_WRITE_ACTIVITY) {
459 /* update timeout if we have written something */
460 if (b->send_max &&
461 (b->flags & (BF_EMPTY|BF_SHUTW|BF_WRITE_PARTIAL)) == BF_WRITE_PARTIAL)
462 b->wex = tick_add_ifset(now_ms, b->wto);
463
464 out_wakeup:
465 if (tick_isset(si->ib->rex)) {
466 /* Note: to prevent the client from expiring read timeouts
467 * during writes, we refresh it. A better solution would be
468 * to merge read+write timeouts into a unique one, although
469 * that needs some study particularly on full-duplex TCP
470 * connections.
471 */
472 si->ib->rex = tick_add_ifset(now_ms, si->ib->rto);
473 }
474
475 /* the producer might be waiting for more room to store data */
476 if (likely((b->flags & (BF_WRITE_PARTIAL|BF_FULL)) == BF_WRITE_PARTIAL &&
477 (b->prod->flags & SI_FL_WAIT_ROOM)))
478 b->prod->chk_rcv(b->prod);
479
480 /* we have to wake up if there is a special event or if we don't have
481 * any more data to forward and it's not planned to send any more.
482 */
483 if (likely((b->flags & (BF_WRITE_NULL|BF_WRITE_ERROR|BF_SHUTW)) ||
484 (!b->to_forward && !b->send_max && !b->splice_len) ||
485 si->state != SI_ST_EST ||
486 b->prod->state != SI_ST_EST))
487 task_wakeup(si->owner, TASK_WOKEN_IO);
488 }
489
490 fdtab[fd].ev &= ~FD_POLL_OUT;
491 return retval;
Willy Tarreauac128fe2009-01-09 13:05:19 +0100492
Willy Tarreau6996e152007-04-30 14:37:43 +0200493 out_error:
Willy Tarreaucff64112008-11-03 06:26:53 +0100494 /* Write error on the file descriptor. We mark the FD as STERROR so
495 * that we don't use it anymore. The error is reported to the stream
496 * interface which will take proper action. We must not perturbate the
497 * buffer because the stream interface wants to ensure transparent
498 * connection retries.
Willy Tarreau6996e152007-04-30 14:37:43 +0200499 */
Willy Tarreaucff64112008-11-03 06:26:53 +0100500
Willy Tarreau6996e152007-04-30 14:37:43 +0200501 fdtab[fd].state = FD_STERROR;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100502 fdtab[fd].ev &= ~FD_POLL_STICKY;
Willy Tarreaucff64112008-11-03 06:26:53 +0100503 si->flags |= SI_FL_ERR;
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200504 task_wakeup(si->owner, TASK_WOKEN_IO);
505 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200506}
507
Willy Tarreau48adac52008-08-30 04:58:38 +0200508/*
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200509 * This function performs a shutdown-write on a stream interface in a connected or
510 * init state (it does nothing for other states). It either shuts the write side
Willy Tarreau99126c32008-11-27 10:30:51 +0100511 * or closes the file descriptor and marks itself as closed. The buffer flags are
512 * updated to reflect the new state.
Willy Tarreau48adac52008-08-30 04:58:38 +0200513 */
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100514void stream_sock_shutw(struct stream_interface *si)
Willy Tarreau48adac52008-08-30 04:58:38 +0200515{
Willy Tarreau99126c32008-11-27 10:30:51 +0100516 if (si->ob->flags & BF_SHUTW)
517 return;
518 si->ob->flags |= BF_SHUTW;
519 si->ob->wex = TICK_ETERNITY;
Willy Tarreaub0ef7352008-12-14 13:26:20 +0100520 si->flags &= ~SI_FL_WAIT_DATA;
Willy Tarreau99126c32008-11-27 10:30:51 +0100521
Willy Tarreaub38903c2008-11-23 21:33:29 +0100522 switch (si->state) {
Willy Tarreaub38903c2008-11-23 21:33:29 +0100523 case SI_ST_EST:
524 if (!(si->ib->flags & BF_SHUTR)) {
525 EV_FD_CLR(si->fd, DIR_WR);
526 shutdown(si->fd, SHUT_WR);
527 return;
528 }
529 /* fall through */
530 case SI_ST_CON:
Willy Tarreaub0ef7352008-12-14 13:26:20 +0100531 si->flags &= ~SI_FL_WAIT_ROOM;
Willy Tarreau8bfa4262008-11-27 09:25:45 +0100532 /* we may have to close a pending connection, and mark the
533 * response buffer as shutr
534 */
Willy Tarreau48adac52008-08-30 04:58:38 +0200535 fd_delete(si->fd);
Willy Tarreaufe3718a2008-11-30 18:14:12 +0100536 /* fall through */
537 case SI_ST_CER:
Willy Tarreau7f006512008-12-07 14:04:04 +0100538 si->state = SI_ST_DIS;
539 default:
Willy Tarreau99126c32008-11-27 10:30:51 +0100540 si->ib->flags |= BF_SHUTR;
Willy Tarreaufe3718a2008-11-30 18:14:12 +0100541 si->ib->rex = TICK_ETERNITY;
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100542 return;
Willy Tarreau48adac52008-08-30 04:58:38 +0200543 }
Willy Tarreau48adac52008-08-30 04:58:38 +0200544}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200545
Willy Tarreau2d212792008-08-27 21:41:35 +0200546/*
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200547 * This function performs a shutdown-read on a stream interface in a connected or
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100548 * init state (it does nothing for other states). It either shuts the read side
Willy Tarreau99126c32008-11-27 10:30:51 +0100549 * or closes the file descriptor and marks itself as closed. The buffer flags are
550 * updated to reflect the new state.
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200551 */
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100552void stream_sock_shutr(struct stream_interface *si)
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200553{
Willy Tarreau99126c32008-11-27 10:30:51 +0100554 if (si->ib->flags & BF_SHUTR)
555 return;
556 si->ib->flags |= BF_SHUTR;
557 si->ib->rex = TICK_ETERNITY;
Willy Tarreaub0ef7352008-12-14 13:26:20 +0100558 si->flags &= ~SI_FL_WAIT_ROOM;
Willy Tarreau99126c32008-11-27 10:30:51 +0100559
Willy Tarreau8bfa4262008-11-27 09:25:45 +0100560 if (si->state != SI_ST_EST && si->state != SI_ST_CON)
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100561 return;
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200562
Willy Tarreaucff64112008-11-03 06:26:53 +0100563 if (si->ob->flags & BF_SHUTW) {
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200564 fd_delete(si->fd);
Willy Tarreau74ab2ac2008-11-23 17:23:07 +0100565 si->state = SI_ST_DIS;
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100566 return;
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200567 }
568 EV_FD_CLR(si->fd, DIR_RD);
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100569 return;
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200570}
571
572/*
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200573 * Updates a connected stream_sock file descriptor status and timeouts
574 * according to the buffers' flags. It should only be called once after the
575 * buffer flags have settled down, and before they are cleared. It doesn't
576 * harm to call it as often as desired (it just slightly hurts performance).
577 */
Willy Tarreaub0253252008-11-30 21:37:12 +0100578void stream_sock_data_finish(struct stream_interface *si)
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200579{
Willy Tarreaub0253252008-11-30 21:37:12 +0100580 struct buffer *ib = si->ib;
581 struct buffer *ob = si->ob;
582 int fd = si->fd;
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200583
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200584 DPRINTF(stderr,"[%u] %s: fd=%d owner=%p ib=%p, ob=%p, exp(r,w)=%u,%u ibf=%08x obf=%08x ibl=%d obl=%d si=%d\n",
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200585 now_ms, __FUNCTION__,
586 fd, fdtab[fd].owner,
587 ib, ob,
588 ib->rex, ob->wex,
589 ib->flags, ob->flags,
Willy Tarreaub0253252008-11-30 21:37:12 +0100590 ib->l, ob->l, si->state);
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200591
592 /* Check if we need to close the read side */
593 if (!(ib->flags & BF_SHUTR)) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200594 /* Read not closed, update FD status and timeout for reads */
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200595 if (ib->flags & (BF_FULL|BF_HIJACK)) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200596 /* stop reading */
Willy Tarreaub0ef7352008-12-14 13:26:20 +0100597 if ((ib->flags & (BF_FULL|BF_HIJACK)) == BF_FULL)
598 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau2d212792008-08-27 21:41:35 +0200599 EV_FD_COND_C(fd, DIR_RD);
600 ib->rex = TICK_ETERNITY;
601 }
602 else {
603 /* (re)start reading and update timeout. Note: we don't recompute the timeout
604 * everytime we get here, otherwise it would risk never to expire. We only
605 * update it if is was not yet set, or if we already got some read status.
606 */
Willy Tarreaub0ef7352008-12-14 13:26:20 +0100607 si->flags &= ~SI_FL_WAIT_ROOM;
Willy Tarreau2d212792008-08-27 21:41:35 +0200608 EV_FD_COND_S(fd, DIR_RD);
Willy Tarreau86491c32008-12-14 09:04:47 +0100609 if (!(ib->flags & BF_READ_NOEXP) &&
610 (!tick_isset(ib->rex) || ib->flags & BF_READ_ACTIVITY))
Willy Tarreau2d212792008-08-27 21:41:35 +0200611 ib->rex = tick_add_ifset(now_ms, ib->rto);
612 }
613 }
614
615 /* Check if we need to close the write side */
616 if (!(ob->flags & BF_SHUTW)) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200617 /* Write not closed, update FD status and timeout for writes */
Willy Tarreaudcef33f2009-01-07 19:33:39 +0100618 if ((ob->send_max == 0 && ob->splice_len == 0) ||
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100619 (ob->flags & BF_EMPTY) ||
Willy Tarreau3da77c52008-08-29 09:58:42 +0200620 (ob->flags & (BF_HIJACK|BF_WRITE_ENA)) == 0) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200621 /* stop writing */
Willy Tarreaub0ef7352008-12-14 13:26:20 +0100622 if ((ob->flags & (BF_EMPTY|BF_HIJACK|BF_WRITE_ENA)) == (BF_EMPTY|BF_WRITE_ENA))
623 si->flags |= SI_FL_WAIT_DATA;
Willy Tarreau2d212792008-08-27 21:41:35 +0200624 EV_FD_COND_C(fd, DIR_WR);
625 ob->wex = TICK_ETERNITY;
626 }
627 else {
628 /* (re)start writing and update timeout. Note: we don't recompute the timeout
629 * everytime we get here, otherwise it would risk never to expire. We only
630 * update it if is was not yet set, or if we already got some write status.
631 */
Willy Tarreaub0ef7352008-12-14 13:26:20 +0100632 si->flags &= ~SI_FL_WAIT_DATA;
Willy Tarreau2d212792008-08-27 21:41:35 +0200633 EV_FD_COND_S(fd, DIR_WR);
Willy Tarreau3da77c52008-08-29 09:58:42 +0200634 if (!tick_isset(ob->wex) || ob->flags & BF_WRITE_ACTIVITY) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200635 ob->wex = tick_add_ifset(now_ms, ob->wto);
Willy Tarreau21e1be82008-08-29 11:30:14 +0200636 if (tick_isset(ob->wex) && tick_isset(ib->rex)) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200637 /* Note: depending on the protocol, we don't know if we're waiting
638 * for incoming data or not. So in order to prevent the socket from
639 * expiring read timeouts during writes, we refresh the read timeout,
640 * except if it was already infinite.
641 */
642 ib->rex = ob->wex;
643 }
644 }
645 }
646 }
Willy Tarreau2d212792008-08-27 21:41:35 +0200647}
648
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100649/* This function is used for inter-stream-interface calls. It is called by the
650 * consumer to inform the producer side that it may be interested in checking
651 * for free space in the buffer. Note that it intentionally does not update
652 * timeouts, so that we can still check them later at wake-up.
653 */
654void stream_sock_chk_rcv(struct stream_interface *si)
655{
656 struct buffer *ib = si->ib;
657
658 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",
659 now_ms, __FUNCTION__,
660 fd, fdtab[fd].owner,
661 ib, ob,
662 ib->rex, ob->wex,
663 ib->flags, ob->flags,
664 ib->l, ob->l, si->state);
665
666 if (unlikely(si->state != SI_ST_EST || (ib->flags & BF_SHUTR)))
667 return;
668
669 if (ib->flags & (BF_FULL|BF_HIJACK)) {
670 /* stop reading */
671 if ((ib->flags & (BF_FULL|BF_HIJACK)) == BF_FULL)
672 si->flags |= SI_FL_WAIT_ROOM;
673 EV_FD_COND_C(si->fd, DIR_RD);
674 }
675 else {
676 /* (re)start reading */
677 si->flags &= ~SI_FL_WAIT_ROOM;
678 EV_FD_COND_S(si->fd, DIR_RD);
679 }
680}
681
682
683/* This function is used for inter-stream-interface calls. It is called by the
684 * producer to inform the consumer side that it may be interested in checking
685 * for data in the buffer. Note that it intentionally does not update timeouts,
686 * so that we can still check them later at wake-up.
687 */
688void stream_sock_chk_snd(struct stream_interface *si)
689{
690 struct buffer *ob = si->ob;
691
692 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",
693 now_ms, __FUNCTION__,
694 fd, fdtab[fd].owner,
695 ib, ob,
696 ib->rex, ob->wex,
697 ib->flags, ob->flags,
698 ib->l, ob->l, si->state);
699
700 if (unlikely(si->state != SI_ST_EST || (ob->flags & BF_SHUTW)))
701 return;
702
Willy Tarreaudcef33f2009-01-07 19:33:39 +0100703 if ((ob->send_max == 0 && ob->splice_len == 0) ||
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100704 (ob->flags & BF_EMPTY) ||
705 (ob->flags & (BF_HIJACK|BF_WRITE_ENA)) == 0) {
706 /* stop writing */
707 if ((ob->flags & (BF_EMPTY|BF_HIJACK|BF_WRITE_ENA)) == (BF_EMPTY|BF_WRITE_ENA))
708 si->flags |= SI_FL_WAIT_DATA;
709 EV_FD_COND_C(si->fd, DIR_WR);
710 }
711 else {
712 /* (re)start writing. */
713 si->flags &= ~SI_FL_WAIT_DATA;
714 EV_FD_COND_S(si->fd, DIR_WR);
715 }
716}
717
Willy Tarreaubaaee002006-06-26 02:48:02 +0200718
719/*
720 * Local variables:
721 * c-indent-level: 8
722 * c-basic-offset: 8
723 * End:
724 */