Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Functions operating on SOCK_STREAM and buffers. |
| 3 | * |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 4 | * Copyright 2000-2008 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5 | * |
| 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 Tarreau | 6b4aad4 | 2009-01-18 21:59:13 +0100 | [diff] [blame] | 13 | #define _GNU_SOURCE |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 14 | #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 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 23 | #include <common/compat.h> |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 24 | #include <common/config.h> |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 25 | #include <common/debug.h> |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 26 | #include <common/standard.h> |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 27 | #include <common/ticks.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 28 | #include <common/time.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 29 | |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 30 | #include <proto/buffers.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 31 | #include <proto/client.h> |
| 32 | #include <proto/fd.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 33 | #include <proto/stream_sock.h> |
| 34 | #include <proto/task.h> |
| 35 | |
Willy Tarreau | 5bd8c37 | 2009-01-19 00:32:22 +0100 | [diff] [blame] | 36 | #include <types/global.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 37 | |
Willy Tarreau | 6b4aad4 | 2009-01-18 21:59:13 +0100 | [diff] [blame] | 38 | /* On recent Linux kernels, the splice() syscall may be used for faster data copy. |
| 39 | * But it's not always defined on some OS versions, and it even happens that some |
| 40 | * definitions are wrong with some glibc due to an offset bug in syscall(). |
| 41 | */ |
| 42 | |
| 43 | #if defined(CONFIG_HAP_LINUX_SPLICE) |
| 44 | #include <unistd.h> |
| 45 | #include <sys/syscall.h> |
| 46 | |
| 47 | #ifndef SPLICE_F_MOVE |
| 48 | #define SPLICE_F_MOVE 0x1 |
| 49 | #endif |
| 50 | |
| 51 | #ifndef SPLICE_F_NONBLOCK |
| 52 | #define SPLICE_F_NONBLOCK 0x2 |
| 53 | #endif |
| 54 | |
| 55 | #ifndef SPLICE_F_MORE |
| 56 | #define SPLICE_F_MORE 0x4 |
| 57 | #endif |
| 58 | |
| 59 | #ifndef __NR_splice |
| 60 | #if defined(__powerpc__) || defined(__powerpc64__) |
| 61 | #define __NR_splice 283 |
| 62 | #elif defined(__sparc__) || defined(__sparc64__) |
| 63 | #define __NR_splice 232 |
| 64 | #elif defined(__x86_64__) |
| 65 | #define __NR_splice 275 |
| 66 | #elif defined(__alpha__) |
| 67 | #define __NR_splice 468 |
| 68 | #elif defined (__i386__) |
| 69 | #define __NR_splice 313 |
| 70 | #else |
| 71 | #warning unsupported architecture, guessing __NR_splice=313 like x86... |
| 72 | #define __NR_splice 313 |
| 73 | #endif /* $arch */ |
| 74 | |
| 75 | _syscall6(int, splice, int, fdin, loff_t *, off_in, int, fdout, loff_t *, off_out, size_t, len, unsigned long, flags) |
| 76 | |
| 77 | #endif /* __NR_splice */ |
Willy Tarreau | 5bd8c37 | 2009-01-19 00:32:22 +0100 | [diff] [blame] | 78 | |
| 79 | /* A pipe contains 16 segments max, and it's common to see segments of 1448 bytes |
| 80 | * because of timestamps. Use this as a hint for not looping on splice(). |
| 81 | */ |
| 82 | #define SPLICE_FULL_HINT 16*1448 |
| 83 | |
| 84 | /* Returns : |
| 85 | * -1 if splice is not possible or not possible anymore and we must switch to |
| 86 | * user-land copy (eg: to_forward reached) |
| 87 | * 0 when we know that polling is required to get more data (EAGAIN) |
| 88 | * 1 for all other cases (we can safely try again, or if an activity has been |
| 89 | * detected (DATA/NULL/ERR)) |
| 90 | * Sets : |
| 91 | * BF_READ_NULL |
| 92 | * BF_READ_PARTIAL |
| 93 | * BF_WRITE_PARTIAL (during copy) |
| 94 | * BF_EMPTY (during copy) |
| 95 | * SI_FL_ERR |
| 96 | * SI_FL_WAIT_ROOM |
| 97 | * (SI_FL_WAIT_RECV) |
| 98 | */ |
| 99 | static int stream_sock_splice_in(struct buffer *b, struct stream_interface *si) |
| 100 | { |
| 101 | int fd = si->fd; |
| 102 | int ret, max, total = 0; |
| 103 | int retval = 1; |
| 104 | |
| 105 | if (!b->to_forward) |
| 106 | return -1; |
| 107 | |
| 108 | if (!(b->flags & BF_KERN_SPLICING)) |
| 109 | return -1; |
| 110 | |
| 111 | if (b->l) { |
| 112 | /* We're embarrassed, there are already data pending in |
| 113 | * the buffer and we don't want to have them at two |
| 114 | * locations at a time. Let's indicate we need some |
| 115 | * place and ask the consumer to hurry. |
| 116 | */ |
| 117 | si->flags |= SI_FL_WAIT_ROOM; |
| 118 | EV_FD_CLR(fd, DIR_RD); |
| 119 | b->rex = TICK_ETERNITY; |
| 120 | b->cons->chk_snd(b->cons); |
| 121 | return 1; |
| 122 | } |
| 123 | |
| 124 | if (unlikely(b->splice.prod == -1)) { |
| 125 | int pipefd[2]; |
| 126 | if (usedpipes >= global.maxpipes || pipe(pipefd) < 0) { |
| 127 | b->flags &= ~BF_KERN_SPLICING; |
| 128 | return -1; |
| 129 | } |
| 130 | usedpipes++; |
| 131 | b->splice.prod = pipefd[1]; |
| 132 | b->splice.cons = pipefd[0]; |
| 133 | } |
| 134 | |
| 135 | while (1) { |
| 136 | max = b->to_forward; |
| 137 | if (max <= 0) { |
| 138 | /* It looks like the buffer + the pipe already contain |
| 139 | * the maximum amount of data to be transferred. Try to |
| 140 | * send those data immediately on the other side if it |
| 141 | * is currently waiting. |
| 142 | */ |
| 143 | retval = -1; /* end of forwarding */ |
| 144 | break; |
| 145 | } |
| 146 | |
| 147 | ret = splice(fd, NULL, b->splice.prod, NULL, max, |
| 148 | SPLICE_F_MOVE|SPLICE_F_NONBLOCK); |
| 149 | |
| 150 | if (ret <= 0) { |
Willy Tarreau | 5bd8c37 | 2009-01-19 00:32:22 +0100 | [diff] [blame] | 151 | if (ret == 0) { |
Willy Tarreau | 98b306b | 2009-01-25 11:11:32 +0100 | [diff] [blame^] | 152 | /* connection closed. This is only detected by |
| 153 | * recent kernels (>= 2.6.27.13). |
Willy Tarreau | 5bd8c37 | 2009-01-19 00:32:22 +0100 | [diff] [blame] | 154 | */ |
Willy Tarreau | 5bd8c37 | 2009-01-19 00:32:22 +0100 | [diff] [blame] | 155 | b->flags |= BF_READ_NULL; |
Willy Tarreau | 5bd8c37 | 2009-01-19 00:32:22 +0100 | [diff] [blame] | 156 | retval = 1; /* no need for further polling */ |
| 157 | break; |
| 158 | } |
| 159 | |
| 160 | if (errno == EAGAIN) { |
| 161 | /* there are two reasons for EAGAIN : |
| 162 | * - nothing in the socket buffer (standard) |
| 163 | * - pipe is full |
Willy Tarreau | 98b306b | 2009-01-25 11:11:32 +0100 | [diff] [blame^] | 164 | * - the connection is closed (kernel < 2.6.27.13) |
Willy Tarreau | 5bd8c37 | 2009-01-19 00:32:22 +0100 | [diff] [blame] | 165 | * Since we don't know if pipe is full, we'll |
| 166 | * stop if the pipe is not empty. Anyway, we |
| 167 | * will almost always fill/empty the pipe. |
| 168 | */ |
| 169 | |
| 170 | if (b->splice_len > 0) { |
| 171 | si->flags |= SI_FL_WAIT_ROOM; |
| 172 | retval = 1; |
| 173 | break; |
| 174 | } |
| 175 | |
Willy Tarreau | 98b306b | 2009-01-25 11:11:32 +0100 | [diff] [blame^] | 176 | /* We don't know if the connection was closed. |
| 177 | * But if we're called upon POLLIN with an empty |
| 178 | * pipe and get EAGAIN, it is suspect enought to |
| 179 | * try to fall back to the normal recv scheme |
| 180 | * which will be able to deal with the situation. |
| 181 | */ |
| 182 | retval = -1; |
Willy Tarreau | 5bd8c37 | 2009-01-19 00:32:22 +0100 | [diff] [blame] | 183 | break; |
| 184 | } |
Willy Tarreau | 5bd8c37 | 2009-01-19 00:32:22 +0100 | [diff] [blame] | 185 | /* here we have another error */ |
| 186 | si->flags |= SI_FL_ERR; |
| 187 | retval = 1; |
| 188 | break; |
| 189 | } /* ret <= 0 */ |
| 190 | |
| 191 | b->to_forward -= ret; |
| 192 | total += ret; |
| 193 | b->total += ret; |
| 194 | b->splice_len += ret; |
| 195 | b->flags |= BF_READ_PARTIAL; |
| 196 | b->flags &= ~BF_EMPTY; /* to prevent shutdowns */ |
| 197 | |
| 198 | if (b->splice_len >= SPLICE_FULL_HINT) { |
| 199 | /* We've read enough of it for this time. */ |
| 200 | retval = 1; |
| 201 | break; |
| 202 | } |
| 203 | } /* while */ |
| 204 | |
| 205 | return retval; |
| 206 | } |
| 207 | |
Willy Tarreau | 6b4aad4 | 2009-01-18 21:59:13 +0100 | [diff] [blame] | 208 | #endif /* CONFIG_HAP_LINUX_SPLICE */ |
| 209 | |
| 210 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 211 | /* |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 212 | * this function is called on a read event from a stream socket. |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 213 | * It returns 0 if we have a high confidence that we will not be |
| 214 | * able to read more data without polling first. Returns non-zero |
| 215 | * otherwise. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 216 | */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 217 | int stream_sock_read(int fd) { |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 218 | struct stream_interface *si = fdtab[fd].owner; |
Willy Tarreau | 48adac5 | 2008-08-30 04:58:38 +0200 | [diff] [blame] | 219 | struct buffer *b = si->ib; |
Willy Tarreau | 8a7af60 | 2008-05-03 23:07:14 +0200 | [diff] [blame] | 220 | int ret, max, retval, cur_read; |
Willy Tarreau | b8949f1 | 2007-03-23 22:39:59 +0100 | [diff] [blame] | 221 | int read_poll = MAX_READ_POLL_LOOPS; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 222 | |
| 223 | #ifdef DEBUG_FULL |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 224 | fprintf(stderr,"stream_sock_read : fd=%d, ev=0x%02x, owner=%p\n", fd, fdtab[fd].ev, fdtab[fd].owner); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 225 | #endif |
| 226 | |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 227 | retval = 1; |
| 228 | |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 229 | /* stop immediately on errors */ |
| 230 | if (fdtab[fd].state == FD_STERROR || (fdtab[fd].ev & FD_POLL_ERR)) |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 231 | goto out_error; |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 232 | |
| 233 | /* stop here if we reached the end of data */ |
| 234 | if ((fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP)) == FD_POLL_HUP) |
| 235 | goto out_shutdown_r; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 236 | |
Willy Tarreau | 5bd8c37 | 2009-01-19 00:32:22 +0100 | [diff] [blame] | 237 | #if defined(CONFIG_HAP_LINUX_SPLICE) |
| 238 | if (b->to_forward && b->flags & BF_KERN_SPLICING) { |
Willy Tarreau | 98b306b | 2009-01-25 11:11:32 +0100 | [diff] [blame^] | 239 | |
| 240 | /* Under Linux, if FD_POLL_HUP is set, we have reached the end. |
| 241 | * Since older splice() implementations were buggy and returned |
| 242 | * EAGAIN on end of read, let's bypass the call to splice() now. |
| 243 | */ |
| 244 | if (fdtab[fd].ev & FD_POLL_HUP) |
| 245 | goto out_shutdown_r; |
| 246 | |
Willy Tarreau | 5bd8c37 | 2009-01-19 00:32:22 +0100 | [diff] [blame] | 247 | retval = stream_sock_splice_in(b, si); |
| 248 | |
| 249 | if (retval >= 0) { |
| 250 | if (si->flags & SI_FL_ERR) |
| 251 | goto out_error; |
| 252 | if (b->flags & BF_READ_NULL) |
| 253 | goto out_shutdown_r; |
| 254 | goto out_wakeup; |
| 255 | } |
| 256 | /* splice not possible (anymore), let's go on on standard copy */ |
| 257 | } |
| 258 | #endif |
Willy Tarreau | 8a7af60 | 2008-05-03 23:07:14 +0200 | [diff] [blame] | 259 | cur_read = 0; |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 260 | while (1) { |
| 261 | /* |
| 262 | * 1. compute the maximum block size we can read at once. |
| 263 | */ |
Willy Tarreau | 03d60bb | 2009-01-09 11:13:00 +0100 | [diff] [blame] | 264 | if (b->l == 0) { |
| 265 | /* let's realign the buffer to optimize I/O */ |
| 266 | b->r = b->w = b->lr = b->data; |
| 267 | max = b->max_len; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 268 | } |
| 269 | else if (b->r > b->w) { |
Willy Tarreau | 03d60bb | 2009-01-09 11:13:00 +0100 | [diff] [blame] | 270 | max = b->data + b->max_len - b->r; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 271 | } |
| 272 | else { |
| 273 | max = b->w - b->r; |
Willy Tarreau | 03d60bb | 2009-01-09 11:13:00 +0100 | [diff] [blame] | 274 | if (max > b->max_len) |
| 275 | max = b->max_len; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 276 | } |
Willy Tarreau | 74ab2ac | 2008-11-23 17:23:07 +0100 | [diff] [blame] | 277 | |
Willy Tarreau | 9c0fe59 | 2009-01-18 16:25:31 +0100 | [diff] [blame] | 278 | if (max == 0) { |
| 279 | b->flags |= BF_FULL; |
Willy Tarreau | a456f2a | 2009-01-18 17:38:44 +0100 | [diff] [blame] | 280 | si->flags |= SI_FL_WAIT_ROOM; |
Willy Tarreau | 9c0fe59 | 2009-01-18 16:25:31 +0100 | [diff] [blame] | 281 | break; |
| 282 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 283 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 284 | /* |
| 285 | * 2. read the largest possible block |
| 286 | */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 287 | #ifndef MSG_NOSIGNAL |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 288 | { |
| 289 | int skerr; |
| 290 | socklen_t lskerr = sizeof(skerr); |
| 291 | |
| 292 | ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr); |
| 293 | if (ret == -1 || skerr) |
| 294 | ret = -1; |
| 295 | else |
| 296 | ret = recv(fd, b->r, max, 0); |
| 297 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 298 | #else |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 299 | ret = recv(fd, b->r, max, MSG_NOSIGNAL); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 300 | #endif |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 301 | if (ret > 0) { |
| 302 | b->r += ret; |
| 303 | b->l += ret; |
Willy Tarreau | 8a7af60 | 2008-05-03 23:07:14 +0200 | [diff] [blame] | 304 | cur_read += ret; |
Willy Tarreau | b38903c | 2008-11-23 21:33:29 +0100 | [diff] [blame] | 305 | |
Willy Tarreau | 0abebcc | 2009-01-08 00:09:41 +0100 | [diff] [blame] | 306 | /* if we're allowed to directly forward data, we must update send_max */ |
| 307 | if (b->to_forward > 0) { |
| 308 | int fwd = MIN(b->to_forward, ret); |
| 309 | b->send_max += fwd; |
| 310 | b->to_forward -= fwd; |
| 311 | } |
Willy Tarreau | f890dc9 | 2008-12-13 21:12:26 +0100 | [diff] [blame] | 312 | |
Willy Tarreau | b38903c | 2008-11-23 21:33:29 +0100 | [diff] [blame] | 313 | if (fdtab[fd].state == FD_STCONN) |
| 314 | fdtab[fd].state = FD_STREADY; |
| 315 | |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 316 | b->flags |= BF_READ_PARTIAL; |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 317 | b->flags &= ~BF_EMPTY; |
Willy Tarreau | 74ab2ac | 2008-11-23 17:23:07 +0100 | [diff] [blame] | 318 | |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 319 | if (b->r == b->data + BUFSIZE) { |
| 320 | b->r = b->data; /* wrap around the buffer */ |
| 321 | } |
Willy Tarreau | 9641e8f | 2007-03-23 23:02:09 +0100 | [diff] [blame] | 322 | |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 323 | b->total += ret; |
Willy Tarreau | 9641e8f | 2007-03-23 23:02:09 +0100 | [diff] [blame] | 324 | |
Willy Tarreau | 03d60bb | 2009-01-09 11:13:00 +0100 | [diff] [blame] | 325 | if (b->l >= b->max_len) { |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 326 | /* The buffer is now full, there's no point in going through |
| 327 | * the loop again. |
| 328 | */ |
Willy Tarreau | 8a7af60 | 2008-05-03 23:07:14 +0200 | [diff] [blame] | 329 | if (!(b->flags & BF_STREAMER_FAST) && (cur_read == b->l)) { |
| 330 | b->xfer_small = 0; |
| 331 | b->xfer_large++; |
| 332 | if (b->xfer_large >= 3) { |
| 333 | /* we call this buffer a fast streamer if it manages |
| 334 | * to be filled in one call 3 consecutive times. |
| 335 | */ |
| 336 | b->flags |= (BF_STREAMER | BF_STREAMER_FAST); |
| 337 | //fputc('+', stderr); |
| 338 | } |
| 339 | } |
| 340 | else if ((b->flags & (BF_STREAMER | BF_STREAMER_FAST)) && |
| 341 | (cur_read <= BUFSIZE / 2)) { |
| 342 | b->xfer_large = 0; |
| 343 | b->xfer_small++; |
| 344 | if (b->xfer_small >= 2) { |
| 345 | /* if the buffer has been at least half full twice, |
| 346 | * we receive faster than we send, so at least it |
| 347 | * is not a "fast streamer". |
| 348 | */ |
| 349 | b->flags &= ~BF_STREAMER_FAST; |
| 350 | //fputc('-', stderr); |
| 351 | } |
| 352 | } |
| 353 | else { |
| 354 | b->xfer_small = 0; |
| 355 | b->xfer_large = 0; |
| 356 | } |
Willy Tarreau | 9c0fe59 | 2009-01-18 16:25:31 +0100 | [diff] [blame] | 357 | |
| 358 | b->flags |= BF_FULL; |
Willy Tarreau | a456f2a | 2009-01-18 17:38:44 +0100 | [diff] [blame] | 359 | si->flags |= SI_FL_WAIT_ROOM; |
Willy Tarreau | 9c0fe59 | 2009-01-18 16:25:31 +0100 | [diff] [blame] | 360 | break; |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 361 | } |
| 362 | |
Willy Tarreau | ab3e1d3 | 2007-06-03 14:10:36 +0200 | [diff] [blame] | 363 | /* if too many bytes were missing from last read, it means that |
| 364 | * it's pointless trying to read again because the system does |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 365 | * not have them in buffers. BTW, if FD_POLL_HUP was present, |
| 366 | * it means that we have reached the end and that the connection |
| 367 | * is closed. |
Willy Tarreau | ab3e1d3 | 2007-06-03 14:10:36 +0200 | [diff] [blame] | 368 | */ |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 369 | if (ret < max) { |
Willy Tarreau | 8a7af60 | 2008-05-03 23:07:14 +0200 | [diff] [blame] | 370 | if ((b->flags & (BF_STREAMER | BF_STREAMER_FAST)) && |
| 371 | (cur_read <= BUFSIZE / 2)) { |
| 372 | b->xfer_large = 0; |
| 373 | b->xfer_small++; |
| 374 | if (b->xfer_small >= 3) { |
| 375 | /* we have read less than half of the buffer in |
| 376 | * one pass, and this happened at least 3 times. |
| 377 | * This is definitely not a streamer. |
| 378 | */ |
| 379 | b->flags &= ~(BF_STREAMER | BF_STREAMER_FAST); |
| 380 | //fputc('!', stderr); |
| 381 | } |
| 382 | } |
Willy Tarreau | 2bea3a1 | 2008-08-28 09:47:43 +0200 | [diff] [blame] | 383 | /* unfortunately, on level-triggered events, POLL_HUP |
| 384 | * is generally delivered AFTER the system buffer is |
| 385 | * empty, so this one might never match. |
| 386 | */ |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 387 | if (fdtab[fd].ev & FD_POLL_HUP) |
| 388 | goto out_shutdown_r; |
Willy Tarreau | 2bea3a1 | 2008-08-28 09:47:43 +0200 | [diff] [blame] | 389 | |
| 390 | /* if a streamer has read few data, it may be because we |
| 391 | * have exhausted system buffers. It's not worth trying |
| 392 | * again. |
| 393 | */ |
| 394 | if (b->flags & BF_STREAMER) |
| 395 | break; |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 396 | } |
Willy Tarreau | ab3e1d3 | 2007-06-03 14:10:36 +0200 | [diff] [blame] | 397 | |
| 398 | /* generally if we read something smaller than 1 or 2 MSS, |
Willy Tarreau | 2bea3a1 | 2008-08-28 09:47:43 +0200 | [diff] [blame] | 399 | * it means that either we have exhausted the system's |
| 400 | * buffers (streamer or question-response protocol) or that |
| 401 | * the connection will be closed. Streamers are easily |
| 402 | * detected so we return early. For other cases, it's still |
| 403 | * better to perform a last read to be sure, because it may |
| 404 | * save one complete poll/read/wakeup cycle in case of shutdown. |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 405 | */ |
Willy Tarreau | 2bea3a1 | 2008-08-28 09:47:43 +0200 | [diff] [blame] | 406 | if (ret < MIN_RET_FOR_READ_LOOP && b->flags & BF_STREAMER) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 407 | break; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 408 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 409 | if (--read_poll <= 0) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 410 | break; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 411 | } |
| 412 | else if (ret == 0) { |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 413 | /* connection closed */ |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 414 | goto out_shutdown_r; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 415 | } |
Willy Tarreau | 9f19529 | 2007-04-15 21:26:58 +0200 | [diff] [blame] | 416 | else if (errno == EAGAIN) { |
| 417 | /* Ignore EAGAIN but inform the poller that there is |
Willy Tarreau | af78d0f | 2009-01-08 10:09:08 +0100 | [diff] [blame] | 418 | * nothing to read left if we did not read much, ie |
| 419 | * less than what we were still expecting to read. |
| 420 | * But we may have done some work justifying to notify |
| 421 | * the task. |
Willy Tarreau | 9f19529 | 2007-04-15 21:26:58 +0200 | [diff] [blame] | 422 | */ |
Willy Tarreau | af78d0f | 2009-01-08 10:09:08 +0100 | [diff] [blame] | 423 | if (cur_read < MIN_RET_FOR_READ_LOOP) |
| 424 | retval = 0; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 425 | break; |
| 426 | } |
| 427 | else { |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 428 | goto out_error; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 429 | } |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 430 | } /* while (1) */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 431 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 432 | out_wakeup: |
Willy Tarreau | 9c0fe59 | 2009-01-18 16:25:31 +0100 | [diff] [blame] | 433 | /* We might have some data the consumer is waiting for */ |
Willy Tarreau | a456f2a | 2009-01-18 17:38:44 +0100 | [diff] [blame] | 434 | if ((b->send_max || b->splice_len) && (b->cons->flags & SI_FL_WAIT_DATA)) { |
| 435 | int last_len = b->splice_len; |
| 436 | |
Willy Tarreau | 3ffeba1 | 2008-12-14 14:42:35 +0100 | [diff] [blame] | 437 | b->cons->chk_snd(b->cons); |
| 438 | |
Willy Tarreau | a456f2a | 2009-01-18 17:38:44 +0100 | [diff] [blame] | 439 | /* check if the consumer has freed some space */ |
| 440 | if (!(b->flags & BF_FULL) && (!last_len || b->splice_len < last_len)) |
| 441 | si->flags &= ~SI_FL_WAIT_ROOM; |
| 442 | } |
| 443 | |
| 444 | if (si->flags & SI_FL_WAIT_ROOM) { |
Willy Tarreau | 9c0fe59 | 2009-01-18 16:25:31 +0100 | [diff] [blame] | 445 | EV_FD_CLR(fd, DIR_RD); |
| 446 | b->rex = TICK_ETERNITY; |
| 447 | } |
Willy Tarreau | a456f2a | 2009-01-18 17:38:44 +0100 | [diff] [blame] | 448 | else if ((b->flags & (BF_READ_PARTIAL|BF_FULL|BF_READ_NOEXP)) == BF_READ_PARTIAL) |
| 449 | b->rex = tick_add_ifset(now_ms, b->rto); |
Willy Tarreau | 9c0fe59 | 2009-01-18 16:25:31 +0100 | [diff] [blame] | 450 | |
Willy Tarreau | 6b66f3e | 2008-12-14 17:31:54 +0100 | [diff] [blame] | 451 | /* we have to wake up if there is a special event or if we don't have |
| 452 | * any more data to forward. |
| 453 | */ |
Willy Tarreau | a456f2a | 2009-01-18 17:38:44 +0100 | [diff] [blame] | 454 | if ((b->flags & (BF_READ_NULL|BF_READ_ERROR|BF_SHUTR)) || |
| 455 | !b->to_forward || |
| 456 | si->state != SI_ST_EST || |
| 457 | b->cons->state != SI_ST_EST || |
| 458 | (si->flags & SI_FL_ERR)) |
Willy Tarreau | 6b66f3e | 2008-12-14 17:31:54 +0100 | [diff] [blame] | 459 | task_wakeup(si->owner, TASK_WOKEN_IO); |
Willy Tarreau | a456f2a | 2009-01-18 17:38:44 +0100 | [diff] [blame] | 460 | |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 461 | fdtab[fd].ev &= ~FD_POLL_IN; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 462 | return retval; |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 463 | |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 464 | out_shutdown_r: |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 465 | /* we received a shutdown */ |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 466 | fdtab[fd].ev &= ~FD_POLL_HUP; |
| 467 | b->flags |= BF_READ_NULL; |
Willy Tarreau | 99126c3 | 2008-11-27 10:30:51 +0100 | [diff] [blame] | 468 | stream_sock_shutr(si); |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 469 | goto out_wakeup; |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 470 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 471 | out_error: |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 472 | /* Read error on the file descriptor. We mark the FD as STERROR so |
| 473 | * that we don't use it anymore. The error is reported to the stream |
| 474 | * interface which will take proper action. We must not perturbate the |
| 475 | * buffer because the stream interface wants to ensure transparent |
| 476 | * connection retries. |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 477 | */ |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 478 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 479 | fdtab[fd].state = FD_STERROR; |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 480 | fdtab[fd].ev &= ~FD_POLL_STICKY; |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 481 | si->flags |= SI_FL_ERR; |
Willy Tarreau | 9c0fe59 | 2009-01-18 16:25:31 +0100 | [diff] [blame] | 482 | retval = 1; |
| 483 | goto out_wakeup; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | |
| 487 | /* |
Willy Tarreau | 0c2fc1f | 2009-01-18 15:30:37 +0100 | [diff] [blame] | 488 | * This function is called to send buffer data to a stream socket. |
| 489 | * It returns -1 in case of unrecoverable error, 0 if the caller needs to poll |
| 490 | * before calling it again, otherwise 1. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 491 | */ |
Willy Tarreau | a456f2a | 2009-01-18 17:38:44 +0100 | [diff] [blame] | 492 | static int stream_sock_write_loop(struct stream_interface *si, struct buffer *b) |
Willy Tarreau | 0c2fc1f | 2009-01-18 15:30:37 +0100 | [diff] [blame] | 493 | { |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 494 | int write_poll = MAX_WRITE_POLL_LOOPS; |
Willy Tarreau | 0c2fc1f | 2009-01-18 15:30:37 +0100 | [diff] [blame] | 495 | int retval = 1; |
| 496 | int ret, max; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 497 | |
Willy Tarreau | 5bd8c37 | 2009-01-19 00:32:22 +0100 | [diff] [blame] | 498 | #if defined(CONFIG_HAP_LINUX_SPLICE) |
| 499 | while (b->splice_len) { |
| 500 | ret = splice(b->splice.cons, NULL, si->fd, NULL, b->splice_len, |
| 501 | SPLICE_F_MOVE|SPLICE_F_NONBLOCK); |
| 502 | if (ret <= 0) { |
| 503 | if (ret == 0 || errno == EAGAIN) { |
| 504 | retval = 0; |
| 505 | return retval; |
| 506 | } |
| 507 | /* here we have another error */ |
| 508 | retval = -1; |
| 509 | return retval; |
| 510 | } |
| 511 | |
| 512 | b->flags |= BF_WRITE_PARTIAL; |
| 513 | b->splice_len -= ret; |
| 514 | |
| 515 | if (!b->splice_len) |
| 516 | break; |
| 517 | |
| 518 | if (--write_poll <= 0) |
| 519 | return retval; |
| 520 | } |
| 521 | |
| 522 | /* At this point, the pipe is empty, but we may still have data pending |
| 523 | * in the normal buffer. |
| 524 | */ |
| 525 | if (!b->l) { |
| 526 | b->flags |= BF_EMPTY; |
| 527 | return retval; |
| 528 | } |
| 529 | #endif |
Willy Tarreau | d2def0f | 2009-01-18 17:37:33 +0100 | [diff] [blame] | 530 | if (!b->send_max) |
Willy Tarreau | 0c2fc1f | 2009-01-18 15:30:37 +0100 | [diff] [blame] | 531 | return retval; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 532 | |
Willy Tarreau | 0c2fc1f | 2009-01-18 15:30:37 +0100 | [diff] [blame] | 533 | /* when we're in this loop, we already know that there is no spliced |
| 534 | * data left, and that there are sendable buffered data. |
| 535 | */ |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 536 | while (1) { |
Willy Tarreau | 0c2fc1f | 2009-01-18 15:30:37 +0100 | [diff] [blame] | 537 | if (b->r > b->w) |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 538 | max = b->r - b->w; |
Willy Tarreau | 0c2fc1f | 2009-01-18 15:30:37 +0100 | [diff] [blame] | 539 | else |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 540 | max = b->data + BUFSIZE - b->w; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 541 | |
Willy Tarreau | f890dc9 | 2008-12-13 21:12:26 +0100 | [diff] [blame] | 542 | /* limit the amount of outgoing data if required */ |
| 543 | if (max > b->send_max) |
| 544 | max = b->send_max; |
| 545 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 546 | #ifndef MSG_NOSIGNAL |
| 547 | { |
| 548 | int skerr; |
| 549 | socklen_t lskerr = sizeof(skerr); |
| 550 | |
Willy Tarreau | c642348 | 2006-10-15 14:59:03 +0200 | [diff] [blame] | 551 | ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr); |
| 552 | if (ret == -1 || skerr) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 553 | ret = -1; |
| 554 | else |
| 555 | ret = send(fd, b->w, max, MSG_DONTWAIT); |
| 556 | } |
| 557 | #else |
Willy Tarreau | 0c2fc1f | 2009-01-18 15:30:37 +0100 | [diff] [blame] | 558 | ret = send(si->fd, b->w, max, MSG_DONTWAIT | MSG_NOSIGNAL); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 559 | #endif |
| 560 | |
| 561 | if (ret > 0) { |
Willy Tarreau | 0c2fc1f | 2009-01-18 15:30:37 +0100 | [diff] [blame] | 562 | if (fdtab[si->fd].state == FD_STCONN) |
| 563 | fdtab[si->fd].state = FD_STREADY; |
Willy Tarreau | b38903c | 2008-11-23 21:33:29 +0100 | [diff] [blame] | 564 | |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 565 | b->flags |= BF_WRITE_PARTIAL; |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 566 | |
Willy Tarreau | 0c2fc1f | 2009-01-18 15:30:37 +0100 | [diff] [blame] | 567 | b->w += ret; |
| 568 | if (b->w == b->data + BUFSIZE) |
| 569 | b->w = b->data; /* wrap around the buffer */ |
| 570 | |
| 571 | b->l -= ret; |
| 572 | if (likely(b->l < b->max_len)) |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 573 | b->flags &= ~BF_FULL; |
Willy Tarreau | 74ab2ac | 2008-11-23 17:23:07 +0100 | [diff] [blame] | 574 | |
Willy Tarreau | 0c2fc1f | 2009-01-18 15:30:37 +0100 | [diff] [blame] | 575 | if (likely(!b->l)) { |
| 576 | /* optimize data alignment in the buffer */ |
| 577 | b->r = b->w = b->lr = b->data; |
| 578 | if (likely(!b->splice_len)) |
| 579 | b->flags |= BF_EMPTY; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 580 | } |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 581 | |
Willy Tarreau | 0c2fc1f | 2009-01-18 15:30:37 +0100 | [diff] [blame] | 582 | b->send_max -= ret; |
| 583 | if (!b->send_max || !b->l) |
| 584 | break; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 585 | |
Willy Tarreau | ab3e1d3 | 2007-06-03 14:10:36 +0200 | [diff] [blame] | 586 | /* if the system buffer is full, don't insist */ |
| 587 | if (ret < max) |
| 588 | break; |
| 589 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 590 | if (--write_poll <= 0) |
| 591 | break; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 592 | } |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 593 | else if (ret == 0 || errno == EAGAIN) { |
Willy Tarreau | 0c2fc1f | 2009-01-18 15:30:37 +0100 | [diff] [blame] | 594 | /* nothing written, we need to poll for write first */ |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 595 | retval = 0; |
| 596 | break; |
| 597 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 598 | else { |
Willy Tarreau | 0c2fc1f | 2009-01-18 15:30:37 +0100 | [diff] [blame] | 599 | /* bad, we got an error */ |
| 600 | retval = -1; |
| 601 | break; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 602 | } |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 603 | } /* while (1) */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 604 | |
Willy Tarreau | 0c2fc1f | 2009-01-18 15:30:37 +0100 | [diff] [blame] | 605 | return retval; |
| 606 | } |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 607 | |
Willy Tarreau | 0c2fc1f | 2009-01-18 15:30:37 +0100 | [diff] [blame] | 608 | |
| 609 | /* |
| 610 | * This function is called on a write event from a stream socket. |
| 611 | * It returns 0 if the caller needs to poll before calling it again, otherwise |
| 612 | * non-zero. |
| 613 | */ |
| 614 | int stream_sock_write(int fd) |
| 615 | { |
| 616 | struct stream_interface *si = fdtab[fd].owner; |
| 617 | struct buffer *b = si->ob; |
| 618 | int retval = 1; |
| 619 | |
| 620 | #ifdef DEBUG_FULL |
| 621 | fprintf(stderr,"stream_sock_write : fd=%d, owner=%p\n", fd, fdtab[fd].owner); |
| 622 | #endif |
| 623 | |
| 624 | retval = 1; |
| 625 | if (fdtab[fd].state == FD_STERROR || (fdtab[fd].ev & FD_POLL_ERR)) |
| 626 | goto out_error; |
| 627 | |
| 628 | if (likely(!(b->flags & BF_EMPTY))) { |
| 629 | /* OK there are data waiting to be sent */ |
| 630 | retval = stream_sock_write_loop(si, b); |
| 631 | if (retval < 0) |
| 632 | goto out_error; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 633 | } |
Willy Tarreau | 0c2fc1f | 2009-01-18 15:30:37 +0100 | [diff] [blame] | 634 | else { |
| 635 | /* may be we have received a connection acknowledgement in TCP mode without data */ |
| 636 | if (likely(fdtab[fd].state == FD_STCONN)) { |
| 637 | /* We have no data to send to check the connection, and |
| 638 | * getsockopt() will not inform us whether the connection |
| 639 | * is still pending. So we'll reuse connect() to check the |
| 640 | * state of the socket. This has the advantage of givig us |
| 641 | * the following info : |
| 642 | * - error |
| 643 | * - connecting (EALREADY, EINPROGRESS) |
| 644 | * - connected (EISCONN, 0) |
| 645 | */ |
| 646 | if ((connect(fd, fdtab[fd].peeraddr, fdtab[fd].peerlen) == 0)) |
| 647 | errno = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 648 | |
Willy Tarreau | 0c2fc1f | 2009-01-18 15:30:37 +0100 | [diff] [blame] | 649 | if (errno == EALREADY || errno == EINPROGRESS) { |
| 650 | retval = 0; |
| 651 | goto out_may_wakeup; |
| 652 | } |
Willy Tarreau | 3ffeba1 | 2008-12-14 14:42:35 +0100 | [diff] [blame] | 653 | |
Willy Tarreau | 0c2fc1f | 2009-01-18 15:30:37 +0100 | [diff] [blame] | 654 | if (errno && errno != EISCONN) |
| 655 | goto out_error; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 656 | |
Willy Tarreau | 0c2fc1f | 2009-01-18 15:30:37 +0100 | [diff] [blame] | 657 | /* OK we just need to indicate that we got a connection |
| 658 | * and that we wrote nothing. |
| 659 | */ |
| 660 | b->flags |= BF_WRITE_NULL; |
| 661 | fdtab[fd].state = FD_STREADY; |
| 662 | } |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 663 | |
Willy Tarreau | 0c2fc1f | 2009-01-18 15:30:37 +0100 | [diff] [blame] | 664 | /* Funny, we were called to write something but there wasn't |
| 665 | * anything. We can get there, for example if we were woken up |
| 666 | * on a write event to finish the splice, but the send_max is 0 |
| 667 | * so we cannot write anything from the buffer. Let's disable |
| 668 | * the write event and pretend we never came there. |
| 669 | */ |
| 670 | } |
| 671 | |
Willy Tarreau | d2def0f | 2009-01-18 17:37:33 +0100 | [diff] [blame] | 672 | if (!b->splice_len && !b->send_max) { |
Willy Tarreau | 0c2fc1f | 2009-01-18 15:30:37 +0100 | [diff] [blame] | 673 | /* the connection is established but we can't write. Either the |
| 674 | * buffer is empty, or we just refrain from sending because the |
| 675 | * send_max limit was reached. Maybe we just wrote the last |
| 676 | * chunk and need to close. |
| 677 | */ |
| 678 | if (((b->flags & (BF_SHUTW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) == |
| 679 | (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR)) && |
| 680 | (si->state == SI_ST_EST)) { |
| 681 | stream_sock_shutw(si); |
| 682 | goto out_wakeup; |
| 683 | } |
| 684 | |
| 685 | if (b->flags & BF_EMPTY) |
Willy Tarreau | ac128fe | 2009-01-09 13:05:19 +0100 | [diff] [blame] | 686 | si->flags |= SI_FL_WAIT_DATA; |
Willy Tarreau | 0c2fc1f | 2009-01-18 15:30:37 +0100 | [diff] [blame] | 687 | |
Willy Tarreau | ac128fe | 2009-01-09 13:05:19 +0100 | [diff] [blame] | 688 | EV_FD_CLR(fd, DIR_WR); |
Willy Tarreau | 0c2fc1f | 2009-01-18 15:30:37 +0100 | [diff] [blame] | 689 | b->wex = TICK_ETERNITY; |
Willy Tarreau | ac128fe | 2009-01-09 13:05:19 +0100 | [diff] [blame] | 690 | } |
Willy Tarreau | 0c2fc1f | 2009-01-18 15:30:37 +0100 | [diff] [blame] | 691 | |
| 692 | out_may_wakeup: |
| 693 | if (b->flags & BF_WRITE_ACTIVITY) { |
| 694 | /* update timeout if we have written something */ |
Willy Tarreau | d2def0f | 2009-01-18 17:37:33 +0100 | [diff] [blame] | 695 | if ((b->send_max || b->splice_len) && |
| 696 | (b->flags & (BF_SHUTW|BF_WRITE_PARTIAL)) == BF_WRITE_PARTIAL) |
Willy Tarreau | 0c2fc1f | 2009-01-18 15:30:37 +0100 | [diff] [blame] | 697 | b->wex = tick_add_ifset(now_ms, b->wto); |
| 698 | |
| 699 | out_wakeup: |
| 700 | if (tick_isset(si->ib->rex)) { |
| 701 | /* Note: to prevent the client from expiring read timeouts |
| 702 | * during writes, we refresh it. A better solution would be |
| 703 | * to merge read+write timeouts into a unique one, although |
| 704 | * that needs some study particularly on full-duplex TCP |
| 705 | * connections. |
| 706 | */ |
| 707 | si->ib->rex = tick_add_ifset(now_ms, si->ib->rto); |
| 708 | } |
| 709 | |
| 710 | /* the producer might be waiting for more room to store data */ |
| 711 | if (likely((b->flags & (BF_WRITE_PARTIAL|BF_FULL)) == BF_WRITE_PARTIAL && |
| 712 | (b->prod->flags & SI_FL_WAIT_ROOM))) |
| 713 | b->prod->chk_rcv(b->prod); |
| 714 | |
| 715 | /* we have to wake up if there is a special event or if we don't have |
| 716 | * any more data to forward and it's not planned to send any more. |
| 717 | */ |
| 718 | if (likely((b->flags & (BF_WRITE_NULL|BF_WRITE_ERROR|BF_SHUTW)) || |
| 719 | (!b->to_forward && !b->send_max && !b->splice_len) || |
| 720 | si->state != SI_ST_EST || |
| 721 | b->prod->state != SI_ST_EST)) |
| 722 | task_wakeup(si->owner, TASK_WOKEN_IO); |
| 723 | } |
| 724 | |
| 725 | fdtab[fd].ev &= ~FD_POLL_OUT; |
| 726 | return retval; |
Willy Tarreau | ac128fe | 2009-01-09 13:05:19 +0100 | [diff] [blame] | 727 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 728 | out_error: |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 729 | /* Write error on the file descriptor. We mark the FD as STERROR so |
| 730 | * that we don't use it anymore. The error is reported to the stream |
| 731 | * interface which will take proper action. We must not perturbate the |
| 732 | * buffer because the stream interface wants to ensure transparent |
| 733 | * connection retries. |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 734 | */ |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 735 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 736 | fdtab[fd].state = FD_STERROR; |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 737 | fdtab[fd].ev &= ~FD_POLL_STICKY; |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 738 | si->flags |= SI_FL_ERR; |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 739 | task_wakeup(si->owner, TASK_WOKEN_IO); |
| 740 | return 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 741 | } |
| 742 | |
Willy Tarreau | 48adac5 | 2008-08-30 04:58:38 +0200 | [diff] [blame] | 743 | /* |
Willy Tarreau | 3c6ab2e | 2008-09-04 11:19:41 +0200 | [diff] [blame] | 744 | * This function performs a shutdown-write on a stream interface in a connected or |
| 745 | * init state (it does nothing for other states). It either shuts the write side |
Willy Tarreau | 99126c3 | 2008-11-27 10:30:51 +0100 | [diff] [blame] | 746 | * or closes the file descriptor and marks itself as closed. The buffer flags are |
| 747 | * updated to reflect the new state. |
Willy Tarreau | 48adac5 | 2008-08-30 04:58:38 +0200 | [diff] [blame] | 748 | */ |
Willy Tarreau | 0a5d5dd | 2008-11-23 19:31:35 +0100 | [diff] [blame] | 749 | void stream_sock_shutw(struct stream_interface *si) |
Willy Tarreau | 48adac5 | 2008-08-30 04:58:38 +0200 | [diff] [blame] | 750 | { |
Willy Tarreau | 99126c3 | 2008-11-27 10:30:51 +0100 | [diff] [blame] | 751 | if (si->ob->flags & BF_SHUTW) |
| 752 | return; |
| 753 | si->ob->flags |= BF_SHUTW; |
| 754 | si->ob->wex = TICK_ETERNITY; |
Willy Tarreau | b0ef735 | 2008-12-14 13:26:20 +0100 | [diff] [blame] | 755 | si->flags &= ~SI_FL_WAIT_DATA; |
Willy Tarreau | 99126c3 | 2008-11-27 10:30:51 +0100 | [diff] [blame] | 756 | |
Willy Tarreau | b38903c | 2008-11-23 21:33:29 +0100 | [diff] [blame] | 757 | switch (si->state) { |
Willy Tarreau | b38903c | 2008-11-23 21:33:29 +0100 | [diff] [blame] | 758 | case SI_ST_EST: |
| 759 | if (!(si->ib->flags & BF_SHUTR)) { |
| 760 | EV_FD_CLR(si->fd, DIR_WR); |
| 761 | shutdown(si->fd, SHUT_WR); |
| 762 | return; |
| 763 | } |
| 764 | /* fall through */ |
| 765 | case SI_ST_CON: |
Willy Tarreau | b0ef735 | 2008-12-14 13:26:20 +0100 | [diff] [blame] | 766 | si->flags &= ~SI_FL_WAIT_ROOM; |
Willy Tarreau | 8bfa426 | 2008-11-27 09:25:45 +0100 | [diff] [blame] | 767 | /* we may have to close a pending connection, and mark the |
| 768 | * response buffer as shutr |
| 769 | */ |
Willy Tarreau | 48adac5 | 2008-08-30 04:58:38 +0200 | [diff] [blame] | 770 | fd_delete(si->fd); |
Willy Tarreau | fe3718a | 2008-11-30 18:14:12 +0100 | [diff] [blame] | 771 | /* fall through */ |
| 772 | case SI_ST_CER: |
Willy Tarreau | 7f00651 | 2008-12-07 14:04:04 +0100 | [diff] [blame] | 773 | si->state = SI_ST_DIS; |
| 774 | default: |
Willy Tarreau | 99126c3 | 2008-11-27 10:30:51 +0100 | [diff] [blame] | 775 | si->ib->flags |= BF_SHUTR; |
Willy Tarreau | fe3718a | 2008-11-30 18:14:12 +0100 | [diff] [blame] | 776 | si->ib->rex = TICK_ETERNITY; |
Willy Tarreau | 0a5d5dd | 2008-11-23 19:31:35 +0100 | [diff] [blame] | 777 | return; |
Willy Tarreau | 48adac5 | 2008-08-30 04:58:38 +0200 | [diff] [blame] | 778 | } |
Willy Tarreau | 48adac5 | 2008-08-30 04:58:38 +0200 | [diff] [blame] | 779 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 780 | |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 781 | /* |
Willy Tarreau | 3c6ab2e | 2008-09-04 11:19:41 +0200 | [diff] [blame] | 782 | * This function performs a shutdown-read on a stream interface in a connected or |
Willy Tarreau | 0a5d5dd | 2008-11-23 19:31:35 +0100 | [diff] [blame] | 783 | * init state (it does nothing for other states). It either shuts the read side |
Willy Tarreau | 99126c3 | 2008-11-27 10:30:51 +0100 | [diff] [blame] | 784 | * or closes the file descriptor and marks itself as closed. The buffer flags are |
| 785 | * updated to reflect the new state. |
Willy Tarreau | 3c6ab2e | 2008-09-04 11:19:41 +0200 | [diff] [blame] | 786 | */ |
Willy Tarreau | 0a5d5dd | 2008-11-23 19:31:35 +0100 | [diff] [blame] | 787 | void stream_sock_shutr(struct stream_interface *si) |
Willy Tarreau | 3c6ab2e | 2008-09-04 11:19:41 +0200 | [diff] [blame] | 788 | { |
Willy Tarreau | 99126c3 | 2008-11-27 10:30:51 +0100 | [diff] [blame] | 789 | if (si->ib->flags & BF_SHUTR) |
| 790 | return; |
| 791 | si->ib->flags |= BF_SHUTR; |
| 792 | si->ib->rex = TICK_ETERNITY; |
Willy Tarreau | b0ef735 | 2008-12-14 13:26:20 +0100 | [diff] [blame] | 793 | si->flags &= ~SI_FL_WAIT_ROOM; |
Willy Tarreau | 99126c3 | 2008-11-27 10:30:51 +0100 | [diff] [blame] | 794 | |
Willy Tarreau | 8bfa426 | 2008-11-27 09:25:45 +0100 | [diff] [blame] | 795 | if (si->state != SI_ST_EST && si->state != SI_ST_CON) |
Willy Tarreau | 0a5d5dd | 2008-11-23 19:31:35 +0100 | [diff] [blame] | 796 | return; |
Willy Tarreau | 3c6ab2e | 2008-09-04 11:19:41 +0200 | [diff] [blame] | 797 | |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 798 | if (si->ob->flags & BF_SHUTW) { |
Willy Tarreau | 3c6ab2e | 2008-09-04 11:19:41 +0200 | [diff] [blame] | 799 | fd_delete(si->fd); |
Willy Tarreau | 74ab2ac | 2008-11-23 17:23:07 +0100 | [diff] [blame] | 800 | si->state = SI_ST_DIS; |
Willy Tarreau | 0a5d5dd | 2008-11-23 19:31:35 +0100 | [diff] [blame] | 801 | return; |
Willy Tarreau | 3c6ab2e | 2008-09-04 11:19:41 +0200 | [diff] [blame] | 802 | } |
| 803 | EV_FD_CLR(si->fd, DIR_RD); |
Willy Tarreau | 0a5d5dd | 2008-11-23 19:31:35 +0100 | [diff] [blame] | 804 | return; |
Willy Tarreau | 3c6ab2e | 2008-09-04 11:19:41 +0200 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | /* |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 808 | * Updates a connected stream_sock file descriptor status and timeouts |
| 809 | * according to the buffers' flags. It should only be called once after the |
| 810 | * buffer flags have settled down, and before they are cleared. It doesn't |
| 811 | * harm to call it as often as desired (it just slightly hurts performance). |
| 812 | */ |
Willy Tarreau | b025325 | 2008-11-30 21:37:12 +0100 | [diff] [blame] | 813 | void stream_sock_data_finish(struct stream_interface *si) |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 814 | { |
Willy Tarreau | b025325 | 2008-11-30 21:37:12 +0100 | [diff] [blame] | 815 | struct buffer *ib = si->ib; |
| 816 | struct buffer *ob = si->ob; |
| 817 | int fd = si->fd; |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 818 | |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 819 | DPRINTF(stderr,"[%u] %s: fd=%d owner=%p ib=%p, ob=%p, exp(r,w)=%u,%u ibf=%08x obf=%08x ibl=%d obl=%d si=%d\n", |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 820 | now_ms, __FUNCTION__, |
| 821 | fd, fdtab[fd].owner, |
| 822 | ib, ob, |
| 823 | ib->rex, ob->wex, |
| 824 | ib->flags, ob->flags, |
Willy Tarreau | b025325 | 2008-11-30 21:37:12 +0100 | [diff] [blame] | 825 | ib->l, ob->l, si->state); |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 826 | |
| 827 | /* Check if we need to close the read side */ |
| 828 | if (!(ib->flags & BF_SHUTR)) { |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 829 | /* Read not closed, update FD status and timeout for reads */ |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 830 | if (ib->flags & (BF_FULL|BF_HIJACK)) { |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 831 | /* stop reading */ |
Willy Tarreau | b0ef735 | 2008-12-14 13:26:20 +0100 | [diff] [blame] | 832 | if ((ib->flags & (BF_FULL|BF_HIJACK)) == BF_FULL) |
| 833 | si->flags |= SI_FL_WAIT_ROOM; |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 834 | EV_FD_COND_C(fd, DIR_RD); |
| 835 | ib->rex = TICK_ETERNITY; |
| 836 | } |
| 837 | else { |
| 838 | /* (re)start reading and update timeout. Note: we don't recompute the timeout |
| 839 | * everytime we get here, otherwise it would risk never to expire. We only |
| 840 | * update it if is was not yet set, or if we already got some read status. |
| 841 | */ |
Willy Tarreau | b0ef735 | 2008-12-14 13:26:20 +0100 | [diff] [blame] | 842 | si->flags &= ~SI_FL_WAIT_ROOM; |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 843 | EV_FD_COND_S(fd, DIR_RD); |
Willy Tarreau | 86491c3 | 2008-12-14 09:04:47 +0100 | [diff] [blame] | 844 | if (!(ib->flags & BF_READ_NOEXP) && |
| 845 | (!tick_isset(ib->rex) || ib->flags & BF_READ_ACTIVITY)) |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 846 | ib->rex = tick_add_ifset(now_ms, ib->rto); |
| 847 | } |
| 848 | } |
| 849 | |
| 850 | /* Check if we need to close the write side */ |
| 851 | if (!(ob->flags & BF_SHUTW)) { |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 852 | /* Write not closed, update FD status and timeout for writes */ |
Willy Tarreau | dcef33f | 2009-01-07 19:33:39 +0100 | [diff] [blame] | 853 | if ((ob->send_max == 0 && ob->splice_len == 0) || |
Willy Tarreau | 3ffeba1 | 2008-12-14 14:42:35 +0100 | [diff] [blame] | 854 | (ob->flags & BF_EMPTY) || |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 855 | (ob->flags & (BF_HIJACK|BF_WRITE_ENA)) == 0) { |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 856 | /* stop writing */ |
Willy Tarreau | b0ef735 | 2008-12-14 13:26:20 +0100 | [diff] [blame] | 857 | if ((ob->flags & (BF_EMPTY|BF_HIJACK|BF_WRITE_ENA)) == (BF_EMPTY|BF_WRITE_ENA)) |
| 858 | si->flags |= SI_FL_WAIT_DATA; |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 859 | EV_FD_COND_C(fd, DIR_WR); |
| 860 | ob->wex = TICK_ETERNITY; |
| 861 | } |
| 862 | else { |
| 863 | /* (re)start writing and update timeout. Note: we don't recompute the timeout |
| 864 | * everytime we get here, otherwise it would risk never to expire. We only |
| 865 | * update it if is was not yet set, or if we already got some write status. |
| 866 | */ |
Willy Tarreau | b0ef735 | 2008-12-14 13:26:20 +0100 | [diff] [blame] | 867 | si->flags &= ~SI_FL_WAIT_DATA; |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 868 | EV_FD_COND_S(fd, DIR_WR); |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 869 | if (!tick_isset(ob->wex) || ob->flags & BF_WRITE_ACTIVITY) { |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 870 | ob->wex = tick_add_ifset(now_ms, ob->wto); |
Willy Tarreau | 21e1be8 | 2008-08-29 11:30:14 +0200 | [diff] [blame] | 871 | if (tick_isset(ob->wex) && tick_isset(ib->rex)) { |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 872 | /* Note: depending on the protocol, we don't know if we're waiting |
| 873 | * for incoming data or not. So in order to prevent the socket from |
| 874 | * expiring read timeouts during writes, we refresh the read timeout, |
| 875 | * except if it was already infinite. |
| 876 | */ |
| 877 | ib->rex = ob->wex; |
| 878 | } |
| 879 | } |
| 880 | } |
| 881 | } |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 882 | } |
| 883 | |
Willy Tarreau | 3ffeba1 | 2008-12-14 14:42:35 +0100 | [diff] [blame] | 884 | /* This function is used for inter-stream-interface calls. It is called by the |
| 885 | * consumer to inform the producer side that it may be interested in checking |
| 886 | * for free space in the buffer. Note that it intentionally does not update |
| 887 | * timeouts, so that we can still check them later at wake-up. |
| 888 | */ |
| 889 | void stream_sock_chk_rcv(struct stream_interface *si) |
| 890 | { |
| 891 | struct buffer *ib = si->ib; |
| 892 | |
| 893 | DPRINTF(stderr,"[%u] %s: fd=%d owner=%p ib=%p, ob=%p, exp(r,w)=%u,%u ibf=%08x obf=%08x ibl=%d obl=%d si=%d\n", |
| 894 | now_ms, __FUNCTION__, |
| 895 | fd, fdtab[fd].owner, |
| 896 | ib, ob, |
| 897 | ib->rex, ob->wex, |
| 898 | ib->flags, ob->flags, |
| 899 | ib->l, ob->l, si->state); |
| 900 | |
| 901 | if (unlikely(si->state != SI_ST_EST || (ib->flags & BF_SHUTR))) |
| 902 | return; |
| 903 | |
| 904 | if (ib->flags & (BF_FULL|BF_HIJACK)) { |
| 905 | /* stop reading */ |
| 906 | if ((ib->flags & (BF_FULL|BF_HIJACK)) == BF_FULL) |
| 907 | si->flags |= SI_FL_WAIT_ROOM; |
| 908 | EV_FD_COND_C(si->fd, DIR_RD); |
| 909 | } |
| 910 | else { |
| 911 | /* (re)start reading */ |
| 912 | si->flags &= ~SI_FL_WAIT_ROOM; |
| 913 | EV_FD_COND_S(si->fd, DIR_RD); |
| 914 | } |
| 915 | } |
| 916 | |
| 917 | |
| 918 | /* This function is used for inter-stream-interface calls. It is called by the |
| 919 | * producer to inform the consumer side that it may be interested in checking |
| 920 | * for data in the buffer. Note that it intentionally does not update timeouts, |
| 921 | * so that we can still check them later at wake-up. |
| 922 | */ |
| 923 | void stream_sock_chk_snd(struct stream_interface *si) |
| 924 | { |
| 925 | struct buffer *ob = si->ob; |
Willy Tarreau | a456f2a | 2009-01-18 17:38:44 +0100 | [diff] [blame] | 926 | int retval; |
Willy Tarreau | 3ffeba1 | 2008-12-14 14:42:35 +0100 | [diff] [blame] | 927 | |
| 928 | DPRINTF(stderr,"[%u] %s: fd=%d owner=%p ib=%p, ob=%p, exp(r,w)=%u,%u ibf=%08x obf=%08x ibl=%d obl=%d si=%d\n", |
| 929 | now_ms, __FUNCTION__, |
| 930 | fd, fdtab[fd].owner, |
| 931 | ib, ob, |
| 932 | ib->rex, ob->wex, |
| 933 | ib->flags, ob->flags, |
| 934 | ib->l, ob->l, si->state); |
| 935 | |
| 936 | if (unlikely(si->state != SI_ST_EST || (ob->flags & BF_SHUTW))) |
| 937 | return; |
| 938 | |
Willy Tarreau | a456f2a | 2009-01-18 17:38:44 +0100 | [diff] [blame] | 939 | if (!(si->flags & SI_FL_WAIT_DATA) || /* not waiting for data */ |
| 940 | (fdtab[si->fd].ev & FD_POLL_OUT) || /* we'll be called anyway */ |
| 941 | !(ob->send_max || ob->splice_len) || /* called with nothing to send ! */ |
| 942 | !(ob->flags & (BF_HIJACK|BF_WRITE_ENA))) /* we may not write */ |
| 943 | return; |
| 944 | |
| 945 | retval = stream_sock_write_loop(si, ob); |
| 946 | if (retval < 0) { |
| 947 | /* Write error on the file descriptor. We mark the FD as STERROR so |
| 948 | * that we don't use it anymore and we notify the task. |
| 949 | */ |
| 950 | fdtab[si->fd].state = FD_STERROR; |
| 951 | fdtab[si->fd].ev &= ~FD_POLL_STICKY; |
| 952 | si->flags |= SI_FL_ERR; |
| 953 | goto out_wakeup; |
| 954 | } |
| 955 | |
| 956 | if (retval > 0 || (ob->send_max == 0 && ob->splice_len == 0)) { |
| 957 | /* the connection is established but we can't write. Either the |
| 958 | * buffer is empty, or we just refrain from sending because the |
| 959 | * send_max limit was reached. Maybe we just wrote the last |
| 960 | * chunk and need to close. |
| 961 | */ |
| 962 | if (((ob->flags & (BF_SHUTW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) == |
| 963 | (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR)) && |
| 964 | (si->state == SI_ST_EST)) { |
| 965 | stream_sock_shutw(si); |
| 966 | goto out_wakeup; |
| 967 | } |
| 968 | |
Willy Tarreau | 3ffeba1 | 2008-12-14 14:42:35 +0100 | [diff] [blame] | 969 | if ((ob->flags & (BF_EMPTY|BF_HIJACK|BF_WRITE_ENA)) == (BF_EMPTY|BF_WRITE_ENA)) |
| 970 | si->flags |= SI_FL_WAIT_DATA; |
Willy Tarreau | a456f2a | 2009-01-18 17:38:44 +0100 | [diff] [blame] | 971 | ob->wex = TICK_ETERNITY; |
Willy Tarreau | 3ffeba1 | 2008-12-14 14:42:35 +0100 | [diff] [blame] | 972 | } |
| 973 | else { |
| 974 | /* (re)start writing. */ |
| 975 | si->flags &= ~SI_FL_WAIT_DATA; |
| 976 | EV_FD_COND_S(si->fd, DIR_WR); |
| 977 | } |
Willy Tarreau | a456f2a | 2009-01-18 17:38:44 +0100 | [diff] [blame] | 978 | |
| 979 | /* in case of special condition (error, shutdown, end of write...), we |
| 980 | * have to notify the task. |
| 981 | */ |
| 982 | if (likely((ob->flags & (BF_WRITE_NULL|BF_WRITE_ERROR|BF_SHUTW)) || |
| 983 | (!ob->to_forward && !ob->send_max && !ob->splice_len) || |
| 984 | si->state != SI_ST_EST)) { |
| 985 | out_wakeup: |
| 986 | task_wakeup(si->owner, TASK_WOKEN_IO); |
| 987 | } |
Willy Tarreau | 3ffeba1 | 2008-12-14 14:42:35 +0100 | [diff] [blame] | 988 | } |
| 989 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 990 | |
| 991 | /* |
| 992 | * Local variables: |
| 993 | * c-indent-level: 8 |
| 994 | * c-basic-offset: 8 |
| 995 | * End: |
| 996 | */ |