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