Debian Codegraph @

tests/osmux/osmux_test.c

raw symbols SCIP
1 /*
2 * (C) 2013 by Pablo Neira Ayuso <[email protected]>
3 * (C) 2013 by On Waves ehf <http://www.on-waves.com>
4 * (C) 2022 by sysmocom - s.f.m.c. GmbH <[email protected]>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 */
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <stdint.h>
16 #include <string.h>
17 #include <signal.h>
18 #include <arpa/inet.h>
19
20 #include <osmocom/core/select.h>
21 #include <osmocom/core/application.h>
22 #include <osmocom/core/logging.h>
23 #include <osmocom/core/msgb.h>
24 #include <osmocom/core/linuxlist.h>
25 #include <osmocom/core/timer.h>
26 #include <osmocom/netif/osmux.h>
27 #include <osmocom/netif/rtp.h>
28
29 #define DOSMUX_TEST 0
30
31 struct log_info_cat osmux_test_cat[] = {
32 [DOSMUX_TEST] = {
33 .name = "DOSMUX_TEST",
34 .description = "osmux test",
35 .color = "\033[1;35m",
36 .enabled = 1, .loglevel = LOGL_DEBUG,
37 },
38 };
39
40 const struct log_info osmux_test_log_info = {
41 .filter_fn = NULL,
42 .cat = osmux_test_cat,
43 .num_cat = ARRAY_SIZE(osmux_test_cat),
44 };
45
46 /* RTP packet with AMR payload */
47 static uint8_t rtp_pkt[] = {
48 0x80, 0x62, 0x3f, 0xcc, 0x00, 0x01, 0xa7, 0x6f, /* RTP */
49 0x07, 0x09, 0x00, 0x62, 0x20, 0x14, 0xff, 0xd4, /* AMR */
50 0xf9, 0xff, 0xfb, 0xe7, 0xeb, 0xf9, 0x9f, 0xf8,
51 0xf2, 0x26, 0x33, 0x65, 0x54,
52 };
53
54 #define PKT_TIME_USEC 20*1000
55
56 static int rtp_pkts;
57 static int mark_pkts;
58
59 #define clock_debug(fmt, args...) \
60 do { \
61 struct timespec ts; \
62 struct timeval tv; \
63 osmo_clock_gettime(CLOCK_MONOTONIC, &ts); \
64 osmo_gettimeofday(&tv, NULL); \
65 fprintf(stderr, "sys={%lu.%06lu}, mono={%lu.%06lu}: " fmt, \
66 (unsigned int long) tv.tv_sec, (unsigned int long) tv.tv_usec, \
67 (unsigned int long) ts.tv_sec, ts.tv_nsec/1000, ##args); \
68 } while(0)
69
70 static void clock_override_enable(bool enable)
71 {
72 osmo_gettimeofday_override = enable;
73 osmo_clock_override_enable(CLOCK_MONOTONIC, enable);
74 }
75
76 static void clock_override_add_debug(long sec, long usec, bool dbg)
77 {
78 osmo_gettimeofday_override_add(sec, usec);
79 osmo_clock_override_add(CLOCK_MONOTONIC, sec, usec*1000);
80 if (dbg)
81 clock_debug("clock_override_add\n");
82 }
83 #define clock_override_add(sec, usec) clock_override_add_debug(sec, usec, true)
84
85 static void tx_cb(struct msgb *msg, void *data)
86 {
87 struct rtp_hdr *rtph = (struct rtp_hdr *)msg->data;
88 char buf[4096];
89
90 osmo_rtp_snprintf(buf, sizeof(buf), msg);
91 clock_debug("extracted packet: %s\n", buf);
92
93 if (memcmp(msg->data + sizeof(struct rtp_hdr),
94 rtp_pkt + sizeof(struct rtp_hdr),
95 sizeof(rtp_pkt) - sizeof(struct rtp_hdr)) != 0) {
96 clock_debug("payload mismatch!\n");
97 exit(EXIT_FAILURE);
98 }
99
100 if (rtph->marker)
101 mark_pkts--;
102 rtp_pkts--;
103 msgb_free(msg);
104 }
105
106 static struct osmux_out_handle *h_output[4];
107
108 static void osmux_deliver(struct msgb *batch_msg, void *data)
109 {
110 struct osmux_hdr *osmuxh;
111 char buf[2048];
112
113 osmux_snprintf(buf, sizeof(buf), batch_msg);
114 clock_debug("OSMUX message (len=%d): %s\n", batch_msg->len, buf);
115
116 /* For each OSMUX message, extract the RTP messages and put them
117 * in a list. Then, reconstruct transmission timing.
118 */
119 while((osmuxh = osmux_xfrm_output_pull(batch_msg)) != NULL)
120 osmux_xfrm_output_sched(h_output[osmuxh->circuit_id], osmuxh);
121 msgb_free(batch_msg);
122 }
123
124 struct osmux_in_handle *h_input;
125
126 static void sigalarm_handler(int foo)
127 {
128 clock_debug("FAIL: test did not run successfully\n");
129 exit(EXIT_FAILURE);
130 }
131
132 static void osmux_test_marker(int num_ccid)
133 {
134 struct msgb *msg;
135 struct rtp_hdr *rtph = (struct rtp_hdr *)rtp_pkt;
136 struct rtp_hdr *cpy_rtph;
137 uint16_t seq;
138 int i, j;
139
140 for (i = 0; i < 64; i++) {
141
142 seq = ntohs(rtph->sequence);
143 seq++;
144 rtph->sequence = htons(seq);
145
146 for (j = 0; j < num_ccid; j++) {
147 msg = msgb_alloc(1500, "test");
148 if (!msg)
149 exit(EXIT_FAILURE);
150
151 memcpy(msg->data, rtp_pkt, sizeof(rtp_pkt));
152 cpy_rtph = (struct rtp_hdr *) msgb_put(msg, sizeof(rtp_pkt));
153
154 /* first condition guarantees that 1st packet per stream contains M bit set. */
155 if (i == 0 || (i+j) % 7 == 0) {
156 cpy_rtph->marker = 1;
157 mark_pkts++;
158 }
159
160 rtp_pkts++;
161 while (osmux_xfrm_input(h_input, msg, j) > 0) {
162 osmux_xfrm_input_deliver(h_input);
163 }
164 }
165 clock_override_add(0, PKT_TIME_USEC);
166 }
167
168 while (rtp_pkts) {
169 clock_override_add(1, 0);
170 osmo_select_main(0);
171 }
172
173 if (mark_pkts) {
174 clock_debug("osmux_test_marker: RTP M bit (marker) mismatch! %d\n", mark_pkts);
175 exit(EXIT_FAILURE);
176 }
177 }
178
179 static void osmux_test_loop(int ccid)
180 {
181 struct rtp_hdr *rtph = (struct rtp_hdr *)rtp_pkt;
182 struct rtp_hdr *cpy_rtph;
183 struct msgb *msg;
184 int i, j, k = 0;
185 char buf[1024];
186 uint16_t seq;
187
188 for (i = 1; i < 65; i++) {
189 msg = msgb_alloc(1500, "test");
190 if (!msg)
191 exit(EXIT_FAILURE);
192
193 memcpy(msg->data, rtp_pkt, sizeof(rtp_pkt));
194 cpy_rtph = (struct rtp_hdr *) msgb_put(msg, sizeof(rtp_pkt));
195
196 seq = ntohs(rtph->sequence);
197 seq++;
198 rtph->sequence = htons(seq);
199 if (i < 3) {
200 /* Mark 1 rtp packet of each stream */
201 cpy_rtph->marker = 1;
202 mark_pkts++;
203 }
204
205 osmo_rtp_snprintf(buf, sizeof(buf), msg);
206 clock_debug("adding to ccid=%u %s\n", (i % 2) + ccid, buf);
207 rtp_pkts++;
208
209 k++;
210 /* Fan out RTP packets between two circuit IDs to test
211 * multi-batch support. Mind that this approach implicitly add
212 * gaps between two messages to test the osmux replaying
213 * feature.
214 */
215 osmux_xfrm_input(h_input, msg, (i % 2) + ccid);
216
217 if (i % 4 == 0) {
218 /* After four RTP messages, squash them into the OSMUX
219 * batch and call the routine to deliver it.
220 */
221 osmux_xfrm_input_deliver(h_input);
222
223 /* The first two RTP message (one per circuit ID batch)
224 * are delivered immediately, wait until the three RTP
225 * messages that are extracted from OSMUX has been
226 * delivered.
227 */
228 for (j = 0; j < k-2; j++) {
229 osmo_select_main(0);
230 clock_override_add(0, PKT_TIME_USEC);
231 }
232
233 k = 0;
234 }
235 }
236
237 if (mark_pkts) {
238 clock_debug("osmux_test_loop: RTP M bit (marker) mismatch! %d\n", mark_pkts);
239 exit(EXIT_FAILURE);
240 }
241 }
242
243 int main(void)
244 {
245 int i;
246
247 if (signal(SIGALRM, sigalarm_handler) == SIG_ERR) {
248 perror("signal");
249 exit(EXIT_FAILURE);
250 }
251
252 /* This test uses fake time to speedup the run, unless we want to manually
253 * test time specific stuff */
254 clock_override_enable(true);
255
256 /* This test doesn't use it, but osmux requires it internally. */
257 void *tall_ctx = talloc_named_const(NULL, 1, "Root context");
258 msgb_talloc_ctx_init(tall_ctx, 0);
259 osmo_init_logging2(tall_ctx, &osmux_test_log_info);
260 log_set_log_level(osmo_stderr_target, LOGL_DEBUG);
261 log_set_print_filename2(osmo_stderr_target, LOG_FILENAME_NONE);
262 log_set_print_category(osmo_stderr_target, 1);
263 log_set_print_category_hex(osmo_stderr_target, 0);
264 log_set_use_color(osmo_stderr_target, 0);
265
266 for (i = 0; i < ARRAY_SIZE(h_output); i++) {
267 h_output[i] = osmux_xfrm_output_alloc(NULL);
268 osmux_xfrm_output_set_rtp_ssrc(h_output[i], 0x7000000 + i);
269 osmux_xfrm_output_set_rtp_pl_type(h_output[i], 98);
270 osmux_xfrm_output_set_tx_cb(h_output[i], tx_cb, NULL);
271 /* These fields are set using random() */
272 h_output[i]->rtp_seq = 9158;
273 h_output[i]->rtp_timestamp = 1681692777;
274 }
275
276 /* If the test takes longer than 10 seconds, abort it */
277 alarm(10);
278
279 /* Check if marker bit features work correctly */
280 h_input = osmux_xfrm_input_alloc(tall_ctx);
281 osmux_xfrm_input_set_name(h_input, "first");
282 osmux_xfrm_input_set_initial_seqnum(h_input, 0);
283 osmux_xfrm_input_set_batch_factor(h_input, 4);
284 osmux_xfrm_input_set_deliver_cb(h_input, osmux_deliver, NULL);
285
286 for (i = 0; i < 4; i++)
287 osmux_xfrm_input_open_circuit(h_input, i, 0);
288 osmux_test_marker(4);
289 for (i = 0; i < 4; i++)
290 osmux_xfrm_input_close_circuit(h_input, i);
291 TALLOC_FREE(h_input);
292
293 h_input = osmux_xfrm_input_alloc(tall_ctx);
294 osmux_xfrm_input_set_name(h_input, "second");
295 osmux_xfrm_input_set_initial_seqnum(h_input, 0);
296 osmux_xfrm_input_set_batch_factor(h_input, 4);
297 osmux_xfrm_input_set_deliver_cb(h_input, osmux_deliver, NULL);
298
299 for (i = 0; i < 2; i++)
300 osmux_xfrm_input_open_circuit(h_input, i, 0);
301
302 /* Add two circuits with dummy padding */
303 osmux_xfrm_input_open_circuit(h_input, 2, 1);
304 osmux_xfrm_input_open_circuit(h_input, 3, 1);
305
306 /* Wait 10 times to make sure dummy padding timer works fine */
307 for (i = 0; i < 10; i++)
308 osmo_select_main(0);
309
310 /* Start pushing voice data to circuits 0 and 1 */
311 osmux_test_loop(0);
312 /* ... now start pushing voice data to circuits 2 and 3. This circuits
313 * comes with dummy padding enabled.
314 */
315 osmux_test_loop(2);
316
317 for (i = 0; i < 4; i++)
318 osmux_xfrm_input_close_circuit(h_input, i);
319
320 /* Reopen with two circuits and retest */
321 osmux_xfrm_input_open_circuit(h_input, 0, 0);
322 osmux_xfrm_input_open_circuit(h_input, 1, 1);
323 osmux_test_loop(0);
324 osmux_xfrm_input_close_circuit(h_input, 0);
325 osmux_xfrm_input_close_circuit(h_input, 1);
326
327 TALLOC_FREE(h_input);
328
329 for (i = 0; i < ARRAY_SIZE(h_output); i++) {
330 clock_debug("Flushing CID %u\n", i);
331 osmux_xfrm_output_flush(h_output[i]);
332 }
333
334 for (i = 0; i < ARRAY_SIZE(h_output); i++) {
335 TALLOC_FREE(h_output[i]);
336 }
337
338 clock_debug("OK: Test passed\n");
339 return EXIT_SUCCESS;
340 }