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 | 74ab2ac | 2008-11-23 17:23:07 +0100 | [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 | b38903c | 2008-11-23 21:33:29 +0100 | [diff] [blame] | 116 | |
| 117 | if (fdtab[fd].state == FD_STCONN) |
| 118 | fdtab[fd].state = FD_STREADY; |
| 119 | |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 120 | b->flags |= BF_READ_PARTIAL; |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 121 | b->flags &= ~BF_EMPTY; |
Willy Tarreau | 74ab2ac | 2008-11-23 17:23:07 +0100 | [diff] [blame] | 122 | |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 123 | if (b->r == b->data + BUFSIZE) { |
| 124 | b->r = b->data; /* wrap around the buffer */ |
| 125 | } |
Willy Tarreau | 9641e8f | 2007-03-23 23:02:09 +0100 | [diff] [blame] | 126 | |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 127 | b->total += ret; |
Willy Tarreau | 9641e8f | 2007-03-23 23:02:09 +0100 | [diff] [blame] | 128 | |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 129 | if (b->l >= b->rlim - b->data) { |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 130 | /* The buffer is now full, there's no point in going through |
| 131 | * the loop again. |
| 132 | */ |
Willy Tarreau | 8a7af60 | 2008-05-03 23:07:14 +0200 | [diff] [blame] | 133 | if (!(b->flags & BF_STREAMER_FAST) && (cur_read == b->l)) { |
| 134 | b->xfer_small = 0; |
| 135 | b->xfer_large++; |
| 136 | if (b->xfer_large >= 3) { |
| 137 | /* we call this buffer a fast streamer if it manages |
| 138 | * to be filled in one call 3 consecutive times. |
| 139 | */ |
| 140 | b->flags |= (BF_STREAMER | BF_STREAMER_FAST); |
| 141 | //fputc('+', stderr); |
| 142 | } |
| 143 | } |
| 144 | else if ((b->flags & (BF_STREAMER | BF_STREAMER_FAST)) && |
| 145 | (cur_read <= BUFSIZE / 2)) { |
| 146 | b->xfer_large = 0; |
| 147 | b->xfer_small++; |
| 148 | if (b->xfer_small >= 2) { |
| 149 | /* if the buffer has been at least half full twice, |
| 150 | * we receive faster than we send, so at least it |
| 151 | * is not a "fast streamer". |
| 152 | */ |
| 153 | b->flags &= ~BF_STREAMER_FAST; |
| 154 | //fputc('-', stderr); |
| 155 | } |
| 156 | } |
| 157 | else { |
| 158 | b->xfer_small = 0; |
| 159 | b->xfer_large = 0; |
| 160 | } |
| 161 | |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 162 | b->flags |= BF_FULL; |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 163 | EV_FD_CLR(fd, DIR_RD); |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 164 | b->rex = TICK_ETERNITY; |
| 165 | goto out_wakeup; |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 166 | } |
| 167 | |
Willy Tarreau | ab3e1d3 | 2007-06-03 14:10:36 +0200 | [diff] [blame] | 168 | /* if too many bytes were missing from last read, it means that |
| 169 | * it's pointless trying to read again because the system does |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 170 | * not have them in buffers. BTW, if FD_POLL_HUP was present, |
| 171 | * it means that we have reached the end and that the connection |
| 172 | * is closed. |
Willy Tarreau | ab3e1d3 | 2007-06-03 14:10:36 +0200 | [diff] [blame] | 173 | */ |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 174 | if (ret < max) { |
Willy Tarreau | 8a7af60 | 2008-05-03 23:07:14 +0200 | [diff] [blame] | 175 | if ((b->flags & (BF_STREAMER | BF_STREAMER_FAST)) && |
| 176 | (cur_read <= BUFSIZE / 2)) { |
| 177 | b->xfer_large = 0; |
| 178 | b->xfer_small++; |
| 179 | if (b->xfer_small >= 3) { |
| 180 | /* we have read less than half of the buffer in |
| 181 | * one pass, and this happened at least 3 times. |
| 182 | * This is definitely not a streamer. |
| 183 | */ |
| 184 | b->flags &= ~(BF_STREAMER | BF_STREAMER_FAST); |
| 185 | //fputc('!', stderr); |
| 186 | } |
| 187 | } |
Willy Tarreau | 2bea3a1 | 2008-08-28 09:47:43 +0200 | [diff] [blame] | 188 | /* unfortunately, on level-triggered events, POLL_HUP |
| 189 | * is generally delivered AFTER the system buffer is |
| 190 | * empty, so this one might never match. |
| 191 | */ |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 192 | if (fdtab[fd].ev & FD_POLL_HUP) |
| 193 | goto out_shutdown_r; |
Willy Tarreau | 2bea3a1 | 2008-08-28 09:47:43 +0200 | [diff] [blame] | 194 | |
| 195 | /* if a streamer has read few data, it may be because we |
| 196 | * have exhausted system buffers. It's not worth trying |
| 197 | * again. |
| 198 | */ |
| 199 | if (b->flags & BF_STREAMER) |
| 200 | break; |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 201 | } |
Willy Tarreau | ab3e1d3 | 2007-06-03 14:10:36 +0200 | [diff] [blame] | 202 | |
| 203 | /* generally if we read something smaller than 1 or 2 MSS, |
Willy Tarreau | 2bea3a1 | 2008-08-28 09:47:43 +0200 | [diff] [blame] | 204 | * it means that either we have exhausted the system's |
| 205 | * buffers (streamer or question-response protocol) or that |
| 206 | * the connection will be closed. Streamers are easily |
| 207 | * detected so we return early. For other cases, it's still |
| 208 | * better to perform a last read to be sure, because it may |
| 209 | * save one complete poll/read/wakeup cycle in case of shutdown. |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 210 | */ |
Willy Tarreau | 2bea3a1 | 2008-08-28 09:47:43 +0200 | [diff] [blame] | 211 | if (ret < MIN_RET_FOR_READ_LOOP && b->flags & BF_STREAMER) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 212 | break; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 213 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 214 | if (--read_poll <= 0) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 215 | break; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 216 | } |
| 217 | else if (ret == 0) { |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 218 | /* connection closed */ |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 219 | goto out_shutdown_r; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 220 | } |
Willy Tarreau | 9f19529 | 2007-04-15 21:26:58 +0200 | [diff] [blame] | 221 | else if (errno == EAGAIN) { |
| 222 | /* Ignore EAGAIN but inform the poller that there is |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 223 | * nothing to read left. But we may have done some work |
| 224 | * justifying to notify the task. |
Willy Tarreau | 9f19529 | 2007-04-15 21:26:58 +0200 | [diff] [blame] | 225 | */ |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 226 | retval = 0; |
| 227 | break; |
| 228 | } |
| 229 | else { |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 230 | goto out_error; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 231 | } |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 232 | } /* while (1) */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 233 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 234 | /* |
| 235 | * The only way to get out of this loop is to have stopped reading |
| 236 | * without any error nor close, either by limiting the number of |
| 237 | * loops, or because of an EAGAIN. We only rearm the timer if we |
| 238 | * have at least read something. |
| 239 | */ |
| 240 | |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 241 | if (tick_isset(b->rex) && b->flags & BF_READ_PARTIAL) |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 242 | b->rex = tick_add_ifset(now_ms, b->rto); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 243 | |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 244 | if (!(b->flags & BF_READ_ACTIVITY)) |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 245 | goto out_skip_wakeup; |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 246 | out_wakeup: |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 247 | task_wakeup(si->owner, TASK_WOKEN_IO); |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 248 | |
| 249 | out_skip_wakeup: |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 250 | fdtab[fd].ev &= ~FD_POLL_IN; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 251 | return retval; |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 252 | |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 253 | out_shutdown_r: |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 254 | /* we received a shutdown */ |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 255 | fdtab[fd].ev &= ~FD_POLL_HUP; |
| 256 | b->flags |= BF_READ_NULL; |
Willy Tarreau | 99126c3 | 2008-11-27 10:30:51 +0100 | [diff] [blame] | 257 | stream_sock_shutr(si); |
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: |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 261 | /* Read error on the file descriptor. We mark the FD as STERROR so |
| 262 | * that we don't use it anymore. The error is reported to the stream |
| 263 | * interface which will take proper action. We must not perturbate the |
| 264 | * buffer because the stream interface wants to ensure transparent |
| 265 | * connection retries. |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 266 | */ |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 267 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 268 | fdtab[fd].state = FD_STERROR; |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 269 | fdtab[fd].ev &= ~FD_POLL_STICKY; |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 270 | si->flags |= SI_FL_ERR; |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 271 | task_wakeup(si->owner, TASK_WOKEN_IO); |
| 272 | return 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | |
| 276 | /* |
Willy Tarreau | f8306d5 | 2006-07-29 19:01:31 +0200 | [diff] [blame] | 277 | * this function is called on a write event from a stream socket. |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 278 | * It returns 0 if we have a high confidence that we will not be |
| 279 | * able to write more data without polling first. Returns non-zero |
| 280 | * otherwise. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 281 | */ |
Willy Tarreau | f8306d5 | 2006-07-29 19:01:31 +0200 | [diff] [blame] | 282 | int stream_sock_write(int fd) { |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 283 | __label__ out_wakeup, out_error; |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 284 | struct stream_interface *si = fdtab[fd].owner; |
Willy Tarreau | 48adac5 | 2008-08-30 04:58:38 +0200 | [diff] [blame] | 285 | struct buffer *b = si->ob; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 286 | int ret, max, retval; |
| 287 | int write_poll = MAX_WRITE_POLL_LOOPS; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 288 | |
| 289 | #ifdef DEBUG_FULL |
Willy Tarreau | f8306d5 | 2006-07-29 19:01:31 +0200 | [diff] [blame] | 290 | 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] | 291 | #endif |
| 292 | |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 293 | retval = 1; |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 294 | if (fdtab[fd].state == FD_STERROR || (fdtab[fd].ev & FD_POLL_ERR)) |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 295 | goto out_error; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 296 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 297 | while (1) { |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 298 | if (b->l == 0) { /* let's realign the buffer to optimize I/O */ |
| 299 | b->r = b->w = b->lr = b->data; |
| 300 | max = 0; |
| 301 | } |
| 302 | else if (b->r > b->w) { |
| 303 | max = b->r - b->w; |
| 304 | } |
| 305 | else { |
| 306 | max = b->data + BUFSIZE - b->w; |
| 307 | } |
| 308 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 309 | if (max == 0) { |
Willy Tarreau | f8306d5 | 2006-07-29 19:01:31 +0200 | [diff] [blame] | 310 | /* 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] | 311 | if (likely(fdtab[fd].state == FD_STCONN)) { |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 312 | /* We have no data to send to check the connection, and |
| 313 | * getsockopt() will not inform us whether the connection |
| 314 | * is still pending. So we'll reuse connect() to check the |
| 315 | * state of the socket. This has the advantage of givig us |
| 316 | * the following info : |
| 317 | * - error |
| 318 | * - connecting (EALREADY, EINPROGRESS) |
| 319 | * - connected (EISCONN, 0) |
| 320 | */ |
Willy Tarreau | e94ebd0 | 2007-10-09 17:14:37 +0200 | [diff] [blame] | 321 | if ((connect(fd, fdtab[fd].peeraddr, fdtab[fd].peerlen) == 0)) |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 322 | errno = 0; |
| 323 | |
| 324 | if (errno == EALREADY || errno == EINPROGRESS) { |
| 325 | retval = 0; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 326 | goto out_may_wakeup; |
Willy Tarreau | f8306d5 | 2006-07-29 19:01:31 +0200 | [diff] [blame] | 327 | } |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 328 | |
| 329 | if (errno && errno != EISCONN) |
| 330 | goto out_error; |
| 331 | |
| 332 | /* OK we just need to indicate that we got a connection |
| 333 | * and that we wrote nothing. |
| 334 | */ |
| 335 | b->flags |= BF_WRITE_NULL; |
| 336 | fdtab[fd].state = FD_STREADY; |
Willy Tarreau | f8306d5 | 2006-07-29 19:01:31 +0200 | [diff] [blame] | 337 | } |
| 338 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 339 | /* Funny, we were called to write something but there wasn't |
| 340 | * anything. Theorically we cannot get there, but just in case, |
| 341 | * let's disable the write event and pretend we never came there. |
| 342 | */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 343 | EV_FD_CLR(fd, DIR_WR); |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 344 | b->wex = TICK_ETERNITY; |
| 345 | goto out_wakeup; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | #ifndef MSG_NOSIGNAL |
| 349 | { |
| 350 | int skerr; |
| 351 | socklen_t lskerr = sizeof(skerr); |
| 352 | |
Willy Tarreau | c642348 | 2006-10-15 14:59:03 +0200 | [diff] [blame] | 353 | ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr); |
| 354 | if (ret == -1 || skerr) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 355 | ret = -1; |
| 356 | else |
| 357 | ret = send(fd, b->w, max, MSG_DONTWAIT); |
| 358 | } |
| 359 | #else |
| 360 | ret = send(fd, b->w, max, MSG_DONTWAIT | MSG_NOSIGNAL); |
| 361 | #endif |
| 362 | |
| 363 | if (ret > 0) { |
| 364 | b->l -= ret; |
| 365 | b->w += ret; |
Willy Tarreau | 74ab2ac | 2008-11-23 17:23:07 +0100 | [diff] [blame] | 366 | |
Willy Tarreau | b38903c | 2008-11-23 21:33:29 +0100 | [diff] [blame] | 367 | if (fdtab[fd].state == FD_STCONN) |
| 368 | fdtab[fd].state = FD_STREADY; |
| 369 | |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 370 | b->flags |= BF_WRITE_PARTIAL; |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 371 | |
| 372 | if (b->l < b->rlim - b->data) |
| 373 | b->flags &= ~BF_FULL; |
Willy Tarreau | 74ab2ac | 2008-11-23 17:23:07 +0100 | [diff] [blame] | 374 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 375 | if (b->w == b->data + BUFSIZE) { |
| 376 | b->w = b->data; /* wrap around the buffer */ |
| 377 | } |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 378 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 379 | if (!b->l) { |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 380 | b->flags |= BF_EMPTY; |
Willy Tarreau | 48adac5 | 2008-08-30 04:58:38 +0200 | [diff] [blame] | 381 | |
| 382 | /* Maybe we just wrote the last chunk and need to close ? */ |
| 383 | if ((b->flags & (BF_SHUTW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) == (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR)) { |
| 384 | if (si->state == SI_ST_EST) { |
Willy Tarreau | 99126c3 | 2008-11-27 10:30:51 +0100 | [diff] [blame] | 385 | stream_sock_shutw(si); |
| 386 | b->wex = TICK_ETERNITY; |
| 387 | goto out_wakeup; |
Willy Tarreau | 48adac5 | 2008-08-30 04:58:38 +0200 | [diff] [blame] | 388 | } |
| 389 | } |
| 390 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 391 | EV_FD_CLR(fd, DIR_WR); |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 392 | b->wex = TICK_ETERNITY; |
| 393 | goto out_wakeup; |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 394 | } |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 395 | |
Willy Tarreau | ab3e1d3 | 2007-06-03 14:10:36 +0200 | [diff] [blame] | 396 | /* if the system buffer is full, don't insist */ |
| 397 | if (ret < max) |
| 398 | break; |
| 399 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 400 | if (--write_poll <= 0) |
| 401 | break; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 402 | } |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 403 | else if (ret == 0 || errno == EAGAIN) { |
| 404 | /* nothing written, just pretend we were never called |
| 405 | * and wait for the socket to be ready. But we may have |
| 406 | * done some work justifying to notify the task. |
| 407 | */ |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 408 | retval = 0; |
| 409 | break; |
| 410 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 411 | else { |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 412 | goto out_error; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 413 | } |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 414 | } /* while (1) */ |
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 | /* |
| 417 | * The only way to get out of this loop is to have stopped writing |
| 418 | * without any error, either by limiting the number of loops, or |
| 419 | * because of an EAGAIN. We only rearm the timer if we have at least |
| 420 | * written something. |
| 421 | */ |
| 422 | |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 423 | if (tick_isset(b->wex) && b->flags & BF_WRITE_PARTIAL) { |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 424 | b->wex = tick_add_ifset(now_ms, b->wto); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 425 | if (tick_isset(b->wex)) { |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 426 | /* FIXME: to prevent the client from expiring read timeouts during writes, |
| 427 | * we refresh it. A solution would be to merge read+write timeouts into a |
| 428 | * unique one, although that needs some study particularly on full-duplex |
| 429 | * TCP connections. */ |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 430 | if (tick_isset(b->rex) && !(b->flags & BF_SHUTR)) |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 431 | b->rex = b->wex; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 432 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 433 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 434 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 435 | out_may_wakeup: |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 436 | if (!(b->flags & BF_WRITE_ACTIVITY)) |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 437 | goto out_skip_wakeup; |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 438 | out_wakeup: |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 439 | task_wakeup(si->owner, TASK_WOKEN_IO); |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 440 | |
| 441 | out_skip_wakeup: |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 442 | fdtab[fd].ev &= ~FD_POLL_OUT; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 443 | return retval; |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 444 | |
| 445 | out_error: |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 446 | /* Write error on the file descriptor. We mark the FD as STERROR so |
| 447 | * that we don't use it anymore. The error is reported to the stream |
| 448 | * interface which will take proper action. We must not perturbate the |
| 449 | * buffer because the stream interface wants to ensure transparent |
| 450 | * connection retries. |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 451 | */ |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 452 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 453 | fdtab[fd].state = FD_STERROR; |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 454 | fdtab[fd].ev &= ~FD_POLL_STICKY; |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 455 | si->flags |= SI_FL_ERR; |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 456 | task_wakeup(si->owner, TASK_WOKEN_IO); |
| 457 | return 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 458 | } |
| 459 | |
Willy Tarreau | 48adac5 | 2008-08-30 04:58:38 +0200 | [diff] [blame] | 460 | /* |
Willy Tarreau | 3c6ab2e | 2008-09-04 11:19:41 +0200 | [diff] [blame] | 461 | * This function performs a shutdown-write on a stream interface in a connected or |
| 462 | * 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] | 463 | * or closes the file descriptor and marks itself as closed. The buffer flags are |
| 464 | * updated to reflect the new state. |
Willy Tarreau | 48adac5 | 2008-08-30 04:58:38 +0200 | [diff] [blame] | 465 | */ |
Willy Tarreau | 0a5d5dd | 2008-11-23 19:31:35 +0100 | [diff] [blame] | 466 | void stream_sock_shutw(struct stream_interface *si) |
Willy Tarreau | 48adac5 | 2008-08-30 04:58:38 +0200 | [diff] [blame] | 467 | { |
Willy Tarreau | 99126c3 | 2008-11-27 10:30:51 +0100 | [diff] [blame] | 468 | if (si->ob->flags & BF_SHUTW) |
| 469 | return; |
| 470 | si->ob->flags |= BF_SHUTW; |
| 471 | si->ob->wex = TICK_ETERNITY; |
| 472 | |
Willy Tarreau | b38903c | 2008-11-23 21:33:29 +0100 | [diff] [blame] | 473 | switch (si->state) { |
Willy Tarreau | b38903c | 2008-11-23 21:33:29 +0100 | [diff] [blame] | 474 | case SI_ST_EST: |
| 475 | if (!(si->ib->flags & BF_SHUTR)) { |
| 476 | EV_FD_CLR(si->fd, DIR_WR); |
| 477 | shutdown(si->fd, SHUT_WR); |
| 478 | return; |
| 479 | } |
| 480 | /* fall through */ |
| 481 | case SI_ST_CON: |
Willy Tarreau | 8bfa426 | 2008-11-27 09:25:45 +0100 | [diff] [blame] | 482 | /* we may have to close a pending connection, and mark the |
| 483 | * response buffer as shutr |
| 484 | */ |
Willy Tarreau | 48adac5 | 2008-08-30 04:58:38 +0200 | [diff] [blame] | 485 | fd_delete(si->fd); |
Willy Tarreau | fe3718a | 2008-11-30 18:14:12 +0100 | [diff] [blame] | 486 | /* fall through */ |
| 487 | case SI_ST_CER: |
Willy Tarreau | 7f00651 | 2008-12-07 14:04:04 +0100 | [diff] [blame] | 488 | si->state = SI_ST_DIS; |
| 489 | default: |
Willy Tarreau | 99126c3 | 2008-11-27 10:30:51 +0100 | [diff] [blame] | 490 | si->ib->flags |= BF_SHUTR; |
Willy Tarreau | fe3718a | 2008-11-30 18:14:12 +0100 | [diff] [blame] | 491 | si->ib->rex = TICK_ETERNITY; |
Willy Tarreau | 0a5d5dd | 2008-11-23 19:31:35 +0100 | [diff] [blame] | 492 | return; |
Willy Tarreau | 48adac5 | 2008-08-30 04:58:38 +0200 | [diff] [blame] | 493 | } |
Willy Tarreau | 48adac5 | 2008-08-30 04:58:38 +0200 | [diff] [blame] | 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 |
Willy Tarreau | 0a5d5dd | 2008-11-23 19:31:35 +0100 | [diff] [blame] | 498 | * 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] | 499 | * or closes the file descriptor and marks itself as closed. The buffer flags are |
| 500 | * updated to reflect the new state. |
Willy Tarreau | 3c6ab2e | 2008-09-04 11:19:41 +0200 | [diff] [blame] | 501 | */ |
Willy Tarreau | 0a5d5dd | 2008-11-23 19:31:35 +0100 | [diff] [blame] | 502 | void stream_sock_shutr(struct stream_interface *si) |
Willy Tarreau | 3c6ab2e | 2008-09-04 11:19:41 +0200 | [diff] [blame] | 503 | { |
Willy Tarreau | 99126c3 | 2008-11-27 10:30:51 +0100 | [diff] [blame] | 504 | if (si->ib->flags & BF_SHUTR) |
| 505 | return; |
| 506 | si->ib->flags |= BF_SHUTR; |
| 507 | si->ib->rex = TICK_ETERNITY; |
| 508 | |
Willy Tarreau | 8bfa426 | 2008-11-27 09:25:45 +0100 | [diff] [blame] | 509 | if (si->state != SI_ST_EST && si->state != SI_ST_CON) |
Willy Tarreau | 0a5d5dd | 2008-11-23 19:31:35 +0100 | [diff] [blame] | 510 | return; |
Willy Tarreau | 3c6ab2e | 2008-09-04 11:19:41 +0200 | [diff] [blame] | 511 | |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 512 | if (si->ob->flags & BF_SHUTW) { |
Willy Tarreau | 3c6ab2e | 2008-09-04 11:19:41 +0200 | [diff] [blame] | 513 | fd_delete(si->fd); |
Willy Tarreau | 74ab2ac | 2008-11-23 17:23:07 +0100 | [diff] [blame] | 514 | si->state = SI_ST_DIS; |
Willy Tarreau | 0a5d5dd | 2008-11-23 19:31:35 +0100 | [diff] [blame] | 515 | return; |
Willy Tarreau | 3c6ab2e | 2008-09-04 11:19:41 +0200 | [diff] [blame] | 516 | } |
| 517 | EV_FD_CLR(si->fd, DIR_RD); |
Willy Tarreau | 0a5d5dd | 2008-11-23 19:31:35 +0100 | [diff] [blame] | 518 | return; |
Willy Tarreau | 3c6ab2e | 2008-09-04 11:19:41 +0200 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | /* |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 522 | * Updates a connected stream_sock file descriptor status and timeouts |
| 523 | * according to the buffers' flags. It should only be called once after the |
| 524 | * buffer flags have settled down, and before they are cleared. It doesn't |
| 525 | * harm to call it as often as desired (it just slightly hurts performance). |
| 526 | */ |
Willy Tarreau | b025325 | 2008-11-30 21:37:12 +0100 | [diff] [blame] | 527 | void stream_sock_data_finish(struct stream_interface *si) |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 528 | { |
Willy Tarreau | b025325 | 2008-11-30 21:37:12 +0100 | [diff] [blame] | 529 | struct buffer *ib = si->ib; |
| 530 | struct buffer *ob = si->ob; |
| 531 | int fd = si->fd; |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 532 | |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 533 | 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] | 534 | now_ms, __FUNCTION__, |
| 535 | fd, fdtab[fd].owner, |
| 536 | ib, ob, |
| 537 | ib->rex, ob->wex, |
| 538 | ib->flags, ob->flags, |
Willy Tarreau | b025325 | 2008-11-30 21:37:12 +0100 | [diff] [blame] | 539 | ib->l, ob->l, si->state); |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 540 | |
| 541 | /* Check if we need to close the read side */ |
| 542 | if (!(ib->flags & BF_SHUTR)) { |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 543 | /* Read not closed, update FD status and timeout for reads */ |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 544 | if (ib->flags & (BF_FULL|BF_HIJACK)) { |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 545 | /* stop reading */ |
| 546 | EV_FD_COND_C(fd, DIR_RD); |
| 547 | ib->rex = TICK_ETERNITY; |
| 548 | } |
| 549 | else { |
| 550 | /* (re)start reading and update timeout. Note: we don't recompute the timeout |
| 551 | * everytime we get here, otherwise it would risk never to expire. We only |
| 552 | * update it if is was not yet set, or if we already got some read status. |
| 553 | */ |
| 554 | EV_FD_COND_S(fd, DIR_RD); |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 555 | if (!tick_isset(ib->rex) || ib->flags & BF_READ_ACTIVITY) |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 556 | ib->rex = tick_add_ifset(now_ms, ib->rto); |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | /* Check if we need to close the write side */ |
| 561 | if (!(ob->flags & BF_SHUTW)) { |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 562 | /* Write not closed, update FD status and timeout for writes */ |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 563 | if ((ob->flags & BF_EMPTY) || |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 564 | (ob->flags & (BF_HIJACK|BF_WRITE_ENA)) == 0) { |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 565 | /* stop writing */ |
| 566 | EV_FD_COND_C(fd, DIR_WR); |
| 567 | ob->wex = TICK_ETERNITY; |
| 568 | } |
| 569 | else { |
| 570 | /* (re)start writing and update timeout. Note: we don't recompute the timeout |
| 571 | * everytime we get here, otherwise it would risk never to expire. We only |
| 572 | * update it if is was not yet set, or if we already got some write status. |
| 573 | */ |
| 574 | EV_FD_COND_S(fd, DIR_WR); |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 575 | if (!tick_isset(ob->wex) || ob->flags & BF_WRITE_ACTIVITY) { |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 576 | ob->wex = tick_add_ifset(now_ms, ob->wto); |
Willy Tarreau | 21e1be8 | 2008-08-29 11:30:14 +0200 | [diff] [blame] | 577 | if (tick_isset(ob->wex) && tick_isset(ib->rex)) { |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 578 | /* Note: depending on the protocol, we don't know if we're waiting |
| 579 | * for incoming data or not. So in order to prevent the socket from |
| 580 | * expiring read timeouts during writes, we refresh the read timeout, |
| 581 | * except if it was already infinite. |
| 582 | */ |
| 583 | ib->rex = ob->wex; |
| 584 | } |
| 585 | } |
| 586 | } |
| 587 | } |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 588 | } |
| 589 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 590 | |
| 591 | /* |
| 592 | * Local variables: |
| 593 | * c-indent-level: 8 |
| 594 | * c-basic-offset: 8 |
| 595 | * End: |
| 596 | */ |