blob: 3c3b5dbaa1e73993c9bbc76dbf95c1f63991b833 [file] [log] [blame]
Philipp Maierc66ab2c2020-06-02 20:55:34 +02001/* A Media Gateway Control Protocol Media Gateway: RFC 3435 */
2/* rate-counter implementation */
3
4/*
5 * (C) 2009-2012 by Holger Hans Peter Freyther <zecke@selfish.org>
6 * (C) 2009-2012 by On-Waves
7 * (C) 2017-2020 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
8 * All Rights Reserved
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Affero General Public License as published by
12 * the Free Software Foundation; either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Affero General Public License for more details.
19 *
20 * You should have received a copy of the GNU Affero General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 *
23 */
24
25#include <errno.h>
26#include <osmocom/core/stats.h>
Philipp Maier993ea6b2020-08-04 18:26:50 +020027#include <osmocom/mgcp/mgcp_conn.h>
Philipp Maiera065e632021-07-09 13:22:42 +020028#include <osmocom/mgcp/mgcp_trunk.h>
Philipp Maierc66ab2c2020-06-02 20:55:34 +020029#include <osmocom/mgcp/mgcp_ratectr.h>
30
31static const struct rate_ctr_desc mgcp_general_ctr_desc[] = {
32 /* rx_msgs = rx_msgs_retransmitted + rx_msgs_handled + rx_msgs_unhandled + err_rx_msg_parse + err_rx_no_endpoint */
33 [MGCP_GENERAL_RX_MSGS_TOTAL] = { "mgcp:rx_msgs", "total number of MGCP messages received." },
34 [MGCP_GENERAL_RX_MSGS_RETRANSMITTED] = { "mgcp:rx_msgs_retransmitted", "number of received retransmissions." },
35 [MGCP_GENERAL_RX_MSGS_HANDLED] = { "mgcp:rx_msgs_handled", "number of handled MGCP messages." },
36 [MGCP_GENERAL_RX_MSGS_UNHANDLED] = { "mgcp:rx_msgs_unhandled", "number of unhandled MGCP messages." },
37 [MGCP_GENERAL_RX_FAIL_MSG_PARSE] = { "mgcp:err_rx_msg_parse", "error parsing MGCP message." },
38 [MGCP_GENERAL_RX_FAIL_NO_ENDPOINT] =
39 { "mgcp:err_rx_no_endpoint", "can't find MGCP endpoint, probably we've used all allocated endpoints." },
40};
41
42const static struct rate_ctr_group_desc mgcp_general_ctr_group_desc = {
43 .group_name_prefix = "mgcp",
44 .group_description = "mgcp general statistics",
45 .class_id = OSMO_STATS_CLASS_GLOBAL,
46 .num_ctr = ARRAY_SIZE(mgcp_general_ctr_desc),
47 .ctr_desc = mgcp_general_ctr_desc
48};
49
50static const struct rate_ctr_desc mgcp_crcx_ctr_desc[] = {
51 [MGCP_CRCX_SUCCESS] = { "crcx:success", "CRCX command processed successfully." },
52 [MGCP_CRCX_FAIL_BAD_ACTION] = { "crcx:bad_action", "bad action in CRCX command." },
53 [MGCP_CRCX_FAIL_UNHANDLED_PARAM] = { "crcx:unhandled_param", "unhandled parameter in CRCX command." },
54 [MGCP_CRCX_FAIL_MISSING_CALLID] = { "crcx:missing_callid", "missing CallId in CRCX command." },
55 [MGCP_CRCX_FAIL_INVALID_MODE] = { "crcx:invalid_mode", "invalid connection mode in CRCX command." },
56 [MGCP_CRCX_FAIL_LIMIT_EXCEEDED] = { "crcx:limit_exceeded", "limit of concurrent connections was reached." },
57 [MGCP_CRCX_FAIL_UNKNOWN_CALLID] = { "crcx:unkown_callid", "unknown CallId in CRCX command." },
58 [MGCP_CRCX_FAIL_ALLOC_CONN] = { "crcx:alloc_conn_fail", "connection allocation failure." },
59 [MGCP_CRCX_FAIL_NO_REMOTE_CONN_DESC] =
60 { "crcx:no_remote_conn_desc", "no opposite end specified for connection." },
61 [MGCP_CRCX_FAIL_START_RTP] = { "crcx:start_rtp_failure", "failure to start RTP processing." },
62 [MGCP_CRCX_FAIL_REJECTED_BY_POLICY] = { "crcx:conn_rejected", "connection rejected by policy." },
63 [MGCP_CRCX_FAIL_NO_OSMUX] = { "crcx:no_osmux", "no osmux offered by peer." },
64 [MGCP_CRCX_FAIL_INVALID_CONN_OPTIONS] = { "crcx:conn_opt", "connection options invalid." },
65 [MGCP_CRCX_FAIL_CODEC_NEGOTIATION] = { "crcx:codec_nego", "codec negotiation failure." },
66 [MGCP_CRCX_FAIL_BIND_PORT] = { "crcx:bind_port", "port bind failure." },
Philipp Maier8d6a1932020-06-18 12:19:31 +020067 [MGCP_CRCX_FAIL_AVAIL] = { "crcx:unavailable", "endpoint unavailable." },
Philipp Maier889fe7f2020-07-06 17:44:12 +020068 [MGCP_CRCX_FAIL_CLAIM] = { "crcx:claim", "endpoint can not be claimed." },
Philipp Maierc66ab2c2020-06-02 20:55:34 +020069};
70
71const static struct rate_ctr_group_desc mgcp_crcx_ctr_group_desc = {
72 .group_name_prefix = "crcx",
73 .group_description = "crxc statistics",
74 .class_id = OSMO_STATS_CLASS_GLOBAL,
75 .num_ctr = ARRAY_SIZE(mgcp_crcx_ctr_desc),
76 .ctr_desc = mgcp_crcx_ctr_desc
77};
78
79static const struct rate_ctr_desc mgcp_mdcx_ctr_desc[] = {
80 [MGCP_MDCX_SUCCESS] = { "mdcx:success", "MDCX command processed successfully." },
81 [MGCP_MDCX_FAIL_WILDCARD] = { "mdcx:wildcard", "wildcard endpoint names in MDCX commands are unsupported." },
82 [MGCP_MDCX_FAIL_NO_CONN] = { "mdcx:no_conn", "endpoint specified in MDCX command has no active connections." },
83 [MGCP_MDCX_FAIL_INVALID_CALLID] = { "mdcx:callid", "invalid CallId specified in MDCX command." },
84 [MGCP_MDCX_FAIL_INVALID_CONNID] = { "mdcx:connid", "invalid connection ID specified in MDCX command." },
85 [MGCP_MDCX_FAIL_UNHANDLED_PARAM] = { "crcx:unhandled_param", "unhandled parameter in MDCX command." },
86 [MGCP_MDCX_FAIL_NO_CONNID] = { "mdcx:no_connid", "no connection ID specified in MDCX command." },
87 [MGCP_MDCX_FAIL_CONN_NOT_FOUND] =
88 { "mdcx:conn_not_found", "connection specified in MDCX command does not exist." },
89 [MGCP_MDCX_FAIL_INVALID_MODE] = { "mdcx:invalid_mode", "invalid connection mode in MDCX command." },
90 [MGCP_MDCX_FAIL_INVALID_CONN_OPTIONS] = { "mdcx:conn_opt", "connection options invalid." },
91 [MGCP_MDCX_FAIL_NO_REMOTE_CONN_DESC] =
92 { "mdcx:no_remote_conn_desc", "no opposite end specified for connection." },
93 [MGCP_MDCX_FAIL_START_RTP] = { "mdcx:start_rtp_failure", "failure to start RTP processing." },
94 [MGCP_MDCX_FAIL_REJECTED_BY_POLICY] = { "mdcx:conn_rejected", "connection rejected by policy." },
95 [MGCP_MDCX_DEFERRED_BY_POLICY] = { "mdcx:conn_deferred", "connection deferred by policy." },
Philipp Maier8d6a1932020-06-18 12:19:31 +020096 [MGCP_MDCX_FAIL_AVAIL] = { "mdcx:unavailable", "endpoint unavailable." },
Philipp Maierc66ab2c2020-06-02 20:55:34 +020097};
98
99const static struct rate_ctr_group_desc mgcp_mdcx_ctr_group_desc = {
100 .group_name_prefix = "mdcx",
101 .group_description = "mdcx statistics",
102 .class_id = OSMO_STATS_CLASS_GLOBAL,
103 .num_ctr = ARRAY_SIZE(mgcp_mdcx_ctr_desc),
104 .ctr_desc = mgcp_mdcx_ctr_desc
105};
106
107static const struct rate_ctr_desc mgcp_dlcx_ctr_desc[] = {
108 [MGCP_DLCX_SUCCESS] = { "dlcx:success", "DLCX command processed successfully." },
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200109 [MGCP_DLCX_FAIL_NO_CONN] = { "dlcx:no_conn", "endpoint specified in DLCX command has no active connections." },
110 [MGCP_DLCX_FAIL_INVALID_CALLID] =
111 { "dlcx:callid", "CallId specified in DLCX command mismatches endpoint's CallId ." },
112 [MGCP_DLCX_FAIL_INVALID_CONNID] =
113 { "dlcx:connid", "connection ID specified in DLCX command does not exist on endpoint." },
114 [MGCP_DLCX_FAIL_UNHANDLED_PARAM] = { "dlcx:unhandled_param", "unhandled parameter in DLCX command." },
115 [MGCP_DLCX_FAIL_REJECTED_BY_POLICY] = { "dlcx:rejected", "connection deletion rejected by policy." },
116 [MGCP_DLCX_DEFERRED_BY_POLICY] = { "dlcx:deferred", "connection deletion deferred by policy." },
Philipp Maier8d6a1932020-06-18 12:19:31 +0200117 [MGCP_DLCX_FAIL_AVAIL] = { "dlcx:unavailable", "endpoint unavailable." },
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200118};
119
120const static struct rate_ctr_group_desc mgcp_dlcx_ctr_group_desc = {
121 .group_name_prefix = "dlcx",
122 .group_description = "dlcx statistics",
123 .class_id = OSMO_STATS_CLASS_GLOBAL,
124 .num_ctr = ARRAY_SIZE(mgcp_dlcx_ctr_desc),
125 .ctr_desc = mgcp_dlcx_ctr_desc
126};
127
Philipp Maier889fe7f2020-07-06 17:44:12 +0200128static const struct rate_ctr_desc e1_rate_ctr_desc[] = {
Philipp Maier97cae472021-07-09 13:26:47 +0200129 [E1_I460_TRAU_RX_FAIL_CTR] = { "e1:rx_fail", "Inbound I.460 TRAU failures." },
130 [E1_I460_TRAU_TX_FAIL_CTR] = { "e1:tx_fail", "Outbound I.460 TRAU failures." },
131 [E1_I460_TRAU_MUX_EMPTY_CTR] = { "e1:i460", "Outbound I.460 MUX queue empty." }
Philipp Maier889fe7f2020-07-06 17:44:12 +0200132};
133
134const static struct rate_ctr_group_desc e1_rate_ctr_group_desc = {
135 .group_name_prefix = "e1",
136 .group_description = "e1 statistics",
137 .class_id = OSMO_STATS_CLASS_GLOBAL,
138 .num_ctr = ARRAY_SIZE(e1_rate_ctr_desc),
139 .ctr_desc = e1_rate_ctr_desc
140};
141
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200142const static struct rate_ctr_group_desc all_rtp_conn_rate_ctr_group_desc = {
143 .group_name_prefix = "all_rtp_conn",
144 .group_description = "aggregated statistics for all rtp connections",
145 .class_id = 1,
146 .num_ctr = ARRAY_SIZE(all_rtp_conn_rate_ctr_desc),
147 .ctr_desc = all_rtp_conn_rate_ctr_desc
148};
149
150static int free_rate_counter_group(struct rate_ctr_group *rate_ctr_group)
151{
152 rate_ctr_group_free(rate_ctr_group);
153 return 0;
154}
155
Philipp Maiera065e632021-07-09 13:22:42 +0200156/*! allocate global rate counters
157 * (called once at startup).
158 * \param[in] cfg mgw configuration for which the rate counters are allocated.
159 * \returns 0 on success, -EINVAL on failure. */
160int mgcp_ratectr_global_alloc(struct mgcp_config *cfg)
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200161{
Philipp Maiera065e632021-07-09 13:22:42 +0200162 struct mgcp_ratectr_global *ratectr = &cfg->ratectr;
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200163 static unsigned int general_rate_ctr_index = 0;
Philipp Maiera065e632021-07-09 13:22:42 +0200164 char ctr_name[512];
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200165
166 if (ratectr->mgcp_general_ctr_group == NULL) {
167 ratectr->mgcp_general_ctr_group =
Philipp Maiera065e632021-07-09 13:22:42 +0200168 rate_ctr_group_alloc(cfg, &mgcp_general_ctr_group_desc, general_rate_ctr_index);
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200169 if (!ratectr->mgcp_general_ctr_group)
170 return -EINVAL;
Philipp Maiera065e632021-07-09 13:22:42 +0200171 snprintf(ctr_name, sizeof(ctr_name), "%s:general", cfg->domain);
172 rate_ctr_group_set_name(ratectr->mgcp_general_ctr_group, ctr_name);
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200173 talloc_set_destructor(ratectr->mgcp_general_ctr_group, free_rate_counter_group);
174 general_rate_ctr_index++;
175 }
176 return 0;
177}
178
Philipp Maiera065e632021-07-09 13:22:42 +0200179/*! allocate trunk specific rate counters
180 * (called once on trunk initialization).
181 * \param[in] trunk mgw trunk for which the rate counters are allocated.
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200182 * \returns 0 on success, -EINVAL on failure */
Philipp Maiera065e632021-07-09 13:22:42 +0200183int mgcp_ratectr_trunk_alloc(struct mgcp_trunk *trunk)
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200184{
Philipp Maiera065e632021-07-09 13:22:42 +0200185 struct mgcp_ratectr_trunk *ratectr = &trunk->ratectr;
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200186 static unsigned int crcx_rate_ctr_index = 0;
187 static unsigned int mdcx_rate_ctr_index = 0;
188 static unsigned int dlcx_rate_ctr_index = 0;
189 static unsigned int all_rtp_conn_rate_ctr_index = 0;
Philipp Maiera065e632021-07-09 13:22:42 +0200190 char ctr_name[256];
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200191
192 if (ratectr->mgcp_crcx_ctr_group == NULL) {
Philipp Maiera065e632021-07-09 13:22:42 +0200193 ratectr->mgcp_crcx_ctr_group =
194 rate_ctr_group_alloc(trunk, &mgcp_crcx_ctr_group_desc, crcx_rate_ctr_index);
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200195 if (!ratectr->mgcp_crcx_ctr_group)
196 return -EINVAL;
Philipp Maierd70eef62021-07-19 13:53:28 +0200197 snprintf(ctr_name, sizeof(ctr_name), "%s-%u:crcx", mgcp_trunk_type_strs_str(trunk->trunk_type),
Philipp Maiera065e632021-07-09 13:22:42 +0200198 trunk->trunk_nr);
199 rate_ctr_group_set_name(ratectr->mgcp_crcx_ctr_group, ctr_name);
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200200 talloc_set_destructor(ratectr->mgcp_crcx_ctr_group, free_rate_counter_group);
201 crcx_rate_ctr_index++;
202 }
203 if (ratectr->mgcp_mdcx_ctr_group == NULL) {
Philipp Maiera065e632021-07-09 13:22:42 +0200204 ratectr->mgcp_mdcx_ctr_group =
205 rate_ctr_group_alloc(trunk, &mgcp_mdcx_ctr_group_desc, mdcx_rate_ctr_index);
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200206 if (!ratectr->mgcp_mdcx_ctr_group)
207 return -EINVAL;
Philipp Maierd70eef62021-07-19 13:53:28 +0200208 snprintf(ctr_name, sizeof(ctr_name), "%s-%u:mdcx", mgcp_trunk_type_strs_str(trunk->trunk_type),
Philipp Maiera065e632021-07-09 13:22:42 +0200209 trunk->trunk_nr);
210 rate_ctr_group_set_name(ratectr->mgcp_mdcx_ctr_group, ctr_name);
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200211 talloc_set_destructor(ratectr->mgcp_mdcx_ctr_group, free_rate_counter_group);
212 mdcx_rate_ctr_index++;
213 }
214 if (ratectr->mgcp_dlcx_ctr_group == NULL) {
Philipp Maiera065e632021-07-09 13:22:42 +0200215 ratectr->mgcp_dlcx_ctr_group =
216 rate_ctr_group_alloc(trunk, &mgcp_dlcx_ctr_group_desc, dlcx_rate_ctr_index);
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200217 if (!ratectr->mgcp_dlcx_ctr_group)
218 return -EINVAL;
Philipp Maierd70eef62021-07-19 13:53:28 +0200219 snprintf(ctr_name, sizeof(ctr_name), "%s-%u:dlcx", mgcp_trunk_type_strs_str(trunk->trunk_type),
Philipp Maiera065e632021-07-09 13:22:42 +0200220 trunk->trunk_nr);
221 rate_ctr_group_set_name(ratectr->mgcp_dlcx_ctr_group, ctr_name);
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200222 talloc_set_destructor(ratectr->mgcp_dlcx_ctr_group, free_rate_counter_group);
223 dlcx_rate_ctr_index++;
224 }
225 if (ratectr->all_rtp_conn_stats == NULL) {
Philipp Maiera065e632021-07-09 13:22:42 +0200226 ratectr->all_rtp_conn_stats = rate_ctr_group_alloc(trunk, &all_rtp_conn_rate_ctr_group_desc,
227 all_rtp_conn_rate_ctr_index);
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200228 if (!ratectr->all_rtp_conn_stats)
229 return -EINVAL;
Philipp Maierd70eef62021-07-19 13:53:28 +0200230 snprintf(ctr_name, sizeof(ctr_name), "%s-%u:rtp_conn", mgcp_trunk_type_strs_str(trunk->trunk_type),
Philipp Maiera065e632021-07-09 13:22:42 +0200231 trunk->trunk_nr);
232 rate_ctr_group_set_name(ratectr->all_rtp_conn_stats, ctr_name);
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200233 talloc_set_destructor(ratectr->all_rtp_conn_stats, free_rate_counter_group);
234 all_rtp_conn_rate_ctr_index++;
235 }
Philipp Maiera065e632021-07-09 13:22:42 +0200236
237 /* E1 specific */
238 if (trunk->trunk_type == MGCP_TRUNK_E1 && ratectr->e1_stats == NULL) {
239 ratectr->e1_stats = rate_ctr_group_alloc(trunk, &e1_rate_ctr_group_desc, mdcx_rate_ctr_index);
Philipp Maier889fe7f2020-07-06 17:44:12 +0200240 if (!ratectr->e1_stats)
241 return -EINVAL;
Philipp Maierd70eef62021-07-19 13:53:28 +0200242 snprintf(ctr_name, sizeof(ctr_name), "%s-%u:e1", mgcp_trunk_type_strs_str(trunk->trunk_type),
Philipp Maiera065e632021-07-09 13:22:42 +0200243 trunk->trunk_nr);
244 rate_ctr_group_set_name(ratectr->e1_stats, ctr_name);
Philipp Maier889fe7f2020-07-06 17:44:12 +0200245 talloc_set_destructor(ratectr->e1_stats, free_rate_counter_group);
246 mdcx_rate_ctr_index++;
247 }
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200248 return 0;
249}