4
0

cpg_test_agent.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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 my_msgs_to_send;
  86. static int32_t total_stored_msgs = 0;
  87. static void send_some_more_messages (void * unused);
  88. static char* err_status_string (char * buf, size_t buf_len, msg_status_t status)
  89. {
  90. switch (status) {
  91. case MSG_OK:
  92. strncpy (buf, "OK", buf_len);
  93. break;
  94. case MSG_NODEID_ERR:
  95. strncpy (buf, "NODEID_ERR", buf_len);
  96. break;
  97. case MSG_PID_ERR:
  98. strncpy (buf, "PID_ERR", buf_len);
  99. break;
  100. case MSG_SEQ_ERR:
  101. strncpy (buf, "SEQ_ERR", buf_len);
  102. break;
  103. case MSG_SIZE_ERR:
  104. strncpy (buf, "SIZE_ERR", buf_len);
  105. break;
  106. case MSG_SHA1_ERR:
  107. strncpy (buf, "SHA1_ERR", buf_len);
  108. break;
  109. default:
  110. strncpy (buf, "UNKNOWN_ERR", buf_len);
  111. break;
  112. }
  113. return buf;
  114. }
  115. static void delivery_callback (
  116. cpg_handle_t handle,
  117. const struct cpg_name *groupName,
  118. uint32_t nodeid,
  119. uint32_t pid,
  120. void *msg,
  121. size_t msg_len)
  122. {
  123. log_entry_t *log_pt;
  124. msg_t *msg_pt = (msg_t*)msg;
  125. msg_status_t status = MSG_OK;
  126. char status_buf[20];
  127. unsigned char sha1_compare[20];
  128. hash_state sha1_hash;
  129. if (record_messages_g == 0) {
  130. return;
  131. }
  132. msg_pt->seq = my_seq;
  133. my_seq++;
  134. if (nodeid != msg_pt->nodeid) {
  135. status = MSG_NODEID_ERR;
  136. }
  137. if (pid != msg_pt->pid) {
  138. status = MSG_PID_ERR;
  139. }
  140. if (msg_len != msg_pt->size) {
  141. status = MSG_SIZE_ERR;
  142. }
  143. sha1_init (&sha1_hash);
  144. sha1_process (&sha1_hash, msg_pt->payload, (msg_pt->size - sizeof (msg_t)));
  145. sha1_done (&sha1_hash, sha1_compare);
  146. if (memcmp (sha1_compare, msg_pt->sha1, 20) != 0) {
  147. syslog (LOG_ERR, "%s(); msg seq:%d; incorrect hash",
  148. __func__, msg_pt->seq);
  149. status = MSG_SHA1_ERR;
  150. }
  151. log_pt = malloc (sizeof(log_entry_t));
  152. list_init (&log_pt->list);
  153. snprintf (log_pt->log, LOG_STR_SIZE, "%d:%d:%d:%s;",
  154. msg_pt->nodeid, msg_pt->pid, msg_pt->seq,
  155. err_status_string (status_buf, 20, status));
  156. list_add_tail (&log_pt->list, &msg_log_head);
  157. total_stored_msgs++;
  158. }
  159. static void config_change_callback (
  160. cpg_handle_t handle,
  161. const struct cpg_name *groupName,
  162. const struct cpg_address *member_list, size_t member_list_entries,
  163. const struct cpg_address *left_list, size_t left_list_entries,
  164. const struct cpg_address *joined_list, size_t joined_list_entries)
  165. {
  166. int i;
  167. log_entry_t *log_pt;
  168. /* group_name,ip,pid,join|leave */
  169. if (record_config_events_g == 0) {
  170. return;
  171. }
  172. for (i = 0; i < left_list_entries; i++) {
  173. syslog (LOG_DEBUG, "%s() inserting leave event into list", __func__);
  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. for (i = 0; i < joined_list_entries; i++) {
  181. syslog (LOG_DEBUG, "%s() inserting join event into list", __func__);
  182. log_pt = malloc (sizeof(log_entry_t));
  183. list_init (&log_pt->list);
  184. snprintf (log_pt->log, LOG_STR_SIZE, "%s,%d,%d,join",
  185. groupName->value, joined_list[i].nodeid,joined_list[i].pid);
  186. list_add_tail (&log_pt->list, &config_chg_log_head);
  187. }
  188. }
  189. static cpg_callbacks_t callbacks = {
  190. .cpg_deliver_fn = delivery_callback,
  191. .cpg_confchg_fn = config_change_callback,
  192. };
  193. static void record_messages (void)
  194. {
  195. record_messages_g = 1;
  196. syslog (LOG_DEBUG,"%s() record:%d", __func__, record_messages_g);
  197. }
  198. static void record_config_events (void)
  199. {
  200. record_config_events_g = 1;
  201. syslog (LOG_DEBUG,"%s() record:%d", __func__, record_config_events_g);
  202. }
  203. static void read_config_event (int sock)
  204. {
  205. const char *empty = "None";
  206. struct list_head * list = config_chg_log_head.next;
  207. log_entry_t *entry;
  208. if (list != &config_chg_log_head) {
  209. entry = list_entry (list, log_entry_t, list);
  210. send (sock, entry->log, strlen (entry->log) + 1, 0);
  211. list_del (&entry->list);
  212. free (entry);
  213. } else {
  214. syslog (LOG_DEBUG,"%s() no events in list", __func__);
  215. send (sock, empty, strlen (empty) + 1, 0);
  216. }
  217. }
  218. static void read_messages (int sock, char* atmost_str)
  219. {
  220. struct list_head * list;
  221. log_entry_t *entry;
  222. int atmost = atoi (atmost_str);
  223. int packed = 0;
  224. if (atmost == 0)
  225. atmost = 1;
  226. if (atmost > (HOW_BIG_AND_BUF / LOG_STR_SIZE))
  227. atmost = (HOW_BIG_AND_BUF / LOG_STR_SIZE);
  228. syslog (LOG_DEBUG, "%s() atmost %d; total_stored_msgs:%d",
  229. __func__, atmost, total_stored_msgs);
  230. big_and_buf[0] = '\0';
  231. for (list = msg_log_head.next;
  232. (!list_empty (&msg_log_head) && packed < atmost); ) {
  233. entry = list_entry (list, log_entry_t, list);
  234. strcat (big_and_buf, entry->log);
  235. packed++;
  236. list = list->next;
  237. list_del (&entry->list);
  238. free (entry);
  239. total_stored_msgs--;
  240. }
  241. syslog (LOG_DEBUG, "%s() sending %d; total_stored_msgs:%d; len:%d",
  242. __func__, packed, total_stored_msgs, (int)strlen (big_and_buf));
  243. if (packed == 0) {
  244. strcpy (big_and_buf, "None");
  245. }
  246. send (sock, big_and_buf, strlen (big_and_buf), 0);
  247. }
  248. static void send_some_more_messages_later (void)
  249. {
  250. poll_timer_handle timer_handle;
  251. cpg_dispatch (cpg_handle, CS_DISPATCH_ALL);
  252. poll_timer_add (
  253. ta_poll_handle_get(),
  254. 100, NULL,
  255. send_some_more_messages,
  256. &timer_handle);
  257. }
  258. static unsigned char buffer[200000];
  259. static void send_some_more_messages (void * unused)
  260. {
  261. msg_t my_msg;
  262. struct iovec iov[2];
  263. int i;
  264. int send_now;
  265. size_t payload_size;
  266. hash_state sha1_hash;
  267. cs_error_t res;
  268. cpg_flow_control_state_t fc_state;
  269. if (cpg_fd < 0)
  270. return;
  271. send_now = my_msgs_to_send;
  272. //syslog (LOG_DEBUG,"%s() send_now:%d", __func__, send_now);
  273. my_msg.pid = my_pid;
  274. my_msg.nodeid = my_nodeid;
  275. payload_size = (rand() % 100000);
  276. my_msg.size = sizeof (msg_t) + payload_size;
  277. my_msg.seq = 0;
  278. for (i = 0; i < payload_size; i++) {
  279. buffer[i] = i;
  280. }
  281. sha1_init (&sha1_hash);
  282. sha1_process (&sha1_hash, buffer, payload_size);
  283. sha1_done (&sha1_hash, my_msg.sha1);
  284. iov[0].iov_len = sizeof (msg_t);
  285. iov[0].iov_base = &my_msg;
  286. iov[1].iov_len = payload_size;
  287. iov[1].iov_base = buffer;
  288. for (i = 0; i < send_now; i++) {
  289. res = cpg_flow_control_state_get (cpg_handle, &fc_state);
  290. if (res == CS_OK && fc_state == CPG_FLOW_CONTROL_ENABLED) {
  291. /* lets do this later */
  292. send_some_more_messages_later ();
  293. syslog (LOG_INFO, "%s() flow control enabled.", __func__);
  294. return;
  295. }
  296. res = cpg_mcast_joined (cpg_handle, CPG_TYPE_AGREED, iov, 2);
  297. if (res == CS_ERR_TRY_AGAIN) {
  298. /* lets do this later */
  299. send_some_more_messages_later ();
  300. syslog (LOG_INFO, "%s() cpg_mcast_joined() says try again.",
  301. __func__);
  302. return;
  303. } else
  304. if (res != CS_OK) {
  305. syslog (LOG_ERR, "%s() -> cpg_mcast_joined error:%d, exiting.",
  306. __func__, res);
  307. exit (-2);
  308. }
  309. my_msgs_to_send--;
  310. }
  311. }
  312. static void msg_blaster (int sock, char* num_to_send_str)
  313. {
  314. my_msgs_to_send = atoi (num_to_send_str);
  315. my_seq = 1;
  316. my_pid = getpid();
  317. cpg_local_get (cpg_handle, &my_nodeid);
  318. /* control the limits */
  319. if (my_msgs_to_send <= 0)
  320. my_msgs_to_send = 1;
  321. if (my_msgs_to_send > 10000)
  322. my_msgs_to_send = 10000;
  323. send_some_more_messages (NULL);
  324. }
  325. static int cpg_dispatch_wrapper_fn (hdb_handle_t handle,
  326. int fd,
  327. int revents,
  328. void *data)
  329. {
  330. cs_error_t error = cpg_dispatch (cpg_handle, CS_DISPATCH_ALL);
  331. if (error == CS_ERR_LIBRARY) {
  332. syslog (LOG_ERR, "%s() got LIB error disconnecting from corosync.", __func__);
  333. poll_dispatch_delete (ta_poll_handle_get(), cpg_fd);
  334. close (cpg_fd);
  335. cpg_fd = -1;
  336. }
  337. return 0;
  338. }
  339. static void do_command (int sock, char* func, char*args[], int num_args)
  340. {
  341. int result;
  342. struct cpg_name group_name;
  343. if (parse_debug)
  344. syslog (LOG_DEBUG,"RPC:%s() called.", func);
  345. if (strcmp ("cpg_mcast_joined",func) == 0) {
  346. struct iovec iov[5];
  347. int a;
  348. for (a = 0; a < num_args; a++) {
  349. iov[a].iov_base = args[a];
  350. iov[a].iov_len = strlen(args[a])+1;
  351. }
  352. cpg_mcast_joined (cpg_handle, CPG_TYPE_AGREED, iov, num_args);
  353. } else if (strcmp ("cpg_join",func) == 0) {
  354. strcpy (group_name.value, args[0]);
  355. group_name.length = strlen(args[0]);
  356. result = cpg_join (cpg_handle, &group_name);
  357. if (result != CS_OK) {
  358. syslog (LOG_ERR,
  359. "Could not join process group, error %d\n", result);
  360. exit (1);
  361. }
  362. } else if (strcmp ("cpg_leave",func) == 0) {
  363. strcpy (group_name.value, args[0]);
  364. group_name.length = strlen(args[0]);
  365. result = cpg_leave (cpg_handle, &group_name);
  366. if (result != CS_OK) {
  367. syslog (LOG_ERR,
  368. "Could not leave process group, error %d\n", result);
  369. exit (1);
  370. }
  371. syslog (LOG_INFO, "called cpg_leave()!");
  372. } else if (strcmp ("cpg_initialize",func) == 0) {
  373. int retry_count = 0;
  374. result = cpg_initialize (&cpg_handle, &callbacks);
  375. while (result != CS_OK) {
  376. syslog (LOG_ERR,
  377. "cpg_initialize error %d (attempt %d)\n",
  378. result, retry_count);
  379. if (retry_count >= 3) {
  380. exit (1);
  381. }
  382. sleep(1);
  383. retry_count++;
  384. }
  385. cpg_fd_get (cpg_handle, &cpg_fd);
  386. poll_dispatch_add (ta_poll_handle_get(), cpg_fd, POLLIN|POLLNVAL, NULL, cpg_dispatch_wrapper_fn);
  387. } else if (strcmp ("cpg_local_get", func) == 0) {
  388. unsigned int local_nodeid;
  389. char response[100];
  390. cpg_local_get (cpg_handle, &local_nodeid);
  391. snprintf (response, 100, "%u",local_nodeid);
  392. send (sock, response, strlen (response) + 1, 0);
  393. } else if (strcmp ("cpg_finalize",func) == 0) {
  394. cpg_finalize (cpg_handle);
  395. poll_dispatch_delete (ta_poll_handle_get(), cpg_fd);
  396. cpg_fd = -1;
  397. } else if (strcmp ("record_config_events",func) == 0) {
  398. record_config_events ();
  399. } else if (strcmp ("record_messages",func) == 0) {
  400. record_messages ();
  401. } else if (strcmp ("read_config_event",func) == 0) {
  402. read_config_event (sock);
  403. } else if (strcmp ("read_messages",func) == 0) {
  404. read_messages (sock, args[0]);
  405. } else if (strcmp ("msg_blaster",func) == 0) {
  406. msg_blaster (sock, args[0]);
  407. } else {
  408. syslog (LOG_ERR,"%s RPC:%s not supported!", __func__, func);
  409. }
  410. }
  411. int main (int argc, char *argv[])
  412. {
  413. openlog (NULL, LOG_CONS|LOG_PID, LOG_DAEMON);
  414. list_init (&msg_log_head);
  415. list_init (&config_chg_log_head);
  416. return test_agent_run (9034, do_command);
  417. }