Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1 | /* |
| 2 | * (C) 2012-2013 by Pablo Neira Ayuso <pablo@gnumonks.org> |
| 3 | * (C) 2012-2013 by On Waves ehf <http://www.on-waves.com> |
| 4 | * All rights not specifically granted under this license are reserved. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU Affero General Public License as published by the |
| 8 | * Free Software Foundation; either version 3 of the License, or (at your |
| 9 | * option) any later version. |
| 10 | */ |
| 11 | |
| 12 | #include <stdio.h> /* for printf */ |
| 13 | #include <string.h> /* for memcpy */ |
| 14 | #include <stdlib.h> /* for abs */ |
| 15 | #include <inttypes.h> /* for PRIu64 */ |
| 16 | #include <netinet/in.h> |
| 17 | #include <osmocom/core/msgb.h> |
Pau Espin Pedrol | f027f17 | 2019-05-06 13:57:31 +0200 | [diff] [blame] | 18 | #include <osmocom/core/socket.h> |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 19 | #include <osmocom/core/talloc.h> |
| 20 | |
| 21 | #include <osmocom/netif/osmux.h> |
| 22 | #include <osmocom/netif/rtp.h> |
Pau Espin Pedrol | c1ad553 | 2019-05-15 23:00:53 +0200 | [diff] [blame] | 23 | #include <osmocom/netif/amr.h> |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 24 | |
Pau Espin Pedrol | ec5e85b | 2022-09-09 16:04:19 +0200 | [diff] [blame] | 25 | #include <osmocom/mgcp/debug.h> |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 26 | #include <osmocom/mgcp/mgcp.h> |
Philipp Maier | 993ea6b | 2020-08-04 18:26:50 +0200 | [diff] [blame] | 27 | #include <osmocom/mgcp/mgcp_protocol.h> |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 28 | #include <osmocom/mgcp/osmux.h> |
| 29 | #include <osmocom/mgcp/mgcp_conn.h> |
Philipp Maier | 37d11c8 | 2018-02-01 14:38:12 +0100 | [diff] [blame] | 30 | #include <osmocom/mgcp/mgcp_endp.h> |
Philipp Maier | c66ab2c | 2020-06-02 20:55:34 +0200 | [diff] [blame] | 31 | #include <osmocom/mgcp/mgcp_trunk.h> |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 32 | |
| 33 | static struct osmo_fd osmux_fd; |
| 34 | |
| 35 | static LLIST_HEAD(osmux_handle_list); |
| 36 | |
| 37 | struct osmux_handle { |
| 38 | struct llist_head head; |
| 39 | struct osmux_in_handle *in; |
Pau Espin Pedrol | 15e7e4f | 2022-09-09 16:52:41 +0200 | [diff] [blame] | 40 | struct osmo_sockaddr rem_addr; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 41 | int refcnt; |
| 42 | }; |
| 43 | |
Pau Espin Pedrol | ea7aaf2 | 2022-09-22 21:07:54 +0200 | [diff] [blame] | 44 | const struct value_string osmux_state_strs[] = { |
| 45 | { OSMUX_STATE_DISABLED, "disabled" }, |
| 46 | { OSMUX_STATE_ACTIVATING, "activating" }, |
| 47 | { OSMUX_STATE_ENABLED, "enabled" }, |
| 48 | { 0, NULL } |
| 49 | }; |
| 50 | |
Pau Espin Pedrol | 582c2bf | 2022-09-22 17:53:44 +0200 | [diff] [blame] | 51 | static const struct rate_ctr_group_desc rate_ctr_group_osmux_desc = { |
| 52 | .group_name_prefix = "conn_osmux", |
| 53 | .group_description = "Osmux connection statistics", |
| 54 | .class_id = 1, |
| 55 | .num_ctr = ARRAY_SIZE(mgcp_conn_osmux_rate_ctr_desc), |
| 56 | .ctr_desc = mgcp_conn_osmux_rate_ctr_desc |
| 57 | }; |
| 58 | |
Pau Espin Pedrol | b4a5947 | 2022-09-23 18:08:53 +0200 | [diff] [blame] | 59 | static void rtpconn_osmux_rate_ctr_add(struct mgcp_conn_rtp *conn_rtp, int id, int inc) |
| 60 | { |
| 61 | struct rate_ctr_group *conn_osmux_stats = conn_rtp->osmux.ctrg; |
| 62 | struct rate_ctr_group *trunk_osmux_stats = conn_rtp->conn->endp->trunk->ratectr.all_osmux_conn_stats; |
| 63 | |
| 64 | /* add to both the per-connection and the global stats */ |
| 65 | rate_ctr_add(rate_ctr_group_get_ctr(conn_osmux_stats, id), inc); |
| 66 | rate_ctr_add(rate_ctr_group_get_ctr(trunk_osmux_stats, id), inc); |
| 67 | } |
| 68 | |
| 69 | static void rtpconn_osmux_rate_ctr_inc(struct mgcp_conn_rtp *conn_rtp, int id) |
| 70 | { |
| 71 | rtpconn_osmux_rate_ctr_add(conn_rtp, id, 1); |
| 72 | } |
| 73 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 74 | /* Deliver OSMUX batch to the remote end */ |
| 75 | static void osmux_deliver_cb(struct msgb *batch_msg, void *data) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 76 | { |
| 77 | struct osmux_handle *handle = data; |
Pau Espin Pedrol | 15e7e4f | 2022-09-09 16:52:41 +0200 | [diff] [blame] | 78 | socklen_t dest_len; |
Pau Espin Pedrol | 4d1a52d | 2022-09-22 17:11:57 +0200 | [diff] [blame] | 79 | int rc; |
Pau Espin Pedrol | 582c2bf | 2022-09-22 17:53:44 +0200 | [diff] [blame] | 80 | struct mgcp_trunk *trunk = (struct mgcp_trunk *)osmux_fd.data; |
| 81 | struct rate_ctr_group *all_osmux_stats = trunk->ratectr.all_osmux_conn_stats; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 82 | |
Pau Espin Pedrol | 15e7e4f | 2022-09-09 16:52:41 +0200 | [diff] [blame] | 83 | switch (handle->rem_addr.u.sa.sa_family) { |
| 84 | case AF_INET6: |
| 85 | dest_len = sizeof(handle->rem_addr.u.sin6); |
| 86 | break; |
| 87 | case AF_INET: |
| 88 | default: |
| 89 | dest_len = sizeof(handle->rem_addr.u.sin); |
| 90 | break; |
| 91 | } |
Pau Espin Pedrol | 4d1a52d | 2022-09-22 17:11:57 +0200 | [diff] [blame] | 92 | rc = sendto(osmux_fd.fd, batch_msg->data, batch_msg->len, 0, |
| 93 | (struct sockaddr *)&handle->rem_addr.u.sa, dest_len); |
| 94 | if (rc < 0) { |
| 95 | char errbuf[129]; |
| 96 | strerror_r(errno, errbuf, sizeof(errbuf)); |
| 97 | LOGP(DOSMUX, LOGL_NOTICE, "osmux sendto(%s) failed: %s\n", |
| 98 | osmo_sockaddr_to_str(&handle->rem_addr), errbuf); |
Pau Espin Pedrol | 582c2bf | 2022-09-22 17:53:44 +0200 | [diff] [blame] | 99 | rate_ctr_inc(rate_ctr_group_get_ctr(all_osmux_stats, OSMUX_DROPPED_PACKETS_CTR)); |
| 100 | } else { |
| 101 | rate_ctr_inc(rate_ctr_group_get_ctr(all_osmux_stats, OSMUX_PACKETS_TX_CTR)); |
Pau Espin Pedrol | 4d1a52d | 2022-09-22 17:11:57 +0200 | [diff] [blame] | 102 | } |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 103 | msgb_free(batch_msg); |
| 104 | } |
| 105 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 106 | /* Lookup existing OSMUX handle for specified destination address. */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 107 | static struct osmux_handle * |
Pau Espin Pedrol | 15e7e4f | 2022-09-09 16:52:41 +0200 | [diff] [blame] | 108 | osmux_handle_find_get(const struct osmo_sockaddr *rem_addr) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 109 | { |
| 110 | struct osmux_handle *h; |
| 111 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 112 | llist_for_each_entry(h, &osmux_handle_list, head) { |
Pau Espin Pedrol | 15e7e4f | 2022-09-09 16:52:41 +0200 | [diff] [blame] | 113 | if (osmo_sockaddr_cmp(&h->rem_addr, rem_addr) == 0) { |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 114 | h->refcnt++; |
Pau Espin Pedrol | c61664c | 2022-09-09 17:03:13 +0200 | [diff] [blame] | 115 | LOGP(DOSMUX, LOGL_DEBUG, |
| 116 | "Using existing OSMUX handle for addr=%s (rfcnt=%u)\n", |
| 117 | osmo_sockaddr_to_str(rem_addr), h->refcnt); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 118 | return h; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | return NULL; |
| 123 | } |
| 124 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 125 | /* Put down no longer needed OSMUX handle */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 126 | static void osmux_handle_put(struct osmux_in_handle *in) |
| 127 | { |
| 128 | struct osmux_handle *h; |
| 129 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 130 | llist_for_each_entry(h, &osmux_handle_list, head) { |
| 131 | if (h->in == in) { |
Pau Espin Pedrol | c61664c | 2022-09-09 17:03:13 +0200 | [diff] [blame] | 132 | LOGP(DOSMUX, LOGL_DEBUG, |
| 133 | "Putting existing OSMUX handle for addr=%s (rfcnt=%u)\n", |
| 134 | osmo_sockaddr_to_str(&h->rem_addr), h->refcnt); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 135 | if (--h->refcnt == 0) { |
Pau Espin Pedrol | ec5e85b | 2022-09-09 16:04:19 +0200 | [diff] [blame] | 136 | LOGP(DOSMUX, LOGL_INFO, |
Pau Espin Pedrol | 15e7e4f | 2022-09-09 16:52:41 +0200 | [diff] [blame] | 137 | "Releasing unused osmux handle for %s\n", |
| 138 | osmo_sockaddr_to_str(&h->rem_addr)); |
Pau Espin Pedrol | ec5e85b | 2022-09-09 16:04:19 +0200 | [diff] [blame] | 139 | LOGP(DOSMUX, LOGL_INFO, "Stats: " |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 140 | "input RTP msgs: %u bytes: %"PRIu64" " |
| 141 | "output osmux msgs: %u bytes: %"PRIu64"\n", |
| 142 | in->stats.input_rtp_msgs, |
| 143 | in->stats.input_rtp_bytes, |
| 144 | in->stats.output_osmux_msgs, |
| 145 | in->stats.output_osmux_bytes); |
| 146 | llist_del(&h->head); |
| 147 | osmux_xfrm_input_fini(h->in); |
| 148 | talloc_free(h); |
| 149 | } |
| 150 | return; |
| 151 | } |
| 152 | } |
Pau Espin Pedrol | 15e7e4f | 2022-09-09 16:52:41 +0200 | [diff] [blame] | 153 | LOGP(DOSMUX, LOGL_ERROR, "Cannot find Osmux input handle %p\n", in); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 154 | } |
| 155 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 156 | /* Allocate free OSMUX handle */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 157 | static struct osmux_handle * |
Pau Espin Pedrol | 15e7e4f | 2022-09-09 16:52:41 +0200 | [diff] [blame] | 158 | osmux_handle_alloc(struct mgcp_conn_rtp *conn, const struct osmo_sockaddr *rem_addr) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 159 | { |
| 160 | struct osmux_handle *h; |
Pau Espin Pedrol | 941e317 | 2022-09-22 21:16:56 +0200 | [diff] [blame] | 161 | struct mgcp_trunk *trunk = conn->conn->endp->trunk; |
| 162 | struct mgcp_config *cfg = trunk->cfg; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 163 | |
Pau Espin Pedrol | 941e317 | 2022-09-22 21:16:56 +0200 | [diff] [blame] | 164 | h = talloc_zero(trunk, struct osmux_handle); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 165 | if (!h) |
| 166 | return NULL; |
Pau Espin Pedrol | 15e7e4f | 2022-09-09 16:52:41 +0200 | [diff] [blame] | 167 | h->rem_addr = *rem_addr; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 168 | h->refcnt++; |
| 169 | |
| 170 | h->in = talloc_zero(h, struct osmux_in_handle); |
| 171 | if (!h->in) { |
| 172 | talloc_free(h); |
| 173 | return NULL; |
| 174 | } |
| 175 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 176 | /* sequence number to start OSMUX message from */ |
| 177 | h->in->osmux_seq = 0; |
| 178 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 179 | h->in->batch_factor = cfg->osmux_batch; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 180 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 181 | /* If batch size is zero, the library defaults to 1470 bytes. */ |
| 182 | h->in->batch_size = cfg->osmux_batch_size; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 183 | h->in->deliver = osmux_deliver_cb; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 184 | osmux_xfrm_input_init(h->in); |
| 185 | h->in->data = h; |
| 186 | |
| 187 | llist_add(&h->head, &osmux_handle_list); |
| 188 | |
Pau Espin Pedrol | 15e7e4f | 2022-09-09 16:52:41 +0200 | [diff] [blame] | 189 | LOGP(DOSMUX, LOGL_DEBUG, "Created new OSMUX handle for rem_addr=%s\n", |
| 190 | osmo_sockaddr_to_str(rem_addr)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 191 | |
| 192 | return h; |
| 193 | } |
| 194 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 195 | /* Lookup existing handle for a specified address, if the handle can not be |
Harald Welte | 1d1b98f | 2017-12-25 10:03:40 +0100 | [diff] [blame] | 196 | * found, the function will automatically allocate one */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 197 | static struct osmux_in_handle * |
Pau Espin Pedrol | 15e7e4f | 2022-09-09 16:52:41 +0200 | [diff] [blame] | 198 | osmux_handle_find_or_create(struct mgcp_conn_rtp *conn, const struct osmo_sockaddr *rem_addr) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 199 | { |
| 200 | struct osmux_handle *h; |
| 201 | |
Pau Espin Pedrol | 15e7e4f | 2022-09-09 16:52:41 +0200 | [diff] [blame] | 202 | if (rem_addr->u.sa.sa_family != AF_INET) { |
Pau Espin Pedrol | ec5e85b | 2022-09-09 16:04:19 +0200 | [diff] [blame] | 203 | LOGP(DOSMUX, LOGL_DEBUG, "IPv6 not supported in osmux yet!\n"); |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 204 | return NULL; |
| 205 | } |
| 206 | |
Pau Espin Pedrol | 15e7e4f | 2022-09-09 16:52:41 +0200 | [diff] [blame] | 207 | h = osmux_handle_find_get(rem_addr); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 208 | if (h != NULL) |
| 209 | return h->in; |
| 210 | |
Pau Espin Pedrol | 15e7e4f | 2022-09-09 16:52:41 +0200 | [diff] [blame] | 211 | h = osmux_handle_alloc(conn, rem_addr); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 212 | if (h == NULL) |
| 213 | return NULL; |
| 214 | |
| 215 | return h->in; |
| 216 | } |
| 217 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 218 | /*! send RTP packet through OSMUX connection. |
| 219 | * \param[in] buf rtp data |
| 220 | * \param[in] buf_len length of rtp data |
| 221 | * \param[in] conn associated RTP connection |
| 222 | * \returns 0 on success, -1 on ERROR */ |
| 223 | int osmux_xfrm_to_osmux(char *buf, int buf_len, struct mgcp_conn_rtp *conn) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 224 | { |
| 225 | int ret; |
| 226 | struct msgb *msg; |
| 227 | |
Pau Espin Pedrol | 582c2bf | 2022-09-22 17:53:44 +0200 | [diff] [blame] | 228 | if (!conn->end.output_enabled) { |
Pau Espin Pedrol | 432ee9d | 2022-09-23 18:18:02 +0200 | [diff] [blame] | 229 | rtpconn_osmux_rate_ctr_inc(conn, OSMUX_RTP_PACKETS_TX_DROPPED_CTR); |
Pau Espin Pedrol | e39ae87 | 2022-09-09 17:36:33 +0200 | [diff] [blame] | 230 | return -1; |
Pau Espin Pedrol | 582c2bf | 2022-09-22 17:53:44 +0200 | [diff] [blame] | 231 | } |
Pau Espin Pedrol | e39ae87 | 2022-09-09 17:36:33 +0200 | [diff] [blame] | 232 | |
Pau Espin Pedrol | 8b0a614 | 2022-09-09 17:34:31 +0200 | [diff] [blame] | 233 | if (conn->osmux.state != OSMUX_STATE_ENABLED) { |
| 234 | LOGPCONN(conn->conn, DOSMUX, LOGL_INFO, "forwarding RTP to Osmux conn not yet enabled, dropping (cid=%d)\n", |
Pau Espin Pedrol | 2177919 | 2022-09-23 16:46:33 +0200 | [diff] [blame] | 235 | conn->osmux.remote_cid); |
Pau Espin Pedrol | 432ee9d | 2022-09-23 18:18:02 +0200 | [diff] [blame] | 236 | rtpconn_osmux_rate_ctr_inc(conn, OSMUX_RTP_PACKETS_TX_DROPPED_CTR); |
Pau Espin Pedrol | 8b0a614 | 2022-09-09 17:34:31 +0200 | [diff] [blame] | 237 | return -1; |
| 238 | } |
| 239 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 240 | msg = msgb_alloc(4096, "RTP"); |
| 241 | if (!msg) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 242 | return -1; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 243 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 244 | memcpy(msg->data, buf, buf_len); |
| 245 | msgb_put(msg, buf_len); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 246 | |
Pau Espin Pedrol | 2177919 | 2022-09-23 16:46:33 +0200 | [diff] [blame] | 247 | while ((ret = osmux_xfrm_input(conn->osmux.in, msg, conn->osmux.remote_cid)) > 0) { |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 248 | /* batch full, build and deliver it */ |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 249 | osmux_xfrm_input_deliver(conn->osmux.in); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 250 | } |
Pau Espin Pedrol | 432ee9d | 2022-09-23 18:18:02 +0200 | [diff] [blame] | 251 | if (ret < 0) { |
| 252 | rtpconn_osmux_rate_ctr_inc(conn, OSMUX_RTP_PACKETS_TX_DROPPED_CTR); |
| 253 | } else { |
| 254 | rtpconn_osmux_rate_ctr_inc(conn, OSMUX_RTP_PACKETS_TX_CTR); |
| 255 | rtpconn_osmux_rate_ctr_add(conn, OSMUX_AMR_OCTETS_TX_CTR, buf_len - sizeof(struct rtp_hdr)); |
| 256 | } |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 257 | return 0; |
| 258 | } |
| 259 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 260 | /* Lookup the endpoint that corresponds to the specified address (port) */ |
Pau Espin Pedrol | 1442c5a | 2019-05-06 16:10:10 +0200 | [diff] [blame] | 261 | static struct mgcp_conn_rtp* |
Pau Espin Pedrol | 2177919 | 2022-09-23 16:46:33 +0200 | [diff] [blame] | 262 | osmux_conn_lookup(struct mgcp_trunk *trunk, uint8_t local_cid, const struct osmo_sockaddr *rem_addr) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 263 | { |
Pau Espin Pedrol | 1442c5a | 2019-05-06 16:10:10 +0200 | [diff] [blame] | 264 | struct mgcp_endpoint *endp; |
| 265 | struct mgcp_conn *conn = NULL; |
| 266 | struct mgcp_conn_rtp * conn_rtp; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 267 | int i; |
| 268 | |
Philipp Maier | d19de2e | 2020-06-03 13:55:33 +0200 | [diff] [blame] | 269 | for (i = 0; i < trunk->number_endpoints; i++) { |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 270 | |
Philipp Maier | d19de2e | 2020-06-03 13:55:33 +0200 | [diff] [blame] | 271 | endp = trunk->endpoints[i]; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 272 | |
Pau Espin Pedrol | 1442c5a | 2019-05-06 16:10:10 +0200 | [diff] [blame] | 273 | llist_for_each_entry(conn, &endp->conns, entry) { |
| 274 | if (conn->type != MGCP_CONN_TYPE_RTP) |
| 275 | continue; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 276 | |
Pau Espin Pedrol | 1442c5a | 2019-05-06 16:10:10 +0200 | [diff] [blame] | 277 | conn_rtp = &conn->u.rtp; |
| 278 | if (!mgcp_conn_rtp_is_osmux(conn_rtp)) |
| 279 | continue; |
| 280 | |
Pau Espin Pedrol | 15e7e4f | 2022-09-09 16:52:41 +0200 | [diff] [blame] | 281 | /* FIXME: Match remote address! */ |
| 282 | |
Pau Espin Pedrol | 2177919 | 2022-09-23 16:46:33 +0200 | [diff] [blame] | 283 | if (conn_rtp->osmux.local_cid == local_cid) |
Pau Espin Pedrol | 1442c5a | 2019-05-06 16:10:10 +0200 | [diff] [blame] | 284 | return conn_rtp; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 285 | } |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 286 | } |
| 287 | |
Pau Espin Pedrol | 2177919 | 2022-09-23 16:46:33 +0200 | [diff] [blame] | 288 | LOGP(DOSMUX, LOGL_ERROR, "Cannot find osmux conn with rem_addr=%s local_cid=%d\n", |
| 289 | osmo_sockaddr_to_str(rem_addr), local_cid); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 290 | |
| 291 | return NULL; |
| 292 | } |
| 293 | |
Pau Espin Pedrol | 1442c5a | 2019-05-06 16:10:10 +0200 | [diff] [blame] | 294 | /* FIXME: this is declared and used in mgcp_network.c, but documentation of mgcp_dispatch_rtp_bridge_cb() states another enum is to be used */ |
| 295 | enum { |
| 296 | MGCP_PROTO_RTP, |
| 297 | MGCP_PROTO_RTCP, |
| 298 | }; |
| 299 | |
| 300 | static void scheduled_from_osmux_tx_rtp_cb(struct msgb *msg, void *data) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 301 | { |
Pau Espin Pedrol | 1442c5a | 2019-05-06 16:10:10 +0200 | [diff] [blame] | 302 | struct mgcp_conn_rtp *conn = data; |
| 303 | struct mgcp_endpoint *endp = conn->conn->endp; |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 304 | struct osmo_sockaddr addr = { /* FIXME: do we know the source address?? */ }; |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 305 | struct osmo_rtp_msg_ctx *mc = OSMO_RTP_MSG_CTX(msg); |
| 306 | *mc = (struct osmo_rtp_msg_ctx){ |
| 307 | .proto = MGCP_PROTO_RTP, |
| 308 | .conn_src = conn, |
| 309 | .from_addr = &addr, |
| 310 | }; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 311 | |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 312 | endp->type->dispatch_rtp_cb(msg); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 313 | msgb_free(msg); |
| 314 | } |
| 315 | |
Pau Espin Pedrol | 15e7e4f | 2022-09-09 16:52:41 +0200 | [diff] [blame] | 316 | static struct msgb *osmux_recv(struct osmo_fd *ofd, struct osmo_sockaddr *addr) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 317 | { |
| 318 | struct msgb *msg; |
Pau Espin Pedrol | 15e7e4f | 2022-09-09 16:52:41 +0200 | [diff] [blame] | 319 | socklen_t slen = sizeof(addr->u.sas); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 320 | int ret; |
| 321 | |
| 322 | msg = msgb_alloc(4096, "OSMUX"); |
| 323 | if (!msg) { |
Pau Espin Pedrol | ec5e85b | 2022-09-09 16:04:19 +0200 | [diff] [blame] | 324 | LOGP(DOSMUX, LOGL_ERROR, "cannot allocate message\n"); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 325 | return NULL; |
| 326 | } |
Pau Espin Pedrol | 15e7e4f | 2022-09-09 16:52:41 +0200 | [diff] [blame] | 327 | ret = recvfrom(ofd->fd, msg->data, msg->data_len, 0, &addr->u.sa, &slen); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 328 | if (ret <= 0) { |
| 329 | msgb_free(msg); |
Pau Espin Pedrol | ec5e85b | 2022-09-09 16:04:19 +0200 | [diff] [blame] | 330 | LOGP(DOSMUX, LOGL_ERROR, "cannot receive message\n"); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 331 | return NULL; |
| 332 | } |
| 333 | msgb_put(msg, ret); |
| 334 | |
| 335 | return msg; |
| 336 | } |
| 337 | |
Pau Espin Pedrol | de2a4d7 | 2018-10-16 16:37:43 +0200 | [diff] [blame] | 338 | /* Updates endp osmux state and returns 0 if it can process messages, -1 otherwise */ |
| 339 | static int endp_osmux_state_check(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn, |
| 340 | bool sending) |
| 341 | { |
Pau Espin Pedrol | 15e7e4f | 2022-09-09 16:52:41 +0200 | [diff] [blame] | 342 | struct osmo_sockaddr rem_addr; |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 343 | |
Pau Espin Pedrol | de2a4d7 | 2018-10-16 16:37:43 +0200 | [diff] [blame] | 344 | switch(conn->osmux.state) { |
| 345 | case OSMUX_STATE_ACTIVATING: |
Pau Espin Pedrol | 15e7e4f | 2022-09-09 16:52:41 +0200 | [diff] [blame] | 346 | rem_addr = conn->end.addr; |
| 347 | osmo_sockaddr_set_port(&rem_addr.u.sa, ntohs(conn->end.rtp_port)); |
| 348 | if (osmux_enable_conn(endp, conn, &rem_addr) < 0) { |
Pau Espin Pedrol | ec5e85b | 2022-09-09 16:04:19 +0200 | [diff] [blame] | 349 | LOGPCONN(conn->conn, DOSMUX, LOGL_ERROR, |
Pau Espin Pedrol | 3752522 | 2019-05-13 17:34:14 +0200 | [diff] [blame] | 350 | "Could not enable osmux for conn on %s: %s\n", |
| 351 | sending ? "sent" : "received", |
Pau Espin Pedrol | c9a6280 | 2019-05-13 13:20:13 +0200 | [diff] [blame] | 352 | mgcp_conn_dump(conn->conn)); |
Pau Espin Pedrol | de2a4d7 | 2018-10-16 16:37:43 +0200 | [diff] [blame] | 353 | return -1; |
| 354 | } |
Pau Espin Pedrol | c5bbb78 | 2022-09-19 16:46:02 +0200 | [diff] [blame] | 355 | LOGPCONN(conn->conn, DOSMUX, LOGL_INFO, |
Pau Espin Pedrol | 15e7e4f | 2022-09-09 16:52:41 +0200 | [diff] [blame] | 356 | "Osmux %s CID %u towards %s is now enabled\n", |
Pau Espin Pedrol | 3752522 | 2019-05-13 17:34:14 +0200 | [diff] [blame] | 357 | sending ? "sent" : "received", |
Pau Espin Pedrol | 2177919 | 2022-09-23 16:46:33 +0200 | [diff] [blame] | 358 | sending ? conn->osmux.remote_cid : conn->osmux.local_cid, |
| 359 | osmo_sockaddr_to_str(&rem_addr)); |
Pau Espin Pedrol | de2a4d7 | 2018-10-16 16:37:43 +0200 | [diff] [blame] | 360 | return 0; |
| 361 | case OSMUX_STATE_ENABLED: |
| 362 | return 0; |
| 363 | default: |
Pau Espin Pedrol | ec5e85b | 2022-09-09 16:04:19 +0200 | [diff] [blame] | 364 | LOGPCONN(conn->conn, DOSMUX, LOGL_ERROR, |
Pau Espin Pedrol | c9a6280 | 2019-05-13 13:20:13 +0200 | [diff] [blame] | 365 | "Osmux %s in conn %s without full negotiation, state %d\n", |
| 366 | sending ? "sent" : "received", |
| 367 | mgcp_conn_dump(conn->conn), conn->osmux.state); |
Pau Espin Pedrol | de2a4d7 | 2018-10-16 16:37:43 +0200 | [diff] [blame] | 368 | return -1; |
| 369 | } |
| 370 | } |
| 371 | |
Pau Espin Pedrol | 15e7e4f | 2022-09-09 16:52:41 +0200 | [diff] [blame] | 372 | static int osmux_legacy_dummy_parse_cid(const struct osmo_sockaddr *rem_addr, struct msgb *msg, |
Pau Espin Pedrol | 662fa42 | 2018-10-16 15:54:38 +0200 | [diff] [blame] | 373 | uint8_t *osmux_cid) |
| 374 | { |
Pau Espin Pedrol | b5583cd | 2019-05-13 12:58:24 +0200 | [diff] [blame] | 375 | if (msg->len < 1 + sizeof(*osmux_cid)) { |
Pau Espin Pedrol | ec5e85b | 2022-09-09 16:04:19 +0200 | [diff] [blame] | 376 | LOGP(DOSMUX, LOGL_ERROR, |
Pau Espin Pedrol | b5583cd | 2019-05-13 12:58:24 +0200 | [diff] [blame] | 377 | "Discarding truncated Osmux dummy load: %s\n", osmo_hexdump(msg->data, msg->len)); |
Pau Espin Pedrol | 662fa42 | 2018-10-16 15:54:38 +0200 | [diff] [blame] | 378 | return -1; |
| 379 | } |
| 380 | |
| 381 | /* extract the osmux CID from the dummy message */ |
| 382 | memcpy(osmux_cid, &msg->data[1], sizeof(*osmux_cid)); |
| 383 | return 0; |
| 384 | } |
| 385 | |
Pau Espin Pedrol | 1442c5a | 2019-05-06 16:10:10 +0200 | [diff] [blame] | 386 | /* This is called from the bsc-nat */ |
Pau Espin Pedrol | 21bcf6a | 2022-09-22 18:23:04 +0200 | [diff] [blame] | 387 | static int osmux_handle_dummy(struct mgcp_trunk *trunk, const struct osmo_sockaddr *rem_addr, |
Pau Espin Pedrol | 1442c5a | 2019-05-06 16:10:10 +0200 | [diff] [blame] | 388 | struct msgb *msg) |
| 389 | { |
| 390 | uint8_t osmux_cid; |
| 391 | struct mgcp_conn_rtp *conn; |
| 392 | |
Pau Espin Pedrol | 15e7e4f | 2022-09-09 16:52:41 +0200 | [diff] [blame] | 393 | if (osmux_legacy_dummy_parse_cid(rem_addr, msg, &osmux_cid) < 0) |
Pau Espin Pedrol | 1442c5a | 2019-05-06 16:10:10 +0200 | [diff] [blame] | 394 | goto out; |
| 395 | |
Pau Espin Pedrol | 21bcf6a | 2022-09-22 18:23:04 +0200 | [diff] [blame] | 396 | conn = osmux_conn_lookup(trunk, osmux_cid, rem_addr); |
Pau Espin Pedrol | 1442c5a | 2019-05-06 16:10:10 +0200 | [diff] [blame] | 397 | if (!conn) { |
Pau Espin Pedrol | ec5e85b | 2022-09-09 16:04:19 +0200 | [diff] [blame] | 398 | LOGP(DOSMUX, LOGL_ERROR, |
Pau Espin Pedrol | 1442c5a | 2019-05-06 16:10:10 +0200 | [diff] [blame] | 399 | "Cannot find conn for Osmux CID %d\n", osmux_cid); |
| 400 | goto out; |
| 401 | } |
| 402 | |
| 403 | endp_osmux_state_check(conn->conn->endp, conn, false); |
| 404 | /* Only needed to punch hole in firewall, it can be dropped */ |
| 405 | out: |
| 406 | msgb_free(msg); |
| 407 | return 0; |
| 408 | } |
| 409 | |
Pau Espin Pedrol | 3fbf035 | 2022-09-22 19:01:33 +0200 | [diff] [blame] | 410 | #define osmux_chunk_length(msg, rem) ((rem) - (msg)->len) |
Pau Espin Pedrol | 1442c5a | 2019-05-06 16:10:10 +0200 | [diff] [blame] | 411 | static int osmux_read_fd_cb(struct osmo_fd *ofd, unsigned int what) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 412 | { |
| 413 | struct msgb *msg; |
| 414 | struct osmux_hdr *osmuxh; |
Pau Espin Pedrol | 15e7e4f | 2022-09-09 16:52:41 +0200 | [diff] [blame] | 415 | struct osmo_sockaddr rem_addr; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 416 | uint32_t rem; |
Pau Espin Pedrol | 582c2bf | 2022-09-22 17:53:44 +0200 | [diff] [blame] | 417 | struct mgcp_trunk *trunk = ofd->data; |
| 418 | struct rate_ctr_group *all_rtp_stats = trunk->ratectr.all_osmux_conn_stats; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 419 | |
Pau Espin Pedrol | 15e7e4f | 2022-09-09 16:52:41 +0200 | [diff] [blame] | 420 | msg = osmux_recv(ofd, &rem_addr); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 421 | if (!msg) |
| 422 | return -1; |
| 423 | |
Pau Espin Pedrol | 582c2bf | 2022-09-22 17:53:44 +0200 | [diff] [blame] | 424 | rate_ctr_inc(rate_ctr_group_get_ctr(all_rtp_stats, OSMUX_PACKETS_RX_CTR)); |
| 425 | |
Pau Espin Pedrol | 928a20b | 2022-09-23 15:38:24 +0200 | [diff] [blame] | 426 | if (trunk->cfg->osmux_use == OSMUX_USAGE_OFF) { |
Pau Espin Pedrol | ec5e85b | 2022-09-09 16:04:19 +0200 | [diff] [blame] | 427 | LOGP(DOSMUX, LOGL_ERROR, |
Pau Espin Pedrol | d14163e | 2018-10-16 15:48:59 +0200 | [diff] [blame] | 428 | "bsc-nat wants to use Osmux but bsc did not request it\n"); |
| 429 | goto out; |
| 430 | } |
| 431 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 432 | /* not any further processing dummy messages */ |
Philipp Maier | b3d14eb | 2021-05-20 14:18:52 +0200 | [diff] [blame] | 433 | if (mgcp_is_rtp_dummy_payload(msg)) |
Pau Espin Pedrol | 21bcf6a | 2022-09-22 18:23:04 +0200 | [diff] [blame] | 434 | return osmux_handle_dummy(trunk, &rem_addr, msg); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 435 | |
| 436 | rem = msg->len; |
| 437 | while((osmuxh = osmux_xfrm_output_pull(msg)) != NULL) { |
Pau Espin Pedrol | 582c2bf | 2022-09-22 17:53:44 +0200 | [diff] [blame] | 438 | struct mgcp_endpoint *endp; |
| 439 | struct mgcp_conn_rtp *conn_src; |
Pau Espin Pedrol | 21bcf6a | 2022-09-22 18:23:04 +0200 | [diff] [blame] | 440 | conn_src = osmux_conn_lookup(trunk, osmuxh->circuit_id, |
Pau Espin Pedrol | 15e7e4f | 2022-09-09 16:52:41 +0200 | [diff] [blame] | 441 | &rem_addr); |
Pau Espin Pedrol | 1442c5a | 2019-05-06 16:10:10 +0200 | [diff] [blame] | 442 | if (!conn_src) { |
Pau Espin Pedrol | ec5e85b | 2022-09-09 16:04:19 +0200 | [diff] [blame] | 443 | LOGP(DOSMUX, LOGL_ERROR, |
Pau Espin Pedrol | 1442c5a | 2019-05-06 16:10:10 +0200 | [diff] [blame] | 444 | "Cannot find a src conn for circuit_id=%d\n", |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 445 | osmuxh->circuit_id); |
| 446 | goto out; |
| 447 | } |
Pau Espin Pedrol | 582c2bf | 2022-09-22 17:53:44 +0200 | [diff] [blame] | 448 | endp = conn_src->conn->endp; |
Pau Espin Pedrol | de13311 | 2020-09-08 17:51:36 +0200 | [diff] [blame] | 449 | mgcp_conn_watchdog_kick(conn_src->conn); |
| 450 | |
Pau Espin Pedrol | 1442c5a | 2019-05-06 16:10:10 +0200 | [diff] [blame] | 451 | /*conn_dst = mgcp_find_dst_conn(conn_src->conn); |
| 452 | if (!conn_dst) { |
Pau Espin Pedrol | ec5e85b | 2022-09-09 16:04:19 +0200 | [diff] [blame] | 453 | LOGP(DOSMUX, LOGL_ERROR, |
Pau Espin Pedrol | 1442c5a | 2019-05-06 16:10:10 +0200 | [diff] [blame] | 454 | "Cannot find a dst conn for circuit_id=%d\n", |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 455 | osmuxh->circuit_id); |
| 456 | goto out; |
Pau Espin Pedrol | 1442c5a | 2019-05-06 16:10:10 +0200 | [diff] [blame] | 457 | }*/ |
Pau Espin Pedrol | 582c2bf | 2022-09-22 17:53:44 +0200 | [diff] [blame] | 458 | if (endp_osmux_state_check(endp, conn_src, false) == 0) { |
Pau Espin Pedrol | b4a5947 | 2022-09-23 18:08:53 +0200 | [diff] [blame] | 459 | rtpconn_osmux_rate_ctr_inc(conn_src, OSMUX_CHUNKS_RX_CTR); |
| 460 | rtpconn_osmux_rate_ctr_add(conn_src, OSMUX_OCTETS_RX_CTR, |
| 461 | osmux_chunk_length(msg, rem)); |
Pau Espin Pedrol | 3363916 | 2022-08-31 17:46:11 +0200 | [diff] [blame] | 462 | osmux_xfrm_output_sched(conn_src->osmux.out, osmuxh); |
Pau Espin Pedrol | de2a4d7 | 2018-10-16 16:37:43 +0200 | [diff] [blame] | 463 | } |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 464 | rem = msg->len; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 465 | } |
| 466 | out: |
| 467 | msgb_free(msg); |
| 468 | return 0; |
| 469 | } |
| 470 | |
Pau Espin Pedrol | 21bcf6a | 2022-09-22 18:23:04 +0200 | [diff] [blame] | 471 | int osmux_init(int role, struct mgcp_trunk *trunk) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 472 | { |
| 473 | int ret; |
Pau Espin Pedrol | 21bcf6a | 2022-09-22 18:23:04 +0200 | [diff] [blame] | 474 | struct mgcp_config *cfg = trunk->cfg; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 475 | |
Pau Espin Pedrol | 21bcf6a | 2022-09-22 18:23:04 +0200 | [diff] [blame] | 476 | /* So far we only support running on one trunk: */ |
| 477 | OSMO_ASSERT(trunk == mgcp_trunk_by_num(cfg, MGCP_TRUNK_VIRTUAL, MGCP_VIRT_TRUNK_ID)); |
| 478 | |
| 479 | osmo_fd_setup(&osmux_fd, -1, OSMO_FD_READ, osmux_read_fd_cb, trunk, 0); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 480 | |
Harald Welte | 55a9229 | 2021-04-28 19:06:34 +0200 | [diff] [blame] | 481 | ret = mgcp_create_bind(cfg->osmux_addr, &osmux_fd, cfg->osmux_port, |
| 482 | cfg->endp_dscp, cfg->endp_priority); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 483 | if (ret < 0) { |
Pau Espin Pedrol | ec5e85b | 2022-09-09 16:04:19 +0200 | [diff] [blame] | 484 | LOGP(DOSMUX, LOGL_ERROR, "cannot bind OSMUX socket to %s:%u\n", |
Pau Espin Pedrol | f027f17 | 2019-05-06 13:57:31 +0200 | [diff] [blame] | 485 | cfg->osmux_addr, cfg->osmux_port); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 486 | return ret; |
| 487 | } |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 488 | |
| 489 | ret = osmo_fd_register(&osmux_fd); |
| 490 | if (ret < 0) { |
Pau Espin Pedrol | ec5e85b | 2022-09-09 16:04:19 +0200 | [diff] [blame] | 491 | LOGP(DOSMUX, LOGL_ERROR, "cannot register OSMUX socket %s\n", |
Pau Espin Pedrol | f027f17 | 2019-05-06 13:57:31 +0200 | [diff] [blame] | 492 | osmo_sock_get_name2(osmux_fd.fd)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 493 | return ret; |
| 494 | } |
Pau Espin Pedrol | b7f52b4 | 2022-09-25 00:14:03 +0200 | [diff] [blame] | 495 | cfg->osmux_initialized = true; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 496 | |
Pau Espin Pedrol | ec5e85b | 2022-09-09 16:04:19 +0200 | [diff] [blame] | 497 | LOGP(DOSMUX, LOGL_INFO, "OSMUX socket listening on %s\n", |
Pau Espin Pedrol | f027f17 | 2019-05-06 13:57:31 +0200 | [diff] [blame] | 498 | osmo_sock_get_name2(osmux_fd.fd)); |
| 499 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 500 | return 0; |
| 501 | } |
| 502 | |
Pau Espin Pedrol | d48a811 | 2022-09-27 12:37:53 +0200 | [diff] [blame^] | 503 | /*! Initialize Osmux bits of a conn. |
| 504 | * \param[in] conn Osmux connection to initialize |
| 505 | * \returns 0 on success, negative on ERROR */ |
| 506 | int osmux_init_conn(struct mgcp_conn_rtp *conn) |
| 507 | { |
| 508 | if (conn_osmux_allocate_local_cid(conn) == -1) |
| 509 | return -1; |
| 510 | conn->osmux.ctrg = rate_ctr_group_alloc(conn->conn, &rate_ctr_group_osmux_desc, conn->ctrg->idx); |
| 511 | |
| 512 | conn->osmux.state = OSMUX_STATE_ACTIVATING; |
| 513 | return 0; |
| 514 | } |
| 515 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 516 | /*! enable OSXMUX circuit for a specified connection. |
| 517 | * \param[in] endp mgcp endpoint (configuration) |
| 518 | * \param[in] conn connection to disable |
Pau Espin Pedrol | 15e7e4f | 2022-09-09 16:52:41 +0200 | [diff] [blame] | 519 | * \param[in] addr IP address and port of remote OSMUX endpoint |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 520 | * \returns 0 on success, -1 on ERROR */ |
| 521 | int osmux_enable_conn(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn, |
Pau Espin Pedrol | 15e7e4f | 2022-09-09 16:52:41 +0200 | [diff] [blame] | 522 | const struct osmo_sockaddr *rem_addr) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 523 | { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 524 | /*! If osmux is enabled, initialize the output handler. This handler is |
| 525 | * used to reconstruct the RTP flow from osmux. The RTP SSRC is |
| 526 | * allocated based on the circuit ID (conn_net->osmux.cid), which is unique |
| 527 | * in the local scope to the BSC/BSC-NAT. We use it to divide the RTP |
Pau Espin Pedrol | 17bf603 | 2018-09-17 13:38:39 +0200 | [diff] [blame] | 528 | * SSRC space (2^32) by the OSMUX_CID_MAX + 1 possible circuit IDs, then randomly |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 529 | * select one value from that window. Thus, we have no chance to have |
| 530 | * overlapping RTP SSRC traveling to the BTSes behind the BSC, |
| 531 | * similarly, for flows traveling to the MSC. |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 532 | */ |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 533 | struct in6_addr addr_unset = {}; |
Pau Espin Pedrol | 17bf603 | 2018-09-17 13:38:39 +0200 | [diff] [blame] | 534 | static const uint32_t rtp_ssrc_winlen = UINT32_MAX / (OSMUX_CID_MAX + 1); |
Eric | fbf78d1 | 2021-08-23 22:31:39 +0200 | [diff] [blame] | 535 | uint16_t osmux_dummy = endp->trunk->cfg->osmux_dummy; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 536 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 537 | /* Check if osmux is enabled for the specified connection */ |
Pau Espin Pedrol | 48aff62 | 2018-10-16 16:03:57 +0200 | [diff] [blame] | 538 | if (conn->osmux.state != OSMUX_STATE_ACTIVATING) { |
Pau Espin Pedrol | ec5e85b | 2022-09-09 16:04:19 +0200 | [diff] [blame] | 539 | LOGPCONN(conn->conn, DOSMUX, LOGL_ERROR, |
Pau Espin Pedrol | c9a6280 | 2019-05-13 13:20:13 +0200 | [diff] [blame] | 540 | "conn:%s didn't negotiate Osmux, state %d\n", |
| 541 | mgcp_conn_dump(conn->conn), conn->osmux.state); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 542 | return -1; |
| 543 | } |
| 544 | |
Pau Espin Pedrol | dac2ca7 | 2019-05-13 17:34:51 +0200 | [diff] [blame] | 545 | /* Wait until we have the connection information from MDCX */ |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 546 | if (memcmp(&conn->end.addr, &addr_unset, |
| 547 | conn->end.addr.u.sa.sa_family == AF_INET6 ? |
| 548 | sizeof(struct in6_addr) : |
| 549 | sizeof(struct in_addr)) == 0) { |
Pau Espin Pedrol | ec5e85b | 2022-09-09 16:04:19 +0200 | [diff] [blame] | 550 | LOGPCONN(conn->conn, DOSMUX, LOGL_INFO, |
Pau Espin Pedrol | dac2ca7 | 2019-05-13 17:34:51 +0200 | [diff] [blame] | 551 | "Osmux remote address/port still unknown\n"); |
| 552 | return -1; |
| 553 | } |
| 554 | |
Pau Espin Pedrol | 15e7e4f | 2022-09-09 16:52:41 +0200 | [diff] [blame] | 555 | conn->osmux.in = osmux_handle_find_or_create(conn, rem_addr); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 556 | if (!conn->osmux.in) { |
Pau Espin Pedrol | ec5e85b | 2022-09-09 16:04:19 +0200 | [diff] [blame] | 557 | LOGPCONN(conn->conn, DOSMUX, LOGL_ERROR, |
Pau Espin Pedrol | c9a6280 | 2019-05-13 13:20:13 +0200 | [diff] [blame] | 558 | "Cannot allocate input osmux handle for conn:%s\n", |
| 559 | mgcp_conn_dump(conn->conn)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 560 | return -1; |
| 561 | } |
Pau Espin Pedrol | 2177919 | 2022-09-23 16:46:33 +0200 | [diff] [blame] | 562 | if (osmux_xfrm_input_open_circuit(conn->osmux.in, conn->osmux.remote_cid, osmux_dummy) < 0) { |
Pau Espin Pedrol | ec5e85b | 2022-09-09 16:04:19 +0200 | [diff] [blame] | 563 | LOGPCONN(conn->conn, DOSMUX, LOGL_ERROR, |
Pau Espin Pedrol | c9a6280 | 2019-05-13 13:20:13 +0200 | [diff] [blame] | 564 | "Cannot open osmux circuit %u for conn:%s\n", |
Pau Espin Pedrol | 2177919 | 2022-09-23 16:46:33 +0200 | [diff] [blame] | 565 | conn->osmux.remote_cid, mgcp_conn_dump(conn->conn)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 566 | return -1; |
| 567 | } |
| 568 | |
Pau Espin Pedrol | 941e317 | 2022-09-22 21:16:56 +0200 | [diff] [blame] | 569 | conn->osmux.out = osmux_xfrm_output_alloc(conn->conn); |
Pau Espin Pedrol | 3363916 | 2022-08-31 17:46:11 +0200 | [diff] [blame] | 570 | osmux_xfrm_output_set_rtp_ssrc(conn->osmux.out, |
Pau Espin Pedrol | 2177919 | 2022-09-23 16:46:33 +0200 | [diff] [blame] | 571 | (conn->osmux.remote_cid * rtp_ssrc_winlen) + |
Pau Espin Pedrol | 3363916 | 2022-08-31 17:46:11 +0200 | [diff] [blame] | 572 | (random() % rtp_ssrc_winlen)); |
| 573 | osmux_xfrm_output_set_rtp_pl_type(conn->osmux.out, conn->end.codec->payload_type); |
| 574 | osmux_xfrm_output_set_tx_cb(conn->osmux.out, |
Pau Espin Pedrol | 1442c5a | 2019-05-06 16:10:10 +0200 | [diff] [blame] | 575 | scheduled_from_osmux_tx_rtp_cb, conn); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 576 | |
| 577 | conn->osmux.state = OSMUX_STATE_ENABLED; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 578 | |
| 579 | return 0; |
| 580 | } |
| 581 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 582 | /*! disable OSXMUX circuit for a specified connection. |
| 583 | * \param[in] conn connection to disable */ |
Pau Espin Pedrol | 85978da | 2019-05-17 14:38:19 +0200 | [diff] [blame] | 584 | void conn_osmux_disable(struct mgcp_conn_rtp *conn) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 585 | { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 586 | |
Pau Espin Pedrol | 85978da | 2019-05-17 14:38:19 +0200 | [diff] [blame] | 587 | OSMO_ASSERT(conn->osmux.state != OSMUX_STATE_DISABLED); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 588 | |
Pau Espin Pedrol | ec5e85b | 2022-09-09 16:04:19 +0200 | [diff] [blame] | 589 | LOGPCONN(conn->conn, DOSMUX, LOGL_INFO, |
Pau Espin Pedrol | 2177919 | 2022-09-23 16:46:33 +0200 | [diff] [blame] | 590 | "Releasing connection using local Osmux CID %u\n", conn->osmux.local_cid); |
Pau Espin Pedrol | f2321b7 | 2018-05-17 13:55:14 +0200 | [diff] [blame] | 591 | |
Pau Espin Pedrol | 582c2bf | 2022-09-22 17:53:44 +0200 | [diff] [blame] | 592 | struct rate_ctr_group *all_osmux_stats = conn->conn->endp->trunk->ratectr.all_osmux_conn_stats; |
| 593 | rate_ctr_inc(rate_ctr_group_get_ctr(all_osmux_stats, OSMUX_NUM_CONNECTIONS)); |
| 594 | |
Pau Espin Pedrol | 85978da | 2019-05-17 14:38:19 +0200 | [diff] [blame] | 595 | if (conn->osmux.state == OSMUX_STATE_ENABLED) { |
| 596 | /* We are closing, we don't need pending RTP packets to be transmitted */ |
Pau Espin Pedrol | 3363916 | 2022-08-31 17:46:11 +0200 | [diff] [blame] | 597 | osmux_xfrm_output_set_tx_cb(conn->osmux.out, NULL, NULL); |
| 598 | TALLOC_FREE(conn->osmux.out); |
Pau Espin Pedrol | f2321b7 | 2018-05-17 13:55:14 +0200 | [diff] [blame] | 599 | |
Pau Espin Pedrol | 2177919 | 2022-09-23 16:46:33 +0200 | [diff] [blame] | 600 | osmux_xfrm_input_close_circuit(conn->osmux.in, conn->osmux.remote_cid); |
Pau Espin Pedrol | 85978da | 2019-05-17 14:38:19 +0200 | [diff] [blame] | 601 | conn->osmux.state = OSMUX_STATE_DISABLED; |
Pau Espin Pedrol | 85978da | 2019-05-17 14:38:19 +0200 | [diff] [blame] | 602 | osmux_handle_put(conn->osmux.in); |
Pau Espin Pedrol | 2177919 | 2022-09-23 16:46:33 +0200 | [diff] [blame] | 603 | conn->osmux.remote_cid = 0; |
| 604 | conn->osmux.remote_cid_present = false; |
Pau Espin Pedrol | 85978da | 2019-05-17 14:38:19 +0200 | [diff] [blame] | 605 | } |
Pau Espin Pedrol | d48a811 | 2022-09-27 12:37:53 +0200 | [diff] [blame^] | 606 | |
Pau Espin Pedrol | 2177919 | 2022-09-23 16:46:33 +0200 | [diff] [blame] | 607 | conn_osmux_release_local_cid(conn); |
Pau Espin Pedrol | d48a811 | 2022-09-27 12:37:53 +0200 | [diff] [blame^] | 608 | |
| 609 | rate_ctr_group_free(conn->osmux.ctrg); |
| 610 | conn->osmux.ctrg = NULL; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 611 | } |
| 612 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 613 | /*! relase OSXMUX cid, that had been allocated to this connection. |
| 614 | * \param[in] conn connection with OSMUX cid to release */ |
Pau Espin Pedrol | 2177919 | 2022-09-23 16:46:33 +0200 | [diff] [blame] | 615 | void conn_osmux_release_local_cid(struct mgcp_conn_rtp *conn) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 616 | { |
Pau Espin Pedrol | 2177919 | 2022-09-23 16:46:33 +0200 | [diff] [blame] | 617 | if (conn->osmux.local_cid_allocated) |
| 618 | osmux_cid_pool_put(conn->osmux.local_cid); |
| 619 | conn->osmux.local_cid = 0; |
| 620 | conn->osmux.local_cid_allocated = false; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 621 | } |
| 622 | |
Pau Espin Pedrol | 2177919 | 2022-09-23 16:46:33 +0200 | [diff] [blame] | 623 | /*! allocate local OSMUX cid to connection. |
| 624 | * \param[in] conn connection for which we allocate the local OSMUX cid |
| 625 | * \returns Allocated OSMUX cid, -1 on error (no free CIDs avail). |
Pau Espin Pedrol | 8de58e7 | 2019-04-24 13:33:46 +0200 | [diff] [blame] | 626 | */ |
Pau Espin Pedrol | 2177919 | 2022-09-23 16:46:33 +0200 | [diff] [blame] | 627 | int conn_osmux_allocate_local_cid(struct mgcp_conn_rtp *conn) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 628 | { |
Pau Espin Pedrol | 2177919 | 2022-09-23 16:46:33 +0200 | [diff] [blame] | 629 | OSMO_ASSERT(conn->osmux.local_cid_allocated == false); |
| 630 | int osmux_cid = osmux_cid_pool_get_next(); |
| 631 | if (osmux_cid == -1) { |
Pau Espin Pedrol | ec5e85b | 2022-09-09 16:04:19 +0200 | [diff] [blame] | 632 | LOGPCONN(conn->conn, DOSMUX, LOGL_INFO, |
Pau Espin Pedrol | 2177919 | 2022-09-23 16:46:33 +0200 | [diff] [blame] | 633 | "no available local Osmux CID to allocate!\n"); |
Pau Espin Pedrol | 8de58e7 | 2019-04-24 13:33:46 +0200 | [diff] [blame] | 634 | return -1; |
| 635 | } |
| 636 | |
Pau Espin Pedrol | 2177919 | 2022-09-23 16:46:33 +0200 | [diff] [blame] | 637 | conn->osmux.local_cid = (uint8_t) osmux_cid; |
| 638 | conn->osmux.local_cid_allocated = true; |
Pau Espin Pedrol | fa810e8 | 2019-05-06 18:54:10 +0200 | [diff] [blame] | 639 | conn->type = MGCP_OSMUX_BSC; |
Pau Espin Pedrol | 8de58e7 | 2019-04-24 13:33:46 +0200 | [diff] [blame] | 640 | return osmux_cid; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 641 | } |
| 642 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 643 | /*! send RTP dummy packet to OSMUX connection port. |
| 644 | * \param[in] endp mcgp endpoint that holds the RTP connection |
| 645 | * \param[in] conn associated RTP connection |
| 646 | * \returns bytes sent, -1 on error */ |
| 647 | int osmux_send_dummy(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 648 | { |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 649 | char ipbuf[INET6_ADDRSTRLEN]; |
Pau Espin Pedrol | c1ad553 | 2019-05-15 23:00:53 +0200 | [diff] [blame] | 650 | struct osmux_hdr *osmuxh; |
| 651 | int buf_len; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 652 | struct in_addr addr_unset = {}; |
| 653 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 654 | /*! The dummy packet will not be sent via the actual OSMUX connection, |
| 655 | * instead it is sent out of band to port where the remote OSMUX |
| 656 | * multplexer is listening. The goal is to ensure that the connection |
| 657 | * is kept open */ |
| 658 | |
| 659 | /*! We don't need to send the dummy load for osmux so often as another |
| 660 | * endpoint may have already punched the hole in the firewall. This |
| 661 | * approach is simple though. */ |
| 662 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 663 | /* Wait until we have the connection information from MDCX */ |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 664 | if (memcmp(&conn->end.addr, &addr_unset, sizeof(addr_unset)) == 0) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 665 | return 0; |
| 666 | |
Pau Espin Pedrol | de2a4d7 | 2018-10-16 16:37:43 +0200 | [diff] [blame] | 667 | if (endp_osmux_state_check(endp, conn, true) < 0) |
| 668 | return 0; |
| 669 | |
Pau Espin Pedrol | c1ad553 | 2019-05-15 23:00:53 +0200 | [diff] [blame] | 670 | buf_len = sizeof(struct osmux_hdr) + osmo_amr_bytes(AMR_FT_0); |
| 671 | osmuxh = (struct osmux_hdr *) alloca(buf_len); |
| 672 | memset(osmuxh, 0, buf_len); |
| 673 | osmuxh->ft = OSMUX_FT_DUMMY; |
| 674 | osmuxh->amr_ft = AMR_FT_0; |
Pau Espin Pedrol | 2177919 | 2022-09-23 16:46:33 +0200 | [diff] [blame] | 675 | osmuxh->circuit_id = conn->osmux.remote_cid; |
Pau Espin Pedrol | c1ad553 | 2019-05-15 23:00:53 +0200 | [diff] [blame] | 676 | |
Pau Espin Pedrol | ec5e85b | 2022-09-09 16:04:19 +0200 | [diff] [blame] | 677 | LOGPCONN(conn->conn, DOSMUX, LOGL_DEBUG, |
Pau Espin Pedrol | 3752522 | 2019-05-13 17:34:14 +0200 | [diff] [blame] | 678 | "sending OSMUX dummy load to %s:%u CID %u\n", |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 679 | osmo_sockaddr_ntop(&conn->end.addr.u.sa, ipbuf), |
Pau Espin Pedrol | 2177919 | 2022-09-23 16:46:33 +0200 | [diff] [blame] | 680 | ntohs(conn->end.rtp_port), conn->osmux.remote_cid); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 681 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 682 | return mgcp_udp_send(osmux_fd.fd, &conn->end.addr, |
Pau Espin Pedrol | c1ad553 | 2019-05-15 23:00:53 +0200 | [diff] [blame] | 683 | conn->end.rtp_port, (char*)osmuxh, buf_len); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 684 | } |
| 685 | |
Pau Espin Pedrol | bcd52e5 | 2018-09-17 12:41:28 +0200 | [diff] [blame] | 686 | /* bsc-nat allocates/releases the Osmux circuit ID. +7 to round up to 8 bit boundary. */ |
| 687 | static uint8_t osmux_cid_bitmap[(OSMUX_CID_MAX + 1 + 7) / 8]; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 688 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 689 | /*! count the number of taken OSMUX cids. |
| 690 | * \returns number of OSMUX cids in use */ |
Pau Espin Pedrol | 8de58e7 | 2019-04-24 13:33:46 +0200 | [diff] [blame] | 691 | int osmux_cid_pool_count_used(void) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 692 | { |
| 693 | int i, j, used = 0; |
| 694 | |
| 695 | for (i = 0; i < sizeof(osmux_cid_bitmap); i++) { |
| 696 | for (j = 0; j < 8; j++) { |
| 697 | if (osmux_cid_bitmap[i] & (1 << j)) |
| 698 | used += 1; |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | return used; |
| 703 | } |
| 704 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 705 | /*! take a free OSMUX cid. |
| 706 | * \returns OSMUX cid */ |
Pau Espin Pedrol | 8de58e7 | 2019-04-24 13:33:46 +0200 | [diff] [blame] | 707 | int osmux_cid_pool_get_next(void) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 708 | { |
| 709 | int i, j; |
| 710 | |
| 711 | for (i = 0; i < sizeof(osmux_cid_bitmap); i++) { |
| 712 | for (j = 0; j < 8; j++) { |
| 713 | if (osmux_cid_bitmap[i] & (1 << j)) |
| 714 | continue; |
| 715 | |
| 716 | osmux_cid_bitmap[i] |= (1 << j); |
Pau Espin Pedrol | ec5e85b | 2022-09-09 16:04:19 +0200 | [diff] [blame] | 717 | LOGP(DOSMUX, LOGL_DEBUG, |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 718 | "Allocating Osmux CID %u from pool\n", (i * 8) + j); |
| 719 | return (i * 8) + j; |
| 720 | } |
| 721 | } |
| 722 | |
Pau Espin Pedrol | ec5e85b | 2022-09-09 16:04:19 +0200 | [diff] [blame] | 723 | LOGP(DOSMUX, LOGL_ERROR, "All Osmux circuits are in use!\n"); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 724 | return -1; |
| 725 | } |
| 726 | |
Pau Espin Pedrol | 8de58e7 | 2019-04-24 13:33:46 +0200 | [diff] [blame] | 727 | /*! take a specific OSMUX cid. |
| 728 | * \param[in] osmux_cid OSMUX cid */ |
| 729 | void osmux_cid_pool_get(uint8_t osmux_cid) |
| 730 | { |
Pau Espin Pedrol | ec5e85b | 2022-09-09 16:04:19 +0200 | [diff] [blame] | 731 | LOGP(DOSMUX, LOGL_DEBUG, "Allocating Osmux CID %u from pool\n", osmux_cid); |
Pau Espin Pedrol | 8de58e7 | 2019-04-24 13:33:46 +0200 | [diff] [blame] | 732 | osmux_cid_bitmap[osmux_cid / 8] |= (1 << (osmux_cid % 8)); |
| 733 | } |
| 734 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 735 | /*! put back a no longer used OSMUX cid. |
| 736 | * \param[in] osmux_cid OSMUX cid */ |
Pau Espin Pedrol | 8de58e7 | 2019-04-24 13:33:46 +0200 | [diff] [blame] | 737 | void osmux_cid_pool_put(uint8_t osmux_cid) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 738 | { |
Pau Espin Pedrol | ec5e85b | 2022-09-09 16:04:19 +0200 | [diff] [blame] | 739 | LOGP(DOSMUX, LOGL_DEBUG, "Osmux CID %u is back to the pool\n", osmux_cid); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 740 | osmux_cid_bitmap[osmux_cid / 8] &= ~(1 << (osmux_cid % 8)); |
| 741 | } |
Pau Espin Pedrol | 8de58e7 | 2019-04-24 13:33:46 +0200 | [diff] [blame] | 742 | |
| 743 | /*! check if OSMUX cid is already taken */ |
| 744 | bool osmux_cid_pool_allocated(uint8_t osmux_cid) |
| 745 | { |
| 746 | return !!(osmux_cid_bitmap[osmux_cid / 8] & (1 << (osmux_cid % 8))); |
| 747 | } |