ipc_glue.c 23 KB

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