totempg.c 34 KB

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