Philipp Maier | c66ab2c | 2020-06-02 20:55:34 +0200 | [diff] [blame] | 1 | /* 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> |
| 27 | #include <osmocom/mgcp/mgcp_internal.h> |
| 28 | #include <osmocom/mgcp/mgcp_ratectr.h> |
| 29 | |
| 30 | static const struct rate_ctr_desc mgcp_general_ctr_desc[] = { |
| 31 | /* rx_msgs = rx_msgs_retransmitted + rx_msgs_handled + rx_msgs_unhandled + err_rx_msg_parse + err_rx_no_endpoint */ |
| 32 | [MGCP_GENERAL_RX_MSGS_TOTAL] = { "mgcp:rx_msgs", "total number of MGCP messages received." }, |
| 33 | [MGCP_GENERAL_RX_MSGS_RETRANSMITTED] = { "mgcp:rx_msgs_retransmitted", "number of received retransmissions." }, |
| 34 | [MGCP_GENERAL_RX_MSGS_HANDLED] = { "mgcp:rx_msgs_handled", "number of handled MGCP messages." }, |
| 35 | [MGCP_GENERAL_RX_MSGS_UNHANDLED] = { "mgcp:rx_msgs_unhandled", "number of unhandled MGCP messages." }, |
| 36 | [MGCP_GENERAL_RX_FAIL_MSG_PARSE] = { "mgcp:err_rx_msg_parse", "error parsing MGCP message." }, |
| 37 | [MGCP_GENERAL_RX_FAIL_NO_ENDPOINT] = |
| 38 | { "mgcp:err_rx_no_endpoint", "can't find MGCP endpoint, probably we've used all allocated endpoints." }, |
| 39 | }; |
| 40 | |
| 41 | const static struct rate_ctr_group_desc mgcp_general_ctr_group_desc = { |
| 42 | .group_name_prefix = "mgcp", |
| 43 | .group_description = "mgcp general statistics", |
| 44 | .class_id = OSMO_STATS_CLASS_GLOBAL, |
| 45 | .num_ctr = ARRAY_SIZE(mgcp_general_ctr_desc), |
| 46 | .ctr_desc = mgcp_general_ctr_desc |
| 47 | }; |
| 48 | |
| 49 | static const struct rate_ctr_desc mgcp_crcx_ctr_desc[] = { |
| 50 | [MGCP_CRCX_SUCCESS] = { "crcx:success", "CRCX command processed successfully." }, |
| 51 | [MGCP_CRCX_FAIL_BAD_ACTION] = { "crcx:bad_action", "bad action in CRCX command." }, |
| 52 | [MGCP_CRCX_FAIL_UNHANDLED_PARAM] = { "crcx:unhandled_param", "unhandled parameter in CRCX command." }, |
| 53 | [MGCP_CRCX_FAIL_MISSING_CALLID] = { "crcx:missing_callid", "missing CallId in CRCX command." }, |
| 54 | [MGCP_CRCX_FAIL_INVALID_MODE] = { "crcx:invalid_mode", "invalid connection mode in CRCX command." }, |
| 55 | [MGCP_CRCX_FAIL_LIMIT_EXCEEDED] = { "crcx:limit_exceeded", "limit of concurrent connections was reached." }, |
| 56 | [MGCP_CRCX_FAIL_UNKNOWN_CALLID] = { "crcx:unkown_callid", "unknown CallId in CRCX command." }, |
| 57 | [MGCP_CRCX_FAIL_ALLOC_CONN] = { "crcx:alloc_conn_fail", "connection allocation failure." }, |
| 58 | [MGCP_CRCX_FAIL_NO_REMOTE_CONN_DESC] = |
| 59 | { "crcx:no_remote_conn_desc", "no opposite end specified for connection." }, |
| 60 | [MGCP_CRCX_FAIL_START_RTP] = { "crcx:start_rtp_failure", "failure to start RTP processing." }, |
| 61 | [MGCP_CRCX_FAIL_REJECTED_BY_POLICY] = { "crcx:conn_rejected", "connection rejected by policy." }, |
| 62 | [MGCP_CRCX_FAIL_NO_OSMUX] = { "crcx:no_osmux", "no osmux offered by peer." }, |
| 63 | [MGCP_CRCX_FAIL_INVALID_CONN_OPTIONS] = { "crcx:conn_opt", "connection options invalid." }, |
| 64 | [MGCP_CRCX_FAIL_CODEC_NEGOTIATION] = { "crcx:codec_nego", "codec negotiation failure." }, |
| 65 | [MGCP_CRCX_FAIL_BIND_PORT] = { "crcx:bind_port", "port bind failure." }, |
| 66 | }; |
| 67 | |
| 68 | const static struct rate_ctr_group_desc mgcp_crcx_ctr_group_desc = { |
| 69 | .group_name_prefix = "crcx", |
| 70 | .group_description = "crxc statistics", |
| 71 | .class_id = OSMO_STATS_CLASS_GLOBAL, |
| 72 | .num_ctr = ARRAY_SIZE(mgcp_crcx_ctr_desc), |
| 73 | .ctr_desc = mgcp_crcx_ctr_desc |
| 74 | }; |
| 75 | |
| 76 | static const struct rate_ctr_desc mgcp_mdcx_ctr_desc[] = { |
| 77 | [MGCP_MDCX_SUCCESS] = { "mdcx:success", "MDCX command processed successfully." }, |
| 78 | [MGCP_MDCX_FAIL_WILDCARD] = { "mdcx:wildcard", "wildcard endpoint names in MDCX commands are unsupported." }, |
| 79 | [MGCP_MDCX_FAIL_NO_CONN] = { "mdcx:no_conn", "endpoint specified in MDCX command has no active connections." }, |
| 80 | [MGCP_MDCX_FAIL_INVALID_CALLID] = { "mdcx:callid", "invalid CallId specified in MDCX command." }, |
| 81 | [MGCP_MDCX_FAIL_INVALID_CONNID] = { "mdcx:connid", "invalid connection ID specified in MDCX command." }, |
| 82 | [MGCP_MDCX_FAIL_UNHANDLED_PARAM] = { "crcx:unhandled_param", "unhandled parameter in MDCX command." }, |
| 83 | [MGCP_MDCX_FAIL_NO_CONNID] = { "mdcx:no_connid", "no connection ID specified in MDCX command." }, |
| 84 | [MGCP_MDCX_FAIL_CONN_NOT_FOUND] = |
| 85 | { "mdcx:conn_not_found", "connection specified in MDCX command does not exist." }, |
| 86 | [MGCP_MDCX_FAIL_INVALID_MODE] = { "mdcx:invalid_mode", "invalid connection mode in MDCX command." }, |
| 87 | [MGCP_MDCX_FAIL_INVALID_CONN_OPTIONS] = { "mdcx:conn_opt", "connection options invalid." }, |
| 88 | [MGCP_MDCX_FAIL_NO_REMOTE_CONN_DESC] = |
| 89 | { "mdcx:no_remote_conn_desc", "no opposite end specified for connection." }, |
| 90 | [MGCP_MDCX_FAIL_START_RTP] = { "mdcx:start_rtp_failure", "failure to start RTP processing." }, |
| 91 | [MGCP_MDCX_FAIL_REJECTED_BY_POLICY] = { "mdcx:conn_rejected", "connection rejected by policy." }, |
| 92 | [MGCP_MDCX_DEFERRED_BY_POLICY] = { "mdcx:conn_deferred", "connection deferred by policy." }, |
| 93 | }; |
| 94 | |
| 95 | const static struct rate_ctr_group_desc mgcp_mdcx_ctr_group_desc = { |
| 96 | .group_name_prefix = "mdcx", |
| 97 | .group_description = "mdcx statistics", |
| 98 | .class_id = OSMO_STATS_CLASS_GLOBAL, |
| 99 | .num_ctr = ARRAY_SIZE(mgcp_mdcx_ctr_desc), |
| 100 | .ctr_desc = mgcp_mdcx_ctr_desc |
| 101 | }; |
| 102 | |
| 103 | static const struct rate_ctr_desc mgcp_dlcx_ctr_desc[] = { |
| 104 | [MGCP_DLCX_SUCCESS] = { "dlcx:success", "DLCX command processed successfully." }, |
| 105 | [MGCP_DLCX_FAIL_WILDCARD] = { "dlcx:wildcard", "wildcard names in DLCX commands are unsupported." }, |
| 106 | [MGCP_DLCX_FAIL_NO_CONN] = { "dlcx:no_conn", "endpoint specified in DLCX command has no active connections." }, |
| 107 | [MGCP_DLCX_FAIL_INVALID_CALLID] = |
| 108 | { "dlcx:callid", "CallId specified in DLCX command mismatches endpoint's CallId ." }, |
| 109 | [MGCP_DLCX_FAIL_INVALID_CONNID] = |
| 110 | { "dlcx:connid", "connection ID specified in DLCX command does not exist on endpoint." }, |
| 111 | [MGCP_DLCX_FAIL_UNHANDLED_PARAM] = { "dlcx:unhandled_param", "unhandled parameter in DLCX command." }, |
| 112 | [MGCP_DLCX_FAIL_REJECTED_BY_POLICY] = { "dlcx:rejected", "connection deletion rejected by policy." }, |
| 113 | [MGCP_DLCX_DEFERRED_BY_POLICY] = { "dlcx:deferred", "connection deletion deferred by policy." }, |
| 114 | }; |
| 115 | |
| 116 | const static struct rate_ctr_group_desc mgcp_dlcx_ctr_group_desc = { |
| 117 | .group_name_prefix = "dlcx", |
| 118 | .group_description = "dlcx statistics", |
| 119 | .class_id = OSMO_STATS_CLASS_GLOBAL, |
| 120 | .num_ctr = ARRAY_SIZE(mgcp_dlcx_ctr_desc), |
| 121 | .ctr_desc = mgcp_dlcx_ctr_desc |
| 122 | }; |
| 123 | |
| 124 | const static struct rate_ctr_group_desc all_rtp_conn_rate_ctr_group_desc = { |
| 125 | .group_name_prefix = "all_rtp_conn", |
| 126 | .group_description = "aggregated statistics for all rtp connections", |
| 127 | .class_id = 1, |
| 128 | .num_ctr = ARRAY_SIZE(all_rtp_conn_rate_ctr_desc), |
| 129 | .ctr_desc = all_rtp_conn_rate_ctr_desc |
| 130 | }; |
| 131 | |
| 132 | static int free_rate_counter_group(struct rate_ctr_group *rate_ctr_group) |
| 133 | { |
| 134 | rate_ctr_group_free(rate_ctr_group); |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | /*! allocate global rate counters into a given rate counter struct |
| 139 | * (called once at startup) |
| 140 | * \param[in] ctx talloc context. |
| 141 | * \param[out] ratectr struct that holds the counters |
| 142 | * \returns 0 on success, -EINVAL on failure */ |
| 143 | int mgcp_ratectr_global_alloc(void *ctx, struct mgcp_ratectr_global *ratectr) |
| 144 | { |
| 145 | /* FIXME: Each new rate counter group requires a unique index. At the |
| 146 | * moment we generate an index using a counter, but perhaps there is |
| 147 | * a better way of assigning indices? */ |
| 148 | static unsigned int general_rate_ctr_index = 0; |
| 149 | |
| 150 | if (ratectr->mgcp_general_ctr_group == NULL) { |
| 151 | ratectr->mgcp_general_ctr_group = |
| 152 | rate_ctr_group_alloc(ctx, &mgcp_general_ctr_group_desc, general_rate_ctr_index); |
| 153 | if (!ratectr->mgcp_general_ctr_group) |
| 154 | return -EINVAL; |
| 155 | talloc_set_destructor(ratectr->mgcp_general_ctr_group, free_rate_counter_group); |
| 156 | general_rate_ctr_index++; |
| 157 | } |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | /*! allocate trunk specific rate counters into a given rate counter struct |
| 162 | * (called once on trunk initialization) |
| 163 | * \param[in] ctx talloc context. |
| 164 | * \param[out] ratectr struct that holds the counters |
| 165 | * \returns 0 on success, -EINVAL on failure */ |
| 166 | int mgcp_ratectr_trunk_alloc(void *ctx, struct mgcp_ratectr_trunk *ratectr) |
| 167 | { |
| 168 | /* FIXME: see comment in mgcp_ratectr_global_alloc() */ |
| 169 | static unsigned int crcx_rate_ctr_index = 0; |
| 170 | static unsigned int mdcx_rate_ctr_index = 0; |
| 171 | static unsigned int dlcx_rate_ctr_index = 0; |
| 172 | static unsigned int all_rtp_conn_rate_ctr_index = 0; |
| 173 | |
| 174 | if (ratectr->mgcp_crcx_ctr_group == NULL) { |
| 175 | ratectr->mgcp_crcx_ctr_group = rate_ctr_group_alloc(ctx, &mgcp_crcx_ctr_group_desc, crcx_rate_ctr_index); |
| 176 | if (!ratectr->mgcp_crcx_ctr_group) |
| 177 | return -EINVAL; |
| 178 | talloc_set_destructor(ratectr->mgcp_crcx_ctr_group, free_rate_counter_group); |
| 179 | crcx_rate_ctr_index++; |
| 180 | } |
| 181 | if (ratectr->mgcp_mdcx_ctr_group == NULL) { |
| 182 | ratectr->mgcp_mdcx_ctr_group = rate_ctr_group_alloc(ctx, &mgcp_mdcx_ctr_group_desc, mdcx_rate_ctr_index); |
| 183 | if (!ratectr->mgcp_mdcx_ctr_group) |
| 184 | return -EINVAL; |
| 185 | talloc_set_destructor(ratectr->mgcp_mdcx_ctr_group, free_rate_counter_group); |
| 186 | mdcx_rate_ctr_index++; |
| 187 | } |
| 188 | if (ratectr->mgcp_dlcx_ctr_group == NULL) { |
| 189 | ratectr->mgcp_dlcx_ctr_group = rate_ctr_group_alloc(ctx, &mgcp_dlcx_ctr_group_desc, dlcx_rate_ctr_index); |
| 190 | if (!ratectr->mgcp_dlcx_ctr_group) |
| 191 | return -EINVAL; |
| 192 | talloc_set_destructor(ratectr->mgcp_dlcx_ctr_group, free_rate_counter_group); |
| 193 | dlcx_rate_ctr_index++; |
| 194 | } |
| 195 | if (ratectr->all_rtp_conn_stats == NULL) { |
| 196 | ratectr->all_rtp_conn_stats = rate_ctr_group_alloc(ctx, &all_rtp_conn_rate_ctr_group_desc, |
| 197 | all_rtp_conn_rate_ctr_index); |
| 198 | if (!ratectr->all_rtp_conn_stats) |
| 199 | return -EINVAL; |
| 200 | talloc_set_destructor(ratectr->all_rtp_conn_stats, free_rate_counter_group); |
| 201 | all_rtp_conn_rate_ctr_index++; |
| 202 | } |
| 203 | return 0; |
| 204 | } |