totempg.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124
  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) (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. static struct assembly *find_assembly (struct totem_ip_address *addr)
  188. {
  189. int i;
  190. for (i = 0; i < assembly_list_entries; i++) {
  191. if (totemip_equal(addr, &assembly_list[i]->addr)) {
  192. return (assembly_list[i]);
  193. }
  194. }
  195. return (0);
  196. }
  197. static inline void app_confchg_fn (
  198. enum totem_configuration_type configuration_type,
  199. struct totem_ip_address *member_list, int member_list_entries,
  200. struct totem_ip_address *left_list, int left_list_entries,
  201. struct totem_ip_address *joined_list, int joined_list_entries,
  202. struct memb_ring_id *ring_id)
  203. {
  204. int i;
  205. struct totempg_group_instance *instance;
  206. unsigned int res;
  207. for (i = 0; i <= totempg_max_handle; i++) {
  208. res = hdb_handle_get (&totempg_groups_instance_database,
  209. i, (void *)&instance);
  210. if (res == 0) {
  211. if (instance->confchg_fn) {
  212. instance->confchg_fn (
  213. configuration_type,
  214. member_list,
  215. member_list_entries,
  216. left_list,
  217. left_list_entries,
  218. joined_list,
  219. joined_list_entries,
  220. ring_id);
  221. }
  222. hdb_handle_put (&totempg_groups_instance_database, i);
  223. }
  224. }
  225. }
  226. static inline void group_endian_convert (
  227. struct iovec *iovec)
  228. {
  229. unsigned short *group_len;
  230. int i;
  231. group_len = (unsigned short *)iovec->iov_base;
  232. group_len[0] = swab16(group_len[0]);
  233. for (i = 1; i < group_len[0] + 1; i++) {
  234. group_len[i] = swab16(group_len[i]);
  235. }
  236. }
  237. static inline int group_matches (
  238. struct iovec *iovec,
  239. unsigned int iov_len,
  240. struct totempg_group *groups_b,
  241. unsigned int group_b_cnt,
  242. unsigned int *adjust_iovec)
  243. {
  244. unsigned short *group_len;
  245. char *group_name;
  246. int i;
  247. int j;
  248. assert (iov_len == 1);
  249. group_len = (unsigned short *)iovec->iov_base;
  250. group_name = ((char *)iovec->iov_base) +
  251. sizeof (unsigned short) * (group_len[0] + 1);
  252. /*
  253. * Calculate amount to adjust the iovec by before delivering to app
  254. */
  255. *adjust_iovec = sizeof (unsigned short) * (group_len[0] + 1);
  256. for (i = 1; i < group_len[0] + 1; i++) {
  257. *adjust_iovec += group_len[i];
  258. }
  259. /*
  260. * Determine if this message should be delivered to this instance
  261. */
  262. for (i = 1; i < group_len[0] + 1; i++) {
  263. for (j = 0; j < group_b_cnt; j++) {
  264. if ((group_len[i] == groups_b[j].group_len) &&
  265. (memcmp (groups_b[j].group, group_name, group_len[i]) == 0)) {
  266. return (1);
  267. }
  268. }
  269. group_name += group_len[i];
  270. }
  271. return (0);
  272. }
  273. static inline void app_deliver_fn (
  274. struct totem_ip_address *source_addr,
  275. struct iovec *iovec,
  276. unsigned int iov_len,
  277. int endian_conversion_required)
  278. {
  279. int i;
  280. struct totempg_group_instance *instance;
  281. struct iovec stripped_iovec;
  282. unsigned int adjust_iovec;
  283. unsigned int res;
  284. if (endian_conversion_required) {
  285. group_endian_convert (iovec);
  286. }
  287. for (i = 0; i <= totempg_max_handle; i++) {
  288. res = hdb_handle_get (&totempg_groups_instance_database,
  289. i, (void *)&instance);
  290. if (res == 0) {
  291. assert (iov_len == 1);
  292. if (group_matches (iovec, iov_len, instance->groups, instance->groups_cnt, &adjust_iovec)) {
  293. stripped_iovec.iov_len = iovec->iov_len - adjust_iovec;
  294. stripped_iovec.iov_base = (char *)iovec->iov_base + adjust_iovec;
  295. instance->deliver_fn (
  296. source_addr,
  297. &stripped_iovec,
  298. iov_len,
  299. endian_conversion_required);
  300. }
  301. hdb_handle_put (&totempg_groups_instance_database, i);
  302. }
  303. }
  304. }
  305. static void totempg_confchg_fn (
  306. enum totem_configuration_type configuration_type,
  307. struct totem_ip_address *member_list, int member_list_entries,
  308. struct totem_ip_address *left_list, int left_list_entries,
  309. struct totem_ip_address *joined_list, int joined_list_entries,
  310. struct memb_ring_id *ring_id)
  311. {
  312. int i;
  313. int j;
  314. int found;
  315. /*
  316. * Clean out the assembly area for nodes that have left the
  317. * membership. If they return, we don't want any stale message
  318. * data that may be there.
  319. */
  320. for (i = 0; i < left_list_entries; i++) {
  321. for (j = 0; j < assembly_list_entries; j++) {
  322. if (totemip_equal(&left_list[i], &assembly_list[j]->addr)) {
  323. assembly_list[j]->index = 0;
  324. }
  325. }
  326. }
  327. /*
  328. * Create a message assembly area for any new members.
  329. */
  330. for (i = 0; i < member_list_entries; i++) {
  331. found = 0;
  332. for (j = 0; j < assembly_list_entries; j++) {
  333. if (totemip_equal(&member_list[i], &assembly_list[j]->addr)) {
  334. found = 1;
  335. break;
  336. }
  337. }
  338. if (found == 0) {
  339. assembly_list[assembly_list_entries] =
  340. malloc (sizeof (struct assembly));
  341. assert (assembly_list[assembly_list_entries]); // TODO
  342. totemip_copy(&assembly_list[assembly_list_entries]->addr,
  343. &member_list[i]);
  344. assembly_list[assembly_list_entries]->index = 0;
  345. assembly_list_entries += 1;
  346. }
  347. }
  348. app_confchg_fn (configuration_type,
  349. member_list, member_list_entries,
  350. left_list, left_list_entries,
  351. joined_list, joined_list_entries,
  352. ring_id);
  353. }
  354. static void totempg_deliver_fn (
  355. struct totem_ip_address *source_addr,
  356. struct iovec *iovec,
  357. int iov_len,
  358. int endian_conversion_required)
  359. {
  360. struct totempg_mcast *mcast;
  361. unsigned short *msg_lens;
  362. int i;
  363. struct assembly *assembly;
  364. char header[FRAME_SIZE_MAX];
  365. int h_index;
  366. int a_i = 0;
  367. int msg_count;
  368. int continuation;
  369. int start;
  370. assembly = find_assembly (source_addr);
  371. assert (assembly);
  372. /*
  373. * Assemble the header into one block of data and
  374. * assemble the packet contents into one block of data to simplify delivery
  375. */
  376. if (iov_len == 1) {
  377. /*
  378. * This message originated from external processor
  379. * because there is only one iovec for the full msg.
  380. */
  381. char *data;
  382. int datasize;
  383. mcast = (struct totempg_mcast *)iovec[0].iov_base;
  384. if (endian_conversion_required) {
  385. mcast->msg_count = swab16 (mcast->msg_count);
  386. }
  387. msg_count = mcast->msg_count;
  388. datasize = sizeof (struct totempg_mcast) +
  389. msg_count * sizeof (unsigned short);
  390. memcpy (header, iovec[0].iov_base, datasize);
  391. assert(iovec);
  392. data = iovec[0].iov_base;
  393. msg_lens = (unsigned short *) (header + sizeof (struct totempg_mcast));
  394. if (endian_conversion_required) {
  395. for (i = 0; i < mcast->msg_count; i++) {
  396. msg_lens[i] = swab16 (msg_lens[i]);
  397. }
  398. }
  399. memcpy (&assembly->data[assembly->index], &data[datasize],
  400. iovec[0].iov_len - datasize);
  401. } else {
  402. /*
  403. * The message originated from local processor
  404. * becasue there is greater than one iovec for then full msg.
  405. */
  406. h_index = 0;
  407. for (i = 0; i < 2; i++) {
  408. memcpy (&header[h_index], iovec[i].iov_base, iovec[i].iov_len);
  409. h_index += iovec[i].iov_len;
  410. }
  411. mcast = (struct totempg_mcast *)header;
  412. // TODO make sure we are using a copy of mcast not the actual data itself
  413. msg_lens = (unsigned short *) (header + sizeof (struct totempg_mcast));
  414. for (i = 2; i < iov_len; i++) {
  415. a_i = assembly->index;
  416. assert (iovec[i].iov_len + a_i <= MESSAGE_SIZE_MAX);
  417. memcpy (&assembly->data[a_i], iovec[i].iov_base, iovec[i].iov_len);
  418. a_i += msg_lens[i - 2];
  419. }
  420. iov_len -= 2;
  421. }
  422. /*
  423. * If the last message in the buffer is a fragment, then we
  424. * can't deliver it. We'll first deliver the full messages
  425. * then adjust the assembly buffer so we can add the rest of the
  426. * fragment when it arrives.
  427. */
  428. msg_count = mcast->fragmented ? mcast->msg_count - 1 : mcast->msg_count;
  429. continuation = mcast->continuation;
  430. iov_delv.iov_base = &assembly->data[0];
  431. iov_delv.iov_len = assembly->index + msg_lens[0];
  432. /*
  433. * Make sure that if this message is a continuation, that it
  434. * matches the sequence number of the previous fragment.
  435. * Also, if the first packed message is a continuation
  436. * of a previous message, but the assembly buffer
  437. * is empty, then we need to discard it since we can't
  438. * assemble a complete message. Likewise, if this message isn't a
  439. * continuation and the assembly buffer is empty, we have to discard
  440. * the continued message.
  441. */
  442. start = 0;
  443. if (continuation) {
  444. if (continuation != assembly->last_frag_num) {
  445. totempg_log_printf (totempg_log_level_error,
  446. "Message continuation doesn't match previous frag e: %u - a: %u\n",
  447. assembly->last_frag_num, continuation);
  448. continuation = 0;
  449. }
  450. if ((assembly->index == 0) ||
  451. (!continuation && assembly->index)) {
  452. totempg_log_printf (totempg_log_level_error,
  453. "Throwing away broken message: continuation %u, index %u\n",
  454. continuation, assembly->index);
  455. continuation = 0;
  456. }
  457. /*
  458. * we decided to throw away the first continued message
  459. * in this buffer, if continuation was set to zero.
  460. */
  461. if (!continuation) {
  462. assembly->index += msg_lens[0];
  463. iov_delv.iov_base = &assembly->data[assembly->index];
  464. iov_delv.iov_len = msg_lens[1];
  465. start = 1;
  466. }
  467. }
  468. for (i = start; i < msg_count; i++) {
  469. app_deliver_fn(source_addr, &iov_delv, 1,
  470. endian_conversion_required);
  471. assembly->index += msg_lens[i];
  472. iov_delv.iov_base = &assembly->data[assembly->index];
  473. if (i < (msg_count - 1)) {
  474. iov_delv.iov_len = msg_lens[i + 1];
  475. }
  476. }
  477. if (mcast->fragmented) {
  478. assembly->last_frag_num = mcast->fragmented;
  479. if (mcast->msg_count > 1) {
  480. memmove (&assembly->data[0],
  481. &assembly->data[assembly->index],
  482. msg_lens[msg_count]);
  483. assembly->index = 0;
  484. }
  485. assembly->index += msg_lens[msg_count];
  486. } else {
  487. assembly->last_frag_num = 0;
  488. assembly->index = 0;
  489. }
  490. }
  491. /*
  492. * Totem Process Group Abstraction
  493. * depends on poll abstraction, POSIX, IPV4
  494. */
  495. void *callback_token_received_handle;
  496. int callback_token_received_fn (enum totem_callback_token_type type,
  497. void *data)
  498. {
  499. struct totempg_mcast mcast;
  500. struct iovec iovecs[3];
  501. int res;
  502. if (mcast_packed_msg_count == 0) {
  503. return (0);
  504. }
  505. if (totemmrp_avail() == 0) {
  506. return (0);
  507. }
  508. mcast.fragmented = 0;
  509. /*
  510. * Was the first message in this buffer a continuation of a
  511. * fragmented message?
  512. */
  513. mcast.continuation = fragment_continuation;
  514. fragment_continuation = 0;
  515. mcast.msg_count = mcast_packed_msg_count;
  516. iovecs[0].iov_base = &mcast;
  517. iovecs[0].iov_len = sizeof (struct totempg_mcast);
  518. iovecs[1].iov_base = mcast_packed_msg_lens;
  519. iovecs[1].iov_len = mcast_packed_msg_count * sizeof (unsigned short);
  520. iovecs[2].iov_base = &fragmentation_data[0];
  521. iovecs[2].iov_len = fragment_size;
  522. res = totemmrp_mcast (iovecs, 3, 0);
  523. mcast_packed_msg_count = 0;
  524. fragment_size = 0;
  525. return (0);
  526. }
  527. /*
  528. * Initialize the totem process group abstraction
  529. */
  530. int totempg_initialize (
  531. poll_handle poll_handle,
  532. struct totem_config *totem_config)
  533. {
  534. int res;
  535. totempg_totem_config = totem_config;
  536. totempg_log_level_security = totem_config->totem_logging_configuration.log_level_security;
  537. totempg_log_level_error = totem_config->totem_logging_configuration.log_level_error;
  538. totempg_log_level_warning = totem_config->totem_logging_configuration.log_level_warning;
  539. totempg_log_level_notice = totem_config->totem_logging_configuration.log_level_notice;
  540. totempg_log_level_debug = totem_config->totem_logging_configuration.log_level_debug;
  541. totempg_log_printf = totem_config->totem_logging_configuration.log_printf;
  542. fragmentation_data = malloc (TOTEMPG_PACKET_SIZE);
  543. if (fragmentation_data == 0) {
  544. return (-1);
  545. }
  546. res = totemmrp_initialize (
  547. poll_handle,
  548. totem_config,
  549. totempg_deliver_fn,
  550. totempg_confchg_fn);
  551. totemmrp_callback_token_create (
  552. &callback_token_received_handle,
  553. TOTEM_CALLBACK_TOKEN_RECEIVED,
  554. 0,
  555. callback_token_received_fn,
  556. 0);
  557. totemsrp_net_mtu_adjust (totem_config);
  558. return (res);
  559. }
  560. void totempg_finalize (void)
  561. {
  562. pthread_mutex_lock (&totempg_mutex);
  563. totemmrp_finalize ();
  564. pthread_mutex_unlock (&totempg_mutex);
  565. }
  566. /*
  567. * Multicast a message
  568. */
  569. static int mcast_msg (
  570. struct iovec *iovec,
  571. int iov_len,
  572. int guarantee)
  573. {
  574. int res = 0;
  575. struct totempg_mcast mcast;
  576. struct iovec iovecs[3];
  577. int i;
  578. int max_packet_size = 0;
  579. int copy_len = 0;
  580. int copy_base = 0;
  581. int total_size = 0;
  582. totemmrp_new_msg_signal ();
  583. max_packet_size = TOTEMPG_PACKET_SIZE -
  584. (sizeof (unsigned short) * (mcast_packed_msg_count + 1));
  585. mcast_packed_msg_lens[mcast_packed_msg_count] = 0;
  586. /*
  587. * Check if we would overwrite new message queue
  588. */
  589. for (i = 0; i < iov_len; i++) {
  590. total_size += iovec[i].iov_len;
  591. }
  592. if (send_ok (total_size + sizeof(unsigned short) *
  593. (mcast_packed_msg_count+1)) == 0) {
  594. return(-1);
  595. }
  596. for (i = 0; i < iov_len; ) {
  597. mcast.fragmented = 0;
  598. mcast.continuation = fragment_continuation;
  599. copy_len = iovec[i].iov_len - copy_base;
  600. /*
  601. * If it all fits with room left over, copy it in.
  602. * We need to leave at least sizeof(short) + 1 bytes in the
  603. * fragment_buffer on exit so that max_packet_size + fragment_size
  604. * doesn't exceed the size of the fragment_buffer on the next call.
  605. */
  606. if ((copy_len + fragment_size) <
  607. (max_packet_size - sizeof (unsigned short))) {
  608. memcpy (&fragmentation_data[fragment_size],
  609. iovec[i].iov_base + copy_base, copy_len);
  610. fragment_size += copy_len;
  611. mcast_packed_msg_lens[mcast_packed_msg_count] += copy_len;
  612. copy_len = 0;
  613. copy_base = 0;
  614. i++;
  615. continue;
  616. /*
  617. * If it just fits or is too big, then send out what fits.
  618. */
  619. } else {
  620. unsigned char *data_ptr;
  621. copy_len = min(copy_len, max_packet_size - fragment_size);
  622. if( copy_len == max_packet_size )
  623. data_ptr = iovec[i].iov_base + copy_base;
  624. else {
  625. data_ptr = fragmentation_data;
  626. memcpy (&fragmentation_data[fragment_size],
  627. iovec[i].iov_base + copy_base, copy_len);
  628. }
  629. memcpy (&fragmentation_data[fragment_size],
  630. iovec[i].iov_base + copy_base, copy_len);
  631. mcast_packed_msg_lens[mcast_packed_msg_count] += copy_len;
  632. /*
  633. * if we're not on the last iovec or the iovec is too large to
  634. * fit, then indicate a fragment. This also means that the next
  635. * message will have the continuation of this one.
  636. */
  637. if ((i < (iov_len - 1)) ||
  638. ((copy_base + copy_len) < iovec[i].iov_len)) {
  639. if (!next_fragment) {
  640. next_fragment++;
  641. }
  642. fragment_continuation = next_fragment;
  643. mcast.fragmented = next_fragment++;
  644. assert(fragment_continuation != 0);
  645. assert(mcast.fragmented != 0);
  646. } else {
  647. fragment_continuation = 0;
  648. }
  649. /*
  650. * assemble the message and send it
  651. */
  652. mcast.msg_count = ++mcast_packed_msg_count;
  653. iovecs[0].iov_base = &mcast;
  654. iovecs[0].iov_len = sizeof(struct totempg_mcast);
  655. iovecs[1].iov_base = mcast_packed_msg_lens;
  656. iovecs[1].iov_len = mcast_packed_msg_count *
  657. sizeof(unsigned short);
  658. iovecs[2].iov_base = data_ptr;
  659. iovecs[2].iov_len = max_packet_size;
  660. assert (totemmrp_avail() > 0);
  661. res = totemmrp_mcast (iovecs, 3, guarantee);
  662. /*
  663. * Recalculate counts and indexes for the next.
  664. */
  665. mcast_packed_msg_lens[0] = 0;
  666. mcast_packed_msg_count = 0;
  667. fragment_size = 0;
  668. max_packet_size = TOTEMPG_PACKET_SIZE - (sizeof(unsigned short));
  669. /*
  670. * If the iovec all fit, go to the next iovec
  671. */
  672. if ((copy_base + copy_len) == iovec[i].iov_len) {
  673. copy_len = 0;
  674. copy_base = 0;
  675. i++;
  676. /*
  677. * Continue with the rest of the current iovec.
  678. */
  679. } else {
  680. copy_base += copy_len;
  681. }
  682. }
  683. }
  684. /*
  685. * Bump only if we added message data. This may be zero if
  686. * the last buffer just fit into the fragmentation_data buffer
  687. * and we were at the last iovec.
  688. */
  689. if (mcast_packed_msg_lens[mcast_packed_msg_count]) {
  690. mcast_packed_msg_count++;
  691. }
  692. return (res);
  693. }
  694. /*
  695. * Determine if a message of msg_size could be queued
  696. */
  697. static int send_ok (
  698. int msg_size)
  699. {
  700. int avail = 0;
  701. int total;
  702. avail = totemmrp_avail ();
  703. /*
  704. * msg size less then totempg_totem_config->net_mtu - 25 will take up
  705. * a full message, so add +1
  706. * totempg_totem_config->net_mtu - 25 is for the totempg_mcast header
  707. */
  708. total = (msg_size / (totempg_totem_config->net_mtu - 25)) + 1;
  709. return (avail >= total);
  710. }
  711. int totempg_callback_token_create (
  712. void **handle_out,
  713. enum totem_callback_token_type type,
  714. int delete,
  715. int (*callback_fn) (enum totem_callback_token_type type, void *),
  716. void *data)
  717. {
  718. unsigned int res;
  719. pthread_mutex_lock (&callback_token_mutex);
  720. res = totemmrp_callback_token_create (handle_out, type, delete,
  721. callback_fn, data);
  722. pthread_mutex_unlock (&callback_token_mutex);
  723. return (res);
  724. }
  725. void totempg_callback_token_destroy (
  726. void *handle_out)
  727. {
  728. pthread_mutex_lock (&callback_token_mutex);
  729. totemmrp_callback_token_destroy (handle_out);
  730. pthread_mutex_unlock (&callback_token_mutex);
  731. }
  732. /*
  733. * vi: set autoindent tabstop=4 shiftwidth=4 :
  734. */
  735. int totempg_groups_initialize (
  736. totempg_groups_handle *handle,
  737. void (*deliver_fn) (
  738. struct totem_ip_address *source_addr,
  739. struct iovec *iovec,
  740. int iov_len,
  741. int endian_conversion_required),
  742. void (*confchg_fn) (
  743. enum totem_configuration_type configuration_type,
  744. struct totem_ip_address *member_list, int member_list_entries,
  745. struct totem_ip_address *left_list, int left_list_entries,
  746. struct totem_ip_address *joined_list, int joined_list_entries,
  747. struct memb_ring_id *ring_id))
  748. {
  749. struct totempg_group_instance *instance;
  750. unsigned int res;
  751. pthread_mutex_lock (&totempg_mutex);
  752. res = hdb_handle_create (&totempg_groups_instance_database,
  753. sizeof (struct totempg_group_instance), handle);
  754. if (res != 0) {
  755. goto error_exit;
  756. }
  757. if (*handle > totempg_max_handle) {
  758. totempg_max_handle = *handle;
  759. }
  760. res = hdb_handle_get (&totempg_groups_instance_database, *handle,
  761. (void *)&instance);
  762. if (res != 0) {
  763. goto error_destroy;
  764. }
  765. instance->deliver_fn = deliver_fn;
  766. instance->confchg_fn = confchg_fn;
  767. instance->groups = 0;
  768. instance->groups_cnt = 0;
  769. hdb_handle_put (&totempg_groups_instance_database, *handle);
  770. pthread_mutex_unlock (&totempg_mutex);
  771. return (0);
  772. error_destroy:
  773. hdb_handle_destroy (&totempg_groups_instance_database, *handle);
  774. error_exit:
  775. pthread_mutex_unlock (&totempg_mutex);
  776. return (-1);
  777. }
  778. int totempg_groups_join (
  779. totempg_groups_handle handle,
  780. struct totempg_group *groups,
  781. int group_cnt)
  782. {
  783. struct totempg_group_instance *instance;
  784. struct totempg_group *new_groups;
  785. unsigned int res;
  786. pthread_mutex_lock (&totempg_mutex);
  787. res = hdb_handle_get (&totempg_groups_instance_database, handle,
  788. (void *)&instance);
  789. if (res != 0) {
  790. goto error_exit;
  791. }
  792. new_groups = realloc (instance->groups,
  793. sizeof (struct totempg_group) *
  794. (instance->groups_cnt + group_cnt));
  795. if (new_groups == 0) {
  796. res = ENOMEM;
  797. goto error_exit;
  798. }
  799. memcpy (&new_groups[instance->groups_cnt],
  800. groups, group_cnt * sizeof (struct totempg_group));
  801. instance->groups = new_groups;
  802. instance->groups_cnt = instance->groups_cnt = group_cnt;
  803. hdb_handle_put (&totempg_groups_instance_database, handle);
  804. error_exit:
  805. pthread_mutex_unlock (&totempg_mutex);
  806. return (res);
  807. }
  808. int totempg_groups_leave (
  809. totempg_groups_handle handle,
  810. struct totempg_group *groups,
  811. int group_cnt)
  812. {
  813. struct totempg_group_instance *instance;
  814. unsigned int res;
  815. pthread_mutex_lock (&totempg_mutex);
  816. res = hdb_handle_get (&totempg_groups_instance_database, handle,
  817. (void *)&instance);
  818. if (res != 0) {
  819. goto error_exit;
  820. }
  821. hdb_handle_put (&totempg_groups_instance_database, handle);
  822. error_exit:
  823. pthread_mutex_unlock (&totempg_mutex);
  824. return (res);
  825. }
  826. #define MAX_IOVECS_FROM_APP 32
  827. #define MAX_GROUPS_PER_MSG 32
  828. int totempg_groups_mcast_joined (
  829. totempg_groups_handle handle,
  830. struct iovec *iovec,
  831. int iov_len,
  832. int guarantee)
  833. {
  834. struct totempg_group_instance *instance;
  835. unsigned short group_len[MAX_GROUPS_PER_MSG + 1];
  836. struct iovec iovec_mcast[MAX_GROUPS_PER_MSG + 1 + MAX_IOVECS_FROM_APP];
  837. int i;
  838. unsigned int res;
  839. pthread_mutex_lock (&totempg_mutex);
  840. res = hdb_handle_get (&totempg_groups_instance_database, handle,
  841. (void *)&instance);
  842. if (res != 0) {
  843. goto error_exit;
  844. }
  845. /*
  846. * Build group_len structure and the iovec_mcast structure
  847. */
  848. group_len[0] = instance->groups_cnt;
  849. for (i = 0; i < instance->groups_cnt; i++) {
  850. group_len[i + 1] = instance->groups[i].group_len;
  851. iovec_mcast[i + 1].iov_len = instance->groups[i].group_len;
  852. iovec_mcast[i + 1].iov_base = instance->groups[i].group;
  853. }
  854. iovec_mcast[0].iov_len = (instance->groups_cnt + 1) * sizeof (unsigned short);
  855. iovec_mcast[0].iov_base = group_len;
  856. for (i = 0; i < iov_len; i++) {
  857. iovec_mcast[i + instance->groups_cnt + 1].iov_len = iovec[i].iov_len;
  858. iovec_mcast[i + instance->groups_cnt + 1].iov_base = iovec[i].iov_base;
  859. }
  860. res = mcast_msg (iovec_mcast, iov_len + instance->groups_cnt + 1, guarantee);
  861. hdb_handle_put (&totempg_groups_instance_database, handle);
  862. error_exit:
  863. pthread_mutex_unlock (&totempg_mutex);
  864. return (res);
  865. }
  866. int totempg_groups_send_ok_joined (
  867. totempg_groups_handle handle,
  868. struct iovec *iovec,
  869. int iov_len)
  870. {
  871. struct totempg_group_instance *instance;
  872. unsigned int size = 0;
  873. unsigned int i;
  874. unsigned int res;
  875. pthread_mutex_lock (&totempg_mutex);
  876. res = hdb_handle_get (&totempg_groups_instance_database, handle,
  877. (void *)&instance);
  878. if (res != 0) {
  879. goto error_exit;
  880. }
  881. for (i = 0; i < instance->groups_cnt; i++) {
  882. size += instance->groups[i].group_len;
  883. }
  884. for (i = 0; i < iov_len; i++) {
  885. size += iovec[i].iov_len;
  886. }
  887. res = send_ok (size);
  888. hdb_handle_put (&totempg_groups_instance_database, handle);
  889. error_exit:
  890. pthread_mutex_unlock (&totempg_mutex);
  891. return (res);
  892. }
  893. int totempg_groups_mcast_groups (
  894. totempg_groups_handle handle,
  895. int guarantee,
  896. struct totempg_group *groups,
  897. int groups_cnt,
  898. struct iovec *iovec,
  899. int iov_len)
  900. {
  901. struct totempg_group_instance *instance;
  902. unsigned short group_len[MAX_GROUPS_PER_MSG + 1];
  903. struct iovec iovec_mcast[MAX_GROUPS_PER_MSG + 1 + MAX_IOVECS_FROM_APP];
  904. int i;
  905. unsigned int res;
  906. pthread_mutex_lock (&totempg_mutex);
  907. res = hdb_handle_get (&totempg_groups_instance_database, handle,
  908. (void *)&instance);
  909. if (res != 0) {
  910. goto error_exit;
  911. }
  912. /*
  913. * Build group_len structure and the iovec_mcast structure
  914. */
  915. group_len[0] = groups_cnt;
  916. for (i = 0; i < groups_cnt; i++) {
  917. group_len[i + 1] = groups[i].group_len;
  918. iovec_mcast[i + 1].iov_len = groups[i].group_len;
  919. iovec_mcast[i + 1].iov_base = groups[i].group;
  920. }
  921. iovec_mcast[0].iov_len = (groups_cnt + 1) * sizeof (unsigned short);
  922. iovec_mcast[0].iov_base = group_len;
  923. for (i = 0; i < iov_len; i++) {
  924. iovec_mcast[i + groups_cnt + 1].iov_len = iovec[i].iov_len;
  925. iovec_mcast[i + groups_cnt + 1].iov_base = iovec[i].iov_base;
  926. }
  927. res = mcast_msg (iovec_mcast, iov_len + groups_cnt + 1, guarantee);
  928. hdb_handle_put (&totempg_groups_instance_database, handle);
  929. error_exit:
  930. pthread_mutex_unlock (&totempg_mutex);
  931. return (res);
  932. }
  933. /*
  934. * Returns -1 if error, 0 if can't send, 1 if can send the message
  935. */
  936. int totempg_groups_send_ok_groups (
  937. totempg_groups_handle handle,
  938. struct totempg_group *groups,
  939. int groups_cnt,
  940. struct iovec *iovec,
  941. int iov_len)
  942. {
  943. struct totempg_group_instance *instance;
  944. unsigned int size = 0;
  945. unsigned int i;
  946. unsigned int res;
  947. pthread_mutex_lock (&totempg_mutex);
  948. res = hdb_handle_get (&totempg_groups_instance_database, handle,
  949. (void *)&instance);
  950. if (res != 0) {
  951. goto error_exit;
  952. }
  953. for (i = 0; i < groups_cnt; i++) {
  954. size += groups[i].group_len;
  955. }
  956. for (i = 0; i < iov_len; i++) {
  957. size += iovec[i].iov_len;
  958. }
  959. res = send_ok (size);
  960. hdb_handle_put (&totempg_groups_instance_database, handle);
  961. error_exit:
  962. pthread_mutex_unlock (&totempg_mutex);
  963. return (res);
  964. }