libosmocore  0.10.2
Osmocom core library
msgb.h
Go to the documentation of this file.
1 #pragma once
2 
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>
23 #include <osmocom/core/linuxlist.h>
24 #include <osmocom/core/utils.h>
25 #include <osmocom/core/bits.h>
26 #include <osmocom/core/defs.h>
27 
32 #define MSGB_DEBUG
33 
35 struct msgb {
36  struct llist_head list;
39  /* Part of which TRX logical channel we were received / transmitted */
40  /* FIXME: move them into the control buffer */
41  union {
42  void *dst;
43  struct gsm_bts_trx *trx;
44  };
45  struct gsm_lchan *lchan;
47  unsigned char *l1h;
48  unsigned char *l2h;
49  unsigned char *l3h;
50  unsigned char *l4h;
52  unsigned long cb[5];
54  uint16_t data_len;
55  uint16_t len;
57  unsigned char *head;
58  unsigned char *tail;
59  unsigned char *data;
60  unsigned char _data[0];
61 };
62 
63 extern struct msgb *msgb_alloc(uint16_t size, const char *name);
64 extern void msgb_free(struct msgb *m);
65 extern void msgb_enqueue(struct llist_head *queue, struct msgb *msg);
66 extern struct msgb *msgb_dequeue(struct llist_head *queue);
67 extern void msgb_reset(struct msgb *m);
68 uint16_t msgb_length(const struct msgb *msg);
69 extern const char *msgb_hexdump(const struct msgb *msg);
70 extern int msgb_resize_area(struct msgb *msg, uint8_t *area,
71  int old_size, int new_size);
72 extern struct msgb *msgb_copy(const struct msgb *msg, const char *name);
73 static int msgb_test_invariant(const struct msgb *msg) __attribute__((pure));
74 
75 #ifdef MSGB_DEBUG
76 #include <osmocom/core/panic.h>
77 #define MSGB_ABORT(msg, fmt, args ...) do { \
78  osmo_panic("msgb(%p): " fmt, msg, ## args); \
79  } while(0)
80 #else
81 #define MSGB_ABORT(msg, fmt, args ...)
82 #endif
83 
85 #define msgb_l1(m) ((void *)(m->l1h))
86 
87 #define msgb_l2(m) ((void *)(m->l2h))
88 
89 #define msgb_l3(m) ((void *)(m->l3h))
90 
91 #define msgb_sms(m) ((void *)(m->l4h))
92 
100 static inline unsigned int msgb_l1len(const struct msgb *msgb)
101 {
102  return msgb->tail - (uint8_t *)msgb_l1(msgb);
103 }
104 
112 static inline unsigned int msgb_l2len(const struct msgb *msgb)
113 {
114  return msgb->tail - (uint8_t *)msgb_l2(msgb);
115 }
116 
124 static inline unsigned int msgb_l3len(const struct msgb *msgb)
125 {
126  return msgb->tail - (uint8_t *)msgb_l3(msgb);
127 }
128 
136 static inline unsigned int msgb_headlen(const struct msgb *msgb)
137 {
138  return msgb->len - msgb->data_len;
139 }
140 
148 static inline int msgb_tailroom(const struct msgb *msgb)
149 {
150  return (msgb->head + msgb->data_len) - msgb->tail;
151 }
152 
160 static inline int msgb_headroom(const struct msgb *msgb)
161 {
162  return (msgb->data - msgb->head);
163 }
164 
177 static inline unsigned char *msgb_put(struct msgb *msgb, unsigned int len)
178 {
179  unsigned char *tmp = msgb->tail;
180  if (msgb_tailroom(msgb) < (int) len)
181  MSGB_ABORT(msgb, "Not enough tailroom msgb_put (%u < %u)\n",
182  msgb_tailroom(msgb), len);
183  msgb->tail += len;
184  msgb->len += len;
185  return tmp;
186 }
187 
192 static inline void msgb_put_u8(struct msgb *msgb, uint8_t word)
193 {
194  uint8_t *space = msgb_put(msgb, 1);
195  space[0] = word & 0xFF;
196 }
197 
202 static inline void msgb_put_u16(struct msgb *msgb, uint16_t word)
203 {
204  uint8_t *space = msgb_put(msgb, 2);
205  osmo_store16be(word, space);
206 }
207 
212 static inline void msgb_put_u32(struct msgb *msgb, uint32_t word)
213 {
214  uint8_t *space = msgb_put(msgb, 4);
215  osmo_store32be(word, space);
216 }
217 
222 static inline unsigned char *msgb_get(struct msgb *msgb, unsigned int len)
223 {
224  unsigned char *tmp = msgb->tail - len;
225  if (msgb_length(msgb) < len)
226  MSGB_ABORT(msgb, "msgb too small to get %u (len %u)\n",
227  len, msgb_length(msgb));
228  msgb->tail -= len;
229  msgb->len -= len;
230  return tmp;
231 }
232 
237 static inline uint8_t msgb_get_u8(struct msgb *msgb)
238 {
239  uint8_t *space = msgb_get(msgb, 1);
240  return space[0];
241 }
242 
247 static inline uint16_t msgb_get_u16(struct msgb *msgb)
248 {
249  uint8_t *space = msgb_get(msgb, 2);
250  return osmo_load16be(space);
251 }
252 
257 static inline uint32_t msgb_get_u32(struct msgb *msgb)
258 {
259  uint8_t *space = msgb_get(msgb, 4);
260  return osmo_load32be(space);
261 }
262 
275 static inline unsigned char *msgb_push(struct msgb *msgb, unsigned int len)
276 {
277  if (msgb_headroom(msgb) < (int) len)
278  MSGB_ABORT(msgb, "Not enough headroom msgb_push (%u < %u)\n",
279  msgb_headroom(msgb), len);
280  msgb->data -= len;
281  msgb->len += len;
282  return msgb->data;
283 }
284 
289 static inline void msgb_push_u8(struct msgb *msg, uint8_t word)
290 {
291  uint8_t *space = msgb_push(msg, 1);
292  space[0] = word;
293 }
294 
299 static inline void msgb_push_u16(struct msgb *msg, uint16_t word)
300 {
301  uint16_t *space = (uint16_t *) msgb_push(msg, 2);
302  osmo_store16be(word, space);
303 }
304 
309 static inline void msgb_push_u32(struct msgb *msg, uint32_t word)
310 {
311  uint32_t *space = (uint32_t *) msgb_push(msg, 4);
312  osmo_store32be(word, space);
313 }
314 
324 static inline unsigned char *msgb_pull(struct msgb *msgb, unsigned int len)
325 {
326  msgb->len -= len;
327  return msgb->data += len;
328 }
329 
338 static inline unsigned char *msgb_pull_to_l3(struct msgb *msg)
339 {
340  unsigned char *ret = msgb_pull(msg, msg->l3h - msg->data);
341  msg->l1h = msg->l2h = NULL;
342  return ret;
343 }
344 
353 static inline unsigned char *msgb_pull_to_l2(struct msgb *msg)
354 {
355  unsigned char *ret = msgb_pull(msg, msg->l2h - msg->data);
356  msg->l1h = NULL;
357  return ret;
358 }
359 
364 static inline uint8_t msgb_pull_u8(struct msgb *msgb)
365 {
366  uint8_t *space = msgb_pull(msgb, 1) - 1;
367  return space[0];
368 }
369 
374 static inline uint16_t msgb_pull_u16(struct msgb *msgb)
375 {
376  uint8_t *space = msgb_pull(msgb, 2) - 2;
377  return osmo_load16be(space);
378 }
379 
384 static inline uint32_t msgb_pull_u32(struct msgb *msgb)
385 {
386  uint8_t *space = msgb_pull(msgb, 4) - 4;
387  return osmo_load32be(space);
388 }
389 
401 static inline void msgb_reserve(struct msgb *msg, int len)
402 {
403  msg->data += len;
404  msg->tail += len;
405 }
406 
412 static inline int msgb_trim(struct msgb *msg, int len)
413 {
414  if (len < 0)
415  MSGB_ABORT(msg, "Negative length is not allowed\n");
416  if (len > msg->data_len)
417  return -1;
418 
419  msg->len = len;
420  msg->tail = msg->data + len;
421 
422  return 0;
423 }
424 
430 static inline int msgb_l3trim(struct msgb *msg, int l3len)
431 {
432  return msgb_trim(msg, (msg->l3h - msg->data) + l3len);
433 }
434 
445 static inline struct msgb *msgb_alloc_headroom(int size, int headroom,
446  const char *name)
447 {
448  osmo_static_assert(size > headroom, headroom_bigger);
449 
450  struct msgb *msg = msgb_alloc(size, name);
451  if (msg)
452  msgb_reserve(msg, headroom);
453  return msg;
454 }
455 
460 static inline int msgb_test_invariant(const struct msgb *msg)
461 {
462  const unsigned char *lbound;
463  if (!msg || !msg->data || !msg->tail ||
464  (msg->data + msg->len != msg->tail) ||
465  (msg->data < msg->head) ||
466  (msg->tail > msg->head + msg->data_len))
467  return 0;
468 
469  lbound = msg->head;
470 
471  if (msg->l1h) {
472  if (msg->l1h < lbound)
473  return 0;
474  lbound = msg->l1h;
475  }
476  if (msg->l2h) {
477  if (msg->l2h < lbound)
478  return 0;
479  lbound = msg->l2h;
480  }
481  if (msg->l3h) {
482  if (msg->l3h < lbound)
483  return 0;
484  lbound = msg->l3h;
485  }
486  if (msg->l4h) {
487  if (msg->l4h < lbound)
488  return 0;
489  lbound = msg->l4h;
490  }
491 
492  return lbound <= msg->head + msg->data_len;
493 }
494 
495 /* non inline functions to ease binding */
496 
497 uint8_t *msgb_data(const struct msgb *msg);
498 
499 void *msgb_talloc_ctx_init(void *root_ctx, unsigned int pool_size);
500 void msgb_set_talloc_ctx(void *ctx) OSMO_DEPRECATED("Use msgb_talloc_ctx_init() instead");
501 int msgb_printf(struct msgb *msgb, const char *format, ...);
502 
static unsigned char * msgb_put(struct msgb *msgb, unsigned int len)
append data to end of message buffer
Definition: msgb.h:177
static uint16_t msgb_pull_u16(struct msgb *msgb)
remove uint16 from front of message
Definition: msgb.h:374
static unsigned int msgb_l3len(const struct msgb *msgb)
determine length of L3 message
Definition: msgb.h:124
unsigned long cb[5]
control buffer
Definition: msgb.h:52
#define OSMO_DEPRECATED(text)
Set the deprecated attribute with a message.
Definition: defs.h:41
unsigned char * l3h
pointer to Layer 3 header.
Definition: msgb.h:49
#define msgb_l3(m)
obtain L3 header of msgb
Definition: msgb.h:89
#define msgb_l1(m)
obtain L1 header of msgb
Definition: msgb.h:85
struct msgb * msgb_alloc(uint16_t size, const char *name)
Allocate a new message buffer.
Definition: msgb.c:78
unsigned char * data
start of message in buffer
Definition: msgb.h:59
struct rb_root __attribute__
Definition: conv_acc_generic.c:138
Osmocom message buffer.
Definition: msgb.h:35
void msgb_set_talloc_ctx(void *ctx) OSMO_DEPRECATED("Use msgb_talloc_ctx_init() instead")
Set the talloc context for msgb_alloc Deprecated, use msgb_talloc_ctx_init() instead.
Definition: msgb.c:186
static int msgb_l3trim(struct msgb *msg, int l3len)
Trim the msgb to a given layer3 length.
Definition: msgb.h:430
General definitions that are meant to be included from header files.
static int msgb_trim(struct msgb *msg, int len)
Trim the msgb to a given absolute length.
Definition: msgb.h:412
uint16_t len
length of bytes used in msgb
Definition: msgb.h:55
char name[32]
source file name
Definition: gsmtap.h:122
static unsigned char * msgb_pull_to_l3(struct msgb *msg)
remove (pull) all headers in front of l3h from the message buffer.
Definition: msgb.h:338
#define osmo_static_assert(exp, name)
Definition: utils.h:52
void msgb_free(struct msgb *m)
Release given message buffer.
Definition: msgb.c:101
static int msgb_tailroom(const struct msgb *msgb)
determine how much tail room is left in msgb
Definition: msgb.h:148
unsigned char * l4h
pointer to layer 4 header
Definition: msgb.h:50
static uint16_t osmo_load16be(const void *p)
load unaligned 16-bit integer (big-endian encoding)
Definition: bit16gen.h:89
static unsigned int msgb_l1len(const struct msgb *msgb)
determine length of L1 message
Definition: msgb.h:100
static unsigned char * msgb_get(struct msgb *msgb, unsigned int len)
remove data from end of message
Definition: msgb.h:222
const char * msgb_hexdump(const struct msgb *msg)
Return a (static) buffer containing a hexdump of the msg.
Definition: msgb.c:304
static void msgb_push_u8(struct msgb *msg, uint8_t word)
prepend a uint8 value to the head of the message
Definition: msgb.h:289
struct msgb * msgb_dequeue(struct llist_head *queue)
Dequeue message buffer from head of queue.
Definition: msgb.c:125
void * msgb_talloc_ctx_init(void *root_ctx, unsigned int pool_size)
Initialize a msgb talloc context for msgb_alloc.
Definition: msgb.c:199
int msgb_resize_area(struct msgb *msg, uint8_t *area, int old_size, int new_size)
Resize an area within an msgb.
Definition: msgb.c:259
static uint32_t msgb_pull_u32(struct msgb *msgb)
remove uint32 from front of message
Definition: msgb.h:384
static unsigned int msgb_l2len(const struct msgb *msgb)
determine length of L2 message
Definition: msgb.h:112
static struct msgb * msgb_alloc_headroom(int size, int headroom, const char *name)
Allocate message buffer with specified headroom.
Definition: msgb.h:445
static unsigned char * msgb_push(struct msgb *msgb, unsigned int len)
prepend (push) some data to start of message
Definition: msgb.h:275
Simple doubly linked list implementation.
struct gsm_bts_trx * trx
Definition: msgb.h:43
(double) linked list header structure
Definition: linuxlist.h:46
unsigned char * l1h
pointer to Layer1 header (if any)
Definition: msgb.h:47
static uint8_t msgb_pull_u8(struct msgb *msgb)
remove uint8 from front of message
Definition: msgb.h:364
void msgb_enqueue(struct llist_head *queue, struct msgb *msg)
Enqueue message buffer to tail of a queue.
Definition: msgb.c:113
struct gsm_lchan * lchan
logical channel
Definition: msgb.h:45
void msgb_reset(struct msgb *m)
Re-set all message buffer pointers.
Definition: msgb.c:148
static void msgb_push_u32(struct msgb *msg, uint32_t word)
prepend a uint32 value to the head of the message
Definition: msgb.h:309
static void msgb_reserve(struct msgb *msg, int len)
Increase headroom of empty msgb, reducing the tailroom.
Definition: msgb.h:401
unsigned char * tail
end of message in buffer
Definition: msgb.h:58
int msgb_printf(struct msgb *msgb, const char *format,...)
Print a string to the end of message buffer.
Definition: msgb.c:399
static void msgb_put_u16(struct msgb *msgb, uint16_t word)
append a uint16 value to the end of the message
Definition: msgb.h:202
static uint8_t msgb_get_u8(struct msgb *msgb)
remove uint8 from end of message
Definition: msgb.h:237
unsigned char * l2h
pointer to A-bis layer 2 header: OML, RSL(RLL), NS
Definition: msgb.h:48
struct msgb * msgb_copy(const struct msgb *msg, const char *name)
Copy an msgb.
Definition: msgb.c:217
unsigned char * head
start of underlying memory buffer
Definition: msgb.h:57
static uint32_t msgb_get_u32(struct msgb *msgb)
remove uint32 from end of message
Definition: msgb.h:257
static int msgb_headroom(const struct msgb *msgb)
determine the amount of headroom in msgb
Definition: msgb.h:160
Osmocom bit level support code.
static uint32_t osmo_load32be(const void *p)
load unaligned 32-bit integer (big-endian encoding)
Definition: bit32gen.h:89
uint16_t data_len
length of underlying data array
Definition: msgb.h:54
static void osmo_store16be(uint16_t x, void *p)
store unaligned 16-bit integer (big-endian encoding)
Definition: bit16gen.h:102
unsigned char _data[0]
optional immediate data array
Definition: msgb.h:60
struct llist_head list
linked list header
Definition: msgb.h:36
uint8_t * msgb_data(const struct msgb *msg)
get pointer to data section of message buffer
Definition: msgb.c:168
#define msgb_l2(m)
obtain L2 header of msgb
Definition: msgb.h:87
static uint16_t msgb_get_u16(struct msgb *msgb)
remove uint16 from end of message
Definition: msgb.h:247
void * dst
reference of origin/destination
Definition: msgb.h:42
static unsigned int msgb_headlen(const struct msgb *msgb)
determine the length of the header
Definition: msgb.h:136
uint16_t msgb_length(const struct msgb *msg)
get length of message buffer
Definition: msgb.c:177
static int msgb_test_invariant(const struct msgb *msg) __attribute__((pure))
Check a message buffer for consistency.
Definition: msgb.h:460
static void msgb_put_u32(struct msgb *msgb, uint32_t word)
append a uint32 value to the end of the message
Definition: msgb.h:212
#define MSGB_ABORT(msg, fmt, args ...)
Definition: msgb.h:77
static unsigned char * msgb_pull(struct msgb *msgb, unsigned int len)
remove (pull) a header from the front of the message buffer
Definition: msgb.h:324
static void osmo_store32be(uint32_t x, void *p)
store unaligned 32-bit integer (big-endian encoding)
Definition: bit32gen.h:102
static unsigned char * msgb_pull_to_l2(struct msgb *msg)
remove (pull) all headers in front of l2h from the message buffer.
Definition: msgb.h:353
static void msgb_push_u16(struct msgb *msg, uint16_t word)
prepend a uint16 value to the head of the message
Definition: msgb.h:299
static void msgb_put_u8(struct msgb *msgb, uint8_t word)
append a uint8 value to the end of the message
Definition: msgb.h:192