Skip to content

Commit 88a8a59

Browse files
falucocodebot
authored andcommitted
srslog: Implemented a UDP sink
1 parent 476f87d commit 88a8a59

File tree

5 files changed

+91
-1
lines changed

5 files changed

+91
-1
lines changed

apps/gnb/gnb.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ int main(int argc, char** argv)
383383

384384
// Set up the JSON log channel used by metrics.
385385
srslog::sink& json_sink =
386-
srslog::fetch_file_sink(gnb_cfg.metrics_cfg.json_filename, 0, false, srslog::create_json_formatter());
386+
srslog::fetch_udp_sink("127.0.0.1", 10000, srslog::create_json_formatter());
387387
srslog::log_channel& json_channel = srslog::fetch_log_channel("JSON_channel", json_sink, {});
388388
json_channel.set_enabled(gnb_cfg.metrics_cfg.enable_json_metrics);
389389

configs/gnb_ru_ran550_tdd_n78_20mhz.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
# This example configuration outlines how to configure the srsRAN Project CU/DU to use an O-RU and split 7.2. This is specifically for use
23
# with the Benetel R550 RU. This config will create a single TDD MIMO cell transmitting in band 78, with 20 MHz bandwidth and 30 kHz sub-carrier-spacing.
34
# The parameters used to configure the RU are found in the `ru_ofh` sub-section. This configuration makes used of the OFH Lib from SRS to enable split 7.2.

include/srsran/srslog/srslog.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,11 @@ sink& fetch_syslog_sink(const std::string& preamble_ = "",
191191
syslog_local_type log_local_ = syslog_local_type::local0,
192192
std::unique_ptr<log_formatter> f = get_default_log_formatter());
193193

194+
/// Returns an instance of a sink that writes to a UDP socket using the given remote IP address and port.
195+
sink& fetch_udp_sink(const std::string& remote_ip,
196+
unsigned port,
197+
std::unique_ptr<log_formatter> f = get_default_log_formatter());
198+
194199
/// Installs a custom user defined sink in the framework getting associated to
195200
/// the specified id. Returns true on success, otherwise false.
196201
/// WARNING: This function is an advanced feature and users should really know

lib/srslog/sinks/udp_sink.h

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
*
3+
* Copyright 2021-2023 Software Radio Systems Limited
4+
*
5+
* By using this file, you agree to the terms and conditions set
6+
* forth in the LICENSE file which can be found at the top level of
7+
* the distribution.
8+
*
9+
*/
10+
11+
#pragma once
12+
13+
#include "srsran/srslog/sink.h"
14+
#include <arpa/inet.h>
15+
#include <unistd.h>
16+
17+
namespace srslog {
18+
19+
/// This sink implementation writes to a UDP socket.
20+
class udp_sink : public sink
21+
{
22+
public:
23+
udp_sink(std::string remote_ip_, unsigned port_, std::unique_ptr<log_formatter> f) :
24+
sink(std::move(f)), remote_ip(std::move(remote_ip_)), port(port_)
25+
{
26+
}
27+
28+
~udp_sink() { ::close(socket_fd); }
29+
30+
udp_sink(const udp_sink& other) = delete;
31+
udp_sink& operator=(const udp_sink& other) = delete;
32+
33+
detail::error_string write(detail::memory_buffer buffer) override
34+
{
35+
if (socket_fd == -1) {
36+
socket_fd = ::socket(AF_INET, SOCK_DGRAM, 0);
37+
if (socket_fd < 0) {
38+
return "Cannot create UDP socket";
39+
}
40+
41+
remote_address = {};
42+
remote_address.sin_family = AF_INET;
43+
remote_address.sin_port = ::htons(port);
44+
if (::inet_pton(AF_INET, remote_ip.c_str(), &remote_address.sin_addr) < 1) {
45+
return "Invalid IP address format";
46+
}
47+
}
48+
49+
::sendto(socket_fd,
50+
buffer.data(),
51+
buffer.size(),
52+
0,
53+
reinterpret_cast<const struct sockaddr*>(&remote_address),
54+
sizeof(remote_address));
55+
56+
return {};
57+
}
58+
59+
detail::error_string flush() override { return {}; }
60+
61+
private:
62+
std::string remote_ip;
63+
unsigned port;
64+
int socket_fd = -1;
65+
::sockaddr_in remote_address;
66+
};
67+
68+
} // namespace srslog

lib/srslog/srslog.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "formatters/json_formatter.h"
1313
#include "sinks/file_sink.h"
1414
#include "sinks/syslog_sink.h"
15+
#include "sinks/udp_sink.h"
1516
#include "srslog_instance.h"
1617

1718
using namespace srslog;
@@ -186,6 +187,21 @@ sink& srslog::fetch_syslog_sink(const std::string& preamble_,
186187
return *s;
187188
}
188189

190+
sink& srslog::fetch_udp_sink(const std::string& remote_ip, unsigned port, std::unique_ptr<log_formatter> f)
191+
{
192+
std::string id = remote_ip + ':' + std::to_string(port);
193+
if (auto* s = find_sink(id)) {
194+
return *s;
195+
}
196+
197+
auto& s = srslog_instance::get().get_sink_repo().emplace(
198+
std::piecewise_construct,
199+
std::forward_as_tuple(id),
200+
std::forward_as_tuple(new udp_sink(remote_ip, port, std::move(f))));
201+
202+
return *s;
203+
}
204+
189205
bool srslog::install_custom_sink(const std::string& id, std::unique_ptr<sink> s)
190206
{
191207
assert(!id.empty() && "Empty path string");

0 commit comments

Comments
 (0)