blob: 9ffc64efb1daed862b817020f10e04ee57d362f1 [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>
Max53777012014-06-04 19:07:41 +020025#include <osmocom/core/bits.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);
Jacob Erlbeckcdd05f02015-11-27 13:26:13 +010077extern int msgb_resize_area(struct msgb *msg, uint8_t *area,
78 int old_size, int new_size);
79extern struct msgb *msgb_copy(const struct msgb *msg, const char *name);
Harald Welteec8b4502010-02-20 20:34:29 +010080
Harald Welte652a7232010-07-22 21:55:24 +020081#ifdef MSGB_DEBUG
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010082#include <osmocom/core/panic.h>
Harald Welte929d8872010-11-05 07:47:41 +010083#define MSGB_ABORT(msg, fmt, args ...) do { \
Harald Weltedab02872010-11-09 13:42:26 +010084 osmo_panic("msgb(%p): " fmt, msg, ## args); \
Harald Welte929d8872010-11-05 07:47:41 +010085 } while(0)
86#else
87#define MSGB_ABORT(msg, fmt, args ...)
Harald Welte652a7232010-07-22 21:55:24 +020088#endif
89
Harald Weltebd598e32011-08-16 23:26:52 +020090/*! \brief obtain L1 header of msgb */
Harald Weltefdd0a702010-03-01 22:30:51 +010091#define msgb_l1(m) ((void *)(m->l1h))
Harald Weltebd598e32011-08-16 23:26:52 +020092/*! \brief obtain L2 header of msgb */
Harald Welteec8b4502010-02-20 20:34:29 +010093#define msgb_l2(m) ((void *)(m->l2h))
Harald Weltebd598e32011-08-16 23:26:52 +020094/*! \brief obtain L3 header of msgb */
Harald Welteec8b4502010-02-20 20:34:29 +010095#define msgb_l3(m) ((void *)(m->l3h))
Harald Weltebd598e32011-08-16 23:26:52 +020096/*! \brief obtain SMS header of msgb */
Harald Weltebb77c9d2010-04-30 14:26:12 +020097#define msgb_sms(m) ((void *)(m->l4h))
Harald Welteec8b4502010-02-20 20:34:29 +010098
Harald Weltebd598e32011-08-16 23:26:52 +020099/*! \brief determine length of L1 message
100 * \param[in] msgb message buffer
101 * \returns size of L1 message in bytes
102 *
103 * This function computes the number of bytes between the tail of the
104 * message and the layer 1 header.
105 */
Harald Weltefdd0a702010-03-01 22:30:51 +0100106static inline unsigned int msgb_l1len(const struct msgb *msgb)
107{
108 return msgb->tail - (uint8_t *)msgb_l1(msgb);
109}
110
Harald Weltebd598e32011-08-16 23:26:52 +0200111/*! \brief determine length of L2 message
112 * \param[in] msgb message buffer
113 * \returns size of L2 message in bytes
114 *
115 * This function computes the number of bytes between the tail of the
116 * message and the layer 2 header.
117 */
Harald Welteec8b4502010-02-20 20:34:29 +0100118static inline unsigned int msgb_l2len(const struct msgb *msgb)
119{
120 return msgb->tail - (uint8_t *)msgb_l2(msgb);
121}
122
Harald Weltebd598e32011-08-16 23:26:52 +0200123/*! \brief determine length of L3 message
124 * \param[in] msgb message buffer
125 * \returns size of L3 message in bytes
126 *
127 * This function computes the number of bytes between the tail of the
128 * message and the layer 3 header.
129 */
Harald Welteec8b4502010-02-20 20:34:29 +0100130static inline unsigned int msgb_l3len(const struct msgb *msgb)
131{
132 return msgb->tail - (uint8_t *)msgb_l3(msgb);
133}
134
Harald Weltebd598e32011-08-16 23:26:52 +0200135/*! \brief determine the length of the header
136 * \param[in] msgb message buffer
137 * \returns number of bytes between start of buffer and start of msg
138 *
139 * This function computes the length difference between the underlying
140 * data buffer and the used section of the \a msgb.
141 */
Harald Welteec8b4502010-02-20 20:34:29 +0100142static inline unsigned int msgb_headlen(const struct msgb *msgb)
143{
144 return msgb->len - msgb->data_len;
145}
Harald Welte652a7232010-07-22 21:55:24 +0200146
Harald Weltebd598e32011-08-16 23:26:52 +0200147/*! \brief determine how much tail room is left in msgb
148 * \param[in] msgb message buffer
149 * \returns number of bytes remaining at end of msgb
150 *
151 * This function computes the amount of octets left in the underlying
152 * data buffer after the end of the message.
153 */
Harald Welte652a7232010-07-22 21:55:24 +0200154static inline int msgb_tailroom(const struct msgb *msgb)
155{
156 return (msgb->head + msgb->data_len) - msgb->tail;
157}
158
Harald Weltebd598e32011-08-16 23:26:52 +0200159/*! \brief determine the amount of headroom in msgb
160 * \param[in] msgb message buffer
161 * \returns number of bytes left ahead of message start in msgb
162 *
163 * This function computes the amount of bytes left in the underlying
164 * data buffer before the start of the actual message.
165 */
Harald Welte652a7232010-07-22 21:55:24 +0200166static inline int msgb_headroom(const struct msgb *msgb)
167{
168 return (msgb->data - msgb->head);
169}
170
Harald Weltebd598e32011-08-16 23:26:52 +0200171/*! \brief append data to end of message buffer
172 * \param[in] msgb message buffer
173 * \param[in] len number of bytes to append to message
174 * \returns pointer to start of newly-appended data
175 *
176 * This function will move the \a tail pointer of the message buffer \a
177 * len bytes further, thus enlarging the message by \a len bytes.
178 *
179 * The return value is a pointer to start of the newly added section at
180 * the end of the message and can be used for actually filling/copying
181 * data into it.
182 */
Harald Welteec8b4502010-02-20 20:34:29 +0100183static inline unsigned char *msgb_put(struct msgb *msgb, unsigned int len)
184{
185 unsigned char *tmp = msgb->tail;
Harald Welte46a45a62010-11-09 13:41:48 +0100186 if (msgb_tailroom(msgb) < (int) len)
Harald Welte929d8872010-11-05 07:47:41 +0100187 MSGB_ABORT(msgb, "Not enough tailroom msgb_push (%u < %u)\n",
188 msgb_tailroom(msgb), len);
Harald Welteec8b4502010-02-20 20:34:29 +0100189 msgb->tail += len;
190 msgb->len += len;
191 return tmp;
192}
Harald Weltebd598e32011-08-16 23:26:52 +0200193
194/*! \brief append a uint8 value to the end of the message
195 * \param[in] msgb message buffer
196 * \param[in] word unsigned 8bit byte to be appended
197 */
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100198static inline void msgb_put_u8(struct msgb *msgb, uint8_t word)
199{
200 uint8_t *space = msgb_put(msgb, 1);
201 space[0] = word & 0xFF;
202}
Harald Weltebd598e32011-08-16 23:26:52 +0200203
204/*! \brief append a uint16 value to the end of the message
205 * \param[in] msgb message buffer
206 * \param[in] word unsigned 16bit byte to be appended
207 */
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100208static inline void msgb_put_u16(struct msgb *msgb, uint16_t word)
209{
210 uint8_t *space = msgb_put(msgb, 2);
Max53777012014-06-04 19:07:41 +0200211 osmo_store16be(word, space);
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100212}
Harald Weltebd598e32011-08-16 23:26:52 +0200213
214/*! \brief append a uint32 value to the end of the message
215 * \param[in] msgb message buffer
216 * \param[in] word unsigned 32bit byte to be appended
217 */
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100218static inline void msgb_put_u32(struct msgb *msgb, uint32_t word)
219{
220 uint8_t *space = msgb_put(msgb, 4);
Max53777012014-06-04 19:07:41 +0200221 osmo_store32be(word, space);
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100222}
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 Weltecac3cd62012-09-10 20:58:20 +0200230 unsigned char *tmp = msgb->tail - len;
Harald Welte972b5022012-09-08 19:58:59 +0200231 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}
Max53777012014-06-04 19:07:41 +0200238
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}
Max53777012014-06-04 19:07:41 +0200248
Harald Weltebd598e32011-08-16 23:26:52 +0200249/*! \brief remove uint16 from end of message
250 * \param[in] msgb message buffer
251 * \returns 16bit value taken from end of msgb
252 */
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100253static inline uint16_t msgb_get_u16(struct msgb *msgb)
254{
255 uint8_t *space = msgb_get(msgb, 2);
Max53777012014-06-04 19:07:41 +0200256 return osmo_load16be(space);
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100257}
Max53777012014-06-04 19:07:41 +0200258
Harald Weltebd598e32011-08-16 23:26:52 +0200259/*! \brief remove uint32 from end of message
260 * \param[in] msgb message buffer
261 * \returns 32bit value taken from end of msgb
262 */
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100263static inline uint32_t msgb_get_u32(struct msgb *msgb)
264{
265 uint8_t *space = msgb_get(msgb, 4);
Max53777012014-06-04 19:07:41 +0200266 return osmo_load32be(space);
Ingo Albrecht48e17f82010-03-07 18:03:41 +0100267}
Harald Weltebd598e32011-08-16 23:26:52 +0200268
269/*! \brief prepend (push) some data to start of message
270 * \param[in] msgb message buffer
271 * \param[in] len number of bytes to pre-pend
272 * \returns pointer to newly added portion at start of \a msgb
273 *
274 * This function moves the \a data pointer of the \ref msgb further
275 * to the front (by \a len bytes), thereby enlarging the message by \a
276 * len bytes.
277 *
278 * The return value is a pointer to the newly added section in the
279 * beginning of the message. It can be used to fill/copy data into it.
280 */
Harald Welteec8b4502010-02-20 20:34:29 +0100281static inline unsigned char *msgb_push(struct msgb *msgb, unsigned int len)
282{
Harald Welte46a45a62010-11-09 13:41:48 +0100283 if (msgb_headroom(msgb) < (int) len)
Harald Welte929d8872010-11-05 07:47:41 +0100284 MSGB_ABORT(msgb, "Not enough headroom msgb_push (%u < %u)\n",
285 msgb_headroom(msgb), len);
Harald Welteec8b4502010-02-20 20:34:29 +0100286 msgb->data -= len;
287 msgb->len += len;
288 return msgb->data;
289}
Max53777012014-06-04 19:07:41 +0200290
Harald Weltebd598e32011-08-16 23:26:52 +0200291/*! \brief remove (pull) a header from the front of the message buffer
292 * \param[in] msgb message buffer
293 * \param[in] len number of octets to be pulled
294 * \returns pointer to new start of msgb
295 *
296 * This function moves the \a data pointer of the \ref msgb further back
297 * in the message, thereby shrinking the size of the message by \a len
298 * bytes.
299 */
Harald Welteec8b4502010-02-20 20:34:29 +0100300static inline unsigned char *msgb_pull(struct msgb *msgb, unsigned int len)
301{
302 msgb->len -= len;
303 return msgb->data += len;
304}
Harald Welteec8b4502010-02-20 20:34:29 +0100305
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100306/*! \brief remove (pull) all headers in front of l3h from the message buffer.
307 * \param[in] msgb message buffer with a valid l3h
308 * \returns pointer to new start of msgb (l3h)
309 *
310 * This function moves the \a data pointer of the \ref msgb further back
311 * in the message, thereby shrinking the size of the message.
312 * l1h and l2h will be cleared.
313 */
314static inline unsigned char *msgb_pull_to_l3(struct msgb *msg)
315{
316 unsigned char *ret = msgb_pull(msg, msg->l3h - msg->data);
317 msg->l1h = msg->l2h = NULL;
318 return ret;
319}
320
Harald Welte972b5022012-09-08 19:58:59 +0200321/*! \brief remove uint8 from front of message
322 * \param[in] msgb message buffer
323 * \returns 8bit value taken from end of msgb
324 */
325static inline uint8_t msgb_pull_u8(struct msgb *msgb)
326{
Steve Markgraf5905d5b2012-11-14 19:36:00 +0100327 uint8_t *space = msgb_pull(msgb, 1) - 1;
Harald Welte972b5022012-09-08 19:58:59 +0200328 return space[0];
329}
Max53777012014-06-04 19:07:41 +0200330
Harald Welte972b5022012-09-08 19:58:59 +0200331/*! \brief remove uint16 from front of message
332 * \param[in] msgb message buffer
333 * \returns 16bit value taken from end of msgb
334 */
335static inline uint16_t msgb_pull_u16(struct msgb *msgb)
336{
Steve Markgraf5905d5b2012-11-14 19:36:00 +0100337 uint8_t *space = msgb_pull(msgb, 2) - 2;
Max53777012014-06-04 19:07:41 +0200338 return osmo_load16be(space);
Harald Welte972b5022012-09-08 19:58:59 +0200339}
Max53777012014-06-04 19:07:41 +0200340
Harald Welte972b5022012-09-08 19:58:59 +0200341/*! \brief remove uint32 from front of message
342 * \param[in] msgb message buffer
343 * \returns 32bit value taken from end of msgb
344 */
345static inline uint32_t msgb_pull_u32(struct msgb *msgb)
346{
Steve Markgraf5905d5b2012-11-14 19:36:00 +0100347 uint8_t *space = msgb_pull(msgb, 4) - 4;
Max53777012014-06-04 19:07:41 +0200348 return osmo_load32be(space);
Harald Welte972b5022012-09-08 19:58:59 +0200349}
350
Harald Weltebd598e32011-08-16 23:26:52 +0200351/*! \brief Increase headroom of empty msgb, reducing the tailroom
352 * \param[in] msg message buffer
353 * \param[in] len amount of extra octets to be reserved as headroom
354 *
355 * This function reserves some memory at the beginning of the underlying
356 * data buffer. The idea is to reserve space in case further headers
357 * have to be pushed to the \ref msgb during further processing.
358 *
359 * Calling this function leads to undefined reusults if it is called on
360 * a non-empty \ref msgb.
361 */
Harald Welteec8b4502010-02-20 20:34:29 +0100362static inline void msgb_reserve(struct msgb *msg, int len)
363{
364 msg->data += len;
365 msg->tail += len;
366}
367
Harald Welteb0f91512012-01-14 21:53:41 +0100368/*! \brief Trim the msgb to a given absolute length
369 * \param[in] msg message buffer
370 * \param[in] len new total length of buffer
371 * \returns 0 in case of success, negative in case of error
372 */
373static inline int msgb_trim(struct msgb *msg, int len)
374{
Harald Welte3068d572012-01-14 22:07:59 +0100375 if (len > msg->data_len)
Harald Welteb0f91512012-01-14 21:53:41 +0100376 return -1;
377
Harald Welte3068d572012-01-14 22:07:59 +0100378 msg->len = len;
379 msg->tail = msg->data + len;
Harald Welteb0f91512012-01-14 21:53:41 +0100380
381 return 0;
382}
383
384/*! \brief Trim the msgb to a given layer3 length
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +0100385 * \param[in] msg message buffer
Harald Welteb0f91512012-01-14 21:53:41 +0100386 * \param[in] l3len new layer3 length
387 * \returns 0 in case of success, negative in case of error
388 */
389static inline int msgb_l3trim(struct msgb *msg, int l3len)
390{
391 return msgb_trim(msg, (msg->l3h - msg->data) + l3len);
392}
393
Harald Weltebd598e32011-08-16 23:26:52 +0200394/*! \brief Allocate message buffer with specified headroom
395 * \param[in] size size in bytes, including headroom
396 * \param[in] headroom headroom in bytes
397 * \param[in] name human-readable name
398 * \returns allocated message buffer with specified headroom
399 *
400 * This function is a convenience wrapper around \ref msgb_alloc
401 * followed by \ref msgb_reserve in order to create a new \ref msgb with
402 * user-specified amount of headroom.
403 */
Harald Welteec8b4502010-02-20 20:34:29 +0100404static inline struct msgb *msgb_alloc_headroom(int size, int headroom,
405 const char *name)
406{
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +0200407 osmo_static_assert(size > headroom, headroom_bigger);
Holger Hans Peter Freyther5853f082011-01-16 17:38:22 +0100408
Harald Welteec8b4502010-02-20 20:34:29 +0100409 struct msgb *msg = msgb_alloc(size, name);
410 if (msg)
411 msgb_reserve(msg, headroom);
412 return msg;
413}
414
Holger Hans Peter Freytheracffb602010-10-18 18:22:31 +0200415/* non inline functions to ease binding */
Harald Weltebd598e32011-08-16 23:26:52 +0200416
Holger Hans Peter Freytheracffb602010-10-18 18:22:31 +0200417uint8_t *msgb_data(const struct msgb *msg);
Harald Welte9e1f0602011-06-29 18:46:10 +0200418void msgb_set_talloc_ctx(void *ctx);
Holger Hans Peter Freytheracffb602010-10-18 18:22:31 +0200419
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200420/*! @} */