Debian Codegraph @

tests/stream/stream_test.c

raw symbols SCIP
1 /*
2 * (C) 2023 by sysmocom - s.f.m.c. GmbH.
3 * Authors: Max Suraev
4 * Alexander Rehbein
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 <inttypes.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <unistd.h>
17 #include <errno.h>
18
19 #include <osmocom/core/byteswap.h>
20 #include <osmocom/core/select.h>
21 #include <osmocom/core/talloc.h>
22 #include <osmocom/core/msgb.h>
23 #include <osmocom/core/logging.h>
24 #include <osmocom/core/application.h>
25 #include <osmocom/core/timer.h>
26 #include <osmocom/gsm/protocol/ipaccess.h>
27
28 #include <osmocom/netif/ipa.h>
29 #include <osmocom/netif/stream.h>
30
31 #define RECONNECT_TIMEOUT_SECS 9
32
33 #define DSTREAMTEST 0
34 struct log_info_cat osmo_stream_test_cat[] = {
35 [DSTREAMTEST] = {
36 .name = "DSTREAMTEST",
37 .description = "STREAM test",
38 .enabled = 1, .loglevel = LOGL_INFO,
39 },
40 };
41
42 const struct log_info osmo_stream_test_log_info = {
43 .filter_fn = NULL,
44 .cat = osmo_stream_test_cat,
45 .num_cat = ARRAY_SIZE(osmo_stream_test_cat),
46 };
47
48 static struct msgb *make_msgb(const char *m)
49 {
50 struct msgb *msg = msgb_alloc(512, "STREAM test");
51 if (!msg) {
52 printf("Unable to allocate message\n");
53 return NULL;
54 }
55
56 if (m)
57 msgb_printf(msg, "%s", m);
58
59 return msg;
60 }
61
62 #define ASTR(rec) ((rec) ? "autoreconnecting" : "non-reconnecting")
63
64 /* client defs */
65 #define LOGCLI(cli, fmt, args...) do { \
66 struct timeval tv; \
67 osmo_gettimeofday(&tv, NULL); \
68 printf("{%lu.%06lu} [%s] Client's %s(): " fmt, \
69 (unsigned int long) tv.tv_sec, (unsigned int long) tv.tv_usec, \
70 osmo_stream_cli_get_data(cli) ? "OK" : "NA", __func__, ##args); \
71 } while (0)
72
73 #define CLI_SND(cli, m) do { \
74 struct msgb *msg = make_msgb(m); \
75 LOGCLI(cli, "sent %d bytes message: %s\n", \
76 msg->len, msgb_hexdump(msg)); \
77 osmo_stream_cli_send(cli, msg); \
78 } while(0)
79
80 /* client callbacks */
81 static int connect_cb_cli(struct osmo_stream_cli *cli)
82 {
83 void *recon = osmo_stream_cli_get_data(cli);
84 LOGCLI(cli, "callback triggered <%s>\n", recon ? "reconnected" : "initial");
85 if (recon) {
86 LOGCLI(cli, "closing connection\n");
87 osmo_stream_cli_close(cli);
88 } else
89 CLI_SND(cli, "Hi! from connect callback :-P");
90
91 return 0;
92 }
93
94 static int read_cb_cli(struct osmo_stream_cli *cli)
95 {
96 int bytes;
97 void *cli_data = osmo_stream_cli_get_data(cli);
98 struct msgb *msg = make_msgb(NULL);
99 if (!msg)
100 return -ENOMEM;
101
102 LOGCLI(cli, "callback triggered\n");
103
104 bytes = osmo_stream_cli_recv(cli, msg);
105 if (bytes < 0) {
106 LOGCLI(cli, "unable to receive message\n");
107 return -EINVAL;
108 }
109
110 if (bytes)
111 LOGCLI(cli, "received %d(%d) bytes: %s\n", bytes, msg->len, msgb_hexdump(msg));
112 else {
113 /* N. B: normally receiving 0 bytes means that we should close the connection and re-establish it
114 but to test autoreconnection logic we ignore it in here to let the test run till completion */
115 LOGCLI(cli, "0-byte read, auto-reconnect will be triggered if enabled\n");
116 osmo_gettimeofday_override_add(RECONNECT_TIMEOUT_SECS, 0);
117 }
118
119 if (!cli_data) {
120 LOGCLI(cli, "initial read, contacting server\n");
121
122 osmo_stream_cli_set_data(cli, msg);
123 CLI_SND(cli, "Doh, responding to server :-D");
124 }
125 msgb_free(msg);
126
127 return 0;
128 }
129
130 /* client helpers */
131 static struct osmo_stream_cli *init_client_reconnection(struct osmo_stream_cli *cli, bool autoreconnect)
132 {
133 /* setting negative timeout ensures that we disable reconnection logic */
134 osmo_stream_cli_set_reconnect_timeout(cli, autoreconnect ? RECONNECT_TIMEOUT_SECS : -1);
135
136 if (osmo_stream_cli_open(cli) < 0) {
137 LOGCLI(cli, "unable to open client\n");
138 return NULL;
139 }
140
141 return cli;
142 }
143
144 /* Without explicit timeout set with osmo_stream_cli_set_reconnect_timeout() default value is used.
145 static struct osmo_stream_cli *init_client_reconnection_broken1(struct osmo_stream_cli *cli, bool autoreconnect)
146 {
147 if (osmo_stream_cli_open2(cli, autoreconnect) < 0) {
148 LOGCLI(cli, "unable to open client\n");
149 return NULL;
150 }
151
152 return cli;
153 }
154 That's why those those functions result in exact the same output despite inverse use of autoreconnect parameter.
155 static struct osmo_stream_cli *init_client_reconnection_broken2(struct osmo_stream_cli *cli, bool autoreconnect)
156 {
157 if (osmo_stream_cli_open2(cli, !autoreconnect) < 0) {
158 LOGCLI(cli, "unable to open client\n");
159 return NULL;
160 }
161
162 return cli;
163 }
164
165 Variant below are also equivalent to each other.
166 static struct osmo_stream_cli *init_client_reconnection_broken1(struct osmo_stream_cli *cli, bool autoreconnect)
167 {
168 osmo_stream_cli_set_reconnect_timeout(cli, (!autoreconnect) ? 2 : -1);
169 if (osmo_stream_cli_open2(cli, autoreconnect) < 0) {
170 LOGCLI(cli, "unable to open client\n");
171 return NULL;
172 }
173
174 return cli;
175 }
176
177 static struct osmo_stream_cli *init_client_reconnection_broken2(struct osmo_stream_cli *cli, bool autoreconnect)
178 {
179 osmo_stream_cli_set_reconnect_timeout(cli, (!autoreconnect) ? 2 : -1);
180 if (osmo_stream_cli_open2(cli, !autoreconnect) < 0) {
181 LOGCLI(cli, "unable to open client\n");
182 return NULL;
183 }
184
185 return cli;
186 }
187 Note: the result differs from normal init_client_reconnection()
188 */
189
190 /* Setting reconnection value explicitly as follows is equivalent to normal init_client_reconnection()
191 static struct osmo_stream_cli *init_client_reconnection_broken1(struct osmo_stream_cli *cli, bool autoreconnect)
192 {
193 osmo_stream_cli_set_reconnect_timeout(cli, autoreconnect ? 2 : -1);
194 if (osmo_stream_cli_open2(cli, autoreconnect) < 0) {
195 LOGCLI(cli, "unable to open client\n");
196 return NULL;
197 }
198
199 return cli;
200 }
201
202 static struct osmo_stream_cli *init_client_reconnection_broken2(struct osmo_stream_cli *cli, bool autoreconnect)
203 {
204 osmo_stream_cli_set_reconnect_timeout(cli, autoreconnect ? 2 : -1);
205 if (osmo_stream_cli_open2(cli, !autoreconnect) < 0) {
206 LOGCLI(cli, "unable to open client\n");
207 return NULL;
208 }
209
210 return cli;
211 }
212 */
213
214 static struct osmo_stream_cli *make_client(void *ctx, const char *host, unsigned port, bool autoreconnect)
215 {
216 struct osmo_stream_cli *cli = osmo_stream_cli_create(ctx);
217 if (!cli) {
218 printf("Unable to create client\n");
219 return NULL;
220 }
221
222 printf("Prepare %s stream client...\n", ASTR(autoreconnect));
223
224 osmo_stream_cli_set_local_port(cli, 8976);
225 osmo_stream_cli_set_name(cli, "cli_test");
226 osmo_stream_cli_set_addr(cli, host);
227 osmo_stream_cli_set_port(cli, port);
228 osmo_stream_cli_set_connect_cb(cli, connect_cb_cli);
229 osmo_stream_cli_set_read_cb(cli, read_cb_cli);
230
231 /* using
232 return init_client_reconnection_broken1(cli, autoreconnect);
233 or
234 return init_client_reconnection_broken2(cli, autoreconnect);
235 will result in exactly the same output which might or might not be the same as with
236 init_client_reconnection() - see preceeding notes */
237 return init_client_reconnection(cli, autoreconnect);
238 }
239
240 /* server defs */
241 #define LOGLNK(lnk, fmt, args...) \
242 printf("[%s] Server's %s(): " fmt, osmo_stream_srv_link_get_data(lnk) ? "OK" : "NA", __func__, ##args)
243
244 #define LOGSRV(srv, fmt, args...) do { \
245 struct timeval tv; \
246 osmo_gettimeofday(&tv, NULL); \
247 printf("{%lu.%06lu} [%s|%s] Server's %s(): " fmt, \
248 (unsigned int long) tv.tv_sec, (unsigned int long) tv.tv_usec, \
249 osmo_stream_srv_get_data(srv) ? "OK" : "NA", \
250 osmo_stream_srv_link_get_data(osmo_stream_srv_get_master(srv)) ? "OK" : "NA", \
251 __func__, ##args); \
252 } while (0)
253
254 #define SRV_SND(srv, m) do { \
255 struct msgb *msg = make_msgb(m); \
256 LOGSRV(srv, "sent %d bytes message: %s\n", \
257 msg->len, msgb_hexdump(msg)); \
258 osmo_stream_srv_send(srv, msg); \
259 } while(0)
260
261 /* server helpers */
262 static bool subsequent_read(struct osmo_stream_srv *srv)
263 {
264 if (osmo_stream_srv_get_data(srv))
265 return true;
266
267 osmo_stream_srv_set_data(srv, srv);
268
269 return false;
270 }
271
272 static void request_test_stop(struct osmo_stream_srv *srv)
273 {
274 osmo_stream_srv_link_set_data(osmo_stream_srv_get_master(srv), NULL);
275 }
276
277 static bool test_stop_requested(struct osmo_stream_srv_link *lnk)
278 {
279 if (osmo_stream_srv_link_get_data(lnk))
280 return false;
281 return true;
282 }
283
284 /* server callbacks */
285 int read_cb_srv(struct osmo_stream_srv *srv)
286 {
287 int bytes;
288 struct msgb *msg = make_msgb(NULL);
289 if (!msg)
290 return -ENOMEM;
291
292 LOGSRV(srv, "callback triggered\n");
293
294 bytes = osmo_stream_srv_recv(srv, msg);
295 if (bytes <= 0) {
296 if (bytes < 0)
297 LOGSRV(srv, "unable to receive message: %s\n", strerror(-bytes));
298 else {
299 LOGSRV(srv, "client have already closed connection\n");
300
301 /* if client have already closed the connection,
302 than it must be subsequent (after reconnect) call */
303 request_test_stop(srv);
304 }
305 msgb_free(msg);
306 osmo_stream_srv_destroy(srv);
307 return -EINVAL;
308 } else {
309 LOGSRV(srv, "received %d(%d) bytes: %s\n", bytes, msg->len, msgb_hexdump(msg));
310 SRV_SND(srv, __func__);
311 }
312
313 msgb_free(msg);
314
315 if (subsequent_read(srv)) {
316 LOGSRV(srv, "force client disconnect on subsequent call\n");
317 osmo_stream_srv_destroy(srv);
318 } else
319 LOGSRV(srv, "keep initial client connection\n");
320
321 return 0;
322 }
323
324 static int close_cb_srv(struct osmo_stream_srv *ignored)
325 {
326 return 0;
327 }
328
329 static int accept_cb_srv(struct osmo_stream_srv_link *lnk, int fd)
330 {
331 struct osmo_stream_srv *srv = osmo_stream_srv_create(osmo_stream_srv_link_get_data(lnk), lnk, fd,
332 read_cb_srv, close_cb_srv, NULL);
333 if (!srv) {
334 LOGLNK(lnk, "error while creating connection\n");
335 return -EINVAL;
336 }
337 osmo_stream_srv_set_name(srv, "srv_test");
338
339 return 0;
340 }
341
342
343 static void test_recon(void *ctx, const char *host, unsigned port, unsigned steps, struct osmo_stream_srv_link *lnk,
344 bool autoreconnect)
345 {
346 struct timeval tv;
347 struct osmo_stream_cli *cli;
348 if (osmo_stream_srv_link_open(lnk) < 0) {
349 printf("Unable to open server\n");
350 osmo_stream_srv_link_destroy(lnk);
351 return;
352 }
353 cli = make_client(ctx, host, port, autoreconnect);
354 if (!cli)
355 return;
356
357
358 printf("=======================================\n");
359 printf("Client/Server entering %s event loop...\n", ASTR(autoreconnect));
360 printf("=======================================\n");
361
362 osmo_stream_srv_link_set_data(lnk, ctx);
363
364 while(steps--) {
365 osmo_gettimeofday_override_add(0, 1); /* small increment to easily spot iterations */
366 osmo_select_main(0);
367 osmo_gettimeofday(&tv, NULL);
368 fprintf(stderr, "\n{%lu.%06lu} %s test step %u [client %s, server %s], FD reg %u\n",
369 (unsigned int long) tv.tv_sec, (unsigned int long) tv.tv_usec,
370 ASTR(autoreconnect), steps,
371 osmo_stream_cli_get_data(cli) ? "OK" : "NA",
372 osmo_stream_srv_link_get_data(lnk) ? "OK" : "NA",
373 osmo_fd_is_registered(osmo_stream_cli_get_ofd(cli)));
374
375 if (test_stop_requested(lnk)) {
376 printf("{%lu.%06lu} Server requested test termination\n",
377 (unsigned int long) tv.tv_sec, (unsigned int long) tv.tv_usec);
378 steps = 0;
379 }
380 }
381
382 osmo_stream_cli_destroy(cli);
383 osmo_stream_srv_link_close(lnk);
384 printf("{%lu.%06lu} %s test complete.\n\n",
385 (unsigned int long) tv.tv_sec, (unsigned int long) tv.tv_usec,
386 ASTR(autoreconnect));
387 }
388
389 /* Segmentation test code (using IPA) */
390 #define IPAC_MSG_PING_LEN 0x01
391 static const uint8_t ipac_msg_ping[] = {
392 0x00, IPAC_MSG_PING_LEN,
393 IPAC_PROTO_IPACCESS,
394 IPAC_MSGT_PING
395 };
396 #define IPAC_MSG_PONG_LEN 0x01
397 static const uint8_t ipac_msg_pong[] = {
398 0x00, IPAC_MSG_PONG_LEN,
399 IPAC_PROTO_IPACCESS,
400 IPAC_MSGT_PONG
401 };
402 #define IPAC_MSG_IDREQ_PAYLOAD_INITIALIZER \
403 IPAC_MSGT_ID_GET, \
404 0x01, IPAC_IDTAG_UNITNAME
405 static const uint8_t ipac_msg_idreq_payload[] = {
406 IPAC_MSG_IDREQ_PAYLOAD_INITIALIZER
407 };
408 #define IPAC_MSG_ID_REQ_LEN 0x03
409 static const uint8_t ipac_msg_idreq[] = {
410 0x00, IPAC_MSG_ID_REQ_LEN,
411 IPAC_PROTO_IPACCESS,
412 IPAC_MSG_IDREQ_PAYLOAD_INITIALIZER
413 };
414 #define ipac_msg_idreq_third (sizeof(ipac_msg_idreq)/3)
415 #define ipac_msg_idreq_last_third (sizeof(ipac_msg_idreq) - 2 * ipac_msg_idreq_third)
416 #define IPAC_MSG_ID_RESP_LEN 0x07
417 static const uint8_t ipac_msg_idresp[] = {
418 0x00, IPAC_MSG_ID_RESP_LEN,
419 IPAC_PROTO_IPACCESS,
420 IPAC_MSGT_ID_RESP,
421 0x01, IPAC_IDTAG_UNITNAME, 0xde, 0xad, 0xbe, 0xef
422 };
423
424 #define put_ipa_msg(unsigned_char_ptr, struct_msgb_ptr, byte_array) do {\
425 (unsigned_char_ptr) = msgb_put(struct_msgb_ptr, sizeof(byte_array));\
426 memcpy(unsigned_char_ptr, byte_array, sizeof(byte_array));\
427 } while (0)
428
429 /* Array indices correspond to enum values stringified on the right */
430 static const char * const IPAC_MSG_TYPES[] = {
431 [0] = "IPAC_MSGT_PING",
432 [1] = "IPAC_MSGT_PONG",
433 [2] = "UNEXPECTED VALUE",
434 [3] = "UNEXPECTED VALUE",
435 [4] = "IPAC_MSGT_ID_GET",
436 [5] = "IPAC_MSGT_ID_RESP",
437 };
438
439 #define IPAC_MSGT_OFFSET 3
440 /* Append a message to UCHAR_PTR_DST. SRC_IPAC_MSG_BUF is expected to be a
441 * buffer containing an IPA message of type IPAC_PROTO_ACCESS that is
442 * syntactically correct up to offset 3 (IPAC_MSGT_OFFSET).
443 * Uses a counter so that appended messages can be distinguished easily in the logs */
444 #define CLI_APPEND_MSG(OSMO_STREAM_CLI_PTR, UCHAR_PTR_DST, STRUCT_MSGB_PTR, SRC_IPAC_MSG_BUF) do {\
445 LOGCLI(OSMO_STREAM_CLI_PTR, "[%u-cli] Appending msg of type %s into buffer\n",\
446 ++test_segm_ipa_stream_srv_msglognum_cli, IPAC_MSG_TYPES[SRC_IPAC_MSG_BUF[IPAC_MSGT_OFFSET]]);\
447 LOGCLI(OSMO_STREAM_CLI_PTR, "\t(msg dump: %s)\n", osmo_hexdump(SRC_IPAC_MSG_BUF,\
448 sizeof(SRC_IPAC_MSG_BUF)));\
449 put_ipa_msg(UCHAR_PTR_DST, STRUCT_MSGB_PTR, SRC_IPAC_MSG_BUF);\
450 } while (0)
451
452 static unsigned test_segm_ipa_stream_srv_msglognum_cli = 0;
453 static int test_segm_ipa_stream_srv_cli_connect_cb(struct osmo_stream_cli *cli)
454 {
455 unsigned char *data;
456 struct msgb *m = msgb_alloc_headroom(128, 0, "IPA messages");
457 if (m == NULL) {
458 fprintf(stderr, "Cannot allocate message\n");
459 return -ENOMEM;
460 }
461
462 /* Send 4 and 1/3 messages */
463 /* Append 4 */
464 CLI_APPEND_MSG(cli, data, m, ipac_msg_ping);
465 CLI_APPEND_MSG(cli, data, m, ipac_msg_pong);
466 CLI_APPEND_MSG(cli, data, m, ipac_msg_ping);
467 CLI_APPEND_MSG(cli, data, m, ipac_msg_idresp);
468 /* Append 1/3 */
469 LOGCLI(cli, "[(0%u + 1/3)-cli] Appending 1st third of msg of type %s into buffer\n",
470 test_segm_ipa_stream_srv_msglognum_cli, IPAC_MSG_TYPES[ipac_msg_idreq[3]]);
471 LOGCLI(cli, "\t(dump: %s)\n", osmo_hexdump(ipac_msg_idreq, ipac_msg_idreq_third));
472 data = msgb_put(m, ipac_msg_idreq_third);
473 memcpy(data, ipac_msg_idreq, ipac_msg_idreq_third);
474
475 LOGCLI(cli, "Sending 4 + 1/3 messages as one:\n");
476 LOGCLI(cli, "\t(msg dump: %s)\n\n", osmo_hexdump(m->data, m->len));
477 osmo_stream_cli_send(cli, m);
478 return 0;
479 }
480
481 static bool test_segm_ipa_stream_srv_all_msgs_processed = false;
482
483 static void send_last_third(void *osmo_stream_cli_arg)
484 {
485 struct osmo_stream_cli *osc = osmo_stream_cli_arg;
486 unsigned char *data;
487 struct msgb *reply = msgb_alloc_headroom(128, 0, "IPA delayed reply");
488
489 LOGCLI(osc, "Delay for sending last third of message is over\n");
490 if (reply == NULL) {
491 fprintf(stderr, "Cannot allocate message\n");
492 return;
493 }
494 LOGCLI(osc, "[%u-cli] Appending: Last third of IPAC_MSGT_ID_GET\n",
495 ++test_segm_ipa_stream_srv_msglognum_cli);
496 data = msgb_put(reply, ipac_msg_idreq_last_third);
497 memcpy(data, ipac_msg_idreq + 2 * ipac_msg_idreq_third,
498 ipac_msg_idreq_last_third);
499 /* Append two entire messages */
500 CLI_APPEND_MSG(osc, data, reply, ipac_msg_pong);
501 CLI_APPEND_MSG(osc, data, reply, ipac_msg_pong);
502 LOGCLI(osc, "\tSending:"
503 "[ Last third of IPAC_MSGT_ID_GET | IPAC_MSGT_PONG | IPAC_MSGT_PONG ]\n");
504 LOGCLI(osc, "\t(msg dump: %s)\n\n", osmo_hexdump(reply->data, reply->len));
505 osmo_stream_cli_send(osc, reply);
506 }
507
508 static struct osmo_timer_list fragmented_send_tl_cli;
509
510 static int test_segm_ipa_stream_srv_cli_read_cb(struct osmo_stream_cli *osc, int res, struct msgb *msg)
511 {
512 unsigned char *data;
513 struct ipa_head *h = (struct ipa_head *) msg->l1h;
514 uint8_t ipac_msg_type = *msg->data;
515 struct msgb *reply;
516
517 if (res < 0) {
518 fprintf(stderr, "cannot receive message (res = %d)\n", res);
519 msgb_free(msg);
520 return -ENOMSG;
521 }
522
523 LOGCLI(osc, "Received message from stream (payload len = %" PRIu16 ")\n", msgb_length(msg));
524 if (5 < ipac_msg_type) {
525 fprintf(stderr, "Received unexpected IPAC message type %"PRIu8"\n", ipac_msg_type);
526 msgb_free(msg);
527 return -ENOMSG;
528 }
529 LOGCLI(osc, "\tType: %s\n", IPAC_MSG_TYPES[ipac_msg_type]);
530 if (ipac_msg_type == IPAC_MSGT_ID_GET) {
531 LOGCLI(osc, "Got IPAC_MSGT_ID_GET from server\n");
532 LOGCLI(osc, "[(%u + 2/3) -cli] Appending: Second third of IPAC_MSGT_ID_GET\n",
533 test_segm_ipa_stream_srv_msglognum_cli);
534 reply = msgb_alloc_headroom(128, 0, "IPA reply");
535 if (reply == NULL) {
536 fprintf(stderr, "Cannot allocate message\n");
537 return -ENOMEM;
538 }
539 data = msgb_put(reply, ipac_msg_idreq_third);
540 memcpy(data, ipac_msg_idreq + ipac_msg_idreq_third,
541 ipac_msg_idreq_third);
542 LOGCLI(osc, "\tSending: Second third of IPAC_MSGT_ID_GET\n");
543 LOGCLI(osc, "\t(msg dump: %s)\n", osmo_hexdump(reply->data, reply->len));
544 osmo_stream_cli_send(osc, reply);
545 osmo_timer_setup(&fragmented_send_tl_cli, send_last_third, osc);
546 osmo_timer_add(&fragmented_send_tl_cli);
547 osmo_timer_schedule(&fragmented_send_tl_cli, 0, 500000);
548 } else if (ipac_msg_type == IPAC_MSGT_ID_RESP) {
549 LOGCLI(osc, "\tresult= %s\n",
550 osmo_hexdump((const unsigned char *)h, sizeof(*h) + h->len));
551 LOGCLI(osc, "\texpected=%s\n",
552 osmo_hexdump(ipac_msg_idresp, sizeof(ipac_msg_idresp)));
553 }
554 msgb_free(msg);
555 printf("\n");
556 return 0;
557 }
558
559 struct osmo_stream_cli *test_segm_ipa_stream_srv_run_client(void *ctx)
560 {
561 struct osmo_stream_cli *osc = osmo_stream_cli_create(ctx);
562 if (osc == NULL) {
563 fprintf(stderr, "osmo_stream_cli_create_iofd()\n");
564 return NULL;
565 }
566 osmo_stream_cli_set_addr(osc, "127.0.0.11");
567 osmo_stream_cli_set_local_port(osc, 8977);
568 osmo_stream_cli_set_port(osc, 1111);
569 osmo_stream_cli_set_connect_cb(osc, test_segm_ipa_stream_srv_cli_connect_cb);
570 osmo_stream_cli_set_read_cb2(osc, test_segm_ipa_stream_srv_cli_read_cb);
571 osmo_stream_cli_set_nodelay(osc, true);
572 if (osmo_stream_cli_open(osc) < 0) {
573 fprintf(stderr, "Cannot open stream client\n");
574 return NULL;
575 }
576 osmo_stream_cli_set_segmentation_cb(osc, osmo_ipa_segmentation_cb);
577
578 return osc;
579 }
580
581 int test_segm_ipa_stream_srv_srv_read_cb(struct osmo_stream_srv *conn, int res, struct msgb *msg)
582 {
583 static unsigned msgnum_srv = 0;
584 struct ipa_head *ih = (struct ipa_head *)msg->l1h;
585 unsigned char *data;
586 struct msgb *m;
587 uint8_t msgt;
588
589 if (res <= 0) {
590 if (res < 0)
591 LOGSRV(conn, "cannot receive message: %s\n", strerror(-res));
592 else
593 LOGSRV(conn, "client closed connection\n");
594 msgb_free(msg);
595 osmo_stream_srv_destroy(conn);
596 return -EBADF;
597 }
598
599 LOGSRV(conn, "[%u-srv] Received IPA message from stream (payload len = %" PRIu16 ")\n",
600 ++msgnum_srv, msgb_length(msg));
601 LOGSRV(conn, "\tmsg buff data (including stripped headers): %s\n",
602 osmo_hexdump((unsigned char *)ih, osmo_ntohs(ih->len) + sizeof(*ih)));
603 LOGSRV(conn, "\tIPA payload: %s\n", osmo_hexdump(ih->data, osmo_ntohs(ih->len)));
604
605 msgt = *msg->l2h; /* Octet right after IPA header */
606 LOGSRV(conn, "\tType: %s\n", IPAC_MSG_TYPES[msgt]);
607 LOGSRV(conn, "\t(msg dump: %s)\n", osmo_hexdump(msg->l1h, msg->len + sizeof(struct ipa_head)));
608
609 msgb_free(msg);
610
611 if (msgt == IPAC_MSGT_ID_RESP) { /* */
612 LOGSRV(conn, "Send IPAC_MSGT_ID_GET to trigger client to send next third\n\n");
613 m = osmo_ipa_msg_alloc(128);
614 if (m == NULL) {
615 fprintf(stderr, "Cannot allocate message\n");
616 return -ENOMEM;
617 }
618 put_ipa_msg(data, m, ipac_msg_idreq_payload);
619 osmo_ipa_msg_push_headers(m, IPAC_PROTO_IPACCESS, -1);
620 osmo_stream_srv_send(conn, m);
621 } else if (msgnum_srv == 7 && msgt == IPAC_MSGT_PONG) {
622 test_segm_ipa_stream_srv_all_msgs_processed = true;
623 osmo_stream_srv_destroy(conn);
624 }
625 return 0;
626 }
627
628 static int test_segm_ipa_stream_srv_srv_accept_cb(struct osmo_stream_srv_link *srv, int fd)
629 {
630 void *ctx = osmo_stream_srv_link_get_data(srv);
631 struct osmo_stream_srv *oss =
632 osmo_stream_srv_create2(ctx, srv, fd, NULL);
633 if (oss == NULL) {
634 fprintf(stderr, "Error while creating connection\n");
635 return -1;
636 }
637 osmo_stream_srv_set_segmentation_cb(oss, osmo_ipa_segmentation_cb);
638 osmo_stream_srv_set_read_cb(oss, test_segm_ipa_stream_srv_srv_read_cb);
639 return 0;
640 }
641
642 static void test_segm_ipa_stream_srv_run(void *ctx, const char *host, unsigned port,
643 struct osmo_stream_srv_link *srv)
644 {
645 struct osmo_stream_cli *osc;
646 const char *testname = "test_segm_ipa_stream_srv";
647 osmo_stream_srv_link_set_accept_cb(srv,
648 test_segm_ipa_stream_srv_srv_accept_cb);
649 if (osmo_stream_srv_link_open(srv) < 0) {
650 printf("Unable to open server\n");
651 exit(1);
652 }
653 osc = test_segm_ipa_stream_srv_run_client(ctx);
654
655 printf("______________________________________Running test %s______________________________________\n", testname);
656 alarm(2);
657
658 while (!test_segm_ipa_stream_srv_all_msgs_processed) {
659 osmo_gettimeofday_override_add(0, 1); /* small increment to easily spot iterations */
660 osmo_select_main(1);
661 }
662 alarm(0);
663 printf("==================================Test %s complete========================================\n\n", testname);
664 if (osc)
665 osmo_stream_cli_destroy(osc);
666 osmo_stream_srv_link_close(srv);
667 }
668
669 static void sigalarm_handler(int _foo)
670 {
671 printf("FAIL: test did not run successfully\n");
672 exit(EXIT_FAILURE);
673 }
674
675 static struct osmo_timer_list fragmented_send_tl_srv;
676 static struct osmo_timer_list fragmented_send_tl_srv_destroy;
677
678 static unsigned test_segm_ipa_stream_cli_srv_msglognum = 0;
679
680 /* Like CLI_APPEND_MSG, but for server side */
681 #define SRV_APPEND_MSG(OSMO_STREAM_SRV_PTR, UCHAR_PTR_DST, STRUCT_MSGB_PTR, SRC_IPAC_MSG_BUF) do {\
682 LOGSRV(OSMO_STREAM_SRV_PTR, "[%u-srv] Appending msg of type %s into buffer\n",\
683 ++test_segm_ipa_stream_cli_srv_msglognum, IPAC_MSG_TYPES[SRC_IPAC_MSG_BUF[IPAC_MSGT_OFFSET]]);\
684 LOGSRV(OSMO_STREAM_SRV_PTR, "\t(msg dump: %s)\n", osmo_hexdump(SRC_IPAC_MSG_BUF,\
685 sizeof(SRC_IPAC_MSG_BUF)));\
686 put_ipa_msg(UCHAR_PTR_DST, STRUCT_MSGB_PTR, SRC_IPAC_MSG_BUF);\
687 } while (0)
688
689 static void destroy_conn(void *osmo_stream_srv_arg)
690 {
691 osmo_stream_srv_destroy(osmo_stream_srv_arg);
692 }
693
694 static void send_last_third_srv(void *osmo_stream_srv_arg)
695 {
696 struct osmo_stream_srv *oss = osmo_stream_srv_arg;
697 unsigned char *data;
698 struct msgb *reply = msgb_alloc_headroom(128, 0, "IPA delayed reply");
699
700 LOGSRV(oss, "Delay for sending last third of message is over\n");
701 if (reply == NULL) {
702 fprintf(stderr, "Cannot allocate message\n");
703 return;
704 }
705 LOGSRV(oss, "[%u-srv] Appending: Last third of IPAC_MSGT_ID_GET\n",
706 ++test_segm_ipa_stream_cli_srv_msglognum);
707 data = msgb_put(reply, ipac_msg_idreq_last_third);
708 memcpy(data, ipac_msg_idreq + 2 * ipac_msg_idreq_third,
709 ipac_msg_idreq_last_third);
710 /* Append two entire messages */
711 SRV_APPEND_MSG(oss, data, reply, ipac_msg_pong);
712 SRV_APPEND_MSG(oss, data, reply, ipac_msg_pong);
713 LOGSRV(oss, "\tSending:"
714 "[ Last third of IPAC_MSGT_ID_GET | IPAC_MSGT_PONG | IPAC_MSGT_PONG ]\n");
715 LOGSRV(oss, "\t(msg dump: %s)\n\n", osmo_hexdump(reply->data, reply->len));
716 osmo_stream_srv_send(oss, reply);
717 osmo_timer_setup(&fragmented_send_tl_srv_destroy, destroy_conn, oss);
718 osmo_timer_add(&fragmented_send_tl_srv_destroy);
719 /* 2 select loop iterations needed, timing only 1 will leave the client side hanging while waiting
720 * to receive the last messages */
721 osmo_timer_schedule(&fragmented_send_tl_srv_destroy, 0, 2);
722 }
723
724 int test_segm_ipa_stream_cli_srv_read_cb(struct osmo_stream_srv *conn, int res, struct msgb *msg)
725 {
726 unsigned char *data;
727 struct ipa_head *h = (struct ipa_head *) msg->l1h;
728 uint8_t ipa_msg_type;
729 struct msgb *reply;
730
731 if (res <= 0) {
732 if (res < 0)
733 LOGSRV(conn, "cannot receive message: %s\n", strerror(-res));
734 else
735 LOGSRV(conn, "client closed connection\n");
736 msgb_free(msg);
737 osmo_stream_srv_destroy(conn);
738 return -EBADF;
739 }
740
741 ipa_msg_type = ((uint8_t *)h)[sizeof(struct ipa_head)];
742
743 reply = msgb_alloc_headroom(128, 0, "IPA reply");
744 if (reply == NULL) {
745 fprintf(stderr, "Cannot allocate message\n");
746 return -ENOMEM;
747 }
748 LOGSRV(conn, "Received message from stream (total len including stripped headers = %zu)\n",
749 osmo_ntohs(h->len) + sizeof(*h));
750 if (5 < ipa_msg_type) {
751 fprintf(stderr, "Received unexpected IPAC message type %"PRIu8"\n", ipa_msg_type);
752 return -ENOMSG;
753 }
754 LOGSRV(conn, "\tType: %s\n", IPAC_MSG_TYPES[ipa_msg_type]);
755 if (ipa_msg_type == IPAC_MSGT_ID_GET) {
756 LOGSRV(conn, "Got IPAC_MSGT_ID_GET from client\n");
757 LOGSRV(conn, "[(%u + 2/3) -srv] Appending: Second third of IPAC_MSGT_ID_GET\n",
758 test_segm_ipa_stream_cli_srv_msglognum);
759 data = msgb_put(reply, ipac_msg_idreq_third);
760 memcpy(data, ipac_msg_idreq + ipac_msg_idreq_third,
761 ipac_msg_idreq_third);
762 LOGSRV(conn, "\tSending: Second third of IPAC_MSGT_ID_GET\n");
763 LOGSRV(conn, "\t(msg dump: %s)\n", osmo_hexdump(reply->data, reply->len));
764 osmo_stream_srv_send(conn, reply);
765 osmo_timer_setup(&fragmented_send_tl_srv, send_last_third_srv, conn);
766 osmo_timer_add(&fragmented_send_tl_srv);
767 osmo_timer_schedule(&fragmented_send_tl_srv, 0, 125000);
768 } else if (ipa_msg_type == IPAC_MSGT_ID_RESP) {
769 LOGSRV(conn, "\tresult= %s\n",
770 osmo_hexdump((const unsigned char *)h, sizeof(*h) + h->len));
771 LOGSRV(conn, "\texpected=%s\n",
772 osmo_hexdump(ipac_msg_idresp, sizeof(ipac_msg_idresp)));
773 }
774 printf("\n");
775 return 0;
776 }
777
778 static int test_segm_ipa_stream_cli_srv_accept_cb(struct osmo_stream_srv_link *srv, int fd)
779 {
780 void *ctx = osmo_stream_srv_link_get_data(srv);
781 struct osmo_stream_srv *oss =
782 osmo_stream_srv_create2(ctx, srv, fd, NULL);
783 unsigned char *data;
784 struct msgb *m = msgb_alloc_headroom(128, 0, "IPA messages");
785 if (oss == NULL) {
786 fprintf(stderr, "Error while creating connection\n");
787 return -1;
788 }
789 if (m == NULL) {
790 fprintf(stderr, "Cannot allocate message\n");
791 return -ENOMEM;
792 }
793 osmo_stream_srv_set_segmentation_cb(oss, osmo_ipa_segmentation_cb);
794 osmo_stream_srv_set_read_cb(oss, test_segm_ipa_stream_cli_srv_read_cb);
795
796 /* Send 4 and 1/3 messages, as done analogously in test_segm_ipa_stream_srv_cli_connect_cb() */
797 /* Append 4 */
798 SRV_APPEND_MSG(oss, data, m, ipac_msg_ping);
799 SRV_APPEND_MSG(oss, data, m, ipac_msg_pong);
800 SRV_APPEND_MSG(oss, data, m, ipac_msg_ping);
801 SRV_APPEND_MSG(oss, data, m, ipac_msg_idresp);
802 /* Append 1/3 */
803 LOGSRV(oss, "[(0%u + 1/3)-srv] Appending 1st third of msg of type %s into buffer\n",
804 test_segm_ipa_stream_cli_srv_msglognum, IPAC_MSG_TYPES[ipac_msg_idreq[3]]);
805 LOGSRV(oss, "\t(dump: %s)\n", osmo_hexdump(ipac_msg_idreq, ipac_msg_idreq_third));
806 data = msgb_put(m, ipac_msg_idreq_third);
807 memcpy(data, ipac_msg_idreq, ipac_msg_idreq_third);
808
809 LOGSRV(oss, "Sending 4 + 1/3 messages as one:\n");
810 LOGSRV(oss, "\t(msg dump: %s)\n\n", osmo_hexdump(m->data, m->len));
811 osmo_stream_srv_send(oss, m);
812 return 0;
813 }
814
815 static bool test_segm_ipa_stream_cli_all_msgs_processed = false;
816
817 static int test_segm_ipa_stream_cli_cli_read_cb(struct osmo_stream_cli *osc, int res, struct msgb *msg)
818 {
819 static unsigned msgnum_cli = 0;
820 unsigned char *data;
821 struct msgb *m;
822 uint8_t *msgt = msg->data;
823 LOGCLI(osc, "[%u-cli] Received message from stream (len = %" PRIu16 ")\n",
824 ++msgnum_cli, msgb_length(msg));
825 LOGCLI(osc, "\tmsg buff data: %s\n", osmo_hexdump(msg->data, msg->len));
826 LOGCLI(osc, "\tIPA payload: %s\n", osmo_hexdump(msg->data, msg->len));
827 LOGCLI(osc, "\tType: %s\n", IPAC_MSG_TYPES[*msgt]);
828 LOGCLI(osc, "\t(msg dump (including stripped headers): %s)\n",
829 osmo_hexdump(msg->l1h, sizeof(struct ipa_head) + msg->len));
830 if (*msgt == IPAC_MSGT_ID_RESP) {
831 LOGCLI(osc, "Send IPAC_MSGT_ID_GET to trigger server to send next third\n\n");
832 m = msgb_alloc_headroom(128, sizeof(struct ipa_head) +
833 sizeof(struct ipa_head_ext), "IPA messages");
834 if (m == NULL) {
835 fprintf(stderr, "Cannot allocate message\n");
836 return -ENOMEM;
837 }
838 put_ipa_msg(data, m, ipac_msg_idreq_payload);
839 osmo_ipa_msg_push_headers(m, IPAC_PROTO_IPACCESS, -1);
840 osmo_stream_cli_send(osc, m);
841 } else if (msgnum_cli == 7 && *msgt == IPAC_MSGT_PONG) {
842 test_segm_ipa_stream_cli_all_msgs_processed = true;
843 }
844 return 0;
845 }
846
847 static struct osmo_stream_cli *test_segm_ipa_stream_cli_run_client(void *ctx)
848 {
849 struct osmo_stream_cli *osc = osmo_stream_cli_create(ctx);
850 if (osc == NULL) {
851 fprintf(stderr, "osmo_stream_cli_create_iofd()\n");
852 return NULL;
853 }
854 osmo_stream_cli_set_addr(osc, "127.0.0.11");
855 osmo_stream_cli_set_local_port(osc, 8977);
856 osmo_stream_cli_set_port(osc, 1112);
857 osmo_stream_cli_set_read_cb2(osc, test_segm_ipa_stream_cli_cli_read_cb);
858 osmo_stream_cli_set_nodelay(osc, true);
859 osmo_stream_cli_set_segmentation_cb(osc, osmo_ipa_segmentation_cb);
860 if (osmo_stream_cli_open(osc) < 0) {
861 fprintf(stderr, "Cannot open stream client\n");
862 return NULL;
863 }
864
865 return osc;
866 }
867
868 static void test_segm_ipa_stream_cli_run(void *ctx, const char *host, unsigned port,
869 struct osmo_stream_srv_link *srv)
870 {
871 const char *testname = "test_segm_ipa_stream_cli";
872 struct osmo_stream_cli *osc = NULL;
873 osmo_stream_srv_link_set_accept_cb(srv,
874 test_segm_ipa_stream_cli_srv_accept_cb);
875 osmo_stream_srv_link_set_port(srv, 1112);
876 if (osmo_stream_srv_link_open(srv) < 0) {
877 printf("Unable to open server\n");
878 exit(1);
879 }
880 osc = test_segm_ipa_stream_cli_run_client(ctx);
881
882 printf("______________________________________Running test %s______________________________________\n", testname);
883 alarm(2);
884
885 while (!test_segm_ipa_stream_cli_all_msgs_processed) {
886 osmo_gettimeofday_override_add(0, 1); /* small increment to easily spot iterations */
887 osmo_select_main(1);
888 }
889 alarm(0);
890 printf("==================================Test %s complete========================================\n\n", testname);
891 if (osc)
892 osmo_stream_cli_destroy(osc);
893 osmo_stream_srv_link_close(srv);
894 }
895
896 int main(void)
897 {
898
899 if (signal(SIGALRM, sigalarm_handler) == SIG_ERR) {
900 perror("signal");
901 exit(EXIT_FAILURE);
902 }
903
904 struct osmo_stream_srv_link *srv;
905 char *host = "127.0.0.11";
906 unsigned port = 1111;
907 void *tall_test = talloc_named_const(NULL, 1, "osmo_stream_test");
908
909 osmo_gettimeofday_override = true;
910 osmo_gettimeofday_override_time.tv_sec = 2;
911 osmo_gettimeofday_override_time.tv_usec = 0;
912
913 osmo_init_logging2(tall_test, &osmo_stream_test_log_info);
914 log_set_log_level(osmo_stderr_target, LOGL_DEBUG);
915 log_set_use_color(osmo_stderr_target, 0);
916 log_set_print_category_hex(osmo_stderr_target, 0);
917 log_set_print_category(osmo_stderr_target, 0);
918 log_set_print_filename2(osmo_stderr_target, LOG_FILENAME_NONE);
919
920 printf("Preparing stream server...\n");
921 srv = osmo_stream_srv_link_create(tall_test);
922 if (!srv) {
923 printf("Unable to create server\n");
924 return EXIT_FAILURE;
925 }
926
927 osmo_stream_srv_link_set_name(srv, "srv_link_test");
928 osmo_stream_srv_link_set_addr(srv, host);
929 osmo_stream_srv_link_set_port(srv, port);
930 osmo_stream_srv_link_set_accept_cb(srv, accept_cb_srv);
931 osmo_stream_srv_link_set_nodelay(srv, true);
932
933 test_recon(tall_test, host, port, 12, srv, true);
934 test_recon(tall_test, host, port, 8, srv, false);
935
936 osmo_stream_srv_link_set_data(srv, tall_test);
937 test_segm_ipa_stream_srv_run(tall_test, host, port, srv);
938 test_segm_ipa_stream_cli_run(tall_test, host, port, srv);
939
940 printf("Stream tests completed\n");
941
942 osmo_stream_srv_link_destroy(srv);
943 log_fini();
944 talloc_free(tall_test);
945 return EXIT_SUCCESS;
946 }