totempg.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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. struct totempg_mcast {
  95. struct totempg_mcast_header header;
  96. short fragmented; /* This message continues into next message */
  97. short msg_count;
  98. /*
  99. * short msg_len[msg_count];
  100. */
  101. /*
  102. * data for messages
  103. */
  104. };
  105. /*
  106. * Maximum packet size for totem pg messages
  107. */
  108. #define TOTEMPG_PACKET_SIZE (TOTEMSRP_PACKET_SIZE_MAX - sizeof (struct totempg_mcast))
  109. /*
  110. * Local variables used for packing small messages
  111. */
  112. static unsigned short mcast_packed_msg_lens[TOTEMSRP_PACKET_SIZE_MAX];
  113. static int mcast_packed_msg_count = 0;
  114. static void (*app_deliver_fn) (
  115. struct in_addr source_addr,
  116. struct iovec *iovec,
  117. int iov_len,
  118. int endian_conversion_required) = 0;
  119. static void (*app_confchg_fn) (
  120. enum totempg_configuration_type configuration_type,
  121. struct in_addr *member_list, void *member_list_private,
  122. int member_list_entries,
  123. struct in_addr *left_list, void *left_list_private,
  124. int left_list_entries,
  125. struct in_addr *joined_list, void *joined_list_private,
  126. int joined_list_entries) = 0;
  127. struct assembly {
  128. struct in_addr addr;
  129. unsigned char data[MESSAGE_SIZE_MAX];
  130. int index;
  131. };
  132. struct assembly *assembly_list[16]; // MAX PROCESSORS TODO
  133. int assembly_list_entries = 0;
  134. static unsigned char fragmentation_data[TOTEMPG_PACKET_SIZE];
  135. int fragment_size = 0;
  136. static struct iovec iov_delv;
  137. static struct assembly *find_assembly (struct in_addr addr)
  138. {
  139. int i;
  140. for (i = 0; i < assembly_list_entries; i++) {
  141. if (addr.s_addr == assembly_list[i]->addr.s_addr) {
  142. return (assembly_list[i]);
  143. }
  144. }
  145. return (0);
  146. }
  147. static void totempg_confchg_fn (
  148. enum totempg_configuration_type configuration_type,
  149. struct in_addr *member_list, void *member_list_private,
  150. int member_list_entries,
  151. struct in_addr *left_list, void *left_list_private,
  152. int left_list_entries,
  153. struct in_addr *joined_list, void *joined_list_private,
  154. int joined_list_entries)
  155. {
  156. int i;
  157. int j;
  158. int found;
  159. for (i = 0; i < member_list_entries; i++) {
  160. found = 0;
  161. for (j = 0; j < assembly_list_entries; j++) {
  162. if (member_list[i].s_addr == assembly_list[j]->addr.s_addr) {
  163. found = 1;
  164. break;
  165. }
  166. }
  167. if (found == 0) {
  168. assembly_list[assembly_list_entries] =
  169. malloc (sizeof (struct assembly));
  170. assert (assembly_list[assembly_list_entries]); // TODO
  171. assembly_list[assembly_list_entries]->addr.s_addr = member_list[i].s_addr;
  172. assembly_list[i]->index = 0;
  173. assembly_list_entries += 1;
  174. }
  175. }
  176. app_confchg_fn (configuration_type,
  177. member_list, member_list_private, member_list_entries,
  178. left_list, left_list_private, left_list_entries,
  179. joined_list, joined_list_private, joined_list_entries);
  180. }
  181. static void totempg_deliver_fn (
  182. struct in_addr source_addr,
  183. struct iovec *iovec,
  184. int iov_len,
  185. int endian_conversion_required)
  186. {
  187. struct totempg_mcast *mcast;
  188. unsigned short *msg_lens;
  189. int i;
  190. struct assembly *assembly;
  191. char header[1500];
  192. int h_index;
  193. int a_i = 0;
  194. int msg_count;
  195. assembly = find_assembly (source_addr);
  196. assert (assembly);
  197. /*
  198. * Assemble the header into one block of data
  199. * Assemble the packet contents into one block of data to simplify delivery
  200. */
  201. if (iov_len == 1) {
  202. /* message originated from external processor - 1 iovec for full msg */
  203. char *data;
  204. int datasize;
  205. mcast = (struct totempg_mcast *)iovec[0].iov_base;
  206. msg_count = mcast->msg_count;
  207. if (endian_conversion_required) {
  208. msg_count = swab16 (mcast->msg_count);
  209. }
  210. datasize = sizeof (struct totempg_mcast) +
  211. msg_count * sizeof (unsigned short);
  212. memcpy (header, iovec[0].iov_base, datasize);
  213. data = iovec[0].iov_base;
  214. msg_lens = (unsigned short *) (header + sizeof (struct totempg_mcast));
  215. memcpy (&assembly->data[assembly->index], &data[datasize],
  216. iovec[0].iov_len - datasize);
  217. } else {
  218. /* message originated from local processor - <1 iovec for full msg */
  219. h_index = 0;
  220. for (i = 0; i < 2; i++) {
  221. memcpy (&header[h_index], iovec[i].iov_base, iovec[i].iov_len);
  222. h_index += iovec[i].iov_len;
  223. }
  224. mcast = (struct totempg_mcast *)header;
  225. // TODO make sure we are using a copy of mcast not the actual data itself
  226. msg_lens = (unsigned short *) (header + sizeof (struct totempg_mcast));
  227. for (i = 2; i < iov_len; i++) {
  228. a_i = assembly->index;
  229. memcpy (&assembly->data[a_i], iovec[i].iov_base, iovec[i].iov_len);
  230. a_i += msg_lens[i - 2];
  231. }
  232. iov_len -= 2;
  233. }
  234. if (endian_conversion_required) {
  235. mcast->fragmented = swab16 (mcast->fragmented);
  236. mcast->msg_count = swab16 (mcast->msg_count);
  237. for (i = 0; i < mcast->msg_count; i++) {
  238. msg_lens[i] = swab16 (msg_lens[i]);
  239. }
  240. }
  241. /*
  242. printf ("Message fragmented %d count %d\n", mcast->fragmented, mcast->msg_count);
  243. for (i = 0; i < mcast->msg_count; i++) {
  244. printf ("len[%d] = %d\n", i, msg_lens[i]);
  245. }
  246. */
  247. /*
  248. * Deliver all full messages in packed message
  249. */
  250. /*
  251. * this message's last packed message is not a fragment
  252. */
  253. if (mcast->fragmented == 0) {
  254. iov_delv.iov_base = &assembly->data[0];
  255. iov_delv.iov_len = assembly->index + msg_lens[0];
  256. for (i = 0; i < mcast->msg_count; i++) {
  257. assembly->index += msg_lens[i];
  258. //printf ("app deliver\n");
  259. app_deliver_fn (source_addr, &iov_delv, 1,
  260. endian_conversion_required);
  261. iov_delv.iov_base = &assembly->data[assembly->index];
  262. iov_delv.iov_len = msg_lens[i + 1];
  263. }
  264. assembly->index = 0;
  265. } else
  266. /*
  267. * This message's last packed message is a fragment
  268. */
  269. if (mcast->fragmented == 1) {
  270. iov_delv.iov_base = &assembly->data[0];
  271. iov_delv.iov_len = assembly->index + msg_lens[0];
  272. for (i = 0; i < mcast->msg_count - 1; i++) {
  273. assembly->index += msg_lens[i];
  274. //printf ("app deliver\n");
  275. app_deliver_fn (source_addr, &iov_delv, 1,
  276. endian_conversion_required);
  277. iov_delv.iov_base = &assembly->data[assembly->index];
  278. iov_delv.iov_len = msg_lens[i + 1];
  279. }
  280. if (mcast->msg_count > 1) {
  281. memmove (&assembly->data[0],
  282. &assembly->data[assembly->index],
  283. msg_lens[mcast->msg_count - 1]);
  284. assembly->index = 0;
  285. }
  286. assembly->index += msg_lens[mcast->msg_count - 1];
  287. }
  288. }
  289. /*
  290. * Totem Process Group Abstraction
  291. * depends on poll abstraction, POSIX, IPV4
  292. */
  293. /*
  294. * Initialize the logger
  295. */
  296. void totempg_log_printf_init (
  297. void (*log_printf) (int , char *, ...),
  298. int log_level_security,
  299. int log_level_error,
  300. int log_level_warning,
  301. int log_level_notice,
  302. int log_level_debug)
  303. {
  304. totemsrp_log_printf_init (
  305. log_printf,
  306. log_level_security,
  307. log_level_error,
  308. log_level_warning,
  309. log_level_notice,
  310. log_level_debug);
  311. }
  312. void *callback_token_received_handle;
  313. int callback_token_received_fn (enum totemsrp_callback_token_type type,
  314. void *data)
  315. {
  316. struct totempg_mcast mcast;
  317. struct iovec iovecs[3];
  318. int res;
  319. if (mcast_packed_msg_count == 0) {
  320. return (0);
  321. }
  322. mcast.fragmented = 0;
  323. mcast.msg_count = mcast_packed_msg_count;
  324. iovecs[0].iov_base = &mcast;
  325. iovecs[0].iov_len = sizeof (struct totempg_mcast);
  326. iovecs[1].iov_base = mcast_packed_msg_lens;
  327. iovecs[1].iov_len = mcast_packed_msg_count * sizeof (unsigned short);
  328. iovecs[2].iov_base = &fragmentation_data[0];
  329. iovecs[2].iov_len = fragment_size;
  330. res = totemsrp_mcast (iovecs, 3, 0);
  331. mcast_packed_msg_count = 0;
  332. fragment_size = 0;
  333. return (0);
  334. }
  335. /*
  336. * Initialize the totem process group abstraction
  337. */
  338. int totempg_initialize (
  339. struct sockaddr_in *sockaddr_mcast,
  340. struct totempg_interface *interfaces,
  341. int interface_count,
  342. poll_handle *poll_handle,
  343. unsigned char *private_key,
  344. int private_key_len,
  345. void *member_private,
  346. int member_private_len,
  347. void (*deliver_fn) (
  348. struct in_addr source_addr,
  349. struct iovec *iovec,
  350. int iov_len,
  351. int endian_conversion_required),
  352. void (*confchg_fn) (
  353. enum totempg_configuration_type configuration_type,
  354. struct in_addr *member_list, void *member_list_private,
  355. int member_list_entries,
  356. struct in_addr *left_list, void *left_list_private,
  357. int left_list_entries,
  358. struct in_addr *joined_list, void *joined_list_private,
  359. int joined_list_entries))
  360. {
  361. int res;
  362. app_deliver_fn = deliver_fn;
  363. app_confchg_fn = confchg_fn;
  364. res = totemsrp_initialize (sockaddr_mcast, (struct totemsrp_interface *)interfaces,
  365. interface_count,
  366. poll_handle,
  367. private_key, private_key_len,
  368. member_private, member_private_len,
  369. totempg_deliver_fn, totempg_confchg_fn);
  370. totemsrp_callback_token_create (&callback_token_received_handle,
  371. TOTEMSRP_CALLBACK_TOKEN_RECEIVED,
  372. 0,
  373. callback_token_received_fn,
  374. 0);
  375. return (res);
  376. }
  377. /*
  378. * Multicast a message
  379. */
  380. int totempg_mcast (
  381. struct iovec *iovec,
  382. int iov_len,
  383. int guarantee)
  384. {
  385. int res = 0;
  386. struct totempg_mcast mcast;
  387. struct iovec iovecs[3];
  388. int i;
  389. int max_packet_size = 0;
  390. int copy_len = 0;
  391. int copy_base = 0;
  392. max_packet_size = TOTEMPG_PACKET_SIZE -
  393. (sizeof (unsigned short) * (mcast_packed_msg_count + 1));
  394. mcast_packed_msg_lens[mcast_packed_msg_count] = 0;
  395. for (i = 0; i < iov_len; ) {
  396. mcast.fragmented = 0;
  397. copy_len = iovec[i].iov_len - copy_base;
  398. /*
  399. * If it all fits with room left over, copy it in.
  400. * We need to leave at least sizeof(short) + 1 bytes in the
  401. * fragment_buffer on exit so that max_packet_size + fragment_size
  402. * doesn't exceed the size of the fragment_buffer on the next call.
  403. */
  404. if ((copy_len + fragment_size) <
  405. (max_packet_size - sizeof (unsigned short))) {
  406. memcpy (&fragmentation_data[fragment_size],
  407. iovec[i].iov_base + copy_base, copy_len);
  408. fragment_size += copy_len;
  409. mcast_packed_msg_lens[mcast_packed_msg_count] += copy_len;
  410. copy_len = 0;
  411. copy_base = 0;
  412. i++;
  413. continue;
  414. /*
  415. * If it just fits or is too big, then send out what fits.
  416. */
  417. } else {
  418. copy_len = min(copy_len, max_packet_size - fragment_size);
  419. memcpy (&fragmentation_data[fragment_size],
  420. iovec[i].iov_base + copy_base, copy_len);
  421. mcast_packed_msg_lens[mcast_packed_msg_count] += copy_len;
  422. /*
  423. * if we're not on the last iovec or the iovec is too large to
  424. * fit, then indicate a fragment.
  425. */
  426. if ((i < (iov_len - 1)) ||
  427. ((copy_base + copy_len) < iovec[i].iov_len)) {
  428. mcast.fragmented = 1;
  429. }
  430. /*
  431. * assemble the message and send it
  432. */
  433. mcast.msg_count = ++mcast_packed_msg_count;
  434. iovecs[0].iov_base = &mcast;
  435. iovecs[0].iov_len = sizeof(struct totempg_mcast);
  436. iovecs[1].iov_base = mcast_packed_msg_lens;
  437. iovecs[1].iov_len = mcast_packed_msg_count *
  438. sizeof(unsigned short);
  439. iovecs[2].iov_base = fragmentation_data;
  440. iovecs[2].iov_len = max_packet_size;
  441. res = totemsrp_mcast (iovecs, 3, guarantee);
  442. /*
  443. * Recalculate counts and indexes for the next.
  444. */
  445. mcast_packed_msg_lens[0] = 0;
  446. mcast_packed_msg_count = 0;
  447. fragment_size = 0;
  448. max_packet_size = TOTEMPG_PACKET_SIZE - (sizeof(unsigned short));
  449. /*
  450. * If the iovec all fit, go to the next iovec
  451. */
  452. if ((copy_base + copy_len) == iovec[i].iov_len) {
  453. copy_len = 0;
  454. copy_base = 0;
  455. i++;
  456. /*
  457. * Continue with the rest of the current iovec.
  458. */
  459. } else {
  460. copy_base += copy_len;
  461. }
  462. }
  463. }
  464. /*
  465. * Bump only if we added message data. This may be zero if
  466. * the last buffer just fit into the fragmentation_data buffer
  467. * and we were at the last iovec.
  468. */
  469. if (mcast_packed_msg_lens[mcast_packed_msg_count]) {
  470. mcast_packed_msg_count++;
  471. }
  472. return (res);
  473. }
  474. /*
  475. * Determine if a message of msg_size could be queued
  476. */
  477. int totempg_send_ok (
  478. int msg_size)
  479. {
  480. int avail = 0;
  481. avail = totemsrp_avail ();
  482. return (avail > 200);
  483. }
  484. /*
  485. * vi: set autoindent tabstop=4 shiftwidth=4 :
  486. */