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> |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 31 | #include <inttypes.h> |
| 32 | |
Philipp Maier | 62612e8 | 2020-05-27 16:29:22 +0200 | [diff] [blame] | 33 | #define LOGPCONN(conn, cat, level, fmt, args...) \ |
| 34 | LOGPENDP((conn)->endp, cat, level, "CI:%s " fmt, \ |
| 35 | (conn)->id, \ |
| 36 | ## args) |
| 37 | |
Philipp Maier | 993ea6b | 2020-08-04 18:26:50 +0200 | [diff] [blame] | 38 | #define LOG_CONN(conn, level, fmt, args...) \ |
| 39 | LOGP(DRTP, level, "(%s I:%s) " fmt, \ |
| 40 | (conn)->endp ? (conn)->endp->name : "none", (conn)->id, ## args) |
| 41 | |
| 42 | #define LOG_CONN_RTP(conn_rtp, level, fmt, args...) \ |
| 43 | LOG_CONN((conn_rtp)->conn, level, fmt, ## args) |
| 44 | |
| 45 | /* Specific rtp connection type (see struct mgcp_conn_rtp) */ |
| 46 | enum mgcp_conn_rtp_type { |
| 47 | MGCP_RTP_DEFAULT = 0, |
| 48 | MGCP_OSMUX_BSC, |
| 49 | MGCP_OSMUX_BSC_NAT, |
| 50 | }; |
| 51 | |
Philipp Maier | b0c05aa | 2020-07-06 11:52:19 +0200 | [diff] [blame] | 52 | /*! Connection type, specifies which member of the union "u" in mgcp_conn |
| 53 | * contains a useful connection description (currently only RTP) */ |
| 54 | enum mgcp_conn_type { |
| 55 | MGCP_CONN_TYPE_RTP, |
| 56 | }; |
| 57 | |
Philipp Maier | 993ea6b | 2020-08-04 18:26:50 +0200 | [diff] [blame] | 58 | /* MGCP connection (RTP) */ |
| 59 | struct mgcp_conn_rtp { |
| 60 | |
| 61 | /* Backpointer to conn struct */ |
| 62 | struct mgcp_conn *conn; |
| 63 | |
| 64 | /* Specific connection type */ |
| 65 | enum mgcp_conn_rtp_type type; |
| 66 | |
| 67 | /* Port status */ |
| 68 | struct mgcp_rtp_end end; |
| 69 | |
| 70 | /* Sequence bits */ |
| 71 | struct mgcp_rtp_state state; |
| 72 | |
| 73 | /* taps for the rtp connection; one per direction */ |
| 74 | struct mgcp_rtp_tap tap_in; |
| 75 | struct mgcp_rtp_tap tap_out; |
| 76 | |
| 77 | /* Osmux states (optional) */ |
| 78 | struct { |
| 79 | /* Osmux state: disabled, activating, active */ |
| 80 | enum osmux_state state; |
| 81 | /* Is cid holding valid data? is it allocated from pool? */ |
| 82 | bool cid_allocated; |
| 83 | /* Allocated Osmux circuit ID for this conn */ |
| 84 | uint8_t cid; |
| 85 | /* handle to batch messages */ |
| 86 | struct osmux_in_handle *in; |
| 87 | /* handle to unbatch messages */ |
| 88 | struct osmux_out_handle out; |
| 89 | /* statistics */ |
| 90 | struct { |
| 91 | uint32_t chunks; |
| 92 | uint32_t octets; |
| 93 | } stats; |
| 94 | } osmux; |
| 95 | |
| 96 | struct rate_ctr_group *rate_ctr_group; |
| 97 | }; |
| 98 | |
Philipp Maier | b0c05aa | 2020-07-06 11:52:19 +0200 | [diff] [blame] | 99 | /*! MGCP connection (untyped) */ |
| 100 | struct mgcp_conn { |
| 101 | /*! list head */ |
| 102 | struct llist_head entry; |
| 103 | |
| 104 | /*! Backpointer to the endpoint where the conn belongs to */ |
| 105 | struct mgcp_endpoint *endp; |
| 106 | |
| 107 | /*! type of the connection (union) */ |
| 108 | enum mgcp_conn_type type; |
| 109 | |
| 110 | /*! mode of the connection */ |
| 111 | enum mgcp_connection_mode mode; |
| 112 | |
| 113 | /*! copy of the mode to restore the original setting (VTY) */ |
| 114 | enum mgcp_connection_mode mode_orig; |
| 115 | |
| 116 | /*! connection id to identify the connection */ |
| 117 | char id[MGCP_CONN_ID_MAXLEN]; |
| 118 | |
| 119 | /*! human readable name (vty, logging) */ |
| 120 | char name[256]; |
| 121 | |
| 122 | /*! activity tracker (for cleaning up inactive connections) */ |
| 123 | struct osmo_timer_list watchdog; |
| 124 | |
| 125 | /*! union with connection description */ |
| 126 | union { |
| 127 | struct mgcp_conn_rtp rtp; |
| 128 | } u; |
| 129 | |
| 130 | /*! pointer to optional private data */ |
| 131 | void *priv; |
| 132 | }; |
| 133 | |
Philipp Maier | cede2a4 | 2018-07-03 14:14:21 +0200 | [diff] [blame] | 134 | /* RTP connection related counters */ |
| 135 | enum { |
| 136 | IN_STREAM_ERR_TSTMP_CTR, |
| 137 | OUT_STREAM_ERR_TSTMP_CTR, |
| 138 | RTP_PACKETS_RX_CTR, |
| 139 | RTP_OCTETS_RX_CTR, |
| 140 | RTP_PACKETS_TX_CTR, |
| 141 | RTP_OCTETS_TX_CTR, |
Stefan Sperling | ba25eab | 2018-10-30 14:32:31 +0100 | [diff] [blame] | 142 | RTP_DROPPED_PACKETS_CTR, |
| 143 | RTP_NUM_CONNECTIONS, |
| 144 | }; |
| 145 | |
| 146 | /* RTP per-connection statistics. Instances of the corresponding rate counter group |
| 147 | * exist for the lifetime of an RTP connection. |
| 148 | * Must be kept in sync with all_rtp_conn_rate_ctr_desc below */ |
| 149 | static const struct rate_ctr_desc mgcp_conn_rate_ctr_desc[] = { |
| 150 | [IN_STREAM_ERR_TSTMP_CTR] = {"stream_err_tstmp:in", "Inbound rtp-stream timestamp errors."}, |
| 151 | [OUT_STREAM_ERR_TSTMP_CTR] = {"stream_err_tstmp:out", "Outbound rtp-stream timestamp errors."}, |
| 152 | [RTP_PACKETS_RX_CTR] = {"rtp:packets_rx", "Inbound rtp packets."}, |
| 153 | [RTP_OCTETS_RX_CTR] = {"rtp:octets_rx", "Inbound rtp octets."}, |
| 154 | [RTP_PACKETS_TX_CTR] = {"rtp:packets_tx", "Outbound rtp packets."}, |
| 155 | [RTP_OCTETS_TX_CTR] = {"rtp:octets_tx", "Outbound rtp octets."}, |
| 156 | [RTP_DROPPED_PACKETS_CTR] = {"rtp:dropped", "dropped rtp packets."} |
| 157 | }; |
| 158 | |
| 159 | /* Aggregated RTP connection stats. These are updated when an RTP connection is freed. |
| 160 | * Must be kept in sync with mgcp_conn_rate_ctr_desc above */ |
| 161 | static const struct rate_ctr_desc all_rtp_conn_rate_ctr_desc[] = { |
| 162 | [IN_STREAM_ERR_TSTMP_CTR] = {"all_rtp:err_tstmp_in", "Total inbound rtp-stream timestamp errors."}, |
| 163 | [OUT_STREAM_ERR_TSTMP_CTR] = {"all_rtp:err_tstmp_out", "Total outbound rtp-stream timestamp errors."}, |
| 164 | [RTP_PACKETS_RX_CTR] = {"all_rtp:packets_rx", "Total inbound rtp packets."}, |
| 165 | [RTP_OCTETS_RX_CTR] = {"all_rtp:octets_rx", "Total inbound rtp octets."}, |
| 166 | [RTP_PACKETS_TX_CTR] = {"all_rtp:packets_tx", "Total outbound rtp packets."}, |
| 167 | [RTP_OCTETS_TX_CTR] = {"all_rtp:octets_tx", "Total outbound rtp octets."}, |
| 168 | [RTP_DROPPED_PACKETS_CTR] = {"all_rtp:dropped", "Total dropped rtp packets."}, |
| 169 | |
| 170 | /* This last counter does not exist in per-connection stats, only here. */ |
| 171 | [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] | 172 | }; |
| 173 | |
Philipp Maier | 993ea6b | 2020-08-04 18:26:50 +0200 | [diff] [blame] | 174 | /* Was conn configured to handle Osmux? */ |
| 175 | static inline bool mgcp_conn_rtp_is_osmux(const struct mgcp_conn_rtp *conn) { |
| 176 | return conn->type == MGCP_OSMUX_BSC || conn->type == MGCP_OSMUX_BSC_NAT; |
| 177 | } |
| 178 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 179 | struct mgcp_conn *mgcp_conn_alloc(void *ctx, struct mgcp_endpoint *endp, |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 180 | enum mgcp_conn_type type, char *name); |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame] | 181 | 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] | 182 | struct mgcp_conn_rtp *mgcp_conn_get_rtp(struct mgcp_endpoint *endp, |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame] | 183 | const char *id); |
| 184 | void mgcp_conn_free(struct mgcp_endpoint *endp, const char *id); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 185 | void mgcp_conn_free_oldest(struct mgcp_endpoint *endp); |
| 186 | void mgcp_conn_free_all(struct mgcp_endpoint *endp); |
| 187 | char *mgcp_conn_dump(struct mgcp_conn *conn); |
| 188 | struct mgcp_conn *mgcp_find_dst_conn(struct mgcp_conn *conn); |
Philipp Maier | 889fe7f | 2020-07-06 17:44:12 +0200 | [diff] [blame] | 189 | struct mgcp_conn *mgcp_conn_get_oldest(struct mgcp_endpoint *endp); |
Philipp Maier | 993ea6b | 2020-08-04 18:26:50 +0200 | [diff] [blame] | 190 | void mgcp_conn_watchdog_kick(struct mgcp_conn *conn); |