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; |
| 98 | /* we hope to read more data or to get a close on next round */ |
| 99 | continue; |
| 100 | } |
| 101 | else if (ret == 0) { |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 102 | b->flags |= BF_READ_NULL; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 103 | break; |
| 104 | } |
| 105 | else if (errno == EAGAIN) {/* ignore EAGAIN */ |
| 106 | break; |
| 107 | } |
| 108 | else { |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 109 | b->flags |= BF_READ_ERROR; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 110 | fdtab[fd].state = FD_STERROR; |
| 111 | break; |
| 112 | } |
| 113 | } /* while(1) */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 114 | } |
| 115 | else { |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 116 | b->flags |= BF_READ_ERROR; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 117 | fdtab[fd].state = FD_STERROR; |
| 118 | } |
| 119 | |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 120 | if (b->flags & BF_READ_STATUS) { |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 121 | if (b->rto && MY_FD_ISSET(fd, StaticReadEvent)) |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 122 | tv_delayfrom(&b->rex, &now, b->rto); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 123 | else |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 124 | tv_eternity(&b->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 125 | |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 126 | task_wakeup(&rq, fdtab[fd].owner); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | |
| 133 | /* |
Willy Tarreau | f8306d5 | 2006-07-29 19:01:31 +0200 | [diff] [blame] | 134 | * this function is called on a write event from a stream socket. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 135 | * It returns 0. |
| 136 | */ |
Willy Tarreau | f8306d5 | 2006-07-29 19:01:31 +0200 | [diff] [blame] | 137 | int stream_sock_write(int fd) { |
Willy Tarreau | 5446940 | 2006-07-29 16:59:06 +0200 | [diff] [blame] | 138 | struct buffer *b = fdtab[fd].cb[DIR_WR].b; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 139 | int ret, max; |
| 140 | |
| 141 | #ifdef DEBUG_FULL |
Willy Tarreau | f8306d5 | 2006-07-29 19:01:31 +0200 | [diff] [blame] | 142 | 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] | 143 | #endif |
| 144 | |
| 145 | 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] | 146 | b->r = b->w = b->lr = b->data; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 147 | max = 0; |
| 148 | } |
| 149 | else if (b->r > b->w) { |
| 150 | max = b->r - b->w; |
| 151 | } |
| 152 | else |
| 153 | max = b->data + BUFSIZE - b->w; |
| 154 | |
| 155 | if (fdtab[fd].state != FD_STERROR) { |
| 156 | if (max == 0) { |
Willy Tarreau | f8306d5 | 2006-07-29 19:01:31 +0200 | [diff] [blame] | 157 | /* may be we have received a connection acknowledgement in TCP mode without data */ |
| 158 | if (fdtab[fd].state == FD_STCONN) { |
| 159 | int skerr; |
| 160 | socklen_t lskerr = sizeof(skerr); |
Willy Tarreau | c642348 | 2006-10-15 14:59:03 +0200 | [diff] [blame] | 161 | ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr); |
| 162 | if (ret == -1 || skerr) { |
Willy Tarreau | f8306d5 | 2006-07-29 19:01:31 +0200 | [diff] [blame] | 163 | b->flags |= BF_WRITE_ERROR; |
| 164 | fdtab[fd].state = FD_STERROR; |
| 165 | task_wakeup(&rq, fdtab[fd].owner); |
| 166 | tv_eternity(&b->wex); |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 167 | MY_FD_CLR(fd, StaticWriteEvent); |
Willy Tarreau | f8306d5 | 2006-07-29 19:01:31 +0200 | [diff] [blame] | 168 | return 0; |
| 169 | } |
| 170 | } |
| 171 | |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 172 | b->flags |= BF_WRITE_NULL; |
Willy Tarreau | f8306d5 | 2006-07-29 19:01:31 +0200 | [diff] [blame] | 173 | task_wakeup(&rq, fdtab[fd].owner); |
| 174 | fdtab[fd].state = FD_STREADY; |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 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 | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | #ifndef MSG_NOSIGNAL |
| 181 | { |
| 182 | int skerr; |
| 183 | socklen_t lskerr = sizeof(skerr); |
| 184 | |
Willy Tarreau | c642348 | 2006-10-15 14:59:03 +0200 | [diff] [blame] | 185 | ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr); |
| 186 | if (ret == -1 || skerr) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 187 | ret = -1; |
| 188 | else |
| 189 | ret = send(fd, b->w, max, MSG_DONTWAIT); |
| 190 | } |
| 191 | #else |
| 192 | ret = send(fd, b->w, max, MSG_DONTWAIT | MSG_NOSIGNAL); |
| 193 | #endif |
| 194 | |
| 195 | if (ret > 0) { |
| 196 | b->l -= ret; |
| 197 | b->w += ret; |
| 198 | |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 199 | b->flags |= BF_PARTIAL_WRITE; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 200 | |
| 201 | if (b->w == b->data + BUFSIZE) { |
| 202 | b->w = b->data; /* wrap around the buffer */ |
| 203 | } |
| 204 | } |
| 205 | else if (ret == 0) { |
Willy Tarreau | f8306d5 | 2006-07-29 19:01:31 +0200 | [diff] [blame] | 206 | /* nothing written, just pretend we were never called */ |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 207 | // b->flags |= BF_WRITE_NULL; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 208 | return 0; |
| 209 | } |
| 210 | else if (errno == EAGAIN) /* ignore EAGAIN */ |
| 211 | return 0; |
| 212 | else { |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 213 | b->flags |= BF_WRITE_ERROR; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 214 | fdtab[fd].state = FD_STERROR; |
| 215 | } |
| 216 | } |
| 217 | else { |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 218 | b->flags |= BF_WRITE_ERROR; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 219 | fdtab[fd].state = FD_STERROR; |
| 220 | } |
| 221 | |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 222 | if (b->wto) { |
| 223 | tv_delayfrom(&b->wex, &now, b->wto); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 224 | /* FIXME: to prevent the client from expiring read timeouts during writes, |
| 225 | * we refresh it. A solution would be to merge read+write timeouts into a |
| 226 | * unique one, although that needs some study particularly on full-duplex |
| 227 | * TCP connections. */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 228 | b->rex = b->wex; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 229 | } |
| 230 | else |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 231 | tv_eternity(&b->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 232 | |
Willy Tarreau | f8306d5 | 2006-07-29 19:01:31 +0200 | [diff] [blame] | 233 | task_wakeup(&rq, fdtab[fd].owner); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 234 | return 0; |
| 235 | } |
| 236 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 237 | |
| 238 | |
| 239 | /* |
| 240 | * Local variables: |
| 241 | * c-indent-level: 8 |
| 242 | * c-basic-offset: 8 |
| 243 | * End: |
| 244 | */ |