blob: 89891b592b196acb47c51c96daf06ac333b32029 [file] [log] [blame]
Philipp Maier87bd9be2017-08-22 16:35:41 +02001/* 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 Maier87bd9be2017-08-22 16:35:41 +020025#include <limits.h>
Philipp Maierbca0ef62018-07-09 17:20:51 +020026#include <inttypes.h>
Philipp Maier993ea6b2020-08-04 18:26:50 +020027#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 Maier87bd9be2017-08-22 16:35:41 +020031
32/* Helper function for mgcp_format_stats_rtp() to calculate packet loss */
Philipp Maiercede2a42018-07-03 14:14:21 +020033void calc_loss(struct mgcp_conn_rtp *conn, uint32_t *expected, int *loss)
Philipp Maier87bd9be2017-08-22 16:35:41 +020034{
Philipp Maiercede2a42018-07-03 14:14:21 +020035 struct mgcp_rtp_state *state = &conn->state;
Pau Espin Pedrol907744e2021-06-04 17:57:34 +020036 struct rate_ctr *packets_rx = rate_ctr_group_get_ctr(conn->rate_ctr_group, RTP_PACKETS_RX_CTR);
Philipp Maiercede2a42018-07-03 14:14:21 +020037
Harald Welte49e3d5a2017-12-25 09:47:57 +010038 *expected = state->stats.cycles + state->stats.max_seq;
39 *expected = *expected - state->stats.base_seq + 1;
Philipp Maier87bd9be2017-08-22 16:35:41 +020040
Harald Welte49e3d5a2017-12-25 09:47:57 +010041 if (!state->stats.initialized) {
Philipp Maier87bd9be2017-08-22 16:35:41 +020042 *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 Maiercede2a42018-07-03 14:14:21 +020051 *loss = *expected - packets_rx->current;
52 if (*expected < packets_rx->current) {
Philipp Maier87bd9be2017-08-22 16:35:41 +020053 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 */
62uint32_t calc_jitter(struct mgcp_rtp_state *state)
63{
Harald Welte49e3d5a2017-12-25 09:47:57 +010064 if (!state->stats.initialized)
Philipp Maier87bd9be2017-08-22 16:35:41 +020065 return 0;
Harald Welte49e3d5a2017-12-25 09:47:57 +010066 return state->stats.jitter >> 4;
Philipp Maier87bd9be2017-08-22 16:35:41 +020067}
68
69/* Generate statistics for an RTP connection */
70static 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 Pedrol907744e2021-06-04 17:57:34 +020077 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 Maiercede2a42018-07-03 14:14:21 +020081
82 calc_loss(conn, &expected, &ploss);
Philipp Maier87bd9be2017-08-22 16:35:41 +020083 jitter = calc_jitter(&conn->state);
84
85 nchars = snprintf(str, str_len,
Philipp Maierbca0ef62018-07-09 17:20:51 +020086 "\r\nP: PS=%" PRIu64 ", OS=%" PRIu64 ", PR=%" PRIu64 ", OR=%" PRIu64 ", PL=%d, JI=%u",
Philipp Maiercede2a42018-07-03 14:14:21 +020087 packets_tx->current, octets_tx->current,
88 packets_rx->current, octets_rx->current,
Philipp Maier87bd9be2017-08-22 16:35:41 +020089 ploss, jitter);
90 if (nchars < 0 || nchars >= str_len)
91 goto truncate;
92
93 str += nchars;
94 str_len -= nchars;
95
Pau Espin Pedrol2da99a22018-02-20 13:11:17 +010096 if (conn->conn->endp->cfg->osmux != OSMUX_USAGE_OFF) {
97 /* Error Counter */
98 nchars = snprintf(str, str_len,
Philipp Maierbca0ef62018-07-09 17:20:51 +020099 "\r\nX-Osmo-CP: EC TI=%" PRIu64 ", TO=%" PRIu64,
Philipp Maier9e1d1642018-05-09 16:26:34 +0200100 conn->state.in_stream.err_ts_ctr->current,
101 conn->state.out_stream.err_ts_ctr->current);
Pau Espin Pedrol2da99a22018-02-20 13:11:17 +0100102 if (nchars < 0 || nchars >= str_len)
103 goto truncate;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200104
Pau Espin Pedrol2da99a22018-02-20 13:11:17 +0100105 str += nchars;
106 str_len -= nchars;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200107
Pau Espin Pedrol2da99a22018-02-20 13:11:17 +0100108 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 Maier87bd9be2017-08-22 16:35:41 +0200113 }
114
115truncate:
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 */
123void 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}