totempg.c 32 KB

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