ipc_glue.c 21 KB

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