comparison extensions/test_ccload/test_ccload.c @ 1374:e99e8f20b346

Rename loadtest_cc to test_ccload to match naming convention.
author Thomas Klausner <tk@giga.or.at>
date Thu, 13 Jun 2019 11:26:42 +0200
parents extensions/loadtest_cc/loadtest_cc.c@6b1a2405cebb
children 2bd83cd4d2b2
comparison
equal deleted inserted replaced
1373:a4dd31276f17 1374:e99e8f20b346
1 /**********************************************************************************************************
2 * Software License Agreement(BSD License) *
3 * Author: Thomas Klausner <tk@giga.or.at> *
4 * *
5 * Copyright(c) 2019, Thomas Klausner *
6 * All rights reserved. *
7 * *
8 * Written under contract by nfotex IT GmbH, http://nfotex.com/ *
9 * *
10 * Redistribution and use of this software in source and binary forms, with or without modification, are *
11 * permitted provided that the following conditions are met: *
12 * *
13 * * Redistributions of source code must retain the above *
14 * copyright notice, this list of conditions and the *
15 * following disclaimer. *
16 * *
17 * * Redistributions in binary form must reproduce the above *
18 * copyright notice, this list of conditions and the *
19 * following disclaimer in the documentation and/or other *
20 * materials provided with the distribution. *
21 * *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED *
23 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
24 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR *
25 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES(INCLUDING, BUT NOT *
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS *
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR *
28 * TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF *
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
30 **********************************************************************************************************/
31
32 /* This extension waits for a signal (SIGUSR2). When it gets it, it
33 * generates messages as quickly as possible to the configured target
34 * host; a second SIGUSR2 signal will stop this */
35
36 #include <freeDiameter/extension.h>
37
38 #include <pthread.h>
39 #include <signal.h>
40
41 #define MODULE_NAME "test_ccload"
42
43 static pthread_t gen_thr = (pthread_t)NULL;
44 struct disp_hdl *ccr_local_hdl;
45 struct fd_rt_fwd_hdl *ccr_fwd_hdl;
46 volatile int do_generate = 0;
47
48 const char *target = NULL;
49
50 struct dict_object * aai_avp_do; /* cache the Auth-Application-Id dictionary object */
51 struct dict_object * crn_avp_do; /* cache the CC-Request-Number dictionary object */
52 struct dict_object * crt_avp_do; /* cache the CC-Request-Type dictionary object */
53 struct dict_object * dh_avp_do; /* cache the Destination-Host dictionary object */
54 struct dict_object * dr_avp_do; /* cache the Destination-Realm dictionary object */
55 struct dict_object * rc_avp_do; /* cache the Result-Code dictionary object */
56 struct dict_object * sci_avp_do; /* cache the Service-Context-Id dictionary object */
57 struct dict_object * si_avp_do; /* cache the Session-Id dictionary object */
58 struct dict_object * pi_avp_do; /* cache the Proxy-Info dictionary object */
59 struct dict_object * ph_avp_do; /* cache the Proxy-Host dictionary object */
60 struct dict_object * ps_avp_do; /* cache the Proxy-State dictionary object */
61
62 struct statistics {
63 uint64_t sent;
64 uint64_t success;
65 uint64_t error;
66 time_t first;
67 time_t last;
68 } statistics;
69
70 void print_statistics(void) {
71 uint64_t missing;
72
73 if (statistics.first == 0 || statistics.last == 0 || statistics.last == statistics.first) {
74 return;
75 }
76
77 missing = statistics.sent - statistics.error - statistics.success;
78
79 fd_log_error("%s: %lld CCR messages sent in %llds (%.2f messages/second), %lld success (%.2f%%), %lld errors (%.2f%%), %lld missing (%.2f%%)",
80 fd_g_config->cnf_diamid,
81 (long long)statistics.sent, (long long)(statistics.last-statistics.first), (float)statistics.sent / (statistics.last-statistics.first),
82 (long long)statistics.success,
83 100*(float)statistics.success/statistics.sent, (long long)statistics.error, 100*(float)statistics.error/statistics.sent,
84 missing, 100*(float)missing/statistics.sent);
85 }
86
87 static int handle_message(struct msg **msg) {
88 struct msg_hdr *hdr = NULL;
89 struct avp_hdr *ahdr = NULL;
90 struct avp *rc;
91
92 if (msg == NULL) {
93 fd_log_error("[%s] NULL CCA message", MODULE_NAME);
94 return -1;
95 }
96
97 CHECK_FCT(fd_msg_hdr(*msg, &hdr));
98 /* handle CCAs only */
99 if ((hdr->msg_code != 272) || (hdr->msg_flags & CMD_FLAG_REQUEST)) {
100 fd_log_error("[%s] invalid message type (type %d, flags 0x%x)", MODULE_NAME, hdr->msg_code, hdr->msg_flags);
101 return -1;
102 }
103
104 /* Answer received, check it */
105 if (fd_msg_search_avp(*msg, rc_avp_do, &rc) < 0 || rc == NULL) {
106 fd_log_error("[%s] Result-Code not found in CCA", MODULE_NAME);
107 return -1;
108 }
109
110 if (fd_msg_avp_hdr(rc, &ahdr) < 0) {
111 fd_log_error("[%s] error parsing Result-Code in CCA", MODULE_NAME);
112 return -1;
113 }
114 fd_log_debug("Credit-Control-Answer with Result-Code %d received", ahdr->avp_value->i32);
115 switch (ahdr->avp_value->i32/1000) {
116 case 2:
117 statistics.success++;
118 break;
119 default:
120 statistics.error++;
121 break;
122 }
123
124 return 0;
125 }
126
127 static int ccr_local_handler(struct msg ** msg, struct avp * avp, struct session * sess, void * data, enum disp_action * act)
128 {
129 fd_log_debug("[%s] CCR local handler called", MODULE_NAME);
130
131 if (handle_message(msg) < 0) {
132 fd_log_error("dropping message");
133 CHECK_FCT(fd_msg_free(*msg));
134 *msg = NULL;
135 return 0;
136 }
137
138 CHECK_FCT(fd_msg_free(*msg));
139 *msg = NULL;
140 return 0;
141 }
142
143 static int ccr_fwd_handler(void *cb_data, struct msg **msg)
144 {
145 fd_log_debug("[%s] CCR FWD handler called", MODULE_NAME);
146
147 if (handle_message(msg) < 0) {
148 return 0;
149 }
150
151 return 0;
152 }
153
154 /* create message to send */
155 struct msg *create_message(const char *destination)
156 {
157 struct dict_object *command;
158 struct msg *msg;
159 struct avp *avp, *avp1;
160 union avp_value val;
161 struct msg_hdr *msg_hdr;
162 const char *realm;
163 char session_id[800];
164 const char *service_context_id = "version2.clci.ipc@vodafone.com";
165 const char *proxy_host = "Dummy-Proxy-Host-to-Increase-Package-Size";
166 const char *proxy_state = "This is just data to increase the package size\nXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
167
168 if (fd_dict_search(fd_g_config->cnf_dict, DICT_COMMAND, CMD_BY_NAME, "Credit-Control-Request", &command, ENOENT) != 0) {
169 fd_log_error("can't find template for 'Credit-Control-Request'");
170 return NULL;
171 }
172
173 if (fd_msg_new(command, MSGFL_ALLOC_ETEID, &msg) != 0) {
174 fd_log_error("can't create new 'Credit-Control-Request' message");
175 return NULL;
176 }
177
178 /* Application Id in header needs to be set to for since this Credit-Control-Request is for Diameter Credit Control */
179 if (fd_msg_hdr(msg, &msg_hdr) != 0) {
180 fd_log_error("can't get message header for 'Credit-Control-Request' message");
181 fd_msg_free(msg);
182 return NULL;
183 }
184 msg_hdr->msg_appl = 4;
185
186 if (fd_msg_add_origin(msg, 0) != 0) {
187 fd_log_error("can't set Origin for 'Credit-Control-Request' message");
188 fd_msg_free(msg);
189 return NULL;
190 }
191
192 /* Destination-Host */
193 fd_msg_avp_new(dh_avp_do, 0, &avp);
194 memset(&val, 0, sizeof(val));
195 val.os.data = (uint8_t *)target;
196 val.os.len = strlen(target);
197 if (fd_msg_avp_setvalue(avp, &val) != 0) {
198 fd_msg_free(msg);
199 fd_log_error("can't set value for 'Destination-Host' for 'Credit-Control-Request' message");
200 return NULL;
201 }
202 fd_msg_avp_add(msg, MSG_BRW_LAST_CHILD, avp);
203
204 if ((realm = strchr(target, '.')) == NULL) {
205 fd_msg_free(msg);
206 fd_log_error("can't extract realm from host '%s'", target);
207 return NULL;
208 }
209 /* skip dot */
210 realm++;
211 /* Destination-Realm */
212 fd_msg_avp_new(dr_avp_do, 0, &avp);
213 memset(&val, 0, sizeof(val));
214 val.os.data = (uint8_t *)realm;
215 val.os.len = strlen(realm);
216 if (fd_msg_avp_setvalue(avp, &val) != 0) {
217 fd_msg_free(msg);
218 fd_log_error("can't set value for 'Destination-Realm' for 'Credit-Control-Request' message");
219 return NULL;
220 }
221 fd_msg_avp_add(msg, MSG_BRW_LAST_CHILD, avp);
222
223 /* Session-Id */
224 snprintf(session_id, sizeof(session_id), "session %ld", random());
225 fd_msg_avp_new(si_avp_do, 0, &avp);
226 memset(&val, 0, sizeof(val));
227 val.os.data = (uint8_t *)session_id;
228 val.os.len = strlen(session_id);
229 if (fd_msg_avp_setvalue(avp, &val) != 0) {
230 fd_msg_free(msg);
231 fd_log_error("can't set value for 'Session-Id' for 'Credit-Control-Request' message");
232 return NULL;
233 }
234 fd_msg_avp_add(msg, MSG_BRW_FIRST_CHILD, avp);
235
236 /* Auth-Application-Id */
237 fd_msg_avp_new(aai_avp_do, 0, &avp);
238 memset(&val, 0, sizeof(val));
239 val.i32 = 4;
240 if (fd_msg_avp_setvalue(avp, &val) != 0) {
241 fd_msg_free(msg);
242 fd_log_error("can't set value for 'Auth-Application-Id' for 'Credit-Control-Request' message");
243 return NULL;
244 }
245 fd_msg_avp_add(msg, MSG_BRW_LAST_CHILD, avp);
246
247 /* Service-Context-Id */
248 fd_msg_avp_new(sci_avp_do, 0, &avp);
249 memset(&val, 0, sizeof(val));
250 val.os.data = (uint8_t *)service_context_id;
251 val.os.len = strlen(service_context_id);
252 if (fd_msg_avp_setvalue(avp, &val) != 0) {
253 fd_msg_free(msg);
254 fd_log_error("can't set value for 'Service-Context-Id' for 'Credit-Control-Request' message");
255 return NULL;
256 }
257 fd_msg_avp_add(msg, MSG_BRW_LAST_CHILD, avp);
258
259 /* CC-Request-Type */
260 fd_msg_avp_new(crt_avp_do, 0, &avp);
261 memset(&val, 0, sizeof(val));
262 val.i32 = 1; /* Initial */
263 if (fd_msg_avp_setvalue(avp, &val) != 0) {
264 fd_msg_free(msg);
265 fd_log_error("can't set value for 'CC-Request-Type' for 'Credit-Control-Request' message");
266 return NULL;
267 }
268 fd_msg_avp_add(msg, MSG_BRW_LAST_CHILD, avp);
269
270 /* CC-Request-Number */
271 fd_msg_avp_new(crn_avp_do, 0, &avp);
272 memset(&val, 0, sizeof(val));
273 val.i32 = 1;
274 if (fd_msg_avp_setvalue(avp, &val) != 0) {
275 fd_msg_free(msg);
276 fd_log_error("can't set value for 'CC-Request-Number' for 'Credit-Control-Request' message");
277 return NULL;
278 }
279 fd_msg_avp_add(msg, MSG_BRW_LAST_CHILD, avp);
280
281 /* Proxy-Info */
282 fd_msg_avp_new(pi_avp_do, 0, &avp);
283 /* Proxy-Host */
284 fd_msg_avp_new(ph_avp_do, 0, &avp1);
285 memset(&val, 0, sizeof(val));
286 val.os.data = (uint8_t *)proxy_host;
287 val.os.len = strlen(proxy_host);
288 if (fd_msg_avp_setvalue(avp1, &val) != 0) {
289 fd_msg_free(msg);
290 fd_log_error("can't set value for 'Proxy-Host' for 'Credit-Control-Request' message");
291 return NULL;
292 }
293 fd_msg_avp_add(avp, MSG_BRW_LAST_CHILD, avp1);
294 /* Proxy-State */
295 fd_msg_avp_new(ps_avp_do, 0, &avp1);
296 memset(&val, 0, sizeof(val));
297 val.os.data = (uint8_t *)proxy_state;
298 val.os.len = strlen(proxy_state);
299 if (fd_msg_avp_setvalue(avp1, &val) != 0) {
300 fd_msg_free(msg);
301 fd_log_error("can't set value for 'Proxy-State' for 'Credit-Control-Request' message");
302 return NULL;
303 }
304 fd_msg_avp_add(avp, MSG_BRW_LAST_CHILD, avp1);
305 fd_msg_avp_add(msg, MSG_BRW_LAST_CHILD, avp);
306
307 return msg;
308 }
309
310 /* The thread that handles expired entries cleanup. */
311 void * gen_thr_fct(void * arg)
312 {
313 struct msg *msg;
314 fd_log_threadname ( "Loadtest/Generator" );
315
316 do {
317 if (do_generate) {
318 time_t now;
319 if (statistics.first == 0) {
320 statistics.first = time(NULL);
321 }
322 msg = create_message(target);
323 fd_msg_send(&msg, NULL, NULL);
324 fd_log_debug("[%s] sent message", MODULE_NAME);
325 now = time(NULL);
326 if (statistics.last != now) {
327 print_statistics();
328 }
329 statistics.last = time(NULL);
330 statistics.sent++;
331 } else {
332 sleep(1);
333 }
334 } while (1);
335 }
336
337 /* signal handler */
338 static void sig_hdlr(void)
339 {
340 if (do_generate) {
341 do_generate = 0;
342 } else {
343 do_generate = 1;
344 }
345 fd_log_notice("%s: switched generation of CCRs %s", MODULE_NAME, do_generate ? "on" : "off");
346 }
347
348 /* entry hook: register callback */
349 static int cc_entry(char * conffile)
350 {
351 struct disp_when data;
352
353 /* initialize random number generator for session IDs */
354 srandom(time(NULL) | (unsigned int)getpid());
355 if ((target = conffile) == NULL) {
356 fd_log_error("invalid conffile");
357 return EINVAL;
358 }
359
360 memset(&data, 0, sizeof(data));
361
362 /* Advertise the support for the Diameter Credit Control application in the peer */
363 CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_APPLICATION, APPLICATION_BY_NAME, "Diameter Credit Control Application", &data.app, ENOENT) );
364 CHECK_FCT( fd_disp_app_support ( data.app, NULL, 1, 0 ) );
365
366 /* the handling of requests is different if it might be locally handled or not */
367 /* for possibly locally handled requests, fd_rt_fwd_register is not called, so we need to add handlers for both cases */
368 /* register forward handler for CCR -- needs to look at all requests */
369 CHECK_FCT(fd_rt_fwd_register(ccr_fwd_handler, NULL, RT_FWD_REQ, &ccr_fwd_hdl));
370 /* register local handler for CCR - if not Destination-Host is set, or the local host is used */
371 memset(&data, 0, sizeof(data));
372 CHECK_FCT(fd_dict_search(fd_g_config->cnf_dict, DICT_COMMAND, CMD_BY_NAME, "Credit-Control-Answer", &data.command, ENOENT));
373 CHECK_FCT(fd_disp_register(ccr_local_handler, DISP_HOW_CC, &data, NULL, &ccr_local_hdl));
374
375 CHECK_FCT_DO(fd_dict_search(fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Auth-Application-Id", &aai_avp_do, ENOENT),
376 { LOG_E("Unable to find 'Auth-Application-Id' AVP in the loaded dictionaries."); });
377 CHECK_FCT_DO(fd_dict_search(fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "CC-Request-Number", &crn_avp_do, ENOENT),
378 { LOG_E("Unable to find 'CC-Request-Number' AVP in the loaded dictionaries."); });
379 CHECK_FCT_DO(fd_dict_search(fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "CC-Request-Type", &crt_avp_do, ENOENT),
380
381 { LOG_E("Unable to find 'CC-Request-Type' AVP in the loaded dictionaries."); });
382 CHECK_FCT_DO(fd_dict_search(fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Destination-Host", &dh_avp_do, ENOENT),
383 { LOG_E("Unable to find 'Destination-Host' AVP in the loaded dictionaries."); });
384 CHECK_FCT_DO(fd_dict_search(fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Destination-Realm", &dr_avp_do, ENOENT),
385 { LOG_E("Unable to find 'Destination-Realm' AVP in the loaded dictionaries."); });
386 CHECK_FCT_DO(fd_dict_search(fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Result-Code", &rc_avp_do, ENOENT),
387 { LOG_E("Unable to find 'Result-Code' AVP in the loaded dictionaries."); });
388 CHECK_FCT_DO(fd_dict_search(fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Session-Id", &si_avp_do, ENOENT),
389 { LOG_E("Unable to find 'Session-Id' AVP in the loaded dictionaries."); });
390 CHECK_FCT_DO(fd_dict_search(fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Service-Context-Id", &sci_avp_do, ENOENT),
391 { LOG_E("Unable to find 'Service-Context-Id' AVP in the loaded dictionaries."); });
392 CHECK_FCT_DO(fd_dict_search(fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Proxy-Info", &pi_avp_do, ENOENT),
393 { LOG_E("Unable to find 'Proxy-Info' AVP in the loaded dictionaries."); });
394 CHECK_FCT_DO(fd_dict_search(fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Proxy-Host", &ph_avp_do, ENOENT),
395 { LOG_E("Unable to find 'Proxy-Host' AVP in the loaded dictionaries."); });
396 CHECK_FCT_DO(fd_dict_search(fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Proxy-State", &ps_avp_do, ENOENT),
397 { LOG_E("Unable to find 'Proxy-State' AVP in the loaded dictionaries."); });
398
399 /* Start the generator thread */
400 CHECK_POSIX( pthread_create( &gen_thr, NULL, gen_thr_fct, NULL ) );
401
402 /* Register generator callback */
403 CHECK_FCT(fd_event_trig_regcb(SIGUSR2, MODULE_NAME, sig_hdlr));
404
405 return 0;
406 }
407
408 /* And terminate it */
409 void fd_ext_fini(void)
410 {
411 /* stop sending */
412 do_generate = 0;
413
414 /* Stop the expiry thread */
415 CHECK_FCT_DO( fd_thr_term(&gen_thr), );
416
417 /* Unregister the callbacks */
418 if (ccr_local_hdl) {
419 CHECK_FCT_DO( fd_disp_unregister(&ccr_local_hdl, NULL), );
420 ccr_local_hdl = NULL;
421 }
422
423 print_statistics();
424
425 return;
426 }
427
428
429 EXTENSION_ENTRY(MODULE_NAME, cc_entry);
"Welcome to our mercurial repository"