Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
Willy Tarreau | b277d6e | 2012-05-11 16:59:14 +0200 | [diff] [blame] | 2 | * Functions used to send/receive data using SOCK_STREAM sockets. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3 | * |
Willy Tarreau | b277d6e | 2012-05-11 16:59:14 +0200 | [diff] [blame] | 4 | * Copyright 2000-2012 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 | |
Willy Tarreau | 6b4aad4 | 2009-01-18 21:59:13 +0100 | [diff] [blame] | 13 | #define _GNU_SOURCE |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 14 | #include <errno.h> |
| 15 | #include <fcntl.h> |
| 16 | #include <stdio.h> |
| 17 | #include <stdlib.h> |
| 18 | |
| 19 | #include <sys/socket.h> |
| 20 | #include <sys/stat.h> |
| 21 | #include <sys/types.h> |
| 22 | |
Dmitry Sivachenko | caf5898 | 2009-08-24 15:11:06 +0400 | [diff] [blame] | 23 | #include <netinet/tcp.h> |
| 24 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 25 | #include <common/compat.h> |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 26 | #include <common/config.h> |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 27 | #include <common/debug.h> |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 28 | #include <common/standard.h> |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 29 | #include <common/ticks.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 30 | #include <common/time.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 31 | |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 32 | #include <proto/buffers.h> |
Willy Tarreau | 8b11708 | 2012-08-06 15:06:49 +0200 | [diff] [blame] | 33 | #include <proto/connection.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 34 | #include <proto/fd.h> |
Willy Tarreau | eb47268 | 2010-05-28 18:46:57 +0200 | [diff] [blame] | 35 | #include <proto/freq_ctr.h> |
| 36 | #include <proto/log.h> |
Willy Tarreau | 3eba98a | 2009-01-25 13:56:13 +0100 | [diff] [blame] | 37 | #include <proto/pipe.h> |
Willy Tarreau | fe598a7 | 2010-09-21 21:48:23 +0200 | [diff] [blame] | 38 | #include <proto/protocols.h> |
Willy Tarreau | c63190d | 2012-05-11 14:23:52 +0200 | [diff] [blame] | 39 | #include <proto/sock_raw.h> |
Willy Tarreau | 73b013b | 2012-05-21 16:31:45 +0200 | [diff] [blame] | 40 | #include <proto/stream_interface.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 41 | #include <proto/task.h> |
| 42 | |
Willy Tarreau | 5bd8c37 | 2009-01-19 00:32:22 +0100 | [diff] [blame] | 43 | #include <types/global.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 44 | |
Willy Tarreau | b277d6e | 2012-05-11 16:59:14 +0200 | [diff] [blame] | 45 | /* main event functions used to move data between sockets and buffers */ |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 46 | static void sock_raw_read(struct connection *conn); |
| 47 | static void sock_raw_write(struct connection *conn); |
Willy Tarreau | 3d8903f | 2012-08-06 17:00:18 +0200 | [diff] [blame] | 48 | static void sock_raw_read0(struct stream_interface *si); |
Willy Tarreau | b277d6e | 2012-05-11 16:59:14 +0200 | [diff] [blame] | 49 | static void sock_raw_chk_snd(struct stream_interface *si); |
| 50 | |
| 51 | |
Willy Tarreau | 6b4aad4 | 2009-01-18 21:59:13 +0100 | [diff] [blame] | 52 | #if defined(CONFIG_HAP_LINUX_SPLICE) |
Willy Tarreau | 43d8fb2 | 2011-08-22 17:12:02 +0200 | [diff] [blame] | 53 | #include <common/splice.h> |
Willy Tarreau | 5bd8c37 | 2009-01-19 00:32:22 +0100 | [diff] [blame] | 54 | |
| 55 | /* A pipe contains 16 segments max, and it's common to see segments of 1448 bytes |
| 56 | * because of timestamps. Use this as a hint for not looping on splice(). |
| 57 | */ |
| 58 | #define SPLICE_FULL_HINT 16*1448 |
| 59 | |
Willy Tarreau | a9de333 | 2009-11-28 07:47:10 +0100 | [diff] [blame] | 60 | /* how many data we attempt to splice at once when the buffer is configured for |
| 61 | * infinite forwarding */ |
| 62 | #define MAX_SPLICE_AT_ONCE (1<<30) |
| 63 | |
Willy Tarreau | 5bd8c37 | 2009-01-19 00:32:22 +0100 | [diff] [blame] | 64 | /* Returns : |
| 65 | * -1 if splice is not possible or not possible anymore and we must switch to |
| 66 | * user-land copy (eg: to_forward reached) |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 67 | * 0 otherwise, including errors and close. |
Willy Tarreau | 5bd8c37 | 2009-01-19 00:32:22 +0100 | [diff] [blame] | 68 | * Sets : |
| 69 | * BF_READ_NULL |
| 70 | * BF_READ_PARTIAL |
| 71 | * BF_WRITE_PARTIAL (during copy) |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 72 | * BF_OUT_EMPTY (during copy) |
Willy Tarreau | 5bd8c37 | 2009-01-19 00:32:22 +0100 | [diff] [blame] | 73 | * SI_FL_ERR |
| 74 | * SI_FL_WAIT_ROOM |
| 75 | * (SI_FL_WAIT_RECV) |
Willy Tarreau | 3eba98a | 2009-01-25 13:56:13 +0100 | [diff] [blame] | 76 | * |
| 77 | * This function automatically allocates a pipe from the pipe pool. It also |
| 78 | * carefully ensures to clear b->pipe whenever it leaves the pipe empty. |
Willy Tarreau | 5bd8c37 | 2009-01-19 00:32:22 +0100 | [diff] [blame] | 79 | */ |
Willy Tarreau | b277d6e | 2012-05-11 16:59:14 +0200 | [diff] [blame] | 80 | static int sock_raw_splice_in(struct buffer *b, struct stream_interface *si) |
Willy Tarreau | 5bd8c37 | 2009-01-19 00:32:22 +0100 | [diff] [blame] | 81 | { |
Willy Tarreau | 82a0456 | 2011-12-11 22:37:06 +0100 | [diff] [blame] | 82 | static int splice_detects_close; |
Willy Tarreau | fb7508a | 2012-05-21 16:47:54 +0200 | [diff] [blame] | 83 | int fd = si_fd(si); |
Willy Tarreau | 31971e5 | 2009-09-20 12:07:52 +0200 | [diff] [blame] | 84 | int ret; |
| 85 | unsigned long max; |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 86 | int retval = 0; |
Willy Tarreau | 5bd8c37 | 2009-01-19 00:32:22 +0100 | [diff] [blame] | 87 | |
| 88 | if (!b->to_forward) |
| 89 | return -1; |
| 90 | |
| 91 | if (!(b->flags & BF_KERN_SPLICING)) |
| 92 | return -1; |
| 93 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 94 | if (buffer_not_empty(b)) { |
Willy Tarreau | 5bd8c37 | 2009-01-19 00:32:22 +0100 | [diff] [blame] | 95 | /* We're embarrassed, there are already data pending in |
| 96 | * the buffer and we don't want to have them at two |
| 97 | * locations at a time. Let's indicate we need some |
| 98 | * place and ask the consumer to hurry. |
| 99 | */ |
| 100 | si->flags |= SI_FL_WAIT_ROOM; |
Willy Tarreau | f9dabec | 2012-08-17 17:33:53 +0200 | [diff] [blame] | 101 | conn_data_stop_recv(&si->conn); |
Willy Tarreau | 5bd8c37 | 2009-01-19 00:32:22 +0100 | [diff] [blame] | 102 | b->rex = TICK_ETERNITY; |
Willy Tarreau | 73b013b | 2012-05-21 16:31:45 +0200 | [diff] [blame] | 103 | si_chk_snd(b->cons); |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 104 | return 0; |
Willy Tarreau | 5bd8c37 | 2009-01-19 00:32:22 +0100 | [diff] [blame] | 105 | } |
| 106 | |
Willy Tarreau | 3eba98a | 2009-01-25 13:56:13 +0100 | [diff] [blame] | 107 | if (unlikely(b->pipe == NULL)) { |
| 108 | if (pipes_used >= global.maxpipes || !(b->pipe = get_pipe())) { |
Willy Tarreau | 5bd8c37 | 2009-01-19 00:32:22 +0100 | [diff] [blame] | 109 | b->flags &= ~BF_KERN_SPLICING; |
| 110 | return -1; |
| 111 | } |
Willy Tarreau | 5bd8c37 | 2009-01-19 00:32:22 +0100 | [diff] [blame] | 112 | } |
| 113 | |
Willy Tarreau | 3eba98a | 2009-01-25 13:56:13 +0100 | [diff] [blame] | 114 | /* At this point, b->pipe is valid */ |
| 115 | |
Willy Tarreau | 5bd8c37 | 2009-01-19 00:32:22 +0100 | [diff] [blame] | 116 | while (1) { |
Willy Tarreau | a9de333 | 2009-11-28 07:47:10 +0100 | [diff] [blame] | 117 | if (b->to_forward == BUF_INFINITE_FORWARD) |
| 118 | max = MAX_SPLICE_AT_ONCE; |
| 119 | else |
| 120 | max = b->to_forward; |
| 121 | |
Willy Tarreau | 31971e5 | 2009-09-20 12:07:52 +0200 | [diff] [blame] | 122 | if (!max) { |
Willy Tarreau | 5bd8c37 | 2009-01-19 00:32:22 +0100 | [diff] [blame] | 123 | /* It looks like the buffer + the pipe already contain |
| 124 | * the maximum amount of data to be transferred. Try to |
| 125 | * send those data immediately on the other side if it |
| 126 | * is currently waiting. |
| 127 | */ |
| 128 | retval = -1; /* end of forwarding */ |
| 129 | break; |
| 130 | } |
| 131 | |
Willy Tarreau | 3eba98a | 2009-01-25 13:56:13 +0100 | [diff] [blame] | 132 | ret = splice(fd, NULL, b->pipe->prod, NULL, max, |
Willy Tarreau | 5bd8c37 | 2009-01-19 00:32:22 +0100 | [diff] [blame] | 133 | SPLICE_F_MOVE|SPLICE_F_NONBLOCK); |
| 134 | |
| 135 | if (ret <= 0) { |
Willy Tarreau | 5bd8c37 | 2009-01-19 00:32:22 +0100 | [diff] [blame] | 136 | if (ret == 0) { |
Willy Tarreau | 98b306b | 2009-01-25 11:11:32 +0100 | [diff] [blame] | 137 | /* connection closed. This is only detected by |
Willy Tarreau | 82a0456 | 2011-12-11 22:37:06 +0100 | [diff] [blame] | 138 | * recent kernels (>= 2.6.27.13). If we notice |
| 139 | * it works, we store the info for later use. |
Willy Tarreau | 5bd8c37 | 2009-01-19 00:32:22 +0100 | [diff] [blame] | 140 | */ |
Willy Tarreau | 82a0456 | 2011-12-11 22:37:06 +0100 | [diff] [blame] | 141 | splice_detects_close = 1; |
Willy Tarreau | 5bd8c37 | 2009-01-19 00:32:22 +0100 | [diff] [blame] | 142 | b->flags |= BF_READ_NULL; |
Willy Tarreau | 5bd8c37 | 2009-01-19 00:32:22 +0100 | [diff] [blame] | 143 | break; |
| 144 | } |
| 145 | |
| 146 | if (errno == EAGAIN) { |
| 147 | /* there are two reasons for EAGAIN : |
| 148 | * - nothing in the socket buffer (standard) |
| 149 | * - pipe is full |
Willy Tarreau | 98b306b | 2009-01-25 11:11:32 +0100 | [diff] [blame] | 150 | * - the connection is closed (kernel < 2.6.27.13) |
Willy Tarreau | 5bd8c37 | 2009-01-19 00:32:22 +0100 | [diff] [blame] | 151 | * Since we don't know if pipe is full, we'll |
| 152 | * stop if the pipe is not empty. Anyway, we |
| 153 | * will almost always fill/empty the pipe. |
| 154 | */ |
| 155 | |
Willy Tarreau | 3eba98a | 2009-01-25 13:56:13 +0100 | [diff] [blame] | 156 | if (b->pipe->data) { |
Willy Tarreau | 5bd8c37 | 2009-01-19 00:32:22 +0100 | [diff] [blame] | 157 | si->flags |= SI_FL_WAIT_ROOM; |
Willy Tarreau | 5bd8c37 | 2009-01-19 00:32:22 +0100 | [diff] [blame] | 158 | break; |
| 159 | } |
| 160 | |
Willy Tarreau | 82a0456 | 2011-12-11 22:37:06 +0100 | [diff] [blame] | 161 | /* We don't know if the connection was closed, |
| 162 | * but if we know splice detects close, then we |
| 163 | * know it for sure. |
Willy Tarreau | 98b306b | 2009-01-25 11:11:32 +0100 | [diff] [blame] | 164 | * But if we're called upon POLLIN with an empty |
Willy Tarreau | 82a0456 | 2011-12-11 22:37:06 +0100 | [diff] [blame] | 165 | * pipe and get EAGAIN, it is suspect enough to |
Willy Tarreau | 98b306b | 2009-01-25 11:11:32 +0100 | [diff] [blame] | 166 | * try to fall back to the normal recv scheme |
| 167 | * which will be able to deal with the situation. |
| 168 | */ |
Willy Tarreau | 82a0456 | 2011-12-11 22:37:06 +0100 | [diff] [blame] | 169 | if (splice_detects_close) |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 170 | conn_data_poll_recv(&si->conn); /* we know for sure that it's EAGAIN */ |
Willy Tarreau | 82a0456 | 2011-12-11 22:37:06 +0100 | [diff] [blame] | 171 | else |
| 172 | retval = -1; |
Willy Tarreau | 5bd8c37 | 2009-01-19 00:32:22 +0100 | [diff] [blame] | 173 | break; |
| 174 | } |
Willy Tarreau | dc340a9 | 2009-06-28 23:10:19 +0200 | [diff] [blame] | 175 | |
Willy Tarreau | a9de333 | 2009-11-28 07:47:10 +0100 | [diff] [blame] | 176 | if (errno == ENOSYS || errno == EINVAL) { |
Willy Tarreau | dc340a9 | 2009-06-28 23:10:19 +0200 | [diff] [blame] | 177 | /* splice not supported on this end, disable it */ |
| 178 | b->flags &= ~BF_KERN_SPLICING; |
| 179 | si->flags &= ~SI_FL_CAP_SPLICE; |
| 180 | put_pipe(b->pipe); |
| 181 | b->pipe = NULL; |
| 182 | return -1; |
| 183 | } |
| 184 | |
Willy Tarreau | 5bd8c37 | 2009-01-19 00:32:22 +0100 | [diff] [blame] | 185 | /* here we have another error */ |
| 186 | si->flags |= SI_FL_ERR; |
Willy Tarreau | 5bd8c37 | 2009-01-19 00:32:22 +0100 | [diff] [blame] | 187 | break; |
| 188 | } /* ret <= 0 */ |
| 189 | |
Willy Tarreau | 31971e5 | 2009-09-20 12:07:52 +0200 | [diff] [blame] | 190 | if (b->to_forward != BUF_INFINITE_FORWARD) |
| 191 | b->to_forward -= ret; |
Willy Tarreau | 5bd8c37 | 2009-01-19 00:32:22 +0100 | [diff] [blame] | 192 | b->total += ret; |
Willy Tarreau | 3eba98a | 2009-01-25 13:56:13 +0100 | [diff] [blame] | 193 | b->pipe->data += ret; |
Willy Tarreau | 5bd8c37 | 2009-01-19 00:32:22 +0100 | [diff] [blame] | 194 | b->flags |= BF_READ_PARTIAL; |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 195 | b->flags &= ~BF_OUT_EMPTY; |
Willy Tarreau | 5bd8c37 | 2009-01-19 00:32:22 +0100 | [diff] [blame] | 196 | |
Willy Tarreau | 6f4a82c | 2009-03-21 20:43:57 +0100 | [diff] [blame] | 197 | if (b->pipe->data >= SPLICE_FULL_HINT || |
| 198 | ret >= global.tune.recv_enough) { |
Willy Tarreau | 5bd8c37 | 2009-01-19 00:32:22 +0100 | [diff] [blame] | 199 | /* We've read enough of it for this time. */ |
Willy Tarreau | 5bd8c37 | 2009-01-19 00:32:22 +0100 | [diff] [blame] | 200 | break; |
| 201 | } |
| 202 | } /* while */ |
| 203 | |
Willy Tarreau | 3eba98a | 2009-01-25 13:56:13 +0100 | [diff] [blame] | 204 | if (unlikely(!b->pipe->data)) { |
| 205 | put_pipe(b->pipe); |
| 206 | b->pipe = NULL; |
| 207 | } |
| 208 | |
Willy Tarreau | 5bd8c37 | 2009-01-19 00:32:22 +0100 | [diff] [blame] | 209 | return retval; |
| 210 | } |
| 211 | |
Willy Tarreau | 6b4aad4 | 2009-01-18 21:59:13 +0100 | [diff] [blame] | 212 | #endif /* CONFIG_HAP_LINUX_SPLICE */ |
| 213 | |
| 214 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 215 | /* |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 216 | * this function is called on a read event from a stream socket. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 217 | */ |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 218 | static void sock_raw_read(struct connection *conn) |
Willy Tarreau | b277d6e | 2012-05-11 16:59:14 +0200 | [diff] [blame] | 219 | { |
Willy Tarreau | 239d718 | 2012-07-23 18:53:03 +0200 | [diff] [blame] | 220 | int fd = conn->t.sock.fd; |
Willy Tarreau | 8018471 | 2012-07-06 14:54:49 +0200 | [diff] [blame] | 221 | struct stream_interface *si = container_of(conn, struct stream_interface, conn); |
Willy Tarreau | 48adac5 | 2008-08-30 04:58:38 +0200 | [diff] [blame] | 222 | struct buffer *b = si->ib; |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 223 | int ret, max, cur_read; |
Willy Tarreau | b8949f1 | 2007-03-23 22:39:59 +0100 | [diff] [blame] | 224 | int read_poll = MAX_READ_POLL_LOOPS; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 225 | |
| 226 | #ifdef DEBUG_FULL |
Willy Tarreau | b277d6e | 2012-05-11 16:59:14 +0200 | [diff] [blame] | 227 | fprintf(stderr,"sock_raw_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] | 228 | #endif |
Willy Tarreau | 7154365 | 2009-07-14 19:55:05 +0200 | [diff] [blame] | 229 | /* stop immediately on errors. Note that we DON'T want to stop on |
| 230 | * POLL_ERR, as the poller might report a write error while there |
| 231 | * are still data available in the recv buffer. This typically |
| 232 | * happens when we send too large a request to a backend server |
| 233 | * which rejects it before reading it all. |
| 234 | */ |
Willy Tarreau | 8018471 | 2012-07-06 14:54:49 +0200 | [diff] [blame] | 235 | if (conn->flags & CO_FL_ERROR) |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 236 | goto out_error; |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 237 | |
| 238 | /* stop here if we reached the end of data */ |
| 239 | if ((fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP)) == FD_POLL_HUP) |
| 240 | goto out_shutdown_r; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 241 | |
Willy Tarreau | d06e711 | 2009-03-29 10:18:41 +0200 | [diff] [blame] | 242 | /* maybe we were called immediately after an asynchronous shutr */ |
| 243 | if (b->flags & BF_SHUTR) |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 244 | return; |
Willy Tarreau | d06e711 | 2009-03-29 10:18:41 +0200 | [diff] [blame] | 245 | |
Willy Tarreau | 5bd8c37 | 2009-01-19 00:32:22 +0100 | [diff] [blame] | 246 | #if defined(CONFIG_HAP_LINUX_SPLICE) |
Willy Tarreau | 14acc70 | 2011-05-11 20:47:24 +0200 | [diff] [blame] | 247 | if (b->to_forward >= MIN_SPLICE_FORWARD && b->flags & BF_KERN_SPLICING) { |
Willy Tarreau | 98b306b | 2009-01-25 11:11:32 +0100 | [diff] [blame] | 248 | |
| 249 | /* Under Linux, if FD_POLL_HUP is set, we have reached the end. |
| 250 | * Since older splice() implementations were buggy and returned |
| 251 | * EAGAIN on end of read, let's bypass the call to splice() now. |
| 252 | */ |
| 253 | if (fdtab[fd].ev & FD_POLL_HUP) |
| 254 | goto out_shutdown_r; |
| 255 | |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 256 | if (sock_raw_splice_in(b, si) >= 0) { |
Willy Tarreau | 5bd8c37 | 2009-01-19 00:32:22 +0100 | [diff] [blame] | 257 | if (si->flags & SI_FL_ERR) |
| 258 | goto out_error; |
| 259 | if (b->flags & BF_READ_NULL) |
| 260 | goto out_shutdown_r; |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 261 | return; |
Willy Tarreau | 5bd8c37 | 2009-01-19 00:32:22 +0100 | [diff] [blame] | 262 | } |
| 263 | /* splice not possible (anymore), let's go on on standard copy */ |
| 264 | } |
| 265 | #endif |
Willy Tarreau | 8a7af60 | 2008-05-03 23:07:14 +0200 | [diff] [blame] | 266 | cur_read = 0; |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 267 | while (1) { |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 268 | max = bi_avail(b); |
Willy Tarreau | 864e825 | 2009-12-28 17:36:37 +0100 | [diff] [blame] | 269 | |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 270 | if (!max) { |
Willy Tarreau | 864e825 | 2009-12-28 17:36:37 +0100 | [diff] [blame] | 271 | b->flags |= BF_FULL; |
| 272 | si->flags |= SI_FL_WAIT_ROOM; |
| 273 | break; |
| 274 | } |
| 275 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 276 | /* |
| 277 | * 1. compute the maximum block size we can read at once. |
| 278 | */ |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 279 | if (buffer_empty(b)) { |
Willy Tarreau | 03d60bb | 2009-01-09 11:13:00 +0100 | [diff] [blame] | 280 | /* let's realign the buffer to optimize I/O */ |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 281 | b->p = b->data; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 282 | } |
Willy Tarreau | 89fa706 | 2012-03-02 16:13:16 +0100 | [diff] [blame] | 283 | else if (b->data + b->o < b->p && |
| 284 | b->p + b->i < b->data + b->size) { |
Willy Tarreau | 864e825 | 2009-12-28 17:36:37 +0100 | [diff] [blame] | 285 | /* remaining space wraps at the end, with a moving limit */ |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 286 | if (max > b->data + b->size - (b->p + b->i)) |
| 287 | max = b->data + b->size - (b->p + b->i); |
Willy Tarreau | 9c0fe59 | 2009-01-18 16:25:31 +0100 | [diff] [blame] | 288 | } |
Willy Tarreau | 864e825 | 2009-12-28 17:36:37 +0100 | [diff] [blame] | 289 | /* else max is already OK */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 290 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 291 | /* |
| 292 | * 2. read the largest possible block |
| 293 | */ |
Willy Tarreau | cc5cfcb | 2012-05-04 21:35:27 +0200 | [diff] [blame] | 294 | ret = recv(fd, bi_end(b), max, 0); |
Willy Tarreau | d6d0690 | 2009-08-19 11:22:33 +0200 | [diff] [blame] | 295 | |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 296 | if (ret > 0) { |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 297 | b->i += ret; |
Willy Tarreau | 8a7af60 | 2008-05-03 23:07:14 +0200 | [diff] [blame] | 298 | cur_read += ret; |
Willy Tarreau | b38903c | 2008-11-23 21:33:29 +0100 | [diff] [blame] | 299 | |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 300 | /* if we're allowed to directly forward data, we must update ->o */ |
Willy Tarreau | 31971e5 | 2009-09-20 12:07:52 +0200 | [diff] [blame] | 301 | if (b->to_forward && !(b->flags & (BF_SHUTW|BF_SHUTW_NOW))) { |
| 302 | unsigned long fwd = ret; |
| 303 | if (b->to_forward != BUF_INFINITE_FORWARD) { |
| 304 | if (fwd > b->to_forward) |
| 305 | fwd = b->to_forward; |
| 306 | b->to_forward -= fwd; |
| 307 | } |
Willy Tarreau | 328582c | 2012-05-05 23:32:27 +0200 | [diff] [blame] | 308 | b_adv(b, fwd); |
Willy Tarreau | 0abebcc | 2009-01-08 00:09:41 +0100 | [diff] [blame] | 309 | } |
Willy Tarreau | f890dc9 | 2008-12-13 21:12:26 +0100 | [diff] [blame] | 310 | |
Willy Tarreau | 8018471 | 2012-07-06 14:54:49 +0200 | [diff] [blame] | 311 | if (conn->flags & CO_FL_WAIT_L4_CONN) { |
| 312 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
Willy Tarreau | 8ae52cb | 2012-05-20 10:38:46 +0200 | [diff] [blame] | 313 | si->exp = TICK_ETERNITY; |
| 314 | } |
Willy Tarreau | b38903c | 2008-11-23 21:33:29 +0100 | [diff] [blame] | 315 | |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 316 | b->flags |= BF_READ_PARTIAL; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 317 | b->total += ret; |
Willy Tarreau | 9641e8f | 2007-03-23 23:02:09 +0100 | [diff] [blame] | 318 | |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 319 | if (bi_full(b)) { |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 320 | /* The buffer is now full, there's no point in going through |
| 321 | * the loop again. |
| 322 | */ |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 323 | if (!(b->flags & BF_STREAMER_FAST) && (cur_read == buffer_len(b))) { |
Willy Tarreau | 8a7af60 | 2008-05-03 23:07:14 +0200 | [diff] [blame] | 324 | b->xfer_small = 0; |
| 325 | b->xfer_large++; |
| 326 | if (b->xfer_large >= 3) { |
| 327 | /* we call this buffer a fast streamer if it manages |
| 328 | * to be filled in one call 3 consecutive times. |
| 329 | */ |
| 330 | b->flags |= (BF_STREAMER | BF_STREAMER_FAST); |
| 331 | //fputc('+', stderr); |
| 332 | } |
| 333 | } |
| 334 | else if ((b->flags & (BF_STREAMER | BF_STREAMER_FAST)) && |
Willy Tarreau | a07a34e | 2009-08-16 23:27:46 +0200 | [diff] [blame] | 335 | (cur_read <= b->size / 2)) { |
Willy Tarreau | 8a7af60 | 2008-05-03 23:07:14 +0200 | [diff] [blame] | 336 | b->xfer_large = 0; |
| 337 | b->xfer_small++; |
| 338 | if (b->xfer_small >= 2) { |
| 339 | /* if the buffer has been at least half full twice, |
| 340 | * we receive faster than we send, so at least it |
| 341 | * is not a "fast streamer". |
| 342 | */ |
| 343 | b->flags &= ~BF_STREAMER_FAST; |
| 344 | //fputc('-', stderr); |
| 345 | } |
| 346 | } |
| 347 | else { |
| 348 | b->xfer_small = 0; |
| 349 | b->xfer_large = 0; |
| 350 | } |
Willy Tarreau | 9c0fe59 | 2009-01-18 16:25:31 +0100 | [diff] [blame] | 351 | |
| 352 | b->flags |= BF_FULL; |
Willy Tarreau | a456f2a | 2009-01-18 17:38:44 +0100 | [diff] [blame] | 353 | si->flags |= SI_FL_WAIT_ROOM; |
Willy Tarreau | 9c0fe59 | 2009-01-18 16:25:31 +0100 | [diff] [blame] | 354 | break; |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 355 | } |
| 356 | |
Willy Tarreau | ab3e1d3 | 2007-06-03 14:10:36 +0200 | [diff] [blame] | 357 | /* if too many bytes were missing from last read, it means that |
| 358 | * it's pointless trying to read again because the system does |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 359 | * not have them in buffers. BTW, if FD_POLL_HUP was present, |
| 360 | * it means that we have reached the end and that the connection |
| 361 | * is closed. |
Willy Tarreau | ab3e1d3 | 2007-06-03 14:10:36 +0200 | [diff] [blame] | 362 | */ |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 363 | if (ret < max) { |
Willy Tarreau | 8a7af60 | 2008-05-03 23:07:14 +0200 | [diff] [blame] | 364 | if ((b->flags & (BF_STREAMER | BF_STREAMER_FAST)) && |
Willy Tarreau | a07a34e | 2009-08-16 23:27:46 +0200 | [diff] [blame] | 365 | (cur_read <= b->size / 2)) { |
Willy Tarreau | 8a7af60 | 2008-05-03 23:07:14 +0200 | [diff] [blame] | 366 | b->xfer_large = 0; |
| 367 | b->xfer_small++; |
| 368 | if (b->xfer_small >= 3) { |
| 369 | /* we have read less than half of the buffer in |
| 370 | * one pass, and this happened at least 3 times. |
| 371 | * This is definitely not a streamer. |
| 372 | */ |
| 373 | b->flags &= ~(BF_STREAMER | BF_STREAMER_FAST); |
| 374 | //fputc('!', stderr); |
| 375 | } |
| 376 | } |
Willy Tarreau | 2bea3a1 | 2008-08-28 09:47:43 +0200 | [diff] [blame] | 377 | /* unfortunately, on level-triggered events, POLL_HUP |
| 378 | * is generally delivered AFTER the system buffer is |
| 379 | * empty, so this one might never match. |
| 380 | */ |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 381 | if (fdtab[fd].ev & FD_POLL_HUP) |
| 382 | goto out_shutdown_r; |
Willy Tarreau | 2bea3a1 | 2008-08-28 09:47:43 +0200 | [diff] [blame] | 383 | |
| 384 | /* if a streamer has read few data, it may be because we |
| 385 | * have exhausted system buffers. It's not worth trying |
| 386 | * again. |
| 387 | */ |
| 388 | if (b->flags & BF_STREAMER) |
| 389 | break; |
Willy Tarreau | ab3e1d3 | 2007-06-03 14:10:36 +0200 | [diff] [blame] | 390 | |
Willy Tarreau | 6f4a82c | 2009-03-21 20:43:57 +0100 | [diff] [blame] | 391 | /* generally if we read something smaller than 1 or 2 MSS, |
| 392 | * it means that either we have exhausted the system's |
| 393 | * buffers (streamer or question-response protocol) or |
| 394 | * that the connection will be closed. Streamers are |
| 395 | * easily detected so we return early. For other cases, |
| 396 | * it's still better to perform a last read to be sure, |
| 397 | * because it may save one complete poll/read/wakeup cycle |
| 398 | * in case of shutdown. |
| 399 | */ |
| 400 | if (ret < MIN_RET_FOR_READ_LOOP && b->flags & BF_STREAMER) |
| 401 | break; |
| 402 | |
| 403 | /* if we read a large block smaller than what we requested, |
| 404 | * it's almost certain we'll never get anything more. |
| 405 | */ |
| 406 | if (ret >= global.tune.recv_enough) |
| 407 | break; |
| 408 | } |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 409 | |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 410 | if ((b->flags & BF_READ_DONTWAIT) || --read_poll <= 0) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 411 | break; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 412 | } |
| 413 | else if (ret == 0) { |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 414 | /* connection closed */ |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 415 | goto out_shutdown_r; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 416 | } |
Willy Tarreau | 9f19529 | 2007-04-15 21:26:58 +0200 | [diff] [blame] | 417 | else if (errno == EAGAIN) { |
| 418 | /* Ignore EAGAIN but inform the poller that there is |
Willy Tarreau | af78d0f | 2009-01-08 10:09:08 +0100 | [diff] [blame] | 419 | * nothing to read left if we did not read much, ie |
| 420 | * less than what we were still expecting to read. |
| 421 | * But we may have done some work justifying to notify |
| 422 | * the task. |
Willy Tarreau | 9f19529 | 2007-04-15 21:26:58 +0200 | [diff] [blame] | 423 | */ |
Willy Tarreau | af78d0f | 2009-01-08 10:09:08 +0100 | [diff] [blame] | 424 | if (cur_read < MIN_RET_FOR_READ_LOOP) |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 425 | conn_data_poll_recv(conn); |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 426 | break; |
| 427 | } |
| 428 | else { |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 429 | goto out_error; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 430 | } |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 431 | } /* while (1) */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 432 | |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 433 | return; |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 434 | |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 435 | out_shutdown_r: |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 436 | /* we received a shutdown */ |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 437 | fdtab[fd].ev &= ~FD_POLL_HUP; |
| 438 | b->flags |= BF_READ_NULL; |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 439 | if (b->flags & BF_AUTO_CLOSE) |
Willy Tarreau | 418fd47 | 2009-09-06 21:37:23 +0200 | [diff] [blame] | 440 | buffer_shutw_now(b); |
Willy Tarreau | 3d8903f | 2012-08-06 17:00:18 +0200 | [diff] [blame] | 441 | sock_raw_read0(si); |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 442 | return; |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 443 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 444 | out_error: |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 445 | /* Read error on the connection, report the error and stop I/O */ |
Willy Tarreau | 8018471 | 2012-07-06 14:54:49 +0200 | [diff] [blame] | 446 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | f9dabec | 2012-08-17 17:33:53 +0200 | [diff] [blame] | 447 | conn_data_stop_both(conn); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | |
| 451 | /* |
Willy Tarreau | 0c2fc1f | 2009-01-18 15:30:37 +0100 | [diff] [blame] | 452 | * This function is called to send buffer data to a stream socket. |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 453 | * It returns -1 in case of unrecoverable error, otherwise zero. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 454 | */ |
Willy Tarreau | b277d6e | 2012-05-11 16:59:14 +0200 | [diff] [blame] | 455 | static int sock_raw_write_loop(struct stream_interface *si, struct buffer *b) |
Willy Tarreau | 0c2fc1f | 2009-01-18 15:30:37 +0100 | [diff] [blame] | 456 | { |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 457 | int write_poll = MAX_WRITE_POLL_LOOPS; |
Willy Tarreau | 0c2fc1f | 2009-01-18 15:30:37 +0100 | [diff] [blame] | 458 | int ret, max; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 459 | |
Willy Tarreau | 5bd8c37 | 2009-01-19 00:32:22 +0100 | [diff] [blame] | 460 | #if defined(CONFIG_HAP_LINUX_SPLICE) |
Willy Tarreau | 3eba98a | 2009-01-25 13:56:13 +0100 | [diff] [blame] | 461 | while (b->pipe) { |
Willy Tarreau | fb7508a | 2012-05-21 16:47:54 +0200 | [diff] [blame] | 462 | ret = splice(b->pipe->cons, NULL, si_fd(si), NULL, b->pipe->data, |
Willy Tarreau | 5bd8c37 | 2009-01-19 00:32:22 +0100 | [diff] [blame] | 463 | SPLICE_F_MOVE|SPLICE_F_NONBLOCK); |
| 464 | if (ret <= 0) { |
| 465 | if (ret == 0 || errno == EAGAIN) { |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 466 | conn_data_poll_send(&si->conn); |
| 467 | return 0; |
Willy Tarreau | 5bd8c37 | 2009-01-19 00:32:22 +0100 | [diff] [blame] | 468 | } |
| 469 | /* here we have another error */ |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 470 | return -1; |
Willy Tarreau | 5bd8c37 | 2009-01-19 00:32:22 +0100 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | b->flags |= BF_WRITE_PARTIAL; |
Willy Tarreau | 3eba98a | 2009-01-25 13:56:13 +0100 | [diff] [blame] | 474 | b->pipe->data -= ret; |
Willy Tarreau | 5bd8c37 | 2009-01-19 00:32:22 +0100 | [diff] [blame] | 475 | |
Willy Tarreau | 3eba98a | 2009-01-25 13:56:13 +0100 | [diff] [blame] | 476 | if (!b->pipe->data) { |
| 477 | put_pipe(b->pipe); |
| 478 | b->pipe = NULL; |
Willy Tarreau | 5bd8c37 | 2009-01-19 00:32:22 +0100 | [diff] [blame] | 479 | break; |
Willy Tarreau | 3eba98a | 2009-01-25 13:56:13 +0100 | [diff] [blame] | 480 | } |
Willy Tarreau | 5bd8c37 | 2009-01-19 00:32:22 +0100 | [diff] [blame] | 481 | |
| 482 | if (--write_poll <= 0) |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 483 | return 0; |
Willy Tarreau | eb9fd51 | 2011-12-11 22:11:47 +0100 | [diff] [blame] | 484 | |
| 485 | /* The only reason we did not empty the pipe is that the output |
| 486 | * buffer is full. |
| 487 | */ |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 488 | conn_data_poll_send(&si->conn); |
Willy Tarreau | eb9fd51 | 2011-12-11 22:11:47 +0100 | [diff] [blame] | 489 | return 0; |
Willy Tarreau | 5bd8c37 | 2009-01-19 00:32:22 +0100 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | /* At this point, the pipe is empty, but we may still have data pending |
| 493 | * in the normal buffer. |
| 494 | */ |
Willy Tarreau | 5bd8c37 | 2009-01-19 00:32:22 +0100 | [diff] [blame] | 495 | #endif |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 496 | if (!b->o) { |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 497 | b->flags |= BF_OUT_EMPTY; |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 498 | return 0; |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 499 | } |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 500 | |
Willy Tarreau | 0c2fc1f | 2009-01-18 15:30:37 +0100 | [diff] [blame] | 501 | /* when we're in this loop, we already know that there is no spliced |
| 502 | * data left, and that there are sendable buffered data. |
| 503 | */ |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 504 | while (1) { |
Willy Tarreau | 89fa706 | 2012-03-02 16:13:16 +0100 | [diff] [blame] | 505 | max = b->o; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 506 | |
Willy Tarreau | 89fa706 | 2012-03-02 16:13:16 +0100 | [diff] [blame] | 507 | /* outgoing data may wrap at the end */ |
| 508 | if (b->data + max > b->p) |
| 509 | max = b->data + max - b->p; |
Willy Tarreau | f890dc9 | 2008-12-13 21:12:26 +0100 | [diff] [blame] | 510 | |
Willy Tarreau | 6db06d3 | 2009-08-19 11:14:11 +0200 | [diff] [blame] | 511 | /* check if we want to inform the kernel that we're interested in |
| 512 | * sending more data after this call. We want this if : |
| 513 | * - we're about to close after this last send and want to merge |
| 514 | * the ongoing FIN with the last segment. |
| 515 | * - we know we can't send everything at once and must get back |
| 516 | * here because of unaligned data |
Willy Tarreau | d38b53b | 2010-01-03 11:18:34 +0100 | [diff] [blame] | 517 | * - there is still a finite amount of data to forward |
Willy Tarreau | 6db06d3 | 2009-08-19 11:14:11 +0200 | [diff] [blame] | 518 | * The test is arranged so that the most common case does only 2 |
| 519 | * tests. |
Willy Tarreau | fb14edc | 2009-06-14 15:24:37 +0200 | [diff] [blame] | 520 | */ |
Willy Tarreau | fb14edc | 2009-06-14 15:24:37 +0200 | [diff] [blame] | 521 | |
Willy Tarreau | face839 | 2010-01-03 11:37:54 +0100 | [diff] [blame] | 522 | if (MSG_NOSIGNAL && MSG_MORE) { |
Willy Tarreau | 6db06d3 | 2009-08-19 11:14:11 +0200 | [diff] [blame] | 523 | unsigned int send_flag = MSG_DONTWAIT | MSG_NOSIGNAL; |
| 524 | |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 525 | if ((!(b->flags & BF_NEVER_WAIT) && |
| 526 | ((b->to_forward && b->to_forward != BUF_INFINITE_FORWARD) || |
| 527 | (b->flags & BF_EXPECT_MORE))) || |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 528 | ((b->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK)) == BF_SHUTW_NOW && (max == b->o)) || |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 529 | (max != b->o)) { |
Willy Tarreau | face839 | 2010-01-03 11:37:54 +0100 | [diff] [blame] | 530 | send_flag |= MSG_MORE; |
| 531 | } |
Willy Tarreau | 6db06d3 | 2009-08-19 11:14:11 +0200 | [diff] [blame] | 532 | |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 533 | /* this flag has precedence over the rest */ |
| 534 | if (b->flags & BF_SEND_DONTWAIT) |
| 535 | send_flag &= ~MSG_MORE; |
| 536 | |
Willy Tarreau | fb7508a | 2012-05-21 16:47:54 +0200 | [diff] [blame] | 537 | ret = send(si_fd(si), bo_ptr(b), max, send_flag); |
Willy Tarreau | d6d0690 | 2009-08-19 11:22:33 +0200 | [diff] [blame] | 538 | } else { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 539 | int skerr; |
| 540 | socklen_t lskerr = sizeof(skerr); |
| 541 | |
Willy Tarreau | fb7508a | 2012-05-21 16:47:54 +0200 | [diff] [blame] | 542 | ret = getsockopt(si_fd(si), SOL_SOCKET, SO_ERROR, &skerr, &lskerr); |
Willy Tarreau | c642348 | 2006-10-15 14:59:03 +0200 | [diff] [blame] | 543 | if (ret == -1 || skerr) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 544 | ret = -1; |
| 545 | else |
Willy Tarreau | fb7508a | 2012-05-21 16:47:54 +0200 | [diff] [blame] | 546 | ret = send(si_fd(si), bo_ptr(b), max, MSG_DONTWAIT); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 547 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 548 | |
| 549 | if (ret > 0) { |
Willy Tarreau | 505e34a | 2012-07-06 10:17:53 +0200 | [diff] [blame] | 550 | if (si->conn.flags & CO_FL_WAIT_L4_CONN) { |
| 551 | si->conn.flags &= ~CO_FL_WAIT_L4_CONN; |
Willy Tarreau | 8ae52cb | 2012-05-20 10:38:46 +0200 | [diff] [blame] | 552 | si->exp = TICK_ETERNITY; |
| 553 | } |
Willy Tarreau | b38903c | 2008-11-23 21:33:29 +0100 | [diff] [blame] | 554 | |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 555 | b->flags |= BF_WRITE_PARTIAL; |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 556 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 557 | b->o -= ret; |
| 558 | if (likely(!buffer_len(b))) |
Willy Tarreau | 0c2fc1f | 2009-01-18 15:30:37 +0100 | [diff] [blame] | 559 | /* optimize data alignment in the buffer */ |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 560 | b->p = b->data; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 561 | |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 562 | if (likely(!bi_full(b))) |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 563 | b->flags &= ~BF_FULL; |
| 564 | |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 565 | if (!b->o) { |
Willy Tarreau | f17810e | 2012-03-09 18:10:44 +0100 | [diff] [blame] | 566 | /* Always clear both flags once everything has been sent, they're one-shot */ |
| 567 | b->flags &= ~(BF_EXPECT_MORE | BF_SEND_DONTWAIT); |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 568 | if (likely(!b->pipe)) |
| 569 | b->flags |= BF_OUT_EMPTY; |
Willy Tarreau | 0c2fc1f | 2009-01-18 15:30:37 +0100 | [diff] [blame] | 570 | break; |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 571 | } |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 572 | |
Willy Tarreau | ab3e1d3 | 2007-06-03 14:10:36 +0200 | [diff] [blame] | 573 | /* if the system buffer is full, don't insist */ |
| 574 | if (ret < max) |
| 575 | break; |
| 576 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 577 | if (--write_poll <= 0) |
| 578 | break; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 579 | } |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 580 | else if (ret == 0 || errno == EAGAIN) { |
Willy Tarreau | 0c2fc1f | 2009-01-18 15:30:37 +0100 | [diff] [blame] | 581 | /* nothing written, we need to poll for write first */ |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 582 | conn_data_poll_send(&si->conn); |
| 583 | return 0; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 584 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 585 | else { |
Willy Tarreau | 0c2fc1f | 2009-01-18 15:30:37 +0100 | [diff] [blame] | 586 | /* bad, we got an error */ |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 587 | return -1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 588 | } |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 589 | } /* while (1) */ |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 590 | return 0; |
Willy Tarreau | 0c2fc1f | 2009-01-18 15:30:37 +0100 | [diff] [blame] | 591 | } |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 592 | |
Willy Tarreau | 0c2fc1f | 2009-01-18 15:30:37 +0100 | [diff] [blame] | 593 | |
| 594 | /* |
| 595 | * This function is called on a write event from a stream socket. |
Willy Tarreau | 0c2fc1f | 2009-01-18 15:30:37 +0100 | [diff] [blame] | 596 | */ |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 597 | static void sock_raw_write(struct connection *conn) |
Willy Tarreau | 0c2fc1f | 2009-01-18 15:30:37 +0100 | [diff] [blame] | 598 | { |
Willy Tarreau | 8018471 | 2012-07-06 14:54:49 +0200 | [diff] [blame] | 599 | struct stream_interface *si = container_of(conn, struct stream_interface, conn); |
Willy Tarreau | 0c2fc1f | 2009-01-18 15:30:37 +0100 | [diff] [blame] | 600 | struct buffer *b = si->ob; |
Willy Tarreau | 0c2fc1f | 2009-01-18 15:30:37 +0100 | [diff] [blame] | 601 | |
| 602 | #ifdef DEBUG_FULL |
Willy Tarreau | b277d6e | 2012-05-11 16:59:14 +0200 | [diff] [blame] | 603 | fprintf(stderr,"sock_raw_write : fd=%d, owner=%p\n", fd, fdtab[fd].owner); |
Willy Tarreau | 0c2fc1f | 2009-01-18 15:30:37 +0100 | [diff] [blame] | 604 | #endif |
| 605 | |
Willy Tarreau | 8018471 | 2012-07-06 14:54:49 +0200 | [diff] [blame] | 606 | if (conn->flags & CO_FL_ERROR) |
Willy Tarreau | 0c2fc1f | 2009-01-18 15:30:37 +0100 | [diff] [blame] | 607 | goto out_error; |
| 608 | |
Willy Tarreau | d06e711 | 2009-03-29 10:18:41 +0200 | [diff] [blame] | 609 | /* we might have been called just after an asynchronous shutw */ |
| 610 | if (b->flags & BF_SHUTW) |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 611 | return; |
Willy Tarreau | d06e711 | 2009-03-29 10:18:41 +0200 | [diff] [blame] | 612 | |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 613 | if (sock_raw_write_loop(si, b) < 0) |
Willy Tarreau | eeda90e | 2012-05-11 19:53:32 +0200 | [diff] [blame] | 614 | goto out_error; |
Willy Tarreau | 0c2fc1f | 2009-01-18 15:30:37 +0100 | [diff] [blame] | 615 | |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 616 | /* OK all done */ |
| 617 | return; |
Willy Tarreau | ac128fe | 2009-01-09 13:05:19 +0100 | [diff] [blame] | 618 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 619 | out_error: |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 620 | /* Write error on the connection, report the error and stop I/O */ |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 621 | |
Willy Tarreau | 8018471 | 2012-07-06 14:54:49 +0200 | [diff] [blame] | 622 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | f9dabec | 2012-08-17 17:33:53 +0200 | [diff] [blame] | 623 | conn_data_stop_both(conn); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 624 | } |
| 625 | |
Willy Tarreau | 48adac5 | 2008-08-30 04:58:38 +0200 | [diff] [blame] | 626 | /* |
Willy Tarreau | 3d8903f | 2012-08-06 17:00:18 +0200 | [diff] [blame] | 627 | * This function propagates a null read received on a connection. It updates |
| 628 | * the stream interface. If the stream interface has SI_FL_NOHALF, we also |
| 629 | * forward the close to the write side. |
| 630 | */ |
| 631 | static void sock_raw_read0(struct stream_interface *si) |
| 632 | { |
| 633 | si->ib->flags &= ~BF_SHUTR_NOW; |
| 634 | if (si->ib->flags & BF_SHUTR) |
| 635 | return; |
| 636 | si->ib->flags |= BF_SHUTR; |
| 637 | si->ib->rex = TICK_ETERNITY; |
| 638 | si->flags &= ~SI_FL_WAIT_ROOM; |
| 639 | |
| 640 | if (si->state != SI_ST_EST && si->state != SI_ST_CON) |
| 641 | return; |
| 642 | |
| 643 | if (si->ob->flags & BF_SHUTW) |
| 644 | goto do_close; |
| 645 | |
| 646 | if (si->flags & SI_FL_NOHALF) { |
| 647 | /* we have to shut before closing, otherwise some short messages |
| 648 | * may never leave the system, especially when there are remaining |
| 649 | * unread data in the socket input buffer, or when nolinger is set. |
| 650 | * However, if SI_FL_NOLINGER is explicitly set, we know there is |
| 651 | * no risk so we close both sides immediately. |
| 652 | */ |
| 653 | if (si->flags & SI_FL_NOLINGER) { |
| 654 | si->flags &= ~SI_FL_NOLINGER; |
| 655 | setsockopt(si_fd(si), SOL_SOCKET, SO_LINGER, |
| 656 | (struct linger *) &nolinger, sizeof(struct linger)); |
| 657 | } |
| 658 | goto do_close; |
| 659 | } |
| 660 | |
| 661 | /* otherwise that's just a normal read shutdown */ |
Willy Tarreau | f9dabec | 2012-08-17 17:33:53 +0200 | [diff] [blame] | 662 | conn_data_stop_recv(&si->conn); |
Willy Tarreau | 3d8903f | 2012-08-06 17:00:18 +0200 | [diff] [blame] | 663 | return; |
| 664 | |
| 665 | do_close: |
| 666 | conn_data_close(&si->conn); |
| 667 | fd_delete(si_fd(si)); |
| 668 | si->state = SI_ST_DIS; |
| 669 | si->exp = TICK_ETERNITY; |
| 670 | if (si->release) |
| 671 | si->release(si); |
| 672 | return; |
| 673 | } |
| 674 | |
Willy Tarreau | 3ffeba1 | 2008-12-14 14:42:35 +0100 | [diff] [blame] | 675 | /* This function is used for inter-stream-interface calls. It is called by the |
Willy Tarreau | 3ffeba1 | 2008-12-14 14:42:35 +0100 | [diff] [blame] | 676 | * producer to inform the consumer side that it may be interested in checking |
| 677 | * for data in the buffer. Note that it intentionally does not update timeouts, |
| 678 | * so that we can still check them later at wake-up. |
| 679 | */ |
Willy Tarreau | b277d6e | 2012-05-11 16:59:14 +0200 | [diff] [blame] | 680 | static void sock_raw_chk_snd(struct stream_interface *si) |
Willy Tarreau | 3ffeba1 | 2008-12-14 14:42:35 +0100 | [diff] [blame] | 681 | { |
| 682 | struct buffer *ob = si->ob; |
| 683 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 684 | DPRINTF(stderr,"[%u] %s: fd=%d owner=%p ib=%p, ob=%p, exp(r,w)=%u,%u ibf=%08x obf=%08x ibh=%d ibt=%d obh=%d obd=%d si=%d\n", |
Willy Tarreau | 3ffeba1 | 2008-12-14 14:42:35 +0100 | [diff] [blame] | 685 | now_ms, __FUNCTION__, |
Willy Tarreau | fb7508a | 2012-05-21 16:47:54 +0200 | [diff] [blame] | 686 | si_fd(si), fdtab[si_fd(si)].owner, |
Vincenzo Farruggia | 9b97cff | 2009-01-30 16:49:10 +0000 | [diff] [blame] | 687 | si->ib, ob, |
| 688 | si->ib->rex, ob->wex, |
| 689 | si->ib->flags, ob->flags, |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 690 | si->ib->i, si->ib->o, ob->i, ob->o, si->state); |
Willy Tarreau | 3ffeba1 | 2008-12-14 14:42:35 +0100 | [diff] [blame] | 691 | |
| 692 | if (unlikely(si->state != SI_ST_EST || (ob->flags & BF_SHUTW))) |
| 693 | return; |
| 694 | |
Willy Tarreau | a190d59 | 2012-05-20 18:35:19 +0200 | [diff] [blame] | 695 | if (unlikely(ob->flags & BF_OUT_EMPTY)) /* called with nothing to send ! */ |
Willy Tarreau | eb9fd51 | 2011-12-11 22:11:47 +0100 | [diff] [blame] | 696 | return; |
| 697 | |
| 698 | if (!ob->pipe && /* spliced data wants to be forwarded ASAP */ |
| 699 | (!(si->flags & SI_FL_WAIT_DATA) || /* not waiting for data */ |
Willy Tarreau | fb7508a | 2012-05-21 16:47:54 +0200 | [diff] [blame] | 700 | (fdtab[si_fd(si)].ev & FD_POLL_OUT))) /* we'll be called anyway */ |
Willy Tarreau | a456f2a | 2009-01-18 17:38:44 +0100 | [diff] [blame] | 701 | return; |
| 702 | |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 703 | if (sock_raw_write_loop(si, ob) < 0) { |
Willy Tarreau | a456f2a | 2009-01-18 17:38:44 +0100 | [diff] [blame] | 704 | /* Write error on the file descriptor. We mark the FD as STERROR so |
| 705 | * that we don't use it anymore and we notify the task. |
| 706 | */ |
Willy Tarreau | 505e34a | 2012-07-06 10:17:53 +0200 | [diff] [blame] | 707 | si->conn.flags |= CO_FL_ERROR; |
Willy Tarreau | fb7508a | 2012-05-21 16:47:54 +0200 | [diff] [blame] | 708 | fdtab[si_fd(si)].ev &= ~FD_POLL_STICKY; |
Willy Tarreau | f9dabec | 2012-08-17 17:33:53 +0200 | [diff] [blame] | 709 | conn_data_stop_both(&si->conn); |
Willy Tarreau | a456f2a | 2009-01-18 17:38:44 +0100 | [diff] [blame] | 710 | si->flags |= SI_FL_ERR; |
| 711 | goto out_wakeup; |
| 712 | } |
| 713 | |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 714 | /* OK, so now we know that some data might have been sent, and that we may |
| 715 | * have to poll first. We have to do that too if the buffer is not empty. |
Willy Tarreau | c54aef3 | 2009-07-27 20:08:06 +0200 | [diff] [blame] | 716 | */ |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 717 | if (ob->flags & BF_OUT_EMPTY) { |
Willy Tarreau | a456f2a | 2009-01-18 17:38:44 +0100 | [diff] [blame] | 718 | /* the connection is established but we can't write. Either the |
| 719 | * buffer is empty, or we just refrain from sending because the |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 720 | * ->o limit was reached. Maybe we just wrote the last |
Willy Tarreau | a456f2a | 2009-01-18 17:38:44 +0100 | [diff] [blame] | 721 | * chunk and need to close. |
| 722 | */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 723 | if (((ob->flags & (BF_SHUTW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTW_NOW)) == |
| 724 | (BF_AUTO_CLOSE|BF_SHUTW_NOW)) && |
Willy Tarreau | a456f2a | 2009-01-18 17:38:44 +0100 | [diff] [blame] | 725 | (si->state == SI_ST_EST)) { |
Willy Tarreau | 4a36b56 | 2012-08-06 19:31:45 +0200 | [diff] [blame] | 726 | si_shutw(si); |
Willy Tarreau | a456f2a | 2009-01-18 17:38:44 +0100 | [diff] [blame] | 727 | goto out_wakeup; |
| 728 | } |
Willy Tarreau | d06e711 | 2009-03-29 10:18:41 +0200 | [diff] [blame] | 729 | |
Willy Tarreau | 59454bf | 2009-09-20 11:13:40 +0200 | [diff] [blame] | 730 | if ((ob->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_FULL|BF_HIJACK)) == 0) |
Willy Tarreau | 3ffeba1 | 2008-12-14 14:42:35 +0100 | [diff] [blame] | 731 | si->flags |= SI_FL_WAIT_DATA; |
Willy Tarreau | a456f2a | 2009-01-18 17:38:44 +0100 | [diff] [blame] | 732 | ob->wex = TICK_ETERNITY; |
Willy Tarreau | 3ffeba1 | 2008-12-14 14:42:35 +0100 | [diff] [blame] | 733 | } |
| 734 | else { |
Willy Tarreau | c54aef3 | 2009-07-27 20:08:06 +0200 | [diff] [blame] | 735 | /* Otherwise there are remaining data to be sent in the buffer, |
| 736 | * which means we have to poll before doing so. |
| 737 | */ |
Willy Tarreau | f9dabec | 2012-08-17 17:33:53 +0200 | [diff] [blame] | 738 | conn_data_want_send(&si->conn); |
Willy Tarreau | c54aef3 | 2009-07-27 20:08:06 +0200 | [diff] [blame] | 739 | si->flags &= ~SI_FL_WAIT_DATA; |
| 740 | if (!tick_isset(ob->wex)) |
| 741 | ob->wex = tick_add_ifset(now_ms, ob->wto); |
Willy Tarreau | 3ffeba1 | 2008-12-14 14:42:35 +0100 | [diff] [blame] | 742 | } |
Willy Tarreau | a456f2a | 2009-01-18 17:38:44 +0100 | [diff] [blame] | 743 | |
Willy Tarreau | c961946 | 2009-03-09 22:40:57 +0100 | [diff] [blame] | 744 | if (likely(ob->flags & BF_WRITE_ACTIVITY)) { |
| 745 | /* update timeout if we have written something */ |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 746 | if ((ob->flags & (BF_OUT_EMPTY|BF_SHUTW|BF_WRITE_PARTIAL)) == BF_WRITE_PARTIAL) |
Willy Tarreau | c961946 | 2009-03-09 22:40:57 +0100 | [diff] [blame] | 747 | ob->wex = tick_add_ifset(now_ms, ob->wto); |
| 748 | |
Willy Tarreau | f27b5ea | 2009-10-03 22:01:18 +0200 | [diff] [blame] | 749 | if (tick_isset(si->ib->rex) && !(si->flags & SI_FL_INDEP_STR)) { |
Willy Tarreau | c961946 | 2009-03-09 22:40:57 +0100 | [diff] [blame] | 750 | /* Note: to prevent the client from expiring read timeouts |
Willy Tarreau | f27b5ea | 2009-10-03 22:01:18 +0200 | [diff] [blame] | 751 | * during writes, we refresh it. We only do this if the |
Jamie Gloudon | 801a0a3 | 2012-08-25 00:18:33 -0400 | [diff] [blame] | 752 | * interface is not configured for "independent streams", |
Willy Tarreau | f27b5ea | 2009-10-03 22:01:18 +0200 | [diff] [blame] | 753 | * because for some applications it's better not to do this, |
| 754 | * for instance when continuously exchanging small amounts |
| 755 | * of data which can full the socket buffers long before a |
| 756 | * write timeout is detected. |
Willy Tarreau | c961946 | 2009-03-09 22:40:57 +0100 | [diff] [blame] | 757 | */ |
| 758 | si->ib->rex = tick_add_ifset(now_ms, si->ib->rto); |
| 759 | } |
| 760 | } |
| 761 | |
Willy Tarreau | a456f2a | 2009-01-18 17:38:44 +0100 | [diff] [blame] | 762 | /* in case of special condition (error, shutdown, end of write...), we |
| 763 | * have to notify the task. |
| 764 | */ |
| 765 | if (likely((ob->flags & (BF_WRITE_NULL|BF_WRITE_ERROR|BF_SHUTW)) || |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 766 | ((ob->flags & BF_OUT_EMPTY) && !ob->to_forward) || |
Willy Tarreau | a456f2a | 2009-01-18 17:38:44 +0100 | [diff] [blame] | 767 | si->state != SI_ST_EST)) { |
| 768 | out_wakeup: |
Willy Tarreau | 89f7ef2 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 769 | if (!(si->flags & SI_FL_DONT_WAKE) && si->owner) |
| 770 | task_wakeup(si->owner, TASK_WOKEN_IO); |
Willy Tarreau | a456f2a | 2009-01-18 17:38:44 +0100 | [diff] [blame] | 771 | } |
Willy Tarreau | 3ffeba1 | 2008-12-14 14:42:35 +0100 | [diff] [blame] | 772 | } |
| 773 | |
Willy Tarreau | 5c979a9 | 2012-05-07 17:15:39 +0200 | [diff] [blame] | 774 | /* stream sock operations */ |
Willy Tarreau | b277d6e | 2012-05-11 16:59:14 +0200 | [diff] [blame] | 775 | struct sock_ops sock_raw = { |
Willy Tarreau | 100c467 | 2012-08-20 12:06:26 +0200 | [diff] [blame] | 776 | .update = stream_int_update_conn, |
Willy Tarreau | 4a36b56 | 2012-08-06 19:31:45 +0200 | [diff] [blame] | 777 | .shutr = NULL, |
| 778 | .shutw = NULL, |
Willy Tarreau | 46a8d92 | 2012-08-20 12:38:36 +0200 | [diff] [blame^] | 779 | .chk_rcv = stream_int_chk_rcv_conn, |
Willy Tarreau | b277d6e | 2012-05-11 16:59:14 +0200 | [diff] [blame] | 780 | .chk_snd = sock_raw_chk_snd, |
| 781 | .read = sock_raw_read, |
| 782 | .write = sock_raw_write, |
Willy Tarreau | 2420827 | 2012-05-21 17:28:50 +0200 | [diff] [blame] | 783 | .close = NULL, |
Willy Tarreau | 5c979a9 | 2012-05-07 17:15:39 +0200 | [diff] [blame] | 784 | }; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 785 | |
| 786 | /* |
| 787 | * Local variables: |
| 788 | * c-indent-level: 8 |
| 789 | * c-basic-offset: 8 |
| 790 | * End: |
| 791 | */ |