]> git.proxmox.com Git - mirror_frr.git/blame - lib/buffer.c
*: rename ferr_ref -> log_ref
[mirror_frr.git] / lib / buffer.c
CommitLineData
718e3744 1/*
d62a17ae 2 * Buffering of output and input.
718e3744 3 * Copyright (C) 1998 Kunihiro Ishiguro
4 *
5 * This file is part of GNU Zebra.
6 *
7 * GNU Zebra is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation; either version 2, or (at your
10 * option) any later version.
896014f4 11 *
718e3744 12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
896014f4
DL
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
718e3744 20 */
21
22#include <zebra.h>
23
24#include "memory.h"
25#include "buffer.h"
2265d20c 26#include "log.h"
9fc7ebf1 27#include "network.h"
481bc15f
DS
28#include "lib_errors.h"
29
49ff6d9d 30#include <stddef.h>
718e3744 31
d62a17ae 32DEFINE_MTYPE_STATIC(LIB, BUFFER, "Buffer")
4a1ab8e4 33DEFINE_MTYPE_STATIC(LIB, BUFFER_DATA, "Buffer data")
718e3744 34
9fc7ebf1 35/* Buffer master. */
d62a17ae 36struct buffer {
37 /* Data list. */
38 struct buffer_data *head;
39 struct buffer_data *tail;
40
41 /* Size of each buffer_data chunk. */
42 size_t size;
9fc7ebf1 43};
44
45/* Data container. */
d62a17ae 46struct buffer_data {
47 struct buffer_data *next;
9fc7ebf1 48
d62a17ae 49 /* Location to add new data. */
50 size_t cp;
9fc7ebf1 51
d62a17ae 52 /* Pointer to data not yet flushed. */
53 size_t sp;
9fc7ebf1 54
d62a17ae 55 /* Actual data stream (variable length). */
56 unsigned char data[]; /* real dimension is buffer->size */
9fc7ebf1 57};
58
59/* It should always be true that: 0 <= sp <= cp <= size */
60
61/* Default buffer size (used if none specified). It is rounded up to the
62 next page boundery. */
63#define BUFFER_SIZE_DEFAULT 4096
64
9fc7ebf1 65#define BUFFER_DATA_FREE(D) XFREE(MTYPE_BUFFER_DATA, (D))
718e3744 66
67/* Make new buffer. */
d62a17ae 68struct buffer *buffer_new(size_t size)
718e3744 69{
d62a17ae 70 struct buffer *b;
71
72 b = XCALLOC(MTYPE_BUFFER, sizeof(struct buffer));
73
74 if (size)
75 b->size = size;
76 else {
77 static size_t default_size;
78 if (!default_size) {
79 long pgsz = sysconf(_SC_PAGESIZE);
80 default_size = ((((BUFFER_SIZE_DEFAULT - 1) / pgsz) + 1)
81 * pgsz);
82 }
83 b->size = default_size;
9fc7ebf1 84 }
718e3744 85
d62a17ae 86 return b;
718e3744 87}
88
89/* Free buffer. */
d62a17ae 90void buffer_free(struct buffer *b)
718e3744 91{
d62a17ae 92 buffer_reset(b);
93 XFREE(MTYPE_BUFFER, b);
718e3744 94}
95
96/* Make string clone. */
d62a17ae 97char *buffer_getstr(struct buffer *b)
718e3744 98{
d62a17ae 99 size_t totlen = 0;
100 struct buffer_data *data;
101 char *s;
102 char *p;
103
104 for (data = b->head; data; data = data->next)
105 totlen += data->cp - data->sp;
106 if (!(s = XMALLOC(MTYPE_TMP, totlen + 1)))
107 return NULL;
108 p = s;
109 for (data = b->head; data; data = data->next) {
110 memcpy(p, data->data + data->sp, data->cp - data->sp);
111 p += data->cp - data->sp;
112 }
113 *p = '\0';
114 return s;
718e3744 115}
116
117/* Return 1 if buffer is empty. */
d62a17ae 118int buffer_empty(struct buffer *b)
718e3744 119{
d62a17ae 120 return (b->head == NULL);
718e3744 121}
122
123/* Clear and free all allocated data. */
d62a17ae 124void buffer_reset(struct buffer *b)
718e3744 125{
d62a17ae 126 struct buffer_data *data;
127 struct buffer_data *next;
128
129 for (data = b->head; data; data = next) {
130 next = data->next;
131 BUFFER_DATA_FREE(data);
132 }
133 b->head = b->tail = NULL;
718e3744 134}
135
136/* Add buffer_data to the end of buffer. */
d62a17ae 137static struct buffer_data *buffer_add(struct buffer *b)
718e3744 138{
d62a17ae 139 struct buffer_data *d;
718e3744 140
d62a17ae 141 d = XMALLOC(MTYPE_BUFFER_DATA,
142 offsetof(struct buffer_data, data) + b->size);
143 d->cp = d->sp = 0;
144 d->next = NULL;
718e3744 145
d62a17ae 146 if (b->tail)
147 b->tail->next = d;
148 else
149 b->head = d;
150 b->tail = d;
718e3744 151
d62a17ae 152 return d;
718e3744 153}
154
155/* Write data to buffer. */
d62a17ae 156void buffer_put(struct buffer *b, const void *p, size_t size)
718e3744 157{
d62a17ae 158 struct buffer_data *data = b->tail;
159 const char *ptr = p;
160
161 /* We use even last one byte of data buffer. */
162 while (size) {
163 size_t chunk;
164
165 /* If there is no data buffer add it. */
166 if (data == NULL || data->cp == b->size)
167 data = buffer_add(b);
168
169 chunk = ((size <= (b->size - data->cp)) ? size
170 : (b->size - data->cp));
171 memcpy((data->data + data->cp), ptr, chunk);
172 size -= chunk;
173 ptr += chunk;
174 data->cp += chunk;
175 }
718e3744 176}
177
178/* Insert character into the buffer. */
d7c0a89a 179void buffer_putc(struct buffer *b, uint8_t c)
718e3744 180{
d62a17ae 181 buffer_put(b, &c, 1);
718e3744 182}
183
184/* Put string to the buffer. */
d62a17ae 185void buffer_putstr(struct buffer *b, const char *c)
718e3744 186{
d62a17ae 187 buffer_put(b, c, strlen(c));
718e3744 188}
189
83eba583 190/* Expand \n to \r\n */
d62a17ae 191void buffer_put_crlf(struct buffer *b, const void *origp, size_t origsize)
83eba583 192{
d62a17ae 193 struct buffer_data *data = b->tail;
194 const char *p = origp, *end = p + origsize, *lf;
195 size_t size;
196
197 lf = memchr(p, '\n', end - p);
198
199 /* We use even last one byte of data buffer. */
200 while (p < end) {
201 size_t avail, chunk;
202
203 /* If there is no data buffer add it. */
204 if (data == NULL || data->cp == b->size)
205 data = buffer_add(b);
206
207 size = (lf ? lf : end) - p;
208 avail = b->size - data->cp;
209
210 chunk = (size <= avail) ? size : avail;
211 memcpy(data->data + data->cp, p, chunk);
212
213 p += chunk;
214 data->cp += chunk;
215
216 if (lf && size <= avail) {
217 /* we just copied up to (including) a '\n' */
218 if (data->cp == b->size)
219 data = buffer_add(b);
220 data->data[data->cp++] = '\r';
221 if (data->cp == b->size)
222 data = buffer_add(b);
223 data->data[data->cp++] = '\n';
224
225 p++;
226 lf = memchr(p, '\n', end - p);
227 }
228 }
83eba583
DL
229}
230
9fc7ebf1 231/* Keep flushing data to the fd until the buffer is empty or an error is
232 encountered or the operation would block. */
d62a17ae 233buffer_status_t buffer_flush_all(struct buffer *b, int fd)
718e3744 234{
d62a17ae 235 buffer_status_t ret;
236 struct buffer_data *head;
237 size_t head_sp;
238
239 if (!b->head)
240 return BUFFER_EMPTY;
241 head_sp = (head = b->head)->sp;
242 /* Flush all data. */
243 while ((ret = buffer_flush_available(b, fd)) == BUFFER_PENDING) {
244 if ((b->head == head) && (head_sp == head->sp)
245 && (errno != EINTR))
246 /* No data was flushed, so kernel buffer must be full.
247 */
248 return ret;
249 head_sp = (head = b->head)->sp;
250 }
718e3744 251
d62a17ae 252 return ret;
718e3744 253}
254
9fc7ebf1 255/* Flush enough data to fill a terminal window of the given scene (used only
256 by vty telnet interface). */
d62a17ae 257buffer_status_t buffer_flush_window(struct buffer *b, int fd, int width,
258 int height, int erase_flag,
259 int no_more_flag)
718e3744 260{
d62a17ae 261 int nbytes;
262 int iov_alloc;
263 int iov_index;
264 struct iovec *iov;
265 struct iovec small_iov[3];
266 char more[] = " --More-- ";
267 char erase[] = {0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
268 0x08, 0x08, ' ', ' ', ' ', ' ', ' ', ' ',
269 ' ', ' ', ' ', ' ', 0x08, 0x08, 0x08, 0x08,
270 0x08, 0x08, 0x08, 0x08, 0x08, 0x08};
271 struct buffer_data *data;
272 int column;
273
274 if (!b->head)
275 return BUFFER_EMPTY;
276
277 if (height < 1) {
278 zlog_warn(
279 "%s called with non-positive window height %d, forcing to 1",
280 __func__, height);
281 height = 1;
282 } else if (height >= 2)
283 height--;
284 if (width < 1) {
285 zlog_warn(
286 "%s called with non-positive window width %d, forcing to 1",
287 __func__, width);
288 width = 1;
718e3744 289 }
718e3744 290
d62a17ae 291 /* For erase and more data add two to b's buffer_data count.*/
292 if (b->head->next == NULL) {
293 iov_alloc = array_size(small_iov);
294 iov = small_iov;
295 } else {
296 iov_alloc = ((height * (width + 2)) / b->size) + 10;
297 iov = XMALLOC(MTYPE_TMP, iov_alloc * sizeof(*iov));
298 }
299 iov_index = 0;
300
301 /* Previously print out is performed. */
302 if (erase_flag) {
303 iov[iov_index].iov_base = erase;
304 iov[iov_index].iov_len = sizeof erase;
305 iov_index++;
306 }
307
308 /* Output data. */
309 column = 1; /* Column position of next character displayed. */
310 for (data = b->head; data && (height > 0); data = data->next) {
311 size_t cp;
312
313 cp = data->sp;
314 while ((cp < data->cp) && (height > 0)) {
315 /* Calculate lines remaining and column position after
316 displaying
317 this character. */
318 if (data->data[cp] == '\r')
319 column = 1;
320 else if ((data->data[cp] == '\n')
321 || (column == width)) {
322 column = 1;
323 height--;
324 } else
325 column++;
326 cp++;
327 }
328 iov[iov_index].iov_base = (char *)(data->data + data->sp);
329 iov[iov_index++].iov_len = cp - data->sp;
330 data->sp = cp;
331
332 if (iov_index == iov_alloc)
333 /* This should not ordinarily happen. */
334 {
335 iov_alloc *= 2;
336 if (iov != small_iov) {
337 zlog_warn(
338 "%s: growing iov array to %d; "
339 "width %d, height %d, size %lu",
340 __func__, iov_alloc, width, height,
d7c0a89a 341 (unsigned long)b->size);
d62a17ae 342 iov = XREALLOC(MTYPE_TMP, iov,
343 iov_alloc * sizeof(*iov));
344 } else {
345 /* This should absolutely never occur. */
af4c2728 346 flog_err(LIB_ERR_SYSTEM_CALL,
481bc15f
DS
347 "%s: corruption detected: iov_small overflowed; "
348 "head %p, tail %p, head->next %p",
349 __func__, (void *)b->head,
350 (void *)b->tail, (void *)b->head->next);
d62a17ae 351 iov = XMALLOC(MTYPE_TMP,
352 iov_alloc * sizeof(*iov));
353 memcpy(iov, small_iov, sizeof(small_iov));
354 }
355 }
356 }
357
358 /* In case of `more' display need. */
359 if (b->tail && (b->tail->sp < b->tail->cp) && !no_more_flag) {
360 iov[iov_index].iov_base = more;
361 iov[iov_index].iov_len = sizeof more;
362 iov_index++;
363 }
718e3744 364
718e3744 365
366#ifdef IOV_MAX
d62a17ae 367 /* IOV_MAX are normally defined in <sys/uio.h> , Posix.1g.
368 example: Solaris2.6 are defined IOV_MAX size at 16. */
369 {
370 struct iovec *c_iov = iov;
371 nbytes = 0; /* Make sure it's initialized. */
372
373 while (iov_index > 0) {
374 int iov_size;
375
376 iov_size =
377 ((iov_index > IOV_MAX) ? IOV_MAX : iov_index);
378 if ((nbytes = writev(fd, c_iov, iov_size)) < 0) {
379 zlog_warn("%s: writev to fd %d failed: %s",
380 __func__, fd, safe_strerror(errno));
381 break;
382 }
383
384 /* move pointer io-vector */
385 c_iov += iov_size;
386 iov_index -= iov_size;
387 }
388 }
718e3744 389#else /* IOV_MAX */
d62a17ae 390 if ((nbytes = writev(fd, iov, iov_index)) < 0)
391 zlog_warn("%s: writev to fd %d failed: %s", __func__, fd,
392 safe_strerror(errno));
718e3744 393#endif /* IOV_MAX */
394
d62a17ae 395 /* Free printed buffer data. */
396 while (b->head && (b->head->sp == b->head->cp)) {
397 struct buffer_data *del;
398 if (!(b->head = (del = b->head)->next))
399 b->tail = NULL;
400 BUFFER_DATA_FREE(del);
401 }
718e3744 402
d62a17ae 403 if (iov != small_iov)
404 XFREE(MTYPE_TMP, iov);
718e3744 405
d62a17ae 406 return (nbytes < 0) ? BUFFER_ERROR
407 : (b->head ? BUFFER_PENDING : BUFFER_EMPTY);
718e3744 408}
49ff6d9d 409
410/* This function (unlike other buffer_flush* functions above) is designed
411to work with non-blocking sockets. It does not attempt to write out
412all of the queued data, just a "big" chunk. It returns 0 if it was
9fc7ebf1 413able to empty out the buffers completely, 1 if more flushing is
414required later, or -1 on a fatal write error. */
d62a17ae 415buffer_status_t buffer_flush_available(struct buffer *b, int fd)
49ff6d9d 416{
417
418/* These are just reasonable values to make sure a significant amount of
419data is written. There's no need to go crazy and try to write it all
420in one shot. */
421#ifdef IOV_MAX
422#define MAX_CHUNKS ((IOV_MAX >= 16) ? 16 : IOV_MAX)
423#else
424#define MAX_CHUNKS 16
425#endif
426#define MAX_FLUSH 131072
427
d62a17ae 428 struct buffer_data *d;
429 size_t written;
430 struct iovec iov[MAX_CHUNKS];
431 size_t iovcnt = 0;
432 size_t nbyte = 0;
433
6451e846
QY
434 if (fd < 0)
435 return BUFFER_ERROR;
436
d62a17ae 437 for (d = b->head; d && (iovcnt < MAX_CHUNKS) && (nbyte < MAX_FLUSH);
438 d = d->next, iovcnt++) {
439 iov[iovcnt].iov_base = d->data + d->sp;
440 nbyte += (iov[iovcnt].iov_len = d->cp - d->sp);
49ff6d9d 441 }
442
d62a17ae 443 if (!nbyte)
444 /* No data to flush: should we issue a warning message? */
445 return BUFFER_EMPTY;
446
447 /* only place where written should be sign compared */
448 if ((ssize_t)(written = writev(fd, iov, iovcnt)) < 0) {
449 if (ERRNO_IO_RETRY(errno))
450 /* Calling code should try again later. */
451 return BUFFER_PENDING;
452 zlog_warn("%s: write error on fd %d: %s", __func__, fd,
453 safe_strerror(errno));
454 return BUFFER_ERROR;
455 }
456
457 /* Free printed buffer data. */
458 while (written > 0) {
459 struct buffer_data *d;
460 if (!(d = b->head)) {
af4c2728 461 flog_err(
472878dc
DS
462 LIB_ERR_DEVELOPMENT,
463 "%s: corruption detected: buffer queue empty, but written is %lu",
d7c0a89a 464 __func__, (unsigned long)written);
d62a17ae 465 break;
466 }
467 if (written < d->cp - d->sp) {
468 d->sp += written;
469 return BUFFER_PENDING;
470 }
471
472 written -= (d->cp - d->sp);
473 if (!(b->head = d->next))
474 b->tail = NULL;
475 BUFFER_DATA_FREE(d);
476 }
49ff6d9d 477
d62a17ae 478 return b->head ? BUFFER_PENDING : BUFFER_EMPTY;
49ff6d9d 479
480#undef MAX_CHUNKS
481#undef MAX_FLUSH
482}
9fc7ebf1 483
d62a17ae 484buffer_status_t buffer_write(struct buffer *b, int fd, const void *p,
485 size_t size)
9fc7ebf1 486{
d62a17ae 487 ssize_t nbytes;
9fc7ebf1 488
07334da0 489#if 0
bf2394f0
DS
490 /*
491 * Should we attempt to drain any previously buffered data?
492 * This could help reduce latency in pushing out the data if
493 * we are stuck in a long-running thread that is preventing
494 * the main select loop from calling the flush thread...
495 */
496 if (b->head && (buffer_flush_available(b, fd) == BUFFER_ERROR))
497 return BUFFER_ERROR;
07334da0 498#endif
d62a17ae 499 if (b->head)
500 /* Buffer is not empty, so do not attempt to write the new data.
501 */
502 nbytes = 0;
503 else if ((nbytes = write(fd, p, size)) < 0) {
504 if (ERRNO_IO_RETRY(errno))
505 nbytes = 0;
506 else {
507 zlog_warn("%s: write error on fd %d: %s", __func__, fd,
508 safe_strerror(errno));
509 return BUFFER_ERROR;
510 }
511 }
512 /* Add any remaining data to the buffer. */
513 {
514 size_t written = nbytes;
515 if (written < size)
516 buffer_put(b, ((const char *)p) + written,
517 size - written);
9fc7ebf1 518 }
d62a17ae 519 return b->head ? BUFFER_PENDING : BUFFER_EMPTY;
9fc7ebf1 520}