cpghum.c 14 KB

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