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