Debian Codegraph @

src/flow.c

raw symbols SCIP
1 /* librist. Copyright © 2019-2020 SipRadius LLC. All right reserved.
2 * Author: Daniele Lacamera <[email protected]>
3 * Author: Kuldeep Singh Dhaka <[email protected]>
4 * Author: Sergio Ammirata <[email protected]>
5 *
6 * SPDX-License-Identifier: BSD-2-Clause
7 */
8
9 #include "rist-private.h"
10 #include "log-private.h"
11 #include "udp-private.h"
12 #include "proto/rist_time.h"
13 #include <assert.h>
14
15 void rist_receiver_missing(struct rist_flow *f, struct rist_peer *peer,uint64_t nack_time, uint32_t seq, uint64_t rtt)
16 {
17 struct rist_missing_buffer *m = calloc(1, sizeof(*m));
18 if (!m)
19 return;
20 uint64_t now = timestampNTP_u64();
21 if (nack_time > now)
22 nack_time = now;
23 if (nack_time < (now - f->recovery_buffer_ticks))
24 nack_time = now;
25 m->seq = seq;
26 m->insertion_time = nack_time;
27
28 m->next_nack = now + rtt;
29 m->peer = peer;
30
31 if (get_cctx(peer)->debug)
32 rist_log_priv(get_cctx(peer), RIST_LOG_DEBUG,
33 "Datagram %" PRIu32 " is missing, inserting into the missing queue "
34 "with deadline in %" PRIu64 "ms (queue=%d), last_seq_found %"PRIu32"\n",
35 seq, m->next_nack > now? (m->next_nack - now)/ RIST_CLOCK: 0, f->missing_counter, f->last_seq_found);
36
37 m->next = NULL;
38 // Insert it at the end of the queue
39 if (!f->missing) {
40 f->missing = m;
41 f->missing_tail = m;
42 } else {
43 f->missing_tail->next = m;
44 f->missing_tail = m;
45 }
46 f->missing_counter++;
47 }
48
49 void empty_receiver_queue(struct rist_flow *f, struct rist_common_ctx *ctx)
50 {
51 size_t output_queue_idx = atomic_load_explicit(&f->receiver_queue_output_idx, memory_order_acquire);
52 size_t counter = output_queue_idx;
53 while (atomic_load_explicit(&f->receiver_queue_size, memory_order_acquire) > 0) {
54 struct rist_buffer *b = f->receiver_queue[counter];
55 if (b)
56 {
57 f->receiver_queue[counter] = NULL;
58 atomic_fetch_sub_explicit(&f->receiver_queue_size, b->size, memory_order_release);
59 free_rist_buffer(ctx, b);
60 }
61 counter = (counter + 1) % f->receiver_queue_max;
62 if (counter == output_queue_idx) {
63 // full loop complete
64 break;
65 }
66 }
67 }
68
69 void rist_flush_missing_flow_queue(struct rist_flow *flow)
70 {
71 struct rist_missing_buffer *current = flow->missing;
72 while (current)
73 {
74 struct rist_missing_buffer *delme = current;
75 current = current->next;
76 free(delme);
77 delme = NULL;
78 }
79 flow->missing = NULL;
80 flow->missing_counter = 0;
81 }
82
83 void rist_delete_flow(struct rist_receiver *ctx, struct rist_flow *f)
84 {
85 rist_log_priv(&ctx->common, RIST_LOG_INFO, "Triggering data output thread termination\n");
86 //This needs to be before the lock, as we may (happens rarely) fail to acquire it at all, due to output thread
87 //locking/unlocking too quickly.
88 atomic_store_explicit(&f->shutdown, 1, memory_order_release);
89 pthread_mutex_lock(&f->mutex);
90 bool running = f->receiver_thread_running;
91 pthread_mutex_unlock(&f->mutex);
92 if (running)
93 pthread_join(f->receiver_thread, NULL);
94 rist_log_priv(&ctx->common, RIST_LOG_INFO, "Resetting peer states\n");
95 struct rist_peer *p = NULL;
96 for (size_t i = 0; i <f->peer_lst_len; i++)
97 {
98 p = f->peer_lst[i];
99 if (p->peer_data) {
100 p->peer_data->flow = NULL;
101 p->peer_data->authenticated = false;
102 }
103 if (p->peer_rtcp) {
104 p->peer_rtcp->flow = NULL;
105 p->peer_rtcp->authenticated = false;
106 }
107 p->authenticated = false;
108 p->flow = NULL;
109 if (ctx->common.profile == RIST_PROFILE_SIMPLE && p->parent && p->parent->flow == f) {
110 if (p->parent->peer_rtcp) {
111 p->parent->peer_rtcp->flow = NULL;
112 p->parent->peer_rtcp->authenticated = false;
113 }
114 if (p->parent->peer_data) {
115 p->parent->peer_data->flow = NULL;
116 p->parent->peer_data->authenticated = false;
117 }
118 p->parent->authenticated = false;
119 p->parent->flow = NULL;
120 }
121 }
122 struct rist_peer *peer = ctx->common.PEERS;
123 while (peer)
124 {
125 if (peer->flow == f) {
126 peer->flow = NULL;
127 }
128 peer = peer->next;
129 }
130
131 rist_log_priv(&ctx->common, RIST_LOG_INFO, "Free flow peer list\n");
132 f->peer_lst_len = 0;
133 free(f->peer_lst);
134 f->peer_lst = NULL;
135
136 rist_log_priv(&ctx->common, RIST_LOG_INFO, "Deleting missing queue elements\n");
137 /* Delete all missing queue elements (if any) */
138 rist_flush_missing_flow_queue(f);
139
140 rist_log_priv(&ctx->common, RIST_LOG_INFO, "Deleting output buffer data\n");
141 /* Delete all buffer data (if any) */
142 empty_receiver_queue(f, &ctx->common);
143
144 rist_log_priv(&ctx->common, RIST_LOG_INFO, "Freeing data fifo queue\n");
145 for (size_t i = 0; i < ctx->fifo_queue_size; i++)
146 {
147 if (f->dataout_fifo_queue[i])
148 {
149 free_data_block(&f->dataout_fifo_queue[i]);
150 }
151 }
152 free(f->dataout_fifo_queue);
153 /* Unlink from the flow list under flows_lock so that concurrent
154 * readers (rist_receiver_data_read2) cannot observe a dangling
155 * pointer. The free happens after the lock is released. */
156 rist_log_priv(&ctx->common, RIST_LOG_INFO, "Deleting flow\n");
157 pthread_mutex_lock(&ctx->common.flows_lock);
158 struct rist_flow **prev_flow = &ctx->common.FLOWS;
159 struct rist_flow *current_flow = *prev_flow;
160 while (current_flow)
161 {
162 if (current_flow == f) {
163 *prev_flow = current_flow->next;
164 break;
165 }
166 prev_flow = &current_flow->next;
167 current_flow = current_flow->next;
168 }
169 pthread_mutex_unlock(&ctx->common.flows_lock);
170 free(f->receiver_queue);
171 free(f);
172 }
173
174 static void rist_flow_append(struct rist_flow **FLOWS, struct rist_flow *f)
175 {
176 if (*FLOWS == NULL) {
177 // First entry
178 *FLOWS = f;
179 return;
180 }
181
182 // Append
183 struct rist_flow *last = *FLOWS;
184 while (last->next) {
185 last = last->next;
186 }
187
188 last->next = f;
189 }
190
191 /* Soft cap on simultaneous receiver flows. A peer can advertise an
192 * arbitrary 32-bit flow_id and we always create_flow() on a previously
193 * unseen one, so without a cap an attacker that can land authenticated
194 * traffic (or any traffic on a non-encrypted receiver) can exhaust
195 * memory by walking flow_id space. 256 is well past any legitimate
196 * deployment we've seen. */
197 #define RIST_MAX_FLOWS 256
198 #define RIST_MAX_PEERS_PER_FLOW 256
199
200 static struct rist_flow *create_flow(struct rist_receiver *ctx, uint32_t flow_id)
201 {
202 struct rist_flow *f = calloc(1, sizeof(*f));
203 if (!f) {
204 rist_log_priv(&ctx->common, RIST_LOG_ERROR,
205 "Could not create receiver buffer of size %d MB, OOM\n", sizeof(*f) / 1000000);
206 return NULL;
207 }
208
209 /* Recovery ring: Simple/Main are hard-capped at 16 bits; Advanced uses
210 * the configured capacity. Heap-allocated so the 16-bit profiles don't
211 * pay the Advanced footprint. Advanced flows default to 32-bit framing
212 * but recv_data refines short_seq from the wire so a Main-framed source
213 * still works; the ring size stays fixed per context regardless. */
214 if (ctx->common.profile < RIST_PROFILE_ADVANCED) {
215 f->short_seq = true;
216 f->receiver_queue_max = UINT16_SIZE;
217 } else {
218 f->short_seq = false;
219 f->receiver_queue_max = ctx->common.recovery_queue_max;
220 }
221 f->receiver_queue = calloc(f->receiver_queue_max, sizeof(*f->receiver_queue));
222 if (!f->receiver_queue) {
223 rist_log_priv(&ctx->common, RIST_LOG_ERROR,
224 "OOM allocating receiver recovery ring (%zu entries)\n", f->receiver_queue_max);
225 free(f);
226 return NULL;
227 }
228
229 f->flow_id = flow_id;
230 f->receiver_id = ctx->id;
231 f->stats_next_time = timestampNTP_u64();
232 f->max_output_jitter = ctx->common.rist_max_jitter;
233
234 /* Output pacing; max_output_jitter becomes the wake ceiling. The occupancy
235 * trim stays off (target 0): target_output_time already governs latency. */
236 f->cbr_output = ctx->common.cbr_output;
237 f->cbr_output_min_us = ctx->common.cbr_output_min_us;
238 rist_pacer_rate_init(&f->cbr_rate, 0);
239 rist_pacer_init(&f->cbr_pacer, 0.0,
240 (uint64_t)f->cbr_output_min_us * 1000,
241 (uint64_t)(f->max_output_jitter / RIST_CLOCK) * 1000000ULL);
242 f->cbr_max_hold_us = rist_cbr_hold_us((uint32_t)(f->max_output_jitter / RIST_CLOCK));
243 atomic_init(&f->cbr_arrived_bytes, 0);
244
245 f->dataout_fifo_queue = calloc(ctx->fifo_queue_size, sizeof(*f->dataout_fifo_queue));
246 if (!f->dataout_fifo_queue) {
247 rist_log_priv(&ctx->common, RIST_LOG_ERROR,
248 "OOM allocating dataout fifo queue (%zu entries)\n", ctx->fifo_queue_size);
249 free(f->receiver_queue);
250 free(f);
251 return NULL;
252 }
253 int ret = pthread_cond_init(&f->condition, NULL);
254 if (ret) {
255 free(f->dataout_fifo_queue);
256 free(f->receiver_queue);
257 free(f);
258 rist_log_priv(&ctx->common, RIST_LOG_ERROR, "Error %d calling pthread_cond_init\n", ret);
259 return NULL;
260 }
261
262 ret = pthread_mutex_init(&f->mutex, NULL);
263 if (ret){
264 pthread_cond_destroy(&f->condition);
265 free(f->dataout_fifo_queue);
266 free(f->receiver_queue);
267 free(f);
268 rist_log_priv(&ctx->common, RIST_LOG_ERROR, "Error %d calling pthread_mutex_init\n", ret);
269 return NULL;
270 }
271
272 atomic_init(&f->receiver_queue_size, 0);
273 atomic_init(&f->receiver_queue_output_idx, 0);
274 atomic_init(&f->dataout_fifo_queue_write_index, 0);
275 atomic_init(&f->dataout_fifo_queue_read_index, 0);
276 atomic_init(&f->fifo_overflow, false);
277
278 f->session_timeout = RIST_DEFAULT_SESSION_TIMEOUT * RIST_CLOCK;
279 f->flow_timeout = 250 * RIST_CLOCK;
280
281 /* Count + cap-check + append under one critical section so concurrent
282 * callers cannot each observe a sub-cap count and each insert. */
283 pthread_mutex_lock(&ctx->common.flows_lock);
284 size_t flow_count = 0;
285 for (struct rist_flow *cur = ctx->common.FLOWS; cur != NULL; cur = cur->next)
286 flow_count++;
287 if (flow_count >= RIST_MAX_FLOWS) {
288 pthread_mutex_unlock(&ctx->common.flows_lock);
289 rist_log_priv(&ctx->common, RIST_LOG_ERROR,
290 "Refusing to create FLOW #%"PRIu32" inside create_flow: "
291 "cap of %d flows reached (race with concurrent caller)\n",
292 flow_id, RIST_MAX_FLOWS);
293 pthread_mutex_destroy(&f->mutex);
294 pthread_cond_destroy(&f->condition);
295 free(f->dataout_fifo_queue);
296 free(f->receiver_queue);
297 free(f);
298 return NULL;
299 }
300 rist_flow_append(&ctx->common.FLOWS, f);
301 pthread_mutex_unlock(&ctx->common.flows_lock);
302 f->logging_settings = ctx->common.logging_settings;
303
304 return f;
305 }
306
307 static bool flow_has_peer(struct rist_flow *f, uint32_t flow_id, uint32_t peer_id)
308 {
309 for (size_t j = 0; j < f->peer_lst_len; j++) {
310 struct rist_peer *p = f->peer_lst[j];
311 if (p->adv_flow_id == flow_id && p->adv_peer_id == peer_id) {
312 return true;
313 }
314 }
315
316 return false;
317 }
318
319 int rist_receiver_associate_flow(struct rist_peer *p, uint32_t flow_id)
320 {
321 struct rist_receiver *ctx = p->receiver_ctx;
322 int ret = 0;
323
324 // Find the flow based on the flow_id
325 struct rist_flow *f = NULL;
326 if (ctx->common.profile > RIST_PROFILE_SIMPLE)
327 {
328 pthread_mutex_lock(&ctx->common.flows_lock);
329 size_t flow_count = 0;
330 for (struct rist_flow *cur = ctx->common.FLOWS; cur != NULL; cur = cur->next) {
331 flow_count++;
332 if (cur->flow_id == flow_id) {
333 f = cur;
334 }
335 }
336 pthread_mutex_unlock(&ctx->common.flows_lock);
337 if (!f && flow_count >= RIST_MAX_FLOWS) {
338 rist_log_priv(&ctx->common, RIST_LOG_ERROR,
339 "Refusing to create FLOW #%"PRIu32": cap of %d flows reached\n",
340 flow_id, RIST_MAX_FLOWS);
341 return -1;
342 }
343 } else
344 {
345 if (!p->parent) {
346 rist_log_priv(&ctx->common, RIST_LOG_ERROR, "FLOW #%"PRIu32" cannot be created yet because this peer has no parent\n", flow_id);
347 return -1;
348 }
349 f = p->parent->flow;
350 }
351
352 /* create flow if necessary */
353 if (!f) {
354 f = create_flow(ctx, flow_id);
355 ret = 1;
356 if (!f) {
357 return -1;
358 }
359 if (p->config.timing_mode == RIST_TIMING_MODE_RTC)
360 f->rtc_timing_mode = true;
361
362 /* short_seq + receiver_queue_max are set (and the ring allocated)
363 * inside create_flow() now. */
364 f->recovery_buffer_ticks = p->recovery_buffer_ticks;
365 rist_log_priv(&ctx->common, RIST_LOG_INFO, "FLOW #%"PRIu32" created (short=%d)\n", flow_id, f->short_seq);
366 } else {
367 /* double check that this peer is not a member of this flow already */
368 if (flow_has_peer(f, flow_id, p->adv_peer_id)) {
369 rist_log_priv(&ctx->common, RIST_LOG_INFO, "FLOW #%"PRIu32", Existing peer (id=%"PRIu32") re-joining existing flow ...\n",
370 flow_id, p->adv_peer_id);
371 ret = 2;
372 } else {
373 rist_log_priv(&ctx->common, RIST_LOG_INFO, "FLOW #%"PRIu32": New peer (id=%u) joining existing flow ...\n",
374 flow_id, p->adv_peer_id);
375 ret = 1;
376 }
377 }
378
379 if (p->config.recovery_length_min != p->config.recovery_length_max) {
380 f->flow_auto_buffer_scaling = true;
381 }
382
383 if (p->session_timeout > f->session_timeout) {
384 f->session_timeout = p->session_timeout;
385 rist_log_priv(&ctx->common, RIST_LOG_INFO, "Setting flow session timeout to %"PRIu64"ms\n", f->session_timeout / RIST_CLOCK);
386 }
387
388 // Set the flow timeout as the buffer size for the flow
389 // However, we start with 250 ms as the minimum/default
390 // to make sure it is larger than the RTCP interval
391 if (f->recovery_buffer_ticks > f->flow_timeout)
392 f->flow_timeout = f->recovery_buffer_ticks;
393 uint64_t stats_report_time = get_cctx(p)->stats_report_time;
394 if (stats_report_time != 0 && stats_report_time != f->stats_report_time)
395 f->stats_report_time = stats_report_time;
396
397 // Set/update max missing counter
398 if (f->missing_counter_max < p->missing_counter_max)
399 f->missing_counter_max = p->missing_counter_max;
400
401 /* now assign flow to peer and add to list */
402 p->flow = f;
403 p->adv_flow_id = flow_id;
404 if (ret == 1) {
405 /* f->mutex protects peer_lst against all concurrent walkers/writers. */
406 pthread_mutex_lock(&f->mutex);
407 if (f->peer_lst_len >= RIST_MAX_PEERS_PER_FLOW) {
408 pthread_mutex_unlock(&f->mutex);
409 rist_log_priv(&ctx->common, RIST_LOG_ERROR,
410 "Refusing to attach peer (id=%u) to flow #%"PRIu32": "
411 "cap of %d peers/flow reached\n",
412 p->adv_peer_id, flow_id, RIST_MAX_PEERS_PER_FLOW);
413 p->flow = NULL;
414 return -1;
415 }
416 struct rist_peer **new_lst = realloc(f->peer_lst, (f->peer_lst_len + 1) * sizeof(*f->peer_lst));
417 if (!new_lst) {
418 pthread_mutex_unlock(&f->mutex);
419 rist_log_priv(&ctx->common, RIST_LOG_ERROR,
420 "OOM growing flow peer list, dropping new peer\n");
421 p->flow = NULL;
422 return -1;
423 }
424 f->peer_lst = new_lst;
425 f->peer_lst[f->peer_lst_len] = p;
426 f->peer_lst_len++;
427 pthread_mutex_unlock(&f->mutex);
428 }
429
430 rist_log_priv(&ctx->common, RIST_LOG_INFO,
431 "Peer with id #%u associated with flow #%" PRIu32 "\n", p->adv_peer_id, flow_id);
432
433 rist_log_priv(&ctx->common, RIST_LOG_INFO,
434 "Flow #%" PRIu32 " has now %zu peers.\n", flow_id, f->peer_lst_len);
435
436 return ret;
437 }
438
439 size_t rist_best_rtt_index(struct rist_flow *f)
440 {
441 size_t index = 0;
442 uint64_t rtt = UINT64_MAX;
443 for (size_t i = 0; i < f->peer_lst_len; i++) {
444 if (!f->peer_lst[i]->is_rtcp)
445 continue;
446 if (rtt > f->peer_lst[i]->eight_times_rtt) {
447 index = i;
448 rtt = f->peer_lst[i]->eight_times_rtt;
449 }
450 }
451
452 return index;
453 }