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 | 5446940 | 2006-07-29 16:59:06 +0200 | [diff] [blame] | 44 | struct buffer *b = fdtab[fd].cb[DIR_RD].b; |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame^] | 45 | struct stream_interface *si = fdtab[fd].owner; |
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); |
| 254 | /* Maybe we have to completely close the socket */ |
| 255 | if (fdtab[fd].cb[DIR_WR].b->flags & BF_SHUTW) |
| 256 | goto do_close_and_return; |
| 257 | EV_FD_CLR(fd, DIR_RD); |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 258 | goto out_wakeup; |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 259 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 260 | out_error: |
| 261 | /* There was an error. we must wakeup the task. No need to clear |
| 262 | * the events, the task will do it. |
| 263 | */ |
| 264 | fdtab[fd].state = FD_STERROR; |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 265 | fdtab[fd].ev &= ~FD_POLL_STICKY; |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 266 | b->rex = TICK_ETERNITY; |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame^] | 267 | |
| 268 | /* Read error on the file descriptor. We close the FD and set |
| 269 | * the error on both buffers. |
| 270 | * Note: right now we only support connected sockets. |
| 271 | */ |
| 272 | if (si->state != SI_ST_EST) |
| 273 | goto out_wakeup; |
| 274 | |
| 275 | if (!si->err_type) |
| 276 | si->err_type = SI_ET_DATA_ERR; |
| 277 | |
| 278 | buffer_shutr(fdtab[fd].cb[DIR_RD].b); |
| 279 | fdtab[fd].cb[DIR_RD].b->flags |= BF_READ_ERROR; |
| 280 | buffer_shutw(fdtab[fd].cb[DIR_WR].b); |
| 281 | fdtab[fd].cb[DIR_WR].b->flags |= BF_WRITE_ERROR; |
| 282 | |
| 283 | do_close_and_return: |
| 284 | fd_delete(fd); |
| 285 | si->state = SI_ST_CLO; |
| 286 | task_wakeup(si->owner, TASK_WOKEN_IO); |
| 287 | return 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | |
| 291 | /* |
Willy Tarreau | f8306d5 | 2006-07-29 19:01:31 +0200 | [diff] [blame] | 292 | * this function is called on a write event from a stream socket. |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 293 | * It returns 0 if we have a high confidence that we will not be |
| 294 | * able to write more data without polling first. Returns non-zero |
| 295 | * otherwise. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 296 | */ |
Willy Tarreau | f8306d5 | 2006-07-29 19:01:31 +0200 | [diff] [blame] | 297 | int stream_sock_write(int fd) { |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 298 | __label__ out_wakeup, out_error; |
Willy Tarreau | 5446940 | 2006-07-29 16:59:06 +0200 | [diff] [blame] | 299 | struct buffer *b = fdtab[fd].cb[DIR_WR].b; |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame^] | 300 | struct stream_interface *si = fdtab[fd].owner; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 301 | int ret, max, retval; |
| 302 | int write_poll = MAX_WRITE_POLL_LOOPS; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 303 | |
| 304 | #ifdef DEBUG_FULL |
Willy Tarreau | f8306d5 | 2006-07-29 19:01:31 +0200 | [diff] [blame] | 305 | 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] | 306 | #endif |
| 307 | |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 308 | retval = 1; |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 309 | if (fdtab[fd].state == FD_STERROR || (fdtab[fd].ev & FD_POLL_ERR)) |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 310 | goto out_error; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 311 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 312 | while (1) { |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 313 | if (b->l == 0) { /* let's realign the buffer to optimize I/O */ |
| 314 | b->r = b->w = b->lr = b->data; |
| 315 | max = 0; |
| 316 | } |
| 317 | else if (b->r > b->w) { |
| 318 | max = b->r - b->w; |
| 319 | } |
| 320 | else { |
| 321 | max = b->data + BUFSIZE - b->w; |
| 322 | } |
| 323 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 324 | if (max == 0) { |
Willy Tarreau | f8306d5 | 2006-07-29 19:01:31 +0200 | [diff] [blame] | 325 | /* 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] | 326 | if (likely(fdtab[fd].state == FD_STCONN)) { |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 327 | /* We have no data to send to check the connection, and |
| 328 | * getsockopt() will not inform us whether the connection |
| 329 | * is still pending. So we'll reuse connect() to check the |
| 330 | * state of the socket. This has the advantage of givig us |
| 331 | * the following info : |
| 332 | * - error |
| 333 | * - connecting (EALREADY, EINPROGRESS) |
| 334 | * - connected (EISCONN, 0) |
| 335 | */ |
Willy Tarreau | e94ebd0 | 2007-10-09 17:14:37 +0200 | [diff] [blame] | 336 | if ((connect(fd, fdtab[fd].peeraddr, fdtab[fd].peerlen) == 0)) |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 337 | errno = 0; |
| 338 | |
| 339 | if (errno == EALREADY || errno == EINPROGRESS) { |
| 340 | retval = 0; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 341 | goto out_may_wakeup; |
Willy Tarreau | f8306d5 | 2006-07-29 19:01:31 +0200 | [diff] [blame] | 342 | } |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 343 | |
| 344 | if (errno && errno != EISCONN) |
| 345 | goto out_error; |
| 346 | |
| 347 | /* OK we just need to indicate that we got a connection |
| 348 | * and that we wrote nothing. |
| 349 | */ |
| 350 | b->flags |= BF_WRITE_NULL; |
| 351 | fdtab[fd].state = FD_STREADY; |
Willy Tarreau | f8306d5 | 2006-07-29 19:01:31 +0200 | [diff] [blame] | 352 | } |
| 353 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 354 | /* Funny, we were called to write something but there wasn't |
| 355 | * anything. Theorically we cannot get there, but just in case, |
| 356 | * let's disable the write event and pretend we never came there. |
| 357 | */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 358 | EV_FD_CLR(fd, DIR_WR); |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 359 | b->wex = TICK_ETERNITY; |
| 360 | goto out_wakeup; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | #ifndef MSG_NOSIGNAL |
| 364 | { |
| 365 | int skerr; |
| 366 | socklen_t lskerr = sizeof(skerr); |
| 367 | |
Willy Tarreau | c642348 | 2006-10-15 14:59:03 +0200 | [diff] [blame] | 368 | ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr); |
| 369 | if (ret == -1 || skerr) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 370 | ret = -1; |
| 371 | else |
| 372 | ret = send(fd, b->w, max, MSG_DONTWAIT); |
| 373 | } |
| 374 | #else |
| 375 | ret = send(fd, b->w, max, MSG_DONTWAIT | MSG_NOSIGNAL); |
| 376 | #endif |
| 377 | |
| 378 | if (ret > 0) { |
| 379 | b->l -= ret; |
| 380 | b->w += ret; |
| 381 | |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 382 | b->flags |= BF_WRITE_PARTIAL; |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 383 | |
| 384 | if (b->l < b->rlim - b->data) |
| 385 | b->flags &= ~BF_FULL; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 386 | |
| 387 | if (b->w == b->data + BUFSIZE) { |
| 388 | b->w = b->data; /* wrap around the buffer */ |
| 389 | } |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 390 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 391 | if (!b->l) { |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 392 | b->flags |= BF_EMPTY; |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 393 | EV_FD_CLR(fd, DIR_WR); |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 394 | b->wex = TICK_ETERNITY; |
| 395 | goto out_wakeup; |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 396 | } |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 397 | |
Willy Tarreau | ab3e1d3 | 2007-06-03 14:10:36 +0200 | [diff] [blame] | 398 | /* if the system buffer is full, don't insist */ |
| 399 | if (ret < max) |
| 400 | break; |
| 401 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 402 | if (--write_poll <= 0) |
| 403 | break; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 404 | } |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 405 | else if (ret == 0 || errno == EAGAIN) { |
| 406 | /* nothing written, just pretend we were never called |
| 407 | * and wait for the socket to be ready. But we may have |
| 408 | * done some work justifying to notify the task. |
| 409 | */ |
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 | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 414 | goto out_error; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 415 | } |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 416 | } /* while (1) */ |
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 | /* |
| 419 | * The only way to get out of this loop is to have stopped writing |
| 420 | * without any error, either by limiting the number of loops, or |
| 421 | * because of an EAGAIN. We only rearm the timer if we have at least |
| 422 | * written something. |
| 423 | */ |
| 424 | |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 425 | if (tick_isset(b->wex) && b->flags & BF_WRITE_PARTIAL) { |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 426 | b->wex = tick_add_ifset(now_ms, b->wto); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 427 | if (tick_isset(b->wex)) { |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 428 | /* FIXME: to prevent the client from expiring read timeouts during writes, |
| 429 | * we refresh it. A solution would be to merge read+write timeouts into a |
| 430 | * unique one, although that needs some study particularly on full-duplex |
| 431 | * TCP connections. */ |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 432 | if (tick_isset(b->rex) && !(b->flags & BF_SHUTR)) |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 433 | b->rex = b->wex; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 434 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 435 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 436 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 437 | out_may_wakeup: |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 438 | if (!(b->flags & BF_WRITE_ACTIVITY)) |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 439 | goto out_skip_wakeup; |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 440 | out_wakeup: |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame^] | 441 | task_wakeup(si->owner, TASK_WOKEN_IO); |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 442 | |
| 443 | out_skip_wakeup: |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 444 | fdtab[fd].ev &= ~FD_POLL_OUT; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 445 | return retval; |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 446 | |
| 447 | out_error: |
| 448 | /* There was an error. we must wakeup the task. No need to clear |
| 449 | * the events, the task will do it. |
| 450 | */ |
| 451 | fdtab[fd].state = FD_STERROR; |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 452 | fdtab[fd].ev &= ~FD_POLL_STICKY; |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 453 | b->wex = TICK_ETERNITY; |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame^] | 454 | /* Read error on the file descriptor. We close the FD and set |
| 455 | * the error on both buffers. |
| 456 | * Note: right now we only support connected sockets. |
| 457 | */ |
| 458 | if (si->state != SI_ST_EST) |
| 459 | goto out_wakeup; |
| 460 | |
| 461 | if (!si->err_type) |
| 462 | si->err_type = SI_ET_DATA_ERR; |
| 463 | |
| 464 | buffer_shutr(fdtab[fd].cb[DIR_RD].b); |
| 465 | fdtab[fd].cb[DIR_RD].b->flags |= BF_READ_ERROR; |
| 466 | buffer_shutw(fdtab[fd].cb[DIR_WR].b); |
| 467 | fdtab[fd].cb[DIR_WR].b->flags |= BF_WRITE_ERROR; |
| 468 | |
| 469 | fd_delete(fd); |
| 470 | si->state = SI_ST_CLO; |
| 471 | task_wakeup(si->owner, TASK_WOKEN_IO); |
| 472 | return 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 473 | } |
| 474 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 475 | |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 476 | /* |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 477 | * This function only has to be called once after a wakeup event during a data |
| 478 | * phase. It controls the file descriptor's status, as well as read and write |
| 479 | * timeouts. |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 480 | */ |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame^] | 481 | int stream_sock_data_check_timeouts(int fd) |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 482 | { |
| 483 | struct buffer *ib = fdtab[fd].cb[DIR_RD].b; |
| 484 | struct buffer *ob = fdtab[fd].cb[DIR_WR].b; |
| 485 | |
| 486 | 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\n", |
| 487 | now_ms, __FUNCTION__, |
| 488 | fd, fdtab[fd].owner, |
| 489 | ib, ob, |
| 490 | ib->rex, ob->wex, |
| 491 | ib->flags, ob->flags, |
| 492 | ib->l, ob->l); |
| 493 | |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 494 | /* Read timeout */ |
| 495 | if (unlikely(!(ib->flags & (BF_SHUTR|BF_READ_TIMEOUT)) && tick_is_expired(ib->rex, now_ms))) { |
| 496 | //trace_term(t, TT_HTTP_SRV_12); |
| 497 | ib->flags |= BF_READ_TIMEOUT; |
| 498 | if (!ob->cons->err_type) { |
| 499 | //ob->cons->err_loc = t->srv; |
| 500 | ob->cons->err_type = SI_ET_DATA_TO; |
| 501 | } |
| 502 | buffer_shutr(ib); |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame^] | 503 | if (ob->flags & BF_SHUTW) { |
| 504 | do_close_and_return: |
| 505 | fd_delete(fd); |
| 506 | ob->cons->state = SI_ST_CLO; |
| 507 | return 0; |
| 508 | } |
| 509 | |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 510 | EV_FD_CLR(fd, DIR_RD); |
| 511 | } |
| 512 | |
| 513 | /* Write timeout */ |
| 514 | if (unlikely(!(ob->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) && tick_is_expired(ob->wex, now_ms))) { |
| 515 | //trace_term(t, TT_HTTP_SRV_13); |
| 516 | ob->flags |= BF_WRITE_TIMEOUT; |
| 517 | if (!ob->cons->err_type) { |
| 518 | //ob->cons->err_loc = t->srv; |
| 519 | ob->cons->err_type = SI_ET_DATA_TO; |
| 520 | } |
| 521 | buffer_shutw(ob); |
| 522 | if (ib->flags & BF_SHUTR) |
| 523 | goto do_close_and_return; |
| 524 | |
| 525 | EV_FD_CLR(fd, DIR_WR); |
| 526 | shutdown(fd, SHUT_WR); |
| 527 | } |
| 528 | return 0; |
| 529 | } |
| 530 | |
| 531 | /* |
| 532 | * Manages a stream_sock connection during its data phase. The buffers are |
| 533 | * examined for various cases of shutdown, then file descriptor and buffers' |
| 534 | * flags are updated accordingly. |
| 535 | */ |
| 536 | int stream_sock_data_update(int fd) |
| 537 | { |
| 538 | struct buffer *ib = fdtab[fd].cb[DIR_RD].b; |
| 539 | struct buffer *ob = fdtab[fd].cb[DIR_WR].b; |
| 540 | |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame^] | 541 | 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] | 542 | now_ms, __FUNCTION__, |
| 543 | fd, fdtab[fd].owner, |
| 544 | ib, ob, |
| 545 | ib->rex, ob->wex, |
| 546 | ib->flags, ob->flags, |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame^] | 547 | ib->l, ob->l, ob->cons->state); |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 548 | |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 549 | /* Check if we need to close the read side */ |
| 550 | if (!(ib->flags & BF_SHUTR)) { |
| 551 | /* Last read, forced read-shutdown, or other end closed */ |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame^] | 552 | if (ib->flags & (BF_SHUTR_NOW|BF_SHUTW)) { |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 553 | //trace_term(t, TT_HTTP_SRV_10); |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 554 | buffer_shutr(ib); |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 555 | if (ob->flags & BF_SHUTW) { |
| 556 | fd_delete(fd); |
| 557 | ob->cons->state = SI_ST_CLO; |
| 558 | return 0; |
| 559 | } |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 560 | EV_FD_CLR(fd, DIR_RD); |
| 561 | } |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | /* Check if we need to close the write side */ |
| 565 | if (!(ob->flags & BF_SHUTW)) { |
| 566 | /* Forced write-shutdown or other end closed with empty buffer. */ |
| 567 | if ((ob->flags & BF_SHUTW_NOW) || |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 568 | (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] | 569 | //trace_term(t, TT_HTTP_SRV_11); |
| 570 | buffer_shutw(ob); |
| 571 | if (ib->flags & BF_SHUTR) { |
| 572 | fd_delete(fd); |
| 573 | ob->cons->state = SI_ST_CLO; |
| 574 | return 0; |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 575 | } |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 576 | EV_FD_CLR(fd, DIR_WR); |
| 577 | shutdown(fd, SHUT_WR); |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 578 | } |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 579 | } |
| 580 | return 0; /* other cases change nothing */ |
| 581 | } |
| 582 | |
| 583 | |
| 584 | /* |
| 585 | * Updates a connected stream_sock file descriptor status and timeouts |
| 586 | * according to the buffers' flags. It should only be called once after the |
| 587 | * buffer flags have settled down, and before they are cleared. It doesn't |
| 588 | * harm to call it as often as desired (it just slightly hurts performance). |
| 589 | */ |
| 590 | int stream_sock_data_finish(int fd) |
| 591 | { |
| 592 | struct buffer *ib = fdtab[fd].cb[DIR_RD].b; |
| 593 | struct buffer *ob = fdtab[fd].cb[DIR_WR].b; |
| 594 | |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame^] | 595 | 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] | 596 | now_ms, __FUNCTION__, |
| 597 | fd, fdtab[fd].owner, |
| 598 | ib, ob, |
| 599 | ib->rex, ob->wex, |
| 600 | ib->flags, ob->flags, |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame^] | 601 | ib->l, ob->l, ob->cons->state); |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 602 | |
| 603 | /* Check if we need to close the read side */ |
| 604 | if (!(ib->flags & BF_SHUTR)) { |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 605 | /* Read not closed, update FD status and timeout for reads */ |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 606 | if (ib->flags & (BF_FULL|BF_HIJACK)) { |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 607 | /* stop reading */ |
| 608 | EV_FD_COND_C(fd, DIR_RD); |
| 609 | ib->rex = TICK_ETERNITY; |
| 610 | } |
| 611 | else { |
| 612 | /* (re)start reading and update timeout. Note: we don't recompute the timeout |
| 613 | * everytime we get here, otherwise it would risk never to expire. We only |
| 614 | * update it if is was not yet set, or if we already got some read status. |
| 615 | */ |
| 616 | EV_FD_COND_S(fd, DIR_RD); |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 617 | if (!tick_isset(ib->rex) || ib->flags & BF_READ_ACTIVITY) |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 618 | ib->rex = tick_add_ifset(now_ms, ib->rto); |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | /* Check if we need to close the write side */ |
| 623 | if (!(ob->flags & BF_SHUTW)) { |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 624 | /* Write not closed, update FD status and timeout for writes */ |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 625 | if ((ob->flags & BF_EMPTY) || |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 626 | (ob->flags & (BF_HIJACK|BF_WRITE_ENA)) == 0) { |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 627 | /* stop writing */ |
| 628 | EV_FD_COND_C(fd, DIR_WR); |
| 629 | ob->wex = TICK_ETERNITY; |
| 630 | } |
| 631 | else { |
| 632 | /* (re)start writing and update timeout. Note: we don't recompute the timeout |
| 633 | * everytime we get here, otherwise it would risk never to expire. We only |
| 634 | * update it if is was not yet set, or if we already got some write status. |
| 635 | */ |
| 636 | EV_FD_COND_S(fd, DIR_WR); |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 637 | if (!tick_isset(ob->wex) || ob->flags & BF_WRITE_ACTIVITY) { |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 638 | ob->wex = tick_add_ifset(now_ms, ob->wto); |
Willy Tarreau | 21e1be8 | 2008-08-29 11:30:14 +0200 | [diff] [blame] | 639 | if (tick_isset(ob->wex) && tick_isset(ib->rex)) { |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 640 | /* Note: depending on the protocol, we don't know if we're waiting |
| 641 | * for incoming data or not. So in order to prevent the socket from |
| 642 | * expiring read timeouts during writes, we refresh the read timeout, |
| 643 | * except if it was already infinite. |
| 644 | */ |
| 645 | ib->rex = ob->wex; |
| 646 | } |
| 647 | } |
| 648 | } |
| 649 | } |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 650 | return 0; |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 651 | } |
| 652 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 653 | |
| 654 | /* |
| 655 | * Local variables: |
| 656 | * c-indent-level: 8 |
| 657 | * c-basic-offset: 8 |
| 658 | * End: |
| 659 | */ |