ipc_glue.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  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 Red Hat, 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 <config.h>
  35. #include <stdlib.h>
  36. #include <stdio.h>
  37. #include <errno.h>
  38. #include <assert.h>
  39. #include <sys/uio.h>
  40. #include <string.h>
  41. #include <qb/qbdefs.h>
  42. #include <qb/qblist.h>
  43. #include <qb/qbutil.h>
  44. #include <qb/qbloop.h>
  45. #include <qb/qbipcs.h>
  46. #include <corosync/swab.h>
  47. #include <corosync/corotypes.h>
  48. #include <corosync/corodefs.h>
  49. #include <corosync/totem/totempg.h>
  50. #include <corosync/logsys.h>
  51. #include <corosync/engine/icmap.h>
  52. #include "mainconfig.h"
  53. #include "sync.h"
  54. #include "syncv2.h"
  55. #include "timer.h"
  56. #include "main.h"
  57. #include "util.h"
  58. #include "apidef.h"
  59. #include "service.h"
  60. LOGSYS_DECLARE_SUBSYS ("MAIN");
  61. static struct corosync_api_v1 *api = NULL;
  62. static int32_t ipc_not_enough_fds_left = 0;
  63. static int32_t ipc_fc_is_quorate; /* boolean */
  64. static int32_t ipc_fc_totem_queue_level; /* percentage used */
  65. static int32_t ipc_fc_sync_in_process; /* boolean */
  66. struct cs_ipcs_mapper {
  67. int32_t id;
  68. qb_ipcs_service_t *inst;
  69. char name[256];
  70. };
  71. struct outq_item {
  72. void *msg;
  73. size_t mlen;
  74. struct list_head list;
  75. };
  76. static struct cs_ipcs_mapper ipcs_mapper[SERVICE_HANDLER_MAXIMUM_COUNT];
  77. static int32_t cs_ipcs_job_add(enum qb_loop_priority p, void *data, qb_loop_job_dispatch_fn fn);
  78. static int32_t cs_ipcs_dispatch_add(enum qb_loop_priority p, int32_t fd, int32_t events,
  79. void *data, qb_ipcs_dispatch_fn_t fn);
  80. static int32_t cs_ipcs_dispatch_mod(enum qb_loop_priority p, int32_t fd, int32_t events,
  81. void *data, qb_ipcs_dispatch_fn_t fn);
  82. static int32_t cs_ipcs_dispatch_del(int32_t fd);
  83. static struct qb_ipcs_poll_handlers corosync_poll_funcs = {
  84. .job_add = cs_ipcs_job_add,
  85. .dispatch_add = cs_ipcs_dispatch_add,
  86. .dispatch_mod = cs_ipcs_dispatch_mod,
  87. .dispatch_del = cs_ipcs_dispatch_del,
  88. };
  89. static int32_t cs_ipcs_connection_accept (qb_ipcs_connection_t *c, uid_t euid, gid_t egid);
  90. static void cs_ipcs_connection_created(qb_ipcs_connection_t *c);
  91. static int32_t cs_ipcs_msg_process(qb_ipcs_connection_t *c,
  92. void *data, size_t size);
  93. static int32_t cs_ipcs_connection_closed (qb_ipcs_connection_t *c);
  94. static void cs_ipcs_connection_destroyed (qb_ipcs_connection_t *c);
  95. static struct qb_ipcs_service_handlers corosync_service_funcs = {
  96. .connection_accept = cs_ipcs_connection_accept,
  97. .connection_created = cs_ipcs_connection_created,
  98. .msg_process = cs_ipcs_msg_process,
  99. .connection_closed = cs_ipcs_connection_closed,
  100. .connection_destroyed = cs_ipcs_connection_destroyed,
  101. };
  102. static const char* cs_ipcs_serv_short_name(int32_t service_id)
  103. {
  104. const char *name;
  105. switch (service_id) {
  106. case EVS_SERVICE:
  107. name = "evs";
  108. break;
  109. case CLM_SERVICE:
  110. name = "saClm";
  111. break;
  112. case AMF_SERVICE:
  113. name = "saAmf";
  114. break;
  115. case CKPT_SERVICE:
  116. name = "saCkpt";
  117. break;
  118. case EVT_SERVICE:
  119. name = "saEvt";
  120. break;
  121. case LCK_SERVICE:
  122. name = "saLck";
  123. break;
  124. case MSG_SERVICE:
  125. name = "saMsg";
  126. break;
  127. case CFG_SERVICE:
  128. name = "cfg";
  129. break;
  130. case CPG_SERVICE:
  131. name = "cpg";
  132. break;
  133. case CMAN_SERVICE:
  134. name = "cman";
  135. break;
  136. case PCMK_SERVICE:
  137. name = "pacemaker.engine";
  138. break;
  139. case CONFDB_SERVICE:
  140. name = "confdb";
  141. break;
  142. case QUORUM_SERVICE:
  143. name = "quorum";
  144. break;
  145. case PLOAD_SERVICE:
  146. name = "pload";
  147. break;
  148. case TMR_SERVICE:
  149. name = "saTmr";
  150. break;
  151. case VOTEQUORUM_SERVICE:
  152. name = "votequorum";
  153. break;
  154. case NTF_SERVICE:
  155. name = "saNtf";
  156. break;
  157. case AMF_V2_SERVICE:
  158. name = "saAmfV2";
  159. break;
  160. case TST_SV1_SERVICE:
  161. name = "tst";
  162. break;
  163. case TST_SV2_SERVICE:
  164. name = "tst2";
  165. break;
  166. case MON_SERVICE:
  167. name = "mon";
  168. break;
  169. case WD_SERVICE:
  170. name = "wd";
  171. break;
  172. case CMAP_SERVICE:
  173. name = "cmap";
  174. break;
  175. default:
  176. name = NULL;
  177. break;
  178. }
  179. return name;
  180. }
  181. int32_t cs_ipcs_service_destroy(int32_t service_id)
  182. {
  183. if (ipcs_mapper[service_id].inst) {
  184. qb_ipcs_destroy(ipcs_mapper[service_id].inst);
  185. ipcs_mapper[service_id].inst = NULL;
  186. }
  187. return 0;
  188. }
  189. static int32_t cs_ipcs_connection_accept (qb_ipcs_connection_t *c, uid_t euid, gid_t egid)
  190. {
  191. int32_t service = qb_ipcs_service_id_get(c);
  192. uint8_t u8;
  193. char key_name[ICMAP_KEYNAME_MAXLEN];
  194. if (corosync_service[service] == NULL ||
  195. corosync_service_exiting[service] ||
  196. ipcs_mapper[service].inst == NULL) {
  197. return -ENOSYS;
  198. }
  199. if (ipc_not_enough_fds_left) {
  200. return -EMFILE;
  201. }
  202. if (euid == 0 || egid == 0) {
  203. return 0;
  204. }
  205. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "uidgid.uid.%u", euid);
  206. if (icmap_get_uint8(key_name, &u8) == CS_OK && u8 == 1)
  207. return 0;
  208. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "uidgid.gid.%u", egid);
  209. if (icmap_get_uint8(key_name, &u8) == CS_OK && u8 == 1)
  210. return 0;
  211. log_printf(LOGSYS_LEVEL_ERROR, "Denied connection attempt from %d:%d", euid, egid);
  212. return -EACCES;
  213. }
  214. static char * pid_to_name (pid_t pid, char *out_name, size_t name_len)
  215. {
  216. char *name;
  217. char *rest;
  218. FILE *fp;
  219. char fname[32];
  220. char buf[256];
  221. snprintf (fname, 32, "/proc/%d/stat", pid);
  222. fp = fopen (fname, "r");
  223. if (!fp) {
  224. return NULL;
  225. }
  226. if (fgets (buf, sizeof (buf), fp) == NULL) {
  227. fclose (fp);
  228. return NULL;
  229. }
  230. fclose (fp);
  231. name = strrchr (buf, '(');
  232. if (!name) {
  233. return NULL;
  234. }
  235. /* move past the bracket */
  236. name++;
  237. rest = strrchr (buf, ')');
  238. if (rest == NULL || rest[1] != ' ') {
  239. return NULL;
  240. }
  241. *rest = '\0';
  242. /* move past the NULL and space */
  243. rest += 2;
  244. /* copy the name */
  245. strncpy (out_name, name, name_len);
  246. out_name[name_len - 1] = '\0';
  247. return out_name;
  248. }
  249. struct cs_ipcs_conn_context {
  250. char *icmap_path;
  251. struct list_head outq_head;
  252. int32_t queuing;
  253. uint32_t queued;
  254. uint64_t invalid_request;
  255. uint64_t overload;
  256. uint32_t sent;
  257. char data[1];
  258. };
  259. static void cs_ipcs_connection_created(qb_ipcs_connection_t *c)
  260. {
  261. int32_t service = 0;
  262. struct cs_ipcs_conn_context *context;
  263. char proc_name[32];
  264. struct qb_ipcs_connection_stats stats;
  265. int32_t size = sizeof(struct cs_ipcs_conn_context);
  266. char key_name[ICMAP_KEYNAME_MAXLEN];
  267. int set_client_pid = 0;
  268. int set_proc_name = 0;
  269. log_printf(LOG_DEBUG, "%s() new connection", __func__);
  270. service = qb_ipcs_service_id_get(c);
  271. size += corosync_service[service]->private_data_size;
  272. context = calloc(1, size);
  273. list_init(&context->outq_head);
  274. context->queuing = QB_FALSE;
  275. context->queued = 0;
  276. context->sent = 0;
  277. qb_ipcs_context_set(c, context);
  278. corosync_service[service]->lib_init_fn(c);
  279. icmap_inc("runtime.connections.active");
  280. qb_ipcs_connection_stats_get(c, &stats, QB_FALSE);
  281. if (stats.client_pid > 0) {
  282. if (pid_to_name (stats.client_pid, proc_name, sizeof(proc_name))) {
  283. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "runtime.connections.%s:%u:%p",
  284. proc_name, stats.client_pid, c);
  285. set_proc_name = 1;
  286. } else {
  287. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "runtime.connections.%u:%p",
  288. stats.client_pid, c);
  289. }
  290. set_client_pid = 1;
  291. } else {
  292. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "runtime.connections.%p", c);
  293. }
  294. icmap_convert_name_to_valid_name(key_name);
  295. context->icmap_path = strdup(key_name);
  296. if (context->icmap_path == NULL) {
  297. return ;
  298. }
  299. if (set_proc_name) {
  300. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s.name", context->icmap_path);
  301. icmap_set_string(key_name, proc_name);
  302. }
  303. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s.client_pid", context->icmap_path);
  304. if (set_client_pid) {
  305. icmap_set_uint32(key_name, stats.client_pid);
  306. } else {
  307. icmap_set_uint32(key_name, 0);
  308. }
  309. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s.service_id", context->icmap_path);
  310. icmap_set_uint32(key_name, service);
  311. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s.responses", context->icmap_path);
  312. icmap_set_uint64(key_name, 0);
  313. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s.dispatched", context->icmap_path);
  314. icmap_set_uint64(key_name, 0);
  315. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s.requests", context->icmap_path);
  316. icmap_set_uint64(key_name, 0);
  317. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s.send_retries", context->icmap_path);
  318. icmap_set_uint64(key_name, 0);
  319. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s.recv_retries", context->icmap_path);
  320. icmap_set_uint64(key_name, 0);
  321. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s.flow_control", context->icmap_path);
  322. icmap_set_uint32(key_name, 0);
  323. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s.flow_control_count", context->icmap_path);
  324. icmap_set_uint64(key_name, 0);
  325. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s.queue_size", context->icmap_path);
  326. icmap_set_uint32(key_name, 0);
  327. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s.invalid_request", context->icmap_path);
  328. icmap_set_uint64(key_name, 0);
  329. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s.overload", context->icmap_path);
  330. icmap_set_uint64(key_name, 0);
  331. }
  332. void cs_ipc_refcnt_inc(void *conn)
  333. {
  334. qb_ipcs_connection_ref(conn);
  335. }
  336. void cs_ipc_refcnt_dec(void *conn)
  337. {
  338. qb_ipcs_connection_unref(conn);
  339. }
  340. void *cs_ipcs_private_data_get(void *conn)
  341. {
  342. struct cs_ipcs_conn_context *cnx;
  343. cnx = qb_ipcs_context_get(conn);
  344. return &cnx->data[0];
  345. }
  346. static void cs_ipcs_connection_destroyed (qb_ipcs_connection_t *c)
  347. {
  348. struct cs_ipcs_conn_context *context;
  349. struct list_head *list, *list_next;
  350. struct outq_item *outq_item;
  351. log_printf(LOG_DEBUG, "%s() ", __func__);
  352. context = qb_ipcs_context_get(c);
  353. if (context) {
  354. for (list = context->outq_head.next;
  355. list != &context->outq_head; list = list_next) {
  356. list_next = list->next;
  357. outq_item = list_entry (list, struct outq_item, list);
  358. list_del (list);
  359. free (outq_item->msg);
  360. free (outq_item);
  361. }
  362. free(context);
  363. }
  364. }
  365. static int32_t cs_ipcs_connection_closed (qb_ipcs_connection_t *c)
  366. {
  367. int32_t res = 0;
  368. int32_t service = qb_ipcs_service_id_get(c);
  369. icmap_iter_t iter;
  370. char prefix[ICMAP_KEYNAME_MAXLEN];
  371. const char *key_name;
  372. struct cs_ipcs_conn_context *cnx;
  373. log_printf(LOG_DEBUG, "%s() ", __func__);
  374. res = corosync_service[service]->lib_exit_fn(c);
  375. if (res != 0) {
  376. return res;
  377. }
  378. cnx = qb_ipcs_context_get(c);
  379. snprintf(prefix, ICMAP_KEYNAME_MAXLEN, "%s.", cnx->icmap_path);
  380. iter = icmap_iter_init(prefix);
  381. while ((key_name = icmap_iter_next(iter, NULL, NULL)) != NULL) {
  382. icmap_delete(key_name);
  383. }
  384. icmap_iter_finalize(iter);
  385. free(cnx->icmap_path);
  386. icmap_inc("runtie.connections.closed");
  387. icmap_dec("runtime.connections.active");
  388. return 0;
  389. }
  390. int cs_ipcs_response_iov_send (void *conn,
  391. const struct iovec *iov,
  392. unsigned int iov_len)
  393. {
  394. int32_t rc = qb_ipcs_response_sendv(conn, iov, iov_len);
  395. if (rc >= 0) {
  396. return 0;
  397. }
  398. return rc;
  399. }
  400. int cs_ipcs_response_send(void *conn, const void *msg, size_t mlen)
  401. {
  402. int32_t rc = qb_ipcs_response_send(conn, msg, mlen);
  403. if (rc >= 0) {
  404. return 0;
  405. }
  406. return rc;
  407. }
  408. static void outq_flush (void *data)
  409. {
  410. qb_ipcs_connection_t *conn = data;
  411. struct list_head *list, *list_next;
  412. struct outq_item *outq_item;
  413. int32_t rc;
  414. struct cs_ipcs_conn_context *context = qb_ipcs_context_get(conn);
  415. for (list = context->outq_head.next;
  416. list != &context->outq_head; list = list_next) {
  417. list_next = list->next;
  418. outq_item = list_entry (list, struct outq_item, list);
  419. rc = qb_ipcs_event_send(conn, outq_item->msg, outq_item->mlen);
  420. if (rc != outq_item->mlen) {
  421. break;
  422. }
  423. context->sent++;
  424. context->queued--;
  425. list_del (list);
  426. free (outq_item->msg);
  427. free (outq_item);
  428. }
  429. if (list_empty (&context->outq_head)) {
  430. context->queuing = QB_FALSE;
  431. log_printf(LOGSYS_LEVEL_INFO, "Q empty, queued:%d sent:%d.",
  432. context->queued, context->sent);
  433. context->queued = 0;
  434. context->sent = 0;
  435. return;
  436. }
  437. qb_loop_job_add(cs_poll_handle_get(), QB_LOOP_HIGH, conn, outq_flush);
  438. if (rc < 0 && rc != -EAGAIN) {
  439. log_printf(LOGSYS_LEVEL_ERROR, "event_send retuned %d!", rc);
  440. }
  441. }
  442. static void msg_send_or_queue(qb_ipcs_connection_t *conn, const struct iovec *iov, uint32_t iov_len)
  443. {
  444. int32_t rc = 0;
  445. int32_t i;
  446. int32_t bytes_msg = 0;
  447. struct outq_item *outq_item;
  448. char *write_buf = 0;
  449. struct cs_ipcs_conn_context *context = qb_ipcs_context_get(conn);
  450. for (i = 0; i < iov_len; i++) {
  451. bytes_msg += iov[i].iov_len;
  452. }
  453. if (!context->queuing) {
  454. assert(list_empty (&context->outq_head));
  455. rc = qb_ipcs_event_sendv(conn, iov, iov_len);
  456. if (rc == bytes_msg) {
  457. context->sent++;
  458. return;
  459. }
  460. if (rc == -EAGAIN) {
  461. context->queued = 0;
  462. context->sent = 0;
  463. context->queuing = QB_TRUE;
  464. qb_loop_job_add(cs_poll_handle_get(), QB_LOOP_HIGH, conn, outq_flush);
  465. } else {
  466. log_printf(LOGSYS_LEVEL_ERROR, "event_send retuned %d, expected %d!", rc, bytes_msg);
  467. return;
  468. }
  469. }
  470. outq_item = malloc (sizeof (struct outq_item));
  471. if (outq_item == NULL) {
  472. qb_ipcs_disconnect(conn);
  473. return;
  474. }
  475. outq_item->msg = malloc (bytes_msg);
  476. if (outq_item->msg == NULL) {
  477. free (outq_item);
  478. qb_ipcs_disconnect(conn);
  479. return;
  480. }
  481. write_buf = outq_item->msg;
  482. for (i = 0; i < iov_len; i++) {
  483. memcpy (write_buf, iov[i].iov_base, iov[i].iov_len);
  484. write_buf += iov[i].iov_len;
  485. }
  486. outq_item->mlen = bytes_msg;
  487. list_init (&outq_item->list);
  488. list_add_tail (&outq_item->list, &context->outq_head);
  489. context->queued++;
  490. }
  491. int cs_ipcs_dispatch_send(void *conn, const void *msg, size_t mlen)
  492. {
  493. struct iovec iov;
  494. iov.iov_base = (void *)msg;
  495. iov.iov_len = mlen;
  496. msg_send_or_queue (conn, &iov, 1);
  497. return 0;
  498. }
  499. int cs_ipcs_dispatch_iov_send (void *conn,
  500. const struct iovec *iov,
  501. unsigned int iov_len)
  502. {
  503. msg_send_or_queue(conn, iov, iov_len);
  504. return 0;
  505. }
  506. static int32_t cs_ipcs_msg_process(qb_ipcs_connection_t *c,
  507. void *data, size_t size)
  508. {
  509. struct qb_ipc_response_header response;
  510. struct qb_ipc_request_header *request_pt = (struct qb_ipc_request_header *)data;
  511. int32_t service = qb_ipcs_service_id_get(c);
  512. int32_t send_ok = 0;
  513. int32_t is_async_call = QB_FALSE;
  514. ssize_t res = -1;
  515. int sending_allowed_private_data;
  516. struct cs_ipcs_conn_context *cnx;
  517. send_ok = corosync_sending_allowed (service,
  518. request_pt->id,
  519. request_pt,
  520. &sending_allowed_private_data);
  521. is_async_call = (service == CPG_SERVICE && request_pt->id == 2);
  522. /*
  523. * This happens when the message contains some kind of invalid
  524. * parameter, such as an invalid size
  525. */
  526. if (send_ok == -EINVAL) {
  527. response.size = sizeof (response);
  528. response.id = 0;
  529. response.error = CS_ERR_INVALID_PARAM;
  530. cnx = qb_ipcs_context_get(c);
  531. if (cnx) {
  532. cnx->invalid_request++;
  533. }
  534. if (is_async_call) {
  535. log_printf(LOGSYS_LEVEL_INFO, "*** %s() invalid message! size:%d error:%d",
  536. __func__, response.size, response.error);
  537. } else {
  538. qb_ipcs_response_send (c,
  539. &response,
  540. sizeof (response));
  541. }
  542. res = -EINVAL;
  543. } else if (send_ok < 0) {
  544. cnx = qb_ipcs_context_get(c);
  545. if (cnx) {
  546. cnx->overload++;
  547. }
  548. if (!is_async_call) {
  549. /*
  550. * Overload, tell library to retry
  551. */
  552. response.size = sizeof (response);
  553. response.id = 0;
  554. response.error = CS_ERR_TRY_AGAIN;
  555. qb_ipcs_response_send (c,
  556. &response,
  557. sizeof (response));
  558. } else {
  559. log_printf(LOGSYS_LEVEL_WARNING,
  560. "*** %s() (%d:%d - %d) %s!",
  561. __func__, service, request_pt->id,
  562. is_async_call, strerror(-send_ok));
  563. }
  564. res = -ENOBUFS;
  565. }
  566. if (send_ok) {
  567. corosync_service[service]->lib_engine[request_pt->id].lib_handler_fn(c, request_pt);
  568. res = 0;
  569. }
  570. corosync_sending_allowed_release (&sending_allowed_private_data);
  571. return res;
  572. }
  573. static int32_t cs_ipcs_job_add(enum qb_loop_priority p, void *data, qb_loop_job_dispatch_fn fn)
  574. {
  575. return qb_loop_job_add(cs_poll_handle_get(), p, data, fn);
  576. }
  577. static int32_t cs_ipcs_dispatch_add(enum qb_loop_priority p, int32_t fd, int32_t events,
  578. void *data, qb_ipcs_dispatch_fn_t fn)
  579. {
  580. return qb_loop_poll_add(cs_poll_handle_get(), p, fd, events, data, fn);
  581. }
  582. static int32_t cs_ipcs_dispatch_mod(enum qb_loop_priority p, int32_t fd, int32_t events,
  583. void *data, qb_ipcs_dispatch_fn_t fn)
  584. {
  585. return qb_loop_poll_mod(cs_poll_handle_get(), p, fd, events, data, fn);
  586. }
  587. static int32_t cs_ipcs_dispatch_del(int32_t fd)
  588. {
  589. return qb_loop_poll_del(cs_poll_handle_get(), fd);
  590. }
  591. static void cs_ipcs_low_fds_event(int32_t not_enough, int32_t fds_available)
  592. {
  593. ipc_not_enough_fds_left = not_enough;
  594. if (not_enough) {
  595. log_printf(LOGSYS_LEVEL_WARNING, "refusing new connections (fds_available:%d)\n",
  596. fds_available);
  597. } else {
  598. log_printf(LOGSYS_LEVEL_NOTICE, "allowing new connections (fds_available:%d)\n",
  599. fds_available);
  600. }
  601. }
  602. int32_t cs_ipcs_q_level_get(void)
  603. {
  604. return ipc_fc_totem_queue_level;
  605. }
  606. static qb_loop_timer_handle ipcs_check_for_flow_control_timer;
  607. static void cs_ipcs_check_for_flow_control(void)
  608. {
  609. int32_t i;
  610. int32_t fc_enabled;
  611. for (i = 0; i < SERVICE_HANDLER_MAXIMUM_COUNT; i++) {
  612. if (corosync_service[i] == NULL || ipcs_mapper[i].inst == NULL) {
  613. continue;
  614. }
  615. fc_enabled = QB_IPCS_RATE_OFF;
  616. if (ipc_fc_is_quorate == 1 ||
  617. corosync_service[i]->allow_inquorate == CS_LIB_ALLOW_INQUORATE) {
  618. /*
  619. * we are quorate
  620. * now check flow control
  621. */
  622. if (ipc_fc_totem_queue_level != TOTEM_Q_LEVEL_CRITICAL &&
  623. ipc_fc_sync_in_process == 0) {
  624. fc_enabled = QB_FALSE;
  625. } else {
  626. fc_enabled = QB_IPCS_RATE_OFF_2;
  627. }
  628. }
  629. if (fc_enabled) {
  630. qb_ipcs_request_rate_limit(ipcs_mapper[i].inst, fc_enabled);
  631. qb_loop_timer_add(cs_poll_handle_get(), QB_LOOP_MED, 1*QB_TIME_NS_IN_MSEC,
  632. NULL, corosync_recheck_the_q_level, &ipcs_check_for_flow_control_timer);
  633. } else if (ipc_fc_totem_queue_level == TOTEM_Q_LEVEL_LOW) {
  634. qb_ipcs_request_rate_limit(ipcs_mapper[i].inst, QB_IPCS_RATE_FAST);
  635. } else if (ipc_fc_totem_queue_level == TOTEM_Q_LEVEL_GOOD) {
  636. qb_ipcs_request_rate_limit(ipcs_mapper[i].inst, QB_IPCS_RATE_NORMAL);
  637. } else if (ipc_fc_totem_queue_level == TOTEM_Q_LEVEL_HIGH) {
  638. qb_ipcs_request_rate_limit(ipcs_mapper[i].inst, QB_IPCS_RATE_SLOW);
  639. }
  640. }
  641. }
  642. static void cs_ipcs_fc_quorum_changed(int quorate, void *context)
  643. {
  644. ipc_fc_is_quorate = quorate;
  645. cs_ipcs_check_for_flow_control();
  646. }
  647. static void cs_ipcs_totem_queue_level_changed(enum totem_q_level level)
  648. {
  649. ipc_fc_totem_queue_level = level;
  650. cs_ipcs_check_for_flow_control();
  651. }
  652. void cs_ipcs_sync_state_changed(int32_t sync_in_process)
  653. {
  654. ipc_fc_sync_in_process = sync_in_process;
  655. cs_ipcs_check_for_flow_control();
  656. }
  657. void cs_ipcs_stats_update(void)
  658. {
  659. int32_t i;
  660. struct qb_ipcs_stats srv_stats;
  661. struct qb_ipcs_connection_stats stats;
  662. qb_ipcs_connection_t *c;
  663. struct cs_ipcs_conn_context *cnx;
  664. char key_name[ICMAP_KEYNAME_MAXLEN];
  665. for (i = 0; i < SERVICE_HANDLER_MAXIMUM_COUNT; i++) {
  666. if (corosync_service[i] == NULL || ipcs_mapper[i].inst == NULL) {
  667. continue;
  668. }
  669. qb_ipcs_stats_get(ipcs_mapper[i].inst, &srv_stats, QB_FALSE);
  670. for (c = qb_ipcs_connection_first_get(ipcs_mapper[i].inst); c;
  671. c = qb_ipcs_connection_next_get(ipcs_mapper[i].inst, c)) {
  672. cnx = qb_ipcs_context_get(c);
  673. if (cnx == NULL) continue;
  674. qb_ipcs_connection_stats_get(c, &stats, QB_FALSE);
  675. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s.client_pid", cnx->icmap_path);
  676. icmap_set_uint32(key_name, stats.client_pid);
  677. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s.requests", cnx->icmap_path);
  678. icmap_set_uint64(key_name, stats.requests);
  679. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s.responses", cnx->icmap_path);
  680. icmap_set_uint64(key_name, stats.responses);
  681. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s.dispatched", cnx->icmap_path);
  682. icmap_set_uint64(key_name, stats.events);
  683. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s.send_retries", cnx->icmap_path);
  684. icmap_set_uint64(key_name, stats.send_retries);
  685. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s.recv_retries", cnx->icmap_path);
  686. icmap_set_uint64(key_name, stats.recv_retries);
  687. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s.flow_control", cnx->icmap_path);
  688. icmap_set_uint32(key_name, stats.flow_control_state);
  689. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s.flow_control_count", cnx->icmap_path);
  690. icmap_set_uint64(key_name, stats.flow_control_count);
  691. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s.queue_size", cnx->icmap_path);
  692. icmap_set_uint32(key_name, cnx->queued);
  693. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s.invalid_request", cnx->icmap_path);
  694. icmap_set_uint64(key_name, cnx->invalid_request);
  695. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s.overload", cnx->icmap_path);
  696. icmap_set_uint64(key_name, cnx->overload);
  697. qb_ipcs_connection_unref(c);
  698. }
  699. }
  700. }
  701. void cs_ipcs_service_init(struct corosync_service_engine *service)
  702. {
  703. if (service->lib_engine_count == 0) {
  704. log_printf (LOGSYS_LEVEL_DEBUG,
  705. "NOT Initializing IPC on %s [%d]",
  706. cs_ipcs_serv_short_name(service->id),
  707. service->id);
  708. return;
  709. }
  710. ipcs_mapper[service->id].id = service->id;
  711. strcpy(ipcs_mapper[service->id].name, cs_ipcs_serv_short_name(service->id));
  712. log_printf (LOGSYS_LEVEL_DEBUG,
  713. "Initializing IPC on %s [%d]",
  714. ipcs_mapper[service->id].name,
  715. ipcs_mapper[service->id].id);
  716. ipcs_mapper[service->id].inst = qb_ipcs_create(ipcs_mapper[service->id].name,
  717. ipcs_mapper[service->id].id,
  718. QB_IPC_SHM,
  719. &corosync_service_funcs);
  720. assert(ipcs_mapper[service->id].inst);
  721. qb_ipcs_poll_handlers_set(ipcs_mapper[service->id].inst,
  722. &corosync_poll_funcs);
  723. qb_ipcs_run(ipcs_mapper[service->id].inst);
  724. }
  725. void cs_ipcs_init(void)
  726. {
  727. api = apidef_get ();
  728. qb_loop_poll_low_fds_event_set(cs_poll_handle_get(), cs_ipcs_low_fds_event);
  729. api->quorum_register_callback (cs_ipcs_fc_quorum_changed, NULL);
  730. totempg_queue_level_register_callback (cs_ipcs_totem_queue_level_changed);
  731. icmap_set_ro_access("runtime.connections.", 1, 1);
  732. icmap_set_uint64("runtime.connections.active", 0);
  733. icmap_set_uint64("runtime.connections.closed", 0);
  734. }