Message buffers

## Files

file | msgb.h
| Osmocom message buffers The Osmocom message buffers are modelled after the 'struct skb' inside the Linux kernel network stack. As they exist in userspace, they are much simplified. However, terminology such as headroom, tailroom, push/pull/put etc. remains the same.
file | msgb.c

## Data Structures

struct | msgb
| Osmocom message buffer. More...

## Macros

#define | MSGB_DEBUG
#define | MSGB_ABORT(msg, fmt, args ...)
#define | msgb_l1(m) ((void *)(m->l1h))
| obtain L1 header of msgb
#define | msgb_l2(m) ((void *)(m->l2h))
| obtain L2 header of msgb
#define | msgb_l3(m) ((void *)(m->l3h))
| obtain L3 header of msgb
#define | msgb_sms(m) ((void *)(m->l4h))
| obtain SMS header of msgb

## Functions

struct msgb * | msgb_alloc (uint16_t size, const char *name)
| Allocate a new message buffer. More...
void | msgb_free (struct msgb *m)
| Release given message buffer. More...
void | msgb_enqueue (struct llist_head *queue, struct msgb *msg)
| Enqueue message buffer to tail of a queue. More...
struct msgb * | msgb_dequeue (struct llist_head *queue)
| Dequeue message buffer from head of queue. More...
void | msgb_reset (struct msgb *msg)
| Re-set all message buffer pointers. More...
uint16_t | msgb_length (const struct msgb *msg)
| get length of message buffer More...
const char * | msgb_hexdump (const struct msgb *msg)
| Return a (static) buffer containing a hexdump of the msg. More...
static unsigned int | msgb_l1len (const struct msgb *msgb)
| determine length of L1 message More...
static unsigned int | msgb_l2len (const struct msgb *msgb)
| determine length of L2 message More...
static unsigned int | msgb_l3len (const struct msgb *msgb)
| determine length of L3 message More...
static unsigned int | msgb_headlen (const struct msgb *msgb)
| determine the length of the header More...
static int | msgb_tailroom (const struct msgb *msgb)
| determine how much tail room is left in msgb More...
static int | msgb_headroom (const struct msgb *msgb)
| determine the amount of headroom in msgb More...
static unsigned char * | msgb_put (struct msgb *msgb, unsigned int len)
| append data to end of message buffer More...
static void | msgb_put_u8 (struct msgb *msgb, uint8_t word)
| append a uint8 value to the end of the message More...
static void | msgb_put_u16 (struct msgb *msgb, uint16_t word)
| append a uint16 value to the end of the message More...
static void | msgb_put_u32 (struct msgb *msgb, uint32_t word)
| append a uint32 value to the end of the message More...
static unsigned char * | msgb_get (struct msgb *msgb, unsigned int len)
| remove data from end of message More...
static uint8_t | msgb_get_u8 (struct msgb *msgb)
| remove uint8 from end of message More...
static uint16_t | msgb_get_u16 (struct msgb *msgb)
| remove uint16 from end of message More...
static uint32_t | msgb_get_u32 (struct msgb *msgb)
| remove uint32 from end of message More...
static unsigned char * | msgb_push (struct msgb *msgb, unsigned int len)
| prepend (push) some data to start of message More...
static unsigned char * | msgb_pull (struct msgb *msgb, unsigned int len)
| remove (pull) a header from the front of the message buffer More...
static unsigned char * | msgb_pull_to_l3 (struct msgb *msg)
| remove (pull) all headers in front of l3h from the message buffer. More...
static uint8_t | msgb_pull_u8 (struct msgb *msgb)
| remove uint8 from front of message More...
static uint16_t | msgb_pull_u16 (struct msgb *msgb)
| remove uint16 from front of message More...
static uint32_t | msgb_pull_u32 (struct msgb *msgb)
| remove uint32 from front of message More...
static void | msgb_reserve (struct msgb *msg, int len)
| Increase headroom of empty msgb, reducing the tailroom. More...
static int | msgb_trim (struct msgb *msg, int len)
| Trim the msgb to a given absolute length. More...
static int | msgb_l3trim (struct msgb *msg, int l3len)
| Trim the msgb to a given layer3 length. More...
static struct msgb * | msgb_alloc_headroom (int size, int headroom, const char *name)
| Allocate message buffer with specified headroom. More...
uint8_t * | msgb_data (const struct msgb *msg)
| get pointer to data section of message buffer More...
void | msgb_set_talloc_ctx (void *ctx)
| Set the talloc context for msgb_alloc. More...

