blob: 5b20f2ff59baf9c5ca141a522e06f2daf528836b [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);
Willy Tarreau3d8903f2012-08-06 17:00:18 +020047static void sock_raw_read0(struct stream_interface *si);
Willy Tarreaub277d6e2012-05-11 16:59:14 +020048
49
Willy Tarreau6b4aad42009-01-18 21:59:13 +010050#if defined(CONFIG_HAP_LINUX_SPLICE)
Willy Tarreau43d8fb22011-08-22 17:12:02 +020051#include <common/splice.h>
Willy Tarreau5bd8c372009-01-19 00:32:22 +010052
53/* A pipe contains 16 segments max, and it's common to see segments of 1448 bytes
54 * because of timestamps. Use this as a hint for not looping on splice().
55 */
56#define SPLICE_FULL_HINT 16*1448
57
Willy Tarreaua9de3332009-11-28 07:47:10 +010058/* how many data we attempt to splice at once when the buffer is configured for
59 * infinite forwarding */
60#define MAX_SPLICE_AT_ONCE (1<<30)
61
Willy Tarreau5bd8c372009-01-19 00:32:22 +010062/* Returns :
63 * -1 if splice is not possible or not possible anymore and we must switch to
64 * user-land copy (eg: to_forward reached)
Willy Tarreauafad0e02012-08-09 14:45:22 +020065 * 0 otherwise, including errors and close.
Willy Tarreau5bd8c372009-01-19 00:32:22 +010066 * Sets :
67 * BF_READ_NULL
68 * BF_READ_PARTIAL
69 * BF_WRITE_PARTIAL (during copy)
Willy Tarreauba0b63d2009-09-20 08:09:44 +020070 * BF_OUT_EMPTY (during copy)
Willy Tarreau5bd8c372009-01-19 00:32:22 +010071 * SI_FL_ERR
72 * SI_FL_WAIT_ROOM
73 * (SI_FL_WAIT_RECV)
Willy Tarreau3eba98a2009-01-25 13:56:13 +010074 *
75 * This function automatically allocates a pipe from the pipe pool. It also
76 * carefully ensures to clear b->pipe whenever it leaves the pipe empty.
Willy Tarreau5bd8c372009-01-19 00:32:22 +010077 */
Willy Tarreaub277d6e2012-05-11 16:59:14 +020078static int sock_raw_splice_in(struct buffer *b, struct stream_interface *si)
Willy Tarreau5bd8c372009-01-19 00:32:22 +010079{
Willy Tarreau82a04562011-12-11 22:37:06 +010080 static int splice_detects_close;
Willy Tarreaufb7508a2012-05-21 16:47:54 +020081 int fd = si_fd(si);
Willy Tarreau31971e52009-09-20 12:07:52 +020082 int ret;
83 unsigned long max;
Willy Tarreauafad0e02012-08-09 14:45:22 +020084 int retval = 0;
Willy Tarreau5bd8c372009-01-19 00:32:22 +010085
86 if (!b->to_forward)
87 return -1;
88
89 if (!(b->flags & BF_KERN_SPLICING))
90 return -1;
91
Willy Tarreau02d6cfc2012-03-01 18:19:58 +010092 if (buffer_not_empty(b)) {
Willy Tarreau5bd8c372009-01-19 00:32:22 +010093 /* We're embarrassed, there are already data pending in
94 * the buffer and we don't want to have them at two
95 * locations at a time. Let's indicate we need some
96 * place and ask the consumer to hurry.
97 */
98 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreauf9dabec2012-08-17 17:33:53 +020099 conn_data_stop_recv(&si->conn);
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100100 b->rex = TICK_ETERNITY;
Willy Tarreau73b013b2012-05-21 16:31:45 +0200101 si_chk_snd(b->cons);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200102 return 0;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100103 }
104
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100105 if (unlikely(b->pipe == NULL)) {
106 if (pipes_used >= global.maxpipes || !(b->pipe = get_pipe())) {
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100107 b->flags &= ~BF_KERN_SPLICING;
108 return -1;
109 }
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100110 }
111
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100112 /* At this point, b->pipe is valid */
113
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100114 while (1) {
Willy Tarreaua9de3332009-11-28 07:47:10 +0100115 if (b->to_forward == BUF_INFINITE_FORWARD)
116 max = MAX_SPLICE_AT_ONCE;
117 else
118 max = b->to_forward;
119
Willy Tarreau31971e52009-09-20 12:07:52 +0200120 if (!max) {
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100121 /* It looks like the buffer + the pipe already contain
122 * the maximum amount of data to be transferred. Try to
123 * send those data immediately on the other side if it
124 * is currently waiting.
125 */
126 retval = -1; /* end of forwarding */
127 break;
128 }
129
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100130 ret = splice(fd, NULL, b->pipe->prod, NULL, max,
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100131 SPLICE_F_MOVE|SPLICE_F_NONBLOCK);
132
133 if (ret <= 0) {
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100134 if (ret == 0) {
Willy Tarreau98b306b2009-01-25 11:11:32 +0100135 /* connection closed. This is only detected by
Willy Tarreau82a04562011-12-11 22:37:06 +0100136 * recent kernels (>= 2.6.27.13). If we notice
137 * it works, we store the info for later use.
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100138 */
Willy Tarreau82a04562011-12-11 22:37:06 +0100139 splice_detects_close = 1;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100140 b->flags |= BF_READ_NULL;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100141 break;
142 }
143
144 if (errno == EAGAIN) {
145 /* there are two reasons for EAGAIN :
146 * - nothing in the socket buffer (standard)
147 * - pipe is full
Willy Tarreau98b306b2009-01-25 11:11:32 +0100148 * - the connection is closed (kernel < 2.6.27.13)
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100149 * Since we don't know if pipe is full, we'll
150 * stop if the pipe is not empty. Anyway, we
151 * will almost always fill/empty the pipe.
152 */
153
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100154 if (b->pipe->data) {
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100155 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100156 break;
157 }
158
Willy Tarreau82a04562011-12-11 22:37:06 +0100159 /* We don't know if the connection was closed,
160 * but if we know splice detects close, then we
161 * know it for sure.
Willy Tarreau98b306b2009-01-25 11:11:32 +0100162 * But if we're called upon POLLIN with an empty
Willy Tarreau82a04562011-12-11 22:37:06 +0100163 * pipe and get EAGAIN, it is suspect enough to
Willy Tarreau98b306b2009-01-25 11:11:32 +0100164 * try to fall back to the normal recv scheme
165 * which will be able to deal with the situation.
166 */
Willy Tarreau82a04562011-12-11 22:37:06 +0100167 if (splice_detects_close)
Willy Tarreauafad0e02012-08-09 14:45:22 +0200168 conn_data_poll_recv(&si->conn); /* we know for sure that it's EAGAIN */
Willy Tarreau82a04562011-12-11 22:37:06 +0100169 else
170 retval = -1;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100171 break;
172 }
Willy Tarreaudc340a92009-06-28 23:10:19 +0200173
Willy Tarreaua9de3332009-11-28 07:47:10 +0100174 if (errno == ENOSYS || errno == EINVAL) {
Willy Tarreaudc340a92009-06-28 23:10:19 +0200175 /* splice not supported on this end, disable it */
176 b->flags &= ~BF_KERN_SPLICING;
177 si->flags &= ~SI_FL_CAP_SPLICE;
178 put_pipe(b->pipe);
179 b->pipe = NULL;
180 return -1;
181 }
182
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100183 /* here we have another error */
184 si->flags |= SI_FL_ERR;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100185 break;
186 } /* ret <= 0 */
187
Willy Tarreau31971e52009-09-20 12:07:52 +0200188 if (b->to_forward != BUF_INFINITE_FORWARD)
189 b->to_forward -= ret;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100190 b->total += ret;
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100191 b->pipe->data += ret;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100192 b->flags |= BF_READ_PARTIAL;
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200193 b->flags &= ~BF_OUT_EMPTY;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100194
Willy Tarreau6f4a82c2009-03-21 20:43:57 +0100195 if (b->pipe->data >= SPLICE_FULL_HINT ||
196 ret >= global.tune.recv_enough) {
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100197 /* We've read enough of it for this time. */
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100198 break;
199 }
200 } /* while */
201
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100202 if (unlikely(!b->pipe->data)) {
203 put_pipe(b->pipe);
204 b->pipe = NULL;
205 }
206
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100207 return retval;
208}
209
Willy Tarreau6b4aad42009-01-18 21:59:13 +0100210#endif /* CONFIG_HAP_LINUX_SPLICE */
211
212
Willy Tarreaubaaee002006-06-26 02:48:02 +0200213/*
Willy Tarreaud7971282006-07-29 18:36:34 +0200214 * this function is called on a read event from a stream socket.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200215 */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200216static void sock_raw_read(struct connection *conn)
Willy Tarreaub277d6e2012-05-11 16:59:14 +0200217{
Willy Tarreau239d7182012-07-23 18:53:03 +0200218 int fd = conn->t.sock.fd;
Willy Tarreau80184712012-07-06 14:54:49 +0200219 struct stream_interface *si = container_of(conn, struct stream_interface, conn);
Willy Tarreau48adac52008-08-30 04:58:38 +0200220 struct buffer *b = si->ib;
Willy Tarreauafad0e02012-08-09 14:45:22 +0200221 int ret, max, cur_read;
Willy Tarreaub8949f12007-03-23 22:39:59 +0100222 int read_poll = MAX_READ_POLL_LOOPS;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200223
224#ifdef DEBUG_FULL
Willy Tarreaub277d6e2012-05-11 16:59:14 +0200225 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 +0200226#endif
Willy Tarreau71543652009-07-14 19:55:05 +0200227 /* stop immediately on errors. Note that we DON'T want to stop on
228 * POLL_ERR, as the poller might report a write error while there
229 * are still data available in the recv buffer. This typically
230 * happens when we send too large a request to a backend server
231 * which rejects it before reading it all.
232 */
Willy Tarreau80184712012-07-06 14:54:49 +0200233 if (conn->flags & CO_FL_ERROR)
Willy Tarreau6996e152007-04-30 14:37:43 +0200234 goto out_error;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100235
236 /* stop here if we reached the end of data */
237 if ((fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP)) == FD_POLL_HUP)
238 goto out_shutdown_r;
Willy Tarreau83749182007-04-15 20:56:27 +0200239
Willy Tarreaud06e7112009-03-29 10:18:41 +0200240 /* maybe we were called immediately after an asynchronous shutr */
241 if (b->flags & BF_SHUTR)
Willy Tarreauafad0e02012-08-09 14:45:22 +0200242 return;
Willy Tarreaud06e7112009-03-29 10:18:41 +0200243
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100244#if defined(CONFIG_HAP_LINUX_SPLICE)
Willy Tarreau14acc702011-05-11 20:47:24 +0200245 if (b->to_forward >= MIN_SPLICE_FORWARD && b->flags & BF_KERN_SPLICING) {
Willy Tarreau98b306b2009-01-25 11:11:32 +0100246
247 /* Under Linux, if FD_POLL_HUP is set, we have reached the end.
248 * Since older splice() implementations were buggy and returned
249 * EAGAIN on end of read, let's bypass the call to splice() now.
250 */
251 if (fdtab[fd].ev & FD_POLL_HUP)
252 goto out_shutdown_r;
253
Willy Tarreauafad0e02012-08-09 14:45:22 +0200254 if (sock_raw_splice_in(b, si) >= 0) {
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100255 if (si->flags & SI_FL_ERR)
256 goto out_error;
257 if (b->flags & BF_READ_NULL)
258 goto out_shutdown_r;
Willy Tarreauafad0e02012-08-09 14:45:22 +0200259 return;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100260 }
261 /* splice not possible (anymore), let's go on on standard copy */
262 }
263#endif
Willy Tarreau8a7af602008-05-03 23:07:14 +0200264 cur_read = 0;
Willy Tarreau6996e152007-04-30 14:37:43 +0200265 while (1) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200266 max = bi_avail(b);
Willy Tarreau864e8252009-12-28 17:36:37 +0100267
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200268 if (!max) {
Willy Tarreau864e8252009-12-28 17:36:37 +0100269 b->flags |= BF_FULL;
270 si->flags |= SI_FL_WAIT_ROOM;
271 break;
272 }
273
Willy Tarreau6996e152007-04-30 14:37:43 +0200274 /*
275 * 1. compute the maximum block size we can read at once.
276 */
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100277 if (buffer_empty(b)) {
Willy Tarreau03d60bb2009-01-09 11:13:00 +0100278 /* let's realign the buffer to optimize I/O */
Willy Tarreaua458b672012-03-05 11:17:50 +0100279 b->p = b->data;
Willy Tarreau83749182007-04-15 20:56:27 +0200280 }
Willy Tarreau89fa7062012-03-02 16:13:16 +0100281 else if (b->data + b->o < b->p &&
282 b->p + b->i < b->data + b->size) {
Willy Tarreau864e8252009-12-28 17:36:37 +0100283 /* remaining space wraps at the end, with a moving limit */
Willy Tarreau363a5bb2012-03-02 20:14:45 +0100284 if (max > b->data + b->size - (b->p + b->i))
285 max = b->data + b->size - (b->p + b->i);
Willy Tarreau9c0fe592009-01-18 16:25:31 +0100286 }
Willy Tarreau864e8252009-12-28 17:36:37 +0100287 /* else max is already OK */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200288
Willy Tarreau6996e152007-04-30 14:37:43 +0200289 /*
290 * 2. read the largest possible block
291 */
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +0200292 ret = recv(fd, bi_end(b), max, 0);
Willy Tarreaud6d06902009-08-19 11:22:33 +0200293
Willy Tarreau83749182007-04-15 20:56:27 +0200294 if (ret > 0) {
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100295 b->i += ret;
Willy Tarreau8a7af602008-05-03 23:07:14 +0200296 cur_read += ret;
Willy Tarreaub38903c2008-11-23 21:33:29 +0100297
Willy Tarreau2e046c62012-03-01 16:08:30 +0100298 /* if we're allowed to directly forward data, we must update ->o */
Willy Tarreau31971e52009-09-20 12:07:52 +0200299 if (b->to_forward && !(b->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
300 unsigned long fwd = ret;
301 if (b->to_forward != BUF_INFINITE_FORWARD) {
302 if (fwd > b->to_forward)
303 fwd = b->to_forward;
304 b->to_forward -= fwd;
305 }
Willy Tarreau328582c2012-05-05 23:32:27 +0200306 b_adv(b, fwd);
Willy Tarreau0abebcc2009-01-08 00:09:41 +0100307 }
Willy Tarreauf890dc92008-12-13 21:12:26 +0100308
Willy Tarreau80184712012-07-06 14:54:49 +0200309 if (conn->flags & CO_FL_WAIT_L4_CONN) {
310 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau8ae52cb2012-05-20 10:38:46 +0200311 si->exp = TICK_ETERNITY;
312 }
Willy Tarreaub38903c2008-11-23 21:33:29 +0100313
Willy Tarreau3da77c52008-08-29 09:58:42 +0200314 b->flags |= BF_READ_PARTIAL;
Willy Tarreau83749182007-04-15 20:56:27 +0200315 b->total += ret;
Willy Tarreau9641e8f2007-03-23 23:02:09 +0100316
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200317 if (bi_full(b)) {
Willy Tarreau6996e152007-04-30 14:37:43 +0200318 /* The buffer is now full, there's no point in going through
319 * the loop again.
320 */
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100321 if (!(b->flags & BF_STREAMER_FAST) && (cur_read == buffer_len(b))) {
Willy Tarreau8a7af602008-05-03 23:07:14 +0200322 b->xfer_small = 0;
323 b->xfer_large++;
324 if (b->xfer_large >= 3) {
325 /* we call this buffer a fast streamer if it manages
326 * to be filled in one call 3 consecutive times.
327 */
328 b->flags |= (BF_STREAMER | BF_STREAMER_FAST);
329 //fputc('+', stderr);
330 }
331 }
332 else if ((b->flags & (BF_STREAMER | BF_STREAMER_FAST)) &&
Willy Tarreaua07a34e2009-08-16 23:27:46 +0200333 (cur_read <= b->size / 2)) {
Willy Tarreau8a7af602008-05-03 23:07:14 +0200334 b->xfer_large = 0;
335 b->xfer_small++;
336 if (b->xfer_small >= 2) {
337 /* if the buffer has been at least half full twice,
338 * we receive faster than we send, so at least it
339 * is not a "fast streamer".
340 */
341 b->flags &= ~BF_STREAMER_FAST;
342 //fputc('-', stderr);
343 }
344 }
345 else {
346 b->xfer_small = 0;
347 b->xfer_large = 0;
348 }
Willy Tarreau9c0fe592009-01-18 16:25:31 +0100349
350 b->flags |= BF_FULL;
Willy Tarreaua456f2a2009-01-18 17:38:44 +0100351 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau9c0fe592009-01-18 16:25:31 +0100352 break;
Willy Tarreau6996e152007-04-30 14:37:43 +0200353 }
354
Willy Tarreauab3e1d32007-06-03 14:10:36 +0200355 /* if too many bytes were missing from last read, it means that
356 * it's pointless trying to read again because the system does
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100357 * not have them in buffers. BTW, if FD_POLL_HUP was present,
358 * it means that we have reached the end and that the connection
359 * is closed.
Willy Tarreauab3e1d32007-06-03 14:10:36 +0200360 */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100361 if (ret < max) {
Willy Tarreau8a7af602008-05-03 23:07:14 +0200362 if ((b->flags & (BF_STREAMER | BF_STREAMER_FAST)) &&
Willy Tarreaua07a34e2009-08-16 23:27:46 +0200363 (cur_read <= b->size / 2)) {
Willy Tarreau8a7af602008-05-03 23:07:14 +0200364 b->xfer_large = 0;
365 b->xfer_small++;
366 if (b->xfer_small >= 3) {
367 /* we have read less than half of the buffer in
368 * one pass, and this happened at least 3 times.
369 * This is definitely not a streamer.
370 */
371 b->flags &= ~(BF_STREAMER | BF_STREAMER_FAST);
372 //fputc('!', stderr);
373 }
374 }
Willy Tarreau2bea3a12008-08-28 09:47:43 +0200375 /* unfortunately, on level-triggered events, POLL_HUP
376 * is generally delivered AFTER the system buffer is
377 * empty, so this one might never match.
378 */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100379 if (fdtab[fd].ev & FD_POLL_HUP)
380 goto out_shutdown_r;
Willy Tarreau2bea3a12008-08-28 09:47:43 +0200381
382 /* if a streamer has read few data, it may be because we
383 * have exhausted system buffers. It's not worth trying
384 * again.
385 */
386 if (b->flags & BF_STREAMER)
387 break;
Willy Tarreauab3e1d32007-06-03 14:10:36 +0200388
Willy Tarreau6f4a82c2009-03-21 20:43:57 +0100389 /* generally if we read something smaller than 1 or 2 MSS,
390 * it means that either we have exhausted the system's
391 * buffers (streamer or question-response protocol) or
392 * that the connection will be closed. Streamers are
393 * easily detected so we return early. For other cases,
394 * it's still better to perform a last read to be sure,
395 * because it may save one complete poll/read/wakeup cycle
396 * in case of shutdown.
397 */
398 if (ret < MIN_RET_FOR_READ_LOOP && b->flags & BF_STREAMER)
399 break;
400
401 /* if we read a large block smaller than what we requested,
402 * it's almost certain we'll never get anything more.
403 */
404 if (ret >= global.tune.recv_enough)
405 break;
406 }
Willy Tarreau83749182007-04-15 20:56:27 +0200407
Willy Tarreau1b194fe2009-03-21 21:10:04 +0100408 if ((b->flags & BF_READ_DONTWAIT) || --read_poll <= 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200409 break;
Willy Tarreau83749182007-04-15 20:56:27 +0200410 }
411 else if (ret == 0) {
Willy Tarreau6996e152007-04-30 14:37:43 +0200412 /* connection closed */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100413 goto out_shutdown_r;
Willy Tarreau83749182007-04-15 20:56:27 +0200414 }
Willy Tarreau9f195292007-04-15 21:26:58 +0200415 else if (errno == EAGAIN) {
416 /* Ignore EAGAIN but inform the poller that there is
Willy Tarreauaf78d0f2009-01-08 10:09:08 +0100417 * nothing to read left if we did not read much, ie
418 * less than what we were still expecting to read.
419 * But we may have done some work justifying to notify
420 * the task.
Willy Tarreau9f195292007-04-15 21:26:58 +0200421 */
Willy Tarreauaf78d0f2009-01-08 10:09:08 +0100422 if (cur_read < MIN_RET_FOR_READ_LOOP)
Willy Tarreauafad0e02012-08-09 14:45:22 +0200423 conn_data_poll_recv(conn);
Willy Tarreau83749182007-04-15 20:56:27 +0200424 break;
425 }
426 else {
Willy Tarreau6996e152007-04-30 14:37:43 +0200427 goto out_error;
Willy Tarreau83749182007-04-15 20:56:27 +0200428 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200429 } /* while (1) */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200430
Willy Tarreauafad0e02012-08-09 14:45:22 +0200431 return;
Willy Tarreau6996e152007-04-30 14:37:43 +0200432
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100433 out_shutdown_r:
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200434 /* we received a shutdown */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100435 fdtab[fd].ev &= ~FD_POLL_HUP;
436 b->flags |= BF_READ_NULL;
Willy Tarreau520d95e2009-09-19 21:04:57 +0200437 if (b->flags & BF_AUTO_CLOSE)
Willy Tarreau418fd472009-09-06 21:37:23 +0200438 buffer_shutw_now(b);
Willy Tarreau3d8903f2012-08-06 17:00:18 +0200439 sock_raw_read0(si);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200440 return;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100441
Willy Tarreau6996e152007-04-30 14:37:43 +0200442 out_error:
Willy Tarreauafad0e02012-08-09 14:45:22 +0200443 /* Read error on the connection, report the error and stop I/O */
Willy Tarreau80184712012-07-06 14:54:49 +0200444 conn->flags |= CO_FL_ERROR;
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200445 conn_data_stop_both(conn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200446}
447
448
449/*
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100450 * This function is called to send buffer data to a stream socket.
Willy Tarreauafad0e02012-08-09 14:45:22 +0200451 * It returns -1 in case of unrecoverable error, otherwise zero.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200452 */
Willy Tarreaufae44992012-08-20 14:02:10 +0200453static int sock_raw_write_loop(struct connection *conn)
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100454{
Willy Tarreaufae44992012-08-20 14:02:10 +0200455 struct stream_interface *si = container_of(conn, struct stream_interface, conn);
456 struct buffer *b = si->ob;
Willy Tarreau83749182007-04-15 20:56:27 +0200457 int write_poll = MAX_WRITE_POLL_LOOPS;
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100458 int ret, max;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200459
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100460#if defined(CONFIG_HAP_LINUX_SPLICE)
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100461 while (b->pipe) {
Willy Tarreaufb7508a2012-05-21 16:47:54 +0200462 ret = splice(b->pipe->cons, NULL, si_fd(si), NULL, b->pipe->data,
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100463 SPLICE_F_MOVE|SPLICE_F_NONBLOCK);
464 if (ret <= 0) {
465 if (ret == 0 || errno == EAGAIN) {
Willy Tarreauafad0e02012-08-09 14:45:22 +0200466 conn_data_poll_send(&si->conn);
467 return 0;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100468 }
469 /* here we have another error */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200470 return -1;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100471 }
472
473 b->flags |= BF_WRITE_PARTIAL;
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100474 b->pipe->data -= ret;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100475
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100476 if (!b->pipe->data) {
477 put_pipe(b->pipe);
478 b->pipe = NULL;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100479 break;
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100480 }
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100481
482 if (--write_poll <= 0)
Willy Tarreauafad0e02012-08-09 14:45:22 +0200483 return 0;
Willy Tarreaueb9fd512011-12-11 22:11:47 +0100484
485 /* The only reason we did not empty the pipe is that the output
486 * buffer is full.
487 */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200488 conn_data_poll_send(&si->conn);
Willy Tarreaueb9fd512011-12-11 22:11:47 +0100489 return 0;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100490 }
491
492 /* At this point, the pipe is empty, but we may still have data pending
493 * in the normal buffer.
494 */
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100495#endif
Willy Tarreau2e046c62012-03-01 16:08:30 +0100496 if (!b->o) {
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200497 b->flags |= BF_OUT_EMPTY;
Willy Tarreauafad0e02012-08-09 14:45:22 +0200498 return 0;
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200499 }
Willy Tarreau83749182007-04-15 20:56:27 +0200500
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100501 /* when we're in this loop, we already know that there is no spliced
502 * data left, and that there are sendable buffered data.
503 */
Willy Tarreau6996e152007-04-30 14:37:43 +0200504 while (1) {
Willy Tarreau89fa7062012-03-02 16:13:16 +0100505 max = b->o;
Willy Tarreau83749182007-04-15 20:56:27 +0200506
Willy Tarreau89fa7062012-03-02 16:13:16 +0100507 /* outgoing data may wrap at the end */
508 if (b->data + max > b->p)
509 max = b->data + max - b->p;
Willy Tarreauf890dc92008-12-13 21:12:26 +0100510
Willy Tarreau6db06d32009-08-19 11:14:11 +0200511 /* check if we want to inform the kernel that we're interested in
512 * sending more data after this call. We want this if :
513 * - we're about to close after this last send and want to merge
514 * the ongoing FIN with the last segment.
515 * - we know we can't send everything at once and must get back
516 * here because of unaligned data
Willy Tarreaud38b53b2010-01-03 11:18:34 +0100517 * - there is still a finite amount of data to forward
Willy Tarreau6db06d32009-08-19 11:14:11 +0200518 * The test is arranged so that the most common case does only 2
519 * tests.
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200520 */
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200521
Willy Tarreauface8392010-01-03 11:37:54 +0100522 if (MSG_NOSIGNAL && MSG_MORE) {
Willy Tarreau6db06d32009-08-19 11:14:11 +0200523 unsigned int send_flag = MSG_DONTWAIT | MSG_NOSIGNAL;
524
Willy Tarreau96e31212011-05-30 18:10:30 +0200525 if ((!(b->flags & BF_NEVER_WAIT) &&
526 ((b->to_forward && b->to_forward != BUF_INFINITE_FORWARD) ||
527 (b->flags & BF_EXPECT_MORE))) ||
Willy Tarreau2e046c62012-03-01 16:08:30 +0100528 ((b->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK)) == BF_SHUTW_NOW && (max == b->o)) ||
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100529 (max != b->o)) {
Willy Tarreauface8392010-01-03 11:37:54 +0100530 send_flag |= MSG_MORE;
531 }
Willy Tarreau6db06d32009-08-19 11:14:11 +0200532
Willy Tarreau2be39392010-01-03 17:24:51 +0100533 /* this flag has precedence over the rest */
534 if (b->flags & BF_SEND_DONTWAIT)
535 send_flag &= ~MSG_MORE;
536
Willy Tarreaufb7508a2012-05-21 16:47:54 +0200537 ret = send(si_fd(si), bo_ptr(b), max, send_flag);
Willy Tarreaud6d06902009-08-19 11:22:33 +0200538 } else {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200539 int skerr;
540 socklen_t lskerr = sizeof(skerr);
541
Willy Tarreaufb7508a2012-05-21 16:47:54 +0200542 ret = getsockopt(si_fd(si), SOL_SOCKET, SO_ERROR, &skerr, &lskerr);
Willy Tarreauc6423482006-10-15 14:59:03 +0200543 if (ret == -1 || skerr)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200544 ret = -1;
545 else
Willy Tarreaufb7508a2012-05-21 16:47:54 +0200546 ret = send(si_fd(si), bo_ptr(b), max, MSG_DONTWAIT);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200547 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200548
549 if (ret > 0) {
Willy Tarreau505e34a2012-07-06 10:17:53 +0200550 if (si->conn.flags & CO_FL_WAIT_L4_CONN) {
551 si->conn.flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau8ae52cb2012-05-20 10:38:46 +0200552 si->exp = TICK_ETERNITY;
553 }
Willy Tarreaub38903c2008-11-23 21:33:29 +0100554
Willy Tarreau3da77c52008-08-29 09:58:42 +0200555 b->flags |= BF_WRITE_PARTIAL;
Willy Tarreaue393fe22008-08-16 22:18:07 +0200556
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100557 b->o -= ret;
558 if (likely(!buffer_len(b)))
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100559 /* optimize data alignment in the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +0100560 b->p = b->data;
Willy Tarreau83749182007-04-15 20:56:27 +0200561
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200562 if (likely(!bi_full(b)))
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100563 b->flags &= ~BF_FULL;
564
Willy Tarreau2e046c62012-03-01 16:08:30 +0100565 if (!b->o) {
Willy Tarreauf17810e2012-03-09 18:10:44 +0100566 /* Always clear both flags once everything has been sent, they're one-shot */
567 b->flags &= ~(BF_EXPECT_MORE | BF_SEND_DONTWAIT);
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200568 if (likely(!b->pipe))
569 b->flags |= BF_OUT_EMPTY;
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100570 break;
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200571 }
Willy Tarreau83749182007-04-15 20:56:27 +0200572
Willy Tarreauab3e1d32007-06-03 14:10:36 +0200573 /* if the system buffer is full, don't insist */
574 if (ret < max)
575 break;
576
Willy Tarreau6996e152007-04-30 14:37:43 +0200577 if (--write_poll <= 0)
578 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200579 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200580 else if (ret == 0 || errno == EAGAIN) {
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100581 /* nothing written, we need to poll for write first */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200582 conn_data_poll_send(&si->conn);
583 return 0;
Willy Tarreau83749182007-04-15 20:56:27 +0200584 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200585 else {
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100586 /* bad, we got an error */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200587 return -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200588 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200589 } /* while (1) */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200590 return 0;
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100591}
Willy Tarreau6996e152007-04-30 14:37:43 +0200592
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100593
594/*
Willy Tarreau3d8903f2012-08-06 17:00:18 +0200595 * This function propagates a null read received on a connection. It updates
596 * the stream interface. If the stream interface has SI_FL_NOHALF, we also
597 * forward the close to the write side.
598 */
599static void sock_raw_read0(struct stream_interface *si)
600{
601 si->ib->flags &= ~BF_SHUTR_NOW;
602 if (si->ib->flags & BF_SHUTR)
603 return;
604 si->ib->flags |= BF_SHUTR;
605 si->ib->rex = TICK_ETERNITY;
606 si->flags &= ~SI_FL_WAIT_ROOM;
607
608 if (si->state != SI_ST_EST && si->state != SI_ST_CON)
609 return;
610
611 if (si->ob->flags & BF_SHUTW)
612 goto do_close;
613
614 if (si->flags & SI_FL_NOHALF) {
615 /* we have to shut before closing, otherwise some short messages
616 * may never leave the system, especially when there are remaining
617 * unread data in the socket input buffer, or when nolinger is set.
618 * However, if SI_FL_NOLINGER is explicitly set, we know there is
619 * no risk so we close both sides immediately.
620 */
621 if (si->flags & SI_FL_NOLINGER) {
622 si->flags &= ~SI_FL_NOLINGER;
623 setsockopt(si_fd(si), SOL_SOCKET, SO_LINGER,
624 (struct linger *) &nolinger, sizeof(struct linger));
625 }
626 goto do_close;
627 }
628
629 /* otherwise that's just a normal read shutdown */
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200630 conn_data_stop_recv(&si->conn);
Willy Tarreau3d8903f2012-08-06 17:00:18 +0200631 return;
632
633 do_close:
634 conn_data_close(&si->conn);
635 fd_delete(si_fd(si));
636 si->state = SI_ST_DIS;
637 si->exp = TICK_ETERNITY;
638 if (si->release)
639 si->release(si);
640 return;
641}
642
Willy Tarreau5c979a92012-05-07 17:15:39 +0200643/* stream sock operations */
Willy Tarreaub277d6e2012-05-11 16:59:14 +0200644struct sock_ops sock_raw = {
Willy Tarreau100c4672012-08-20 12:06:26 +0200645 .update = stream_int_update_conn,
Willy Tarreau4a36b562012-08-06 19:31:45 +0200646 .shutr = NULL,
647 .shutw = NULL,
Willy Tarreau46a8d922012-08-20 12:38:36 +0200648 .chk_rcv = stream_int_chk_rcv_conn,
Willy Tarreaude5722c2012-08-20 15:01:10 +0200649 .chk_snd = stream_int_chk_snd_conn,
Willy Tarreaub277d6e2012-05-11 16:59:14 +0200650 .read = sock_raw_read,
Willy Tarreaueecf6ca2012-08-20 15:09:53 +0200651 .write = si_conn_send_cb,
Willy Tarreaufae44992012-08-20 14:02:10 +0200652 .snd_buf = sock_raw_write_loop,
Willy Tarreau24208272012-05-21 17:28:50 +0200653 .close = NULL,
Willy Tarreau5c979a92012-05-07 17:15:39 +0200654};
Willy Tarreaubaaee002006-06-26 02:48:02 +0200655
656/*
657 * Local variables:
658 * c-indent-level: 8
659 * c-basic-offset: 8
660 * End:
661 */