ipc_glue.c 23 KB

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