blob: b4b6e1a1e9888207800662d2d5828d65a878bf16 [file] [log] [blame]
Willy Tarreauc7e42382012-08-24 19:22:53 +02001/*
2 * Buffer management functions.
3 *
4 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
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
William Lallemandbe0efd82012-11-22 18:01:40 +010013#include <ctype.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020014#include <stdio.h>
15#include <string.h>
16
17#include <common/config.h>
18#include <common/buffer.h>
Willy Tarreau9b28e032012-10-12 23:49:43 +020019#include <common/memory.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020020
21#include <types/global.h>
22
Willy Tarreaubafbe012017-11-24 17:34:44 +010023struct pool_head *pool_head_buffer;
Willy Tarreau9b28e032012-10-12 23:49:43 +020024
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +010025/* These buffers are used to always have a valid pointer to an empty buffer in
26 * channels. The first buffer is set once a buffer is empty. The second one is
27 * set when a buffer is desired but no more are available. It helps knowing
28 * what channel wants a buffer. They can reliably be exchanged, the split
29 * between the two is only an optimization.
Willy Tarreau2a4b5432014-11-24 11:39:34 +010030 */
31struct buffer buf_empty = { .p = buf_empty.data };
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +010032struct buffer buf_wanted = { .p = buf_wanted.data };
Willy Tarreau9b28e032012-10-12 23:49:43 +020033
Christopher Fauleta73e59b2016-12-09 17:30:18 +010034/* list of objects waiting for at least one buffer */
35struct list buffer_wq = LIST_HEAD_INIT(buffer_wq);
Willy Tarreaub6a2f582017-11-26 11:08:14 +010036__decl_hathreads(HA_SPINLOCK_T __attribute__((aligned(64))) buffer_wq_lock);
Christopher Fauleta73e59b2016-12-09 17:30:18 +010037
Willy Tarreau9b28e032012-10-12 23:49:43 +020038/* perform minimal intializations, report 0 in case of error, 1 if OK. */
39int init_buffer()
40{
Willy Tarreaua24adf02014-11-27 01:11:56 +010041 void *buffer;
42
Willy Tarreaubafbe012017-11-24 17:34:44 +010043 pool_head_buffer = create_pool("buffer", sizeof (struct buffer) + global.tune.bufsize, MEM_F_SHARED|MEM_F_EXACT);
44 if (!pool_head_buffer)
Willy Tarreaua24adf02014-11-27 01:11:56 +010045 return 0;
46
47 /* The reserved buffer is what we leave behind us. Thus we always need
48 * at least one extra buffer in minavail otherwise we'll end up waking
49 * up tasks with no memory available, causing a lot of useless wakeups.
50 * That means that we always want to have at least 3 buffers available
51 * (2 for current session, one for next session that might be needed to
52 * release a server connection).
53 */
Willy Tarreaubafbe012017-11-24 17:34:44 +010054 pool_head_buffer->minavail = MAX(global.tune.reserved_bufs, 3);
Willy Tarreau33cb0652014-12-23 22:52:37 +010055 if (global.tune.buf_limit)
Willy Tarreaubafbe012017-11-24 17:34:44 +010056 pool_head_buffer->limit = global.tune.buf_limit;
Willy Tarreaua24adf02014-11-27 01:11:56 +010057
Christopher Faulet2a944ee2017-11-07 10:42:54 +010058 HA_SPIN_INIT(&buffer_wq_lock);
Emeric Bruna1dd2432017-06-21 15:42:52 +020059
Willy Tarreaubafbe012017-11-24 17:34:44 +010060 buffer = pool_refill_alloc(pool_head_buffer, pool_head_buffer->minavail - 1);
Willy Tarreaua24adf02014-11-27 01:11:56 +010061 if (!buffer)
62 return 0;
63
Willy Tarreaubafbe012017-11-24 17:34:44 +010064 pool_free(pool_head_buffer, buffer);
Willy Tarreaua24adf02014-11-27 01:11:56 +010065 return 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +020066}
67
Christopher Fauletad405f12017-08-29 15:30:11 +020068void deinit_buffer()
69{
Willy Tarreaubafbe012017-11-24 17:34:44 +010070 pool_destroy(pool_head_buffer);
Christopher Fauletad405f12017-08-29 15:30:11 +020071}
72
Willy Tarreauaf819352012-08-27 22:08:00 +020073/* This function writes the string <str> at position <pos> which must be in
74 * buffer <b>, and moves <end> just after the end of <str>. <b>'s parameters
75 * <l> and <r> are updated to be valid after the shift. The shift value
76 * (positive or negative) is returned. If there's no space left, the move is
77 * not done. The function does not adjust ->o because it does not make sense to
78 * use it on data scheduled to be sent. For the same reason, it does not make
79 * sense to call this function on unparsed data, so <orig> is not updated. The
80 * string length is taken from parameter <len>. If <len> is null, the <str>
81 * pointer is allowed to be null.
82 */
83int buffer_replace2(struct buffer *b, char *pos, char *end, const char *str, int len)
84{
85 int delta;
86
87 delta = len - (end - pos);
88
Willy Tarreau591d4452018-06-15 17:21:00 +020089 if (b_tail(b) + delta > b_wrap(b))
Willy Tarreauaf819352012-08-27 22:08:00 +020090 return 0; /* no space left */
91
Willy Tarreaud760eec2018-07-10 09:50:25 +020092 if (b_data(b) &&
Willy Tarreau8f9c72d2018-06-07 18:46:28 +020093 b_tail(b) + delta > b_head(b) &&
94 b_head(b) >= b_tail(b))
Willy Tarreauaf819352012-08-27 22:08:00 +020095 return 0; /* no space left before wrapping data */
96
97 /* first, protect the end of the buffer */
Willy Tarreau8f9c72d2018-06-07 18:46:28 +020098 memmove(end + delta, end, b_tail(b) - end);
Willy Tarreauaf819352012-08-27 22:08:00 +020099
100 /* now, copy str over pos */
101 if (len)
102 memcpy(pos, str, len);
103
Olivier Houchardacd14032018-06-28 18:17:23 +0200104 b_add(b, delta);
Willy Tarreau0c7ed5d2018-07-10 09:53:31 +0200105 b_realign_if_empty(b);
Willy Tarreauaf819352012-08-27 22:08:00 +0200106
107 return delta;
108}
109
110/*
111 * Inserts <str> followed by "\r\n" at position <pos> in buffer <b>. The <len>
112 * argument informs about the length of string <str> so that we don't have to
113 * measure it. It does not include the "\r\n". If <str> is NULL, then the buffer
114 * is only opened for len+2 bytes but nothing is copied in. It may be useful in
115 * some circumstances. The send limit is *not* adjusted. Same comments as above
116 * for the valid use cases.
117 *
118 * The number of bytes added is returned on success. 0 is returned on failure.
119 */
120int buffer_insert_line2(struct buffer *b, char *pos, const char *str, int len)
121{
122 int delta;
123
124 delta = len + 2;
125
Willy Tarreau591d4452018-06-15 17:21:00 +0200126 if (b_tail(b) + delta >= b_wrap(b))
Willy Tarreauaf819352012-08-27 22:08:00 +0200127 return 0; /* no space left */
128
Willy Tarreaud760eec2018-07-10 09:50:25 +0200129 if (b_data(b) &&
Willy Tarreau8f9c72d2018-06-07 18:46:28 +0200130 b_tail(b) + delta > b_head(b) &&
131 b_head(b) >= b_tail(b))
Godbacha6547c12014-10-31 13:16:37 +0800132 return 0; /* no space left before wrapping data */
133
Willy Tarreauaf819352012-08-27 22:08:00 +0200134 /* first, protect the end of the buffer */
Willy Tarreau8f9c72d2018-06-07 18:46:28 +0200135 memmove(pos + delta, pos, b_tail(b) - pos);
Willy Tarreauaf819352012-08-27 22:08:00 +0200136
137 /* now, copy str over pos */
138 if (len && str) {
139 memcpy(pos, str, len);
140 pos[len] = '\r';
141 pos[len + 1] = '\n';
142 }
143
Olivier Houchardacd14032018-06-28 18:17:23 +0200144 b_add(b, delta);
Willy Tarreauaf819352012-08-27 22:08:00 +0200145 return delta;
146}
147
Willy Tarreauc7e42382012-08-24 19:22:53 +0200148/*
149 * Dumps part or all of a buffer.
150 */
151void buffer_dump(FILE *o, struct buffer *b, int from, int to)
152{
153 fprintf(o, "Dumping buffer %p\n", b);
Willy Tarreau506a29a2018-07-18 10:07:58 +0200154 fprintf(o, " data=%p o=%u i=%u p=%p\n"
William Lallemandbe0efd82012-11-22 18:01:40 +0100155 " relative: p=0x%04x\n",
Willy Tarreau506a29a2018-07-18 10:07:58 +0200156 b->data, (unsigned int)b->o, (unsigned int)b->i, b->p, (unsigned int)(b->p - b->data));
Willy Tarreauc7e42382012-08-24 19:22:53 +0200157
158 fprintf(o, "Dumping contents from byte %d to byte %d\n", from, to);
William Lallemandbe0efd82012-11-22 18:01:40 +0100159 fprintf(o, " 0 1 2 3 4 5 6 7 8 9 a b c d e f\n");
160 /* dump hexa */
161 while (from < to) {
162 int i;
163
164 fprintf(o, " %04x: ", from);
165 for (i = 0; ((from + i) < to) && (i < 16) ; i++) {
166 fprintf(o, "%02x ", (unsigned char)b->data[from + i]);
167 if (((from + i) & 15) == 7)
168 fprintf(o, "- ");
169 }
Godbachc08057c2013-11-14 10:15:20 +0800170 if (to - from < 16) {
Godbachc3916a72013-11-21 10:21:22 +0800171 int j = 0;
172
Godbachc08057c2013-11-14 10:15:20 +0800173 for (j = 0; j < from + 16 - to; j++)
174 fprintf(o, " ");
Godbachc3916a72013-11-21 10:21:22 +0800175 if (j > 8)
176 fprintf(o, " ");
Godbachc08057c2013-11-14 10:15:20 +0800177 }
William Lallemandbe0efd82012-11-22 18:01:40 +0100178 fprintf(o, " ");
179 for (i = 0; (from + i < to) && (i < 16) ; i++) {
Willy Tarreau95898ac2012-11-26 00:57:40 +0100180 fprintf(o, "%c", isprint((int)b->data[from + i]) ? b->data[from + i] : '.') ;
William Lallemandbe0efd82012-11-22 18:01:40 +0100181 if ((((from + i) & 15) == 15) && ((from + i) != to-1))
182 fprintf(o, "\n");
183 }
184 from += i;
Willy Tarreauc7e42382012-08-24 19:22:53 +0200185 }
186 fprintf(o, "\n--\n");
William Lallemandbe0efd82012-11-22 18:01:40 +0100187 fflush(o);
Willy Tarreauc7e42382012-08-24 19:22:53 +0200188}
189
Willy Tarreauc41b3e82018-03-02 10:27:12 +0100190/* see offer_buffer() for details */
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100191void __offer_buffer(void *from, unsigned int threshold)
192{
193 struct buffer_wait *wait, *bak;
194 int avail;
195
196 /* For now, we consider that all objects need 1 buffer, so we can stop
197 * waking up them once we have enough of them to eat all the available
198 * buffers. Note that we don't really know if they are streams or just
199 * other tasks, but that's a rough estimate. Similarly, for each cached
200 * event we'll need 1 buffer. If no buffer is currently used, always
201 * wake up the number of tasks we can offer a buffer based on what is
202 * allocated, and in any case at least one task per two reserved
203 * buffers.
204 */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100205 avail = pool_head_buffer->allocated - pool_head_buffer->used - global.tune.reserved_bufs / 2;
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100206
207 list_for_each_entry_safe(wait, bak, &buffer_wq, list) {
208 if (avail <= threshold)
209 break;
210
211 if (wait->target == from || !wait->wakeup_cb(wait->target))
212 continue;
213
214 LIST_DEL(&wait->list);
215 LIST_INIT(&wait->list);
216
217 avail--;
218 }
219}
Willy Tarreauc7e42382012-08-24 19:22:53 +0200220
221/*
222 * Local variables:
223 * c-indent-level: 8
224 * c-basic-offset: 8
225 * End:
226 */