blob: 0d1d3a22b8d46053999ebb9814beab03e310fd56 [file] [log] [blame]
Benny Prijono4bac2c12008-05-11 18:12:16 +00001/* $Id$ */
2/*
Benny Prijono844653c2008-12-23 17:27:53 +00003 * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com)
4 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
Benny Prijono4bac2c12008-05-11 18:12:16 +00005 *
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 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20#include "test.h"
21#include <pjlib.h>
22
23/**
24 * \page page_pjlib_activesock_test Test: Active Socket
25 *
26 * This file is <b>pjlib-test/activesock.c</b>
27 *
28 * \include pjlib-test/activesock.c
29 */
30
31#if INCLUDE_ACTIVESOCK_TEST
32
33#define THIS_FILE "activesock.c"
34
35
36/*******************************************************************
37 * Simple UDP echo server.
38 */
39struct udp_echo_srv
40{
41 pj_activesock_t *asock;
42 pj_bool_t echo_enabled;
43 pj_uint16_t port;
44 pj_ioqueue_op_key_t send_key;
45 pj_status_t status;
46 unsigned rx_cnt;
47 unsigned rx_err_cnt, tx_err_cnt;
48};
49
50static void udp_echo_err(const char *title, pj_status_t status)
51{
52 char errmsg[PJ_ERR_MSG_SIZE];
53
54 pj_strerror(status, errmsg, sizeof(errmsg));
55 PJ_LOG(3,(THIS_FILE, " error: %s: %s", title, errmsg));
56}
57
58static pj_bool_t udp_echo_srv_on_data_recvfrom(pj_activesock_t *asock,
59 void *data,
60 pj_size_t size,
61 const pj_sockaddr_t *src_addr,
62 int addr_len,
63 pj_status_t status)
64{
65 struct udp_echo_srv *srv;
66 pj_ssize_t sent;
67
68
69 srv = (struct udp_echo_srv*) pj_activesock_get_user_data(asock);
70
71 if (status != PJ_SUCCESS) {
72 srv->status = status;
73 srv->rx_err_cnt++;
74 udp_echo_err("recvfrom() callback", status);
75 return PJ_TRUE;
76 }
77
78 srv->rx_cnt++;
79
80 /* Send back if echo is enabled */
81 if (srv->echo_enabled) {
82 sent = size;
83 srv->status = pj_activesock_sendto(asock, &srv->send_key, data,
84 &sent, 0,
85 src_addr, addr_len);
86 if (srv->status != PJ_SUCCESS) {
87 srv->tx_err_cnt++;
88 udp_echo_err("sendto()", status);
89 }
90 }
91
92 return PJ_TRUE;
93}
94
95
96static pj_status_t udp_echo_srv_create(pj_pool_t *pool,
97 pj_ioqueue_t *ioqueue,
98 pj_bool_t enable_echo,
99 struct udp_echo_srv **p_srv)
100{
101 struct udp_echo_srv *srv;
102 pj_sock_t sock_fd = PJ_INVALID_SOCKET;
103 pj_sockaddr addr;
104 int addr_len;
105 pj_activesock_cb activesock_cb;
106 pj_status_t status;
107
108 srv = PJ_POOL_ZALLOC_T(pool, struct udp_echo_srv);
109 srv->echo_enabled = enable_echo;
110
111 pj_sockaddr_in_init(&addr.ipv4, NULL, 0);
112 addr_len = sizeof(addr);
113
114 pj_bzero(&activesock_cb, sizeof(activesock_cb));
115 activesock_cb.on_data_recvfrom = &udp_echo_srv_on_data_recvfrom;
116
117 status = pj_activesock_create_udp(pool, &addr, NULL, ioqueue, &activesock_cb,
Benny Prijonoea8e4362008-06-06 14:12:23 +0000118 srv, &srv->asock, &addr);
Benny Prijono4bac2c12008-05-11 18:12:16 +0000119 if (status != PJ_SUCCESS) {
120 pj_sock_close(sock_fd);
121 udp_echo_err("pj_activesock_create()", status);
122 return status;
123 }
124
125 srv->port = pj_ntohs(addr.ipv4.sin_port);
126
Benny Prijono4bac2c12008-05-11 18:12:16 +0000127 pj_ioqueue_op_key_init(&srv->send_key, sizeof(srv->send_key));
128
129 status = pj_activesock_start_recvfrom(srv->asock, pool, 32, 0);
130 if (status != PJ_SUCCESS) {
131 pj_activesock_close(srv->asock);
132 udp_echo_err("pj_activesock_start_recvfrom()", status);
133 return status;
134 }
135
136
137 *p_srv = srv;
138 return PJ_SUCCESS;
139}
140
141static void udp_echo_srv_destroy(struct udp_echo_srv *srv)
142{
143 pj_activesock_close(srv->asock);
144}
145
146/*******************************************************************
147 * UDP ping pong test (send packet back and forth between two UDP echo
148 * servers.
149 */
150static int udp_ping_pong_test(void)
151{
152 pj_ioqueue_t *ioqueue = NULL;
153 pj_pool_t *pool = NULL;
154 struct udp_echo_srv *srv1=NULL, *srv2=NULL;
155 pj_bool_t need_send = PJ_TRUE;
156 unsigned data = 0;
157 int count, ret;
158 pj_status_t status;
159
160 pool = pj_pool_create(mem, "pingpong", 512, 512, NULL);
161 if (!pool)
162 return -10;
163
164 status = pj_ioqueue_create(pool, 4, &ioqueue);
165 if (status != PJ_SUCCESS) {
166 ret = -20;
167 udp_echo_err("pj_ioqueue_create()", status);
168 goto on_return;
169 }
170
171 status = udp_echo_srv_create(pool, ioqueue, PJ_TRUE, &srv1);
172 if (status != PJ_SUCCESS) {
173 ret = -30;
174 goto on_return;
175 }
176
177 status = udp_echo_srv_create(pool, ioqueue, PJ_TRUE, &srv2);
178 if (status != PJ_SUCCESS) {
179 ret = -40;
180 goto on_return;
181 }
182
183 /* initiate the first send */
184 for (count=0; count<1000; ++count) {
185 unsigned last_rx1, last_rx2;
186 unsigned i;
187
188 if (need_send) {
189 pj_str_t loopback;
190 pj_sockaddr_in addr;
191 pj_ssize_t sent;
192
193 ++data;
194
195 sent = sizeof(data);
196 loopback = pj_str("127.0.0.1");
197 pj_sockaddr_in_init(&addr, &loopback, srv2->port);
198 status = pj_activesock_sendto(srv1->asock, &srv1->send_key,
199 &data, &sent, 0,
200 &addr, sizeof(addr));
201 if (status != PJ_SUCCESS && status != PJ_EPENDING) {
202 ret = -50;
203 udp_echo_err("sendto()", status);
204 goto on_return;
205 }
206
207 need_send = PJ_FALSE;
208 }
209
210 last_rx1 = srv1->rx_cnt;
211 last_rx2 = srv2->rx_cnt;
212
213 for (i=0; i<10 && last_rx1 == srv1->rx_cnt && last_rx2 == srv2->rx_cnt; ++i) {
214 pj_time_val delay = {0, 10};
215 pj_ioqueue_poll(ioqueue, &delay);
216 }
217
218 if (srv1->rx_err_cnt+srv1->tx_err_cnt != 0 ||
219 srv2->rx_err_cnt+srv2->tx_err_cnt != 0)
220 {
221 /* Got error */
222 ret = -60;
223 goto on_return;
224 }
225
226 if (last_rx1 == srv1->rx_cnt && last_rx2 == srv2->rx_cnt) {
227 /* Packet lost */
228 ret = -70;
229 udp_echo_err("packets have been lost", PJ_ETIMEDOUT);
230 goto on_return;
231 }
232 }
233
234 ret = 0;
235
236on_return:
237 if (srv2)
238 udp_echo_srv_destroy(srv2);
239 if (srv1)
240 udp_echo_srv_destroy(srv1);
241 if (ioqueue)
242 pj_ioqueue_destroy(ioqueue);
243 if (pool)
244 pj_pool_release(pool);
245
246 return ret;
247}
248
249
Benny Prijono417d6052008-07-29 20:15:15 +0000250
251#define SIGNATURE 0xdeadbeef
252struct tcp_pkt
253{
254 pj_uint32_t signature;
255 pj_uint32_t seq;
256 char fill[513];
257};
258
259struct tcp_state
260{
261 pj_bool_t err;
262 pj_bool_t sent;
263 pj_uint32_t next_recv_seq;
264 pj_uint8_t pkt[600];
265};
266
267struct send_key
268{
269 pj_ioqueue_op_key_t op_key;
270};
271
272
273static pj_bool_t tcp_on_data_read(pj_activesock_t *asock,
274 void *data,
275 pj_size_t size,
276 pj_status_t status,
277 pj_size_t *remainder)
278{
279 struct tcp_state *st = (struct tcp_state*) pj_activesock_get_user_data(asock);
280 char *next = (char*) data;
281
282 if (status != PJ_SUCCESS && status != PJ_EPENDING) {
283 PJ_LOG(1,("", " err: status=%d", status));
284 st->err = PJ_TRUE;
285 return PJ_FALSE;
286 }
287
288 while (size >= sizeof(struct tcp_pkt)) {
289 struct tcp_pkt *tcp_pkt = (struct tcp_pkt*) next;
290
291 if (tcp_pkt->signature != SIGNATURE) {
292 PJ_LOG(1,("", " err: invalid signature at seq=%d",
293 st->next_recv_seq));
294 st->err = PJ_TRUE;
295 return PJ_FALSE;
296 }
297 if (tcp_pkt->seq != st->next_recv_seq) {
298 PJ_LOG(1,("", " err: wrong sequence"));
299 st->err = PJ_TRUE;
300 return PJ_FALSE;
301 }
302
303 st->next_recv_seq++;
304 next += sizeof(struct tcp_pkt);
305 size -= sizeof(struct tcp_pkt);
306 }
307
308 if (size) {
309 pj_memmove(data, next, size);
310 *remainder = size;
311 }
312
313 return PJ_TRUE;
314}
315
316static pj_bool_t tcp_on_data_sent(pj_activesock_t *asock,
317 pj_ioqueue_op_key_t *op_key,
318 pj_ssize_t sent)
319{
320 struct tcp_state *st=(struct tcp_state*)pj_activesock_get_user_data(asock);
321
Benny Prijono4df98092008-08-28 10:41:29 +0000322 PJ_UNUSED_ARG(op_key);
323
Benny Prijono417d6052008-07-29 20:15:15 +0000324 st->sent = 1;
325
326 if (sent < 1) {
327 st->err = PJ_TRUE;
328 return PJ_FALSE;
329 }
330
331 return PJ_TRUE;
332}
333
334static int tcp_perf_test(void)
335{
336 enum { COUNT=100000 };
337 pj_pool_t *pool = NULL;
338 pj_ioqueue_t *ioqueue = NULL;
339 pj_sock_t sock1=PJ_INVALID_SOCKET, sock2=PJ_INVALID_SOCKET;
340 pj_activesock_t *asock1 = NULL, *asock2 = NULL;
341 pj_activesock_cb cb;
342 struct tcp_state *state1, *state2;
343 unsigned i;
344 pj_status_t status;
345
346 pool = pj_pool_create(mem, "tcpperf", 256, 256, NULL);
347
348 status = app_socketpair(pj_AF_INET(), pj_SOCK_STREAM(), 0, &sock1,
349 &sock2);
350 if (status != PJ_SUCCESS) {
351 status = -100;
352 goto on_return;
353 }
354
355 status = pj_ioqueue_create(pool, 4, &ioqueue);
356 if (status != PJ_SUCCESS) {
357 status = -110;
358 goto on_return;
359 }
360
361 pj_bzero(&cb, sizeof(cb));
362 cb.on_data_read = &tcp_on_data_read;
363 cb.on_data_sent = &tcp_on_data_sent;
364
365 state1 = PJ_POOL_ZALLOC_T(pool, struct tcp_state);
366 status = pj_activesock_create(pool, sock1, pj_SOCK_STREAM(), NULL, ioqueue,
367 &cb, state1, &asock1);
368 if (status != PJ_SUCCESS) {
369 status = -120;
370 goto on_return;
371 }
372
373 state2 = PJ_POOL_ZALLOC_T(pool, struct tcp_state);
374 status = pj_activesock_create(pool, sock2, pj_SOCK_STREAM(), NULL, ioqueue,
375 &cb, state2, &asock2);
376 if (status != PJ_SUCCESS) {
377 status = -130;
378 goto on_return;
379 }
380
381 status = pj_activesock_start_read(asock1, pool, 1000, 0);
382 if (status != PJ_SUCCESS) {
383 status = -140;
384 goto on_return;
385 }
386
387 /* Send packet as quickly as possible */
388 for (i=0; i<COUNT && !state1->err && !state2->err; ++i) {
389 struct tcp_pkt *pkt;
390 struct send_key send_key[2], *op_key;
391 pj_ssize_t len;
392
393 pkt = (struct tcp_pkt*)state2->pkt;
394 pkt->signature = SIGNATURE;
395 pkt->seq = i;
396 pj_memset(pkt->fill, 'a', sizeof(pkt->fill));
397
398 op_key = &send_key[i%2];
399 pj_ioqueue_op_key_init(&op_key->op_key, sizeof(*op_key));
400
401 state2->sent = PJ_FALSE;
402 len = sizeof(*pkt);
403 status = pj_activesock_send(asock2, &op_key->op_key, pkt, &len, 0);
404 if (status == PJ_EPENDING) {
405 do {
406 pj_ioqueue_poll(ioqueue, NULL);
407 } while (!state2->sent);
408 } else if (status != PJ_SUCCESS) {
409 PJ_LOG(1,("", " err: send status=%d", status));
410 status = -180;
411 break;
412 } else if (status == PJ_SUCCESS) {
413 if (len != sizeof(*pkt)) {
414 PJ_LOG(1,("", " err: shouldn't report partial sent"));
415 status = -190;
416 break;
417 }
418 }
419 }
420
421 /* Wait until everything has been sent/received */
422 if (state1->next_recv_seq < COUNT) {
423 pj_time_val delay = {0, 100};
424 while (pj_ioqueue_poll(ioqueue, &delay) > 0)
425 ;
426 }
427
428 if (status == PJ_EPENDING)
429 status = PJ_SUCCESS;
430
431 if (status != 0)
432 goto on_return;
433
434 if (state1->err) {
435 status = -183;
436 goto on_return;
437 }
438 if (state2->err) {
439 status = -186;
440 goto on_return;
441 }
442 if (state1->next_recv_seq != COUNT) {
443 PJ_LOG(3,("", " err: only %u packets received, expecting %u",
444 state1->next_recv_seq, COUNT));
445 status = -195;
446 goto on_return;
447 }
448
449on_return:
450 if (asock2)
451 pj_activesock_close(asock2);
452 if (asock1)
453 pj_activesock_close(asock1);
454 if (ioqueue)
455 pj_ioqueue_destroy(ioqueue);
456 if (pool)
457 pj_pool_release(pool);
458
459 return status;
460}
461
462
463
Benny Prijono4bac2c12008-05-11 18:12:16 +0000464int activesock_test(void)
465{
466 int ret;
467
Benny Prijono417d6052008-07-29 20:15:15 +0000468 ret = (int)&udp_ping_pong_test;
469
Benny Prijono4bac2c12008-05-11 18:12:16 +0000470 PJ_LOG(3,("", "..udp ping/pong test"));
471 ret = udp_ping_pong_test();
472 if (ret != 0)
473 return ret;
474
Benny Prijono417d6052008-07-29 20:15:15 +0000475 PJ_LOG(3,("", "..tcp perf test"));
476 ret = tcp_perf_test();
477 if (ret != 0)
478 return ret;
479
Benny Prijono4bac2c12008-05-11 18:12:16 +0000480 return 0;
481}
482
483#else /* INCLUDE_ACTIVESOCK_TEST */
484/* To prevent warning about "translation unit is empty"
485 * when this test is disabled.
486 */
487int dummy_active_sock_test;
488#endif /* INCLUDE_ACTIVESOCK_TEST */
489