totempg.c 29 KB

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