1 /* $Id$ */
2 /*
3  * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4  * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
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  * 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 
22 
23 /**
24  * \page page_pjlib_ioqueue_udp_test Test: I/O Queue (UDP)
25  *
26  * This file provides implementation to test the
27  * functionality of the I/O queue when UDP socket is used.
28  *
29  *
30  * This file is <b>pjlib-test/ioq_udp.c</b>
31  *
32  * \include pjlib-test/ioq_udp.c
33  */
34 
35 
36 #if INCLUDE_UDP_IOQUEUE_TEST
37 
38 #include <pjlib.h>
39 
40 #include <pj/compat/socket.h>
41 
42 #define THIS_FILE	    "test_udp"
43 #define PORT		    51233
44 #define LOOP		    2
45 ///#define LOOP		    2
46 #define BUF_MIN_SIZE	    32
47 #define BUF_MAX_SIZE	    2048
48 #define SOCK_INACTIVE_MIN   (1)
49 #define SOCK_INACTIVE_MAX   (PJ_IOQUEUE_MAX_HANDLES - 2)
50 #define POOL_SIZE	    (2*BUF_MAX_SIZE + SOCK_INACTIVE_MAX*128 + 2048)
51 
52 #undef TRACE_
53 #define TRACE_(msg)	    PJ_LOG(3,(THIS_FILE,"....." msg))
54 
55 #if 0
56 #  define TRACE__(args)	    PJ_LOG(3,args)
57 #else
58 #  define TRACE__(args)
59 #endif
60 
61 
62 static pj_ssize_t            callback_read_size,
63                              callback_write_size,
64                              callback_accept_status,
65                              callback_connect_status;
66 static pj_ioqueue_key_t     *callback_read_key,
67                             *callback_write_key,
68                             *callback_accept_key,
69                             *callback_connect_key;
70 static pj_ioqueue_op_key_t  *callback_read_op,
71                             *callback_write_op,
72                             *callback_accept_op;
73 
on_ioqueue_read(pj_ioqueue_key_t * key,pj_ioqueue_op_key_t * op_key,pj_ssize_t bytes_read)74 static void on_ioqueue_read(pj_ioqueue_key_t *key,
75                             pj_ioqueue_op_key_t *op_key,
76                             pj_ssize_t bytes_read)
77 {
78     callback_read_key = key;
79     callback_read_op = op_key;
80     callback_read_size = bytes_read;
81     TRACE__((THIS_FILE, "     callback_read_key = %p, bytes=%d",
82 	     key, bytes_read));
83 }
84 
on_ioqueue_write(pj_ioqueue_key_t * key,pj_ioqueue_op_key_t * op_key,pj_ssize_t bytes_written)85 static void on_ioqueue_write(pj_ioqueue_key_t *key,
86                              pj_ioqueue_op_key_t *op_key,
87                              pj_ssize_t bytes_written)
88 {
89     callback_write_key = key;
90     callback_write_op = op_key;
91     callback_write_size = bytes_written;
92 }
93 
on_ioqueue_accept(pj_ioqueue_key_t * key,pj_ioqueue_op_key_t * op_key,pj_sock_t sock,int status)94 static void on_ioqueue_accept(pj_ioqueue_key_t *key,
95                               pj_ioqueue_op_key_t *op_key,
96                               pj_sock_t sock, int status)
97 {
98     PJ_UNUSED_ARG(sock);
99     callback_accept_key = key;
100     callback_accept_op = op_key;
101     callback_accept_status = status;
102 }
103 
on_ioqueue_connect(pj_ioqueue_key_t * key,int status)104 static void on_ioqueue_connect(pj_ioqueue_key_t *key, int status)
105 {
106     callback_connect_key = key;
107     callback_connect_status = status;
108 }
109 
110 static pj_ioqueue_callback test_cb =
111 {
112     &on_ioqueue_read,
113     &on_ioqueue_write,
114     &on_ioqueue_accept,
115     &on_ioqueue_connect,
116 };
117 
118 #if defined(PJ_WIN32) || defined(PJ_WIN64)
119 #  define S_ADDR S_un.S_addr
120 #else
121 #  define S_ADDR s_addr
122 #endif
123 
124 /*
125  * compliance_test()
126  * To test that the basic IOQueue functionality works. It will just exchange
127  * data between two sockets.
128  */
compliance_test(pj_bool_t allow_concur)129 static int compliance_test(pj_bool_t allow_concur)
130 {
131     pj_sock_t ssock=-1, csock=-1;
132     pj_sockaddr_in addr, dst_addr;
133     int addrlen;
134     pj_pool_t *pool = NULL;
135     char *send_buf, *recv_buf;
136     pj_ioqueue_t *ioque = NULL;
137     pj_ioqueue_key_t *skey = NULL, *ckey = NULL;
138     pj_ioqueue_op_key_t read_op, write_op;
139     int bufsize = BUF_MIN_SIZE;
140     pj_ssize_t bytes;
141     int status = -1;
142     pj_str_t temp;
143     pj_bool_t send_pending, recv_pending;
144     pj_status_t rc;
145 
146     pj_set_os_error(PJ_SUCCESS);
147 
148     // Create pool.
149     pool = pj_pool_create(mem, NULL, POOL_SIZE, 4000, NULL);
150 
151     // Allocate buffers for send and receive.
152     send_buf = (char*)pj_pool_alloc(pool, bufsize);
153     recv_buf = (char*)pj_pool_alloc(pool, bufsize);
154 
155     // Allocate sockets for sending and receiving.
156     TRACE_("creating sockets...");
157     rc = pj_sock_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, &ssock);
158     if (rc==PJ_SUCCESS)
159         rc = pj_sock_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, &csock);
160     else
161         csock = PJ_INVALID_SOCKET;
162     if (rc != PJ_SUCCESS) {
163         app_perror("...ERROR in pj_sock_socket()", rc);
164 	status=-1; goto on_error;
165     }
166 
167     // Bind server socket.
168     TRACE_("bind socket...");
169     pj_bzero(&addr, sizeof(addr));
170     addr.sin_family = pj_AF_INET();
171     addr.sin_port = pj_htons(PORT);
172     if (pj_sock_bind(ssock, &addr, sizeof(addr))) {
173 	status=-10; goto on_error;
174     }
175 
176     // Create I/O Queue.
177     TRACE_("create ioqueue...");
178     rc = pj_ioqueue_create(pool, PJ_IOQUEUE_MAX_HANDLES, &ioque);
179     if (rc != PJ_SUCCESS) {
180 	status=-20; goto on_error;
181     }
182 
183     // Set concurrency
184     TRACE_("set concurrency...");
185     rc = pj_ioqueue_set_default_concurrency(ioque, allow_concur);
186     if (rc != PJ_SUCCESS) {
187 	status=-21; goto on_error;
188     }
189 
190     // Register server and client socket.
191     // We put this after inactivity socket, hopefully this can represent the
192     // worst waiting time.
193     TRACE_("registering first sockets...");
194     rc = pj_ioqueue_register_sock(pool, ioque, ssock, NULL,
195 			          &test_cb, &skey);
196     if (rc != PJ_SUCCESS) {
197 	app_perror("...error(10): ioqueue_register error", rc);
198 	status=-25; goto on_error;
199     }
200     TRACE_("registering second sockets...");
201     rc = pj_ioqueue_register_sock( pool, ioque, csock, NULL,
202 			           &test_cb, &ckey);
203     if (rc != PJ_SUCCESS) {
204 	app_perror("...error(11): ioqueue_register error", rc);
205 	status=-26; goto on_error;
206     }
207 
208     // Randomize send_buf.
209     pj_create_random_string(send_buf, bufsize);
210 
211     // Init operation keys.
212     pj_ioqueue_op_key_init(&read_op, sizeof(read_op));
213     pj_ioqueue_op_key_init(&write_op, sizeof(write_op));
214 
215     // Register reading from ioqueue.
216     TRACE_("start recvfrom...");
217     pj_bzero(&addr, sizeof(addr));
218     addrlen = sizeof(addr);
219     bytes = bufsize;
220     rc = pj_ioqueue_recvfrom(skey, &read_op, recv_buf, &bytes, 0,
221 			     &addr, &addrlen);
222     if (rc != PJ_SUCCESS && rc != PJ_EPENDING) {
223         app_perror("...error: pj_ioqueue_recvfrom", rc);
224 	status=-28; goto on_error;
225     } else if (rc == PJ_EPENDING) {
226 	recv_pending = 1;
227 	PJ_LOG(3, (THIS_FILE,
228 		   "......ok: recvfrom returned pending"));
229     } else {
230 	PJ_LOG(3, (THIS_FILE,
231 		   "......error: recvfrom returned immediate ok!"));
232 	status=-29; goto on_error;
233     }
234 
235     // Set destination address to send the packet.
236     TRACE_("set destination address...");
237     temp = pj_str("127.0.0.1");
238     if ((rc=pj_sockaddr_in_init(&dst_addr, &temp, PORT)) != 0) {
239 	app_perror("...error: unable to resolve 127.0.0.1", rc);
240 	status=-290; goto on_error;
241     }
242 
243     // Write must return the number of bytes.
244     TRACE_("start sendto...");
245     bytes = bufsize;
246     rc = pj_ioqueue_sendto(ckey, &write_op, send_buf, &bytes, 0, &dst_addr,
247 			   sizeof(dst_addr));
248     if (rc != PJ_SUCCESS && rc != PJ_EPENDING) {
249         app_perror("...error: pj_ioqueue_sendto", rc);
250 	status=-30; goto on_error;
251     } else if (rc == PJ_EPENDING) {
252 	send_pending = 1;
253 	PJ_LOG(3, (THIS_FILE,
254 		   "......ok: sendto returned pending"));
255     } else {
256 	send_pending = 0;
257 	PJ_LOG(3, (THIS_FILE,
258 		   "......ok: sendto returned immediate success"));
259     }
260 
261     // reset callback variables.
262     callback_read_size = callback_write_size = 0;
263     callback_accept_status = callback_connect_status = -2;
264     callback_read_key = callback_write_key =
265         callback_accept_key = callback_connect_key = NULL;
266     callback_read_op = callback_write_op = NULL;
267 
268     // Poll if pending.
269     while (send_pending || recv_pending) {
270 	int ret;
271 	pj_time_val timeout = { 5, 0 };
272 
273 	TRACE_("poll...");
274 #ifdef PJ_SYMBIAN
275 	ret = pj_symbianos_poll(-1, PJ_TIME_VAL_MSEC(timeout));
276 #else
277 	ret = pj_ioqueue_poll(ioque, &timeout);
278 #endif
279 
280 	if (ret == 0) {
281 	    PJ_LOG(1,(THIS_FILE, "...ERROR: timed out..."));
282 	    status=-45; goto on_error;
283         } else if (ret < 0) {
284             app_perror("...ERROR in ioqueue_poll()", -ret);
285 	    status=-50; goto on_error;
286 	}
287 
288 	if (callback_read_key != NULL) {
289             if (callback_read_size != bufsize) {
290                 status=-61; goto on_error;
291             }
292             if (callback_read_key != skey) {
293                 status=-65; goto on_error;
294             }
295             if (callback_read_op != &read_op) {
296                 status=-66; goto on_error;
297             }
298 
299 	    if (pj_memcmp(send_buf, recv_buf, bufsize) != 0) {
300 		status=-67; goto on_error;
301 	    }
302 	    if (addrlen != sizeof(pj_sockaddr_in)) {
303 		status=-68; goto on_error;
304 	    }
305 	    if (addr.sin_family != pj_AF_INET()) {
306 		status=-69; goto on_error;
307 	    }
308 
309 
310 	    recv_pending = 0;
311 	}
312 
313         if (callback_write_key != NULL) {
314             if (callback_write_size != bufsize) {
315                 status=-73; goto on_error;
316             }
317             if (callback_write_key != ckey) {
318                 status=-75; goto on_error;
319             }
320             if (callback_write_op != &write_op) {
321                 status=-76; goto on_error;
322             }
323 
324             send_pending = 0;
325 	}
326     }
327 
328     // Success
329     status = 0;
330 
331 on_error:
332     if (skey)
333     	pj_ioqueue_unregister(skey);
334     else if (ssock != -1)
335 	pj_sock_close(ssock);
336 
337     if (ckey)
338     	pj_ioqueue_unregister(ckey);
339     else if (csock != -1)
340 	pj_sock_close(csock);
341 
342     if (ioque != NULL)
343 	pj_ioqueue_destroy(ioque);
344     pj_pool_release(pool);
345     return status;
346 
347 }
348 
349 
on_read_complete(pj_ioqueue_key_t * key,pj_ioqueue_op_key_t * op_key,pj_ssize_t bytes_read)350 static void on_read_complete(pj_ioqueue_key_t *key,
351                              pj_ioqueue_op_key_t *op_key,
352                              pj_ssize_t bytes_read)
353 {
354     unsigned *p_packet_cnt = (unsigned*) pj_ioqueue_get_user_data(key);
355 
356     PJ_UNUSED_ARG(op_key);
357     PJ_UNUSED_ARG(bytes_read);
358 
359     (*p_packet_cnt)++;
360 }
361 
362 /*
363  * unregister_test()
364  * Check if callback is still called after socket has been unregistered or
365  * closed.
366  */
unregister_test(pj_bool_t allow_concur)367 static int unregister_test(pj_bool_t allow_concur)
368 {
369     enum { RPORT = 50000, SPORT = 50001 };
370     pj_pool_t *pool;
371     pj_ioqueue_t *ioqueue;
372     pj_sock_t ssock;
373     pj_sock_t rsock;
374     int addrlen;
375     pj_sockaddr_in addr;
376     pj_ioqueue_key_t *key;
377     pj_ioqueue_op_key_t opkey;
378     pj_ioqueue_callback cb;
379     unsigned packet_cnt;
380     char sendbuf[10], recvbuf[10];
381     pj_ssize_t bytes;
382     pj_time_val timeout;
383     pj_status_t status;
384 
385     pool = pj_pool_create(mem, "test", 4000, 4000, NULL);
386     if (!pool) {
387 	app_perror("Unable to create pool", PJ_ENOMEM);
388 	return -100;
389     }
390 
391     status = pj_ioqueue_create(pool, 16, &ioqueue);
392     if (status != PJ_SUCCESS) {
393 	app_perror("Error creating ioqueue", status);
394 	return -110;
395     }
396 
397     // Set concurrency
398     TRACE_("set concurrency...");
399     status = pj_ioqueue_set_default_concurrency(ioqueue, allow_concur);
400     if (status != PJ_SUCCESS) {
401 	return -112;
402     }
403 
404     /* Create sender socket */
405     status = app_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, SPORT, &ssock);
406     if (status != PJ_SUCCESS) {
407 	app_perror("Error initializing socket", status);
408 	return -120;
409     }
410 
411     /* Create receiver socket. */
412     status = app_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, RPORT, &rsock);
413     if (status != PJ_SUCCESS) {
414 	app_perror("Error initializing socket", status);
415 	return -130;
416     }
417 
418     /* Register rsock to ioqueue. */
419     pj_bzero(&cb, sizeof(cb));
420     cb.on_read_complete = &on_read_complete;
421     packet_cnt = 0;
422     status = pj_ioqueue_register_sock(pool, ioqueue, rsock, &packet_cnt,
423 				      &cb, &key);
424     if (status != PJ_SUCCESS) {
425 	app_perror("Error registering to ioqueue", status);
426 	return -140;
427     }
428 
429     /* Init operation key. */
430     pj_ioqueue_op_key_init(&opkey, sizeof(opkey));
431 
432     /* Start reading. */
433     bytes = sizeof(recvbuf);
434     status = pj_ioqueue_recv( key, &opkey, recvbuf, &bytes, 0);
435     if (status != PJ_EPENDING) {
436 	app_perror("Expecting PJ_EPENDING, but got this", status);
437 	return -150;
438     }
439 
440     /* Init destination address. */
441     addrlen = sizeof(addr);
442     status = pj_sock_getsockname(rsock, &addr, &addrlen);
443     if (status != PJ_SUCCESS) {
444 	app_perror("getsockname error", status);
445 	return -160;
446     }
447 
448     /* Override address with 127.0.0.1, since getsockname will return
449      * zero in the address field.
450      */
451     addr.sin_addr = pj_inet_addr2("127.0.0.1");
452 
453     /* Init buffer to send */
454     pj_ansi_strcpy(sendbuf, "Hello0123");
455 
456     /* Send one packet. */
457     bytes = sizeof(sendbuf);
458     status = pj_sock_sendto(ssock, sendbuf, &bytes, 0,
459 			    &addr, sizeof(addr));
460 
461     if (status != PJ_SUCCESS) {
462 	app_perror("sendto error", status);
463 	return -170;
464     }
465 
466     /* Check if packet is received. */
467     timeout.sec = 1; timeout.msec = 0;
468 #ifdef PJ_SYMBIAN
469     pj_symbianos_poll(-1, 1000);
470 #else
471     pj_ioqueue_poll(ioqueue, &timeout);
472 #endif
473 
474     if (packet_cnt != 1) {
475 	return -180;
476     }
477 
478     /* Just to make sure things are settled.. */
479     pj_thread_sleep(100);
480 
481     /* Start reading again. */
482     bytes = sizeof(recvbuf);
483     status = pj_ioqueue_recv( key, &opkey, recvbuf, &bytes, 0);
484     if (status != PJ_EPENDING) {
485 	app_perror("Expecting PJ_EPENDING, but got this", status);
486 	return -190;
487     }
488 
489     /* Reset packet counter */
490     packet_cnt = 0;
491 
492     /* Send one packet. */
493     bytes = sizeof(sendbuf);
494     status = pj_sock_sendto(ssock, sendbuf, &bytes, 0,
495 			    &addr, sizeof(addr));
496 
497     if (status != PJ_SUCCESS) {
498 	app_perror("sendto error", status);
499 	return -200;
500     }
501 
502     /* Now unregister and close socket. */
503     pj_ioqueue_unregister(key);
504 
505     /* Poll ioqueue. */
506 #ifdef PJ_SYMBIAN
507     pj_symbianos_poll(-1, 1000);
508 #else
509     timeout.sec = 1; timeout.msec = 0;
510     pj_ioqueue_poll(ioqueue, &timeout);
511 #endif
512 
513     /* Must NOT receive any packets after socket is closed! */
514     if (packet_cnt > 0) {
515 	PJ_LOG(3,(THIS_FILE, "....errror: not expecting to receive packet "
516 			     "after socket has been closed"));
517 	return -210;
518     }
519 
520     /* Success */
521     pj_sock_close(ssock);
522     pj_ioqueue_destroy(ioqueue);
523 
524     pj_pool_release(pool);
525 
526     return 0;
527 }
528 
529 
530 /*
531  * Testing with many handles.
532  * This will just test registering PJ_IOQUEUE_MAX_HANDLES count
533  * of sockets to the ioqueue.
534  */
many_handles_test(pj_bool_t allow_concur)535 static int many_handles_test(pj_bool_t allow_concur)
536 {
537     enum { MAX = PJ_IOQUEUE_MAX_HANDLES };
538     pj_pool_t *pool;
539     pj_ioqueue_t *ioqueue;
540     pj_sock_t *sock;
541     pj_ioqueue_key_t **key;
542     pj_status_t rc;
543     int count, i; /* must be signed */
544 
545     PJ_LOG(3,(THIS_FILE,"...testing with so many handles"));
546 
547     pool = pj_pool_create(mem, NULL, 4000, 4000, NULL);
548     if (!pool)
549 	return PJ_ENOMEM;
550 
551     key = (pj_ioqueue_key_t**)
552     	  pj_pool_alloc(pool, MAX*sizeof(pj_ioqueue_key_t*));
553     sock = (pj_sock_t*) pj_pool_alloc(pool, MAX*sizeof(pj_sock_t));
554 
555     /* Create IOQueue */
556     rc = pj_ioqueue_create(pool, MAX, &ioqueue);
557     if (rc != PJ_SUCCESS || ioqueue == NULL) {
558 	app_perror("...error in pj_ioqueue_create", rc);
559 	return -10;
560     }
561 
562     // Set concurrency
563     rc = pj_ioqueue_set_default_concurrency(ioqueue, allow_concur);
564     if (rc != PJ_SUCCESS) {
565 	return -11;
566     }
567 
568     /* Register as many sockets. */
569     for (count=0; count<MAX; ++count) {
570 	sock[count] = PJ_INVALID_SOCKET;
571 	rc = pj_sock_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, &sock[count]);
572 	if (rc != PJ_SUCCESS || sock[count] == PJ_INVALID_SOCKET) {
573 	    PJ_LOG(3,(THIS_FILE, "....unable to create %d-th socket, rc=%d",
574 				 count, rc));
575 	    break;
576 	}
577 	key[count] = NULL;
578 	rc = pj_ioqueue_register_sock(pool, ioqueue, sock[count],
579 				      NULL, &test_cb, &key[count]);
580 	if (rc != PJ_SUCCESS || key[count] == NULL) {
581 	    PJ_LOG(3,(THIS_FILE, "....unable to register %d-th socket, rc=%d",
582 				 count, rc));
583 	    return -30;
584 	}
585     }
586 
587     /* Test complete. */
588 
589     /* Now deregister and close all handles. */
590 
591     /* NOTE for RTEMS:
592      *  It seems that the order of close(sock) is pretty important here.
593      *  If we close the sockets with the same order as when they were created,
594      *  RTEMS doesn't seem to reuse the sockets, thus next socket created
595      *  will have descriptor higher than the last socket created.
596      *  If we close the sockets in the reverse order, then the descriptor will
597      *  get reused.
598      *  This used to cause problem with select ioqueue, since the ioqueue
599      *  always gives FD_SETSIZE for the first select() argument. This ioqueue
600      *  behavior can be changed with setting PJ_SELECT_NEEDS_NFDS macro.
601      */
602     for (i=count-1; i>=0; --i) {
603     ///for (i=0; i<count; ++i) {
604 	rc = pj_ioqueue_unregister(key[i]);
605 	if (rc != PJ_SUCCESS) {
606 	    app_perror("...error in pj_ioqueue_unregister", rc);
607 	}
608     }
609 
610     rc = pj_ioqueue_destroy(ioqueue);
611     if (rc != PJ_SUCCESS) {
612 	app_perror("...error in pj_ioqueue_destroy", rc);
613     }
614 
615     pj_pool_release(pool);
616 
617     PJ_LOG(3,(THIS_FILE,"....many_handles_test() ok"));
618 
619     return 0;
620 }
621 
622 /*
623  * Multi-operation test.
624  */
625 
626 /*
627  * Benchmarking IOQueue
628  */
bench_test(pj_bool_t allow_concur,int bufsize,int inactive_sock_count)629 static int bench_test(pj_bool_t allow_concur, int bufsize,
630 		      int inactive_sock_count)
631 {
632     pj_sock_t ssock=-1, csock=-1;
633     pj_sockaddr_in addr;
634     pj_pool_t *pool = NULL;
635     pj_sock_t *inactive_sock=NULL;
636     pj_ioqueue_op_key_t *inactive_read_op;
637     char *send_buf, *recv_buf;
638     pj_ioqueue_t *ioque = NULL;
639     pj_ioqueue_key_t *skey, *ckey, *keys[SOCK_INACTIVE_MAX+2];
640     pj_timestamp t1, t2, t_elapsed;
641     int rc=0, i;    /* i must be signed */
642     pj_str_t temp;
643     char errbuf[PJ_ERR_MSG_SIZE];
644 
645     TRACE__((THIS_FILE, "   bench test %d", inactive_sock_count));
646 
647     // Create pool.
648     pool = pj_pool_create(mem, NULL, POOL_SIZE, 4000, NULL);
649 
650     // Allocate buffers for send and receive.
651     send_buf = (char*)pj_pool_alloc(pool, bufsize);
652     recv_buf = (char*)pj_pool_alloc(pool, bufsize);
653 
654     // Allocate sockets for sending and receiving.
655     rc = pj_sock_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, &ssock);
656     if (rc == PJ_SUCCESS) {
657         rc = pj_sock_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, &csock);
658     } else
659         csock = PJ_INVALID_SOCKET;
660     if (rc != PJ_SUCCESS) {
661 	app_perror("...error: pj_sock_socket()", rc);
662 	goto on_error;
663     }
664 
665     // Bind server socket.
666     pj_bzero(&addr, sizeof(addr));
667     addr.sin_family = pj_AF_INET();
668     addr.sin_port = pj_htons(PORT);
669     if (pj_sock_bind(ssock, &addr, sizeof(addr)))
670 	goto on_error;
671 
672     pj_assert(inactive_sock_count+2 <= PJ_IOQUEUE_MAX_HANDLES);
673 
674     // Create I/O Queue.
675     rc = pj_ioqueue_create(pool, PJ_IOQUEUE_MAX_HANDLES, &ioque);
676     if (rc != PJ_SUCCESS) {
677 	app_perror("...error: pj_ioqueue_create()", rc);
678 	goto on_error;
679     }
680 
681     // Set concurrency
682     rc = pj_ioqueue_set_default_concurrency(ioque, allow_concur);
683     if (rc != PJ_SUCCESS) {
684 	app_perror("...error: pj_ioqueue_set_default_concurrency()", rc);
685 	goto on_error;
686     }
687 
688     // Allocate inactive sockets, and bind them to some arbitrary address.
689     // Then register them to the I/O queue, and start a read operation.
690     inactive_sock = (pj_sock_t*)pj_pool_alloc(pool,
691 				    inactive_sock_count*sizeof(pj_sock_t));
692     inactive_read_op = (pj_ioqueue_op_key_t*)pj_pool_alloc(pool,
693                               inactive_sock_count*sizeof(pj_ioqueue_op_key_t));
694     pj_bzero(&addr, sizeof(addr));
695     addr.sin_family = pj_AF_INET();
696     for (i=0; i<inactive_sock_count; ++i) {
697         pj_ssize_t bytes;
698 
699 	rc = pj_sock_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, &inactive_sock[i]);
700 	if (rc != PJ_SUCCESS || inactive_sock[i] < 0) {
701 	    app_perror("...error: pj_sock_socket()", rc);
702 	    goto on_error;
703 	}
704 	if ((rc=pj_sock_bind(inactive_sock[i], &addr, sizeof(addr))) != 0) {
705 	    pj_sock_close(inactive_sock[i]);
706 	    inactive_sock[i] = PJ_INVALID_SOCKET;
707 	    app_perror("...error: pj_sock_bind()", rc);
708 	    goto on_error;
709 	}
710 	rc = pj_ioqueue_register_sock(pool, ioque, inactive_sock[i],
711 			              NULL, &test_cb, &keys[i]);
712 	if (rc != PJ_SUCCESS) {
713 	    pj_sock_close(inactive_sock[i]);
714 	    inactive_sock[i] = PJ_INVALID_SOCKET;
715 	    app_perror("...error(1): pj_ioqueue_register_sock()", rc);
716 	    PJ_LOG(3,(THIS_FILE, "....i=%d", i));
717 	    goto on_error;
718 	}
719         bytes = bufsize;
720         pj_ioqueue_op_key_init(&inactive_read_op[i],
721         		       sizeof(inactive_read_op[i]));
722 	rc = pj_ioqueue_recv(keys[i], &inactive_read_op[i], recv_buf, &bytes, 0);
723 	if (rc != PJ_EPENDING) {
724 	    pj_sock_close(inactive_sock[i]);
725 	    inactive_sock[i] = PJ_INVALID_SOCKET;
726 	    app_perror("...error: pj_ioqueue_read()", rc);
727 	    goto on_error;
728 	}
729     }
730 
731     // Register server and client socket.
732     // We put this after inactivity socket, hopefully this can represent the
733     // worst waiting time.
734     rc = pj_ioqueue_register_sock(pool, ioque, ssock, NULL,
735 			          &test_cb, &skey);
736     if (rc != PJ_SUCCESS) {
737 	app_perror("...error(2): pj_ioqueue_register_sock()", rc);
738 	goto on_error;
739     }
740 
741     rc = pj_ioqueue_register_sock(pool, ioque, csock, NULL,
742 			          &test_cb, &ckey);
743     if (rc != PJ_SUCCESS) {
744 	app_perror("...error(3): pj_ioqueue_register_sock()", rc);
745 	goto on_error;
746     }
747 
748     // Set destination address to send the packet.
749     pj_sockaddr_in_init(&addr, pj_cstr(&temp, "127.0.0.1"), PORT);
750 
751     // Test loop.
752     t_elapsed.u64 = 0;
753     for (i=0; i<LOOP; ++i) {
754 	pj_ssize_t bytes;
755         pj_ioqueue_op_key_t read_op, write_op;
756 
757 	// Randomize send buffer.
758 	pj_create_random_string(send_buf, bufsize);
759 
760         // Init operation keys.
761         pj_ioqueue_op_key_init(&read_op, sizeof(read_op));
762         pj_ioqueue_op_key_init(&write_op, sizeof(write_op));
763 
764 	// Start reading on the server side.
765         bytes = bufsize;
766 	rc = pj_ioqueue_recv(skey, &read_op, recv_buf, &bytes, 0);
767 	if (rc != PJ_EPENDING) {
768 	    app_perror("...error: pj_ioqueue_read()", rc);
769 	    break;
770 	}
771 
772 	// Starts send on the client side.
773         bytes = bufsize;
774 	rc = pj_ioqueue_sendto(ckey, &write_op, send_buf, &bytes, 0,
775 			       &addr, sizeof(addr));
776 	if (rc != PJ_SUCCESS && rc != PJ_EPENDING) {
777 	    app_perror("...error: pj_ioqueue_write()", rc);
778 	    break;
779 	}
780 	if (rc == PJ_SUCCESS) {
781 	    if (bytes < 0) {
782 		app_perror("...error: pj_ioqueue_sendto()",(pj_status_t)-bytes);
783 		break;
784 	    }
785 	}
786 
787 	// Begin time.
788 	pj_get_timestamp(&t1);
789 
790 	// Poll the queue until we've got completion event in the server side.
791         callback_read_key = NULL;
792         callback_read_size = 0;
793 	TRACE__((THIS_FILE, "     waiting for key = %p", skey));
794 	do {
795 	    pj_time_val timeout = { 1, 0 };
796 #ifdef PJ_SYMBIAN
797 	    rc = pj_symbianos_poll(-1, PJ_TIME_VAL_MSEC(timeout));
798 #else
799 	    rc = pj_ioqueue_poll(ioque, &timeout);
800 #endif
801 	    TRACE__((THIS_FILE, "     poll rc=%d", rc));
802 	} while (rc >= 0 && callback_read_key != skey);
803 
804 	// End time.
805 	pj_get_timestamp(&t2);
806 	t_elapsed.u64 += (t2.u64 - t1.u64);
807 
808 	if (rc < 0) {
809 	    app_perror("   error: pj_ioqueue_poll", -rc);
810 	    break;
811 	}
812 
813 	// Compare recv buffer with send buffer.
814 	if (callback_read_size != bufsize ||
815 	    pj_memcmp(send_buf, recv_buf, bufsize))
816 	{
817 	    rc = -10;
818 	    PJ_LOG(3,(THIS_FILE, "   error: size/buffer mismatch"));
819 	    break;
820 	}
821 
822 	// Poll until all events are exhausted, before we start the next loop.
823 	do {
824 	    pj_time_val timeout = { 0, 10 };
825 #ifdef PJ_SYMBIAN
826 	    PJ_UNUSED_ARG(timeout);
827 	    rc = pj_symbianos_poll(-1, 100);
828 #else
829 	    rc = pj_ioqueue_poll(ioque, &timeout);
830 #endif
831 	} while (rc>0);
832 
833 	rc = 0;
834     }
835 
836     // Print results
837     if (rc == 0) {
838 	pj_timestamp tzero;
839 	pj_uint32_t usec_delay;
840 
841 	tzero.u32.hi = tzero.u32.lo = 0;
842 	usec_delay = pj_elapsed_usec( &tzero, &t_elapsed);
843 
844 	PJ_LOG(3, (THIS_FILE, "...%10d %15d  % 9d",
845 	           bufsize, inactive_sock_count, usec_delay));
846 
847     } else {
848 	PJ_LOG(2, (THIS_FILE, "...ERROR rc=%d (buf:%d, fds:%d)",
849 			      rc, bufsize, inactive_sock_count+2));
850     }
851 
852     // Cleaning up.
853     for (i=inactive_sock_count-1; i>=0; --i) {
854 	pj_ioqueue_unregister(keys[i]);
855     }
856 
857     pj_ioqueue_unregister(skey);
858     pj_ioqueue_unregister(ckey);
859 
860 
861     pj_ioqueue_destroy(ioque);
862     pj_pool_release( pool);
863     return rc;
864 
865 on_error:
866     PJ_LOG(1,(THIS_FILE, "...ERROR: %s",
867 	      pj_strerror(pj_get_netos_error(), errbuf, sizeof(errbuf))));
868     if (ssock)
869 	pj_sock_close(ssock);
870     if (csock)
871 	pj_sock_close(csock);
872     for (i=0; i<inactive_sock_count && inactive_sock &&
873 	      inactive_sock[i]!=PJ_INVALID_SOCKET; ++i)
874     {
875 	pj_sock_close(inactive_sock[i]);
876     }
877     if (ioque != NULL)
878 	pj_ioqueue_destroy(ioque);
879     pj_pool_release( pool);
880     return -1;
881 }
882 
udp_ioqueue_test_imp(pj_bool_t allow_concur)883 static int udp_ioqueue_test_imp(pj_bool_t allow_concur)
884 {
885     int status;
886     int bufsize, sock_count;
887 
888     PJ_LOG(3,(THIS_FILE, "..testing with concurency=%d", allow_concur));
889 
890     //goto pass1;
891 
892     PJ_LOG(3, (THIS_FILE, "...compliance test (%s)", pj_ioqueue_name()));
893     if ((status=compliance_test(allow_concur)) != 0) {
894 	return status;
895     }
896     PJ_LOG(3, (THIS_FILE, "....compliance test ok"));
897 
898 
899     PJ_LOG(3, (THIS_FILE, "...unregister test (%s)", pj_ioqueue_name()));
900     if ((status=unregister_test(allow_concur)) != 0) {
901 	return status;
902     }
903     PJ_LOG(3, (THIS_FILE, "....unregister test ok"));
904 
905     if ((status=many_handles_test(allow_concur)) != 0) {
906 	return status;
907     }
908 
909     //return 0;
910 
911     PJ_LOG(4, (THIS_FILE, "...benchmarking different buffer size:"));
912     PJ_LOG(4, (THIS_FILE, "... note: buf=bytes sent, fds=# of fds, "
913 			  "elapsed=in timer ticks"));
914 
915 //pass1:
916     PJ_LOG(3, (THIS_FILE, "...Benchmarking poll times for %s:", pj_ioqueue_name()));
917     PJ_LOG(3, (THIS_FILE, "...====================================="));
918     PJ_LOG(3, (THIS_FILE, "...Buf.size   #inactive-socks  Time/poll"));
919     PJ_LOG(3, (THIS_FILE, "... (bytes)                    (nanosec)"));
920     PJ_LOG(3, (THIS_FILE, "...====================================="));
921 
922     //goto pass2;
923 
924     for (bufsize=BUF_MIN_SIZE; bufsize <= BUF_MAX_SIZE; bufsize *= 2) {
925 	if ((status=bench_test(allow_concur, bufsize, SOCK_INACTIVE_MIN)) != 0)
926 	    return status;
927     }
928 //pass2:
929     bufsize = 512;
930     for (sock_count=SOCK_INACTIVE_MIN+2;
931 	 sock_count<=SOCK_INACTIVE_MAX+2;
932 	 sock_count *= 2)
933     {
934 	//PJ_LOG(3,(THIS_FILE, "...testing with %d fds", sock_count));
935 	if ((status=bench_test(allow_concur, bufsize, sock_count-2)) != 0)
936 	    return status;
937     }
938     return 0;
939 }
940 
udp_ioqueue_test()941 int udp_ioqueue_test()
942 {
943     int rc;
944 
945     rc = udp_ioqueue_test_imp(PJ_TRUE);
946     if (rc != 0)
947 	return rc;
948 
949     rc = udp_ioqueue_test_imp(PJ_FALSE);
950     if (rc != 0)
951 	return rc;
952 
953     return 0;
954 }
955 
956 #else
957 /* To prevent warning about "translation unit is empty"
958  * when this test is disabled.
959  */
960 int dummy_uiq_udp;
961 #endif	/* INCLUDE_UDP_IOQUEUE_TEST */
962 
963 
964