blob: 5d4bd84bc95bf64c8471c0e348568602fb963677 [file] [log] [blame]
Harald Welteec8b4502010-02-20 20:34:29 +01001#ifndef _MSGB_H
2#define _MSGB_H
3
4/* (C) 2008 by Harald Welte <laforge@gnumonks.org>
5 * All Rights Reserved
6 *
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.
11 *
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.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 */
22
23#include <stdint.h>
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010024#include <osmocom/core/linuxlist.h>
25#include <osmocom/core/utils.h>
Harald Welteec8b4502010-02-20 20:34:29 +010026
Harald Welteba6988b2011-08-17 12:46:48 +020027/*! \defgroup msgb Message buffers
28 * @{
29 */
30
Harald Weltebd598e32011-08-16 23:26:52 +020031/*! \file msgb.h
32 * \brief Osmocom message buffers
33 * The Osmocom message buffers are modelled after the 'struct skb'
34 * inside the Linux kernel network stack. As they exist in userspace,
35 * they are much simplified. However, terminology such as headroom,
36 * tailroom, push/pull/put etc. remains the same.
37 */
38
Harald Welte652a7232010-07-22 21:55:24 +020039#define MSGB_DEBUG
40
Harald Weltebd598e32011-08-16 23:26:52 +020041/*! \brief Osmocom message buffer */
Harald Welteec8b4502010-02-20 20:34:29 +010042struct msgb {
Harald Weltebd598e32011-08-16 23:26:52 +020043 struct llist_head list; /*!< \brief linked list header */
Harald Welteec8b4502010-02-20 20:34:29 +010044
Pablo Neira Ayuso29cbf612011-07-07 19:46:44 +020045
Harald Welteec8b4502010-02-20 20:34:29 +010046 /* Part of which TRX logical channel we were received / transmitted */
Harald Welte074c9f92010-04-30 14:29:11 +020047 /* FIXME: move them into the control buffer */
Harald Weltec5a0ded2011-07-18 16:59:27 +020048 union {
Harald Weltebd598e32011-08-16 23:26:52 +020049 void *dst; /*!< \brief reference of origin/destination */
Harald Weltec5a0ded2011-07-18 16:59:27 +020050 struct gsm_bts_trx *trx;
51 };
Harald Weltebd598e32011-08-16 23:26:52 +020052 struct gsm_lchan *lchan; /*!< \brief logical channel */
Harald Welteec8b4502010-02-20 20:34:29 +010053
Harald Weltebd598e32011-08-16 23:26:52 +020054 unsigned char *l1h; /*!< \brief pointer to Layer1 header (if any) */
55 unsigned char *l2h; /*!< \brief pointer to A-bis layer 2 header: OML, RSL(RLL), NS */
56 unsigned char *l3h; /*!< \brief pointer to Layer 3 header. For OML: FOM; RSL: 04.08; GPRS: BSSGP */
57 unsigned char *l4h; /*!< \brief pointer to layer 4 header */
Harald Welteec8b4502010-02-20 20:34:29 +010058
Harald Weltebd598e32011-08-16 23:26:52 +020059 unsigned long cb[5]; /*!< \brief control buffer */
Harald Welte074c9f92010-04-30 14:29:11 +020060
Harald Weltebd598e32011-08-16 23:26:52 +020061 uint16_t data_len; /*!< \brief length of underlying data array */
62 uint16_t len; /*!< \brief length of bytes used in msgb */
Harald Welteec8b4502010-02-20 20:34:29 +010063
Harald Weltebd598e32011-08-16 23:26:52 +020064 unsigned char *head; /*!< \brief start of underlying memory buffer */
65 unsigned char *tail; /*!< \brief end of message in buffer */
66 unsigned char *data; /*!< \brief start of message in buffer */
67 unsigned char _data[0]; /*!< \brief optional immediate data array */
Harald Welteec8b4502010-02-20 20:34:29 +010068};
69
70extern struct msgb *msgb_alloc(uint16_t size, const char *name);
71extern void msgb_free(struct msgb *m);
72extern void msgb_enqueue(struct llist_head *queue, struct msgb *msg);
73extern struct msgb *msgb_dequeue(struct llist_head *queue);
74extern void msgb_reset(struct msgb *m);
Harald Welte972b5022012-09-08 19:58:59 +020075uint16_t msgb_length(const struct msgb *msg);
Jacob Erlbeckbaa225e2014-02-28 15:14:40 +010076extern const char *msgb_hexdump(const struct msgb *msg);
Harald Welteec8b4502010-02-20 20:34:29 +010077
Harald Welte652a7232010-07-22 21:55:24 +020078#ifdef MSGB_DEBUG
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010079#include <osmocom/core/panic.h>
Harald Welte929d8872010-11-05 07:47:41 +010080#define MSGB_ABORT(msg, fmt, args ...) do { \
Harald Weltedab02872010-11-09 13:42:26 +010081 osmo_panic("msgb(%p): " fmt, msg, ## args); \
Harald Welte929d8872010-11-05 07:47:41 +010082 } while(0)
83#else
84#define MSGB_ABORT(msg, fmt, args ...)
Harald Welte652a7232010-07-22 21:55:24 +020085#endif
86
Harald Weltebd598e32011-08-16 23:26:52 +020087/*! \brief obtain L1 header of msgb */
Harald Weltefdd0a702010-03-01 22:30:51 +010088#define msgb_l1(m) ((void *)(m->l1h))
Harald Weltebd598e32011-08-16 23:26:52 +020089/*! \brief obtain L2 header of msgb */
Harald Welteec8b4502010-02-20 20:34:29 +010090#define msgb_l2(m) ((void *)(m->l2h))
Harald Weltebd598e32011-08-16 23:26:52 +020091/*! \brief obtain L3 header of msgb */
Harald Welteec8b4502010-02-20 20:34:29 +010092#define msgb_l3(m) ((void *)(m->l3h))
Harald Weltebd598e32011-08-16 23:26:52 +020093/*! \brief obtain SMS header of msgb */
Harald Weltebb77c9d2010-04-30 14:26:12 +020094#define msgb_sms(m) ((void *)(m->l4h))
Harald Welteec8b4502010-02-20 20:34:29 +010095
Harald Weltebd598e32011-08-16 23:26:52 +020096/*! \brief determine length of L1 message
97 * \param[in] msgb message buffer
98 * \returns size of L1 message in bytes
99 *
100 * This function computes the number of bytes between the tail of the
101 * message and the layer 1 header.
102 */
Harald Weltefdd0a702010-03-01 22:30:51 +0100103static inline unsigned int msgb_l1len(const struct msgb *msgb)
104{
105 return msgb->tail - (uint8_t *)msgb_l1(msgb);
106}
107
Harald Weltebd598e32011-08-16 23:26:52 +0200108/*! \brief determine length of L2 message
109 * \param[in] msgb message buffer
110 * \returns size of L2 message in bytes
111 *
112 * This function computes the number of bytes between the tail of the
113 * message and the layer 2 header.
114 */
Harald Welteec8b4502010-02-20 20:34:29 +0100115static inline unsigned int msgb_l2len(const struct msgb *msgb)
116{
117 return msgb->tail - (uint8_t *)msgb_l2(msgb);
118}
119
Harald Weltebd598e32011-08-16 23:26:52 +0200120/*! \brief determine length of L3 message
121 * \param[in] msgb message buffer
122 * \returns size of L3 message in bytes
123 *
124 * This function computes the number of bytes between the tail of the
125 * message and the layer 3 header.
126 */
Harald Welteec8b4502010-02-20 20:34:29 +0100127static inline unsigned int msgb_l3len(const struct msgb *msgb)
128{
129 return msgb->tail - (uint8_t *)msgb_l3(msgb);
130}
131
Harald Weltebd598e32011-08-16 23:26:52 +0200132/*! \brief determine the length of the header
133 * \param[in] msgb message buffer
134 * \returns number of bytes between start of buffer and start of msg
135 *
136 * This function computes the length difference between the underlying
137 * data buffer and the used section of the \a msgb.
138 */
Harald Welteec8b4502010-02-20 20:34:29 +0100139static inline unsigned int msgb_headlen(const struct msgb *msgb)
140{
141 return msgb->len - msgb->data_len;
142}
Harald Welte652a7232010-07-22 21:55:24 +0200143
Harald Weltebd598e32011-08-16 23:26:52 +0200144/*! \brief determine how much tail room is left in msgb
145 * \param[in] msgb message buffer
146 * \returns number of bytes remaining at end of msgb
147 *
148 * This function computes the amount of octets left in the underlying
149 * data buffer after the end of the message.
150 */
Harald Welte652a7232010-07-22 21:55:24 +0200151static inline int msgb_tailroom(const struct msgb *msgb)
152{
153 return (msgb->head + msgb->data_len) - msgb->tail;
154}
155
Harald Weltebd598e32011-08-16 23:26:52 +0200156/*! \brief determine the amount of headroom in msgb
157 * \param[in] msgb message buffer
158 * \returns number of bytes left ahead of message start in msgb
159 *
160 * This function computes the amount of bytes left in the underlying
161 * data buffer before the start of the actual message.
162 */
Harald Welte652a7232010-07-22 21:55:24 +0200163static inline int msgb_headroom(const struct msgb *msgb)
164{
165 return (msgb->data - msgb->head);
166}
167
Harald Weltebd598e32011-08-16 23:26:52 +0200168/*! \brief append data to end of message buffer
169 * \param[in] msgb message buffer
170 * \param[in] len number of bytes to append to message
171 * \returns pointer to start of newly-appended data
172 *
173 * This function will move the \a tail pointer of the message buffer \a
174 * len bytes further, thus enlarging the message by \a len bytes.
175 *
176 * The return value is a pointer to start of the newly added section at
177 * the end of the message and can be used for actually filling/copying
178 * data into it.
179 */
Harald Welteec8b4502010-02-20 20:34:29 +0100180static inline unsigned char *msgb_put(struct msgb *msgb, unsigned int len)
181{
182 unsigned char *tmp = msgb->tail;
Harald Welte46a45a62010-11-09 13:41:48 +0100183 if (msgb_tailroom(msgb) < (int) len)
Harald Welte929d8872010-11-05 07:47:41 +0100184 MSGB_ABORT(msgb, "Not enough tailroom msgb_push (%u < %u)\n",
185 msgb_tailroom(msgb), len);
Harald Welteec8b4502010-02-20 20:34:29 +0100186 msgb->tail += len;
187 msgb->len += len;
188 return tmp;
189}
Harald Weltebd598e32011-08-16 23:26:52 +0200190
191/*! \brief append a uint8 value to the end of the message
192 * \param[in] msgb message buffer
193 * \param[in] word unsigned 8bit byte to be appended
194 */
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100195static inline void msgb_put_u8(struct msgb *msgb, uint8_t word)
196{
197 uint8_t *space = msgb_put(msgb, 1);
198 space[0] = word & 0xFF;
199}
Harald Weltebd598e32011-08-16 23:26:52 +0200200
201/*! \brief append a uint16 value to the end of the message
202 * \param[in] msgb message buffer
203 * \param[in] word unsigned 16bit byte to be appended
204 */
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100205static inline void msgb_put_u16(struct msgb *msgb, uint16_t word)
206{
207 uint8_t *space = msgb_put(msgb, 2);
208 space[0] = word >> 8 & 0xFF;
209 space[1] = word & 0xFF;
210}
Harald Weltebd598e32011-08-16 23:26:52 +0200211
212/*! \brief append a uint32 value to the end of the message
213 * \param[in] msgb message buffer
214 * \param[in] word unsigned 32bit byte to be appended
215 */
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100216static inline void msgb_put_u32(struct msgb *msgb, uint32_t word)
217{
218 uint8_t *space = msgb_put(msgb, 4);
219 space[0] = word >> 24 & 0xFF;
220 space[1] = word >> 16 & 0xFF;
221 space[2] = word >> 8 & 0xFF;
222 space[3] = word & 0xFF;
223}
Harald Weltebd598e32011-08-16 23:26:52 +0200224
225/*! \brief remove data from end of message
226 * \param[in] msgb message buffer
227 * \param[in] len number of bytes to remove from end
228 */
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100229static inline unsigned char *msgb_get(struct msgb *msgb, unsigned int len)
230{
Harald Welte972b5022012-09-08 19:58:59 +0200231 unsigned char *tmp = msgb->data - len;
232 if (msgb_length(msgb) < len)
233 MSGB_ABORT(msgb, "msgb too small to get %u (len %u)\n",
234 len, msgb_length(msgb));
235 msgb->tail -= len;
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100236 msgb->len -= len;
237 return tmp;
238}
Harald Weltebd598e32011-08-16 23:26:52 +0200239/*! \brief remove uint8 from end of message
240 * \param[in] msgb message buffer
241 * \returns 8bit value taken from end of msgb
242 */
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100243static inline uint8_t msgb_get_u8(struct msgb *msgb)
244{
245 uint8_t *space = msgb_get(msgb, 1);
246 return space[0];
247}
Harald Weltebd598e32011-08-16 23:26:52 +0200248/*! \brief remove uint16 from end of message
249 * \param[in] msgb message buffer
250 * \returns 16bit value taken from end of msgb
251 */
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100252static inline uint16_t msgb_get_u16(struct msgb *msgb)
253{
254 uint8_t *space = msgb_get(msgb, 2);
255 return space[0] << 8 | space[1];
256}
Harald Weltebd598e32011-08-16 23:26:52 +0200257/*! \brief remove uint32 from end of message
258 * \param[in] msgb message buffer
259 * \returns 32bit value taken from end of msgb
260 */
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100261static inline uint32_t msgb_get_u32(struct msgb *msgb)
262{
263 uint8_t *space = msgb_get(msgb, 4);
264 return space[0] << 24 | space[1] << 16 | space[2] << 8 | space[3];
265}
Harald Weltebd598e32011-08-16 23:26:52 +0200266
267/*! \brief prepend (push) some data to start of message
268 * \param[in] msgb message buffer
269 * \param[in] len number of bytes to pre-pend
270 * \returns pointer to newly added portion at start of \a msgb
271 *
272 * This function moves the \a data pointer of the \ref msgb further
273 * to the front (by \a len bytes), thereby enlarging the message by \a
274 * len bytes.
275 *
276 * The return value is a pointer to the newly added section in the
277 * beginning of the message. It can be used to fill/copy data into it.
278 */
Harald Welteec8b4502010-02-20 20:34:29 +0100279static inline unsigned char *msgb_push(struct msgb *msgb, unsigned int len)
280{
Harald Welte46a45a62010-11-09 13:41:48 +0100281 if (msgb_headroom(msgb) < (int) len)
Harald Welte929d8872010-11-05 07:47:41 +0100282 MSGB_ABORT(msgb, "Not enough headroom msgb_push (%u < %u)\n",
283 msgb_headroom(msgb), len);
Harald Welteec8b4502010-02-20 20:34:29 +0100284 msgb->data -= len;
285 msgb->len += len;
286 return msgb->data;
287}
Harald Weltebd598e32011-08-16 23:26:52 +0200288/*! \brief remove (pull) a header from the front of the message buffer
289 * \param[in] msgb message buffer
290 * \param[in] len number of octets to be pulled
291 * \returns pointer to new start of msgb
292 *
293 * This function moves the \a data pointer of the \ref msgb further back
294 * in the message, thereby shrinking the size of the message by \a len
295 * bytes.
296 */
Harald Welteec8b4502010-02-20 20:34:29 +0100297static inline unsigned char *msgb_pull(struct msgb *msgb, unsigned int len)
298{
299 msgb->len -= len;
300 return msgb->data += len;
301}
Harald Welteec8b4502010-02-20 20:34:29 +0100302
Harald Welte972b5022012-09-08 19:58:59 +0200303/*! \brief remove uint8 from front of message
304 * \param[in] msgb message buffer
305 * \returns 8bit value taken from end of msgb
306 */
307static inline uint8_t msgb_pull_u8(struct msgb *msgb)
308{
Steve Markgraf5905d5b2012-11-14 19:36:00 +0100309 uint8_t *space = msgb_pull(msgb, 1) - 1;
Harald Welte972b5022012-09-08 19:58:59 +0200310 return space[0];
311}
312/*! \brief remove uint16 from front of message
313 * \param[in] msgb message buffer
314 * \returns 16bit value taken from end of msgb
315 */
316static inline uint16_t msgb_pull_u16(struct msgb *msgb)
317{
Steve Markgraf5905d5b2012-11-14 19:36:00 +0100318 uint8_t *space = msgb_pull(msgb, 2) - 2;
Harald Welte972b5022012-09-08 19:58:59 +0200319 return space[0] << 8 | space[1];
320}
321/*! \brief remove uint32 from front of message
322 * \param[in] msgb message buffer
323 * \returns 32bit value taken from end of msgb
324 */
325static inline uint32_t msgb_pull_u32(struct msgb *msgb)
326{
Steve Markgraf5905d5b2012-11-14 19:36:00 +0100327 uint8_t *space = msgb_pull(msgb, 4) - 4;
Harald Welte972b5022012-09-08 19:58:59 +0200328 return space[0] << 24 | space[1] << 16 | space[2] << 8 | space[3];
329}
330
Harald Weltebd598e32011-08-16 23:26:52 +0200331/*! \brief Increase headroom of empty msgb, reducing the tailroom
332 * \param[in] msg message buffer
333 * \param[in] len amount of extra octets to be reserved as headroom
334 *
335 * This function reserves some memory at the beginning of the underlying
336 * data buffer. The idea is to reserve space in case further headers
337 * have to be pushed to the \ref msgb during further processing.
338 *
339 * Calling this function leads to undefined reusults if it is called on
340 * a non-empty \ref msgb.
341 */
Harald Welteec8b4502010-02-20 20:34:29 +0100342static inline void msgb_reserve(struct msgb *msg, int len)
343{
344 msg->data += len;
345 msg->tail += len;
346}
347
Harald Welteb0f91512012-01-14 21:53:41 +0100348/*! \brief Trim the msgb to a given absolute length
349 * \param[in] msg message buffer
350 * \param[in] len new total length of buffer
351 * \returns 0 in case of success, negative in case of error
352 */
353static inline int msgb_trim(struct msgb *msg, int len)
354{
Harald Welte3068d572012-01-14 22:07:59 +0100355 if (len > msg->data_len)
Harald Welteb0f91512012-01-14 21:53:41 +0100356 return -1;
357
Harald Welte3068d572012-01-14 22:07:59 +0100358 msg->len = len;
359 msg->tail = msg->data + len;
Harald Welteb0f91512012-01-14 21:53:41 +0100360
361 return 0;
362}
363
364/*! \brief Trim the msgb to a given layer3 length
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +0100365 * \param[in] msg message buffer
Harald Welteb0f91512012-01-14 21:53:41 +0100366 * \param[in] l3len new layer3 length
367 * \returns 0 in case of success, negative in case of error
368 */
369static inline int msgb_l3trim(struct msgb *msg, int l3len)
370{
371 return msgb_trim(msg, (msg->l3h - msg->data) + l3len);
372}
373
Harald Weltebd598e32011-08-16 23:26:52 +0200374/*! \brief Allocate message buffer with specified headroom
375 * \param[in] size size in bytes, including headroom
376 * \param[in] headroom headroom in bytes
377 * \param[in] name human-readable name
378 * \returns allocated message buffer with specified headroom
379 *
380 * This function is a convenience wrapper around \ref msgb_alloc
381 * followed by \ref msgb_reserve in order to create a new \ref msgb with
382 * user-specified amount of headroom.
383 */
Harald Welteec8b4502010-02-20 20:34:29 +0100384static inline struct msgb *msgb_alloc_headroom(int size, int headroom,
385 const char *name)
386{
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +0200387 osmo_static_assert(size > headroom, headroom_bigger);
Holger Hans Peter Freyther5853f082011-01-16 17:38:22 +0100388
Harald Welteec8b4502010-02-20 20:34:29 +0100389 struct msgb *msg = msgb_alloc(size, name);
390 if (msg)
391 msgb_reserve(msg, headroom);
392 return msg;
393}
394
Holger Hans Peter Freytheracffb602010-10-18 18:22:31 +0200395/* non inline functions to ease binding */
Harald Weltebd598e32011-08-16 23:26:52 +0200396
Holger Hans Peter Freytheracffb602010-10-18 18:22:31 +0200397uint8_t *msgb_data(const struct msgb *msg);
Harald Welte9e1f0602011-06-29 18:46:10 +0200398void msgb_set_talloc_ctx(void *ctx);
Holger Hans Peter Freytheracffb602010-10-18 18:22:31 +0200399
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200400/*! @} */
Harald Welteba6988b2011-08-17 12:46:48 +0200401
Harald Welteec8b4502010-02-20 20:34:29 +0100402#endif /* _MSGB_H */