totempg.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136
  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. struct totem_ip_address addr;
  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. struct totem_ip_address *source_addr,
  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. struct totem_ip_address *member_list, int member_list_entries,
  172. struct totem_ip_address *left_list, int left_list_entries,
  173. struct totem_ip_address *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 (struct totem_ip_address *addr)
  192. {
  193. int i;
  194. for (i = 0; i < assembly_list_entries; i++) {
  195. if (totemip_equal(addr, &assembly_list[i]->addr)) {
  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. struct totem_ip_address *member_list, int member_list_entries,
  204. struct totem_ip_address *left_list, int left_list_entries,
  205. struct totem_ip_address *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. struct totem_ip_address *source_addr,
  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. source_addr,
  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. struct totem_ip_address *member_list, int member_list_entries,
  312. struct totem_ip_address *left_list, int left_list_entries,
  313. struct totem_ip_address *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 (totemip_equal(&left_list[i], &assembly_list[j]->addr)) {
  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 (totemip_equal(&member_list[i], &assembly_list[j]->addr)) {
  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. totemip_copy(&assembly_list[assembly_list_entries]->addr,
  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. struct totem_ip_address *source_addr,
  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 (source_addr);
  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(source_addr, &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. struct totem_ip_address *source_addr,
  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. struct totem_ip_address *member_list, int member_list_entries,
  756. struct totem_ip_address *left_list, int left_list_entries,
  757. struct totem_ip_address *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. res = hdb_handle_get (&totempg_groups_instance_database, handle,
  888. (void *)&instance);
  889. if (res != 0) {
  890. goto error_exit;
  891. }
  892. for (i = 0; i < instance->groups_cnt; i++) {
  893. size += instance->groups[i].group_len;
  894. }
  895. for (i = 0; i < iov_len; i++) {
  896. size += iovec[i].iov_len;
  897. }
  898. res = send_ok (size);
  899. hdb_handle_put (&totempg_groups_instance_database, handle);
  900. error_exit:
  901. pthread_mutex_unlock (&totempg_mutex);
  902. return (res);
  903. }
  904. int totempg_groups_mcast_groups (
  905. totempg_groups_handle handle,
  906. int guarantee,
  907. struct totempg_group *groups,
  908. int groups_cnt,
  909. struct iovec *iovec,
  910. int iov_len)
  911. {
  912. struct totempg_group_instance *instance;
  913. unsigned short group_len[MAX_GROUPS_PER_MSG + 1];
  914. struct iovec iovec_mcast[MAX_GROUPS_PER_MSG + 1 + MAX_IOVECS_FROM_APP];
  915. int i;
  916. unsigned int res;
  917. pthread_mutex_lock (&totempg_mutex);
  918. res = hdb_handle_get (&totempg_groups_instance_database, handle,
  919. (void *)&instance);
  920. if (res != 0) {
  921. goto error_exit;
  922. }
  923. /*
  924. * Build group_len structure and the iovec_mcast structure
  925. */
  926. group_len[0] = groups_cnt;
  927. for (i = 0; i < groups_cnt; i++) {
  928. group_len[i + 1] = groups[i].group_len;
  929. iovec_mcast[i + 1].iov_len = groups[i].group_len;
  930. iovec_mcast[i + 1].iov_base = groups[i].group;
  931. }
  932. iovec_mcast[0].iov_len = (groups_cnt + 1) * sizeof (unsigned short);
  933. iovec_mcast[0].iov_base = group_len;
  934. for (i = 0; i < iov_len; i++) {
  935. iovec_mcast[i + groups_cnt + 1].iov_len = iovec[i].iov_len;
  936. iovec_mcast[i + groups_cnt + 1].iov_base = iovec[i].iov_base;
  937. }
  938. res = mcast_msg (iovec_mcast, iov_len + groups_cnt + 1, guarantee);
  939. hdb_handle_put (&totempg_groups_instance_database, handle);
  940. error_exit:
  941. pthread_mutex_unlock (&totempg_mutex);
  942. return (res);
  943. }
  944. /*
  945. * Returns -1 if error, 0 if can't send, 1 if can send the message
  946. */
  947. int totempg_groups_send_ok_groups (
  948. totempg_groups_handle handle,
  949. struct totempg_group *groups,
  950. int groups_cnt,
  951. struct iovec *iovec,
  952. int iov_len)
  953. {
  954. struct totempg_group_instance *instance;
  955. unsigned int size = 0;
  956. unsigned int i;
  957. unsigned int res;
  958. pthread_mutex_lock (&totempg_mutex);
  959. res = hdb_handle_get (&totempg_groups_instance_database, handle,
  960. (void *)&instance);
  961. if (res != 0) {
  962. goto error_exit;
  963. }
  964. for (i = 0; i < groups_cnt; i++) {
  965. size += groups[i].group_len;
  966. }
  967. for (i = 0; i < iov_len; i++) {
  968. size += iovec[i].iov_len;
  969. }
  970. res = send_ok (size);
  971. hdb_handle_put (&totempg_groups_instance_database, handle);
  972. error_exit:
  973. pthread_mutex_unlock (&totempg_mutex);
  974. return (res);
  975. }