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 | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 29 | #include <proto/client.h> |
| 30 | #include <proto/fd.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 31 | #include <proto/stream_sock.h> |
| 32 | #include <proto/task.h> |
| 33 | |
| 34 | |
| 35 | /* |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 36 | * this function is called on a read event from a stream socket. |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 37 | * It returns 0 if we have a high confidence that we will not be |
| 38 | * able to read more data without polling first. Returns non-zero |
| 39 | * otherwise. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 40 | */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 41 | int stream_sock_read(int fd) { |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 42 | __label__ out_wakeup, out_shutdown_r, out_error; |
Willy Tarreau | 5446940 | 2006-07-29 16:59:06 +0200 | [diff] [blame] | 43 | struct buffer *b = fdtab[fd].cb[DIR_RD].b; |
Willy Tarreau | 8a7af60 | 2008-05-03 23:07:14 +0200 | [diff] [blame] | 44 | int ret, max, retval, cur_read; |
Willy Tarreau | b8949f1 | 2007-03-23 22:39:59 +0100 | [diff] [blame] | 45 | int read_poll = MAX_READ_POLL_LOOPS; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 46 | |
| 47 | #ifdef DEBUG_FULL |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 48 | 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] | 49 | #endif |
| 50 | |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 51 | retval = 1; |
| 52 | |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 53 | /* stop immediately on errors */ |
| 54 | if (fdtab[fd].state == FD_STERROR || (fdtab[fd].ev & FD_POLL_ERR)) |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 55 | goto out_error; |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 56 | |
| 57 | /* stop here if we reached the end of data */ |
| 58 | if ((fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP)) == FD_POLL_HUP) |
| 59 | goto out_shutdown_r; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 60 | |
Willy Tarreau | 8a7af60 | 2008-05-03 23:07:14 +0200 | [diff] [blame] | 61 | cur_read = 0; |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 62 | while (1) { |
| 63 | /* |
| 64 | * 1. compute the maximum block size we can read at once. |
| 65 | */ |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 66 | if (b->l == 0) { /* let's realign the buffer to optimize I/O */ |
| 67 | b->r = b->w = b->lr = b->data; |
| 68 | max = b->rlim - b->data; |
| 69 | } |
| 70 | else if (b->r > b->w) { |
| 71 | max = b->rlim - b->r; |
| 72 | } |
| 73 | else { |
| 74 | max = b->w - b->r; |
| 75 | /* FIXME: theorically, if w>0, we shouldn't have rlim < data+size anymore |
| 76 | * since it means that the rewrite protection has been removed. This |
| 77 | * implies that the if statement can be removed. |
| 78 | */ |
| 79 | if (max > b->rlim - b->data) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 80 | max = b->rlim - b->data; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 81 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 82 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 83 | if (unlikely(max == 0)) { |
| 84 | /* Not anymore room to store data. This should theorically |
| 85 | * never happen, but better safe than sorry ! |
| 86 | */ |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 87 | b->flags |= BF_FULL; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 88 | EV_FD_CLR(fd, DIR_RD); |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 89 | b->rex = TICK_ETERNITY; |
| 90 | goto out_wakeup; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 91 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 92 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 93 | /* |
| 94 | * 2. read the largest possible block |
| 95 | */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 96 | #ifndef MSG_NOSIGNAL |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 97 | { |
| 98 | int skerr; |
| 99 | socklen_t lskerr = sizeof(skerr); |
| 100 | |
| 101 | ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr); |
| 102 | if (ret == -1 || skerr) |
| 103 | ret = -1; |
| 104 | else |
| 105 | ret = recv(fd, b->r, max, 0); |
| 106 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 107 | #else |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 108 | ret = recv(fd, b->r, max, MSG_NOSIGNAL); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 109 | #endif |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 110 | if (ret > 0) { |
| 111 | b->r += ret; |
| 112 | b->l += ret; |
Willy Tarreau | 8a7af60 | 2008-05-03 23:07:14 +0200 | [diff] [blame] | 113 | cur_read += ret; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 114 | b->flags |= BF_PARTIAL_READ; |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 115 | b->flags &= ~BF_EMPTY; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 116 | |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 117 | if (b->r == b->data + BUFSIZE) { |
| 118 | b->r = b->data; /* wrap around the buffer */ |
| 119 | } |
Willy Tarreau | 9641e8f | 2007-03-23 23:02:09 +0100 | [diff] [blame] | 120 | |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 121 | b->total += ret; |
Willy Tarreau | 9641e8f | 2007-03-23 23:02:09 +0100 | [diff] [blame] | 122 | |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 123 | if (b->l >= b->rlim - b->data) { |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 124 | /* The buffer is now full, there's no point in going through |
| 125 | * the loop again. |
| 126 | */ |
Willy Tarreau | 8a7af60 | 2008-05-03 23:07:14 +0200 | [diff] [blame] | 127 | if (!(b->flags & BF_STREAMER_FAST) && (cur_read == b->l)) { |
| 128 | b->xfer_small = 0; |
| 129 | b->xfer_large++; |
| 130 | if (b->xfer_large >= 3) { |
| 131 | /* we call this buffer a fast streamer if it manages |
| 132 | * to be filled in one call 3 consecutive times. |
| 133 | */ |
| 134 | b->flags |= (BF_STREAMER | BF_STREAMER_FAST); |
| 135 | //fputc('+', stderr); |
| 136 | } |
| 137 | } |
| 138 | else if ((b->flags & (BF_STREAMER | BF_STREAMER_FAST)) && |
| 139 | (cur_read <= BUFSIZE / 2)) { |
| 140 | b->xfer_large = 0; |
| 141 | b->xfer_small++; |
| 142 | if (b->xfer_small >= 2) { |
| 143 | /* if the buffer has been at least half full twice, |
| 144 | * we receive faster than we send, so at least it |
| 145 | * is not a "fast streamer". |
| 146 | */ |
| 147 | b->flags &= ~BF_STREAMER_FAST; |
| 148 | //fputc('-', stderr); |
| 149 | } |
| 150 | } |
| 151 | else { |
| 152 | b->xfer_small = 0; |
| 153 | b->xfer_large = 0; |
| 154 | } |
| 155 | |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 156 | b->flags |= BF_FULL; |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 157 | EV_FD_CLR(fd, DIR_RD); |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 158 | b->rex = TICK_ETERNITY; |
| 159 | goto out_wakeup; |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 160 | } |
| 161 | |
Willy Tarreau | ab3e1d3 | 2007-06-03 14:10:36 +0200 | [diff] [blame] | 162 | /* if too many bytes were missing from last read, it means that |
| 163 | * it's pointless trying to read again because the system does |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 164 | * not have them in buffers. BTW, if FD_POLL_HUP was present, |
| 165 | * it means that we have reached the end and that the connection |
| 166 | * is closed. |
Willy Tarreau | ab3e1d3 | 2007-06-03 14:10:36 +0200 | [diff] [blame] | 167 | */ |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 168 | if (ret < max) { |
Willy Tarreau | 8a7af60 | 2008-05-03 23:07:14 +0200 | [diff] [blame] | 169 | if ((b->flags & (BF_STREAMER | BF_STREAMER_FAST)) && |
| 170 | (cur_read <= BUFSIZE / 2)) { |
| 171 | b->xfer_large = 0; |
| 172 | b->xfer_small++; |
| 173 | if (b->xfer_small >= 3) { |
| 174 | /* we have read less than half of the buffer in |
| 175 | * one pass, and this happened at least 3 times. |
| 176 | * This is definitely not a streamer. |
| 177 | */ |
| 178 | b->flags &= ~(BF_STREAMER | BF_STREAMER_FAST); |
| 179 | //fputc('!', stderr); |
| 180 | } |
| 181 | } |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 182 | if (fdtab[fd].ev & FD_POLL_HUP) |
| 183 | goto out_shutdown_r; |
Willy Tarreau | ab3e1d3 | 2007-06-03 14:10:36 +0200 | [diff] [blame] | 184 | break; |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 185 | } |
Willy Tarreau | ab3e1d3 | 2007-06-03 14:10:36 +0200 | [diff] [blame] | 186 | |
| 187 | /* generally if we read something smaller than 1 or 2 MSS, |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 188 | * it means that it's not worth trying to read again. It may |
| 189 | * also happen on headers, but the application then can stop |
| 190 | * reading before we start polling. |
| 191 | */ |
| 192 | if (ret < MIN_RET_FOR_READ_LOOP) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 193 | break; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 194 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 195 | if (--read_poll <= 0) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 196 | break; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 197 | |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 198 | } |
| 199 | else if (ret == 0) { |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 200 | /* connection closed */ |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 201 | goto out_shutdown_r; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 202 | } |
Willy Tarreau | 9f19529 | 2007-04-15 21:26:58 +0200 | [diff] [blame] | 203 | else if (errno == EAGAIN) { |
| 204 | /* Ignore EAGAIN but inform the poller that there is |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 205 | * nothing to read left. But we may have done some work |
| 206 | * justifying to notify the task. |
Willy Tarreau | 9f19529 | 2007-04-15 21:26:58 +0200 | [diff] [blame] | 207 | */ |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 208 | retval = 0; |
| 209 | break; |
| 210 | } |
| 211 | else { |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 212 | goto out_error; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 213 | } |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 214 | } /* while (1) */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 215 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 216 | /* |
| 217 | * The only way to get out of this loop is to have stopped reading |
| 218 | * without any error nor close, either by limiting the number of |
| 219 | * loops, or because of an EAGAIN. We only rearm the timer if we |
| 220 | * have at least read something. |
| 221 | */ |
| 222 | |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 223 | if (tick_isset(b->rex) && b->flags & BF_PARTIAL_READ) |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 224 | b->rex = tick_add_ifset(now_ms, b->rto); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 225 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 226 | if (!(b->flags & BF_READ_STATUS)) |
| 227 | goto out_skip_wakeup; |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 228 | out_wakeup: |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 229 | task_wakeup(fdtab[fd].owner); |
| 230 | |
| 231 | out_skip_wakeup: |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 232 | fdtab[fd].ev &= ~FD_POLL_IN; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 233 | return retval; |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 234 | |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 235 | out_shutdown_r: |
| 236 | fdtab[fd].ev &= ~FD_POLL_HUP; |
| 237 | b->flags |= BF_READ_NULL; |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 238 | b->rex = TICK_ETERNITY; |
| 239 | goto out_wakeup; |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 240 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 241 | out_error: |
| 242 | /* There was an error. we must wakeup the task. No need to clear |
| 243 | * the events, the task will do it. |
| 244 | */ |
| 245 | fdtab[fd].state = FD_STERROR; |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 246 | fdtab[fd].ev &= ~FD_POLL_STICKY; |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 247 | b->rex = TICK_ETERNITY; |
| 248 | goto out_wakeup; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | |
| 252 | /* |
Willy Tarreau | f8306d5 | 2006-07-29 19:01:31 +0200 | [diff] [blame] | 253 | * this function is called on a write event from a stream socket. |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 254 | * It returns 0 if we have a high confidence that we will not be |
| 255 | * able to write more data without polling first. Returns non-zero |
| 256 | * otherwise. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 257 | */ |
Willy Tarreau | f8306d5 | 2006-07-29 19:01:31 +0200 | [diff] [blame] | 258 | int stream_sock_write(int fd) { |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 259 | __label__ out_wakeup, out_error; |
Willy Tarreau | 5446940 | 2006-07-29 16:59:06 +0200 | [diff] [blame] | 260 | struct buffer *b = fdtab[fd].cb[DIR_WR].b; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 261 | int ret, max, retval; |
| 262 | int write_poll = MAX_WRITE_POLL_LOOPS; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 263 | |
| 264 | #ifdef DEBUG_FULL |
Willy Tarreau | f8306d5 | 2006-07-29 19:01:31 +0200 | [diff] [blame] | 265 | 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] | 266 | #endif |
| 267 | |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 268 | retval = 1; |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 269 | if (fdtab[fd].state == FD_STERROR || (fdtab[fd].ev & FD_POLL_ERR)) |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 270 | goto out_error; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 271 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 272 | while (1) { |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 273 | if (b->l == 0) { /* let's realign the buffer to optimize I/O */ |
| 274 | b->r = b->w = b->lr = b->data; |
| 275 | max = 0; |
| 276 | } |
| 277 | else if (b->r > b->w) { |
| 278 | max = b->r - b->w; |
| 279 | } |
| 280 | else { |
| 281 | max = b->data + BUFSIZE - b->w; |
| 282 | } |
| 283 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 284 | if (max == 0) { |
Willy Tarreau | f8306d5 | 2006-07-29 19:01:31 +0200 | [diff] [blame] | 285 | /* 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] | 286 | if (likely(fdtab[fd].state == FD_STCONN)) { |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 287 | /* We have no data to send to check the connection, and |
| 288 | * getsockopt() will not inform us whether the connection |
| 289 | * is still pending. So we'll reuse connect() to check the |
| 290 | * state of the socket. This has the advantage of givig us |
| 291 | * the following info : |
| 292 | * - error |
| 293 | * - connecting (EALREADY, EINPROGRESS) |
| 294 | * - connected (EISCONN, 0) |
| 295 | */ |
Willy Tarreau | e94ebd0 | 2007-10-09 17:14:37 +0200 | [diff] [blame] | 296 | if ((connect(fd, fdtab[fd].peeraddr, fdtab[fd].peerlen) == 0)) |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 297 | errno = 0; |
| 298 | |
| 299 | if (errno == EALREADY || errno == EINPROGRESS) { |
| 300 | retval = 0; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 301 | goto out_may_wakeup; |
Willy Tarreau | f8306d5 | 2006-07-29 19:01:31 +0200 | [diff] [blame] | 302 | } |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 303 | |
| 304 | if (errno && errno != EISCONN) |
| 305 | goto out_error; |
| 306 | |
| 307 | /* OK we just need to indicate that we got a connection |
| 308 | * and that we wrote nothing. |
| 309 | */ |
| 310 | b->flags |= BF_WRITE_NULL; |
| 311 | fdtab[fd].state = FD_STREADY; |
Willy Tarreau | f8306d5 | 2006-07-29 19:01:31 +0200 | [diff] [blame] | 312 | } |
| 313 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 314 | /* Funny, we were called to write something but there wasn't |
| 315 | * anything. Theorically we cannot get there, but just in case, |
| 316 | * let's disable the write event and pretend we never came there. |
| 317 | */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 318 | EV_FD_CLR(fd, DIR_WR); |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 319 | b->wex = TICK_ETERNITY; |
| 320 | goto out_wakeup; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | #ifndef MSG_NOSIGNAL |
| 324 | { |
| 325 | int skerr; |
| 326 | socklen_t lskerr = sizeof(skerr); |
| 327 | |
Willy Tarreau | c642348 | 2006-10-15 14:59:03 +0200 | [diff] [blame] | 328 | ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr); |
| 329 | if (ret == -1 || skerr) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 330 | ret = -1; |
| 331 | else |
| 332 | ret = send(fd, b->w, max, MSG_DONTWAIT); |
| 333 | } |
| 334 | #else |
| 335 | ret = send(fd, b->w, max, MSG_DONTWAIT | MSG_NOSIGNAL); |
| 336 | #endif |
| 337 | |
| 338 | if (ret > 0) { |
| 339 | b->l -= ret; |
| 340 | b->w += ret; |
| 341 | |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 342 | b->flags |= BF_PARTIAL_WRITE; |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 343 | |
| 344 | if (b->l < b->rlim - b->data) |
| 345 | b->flags &= ~BF_FULL; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 346 | |
| 347 | if (b->w == b->data + BUFSIZE) { |
| 348 | b->w = b->data; /* wrap around the buffer */ |
| 349 | } |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 350 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 351 | if (!b->l) { |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 352 | b->flags |= BF_EMPTY; |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 353 | EV_FD_CLR(fd, DIR_WR); |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 354 | b->wex = TICK_ETERNITY; |
| 355 | goto out_wakeup; |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 356 | } |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 357 | |
Willy Tarreau | ab3e1d3 | 2007-06-03 14:10:36 +0200 | [diff] [blame] | 358 | /* if the system buffer is full, don't insist */ |
| 359 | if (ret < max) |
| 360 | break; |
| 361 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 362 | if (--write_poll <= 0) |
| 363 | break; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 364 | } |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 365 | else if (ret == 0 || errno == EAGAIN) { |
| 366 | /* nothing written, just pretend we were never called |
| 367 | * and wait for the socket to be ready. But we may have |
| 368 | * done some work justifying to notify the task. |
| 369 | */ |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 370 | retval = 0; |
| 371 | break; |
| 372 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 373 | else { |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 374 | goto out_error; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 375 | } |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 376 | } /* while (1) */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 377 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 378 | /* |
| 379 | * The only way to get out of this loop is to have stopped writing |
| 380 | * without any error, either by limiting the number of loops, or |
| 381 | * because of an EAGAIN. We only rearm the timer if we have at least |
| 382 | * written something. |
| 383 | */ |
| 384 | |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 385 | if (tick_isset(b->wex) && b->flags & BF_PARTIAL_WRITE) { |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 386 | b->wex = tick_add_ifset(now_ms, b->wto); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 387 | if (tick_isset(b->wex)) { |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 388 | /* FIXME: to prevent the client from expiring read timeouts during writes, |
| 389 | * we refresh it. A solution would be to merge read+write timeouts into a |
| 390 | * unique one, although that needs some study particularly on full-duplex |
| 391 | * TCP connections. */ |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 392 | if (tick_isset(b->rex) && !(b->flags & BF_SHUTR)) |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 393 | b->rex = b->wex; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 394 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 395 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 396 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 397 | out_may_wakeup: |
| 398 | if (!(b->flags & BF_WRITE_STATUS)) |
| 399 | goto out_skip_wakeup; |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 400 | out_wakeup: |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 401 | task_wakeup(fdtab[fd].owner); |
| 402 | |
| 403 | out_skip_wakeup: |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 404 | fdtab[fd].ev &= ~FD_POLL_OUT; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 405 | return retval; |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 406 | |
| 407 | out_error: |
| 408 | /* There was an error. we must wakeup the task. No need to clear |
| 409 | * the events, the task will do it. |
| 410 | */ |
| 411 | fdtab[fd].state = FD_STERROR; |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 412 | fdtab[fd].ev &= ~FD_POLL_STICKY; |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 413 | b->wex = TICK_ETERNITY; |
| 414 | goto out_wakeup; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 415 | } |
| 416 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 417 | |
| 418 | |
| 419 | /* |
| 420 | * Local variables: |
| 421 | * c-indent-level: 8 |
| 422 | * c-basic-offset: 8 |
| 423 | * End: |
| 424 | */ |