]> git.proxmox.com Git - mirror_frr.git/blob - nhrpd/zbuf.c
zebra: Refactor kernel_rtm to be a bit smarter about how it handles options
[mirror_frr.git] / nhrpd / zbuf.c
1 /* Stream/packet buffer API implementation
2 * Copyright (c) 2014-2015 Timo Teräs
3 *
4 * This file is free software: you may copy, redistribute and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 */
9
10 #ifdef HAVE_CONFIG_H
11 #include "config.h"
12 #endif
13
14 #include <string.h>
15 #include <unistd.h>
16 #include <errno.h>
17 #include "zassert.h"
18 #include "zbuf.h"
19 #include "memory.h"
20 #include "nhrpd.h"
21
22 #define ERRNO_IO_RETRY(EN) (((EN) == EAGAIN) || ((EN) == EWOULDBLOCK) || ((EN) == EINTR))
23
24 DEFINE_MTYPE_STATIC(NHRPD, ZBUF_DATA, "NHRPD zbuf data")
25
26 struct zbuf *zbuf_alloc(size_t size)
27 {
28 struct zbuf *zb;
29
30 zb = XMALLOC(MTYPE_ZBUF_DATA, sizeof(*zb) + size);
31
32 zbuf_init(zb, zb + 1, size, 0);
33 zb->allocated = 1;
34
35 return zb;
36 }
37
38 void zbuf_init(struct zbuf *zb, void *buf, size_t len, size_t datalen)
39 {
40 *zb = (struct zbuf){
41 .buf = buf,
42 .end = (uint8_t *)buf + len,
43 .head = buf,
44 .tail = (uint8_t *)buf + datalen,
45 };
46 }
47
48 void zbuf_free(struct zbuf *zb)
49 {
50 if (zb->allocated)
51 XFREE(MTYPE_ZBUF_DATA, zb);
52 }
53
54 void zbuf_reset(struct zbuf *zb)
55 {
56 zb->head = zb->tail = zb->buf;
57 zb->error = 0;
58 }
59
60 void zbuf_reset_head(struct zbuf *zb, void *ptr)
61 {
62 zassert((void *)zb->buf <= ptr && ptr <= (void *)zb->tail);
63 zb->head = ptr;
64 }
65
66 static void zbuf_remove_headroom(struct zbuf *zb)
67 {
68 ssize_t headroom = zbuf_headroom(zb);
69 if (!headroom)
70 return;
71 memmove(zb->buf, zb->head, zbuf_used(zb));
72 zb->head -= headroom;
73 zb->tail -= headroom;
74 }
75
76 ssize_t zbuf_read(struct zbuf *zb, int fd, size_t maxlen)
77 {
78 ssize_t r;
79
80 if (zb->error)
81 return -3;
82
83 zbuf_remove_headroom(zb);
84 if (maxlen > zbuf_tailroom(zb))
85 maxlen = zbuf_tailroom(zb);
86
87 r = read(fd, zb->tail, maxlen);
88 if (r > 0)
89 zb->tail += r;
90 else if (r == 0)
91 r = -2;
92 else if (r < 0 && ERRNO_IO_RETRY(errno))
93 r = 0;
94
95 return r;
96 }
97
98 ssize_t zbuf_write(struct zbuf *zb, int fd)
99 {
100 ssize_t r;
101
102 if (zb->error)
103 return -3;
104
105 r = write(fd, zb->head, zbuf_used(zb));
106 if (r > 0) {
107 zb->head += r;
108 if (zb->head == zb->tail)
109 zbuf_reset(zb);
110 } else if (r == 0)
111 r = -2;
112 else if (r < 0 && ERRNO_IO_RETRY(errno))
113 r = 0;
114
115 return r;
116 }
117
118 ssize_t zbuf_recv(struct zbuf *zb, int fd)
119 {
120 ssize_t r;
121
122 if (zb->error)
123 return -3;
124
125 zbuf_remove_headroom(zb);
126 r = recv(fd, zb->tail, zbuf_tailroom(zb), 0);
127 if (r > 0)
128 zb->tail += r;
129 else if (r == 0)
130 r = -2;
131 else if (r < 0 && ERRNO_IO_RETRY(errno))
132 r = 0;
133 return r;
134 }
135
136 ssize_t zbuf_send(struct zbuf *zb, int fd)
137 {
138 ssize_t r;
139
140 if (zb->error)
141 return -3;
142
143 r = send(fd, zb->head, zbuf_used(zb), 0);
144 if (r >= 0)
145 zbuf_reset(zb);
146
147 return r;
148 }
149
150 void *zbuf_may_pull_until(struct zbuf *zb, const char *sep, struct zbuf *msg)
151 {
152 size_t seplen = strlen(sep), len;
153 uint8_t *ptr;
154
155 ptr = memmem(zb->head, zbuf_used(zb), sep, seplen);
156 if (!ptr)
157 return NULL;
158
159 len = ptr - zb->head + seplen;
160 zbuf_init(msg, zbuf_pulln(zb, len), len, len);
161 return msg->head;
162 }
163
164 void zbufq_init(struct zbuf_queue *zbq)
165 {
166 *zbq = (struct zbuf_queue){
167 .queue_head = LIST_INITIALIZER(zbq->queue_head),
168 };
169 }
170
171 void zbufq_reset(struct zbuf_queue *zbq)
172 {
173 struct zbuf *buf, *bufn;
174
175 list_for_each_entry_safe(buf, bufn, &zbq->queue_head, queue_list)
176 {
177 list_del(&buf->queue_list);
178 zbuf_free(buf);
179 }
180 }
181
182 void zbufq_queue(struct zbuf_queue *zbq, struct zbuf *zb)
183 {
184 list_add_tail(&zb->queue_list, &zbq->queue_head);
185 }
186
187 int zbufq_write(struct zbuf_queue *zbq, int fd)
188 {
189 struct iovec iov[16];
190 struct zbuf *zb, *zbn;
191 ssize_t r;
192 size_t iovcnt = 0;
193
194 list_for_each_entry_safe(zb, zbn, &zbq->queue_head, queue_list)
195 {
196 iov[iovcnt++] = (struct iovec){
197 .iov_base = zb->head, .iov_len = zbuf_used(zb),
198 };
199 if (iovcnt >= ZEBRA_NUM_OF(iov))
200 break;
201 }
202
203 r = writev(fd, iov, iovcnt);
204 if (r < 0)
205 return r;
206
207 list_for_each_entry_safe(zb, zbn, &zbq->queue_head, queue_list)
208 {
209 if (r < (ssize_t)zbuf_used(zb)) {
210 zb->head += r;
211 return 1;
212 }
213
214 r -= zbuf_used(zb);
215 list_del(&zb->queue_list);
216 zbuf_free(zb);
217 }
218
219 return 0;
220 }
221
222 void zbuf_copy(struct zbuf *zdst, struct zbuf *zsrc, size_t len)
223 {
224 const void *src;
225 void *dst;
226
227 dst = zbuf_pushn(zdst, len);
228 src = zbuf_pulln(zsrc, len);
229 if (!dst || !src)
230 return;
231 memcpy(dst, src, len);
232 }