blob: 6ce4c91181e1c105c7aef7614b1b634eec469496 [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 Tarreaub277d6e2012-05-11 16:59:14 +020048static void sock_raw_data_finish(struct stream_interface *si);
Willy Tarreau3d8903f2012-08-06 17:00:18 +020049static void sock_raw_read0(struct stream_interface *si);
Willy Tarreaub277d6e2012-05-11 16:59:14 +020050static void sock_raw_chk_rcv(struct stream_interface *si);
51static void sock_raw_chk_snd(struct stream_interface *si);
52
53
Willy Tarreau6b4aad42009-01-18 21:59:13 +010054#if defined(CONFIG_HAP_LINUX_SPLICE)
Willy Tarreau43d8fb22011-08-22 17:12:02 +020055#include <common/splice.h>
Willy Tarreau5bd8c372009-01-19 00:32:22 +010056
57/* A pipe contains 16 segments max, and it's common to see segments of 1448 bytes
58 * because of timestamps. Use this as a hint for not looping on splice().
59 */
60#define SPLICE_FULL_HINT 16*1448
61
Willy Tarreaua9de3332009-11-28 07:47:10 +010062/* how many data we attempt to splice at once when the buffer is configured for
63 * infinite forwarding */
64#define MAX_SPLICE_AT_ONCE (1<<30)
65
Willy Tarreau5bd8c372009-01-19 00:32:22 +010066/* Returns :
67 * -1 if splice is not possible or not possible anymore and we must switch to
68 * user-land copy (eg: to_forward reached)
Willy Tarreauafad0e02012-08-09 14:45:22 +020069 * 0 otherwise, including errors and close.
Willy Tarreau5bd8c372009-01-19 00:32:22 +010070 * Sets :
71 * BF_READ_NULL
72 * BF_READ_PARTIAL
73 * BF_WRITE_PARTIAL (during copy)
Willy Tarreauba0b63d2009-09-20 08:09:44 +020074 * BF_OUT_EMPTY (during copy)
Willy Tarreau5bd8c372009-01-19 00:32:22 +010075 * SI_FL_ERR
76 * SI_FL_WAIT_ROOM
77 * (SI_FL_WAIT_RECV)
Willy Tarreau3eba98a2009-01-25 13:56:13 +010078 *
79 * This function automatically allocates a pipe from the pipe pool. It also
80 * carefully ensures to clear b->pipe whenever it leaves the pipe empty.
Willy Tarreau5bd8c372009-01-19 00:32:22 +010081 */
Willy Tarreaub277d6e2012-05-11 16:59:14 +020082static int sock_raw_splice_in(struct buffer *b, struct stream_interface *si)
Willy Tarreau5bd8c372009-01-19 00:32:22 +010083{
Willy Tarreau82a04562011-12-11 22:37:06 +010084 static int splice_detects_close;
Willy Tarreaufb7508a2012-05-21 16:47:54 +020085 int fd = si_fd(si);
Willy Tarreau31971e52009-09-20 12:07:52 +020086 int ret;
87 unsigned long max;
Willy Tarreauafad0e02012-08-09 14:45:22 +020088 int retval = 0;
Willy Tarreau5bd8c372009-01-19 00:32:22 +010089
90 if (!b->to_forward)
91 return -1;
92
93 if (!(b->flags & BF_KERN_SPLICING))
94 return -1;
95
Willy Tarreau02d6cfc2012-03-01 18:19:58 +010096 if (buffer_not_empty(b)) {
Willy Tarreau5bd8c372009-01-19 00:32:22 +010097 /* We're embarrassed, there are already data pending in
98 * the buffer and we don't want to have them at two
99 * locations at a time. Let's indicate we need some
100 * place and ask the consumer to hurry.
101 */
102 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200103 conn_data_stop_recv(&si->conn);
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100104 b->rex = TICK_ETERNITY;
Willy Tarreau73b013b2012-05-21 16:31:45 +0200105 si_chk_snd(b->cons);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200106 return 0;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100107 }
108
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100109 if (unlikely(b->pipe == NULL)) {
110 if (pipes_used >= global.maxpipes || !(b->pipe = get_pipe())) {
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100111 b->flags &= ~BF_KERN_SPLICING;
112 return -1;
113 }
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100114 }
115
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100116 /* At this point, b->pipe is valid */
117
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100118 while (1) {
Willy Tarreaua9de3332009-11-28 07:47:10 +0100119 if (b->to_forward == BUF_INFINITE_FORWARD)
120 max = MAX_SPLICE_AT_ONCE;
121 else
122 max = b->to_forward;
123
Willy Tarreau31971e52009-09-20 12:07:52 +0200124 if (!max) {
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100125 /* It looks like the buffer + the pipe already contain
126 * the maximum amount of data to be transferred. Try to
127 * send those data immediately on the other side if it
128 * is currently waiting.
129 */
130 retval = -1; /* end of forwarding */
131 break;
132 }
133
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100134 ret = splice(fd, NULL, b->pipe->prod, NULL, max,
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100135 SPLICE_F_MOVE|SPLICE_F_NONBLOCK);
136
137 if (ret <= 0) {
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100138 if (ret == 0) {
Willy Tarreau98b306b2009-01-25 11:11:32 +0100139 /* connection closed. This is only detected by
Willy Tarreau82a04562011-12-11 22:37:06 +0100140 * recent kernels (>= 2.6.27.13). If we notice
141 * it works, we store the info for later use.
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100142 */
Willy Tarreau82a04562011-12-11 22:37:06 +0100143 splice_detects_close = 1;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100144 b->flags |= BF_READ_NULL;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100145 break;
146 }
147
148 if (errno == EAGAIN) {
149 /* there are two reasons for EAGAIN :
150 * - nothing in the socket buffer (standard)
151 * - pipe is full
Willy Tarreau98b306b2009-01-25 11:11:32 +0100152 * - the connection is closed (kernel < 2.6.27.13)
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100153 * Since we don't know if pipe is full, we'll
154 * stop if the pipe is not empty. Anyway, we
155 * will almost always fill/empty the pipe.
156 */
157
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100158 if (b->pipe->data) {
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100159 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100160 break;
161 }
162
Willy Tarreau82a04562011-12-11 22:37:06 +0100163 /* We don't know if the connection was closed,
164 * but if we know splice detects close, then we
165 * know it for sure.
Willy Tarreau98b306b2009-01-25 11:11:32 +0100166 * But if we're called upon POLLIN with an empty
Willy Tarreau82a04562011-12-11 22:37:06 +0100167 * pipe and get EAGAIN, it is suspect enough to
Willy Tarreau98b306b2009-01-25 11:11:32 +0100168 * try to fall back to the normal recv scheme
169 * which will be able to deal with the situation.
170 */
Willy Tarreau82a04562011-12-11 22:37:06 +0100171 if (splice_detects_close)
Willy Tarreauafad0e02012-08-09 14:45:22 +0200172 conn_data_poll_recv(&si->conn); /* we know for sure that it's EAGAIN */
Willy Tarreau82a04562011-12-11 22:37:06 +0100173 else
174 retval = -1;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100175 break;
176 }
Willy Tarreaudc340a92009-06-28 23:10:19 +0200177
Willy Tarreaua9de3332009-11-28 07:47:10 +0100178 if (errno == ENOSYS || errno == EINVAL) {
Willy Tarreaudc340a92009-06-28 23:10:19 +0200179 /* splice not supported on this end, disable it */
180 b->flags &= ~BF_KERN_SPLICING;
181 si->flags &= ~SI_FL_CAP_SPLICE;
182 put_pipe(b->pipe);
183 b->pipe = NULL;
184 return -1;
185 }
186
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100187 /* here we have another error */
188 si->flags |= SI_FL_ERR;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100189 break;
190 } /* ret <= 0 */
191
Willy Tarreau31971e52009-09-20 12:07:52 +0200192 if (b->to_forward != BUF_INFINITE_FORWARD)
193 b->to_forward -= ret;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100194 b->total += ret;
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100195 b->pipe->data += ret;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100196 b->flags |= BF_READ_PARTIAL;
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200197 b->flags &= ~BF_OUT_EMPTY;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100198
Willy Tarreau6f4a82c2009-03-21 20:43:57 +0100199 if (b->pipe->data >= SPLICE_FULL_HINT ||
200 ret >= global.tune.recv_enough) {
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100201 /* We've read enough of it for this time. */
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100202 break;
203 }
204 } /* while */
205
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100206 if (unlikely(!b->pipe->data)) {
207 put_pipe(b->pipe);
208 b->pipe = NULL;
209 }
210
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100211 return retval;
212}
213
Willy Tarreau6b4aad42009-01-18 21:59:13 +0100214#endif /* CONFIG_HAP_LINUX_SPLICE */
215
216
Willy Tarreaubaaee002006-06-26 02:48:02 +0200217/*
Willy Tarreaud7971282006-07-29 18:36:34 +0200218 * this function is called on a read event from a stream socket.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200219 */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200220static void sock_raw_read(struct connection *conn)
Willy Tarreaub277d6e2012-05-11 16:59:14 +0200221{
Willy Tarreau239d7182012-07-23 18:53:03 +0200222 int fd = conn->t.sock.fd;
Willy Tarreau80184712012-07-06 14:54:49 +0200223 struct stream_interface *si = container_of(conn, struct stream_interface, conn);
Willy Tarreau48adac52008-08-30 04:58:38 +0200224 struct buffer *b = si->ib;
Willy Tarreauafad0e02012-08-09 14:45:22 +0200225 int ret, max, cur_read;
Willy Tarreaub8949f12007-03-23 22:39:59 +0100226 int read_poll = MAX_READ_POLL_LOOPS;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200227
228#ifdef DEBUG_FULL
Willy Tarreaub277d6e2012-05-11 16:59:14 +0200229 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 +0200230#endif
Willy Tarreau71543652009-07-14 19:55:05 +0200231 /* stop immediately on errors. Note that we DON'T want to stop on
232 * POLL_ERR, as the poller might report a write error while there
233 * are still data available in the recv buffer. This typically
234 * happens when we send too large a request to a backend server
235 * which rejects it before reading it all.
236 */
Willy Tarreau80184712012-07-06 14:54:49 +0200237 if (conn->flags & CO_FL_ERROR)
Willy Tarreau6996e152007-04-30 14:37:43 +0200238 goto out_error;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100239
240 /* stop here if we reached the end of data */
241 if ((fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP)) == FD_POLL_HUP)
242 goto out_shutdown_r;
Willy Tarreau83749182007-04-15 20:56:27 +0200243
Willy Tarreaud06e7112009-03-29 10:18:41 +0200244 /* maybe we were called immediately after an asynchronous shutr */
245 if (b->flags & BF_SHUTR)
Willy Tarreauafad0e02012-08-09 14:45:22 +0200246 return;
Willy Tarreaud06e7112009-03-29 10:18:41 +0200247
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100248#if defined(CONFIG_HAP_LINUX_SPLICE)
Willy Tarreau14acc702011-05-11 20:47:24 +0200249 if (b->to_forward >= MIN_SPLICE_FORWARD && b->flags & BF_KERN_SPLICING) {
Willy Tarreau98b306b2009-01-25 11:11:32 +0100250
251 /* Under Linux, if FD_POLL_HUP is set, we have reached the end.
252 * Since older splice() implementations were buggy and returned
253 * EAGAIN on end of read, let's bypass the call to splice() now.
254 */
255 if (fdtab[fd].ev & FD_POLL_HUP)
256 goto out_shutdown_r;
257
Willy Tarreauafad0e02012-08-09 14:45:22 +0200258 if (sock_raw_splice_in(b, si) >= 0) {
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100259 if (si->flags & SI_FL_ERR)
260 goto out_error;
261 if (b->flags & BF_READ_NULL)
262 goto out_shutdown_r;
Willy Tarreauafad0e02012-08-09 14:45:22 +0200263 return;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100264 }
265 /* splice not possible (anymore), let's go on on standard copy */
266 }
267#endif
Willy Tarreau8a7af602008-05-03 23:07:14 +0200268 cur_read = 0;
Willy Tarreau6996e152007-04-30 14:37:43 +0200269 while (1) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200270 max = bi_avail(b);
Willy Tarreau864e8252009-12-28 17:36:37 +0100271
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200272 if (!max) {
Willy Tarreau864e8252009-12-28 17:36:37 +0100273 b->flags |= BF_FULL;
274 si->flags |= SI_FL_WAIT_ROOM;
275 break;
276 }
277
Willy Tarreau6996e152007-04-30 14:37:43 +0200278 /*
279 * 1. compute the maximum block size we can read at once.
280 */
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100281 if (buffer_empty(b)) {
Willy Tarreau03d60bb2009-01-09 11:13:00 +0100282 /* let's realign the buffer to optimize I/O */
Willy Tarreaua458b672012-03-05 11:17:50 +0100283 b->p = b->data;
Willy Tarreau83749182007-04-15 20:56:27 +0200284 }
Willy Tarreau89fa7062012-03-02 16:13:16 +0100285 else if (b->data + b->o < b->p &&
286 b->p + b->i < b->data + b->size) {
Willy Tarreau864e8252009-12-28 17:36:37 +0100287 /* remaining space wraps at the end, with a moving limit */
Willy Tarreau363a5bb2012-03-02 20:14:45 +0100288 if (max > b->data + b->size - (b->p + b->i))
289 max = b->data + b->size - (b->p + b->i);
Willy Tarreau9c0fe592009-01-18 16:25:31 +0100290 }
Willy Tarreau864e8252009-12-28 17:36:37 +0100291 /* else max is already OK */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200292
Willy Tarreau6996e152007-04-30 14:37:43 +0200293 /*
294 * 2. read the largest possible block
295 */
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +0200296 ret = recv(fd, bi_end(b), max, 0);
Willy Tarreaud6d06902009-08-19 11:22:33 +0200297
Willy Tarreau83749182007-04-15 20:56:27 +0200298 if (ret > 0) {
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100299 b->i += ret;
Willy Tarreau8a7af602008-05-03 23:07:14 +0200300 cur_read += ret;
Willy Tarreaub38903c2008-11-23 21:33:29 +0100301
Willy Tarreau2e046c62012-03-01 16:08:30 +0100302 /* if we're allowed to directly forward data, we must update ->o */
Willy Tarreau31971e52009-09-20 12:07:52 +0200303 if (b->to_forward && !(b->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
304 unsigned long fwd = ret;
305 if (b->to_forward != BUF_INFINITE_FORWARD) {
306 if (fwd > b->to_forward)
307 fwd = b->to_forward;
308 b->to_forward -= fwd;
309 }
Willy Tarreau328582c2012-05-05 23:32:27 +0200310 b_adv(b, fwd);
Willy Tarreau0abebcc2009-01-08 00:09:41 +0100311 }
Willy Tarreauf890dc92008-12-13 21:12:26 +0100312
Willy Tarreau80184712012-07-06 14:54:49 +0200313 if (conn->flags & CO_FL_WAIT_L4_CONN) {
314 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau8ae52cb2012-05-20 10:38:46 +0200315 si->exp = TICK_ETERNITY;
316 }
Willy Tarreaub38903c2008-11-23 21:33:29 +0100317
Willy Tarreau3da77c52008-08-29 09:58:42 +0200318 b->flags |= BF_READ_PARTIAL;
Willy Tarreau83749182007-04-15 20:56:27 +0200319 b->total += ret;
Willy Tarreau9641e8f2007-03-23 23:02:09 +0100320
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200321 if (bi_full(b)) {
Willy Tarreau6996e152007-04-30 14:37:43 +0200322 /* The buffer is now full, there's no point in going through
323 * the loop again.
324 */
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100325 if (!(b->flags & BF_STREAMER_FAST) && (cur_read == buffer_len(b))) {
Willy Tarreau8a7af602008-05-03 23:07:14 +0200326 b->xfer_small = 0;
327 b->xfer_large++;
328 if (b->xfer_large >= 3) {
329 /* we call this buffer a fast streamer if it manages
330 * to be filled in one call 3 consecutive times.
331 */
332 b->flags |= (BF_STREAMER | BF_STREAMER_FAST);
333 //fputc('+', stderr);
334 }
335 }
336 else if ((b->flags & (BF_STREAMER | BF_STREAMER_FAST)) &&
Willy Tarreaua07a34e2009-08-16 23:27:46 +0200337 (cur_read <= b->size / 2)) {
Willy Tarreau8a7af602008-05-03 23:07:14 +0200338 b->xfer_large = 0;
339 b->xfer_small++;
340 if (b->xfer_small >= 2) {
341 /* if the buffer has been at least half full twice,
342 * we receive faster than we send, so at least it
343 * is not a "fast streamer".
344 */
345 b->flags &= ~BF_STREAMER_FAST;
346 //fputc('-', stderr);
347 }
348 }
349 else {
350 b->xfer_small = 0;
351 b->xfer_large = 0;
352 }
Willy Tarreau9c0fe592009-01-18 16:25:31 +0100353
354 b->flags |= BF_FULL;
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100355 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau9c0fe592009-01-18 16:25:31 +0100356 break;
Willy Tarreau6996e152007-04-30 14:37:43 +0200357 }
358
Willy Tarreauab3e1d32007-06-03 14:10:36 +0200359 /* if too many bytes were missing from last read, it means that
360 * it's pointless trying to read again because the system does
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100361 * not have them in buffers. BTW, if FD_POLL_HUP was present,
362 * it means that we have reached the end and that the connection
363 * is closed.
Willy Tarreauab3e1d32007-06-03 14:10:36 +0200364 */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100365 if (ret < max) {
Willy Tarreau8a7af602008-05-03 23:07:14 +0200366 if ((b->flags & (BF_STREAMER | BF_STREAMER_FAST)) &&
Willy Tarreaua07a34e2009-08-16 23:27:46 +0200367 (cur_read <= b->size / 2)) {
Willy Tarreau8a7af602008-05-03 23:07:14 +0200368 b->xfer_large = 0;
369 b->xfer_small++;
370 if (b->xfer_small >= 3) {
371 /* we have read less than half of the buffer in
372 * one pass, and this happened at least 3 times.
373 * This is definitely not a streamer.
374 */
375 b->flags &= ~(BF_STREAMER | BF_STREAMER_FAST);
376 //fputc('!', stderr);
377 }
378 }
Willy Tarreau2bea3a12008-08-28 09:47:43 +0200379 /* unfortunately, on level-triggered events, POLL_HUP
380 * is generally delivered AFTER the system buffer is
381 * empty, so this one might never match.
382 */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100383 if (fdtab[fd].ev & FD_POLL_HUP)
384 goto out_shutdown_r;
Willy Tarreau2bea3a12008-08-28 09:47:43 +0200385
386 /* if a streamer has read few data, it may be because we
387 * have exhausted system buffers. It's not worth trying
388 * again.
389 */
390 if (b->flags & BF_STREAMER)
391 break;
Willy Tarreauab3e1d32007-06-03 14:10:36 +0200392
Willy Tarreau6f4a82c2009-03-21 20:43:57 +0100393 /* generally if we read something smaller than 1 or 2 MSS,
394 * it means that either we have exhausted the system's
395 * buffers (streamer or question-response protocol) or
396 * that the connection will be closed. Streamers are
397 * easily detected so we return early. For other cases,
398 * it's still better to perform a last read to be sure,
399 * because it may save one complete poll/read/wakeup cycle
400 * in case of shutdown.
401 */
402 if (ret < MIN_RET_FOR_READ_LOOP && b->flags & BF_STREAMER)
403 break;
404
405 /* if we read a large block smaller than what we requested,
406 * it's almost certain we'll never get anything more.
407 */
408 if (ret >= global.tune.recv_enough)
409 break;
410 }
Willy Tarreau83749182007-04-15 20:56:27 +0200411
Willy Tarreau1b194fe2009-03-21 21:10:04 +0100412 if ((b->flags & BF_READ_DONTWAIT) || --read_poll <= 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200413 break;
Willy Tarreau83749182007-04-15 20:56:27 +0200414 }
415 else if (ret == 0) {
Willy Tarreau6996e152007-04-30 14:37:43 +0200416 /* connection closed */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100417 goto out_shutdown_r;
Willy Tarreau83749182007-04-15 20:56:27 +0200418 }
Willy Tarreau9f195292007-04-15 21:26:58 +0200419 else if (errno == EAGAIN) {
420 /* Ignore EAGAIN but inform the poller that there is
Willy Tarreauaf78d0f2009-01-08 10:09:08 +0100421 * nothing to read left if we did not read much, ie
422 * less than what we were still expecting to read.
423 * But we may have done some work justifying to notify
424 * the task.
Willy Tarreau9f195292007-04-15 21:26:58 +0200425 */
Willy Tarreauaf78d0f2009-01-08 10:09:08 +0100426 if (cur_read < MIN_RET_FOR_READ_LOOP)
Willy Tarreauafad0e02012-08-09 14:45:22 +0200427 conn_data_poll_recv(conn);
Willy Tarreau83749182007-04-15 20:56:27 +0200428 break;
429 }
430 else {
Willy Tarreau6996e152007-04-30 14:37:43 +0200431 goto out_error;
Willy Tarreau83749182007-04-15 20:56:27 +0200432 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200433 } /* while (1) */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200434
Willy Tarreauafad0e02012-08-09 14:45:22 +0200435 return;
Willy Tarreau6996e152007-04-30 14:37:43 +0200436
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100437 out_shutdown_r:
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200438 /* we received a shutdown */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100439 fdtab[fd].ev &= ~FD_POLL_HUP;
440 b->flags |= BF_READ_NULL;
Willy Tarreau520d95e2009-09-19 21:04:57 +0200441 if (b->flags & BF_AUTO_CLOSE)
Willy Tarreau418fd472009-09-06 21:37:23 +0200442 buffer_shutw_now(b);
Willy Tarreau3d8903f2012-08-06 17:00:18 +0200443 sock_raw_read0(si);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200444 return;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100445
Willy Tarreau6996e152007-04-30 14:37:43 +0200446 out_error:
Willy Tarreauafad0e02012-08-09 14:45:22 +0200447 /* Read error on the connection, report the error and stop I/O */
Willy Tarreau80184712012-07-06 14:54:49 +0200448 conn->flags |= CO_FL_ERROR;
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200449 conn_data_stop_both(conn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200450}
451
452
453/*
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100454 * This function is called to send buffer data to a stream socket.
Willy Tarreauafad0e02012-08-09 14:45:22 +0200455 * It returns -1 in case of unrecoverable error, otherwise zero.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200456 */
Willy Tarreaub277d6e2012-05-11 16:59:14 +0200457static int sock_raw_write_loop(struct stream_interface *si, struct buffer *b)
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100458{
Willy Tarreau83749182007-04-15 20:56:27 +0200459 int write_poll = MAX_WRITE_POLL_LOOPS;
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100460 int ret, max;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200461
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100462#if defined(CONFIG_HAP_LINUX_SPLICE)
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100463 while (b->pipe) {
Willy Tarreaufb7508a2012-05-21 16:47:54 +0200464 ret = splice(b->pipe->cons, NULL, si_fd(si), NULL, b->pipe->data,
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100465 SPLICE_F_MOVE|SPLICE_F_NONBLOCK);
466 if (ret <= 0) {
467 if (ret == 0 || errno == EAGAIN) {
Willy Tarreauafad0e02012-08-09 14:45:22 +0200468 conn_data_poll_send(&si->conn);
469 return 0;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100470 }
471 /* here we have another error */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200472 return -1;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100473 }
474
475 b->flags |= BF_WRITE_PARTIAL;
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100476 b->pipe->data -= ret;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100477
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100478 if (!b->pipe->data) {
479 put_pipe(b->pipe);
480 b->pipe = NULL;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100481 break;
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100482 }
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100483
484 if (--write_poll <= 0)
Willy Tarreauafad0e02012-08-09 14:45:22 +0200485 return 0;
Willy Tarreaueb9fd512011-12-11 22:11:47 +0100486
487 /* The only reason we did not empty the pipe is that the output
488 * buffer is full.
489 */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200490 conn_data_poll_send(&si->conn);
Willy Tarreaueb9fd512011-12-11 22:11:47 +0100491 return 0;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100492 }
493
494 /* At this point, the pipe is empty, but we may still have data pending
495 * in the normal buffer.
496 */
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100497#endif
Willy Tarreau2e046c62012-03-01 16:08:30 +0100498 if (!b->o) {
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200499 b->flags |= BF_OUT_EMPTY;
Willy Tarreauafad0e02012-08-09 14:45:22 +0200500 return 0;
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200501 }
Willy Tarreau83749182007-04-15 20:56:27 +0200502
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100503 /* when we're in this loop, we already know that there is no spliced
504 * data left, and that there are sendable buffered data.
505 */
Willy Tarreau6996e152007-04-30 14:37:43 +0200506 while (1) {
Willy Tarreau89fa7062012-03-02 16:13:16 +0100507 max = b->o;
Willy Tarreau83749182007-04-15 20:56:27 +0200508
Willy Tarreau89fa7062012-03-02 16:13:16 +0100509 /* outgoing data may wrap at the end */
510 if (b->data + max > b->p)
511 max = b->data + max - b->p;
Willy Tarreauf890dc92008-12-13 21:12:26 +0100512
Willy Tarreau6db06d32009-08-19 11:14:11 +0200513 /* check if we want to inform the kernel that we're interested in
514 * sending more data after this call. We want this if :
515 * - we're about to close after this last send and want to merge
516 * the ongoing FIN with the last segment.
517 * - we know we can't send everything at once and must get back
518 * here because of unaligned data
Willy Tarreaud38b53b2010-01-03 11:18:34 +0100519 * - there is still a finite amount of data to forward
Willy Tarreau6db06d32009-08-19 11:14:11 +0200520 * The test is arranged so that the most common case does only 2
521 * tests.
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200522 */
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200523
Willy Tarreauface8392010-01-03 11:37:54 +0100524 if (MSG_NOSIGNAL && MSG_MORE) {
Willy Tarreau6db06d32009-08-19 11:14:11 +0200525 unsigned int send_flag = MSG_DONTWAIT | MSG_NOSIGNAL;
526
Willy Tarreau96e31212011-05-30 18:10:30 +0200527 if ((!(b->flags & BF_NEVER_WAIT) &&
528 ((b->to_forward && b->to_forward != BUF_INFINITE_FORWARD) ||
529 (b->flags & BF_EXPECT_MORE))) ||
Willy Tarreau2e046c62012-03-01 16:08:30 +0100530 ((b->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK)) == BF_SHUTW_NOW && (max == b->o)) ||
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100531 (max != b->o)) {
Willy Tarreauface8392010-01-03 11:37:54 +0100532 send_flag |= MSG_MORE;
533 }
Willy Tarreau6db06d32009-08-19 11:14:11 +0200534
Willy Tarreau2be39392010-01-03 17:24:51 +0100535 /* this flag has precedence over the rest */
536 if (b->flags & BF_SEND_DONTWAIT)
537 send_flag &= ~MSG_MORE;
538
Willy Tarreaufb7508a2012-05-21 16:47:54 +0200539 ret = send(si_fd(si), bo_ptr(b), max, send_flag);
Willy Tarreaud6d06902009-08-19 11:22:33 +0200540 } else {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200541 int skerr;
542 socklen_t lskerr = sizeof(skerr);
543
Willy Tarreaufb7508a2012-05-21 16:47:54 +0200544 ret = getsockopt(si_fd(si), SOL_SOCKET, SO_ERROR, &skerr, &lskerr);
Willy Tarreauc6423482006-10-15 14:59:03 +0200545 if (ret == -1 || skerr)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200546 ret = -1;
547 else
Willy Tarreaufb7508a2012-05-21 16:47:54 +0200548 ret = send(si_fd(si), bo_ptr(b), max, MSG_DONTWAIT);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200549 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200550
551 if (ret > 0) {
Willy Tarreau505e34a2012-07-06 10:17:53 +0200552 if (si->conn.flags & CO_FL_WAIT_L4_CONN) {
553 si->conn.flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau8ae52cb2012-05-20 10:38:46 +0200554 si->exp = TICK_ETERNITY;
555 }
Willy Tarreaub38903c2008-11-23 21:33:29 +0100556
Willy Tarreau3da77c52008-08-29 09:58:42 +0200557 b->flags |= BF_WRITE_PARTIAL;
Willy Tarreaue393fe22008-08-16 22:18:07 +0200558
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100559 b->o -= ret;
560 if (likely(!buffer_len(b)))
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100561 /* optimize data alignment in the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +0100562 b->p = b->data;
Willy Tarreau83749182007-04-15 20:56:27 +0200563
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200564 if (likely(!bi_full(b)))
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100565 b->flags &= ~BF_FULL;
566
Willy Tarreau2e046c62012-03-01 16:08:30 +0100567 if (!b->o) {
Willy Tarreauf17810e2012-03-09 18:10:44 +0100568 /* Always clear both flags once everything has been sent, they're one-shot */
569 b->flags &= ~(BF_EXPECT_MORE | BF_SEND_DONTWAIT);
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200570 if (likely(!b->pipe))
571 b->flags |= BF_OUT_EMPTY;
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100572 break;
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200573 }
Willy Tarreau83749182007-04-15 20:56:27 +0200574
Willy Tarreauab3e1d32007-06-03 14:10:36 +0200575 /* if the system buffer is full, don't insist */
576 if (ret < max)
577 break;
578
Willy Tarreau6996e152007-04-30 14:37:43 +0200579 if (--write_poll <= 0)
580 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200581 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200582 else if (ret == 0 || errno == EAGAIN) {
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100583 /* nothing written, we need to poll for write first */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200584 conn_data_poll_send(&si->conn);
585 return 0;
Willy Tarreau83749182007-04-15 20:56:27 +0200586 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200587 else {
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100588 /* bad, we got an error */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200589 return -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200590 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200591 } /* while (1) */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200592 return 0;
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100593}
Willy Tarreau6996e152007-04-30 14:37:43 +0200594
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100595
596/*
597 * This function is called on a write event from a stream socket.
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100598 */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200599static void sock_raw_write(struct connection *conn)
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100600{
Willy Tarreau80184712012-07-06 14:54:49 +0200601 struct stream_interface *si = container_of(conn, struct stream_interface, conn);
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100602 struct buffer *b = si->ob;
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100603
604#ifdef DEBUG_FULL
Willy Tarreaub277d6e2012-05-11 16:59:14 +0200605 fprintf(stderr,"sock_raw_write : fd=%d, owner=%p\n", fd, fdtab[fd].owner);
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100606#endif
607
Willy Tarreau80184712012-07-06 14:54:49 +0200608 if (conn->flags & CO_FL_ERROR)
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100609 goto out_error;
610
Willy Tarreaud06e7112009-03-29 10:18:41 +0200611 /* we might have been called just after an asynchronous shutw */
612 if (b->flags & BF_SHUTW)
Willy Tarreauafad0e02012-08-09 14:45:22 +0200613 return;
Willy Tarreaud06e7112009-03-29 10:18:41 +0200614
Willy Tarreauafad0e02012-08-09 14:45:22 +0200615 if (sock_raw_write_loop(si, b) < 0)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200616 goto out_error;
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100617
Willy Tarreauafad0e02012-08-09 14:45:22 +0200618 /* OK all done */
619 return;
Willy Tarreauac128fe2009-01-09 13:05:19 +0100620
Willy Tarreau6996e152007-04-30 14:37:43 +0200621 out_error:
Willy Tarreauafad0e02012-08-09 14:45:22 +0200622 /* Write error on the connection, report the error and stop I/O */
Willy Tarreaucff64112008-11-03 06:26:53 +0100623
Willy Tarreau80184712012-07-06 14:54:49 +0200624 conn->flags |= CO_FL_ERROR;
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200625 conn_data_stop_both(conn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200626}
627
Willy Tarreau48adac52008-08-30 04:58:38 +0200628/*
Willy Tarreau3d8903f2012-08-06 17:00:18 +0200629 * This function propagates a null read received on a connection. It updates
630 * the stream interface. If the stream interface has SI_FL_NOHALF, we also
631 * forward the close to the write side.
632 */
633static void sock_raw_read0(struct stream_interface *si)
634{
635 si->ib->flags &= ~BF_SHUTR_NOW;
636 if (si->ib->flags & BF_SHUTR)
637 return;
638 si->ib->flags |= BF_SHUTR;
639 si->ib->rex = TICK_ETERNITY;
640 si->flags &= ~SI_FL_WAIT_ROOM;
641
642 if (si->state != SI_ST_EST && si->state != SI_ST_CON)
643 return;
644
645 if (si->ob->flags & BF_SHUTW)
646 goto do_close;
647
648 if (si->flags & SI_FL_NOHALF) {
649 /* we have to shut before closing, otherwise some short messages
650 * may never leave the system, especially when there are remaining
651 * unread data in the socket input buffer, or when nolinger is set.
652 * However, if SI_FL_NOLINGER is explicitly set, we know there is
653 * no risk so we close both sides immediately.
654 */
655 if (si->flags & SI_FL_NOLINGER) {
656 si->flags &= ~SI_FL_NOLINGER;
657 setsockopt(si_fd(si), SOL_SOCKET, SO_LINGER,
658 (struct linger *) &nolinger, sizeof(struct linger));
659 }
660 goto do_close;
661 }
662
663 /* otherwise that's just a normal read shutdown */
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200664 conn_data_stop_recv(&si->conn);
Willy Tarreau3d8903f2012-08-06 17:00:18 +0200665 return;
666
667 do_close:
668 conn_data_close(&si->conn);
669 fd_delete(si_fd(si));
670 si->state = SI_ST_DIS;
671 si->exp = TICK_ETERNITY;
672 if (si->release)
673 si->release(si);
674 return;
675}
676
677/*
Willy Tarreaub277d6e2012-05-11 16:59:14 +0200678 * Updates a connected sock_raw file descriptor status and timeouts
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200679 * according to the buffers' flags. It should only be called once after the
680 * buffer flags have settled down, and before they are cleared. It doesn't
681 * harm to call it as often as desired (it just slightly hurts performance).
682 */
Willy Tarreaub277d6e2012-05-11 16:59:14 +0200683static void sock_raw_data_finish(struct stream_interface *si)
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200684{
Willy Tarreaub0253252008-11-30 21:37:12 +0100685 struct buffer *ib = si->ib;
686 struct buffer *ob = si->ob;
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200687
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100688 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 +0200689 now_ms, __FUNCTION__,
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200690 si_fd(si), fdtab[si_fd(fd)].owner,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200691 ib, ob,
692 ib->rex, ob->wex,
693 ib->flags, ob->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100694 ib->i, ib->o, ob->i, ob->o, si->state);
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200695
696 /* Check if we need to close the read side */
697 if (!(ib->flags & BF_SHUTR)) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200698 /* Read not closed, update FD status and timeout for reads */
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200699 if (ib->flags & (BF_FULL|BF_HIJACK|BF_DONT_READ)) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200700 /* stop reading */
Willy Tarreau11f49402010-11-11 23:08:17 +0100701 if (!(si->flags & SI_FL_WAIT_ROOM)) {
702 if ((ib->flags & (BF_FULL|BF_HIJACK|BF_DONT_READ)) == BF_FULL)
703 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200704 conn_data_stop_recv(&si->conn);
Willy Tarreau11f49402010-11-11 23:08:17 +0100705 ib->rex = TICK_ETERNITY;
706 }
Willy Tarreau2d212792008-08-27 21:41:35 +0200707 }
708 else {
709 /* (re)start reading and update timeout. Note: we don't recompute the timeout
710 * everytime we get here, otherwise it would risk never to expire. We only
Willy Tarreaufe8903c2009-10-04 10:56:08 +0200711 * update it if is was not yet set. The stream socket handler will already
712 * have updated it if there has been a completed I/O.
Willy Tarreau2d212792008-08-27 21:41:35 +0200713 */
Willy Tarreaub0ef7352008-12-14 13:26:20 +0100714 si->flags &= ~SI_FL_WAIT_ROOM;
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200715 conn_data_want_recv(&si->conn);
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200716 if (!(ib->flags & (BF_READ_NOEXP|BF_DONT_READ)) && !tick_isset(ib->rex))
Willy Tarreau2d212792008-08-27 21:41:35 +0200717 ib->rex = tick_add_ifset(now_ms, ib->rto);
718 }
719 }
720
721 /* Check if we need to close the write side */
722 if (!(ob->flags & BF_SHUTW)) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200723 /* Write not closed, update FD status and timeout for writes */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200724 if (ob->flags & BF_OUT_EMPTY) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200725 /* stop writing */
Willy Tarreau11f49402010-11-11 23:08:17 +0100726 if (!(si->flags & SI_FL_WAIT_DATA)) {
727 if ((ob->flags & (BF_FULL|BF_HIJACK|BF_SHUTW_NOW)) == 0)
728 si->flags |= SI_FL_WAIT_DATA;
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200729 conn_data_stop_send(&si->conn);
Willy Tarreau11f49402010-11-11 23:08:17 +0100730 ob->wex = TICK_ETERNITY;
731 }
Willy Tarreau2d212792008-08-27 21:41:35 +0200732 }
733 else {
734 /* (re)start writing and update timeout. Note: we don't recompute the timeout
735 * everytime we get here, otherwise it would risk never to expire. We only
Willy Tarreaufe8903c2009-10-04 10:56:08 +0200736 * update it if is was not yet set. The stream socket handler will already
737 * have updated it if there has been a completed I/O.
Willy Tarreau2d212792008-08-27 21:41:35 +0200738 */
Willy Tarreaub0ef7352008-12-14 13:26:20 +0100739 si->flags &= ~SI_FL_WAIT_DATA;
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200740 conn_data_want_send(&si->conn);
Willy Tarreaufe8903c2009-10-04 10:56:08 +0200741 if (!tick_isset(ob->wex)) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200742 ob->wex = tick_add_ifset(now_ms, ob->wto);
Willy Tarreauf27b5ea2009-10-03 22:01:18 +0200743 if (tick_isset(ib->rex) && !(si->flags & SI_FL_INDEP_STR)) {
Willy Tarreau2d212792008-08-27 21:41:35 +0200744 /* Note: depending on the protocol, we don't know if we're waiting
745 * for incoming data or not. So in order to prevent the socket from
746 * expiring read timeouts during writes, we refresh the read timeout,
Willy Tarreauf27b5ea2009-10-03 22:01:18 +0200747 * except if it was already infinite or if we have explicitly setup
Jamie Gloudon801a0a32012-08-25 00:18:33 -0400748 * independent streams.
Willy Tarreau2d212792008-08-27 21:41:35 +0200749 */
Willy Tarreaud06e7112009-03-29 10:18:41 +0200750 ib->rex = tick_add_ifset(now_ms, ib->rto);
Willy Tarreau2d212792008-08-27 21:41:35 +0200751 }
752 }
753 }
754 }
Willy Tarreau2d212792008-08-27 21:41:35 +0200755}
756
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100757/* This function is used for inter-stream-interface calls. It is called by the
758 * consumer to inform the producer side that it may be interested in checking
759 * for free space in the buffer. Note that it intentionally does not update
760 * timeouts, so that we can still check them later at wake-up.
761 */
Willy Tarreaub277d6e2012-05-11 16:59:14 +0200762static void sock_raw_chk_rcv(struct stream_interface *si)
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100763{
764 struct buffer *ib = si->ib;
765
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100766 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 +0100767 now_ms, __FUNCTION__,
Willy Tarreaufb7508a2012-05-21 16:47:54 +0200768 si_fd(si), fdtab[si_fd(si)].owner,
Vincenzo Farruggia9b97cff2009-01-30 16:49:10 +0000769 ib, si->ob,
770 ib->rex, si->ob->wex,
771 ib->flags, si->ob->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100772 ib->i, ib->o, si->ob->i, si->ob->o, si->state);
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100773
774 if (unlikely(si->state != SI_ST_EST || (ib->flags & BF_SHUTR)))
775 return;
776
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200777 if (ib->flags & (BF_FULL|BF_HIJACK|BF_DONT_READ)) {
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100778 /* stop reading */
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200779 if ((ib->flags & (BF_FULL|BF_HIJACK|BF_DONT_READ)) == BF_FULL)
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100780 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200781 conn_data_stop_recv(&si->conn);
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100782 }
783 else {
784 /* (re)start reading */
785 si->flags &= ~SI_FL_WAIT_ROOM;
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200786 conn_data_want_recv(&si->conn);
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100787 }
788}
789
790
791/* This function is used for inter-stream-interface calls. It is called by the
792 * producer to inform the consumer side that it may be interested in checking
793 * for data in the buffer. Note that it intentionally does not update timeouts,
794 * so that we can still check them later at wake-up.
795 */
Willy Tarreaub277d6e2012-05-11 16:59:14 +0200796static void sock_raw_chk_snd(struct stream_interface *si)
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100797{
798 struct buffer *ob = si->ob;
799
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100800 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 +0100801 now_ms, __FUNCTION__,
Willy Tarreaufb7508a2012-05-21 16:47:54 +0200802 si_fd(si), fdtab[si_fd(si)].owner,
Vincenzo Farruggia9b97cff2009-01-30 16:49:10 +0000803 si->ib, ob,
804 si->ib->rex, ob->wex,
805 si->ib->flags, ob->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100806 si->ib->i, si->ib->o, ob->i, ob->o, si->state);
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100807
808 if (unlikely(si->state != SI_ST_EST || (ob->flags & BF_SHUTW)))
809 return;
810
Willy Tarreaua190d592012-05-20 18:35:19 +0200811 if (unlikely(ob->flags & BF_OUT_EMPTY)) /* called with nothing to send ! */
Willy Tarreaueb9fd512011-12-11 22:11:47 +0100812 return;
813
814 if (!ob->pipe && /* spliced data wants to be forwarded ASAP */
815 (!(si->flags & SI_FL_WAIT_DATA) || /* not waiting for data */
Willy Tarreaufb7508a2012-05-21 16:47:54 +0200816 (fdtab[si_fd(si)].ev & FD_POLL_OUT))) /* we'll be called anyway */
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100817 return;
818
Willy Tarreauafad0e02012-08-09 14:45:22 +0200819 if (sock_raw_write_loop(si, ob) < 0) {
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100820 /* Write error on the file descriptor. We mark the FD as STERROR so
821 * that we don't use it anymore and we notify the task.
822 */
Willy Tarreau505e34a2012-07-06 10:17:53 +0200823 si->conn.flags |= CO_FL_ERROR;
Willy Tarreaufb7508a2012-05-21 16:47:54 +0200824 fdtab[si_fd(si)].ev &= ~FD_POLL_STICKY;
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200825 conn_data_stop_both(&si->conn);
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100826 si->flags |= SI_FL_ERR;
827 goto out_wakeup;
828 }
829
Willy Tarreauafad0e02012-08-09 14:45:22 +0200830 /* OK, so now we know that some data might have been sent, and that we may
831 * have to poll first. We have to do that too if the buffer is not empty.
Willy Tarreauc54aef32009-07-27 20:08:06 +0200832 */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200833 if (ob->flags & BF_OUT_EMPTY) {
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100834 /* the connection is established but we can't write. Either the
835 * buffer is empty, or we just refrain from sending because the
Willy Tarreau2e046c62012-03-01 16:08:30 +0100836 * ->o limit was reached. Maybe we just wrote the last
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100837 * chunk and need to close.
838 */
Willy Tarreau520d95e2009-09-19 21:04:57 +0200839 if (((ob->flags & (BF_SHUTW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTW_NOW)) ==
840 (BF_AUTO_CLOSE|BF_SHUTW_NOW)) &&
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100841 (si->state == SI_ST_EST)) {
Willy Tarreau4a36b562012-08-06 19:31:45 +0200842 si_shutw(si);
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100843 goto out_wakeup;
844 }
Willy Tarreaud06e7112009-03-29 10:18:41 +0200845
Willy Tarreau59454bf2009-09-20 11:13:40 +0200846 if ((ob->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_FULL|BF_HIJACK)) == 0)
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100847 si->flags |= SI_FL_WAIT_DATA;
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100848 ob->wex = TICK_ETERNITY;
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100849 }
850 else {
Willy Tarreauc54aef32009-07-27 20:08:06 +0200851 /* Otherwise there are remaining data to be sent in the buffer,
852 * which means we have to poll before doing so.
853 */
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200854 conn_data_want_send(&si->conn);
Willy Tarreauc54aef32009-07-27 20:08:06 +0200855 si->flags &= ~SI_FL_WAIT_DATA;
856 if (!tick_isset(ob->wex))
857 ob->wex = tick_add_ifset(now_ms, ob->wto);
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100858 }
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100859
Willy Tarreauc9619462009-03-09 22:40:57 +0100860 if (likely(ob->flags & BF_WRITE_ACTIVITY)) {
861 /* update timeout if we have written something */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200862 if ((ob->flags & (BF_OUT_EMPTY|BF_SHUTW|BF_WRITE_PARTIAL)) == BF_WRITE_PARTIAL)
Willy Tarreauc9619462009-03-09 22:40:57 +0100863 ob->wex = tick_add_ifset(now_ms, ob->wto);
864
Willy Tarreauf27b5ea2009-10-03 22:01:18 +0200865 if (tick_isset(si->ib->rex) && !(si->flags & SI_FL_INDEP_STR)) {
Willy Tarreauc9619462009-03-09 22:40:57 +0100866 /* Note: to prevent the client from expiring read timeouts
Willy Tarreauf27b5ea2009-10-03 22:01:18 +0200867 * during writes, we refresh it. We only do this if the
Jamie Gloudon801a0a32012-08-25 00:18:33 -0400868 * interface is not configured for "independent streams",
Willy Tarreauf27b5ea2009-10-03 22:01:18 +0200869 * because for some applications it's better not to do this,
870 * for instance when continuously exchanging small amounts
871 * of data which can full the socket buffers long before a
872 * write timeout is detected.
Willy Tarreauc9619462009-03-09 22:40:57 +0100873 */
874 si->ib->rex = tick_add_ifset(now_ms, si->ib->rto);
875 }
876 }
877
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100878 /* in case of special condition (error, shutdown, end of write...), we
879 * have to notify the task.
880 */
881 if (likely((ob->flags & (BF_WRITE_NULL|BF_WRITE_ERROR|BF_SHUTW)) ||
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200882 ((ob->flags & BF_OUT_EMPTY) && !ob->to_forward) ||
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100883 si->state != SI_ST_EST)) {
884 out_wakeup:
Willy Tarreau89f7ef22009-09-05 20:57:35 +0200885 if (!(si->flags & SI_FL_DONT_WAKE) && si->owner)
886 task_wakeup(si->owner, TASK_WOKEN_IO);
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100887 }
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100888}
889
Willy Tarreau5c979a92012-05-07 17:15:39 +0200890/* stream sock operations */
Willy Tarreaub277d6e2012-05-11 16:59:14 +0200891struct sock_ops sock_raw = {
892 .update = sock_raw_data_finish,
Willy Tarreau4a36b562012-08-06 19:31:45 +0200893 .shutr = NULL,
894 .shutw = NULL,
Willy Tarreaub277d6e2012-05-11 16:59:14 +0200895 .chk_rcv = sock_raw_chk_rcv,
896 .chk_snd = sock_raw_chk_snd,
897 .read = sock_raw_read,
898 .write = sock_raw_write,
Willy Tarreau24208272012-05-21 17:28:50 +0200899 .close = NULL,
Willy Tarreau5c979a92012-05-07 17:15:39 +0200900};
Willy Tarreaubaaee002006-06-26 02:48:02 +0200901
902/*
903 * Local variables:
904 * c-indent-level: 8
905 * c-basic-offset: 8
906 * End:
907 */