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