treewide: remove FSF address
[osmocom-bb.git] / src / shared / libosmocore / include / osmocom / core / msgb.h
blob0390a96f3697397db18ec49b50b80159688953a5
1 #ifndef _MSGB_H
2 #define _MSGB_H
4 /* (C) 2008 by Harald Welte <laforge@gnumonks.org>
5 * All Rights Reserved
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
19 #include <stdint.h>
20 #include <osmocom/core/linuxlist.h>
21 #include <osmocom/core/utils.h>
23 /*! \defgroup msgb Message buffers
24 * @{
27 /*! \file msgb.h
28 * \brief Osmocom message buffers
29 * The Osmocom message buffers are modelled after the 'struct skb'
30 * inside the Linux kernel network stack. As they exist in userspace,
31 * they are much simplified. However, terminology such as headroom,
32 * tailroom, push/pull/put etc. remains the same.
35 #define MSGB_DEBUG
37 /*! \brief Osmocom message buffer */
38 struct msgb {
39 struct llist_head list; /*!< \brief linked list header */
42 /* Part of which TRX logical channel we were received / transmitted */
43 /* FIXME: move them into the control buffer */
44 union {
45 void *dst; /*!< \brief reference of origin/destination */
46 struct gsm_bts_trx *trx;
48 struct gsm_lchan *lchan; /*!< \brief logical channel */
50 unsigned char *l1h; /*!< \brief pointer to Layer1 header (if any) */
51 unsigned char *l2h; /*!< \brief pointer to A-bis layer 2 header: OML, RSL(RLL), NS */
52 unsigned char *l3h; /*!< \brief pointer to Layer 3 header. For OML: FOM; RSL: 04.08; GPRS: BSSGP */
53 unsigned char *l4h; /*!< \brief pointer to layer 4 header */
55 unsigned long cb[5]; /*!< \brief control buffer */
57 uint16_t data_len; /*!< \brief length of underlying data array */
58 uint16_t len; /*!< \brief length of bytes used in msgb */
60 unsigned char *head; /*!< \brief start of underlying memory buffer */
61 unsigned char *tail; /*!< \brief end of message in buffer */
62 unsigned char *data; /*!< \brief start of message in buffer */
63 unsigned char _data[0]; /*!< \brief optional immediate data array */
66 extern struct msgb *msgb_alloc(uint16_t size, const char *name);
67 extern void msgb_free(struct msgb *m);
68 extern void msgb_enqueue(struct llist_head *queue, struct msgb *msg);
69 extern struct msgb *msgb_dequeue(struct llist_head *queue);
70 extern void msgb_reset(struct msgb *m);
71 uint16_t msgb_length(const struct msgb *msg);
73 #ifdef MSGB_DEBUG
74 #include <osmocom/core/panic.h>
75 #define MSGB_ABORT(msg, fmt, args ...) do { \
76 osmo_panic("msgb(%p): " fmt, msg, ## args); \
77 } while(0)
78 #else
79 #define MSGB_ABORT(msg, fmt, args ...)
80 #endif
82 /*! \brief obtain L1 header of msgb */
83 #define msgb_l1(m) ((void *)(m->l1h))
84 /*! \brief obtain L2 header of msgb */
85 #define msgb_l2(m) ((void *)(m->l2h))
86 /*! \brief obtain L3 header of msgb */
87 #define msgb_l3(m) ((void *)(m->l3h))
88 /*! \brief obtain SMS header of msgb */
89 #define msgb_sms(m) ((void *)(m->l4h))
91 /*! \brief determine length of L1 message
92 * \param[in] msgb message buffer
93 * \returns size of L1 message in bytes
95 * This function computes the number of bytes between the tail of the
96 * message and the layer 1 header.
98 static inline unsigned int msgb_l1len(const struct msgb *msgb)
100 return msgb->tail - (uint8_t *)msgb_l1(msgb);
103 /*! \brief determine length of L2 message
104 * \param[in] msgb message buffer
105 * \returns size of L2 message in bytes
107 * This function computes the number of bytes between the tail of the
108 * message and the layer 2 header.
110 static inline unsigned int msgb_l2len(const struct msgb *msgb)
112 return msgb->tail - (uint8_t *)msgb_l2(msgb);
115 /*! \brief determine length of L3 message
116 * \param[in] msgb message buffer
117 * \returns size of L3 message in bytes
119 * This function computes the number of bytes between the tail of the
120 * message and the layer 3 header.
122 static inline unsigned int msgb_l3len(const struct msgb *msgb)
124 return msgb->tail - (uint8_t *)msgb_l3(msgb);
127 /*! \brief determine the length of the header
128 * \param[in] msgb message buffer
129 * \returns number of bytes between start of buffer and start of msg
131 * This function computes the length difference between the underlying
132 * data buffer and the used section of the \a msgb.
134 static inline unsigned int msgb_headlen(const struct msgb *msgb)
136 return msgb->len - msgb->data_len;
139 /*! \brief determine how much tail room is left in msgb
140 * \param[in] msgb message buffer
141 * \returns number of bytes remaining at end of msgb
143 * This function computes the amount of octets left in the underlying
144 * data buffer after the end of the message.
146 static inline int msgb_tailroom(const struct msgb *msgb)
148 return (msgb->head + msgb->data_len) - msgb->tail;
151 /*! \brief determine the amount of headroom in msgb
152 * \param[in] msgb message buffer
153 * \returns number of bytes left ahead of message start in msgb
155 * This function computes the amount of bytes left in the underlying
156 * data buffer before the start of the actual message.
158 static inline int msgb_headroom(const struct msgb *msgb)
160 return (msgb->data - msgb->head);
163 /*! \brief append data to end of message buffer
164 * \param[in] msgb message buffer
165 * \param[in] len number of bytes to append to message
166 * \returns pointer to start of newly-appended data
168 * This function will move the \a tail pointer of the message buffer \a
169 * len bytes further, thus enlarging the message by \a len bytes.
171 * The return value is a pointer to start of the newly added section at
172 * the end of the message and can be used for actually filling/copying
173 * data into it.
175 static inline unsigned char *msgb_put(struct msgb *msgb, unsigned int len)
177 unsigned char *tmp = msgb->tail;
178 if (msgb_tailroom(msgb) < (int) len)
179 MSGB_ABORT(msgb, "Not enough tailroom msgb_push (%u < %u)\n",
180 msgb_tailroom(msgb), len);
181 msgb->tail += len;
182 msgb->len += len;
183 return tmp;
186 /*! \brief append a uint8 value to the end of the message
187 * \param[in] msgb message buffer
188 * \param[in] word unsigned 8bit byte to be appended
190 static inline void msgb_put_u8(struct msgb *msgb, uint8_t word)
192 uint8_t *space = msgb_put(msgb, 1);
193 space[0] = word & 0xFF;
196 /*! \brief append a uint16 value to the end of the message
197 * \param[in] msgb message buffer
198 * \param[in] word unsigned 16bit byte to be appended
200 static inline void msgb_put_u16(struct msgb *msgb, uint16_t word)
202 uint8_t *space = msgb_put(msgb, 2);
203 space[0] = word >> 8 & 0xFF;
204 space[1] = word & 0xFF;
207 /*! \brief append a uint32 value to the end of the message
208 * \param[in] msgb message buffer
209 * \param[in] word unsigned 32bit byte to be appended
211 static inline void msgb_put_u32(struct msgb *msgb, uint32_t word)
213 uint8_t *space = msgb_put(msgb, 4);
214 space[0] = word >> 24 & 0xFF;
215 space[1] = word >> 16 & 0xFF;
216 space[2] = word >> 8 & 0xFF;
217 space[3] = word & 0xFF;
220 /*! \brief remove data from end of message
221 * \param[in] msgb message buffer
222 * \param[in] len number of bytes to remove from end
224 static inline unsigned char *msgb_get(struct msgb *msgb, unsigned int len)
226 unsigned char *tmp = msgb->data - len;
227 if (msgb_length(msgb) < len)
228 MSGB_ABORT(msgb, "msgb too small to get %u (len %u)\n",
229 len, msgb_length(msgb));
230 msgb->tail -= len;
231 msgb->len -= len;
232 return tmp;
234 /*! \brief remove uint8 from end of message
235 * \param[in] msgb message buffer
236 * \returns 8bit value taken from end of msgb
238 static inline uint8_t msgb_get_u8(struct msgb *msgb)
240 uint8_t *space = msgb_get(msgb, 1);
241 return space[0];
243 /*! \brief remove uint16 from end of message
244 * \param[in] msgb message buffer
245 * \returns 16bit value taken from end of msgb
247 static inline uint16_t msgb_get_u16(struct msgb *msgb)
249 uint8_t *space = msgb_get(msgb, 2);
250 return space[0] << 8 | space[1];
252 /*! \brief remove uint32 from end of message
253 * \param[in] msgb message buffer
254 * \returns 32bit value taken from end of msgb
256 static inline uint32_t msgb_get_u32(struct msgb *msgb)
258 uint8_t *space = msgb_get(msgb, 4);
259 return space[0] << 24 | space[1] << 16 | space[2] << 8 | space[3];
262 /*! \brief prepend (push) some data to start of message
263 * \param[in] msgb message buffer
264 * \param[in] len number of bytes to pre-pend
265 * \returns pointer to newly added portion at start of \a msgb
267 * This function moves the \a data pointer of the \ref msgb further
268 * to the front (by \a len bytes), thereby enlarging the message by \a
269 * len bytes.
271 * The return value is a pointer to the newly added section in the
272 * beginning of the message. It can be used to fill/copy data into it.
274 static inline unsigned char *msgb_push(struct msgb *msgb, unsigned int len)
276 if (msgb_headroom(msgb) < (int) len)
277 MSGB_ABORT(msgb, "Not enough headroom msgb_push (%u < %u)\n",
278 msgb_headroom(msgb), len);
279 msgb->data -= len;
280 msgb->len += len;
281 return msgb->data;
283 /*! \brief remove (pull) a header from the front of the message buffer
284 * \param[in] msgb message buffer
285 * \param[in] len number of octets to be pulled
286 * \returns pointer to new start of msgb
288 * This function moves the \a data pointer of the \ref msgb further back
289 * in the message, thereby shrinking the size of the message by \a len
290 * bytes.
292 static inline unsigned char *msgb_pull(struct msgb *msgb, unsigned int len)
294 msgb->len -= len;
295 return msgb->data += len;
298 /*! \brief remove uint8 from front of message
299 * \param[in] msgb message buffer
300 * \returns 8bit value taken from end of msgb
302 static inline uint8_t msgb_pull_u8(struct msgb *msgb)
304 uint8_t *space = msgb_pull(msgb, 1) - 1;
305 return space[0];
307 /*! \brief remove uint16 from front of message
308 * \param[in] msgb message buffer
309 * \returns 16bit value taken from end of msgb
311 static inline uint16_t msgb_pull_u16(struct msgb *msgb)
313 uint8_t *space = msgb_pull(msgb, 2) - 2;
314 return space[0] << 8 | space[1];
316 /*! \brief remove uint32 from front of message
317 * \param[in] msgb message buffer
318 * \returns 32bit value taken from end of msgb
320 static inline uint32_t msgb_pull_u32(struct msgb *msgb)
322 uint8_t *space = msgb_pull(msgb, 4) - 4;
323 return space[0] << 24 | space[1] << 16 | space[2] << 8 | space[3];
326 /*! \brief Increase headroom of empty msgb, reducing the tailroom
327 * \param[in] msg message buffer
328 * \param[in] len amount of extra octets to be reserved as headroom
330 * This function reserves some memory at the beginning of the underlying
331 * data buffer. The idea is to reserve space in case further headers
332 * have to be pushed to the \ref msgb during further processing.
334 * Calling this function leads to undefined reusults if it is called on
335 * a non-empty \ref msgb.
337 static inline void msgb_reserve(struct msgb *msg, int len)
339 msg->data += len;
340 msg->tail += len;
343 /*! \brief Trim the msgb to a given absolute length
344 * \param[in] msg message buffer
345 * \param[in] len new total length of buffer
346 * \returns 0 in case of success, negative in case of error
348 static inline int msgb_trim(struct msgb *msg, int len)
350 if (len > msg->data_len)
351 return -1;
353 msg->len = len;
354 msg->tail = msg->data + len;
356 return 0;
359 /*! \brief Trim the msgb to a given layer3 length
360 * \pram[in] msg message buffer
361 * \param[in] l3len new layer3 length
362 * \returns 0 in case of success, negative in case of error
364 static inline int msgb_l3trim(struct msgb *msg, int l3len)
366 return msgb_trim(msg, (msg->l3h - msg->data) + l3len);
369 /*! \brief Allocate message buffer with specified headroom
370 * \param[in] size size in bytes, including headroom
371 * \param[in] headroom headroom in bytes
372 * \param[in] name human-readable name
373 * \returns allocated message buffer with specified headroom
375 * This function is a convenience wrapper around \ref msgb_alloc
376 * followed by \ref msgb_reserve in order to create a new \ref msgb with
377 * user-specified amount of headroom.
379 static inline struct msgb *msgb_alloc_headroom(int size, int headroom,
380 const char *name)
382 osmo_static_assert(size > headroom, headroom_bigger);
384 struct msgb *msg = msgb_alloc(size, name);
385 if (msg)
386 msgb_reserve(msg, headroom);
387 return msg;
390 /* non inline functions to ease binding */
392 uint8_t *msgb_data(const struct msgb *msg);
393 void msgb_set_talloc_ctx(void *ctx);
395 /*! @} */
397 #endif /* _MSGB_H */