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