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