cpg_test_agent.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. /*
  2. * Copyright (c) 2010 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Angus Salkeld (asalkeld@redhat.com)
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <errno.h>
  35. #include <unistd.h>
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <assert.h>
  39. #include <string.h>
  40. #include <sys/types.h>
  41. #include <sys/socket.h>
  42. #include <netinet/in.h>
  43. #include <arpa/inet.h>
  44. #include <netdb.h>
  45. #include <syslog.h>
  46. #include <poll.h>
  47. #include <unistd.h>
  48. #include <fcntl.h>
  49. #include <corosync/totem/coropoll.h>
  50. #include <corosync/list.h>
  51. #include <corosync/cpg.h>
  52. #include "../../exec/crypto.h"
  53. #include "common_test_agent.h"
  54. typedef enum {
  55. MSG_OK,
  56. MSG_NODEID_ERR,
  57. MSG_PID_ERR,
  58. MSG_SEQ_ERR,
  59. MSG_SIZE_ERR,
  60. MSG_SHA1_ERR,
  61. } msg_status_t;
  62. typedef struct {
  63. uint32_t nodeid;
  64. pid_t pid;
  65. unsigned char sha1[20];
  66. uint32_t seq;
  67. size_t size;
  68. unsigned char payload[0];
  69. } msg_t;
  70. #define LOG_STR_SIZE 256
  71. typedef struct {
  72. char log[LOG_STR_SIZE];
  73. struct list_head list;
  74. } log_entry_t;
  75. static char big_and_buf[HOW_BIG_AND_BUF];
  76. static int32_t record_config_events_g = 0;
  77. static int32_t record_messages_g = 0;
  78. static cpg_handle_t cpg_handle = 0;
  79. static int32_t cpg_fd = -1;
  80. static struct list_head config_chg_log_head;
  81. static struct list_head msg_log_head;
  82. static pid_t my_pid;
  83. static uint32_t my_nodeid;
  84. static int32_t my_seq;
  85. static int32_t use_zcb = 0;
  86. static int32_t my_msgs_to_send;
  87. static int32_t total_stored_msgs = 0;
  88. static void send_some_more_messages (void * unused);
  89. static char* err_status_string (char * buf, size_t buf_len, msg_status_t status)
  90. {
  91. switch (status) {
  92. case MSG_OK:
  93. strncpy (buf, "OK", buf_len);
  94. break;
  95. case MSG_NODEID_ERR:
  96. strncpy (buf, "NODEID_ERR", buf_len);
  97. break;
  98. case MSG_PID_ERR:
  99. strncpy (buf, "PID_ERR", buf_len);
  100. break;
  101. case MSG_SEQ_ERR:
  102. strncpy (buf, "SEQ_ERR", buf_len);
  103. break;
  104. case MSG_SIZE_ERR:
  105. strncpy (buf, "SIZE_ERR", buf_len);
  106. break;
  107. case MSG_SHA1_ERR:
  108. strncpy (buf, "SHA1_ERR", buf_len);
  109. break;
  110. default:
  111. strncpy (buf, "UNKNOWN_ERR", buf_len);
  112. break;
  113. }
  114. return buf;
  115. }
  116. static void delivery_callback (
  117. cpg_handle_t handle,
  118. const struct cpg_name *groupName,
  119. uint32_t nodeid,
  120. uint32_t pid,
  121. void *msg,
  122. size_t msg_len)
  123. {
  124. log_entry_t *log_pt;
  125. msg_t *msg_pt = (msg_t*)msg;
  126. msg_status_t status = MSG_OK;
  127. char status_buf[20];
  128. unsigned char sha1_compare[20];
  129. hash_state sha1_hash;
  130. if (record_messages_g == 0) {
  131. return;
  132. }
  133. msg_pt->seq = my_seq;
  134. my_seq++;
  135. if (nodeid != msg_pt->nodeid) {
  136. status = MSG_NODEID_ERR;
  137. }
  138. if (pid != msg_pt->pid) {
  139. status = MSG_PID_ERR;
  140. }
  141. if (msg_len != msg_pt->size) {
  142. status = MSG_SIZE_ERR;
  143. }
  144. sha1_init (&sha1_hash);
  145. sha1_process (&sha1_hash, msg_pt->payload, (msg_pt->size - sizeof (msg_t)));
  146. sha1_done (&sha1_hash, sha1_compare);
  147. if (memcmp (sha1_compare, msg_pt->sha1, 20) != 0) {
  148. syslog (LOG_ERR, "%s(); msg seq:%d; incorrect hash",
  149. __func__, msg_pt->seq);
  150. status = MSG_SHA1_ERR;
  151. }
  152. log_pt = malloc (sizeof(log_entry_t));
  153. list_init (&log_pt->list);
  154. snprintf (log_pt->log, LOG_STR_SIZE, "%d:%d:%d:%s;",
  155. msg_pt->nodeid, msg_pt->pid, msg_pt->seq,
  156. err_status_string (status_buf, 20, status));
  157. list_add_tail (&log_pt->list, &msg_log_head);
  158. total_stored_msgs++;
  159. }
  160. static void config_change_callback (
  161. cpg_handle_t handle,
  162. const struct cpg_name *groupName,
  163. const struct cpg_address *member_list, size_t member_list_entries,
  164. const struct cpg_address *left_list, size_t left_list_entries,
  165. const struct cpg_address *joined_list, size_t joined_list_entries)
  166. {
  167. int i;
  168. log_entry_t *log_pt;
  169. /* group_name,ip,pid,join|leave */
  170. for (i = 0; i < left_list_entries; i++) {
  171. syslog (LOG_INFO, "%s(%d) %d:%s left", __func__, record_config_events_g,
  172. left_list[i].nodeid, groupName->value);
  173. if (record_config_events_g > 0) {
  174. log_pt = malloc (sizeof(log_entry_t));
  175. list_init (&log_pt->list);
  176. snprintf (log_pt->log, LOG_STR_SIZE, "%s,%d,%d,left",
  177. groupName->value, left_list[i].nodeid,left_list[i].pid);
  178. list_add_tail(&log_pt->list, &config_chg_log_head);
  179. }
  180. }
  181. for (i = 0; i < joined_list_entries; i++) {
  182. syslog (LOG_INFO, "%s(%d) %d:%s joined", __func__, record_config_events_g,
  183. left_list[i].nodeid, groupName->value);
  184. if (record_config_events_g > 0) {
  185. log_pt = malloc (sizeof(log_entry_t));
  186. list_init (&log_pt->list);
  187. snprintf (log_pt->log, LOG_STR_SIZE, "%s,%d,%d,join",
  188. groupName->value, joined_list[i].nodeid,joined_list[i].pid);
  189. list_add_tail (&log_pt->list, &config_chg_log_head);
  190. }
  191. }
  192. }
  193. static cpg_callbacks_t callbacks = {
  194. .cpg_deliver_fn = delivery_callback,
  195. .cpg_confchg_fn = config_change_callback,
  196. };
  197. static void record_messages (void)
  198. {
  199. record_messages_g = 1;
  200. syslog (LOG_DEBUG,"%s() record:%d", __func__, record_messages_g);
  201. }
  202. static void record_config_events (int sock)
  203. {
  204. char response[100];
  205. record_config_events_g = 1;
  206. syslog (LOG_INFO, "%s() record:%d", __func__, record_config_events_g);
  207. snprintf (response, 100, "%s", OK_STR);
  208. send (sock, response, strlen (response), 0);
  209. }
  210. static void read_config_event (int sock)
  211. {
  212. const char *empty = "None";
  213. struct list_head * list = config_chg_log_head.next;
  214. log_entry_t *entry;
  215. if (list != &config_chg_log_head) {
  216. entry = list_entry (list, log_entry_t, list);
  217. send (sock, entry->log, strlen (entry->log), 0);
  218. list_del (&entry->list);
  219. free (entry);
  220. } else {
  221. syslog (LOG_DEBUG,"%s() no events in list", __func__);
  222. send (sock, empty, strlen (empty), 0);
  223. }
  224. }
  225. static void read_messages (int sock, char* atmost_str)
  226. {
  227. struct list_head * list;
  228. log_entry_t *entry;
  229. int atmost = atoi (atmost_str);
  230. int packed = 0;
  231. if (atmost == 0)
  232. atmost = 1;
  233. if (atmost > (HOW_BIG_AND_BUF / LOG_STR_SIZE))
  234. atmost = (HOW_BIG_AND_BUF / LOG_STR_SIZE);
  235. syslog (LOG_DEBUG, "%s() atmost %d; total_stored_msgs:%d",
  236. __func__, atmost, total_stored_msgs);
  237. big_and_buf[0] = '\0';
  238. for (list = msg_log_head.next;
  239. (!list_empty (&msg_log_head) && packed < atmost); ) {
  240. entry = list_entry (list, log_entry_t, list);
  241. strcat (big_and_buf, entry->log);
  242. packed++;
  243. list = list->next;
  244. list_del (&entry->list);
  245. free (entry);
  246. total_stored_msgs--;
  247. }
  248. syslog (LOG_DEBUG, "%s() sending %d; total_stored_msgs:%d; len:%d",
  249. __func__, packed, total_stored_msgs, (int)strlen (big_and_buf));
  250. if (packed == 0) {
  251. strcpy (big_and_buf, "None");
  252. }
  253. send (sock, big_and_buf, strlen (big_and_buf), 0);
  254. }
  255. static void send_some_more_messages_later (void)
  256. {
  257. poll_timer_handle timer_handle;
  258. cpg_dispatch (cpg_handle, CS_DISPATCH_ALL);
  259. poll_timer_add (
  260. ta_poll_handle_get(),
  261. 300, NULL,
  262. send_some_more_messages,
  263. &timer_handle);
  264. }
  265. static void send_some_more_messages_zcb (void)
  266. {
  267. msg_t *my_msg;
  268. int i;
  269. int send_now;
  270. size_t payload_size;
  271. size_t total_size;
  272. hash_state sha1_hash;
  273. cs_error_t res;
  274. cpg_flow_control_state_t fc_state;
  275. void *zcb_buffer;
  276. if (cpg_fd < 0)
  277. return;
  278. send_now = my_msgs_to_send;
  279. payload_size = (rand() % 100000);
  280. total_size = payload_size + sizeof (msg_t);
  281. cpg_zcb_alloc (cpg_handle, total_size, &zcb_buffer);
  282. my_msg = (msg_t*)zcb_buffer;
  283. //syslog (LOG_DEBUG,"%s() send_now:%d", __func__, send_now);
  284. my_msg->pid = my_pid;
  285. my_msg->nodeid = my_nodeid;
  286. my_msg->size = sizeof (msg_t) + payload_size;
  287. my_msg->seq = 0;
  288. for (i = 0; i < payload_size; i++) {
  289. my_msg->payload[i] = i;
  290. }
  291. sha1_init (&sha1_hash);
  292. sha1_process (&sha1_hash, my_msg->payload, payload_size);
  293. sha1_done (&sha1_hash, my_msg->sha1);
  294. for (i = 0; i < send_now; i++) {
  295. res = cpg_flow_control_state_get (cpg_handle, &fc_state);
  296. if (res == CS_OK && fc_state == CPG_FLOW_CONTROL_ENABLED) {
  297. /* lets do this later */
  298. send_some_more_messages_later ();
  299. syslog (LOG_INFO, "%s() flow control enabled.", __func__);
  300. goto free_buffer;
  301. }
  302. res = cpg_zcb_mcast_joined (cpg_handle, CPG_TYPE_AGREED, zcb_buffer, total_size);
  303. if (res == CS_ERR_TRY_AGAIN) {
  304. /* lets do this later */
  305. send_some_more_messages_later ();
  306. syslog (LOG_INFO, "%s() cpg_mcast_joined() says try again.",
  307. __func__);
  308. goto free_buffer;
  309. } else if (res != CS_OK) {
  310. syslog (LOG_ERR, "%s() -> cpg_mcast_joined error:%d, exiting.",
  311. __func__, res);
  312. exit (-2);
  313. }
  314. my_msgs_to_send--;
  315. }
  316. free_buffer:
  317. cpg_zcb_free (cpg_handle, zcb_buffer);
  318. }
  319. static unsigned char buffer[200000];
  320. static void send_some_more_messages_normal (void)
  321. {
  322. msg_t my_msg;
  323. struct iovec iov[2];
  324. int i;
  325. int send_now;
  326. size_t payload_size;
  327. hash_state sha1_hash;
  328. cs_error_t res;
  329. cpg_flow_control_state_t fc_state;
  330. if (cpg_fd < 0)
  331. return;
  332. send_now = my_msgs_to_send;
  333. //syslog (LOG_DEBUG,"%s() send_now:%d", __func__, send_now);
  334. my_msg.pid = my_pid;
  335. my_msg.nodeid = my_nodeid;
  336. payload_size = (rand() % 100000);
  337. my_msg.size = sizeof (msg_t) + payload_size;
  338. my_msg.seq = 0;
  339. for (i = 0; i < payload_size; i++) {
  340. buffer[i] = i;
  341. }
  342. sha1_init (&sha1_hash);
  343. sha1_process (&sha1_hash, buffer, payload_size);
  344. sha1_done (&sha1_hash, my_msg.sha1);
  345. iov[0].iov_len = sizeof (msg_t);
  346. iov[0].iov_base = &my_msg;
  347. iov[1].iov_len = payload_size;
  348. iov[1].iov_base = buffer;
  349. for (i = 0; i < send_now; i++) {
  350. res = cpg_flow_control_state_get (cpg_handle, &fc_state);
  351. if (res == CS_OK && fc_state == CPG_FLOW_CONTROL_ENABLED) {
  352. /* lets do this later */
  353. send_some_more_messages_later ();
  354. syslog (LOG_INFO, "%s() flow control enabled.", __func__);
  355. return;
  356. }
  357. res = cpg_mcast_joined (cpg_handle, CPG_TYPE_AGREED, iov, 2);
  358. if (res == CS_ERR_TRY_AGAIN) {
  359. /* lets do this later */
  360. send_some_more_messages_later ();
  361. syslog (LOG_INFO, "%s() cpg_mcast_joined() says try again.",
  362. __func__);
  363. return;
  364. } else
  365. if (res != CS_OK) {
  366. syslog (LOG_ERR, "%s() -> cpg_mcast_joined error:%d, exiting.",
  367. __func__, res);
  368. exit (-2);
  369. }
  370. my_msgs_to_send--;
  371. }
  372. }
  373. static void send_some_more_messages (void * unused)
  374. {
  375. if (use_zcb) {
  376. send_some_more_messages_zcb ();
  377. } else {
  378. send_some_more_messages_normal ();
  379. }
  380. }
  381. static void msg_blaster (int sock, char* num_to_send_str)
  382. {
  383. my_msgs_to_send = atoi (num_to_send_str);
  384. my_seq = 1;
  385. my_pid = getpid();
  386. use_zcb = 0;
  387. cpg_local_get (cpg_handle, &my_nodeid);
  388. /* control the limits */
  389. if (my_msgs_to_send <= 0)
  390. my_msgs_to_send = 1;
  391. if (my_msgs_to_send > 10000)
  392. my_msgs_to_send = 10000;
  393. send_some_more_messages_normal ();
  394. }
  395. static void context_test (int sock)
  396. {
  397. char response[100];
  398. char *cmp;
  399. cpg_context_set (cpg_handle, response);
  400. cpg_context_get (cpg_handle, (void**)&cmp);
  401. if (response != cmp) {
  402. snprintf (response, 100, "%s", FAIL_STR);
  403. }
  404. else {
  405. snprintf (response, 100, "%s", OK_STR);
  406. }
  407. send (sock, response, strlen (response), 0);
  408. }
  409. static void msg_blaster_zcb (int sock, char* num_to_send_str)
  410. {
  411. my_msgs_to_send = atoi (num_to_send_str);
  412. my_seq = 1;
  413. my_pid = getpid();
  414. use_zcb = 1;
  415. cpg_local_get (cpg_handle, &my_nodeid);
  416. /* control the limits */
  417. if (my_msgs_to_send <= 0)
  418. my_msgs_to_send = 1;
  419. if (my_msgs_to_send > 10000)
  420. my_msgs_to_send = 10000;
  421. send_some_more_messages_zcb ();
  422. }
  423. static int cpg_dispatch_wrapper_fn (hdb_handle_t handle,
  424. int fd,
  425. int revents,
  426. void *data)
  427. {
  428. cs_error_t error = cpg_dispatch (cpg_handle, CS_DISPATCH_ALL);
  429. if (error == CS_ERR_LIBRARY) {
  430. syslog (LOG_ERR, "%s() got LIB error disconnecting from corosync.", __func__);
  431. poll_dispatch_delete (ta_poll_handle_get(), cpg_fd);
  432. close (cpg_fd);
  433. cpg_fd = -1;
  434. }
  435. return 0;
  436. }
  437. static void do_command (int sock, char* func, char*args[], int num_args)
  438. {
  439. int result;
  440. char response[100];
  441. struct cpg_name group_name;
  442. if (parse_debug)
  443. syslog (LOG_DEBUG,"RPC:%s() called.", func);
  444. if (strcmp ("cpg_mcast_joined",func) == 0) {
  445. struct iovec iov[5];
  446. int a;
  447. for (a = 0; a < num_args; a++) {
  448. iov[a].iov_base = args[a];
  449. iov[a].iov_len = strlen(args[a])+1;
  450. }
  451. cpg_mcast_joined (cpg_handle, CPG_TYPE_AGREED, iov, num_args);
  452. } else if (strcmp ("cpg_join",func) == 0) {
  453. strcpy (group_name.value, args[0]);
  454. group_name.length = strlen(args[0]);
  455. result = cpg_join (cpg_handle, &group_name);
  456. if (result != CS_OK) {
  457. syslog (LOG_ERR,
  458. "Could not join process group, error %d\n", result);
  459. exit (1);
  460. }
  461. syslog (LOG_INFO, "called cpg_join()!");
  462. } else if (strcmp ("cpg_leave",func) == 0) {
  463. strcpy (group_name.value, args[0]);
  464. group_name.length = strlen(args[0]);
  465. result = cpg_leave (cpg_handle, &group_name);
  466. if (result != CS_OK) {
  467. syslog (LOG_ERR,
  468. "Could not leave process group, error %d\n", result);
  469. exit (1);
  470. }
  471. syslog (LOG_INFO, "called cpg_leave()!");
  472. } else if (strcmp ("cpg_initialize",func) == 0) {
  473. int retry_count = 0;
  474. result = cpg_initialize (&cpg_handle, &callbacks);
  475. while (result != CS_OK) {
  476. syslog (LOG_ERR,
  477. "cpg_initialize error %d (attempt %d)\n",
  478. result, retry_count);
  479. if (retry_count >= 3) {
  480. exit (1);
  481. }
  482. sleep(1);
  483. retry_count++;
  484. result = cpg_initialize (&cpg_handle, &callbacks);
  485. }
  486. cpg_fd_get (cpg_handle, &cpg_fd);
  487. poll_dispatch_add (ta_poll_handle_get(), cpg_fd, POLLIN|POLLNVAL, NULL, cpg_dispatch_wrapper_fn);
  488. } else if (strcmp ("cpg_local_get", func) == 0) {
  489. unsigned int local_nodeid;
  490. cpg_local_get (cpg_handle, &local_nodeid);
  491. snprintf (response, 100, "%u",local_nodeid);
  492. send (sock, response, strlen (response), 0);
  493. } else if (strcmp ("cpg_finalize", func) == 0) {
  494. cpg_finalize (cpg_handle);
  495. poll_dispatch_delete (ta_poll_handle_get(), cpg_fd);
  496. cpg_fd = -1;
  497. } else if (strcmp ("record_config_events", func) == 0) {
  498. record_config_events (sock);
  499. } else if (strcmp ("record_messages", func) == 0) {
  500. record_messages ();
  501. } else if (strcmp ("read_config_event", func) == 0) {
  502. read_config_event (sock);
  503. } else if (strcmp ("read_messages", func) == 0) {
  504. read_messages (sock, args[0]);
  505. } else if (strcmp ("msg_blaster_zcb", func) == 0) {
  506. msg_blaster_zcb (sock, args[0]);
  507. } else if (strcmp ("msg_blaster", func) == 0) {
  508. msg_blaster (sock, args[0]);
  509. } else if (strcmp ("context_test", func) == 0) {
  510. context_test (sock);
  511. } else if (strcmp ("are_you_ok_dude", func) == 0) {
  512. snprintf (response, 100, "%s", OK_STR);
  513. send (sock, response, strlen (response), 0);
  514. } else {
  515. syslog (LOG_ERR,"%s RPC:%s not supported!", __func__, func);
  516. }
  517. }
  518. int main (int argc, char *argv[])
  519. {
  520. openlog (NULL, LOG_CONS|LOG_PID, LOG_DAEMON);
  521. list_init (&msg_log_head);
  522. list_init (&config_chg_log_head);
  523. return test_agent_run (9034, do_command);
  524. }