totempg.c 31 KB

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