totempg.c 32 KB

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