Debian Codegraph @

Xpjproject/pjsip/src/test/transport_test.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20 #include "test.h"
21 #include <pjsip.h>
22 #include <pjlib.h>
23
24 #define THIS_FILE "transport_test.c"
25
26 ///////////////////////////////////////////////////////////////////////////////
27 /*
28 * Generic testing for transport, to make sure that basic
29 * attributes have been initialized properly.
30 */
31 int generic_transport_test(pjsip_transport *tp)
32 {
33 PJ_LOG(3,(THIS_FILE, " structure test..."));
34
35 /* Check that local address name is valid. */
36 {
37 pj_in_addr addr;
38
39 if (pj_inet_pton(pj_AF_INET(), &tp->local_name.host,
40 &addr) == PJ_SUCCESS)
41 {
42 PJ_TEST_TRUE(addr.s_addr!=PJ_INADDR_ANY && addr.s_addr!=PJ_INADDR_NONE,
43 "invalid address name", return -420);
44 } else {
45 /* It's okay. local_name.host may be a hostname instead of
46 * IP address.
47 */
48 }
49 }
50
51 /* Check that port is valid. */
52 PJ_TEST_GT(tp->local_name.port, 0, NULL, return -430);
53
54 /* Check length of address (for now we only check against sockaddr_in). */
55 PJ_TEST_EQ(tp->addr_len, sizeof(pj_sockaddr_in), NULL, return -440);
56
57 /* Check type. */
58 PJ_TEST_NEQ(tp->key.type, PJSIP_TRANSPORT_UNSPECIFIED, NULL, return -450);
59
60 /* That's it. */
61 return PJ_SUCCESS;
62 }
63
64 ///////////////////////////////////////////////////////////////////////////////
65 /*
66 * Send/receive test.
67 *
68 * This test sends a request to loopback address; as soon as request is
69 * received, response will be sent, and time is recorded.
70 *
71 * The main purpose is to test that the basic transport functionalities works,
72 * before we continue with more complicated tests.
73 */
74 #define SEND_RECV_FROM_HDR "Bob <sip:[email protected]>"
75 #define SEND_RECV_CALL_ID_HDR "Transport-SendRecv-Test"
76 #define RT_FROM_HDR "Bob <sip:[email protected]>"
77 #define CONTACT_HDR "Bob <sip:[email protected]>"
78 #define CSEQ_VALUE 100
79 #define BODY "Hello World!"
80
81 static pj_bool_t my_on_rx_request(pjsip_rx_data *rdata);
82 static pj_bool_t my_on_rx_response(pjsip_rx_data *rdata);
83
84 /* Flag to indicate message has been received
85 * (or failed to send)
86 */
87 #define NO_STATUS -2
88
89 struct send_recv_test_global_t
90 {
91 int send_status;
92 int recv_status;
93 pj_timestamp my_send_time;
94 pj_timestamp my_recv_time;
95 };
96 static struct send_recv_test_global_t sr_g[PJSIP_TRANSPORT_START_OTHER];
97
98 /* Module to receive messages for this test. */
99 static pjsip_module send_recv_module =
100 {
101 NULL, NULL, /* prev and next */
102 { "Transport-Test", 14}, /* Name. */
103 -1, /* Id */
104 PJSIP_MOD_PRIORITY_TSX_LAYER-1, /* Priority */
105 NULL, /* load() */
106 NULL, /* start() */
107 NULL, /* stop() */
108 NULL, /* unload() */
109 &my_on_rx_request, /* on_rx_request() */
110 &my_on_rx_response, /* on_rx_response() */
111 NULL, /* on_tsx_state() */
112 };
113
114
115 static pj_bool_t my_on_rx_request(pjsip_rx_data *rdata)
116 {
117 pjsip_to_hdr *to_hdr = rdata->msg_info.to;
118 pjsip_sip_uri *target = (pjsip_sip_uri*)pjsip_uri_get_uri(to_hdr->uri);
119 unsigned tid;
120
121 if (!is_user_equal(rdata->msg_info.from, "transport_send_recv_test"))
122 return PJ_FALSE;
123
124 tid = (unsigned)pj_strtoul(&target->user);
125 pj_assert(tid < PJSIP_TRANSPORT_START_OTHER);
126
127 /* Check that this is our request. */
128 if (pj_strcmp2(&rdata->msg_info.cid->id, SEND_RECV_CALL_ID_HDR) == 0) {
129 /* It is! */
130 /* Send response. */
131 pjsip_tx_data *tdata;
132 pjsip_response_addr res_addr;
133 pj_status_t status;
134
135 status = pjsip_endpt_create_response( endpt, rdata, 200, NULL, &tdata);
136 if (status != PJ_SUCCESS) {
137 sr_g[tid].recv_status = status;
138 return PJ_TRUE;
139 }
140 status = pjsip_get_response_addr( tdata->pool, rdata, &res_addr);
141 if (status != PJ_SUCCESS) {
142 sr_g[tid].recv_status = status;
143 pjsip_tx_data_dec_ref(tdata);
144 return PJ_TRUE;
145 }
146 status = pjsip_endpt_send_response( endpt, &res_addr, tdata, NULL, NULL);
147 if (status != PJ_SUCCESS) {
148 sr_g[tid].recv_status = status;
149 pjsip_tx_data_dec_ref(tdata);
150 return PJ_TRUE;
151 }
152 return PJ_TRUE;
153 }
154
155 /* Not ours. */
156 return PJ_FALSE;
157 }
158
159 static pj_bool_t my_on_rx_response(pjsip_rx_data *rdata)
160 {
161 pjsip_to_hdr *to_hdr = rdata->msg_info.to;
162 pjsip_sip_uri *target = (pjsip_sip_uri*)pjsip_uri_get_uri(to_hdr->uri);
163 unsigned tid;
164
165 if (!is_user_equal(rdata->msg_info.from, "transport_send_recv_test"))
166 return PJ_FALSE;
167
168 tid = (unsigned)pj_strtoul(&target->user);
169 pj_assert(tid < PJSIP_TRANSPORT_START_OTHER);
170
171 if (pj_strcmp2(&rdata->msg_info.cid->id, SEND_RECV_CALL_ID_HDR) == 0) {
172 pj_get_timestamp(&sr_g[tid].my_recv_time);
173 sr_g[tid].recv_status = PJ_SUCCESS;
174 return PJ_TRUE;
175 }
176 return PJ_FALSE;
177 }
178
179 /* Transport callback. */
180 static void send_msg_callback(pjsip_send_state *stateless_data,
181 pj_ssize_t sent, pj_bool_t *cont)
182 {
183 unsigned tid = (unsigned)(uintptr_t)stateless_data->token;
184 pj_assert(tid < PJSIP_TRANSPORT_START_OTHER);
185
186 if (sent < 1) {
187 /* Obtain the error code. */
188 PJ_LOG(3,(THIS_FILE, " Sending %s got callback error %ld",
189 stateless_data->tdata->info, -sent));
190 sr_g[tid].send_status = (int)-sent;
191 } else {
192 sr_g[tid].send_status = PJ_SUCCESS;
193 }
194
195 /* Don't want to continue. */
196 *cont = PJ_FALSE;
197 }
198
199
200 /* Test that we receive loopback message. */
201 int transport_send_recv_test( pjsip_transport_type_e tp_type,
202 pjsip_transport *ref_tp,
203 const char *host_port_transport,
204 int *p_usec_rtt)
205 {
206 unsigned tid = tp_type;
207 pj_bool_t msg_log_enabled;
208 char target_url[64];
209 pj_status_t status;
210 pj_str_t target, from, to, contact, call_id, body;
211 pjsip_method method;
212 pjsip_tx_data *tdata;
213 pj_time_val timeout;
214
215 PJ_UNUSED_ARG(tp_type);
216 PJ_UNUSED_ARG(ref_tp);
217
218 PJ_LOG(3,(THIS_FILE, " single message round-trip test..."));
219
220 /* Register out test module to receive the message (if necessary). */
221 pj_enter_critical_section();
222 if (send_recv_module.id == -1) {
223 status = pjsip_endpt_register_module( endpt, &send_recv_module );
224 if (status != PJ_SUCCESS) {
225 pj_leave_critical_section();
226 app_perror(" error: unable to register module", status);
227 return -500;
228 }
229 }
230 pj_leave_critical_section();
231
232 /* Disable message logging. */
233 msg_log_enabled = msg_logger_set_enabled(0);
234
235 pj_ansi_snprintf(target_url, sizeof(target_url), "sip:%d@%s",
236 tp_type, host_port_transport);
237
238 /* Create a request message. */
239 target = pj_str(target_url);
240 from = pj_str(SEND_RECV_FROM_HDR);
241 to = pj_str(target_url);
242 contact = pj_str(CONTACT_HDR);
243 call_id = pj_str(SEND_RECV_CALL_ID_HDR);
244 body = pj_str(BODY);
245
246 pjsip_method_set(&method, PJSIP_OPTIONS_METHOD);
247 status = pjsip_endpt_create_request( endpt, &method, &target, &from, &to,
248 &contact, &call_id, CSEQ_VALUE,
249 &body, &tdata );
250 if (status != PJ_SUCCESS) {
251 app_perror(" error: unable to create request", status);
252 return -510;
253 }
254
255 /* Reset statuses */
256 sr_g[tid].send_status = sr_g[tid].recv_status = NO_STATUS;
257
258 /* Need this to make sure we use our loop transport rather than other
259 * loop transport instances created by other test thread (which they
260 * can disappear at any time)
261 */
262 if (tp_type==PJSIP_TRANSPORT_LOOP_DGRAM) {
263 pjsip_tpselector tp_sel;
264 pj_bzero(&tp_sel, sizeof(tp_sel));
265 tp_sel.type = PJSIP_TPSELECTOR_TRANSPORT;
266 tp_sel.u.transport = ref_tp;
267
268 pjsip_tx_data_set_transport(tdata, &tp_sel);
269 }
270
271 /* Start time. */
272 pj_get_timestamp(&sr_g[tid].my_send_time);
273
274 /* Send the message (statelessly). */
275 PJ_LOG(3,(THIS_FILE, "Sending request to %.*s",
276 (int)target.slen, target.ptr));
277 status = pjsip_endpt_send_request_stateless( endpt, tdata,
278 (void*)(uintptr_t)tid,
279 &send_msg_callback);
280 if (status != PJ_SUCCESS) {
281 /* Immediate error! */
282 PJ_LOG(3,(THIS_FILE, " Sending %s to %.*s got immediate error %d",
283 tdata->info, (int)target.slen, target.ptr, status));
284 pjsip_tx_data_dec_ref(tdata);
285 sr_g[tid].send_status = status;
286 }
287
288 /* Set the timeout (2 seconds from now) */
289 pj_gettimeofday(&timeout);
290 timeout.sec += 2;
291
292 /* Loop handling events until we get status */
293 do {
294 pj_time_val now;
295 pj_time_val poll_interval = { 0, 10 };
296
297 pj_gettimeofday(&now);
298 if (PJ_TIME_VAL_GTE(now, timeout)) {
299 PJ_LOG(3,(THIS_FILE, " error: timeout in send/recv test"));
300 status = -540;
301 goto on_return;
302 }
303
304 if (sr_g[tid].send_status!=NO_STATUS && sr_g[tid].send_status!=PJ_SUCCESS) {
305 app_perror(" error sending message", sr_g[tid].send_status);
306 status = -550;
307 goto on_return;
308 }
309
310 if (sr_g[tid].recv_status!=NO_STATUS && sr_g[tid].recv_status!=PJ_SUCCESS) {
311 app_perror(" error receiving message", sr_g[tid].recv_status);
312 status = -560;
313 goto on_return;
314 }
315
316 if (sr_g[tid].send_status!=NO_STATUS && sr_g[tid].recv_status!=NO_STATUS) {
317 /* Success! */
318 break;
319 }
320
321 pjsip_endpt_handle_events(endpt, &poll_interval);
322
323 } while (1);
324
325 pjsip_endpt_stop_handle_events(endpt);
326
327 if (status == PJ_SUCCESS) {
328 unsigned usec_rt;
329 usec_rt = pj_elapsed_usec(&sr_g[tid].my_send_time, &sr_g[tid].my_recv_time);
330
331 PJ_LOG(3,(THIS_FILE, " round-trip = %d usec", usec_rt));
332
333 *p_usec_rtt = usec_rt;
334 }
335
336 /* Restore message logging. */
337 msg_logger_set_enabled(msg_log_enabled);
338
339 status = PJ_SUCCESS;
340
341 on_return:
342 return status;
343 }
344
345
346 ///////////////////////////////////////////////////////////////////////////////
347 /*
348 * Multithreaded round-trip test
349 *
350 * This test will spawn multiple threads, each of them send a request. As soon
351 * as request is received, response will be sent, and time is recorded.
352 *
353 * The main purpose of this test is to ensure there's no crash when multiple
354 * threads are sending/receiving messages.
355 *
356 */
357 static pj_bool_t rt_on_rx_request(pjsip_rx_data *rdata);
358 static pj_bool_t rt_on_rx_response(pjsip_rx_data *rdata);
359
360 static pjsip_module rt_module =
361 {
362 NULL, NULL, /* prev and next */
363 { "Transport-RT-Test", 17}, /* Name. */
364 -1, /* Id */
365 PJSIP_MOD_PRIORITY_TSX_LAYER-1, /* Priority */
366 NULL, /* load() */
367 NULL, /* start() */
368 NULL, /* stop() */
369 NULL, /* unload() */
370 &rt_on_rx_request, /* on_rx_request() */
371 &rt_on_rx_response, /* on_rx_response() */
372 NULL, /* tsx_handler() */
373 };
374
375 enum
376 {
377 STOP_SEND = 1,
378 STOP_RECV = 2,
379 };
380
381 static struct rt_global_t {
382 struct {
383 pj_thread_t *thread;
384 pj_timestamp send_time;
385 pj_timestamp total_rt_time;
386 int sent_request_count, recv_response_count;
387 pj_str_t call_id;
388 pj_timer_entry timeout_timer;
389 pj_timer_entry tx_timer;
390 pj_mutex_t *mutex;
391 } rt_test_data[16];
392
393 char rt_target_uri[64];
394 int rt_stop;
395 pj_str_t rt_call_id;
396 pjsip_transport *rt_transport;
397
398 } g_rt[PJSIP_TRANSPORT_START_OTHER];
399
400 static pj_bool_t rt_on_rx_request(pjsip_rx_data *rdata)
401 {
402 pjsip_to_hdr *to_hdr = rdata->msg_info.to;
403 pjsip_sip_uri *target = (pjsip_sip_uri*)pjsip_uri_get_uri(to_hdr->uri);
404 unsigned tid;
405
406 if (!is_user_equal(rdata->msg_info.from, "transport_rt_test"))
407 return PJ_FALSE;
408
409 tid = (unsigned)pj_strtoul(&target->user);
410 pj_assert(tid < PJSIP_TRANSPORT_START_OTHER);
411
412 if (!pj_strncmp(&rdata->msg_info.cid->id, &g_rt[tid].rt_call_id,
413 g_rt[tid].rt_call_id.slen))
414 {
415 pjsip_tx_data *tdata;
416 pjsip_response_addr res_addr;
417 pj_status_t status;
418
419 status = pjsip_endpt_create_response( endpt, rdata, 200, NULL, &tdata);
420 if (status != PJ_SUCCESS) {
421 app_perror(" error creating response", status);
422 return PJ_TRUE;
423 }
424 status = pjsip_get_response_addr( tdata->pool, rdata, &res_addr);
425 if (status != PJ_SUCCESS) {
426 app_perror(" error in get response address", status);
427 pjsip_tx_data_dec_ref(tdata);
428 return PJ_TRUE;
429 }
430 status = pjsip_endpt_send_response( endpt, &res_addr, tdata, NULL, NULL);
431 if (status != PJ_SUCCESS) {
432 app_perror(" error sending response", status);
433 pjsip_tx_data_dec_ref(tdata);
434 return PJ_TRUE;
435 }
436 return PJ_TRUE;
437
438 }
439 return PJ_FALSE;
440 }
441
442 static pj_status_t rt_send_request(unsigned tid, int thread_id)
443 {
444 pj_status_t status;
445 pj_str_t target, from, to, contact, call_id;
446 pjsip_tx_data *tdata;
447 pj_time_val timeout_delay;
448 pjsip_tpselector tp_sel;
449
450 pj_mutex_lock(g_rt[tid].rt_test_data[thread_id].mutex);
451
452 /* Create a request message. */
453 target = pj_str(g_rt[tid].rt_target_uri);
454 from = pj_str(RT_FROM_HDR);
455 to = pj_str(g_rt[tid].rt_target_uri);
456 contact = pj_str(CONTACT_HDR);
457 call_id = g_rt[tid].rt_test_data[thread_id].call_id;
458
459 status = pjsip_endpt_create_request( endpt, &pjsip_options_method,
460 &target, &from, &to,
461 &contact, &call_id, -1,
462 NULL, &tdata );
463 if (status != PJ_SUCCESS) {
464 app_perror(" error: unable to create request", status);
465 pj_mutex_unlock(g_rt[tid].rt_test_data[thread_id].mutex);
466 return -610;
467 }
468
469 if (g_rt[tid].rt_transport) {
470 pj_bzero(&tp_sel, sizeof(tp_sel));
471 tp_sel.type = PJSIP_TPSELECTOR_TRANSPORT;
472 tp_sel.u.transport = g_rt[tid].rt_transport;
473 pjsip_tx_data_set_transport(tdata, &tp_sel);
474
475 }
476 /* Start time. */
477 pj_get_timestamp(&g_rt[tid].rt_test_data[thread_id].send_time);
478
479 /* Send the message (statelessly). */
480 status = pjsip_endpt_send_request_stateless( endpt, tdata, NULL, NULL);
481 if (status != PJ_SUCCESS) {
482 /* Immediate error! */
483 app_perror(" error: send request", status);
484 pjsip_tx_data_dec_ref(tdata);
485 pj_mutex_unlock(g_rt[tid].rt_test_data[thread_id].mutex);
486 return -620;
487 }
488
489 /* Update counter. */
490 g_rt[tid].rt_test_data[thread_id].sent_request_count++;
491
492 /* Set timeout timer. */
493 if (g_rt[tid].rt_test_data[thread_id].timeout_timer.user_data != NULL) {
494 pjsip_endpt_cancel_timer(endpt, &g_rt[tid].rt_test_data[thread_id].timeout_timer);
495 }
496 timeout_delay.sec = 100; timeout_delay.msec = 0;
497 g_rt[tid].rt_test_data[thread_id].timeout_timer.user_data = (void*)(pj_ssize_t)1;
498 pjsip_endpt_schedule_timer(endpt, &g_rt[tid].rt_test_data[thread_id].timeout_timer,
499 &timeout_delay);
500
501 pj_mutex_unlock(g_rt[tid].rt_test_data[thread_id].mutex);
502 return PJ_SUCCESS;
503 }
504
505 static pj_bool_t rt_on_rx_response(pjsip_rx_data *rdata)
506 {
507 pjsip_to_hdr *to_hdr = rdata->msg_info.to;
508 pjsip_sip_uri *target = (pjsip_sip_uri*)pjsip_uri_get_uri(to_hdr->uri);
509 unsigned tid;
510
511 if (!is_user_equal(rdata->msg_info.from, "transport_rt_test"))
512 return PJ_FALSE;
513
514 tid = (unsigned)pj_strtoul(&target->user);
515 pj_assert(tid < PJSIP_TRANSPORT_START_OTHER);
516
517 if (!pj_strncmp(&rdata->msg_info.cid->id, &g_rt[tid].rt_call_id,
518 g_rt[tid].rt_call_id.slen))
519 {
520 char *pos = pj_strchr(&rdata->msg_info.cid->id, '/')+1;
521 int thread_id = (*pos - '0');
522 pj_timestamp recv_time;
523
524 pj_mutex_lock(g_rt[tid].rt_test_data[thread_id].mutex);
525
526 /* Stop timer. */
527 pjsip_endpt_cancel_timer(endpt, &g_rt[tid].rt_test_data[thread_id].timeout_timer);
528
529 /* Update counter and end-time. */
530 g_rt[tid].rt_test_data[thread_id].recv_response_count++;
531 pj_get_timestamp(&recv_time);
532
533 pj_sub_timestamp(&recv_time, &g_rt[tid].rt_test_data[thread_id].send_time);
534 pj_add_timestamp(&g_rt[tid].rt_test_data[thread_id].total_rt_time, &recv_time);
535
536 if ((g_rt[tid].rt_stop & STOP_SEND)==0) {
537 pj_time_val tx_delay = { 0, 0 };
538 pj_assert(g_rt[tid].rt_test_data[thread_id].tx_timer.user_data == NULL);
539 g_rt[tid].rt_test_data[thread_id].tx_timer.user_data = (void*)(pj_ssize_t)1;
540 pjsip_endpt_schedule_timer(endpt, &g_rt[tid].rt_test_data[thread_id].tx_timer,
541 &tx_delay);
542 }
543
544 pj_mutex_unlock(g_rt[tid].rt_test_data[thread_id].mutex);
545
546 return PJ_TRUE;
547 }
548 return PJ_FALSE;
549 }
550
551 static void rt_timeout_timer( pj_timer_heap_t *timer_heap,
552 struct pj_timer_entry *entry )
553 {
554 unsigned tid = entry->id >> 16;
555 unsigned thread_id = entry->id & 0xFFFF;
556
557 pj_assert(tid < PJSIP_TRANSPORT_START_OTHER);
558 pj_mutex_lock(g_rt[tid].rt_test_data[thread_id].mutex);
559
560 PJ_UNUSED_ARG(timer_heap);
561 PJ_LOG(3,(THIS_FILE, " timeout waiting for response"));
562 g_rt[tid].rt_test_data[thread_id].timeout_timer.user_data = NULL;
563
564 if (g_rt[tid].rt_test_data[thread_id].tx_timer.user_data == NULL) {
565 pj_time_val delay = { 0, 0 };
566 g_rt[tid].rt_test_data[thread_id].tx_timer.user_data = (void*)(pj_ssize_t)1;
567 pjsip_endpt_schedule_timer(endpt, &g_rt[tid].rt_test_data[thread_id].tx_timer,
568 &delay);
569 }
570
571 pj_mutex_unlock(g_rt[tid].rt_test_data[thread_id].mutex);
572 }
573
574 static void rt_tx_timer( pj_timer_heap_t *timer_heap,
575 struct pj_timer_entry *entry )
576 {
577 unsigned tid = entry->id >> 16;
578 unsigned thread_id = entry->id & 0xFFFF;
579
580 pj_assert(tid < PJSIP_TRANSPORT_START_OTHER);
581 pj_mutex_lock(g_rt[tid].rt_test_data[thread_id].mutex);
582
583 PJ_UNUSED_ARG(timer_heap);
584 pj_assert(g_rt[tid].rt_test_data[thread_id].tx_timer.user_data != NULL);
585 g_rt[tid].rt_test_data[thread_id].tx_timer.user_data = NULL;
586 rt_send_request(tid, thread_id);
587
588 pj_mutex_unlock(g_rt[tid].rt_test_data[thread_id].mutex);
589 }
590
591
592 static int rt_worker_thread(void *arg)
593 {
594 unsigned tid;
595 int i;
596 pj_time_val poll_delay = { 0, 10 };
597
598 tid = (unsigned)(uintptr_t)arg;
599 pj_assert(tid < PJSIP_TRANSPORT_START_OTHER);
600
601 /* Sleep to allow main threads to run. */
602 pj_thread_sleep(10);
603
604 while ((g_rt[tid].rt_stop & STOP_RECV)==0) {
605 pjsip_endpt_handle_events(endpt, &poll_delay);
606 }
607
608 /* Exhaust responses. */
609 for (i=0; i<100; ++i) {
610 pjsip_endpt_handle_events(endpt, &poll_delay);
611 }
612
613 pjsip_endpt_stop_handle_events(endpt);
614
615 return 0;
616 }
617
618 int transport_rt_test( pjsip_transport_type_e tp_type,
619 pjsip_transport *ref_tp,
620 const char *host_port_transport,
621 int *lost)
622 {
623 enum { THREADS = 1, INTERVAL = 120 };
624 int i, tid=tp_type;
625 char target_url[80];
626 pj_status_t status;
627 pj_pool_t *pool;
628 pj_bool_t logger_enabled;
629
630 pj_timestamp zero_time, total_time;
631 unsigned usec_rt;
632 unsigned total_sent;
633 unsigned total_recv;
634
635 PJ_UNUSED_ARG(tp_type);
636 PJ_UNUSED_ARG(ref_tp);
637
638 PJ_LOG(3,(THIS_FILE, " multithreaded round-trip test (%d threads)...",
639 THREADS));
640 PJ_LOG(3,(THIS_FILE, " this will take approx %d seconds, please wait..",
641 INTERVAL));
642
643 /* Make sure msg logger is disabled. */
644 logger_enabled = msg_logger_set_enabled(0);
645
646 /* Register module (if not yet registered) */
647 pj_enter_critical_section();
648 if (rt_module.id == -1) {
649 status = pjsip_endpt_register_module( endpt, &rt_module );
650 if (status != PJ_SUCCESS) {
651 pj_leave_critical_section();
652 app_perror(" error: unable to register module", status);
653 return -600;
654 }
655 }
656 pj_leave_critical_section();
657
658 /* Create pool for this test. */
659 pool = pjsip_endpt_create_pool(endpt, NULL, 4000, 4000);
660 if (!pool)
661 return -610;
662
663 /* Initialize static test data. */
664 pj_ansi_snprintf(target_url, sizeof(target_url), "sip:%d@%s",
665 tp_type, host_port_transport);
666 pj_ansi_strxcpy(g_rt[tid].rt_target_uri, target_url,
667 sizeof(g_rt[tid].rt_target_uri));
668 g_rt[tid].rt_call_id = pj_str("RT-Call-Id/");
669 g_rt[tid].rt_stop = 0;
670
671 pj_bzero(g_rt[tid].rt_test_data, sizeof(g_rt[tid].rt_test_data));
672
673 /* Need to use specific loop transport (otherwise we may get transport
674 * that is being shutdown) */
675 g_rt[tid].rt_transport = tp_type==PJSIP_TRANSPORT_LOOP_DGRAM? ref_tp : NULL;
676
677 /* Initialize thread data. */
678 for (i=0; i<THREADS; ++i) {
679 char buf[1];
680 pj_str_t str_id;
681
682 pj_strset(&str_id, buf, 1);
683
684 /* Init timer entry */
685 g_rt[tid].rt_test_data[i].tx_timer.id = (tid << 16) | i;
686 g_rt[tid].rt_test_data[i].tx_timer.cb = &rt_tx_timer;
687 g_rt[tid].rt_test_data[i].timeout_timer.id = (tid << 16) | i;
688 g_rt[tid].rt_test_data[i].timeout_timer.cb = &rt_timeout_timer;
689
690 /* Generate Call-ID for each thread. */
691 g_rt[tid].rt_test_data[i].call_id.ptr = (char*) pj_pool_alloc(pool, g_rt[tid].rt_call_id.slen+1);
692 pj_strcpy(&g_rt[tid].rt_test_data[i].call_id, &g_rt[tid].rt_call_id);
693 buf[0] = '0' + (char)i;
694 pj_strcat(&g_rt[tid].rt_test_data[i].call_id, &str_id);
695
696 /* Init mutex. */
697 status = pj_mutex_create_recursive(pool, "rt", &g_rt[tid].rt_test_data[i].mutex);
698 if (status != PJ_SUCCESS) {
699 app_perror(" error: unable to create mutex", status);
700 return -615;
701 }
702
703 /* Create thread, suspended. */
704 status = pj_thread_create(pool, "rttest%p", &rt_worker_thread,
705 (void*)(pj_ssize_t)tid, 0,
706 PJ_THREAD_SUSPENDED, &g_rt[tid].rt_test_data[i].thread);
707 if (status != PJ_SUCCESS) {
708 app_perror(" error: unable to create thread", status);
709 return -620;
710 }
711 }
712
713 /* Start threads! */
714 for (i=0; i<THREADS; ++i) {
715 pj_time_val delay = {0, 0};
716 pj_thread_resume(g_rt[tid].rt_test_data[i].thread);
717
718 /* Schedule first message transmissions. */
719 delay.msec = i*10;
720 g_rt[tid].rt_test_data[i].tx_timer.user_data = (void*)(pj_ssize_t)1;
721 pjsip_endpt_schedule_timer(endpt, &g_rt[tid].rt_test_data[i].tx_timer, &delay);
722 }
723
724 /* Sleep for some time. */
725 pj_thread_sleep(INTERVAL * 1000);
726
727 /* Signal thread to stop sending. */
728 g_rt[tid].rt_stop |= STOP_SEND;
729
730 /* Sleep for some time. */
731 pj_thread_sleep(2 * 1000);
732
733 /* Signal thread to stop . */
734 g_rt[tid].rt_stop |= STOP_RECV;
735
736 /* Wait threads to complete. */
737 for (i=0; i<THREADS; ++i) {
738 pj_thread_join(g_rt[tid].rt_test_data[i].thread);
739 pj_thread_destroy(g_rt[tid].rt_test_data[i].thread);
740 }
741
742 /* Destroy rt_test_data */
743 for (i=0; i<THREADS; ++i) {
744 pj_mutex_destroy(g_rt[tid].rt_test_data[i].mutex);
745 pjsip_endpt_cancel_timer(endpt, &g_rt[tid].rt_test_data[i].timeout_timer);
746 }
747
748 /* Gather statistics. */
749 pj_bzero(&total_time, sizeof(total_time));
750 pj_bzero(&zero_time, sizeof(zero_time));
751 usec_rt = total_sent = total_recv = 0;
752 for (i=0; i<THREADS; ++i) {
753 total_sent += g_rt[tid].rt_test_data[i].sent_request_count;
754 total_recv += g_rt[tid].rt_test_data[i].recv_response_count;
755 pj_add_timestamp(&total_time, &g_rt[tid].rt_test_data[i].total_rt_time);
756 }
757
758 /* Display statistics. */
759 if (total_recv)
760 total_time.u64 = total_time.u64/total_recv;
761 else
762 total_time.u64 = 0;
763 usec_rt = pj_elapsed_usec(&zero_time, &total_time);
764 PJ_LOG(3,(THIS_FILE, " done."));
765 PJ_LOG(3,(THIS_FILE, " total %d messages sent", total_sent));
766 PJ_LOG(3,(THIS_FILE, " average round-trip=%d usec", usec_rt));
767
768 pjsip_endpt_release_pool(endpt, pool);
769
770 *lost = total_sent-total_recv;
771
772 /* Flush events. */
773 flush_events(500);
774
775 /* Restore msg logger. */
776 msg_logger_set_enabled(logger_enabled);
777
778 return 0;
779 }
780
781 ///////////////////////////////////////////////////////////////////////////////
782 /*
783 * Transport load testing
784 */
785 static pj_bool_t load_on_rx_request(pjsip_rx_data *rdata);
786
787 static struct load_test_global_t
788 {
789 pj_int32_t next_seq;
790 pj_bool_t err;
791 } g_lt[PJSIP_TRANSPORT_START_OTHER];
792
793
794 static pjsip_module mod_load_test = {
795 NULL, NULL, /* prev and next */
796 { "mod-load-test", 13}, /* Name. */
797 -1, /* Id */
798 PJSIP_MOD_PRIORITY_TSX_LAYER-1, /* Priority */
799 NULL, /* load() */
800 NULL, /* start() */
801 NULL, /* stop() */
802 NULL, /* unload() */
803 &load_on_rx_request, /* on_rx_request() */
804 NULL, /* on_rx_response() */
805 NULL, /* tsx_handler() */
806 };
807
808
809 static pj_bool_t load_on_rx_request(pjsip_rx_data *rdata)
810 {
811 pjsip_to_hdr *to_hdr = rdata->msg_info.to;
812 pjsip_sip_uri *target = (pjsip_sip_uri*)pjsip_uri_get_uri(to_hdr->uri);
813 unsigned tid;
814
815 if (!is_user_equal(rdata->msg_info.from, "transport_load_test"))
816 return PJ_FALSE;
817
818 tid = (unsigned)pj_strtoul(&target->user);
819 pj_assert(tid < PJSIP_TRANSPORT_START_OTHER);
820
821 if (rdata->msg_info.cseq->cseq != g_lt[tid].next_seq) {
822 PJ_LOG(1,(THIS_FILE, " err: expecting cseq %u, got %u",
823 g_lt[tid].next_seq, rdata->msg_info.cseq->cseq));
824 g_lt[tid].err = PJ_TRUE;
825 g_lt[tid].next_seq = rdata->msg_info.cseq->cseq + 1;
826 } else
827 g_lt[tid].next_seq++;
828 return PJ_TRUE;
829 }
830
831 int transport_load_test(pjsip_transport_type_e tp_type,
832 const char *host_port_transport)
833 {
834 #define ERR(rc__) { rc=rc__; goto on_return; }
835 enum { COUNT = 2000 };
836 char target_url[64];
837 unsigned i, tid=tp_type;
838 int rc;
839
840 pj_ansi_snprintf(target_url, sizeof(target_url), "sip:%d@%s",
841 tp_type, host_port_transport);
842
843 /* exhaust packets */
844 flush_events(500);
845
846 PJ_LOG(3,(THIS_FILE, " transport load test..."));
847
848 pj_enter_critical_section();
849 if (mod_load_test.id == -1) {
850 rc = pjsip_endpt_register_module( endpt, &mod_load_test);
851 if (rc != PJ_SUCCESS) {
852 pj_leave_critical_section();
853 app_perror("error registering module", rc);
854 return -610;
855 }
856 }
857 pj_leave_critical_section();
858 g_lt[tid].err = PJ_FALSE;
859 g_lt[tid].next_seq = 0;
860
861 for (i=0; i<COUNT && !g_lt[tid].err; ++i) {
862 pj_str_t target, from, call_id;
863 pjsip_tx_data *tdata;
864
865 target = pj_str(target_url);
866 from = pj_str("<sip:transport_load_test@host>");
867 call_id = pj_str("thecallid");
868 PJ_TEST_SUCCESS(pjsip_endpt_create_request(endpt, &pjsip_invite_method,
869 &target, &from,
870 &target, &from, &call_id,
871 i, NULL, &tdata ),
872 NULL, ERR(-620));
873
874 PJ_TEST_SUCCESS(pjsip_endpt_send_request_stateless(endpt, tdata,
875 NULL, NULL),
876 NULL, ERR(-630));
877
878 flush_events(20);
879 }
880
881 flush_events(1000);
882
883 PJ_TEST_EQ(g_lt[tid].next_seq, COUNT, "message count mismatch",
884 ERR(-640));
885
886 rc = 0;
887
888 on_return:
889 if (mod_load_test.id != -1) {
890 /* Don't unregister if there are multiple transport_load_test() */
891 pjsip_endpt_unregister_module( endpt, &mod_load_test);
892 mod_load_test.id = -1;
893 }
894 return rc;
895 }
896
897