Debian Codegraph @

tests/osmux/osmux_input_test.c

raw symbols SCIP
1 /* (C) 2022 by sysmocom - s.f.m.c. GmbH <[email protected]>
2 *
3 * Author: Pau Espin Pedrol <[email protected]>
4 *
5 * SPDX-License-Identifier: GPL-2.0+
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <stdbool.h>
16 #include <unistd.h>
17 #include <stdint.h>
18 #include <inttypes.h>
19 #include <string.h>
20 #include <signal.h>
21 #include <arpa/inet.h>
22 #include <sys/time.h>
23 #include <getopt.h>
24
25 #include <osmocom/core/select.h>
26 #include <osmocom/core/application.h>
27 #include <osmocom/core/logging.h>
28 #include <osmocom/core/msgb.h>
29 #include <osmocom/netif/rtp.h>
30 #include <osmocom/netif/osmux.h>
31 #include <osmocom/netif/amr.h>
32
33 static uint16_t rtp_next_seq;
34 static uint16_t rtp_next_ts;
35
36 void *tall_ctx;
37
38 #define TIME_RTP_PKT_MS 20
39 #define BATCH_FACTOR 6
40 /* ----------------------------- */
41
42 /* Logging related stuff */
43 #define INT2IDX(x) (-1*(x)-1)
44 struct log_info_cat jibuf_test_cat[] = {
45 [INT2IDX(DLMUX)] = {
46 .name = "DLMUX",
47 .description = "Osmocom Osmux",
48 .enabled = 1, .loglevel = LOGL_DEBUG,
49 },
50 };
51 const struct log_info log_info = {
52 .filter_fn = NULL,
53 .cat = jibuf_test_cat,
54 .num_cat = ARRAY_SIZE(jibuf_test_cat),
55 };
56 /* ----------------------------- */
57
58 static void rtp_init(uint16_t seq, uint16_t ts)
59 {
60 rtp_next_seq = seq;
61 rtp_next_ts = ts;
62 }
63
64 static struct msgb *rtp_new(uint16_t seq, uint8_t timestamp, uint8_t marker)
65 {
66 struct msgb *msg;
67 struct rtp_hdr *rtph;
68
69 msg = msgb_alloc(1500, "rtp");
70 if (!msg)
71 exit(EXIT_FAILURE);
72 msgb_put(msg, sizeof(struct rtp_hdr));
73
74 rtph = (struct rtp_hdr *)msg->data;
75 rtph->version = RTP_VERSION;
76 rtph->marker = marker;
77 rtph->sequence = htons(seq);
78 rtph->timestamp = htons(timestamp);
79 rtph->ssrc = 0x6789;
80 return msg;
81 }
82
83 static struct msgb *rtp_next(void)
84 {
85 rtp_next_seq++;
86 rtp_next_ts += TIME_RTP_PKT_MS;
87 return rtp_new(rtp_next_seq, rtp_next_ts, 0);
88 }
89
90 static struct amr_hdr *rtp_append_amr(struct msgb *msg, uint8_t ft)
91 {
92 struct amr_hdr *amrh;
93 struct rtp_hdr *rtph = (struct rtp_hdr *)msg->data;
94
95 msgb_put(msg, sizeof(struct amr_hdr));
96 amrh = (struct amr_hdr *)rtph->data;
97
98 amrh->cmr = 0;
99 amrh->q = 1;
100 amrh->f = 0;
101 amrh->ft = ft;
102 msgb_put(msg, osmo_amr_bytes(amrh->ft));
103 return amrh;
104 }
105
106 static void sigalarm_handler(int foo)
107 {
108 printf("FAIL: test did not run successfully\n");
109 exit(EXIT_FAILURE);
110 }
111
112 #define clock_debug(fmt, args...) \
113 do { \
114 struct timespec ts; \
115 struct timeval tv; \
116 osmo_clock_gettime(CLOCK_MONOTONIC, &ts); \
117 osmo_gettimeofday(&tv, NULL); \
118 fprintf(stdout, "sys={%lu.%06lu}, mono={%lu.%06lu}: " fmt "\n", \
119 (unsigned int long) tv.tv_sec, (unsigned int long) tv.tv_usec, \
120 (unsigned int long) ts.tv_sec, ts.tv_nsec/1000, ##args); \
121 } while (0)
122
123 static void clock_override_enable(bool enable)
124 {
125 osmo_gettimeofday_override = enable;
126 osmo_clock_override_enable(CLOCK_MONOTONIC, enable);
127 }
128
129 static void clock_override_set(long sec, long usec)
130 {
131 struct timespec *mono;
132 osmo_gettimeofday_override_time.tv_sec = sec;
133 osmo_gettimeofday_override_time.tv_usec = usec;
134 mono = osmo_clock_override_gettimespec(CLOCK_MONOTONIC);
135 mono->tv_sec = sec;
136 mono->tv_nsec = usec*1000;
137
138 clock_debug("clock_override_set");
139 }
140
141 static void clock_override_add_debug(long sec, long usec, bool dbg)
142 {
143 osmo_gettimeofday_override_add(sec, usec);
144 osmo_clock_override_add(CLOCK_MONOTONIC, sec, usec*1000);
145 if (dbg)
146 clock_debug("clock_override_add");
147 }
148 #define clock_override_add(sec, usec) clock_override_add_debug(sec, usec, true)
149
150 static void test_amr_ft_change_middle_batch_osmux_deliver_cb(struct msgb *batch_msg, void *data)
151 {
152 struct osmux_hdr *osmuxh;
153 char buf[2048];
154 int n = 0;
155 bool *osmux_transmitted = (bool *)data;
156
157 osmux_snprintf(buf, sizeof(buf), batch_msg);
158 clock_debug("OSMUX message (len=%d): %s\n", batch_msg->len, buf);
159
160 /* We expect 3 batches: */
161 while ((osmuxh = osmux_xfrm_output_pull(batch_msg))) {
162 n++;
163 OSMO_ASSERT(osmuxh->ft == OSMUX_FT_VOICE_AMR);
164 OSMO_ASSERT(osmuxh->rtp_m == 0);
165 OSMO_ASSERT(osmuxh->amr_cmr == 0);
166 OSMO_ASSERT(osmuxh->amr_q == 1);
167 switch (n) {
168 case 1:
169 OSMO_ASSERT(osmuxh->seq == 0);
170 OSMO_ASSERT(osmuxh->ctr == 1);
171 OSMO_ASSERT(osmuxh->amr_ft == AMR_FT_2);
172 break;
173 case 2:
174 OSMO_ASSERT(osmuxh->seq == 1);
175 OSMO_ASSERT(osmuxh->ctr == 0);
176 OSMO_ASSERT(osmuxh->amr_ft == AMR_FT_6);
177 break;
178 case 3:
179 OSMO_ASSERT(osmuxh->seq == 2);
180 OSMO_ASSERT(osmuxh->ctr == 0);
181 OSMO_ASSERT(osmuxh->amr_ft == AMR_FT_1);
182 break;
183 }
184 }
185 OSMO_ASSERT(n == 3);
186
187 msgb_free(batch_msg);
188
189 *osmux_transmitted = true;
190 }
191 /* Test if an RTP pkt with changed AMR FT passed to osmux_input is properly
192 * processed: The current batch ends and a new batch with a new osmux header is
193 * appeneded to the generated packet. */
194 static void test_amr_ft_change_middle_batch(void)
195 {
196 struct msgb *msg;
197 int rc;
198 const uint8_t cid = 30;
199 bool osmux_transmitted = false;
200 struct osmux_in_handle *h_input;
201
202 printf("===%s===\n", __func__);
203
204
205 clock_override_enable(true);
206 clock_override_set(0, 0);
207 rtp_init(0, 0);
208
209 h_input = osmux_xfrm_input_alloc(tall_ctx);
210 osmux_xfrm_input_set_initial_seqnum(h_input, 0);
211 osmux_xfrm_input_set_batch_factor(h_input, 4);
212 osmux_xfrm_input_set_deliver_cb(h_input,
213 test_amr_ft_change_middle_batch_osmux_deliver_cb,
214 &osmux_transmitted);
215 osmux_xfrm_input_open_circuit(h_input, cid, false);
216
217 /* First RTP frame at t=0 */
218 msg = rtp_next();
219 rtp_append_amr(msg, AMR_FT_2);
220 rc = osmux_xfrm_input(h_input, msg, cid);
221 OSMO_ASSERT(rc == 0);
222
223 /* Second RTP frame at t=20 */
224 clock_override_add(0, TIME_RTP_PKT_MS*1000);
225 msg = rtp_next();
226 rtp_append_amr(msg, AMR_FT_2);
227 rc = osmux_xfrm_input(h_input, msg, cid);
228 OSMO_ASSERT(rc == 0);
229
230 /* Third RTP frame at t=40, AMR FT changes: */
231 clock_debug("Submit RTP with 1st AMR FT change");
232 clock_override_add(0, TIME_RTP_PKT_MS*1000);
233 msg = rtp_next();
234 rtp_append_amr(msg, AMR_FT_6);
235 rc = osmux_xfrm_input(h_input, msg, cid);
236 OSMO_ASSERT(rc == 0);
237
238 /* Forth RTP frame at t=60, AMR FT changes again: */
239 clock_debug("Submit RTP with 2nd AMR FT change");
240 clock_override_add(0, TIME_RTP_PKT_MS*1000);
241 msg = rtp_next();
242 rtp_append_amr(msg, AMR_FT_1);
243 rc = osmux_xfrm_input(h_input, msg, cid);
244 OSMO_ASSERT(rc == 0);
245
246 /* t=80, osmux batch is scheduled to be transmitted: */
247 clock_override_add(0, TIME_RTP_PKT_MS*1000);
248 clock_debug("Osmux frame should now be transmitted");
249 osmo_select_main(0);
250 OSMO_ASSERT(osmux_transmitted == true);
251
252 clock_debug("Closing circuit");
253 osmux_xfrm_input_close_circuit(h_input, cid);
254 talloc_free(h_input);
255 }
256
257 static void test_last_amr_cmr_f_q_used_osmux_deliver_cb(struct msgb *batch_msg, void *data)
258 {
259 struct osmux_hdr *osmuxh;
260 char buf[2048];
261 bool *osmux_transmitted = (bool *)data;
262
263 osmux_snprintf(buf, sizeof(buf), batch_msg);
264 clock_debug("OSMUX message (len=%d): %s\n", batch_msg->len, buf);
265
266 /* We expect 1 batch: */
267 osmuxh = osmux_xfrm_output_pull(batch_msg);
268 OSMO_ASSERT(osmuxh);
269 OSMO_ASSERT(osmuxh->ft == OSMUX_FT_VOICE_AMR);
270 /* Check CMR and Q values are the ones from the last message: */
271 OSMO_ASSERT(osmuxh->amr_f == 0);
272 OSMO_ASSERT(osmuxh->amr_q == 0);
273 OSMO_ASSERT(osmuxh->amr_cmr == 2);
274
275 osmuxh = osmux_xfrm_output_pull(batch_msg);
276 OSMO_ASSERT(osmuxh == NULL);
277
278 msgb_free(batch_msg);
279
280 *osmux_transmitted = true;
281 }
282 /* Test that fields CMR, F and Q of the last RTP packet in the batch are the
283 * ones set in the osmux batch header. */
284 static void test_last_amr_cmr_f_q_used(void)
285 {
286 struct msgb *msg;
287 int rc;
288 const uint8_t cid = 32;
289 bool osmux_transmitted = false;
290 struct amr_hdr *amrh;
291 struct osmux_in_handle *h_input;
292
293 printf("===%s===\n", __func__);
294
295
296
297 clock_override_enable(true);
298 clock_override_set(0, 0);
299 rtp_init(0, 0);
300
301 h_input = osmux_xfrm_input_alloc(tall_ctx);
302 osmux_xfrm_input_set_initial_seqnum(h_input, 0);
303 osmux_xfrm_input_set_batch_factor(h_input, 3);
304 osmux_xfrm_input_set_deliver_cb(h_input,
305 test_last_amr_cmr_f_q_used_osmux_deliver_cb,
306 &osmux_transmitted);
307 osmux_xfrm_input_open_circuit(h_input, cid, false);
308
309 /* First RTP frame at t=0 */
310 msg = rtp_next();
311 amrh = rtp_append_amr(msg, AMR_FT_2);
312 amrh->f = 1;
313 amrh->q = 1;
314 amrh->cmr = 0;
315 rc = osmux_xfrm_input(h_input, msg, cid);
316 OSMO_ASSERT(rc == 0);
317
318 /* Second RTP frame at t=20, CMR changes 0->1 */
319 clock_debug("Submit 2nd RTP packet, CMR changes");
320 clock_override_add(0, TIME_RTP_PKT_MS*1000);
321 msg = rtp_next();
322 amrh = rtp_append_amr(msg, AMR_FT_2);
323 amrh->f = 1;
324 amrh->q = 1;
325 amrh->cmr = 1;
326 rc = osmux_xfrm_input(h_input, msg, cid);
327 OSMO_ASSERT(rc == 0);
328
329 /* Third RTP frame at t=40, q changes 1->0, CMR changes 1->2: */
330 clock_debug("Submit 3rd RTP packet with Q and CMR changes");
331 clock_override_add(0, TIME_RTP_PKT_MS*1000);
332 msg = rtp_next();
333 amrh = rtp_append_amr(msg, AMR_FT_2);
334 amrh->f = 0;
335 amrh->q = 0;
336 amrh->cmr = 2;
337 rc = osmux_xfrm_input(h_input, msg, cid);
338 OSMO_ASSERT(rc == 0);
339
340 /* t=60, osmux batch is scheduled to be transmitted: */
341 clock_override_add(0, TIME_RTP_PKT_MS*1000);
342 clock_debug("Osmux frame should now be transmitted");
343 osmo_select_main(0);
344 OSMO_ASSERT(osmux_transmitted == true);
345
346 clock_debug("Closing circuit");
347 osmux_xfrm_input_close_circuit(h_input, cid);
348 talloc_free(h_input);
349 }
350
351 static void test_initial_osmux_seqnum_osmux_deliver_cb(struct msgb *batch_msg, void *data)
352 {
353 struct osmux_hdr *osmuxh;
354 char buf[2048];
355 bool *osmux_transmitted = (bool *)data;
356
357 osmux_snprintf(buf, sizeof(buf), batch_msg);
358 clock_debug("OSMUX message (len=%d): %s\n", batch_msg->len, buf);
359
360 /* We expect 1 batch: */
361 osmuxh = osmux_xfrm_output_pull(batch_msg);
362 OSMO_ASSERT(osmuxh);
363 /* Check seqnum is the one configured beforehand: */
364 OSMO_ASSERT(osmuxh->seq == 123);
365
366 osmuxh = osmux_xfrm_output_pull(batch_msg);
367 OSMO_ASSERT(osmuxh == NULL);
368
369 msgb_free(batch_msg);
370
371 *osmux_transmitted = true;
372 }
373 /* Test that the first transmitted osmux header is set according to what has been configured. */
374 static void test_initial_osmux_seqnum(void)
375 {
376 struct msgb *msg;
377 int rc;
378 const uint8_t cid = 33;
379 bool osmux_transmitted = false;
380 struct amr_hdr *amrh;
381 struct osmux_in_handle *h_input;
382
383 printf("===%s===\n", __func__);
384
385
386
387 clock_override_enable(true);
388 clock_override_set(0, 0);
389 rtp_init(0, 0);
390
391 h_input = osmux_xfrm_input_alloc(tall_ctx);
392 osmux_xfrm_input_set_initial_seqnum(h_input, 123);
393 osmux_xfrm_input_set_batch_factor(h_input, 1);
394 osmux_xfrm_input_set_deliver_cb(h_input,
395 test_initial_osmux_seqnum_osmux_deliver_cb,
396 &osmux_transmitted);
397 osmux_xfrm_input_open_circuit(h_input, cid, false);
398
399 /* First RTP frame at t=0 */
400 msg = rtp_next();
401 amrh = rtp_append_amr(msg, AMR_FT_2);
402 amrh->f = 1;
403 amrh->q = 1;
404 amrh->cmr = 0;
405 rc = osmux_xfrm_input(h_input, msg, cid);
406 OSMO_ASSERT(rc == 0);
407
408 /* t=20, osmux batch is scheduled to be transmitted: */
409 clock_debug("Submit 2nd RTP packet, CMR changes");
410 clock_override_add(0, TIME_RTP_PKT_MS*1000);
411 osmo_select_main(0);
412 OSMO_ASSERT(osmux_transmitted == true);
413
414 clock_debug("Closing circuit");
415 osmux_xfrm_input_close_circuit(h_input, cid);
416 talloc_free(h_input);
417 }
418
419 static void test_rtp_dup_osmux_deliver_cb(struct msgb *batch_msg, void *data)
420 {
421 struct osmux_hdr *osmuxh;
422 char buf[2048];
423 uint8_t *osmux_pl;
424 bool *osmux_transmitted = (bool *)data;
425
426 osmux_snprintf(buf, sizeof(buf), batch_msg);
427 clock_debug("OSMUX message (len=%d): %s\n", batch_msg->len, buf);
428
429 /* We expect 1 batch: */
430 osmuxh = osmux_xfrm_output_pull(batch_msg);
431 OSMO_ASSERT(osmuxh);
432 /* Check seqnum is the one configured beforehand: */
433 OSMO_ASSERT(osmuxh->seq == 123);
434 osmux_pl = (uint8_t *)osmuxh + sizeof(*osmuxh);
435 OSMO_ASSERT(osmux_pl[0] == 0x12);
436
437 osmuxh = osmux_xfrm_output_pull(batch_msg);
438 OSMO_ASSERT(osmuxh == NULL);
439
440 msgb_free(batch_msg);
441
442 *osmux_transmitted = true;
443 }
444 /* Test user pushes duplicated RTP (dup seqnum) to osmux: */
445 static void test_rtp_dup(void)
446 {
447 struct msgb *msg, *msg_dup;
448 struct amr_hdr *amrh;
449 int rc;
450 const uint8_t cid = 33;
451 bool osmux_transmitted = false;
452 struct osmux_in_handle *h_input;
453
454 printf("===%s===\n", __func__);
455
456
457
458 clock_override_enable(true);
459 clock_override_set(0, 0);
460 rtp_init(0, 0);
461
462 h_input = osmux_xfrm_input_alloc(tall_ctx);
463 osmux_xfrm_input_set_initial_seqnum(h_input, 123);
464 osmux_xfrm_input_set_batch_factor(h_input, 2);
465 osmux_xfrm_input_set_deliver_cb(h_input,
466 test_rtp_dup_osmux_deliver_cb,
467 &osmux_transmitted);
468 osmux_xfrm_input_open_circuit(h_input, cid, false);
469
470 /* First RTP frame at t=0 */
471 msg = rtp_next();
472 msg_dup = msgb_copy(msg, "dup");
473 rtp_append_amr(msg, AMR_FT_2);
474 rc = osmux_xfrm_input(h_input, msg, cid);
475 OSMO_ASSERT(rc == 0);
476
477 clock_debug("Submit 2nd RTP packet, seqnum dup");
478 clock_override_add(0, TIME_RTP_PKT_MS*1000);
479 amrh = rtp_append_amr(msg_dup, AMR_FT_2);
480 amrh->data[0] = 0x12; /* Change AMR payload to check it is updated. */
481 rc = osmux_xfrm_input(h_input, msg_dup, cid);
482 OSMO_ASSERT(rc == 0);
483 OSMO_ASSERT(osmux_transmitted == false);
484
485 /* t=60, osmux batch is scheduled to be transmitted: */
486 clock_debug("Submit 3rd RTP packet, triggers osmux batch");
487 clock_override_add(0, TIME_RTP_PKT_MS*1000);
488 msg = rtp_next();
489 rtp_append_amr(msg, AMR_FT_2);
490 osmo_select_main(0);
491 OSMO_ASSERT(osmux_transmitted == true);
492
493 clock_debug("Closing circuit");
494 osmux_xfrm_input_close_circuit(h_input, cid);
495 talloc_free(h_input);
496 }
497
498 /* Test some scenario where one RTP packet is lost */
499 static void test_rtp_pkt_gap_osmux_deliver_cb(struct msgb *batch_msg, void *data)
500 {
501 struct osmux_hdr *osmuxh;
502 char buf[2048];
503 bool *osmux_transmitted = (bool *)data;
504
505 osmux_snprintf(buf, sizeof(buf), batch_msg);
506 clock_debug("OSMUX message (len=%d): %s\n", batch_msg->len, buf);
507
508 /* We expect 1 batch: */
509 osmuxh = osmux_xfrm_output_pull(batch_msg);
510 OSMO_ASSERT(osmuxh);
511 /* Check seqnum is the one configured beforehand: */
512 OSMO_ASSERT(osmuxh->seq == 123);
513 /* Check four AMR payloads appear: */
514 OSMO_ASSERT(osmuxh->ctr == 3);
515
516 osmuxh = osmux_xfrm_output_pull(batch_msg);
517 OSMO_ASSERT(osmuxh == NULL);
518
519 msgb_free(batch_msg);
520
521 *osmux_transmitted = true;
522 }
523 static void test_rtp_pkt_gap(uint16_t rtp_start_seqnum)
524 {
525 struct msgb *msg;
526 int rc;
527 const uint8_t cid = 33;
528 bool osmux_transmitted = false;
529 struct osmux_in_handle *h_input;
530
531 printf("===%s(%" PRIu16 ")===\n", __func__, rtp_start_seqnum);
532
533 clock_override_enable(true);
534 clock_override_set(0, 0);
535 rtp_init(rtp_start_seqnum, 18000);
536
537 h_input = osmux_xfrm_input_alloc(tall_ctx);
538 osmux_xfrm_input_set_initial_seqnum(h_input, 123);
539 osmux_xfrm_input_set_batch_factor(h_input, 4);
540 osmux_xfrm_input_set_deliver_cb(h_input,
541 test_rtp_pkt_gap_osmux_deliver_cb,
542 &osmux_transmitted);
543 osmux_xfrm_input_open_circuit(h_input, cid, false);
544
545 /* First RTP frame at t=0 */
546 clock_debug("1st RTP packet is received");
547 msg = rtp_next();
548 rtp_append_amr(msg, AMR_FT_2);
549 rc = osmux_xfrm_input(h_input, msg, cid);
550 OSMO_ASSERT(rc == 0);
551
552 clock_debug("2nd RTP packet is lost");
553 clock_override_add(0, TIME_RTP_PKT_MS*1000);
554 msg = rtp_next();
555 msgb_free(msg);
556
557 clock_debug("3rd RTP packet is received");
558 clock_override_add(0, TIME_RTP_PKT_MS*1000);
559 msg = rtp_next();
560 rtp_append_amr(msg, AMR_FT_2);
561 rc = osmux_xfrm_input(h_input, msg, cid);
562 OSMO_ASSERT(rc == 0);
563
564 clock_debug("4th RTP packet is received");
565 clock_override_add(0, TIME_RTP_PKT_MS*1000);
566 msg = rtp_next();
567 rtp_append_amr(msg, AMR_FT_2);
568 rc = osmux_xfrm_input(h_input, msg, cid);
569 OSMO_ASSERT(rc == 0);
570
571 /* t=80, osmux batch is scheduled to be transmitted: */
572 clock_debug("osmux batch transmitted");
573 clock_override_add(0, TIME_RTP_PKT_MS*1000);
574 osmo_select_main(0);
575 OSMO_ASSERT(osmux_transmitted == true);
576
577 clock_debug("Closing circuit");
578 osmux_xfrm_input_close_circuit(h_input, cid);
579 talloc_free(h_input);
580 }
581
582 /* Test some scenario where some RTP packets (>batch_factor) are lost */
583 static void test_rtp_pkt_gap_bigger_than_batch_factor_osmux_deliver_cb(struct msgb *batch_msg, void *data)
584 {
585 struct osmux_hdr *osmuxh;
586 char buf[2048];
587 unsigned *osmux_transmitted = (unsigned *)data;
588 (*osmux_transmitted)++;
589
590 osmux_snprintf(buf, sizeof(buf), batch_msg);
591 clock_debug("OSMUX message %u (len=%d): %s\n", *osmux_transmitted, batch_msg->len, buf);
592
593 /* We expect 1 batch: */
594 osmuxh = osmux_xfrm_output_pull(batch_msg);
595 OSMO_ASSERT(osmuxh);
596 /* Check seqnum is the one configured beforehand: */
597 OSMO_ASSERT(osmuxh->seq == (123 + *osmux_transmitted - 1));
598 /* Check four AMR payloads appear: */
599 OSMO_ASSERT(osmuxh->ctr == 3);
600
601 /* Second osmux frame: is it expected to have the Marker bit set, since
602 * there was an incoming RTP jump between this resulting osmux frame and
603 * the previous one. */
604 if (*osmux_transmitted == 2)
605 OSMO_ASSERT(osmuxh->rtp_m == 1);
606
607 osmuxh = osmux_xfrm_output_pull(batch_msg);
608 OSMO_ASSERT(osmuxh == NULL);
609
610 msgb_free(batch_msg);
611 }
612 static void test_rtp_pkt_gap_bigger_than_batch_factor(uint16_t rtp_start_seqnum)
613 {
614 struct msgb *msg;
615 int rc;
616 const uint8_t cid = 33;
617 unsigned osmux_transmitted = 0;
618 struct osmux_in_handle *h_input;
619 int i;
620
621 printf("===%s(%" PRIu16 ")===\n", __func__, rtp_start_seqnum);
622
623 clock_override_enable(true);
624 clock_override_set(0, 0);
625 rtp_init(rtp_start_seqnum, 18000);
626
627 h_input = osmux_xfrm_input_alloc(tall_ctx);
628 osmux_xfrm_input_set_initial_seqnum(h_input, 123);
629 osmux_xfrm_input_set_batch_factor(h_input, 4);
630 osmux_xfrm_input_set_deliver_cb(h_input,
631 test_rtp_pkt_gap_bigger_than_batch_factor_osmux_deliver_cb,
632 &osmux_transmitted);
633 osmux_xfrm_input_open_circuit(h_input, cid, false);
634
635 /* First RTP frame at t=0 */
636 clock_debug("1st RTP packet is received");
637 msg = rtp_next();
638 rtp_append_amr(msg, AMR_FT_2);
639 rc = osmux_xfrm_input(h_input, msg, cid);
640 OSMO_ASSERT(rc == 0);
641
642 clock_debug("10 packets are lost");
643 for (i = 0; i < 10; i++) {
644 clock_override_add(0, TIME_RTP_PKT_MS*1000);
645 msg = rtp_next();
646 msgb_free(msg);
647 }
648
649 clock_debug("12th RTP packet is received");
650 clock_override_add(0, TIME_RTP_PKT_MS*1000);
651 msg = rtp_next();
652 rtp_append_amr(msg, AMR_FT_2);
653 /* The recreation of lost pkts fills the batch and hence triggers transmission of osmux frame: */
654 rc = osmux_xfrm_input(h_input, msg, cid);
655 OSMO_ASSERT(rc == 1);
656 /* We call deliver as requested by API: */
657 osmux_xfrm_input_deliver(h_input);
658 OSMO_ASSERT(osmux_transmitted == 1);
659 /* We try to feed the RTP packet again as per API. This time 0 is
660 * received, pkt is stored in osmux layer. This is because we don't
661 * recreate entire batches with fake data. Instead, Osmux should use the
662 * Marker bit to announce the peer there was a (potentially huge) jump
663 * in seqnums. */
664 rc = osmux_xfrm_input(h_input, msg, cid);
665 OSMO_ASSERT(rc == 0);
666
667 /* We continue filling the current batch so it gets transmitted: */
668 clock_debug("12th+1 RTP packet is received");
669 clock_override_add(0, TIME_RTP_PKT_MS*1000);
670 msg = rtp_next();
671 rtp_append_amr(msg, AMR_FT_2);
672 rc = osmux_xfrm_input(h_input, msg, cid);
673 OSMO_ASSERT(rc == 0);
674
675 clock_debug("12th+2 RTP packet is received");
676 clock_override_add(0, TIME_RTP_PKT_MS*1000);
677 msg = rtp_next();
678 rtp_append_amr(msg, AMR_FT_2);
679 rc = osmux_xfrm_input(h_input, msg, cid);
680 OSMO_ASSERT(rc == 0);
681
682 clock_debug("12th+3 RTP packet is received");
683 clock_override_add(0, TIME_RTP_PKT_MS*1000);
684 msg = rtp_next();
685 rtp_append_amr(msg, AMR_FT_2);
686 rc = osmux_xfrm_input(h_input, msg, cid);
687 OSMO_ASSERT(rc == 0);
688
689 clock_debug("osmux batch transmitted");
690 clock_override_add(0, TIME_RTP_PKT_MS*1000);
691 osmo_select_main(0);
692 OSMO_ASSERT(osmux_transmitted == 2);
693
694 clock_debug("Closing circuit");
695 osmux_xfrm_input_close_circuit(h_input, cid);
696 talloc_free(h_input);
697 }
698
699 int main(int argc, char **argv)
700 {
701
702 if (signal(SIGALRM, sigalarm_handler) == SIG_ERR) {
703 perror("signal");
704 exit(EXIT_FAILURE);
705 }
706
707 tall_ctx = talloc_named_const(NULL, 1, "Root context");
708 msgb_talloc_ctx_init(tall_ctx, 0);
709 osmo_init_logging2(tall_ctx, &log_info);
710 log_set_print_filename2(osmo_stderr_target, LOG_FILENAME_NONE);
711 log_set_log_level(osmo_stderr_target, LOGL_DEBUG);
712 log_set_category_filter(osmo_stderr_target, DLMUX, 1, LOGL_DEBUG);
713
714 alarm(10);
715
716 test_amr_ft_change_middle_batch();
717 test_last_amr_cmr_f_q_used();
718 test_initial_osmux_seqnum();
719 test_rtp_dup();
720 test_rtp_pkt_gap(60);
721 /* Test several wraparound scenarios: */
722 test_rtp_pkt_gap(65533);
723 test_rtp_pkt_gap(65534);
724 test_rtp_pkt_gap(65535);
725 test_rtp_pkt_gap_bigger_than_batch_factor(60);
726 /* Test several wraparound scenarios: */
727 test_rtp_pkt_gap_bigger_than_batch_factor(65533);
728 test_rtp_pkt_gap_bigger_than_batch_factor(65534);
729 test_rtp_pkt_gap_bigger_than_batch_factor(65535);
730
731 fprintf(stdout, "OK: Test passed\n");
732 return EXIT_SUCCESS;
733 }