totempg.c 33 KB

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