blob: 6e945d4c4d0a841c6f90661e04fee22a7b63355e [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>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
Willy Tarreau9b28e032012-10-12 23:49:43 +020022#include <proto/channel.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020023
Willy Tarreau8263d2b2012-08-28 00:06:31 +020024struct pool_head *pool2_channel;
Willy Tarreau7341d942007-05-13 19:56:02 +020025
26
27/* perform minimal intializations, report 0 in case of error, 1 if OK. */
Willy Tarreau8263d2b2012-08-28 00:06:31 +020028int init_channel()
Willy Tarreau7341d942007-05-13 19:56:02 +020029{
Willy Tarreau9b28e032012-10-12 23:49:43 +020030 pool2_channel = create_pool("channel", sizeof(struct channel), MEM_F_SHARED);
Willy Tarreau8263d2b2012-08-28 00:06:31 +020031 return pool2_channel != NULL;
Willy Tarreau7341d942007-05-13 19:56:02 +020032}
33
Willy Tarreau8263d2b2012-08-28 00:06:31 +020034/* Schedule up to <bytes> more bytes to be forwarded via the channel without
35 * notifying the owner task. Any data pending in the buffer are scheduled to be
36 * sent as well, in the limit of the number of bytes to forward. This must be
37 * the only method to use to schedule bytes to be forwarded. If the requested
38 * number is too large, it is automatically adjusted. The number of bytes taken
39 * into account is returned. Directly touching ->to_forward will cause lockups
40 * when buf->o goes down to zero if nobody is ready to push the remaining data.
Willy Tarreau0bc34932011-03-28 16:25:58 +020041 */
Willy Tarreau974ced62012-10-12 23:11:02 +020042unsigned long long channel_forward(struct channel *chn, unsigned long long bytes)
Willy Tarreau0bc34932011-03-28 16:25:58 +020043{
Willy Tarreau0bc34932011-03-28 16:25:58 +020044 unsigned int new_forward;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +010045 unsigned int forwarded;
Willy Tarreau328582c2012-05-05 23:32:27 +020046 unsigned int bytes32;
Willy Tarreau0bc34932011-03-28 16:25:58 +020047
Willy Tarreau328582c2012-05-05 23:32:27 +020048 bytes32 = bytes;
49
50 /* hint: avoid comparisons on long long for the fast case, since if the
51 * length does not fit in an unsigned it, it will never be forwarded at
52 * once anyway.
53 */
54 if (bytes <= ~0U) {
Willy Tarreau9b28e032012-10-12 23:49:43 +020055 if (bytes32 <= chn->buf->i) {
Willy Tarreau328582c2012-05-05 23:32:27 +020056 /* OK this amount of bytes might be forwarded at once */
57 if (!bytes32)
58 return 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +020059 b_adv(chn->buf, bytes32);
Willy Tarreau328582c2012-05-05 23:32:27 +020060 return bytes;
61 }
Willy Tarreau0bc34932011-03-28 16:25:58 +020062 }
63
Willy Tarreau9b28e032012-10-12 23:49:43 +020064 forwarded = chn->buf->i;
65 b_adv(chn->buf, chn->buf->i);
Willy Tarreau0bc34932011-03-28 16:25:58 +020066
Willy Tarreau0bc34932011-03-28 16:25:58 +020067 /* Note: the case below is the only case where we may return
68 * a byte count that does not fit into a 32-bit number.
69 */
Willy Tarreau974ced62012-10-12 23:11:02 +020070 if (likely(chn->to_forward == CHN_INFINITE_FORWARD))
Willy Tarreau0bc34932011-03-28 16:25:58 +020071 return bytes;
72
Willy Tarreau03cdb7c2012-08-27 23:14:58 +020073 if (likely(bytes == CHN_INFINITE_FORWARD)) {
Willy Tarreau974ced62012-10-12 23:11:02 +020074 chn->to_forward = bytes;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +010075 return bytes;
76 }
77
Willy Tarreau974ced62012-10-12 23:11:02 +020078 new_forward = chn->to_forward + bytes - forwarded;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +010079 bytes = forwarded; /* at least those bytes were scheduled */
Willy Tarreau0bc34932011-03-28 16:25:58 +020080
Willy Tarreau974ced62012-10-12 23:11:02 +020081 if (new_forward <= chn->to_forward) {
Willy Tarreau0bc34932011-03-28 16:25:58 +020082 /* integer overflow detected, let's assume no more than 2G at once */
83 new_forward = MID_RANGE(new_forward);
84 }
85
Willy Tarreau974ced62012-10-12 23:11:02 +020086 if (new_forward > chn->to_forward) {
87 bytes += new_forward - chn->to_forward;
88 chn->to_forward = new_forward;
Willy Tarreau0bc34932011-03-28 16:25:58 +020089 }
90 return bytes;
91}
Willy Tarreaubaaee002006-06-26 02:48:02 +020092
Willy Tarreau8263d2b2012-08-28 00:06:31 +020093/* writes <len> bytes from message <msg> to the channel's buffer. Returns -1 in
94 * case of success, -2 if the message is larger than the buffer size, or the
95 * number of bytes available otherwise. The send limit is automatically
96 * adjusted to the amount of data written. FIXME-20060521: handle unaligned
97 * data. Note: this function appends data to the buffer's output and possibly
98 * overwrites any pending input data which are assumed not to exist.
Willy Tarreaubaaee002006-06-26 02:48:02 +020099 */
Willy Tarreau974ced62012-10-12 23:11:02 +0200100int bo_inject(struct channel *chn, const char *msg, int len)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200101{
102 int max;
103
Willy Tarreauaeac3192009-08-31 08:09:57 +0200104 if (len == 0)
105 return -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200106
Willy Tarreau9b28e032012-10-12 23:49:43 +0200107 if (len > chn->buf->size) {
Willy Tarreau078e2942009-08-18 07:19:39 +0200108 /* we can't write this chunk and will never be able to, because
109 * it is larger than the buffer. This must be reported as an
110 * error. Then we return -2 so that writers that don't care can
111 * ignore it and go on, and others can check for this value.
112 */
113 return -2;
114 }
115
Willy Tarreau9b28e032012-10-12 23:49:43 +0200116 max = buffer_realign(chn->buf);
Willy Tarreauaeac3192009-08-31 08:09:57 +0200117
Willy Tarreaubaaee002006-06-26 02:48:02 +0200118 if (len > max)
119 return max;
120
Willy Tarreau9b28e032012-10-12 23:49:43 +0200121 memcpy(chn->buf->p, msg, len);
122 chn->buf->o += len;
123 chn->buf->p = b_ptr(chn->buf, len);
Willy Tarreau974ced62012-10-12 23:11:02 +0200124 chn->total += len;
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200125 return -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200126}
127
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200128/* Tries to copy character <c> into the channel's buffer after some length
Willy Tarreau974ced62012-10-12 23:11:02 +0200129 * controls. The chn->o and to_forward pointers are updated. If the channel
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200130 * input is closed, -2 is returned. If there is not enough room left in the
131 * buffer, -1 is returned. Otherwise the number of bytes copied is returned
132 * (1). Channel flag READ_PARTIAL is updated if some data can be transferred.
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100133 */
Willy Tarreau974ced62012-10-12 23:11:02 +0200134int bi_putchr(struct channel *chn, char c)
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100135{
Willy Tarreau974ced62012-10-12 23:11:02 +0200136 if (unlikely(channel_input_closed(chn)))
Willy Tarreau74b08c92010-09-08 17:04:31 +0200137 return -2;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100138
Willy Tarreau974ced62012-10-12 23:11:02 +0200139 if (channel_full(chn))
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200140 return -1;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100141
Willy Tarreau9b28e032012-10-12 23:49:43 +0200142 *bi_end(chn->buf) = c;
Willy Tarreau74b08c92010-09-08 17:04:31 +0200143
Willy Tarreau9b28e032012-10-12 23:49:43 +0200144 chn->buf->i++;
Willy Tarreau974ced62012-10-12 23:11:02 +0200145 chn->flags |= CF_READ_PARTIAL;
Willy Tarreau74b08c92010-09-08 17:04:31 +0200146
Willy Tarreau974ced62012-10-12 23:11:02 +0200147 if (chn->to_forward >= 1) {
148 if (chn->to_forward != CHN_INFINITE_FORWARD)
149 chn->to_forward--;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200150 b_adv(chn->buf, 1);
Willy Tarreau74b08c92010-09-08 17:04:31 +0200151 }
152
Willy Tarreau974ced62012-10-12 23:11:02 +0200153 chn->total++;
Willy Tarreau74b08c92010-09-08 17:04:31 +0200154 return 1;
155}
156
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200157/* Tries to copy block <blk> at once into the channel's buffer after length
Willy Tarreau974ced62012-10-12 23:11:02 +0200158 * controls. The chn->o and to_forward pointers are updated. If the channel
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200159 * input is closed, -2 is returned. If the block is too large for this buffer,
160 * -3 is returned. If there is not enough room left in the buffer, -1 is
161 * returned. Otherwise the number of bytes copied is returned (0 being a valid
162 * number). Channel flag READ_PARTIAL is updated if some data can be
163 * transferred.
Willy Tarreau74b08c92010-09-08 17:04:31 +0200164 */
Willy Tarreau974ced62012-10-12 23:11:02 +0200165int bi_putblk(struct channel *chn, const char *blk, int len)
Willy Tarreau74b08c92010-09-08 17:04:31 +0200166{
167 int max;
168
Willy Tarreau974ced62012-10-12 23:11:02 +0200169 if (unlikely(channel_input_closed(chn)))
Willy Tarreau74b08c92010-09-08 17:04:31 +0200170 return -2;
171
Willy Tarreau974ced62012-10-12 23:11:02 +0200172 max = buffer_max_len(chn);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200173 if (unlikely(len > max - buffer_len(chn->buf))) {
Willy Tarreau591fedc2010-08-10 15:28:21 +0200174 /* we can't write this chunk right now because the buffer is
175 * almost full or because the block is too large. Return the
176 * available space or -2 if impossible.
Willy Tarreau078e2942009-08-18 07:19:39 +0200177 */
Willy Tarreau591fedc2010-08-10 15:28:21 +0200178 if (len > max)
Willy Tarreau74b08c92010-09-08 17:04:31 +0200179 return -3;
Willy Tarreau078e2942009-08-18 07:19:39 +0200180
Willy Tarreau74b08c92010-09-08 17:04:31 +0200181 return -1;
Willy Tarreau591fedc2010-08-10 15:28:21 +0200182 }
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100183
Willy Tarreau74b08c92010-09-08 17:04:31 +0200184 if (unlikely(len == 0))
185 return 0;
186
Willy Tarreau591fedc2010-08-10 15:28:21 +0200187 /* OK so the data fits in the buffer in one or two blocks */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200188 max = buffer_contig_space_with_res(chn->buf, chn->buf->size - max);
189 memcpy(bi_end(chn->buf), blk, MIN(len, max));
Willy Tarreauaeac3192009-08-31 08:09:57 +0200190 if (len > max)
Willy Tarreau9b28e032012-10-12 23:49:43 +0200191 memcpy(chn->buf->data, blk + max, len - max);
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100192
Willy Tarreau9b28e032012-10-12 23:49:43 +0200193 chn->buf->i += len;
Willy Tarreau974ced62012-10-12 23:11:02 +0200194 chn->total += len;
195 if (chn->to_forward) {
Willy Tarreau31971e52009-09-20 12:07:52 +0200196 unsigned long fwd = len;
Willy Tarreau974ced62012-10-12 23:11:02 +0200197 if (chn->to_forward != CHN_INFINITE_FORWARD) {
198 if (fwd > chn->to_forward)
199 fwd = chn->to_forward;
200 chn->to_forward -= fwd;
Willy Tarreau31971e52009-09-20 12:07:52 +0200201 }
Willy Tarreau9b28e032012-10-12 23:49:43 +0200202 b_adv(chn->buf, fwd);
Willy Tarreauaeac3192009-08-31 08:09:57 +0200203 }
204
Willy Tarreaufb0e9202009-09-23 23:47:55 +0200205 /* notify that some data was read from the SI into the buffer */
Willy Tarreau974ced62012-10-12 23:11:02 +0200206 chn->flags |= CF_READ_PARTIAL;
Willy Tarreau74b08c92010-09-08 17:04:31 +0200207 return len;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100208}
209
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200210/* Gets one text line out of a channel's buffer from a stream interface.
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200211 * Return values :
212 * >0 : number of bytes read. Includes the \n if present before len or end.
Willy Tarreau74b08c92010-09-08 17:04:31 +0200213 * =0 : no '\n' before end found. <str> is left undefined.
214 * <0 : no more bytes readable because output is shut.
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200215 * The channel status is not changed. The caller must call bo_skip() to
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200216 * update it. The '\n' is waited for as long as neither the buffer nor the
217 * output are full. If either of them is full, the string may be returned
218 * as is, without the '\n'.
219 */
Willy Tarreau974ced62012-10-12 23:11:02 +0200220int bo_getline(struct channel *chn, char *str, int len)
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200221{
222 int ret, max;
223 char *p;
224
225 ret = 0;
226 max = len;
Willy Tarreau74b08c92010-09-08 17:04:31 +0200227
228 /* closed or empty + imminent close = -1; empty = 0 */
Willy Tarreau974ced62012-10-12 23:11:02 +0200229 if (unlikely((chn->flags & CF_SHUTW) || channel_is_empty(chn))) {
230 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200231 ret = -1;
232 goto out;
233 }
234
Willy Tarreau9b28e032012-10-12 23:49:43 +0200235 p = bo_ptr(chn->buf);
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200236
Willy Tarreau9b28e032012-10-12 23:49:43 +0200237 if (max > chn->buf->o) {
238 max = chn->buf->o;
Willy Tarreau2e1dd3d2009-09-23 22:56:07 +0200239 str[max-1] = 0;
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200240 }
241 while (max) {
242 *str++ = *p;
243 ret++;
244 max--;
245
246 if (*p == '\n')
247 break;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200248 p = buffer_wrap_add(chn->buf, p + 1);
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200249 }
Willy Tarreau9b28e032012-10-12 23:49:43 +0200250 if (ret > 0 && ret < len && ret < chn->buf->o &&
Willy Tarreau2e1dd3d2009-09-23 22:56:07 +0200251 *(str-1) != '\n' &&
Willy Tarreau974ced62012-10-12 23:11:02 +0200252 !(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200253 ret = 0;
254 out:
255 if (max)
256 *str = 0;
257 return ret;
258}
259
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200260/* Gets one full block of data at once from a channel's buffer, optionally from
261 * a specific offset. Return values :
Willy Tarreau74b08c92010-09-08 17:04:31 +0200262 * >0 : number of bytes read, equal to requested size.
263 * =0 : not enough data available. <blk> is left undefined.
264 * <0 : no more bytes readable because output is shut.
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200265 * The channel status is not changed. The caller must call bo_skip() to
Willy Tarreau74b08c92010-09-08 17:04:31 +0200266 * update it.
267 */
Willy Tarreau974ced62012-10-12 23:11:02 +0200268int bo_getblk(struct channel *chn, char *blk, int len, int offset)
Willy Tarreau74b08c92010-09-08 17:04:31 +0200269{
270 int firstblock;
271
Willy Tarreau974ced62012-10-12 23:11:02 +0200272 if (chn->flags & CF_SHUTW)
Willy Tarreau74b08c92010-09-08 17:04:31 +0200273 return -1;
274
Willy Tarreau9b28e032012-10-12 23:49:43 +0200275 if (len + offset > chn->buf->o) {
Willy Tarreau974ced62012-10-12 23:11:02 +0200276 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreau74b08c92010-09-08 17:04:31 +0200277 return -1;
278 return 0;
279 }
280
Willy Tarreau9b28e032012-10-12 23:49:43 +0200281 firstblock = chn->buf->data + chn->buf->size - bo_ptr(chn->buf);
Willy Tarreau74b08c92010-09-08 17:04:31 +0200282 if (firstblock > offset) {
283 if (firstblock >= len + offset) {
Willy Tarreau9b28e032012-10-12 23:49:43 +0200284 memcpy(blk, bo_ptr(chn->buf) + offset, len);
Willy Tarreau74b08c92010-09-08 17:04:31 +0200285 return len;
286 }
287
Willy Tarreau9b28e032012-10-12 23:49:43 +0200288 memcpy(blk, bo_ptr(chn->buf) + offset, firstblock - offset);
289 memcpy(blk + firstblock - offset, chn->buf->data, len - firstblock + offset);
Willy Tarreau74b08c92010-09-08 17:04:31 +0200290 return len;
291 }
292
Willy Tarreau9b28e032012-10-12 23:49:43 +0200293 memcpy(blk, chn->buf->data + offset - firstblock, len);
Willy Tarreau74b08c92010-09-08 17:04:31 +0200294 return len;
295}
296
Krzysztof Piotr Oledzkiba8d7d32009-10-10 21:06:03 +0200297/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200298 * Local variables:
299 * c-indent-level: 8
300 * c-basic-offset: 8
301 * End:
302 */