blob: 357b5934396bbcc45333a99c186afc7b70fb95b5 [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 */
Ericf936e102021-09-11 02:02:00 +020033#if defined(__has_attribute)
34#if __has_attribute(no_sanitize)
35__attribute__((no_sanitize("integer")))
36#endif
37#endif
Philipp Maiercede2a42018-07-03 14:14:21 +020038void calc_loss(struct mgcp_conn_rtp *conn, uint32_t *expected, int *loss)
Philipp Maier87bd9be2017-08-22 16:35:41 +020039{
Philipp Maiercede2a42018-07-03 14:14:21 +020040 struct mgcp_rtp_state *state = &conn->state;
Pau Espin Pedrol907744e2021-06-04 17:57:34 +020041 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 +020042
Harald Welte49e3d5a2017-12-25 09:47:57 +010043 *expected = state->stats.cycles + state->stats.max_seq;
44 *expected = *expected - state->stats.base_seq + 1;
Philipp Maier87bd9be2017-08-22 16:35:41 +020045
Harald Welte49e3d5a2017-12-25 09:47:57 +010046 if (!state->stats.initialized) {
Philipp Maier87bd9be2017-08-22 16:35:41 +020047 *expected = 0;
48 *loss = 0;
49 return;
50 }
51
52 /*
53 * Make sure the sign is correct and use the biggest
54 * positive/negative number that fits.
55 */
Philipp Maiercede2a42018-07-03 14:14:21 +020056 *loss = *expected - packets_rx->current;
57 if (*expected < packets_rx->current) {
Philipp Maier87bd9be2017-08-22 16:35:41 +020058 if (*loss > 0)
59 *loss = INT_MIN;
60 } else {
61 if (*loss < 0)
62 *loss = INT_MAX;
63 }
64}
65
66/* Helper function for mgcp_format_stats_rtp() to calculate jitter */
67uint32_t calc_jitter(struct mgcp_rtp_state *state)
68{
Harald Welte49e3d5a2017-12-25 09:47:57 +010069 if (!state->stats.initialized)
Philipp Maier87bd9be2017-08-22 16:35:41 +020070 return 0;
Harald Welte49e3d5a2017-12-25 09:47:57 +010071 return state->stats.jitter >> 4;
Philipp Maier87bd9be2017-08-22 16:35:41 +020072}
73
74/* Generate statistics for an RTP connection */
75static void mgcp_format_stats_rtp(char *str, size_t str_len,
76 struct mgcp_conn_rtp *conn)
77{
78 uint32_t expected, jitter;
79 int ploss;
80 int nchars;
81
Pau Espin Pedrol907744e2021-06-04 17:57:34 +020082 struct rate_ctr *packets_rx = rate_ctr_group_get_ctr(conn->rate_ctr_group, RTP_PACKETS_RX_CTR);
83 struct rate_ctr *octets_rx = rate_ctr_group_get_ctr(conn->rate_ctr_group, RTP_OCTETS_RX_CTR);
84 struct rate_ctr *packets_tx = rate_ctr_group_get_ctr(conn->rate_ctr_group, RTP_PACKETS_TX_CTR);
85 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 +020086
87 calc_loss(conn, &expected, &ploss);
Philipp Maier87bd9be2017-08-22 16:35:41 +020088 jitter = calc_jitter(&conn->state);
89
90 nchars = snprintf(str, str_len,
Philipp Maierbca0ef62018-07-09 17:20:51 +020091 "\r\nP: PS=%" PRIu64 ", OS=%" PRIu64 ", PR=%" PRIu64 ", OR=%" PRIu64 ", PL=%d, JI=%u",
Philipp Maiercede2a42018-07-03 14:14:21 +020092 packets_tx->current, octets_tx->current,
93 packets_rx->current, octets_rx->current,
Philipp Maier87bd9be2017-08-22 16:35:41 +020094 ploss, jitter);
95 if (nchars < 0 || nchars >= str_len)
96 goto truncate;
97
98 str += nchars;
99 str_len -= nchars;
100
Pau Espin Pedrol2da99a22018-02-20 13:11:17 +0100101 if (conn->conn->endp->cfg->osmux != OSMUX_USAGE_OFF) {
102 /* Error Counter */
103 nchars = snprintf(str, str_len,
Philipp Maierbca0ef62018-07-09 17:20:51 +0200104 "\r\nX-Osmo-CP: EC TI=%" PRIu64 ", TO=%" PRIu64,
Philipp Maier9e1d1642018-05-09 16:26:34 +0200105 conn->state.in_stream.err_ts_ctr->current,
106 conn->state.out_stream.err_ts_ctr->current);
Pau Espin Pedrol2da99a22018-02-20 13:11:17 +0100107 if (nchars < 0 || nchars >= str_len)
108 goto truncate;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200109
Pau Espin Pedrol2da99a22018-02-20 13:11:17 +0100110 str += nchars;
111 str_len -= nchars;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200112
Pau Espin Pedrol2da99a22018-02-20 13:11:17 +0100113 if (conn->osmux.state == OSMUX_STATE_ENABLED) {
114 snprintf(str, str_len,
115 "\r\nX-Osmux-ST: CR=%u, BR=%u",
116 conn->osmux.stats.chunks, conn->osmux.stats.octets);
117 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200118 }
119
120truncate:
121 str[str_len - 1] = '\0';
122}
123
124/*! format statistics into an mgcp parameter string.
125 * \param[out] str resulting string
126 * \param[in] str_len length of the string buffer
127 * \param[in] conn connection to evaluate */
128void mgcp_format_stats(char *str, size_t str_len, struct mgcp_conn *conn)
129{
130 memset(str, 0, str_len);
131 if (!conn)
132 return;
133
134 /* NOTE: At the moment we only support generating statistics for
135 * RTP connections. However, in the future we may also want to
136 * generate statistics for other connection types as well. Lets
137 * keep this option open: */
138 switch (conn->type) {
139 case MGCP_CONN_TYPE_RTP:
140 mgcp_format_stats_rtp(str, str_len, &conn->u.rtp);
141 break;
142 default:
143 break;
144 }
145}