totempg.c 34 KB

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