totempg.c 33 KB

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