4
0

cpg.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355
  1. /*
  2. * vi: set autoindent tabstop=4 shiftwidth=4 :
  3. *
  4. * Copyright (c) 2006-2015 Red Hat, Inc.
  5. *
  6. * All rights reserved.
  7. *
  8. * Author: Christine Caulfield (ccaulfi@redhat.com)
  9. * Author: Jan Friesse (jfriesse@redhat.com)
  10. *
  11. * This software licensed under BSD license, the text of which follows:
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright notice,
  17. * this list of conditions and the following disclaimer.
  18. * - Redistributions in binary form must reproduce the above copyright notice,
  19. * this list of conditions and the following disclaimer in the documentation
  20. * and/or other materials provided with the distribution.
  21. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  22. * contributors may be used to endorse or promote products derived from this
  23. * software without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  26. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  29. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  30. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  31. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  32. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  33. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  34. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  35. * THE POSSIBILITY OF SUCH DAMAGE.
  36. */
  37. /*
  38. * Provides a closed process group API using the coroipcc executive
  39. */
  40. #include <config.h>
  41. #include <stdlib.h>
  42. #include <stdio.h>
  43. #include <string.h>
  44. #include <unistd.h>
  45. #include <sys/types.h>
  46. #include <sys/socket.h>
  47. #include <sys/mman.h>
  48. #include <sys/uio.h>
  49. #include <sys/stat.h>
  50. #include <errno.h>
  51. #include <limits.h>
  52. #include <qb/qbdefs.h>
  53. #include <qb/qbipcc.h>
  54. #include <qb/qblog.h>
  55. #include <corosync/hdb.h>
  56. #include <corosync/list.h>
  57. #include <corosync/corotypes.h>
  58. #include <corosync/corodefs.h>
  59. #include <corosync/cpg.h>
  60. #include <corosync/ipc_cpg.h>
  61. #include "util.h"
  62. #ifndef MAP_ANONYMOUS
  63. #define MAP_ANONYMOUS MAP_ANON
  64. #endif
  65. /*
  66. * Maximum number of times to retry a send when transmitting
  67. * a large message fragment
  68. */
  69. #define MAX_RETRIES 100
  70. /*
  71. * ZCB files have following umask (umask is same as used in libqb)
  72. */
  73. #define CPG_MEMORY_MAP_UMASK 077
  74. struct cpg_inst {
  75. qb_ipcc_connection_t *c;
  76. int finalize;
  77. void *context;
  78. union {
  79. cpg_model_data_t model_data;
  80. cpg_model_v1_data_t model_v1_data;
  81. };
  82. struct list_head iteration_list_head;
  83. uint32_t max_msg_size;
  84. char *assembly_buf;
  85. uint32_t assembly_buf_ptr;
  86. int assembling; /* Flag that says we have started assembling a message.
  87. * It's here to catch the situation where a node joins
  88. * the cluster/group in the middle of a CPG message send
  89. * so we don't pass on a partial message to the client.
  90. */
  91. };
  92. static void cpg_inst_free (void *inst);
  93. DECLARE_HDB_DATABASE(cpg_handle_t_db, cpg_inst_free);
  94. struct cpg_iteration_instance_t {
  95. cpg_iteration_handle_t cpg_iteration_handle;
  96. qb_ipcc_connection_t *conn;
  97. hdb_handle_t executive_iteration_handle;
  98. struct list_head list;
  99. };
  100. DECLARE_HDB_DATABASE(cpg_iteration_handle_t_db,NULL);
  101. /*
  102. * Internal (not visible by API) functions
  103. */
  104. static cs_error_t
  105. coroipcc_msg_send_reply_receive (
  106. qb_ipcc_connection_t *c,
  107. const struct iovec *iov,
  108. unsigned int iov_len,
  109. void *res_msg,
  110. size_t res_len)
  111. {
  112. return qb_to_cs_error(qb_ipcc_sendv_recv(c, iov, iov_len, res_msg, res_len,
  113. CS_IPC_TIMEOUT_MS));
  114. }
  115. static void cpg_iteration_instance_finalize (struct cpg_iteration_instance_t *cpg_iteration_instance)
  116. {
  117. list_del (&cpg_iteration_instance->list);
  118. hdb_handle_destroy (&cpg_iteration_handle_t_db, cpg_iteration_instance->cpg_iteration_handle);
  119. }
  120. static void cpg_inst_free (void *inst)
  121. {
  122. struct cpg_inst *cpg_inst = (struct cpg_inst *)inst;
  123. qb_ipcc_disconnect(cpg_inst->c);
  124. }
  125. static void cpg_inst_finalize (struct cpg_inst *cpg_inst, hdb_handle_t handle)
  126. {
  127. struct list_head *iter, *iter_next;
  128. struct cpg_iteration_instance_t *cpg_iteration_instance;
  129. /*
  130. * Traverse thru iteration instances and delete them
  131. */
  132. for (iter = cpg_inst->iteration_list_head.next; iter != &cpg_inst->iteration_list_head;iter = iter_next) {
  133. iter_next = iter->next;
  134. cpg_iteration_instance = list_entry (iter, struct cpg_iteration_instance_t, list);
  135. cpg_iteration_instance_finalize (cpg_iteration_instance);
  136. }
  137. hdb_handle_destroy (&cpg_handle_t_db, handle);
  138. }
  139. /**
  140. * @defgroup cpg_coroipcc The closed process group API
  141. * @ingroup coroipcc
  142. *
  143. * @{
  144. */
  145. cs_error_t cpg_initialize (
  146. cpg_handle_t *handle,
  147. cpg_callbacks_t *callbacks)
  148. {
  149. cpg_model_v1_data_t model_v1_data;
  150. memset (&model_v1_data, 0, sizeof (cpg_model_v1_data_t));
  151. if (callbacks) {
  152. model_v1_data.cpg_deliver_fn = callbacks->cpg_deliver_fn;
  153. model_v1_data.cpg_confchg_fn = callbacks->cpg_confchg_fn;
  154. }
  155. return (cpg_model_initialize (handle, CPG_MODEL_V1, (cpg_model_data_t *)&model_v1_data, NULL));
  156. }
  157. cs_error_t cpg_model_initialize (
  158. cpg_handle_t *handle,
  159. cpg_model_t model,
  160. cpg_model_data_t *model_data,
  161. void *context)
  162. {
  163. cs_error_t error;
  164. struct cpg_inst *cpg_inst;
  165. if (model != CPG_MODEL_V1) {
  166. error = CS_ERR_INVALID_PARAM;
  167. goto error_no_destroy;
  168. }
  169. error = hdb_error_to_cs (hdb_handle_create (&cpg_handle_t_db, sizeof (struct cpg_inst), handle));
  170. if (error != CS_OK) {
  171. goto error_no_destroy;
  172. }
  173. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, *handle, (void *)&cpg_inst));
  174. if (error != CS_OK) {
  175. goto error_destroy;
  176. }
  177. cpg_inst->c = qb_ipcc_connect ("cpg", IPC_REQUEST_SIZE);
  178. if (cpg_inst->c == NULL) {
  179. error = qb_to_cs_error(-errno);
  180. goto error_put_destroy;
  181. }
  182. if (model_data != NULL) {
  183. switch (model) {
  184. case CPG_MODEL_V1:
  185. memcpy (&cpg_inst->model_v1_data, model_data, sizeof (cpg_model_v1_data_t));
  186. if ((cpg_inst->model_v1_data.flags & ~(CPG_MODEL_V1_DELIVER_INITIAL_TOTEM_CONF)) != 0) {
  187. error = CS_ERR_INVALID_PARAM;
  188. goto error_destroy;
  189. }
  190. break;
  191. }
  192. }
  193. /* Allow space for corosync internal headers */
  194. cpg_inst->max_msg_size = IPC_REQUEST_SIZE - 1024;
  195. cpg_inst->model_data.model = model;
  196. cpg_inst->context = context;
  197. list_init(&cpg_inst->iteration_list_head);
  198. hdb_handle_put (&cpg_handle_t_db, *handle);
  199. return (CS_OK);
  200. error_put_destroy:
  201. hdb_handle_put (&cpg_handle_t_db, *handle);
  202. error_destroy:
  203. hdb_handle_destroy (&cpg_handle_t_db, *handle);
  204. error_no_destroy:
  205. return (error);
  206. }
  207. cs_error_t cpg_finalize (
  208. cpg_handle_t handle)
  209. {
  210. struct cpg_inst *cpg_inst;
  211. struct iovec iov;
  212. struct req_lib_cpg_finalize req_lib_cpg_finalize;
  213. struct res_lib_cpg_finalize res_lib_cpg_finalize;
  214. cs_error_t error;
  215. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  216. if (error != CS_OK) {
  217. return (error);
  218. }
  219. /*
  220. * Another thread has already started finalizing
  221. */
  222. if (cpg_inst->finalize) {
  223. hdb_handle_put (&cpg_handle_t_db, handle);
  224. return (CS_ERR_BAD_HANDLE);
  225. }
  226. cpg_inst->finalize = 1;
  227. /*
  228. * Send service request
  229. */
  230. req_lib_cpg_finalize.header.size = sizeof (struct req_lib_cpg_finalize);
  231. req_lib_cpg_finalize.header.id = MESSAGE_REQ_CPG_FINALIZE;
  232. iov.iov_base = (void *)&req_lib_cpg_finalize;
  233. iov.iov_len = sizeof (struct req_lib_cpg_finalize);
  234. error = coroipcc_msg_send_reply_receive (cpg_inst->c,
  235. &iov,
  236. 1,
  237. &res_lib_cpg_finalize,
  238. sizeof (struct res_lib_cpg_finalize));
  239. cpg_inst_finalize (cpg_inst, handle);
  240. hdb_handle_put (&cpg_handle_t_db, handle);
  241. return (error);
  242. }
  243. cs_error_t cpg_fd_get (
  244. cpg_handle_t handle,
  245. int *fd)
  246. {
  247. cs_error_t error;
  248. struct cpg_inst *cpg_inst;
  249. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  250. if (error != CS_OK) {
  251. return (error);
  252. }
  253. error = qb_to_cs_error (qb_ipcc_fd_get (cpg_inst->c, fd));
  254. hdb_handle_put (&cpg_handle_t_db, handle);
  255. return (error);
  256. }
  257. cs_error_t cpg_max_atomic_msgsize_get (
  258. cpg_handle_t handle,
  259. uint32_t *size)
  260. {
  261. cs_error_t error;
  262. struct cpg_inst *cpg_inst;
  263. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  264. if (error != CS_OK) {
  265. return (error);
  266. }
  267. *size = cpg_inst->max_msg_size;
  268. hdb_handle_put (&cpg_handle_t_db, handle);
  269. return (error);
  270. }
  271. cs_error_t cpg_context_get (
  272. cpg_handle_t handle,
  273. void **context)
  274. {
  275. cs_error_t error;
  276. struct cpg_inst *cpg_inst;
  277. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  278. if (error != CS_OK) {
  279. return (error);
  280. }
  281. *context = cpg_inst->context;
  282. hdb_handle_put (&cpg_handle_t_db, handle);
  283. return (CS_OK);
  284. }
  285. cs_error_t cpg_context_set (
  286. cpg_handle_t handle,
  287. void *context)
  288. {
  289. cs_error_t error;
  290. struct cpg_inst *cpg_inst;
  291. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  292. if (error != CS_OK) {
  293. return (error);
  294. }
  295. cpg_inst->context = context;
  296. hdb_handle_put (&cpg_handle_t_db, handle);
  297. return (CS_OK);
  298. }
  299. cs_error_t cpg_dispatch (
  300. cpg_handle_t handle,
  301. cs_dispatch_flags_t dispatch_types)
  302. {
  303. int timeout = -1;
  304. cs_error_t error;
  305. int cont = 1; /* always continue do loop except when set to 0 */
  306. struct cpg_inst *cpg_inst;
  307. struct res_lib_cpg_confchg_callback *res_cpg_confchg_callback;
  308. struct res_lib_cpg_deliver_callback *res_cpg_deliver_callback;
  309. struct res_lib_cpg_partial_deliver_callback *res_cpg_partial_deliver_callback;
  310. struct res_lib_cpg_totem_confchg_callback *res_cpg_totem_confchg_callback;
  311. struct cpg_inst cpg_inst_copy;
  312. struct qb_ipc_response_header *dispatch_data;
  313. struct cpg_address member_list[CPG_MEMBERS_MAX];
  314. struct cpg_address left_list[CPG_MEMBERS_MAX];
  315. struct cpg_address joined_list[CPG_MEMBERS_MAX];
  316. struct cpg_name group_name;
  317. mar_cpg_address_t *left_list_start;
  318. mar_cpg_address_t *joined_list_start;
  319. unsigned int i;
  320. struct cpg_ring_id ring_id;
  321. uint32_t totem_member_list[CPG_MEMBERS_MAX];
  322. int32_t errno_res;
  323. char dispatch_buf[IPC_DISPATCH_SIZE];
  324. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  325. if (error != CS_OK) {
  326. return (error);
  327. }
  328. /*
  329. * Timeout instantly for CS_DISPATCH_ONE_NONBLOCKING or CS_DISPATCH_ALL and
  330. * wait indefinitely for CS_DISPATCH_ONE or CS_DISPATCH_BLOCKING
  331. */
  332. if (dispatch_types == CS_DISPATCH_ALL || dispatch_types == CS_DISPATCH_ONE_NONBLOCKING) {
  333. timeout = 0;
  334. }
  335. dispatch_data = (struct qb_ipc_response_header *)dispatch_buf;
  336. do {
  337. errno_res = qb_ipcc_event_recv (
  338. cpg_inst->c,
  339. dispatch_buf,
  340. IPC_DISPATCH_SIZE,
  341. timeout);
  342. error = qb_to_cs_error (errno_res);
  343. if (error == CS_ERR_BAD_HANDLE) {
  344. error = CS_OK;
  345. goto error_put;
  346. }
  347. if (error == CS_ERR_TRY_AGAIN) {
  348. if (dispatch_types == CS_DISPATCH_ONE_NONBLOCKING) {
  349. /*
  350. * Don't mask error
  351. */
  352. goto error_put;
  353. }
  354. error = CS_OK;
  355. if (dispatch_types == CS_DISPATCH_ALL) {
  356. break; /* exit do while cont is 1 loop */
  357. } else {
  358. continue; /* next poll */
  359. }
  360. }
  361. if (error != CS_OK) {
  362. goto error_put;
  363. }
  364. /*
  365. * Make copy of callbacks, message data, unlock instance, and call callback
  366. * A risk of this dispatch method is that the callback routines may
  367. * operate at the same time that cpgFinalize has been called.
  368. */
  369. memcpy (&cpg_inst_copy, cpg_inst, sizeof (struct cpg_inst));
  370. switch (cpg_inst_copy.model_data.model) {
  371. case CPG_MODEL_V1:
  372. /*
  373. * Dispatch incoming message
  374. */
  375. switch (dispatch_data->id) {
  376. case MESSAGE_RES_CPG_DELIVER_CALLBACK:
  377. if (cpg_inst_copy.model_v1_data.cpg_deliver_fn == NULL) {
  378. break;
  379. }
  380. res_cpg_deliver_callback = (struct res_lib_cpg_deliver_callback *)dispatch_data;
  381. marshall_from_mar_cpg_name_t (
  382. &group_name,
  383. &res_cpg_deliver_callback->group_name);
  384. cpg_inst_copy.model_v1_data.cpg_deliver_fn (handle,
  385. &group_name,
  386. res_cpg_deliver_callback->nodeid,
  387. res_cpg_deliver_callback->pid,
  388. &res_cpg_deliver_callback->message,
  389. res_cpg_deliver_callback->msglen);
  390. break;
  391. case MESSAGE_RES_CPG_PARTIAL_DELIVER_CALLBACK:
  392. res_cpg_partial_deliver_callback = (struct res_lib_cpg_partial_deliver_callback *)dispatch_data;
  393. marshall_from_mar_cpg_name_t (
  394. &group_name,
  395. &res_cpg_partial_deliver_callback->group_name);
  396. if (res_cpg_partial_deliver_callback->type == LIBCPG_PARTIAL_FIRST) {
  397. /*
  398. * Allocate a buffer to contain a full message.
  399. */
  400. cpg_inst->assembly_buf = malloc(res_cpg_partial_deliver_callback->msglen);
  401. if (!cpg_inst->assembly_buf) {
  402. error = CS_ERR_NO_MEMORY;
  403. goto error_put;
  404. }
  405. cpg_inst->assembling = 1;
  406. cpg_inst->assembly_buf_ptr = 0;
  407. }
  408. if (cpg_inst->assembling) {
  409. memcpy(cpg_inst->assembly_buf + cpg_inst->assembly_buf_ptr,
  410. res_cpg_partial_deliver_callback->message, res_cpg_partial_deliver_callback->fraglen);
  411. cpg_inst->assembly_buf_ptr += res_cpg_partial_deliver_callback->fraglen;
  412. if (res_cpg_partial_deliver_callback->type == LIBCPG_PARTIAL_LAST) {
  413. cpg_inst_copy.model_v1_data.cpg_deliver_fn (handle,
  414. &group_name,
  415. res_cpg_partial_deliver_callback->nodeid,
  416. res_cpg_partial_deliver_callback->pid,
  417. cpg_inst->assembly_buf,
  418. res_cpg_partial_deliver_callback->msglen);
  419. free(cpg_inst->assembly_buf);
  420. cpg_inst->assembling = 0;
  421. }
  422. }
  423. break;
  424. case MESSAGE_RES_CPG_CONFCHG_CALLBACK:
  425. if (cpg_inst_copy.model_v1_data.cpg_confchg_fn == NULL) {
  426. break;
  427. }
  428. res_cpg_confchg_callback = (struct res_lib_cpg_confchg_callback *)dispatch_data;
  429. for (i = 0; i < res_cpg_confchg_callback->member_list_entries; i++) {
  430. marshall_from_mar_cpg_address_t (&member_list[i],
  431. &res_cpg_confchg_callback->member_list[i]);
  432. }
  433. left_list_start = res_cpg_confchg_callback->member_list +
  434. res_cpg_confchg_callback->member_list_entries;
  435. for (i = 0; i < res_cpg_confchg_callback->left_list_entries; i++) {
  436. marshall_from_mar_cpg_address_t (&left_list[i],
  437. &left_list_start[i]);
  438. }
  439. joined_list_start = res_cpg_confchg_callback->member_list +
  440. res_cpg_confchg_callback->member_list_entries +
  441. res_cpg_confchg_callback->left_list_entries;
  442. for (i = 0; i < res_cpg_confchg_callback->joined_list_entries; i++) {
  443. marshall_from_mar_cpg_address_t (&joined_list[i],
  444. &joined_list_start[i]);
  445. }
  446. marshall_from_mar_cpg_name_t (
  447. &group_name,
  448. &res_cpg_confchg_callback->group_name);
  449. cpg_inst_copy.model_v1_data.cpg_confchg_fn (handle,
  450. &group_name,
  451. member_list,
  452. res_cpg_confchg_callback->member_list_entries,
  453. left_list,
  454. res_cpg_confchg_callback->left_list_entries,
  455. joined_list,
  456. res_cpg_confchg_callback->joined_list_entries);
  457. break;
  458. case MESSAGE_RES_CPG_TOTEM_CONFCHG_CALLBACK:
  459. if (cpg_inst_copy.model_v1_data.cpg_totem_confchg_fn == NULL) {
  460. break;
  461. }
  462. res_cpg_totem_confchg_callback = (struct res_lib_cpg_totem_confchg_callback *)dispatch_data;
  463. marshall_from_mar_cpg_ring_id_t (&ring_id, &res_cpg_totem_confchg_callback->ring_id);
  464. for (i = 0; i < res_cpg_totem_confchg_callback->member_list_entries; i++) {
  465. totem_member_list[i] = res_cpg_totem_confchg_callback->member_list[i];
  466. }
  467. cpg_inst_copy.model_v1_data.cpg_totem_confchg_fn (handle,
  468. ring_id,
  469. res_cpg_totem_confchg_callback->member_list_entries,
  470. totem_member_list);
  471. break;
  472. default:
  473. error = CS_ERR_LIBRARY;
  474. goto error_put;
  475. break;
  476. } /* - switch (dispatch_data->id) */
  477. break; /* case CPG_MODEL_V1 */
  478. } /* - switch (cpg_inst_copy.model_data.model) */
  479. if (cpg_inst_copy.finalize || cpg_inst->finalize) {
  480. /*
  481. * If the finalize has been called then get out of the dispatch.
  482. */
  483. cpg_inst->finalize = 1;
  484. error = CS_ERR_BAD_HANDLE;
  485. goto error_put;
  486. }
  487. /*
  488. * Determine if more messages should be processed
  489. */
  490. if (dispatch_types == CS_DISPATCH_ONE || dispatch_types == CS_DISPATCH_ONE_NONBLOCKING) {
  491. cont = 0;
  492. }
  493. } while (cont);
  494. error_put:
  495. hdb_handle_put (&cpg_handle_t_db, handle);
  496. return (error);
  497. }
  498. cs_error_t cpg_join (
  499. cpg_handle_t handle,
  500. const struct cpg_name *group)
  501. {
  502. cs_error_t error;
  503. struct cpg_inst *cpg_inst;
  504. struct iovec iov[2];
  505. struct req_lib_cpg_join req_lib_cpg_join;
  506. struct res_lib_cpg_join response;
  507. if (group->length > CPG_MAX_NAME_LENGTH) {
  508. return (CS_ERR_NAME_TOO_LONG);
  509. }
  510. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  511. if (error != CS_OK) {
  512. return (error);
  513. }
  514. /* Now join */
  515. req_lib_cpg_join.header.size = sizeof (struct req_lib_cpg_join);
  516. req_lib_cpg_join.header.id = MESSAGE_REQ_CPG_JOIN;
  517. req_lib_cpg_join.pid = getpid();
  518. req_lib_cpg_join.flags = 0;
  519. switch (cpg_inst->model_data.model) {
  520. case CPG_MODEL_V1:
  521. req_lib_cpg_join.flags = cpg_inst->model_v1_data.flags;
  522. break;
  523. }
  524. marshall_to_mar_cpg_name_t (&req_lib_cpg_join.group_name,
  525. group);
  526. iov[0].iov_base = (void *)&req_lib_cpg_join;
  527. iov[0].iov_len = sizeof (struct req_lib_cpg_join);
  528. do {
  529. error = coroipcc_msg_send_reply_receive (cpg_inst->c, iov, 1,
  530. &response, sizeof (struct res_lib_cpg_join));
  531. if (error != CS_OK) {
  532. goto error_exit;
  533. }
  534. } while (response.header.error == CS_ERR_BUSY);
  535. error = response.header.error;
  536. error_exit:
  537. hdb_handle_put (&cpg_handle_t_db, handle);
  538. return (error);
  539. }
  540. cs_error_t cpg_leave (
  541. cpg_handle_t handle,
  542. const struct cpg_name *group)
  543. {
  544. cs_error_t error;
  545. struct cpg_inst *cpg_inst;
  546. struct iovec iov[2];
  547. struct req_lib_cpg_leave req_lib_cpg_leave;
  548. struct res_lib_cpg_leave res_lib_cpg_leave;
  549. if (group->length > CPG_MAX_NAME_LENGTH) {
  550. return (CS_ERR_NAME_TOO_LONG);
  551. }
  552. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  553. if (error != CS_OK) {
  554. return (error);
  555. }
  556. req_lib_cpg_leave.header.size = sizeof (struct req_lib_cpg_leave);
  557. req_lib_cpg_leave.header.id = MESSAGE_REQ_CPG_LEAVE;
  558. req_lib_cpg_leave.pid = getpid();
  559. marshall_to_mar_cpg_name_t (&req_lib_cpg_leave.group_name,
  560. group);
  561. iov[0].iov_base = (void *)&req_lib_cpg_leave;
  562. iov[0].iov_len = sizeof (struct req_lib_cpg_leave);
  563. do {
  564. error = coroipcc_msg_send_reply_receive (cpg_inst->c, iov, 1,
  565. &res_lib_cpg_leave, sizeof (struct res_lib_cpg_leave));
  566. if (error != CS_OK) {
  567. goto error_exit;
  568. }
  569. } while (res_lib_cpg_leave.header.error == CS_ERR_BUSY);
  570. error = res_lib_cpg_leave.header.error;
  571. error_exit:
  572. hdb_handle_put (&cpg_handle_t_db, handle);
  573. return (error);
  574. }
  575. cs_error_t cpg_membership_get (
  576. cpg_handle_t handle,
  577. struct cpg_name *group_name,
  578. struct cpg_address *member_list,
  579. int *member_list_entries)
  580. {
  581. cs_error_t error;
  582. struct cpg_inst *cpg_inst;
  583. struct iovec iov;
  584. struct req_lib_cpg_membership_get req_lib_cpg_membership_get;
  585. struct res_lib_cpg_membership_get res_lib_cpg_membership_get;
  586. unsigned int i;
  587. if (group_name->length > CPG_MAX_NAME_LENGTH) {
  588. return (CS_ERR_NAME_TOO_LONG);
  589. }
  590. if (member_list == NULL) {
  591. return (CS_ERR_INVALID_PARAM);
  592. }
  593. if (member_list_entries == NULL) {
  594. return (CS_ERR_INVALID_PARAM);
  595. }
  596. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  597. if (error != CS_OK) {
  598. return (error);
  599. }
  600. req_lib_cpg_membership_get.header.size = sizeof (struct req_lib_cpg_membership_get);
  601. req_lib_cpg_membership_get.header.id = MESSAGE_REQ_CPG_MEMBERSHIP;
  602. marshall_to_mar_cpg_name_t (&req_lib_cpg_membership_get.group_name,
  603. group_name);
  604. iov.iov_base = (void *)&req_lib_cpg_membership_get;
  605. iov.iov_len = sizeof (struct req_lib_cpg_membership_get);
  606. error = coroipcc_msg_send_reply_receive (cpg_inst->c, &iov, 1,
  607. &res_lib_cpg_membership_get, sizeof (res_lib_cpg_membership_get));
  608. if (error != CS_OK) {
  609. goto error_exit;
  610. }
  611. error = res_lib_cpg_membership_get.header.error;
  612. /*
  613. * Copy results to caller
  614. */
  615. *member_list_entries = res_lib_cpg_membership_get.member_count;
  616. if (member_list) {
  617. for (i = 0; i < res_lib_cpg_membership_get.member_count; i++) {
  618. marshall_from_mar_cpg_address_t (&member_list[i],
  619. &res_lib_cpg_membership_get.member_list[i]);
  620. }
  621. }
  622. error_exit:
  623. hdb_handle_put (&cpg_handle_t_db, handle);
  624. return (error);
  625. }
  626. cs_error_t cpg_local_get (
  627. cpg_handle_t handle,
  628. unsigned int *local_nodeid)
  629. {
  630. cs_error_t error;
  631. struct cpg_inst *cpg_inst;
  632. struct iovec iov;
  633. struct req_lib_cpg_local_get req_lib_cpg_local_get;
  634. struct res_lib_cpg_local_get res_lib_cpg_local_get;
  635. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  636. if (error != CS_OK) {
  637. return (error);
  638. }
  639. req_lib_cpg_local_get.header.size = sizeof (struct qb_ipc_request_header);
  640. req_lib_cpg_local_get.header.id = MESSAGE_REQ_CPG_LOCAL_GET;
  641. iov.iov_base = (void *)&req_lib_cpg_local_get;
  642. iov.iov_len = sizeof (struct req_lib_cpg_local_get);
  643. error = coroipcc_msg_send_reply_receive (cpg_inst->c, &iov, 1,
  644. &res_lib_cpg_local_get, sizeof (res_lib_cpg_local_get));
  645. if (error != CS_OK) {
  646. goto error_exit;
  647. }
  648. error = res_lib_cpg_local_get.header.error;
  649. *local_nodeid = res_lib_cpg_local_get.local_nodeid;
  650. error_exit:
  651. hdb_handle_put (&cpg_handle_t_db, handle);
  652. return (error);
  653. }
  654. cs_error_t cpg_flow_control_state_get (
  655. cpg_handle_t handle,
  656. cpg_flow_control_state_t *flow_control_state)
  657. {
  658. cs_error_t error;
  659. struct cpg_inst *cpg_inst;
  660. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  661. if (error != CS_OK) {
  662. return (error);
  663. }
  664. *flow_control_state = CPG_FLOW_CONTROL_DISABLED;
  665. error = CS_OK;
  666. hdb_handle_put (&cpg_handle_t_db, handle);
  667. return (error);
  668. }
  669. static int
  670. memory_map (char *path, const char *file, void **buf, size_t bytes)
  671. {
  672. int32_t fd;
  673. void *addr;
  674. int32_t res;
  675. char *buffer;
  676. int32_t i;
  677. size_t written;
  678. size_t page_size;
  679. long int sysconf_page_size;
  680. mode_t old_umask;
  681. snprintf (path, PATH_MAX, "/dev/shm/%s", file);
  682. old_umask = umask(CPG_MEMORY_MAP_UMASK);
  683. fd = mkstemp (path);
  684. (void)umask(old_umask);
  685. if (fd == -1) {
  686. snprintf (path, PATH_MAX, LOCALSTATEDIR "/run/%s", file);
  687. old_umask = umask(CPG_MEMORY_MAP_UMASK);
  688. fd = mkstemp (path);
  689. (void)umask(old_umask);
  690. if (fd == -1) {
  691. return (-1);
  692. }
  693. }
  694. res = ftruncate (fd, bytes);
  695. if (res == -1) {
  696. goto error_close_unlink;
  697. }
  698. sysconf_page_size = sysconf(_SC_PAGESIZE);
  699. if (sysconf_page_size <= 0) {
  700. goto error_close_unlink;
  701. }
  702. page_size = sysconf_page_size;
  703. buffer = malloc (page_size);
  704. if (buffer == NULL) {
  705. goto error_close_unlink;
  706. }
  707. memset (buffer, 0, page_size);
  708. for (i = 0; i < (bytes / page_size); i++) {
  709. retry_write:
  710. written = write (fd, buffer, page_size);
  711. if (written == -1 && errno == EINTR) {
  712. goto retry_write;
  713. }
  714. if (written != page_size) {
  715. free (buffer);
  716. goto error_close_unlink;
  717. }
  718. }
  719. free (buffer);
  720. addr = mmap (NULL, bytes, PROT_READ | PROT_WRITE,
  721. MAP_SHARED, fd, 0);
  722. if (addr == MAP_FAILED) {
  723. goto error_close_unlink;
  724. }
  725. #ifdef MADV_NOSYNC
  726. madvise(addr, bytes, MADV_NOSYNC);
  727. #endif
  728. res = close (fd);
  729. if (res) {
  730. return (-1);
  731. }
  732. *buf = addr;
  733. return 0;
  734. error_close_unlink:
  735. close (fd);
  736. unlink(path);
  737. return -1;
  738. }
  739. cs_error_t cpg_zcb_alloc (
  740. cpg_handle_t handle,
  741. size_t size,
  742. void **buffer)
  743. {
  744. void *buf = NULL;
  745. char path[PATH_MAX];
  746. mar_req_coroipcc_zc_alloc_t req_coroipcc_zc_alloc;
  747. struct qb_ipc_response_header res_coroipcs_zc_alloc;
  748. size_t map_size;
  749. struct iovec iovec;
  750. struct coroipcs_zc_header *hdr;
  751. cs_error_t error;
  752. struct cpg_inst *cpg_inst;
  753. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  754. if (error != CS_OK) {
  755. return (error);
  756. }
  757. map_size = size + sizeof (struct req_lib_cpg_mcast) + sizeof (struct coroipcs_zc_header);
  758. assert(memory_map (path, "corosync_zerocopy-XXXXXX", &buf, map_size) != -1);
  759. if (strlen(path) >= CPG_ZC_PATH_LEN) {
  760. unlink(path);
  761. munmap (buf, map_size);
  762. return (CS_ERR_NAME_TOO_LONG);
  763. }
  764. req_coroipcc_zc_alloc.header.size = sizeof (mar_req_coroipcc_zc_alloc_t);
  765. req_coroipcc_zc_alloc.header.id = MESSAGE_REQ_CPG_ZC_ALLOC;
  766. req_coroipcc_zc_alloc.map_size = map_size;
  767. strcpy (req_coroipcc_zc_alloc.path_to_file, path);
  768. iovec.iov_base = (void *)&req_coroipcc_zc_alloc;
  769. iovec.iov_len = sizeof (mar_req_coroipcc_zc_alloc_t);
  770. error = coroipcc_msg_send_reply_receive (
  771. cpg_inst->c,
  772. &iovec,
  773. 1,
  774. &res_coroipcs_zc_alloc,
  775. sizeof (struct qb_ipc_response_header));
  776. hdr = (struct coroipcs_zc_header *)buf;
  777. hdr->map_size = map_size;
  778. *buffer = ((char *)buf) + sizeof (struct coroipcs_zc_header);
  779. hdb_handle_put (&cpg_handle_t_db, handle);
  780. *buffer = ((char *)*buffer) + sizeof (struct req_lib_cpg_mcast);
  781. return (error);
  782. }
  783. cs_error_t cpg_zcb_free (
  784. cpg_handle_t handle,
  785. void *buffer)
  786. {
  787. cs_error_t error;
  788. struct cpg_inst *cpg_inst;
  789. mar_req_coroipcc_zc_free_t req_coroipcc_zc_free;
  790. struct qb_ipc_response_header res_coroipcs_zc_free;
  791. struct iovec iovec;
  792. struct coroipcs_zc_header *header = (struct coroipcs_zc_header *)((char *)buffer - sizeof (struct coroipcs_zc_header));
  793. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  794. if (error != CS_OK) {
  795. return (error);
  796. }
  797. req_coroipcc_zc_free.header.size = sizeof (mar_req_coroipcc_zc_free_t);
  798. req_coroipcc_zc_free.header.id = MESSAGE_REQ_CPG_ZC_FREE;
  799. req_coroipcc_zc_free.map_size = header->map_size;
  800. req_coroipcc_zc_free.server_address = header->server_address;
  801. iovec.iov_base = (void *)&req_coroipcc_zc_free;
  802. iovec.iov_len = sizeof (mar_req_coroipcc_zc_free_t);
  803. error = coroipcc_msg_send_reply_receive (
  804. cpg_inst->c,
  805. &iovec,
  806. 1,
  807. &res_coroipcs_zc_free,
  808. sizeof (struct qb_ipc_response_header));
  809. munmap ((void *)header, header->map_size);
  810. hdb_handle_put (&cpg_handle_t_db, handle);
  811. return (error);
  812. }
  813. cs_error_t cpg_zcb_mcast_joined (
  814. cpg_handle_t handle,
  815. cpg_guarantee_t guarantee,
  816. void *msg,
  817. size_t msg_len)
  818. {
  819. cs_error_t error;
  820. struct cpg_inst *cpg_inst;
  821. struct req_lib_cpg_mcast *req_lib_cpg_mcast;
  822. struct res_lib_cpg_mcast res_lib_cpg_mcast;
  823. mar_req_coroipcc_zc_execute_t req_coroipcc_zc_execute;
  824. struct coroipcs_zc_header *hdr;
  825. struct iovec iovec;
  826. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  827. if (error != CS_OK) {
  828. return (error);
  829. }
  830. if (msg_len > IPC_REQUEST_SIZE) {
  831. error = CS_ERR_TOO_BIG;
  832. goto error_exit;
  833. }
  834. req_lib_cpg_mcast = (struct req_lib_cpg_mcast *)(((char *)msg) - sizeof (struct req_lib_cpg_mcast));
  835. req_lib_cpg_mcast->header.size = sizeof (struct req_lib_cpg_mcast) +
  836. msg_len;
  837. req_lib_cpg_mcast->header.id = MESSAGE_REQ_CPG_MCAST;
  838. req_lib_cpg_mcast->guarantee = guarantee;
  839. req_lib_cpg_mcast->msglen = msg_len;
  840. hdr = (struct coroipcs_zc_header *)(((char *)req_lib_cpg_mcast) - sizeof (struct coroipcs_zc_header));
  841. req_coroipcc_zc_execute.header.size = sizeof (mar_req_coroipcc_zc_execute_t);
  842. req_coroipcc_zc_execute.header.id = MESSAGE_REQ_CPG_ZC_EXECUTE;
  843. req_coroipcc_zc_execute.server_address = hdr->server_address;
  844. iovec.iov_base = (void *)&req_coroipcc_zc_execute;
  845. iovec.iov_len = sizeof (mar_req_coroipcc_zc_execute_t);
  846. error = coroipcc_msg_send_reply_receive (
  847. cpg_inst->c,
  848. &iovec,
  849. 1,
  850. &res_lib_cpg_mcast,
  851. sizeof(res_lib_cpg_mcast));
  852. if (error != CS_OK) {
  853. goto error_exit;
  854. }
  855. error = res_lib_cpg_mcast.header.error;
  856. error_exit:
  857. hdb_handle_put (&cpg_handle_t_db, handle);
  858. return (error);
  859. }
  860. static cs_error_t send_fragments (
  861. struct cpg_inst *cpg_inst,
  862. cpg_guarantee_t guarantee,
  863. size_t msg_len,
  864. const struct iovec *iovec,
  865. unsigned int iov_len)
  866. {
  867. int i;
  868. cs_error_t error = CS_OK;
  869. struct iovec iov[2];
  870. struct req_lib_cpg_partial_mcast req_lib_cpg_mcast;
  871. struct res_lib_cpg_partial_send res_lib_cpg_partial_send;
  872. size_t sent = 0;
  873. size_t iov_sent = 0;
  874. int retry_count;
  875. req_lib_cpg_mcast.header.id = MESSAGE_REQ_CPG_PARTIAL_MCAST;
  876. req_lib_cpg_mcast.guarantee = guarantee;
  877. req_lib_cpg_mcast.msglen = msg_len;
  878. iov[0].iov_base = (void *)&req_lib_cpg_mcast;
  879. iov[0].iov_len = sizeof (struct req_lib_cpg_partial_mcast);
  880. i=0;
  881. iov_sent = 0 ;
  882. qb_ipcc_fc_enable_max_set(cpg_inst->c, 2);
  883. while (error == CS_OK && sent < msg_len) {
  884. retry_count = 0;
  885. if ( (iovec[i].iov_len - iov_sent) > cpg_inst->max_msg_size) {
  886. iov[1].iov_len = cpg_inst->max_msg_size;
  887. }
  888. else {
  889. iov[1].iov_len = iovec[i].iov_len - iov_sent;
  890. }
  891. if (sent == 0) {
  892. req_lib_cpg_mcast.type = LIBCPG_PARTIAL_FIRST;
  893. }
  894. else if ((sent + iov[1].iov_len) == msg_len) {
  895. req_lib_cpg_mcast.type = LIBCPG_PARTIAL_LAST;
  896. }
  897. else {
  898. req_lib_cpg_mcast.type = LIBCPG_PARTIAL_CONTINUED;
  899. }
  900. req_lib_cpg_mcast.fraglen = iov[1].iov_len;
  901. req_lib_cpg_mcast.header.size = sizeof (struct req_lib_cpg_partial_mcast) + iov[1].iov_len;
  902. iov[1].iov_base = (char *)iovec[i].iov_base + iov_sent;
  903. resend:
  904. error = coroipcc_msg_send_reply_receive (cpg_inst->c, iov, 2,
  905. &res_lib_cpg_partial_send,
  906. sizeof (res_lib_cpg_partial_send));
  907. if (error == CS_ERR_TRY_AGAIN) {
  908. fprintf(stderr, "sleep. counter=%d\n", retry_count);
  909. if (++retry_count > MAX_RETRIES) {
  910. goto error_exit;
  911. }
  912. usleep(10000);
  913. goto resend;
  914. }
  915. iov_sent += iov[1].iov_len;
  916. sent += iov[1].iov_len;
  917. /* Next iovec */
  918. if (iov_sent >= iovec[i].iov_len) {
  919. i++;
  920. iov_sent = 0;
  921. }
  922. error = res_lib_cpg_partial_send.header.error;
  923. }
  924. error_exit:
  925. qb_ipcc_fc_enable_max_set(cpg_inst->c, 1);
  926. return error;
  927. }
  928. cs_error_t cpg_mcast_joined (
  929. cpg_handle_t handle,
  930. cpg_guarantee_t guarantee,
  931. const struct iovec *iovec,
  932. unsigned int iov_len)
  933. {
  934. int i;
  935. cs_error_t error;
  936. struct cpg_inst *cpg_inst;
  937. struct iovec iov[64];
  938. struct req_lib_cpg_mcast req_lib_cpg_mcast;
  939. size_t msg_len = 0;
  940. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  941. if (error != CS_OK) {
  942. return (error);
  943. }
  944. for (i = 0; i < iov_len; i++ ) {
  945. msg_len += iovec[i].iov_len;
  946. }
  947. if (msg_len > cpg_inst->max_msg_size) {
  948. error = send_fragments(cpg_inst, guarantee, msg_len, iovec, iov_len);
  949. goto error_exit;
  950. }
  951. req_lib_cpg_mcast.header.size = sizeof (struct req_lib_cpg_mcast) +
  952. msg_len;
  953. req_lib_cpg_mcast.header.id = MESSAGE_REQ_CPG_MCAST;
  954. req_lib_cpg_mcast.guarantee = guarantee;
  955. req_lib_cpg_mcast.msglen = msg_len;
  956. iov[0].iov_base = (void *)&req_lib_cpg_mcast;
  957. iov[0].iov_len = sizeof (struct req_lib_cpg_mcast);
  958. memcpy (&iov[1], iovec, iov_len * sizeof (struct iovec));
  959. qb_ipcc_fc_enable_max_set(cpg_inst->c, 2);
  960. error = qb_to_cs_error(qb_ipcc_sendv(cpg_inst->c, iov, iov_len + 1));
  961. qb_ipcc_fc_enable_max_set(cpg_inst->c, 1);
  962. error_exit:
  963. hdb_handle_put (&cpg_handle_t_db, handle);
  964. return (error);
  965. }
  966. cs_error_t cpg_iteration_initialize(
  967. cpg_handle_t handle,
  968. cpg_iteration_type_t iteration_type,
  969. const struct cpg_name *group,
  970. cpg_iteration_handle_t *cpg_iteration_handle)
  971. {
  972. cs_error_t error;
  973. struct iovec iov;
  974. struct cpg_inst *cpg_inst;
  975. struct cpg_iteration_instance_t *cpg_iteration_instance;
  976. struct req_lib_cpg_iterationinitialize req_lib_cpg_iterationinitialize;
  977. struct res_lib_cpg_iterationinitialize res_lib_cpg_iterationinitialize;
  978. if (group && group->length > CPG_MAX_NAME_LENGTH) {
  979. return (CS_ERR_NAME_TOO_LONG);
  980. }
  981. if (cpg_iteration_handle == NULL) {
  982. return (CS_ERR_INVALID_PARAM);
  983. }
  984. if ((iteration_type == CPG_ITERATION_ONE_GROUP && group == NULL) ||
  985. (iteration_type != CPG_ITERATION_ONE_GROUP && group != NULL)) {
  986. return (CS_ERR_INVALID_PARAM);
  987. }
  988. if (iteration_type != CPG_ITERATION_NAME_ONLY && iteration_type != CPG_ITERATION_ONE_GROUP &&
  989. iteration_type != CPG_ITERATION_ALL) {
  990. return (CS_ERR_INVALID_PARAM);
  991. }
  992. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  993. if (error != CS_OK) {
  994. return (error);
  995. }
  996. error = hdb_error_to_cs (hdb_handle_create (&cpg_iteration_handle_t_db,
  997. sizeof (struct cpg_iteration_instance_t), cpg_iteration_handle));
  998. if (error != CS_OK) {
  999. goto error_put_cpg_db;
  1000. }
  1001. error = hdb_error_to_cs (hdb_handle_get (&cpg_iteration_handle_t_db, *cpg_iteration_handle,
  1002. (void *)&cpg_iteration_instance));
  1003. if (error != CS_OK) {
  1004. goto error_destroy;
  1005. }
  1006. cpg_iteration_instance->conn = cpg_inst->c;
  1007. list_init (&cpg_iteration_instance->list);
  1008. req_lib_cpg_iterationinitialize.header.size = sizeof (struct req_lib_cpg_iterationinitialize);
  1009. req_lib_cpg_iterationinitialize.header.id = MESSAGE_REQ_CPG_ITERATIONINITIALIZE;
  1010. req_lib_cpg_iterationinitialize.iteration_type = iteration_type;
  1011. if (group) {
  1012. marshall_to_mar_cpg_name_t (&req_lib_cpg_iterationinitialize.group_name, group);
  1013. }
  1014. iov.iov_base = (void *)&req_lib_cpg_iterationinitialize;
  1015. iov.iov_len = sizeof (struct req_lib_cpg_iterationinitialize);
  1016. error = coroipcc_msg_send_reply_receive (cpg_inst->c,
  1017. &iov,
  1018. 1,
  1019. &res_lib_cpg_iterationinitialize,
  1020. sizeof (struct res_lib_cpg_iterationinitialize));
  1021. if (error != CS_OK) {
  1022. goto error_put_destroy;
  1023. }
  1024. cpg_iteration_instance->executive_iteration_handle =
  1025. res_lib_cpg_iterationinitialize.iteration_handle;
  1026. cpg_iteration_instance->cpg_iteration_handle = *cpg_iteration_handle;
  1027. list_add (&cpg_iteration_instance->list, &cpg_inst->iteration_list_head);
  1028. hdb_handle_put (&cpg_iteration_handle_t_db, *cpg_iteration_handle);
  1029. hdb_handle_put (&cpg_handle_t_db, handle);
  1030. return (res_lib_cpg_iterationinitialize.header.error);
  1031. error_put_destroy:
  1032. hdb_handle_put (&cpg_iteration_handle_t_db, *cpg_iteration_handle);
  1033. error_destroy:
  1034. hdb_handle_destroy (&cpg_iteration_handle_t_db, *cpg_iteration_handle);
  1035. error_put_cpg_db:
  1036. hdb_handle_put (&cpg_handle_t_db, handle);
  1037. return (error);
  1038. }
  1039. cs_error_t cpg_iteration_next(
  1040. cpg_iteration_handle_t handle,
  1041. struct cpg_iteration_description_t *description)
  1042. {
  1043. cs_error_t error;
  1044. struct cpg_iteration_instance_t *cpg_iteration_instance;
  1045. struct req_lib_cpg_iterationnext req_lib_cpg_iterationnext;
  1046. struct res_lib_cpg_iterationnext res_lib_cpg_iterationnext;
  1047. if (description == NULL) {
  1048. return CS_ERR_INVALID_PARAM;
  1049. }
  1050. error = hdb_error_to_cs (hdb_handle_get (&cpg_iteration_handle_t_db, handle,
  1051. (void *)&cpg_iteration_instance));
  1052. if (error != CS_OK) {
  1053. goto error_exit;
  1054. }
  1055. req_lib_cpg_iterationnext.header.size = sizeof (struct req_lib_cpg_iterationnext);
  1056. req_lib_cpg_iterationnext.header.id = MESSAGE_REQ_CPG_ITERATIONNEXT;
  1057. req_lib_cpg_iterationnext.iteration_handle = cpg_iteration_instance->executive_iteration_handle;
  1058. error = qb_to_cs_error (qb_ipcc_send (cpg_iteration_instance->conn,
  1059. &req_lib_cpg_iterationnext,
  1060. req_lib_cpg_iterationnext.header.size));
  1061. if (error != CS_OK) {
  1062. goto error_put;
  1063. }
  1064. error = qb_to_cs_error (qb_ipcc_recv (cpg_iteration_instance->conn,
  1065. &res_lib_cpg_iterationnext,
  1066. sizeof(struct res_lib_cpg_iterationnext), -1));
  1067. if (error != CS_OK) {
  1068. goto error_put;
  1069. }
  1070. marshall_from_mar_cpg_iteration_description_t(
  1071. description,
  1072. &res_lib_cpg_iterationnext.description);
  1073. error = res_lib_cpg_iterationnext.header.error;
  1074. error_put:
  1075. hdb_handle_put (&cpg_iteration_handle_t_db, handle);
  1076. error_exit:
  1077. return (error);
  1078. }
  1079. cs_error_t cpg_iteration_finalize (
  1080. cpg_iteration_handle_t handle)
  1081. {
  1082. cs_error_t error;
  1083. struct iovec iov;
  1084. struct cpg_iteration_instance_t *cpg_iteration_instance;
  1085. struct req_lib_cpg_iterationfinalize req_lib_cpg_iterationfinalize;
  1086. struct res_lib_cpg_iterationfinalize res_lib_cpg_iterationfinalize;
  1087. error = hdb_error_to_cs (hdb_handle_get (&cpg_iteration_handle_t_db, handle,
  1088. (void *)&cpg_iteration_instance));
  1089. if (error != CS_OK) {
  1090. goto error_exit;
  1091. }
  1092. req_lib_cpg_iterationfinalize.header.size = sizeof (struct req_lib_cpg_iterationfinalize);
  1093. req_lib_cpg_iterationfinalize.header.id = MESSAGE_REQ_CPG_ITERATIONFINALIZE;
  1094. req_lib_cpg_iterationfinalize.iteration_handle = cpg_iteration_instance->executive_iteration_handle;
  1095. iov.iov_base = (void *)&req_lib_cpg_iterationfinalize;
  1096. iov.iov_len = sizeof (struct req_lib_cpg_iterationfinalize);
  1097. error = coroipcc_msg_send_reply_receive (cpg_iteration_instance->conn,
  1098. &iov,
  1099. 1,
  1100. &res_lib_cpg_iterationfinalize,
  1101. sizeof (struct req_lib_cpg_iterationfinalize));
  1102. if (error != CS_OK) {
  1103. goto error_put;
  1104. }
  1105. cpg_iteration_instance_finalize (cpg_iteration_instance);
  1106. hdb_handle_put (&cpg_iteration_handle_t_db, cpg_iteration_instance->cpg_iteration_handle);
  1107. return (res_lib_cpg_iterationfinalize.header.error);
  1108. error_put:
  1109. hdb_handle_put (&cpg_iteration_handle_t_db, handle);
  1110. error_exit:
  1111. return (error);
  1112. }
  1113. /** @} */