Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 1 | /*! \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 Welte | aa00f99 | 2016-12-02 15:30:02 +0100 | [diff] [blame] | 10 | * All Rights Reserved |
| 11 | * |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 12 | * SPDX-License-Identifier: GPL-2.0+ |
| 13 | * |
Harald Welte | aa00f99 | 2016-12-02 15:30:02 +0100 | [diff] [blame] | 14 | * 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 | * |
Harald Welte | aa00f99 | 2016-12-02 15:30:02 +0100 | [diff] [blame] | 24 | */ |
| 25 | |
| 26 | /*! \addtogroup logging |
| 27 | * @{ |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 28 | * \file logging_gsmtap.c */ |
Harald Welte | aa00f99 | 2016-12-02 15:30:02 +0100 | [diff] [blame] | 29 | |
Pau Espin Pedrol | 88955fb | 2023-01-18 18:54:00 +0100 | [diff] [blame] | 30 | #include "config.h" |
Harald Welte | aa00f99 | 2016-12-02 15:30:02 +0100 | [diff] [blame] | 31 | |
| 32 | #include <stdarg.h> |
| 33 | #include <stdlib.h> |
| 34 | #include <stdio.h> |
| 35 | #include <string.h> |
| 36 | #include <stdbool.h> |
Pau Espin Pedrol | bb149ec | 2021-02-15 12:59:48 +0100 | [diff] [blame] | 37 | #include <unistd.h> |
Harald Welte | aa00f99 | 2016-12-02 15:30:02 +0100 | [diff] [blame] | 38 | |
| 39 | #ifdef HAVE_STRINGS_H |
| 40 | #include <strings.h> |
| 41 | #endif |
| 42 | |
| 43 | #include <osmocom/core/talloc.h> |
| 44 | #include <osmocom/core/utils.h> |
| 45 | #include <osmocom/core/gsmtap.h> |
| 46 | #include <osmocom/core/gsmtap_util.h> |
| 47 | #include <osmocom/core/logging.h> |
| 48 | #include <osmocom/core/timer.h> |
Harald Welte | 95871da | 2017-05-15 12:11:36 +0200 | [diff] [blame] | 49 | #include <osmocom/core/byteswap.h> |
Pau Espin Pedrol | 74dddef | 2021-02-17 18:55:44 +0100 | [diff] [blame] | 50 | #include <osmocom/core/thread.h> |
Harald Welte | aa00f99 | 2016-12-02 15:30:02 +0100 | [diff] [blame] | 51 | |
| 52 | #define GSMTAP_LOG_MAX_SIZE 4096 |
| 53 | |
Pau Espin Pedrol | 74dddef | 2021-02-17 18:55:44 +0100 | [diff] [blame] | 54 | static __thread uint32_t logging_gsmtap_tid; |
Pau Espin Pedrol | bb149ec | 2021-02-15 12:59:48 +0100 | [diff] [blame] | 55 | |
Harald Welte | aa00f99 | 2016-12-02 15:30:02 +0100 | [diff] [blame] | 56 | static 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 Hofmeyr | bb62cbc | 2018-01-12 05:38:00 +0100 | [diff] [blame] | 67 | const char *file_basename; |
Harald Welte | aa00f99 | 2016-12-02 15:30:02 +0100 | [diff] [blame] | 68 | |
| 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)); |
Max | 18c014d | 2018-01-30 14:33:01 +0100 | [diff] [blame] | 84 | OSMO_STRLCPY_ARRAY(golh->proc_name, target->tgt_gsmtap.ident); |
Pau Espin Pedrol | 74dddef | 2021-02-17 18:55:44 +0100 | [diff] [blame] | 85 | if (logging_gsmtap_tid == 0) |
| 86 | osmo_store32be((uint32_t)osmo_gettid(), &logging_gsmtap_tid); |
| 87 | golh->pid = logging_gsmtap_tid; |
Harald Welte | aa00f99 | 2016-12-02 15:30:02 +0100 | [diff] [blame] | 88 | if (subsys_name) |
Max | 18c014d | 2018-01-30 14:33:01 +0100 | [diff] [blame] | 89 | OSMO_STRLCPY_ARRAY(golh->subsys, subsys_name + 1); |
Harald Welte | aa00f99 | 2016-12-02 15:30:02 +0100 | [diff] [blame] | 90 | else |
| 91 | golh->subsys[0] = '\0'; |
Neels Hofmeyr | bb62cbc | 2018-01-12 05:38:00 +0100 | [diff] [blame] | 92 | |
| 93 | /* strip all leading path elements from file, if any. */ |
| 94 | file_basename = strrchr(file, '/'); |
| 95 | file = (file_basename && file_basename[1])? file_basename + 1 : file; |
Max | 18c014d | 2018-01-30 14:33:01 +0100 | [diff] [blame] | 96 | OSMO_STRLCPY_ARRAY(golh->src_file.name, file); |
Harald Welte | 95871da | 2017-05-15 12:11:36 +0200 | [diff] [blame] | 97 | golh->src_file.line_nr = osmo_htonl(line); |
Harald Welte | aa00f99 | 2016-12-02 15:30:02 +0100 | [diff] [blame] | 98 | golh->level = level; |
| 99 | /* we always store the timestamp in the message, irrespective |
| 100 | * of hat prrint_[ext_]timestamp say */ |
Harald Welte | 95871da | 2017-05-15 12:11:36 +0200 | [diff] [blame] | 101 | golh->ts.sec = osmo_htonl(tv.tv_sec); |
| 102 | golh->ts.usec = osmo_htonl(tv.tv_usec); |
Harald Welte | aa00f99 | 2016-12-02 15:30:02 +0100 | [diff] [blame] | 103 | |
| 104 | rc = vsnprintf((char *) msg->tail, msgb_tailroom(msg), format, ap); |
Neels Hofmeyr | 4a8a9f4 | 2018-02-16 01:26:30 +0100 | [diff] [blame] | 105 | if (rc < 0) { |
| 106 | msgb_free(msg); |
Harald Welte | aa00f99 | 2016-12-02 15:30:02 +0100 | [diff] [blame] | 107 | return; |
Vadim Yanitskiy | 785ecc9 | 2018-12-28 14:34:52 +0100 | [diff] [blame] | 108 | } else if (rc >= msgb_tailroom(msg)) { |
| 109 | /* If the output was truncated, vsnprintf() returns the |
| 110 | * number of characters which would have been written |
| 111 | * if enough space had been available (excluding '\0'). */ |
| 112 | rc = msgb_tailroom(msg); |
| 113 | msg->tail[rc - 1] = '\0'; |
Neels Hofmeyr | 4a8a9f4 | 2018-02-16 01:26:30 +0100 | [diff] [blame] | 114 | } |
Harald Welte | aa00f99 | 2016-12-02 15:30:02 +0100 | [diff] [blame] | 115 | msgb_put(msg, rc); |
| 116 | |
Neels Hofmeyr | a4952aa | 2018-02-16 01:26:00 +0100 | [diff] [blame] | 117 | rc = gsmtap_sendmsg(target->tgt_gsmtap.gsmtap_inst, msg); |
| 118 | if (rc) |
| 119 | msgb_free(msg); |
Harald Welte | aa00f99 | 2016-12-02 15:30:02 +0100 | [diff] [blame] | 120 | } |
| 121 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 122 | /*! Create a new logging target for GSMTAP logging |
Vadim Yanitskiy | ccf7c7c | 2019-03-25 21:41:10 +0700 | [diff] [blame] | 123 | * \param[in] host remote host to send the logs to |
| 124 | * \param[in] port remote port to send the logs to |
Harald Welte | aa00f99 | 2016-12-02 15:30:02 +0100 | [diff] [blame] | 125 | * \param[in] ident string identifier |
Vadim Yanitskiy | ccf7c7c | 2019-03-25 21:41:10 +0700 | [diff] [blame] | 126 | * \param[in] ofd_wq_mode register osmo_wqueue (1) or not (0) |
| 127 | * \param[in] add_sink add GSMTAP sink or not |
Harald Welte | aa00f99 | 2016-12-02 15:30:02 +0100 | [diff] [blame] | 128 | * \returns Log target in case of success, NULL in case of error |
| 129 | */ |
| 130 | struct log_target *log_target_create_gsmtap(const char *host, uint16_t port, |
| 131 | const char *ident, |
| 132 | bool ofd_wq_mode, |
| 133 | bool add_sink) |
| 134 | { |
| 135 | struct log_target *target; |
| 136 | struct gsmtap_inst *gti; |
| 137 | |
| 138 | target = log_target_create(); |
| 139 | if (!target) |
| 140 | return NULL; |
| 141 | |
| 142 | gti = gsmtap_source_init(host, port, ofd_wq_mode); |
| 143 | if (!gti) { |
| 144 | log_target_destroy(target); |
| 145 | return NULL; |
| 146 | } |
| 147 | |
| 148 | if (add_sink) |
| 149 | gsmtap_source_add_sink(gti); |
| 150 | |
| 151 | target->tgt_gsmtap.gsmtap_inst = gti; |
| 152 | target->tgt_gsmtap.ident = talloc_strdup(target, ident); |
| 153 | target->tgt_gsmtap.hostname = talloc_strdup(target, host); |
| 154 | |
| 155 | target->type = LOG_TGT_TYPE_GSMTAP; |
| 156 | target->raw_output = _gsmtap_raw_output; |
| 157 | |
Harald Welte | aa00f99 | 2016-12-02 15:30:02 +0100 | [diff] [blame] | 158 | return target; |
| 159 | } |
| 160 | |
| 161 | /* @} */ |