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