GCC Code Coverage Report


Directory: ./
File: channeloutput/UDPOutput.cpp
Date: 2026-12-01 12:27:28
Exec Total Coverage
Lines: 1103 1176 93.8%
Functions: 73 75 97.3%
Branches: 1317 2066 63.7%

Line Branch Exec Source
1 /*
2 * This file is part of the Falcon Player (FPP) and is Copyright (C)
3 * 2013-2022 by the Falcon Player Developers.
4 *
5 * The Falcon Player (FPP) is free software, and is covered under
6 * multiple Open Source licenses. Please see the included 'LICENSES'
7 * file for descriptions of what files are covered by each license.
8 *
9 * This source file is covered under the GPL v2 as described in the
10 * included LICENSE.GPL file.
11 */
12
13 #include "fpp-pch.h"
14
15 #include "fpp-json.h"
16
17 #ifndef PLATFORM_OSX
18 #include <linux/sockios.h>
19 #include <sys/ioctl.h>
20 #endif
21
22 #include <arpa/inet.h>
23 #include <netinet/in.h>
24 #include <sys/socket.h>
25 #include <ifaddrs.h>
26 #include <netdb.h>
27 #include <unistd.h>
28
29 #include <curl/curl.h>
30 #include <cmath> // ceil -- pacing-budget warning rate (needed directly for NOPCH builds)
31 #include <algorithm> // find_if -- one pending work item per destination (needed directly for NOPCH builds)
32 #include <filesystem>
33 #include <set>
34 #include <sstream>
35
36 #include "../CurlManager.h"
37 #include "../Sequence.h"
38 #include "../Warnings.h"
39 #include "../common.h"
40 #include "../log.h"
41 #include "../settings.h"
42
43 #include "UDPOutput.h"
44 #include "ChannelOutputSetup.h"
45 #include "ping.h"
46
47 #include "NetworkMonitor.h"
48
49 #include "ArtNet.h"
50 #include "DDP.h"
51 #include "E131.h"
52 #include "KiNet.h"
53 #include "Twinkly.h"
54 #include "../e131bridge.h"
55
56 #include "Plugin.h"
57
58 constexpr int UDP_PING_TIMEOUT = 250;
59
60 class UDPPlugin : public FPPPlugins::Plugin, public FPPPlugins::ChannelOutputPlugin {
61 public:
62 979 UDPPlugin() :
63
2/4
✓ Branch 0 taken 979 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 979 times.
✗ Branch 3 not taken.
2937 FPPPlugins::Plugin("UDP") {
64 979 }
65 979 virtual ChannelOutput* createChannelOutput(unsigned int startChannel, unsigned int channelCount) override {
66
2/6
✓ Branch 0 taken 979 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 979 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
1958 return new UDPOutput(startChannel, channelCount);
67 }
68 };
69
70 extern "C" {
71 979 FPPPlugins::Plugin* createPlugin() {
72
2/6
✓ Branch 0 taken 979 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 979 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
1958 return new UDPPlugin();
73 }
74 }
75
76 85 static inline std::string createWarning(const std::string& host, const std::string& type, const std::string& description) {
77
5/10
✓ Branch 0 taken 85 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 85 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 85 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 85 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 85 times.
✗ Branch 9 not taken.
85 return "Cannot Ping " + type + " Channel Data Target " + host + " " + description;
78 }
79
80 struct UDPOutput::PingHost {
81 std::string address;
82 std::vector<UDPOutputData*> outputs;
83 std::string warning;
84 // the ping thread and the HEAD probe's callback (main loop) both update it
85 std::mutex lock;
86 int failCount = 0;
87 bool answered = false;
88 bool valid = true;
89 bool headPending = false;
90 };
91
92 // Interface names that reach shells (interfaceHasFQ's tc popen below) and
93 // sysfs paths must be plausible kernel names: IFNAMSIZ-1 chars, no '/' and no
94 // leading '.'/'-'. The configured value is operator-editable JSON. Deliberately
95 // platform-independent (used on all targets); manual char checks avoid any new
96 // header dependency.
97 1924 static bool isValidInterfaceName(const std::string& n) {
98
5/6
✓ Branch 0 taken 1924 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1923 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 1923 times.
1924 if (n.empty() || n.size() > 15) {
99 1 return false;
100 }
101 8091 auto ok = [](char c) {
102
8/18
✓ Branch 0 taken 7027 times.
✓ Branch 1 taken 1064 times.
✓ Branch 2 taken 7027 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 7027 times.
✓ Branch 5 taken 1064 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 7027 times.
✓ Branch 8 taken 1064 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 1064 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
8091 return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '_' || c == '.' || c == '-';
103 };
104
4/8
✓ Branch 0 taken 1923 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1923 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1923 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 1923 times.
1923 if (!ok(n[0]) || n[0] == '.' || n[0] == '-') {
105 ✗ return false;
106 }
107
2/2
✓ Branch 0 taken 6168 times.
✓ Branch 1 taken 1923 times.
8091 for (char c : n) {
108
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6168 times.
6168 if (!ok(c)) {
109 ✗ return false;
110 }
111 }
112 1923 return true;
113 }
114
115 #ifndef PLATFORM_OSX
116 // SO_MAX_PACING_RATE only works if the interface's root qdisc is fq.
117 // Some kernels expose the qdisc name in sysfs; fall back to asking tc.
118 1897 static bool queryInterfaceHasFQ(const std::string& iface) {
119
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1897 times.
1897 if (!isValidInterfaceName(iface)) {
120 ✗ return false;
121 }
122
3/6
✓ Branch 0 taken 1897 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1897 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1897 times.
✗ Branch 5 not taken.
1897 std::string qdisc = GetFileContents("/sys/class/net/" + iface + "/qdisc");
123
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1897 times.
1897 if (!qdisc.empty()) {
124 ✗ TrimWhiteSpace(qdisc);
125 ✗ return qdisc == "fq";
126 }
127 // some kernels don't expose the qdisc name in sysfs; ask tc
128
4/8
✓ Branch 0 taken 1897 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1897 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1897 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1897 times.
✗ Branch 7 not taken.
1897 std::string cmd = TcPath() + " qdisc show dev " + iface + " 2>/dev/null";
129
1/2
✓ Branch 0 taken 1897 times.
✗ Branch 1 not taken.
1897 FILE* pipe = popen(cmd.c_str(), "r");
130
1/2
✓ Branch 0 taken 1897 times.
✗ Branch 1 not taken.
1897 if (pipe) {
131 char buf[256];
132
3/4
✓ Branch 0 taken 4680 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2783 times.
✓ Branch 3 taken 1897 times.
4680 while (fgets(buf, sizeof(buf), pipe)) {
133
4/4
✓ Branch 0 taken 1841 times.
✓ Branch 1 taken 942 times.
✓ Branch 2 taken 949 times.
✓ Branch 3 taken 892 times.
2783 if (strstr(buf, " root ") && strncmp(buf, "qdisc fq ", 9) == 0) {
134
1/2
✓ Branch 0 taken 949 times.
✗ Branch 1 not taken.
949 qdisc = "fq";
135 }
136 }
137
1/2
✓ Branch 0 taken 1897 times.
✗ Branch 1 not taken.
1897 pclose(pipe);
138 }
139
1/2
✓ Branch 0 taken 1897 times.
✗ Branch 1 not taken.
1897 return qdisc == "fq";
140 1897 }
141 // The answer per interface, because the sysfs file does not exist on the
142 // kernels FPP ships (6.x), so every query forks tc - and findOrCreateSocket()
143 // asks once per destination, on the output thread, at startup, on every
144 // reload and after every address loss. fppinit sets the qdisc at boot, so
145 // it does not change under a running output; UDPOutput::Init() clears the
146 // cache and warms it from the main thread.
147 static std::mutex fqCacheLock;
148 static std::map<std::string, bool> fqCache;
149 1013 static void clearFQCache() {
150
1/2
✓ Branch 0 taken 1013 times.
✗ Branch 1 not taken.
1013 std::lock_guard<std::mutex> lk(fqCacheLock);
151 1013 fqCache.clear();
152 1013 }
153 2984 static bool interfaceHasFQ(const std::string& iface) {
154 {
155
1/2
✓ Branch 0 taken 2984 times.
✗ Branch 1 not taken.
2984 std::lock_guard<std::mutex> lk(fqCacheLock);
156
1/2
✓ Branch 0 taken 2984 times.
✗ Branch 1 not taken.
2984 auto it = fqCache.find(iface);
157
2/2
✓ Branch 0 taken 1087 times.
✓ Branch 1 taken 1897 times.
2984 if (it != fqCache.end()) {
158 1087 return it->second;
159 }
160
2/2
✓ Branch 0 taken 1897 times.
✓ Branch 1 taken 1087 times.
2984 }
161 // not under the lock: tc can take a while, and a duplicate query by
162 // another thread is harmless
163
1/2
✓ Branch 0 taken 1897 times.
✗ Branch 1 not taken.
1897 bool fq = queryInterfaceHasFQ(iface);
164
1/2
✓ Branch 0 taken 1897 times.
✗ Branch 1 not taken.
1897 std::lock_guard<std::mutex> lk(fqCacheLock);
165
1/2
✓ Branch 0 taken 1897 times.
✗ Branch 1 not taken.
1897 fqCache[iface] = fq;
166 1897 return fq;
167 1897 }
168 // Unicast sockets follow the routing table, not the configured output
169 // interface, and the interface scan at startup can race the network coming
170 // up. fppinit installs fq on every ethernet interface at boot, so consider
171 // pacing available if any of them has it. Whether a specific destination's
172 // socket is actually paced is decided per destination by looking up the
173 // egress interface of its route (see findOrCreateSocket).
174 1948 static bool anyEthernetHasFQ(const std::string& primary) {
175
5/6
✓ Branch 0 taken 1948 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1856 times.
✓ Branch 3 taken 92 times.
✓ Branch 4 taken 1856 times.
✓ Branch 5 taken 92 times.
1948 if (!primary.empty() && interfaceHasFQ(primary)) {
176 1856 return true;
177 }
178
5/8
✓ Branch 0 taken 92 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 92 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 106 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 135 times.
✓ Branch 7 taken 63 times.
198 for (const auto& entry : std::filesystem::directory_iterator("/sys/class/net/")) {
179
2/4
✓ Branch 0 taken 135 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 135 times.
✗ Branch 3 not taken.
135 std::string dev = entry.path().filename();
180
10/12
✓ Branch 0 taken 135 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 40 times.
✓ Branch 3 taken 95 times.
✓ Branch 4 taken 30 times.
✓ Branch 5 taken 10 times.
✓ Branch 6 taken 30 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 29 times.
✓ Branch 9 taken 1 times.
✓ Branch 10 taken 29 times.
✓ Branch 11 taken 106 times.
135 if (IsShowEthernetInterface(dev) && dev != primary && interfaceHasFQ(dev)) {
181 29 return true;
182 }
183
8/8
✓ Branch 0 taken 106 times.
✓ Branch 1 taken 29 times.
✓ Branch 2 taken 63 times.
✓ Branch 3 taken 29 times.
✓ Branch 4 taken 63 times.
✓ Branch 5 taken 29 times.
✓ Branch 6 taken 63 times.
✓ Branch 7 taken 29 times.
285 }
184 63 return false;
185 }
186 // Send buffer for paced sockets: the kernel silently caps SO_SNDBUF at
187 // wmem_max (set by fppinit at boot), so ask for exactly what it will grant.
188 118 static int pacedSndBufSize() {
189 148 static int size = []() {
190
2/4
✓ Branch 0 taken 74 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 74 times.
✗ Branch 3 not taken.
74 int v = atoi(GetFileContents("/proc/sys/net/core/wmem_max").c_str());
191
1/2
✓ Branch 0 taken 74 times.
✗ Branch 1 not taken.
74 return v > 0 ? v : 393216;
192
4/8
✓ Branch 0 taken 74 times.
✓ Branch 1 taken 44 times.
✓ Branch 2 taken 74 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 74 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
118 }();
193 118 return size;
194 }
195 // Which interface will packets to this destination actually leave through?
196 // A connect() on a UDP socket does the route lookup without sending anything.
197 // (The address->interface tail is intentionally not FindInterfaceForIP(): that
198 // helper returns a static buffer and round-trips through strings; comparing
199 // s_addr directly is thread-safe and exact.)
200 1011 static std::string egressInterfaceFor(in_addr_t dest) {
201 1011 int s = OpenSocket(AF_INET, SOCK_DGRAM, 0);
202
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1011 times.
1011 if (s < 0) {
203 ✗ return "";
204 }
205 struct sockaddr_in sa;
206 1011 memset(&sa, 0, sizeof(sa));
207 1011 sa.sin_family = AF_INET;
208 1011 sa.sin_port = htons(53);
209 1011 sa.sin_addr.s_addr = dest;
210 1011 std::string ret;
211
3/4
✓ Branch 0 taken 1011 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1007 times.
✓ Branch 3 taken 4 times.
1011 if (connect(s, (struct sockaddr*)&sa, sizeof(sa)) == 0) {
212 struct sockaddr_in local;
213 1007 socklen_t len = sizeof(local);
214
1/2
✓ Branch 0 taken 1007 times.
✗ Branch 1 not taken.
1007 if (getsockname(s, (struct sockaddr*)&local, &len) == 0) {
215 struct ifaddrs *interfaces, *tmp;
216
1/2
✓ Branch 0 taken 1007 times.
✗ Branch 1 not taken.
1007 if (getifaddrs(&interfaces) == 0) {
217
1/2
✓ Branch 0 taken 3347 times.
✗ Branch 1 not taken.
3347 for (tmp = interfaces; tmp; tmp = tmp->ifa_next) {
218
3/4
✓ Branch 0 taken 3347 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1237 times.
✓ Branch 3 taken 2110 times.
3347 if (tmp->ifa_addr && tmp->ifa_addr->sa_family == AF_INET &&
219
2/2
✓ Branch 0 taken 1007 times.
✓ Branch 1 taken 230 times.
1237 ((struct sockaddr_in*)tmp->ifa_addr)->sin_addr.s_addr == local.sin_addr.s_addr) {
220
1/2
✓ Branch 0 taken 1007 times.
✗ Branch 1 not taken.
1007 ret = tmp->ifa_name;
221 1007 break;
222 }
223 }
224 1007 freeifaddrs(interfaces);
225 }
226 }
227 }
228
1/2
✓ Branch 0 taken 1011 times.
✗ Branch 1 not taken.
1011 close(s);
229 1011 return ret;
230 1011 }
231 #endif
232
233 // Sites that log a failed send. A destination that is persistently behind
234 // fails every frame, and logging each one wrote 1.4 million lines (360MB of
235 // fppd.log) on one player, so each site logs a destination at most once per
236 // SEND_ERR_LOG_INTERVAL_MS. CheckPacingBudget() reports the totals.
237 enum SendErrLogSite { SEND_ERR_BUDGET,
238 SEND_ERR_EAGAIN,
239 SEND_ERR_FRAME,
240 SEND_ERR_SITES };
241 static constexpr long long SEND_ERR_LOG_INTERVAL_MS = 10000;
242
243 class SendSocketInfo {
244 public:
245 1076 SendSocketInfo() {
246 1076 errCount = 0;
247 1076 curSocket = -1;
248 1076 }
249 ~SendSocketInfo();
250
251 // Never holds -1: a socket that couldn't be opened (out of descriptors)
252 // is left out, so the list stays empty and findOrCreateSocket() tries
253 // again on the next frame. A -1 kept here made the list look complete,
254 // and every later send failed on it with EBADF until the config reloaded.
255 std::vector<int> sockets;
256 // read/written by workers and the frame thread without a common lock;
257 // atomic so the errCount >= 3 re-ping trigger can't act on a torn value.
258 // A counter only, so relaxed throughout.
259 std::atomic_int errCount;
260 // Runs on every successful send, per destination per frame: skip the
261 // store (two barriers on an AM335x) when there is nothing to clear.
262 166572 void clearErrors() {
263
2/2
✓ Branch 0 taken 466 times.
✓ Branch 1 taken 166106 times.
333144 if (errCount.load(std::memory_order_relaxed) != 0)
264 466 errCount.store(0, std::memory_order_relaxed);
265 166572 }
266 int curSocket;
267 bool preventClose = false;
268 // kernel (fq) pacing is active on these sockets; drain waits are the
269 // qdisc's job and EAGAIN means "wait", not "get another socket".
270 // pacedChecked latches once the destination's route has resolved; until
271 // then (e.g. interface still waiting on DHCP at startup) the pacing
272 // decision is re-attempted periodically (pacedCheckCountdown frames).
273 bool paced = false;
274 bool pacedChecked = false;
275 int pacedCheckCountdown = 0;
276 // Serializes all use/mutation of sockets/curSocket (errCount is atomic
277 // and accessed outside the lock). A worker that outlives its frame
278 // (stuck send) can still be inside SendMessages when the next frame's
279 // worker gets the same destination, and SendMessages rotates curSocket
280 // and grows the sockets vector on EAGAIN.
281 std::mutex sendLock;
282
283 // Pacing-budget accounting, read and reset by CheckPacingBudget().
284 // pacedRate is the rate (bytes/sec) the kernel clocks this destination at,
285 // 0 while it isn't paced. framesBehind counts frames that were folded
286 // into a still-queued item or left out because the destination was still
287 // busy (late), or whose send came up short. Counted per destination per
288 // frame, so the increments are relaxed: statistics, nothing published
289 // through them.
290 std::atomic_uint pacedRate{ 0 };
291 std::atomic<uint64_t> bytesQueued{ 0 };
292 std::atomic_uint framesQueued{ 0 };
293 std::atomic_uint framesBehind{ 0 };
294 // the size of the last frame queued for this destination, which a frame
295 // it misses would have been; frame thread only, under socketMutex
296 uint64_t lastFrameBytes = 0;
297 // only the stats thread touches these, and only one runs at a time.
298 // budgetWarning is the pacing-budget banner raised for this destination;
299 // it comes down with this record (see ~SendSocketInfo).
300 std::chrono::steady_clock::time_point budgetCheckTime = std::chrono::steady_clock::now();
301 std::string budgetWarning;
302
303 std::atomic<long long> lastSendErrLog[SEND_ERR_SITES] = {};
304 };
305
306 3669 static bool ShouldLogSendError(SendSocketInfo* si, SendErrLogSite site) {
307
1/2
✓ Branch 0 taken 3669 times.
✗ Branch 1 not taken.
3669 long long now = GetMonotonicTimeMS(); // the wall clock stepping back silenced it
308 3669 long long last = si->lastSendErrLog[site].load();
309
5/6
✓ Branch 0 taken 3634 times.
✓ Branch 1 taken 35 times.
✓ Branch 2 taken 44 times.
✓ Branch 3 taken 3590 times.
✓ Branch 4 taken 79 times.
✗ Branch 5 not taken.
7417 return (last == 0 || now - last >= SEND_ERR_LOG_INTERVAL_MS) && si->lastSendErrLog[site].compare_exchange_strong(last, now);
310 }
311
312 151812 static uint64_t MessageBytes(const std::vector<struct mmsghdr>& msgs) {
313 151812 uint64_t bytes = 0;
314
2/2
✓ Branch 0 taken 943300 times.
✓ Branch 1 taken 151812 times.
1095112 for (auto& m : msgs) {
315
2/2
✓ Branch 0 taken 1879698 times.
✓ Branch 1 taken 943300 times.
2822998 for (size_t v = 0; v < m.msg_hdr.msg_iovlen; v++)
316 1879698 bytes += m.msg_hdr.msg_iov[v].iov_len;
317 }
318 151812 return bytes;
319 }
320
321 // A destination that needs more than its pacing rate can't be fixed from here:
322 // the kernel clocks its packets out at that rate, so each frame finishes late
323 // or is cut off at the send budget, and the lights on that controller stutter.
324 // Nothing else tells the user. The packets wait in our own socket, not in a
325 // queue that drops them, so the local-drop check never sees them.
326 static constexpr int PACING_BUDGET_WARNING_ID = 68;
327 // Re-raised on every check (every 1500 frames, 40-75 s at typical rates) and
328 // removed by the first clean one, so this only matters once output stops: the
329 // banner then ages out instead of outliving the show.
330 static constexpr int PACING_BUDGET_WARNING_TIMEOUT_S = 300;
331
332 3 static void RaisePacingBudgetWarning(SendSocketInfo* si, const std::string& msg) {
333
2/6
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 3 times.
3 if (!si->budgetWarning.empty() && si->budgetWarning != msg)
334 ✗ WarningHolder::RemoveWarning(PACING_BUDGET_WARNING_ID, si->budgetWarning);
335 3 si->budgetWarning = msg;
336
5/10
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 6 times.
✓ Branch 7 taken 3 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
15 WarningHolder::AddWarningTimeout(PACING_BUDGET_WARNING_TIMEOUT_S, PACING_BUDGET_WARNING_ID, msg,
337 { { "fixUrl", "channeloutputs.php#tab-e131" }, { "fixText", "Channel Outputs" } });
338
2/8
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
6 }
339
340 1105 static void ClearPacingBudgetWarning(SendSocketInfo* si) {
341
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1102 times.
1105 if (!si->budgetWarning.empty()) {
342
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 WarningHolder::RemoveWarning(PACING_BUDGET_WARNING_ID, si->budgetWarning);
343 3 si->budgetWarning.clear();
344 }
345 1105 }
346
347 // A destination's record is dropped whenever its sockets are: by Close()
348 // when the outputs reload, and by an address change. Its banner goes with
349 // it; a destination still over budget raises it again from its new record.
350 // Clearing it from ~UDPOutput instead found the map already emptied by
351 // Close(), and the banner stayed for its whole timeout. The last reference
352 // can be a worker's or the stats thread's, so this is the one place that
353 // sees every record go.
354 1074 SendSocketInfo::~SendSocketInfo() {
355 1074 ClearPacingBudgetWarning(this);
356
2/2
✓ Branch 0 taken 1059 times.
✓ Branch 1 taken 15 times.
1074 if (!preventClose) {
357
2/2
✓ Branch 0 taken 1063 times.
✓ Branch 1 taken 1059 times.
2122 for (int x : sockets) {
358 1063 close(x);
359 }
360 }
361 1074 }
362
363 // A unicast destination as the frame thread saw it; the address is formatted
364 // there because HexToIP() is a UDPOutput member.
365 struct PacingBudgetDest {
366 unsigned int key;
367 std::string ip;
368 std::shared_ptr<SendSocketInfo> info;
369 };
370 static void CheckPacingBudget(const std::vector<PacingBudgetDest>& dests,
371 const std::list<UDPOutputData*>& outputs);
372
373 979 UDPOutputMessages::UDPOutputMessages() {
374 979 }
375 978 UDPOutputMessages::~UDPOutputMessages() {
376 978 sendSockets.clear();
377 978 }
378 61033 int UDPOutputMessages::GetSocket(unsigned int key) {
379 61033 std::shared_ptr<SendSocketInfo>& info = sendSockets[key];
380
2/2
✓ Branch 0 taken 59542 times.
✓ Branch 1 taken 1491 times.
61033 if (info) {
381
1/2
✓ Branch 0 taken 59542 times.
✗ Branch 1 not taken.
59542 std::lock_guard<std::mutex> lg(info->sendLock);
382
1/2
✓ Branch 0 taken 59542 times.
✗ Branch 1 not taken.
59542 if (!info->sockets.empty()) {
383 59542 return info->sockets[0];
384 }
385
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 59542 times.
59542 }
386 1491 return -1;
387 }
388 25 void UDPOutputMessages::ForceSocket(unsigned int key, int socket, bool preventClose) {
389
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 if (socket < 0) {
390 // left for findOrCreateSocket() to make on a later frame
391 ✗ return;
392 }
393
1/2
✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
25 std::shared_ptr<SendSocketInfo>& info = sendSockets[key];
394
1/2
✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
25 if (!info) {
395
1/2
✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
25 info = std::make_shared<SendSocketInfo>();
396 }
397
1/2
✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
25 std::lock_guard<std::mutex> lg(info->sendLock);
398
1/2
✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
25 if (!info->preventClose) {
399
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 for (int x = 0; x < info->sockets.size(); x++) {
400 ✗ close(info->sockets[x]);
401 }
402 }
403 25 info->preventClose = preventClose;
404 25 info->sockets.clear();
405
1/2
✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
25 info->sockets.push_back(socket);
406 25 }
407 1016219 std::vector<struct mmsghdr>& UDPOutputMessages::GetMessages(unsigned int key) {
408
7/8
✓ Branch 0 taken 979798 times.
✓ Branch 1 taken 36421 times.
✓ Branch 2 taken 979798 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 163509 times.
✓ Branch 5 taken 816289 times.
✓ Branch 6 taken 163509 times.
✓ Branch 7 taken 852710 times.
1016219 if (recordKeys && std::find(recordKeys->begin(), recordKeys->end(), key) == recordKeys->end()) {
409 163509 recordKeys->push_back(key);
410 }
411 1016219 return messages[key];
412 }
413 153649 void UDPOutputMessages::clearMessages() {
414
2/2
✓ Branch 0 taken 179089 times.
✓ Branch 1 taken 153649 times.
332738 for (auto& m : messages) {
415 179089 m.second.clear();
416 }
417 153649 }
418 986 void UDPOutputMessages::clearSockets() {
419 // sockets are shared_ptrs; any worker thread still mid-send keeps its
420 // SendSocketInfo alive until it finishes
421 986 sendSockets.clear();
422 986 }
423
424 // Keys whose packets go to multicast groups. Their sockets need
425 // IP_MULTICAST_IF, or the packets leave by the default route instead of the
426 // output interface. E1.31 sync is always sent to 239.255.<universe>.
427 1233 static bool isMulticastKey(unsigned int key) {
428
5/6
✓ Branch 0 taken 1215 times.
✓ Branch 1 taken 18 times.
✓ Branch 2 taken 1215 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✓ Branch 5 taken 1212 times.
1233 return key == MULTICAST_MESSAGES_KEY || key == LATE_MULTICAST_MESSAGES_KEY || key == E131_SYNC_KEY;
429 }
430
431 static constexpr int LOCAL_DROP_WARNING_ID = 69;
432 static const std::string LOCAL_DROP_WARNING = "The network interface is dropping outgoing packets before they reach the wire. The link may be down, saturated, or misconfigured. See logs for details.";
433
434 UDPOutput* UDPOutput::INSTANCE = nullptr;
435 std::mutex UDPOutput::externalLock;
436 std::map<const void*, std::function<UDPOutputData*()>> UDPOutput::externalFactories;
437
438 1157 UDPOutputData::UDPOutputData(const Json::Value& config) :
439 1157 valid(true),
440 1157 type(0),
441 1157 monitor(true),
442 1157 skippedFrames(0),
443 1157 lastData(nullptr) {
444
3/4
✓ Branch 0 taken 1157 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1150 times.
✓ Branch 3 taken 7 times.
1157 if (config.isMember("description")) {
445
2/4
✓ Branch 0 taken 1150 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1150 times.
✗ Branch 3 not taken.
1150 description = config["description"].asString();
446 }
447
2/4
✓ Branch 0 taken 1157 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1157 times.
✗ Branch 3 not taken.
1157 startChannel = config["startChannel"].asInt();
448
2/4
✓ Branch 0 taken 1157 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1157 times.
✗ Branch 3 not taken.
1157 channelCount = config["channelCount"].asInt();
449
3/4
✓ Branch 0 taken 1157 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1091 times.
✓ Branch 3 taken 66 times.
1157 if (config.isMember("active")) {
450
2/4
✓ Branch 0 taken 1091 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1091 times.
✗ Branch 3 not taken.
1091 active = config["active"].asInt();
451 } else {
452 66 active = 1;
453 }
454
3/4
✓ Branch 0 taken 1157 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1089 times.
✓ Branch 3 taken 68 times.
1157 if (config.isMember("type")) {
455
2/4
✓ Branch 0 taken 1089 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1089 times.
✗ Branch 3 not taken.
1089 type = config["type"].asInt();
456 }
457
2/4
✓ Branch 0 taken 1157 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1157 times.
✗ Branch 3 not taken.
1157 if (config.isMember("address")) {
458
2/4
✓ Branch 0 taken 1157 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1157 times.
✗ Branch 3 not taken.
1157 ipAddress = config["address"].asString();
459 }
460
3/4
✓ Branch 0 taken 1157 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1089 times.
✓ Branch 3 taken 68 times.
1157 if (config.isMember("monitor")) {
461
2/4
✓ Branch 0 taken 1089 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1089 times.
✗ Branch 3 not taken.
1089 monitor = config["monitor"].asInt() ? true : false;
462 }
463
3/4
✓ Branch 0 taken 1157 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1089 times.
✓ Branch 3 taken 68 times.
1157 if (config.isMember("deDuplicate")) {
464
2/4
✓ Branch 0 taken 1089 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1089 times.
✗ Branch 3 not taken.
1089 deDuplicate = config["deDuplicate"].asInt() ? true : false;
465 }
466 // per-destination pacing override; absent (or -1) keeps the global rate
467
3/4
✓ Branch 0 taken 1157 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 1155 times.
1157 if (config.isMember("pacingRate")) {
468
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 pacingRateMbps = config["pacingRate"].asInt();
469 }
470 1157 }
471 2310 UDPOutputData::~UDPOutputData() {
472
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 1146 times.
2310 if (lastData) {
473 18 free(lastData);
474 }
475 2310 }
476
477 static const std::string UNKNOWN_TYPE = "UDP";
478 ✗ const std::string& UDPOutputData::GetOutputTypeString() const {
479 ✗ return UNKNOWN_TYPE;
480 }
481
482 1145 in_addr_t UDPOutputData::toInetAddr(const std::string& ipAddress, bool& valid) {
483 1145 valid = true;
484 1145 bool isAlpha = false;
485
2/2
✓ Branch 0 taken 10524 times.
✓ Branch 1 taken 1145 times.
11669 for (int x = 0; x < ipAddress.length(); x++) {
486 10524 isAlpha |= isalpha(ipAddress[x]);
487 }
488
489
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 1131 times.
1145 if (isAlpha) {
490 // ResolveHostToIPv4() is thread-safe; gethostbyname() returns a shared
491 // static hostent and can be corrupted by concurrent resolves on other
492 // threads. It also caches the answer, so a destination configured by a
493 // name that no longer resolves costs its multi-second timeout once at
494 // startup rather than once per caller.
495 14 uint32_t addr = 0;
496
3/4
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 2 times.
14 if (!ResolveHostToIPv4(ipAddress, addr)) {
497
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 LogErr(VB_CHANNELOUT,
498 "Error looking up hostname: %s\n",
499 ipAddress.c_str());
500 12 valid = false;
501 12 return 0;
502 }
503 2 return addr;
504 }
505 1131 return inet_addr(ipAddress.c_str());
506 }
507
508 163409 void UDPOutputData::SaveFrame(unsigned char* channelData, int len) {
509
3/4
✓ Branch 0 taken 178 times.
✓ Branch 1 taken 163231 times.
✓ Branch 2 taken 178 times.
✗ Branch 3 not taken.
163409 if (deDuplicate && len >= 0) {
510
3/4
✓ Branch 0 taken 169 times.
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 169 times.
178 if (lastData == nullptr || len > lastDataSize) {
511 // Frame length is constant for a given output object today; this
512 // path is defensive, for a subclass or future caller that passes a
513 // longer frame. Grow rather than overrun the old allocation. On
514 // failure nothing was saved, so invalidate the saved length too:
515 // NeedToOutputFrame() must treat every range as changed instead of
516 // matching it against the stale copy. Keep the old buffer and its
517 // capacity for the next attempt.
518 // Zero-length frames still allocate one byte so they record a
519 // saved frame, as the old calloc(1, 0) did.
520 9 int alloc = len > 0 ? len : 1;
521 9 unsigned char* grown = (unsigned char*)realloc(lastData, alloc);
522
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (grown == nullptr) {
523 ✗ lastDataLen = 0;
524 ✗ return;
525 }
526 9 lastData = grown;
527 9 lastDataSize = alloc;
528 }
529 /*
530 printf("Saving: %d %2X%2X%2X %2X%2X%2X %2X%2X%2X %2X%2X%2X\n", len,
531 channelData[0], channelData[1], channelData[2], channelData[3], channelData[4], channelData[5],
532 channelData[6], channelData[7], channelData[8], channelData[9], channelData[10], channelData[11]);
533 */
534
1/2
✓ Branch 0 taken 178 times.
✗ Branch 1 not taken.
178 if (len > 0) {
535 178 memcpy(lastData, channelData, len);
536 }
537 178 lastDataLen = len;
538 }
539 }
540
541 984201 bool UDPOutputData::NeedToOutputFrame(unsigned char* channelData, int startChannel, int savedIdx, int count) {
542
4/4
✓ Branch 0 taken 7609 times.
✓ Branch 1 taken 976592 times.
✓ Branch 2 taken 7572 times.
✓ Branch 3 taken 37 times.
984201 if (deDuplicate && skippedFrames < 10) {
543 // Not saved yet, or (defensively) the caller wants a range the saved
544 // frame does not cover. Report it as changed so it is sent and
545 // SaveFrame() can grow the buffer; never compare against bytes outside
546 // the allocation.
547
4/6
✓ Branch 0 taken 5565 times.
✓ Branch 1 taken 2007 times.
✓ Branch 2 taken 5565 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 5565 times.
✗ Branch 5 not taken.
7572 if (lastData == nullptr || savedIdx < 0 || count < 0 ||
548
2/4
✓ Branch 0 taken 5565 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5565 times.
5565 savedIdx > lastDataLen || count > lastDataLen - savedIdx) {
549 2007 return true;
550 }
551
2/2
✓ Branch 0 taken 5818183 times.
✓ Branch 1 taken 4434 times.
5822617 for (int x = 0; x < count; x++) {
552
2/2
✓ Branch 0 taken 1131 times.
✓ Branch 1 taken 5817052 times.
5818183 if (channelData[x + savedIdx + startChannel] != lastData[x + savedIdx]) {
553 /*
554 printf("ND: %d %2X%2X%2X %2X%2X%2X %2X%2X%2X %2X%2X%2X\n", x,
555 lastData[0], lastData[1], lastData[2], lastData[3], lastData[4], lastData[5],
556 lastData[6], lastData[7], lastData[8], lastData[9], lastData[10], lastData[11]);
557 printf("%d %d %2X%2X%2X %2X%2X%2X %2X%2X%2X %2X%2X%2X\n", start, startChannel, x,
558 channelData[start + startChannel + 0], channelData[start + startChannel + 1], channelData[start + startChannel + 2],
559 channelData[start + startChannel + 3], channelData[start + startChannel + 4], channelData[start + startChannel + 5],
560 channelData[start + startChannel + 6], channelData[start + startChannel + 7], channelData[start + startChannel + 8],
561 channelData[start + startChannel + 9], channelData[start + startChannel + 10], channelData[start + startChannel + 11]);
562 printf("New data %d %d %d %X %X\n", x, startChannel, savedIdx, channelData[x + savedIdx + startChannel], lastData[x + savedIdx]);
563 */
564 1131 return true;
565 }
566 }
567 4434 return false;
568 }
569 976629 return true;
570 }
571
572 979 UDPOutput::UDPOutput(unsigned int startChannel, unsigned int channelCount) :
573 979 networkCallbackId(-1),
574 979 doneWorkCount(0),
575 979 numWorkThreads(0),
576 979 workGeneration(0),
577 979 runWorkThreads(true),
578 979 useThreadedOutput(true),
579
3/6
✓ Branch 0 taken 979 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 979 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 979 times.
✗ Branch 5 not taken.
1958 blockingOutput(false) {
580 979 INSTANCE = this;
581 979 }
582 5868 UDPOutput::~UDPOutput() {
583 1956 StopPinging();
584 {
585 // under workMutex: a worker between its runWorkThreads check and its
586 // wait() would otherwise miss this notify and sleep forever, and the
587 // loop below would then spin with outputThreadLock held
588 1956 std::lock_guard<std::mutex> lk(workMutex);
589 1956 runWorkThreads = false;
590 1956 }
591 1956 workSignal.notify_all();
592
593 {
594 1956 std::lock_guard<std::mutex> lk(externalLock);
595 1956 INSTANCE = nullptr;
596 1956 }
597 1956 NetworkMonitor::INSTANCE.removeCallback(networkCallbackId);
598
2/2
✓ Branch 0 taken 550 times.
✓ Branch 1 taken 978 times.
3056 while (numWorkThreads) {
599 1100 std::this_thread::sleep_for(std::chrono::milliseconds(1));
600 }
601
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 978 times.
1956 while (statCheckRunning) {
602 ✗ std::this_thread::sleep_for(std::chrono::milliseconds(1));
603 }
604 // Requests in flight capture this (the HEAD probe) or an output deleted
605 // below (Twinkly, which cancels its own). Cancel ours rather than
606 // draining every request in the process, which waited out unrelated
607 // ones - up to the 10s curl timeout each - with outputThreadLock held.
608 // The destructor runs on the main loop, where cancelling is allowed.
609 1956 CurlManager::INSTANCE.cancelRequests(curlOwner);
610
2/2
✓ Branch 0 taken 1152 times.
✓ Branch 1 taken 978 times.
4260 for (auto a : outputs) {
611
1/2
✓ Branch 0 taken 1152 times.
✗ Branch 1 not taken.
2304 delete a;
612 }
613 3912 }
614
615 // Each output sends straight out of the sequence buffer from the range its
616 // startChannel/channelCount (times universeCount) describe. A startChannel
617 // of 0 made that range start at -1, and a large universeCount overflowed the
618 // int it is computed in, wrapping it to the "nothing to send" form the range
619 // check lets through; both then read outside the buffer every frame. Worked
620 // out in 64 bits from the config, before the output allocates per universe.
621 1091 static bool UniverseRangeFits(const Json::Value& s, int type) {
622
2/4
✓ Branch 0 taken 1091 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1091 times.
✗ Branch 3 not taken.
1091 int64_t start = s["startChannel"].asInt64();
623
2/4
✓ Branch 0 taken 1091 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1091 times.
✗ Branch 3 not taken.
1091 int64_t count = s["channelCount"].asInt64();
624 1091 int64_t universes = 1;
625
8/8
✓ Branch 0 taken 877 times.
✓ Branch 1 taken 214 times.
✓ Branch 2 taken 876 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 873 times.
✓ Branch 5 taken 3 times.
✓ Branch 6 taken 4 times.
✓ Branch 7 taken 869 times.
1091 if (type <= 3 || type == 6 || type == 7 || type == 9) {
626 // E1.31, ArtNet and KiNet repeat channelCount per universe/port
627
3/4
✓ Branch 0 taken 222 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 58 times.
✓ Branch 3 taken 164 times.
222 if (s.isMember("universeCount")) {
628
2/4
✓ Branch 0 taken 58 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 58 times.
✗ Branch 3 not taken.
58 universes = std::max<int64_t>(1, s["universeCount"].asInt64());
629 }
630 }
631
6/10
✓ Branch 0 taken 1091 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1091 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1091 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1091 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 1087 times.
✓ Branch 9 taken 4 times.
1091 bool active = !s.isMember("active") || s["active"].asInt();
632
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1087 times.
1091 if (!active) {
633 4 return true;
634 }
635
4/6
✓ Branch 0 taken 1086 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1086 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1086 times.
✗ Branch 5 not taken.
1087 if (start >= 1 && count >= 0 && universes <= 64000 &&
636
2/2
✓ Branch 0 taken 1085 times.
✓ Branch 1 taken 1 times.
1086 start - 1 + count * universes <= (int64_t)FPPD_MAX_CHANNELS) {
637 1085 return true;
638 }
639
4/8
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
2 std::string desc = s.isMember("description") ? s["description"].asString() : std::string();
640
3/10
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 2 times.
✗ Branch 9 not taken.
4 std::string warning = "Network output " + (desc.empty() ? s["address"].asString() : desc) +
641
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
8 " needs channels outside 1-" + std::to_string(FPPD_MAX_CHANNELS) +
642
6/12
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 2 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 2 times.
✗ Branch 11 not taken.
8 " (start " + std::to_string(start) + ", " + std::to_string(count) +
643
4/8
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
6 " channels x " + std::to_string(universes) + "). Output disabled.";
644
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 AddOutputConfigWarning(26, warning);
645
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 LogErr(VB_CHANNELOUT, "%s\n", warning.c_str());
646 2 return false;
647 2 }
648
649 979 int UDPOutput::Init(Json::Value config) {
650 // a reload may have dropped the ArtNet output that last wanted the socket
651
1/2
✓ Branch 0 taken 979 times.
✗ Branch 1 not taken.
979 ResetArtNetSocketFailure();
652
2/4
✓ Branch 0 taken 979 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 979 times.
✗ Branch 3 not taken.
979 enabled = config["enabled"].asInt();
653 979 hasArtNet = false;
654
4/6
✓ Branch 0 taken 2070 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2070 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1093 times.
✓ Branch 5 taken 977 times.
2070 for (int i = 0; i < config["universes"].size(); i++) {
655
3/6
✓ Branch 0 taken 1093 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1093 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1093 times.
✗ Branch 5 not taken.
1093 Json::Value s = config["universes"][i];
656
4/4
✓ Branch 0 taken 1092 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1091 times.
✓ Branch 3 taken 1 times.
1093 int type = s["type"].asInt();
657
3/4
✓ Branch 0 taken 1091 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 1089 times.
1091 if (!UniverseRangeFits(s, type)) {
658 2 continue;
659 }
660
5/6
✓ Branch 0 taken 185 times.
✓ Branch 1 taken 31 times.
✓ Branch 2 taken 849 times.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
1089 switch (type) {
661 185 case 0:
662 case 1:
663 // E1.31 types
664
4/10
✓ Branch 0 taken 185 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 185 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 185 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 185 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
185 outputs.push_back(new E131OutputData(s));
665 185 break;
666 31 case 2:
667 case 3:
668 case 9:
669 // ArtNet types
670
4/10
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 31 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 31 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 31 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
31 outputs.push_back(new ArtNetOutputData(s));
671 31 hasArtNet = true;
672 31 break;
673 849 case 4:
674 case 5:
675 // DDP types
676
4/10
✓ Branch 0 taken 849 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 849 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 849 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 849 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
849 outputs.push_back(new DDPOutputData(s));
677 849 break;
678 4 case 6:
679 case 7:
680 // KiNet types
681
4/10
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 4 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
4 outputs.push_back(new KiNetOutputData(s));
682 4 break;
683 20 case 8:
684 // Twinkly types
685
4/10
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 20 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 20 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
20 outputs.push_back(new TwinklyOutputData(s));
686 20 break;
687 ✗ default:
688 ✗ LogErr(VB_CHANNELOUT, "Unknown IP output type %d\n", type);
689 ✗ break;
690 }
691
692
3/4
✓ Branch 0 taken 1080 times.
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1080 times.
1089 needsBroadcast |= (type == 0 || type == 2);
693
2/2
✓ Branch 0 taken 1089 times.
✓ Branch 1 taken 2 times.
1093 }
694
3/4
✓ Branch 0 taken 977 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 36 times.
✓ Branch 3 taken 941 times.
977 if (config.isMember("threaded")) {
695
2/4
✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 36 times.
✗ Branch 3 not taken.
36 int style = config["threaded"].asInt();
696
4/4
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 31 times.
36 useThreadedOutput = style == 1 || style == 3;
697
4/4
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 30 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 2 times.
36 blockingOutput = style == 0 || style == 1;
698 }
699
3/4
✓ Branch 0 taken 977 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
✓ Branch 3 taken 950 times.
977 if (config.isMember("interface")) {
700
2/4
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
✗ Branch 3 not taken.
27 std::string configured = config["interface"].asString();
701
5/6
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 26 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 26 times.
✓ Branch 5 taken 1 times.
27 if (configured.empty() || isValidInterfaceName(configured)) {
702
1/2
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
26 outInterface = configured;
703 } else {
704 // Garbage never produced working output (address/sysfs lookups fail
705 // on it) and reached a shell below -- fall through to auto-detect.
706
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 LogErr(VB_CHANNELOUT, "UDPOutput: ignoring invalid interface '%s'\n", configured.c_str());
707 }
708 27 }
709
710 1954 std::set<std::string> myIps;
711 // get all the addresses
712 struct ifaddrs *interfaces, *tmp;
713 977 getifaddrs(&interfaces);
714 977 tmp = interfaces;
715 // loop through all the interfaces and get the addresses
716 char address[16];
717 977 memset(address, 0, sizeof(address));
718
1/2
✓ Branch 0 taken 977 times.
✗ Branch 1 not taken.
1954 std::string firstInterface = "";
719
2/2
✓ Branch 0 taken 5021 times.
✓ Branch 1 taken 977 times.
5998 while (tmp) {
720
3/4
✓ Branch 0 taken 5021 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1985 times.
✓ Branch 3 taken 3036 times.
5021 if (tmp->ifa_addr && tmp->ifa_addr->sa_family == AF_INET) {
721
1/2
✓ Branch 0 taken 1985 times.
✗ Branch 1 not taken.
1985 GetInterfaceAddress(tmp->ifa_name, address, NULL, NULL);
722
1/2
✓ Branch 0 taken 1985 times.
✗ Branch 1 not taken.
1985 myIps.emplace(address);
723
7/8
✓ Branch 0 taken 1985 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1877 times.
✓ Branch 3 taken 108 times.
✓ Branch 4 taken 925 times.
✓ Branch 5 taken 952 times.
✓ Branch 6 taken 925 times.
✓ Branch 7 taken 1060 times.
1985 if ((outInterface == "") && (strcmp(tmp->ifa_name, "lo"))) {
724
3/4
✓ Branch 0 taken 925 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 924 times.
✓ Branch 3 taken 1 times.
925 if (firstInterface == "") {
725
1/2
✓ Branch 0 taken 924 times.
✗ Branch 1 not taken.
924 firstInterface = tmp->ifa_name;
726 }
727
2/2
✓ Branch 0 taken 923 times.
✓ Branch 1 taken 2 times.
925 if (tmp->ifa_name[0] == 'e') {
728
1/2
✓ Branch 0 taken 923 times.
✗ Branch 1 not taken.
923 outInterface = tmp->ifa_name;
729 }
730 }
731 }
732 5021 tmp = tmp->ifa_next;
733 }
734 977 freeifaddrs(interfaces);
735
3/4
✓ Branch 0 taken 977 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
✓ Branch 3 taken 949 times.
977 if (outInterface == "") {
736
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 outInterface = firstInterface;
737 }
738
3/4
✓ Branch 0 taken 977 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
✓ Branch 3 taken 950 times.
977 if (outInterface == "") {
739
1/2
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
27 outInterface = "eth0";
740 }
741
742 // Build the per-destination pacing overrides keyed by controller IP. The
743 // send socket is shared per destination IP, so when multiple outputs target
744 // the same controller with different overrides we keep the most conservative
745 // (lowest) cap; an explicit "unpaced" (0 Mbps) is stored as ~0U so any real
746 // rate on the same IP wins over it.
747
2/2
✓ Branch 0 taken 1089 times.
✓ Branch 1 taken 977 times.
2066 for (auto o : outputs) {
748
5/6
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1087 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 1087 times.
✓ Branch 5 taken 2 times.
1089 if (o->pacingRateMbps < 0 || o->ipAddress.empty()) {
749 1087 continue;
750 }
751 2 bool valid = false;
752
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 in_addr_t addr = UDPOutputData::toInetAddr(o->ipAddress, valid);
753
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!valid) {
754 ✗ continue;
755 }
756
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 unsigned int rate = o->pacingRateMbps == 0 ? ~0U : (unsigned int)(o->pacingRateMbps * 1000000ULL / 8);
757
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 auto it = perDestPacingRate.find(addr);
758
2/6
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
2 if (it == perDestPacingRate.end() || rate < it->second) {
759
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 perDestPacingRate[addr] = rate;
760 }
761 }
762
763 #ifndef PLATFORM_OSX
764 // Kernel-level packet pacing. Bursting a full frame at line rate overruns
765 // switch buffers on the 1G->100M speed mismatch to most pixel controllers
766 // and the drops are silent. With the fq qdisc installed (fppinit does this
767 // at boot), SO_MAX_PACING_RATE lets the kernel clock each destination's
768 // packets out at a rate the far end can actually absorb.
769 // pacing rate lives in the outputs config (hot-reloadable with the rest
770 // of it); fall back to the old global setting for configs saved before
771 // the UI moved, then to the 90Mbps default
772
6/10
✓ Branch 0 taken 977 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✓ Branch 3 taken 967 times.
✓ Branch 4 taken 10 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 10 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 967 times.
✗ Branch 9 not taken.
977 int pacingMbps = config.isMember("pacingRate") ? config["pacingRate"].asInt() : getSettingInt("UDPPacingRate", 90);
773 // per-output overrides can request pacing even when the global rate is
774 // Disabled (e.g. only a single ESP32 needs a cap); install fq and arm the
775 // machinery if either the global rate or any override wants it
776 977 bool anyPerDestPacing = false;
777
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 976 times.
977 for (const auto& e : perDestPacingRate) {
778
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (e.second != ~0U) {
779 1 anyPerDestPacing = true;
780 1 break;
781 }
782 }
783 // a reload re-reads the qdiscs, and warms the cache here on the main
784 // thread rather than in the first frame
785
1/2
✓ Branch 0 taken 977 times.
✗ Branch 1 not taken.
977 clearFQCache();
786
3/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 974 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
977 if (pacingMbps > 0 || anyPerDestPacing) {
787
3/4
✓ Branch 0 taken 974 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 36 times.
✓ Branch 3 taken 938 times.
974 if (!anyEthernetHasFQ(outInterface)) {
788 // fppinit only installs fq at boot; if the user enabled pacing
789 // since then, install it now so the setting takes effect on the
790 // fppd restart it advertises rather than silently needing a
791 // reboot. Only interfaces with an IPv4 address can carry the
792 // routed UDP we pace - an address-less interface dedicated to a
793 // raw layer-2 output (ColorLight) keeps its default qdisc.
794
5/8
✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 36 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 48 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 48 times.
✓ Branch 7 taken 36 times.
84 for (const auto& entry : std::filesystem::directory_iterator("/sys/class/net/")) {
795
2/4
✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 48 times.
✗ Branch 3 not taken.
48 std::string dev = entry.path().filename();
796
7/10
✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
✓ Branch 3 taken 39 times.
✓ Branch 4 taken 9 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 9 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 9 times.
✓ Branch 9 taken 39 times.
48 if (IsShowEthernetInterface(dev) && InterfaceHasRoutableIPv4(dev)) {
797
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 InstallShowTrafficQdisc(dev);
798 }
799 84 }
800
1/2
✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
36 clearFQCache();
801 }
802
3/4
✓ Branch 0 taken 974 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 947 times.
✓ Branch 3 taken 27 times.
974 if (anyEthernetHasFQ(outInterface)) {
803 947 pacingEnabled = true;
804 // 0 when the global rate is Disabled: destinations without an
805 // override then stay unpaced while overridden ones still get capped
806
1/2
✓ Branch 0 taken 947 times.
✗ Branch 1 not taken.
947 pacingRate = pacingMbps > 0 ? (unsigned int)(pacingMbps * 1000000ULL / 8) : 0;
807 // slowest positive rate in play, for the drain-sleep math below
808 947 pacingDrainRate = pacingRate;
809
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 947 times.
949 for (const auto& e : perDestPacingRate) {
810
4/6
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
2 if (e.second != ~0U && (pacingDrainRate == 0 || e.second < pacingDrainRate)) {
811 1 pacingDrainRate = e.second;
812 }
813 }
814
1/2
✓ Branch 0 taken 947 times.
✗ Branch 1 not taken.
947 if (pacingMbps > 0) {
815
1/2
✓ Branch 0 taken 947 times.
✗ Branch 1 not taken.
947 LogInfo(VB_CHANNELOUT, "UDP output pacing enabled at %d Mbps per destination (%d per-destination override(s))\n",
816 pacingMbps, (int)perDestPacingRate.size());
817 } else {
818 ✗ LogInfo(VB_CHANNELOUT, "UDP output pacing enabled for %d per-destination override(s), global rate disabled\n",
819 (int)perDestPacingRate.size());
820 }
821 } else {
822
1/2
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
27 LogInfo(VB_CHANNELOUT, "UDP output pacing requested but %s does not have the fq qdisc, using buffer drain pacing\n",
823 outInterface.c_str());
824 }
825 974 } else {
826
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 LogInfo(VB_CHANNELOUT, "UDP output pacing disabled, using buffer drain pacing\n");
827 }
828 #endif
829
830
1/2
✓ Branch 0 taken 977 times.
✗ Branch 1 not taken.
977 bool disableFakeBridges = getSettingInt("DisableFakeNetworkBridges");
831
832
2/2
✓ Branch 0 taken 1089 times.
✓ Branch 1 taken 977 times.
2066 for (auto o : outputs) {
833
7/8
✓ Branch 0 taken 1089 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1080 times.
✓ Branch 3 taken 9 times.
✓ Branch 4 taken 1064 times.
✓ Branch 5 taken 16 times.
✓ Branch 6 taken 1064 times.
✓ Branch 7 taken 25 times.
1089 if (o->IsPingable() && o->active) {
834
2/2
✓ Branch 0 taken 78 times.
✓ Branch 1 taken 986 times.
1064 if (!disableFakeBridges) {
835
1/2
✓ Branch 0 taken 78 times.
✗ Branch 1 not taken.
78 std::string host = o->ipAddress;
836
3/4
✓ Branch 0 taken 78 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 66 times.
78 if (myIps.find(host) != myIps.end()) {
837 // trying to send UDP data to myself, that's bad. Disable
838
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 std::string msg = "UDP Output set to send data to myself. Disabling ";
839
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 msg += host.c_str();
840
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 LogWarn(VB_CHANNELOUT, msg);
841
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 AddOutputConfigWarning(6, msg);
842 12 o->active = false;
843 12 }
844 78 }
845 }
846 }
847
848 2146 std::function<void(NetworkMonitor::NetEventType, int, const std::string&)> f = [this](NetworkMonitor::NetEventType i, int up, const std::string& s) {
849
1/2
✓ Branch 0 taken 192 times.
✗ Branch 1 not taken.
192 std::string interface = outInterface;
850
5/6
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 174 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 18 times.
✓ Branch 4 taken 59 times.
✓ Branch 5 taken 115 times.
192 if ((i == NetworkMonitor::NetEventType::NEW_ADDR && up) || i == NetworkMonitor::NetEventType::DEL_ADDR) {
851 // any address change can move routes; re-evaluate the pacing
852 // decision for every unicast destination. This covers both
853 // directions: a destination that couldn't be paced yet (route
854 // resolved via wifi while ethernet waited on DHCP) and a paced
855 // destination whose route failed over to a non-fq interface,
856 // which must be un-paced or it sends unpaced line-rate bursts
857 // with the large buffer and no drain waits. Stagger the
858 // re-checks across frames so one network event doesn't trigger
859 // a route-lookup storm in a single frame.
860
1/2
✓ Branch 0 taken 77 times.
✗ Branch 1 not taken.
77 std::unique_lock<std::mutex> lk(socketMutex);
861 77 int stagger = 0;
862
2/2
✓ Branch 0 taken 99 times.
✓ Branch 1 taken 77 times.
176 for (auto& si : messages.sendSockets) {
863
1/2
✓ Branch 0 taken 99 times.
✗ Branch 1 not taken.
99 if (si.second) {
864 // try_lock: blocking here (while holding socketMutex) on a
865 // straggler worker would transitively stall the frame thread
866 99 std::unique_lock<std::mutex> lg(si.second->sendLock, std::try_to_lock);
867
1/2
✓ Branch 0 taken 99 times.
✗ Branch 1 not taken.
99 if (lg.owns_lock()) {
868 99 si.second->pacedChecked = false;
869 99 si.second->pacedCheckCountdown = stagger++ % 16;
870 }
871 99 }
872 }
873 77 }
874
7/8
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 165 times.
✓ Branch 2 taken 7 times.
✓ Branch 3 taken 20 times.
✓ Branch 4 taken 7 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 7 times.
✓ Branch 7 taken 185 times.
192 if (s == interface && i == NetworkMonitor::NetEventType::NEW_ADDR && up) {
875
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 2 times.
7 if (!interfaceUp) {
876
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 LogInfo(VB_CHANNELOUT, "UDP Interface %s now up\n", s.c_str());
877
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 PingControllers(false);
878
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 std::this_thread::sleep_for(std::chrono::milliseconds(200));
879 // the frame thread kept creating sockets while the address was
880 // missing, bound to (and multicasting out of) loopback or the
881 // old address; drop them so they are rebuilt on the new one
882
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 interfaceUp = InitNetwork(true);
883
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
5 if (interfaceUp && hasArtNet) {
884 ✗ CreateArtNetSocket(localAddress.sin_addr.s_addr, true);
885 }
886 }
887
6/6
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 165 times.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 15 times.
✓ Branch 4 taken 5 times.
✓ Branch 5 taken 180 times.
185 } else if (s == interface && i == NetworkMonitor::NetEventType::DEL_ADDR) {
888 // The interface can still have an address: a DHCP lease that
889 // replaces a fallback address arrives before the old one goes, and
890 // its NEW_ADDR was ignored because the interface was up. Re-read
891 // the address, as NEW_ADDR does, or the sockets are rebuilt bound
892 // to (and multicasting out of) the address just deleted.
893
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 interfaceUp = InitNetwork(true);
894
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 4 times.
5 if (interfaceUp) {
895
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 LogInfo(VB_CHANNELOUT, "UDP Interface %s address changed\n", s.c_str());
896
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (hasArtNet) {
897 ✗ CreateArtNetSocket(localAddress.sin_addr.s_addr, true);
898 }
899 } else {
900
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 LogInfo(VB_CHANNELOUT, "UDP Interface %s now down\n", s.c_str());
901 }
902
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 PingControllers(false);
903 }
904 1169 };
905
1/2
✓ Branch 0 taken 977 times.
✗ Branch 1 not taken.
977 networkCallbackId = NetworkMonitor::INSTANCE.registerCallback(f);
906
907 // An interface with no address yet (DHCP still waiting, cable out) is
908 // down: the NEW_ADDR event for it then rebuilds the sockets on the real
909 // address instead of being ignored.
910
1/2
✓ Branch 0 taken 977 times.
✗ Branch 1 not taken.
977 interfaceUp = InitNetwork();
911 {
912
1/2
✓ Branch 0 taken 977 times.
✗ Branch 1 not taken.
977 std::lock_guard<std::mutex> l(pingHostsLock);
913
2/2
✓ Branch 0 taken 1089 times.
✓ Branch 1 taken 977 times.
2066 for (auto o : outputs) {
914
1/2
✓ Branch 0 taken 1089 times.
✗ Branch 1 not taken.
1089 addPingTarget(o);
915 }
916 977 failedCount = (int)pingHosts.size();
917 977 }
918 // need to do three pings to detect down hosts
919
1/2
✓ Branch 0 taken 977 times.
✗ Branch 1 not taken.
977 PingControllers(false);
920 977 int sleepCount = 0;
921
6/6
✓ Branch 0 taken 4700 times.
✓ Branch 1 taken 959 times.
✓ Branch 2 taken 4682 times.
✓ Branch 3 taken 18 times.
✓ Branch 4 taken 4682 times.
✓ Branch 5 taken 977 times.
5659 while (failedCount > 0 && sleepCount < (UDP_PING_TIMEOUT + 10)) {
922
1/2
✓ Branch 0 taken 4682 times.
✗ Branch 1 not taken.
4682 std::this_thread::sleep_for(std::chrono::milliseconds(1));
923 4682 ++sleepCount;
924 }
925
1/2
✓ Branch 0 taken 977 times.
✗ Branch 1 not taken.
977 PingControllers(true);
926 977 sleepCount = 0;
927
6/6
✓ Branch 0 taken 8635 times.
✓ Branch 1 taken 960 times.
✓ Branch 2 taken 8618 times.
✓ Branch 3 taken 17 times.
✓ Branch 4 taken 8618 times.
✓ Branch 5 taken 977 times.
9595 while (failedCount > 0 && sleepCount < (UDP_PING_TIMEOUT * 2)) {
928
1/2
✓ Branch 0 taken 8618 times.
✗ Branch 1 not taken.
8618 std::this_thread::sleep_for(std::chrono::milliseconds(1));
929
1/2
✓ Branch 0 taken 8618 times.
✗ Branch 1 not taken.
8618 CurlManager::INSTANCE.processCurls();
930 8618 ++sleepCount;
931 }
932
1/2
✓ Branch 0 taken 977 times.
✗ Branch 1 not taken.
977 PingControllers(true);
933 977 sleepCount = 0;
934
6/6
✓ Branch 0 taken 8200 times.
✓ Branch 1 taken 961 times.
✓ Branch 2 taken 8184 times.
✓ Branch 3 taken 16 times.
✓ Branch 4 taken 8184 times.
✓ Branch 5 taken 977 times.
9161 while (failedCount > 0 && sleepCount < (UDP_PING_TIMEOUT * 2)) {
935
1/2
✓ Branch 0 taken 8184 times.
✗ Branch 1 not taken.
8184 std::this_thread::sleep_for(std::chrono::milliseconds(1));
936
1/2
✓ Branch 0 taken 8184 times.
✗ Branch 1 not taken.
8184 CurlManager::INSTANCE.processCurls();
937 8184 ++sleepCount;
938 }
939
4/4
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 966 times.
✓ Branch 2 taken 7 times.
✓ Branch 3 taken 4 times.
977 if (hasArtNet && interfaceUp) {
940 // we have artnet packets so we'll need to get the special artnet socket created,
941 // but we want to make sure it's on the given interface. Without an
942 // address yet this waits for NEW_ADDR; binding it now would pin the
943 // shared ArtNet socket to loopback for good, and if ArtNet sends
944 // first it creates its own socket on INADDR_ANY.
945
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 CreateArtNetSocket(localAddress.sin_addr.s_addr, true);
946 }
947 {
948 // From here an attached output's controller is pinged as it attaches;
949 // left in the list above, the ones attached below were never pinged.
950
1/2
✓ Branch 0 taken 977 times.
✗ Branch 1 not taken.
977 std::lock_guard<std::mutex> l(pingHostsLock);
951 977 pingsStarted = true;
952 977 }
953 {
954 // outputs registered by other plugins before this instance existed,
955 // or kept across a co-universes reload that replaced the previous one
956
1/2
✓ Branch 0 taken 977 times.
✗ Branch 1 not taken.
977 std::lock_guard<std::mutex> lk(externalLock);
957
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 977 times.
985 for (auto& [owner, create] : externalFactories) {
958
2/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
8 attachExternalOutput(owner, createExternalOutput(create));
959 }
960 977 }
961
2/4
✓ Branch 0 taken 977 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 977 times.
✗ Branch 3 not taken.
1954 return ChannelOutput::Init(config);
962 }
963 976 int UDPOutput::Close() {
964 976 NetworkMonitor::INSTANCE.removeCallback(networkCallbackId);
965 976 messages.clearMessages();
966 976 messages.clearSockets();
967 976 StopPinging();
968
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 974 times.
976 if (dropWarningActive) {
969 // the replacement instance starts with dropWarningActive false and
970 // would never remove this one
971
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 WarningHolder::RemoveWarning(LOCAL_DROP_WARNING_ID, LOCAL_DROP_WARNING);
972 2 dropWarningActive = false;
973 }
974
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 974 times.
976 if (!interfaceWarning.empty()) {
975 // the replacement raises its own if the interface still has no address
976
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 WarningHolder::RemoveWarning(22, interfaceWarning);
977 2 interfaceWarning.clear();
978 }
979 976 return ChannelOutput::Close();
980 }
981 152673 void UDPOutput::PrepData(unsigned char* channelData) {
982
1/2
✓ Branch 0 taken 152673 times.
✗ Branch 1 not taken.
152673 if (enabled) {
983
1/2
✓ Branch 0 taken 152673 times.
✗ Branch 1 not taken.
152673 std::unique_lock<std::mutex> lk(socketMutex);
984
1/2
✓ Branch 0 taken 152673 times.
✗ Branch 1 not taken.
152673 messages.clearMessages();
985 // A destination that couldn't keep up still has a work item from an
986 // earlier frame, queued or being sent, whose iovecs point at its
987 // outputs' packet buffers (headers, sequence numbers, the dedup copy).
988 // Preparing those outputs again rewrote the buffers while a worker
989 // read them, so packets went out half one frame, half the next. Leave
990 // them as they are this frame: the destination is behind anyway, and
991 // its pending item still goes out.
992 152673 std::set<unsigned int> busy;
993
2/2
✓ Branch 0 taken 151938 times.
✓ Branch 1 taken 735 times.
152673 if (useThreadedOutput) {
994
1/2
✓ Branch 0 taken 151938 times.
✗ Branch 1 not taken.
151938 std::lock_guard<std::mutex> wl(workMutex);
995
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 151938 times.
151938 for (auto& w : workQueue) {
996 ✗ busy.insert(w.id);
997 }
998
1/2
✓ Branch 0 taken 151938 times.
✗ Branch 1 not taken.
151938 busy.insert(sendingKeys.begin(), sendingKeys.end());
999 151938 }
1000 152673 skippedOutputs.clear();
1001
2/2
✓ Branch 0 taken 170702 times.
✓ Branch 1 taken 152673 times.
323375 for (auto a : outputs) {
1002
6/6
✓ Branch 0 taken 169152 times.
✓ Branch 1 taken 1550 times.
✓ Branch 2 taken 167932 times.
✓ Branch 3 taken 1220 times.
✓ Branch 4 taken 167932 times.
✓ Branch 5 taken 2770 times.
170702 if (a->valid.load(std::memory_order_relaxed) && a->active) {
1003
2/2
✓ Branch 0 taken 3729 times.
✓ Branch 1 taken 164203 times.
167932 if (!busy.empty()) {
1004
1/2
✓ Branch 0 taken 3729 times.
✗ Branch 1 not taken.
3729 auto k = outputKeys.find(a);
1005
2/4
✓ Branch 0 taken 3729 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3729 times.
✗ Branch 3 not taken.
7458 if (k != outputKeys.end() &&
1006
2/4
✓ Branch 0 taken 3729 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3729 times.
✗ Branch 3 not taken.
3729 std::any_of(k->second.begin(), k->second.end(),
1007 3729 [&busy](unsigned int key) { return busy.count(key) != 0; })) {
1008
1/2
✓ Branch 0 taken 3729 times.
✗ Branch 1 not taken.
3729 skippedOutputs.insert(a);
1009 3729 continue;
1010 }
1011 }
1012
4/4
✓ Branch 0 taken 163824 times.
✓ Branch 1 taken 379 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 163820 times.
164203 if (outputRunning && !a->started) {
1013 // StartingOutput() only starts the outputs that were
1014 // reachable then; one whose controller answers later (a
1015 // Twinkly powered on after the show started) is started
1016 // here, or it would get frames without ever having
1017 // authenticated
1018 4 a->started = true;
1019
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 a->StartingOutput();
1020 }
1021
1/2
✓ Branch 0 taken 164203 times.
✗ Branch 1 not taken.
164203 std::vector<unsigned int>& keys = outputKeys[a];
1022 164203 keys.clear();
1023 164203 messages.recordKeys = &keys;
1024
1/2
✓ Branch 0 taken 164203 times.
✗ Branch 1 not taken.
164203 a->PrepareData(channelData, messages);
1025 164203 messages.recordKeys = nullptr;
1026 }
1027 }
1028 // add any sync packets or whatever that are needed
1029
2/2
✓ Branch 0 taken 170702 times.
✓ Branch 1 taken 152673 times.
323375 for (auto a : outputs) {
1030
9/10
✓ Branch 0 taken 169152 times.
✓ Branch 1 taken 1550 times.
✓ Branch 2 taken 167932 times.
✓ Branch 3 taken 1220 times.
✓ Branch 4 taken 167932 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 164203 times.
✓ Branch 7 taken 3729 times.
✓ Branch 8 taken 164203 times.
✓ Branch 9 taken 6499 times.
170702 if (a->valid.load(std::memory_order_relaxed) && a->active && !skippedOutputs.count(a)) {
1031
1/2
✓ Branch 0 taken 164203 times.
✗ Branch 1 not taken.
164203 a->PostPrepareData(channelData, messages);
1032 }
1033 }
1034 152673 }
1035 152673 }
1036 2067 void UDPOutput::GetRequiredChannelRanges(const std::function<void(int, int)>& addRange) {
1037
1/2
✓ Branch 0 taken 2067 times.
✗ Branch 1 not taken.
2067 if (enabled) {
1038
2/2
✓ Branch 0 taken 2369 times.
✓ Branch 1 taken 2067 times.
4436 for (auto a : outputs) {
1039
2/2
✓ Branch 0 taken 2302 times.
✓ Branch 1 taken 67 times.
2369 if (a->active) {
1040 int mi, mx;
1041
1/2
✓ Branch 0 taken 2302 times.
✗ Branch 1 not taken.
2302 a->GetRequiredChannelRange(mi, mx);
1042 // max is one past the last channel here and inclusive in
1043 // addRange(): handed on as it was, an output ending on the
1044 // last channel of the buffer failed the loader's range check
1045 // and took every UDP output down with it
1046
1/2
✓ Branch 0 taken 2302 times.
✗ Branch 1 not taken.
2302 addRange(mi, mx - 1);
1047 }
1048 }
1049 }
1050 2067 }
1051
1052 ✗ void UDPOutput::addOutput(UDPOutputData* out) {
1053 // the output thread walks the list in PrepData under socketMutex
1054 ✗ std::lock_guard<std::mutex> lk(socketMutex);
1055 ✗ outputs.push_back(out);
1056 ✗ }
1057
1058 1954 void UDPOutput::StopPinging() {
1059
1/2
✓ Branch 0 taken 1954 times.
✗ Branch 1 not taken.
1954 std::lock_guard<std::mutex> lk(pingHostsLock);
1060
2/2
✓ Branch 0 taken 976 times.
✓ Branch 1 taken 978 times.
1954 if (pingsStopped) {
1061 976 return;
1062 }
1063 978 pingsStopped = true;
1064
2/2
✓ Branch 0 taken 76 times.
✓ Branch 1 taken 978 times.
1054 for (auto& h : pingHosts) {
1065
1/2
✓ Branch 0 taken 76 times.
✗ Branch 1 not taken.
76 PingManager::INSTANCE.removePeriodicPing(h.first);
1066 // the replacement output raises it again if the controller is still
1067 // down; one removed from the config would otherwise keep its
1068 // "unreachable" banner until fppd restarted
1069
1/2
✓ Branch 0 taken 76 times.
✗ Branch 1 not taken.
76 std::unique_lock<std::mutex> l(h.second->lock);
1070
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 69 times.
76 if (!h.second->valid) {
1071
2/4
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
14 WarningHolder::RemoveWarning(27, h.second->warning);
1072 }
1073 76 }
1074
2/2
✓ Branch 0 taken 978 times.
✓ Branch 1 taken 976 times.
1954 }
1075
1076 // An external output whose config can't be read (a GenericUDP output with a
1077 // list for its port) throws from its constructor. That failed this whole
1078 // output, every E1.31/DDP universe with it, at each rebuild; skip just that one.
1079 68 UDPOutputData* UDPOutput::createExternalOutput(const std::function<UDPOutputData*()>& create) {
1080 try {
1081
2/2
✓ Branch 0 taken 65 times.
✓ Branch 1 taken 3 times.
68 return create();
1082
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 } catch (const std::exception& e) {
1083
3/6
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
6 std::string warning = std::string("Could not create a network output from its config (") + e.what() + "). Output disabled.";
1084
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 LogErr(VB_CHANNELOUT, "%s\n", warning.c_str());
1085
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 AddOutputConfigWarning(26, warning);
1086 3 return nullptr;
1087 3 }
1088 }
1089
1090 68 void UDPOutput::attachExternalOutput(const void* owner, UDPOutputData* d) {
1091
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 65 times.
68 if (d == nullptr) {
1092 3 return;
1093 }
1094 {
1095
1/2
✓ Branch 0 taken 65 times.
✗ Branch 1 not taken.
65 std::lock_guard<std::mutex> lk(socketMutex);
1096
1/2
✓ Branch 0 taken 65 times.
✗ Branch 1 not taken.
65 outputs.push_back(d);
1097
1/2
✓ Branch 0 taken 65 times.
✗ Branch 1 not taken.
65 externalOutputs[owner] = d;
1098 65 }
1099 // Under pingHostsLock to the end, as in PingControllers().
1100
1/2
✓ Branch 0 taken 65 times.
✗ Branch 1 not taken.
65 std::lock_guard<std::mutex> lk(pingHostsLock);
1101
2/4
✓ Branch 0 taken 65 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 65 times.
65 if (!pingsStarted || pingsStopped) {
1102 // Init() pings what is attached before it starts the pings
1103 ✗ return;
1104 }
1105
3/4
✓ Branch 0 taken 65 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 61 times.
✓ Branch 3 taken 4 times.
65 if (std::shared_ptr<PingHost> host = addPingTarget(d)) {
1106 61 ++failedCount;
1107
2/4
✓ Branch 0 taken 61 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 61 times.
✗ Branch 3 not taken.
61 PingManager::INSTANCE.addPeriodicPing(host->address, UDP_PING_TIMEOUT, 15000, [this, host](int ms) {
1108 64 OnPingResult(host, ms);
1109 64 });
1110 65 }
1111
1/2
✓ Branch 0 taken 65 times.
✗ Branch 1 not taken.
65 }
1112
1113 1154 std::shared_ptr<UDPOutput::PingHost> UDPOutput::addPingTarget(UDPOutputData* o) {
1114
10/12
✓ Branch 0 taken 1154 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1143 times.
✓ Branch 3 taken 11 times.
✓ Branch 4 taken 1143 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 98 times.
✓ Branch 7 taken 1045 times.
✓ Branch 8 taken 2 times.
✓ Branch 9 taken 96 times.
✓ Branch 10 taken 1058 times.
✓ Branch 11 taken 96 times.
1154 if (!o->IsPingable() || !o->Monitor() || !o->active) {
1115 1058 return nullptr;
1116 }
1117
1/2
✓ Branch 0 taken 96 times.
✗ Branch 1 not taken.
96 std::shared_ptr<PingHost>& host = pingHosts[o->ipAddress];
1118 96 bool created = !host;
1119
2/2
✓ Branch 0 taken 85 times.
✓ Branch 1 taken 11 times.
96 if (created) {
1120
1/2
✓ Branch 0 taken 85 times.
✗ Branch 1 not taken.
85 host = std::make_shared<PingHost>();
1121
1/2
✓ Branch 0 taken 85 times.
✗ Branch 1 not taken.
85 host->address = o->ipAddress;
1122
2/4
✓ Branch 0 taken 85 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 85 times.
✗ Branch 3 not taken.
85 host->warning = createWarning(o->ipAddress, o->GetOutputTypeString(), o->description);
1123 }
1124
1/2
✓ Branch 0 taken 96 times.
✗ Branch 1 not taken.
96 std::unique_lock<std::mutex> l(host->lock);
1125
1/2
✓ Branch 0 taken 96 times.
✗ Branch 1 not taken.
96 host->outputs.push_back(o);
1126
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 96 times.
96 if (!host->valid) {
1127 // one joining a controller already found down starts out of the frame too
1128 ✗ o->valid = false;
1129 }
1130
2/2
✓ Branch 0 taken 85 times.
✓ Branch 1 taken 11 times.
96 return created ? host : nullptr;
1131 96 }
1132
1133 62 void UDPOutput::removePingTarget(UDPOutputData* o) {
1134
1/2
✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
62 auto it = pingHosts.find(o->ipAddress);
1135
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 58 times.
62 if (it == pingHosts.end()) {
1136 4 return;
1137 }
1138 58 std::shared_ptr<PingHost> host = it->second;
1139 {
1140
1/2
✓ Branch 0 taken 58 times.
✗ Branch 1 not taken.
58 std::unique_lock<std::mutex> l(host->lock);
1141 58 auto& outs = host->outputs;
1142
2/4
✓ Branch 0 taken 58 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 58 times.
✗ Branch 3 not taken.
58 outs.erase(std::remove(outs.begin(), outs.end(), o), outs.end());
1143
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
58 if (!outs.empty()) {
1144 ✗ return;
1145 }
1146
1/2
✓ Branch 0 taken 58 times.
✗ Branch 1 not taken.
58 }
1147 // Nothing sends there any more. Not under host->lock: this waits out a
1148 // running ping callback, which takes it.
1149
1/2
✓ Branch 0 taken 58 times.
✗ Branch 1 not taken.
58 pingHosts.erase(it);
1150
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 50 times.
58 if (!pingsStopped) {
1151
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 PingManager::INSTANCE.removePeriodicPing(host->address);
1152 }
1153
1/2
✓ Branch 0 taken 58 times.
✗ Branch 1 not taken.
58 std::unique_lock<std::mutex> l(host->lock);
1154
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 57 times.
58 if (!host->valid) {
1155
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
2 WarningHolder::RemoveWarning(27, host->warning);
1156 }
1157
1/2
✓ Branch 0 taken 58 times.
✗ Branch 1 not taken.
58 }
1158
1159 71 void UDPOutput::RegisterExternalOutput(const void* owner, std::function<UDPOutputData*()> create) {
1160
1/2
✓ Branch 0 taken 71 times.
✗ Branch 1 not taken.
71 std::lock_guard<std::mutex> lk(externalLock);
1161
2/4
✓ Branch 0 taken 71 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 71 times.
✗ Branch 3 not taken.
71 externalFactories[owner] = create;
1162
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 11 times.
71 if (INSTANCE) {
1163
2/4
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 60 times.
✗ Branch 3 not taken.
60 INSTANCE->attachExternalOutput(owner, createExternalOutput(create));
1164 }
1165 71 }
1166
1167 140 void UDPOutput::UnregisterExternalOutput(const void* owner) {
1168
1/2
✓ Branch 0 taken 140 times.
✗ Branch 1 not taken.
140 std::lock_guard<std::mutex> lk(externalLock);
1169
1/2
✓ Branch 0 taken 140 times.
✗ Branch 1 not taken.
140 externalFactories.erase(owner);
1170
2/2
✓ Branch 0 taken 74 times.
✓ Branch 1 taken 66 times.
140 if (INSTANCE) {
1171
1/2
✓ Branch 0 taken 74 times.
✗ Branch 1 not taken.
74 std::lock_guard<std::mutex> slk(INSTANCE->socketMutex);
1172
1/2
✓ Branch 0 taken 74 times.
✗ Branch 1 not taken.
74 auto it = INSTANCE->externalOutputs.find(owner);
1173
2/2
✓ Branch 0 taken 62 times.
✓ Branch 1 taken 12 times.
74 if (it != INSTANCE->externalOutputs.end()) {
1174 // Retired, not deleted: a queued or running send may still point
1175 // into its packet buffers. It stays in outputs, inactive, so
1176 // PrepData skips it and the destructor frees it once the workers
1177 // have exited.
1178 62 it->second->active = false;
1179 {
1180
1/2
✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
62 std::lock_guard<std::mutex> plk(INSTANCE->pingHostsLock);
1181
1/2
✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
62 INSTANCE->removePingTarget(it->second);
1182 62 }
1183
1/2
✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
62 INSTANCE->externalOutputs.erase(it);
1184 }
1185 74 }
1186 140 }
1187
1188 70340 static void flushBuffers(int socket, int msgs, int total) {
1189 #ifndef PLATFORM_OSX
1190 70340 int bytes_in_buffer = 0;
1191
1/2
✓ Branch 0 taken 70340 times.
✗ Branch 1 not taken.
70340 if (ioctl(socket, SIOCOUTQ, &bytes_in_buffer) == 0) {
1192 // Check bytes_in_buffer, if its too high, wait for it to drain
1193 70340 int cnt = 0;
1194 70340 int start = bytes_in_buffer;
1195
4/4
✓ Branch 0 taken 943551 times.
✓ Branch 1 taken 51839 times.
✓ Branch 2 taken 925050 times.
✓ Branch 3 taken 18501 times.
995390 while (bytes_in_buffer > 1024 && cnt < 50) {
1196 925050 cnt++;
1197
1/2
✓ Branch 0 taken 925050 times.
✗ Branch 1 not taken.
925050 std::this_thread::sleep_for(std::chrono::microseconds(1));
1198 925050 ioctl(socket, SIOCOUTQ, &bytes_in_buffer);
1199 }
1200 // if (cnt > 0) {
1201 // printf("Flush: Socket %d had to wait %d (%d->%d) (%d/%d)...\n", socket, cnt, start, bytes_in_buffer, msgs, total);
1202 // }
1203 }
1204 #endif
1205 70340 }
1206
1207 constexpr int MSGS_PER_SENDMMSG = 8;
1208 // kernel-paced sockets take the whole frame in one call (kernel caps a single
1209 // sendmmsg at UIO_MAXIOV=1024 anyway)
1210 constexpr int PACED_MSGS_PER_SENDMMSG = 1024;
1211 168290 int UDPOutput::SendMessages(unsigned int socketKey, SendSocketInfo* socketInfo, std::vector<struct mmsghdr>& sendmsgs) {
1212 168290 errno = 0;
1213 168290 struct mmsghdr* msgs = &sendmsgs[0];
1214 168290 int msgCount = sendmsgs.size();
1215
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 168290 times.
168290 if (msgCount == 0) {
1216 ✗ return 0;
1217 }
1218
1219 168290 int newSockKey = socketKey;
1220 // Serialize against a straggler worker from a previous frame still sending
1221 // to this destination; SendMessages mutates curSocket/sockets. Blocking
1222 // (rather than try_lock) is intentional and safe for the frame thread:
1223 // workers exclusively own the early-message keys, and the frame thread
1224 // only calls SendMessages for late/sync keys (threaded mode) or when no
1225 // workers exist at all (non-threaded mode), so it can never block on a
1226 // straggler here.
1227
1/2
✓ Branch 0 taken 168290 times.
✗ Branch 1 not taken.
168290 std::lock_guard<std::mutex> sendGuard(socketInfo->sendLock);
1228
2/2
✓ Branch 0 taken 90 times.
✓ Branch 1 taken 168200 times.
168290 if (socketInfo->sockets.empty()) {
1229 90 return 0;
1230 }
1231
3/6
✓ Branch 0 taken 168200 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 168200 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 168200 times.
168200 if (socketInfo->curSocket < 0 || socketInfo->curSocket >= socketInfo->sockets.size()) {
1232 ✗ socketInfo->curSocket = 0;
1233 }
1234 168200 int sendSocket = socketInfo->sockets[socketInfo->curSocket];
1235 168200 errno = 0;
1236 // uint64_t st = GetTimeMicros();
1237
1238 // when the kernel is pacing this socket (fq + SO_MAX_PACING_RATE) the
1239 // socket buffer is sized for the whole frame; hand everything over at once
1240 // and skip the drain waits, the qdisc clocks packets out at the target rate
1241 168200 const bool paced = socketInfo->paced;
1242
2/2
✓ Branch 0 taken 9672 times.
✓ Branch 1 taken 158528 times.
168200 const int maxBatch = paced ? PACED_MSGS_PER_SENDMMSG : MSGS_PER_SENDMMSG;
1243
1244 168200 int outputCount = 0;
1245
3/4
✓ Branch 0 taken 844 times.
✓ Branch 1 taken 167356 times.
✓ Branch 2 taken 844 times.
✗ Branch 3 not taken.
168200 if (blockingOutput && !paced) {
1246 // Blocking mode only applies to unpaced sockets. For paced sockets
1247 // the kernel throttling that blocking mode approximates is done
1248 // properly by the fq qdisc, and the per-packet 1ms SO_SNDTIMEO would
1249 // abort a frame that legitimately sits in the paced buffer; they
1250 // always use the batched path below.
1251 844 int errorCount = 0;
1252
2/2
✓ Branch 0 taken 844 times.
✓ Branch 1 taken 844 times.
1688 for (int x = 0; x < msgCount; x++) {
1253
1/2
✓ Branch 0 taken 844 times.
✗ Branch 1 not taken.
844 flushBuffers(sendSocket, x, msgCount);
1254
1/2
✓ Branch 0 taken 844 times.
✗ Branch 1 not taken.
844 ssize_t s = sendmsg(sendSocket, &msgs[x].msg_hdr, 0);
1255
1/2
✓ Branch 0 taken 844 times.
✗ Branch 1 not taken.
844 if (s != -1) {
1256 844 errorCount = 0;
1257 844 ++outputCount;
1258 ✗ } else if (errorCount) {
1259 ✗ return outputCount;
1260 } else {
1261 // didn't send, we'll yield once and re-send
1262 ✗ --x;
1263 ✗ ++errorCount;
1264 ✗ std::this_thread::yield();
1265 }
1266 }
1267 844 } else {
1268
1/2
✓ Branch 0 taken 167356 times.
✗ Branch 1 not taken.
167356 int oc = sendmmsg(sendSocket, msgs, std::min(msgCount, maxBatch), MSG_DONTWAIT);
1269
2/2
✓ Branch 0 taken 166651 times.
✓ Branch 1 taken 705 times.
167356 if (oc > 0) {
1270 166651 outputCount += oc;
1271 }
1272
2/2
✓ Branch 0 taken 8878 times.
✓ Branch 1 taken 158478 times.
167356 if (outputCount != msgCount) {
1273 #ifndef PLATFORM_OSX
1274
2/2
✓ Branch 0 taken 7894 times.
✓ Branch 1 taken 984 times.
8878 if (!paced) {
1275
1/2
✓ Branch 0 taken 7894 times.
✗ Branch 1 not taken.
7894 flushBuffers(sendSocket, outputCount, msgCount);
1276 }
1277 #else
1278 // On OSX we have no good way to check the socket buffer, so just
1279 // sleep a bit to allow the stack to flush
1280 std::this_thread::sleep_for(std::chrono::microseconds(100));
1281 #endif
1282 8878 int outMsgCnt = msgCount - outputCount;
1283
1/2
✓ Branch 0 taken 8878 times.
✗ Branch 1 not taken.
8878 oc = sendmmsg(sendSocket, &msgs[outputCount], std::min(outMsgCnt, maxBatch), MSG_DONTWAIT);
1284
2/2
✓ Branch 0 taken 61827 times.
✓ Branch 1 taken 8878 times.
70705 while (oc > 0) {
1285 61827 outputCount += oc;
1286 #ifndef PLATFORM_OSX
1287
2/2
✓ Branch 0 taken 61602 times.
✓ Branch 1 taken 225 times.
61827 if (!paced) {
1288
1/2
✓ Branch 0 taken 61602 times.
✗ Branch 1 not taken.
61602 flushBuffers(sendSocket, outputCount, msgCount);
1289 }
1290 #else
1291 std::this_thread::sleep_for(std::chrono::microseconds(100));
1292 #endif
1293 61827 outMsgCnt = msgCount - outputCount;
1294
1/2
✓ Branch 0 taken 61827 times.
✗ Branch 1 not taken.
61827 oc = sendmmsg(sendSocket, &msgs[outputCount], std::min(outMsgCnt, maxBatch), MSG_DONTWAIT);
1295 }
1296 }
1297 }
1298 // uint64_t ed = GetTimeMicros();
1299 // uint64_t tm = ed - st;
1300 // printf("MSG: %d/%d %d \n", outputCount, msgCount, (int)tm);
1301
1302 168200 int errCount = 0;
1303 168200 auto retryStart = std::chrono::steady_clock::now();
1304
2/2
✓ Branch 0 taken 48905 times.
✓ Branch 1 taken 166602 times.
215507 while (outputCount != msgCount) {
1305 // Absolute wall-clock bound in addition to the consecutive-failure
1306 // count: a destination that needs more data than the pacing rate
1307 // allows keeps making slow progress (resetting errCount), and without
1308 // this cap the worker would hold sendLock across whole frames as a
1309 // permanent straggler.
1310
4/6
✓ Branch 0 taken 48905 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 48905 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 839 times.
✓ Branch 5 taken 48066 times.
48905 if (std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - retryStart).count() > 100) {
1311
3/4
✓ Branch 0 taken 839 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
✓ Branch 3 taken 821 times.
839 if (ShouldLogSendError(socketInfo, SEND_ERR_BUDGET))
1312
2/4
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
✗ Branch 3 not taken.
18 LogErr(VB_CHANNELOUT, "sendmmsg() send to %s exceeded time budget, dropping remainder of frame (output count: %d/%d)\n",
1313 HexToIP(socketKey).c_str(), outputCount, msgCount);
1314 839 return outputCount;
1315 }
1316
3/4
✓ Branch 0 taken 5237 times.
✓ Branch 1 taken 42829 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 5237 times.
48066 if (errno == EAGAIN || errno == EWOULDBLOCK) {
1317
2/2
✓ Branch 0 taken 40318 times.
✓ Branch 1 taken 2511 times.
42829 if (paced) {
1318 // the qdisc is intentionally holding packets; wait for the
1319 // buffer to drain at the pacing rate rather than spreading the
1320 // overflow across additional sockets, which would multiply the
1321 // per-controller rate cap and defeat the pacing
1322
1/2
✓ Branch 0 taken 40318 times.
✗ Branch 1 not taken.
40318 std::this_thread::sleep_for(std::chrono::milliseconds(2));
1323
2/2
✓ Branch 0 taken 2505 times.
✓ Branch 1 taken 6 times.
2511 } else if (socketInfo->preventClose) {
1324 // a shared socket pinned with ForceSocket (the ArtNet socket,
1325 // bound to port 6454 with SO_BROADCAST): an extra socket made
1326 // here would have neither, so broadcast ArtNet and ArtNet sync
1327 // would fail on it for good, and it would never be closed.
1328 // Wait for room on the one socket instead.
1329
1/2
✓ Branch 0 taken 2505 times.
✗ Branch 1 not taken.
2505 std::this_thread::sleep_for(std::chrono::milliseconds(1));
1330
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 } else if (socketKey != BROADCAST_MESSAGES_KEY) {
1331 6 ++socketInfo->curSocket;
1332
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1 times.
6 if (socketInfo->curSocket == socketInfo->sockets.size()) {
1333 // the extra socket must keep the multicast setup or the
1334 // packets will egress the default route instead of the
1335 // show network
1336
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
5 int s = socketInfo->sockets.size() < 5 ? createSocket(0, false, isMulticastKey(socketKey)) : -1;
1337
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
5 if (s != -1) {
1338
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 socketInfo->sockets.push_back(s);
1339 } else {
1340 1 socketInfo->curSocket = 0;
1341 }
1342 }
1343 6 sendSocket = socketInfo->sockets[socketInfo->curSocket];
1344 } else {
1345 ✗ return outputCount;
1346 }
1347 }
1348 48066 ++errCount;
1349
2/2
✓ Branch 0 taken 759 times.
✓ Branch 1 taken 47307 times.
48066 if (errCount >= 10) {
1350
3/4
✓ Branch 0 taken 759 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 751 times.
759 if (ShouldLogSendError(socketInfo, SEND_ERR_EAGAIN))
1351
3/6
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
8 LogErr(VB_CHANNELOUT, "sendmmsg() failed for UDP output (IP: %s Socket: %d output count: %d/%d) with error: %d %s\n",
1352 HexToIP(socketKey).c_str(), sendSocket,
1353 outputCount, msgCount,
1354 errno,
1355 FPPstrerror(errno));
1356 759 return outputCount;
1357 }
1358 47307 errno = 0;
1359
1/2
✓ Branch 0 taken 47307 times.
✗ Branch 1 not taken.
47307 int oc = sendmmsg(sendSocket, &msgs[outputCount], msgCount - outputCount, MSG_DONTWAIT);
1360
2/2
✓ Branch 0 taken 42862 times.
✓ Branch 1 taken 47307 times.
90169 while (oc > 0) {
1361
2/2
✓ Branch 0 taken 42794 times.
✓ Branch 1 taken 68 times.
42862 if (paced) {
1362 // making progress; for a paced socket EAGAIN is the expected
1363 // steady state when a frame exceeds the buffer, so only give
1364 // up after consecutive no-progress attempts. Unpaced sockets
1365 // keep the historical ~10-attempt bound so a congested
1366 // destination cannot pin a worker for a whole frame.
1367 42794 errCount = 0;
1368 }
1369 42862 outputCount += oc;
1370
1/2
✓ Branch 0 taken 42862 times.
✗ Branch 1 not taken.
42862 std::this_thread::sleep_for(std::chrono::microseconds(100));
1371
1/2
✓ Branch 0 taken 42862 times.
✗ Branch 1 not taken.
42862 oc = sendmmsg(sendSocket, &msgs[outputCount], msgCount - outputCount, MSG_DONTWAIT);
1372 }
1373 }
1374 166602 return outputCount;
1375 168290 }
1376
1377 1015 static void DoWorkThread(UDPOutput* output) {
1378
2/4
✓ Branch 0 taken 1015 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1015 times.
✗ Branch 3 not taken.
1015 SetThreadName("FPP-UDPWork");
1379 // Keep the send threads from being delayed by other load (video decode,
1380 // php, etc.). A delayed frame bunches with the next one and doubles the
1381 // burst hitting the switch. Priority sits below the output thread (10)
1382 // so the frame loop always preempts stragglers; worker count can grow to
1383 // the destination fan-out and these all park on workSignal when idle.
1384 1015 SetThreadRealtimePriority(5);
1385 1015 output->BackgroundOutputWork();
1386 1013 }
1387
1388 1015 void UDPOutput::BackgroundOutputWork() {
1389 std::chrono::high_resolution_clock clock;
1390
2/2
✓ Branch 0 taken 152110 times.
✓ Branch 1 taken 1013 times.
153123 while (runWorkThreads) {
1391
1/2
✓ Branch 0 taken 152110 times.
✗ Branch 1 not taken.
152110 std::unique_lock<std::mutex> lock(workMutex);
1392
5/6
✓ Branch 0 taken 152108 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 302488 times.
✓ Branch 3 taken 1013 times.
✓ Branch 4 taken 151095 times.
✓ Branch 5 taken 151393 times.
455611 workSignal.wait(lock, [this]() { return !runWorkThreads || !workQueue.empty(); });
1393
2/2
✓ Branch 0 taken 151095 times.
✓ Branch 1 taken 1013 times.
152108 if (!workQueue.empty()) {
1394 151095 WorkItem i = std::move(workQueue.front());
1395 151095 workQueue.pop_front();
1396
1/2
✓ Branch 0 taken 151095 times.
✗ Branch 1 not taken.
151095 sendingKeys.insert(i.id);
1397
1/2
✓ Branch 0 taken 151095 times.
✗ Branch 1 not taken.
151095 lock.unlock();
1398
1399 151095 auto t1 = clock.now();
1400
1/2
✓ Branch 0 taken 151095 times.
✗ Branch 1 not taken.
151095 int outputCount = SendMessages(i.id, i.socketInfo.get(), i.msgs);
1401 151095 auto t2 = clock.now();
1402
1403
2/4
✓ Branch 0 taken 151095 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 151095 times.
✗ Branch 3 not taken.
151095 long diff = std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1).count();
1404
2/2
✓ Branch 0 taken 1595 times.
✓ Branch 1 taken 149500 times.
151095 if (outputCount != i.msgs.size())
1405 1595 i.socketInfo->framesBehind.fetch_add(1, std::memory_order_relaxed);
1406
6/6
✓ Branch 0 taken 149500 times.
✓ Branch 1 taken 1595 times.
✓ Branch 2 taken 140 times.
✓ Branch 3 taken 149360 times.
✓ Branch 4 taken 1735 times.
✓ Branch 5 taken 149360 times.
151095 if ((outputCount != i.msgs.size()) || (diff > 100)) {
1407 1735 i.socketInfo->errCount++;
1408
1409 // failed to send all messages or it took more than 100ms to send them
1410
3/4
✓ Branch 0 taken 1735 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 34 times.
✓ Branch 3 taken 1701 times.
1735 if (ShouldLogSendError(i.socketInfo.get(), SEND_ERR_FRAME))
1411
6/8
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 34 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 32 times.
✓ Branch 6 taken 34 times.
✗ Branch 7 not taken.
68 LogErr(VB_CHANNELOUT, "%s() failed for UDP output (IP: %s output count: %d/%d time: %u ms errCount: %d) with error: %d %s\n",
1412 blockingOutput ? "sendmsg" : "sendmmsg", HexToIP(i.id).c_str(),
1413 outputCount, i.msgs.size(), diff, i.socketInfo->errCount.load(),
1414 errno,
1415 FPPstrerror(errno));
1416 } else {
1417 149360 i.socketInfo->clearErrors();
1418 }
1419
1420 // only count work for the current frame; if SendData timed out and
1421 // moved on, this completion must not skew the next frame's count.
1422 // The check and increment must be one unit under workMutex or a
1423 // generation bump between them counts old work toward the new frame.
1424 {
1425
1/2
✓ Branch 0 taken 151095 times.
✗ Branch 1 not taken.
151095 std::unique_lock<std::mutex> countLock(workMutex);
1426
2/4
✓ Branch 0 taken 151095 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 151095 times.
✗ Branch 3 not taken.
151095 sendingKeys.erase(sendingKeys.find(i.id));
1427
2/2
✓ Branch 0 taken 149943 times.
✓ Branch 1 taken 1152 times.
151095 if (i.generation == workGeneration) {
1428 149943 doneWorkCount++;
1429 }
1430 151095 }
1431 151095 }
1432 152108 }
1433
1434 1013 --numWorkThreads;
1435 1013 }
1436
1437 147458 int UDPOutput::SendData(unsigned char* channelData) {
1438
1/2
✓ Branch 0 taken 147458 times.
✗ Branch 1 not taken.
147458 std::unique_lock<std::mutex> lk(socketMutex);
1439
5/6
✓ Branch 0 taken 147458 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 312 times.
✓ Branch 3 taken 147146 times.
✓ Branch 4 taken 312 times.
✓ Branch 5 taken 147146 times.
147458 if (!enabled || messages.messages.empty()) {
1440 312 return 0;
1441 }
1442 147146 std::set<unsigned int> skippedKeys;
1443
2/2
✓ Branch 0 taken 164836 times.
✓ Branch 1 taken 147146 times.
311982 for (auto a : outputs) {
1444 // A skipped output prepared nothing, so it has nothing to commit.
1445
3/4
✓ Branch 0 taken 164836 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 161108 times.
✓ Branch 3 taken 3728 times.
164836 if (!skippedOutputs.count(a)) {
1446
1/2
✓ Branch 0 taken 161108 times.
✗ Branch 1 not taken.
161108 a->FrameSent();
1447 } else {
1448
1/2
✓ Branch 0 taken 3728 times.
✗ Branch 1 not taken.
3728 auto k = outputKeys.find(a);
1449
1/2
✓ Branch 0 taken 3728 times.
✗ Branch 1 not taken.
3728 if (k != outputKeys.end()) {
1450
1/2
✓ Branch 0 taken 3728 times.
✗ Branch 1 not taken.
3728 skippedKeys.insert(k->second.begin(), k->second.end());
1451 }
1452 }
1453 }
1454 // A destination PrepData() left alone because it was still busy misses
1455 // this frame. It has no messages, so the loop below counts nothing for
1456 // it: count the frame as late here, or a controller that keeps up with
1457 // only every other frame (each send slower than a frame, but inside the
1458 // send budget, so never short) never raises the pacing-budget banner.
1459
2/2
✓ Branch 0 taken 3728 times.
✓ Branch 1 taken 147146 times.
150874 for (unsigned int key : skippedKeys) {
1460
1/2
✓ Branch 0 taken 3728 times.
✗ Branch 1 not taken.
3728 auto m = messages.messages.find(key);
1461
1/2
✓ Branch 0 taken 3728 times.
✗ Branch 1 not taken.
3728 auto si = messages.sendSockets.find(key);
1462
5/10
✓ Branch 0 taken 3728 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3728 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3728 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 3728 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 3728 times.
✗ Branch 9 not taken.
3728 if ((m == messages.messages.end() || m->second.empty()) && si != messages.sendSockets.end() && si->second) {
1463 3728 si->second->bytesQueued.fetch_add(si->second->lastFrameBytes, std::memory_order_relaxed);
1464 3728 si->second->framesQueued.fetch_add(1, std::memory_order_relaxed);
1465 3728 si->second->framesBehind.fetch_add(1, std::memory_order_relaxed);
1466 }
1467 }
1468
2/2
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 147103 times.
147146 if (++statCheckCounter >= 1500) {
1469 // roughly once a minute at typical frame rates; run off-thread so the
1470 // tc fork in the qdisc check can't hiccup the frame
1471 43 statCheckCounter = 0;
1472
1/2
✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
43 if (!statCheckRunning.exchange(true)) {
1473 // the socket map is only safe to walk under socketMutex, held here
1474 43 std::vector<PacingBudgetDest> dests;
1475
2/2
✓ Branch 0 taken 54 times.
✓ Branch 1 taken 43 times.
97 for (auto& si : messages.sendSockets) {
1476
7/8
✓ Branch 0 taken 54 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 44 times.
✓ Branch 3 taken 10 times.
✓ Branch 4 taken 34 times.
✓ Branch 5 taken 10 times.
✓ Branch 6 taken 34 times.
✓ Branch 7 taken 20 times.
54 if (si.second && si.first > ARTNET_MESSAGES_KEY && si.first < LATE_MESSAGES_START)
1477 34 dests.push_back({ si.first, HexToIP(si.first), si.second });
1478 }
1479 // a copy: the list itself can grow under socketMutex while the
1480 // stats thread runs; the entries live as long as this instance
1481
1/4
✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
129 std::thread([this, dests = std::move(dests), outs = outputs]() {
1482
2/4
✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 43 times.
✗ Branch 3 not taken.
43 SetThreadName("FPP-UDPStats");
1483 try {
1484
1/2
✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
43 CheckLocalDrops();
1485
1/2
✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
43 CheckPacingBudget(dests, outs);
1486 ✗ } catch (...) {
1487 // an escaped exception on a detached thread would call
1488 // std::terminate and take down the show over a diagnostic
1489 ✗ }
1490 43 statCheckRunning = false;
1491
2/4
✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 43 times.
✗ Branch 3 not taken.
86 }).detach();
1492 43 }
1493 }
1494 std::chrono::high_resolution_clock clock;
1495
2/2
✓ Branch 0 taken 146429 times.
✓ Branch 1 taken 717 times.
147146 if (useThreadedOutput) {
1496 unsigned int generation;
1497 {
1498 // start a new generation so late completions of the previous
1499 // frame's work don't count toward this frame.
1500
1/2
✓ Branch 0 taken 146429 times.
✗ Branch 1 not taken.
146429 std::unique_lock<std::mutex> lock(workMutex);
1501 146429 generation = ++workGeneration;
1502 146429 doneWorkCount = 0;
1503 146429 }
1504 146429 int total = 0;
1505 146429 auto t1 = clock.now();
1506
2/2
✓ Branch 0 taken 172921 times.
✓ Branch 1 taken 146429 times.
319350 for (auto& msgs : messages.messages) {
1507
6/6
✓ Branch 0 taken 167573 times.
✓ Branch 1 taken 5348 times.
✓ Branch 2 taken 151095 times.
✓ Branch 3 taken 16478 times.
✓ Branch 4 taken 151095 times.
✓ Branch 5 taken 21826 times.
172921 if (!msgs.second.empty() && msgs.first < LATE_MESSAGES_START) {
1508
1/2
✓ Branch 0 taken 151095 times.
✗ Branch 1 not taken.
151095 std::shared_ptr<SendSocketInfo> socketInfo = findOrCreateSocket(msgs.first);
1509
1/2
✓ Branch 0 taken 151095 times.
✗ Branch 1 not taken.
151095 std::unique_lock<std::mutex> lock(workMutex);
1510 // A destination that still has an unstarted item from an
1511 // earlier frame is behind: SendMessages() serializes on its
1512 // sendLock, so another item would only queue up behind it, and
1513 // every queued item gets a worker thread below. A destination
1514 // that can't keep up (more data than its pacing rate allows)
1515 // grew one item and one blocked thread per frame until thread
1516 // creation failed and std::terminate took fppd down. Fold this
1517 // frame into the pending item instead. Nothing is lost: the
1518 // iovecs point at the live packet buffers, so the pending item
1519 // sends this frame's data when it runs, and the union keeps any
1520 // message the dedup logic left out of this frame but not that one.
1521
1/2
✓ Branch 0 taken 151095 times.
✗ Branch 1 not taken.
151095 auto pending = std::find_if(workQueue.begin(), workQueue.end(),
1522 14475 [&](const WorkItem& w) { return w.id == msgs.first; });
1523 151095 socketInfo->lastFrameBytes = MessageBytes(msgs.second);
1524 151095 socketInfo->bytesQueued.fetch_add(socketInfo->lastFrameBytes, std::memory_order_relaxed);
1525 151095 socketInfo->framesQueued.fetch_add(1, std::memory_order_relaxed);
1526
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 151095 times.
151095 if (pending != workQueue.end()) {
1527 ✗ socketInfo->framesBehind.fetch_add(1, std::memory_order_relaxed);
1528 ✗ std::set<std::pair<const void*, const void*>> have;
1529 ✗ for (auto& m : pending->msgs)
1530 ✗ have.emplace(m.msg_hdr.msg_iov, m.msg_hdr.msg_name);
1531 ✗ for (auto& m : msgs.second) {
1532 ✗ if (!have.count({ m.msg_hdr.msg_iov, m.msg_hdr.msg_name }))
1533 ✗ pending->msgs.push_back(m);
1534 }
1535 ✗ pending->generation = generation;
1536 ✗ } else {
1537
1/2
✓ Branch 0 taken 151095 times.
✗ Branch 1 not taken.
151095 workQueue.emplace_back(msgs.first, socketInfo, msgs.second, generation);
1538 }
1539
1/2
✓ Branch 0 taken 151095 times.
✗ Branch 1 not taken.
151095 lock.unlock();
1540 151095 workSignal.notify_one();
1541 151095 ++total;
1542 151095 }
1543 }
1544
1/2
✓ Branch 0 taken 146429 times.
✗ Branch 1 not taken.
146429 std::unique_lock<std::mutex> lock(workMutex);
1545
2/2
✓ Branch 0 taken 1015 times.
✓ Branch 1 taken 146429 times.
147444 while (numWorkThreads < workQueue.size()) {
1546
2/4
✓ Branch 0 taken 1015 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1015 times.
✗ Branch 3 not taken.
1015 std::thread(DoWorkThread, this).detach();
1547 1015 numWorkThreads++;
1548 }
1549
2/2
✓ Branch 0 taken 140430 times.
✓ Branch 1 taken 5999 times.
146429 if (workQueue.size()) {
1550 140430 workSignal.notify_all();
1551 }
1552
1/2
✓ Branch 0 taken 146429 times.
✗ Branch 1 not taken.
146429 lock.unlock();
1553 146429 auto t2 = clock.now();
1554
8/10
✓ Branch 0 taken 403744 times.
✓ Branch 1 taken 145277 times.
✓ Branch 2 taken 403744 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 403744 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 402592 times.
✓ Branch 7 taken 1152 times.
✓ Branch 8 taken 402592 times.
✓ Branch 9 taken 146429 times.
549021 while (doneWorkCount != total && std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1).count() < 50) {
1555
1/2
✓ Branch 0 taken 402592 times.
✗ Branch 1 not taken.
402592 std::this_thread::sleep_for(std::chrono::microseconds(250));
1556 402592 t2 = clock.now();
1557 }
1558
2/2
✓ Branch 0 taken 145277 times.
✓ Branch 1 taken 1152 times.
146429 if (doneWorkCount == total) {
1559 #ifndef PLATFORM_OSX
1560 // now make sure the buffers are drained for the early packets so that they are
1561 // fully received before we send the late packets (sync or broadcast).
1562 // Only worth the wait if there actually are late packets this frame.
1563 145277 bool haveLate = false;
1564
2/2
✓ Branch 0 taken 171769 times.
✓ Branch 1 taken 128799 times.
300568 for (auto& msgs : messages.messages) {
1565
6/6
✓ Branch 0 taken 166421 times.
✓ Branch 1 taken 5348 times.
✓ Branch 2 taken 16478 times.
✓ Branch 3 taken 149943 times.
✓ Branch 4 taken 16478 times.
✓ Branch 5 taken 155291 times.
171769 if (!msgs.second.empty() && msgs.first >= LATE_MESSAGES_START) {
1566 16478 haveLate = true;
1567 16478 break;
1568 }
1569 }
1570
2/2
✓ Branch 0 taken 16478 times.
✓ Branch 1 taken 128799 times.
145277 if (haveLate) {
1571 // Collect every early-message socket that may still have data
1572 // queued, then wait on them together: the drains overlap in
1573 // wall-clock, so the bound is one drain, not one per destination.
1574 16478 std::vector<int> drainFds;
1575 16478 bool anyPaced = false;
1576
2/2
✓ Branch 0 taken 32960 times.
✓ Branch 1 taken 16478 times.
49438 for (auto& msgs : messages.messages) {
1577
6/6
✓ Branch 0 taken 32863 times.
✓ Branch 1 taken 97 times.
✓ Branch 2 taken 16385 times.
✓ Branch 3 taken 16478 times.
✓ Branch 4 taken 16385 times.
✓ Branch 5 taken 16575 times.
32960 if (!msgs.second.empty() && msgs.first < LATE_MESSAGES_START) {
1578
1/2
✓ Branch 0 taken 16385 times.
✗ Branch 1 not taken.
16385 std::shared_ptr<SendSocketInfo> socketInfo = findOrCreateSocket(msgs.first);
1579 // try_lock: a straggler worker still sending here means
1580 // sync ordering is already lost for this destination;
1581 // don't stall the frame thread waiting for it
1582 16385 std::unique_lock<std::mutex> lg(socketInfo->sendLock, std::try_to_lock);
1583
3/6
✓ Branch 0 taken 16385 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16385 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 16385 times.
✗ Branch 5 not taken.
16385 if (lg.owns_lock() && !socketInfo->sockets.empty()) {
1584
1/2
✓ Branch 0 taken 16385 times.
✗ Branch 1 not taken.
16385 drainFds.push_back(socketInfo->sockets[socketInfo->curSocket]);
1585 16385 anyPaced |= socketInfo->paced;
1586 }
1587 16385 }
1588 }
1589 // Paced packets sit in the fq qdisc for several ms; sleep
1590 // proportionally to the deepest queue instead of busy-polling.
1591 // The bound must cover a full send buffer draining at the
1592 // pacing rate (~34ms for 384KB at 90Mbps) or the sync packet
1593 // can beat the tail of the data it synchronizes.
1594
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 16474 times.
16478 int totalBudget = anyPaced ? 35000 : 5000;
1595 16478 int waited = 0;
1596
2/2
✓ Branch 0 taken 25495 times.
✓ Branch 1 taken 184 times.
25679 while (waited < totalBudget) {
1597 25495 int maxBytes = 0;
1598
2/2
✓ Branch 0 taken 25402 times.
✓ Branch 1 taken 25495 times.
50897 for (int fd : drainFds) {
1599 25402 int b = 0;
1600
5/6
✓ Branch 0 taken 25402 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9201 times.
✓ Branch 3 taken 16201 times.
✓ Branch 4 taken 9201 times.
✓ Branch 5 taken 16201 times.
25402 if (ioctl(fd, SIOCOUTQ, &b) == 0 && b > maxBytes) {
1601 9201 maxBytes = b;
1602 }
1603 }
1604
2/2
✓ Branch 0 taken 16294 times.
✓ Branch 1 taken 9201 times.
25495 if (maxBytes <= 1024) {
1605 16294 break;
1606 }
1607 9201 int us = 100;
1608
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 9201 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
9201 if (anyPaced && pacingDrainRate > 0) {
1609 ✗ us = std::clamp((int)(((uint64_t)maxBytes * 8000000ULL) / pacingDrainRate), 100, 2000);
1610 }
1611 9201 us = std::min(us, totalBudget - waited);
1612
1/2
✓ Branch 0 taken 9201 times.
✗ Branch 1 not taken.
9201 std::this_thread::sleep_for(std::chrono::microseconds(us));
1613 9201 waited += us;
1614 }
1615 16478 }
1616 #endif
1617 // now output the LATE/Broadcast packets (likely sync packets)
1618
2/2
✓ Branch 0 taken 171769 times.
✓ Branch 1 taken 145277 times.
317046 for (auto& msgs : messages.messages) {
1619
2/2
✓ Branch 0 taken 166421 times.
✓ Branch 1 taken 5348 times.
171769 if (!msgs.second.empty()) {
1620
1/2
✓ Branch 0 taken 166421 times.
✗ Branch 1 not taken.
166421 std::shared_ptr<SendSocketInfo> socketInfo = findOrCreateSocket(msgs.first);
1621
2/2
✓ Branch 0 taken 16478 times.
✓ Branch 1 taken 149943 times.
166421 if (msgs.first >= LATE_MESSAGES_START) {
1622 16478 t1 = clock.now();
1623
1/2
✓ Branch 0 taken 16478 times.
✗ Branch 1 not taken.
16478 int outputCount = SendMessages(msgs.first, socketInfo.get(), msgs.second);
1624 16478 t2 = clock.now();
1625
2/4
✓ Branch 0 taken 16478 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16478 times.
✗ Branch 3 not taken.
16478 long diff = std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1).count();
1626
5/6
✓ Branch 0 taken 16385 times.
✓ Branch 1 taken 93 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 16385 times.
✓ Branch 4 taken 93 times.
✓ Branch 5 taken 16385 times.
16478 if ((outputCount != msgs.second.size()) || (diff > 100)) {
1627 93 socketInfo->errCount++;
1628
1629 // failed to send all messages or it took more than 100ms to send them
1630
3/4
✓ Branch 0 taken 93 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 92 times.
93 if (ShouldLogSendError(socketInfo.get(), SEND_ERR_FRAME))
1631
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
2 LogErr(VB_CHANNELOUT, "sendmmsg() failed for UDP output (IP: %s output count: %d/%d time: %u ms errCount: %d) with error: %d %s\n",
1632 HexToIP(msgs.first).c_str(),
1633 outputCount, msgs.second.size(), diff, socketInfo->errCount.load(),
1634 errno,
1635 FPPstrerror(errno));
1636 } else {
1637 16385 socketInfo->clearErrors();
1638 }
1639 }
1640
2/2
✓ Branch 0 taken 276 times.
✓ Branch 1 taken 166145 times.
332842 if (socketInfo->errCount.load(std::memory_order_relaxed) >= 3) {
1641 // we'll ping the controllers and rebuild the valid message list, this could take time
1642
1/2
✓ Branch 0 taken 276 times.
✗ Branch 1 not taken.
276 PingControllers(false);
1643 276 socketInfo->clearErrors();
1644 }
1645 166421 }
1646 }
1647 }
1648 146429 return 1;
1649 146429 }
1650
2/2
✓ Branch 0 taken 717 times.
✓ Branch 1 taken 717 times.
1434 for (auto& msgs : messages.messages) {
1651
1/2
✓ Branch 0 taken 717 times.
✗ Branch 1 not taken.
717 if (!msgs.second.empty()) {
1652
1/2
✓ Branch 0 taken 717 times.
✗ Branch 1 not taken.
717 std::shared_ptr<SendSocketInfo> socketInfo = findOrCreateSocket(msgs.first);
1653 717 auto t1 = clock.now();
1654
1/2
✓ Branch 0 taken 717 times.
✗ Branch 1 not taken.
717 int outputCount = SendMessages(msgs.first, socketInfo.get(), msgs.second);
1655 717 auto t2 = clock.now();
1656
2/4
✓ Branch 0 taken 717 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 717 times.
✗ Branch 3 not taken.
717 long diff = std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1).count();
1657 717 socketInfo->bytesQueued.fetch_add(MessageBytes(msgs.second), std::memory_order_relaxed);
1658 717 socketInfo->framesQueued.fetch_add(1, std::memory_order_relaxed);
1659
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 717 times.
717 if (outputCount != msgs.second.size())
1660 ✗ socketInfo->framesBehind.fetch_add(1, std::memory_order_relaxed);
1661
5/6
✓ Branch 0 taken 717 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 243 times.
✓ Branch 3 taken 474 times.
✓ Branch 4 taken 243 times.
✓ Branch 5 taken 474 times.
717 if ((outputCount != msgs.second.size()) || (diff > 100)) {
1662 243 socketInfo->errCount++;
1663
1664 // failed to send all messages or it took more than 100ms to send them
1665
3/4
✓ Branch 0 taken 243 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
✓ Branch 3 taken 225 times.
243 if (ShouldLogSendError(socketInfo.get(), SEND_ERR_FRAME))
1666
3/6
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 18 times.
✗ Branch 5 not taken.
36 LogErr(VB_CHANNELOUT, "sendmmsg() failed for UDP output (IP: %s output count: %d/%d time: %u ms errCount: %d) with error: %d %s\n",
1667 HexToIP(msgs.first).c_str(),
1668 outputCount, msgs.second.size(), diff, socketInfo->errCount.load(),
1669 errno,
1670 FPPstrerror(errno));
1671
1672
2/2
✓ Branch 0 taken 77 times.
✓ Branch 1 taken 166 times.
486 if (socketInfo->errCount.load(std::memory_order_relaxed) >= 3) {
1673 // we'll ping the controllers and rebuild the valid message list, this could take time
1674
1/2
✓ Branch 0 taken 77 times.
✗ Branch 1 not taken.
77 PingControllers(false);
1675 77 socketInfo->clearErrors();
1676 }
1677 } else {
1678 474 socketInfo->clearErrors();
1679 }
1680 717 }
1681 }
1682 717 return 1;
1683
2/6
✓ Branch 0 taken 34 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 34 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
147492 }
1684
1685 // Not 57: that id belongs to the plugin-reinstall warning (Plugins.cpp), and
1686 // sharing it gave this banner that warning's title and help.
1687
1688 #ifndef PLATFORM_OSX
1689 // Total packets dropped by the qdiscs on the ethernet interfaces. This is
1690 // the only place fq enqueue drops (e.g. flow_limit) are visible: they return
1691 // NET_XMIT_CN, which UDP reports to the sender as success, so neither
1692 // SndbufErrors nor tx_dropped ever increments for them.
1693 43 static uint64_t qdiscDroppedCount() {
1694 43 uint64_t total = 0;
1695
5/8
✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 43 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 80 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 80 times.
✓ Branch 7 taken 43 times.
123 for (const auto& entry : std::filesystem::directory_iterator("/sys/class/net/")) {
1696
2/4
✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 80 times.
✗ Branch 3 not taken.
80 std::string dev = entry.path().filename();
1697
3/4
✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 47 times.
✓ Branch 3 taken 33 times.
80 if (!IsShowEthernetInterface(dev)) {
1698 47 continue;
1699 }
1700
4/8
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 33 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 33 times.
✗ Branch 7 not taken.
33 std::string cmd = TcPath() + " -s qdisc show dev " + dev + " 2>/dev/null";
1701
1/2
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
33 FILE* pipe = popen(cmd.c_str(), "r");
1702
1/2
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
33 if (pipe) {
1703 char buf[512];
1704
3/4
✓ Branch 0 taken 178 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 145 times.
✓ Branch 3 taken 33 times.
178 while (fgets(buf, sizeof(buf), pipe)) {
1705 145 const char* d = strstr(buf, "(dropped ");
1706
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 115 times.
145 if (d) {
1707 30 total += strtoull(d + 9, nullptr, 10);
1708 }
1709 }
1710
1/2
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
33 pclose(pipe);
1711 }
1712
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 47 times.
123 }
1713 43 return total;
1714 }
1715 #endif
1716
1717 // Called with the unicast destinations from the frame thread's snapshot, on
1718 // the stats thread. Raises the over-budget banner for a paced destination that
1719 // fell behind on more than 1% of its frames since the last check, and clears it
1720 // on the first check that doesn't.
1721 43 static void CheckPacingBudget(const std::vector<PacingBudgetDest>& dests,
1722 const std::list<UDPOutputData*>& outputs) {
1723 43 auto now = std::chrono::steady_clock::now();
1724
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 43 times.
77 for (auto& [key, ip, si] : dests) {
1725
2/4
✓ Branch 0 taken 34 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 34 times.
✗ Branch 3 not taken.
34 double secs = std::chrono::duration<double>(now - si->budgetCheckTime).count();
1726 34 si->budgetCheckTime = now;
1727 34 uint64_t bytes = si->bytesQueued.exchange(0);
1728 34 unsigned int frames = si->framesQueued.exchange(0);
1729 34 unsigned int behind = si->framesBehind.exchange(0);
1730 34 unsigned int rate = si->pacedRate;
1731 // a handful of late frames is a hiccup, not something to change config for
1732
7/10
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 30 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 3 times.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 3 times.
34 if (rate == 0 || frames == 0 || secs <= 0 || behind < 10 || behind * 100ULL < frames) {
1733
1/2
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
31 ClearPacingBudgetWarning(si.get());
1734 31 continue;
1735 }
1736 // Rounded up to 10 Mbps so the text, which is what identifies the
1737 // banner, stays the same from one check to the next.
1738 3 unsigned int needMbps = (unsigned int)std::ceil(bytes * 8 / secs / 1000000.0 / 10.0) * 10;
1739 3 unsigned int pacedMbps = (unsigned int)(rate * 8ULL / 1000000ULL);
1740
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 std::string name = ip;
1741
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 for (auto o : outputs) {
1742 3 bool valid = false;
1743
5/10
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 3 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 3 times.
✗ Branch 9 not taken.
3 if (!o->description.empty() && UDPOutputData::toInetAddr(o->ipAddress, valid) == (in_addr_t)key && valid) {
1744
3/6
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
3 name += " (" + o->description + ")";
1745 3 break;
1746 }
1747 }
1748
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 LogWarn(VB_CHANNELOUT, "UDP output to %s: %u of %u frames late or incomplete in the last %.0f s; sending ~%u Mbps, paced at %u Mbps\n",
1749 name.c_str(), behind, frames, secs, needMbps, pacedMbps);
1750
3/8
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
5 std::string need = needMbps > pacedMbps ? "needs about " + std::to_string(needMbps) + " Mbps but is paced at "
1751
8/12
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
✓ Branch 7 taken 2 times.
✓ Branch 8 taken 2 times.
✓ Branch 9 taken 1 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
7 : "is sending close to its pacing rate of ";
1752
5/10
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 3 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 3 times.
✗ Branch 9 not taken.
6 RaisePacingBudgetWarning(si.get(), "Controller " + name + " " + need + std::to_string(pacedMbps) +
1753
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 " Mbps, so its frames arrive late or incomplete and its lights will stutter.");
1754 3 }
1755 43 }
1756
1757 43 void UDPOutput::CheckLocalDrops() {
1758 #ifndef PLATFORM_OSX
1759 // The protocols have no acks, so the only drops we can actually see are the
1760 // local ones. Surface them so users can tell "player is dropping" apart
1761 // from "switch/controller is dropping".
1762 43 uint64_t sndbufErrors = 0;
1763
3/6
✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 43 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 43 times.
✗ Branch 5 not taken.
43 std::istringstream snmp(GetFileContents("/proc/net/snmp"));
1764 43 std::string line;
1765 43 std::vector<std::string> headers;
1766
3/6
✓ Branch 0 taken 430 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 430 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 430 times.
✗ Branch 5 not taken.
430 while (std::getline(snmp, line)) {
1767
3/4
✓ Branch 0 taken 430 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 86 times.
✓ Branch 3 taken 344 times.
430 if (line.rfind("Udp:", 0) == 0) {
1768
1/2
✓ Branch 0 taken 86 times.
✗ Branch 1 not taken.
86 std::istringstream ss(line);
1769 86 std::vector<std::string> tokens;
1770 86 std::string token;
1771
4/6
✓ Branch 0 taken 946 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 946 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 860 times.
✓ Branch 5 taken 86 times.
946 while (ss >> token) {
1772
1/2
✓ Branch 0 taken 860 times.
✗ Branch 1 not taken.
860 tokens.push_back(token);
1773 }
1774
2/2
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 43 times.
86 if (headers.empty()) {
1775
1/2
✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
43 headers = tokens;
1776 } else {
1777
5/6
✓ Branch 0 taken 387 times.
✓ Branch 1 taken 43 times.
✓ Branch 2 taken 387 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 387 times.
✓ Branch 5 taken 43 times.
430 for (size_t x = 1; x < headers.size() && x < tokens.size(); x++) {
1778
3/4
✓ Branch 0 taken 387 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 43 times.
✓ Branch 3 taken 344 times.
387 if (headers[x] == "SndbufErrors") {
1779 // strtoull rather than stoull: this runs on a detached
1780 // thread where an unexpected exception is fatal
1781 43 sndbufErrors = strtoull(tokens[x].c_str(), nullptr, 10);
1782 }
1783 }
1784 43 break;
1785 }
1786
6/6
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 43 times.
✓ Branch 2 taken 43 times.
✓ Branch 3 taken 43 times.
✓ Branch 4 taken 43 times.
✓ Branch 5 taken 43 times.
172 }
1787 }
1788
3/6
✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 43 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 43 times.
✗ Branch 5 not taken.
43 uint64_t txDropped = strtoull(GetFileContents("/sys/class/net/" + outInterface + "/statistics/tx_dropped").c_str(), nullptr, 10);
1789
1/2
✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
43 uint64_t qdiscDropped = qdiscDroppedCount();
1790
1791 // counters can go backwards if the interface bounces or a driver reloads;
1792 // rebase silently rather than computing a huge unsigned delta
1793
5/8
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 25 times.
✓ Branch 2 taken 18 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 18 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 18 times.
✗ Branch 7 not taken.
43 if (statBaselineValid && sndbufErrors >= lastSndbufErrors && txDropped >= lastTxDropped && qdiscDropped >= lastQdiscDropped) {
1794 18 uint64_t sbDelta = sndbufErrors - lastSndbufErrors;
1795 18 uint64_t txDelta = txDropped - lastTxDropped;
1796 18 uint64_t qdDelta = qdiscDropped - lastQdiscDropped;
1797
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 15 times.
18 if (sbDelta) {
1798 // host-wide counter (covers every UDP socket of every process),
1799 // so log it as a diagnostic breadcrumb but don't raise a warning
1800
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 LogWarn(VB_CHANNELOUT, "System-wide UDP send buffer errors since last check: %llu (may include non-FPP traffic)\n",
1801 (unsigned long long)sbDelta);
1802 }
1803
3/4
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 16 times.
18 if (txDelta || qdDelta) {
1804
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 LogWarn(VB_CHANNELOUT, "Local packet drops since last check: %llu on interface %s, %llu in ethernet qdiscs\n",
1805 (unsigned long long)txDelta, outInterface.c_str(), (unsigned long long)qdDelta);
1806
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (!dropWarningActive) {
1807
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 WarningHolder::AddWarning(LOCAL_DROP_WARNING_ID, LOCAL_DROP_WARNING);
1808 2 dropWarningActive = true;
1809 }
1810
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 } else if (dropWarningActive) {
1811 ✗ WarningHolder::RemoveWarning(LOCAL_DROP_WARNING_ID, LOCAL_DROP_WARNING);
1812 ✗ dropWarningActive = false;
1813 }
1814 }
1815 43 lastSndbufErrors = sndbufErrors;
1816 43 lastTxDropped = txDropped;
1817 43 lastQdiscDropped = qdiscDropped;
1818 43 statBaselineValid = true;
1819 #endif
1820 43 }
1821
1822 3294 void UDPOutput::PingControllers(bool failedOnly) {
1823
1/2
✓ Branch 0 taken 3294 times.
✗ Branch 1 not taken.
3294 LogExcess(VB_CHANNELOUT, "Pinging controllers to see what is online\n");
1824 // Held across the pings: a host removed meanwhile (its output deleted)
1825 // would be pinged again after removePingTarget() stopped it, and that
1826 // ping's callback would outlive this output.
1827
1/2
✓ Branch 0 taken 3294 times.
✗ Branch 1 not taken.
3294 std::lock_guard<std::mutex> lk(pingHostsLock);
1828
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3294 times.
3294 if (pingsStopped) {
1829 ✗ return;
1830 }
1831
2/2
✓ Branch 0 taken 162 times.
✓ Branch 1 taken 3294 times.
3456 for (auto& h : pingHosts) {
1832 162 std::shared_ptr<PingHost> host = h.second;
1833
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 114 times.
162 if (failedOnly) {
1834
1/2
✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
48 std::unique_lock<std::mutex> l(host->lock);
1835
5/6
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 38 times.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 10 times.
✓ Branch 5 taken 38 times.
48 if (host->answered && host->failCount == 0) {
1836 10 continue;
1837 }
1838
2/2
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 10 times.
48 }
1839
2/4
✓ Branch 0 taken 152 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 152 times.
✗ Branch 3 not taken.
152 PingManager::INSTANCE.addPeriodicPing(host->address, UDP_PING_TIMEOUT, 15000, [this, host](int ms) {
1840 74 OnPingResult(host, ms);
1841 74 });
1842
2/2
✓ Branch 0 taken 152 times.
✓ Branch 1 taken 10 times.
162 }
1843
1/2
✓ Branch 0 taken 3294 times.
✗ Branch 1 not taken.
3294 }
1844
1845 // caller holds host.lock
1846 18 void UDPOutput::SetHostValid(PingHost& host, bool valid) {
1847 18 host.valid = valid;
1848
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 18 times.
38 for (auto o : host.outputs) {
1849 20 o->valid = valid;
1850 }
1851
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 13 times.
18 if (valid) {
1852
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
5 WarningHolder::RemoveWarning(27, host.warning);
1853 5 LogWarn(VB_CHANNELOUT, "Could ping host %s, re-adding to outputs\n", host.address.c_str());
1854 } else {
1855
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 WarningHolder::AddWarning(27, host.warning);
1856 13 LogWarn(VB_CHANNELOUT, "Could not ping host %s, removing from output\n", host.address.c_str());
1857 }
1858 18 }
1859
1860 138 void UDPOutput::OnPingResult(const std::shared_ptr<PingHost>& host, int ms) {
1861
1/2
✓ Branch 0 taken 138 times.
✗ Branch 1 not taken.
138 std::unique_lock<std::mutex> l(host->lock);
1862
2/2
✓ Branch 0 taken 91 times.
✓ Branch 1 taken 47 times.
138 if (ms > 0) {
1863 91 host->failCount = 0;
1864
2/2
✓ Branch 0 taken 74 times.
✓ Branch 1 taken 17 times.
91 if (!host->answered) {
1865 74 host->answered = true;
1866 74 --failedCount;
1867 }
1868
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 86 times.
91 if (!host->valid) {
1869
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 SetHostValid(*host, true);
1870 }
1871 91 return;
1872 }
1873
1/2
✓ Branch 0 taken 47 times.
✗ Branch 1 not taken.
47 LogDebug(VB_CHANNELOUT, "Could not ping host %s Fail count: %d Currently Valid: %d\n", host->address.c_str(), host->failCount, host->valid);
1874 47 host->failCount++;
1875
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 18 times.
47 if (host->failCount == 1) {
1876 // ignore a single ping failure, could be transient
1877
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 13 times.
29 } else if (host->failCount == 2) {
1878 // if two pings fail, we'll try a HEAD request via HTTP. One at a
1879 // time: a probe can take the whole curl timeout, and the pings keep
1880 // failing meanwhile.
1881
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (host->headPending) {
1882 ✗ return;
1883 }
1884 16 host->headPending = true;
1885
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 l.unlock();
1886 16 std::shared_ptr<PingHost> h = host;
1887
7/14
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 16 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 16 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 16 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 16 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 16 times.
✗ Branch 13 not taken.
96 CurlManager::INSTANCE.add("http://" + h->address + "/", "HEAD", "", {}, [this, h](int rc, const std::string& resp) {
1888
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 std::unique_lock<std::mutex> l(h->lock);
1889 13 h->headPending = false;
1890
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 if (rc) {
1891 ✗ h->failCount = 0;
1892 ✗ if (!h->answered) {
1893 ✗ h->answered = true;
1894 ✗ --failedCount;
1895 }
1896 ✗ if (!h->valid) {
1897 ✗ SetHostValid(*h, true);
1898 }
1899 }
1900 13 },
1901 16 curlOwner);
1902
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
29 } else if (host->failCount >= 3) {
1903 // three pings and a HEAD request failed, mark invalid
1904
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 if (host->valid) {
1905
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 SetHostValid(*host, false);
1906 }
1907
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 if (host->failCount > 5) {
1908 // make sure we wrap around so another HEAD request later may pick it up
1909 ✗ host->failCount = 0;
1910 }
1911 }
1912
2/2
✓ Branch 0 taken 47 times.
✓ Branch 1 taken 91 times.
138 }
1913 977 void UDPOutput::DumpConfig() {
1914 977 ChannelOutput::DumpConfig();
1915 977 LogDebug(VB_CHANNELOUT, " Interface : %s\n", outInterface.c_str());
1916 977 LogDebug(VB_CHANNELOUT, " Threaded : %d\n", useThreadedOutput);
1917 977 LogDebug(VB_CHANNELOUT, " Blocking : %d\n", blockingOutput);
1918 977 LogDebug(VB_CHANNELOUT, " Needs Broadcast : %d\n", needsBroadcast);
1919
2/2
✓ Branch 0 taken 1094 times.
✓ Branch 1 taken 977 times.
2071 for (auto u : outputs) {
1920
1/2
✓ Branch 0 taken 1094 times.
✗ Branch 1 not taken.
1094 u->DumpConfig();
1921 }
1922 977 }
1923
1924 334618 std::shared_ptr<SendSocketInfo> UDPOutput::findOrCreateSocket(unsigned int socketKey, int sc) {
1925
1/2
✓ Branch 0 taken 334618 times.
✗ Branch 1 not taken.
334618 auto it = messages.sendSockets.find(socketKey);
1926 334618 std::shared_ptr<SendSocketInfo> info;
1927
2/2
✓ Branch 0 taken 1051 times.
✓ Branch 1 taken 333567 times.
334618 if (it == messages.sendSockets.end()) {
1928
1/2
✓ Branch 0 taken 1051 times.
✗ Branch 1 not taken.
1051 info = std::make_shared<SendSocketInfo>();
1929
1/2
✓ Branch 0 taken 1051 times.
✗ Branch 1 not taken.
1051 messages.sendSockets[socketKey] = info;
1930 } else {
1931 333567 info = it->second;
1932 }
1933
1934 // try_lock: if a straggler worker from a previous frame is still sending
1935 // to this destination, its sockets already exist and the pacing check can
1936 // wait a frame - nothing here is worth stalling the frame thread on
1937 334618 std::unique_lock<std::mutex> lg(info->sendLock, std::try_to_lock);
1938
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 334618 times.
334618 if (!lg.owns_lock()) {
1939 ✗ return info;
1940 }
1941
2/2
✓ Branch 0 taken 1229 times.
✓ Branch 1 taken 333389 times.
334618 if (info->sockets.empty()) {
1942 1229 bool broadCast = socketKey == BROADCAST_MESSAGES_KEY;
1943 1229 bool multiCast = isMulticastKey(socketKey);
1944
2/2
✓ Branch 0 taken 1229 times.
✓ Branch 1 taken 1051 times.
2280 for (int x = info->sockets.size(); x < sc; x++) {
1945
1/2
✓ Branch 0 taken 1229 times.
✗ Branch 1 not taken.
1229 int s = createSocket(0, broadCast, multiCast);
1946
2/2
✓ Branch 0 taken 178 times.
✓ Branch 1 taken 1051 times.
1229 if (s == -1) {
1947 178 break;
1948 }
1949
1/2
✓ Branch 0 taken 1051 times.
✗ Branch 1 not taken.
1051 info->sockets.push_back(s);
1950 }
1951 }
1952 #if !defined(PLATFORM_OSX) && defined(SO_MAX_PACING_RATE)
1953 // Pacing is only applied to unicast destination sockets: the key IS
1954 // the destination IP, so the rate cap is truly per controller, and we
1955 // can check that the route's egress interface actually has fq (wifi
1956 // and other non-fq routes keep the legacy drain-based pacing). The
1957 // shared multicast socket is left unpaced - it carries the aggregate
1958 // of all multicast controllers so a per-controller rate would be wrong.
1959 // Not while the socket couldn't be opened: the decision latches, and the
1960 // socket made later would never get the rate and buffer it applies.
1961
4/4
✓ Branch 0 taken 69101 times.
✓ Branch 1 taken 179487 times.
✓ Branch 2 taken 68923 times.
✓ Branch 3 taken 178 times.
248588 if (pacingEnabled && !info->pacedChecked && !info->sockets.empty() &&
1962
8/8
✓ Branch 0 taken 248588 times.
✓ Branch 1 taken 86030 times.
✓ Branch 2 taken 17986 times.
✓ Branch 3 taken 50937 times.
✓ Branch 4 taken 1508 times.
✓ Branch 5 taken 16478 times.
✓ Branch 6 taken 1508 times.
✓ Branch 7 taken 333110 times.
583206 socketKey > ARTNET_MESSAGES_KEY && socketKey < LATE_MESSAGES_START) {
1963
2/2
✓ Branch 0 taken 497 times.
✓ Branch 1 taken 1011 times.
1508 if (info->pacedCheckCountdown > 0) {
1964 497 --info->pacedCheckCountdown;
1965 } else {
1966
1/2
✓ Branch 0 taken 1011 times.
✗ Branch 1 not taken.
1011 std::string iface = egressInterfaceFor((in_addr_t)socketKey);
1967
2/2
✓ Branch 0 taken 1007 times.
✓ Branch 1 taken 4 times.
1011 if (!iface.empty()) {
1968 // only latch the decision once the route has resolved; during
1969 // startup the route may not exist yet (interface waiting on
1970 // DHCP) and we want to try again later or on an address change
1971 1007 info->pacedChecked = true;
1972 // per-destination rate override (e.g. a slower cap for an
1973 // ESP32-class controller, or a faster one for a gigabit FPP
1974 // instance) falls back to the global pacingRate. ~0U in the
1975 // map means the controller was explicitly set to unpaced.
1976 1007 unsigned int destRate = pacingRate;
1977
1/2
✓ Branch 0 taken 1007 times.
✗ Branch 1 not taken.
1007 auto rit = perDestPacingRate.find((in_addr_t)socketKey);
1978
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1005 times.
1007 if (rit != perDestPacingRate.end()) {
1979 2 destRate = rit->second;
1980 }
1981 // pace only if this destination actually has a rate cap (a
1982 // global/override rate that isn't 0 or explicitly-unpaced) AND
1983 // the route egresses an fq interface
1984
6/8
✓ Branch 0 taken 1007 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1006 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 1006 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 120 times.
✓ Branch 7 taken 886 times.
1007 bool shouldPace = destRate != 0 && destRate != ~0U && interfaceHasFQ(iface);
1985
2/2
✓ Branch 0 taken 121 times.
✓ Branch 1 taken 886 times.
1007 if (shouldPace != info->paced) {
1986
4/6
✓ Branch 0 taken 118 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 121 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 121 times.
✗ Branch 5 not taken.
121 LogInfo(VB_CHANNELOUT, "UDP output to %s via %s is %s\n",
1987 HexToIP(socketKey).c_str(), iface.c_str(),
1988 shouldPace ? "paced" : "not paced (no fq qdisc on egress)");
1989 121 info->paced = shouldPace;
1990
2/2
✓ Branch 0 taken 118 times.
✓ Branch 1 taken 3 times.
121 info->pacedRate = shouldPace ? destRate : 0;
1991 // ~0 = unlimited; used when un-pacing after the route
1992 // moved to a non-fq interface (e.g. eth -> wifi failover)
1993
2/2
✓ Branch 0 taken 118 times.
✓ Branch 1 taken 3 times.
121 unsigned int rate = shouldPace ? destRate : ~0U;
1994 // paced sockets need the buffer to hold a full frame for
1995 // the qdisc to clock out; unpaced revert to the small
1996 // buffer the drain-based pacing depends on
1997
4/6
✓ Branch 0 taken 118 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 118 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 3 times.
121 int bufSize = shouldPace ? pacedSndBufSize() : ((blockingOutput ? 4096 : (MSGS_PER_SENDMMSG * 1511)) - 1);
1998
2/2
✓ Branch 0 taken 121 times.
✓ Branch 1 taken 121 times.
242 for (int s : info->sockets) {
1999 121 setsockopt(s, SOL_SOCKET, SO_MAX_PACING_RATE, &rate, sizeof(rate));
2000 121 setsockopt(s, SOL_SOCKET, SO_SNDBUF, &bufSize, sizeof(bufSize));
2001 }
2002 }
2003 } else {
2004 // no route: don't re-run the lookup syscalls on the hot path
2005 // every frame, retry after a few seconds worth of frames
2006 4 info->pacedCheckCountdown = 150;
2007 }
2008 1011 }
2009 }
2010 #endif
2011
2/2
✓ Branch 0 taken 1066 times.
✓ Branch 1 taken 333552 times.
334618 if (info->curSocket == -1) {
2012 1066 info->curSocket = 0;
2013 }
2014 334618 return info;
2015 334618 }
2016
2017 1243 int UDPOutput::createSocket(int port, bool broadCast, bool multiCast) {
2018 1243 int sendSocket = OpenSocket(AF_INET, SOCK_DGRAM, 0);
2019
2/2
✓ Branch 0 taken 178 times.
✓ Branch 1 taken 1065 times.
1243 if (sendSocket < 0) {
2020 178 int err = errno;
2021 // Out of file descriptors, typically. This used to exit(1) and take
2022 // the show down; the destination is left without a socket, its sends
2023 // fail and are counted, and the next frame tries again, so output
2024 // comes back once descriptors free up (as e131bridge does since
2025 // 42183b6d4). That is a try per destination per frame, so the log
2026 // line is limited to one every few seconds.
2027 static std::atomic<long long> lastLog{ 0 };
2028
1/2
✓ Branch 0 taken 178 times.
✗ Branch 1 not taken.
178 long long now = GetMonotonicTimeMS();
2029 178 long long last = lastLog.load();
2030
6/8
✓ Branch 0 taken 176 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 176 times.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 176 times.
180 if ((last == 0 || now - last >= SEND_ERR_LOG_INTERVAL_MS) && lastLog.compare_exchange_strong(last, now)) {
2031
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 LogErr(VB_CHANNELOUT, "Error opening datagram socket: %s\n", FPPstrerror(err));
2032 }
2033 178 return -1;
2034 }
2035
2036 1065 errno = 0;
2037 // Disable loopback so I do not receive my own datagrams.
2038 1065 char loopch = 0;
2039
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1065 times.
1065 if (setsockopt(sendSocket, IPPROTO_IP, IP_MULTICAST_LOOP, (char*)&loopch, sizeof(loopch)) < 0) {
2040 ✗ LogErr(VB_CHANNELOUT, "Error setting IP_MULTICAST_LOOP error\n");
2041 ✗ close(sendSocket);
2042 ✗ return -1;
2043 }
2044 // Mark show traffic as Expedited Forwarding (DSCP EF). Managed switches
2045 // prioritize it out of the box and on wifi it maps to the WMM voice queue.
2046 1065 int tos = 0xB8;
2047 1065 setsockopt(sendSocket, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
2048 #ifndef PLATFORM_OSX
2049 // ahead of fppd's own HTTP/MQTT/curl traffic in the local qdisc
2050 1065 int prio = 6;
2051 1065 setsockopt(sendSocket, SOL_SOCKET, SO_PRIORITY, &prio, sizeof(prio));
2052 #endif
2053 // make sure the send buffer is actually set to a reasonable size for non-blocking mode
2054 // (paced sockets get a larger buffer in findOrCreateSocket)
2055
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 1030 times.
1065 int bufSize = (blockingOutput ? 4096 : (MSGS_PER_SENDMMSG * 1511)) - 1;
2056 1065 setsockopt(sendSocket, SOL_SOCKET, SO_SNDBUF, &bufSize, sizeof(bufSize));
2057 // these sockets are for sending only, don't need a large receive buffer so
2058 // free some memory by setting to just a single page
2059 1065 bufSize = 4096;
2060 1065 setsockopt(sendSocket, SOL_SOCKET, SO_RCVBUF, &bufSize, sizeof(bufSize));
2061
2062
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 1030 times.
1065 if (blockingOutput) {
2063 struct timeval timeout;
2064 35 timeout.tv_sec = 0;
2065 35 timeout.tv_usec = 1000; // 1ms timeout
2066 35 setsockopt(sendSocket, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof timeout);
2067 }
2068
2069
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 1055 times.
1065 if (broadCast) {
2070 10 int broadcast = 1;
2071
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (setsockopt(sendSocket, SOL_SOCKET, SO_BROADCAST, &broadcast, sizeof(broadcast)) < 0) {
2072 ✗ LogErr(VB_CHANNELOUT, "Error setting SO_BROADCAST: %s\n", FPPstrerror(errno));
2073 ✗ close(sendSocket);
2074 ✗ return -1;
2075 }
2076 #ifndef PLATFORM_OSX
2077
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 1034 times.
1055 } else if (multiCast) {
2078 static struct sockaddr_in address;
2079 21 memcpy(&address, &localAddress, sizeof(localAddress));
2080 21 address.sin_port = ntohs(port);
2081
2082
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (setsockopt(sendSocket, IPPROTO_IP, IP_MULTICAST_IF, (char*)&address, sizeof(address)) < 0) {
2083 ✗ LogErr(VB_CHANNELOUT, "Error setting IP_MULTICAST_IF error\n");
2084 }
2085 #endif
2086 }
2087
4/4
✓ Branch 0 taken 1055 times.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 21 times.
✓ Branch 3 taken 1034 times.
1065 if (broadCast || multiCast) {
2088 static struct sockaddr_in address;
2089 31 memcpy(&address, &localAddress, sizeof(localAddress));
2090 31 address.sin_port = ntohs(port);
2091
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
31 if (bind(sendSocket, (struct sockaddr*)&address, sizeof(struct sockaddr_in)) == -1) {
2092 ✗ LogErr(VB_CHANNELOUT, "Error in bind:errno=%d, %s\n", errno, FPPstrerror(errno));
2093 }
2094 #ifndef PLATFORM_OSX
2095
2/4
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 31 times.
31 if (connect(sendSocket, (struct sockaddr*)&address, sizeof(address)) < 0) {
2096 ✗ LogErr(VB_CHANNELOUT, "Error connecting IP_MULTICAST_LOOP socket\n");
2097 }
2098 #endif
2099 }
2100 1065 return sendSocket;
2101 }
2102
2103 987 bool UDPOutput::InitNetwork(bool rebuildSockets) {
2104 // Returns true when the output interface has a usable address.
2105 char E131LocalAddress[16];
2106
1/2
✓ Branch 0 taken 987 times.
✗ Branch 1 not taken.
987 int rv = GetInterfaceAddress(outInterface.c_str(), E131LocalAddress, NULL, NULL);
2107
1/2
✓ Branch 0 taken 987 times.
✗ Branch 1 not taken.
987 LogDebug(VB_CHANNELOUT, "UDPLocalAddress = %s\n", E131LocalAddress);
2108 // GetInterfaceAddress() reports 127.0.0.1 for an interface without an
2109 // address; the entire 127.* subnet is localhost
2110
4/4
✓ Branch 0 taken 951 times.
✓ Branch 1 taken 36 times.
✓ Branch 2 taken 947 times.
✓ Branch 3 taken 4 times.
987 bool haveAddress = rv == 0 && strncmp(E131LocalAddress, "127.", 4) != 0;
2111
2112 // createSocket() binds broadcast/multicast sockets to this address and
2113 // sends multicast out of it, and it runs on the frame thread under
2114 // socketMutex, so the address changes under that lock too. It is
2115 // refreshed every time: resetting it to loopback and then returning early
2116 // because sockets already existed is what left multicast going out of lo
2117 // after an address change. After an address change the sockets are
2118 // dropped in the same critical section, so a frame can't rebuild them on
2119 // the old address between the two.
2120
1/2
✓ Branch 0 taken 987 times.
✗ Branch 1 not taken.
987 std::unique_lock<std::mutex> lk(socketMutex);
2121 987 memset(&localAddress, 0, sizeof(struct sockaddr_in));
2122 987 localAddress.sin_family = AF_INET;
2123 987 localAddress.sin_addr.s_addr = inet_addr(E131LocalAddress);
2124
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 977 times.
987 if (rebuildSockets) {
2125
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 messages.clearSockets();
2126 }
2127
6/6
✓ Branch 0 taken 951 times.
✓ Branch 1 taken 36 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 948 times.
✓ Branch 4 taken 3 times.
✓ Branch 5 taken 984 times.
987 if (rv == 0 && !interfaceWarning.empty()) {
2128 // An interface that had no address yet (DHCP still waiting, cable
2129 // out) is usable once NEW_ADDR brings one; the banner was otherwise
2130 // only retracted at the next config load.
2131
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 WarningHolder::RemoveWarning(22, interfaceWarning);
2132 3 interfaceWarning.clear();
2133 }
2134
2135
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 987 times.
987 if (!messages.sendSockets.empty()) {
2136 ✗ return haveAddress;
2137 }
2138
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 970 times.
987 if (needsBroadcast) {
2139
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 10 times.
17 if (rv) {
2140
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 LogErr(VB_CHANNELOUT, "Invalid interface %s\n", outInterface.c_str());
2141
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 interfaceWarning = "Invalid interface for UDP broadcast/multicast: " + outInterface;
2142
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 AddOutputConfigWarning(22, interfaceWarning);
2143 7 return false;
2144 }
2145
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (!haveAddress) {
2146 ✗ return false;
2147 }
2148
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 int broadcastSocket = createSocket(0, true);
2149
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 messages.ForceSocket(BROADCAST_MESSAGES_KEY, broadcastSocket);
2150 }
2151 980 return haveAddress;
2152 987 }
2153
2154 1040 void UDPOutput::StartingOutput() {
2155 1040 outputRunning = true;
2156
2/2
✓ Branch 0 taken 1242 times.
✓ Branch 1 taken 1040 times.
2282 for (auto a : outputs) {
2157
7/8
✓ Branch 0 taken 1231 times.
✓ Branch 1 taken 11 times.
✓ Branch 2 taken 1217 times.
✓ Branch 3 taken 14 times.
✓ Branch 4 taken 1217 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1217 times.
✓ Branch 7 taken 25 times.
1242 if (a->valid && a->active && !a->started) {
2158 1217 a->started = true;
2159
1/2
✓ Branch 0 taken 1217 times.
✗ Branch 1 not taken.
1217 a->StartingOutput();
2160 }
2161 }
2162 1040 }
2163 1039 void UDPOutput::StoppingOutput() {
2164 // Every output that was started is stopped, whether or not it is still
2165 // valid: one whose ping failed mid-show would otherwise keep whatever
2166 // StartingOutput() set up (Twinkly's token timer) past its own deletion.
2167 1039 outputRunning = false;
2168
2/2
✓ Branch 0 taken 1243 times.
✓ Branch 1 taken 1039 times.
2282 for (auto a : outputs) {
2169
2/2
✓ Branch 0 taken 1219 times.
✓ Branch 1 taken 24 times.
1243 if (a->started) {
2170 1219 a->started = false;
2171
1/2
✓ Branch 0 taken 1219 times.
✗ Branch 1 not taken.
1219 a->StoppingOutput();
2172 }
2173 }
2174 1039 }
2175
2176 234 std::string UDPOutput::HexToIP(unsigned int hex) {
2177 // Extract each byte in LSB order
2178 234 uint8_t octet1 = hex & 0xFF; // Least significant byte
2179 234 uint8_t octet2 = (hex >> 8) & 0xFF;
2180 234 uint8_t octet3 = (hex >> 16) & 0xFF;
2181 234 uint8_t octet4 = (hex >> 24) & 0xFF; // Most significant byte
2182
2183
1/2
✓ Branch 0 taken 234 times.
✗ Branch 1 not taken.
489 return std::to_string(octet1) + "." +
2184
2/4
✓ Branch 0 taken 234 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 234 times.
✗ Branch 3 not taken.
915 std::to_string(octet2) + "." +
2185
2/4
✓ Branch 0 taken 234 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 234 times.
✗ Branch 3 not taken.
915 std::to_string(octet3) + "." +
2186
1/2
✓ Branch 0 taken 234 times.
✗ Branch 1 not taken.
681 std::to_string(octet4);
2187 }
2188