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 | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 24 | #include <common/time.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 25 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 26 | #include <types/buffers.h> |
| 27 | #include <types/global.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 28 | #include <types/polling.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 29 | |
| 30 | #include <proto/client.h> |
| 31 | #include <proto/fd.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 32 | #include <proto/stream_sock.h> |
| 33 | #include <proto/task.h> |
| 34 | |
| 35 | |
| 36 | /* |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 37 | * this function is called on a read event from a stream socket. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 38 | * It returns 0. |
| 39 | */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 40 | int stream_sock_read(int fd) { |
Willy Tarreau | 5446940 | 2006-07-29 16:59:06 +0200 | [diff] [blame] | 41 | struct buffer *b = fdtab[fd].cb[DIR_RD].b; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 42 | int ret, max; |
Willy Tarreau | b8949f1 | 2007-03-23 22:39:59 +0100 | [diff] [blame] | 43 | int read_poll = MAX_READ_POLL_LOOPS; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 44 | |
| 45 | #ifdef DEBUG_FULL |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 46 | 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] | 47 | #endif |
| 48 | |
| 49 | if (fdtab[fd].state != FD_STERROR) { |
Willy Tarreau | b8949f1 | 2007-03-23 22:39:59 +0100 | [diff] [blame] | 50 | while (read_poll-- > 0) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 51 | { |
| 52 | if (b->l == 0) { /* let's realign the buffer to optimize I/O */ |
Willy Tarreau | e09e0ce | 2007-03-18 16:31:29 +0100 | [diff] [blame] | 53 | b->r = b->w = b->lr = b->data; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 54 | max = b->rlim - b->data; |
| 55 | } |
| 56 | else if (b->r > b->w) { |
| 57 | max = b->rlim - b->r; |
| 58 | } |
| 59 | else { |
| 60 | max = b->w - b->r; |
| 61 | /* FIXME: theorically, if w>0, we shouldn't have rlim < data+size anymore |
| 62 | * since it means that the rewrite protection has been removed. This |
| 63 | * implies that the if statement can be removed. |
| 64 | */ |
| 65 | if (max > b->rlim - b->data) |
| 66 | max = b->rlim - b->data; |
| 67 | } |
| 68 | |
| 69 | if (max == 0) { /* not anymore room to store data */ |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 70 | MY_FD_CLR(fd, StaticReadEvent); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 71 | break; |
| 72 | } |
| 73 | |
| 74 | #ifndef MSG_NOSIGNAL |
| 75 | { |
| 76 | int skerr; |
| 77 | socklen_t lskerr = sizeof(skerr); |
| 78 | |
Willy Tarreau | c642348 | 2006-10-15 14:59:03 +0200 | [diff] [blame] | 79 | ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr); |
| 80 | if (ret == -1 || skerr) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 81 | ret = -1; |
| 82 | else |
| 83 | ret = recv(fd, b->r, max, 0); |
| 84 | } |
| 85 | #else |
| 86 | ret = recv(fd, b->r, max, MSG_NOSIGNAL); |
| 87 | #endif |
| 88 | if (ret > 0) { |
| 89 | b->r += ret; |
| 90 | b->l += ret; |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 91 | b->flags |= BF_PARTIAL_READ; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 92 | |
| 93 | if (b->r == b->data + BUFSIZE) { |
| 94 | b->r = b->data; /* wrap around the buffer */ |
| 95 | } |
| 96 | |
| 97 | b->total += ret; |
Willy Tarreau | 9641e8f | 2007-03-23 23:02:09 +0100 | [diff] [blame] | 98 | |
| 99 | /* generally if we read something smaller than the 1 or 2 MSS, |
| 100 | * it means that it's not worth trying to read again. |
| 101 | */ |
| 102 | if (ret < MIN_RET_FOR_READ_LOOP) |
| 103 | break; |
| 104 | if (!read_poll) |
| 105 | break; |
| 106 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 107 | /* we hope to read more data or to get a close on next round */ |
| 108 | continue; |
| 109 | } |
| 110 | else if (ret == 0) { |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 111 | b->flags |= BF_READ_NULL; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 112 | break; |
| 113 | } |
| 114 | else if (errno == EAGAIN) {/* ignore EAGAIN */ |
| 115 | break; |
| 116 | } |
| 117 | else { |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 118 | b->flags |= BF_READ_ERROR; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 119 | fdtab[fd].state = FD_STERROR; |
| 120 | break; |
| 121 | } |
| 122 | } /* while(1) */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 123 | } |
| 124 | else { |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 125 | b->flags |= BF_READ_ERROR; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 126 | fdtab[fd].state = FD_STERROR; |
| 127 | } |
| 128 | |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 129 | if (b->flags & BF_READ_STATUS) { |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 130 | if (b->rto && MY_FD_ISSET(fd, StaticReadEvent)) |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 131 | tv_delayfrom(&b->rex, &now, b->rto); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 132 | else |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 133 | tv_eternity(&b->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 134 | |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 135 | task_wakeup(&rq, fdtab[fd].owner); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | |
| 142 | /* |
Willy Tarreau | f8306d5 | 2006-07-29 19:01:31 +0200 | [diff] [blame] | 143 | * this function is called on a write event from a stream socket. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 144 | * It returns 0. |
| 145 | */ |
Willy Tarreau | f8306d5 | 2006-07-29 19:01:31 +0200 | [diff] [blame] | 146 | int stream_sock_write(int fd) { |
Willy Tarreau | 5446940 | 2006-07-29 16:59:06 +0200 | [diff] [blame] | 147 | struct buffer *b = fdtab[fd].cb[DIR_WR].b; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 148 | int ret, max; |
| 149 | |
| 150 | #ifdef DEBUG_FULL |
Willy Tarreau | f8306d5 | 2006-07-29 19:01:31 +0200 | [diff] [blame] | 151 | 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] | 152 | #endif |
| 153 | |
| 154 | if (b->l == 0) { /* let's realign the buffer to optimize I/O */ |
Willy Tarreau | e09e0ce | 2007-03-18 16:31:29 +0100 | [diff] [blame] | 155 | b->r = b->w = b->lr = b->data; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 156 | max = 0; |
| 157 | } |
| 158 | else if (b->r > b->w) { |
| 159 | max = b->r - b->w; |
| 160 | } |
| 161 | else |
| 162 | max = b->data + BUFSIZE - b->w; |
| 163 | |
| 164 | if (fdtab[fd].state != FD_STERROR) { |
| 165 | if (max == 0) { |
Willy Tarreau | f8306d5 | 2006-07-29 19:01:31 +0200 | [diff] [blame] | 166 | /* may be we have received a connection acknowledgement in TCP mode without data */ |
| 167 | if (fdtab[fd].state == FD_STCONN) { |
| 168 | int skerr; |
| 169 | socklen_t lskerr = sizeof(skerr); |
Willy Tarreau | c642348 | 2006-10-15 14:59:03 +0200 | [diff] [blame] | 170 | ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr); |
| 171 | if (ret == -1 || skerr) { |
Willy Tarreau | f8306d5 | 2006-07-29 19:01:31 +0200 | [diff] [blame] | 172 | b->flags |= BF_WRITE_ERROR; |
| 173 | fdtab[fd].state = FD_STERROR; |
| 174 | task_wakeup(&rq, fdtab[fd].owner); |
| 175 | tv_eternity(&b->wex); |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 176 | MY_FD_CLR(fd, StaticWriteEvent); |
Willy Tarreau | f8306d5 | 2006-07-29 19:01:31 +0200 | [diff] [blame] | 177 | return 0; |
| 178 | } |
| 179 | } |
| 180 | |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 181 | b->flags |= BF_WRITE_NULL; |
Willy Tarreau | f8306d5 | 2006-07-29 19:01:31 +0200 | [diff] [blame] | 182 | task_wakeup(&rq, fdtab[fd].owner); |
| 183 | fdtab[fd].state = FD_STREADY; |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 184 | tv_eternity(&b->wex); |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 185 | MY_FD_CLR(fd, StaticWriteEvent); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | #ifndef MSG_NOSIGNAL |
| 190 | { |
| 191 | int skerr; |
| 192 | socklen_t lskerr = sizeof(skerr); |
| 193 | |
Willy Tarreau | c642348 | 2006-10-15 14:59:03 +0200 | [diff] [blame] | 194 | ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr); |
| 195 | if (ret == -1 || skerr) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 196 | ret = -1; |
| 197 | else |
| 198 | ret = send(fd, b->w, max, MSG_DONTWAIT); |
| 199 | } |
| 200 | #else |
| 201 | ret = send(fd, b->w, max, MSG_DONTWAIT | MSG_NOSIGNAL); |
| 202 | #endif |
| 203 | |
| 204 | if (ret > 0) { |
| 205 | b->l -= ret; |
| 206 | b->w += ret; |
| 207 | |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 208 | b->flags |= BF_PARTIAL_WRITE; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 209 | |
| 210 | if (b->w == b->data + BUFSIZE) { |
| 211 | b->w = b->data; /* wrap around the buffer */ |
| 212 | } |
| 213 | } |
| 214 | else if (ret == 0) { |
Willy Tarreau | f8306d5 | 2006-07-29 19:01:31 +0200 | [diff] [blame] | 215 | /* nothing written, just pretend we were never called */ |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 216 | // b->flags |= BF_WRITE_NULL; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 217 | return 0; |
| 218 | } |
| 219 | else if (errno == EAGAIN) /* ignore EAGAIN */ |
| 220 | return 0; |
| 221 | else { |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 222 | b->flags |= BF_WRITE_ERROR; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 223 | fdtab[fd].state = FD_STERROR; |
| 224 | } |
| 225 | } |
| 226 | else { |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 227 | b->flags |= BF_WRITE_ERROR; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 228 | fdtab[fd].state = FD_STERROR; |
| 229 | } |
| 230 | |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 231 | if (b->wto) { |
| 232 | tv_delayfrom(&b->wex, &now, b->wto); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 233 | /* FIXME: to prevent the client from expiring read timeouts during writes, |
| 234 | * we refresh it. A solution would be to merge read+write timeouts into a |
| 235 | * unique one, although that needs some study particularly on full-duplex |
| 236 | * TCP connections. */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 237 | b->rex = b->wex; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 238 | } |
| 239 | else |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 240 | tv_eternity(&b->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 241 | |
Willy Tarreau | f8306d5 | 2006-07-29 19:01:31 +0200 | [diff] [blame] | 242 | task_wakeup(&rq, fdtab[fd].owner); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 243 | return 0; |
| 244 | } |
| 245 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 246 | |
| 247 | |
| 248 | /* |
| 249 | * Local variables: |
| 250 | * c-indent-level: 8 |
| 251 | * c-basic-offset: 8 |
| 252 | * End: |
| 253 | */ |