blob: 39a962e01ec21c3bf1d7571c1fb2f2077c58637d [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
Willy Tarreaub277d6e2012-05-11 16:59:14 +02002 * Functions used to send/receive data using SOCK_STREAM sockets.
Willy Tarreaubaaee002006-06-26 02:48:02 +02003 *
Willy Tarreaub277d6e2012-05-11 16:59:14 +02004 * Copyright 2000-2012 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
Willy Tarreau6b4aad42009-01-18 21:59:13 +010013#define _GNU_SOURCE
Willy Tarreaubaaee002006-06-26 02:48:02 +020014#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18
19#include <sys/socket.h>
20#include <sys/stat.h>
21#include <sys/types.h>
22
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +040023#include <netinet/tcp.h>
24
Willy Tarreau2dd0d472006-06-29 17:53:05 +020025#include <common/compat.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020026#include <common/config.h>
Willy Tarreaud6f087e2008-01-18 17:20:13 +010027#include <common/debug.h>
Willy Tarreau83749182007-04-15 20:56:27 +020028#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020029#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020030#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020031
Willy Tarreau2d212792008-08-27 21:41:35 +020032#include <proto/buffers.h>
Willy Tarreau8b117082012-08-06 15:06:49 +020033#include <proto/connection.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020034#include <proto/fd.h>
Willy Tarreaueb472682010-05-28 18:46:57 +020035#include <proto/freq_ctr.h>
36#include <proto/log.h>
Willy Tarreau3eba98a2009-01-25 13:56:13 +010037#include <proto/pipe.h>
Willy Tarreaufe598a72010-09-21 21:48:23 +020038#include <proto/protocols.h>
Willy Tarreauc63190d2012-05-11 14:23:52 +020039#include <proto/sock_raw.h>
Willy Tarreau73b013b2012-05-21 16:31:45 +020040#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020041#include <proto/task.h>
42
Willy Tarreau5bd8c372009-01-19 00:32:22 +010043#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020044
Willy Tarreaub277d6e2012-05-11 16:59:14 +020045/* main event functions used to move data between sockets and buffers */
Willy Tarreau239d7182012-07-23 18:53:03 +020046static int sock_raw_read(struct connection *conn);
47static int sock_raw_write(struct connection *conn);
Willy Tarreaub277d6e2012-05-11 16:59:14 +020048static void sock_raw_data_finish(struct stream_interface *si);
49static void sock_raw_shutr(struct stream_interface *si);
50static void sock_raw_shutw(struct stream_interface *si);
51static void sock_raw_chk_rcv(struct stream_interface *si);
52static void sock_raw_chk_snd(struct stream_interface *si);
53
54
Willy Tarreau6b4aad42009-01-18 21:59:13 +010055#if defined(CONFIG_HAP_LINUX_SPLICE)
Willy Tarreau43d8fb22011-08-22 17:12:02 +020056#include <common/splice.h>
Willy Tarreau5bd8c372009-01-19 00:32:22 +010057
58/* A pipe contains 16 segments max, and it's common to see segments of 1448 bytes
59 * because of timestamps. Use this as a hint for not looping on splice().
60 */
61#define SPLICE_FULL_HINT 16*1448
62
Willy Tarreaua9de3332009-11-28 07:47:10 +010063/* how many data we attempt to splice at once when the buffer is configured for
64 * infinite forwarding */
65#define MAX_SPLICE_AT_ONCE (1<<30)
66
Willy Tarreau5bd8c372009-01-19 00:32:22 +010067/* Returns :
68 * -1 if splice is not possible or not possible anymore and we must switch to
69 * user-land copy (eg: to_forward reached)
70 * 0 when we know that polling is required to get more data (EAGAIN)
71 * 1 for all other cases (we can safely try again, or if an activity has been
72 * detected (DATA/NULL/ERR))
73 * Sets :
74 * BF_READ_NULL
75 * BF_READ_PARTIAL
76 * BF_WRITE_PARTIAL (during copy)
Willy Tarreauba0b63d2009-09-20 08:09:44 +020077 * BF_OUT_EMPTY (during copy)
Willy Tarreau5bd8c372009-01-19 00:32:22 +010078 * SI_FL_ERR
79 * SI_FL_WAIT_ROOM
80 * (SI_FL_WAIT_RECV)
Willy Tarreau3eba98a2009-01-25 13:56:13 +010081 *
82 * This function automatically allocates a pipe from the pipe pool. It also
83 * carefully ensures to clear b->pipe whenever it leaves the pipe empty.
Willy Tarreau5bd8c372009-01-19 00:32:22 +010084 */
Willy Tarreaub277d6e2012-05-11 16:59:14 +020085static int sock_raw_splice_in(struct buffer *b, struct stream_interface *si)
Willy Tarreau5bd8c372009-01-19 00:32:22 +010086{
Willy Tarreau82a04562011-12-11 22:37:06 +010087 static int splice_detects_close;
Willy Tarreaufb7508a2012-05-21 16:47:54 +020088 int fd = si_fd(si);
Willy Tarreau31971e52009-09-20 12:07:52 +020089 int ret;
90 unsigned long max;
Willy Tarreau5bd8c372009-01-19 00:32:22 +010091 int retval = 1;
92
93 if (!b->to_forward)
94 return -1;
95
96 if (!(b->flags & BF_KERN_SPLICING))
97 return -1;
98
Willy Tarreau02d6cfc2012-03-01 18:19:58 +010099 if (buffer_not_empty(b)) {
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100100 /* We're embarrassed, there are already data pending in
101 * the buffer and we don't want to have them at two
102 * locations at a time. Let's indicate we need some
103 * place and ask the consumer to hurry.
104 */
105 si->flags |= SI_FL_WAIT_ROOM;
106 EV_FD_CLR(fd, DIR_RD);
107 b->rex = TICK_ETERNITY;
Willy Tarreau73b013b2012-05-21 16:31:45 +0200108 si_chk_snd(b->cons);
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100109 return 1;
110 }
111
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100112 if (unlikely(b->pipe == NULL)) {
113 if (pipes_used >= global.maxpipes || !(b->pipe = get_pipe())) {
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100114 b->flags &= ~BF_KERN_SPLICING;
115 return -1;
116 }
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100117 }
118
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100119 /* At this point, b->pipe is valid */
120
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100121 while (1) {
Willy Tarreaua9de3332009-11-28 07:47:10 +0100122 if (b->to_forward == BUF_INFINITE_FORWARD)
123 max = MAX_SPLICE_AT_ONCE;
124 else
125 max = b->to_forward;
126
Willy Tarreau31971e52009-09-20 12:07:52 +0200127 if (!max) {
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100128 /* It looks like the buffer + the pipe already contain
129 * the maximum amount of data to be transferred. Try to
130 * send those data immediately on the other side if it
131 * is currently waiting.
132 */
133 retval = -1; /* end of forwarding */
134 break;
135 }
136
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100137 ret = splice(fd, NULL, b->pipe->prod, NULL, max,
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100138 SPLICE_F_MOVE|SPLICE_F_NONBLOCK);
139
140 if (ret <= 0) {
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100141 if (ret == 0) {
Willy Tarreau98b306b2009-01-25 11:11:32 +0100142 /* connection closed. This is only detected by
Willy Tarreau82a04562011-12-11 22:37:06 +0100143 * recent kernels (>= 2.6.27.13). If we notice
144 * it works, we store the info for later use.
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100145 */
Willy Tarreau82a04562011-12-11 22:37:06 +0100146 splice_detects_close = 1;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100147 b->flags |= BF_READ_NULL;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100148 retval = 1; /* no need for further polling */
149 break;
150 }
151
152 if (errno == EAGAIN) {
153 /* there are two reasons for EAGAIN :
154 * - nothing in the socket buffer (standard)
155 * - pipe is full
Willy Tarreau98b306b2009-01-25 11:11:32 +0100156 * - the connection is closed (kernel < 2.6.27.13)
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100157 * Since we don't know if pipe is full, we'll
158 * stop if the pipe is not empty. Anyway, we
159 * will almost always fill/empty the pipe.
160 */
161
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100162 if (b->pipe->data) {
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100163 si->flags |= SI_FL_WAIT_ROOM;
164 retval = 1;
165 break;
166 }
167
Willy Tarreau82a04562011-12-11 22:37:06 +0100168 /* We don't know if the connection was closed,
169 * but if we know splice detects close, then we
170 * know it for sure.
Willy Tarreau98b306b2009-01-25 11:11:32 +0100171 * But if we're called upon POLLIN with an empty
Willy Tarreau82a04562011-12-11 22:37:06 +0100172 * pipe and get EAGAIN, it is suspect enough to
Willy Tarreau98b306b2009-01-25 11:11:32 +0100173 * try to fall back to the normal recv scheme
174 * which will be able to deal with the situation.
175 */
Willy Tarreau82a04562011-12-11 22:37:06 +0100176 if (splice_detects_close)
177 retval = 0; /* we know for sure that it's EAGAIN */
178 else
179 retval = -1;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100180 break;
181 }
Willy Tarreaudc340a92009-06-28 23:10:19 +0200182
Willy Tarreaua9de3332009-11-28 07:47:10 +0100183 if (errno == ENOSYS || errno == EINVAL) {
Willy Tarreaudc340a92009-06-28 23:10:19 +0200184 /* splice not supported on this end, disable it */
185 b->flags &= ~BF_KERN_SPLICING;
186 si->flags &= ~SI_FL_CAP_SPLICE;
187 put_pipe(b->pipe);
188 b->pipe = NULL;
189 return -1;
190 }
191
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100192 /* here we have another error */
193 si->flags |= SI_FL_ERR;
194 retval = 1;
195 break;
196 } /* ret <= 0 */
197
Willy Tarreau31971e52009-09-20 12:07:52 +0200198 if (b->to_forward != BUF_INFINITE_FORWARD)
199 b->to_forward -= ret;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100200 b->total += ret;
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100201 b->pipe->data += ret;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100202 b->flags |= BF_READ_PARTIAL;
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200203 b->flags &= ~BF_OUT_EMPTY;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100204
Willy Tarreau6f4a82c2009-03-21 20:43:57 +0100205 if (b->pipe->data >= SPLICE_FULL_HINT ||
206 ret >= global.tune.recv_enough) {
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100207 /* We've read enough of it for this time. */
208 retval = 1;
209 break;
210 }
211 } /* while */
212
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100213 if (unlikely(!b->pipe->data)) {
214 put_pipe(b->pipe);
215 b->pipe = NULL;
216 }
217
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100218 return retval;
219}
220
Willy Tarreau6b4aad42009-01-18 21:59:13 +0100221#endif /* CONFIG_HAP_LINUX_SPLICE */
222
223
Willy Tarreaubaaee002006-06-26 02:48:02 +0200224/*
Willy Tarreaud7971282006-07-29 18:36:34 +0200225 * this function is called on a read event from a stream socket.
Willy Tarreau83749182007-04-15 20:56:27 +0200226 * It returns 0 if we have a high confidence that we will not be
227 * able to read more data without polling first. Returns non-zero
228 * otherwise.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200229 */
Willy Tarreau239d7182012-07-23 18:53:03 +0200230static int sock_raw_read(struct connection *conn)
Willy Tarreaub277d6e2012-05-11 16:59:14 +0200231{
Willy Tarreau239d7182012-07-23 18:53:03 +0200232 int fd = conn->t.sock.fd;
Willy Tarreau80184712012-07-06 14:54:49 +0200233 struct stream_interface *si = container_of(conn, struct stream_interface, conn);
Willy Tarreau48adac52008-08-30 04:58:38 +0200234 struct buffer *b = si->ib;
Willy Tarreau8a7af602008-05-03 23:07:14 +0200235 int ret, max, retval, cur_read;
Willy Tarreaub8949f12007-03-23 22:39:59 +0100236 int read_poll = MAX_READ_POLL_LOOPS;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200237
238#ifdef DEBUG_FULL
Willy Tarreaub277d6e2012-05-11 16:59:14 +0200239 fprintf(stderr,"sock_raw_read : fd=%d, ev=0x%02x, owner=%p\n", fd, fdtab[fd].ev, fdtab[fd].owner);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200240#endif
241
Willy Tarreau83749182007-04-15 20:56:27 +0200242 retval = 1;
243
Willy Tarreau71543652009-07-14 19:55:05 +0200244 /* stop immediately on errors. Note that we DON'T want to stop on
245 * POLL_ERR, as the poller might report a write error while there
246 * are still data available in the recv buffer. This typically
247 * happens when we send too large a request to a backend server
248 * which rejects it before reading it all.
249 */
Willy Tarreau80184712012-07-06 14:54:49 +0200250 if (conn->flags & CO_FL_ERROR)
Willy Tarreau6996e152007-04-30 14:37:43 +0200251 goto out_error;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100252
253 /* stop here if we reached the end of data */
254 if ((fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP)) == FD_POLL_HUP)
255 goto out_shutdown_r;
Willy Tarreau83749182007-04-15 20:56:27 +0200256
Willy Tarreaud06e7112009-03-29 10:18:41 +0200257 /* maybe we were called immediately after an asynchronous shutr */
258 if (b->flags & BF_SHUTR)
259 goto out_wakeup;
260
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100261#if defined(CONFIG_HAP_LINUX_SPLICE)
Willy Tarreau14acc702011-05-11 20:47:24 +0200262 if (b->to_forward >= MIN_SPLICE_FORWARD && b->flags & BF_KERN_SPLICING) {
Willy Tarreau98b306b2009-01-25 11:11:32 +0100263
264 /* Under Linux, if FD_POLL_HUP is set, we have reached the end.
265 * Since older splice() implementations were buggy and returned
266 * EAGAIN on end of read, let's bypass the call to splice() now.
267 */
268 if (fdtab[fd].ev & FD_POLL_HUP)
269 goto out_shutdown_r;
270
Willy Tarreaub277d6e2012-05-11 16:59:14 +0200271 retval = sock_raw_splice_in(b, si);
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100272
273 if (retval >= 0) {
274 if (si->flags & SI_FL_ERR)
275 goto out_error;
276 if (b->flags & BF_READ_NULL)
277 goto out_shutdown_r;
278 goto out_wakeup;
279 }
280 /* splice not possible (anymore), let's go on on standard copy */
281 }
282#endif
Willy Tarreau8a7af602008-05-03 23:07:14 +0200283 cur_read = 0;
Willy Tarreau6996e152007-04-30 14:37:43 +0200284 while (1) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200285 max = bi_avail(b);
Willy Tarreau864e8252009-12-28 17:36:37 +0100286
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200287 if (!max) {
Willy Tarreau864e8252009-12-28 17:36:37 +0100288 b->flags |= BF_FULL;
289 si->flags |= SI_FL_WAIT_ROOM;
290 break;
291 }
292
Willy Tarreau6996e152007-04-30 14:37:43 +0200293 /*
294 * 1. compute the maximum block size we can read at once.
295 */
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100296 if (buffer_empty(b)) {
Willy Tarreau03d60bb2009-01-09 11:13:00 +0100297 /* let's realign the buffer to optimize I/O */
Willy Tarreaua458b672012-03-05 11:17:50 +0100298 b->p = b->data;
Willy Tarreau83749182007-04-15 20:56:27 +0200299 }
Willy Tarreau89fa7062012-03-02 16:13:16 +0100300 else if (b->data + b->o < b->p &&
301 b->p + b->i < b->data + b->size) {
Willy Tarreau864e8252009-12-28 17:36:37 +0100302 /* remaining space wraps at the end, with a moving limit */
Willy Tarreau363a5bb2012-03-02 20:14:45 +0100303 if (max > b->data + b->size - (b->p + b->i))
304 max = b->data + b->size - (b->p + b->i);
Willy Tarreau9c0fe592009-01-18 16:25:31 +0100305 }
Willy Tarreau864e8252009-12-28 17:36:37 +0100306 /* else max is already OK */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200307
Willy Tarreau6996e152007-04-30 14:37:43 +0200308 /*
309 * 2. read the largest possible block
310 */
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +0200311 ret = recv(fd, bi_end(b), max, 0);
Willy Tarreaud6d06902009-08-19 11:22:33 +0200312
Willy Tarreau83749182007-04-15 20:56:27 +0200313 if (ret > 0) {
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100314 b->i += ret;
Willy Tarreau8a7af602008-05-03 23:07:14 +0200315 cur_read += ret;
Willy Tarreaub38903c2008-11-23 21:33:29 +0100316
Willy Tarreau2e046c62012-03-01 16:08:30 +0100317 /* if we're allowed to directly forward data, we must update ->o */
Willy Tarreau31971e52009-09-20 12:07:52 +0200318 if (b->to_forward && !(b->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
319 unsigned long fwd = ret;
320 if (b->to_forward != BUF_INFINITE_FORWARD) {
321 if (fwd > b->to_forward)
322 fwd = b->to_forward;
323 b->to_forward -= fwd;
324 }
Willy Tarreau328582c2012-05-05 23:32:27 +0200325 b_adv(b, fwd);
Willy Tarreau0abebcc2009-01-08 00:09:41 +0100326 }
Willy Tarreauf890dc92008-12-13 21:12:26 +0100327
Willy Tarreau80184712012-07-06 14:54:49 +0200328 if (conn->flags & CO_FL_WAIT_L4_CONN) {
329 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau8ae52cb2012-05-20 10:38:46 +0200330 si->exp = TICK_ETERNITY;
331 }
Willy Tarreaub38903c2008-11-23 21:33:29 +0100332
Willy Tarreau3da77c52008-08-29 09:58:42 +0200333 b->flags |= BF_READ_PARTIAL;
Willy Tarreau83749182007-04-15 20:56:27 +0200334 b->total += ret;
Willy Tarreau9641e8f2007-03-23 23:02:09 +0100335
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200336 if (bi_full(b)) {
Willy Tarreau6996e152007-04-30 14:37:43 +0200337 /* The buffer is now full, there's no point in going through
338 * the loop again.
339 */
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100340 if (!(b->flags & BF_STREAMER_FAST) && (cur_read == buffer_len(b))) {
Willy Tarreau8a7af602008-05-03 23:07:14 +0200341 b->xfer_small = 0;
342 b->xfer_large++;
343 if (b->xfer_large >= 3) {
344 /* we call this buffer a fast streamer if it manages
345 * to be filled in one call 3 consecutive times.
346 */
347 b->flags |= (BF_STREAMER | BF_STREAMER_FAST);
348 //fputc('+', stderr);
349 }
350 }
351 else if ((b->flags & (BF_STREAMER | BF_STREAMER_FAST)) &&
Willy Tarreaua07a34e2009-08-16 23:27:46 +0200352 (cur_read <= b->size / 2)) {
Willy Tarreau8a7af602008-05-03 23:07:14 +0200353 b->xfer_large = 0;
354 b->xfer_small++;
355 if (b->xfer_small >= 2) {
356 /* if the buffer has been at least half full twice,
357 * we receive faster than we send, so at least it
358 * is not a "fast streamer".
359 */
360 b->flags &= ~BF_STREAMER_FAST;
361 //fputc('-', stderr);
362 }
363 }
364 else {
365 b->xfer_small = 0;
366 b->xfer_large = 0;
367 }
Willy Tarreau9c0fe592009-01-18 16:25:31 +0100368
369 b->flags |= BF_FULL;
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100370 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau9c0fe592009-01-18 16:25:31 +0100371 break;
Willy Tarreau6996e152007-04-30 14:37:43 +0200372 }
373
Willy Tarreauab3e1d32007-06-03 14:10:36 +0200374 /* if too many bytes were missing from last read, it means that
375 * it's pointless trying to read again because the system does
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100376 * not have them in buffers. BTW, if FD_POLL_HUP was present,
377 * it means that we have reached the end and that the connection
378 * is closed.
Willy Tarreauab3e1d32007-06-03 14:10:36 +0200379 */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100380 if (ret < max) {
Willy Tarreau8a7af602008-05-03 23:07:14 +0200381 if ((b->flags & (BF_STREAMER | BF_STREAMER_FAST)) &&
Willy Tarreaua07a34e2009-08-16 23:27:46 +0200382 (cur_read <= b->size / 2)) {
Willy Tarreau8a7af602008-05-03 23:07:14 +0200383 b->xfer_large = 0;
384 b->xfer_small++;
385 if (b->xfer_small >= 3) {
386 /* we have read less than half of the buffer in
387 * one pass, and this happened at least 3 times.
388 * This is definitely not a streamer.
389 */
390 b->flags &= ~(BF_STREAMER | BF_STREAMER_FAST);
391 //fputc('!', stderr);
392 }
393 }
Willy Tarreau2bea3a12008-08-28 09:47:43 +0200394 /* unfortunately, on level-triggered events, POLL_HUP
395 * is generally delivered AFTER the system buffer is
396 * empty, so this one might never match.
397 */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100398 if (fdtab[fd].ev & FD_POLL_HUP)
399 goto out_shutdown_r;
Willy Tarreau2bea3a12008-08-28 09:47:43 +0200400
401 /* if a streamer has read few data, it may be because we
402 * have exhausted system buffers. It's not worth trying
403 * again.
404 */
405 if (b->flags & BF_STREAMER)
406 break;
Willy Tarreauab3e1d32007-06-03 14:10:36 +0200407
Willy Tarreau6f4a82c2009-03-21 20:43:57 +0100408 /* generally if we read something smaller than 1 or 2 MSS,
409 * it means that either we have exhausted the system's
410 * buffers (streamer or question-response protocol) or
411 * that the connection will be closed. Streamers are
412 * easily detected so we return early. For other cases,
413 * it's still better to perform a last read to be sure,
414 * because it may save one complete poll/read/wakeup cycle
415 * in case of shutdown.
416 */
417 if (ret < MIN_RET_FOR_READ_LOOP && b->flags & BF_STREAMER)
418 break;
419
420 /* if we read a large block smaller than what we requested,
421 * it's almost certain we'll never get anything more.
422 */
423 if (ret >= global.tune.recv_enough)
424 break;
425 }
Willy Tarreau83749182007-04-15 20:56:27 +0200426
Willy Tarreau1b194fe2009-03-21 21:10:04 +0100427 if ((b->flags & BF_READ_DONTWAIT) || --read_poll <= 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200428 break;
Willy Tarreau83749182007-04-15 20:56:27 +0200429 }
430 else if (ret == 0) {
Willy Tarreau6996e152007-04-30 14:37:43 +0200431 /* connection closed */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100432 goto out_shutdown_r;
Willy Tarreau83749182007-04-15 20:56:27 +0200433 }
Willy Tarreau9f195292007-04-15 21:26:58 +0200434 else if (errno == EAGAIN) {
435 /* Ignore EAGAIN but inform the poller that there is
Willy Tarreauaf78d0f2009-01-08 10:09:08 +0100436 * nothing to read left if we did not read much, ie
437 * less than what we were still expecting to read.
438 * But we may have done some work justifying to notify
439 * the task.
Willy Tarreau9f195292007-04-15 21:26:58 +0200440 */
Willy Tarreauaf78d0f2009-01-08 10:09:08 +0100441 if (cur_read < MIN_RET_FOR_READ_LOOP)
442 retval = 0;
Willy Tarreau83749182007-04-15 20:56:27 +0200443 break;
444 }
445 else {
Willy Tarreau6996e152007-04-30 14:37:43 +0200446 goto out_error;
Willy Tarreau83749182007-04-15 20:56:27 +0200447 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200448 } /* while (1) */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200449
Willy Tarreau6996e152007-04-30 14:37:43 +0200450 out_wakeup:
Willy Tarreau83749182007-04-15 20:56:27 +0200451 return retval;
Willy Tarreau6996e152007-04-30 14:37:43 +0200452
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100453 out_shutdown_r:
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200454 /* we received a shutdown */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100455 fdtab[fd].ev &= ~FD_POLL_HUP;
456 b->flags |= BF_READ_NULL;
Willy Tarreau520d95e2009-09-19 21:04:57 +0200457 if (b->flags & BF_AUTO_CLOSE)
Willy Tarreau418fd472009-09-06 21:37:23 +0200458 buffer_shutw_now(b);
Willy Tarreaub277d6e2012-05-11 16:59:14 +0200459 sock_raw_shutr(si);
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200460 goto out_wakeup;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100461
Willy Tarreau6996e152007-04-30 14:37:43 +0200462 out_error:
Willy Tarreaucff64112008-11-03 06:26:53 +0100463 /* Read error on the file descriptor. We mark the FD as STERROR so
464 * that we don't use it anymore. The error is reported to the stream
465 * interface which will take proper action. We must not perturbate the
466 * buffer because the stream interface wants to ensure transparent
467 * connection retries.
Willy Tarreau6996e152007-04-30 14:37:43 +0200468 */
Willy Tarreaucff64112008-11-03 06:26:53 +0100469
Willy Tarreau80184712012-07-06 14:54:49 +0200470 conn->flags |= CO_FL_ERROR;
Willy Tarreau1714e0f2009-03-28 20:54:53 +0100471 EV_FD_REM(fd);
Willy Tarreau9c0fe592009-01-18 16:25:31 +0100472 retval = 1;
473 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200474}
475
476
477/*
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100478 * This function is called to send buffer data to a stream socket.
479 * It returns -1 in case of unrecoverable error, 0 if the caller needs to poll
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100480 * before calling it again, otherwise 1. If a pipe was associated with the
481 * buffer and it empties it, it releases it as well.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200482 */
Willy Tarreaub277d6e2012-05-11 16:59:14 +0200483static int sock_raw_write_loop(struct stream_interface *si, struct buffer *b)
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100484{
Willy Tarreau83749182007-04-15 20:56:27 +0200485 int write_poll = MAX_WRITE_POLL_LOOPS;
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100486 int retval = 1;
487 int ret, max;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200488
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100489#if defined(CONFIG_HAP_LINUX_SPLICE)
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100490 while (b->pipe) {
Willy Tarreaufb7508a2012-05-21 16:47:54 +0200491 ret = splice(b->pipe->cons, NULL, si_fd(si), NULL, b->pipe->data,
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100492 SPLICE_F_MOVE|SPLICE_F_NONBLOCK);
493 if (ret <= 0) {
494 if (ret == 0 || errno == EAGAIN) {
495 retval = 0;
496 return retval;
497 }
498 /* here we have another error */
499 retval = -1;
500 return retval;
501 }
502
503 b->flags |= BF_WRITE_PARTIAL;
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100504 b->pipe->data -= ret;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100505
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100506 if (!b->pipe->data) {
507 put_pipe(b->pipe);
508 b->pipe = NULL;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100509 break;
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100510 }
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100511
512 if (--write_poll <= 0)
513 return retval;
Willy Tarreaueb9fd512011-12-11 22:11:47 +0100514
515 /* The only reason we did not empty the pipe is that the output
516 * buffer is full.
517 */
518 return 0;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100519 }
520
521 /* At this point, the pipe is empty, but we may still have data pending
522 * in the normal buffer.
523 */
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100524#endif
Willy Tarreau2e046c62012-03-01 16:08:30 +0100525 if (!b->o) {
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200526 b->flags |= BF_OUT_EMPTY;
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100527 return retval;
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200528 }
Willy Tarreau83749182007-04-15 20:56:27 +0200529
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100530 /* when we're in this loop, we already know that there is no spliced
531 * data left, and that there are sendable buffered data.
532 */
Willy Tarreau6996e152007-04-30 14:37:43 +0200533 while (1) {
Willy Tarreau89fa7062012-03-02 16:13:16 +0100534 max = b->o;
Willy Tarreau83749182007-04-15 20:56:27 +0200535
Willy Tarreau89fa7062012-03-02 16:13:16 +0100536 /* outgoing data may wrap at the end */
537 if (b->data + max > b->p)
538 max = b->data + max - b->p;
Willy Tarreauf890dc92008-12-13 21:12:26 +0100539
Willy Tarreau6db06d32009-08-19 11:14:11 +0200540 /* check if we want to inform the kernel that we're interested in
541 * sending more data after this call. We want this if :
542 * - we're about to close after this last send and want to merge
543 * the ongoing FIN with the last segment.
544 * - we know we can't send everything at once and must get back
545 * here because of unaligned data
Willy Tarreaud38b53b2010-01-03 11:18:34 +0100546 * - there is still a finite amount of data to forward
Willy Tarreau6db06d32009-08-19 11:14:11 +0200547 * The test is arranged so that the most common case does only 2
548 * tests.
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200549 */
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200550
Willy Tarreauface8392010-01-03 11:37:54 +0100551 if (MSG_NOSIGNAL && MSG_MORE) {
Willy Tarreau6db06d32009-08-19 11:14:11 +0200552 unsigned int send_flag = MSG_DONTWAIT | MSG_NOSIGNAL;
553
Willy Tarreau96e31212011-05-30 18:10:30 +0200554 if ((!(b->flags & BF_NEVER_WAIT) &&
555 ((b->to_forward && b->to_forward != BUF_INFINITE_FORWARD) ||
556 (b->flags & BF_EXPECT_MORE))) ||
Willy Tarreau2e046c62012-03-01 16:08:30 +0100557 ((b->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK)) == BF_SHUTW_NOW && (max == b->o)) ||
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100558 (max != b->o)) {
Willy Tarreauface8392010-01-03 11:37:54 +0100559 send_flag |= MSG_MORE;
560 }
Willy Tarreau6db06d32009-08-19 11:14:11 +0200561
Willy Tarreau2be39392010-01-03 17:24:51 +0100562 /* this flag has precedence over the rest */
563 if (b->flags & BF_SEND_DONTWAIT)
564 send_flag &= ~MSG_MORE;
565
Willy Tarreaufb7508a2012-05-21 16:47:54 +0200566 ret = send(si_fd(si), bo_ptr(b), max, send_flag);
Willy Tarreaud6d06902009-08-19 11:22:33 +0200567 } else {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200568 int skerr;
569 socklen_t lskerr = sizeof(skerr);
570
Willy Tarreaufb7508a2012-05-21 16:47:54 +0200571 ret = getsockopt(si_fd(si), SOL_SOCKET, SO_ERROR, &skerr, &lskerr);
Willy Tarreauc6423482006-10-15 14:59:03 +0200572 if (ret == -1 || skerr)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200573 ret = -1;
574 else
Willy Tarreaufb7508a2012-05-21 16:47:54 +0200575 ret = send(si_fd(si), bo_ptr(b), max, MSG_DONTWAIT);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200576 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200577
578 if (ret > 0) {
Willy Tarreau505e34a2012-07-06 10:17:53 +0200579 if (si->conn.flags & CO_FL_WAIT_L4_CONN) {
580 si->conn.flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau8ae52cb2012-05-20 10:38:46 +0200581 si->exp = TICK_ETERNITY;
582 }
Willy Tarreaub38903c2008-11-23 21:33:29 +0100583
Willy Tarreau3da77c52008-08-29 09:58:42 +0200584 b->flags |= BF_WRITE_PARTIAL;
Willy Tarreaue393fe22008-08-16 22:18:07 +0200585
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100586 b->o -= ret;
587 if (likely(!buffer_len(b)))
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100588 /* optimize data alignment in the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +0100589 b->p = b->data;
Willy Tarreau83749182007-04-15 20:56:27 +0200590
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200591 if (likely(!bi_full(b)))
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100592 b->flags &= ~BF_FULL;
593
Willy Tarreau2e046c62012-03-01 16:08:30 +0100594 if (!b->o) {
Willy Tarreauf17810e2012-03-09 18:10:44 +0100595 /* Always clear both flags once everything has been sent, they're one-shot */
596 b->flags &= ~(BF_EXPECT_MORE | BF_SEND_DONTWAIT);
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200597 if (likely(!b->pipe))
598 b->flags |= BF_OUT_EMPTY;
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100599 break;
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200600 }
Willy Tarreau83749182007-04-15 20:56:27 +0200601
Willy Tarreauab3e1d32007-06-03 14:10:36 +0200602 /* if the system buffer is full, don't insist */
603 if (ret < max)
604 break;
605
Willy Tarreau6996e152007-04-30 14:37:43 +0200606 if (--write_poll <= 0)
607 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200608 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200609 else if (ret == 0 || errno == EAGAIN) {
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100610 /* nothing written, we need to poll for write first */
Willy Tarreau83749182007-04-15 20:56:27 +0200611 retval = 0;
612 break;
613 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200614 else {
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100615 /* bad, we got an error */
616 retval = -1;
617 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200618 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200619 } /* while (1) */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200620
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100621 return retval;
622}
Willy Tarreau6996e152007-04-30 14:37:43 +0200623
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100624
625/*
626 * This function is called on a write event from a stream socket.
627 * It returns 0 if the caller needs to poll before calling it again, otherwise
628 * non-zero.
629 */
Willy Tarreau239d7182012-07-23 18:53:03 +0200630static int sock_raw_write(struct connection *conn)
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100631{
Willy Tarreau239d7182012-07-23 18:53:03 +0200632 int fd = conn->t.sock.fd;
Willy Tarreau80184712012-07-06 14:54:49 +0200633 struct stream_interface *si = container_of(conn, struct stream_interface, conn);
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100634 struct buffer *b = si->ob;
635 int retval = 1;
636
637#ifdef DEBUG_FULL
Willy Tarreaub277d6e2012-05-11 16:59:14 +0200638 fprintf(stderr,"sock_raw_write : fd=%d, owner=%p\n", fd, fdtab[fd].owner);
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100639#endif
640
Willy Tarreau80184712012-07-06 14:54:49 +0200641 if (conn->flags & CO_FL_ERROR)
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100642 goto out_error;
643
Willy Tarreaud06e7112009-03-29 10:18:41 +0200644 /* we might have been called just after an asynchronous shutw */
645 if (b->flags & BF_SHUTW)
646 goto out_wakeup;
647
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200648 retval = sock_raw_write_loop(si, b);
649 if (retval < 0)
650 goto out_error;
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100651
Willy Tarreaufd31e532012-07-23 18:24:25 +0200652 out_wakeup:
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100653 return retval;
Willy Tarreauac128fe2009-01-09 13:05:19 +0100654
Willy Tarreau6996e152007-04-30 14:37:43 +0200655 out_error:
Willy Tarreaucff64112008-11-03 06:26:53 +0100656 /* Write error on the file descriptor. We mark the FD as STERROR so
657 * that we don't use it anymore. The error is reported to the stream
658 * interface which will take proper action. We must not perturbate the
659 * buffer because the stream interface wants to ensure transparent
660 * connection retries.
Willy Tarreau6996e152007-04-30 14:37:43 +0200661 */
Willy Tarreaucff64112008-11-03 06:26:53 +0100662
Willy Tarreau80184712012-07-06 14:54:49 +0200663 conn->flags |= CO_FL_ERROR;
Willy Tarreau1714e0f2009-03-28 20:54:53 +0100664 EV_FD_REM(fd);
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200665 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200666}
667
Willy Tarreau48adac52008-08-30 04:58:38 +0200668/*
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200669 * This function performs a shutdown-write on a stream interface in a connected or
670 * init state (it does nothing for other states). It either shuts the write side
Willy Tarreau99126c32008-11-27 10:30:51 +0100671 * or closes the file descriptor and marks itself as closed. The buffer flags are
Willy Tarreau7340ca52010-01-16 10:03:45 +0100672 * updated to reflect the new state. It does also close everything is the SI was
673 * marked as being in error state.
Willy Tarreau48adac52008-08-30 04:58:38 +0200674 */
Willy Tarreaub277d6e2012-05-11 16:59:14 +0200675static void sock_raw_shutw(struct stream_interface *si)
Willy Tarreau48adac52008-08-30 04:58:38 +0200676{
Willy Tarreau418fd472009-09-06 21:37:23 +0200677 si->ob->flags &= ~BF_SHUTW_NOW;
Willy Tarreau99126c32008-11-27 10:30:51 +0100678 if (si->ob->flags & BF_SHUTW)
679 return;
680 si->ob->flags |= BF_SHUTW;
681 si->ob->wex = TICK_ETERNITY;
Willy Tarreaub0ef7352008-12-14 13:26:20 +0100682 si->flags &= ~SI_FL_WAIT_DATA;
Willy Tarreau99126c32008-11-27 10:30:51 +0100683
Willy Tarreaub38903c2008-11-23 21:33:29 +0100684 switch (si->state) {
Willy Tarreaub38903c2008-11-23 21:33:29 +0100685 case SI_ST_EST:
Willy Tarreau720058c2009-07-14 19:21:50 +0200686 /* we have to shut before closing, otherwise some short messages
687 * may never leave the system, especially when there are remaining
688 * unread data in the socket input buffer, or when nolinger is set.
Willy Tarreau4c283dc2009-12-29 14:36:34 +0100689 * However, if SI_FL_NOLINGER is explicitly set, we know there is
690 * no risk so we close both sides immediately.
Willy Tarreau720058c2009-07-14 19:21:50 +0200691 */
Willy Tarreau7340ca52010-01-16 10:03:45 +0100692 if (si->flags & SI_FL_ERR) {
693 /* quick close, the socket is already shut. Remove pending flags. */
694 si->flags &= ~SI_FL_NOLINGER;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +0200695 }
696 else if (si->flags & SI_FL_NOLINGER) {
Willy Tarreau4c283dc2009-12-29 14:36:34 +0100697 si->flags &= ~SI_FL_NOLINGER;
Willy Tarreaufb7508a2012-05-21 16:47:54 +0200698 setsockopt(si_fd(si), SOL_SOCKET, SO_LINGER,
Willy Tarreau4c283dc2009-12-29 14:36:34 +0100699 (struct linger *) &nolinger, sizeof(struct linger));
Willy Tarreau7bb68ab2012-05-13 14:48:59 +0200700 }
701 else if (!(si->flags & SI_FL_NOHALF)) {
Willy Tarreaufb7508a2012-05-21 16:47:54 +0200702 EV_FD_CLR(si_fd(si), DIR_WR);
703 shutdown(si_fd(si), SHUT_WR);
Willy Tarreau720058c2009-07-14 19:21:50 +0200704
Willy Tarreau4c283dc2009-12-29 14:36:34 +0100705 if (!(si->ib->flags & (BF_SHUTR|BF_DONT_READ)))
706 return;
707 }
Willy Tarreau5d707e12009-06-28 11:09:07 +0200708
Willy Tarreaub38903c2008-11-23 21:33:29 +0100709 /* fall through */
710 case SI_ST_CON:
Willy Tarreau8bfa4262008-11-27 09:25:45 +0100711 /* we may have to close a pending connection, and mark the
712 * response buffer as shutr
713 */
Willy Tarreau8b117082012-08-06 15:06:49 +0200714 conn_data_close(&si->conn);
Willy Tarreaufb7508a2012-05-21 16:47:54 +0200715 fd_delete(si_fd(si));
Willy Tarreaufe3718a2008-11-30 18:14:12 +0100716 /* fall through */
717 case SI_ST_CER:
Willy Tarreau32d3ee92010-12-29 14:03:02 +0100718 case SI_ST_QUE:
719 case SI_ST_TAR:
Willy Tarreau7f006512008-12-07 14:04:04 +0100720 si->state = SI_ST_DIS;
Willy Tarreauad4cd582012-03-10 13:42:32 +0100721
722 if (si->release)
723 si->release(si);
Willy Tarreau7f006512008-12-07 14:04:04 +0100724 default:
Willy Tarreaud06e7112009-03-29 10:18:41 +0200725 si->flags &= ~SI_FL_WAIT_ROOM;
Willy Tarreau99126c32008-11-27 10:30:51 +0100726 si->ib->flags |= BF_SHUTR;
Willy Tarreaufe3718a2008-11-30 18:14:12 +0100727 si->ib->rex = TICK_ETERNITY;
Willy Tarreau127334e2009-03-28 10:47:26 +0100728 si->exp = TICK_ETERNITY;
Willy Tarreau48adac52008-08-30 04:58:38 +0200729 }
Willy Tarreau48adac52008-08-30 04:58:38 +0200730}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200731
Willy Tarreau2d212792008-08-27 21:41:35 +0200732/*
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200733 * This function performs a shutdown-read on a stream interface in a connected or
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100734 * init state (it does nothing for other states). It either shuts the read side
Willy Tarreau99126c32008-11-27 10:30:51 +0100735 * or closes the file descriptor and marks itself as closed. The buffer flags are
Willy Tarreau7bb68ab2012-05-13 14:48:59 +0200736 * updated to reflect the new state. If the stream interface has SI_FL_NOHALF,
737 * we also forward the close to the write side.
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200738 */
Willy Tarreaub277d6e2012-05-11 16:59:14 +0200739static void sock_raw_shutr(struct stream_interface *si)
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200740{
Willy Tarreau418fd472009-09-06 21:37:23 +0200741 si->ib->flags &= ~BF_SHUTR_NOW;
Willy Tarreau99126c32008-11-27 10:30:51 +0100742 if (si->ib->flags & BF_SHUTR)
743 return;
744 si->ib->flags |= BF_SHUTR;
745 si->ib->rex = TICK_ETERNITY;
Willy Tarreaub0ef7352008-12-14 13:26:20 +0100746 si->flags &= ~SI_FL_WAIT_ROOM;
Willy Tarreau99126c32008-11-27 10:30:51 +0100747
Willy Tarreau8bfa4262008-11-27 09:25:45 +0100748 if (si->state != SI_ST_EST && si->state != SI_ST_CON)
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100749 return;
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200750
Willy Tarreaucff64112008-11-03 06:26:53 +0100751 if (si->ob->flags & BF_SHUTW) {
Willy Tarreau8b117082012-08-06 15:06:49 +0200752 conn_data_close(&si->conn);
Willy Tarreaufb7508a2012-05-21 16:47:54 +0200753 fd_delete(si_fd(si));
Willy Tarreau74ab2ac2008-11-23 17:23:07 +0100754 si->state = SI_ST_DIS;
Willy Tarreau127334e2009-03-28 10:47:26 +0100755 si->exp = TICK_ETERNITY;
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200756
757 if (si->release)
758 si->release(si);
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100759 return;
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200760 }
Willy Tarreau7bb68ab2012-05-13 14:48:59 +0200761 else if (si->flags & SI_FL_NOHALF) {
762 /* we want to immediately forward this close to the write side */
763 return sock_raw_shutw(si);
764 }
Willy Tarreaufb7508a2012-05-21 16:47:54 +0200765 EV_FD_CLR(si_fd(si), DIR_RD);
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100766 return;
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200767}
768
769/*
Willy Tarreaub277d6e2012-05-11 16:59:14 +0200770 * Updates a connected sock_raw file descriptor status and timeouts
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200771 * according to the buffers' flags. It should only be called once after the
772 * buffer flags have settled down, and before they are cleared. It doesn't
773 * harm to call it as often as desired (it just slightly hurts performance).
774 */
Willy Tarreaub277d6e2012-05-11 16:59:14 +0200775static void sock_raw_data_finish(struct stream_interface *si)
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200776{
Willy Tarreaub0253252008-11-30 21:37:12 +0100777 struct buffer *ib = si->ib;
778 struct buffer *ob = si->ob;
Willy Tarreaufb7508a2012-05-21 16:47:54 +0200779 int fd = si_fd(si);
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200780
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100781 DPRINTF(stderr,"[%u] %s: fd=%d owner=%p ib=%p, ob=%p, exp(r,w)=%u,%u ibf=%08x obf=%08x ibh=%d ibt=%d obh=%d obd=%d si=%d\n",
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200782 now_ms, __FUNCTION__,
783 fd, fdtab[fd].owner,
784 ib, ob,
785 ib->rex, ob->wex,
786 ib->flags, ob->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100787 ib->i, ib->o, ob->i, ob->o, si->state);
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200788
789 /* Check if we need to close the read side */
790 if (!(ib->flags & BF_SHUTR)) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200791 /* Read not closed, update FD status and timeout for reads */
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200792 if (ib->flags & (BF_FULL|BF_HIJACK|BF_DONT_READ)) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200793 /* stop reading */
Willy Tarreau11f49402010-11-11 23:08:17 +0100794 if (!(si->flags & SI_FL_WAIT_ROOM)) {
795 if ((ib->flags & (BF_FULL|BF_HIJACK|BF_DONT_READ)) == BF_FULL)
796 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau3788e4c2012-07-30 14:29:35 +0200797 EV_FD_CLR(fd, DIR_RD);
Willy Tarreau11f49402010-11-11 23:08:17 +0100798 ib->rex = TICK_ETERNITY;
799 }
Willy Tarreau2d212792008-08-27 21:41:35 +0200800 }
801 else {
802 /* (re)start reading and update timeout. Note: we don't recompute the timeout
803 * everytime we get here, otherwise it would risk never to expire. We only
Willy Tarreaufe8903c2009-10-04 10:56:08 +0200804 * update it if is was not yet set. The stream socket handler will already
805 * have updated it if there has been a completed I/O.
Willy Tarreau2d212792008-08-27 21:41:35 +0200806 */
Willy Tarreaub0ef7352008-12-14 13:26:20 +0100807 si->flags &= ~SI_FL_WAIT_ROOM;
Willy Tarreau3788e4c2012-07-30 14:29:35 +0200808 EV_FD_SET(fd, DIR_RD);
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200809 if (!(ib->flags & (BF_READ_NOEXP|BF_DONT_READ)) && !tick_isset(ib->rex))
Willy Tarreau2d212792008-08-27 21:41:35 +0200810 ib->rex = tick_add_ifset(now_ms, ib->rto);
811 }
812 }
813
814 /* Check if we need to close the write side */
815 if (!(ob->flags & BF_SHUTW)) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200816 /* Write not closed, update FD status and timeout for writes */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200817 if (ob->flags & BF_OUT_EMPTY) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200818 /* stop writing */
Willy Tarreau11f49402010-11-11 23:08:17 +0100819 if (!(si->flags & SI_FL_WAIT_DATA)) {
820 if ((ob->flags & (BF_FULL|BF_HIJACK|BF_SHUTW_NOW)) == 0)
821 si->flags |= SI_FL_WAIT_DATA;
Willy Tarreau3788e4c2012-07-30 14:29:35 +0200822 EV_FD_CLR(fd, DIR_WR);
Willy Tarreau11f49402010-11-11 23:08:17 +0100823 ob->wex = TICK_ETERNITY;
824 }
Willy Tarreau2d212792008-08-27 21:41:35 +0200825 }
826 else {
827 /* (re)start writing and update timeout. Note: we don't recompute the timeout
828 * everytime we get here, otherwise it would risk never to expire. We only
Willy Tarreaufe8903c2009-10-04 10:56:08 +0200829 * update it if is was not yet set. The stream socket handler will already
830 * have updated it if there has been a completed I/O.
Willy Tarreau2d212792008-08-27 21:41:35 +0200831 */
Willy Tarreaub0ef7352008-12-14 13:26:20 +0100832 si->flags &= ~SI_FL_WAIT_DATA;
Willy Tarreau3788e4c2012-07-30 14:29:35 +0200833 EV_FD_SET(fd, DIR_WR);
Willy Tarreaufe8903c2009-10-04 10:56:08 +0200834 if (!tick_isset(ob->wex)) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200835 ob->wex = tick_add_ifset(now_ms, ob->wto);
Willy Tarreauf27b5ea2009-10-03 22:01:18 +0200836 if (tick_isset(ib->rex) && !(si->flags & SI_FL_INDEP_STR)) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200837 /* Note: depending on the protocol, we don't know if we're waiting
838 * for incoming data or not. So in order to prevent the socket from
839 * expiring read timeouts during writes, we refresh the read timeout,
Willy Tarreauf27b5ea2009-10-03 22:01:18 +0200840 * except if it was already infinite or if we have explicitly setup
Jamie Gloudon801a0a32012-08-25 00:18:33 -0400841 * independent streams.
Willy Tarreau2d212792008-08-27 21:41:35 +0200842 */
Willy Tarreaud06e7112009-03-29 10:18:41 +0200843 ib->rex = tick_add_ifset(now_ms, ib->rto);
Willy Tarreau2d212792008-08-27 21:41:35 +0200844 }
845 }
846 }
847 }
Willy Tarreau2d212792008-08-27 21:41:35 +0200848}
849
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100850/* This function is used for inter-stream-interface calls. It is called by the
851 * consumer to inform the producer side that it may be interested in checking
852 * for free space in the buffer. Note that it intentionally does not update
853 * timeouts, so that we can still check them later at wake-up.
854 */
Willy Tarreaub277d6e2012-05-11 16:59:14 +0200855static void sock_raw_chk_rcv(struct stream_interface *si)
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100856{
857 struct buffer *ib = si->ib;
858
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100859 DPRINTF(stderr,"[%u] %s: fd=%d owner=%p ib=%p, ob=%p, exp(r,w)=%u,%u ibf=%08x obf=%08x ibh=%d ibt=%d obh=%d obd=%d si=%d\n",
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100860 now_ms, __FUNCTION__,
Willy Tarreaufb7508a2012-05-21 16:47:54 +0200861 si_fd(si), fdtab[si_fd(si)].owner,
Vincenzo Farruggia9b97cff2009-01-30 16:49:10 +0000862 ib, si->ob,
863 ib->rex, si->ob->wex,
864 ib->flags, si->ob->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100865 ib->i, ib->o, si->ob->i, si->ob->o, si->state);
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100866
867 if (unlikely(si->state != SI_ST_EST || (ib->flags & BF_SHUTR)))
868 return;
869
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200870 if (ib->flags & (BF_FULL|BF_HIJACK|BF_DONT_READ)) {
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100871 /* stop reading */
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200872 if ((ib->flags & (BF_FULL|BF_HIJACK|BF_DONT_READ)) == BF_FULL)
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100873 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau3788e4c2012-07-30 14:29:35 +0200874 EV_FD_CLR(si_fd(si), DIR_RD);
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100875 }
876 else {
877 /* (re)start reading */
878 si->flags &= ~SI_FL_WAIT_ROOM;
Willy Tarreau3788e4c2012-07-30 14:29:35 +0200879 EV_FD_SET(si_fd(si), DIR_RD);
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100880 }
881}
882
883
884/* This function is used for inter-stream-interface calls. It is called by the
885 * producer to inform the consumer side that it may be interested in checking
886 * for data in the buffer. Note that it intentionally does not update timeouts,
887 * so that we can still check them later at wake-up.
888 */
Willy Tarreaub277d6e2012-05-11 16:59:14 +0200889static void sock_raw_chk_snd(struct stream_interface *si)
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100890{
891 struct buffer *ob = si->ob;
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100892 int retval;
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100893
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100894 DPRINTF(stderr,"[%u] %s: fd=%d owner=%p ib=%p, ob=%p, exp(r,w)=%u,%u ibf=%08x obf=%08x ibh=%d ibt=%d obh=%d obd=%d si=%d\n",
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100895 now_ms, __FUNCTION__,
Willy Tarreaufb7508a2012-05-21 16:47:54 +0200896 si_fd(si), fdtab[si_fd(si)].owner,
Vincenzo Farruggia9b97cff2009-01-30 16:49:10 +0000897 si->ib, ob,
898 si->ib->rex, ob->wex,
899 si->ib->flags, ob->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100900 si->ib->i, si->ib->o, ob->i, ob->o, si->state);
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100901
902 if (unlikely(si->state != SI_ST_EST || (ob->flags & BF_SHUTW)))
903 return;
904
Willy Tarreaua190d592012-05-20 18:35:19 +0200905 if (unlikely(ob->flags & BF_OUT_EMPTY)) /* called with nothing to send ! */
Willy Tarreaueb9fd512011-12-11 22:11:47 +0100906 return;
907
908 if (!ob->pipe && /* spliced data wants to be forwarded ASAP */
909 (!(si->flags & SI_FL_WAIT_DATA) || /* not waiting for data */
Willy Tarreaufb7508a2012-05-21 16:47:54 +0200910 (fdtab[si_fd(si)].ev & FD_POLL_OUT))) /* we'll be called anyway */
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100911 return;
912
Willy Tarreaub277d6e2012-05-11 16:59:14 +0200913 retval = sock_raw_write_loop(si, ob);
Willy Tarreauc54aef32009-07-27 20:08:06 +0200914 /* here, we have :
915 * retval < 0 if an error was encountered during write.
916 * retval = 0 if we can't write anymore without polling
917 * retval = 1 if we're invited to come back when desired
918 */
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100919 if (retval < 0) {
920 /* Write error on the file descriptor. We mark the FD as STERROR so
921 * that we don't use it anymore and we notify the task.
922 */
Willy Tarreau505e34a2012-07-06 10:17:53 +0200923 si->conn.flags |= CO_FL_ERROR;
Willy Tarreaufb7508a2012-05-21 16:47:54 +0200924 fdtab[si_fd(si)].ev &= ~FD_POLL_STICKY;
925 EV_FD_REM(si_fd(si));
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100926 si->flags |= SI_FL_ERR;
927 goto out_wakeup;
928 }
929
Willy Tarreauc54aef32009-07-27 20:08:06 +0200930 /* OK, so now we know that retval >= 0 means that some data might have
931 * been sent, and that we may have to poll first. We have to do that
932 * too if the buffer is not empty.
933 */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200934 if (ob->flags & BF_OUT_EMPTY) {
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100935 /* the connection is established but we can't write. Either the
936 * buffer is empty, or we just refrain from sending because the
Willy Tarreau2e046c62012-03-01 16:08:30 +0100937 * ->o limit was reached. Maybe we just wrote the last
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100938 * chunk and need to close.
939 */
Willy Tarreau520d95e2009-09-19 21:04:57 +0200940 if (((ob->flags & (BF_SHUTW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTW_NOW)) ==
941 (BF_AUTO_CLOSE|BF_SHUTW_NOW)) &&
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100942 (si->state == SI_ST_EST)) {
Willy Tarreaub277d6e2012-05-11 16:59:14 +0200943 sock_raw_shutw(si);
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100944 goto out_wakeup;
945 }
Willy Tarreaud06e7112009-03-29 10:18:41 +0200946
Willy Tarreau59454bf2009-09-20 11:13:40 +0200947 if ((ob->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_FULL|BF_HIJACK)) == 0)
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100948 si->flags |= SI_FL_WAIT_DATA;
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100949 ob->wex = TICK_ETERNITY;
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100950 }
951 else {
Willy Tarreauc54aef32009-07-27 20:08:06 +0200952 /* Otherwise there are remaining data to be sent in the buffer,
953 * which means we have to poll before doing so.
954 */
Willy Tarreau3788e4c2012-07-30 14:29:35 +0200955 EV_FD_SET(si_fd(si), DIR_WR);
Willy Tarreauc54aef32009-07-27 20:08:06 +0200956 si->flags &= ~SI_FL_WAIT_DATA;
957 if (!tick_isset(ob->wex))
958 ob->wex = tick_add_ifset(now_ms, ob->wto);
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100959 }
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100960
Willy Tarreauc9619462009-03-09 22:40:57 +0100961 if (likely(ob->flags & BF_WRITE_ACTIVITY)) {
962 /* update timeout if we have written something */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200963 if ((ob->flags & (BF_OUT_EMPTY|BF_SHUTW|BF_WRITE_PARTIAL)) == BF_WRITE_PARTIAL)
Willy Tarreauc9619462009-03-09 22:40:57 +0100964 ob->wex = tick_add_ifset(now_ms, ob->wto);
965
Willy Tarreauf27b5ea2009-10-03 22:01:18 +0200966 if (tick_isset(si->ib->rex) && !(si->flags & SI_FL_INDEP_STR)) {
Willy Tarreauc9619462009-03-09 22:40:57 +0100967 /* Note: to prevent the client from expiring read timeouts
Willy Tarreauf27b5ea2009-10-03 22:01:18 +0200968 * during writes, we refresh it. We only do this if the
Jamie Gloudon801a0a32012-08-25 00:18:33 -0400969 * interface is not configured for "independent streams",
Willy Tarreauf27b5ea2009-10-03 22:01:18 +0200970 * because for some applications it's better not to do this,
971 * for instance when continuously exchanging small amounts
972 * of data which can full the socket buffers long before a
973 * write timeout is detected.
Willy Tarreauc9619462009-03-09 22:40:57 +0100974 */
975 si->ib->rex = tick_add_ifset(now_ms, si->ib->rto);
976 }
977 }
978
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100979 /* in case of special condition (error, shutdown, end of write...), we
980 * have to notify the task.
981 */
982 if (likely((ob->flags & (BF_WRITE_NULL|BF_WRITE_ERROR|BF_SHUTW)) ||
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200983 ((ob->flags & BF_OUT_EMPTY) && !ob->to_forward) ||
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100984 si->state != SI_ST_EST)) {
985 out_wakeup:
Willy Tarreau89f7ef22009-09-05 20:57:35 +0200986 if (!(si->flags & SI_FL_DONT_WAKE) && si->owner)
987 task_wakeup(si->owner, TASK_WOKEN_IO);
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100988 }
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100989}
990
Willy Tarreau5c979a92012-05-07 17:15:39 +0200991/* stream sock operations */
Willy Tarreaub277d6e2012-05-11 16:59:14 +0200992struct sock_ops sock_raw = {
993 .update = sock_raw_data_finish,
994 .shutr = sock_raw_shutr,
995 .shutw = sock_raw_shutw,
996 .chk_rcv = sock_raw_chk_rcv,
997 .chk_snd = sock_raw_chk_snd,
998 .read = sock_raw_read,
999 .write = sock_raw_write,
Willy Tarreau24208272012-05-21 17:28:50 +02001000 .close = NULL,
Willy Tarreau5c979a92012-05-07 17:15:39 +02001001};
Willy Tarreaubaaee002006-06-26 02:48:02 +02001002
1003/*
1004 * Local variables:
1005 * c-indent-level: 8
1006 * c-basic-offset: 8
1007 * End:
1008 */