blob: 82535adca8b5b341ccf1ef99e3820c0ac303c55a [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file logging_gsmtap.c
2 * libosmocore log output encapsulated in GSMTAP.
3 *
4 * Encapsulating the log output inside GSMTAP frames allows us to
5 * observer protocol traces (of Um, Abis, A or any other interface in
6 * the Osmocom world) with synchronous interspersed log messages.
7 */
8/*
9 * (C) 2016 by Harald Welte <laforge@gnumonks.org>
Harald Welteaa00f992016-12-02 15:30:02 +010010 * All Rights Reserved
11 *
Harald Weltee08da972017-11-13 01:00:26 +090012 * SPDX-License-Identifier: GPL-2.0+
13 *
Harald Welteaa00f992016-12-02 15:30:02 +010014 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, write to the Free Software Foundation, Inc.,
26 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 *
28 */
29
30/*! \addtogroup logging
31 * @{
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020032 * \file logging_gsmtap.c */
Harald Welteaa00f992016-12-02 15:30:02 +010033
34#include "../config.h"
35
36#include <stdarg.h>
37#include <stdlib.h>
38#include <stdio.h>
39#include <string.h>
40#include <stdbool.h>
Harald Welteaa00f992016-12-02 15:30:02 +010041
42#ifdef HAVE_STRINGS_H
43#include <strings.h>
44#endif
45
46#include <osmocom/core/talloc.h>
47#include <osmocom/core/utils.h>
48#include <osmocom/core/gsmtap.h>
49#include <osmocom/core/gsmtap_util.h>
50#include <osmocom/core/logging.h>
51#include <osmocom/core/timer.h>
Harald Welte95871da2017-05-15 12:11:36 +020052#include <osmocom/core/byteswap.h>
Harald Welteaa00f992016-12-02 15:30:02 +010053
54#define GSMTAP_LOG_MAX_SIZE 4096
55
56static void _gsmtap_raw_output(struct log_target *target, int subsys,
57 unsigned int level, const char *file,
58 int line, int cont, const char *format,
59 va_list ap)
60{
61 struct msgb *msg;
62 struct gsmtap_hdr *gh;
63 struct gsmtap_osmocore_log_hdr *golh;
64 const char *subsys_name = log_category_name(subsys);
65 struct timeval tv;
66 int rc;
Neels Hofmeyrbb62cbc2018-01-12 05:38:00 +010067 const char *file_basename;
Harald Welteaa00f992016-12-02 15:30:02 +010068
69 /* get timestamp ASAP */
70 osmo_gettimeofday(&tv, NULL);
71
72 msg = msgb_alloc(sizeof(*gh)+sizeof(*golh)+GSMTAP_LOG_MAX_SIZE,
73 "GSMTAP logging");
74
75 /* GSMTAP header */
76 gh = (struct gsmtap_hdr *) msgb_put(msg, sizeof(*gh));
77 memset(gh, 0, sizeof(*gh));
78 gh->version = GSMTAP_VERSION;
79 gh->hdr_len = sizeof(*gh)/4;
80 gh->type = GSMTAP_TYPE_OSMOCORE_LOG;
81
82 /* Logging header */
83 golh = (struct gsmtap_osmocore_log_hdr *) msgb_put(msg, sizeof(*golh));
84 osmo_strlcpy(golh->proc_name, target->tgt_gsmtap.ident,
85 sizeof(golh->proc_name));
86 if (subsys_name)
87 osmo_strlcpy(golh->subsys, subsys_name+1, sizeof(golh->subsys));
88 else
89 golh->subsys[0] = '\0';
Neels Hofmeyrbb62cbc2018-01-12 05:38:00 +010090
91 /* strip all leading path elements from file, if any. */
92 file_basename = strrchr(file, '/');
93 file = (file_basename && file_basename[1])? file_basename + 1 : file;
Harald Welteaa00f992016-12-02 15:30:02 +010094 osmo_strlcpy(golh->src_file.name, file, sizeof(golh->src_file.name));
Harald Welte95871da2017-05-15 12:11:36 +020095 golh->src_file.line_nr = osmo_htonl(line);
Harald Welteaa00f992016-12-02 15:30:02 +010096 golh->level = level;
97 /* we always store the timestamp in the message, irrespective
98 * of hat prrint_[ext_]timestamp say */
Harald Welte95871da2017-05-15 12:11:36 +020099 golh->ts.sec = osmo_htonl(tv.tv_sec);
100 golh->ts.usec = osmo_htonl(tv.tv_usec);
Harald Welteaa00f992016-12-02 15:30:02 +0100101
102 rc = vsnprintf((char *) msg->tail, msgb_tailroom(msg), format, ap);
103 if (rc < 0)
104 return;
105 msgb_put(msg, rc);
106
107 gsmtap_sendmsg(target->tgt_gsmtap.gsmtap_inst, msg);
108}
109
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200110/*! Create a new logging target for GSMTAP logging
Harald Welteaa00f992016-12-02 15:30:02 +0100111 * \param[in] ident string identifier
112 * \returns Log target in case of success, NULL in case of error
113 */
114struct log_target *log_target_create_gsmtap(const char *host, uint16_t port,
115 const char *ident,
116 bool ofd_wq_mode,
117 bool add_sink)
118{
119 struct log_target *target;
120 struct gsmtap_inst *gti;
121
122 target = log_target_create();
123 if (!target)
124 return NULL;
125
126 gti = gsmtap_source_init(host, port, ofd_wq_mode);
127 if (!gti) {
128 log_target_destroy(target);
129 return NULL;
130 }
131
132 if (add_sink)
133 gsmtap_source_add_sink(gti);
134
135 target->tgt_gsmtap.gsmtap_inst = gti;
136 target->tgt_gsmtap.ident = talloc_strdup(target, ident);
137 target->tgt_gsmtap.hostname = talloc_strdup(target, host);
138
139 target->type = LOG_TGT_TYPE_GSMTAP;
140 target->raw_output = _gsmtap_raw_output;
141
142 return target;
143}
144
145/* @} */