totempg.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  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 loose 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 "totempg.h"
  81. #include "totemsrp.h"
  82. #include <sys/uio.h>
  83. #include <stdio.h>
  84. #include <stdlib.h>
  85. #include <string.h>
  86. #include <assert.h>
  87. #include <netinet/in.h>
  88. #include "swab.h"
  89. #define min(a,b) ((a) < (b)) ? a : b
  90. struct totempg_mcast_header {
  91. short version;
  92. short type;
  93. };
  94. /*
  95. * totempg_mcast structure
  96. *
  97. * header: Identify the mcast.
  98. * fragmented: Set if this message continues into next message
  99. * continuation: Set if this message is a continuation from last message
  100. * msg_count Indicates how many packed messages are contained
  101. * in the mcast.
  102. * Also, the size of each packed message and the messages themselves are
  103. * appended to the end of this structure when sent.
  104. */
  105. struct totempg_mcast {
  106. struct totempg_mcast_header header;
  107. unsigned char fragmented;
  108. unsigned char continuation;
  109. short msg_count;
  110. /*
  111. * short msg_len[msg_count];
  112. */
  113. /*
  114. * data for messages
  115. */
  116. };
  117. /*
  118. * Maximum packet size for totem pg messages
  119. */
  120. #define TOTEMPG_PACKET_SIZE (TOTEMSRP_PACKET_SIZE_MAX - \
  121. sizeof (struct totempg_mcast))
  122. /*
  123. * Local variables used for packing small messages
  124. */
  125. static unsigned short mcast_packed_msg_lens[TOTEMSRP_PACKET_SIZE_MAX];
  126. static int mcast_packed_msg_count = 0;
  127. static void (*app_deliver_fn) (
  128. struct in_addr source_addr,
  129. struct iovec *iovec,
  130. int iov_len,
  131. int endian_conversion_required) = 0;
  132. static void (*app_confchg_fn) (
  133. enum totem_configuration_type configuration_type,
  134. struct in_addr *member_list, int member_list_entries,
  135. struct in_addr *left_list, int left_list_entries,
  136. struct in_addr *joined_list, int joined_list_entries,
  137. struct memb_ring_id *ring_id) = 0;
  138. struct assembly {
  139. struct in_addr addr;
  140. unsigned char data[MESSAGE_SIZE_MAX];
  141. int index;
  142. };
  143. struct assembly *assembly_list[16]; // MAX PROCESSORS TODO
  144. int assembly_list_entries = 0;
  145. /*
  146. * Staging buffer for packed messages. Messages are staged in this buffer
  147. * before sending. Multiple messages may fit which cuts down on the
  148. * number of mcasts sent. If a message doesn't completely fit, then
  149. * the mcast header has a fragment bit set that says that there are more
  150. * data to follow. fragment_size is an index into the buffer. It indicates
  151. * the size of message data and where to place new message data.
  152. * fragment_contuation indicates whether the first packed message in
  153. * the buffer is a continuation of a previously packed fragment.
  154. */
  155. static unsigned char fragmentation_data[TOTEMPG_PACKET_SIZE];
  156. int fragment_size = 0;
  157. int fragment_continuation = 0;
  158. static struct iovec iov_delv;
  159. static struct assembly *find_assembly (struct in_addr addr)
  160. {
  161. int i;
  162. for (i = 0; i < assembly_list_entries; i++) {
  163. if (addr.s_addr == assembly_list[i]->addr.s_addr) {
  164. return (assembly_list[i]);
  165. }
  166. }
  167. return (0);
  168. }
  169. static void totempg_confchg_fn (
  170. enum totem_configuration_type configuration_type,
  171. struct in_addr *member_list, int member_list_entries,
  172. struct in_addr *left_list, int left_list_entries,
  173. struct in_addr *joined_list, int joined_list_entries,
  174. struct memb_ring_id *ring_id)
  175. {
  176. int i;
  177. int j;
  178. int found;
  179. /*
  180. * Clean out the assembly area for nodes that have left the
  181. * membership. If they return, we don't want any stale message
  182. * data that may be there.
  183. */
  184. for (i = 0; i < left_list_entries; i++) {
  185. for (j = 0; j < assembly_list_entries; j++) {
  186. if (left_list[i].s_addr == assembly_list[j]->addr.s_addr) {
  187. assembly_list[j]->index = 0;
  188. }
  189. }
  190. }
  191. /*
  192. * Create a message assembly area for any new members.
  193. */
  194. for (i = 0; i < member_list_entries; i++) {
  195. found = 0;
  196. for (j = 0; j < assembly_list_entries; j++) {
  197. if (member_list[i].s_addr == assembly_list[j]->addr.s_addr) {
  198. found = 1;
  199. break;
  200. }
  201. }
  202. if (found == 0) {
  203. assembly_list[assembly_list_entries] =
  204. malloc (sizeof (struct assembly));
  205. assert (assembly_list[assembly_list_entries]); // TODO
  206. assembly_list[assembly_list_entries]->addr.s_addr =
  207. member_list[i].s_addr;
  208. assembly_list[assembly_list_entries]->index = 0;
  209. assembly_list_entries += 1;
  210. }
  211. }
  212. app_confchg_fn (configuration_type,
  213. member_list, member_list_entries,
  214. left_list, left_list_entries,
  215. joined_list, joined_list_entries,
  216. ring_id);
  217. }
  218. static void totempg_deliver_fn (
  219. struct in_addr source_addr,
  220. struct iovec *iovec,
  221. int iov_len,
  222. int endian_conversion_required)
  223. {
  224. struct totempg_mcast *mcast;
  225. unsigned short *msg_lens;
  226. int i;
  227. struct assembly *assembly;
  228. char header[1500];
  229. int h_index;
  230. int a_i = 0;
  231. int msg_count;
  232. int continuation;
  233. assembly = find_assembly (source_addr);
  234. assert (assembly);
  235. /*
  236. * Assemble the header into one block of data and
  237. * assemble the packet contents into one block of data to simplify delivery
  238. */
  239. if (iov_len == 1) {
  240. /*
  241. * This message originated from external processor
  242. * because there is only one iovec for the full msg.
  243. */
  244. char *data;
  245. int datasize;
  246. mcast = (struct totempg_mcast *)iovec[0].iov_base;
  247. msg_count = mcast->msg_count;
  248. if (endian_conversion_required) {
  249. msg_count = swab16 (mcast->msg_count);
  250. }
  251. datasize = sizeof (struct totempg_mcast) +
  252. msg_count * sizeof (unsigned short);
  253. memcpy (header, iovec[0].iov_base, datasize);
  254. assert(iovec);
  255. data = iovec[0].iov_base;
  256. msg_lens = (unsigned short *) (header + sizeof (struct totempg_mcast));
  257. memcpy (&assembly->data[assembly->index], &data[datasize],
  258. iovec[0].iov_len - datasize);
  259. } else {
  260. /*
  261. * The message originated from local processor
  262. * becasue there is greater than one iovec for then full msg.
  263. */
  264. h_index = 0;
  265. for (i = 0; i < 2; i++) {
  266. memcpy (&header[h_index], iovec[i].iov_base, iovec[i].iov_len);
  267. h_index += iovec[i].iov_len;
  268. }
  269. mcast = (struct totempg_mcast *)header;
  270. // TODO make sure we are using a copy of mcast not the actual data itself
  271. msg_lens = (unsigned short *) (header + sizeof (struct totempg_mcast));
  272. for (i = 2; i < iov_len; i++) {
  273. a_i = assembly->index;
  274. memcpy (&assembly->data[a_i], iovec[i].iov_base, iovec[i].iov_len);
  275. a_i += msg_lens[i - 2];
  276. }
  277. iov_len -= 2;
  278. }
  279. if (endian_conversion_required) {
  280. mcast->msg_count = swab16 (mcast->msg_count);
  281. for (i = 0; i < mcast->msg_count; i++) {
  282. msg_lens[i] = swab16 (msg_lens[i]);
  283. }
  284. }
  285. /*
  286. printf ("Message fragmented %d count %d\n", mcast->fragmented, mcast->msg_count);
  287. for (i = 0; i < mcast->msg_count; i++) {
  288. printf ("len[%d] = %d\n", i, msg_lens[i]);
  289. }
  290. */
  291. /*
  292. * If the last message in the buffer is a fragment, then we
  293. * can't deliver it. We'll first deliver the full messages
  294. * then adjust the assembly buffer so we can add the rest of the
  295. * fragment when it arrives.
  296. */
  297. msg_count = mcast->fragmented ? mcast->msg_count - 1 : mcast->msg_count;
  298. continuation = mcast->continuation;
  299. iov_delv.iov_base = &assembly->data[0];
  300. iov_delv.iov_len = assembly->index + msg_lens[0];
  301. for (i = 0; i < msg_count; i++) {
  302. /*
  303. * If the first packed message is a continuation
  304. * of a previous message, but the assembly buffer
  305. * is empty, then we need to discard it since we can't
  306. * assemble a complete message.
  307. */
  308. if (continuation && (assembly->index == 0)) {
  309. continuation = 0;
  310. } else {
  311. app_deliver_fn(source_addr, &iov_delv, 1,
  312. endian_conversion_required);
  313. }
  314. assembly->index += msg_lens[i];
  315. iov_delv.iov_base = &assembly->data[assembly->index];
  316. if (i < (msg_count - 1)) {
  317. iov_delv.iov_len = msg_lens[i + 1];
  318. }
  319. }
  320. if (mcast->fragmented) {
  321. if (mcast->msg_count > 1) {
  322. memmove (&assembly->data[0],
  323. &assembly->data[assembly->index],
  324. msg_lens[msg_count]);
  325. assembly->index = 0;
  326. }
  327. assembly->index += msg_lens[msg_count];
  328. } else {
  329. assembly->index = 0;
  330. }
  331. }
  332. /*
  333. * Totem Process Group Abstraction
  334. * depends on poll abstraction, POSIX, IPV4
  335. */
  336. /*
  337. * Initialize the logger
  338. */
  339. void totempg_log_printf_init (
  340. void (*log_printf) (int , char *, ...),
  341. int log_level_security,
  342. int log_level_error,
  343. int log_level_warning,
  344. int log_level_notice,
  345. int log_level_debug)
  346. {
  347. totemsrp_log_printf_init (
  348. log_printf,
  349. log_level_security,
  350. log_level_error,
  351. log_level_warning,
  352. log_level_notice,
  353. log_level_debug);
  354. }
  355. void *callback_token_received_handle;
  356. int callback_token_received_fn (enum totem_callback_token_type type,
  357. void *data)
  358. {
  359. struct totempg_mcast mcast;
  360. struct iovec iovecs[3];
  361. int res;
  362. if (mcast_packed_msg_count == 0) {
  363. return (0);
  364. }
  365. mcast.fragmented = 0;
  366. /*
  367. * Was the first message in this buffer a continuation of a
  368. * fragmented message?
  369. */
  370. mcast.continuation = fragment_continuation;
  371. fragment_continuation = 0;
  372. mcast.msg_count = mcast_packed_msg_count;
  373. iovecs[0].iov_base = &mcast;
  374. iovecs[0].iov_len = sizeof (struct totempg_mcast);
  375. iovecs[1].iov_base = mcast_packed_msg_lens;
  376. iovecs[1].iov_len = mcast_packed_msg_count * sizeof (unsigned short);
  377. iovecs[2].iov_base = &fragmentation_data[0];
  378. iovecs[2].iov_len = fragment_size;
  379. res = totemsrp_mcast (iovecs, 3, 0);
  380. mcast_packed_msg_count = 0;
  381. fragment_size = 0;
  382. return (0);
  383. }
  384. /*
  385. * Initialize the totem process group abstraction
  386. */
  387. int totempg_initialize (
  388. struct openais_config *openais_config,
  389. poll_handle *poll_handle,
  390. unsigned char *private_key,
  391. int private_key_len,
  392. void *member_private,
  393. int member_private_len,
  394. void (*deliver_fn) (
  395. struct in_addr source_addr,
  396. struct iovec *iovec,
  397. int iov_len,
  398. int endian_conversion_required),
  399. void (*confchg_fn) (
  400. enum totem_configuration_type configuration_type,
  401. struct in_addr *member_list, int member_list_entries,
  402. struct in_addr *left_list, int left_list_entries,
  403. struct in_addr *joined_list, int joined_list_entries,
  404. struct memb_ring_id *ring_id))
  405. {
  406. int res;
  407. app_deliver_fn = deliver_fn;
  408. app_confchg_fn = confchg_fn;
  409. res = totemsrp_initialize (openais_config,
  410. poll_handle,
  411. private_key, private_key_len,
  412. member_private, member_private_len,
  413. totempg_deliver_fn, totempg_confchg_fn);
  414. totemsrp_callback_token_create (&callback_token_received_handle,
  415. TOTEM_CALLBACK_TOKEN_RECEIVED,
  416. 0,
  417. callback_token_received_fn,
  418. 0);
  419. return (res);
  420. }
  421. /*
  422. * Multicast a message
  423. */
  424. int totempg_mcast (
  425. struct iovec *iovec,
  426. int iov_len,
  427. int guarantee)
  428. {
  429. int res = 0;
  430. struct totempg_mcast mcast;
  431. struct iovec iovecs[3];
  432. int i;
  433. int max_packet_size = 0;
  434. int copy_len = 0;
  435. int copy_base = 0;
  436. max_packet_size = TOTEMPG_PACKET_SIZE -
  437. (sizeof (unsigned short) * (mcast_packed_msg_count + 1));
  438. mcast_packed_msg_lens[mcast_packed_msg_count] = 0;
  439. for (i = 0; i < iov_len; ) {
  440. mcast.fragmented = 0;
  441. mcast.continuation = fragment_continuation;
  442. copy_len = iovec[i].iov_len - copy_base;
  443. /*
  444. * If it all fits with room left over, copy it in.
  445. * We need to leave at least sizeof(short) + 1 bytes in the
  446. * fragment_buffer on exit so that max_packet_size + fragment_size
  447. * doesn't exceed the size of the fragment_buffer on the next call.
  448. */
  449. if ((copy_len + fragment_size) <
  450. (max_packet_size - sizeof (unsigned short))) {
  451. memcpy (&fragmentation_data[fragment_size],
  452. iovec[i].iov_base + copy_base, copy_len);
  453. fragment_size += copy_len;
  454. mcast_packed_msg_lens[mcast_packed_msg_count] += copy_len;
  455. copy_len = 0;
  456. copy_base = 0;
  457. i++;
  458. continue;
  459. /*
  460. * If it just fits or is too big, then send out what fits.
  461. */
  462. } else {
  463. copy_len = min(copy_len, max_packet_size - fragment_size);
  464. memcpy (&fragmentation_data[fragment_size],
  465. iovec[i].iov_base + copy_base, copy_len);
  466. mcast_packed_msg_lens[mcast_packed_msg_count] += copy_len;
  467. /*
  468. * if we're not on the last iovec or the iovec is too large to
  469. * fit, then indicate a fragment. This also means that the next
  470. * message will have the continuation of this one.
  471. */
  472. if ((i < (iov_len - 1)) ||
  473. ((copy_base + copy_len) < iovec[i].iov_len)) {
  474. mcast.fragmented = 1;
  475. fragment_continuation = 1;
  476. } else {
  477. fragment_continuation = 0;
  478. }
  479. /*
  480. * assemble the message and send it
  481. */
  482. mcast.msg_count = ++mcast_packed_msg_count;
  483. iovecs[0].iov_base = &mcast;
  484. iovecs[0].iov_len = sizeof(struct totempg_mcast);
  485. iovecs[1].iov_base = mcast_packed_msg_lens;
  486. iovecs[1].iov_len = mcast_packed_msg_count *
  487. sizeof(unsigned short);
  488. iovecs[2].iov_base = fragmentation_data;
  489. iovecs[2].iov_len = max_packet_size;
  490. res = totemsrp_mcast (iovecs, 3, guarantee);
  491. /*
  492. * Recalculate counts and indexes for the next.
  493. */
  494. mcast_packed_msg_lens[0] = 0;
  495. mcast_packed_msg_count = 0;
  496. fragment_size = 0;
  497. max_packet_size = TOTEMPG_PACKET_SIZE - (sizeof(unsigned short));
  498. /*
  499. * If the iovec all fit, go to the next iovec
  500. */
  501. if ((copy_base + copy_len) == iovec[i].iov_len) {
  502. copy_len = 0;
  503. copy_base = 0;
  504. i++;
  505. /*
  506. * Continue with the rest of the current iovec.
  507. */
  508. } else {
  509. copy_base += copy_len;
  510. }
  511. }
  512. }
  513. /*
  514. * Bump only if we added message data. This may be zero if
  515. * the last buffer just fit into the fragmentation_data buffer
  516. * and we were at the last iovec.
  517. */
  518. if (mcast_packed_msg_lens[mcast_packed_msg_count]) {
  519. mcast_packed_msg_count++;
  520. }
  521. return (res);
  522. }
  523. /*
  524. * Determine if a message of msg_size could be queued
  525. */
  526. int totempg_send_ok (
  527. int msg_size)
  528. {
  529. int avail = 0;
  530. avail = totemsrp_avail ();
  531. return (avail > 200);
  532. }
  533. /*
  534. * vi: set autoindent tabstop=4 shiftwidth=4 :
  535. */