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 f->dataout_fifo_queue = calloc(ctx->fifo_queue_size, sizeof(*f->dataout_fifo_queue));
234 if (!f->dataout_fifo_queue) {
235 rist_log_priv(&ctx->common, RIST_LOG_ERROR,
236 "OOM allocating dataout fifo queue (%zu entries)\n", ctx->fifo_queue_size);
237 free(f->receiver_queue);
238 free(f);
239 return NULL;
240 }
241 int ret = pthread_cond_init(&f->condition, NULL);
242 if (ret) {
243 free(f->dataout_fifo_queue);
244 free(f->receiver_queue);
245 free(f);
246 rist_log_priv(&ctx->common, RIST_LOG_ERROR, "Error %d calling pthread_cond_init\n", ret);
247 return NULL;
248 }
249
250 ret = pthread_mutex_init(&f->mutex, NULL);
251 if (ret){
252 pthread_cond_destroy(&f->condition);
253 free(f->dataout_fifo_queue);
254 free(f->receiver_queue);
255 free(f);
256 rist_log_priv(&ctx->common, RIST_LOG_ERROR, "Error %d calling pthread_mutex_init\n", ret);
257 return NULL;
258 }
259
260 atomic_init(&f->receiver_queue_size, 0);
261 atomic_init(&f->receiver_queue_output_idx, 0);
262 atomic_init(&f->dataout_fifo_queue_write_index, 0);
263 atomic_init(&f->dataout_fifo_queue_read_index, 0);
264 atomic_init(&f->fifo_overflow, false);
265
266 f->session_timeout = RIST_DEFAULT_SESSION_TIMEOUT * RIST_CLOCK;
267 f->flow_timeout = 250 * RIST_CLOCK;
268
269 /* Count + cap-check + append under one critical section so concurrent
270 * callers cannot each observe a sub-cap count and each insert. */
271 pthread_mutex_lock(&ctx->common.flows_lock);
272 size_t flow_count = 0;
273 for (struct rist_flow *cur = ctx->common.FLOWS; cur != NULL; cur = cur->next)
274 flow_count++;
275 if (flow_count >= RIST_MAX_FLOWS) {
276 pthread_mutex_unlock(&ctx->common.flows_lock);
277 rist_log_priv(&ctx->common, RIST_LOG_ERROR,
278 "Refusing to create FLOW #%"PRIu32" inside create_flow: "
279 "cap of %d flows reached (race with concurrent caller)\n",
280 flow_id, RIST_MAX_FLOWS);
281 pthread_mutex_destroy(&f->mutex);
282 pthread_cond_destroy(&f->condition);
283 free(f->dataout_fifo_queue);
284 free(f->receiver_queue);
285 free(f);
286 return NULL;
287 }
288 rist_flow_append(&ctx->common.FLOWS, f);
289 pthread_mutex_unlock(&ctx->common.flows_lock);
290 f->logging_settings = ctx->common.logging_settings;
291
292 return f;
293 }
294
295 static bool flow_has_peer(struct rist_flow *f, uint32_t flow_id, uint32_t peer_id)
296 {
297 for (size_t j = 0; j < f->peer_lst_len; j++) {
298 struct rist_peer *p = f->peer_lst[j];
299 if (p->adv_flow_id == flow_id && p->adv_peer_id == peer_id) {
300 return true;
301 }
302 }
303
304 return false;
305 }
306
307 int rist_receiver_associate_flow(struct rist_peer *p, uint32_t flow_id)
308 {
309 struct rist_receiver *ctx = p->receiver_ctx;
310 int ret = 0;
311
312 // Find the flow based on the flow_id
313 struct rist_flow *f = NULL;
314 if (ctx->common.profile > RIST_PROFILE_SIMPLE)
315 {
316 pthread_mutex_lock(&ctx->common.flows_lock);
317 size_t flow_count = 0;
318 for (struct rist_flow *cur = ctx->common.FLOWS; cur != NULL; cur = cur->next) {
319 flow_count++;
320 if (cur->flow_id == flow_id) {
321 f = cur;
322 }
323 }
324 pthread_mutex_unlock(&ctx->common.flows_lock);
325 if (!f && flow_count >= RIST_MAX_FLOWS) {
326 rist_log_priv(&ctx->common, RIST_LOG_ERROR,
327 "Refusing to create FLOW #%"PRIu32": cap of %d flows reached\n",
328 flow_id, RIST_MAX_FLOWS);
329 return -1;
330 }
331 } else
332 {
333 if (!p->parent) {
334 rist_log_priv(&ctx->common, RIST_LOG_ERROR, "FLOW #%"PRIu32" cannot be created yet because this peer has no parent\n", flow_id);
335 return -1;
336 }
337 f = p->parent->flow;
338 }
339
340 /* create flow if necessary */
341 if (!f) {
342 f = create_flow(ctx, flow_id);
343 ret = 1;
344 if (!f) {
345 return -1;
346 }
347 if (p->config.timing_mode == RIST_TIMING_MODE_RTC)
348 f->rtc_timing_mode = true;
349
350 /* short_seq + receiver_queue_max are set (and the ring allocated)
351 * inside create_flow() now. */
352 f->recovery_buffer_ticks = p->recovery_buffer_ticks;
353 rist_log_priv(&ctx->common, RIST_LOG_INFO, "FLOW #%"PRIu32" created (short=%d)\n", flow_id, f->short_seq);
354 } else {
355 /* double check that this peer is not a member of this flow already */
356 if (flow_has_peer(f, flow_id, p->adv_peer_id)) {
357 rist_log_priv(&ctx->common, RIST_LOG_INFO, "FLOW #%"PRIu32", Existing peer (id=%"PRIu32") re-joining existing flow ...\n",
358 flow_id, p->adv_peer_id);
359 ret = 2;
360 } else {
361 rist_log_priv(&ctx->common, RIST_LOG_INFO, "FLOW #%"PRIu32": New peer (id=%u) joining existing flow ...\n",
362 flow_id, p->adv_peer_id);
363 ret = 1;
364 }
365 }
366
367 if (p->config.recovery_length_min != p->config.recovery_length_max) {
368 f->flow_auto_buffer_scaling = true;
369 }
370
371 if (p->session_timeout > f->session_timeout) {
372 f->session_timeout = p->session_timeout;
373 rist_log_priv(&ctx->common, RIST_LOG_INFO, "Setting flow session timeout to %"PRIu64"ms\n", f->session_timeout / RIST_CLOCK);
374 }
375
376 // Set the flow timeout as the buffer size for the flow
377 // However, we start with 250 ms as the minimum/default
378 // to make sure it is larger than the RTCP interval
379 if (f->recovery_buffer_ticks > f->flow_timeout)
380 f->flow_timeout = f->recovery_buffer_ticks;
381 uint64_t stats_report_time = get_cctx(p)->stats_report_time;
382 if (stats_report_time != 0 && stats_report_time != f->stats_report_time)
383 f->stats_report_time = stats_report_time;
384
385 // Set/update max missing counter
386 if (f->missing_counter_max < p->missing_counter_max)
387 f->missing_counter_max = p->missing_counter_max;
388
389 /* now assign flow to peer and add to list */
390 p->flow = f;
391 p->adv_flow_id = flow_id;
392 if (ret == 1) {
393 /* f->mutex protects peer_lst against all concurrent walkers/writers. */
394 pthread_mutex_lock(&f->mutex);
395 if (f->peer_lst_len >= RIST_MAX_PEERS_PER_FLOW) {
396 pthread_mutex_unlock(&f->mutex);
397 rist_log_priv(&ctx->common, RIST_LOG_ERROR,
398 "Refusing to attach peer (id=%u) to flow #%"PRIu32": "
399 "cap of %d peers/flow reached\n",
400 p->adv_peer_id, flow_id, RIST_MAX_PEERS_PER_FLOW);
401 p->flow = NULL;
402 return -1;
403 }
404 struct rist_peer **new_lst = realloc(f->peer_lst, (f->peer_lst_len + 1) * sizeof(*f->peer_lst));
405 if (!new_lst) {
406 pthread_mutex_unlock(&f->mutex);
407 rist_log_priv(&ctx->common, RIST_LOG_ERROR,
408 "OOM growing flow peer list, dropping new peer\n");
409 p->flow = NULL;
410 return -1;
411 }
412 f->peer_lst = new_lst;
413 f->peer_lst[f->peer_lst_len] = p;
414 f->peer_lst_len++;
415 pthread_mutex_unlock(&f->mutex);
416 }
417
418 rist_log_priv(&ctx->common, RIST_LOG_INFO,
419 "Peer with id #%u associated with flow #%" PRIu32 "\n", p->adv_peer_id, flow_id);
420
421 rist_log_priv(&ctx->common, RIST_LOG_INFO,
422 "Flow #%" PRIu32 " has now %zu peers.\n", flow_id, f->peer_lst_len);
423
424 return ret;
425 }
426
427 size_t rist_best_rtt_index(struct rist_flow *f)
428 {
429 size_t index = 0;
430 uint64_t rtt = UINT64_MAX;
431 for (size_t i = 0; i < f->peer_lst_len; i++) {
432 if (!f->peer_lst[i]->is_rtcp)
433 continue;
434 if (rtt > f->peer_lst[i]->eight_times_rtt) {
435 index = i;
436 rtt = f->peer_lst[i]->eight_times_rtt;
437 }
438 }
439
440 return index;
441 }