cpghum.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /*
  2. * Copyright (c) 2015 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Christine Caulfield <ccaulfie@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 <stdio.h>
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include <signal.h>
  38. #include <unistd.h>
  39. #include <errno.h>
  40. #include <time.h>
  41. #include <sys/time.h>
  42. #include <sys/types.h>
  43. #include <sys/socket.h>
  44. #include <sys/select.h>
  45. #include <sys/uio.h>
  46. #include <sys/un.h>
  47. #include <netinet/in.h>
  48. #include <arpa/inet.h>
  49. #include <pthread.h>
  50. #include <zlib.h>
  51. #include <libgen.h>
  52. #include <qb/qblog.h>
  53. #include <qb/qbutil.h>
  54. #include <corosync/corotypes.h>
  55. #include <corosync/cpg.h>
  56. static cpg_handle_t handle;
  57. static pthread_t thread;
  58. #ifndef timersub
  59. #define timersub(a, b, result) \
  60. do { \
  61. (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
  62. (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
  63. if ((result)->tv_usec < 0) { \
  64. --(result)->tv_sec; \
  65. (result)->tv_usec += 1000000; \
  66. } \
  67. } while (0)
  68. #endif /* timersub */
  69. static int alarm_notice;
  70. #define ONE_MEG 1048576
  71. #define DATASIZE (ONE_MEG*20)
  72. static char data[DATASIZE];
  73. static int send_counter = 0;
  74. static int do_syslog = 0;
  75. static int quiet = 0;
  76. static volatile int stopped;
  77. // stats
  78. static unsigned int length_errors=0;
  79. static unsigned int crc_errors=0;
  80. static unsigned int sequence_errors=0;
  81. static unsigned int packets_sent=0;
  82. static unsigned int packets_recvd=0;
  83. static unsigned int send_retries=0;
  84. static unsigned int send_fails=0;
  85. static void cpg_bm_confchg_fn (
  86. cpg_handle_t handle_in,
  87. const struct cpg_name *group_name,
  88. const struct cpg_address *member_list, size_t member_list_entries,
  89. const struct cpg_address *left_list, size_t left_list_entries,
  90. const struct cpg_address *joined_list, size_t joined_list_entries)
  91. {
  92. }
  93. static unsigned int g_recv_count;
  94. static unsigned int g_recv_length;
  95. static unsigned int g_write_size;
  96. static int g_recv_counter = 0;
  97. static void cpg_bm_deliver_fn (
  98. cpg_handle_t handle_in,
  99. const struct cpg_name *group_name,
  100. uint32_t nodeid,
  101. uint32_t pid,
  102. void *msg,
  103. size_t msg_len)
  104. {
  105. int *value = msg;
  106. uLong crc=0;
  107. uLong recv_crc = value[1] & 0xFFFFFFFF;
  108. packets_recvd++;
  109. g_recv_length = msg_len;
  110. // Basic check, packets should all be the right size
  111. if (g_write_size && (msg_len != g_write_size)) {
  112. length_errors++;
  113. fprintf(stderr, "%s: message sizes don't match. got %zu, expected %u\n", group_name->value, msg_len, g_write_size);
  114. if (do_syslog) {
  115. syslog(LOG_ERR, "%s: message sizes don't match. got %zu, expected %u\n", group_name->value, msg_len, g_write_size);
  116. }
  117. }
  118. // Sequence counters are incrementing in step?
  119. if (*value != g_recv_counter) {
  120. sequence_errors++;
  121. fprintf(stderr, "%s: counters don't match. got %d, expected %d\n", group_name->value, *value, g_recv_counter);
  122. if (do_syslog) {
  123. syslog(LOG_ERR, "%s: counters don't match. got %d, expected %d\n", group_name->value, *value, g_recv_counter);
  124. }
  125. // Catch up or we'll be printing errors for ever
  126. g_recv_counter = *value +1;
  127. } else {
  128. g_recv_counter++;
  129. }
  130. // Check crc
  131. crc = crc32(0, NULL, 0);
  132. crc = crc32(crc, (Bytef *)&value[2], msg_len-sizeof(int)*2) & 0xFFFFFFFF;
  133. if (crc != recv_crc) {
  134. crc_errors++;
  135. fprintf(stderr, "%s: CRCs don't match. got %lx, expected %lx\n", group_name->value, recv_crc, crc);
  136. if (do_syslog) {
  137. syslog(LOG_ERR, "%s: CRCs don't match. got %lx, expected %lx\n", group_name->value, recv_crc, crc);
  138. }
  139. }
  140. g_recv_count++;
  141. }
  142. static cpg_model_v1_data_t model1_data = {
  143. .cpg_deliver_fn = cpg_bm_deliver_fn,
  144. .cpg_confchg_fn = cpg_bm_confchg_fn,
  145. };
  146. static cpg_callbacks_t callbacks = {
  147. .cpg_deliver_fn = cpg_bm_deliver_fn,
  148. .cpg_confchg_fn = cpg_bm_confchg_fn
  149. };
  150. static struct cpg_name group_name = {
  151. .value = "cpghum",
  152. .length = 7
  153. };
  154. static void cpg_test (
  155. cpg_handle_t handle_in,
  156. int write_size,
  157. int delay_time,
  158. int print_time)
  159. {
  160. struct iovec iov;
  161. unsigned int res;
  162. int i;
  163. unsigned int *dataint = (unsigned int *)data;
  164. uLong crc;
  165. alarm_notice = 0;
  166. iov.iov_base = data;
  167. iov.iov_len = write_size;
  168. g_recv_count = 0;
  169. alarm (print_time);
  170. do {
  171. dataint[0] = send_counter++;
  172. for (i=2; i<(DATASIZE-sizeof(int)*2)/4; i++) {
  173. dataint[i] = rand();
  174. }
  175. crc = crc32(0, NULL, 0);
  176. dataint[1] = crc32(crc, (Bytef*)&dataint[2], write_size-sizeof(int)*2);
  177. resend:
  178. res = cpg_mcast_joined (handle_in, CPG_TYPE_AGREED, &iov, 1);
  179. if (res == CS_ERR_TRY_AGAIN) {
  180. usleep(10000);
  181. send_retries++;
  182. goto resend;
  183. }
  184. if (res != CS_OK) {
  185. fprintf(stderr, "send failed: %d\n", res);
  186. send_fails++;
  187. }
  188. else {
  189. packets_sent++;
  190. }
  191. usleep(delay_time*1000);
  192. } while (alarm_notice == 0 && (res == CS_OK || res == CS_ERR_TRY_AGAIN) && stopped == 0);
  193. if (!quiet) {
  194. printf ("%s: %5d message%s received, ", group_name.value, g_recv_count, g_recv_count==1?"":"s");
  195. printf ("%5d bytes per write\n", write_size);
  196. }
  197. }
  198. static void sigalrm_handler (int num)
  199. {
  200. alarm_notice = 1;
  201. }
  202. static void sigint_handler (int num)
  203. {
  204. stopped = 1;
  205. }
  206. static void* dispatch_thread (void *arg)
  207. {
  208. cpg_dispatch (handle, CS_DISPATCH_BLOCKING);
  209. return NULL;
  210. }
  211. static void usage(char *cmd)
  212. {
  213. fprintf(stderr, "%s [OPTIONS]\n", cmd);
  214. fprintf(stderr, "\n");
  215. fprintf(stderr, "%s sends CPG messages to all registered users of the CPG.\n", cmd);
  216. fprintf(stderr, "The messages have a sequence number and a CRC so that missing or\n");
  217. fprintf(stderr, "corrupted messages will be detected and reported.\n");
  218. fprintf(stderr, "\n");
  219. fprintf(stderr, "%s can also be asked to simply listen for (and check) packets\n", cmd);
  220. fprintf(stderr, "so that there is another node in the cluster connected to the CPG.\n");
  221. fprintf(stderr, "\n");
  222. fprintf(stderr, "When -l is present, packet size is only checked if specified by -w or -W\n");
  223. fprintf(stderr, "and it, obviously, must match that of the sender.\n");
  224. fprintf(stderr, "\n");
  225. fprintf(stderr, "Multiple copies, in different CPGs, can also be run on the same or\n");
  226. fprintf(stderr, "different nodes by using the -n option.\n");
  227. fprintf(stderr, "\n");
  228. fprintf(stderr, "%s can't handle more than 1 sender in the same CPG as it messes with the\n", cmd);
  229. fprintf(stderr, "sequence numbers.\n");
  230. fprintf(stderr, "\n");
  231. fprintf(stderr, " -w Write size in Kbytes, default 4\n");
  232. fprintf(stderr, " -W Write size in bytes, default 4096\n");
  233. fprintf(stderr, " -n CPG name to use, default 'cpghum'\n");
  234. fprintf(stderr, " -d Delay between sending packets (mS), default 1000\n");
  235. fprintf(stderr, " -r Number of repetitions, default 100\n");
  236. fprintf(stderr, " -p Delay between printing output(S), default 10s\n");
  237. fprintf(stderr, " -l Listen and check CRCs only, don't send (^C to quit)\n");
  238. fprintf(stderr, " -m cpg_initialise() model. Default 1.\n");
  239. fprintf(stderr, " -s Also send errors to syslog (for daemon log correlation).\n");
  240. fprintf(stderr, " -q Quiet. Don't print messages every 10 seconds (see also -p)\n");
  241. fprintf(stderr, "\n");
  242. }
  243. int main (int argc, char *argv[]) {
  244. int i;
  245. unsigned int res;
  246. uint32_t maxsize;
  247. int opt;
  248. int bs;
  249. int write_size = 4096;
  250. int delay_time = 1000;
  251. int repetitions = 100;
  252. int print_time = 10;
  253. int have_size = 0;
  254. int listen_only = 0;
  255. int model = 1;
  256. while ( (opt = getopt(argc, argv, "qlsn:d:r:p:m:w:W:")) != -1 ) {
  257. switch (opt) {
  258. case 'w': // Write size in K
  259. bs = atoi(optarg);
  260. if (bs > 0) {
  261. write_size = bs*1024;
  262. have_size = 1;
  263. }
  264. break;
  265. case 'W': // Write size in bytes
  266. bs = atoi(optarg);
  267. if (bs > 0) {
  268. write_size = bs;
  269. have_size = 1;
  270. }
  271. break;
  272. case 'n':
  273. if (strlen(optarg) >= CPG_MAX_NAME_LENGTH) {
  274. fprintf(stderr, "CPG name too long\n");
  275. exit(1);
  276. }
  277. strcpy(group_name.value, optarg);
  278. group_name.length = strlen(group_name.value);
  279. break;
  280. case 'd':
  281. delay_time = atoi(optarg);
  282. break;
  283. case 'r':
  284. repetitions = atoi(optarg);
  285. break;
  286. case 'p':
  287. print_time = atoi(optarg);
  288. break;
  289. case 'l':
  290. listen_only = 1;
  291. break;
  292. case 's':
  293. do_syslog = 1;
  294. break;
  295. case 'q':
  296. quiet = 1;
  297. break;
  298. case 'm':
  299. model = atoi(optarg);
  300. if (model < 0 || model > 1) {
  301. fprintf(stderr, "%s: Model must be 0-1\n", argv[0]);
  302. exit(1);
  303. }
  304. break;
  305. case '?':
  306. usage(basename(argv[0]));
  307. exit(0);
  308. }
  309. }
  310. qb_log_init("cpghum", LOG_USER, LOG_EMERG);
  311. qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_ENABLED, QB_FALSE);
  312. qb_log_filter_ctl(QB_LOG_STDERR, QB_LOG_FILTER_ADD,
  313. QB_LOG_FILTER_FILE, "*", LOG_DEBUG);
  314. qb_log_ctl(QB_LOG_STDERR, QB_LOG_CONF_ENABLED, QB_TRUE);
  315. g_write_size = write_size;
  316. signal (SIGALRM, sigalrm_handler);
  317. signal (SIGINT, sigint_handler);
  318. switch (model) {
  319. case 0:
  320. res = cpg_initialize (&handle, &callbacks);
  321. break;
  322. case 1:
  323. res = cpg_model_initialize (&handle, CPG_MODEL_V1, (cpg_model_data_t *)&model1_data, NULL);
  324. break;
  325. default:
  326. res=999; // can't get here but it keeps the compiler happy
  327. break;
  328. }
  329. if (res != CS_OK) {
  330. printf ("cpg_initialize failed with result %d\n", res);
  331. exit (1);
  332. }
  333. pthread_create (&thread, NULL, dispatch_thread, NULL);
  334. res = cpg_join (handle, &group_name);
  335. if (res != CS_OK) {
  336. printf ("cpg_join failed with result %d\n", res);
  337. exit (1);
  338. }
  339. if (listen_only) {
  340. int secs = 0;
  341. if (!quiet) {
  342. printf("-- Listening on CPG %s\n", group_name.value);
  343. printf("-- Ignore any starting \"counters don't match\" error while we catch up\n");
  344. }
  345. /* Only check packet size if specified on the command-line */
  346. if (!have_size) {
  347. g_write_size = 0;
  348. }
  349. while (!stopped) {
  350. sleep(1);
  351. if (++secs > print_time && !quiet) {
  352. printf ("%s: %5d message%s received. %d bytes\n", group_name.value, g_recv_count, g_recv_count==1?"":"s", g_recv_length);
  353. secs = 0;
  354. g_recv_count = 0;
  355. }
  356. }
  357. }
  358. else {
  359. cpg_max_atomic_msgsize_get (handle, &maxsize);
  360. if ( write_size > maxsize) {
  361. fprintf(stderr, "INFO: packet size (%d) is larger than the maximum atomic size (%d), libcpg will fragment\n",
  362. write_size, maxsize);
  363. }
  364. for (i = 0; i < repetitions && !stopped; i++) {
  365. cpg_test (handle, write_size, delay_time, print_time);
  366. signal (SIGALRM, sigalrm_handler);
  367. }
  368. }
  369. res = cpg_finalize (handle);
  370. if (res != CS_OK) {
  371. printf ("cpg_finalize failed with result %d\n", res);
  372. exit (1);
  373. }
  374. printf("\n");
  375. printf("Stats:\n");
  376. if (!listen_only) {
  377. printf(" packets sent: %d\n", packets_sent);
  378. printf(" send failures: %d\n", send_fails);
  379. printf(" send retries: %d\n", send_retries);
  380. }
  381. if (have_size) {
  382. printf(" length errors: %d\n", length_errors);
  383. }
  384. printf(" packets recvd: %d\n", packets_recvd);
  385. printf(" sequence errors: %d\n", sequence_errors);
  386. printf(" crc errors: %d\n", crc_errors);
  387. printf("\n");
  388. return (0);
  389. }