totempg.c 33 KB

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