4
0

ipc_glue.c 24 KB

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