totempg.c 32 KB

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