Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1 | /* A Media Gateway Control Protocol Media Gateway: RFC 3435 */ |
| 2 | /* The statistics generator */ |
| 3 | |
| 4 | /* |
| 5 | * (C) 2009-2012 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 6 | * (C) 2009-2012 by On-Waves |
| 7 | * (C) 2017 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 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 25 | #include <limits.h> |
Philipp Maier | bca0ef6 | 2018-07-09 17:20:51 +0200 | [diff] [blame] | 26 | #include <inttypes.h> |
Philipp Maier | 993ea6b | 2020-08-04 18:26:50 +0200 | [diff] [blame] | 27 | #include <osmocom/mgcp/mgcp_protocol.h> |
| 28 | #include <osmocom/mgcp/mgcp_conn.h> |
| 29 | #include <osmocom/mgcp/mgcp_stat.h> |
| 30 | #include <osmocom/mgcp/mgcp_endp.h> |
Eric | fbf78d1 | 2021-08-23 22:31:39 +0200 | [diff] [blame] | 31 | #include <osmocom/mgcp/mgcp_trunk.h> |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 32 | |
| 33 | /* Helper function for mgcp_format_stats_rtp() to calculate packet loss */ |
Eric | f936e10 | 2021-09-11 02:02:00 +0200 | [diff] [blame] | 34 | #if defined(__has_attribute) |
| 35 | #if __has_attribute(no_sanitize) |
Pau Espin Pedrol | a0b69f1 | 2021-09-16 13:30:18 +0200 | [diff] [blame] | 36 | __attribute__((no_sanitize("undefined"))) |
Eric | f936e10 | 2021-09-11 02:02:00 +0200 | [diff] [blame] | 37 | #endif |
| 38 | #endif |
Philipp Maier | cede2a4 | 2018-07-03 14:14:21 +0200 | [diff] [blame] | 39 | void calc_loss(struct mgcp_conn_rtp *conn, uint32_t *expected, int *loss) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 40 | { |
Philipp Maier | cede2a4 | 2018-07-03 14:14:21 +0200 | [diff] [blame] | 41 | struct mgcp_rtp_state *state = &conn->state; |
Pau Espin Pedrol | 907744e | 2021-06-04 17:57:34 +0200 | [diff] [blame] | 42 | struct rate_ctr *packets_rx = rate_ctr_group_get_ctr(conn->rate_ctr_group, RTP_PACKETS_RX_CTR); |
Philipp Maier | cede2a4 | 2018-07-03 14:14:21 +0200 | [diff] [blame] | 43 | |
Harald Welte | 49e3d5a | 2017-12-25 09:47:57 +0100 | [diff] [blame] | 44 | *expected = state->stats.cycles + state->stats.max_seq; |
| 45 | *expected = *expected - state->stats.base_seq + 1; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 46 | |
Harald Welte | 49e3d5a | 2017-12-25 09:47:57 +0100 | [diff] [blame] | 47 | if (!state->stats.initialized) { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 48 | *expected = 0; |
| 49 | *loss = 0; |
| 50 | return; |
| 51 | } |
| 52 | |
| 53 | /* |
| 54 | * Make sure the sign is correct and use the biggest |
| 55 | * positive/negative number that fits. |
| 56 | */ |
Philipp Maier | cede2a4 | 2018-07-03 14:14:21 +0200 | [diff] [blame] | 57 | *loss = *expected - packets_rx->current; |
| 58 | if (*expected < packets_rx->current) { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 59 | if (*loss > 0) |
| 60 | *loss = INT_MIN; |
| 61 | } else { |
| 62 | if (*loss < 0) |
| 63 | *loss = INT_MAX; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /* Helper function for mgcp_format_stats_rtp() to calculate jitter */ |
| 68 | uint32_t calc_jitter(struct mgcp_rtp_state *state) |
| 69 | { |
Harald Welte | 49e3d5a | 2017-12-25 09:47:57 +0100 | [diff] [blame] | 70 | if (!state->stats.initialized) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 71 | return 0; |
Harald Welte | 49e3d5a | 2017-12-25 09:47:57 +0100 | [diff] [blame] | 72 | return state->stats.jitter >> 4; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | /* Generate statistics for an RTP connection */ |
| 76 | static void mgcp_format_stats_rtp(char *str, size_t str_len, |
| 77 | struct mgcp_conn_rtp *conn) |
| 78 | { |
| 79 | uint32_t expected, jitter; |
| 80 | int ploss; |
| 81 | int nchars; |
| 82 | |
Pau Espin Pedrol | 907744e | 2021-06-04 17:57:34 +0200 | [diff] [blame] | 83 | struct rate_ctr *packets_rx = rate_ctr_group_get_ctr(conn->rate_ctr_group, RTP_PACKETS_RX_CTR); |
| 84 | struct rate_ctr *octets_rx = rate_ctr_group_get_ctr(conn->rate_ctr_group, RTP_OCTETS_RX_CTR); |
| 85 | struct rate_ctr *packets_tx = rate_ctr_group_get_ctr(conn->rate_ctr_group, RTP_PACKETS_TX_CTR); |
| 86 | struct rate_ctr *octets_tx = rate_ctr_group_get_ctr(conn->rate_ctr_group, RTP_OCTETS_TX_CTR); |
Philipp Maier | cede2a4 | 2018-07-03 14:14:21 +0200 | [diff] [blame] | 87 | |
| 88 | calc_loss(conn, &expected, &ploss); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 89 | jitter = calc_jitter(&conn->state); |
| 90 | |
| 91 | nchars = snprintf(str, str_len, |
Philipp Maier | bca0ef6 | 2018-07-09 17:20:51 +0200 | [diff] [blame] | 92 | "\r\nP: PS=%" PRIu64 ", OS=%" PRIu64 ", PR=%" PRIu64 ", OR=%" PRIu64 ", PL=%d, JI=%u", |
Philipp Maier | cede2a4 | 2018-07-03 14:14:21 +0200 | [diff] [blame] | 93 | packets_tx->current, octets_tx->current, |
| 94 | packets_rx->current, octets_rx->current, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 95 | ploss, jitter); |
| 96 | if (nchars < 0 || nchars >= str_len) |
| 97 | goto truncate; |
| 98 | |
| 99 | str += nchars; |
| 100 | str_len -= nchars; |
| 101 | |
Eric | fbf78d1 | 2021-08-23 22:31:39 +0200 | [diff] [blame] | 102 | if (conn->conn->endp->trunk->cfg->osmux != OSMUX_USAGE_OFF) { |
Pau Espin Pedrol | 2da99a2 | 2018-02-20 13:11:17 +0100 | [diff] [blame] | 103 | /* Error Counter */ |
| 104 | nchars = snprintf(str, str_len, |
Philipp Maier | bca0ef6 | 2018-07-09 17:20:51 +0200 | [diff] [blame] | 105 | "\r\nX-Osmo-CP: EC TI=%" PRIu64 ", TO=%" PRIu64, |
Philipp Maier | 9e1d164 | 2018-05-09 16:26:34 +0200 | [diff] [blame] | 106 | conn->state.in_stream.err_ts_ctr->current, |
| 107 | conn->state.out_stream.err_ts_ctr->current); |
Pau Espin Pedrol | 2da99a2 | 2018-02-20 13:11:17 +0100 | [diff] [blame] | 108 | if (nchars < 0 || nchars >= str_len) |
| 109 | goto truncate; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 110 | |
Pau Espin Pedrol | 2da99a2 | 2018-02-20 13:11:17 +0100 | [diff] [blame] | 111 | str += nchars; |
| 112 | str_len -= nchars; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 113 | |
Pau Espin Pedrol | 2da99a2 | 2018-02-20 13:11:17 +0100 | [diff] [blame] | 114 | if (conn->osmux.state == OSMUX_STATE_ENABLED) { |
| 115 | snprintf(str, str_len, |
| 116 | "\r\nX-Osmux-ST: CR=%u, BR=%u", |
| 117 | conn->osmux.stats.chunks, conn->osmux.stats.octets); |
| 118 | } |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | truncate: |
| 122 | str[str_len - 1] = '\0'; |
| 123 | } |
| 124 | |
| 125 | /*! format statistics into an mgcp parameter string. |
| 126 | * \param[out] str resulting string |
| 127 | * \param[in] str_len length of the string buffer |
| 128 | * \param[in] conn connection to evaluate */ |
| 129 | void mgcp_format_stats(char *str, size_t str_len, struct mgcp_conn *conn) |
| 130 | { |
| 131 | memset(str, 0, str_len); |
| 132 | if (!conn) |
| 133 | return; |
| 134 | |
| 135 | /* NOTE: At the moment we only support generating statistics for |
| 136 | * RTP connections. However, in the future we may also want to |
| 137 | * generate statistics for other connection types as well. Lets |
| 138 | * keep this option open: */ |
| 139 | switch (conn->type) { |
| 140 | case MGCP_CONN_TYPE_RTP: |
| 141 | mgcp_format_stats_rtp(str, str_len, &conn->u.rtp); |
| 142 | break; |
| 143 | default: |
| 144 | break; |
| 145 | } |
| 146 | } |