totempg.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203
  1. /*
  2. * Copyright (c) 2003-2005 MontaVista Software, Inc.
  3. * Copyright (c) 2005 OSDL.
  4. *
  5. * All rights reserved.
  6. *
  7. * Author: Steven Dake (sdake@mvista.com)
  8. * Mark Haverkamp (markh@osdl.org)
  9. *
  10. * This software licensed under BSD license, the text of which follows:
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. * - Redistributions in binary form must reproduce the above copyright notice,
  18. * this list of conditions and the following disclaimer in the documentation
  19. * and/or other materials provided with the distribution.
  20. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  21. * contributors may be used to endorse or promote products derived from this
  22. * software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  28. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  31. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  32. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  33. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  34. * THE POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. /*
  37. * FRAGMENTATION AND PACKING ALGORITHM:
  38. *
  39. * Assemble the entire message into one buffer
  40. * if full fragment
  41. * store fragment into lengths list
  42. * for each full fragment
  43. * multicast fragment
  44. * set length and fragment fields of pg mesage
  45. * store remaining multicast into head of fragmentation data and set lens field
  46. *
  47. * If a message exceeds the maximum packet size allowed by the totem
  48. * single ring protocol, the protocol could lose forward progress.
  49. * Statically calculating the allowed data amount doesn't work because
  50. * the amount of data allowed depends on the number of fragments in
  51. * each message. In this implementation, the maximum fragment size
  52. * is dynamically calculated for each fragment added to the message.
  53. * It is possible for a message to be two bytes short of the maximum
  54. * packet size. This occurs when a message or collection of
  55. * messages + the mcast header + the lens are two bytes short of the
  56. * end of the packet. Since another len field consumes two bytes, the
  57. * len field would consume the rest of the packet without room for data.
  58. *
  59. * One optimization would be to forgo the final len field and determine
  60. * it from the size of the udp datagram. Then this condition would no
  61. * longer occur.
  62. */
  63. /*
  64. * ASSEMBLY AND UNPACKING ALGORITHM:
  65. *
  66. * copy incoming packet into assembly data buffer indexed by current
  67. * location of end of fragment
  68. *
  69. * if not fragmented
  70. * deliver all messages in assembly data buffer
  71. * else
  72. * if msg_count > 1 and fragmented
  73. * deliver all messages except last message in assembly data buffer
  74. * copy last fragmented section to start of assembly data buffer
  75. * else
  76. * if msg_count = 1 and fragmented
  77. * do nothing
  78. *
  79. */
  80. #include <netinet/in.h>
  81. #include <sys/uio.h>
  82. #include <stdio.h>
  83. #include <stdlib.h>
  84. #include <string.h>
  85. #include <assert.h>
  86. #include <pthread.h>
  87. #include "../include/hdb.h"
  88. #include "../include/list.h"
  89. #include "totempg.h"
  90. #include "totemmrp.h"
  91. #include "totemsrp.h"
  92. #include "swab.h"
  93. #define min(a,b) ((a) < (b)) ? a : b
  94. struct totempg_mcast_header {
  95. short version;
  96. short type;
  97. };
  98. /*
  99. * totempg_mcast structure
  100. *
  101. * header: Identify the mcast.
  102. * fragmented: Set if this message continues into next message
  103. * continuation: Set if this message is a continuation from last message
  104. * msg_count Indicates how many packed messages are contained
  105. * in the mcast.
  106. * Also, the size of each packed message and the messages themselves are
  107. * appended to the end of this structure when sent.
  108. */
  109. struct totempg_mcast {
  110. struct totempg_mcast_header header;
  111. unsigned char fragmented;
  112. unsigned char continuation;
  113. unsigned short msg_count;
  114. /*
  115. * short msg_len[msg_count];
  116. */
  117. /*
  118. * data for messages
  119. */
  120. };
  121. /*
  122. * Maximum packet size for totem pg messages
  123. */
  124. #define TOTEMPG_PACKET_SIZE (totempg_totem_config->net_mtu - \
  125. sizeof (struct totempg_mcast))
  126. /*
  127. * Local variables used for packing small messages
  128. */
  129. static unsigned short mcast_packed_msg_lens[FRAME_SIZE_MAX];
  130. static int mcast_packed_msg_count = 0;
  131. /*
  132. * Function and data used to log messages
  133. */
  134. static int totempg_log_level_security;
  135. static int totempg_log_level_error;
  136. static int totempg_log_level_warning;
  137. static int totempg_log_level_notice;
  138. static int totempg_log_level_debug;
  139. static void (*totempg_log_printf) (char *file, int line, int level, char *format, ...) = NULL;
  140. struct totem_config *totempg_totem_config;
  141. struct assembly {
  142. unsigned int nodeid;
  143. unsigned char data[MESSAGE_SIZE_MAX];
  144. int index;
  145. unsigned char last_frag_num;
  146. struct list_head list;
  147. };
  148. DECLARE_LIST_INIT(assembly_list_inuse);
  149. DECLARE_LIST_INIT(assembly_list_free);
  150. /*
  151. * Staging buffer for packed messages. Messages are staged in this buffer
  152. * before sending. Multiple messages may fit which cuts down on the
  153. * number of mcasts sent. If a message doesn't completely fit, then
  154. * the mcast header has a fragment bit set that says that there are more
  155. * data to follow. fragment_size is an index into the buffer. It indicates
  156. * the size of message data and where to place new message data.
  157. * fragment_contuation indicates whether the first packed message in
  158. * the buffer is a continuation of a previously packed fragment.
  159. */
  160. static unsigned char *fragmentation_data;
  161. static int fragment_size = 0;
  162. static int fragment_continuation = 0;
  163. static struct iovec iov_delv;
  164. static unsigned int totempg_max_handle = 0;
  165. struct totempg_group_instance {
  166. void (*deliver_fn) (
  167. unsigned int nodeid,
  168. struct iovec *iovec,
  169. int iov_len,
  170. int endian_conversion_required);
  171. void (*confchg_fn) (
  172. enum totem_configuration_type configuration_type,
  173. unsigned int *member_list, int member_list_entries,
  174. unsigned int *left_list, int left_list_entries,
  175. unsigned int *joined_list, int joined_list_entries,
  176. struct memb_ring_id *ring_id);
  177. struct totempg_group *groups;
  178. int groups_cnt;
  179. };
  180. static struct hdb_handle_database totempg_groups_instance_database = {
  181. .handle_count = 0,
  182. .handles = 0,
  183. .iterator = 0,
  184. .mutex = PTHREAD_MUTEX_INITIALIZER
  185. };
  186. static int send_ok (int msg_size);
  187. static unsigned char next_fragment = 1;
  188. static pthread_mutex_t totempg_mutex = PTHREAD_MUTEX_INITIALIZER;
  189. static pthread_mutex_t callback_token_mutex = PTHREAD_MUTEX_INITIALIZER;
  190. static pthread_mutex_t mcast_msg_mutex = PTHREAD_MUTEX_INITIALIZER;
  191. #define log_printf(level, format, args...) \
  192. totempg_log_printf (__FILE__, __LINE__, level, format, ##args)
  193. static struct assembly *assembly_ref (unsigned int nodeid)
  194. {
  195. struct assembly *assembly;
  196. struct list_head *list;
  197. /*
  198. * Search inuse list for node id and return assembly buffer if found
  199. */
  200. for (list = assembly_list_inuse.next;
  201. list != &assembly_list_inuse;
  202. list = list->next) {
  203. assembly = list_entry (list, struct assembly, list);
  204. if (nodeid == assembly->nodeid) {
  205. return (assembly);
  206. }
  207. }
  208. /*
  209. * Nothing found in inuse list get one from free list if available
  210. */
  211. if (list_empty (&assembly_list_free) == 0) {
  212. assembly = list_entry (assembly_list_free.next, struct assembly, list);
  213. list_del (&assembly->list);
  214. list_add (&assembly->list, &assembly_list_inuse);
  215. assembly->nodeid = nodeid;
  216. return (assembly);
  217. }
  218. /*
  219. * Nothing available in inuse or free list, so allocate a new one
  220. */
  221. assembly = malloc (sizeof (struct assembly));
  222. memset (assembly, 0, sizeof (struct assembly));
  223. /*
  224. * TODO handle memory allocation failure here
  225. */
  226. assert (assembly);
  227. assembly->nodeid = nodeid;
  228. list_init (&assembly->list);
  229. list_add (&assembly->list, &assembly_list_inuse);
  230. return (assembly);
  231. }
  232. void assembly_deref (struct assembly *assembly)
  233. {
  234. list_del (&assembly->list);
  235. list_add (&assembly->list, &assembly_list_free);
  236. }
  237. static inline void app_confchg_fn (
  238. enum totem_configuration_type configuration_type,
  239. unsigned int *member_list, int member_list_entries,
  240. unsigned int *left_list, int left_list_entries,
  241. unsigned int *joined_list, int joined_list_entries,
  242. struct memb_ring_id *ring_id)
  243. {
  244. int i;
  245. struct totempg_group_instance *instance;
  246. unsigned int res;
  247. for (i = 0; i <= totempg_max_handle; i++) {
  248. res = hdb_handle_get (&totempg_groups_instance_database,
  249. i, (void *)&instance);
  250. if (res == 0) {
  251. if (instance->confchg_fn) {
  252. instance->confchg_fn (
  253. configuration_type,
  254. member_list,
  255. member_list_entries,
  256. left_list,
  257. left_list_entries,
  258. joined_list,
  259. joined_list_entries,
  260. ring_id);
  261. }
  262. hdb_handle_put (&totempg_groups_instance_database, i);
  263. }
  264. }
  265. }
  266. static inline void group_endian_convert (
  267. struct iovec *iovec)
  268. {
  269. unsigned short *group_len;
  270. int i;
  271. group_len = (unsigned short *)iovec->iov_base;
  272. group_len[0] = swab16(group_len[0]);
  273. for (i = 1; i < group_len[0] + 1; i++) {
  274. group_len[i] = swab16(group_len[i]);
  275. }
  276. }
  277. static inline int group_matches (
  278. struct iovec *iovec,
  279. unsigned int iov_len,
  280. struct totempg_group *groups_b,
  281. unsigned int group_b_cnt,
  282. unsigned int *adjust_iovec)
  283. {
  284. unsigned short *group_len;
  285. char *group_name;
  286. int i;
  287. int j;
  288. assert (iov_len == 1);
  289. group_len = (unsigned short *)iovec->iov_base;
  290. group_name = ((char *)iovec->iov_base) +
  291. sizeof (unsigned short) * (group_len[0] + 1);
  292. /*
  293. * Calculate amount to adjust the iovec by before delivering to app
  294. */
  295. *adjust_iovec = sizeof (unsigned short) * (group_len[0] + 1);
  296. for (i = 1; i < group_len[0] + 1; i++) {
  297. *adjust_iovec += group_len[i];
  298. }
  299. /*
  300. * Determine if this message should be delivered to this instance
  301. */
  302. for (i = 1; i < group_len[0] + 1; i++) {
  303. for (j = 0; j < group_b_cnt; j++) {
  304. if ((group_len[i] == groups_b[j].group_len) &&
  305. (memcmp (groups_b[j].group, group_name, group_len[i]) == 0)) {
  306. return (1);
  307. }
  308. }
  309. group_name += group_len[i];
  310. }
  311. return (0);
  312. }
  313. static inline void app_deliver_fn (
  314. unsigned int nodeid,
  315. struct iovec *iovec,
  316. unsigned int iov_len,
  317. int endian_conversion_required)
  318. {
  319. int i;
  320. struct totempg_group_instance *instance;
  321. struct iovec stripped_iovec;
  322. unsigned int adjust_iovec;
  323. unsigned int res;
  324. if (endian_conversion_required) {
  325. group_endian_convert (iovec);
  326. }
  327. for (i = 0; i <= totempg_max_handle; i++) {
  328. res = hdb_handle_get (&totempg_groups_instance_database,
  329. i, (void *)&instance);
  330. if (res == 0) {
  331. assert (iov_len == 1);
  332. if (group_matches (iovec, iov_len, instance->groups, instance->groups_cnt, &adjust_iovec)) {
  333. stripped_iovec.iov_len = iovec->iov_len - adjust_iovec;
  334. stripped_iovec.iov_base = (char *)iovec->iov_base + adjust_iovec;
  335. instance->deliver_fn (
  336. nodeid,
  337. &stripped_iovec,
  338. iov_len,
  339. endian_conversion_required);
  340. }
  341. hdb_handle_put (&totempg_groups_instance_database, i);
  342. }
  343. }
  344. }
  345. static void totempg_confchg_fn (
  346. enum totem_configuration_type configuration_type,
  347. unsigned int *member_list, int member_list_entries,
  348. unsigned int *left_list, int left_list_entries,
  349. unsigned int *joined_list, int joined_list_entries,
  350. struct memb_ring_id *ring_id)
  351. {
  352. // TODO optimize this
  353. app_confchg_fn (configuration_type,
  354. member_list, member_list_entries,
  355. left_list, left_list_entries,
  356. joined_list, joined_list_entries,
  357. ring_id);
  358. }
  359. static void totempg_deliver_fn (
  360. unsigned int nodeid,
  361. struct iovec *iovec,
  362. int iov_len,
  363. int endian_conversion_required)
  364. {
  365. struct totempg_mcast *mcast;
  366. unsigned short *msg_lens;
  367. int i;
  368. struct assembly *assembly;
  369. char header[FRAME_SIZE_MAX];
  370. int h_index;
  371. int a_i = 0;
  372. int msg_count;
  373. int continuation;
  374. int start;
  375. assembly = assembly_ref (nodeid);
  376. assert (assembly);
  377. /*
  378. * Assemble the header into one block of data and
  379. * assemble the packet contents into one block of data to simplify delivery
  380. */
  381. if (iov_len == 1) {
  382. /*
  383. * This message originated from external processor
  384. * because there is only one iovec for the full msg.
  385. */
  386. char *data;
  387. int datasize;
  388. mcast = (struct totempg_mcast *)iovec[0].iov_base;
  389. if (endian_conversion_required) {
  390. mcast->msg_count = swab16 (mcast->msg_count);
  391. }
  392. msg_count = mcast->msg_count;
  393. datasize = sizeof (struct totempg_mcast) +
  394. msg_count * sizeof (unsigned short);
  395. memcpy (header, iovec[0].iov_base, datasize);
  396. assert(iovec);
  397. data = iovec[0].iov_base;
  398. msg_lens = (unsigned short *) (header + sizeof (struct totempg_mcast));
  399. if (endian_conversion_required) {
  400. for (i = 0; i < mcast->msg_count; i++) {
  401. msg_lens[i] = swab16 (msg_lens[i]);
  402. }
  403. }
  404. memcpy (&assembly->data[assembly->index], &data[datasize],
  405. iovec[0].iov_len - datasize);
  406. } else {
  407. /*
  408. * The message originated from local processor
  409. * becasue there is greater than one iovec for then full msg.
  410. */
  411. h_index = 0;
  412. for (i = 0; i < 2; i++) {
  413. memcpy (&header[h_index], iovec[i].iov_base, iovec[i].iov_len);
  414. h_index += iovec[i].iov_len;
  415. }
  416. mcast = (struct totempg_mcast *)header;
  417. // TODO make sure we are using a copy of mcast not the actual data itself
  418. msg_lens = (unsigned short *) (header + sizeof (struct totempg_mcast));
  419. for (i = 2; i < iov_len; i++) {
  420. a_i = assembly->index;
  421. assert (iovec[i].iov_len + a_i <= MESSAGE_SIZE_MAX);
  422. memcpy (&assembly->data[a_i], iovec[i].iov_base, iovec[i].iov_len);
  423. a_i += msg_lens[i - 2];
  424. }
  425. iov_len -= 2;
  426. }
  427. /*
  428. * If the last message in the buffer is a fragment, then we
  429. * can't deliver it. We'll first deliver the full messages
  430. * then adjust the assembly buffer so we can add the rest of the
  431. * fragment when it arrives.
  432. */
  433. msg_count = mcast->fragmented ? mcast->msg_count - 1 : mcast->msg_count;
  434. continuation = mcast->continuation;
  435. iov_delv.iov_base = &assembly->data[0];
  436. iov_delv.iov_len = assembly->index + msg_lens[0];
  437. /*
  438. * Make sure that if this message is a continuation, that it
  439. * matches the sequence number of the previous fragment.
  440. * Also, if the first packed message is a continuation
  441. * of a previous message, but the assembly buffer
  442. * is empty, then we need to discard it since we can't
  443. * assemble a complete message. Likewise, if this message isn't a
  444. * continuation and the assembly buffer is empty, we have to discard
  445. * the continued message.
  446. */
  447. start = 0;
  448. if (continuation) {
  449. if (continuation != assembly->last_frag_num) {
  450. log_printf (totempg_log_level_error,
  451. "Message continuation doesn't match previous frag e: %u - a: %u\n",
  452. assembly->last_frag_num, continuation);
  453. continuation = 0;
  454. }
  455. if ((assembly->index == 0) ||
  456. (!continuation && assembly->index)) {
  457. log_printf (totempg_log_level_error,
  458. "Throwing away broken message: continuation %u, index %u\n",
  459. continuation, assembly->index);
  460. continuation = 0;
  461. }
  462. /*
  463. * we decided to throw away the first continued message
  464. * in this buffer, if continuation was set to zero.
  465. */
  466. if (!continuation) {
  467. assembly->index += msg_lens[0];
  468. iov_delv.iov_base = &assembly->data[assembly->index];
  469. iov_delv.iov_len = msg_lens[1];
  470. start = 1;
  471. }
  472. }
  473. for (i = start; i < msg_count; i++) {
  474. app_deliver_fn(nodeid, &iov_delv, 1,
  475. endian_conversion_required);
  476. assembly->index += msg_lens[i];
  477. iov_delv.iov_base = &assembly->data[assembly->index];
  478. if (i < (msg_count - 1)) {
  479. iov_delv.iov_len = msg_lens[i + 1];
  480. }
  481. }
  482. if (mcast->fragmented == 0) {
  483. /*
  484. * End of messages, dereference assembly struct
  485. */
  486. assembly->last_frag_num = 0;
  487. assembly->index = 0;
  488. assembly_deref (assembly);
  489. } else {
  490. /*
  491. * Message is fragmented, keep around assembly list
  492. */
  493. assembly->last_frag_num = mcast->fragmented;
  494. if (mcast->msg_count > 1) {
  495. memmove (&assembly->data[0],
  496. &assembly->data[assembly->index],
  497. msg_lens[msg_count]);
  498. assembly->index = 0;
  499. }
  500. assembly->index += msg_lens[msg_count];
  501. }
  502. }
  503. /*
  504. * Totem Process Group Abstraction
  505. * depends on poll abstraction, POSIX, IPV4
  506. */
  507. void *callback_token_received_handle;
  508. int callback_token_received_fn (enum totem_callback_token_type type,
  509. void *data)
  510. {
  511. struct totempg_mcast mcast;
  512. struct iovec iovecs[3];
  513. int res;
  514. pthread_mutex_lock (&mcast_msg_mutex);
  515. if (mcast_packed_msg_count == 0) {
  516. pthread_mutex_unlock (&mcast_msg_mutex);
  517. return (0);
  518. }
  519. if (totemmrp_avail() == 0) {
  520. pthread_mutex_unlock (&mcast_msg_mutex);
  521. return (0);
  522. }
  523. mcast.fragmented = 0;
  524. /*
  525. * Was the first message in this buffer a continuation of a
  526. * fragmented message?
  527. */
  528. mcast.continuation = fragment_continuation;
  529. fragment_continuation = 0;
  530. mcast.msg_count = mcast_packed_msg_count;
  531. iovecs[0].iov_base = &mcast;
  532. iovecs[0].iov_len = sizeof (struct totempg_mcast);
  533. iovecs[1].iov_base = mcast_packed_msg_lens;
  534. iovecs[1].iov_len = mcast_packed_msg_count * sizeof (unsigned short);
  535. iovecs[2].iov_base = &fragmentation_data[0];
  536. iovecs[2].iov_len = fragment_size;
  537. res = totemmrp_mcast (iovecs, 3, 0);
  538. mcast_packed_msg_count = 0;
  539. fragment_size = 0;
  540. pthread_mutex_unlock (&mcast_msg_mutex);
  541. return (0);
  542. }
  543. /*
  544. * Initialize the totem process group abstraction
  545. */
  546. int totempg_initialize (
  547. poll_handle poll_handle,
  548. struct totem_config *totem_config)
  549. {
  550. int res;
  551. totempg_totem_config = totem_config;
  552. totempg_log_level_security = totem_config->totem_logging_configuration.log_level_security;
  553. totempg_log_level_error = totem_config->totem_logging_configuration.log_level_error;
  554. totempg_log_level_warning = totem_config->totem_logging_configuration.log_level_warning;
  555. totempg_log_level_notice = totem_config->totem_logging_configuration.log_level_notice;
  556. totempg_log_level_debug = totem_config->totem_logging_configuration.log_level_debug;
  557. totempg_log_printf = totem_config->totem_logging_configuration.log_printf;
  558. fragmentation_data = malloc (TOTEMPG_PACKET_SIZE);
  559. if (fragmentation_data == 0) {
  560. return (-1);
  561. }
  562. res = totemmrp_initialize (
  563. poll_handle,
  564. totem_config,
  565. totempg_deliver_fn,
  566. totempg_confchg_fn);
  567. totemmrp_callback_token_create (
  568. &callback_token_received_handle,
  569. TOTEM_CALLBACK_TOKEN_RECEIVED,
  570. 0,
  571. callback_token_received_fn,
  572. 0);
  573. totemsrp_net_mtu_adjust (totem_config);
  574. return (res);
  575. }
  576. void totempg_finalize (void)
  577. {
  578. pthread_mutex_lock (&totempg_mutex);
  579. totemmrp_finalize ();
  580. pthread_mutex_unlock (&totempg_mutex);
  581. }
  582. /*
  583. * Multicast a message
  584. */
  585. static int mcast_msg (
  586. struct iovec *iovec,
  587. int iov_len,
  588. int guarantee)
  589. {
  590. int res = 0;
  591. struct totempg_mcast mcast;
  592. struct iovec iovecs[3];
  593. int i;
  594. int max_packet_size = 0;
  595. int copy_len = 0;
  596. int copy_base = 0;
  597. int total_size = 0;
  598. pthread_mutex_lock (&mcast_msg_mutex);
  599. totemmrp_new_msg_signal ();
  600. max_packet_size = TOTEMPG_PACKET_SIZE -
  601. (sizeof (unsigned short) * (mcast_packed_msg_count + 1));
  602. mcast_packed_msg_lens[mcast_packed_msg_count] = 0;
  603. /*
  604. * Check if we would overwrite new message queue
  605. */
  606. for (i = 0; i < iov_len; i++) {
  607. total_size += iovec[i].iov_len;
  608. }
  609. if (send_ok (total_size + sizeof(unsigned short) *
  610. (mcast_packed_msg_count+1)) == 0) {
  611. pthread_mutex_unlock (&mcast_msg_mutex);
  612. return(-1);
  613. }
  614. for (i = 0; i < iov_len; ) {
  615. mcast.fragmented = 0;
  616. mcast.continuation = fragment_continuation;
  617. copy_len = iovec[i].iov_len - copy_base;
  618. /*
  619. * If it all fits with room left over, copy it in.
  620. * We need to leave at least sizeof(short) + 1 bytes in the
  621. * fragment_buffer on exit so that max_packet_size + fragment_size
  622. * doesn't exceed the size of the fragment_buffer on the next call.
  623. */
  624. if ((copy_len + fragment_size) <
  625. (max_packet_size - sizeof (unsigned short))) {
  626. memcpy (&fragmentation_data[fragment_size],
  627. iovec[i].iov_base + copy_base, copy_len);
  628. fragment_size += copy_len;
  629. mcast_packed_msg_lens[mcast_packed_msg_count] += copy_len;
  630. copy_len = 0;
  631. copy_base = 0;
  632. i++;
  633. continue;
  634. /*
  635. * If it just fits or is too big, then send out what fits.
  636. */
  637. } else {
  638. unsigned char *data_ptr;
  639. copy_len = min(copy_len, max_packet_size - fragment_size);
  640. if( copy_len == max_packet_size )
  641. data_ptr = iovec[i].iov_base + copy_base;
  642. else {
  643. data_ptr = fragmentation_data;
  644. memcpy (&fragmentation_data[fragment_size],
  645. iovec[i].iov_base + copy_base, copy_len);
  646. }
  647. memcpy (&fragmentation_data[fragment_size],
  648. iovec[i].iov_base + copy_base, copy_len);
  649. mcast_packed_msg_lens[mcast_packed_msg_count] += copy_len;
  650. /*
  651. * if we're not on the last iovec or the iovec is too large to
  652. * fit, then indicate a fragment. This also means that the next
  653. * message will have the continuation of this one.
  654. */
  655. if ((i < (iov_len - 1)) ||
  656. ((copy_base + copy_len) < iovec[i].iov_len)) {
  657. if (!next_fragment) {
  658. next_fragment++;
  659. }
  660. fragment_continuation = next_fragment;
  661. mcast.fragmented = next_fragment++;
  662. assert(fragment_continuation != 0);
  663. assert(mcast.fragmented != 0);
  664. } else {
  665. fragment_continuation = 0;
  666. }
  667. /*
  668. * assemble the message and send it
  669. */
  670. mcast.msg_count = ++mcast_packed_msg_count;
  671. iovecs[0].iov_base = &mcast;
  672. iovecs[0].iov_len = sizeof(struct totempg_mcast);
  673. iovecs[1].iov_base = mcast_packed_msg_lens;
  674. iovecs[1].iov_len = mcast_packed_msg_count *
  675. sizeof(unsigned short);
  676. iovecs[2].iov_base = data_ptr;
  677. iovecs[2].iov_len = max_packet_size;
  678. assert (totemmrp_avail() > 0);
  679. res = totemmrp_mcast (iovecs, 3, guarantee);
  680. /*
  681. * Recalculate counts and indexes for the next.
  682. */
  683. mcast_packed_msg_lens[0] = 0;
  684. mcast_packed_msg_count = 0;
  685. fragment_size = 0;
  686. max_packet_size = TOTEMPG_PACKET_SIZE - (sizeof(unsigned short));
  687. /*
  688. * If the iovec all fit, go to the next iovec
  689. */
  690. if ((copy_base + copy_len) == iovec[i].iov_len) {
  691. copy_len = 0;
  692. copy_base = 0;
  693. i++;
  694. /*
  695. * Continue with the rest of the current iovec.
  696. */
  697. } else {
  698. copy_base += copy_len;
  699. }
  700. }
  701. }
  702. /*
  703. * Bump only if we added message data. This may be zero if
  704. * the last buffer just fit into the fragmentation_data buffer
  705. * and we were at the last iovec.
  706. */
  707. if (mcast_packed_msg_lens[mcast_packed_msg_count]) {
  708. mcast_packed_msg_count++;
  709. }
  710. pthread_mutex_unlock (&mcast_msg_mutex);
  711. return (res);
  712. }
  713. /*
  714. * Determine if a message of msg_size could be queued
  715. */
  716. static int send_ok (
  717. int msg_size)
  718. {
  719. int avail = 0;
  720. int total;
  721. avail = totemmrp_avail ();
  722. /*
  723. * msg size less then totempg_totem_config->net_mtu - 25 will take up
  724. * a full message, so add +1
  725. * totempg_totem_config->net_mtu - 25 is for the totempg_mcast header
  726. */
  727. total = (msg_size / (totempg_totem_config->net_mtu - 25)) + 1;
  728. return (avail >= total);
  729. }
  730. int totempg_callback_token_create (
  731. void **handle_out,
  732. enum totem_callback_token_type type,
  733. int delete,
  734. int (*callback_fn) (enum totem_callback_token_type type, void *),
  735. void *data)
  736. {
  737. unsigned int res;
  738. pthread_mutex_lock (&callback_token_mutex);
  739. res = totemmrp_callback_token_create (handle_out, type, delete,
  740. callback_fn, data);
  741. pthread_mutex_unlock (&callback_token_mutex);
  742. return (res);
  743. }
  744. void totempg_callback_token_destroy (
  745. void *handle_out)
  746. {
  747. pthread_mutex_lock (&callback_token_mutex);
  748. totemmrp_callback_token_destroy (handle_out);
  749. pthread_mutex_unlock (&callback_token_mutex);
  750. }
  751. /*
  752. * vi: set autoindent tabstop=4 shiftwidth=4 :
  753. */
  754. int totempg_groups_initialize (
  755. totempg_groups_handle *handle,
  756. void (*deliver_fn) (
  757. unsigned int nodeid,
  758. struct iovec *iovec,
  759. int iov_len,
  760. int endian_conversion_required),
  761. void (*confchg_fn) (
  762. enum totem_configuration_type configuration_type,
  763. unsigned int *member_list, int member_list_entries,
  764. unsigned int *left_list, int left_list_entries,
  765. unsigned int *joined_list, int joined_list_entries,
  766. struct memb_ring_id *ring_id))
  767. {
  768. struct totempg_group_instance *instance;
  769. unsigned int res;
  770. pthread_mutex_lock (&totempg_mutex);
  771. res = hdb_handle_create (&totempg_groups_instance_database,
  772. sizeof (struct totempg_group_instance), handle);
  773. if (res != 0) {
  774. goto error_exit;
  775. }
  776. if (*handle > totempg_max_handle) {
  777. totempg_max_handle = *handle;
  778. }
  779. res = hdb_handle_get (&totempg_groups_instance_database, *handle,
  780. (void *)&instance);
  781. if (res != 0) {
  782. goto error_destroy;
  783. }
  784. instance->deliver_fn = deliver_fn;
  785. instance->confchg_fn = confchg_fn;
  786. instance->groups = 0;
  787. instance->groups_cnt = 0;
  788. hdb_handle_put (&totempg_groups_instance_database, *handle);
  789. pthread_mutex_unlock (&totempg_mutex);
  790. return (0);
  791. error_destroy:
  792. hdb_handle_destroy (&totempg_groups_instance_database, *handle);
  793. error_exit:
  794. pthread_mutex_unlock (&totempg_mutex);
  795. return (-1);
  796. }
  797. int totempg_groups_join (
  798. totempg_groups_handle handle,
  799. struct totempg_group *groups,
  800. int group_cnt)
  801. {
  802. struct totempg_group_instance *instance;
  803. struct totempg_group *new_groups;
  804. unsigned int res;
  805. pthread_mutex_lock (&totempg_mutex);
  806. res = hdb_handle_get (&totempg_groups_instance_database, handle,
  807. (void *)&instance);
  808. if (res != 0) {
  809. goto error_exit;
  810. }
  811. new_groups = realloc (instance->groups,
  812. sizeof (struct totempg_group) *
  813. (instance->groups_cnt + group_cnt));
  814. if (new_groups == 0) {
  815. res = ENOMEM;
  816. goto error_exit;
  817. }
  818. memcpy (&new_groups[instance->groups_cnt],
  819. groups, group_cnt * sizeof (struct totempg_group));
  820. instance->groups = new_groups;
  821. instance->groups_cnt = instance->groups_cnt = group_cnt;
  822. hdb_handle_put (&totempg_groups_instance_database, handle);
  823. error_exit:
  824. pthread_mutex_unlock (&totempg_mutex);
  825. return (res);
  826. }
  827. int totempg_groups_leave (
  828. totempg_groups_handle handle,
  829. struct totempg_group *groups,
  830. int group_cnt)
  831. {
  832. struct totempg_group_instance *instance;
  833. unsigned int res;
  834. pthread_mutex_lock (&totempg_mutex);
  835. res = hdb_handle_get (&totempg_groups_instance_database, handle,
  836. (void *)&instance);
  837. if (res != 0) {
  838. goto error_exit;
  839. }
  840. hdb_handle_put (&totempg_groups_instance_database, handle);
  841. error_exit:
  842. pthread_mutex_unlock (&totempg_mutex);
  843. return (res);
  844. }
  845. #define MAX_IOVECS_FROM_APP 32
  846. #define MAX_GROUPS_PER_MSG 32
  847. int totempg_groups_mcast_joined (
  848. totempg_groups_handle handle,
  849. struct iovec *iovec,
  850. int iov_len,
  851. int guarantee)
  852. {
  853. struct totempg_group_instance *instance;
  854. unsigned short group_len[MAX_GROUPS_PER_MSG + 1];
  855. struct iovec iovec_mcast[MAX_GROUPS_PER_MSG + 1 + MAX_IOVECS_FROM_APP];
  856. int i;
  857. unsigned int res;
  858. pthread_mutex_lock (&totempg_mutex);
  859. res = hdb_handle_get (&totempg_groups_instance_database, handle,
  860. (void *)&instance);
  861. if (res != 0) {
  862. goto error_exit;
  863. }
  864. /*
  865. * Build group_len structure and the iovec_mcast structure
  866. */
  867. group_len[0] = instance->groups_cnt;
  868. for (i = 0; i < instance->groups_cnt; i++) {
  869. group_len[i + 1] = instance->groups[i].group_len;
  870. iovec_mcast[i + 1].iov_len = instance->groups[i].group_len;
  871. iovec_mcast[i + 1].iov_base = instance->groups[i].group;
  872. }
  873. iovec_mcast[0].iov_len = (instance->groups_cnt + 1) * sizeof (unsigned short);
  874. iovec_mcast[0].iov_base = group_len;
  875. for (i = 0; i < iov_len; i++) {
  876. iovec_mcast[i + instance->groups_cnt + 1].iov_len = iovec[i].iov_len;
  877. iovec_mcast[i + instance->groups_cnt + 1].iov_base = iovec[i].iov_base;
  878. }
  879. res = mcast_msg (iovec_mcast, iov_len + instance->groups_cnt + 1, guarantee);
  880. hdb_handle_put (&totempg_groups_instance_database, handle);
  881. error_exit:
  882. pthread_mutex_unlock (&totempg_mutex);
  883. return (res);
  884. }
  885. int totempg_groups_send_ok_joined (
  886. totempg_groups_handle handle,
  887. struct iovec *iovec,
  888. int iov_len)
  889. {
  890. struct totempg_group_instance *instance;
  891. unsigned int size = 0;
  892. unsigned int i;
  893. unsigned int res;
  894. pthread_mutex_lock (&totempg_mutex);
  895. pthread_mutex_lock (&mcast_msg_mutex);
  896. res = hdb_handle_get (&totempg_groups_instance_database, handle,
  897. (void *)&instance);
  898. if (res != 0) {
  899. goto error_exit;
  900. }
  901. for (i = 0; i < instance->groups_cnt; i++) {
  902. size += instance->groups[i].group_len;
  903. }
  904. for (i = 0; i < iov_len; i++) {
  905. size += iovec[i].iov_len;
  906. }
  907. res = send_ok (size);
  908. hdb_handle_put (&totempg_groups_instance_database, handle);
  909. error_exit:
  910. pthread_mutex_unlock (&mcast_msg_mutex);
  911. pthread_mutex_unlock (&totempg_mutex);
  912. return (res);
  913. }
  914. int totempg_groups_mcast_groups (
  915. totempg_groups_handle handle,
  916. int guarantee,
  917. struct totempg_group *groups,
  918. int groups_cnt,
  919. struct iovec *iovec,
  920. int iov_len)
  921. {
  922. struct totempg_group_instance *instance;
  923. unsigned short group_len[MAX_GROUPS_PER_MSG + 1];
  924. struct iovec iovec_mcast[MAX_GROUPS_PER_MSG + 1 + MAX_IOVECS_FROM_APP];
  925. int i;
  926. unsigned int res;
  927. pthread_mutex_lock (&totempg_mutex);
  928. res = hdb_handle_get (&totempg_groups_instance_database, handle,
  929. (void *)&instance);
  930. if (res != 0) {
  931. goto error_exit;
  932. }
  933. /*
  934. * Build group_len structure and the iovec_mcast structure
  935. */
  936. group_len[0] = groups_cnt;
  937. for (i = 0; i < groups_cnt; i++) {
  938. group_len[i + 1] = groups[i].group_len;
  939. iovec_mcast[i + 1].iov_len = groups[i].group_len;
  940. iovec_mcast[i + 1].iov_base = groups[i].group;
  941. }
  942. iovec_mcast[0].iov_len = (groups_cnt + 1) * sizeof (unsigned short);
  943. iovec_mcast[0].iov_base = group_len;
  944. for (i = 0; i < iov_len; i++) {
  945. iovec_mcast[i + groups_cnt + 1].iov_len = iovec[i].iov_len;
  946. iovec_mcast[i + groups_cnt + 1].iov_base = iovec[i].iov_base;
  947. }
  948. res = mcast_msg (iovec_mcast, iov_len + groups_cnt + 1, guarantee);
  949. hdb_handle_put (&totempg_groups_instance_database, handle);
  950. error_exit:
  951. pthread_mutex_unlock (&totempg_mutex);
  952. return (res);
  953. }
  954. /*
  955. * Returns -1 if error, 0 if can't send, 1 if can send the message
  956. */
  957. int totempg_groups_send_ok_groups (
  958. totempg_groups_handle handle,
  959. struct totempg_group *groups,
  960. int groups_cnt,
  961. struct iovec *iovec,
  962. int iov_len)
  963. {
  964. struct totempg_group_instance *instance;
  965. unsigned int size = 0;
  966. unsigned int i;
  967. unsigned int res;
  968. pthread_mutex_lock (&totempg_mutex);
  969. res = hdb_handle_get (&totempg_groups_instance_database, handle,
  970. (void *)&instance);
  971. if (res != 0) {
  972. goto error_exit;
  973. }
  974. for (i = 0; i < groups_cnt; i++) {
  975. size += groups[i].group_len;
  976. }
  977. for (i = 0; i < iov_len; i++) {
  978. size += iovec[i].iov_len;
  979. }
  980. res = send_ok (size);
  981. hdb_handle_put (&totempg_groups_instance_database, handle);
  982. error_exit:
  983. pthread_mutex_unlock (&totempg_mutex);
  984. return (res);
  985. }
  986. int totempg_ifaces_get (
  987. unsigned int nodeid,
  988. struct totem_ip_address *interfaces,
  989. char ***status,
  990. unsigned int *iface_count)
  991. {
  992. int res;
  993. res = totemmrp_ifaces_get (
  994. nodeid,
  995. interfaces,
  996. status,
  997. iface_count);
  998. return (res);
  999. }
  1000. int totempg_ring_reenable (void)
  1001. {
  1002. int res;
  1003. res = totemmrp_ring_reenable ();
  1004. return (res);
  1005. }
  1006. char *totempg_ifaces_print (unsigned int nodeid)
  1007. {
  1008. static char iface_string[256 * INTERFACE_MAX];
  1009. char one_iface[64];
  1010. struct totem_ip_address interfaces[INTERFACE_MAX];
  1011. char **status;
  1012. unsigned int iface_count;
  1013. unsigned int i;
  1014. int res;
  1015. iface_string[0] = '\0';
  1016. res = totempg_ifaces_get (nodeid, interfaces, &status, &iface_count);
  1017. if (res == -1) {
  1018. return ("no interface found for nodeid");
  1019. }
  1020. for (i = 0; i < iface_count; i++) {
  1021. sprintf (one_iface, "r(%d) ip(%s) ",
  1022. i, totemip_print (&interfaces[i]));
  1023. strcat (iface_string, one_iface);
  1024. }
  1025. return (iface_string);
  1026. }