blob: fffb10aa92cdc2d40c3901f8f9588c2695054b13 [file] [log] [blame]
Philipp Maierb1ef8f52021-12-06 16:31:02 +01001/*
2 * (C) 2021 by sysmocom - s.f.m.c. GmbH
3 * Author: Philipp Maier <pmaier@sysmocom.de>
4 * All Rights Reserved
5 *
6 * SPDX-License-Identifier: GPL-2.0+
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 */
19
20/*! \addtogroup stats
21 * @{
22 * \file stats_tcp.c */
23
24#include "config.h"
25#if !defined(EMBEDDED)
26
27#include <sys/types.h>
28#include <sys/stat.h>
29#include <sys/socket.h>
30#include <netinet/in.h>
31#include <netinet/ip.h>
32#include <linux/tcp.h>
33#include <errno.h>
34#include <pthread.h>
35
36#include <osmocom/core/select.h>
37#include <osmocom/core/linuxlist.h>
38#include <osmocom/core/talloc.h>
39#include <osmocom/core/utils.h>
40#include <osmocom/core/timer.h>
41#include <osmocom/core/stat_item.h>
42#include <osmocom/core/stats.h>
43#include <osmocom/core/socket.h>
44#include <osmocom/core/stats_tcp.h>
45
46static struct osmo_tcp_stats_config s_tcp_stats_config = {
47 .interval = TCP_STATS_DEFAULT_INTERVAL,
48};
49
50struct osmo_tcp_stats_config *osmo_tcp_stats_config = &s_tcp_stats_config;
51
52static struct osmo_timer_list stats_tcp_poll_timer;
53
54static LLIST_HEAD(stats_tcp);
55static struct stats_tcp_entry *stats_tcp_entry_cur;
56pthread_mutex_t stats_tcp_lock;
57
58struct stats_tcp_entry {
59 struct llist_head entry;
60 const struct osmo_fd *fd;
61 struct osmo_stat_item_group *stats_tcp;
62 const char *name;
63};
64
65enum {
66 STATS_TCP_UNACKED,
67 STATS_TCP_LOST,
68 STATS_TCP_RETRANS,
69 STATS_TCP_RTT,
70 STATS_TCP_RCV_RTT,
71 STATS_TCP_NOTSENT_BYTES,
72 STATS_TCP_RWND_LIMITED,
73 STATS_TCP_SNDBUF_LIMITED,
74 STATS_TCP_REORD_SEEN,
75};
76
77static struct osmo_stat_item_desc stats_tcp_item_desc[] = {
78 [STATS_TCP_UNACKED] = { "tcp:unacked", "unacknowledged packets", "", 60, 0 },
79 [STATS_TCP_LOST] = { "tcp:lost", "lost packets", "", 60, 0 },
80 [STATS_TCP_RETRANS] = { "tcp:retrans", "retransmitted packets", "", 60, 0 },
81 [STATS_TCP_RTT] = { "tcp:rtt", "roundtrip-time", "", 60, 0 },
82 [STATS_TCP_RCV_RTT] = { "tcp:rcv_rtt", "roundtrip-time (receive)", "", 60, 0 },
83 [STATS_TCP_NOTSENT_BYTES] = { "tcp:notsent_bytes", "bytes not yet sent", "", 60, 0 },
84 [STATS_TCP_RWND_LIMITED] = { "tcp:rwnd_limited", "time (usec) limited by receive window", "", 60, 0 },
85 [STATS_TCP_SNDBUF_LIMITED] = { "tcp:sndbuf_limited", "Time (usec) limited by send buffer", "", 60, 0 },
86 [STATS_TCP_REORD_SEEN] = { "tcp:sndbuf_limited", "reordering events seen", "", 60, 0 },
87};
88
89static struct osmo_stat_item_group_desc stats_tcp_desc = {
90 .group_name_prefix = "tcp",
91 .group_description = "stats tcp",
92 .class_id = OSMO_STATS_CLASS_GLOBAL,
93 .num_items = ARRAY_SIZE(stats_tcp_item_desc),
94 .item_desc = stats_tcp_item_desc,
95};
96
97static void fill_stats(struct stats_tcp_entry *stats_tcp_entry)
98{
99 int rc;
100 struct tcp_info tcp_info;
101 socklen_t tcp_info_len = sizeof(tcp_info);
102 char stat_name[256];
103
104 /* Do not fill in anything before the socket is connected to a remote end */
105 if (osmo_sock_get_ip_and_port(stats_tcp_entry->fd->fd, NULL, 0, NULL, 0, false) != 0)
106 return;
107
108 /* Gather TCP statistics and update the stats items */
109 rc = getsockopt(stats_tcp_entry->fd->fd, IPPROTO_TCP, TCP_INFO, &tcp_info, &tcp_info_len);
110 if (rc < 0)
111 return;
112
113 /* Create stats items if they do not exist yet */
114 if (!stats_tcp_entry->stats_tcp) {
115 stats_tcp_entry->stats_tcp =
116 osmo_stat_item_group_alloc(stats_tcp_entry, &stats_tcp_desc, stats_tcp_entry->fd->fd);
117 OSMO_ASSERT(stats_tcp_entry->stats_tcp);
118 }
119
120 /* Update statistics */
121 if (stats_tcp_entry->name)
122 snprintf(stat_name, sizeof(stat_name), "%s,%s", stats_tcp_entry->name,
123 osmo_sock_get_name2(stats_tcp_entry->fd->fd));
124 else
125 snprintf(stat_name, sizeof(stat_name), "%s", osmo_sock_get_name2(stats_tcp_entry->fd->fd));
126 osmo_stat_item_group_set_name(stats_tcp_entry->stats_tcp, stat_name);
127
128 osmo_stat_item_set(osmo_stat_item_group_get_item(stats_tcp_entry->stats_tcp, STATS_TCP_UNACKED),
129 tcp_info.tcpi_unacked);
130 osmo_stat_item_set(osmo_stat_item_group_get_item(stats_tcp_entry->stats_tcp, STATS_TCP_LOST),
131 tcp_info.tcpi_lost);
132 osmo_stat_item_set(osmo_stat_item_group_get_item(stats_tcp_entry->stats_tcp, STATS_TCP_RETRANS),
133 tcp_info.tcpi_retrans);
134 osmo_stat_item_set(osmo_stat_item_group_get_item(stats_tcp_entry->stats_tcp, STATS_TCP_RTT), tcp_info.tcpi_rtt);
135 osmo_stat_item_set(osmo_stat_item_group_get_item(stats_tcp_entry->stats_tcp, STATS_TCP_RCV_RTT),
136 tcp_info.tcpi_rcv_rtt);
137 osmo_stat_item_set(osmo_stat_item_group_get_item(stats_tcp_entry->stats_tcp, STATS_TCP_NOTSENT_BYTES),
138 tcp_info.tcpi_notsent_bytes);
139
140#if HAVE_TCP_INFO_TCPI_RWND_LIMITED == 1
141 osmo_stat_item_set(osmo_stat_item_group_get_item(stats_tcp_entry->stats_tcp, STATS_TCP_RWND_LIMITED),
142 tcp_info.tcpi_rwnd_limited);
143#else
144 osmo_stat_item_set(osmo_stat_item_group_get_item(stats_tcp_entry->stats_tcp, STATS_TCP_RWND_LIMITED), -1);
145#endif
146
147#if STATS_TCP_SNDBUF_LIMITED == 1
148 osmo_stat_item_set(osmo_stat_item_group_get_item(stats_tcp_entry->stats_tcp, STATS_TCP_REORD_SEEN),
149 tcp_info.tcpi_sndbuf_limited);
150#else
151 osmo_stat_item_set(osmo_stat_item_group_get_item(stats_tcp_entry->stats_tcp, STATS_TCP_REORD_SEEN), -1);
152#endif
153
154#if HAVE_TCP_INFO_TCPI_REORD_SEEN == 1
155 osmo_stat_item_set(osmo_stat_item_group_get_item(stats_tcp_entry->stats_tcp, STATS_TCP_REORD_SEEN),
156 tcp_info.tcpi_reord_seen);
157#else
158 osmo_stat_item_set(osmo_stat_item_group_get_item(stats_tcp_entry->stats_tcp, STATS_TCP_REORD_SEEN), -1);
159#endif
160
161}
162
163static bool is_tcp(const struct osmo_fd *fd)
164{
165 int rc;
166 struct stat fd_stat;
167 int so_protocol = 0;
168 socklen_t so_protocol_len = sizeof(so_protocol);
169
170 /* Is this a socket? */
171 rc = fstat(fd->fd, &fd_stat);
172 if (rc < 0)
173 return false;
174 if (!S_ISSOCK(fd_stat.st_mode))
175 return false;
176
177 /* Is it a TCP socket? */
178 rc = getsockopt(fd->fd, SOL_SOCKET, SO_PROTOCOL, &so_protocol, &so_protocol_len);
179 if (rc < 0)
180 return false;
181 if (so_protocol == IPPROTO_TCP)
182 return true;
183
184 return false;
185}
186
187/*! Register an osmo_fd for TCP stats monitoring.
188 * \param[in] fd osmocom file descriptor to be registered.
189 * \param[in] human readbla name that is used as prefix for the related stats item.
190 * \returns 0 on success; negative in case of error. */
191int osmo_stats_tcp_osmo_fd_register(const struct osmo_fd *fd, const char *name)
192{
193 struct stats_tcp_entry *stats_tcp_entry;
194
195 /* Only TCP sockets can be registered for monitoring, anything else will fall through. */
196 if (!is_tcp(fd))
197 return -EINVAL;
198
199 /* When the osmo_fd is registered and unregistered properly there shouldn't be any leftovers from already closed
200 * osmo_fds in the stats_tcp list. But lets proactively make sure that any leftovers are cleaned up. */
201 osmo_stats_tcp_osmo_fd_unregister(fd);
202
203 /* Make a new list object, attach the osmo_fd... */
204 stats_tcp_entry = talloc_zero(OTC_GLOBAL, struct stats_tcp_entry);
205 OSMO_ASSERT(stats_tcp_entry);
206 stats_tcp_entry->fd = fd;
207 stats_tcp_entry->name = talloc_strdup(stats_tcp_entry, name);
208
209 pthread_mutex_lock(&stats_tcp_lock);
210 llist_add_tail(&stats_tcp_entry->entry, &stats_tcp);
211 pthread_mutex_unlock(&stats_tcp_lock);
212
213 return 0;
214}
215
216static void next_stats_tcp_entry(void)
217{
218 struct stats_tcp_entry *last;
219
220 if (llist_empty(&stats_tcp)) {
221 stats_tcp_entry_cur = NULL;
222 return;
223 }
224
225 last = (struct stats_tcp_entry *)llist_last_entry(&stats_tcp, struct stats_tcp_entry, entry);
226
227 if (!stats_tcp_entry_cur || stats_tcp_entry_cur == last)
228 stats_tcp_entry_cur =
229 (struct stats_tcp_entry *)llist_first_entry(&stats_tcp, struct stats_tcp_entry, entry);
230 else
231 stats_tcp_entry_cur =
232 (struct stats_tcp_entry *)llist_entry(stats_tcp_entry_cur->entry.next, struct stats_tcp_entry,
233 entry);
234}
235
236/*! Register an osmo_fd for TCP stats monitoring.
237 * \param[in] fd osmocom file descriptor to be unregistered.
238 * \returns 0 on success; negative in case of error. */
239int osmo_stats_tcp_osmo_fd_unregister(const struct osmo_fd *fd)
240{
241 struct stats_tcp_entry *stats_tcp_entry;
242 int rc = -EINVAL;
243
244 pthread_mutex_lock(&stats_tcp_lock);
245 llist_for_each_entry(stats_tcp_entry, &stats_tcp, entry) {
246 if (fd->fd == stats_tcp_entry->fd->fd) {
247 /* In case we want to remove exactly that item which is also selected as the current itemy, we
248 * must designate either a different item or invalidate the current item. */
249 if (stats_tcp_entry == stats_tcp_entry_cur) {
250 if (llist_count(&stats_tcp) > 2)
251 next_stats_tcp_entry();
252 else
253 stats_tcp_entry_cur = NULL;
254 }
255
256 /* Date item from list */
257 llist_del(&stats_tcp_entry->entry);
258 osmo_stat_item_group_free(stats_tcp_entry->stats_tcp);
259 talloc_free(stats_tcp_entry);
260 rc = 0;
261 break;
262 }
263 }
264 pthread_mutex_unlock(&stats_tcp_lock);
265
266 return rc;
267}
268
269static void stats_tcp_poll_timer_cb(void *data)
270{
271 int i;
272 int batch_size;
273 int llist_size;
274
275 pthread_mutex_lock(&stats_tcp_lock);
276
277 /* Make sure we do not run over the same sockets multiple times if the
278 * configured llist_size is larger then the actual list */
279 batch_size = osmo_tcp_stats_config->batch_size;
280 llist_size = llist_count(&stats_tcp);
281 if (llist_size < batch_size)
282 batch_size = llist_size;
283
284 /* Process a batch of sockets */
285 for (i = 0; i < batch_size; i++) {
286 next_stats_tcp_entry();
287 if (stats_tcp_entry_cur)
288 fill_stats(stats_tcp_entry_cur);
289 }
290
291 pthread_mutex_unlock(&stats_tcp_lock);
292
293 if (osmo_tcp_stats_config->interval > 0)
294 osmo_timer_schedule(&stats_tcp_poll_timer, osmo_tcp_stats_config->interval, 0);
295}
296
297/*! Set the polling interval (common for all sockets)
298 * \param[in] interval Poll interval in seconds
299 * \returns 0 on success; negative on error */
300int osmo_stats_tcp_set_interval(int interval)
301{
302 osmo_tcp_stats_config->interval = interval;
303 if (osmo_tcp_stats_config->interval > 0)
304 osmo_timer_schedule(&stats_tcp_poll_timer, osmo_tcp_stats_config->interval, 0);
305 return 0;
306}
307
308static __attribute__((constructor))
309void on_dso_load_stats_tcp(void)
310{
311 stats_tcp_entry_cur = NULL;
312 pthread_mutex_init(&stats_tcp_lock, NULL);
313
314 osmo_tcp_stats_config->interval = TCP_STATS_DEFAULT_INTERVAL;
315 osmo_tcp_stats_config->batch_size = TCP_STATS_DEFAULT_BATCH_SIZE;
316
317 osmo_timer_setup(&stats_tcp_poll_timer, stats_tcp_poll_timer_cb, NULL);
318}
319
320#endif /* !EMBEDDED */
321
322/* @} */