blob: 2f983960db867a018dd8e7256d2ae93d4f32ab2a [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 Tarreau55a69062012-10-26 00:21:52 +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 Tarreau0bc34932011-03-28 16:25:58 +020046
Willy Tarreau9b28e032012-10-12 23:49:43 +020047 forwarded = chn->buf->i;
48 b_adv(chn->buf, chn->buf->i);
Willy Tarreau0bc34932011-03-28 16:25:58 +020049
Willy Tarreau0bc34932011-03-28 16:25:58 +020050 /* Note: the case below is the only case where we may return
51 * a byte count that does not fit into a 32-bit number.
52 */
Willy Tarreau974ced62012-10-12 23:11:02 +020053 if (likely(chn->to_forward == CHN_INFINITE_FORWARD))
Willy Tarreau0bc34932011-03-28 16:25:58 +020054 return bytes;
55
Willy Tarreau03cdb7c2012-08-27 23:14:58 +020056 if (likely(bytes == CHN_INFINITE_FORWARD)) {
Willy Tarreau974ced62012-10-12 23:11:02 +020057 chn->to_forward = bytes;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +010058 return bytes;
59 }
60
Willy Tarreau974ced62012-10-12 23:11:02 +020061 new_forward = chn->to_forward + bytes - forwarded;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +010062 bytes = forwarded; /* at least those bytes were scheduled */
Willy Tarreau0bc34932011-03-28 16:25:58 +020063
Willy Tarreau974ced62012-10-12 23:11:02 +020064 if (new_forward <= chn->to_forward) {
Willy Tarreau0bc34932011-03-28 16:25:58 +020065 /* integer overflow detected, let's assume no more than 2G at once */
66 new_forward = MID_RANGE(new_forward);
67 }
68
Willy Tarreau974ced62012-10-12 23:11:02 +020069 if (new_forward > chn->to_forward) {
70 bytes += new_forward - chn->to_forward;
71 chn->to_forward = new_forward;
Willy Tarreau0bc34932011-03-28 16:25:58 +020072 }
73 return bytes;
74}
Willy Tarreaubaaee002006-06-26 02:48:02 +020075
Willy Tarreau8263d2b2012-08-28 00:06:31 +020076/* writes <len> bytes from message <msg> to the channel's buffer. Returns -1 in
77 * case of success, -2 if the message is larger than the buffer size, or the
78 * number of bytes available otherwise. The send limit is automatically
79 * adjusted to the amount of data written. FIXME-20060521: handle unaligned
80 * data. Note: this function appends data to the buffer's output and possibly
81 * overwrites any pending input data which are assumed not to exist.
Willy Tarreaubaaee002006-06-26 02:48:02 +020082 */
Willy Tarreau974ced62012-10-12 23:11:02 +020083int bo_inject(struct channel *chn, const char *msg, int len)
Willy Tarreaubaaee002006-06-26 02:48:02 +020084{
85 int max;
86
Willy Tarreauaeac3192009-08-31 08:09:57 +020087 if (len == 0)
88 return -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +020089
Willy Tarreau9b28e032012-10-12 23:49:43 +020090 if (len > chn->buf->size) {
Willy Tarreau078e2942009-08-18 07:19:39 +020091 /* we can't write this chunk and will never be able to, because
92 * it is larger than the buffer. This must be reported as an
93 * error. Then we return -2 so that writers that don't care can
94 * ignore it and go on, and others can check for this value.
95 */
96 return -2;
97 }
98
Willy Tarreau9b28e032012-10-12 23:49:43 +020099 max = buffer_realign(chn->buf);
Willy Tarreauaeac3192009-08-31 08:09:57 +0200100
Willy Tarreaubaaee002006-06-26 02:48:02 +0200101 if (len > max)
102 return max;
103
Willy Tarreau9b28e032012-10-12 23:49:43 +0200104 memcpy(chn->buf->p, msg, len);
105 chn->buf->o += len;
106 chn->buf->p = b_ptr(chn->buf, len);
Willy Tarreau974ced62012-10-12 23:11:02 +0200107 chn->total += len;
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200108 return -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200109}
110
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200111/* Tries to copy character <c> into the channel's buffer after some length
Willy Tarreau974ced62012-10-12 23:11:02 +0200112 * controls. The chn->o and to_forward pointers are updated. If the channel
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200113 * input is closed, -2 is returned. If there is not enough room left in the
114 * buffer, -1 is returned. Otherwise the number of bytes copied is returned
115 * (1). Channel flag READ_PARTIAL is updated if some data can be transferred.
Willy Tarreaud7ad9f52013-12-31 17:26:25 +0100116 * Channel flag CF_WAKE_WRITE is set if the write fails because the buffer is
117 * full.
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100118 */
Willy Tarreau974ced62012-10-12 23:11:02 +0200119int bi_putchr(struct channel *chn, char c)
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100120{
Willy Tarreau974ced62012-10-12 23:11:02 +0200121 if (unlikely(channel_input_closed(chn)))
Willy Tarreau74b08c92010-09-08 17:04:31 +0200122 return -2;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100123
Willy Tarreaud7ad9f52013-12-31 17:26:25 +0100124 if (channel_full(chn)) {
125 chn->flags |= CF_WAKE_WRITE;
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200126 return -1;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +0100127 }
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100128
Willy Tarreau9b28e032012-10-12 23:49:43 +0200129 *bi_end(chn->buf) = c;
Willy Tarreau74b08c92010-09-08 17:04:31 +0200130
Willy Tarreau9b28e032012-10-12 23:49:43 +0200131 chn->buf->i++;
Willy Tarreau974ced62012-10-12 23:11:02 +0200132 chn->flags |= CF_READ_PARTIAL;
Willy Tarreau74b08c92010-09-08 17:04:31 +0200133
Willy Tarreau974ced62012-10-12 23:11:02 +0200134 if (chn->to_forward >= 1) {
135 if (chn->to_forward != CHN_INFINITE_FORWARD)
136 chn->to_forward--;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200137 b_adv(chn->buf, 1);
Willy Tarreau74b08c92010-09-08 17:04:31 +0200138 }
139
Willy Tarreau974ced62012-10-12 23:11:02 +0200140 chn->total++;
Willy Tarreau74b08c92010-09-08 17:04:31 +0200141 return 1;
142}
143
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200144/* Tries to copy block <blk> at once into the channel's buffer after length
Willy Tarreau974ced62012-10-12 23:11:02 +0200145 * controls. The chn->o and to_forward pointers are updated. If the channel
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200146 * input is closed, -2 is returned. If the block is too large for this buffer,
147 * -3 is returned. If there is not enough room left in the buffer, -1 is
148 * returned. Otherwise the number of bytes copied is returned (0 being a valid
149 * number). Channel flag READ_PARTIAL is updated if some data can be
Willy Tarreaud7ad9f52013-12-31 17:26:25 +0100150 * transferred. Channel flag CF_WAKE_WRITE is set if the write fails because
151 * the buffer is full.
Willy Tarreau74b08c92010-09-08 17:04:31 +0200152 */
Willy Tarreau974ced62012-10-12 23:11:02 +0200153int bi_putblk(struct channel *chn, const char *blk, int len)
Willy Tarreau74b08c92010-09-08 17:04:31 +0200154{
155 int max;
156
Willy Tarreau974ced62012-10-12 23:11:02 +0200157 if (unlikely(channel_input_closed(chn)))
Willy Tarreau74b08c92010-09-08 17:04:31 +0200158 return -2;
159
Willy Tarreau974ced62012-10-12 23:11:02 +0200160 max = buffer_max_len(chn);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200161 if (unlikely(len > max - buffer_len(chn->buf))) {
Willy Tarreau591fedc2010-08-10 15:28:21 +0200162 /* we can't write this chunk right now because the buffer is
163 * almost full or because the block is too large. Return the
164 * available space or -2 if impossible.
Willy Tarreau078e2942009-08-18 07:19:39 +0200165 */
Willy Tarreau591fedc2010-08-10 15:28:21 +0200166 if (len > max)
Willy Tarreau74b08c92010-09-08 17:04:31 +0200167 return -3;
Willy Tarreau078e2942009-08-18 07:19:39 +0200168
Willy Tarreaud7ad9f52013-12-31 17:26:25 +0100169 chn->flags |= CF_WAKE_WRITE;
Willy Tarreau74b08c92010-09-08 17:04:31 +0200170 return -1;
Willy Tarreau591fedc2010-08-10 15:28:21 +0200171 }
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100172
Willy Tarreau74b08c92010-09-08 17:04:31 +0200173 if (unlikely(len == 0))
174 return 0;
175
Willy Tarreau591fedc2010-08-10 15:28:21 +0200176 /* OK so the data fits in the buffer in one or two blocks */
Willy Tarreau285ff0f2014-04-24 17:02:57 +0200177 max = buffer_contig_space(chn->buf);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200178 memcpy(bi_end(chn->buf), blk, MIN(len, max));
Willy Tarreauaeac3192009-08-31 08:09:57 +0200179 if (len > max)
Willy Tarreau9b28e032012-10-12 23:49:43 +0200180 memcpy(chn->buf->data, blk + max, len - max);
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100181
Willy Tarreau9b28e032012-10-12 23:49:43 +0200182 chn->buf->i += len;
Willy Tarreau974ced62012-10-12 23:11:02 +0200183 chn->total += len;
184 if (chn->to_forward) {
Willy Tarreau31971e52009-09-20 12:07:52 +0200185 unsigned long fwd = len;
Willy Tarreau974ced62012-10-12 23:11:02 +0200186 if (chn->to_forward != CHN_INFINITE_FORWARD) {
187 if (fwd > chn->to_forward)
188 fwd = chn->to_forward;
189 chn->to_forward -= fwd;
Willy Tarreau31971e52009-09-20 12:07:52 +0200190 }
Willy Tarreau9b28e032012-10-12 23:49:43 +0200191 b_adv(chn->buf, fwd);
Willy Tarreauaeac3192009-08-31 08:09:57 +0200192 }
193
Willy Tarreaufb0e9202009-09-23 23:47:55 +0200194 /* notify that some data was read from the SI into the buffer */
Willy Tarreau974ced62012-10-12 23:11:02 +0200195 chn->flags |= CF_READ_PARTIAL;
Willy Tarreau74b08c92010-09-08 17:04:31 +0200196 return len;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100197}
198
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200199/* Gets one text line out of a channel's buffer from a stream interface.
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200200 * Return values :
201 * >0 : number of bytes read. Includes the \n if present before len or end.
Willy Tarreau74b08c92010-09-08 17:04:31 +0200202 * =0 : no '\n' before end found. <str> is left undefined.
203 * <0 : no more bytes readable because output is shut.
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200204 * The channel status is not changed. The caller must call bo_skip() to
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200205 * update it. The '\n' is waited for as long as neither the buffer nor the
206 * output are full. If either of them is full, the string may be returned
207 * as is, without the '\n'.
208 */
Willy Tarreau974ced62012-10-12 23:11:02 +0200209int bo_getline(struct channel *chn, char *str, int len)
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200210{
211 int ret, max;
212 char *p;
213
214 ret = 0;
215 max = len;
Willy Tarreau74b08c92010-09-08 17:04:31 +0200216
217 /* closed or empty + imminent close = -1; empty = 0 */
Willy Tarreau974ced62012-10-12 23:11:02 +0200218 if (unlikely((chn->flags & CF_SHUTW) || channel_is_empty(chn))) {
219 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200220 ret = -1;
221 goto out;
222 }
223
Willy Tarreau9b28e032012-10-12 23:49:43 +0200224 p = bo_ptr(chn->buf);
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200225
Willy Tarreau9b28e032012-10-12 23:49:43 +0200226 if (max > chn->buf->o) {
227 max = chn->buf->o;
Willy Tarreau2e1dd3d2009-09-23 22:56:07 +0200228 str[max-1] = 0;
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200229 }
230 while (max) {
231 *str++ = *p;
232 ret++;
233 max--;
234
235 if (*p == '\n')
236 break;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200237 p = buffer_wrap_add(chn->buf, p + 1);
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200238 }
Willy Tarreau82de2b62013-12-10 18:58:23 +0100239 if (ret > 0 && ret < len &&
240 (ret < chn->buf->o || !channel_full(chn)) &&
Willy Tarreau2e1dd3d2009-09-23 22:56:07 +0200241 *(str-1) != '\n' &&
Willy Tarreau974ced62012-10-12 23:11:02 +0200242 !(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200243 ret = 0;
244 out:
245 if (max)
246 *str = 0;
247 return ret;
248}
249
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200250/* Gets one full block of data at once from a channel's buffer, optionally from
251 * a specific offset. Return values :
Willy Tarreau74b08c92010-09-08 17:04:31 +0200252 * >0 : number of bytes read, equal to requested size.
253 * =0 : not enough data available. <blk> is left undefined.
254 * <0 : no more bytes readable because output is shut.
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200255 * The channel status is not changed. The caller must call bo_skip() to
Willy Tarreau74b08c92010-09-08 17:04:31 +0200256 * update it.
257 */
Willy Tarreau974ced62012-10-12 23:11:02 +0200258int bo_getblk(struct channel *chn, char *blk, int len, int offset)
Willy Tarreau74b08c92010-09-08 17:04:31 +0200259{
260 int firstblock;
261
Willy Tarreau974ced62012-10-12 23:11:02 +0200262 if (chn->flags & CF_SHUTW)
Willy Tarreau74b08c92010-09-08 17:04:31 +0200263 return -1;
264
Willy Tarreau9b28e032012-10-12 23:49:43 +0200265 if (len + offset > chn->buf->o) {
Willy Tarreau974ced62012-10-12 23:11:02 +0200266 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreau74b08c92010-09-08 17:04:31 +0200267 return -1;
268 return 0;
269 }
270
Willy Tarreau9b28e032012-10-12 23:49:43 +0200271 firstblock = chn->buf->data + chn->buf->size - bo_ptr(chn->buf);
Willy Tarreau74b08c92010-09-08 17:04:31 +0200272 if (firstblock > offset) {
273 if (firstblock >= len + offset) {
Willy Tarreau9b28e032012-10-12 23:49:43 +0200274 memcpy(blk, bo_ptr(chn->buf) + offset, len);
Willy Tarreau74b08c92010-09-08 17:04:31 +0200275 return len;
276 }
277
Willy Tarreau9b28e032012-10-12 23:49:43 +0200278 memcpy(blk, bo_ptr(chn->buf) + offset, firstblock - offset);
279 memcpy(blk + firstblock - offset, chn->buf->data, len - firstblock + offset);
Willy Tarreau74b08c92010-09-08 17:04:31 +0200280 return len;
281 }
282
Willy Tarreau9b28e032012-10-12 23:49:43 +0200283 memcpy(blk, chn->buf->data + offset - firstblock, len);
Willy Tarreau74b08c92010-09-08 17:04:31 +0200284 return len;
285}
286
Krzysztof Piotr Oledzkiba8d7d32009-10-10 21:06:03 +0200287/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200288 * Local variables:
289 * c-indent-level: 8
290 * c-basic-offset: 8
291 * End:
292 */