blob: 6a3a2565cfa3de10a20358e559a2fede07f71231 [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 Tarreauafad0e02012-08-09 14:45:22 +020046static void sock_raw_read(struct connection *conn);
47static void sock_raw_write(struct connection *conn);
Willy Tarreau3d8903f2012-08-06 17:00:18 +020048static void sock_raw_read0(struct stream_interface *si);
Willy Tarreaub277d6e2012-05-11 16:59:14 +020049static void sock_raw_chk_rcv(struct stream_interface *si);
50static void sock_raw_chk_snd(struct stream_interface *si);
51
52
Willy Tarreau6b4aad42009-01-18 21:59:13 +010053#if defined(CONFIG_HAP_LINUX_SPLICE)
Willy Tarreau43d8fb22011-08-22 17:12:02 +020054#include <common/splice.h>
Willy Tarreau5bd8c372009-01-19 00:32:22 +010055
56/* A pipe contains 16 segments max, and it's common to see segments of 1448 bytes
57 * because of timestamps. Use this as a hint for not looping on splice().
58 */
59#define SPLICE_FULL_HINT 16*1448
60
Willy Tarreaua9de3332009-11-28 07:47:10 +010061/* how many data we attempt to splice at once when the buffer is configured for
62 * infinite forwarding */
63#define MAX_SPLICE_AT_ONCE (1<<30)
64
Willy Tarreau5bd8c372009-01-19 00:32:22 +010065/* Returns :
66 * -1 if splice is not possible or not possible anymore and we must switch to
67 * user-land copy (eg: to_forward reached)
Willy Tarreauafad0e02012-08-09 14:45:22 +020068 * 0 otherwise, including errors and close.
Willy Tarreau5bd8c372009-01-19 00:32:22 +010069 * Sets :
70 * BF_READ_NULL
71 * BF_READ_PARTIAL
72 * BF_WRITE_PARTIAL (during copy)
Willy Tarreauba0b63d2009-09-20 08:09:44 +020073 * BF_OUT_EMPTY (during copy)
Willy Tarreau5bd8c372009-01-19 00:32:22 +010074 * SI_FL_ERR
75 * SI_FL_WAIT_ROOM
76 * (SI_FL_WAIT_RECV)
Willy Tarreau3eba98a2009-01-25 13:56:13 +010077 *
78 * This function automatically allocates a pipe from the pipe pool. It also
79 * carefully ensures to clear b->pipe whenever it leaves the pipe empty.
Willy Tarreau5bd8c372009-01-19 00:32:22 +010080 */
Willy Tarreaub277d6e2012-05-11 16:59:14 +020081static int sock_raw_splice_in(struct buffer *b, struct stream_interface *si)
Willy Tarreau5bd8c372009-01-19 00:32:22 +010082{
Willy Tarreau82a04562011-12-11 22:37:06 +010083 static int splice_detects_close;
Willy Tarreaufb7508a2012-05-21 16:47:54 +020084 int fd = si_fd(si);
Willy Tarreau31971e52009-09-20 12:07:52 +020085 int ret;
86 unsigned long max;
Willy Tarreauafad0e02012-08-09 14:45:22 +020087 int retval = 0;
Willy Tarreau5bd8c372009-01-19 00:32:22 +010088
89 if (!b->to_forward)
90 return -1;
91
92 if (!(b->flags & BF_KERN_SPLICING))
93 return -1;
94
Willy Tarreau02d6cfc2012-03-01 18:19:58 +010095 if (buffer_not_empty(b)) {
Willy Tarreau5bd8c372009-01-19 00:32:22 +010096 /* We're embarrassed, there are already data pending in
97 * the buffer and we don't want to have them at two
98 * locations at a time. Let's indicate we need some
99 * place and ask the consumer to hurry.
100 */
101 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200102 conn_data_stop_recv(&si->conn);
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100103 b->rex = TICK_ETERNITY;
Willy Tarreau73b013b2012-05-21 16:31:45 +0200104 si_chk_snd(b->cons);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200105 return 0;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100106 }
107
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100108 if (unlikely(b->pipe == NULL)) {
109 if (pipes_used >= global.maxpipes || !(b->pipe = get_pipe())) {
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100110 b->flags &= ~BF_KERN_SPLICING;
111 return -1;
112 }
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100113 }
114
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100115 /* At this point, b->pipe is valid */
116
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100117 while (1) {
Willy Tarreaua9de3332009-11-28 07:47:10 +0100118 if (b->to_forward == BUF_INFINITE_FORWARD)
119 max = MAX_SPLICE_AT_ONCE;
120 else
121 max = b->to_forward;
122
Willy Tarreau31971e52009-09-20 12:07:52 +0200123 if (!max) {
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100124 /* It looks like the buffer + the pipe already contain
125 * the maximum amount of data to be transferred. Try to
126 * send those data immediately on the other side if it
127 * is currently waiting.
128 */
129 retval = -1; /* end of forwarding */
130 break;
131 }
132
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100133 ret = splice(fd, NULL, b->pipe->prod, NULL, max,
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100134 SPLICE_F_MOVE|SPLICE_F_NONBLOCK);
135
136 if (ret <= 0) {
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100137 if (ret == 0) {
Willy Tarreau98b306b2009-01-25 11:11:32 +0100138 /* connection closed. This is only detected by
Willy Tarreau82a04562011-12-11 22:37:06 +0100139 * recent kernels (>= 2.6.27.13). If we notice
140 * it works, we store the info for later use.
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100141 */
Willy Tarreau82a04562011-12-11 22:37:06 +0100142 splice_detects_close = 1;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100143 b->flags |= BF_READ_NULL;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100144 break;
145 }
146
147 if (errno == EAGAIN) {
148 /* there are two reasons for EAGAIN :
149 * - nothing in the socket buffer (standard)
150 * - pipe is full
Willy Tarreau98b306b2009-01-25 11:11:32 +0100151 * - the connection is closed (kernel < 2.6.27.13)
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100152 * Since we don't know if pipe is full, we'll
153 * stop if the pipe is not empty. Anyway, we
154 * will almost always fill/empty the pipe.
155 */
156
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100157 if (b->pipe->data) {
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100158 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100159 break;
160 }
161
Willy Tarreau82a04562011-12-11 22:37:06 +0100162 /* We don't know if the connection was closed,
163 * but if we know splice detects close, then we
164 * know it for sure.
Willy Tarreau98b306b2009-01-25 11:11:32 +0100165 * But if we're called upon POLLIN with an empty
Willy Tarreau82a04562011-12-11 22:37:06 +0100166 * pipe and get EAGAIN, it is suspect enough to
Willy Tarreau98b306b2009-01-25 11:11:32 +0100167 * try to fall back to the normal recv scheme
168 * which will be able to deal with the situation.
169 */
Willy Tarreau82a04562011-12-11 22:37:06 +0100170 if (splice_detects_close)
Willy Tarreauafad0e02012-08-09 14:45:22 +0200171 conn_data_poll_recv(&si->conn); /* we know for sure that it's EAGAIN */
Willy Tarreau82a04562011-12-11 22:37:06 +0100172 else
173 retval = -1;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100174 break;
175 }
Willy Tarreaudc340a92009-06-28 23:10:19 +0200176
Willy Tarreaua9de3332009-11-28 07:47:10 +0100177 if (errno == ENOSYS || errno == EINVAL) {
Willy Tarreaudc340a92009-06-28 23:10:19 +0200178 /* splice not supported on this end, disable it */
179 b->flags &= ~BF_KERN_SPLICING;
180 si->flags &= ~SI_FL_CAP_SPLICE;
181 put_pipe(b->pipe);
182 b->pipe = NULL;
183 return -1;
184 }
185
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100186 /* here we have another error */
187 si->flags |= SI_FL_ERR;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100188 break;
189 } /* ret <= 0 */
190
Willy Tarreau31971e52009-09-20 12:07:52 +0200191 if (b->to_forward != BUF_INFINITE_FORWARD)
192 b->to_forward -= ret;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100193 b->total += ret;
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100194 b->pipe->data += ret;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100195 b->flags |= BF_READ_PARTIAL;
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200196 b->flags &= ~BF_OUT_EMPTY;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100197
Willy Tarreau6f4a82c2009-03-21 20:43:57 +0100198 if (b->pipe->data >= SPLICE_FULL_HINT ||
199 ret >= global.tune.recv_enough) {
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100200 /* We've read enough of it for this time. */
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100201 break;
202 }
203 } /* while */
204
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100205 if (unlikely(!b->pipe->data)) {
206 put_pipe(b->pipe);
207 b->pipe = NULL;
208 }
209
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100210 return retval;
211}
212
Willy Tarreau6b4aad42009-01-18 21:59:13 +0100213#endif /* CONFIG_HAP_LINUX_SPLICE */
214
215
Willy Tarreaubaaee002006-06-26 02:48:02 +0200216/*
Willy Tarreaud7971282006-07-29 18:36:34 +0200217 * this function is called on a read event from a stream socket.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200218 */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200219static void sock_raw_read(struct connection *conn)
Willy Tarreaub277d6e2012-05-11 16:59:14 +0200220{
Willy Tarreau239d7182012-07-23 18:53:03 +0200221 int fd = conn->t.sock.fd;
Willy Tarreau80184712012-07-06 14:54:49 +0200222 struct stream_interface *si = container_of(conn, struct stream_interface, conn);
Willy Tarreau48adac52008-08-30 04:58:38 +0200223 struct buffer *b = si->ib;
Willy Tarreauafad0e02012-08-09 14:45:22 +0200224 int ret, max, cur_read;
Willy Tarreaub8949f12007-03-23 22:39:59 +0100225 int read_poll = MAX_READ_POLL_LOOPS;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200226
227#ifdef DEBUG_FULL
Willy Tarreaub277d6e2012-05-11 16:59:14 +0200228 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 +0200229#endif
Willy Tarreau71543652009-07-14 19:55:05 +0200230 /* stop immediately on errors. Note that we DON'T want to stop on
231 * POLL_ERR, as the poller might report a write error while there
232 * are still data available in the recv buffer. This typically
233 * happens when we send too large a request to a backend server
234 * which rejects it before reading it all.
235 */
Willy Tarreau80184712012-07-06 14:54:49 +0200236 if (conn->flags & CO_FL_ERROR)
Willy Tarreau6996e152007-04-30 14:37:43 +0200237 goto out_error;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100238
239 /* stop here if we reached the end of data */
240 if ((fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP)) == FD_POLL_HUP)
241 goto out_shutdown_r;
Willy Tarreau83749182007-04-15 20:56:27 +0200242
Willy Tarreaud06e7112009-03-29 10:18:41 +0200243 /* maybe we were called immediately after an asynchronous shutr */
244 if (b->flags & BF_SHUTR)
Willy Tarreauafad0e02012-08-09 14:45:22 +0200245 return;
Willy Tarreaud06e7112009-03-29 10:18:41 +0200246
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100247#if defined(CONFIG_HAP_LINUX_SPLICE)
Willy Tarreau14acc702011-05-11 20:47:24 +0200248 if (b->to_forward >= MIN_SPLICE_FORWARD && b->flags & BF_KERN_SPLICING) {
Willy Tarreau98b306b2009-01-25 11:11:32 +0100249
250 /* Under Linux, if FD_POLL_HUP is set, we have reached the end.
251 * Since older splice() implementations were buggy and returned
252 * EAGAIN on end of read, let's bypass the call to splice() now.
253 */
254 if (fdtab[fd].ev & FD_POLL_HUP)
255 goto out_shutdown_r;
256
Willy Tarreauafad0e02012-08-09 14:45:22 +0200257 if (sock_raw_splice_in(b, si) >= 0) {
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100258 if (si->flags & SI_FL_ERR)
259 goto out_error;
260 if (b->flags & BF_READ_NULL)
261 goto out_shutdown_r;
Willy Tarreauafad0e02012-08-09 14:45:22 +0200262 return;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100263 }
264 /* splice not possible (anymore), let's go on on standard copy */
265 }
266#endif
Willy Tarreau8a7af602008-05-03 23:07:14 +0200267 cur_read = 0;
Willy Tarreau6996e152007-04-30 14:37:43 +0200268 while (1) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200269 max = bi_avail(b);
Willy Tarreau864e8252009-12-28 17:36:37 +0100270
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200271 if (!max) {
Willy Tarreau864e8252009-12-28 17:36:37 +0100272 b->flags |= BF_FULL;
273 si->flags |= SI_FL_WAIT_ROOM;
274 break;
275 }
276
Willy Tarreau6996e152007-04-30 14:37:43 +0200277 /*
278 * 1. compute the maximum block size we can read at once.
279 */
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100280 if (buffer_empty(b)) {
Willy Tarreau03d60bb2009-01-09 11:13:00 +0100281 /* let's realign the buffer to optimize I/O */
Willy Tarreaua458b672012-03-05 11:17:50 +0100282 b->p = b->data;
Willy Tarreau83749182007-04-15 20:56:27 +0200283 }
Willy Tarreau89fa7062012-03-02 16:13:16 +0100284 else if (b->data + b->o < b->p &&
285 b->p + b->i < b->data + b->size) {
Willy Tarreau864e8252009-12-28 17:36:37 +0100286 /* remaining space wraps at the end, with a moving limit */
Willy Tarreau363a5bb2012-03-02 20:14:45 +0100287 if (max > b->data + b->size - (b->p + b->i))
288 max = b->data + b->size - (b->p + b->i);
Willy Tarreau9c0fe592009-01-18 16:25:31 +0100289 }
Willy Tarreau864e8252009-12-28 17:36:37 +0100290 /* else max is already OK */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200291
Willy Tarreau6996e152007-04-30 14:37:43 +0200292 /*
293 * 2. read the largest possible block
294 */
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +0200295 ret = recv(fd, bi_end(b), max, 0);
Willy Tarreaud6d06902009-08-19 11:22:33 +0200296
Willy Tarreau83749182007-04-15 20:56:27 +0200297 if (ret > 0) {
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100298 b->i += ret;
Willy Tarreau8a7af602008-05-03 23:07:14 +0200299 cur_read += ret;
Willy Tarreaub38903c2008-11-23 21:33:29 +0100300
Willy Tarreau2e046c62012-03-01 16:08:30 +0100301 /* if we're allowed to directly forward data, we must update ->o */
Willy Tarreau31971e52009-09-20 12:07:52 +0200302 if (b->to_forward && !(b->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
303 unsigned long fwd = ret;
304 if (b->to_forward != BUF_INFINITE_FORWARD) {
305 if (fwd > b->to_forward)
306 fwd = b->to_forward;
307 b->to_forward -= fwd;
308 }
Willy Tarreau328582c2012-05-05 23:32:27 +0200309 b_adv(b, fwd);
Willy Tarreau0abebcc2009-01-08 00:09:41 +0100310 }
Willy Tarreauf890dc92008-12-13 21:12:26 +0100311
Willy Tarreau80184712012-07-06 14:54:49 +0200312 if (conn->flags & CO_FL_WAIT_L4_CONN) {
313 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau8ae52cb2012-05-20 10:38:46 +0200314 si->exp = TICK_ETERNITY;
315 }
Willy Tarreaub38903c2008-11-23 21:33:29 +0100316
Willy Tarreau3da77c52008-08-29 09:58:42 +0200317 b->flags |= BF_READ_PARTIAL;
Willy Tarreau83749182007-04-15 20:56:27 +0200318 b->total += ret;
Willy Tarreau9641e8f2007-03-23 23:02:09 +0100319
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200320 if (bi_full(b)) {
Willy Tarreau6996e152007-04-30 14:37:43 +0200321 /* The buffer is now full, there's no point in going through
322 * the loop again.
323 */
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100324 if (!(b->flags & BF_STREAMER_FAST) && (cur_read == buffer_len(b))) {
Willy Tarreau8a7af602008-05-03 23:07:14 +0200325 b->xfer_small = 0;
326 b->xfer_large++;
327 if (b->xfer_large >= 3) {
328 /* we call this buffer a fast streamer if it manages
329 * to be filled in one call 3 consecutive times.
330 */
331 b->flags |= (BF_STREAMER | BF_STREAMER_FAST);
332 //fputc('+', stderr);
333 }
334 }
335 else if ((b->flags & (BF_STREAMER | BF_STREAMER_FAST)) &&
Willy Tarreaua07a34e2009-08-16 23:27:46 +0200336 (cur_read <= b->size / 2)) {
Willy Tarreau8a7af602008-05-03 23:07:14 +0200337 b->xfer_large = 0;
338 b->xfer_small++;
339 if (b->xfer_small >= 2) {
340 /* if the buffer has been at least half full twice,
341 * we receive faster than we send, so at least it
342 * is not a "fast streamer".
343 */
344 b->flags &= ~BF_STREAMER_FAST;
345 //fputc('-', stderr);
346 }
347 }
348 else {
349 b->xfer_small = 0;
350 b->xfer_large = 0;
351 }
Willy Tarreau9c0fe592009-01-18 16:25:31 +0100352
353 b->flags |= BF_FULL;
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100354 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau9c0fe592009-01-18 16:25:31 +0100355 break;
Willy Tarreau6996e152007-04-30 14:37:43 +0200356 }
357
Willy Tarreauab3e1d32007-06-03 14:10:36 +0200358 /* if too many bytes were missing from last read, it means that
359 * it's pointless trying to read again because the system does
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100360 * not have them in buffers. BTW, if FD_POLL_HUP was present,
361 * it means that we have reached the end and that the connection
362 * is closed.
Willy Tarreauab3e1d32007-06-03 14:10:36 +0200363 */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100364 if (ret < max) {
Willy Tarreau8a7af602008-05-03 23:07:14 +0200365 if ((b->flags & (BF_STREAMER | BF_STREAMER_FAST)) &&
Willy Tarreaua07a34e2009-08-16 23:27:46 +0200366 (cur_read <= b->size / 2)) {
Willy Tarreau8a7af602008-05-03 23:07:14 +0200367 b->xfer_large = 0;
368 b->xfer_small++;
369 if (b->xfer_small >= 3) {
370 /* we have read less than half of the buffer in
371 * one pass, and this happened at least 3 times.
372 * This is definitely not a streamer.
373 */
374 b->flags &= ~(BF_STREAMER | BF_STREAMER_FAST);
375 //fputc('!', stderr);
376 }
377 }
Willy Tarreau2bea3a12008-08-28 09:47:43 +0200378 /* unfortunately, on level-triggered events, POLL_HUP
379 * is generally delivered AFTER the system buffer is
380 * empty, so this one might never match.
381 */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100382 if (fdtab[fd].ev & FD_POLL_HUP)
383 goto out_shutdown_r;
Willy Tarreau2bea3a12008-08-28 09:47:43 +0200384
385 /* if a streamer has read few data, it may be because we
386 * have exhausted system buffers. It's not worth trying
387 * again.
388 */
389 if (b->flags & BF_STREAMER)
390 break;
Willy Tarreauab3e1d32007-06-03 14:10:36 +0200391
Willy Tarreau6f4a82c2009-03-21 20:43:57 +0100392 /* generally if we read something smaller than 1 or 2 MSS,
393 * it means that either we have exhausted the system's
394 * buffers (streamer or question-response protocol) or
395 * that the connection will be closed. Streamers are
396 * easily detected so we return early. For other cases,
397 * it's still better to perform a last read to be sure,
398 * because it may save one complete poll/read/wakeup cycle
399 * in case of shutdown.
400 */
401 if (ret < MIN_RET_FOR_READ_LOOP && b->flags & BF_STREAMER)
402 break;
403
404 /* if we read a large block smaller than what we requested,
405 * it's almost certain we'll never get anything more.
406 */
407 if (ret >= global.tune.recv_enough)
408 break;
409 }
Willy Tarreau83749182007-04-15 20:56:27 +0200410
Willy Tarreau1b194fe2009-03-21 21:10:04 +0100411 if ((b->flags & BF_READ_DONTWAIT) || --read_poll <= 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200412 break;
Willy Tarreau83749182007-04-15 20:56:27 +0200413 }
414 else if (ret == 0) {
Willy Tarreau6996e152007-04-30 14:37:43 +0200415 /* connection closed */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100416 goto out_shutdown_r;
Willy Tarreau83749182007-04-15 20:56:27 +0200417 }
Willy Tarreau9f195292007-04-15 21:26:58 +0200418 else if (errno == EAGAIN) {
419 /* Ignore EAGAIN but inform the poller that there is
Willy Tarreauaf78d0f2009-01-08 10:09:08 +0100420 * nothing to read left if we did not read much, ie
421 * less than what we were still expecting to read.
422 * But we may have done some work justifying to notify
423 * the task.
Willy Tarreau9f195292007-04-15 21:26:58 +0200424 */
Willy Tarreauaf78d0f2009-01-08 10:09:08 +0100425 if (cur_read < MIN_RET_FOR_READ_LOOP)
Willy Tarreauafad0e02012-08-09 14:45:22 +0200426 conn_data_poll_recv(conn);
Willy Tarreau83749182007-04-15 20:56:27 +0200427 break;
428 }
429 else {
Willy Tarreau6996e152007-04-30 14:37:43 +0200430 goto out_error;
Willy Tarreau83749182007-04-15 20:56:27 +0200431 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200432 } /* while (1) */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200433
Willy Tarreauafad0e02012-08-09 14:45:22 +0200434 return;
Willy Tarreau6996e152007-04-30 14:37:43 +0200435
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100436 out_shutdown_r:
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200437 /* we received a shutdown */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100438 fdtab[fd].ev &= ~FD_POLL_HUP;
439 b->flags |= BF_READ_NULL;
Willy Tarreau520d95e2009-09-19 21:04:57 +0200440 if (b->flags & BF_AUTO_CLOSE)
Willy Tarreau418fd472009-09-06 21:37:23 +0200441 buffer_shutw_now(b);
Willy Tarreau3d8903f2012-08-06 17:00:18 +0200442 sock_raw_read0(si);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200443 return;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100444
Willy Tarreau6996e152007-04-30 14:37:43 +0200445 out_error:
Willy Tarreauafad0e02012-08-09 14:45:22 +0200446 /* Read error on the connection, report the error and stop I/O */
Willy Tarreau80184712012-07-06 14:54:49 +0200447 conn->flags |= CO_FL_ERROR;
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200448 conn_data_stop_both(conn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200449}
450
451
452/*
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100453 * This function is called to send buffer data to a stream socket.
Willy Tarreauafad0e02012-08-09 14:45:22 +0200454 * It returns -1 in case of unrecoverable error, otherwise zero.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200455 */
Willy Tarreaub277d6e2012-05-11 16:59:14 +0200456static int sock_raw_write_loop(struct stream_interface *si, struct buffer *b)
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100457{
Willy Tarreau83749182007-04-15 20:56:27 +0200458 int write_poll = MAX_WRITE_POLL_LOOPS;
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100459 int ret, max;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200460
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100461#if defined(CONFIG_HAP_LINUX_SPLICE)
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100462 while (b->pipe) {
Willy Tarreaufb7508a2012-05-21 16:47:54 +0200463 ret = splice(b->pipe->cons, NULL, si_fd(si), NULL, b->pipe->data,
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100464 SPLICE_F_MOVE|SPLICE_F_NONBLOCK);
465 if (ret <= 0) {
466 if (ret == 0 || errno == EAGAIN) {
Willy Tarreauafad0e02012-08-09 14:45:22 +0200467 conn_data_poll_send(&si->conn);
468 return 0;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100469 }
470 /* here we have another error */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200471 return -1;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100472 }
473
474 b->flags |= BF_WRITE_PARTIAL;
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100475 b->pipe->data -= ret;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100476
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100477 if (!b->pipe->data) {
478 put_pipe(b->pipe);
479 b->pipe = NULL;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100480 break;
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100481 }
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100482
483 if (--write_poll <= 0)
Willy Tarreauafad0e02012-08-09 14:45:22 +0200484 return 0;
Willy Tarreaueb9fd512011-12-11 22:11:47 +0100485
486 /* The only reason we did not empty the pipe is that the output
487 * buffer is full.
488 */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200489 conn_data_poll_send(&si->conn);
Willy Tarreaueb9fd512011-12-11 22:11:47 +0100490 return 0;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100491 }
492
493 /* At this point, the pipe is empty, but we may still have data pending
494 * in the normal buffer.
495 */
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100496#endif
Willy Tarreau2e046c62012-03-01 16:08:30 +0100497 if (!b->o) {
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200498 b->flags |= BF_OUT_EMPTY;
Willy Tarreauafad0e02012-08-09 14:45:22 +0200499 return 0;
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200500 }
Willy Tarreau83749182007-04-15 20:56:27 +0200501
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100502 /* when we're in this loop, we already know that there is no spliced
503 * data left, and that there are sendable buffered data.
504 */
Willy Tarreau6996e152007-04-30 14:37:43 +0200505 while (1) {
Willy Tarreau89fa7062012-03-02 16:13:16 +0100506 max = b->o;
Willy Tarreau83749182007-04-15 20:56:27 +0200507
Willy Tarreau89fa7062012-03-02 16:13:16 +0100508 /* outgoing data may wrap at the end */
509 if (b->data + max > b->p)
510 max = b->data + max - b->p;
Willy Tarreauf890dc92008-12-13 21:12:26 +0100511
Willy Tarreau6db06d32009-08-19 11:14:11 +0200512 /* check if we want to inform the kernel that we're interested in
513 * sending more data after this call. We want this if :
514 * - we're about to close after this last send and want to merge
515 * the ongoing FIN with the last segment.
516 * - we know we can't send everything at once and must get back
517 * here because of unaligned data
Willy Tarreaud38b53b2010-01-03 11:18:34 +0100518 * - there is still a finite amount of data to forward
Willy Tarreau6db06d32009-08-19 11:14:11 +0200519 * The test is arranged so that the most common case does only 2
520 * tests.
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200521 */
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200522
Willy Tarreauface8392010-01-03 11:37:54 +0100523 if (MSG_NOSIGNAL && MSG_MORE) {
Willy Tarreau6db06d32009-08-19 11:14:11 +0200524 unsigned int send_flag = MSG_DONTWAIT | MSG_NOSIGNAL;
525
Willy Tarreau96e31212011-05-30 18:10:30 +0200526 if ((!(b->flags & BF_NEVER_WAIT) &&
527 ((b->to_forward && b->to_forward != BUF_INFINITE_FORWARD) ||
528 (b->flags & BF_EXPECT_MORE))) ||
Willy Tarreau2e046c62012-03-01 16:08:30 +0100529 ((b->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK)) == BF_SHUTW_NOW && (max == b->o)) ||
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100530 (max != b->o)) {
Willy Tarreauface8392010-01-03 11:37:54 +0100531 send_flag |= MSG_MORE;
532 }
Willy Tarreau6db06d32009-08-19 11:14:11 +0200533
Willy Tarreau2be39392010-01-03 17:24:51 +0100534 /* this flag has precedence over the rest */
535 if (b->flags & BF_SEND_DONTWAIT)
536 send_flag &= ~MSG_MORE;
537
Willy Tarreaufb7508a2012-05-21 16:47:54 +0200538 ret = send(si_fd(si), bo_ptr(b), max, send_flag);
Willy Tarreaud6d06902009-08-19 11:22:33 +0200539 } else {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200540 int skerr;
541 socklen_t lskerr = sizeof(skerr);
542
Willy Tarreaufb7508a2012-05-21 16:47:54 +0200543 ret = getsockopt(si_fd(si), SOL_SOCKET, SO_ERROR, &skerr, &lskerr);
Willy Tarreauc6423482006-10-15 14:59:03 +0200544 if (ret == -1 || skerr)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200545 ret = -1;
546 else
Willy Tarreaufb7508a2012-05-21 16:47:54 +0200547 ret = send(si_fd(si), bo_ptr(b), max, MSG_DONTWAIT);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200548 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200549
550 if (ret > 0) {
Willy Tarreau505e34a2012-07-06 10:17:53 +0200551 if (si->conn.flags & CO_FL_WAIT_L4_CONN) {
552 si->conn.flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau8ae52cb2012-05-20 10:38:46 +0200553 si->exp = TICK_ETERNITY;
554 }
Willy Tarreaub38903c2008-11-23 21:33:29 +0100555
Willy Tarreau3da77c52008-08-29 09:58:42 +0200556 b->flags |= BF_WRITE_PARTIAL;
Willy Tarreaue393fe22008-08-16 22:18:07 +0200557
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100558 b->o -= ret;
559 if (likely(!buffer_len(b)))
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100560 /* optimize data alignment in the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +0100561 b->p = b->data;
Willy Tarreau83749182007-04-15 20:56:27 +0200562
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200563 if (likely(!bi_full(b)))
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100564 b->flags &= ~BF_FULL;
565
Willy Tarreau2e046c62012-03-01 16:08:30 +0100566 if (!b->o) {
Willy Tarreauf17810e2012-03-09 18:10:44 +0100567 /* Always clear both flags once everything has been sent, they're one-shot */
568 b->flags &= ~(BF_EXPECT_MORE | BF_SEND_DONTWAIT);
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200569 if (likely(!b->pipe))
570 b->flags |= BF_OUT_EMPTY;
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100571 break;
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200572 }
Willy Tarreau83749182007-04-15 20:56:27 +0200573
Willy Tarreauab3e1d32007-06-03 14:10:36 +0200574 /* if the system buffer is full, don't insist */
575 if (ret < max)
576 break;
577
Willy Tarreau6996e152007-04-30 14:37:43 +0200578 if (--write_poll <= 0)
579 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200580 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200581 else if (ret == 0 || errno == EAGAIN) {
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100582 /* nothing written, we need to poll for write first */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200583 conn_data_poll_send(&si->conn);
584 return 0;
Willy Tarreau83749182007-04-15 20:56:27 +0200585 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200586 else {
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100587 /* bad, we got an error */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200588 return -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200589 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200590 } /* while (1) */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200591 return 0;
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100592}
Willy Tarreau6996e152007-04-30 14:37:43 +0200593
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100594
595/*
596 * This function is called on a write event from a stream socket.
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100597 */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200598static void sock_raw_write(struct connection *conn)
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100599{
Willy Tarreau80184712012-07-06 14:54:49 +0200600 struct stream_interface *si = container_of(conn, struct stream_interface, conn);
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100601 struct buffer *b = si->ob;
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100602
603#ifdef DEBUG_FULL
Willy Tarreaub277d6e2012-05-11 16:59:14 +0200604 fprintf(stderr,"sock_raw_write : fd=%d, owner=%p\n", fd, fdtab[fd].owner);
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100605#endif
606
Willy Tarreau80184712012-07-06 14:54:49 +0200607 if (conn->flags & CO_FL_ERROR)
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100608 goto out_error;
609
Willy Tarreaud06e7112009-03-29 10:18:41 +0200610 /* we might have been called just after an asynchronous shutw */
611 if (b->flags & BF_SHUTW)
Willy Tarreauafad0e02012-08-09 14:45:22 +0200612 return;
Willy Tarreaud06e7112009-03-29 10:18:41 +0200613
Willy Tarreauafad0e02012-08-09 14:45:22 +0200614 if (sock_raw_write_loop(si, b) < 0)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200615 goto out_error;
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100616
Willy Tarreauafad0e02012-08-09 14:45:22 +0200617 /* OK all done */
618 return;
Willy Tarreauac128fe2009-01-09 13:05:19 +0100619
Willy Tarreau6996e152007-04-30 14:37:43 +0200620 out_error:
Willy Tarreauafad0e02012-08-09 14:45:22 +0200621 /* Write error on the connection, report the error and stop I/O */
Willy Tarreaucff64112008-11-03 06:26:53 +0100622
Willy Tarreau80184712012-07-06 14:54:49 +0200623 conn->flags |= CO_FL_ERROR;
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200624 conn_data_stop_both(conn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200625}
626
Willy Tarreau48adac52008-08-30 04:58:38 +0200627/*
Willy Tarreau3d8903f2012-08-06 17:00:18 +0200628 * This function propagates a null read received on a connection. It updates
629 * the stream interface. If the stream interface has SI_FL_NOHALF, we also
630 * forward the close to the write side.
631 */
632static void sock_raw_read0(struct stream_interface *si)
633{
634 si->ib->flags &= ~BF_SHUTR_NOW;
635 if (si->ib->flags & BF_SHUTR)
636 return;
637 si->ib->flags |= BF_SHUTR;
638 si->ib->rex = TICK_ETERNITY;
639 si->flags &= ~SI_FL_WAIT_ROOM;
640
641 if (si->state != SI_ST_EST && si->state != SI_ST_CON)
642 return;
643
644 if (si->ob->flags & BF_SHUTW)
645 goto do_close;
646
647 if (si->flags & SI_FL_NOHALF) {
648 /* we have to shut before closing, otherwise some short messages
649 * may never leave the system, especially when there are remaining
650 * unread data in the socket input buffer, or when nolinger is set.
651 * However, if SI_FL_NOLINGER is explicitly set, we know there is
652 * no risk so we close both sides immediately.
653 */
654 if (si->flags & SI_FL_NOLINGER) {
655 si->flags &= ~SI_FL_NOLINGER;
656 setsockopt(si_fd(si), SOL_SOCKET, SO_LINGER,
657 (struct linger *) &nolinger, sizeof(struct linger));
658 }
659 goto do_close;
660 }
661
662 /* otherwise that's just a normal read shutdown */
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200663 conn_data_stop_recv(&si->conn);
Willy Tarreau3d8903f2012-08-06 17:00:18 +0200664 return;
665
666 do_close:
667 conn_data_close(&si->conn);
668 fd_delete(si_fd(si));
669 si->state = SI_ST_DIS;
670 si->exp = TICK_ETERNITY;
671 if (si->release)
672 si->release(si);
673 return;
674}
675
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100676/* This function is used for inter-stream-interface calls. It is called by the
677 * consumer to inform the producer side that it may be interested in checking
678 * for free space in the buffer. Note that it intentionally does not update
679 * timeouts, so that we can still check them later at wake-up.
680 */
Willy Tarreaub277d6e2012-05-11 16:59:14 +0200681static void sock_raw_chk_rcv(struct stream_interface *si)
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100682{
683 struct buffer *ib = si->ib;
684
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100685 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 +0100686 now_ms, __FUNCTION__,
Willy Tarreaufb7508a2012-05-21 16:47:54 +0200687 si_fd(si), fdtab[si_fd(si)].owner,
Vincenzo Farruggia9b97cff2009-01-30 16:49:10 +0000688 ib, si->ob,
689 ib->rex, si->ob->wex,
690 ib->flags, si->ob->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100691 ib->i, ib->o, si->ob->i, si->ob->o, si->state);
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100692
693 if (unlikely(si->state != SI_ST_EST || (ib->flags & BF_SHUTR)))
694 return;
695
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200696 if (ib->flags & (BF_FULL|BF_HIJACK|BF_DONT_READ)) {
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100697 /* stop reading */
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200698 if ((ib->flags & (BF_FULL|BF_HIJACK|BF_DONT_READ)) == BF_FULL)
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100699 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200700 conn_data_stop_recv(&si->conn);
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100701 }
702 else {
703 /* (re)start reading */
704 si->flags &= ~SI_FL_WAIT_ROOM;
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200705 conn_data_want_recv(&si->conn);
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100706 }
707}
708
709
710/* This function is used for inter-stream-interface calls. It is called by the
711 * producer to inform the consumer side that it may be interested in checking
712 * for data in the buffer. Note that it intentionally does not update timeouts,
713 * so that we can still check them later at wake-up.
714 */
Willy Tarreaub277d6e2012-05-11 16:59:14 +0200715static void sock_raw_chk_snd(struct stream_interface *si)
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100716{
717 struct buffer *ob = si->ob;
718
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100719 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 +0100720 now_ms, __FUNCTION__,
Willy Tarreaufb7508a2012-05-21 16:47:54 +0200721 si_fd(si), fdtab[si_fd(si)].owner,
Vincenzo Farruggia9b97cff2009-01-30 16:49:10 +0000722 si->ib, ob,
723 si->ib->rex, ob->wex,
724 si->ib->flags, ob->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100725 si->ib->i, si->ib->o, ob->i, ob->o, si->state);
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100726
727 if (unlikely(si->state != SI_ST_EST || (ob->flags & BF_SHUTW)))
728 return;
729
Willy Tarreaua190d592012-05-20 18:35:19 +0200730 if (unlikely(ob->flags & BF_OUT_EMPTY)) /* called with nothing to send ! */
Willy Tarreaueb9fd512011-12-11 22:11:47 +0100731 return;
732
733 if (!ob->pipe && /* spliced data wants to be forwarded ASAP */
734 (!(si->flags & SI_FL_WAIT_DATA) || /* not waiting for data */
Willy Tarreaufb7508a2012-05-21 16:47:54 +0200735 (fdtab[si_fd(si)].ev & FD_POLL_OUT))) /* we'll be called anyway */
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100736 return;
737
Willy Tarreauafad0e02012-08-09 14:45:22 +0200738 if (sock_raw_write_loop(si, ob) < 0) {
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100739 /* Write error on the file descriptor. We mark the FD as STERROR so
740 * that we don't use it anymore and we notify the task.
741 */
Willy Tarreau505e34a2012-07-06 10:17:53 +0200742 si->conn.flags |= CO_FL_ERROR;
Willy Tarreaufb7508a2012-05-21 16:47:54 +0200743 fdtab[si_fd(si)].ev &= ~FD_POLL_STICKY;
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200744 conn_data_stop_both(&si->conn);
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100745 si->flags |= SI_FL_ERR;
746 goto out_wakeup;
747 }
748
Willy Tarreauafad0e02012-08-09 14:45:22 +0200749 /* OK, so now we know that some data might have been sent, and that we may
750 * have to poll first. We have to do that too if the buffer is not empty.
Willy Tarreauc54aef32009-07-27 20:08:06 +0200751 */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200752 if (ob->flags & BF_OUT_EMPTY) {
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100753 /* the connection is established but we can't write. Either the
754 * buffer is empty, or we just refrain from sending because the
Willy Tarreau2e046c62012-03-01 16:08:30 +0100755 * ->o limit was reached. Maybe we just wrote the last
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100756 * chunk and need to close.
757 */
Willy Tarreau520d95e2009-09-19 21:04:57 +0200758 if (((ob->flags & (BF_SHUTW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTW_NOW)) ==
759 (BF_AUTO_CLOSE|BF_SHUTW_NOW)) &&
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100760 (si->state == SI_ST_EST)) {
Willy Tarreau4a36b562012-08-06 19:31:45 +0200761 si_shutw(si);
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100762 goto out_wakeup;
763 }
Willy Tarreaud06e7112009-03-29 10:18:41 +0200764
Willy Tarreau59454bf2009-09-20 11:13:40 +0200765 if ((ob->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_FULL|BF_HIJACK)) == 0)
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100766 si->flags |= SI_FL_WAIT_DATA;
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100767 ob->wex = TICK_ETERNITY;
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100768 }
769 else {
Willy Tarreauc54aef32009-07-27 20:08:06 +0200770 /* Otherwise there are remaining data to be sent in the buffer,
771 * which means we have to poll before doing so.
772 */
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200773 conn_data_want_send(&si->conn);
Willy Tarreauc54aef32009-07-27 20:08:06 +0200774 si->flags &= ~SI_FL_WAIT_DATA;
775 if (!tick_isset(ob->wex))
776 ob->wex = tick_add_ifset(now_ms, ob->wto);
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100777 }
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100778
Willy Tarreauc9619462009-03-09 22:40:57 +0100779 if (likely(ob->flags & BF_WRITE_ACTIVITY)) {
780 /* update timeout if we have written something */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200781 if ((ob->flags & (BF_OUT_EMPTY|BF_SHUTW|BF_WRITE_PARTIAL)) == BF_WRITE_PARTIAL)
Willy Tarreauc9619462009-03-09 22:40:57 +0100782 ob->wex = tick_add_ifset(now_ms, ob->wto);
783
Willy Tarreauf27b5ea2009-10-03 22:01:18 +0200784 if (tick_isset(si->ib->rex) && !(si->flags & SI_FL_INDEP_STR)) {
Willy Tarreauc9619462009-03-09 22:40:57 +0100785 /* Note: to prevent the client from expiring read timeouts
Willy Tarreauf27b5ea2009-10-03 22:01:18 +0200786 * during writes, we refresh it. We only do this if the
Jamie Gloudon801a0a32012-08-25 00:18:33 -0400787 * interface is not configured for "independent streams",
Willy Tarreauf27b5ea2009-10-03 22:01:18 +0200788 * because for some applications it's better not to do this,
789 * for instance when continuously exchanging small amounts
790 * of data which can full the socket buffers long before a
791 * write timeout is detected.
Willy Tarreauc9619462009-03-09 22:40:57 +0100792 */
793 si->ib->rex = tick_add_ifset(now_ms, si->ib->rto);
794 }
795 }
796
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100797 /* in case of special condition (error, shutdown, end of write...), we
798 * have to notify the task.
799 */
800 if (likely((ob->flags & (BF_WRITE_NULL|BF_WRITE_ERROR|BF_SHUTW)) ||
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200801 ((ob->flags & BF_OUT_EMPTY) && !ob->to_forward) ||
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100802 si->state != SI_ST_EST)) {
803 out_wakeup:
Willy Tarreau89f7ef22009-09-05 20:57:35 +0200804 if (!(si->flags & SI_FL_DONT_WAKE) && si->owner)
805 task_wakeup(si->owner, TASK_WOKEN_IO);
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100806 }
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100807}
808
Willy Tarreau5c979a92012-05-07 17:15:39 +0200809/* stream sock operations */
Willy Tarreaub277d6e2012-05-11 16:59:14 +0200810struct sock_ops sock_raw = {
Willy Tarreau100c4672012-08-20 12:06:26 +0200811 .update = stream_int_update_conn,
Willy Tarreau4a36b562012-08-06 19:31:45 +0200812 .shutr = NULL,
813 .shutw = NULL,
Willy Tarreaub277d6e2012-05-11 16:59:14 +0200814 .chk_rcv = sock_raw_chk_rcv,
815 .chk_snd = sock_raw_chk_snd,
816 .read = sock_raw_read,
817 .write = sock_raw_write,
Willy Tarreau24208272012-05-21 17:28:50 +0200818 .close = NULL,
Willy Tarreau5c979a92012-05-07 17:15:39 +0200819};
Willy Tarreaubaaee002006-06-26 02:48:02 +0200820
821/*
822 * Local variables:
823 * c-indent-level: 8
824 * c-basic-offset: 8
825 * End:
826 */