blob: 7ac40ab42afc549e2240b36aac5171e15ea8d853 [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};
52
Philipp Maierb0c05aa2020-07-06 11:52:19 +020053/*! Connection type, specifies which member of the union "u" in mgcp_conn
54 * contains a useful connection description (currently only RTP) */
55enum mgcp_conn_type {
56 MGCP_CONN_TYPE_RTP,
57};
58
Philipp Maier993ea6b2020-08-04 18:26:50 +020059/* MGCP connection (RTP) */
60struct 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 Pedrol21779192022-09-23 16:46:33 +020082 /* 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 Pedrol33639162022-08-31 17:46:11 +020090 /* handle to batch messages, shared (refcounted) among several conns */
Philipp Maier993ea6b2020-08-04 18:26:50 +020091 struct osmux_in_handle *in;
Pau Espin Pedrol33639162022-08-31 17:46:11 +020092 /* handle to unbatch messages, one allocated and owned per conn */
93 struct osmux_out_handle *out;
Pau Espin Pedrol582c2bf2022-09-22 17:53:44 +020094 /* statistics: */
95 struct rate_ctr_group *ctrg;
Philipp Maier993ea6b2020-08-04 18:26:50 +020096 } osmux;
97
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +010098 struct {
99 struct osmo_iuup_instance *iui;
100 bool active_init; /* true: Send IuUP Init */
Pau Espin Pedrol452f2ba2022-05-24 19:15:54 +0200101 int rfci_id_no_data; /* RFCI Id for RFCI NO_DATA (-1 if not available) */
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100102 bool configured;
103 struct osmo_iuup_rnl_prim *init_ind;
104 } iuup;
105
Pau Espin Pedroldaf5bce2022-09-22 19:14:24 +0200106 struct rate_ctr_group *ctrg;
Philipp Maier993ea6b2020-08-04 18:26:50 +0200107};
108
Philipp Maierb0c05aa2020-07-06 11:52:19 +0200109/*! MGCP connection (untyped) */
110struct 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 Maiercede2a42018-07-03 14:14:21 +0200144/* RTP connection related counters */
145enum {
146 IN_STREAM_ERR_TSTMP_CTR,
147 OUT_STREAM_ERR_TSTMP_CTR,
Pau Espin Pedrol26e810d2022-09-22 17:16:20 +0200148 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 Sperlingba25eab2018-10-30 14:32:31 +0100154};
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 */
159static 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 Pedrola29f1552022-09-22 19:06:06 +0200166 [RTP_DROPPED_PACKETS_CTR] = {"rtp:dropped", "Dropped rtp packets."}
Stefan Sperlingba25eab2018-10-30 14:32:31 +0100167};
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 */
171static 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 Maiercede2a42018-07-03 14:14:21 +0200182};
183
Pau Espin Pedrol582c2bf2022-09-22 17:53:44 +0200184/* Osmux connection related counters */
185enum {
186 OSMUX_CHUNKS_RX_CTR,
187 OSMUX_OCTETS_RX_CTR,
Pau Espin Pedrol432ee9d2022-09-23 18:18:02 +0200188 OSMUX_RTP_PACKETS_TX_CTR,
189 OSMUX_RTP_PACKETS_TX_DROPPED_CTR,
190 OSMUX_AMR_OCTETS_TX_CTR,
Pau Espin Pedrol582c2bf2022-09-22 17:53:44 +0200191 /* 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 */
201static 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 Pedrol432ee9d2022-09-23 18:18:02 +0200204 [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 Pedrol582c2bf2022-09-22 17:53:44 +0200207};
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 */
211static 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 Pedrol432ee9d2022-09-23 18:18:02 +0200214 [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 Pedrol582c2bf2022-09-22 17:53:44 +0200217 /* 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 Maier993ea6b2020-08-04 18:26:50 +0200224/* Was conn configured to handle Osmux? */
225static inline bool mgcp_conn_rtp_is_osmux(const struct mgcp_conn_rtp *conn) {
Pau Espin Pedrol9d939b62022-10-03 16:59:20 +0200226 return conn->type == MGCP_RTP_OSMUX;
Philipp Maier993ea6b2020-08-04 18:26:50 +0200227}
228
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100229/* Was conn configured to handle Osmux? */
230static inline bool mgcp_conn_rtp_is_iuup(const struct mgcp_conn_rtp *conn)
231{
232 return conn->type == MGCP_RTP_IUUP;
233}
234
Philipp Maier87bd9be2017-08-22 16:35:41 +0200235struct mgcp_conn *mgcp_conn_alloc(void *ctx, struct mgcp_endpoint *endp,
Philipp Maierffd75e42017-11-22 11:44:50 +0100236 enum mgcp_conn_type type, char *name);
Philipp Maier01d24a32017-11-21 17:26:09 +0100237struct mgcp_conn *mgcp_conn_get(struct mgcp_endpoint *endp, const char *id);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200238struct mgcp_conn_rtp *mgcp_conn_get_rtp(struct mgcp_endpoint *endp,
Philipp Maier01d24a32017-11-21 17:26:09 +0100239 const char *id);
240void mgcp_conn_free(struct mgcp_endpoint *endp, const char *id);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200241void mgcp_conn_free_oldest(struct mgcp_endpoint *endp);
242void mgcp_conn_free_all(struct mgcp_endpoint *endp);
243char *mgcp_conn_dump(struct mgcp_conn *conn);
244struct mgcp_conn *mgcp_find_dst_conn(struct mgcp_conn *conn);
Philipp Maier889fe7f2020-07-06 17:44:12 +0200245struct mgcp_conn *mgcp_conn_get_oldest(struct mgcp_endpoint *endp);
Philipp Maier993ea6b2020-08-04 18:26:50 +0200246void mgcp_conn_watchdog_kick(struct mgcp_conn *conn);