blob: 3685cfed0414eab7b090d76ca6b24363ffb91c4f [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
25#include <osmocom/mgcp/mgcp_stat.h>
Pau Espin Pedrol2da99a22018-02-20 13:11:17 +010026#include <osmocom/mgcp/mgcp_endp.h>
Philipp Maier87bd9be2017-08-22 16:35:41 +020027#include <limits.h>
Philipp Maierbca0ef62018-07-09 17:20:51 +020028#include <inttypes.h>
Philipp Maier87bd9be2017-08-22 16:35:41 +020029
30/* Helper function for mgcp_format_stats_rtp() to calculate packet loss */
Philipp Maiercede2a42018-07-03 14:14:21 +020031void calc_loss(struct mgcp_conn_rtp *conn, uint32_t *expected, int *loss)
Philipp Maier87bd9be2017-08-22 16:35:41 +020032{
Philipp Maiercede2a42018-07-03 14:14:21 +020033 struct mgcp_rtp_state *state = &conn->state;
34 struct rate_ctr *packets_rx = &conn->rate_ctr_group->ctr[RTP_PACKETS_RX_CTR];
35
Harald Welte49e3d5a2017-12-25 09:47:57 +010036 *expected = state->stats.cycles + state->stats.max_seq;
37 *expected = *expected - state->stats.base_seq + 1;
Philipp Maier87bd9be2017-08-22 16:35:41 +020038
Harald Welte49e3d5a2017-12-25 09:47:57 +010039 if (!state->stats.initialized) {
Philipp Maier87bd9be2017-08-22 16:35:41 +020040 *expected = 0;
41 *loss = 0;
42 return;
43 }
44
45 /*
46 * Make sure the sign is correct and use the biggest
47 * positive/negative number that fits.
48 */
Philipp Maiercede2a42018-07-03 14:14:21 +020049 *loss = *expected - packets_rx->current;
50 if (*expected < packets_rx->current) {
Philipp Maier87bd9be2017-08-22 16:35:41 +020051 if (*loss > 0)
52 *loss = INT_MIN;
53 } else {
54 if (*loss < 0)
55 *loss = INT_MAX;
56 }
57}
58
59/* Helper function for mgcp_format_stats_rtp() to calculate jitter */
60uint32_t calc_jitter(struct mgcp_rtp_state *state)
61{
Harald Welte49e3d5a2017-12-25 09:47:57 +010062 if (!state->stats.initialized)
Philipp Maier87bd9be2017-08-22 16:35:41 +020063 return 0;
Harald Welte49e3d5a2017-12-25 09:47:57 +010064 return state->stats.jitter >> 4;
Philipp Maier87bd9be2017-08-22 16:35:41 +020065}
66
67/* Generate statistics for an RTP connection */
68static void mgcp_format_stats_rtp(char *str, size_t str_len,
69 struct mgcp_conn_rtp *conn)
70{
71 uint32_t expected, jitter;
72 int ploss;
73 int nchars;
74
Philipp Maiercede2a42018-07-03 14:14:21 +020075 struct rate_ctr *packets_rx = &conn->rate_ctr_group->ctr[RTP_PACKETS_RX_CTR];
76 struct rate_ctr *octets_rx = &conn->rate_ctr_group->ctr[RTP_OCTETS_RX_CTR];
77 struct rate_ctr *packets_tx = &conn->rate_ctr_group->ctr[RTP_PACKETS_TX_CTR];
78 struct rate_ctr *octets_tx = &conn->rate_ctr_group->ctr[RTP_OCTETS_TX_CTR];
79
80 calc_loss(conn, &expected, &ploss);
Philipp Maier87bd9be2017-08-22 16:35:41 +020081 jitter = calc_jitter(&conn->state);
82
83 nchars = snprintf(str, str_len,
Philipp Maierbca0ef62018-07-09 17:20:51 +020084 "\r\nP: PS=%" PRIu64 ", OS=%" PRIu64 ", PR=%" PRIu64 ", OR=%" PRIu64 ", PL=%d, JI=%u",
Philipp Maiercede2a42018-07-03 14:14:21 +020085 packets_tx->current, octets_tx->current,
86 packets_rx->current, octets_rx->current,
Philipp Maier87bd9be2017-08-22 16:35:41 +020087 ploss, jitter);
88 if (nchars < 0 || nchars >= str_len)
89 goto truncate;
90
91 str += nchars;
92 str_len -= nchars;
93
Pau Espin Pedrol2da99a22018-02-20 13:11:17 +010094 if (conn->conn->endp->cfg->osmux != OSMUX_USAGE_OFF) {
95 /* Error Counter */
96 nchars = snprintf(str, str_len,
Philipp Maierbca0ef62018-07-09 17:20:51 +020097 "\r\nX-Osmo-CP: EC TI=%" PRIu64 ", TO=%" PRIu64,
Philipp Maier9e1d1642018-05-09 16:26:34 +020098 conn->state.in_stream.err_ts_ctr->current,
99 conn->state.out_stream.err_ts_ctr->current);
Pau Espin Pedrol2da99a22018-02-20 13:11:17 +0100100 if (nchars < 0 || nchars >= str_len)
101 goto truncate;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200102
Pau Espin Pedrol2da99a22018-02-20 13:11:17 +0100103 str += nchars;
104 str_len -= nchars;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200105
Pau Espin Pedrol2da99a22018-02-20 13:11:17 +0100106 if (conn->osmux.state == OSMUX_STATE_ENABLED) {
107 snprintf(str, str_len,
108 "\r\nX-Osmux-ST: CR=%u, BR=%u",
109 conn->osmux.stats.chunks, conn->osmux.stats.octets);
110 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200111 }
112
113truncate:
114 str[str_len - 1] = '\0';
115}
116
117/*! format statistics into an mgcp parameter string.
118 * \param[out] str resulting string
119 * \param[in] str_len length of the string buffer
120 * \param[in] conn connection to evaluate */
121void mgcp_format_stats(char *str, size_t str_len, struct mgcp_conn *conn)
122{
123 memset(str, 0, str_len);
124 if (!conn)
125 return;
126
127 /* NOTE: At the moment we only support generating statistics for
128 * RTP connections. However, in the future we may also want to
129 * generate statistics for other connection types as well. Lets
130 * keep this option open: */
131 switch (conn->type) {
132 case MGCP_CONN_TYPE_RTP:
133 mgcp_format_stats_rtp(str, str_len, &conn->u.rtp);
134 break;
135 default:
136 break;
137 }
138}