## Variables

void * | tall_msgb_ctx

## Detailed Description

## Macro Definition Documentation

## MSGB_ABORT

#define MSGB_ABORT | ( |  | msg,
|  |  | fmt,
|  |  | args ...
| ) |  |

Value:
do { \
osmo_panic("msgb(%p): " fmt, msg, ## args); \
} while(0)

## Function Documentation

## msgb_alloc()

struct msgb * msgb_alloc | ( | uint16_t | size,
|  | const char * | name
| ) |  |

Allocate a new message buffer.

Parameters

  [in] | size | Length in octets, including headroom
  [in] | name | Human-readable name to be associated with msgb

This function allocates a 'struct msgb' as well as the underlying memory buffer for the actual message data (size specified by size) using the talloc memory context previously set by msgb_set_talloc_ctx

Referenced by gsmtap_makemsg_ex().

## msgb_alloc_headroom()

static struct msgb* msgb_alloc_headroom | ( | int | size,
|  | int | headroom,
|  | const char * | name
| ) |  |

static

Allocate message buffer with specified headroom.

Parameters

  [in] | size | size in bytes, including headroom
  [in] | headroom | headroom in bytes
  [in] | name | human-readable name

Returns
  allocated message buffer with specified headroom

This function is a convenience wrapper around msgb_alloc followed by msgb_reserve in order to create a new Message buffers with user-specified amount of headroom.

## msgb_data()

uint8_t * msgb_data | ( | const struct msgb * | msg | ) |

get pointer to data section of message buffer

Parameters

  [in] | msg | message buffer

Returns
  pointer to data section of message buffer

References data.

## msgb_dequeue()

struct msgb * msgb_dequeue | ( | struct llist_head * | queue | ) |

Dequeue message buffer from head of queue.

Parameters

  [in] | queue | linked list header of queue

Returns
  message buffer (if any) or NULL if queue empty

The function will remove the first message buffer from the queue implemented by llist_head queue.

Referenced by osmo_wqueue_bfd_cb(), and osmo_wqueue_clear().

## msgb_enqueue()

void msgb_enqueue | ( | struct llist_head * | queue,
|  | struct msgb * | msg
| ) |  |

Enqueue message buffer to tail of a queue.

Parameters

  [in] | queue | linked list header of queue
  [in] | msg | message buffer to be added to the queue

The function will append the specified message buffer msg to the queue implemented by llist_head queue

References list.

Referenced by osmo_wqueue_enqueue().

## msgb_free()

void msgb_free | ( | struct msgb * | m | ) |

Release given message buffer.

Parameters

  [in] | m | Message buffer to be free'd

Referenced by gsmtap_sendmsg(), osmo_wqueue_bfd_cb(), and osmo_wqueue_clear().

## msgb_get()

static unsigned char* msgb_get | ( | struct msgb * | msgb,
|  | unsigned int | len
| ) |  |

inline static

remove data from end of message

Parameters

  [in] | msgb | message buffer
  [in] | len | number of bytes to remove from end

References len, msgb_length(), and tail.

Referenced by msgb_get_u16(), msgb_get_u32(), and msgb_get_u8().

## msgb_get_u16()

static uint16_t msgb_get_u16 | ( | struct msgb * | msgb | ) |

inline static

remove uint16 from end of message

Parameters

  [in] | msgb | message buffer

Returns
  16bit value taken from end of msgb

References msgb_get().

## msgb_get_u32()

static uint32_t msgb_get_u32 | ( | struct msgb * | msgb | ) |

inline static

remove uint32 from end of message

Parameters

  [in] | msgb | message buffer

Returns
  32bit value taken from end of msgb

References msgb_get().

## msgb_get_u8()

static uint8_t msgb_get_u8 | ( | struct msgb * | msgb | ) |

inline static

remove uint8 from end of message

Parameters

  [in] | msgb | message buffer

Returns
  8bit value taken from end of msgb

References msgb_get().

## msgb_headlen()

static unsigned int msgb_headlen | ( | const struct msgb * | msgb | ) |

inline static

determine the length of the header

Parameters

  [in] | msgb | message buffer

Returns
  number of bytes between start of buffer and start of msg

This function computes the length difference between the underlying data buffer and the used section of the msgb.

References data_len, and len.

## msgb_headroom()

static int msgb_headroom | ( | const struct msgb * | msgb | ) |

inline static

determine the amount of headroom in msgb

Parameters

  [in] | msgb | message buffer

Returns
  number of bytes left ahead of message start in msgb

This function computes the amount of bytes left in the underlying data buffer before the start of the actual message.

References data, and head.

Referenced by msgb_push().

## msgb_hexdump()

const char * msgb_hexdump | ( | const struct msgb * | msg | ) |

Return a (static) buffer containing a hexdump of the msg.

Parameters

  [in] | msg | message buffer

Returns
  a pointer to a static char array

References ARRAY_SIZE, data, l1h, l2h, l3h, l4h, osmo_hexdump(), and tail.

## msgb_l1len()

static unsigned int msgb_l1len | ( | const struct msgb * | msgb | ) |

inline static

determine length of L1 message

Parameters

  [in] | msgb | message buffer

Returns
  size of L1 message in bytes

This function computes the number of bytes between the tail of the message and the layer 1 header.

References msgb_l1, and tail.

## msgb_l2len()

static unsigned int msgb_l2len | ( | const struct msgb * | msgb | ) |

inline static

determine length of L2 message

Parameters

  [in] | msgb | message buffer

Returns
  size of L2 message in bytes

This function computes the number of bytes between the tail of the message and the layer 2 header.

References msgb_l2, and tail.

## msgb_l3len()

static unsigned int msgb_l3len | ( | const struct msgb * | msgb | ) |

inline static

determine length of L3 message

Parameters

  [in] | msgb | message buffer

Returns
  size of L3 message in bytes

This function computes the number of bytes between the tail of the message and the layer 3 header.

References msgb_l3, and tail.

## msgb_l3trim()

static int msgb_l3trim | ( | struct msgb * | msg,
|  | int | l3len
| ) |  |

inline static

Trim the msgb to a given layer3 length.

Parameters

  [in] | msg | message buffer
  [in] | l3len | new layer3 length

Returns
  0 in case of success, negative in case of error

References data, l3h, and msgb_trim().

## msgb_length()

uint16_t msgb_length | ( | const struct msgb * | msg | ) |

get length of message buffer

Parameters

  [in] | msg | message buffer

Returns
  length of data section in message buffer

References len.

Referenced by msgb_get().

## msgb_pull()

static unsigned char* msgb_pull | ( | struct msgb * | msgb,
|  | unsigned int | len
| ) |  |

inline static

remove (pull) a header from the front of the message buffer

Parameters

  [in] | msgb | message buffer
  [in] | len | number of octets to be pulled

Returns
  pointer to new start of msgb

This function moves the data pointer of the Message buffers further back in the message, thereby shrinking the size of the message by len bytes.

References data, and len.

Referenced by msgb_pull_to_l3(), msgb_pull_u16(), msgb_pull_u32(), and msgb_pull_u8().

## msgb_pull_to_l3()

static unsigned char* msgb_pull_to_l3 | ( | struct msgb * | msg | ) |

inline static

remove (pull) all headers in front of l3h from the message buffer.

Parameters

  [in] | msgb | message buffer with a valid l3h

Returns
  pointer to new start of msgb (l3h)

This function moves the data pointer of the Message buffers further back in the message, thereby shrinking the size of the message. l1h and l2h will be cleared.

References data, l1h, l2h, l3h, and msgb_pull().

## msgb_pull_u16()

static uint16_t msgb_pull_u16 | ( | struct msgb * | msgb | ) |

inline static

remove uint16 from front of message

Parameters

  [in] | msgb | message buffer

Returns
  16bit value taken from end of msgb

References msgb_pull().

## msgb_pull_u32()

static uint32_t msgb_pull_u32 | ( | struct msgb * | msgb | ) |

inline static

remove uint32 from front of message

Parameters

  [in] | msgb | message buffer

Returns
  32bit value taken from end of msgb

References msgb_pull().

## msgb_pull_u8()

static uint8_t msgb_pull_u8 | ( | struct msgb * | msgb | ) |

inline static

remove uint8 from front of message

Parameters

  [in] | msgb | message buffer

Returns
  8bit value taken from end of msgb

References msgb_pull().

## msgb_push()

static unsigned char* msgb_push | ( | struct msgb * | msgb,
|  | unsigned int | len
| ) |  |

inline static

prepend (push) some data to start of message

Parameters

  [in] | msgb | message buffer
  [in] | len | number of bytes to pre-pend

Returns
  pointer to newly added portion at start of msgb

This function moves the data pointer of the Message buffers further to the front (by len bytes), thereby enlarging the message by len bytes.

The return value is a pointer to the newly added section in the beginning of the message. It can be used to fill/copy data into it.

References msgb_headroom().

## msgb_put()

static unsigned char* msgb_put | ( | struct msgb * | msgb,
|  | unsigned int | len
| ) |  |

inline static

append data to end of message buffer

Parameters

  [in] | msgb | message buffer
  [in] | len | number of bytes to append to message

Returns
  pointer to start of newly-appended data

This function will move the tail pointer of the message buffer len bytes further, thus enlarging the message by len bytes.

The return value is a pointer to start of the newly added section at the end of the message and can be used for actually filling/copying data into it.

References msgb_tailroom(), and tail.

Referenced by gsmtap_makemsg_ex(), msgb_put_u16(), msgb_put_u32(), and msgb_put_u8().

## msgb_put_u16()

static void msgb_put_u16 | ( | struct msgb * | msgb,
|  | uint16_t | word
| ) |  |

inline static

append a uint16 value to the end of the message

Parameters

  [in] | msgb | message buffer
  [in] | word | unsigned 16bit byte to be appended

References msgb_put().

## msgb_put_u32()

static void msgb_put_u32 | ( | struct msgb * | msgb,
|  | uint32_t | word
| ) |  |

inline static

append a uint32 value to the end of the message

Parameters

  [in] | msgb | message buffer
  [in] | word | unsigned 32bit byte to be appended

References msgb_put().

## msgb_put_u8()

static void msgb_put_u8 | ( | struct msgb * | msgb,
|  | uint8_t | word
| ) |  |

inline static

append a uint8 value to the end of the message

Parameters

  [in] | msgb | message buffer
  [in] | word | unsigned 8bit byte to be appended

References msgb_put().

## msgb_reserve()

static void msgb_reserve | ( | struct msgb * | msg,
|  | int | len
| ) |  |

inline static

Increase headroom of empty msgb, reducing the tailroom.

Parameters

  [in] | msg | message buffer
  [in] | len | amount of extra octets to be reserved as headroom

This function reserves some memory at the beginning of the underlying data buffer. The idea is to reserve space in case further headers have to be pushed to the Message buffers during further processing.

Calling this function leads to undefined reusults if it is called on a non-empty Message buffers.

References data, len, and tail.

## msgb_reset()

void msgb_reset | ( | struct msgb * | msg | ) |

Re-set all message buffer pointers.

Parameters

  [in] | msg | message buffer that is to be resetted

This will re-set the various internal pointers into the underlying message buffer, i.e. remvoe all headroom and treat the msgb as completely empty. It also initializes the control buffer to zero.

References _data, cb, data, head, l2h, l3h, l4h, lchan, len, and tail.

## msgb_set_talloc_ctx()

void msgb_set_talloc_ctx | ( | void * | ctx | ) |

Set the talloc context for msgb_alloc.

Parameters

  [in] | ctx | talloc context to be used as root for msgb allocations

## msgb_tailroom()

static int msgb_tailroom | ( | const struct msgb * | msgb | ) |

inline static

determine how much tail room is left in msgb

Parameters

  [in] | msgb | message buffer

Returns
  number of bytes remaining at end of msgb

This function computes the amount of octets left in the underlying data buffer after the end of the message.

References data_len, head, and tail.

Referenced by msgb_put().

## msgb_trim()

static int msgb_trim | ( | struct msgb * | msg,
|  | int | len
| ) |  |

inline static

Trim the msgb to a given absolute length.

Parameters

  [in] | msg | message buffer
  [in] | len | new total length of buffer

Returns
  0 in case of success, negative in case of error

References data, data_len, len, and tail.

Referenced by msgb_l3trim().