totempg.c 30 KB

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