blob: bf83d677d9b9831a106c2bd98491f12c3ae86c90 [file] [log] [blame]
Sylvain Munaut12ba7782014-06-16 10:13:40 +02001#pragma once
Harald Welteec8b4502010-02-20 20:34:29 +01002
3/* (C) 2008 by Harald Welte <laforge@gnumonks.org>
4 * All Rights Reserved
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 */
21
22#include <stdint.h>
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010023#include <osmocom/core/linuxlist.h>
24#include <osmocom/core/utils.h>
Harald Welteec8b4502010-02-20 20:34:29 +010025
Harald Welteba6988b2011-08-17 12:46:48 +020026/*! \defgroup msgb Message buffers
27 * @{
28 */
29
Harald Weltebd598e32011-08-16 23:26:52 +020030/*! \file msgb.h
31 * \brief Osmocom message buffers
32 * The Osmocom message buffers are modelled after the 'struct skb'
33 * inside the Linux kernel network stack. As they exist in userspace,
34 * they are much simplified. However, terminology such as headroom,
35 * tailroom, push/pull/put etc. remains the same.
36 */
37
Harald Welte652a7232010-07-22 21:55:24 +020038#define MSGB_DEBUG
39
Harald Weltebd598e32011-08-16 23:26:52 +020040/*! \brief Osmocom message buffer */
Harald Welteec8b4502010-02-20 20:34:29 +010041struct msgb {
Harald Weltebd598e32011-08-16 23:26:52 +020042 struct llist_head list; /*!< \brief linked list header */
Harald Welteec8b4502010-02-20 20:34:29 +010043
Pablo Neira Ayuso29cbf612011-07-07 19:46:44 +020044
Harald Welteec8b4502010-02-20 20:34:29 +010045 /* Part of which TRX logical channel we were received / transmitted */
Harald Welte074c9f92010-04-30 14:29:11 +020046 /* FIXME: move them into the control buffer */
Harald Weltec5a0ded2011-07-18 16:59:27 +020047 union {
Harald Weltebd598e32011-08-16 23:26:52 +020048 void *dst; /*!< \brief reference of origin/destination */
Harald Weltec5a0ded2011-07-18 16:59:27 +020049 struct gsm_bts_trx *trx;
50 };
Harald Weltebd598e32011-08-16 23:26:52 +020051 struct gsm_lchan *lchan; /*!< \brief logical channel */
Harald Welteec8b4502010-02-20 20:34:29 +010052
Harald Weltebd598e32011-08-16 23:26:52 +020053 unsigned char *l1h; /*!< \brief pointer to Layer1 header (if any) */
54 unsigned char *l2h; /*!< \brief pointer to A-bis layer 2 header: OML, RSL(RLL), NS */
55 unsigned char *l3h; /*!< \brief pointer to Layer 3 header. For OML: FOM; RSL: 04.08; GPRS: BSSGP */
56 unsigned char *l4h; /*!< \brief pointer to layer 4 header */
Harald Welteec8b4502010-02-20 20:34:29 +010057
Harald Weltebd598e32011-08-16 23:26:52 +020058 unsigned long cb[5]; /*!< \brief control buffer */
Harald Welte074c9f92010-04-30 14:29:11 +020059
Harald Weltebd598e32011-08-16 23:26:52 +020060 uint16_t data_len; /*!< \brief length of underlying data array */
61 uint16_t len; /*!< \brief length of bytes used in msgb */
Harald Welteec8b4502010-02-20 20:34:29 +010062
Harald Weltebd598e32011-08-16 23:26:52 +020063 unsigned char *head; /*!< \brief start of underlying memory buffer */
64 unsigned char *tail; /*!< \brief end of message in buffer */
65 unsigned char *data; /*!< \brief start of message in buffer */
66 unsigned char _data[0]; /*!< \brief optional immediate data array */
Harald Welteec8b4502010-02-20 20:34:29 +010067};
68
69extern struct msgb *msgb_alloc(uint16_t size, const char *name);
70extern void msgb_free(struct msgb *m);
71extern void msgb_enqueue(struct llist_head *queue, struct msgb *msg);
72extern struct msgb *msgb_dequeue(struct llist_head *queue);
73extern void msgb_reset(struct msgb *m);
Harald Welte972b5022012-09-08 19:58:59 +020074uint16_t msgb_length(const struct msgb *msg);
Jacob Erlbeckbaa225e2014-02-28 15:14:40 +010075extern const char *msgb_hexdump(const struct msgb *msg);
Harald Welteec8b4502010-02-20 20:34:29 +010076
Harald Welte652a7232010-07-22 21:55:24 +020077#ifdef MSGB_DEBUG
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010078#include <osmocom/core/panic.h>
Harald Welte929d8872010-11-05 07:47:41 +010079#define MSGB_ABORT(msg, fmt, args ...) do { \
Harald Weltedab02872010-11-09 13:42:26 +010080 osmo_panic("msgb(%p): " fmt, msg, ## args); \
Harald Welte929d8872010-11-05 07:47:41 +010081 } while(0)
82#else
83#define MSGB_ABORT(msg, fmt, args ...)
Harald Welte652a7232010-07-22 21:55:24 +020084#endif
85
Harald Weltebd598e32011-08-16 23:26:52 +020086/*! \brief obtain L1 header of msgb */
Harald Weltefdd0a702010-03-01 22:30:51 +010087#define msgb_l1(m) ((void *)(m->l1h))
Harald Weltebd598e32011-08-16 23:26:52 +020088/*! \brief obtain L2 header of msgb */
Harald Welteec8b4502010-02-20 20:34:29 +010089#define msgb_l2(m) ((void *)(m->l2h))
Harald Weltebd598e32011-08-16 23:26:52 +020090/*! \brief obtain L3 header of msgb */
Harald Welteec8b4502010-02-20 20:34:29 +010091#define msgb_l3(m) ((void *)(m->l3h))
Harald Weltebd598e32011-08-16 23:26:52 +020092/*! \brief obtain SMS header of msgb */
Harald Weltebb77c9d2010-04-30 14:26:12 +020093#define msgb_sms(m) ((void *)(m->l4h))
Harald Welteec8b4502010-02-20 20:34:29 +010094
Harald Weltebd598e32011-08-16 23:26:52 +020095/*! \brief determine length of L1 message
96 * \param[in] msgb message buffer
97 * \returns size of L1 message in bytes
98 *
99 * This function computes the number of bytes between the tail of the
100 * message and the layer 1 header.
101 */
Harald Weltefdd0a702010-03-01 22:30:51 +0100102static inline unsigned int msgb_l1len(const struct msgb *msgb)
103{
104 return msgb->tail - (uint8_t *)msgb_l1(msgb);
105}
106
Harald Weltebd598e32011-08-16 23:26:52 +0200107/*! \brief determine length of L2 message
108 * \param[in] msgb message buffer
109 * \returns size of L2 message in bytes
110 *
111 * This function computes the number of bytes between the tail of the
112 * message and the layer 2 header.
113 */
Harald Welteec8b4502010-02-20 20:34:29 +0100114static inline unsigned int msgb_l2len(const struct msgb *msgb)
115{
116 return msgb->tail - (uint8_t *)msgb_l2(msgb);
117}
118
Harald Weltebd598e32011-08-16 23:26:52 +0200119/*! \brief determine length of L3 message
120 * \param[in] msgb message buffer
121 * \returns size of L3 message in bytes
122 *
123 * This function computes the number of bytes between the tail of the
124 * message and the layer 3 header.
125 */
Harald Welteec8b4502010-02-20 20:34:29 +0100126static inline unsigned int msgb_l3len(const struct msgb *msgb)
127{
128 return msgb->tail - (uint8_t *)msgb_l3(msgb);
129}
130
Harald Weltebd598e32011-08-16 23:26:52 +0200131/*! \brief determine the length of the header
132 * \param[in] msgb message buffer
133 * \returns number of bytes between start of buffer and start of msg
134 *
135 * This function computes the length difference between the underlying
136 * data buffer and the used section of the \a msgb.
137 */
Harald Welteec8b4502010-02-20 20:34:29 +0100138static inline unsigned int msgb_headlen(const struct msgb *msgb)
139{
140 return msgb->len - msgb->data_len;
141}
Harald Welte652a7232010-07-22 21:55:24 +0200142
Harald Weltebd598e32011-08-16 23:26:52 +0200143/*! \brief determine how much tail room is left in msgb
144 * \param[in] msgb message buffer
145 * \returns number of bytes remaining at end of msgb
146 *
147 * This function computes the amount of octets left in the underlying
148 * data buffer after the end of the message.
149 */
Harald Welte652a7232010-07-22 21:55:24 +0200150static inline int msgb_tailroom(const struct msgb *msgb)
151{
152 return (msgb->head + msgb->data_len) - msgb->tail;
153}
154
Harald Weltebd598e32011-08-16 23:26:52 +0200155/*! \brief determine the amount of headroom in msgb
156 * \param[in] msgb message buffer
157 * \returns number of bytes left ahead of message start in msgb
158 *
159 * This function computes the amount of bytes left in the underlying
160 * data buffer before the start of the actual message.
161 */
Harald Welte652a7232010-07-22 21:55:24 +0200162static inline int msgb_headroom(const struct msgb *msgb)
163{
164 return (msgb->data - msgb->head);
165}
166
Harald Weltebd598e32011-08-16 23:26:52 +0200167/*! \brief append data to end of message buffer
168 * \param[in] msgb message buffer
169 * \param[in] len number of bytes to append to message
170 * \returns pointer to start of newly-appended data
171 *
172 * This function will move the \a tail pointer of the message buffer \a
173 * len bytes further, thus enlarging the message by \a len bytes.
174 *
175 * The return value is a pointer to start of the newly added section at
176 * the end of the message and can be used for actually filling/copying
177 * data into it.
178 */
Harald Welteec8b4502010-02-20 20:34:29 +0100179static inline unsigned char *msgb_put(struct msgb *msgb, unsigned int len)
180{
181 unsigned char *tmp = msgb->tail;
Harald Welte46a45a62010-11-09 13:41:48 +0100182 if (msgb_tailroom(msgb) < (int) len)
Harald Welte929d8872010-11-05 07:47:41 +0100183 MSGB_ABORT(msgb, "Not enough tailroom msgb_push (%u < %u)\n",
184 msgb_tailroom(msgb), len);
Harald Welteec8b4502010-02-20 20:34:29 +0100185 msgb->tail += len;
186 msgb->len += len;
187 return tmp;
188}
Harald Weltebd598e32011-08-16 23:26:52 +0200189
190/*! \brief append a uint8 value to the end of the message
191 * \param[in] msgb message buffer
192 * \param[in] word unsigned 8bit byte to be appended
193 */
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100194static inline void msgb_put_u8(struct msgb *msgb, uint8_t word)
195{
196 uint8_t *space = msgb_put(msgb, 1);
197 space[0] = word & 0xFF;
198}
Harald Weltebd598e32011-08-16 23:26:52 +0200199
200/*! \brief append a uint16 value to the end of the message
201 * \param[in] msgb message buffer
202 * \param[in] word unsigned 16bit byte to be appended
203 */
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100204static inline void msgb_put_u16(struct msgb *msgb, uint16_t word)
205{
206 uint8_t *space = msgb_put(msgb, 2);
207 space[0] = word >> 8 & 0xFF;
208 space[1] = word & 0xFF;
209}
Harald Weltebd598e32011-08-16 23:26:52 +0200210
211/*! \brief append a uint32 value to the end of the message
212 * \param[in] msgb message buffer
213 * \param[in] word unsigned 32bit byte to be appended
214 */
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100215static inline void msgb_put_u32(struct msgb *msgb, uint32_t word)
216{
217 uint8_t *space = msgb_put(msgb, 4);
218 space[0] = word >> 24 & 0xFF;
219 space[1] = word >> 16 & 0xFF;
220 space[2] = word >> 8 & 0xFF;
221 space[3] = word & 0xFF;
222}
Harald Weltebd598e32011-08-16 23:26:52 +0200223
224/*! \brief remove data from end of message
225 * \param[in] msgb message buffer
226 * \param[in] len number of bytes to remove from end
227 */
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100228static inline unsigned char *msgb_get(struct msgb *msgb, unsigned int len)
229{
Harald Welte972b5022012-09-08 19:58:59 +0200230 unsigned char *tmp = msgb->data - len;
231 if (msgb_length(msgb) < len)
232 MSGB_ABORT(msgb, "msgb too small to get %u (len %u)\n",
233 len, msgb_length(msgb));
234 msgb->tail -= len;
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100235 msgb->len -= len;
236 return tmp;
237}
Harald Weltebd598e32011-08-16 23:26:52 +0200238/*! \brief remove uint8 from end of message
239 * \param[in] msgb message buffer
240 * \returns 8bit value taken from end of msgb
241 */
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100242static inline uint8_t msgb_get_u8(struct msgb *msgb)
243{
244 uint8_t *space = msgb_get(msgb, 1);
245 return space[0];
246}
Harald Weltebd598e32011-08-16 23:26:52 +0200247/*! \brief remove uint16 from end of message
248 * \param[in] msgb message buffer
249 * \returns 16bit value taken from end of msgb
250 */
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100251static inline uint16_t msgb_get_u16(struct msgb *msgb)
252{
253 uint8_t *space = msgb_get(msgb, 2);
254 return space[0] << 8 | space[1];
255}
Harald Weltebd598e32011-08-16 23:26:52 +0200256/*! \brief remove uint32 from end of message
257 * \param[in] msgb message buffer
258 * \returns 32bit value taken from end of msgb
259 */
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100260static inline uint32_t msgb_get_u32(struct msgb *msgb)
261{
262 uint8_t *space = msgb_get(msgb, 4);
263 return space[0] << 24 | space[1] << 16 | space[2] << 8 | space[3];
264}
Harald Weltebd598e32011-08-16 23:26:52 +0200265
266/*! \brief prepend (push) some data to start of message
267 * \param[in] msgb message buffer
268 * \param[in] len number of bytes to pre-pend
269 * \returns pointer to newly added portion at start of \a msgb
270 *
271 * This function moves the \a data pointer of the \ref msgb further
272 * to the front (by \a len bytes), thereby enlarging the message by \a
273 * len bytes.
274 *
275 * The return value is a pointer to the newly added section in the
276 * beginning of the message. It can be used to fill/copy data into it.
277 */
Harald Welteec8b4502010-02-20 20:34:29 +0100278static inline unsigned char *msgb_push(struct msgb *msgb, unsigned int len)
279{
Harald Welte46a45a62010-11-09 13:41:48 +0100280 if (msgb_headroom(msgb) < (int) len)
Harald Welte929d8872010-11-05 07:47:41 +0100281 MSGB_ABORT(msgb, "Not enough headroom msgb_push (%u < %u)\n",
282 msgb_headroom(msgb), len);
Harald Welteec8b4502010-02-20 20:34:29 +0100283 msgb->data -= len;
284 msgb->len += len;
285 return msgb->data;
286}
Harald Weltebd598e32011-08-16 23:26:52 +0200287/*! \brief remove (pull) a header from the front of the message buffer
288 * \param[in] msgb message buffer
289 * \param[in] len number of octets to be pulled
290 * \returns pointer to new start of msgb
291 *
292 * This function moves the \a data pointer of the \ref msgb further back
293 * in the message, thereby shrinking the size of the message by \a len
294 * bytes.
295 */
Harald Welteec8b4502010-02-20 20:34:29 +0100296static inline unsigned char *msgb_pull(struct msgb *msgb, unsigned int len)
297{
298 msgb->len -= len;
299 return msgb->data += len;
300}
Harald Welteec8b4502010-02-20 20:34:29 +0100301
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100302/*! \brief remove (pull) all headers in front of l3h from the message buffer.
303 * \param[in] msgb message buffer with a valid l3h
304 * \returns pointer to new start of msgb (l3h)
305 *
306 * This function moves the \a data pointer of the \ref msgb further back
307 * in the message, thereby shrinking the size of the message.
308 * l1h and l2h will be cleared.
309 */
310static inline unsigned char *msgb_pull_to_l3(struct msgb *msg)
311{
312 unsigned char *ret = msgb_pull(msg, msg->l3h - msg->data);
313 msg->l1h = msg->l2h = NULL;
314 return ret;
315}
316
Harald Welte972b5022012-09-08 19:58:59 +0200317/*! \brief remove uint8 from front of message
318 * \param[in] msgb message buffer
319 * \returns 8bit value taken from end of msgb
320 */
321static inline uint8_t msgb_pull_u8(struct msgb *msgb)
322{
Steve Markgraf5905d5b2012-11-14 19:36:00 +0100323 uint8_t *space = msgb_pull(msgb, 1) - 1;
Harald Welte972b5022012-09-08 19:58:59 +0200324 return space[0];
325}
326/*! \brief remove uint16 from front of message
327 * \param[in] msgb message buffer
328 * \returns 16bit value taken from end of msgb
329 */
330static inline uint16_t msgb_pull_u16(struct msgb *msgb)
331{
Steve Markgraf5905d5b2012-11-14 19:36:00 +0100332 uint8_t *space = msgb_pull(msgb, 2) - 2;
Harald Welte972b5022012-09-08 19:58:59 +0200333 return space[0] << 8 | space[1];
334}
335/*! \brief remove uint32 from front of message
336 * \param[in] msgb message buffer
337 * \returns 32bit value taken from end of msgb
338 */
339static inline uint32_t msgb_pull_u32(struct msgb *msgb)
340{
Steve Markgraf5905d5b2012-11-14 19:36:00 +0100341 uint8_t *space = msgb_pull(msgb, 4) - 4;
Harald Welte972b5022012-09-08 19:58:59 +0200342 return space[0] << 24 | space[1] << 16 | space[2] << 8 | space[3];
343}
344
Harald Weltebd598e32011-08-16 23:26:52 +0200345/*! \brief Increase headroom of empty msgb, reducing the tailroom
346 * \param[in] msg message buffer
347 * \param[in] len amount of extra octets to be reserved as headroom
348 *
349 * This function reserves some memory at the beginning of the underlying
350 * data buffer. The idea is to reserve space in case further headers
351 * have to be pushed to the \ref msgb during further processing.
352 *
353 * Calling this function leads to undefined reusults if it is called on
354 * a non-empty \ref msgb.
355 */
Harald Welteec8b4502010-02-20 20:34:29 +0100356static inline void msgb_reserve(struct msgb *msg, int len)
357{
358 msg->data += len;
359 msg->tail += len;
360}
361
Harald Welteb0f91512012-01-14 21:53:41 +0100362/*! \brief Trim the msgb to a given absolute length
363 * \param[in] msg message buffer
364 * \param[in] len new total length of buffer
365 * \returns 0 in case of success, negative in case of error
366 */
367static inline int msgb_trim(struct msgb *msg, int len)
368{
Harald Welte3068d572012-01-14 22:07:59 +0100369 if (len > msg->data_len)
Harald Welteb0f91512012-01-14 21:53:41 +0100370 return -1;
371
Harald Welte3068d572012-01-14 22:07:59 +0100372 msg->len = len;
373 msg->tail = msg->data + len;
Harald Welteb0f91512012-01-14 21:53:41 +0100374
375 return 0;
376}
377
378/*! \brief Trim the msgb to a given layer3 length
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +0100379 * \param[in] msg message buffer
Harald Welteb0f91512012-01-14 21:53:41 +0100380 * \param[in] l3len new layer3 length
381 * \returns 0 in case of success, negative in case of error
382 */
383static inline int msgb_l3trim(struct msgb *msg, int l3len)
384{
385 return msgb_trim(msg, (msg->l3h - msg->data) + l3len);
386}
387
Harald Weltebd598e32011-08-16 23:26:52 +0200388/*! \brief Allocate message buffer with specified headroom
389 * \param[in] size size in bytes, including headroom
390 * \param[in] headroom headroom in bytes
391 * \param[in] name human-readable name
392 * \returns allocated message buffer with specified headroom
393 *
394 * This function is a convenience wrapper around \ref msgb_alloc
395 * followed by \ref msgb_reserve in order to create a new \ref msgb with
396 * user-specified amount of headroom.
397 */
Harald Welteec8b4502010-02-20 20:34:29 +0100398static inline struct msgb *msgb_alloc_headroom(int size, int headroom,
399 const char *name)
400{
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +0200401 osmo_static_assert(size > headroom, headroom_bigger);
Holger Hans Peter Freyther5853f082011-01-16 17:38:22 +0100402
Harald Welteec8b4502010-02-20 20:34:29 +0100403 struct msgb *msg = msgb_alloc(size, name);
404 if (msg)
405 msgb_reserve(msg, headroom);
406 return msg;
407}
408
Holger Hans Peter Freytheracffb602010-10-18 18:22:31 +0200409/* non inline functions to ease binding */
Harald Weltebd598e32011-08-16 23:26:52 +0200410
Holger Hans Peter Freytheracffb602010-10-18 18:22:31 +0200411uint8_t *msgb_data(const struct msgb *msg);
Harald Welte9e1f0602011-06-29 18:46:10 +0200412void msgb_set_talloc_ctx(void *ctx);
Holger Hans Peter Freytheracffb602010-10-18 18:22:31 +0200413
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200414/*! @} */