Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1 | /* Message connection list handling */ |
| 2 | |
| 3 | /* |
| 4 | * (C) 2017 by sysmocom s.f.m.c. GmbH <info@sysmocom.de> |
| 5 | * All Rights Reserved |
| 6 | * |
| 7 | * Author: Philipp Maier |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU Affero General Public License as published by |
| 11 | * the Free Software Foundation; either version 3 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU Affero General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU Affero General Public License |
| 20 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | #pragma once |
| 25 | |
Philipp Maier | 993ea6b | 2020-08-04 18:26:50 +0200 | [diff] [blame] | 26 | #include <osmocom/mgcp/mgcp.h> |
| 27 | #include <osmocom/mgcp/mgcp_network.h> |
| 28 | #include <osmocom/mgcp/osmux.h> |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 29 | #include <osmocom/core/linuxlist.h> |
Stefan Sperling | ba25eab | 2018-10-30 14:32:31 +0100 | [diff] [blame] | 30 | #include <osmocom/core/rate_ctr.h> |
Pau Espin Pedrol | bb3ccde | 2021-12-23 19:49:26 +0100 | [diff] [blame] | 31 | #include <osmocom/gsm/iuup.h> |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 32 | #include <inttypes.h> |
| 33 | |
Philipp Maier | 62612e8 | 2020-05-27 16:29:22 +0200 | [diff] [blame] | 34 | #define LOGPCONN(conn, cat, level, fmt, args...) \ |
| 35 | LOGPENDP((conn)->endp, cat, level, "CI:%s " fmt, \ |
| 36 | (conn)->id, \ |
| 37 | ## args) |
| 38 | |
Philipp Maier | 993ea6b | 2020-08-04 18:26:50 +0200 | [diff] [blame] | 39 | #define LOG_CONN(conn, level, fmt, args...) \ |
| 40 | LOGP(DRTP, level, "(%s I:%s) " fmt, \ |
| 41 | (conn)->endp ? (conn)->endp->name : "none", (conn)->id, ## args) |
| 42 | |
| 43 | #define LOG_CONN_RTP(conn_rtp, level, fmt, args...) \ |
| 44 | LOG_CONN((conn_rtp)->conn, level, fmt, ## args) |
| 45 | |
| 46 | /* Specific rtp connection type (see struct mgcp_conn_rtp) */ |
| 47 | enum mgcp_conn_rtp_type { |
| 48 | MGCP_RTP_DEFAULT = 0, |
Pau Espin Pedrol | 9d939b6 | 2022-10-03 16:59:20 +0200 | [diff] [blame] | 49 | MGCP_RTP_OSMUX, |
Pau Espin Pedrol | bb3ccde | 2021-12-23 19:49:26 +0100 | [diff] [blame] | 50 | MGCP_RTP_IUUP, |
Philipp Maier | 993ea6b | 2020-08-04 18:26:50 +0200 | [diff] [blame] | 51 | }; |
| 52 | |
Philipp Maier | b0c05aa | 2020-07-06 11:52:19 +0200 | [diff] [blame] | 53 | /*! Connection type, specifies which member of the union "u" in mgcp_conn |
| 54 | * contains a useful connection description (currently only RTP) */ |
| 55 | enum mgcp_conn_type { |
| 56 | MGCP_CONN_TYPE_RTP, |
| 57 | }; |
| 58 | |
Philipp Maier | 993ea6b | 2020-08-04 18:26:50 +0200 | [diff] [blame] | 59 | /* MGCP connection (RTP) */ |
| 60 | struct mgcp_conn_rtp { |
| 61 | |
| 62 | /* Backpointer to conn struct */ |
| 63 | struct mgcp_conn *conn; |
| 64 | |
| 65 | /* Specific connection type */ |
| 66 | enum mgcp_conn_rtp_type type; |
| 67 | |
| 68 | /* Port status */ |
| 69 | struct mgcp_rtp_end end; |
| 70 | |
| 71 | /* Sequence bits */ |
| 72 | struct mgcp_rtp_state state; |
| 73 | |
| 74 | /* taps for the rtp connection; one per direction */ |
| 75 | struct mgcp_rtp_tap tap_in; |
| 76 | struct mgcp_rtp_tap tap_out; |
| 77 | |
| 78 | /* Osmux states (optional) */ |
| 79 | struct { |
| 80 | /* Osmux state: disabled, activating, active */ |
| 81 | enum osmux_state state; |
Pau Espin Pedrol | 2177919 | 2022-09-23 16:46:33 +0200 | [diff] [blame] | 82 | /* Is local_cid holding valid data? is it allocated from pool? */ |
| 83 | bool local_cid_allocated; |
| 84 | /* Allocated local Osmux circuit ID for this conn */ |
| 85 | uint8_t local_cid; |
| 86 | /* Is remote_cid holding valid data? was it already received from client? */ |
| 87 | bool remote_cid_present; |
| 88 | /* Received remote Osmux circuit ID for this conn */ |
| 89 | uint8_t remote_cid; |
Pau Espin Pedrol | 3363916 | 2022-08-31 17:46:11 +0200 | [diff] [blame] | 90 | /* handle to batch messages, shared (refcounted) among several conns */ |
Philipp Maier | 993ea6b | 2020-08-04 18:26:50 +0200 | [diff] [blame] | 91 | struct osmux_in_handle *in; |
Pau Espin Pedrol | 3363916 | 2022-08-31 17:46:11 +0200 | [diff] [blame] | 92 | /* handle to unbatch messages, one allocated and owned per conn */ |
| 93 | struct osmux_out_handle *out; |
Pau Espin Pedrol | 582c2bf | 2022-09-22 17:53:44 +0200 | [diff] [blame] | 94 | /* statistics: */ |
| 95 | struct rate_ctr_group *ctrg; |
Philipp Maier | 993ea6b | 2020-08-04 18:26:50 +0200 | [diff] [blame] | 96 | } osmux; |
| 97 | |
Pau Espin Pedrol | bb3ccde | 2021-12-23 19:49:26 +0100 | [diff] [blame] | 98 | struct { |
| 99 | struct osmo_iuup_instance *iui; |
| 100 | bool active_init; /* true: Send IuUP Init */ |
Pau Espin Pedrol | 452f2ba | 2022-05-24 19:15:54 +0200 | [diff] [blame] | 101 | int rfci_id_no_data; /* RFCI Id for RFCI NO_DATA (-1 if not available) */ |
Pau Espin Pedrol | bb3ccde | 2021-12-23 19:49:26 +0100 | [diff] [blame] | 102 | bool configured; |
| 103 | struct osmo_iuup_rnl_prim *init_ind; |
| 104 | } iuup; |
| 105 | |
Pau Espin Pedrol | daf5bce | 2022-09-22 19:14:24 +0200 | [diff] [blame] | 106 | struct rate_ctr_group *ctrg; |
Philipp Maier | 993ea6b | 2020-08-04 18:26:50 +0200 | [diff] [blame] | 107 | }; |
| 108 | |
Philipp Maier | b0c05aa | 2020-07-06 11:52:19 +0200 | [diff] [blame] | 109 | /*! MGCP connection (untyped) */ |
| 110 | struct mgcp_conn { |
| 111 | /*! list head */ |
| 112 | struct llist_head entry; |
| 113 | |
| 114 | /*! Backpointer to the endpoint where the conn belongs to */ |
| 115 | struct mgcp_endpoint *endp; |
| 116 | |
| 117 | /*! type of the connection (union) */ |
| 118 | enum mgcp_conn_type type; |
| 119 | |
| 120 | /*! mode of the connection */ |
| 121 | enum mgcp_connection_mode mode; |
| 122 | |
| 123 | /*! copy of the mode to restore the original setting (VTY) */ |
| 124 | enum mgcp_connection_mode mode_orig; |
| 125 | |
| 126 | /*! connection id to identify the connection */ |
| 127 | char id[MGCP_CONN_ID_MAXLEN]; |
| 128 | |
| 129 | /*! human readable name (vty, logging) */ |
| 130 | char name[256]; |
| 131 | |
| 132 | /*! activity tracker (for cleaning up inactive connections) */ |
| 133 | struct osmo_timer_list watchdog; |
| 134 | |
| 135 | /*! union with connection description */ |
| 136 | union { |
| 137 | struct mgcp_conn_rtp rtp; |
| 138 | } u; |
| 139 | |
| 140 | /*! pointer to optional private data */ |
| 141 | void *priv; |
| 142 | }; |
| 143 | |
Philipp Maier | cede2a4 | 2018-07-03 14:14:21 +0200 | [diff] [blame] | 144 | /* RTP connection related counters */ |
| 145 | enum { |
| 146 | IN_STREAM_ERR_TSTMP_CTR, |
| 147 | OUT_STREAM_ERR_TSTMP_CTR, |
Pau Espin Pedrol | 26e810d | 2022-09-22 17:16:20 +0200 | [diff] [blame] | 148 | RTP_PACKETS_RX_CTR, |
| 149 | RTP_OCTETS_RX_CTR, |
| 150 | RTP_PACKETS_TX_CTR, |
| 151 | RTP_OCTETS_TX_CTR, |
| 152 | RTP_DROPPED_PACKETS_CTR, |
| 153 | RTP_NUM_CONNECTIONS, |
Stefan Sperling | ba25eab | 2018-10-30 14:32:31 +0100 | [diff] [blame] | 154 | }; |
| 155 | |
| 156 | /* RTP per-connection statistics. Instances of the corresponding rate counter group |
| 157 | * exist for the lifetime of an RTP connection. |
| 158 | * Must be kept in sync with all_rtp_conn_rate_ctr_desc below */ |
| 159 | static const struct rate_ctr_desc mgcp_conn_rate_ctr_desc[] = { |
| 160 | [IN_STREAM_ERR_TSTMP_CTR] = {"stream_err_tstmp:in", "Inbound rtp-stream timestamp errors."}, |
| 161 | [OUT_STREAM_ERR_TSTMP_CTR] = {"stream_err_tstmp:out", "Outbound rtp-stream timestamp errors."}, |
| 162 | [RTP_PACKETS_RX_CTR] = {"rtp:packets_rx", "Inbound rtp packets."}, |
| 163 | [RTP_OCTETS_RX_CTR] = {"rtp:octets_rx", "Inbound rtp octets."}, |
| 164 | [RTP_PACKETS_TX_CTR] = {"rtp:packets_tx", "Outbound rtp packets."}, |
| 165 | [RTP_OCTETS_TX_CTR] = {"rtp:octets_tx", "Outbound rtp octets."}, |
Pau Espin Pedrol | a29f155 | 2022-09-22 19:06:06 +0200 | [diff] [blame] | 166 | [RTP_DROPPED_PACKETS_CTR] = {"rtp:dropped", "Dropped rtp packets."} |
Stefan Sperling | ba25eab | 2018-10-30 14:32:31 +0100 | [diff] [blame] | 167 | }; |
| 168 | |
| 169 | /* Aggregated RTP connection stats. These are updated when an RTP connection is freed. |
| 170 | * Must be kept in sync with mgcp_conn_rate_ctr_desc above */ |
| 171 | static const struct rate_ctr_desc all_rtp_conn_rate_ctr_desc[] = { |
| 172 | [IN_STREAM_ERR_TSTMP_CTR] = {"all_rtp:err_tstmp_in", "Total inbound rtp-stream timestamp errors."}, |
| 173 | [OUT_STREAM_ERR_TSTMP_CTR] = {"all_rtp:err_tstmp_out", "Total outbound rtp-stream timestamp errors."}, |
| 174 | [RTP_PACKETS_RX_CTR] = {"all_rtp:packets_rx", "Total inbound rtp packets."}, |
| 175 | [RTP_OCTETS_RX_CTR] = {"all_rtp:octets_rx", "Total inbound rtp octets."}, |
| 176 | [RTP_PACKETS_TX_CTR] = {"all_rtp:packets_tx", "Total outbound rtp packets."}, |
| 177 | [RTP_OCTETS_TX_CTR] = {"all_rtp:octets_tx", "Total outbound rtp octets."}, |
| 178 | [RTP_DROPPED_PACKETS_CTR] = {"all_rtp:dropped", "Total dropped rtp packets."}, |
| 179 | |
| 180 | /* This last counter does not exist in per-connection stats, only here. */ |
| 181 | [RTP_NUM_CONNECTIONS] = {"all_rtp:num_closed_conns", "Total number of rtp connections closed."} |
Philipp Maier | cede2a4 | 2018-07-03 14:14:21 +0200 | [diff] [blame] | 182 | }; |
| 183 | |
Pau Espin Pedrol | 582c2bf | 2022-09-22 17:53:44 +0200 | [diff] [blame] | 184 | /* Osmux connection related counters */ |
| 185 | enum { |
| 186 | OSMUX_CHUNKS_RX_CTR, |
| 187 | OSMUX_OCTETS_RX_CTR, |
Pau Espin Pedrol | 432ee9d | 2022-09-23 18:18:02 +0200 | [diff] [blame] | 188 | OSMUX_RTP_PACKETS_TX_CTR, |
| 189 | OSMUX_RTP_PACKETS_TX_DROPPED_CTR, |
| 190 | OSMUX_AMR_OCTETS_TX_CTR, |
Pau Espin Pedrol | 582c2bf | 2022-09-22 17:53:44 +0200 | [diff] [blame] | 191 | /* Only available in global stats: */ |
| 192 | OSMUX_NUM_CONNECTIONS, |
| 193 | OSMUX_PACKETS_RX_CTR, |
| 194 | OSMUX_PACKETS_TX_CTR, |
| 195 | OSMUX_DROPPED_PACKETS_CTR, |
| 196 | }; |
| 197 | |
| 198 | /* RTP per-connection statistics. Instances of the corresponding rate counter group |
| 199 | * exist for the lifetime of an RTP connection. |
| 200 | * Must be kept in sync with all_rtp_conn_rate_ctr_desc below */ |
| 201 | static const struct rate_ctr_desc mgcp_conn_osmux_rate_ctr_desc[] = { |
| 202 | [OSMUX_CHUNKS_RX_CTR] = {"osmux:chunks_rx", "Inbound Osmux chunks."}, |
| 203 | [OSMUX_OCTETS_RX_CTR] = {"osmux:octets_rx", "Inbound Osmux octets."}, |
Pau Espin Pedrol | 432ee9d | 2022-09-23 18:18:02 +0200 | [diff] [blame] | 204 | [OSMUX_RTP_PACKETS_TX_CTR] = {"osmux:rtp_packets_tx", "Tx outbound RTP packets to encode as Osmux."}, |
| 205 | [OSMUX_RTP_PACKETS_TX_DROPPED_CTR] = {"osmux:rtp_packets_tx_dropped", "Dropped Tx outbound RTP packets to encode as Osmux."}, |
| 206 | [OSMUX_AMR_OCTETS_TX_CTR] = {"osmux:amr_octets_tx", "Tx outbound AMD payload octets."}, |
Pau Espin Pedrol | 582c2bf | 2022-09-22 17:53:44 +0200 | [diff] [blame] | 207 | }; |
| 208 | |
| 209 | /* Aggregated Osmux connection stats. These are updated when an Osmux connection is freed. |
| 210 | * Must be kept in sync with mgcp_conn_osmux_rate_ctr_desc above */ |
| 211 | static const struct rate_ctr_desc all_osmux_conn_rate_ctr_desc[] = { |
| 212 | [OSMUX_CHUNKS_RX_CTR] = {"all_osmux:chunks_rx", "Inbound Osmux chunks."}, |
| 213 | [OSMUX_OCTETS_RX_CTR] = {"all_osmux:octets_rx", "Inbound Osmux octets."}, |
Pau Espin Pedrol | 432ee9d | 2022-09-23 18:18:02 +0200 | [diff] [blame] | 214 | [OSMUX_RTP_PACKETS_TX_CTR] = {"all_osmux:rtp_packets_tx", "Tx outbound RTP packets to encode as Osmux."}, |
| 215 | [OSMUX_RTP_PACKETS_TX_DROPPED_CTR] = {"all_osmux:rtp_packets_tx_dropped", "Dropped Tx outbound RTP packets to encode as Osmux."}, |
| 216 | [OSMUX_AMR_OCTETS_TX_CTR] = {"all_osmux:amr_octets_tx", "Tx outbound AMD payload octets."}, |
Pau Espin Pedrol | 582c2bf | 2022-09-22 17:53:44 +0200 | [diff] [blame] | 217 | /* These last counters below do not exist in per-connection stats, only here: */ |
| 218 | [OSMUX_NUM_CONNECTIONS] = {"all_osmux:num_closed_conns", "Total number of osmux connections closed."}, |
| 219 | [OSMUX_PACKETS_RX_CTR] = {"all_osmux:packets_rx", "Total inbound UDP/Osmux packets."}, |
| 220 | [OSMUX_PACKETS_TX_CTR] = {"all_osmux:packets_tx", "Total outbound UDP/Osmux packets."}, |
| 221 | [OSMUX_DROPPED_PACKETS_CTR] = {"all_osmux:dropped_packets", "Dropped outbound UDP/Osmux packets."} |
| 222 | }; |
| 223 | |
Philipp Maier | 993ea6b | 2020-08-04 18:26:50 +0200 | [diff] [blame] | 224 | /* Was conn configured to handle Osmux? */ |
| 225 | static inline bool mgcp_conn_rtp_is_osmux(const struct mgcp_conn_rtp *conn) { |
Pau Espin Pedrol | 9d939b6 | 2022-10-03 16:59:20 +0200 | [diff] [blame] | 226 | return conn->type == MGCP_RTP_OSMUX; |
Philipp Maier | 993ea6b | 2020-08-04 18:26:50 +0200 | [diff] [blame] | 227 | } |
| 228 | |
Pau Espin Pedrol | bb3ccde | 2021-12-23 19:49:26 +0100 | [diff] [blame] | 229 | /* Was conn configured to handle Osmux? */ |
| 230 | static inline bool mgcp_conn_rtp_is_iuup(const struct mgcp_conn_rtp *conn) |
| 231 | { |
| 232 | return conn->type == MGCP_RTP_IUUP; |
| 233 | } |
| 234 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 235 | struct mgcp_conn *mgcp_conn_alloc(void *ctx, struct mgcp_endpoint *endp, |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 236 | enum mgcp_conn_type type, char *name); |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame] | 237 | struct mgcp_conn *mgcp_conn_get(struct mgcp_endpoint *endp, const char *id); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 238 | struct mgcp_conn_rtp *mgcp_conn_get_rtp(struct mgcp_endpoint *endp, |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame] | 239 | const char *id); |
| 240 | void mgcp_conn_free(struct mgcp_endpoint *endp, const char *id); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 241 | void mgcp_conn_free_oldest(struct mgcp_endpoint *endp); |
| 242 | void mgcp_conn_free_all(struct mgcp_endpoint *endp); |
| 243 | char *mgcp_conn_dump(struct mgcp_conn *conn); |
| 244 | struct mgcp_conn *mgcp_find_dst_conn(struct mgcp_conn *conn); |
Philipp Maier | 889fe7f | 2020-07-06 17:44:12 +0200 | [diff] [blame] | 245 | struct mgcp_conn *mgcp_conn_get_oldest(struct mgcp_endpoint *endp); |
Philipp Maier | 993ea6b | 2020-08-04 18:26:50 +0200 | [diff] [blame] | 246 | void mgcp_conn_watchdog_kick(struct mgcp_conn *conn); |