Brian Waters | 13d9601 | 2017-12-08 16:53:31 -0600 | [diff] [blame] | 1 | /********************************************************************************************************* |
| 2 | * Software License Agreement (BSD License) * |
| 3 | * Author: Sebastien Decugis <sdecugis@freediameter.net> * |
| 4 | * * |
| 5 | * Copyright (c) 2013, WIDE Project and NICT * |
| 6 | * All rights reserved. * |
| 7 | * * |
| 8 | * Redistribution and use of this software in source and binary forms, with or without modification, are * |
| 9 | * permitted provided that the following conditions are met: * |
| 10 | * * |
| 11 | * * Redistributions of source code must retain the above * |
| 12 | * copyright notice, this list of conditions and the * |
| 13 | * following disclaimer. * |
| 14 | * * |
| 15 | * * Redistributions in binary form must reproduce the above * |
| 16 | * copyright notice, this list of conditions and the * |
| 17 | * following disclaimer in the documentation and/or other * |
| 18 | * materials provided with the distribution. * |
| 19 | * * |
| 20 | * * Neither the name of the WIDE Project or NICT nor the * |
| 21 | * names of its contributors may be used to endorse or * |
| 22 | * promote products derived from this software without * |
| 23 | * specific prior written permission of WIDE Project and * |
| 24 | * NICT. * |
| 25 | * * |
| 26 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED * |
| 27 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A * |
| 28 | * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR * |
| 29 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * |
| 30 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * |
| 31 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR * |
| 32 | * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * |
| 33 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * |
| 34 | *********************************************************************************************************/ |
| 35 | |
| 36 | /* |
| 37 | * Test application for freeDiameter. |
| 38 | */ |
| 39 | |
| 40 | #include "test_app.h" |
| 41 | |
| 42 | /* Initialize the configuration */ |
| 43 | struct ta_conf * ta_conf = NULL; |
| 44 | static struct ta_conf _conf; |
| 45 | static pthread_t ta_stats_th = (pthread_t)NULL; |
| 46 | |
| 47 | static int ta_conf_init(void) |
| 48 | { |
| 49 | ta_conf = &_conf; |
| 50 | memset(ta_conf, 0, sizeof(struct ta_conf)); |
| 51 | |
| 52 | /* Set the default values */ |
| 53 | ta_conf->vendor_id = 999999; /* Dummy value */ |
| 54 | ta_conf->appli_id = 0xffffff; /* dummy value */ |
| 55 | ta_conf->cmd_id = 0xfffffe; /* Experimental */ |
| 56 | ta_conf->avp_id = 0xffffff; /* dummy value */ |
| 57 | ta_conf->long_avp_len = 5000; |
| 58 | ta_conf->mode = MODE_SERV | MODE_CLI; |
| 59 | ta_conf->dest_realm = strdup(fd_g_config->cnf_diamrlm); |
| 60 | ta_conf->dest_host = NULL; |
| 61 | ta_conf->signal = TEST_APP_DEFAULT_SIGNAL; |
| 62 | ta_conf->bench_concur = 100; |
| 63 | ta_conf->bench_duration = 10; |
| 64 | |
| 65 | /* Initialize the mutex */ |
| 66 | CHECK_POSIX( pthread_mutex_init(&ta_conf->stats_lock, NULL) ); |
| 67 | |
| 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | static void ta_conf_dump(void) |
| 72 | { |
| 73 | if (!TRACE_BOOL(INFO)) |
| 74 | return; |
| 75 | fd_log_debug( "------- app_test configuration dump: ---------"); |
| 76 | fd_log_debug( " Vendor Id .......... : %u", ta_conf->vendor_id); |
| 77 | fd_log_debug( " Application Id ..... : %u", ta_conf->appli_id); |
| 78 | fd_log_debug( " Command Id ......... : %u", ta_conf->cmd_id); |
| 79 | fd_log_debug( " AVP Id ............. : %u", ta_conf->avp_id); |
| 80 | fd_log_debug( " Long AVP Id ........ : %u", ta_conf->long_avp_id); |
| 81 | fd_log_debug( " Long AVP len ....... : %zu", ta_conf->long_avp_len); |
| 82 | fd_log_debug( " Mode ............... : %s%s%s", ta_conf->mode & MODE_SERV ? "Serv" : "", ta_conf->mode & MODE_CLI ? "Cli" : "", ta_conf->mode & MODE_BENCH ? " (Benchmark)" : ""); |
| 83 | fd_log_debug( " Destination Realm .. : %s", ta_conf->dest_realm ?: "- none -"); |
| 84 | fd_log_debug( " Destination Host ... : %s", ta_conf->dest_host ?: "- none -"); |
| 85 | fd_log_debug( " Signal ............. : %i", ta_conf->signal); |
| 86 | fd_log_debug( "------- /app_test configuration dump ---------"); |
| 87 | } |
| 88 | |
| 89 | /* Function to display statistics periodically */ |
| 90 | static void * ta_stats(void * arg) { |
| 91 | |
| 92 | struct timespec start, now; |
| 93 | struct ta_stats copy; |
| 94 | |
| 95 | /* Get the start time */ |
| 96 | CHECK_SYS_DO( clock_gettime(CLOCK_REALTIME, &start), ); |
| 97 | |
| 98 | /* Now, loop until canceled */ |
| 99 | while (1) { |
| 100 | /* Display statistics every XX seconds */ |
| 101 | sleep(ta_conf->bench_duration + 3); |
| 102 | |
| 103 | /* Now, get the current stats */ |
| 104 | CHECK_POSIX_DO( pthread_mutex_lock(&ta_conf->stats_lock), ); |
| 105 | memcpy(©, &ta_conf->stats, sizeof(struct ta_stats)); |
| 106 | CHECK_POSIX_DO( pthread_mutex_unlock(&ta_conf->stats_lock), ); |
| 107 | |
| 108 | /* Get the current execution time */ |
| 109 | CHECK_SYS_DO( clock_gettime(CLOCK_REALTIME, &now), ); |
| 110 | |
| 111 | /* Now, display everything */ |
| 112 | fd_log_debug( "------- app_test statistics ---------"); |
| 113 | if (now.tv_nsec >= start.tv_nsec) { |
| 114 | fd_log_debug( " Executing for: %d.%06ld sec", |
| 115 | (int)(now.tv_sec - start.tv_sec), |
| 116 | (long)(now.tv_nsec - start.tv_nsec) / 1000); |
| 117 | } else { |
| 118 | fd_log_debug( " Executing for: %d.%06ld sec", |
| 119 | (int)(now.tv_sec - 1 - start.tv_sec), |
| 120 | (long)(now.tv_nsec + 1000000000 - start.tv_nsec) / 1000); |
| 121 | } |
| 122 | |
| 123 | if (ta_conf->mode & MODE_SERV) { |
| 124 | fd_log_debug( " Server: %llu message(s) echoed", copy.nb_echoed); |
| 125 | } |
| 126 | if (ta_conf->mode & MODE_CLI) { |
| 127 | fd_log_debug( " Client:"); |
| 128 | fd_log_debug( " %llu message(s) sent", copy.nb_sent); |
| 129 | fd_log_debug( " %llu error(s) received", copy.nb_errs); |
| 130 | fd_log_debug( " %llu answer(s) received", copy.nb_recv); |
| 131 | fd_log_debug( " fastest: %ld.%06ld sec.", copy.shortest / 1000000, copy.shortest % 1000000); |
| 132 | fd_log_debug( " slowest: %ld.%06ld sec.", copy.longest / 1000000, copy.longest % 1000000); |
| 133 | fd_log_debug( " Average: %ld.%06ld sec.", copy.avg / 1000000, copy.avg % 1000000); |
| 134 | } |
| 135 | fd_log_debug( "-------------------------------------"); |
| 136 | } |
| 137 | |
| 138 | return NULL; /* never called */ |
| 139 | } |
| 140 | |
| 141 | static struct fd_hook_hdl * hookhdl[2] = { NULL, NULL }; |
| 142 | static void ta_hook_cb_silent(enum fd_hook_type type, struct msg * msg, struct peer_hdr * peer, void * other, struct fd_hook_permsgdata *pmd, void * regdata) { |
| 143 | } |
| 144 | static void ta_hook_cb_oneline(enum fd_hook_type type, struct msg * msg, struct peer_hdr * peer, void * other, struct fd_hook_permsgdata *pmd, void * regdata) { |
| 145 | char * buf = NULL; |
| 146 | size_t len; |
| 147 | |
| 148 | CHECK_MALLOC_DO( fd_msg_dump_summary(&buf, &len, NULL, msg, NULL, 0, 0), |
| 149 | { LOG_E("Error while dumping a message"); return; } ); |
| 150 | |
| 151 | LOG_N("{%d} %s: %s", type, (char *)other ?:"<nil>", buf ?:"<nil>"); |
| 152 | |
| 153 | free(buf); |
| 154 | } |
| 155 | |
| 156 | |
| 157 | /* entry point */ |
| 158 | static int ta_entry(char * conffile) |
| 159 | { |
| 160 | TRACE_ENTRY("%p", conffile); |
| 161 | |
| 162 | /* Initialize configuration */ |
| 163 | CHECK_FCT( ta_conf_init() ); |
| 164 | |
| 165 | /* Parse configuration file */ |
| 166 | if (conffile != NULL) { |
| 167 | CHECK_FCT( ta_conf_handle(conffile) ); |
| 168 | } |
| 169 | |
| 170 | TRACE_DEBUG(INFO, "Extension Test_App initialized with configuration: '%s'", conffile); |
| 171 | ta_conf_dump(); |
| 172 | |
| 173 | /* Install objects definitions for this test application */ |
| 174 | CHECK_FCT( ta_dict_init() ); |
| 175 | |
| 176 | /* Install the handlers for incoming messages */ |
| 177 | if (ta_conf->mode & MODE_SERV) { |
| 178 | CHECK_FCT( ta_serv_init() ); |
| 179 | } |
| 180 | |
| 181 | /* Start the signal handler thread */ |
| 182 | if (ta_conf->mode & MODE_CLI) { |
| 183 | if (ta_conf->mode & MODE_BENCH) { |
| 184 | CHECK_FCT( ta_bench_init() ); |
| 185 | } else { |
| 186 | CHECK_FCT( ta_cli_init() ); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | /* Advertise the support for the test application in the peer */ |
| 191 | CHECK_FCT( fd_disp_app_support ( ta_appli, ta_vendor, 1, 0 ) ); |
| 192 | |
| 193 | if (ta_conf->mode & MODE_BENCH) { |
| 194 | /* Register an empty hook to disable the default handling */ |
| 195 | CHECK_FCT( fd_hook_register( HOOK_MASK( HOOK_DATA_RECEIVED, HOOK_MESSAGE_RECEIVED, HOOK_MESSAGE_LOCAL, HOOK_MESSAGE_SENT, HOOK_MESSAGE_ROUTING_FORWARD, HOOK_MESSAGE_ROUTING_LOCAL ), |
| 196 | ta_hook_cb_silent, NULL, NULL, &hookhdl[0]) ); |
| 197 | CHECK_FCT( fd_hook_register( HOOK_MASK( HOOK_MESSAGE_ROUTING_ERROR, HOOK_MESSAGE_DROPPED ), |
| 198 | ta_hook_cb_oneline, NULL, NULL, &hookhdl[1]) ); |
| 199 | |
| 200 | } |
| 201 | |
| 202 | /* Start the statistics thread */ |
| 203 | CHECK_POSIX( pthread_create(&ta_stats_th, NULL, ta_stats, NULL) ); |
| 204 | |
| 205 | return 0; |
| 206 | } |
| 207 | |
| 208 | /* Unload */ |
| 209 | void fd_ext_fini(void) |
| 210 | { |
| 211 | if (ta_conf->mode & MODE_CLI) |
| 212 | ta_cli_fini(); |
| 213 | if (ta_conf->mode & MODE_SERV) |
| 214 | ta_serv_fini(); |
| 215 | if (hookhdl[0]) |
| 216 | fd_hook_unregister( hookhdl[0] ); |
| 217 | if (hookhdl[1]) |
| 218 | fd_hook_unregister( hookhdl[1] ); |
| 219 | CHECK_FCT_DO( fd_thr_term(&ta_stats_th), ); |
| 220 | CHECK_POSIX_DO( pthread_mutex_destroy(&ta_conf->stats_lock), ); |
| 221 | } |
| 222 | |
| 223 | EXTENSION_ENTRY("test_app", ta_entry); |