cpghum.c 14 KB

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