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