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