totempg.c 32 KB

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