cpghum.c 12 KB

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