totempg.c 34 KB

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