totempg.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322
  1. /*
  2. * Copyright (c) 2003-2005 MontaVista Software, Inc.
  3. * Copyright (c) 2005 OSDL.
  4. * Copyright (c) 2006-2009 Red Hat, Inc.
  5. *
  6. * All rights reserved.
  7. *
  8. * Author: Steven Dake (sdake@redhat.com)
  9. * Author: 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 <config.h>
  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 <limits.h>
  91. #include <corosync/swab.h>
  92. #include <corosync/hdb.h>
  93. #include <corosync/list.h>
  94. #include <corosync/totem/coropoll.h>
  95. #include <corosync/totem/totempg.h>
  96. #include "totemmrp.h"
  97. #include "totemsrp.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. static int totempg_reserved = 0;
  137. /*
  138. * Function and data used to log messages
  139. */
  140. static int totempg_log_level_security;
  141. static int totempg_log_level_error;
  142. static int totempg_log_level_warning;
  143. static int totempg_log_level_notice;
  144. static int totempg_log_level_debug;
  145. static int totempg_subsys_id;
  146. static void (*totempg_log_printf) (int subsys_id, const char *function,
  147. const char *file, int line, unsigned int level, unsigned int rec_ident,
  148. const char *format, ...) __attribute__((format(printf, 7, 8)));
  149. struct totem_config *totempg_totem_config;
  150. enum throw_away_mode {
  151. THROW_AWAY_INACTIVE,
  152. THROW_AWAY_ACTIVE
  153. };
  154. struct assembly {
  155. unsigned int nodeid;
  156. unsigned char data[MESSAGE_SIZE_MAX];
  157. int index;
  158. unsigned char last_frag_num;
  159. enum throw_away_mode throw_away_mode;
  160. struct list_head list;
  161. };
  162. static void assembly_deref (struct assembly *assembly);
  163. static int callback_token_received_fn (enum totem_callback_token_type type,
  164. const void *data);
  165. DECLARE_LIST_INIT(assembly_list_inuse);
  166. DECLARE_LIST_INIT(assembly_list_free);
  167. /*
  168. * Staging buffer for packed messages. Messages are staged in this buffer
  169. * before sending. Multiple messages may fit which cuts down on the
  170. * number of mcasts sent. If a message doesn't completely fit, then
  171. * the mcast header has a fragment bit set that says that there are more
  172. * data to follow. fragment_size is an index into the buffer. It indicates
  173. * the size of message data and where to place new message data.
  174. * fragment_contuation indicates whether the first packed message in
  175. * the buffer is a continuation of a previously packed fragment.
  176. */
  177. static unsigned char *fragmentation_data;
  178. static int fragment_size = 0;
  179. static int fragment_continuation = 0;
  180. static struct iovec iov_delv;
  181. static unsigned int totempg_max_handle = 0;
  182. struct totempg_group_instance {
  183. void (*deliver_fn) (
  184. unsigned int nodeid,
  185. const void *msg,
  186. unsigned int msg_len,
  187. int endian_conversion_required);
  188. void (*confchg_fn) (
  189. enum totem_configuration_type configuration_type,
  190. const unsigned int *member_list, size_t member_list_entries,
  191. const unsigned int *left_list, size_t left_list_entries,
  192. const unsigned int *joined_list, size_t joined_list_entries,
  193. const struct memb_ring_id *ring_id);
  194. struct totempg_group *groups;
  195. int groups_cnt;
  196. };
  197. DECLARE_HDB_DATABASE (totempg_groups_instance_database,NULL);
  198. static unsigned char next_fragment = 1;
  199. static pthread_mutex_t totempg_mutex = PTHREAD_MUTEX_INITIALIZER;
  200. static pthread_mutex_t callback_token_mutex = PTHREAD_MUTEX_INITIALIZER;
  201. static pthread_mutex_t mcast_msg_mutex = PTHREAD_MUTEX_INITIALIZER;
  202. #define RECID_LOG UINT_MAX - 1
  203. #define log_printf(level, format, args...) \
  204. do { \
  205. totempg_log_printf (totempg_subsys_id, __FUNCTION__, \
  206. __FILE__, __LINE__, level, RECID_LOG, format, ##args); \
  207. } while (0);
  208. static int msg_count_send_ok (int msg_count);
  209. static int byte_count_send_ok (int byte_count);
  210. static struct assembly *assembly_ref (unsigned int nodeid)
  211. {
  212. struct assembly *assembly;
  213. struct list_head *list;
  214. /*
  215. * Search inuse list for node id and return assembly buffer if found
  216. */
  217. for (list = assembly_list_inuse.next;
  218. list != &assembly_list_inuse;
  219. list = list->next) {
  220. assembly = list_entry (list, struct assembly, list);
  221. if (nodeid == assembly->nodeid) {
  222. return (assembly);
  223. }
  224. }
  225. /*
  226. * Nothing found in inuse list get one from free list if available
  227. */
  228. if (list_empty (&assembly_list_free) == 0) {
  229. assembly = list_entry (assembly_list_free.next, struct assembly, list);
  230. list_del (&assembly->list);
  231. list_add (&assembly->list, &assembly_list_inuse);
  232. assembly->nodeid = nodeid;
  233. assembly->index = 0;
  234. assembly->last_frag_num = 0;
  235. assembly->throw_away_mode = THROW_AWAY_INACTIVE;
  236. return (assembly);
  237. }
  238. /*
  239. * Nothing available in inuse or free list, so allocate a new one
  240. */
  241. assembly = malloc (sizeof (struct assembly));
  242. /*
  243. * TODO handle memory allocation failure here
  244. */
  245. assert (assembly);
  246. assembly->nodeid = nodeid;
  247. assembly->data[0] = 0;
  248. assembly->index = 0;
  249. assembly->last_frag_num = 0;
  250. assembly->throw_away_mode = THROW_AWAY_INACTIVE;
  251. list_init (&assembly->list);
  252. list_add (&assembly->list, &assembly_list_inuse);
  253. return (assembly);
  254. }
  255. static void assembly_deref (struct assembly *assembly)
  256. {
  257. list_del (&assembly->list);
  258. list_add (&assembly->list, &assembly_list_free);
  259. }
  260. static inline void app_confchg_fn (
  261. enum totem_configuration_type configuration_type,
  262. const unsigned int *member_list, size_t member_list_entries,
  263. const unsigned int *left_list, size_t left_list_entries,
  264. const unsigned int *joined_list, size_t joined_list_entries,
  265. const struct memb_ring_id *ring_id)
  266. {
  267. int i;
  268. struct totempg_group_instance *instance;
  269. unsigned int res;
  270. for (i = 0; i <= totempg_max_handle; i++) {
  271. res = hdb_handle_get (&totempg_groups_instance_database,
  272. hdb_nocheck_convert (i), (void *)&instance);
  273. if (res == 0) {
  274. if (instance->confchg_fn) {
  275. instance->confchg_fn (
  276. configuration_type,
  277. member_list,
  278. member_list_entries,
  279. left_list,
  280. left_list_entries,
  281. joined_list,
  282. joined_list_entries,
  283. ring_id);
  284. }
  285. hdb_handle_put (&totempg_groups_instance_database,
  286. hdb_nocheck_convert (i));
  287. }
  288. }
  289. }
  290. static inline void group_endian_convert (
  291. void *msg,
  292. int msg_len)
  293. {
  294. unsigned short *group_len;
  295. int i;
  296. char *aligned_msg;
  297. /*
  298. * Align data structure for sparc and ia64
  299. */
  300. if ((size_t)msg % 4 != 0) {
  301. aligned_msg = alloca(msg_len);
  302. memcpy(aligned_msg, msg, msg_len);
  303. } else {
  304. aligned_msg = msg;
  305. }
  306. group_len = (unsigned short *)aligned_msg;
  307. group_len[0] = swab16(group_len[0]);
  308. for (i = 1; i < group_len[0] + 1; i++) {
  309. group_len[i] = swab16(group_len[i]);
  310. }
  311. if (aligned_msg != msg) {
  312. memcpy(msg, aligned_msg, msg_len);
  313. }
  314. }
  315. static inline int group_matches (
  316. struct iovec *iovec,
  317. unsigned int iov_len,
  318. struct totempg_group *groups_b,
  319. unsigned int group_b_cnt,
  320. unsigned int *adjust_iovec)
  321. {
  322. unsigned short *group_len;
  323. char *group_name;
  324. int i;
  325. int j;
  326. struct iovec iovec_aligned = { NULL, 0 };
  327. assert (iov_len == 1);
  328. /*
  329. * Align data structure for sparc and ia64
  330. */
  331. if ((size_t)iovec->iov_base % 4 != 0) {
  332. iovec_aligned.iov_base = alloca(iovec->iov_len);
  333. memcpy(iovec_aligned.iov_base, iovec->iov_base, iovec->iov_len);
  334. iovec_aligned.iov_len = iovec->iov_len;
  335. iovec = &iovec_aligned;
  336. }
  337. group_len = (unsigned short *)iovec->iov_base;
  338. group_name = ((char *)iovec->iov_base) +
  339. sizeof (unsigned short) * (group_len[0] + 1);
  340. /*
  341. * Calculate amount to adjust the iovec by before delivering to app
  342. */
  343. *adjust_iovec = sizeof (unsigned short) * (group_len[0] + 1);
  344. for (i = 1; i < group_len[0] + 1; i++) {
  345. *adjust_iovec += group_len[i];
  346. }
  347. /*
  348. * Determine if this message should be delivered to this instance
  349. */
  350. for (i = 1; i < group_len[0] + 1; i++) {
  351. for (j = 0; j < group_b_cnt; j++) {
  352. if ((group_len[i] == groups_b[j].group_len) &&
  353. (memcmp (groups_b[j].group, group_name, group_len[i]) == 0)) {
  354. return (1);
  355. }
  356. }
  357. group_name += group_len[i];
  358. }
  359. return (0);
  360. }
  361. static inline void app_deliver_fn (
  362. unsigned int nodeid,
  363. void *msg,
  364. unsigned int msg_len,
  365. int endian_conversion_required)
  366. {
  367. int i;
  368. struct totempg_group_instance *instance;
  369. struct iovec stripped_iovec;
  370. unsigned int adjust_iovec;
  371. unsigned int res;
  372. struct iovec *iovec;
  373. struct iovec aligned_iovec = { NULL, 0 };
  374. if (endian_conversion_required) {
  375. group_endian_convert (msg, msg_len);
  376. }
  377. /*
  378. * TODO This function needs to be rewritten for proper alignment to avoid 3+ memory copies
  379. */
  380. /*
  381. * Align data structure for sparc and ia64
  382. */
  383. aligned_iovec.iov_base = alloca(msg_len);
  384. aligned_iovec.iov_len = msg_len;
  385. memcpy(aligned_iovec.iov_base, msg, msg_len);
  386. iovec = &aligned_iovec;
  387. for (i = 0; i <= totempg_max_handle; i++) {
  388. res = hdb_handle_get (&totempg_groups_instance_database,
  389. hdb_nocheck_convert (i), (void *)&instance);
  390. if (res == 0) {
  391. if (group_matches (iovec, 1, instance->groups, instance->groups_cnt, &adjust_iovec)) {
  392. stripped_iovec.iov_len = iovec->iov_len - adjust_iovec;
  393. stripped_iovec.iov_base = (char *)iovec->iov_base + adjust_iovec;
  394. /*
  395. * Align data structure for sparc and ia64
  396. */
  397. if ((char *)iovec->iov_base + adjust_iovec % 4 != 0) {
  398. /*
  399. * Deal with misalignment
  400. */
  401. stripped_iovec.iov_base =
  402. alloca (stripped_iovec.iov_len);
  403. memcpy (stripped_iovec.iov_base,
  404. (char *)iovec->iov_base + adjust_iovec,
  405. stripped_iovec.iov_len);
  406. }
  407. instance->deliver_fn (
  408. nodeid,
  409. stripped_iovec.iov_base,
  410. stripped_iovec.iov_len,
  411. endian_conversion_required);
  412. }
  413. hdb_handle_put (&totempg_groups_instance_database, hdb_nocheck_convert(i));
  414. }
  415. }
  416. }
  417. static void totempg_confchg_fn (
  418. enum totem_configuration_type configuration_type,
  419. const unsigned int *member_list, size_t member_list_entries,
  420. const unsigned int *left_list, size_t left_list_entries,
  421. const unsigned int *joined_list, size_t joined_list_entries,
  422. const struct memb_ring_id *ring_id)
  423. {
  424. // TODO optimize this
  425. app_confchg_fn (configuration_type,
  426. member_list, member_list_entries,
  427. left_list, left_list_entries,
  428. joined_list, joined_list_entries,
  429. ring_id);
  430. }
  431. static void totempg_deliver_fn (
  432. unsigned int nodeid,
  433. const void *msg,
  434. unsigned int msg_len,
  435. int endian_conversion_required)
  436. {
  437. struct totempg_mcast *mcast;
  438. unsigned short *msg_lens;
  439. int i;
  440. struct assembly *assembly;
  441. char header[FRAME_SIZE_MAX];
  442. int msg_count;
  443. int continuation;
  444. int start;
  445. const char *data;
  446. int datasize;
  447. assembly = assembly_ref (nodeid);
  448. assert (assembly);
  449. /*
  450. * Assemble the header into one block of data and
  451. * assemble the packet contents into one block of data to simplify delivery
  452. */
  453. mcast = (struct totempg_mcast *)msg;
  454. if (endian_conversion_required) {
  455. mcast->msg_count = swab16 (mcast->msg_count);
  456. }
  457. msg_count = mcast->msg_count;
  458. datasize = sizeof (struct totempg_mcast) +
  459. msg_count * sizeof (unsigned short);
  460. memcpy (header, msg, datasize);
  461. data = msg;
  462. msg_lens = (unsigned short *) (header + sizeof (struct totempg_mcast));
  463. if (endian_conversion_required) {
  464. for (i = 0; i < mcast->msg_count; i++) {
  465. msg_lens[i] = swab16 (msg_lens[i]);
  466. }
  467. }
  468. memcpy (&assembly->data[assembly->index], &data[datasize],
  469. msg_len - datasize);
  470. /*
  471. * If the last message in the buffer is a fragment, then we
  472. * can't deliver it. We'll first deliver the full messages
  473. * then adjust the assembly buffer so we can add the rest of the
  474. * fragment when it arrives.
  475. */
  476. msg_count = mcast->fragmented ? mcast->msg_count - 1 : mcast->msg_count;
  477. continuation = mcast->continuation;
  478. iov_delv.iov_base = &assembly->data[0];
  479. iov_delv.iov_len = assembly->index + msg_lens[0];
  480. /*
  481. * Make sure that if this message is a continuation, that it
  482. * matches the sequence number of the previous fragment.
  483. * Also, if the first packed message is a continuation
  484. * of a previous message, but the assembly buffer
  485. * is empty, then we need to discard it since we can't
  486. * assemble a complete message. Likewise, if this message isn't a
  487. * continuation and the assembly buffer is empty, we have to discard
  488. * the continued message.
  489. */
  490. start = 0;
  491. if (assembly->throw_away_mode == THROW_AWAY_ACTIVE) {
  492. /* Throw away the first msg block */
  493. if (mcast->fragmented == 0 || mcast->fragmented == 1) {
  494. assembly->throw_away_mode = THROW_AWAY_INACTIVE;
  495. assembly->index += msg_lens[0];
  496. iov_delv.iov_base = &assembly->data[assembly->index];
  497. iov_delv.iov_len = msg_lens[1];
  498. start = 1;
  499. }
  500. } else
  501. if (assembly->throw_away_mode == THROW_AWAY_INACTIVE) {
  502. if (continuation == assembly->last_frag_num) {
  503. assembly->last_frag_num = mcast->fragmented;
  504. for (i = start; i < msg_count; i++) {
  505. app_deliver_fn(nodeid, iov_delv.iov_base, iov_delv.iov_len,
  506. endian_conversion_required);
  507. assembly->index += msg_lens[i];
  508. iov_delv.iov_base = &assembly->data[assembly->index];
  509. if (i < (msg_count - 1)) {
  510. iov_delv.iov_len = msg_lens[i + 1];
  511. }
  512. }
  513. } else {
  514. assembly->throw_away_mode = THROW_AWAY_ACTIVE;
  515. }
  516. }
  517. if (mcast->fragmented == 0) {
  518. /*
  519. * End of messages, dereference assembly struct
  520. */
  521. assembly->last_frag_num = 0;
  522. assembly->index = 0;
  523. assembly_deref (assembly);
  524. } else {
  525. /*
  526. * Message is fragmented, keep around assembly list
  527. */
  528. if (mcast->msg_count > 1) {
  529. memmove (&assembly->data[0],
  530. &assembly->data[assembly->index],
  531. msg_lens[msg_count]);
  532. assembly->index = 0;
  533. }
  534. assembly->index += msg_lens[msg_count];
  535. }
  536. }
  537. /*
  538. * Totem Process Group Abstraction
  539. * depends on poll abstraction, POSIX, IPV4
  540. */
  541. void *callback_token_received_handle;
  542. int callback_token_received_fn (enum totem_callback_token_type type,
  543. const void *data)
  544. {
  545. struct totempg_mcast mcast;
  546. struct iovec iovecs[3];
  547. int res;
  548. pthread_mutex_lock (&mcast_msg_mutex);
  549. if (mcast_packed_msg_count == 0) {
  550. pthread_mutex_unlock (&mcast_msg_mutex);
  551. return (0);
  552. }
  553. if (totemmrp_avail() == 0) {
  554. pthread_mutex_unlock (&mcast_msg_mutex);
  555. return (0);
  556. }
  557. mcast.fragmented = 0;
  558. /*
  559. * Was the first message in this buffer a continuation of a
  560. * fragmented message?
  561. */
  562. mcast.continuation = fragment_continuation;
  563. fragment_continuation = 0;
  564. mcast.msg_count = mcast_packed_msg_count;
  565. iovecs[0].iov_base = &mcast;
  566. iovecs[0].iov_len = sizeof (struct totempg_mcast);
  567. iovecs[1].iov_base = mcast_packed_msg_lens;
  568. iovecs[1].iov_len = mcast_packed_msg_count * sizeof (unsigned short);
  569. iovecs[2].iov_base = &fragmentation_data[0];
  570. iovecs[2].iov_len = fragment_size;
  571. res = totemmrp_mcast (iovecs, 3, 0);
  572. mcast_packed_msg_count = 0;
  573. fragment_size = 0;
  574. pthread_mutex_unlock (&mcast_msg_mutex);
  575. return (0);
  576. }
  577. /*
  578. * Initialize the totem process group abstraction
  579. */
  580. int totempg_initialize (
  581. hdb_handle_t poll_handle,
  582. struct totem_config *totem_config)
  583. {
  584. int res;
  585. totempg_totem_config = totem_config;
  586. totempg_log_level_security = totem_config->totem_logging_configuration.log_level_security;
  587. totempg_log_level_error = totem_config->totem_logging_configuration.log_level_error;
  588. totempg_log_level_warning = totem_config->totem_logging_configuration.log_level_warning;
  589. totempg_log_level_notice = totem_config->totem_logging_configuration.log_level_notice;
  590. totempg_log_level_debug = totem_config->totem_logging_configuration.log_level_debug;
  591. totempg_log_printf = totem_config->totem_logging_configuration.log_printf;
  592. totempg_subsys_id = totem_config->totem_logging_configuration.log_subsys_id;
  593. fragmentation_data = malloc (TOTEMPG_PACKET_SIZE);
  594. if (fragmentation_data == 0) {
  595. return (-1);
  596. }
  597. res = totemmrp_initialize (
  598. poll_handle,
  599. totem_config,
  600. totempg_deliver_fn,
  601. totempg_confchg_fn);
  602. totemmrp_callback_token_create (
  603. &callback_token_received_handle,
  604. TOTEM_CALLBACK_TOKEN_RECEIVED,
  605. 0,
  606. callback_token_received_fn,
  607. 0);
  608. totemsrp_net_mtu_adjust (totem_config);
  609. return (res);
  610. }
  611. void totempg_finalize (void)
  612. {
  613. pthread_mutex_lock (&totempg_mutex);
  614. totemmrp_finalize ();
  615. pthread_mutex_unlock (&totempg_mutex);
  616. }
  617. /*
  618. * Multicast a message
  619. */
  620. static int mcast_msg (
  621. struct iovec *iovec_in,
  622. unsigned int iov_len,
  623. int guarantee)
  624. {
  625. int res = 0;
  626. struct totempg_mcast mcast;
  627. struct iovec iovecs[3];
  628. struct iovec iovec[64];
  629. int i;
  630. int dest, src;
  631. int max_packet_size = 0;
  632. int copy_len = 0;
  633. int copy_base = 0;
  634. int total_size = 0;
  635. pthread_mutex_lock (&mcast_msg_mutex);
  636. totemmrp_new_msg_signal ();
  637. /*
  638. * Remove zero length iovectors from the list
  639. */
  640. assert (iov_len < 64);
  641. for (dest = 0, src = 0; src < iov_len; src++) {
  642. if (iovec_in[src].iov_len) {
  643. memcpy (&iovec[dest++], &iovec_in[src],
  644. sizeof (struct iovec));
  645. }
  646. }
  647. iov_len = dest;
  648. max_packet_size = TOTEMPG_PACKET_SIZE -
  649. (sizeof (unsigned short) * (mcast_packed_msg_count + 1));
  650. mcast_packed_msg_lens[mcast_packed_msg_count] = 0;
  651. /*
  652. * Check if we would overwrite new message queue
  653. */
  654. for (i = 0; i < iov_len; i++) {
  655. total_size += iovec[i].iov_len;
  656. }
  657. if (byte_count_send_ok (total_size + sizeof(unsigned short) *
  658. (mcast_packed_msg_count+1)) == 0) {
  659. pthread_mutex_unlock (&mcast_msg_mutex);
  660. return(-1);
  661. }
  662. for (i = 0; i < iov_len; ) {
  663. mcast.fragmented = 0;
  664. mcast.continuation = fragment_continuation;
  665. copy_len = iovec[i].iov_len - copy_base;
  666. /*
  667. * If it all fits with room left over, copy it in.
  668. * We need to leave at least sizeof(short) + 1 bytes in the
  669. * fragment_buffer on exit so that max_packet_size + fragment_size
  670. * doesn't exceed the size of the fragment_buffer on the next call.
  671. */
  672. if ((copy_len + fragment_size) <
  673. (max_packet_size - sizeof (unsigned short))) {
  674. memcpy (&fragmentation_data[fragment_size],
  675. (char *)iovec[i].iov_base + copy_base, copy_len);
  676. fragment_size += copy_len;
  677. mcast_packed_msg_lens[mcast_packed_msg_count] += copy_len;
  678. next_fragment = 1;
  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. unsigned char *data_ptr;
  688. copy_len = min(copy_len, max_packet_size - fragment_size);
  689. if( copy_len == max_packet_size )
  690. data_ptr = (unsigned char *)iovec[i].iov_base + copy_base;
  691. else {
  692. data_ptr = fragmentation_data;
  693. memcpy (&fragmentation_data[fragment_size],
  694. (unsigned char *)iovec[i].iov_base + copy_base, copy_len);
  695. }
  696. memcpy (&fragmentation_data[fragment_size],
  697. (unsigned char *)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 = &mcast;
  721. iovecs[0].iov_len = sizeof(struct totempg_mcast);
  722. iovecs[1].iov_base = 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 msg_count_send_ok (
  766. int msg_count)
  767. {
  768. int avail = 0;
  769. avail = totemmrp_avail () - totempg_reserved - 1;
  770. return (avail > msg_count);
  771. }
  772. static int byte_count_send_ok (
  773. int byte_count)
  774. {
  775. unsigned int msg_count = 0;
  776. int avail = 0;
  777. avail = totemmrp_avail () - 1;
  778. msg_count = (byte_count / (totempg_totem_config->net_mtu - 25)) + 1;
  779. return (avail > msg_count);
  780. }
  781. static int send_reserve (
  782. int msg_size)
  783. {
  784. unsigned int msg_count = 0;
  785. msg_count = (msg_size / (totempg_totem_config->net_mtu - 25)) + 1;
  786. totempg_reserved += msg_count;
  787. return (msg_count);
  788. }
  789. static void send_release (
  790. int msg_count)
  791. {
  792. totempg_reserved -= msg_count;
  793. }
  794. int totempg_callback_token_create (
  795. void **handle_out,
  796. enum totem_callback_token_type type,
  797. int delete,
  798. int (*callback_fn) (enum totem_callback_token_type type, const void *),
  799. const void *data)
  800. {
  801. unsigned int res;
  802. pthread_mutex_lock (&callback_token_mutex);
  803. res = totemmrp_callback_token_create (handle_out, type, delete,
  804. callback_fn, data);
  805. pthread_mutex_unlock (&callback_token_mutex);
  806. return (res);
  807. }
  808. void totempg_callback_token_destroy (
  809. void *handle_out)
  810. {
  811. pthread_mutex_lock (&callback_token_mutex);
  812. totemmrp_callback_token_destroy (handle_out);
  813. pthread_mutex_unlock (&callback_token_mutex);
  814. }
  815. /*
  816. * vi: set autoindent tabstop=4 shiftwidth=4 :
  817. */
  818. int totempg_groups_initialize (
  819. hdb_handle_t *handle,
  820. void (*deliver_fn) (
  821. unsigned int nodeid,
  822. const void *msg,
  823. unsigned int msg_len,
  824. int endian_conversion_required),
  825. void (*confchg_fn) (
  826. enum totem_configuration_type configuration_type,
  827. const unsigned int *member_list, size_t member_list_entries,
  828. const unsigned int *left_list, size_t left_list_entries,
  829. const unsigned int *joined_list, size_t joined_list_entries,
  830. const struct memb_ring_id *ring_id))
  831. {
  832. struct totempg_group_instance *instance;
  833. unsigned int res;
  834. pthread_mutex_lock (&totempg_mutex);
  835. res = hdb_handle_create (&totempg_groups_instance_database,
  836. sizeof (struct totempg_group_instance), handle);
  837. if (res != 0) {
  838. goto error_exit;
  839. }
  840. if (*handle > totempg_max_handle) {
  841. totempg_max_handle = *handle;
  842. }
  843. res = hdb_handle_get (&totempg_groups_instance_database, *handle,
  844. (void *)&instance);
  845. if (res != 0) {
  846. goto error_destroy;
  847. }
  848. instance->deliver_fn = deliver_fn;
  849. instance->confchg_fn = confchg_fn;
  850. instance->groups = 0;
  851. instance->groups_cnt = 0;
  852. hdb_handle_put (&totempg_groups_instance_database, *handle);
  853. pthread_mutex_unlock (&totempg_mutex);
  854. return (0);
  855. error_destroy:
  856. hdb_handle_destroy (&totempg_groups_instance_database, *handle);
  857. error_exit:
  858. pthread_mutex_unlock (&totempg_mutex);
  859. return (-1);
  860. }
  861. int totempg_groups_join (
  862. hdb_handle_t handle,
  863. const struct totempg_group *groups,
  864. size_t group_cnt)
  865. {
  866. struct totempg_group_instance *instance;
  867. struct totempg_group *new_groups;
  868. unsigned int res;
  869. pthread_mutex_lock (&totempg_mutex);
  870. res = hdb_handle_get (&totempg_groups_instance_database, handle,
  871. (void *)&instance);
  872. if (res != 0) {
  873. goto error_exit;
  874. }
  875. new_groups = realloc (instance->groups,
  876. sizeof (struct totempg_group) *
  877. (instance->groups_cnt + group_cnt));
  878. if (new_groups == 0) {
  879. res = ENOMEM;
  880. goto error_exit;
  881. }
  882. memcpy (&new_groups[instance->groups_cnt],
  883. groups, group_cnt * sizeof (struct totempg_group));
  884. instance->groups = new_groups;
  885. instance->groups_cnt += group_cnt;
  886. hdb_handle_put (&totempg_groups_instance_database, handle);
  887. error_exit:
  888. pthread_mutex_unlock (&totempg_mutex);
  889. return (res);
  890. }
  891. int totempg_groups_leave (
  892. hdb_handle_t handle,
  893. const struct totempg_group *groups,
  894. size_t group_cnt)
  895. {
  896. struct totempg_group_instance *instance;
  897. unsigned int res;
  898. pthread_mutex_lock (&totempg_mutex);
  899. res = hdb_handle_get (&totempg_groups_instance_database, handle,
  900. (void *)&instance);
  901. if (res != 0) {
  902. goto error_exit;
  903. }
  904. hdb_handle_put (&totempg_groups_instance_database, handle);
  905. error_exit:
  906. pthread_mutex_unlock (&totempg_mutex);
  907. return (res);
  908. }
  909. #define MAX_IOVECS_FROM_APP 32
  910. #define MAX_GROUPS_PER_MSG 32
  911. int totempg_groups_mcast_joined (
  912. hdb_handle_t handle,
  913. const struct iovec *iovec,
  914. unsigned int iov_len,
  915. int guarantee)
  916. {
  917. struct totempg_group_instance *instance;
  918. unsigned short group_len[MAX_GROUPS_PER_MSG + 1];
  919. struct iovec iovec_mcast[MAX_GROUPS_PER_MSG + 1 + MAX_IOVECS_FROM_APP];
  920. int i;
  921. unsigned int res;
  922. pthread_mutex_lock (&totempg_mutex);
  923. res = hdb_handle_get (&totempg_groups_instance_database, handle,
  924. (void *)&instance);
  925. if (res != 0) {
  926. goto error_exit;
  927. }
  928. /*
  929. * Build group_len structure and the iovec_mcast structure
  930. */
  931. group_len[0] = instance->groups_cnt;
  932. for (i = 0; i < instance->groups_cnt; i++) {
  933. group_len[i + 1] = instance->groups[i].group_len;
  934. iovec_mcast[i + 1].iov_len = instance->groups[i].group_len;
  935. iovec_mcast[i + 1].iov_base = (void *) instance->groups[i].group;
  936. }
  937. iovec_mcast[0].iov_len = (instance->groups_cnt + 1) * sizeof (unsigned short);
  938. iovec_mcast[0].iov_base = group_len;
  939. for (i = 0; i < iov_len; i++) {
  940. iovec_mcast[i + instance->groups_cnt + 1].iov_len = iovec[i].iov_len;
  941. iovec_mcast[i + instance->groups_cnt + 1].iov_base = iovec[i].iov_base;
  942. }
  943. res = mcast_msg (iovec_mcast, iov_len + instance->groups_cnt + 1, guarantee);
  944. hdb_handle_put (&totempg_groups_instance_database, handle);
  945. error_exit:
  946. pthread_mutex_unlock (&totempg_mutex);
  947. return (res);
  948. }
  949. int totempg_groups_joined_reserve (
  950. hdb_handle_t handle,
  951. const struct iovec *iovec,
  952. unsigned int iov_len)
  953. {
  954. struct totempg_group_instance *instance;
  955. unsigned int size = 0;
  956. unsigned int i;
  957. unsigned int res;
  958. unsigned int reserved = 0;
  959. pthread_mutex_lock (&totempg_mutex);
  960. pthread_mutex_lock (&mcast_msg_mutex);
  961. res = hdb_handle_get (&totempg_groups_instance_database, handle,
  962. (void *)&instance);
  963. if (res != 0) {
  964. goto error_exit;
  965. }
  966. for (i = 0; i < instance->groups_cnt; i++) {
  967. size += instance->groups[i].group_len;
  968. }
  969. for (i = 0; i < iov_len; i++) {
  970. size += iovec[i].iov_len;
  971. }
  972. reserved = send_reserve (size);
  973. if (msg_count_send_ok (reserved) == 0) {
  974. send_release (reserved);
  975. reserved = 0;
  976. }
  977. hdb_handle_put (&totempg_groups_instance_database, handle);
  978. error_exit:
  979. pthread_mutex_unlock (&mcast_msg_mutex);
  980. pthread_mutex_unlock (&totempg_mutex);
  981. return (reserved);
  982. }
  983. int totempg_groups_joined_release (int msg_count)
  984. {
  985. pthread_mutex_lock (&totempg_mutex);
  986. pthread_mutex_lock (&mcast_msg_mutex);
  987. send_release (msg_count);
  988. pthread_mutex_unlock (&mcast_msg_mutex);
  989. pthread_mutex_unlock (&totempg_mutex);
  990. return 0;
  991. }
  992. int totempg_groups_mcast_groups (
  993. hdb_handle_t handle,
  994. int guarantee,
  995. const struct totempg_group *groups,
  996. size_t groups_cnt,
  997. const struct iovec *iovec,
  998. unsigned int iov_len)
  999. {
  1000. struct totempg_group_instance *instance;
  1001. unsigned short group_len[MAX_GROUPS_PER_MSG + 1];
  1002. struct iovec iovec_mcast[MAX_GROUPS_PER_MSG + 1 + MAX_IOVECS_FROM_APP];
  1003. int i;
  1004. unsigned int res;
  1005. pthread_mutex_lock (&totempg_mutex);
  1006. res = hdb_handle_get (&totempg_groups_instance_database, handle,
  1007. (void *)&instance);
  1008. if (res != 0) {
  1009. goto error_exit;
  1010. }
  1011. /*
  1012. * Build group_len structure and the iovec_mcast structure
  1013. */
  1014. group_len[0] = groups_cnt;
  1015. for (i = 0; i < groups_cnt; i++) {
  1016. group_len[i + 1] = groups[i].group_len;
  1017. iovec_mcast[i + 1].iov_len = groups[i].group_len;
  1018. iovec_mcast[i + 1].iov_base = (void *) groups[i].group;
  1019. }
  1020. iovec_mcast[0].iov_len = (groups_cnt + 1) * sizeof (unsigned short);
  1021. iovec_mcast[0].iov_base = group_len;
  1022. for (i = 0; i < iov_len; i++) {
  1023. iovec_mcast[i + groups_cnt + 1].iov_len = iovec[i].iov_len;
  1024. iovec_mcast[i + groups_cnt + 1].iov_base = iovec[i].iov_base;
  1025. }
  1026. res = mcast_msg (iovec_mcast, iov_len + groups_cnt + 1, guarantee);
  1027. hdb_handle_put (&totempg_groups_instance_database, handle);
  1028. error_exit:
  1029. pthread_mutex_unlock (&totempg_mutex);
  1030. return (res);
  1031. }
  1032. /*
  1033. * Returns -1 if error, 0 if can't send, 1 if can send the message
  1034. */
  1035. int totempg_groups_send_ok_groups (
  1036. hdb_handle_t handle,
  1037. const struct totempg_group *groups,
  1038. size_t groups_cnt,
  1039. const struct iovec *iovec,
  1040. unsigned int iov_len)
  1041. {
  1042. struct totempg_group_instance *instance;
  1043. unsigned int size = 0;
  1044. unsigned int i;
  1045. unsigned int res;
  1046. pthread_mutex_lock (&totempg_mutex);
  1047. res = hdb_handle_get (&totempg_groups_instance_database, handle,
  1048. (void *)&instance);
  1049. if (res != 0) {
  1050. goto error_exit;
  1051. }
  1052. for (i = 0; i < groups_cnt; i++) {
  1053. size += groups[i].group_len;
  1054. }
  1055. for (i = 0; i < iov_len; i++) {
  1056. size += iovec[i].iov_len;
  1057. }
  1058. res = msg_count_send_ok (size);
  1059. hdb_handle_put (&totempg_groups_instance_database, handle);
  1060. error_exit:
  1061. pthread_mutex_unlock (&totempg_mutex);
  1062. return (res);
  1063. }
  1064. int totempg_ifaces_get (
  1065. unsigned int nodeid,
  1066. struct totem_ip_address *interfaces,
  1067. char ***status,
  1068. unsigned int *iface_count)
  1069. {
  1070. int res;
  1071. res = totemmrp_ifaces_get (
  1072. nodeid,
  1073. interfaces,
  1074. status,
  1075. iface_count);
  1076. return (res);
  1077. }
  1078. int totempg_crypto_set (
  1079. unsigned int type)
  1080. {
  1081. int res;
  1082. res = totemmrp_crypto_set (
  1083. type);
  1084. return (res);
  1085. }
  1086. int totempg_ring_reenable (void)
  1087. {
  1088. int res;
  1089. res = totemmrp_ring_reenable ();
  1090. return (res);
  1091. }
  1092. const char *totempg_ifaces_print (unsigned int nodeid)
  1093. {
  1094. static char iface_string[256 * INTERFACE_MAX];
  1095. char one_iface[64];
  1096. struct totem_ip_address interfaces[INTERFACE_MAX];
  1097. char **status;
  1098. unsigned int iface_count;
  1099. unsigned int i;
  1100. int res;
  1101. iface_string[0] = '\0';
  1102. res = totempg_ifaces_get (nodeid, interfaces, &status, &iface_count);
  1103. if (res == -1) {
  1104. return ("no interface found for nodeid");
  1105. }
  1106. for (i = 0; i < iface_count; i++) {
  1107. sprintf (one_iface, "r(%d) ip(%s) ",
  1108. i, totemip_print (&interfaces[i]));
  1109. strcat (iface_string, one_iface);
  1110. }
  1111. return (iface_string);
  1112. }
  1113. unsigned int totempg_my_nodeid_get (void)
  1114. {
  1115. return (totemmrp_my_nodeid_get());
  1116. }
  1117. int totempg_my_family_get (void)
  1118. {
  1119. return (totemmrp_my_family_get());
  1120. }