blob: 4f7b70556b028855f73df4d23e13f9e4c826fbcc [file] [log] [blame]
Philipp Maier87bd9be2017-08-22 16:35:41 +02001/* 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 Maier993ea6b2020-08-04 18:26:50 +020026#include <osmocom/mgcp/mgcp.h>
27#include <osmocom/mgcp/mgcp_network.h>
28#include <osmocom/mgcp/osmux.h>
Philipp Maier87bd9be2017-08-22 16:35:41 +020029#include <osmocom/core/linuxlist.h>
Stefan Sperlingba25eab2018-10-30 14:32:31 +010030#include <osmocom/core/rate_ctr.h>
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +010031#include <osmocom/gsm/iuup.h>
Philipp Maier87bd9be2017-08-22 16:35:41 +020032#include <inttypes.h>
33
Philipp Maier62612e82020-05-27 16:29:22 +020034#define LOGPCONN(conn, cat, level, fmt, args...) \
35LOGPENDP((conn)->endp, cat, level, "CI:%s " fmt, \
36 (conn)->id, \
37 ## args)
38
Philipp Maier993ea6b2020-08-04 18:26:50 +020039#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) */
47enum mgcp_conn_rtp_type {
48 MGCP_RTP_DEFAULT = 0,
Pau Espin Pedrol9d939b62022-10-03 16:59:20 +020049 MGCP_RTP_OSMUX,
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +010050 MGCP_RTP_IUUP,
Philipp Maier993ea6b2020-08-04 18:26:50 +020051};
Neels Hofmeyrc695e9f2023-11-01 21:29:30 +010052extern const struct value_string mgcp_conn_rtp_type_names[];
53static inline const char *mgcp_conn_rtp_type_name(enum mgcp_conn_rtp_type val)
54{
55 return get_value_string(mgcp_conn_rtp_type_names, val);
56}
Philipp Maier993ea6b2020-08-04 18:26:50 +020057
Philipp Maierb0c05aa2020-07-06 11:52:19 +020058/*! Connection type, specifies which member of the union "u" in mgcp_conn
59 * contains a useful connection description (currently only RTP) */
60enum mgcp_conn_type {
61 MGCP_CONN_TYPE_RTP,
62};
63
Philipp Maier993ea6b2020-08-04 18:26:50 +020064/* MGCP connection (RTP) */
65struct mgcp_conn_rtp {
66
67 /* Backpointer to conn struct */
68 struct mgcp_conn *conn;
69
70 /* Specific connection type */
71 enum mgcp_conn_rtp_type type;
72
73 /* Port status */
74 struct mgcp_rtp_end end;
75
76 /* Sequence bits */
77 struct mgcp_rtp_state state;
78
79 /* taps for the rtp connection; one per direction */
80 struct mgcp_rtp_tap tap_in;
81 struct mgcp_rtp_tap tap_out;
82
83 /* Osmux states (optional) */
84 struct {
85 /* Osmux state: disabled, activating, active */
86 enum osmux_state state;
Pau Espin Pedrol21779192022-09-23 16:46:33 +020087 /* Is local_cid holding valid data? is it allocated from pool? */
88 bool local_cid_allocated;
89 /* Allocated local Osmux circuit ID for this conn */
90 uint8_t local_cid;
91 /* Is remote_cid holding valid data? was it already received from client? */
92 bool remote_cid_present;
93 /* Received remote Osmux circuit ID for this conn */
94 uint8_t remote_cid;
Pau Espin Pedrol33639162022-08-31 17:46:11 +020095 /* handle to batch messages, shared (refcounted) among several conns */
Philipp Maier993ea6b2020-08-04 18:26:50 +020096 struct osmux_in_handle *in;
Pau Espin Pedrol33639162022-08-31 17:46:11 +020097 /* handle to unbatch messages, one allocated and owned per conn */
98 struct osmux_out_handle *out;
Pau Espin Pedrol582c2bf2022-09-22 17:53:44 +020099 /* statistics: */
100 struct rate_ctr_group *ctrg;
Philipp Maier993ea6b2020-08-04 18:26:50 +0200101 } osmux;
102
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100103 struct {
104 struct osmo_iuup_instance *iui;
105 bool active_init; /* true: Send IuUP Init */
Pau Espin Pedrol452f2ba2022-05-24 19:15:54 +0200106 int rfci_id_no_data; /* RFCI Id for RFCI NO_DATA (-1 if not available) */
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100107 bool configured;
108 struct osmo_iuup_rnl_prim *init_ind;
109 } iuup;
110
Pau Espin Pedroldaf5bce2022-09-22 19:14:24 +0200111 struct rate_ctr_group *ctrg;
Philipp Maier993ea6b2020-08-04 18:26:50 +0200112};
113
Philipp Maierb0c05aa2020-07-06 11:52:19 +0200114/*! MGCP connection (untyped) */
115struct mgcp_conn {
116 /*! list head */
117 struct llist_head entry;
118
119 /*! Backpointer to the endpoint where the conn belongs to */
120 struct mgcp_endpoint *endp;
121
122 /*! type of the connection (union) */
123 enum mgcp_conn_type type;
124
125 /*! mode of the connection */
126 enum mgcp_connection_mode mode;
127
128 /*! copy of the mode to restore the original setting (VTY) */
129 enum mgcp_connection_mode mode_orig;
130
131 /*! connection id to identify the connection */
132 char id[MGCP_CONN_ID_MAXLEN];
133
134 /*! human readable name (vty, logging) */
135 char name[256];
136
137 /*! activity tracker (for cleaning up inactive connections) */
138 struct osmo_timer_list watchdog;
139
140 /*! union with connection description */
141 union {
142 struct mgcp_conn_rtp rtp;
143 } u;
144
145 /*! pointer to optional private data */
146 void *priv;
147};
148
Philipp Maiercede2a42018-07-03 14:14:21 +0200149/* RTP connection related counters */
150enum {
151 IN_STREAM_ERR_TSTMP_CTR,
152 OUT_STREAM_ERR_TSTMP_CTR,
Pau Espin Pedrol26e810d2022-09-22 17:16:20 +0200153 RTP_PACKETS_RX_CTR,
154 RTP_OCTETS_RX_CTR,
155 RTP_PACKETS_TX_CTR,
156 RTP_OCTETS_TX_CTR,
157 RTP_DROPPED_PACKETS_CTR,
158 RTP_NUM_CONNECTIONS,
Stefan Sperlingba25eab2018-10-30 14:32:31 +0100159};
160
161/* RTP per-connection statistics. Instances of the corresponding rate counter group
162 * exist for the lifetime of an RTP connection.
163 * Must be kept in sync with all_rtp_conn_rate_ctr_desc below */
164static const struct rate_ctr_desc mgcp_conn_rate_ctr_desc[] = {
165 [IN_STREAM_ERR_TSTMP_CTR] = {"stream_err_tstmp:in", "Inbound rtp-stream timestamp errors."},
166 [OUT_STREAM_ERR_TSTMP_CTR] = {"stream_err_tstmp:out", "Outbound rtp-stream timestamp errors."},
167 [RTP_PACKETS_RX_CTR] = {"rtp:packets_rx", "Inbound rtp packets."},
168 [RTP_OCTETS_RX_CTR] = {"rtp:octets_rx", "Inbound rtp octets."},
169 [RTP_PACKETS_TX_CTR] = {"rtp:packets_tx", "Outbound rtp packets."},
170 [RTP_OCTETS_TX_CTR] = {"rtp:octets_tx", "Outbound rtp octets."},
Pau Espin Pedrola29f1552022-09-22 19:06:06 +0200171 [RTP_DROPPED_PACKETS_CTR] = {"rtp:dropped", "Dropped rtp packets."}
Stefan Sperlingba25eab2018-10-30 14:32:31 +0100172};
173
174/* Aggregated RTP connection stats. These are updated when an RTP connection is freed.
175 * Must be kept in sync with mgcp_conn_rate_ctr_desc above */
176static const struct rate_ctr_desc all_rtp_conn_rate_ctr_desc[] = {
177 [IN_STREAM_ERR_TSTMP_CTR] = {"all_rtp:err_tstmp_in", "Total inbound rtp-stream timestamp errors."},
178 [OUT_STREAM_ERR_TSTMP_CTR] = {"all_rtp:err_tstmp_out", "Total outbound rtp-stream timestamp errors."},
179 [RTP_PACKETS_RX_CTR] = {"all_rtp:packets_rx", "Total inbound rtp packets."},
180 [RTP_OCTETS_RX_CTR] = {"all_rtp:octets_rx", "Total inbound rtp octets."},
181 [RTP_PACKETS_TX_CTR] = {"all_rtp:packets_tx", "Total outbound rtp packets."},
182 [RTP_OCTETS_TX_CTR] = {"all_rtp:octets_tx", "Total outbound rtp octets."},
183 [RTP_DROPPED_PACKETS_CTR] = {"all_rtp:dropped", "Total dropped rtp packets."},
184
185 /* This last counter does not exist in per-connection stats, only here. */
186 [RTP_NUM_CONNECTIONS] = {"all_rtp:num_closed_conns", "Total number of rtp connections closed."}
Philipp Maiercede2a42018-07-03 14:14:21 +0200187};
188
Pau Espin Pedrol582c2bf2022-09-22 17:53:44 +0200189/* Osmux connection related counters */
190enum {
191 OSMUX_CHUNKS_RX_CTR,
192 OSMUX_OCTETS_RX_CTR,
Pau Espin Pedrol432ee9d2022-09-23 18:18:02 +0200193 OSMUX_RTP_PACKETS_TX_CTR,
194 OSMUX_RTP_PACKETS_TX_DROPPED_CTR,
195 OSMUX_AMR_OCTETS_TX_CTR,
Pau Espin Pedrol582c2bf2022-09-22 17:53:44 +0200196 /* Only available in global stats: */
197 OSMUX_NUM_CONNECTIONS,
198 OSMUX_PACKETS_RX_CTR,
199 OSMUX_PACKETS_TX_CTR,
200 OSMUX_DROPPED_PACKETS_CTR,
201};
202
203/* RTP per-connection statistics. Instances of the corresponding rate counter group
204 * exist for the lifetime of an RTP connection.
205 * Must be kept in sync with all_rtp_conn_rate_ctr_desc below */
206static const struct rate_ctr_desc mgcp_conn_osmux_rate_ctr_desc[] = {
207 [OSMUX_CHUNKS_RX_CTR] = {"osmux:chunks_rx", "Inbound Osmux chunks."},
208 [OSMUX_OCTETS_RX_CTR] = {"osmux:octets_rx", "Inbound Osmux octets."},
Pau Espin Pedrol432ee9d2022-09-23 18:18:02 +0200209 [OSMUX_RTP_PACKETS_TX_CTR] = {"osmux:rtp_packets_tx", "Tx outbound RTP packets to encode as Osmux."},
210 [OSMUX_RTP_PACKETS_TX_DROPPED_CTR] = {"osmux:rtp_packets_tx_dropped", "Dropped Tx outbound RTP packets to encode as Osmux."},
211 [OSMUX_AMR_OCTETS_TX_CTR] = {"osmux:amr_octets_tx", "Tx outbound AMD payload octets."},
Pau Espin Pedrol582c2bf2022-09-22 17:53:44 +0200212};
213
214/* Aggregated Osmux connection stats. These are updated when an Osmux connection is freed.
215 * Must be kept in sync with mgcp_conn_osmux_rate_ctr_desc above */
216static const struct rate_ctr_desc all_osmux_conn_rate_ctr_desc[] = {
217 [OSMUX_CHUNKS_RX_CTR] = {"all_osmux:chunks_rx", "Inbound Osmux chunks."},
218 [OSMUX_OCTETS_RX_CTR] = {"all_osmux:octets_rx", "Inbound Osmux octets."},
Pau Espin Pedrol432ee9d2022-09-23 18:18:02 +0200219 [OSMUX_RTP_PACKETS_TX_CTR] = {"all_osmux:rtp_packets_tx", "Tx outbound RTP packets to encode as Osmux."},
220 [OSMUX_RTP_PACKETS_TX_DROPPED_CTR] = {"all_osmux:rtp_packets_tx_dropped", "Dropped Tx outbound RTP packets to encode as Osmux."},
221 [OSMUX_AMR_OCTETS_TX_CTR] = {"all_osmux:amr_octets_tx", "Tx outbound AMD payload octets."},
Pau Espin Pedrol582c2bf2022-09-22 17:53:44 +0200222 /* These last counters below do not exist in per-connection stats, only here: */
223 [OSMUX_NUM_CONNECTIONS] = {"all_osmux:num_closed_conns", "Total number of osmux connections closed."},
224 [OSMUX_PACKETS_RX_CTR] = {"all_osmux:packets_rx", "Total inbound UDP/Osmux packets."},
225 [OSMUX_PACKETS_TX_CTR] = {"all_osmux:packets_tx", "Total outbound UDP/Osmux packets."},
226 [OSMUX_DROPPED_PACKETS_CTR] = {"all_osmux:dropped_packets", "Dropped outbound UDP/Osmux packets."}
227};
228
Philipp Maier993ea6b2020-08-04 18:26:50 +0200229/* Was conn configured to handle Osmux? */
230static inline bool mgcp_conn_rtp_is_osmux(const struct mgcp_conn_rtp *conn) {
Pau Espin Pedrol9d939b62022-10-03 16:59:20 +0200231 return conn->type == MGCP_RTP_OSMUX;
Philipp Maier993ea6b2020-08-04 18:26:50 +0200232}
233
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100234/* Was conn configured to handle Osmux? */
235static inline bool mgcp_conn_rtp_is_iuup(const struct mgcp_conn_rtp *conn)
236{
237 return conn->type == MGCP_RTP_IUUP;
238}
239
Philipp Maier87bd9be2017-08-22 16:35:41 +0200240struct mgcp_conn *mgcp_conn_alloc(void *ctx, struct mgcp_endpoint *endp,
Philipp Maierffd75e42017-11-22 11:44:50 +0100241 enum mgcp_conn_type type, char *name);
Philipp Maier01d24a32017-11-21 17:26:09 +0100242struct mgcp_conn *mgcp_conn_get(struct mgcp_endpoint *endp, const char *id);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200243struct mgcp_conn_rtp *mgcp_conn_get_rtp(struct mgcp_endpoint *endp,
Philipp Maier01d24a32017-11-21 17:26:09 +0100244 const char *id);
245void mgcp_conn_free(struct mgcp_endpoint *endp, const char *id);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200246void mgcp_conn_free_oldest(struct mgcp_endpoint *endp);
247void mgcp_conn_free_all(struct mgcp_endpoint *endp);
248char *mgcp_conn_dump(struct mgcp_conn *conn);
249struct mgcp_conn *mgcp_find_dst_conn(struct mgcp_conn *conn);
Philipp Maier889fe7f2020-07-06 17:44:12 +0200250struct mgcp_conn *mgcp_conn_get_oldest(struct mgcp_endpoint *endp);
Philipp Maier993ea6b2020-08-04 18:26:50 +0200251void mgcp_conn_watchdog_kick(struct mgcp_conn *conn);