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