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