Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 1 | #ifndef _MSGB_H |
| 2 | #define _MSGB_H |
| 3 | |
| 4 | /* (C) 2008 by Harald Welte <laforge@gnumonks.org> |
| 5 | * All Rights Reserved |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along |
| 18 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include <stdint.h> |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 24 | #include <osmocom/core/linuxlist.h> |
| 25 | #include <osmocom/core/utils.h> |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 26 | |
Harald Welte | 652a723 | 2010-07-22 21:55:24 +0200 | [diff] [blame] | 27 | #define MSGB_DEBUG |
| 28 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 29 | struct msgb { |
| 30 | struct llist_head list; |
| 31 | |
Pablo Neira Ayuso | 29cbf61 | 2011-07-07 19:46:44 +0200 | [diff] [blame^] | 32 | /* routing information. */ |
| 33 | void *dst; |
| 34 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 35 | /* Part of which TRX logical channel we were received / transmitted */ |
Harald Welte | 074c9f9 | 2010-04-30 14:29:11 +0200 | [diff] [blame] | 36 | /* FIXME: move them into the control buffer */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 37 | struct gsm_bts_trx *trx; |
| 38 | struct gsm_lchan *lchan; |
| 39 | |
Harald Welte | 00096ac | 2010-03-01 12:55:15 +0100 | [diff] [blame] | 40 | /* the Layer1 header (if any) */ |
| 41 | unsigned char *l1h; |
Harald Welte | 3415d41 | 2010-02-21 19:03:41 +0100 | [diff] [blame] | 42 | /* the A-bis layer 2 header: OML, RSL(RLL), NS */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 43 | unsigned char *l2h; |
Harald Welte | 3415d41 | 2010-02-21 19:03:41 +0100 | [diff] [blame] | 44 | /* the layer 3 header. For OML: FOM; RSL: 04.08; GPRS: BSSGP */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 45 | unsigned char *l3h; |
Harald Welte | 3415d41 | 2010-02-21 19:03:41 +0100 | [diff] [blame] | 46 | /* the layer 4 header */ |
Harald Welte | bb77c9d | 2010-04-30 14:26:12 +0200 | [diff] [blame] | 47 | unsigned char *l4h; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 48 | |
Harald Welte | 074c9f9 | 2010-04-30 14:29:11 +0200 | [diff] [blame] | 49 | /* the 'control buffer', large enough to contain 5 pointers */ |
| 50 | unsigned long cb[5]; |
| 51 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 52 | uint16_t data_len; |
| 53 | uint16_t len; |
| 54 | |
| 55 | unsigned char *head; |
| 56 | unsigned char *tail; |
| 57 | unsigned char *data; |
| 58 | unsigned char _data[0]; |
| 59 | }; |
| 60 | |
| 61 | extern struct msgb *msgb_alloc(uint16_t size, const char *name); |
| 62 | extern void msgb_free(struct msgb *m); |
| 63 | extern void msgb_enqueue(struct llist_head *queue, struct msgb *msg); |
| 64 | extern struct msgb *msgb_dequeue(struct llist_head *queue); |
| 65 | extern void msgb_reset(struct msgb *m); |
| 66 | |
Harald Welte | 652a723 | 2010-07-22 21:55:24 +0200 | [diff] [blame] | 67 | #ifdef MSGB_DEBUG |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 68 | #include <osmocom/core/panic.h> |
Harald Welte | 929d887 | 2010-11-05 07:47:41 +0100 | [diff] [blame] | 69 | #define MSGB_ABORT(msg, fmt, args ...) do { \ |
Harald Welte | dab0287 | 2010-11-09 13:42:26 +0100 | [diff] [blame] | 70 | osmo_panic("msgb(%p): " fmt, msg, ## args); \ |
Harald Welte | 929d887 | 2010-11-05 07:47:41 +0100 | [diff] [blame] | 71 | } while(0) |
| 72 | #else |
| 73 | #define MSGB_ABORT(msg, fmt, args ...) |
Harald Welte | 652a723 | 2010-07-22 21:55:24 +0200 | [diff] [blame] | 74 | #endif |
| 75 | |
Harald Welte | fdd0a70 | 2010-03-01 22:30:51 +0100 | [diff] [blame] | 76 | #define msgb_l1(m) ((void *)(m->l1h)) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 77 | #define msgb_l2(m) ((void *)(m->l2h)) |
| 78 | #define msgb_l3(m) ((void *)(m->l3h)) |
Harald Welte | bb77c9d | 2010-04-30 14:26:12 +0200 | [diff] [blame] | 79 | #define msgb_sms(m) ((void *)(m->l4h)) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 80 | |
Harald Welte | fdd0a70 | 2010-03-01 22:30:51 +0100 | [diff] [blame] | 81 | static inline unsigned int msgb_l1len(const struct msgb *msgb) |
| 82 | { |
| 83 | return msgb->tail - (uint8_t *)msgb_l1(msgb); |
| 84 | } |
| 85 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 86 | static inline unsigned int msgb_l2len(const struct msgb *msgb) |
| 87 | { |
| 88 | return msgb->tail - (uint8_t *)msgb_l2(msgb); |
| 89 | } |
| 90 | |
| 91 | static inline unsigned int msgb_l3len(const struct msgb *msgb) |
| 92 | { |
| 93 | return msgb->tail - (uint8_t *)msgb_l3(msgb); |
| 94 | } |
| 95 | |
| 96 | static inline unsigned int msgb_headlen(const struct msgb *msgb) |
| 97 | { |
| 98 | return msgb->len - msgb->data_len; |
| 99 | } |
Harald Welte | 652a723 | 2010-07-22 21:55:24 +0200 | [diff] [blame] | 100 | |
| 101 | static inline int msgb_tailroom(const struct msgb *msgb) |
| 102 | { |
| 103 | return (msgb->head + msgb->data_len) - msgb->tail; |
| 104 | } |
| 105 | |
| 106 | static inline int msgb_headroom(const struct msgb *msgb) |
| 107 | { |
| 108 | return (msgb->data - msgb->head); |
| 109 | } |
| 110 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 111 | static inline unsigned char *msgb_put(struct msgb *msgb, unsigned int len) |
| 112 | { |
| 113 | unsigned char *tmp = msgb->tail; |
Harald Welte | 46a45a6 | 2010-11-09 13:41:48 +0100 | [diff] [blame] | 114 | if (msgb_tailroom(msgb) < (int) len) |
Harald Welte | 929d887 | 2010-11-05 07:47:41 +0100 | [diff] [blame] | 115 | MSGB_ABORT(msgb, "Not enough tailroom msgb_push (%u < %u)\n", |
| 116 | msgb_tailroom(msgb), len); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 117 | msgb->tail += len; |
| 118 | msgb->len += len; |
| 119 | return tmp; |
| 120 | } |
Ingo Albrecht | 48e17f8 | 2010-03-07 18:03:41 +0100 | [diff] [blame] | 121 | static inline void msgb_put_u8(struct msgb *msgb, uint8_t word) |
| 122 | { |
| 123 | uint8_t *space = msgb_put(msgb, 1); |
| 124 | space[0] = word & 0xFF; |
| 125 | } |
| 126 | static inline void msgb_put_u16(struct msgb *msgb, uint16_t word) |
| 127 | { |
| 128 | uint8_t *space = msgb_put(msgb, 2); |
| 129 | space[0] = word >> 8 & 0xFF; |
| 130 | space[1] = word & 0xFF; |
| 131 | } |
| 132 | static inline void msgb_put_u32(struct msgb *msgb, uint32_t word) |
| 133 | { |
| 134 | uint8_t *space = msgb_put(msgb, 4); |
| 135 | space[0] = word >> 24 & 0xFF; |
| 136 | space[1] = word >> 16 & 0xFF; |
| 137 | space[2] = word >> 8 & 0xFF; |
| 138 | space[3] = word & 0xFF; |
| 139 | } |
| 140 | static inline unsigned char *msgb_get(struct msgb *msgb, unsigned int len) |
| 141 | { |
| 142 | unsigned char *tmp = msgb->data; |
| 143 | msgb->data += len; |
| 144 | msgb->len -= len; |
| 145 | return tmp; |
| 146 | } |
| 147 | static inline uint8_t msgb_get_u8(struct msgb *msgb) |
| 148 | { |
| 149 | uint8_t *space = msgb_get(msgb, 1); |
| 150 | return space[0]; |
| 151 | } |
| 152 | static inline uint16_t msgb_get_u16(struct msgb *msgb) |
| 153 | { |
| 154 | uint8_t *space = msgb_get(msgb, 2); |
| 155 | return space[0] << 8 | space[1]; |
| 156 | } |
| 157 | static inline uint32_t msgb_get_u32(struct msgb *msgb) |
| 158 | { |
| 159 | uint8_t *space = msgb_get(msgb, 4); |
| 160 | return space[0] << 24 | space[1] << 16 | space[2] << 8 | space[3]; |
| 161 | } |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 162 | static inline unsigned char *msgb_push(struct msgb *msgb, unsigned int len) |
| 163 | { |
Harald Welte | 46a45a6 | 2010-11-09 13:41:48 +0100 | [diff] [blame] | 164 | if (msgb_headroom(msgb) < (int) len) |
Harald Welte | 929d887 | 2010-11-05 07:47:41 +0100 | [diff] [blame] | 165 | MSGB_ABORT(msgb, "Not enough headroom msgb_push (%u < %u)\n", |
| 166 | msgb_headroom(msgb), len); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 167 | msgb->data -= len; |
| 168 | msgb->len += len; |
| 169 | return msgb->data; |
| 170 | } |
| 171 | static inline unsigned char *msgb_pull(struct msgb *msgb, unsigned int len) |
| 172 | { |
| 173 | msgb->len -= len; |
| 174 | return msgb->data += len; |
| 175 | } |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 176 | |
| 177 | /* increase the headroom of an empty msgb, reducing the tailroom */ |
| 178 | static inline void msgb_reserve(struct msgb *msg, int len) |
| 179 | { |
| 180 | msg->data += len; |
| 181 | msg->tail += len; |
| 182 | } |
| 183 | |
| 184 | static inline struct msgb *msgb_alloc_headroom(int size, int headroom, |
| 185 | const char *name) |
| 186 | { |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 187 | osmo_static_assert(size > headroom, headroom_bigger); |
Holger Hans Peter Freyther | 5853f08 | 2011-01-16 17:38:22 +0100 | [diff] [blame] | 188 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 189 | struct msgb *msg = msgb_alloc(size, name); |
| 190 | if (msg) |
| 191 | msgb_reserve(msg, headroom); |
| 192 | return msg; |
| 193 | } |
| 194 | |
Holger Hans Peter Freyther | acffb60 | 2010-10-18 18:22:31 +0200 | [diff] [blame] | 195 | /* non inline functions to ease binding */ |
| 196 | uint8_t *msgb_data(const struct msgb *msg); |
| 197 | uint16_t msgb_length(const struct msgb *msg); |
| 198 | |
Harald Welte | 9e1f060 | 2011-06-29 18:46:10 +0200 | [diff] [blame] | 199 | /* set the talloc context for msgb_alloc[_headroom] */ |
| 200 | void msgb_set_talloc_ctx(void *ctx); |
Holger Hans Peter Freyther | acffb60 | 2010-10-18 18:22:31 +0200 | [diff] [blame] | 201 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 202 | #endif /* _MSGB_H */ |