totempg.c 34 KB

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