blob: 5e262c35d46b6cd47c1e9e05ab1eb27e5bc6e7f1 [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;
83 /* Is cid holding valid data? is it allocated from pool? */
84 bool cid_allocated;
85 /* Allocated Osmux circuit ID for this conn */
86 uint8_t cid;
Pau Espin Pedrol33639162022-08-31 17:46:11 +020087 /* handle to batch messages, shared (refcounted) among several conns */
Philipp Maier993ea6b2020-08-04 18:26:50 +020088 struct osmux_in_handle *in;
Pau Espin Pedrol33639162022-08-31 17:46:11 +020089 /* handle to unbatch messages, one allocated and owned per conn */
90 struct osmux_out_handle *out;
Philipp Maier993ea6b2020-08-04 18:26:50 +020091 /* statistics */
92 struct {
93 uint32_t chunks;
94 uint32_t octets;
95 } stats;
96 } 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
Philipp Maier993ea6b2020-08-04 18:26:50 +0200106 struct rate_ctr_group *rate_ctr_group;
107};
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
Philipp Maier993ea6b2020-08-04 18:26:50 +0200184/* Was conn configured to handle Osmux? */
185static inline bool mgcp_conn_rtp_is_osmux(const struct mgcp_conn_rtp *conn) {
186 return conn->type == MGCP_OSMUX_BSC || conn->type == MGCP_OSMUX_BSC_NAT;
187}
188
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100189/* Was conn configured to handle Osmux? */
190static inline bool mgcp_conn_rtp_is_iuup(const struct mgcp_conn_rtp *conn)
191{
192 return conn->type == MGCP_RTP_IUUP;
193}
194
Philipp Maier87bd9be2017-08-22 16:35:41 +0200195struct mgcp_conn *mgcp_conn_alloc(void *ctx, struct mgcp_endpoint *endp,
Philipp Maierffd75e42017-11-22 11:44:50 +0100196 enum mgcp_conn_type type, char *name);
Philipp Maier01d24a32017-11-21 17:26:09 +0100197struct mgcp_conn *mgcp_conn_get(struct mgcp_endpoint *endp, const char *id);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200198struct mgcp_conn_rtp *mgcp_conn_get_rtp(struct mgcp_endpoint *endp,
Philipp Maier01d24a32017-11-21 17:26:09 +0100199 const char *id);
200void mgcp_conn_free(struct mgcp_endpoint *endp, const char *id);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200201void mgcp_conn_free_oldest(struct mgcp_endpoint *endp);
202void mgcp_conn_free_all(struct mgcp_endpoint *endp);
203char *mgcp_conn_dump(struct mgcp_conn *conn);
204struct mgcp_conn *mgcp_find_dst_conn(struct mgcp_conn *conn);
Philipp Maier889fe7f2020-07-06 17:44:12 +0200205struct mgcp_conn *mgcp_conn_get_oldest(struct mgcp_endpoint *endp);
Philipp Maier993ea6b2020-08-04 18:26:50 +0200206void mgcp_conn_watchdog_kick(struct mgcp_conn *conn);