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 | |
| 13 | #include <errno.h> |
| 14 | #include <fcntl.h> |
| 15 | #include <stdio.h> |
| 16 | #include <stdlib.h> |
| 17 | |
| 18 | #include <sys/socket.h> |
| 19 | #include <sys/stat.h> |
| 20 | #include <sys/types.h> |
| 21 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 22 | #include <common/compat.h> |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 23 | #include <common/config.h> |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 24 | #include <common/debug.h> |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 25 | #include <common/standard.h> |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 26 | #include <common/ticks.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 27 | #include <common/time.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 28 | |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 29 | #include <proto/buffers.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 30 | #include <proto/client.h> |
| 31 | #include <proto/fd.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 32 | #include <proto/stream_sock.h> |
| 33 | #include <proto/task.h> |
| 34 | |
| 35 | |
| 36 | /* |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 37 | * this function is called on a read event from a stream socket. |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 38 | * It returns 0 if we have a high confidence that we will not be |
| 39 | * able to read more data without polling first. Returns non-zero |
| 40 | * otherwise. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 41 | */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 42 | int stream_sock_read(int fd) { |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 43 | __label__ out_wakeup, out_shutdown_r, out_error; |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 44 | struct stream_interface *si = fdtab[fd].owner; |
Willy Tarreau | 48adac5 | 2008-08-30 04:58:38 +0200 | [diff] [blame] | 45 | struct buffer *b = si->ib; |
Willy Tarreau | 8a7af60 | 2008-05-03 23:07:14 +0200 | [diff] [blame] | 46 | int ret, max, retval, cur_read; |
Willy Tarreau | b8949f1 | 2007-03-23 22:39:59 +0100 | [diff] [blame] | 47 | int read_poll = MAX_READ_POLL_LOOPS; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 48 | |
| 49 | #ifdef DEBUG_FULL |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 50 | 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] | 51 | #endif |
| 52 | |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 53 | retval = 1; |
| 54 | |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 55 | /* stop immediately on errors */ |
| 56 | if (fdtab[fd].state == FD_STERROR || (fdtab[fd].ev & FD_POLL_ERR)) |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 57 | goto out_error; |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 58 | |
| 59 | /* stop here if we reached the end of data */ |
| 60 | if ((fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP)) == FD_POLL_HUP) |
| 61 | goto out_shutdown_r; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 62 | |
Willy Tarreau | 8a7af60 | 2008-05-03 23:07:14 +0200 | [diff] [blame] | 63 | cur_read = 0; |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 64 | while (1) { |
| 65 | /* |
| 66 | * 1. compute the maximum block size we can read at once. |
| 67 | */ |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 68 | if (b->l == 0) { /* let's realign the buffer to optimize I/O */ |
| 69 | b->r = b->w = b->lr = b->data; |
| 70 | max = b->rlim - b->data; |
| 71 | } |
| 72 | else if (b->r > b->w) { |
| 73 | max = b->rlim - b->r; |
| 74 | } |
| 75 | else { |
| 76 | max = b->w - b->r; |
| 77 | /* FIXME: theorically, if w>0, we shouldn't have rlim < data+size anymore |
| 78 | * since it means that the rewrite protection has been removed. This |
| 79 | * implies that the if statement can be removed. |
| 80 | */ |
| 81 | if (max > b->rlim - b->data) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 82 | max = b->rlim - b->data; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 83 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 84 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 85 | if (unlikely(max == 0)) { |
| 86 | /* Not anymore room to store data. This should theorically |
| 87 | * never happen, but better safe than sorry ! |
| 88 | */ |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 89 | b->flags |= BF_FULL; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 90 | EV_FD_CLR(fd, DIR_RD); |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 91 | b->rex = TICK_ETERNITY; |
| 92 | goto out_wakeup; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 93 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 94 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 95 | /* |
| 96 | * 2. read the largest possible block |
| 97 | */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 98 | #ifndef MSG_NOSIGNAL |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 99 | { |
| 100 | int skerr; |
| 101 | socklen_t lskerr = sizeof(skerr); |
| 102 | |
| 103 | ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr); |
| 104 | if (ret == -1 || skerr) |
| 105 | ret = -1; |
| 106 | else |
| 107 | ret = recv(fd, b->r, max, 0); |
| 108 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 109 | #else |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 110 | ret = recv(fd, b->r, max, MSG_NOSIGNAL); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 111 | #endif |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 112 | if (ret > 0) { |
| 113 | b->r += ret; |
| 114 | b->l += ret; |
Willy Tarreau | 8a7af60 | 2008-05-03 23:07:14 +0200 | [diff] [blame] | 115 | cur_read += ret; |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 116 | b->flags |= BF_READ_PARTIAL; |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 117 | b->flags &= ~BF_EMPTY; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 118 | |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 119 | if (b->r == b->data + BUFSIZE) { |
| 120 | b->r = b->data; /* wrap around the buffer */ |
| 121 | } |
Willy Tarreau | 9641e8f | 2007-03-23 23:02:09 +0100 | [diff] [blame] | 122 | |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 123 | b->total += ret; |
Willy Tarreau | 9641e8f | 2007-03-23 23:02:09 +0100 | [diff] [blame] | 124 | |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 125 | if (b->l >= b->rlim - b->data) { |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 126 | /* The buffer is now full, there's no point in going through |
| 127 | * the loop again. |
| 128 | */ |
Willy Tarreau | 8a7af60 | 2008-05-03 23:07:14 +0200 | [diff] [blame] | 129 | if (!(b->flags & BF_STREAMER_FAST) && (cur_read == b->l)) { |
| 130 | b->xfer_small = 0; |
| 131 | b->xfer_large++; |
| 132 | if (b->xfer_large >= 3) { |
| 133 | /* we call this buffer a fast streamer if it manages |
| 134 | * to be filled in one call 3 consecutive times. |
| 135 | */ |
| 136 | b->flags |= (BF_STREAMER | BF_STREAMER_FAST); |
| 137 | //fputc('+', stderr); |
| 138 | } |
| 139 | } |
| 140 | else if ((b->flags & (BF_STREAMER | BF_STREAMER_FAST)) && |
| 141 | (cur_read <= BUFSIZE / 2)) { |
| 142 | b->xfer_large = 0; |
| 143 | b->xfer_small++; |
| 144 | if (b->xfer_small >= 2) { |
| 145 | /* if the buffer has been at least half full twice, |
| 146 | * we receive faster than we send, so at least it |
| 147 | * is not a "fast streamer". |
| 148 | */ |
| 149 | b->flags &= ~BF_STREAMER_FAST; |
| 150 | //fputc('-', stderr); |
| 151 | } |
| 152 | } |
| 153 | else { |
| 154 | b->xfer_small = 0; |
| 155 | b->xfer_large = 0; |
| 156 | } |
| 157 | |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 158 | b->flags |= BF_FULL; |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 159 | EV_FD_CLR(fd, DIR_RD); |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 160 | b->rex = TICK_ETERNITY; |
| 161 | goto out_wakeup; |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 162 | } |
| 163 | |
Willy Tarreau | ab3e1d3 | 2007-06-03 14:10:36 +0200 | [diff] [blame] | 164 | /* if too many bytes were missing from last read, it means that |
| 165 | * it's pointless trying to read again because the system does |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 166 | * not have them in buffers. BTW, if FD_POLL_HUP was present, |
| 167 | * it means that we have reached the end and that the connection |
| 168 | * is closed. |
Willy Tarreau | ab3e1d3 | 2007-06-03 14:10:36 +0200 | [diff] [blame] | 169 | */ |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 170 | if (ret < max) { |
Willy Tarreau | 8a7af60 | 2008-05-03 23:07:14 +0200 | [diff] [blame] | 171 | if ((b->flags & (BF_STREAMER | BF_STREAMER_FAST)) && |
| 172 | (cur_read <= BUFSIZE / 2)) { |
| 173 | b->xfer_large = 0; |
| 174 | b->xfer_small++; |
| 175 | if (b->xfer_small >= 3) { |
| 176 | /* we have read less than half of the buffer in |
| 177 | * one pass, and this happened at least 3 times. |
| 178 | * This is definitely not a streamer. |
| 179 | */ |
| 180 | b->flags &= ~(BF_STREAMER | BF_STREAMER_FAST); |
| 181 | //fputc('!', stderr); |
| 182 | } |
| 183 | } |
Willy Tarreau | 2bea3a1 | 2008-08-28 09:47:43 +0200 | [diff] [blame] | 184 | /* unfortunately, on level-triggered events, POLL_HUP |
| 185 | * is generally delivered AFTER the system buffer is |
| 186 | * empty, so this one might never match. |
| 187 | */ |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 188 | if (fdtab[fd].ev & FD_POLL_HUP) |
| 189 | goto out_shutdown_r; |
Willy Tarreau | 2bea3a1 | 2008-08-28 09:47:43 +0200 | [diff] [blame] | 190 | |
| 191 | /* if a streamer has read few data, it may be because we |
| 192 | * have exhausted system buffers. It's not worth trying |
| 193 | * again. |
| 194 | */ |
| 195 | if (b->flags & BF_STREAMER) |
| 196 | break; |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 197 | } |
Willy Tarreau | ab3e1d3 | 2007-06-03 14:10:36 +0200 | [diff] [blame] | 198 | |
| 199 | /* generally if we read something smaller than 1 or 2 MSS, |
Willy Tarreau | 2bea3a1 | 2008-08-28 09:47:43 +0200 | [diff] [blame] | 200 | * it means that either we have exhausted the system's |
| 201 | * buffers (streamer or question-response protocol) or that |
| 202 | * the connection will be closed. Streamers are easily |
| 203 | * detected so we return early. For other cases, it's still |
| 204 | * better to perform a last read to be sure, because it may |
| 205 | * save one complete poll/read/wakeup cycle in case of shutdown. |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 206 | */ |
Willy Tarreau | 2bea3a1 | 2008-08-28 09:47:43 +0200 | [diff] [blame] | 207 | if (ret < MIN_RET_FOR_READ_LOOP && b->flags & BF_STREAMER) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 208 | break; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 209 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 210 | if (--read_poll <= 0) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 211 | break; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 212 | } |
| 213 | else if (ret == 0) { |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 214 | /* connection closed */ |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 215 | goto out_shutdown_r; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 216 | } |
Willy Tarreau | 9f19529 | 2007-04-15 21:26:58 +0200 | [diff] [blame] | 217 | else if (errno == EAGAIN) { |
| 218 | /* Ignore EAGAIN but inform the poller that there is |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 219 | * nothing to read left. But we may have done some work |
| 220 | * justifying to notify the task. |
Willy Tarreau | 9f19529 | 2007-04-15 21:26:58 +0200 | [diff] [blame] | 221 | */ |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 222 | retval = 0; |
| 223 | break; |
| 224 | } |
| 225 | else { |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 226 | goto out_error; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 227 | } |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 228 | } /* while (1) */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 229 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 230 | /* |
| 231 | * The only way to get out of this loop is to have stopped reading |
| 232 | * without any error nor close, either by limiting the number of |
| 233 | * loops, or because of an EAGAIN. We only rearm the timer if we |
| 234 | * have at least read something. |
| 235 | */ |
| 236 | |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 237 | if (tick_isset(b->rex) && b->flags & BF_READ_PARTIAL) |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 238 | b->rex = tick_add_ifset(now_ms, b->rto); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 239 | |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 240 | if (!(b->flags & BF_READ_ACTIVITY)) |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 241 | goto out_skip_wakeup; |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 242 | out_wakeup: |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 243 | task_wakeup(si->owner, TASK_WOKEN_IO); |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 244 | |
| 245 | out_skip_wakeup: |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 246 | fdtab[fd].ev &= ~FD_POLL_IN; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 247 | return retval; |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 248 | |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 249 | out_shutdown_r: |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 250 | /* we received a shutdown */ |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 251 | fdtab[fd].ev &= ~FD_POLL_HUP; |
| 252 | b->flags |= BF_READ_NULL; |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 253 | buffer_shutr(b); |
Willy Tarreau | 48adac5 | 2008-08-30 04:58:38 +0200 | [diff] [blame] | 254 | |
| 255 | /* Maybe we have to completely close the local socket */ |
| 256 | if (si->ob->flags & BF_SHUTW) |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 257 | goto do_close_and_return; |
| 258 | EV_FD_CLR(fd, DIR_RD); |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 259 | goto out_wakeup; |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 260 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 261 | out_error: |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame^] | 262 | /* Read error on the file descriptor. We mark the FD as STERROR so |
| 263 | * that we don't use it anymore. The error is reported to the stream |
| 264 | * interface which will take proper action. We must not perturbate the |
| 265 | * buffer because the stream interface wants to ensure transparent |
| 266 | * connection retries. |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 267 | */ |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame^] | 268 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 269 | fdtab[fd].state = FD_STERROR; |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 270 | fdtab[fd].ev &= ~FD_POLL_STICKY; |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame^] | 271 | si->flags |= SI_FL_ERR; |
| 272 | goto wakeup_return; |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 273 | |
| 274 | do_close_and_return: |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 275 | si->state = SI_ST_CLO; |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame^] | 276 | fd_delete(fd); |
| 277 | wakeup_return: |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 278 | task_wakeup(si->owner, TASK_WOKEN_IO); |
| 279 | return 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | |
| 283 | /* |
Willy Tarreau | f8306d5 | 2006-07-29 19:01:31 +0200 | [diff] [blame] | 284 | * this function is called on a write event from a stream socket. |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 285 | * It returns 0 if we have a high confidence that we will not be |
| 286 | * able to write more data without polling first. Returns non-zero |
| 287 | * otherwise. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 288 | */ |
Willy Tarreau | f8306d5 | 2006-07-29 19:01:31 +0200 | [diff] [blame] | 289 | int stream_sock_write(int fd) { |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 290 | __label__ out_wakeup, out_error; |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 291 | struct stream_interface *si = fdtab[fd].owner; |
Willy Tarreau | 48adac5 | 2008-08-30 04:58:38 +0200 | [diff] [blame] | 292 | struct buffer *b = si->ob; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 293 | int ret, max, retval; |
| 294 | int write_poll = MAX_WRITE_POLL_LOOPS; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 295 | |
| 296 | #ifdef DEBUG_FULL |
Willy Tarreau | f8306d5 | 2006-07-29 19:01:31 +0200 | [diff] [blame] | 297 | fprintf(stderr,"stream_sock_write : fd=%d, owner=%p\n", fd, fdtab[fd].owner); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 298 | #endif |
| 299 | |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 300 | retval = 1; |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 301 | if (fdtab[fd].state == FD_STERROR || (fdtab[fd].ev & FD_POLL_ERR)) |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 302 | goto out_error; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 303 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 304 | while (1) { |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 305 | if (b->l == 0) { /* let's realign the buffer to optimize I/O */ |
| 306 | b->r = b->w = b->lr = b->data; |
| 307 | max = 0; |
| 308 | } |
| 309 | else if (b->r > b->w) { |
| 310 | max = b->r - b->w; |
| 311 | } |
| 312 | else { |
| 313 | max = b->data + BUFSIZE - b->w; |
| 314 | } |
| 315 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 316 | if (max == 0) { |
Willy Tarreau | f8306d5 | 2006-07-29 19:01:31 +0200 | [diff] [blame] | 317 | /* may be we have received a connection acknowledgement in TCP mode without data */ |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 318 | if (likely(fdtab[fd].state == FD_STCONN)) { |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 319 | /* We have no data to send to check the connection, and |
| 320 | * getsockopt() will not inform us whether the connection |
| 321 | * is still pending. So we'll reuse connect() to check the |
| 322 | * state of the socket. This has the advantage of givig us |
| 323 | * the following info : |
| 324 | * - error |
| 325 | * - connecting (EALREADY, EINPROGRESS) |
| 326 | * - connected (EISCONN, 0) |
| 327 | */ |
Willy Tarreau | e94ebd0 | 2007-10-09 17:14:37 +0200 | [diff] [blame] | 328 | if ((connect(fd, fdtab[fd].peeraddr, fdtab[fd].peerlen) == 0)) |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 329 | errno = 0; |
| 330 | |
| 331 | if (errno == EALREADY || errno == EINPROGRESS) { |
| 332 | retval = 0; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 333 | goto out_may_wakeup; |
Willy Tarreau | f8306d5 | 2006-07-29 19:01:31 +0200 | [diff] [blame] | 334 | } |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 335 | |
| 336 | if (errno && errno != EISCONN) |
| 337 | goto out_error; |
| 338 | |
| 339 | /* OK we just need to indicate that we got a connection |
| 340 | * and that we wrote nothing. |
| 341 | */ |
| 342 | b->flags |= BF_WRITE_NULL; |
| 343 | fdtab[fd].state = FD_STREADY; |
Willy Tarreau | f8306d5 | 2006-07-29 19:01:31 +0200 | [diff] [blame] | 344 | } |
| 345 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 346 | /* Funny, we were called to write something but there wasn't |
| 347 | * anything. Theorically we cannot get there, but just in case, |
| 348 | * let's disable the write event and pretend we never came there. |
| 349 | */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 350 | EV_FD_CLR(fd, DIR_WR); |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 351 | b->wex = TICK_ETERNITY; |
| 352 | goto out_wakeup; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | #ifndef MSG_NOSIGNAL |
| 356 | { |
| 357 | int skerr; |
| 358 | socklen_t lskerr = sizeof(skerr); |
| 359 | |
Willy Tarreau | c642348 | 2006-10-15 14:59:03 +0200 | [diff] [blame] | 360 | ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr); |
| 361 | if (ret == -1 || skerr) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 362 | ret = -1; |
| 363 | else |
| 364 | ret = send(fd, b->w, max, MSG_DONTWAIT); |
| 365 | } |
| 366 | #else |
| 367 | ret = send(fd, b->w, max, MSG_DONTWAIT | MSG_NOSIGNAL); |
| 368 | #endif |
| 369 | |
| 370 | if (ret > 0) { |
| 371 | b->l -= ret; |
| 372 | b->w += ret; |
| 373 | |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 374 | b->flags |= BF_WRITE_PARTIAL; |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 375 | |
| 376 | if (b->l < b->rlim - b->data) |
| 377 | b->flags &= ~BF_FULL; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 378 | |
| 379 | if (b->w == b->data + BUFSIZE) { |
| 380 | b->w = b->data; /* wrap around the buffer */ |
| 381 | } |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 382 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 383 | if (!b->l) { |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 384 | b->flags |= BF_EMPTY; |
Willy Tarreau | 48adac5 | 2008-08-30 04:58:38 +0200 | [diff] [blame] | 385 | |
| 386 | /* Maybe we just wrote the last chunk and need to close ? */ |
| 387 | if ((b->flags & (BF_SHUTW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) == (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR)) { |
| 388 | if (si->state == SI_ST_EST) { |
| 389 | buffer_shutw(b); |
| 390 | if (si->ib->flags & BF_SHUTR) |
| 391 | goto do_close_and_return; |
| 392 | shutdown(fd, SHUT_WR); |
| 393 | } |
| 394 | } |
| 395 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 396 | EV_FD_CLR(fd, DIR_WR); |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 397 | b->wex = TICK_ETERNITY; |
| 398 | goto out_wakeup; |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 399 | } |
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) { |
| 409 | /* nothing written, just pretend we were never called |
| 410 | * and wait for the socket to be ready. But we may have |
| 411 | * done some work justifying to notify the task. |
| 412 | */ |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 413 | retval = 0; |
| 414 | break; |
| 415 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 416 | else { |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 417 | goto out_error; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 418 | } |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 419 | } /* while (1) */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 420 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 421 | /* |
| 422 | * The only way to get out of this loop is to have stopped writing |
| 423 | * without any error, either by limiting the number of loops, or |
| 424 | * because of an EAGAIN. We only rearm the timer if we have at least |
| 425 | * written something. |
| 426 | */ |
| 427 | |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 428 | if (tick_isset(b->wex) && b->flags & BF_WRITE_PARTIAL) { |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 429 | b->wex = tick_add_ifset(now_ms, b->wto); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 430 | if (tick_isset(b->wex)) { |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 431 | /* FIXME: to prevent the client from expiring read timeouts during writes, |
| 432 | * we refresh it. A solution would be to merge read+write timeouts into a |
| 433 | * unique one, although that needs some study particularly on full-duplex |
| 434 | * TCP connections. */ |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 435 | if (tick_isset(b->rex) && !(b->flags & BF_SHUTR)) |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 436 | b->rex = b->wex; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 437 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 438 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 439 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 440 | out_may_wakeup: |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 441 | if (!(b->flags & BF_WRITE_ACTIVITY)) |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 442 | goto out_skip_wakeup; |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 443 | out_wakeup: |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 444 | task_wakeup(si->owner, TASK_WOKEN_IO); |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 445 | |
| 446 | out_skip_wakeup: |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 447 | fdtab[fd].ev &= ~FD_POLL_OUT; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 448 | return retval; |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 449 | |
| 450 | out_error: |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame^] | 451 | /* Write error on the file descriptor. We mark the FD as STERROR so |
| 452 | * that we don't use it anymore. The error is reported to the stream |
| 453 | * interface which will take proper action. We must not perturbate the |
| 454 | * buffer because the stream interface wants to ensure transparent |
| 455 | * connection retries. |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 456 | */ |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame^] | 457 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 458 | fdtab[fd].state = FD_STERROR; |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 459 | fdtab[fd].ev &= ~FD_POLL_STICKY; |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame^] | 460 | si->flags |= SI_FL_ERR; |
| 461 | goto wakeup_return; |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 462 | |
Willy Tarreau | 48adac5 | 2008-08-30 04:58:38 +0200 | [diff] [blame] | 463 | do_close_and_return: |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 464 | si->state = SI_ST_CLO; |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame^] | 465 | fd_delete(fd); |
| 466 | wakeup_return: |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 467 | task_wakeup(si->owner, TASK_WOKEN_IO); |
| 468 | return 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 469 | } |
| 470 | |
Willy Tarreau | 48adac5 | 2008-08-30 04:58:38 +0200 | [diff] [blame] | 471 | /* |
Willy Tarreau | 3c6ab2e | 2008-09-04 11:19:41 +0200 | [diff] [blame] | 472 | * This function performs a shutdown-write on a stream interface in a connected or |
| 473 | * init state (it does nothing for other states). It either shuts the write side |
Willy Tarreau | 48adac5 | 2008-08-30 04:58:38 +0200 | [diff] [blame] | 474 | * closes the file descriptor and marks itself as closed. No buffer flags are |
| 475 | * changed, it's up to the caller to adjust them. The sole purpose of this |
| 476 | * function is to be called from the other stream interface to notify of a |
| 477 | * close_read, or by itself upon a full write leading to an empty buffer. |
| 478 | * It normally returns zero, unless it has completely closed the socket, in |
| 479 | * which case it returns 1. |
| 480 | */ |
| 481 | int stream_sock_shutw(struct stream_interface *si) |
| 482 | { |
Willy Tarreau | 3c6ab2e | 2008-09-04 11:19:41 +0200 | [diff] [blame] | 483 | if (si->state != SI_ST_EST && si->state != SI_ST_CON) |
Willy Tarreau | 48adac5 | 2008-08-30 04:58:38 +0200 | [diff] [blame] | 484 | return 0; |
| 485 | |
| 486 | if (si->ib->flags & BF_SHUTR) { |
| 487 | fd_delete(si->fd); |
| 488 | si->state = SI_ST_CLO; |
| 489 | return 1; |
| 490 | } |
| 491 | EV_FD_CLR(si->fd, DIR_WR); |
| 492 | shutdown(si->fd, SHUT_WR); |
| 493 | return 0; |
| 494 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 495 | |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 496 | /* |
Willy Tarreau | 3c6ab2e | 2008-09-04 11:19:41 +0200 | [diff] [blame] | 497 | * This function performs a shutdown-read on a stream interface in a connected or |
| 498 | * init state (it does nothing for other states). It either shuts the read side or |
| 499 | * closes the file descriptor and marks itself as closed. No buffer flags are |
| 500 | * changed, it's up to the caller to adjust them. The sole purpose of this |
| 501 | * function is to be called from the other stream interface to notify of a |
| 502 | * close_read, or by itself upon a full write leading to an empty buffer. |
| 503 | * It normally returns zero, unless it has completely closed the socket, in |
| 504 | * which case it returns 1. |
| 505 | */ |
| 506 | int stream_sock_shutr(struct stream_interface *si) |
| 507 | { |
| 508 | if (si->state != SI_ST_EST && si->state != SI_ST_CON) |
| 509 | return 0; |
| 510 | |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame^] | 511 | if (si->ob->flags & BF_SHUTW) { |
Willy Tarreau | 3c6ab2e | 2008-09-04 11:19:41 +0200 | [diff] [blame] | 512 | fd_delete(si->fd); |
| 513 | si->state = SI_ST_CLO; |
| 514 | return 1; |
| 515 | } |
| 516 | EV_FD_CLR(si->fd, DIR_RD); |
| 517 | return 0; |
| 518 | } |
| 519 | |
| 520 | /* |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 521 | * Manages a stream_sock connection during its data phase. The buffers are |
| 522 | * examined for various cases of shutdown, then file descriptor and buffers' |
| 523 | * flags are updated accordingly. |
| 524 | */ |
| 525 | int stream_sock_data_update(int fd) |
| 526 | { |
| 527 | struct buffer *ib = fdtab[fd].cb[DIR_RD].b; |
| 528 | struct buffer *ob = fdtab[fd].cb[DIR_WR].b; |
| 529 | |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 530 | 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] | 531 | now_ms, __FUNCTION__, |
| 532 | fd, fdtab[fd].owner, |
| 533 | ib, ob, |
| 534 | ib->rex, ob->wex, |
| 535 | ib->flags, ob->flags, |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 536 | ib->l, ob->l, ob->cons->state); |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 537 | |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 538 | /* Check if we need to close the read side */ |
| 539 | if (!(ib->flags & BF_SHUTR)) { |
| 540 | /* Last read, forced read-shutdown, or other end closed */ |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 541 | if (ib->flags & (BF_SHUTR_NOW|BF_SHUTW)) { |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 542 | //trace_term(t, TT_HTTP_SRV_10); |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 543 | buffer_shutr(ib); |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 544 | if (ob->flags & BF_SHUTW) { |
| 545 | fd_delete(fd); |
| 546 | ob->cons->state = SI_ST_CLO; |
| 547 | return 0; |
| 548 | } |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 549 | EV_FD_CLR(fd, DIR_RD); |
| 550 | } |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | /* Check if we need to close the write side */ |
| 554 | if (!(ob->flags & BF_SHUTW)) { |
| 555 | /* Forced write-shutdown or other end closed with empty buffer. */ |
| 556 | if ((ob->flags & BF_SHUTW_NOW) || |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 557 | (ob->flags & (BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) == (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR)) { |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 558 | //trace_term(t, TT_HTTP_SRV_11); |
| 559 | buffer_shutw(ob); |
| 560 | if (ib->flags & BF_SHUTR) { |
| 561 | fd_delete(fd); |
| 562 | ob->cons->state = SI_ST_CLO; |
| 563 | return 0; |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 564 | } |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 565 | EV_FD_CLR(fd, DIR_WR); |
| 566 | shutdown(fd, SHUT_WR); |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 567 | } |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 568 | } |
| 569 | return 0; /* other cases change nothing */ |
| 570 | } |
| 571 | |
| 572 | |
| 573 | /* |
| 574 | * Updates a connected stream_sock file descriptor status and timeouts |
| 575 | * according to the buffers' flags. It should only be called once after the |
| 576 | * buffer flags have settled down, and before they are cleared. It doesn't |
| 577 | * harm to call it as often as desired (it just slightly hurts performance). |
| 578 | */ |
| 579 | int stream_sock_data_finish(int fd) |
| 580 | { |
| 581 | struct buffer *ib = fdtab[fd].cb[DIR_RD].b; |
| 582 | struct buffer *ob = fdtab[fd].cb[DIR_WR].b; |
| 583 | |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 584 | 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] | 585 | now_ms, __FUNCTION__, |
| 586 | fd, fdtab[fd].owner, |
| 587 | ib, ob, |
| 588 | ib->rex, ob->wex, |
| 589 | ib->flags, ob->flags, |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 590 | ib->l, ob->l, ob->cons->state); |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 591 | |
| 592 | /* Check if we need to close the read side */ |
| 593 | if (!(ib->flags & BF_SHUTR)) { |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 594 | /* Read not closed, update FD status and timeout for reads */ |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 595 | if (ib->flags & (BF_FULL|BF_HIJACK)) { |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 596 | /* stop reading */ |
| 597 | EV_FD_COND_C(fd, DIR_RD); |
| 598 | ib->rex = TICK_ETERNITY; |
| 599 | } |
| 600 | else { |
| 601 | /* (re)start reading and update timeout. Note: we don't recompute the timeout |
| 602 | * everytime we get here, otherwise it would risk never to expire. We only |
| 603 | * update it if is was not yet set, or if we already got some read status. |
| 604 | */ |
| 605 | EV_FD_COND_S(fd, DIR_RD); |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 606 | if (!tick_isset(ib->rex) || ib->flags & BF_READ_ACTIVITY) |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 607 | ib->rex = tick_add_ifset(now_ms, ib->rto); |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | /* Check if we need to close the write side */ |
| 612 | if (!(ob->flags & BF_SHUTW)) { |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 613 | /* Write not closed, update FD status and timeout for writes */ |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 614 | if ((ob->flags & BF_EMPTY) || |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 615 | (ob->flags & (BF_HIJACK|BF_WRITE_ENA)) == 0) { |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 616 | /* stop writing */ |
| 617 | EV_FD_COND_C(fd, DIR_WR); |
| 618 | ob->wex = TICK_ETERNITY; |
| 619 | } |
| 620 | else { |
| 621 | /* (re)start writing and update timeout. Note: we don't recompute the timeout |
| 622 | * everytime we get here, otherwise it would risk never to expire. We only |
| 623 | * update it if is was not yet set, or if we already got some write status. |
| 624 | */ |
| 625 | EV_FD_COND_S(fd, DIR_WR); |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 626 | if (!tick_isset(ob->wex) || ob->flags & BF_WRITE_ACTIVITY) { |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 627 | ob->wex = tick_add_ifset(now_ms, ob->wto); |
Willy Tarreau | 21e1be8 | 2008-08-29 11:30:14 +0200 | [diff] [blame] | 628 | if (tick_isset(ob->wex) && tick_isset(ib->rex)) { |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 629 | /* Note: depending on the protocol, we don't know if we're waiting |
| 630 | * for incoming data or not. So in order to prevent the socket from |
| 631 | * expiring read timeouts during writes, we refresh the read timeout, |
| 632 | * except if it was already infinite. |
| 633 | */ |
| 634 | ib->rex = ob->wex; |
| 635 | } |
| 636 | } |
| 637 | } |
| 638 | } |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 639 | return 0; |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 640 | } |
| 641 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 642 | |
| 643 | /* |
| 644 | * Local variables: |
| 645 | * c-indent-level: 8 |
| 646 | * c-basic-offset: 8 |
| 647 | * End: |
| 648 | */ |