totempg.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177
  1. /*
  2. * Copyright (c) 2003-2005 MontaVista Software, Inc.
  3. * Copyright (c) 2005 OSDL.
  4. *
  5. * All rights reserved.
  6. *
  7. * Author: Steven Dake (sdake@mvista.com)
  8. * Mark Haverkamp (markh@osdl.org)
  9. *
  10. * This software licensed under BSD license, the text of which follows:
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. * - Redistributions in binary form must reproduce the above copyright notice,
  18. * this list of conditions and the following disclaimer in the documentation
  19. * and/or other materials provided with the distribution.
  20. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  21. * contributors may be used to endorse or promote products derived from this
  22. * software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  28. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  31. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  32. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  33. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  34. * THE POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. /*
  37. * FRAGMENTATION AND PACKING ALGORITHM:
  38. *
  39. * Assemble the entire message into one buffer
  40. * if full fragment
  41. * store fragment into lengths list
  42. * for each full fragment
  43. * multicast fragment
  44. * set length and fragment fields of pg mesage
  45. * store remaining multicast into head of fragmentation data and set lens field
  46. *
  47. * If a message exceeds the maximum packet size allowed by the totem
  48. * single ring protocol, the protocol could lose forward progress.
  49. * Statically calculating the allowed data amount doesn't work because
  50. * the amount of data allowed depends on the number of fragments in
  51. * each message. In this implementation, the maximum fragment size
  52. * is dynamically calculated for each fragment added to the message.
  53. * It is possible for a message to be two bytes short of the maximum
  54. * packet size. This occurs when a message or collection of
  55. * messages + the mcast header + the lens are two bytes short of the
  56. * end of the packet. Since another len field consumes two bytes, the
  57. * len field would consume the rest of the packet without room for data.
  58. *
  59. * One optimization would be to forgo the final len field and determine
  60. * it from the size of the udp datagram. Then this condition would no
  61. * longer occur.
  62. */
  63. /*
  64. * ASSEMBLY AND UNPACKING ALGORITHM:
  65. *
  66. * copy incoming packet into assembly data buffer indexed by current
  67. * location of end of fragment
  68. *
  69. * if not fragmented
  70. * deliver all messages in assembly data buffer
  71. * else
  72. * if msg_count > 1 and fragmented
  73. * deliver all messages except last message in assembly data buffer
  74. * copy last fragmented section to start of assembly data buffer
  75. * else
  76. * if msg_count = 1 and fragmented
  77. * do nothing
  78. *
  79. */
  80. #include <netinet/in.h>
  81. #include <sys/uio.h>
  82. #include <stdio.h>
  83. #include <stdlib.h>
  84. #include <string.h>
  85. #include <assert.h>
  86. #include <pthread.h>
  87. #include "../include/hdb.h"
  88. #include "totempg.h"
  89. #include "totemmrp.h"
  90. #include "totemsrp.h"
  91. #include "swab.h"
  92. #define min(a,b) ((a) < (b)) ? a : b
  93. struct totempg_mcast_header {
  94. short version;
  95. short type;
  96. };
  97. /*
  98. * totempg_mcast structure
  99. *
  100. * header: Identify the mcast.
  101. * fragmented: Set if this message continues into next message
  102. * continuation: Set if this message is a continuation from last message
  103. * msg_count Indicates how many packed messages are contained
  104. * in the mcast.
  105. * Also, the size of each packed message and the messages themselves are
  106. * appended to the end of this structure when sent.
  107. */
  108. struct totempg_mcast {
  109. struct totempg_mcast_header header;
  110. unsigned char fragmented;
  111. unsigned char continuation;
  112. unsigned short msg_count;
  113. /*
  114. * short msg_len[msg_count];
  115. */
  116. /*
  117. * data for messages
  118. */
  119. };
  120. /*
  121. * Maximum packet size for totem pg messages
  122. */
  123. #define TOTEMPG_PACKET_SIZE (totempg_totem_config->net_mtu - \
  124. sizeof (struct totempg_mcast))
  125. /*
  126. * Local variables used for packing small messages
  127. */
  128. static unsigned short mcast_packed_msg_lens[FRAME_SIZE_MAX];
  129. static int mcast_packed_msg_count = 0;
  130. /*
  131. * Function and data used to log messages
  132. */
  133. static int totempg_log_level_security;
  134. static int totempg_log_level_error;
  135. static int totempg_log_level_warning;
  136. static int totempg_log_level_notice;
  137. static int totempg_log_level_debug;
  138. static void (*totempg_log_printf) (char *file, int line, int level, char *format, ...) = NULL;
  139. struct totem_config *totempg_totem_config;
  140. struct assembly {
  141. unsigned int nodeid;
  142. unsigned char data[MESSAGE_SIZE_MAX];
  143. int index;
  144. unsigned char last_frag_num;
  145. };
  146. struct assembly *assembly_list[PROCESSOR_COUNT_MAX];
  147. int assembly_list_entries = 0;
  148. /*
  149. * Staging buffer for packed messages. Messages are staged in this buffer
  150. * before sending. Multiple messages may fit which cuts down on the
  151. * number of mcasts sent. If a message doesn't completely fit, then
  152. * the mcast header has a fragment bit set that says that there are more
  153. * data to follow. fragment_size is an index into the buffer. It indicates
  154. * the size of message data and where to place new message data.
  155. * fragment_contuation indicates whether the first packed message in
  156. * the buffer is a continuation of a previously packed fragment.
  157. */
  158. static unsigned char *fragmentation_data;
  159. static int fragment_size = 0;
  160. static int fragment_continuation = 0;
  161. static struct iovec iov_delv;
  162. static unsigned int totempg_max_handle = 0;
  163. struct totempg_group_instance {
  164. void (*deliver_fn) (
  165. unsigned int nodeid,
  166. struct iovec *iovec,
  167. int iov_len,
  168. int endian_conversion_required);
  169. void (*confchg_fn) (
  170. enum totem_configuration_type configuration_type,
  171. unsigned int *member_list, int member_list_entries,
  172. unsigned int *left_list, int left_list_entries,
  173. unsigned int *joined_list, int joined_list_entries,
  174. struct memb_ring_id *ring_id);
  175. struct totempg_group *groups;
  176. int groups_cnt;
  177. };
  178. static struct hdb_handle_database totempg_groups_instance_database = {
  179. .handle_count = 0,
  180. .handles = 0,
  181. .iterator = 0,
  182. .mutex = PTHREAD_MUTEX_INITIALIZER
  183. };
  184. static int send_ok (int msg_size);
  185. static unsigned char next_fragment = 1;
  186. static pthread_mutex_t totempg_mutex = PTHREAD_MUTEX_INITIALIZER;
  187. static pthread_mutex_t callback_token_mutex = PTHREAD_MUTEX_INITIALIZER;
  188. static pthread_mutex_t mcast_msg_mutex = PTHREAD_MUTEX_INITIALIZER;
  189. #define log_printf(level, format, args...) \
  190. totempg_log_printf (__FILE__, __LINE__, level, format, ##args)
  191. static struct assembly *find_assembly (unsigned int nodeid)
  192. {
  193. int i;
  194. for (i = 0; i < assembly_list_entries; i++) {
  195. if (nodeid == assembly_list[i]->nodeid) {
  196. return (assembly_list[i]);
  197. }
  198. }
  199. return (0);
  200. }
  201. static inline void app_confchg_fn (
  202. enum totem_configuration_type configuration_type,
  203. unsigned int *member_list, int member_list_entries,
  204. unsigned int *left_list, int left_list_entries,
  205. unsigned int *joined_list, int joined_list_entries,
  206. struct memb_ring_id *ring_id)
  207. {
  208. int i;
  209. struct totempg_group_instance *instance;
  210. unsigned int res;
  211. for (i = 0; i <= totempg_max_handle; i++) {
  212. res = hdb_handle_get (&totempg_groups_instance_database,
  213. i, (void *)&instance);
  214. if (res == 0) {
  215. if (instance->confchg_fn) {
  216. instance->confchg_fn (
  217. configuration_type,
  218. member_list,
  219. member_list_entries,
  220. left_list,
  221. left_list_entries,
  222. joined_list,
  223. joined_list_entries,
  224. ring_id);
  225. }
  226. hdb_handle_put (&totempg_groups_instance_database, i);
  227. }
  228. }
  229. }
  230. static inline void group_endian_convert (
  231. struct iovec *iovec)
  232. {
  233. unsigned short *group_len;
  234. int i;
  235. group_len = (unsigned short *)iovec->iov_base;
  236. group_len[0] = swab16(group_len[0]);
  237. for (i = 1; i < group_len[0] + 1; i++) {
  238. group_len[i] = swab16(group_len[i]);
  239. }
  240. }
  241. static inline int group_matches (
  242. struct iovec *iovec,
  243. unsigned int iov_len,
  244. struct totempg_group *groups_b,
  245. unsigned int group_b_cnt,
  246. unsigned int *adjust_iovec)
  247. {
  248. unsigned short *group_len;
  249. char *group_name;
  250. int i;
  251. int j;
  252. assert (iov_len == 1);
  253. group_len = (unsigned short *)iovec->iov_base;
  254. group_name = ((char *)iovec->iov_base) +
  255. sizeof (unsigned short) * (group_len[0] + 1);
  256. /*
  257. * Calculate amount to adjust the iovec by before delivering to app
  258. */
  259. *adjust_iovec = sizeof (unsigned short) * (group_len[0] + 1);
  260. for (i = 1; i < group_len[0] + 1; i++) {
  261. *adjust_iovec += group_len[i];
  262. }
  263. /*
  264. * Determine if this message should be delivered to this instance
  265. */
  266. for (i = 1; i < group_len[0] + 1; i++) {
  267. for (j = 0; j < group_b_cnt; j++) {
  268. if ((group_len[i] == groups_b[j].group_len) &&
  269. (memcmp (groups_b[j].group, group_name, group_len[i]) == 0)) {
  270. return (1);
  271. }
  272. }
  273. group_name += group_len[i];
  274. }
  275. return (0);
  276. }
  277. static inline void app_deliver_fn (
  278. unsigned int nodeid,
  279. struct iovec *iovec,
  280. unsigned int iov_len,
  281. int endian_conversion_required)
  282. {
  283. int i;
  284. struct totempg_group_instance *instance;
  285. struct iovec stripped_iovec;
  286. unsigned int adjust_iovec;
  287. unsigned int res;
  288. if (endian_conversion_required) {
  289. group_endian_convert (iovec);
  290. }
  291. for (i = 0; i <= totempg_max_handle; i++) {
  292. res = hdb_handle_get (&totempg_groups_instance_database,
  293. i, (void *)&instance);
  294. if (res == 0) {
  295. assert (iov_len == 1);
  296. if (group_matches (iovec, iov_len, instance->groups, instance->groups_cnt, &adjust_iovec)) {
  297. stripped_iovec.iov_len = iovec->iov_len - adjust_iovec;
  298. stripped_iovec.iov_base = (char *)iovec->iov_base + adjust_iovec;
  299. instance->deliver_fn (
  300. nodeid,
  301. &stripped_iovec,
  302. iov_len,
  303. endian_conversion_required);
  304. }
  305. hdb_handle_put (&totempg_groups_instance_database, i);
  306. }
  307. }
  308. }
  309. static void totempg_confchg_fn (
  310. enum totem_configuration_type configuration_type,
  311. unsigned int *member_list, int member_list_entries,
  312. unsigned int *left_list, int left_list_entries,
  313. unsigned int *joined_list, int joined_list_entries,
  314. struct memb_ring_id *ring_id)
  315. {
  316. int i;
  317. int j;
  318. int found;
  319. /*
  320. * Clean out the assembly area for nodes that have left the
  321. * membership. If they return, we don't want any stale message
  322. * data that may be there.
  323. */
  324. for (i = 0; i < left_list_entries; i++) {
  325. for (j = 0; j < assembly_list_entries; j++) {
  326. if (left_list[i] == assembly_list[j]->nodeid) {
  327. assembly_list[j]->index = 0;
  328. }
  329. }
  330. }
  331. /*
  332. * Create a message assembly area for any new members.
  333. */
  334. for (i = 0; i < member_list_entries; i++) {
  335. found = 0;
  336. for (j = 0; j < assembly_list_entries; j++) {
  337. if (member_list[i] == assembly_list[j]->nodeid) {
  338. found = 1;
  339. break;
  340. }
  341. }
  342. if (found == 0) {
  343. assembly_list[assembly_list_entries] =
  344. malloc (sizeof (struct assembly));
  345. assert (assembly_list[assembly_list_entries]); // TODO
  346. assembly_list[assembly_list_entries]->nodeid =
  347. member_list[i];
  348. assembly_list[assembly_list_entries]->index = 0;
  349. assembly_list_entries += 1;
  350. }
  351. }
  352. app_confchg_fn (configuration_type,
  353. member_list, member_list_entries,
  354. left_list, left_list_entries,
  355. joined_list, joined_list_entries,
  356. ring_id);
  357. }
  358. static void totempg_deliver_fn (
  359. unsigned int nodeid,
  360. struct iovec *iovec,
  361. int iov_len,
  362. int endian_conversion_required)
  363. {
  364. struct totempg_mcast *mcast;
  365. unsigned short *msg_lens;
  366. int i;
  367. struct assembly *assembly;
  368. char header[FRAME_SIZE_MAX];
  369. int h_index;
  370. int a_i = 0;
  371. int msg_count;
  372. int continuation;
  373. int start;
  374. assembly = find_assembly (nodeid);
  375. assert (assembly);
  376. /*
  377. * Assemble the header into one block of data and
  378. * assemble the packet contents into one block of data to simplify delivery
  379. */
  380. if (iov_len == 1) {
  381. /*
  382. * This message originated from external processor
  383. * because there is only one iovec for the full msg.
  384. */
  385. char *data;
  386. int datasize;
  387. mcast = (struct totempg_mcast *)iovec[0].iov_base;
  388. if (endian_conversion_required) {
  389. mcast->msg_count = swab16 (mcast->msg_count);
  390. }
  391. msg_count = mcast->msg_count;
  392. datasize = sizeof (struct totempg_mcast) +
  393. msg_count * sizeof (unsigned short);
  394. memcpy (header, iovec[0].iov_base, datasize);
  395. assert(iovec);
  396. data = iovec[0].iov_base;
  397. msg_lens = (unsigned short *) (header + sizeof (struct totempg_mcast));
  398. if (endian_conversion_required) {
  399. for (i = 0; i < mcast->msg_count; i++) {
  400. msg_lens[i] = swab16 (msg_lens[i]);
  401. }
  402. }
  403. memcpy (&assembly->data[assembly->index], &data[datasize],
  404. iovec[0].iov_len - datasize);
  405. } else {
  406. /*
  407. * The message originated from local processor
  408. * becasue there is greater than one iovec for then full msg.
  409. */
  410. h_index = 0;
  411. for (i = 0; i < 2; i++) {
  412. memcpy (&header[h_index], iovec[i].iov_base, iovec[i].iov_len);
  413. h_index += iovec[i].iov_len;
  414. }
  415. mcast = (struct totempg_mcast *)header;
  416. // TODO make sure we are using a copy of mcast not the actual data itself
  417. msg_lens = (unsigned short *) (header + sizeof (struct totempg_mcast));
  418. for (i = 2; i < iov_len; i++) {
  419. a_i = assembly->index;
  420. assert (iovec[i].iov_len + a_i <= MESSAGE_SIZE_MAX);
  421. memcpy (&assembly->data[a_i], iovec[i].iov_base, iovec[i].iov_len);
  422. a_i += msg_lens[i - 2];
  423. }
  424. iov_len -= 2;
  425. }
  426. /*
  427. * If the last message in the buffer is a fragment, then we
  428. * can't deliver it. We'll first deliver the full messages
  429. * then adjust the assembly buffer so we can add the rest of the
  430. * fragment when it arrives.
  431. */
  432. msg_count = mcast->fragmented ? mcast->msg_count - 1 : mcast->msg_count;
  433. continuation = mcast->continuation;
  434. iov_delv.iov_base = &assembly->data[0];
  435. iov_delv.iov_len = assembly->index + msg_lens[0];
  436. /*
  437. * Make sure that if this message is a continuation, that it
  438. * matches the sequence number of the previous fragment.
  439. * Also, if the first packed message is a continuation
  440. * of a previous message, but the assembly buffer
  441. * is empty, then we need to discard it since we can't
  442. * assemble a complete message. Likewise, if this message isn't a
  443. * continuation and the assembly buffer is empty, we have to discard
  444. * the continued message.
  445. */
  446. start = 0;
  447. if (continuation) {
  448. if (continuation != assembly->last_frag_num) {
  449. log_printf (totempg_log_level_error,
  450. "Message continuation doesn't match previous frag e: %u - a: %u\n",
  451. assembly->last_frag_num, continuation);
  452. continuation = 0;
  453. }
  454. if ((assembly->index == 0) ||
  455. (!continuation && assembly->index)) {
  456. log_printf (totempg_log_level_error,
  457. "Throwing away broken message: continuation %u, index %u\n",
  458. continuation, assembly->index);
  459. continuation = 0;
  460. }
  461. /*
  462. * we decided to throw away the first continued message
  463. * in this buffer, if continuation was set to zero.
  464. */
  465. if (!continuation) {
  466. assembly->index += msg_lens[0];
  467. iov_delv.iov_base = &assembly->data[assembly->index];
  468. iov_delv.iov_len = msg_lens[1];
  469. start = 1;
  470. }
  471. }
  472. for (i = start; i < msg_count; i++) {
  473. app_deliver_fn(nodeid, &iov_delv, 1,
  474. endian_conversion_required);
  475. assembly->index += msg_lens[i];
  476. iov_delv.iov_base = &assembly->data[assembly->index];
  477. if (i < (msg_count - 1)) {
  478. iov_delv.iov_len = msg_lens[i + 1];
  479. }
  480. }
  481. if (mcast->fragmented) {
  482. assembly->last_frag_num = mcast->fragmented;
  483. if (mcast->msg_count > 1) {
  484. memmove (&assembly->data[0],
  485. &assembly->data[assembly->index],
  486. msg_lens[msg_count]);
  487. assembly->index = 0;
  488. }
  489. assembly->index += msg_lens[msg_count];
  490. } else {
  491. assembly->last_frag_num = 0;
  492. assembly->index = 0;
  493. }
  494. }
  495. /*
  496. * Totem Process Group Abstraction
  497. * depends on poll abstraction, POSIX, IPV4
  498. */
  499. void *callback_token_received_handle;
  500. int callback_token_received_fn (enum totem_callback_token_type type,
  501. void *data)
  502. {
  503. struct totempg_mcast mcast;
  504. struct iovec iovecs[3];
  505. int res;
  506. pthread_mutex_lock (&mcast_msg_mutex);
  507. if (mcast_packed_msg_count == 0) {
  508. pthread_mutex_unlock (&mcast_msg_mutex);
  509. return (0);
  510. }
  511. if (totemmrp_avail() == 0) {
  512. pthread_mutex_unlock (&mcast_msg_mutex);
  513. return (0);
  514. }
  515. mcast.fragmented = 0;
  516. /*
  517. * Was the first message in this buffer a continuation of a
  518. * fragmented message?
  519. */
  520. mcast.continuation = fragment_continuation;
  521. fragment_continuation = 0;
  522. mcast.msg_count = mcast_packed_msg_count;
  523. iovecs[0].iov_base = &mcast;
  524. iovecs[0].iov_len = sizeof (struct totempg_mcast);
  525. iovecs[1].iov_base = mcast_packed_msg_lens;
  526. iovecs[1].iov_len = mcast_packed_msg_count * sizeof (unsigned short);
  527. iovecs[2].iov_base = &fragmentation_data[0];
  528. iovecs[2].iov_len = fragment_size;
  529. res = totemmrp_mcast (iovecs, 3, 0);
  530. mcast_packed_msg_count = 0;
  531. fragment_size = 0;
  532. pthread_mutex_unlock (&mcast_msg_mutex);
  533. return (0);
  534. }
  535. /*
  536. * Initialize the totem process group abstraction
  537. */
  538. int totempg_initialize (
  539. poll_handle poll_handle,
  540. struct totem_config *totem_config)
  541. {
  542. int res;
  543. totempg_totem_config = totem_config;
  544. totempg_log_level_security = totem_config->totem_logging_configuration.log_level_security;
  545. totempg_log_level_error = totem_config->totem_logging_configuration.log_level_error;
  546. totempg_log_level_warning = totem_config->totem_logging_configuration.log_level_warning;
  547. totempg_log_level_notice = totem_config->totem_logging_configuration.log_level_notice;
  548. totempg_log_level_debug = totem_config->totem_logging_configuration.log_level_debug;
  549. totempg_log_printf = totem_config->totem_logging_configuration.log_printf;
  550. fragmentation_data = malloc (TOTEMPG_PACKET_SIZE);
  551. if (fragmentation_data == 0) {
  552. return (-1);
  553. }
  554. res = totemmrp_initialize (
  555. poll_handle,
  556. totem_config,
  557. totempg_deliver_fn,
  558. totempg_confchg_fn);
  559. totemmrp_callback_token_create (
  560. &callback_token_received_handle,
  561. TOTEM_CALLBACK_TOKEN_RECEIVED,
  562. 0,
  563. callback_token_received_fn,
  564. 0);
  565. totemsrp_net_mtu_adjust (totem_config);
  566. return (res);
  567. }
  568. void totempg_finalize (void)
  569. {
  570. pthread_mutex_lock (&totempg_mutex);
  571. totemmrp_finalize ();
  572. pthread_mutex_unlock (&totempg_mutex);
  573. }
  574. /*
  575. * Multicast a message
  576. */
  577. static int mcast_msg (
  578. struct iovec *iovec,
  579. int iov_len,
  580. int guarantee)
  581. {
  582. int res = 0;
  583. struct totempg_mcast mcast;
  584. struct iovec iovecs[3];
  585. int i;
  586. int max_packet_size = 0;
  587. int copy_len = 0;
  588. int copy_base = 0;
  589. int total_size = 0;
  590. pthread_mutex_lock (&mcast_msg_mutex);
  591. totemmrp_new_msg_signal ();
  592. max_packet_size = TOTEMPG_PACKET_SIZE -
  593. (sizeof (unsigned short) * (mcast_packed_msg_count + 1));
  594. mcast_packed_msg_lens[mcast_packed_msg_count] = 0;
  595. /*
  596. * Check if we would overwrite new message queue
  597. */
  598. for (i = 0; i < iov_len; i++) {
  599. total_size += iovec[i].iov_len;
  600. }
  601. if (send_ok (total_size + sizeof(unsigned short) *
  602. (mcast_packed_msg_count+1)) == 0) {
  603. pthread_mutex_unlock (&mcast_msg_mutex);
  604. return(-1);
  605. }
  606. for (i = 0; i < iov_len; ) {
  607. mcast.fragmented = 0;
  608. mcast.continuation = fragment_continuation;
  609. copy_len = iovec[i].iov_len - copy_base;
  610. /*
  611. * If it all fits with room left over, copy it in.
  612. * We need to leave at least sizeof(short) + 1 bytes in the
  613. * fragment_buffer on exit so that max_packet_size + fragment_size
  614. * doesn't exceed the size of the fragment_buffer on the next call.
  615. */
  616. if ((copy_len + fragment_size) <
  617. (max_packet_size - sizeof (unsigned short))) {
  618. memcpy (&fragmentation_data[fragment_size],
  619. iovec[i].iov_base + copy_base, copy_len);
  620. fragment_size += copy_len;
  621. mcast_packed_msg_lens[mcast_packed_msg_count] += copy_len;
  622. copy_len = 0;
  623. copy_base = 0;
  624. i++;
  625. continue;
  626. /*
  627. * If it just fits or is too big, then send out what fits.
  628. */
  629. } else {
  630. unsigned char *data_ptr;
  631. copy_len = min(copy_len, max_packet_size - fragment_size);
  632. if( copy_len == max_packet_size )
  633. data_ptr = iovec[i].iov_base + copy_base;
  634. else {
  635. data_ptr = fragmentation_data;
  636. memcpy (&fragmentation_data[fragment_size],
  637. iovec[i].iov_base + copy_base, copy_len);
  638. }
  639. memcpy (&fragmentation_data[fragment_size],
  640. iovec[i].iov_base + copy_base, copy_len);
  641. mcast_packed_msg_lens[mcast_packed_msg_count] += copy_len;
  642. /*
  643. * if we're not on the last iovec or the iovec is too large to
  644. * fit, then indicate a fragment. This also means that the next
  645. * message will have the continuation of this one.
  646. */
  647. if ((i < (iov_len - 1)) ||
  648. ((copy_base + copy_len) < iovec[i].iov_len)) {
  649. if (!next_fragment) {
  650. next_fragment++;
  651. }
  652. fragment_continuation = next_fragment;
  653. mcast.fragmented = next_fragment++;
  654. assert(fragment_continuation != 0);
  655. assert(mcast.fragmented != 0);
  656. } else {
  657. fragment_continuation = 0;
  658. }
  659. /*
  660. * assemble the message and send it
  661. */
  662. mcast.msg_count = ++mcast_packed_msg_count;
  663. iovecs[0].iov_base = &mcast;
  664. iovecs[0].iov_len = sizeof(struct totempg_mcast);
  665. iovecs[1].iov_base = mcast_packed_msg_lens;
  666. iovecs[1].iov_len = mcast_packed_msg_count *
  667. sizeof(unsigned short);
  668. iovecs[2].iov_base = data_ptr;
  669. iovecs[2].iov_len = max_packet_size;
  670. assert (totemmrp_avail() > 0);
  671. res = totemmrp_mcast (iovecs, 3, guarantee);
  672. /*
  673. * Recalculate counts and indexes for the next.
  674. */
  675. mcast_packed_msg_lens[0] = 0;
  676. mcast_packed_msg_count = 0;
  677. fragment_size = 0;
  678. max_packet_size = TOTEMPG_PACKET_SIZE - (sizeof(unsigned short));
  679. /*
  680. * If the iovec all fit, go to the next iovec
  681. */
  682. if ((copy_base + copy_len) == iovec[i].iov_len) {
  683. copy_len = 0;
  684. copy_base = 0;
  685. i++;
  686. /*
  687. * Continue with the rest of the current iovec.
  688. */
  689. } else {
  690. copy_base += copy_len;
  691. }
  692. }
  693. }
  694. /*
  695. * Bump only if we added message data. This may be zero if
  696. * the last buffer just fit into the fragmentation_data buffer
  697. * and we were at the last iovec.
  698. */
  699. if (mcast_packed_msg_lens[mcast_packed_msg_count]) {
  700. mcast_packed_msg_count++;
  701. }
  702. pthread_mutex_unlock (&mcast_msg_mutex);
  703. return (res);
  704. }
  705. /*
  706. * Determine if a message of msg_size could be queued
  707. */
  708. static int send_ok (
  709. int msg_size)
  710. {
  711. int avail = 0;
  712. int total;
  713. avail = totemmrp_avail ();
  714. /*
  715. * msg size less then totempg_totem_config->net_mtu - 25 will take up
  716. * a full message, so add +1
  717. * totempg_totem_config->net_mtu - 25 is for the totempg_mcast header
  718. */
  719. total = (msg_size / (totempg_totem_config->net_mtu - 25)) + 1;
  720. return (avail >= total);
  721. }
  722. int totempg_callback_token_create (
  723. void **handle_out,
  724. enum totem_callback_token_type type,
  725. int delete,
  726. int (*callback_fn) (enum totem_callback_token_type type, void *),
  727. void *data)
  728. {
  729. unsigned int res;
  730. pthread_mutex_lock (&callback_token_mutex);
  731. res = totemmrp_callback_token_create (handle_out, type, delete,
  732. callback_fn, data);
  733. pthread_mutex_unlock (&callback_token_mutex);
  734. return (res);
  735. }
  736. void totempg_callback_token_destroy (
  737. void *handle_out)
  738. {
  739. pthread_mutex_lock (&callback_token_mutex);
  740. totemmrp_callback_token_destroy (handle_out);
  741. pthread_mutex_unlock (&callback_token_mutex);
  742. }
  743. /*
  744. * vi: set autoindent tabstop=4 shiftwidth=4 :
  745. */
  746. int totempg_groups_initialize (
  747. totempg_groups_handle *handle,
  748. void (*deliver_fn) (
  749. unsigned int nodeid,
  750. struct iovec *iovec,
  751. int iov_len,
  752. int endian_conversion_required),
  753. void (*confchg_fn) (
  754. enum totem_configuration_type configuration_type,
  755. unsigned int *member_list, int member_list_entries,
  756. unsigned int *left_list, int left_list_entries,
  757. unsigned int *joined_list, int joined_list_entries,
  758. struct memb_ring_id *ring_id))
  759. {
  760. struct totempg_group_instance *instance;
  761. unsigned int res;
  762. pthread_mutex_lock (&totempg_mutex);
  763. res = hdb_handle_create (&totempg_groups_instance_database,
  764. sizeof (struct totempg_group_instance), handle);
  765. if (res != 0) {
  766. goto error_exit;
  767. }
  768. if (*handle > totempg_max_handle) {
  769. totempg_max_handle = *handle;
  770. }
  771. res = hdb_handle_get (&totempg_groups_instance_database, *handle,
  772. (void *)&instance);
  773. if (res != 0) {
  774. goto error_destroy;
  775. }
  776. instance->deliver_fn = deliver_fn;
  777. instance->confchg_fn = confchg_fn;
  778. instance->groups = 0;
  779. instance->groups_cnt = 0;
  780. hdb_handle_put (&totempg_groups_instance_database, *handle);
  781. pthread_mutex_unlock (&totempg_mutex);
  782. return (0);
  783. error_destroy:
  784. hdb_handle_destroy (&totempg_groups_instance_database, *handle);
  785. error_exit:
  786. pthread_mutex_unlock (&totempg_mutex);
  787. return (-1);
  788. }
  789. int totempg_groups_join (
  790. totempg_groups_handle handle,
  791. struct totempg_group *groups,
  792. int group_cnt)
  793. {
  794. struct totempg_group_instance *instance;
  795. struct totempg_group *new_groups;
  796. unsigned int res;
  797. pthread_mutex_lock (&totempg_mutex);
  798. res = hdb_handle_get (&totempg_groups_instance_database, handle,
  799. (void *)&instance);
  800. if (res != 0) {
  801. goto error_exit;
  802. }
  803. new_groups = realloc (instance->groups,
  804. sizeof (struct totempg_group) *
  805. (instance->groups_cnt + group_cnt));
  806. if (new_groups == 0) {
  807. res = ENOMEM;
  808. goto error_exit;
  809. }
  810. memcpy (&new_groups[instance->groups_cnt],
  811. groups, group_cnt * sizeof (struct totempg_group));
  812. instance->groups = new_groups;
  813. instance->groups_cnt = instance->groups_cnt = group_cnt;
  814. hdb_handle_put (&totempg_groups_instance_database, handle);
  815. error_exit:
  816. pthread_mutex_unlock (&totempg_mutex);
  817. return (res);
  818. }
  819. int totempg_groups_leave (
  820. totempg_groups_handle handle,
  821. struct totempg_group *groups,
  822. int group_cnt)
  823. {
  824. struct totempg_group_instance *instance;
  825. unsigned int res;
  826. pthread_mutex_lock (&totempg_mutex);
  827. res = hdb_handle_get (&totempg_groups_instance_database, handle,
  828. (void *)&instance);
  829. if (res != 0) {
  830. goto error_exit;
  831. }
  832. hdb_handle_put (&totempg_groups_instance_database, handle);
  833. error_exit:
  834. pthread_mutex_unlock (&totempg_mutex);
  835. return (res);
  836. }
  837. #define MAX_IOVECS_FROM_APP 32
  838. #define MAX_GROUPS_PER_MSG 32
  839. int totempg_groups_mcast_joined (
  840. totempg_groups_handle handle,
  841. struct iovec *iovec,
  842. int iov_len,
  843. int guarantee)
  844. {
  845. struct totempg_group_instance *instance;
  846. unsigned short group_len[MAX_GROUPS_PER_MSG + 1];
  847. struct iovec iovec_mcast[MAX_GROUPS_PER_MSG + 1 + MAX_IOVECS_FROM_APP];
  848. int i;
  849. unsigned int res;
  850. pthread_mutex_lock (&totempg_mutex);
  851. res = hdb_handle_get (&totempg_groups_instance_database, handle,
  852. (void *)&instance);
  853. if (res != 0) {
  854. goto error_exit;
  855. }
  856. /*
  857. * Build group_len structure and the iovec_mcast structure
  858. */
  859. group_len[0] = instance->groups_cnt;
  860. for (i = 0; i < instance->groups_cnt; i++) {
  861. group_len[i + 1] = instance->groups[i].group_len;
  862. iovec_mcast[i + 1].iov_len = instance->groups[i].group_len;
  863. iovec_mcast[i + 1].iov_base = instance->groups[i].group;
  864. }
  865. iovec_mcast[0].iov_len = (instance->groups_cnt + 1) * sizeof (unsigned short);
  866. iovec_mcast[0].iov_base = group_len;
  867. for (i = 0; i < iov_len; i++) {
  868. iovec_mcast[i + instance->groups_cnt + 1].iov_len = iovec[i].iov_len;
  869. iovec_mcast[i + instance->groups_cnt + 1].iov_base = iovec[i].iov_base;
  870. }
  871. res = mcast_msg (iovec_mcast, iov_len + instance->groups_cnt + 1, guarantee);
  872. hdb_handle_put (&totempg_groups_instance_database, handle);
  873. error_exit:
  874. pthread_mutex_unlock (&totempg_mutex);
  875. return (res);
  876. }
  877. int totempg_groups_send_ok_joined (
  878. totempg_groups_handle handle,
  879. struct iovec *iovec,
  880. int iov_len)
  881. {
  882. struct totempg_group_instance *instance;
  883. unsigned int size = 0;
  884. unsigned int i;
  885. unsigned int res;
  886. pthread_mutex_lock (&totempg_mutex);
  887. pthread_mutex_lock (&mcast_msg_mutex);
  888. res = hdb_handle_get (&totempg_groups_instance_database, handle,
  889. (void *)&instance);
  890. if (res != 0) {
  891. goto error_exit;
  892. }
  893. for (i = 0; i < instance->groups_cnt; i++) {
  894. size += instance->groups[i].group_len;
  895. }
  896. for (i = 0; i < iov_len; i++) {
  897. size += iovec[i].iov_len;
  898. }
  899. res = send_ok (size);
  900. hdb_handle_put (&totempg_groups_instance_database, handle);
  901. error_exit:
  902. pthread_mutex_unlock (&mcast_msg_mutex);
  903. pthread_mutex_unlock (&totempg_mutex);
  904. return (res);
  905. }
  906. int totempg_groups_mcast_groups (
  907. totempg_groups_handle handle,
  908. int guarantee,
  909. struct totempg_group *groups,
  910. int groups_cnt,
  911. struct iovec *iovec,
  912. int iov_len)
  913. {
  914. struct totempg_group_instance *instance;
  915. unsigned short group_len[MAX_GROUPS_PER_MSG + 1];
  916. struct iovec iovec_mcast[MAX_GROUPS_PER_MSG + 1 + MAX_IOVECS_FROM_APP];
  917. int i;
  918. unsigned int res;
  919. pthread_mutex_lock (&totempg_mutex);
  920. res = hdb_handle_get (&totempg_groups_instance_database, handle,
  921. (void *)&instance);
  922. if (res != 0) {
  923. goto error_exit;
  924. }
  925. /*
  926. * Build group_len structure and the iovec_mcast structure
  927. */
  928. group_len[0] = groups_cnt;
  929. for (i = 0; i < groups_cnt; i++) {
  930. group_len[i + 1] = groups[i].group_len;
  931. iovec_mcast[i + 1].iov_len = groups[i].group_len;
  932. iovec_mcast[i + 1].iov_base = groups[i].group;
  933. }
  934. iovec_mcast[0].iov_len = (groups_cnt + 1) * sizeof (unsigned short);
  935. iovec_mcast[0].iov_base = group_len;
  936. for (i = 0; i < iov_len; i++) {
  937. iovec_mcast[i + groups_cnt + 1].iov_len = iovec[i].iov_len;
  938. iovec_mcast[i + groups_cnt + 1].iov_base = iovec[i].iov_base;
  939. }
  940. res = mcast_msg (iovec_mcast, iov_len + groups_cnt + 1, guarantee);
  941. hdb_handle_put (&totempg_groups_instance_database, handle);
  942. error_exit:
  943. pthread_mutex_unlock (&totempg_mutex);
  944. return (res);
  945. }
  946. /*
  947. * Returns -1 if error, 0 if can't send, 1 if can send the message
  948. */
  949. int totempg_groups_send_ok_groups (
  950. totempg_groups_handle handle,
  951. struct totempg_group *groups,
  952. int groups_cnt,
  953. struct iovec *iovec,
  954. int iov_len)
  955. {
  956. struct totempg_group_instance *instance;
  957. unsigned int size = 0;
  958. unsigned int i;
  959. unsigned int res;
  960. pthread_mutex_lock (&totempg_mutex);
  961. res = hdb_handle_get (&totempg_groups_instance_database, handle,
  962. (void *)&instance);
  963. if (res != 0) {
  964. goto error_exit;
  965. }
  966. for (i = 0; i < groups_cnt; i++) {
  967. size += groups[i].group_len;
  968. }
  969. for (i = 0; i < iov_len; i++) {
  970. size += iovec[i].iov_len;
  971. }
  972. res = send_ok (size);
  973. hdb_handle_put (&totempg_groups_instance_database, handle);
  974. error_exit:
  975. pthread_mutex_unlock (&totempg_mutex);
  976. return (res);
  977. }
  978. int totempg_ifaces_get (
  979. unsigned int nodeid,
  980. struct totem_ip_address *interfaces,
  981. unsigned int *iface_count)
  982. {
  983. int res;
  984. res = totemmrp_interfaces_get (
  985. nodeid,
  986. interfaces,
  987. iface_count);
  988. return (res);
  989. }
  990. char *totempg_ifaces_print (unsigned int nodeid)
  991. {
  992. static char iface_string[256 * INTERFACE_MAX];
  993. char one_iface[32];
  994. struct totem_ip_address interfaces[INTERFACE_MAX];
  995. unsigned int iface_count;
  996. unsigned int i;
  997. int res;
  998. iface_string[0] = '\0';
  999. res = totempg_ifaces_get (nodeid, interfaces, &iface_count);
  1000. if (res == -1) {
  1001. return ("no interface found for nodeid");
  1002. }
  1003. for (i = 0; i < iface_count; i++) {
  1004. sprintf (one_iface, "r(%d) ip(%s) ",
  1005. i, totemip_print (&interfaces[i]));
  1006. strcat (iface_string, one_iface);
  1007. }
  1008. return (iface_string);
  1009. }