ipc.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272
  1. /*
  2. * Copyright (c) 2002-2006 MontaVista Software, Inc.
  3. * Copyright (c) 2006 Red Hat, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Author: Steven Dake (sdake@mvista.com)
  8. *
  9. * This software licensed under BSD license, the text of which follows:
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above copyright notice,
  15. * this list of conditions and the following disclaimer.
  16. * - Redistributions in binary form must reproduce the above copyright notice,
  17. * this list of conditions and the following disclaimer in the documentation
  18. * and/or other materials provided with the distribution.
  19. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  20. * contributors may be used to endorse or promote products derived from this
  21. * software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  33. * THE POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. #include <pthread.h>
  36. #include <assert.h>
  37. #include <pwd.h>
  38. #include <grp.h>
  39. #include <sys/types.h>
  40. #include <sys/poll.h>
  41. #include <sys/uio.h>
  42. #include <sys/mman.h>
  43. #include <sys/socket.h>
  44. #include <sys/un.h>
  45. #include <sys/time.h>
  46. #include <sys/resource.h>
  47. #include <netinet/in.h>
  48. #include <arpa/inet.h>
  49. #include <unistd.h>
  50. #include <fcntl.h>
  51. #include <stdlib.h>
  52. #include <stdio.h>
  53. #include <errno.h>
  54. #include <signal.h>
  55. #include <sched.h>
  56. #include <time.h>
  57. #include "../include/saAis.h"
  58. #include "../include/list.h"
  59. #include "../include/queue.h"
  60. #include "../lcr/lcr_ifact.h"
  61. #include "poll.h"
  62. #include "totempg.h"
  63. #include "totemsrp.h"
  64. #include "mempool.h"
  65. #include "mainconfig.h"
  66. #include "totemconfig.h"
  67. #include "main.h"
  68. #include "ipc.h"
  69. #include "flow.h"
  70. #include "service.h"
  71. #include "sync.h"
  72. #include "swab.h"
  73. #include "objdb.h"
  74. #include "config.h"
  75. #include "tlist.h"
  76. #define LOG_SERVICE LOG_SERVICE_IPC
  77. #include "print.h"
  78. #include "util.h"
  79. #define SERVER_BACKLOG 5
  80. /*
  81. * When there are this many entries left in a queue, turn on flow control
  82. */
  83. #define FLOW_CONTROL_ENTRIES_ENABLE 400
  84. /*
  85. * When there are this many entries in a queue, turn off flow control
  86. */
  87. #define FLOW_CONTROL_ENTRIES_DISABLE 64
  88. static unsigned int g_gid_valid = 0;
  89. static struct totem_ip_address *my_ip;
  90. static totempg_groups_handle ipc_handle;
  91. DECLARE_LIST_INIT (conn_info_list_head);
  92. static void (*ipc_serialize_lock_fn) (void);
  93. static void (*ipc_serialize_unlock_fn) (void);
  94. struct outq_item {
  95. void *msg;
  96. size_t mlen;
  97. };
  98. enum conn_state {
  99. CONN_STATE_ACTIVE,
  100. CONN_STATE_SECURITY,
  101. CONN_STATE_REQUESTED,
  102. CONN_STATE_CLOSED,
  103. CONN_STATE_DISCONNECTED
  104. };
  105. struct conn_info {
  106. int fd; /* File descriptor */
  107. unsigned int events; /* events polled for by file descriptor */
  108. enum conn_state state; /* State of this connection */
  109. pthread_t thread; /* thread identifier */
  110. pthread_attr_t thread_attr; /* thread attribute */
  111. char *inb; /* Input buffer for non-blocking reads */
  112. int inb_nextheader; /* Next message header starts here */
  113. int inb_start; /* Start location of input buffer */
  114. int inb_inuse; /* Bytes currently stored in input buffer */
  115. struct queue outq; /* Circular queue for outgoing requests */
  116. int byte_start; /* Byte to start sending from in head of queue */
  117. enum service_types service;/* Type of service so dispatch knows how to route message */
  118. int authenticated; /* Is this connection authenticated? */
  119. void *private_data; /* library connection private data */
  120. struct conn_info *conn_info_partner; /* partner connection dispatch<->response */
  121. unsigned int flow_control_handle; /* flow control identifier */
  122. unsigned int flow_control_enabled; /* flow control enabled bit */
  123. unsigned int flow_control_local_count; /* flow control local count */
  124. enum openais_flow_control flow_control; /* Does this service use IPC flow control */
  125. pthread_mutex_t flow_control_mutex;
  126. int (*lib_exit_fn) (void *conn);
  127. struct timerlist timerlist;
  128. pthread_mutex_t mutex;
  129. pthread_mutex_t *shared_mutex;
  130. struct list_head list;
  131. };
  132. static void *prioritized_poll_thread (void *conn);
  133. static int conn_info_outq_flush (struct conn_info *conn_info);
  134. static void libais_deliver (struct conn_info *conn_info);
  135. static void ipc_flow_control (struct conn_info *conn_info);
  136. /*
  137. * IPC Initializers
  138. */
  139. static int response_init_send_response (
  140. struct conn_info *conn_info,
  141. void *message);
  142. static int dispatch_init_send_response (
  143. struct conn_info *conn_info,
  144. void *message);
  145. static int (*ais_init_service[]) (struct conn_info *conn_info, void *message) = {
  146. response_init_send_response,
  147. dispatch_init_send_response
  148. };
  149. static void libais_disconnect_security (struct conn_info *conn_info)
  150. {
  151. conn_info->state = CONN_STATE_SECURITY;
  152. close (conn_info->fd);
  153. }
  154. static int response_init_send_response (
  155. struct conn_info *conn_info,
  156. void *message)
  157. {
  158. SaAisErrorT error = SA_AIS_ERR_ACCESS;
  159. size_t cinfo = (size_t)conn_info;
  160. mar_req_lib_response_init_t *req_lib_response_init = (mar_req_lib_response_init_t *)message;
  161. mar_res_lib_response_init_t res_lib_response_init;
  162. if (conn_info->authenticated) {
  163. conn_info->service = req_lib_response_init->resdis_header.service;
  164. error = SA_AIS_OK;
  165. }
  166. res_lib_response_init.header.size = sizeof (mar_res_lib_response_init_t);
  167. res_lib_response_init.header.id = MESSAGE_RES_INIT;
  168. res_lib_response_init.header.error = error;
  169. res_lib_response_init.conn_info = (mar_uint64_t)cinfo;
  170. openais_conn_send_response (
  171. conn_info,
  172. &res_lib_response_init,
  173. sizeof (res_lib_response_init));
  174. if (error == SA_AIS_ERR_ACCESS) {
  175. libais_disconnect_security (conn_info);
  176. return (-1);
  177. }
  178. return (0);
  179. }
  180. static int dispatch_init_send_response (
  181. struct conn_info *conn_info,
  182. void *message)
  183. {
  184. SaAisErrorT error = SA_AIS_ERR_ACCESS;
  185. size_t cinfo;
  186. mar_req_lib_dispatch_init_t *req_lib_dispatch_init = (mar_req_lib_dispatch_init_t *)message;
  187. mar_res_lib_dispatch_init_t res_lib_dispatch_init;
  188. struct conn_info *msg_conn_info;
  189. if (conn_info->authenticated) {
  190. conn_info->service = req_lib_dispatch_init->resdis_header.service;
  191. if (!ais_service[req_lib_dispatch_init->resdis_header.service])
  192. error = SA_AIS_ERR_NOT_SUPPORTED;
  193. else
  194. error = SA_AIS_OK;
  195. cinfo = (size_t)req_lib_dispatch_init->conn_info;
  196. conn_info->conn_info_partner = (struct conn_info *)cinfo;
  197. conn_info->conn_info_partner->shared_mutex = conn_info->shared_mutex;
  198. msg_conn_info = (struct conn_info *)cinfo;
  199. msg_conn_info->conn_info_partner = conn_info;
  200. if (error == SA_AIS_OK) {
  201. int private_data_size;
  202. private_data_size = ais_service[req_lib_dispatch_init->resdis_header.service]->private_data_size;
  203. if (private_data_size) {
  204. conn_info->private_data = malloc (private_data_size);
  205. conn_info->conn_info_partner->private_data = conn_info->private_data;
  206. if (conn_info->private_data == NULL) {
  207. error = SA_AIS_ERR_NO_MEMORY;
  208. } else {
  209. memset (conn_info->private_data, 0, private_data_size);
  210. }
  211. } else {
  212. conn_info->private_data = NULL;
  213. conn_info->conn_info_partner->private_data = NULL;
  214. }
  215. }
  216. }
  217. res_lib_dispatch_init.header.size = sizeof (mar_res_lib_dispatch_init_t);
  218. res_lib_dispatch_init.header.id = MESSAGE_RES_INIT;
  219. res_lib_dispatch_init.header.error = error;
  220. openais_conn_send_response (
  221. conn_info,
  222. &res_lib_dispatch_init,
  223. sizeof (res_lib_dispatch_init));
  224. if (error == SA_AIS_ERR_ACCESS) {
  225. libais_disconnect_security (conn_info);
  226. return (-1);
  227. }
  228. if (error != SA_AIS_OK) {
  229. return (-1);
  230. }
  231. conn_info->state = CONN_STATE_ACTIVE;
  232. conn_info->conn_info_partner->state = CONN_STATE_ACTIVE;
  233. conn_info->lib_exit_fn = ais_service[conn_info->service]->lib_exit_fn;
  234. ais_service[conn_info->service]->lib_init_fn (conn_info);
  235. conn_info->flow_control = ais_service[conn_info->service]->flow_control;
  236. conn_info->conn_info_partner->flow_control = ais_service[conn_info->service]->flow_control;
  237. if (ais_service[conn_info->service]->flow_control == OPENAIS_FLOW_CONTROL_REQUIRED) {
  238. openais_flow_control_ipc_init (
  239. &conn_info->flow_control_handle,
  240. conn_info->service);
  241. }
  242. return (0);
  243. }
  244. /*
  245. * Create a connection data structure
  246. */
  247. static inline unsigned int conn_info_create (int fd) {
  248. struct conn_info *conn_info;
  249. int res;
  250. conn_info = malloc (sizeof (struct conn_info));
  251. if (conn_info == 0) {
  252. return (ENOMEM);
  253. }
  254. memset (conn_info, 0, sizeof (struct conn_info));
  255. res = queue_init (&conn_info->outq, SIZEQUEUE,
  256. sizeof (struct outq_item));
  257. if (res != 0) {
  258. free (conn_info);
  259. return (ENOMEM);
  260. }
  261. conn_info->inb = malloc (sizeof (char) * SIZEINB);
  262. if (conn_info->inb == NULL) {
  263. queue_free (&conn_info->outq);
  264. free (conn_info);
  265. return (ENOMEM);
  266. }
  267. conn_info->shared_mutex = malloc (sizeof (pthread_mutex_t));
  268. if (conn_info->shared_mutex == NULL) {
  269. free (conn_info->inb);
  270. queue_free (&conn_info->outq);
  271. free (conn_info);
  272. return (ENOMEM);
  273. }
  274. pthread_mutex_init (&conn_info->mutex, NULL);
  275. pthread_mutex_init (&conn_info->flow_control_mutex, NULL);
  276. pthread_mutex_init (conn_info->shared_mutex, NULL);
  277. conn_info->state = CONN_STATE_ACTIVE;
  278. conn_info->fd = fd;
  279. conn_info->events = POLLIN|POLLNVAL;
  280. conn_info->service = SOCKET_SERVICE_INIT;
  281. list_init (&conn_info->list);
  282. list_add (&conn_info_list_head, &conn_info->list);
  283. pthread_attr_init (&conn_info->thread_attr);
  284. pthread_attr_setstacksize (&conn_info->thread_attr, 200000);
  285. pthread_attr_setdetachstate (&conn_info->thread_attr, PTHREAD_CREATE_DETACHED);
  286. res = pthread_create (&conn_info->thread, &conn_info->thread_attr,
  287. prioritized_poll_thread, conn_info);
  288. return (res);
  289. }
  290. static void conn_info_destroy (struct conn_info *conn_info)
  291. {
  292. struct outq_item *outq_item;
  293. /*
  294. * Free the outq queued items
  295. */
  296. while (!queue_is_empty (&conn_info->outq)) {
  297. outq_item = queue_item_get (&conn_info->outq);
  298. free (outq_item->msg);
  299. queue_item_remove (&conn_info->outq);
  300. }
  301. queue_free (&conn_info->outq);
  302. free (conn_info->inb);
  303. if (conn_info->conn_info_partner) {
  304. conn_info->conn_info_partner->conn_info_partner = NULL;
  305. }
  306. list_del (&conn_info->list);
  307. free (conn_info);
  308. }
  309. static int libais_connection_active (struct conn_info *conn_info)
  310. {
  311. return (conn_info->state == CONN_STATE_ACTIVE);
  312. }
  313. static void libais_disconnect_request (struct conn_info *conn_info)
  314. {
  315. if (conn_info->state == CONN_STATE_ACTIVE) {
  316. conn_info->state = CONN_STATE_REQUESTED;
  317. conn_info->conn_info_partner->state = CONN_STATE_REQUESTED;
  318. }
  319. }
  320. static int libais_disconnect (struct conn_info *conn_info)
  321. {
  322. int res = 0;
  323. assert (conn_info->state != CONN_STATE_ACTIVE);
  324. if (conn_info->state == CONN_STATE_DISCONNECTED) {
  325. assert (0);
  326. }
  327. /*
  328. * Close active connections
  329. */
  330. if (conn_info->state == CONN_STATE_ACTIVE || conn_info->state == CONN_STATE_REQUESTED) {
  331. close (conn_info->fd);
  332. conn_info->state = CONN_STATE_CLOSED;
  333. close (conn_info->conn_info_partner->fd);
  334. conn_info->conn_info_partner->state = CONN_STATE_CLOSED;
  335. }
  336. /*
  337. * Note we will only call the close operation once on the first time
  338. * one of the connections is closed
  339. */
  340. if (conn_info->state == CONN_STATE_CLOSED) {
  341. if (conn_info->lib_exit_fn) {
  342. res = conn_info->lib_exit_fn (conn_info);
  343. }
  344. if (res == -1) {
  345. return (-1);
  346. }
  347. if (conn_info->conn_info_partner->lib_exit_fn) {
  348. res = conn_info->conn_info_partner->lib_exit_fn (conn_info);
  349. }
  350. if (res == -1) {
  351. return (-1);
  352. }
  353. }
  354. conn_info->state = CONN_STATE_DISCONNECTED;
  355. conn_info->conn_info_partner->state = CONN_STATE_DISCONNECTED;
  356. if (conn_info->flow_control_enabled == 1) {
  357. openais_flow_control_disable (conn_info->flow_control_handle);
  358. }
  359. return (0);
  360. }
  361. static inline void conn_info_mutex_lock (
  362. struct conn_info *conn_info,
  363. unsigned int service)
  364. {
  365. if (service == SOCKET_SERVICE_INIT) {
  366. pthread_mutex_lock (&conn_info->mutex);
  367. } else {
  368. pthread_mutex_lock (conn_info->shared_mutex);
  369. }
  370. }
  371. static inline void conn_info_mutex_unlock (
  372. struct conn_info *conn_info,
  373. unsigned int service)
  374. {
  375. if (service == SOCKET_SERVICE_INIT) {
  376. pthread_mutex_unlock (&conn_info->mutex);
  377. } else {
  378. pthread_mutex_unlock (conn_info->shared_mutex);
  379. }
  380. }
  381. /*
  382. * This thread runs in a specific thread priority mode to handle
  383. * I/O requests from the library
  384. */
  385. static void *prioritized_poll_thread (void *conn)
  386. {
  387. struct conn_info *conn_info = (struct conn_info *)conn;
  388. struct pollfd ufd;
  389. int fds;
  390. struct sched_param sched_param;
  391. int res;
  392. pthread_mutex_t *rel_mutex;
  393. unsigned int service;
  394. struct conn_info *cinfo_partner;
  395. sched_param.sched_priority = 1;
  396. res = pthread_setschedparam (conn_info->thread, SCHED_RR, &sched_param);
  397. ufd.fd = conn_info->fd;
  398. for (;;) {
  399. retry_poll:
  400. service = conn_info->service;
  401. ufd.events = conn_info->events;
  402. ufd.revents = 0;
  403. fds = poll (&ufd, 1, -1);
  404. conn_info_mutex_lock (conn_info, service);
  405. switch (conn_info->state) {
  406. case CONN_STATE_SECURITY:
  407. conn_info_mutex_unlock (conn_info, service);
  408. free (conn_info->shared_mutex);
  409. conn_info_destroy (conn);
  410. pthread_exit (0);
  411. break;
  412. case CONN_STATE_REQUESTED:
  413. case CONN_STATE_CLOSED:
  414. res = libais_disconnect (conn);
  415. if (res != 0) {
  416. conn_info_mutex_unlock (conn_info, service);
  417. goto retry_poll;
  418. }
  419. break;
  420. case CONN_STATE_DISCONNECTED:
  421. rel_mutex = conn_info->shared_mutex;
  422. cinfo_partner = conn_info->conn_info_partner;
  423. conn_info_destroy (conn);
  424. if (service == SOCKET_SERVICE_INIT) {
  425. pthread_mutex_unlock (&conn_info->mutex);
  426. } else {
  427. pthread_mutex_unlock (rel_mutex);
  428. }
  429. if (cinfo_partner == NULL) {
  430. free (rel_mutex);
  431. }
  432. pthread_exit (0);
  433. /*
  434. * !! NOTE !! this is the exit point for this thread
  435. */
  436. break;
  437. default:
  438. break;
  439. }
  440. if (fds == -1) {
  441. conn_info_mutex_unlock (conn_info, service);
  442. goto retry_poll;
  443. }
  444. ipc_serialize_lock_fn ();
  445. if (fds == 1 && ufd.revents) {
  446. if (ufd.revents & (POLLERR|POLLHUP)) {
  447. libais_disconnect_request (conn_info);
  448. conn_info_mutex_unlock (conn_info, service);
  449. ipc_serialize_unlock_fn ();
  450. continue;
  451. }
  452. if (ufd.revents & POLLOUT) {
  453. conn_info_outq_flush (conn_info);
  454. }
  455. if ((ufd.revents & POLLIN) == POLLIN) {
  456. libais_deliver (conn_info);
  457. }
  458. ipc_flow_control (conn_info);
  459. }
  460. ipc_serialize_unlock_fn ();
  461. conn_info_mutex_unlock (conn_info, service);
  462. }
  463. /*
  464. * This code never reached
  465. */
  466. return (0);
  467. }
  468. #if defined(OPENAIS_LINUX)
  469. /* SUN_LEN is broken for abstract namespace
  470. */
  471. #define AIS_SUN_LEN(a) sizeof(*(a))
  472. char *socketname = "libais.socket";
  473. #else
  474. #define AIS_SUN_LEN(a) SUN_LEN(a)
  475. char *socketname = "/var/run/libais.socket";
  476. #endif
  477. static void ipc_flow_control (struct conn_info *conn_info)
  478. {
  479. unsigned int entries_used;
  480. unsigned int entries_usedhw;
  481. entries_used = queue_used (&conn_info->outq);
  482. if (conn_info->flow_control_local_count > entries_used) {
  483. entries_used = conn_info->flow_control_local_count;
  484. }
  485. /*
  486. * IPC group-wide flow control
  487. */
  488. if (conn_info->flow_control == OPENAIS_FLOW_CONTROL_REQUIRED) {
  489. if (conn_info->flow_control_enabled == 0 &&
  490. ((entries_used + FLOW_CONTROL_ENTRIES_ENABLE) > SIZEQUEUE)) {
  491. entries_usedhw = queue_usedhw (&conn_info->outq);
  492. log_printf (LOG_LEVEL_NOTICE, "Enabling flow control - HW mark %d of %d %p.\n", entries_usedhw, SIZEQUEUE, &conn_info->outq);
  493. openais_flow_control_enable (conn_info->flow_control_handle);
  494. conn_info->flow_control_enabled = 1;
  495. conn_info->conn_info_partner->flow_control_enabled = 1;
  496. }
  497. if (conn_info->flow_control_enabled == 1 &&
  498. entries_used <= FLOW_CONTROL_ENTRIES_DISABLE) {
  499. entries_usedhw = queue_usedhw (&conn_info->outq);
  500. log_printf (LOG_LEVEL_NOTICE, "Disabling flow control - HW mark [%d/%d].\n",
  501. entries_usedhw, SIZEQUEUE);
  502. openais_flow_control_disable (conn_info->flow_control_handle);
  503. conn_info->flow_control_enabled = 0;
  504. conn_info->conn_info_partner->flow_control_enabled = 0;
  505. }
  506. }
  507. }
  508. static int conn_info_outq_flush (struct conn_info *conn_info) {
  509. struct queue *outq;
  510. int res = 0;
  511. struct outq_item *queue_item;
  512. struct msghdr msg_send;
  513. struct iovec iov_send;
  514. char *msg_addr;
  515. if (!libais_connection_active (conn_info)) {
  516. return (-1);
  517. }
  518. outq = &conn_info->outq;
  519. msg_send.msg_iov = &iov_send;
  520. msg_send.msg_name = 0;
  521. msg_send.msg_namelen = 0;
  522. msg_send.msg_iovlen = 1;
  523. msg_send.msg_control = 0;
  524. msg_send.msg_controllen = 0;
  525. msg_send.msg_flags = 0;
  526. while (!queue_is_empty (outq)) {
  527. queue_item = queue_item_get (outq);
  528. msg_addr = (char *)queue_item->msg;
  529. msg_addr = &msg_addr[conn_info->byte_start];
  530. iov_send.iov_base = msg_addr;
  531. iov_send.iov_len = queue_item->mlen - conn_info->byte_start;
  532. retry_sendmsg:
  533. res = sendmsg (conn_info->fd, &msg_send, MSG_NOSIGNAL);
  534. if (res == -1 && errno == EINTR) {
  535. goto retry_sendmsg;
  536. }
  537. if (res == -1 && errno == EAGAIN) {
  538. return (0);
  539. }
  540. if (res == -1 && errno == EPIPE) {
  541. libais_disconnect_request (conn_info);
  542. return (0);
  543. }
  544. if (res == -1) {
  545. printf ("ERRNO is %d\n", errno);
  546. assert (0); /* some other unhandled error here */
  547. }
  548. if (res + conn_info->byte_start != queue_item->mlen) {
  549. conn_info->byte_start += res;
  550. return (0);
  551. }
  552. /*
  553. * Message sent, try sending another message
  554. */
  555. queue_item_remove (outq);
  556. conn_info->byte_start = 0;
  557. free (queue_item->msg);
  558. } /* while queue not empty */
  559. if (queue_is_empty (outq)) {
  560. conn_info->events = POLLIN|POLLNVAL;
  561. }
  562. return (0);
  563. }
  564. struct res_overlay {
  565. mar_res_header_t header __attribute((aligned(8)));
  566. char buf[4096];
  567. };
  568. static void libais_deliver (struct conn_info *conn_info)
  569. {
  570. int res;
  571. mar_req_header_t *header;
  572. int service;
  573. struct msghdr msg_recv;
  574. struct iovec iov_recv;
  575. #ifdef OPENAIS_LINUX
  576. struct cmsghdr *cmsg;
  577. char cmsg_cred[CMSG_SPACE (sizeof (struct ucred))];
  578. struct ucred *cred;
  579. int on = 0;
  580. #else
  581. uid_t euid;
  582. gid_t egid;
  583. #endif
  584. int send_ok = 0;
  585. int send_ok_joined = 0;
  586. struct iovec send_ok_joined_iovec;
  587. struct res_overlay res_overlay;
  588. msg_recv.msg_iov = &iov_recv;
  589. msg_recv.msg_iovlen = 1;
  590. msg_recv.msg_name = 0;
  591. msg_recv.msg_namelen = 0;
  592. msg_recv.msg_flags = 0;
  593. if (conn_info->authenticated) {
  594. msg_recv.msg_control = 0;
  595. msg_recv.msg_controllen = 0;
  596. } else {
  597. #ifdef OPENAIS_LINUX
  598. msg_recv.msg_control = (void *)cmsg_cred;
  599. msg_recv.msg_controllen = sizeof (cmsg_cred);
  600. #else
  601. euid = -1; egid = -1;
  602. if (getpeereid(conn_info->fd, &euid, &egid) != -1 &&
  603. (euid == 0 || egid == g_gid_valid)) {
  604. conn_info->authenticated = 1;
  605. }
  606. if (conn_info->authenticated == 0) {
  607. log_printf (LOG_LEVEL_SECURITY, "Connection not authenticated because gid is %d, expecting %d\n", egid, g_gid_valid);
  608. }
  609. #endif
  610. }
  611. iov_recv.iov_base = &conn_info->inb[conn_info->inb_start];
  612. iov_recv.iov_len = (SIZEINB) - conn_info->inb_start;
  613. if (conn_info->inb_inuse == SIZEINB) {
  614. return;
  615. }
  616. retry_recv:
  617. res = recvmsg (conn_info->fd, &msg_recv, MSG_NOSIGNAL);
  618. if (res == -1 && errno == EINTR) {
  619. goto retry_recv;
  620. } else
  621. if (res == -1 && errno != EAGAIN) {
  622. return;
  623. } else
  624. if (res == 0) {
  625. return;
  626. }
  627. /*
  628. * Authenticate if this connection has not been authenticated
  629. */
  630. #ifdef OPENAIS_LINUX
  631. if (conn_info->authenticated == 0) {
  632. cmsg = CMSG_FIRSTHDR (&msg_recv);
  633. assert (cmsg);
  634. cred = (struct ucred *)CMSG_DATA (cmsg);
  635. if (cred) {
  636. if (cred->uid == 0 || cred->gid == g_gid_valid) {
  637. setsockopt(conn_info->fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof (on));
  638. conn_info->authenticated = 1;
  639. }
  640. }
  641. if (conn_info->authenticated == 0) {
  642. log_printf (LOG_LEVEL_SECURITY, "Connection not authenticated because gid is %d, expecting %d\n", cred->gid, g_gid_valid);
  643. }
  644. }
  645. #endif
  646. /*
  647. * Dispatch all messages received in recvmsg that can be dispatched
  648. * sizeof (mar_req_header_t) needed at minimum to do any processing
  649. */
  650. conn_info->inb_inuse += res;
  651. conn_info->inb_start += res;
  652. while (conn_info->inb_inuse >= sizeof (mar_req_header_t) && res != -1) {
  653. header = (mar_req_header_t *)&conn_info->inb[conn_info->inb_start - conn_info->inb_inuse];
  654. if (header->size > conn_info->inb_inuse) {
  655. break;
  656. }
  657. service = conn_info->service;
  658. /*
  659. * If this service is in init phase, initialize service
  660. * else handle message using service service
  661. */
  662. if (service == SOCKET_SERVICE_INIT) {
  663. res = ais_init_service[header->id] (conn_info, header);
  664. } else {
  665. /*
  666. * Not an init service, but a standard service
  667. */
  668. if (header->id < 0 || header->id > ais_service[service]->lib_service_count) {
  669. log_printf (LOG_LEVEL_SECURITY, "Invalid header id is %d min 0 max %d\n",
  670. header->id, ais_service[service]->lib_service_count);
  671. return ;
  672. }
  673. /*
  674. * If flow control is required of the library handle, determine that
  675. * openais is not in synchronization and that totempg has room available
  676. * to queue a message, otherwise tell the library we are busy and to
  677. * try again later
  678. */
  679. send_ok_joined_iovec.iov_base = header;
  680. send_ok_joined_iovec.iov_len = header->size;
  681. send_ok_joined = totempg_groups_send_ok_joined (openais_group_handle,
  682. &send_ok_joined_iovec, 1);
  683. send_ok =
  684. (sync_primary_designated() == 1) && (
  685. (ais_service[service]->lib_service[header->id].flow_control == OPENAIS_FLOW_CONTROL_NOT_REQUIRED) ||
  686. ((ais_service[service]->lib_service[header->id].flow_control == OPENAIS_FLOW_CONTROL_REQUIRED) &&
  687. (send_ok_joined) &&
  688. (sync_in_process() == 0)));
  689. if (send_ok) {
  690. ais_service[service]->lib_service[header->id].lib_handler_fn(conn_info, header);
  691. } else {
  692. /*
  693. * Overload, tell library to retry
  694. */
  695. res_overlay.header.size =
  696. ais_service[service]->lib_service[header->id].response_size;
  697. res_overlay.header.id =
  698. ais_service[service]->lib_service[header->id].response_id;
  699. res_overlay.header.error = SA_AIS_ERR_TRY_AGAIN;
  700. openais_conn_send_response (
  701. conn_info,
  702. &res_overlay,
  703. res_overlay.header.size);
  704. }
  705. }
  706. conn_info->inb_inuse -= header->size;
  707. } /* while */
  708. if (conn_info->inb_inuse == 0) {
  709. conn_info->inb_start = 0;
  710. } else
  711. // BUG if (connections[conn_info->fd].inb_start + connections[conn_info->fd].inb_inuse >= SIZEINB) {
  712. if (conn_info->inb_start >= SIZEINB) {
  713. /*
  714. * If in buffer is full, move it back to start
  715. */
  716. memmove (conn_info->inb,
  717. &conn_info->inb[conn_info->inb_start - conn_info->inb_inuse],
  718. sizeof (char) * conn_info->inb_inuse);
  719. conn_info->inb_start = conn_info->inb_inuse;
  720. }
  721. return;
  722. }
  723. static int poll_handler_libais_accept (
  724. poll_handle handle,
  725. int fd,
  726. int revent,
  727. void *data)
  728. {
  729. socklen_t addrlen;
  730. struct sockaddr_un un_addr;
  731. int new_fd;
  732. #ifdef OPENAIS_LINUX
  733. int on = 1;
  734. #endif
  735. int res;
  736. addrlen = sizeof (struct sockaddr_un);
  737. retry_accept:
  738. new_fd = accept (fd, (struct sockaddr *)&un_addr, &addrlen);
  739. if (new_fd == -1 && errno == EINTR) {
  740. goto retry_accept;
  741. }
  742. if (new_fd == -1) {
  743. log_printf (LOG_LEVEL_ERROR, "ERROR: Could not accept Library connection: %s\n", strerror (errno));
  744. return (0); /* This is an error, but -1 would indicate disconnect from poll loop */
  745. }
  746. totemip_nosigpipe(new_fd);
  747. res = fcntl (new_fd, F_SETFL, O_NONBLOCK);
  748. if (res == -1) {
  749. log_printf (LOG_LEVEL_ERROR, "Could not set non-blocking operation on library connection: %s\n", strerror (errno));
  750. close (new_fd);
  751. return (0); /* This is an error, but -1 would indicate disconnect from poll loop */
  752. }
  753. /*
  754. * Valid accept
  755. */
  756. /*
  757. * Request credentials of sender provided by kernel
  758. */
  759. #ifdef OPENAIS_LINUX
  760. setsockopt(new_fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof (on));
  761. #endif
  762. log_printf (LOG_LEVEL_DEBUG, "connection received from libais client %d.\n", new_fd);
  763. res = conn_info_create (new_fd);
  764. if (res != 0) {
  765. close (new_fd);
  766. }
  767. return (0);
  768. }
  769. /*
  770. * Exported functions
  771. */
  772. int message_source_is_local(mar_message_source_t *source)
  773. {
  774. int ret = 0;
  775. assert (source != NULL);
  776. if (source->nodeid == my_ip->nodeid) {
  777. ret = 1;
  778. }
  779. return ret;
  780. }
  781. void message_source_set (
  782. mar_message_source_t *source,
  783. void *conn)
  784. {
  785. assert ((source != NULL) && (conn != NULL));
  786. source->nodeid = my_ip->nodeid;
  787. source->conn = conn;
  788. }
  789. static void ipc_confchg_fn (
  790. enum totem_configuration_type configuration_type,
  791. unsigned int *member_list, int member_list_entries,
  792. unsigned int *left_list, int left_list_entries,
  793. unsigned int *joined_list, int joined_list_entries,
  794. struct memb_ring_id *ring_id)
  795. {
  796. struct conn_info *conn_info;
  797. struct list_head *list;
  798. /*
  799. * Turn on flow control enabled flag for all connections
  800. */
  801. for (list = conn_info_list_head.next;
  802. list != &conn_info_list_head;
  803. list = list->next) {
  804. conn_info = list_entry (list, struct conn_info, list);
  805. conn_info->flow_control_enabled = 1;
  806. conn_info->conn_info_partner->flow_control_enabled = 1;
  807. }
  808. }
  809. void openais_ipc_init (
  810. void (*serialize_lock_fn) (void),
  811. void (*serialize_unlock_fn) (void),
  812. unsigned int gid_valid,
  813. struct totem_ip_address *my_ip_in)
  814. {
  815. int libais_server_fd;
  816. struct sockaddr_un un_addr;
  817. int res;
  818. log_init ("IPC");
  819. ipc_serialize_lock_fn = serialize_lock_fn;
  820. ipc_serialize_unlock_fn = serialize_unlock_fn;
  821. /*
  822. * Create socket for libais clients, name socket, listen for connections
  823. */
  824. libais_server_fd = socket (PF_UNIX, SOCK_STREAM, 0);
  825. if (libais_server_fd == -1) {
  826. log_printf (LOG_LEVEL_ERROR ,"Cannot create libais client connections socket.\n");
  827. openais_exit_error (AIS_DONE_LIBAIS_SOCKET);
  828. };
  829. totemip_nosigpipe(libais_server_fd);
  830. res = fcntl (libais_server_fd, F_SETFL, O_NONBLOCK);
  831. if (res == -1) {
  832. log_printf (LOG_LEVEL_ERROR, "Could not set non-blocking operation on server socket: %s\n", strerror (errno));
  833. openais_exit_error (AIS_DONE_LIBAIS_SOCKET);
  834. }
  835. #if !defined(OPENAIS_LINUX)
  836. unlink(socketname);
  837. #endif
  838. memset (&un_addr, 0, sizeof (struct sockaddr_un));
  839. un_addr.sun_family = AF_UNIX;
  840. #if defined(OPENAIS_BSD) || defined(OPENAIS_DARWIN)
  841. un_addr.sun_len = sizeof(struct sockaddr_un);
  842. #endif
  843. #if defined(OPENAIS_LINUX)
  844. strcpy (un_addr.sun_path + 1, socketname);
  845. #else
  846. strcpy (un_addr.sun_path, socketname);
  847. #endif
  848. res = bind (libais_server_fd, (struct sockaddr *)&un_addr, AIS_SUN_LEN(&un_addr));
  849. if (res) {
  850. log_printf (LOG_LEVEL_ERROR, "ERROR: Could not bind AF_UNIX: %s.\n", strerror (errno));
  851. openais_exit_error (AIS_DONE_LIBAIS_BIND);
  852. }
  853. listen (libais_server_fd, SERVER_BACKLOG);
  854. /*
  855. * Setup libais connection dispatch routine
  856. */
  857. poll_dispatch_add (aisexec_poll_handle, libais_server_fd,
  858. POLLIN, 0, poll_handler_libais_accept);
  859. g_gid_valid = gid_valid;
  860. my_ip = my_ip_in;
  861. /*
  862. * Reset internal state of flow control when
  863. * configuration change occurs
  864. */
  865. res = totempg_groups_initialize (
  866. &ipc_handle,
  867. NULL,
  868. ipc_confchg_fn);
  869. }
  870. /*
  871. * Get the conn info private data
  872. */
  873. void *openais_conn_private_data_get (void *conn)
  874. {
  875. struct conn_info *conn_info = (struct conn_info *)conn;
  876. if (conn != NULL) {
  877. return ((void *)conn_info->private_data);
  878. } else {
  879. return NULL;
  880. }
  881. }
  882. /*
  883. * Get the conn info partner connection
  884. */
  885. void *openais_conn_partner_get (void *conn)
  886. {
  887. struct conn_info *conn_info = (struct conn_info *)conn;
  888. if (conn != NULL) {
  889. return ((void *)conn_info->conn_info_partner);
  890. } else {
  891. return NULL;
  892. }
  893. }
  894. int openais_conn_send_response (
  895. void *conn,
  896. void *msg,
  897. int mlen)
  898. {
  899. struct queue *outq;
  900. char *cmsg;
  901. int res = 0;
  902. int queue_empty;
  903. struct outq_item *queue_item;
  904. struct outq_item queue_item_out;
  905. struct msghdr msg_send;
  906. struct iovec iov_send;
  907. char *msg_addr;
  908. struct conn_info *conn_info = (struct conn_info *)conn;
  909. if (conn_info == NULL) {
  910. return -1;
  911. }
  912. if (!libais_connection_active (conn_info)) {
  913. return (-1);
  914. }
  915. ipc_flow_control (conn_info);
  916. outq = &conn_info->outq;
  917. msg_send.msg_iov = &iov_send;
  918. msg_send.msg_name = 0;
  919. msg_send.msg_namelen = 0;
  920. msg_send.msg_iovlen = 1;
  921. msg_send.msg_control = 0;
  922. msg_send.msg_controllen = 0;
  923. msg_send.msg_flags = 0;
  924. if (queue_is_full (outq)) {
  925. /*
  926. * Start a disconnect if we have not already started one
  927. * and report that the outgoing queue is full
  928. */
  929. log_printf (LOG_LEVEL_ERROR, "Library queue is full, disconnecting library connection.\n");
  930. libais_disconnect_request (conn_info);
  931. return (-1);
  932. }
  933. while (!queue_is_empty (outq)) {
  934. queue_item = queue_item_get (outq);
  935. msg_addr = (char *)queue_item->msg;
  936. msg_addr = &msg_addr[conn_info->byte_start];
  937. iov_send.iov_base = msg_addr;
  938. iov_send.iov_len = queue_item->mlen - conn_info->byte_start;
  939. retry_sendmsg:
  940. res = sendmsg (conn_info->fd, &msg_send, MSG_NOSIGNAL);
  941. if (res == -1 && errno == EINTR) {
  942. goto retry_sendmsg;
  943. }
  944. if (res == -1 && errno == EAGAIN) {
  945. break; /* outgoing kernel queue full */
  946. }
  947. if (res == -1 && errno == EPIPE) {
  948. libais_disconnect_request (conn_info);
  949. return (0);
  950. }
  951. if (res == -1) {
  952. assert (0);
  953. break; /* some other error, stop trying to send message */
  954. }
  955. if (res + conn_info->byte_start != queue_item->mlen) {
  956. conn_info->byte_start += res;
  957. break;
  958. }
  959. /*
  960. * Message sent, try sending another message
  961. */
  962. queue_item_remove (outq);
  963. conn_info->byte_start = 0;
  964. free (queue_item->msg);
  965. } /* while queue not empty */
  966. res = -1;
  967. queue_empty = queue_is_empty (outq);
  968. /*
  969. * Send request message
  970. */
  971. if (queue_empty) {
  972. iov_send.iov_base = msg;
  973. iov_send.iov_len = mlen;
  974. retry_sendmsg_two:
  975. res = sendmsg (conn_info->fd, &msg_send, MSG_NOSIGNAL);
  976. if (res == -1 && errno == EINTR) {
  977. goto retry_sendmsg_two;
  978. }
  979. if (res == -1 && errno == EAGAIN) {
  980. conn_info->byte_start = 0;
  981. conn_info->events = POLLIN|POLLNVAL;
  982. }
  983. if (res != -1) {
  984. if (res + conn_info->byte_start != mlen) {
  985. conn_info->byte_start += res;
  986. res = -1;
  987. } else {
  988. conn_info->byte_start = 0;
  989. conn_info->events = POLLIN|POLLNVAL;
  990. }
  991. }
  992. }
  993. /*
  994. * If res == -1 , errrno == EAGAIN which means kernel queue full
  995. */
  996. if (res == -1) {
  997. cmsg = malloc (mlen);
  998. if (cmsg == 0) {
  999. log_printf (LOG_LEVEL_ERROR, "Library queue couldn't allocate a message, disconnecting library connection.\n");
  1000. libais_disconnect_request (conn_info);
  1001. return (-1);
  1002. }
  1003. queue_item_out.msg = cmsg;
  1004. queue_item_out.mlen = mlen;
  1005. memcpy (cmsg, msg, mlen);
  1006. queue_item_add (outq, &queue_item_out);
  1007. /*
  1008. * Send a pthread_kill to interrupt the poll syscall
  1009. * and start a new poll operation in the thread
  1010. */
  1011. conn_info->events = POLLIN|POLLOUT|POLLNVAL;
  1012. pthread_kill (conn_info->thread, SIGUSR1);
  1013. }
  1014. return (0);
  1015. }
  1016. int openais_ipc_timer_add (
  1017. void *conn,
  1018. void (*timer_fn) (void *data),
  1019. void *data,
  1020. unsigned int msec_in_future,
  1021. timer_handle *handle)
  1022. {
  1023. struct conn_info *conn_info = (struct conn_info *)conn;
  1024. int res;
  1025. res = timerlist_add_future (
  1026. &conn_info->timerlist,
  1027. timer_fn,
  1028. data,
  1029. msec_in_future,
  1030. handle);
  1031. return (res);
  1032. }
  1033. void openais_ipc_timer_del (
  1034. void *conn,
  1035. timer_handle timer_handle)
  1036. {
  1037. struct conn_info *conn_info = (struct conn_info *)conn;
  1038. timerlist_del (&conn_info->timerlist, timer_handle);
  1039. }
  1040. void openais_ipc_timer_del_data (
  1041. void *conn,
  1042. timer_handle timer_handle)
  1043. {
  1044. struct conn_info *conn_info = (struct conn_info *)conn;
  1045. timerlist_del (&conn_info->timerlist, timer_handle);
  1046. }
  1047. void openais_ipc_flow_control_create (
  1048. void *conn,
  1049. unsigned int service,
  1050. char *id,
  1051. int id_len,
  1052. void (*flow_control_state_set_fn) (void *conn, enum openais_flow_control_state),
  1053. void *context)
  1054. {
  1055. struct conn_info *conn_info = (struct conn_info *)conn;
  1056. openais_flow_control_create (
  1057. conn_info->flow_control_handle,
  1058. service,
  1059. id,
  1060. id_len,
  1061. flow_control_state_set_fn,
  1062. context);
  1063. conn_info->conn_info_partner->flow_control_handle = conn_info->flow_control_handle;
  1064. }
  1065. void openais_ipc_flow_control_destroy (
  1066. void *conn,
  1067. unsigned int service,
  1068. unsigned char *id,
  1069. int id_len)
  1070. {
  1071. struct conn_info *conn_info = (struct conn_info *)conn;
  1072. openais_flow_control_destroy (
  1073. conn_info->flow_control_handle,
  1074. service,
  1075. id,
  1076. id_len);
  1077. }
  1078. void openais_ipc_flow_control_local_increment (
  1079. void *conn)
  1080. {
  1081. struct conn_info *conn_info = (struct conn_info *)conn;
  1082. pthread_mutex_lock (&conn_info->flow_control_mutex);
  1083. conn_info->flow_control_local_count++;
  1084. pthread_mutex_unlock (&conn_info->flow_control_mutex);
  1085. }
  1086. void openais_ipc_flow_control_local_decrement (
  1087. void *conn)
  1088. {
  1089. struct conn_info *conn_info = (struct conn_info *)conn;
  1090. pthread_mutex_lock (&conn_info->flow_control_mutex);
  1091. conn_info->flow_control_local_count--;
  1092. pthread_mutex_unlock (&conn_info->flow_control_mutex);
  1093. }