totempg.c 32 KB

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