Debian Codegraph @

tests/osmux/osmux_output_test.c

raw symbols SCIP
1 /* (C) 2017 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 uint8_t osmux_next_seq;
34
35 #define TIME_RTP_PKT_MS 20
36 #define BATCH_FACTOR 6
37 /* ----------------------------- */
38
39 /* Logging related stuff */
40 #define INT2IDX(x) (-1*(x)-1)
41 struct log_info_cat jibuf_test_cat[] = {
42 [INT2IDX(DLMUX)] = {
43 .name = "DLMUX",
44 .description = "Osmocom Osmux",
45 .enabled = 1, .loglevel = LOGL_DEBUG,
46 },
47 };
48 const struct log_info log_info = {
49 .filter_fn = NULL,
50 .cat = jibuf_test_cat,
51 .num_cat = ARRAY_SIZE(jibuf_test_cat),
52 };
53 /* ----------------------------- */
54
55 static void osmux_init(uint8_t seq)
56 {
57 osmux_next_seq = seq;
58 }
59
60 static struct msgb *osmux_new(uint8_t cid, uint8_t seq, uint8_t batch_factor)
61 {
62 struct msgb *msg;
63 struct osmux_hdr *osmuxh;
64
65 msg = msgb_alloc(1500, "test");
66 if (!msg)
67 exit(EXIT_FAILURE);
68 msgb_put(msg, sizeof(struct osmux_hdr));
69
70 osmuxh = (struct osmux_hdr *)msg->data;
71 osmuxh->amr_q = 0;
72 osmuxh->amr_f = 0;
73 osmuxh->rtp_m = 0;
74 osmuxh->ctr = batch_factor - 1;
75 osmuxh->ft = 1;
76 osmuxh->seq = osmux_next_seq;
77 osmuxh->circuit_id = cid;
78 osmuxh->amr_ft = AMR_FT_2; /* 5.90 */
79 osmuxh->amr_cmr = 0;
80 msgb_put(msg, osmo_amr_bytes(osmuxh->amr_ft)*batch_factor);
81 return msg;
82 }
83
84 static struct msgb *osmux_next(void)
85 {
86 osmux_next_seq++;
87 return osmux_new(0, osmux_next_seq, BATCH_FACTOR);
88 }
89
90 static void sigalarm_handler(int foo)
91 {
92 printf("FAIL: test did not run successfully\n");
93 exit(EXIT_FAILURE);
94 }
95
96
97 static void clock_debug(char* str)
98 {
99 struct timespec ts;
100 struct timeval tv;
101 osmo_clock_gettime(CLOCK_MONOTONIC, &ts);
102 osmo_gettimeofday(&tv, NULL);
103 printf("sys={%lu.%06lu}, mono={%lu.%06lu}: %s\n",
104 (unsigned int long) tv.tv_sec, (unsigned int long) tv.tv_usec,
105 (unsigned int long) ts.tv_sec, ts.tv_nsec/1000, str);
106 }
107
108 static void clock_override_enable(bool enable)
109 {
110 osmo_gettimeofday_override = enable;
111 osmo_clock_override_enable(CLOCK_MONOTONIC, enable);
112 }
113
114 static void clock_override_set(long sec, long usec)
115 {
116 struct timespec *mono;
117 osmo_gettimeofday_override_time.tv_sec = sec;
118 osmo_gettimeofday_override_time.tv_usec = usec;
119 mono = osmo_clock_override_gettimespec(CLOCK_MONOTONIC);
120 mono->tv_sec = sec;
121 mono->tv_nsec = usec*1000;
122
123 clock_debug("clock_override_set");
124 }
125
126 static void clock_override_add_debug(long sec, long usec, bool dbg)
127 {
128 osmo_gettimeofday_override_add(sec, usec);
129 osmo_clock_override_add(CLOCK_MONOTONIC, sec, usec*1000);
130 if (dbg)
131 clock_debug("clock_override_add");
132 }
133 #define clock_override_add(sec, usec) clock_override_add_debug(sec, usec, true)
134
135 static void tx_cb(struct msgb *msg, void *data)
136 {
137 struct osmux_out_handle *h_output = (struct osmux_out_handle *) data;
138 struct rtp_hdr *rtph;
139 char buf[250];
140 rtph = osmo_rtp_get_hdr(msg);
141 snprintf(buf, sizeof(buf), "dequeue: seq=%"PRIu16" ts=%"PRIu32"%s enqueued=%u",
142 ntohs(rtph->sequence), ntohl(rtph->timestamp), rtph->marker ? " M" : "",
143 llist_count(&h_output->list));
144 clock_debug(buf);
145 msgb_free(msg);
146 }
147
148 #define PULL_NEXT(h_output) { \
149 struct msgb *_msg; \
150 struct osmux_hdr *_osmuxh; \
151 int _rc; \
152 _msg = osmux_next(); \
153 _osmuxh = osmux_xfrm_output_pull(_msg); \
154 OSMO_ASSERT(_osmuxh); \
155 _rc = osmux_xfrm_output_sched((h_output), _osmuxh); \
156 OSMO_ASSERT(_rc == _osmuxh->ctr+1); \
157 }
158
159 /* Test some regular scenario where frames arrive at exactly the time they should. */
160 static void test_output_consecutive(void)
161 {
162 struct osmux_out_handle *h_output;
163
164 printf("===test_output_consecutive===\n");
165
166 clock_override_enable(true);
167 clock_override_set(0, 0);
168 osmux_init(32);
169
170 h_output = osmux_xfrm_output_alloc(NULL);
171 osmux_xfrm_output_set_rtp_ssrc(h_output, 0x7000000);
172 osmux_xfrm_output_set_rtp_pl_type(h_output, 98);
173 osmux_xfrm_output_set_tx_cb(h_output, tx_cb, h_output);
174 h_output->rtp_seq = (uint16_t)50;
175 h_output->rtp_timestamp = (uint32_t)500;
176
177 /* First osmux frame at t=0 */
178 PULL_NEXT(h_output);
179 clock_debug("first dequed before first select");
180 osmo_select_main(0);
181
182 clock_override_add(0, TIME_RTP_PKT_MS*1000);
183 clock_debug("second select, second dequed");
184 osmo_select_main(0);
185
186 clock_override_add(0, TIME_RTP_PKT_MS*1000);
187 clock_debug("third select, third dequed");
188 osmo_select_main(0);
189
190 clock_override_add(0, TIME_RTP_PKT_MS*1000);
191 clock_debug("fourth select, fourth dequed");
192 osmo_select_main(0);
193
194 clock_override_add(0, TIME_RTP_PKT_MS*1000);
195 clock_debug("fifth select, fifth dequed");
196 osmo_select_main(0);
197
198 clock_override_add(0, TIME_RTP_PKT_MS*1000);
199 clock_debug("sixth select, sixth dequed");
200 osmo_select_main(0);
201 OSMO_ASSERT(llist_empty(&h_output->list));
202
203 /* Second osmux frame at t=80 */
204 clock_debug("send second osmux frame");
205 PULL_NEXT(h_output);
206 clock_debug("first dequed before first select");
207 osmo_select_main(0);
208
209 clock_override_add(0, TIME_RTP_PKT_MS*1000);
210 clock_debug("second select, second dequed");
211 osmo_select_main(0);
212
213 clock_override_add(0, 4*TIME_RTP_PKT_MS*1000);
214 clock_debug("third select, four packet should be dequeued");
215 osmo_select_main(0);
216 OSMO_ASSERT(llist_empty(&h_output->list));
217 OSMO_ASSERT(!osmo_timer_pending(&h_output->timer));
218
219 clock_debug("calling flush on empty list, should do nothing");
220 osmux_xfrm_output_flush(h_output);
221 OSMO_ASSERT(llist_empty(&h_output->list));
222 OSMO_ASSERT(!osmo_timer_pending(&h_output->timer));
223
224 talloc_free(h_output);
225 }
226
227 /* Test that receiving new osmux frame triggers flushing of RTP pakcets
228 * generated from previous one, to avoid steady growing delay in scheduling due to
229 * jitter of osmux packets received. Specifically test case where the 2 Osmux
230 * packets arrive with a small delay of system time in between them, aka the 1st
231 * Osmux frame has had some of its AMR payloads already forwarded as RTP to the
232 * upper layers. */
233 static void test_output_interleaved(void)
234 {
235 struct osmux_out_handle *h_output;
236
237 printf("===test_output_interleaved===\n");
238
239 clock_override_enable(true);
240 clock_override_set(0, 0);
241 osmux_init(32);
242
243 h_output = osmux_xfrm_output_alloc(NULL);
244 osmux_xfrm_output_set_rtp_ssrc(h_output, 0x7000000);
245 osmux_xfrm_output_set_rtp_pl_type(h_output, 98);
246 osmux_xfrm_output_set_tx_cb(h_output, tx_cb, h_output);
247 h_output->rtp_seq = (uint16_t)50;
248 h_output->rtp_timestamp = (uint32_t)500;
249
250 /* First osmux frame at t=0, but it actually arrives late due to jitter,
251 so 2nd frame is going to arrive before the 1st one is completelly
252 scheduled */
253 PULL_NEXT(h_output);
254
255 clock_override_add(0, 2*TIME_RTP_PKT_MS*1000);
256 clock_debug("select, 3 dequed, 3 still queued");
257 osmo_select_main(0);
258
259 /* Second osmux frame at t=0 */
260 clock_debug("next frame arrives, 3 pending rtp packets are dequeued and first of new osmux frame too");
261 PULL_NEXT(h_output);
262 osmo_select_main(0);
263 OSMO_ASSERT(llist_count(&h_output->list) == 5);
264
265 clock_override_add(0, 5*TIME_RTP_PKT_MS*1000);
266 clock_debug("calling select, then all should be out");
267 osmo_select_main(0);
268
269 OSMO_ASSERT(llist_empty(&h_output->list));
270 OSMO_ASSERT(!osmo_timer_pending(&h_output->timer));
271
272 talloc_free(h_output);
273 }
274
275 /* Test that receiving new osmux frame triggers flushing of RTP pakcets
276 * generated from previous one, to avoid steady growing delay in scheduling due to
277 * jitter of osmux packets received. Specifically test case where the 2 Osmux
278 * packets arrive (almost) exactly at the same time, so no internal acton is
279 * triggered between receival of those. */
280 static void test_output_2together(void)
281 {
282 struct osmux_out_handle *h_output;
283
284 printf("===test_output_2together===\n");
285
286 clock_override_enable(true);
287 clock_override_set(0, 0);
288 osmux_init(32);
289
290 h_output = osmux_xfrm_output_alloc(NULL);
291 osmux_xfrm_output_set_rtp_ssrc(h_output, 0x7000000);
292 osmux_xfrm_output_set_rtp_pl_type(h_output, 98);
293 osmux_xfrm_output_set_tx_cb(h_output, tx_cb, h_output);
294 h_output->rtp_seq = (uint16_t)50;
295 h_output->rtp_timestamp = (uint32_t)500;
296
297 /* First osmux frame at t=0, but it actually arrives late due to jitter,
298 so we receive both at the same time. */
299 PULL_NEXT(h_output);
300 clock_debug("calling select in between 2 osmux recv");
301 osmo_select_main(0);
302 PULL_NEXT(h_output);
303
304 clock_debug("calling select after receiving 2nd osmux. Dequeue 1st osmux frame and 1st rtp from 2nd osmux frame.");
305 osmo_select_main(0);
306 OSMO_ASSERT(llist_count(&h_output->list) == 5);
307
308 clock_override_add(0, 5*TIME_RTP_PKT_MS*1000);
309 clock_debug("select, all 5 remaining should be out");
310 osmo_select_main(0);
311
312 OSMO_ASSERT(llist_empty(&h_output->list));
313 OSMO_ASSERT(!osmo_timer_pending(&h_output->timer));
314
315 talloc_free(h_output);
316 }
317
318 /* Generated rtp stream gets first RTP pkt marked with M bit after osmux frame
319 * lost is detected (hence a gap in sequence) */
320 static void test_output_frame_lost(void)
321 {
322 struct osmux_out_handle *h_output;
323
324 printf("===test_output_frame_lost===\n");
325
326 clock_override_enable(true);
327 clock_override_set(0, 0);
328 osmux_init(32);
329
330 h_output = osmux_xfrm_output_alloc(NULL);
331 osmux_xfrm_output_set_rtp_ssrc(h_output, 0x7000000);
332 osmux_xfrm_output_set_rtp_pl_type(h_output, 98);
333 osmux_xfrm_output_set_tx_cb(h_output, tx_cb, h_output);
334 h_output->rtp_seq = (uint16_t)50;
335 h_output->rtp_timestamp = (uint32_t)500;
336
337 clock_debug("first osmux frame");
338 PULL_NEXT(h_output);
339 clock_override_add(0, 5*TIME_RTP_PKT_MS*1000);
340 osmo_select_main(0);
341
342 clock_debug("one osmux frame is now lost (seq++)");
343 osmux_next();
344 clock_override_add(0, 6*TIME_RTP_PKT_MS*1000);
345
346 clock_debug("3rd osmux frame arrives");
347 PULL_NEXT(h_output);
348 clock_override_add(0, 5*TIME_RTP_PKT_MS*1000);
349 osmo_select_main(0);
350
351 OSMO_ASSERT(llist_empty(&h_output->list));
352 OSMO_ASSERT(!osmo_timer_pending(&h_output->timer));
353
354 talloc_free(h_output);
355 }
356
357 /* Test all packets are transmitted immediately when osmux_xfrm_output_flush()
358 * is called. */
359 static void test_output_flush(void)
360 {
361 struct osmux_out_handle *h_output;
362
363 printf("===test_output_flush===\n");
364
365 clock_override_enable(true);
366 clock_override_set(0, 0);
367 osmux_init(32);
368
369 h_output = osmux_xfrm_output_alloc(NULL);
370 osmux_xfrm_output_set_rtp_ssrc(h_output, 0x7000000);
371 osmux_xfrm_output_set_rtp_pl_type(h_output, 98);
372 osmux_xfrm_output_set_tx_cb(h_output, tx_cb, h_output);
373 h_output->rtp_seq = (uint16_t)50;
374 h_output->rtp_timestamp = (uint32_t)500;
375
376 clock_debug("first osmux frame");
377 PULL_NEXT(h_output);
378 clock_override_add(0, 2*TIME_RTP_PKT_MS*1000);
379 osmo_select_main(0);
380
381 clock_debug("2nd osmux frame arrives");
382 PULL_NEXT(h_output);
383
384 clock_debug("flushing, all packet should be transmitted immediately");
385 OSMO_ASSERT(llist_count(&h_output->list) == 9);
386 OSMO_ASSERT(osmo_timer_pending(&h_output->timer));
387 osmux_xfrm_output_flush(h_output);
388 OSMO_ASSERT(llist_empty(&h_output->list));
389 OSMO_ASSERT(!osmo_timer_pending(&h_output->timer));
390
391 talloc_free(h_output);
392 }
393
394 /* Test how the osmux_out_handle behaves when seqnum 0xff->0x00 is received */
395 static void test_output_seqnum_wraparound(void)
396 {
397 struct osmux_out_handle *h_output;
398
399 printf("===test_output_seqnum_wraparound===\n");
400
401 clock_override_enable(true);
402 clock_override_set(0, 0);
403 osmux_init(0xff);
404
405 h_output = osmux_xfrm_output_alloc(NULL);
406 osmux_xfrm_output_set_rtp_ssrc(h_output, 0x7000000);
407 osmux_xfrm_output_set_rtp_pl_type(h_output, 98);
408 osmux_xfrm_output_set_tx_cb(h_output, tx_cb, h_output);
409 h_output->rtp_seq = (uint16_t)50;
410 h_output->rtp_timestamp = (uint32_t)500;
411 h_output->osmux_seq_ack = 0xff;
412
413 clock_debug("Sending osmux frame with seqnum=0");
414 PULL_NEXT(h_output);
415 clock_override_add(0, 2*TIME_RTP_PKT_MS*1000);
416 osmo_select_main(0);
417
418 clock_debug("flushing other RTP packets");
419 osmux_xfrm_output_flush(h_output);
420
421 talloc_free(h_output);
422 }
423
424 int main(int argc, char **argv)
425 {
426
427 if (signal(SIGALRM, sigalarm_handler) == SIG_ERR) {
428 perror("signal");
429 exit(EXIT_FAILURE);
430 }
431
432 void *tall_ctx = talloc_named_const(NULL, 1, "Root context");
433 msgb_talloc_ctx_init(tall_ctx, 0);
434 osmo_init_logging2(tall_ctx, &log_info);
435 log_set_print_filename2(osmo_stderr_target, LOG_FILENAME_NONE);
436 log_set_log_level(osmo_stderr_target, LOGL_DEBUG);
437 log_set_category_filter(osmo_stderr_target, DLMUX, 1, LOGL_DEBUG);
438
439 alarm(10);
440
441 test_output_consecutive();
442 test_output_interleaved();
443 test_output_2together();
444 test_output_frame_lost();
445 test_output_flush();
446 test_output_seqnum_wraparound();
447
448 fprintf(stdout, "OK: Test passed\n");
449 return EXIT_SUCCESS;
450 }