ipc.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280
  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. unsigned int flow_control_local_count;
  482. unsigned int fcc;
  483. /*
  484. * Determine FCC variable and printing variables
  485. */
  486. entries_used = queue_used (&conn_info->outq);
  487. if (conn_info->conn_info_partner &&
  488. queue_used (&conn_info->conn_info_partner->outq) > entries_used) {
  489. entries_used = queue_used (&conn_info->conn_info_partner->outq);
  490. }
  491. entries_usedhw = queue_usedhw (&conn_info->outq);
  492. if (conn_info->conn_info_partner &&
  493. queue_usedhw (&conn_info->conn_info_partner->outq) > entries_used) {
  494. entries_usedhw = queue_usedhw (&conn_info->conn_info_partner->outq);
  495. }
  496. flow_control_local_count = conn_info->flow_control_local_count;
  497. if (conn_info->conn_info_partner &&
  498. conn_info->conn_info_partner->flow_control_local_count > flow_control_local_count) {
  499. flow_control_local_count = conn_info->conn_info_partner->flow_control_local_count;
  500. }
  501. fcc = entries_used;
  502. if (flow_control_local_count > fcc) {
  503. fcc = flow_control_local_count;
  504. }
  505. /*
  506. * IPC group-wide flow control
  507. */
  508. if (conn_info->flow_control == OPENAIS_FLOW_CONTROL_REQUIRED) {
  509. if (conn_info->flow_control_enabled == 0 &&
  510. ((fcc + FLOW_CONTROL_ENTRIES_ENABLE) > SIZEQUEUE)) {
  511. log_printf (LOG_LEVEL_NOTICE, "Enabling flow control [%d/%d] - [%d].\n",
  512. entries_usedhw, SIZEQUEUE,
  513. flow_control_local_count);
  514. openais_flow_control_enable (conn_info->flow_control_handle);
  515. conn_info->flow_control_enabled = 1;
  516. conn_info->conn_info_partner->flow_control_enabled = 1;
  517. }
  518. if (conn_info->flow_control_enabled == 1 &&
  519. fcc <= FLOW_CONTROL_ENTRIES_DISABLE) {
  520. log_printf (LOG_LEVEL_NOTICE, "Disabling flow control [%d/%d] - [%d].\n",
  521. entries_usedhw, SIZEQUEUE,
  522. flow_control_local_count);
  523. openais_flow_control_disable (conn_info->flow_control_handle);
  524. conn_info->flow_control_enabled = 0;
  525. conn_info->conn_info_partner->flow_control_enabled = 0;
  526. }
  527. }
  528. }
  529. static int conn_info_outq_flush (struct conn_info *conn_info) {
  530. struct queue *outq;
  531. int res = 0;
  532. struct outq_item *queue_item;
  533. struct msghdr msg_send;
  534. struct iovec iov_send;
  535. char *msg_addr;
  536. if (!libais_connection_active (conn_info)) {
  537. return (-1);
  538. }
  539. outq = &conn_info->outq;
  540. msg_send.msg_iov = &iov_send;
  541. msg_send.msg_name = 0;
  542. msg_send.msg_namelen = 0;
  543. msg_send.msg_iovlen = 1;
  544. msg_send.msg_control = 0;
  545. msg_send.msg_controllen = 0;
  546. msg_send.msg_flags = 0;
  547. while (!queue_is_empty (outq)) {
  548. queue_item = queue_item_get (outq);
  549. msg_addr = (char *)queue_item->msg;
  550. msg_addr = &msg_addr[conn_info->byte_start];
  551. iov_send.iov_base = msg_addr;
  552. iov_send.iov_len = queue_item->mlen - conn_info->byte_start;
  553. retry_sendmsg:
  554. res = sendmsg (conn_info->fd, &msg_send, MSG_NOSIGNAL);
  555. if (res == -1 && errno == EINTR) {
  556. goto retry_sendmsg;
  557. }
  558. if (res == -1 && errno == EAGAIN) {
  559. return (0);
  560. }
  561. if (res == -1 && errno == EPIPE) {
  562. libais_disconnect_request (conn_info);
  563. return (0);
  564. }
  565. if (res == -1) {
  566. printf ("ERRNO is %d\n", errno);
  567. assert (0); /* some other unhandled error here */
  568. }
  569. if (res + conn_info->byte_start != queue_item->mlen) {
  570. conn_info->byte_start += res;
  571. return (0);
  572. }
  573. /*
  574. * Message sent, try sending another message
  575. */
  576. queue_item_remove (outq);
  577. conn_info->byte_start = 0;
  578. free (queue_item->msg);
  579. } /* while queue not empty */
  580. if (queue_is_empty (outq)) {
  581. conn_info->events = POLLIN|POLLNVAL;
  582. }
  583. return (0);
  584. }
  585. struct res_overlay {
  586. mar_res_header_t header __attribute((aligned(8)));
  587. char buf[4096];
  588. };
  589. static void libais_deliver (struct conn_info *conn_info)
  590. {
  591. int res;
  592. mar_req_header_t *header;
  593. int service;
  594. struct msghdr msg_recv;
  595. struct iovec iov_recv;
  596. #ifdef OPENAIS_LINUX
  597. struct cmsghdr *cmsg;
  598. char cmsg_cred[CMSG_SPACE (sizeof (struct ucred))];
  599. struct ucred *cred;
  600. int on = 0;
  601. #else
  602. uid_t euid;
  603. gid_t egid;
  604. #endif
  605. int send_ok = 0;
  606. int send_ok_joined = 0;
  607. struct iovec send_ok_joined_iovec;
  608. struct res_overlay res_overlay;
  609. msg_recv.msg_iov = &iov_recv;
  610. msg_recv.msg_iovlen = 1;
  611. msg_recv.msg_name = 0;
  612. msg_recv.msg_namelen = 0;
  613. msg_recv.msg_flags = 0;
  614. if (conn_info->authenticated) {
  615. msg_recv.msg_control = 0;
  616. msg_recv.msg_controllen = 0;
  617. } else {
  618. #ifdef OPENAIS_LINUX
  619. msg_recv.msg_control = (void *)cmsg_cred;
  620. msg_recv.msg_controllen = sizeof (cmsg_cred);
  621. #else
  622. euid = -1; egid = -1;
  623. if (getpeereid(conn_info->fd, &euid, &egid) != -1 &&
  624. (euid == 0 || egid == g_gid_valid)) {
  625. conn_info->authenticated = 1;
  626. }
  627. if (conn_info->authenticated == 0) {
  628. log_printf (LOG_LEVEL_SECURITY, "Connection not authenticated because gid is %d, expecting %d\n", egid, g_gid_valid);
  629. }
  630. #endif
  631. }
  632. iov_recv.iov_base = &conn_info->inb[conn_info->inb_start];
  633. iov_recv.iov_len = (SIZEINB) - conn_info->inb_start;
  634. if (conn_info->inb_inuse == SIZEINB) {
  635. return;
  636. }
  637. retry_recv:
  638. res = recvmsg (conn_info->fd, &msg_recv, MSG_NOSIGNAL);
  639. if (res == -1 && errno == EINTR) {
  640. goto retry_recv;
  641. } else
  642. if (res == -1 && errno != EAGAIN) {
  643. return;
  644. } else
  645. if (res == 0) {
  646. return;
  647. }
  648. /*
  649. * Authenticate if this connection has not been authenticated
  650. */
  651. #ifdef OPENAIS_LINUX
  652. if (conn_info->authenticated == 0) {
  653. cmsg = CMSG_FIRSTHDR (&msg_recv);
  654. assert (cmsg);
  655. cred = (struct ucred *)CMSG_DATA (cmsg);
  656. if (cred) {
  657. if (cred->uid == 0 || cred->gid == g_gid_valid) {
  658. setsockopt(conn_info->fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof (on));
  659. conn_info->authenticated = 1;
  660. }
  661. }
  662. if (conn_info->authenticated == 0) {
  663. log_printf (LOG_LEVEL_SECURITY, "Connection not authenticated because gid is %d, expecting %d\n", cred->gid, g_gid_valid);
  664. }
  665. }
  666. #endif
  667. /*
  668. * Dispatch all messages received in recvmsg that can be dispatched
  669. * sizeof (mar_req_header_t) needed at minimum to do any processing
  670. */
  671. conn_info->inb_inuse += res;
  672. conn_info->inb_start += res;
  673. while (conn_info->inb_inuse >= sizeof (mar_req_header_t) && res != -1) {
  674. header = (mar_req_header_t *)&conn_info->inb[conn_info->inb_start - conn_info->inb_inuse];
  675. if (header->size > conn_info->inb_inuse) {
  676. break;
  677. }
  678. service = conn_info->service;
  679. /*
  680. * If this service is in init phase, initialize service
  681. * else handle message using service service
  682. */
  683. if (service == SOCKET_SERVICE_INIT) {
  684. res = ais_init_service[header->id] (conn_info, header);
  685. } else {
  686. /*
  687. * Not an init service, but a standard service
  688. */
  689. if (header->id < 0 || header->id > ais_service[service]->lib_service_count) {
  690. log_printf (LOG_LEVEL_SECURITY, "Invalid header id is %d min 0 max %d\n",
  691. header->id, ais_service[service]->lib_service_count);
  692. return ;
  693. }
  694. /*
  695. * If flow control is required of the library handle, determine that
  696. * openais is not in synchronization and that totempg has room available
  697. * to queue a message, otherwise tell the library we are busy and to
  698. * try again later
  699. */
  700. send_ok_joined_iovec.iov_base = header;
  701. send_ok_joined_iovec.iov_len = header->size;
  702. send_ok_joined = totempg_groups_send_ok_joined (openais_group_handle,
  703. &send_ok_joined_iovec, 1);
  704. send_ok =
  705. (sync_primary_designated() == 1) && (
  706. (ais_service[service]->lib_service[header->id].flow_control == OPENAIS_FLOW_CONTROL_NOT_REQUIRED) ||
  707. ((ais_service[service]->lib_service[header->id].flow_control == OPENAIS_FLOW_CONTROL_REQUIRED) &&
  708. (send_ok_joined) &&
  709. (sync_in_process() == 0)));
  710. if (send_ok) {
  711. ais_service[service]->lib_service[header->id].lib_handler_fn(conn_info, header);
  712. } else {
  713. /*
  714. * Overload, tell library to retry
  715. */
  716. res_overlay.header.size =
  717. ais_service[service]->lib_service[header->id].response_size;
  718. res_overlay.header.id =
  719. ais_service[service]->lib_service[header->id].response_id;
  720. res_overlay.header.error = SA_AIS_ERR_TRY_AGAIN;
  721. openais_conn_send_response (
  722. conn_info,
  723. &res_overlay,
  724. res_overlay.header.size);
  725. }
  726. }
  727. conn_info->inb_inuse -= header->size;
  728. } /* while */
  729. if (conn_info->inb_inuse == 0) {
  730. conn_info->inb_start = 0;
  731. } else
  732. // BUG if (connections[conn_info->fd].inb_start + connections[conn_info->fd].inb_inuse >= SIZEINB) {
  733. if (conn_info->inb_start >= SIZEINB) {
  734. /*
  735. * If in buffer is full, move it back to start
  736. */
  737. memmove (conn_info->inb,
  738. &conn_info->inb[conn_info->inb_start - conn_info->inb_inuse],
  739. sizeof (char) * conn_info->inb_inuse);
  740. conn_info->inb_start = conn_info->inb_inuse;
  741. }
  742. return;
  743. }
  744. static int poll_handler_libais_accept (
  745. poll_handle handle,
  746. int fd,
  747. int revent,
  748. void *data)
  749. {
  750. socklen_t addrlen;
  751. struct sockaddr_un un_addr;
  752. int new_fd;
  753. #ifdef OPENAIS_LINUX
  754. int on = 1;
  755. #endif
  756. int res;
  757. addrlen = sizeof (struct sockaddr_un);
  758. retry_accept:
  759. new_fd = accept (fd, (struct sockaddr *)&un_addr, &addrlen);
  760. if (new_fd == -1 && errno == EINTR) {
  761. goto retry_accept;
  762. }
  763. if (new_fd == -1) {
  764. log_printf (LOG_LEVEL_ERROR, "ERROR: Could not accept Library connection: %s\n", strerror (errno));
  765. return (0); /* This is an error, but -1 would indicate disconnect from poll loop */
  766. }
  767. totemip_nosigpipe(new_fd);
  768. res = fcntl (new_fd, F_SETFL, O_NONBLOCK);
  769. if (res == -1) {
  770. log_printf (LOG_LEVEL_ERROR, "Could not set non-blocking operation on library connection: %s\n", strerror (errno));
  771. close (new_fd);
  772. return (0); /* This is an error, but -1 would indicate disconnect from poll loop */
  773. }
  774. /*
  775. * Valid accept
  776. */
  777. /*
  778. * Request credentials of sender provided by kernel
  779. */
  780. #ifdef OPENAIS_LINUX
  781. setsockopt(new_fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof (on));
  782. #endif
  783. log_printf (LOG_LEVEL_DEBUG, "connection received from libais client %d.\n", new_fd);
  784. res = conn_info_create (new_fd);
  785. if (res != 0) {
  786. close (new_fd);
  787. }
  788. return (0);
  789. }
  790. /*
  791. * Exported functions
  792. */
  793. int message_source_is_local(mar_message_source_t *source)
  794. {
  795. int ret = 0;
  796. assert (source != NULL);
  797. if (source->nodeid == my_ip->nodeid) {
  798. ret = 1;
  799. }
  800. return ret;
  801. }
  802. void message_source_set (
  803. mar_message_source_t *source,
  804. void *conn)
  805. {
  806. assert ((source != NULL) && (conn != NULL));
  807. source->nodeid = my_ip->nodeid;
  808. source->conn = conn;
  809. }
  810. static void ipc_confchg_fn (
  811. enum totem_configuration_type configuration_type,
  812. unsigned int *member_list, int member_list_entries,
  813. unsigned int *left_list, int left_list_entries,
  814. unsigned int *joined_list, int joined_list_entries,
  815. struct memb_ring_id *ring_id)
  816. {
  817. }
  818. void openais_ipc_init (
  819. void (*serialize_lock_fn) (void),
  820. void (*serialize_unlock_fn) (void),
  821. unsigned int gid_valid,
  822. struct totem_ip_address *my_ip_in)
  823. {
  824. int libais_server_fd;
  825. struct sockaddr_un un_addr;
  826. int res;
  827. log_init ("IPC");
  828. ipc_serialize_lock_fn = serialize_lock_fn;
  829. ipc_serialize_unlock_fn = serialize_unlock_fn;
  830. /*
  831. * Create socket for libais clients, name socket, listen for connections
  832. */
  833. libais_server_fd = socket (PF_UNIX, SOCK_STREAM, 0);
  834. if (libais_server_fd == -1) {
  835. log_printf (LOG_LEVEL_ERROR ,"Cannot create libais client connections socket.\n");
  836. openais_exit_error (AIS_DONE_LIBAIS_SOCKET);
  837. };
  838. totemip_nosigpipe(libais_server_fd);
  839. res = fcntl (libais_server_fd, F_SETFL, O_NONBLOCK);
  840. if (res == -1) {
  841. log_printf (LOG_LEVEL_ERROR, "Could not set non-blocking operation on server socket: %s\n", strerror (errno));
  842. openais_exit_error (AIS_DONE_LIBAIS_SOCKET);
  843. }
  844. #if !defined(OPENAIS_LINUX)
  845. unlink(socketname);
  846. #endif
  847. memset (&un_addr, 0, sizeof (struct sockaddr_un));
  848. un_addr.sun_family = AF_UNIX;
  849. #if defined(OPENAIS_BSD) || defined(OPENAIS_DARWIN)
  850. un_addr.sun_len = sizeof(struct sockaddr_un);
  851. #endif
  852. #if defined(OPENAIS_LINUX)
  853. strcpy (un_addr.sun_path + 1, socketname);
  854. #else
  855. strcpy (un_addr.sun_path, socketname);
  856. #endif
  857. res = bind (libais_server_fd, (struct sockaddr *)&un_addr, AIS_SUN_LEN(&un_addr));
  858. if (res) {
  859. log_printf (LOG_LEVEL_ERROR, "ERROR: Could not bind AF_UNIX: %s.\n", strerror (errno));
  860. openais_exit_error (AIS_DONE_LIBAIS_BIND);
  861. }
  862. listen (libais_server_fd, SERVER_BACKLOG);
  863. /*
  864. * Setup libais connection dispatch routine
  865. */
  866. poll_dispatch_add (aisexec_poll_handle, libais_server_fd,
  867. POLLIN, 0, poll_handler_libais_accept);
  868. g_gid_valid = gid_valid;
  869. my_ip = my_ip_in;
  870. /*
  871. * Reset internal state of flow control when
  872. * configuration change occurs
  873. */
  874. res = totempg_groups_initialize (
  875. &ipc_handle,
  876. NULL,
  877. ipc_confchg_fn);
  878. }
  879. /*
  880. * Get the conn info private data
  881. */
  882. void *openais_conn_private_data_get (void *conn)
  883. {
  884. struct conn_info *conn_info = (struct conn_info *)conn;
  885. if (conn != NULL) {
  886. return ((void *)conn_info->private_data);
  887. } else {
  888. return NULL;
  889. }
  890. }
  891. /*
  892. * Get the conn info partner connection
  893. */
  894. void *openais_conn_partner_get (void *conn)
  895. {
  896. struct conn_info *conn_info = (struct conn_info *)conn;
  897. if (conn != NULL) {
  898. return ((void *)conn_info->conn_info_partner);
  899. } else {
  900. return NULL;
  901. }
  902. }
  903. int openais_conn_send_response (
  904. void *conn,
  905. void *msg,
  906. int mlen)
  907. {
  908. struct queue *outq;
  909. char *cmsg;
  910. int res = 0;
  911. int queue_empty;
  912. struct outq_item *queue_item;
  913. struct outq_item queue_item_out;
  914. struct msghdr msg_send;
  915. struct iovec iov_send;
  916. char *msg_addr;
  917. struct conn_info *conn_info = (struct conn_info *)conn;
  918. if (conn_info == NULL) {
  919. return -1;
  920. }
  921. if (!libais_connection_active (conn_info)) {
  922. return (-1);
  923. }
  924. ipc_flow_control (conn_info);
  925. outq = &conn_info->outq;
  926. msg_send.msg_iov = &iov_send;
  927. msg_send.msg_name = 0;
  928. msg_send.msg_namelen = 0;
  929. msg_send.msg_iovlen = 1;
  930. msg_send.msg_control = 0;
  931. msg_send.msg_controllen = 0;
  932. msg_send.msg_flags = 0;
  933. if (queue_is_full (outq)) {
  934. /*
  935. * Start a disconnect if we have not already started one
  936. * and report that the outgoing queue is full
  937. */
  938. log_printf (LOG_LEVEL_ERROR, "Library queue is full, disconnecting library connection.\n");
  939. libais_disconnect_request (conn_info);
  940. return (-1);
  941. }
  942. while (!queue_is_empty (outq)) {
  943. queue_item = queue_item_get (outq);
  944. msg_addr = (char *)queue_item->msg;
  945. msg_addr = &msg_addr[conn_info->byte_start];
  946. iov_send.iov_base = msg_addr;
  947. iov_send.iov_len = queue_item->mlen - conn_info->byte_start;
  948. retry_sendmsg:
  949. res = sendmsg (conn_info->fd, &msg_send, MSG_NOSIGNAL);
  950. if (res == -1 && errno == EINTR) {
  951. goto retry_sendmsg;
  952. }
  953. if (res == -1 && errno == EAGAIN) {
  954. break; /* outgoing kernel queue full */
  955. }
  956. if (res == -1 && errno == EPIPE) {
  957. libais_disconnect_request (conn_info);
  958. return (0);
  959. }
  960. if (res == -1) {
  961. assert (0);
  962. break; /* some other error, stop trying to send message */
  963. }
  964. if (res + conn_info->byte_start != queue_item->mlen) {
  965. conn_info->byte_start += res;
  966. break;
  967. }
  968. /*
  969. * Message sent, try sending another message
  970. */
  971. queue_item_remove (outq);
  972. conn_info->byte_start = 0;
  973. free (queue_item->msg);
  974. } /* while queue not empty */
  975. res = -1;
  976. queue_empty = queue_is_empty (outq);
  977. /*
  978. * Send request message
  979. */
  980. if (queue_empty) {
  981. iov_send.iov_base = msg;
  982. iov_send.iov_len = mlen;
  983. retry_sendmsg_two:
  984. res = sendmsg (conn_info->fd, &msg_send, MSG_NOSIGNAL);
  985. if (res == -1 && errno == EINTR) {
  986. goto retry_sendmsg_two;
  987. }
  988. if (res == -1 && errno == EAGAIN) {
  989. conn_info->byte_start = 0;
  990. conn_info->events = POLLIN|POLLNVAL;
  991. }
  992. if (res != -1) {
  993. if (res + conn_info->byte_start != mlen) {
  994. conn_info->byte_start += res;
  995. res = -1;
  996. } else {
  997. conn_info->byte_start = 0;
  998. conn_info->events = POLLIN|POLLNVAL;
  999. }
  1000. }
  1001. }
  1002. /*
  1003. * If res == -1 , errrno == EAGAIN which means kernel queue full
  1004. */
  1005. if (res == -1) {
  1006. cmsg = malloc (mlen);
  1007. if (cmsg == 0) {
  1008. log_printf (LOG_LEVEL_ERROR, "Library queue couldn't allocate a message, disconnecting library connection.\n");
  1009. libais_disconnect_request (conn_info);
  1010. return (-1);
  1011. }
  1012. queue_item_out.msg = cmsg;
  1013. queue_item_out.mlen = mlen;
  1014. memcpy (cmsg, msg, mlen);
  1015. queue_item_add (outq, &queue_item_out);
  1016. /*
  1017. * Send a pthread_kill to interrupt the poll syscall
  1018. * and start a new poll operation in the thread
  1019. */
  1020. conn_info->events = POLLIN|POLLOUT|POLLNVAL;
  1021. pthread_kill (conn_info->thread, SIGUSR1);
  1022. }
  1023. return (0);
  1024. }
  1025. int openais_ipc_timer_add (
  1026. void *conn,
  1027. void (*timer_fn) (void *data),
  1028. void *data,
  1029. unsigned int msec_in_future,
  1030. timer_handle *handle)
  1031. {
  1032. struct conn_info *conn_info = (struct conn_info *)conn;
  1033. int res;
  1034. res = timerlist_add_future (
  1035. &conn_info->timerlist,
  1036. timer_fn,
  1037. data,
  1038. msec_in_future,
  1039. handle);
  1040. return (res);
  1041. }
  1042. void openais_ipc_timer_del (
  1043. void *conn,
  1044. timer_handle timer_handle)
  1045. {
  1046. struct conn_info *conn_info = (struct conn_info *)conn;
  1047. timerlist_del (&conn_info->timerlist, timer_handle);
  1048. }
  1049. void openais_ipc_timer_del_data (
  1050. void *conn,
  1051. timer_handle timer_handle)
  1052. {
  1053. struct conn_info *conn_info = (struct conn_info *)conn;
  1054. timerlist_del (&conn_info->timerlist, timer_handle);
  1055. }
  1056. void openais_ipc_flow_control_create (
  1057. void *conn,
  1058. unsigned int service,
  1059. char *id,
  1060. int id_len,
  1061. void (*flow_control_state_set_fn) (void *conn, enum openais_flow_control_state),
  1062. void *context)
  1063. {
  1064. struct conn_info *conn_info = (struct conn_info *)conn;
  1065. openais_flow_control_create (
  1066. conn_info->flow_control_handle,
  1067. service,
  1068. id,
  1069. id_len,
  1070. flow_control_state_set_fn,
  1071. context);
  1072. conn_info->conn_info_partner->flow_control_handle = conn_info->flow_control_handle;
  1073. }
  1074. void openais_ipc_flow_control_destroy (
  1075. void *conn,
  1076. unsigned int service,
  1077. unsigned char *id,
  1078. int id_len)
  1079. {
  1080. struct conn_info *conn_info = (struct conn_info *)conn;
  1081. openais_flow_control_destroy (
  1082. conn_info->flow_control_handle,
  1083. service,
  1084. id,
  1085. id_len);
  1086. }
  1087. void openais_ipc_flow_control_local_increment (
  1088. void *conn)
  1089. {
  1090. struct conn_info *conn_info = (struct conn_info *)conn;
  1091. pthread_mutex_lock (&conn_info->flow_control_mutex);
  1092. conn_info->flow_control_local_count++;
  1093. pthread_mutex_unlock (&conn_info->flow_control_mutex);
  1094. }
  1095. void openais_ipc_flow_control_local_decrement (
  1096. void *conn)
  1097. {
  1098. struct conn_info *conn_info = (struct conn_info *)conn;
  1099. pthread_mutex_lock (&conn_info->flow_control_mutex);
  1100. conn_info->flow_control_local_count--;
  1101. pthread_mutex_unlock (&conn_info->flow_control_mutex);
  1102. }