]> git.proxmox.com Git - mirror_frr.git/blame - nhrpd/zbuf.h
zebra: reduce rib workqueue retry timeout
[mirror_frr.git] / nhrpd / zbuf.h
CommitLineData
2fb975da
TT
1/* Stream/packet buffer API
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#ifndef ZBUF_H
11#define ZBUF_H
12
13#include <stdint.h>
14#include <string.h>
15#include <endian.h>
16#include <sys/types.h>
17
18#include "zassert.h"
19#include "list.h"
20
21struct zbuf {
22 struct list_head queue_list;
23 unsigned allocated : 1;
24 unsigned error : 1;
25 uint8_t *buf, *end;
26 uint8_t *head, *tail;
27};
28
29struct zbuf_queue {
30 struct list_head queue_head;
31};
32
33struct zbuf *zbuf_alloc(size_t size);
34void zbuf_init(struct zbuf *zb, void *buf, size_t len, size_t datalen);
35void zbuf_free(struct zbuf *zb);
36
37static inline size_t zbuf_size(struct zbuf *zb)
38{
39 return zb->end - zb->buf;
40}
41
42static inline size_t zbuf_used(struct zbuf *zb)
43{
44 return zb->tail - zb->head;
45}
46
47static inline size_t zbuf_tailroom(struct zbuf *zb)
48{
49 return zb->end - zb->tail;
50}
51
52static inline size_t zbuf_headroom(struct zbuf *zb)
53{
54 return zb->head - zb->buf;
55}
56
57void zbuf_reset(struct zbuf *zb);
58void zbuf_reset_head(struct zbuf *zb, void *ptr);
59ssize_t zbuf_read(struct zbuf *zb, int fd, size_t maxlen);
60ssize_t zbuf_write(struct zbuf *zb, int fd);
61ssize_t zbuf_recv(struct zbuf *zb, int fd);
62ssize_t zbuf_send(struct zbuf *zb, int fd);
63
64static inline void zbuf_set_rerror(struct zbuf *zb)
65{
66 zb->error = 1;
67 zb->head = zb->tail;
68}
69
70static inline void zbuf_set_werror(struct zbuf *zb)
71{
72 zb->error = 1;
73 zb->tail = zb->end;
74}
75
76static inline void *__zbuf_pull(struct zbuf *zb, size_t size, int error)
77{
78 void *head = zb->head;
79 if (size > zbuf_used(zb)) {
996c9314
LB
80 if (error)
81 zbuf_set_rerror(zb);
2fb975da
TT
82 return NULL;
83 }
84 zb->head += size;
85 return head;
86}
87
88#define zbuf_pull(zb, type) ((type *)__zbuf_pull(zb, sizeof(type), 1))
89#define zbuf_pulln(zb, sz) ((void *)__zbuf_pull(zb, sz, 1))
90#define zbuf_may_pull(zb, type) ((type *)__zbuf_pull(zb, sizeof(type), 0))
91#define zbuf_may_pulln(zb, sz) ((void *)__zbuf_pull(zb, sz, 0))
92
93void *zbuf_may_pull_until(struct zbuf *zb, const char *sep, struct zbuf *msg);
94
95static inline void zbuf_get(struct zbuf *zb, void *dst, size_t len)
96{
97 void *src = zbuf_pulln(zb, len);
996c9314
LB
98 if (src)
99 memcpy(dst, src, len);
2fb975da
TT
100}
101
102static inline uint8_t zbuf_get8(struct zbuf *zb)
103{
104 uint8_t *src = zbuf_pull(zb, uint8_t);
996c9314
LB
105 if (src)
106 return *src;
2fb975da
TT
107 return 0;
108}
109
110static inline uint32_t zbuf_get32(struct zbuf *zb)
111{
112 struct unaligned32 {
113 uint32_t value;
114 } __attribute__((packed));
115
116 struct unaligned32 *v = zbuf_pull(zb, struct unaligned32);
996c9314
LB
117 if (v)
118 return v->value;
2fb975da
TT
119 return 0;
120}
121
122static inline uint16_t zbuf_get_be16(struct zbuf *zb)
123{
124 struct unaligned16 {
125 uint16_t value;
126 } __attribute__((packed));
127
128 struct unaligned16 *v = zbuf_pull(zb, struct unaligned16);
996c9314
LB
129 if (v)
130 return be16toh(v->value);
2fb975da
TT
131 return 0;
132}
133
134static inline uint32_t zbuf_get_be32(struct zbuf *zb)
135{
136 return be32toh(zbuf_get32(zb));
137}
138
139static inline void *__zbuf_push(struct zbuf *zb, size_t size, int error)
140{
141 void *tail = zb->tail;
142 if (size > zbuf_tailroom(zb)) {
996c9314
LB
143 if (error)
144 zbuf_set_werror(zb);
2fb975da
TT
145 return NULL;
146 }
147 zb->tail += size;
148 return tail;
149}
150
151#define zbuf_push(zb, type) ((type *)__zbuf_push(zb, sizeof(type), 1))
152#define zbuf_pushn(zb, sz) ((void *)__zbuf_push(zb, sz, 1))
153#define zbuf_may_push(zb, type) ((type *)__zbuf_may_push(zb, sizeof(type), 0))
154#define zbuf_may_pushn(zb, sz) ((void *)__zbuf_push(zb, sz, 0))
155
156static inline void zbuf_put(struct zbuf *zb, const void *src, size_t len)
157{
158 void *dst = zbuf_pushn(zb, len);
996c9314
LB
159 if (dst)
160 memcpy(dst, src, len);
2fb975da
TT
161}
162
163static inline void zbuf_put8(struct zbuf *zb, uint8_t val)
164{
165 uint8_t *dst = zbuf_push(zb, uint8_t);
996c9314
LB
166 if (dst)
167 *dst = val;
2fb975da
TT
168}
169
170static inline void zbuf_put_be16(struct zbuf *zb, uint16_t val)
171{
172 struct unaligned16 {
173 uint16_t value;
174 } __attribute__((packed));
175
176 struct unaligned16 *v = zbuf_push(zb, struct unaligned16);
996c9314
LB
177 if (v)
178 v->value = htobe16(val);
2fb975da
TT
179}
180
181static inline void zbuf_put_be32(struct zbuf *zb, uint32_t val)
182{
183 struct unaligned32 {
184 uint32_t value;
185 } __attribute__((packed));
186
187 struct unaligned32 *v = zbuf_push(zb, struct unaligned32);
996c9314
LB
188 if (v)
189 v->value = htobe32(val);
2fb975da
TT
190}
191
192void zbuf_copy(struct zbuf *zb, struct zbuf *src, size_t len);
193
194void zbufq_init(struct zbuf_queue *);
195void zbufq_reset(struct zbuf_queue *);
196void zbufq_queue(struct zbuf_queue *, struct zbuf *);
197int zbufq_write(struct zbuf_queue *, int);
198
199#endif