blob: 1bc40424af9e18b05d5530f98dd63d83f0a6ea7f [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 Tarreau75bf2c92012-08-20 17:01:35 +020039#include <proto/raw_sock.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
Willy Tarreauaf978c42012-08-20 20:21:01 +020046#if 0 && defined(CONFIG_HAP_LINUX_SPLICE)
Willy Tarreau43d8fb22011-08-22 17:12:02 +020047#include <common/splice.h>
Willy Tarreau5bd8c372009-01-19 00:32:22 +010048
49/* A pipe contains 16 segments max, and it's common to see segments of 1448 bytes
50 * because of timestamps. Use this as a hint for not looping on splice().
51 */
52#define SPLICE_FULL_HINT 16*1448
53
Willy Tarreaua9de3332009-11-28 07:47:10 +010054/* how many data we attempt to splice at once when the buffer is configured for
55 * infinite forwarding */
56#define MAX_SPLICE_AT_ONCE (1<<30)
57
Willy Tarreau5bd8c372009-01-19 00:32:22 +010058/* Returns :
59 * -1 if splice is not possible or not possible anymore and we must switch to
60 * user-land copy (eg: to_forward reached)
Willy Tarreauafad0e02012-08-09 14:45:22 +020061 * 0 otherwise, including errors and close.
Willy Tarreau5bd8c372009-01-19 00:32:22 +010062 * Sets :
63 * BF_READ_NULL
64 * BF_READ_PARTIAL
65 * BF_WRITE_PARTIAL (during copy)
Willy Tarreauba0b63d2009-09-20 08:09:44 +020066 * BF_OUT_EMPTY (during copy)
Willy Tarreau5bd8c372009-01-19 00:32:22 +010067 * SI_FL_ERR
68 * SI_FL_WAIT_ROOM
69 * (SI_FL_WAIT_RECV)
Willy Tarreau3eba98a2009-01-25 13:56:13 +010070 *
71 * This function automatically allocates a pipe from the pipe pool. It also
72 * carefully ensures to clear b->pipe whenever it leaves the pipe empty.
Willy Tarreau5bd8c372009-01-19 00:32:22 +010073 */
Willy Tarreau7421efb2012-07-02 15:11:27 +020074static int sock_raw_splice_in(struct channel *b, struct stream_interface *si)
Willy Tarreau5bd8c372009-01-19 00:32:22 +010075{
Willy Tarreau82a04562011-12-11 22:37:06 +010076 static int splice_detects_close;
Willy Tarreaufb7508a2012-05-21 16:47:54 +020077 int fd = si_fd(si);
Willy Tarreau31971e52009-09-20 12:07:52 +020078 int ret;
79 unsigned long max;
Willy Tarreauafad0e02012-08-09 14:45:22 +020080 int retval = 0;
Willy Tarreau5bd8c372009-01-19 00:32:22 +010081
82 if (!b->to_forward)
83 return -1;
84
85 if (!(b->flags & BF_KERN_SPLICING))
86 return -1;
87
Willy Tarreau572bf902012-07-02 17:01:20 +020088 if (buffer_not_empty(&b->buf)) {
Willy Tarreau5bd8c372009-01-19 00:32:22 +010089 /* We're embarrassed, there are already data pending in
90 * the buffer and we don't want to have them at two
91 * locations at a time. Let's indicate we need some
92 * place and ask the consumer to hurry.
93 */
94 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreauf9dabec2012-08-17 17:33:53 +020095 conn_data_stop_recv(&si->conn);
Willy Tarreau5bd8c372009-01-19 00:32:22 +010096 b->rex = TICK_ETERNITY;
Willy Tarreau73b013b2012-05-21 16:31:45 +020097 si_chk_snd(b->cons);
Willy Tarreauafad0e02012-08-09 14:45:22 +020098 return 0;
Willy Tarreau5bd8c372009-01-19 00:32:22 +010099 }
100
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100101 if (unlikely(b->pipe == NULL)) {
102 if (pipes_used >= global.maxpipes || !(b->pipe = get_pipe())) {
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100103 b->flags &= ~BF_KERN_SPLICING;
104 return -1;
105 }
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100106 }
107
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100108 /* At this point, b->pipe is valid */
109
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100110 while (1) {
Willy Tarreaua9de3332009-11-28 07:47:10 +0100111 if (b->to_forward == BUF_INFINITE_FORWARD)
112 max = MAX_SPLICE_AT_ONCE;
113 else
114 max = b->to_forward;
115
Willy Tarreau31971e52009-09-20 12:07:52 +0200116 if (!max) {
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100117 /* It looks like the buffer + the pipe already contain
118 * the maximum amount of data to be transferred. Try to
119 * send those data immediately on the other side if it
120 * is currently waiting.
121 */
122 retval = -1; /* end of forwarding */
123 break;
124 }
125
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100126 ret = splice(fd, NULL, b->pipe->prod, NULL, max,
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100127 SPLICE_F_MOVE|SPLICE_F_NONBLOCK);
128
129 if (ret <= 0) {
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100130 if (ret == 0) {
Willy Tarreau98b306b2009-01-25 11:11:32 +0100131 /* connection closed. This is only detected by
Willy Tarreau82a04562011-12-11 22:37:06 +0100132 * recent kernels (>= 2.6.27.13). If we notice
133 * it works, we store the info for later use.
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100134 */
Willy Tarreau82a04562011-12-11 22:37:06 +0100135 splice_detects_close = 1;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100136 b->flags |= BF_READ_NULL;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100137 break;
138 }
139
140 if (errno == EAGAIN) {
141 /* there are two reasons for EAGAIN :
142 * - nothing in the socket buffer (standard)
143 * - pipe is full
Willy Tarreau98b306b2009-01-25 11:11:32 +0100144 * - the connection is closed (kernel < 2.6.27.13)
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100145 * Since we don't know if pipe is full, we'll
146 * stop if the pipe is not empty. Anyway, we
147 * will almost always fill/empty the pipe.
148 */
149
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100150 if (b->pipe->data) {
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100151 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100152 break;
153 }
154
Willy Tarreau82a04562011-12-11 22:37:06 +0100155 /* We don't know if the connection was closed,
156 * but if we know splice detects close, then we
157 * know it for sure.
Willy Tarreau98b306b2009-01-25 11:11:32 +0100158 * But if we're called upon POLLIN with an empty
Willy Tarreau82a04562011-12-11 22:37:06 +0100159 * pipe and get EAGAIN, it is suspect enough to
Willy Tarreau98b306b2009-01-25 11:11:32 +0100160 * try to fall back to the normal recv scheme
161 * which will be able to deal with the situation.
162 */
Willy Tarreau82a04562011-12-11 22:37:06 +0100163 if (splice_detects_close)
Willy Tarreauafad0e02012-08-09 14:45:22 +0200164 conn_data_poll_recv(&si->conn); /* we know for sure that it's EAGAIN */
Willy Tarreau82a04562011-12-11 22:37:06 +0100165 else
166 retval = -1;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100167 break;
168 }
Willy Tarreaudc340a92009-06-28 23:10:19 +0200169
Willy Tarreaua9de3332009-11-28 07:47:10 +0100170 if (errno == ENOSYS || errno == EINVAL) {
Willy Tarreaudc340a92009-06-28 23:10:19 +0200171 /* splice not supported on this end, disable it */
172 b->flags &= ~BF_KERN_SPLICING;
173 si->flags &= ~SI_FL_CAP_SPLICE;
174 put_pipe(b->pipe);
175 b->pipe = NULL;
176 return -1;
177 }
178
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100179 /* here we have another error */
180 si->flags |= SI_FL_ERR;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100181 break;
182 } /* ret <= 0 */
183
Willy Tarreau31971e52009-09-20 12:07:52 +0200184 if (b->to_forward != BUF_INFINITE_FORWARD)
185 b->to_forward -= ret;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100186 b->total += ret;
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100187 b->pipe->data += ret;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100188 b->flags |= BF_READ_PARTIAL;
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200189 b->flags &= ~BF_OUT_EMPTY;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100190
Willy Tarreau6f4a82c2009-03-21 20:43:57 +0100191 if (b->pipe->data >= SPLICE_FULL_HINT ||
192 ret >= global.tune.recv_enough) {
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100193 /* We've read enough of it for this time. */
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100194 break;
195 }
196 } /* while */
197
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100198 if (unlikely(!b->pipe->data)) {
199 put_pipe(b->pipe);
200 b->pipe = NULL;
201 }
202
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100203 return retval;
204}
205
Willy Tarreau6b4aad42009-01-18 21:59:13 +0100206#endif /* CONFIG_HAP_LINUX_SPLICE */
207
208
Willy Tarreau2ba44652012-08-20 17:30:32 +0200209/* Receive up to <count> bytes from connection <conn>'s socket and store them
210 * into buffer <buf>. The caller must ensure that <count> is always smaller
211 * than the buffer's size. Only one call to recv() is performed, unless the
212 * buffer wraps, in which case a second call may be performed. The connection's
213 * flags are updated with whatever special event is detected (error, read0,
214 * empty). The caller is responsible for taking care of those events and
215 * avoiding the call if inappropriate. The function does not call the
216 * connection's polling update function, so the caller is responsible for this.
217 */
218static int raw_sock_to_buf(struct connection *conn, struct buffer *buf, int count)
219{
220 int ret, done = 0;
221 int try = count;
222
223 /* stop here if we reached the end of data */
224 if ((fdtab[conn->t.sock.fd].ev & (FD_POLL_IN|FD_POLL_HUP)) == FD_POLL_HUP)
225 goto read0;
226
227 /* compute the maximum block size we can read at once. */
228 if (buffer_empty(buf)) {
229 /* let's realign the buffer to optimize I/O */
230 buf->p = buf->data;
231 }
232 else if (buf->data + buf->o < buf->p &&
233 buf->p + buf->i < buf->data + buf->size) {
234 /* remaining space wraps at the end, with a moving limit */
235 if (try > buf->data + buf->size - (buf->p + buf->i))
236 try = buf->data + buf->size - (buf->p + buf->i);
237 }
238
239 /* read the largest possible block. For this, we perform only one call
240 * to recv() unless the buffer wraps and we exactly fill the first hunk,
241 * in which case we accept to do it once again. A new attempt is made on
242 * EINTR too.
243 */
244 while (try) {
245 ret = recv(conn->t.sock.fd, bi_end(buf), try, 0);
246
247 if (ret > 0) {
248 buf->i += ret;
249 done += ret;
250 if (ret < try) {
251 /* unfortunately, on level-triggered events, POLL_HUP
252 * is generally delivered AFTER the system buffer is
253 * empty, so this one might never match.
254 */
255 if (fdtab[conn->t.sock.fd].ev & FD_POLL_HUP)
256 goto read0;
257 break;
258 }
259 count -= ret;
260 try = count;
261 }
262 else if (ret == 0) {
263 goto read0;
264 }
265 else if (errno == EAGAIN) {
266 conn->flags |= CO_FL_WAIT_DATA;
267 break;
268 }
269 else if (errno != EINTR) {
270 conn->flags |= CO_FL_ERROR;
271 break;
272 }
273 }
274 return done;
275
276 read0:
277 conn_sock_read0(conn);
278 return done;
279}
280
281
Willy Tarreaubaaee002006-06-26 02:48:02 +0200282/*
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100283 * This function is called to send buffer data to a stream socket.
Willy Tarreauafad0e02012-08-09 14:45:22 +0200284 * It returns -1 in case of unrecoverable error, otherwise zero.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200285 */
Willy Tarreaufae44992012-08-20 14:02:10 +0200286static int sock_raw_write_loop(struct connection *conn)
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100287{
Willy Tarreaufae44992012-08-20 14:02:10 +0200288 struct stream_interface *si = container_of(conn, struct stream_interface, conn);
Willy Tarreau7421efb2012-07-02 15:11:27 +0200289 struct channel *b = si->ob;
Willy Tarreau83749182007-04-15 20:56:27 +0200290 int write_poll = MAX_WRITE_POLL_LOOPS;
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100291 int ret, max;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200292
Willy Tarreauaf978c42012-08-20 20:21:01 +0200293#if 0 && defined(CONFIG_HAP_LINUX_SPLICE)
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100294 while (b->pipe) {
Willy Tarreaufb7508a2012-05-21 16:47:54 +0200295 ret = splice(b->pipe->cons, NULL, si_fd(si), NULL, b->pipe->data,
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100296 SPLICE_F_MOVE|SPLICE_F_NONBLOCK);
297 if (ret <= 0) {
298 if (ret == 0 || errno == EAGAIN) {
Willy Tarreauafad0e02012-08-09 14:45:22 +0200299 conn_data_poll_send(&si->conn);
300 return 0;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100301 }
302 /* here we have another error */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200303 return -1;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100304 }
305
306 b->flags |= BF_WRITE_PARTIAL;
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100307 b->pipe->data -= ret;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100308
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100309 if (!b->pipe->data) {
310 put_pipe(b->pipe);
311 b->pipe = NULL;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100312 break;
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100313 }
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100314
315 if (--write_poll <= 0)
Willy Tarreauafad0e02012-08-09 14:45:22 +0200316 return 0;
Willy Tarreaueb9fd512011-12-11 22:11:47 +0100317
318 /* The only reason we did not empty the pipe is that the output
319 * buffer is full.
320 */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200321 conn_data_poll_send(&si->conn);
Willy Tarreaueb9fd512011-12-11 22:11:47 +0100322 return 0;
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100323 }
324
325 /* At this point, the pipe is empty, but we may still have data pending
326 * in the normal buffer.
327 */
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100328#endif
Willy Tarreau572bf902012-07-02 17:01:20 +0200329 if (!b->buf.o) {
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200330 b->flags |= BF_OUT_EMPTY;
Willy Tarreauafad0e02012-08-09 14:45:22 +0200331 return 0;
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200332 }
Willy Tarreau83749182007-04-15 20:56:27 +0200333
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100334 /* when we're in this loop, we already know that there is no spliced
335 * data left, and that there are sendable buffered data.
336 */
Willy Tarreau6996e152007-04-30 14:37:43 +0200337 while (1) {
Willy Tarreau572bf902012-07-02 17:01:20 +0200338 max = b->buf.o;
Willy Tarreau83749182007-04-15 20:56:27 +0200339
Willy Tarreau89fa7062012-03-02 16:13:16 +0100340 /* outgoing data may wrap at the end */
Willy Tarreau572bf902012-07-02 17:01:20 +0200341 if (b->buf.data + max > b->buf.p)
342 max = b->buf.data + max - b->buf.p;
Willy Tarreauf890dc92008-12-13 21:12:26 +0100343
Willy Tarreau6db06d32009-08-19 11:14:11 +0200344 /* check if we want to inform the kernel that we're interested in
345 * sending more data after this call. We want this if :
346 * - we're about to close after this last send and want to merge
347 * the ongoing FIN with the last segment.
348 * - we know we can't send everything at once and must get back
349 * here because of unaligned data
Willy Tarreaud38b53b2010-01-03 11:18:34 +0100350 * - there is still a finite amount of data to forward
Willy Tarreau6db06d32009-08-19 11:14:11 +0200351 * The test is arranged so that the most common case does only 2
352 * tests.
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200353 */
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200354
Willy Tarreauface8392010-01-03 11:37:54 +0100355 if (MSG_NOSIGNAL && MSG_MORE) {
Willy Tarreau6db06d32009-08-19 11:14:11 +0200356 unsigned int send_flag = MSG_DONTWAIT | MSG_NOSIGNAL;
357
Willy Tarreau96e31212011-05-30 18:10:30 +0200358 if ((!(b->flags & BF_NEVER_WAIT) &&
359 ((b->to_forward && b->to_forward != BUF_INFINITE_FORWARD) ||
360 (b->flags & BF_EXPECT_MORE))) ||
Willy Tarreau572bf902012-07-02 17:01:20 +0200361 ((b->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK)) == BF_SHUTW_NOW && (max == b->buf.o)) ||
362 (max != b->buf.o)) {
Willy Tarreauface8392010-01-03 11:37:54 +0100363 send_flag |= MSG_MORE;
364 }
Willy Tarreau6db06d32009-08-19 11:14:11 +0200365
Willy Tarreau2be39392010-01-03 17:24:51 +0100366 /* this flag has precedence over the rest */
367 if (b->flags & BF_SEND_DONTWAIT)
368 send_flag &= ~MSG_MORE;
369
Willy Tarreau572bf902012-07-02 17:01:20 +0200370 ret = send(si_fd(si), bo_ptr(&b->buf), max, send_flag);
Willy Tarreaud6d06902009-08-19 11:22:33 +0200371 } else {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200372 int skerr;
373 socklen_t lskerr = sizeof(skerr);
374
Willy Tarreaufb7508a2012-05-21 16:47:54 +0200375 ret = getsockopt(si_fd(si), SOL_SOCKET, SO_ERROR, &skerr, &lskerr);
Willy Tarreauc6423482006-10-15 14:59:03 +0200376 if (ret == -1 || skerr)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200377 ret = -1;
378 else
Willy Tarreau572bf902012-07-02 17:01:20 +0200379 ret = send(si_fd(si), bo_ptr(&b->buf), max, MSG_DONTWAIT);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200380 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200381
382 if (ret > 0) {
Willy Tarreau505e34a2012-07-06 10:17:53 +0200383 if (si->conn.flags & CO_FL_WAIT_L4_CONN) {
384 si->conn.flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau8ae52cb2012-05-20 10:38:46 +0200385 si->exp = TICK_ETERNITY;
386 }
Willy Tarreaub38903c2008-11-23 21:33:29 +0100387
Willy Tarreau3da77c52008-08-29 09:58:42 +0200388 b->flags |= BF_WRITE_PARTIAL;
Willy Tarreaue393fe22008-08-16 22:18:07 +0200389
Willy Tarreau572bf902012-07-02 17:01:20 +0200390 b->buf.o -= ret;
391 if (likely(!buffer_len(&b->buf)))
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100392 /* optimize data alignment in the buffer */
Willy Tarreau572bf902012-07-02 17:01:20 +0200393 b->buf.p = b->buf.data;
Willy Tarreau83749182007-04-15 20:56:27 +0200394
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200395 if (likely(!bi_full(b)))
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100396 b->flags &= ~BF_FULL;
397
Willy Tarreau572bf902012-07-02 17:01:20 +0200398 if (!b->buf.o) {
Willy Tarreauf17810e2012-03-09 18:10:44 +0100399 /* Always clear both flags once everything has been sent, they're one-shot */
400 b->flags &= ~(BF_EXPECT_MORE | BF_SEND_DONTWAIT);
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200401 if (likely(!b->pipe))
402 b->flags |= BF_OUT_EMPTY;
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100403 break;
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200404 }
Willy Tarreau83749182007-04-15 20:56:27 +0200405
Willy Tarreauab3e1d32007-06-03 14:10:36 +0200406 /* if the system buffer is full, don't insist */
407 if (ret < max)
408 break;
409
Willy Tarreau6996e152007-04-30 14:37:43 +0200410 if (--write_poll <= 0)
411 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200412 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200413 else if (ret == 0 || errno == EAGAIN) {
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100414 /* nothing written, we need to poll for write first */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200415 conn_data_poll_send(&si->conn);
416 return 0;
Willy Tarreau83749182007-04-15 20:56:27 +0200417 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200418 else {
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100419 /* bad, we got an error */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200420 return -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200421 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200422 } /* while (1) */
Willy Tarreauafad0e02012-08-09 14:45:22 +0200423 return 0;
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100424}
Willy Tarreau6996e152007-04-30 14:37:43 +0200425
Willy Tarreau0c2fc1f2009-01-18 15:30:37 +0100426
Willy Tarreau5c979a92012-05-07 17:15:39 +0200427/* stream sock operations */
Willy Tarreau75bf2c92012-08-20 17:01:35 +0200428struct sock_ops raw_sock = {
Willy Tarreau100c4672012-08-20 12:06:26 +0200429 .update = stream_int_update_conn,
Willy Tarreau4a36b562012-08-06 19:31:45 +0200430 .shutr = NULL,
431 .shutw = NULL,
Willy Tarreau46a8d922012-08-20 12:38:36 +0200432 .chk_rcv = stream_int_chk_rcv_conn,
Willy Tarreaude5722c2012-08-20 15:01:10 +0200433 .chk_snd = stream_int_chk_snd_conn,
Willy Tarreauce323de2012-08-20 21:41:06 +0200434 .read = si_conn_recv_cb,
Willy Tarreaueecf6ca2012-08-20 15:09:53 +0200435 .write = si_conn_send_cb,
Willy Tarreaufae44992012-08-20 14:02:10 +0200436 .snd_buf = sock_raw_write_loop,
Willy Tarreau1fe6bc32012-08-20 20:27:59 +0200437 .rcv_buf = raw_sock_to_buf,
Willy Tarreau24208272012-05-21 17:28:50 +0200438 .close = NULL,
Willy Tarreau5c979a92012-05-07 17:15:39 +0200439};
Willy Tarreaubaaee002006-06-26 02:48:02 +0200440
441/*
442 * Local variables:
443 * c-indent-level: 8
444 * c-basic-offset: 8
445 * End:
446 */