blob: 9ffa7dc0736f7fc5ebd421e4be06ee420b942ff3 [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,
49 MGCP_OSMUX_BSC,
50 MGCP_OSMUX_BSC_NAT,
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +010051 MGCP_RTP_IUUP,
Philipp Maier993ea6b2020-08-04 18:26:50 +020052};
53
Philipp Maierb0c05aa2020-07-06 11:52:19 +020054/*! Connection type, specifies which member of the union "u" in mgcp_conn
55 * contains a useful connection description (currently only RTP) */
56enum mgcp_conn_type {
57 MGCP_CONN_TYPE_RTP,
58};
59
Philipp Maier993ea6b2020-08-04 18:26:50 +020060/* MGCP connection (RTP) */
61struct mgcp_conn_rtp {
62
63 /* Backpointer to conn struct */
64 struct mgcp_conn *conn;
65
66 /* Specific connection type */
67 enum mgcp_conn_rtp_type type;
68
69 /* Port status */
70 struct mgcp_rtp_end end;
71
72 /* Sequence bits */
73 struct mgcp_rtp_state state;
74
75 /* taps for the rtp connection; one per direction */
76 struct mgcp_rtp_tap tap_in;
77 struct mgcp_rtp_tap tap_out;
78
79 /* Osmux states (optional) */
80 struct {
81 /* Osmux state: disabled, activating, active */
82 enum osmux_state state;
Pau Espin Pedrol21779192022-09-23 16:46:33 +020083 /* Is local_cid holding valid data? is it allocated from pool? */
84 bool local_cid_allocated;
85 /* Allocated local Osmux circuit ID for this conn */
86 uint8_t local_cid;
87 /* Is remote_cid holding valid data? was it already received from client? */
88 bool remote_cid_present;
89 /* Received remote Osmux circuit ID for this conn */
90 uint8_t remote_cid;
Pau Espin Pedrol33639162022-08-31 17:46:11 +020091 /* handle to batch messages, shared (refcounted) among several conns */
Philipp Maier993ea6b2020-08-04 18:26:50 +020092 struct osmux_in_handle *in;
Pau Espin Pedrol33639162022-08-31 17:46:11 +020093 /* handle to unbatch messages, one allocated and owned per conn */
94 struct osmux_out_handle *out;
Pau Espin Pedrol582c2bf2022-09-22 17:53:44 +020095 /* statistics: */
96 struct rate_ctr_group *ctrg;
Philipp Maier993ea6b2020-08-04 18:26:50 +020097 } osmux;
98
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +010099 struct {
100 struct osmo_iuup_instance *iui;
101 bool active_init; /* true: Send IuUP Init */
Pau Espin Pedrol452f2ba2022-05-24 19:15:54 +0200102 int rfci_id_no_data; /* RFCI Id for RFCI NO_DATA (-1 if not available) */
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100103 bool configured;
104 struct osmo_iuup_rnl_prim *init_ind;
105 } iuup;
106
Pau Espin Pedroldaf5bce2022-09-22 19:14:24 +0200107 struct rate_ctr_group *ctrg;
Philipp Maier993ea6b2020-08-04 18:26:50 +0200108};
109
Philipp Maierb0c05aa2020-07-06 11:52:19 +0200110/*! MGCP connection (untyped) */
111struct mgcp_conn {
112 /*! list head */
113 struct llist_head entry;
114
115 /*! Backpointer to the endpoint where the conn belongs to */
116 struct mgcp_endpoint *endp;
117
118 /*! type of the connection (union) */
119 enum mgcp_conn_type type;
120
121 /*! mode of the connection */
122 enum mgcp_connection_mode mode;
123
124 /*! copy of the mode to restore the original setting (VTY) */
125 enum mgcp_connection_mode mode_orig;
126
127 /*! connection id to identify the connection */
128 char id[MGCP_CONN_ID_MAXLEN];
129
130 /*! human readable name (vty, logging) */
131 char name[256];
132
133 /*! activity tracker (for cleaning up inactive connections) */
134 struct osmo_timer_list watchdog;
135
136 /*! union with connection description */
137 union {
138 struct mgcp_conn_rtp rtp;
139 } u;
140
141 /*! pointer to optional private data */
142 void *priv;
143};
144
Philipp Maiercede2a42018-07-03 14:14:21 +0200145/* RTP connection related counters */
146enum {
147 IN_STREAM_ERR_TSTMP_CTR,
148 OUT_STREAM_ERR_TSTMP_CTR,
Pau Espin Pedrol26e810d2022-09-22 17:16:20 +0200149 RTP_PACKETS_RX_CTR,
150 RTP_OCTETS_RX_CTR,
151 RTP_PACKETS_TX_CTR,
152 RTP_OCTETS_TX_CTR,
153 RTP_DROPPED_PACKETS_CTR,
154 RTP_NUM_CONNECTIONS,
Stefan Sperlingba25eab2018-10-30 14:32:31 +0100155};
156
157/* RTP per-connection statistics. Instances of the corresponding rate counter group
158 * exist for the lifetime of an RTP connection.
159 * Must be kept in sync with all_rtp_conn_rate_ctr_desc below */
160static const struct rate_ctr_desc mgcp_conn_rate_ctr_desc[] = {
161 [IN_STREAM_ERR_TSTMP_CTR] = {"stream_err_tstmp:in", "Inbound rtp-stream timestamp errors."},
162 [OUT_STREAM_ERR_TSTMP_CTR] = {"stream_err_tstmp:out", "Outbound rtp-stream timestamp errors."},
163 [RTP_PACKETS_RX_CTR] = {"rtp:packets_rx", "Inbound rtp packets."},
164 [RTP_OCTETS_RX_CTR] = {"rtp:octets_rx", "Inbound rtp octets."},
165 [RTP_PACKETS_TX_CTR] = {"rtp:packets_tx", "Outbound rtp packets."},
166 [RTP_OCTETS_TX_CTR] = {"rtp:octets_tx", "Outbound rtp octets."},
Pau Espin Pedrola29f1552022-09-22 19:06:06 +0200167 [RTP_DROPPED_PACKETS_CTR] = {"rtp:dropped", "Dropped rtp packets."}
Stefan Sperlingba25eab2018-10-30 14:32:31 +0100168};
169
170/* Aggregated RTP connection stats. These are updated when an RTP connection is freed.
171 * Must be kept in sync with mgcp_conn_rate_ctr_desc above */
172static const struct rate_ctr_desc all_rtp_conn_rate_ctr_desc[] = {
173 [IN_STREAM_ERR_TSTMP_CTR] = {"all_rtp:err_tstmp_in", "Total inbound rtp-stream timestamp errors."},
174 [OUT_STREAM_ERR_TSTMP_CTR] = {"all_rtp:err_tstmp_out", "Total outbound rtp-stream timestamp errors."},
175 [RTP_PACKETS_RX_CTR] = {"all_rtp:packets_rx", "Total inbound rtp packets."},
176 [RTP_OCTETS_RX_CTR] = {"all_rtp:octets_rx", "Total inbound rtp octets."},
177 [RTP_PACKETS_TX_CTR] = {"all_rtp:packets_tx", "Total outbound rtp packets."},
178 [RTP_OCTETS_TX_CTR] = {"all_rtp:octets_tx", "Total outbound rtp octets."},
179 [RTP_DROPPED_PACKETS_CTR] = {"all_rtp:dropped", "Total dropped rtp packets."},
180
181 /* This last counter does not exist in per-connection stats, only here. */
182 [RTP_NUM_CONNECTIONS] = {"all_rtp:num_closed_conns", "Total number of rtp connections closed."}
Philipp Maiercede2a42018-07-03 14:14:21 +0200183};
184
Pau Espin Pedrol582c2bf2022-09-22 17:53:44 +0200185/* Osmux connection related counters */
186enum {
187 OSMUX_CHUNKS_RX_CTR,
188 OSMUX_OCTETS_RX_CTR,
189 OSMUX_DROPPED_AMR_PAYLOADS_CTR,
190 /* Only available in global stats: */
191 OSMUX_NUM_CONNECTIONS,
192 OSMUX_PACKETS_RX_CTR,
193 OSMUX_PACKETS_TX_CTR,
194 OSMUX_DROPPED_PACKETS_CTR,
195};
196
197/* RTP per-connection statistics. Instances of the corresponding rate counter group
198 * exist for the lifetime of an RTP connection.
199 * Must be kept in sync with all_rtp_conn_rate_ctr_desc below */
200static const struct rate_ctr_desc mgcp_conn_osmux_rate_ctr_desc[] = {
201 [OSMUX_CHUNKS_RX_CTR] = {"osmux:chunks_rx", "Inbound Osmux chunks."},
202 [OSMUX_OCTETS_RX_CTR] = {"osmux:octets_rx", "Inbound Osmux octets."},
203 [OSMUX_DROPPED_AMR_PAYLOADS_CTR] = {"osmux:dropped_amr_payloads", "Dropped outbound AMR payloads."}
204};
205
206/* Aggregated Osmux connection stats. These are updated when an Osmux connection is freed.
207 * Must be kept in sync with mgcp_conn_osmux_rate_ctr_desc above */
208static const struct rate_ctr_desc all_osmux_conn_rate_ctr_desc[] = {
209 [OSMUX_CHUNKS_RX_CTR] = {"all_osmux:chunks_rx", "Inbound Osmux chunks."},
210 [OSMUX_OCTETS_RX_CTR] = {"all_osmux:octets_rx", "Inbound Osmux octets."},
211 [OSMUX_DROPPED_AMR_PAYLOADS_CTR] = {"all_osmux:dropped_amr_payloads", "Dropped outbound AMR payloads."},
212 /* These last counters below do not exist in per-connection stats, only here: */
213 [OSMUX_NUM_CONNECTIONS] = {"all_osmux:num_closed_conns", "Total number of osmux connections closed."},
214 [OSMUX_PACKETS_RX_CTR] = {"all_osmux:packets_rx", "Total inbound UDP/Osmux packets."},
215 [OSMUX_PACKETS_TX_CTR] = {"all_osmux:packets_tx", "Total outbound UDP/Osmux packets."},
216 [OSMUX_DROPPED_PACKETS_CTR] = {"all_osmux:dropped_packets", "Dropped outbound UDP/Osmux packets."}
217};
218
Philipp Maier993ea6b2020-08-04 18:26:50 +0200219/* Was conn configured to handle Osmux? */
220static inline bool mgcp_conn_rtp_is_osmux(const struct mgcp_conn_rtp *conn) {
221 return conn->type == MGCP_OSMUX_BSC || conn->type == MGCP_OSMUX_BSC_NAT;
222}
223
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100224/* Was conn configured to handle Osmux? */
225static inline bool mgcp_conn_rtp_is_iuup(const struct mgcp_conn_rtp *conn)
226{
227 return conn->type == MGCP_RTP_IUUP;
228}
229
Philipp Maier87bd9be2017-08-22 16:35:41 +0200230struct mgcp_conn *mgcp_conn_alloc(void *ctx, struct mgcp_endpoint *endp,
Philipp Maierffd75e42017-11-22 11:44:50 +0100231 enum mgcp_conn_type type, char *name);
Philipp Maier01d24a32017-11-21 17:26:09 +0100232struct mgcp_conn *mgcp_conn_get(struct mgcp_endpoint *endp, const char *id);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200233struct mgcp_conn_rtp *mgcp_conn_get_rtp(struct mgcp_endpoint *endp,
Philipp Maier01d24a32017-11-21 17:26:09 +0100234 const char *id);
235void mgcp_conn_free(struct mgcp_endpoint *endp, const char *id);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200236void mgcp_conn_free_oldest(struct mgcp_endpoint *endp);
237void mgcp_conn_free_all(struct mgcp_endpoint *endp);
238char *mgcp_conn_dump(struct mgcp_conn *conn);
239struct mgcp_conn *mgcp_find_dst_conn(struct mgcp_conn *conn);
Philipp Maier889fe7f2020-07-06 17:44:12 +0200240struct mgcp_conn *mgcp_conn_get_oldest(struct mgcp_endpoint *endp);
Philipp Maier993ea6b2020-08-04 18:26:50 +0200241void mgcp_conn_watchdog_kick(struct mgcp_conn *conn);