From 638d3c527e06aed1a281325f4b18591b13b48ed9 Mon Sep 17 00:00:00 2001 From: Audric Date: Sun, 5 Apr 2026 11:55:43 +0200 Subject: [PATCH] Add TRUNK_DEBUG option for verbose trunk connection diagnostics Add TRUNK_DEBUG=1 config option in [GLOBAL] that enables detailed logging of trunk connection lifecycle: state on connect/disconnect, heartbeat countdown warnings near timeout, every non-heartbeat frame with direction and type, send path decisions, and prefix matching details on the trunk server. Helps diagnose connection issues reported in #1 where the remote side kills connections. --- README.md | 30 +++++- src/svxlink/reflector/Reflector.cpp | 49 ++++++++++ src/svxlink/reflector/Reflector.h | 1 + src/svxlink/reflector/TrunkLink.cpp | 103 ++++++++++++++++++++- src/svxlink/reflector/TrunkLink.h | 1 + src/svxlink/reflector/svxreflector.conf.in | 1 + tests/TESTS.md | 2 + tests/generate_configs.py | 1 + 8 files changed, 186 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c6f57cf..6a30194 100644 --- a/README.md +++ b/README.md @@ -151,7 +151,35 @@ Cluster TG numbers must not overlap with any `LOCAL_PREFIX` or `REMOTE_PREFIX`. **Note:** Satellite links are unaffected by cluster TG configuration — they forward all TGs unconditionally (see [Satellite reflectors](#satellite-reflectors)). -### 3. Add a trunk section per peer +### 3. Trunk debug logging (optional) + +Enable verbose trunk logging to diagnose connection issues: + +```ini +[GLOBAL] +TRUNK_DEBUG=1 +``` + +When enabled, the reflector logs detailed information about every trunk +connection state change, including: + +- Connection and disconnection events with full state (both directions' + hello/heartbeat counters, whether the other direction is up) +- Heartbeat countdown warnings as the RX counter approaches timeout +- Every non-heartbeat frame received, with direction (IB/OB), type, and size +- Send path decisions (outbound, fallback to inbound, or dropped) +- Trunk server peer matching: which sections matched or mismatched on HMAC + secret and prefix, making configuration errors immediately visible + +**Warning:** `TRUNK_DEBUG` logs **every audio frame** received over trunk. During +active transmissions this means dozens of lines per second per TG per trunk link +(e.g. ~50 lines/sec with 20ms Opus framing). On a busy reflector with multiple +simultaneous talkers across several trunk peers, this can produce hundreds of +log lines per second and rapidly fill disk. **Do not leave `TRUNK_DEBUG=1` +enabled in production.** Use it only to diagnose a specific issue, then disable +it immediately. + +### 4. Add a trunk section per peer ```ini [TRUNK_2] diff --git a/src/svxlink/reflector/Reflector.cpp b/src/svxlink/reflector/Reflector.cpp index 476f441..88391fd 100644 --- a/src/svxlink/reflector/Reflector.cpp +++ b/src/svxlink/reflector/Reflector.cpp @@ -455,6 +455,18 @@ bool Reflector::initialize(Async::Config &cfg) return true; } + // TRUNK_DEBUG — verbose logging for diagnosing trunk connection issues + std::string trunk_debug_str; + if (cfg.getValue("GLOBAL", "TRUNK_DEBUG", trunk_debug_str)) + { + m_trunk_debug = (trunk_debug_str == "1" || trunk_debug_str == "true" + || trunk_debug_str == "yes"); + if (m_trunk_debug) + { + std::cout << "Trunk debug logging enabled" << std::endl; + } + } + initTrunkLinks(); initTrunkServer(); initSatelliteServer(); @@ -2079,6 +2091,13 @@ void Reflector::trunkClientConnected(Async::FramedTcpConnection* con) std::cout << "TRUNK: Inbound connection from " << con->remoteHost() << ":" << con->remotePort() << std::endl; + if (m_trunk_debug) + { + std::cout << "TRUNK [DEBUG]: pending_count=" << m_trunk_pending_cons.size() + << " ip_pending=" << ip_pending + << " from " << remote_ip << std::endl; + } + // Set up a timeout for receiving the hello message auto* timer = new Async::Timer(10000, Async::Timer::TYPE_ONESHOT); timer->expired.connect( @@ -2187,6 +2206,12 @@ void Reflector::trunkPendingFrameReceived(Async::FramedTcpConnection* con, { if (!msg.verify(link->secret())) { + if (m_trunk_debug) + { + std::cout << "TRUNK [DEBUG]: peer '" << safe_id + << "' HMAC mismatch against " << link->section() + << std::endl; + } continue; } @@ -2214,6 +2239,24 @@ void Reflector::trunkPendingFrameReceived(Async::FramedTcpConnection* con, matched_link = link; break; } + else if (m_trunk_debug) + { + std::string exp_str, peer_str; + for (const auto& p : sorted_expected) + { + if (!exp_str.empty()) exp_str += ","; + exp_str += p; + } + for (const auto& p : sorted_peer) + { + if (!peer_str.empty()) peer_str += ","; + peer_str += p; + } + std::cout << "TRUNK [DEBUG]: peer '" << safe_id + << "' secret OK for " << link->section() + << " but prefix mismatch: expected=[" << exp_str + << "] got=[" << peer_str << "]" << std::endl; + } } if (matched_link == nullptr) @@ -2225,6 +2268,12 @@ void Reflector::trunkPendingFrameReceived(Async::FramedTcpConnection* con, return; } + if (m_trunk_debug) + { + std::cout << "TRUNK [DEBUG]: peer '" << safe_id + << "' matched to " << matched_link->section() << std::endl; + } + // Clean up the pending entry delete pit->second; // timer m_trunk_pending_cons.erase(pit); diff --git a/src/svxlink/reflector/Reflector.h b/src/svxlink/reflector/Reflector.h index 5aac4dd..97f8084 100644 --- a/src/svxlink/reflector/Reflector.h +++ b/src/svxlink/reflector/Reflector.h @@ -299,6 +299,7 @@ class Reflector : public sigc::trackable std::vector m_trunk_links; std::set m_cluster_tgs; + bool m_trunk_debug = false; static const size_t TRUNK_MAX_PENDING_CONS = 5; FramedTcpServer* m_trunk_srv = nullptr; diff --git a/src/svxlink/reflector/TrunkLink.cpp b/src/svxlink/reflector/TrunkLink.cpp index 9ffe5f0..4073b1a 100644 --- a/src/svxlink/reflector/TrunkLink.cpp +++ b/src/svxlink/reflector/TrunkLink.cpp @@ -184,9 +184,17 @@ bool TrunkLink::initialize(void) return false; } + // TRUNK_DEBUG — verbose logging for connection diagnostics + std::string debug_str; + if (m_cfg.getValue("GLOBAL", "TRUNK_DEBUG", debug_str)) + { + m_debug = (debug_str == "1" || debug_str == "true" || debug_str == "yes"); + } + cout << m_section << ": Trunk to " << m_peer_host << ":" << m_peer_port << " local_prefix=" << joinPrefixes(m_local_prefix) - << " remote_prefix=" << joinPrefixes(m_remote_prefix) << endl; + << " remote_prefix=" << joinPrefixes(m_remote_prefix) + << (m_debug ? " [debug on]" : "") << endl; m_con.addStaticSRVRecord(0, 0, 0, m_peer_port, m_peer_host); m_con.setReconnectMinTime(2000); @@ -315,6 +323,16 @@ void TrunkLink::acceptInboundConnection(Async::FramedTcpConnection* con, { cerr << "*** WARNING[" << m_section << "]: Already have an inbound connection, rejecting new one" << endl; + if (m_debug) + { + cerr << m_section << " [DEBUG]: existing inbound from " + << m_inbound_con->remoteHost() << ":" + << m_inbound_con->remotePort() + << " ib_hello=" << m_ib_hello_received + << " ib_hb_rx=" << m_ib_hb_rx_cnt + << " new inbound from " << con->remoteHost() << ":" + << con->remotePort() << endl; + } con->disconnect(); return; } @@ -336,6 +354,14 @@ void TrunkLink::acceptInboundConnection(Async::FramedTcpConnection* con, << ":" << con->remotePort() << " peer='" << hello.id() << "' priority=" << m_peer_priority << endl; + if (m_debug) + { + cout << m_section << " [DEBUG]: inbound state: ob_connected=" + << m_con.isConnected() << " ob_hello=" << m_ob_hello_received + << " ib_hb_tx=" << m_ib_hb_tx_cnt + << " ib_hb_rx=" << m_ib_hb_rx_cnt << endl; + } + // Send our hello back on the inbound connection sendMsgOnInbound(MsgTrunkHello(m_section, joinPrefixes(m_local_prefix), m_priority, m_secret)); @@ -347,11 +373,27 @@ void TrunkLink::onInboundDisconnected(Async::FramedTcpConnection* con, { if (con != m_inbound_con) { + if (m_debug) + { + cerr << m_section << " [DEBUG]: onInboundDisconnected for unknown con " + << con->remoteHost() << ":" << con->remotePort() + << " (m_inbound_con=" + << (m_inbound_con ? "set" : "null") << ")" << endl; + } return; } cout << m_section << ": Inbound trunk connection lost" << endl; + if (m_debug) + { + cout << m_section << " [DEBUG]: inbound lost: ib_hello=" << m_ib_hello_received + << " ib_hb_rx=" << m_ib_hb_rx_cnt + << " ob_connected=" << m_con.isConnected() + << " ob_hello=" << m_ob_hello_received + << " peer_active_tgs=" << m_peer_active_tgs.size() << endl; + } + m_inbound_con = nullptr; m_ib_hello_received = false; m_ib_hb_tx_cnt = 0; @@ -434,6 +476,13 @@ void TrunkLink::onConnected(void) cout << m_section << ": Outbound connected to " << m_con.remoteHost() << ":" << m_con.remotePort() << endl; + if (m_debug) + { + cout << m_section << " [DEBUG]: outbound up: ib_connected=" + << (m_inbound_con != nullptr) << " ib_hello=" << m_ib_hello_received + << " sending hello with priority=" << m_priority << endl; + } + m_ob_hello_received = false; m_ob_hb_tx_cnt = HEARTBEAT_TX_CNT_RESET; m_ob_hb_rx_cnt = HEARTBEAT_RX_CNT_RESET; @@ -450,6 +499,14 @@ void TrunkLink::onDisconnected(TcpConnection* con, cout << m_section << ": Outbound disconnected: " << TcpConnection::disconnectReasonStr(reason) << endl; + if (m_debug) + { + cout << m_section << " [DEBUG]: outbound lost: ob_hello=" << m_ob_hello_received + << " ob_hb_rx=" << m_ob_hb_rx_cnt + << " ib_connected=" << (m_inbound_con != nullptr) + << " ib_hello=" << m_ib_hello_received << endl; + } + m_ob_hello_received = false; m_ob_hb_tx_cnt = 0; m_ob_hb_rx_cnt = 0; @@ -504,6 +561,12 @@ void TrunkLink::onFrameReceived(FramedTcpConnection* con, m_ob_hb_rx_cnt = HEARTBEAT_RX_CNT_RESET; } + if (m_debug && header.type() != MsgTrunkHeartbeat::TYPE) + { + cout << m_section << " [DEBUG]: rx " << (is_inbound ? "IB" : "OB") + << " type=" << header.type() << " len=" << data.size() << endl; + } + switch (header.type()) { case MsgTrunkHeartbeat::TYPE: @@ -574,6 +637,14 @@ void TrunkLink::handleMsgTrunkHello(std::istream& is) << "' local_prefix=" << msg.localPrefix() << " priority=" << m_peer_priority << " (authenticated)" << endl; + + if (m_debug) + { + cout << m_section << " [DEBUG]: hello done: ob_hello=" << m_ob_hello_received + << " ib_connected=" << (m_inbound_con != nullptr) + << " ib_hello=" << m_ib_hello_received + << " isActive=" << isActive() << endl; + } } /* TrunkLink::handleMsgTrunkHello */ @@ -710,8 +781,18 @@ void TrunkLink::sendMsg(const ReflectorMsg& msg) } else if (isInboundReady()) { + if (m_debug) + { + cout << m_section << " [DEBUG]: tx fallback to IB type=" + << msg.type() << endl; + } sendMsgOnInbound(msg); } + else if (m_debug) + { + cerr << m_section << " [DEBUG]: tx dropped type=" << msg.type() + << " (no active connection)" << endl; + } } /* TrunkLink::sendMsg */ @@ -753,6 +834,11 @@ void TrunkLink::heartbeatTick(Async::Timer* t) { if (--m_ob_hb_tx_cnt == 0) { + if (m_debug) + { + cout << m_section << " [DEBUG]: OB heartbeat tx" + << " ob_hb_rx=" << m_ob_hb_rx_cnt << endl; + } sendMsgOnOutbound(MsgTrunkHeartbeat()); } if (--m_ob_hb_rx_cnt == 0) @@ -761,6 +847,11 @@ void TrunkLink::heartbeatTick(Async::Timer* t) << "]: Outbound heartbeat timeout" << endl; m_con.disconnect(); } + else if (m_debug && m_ob_hb_rx_cnt <= 5) + { + cerr << m_section << " [DEBUG]: OB heartbeat rx countdown: " + << m_ob_hb_rx_cnt << endl; + } } // Inbound heartbeat @@ -768,6 +859,11 @@ void TrunkLink::heartbeatTick(Async::Timer* t) { if (--m_ib_hb_tx_cnt == 0) { + if (m_debug) + { + cout << m_section << " [DEBUG]: IB heartbeat tx" + << " ib_hb_rx=" << m_ib_hb_rx_cnt << endl; + } sendMsgOnInbound(MsgTrunkHeartbeat()); } if (--m_ib_hb_rx_cnt == 0) @@ -776,6 +872,11 @@ void TrunkLink::heartbeatTick(Async::Timer* t) << "]: Inbound heartbeat timeout" << endl; m_inbound_con->disconnect(); } + else if (m_debug && m_ib_hb_rx_cnt <= 5) + { + cerr << m_section << " [DEBUG]: IB heartbeat rx countdown: " + << m_ib_hb_rx_cnt << endl; + } } // Prune expired peer interest entries diff --git a/src/svxlink/reflector/TrunkLink.h b/src/svxlink/reflector/TrunkLink.h index e2385d5..eb2cb99 100644 --- a/src/svxlink/reflector/TrunkLink.h +++ b/src/svxlink/reflector/TrunkLink.h @@ -140,6 +140,7 @@ class TrunkLink : public sigc::trackable Reflector* m_reflector; Async::Config& m_cfg; std::string m_section; + bool m_debug = false; std::string m_peer_host; uint16_t m_peer_port; std::string m_secret; diff --git a/src/svxlink/reflector/svxreflector.conf.in b/src/svxlink/reflector/svxreflector.conf.in index 9a2185e..1829120 100644 --- a/src/svxlink/reflector/svxreflector.conf.in +++ b/src/svxlink/reflector/svxreflector.conf.in @@ -94,6 +94,7 @@ CERT_CA_HOOK=@SVX_SHARE_INSTALL_DIR@/ca-hook.py # #LOCAL_PREFIX=1 #TRUNK_LISTEN_PORT=5302 +#TRUNK_DEBUG=0 # verbose trunk logging (high log volume — disable after diagnosis) #[TRUNK_2] #HOST=reflector-b.example.com diff --git a/tests/TESTS.md b/tests/TESTS.md index 989e2b5..c85b0ba 100644 --- a/tests/TESTS.md +++ b/tests/TESTS.md @@ -50,6 +50,8 @@ Two V2 clients are configured on every reflector: A satellite server is enabled on reflector-a (port 5303, secret `sat_secret`). +All test configs have `TRUNK_DEBUG=1` enabled for verbose trunk logging during test runs. + ### Port Mapping | Reflector | Client (TCP+UDP) | Trunk (TCP) | HTTP | Satellite | diff --git a/tests/generate_configs.py b/tests/generate_configs.py index 46daf40..e4b4386 100644 --- a/tests/generate_configs.py +++ b/tests/generate_configs.py @@ -24,6 +24,7 @@ def generate_reflector_conf(name: str) -> str: f"CLUSTER_TGS={cluster_str}", f"TRUNK_LISTEN_PORT={T.INTERNAL_TRUNK_PORT}", f"HTTP_SRV_PORT={T.INTERNAL_HTTP_PORT}", + "TRUNK_DEBUG=1", "", "[SERVER_CERT]", f"COMMON_NAME={T.service_name(name)}",