msgb.h
Go to the documentation of this file.
#pragma once
/* (C) 2008 by Harald Welte <laforge@gnumonks.org>
* All Rights Reserved
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#include <stdint.h>
#include <osmocom/core/linuxlist.h>
#include <osmocom/core/utils.h>
#include <osmocom/core/bits.h>
#define MSGB_DEBUG
struct msgb {
struct llist_head list;
/* Part of which TRX logical channel we were received / transmitted */
/* FIXME: move them into the control buffer */
union {
void *dst;
struct gsm_bts_trx *trx;
};
struct gsm_lchan *lchan;
unsigned char *l1h;
unsigned char *l2h;
unsigned char *l3h;
unsigned char *l4h;
unsigned long cb[5];
uint16_t data_len;
uint16_t len;
unsigned char *head;
unsigned char *tail;
unsigned char *data;
unsigned char _data[0];
};
extern struct msgb *msgb_alloc(uint16_t size, const char *name);
extern void msgb_free(struct msgb *m);
extern void msgb_enqueue(struct llist_head *queue, struct msgb *msg);
extern struct msgb *msgb_dequeue(struct llist_head *queue);
extern void msgb_reset(struct msgb *m);
uint16_t msgb_length(const struct msgb *msg);
extern const char *msgb_hexdump(const struct msgb *msg);
#ifdef MSGB_DEBUG
#include <osmocom/core/panic.h>
#define MSGB_ABORT(msg, fmt, args ...) do { \
osmo_panic("msgb(%p): " fmt, msg, ## args); \
} while(0)
#else
#define MSGB_ABORT(msg, fmt, args ...)
#endif
#define msgb_l1(m) ((void *)(m->l1h))
#define msgb_l2(m) ((void *)(m->l2h))
#define msgb_l3(m) ((void *)(m->l3h))
#define msgb_sms(m) ((void *)(m->l4h))
static inline unsigned int msgb_l1len(const struct msgb *msgb)
{
return msgb->tail - (uint8_t *)msgb_l1(msgb);
}
static inline unsigned int msgb_l2len(const struct msgb *msgb)
{
return msgb->tail - (uint8_t *)msgb_l2(msgb);
}
static inline unsigned int msgb_l3len(const struct msgb *msgb)
{
return msgb->tail - (uint8_t *)msgb_l3(msgb);
}
static inline unsigned int msgb_headlen(const struct msgb *msgb)
{
return msgb->len - msgb->data_len;
}
static inline int msgb_tailroom(const struct msgb *msgb)
{
return (msgb->head + msgb->data_len) - msgb->tail;
}
static inline int msgb_headroom(const struct msgb *msgb)
{
return (msgb->data - msgb->head);
}
static inline unsigned char *msgb_put(struct msgb *msgb, unsigned int len)
{
unsigned char *tmp = msgb->tail;
if (msgb_tailroom(msgb) < (int) len)
MSGB_ABORT(msgb, "Not enough tailroom msgb_push (%u < %u)\n",
msgb_tailroom(msgb), len);
msgb->tail += len;
msgb->len += len;
return tmp;
}
static inline void msgb_put_u8(struct msgb *msgb, uint8_t word)
{
uint8_t *space = msgb_put(msgb, 1);
space[0] = word & 0xFF;
}
static inline void msgb_put_u16(struct msgb *msgb, uint16_t word)
{
uint8_t *space = msgb_put(msgb, 2);
osmo_store16be(word, space);
}
static inline void msgb_put_u32(struct msgb *msgb, uint32_t word)
{
uint8_t *space = msgb_put(msgb, 4);
osmo_store32be(word, space);
}
static inline unsigned char *msgb_get(struct msgb *msgb, unsigned int len)
{
unsigned char *tmp = msgb->tail - len;
if (msgb_length(msgb) < len)
MSGB_ABORT(msgb, "msgb too small to get %u (len %u)\n",
len, msgb_length(msgb));
msgb->tail -= len;
msgb->len -= len;
return tmp;
}
static inline uint8_t msgb_get_u8(struct msgb *msgb)
{
uint8_t *space = msgb_get(msgb, 1);
return space[0];
}
static inline uint16_t msgb_get_u16(struct msgb *msgb)
{
uint8_t *space = msgb_get(msgb, 2);
return osmo_load16be(space);
}
static inline uint32_t msgb_get_u32(struct msgb *msgb)
{
uint8_t *space = msgb_get(msgb, 4);
return osmo_load32be(space);
}
static inline unsigned char *msgb_push(struct msgb *msgb, unsigned int len)
{
if (msgb_headroom(msgb) < (int) len)
MSGB_ABORT(msgb, "Not enough headroom msgb_push (%u < %u)\n",
msgb_headroom(msgb), len);
msgb->data -= len;
msgb->len += len;
return msgb->data;
}
static inline unsigned char *msgb_pull(struct msgb *msgb, unsigned int len)
{
msgb->len -= len;
return msgb->data += len;
}
static inline unsigned char *msgb_pull_to_l3(struct msgb *msg)
{
unsigned char *ret = msgb_pull(msg, msg->l3h - msg->data);
msg->l1h = msg->l2h = NULL;
return ret;
}
static inline uint8_t msgb_pull_u8(struct msgb *msgb)
{
uint8_t *space = msgb_pull(msgb, 1) - 1;
return space[0];
}
static inline uint16_t msgb_pull_u16(struct msgb *msgb)
{
uint8_t *space = msgb_pull(msgb, 2) - 2;
return osmo_load16be(space);
}
static inline uint32_t msgb_pull_u32(struct msgb *msgb)
{
uint8_t *space = msgb_pull(msgb, 4) - 4;
return osmo_load32be(space);
}
static inline void msgb_reserve(struct msgb *msg, int len)
{
msg->data += len;
msg->tail += len;
}
static inline int msgb_trim(struct msgb *msg, int len)
{
if (len > msg->data_len)
return -1;
msg->len = len;
msg->tail = msg->data + len;
return 0;
}
static inline int msgb_l3trim(struct msgb *msg, int l3len)
{
return msgb_trim(msg, (msg->l3h - msg->data) + l3len);
}
static inline struct msgb *msgb_alloc_headroom(int size, int headroom,
const char *name)
{
osmo_static_assert(size > headroom, headroom_bigger);
struct msgb *msg = msgb_alloc(size, name);
if (msg)
msgb_reserve(msg, headroom);
return msg;
}
/* non inline functions to ease binding */
uint8_t *msgb_data(const struct msgb *msg);
void msgb_set_talloc_ctx(void *ctx);
msgb_put
static unsigned char * msgb_put(struct msgb *msgb, unsigned int len)
append data to end of message buffer
Definition: msgb.h:180
msgb_pull_u16
static uint16_t msgb_pull_u16(struct msgb *msgb)
remove uint16 from front of message
Definition: msgb.h:332
msgb_l3len
static unsigned int msgb_l3len(const struct msgb *msgb)
determine length of L3 message
Definition: msgb.h:127
msgb::cb
unsigned long cb[5]
control buffer
Definition: msgb.h:59
msgb::l3h
unsigned char * l3h
pointer to Layer 3 header. For OML: FOM; RSL: 04.08; GPRS: BSSGP
Definition: msgb.h:56
msgb_l3
#define msgb_l3(m)
obtain L3 header of msgb
Definition: msgb.h:92
msgb_l1
#define msgb_l1(m)
obtain L1 header of msgb
Definition: msgb.h:88
msgb_alloc
struct msgb * msgb_alloc(uint16_t size, const char *name)
Allocate a new message buffer.
Definition: msgb.c:47
msgb::data
unsigned char * data
start of message in buffer
Definition: msgb.h:66
msgb
Osmocom message buffer.
Definition: msgb.h:42
msgb_set_talloc_ctx
void msgb_set_talloc_ctx(void *ctx)
Set the talloc context for msgb_alloc.
Definition: msgb.c:151
msgb_l3trim
static int msgb_l3trim(struct msgb *msg, int l3len)
Trim the msgb to a given layer3 length.
Definition: msgb.h:386
msgb_trim
static int msgb_trim(struct msgb *msg, int len)
Trim the msgb to a given absolute length.
Definition: msgb.h:370
msgb::len
uint16_t len
length of bytes used in msgb
Definition: msgb.h:62
msgb_pull_to_l3
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:311
utils.h
msgb_free
void msgb_free(struct msgb *m)
Release given message buffer.
Definition: msgb.c:70
msgb_tailroom
static int msgb_tailroom(const struct msgb *msgb)
determine how much tail room is left in msgb
Definition: msgb.h:151
msgb::l4h
unsigned char * l4h
pointer to layer 4 header
Definition: msgb.h:57
msgb_l1len
static unsigned int msgb_l1len(const struct msgb *msgb)
determine length of L1 message
Definition: msgb.h:103
msgb_get
static unsigned char * msgb_get(struct msgb *msgb, unsigned int len)
remove data from end of message
Definition: msgb.h:225
msgb_hexdump
const char * msgb_hexdump(const struct msgb *msg)
Return a (static) buffer containing a hexdump of the msg.
Definition: msgb.c:160
msgb_dequeue
struct msgb * msgb_dequeue(struct llist_head *queue)
Dequeue message buffer from head of queue.
Definition: msgb.c:94
msgb_pull_u32
static uint32_t msgb_pull_u32(struct msgb *msgb)
remove uint32 from front of message
Definition: msgb.h:342
msgb_l2len
static unsigned int msgb_l2len(const struct msgb *msgb)
determine length of L2 message
Definition: msgb.h:115
msgb_alloc_headroom
static struct msgb * msgb_alloc_headroom(int size, int headroom, const char *name)
Allocate message buffer with specified headroom.
Definition: msgb.h:401
msgb_push
static unsigned char * msgb_push(struct msgb *msgb, unsigned int len)
prepend (push) some data to start of message
Definition: msgb.h:278
llist_head
Definition: linuxlist.h:42
msgb::l1h
unsigned char * l1h
pointer to Layer1 header (if any)
Definition: msgb.h:54
msgb_pull_u8
static uint8_t msgb_pull_u8(struct msgb *msgb)
remove uint8 from front of message
Definition: msgb.h:322
msgb_enqueue
void msgb_enqueue(struct llist_head *queue, struct msgb *msg)
Enqueue message buffer to tail of a queue.
Definition: msgb.c:82
msgb::lchan
struct gsm_lchan * lchan
logical channel
Definition: msgb.h:52
msgb_reset
void msgb_reset(struct msgb *m)
Re-set all message buffer pointers.
Definition: msgb.c:114
msgb_reserve
static void msgb_reserve(struct msgb *msg, int len)
Increase headroom of empty msgb, reducing the tailroom.
Definition: msgb.h:359
msgb::tail
unsigned char * tail
end of message in buffer
Definition: msgb.h:65
msgb_put_u16
static void msgb_put_u16(struct msgb *msgb, uint16_t word)
append a uint16 value to the end of the message
Definition: msgb.h:205
msgb_get_u8
static uint8_t msgb_get_u8(struct msgb *msgb)
remove uint8 from end of message
Definition: msgb.h:240
msgb::l2h
unsigned char * l2h
pointer to A-bis layer 2 header: OML, RSL(RLL), NS
Definition: msgb.h:55
panic.h
msgb::head
unsigned char * head
start of underlying memory buffer
Definition: msgb.h:64
msgb_get_u32
static uint32_t msgb_get_u32(struct msgb *msgb)
remove uint32 from end of message
Definition: msgb.h:260
msgb_headroom
static int msgb_headroom(const struct msgb *msgb)
determine the amount of headroom in msgb
Definition: msgb.h:163
bits.h
Osmocom bit level support code.
msgb::data_len
uint16_t data_len
length of underlying data array
Definition: msgb.h:61
msgb::_data
unsigned char _data[0]
optional immediate data array
Definition: msgb.h:67
msgb::list
struct llist_head list
linked list header
Definition: msgb.h:43
msgb_data
uint8_t * msgb_data(const struct msgb *msg)
get pointer to data section of message buffer
Definition: msgb.c:134
msgb_l2
#define msgb_l2(m)
obtain L2 header of msgb
Definition: msgb.h:90
msgb_get_u16
static uint16_t msgb_get_u16(struct msgb *msgb)
remove uint16 from end of message
Definition: msgb.h:250
msgb::dst
void * dst
reference of origin/destination
Definition: msgb.h:49
msgb_headlen
static unsigned int msgb_headlen(const struct msgb *msgb)
determine the length of the header
Definition: msgb.h:139
msgb_length
uint16_t msgb_length(const struct msgb *msg)
get length of message buffer
Definition: msgb.c:143
msgb_put_u32
static void msgb_put_u32(struct msgb *msgb, uint32_t word)
append a uint32 value to the end of the message
Definition: msgb.h:215
msgb_pull
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:297
msgb_put_u8
static void msgb_put_u8(struct msgb *msgb, uint8_t word)
append a uint8 value to the end of the message
Definition: msgb.h:195