blob: 65fe66597992f2f4c03be84f51bae3d162b563e6 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
Willy Tarreauc7e42382012-08-24 19:22:53 +02002 * Channel management functions.
Willy Tarreaubaaee002006-06-26 02:48:02 +02003 *
Willy Tarreauc7e42382012-08-24 19:22:53 +02004 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
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
Krzysztof Piotr Oledzkiba8d7d32009-10-10 21:06:03 +020013#include <ctype.h>
Willy Tarreauc0dde7a2007-01-01 21:38:07 +010014#include <stdarg.h>
15#include <stdio.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020016#include <string.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020017
18#include <common/config.h>
Willy Tarreau7341d942007-05-13 19:56:02 +020019#include <common/memory.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020020#include <common/buffer.h>
21#include <proto/channel.h>
Willy Tarreau27a674e2009-08-17 07:23:33 +020022#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020023
Willy Tarreauc7e42382012-08-24 19:22:53 +020024
Willy Tarreau8263d2b2012-08-28 00:06:31 +020025struct pool_head *pool2_channel;
Willy Tarreau7341d942007-05-13 19:56:02 +020026
27
28/* perform minimal intializations, report 0 in case of error, 1 if OK. */
Willy Tarreau8263d2b2012-08-28 00:06:31 +020029int init_channel()
Willy Tarreau7341d942007-05-13 19:56:02 +020030{
Willy Tarreau8263d2b2012-08-28 00:06:31 +020031 pool2_channel = create_pool("channel", sizeof(struct channel) + global.tune.bufsize, MEM_F_SHARED);
32 return pool2_channel != NULL;
Willy Tarreau7341d942007-05-13 19:56:02 +020033}
34
Willy Tarreau8263d2b2012-08-28 00:06:31 +020035/* Schedule up to <bytes> more bytes to be forwarded via the channel without
36 * notifying the owner task. Any data pending in the buffer are scheduled to be
37 * sent as well, in the limit of the number of bytes to forward. This must be
38 * the only method to use to schedule bytes to be forwarded. If the requested
39 * number is too large, it is automatically adjusted. The number of bytes taken
40 * into account is returned. Directly touching ->to_forward will cause lockups
41 * when buf->o goes down to zero if nobody is ready to push the remaining data.
Willy Tarreau0bc34932011-03-28 16:25:58 +020042 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +020043unsigned long long channel_forward(struct channel *buf, unsigned long long bytes)
Willy Tarreau0bc34932011-03-28 16:25:58 +020044{
Willy Tarreau0bc34932011-03-28 16:25:58 +020045 unsigned int new_forward;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +010046 unsigned int forwarded;
Willy Tarreau328582c2012-05-05 23:32:27 +020047 unsigned int bytes32;
Willy Tarreau0bc34932011-03-28 16:25:58 +020048
Willy Tarreau328582c2012-05-05 23:32:27 +020049 bytes32 = bytes;
50
51 /* hint: avoid comparisons on long long for the fast case, since if the
52 * length does not fit in an unsigned it, it will never be forwarded at
53 * once anyway.
54 */
55 if (bytes <= ~0U) {
Willy Tarreau572bf902012-07-02 17:01:20 +020056 if (bytes32 <= buf->buf.i) {
Willy Tarreau328582c2012-05-05 23:32:27 +020057 /* OK this amount of bytes might be forwarded at once */
58 if (!bytes32)
59 return 0;
Willy Tarreaua75bcef2012-08-24 22:56:11 +020060 b_adv(&buf->buf, bytes32);
Willy Tarreau328582c2012-05-05 23:32:27 +020061 return bytes;
62 }
Willy Tarreau0bc34932011-03-28 16:25:58 +020063 }
64
Willy Tarreau572bf902012-07-02 17:01:20 +020065 forwarded = buf->buf.i;
Willy Tarreaua75bcef2012-08-24 22:56:11 +020066 b_adv(&buf->buf, buf->buf.i);
Willy Tarreau0bc34932011-03-28 16:25:58 +020067
Willy Tarreau0bc34932011-03-28 16:25:58 +020068 /* Note: the case below is the only case where we may return
69 * a byte count that does not fit into a 32-bit number.
70 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +020071 if (likely(buf->to_forward == CHN_INFINITE_FORWARD))
Willy Tarreau0bc34932011-03-28 16:25:58 +020072 return bytes;
73
Willy Tarreau03cdb7c2012-08-27 23:14:58 +020074 if (likely(bytes == CHN_INFINITE_FORWARD)) {
Willy Tarreau02d6cfc2012-03-01 18:19:58 +010075 buf->to_forward = bytes;
76 return bytes;
77 }
78
79 new_forward = buf->to_forward + bytes - forwarded;
80 bytes = forwarded; /* at least those bytes were scheduled */
Willy Tarreau0bc34932011-03-28 16:25:58 +020081
82 if (new_forward <= buf->to_forward) {
83 /* integer overflow detected, let's assume no more than 2G at once */
84 new_forward = MID_RANGE(new_forward);
85 }
86
87 if (new_forward > buf->to_forward) {
88 bytes += new_forward - buf->to_forward;
89 buf->to_forward = new_forward;
90 }
91 return bytes;
92}
Willy Tarreaubaaee002006-06-26 02:48:02 +020093
Willy Tarreau8263d2b2012-08-28 00:06:31 +020094/* writes <len> bytes from message <msg> to the channel's buffer. Returns -1 in
95 * case of success, -2 if the message is larger than the buffer size, or the
96 * number of bytes available otherwise. The send limit is automatically
97 * adjusted to the amount of data written. FIXME-20060521: handle unaligned
98 * data. Note: this function appends data to the buffer's output and possibly
99 * overwrites any pending input data which are assumed not to exist.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200100 */
Willy Tarreau7421efb2012-07-02 15:11:27 +0200101int bo_inject(struct channel *buf, const char *msg, int len)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200102{
103 int max;
104
Willy Tarreauaeac3192009-08-31 08:09:57 +0200105 if (len == 0)
106 return -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200107
Willy Tarreau572bf902012-07-02 17:01:20 +0200108 if (len > buf->buf.size) {
Willy Tarreau078e2942009-08-18 07:19:39 +0200109 /* we can't write this chunk and will never be able to, because
110 * it is larger than the buffer. This must be reported as an
111 * error. Then we return -2 so that writers that don't care can
112 * ignore it and go on, and others can check for this value.
113 */
114 return -2;
115 }
116
Willy Tarreau572bf902012-07-02 17:01:20 +0200117 max = buffer_realign(&buf->buf);
Willy Tarreauaeac3192009-08-31 08:09:57 +0200118
Willy Tarreaubaaee002006-06-26 02:48:02 +0200119 if (len > max)
120 return max;
121
Willy Tarreau572bf902012-07-02 17:01:20 +0200122 memcpy(buf->buf.p, msg, len);
123 buf->buf.o += len;
124 buf->buf.p = b_ptr(&buf->buf, len);
Willy Tarreau35d66b02007-01-02 00:28:21 +0100125 buf->total += len;
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200126 return -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200127}
128
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200129/* Tries to copy character <c> into the channel's buffer after some length
130 * controls. The buf->o and to_forward pointers are updated. If the channel
131 * input is closed, -2 is returned. If there is not enough room left in the
132 * buffer, -1 is returned. Otherwise the number of bytes copied is returned
133 * (1). Channel flag READ_PARTIAL is updated if some data can be transferred.
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100134 */
Willy Tarreau7421efb2012-07-02 15:11:27 +0200135int bi_putchr(struct channel *buf, char c)
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100136{
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200137 if (unlikely(channel_input_closed(buf)))
Willy Tarreau74b08c92010-09-08 17:04:31 +0200138 return -2;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100139
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +0200140 if (channel_full(buf))
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200141 return -1;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100142
Willy Tarreau572bf902012-07-02 17:01:20 +0200143 *bi_end(&buf->buf) = c;
Willy Tarreau74b08c92010-09-08 17:04:31 +0200144
Willy Tarreau572bf902012-07-02 17:01:20 +0200145 buf->buf.i++;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200146 buf->flags |= CF_READ_PARTIAL;
Willy Tarreau74b08c92010-09-08 17:04:31 +0200147
Willy Tarreau74b08c92010-09-08 17:04:31 +0200148 if (buf->to_forward >= 1) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200149 if (buf->to_forward != CHN_INFINITE_FORWARD)
Willy Tarreau74b08c92010-09-08 17:04:31 +0200150 buf->to_forward--;
Willy Tarreaua75bcef2012-08-24 22:56:11 +0200151 b_adv(&buf->buf, 1);
Willy Tarreau74b08c92010-09-08 17:04:31 +0200152 }
153
154 buf->total++;
155 return 1;
156}
157
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200158/* Tries to copy block <blk> at once into the channel's buffer after length
159 * controls. The buf->o and to_forward pointers are updated. If the channel
160 * input is closed, -2 is returned. If the block is too large for this buffer,
161 * -3 is returned. If there is not enough room left in the buffer, -1 is
162 * returned. Otherwise the number of bytes copied is returned (0 being a valid
163 * number). Channel flag READ_PARTIAL is updated if some data can be
164 * transferred.
Willy Tarreau74b08c92010-09-08 17:04:31 +0200165 */
Willy Tarreau7421efb2012-07-02 15:11:27 +0200166int bi_putblk(struct channel *buf, const char *blk, int len)
Willy Tarreau74b08c92010-09-08 17:04:31 +0200167{
168 int max;
169
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200170 if (unlikely(channel_input_closed(buf)))
Willy Tarreau74b08c92010-09-08 17:04:31 +0200171 return -2;
172
Willy Tarreau591fedc2010-08-10 15:28:21 +0200173 max = buffer_max_len(buf);
Willy Tarreau572bf902012-07-02 17:01:20 +0200174 if (unlikely(len > max - buffer_len(&buf->buf))) {
Willy Tarreau591fedc2010-08-10 15:28:21 +0200175 /* we can't write this chunk right now because the buffer is
176 * almost full or because the block is too large. Return the
177 * available space or -2 if impossible.
Willy Tarreau078e2942009-08-18 07:19:39 +0200178 */
Willy Tarreau591fedc2010-08-10 15:28:21 +0200179 if (len > max)
Willy Tarreau74b08c92010-09-08 17:04:31 +0200180 return -3;
Willy Tarreau078e2942009-08-18 07:19:39 +0200181
Willy Tarreau74b08c92010-09-08 17:04:31 +0200182 return -1;
Willy Tarreau591fedc2010-08-10 15:28:21 +0200183 }
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100184
Willy Tarreau74b08c92010-09-08 17:04:31 +0200185 if (unlikely(len == 0))
186 return 0;
187
Willy Tarreau591fedc2010-08-10 15:28:21 +0200188 /* OK so the data fits in the buffer in one or two blocks */
Willy Tarreau572bf902012-07-02 17:01:20 +0200189 max = buffer_contig_space_with_res(&buf->buf, buf->buf.size - max);
190 memcpy(bi_end(&buf->buf), blk, MIN(len, max));
Willy Tarreauaeac3192009-08-31 08:09:57 +0200191 if (len > max)
Willy Tarreau572bf902012-07-02 17:01:20 +0200192 memcpy(buf->buf.data, blk + max, len - max);
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100193
Willy Tarreau572bf902012-07-02 17:01:20 +0200194 buf->buf.i += len;
Willy Tarreauaeac3192009-08-31 08:09:57 +0200195 buf->total += len;
Willy Tarreau31971e52009-09-20 12:07:52 +0200196 if (buf->to_forward) {
197 unsigned long fwd = len;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200198 if (buf->to_forward != CHN_INFINITE_FORWARD) {
Willy Tarreau31971e52009-09-20 12:07:52 +0200199 if (fwd > buf->to_forward)
200 fwd = buf->to_forward;
201 buf->to_forward -= fwd;
202 }
Willy Tarreaua75bcef2012-08-24 22:56:11 +0200203 b_adv(&buf->buf, fwd);
Willy Tarreauaeac3192009-08-31 08:09:57 +0200204 }
205
Willy Tarreaufb0e9202009-09-23 23:47:55 +0200206 /* notify that some data was read from the SI into the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200207 buf->flags |= CF_READ_PARTIAL;
Willy Tarreau74b08c92010-09-08 17:04:31 +0200208 return len;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100209}
210
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200211/* Gets one text line out of a channel's buffer from a stream interface.
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200212 * Return values :
213 * >0 : number of bytes read. Includes the \n if present before len or end.
Willy Tarreau74b08c92010-09-08 17:04:31 +0200214 * =0 : no '\n' before end found. <str> is left undefined.
215 * <0 : no more bytes readable because output is shut.
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200216 * The channel status is not changed. The caller must call bo_skip() to
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200217 * update it. The '\n' is waited for as long as neither the buffer nor the
218 * output are full. If either of them is full, the string may be returned
219 * as is, without the '\n'.
220 */
Willy Tarreau7421efb2012-07-02 15:11:27 +0200221int bo_getline(struct channel *buf, char *str, int len)
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200222{
223 int ret, max;
224 char *p;
225
226 ret = 0;
227 max = len;
Willy Tarreau74b08c92010-09-08 17:04:31 +0200228
229 /* closed or empty + imminent close = -1; empty = 0 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200230 if (unlikely((buf->flags & CF_SHUTW) || channel_is_empty(buf))) {
231 if (buf->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200232 ret = -1;
233 goto out;
234 }
235
Willy Tarreau572bf902012-07-02 17:01:20 +0200236 p = bo_ptr(&buf->buf);
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200237
Willy Tarreau572bf902012-07-02 17:01:20 +0200238 if (max > buf->buf.o) {
239 max = buf->buf.o;
Willy Tarreau2e1dd3d2009-09-23 22:56:07 +0200240 str[max-1] = 0;
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200241 }
242 while (max) {
243 *str++ = *p;
244 ret++;
245 max--;
246
247 if (*p == '\n')
248 break;
Willy Tarreau572bf902012-07-02 17:01:20 +0200249 p = buffer_wrap_add(&buf->buf, p + 1);
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200250 }
Willy Tarreau572bf902012-07-02 17:01:20 +0200251 if (ret > 0 && ret < len && ret < buf->buf.o &&
Willy Tarreau2e1dd3d2009-09-23 22:56:07 +0200252 *(str-1) != '\n' &&
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200253 !(buf->flags & (CF_SHUTW|CF_SHUTW_NOW)))
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200254 ret = 0;
255 out:
256 if (max)
257 *str = 0;
258 return ret;
259}
260
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200261/* Gets one full block of data at once from a channel's buffer, optionally from
262 * a specific offset. Return values :
Willy Tarreau74b08c92010-09-08 17:04:31 +0200263 * >0 : number of bytes read, equal to requested size.
264 * =0 : not enough data available. <blk> is left undefined.
265 * <0 : no more bytes readable because output is shut.
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200266 * The channel status is not changed. The caller must call bo_skip() to
Willy Tarreau74b08c92010-09-08 17:04:31 +0200267 * update it.
268 */
Willy Tarreau7421efb2012-07-02 15:11:27 +0200269int bo_getblk(struct channel *buf, char *blk, int len, int offset)
Willy Tarreau74b08c92010-09-08 17:04:31 +0200270{
271 int firstblock;
272
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200273 if (buf->flags & CF_SHUTW)
Willy Tarreau74b08c92010-09-08 17:04:31 +0200274 return -1;
275
Willy Tarreau572bf902012-07-02 17:01:20 +0200276 if (len + offset > buf->buf.o) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200277 if (buf->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreau74b08c92010-09-08 17:04:31 +0200278 return -1;
279 return 0;
280 }
281
Willy Tarreau572bf902012-07-02 17:01:20 +0200282 firstblock = buf->buf.data + buf->buf.size - bo_ptr(&buf->buf);
Willy Tarreau74b08c92010-09-08 17:04:31 +0200283 if (firstblock > offset) {
284 if (firstblock >= len + offset) {
Willy Tarreau572bf902012-07-02 17:01:20 +0200285 memcpy(blk, bo_ptr(&buf->buf) + offset, len);
Willy Tarreau74b08c92010-09-08 17:04:31 +0200286 return len;
287 }
288
Willy Tarreau572bf902012-07-02 17:01:20 +0200289 memcpy(blk, bo_ptr(&buf->buf) + offset, firstblock - offset);
290 memcpy(blk + firstblock - offset, buf->buf.data, len - firstblock + offset);
Willy Tarreau74b08c92010-09-08 17:04:31 +0200291 return len;
292 }
293
Willy Tarreau572bf902012-07-02 17:01:20 +0200294 memcpy(blk, buf->buf.data + offset - firstblock, len);
Willy Tarreau74b08c92010-09-08 17:04:31 +0200295 return len;
296}
297
Krzysztof Piotr Oledzkiba8d7d32009-10-10 21:06:03 +0200298/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200299 * Local variables:
300 * c-indent-level: 8
301 * c-basic-offset: 8
302 * End:
303 */