totempg.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. /*
  2. * Copyright (c) 2003-2005 MontaVista Software, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Steven Dake (sdake@mvista.com)
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. /*
  35. * FRAGMENTATION AND PACKING ALGORITHM:
  36. *
  37. * Assemble the entire message into one buffer
  38. * if full fragment
  39. * store fragment into lengths list
  40. * for each full fragment
  41. * multicast fragment
  42. * set length and fragment fields of pg mesage
  43. * store remaining multicast into head of fragmentation data and set lens field
  44. *
  45. * If a message exceeds the maximum packet size allowed by the totem
  46. * single ring protocol, the protocol could loose forward progress.
  47. * Statically calculating the allowed data amount doesn't work because
  48. * the amount of data allowed depends on the number of fragments in
  49. * each message. In this implementation, the maximum fragment size
  50. * is dynamically calculated for each fragment added to the message.
  51. * It is possible for a message to be two bytes short of the maximum
  52. * packet size. This occurs when a message or collection of
  53. * messages + the mcast header + the lens are two bytes short of the
  54. * end of the packet. Since another len field consumes two bytes, the
  55. * len field would consume the rest of the packet without room for data.
  56. *
  57. * One optimization would be to forgo the final len field and determine
  58. * it from the size of the udp datagram. Then this condition would no
  59. * longer occur.
  60. */
  61. /*
  62. * ASSEMBLY AND UNPACKING ALGORITHM:
  63. *
  64. * copy incoming packet into assembly data buffer indexed by current
  65. * location of end of fragment
  66. *
  67. * if not fragmented
  68. * deliver all messages in assembly data buffer
  69. * else
  70. * if msg_count > 1 and fragmented
  71. * deliver all messages except last message in assembly data buffer
  72. * copy last fragmented section to start of assembly data buffer
  73. * else
  74. * if msg_count = 1 and fragmented
  75. * do nothing
  76. *
  77. */
  78. #include "totempg.h"
  79. #include "totemsrp.h"
  80. #include <sys/uio.h>
  81. #include <stdio.h>
  82. #include <stdlib.h>
  83. #include <string.h>
  84. #include <assert.h>
  85. #include <netinet/in.h>
  86. #include "swab.h"
  87. struct totempg_mcast_header {
  88. short version;
  89. short type;
  90. };
  91. struct totempg_mcast {
  92. struct totempg_mcast_header header;
  93. short fragmented; /* This message continues into next message */
  94. short msg_count;
  95. /*
  96. * short msg_len[msg_count];
  97. */
  98. /*
  99. * data for messages
  100. */
  101. };
  102. /*
  103. * Maximum packet size for totem pg messages
  104. */
  105. #define TOTEMPG_PACKET_SIZE (TOTEMSRP_PACKET_SIZE_MAX - sizeof (struct totempg_mcast))
  106. /*
  107. * Local variables used for packing small messages
  108. */
  109. static unsigned short mcast_packed_msg_lens[TOTEMSRP_PACKET_SIZE_MAX];
  110. static int mcast_packed_msg_count = 0;
  111. static void (*app_deliver_fn) (
  112. struct in_addr source_addr,
  113. struct iovec *iovec,
  114. int iov_len,
  115. int endian_conversion_required) = 0;
  116. static void (*app_confchg_fn) (
  117. enum totempg_configuration_type configuration_type,
  118. struct in_addr *member_list, void *member_list_private,
  119. int member_list_entries,
  120. struct in_addr *left_list, void *left_list_private,
  121. int left_list_entries,
  122. struct in_addr *joined_list, void *joined_list_private,
  123. int joined_list_entries) = 0;
  124. struct assembly {
  125. struct in_addr addr;
  126. unsigned char data[MESSAGE_SIZE_MAX];
  127. int index;
  128. };
  129. struct assembly *assembly_list[16]; // MAX PROCESSORS TODO
  130. int assembly_list_entries = 0;
  131. static unsigned char fragmentation_data[MESSAGE_SIZE_MAX];
  132. int fragment_size = 0;
  133. static struct iovec iov_delv;
  134. static struct assembly *find_assembly (struct in_addr addr)
  135. {
  136. int i;
  137. for (i = 0; i < assembly_list_entries; i++) {
  138. if (addr.s_addr == assembly_list[i]->addr.s_addr) {
  139. return (assembly_list[i]);
  140. }
  141. }
  142. return (0);
  143. }
  144. static void totempg_confchg_fn (
  145. enum totempg_configuration_type configuration_type,
  146. struct in_addr *member_list, void *member_list_private,
  147. int member_list_entries,
  148. struct in_addr *left_list, void *left_list_private,
  149. int left_list_entries,
  150. struct in_addr *joined_list, void *joined_list_private,
  151. int joined_list_entries)
  152. {
  153. int i;
  154. int j;
  155. int found;
  156. for (i = 0; i < member_list_entries; i++) {
  157. found = 0;
  158. for (j = 0; j < assembly_list_entries; j++) {
  159. if (member_list[i].s_addr == assembly_list[j]->addr.s_addr) {
  160. found = 1;
  161. break;
  162. }
  163. }
  164. if (found == 0) {
  165. assembly_list[assembly_list_entries] =
  166. malloc (sizeof (struct assembly));
  167. assert (assembly_list[assembly_list_entries]); // TODO
  168. assembly_list[assembly_list_entries]->addr.s_addr = member_list[i].s_addr;
  169. assembly_list[i]->index = 0;
  170. assembly_list_entries += 1;
  171. }
  172. }
  173. app_confchg_fn (configuration_type,
  174. member_list, member_list_private, member_list_entries,
  175. left_list, left_list_private, left_list_entries,
  176. joined_list, joined_list_private, joined_list_entries);
  177. }
  178. static void totempg_deliver_fn (
  179. struct in_addr source_addr,
  180. struct iovec *iovec,
  181. int iov_len,
  182. int endian_conversion_required)
  183. {
  184. struct totempg_mcast *mcast;
  185. unsigned short *msg_lens;
  186. int i;
  187. struct assembly *assembly;
  188. char header[1500];
  189. int h_index;
  190. int a_i = 0;
  191. int msg_count;
  192. assembly = find_assembly (source_addr);
  193. assert (assembly);
  194. /*
  195. * Assemble the header into one block of data
  196. * Assemble the packet contents into one block of data to simplify delivery
  197. */
  198. if (iov_len == 1) {
  199. /* message originated from external processor - 1 iovec for full msg */
  200. char *data;
  201. int datasize;
  202. mcast = (struct totempg_mcast *)iovec[0].iov_base;
  203. msg_count = mcast->msg_count;
  204. if (endian_conversion_required) {
  205. msg_count = swab16 (mcast->msg_count);
  206. }
  207. datasize = sizeof (struct totempg_mcast) +
  208. msg_count * sizeof (unsigned short);
  209. memcpy (header, iovec[0].iov_base, datasize);
  210. data = iovec[0].iov_base;
  211. msg_lens = (unsigned short *) (header + sizeof (struct totempg_mcast));
  212. memcpy (&assembly->data[assembly->index], &data[datasize],
  213. iovec[0].iov_len - datasize);
  214. } else {
  215. /* message originated from local processor - <1 iovec for full msg */
  216. h_index = 0;
  217. for (i = 0; i < 2; i++) {
  218. memcpy (&header[h_index], iovec[i].iov_base, iovec[i].iov_len);
  219. h_index += iovec[i].iov_len;
  220. }
  221. mcast = (struct totempg_mcast *)header;
  222. // TODO make sure we are using a copy of mcast not the actual data itself
  223. msg_lens = (unsigned short *) (header + sizeof (struct totempg_mcast));
  224. for (i = 2; i < iov_len; i++) {
  225. a_i = assembly->index;
  226. memcpy (&assembly->data[a_i], iovec[i].iov_base, iovec[i].iov_len);
  227. a_i += msg_lens[i - 2];
  228. }
  229. iov_len -= 2;
  230. }
  231. if (endian_conversion_required) {
  232. mcast->fragmented = swab16 (mcast->fragmented);
  233. mcast->msg_count = swab16 (mcast->msg_count);
  234. for (i = 0; i < mcast->msg_count; i++) {
  235. msg_lens[i] = swab16 (msg_lens[i]);
  236. }
  237. }
  238. /*
  239. printf ("Message fragmented %d count %d\n", mcast->fragmented, mcast->msg_count);
  240. for (i = 0; i < mcast->msg_count; i++) {
  241. printf ("len[%d] = %d\n", i, msg_lens[i]);
  242. }
  243. */
  244. /*
  245. * Deliver all full messages in packed message
  246. */
  247. /*
  248. * this message's last packed message is not a fragment
  249. */
  250. if (mcast->fragmented == 0) {
  251. iov_delv.iov_base = &assembly->data[0];
  252. iov_delv.iov_len = assembly->index + msg_lens[0];
  253. for (i = 0; i < mcast->msg_count; i++) {
  254. assembly->index += msg_lens[i];
  255. //printf ("app deliver\n");
  256. app_deliver_fn (source_addr, &iov_delv, 1,
  257. endian_conversion_required);
  258. iov_delv.iov_base = &assembly->data[assembly->index];
  259. iov_delv.iov_len = msg_lens[i + 1];
  260. }
  261. assembly->index = 0;
  262. } else
  263. /*
  264. * This message's last packed message is a fragment
  265. */
  266. if (mcast->fragmented == 1) {
  267. iov_delv.iov_base = &assembly->data[0];
  268. iov_delv.iov_len = assembly->index + msg_lens[0];
  269. for (i = 0; i < mcast->msg_count - 1; i++) {
  270. assembly->index += msg_lens[i];
  271. //printf ("app deliver\n");
  272. app_deliver_fn (source_addr, &iov_delv, 1,
  273. endian_conversion_required);
  274. iov_delv.iov_base = &assembly->data[assembly->index];
  275. iov_delv.iov_len = msg_lens[i + 1];
  276. }
  277. if (mcast->msg_count > 1) {
  278. memmove (&assembly->data[0],
  279. &assembly->data[assembly->index],
  280. msg_lens[mcast->msg_count - 1]);
  281. assembly->index = 0;
  282. }
  283. assembly->index += msg_lens[mcast->msg_count - 1];
  284. }
  285. }
  286. /*
  287. * Totem Process Group Abstraction
  288. * depends on poll abstraction, POSIX, IPV4
  289. */
  290. /*
  291. * Initialize the logger
  292. */
  293. void totempg_log_printf_init (
  294. void (*log_printf) (int , char *, ...),
  295. int log_level_security,
  296. int log_level_error,
  297. int log_level_warning,
  298. int log_level_notice,
  299. int log_level_debug)
  300. {
  301. totemsrp_log_printf_init (
  302. log_printf,
  303. log_level_security,
  304. log_level_error,
  305. log_level_warning,
  306. log_level_notice,
  307. log_level_debug);
  308. }
  309. void *callback_token_received_handle;
  310. int callback_token_received_fn (enum totemsrp_callback_token_type type,
  311. void *data)
  312. {
  313. struct totempg_mcast mcast;
  314. struct iovec iovecs[3];
  315. int res;
  316. if (mcast_packed_msg_count == 0) {
  317. return (0);
  318. }
  319. mcast.fragmented = 0;
  320. mcast.msg_count = mcast_packed_msg_count;
  321. iovecs[0].iov_base = &mcast;
  322. iovecs[0].iov_len = sizeof (struct totempg_mcast);
  323. iovecs[1].iov_base = mcast_packed_msg_lens;
  324. iovecs[1].iov_len = mcast_packed_msg_count * sizeof (unsigned short);
  325. iovecs[2].iov_base = &fragmentation_data[0];
  326. iovecs[2].iov_len = fragment_size;
  327. res = totemsrp_mcast (iovecs, 3, 0);
  328. mcast_packed_msg_count = 0;
  329. fragment_size = 0;
  330. return (0);
  331. }
  332. /*
  333. * Initialize the totem process group abstraction
  334. */
  335. int totempg_initialize (
  336. struct sockaddr_in *sockaddr_mcast,
  337. struct totempg_interface *interfaces,
  338. int interface_count,
  339. poll_handle *poll_handle,
  340. unsigned char *private_key,
  341. int private_key_len,
  342. void *member_private,
  343. int member_private_len,
  344. void (*deliver_fn) (
  345. struct in_addr source_addr,
  346. struct iovec *iovec,
  347. int iov_len,
  348. int endian_conversion_required),
  349. void (*confchg_fn) (
  350. enum totempg_configuration_type configuration_type,
  351. struct in_addr *member_list, void *member_list_private,
  352. int member_list_entries,
  353. struct in_addr *left_list, void *left_list_private,
  354. int left_list_entries,
  355. struct in_addr *joined_list, void *joined_list_private,
  356. int joined_list_entries))
  357. {
  358. int res;
  359. app_deliver_fn = deliver_fn;
  360. app_confchg_fn = confchg_fn;
  361. res = totemsrp_initialize (sockaddr_mcast, (struct totemsrp_interface *)interfaces,
  362. interface_count,
  363. poll_handle,
  364. private_key, private_key_len,
  365. member_private, member_private_len,
  366. totempg_deliver_fn, totempg_confchg_fn);
  367. totemsrp_callback_token_create (&callback_token_received_handle,
  368. TOTEMSRP_CALLBACK_TOKEN_RECEIVED,
  369. 0,
  370. callback_token_received_fn,
  371. 0);
  372. return (res);
  373. }
  374. /*
  375. * Multicast a message
  376. */
  377. int totempg_mcast (
  378. struct iovec *iovec,
  379. int iov_len,
  380. int guarantee)
  381. {
  382. int res = 0;
  383. struct totempg_mcast mcast;
  384. struct iovec iovecs[3];
  385. int i;
  386. int j;
  387. int copy_len;
  388. int fragment_index = 0;
  389. int fragment_size_assem = 0;
  390. int max_packet_size = 0;
  391. int remaining_size = 0;
  392. int goober;
  393. int f_i;
  394. mcast.msg_count = 0;
  395. copy_len = 0;
  396. for (i = 0; i < iov_len; i++) {
  397. memcpy (&fragmentation_data[fragment_size],
  398. iovec[i].iov_base, iovec[i].iov_len);
  399. fragment_size += iovec[i].iov_len;
  400. copy_len += iovec[i].iov_len;
  401. }
  402. max_packet_size = TOTEMPG_PACKET_SIZE -
  403. (sizeof (unsigned short) * (mcast_packed_msg_count + 1));
  404. if (fragment_size >= max_packet_size) {
  405. /*
  406. * Determine size of packed data so far
  407. */
  408. for (j = 0; j < mcast_packed_msg_count; j++) {
  409. fragment_size_assem += mcast_packed_msg_lens[j];
  410. }
  411. /*
  412. * If there was previously packed data, remainder of packet
  413. * should be consumed
  414. */
  415. if (max_packet_size - fragment_size_assem) {
  416. mcast_packed_msg_lens[mcast_packed_msg_count] = max_packet_size - fragment_size_assem;
  417. mcast_packed_msg_count++;
  418. } else {
  419. max_packet_size = TOTEMPG_PACKET_SIZE -
  420. (sizeof (unsigned short) * mcast_packed_msg_count);
  421. }
  422. mcast.fragmented = 1;
  423. /*
  424. * Multicast any full fragments
  425. */
  426. fragment_index = max_packet_size;
  427. f_i = 0;
  428. while (fragment_index <= fragment_size) {
  429. mcast.msg_count = mcast_packed_msg_count;
  430. if (fragment_index == fragment_size) {
  431. mcast.fragmented = 0;
  432. }
  433. iovecs[0].iov_base = &mcast;
  434. iovecs[0].iov_len = sizeof (struct totempg_mcast);
  435. iovecs[1].iov_base = mcast_packed_msg_lens;
  436. iovecs[1].iov_len = mcast_packed_msg_count * sizeof (unsigned short);
  437. iovecs[2].iov_base = &fragmentation_data[f_i];
  438. iovecs[2].iov_len = max_packet_size;
  439. f_i += max_packet_size;
  440. /*
  441. * Ensure maximum message size is being queued
  442. */
  443. for (goober = 0, j = 0; j < 3; j++) {
  444. goober += iovecs[j].iov_len;
  445. }
  446. //assert (goober == 1408);
  447. for (i = 0; i < mcast_packed_msg_count; i++) {
  448. }
  449. res = totemsrp_mcast (iovecs, 3, guarantee);
  450. remaining_size = fragment_size - f_i;
  451. fragment_index += max_packet_size;
  452. mcast_packed_msg_count = 1;
  453. max_packet_size = TOTEMPG_PACKET_SIZE -
  454. (sizeof (unsigned short) * 1);
  455. mcast_packed_msg_lens[0] = max_packet_size;
  456. }
  457. /*
  458. * Copy remaining fragmented data
  459. */
  460. assert (remaining_size >= 0);
  461. if (remaining_size > 0) {
  462. memmove (&fragmentation_data[0],
  463. &fragmentation_data[fragment_size - remaining_size],
  464. remaining_size);
  465. mcast_packed_msg_lens[0] = remaining_size;
  466. mcast_packed_msg_count = 1;
  467. fragment_size = remaining_size;
  468. } else {
  469. mcast_packed_msg_count = 0;
  470. fragment_size = 0;
  471. }
  472. } else {
  473. mcast_packed_msg_lens[mcast_packed_msg_count] = copy_len;
  474. mcast_packed_msg_count++;
  475. }
  476. return (res);
  477. }
  478. /*
  479. * Determine if a message of msg_size could be queued
  480. */
  481. int totempg_send_ok (
  482. int msg_size)
  483. {
  484. int avail = 0;
  485. avail = totemsrp_avail ();
  486. return (avail > 200);
  487. }