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 | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 24 | #include <common/standard.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 25 | #include <common/time.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 26 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 27 | #include <types/buffers.h> |
| 28 | #include <types/global.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 29 | #include <types/polling.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 30 | |
| 31 | #include <proto/client.h> |
| 32 | #include <proto/fd.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 33 | #include <proto/stream_sock.h> |
| 34 | #include <proto/task.h> |
| 35 | |
| 36 | |
| 37 | /* |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 38 | * this function is called on a read event from a stream socket. |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 39 | * It returns 0 if we have a high confidence that we will not be |
| 40 | * able to read more data without polling first. Returns non-zero |
| 41 | * otherwise. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 42 | */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 43 | int stream_sock_read(int fd) { |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 44 | __label__ out_wakeup; |
Willy Tarreau | 5446940 | 2006-07-29 16:59:06 +0200 | [diff] [blame] | 45 | struct buffer *b = fdtab[fd].cb[DIR_RD].b; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 46 | int ret, max, retval; |
Willy Tarreau | b8949f1 | 2007-03-23 22:39:59 +0100 | [diff] [blame] | 47 | int read_poll = MAX_READ_POLL_LOOPS; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 48 | |
| 49 | #ifdef DEBUG_FULL |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 50 | fprintf(stderr,"stream_sock_read : fd=%d, owner=%p\n", fd, fdtab[fd].owner); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 51 | #endif |
| 52 | |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 53 | retval = 1; |
| 54 | |
| 55 | if (unlikely(fdtab[fd].state == FD_STERROR || (fdtab[fd].ev & FD_POLL_ERR))) { |
| 56 | /* read/write error */ |
| 57 | b->flags |= BF_READ_ERROR; |
| 58 | fdtab[fd].state = FD_STERROR; |
| 59 | goto out_wakeup; |
| 60 | } |
| 61 | |
| 62 | if (unlikely(fdtab[fd].ev & FD_POLL_HUP)) { |
| 63 | /* connection closed */ |
| 64 | b->flags |= BF_READ_NULL; |
| 65 | goto out_wakeup; |
| 66 | } |
| 67 | |
| 68 | retval = 0; |
| 69 | while (read_poll-- > 0) { |
| 70 | if (b->l == 0) { /* let's realign the buffer to optimize I/O */ |
| 71 | b->r = b->w = b->lr = b->data; |
| 72 | max = b->rlim - b->data; |
| 73 | } |
| 74 | else if (b->r > b->w) { |
| 75 | max = b->rlim - b->r; |
| 76 | } |
| 77 | else { |
| 78 | max = b->w - b->r; |
| 79 | /* FIXME: theorically, if w>0, we shouldn't have rlim < data+size anymore |
| 80 | * since it means that the rewrite protection has been removed. This |
| 81 | * implies that the if statement can be removed. |
| 82 | */ |
| 83 | if (max > b->rlim - b->data) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 84 | max = b->rlim - b->data; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 85 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 86 | |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 87 | if (max == 0) { /* not anymore room to store data */ |
| 88 | EV_FD_CLR(fd, DIR_RD); |
| 89 | break; |
| 90 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 91 | |
| 92 | #ifndef MSG_NOSIGNAL |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 93 | { |
| 94 | int skerr; |
| 95 | socklen_t lskerr = sizeof(skerr); |
| 96 | |
| 97 | ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr); |
| 98 | if (ret == -1 || skerr) |
| 99 | ret = -1; |
| 100 | else |
| 101 | ret = recv(fd, b->r, max, 0); |
| 102 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 103 | #else |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 104 | ret = recv(fd, b->r, max, MSG_NOSIGNAL); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 105 | #endif |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 106 | if (ret > 0) { |
| 107 | b->r += ret; |
| 108 | b->l += ret; |
| 109 | b->flags |= BF_PARTIAL_READ; |
| 110 | retval = 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 111 | |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 112 | if (b->r == b->data + BUFSIZE) { |
| 113 | b->r = b->data; /* wrap around the buffer */ |
| 114 | } |
Willy Tarreau | 9641e8f | 2007-03-23 23:02:09 +0100 | [diff] [blame] | 115 | |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 116 | b->total += ret; |
Willy Tarreau | 9641e8f | 2007-03-23 23:02:09 +0100 | [diff] [blame] | 117 | |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 118 | /* generally if we read something smaller than the 1 or 2 MSS, |
| 119 | * it means that it's not worth trying to read again. It may |
| 120 | * also happen on headers, but the application then can stop |
| 121 | * reading before we start polling. |
| 122 | */ |
| 123 | if (ret < MIN_RET_FOR_READ_LOOP) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 124 | break; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 125 | |
| 126 | if (!read_poll) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 127 | break; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 128 | |
| 129 | /* we hope to read more data or to get a close on next round */ |
| 130 | continue; |
| 131 | } |
| 132 | else if (ret == 0) { |
| 133 | b->flags |= BF_READ_NULL; |
| 134 | retval = 1; // connection closed |
| 135 | break; |
| 136 | } |
Willy Tarreau | 9f19529 | 2007-04-15 21:26:58 +0200 | [diff] [blame] | 137 | else if (errno == EAGAIN) { |
| 138 | /* Ignore EAGAIN but inform the poller that there is |
| 139 | * nothing to read left. |
| 140 | */ |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 141 | retval = 0; |
| 142 | break; |
| 143 | } |
| 144 | else { |
| 145 | retval = 1; |
| 146 | b->flags |= BF_READ_ERROR; |
| 147 | fdtab[fd].state = FD_STERROR; |
| 148 | break; |
| 149 | } |
| 150 | } /* while (read_poll) */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 151 | |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 152 | if (b->flags & BF_READ_STATUS) { |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 153 | out_wakeup: |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 154 | if (b->rto && EV_FD_ISSET(fd, DIR_RD)) |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame^] | 155 | tv_ms_add(&b->rex, &now, b->rto); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 156 | else |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 157 | tv_eternity(&b->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 158 | |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 159 | task_wakeup(fdtab[fd].owner); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 160 | } |
| 161 | |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 162 | fdtab[fd].ev &= ~FD_POLL_RD; |
| 163 | return retval; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | |
| 167 | /* |
Willy Tarreau | f8306d5 | 2006-07-29 19:01:31 +0200 | [diff] [blame] | 168 | * this function is called on a write event from a stream socket. |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 169 | * It returns 0 if we have a high confidence that we will not be |
| 170 | * able to write more data without polling first. Returns non-zero |
| 171 | * otherwise. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 172 | */ |
Willy Tarreau | f8306d5 | 2006-07-29 19:01:31 +0200 | [diff] [blame] | 173 | int stream_sock_write(int fd) { |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 174 | __label__ out_eternity; |
Willy Tarreau | 5446940 | 2006-07-29 16:59:06 +0200 | [diff] [blame] | 175 | struct buffer *b = fdtab[fd].cb[DIR_WR].b; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 176 | int ret, max, retval; |
| 177 | int write_poll = MAX_WRITE_POLL_LOOPS; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 178 | |
| 179 | #ifdef DEBUG_FULL |
Willy Tarreau | f8306d5 | 2006-07-29 19:01:31 +0200 | [diff] [blame] | 180 | 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] | 181 | #endif |
| 182 | |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 183 | retval = 1; |
| 184 | |
| 185 | if (unlikely(fdtab[fd].state == FD_STERROR || (fdtab[fd].ev & FD_POLL_ERR))) { |
| 186 | /* read/write error */ |
| 187 | b->flags |= BF_WRITE_ERROR; |
| 188 | fdtab[fd].state = FD_STERROR; |
| 189 | EV_FD_CLR(fd, DIR_WR); |
| 190 | goto out_eternity; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 191 | } |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 192 | |
| 193 | retval = 0; |
| 194 | while (write_poll-- > 0) { |
| 195 | if (b->l == 0) { /* let's realign the buffer to optimize I/O */ |
| 196 | b->r = b->w = b->lr = b->data; |
| 197 | max = 0; |
| 198 | } |
| 199 | else if (b->r > b->w) { |
| 200 | max = b->r - b->w; |
| 201 | } |
| 202 | else { |
| 203 | max = b->data + BUFSIZE - b->w; |
| 204 | } |
| 205 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 206 | if (max == 0) { |
Willy Tarreau | f8306d5 | 2006-07-29 19:01:31 +0200 | [diff] [blame] | 207 | /* may be we have received a connection acknowledgement in TCP mode without data */ |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 208 | if (!(b->flags & BF_PARTIAL_WRITE) |
| 209 | && fdtab[fd].state == FD_STCONN) { |
Willy Tarreau | f8306d5 | 2006-07-29 19:01:31 +0200 | [diff] [blame] | 210 | int skerr; |
| 211 | socklen_t lskerr = sizeof(skerr); |
Willy Tarreau | c642348 | 2006-10-15 14:59:03 +0200 | [diff] [blame] | 212 | ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr); |
| 213 | if (ret == -1 || skerr) { |
Willy Tarreau | f8306d5 | 2006-07-29 19:01:31 +0200 | [diff] [blame] | 214 | b->flags |= BF_WRITE_ERROR; |
| 215 | fdtab[fd].state = FD_STERROR; |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 216 | EV_FD_CLR(fd, DIR_WR); |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 217 | retval = 1; |
| 218 | goto out_eternity; |
Willy Tarreau | f8306d5 | 2006-07-29 19:01:31 +0200 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 222 | b->flags |= BF_WRITE_NULL; |
Willy Tarreau | f8306d5 | 2006-07-29 19:01:31 +0200 | [diff] [blame] | 223 | fdtab[fd].state = FD_STREADY; |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 224 | EV_FD_CLR(fd, DIR_WR); |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 225 | retval = 1; |
| 226 | goto out_eternity; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | #ifndef MSG_NOSIGNAL |
| 230 | { |
| 231 | int skerr; |
| 232 | socklen_t lskerr = sizeof(skerr); |
| 233 | |
Willy Tarreau | c642348 | 2006-10-15 14:59:03 +0200 | [diff] [blame] | 234 | ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr); |
| 235 | if (ret == -1 || skerr) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 236 | ret = -1; |
| 237 | else |
| 238 | ret = send(fd, b->w, max, MSG_DONTWAIT); |
| 239 | } |
| 240 | #else |
| 241 | ret = send(fd, b->w, max, MSG_DONTWAIT | MSG_NOSIGNAL); |
| 242 | #endif |
| 243 | |
| 244 | if (ret > 0) { |
| 245 | b->l -= ret; |
| 246 | b->w += ret; |
| 247 | |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 248 | b->flags |= BF_PARTIAL_WRITE; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 249 | retval = 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 250 | |
| 251 | if (b->w == b->data + BUFSIZE) { |
| 252 | b->w = b->data; /* wrap around the buffer */ |
| 253 | } |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 254 | |
| 255 | if (!write_poll) |
| 256 | break; |
| 257 | |
| 258 | /* we hope to be able to write more data */ |
| 259 | continue; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 260 | } |
| 261 | else if (ret == 0) { |
Willy Tarreau | f8306d5 | 2006-07-29 19:01:31 +0200 | [diff] [blame] | 262 | /* nothing written, just pretend we were never called */ |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 263 | retval = 0; |
| 264 | break; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 265 | } |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 266 | else if (errno == EAGAIN) {/* ignore EAGAIN */ |
| 267 | retval = 0; |
| 268 | break; |
| 269 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 270 | else { |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 271 | b->flags |= BF_WRITE_ERROR; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 272 | fdtab[fd].state = FD_STERROR; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 273 | EV_FD_CLR(fd, DIR_WR); |
| 274 | retval = 1; |
| 275 | goto out_eternity; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 276 | } |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 277 | } /* while (write_poll) */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 278 | |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 279 | if (b->flags & BF_WRITE_STATUS) { |
| 280 | if (b->wto) { |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame^] | 281 | tv_ms_add(&b->wex, &now, b->wto); |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 282 | /* FIXME: to prevent the client from expiring read timeouts during writes, |
| 283 | * we refresh it. A solution would be to merge read+write timeouts into a |
| 284 | * unique one, although that needs some study particularly on full-duplex |
| 285 | * TCP connections. */ |
| 286 | b->rex = b->wex; |
| 287 | } |
| 288 | else { |
| 289 | out_eternity: |
| 290 | tv_eternity(&b->wex); |
| 291 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 292 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 293 | |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 294 | task_wakeup(fdtab[fd].owner); |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 295 | fdtab[fd].ev &= ~FD_POLL_WR; |
| 296 | return retval; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 297 | } |
| 298 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 299 | |
| 300 | |
| 301 | /* |
| 302 | * Local variables: |
| 303 | * c-indent-level: 8 |
| 304 | * c-basic-offset: 8 |
| 305 | * End: |
| 306 | */ |