4
0

totempg.c 31 KB

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