buffer.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. /*
  2. * Buffering of output and input.
  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.
  11. *
  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. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with GNU Zebra; see the file COPYING. If not, write to the
  19. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  20. * Boston, MA 02111-1307, USA.
  21. */
  22. #include <zebra.h>
  23. #include "memory.h"
  24. #include "buffer.h"
  25. #include "log.h"
  26. #include "network.h"
  27. #include <stddef.h>
  28. /* Buffer master. */
  29. struct buffer
  30. {
  31. /* Data list. */
  32. struct buffer_data *head;
  33. struct buffer_data *tail;
  34. /* Size of each buffer_data chunk. */
  35. size_t size;
  36. };
  37. /* Data container. */
  38. struct buffer_data
  39. {
  40. struct buffer_data *next;
  41. /* Location to add new data. */
  42. size_t cp;
  43. /* Pointer to data not yet flushed. */
  44. size_t sp;
  45. /* Actual data stream (variable length). */
  46. unsigned char data[]; /* real dimension is buffer->size */
  47. };
  48. /* It should always be true that: 0 <= sp <= cp <= size */
  49. /* Default buffer size (used if none specified). It is rounded up to the
  50. next page boundery. */
  51. #define BUFFER_SIZE_DEFAULT 4096
  52. #define BUFFER_DATA_FREE(D) XFREE(MTYPE_BUFFER_DATA, (D))
  53. /* Make new buffer. */
  54. struct buffer *
  55. buffer_new (size_t size)
  56. {
  57. struct buffer *b;
  58. b = XCALLOC (MTYPE_BUFFER, sizeof (struct buffer));
  59. if (size)
  60. b->size = size;
  61. else
  62. {
  63. static size_t default_size;
  64. if (!default_size)
  65. {
  66. long pgsz = sysconf(_SC_PAGESIZE);
  67. default_size = ((((BUFFER_SIZE_DEFAULT-1)/pgsz)+1)*pgsz);
  68. }
  69. b->size = default_size;
  70. }
  71. return b;
  72. }
  73. /* Free buffer. */
  74. void
  75. buffer_free (struct buffer *b)
  76. {
  77. buffer_reset(b);
  78. XFREE (MTYPE_BUFFER, b);
  79. }
  80. /* Make string clone. */
  81. char *
  82. buffer_getstr (struct buffer *b)
  83. {
  84. size_t totlen = 0;
  85. struct buffer_data *data;
  86. char *s;
  87. char *p;
  88. for (data = b->head; data; data = data->next)
  89. totlen += data->cp - data->sp;
  90. if (!(s = XMALLOC(MTYPE_TMP, totlen+1)))
  91. return NULL;
  92. p = s;
  93. for (data = b->head; data; data = data->next)
  94. {
  95. memcpy(p, data->data + data->sp, data->cp - data->sp);
  96. p += data->cp - data->sp;
  97. }
  98. *p = '\0';
  99. return s;
  100. }
  101. /* Return 1 if buffer is empty. */
  102. int
  103. buffer_empty (struct buffer *b)
  104. {
  105. return (b->head == NULL);
  106. }
  107. /* Clear and free all allocated data. */
  108. void
  109. buffer_reset (struct buffer *b)
  110. {
  111. struct buffer_data *data;
  112. struct buffer_data *next;
  113. for (data = b->head; data; data = next)
  114. {
  115. next = data->next;
  116. BUFFER_DATA_FREE(data);
  117. }
  118. b->head = b->tail = NULL;
  119. }
  120. /* Add buffer_data to the end of buffer. */
  121. static struct buffer_data *
  122. buffer_add (struct buffer *b)
  123. {
  124. struct buffer_data *d;
  125. d = XMALLOC(MTYPE_BUFFER_DATA, offsetof(struct buffer_data, data) + b->size);
  126. d->cp = d->sp = 0;
  127. d->next = NULL;
  128. if (b->tail)
  129. b->tail->next = d;
  130. else
  131. b->head = d;
  132. b->tail = d;
  133. return d;
  134. }
  135. /* Write data to buffer. */
  136. void
  137. buffer_put(struct buffer *b, const void *p, size_t size)
  138. {
  139. struct buffer_data *data = b->tail;
  140. const char *ptr = p;
  141. /* We use even last one byte of data buffer. */
  142. while (size)
  143. {
  144. size_t chunk;
  145. /* If there is no data buffer add it. */
  146. if (data == NULL || data->cp == b->size)
  147. data = buffer_add (b);
  148. chunk = ((size <= (b->size - data->cp)) ? size : (b->size - data->cp));
  149. memcpy ((data->data + data->cp), ptr, chunk);
  150. size -= chunk;
  151. ptr += chunk;
  152. data->cp += chunk;
  153. }
  154. }
  155. /* Insert character into the buffer. */
  156. void
  157. buffer_putc (struct buffer *b, u_char c)
  158. {
  159. buffer_put(b, &c, 1);
  160. }
  161. /* Put string to the buffer. */
  162. void
  163. buffer_putstr (struct buffer *b, const char *c)
  164. {
  165. buffer_put(b, c, strlen(c));
  166. }
  167. /* Keep flushing data to the fd until the buffer is empty or an error is
  168. encountered or the operation would block. */
  169. buffer_status_t
  170. buffer_flush_all (struct buffer *b, int fd)
  171. {
  172. buffer_status_t ret;
  173. struct buffer_data *head;
  174. size_t head_sp;
  175. if (!b->head)
  176. return BUFFER_EMPTY;
  177. head_sp = (head = b->head)->sp;
  178. /* Flush all data. */
  179. while ((ret = buffer_flush_available(b, fd)) == BUFFER_PENDING)
  180. {
  181. if ((b->head == head) && (head_sp == head->sp) && (errno != EINTR))
  182. /* No data was flushed, so kernel buffer must be full. */
  183. return ret;
  184. head_sp = (head = b->head)->sp;
  185. }
  186. return ret;
  187. }
  188. /* Flush enough data to fill a terminal window of the given scene (used only
  189. by vty telnet interface). */
  190. buffer_status_t
  191. buffer_flush_window (struct buffer *b, int fd, int width, int height,
  192. int erase_flag, int no_more_flag)
  193. {
  194. int nbytes;
  195. int iov_alloc;
  196. int iov_index;
  197. struct iovec *iov;
  198. struct iovec small_iov[3];
  199. char more[] = " --More-- ";
  200. char erase[] = { 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
  201. ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
  202. 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08};
  203. struct buffer_data *data;
  204. int column;
  205. if (!b->head)
  206. return BUFFER_EMPTY;
  207. if (height < 1)
  208. {
  209. zlog_warn("%s called with non-positive window height %d, forcing to 1",
  210. __func__, height);
  211. height = 1;
  212. }
  213. else if (height >= 2)
  214. height--;
  215. if (width < 1)
  216. {
  217. zlog_warn("%s called with non-positive window width %d, forcing to 1",
  218. __func__, width);
  219. width = 1;
  220. }
  221. /* For erase and more data add two to b's buffer_data count.*/
  222. if (b->head->next == NULL)
  223. {
  224. iov_alloc = array_size(small_iov);
  225. iov = small_iov;
  226. }
  227. else
  228. {
  229. iov_alloc = ((height*(width+2))/b->size)+10;
  230. iov = XMALLOC(MTYPE_TMP, iov_alloc*sizeof(*iov));
  231. }
  232. iov_index = 0;
  233. /* Previously print out is performed. */
  234. if (erase_flag)
  235. {
  236. iov[iov_index].iov_base = erase;
  237. iov[iov_index].iov_len = sizeof erase;
  238. iov_index++;
  239. }
  240. /* Output data. */
  241. column = 1; /* Column position of next character displayed. */
  242. for (data = b->head; data && (height > 0); data = data->next)
  243. {
  244. size_t cp;
  245. cp = data->sp;
  246. while ((cp < data->cp) && (height > 0))
  247. {
  248. /* Calculate lines remaining and column position after displaying
  249. this character. */
  250. if (data->data[cp] == '\r')
  251. column = 1;
  252. else if ((data->data[cp] == '\n') || (column == width))
  253. {
  254. column = 1;
  255. height--;
  256. }
  257. else
  258. column++;
  259. cp++;
  260. }
  261. iov[iov_index].iov_base = (char *)(data->data + data->sp);
  262. iov[iov_index++].iov_len = cp-data->sp;
  263. data->sp = cp;
  264. if (iov_index == iov_alloc)
  265. /* This should not ordinarily happen. */
  266. {
  267. iov_alloc *= 2;
  268. if (iov != small_iov)
  269. {
  270. zlog_warn("%s: growing iov array to %d; "
  271. "width %d, height %d, size %lu",
  272. __func__, iov_alloc, width, height, (u_long)b->size);
  273. iov = XREALLOC(MTYPE_TMP, iov, iov_alloc*sizeof(*iov));
  274. }
  275. else
  276. {
  277. /* This should absolutely never occur. */
  278. zlog_err("%s: corruption detected: iov_small overflowed; "
  279. "head %p, tail %p, head->next %p",
  280. __func__, (void *)b->head, (void *)b->tail,
  281. (void *)b->head->next);
  282. iov = XMALLOC(MTYPE_TMP, iov_alloc*sizeof(*iov));
  283. memcpy(iov, small_iov, sizeof(small_iov));
  284. }
  285. }
  286. }
  287. /* In case of `more' display need. */
  288. if (b->tail && (b->tail->sp < b->tail->cp) && !no_more_flag)
  289. {
  290. iov[iov_index].iov_base = more;
  291. iov[iov_index].iov_len = sizeof more;
  292. iov_index++;
  293. }
  294. #ifdef IOV_MAX
  295. /* IOV_MAX are normally defined in <sys/uio.h> , Posix.1g.
  296. example: Solaris2.6 are defined IOV_MAX size at 16. */
  297. {
  298. struct iovec *c_iov = iov;
  299. nbytes = 0; /* Make sure it's initialized. */
  300. while (iov_index > 0)
  301. {
  302. int iov_size;
  303. iov_size = ((iov_index > IOV_MAX) ? IOV_MAX : iov_index);
  304. if ((nbytes = writev(fd, c_iov, iov_size)) < 0)
  305. {
  306. zlog_warn("%s: writev to fd %d failed: %s",
  307. __func__, fd, safe_strerror(errno));
  308. break;
  309. }
  310. /* move pointer io-vector */
  311. c_iov += iov_size;
  312. iov_index -= iov_size;
  313. }
  314. }
  315. #else /* IOV_MAX */
  316. if ((nbytes = writev (fd, iov, iov_index)) < 0)
  317. zlog_warn("%s: writev to fd %d failed: %s",
  318. __func__, fd, safe_strerror(errno));
  319. #endif /* IOV_MAX */
  320. /* Free printed buffer data. */
  321. while (b->head && (b->head->sp == b->head->cp))
  322. {
  323. struct buffer_data *del;
  324. if (!(b->head = (del = b->head)->next))
  325. b->tail = NULL;
  326. BUFFER_DATA_FREE(del);
  327. }
  328. if (iov != small_iov)
  329. XFREE (MTYPE_TMP, iov);
  330. return (nbytes < 0) ? BUFFER_ERROR :
  331. (b->head ? BUFFER_PENDING : BUFFER_EMPTY);
  332. }
  333. /* This function (unlike other buffer_flush* functions above) is designed
  334. to work with non-blocking sockets. It does not attempt to write out
  335. all of the queued data, just a "big" chunk. It returns 0 if it was
  336. able to empty out the buffers completely, 1 if more flushing is
  337. required later, or -1 on a fatal write error. */
  338. buffer_status_t
  339. buffer_flush_available(struct buffer *b, int fd)
  340. {
  341. /* These are just reasonable values to make sure a significant amount of
  342. data is written. There's no need to go crazy and try to write it all
  343. in one shot. */
  344. #ifdef IOV_MAX
  345. #define MAX_CHUNKS ((IOV_MAX >= 16) ? 16 : IOV_MAX)
  346. #else
  347. #define MAX_CHUNKS 16
  348. #endif
  349. #define MAX_FLUSH 131072
  350. struct buffer_data *d;
  351. size_t written;
  352. struct iovec iov[MAX_CHUNKS];
  353. size_t iovcnt = 0;
  354. size_t nbyte = 0;
  355. for (d = b->head; d && (iovcnt < MAX_CHUNKS) && (nbyte < MAX_FLUSH);
  356. d = d->next, iovcnt++)
  357. {
  358. iov[iovcnt].iov_base = d->data+d->sp;
  359. nbyte += (iov[iovcnt].iov_len = d->cp-d->sp);
  360. }
  361. if (!nbyte)
  362. /* No data to flush: should we issue a warning message? */
  363. return BUFFER_EMPTY;
  364. /* only place where written should be sign compared */
  365. if ((ssize_t)(written = writev(fd,iov,iovcnt)) < 0)
  366. {
  367. if (ERRNO_IO_RETRY(errno))
  368. /* Calling code should try again later. */
  369. return BUFFER_PENDING;
  370. zlog_warn("%s: write error on fd %d: %s",
  371. __func__, fd, safe_strerror(errno));
  372. return BUFFER_ERROR;
  373. }
  374. /* Free printed buffer data. */
  375. while (written > 0)
  376. {
  377. struct buffer_data *d;
  378. if (!(d = b->head))
  379. {
  380. zlog_err("%s: corruption detected: buffer queue empty, "
  381. "but written is %lu", __func__, (u_long)written);
  382. break;
  383. }
  384. if (written < d->cp-d->sp)
  385. {
  386. d->sp += written;
  387. return BUFFER_PENDING;
  388. }
  389. written -= (d->cp-d->sp);
  390. if (!(b->head = d->next))
  391. b->tail = NULL;
  392. BUFFER_DATA_FREE(d);
  393. }
  394. return b->head ? BUFFER_PENDING : BUFFER_EMPTY;
  395. #undef MAX_CHUNKS
  396. #undef MAX_FLUSH
  397. }
  398. buffer_status_t
  399. buffer_write(struct buffer *b, int fd, const void *p, size_t size)
  400. {
  401. ssize_t nbytes;
  402. #if 0
  403. /* Should we attempt to drain any previously buffered data? This could help
  404. reduce latency in pushing out the data if we are stuck in a long-running
  405. thread that is preventing the main select loop from calling the flush
  406. thread... */
  407. if (b->head && (buffer_flush_available(b, fd) == BUFFER_ERROR))
  408. return BUFFER_ERROR;
  409. #endif
  410. if (b->head)
  411. /* Buffer is not empty, so do not attempt to write the new data. */
  412. nbytes = 0;
  413. else if ((nbytes = write(fd, p, size)) < 0)
  414. {
  415. if (ERRNO_IO_RETRY(errno))
  416. nbytes = 0;
  417. else
  418. {
  419. zlog_warn("%s: write error on fd %d: %s",
  420. __func__, fd, safe_strerror(errno));
  421. return BUFFER_ERROR;
  422. }
  423. }
  424. /* Add any remaining data to the buffer. */
  425. {
  426. size_t written = nbytes;
  427. if (written < size)
  428. buffer_put(b, ((const char *)p)+written, size-written);
  429. }
  430. return b->head ? BUFFER_PENDING : BUFFER_EMPTY;
  431. }