gmi.c 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022
  1. /*
  2. * Copyright (c) 2003-2004 MontaVista Software, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Steven Dake (sdake@mvista.com)
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. /*
  35. * This code implements the ring protocol specified in Yair Amir's PhD thesis:
  36. * http://www.cs.jhu.edu/~yairamir/phd.ps) (ch4,5).
  37. *
  38. * Some changes have been made to the design to support things like fragmentation,
  39. * multiple I/O queues, and other things.
  40. */
  41. #include <assert.h>
  42. #include <sys/mman.h>
  43. #include <sys/types.h>
  44. #include <sys/socket.h>
  45. #include <netdb.h>
  46. #include <sys/un.h>
  47. #include <sys/sysinfo.h>
  48. #include <sys/ioctl.h>
  49. #include <netinet/in.h>
  50. #include <arpa/inet.h>
  51. #include <linux/if.h>
  52. #include <linux/sockios.h>
  53. #include <unistd.h>
  54. #include <fcntl.h>
  55. #include <stdlib.h>
  56. #include <stdio.h>
  57. #include <errno.h>
  58. #include <signal.h>
  59. #include <sched.h>
  60. #include <time.h>
  61. #include <sys/time.h>
  62. #include <sys/poll.h>
  63. #include "aispoll.h"
  64. #include "gmi.h"
  65. #include "../include/queue.h"
  66. #include "../include/sq.h"
  67. #define LOCALHOST_IP inet_addr("127.0.0.1")
  68. #define QUEUE_PEND_DELV_SIZE_MAX ((MESSAGE_SIZE_MAX / 1472) + 1) * 2
  69. #define QUEUE_RTR_ITEMS_SIZE_MAX 512
  70. #define QUEUE_PEND_TRANS_SIZE_MAX ((MESSAGE_SIZE_MAX / 1472) + 1) * 500
  71. #define MAXIOVS 8
  72. #define RTR_TOKEN_SIZE_MAX 32
  73. #define MISSING_MCAST_WINDOW 64
  74. #define TIMEOUT_STATE_GATHER 100
  75. #define TIMEOUT_TOKEN 300
  76. #define TIMEOUT_STATE_COMMIT 100
  77. #define MAX_MEMBERS 16
  78. #define HOLE_LIST_MAX MISSING_MCAST_WINDOW
  79. #define PRIORITY_MAX 3
  80. int stats_sent = 0;
  81. int stats_recv = 0;
  82. int stats_delv = 0;
  83. int stats_remcasts = 0;
  84. int stats_orf_token = 0;
  85. int stats_form_token = 0;
  86. struct timeval stats_tv_start = { 0, 0 };
  87. /*
  88. * Flow control mcasts and remcasts on last and current orf_token
  89. */
  90. int fcc_remcast_last = 0;
  91. int fcc_mcast_last = 0;
  92. int fcc_mcast_current = 0;
  93. int fcc_remcast_current = 0;
  94. enum message_type {
  95. MESSAGE_TYPE_ORF_TOKEN = 0, /* Ordering, Reliability, Flow (ORF) control Token */
  96. MESSAGE_TYPE_MCAST = 1, /* ring ordered multicast message */
  97. MESSAGE_TYPE_MEMB_ATTEMPT_JOIN = 2, /* membership join attempt message */
  98. MESSAGE_TYPE_MEMB_JOIN = 3, /* membership join message */
  99. MESSAGE_TYPE_MEMB_FORM_TOKEN = 4 /* membership FORM token */
  100. };
  101. /*
  102. * In-order pending transmit queue
  103. */
  104. struct queue queues_pend_trans[PRIORITY_MAX];
  105. /*
  106. * In-order pending delivery queue
  107. */
  108. struct pend_delv {
  109. struct in_addr ip;
  110. int seqid;
  111. int first_delivery;
  112. struct queue queue;
  113. };
  114. struct pend_delv queues_pend_delv[MAX_MEMBERS];
  115. /*
  116. * Sorted delivery/retransmit queue
  117. */
  118. struct sq queue_rtr_items;
  119. /*
  120. * Multicast address
  121. */
  122. struct sockaddr_in sockaddr_in_mcast;
  123. /*
  124. * File descriptor used when multicasting or receiving multicasts
  125. */
  126. int gmi_fd_mcast;
  127. /*
  128. * File descriptor used when unicasting the token or receiving unicast tokens
  129. */
  130. int gmi_fd_token;
  131. /*
  132. * Received up to and including
  133. */
  134. int gmi_arut = 0;
  135. /*
  136. * Delivered up to and including
  137. */
  138. int gmi_adut = 0;
  139. int gmi_adut_old = 0;
  140. int gmi_original_arut = 0;
  141. int gmi_highest_seq = 0;
  142. int gmi_highest_seq_old = 0;
  143. int gmi_barrier_seq = 0;
  144. int gmi_last_seqid = 0;
  145. int gmi_fragment = 0;
  146. int gmi_pend_queue_priority = 0;
  147. /*
  148. * Timers
  149. */
  150. poll_timer_handle timer_orf_token_timeout = 0;
  151. poll_timer_handle timer_form_token_timeout = 0;
  152. poll_timer_handle timer_memb_state_gather_timeout = 0;
  153. poll_timer_handle timer_memb_state_commit_timeout = 0;
  154. poll_timer_handle timer_single_member = 0;
  155. /*
  156. * Function called when new message received
  157. */
  158. int (*gmi_recv) (char *group, struct iovec *iovec, int iov_len);
  159. /*
  160. * Function and data used to log messages
  161. */
  162. static void (*gmi_log_printf) (int level, char *format, ...);
  163. int gmi_log_level_error;
  164. int gmi_log_level_warning;
  165. int gmi_log_level_notice;
  166. int gmi_log_level_debug;
  167. struct message_header {
  168. int type;
  169. int seqid;
  170. };
  171. struct memb_conf_id {
  172. struct in_addr rep;
  173. struct timeval tv;
  174. };
  175. struct mcast {
  176. struct message_header header;
  177. char priority;
  178. struct memb_conf_id memb_conf_id;
  179. short packet_number;
  180. short packet_count;
  181. int packet_seq;
  182. struct in_addr source;
  183. struct gmi_groupname groupname;
  184. };
  185. struct rtr_item {
  186. struct memb_conf_id conf_id;
  187. int seqid;
  188. };
  189. struct orf_token {
  190. struct message_header header;
  191. int group_arut;
  192. struct in_addr addr_arut;
  193. short int fcc;
  194. short int brake;
  195. struct in_addr brake_addr;
  196. struct rtr_item rtr_list[RTR_TOKEN_SIZE_MAX];
  197. int rtr_list_entries;
  198. };
  199. struct conf_desc {
  200. struct memb_conf_id conf_id;
  201. int highest_seq;
  202. int arut;
  203. #ifdef COMPLIE_OUT
  204. int hole_list[HOLE_LIST_MAX];
  205. int hole_list_entries;
  206. #endif
  207. };
  208. struct memb_form_token {
  209. struct message_header header;
  210. struct memb_conf_id conf_id;
  211. struct conf_desc conf_desc_list[MAX_MEMBERS]; /* SHOULD BE MAX_MEMBERS */
  212. int conf_desc_list_entries;
  213. struct in_addr member_list[MAX_MEMBERS];
  214. int member_list_entries;
  215. struct in_addr rep_list[MAX_MEMBERS];
  216. int rep_list_entries;
  217. };
  218. struct memb_attempt_join {
  219. struct message_header header;
  220. };
  221. struct memb_join {
  222. struct message_header header;
  223. struct in_addr active_rep_list[MAX_MEMBERS];
  224. int active_rep_list_entries;
  225. struct in_addr failed_rep_list[MAX_MEMBERS];
  226. int failed_rep_list_entries;
  227. };
  228. struct gmi_pend_trans_item {
  229. struct mcast *mcast;
  230. struct iovec iovec[MAXIOVS];
  231. int iov_len;
  232. };
  233. struct gmi_pend_delv_item {
  234. struct iovec iovec[MAXIOVS];
  235. int iov_len;
  236. };
  237. struct gmi_rtr_item {
  238. struct iovec iovec[MAXIOVS+2]; /* +2 is for mcast msg + group name TODO is this right */
  239. int iov_len;
  240. };
  241. enum memb_state {
  242. MEMB_STATE_OPERATIONAL,
  243. MEMB_STATE_GATHER,
  244. MEMB_STATE_COMMIT,
  245. MEMB_STATE_FORM,
  246. MEMB_STATE_EVS
  247. };
  248. static enum memb_state memb_state = MEMB_STATE_GATHER;
  249. static struct sockaddr_in gmi_bound_to;
  250. static struct sockaddr_in memb_list[MAX_MEMBERS];
  251. static int memb_list_entries = 1;
  252. static int memb_list_entries_confchg = 1;
  253. struct sockaddr_in memb_next;
  254. struct in_addr memb_gather_set[MAX_MEMBERS];
  255. int memb_gather_set_entries = 0;
  256. struct memb_commit_set {
  257. struct sockaddr_in rep;
  258. struct in_addr join_rep_list[MAX_MEMBERS];
  259. int join_rep_list_entries;
  260. struct in_addr member_list[MAX_MEMBERS];
  261. int member_list_entries;
  262. };
  263. static struct memb_commit_set memb_commit_set[MAX_MEMBERS];
  264. static int memb_commit_set_entries = 0;
  265. static struct in_addr memb_failed_list[MAX_MEMBERS];
  266. static int memb_failed_list_entries = 0;
  267. static struct sockaddr_in memb_local_sockaddr_in;
  268. static struct memb_conf_id memb_conf_id;
  269. static struct memb_conf_id memb_form_token_conf_id;
  270. static struct memb_join memb_join;
  271. static struct memb_form_token memb_form_token;
  272. static char iov_buffer[MESSAGE_SIZE_MAX];
  273. static struct iovec gmi_iov_recv = {
  274. .iov_base = iov_buffer,
  275. .iov_len = sizeof (iov_buffer)
  276. };
  277. struct message_handlers {
  278. int count;
  279. int (*handler_functions[5]) (struct sockaddr_in *, struct iovec *, int, int);
  280. };
  281. poll_handle *gmi_poll_handle;
  282. void (*gmi_deliver_fn) (
  283. struct gmi_groupname *groupname,
  284. struct iovec *iovec,
  285. int iov_len) = 0;
  286. void (*gmi_confchg_fn) (
  287. struct sockaddr_in *member_list, int member_list_entries,
  288. struct sockaddr_in *left_list, int left_list_entries,
  289. struct sockaddr_in *joined_list, int joined_list_entries) = 0;
  290. /*
  291. * forward decls
  292. */
  293. static int message_handler_orf_token (struct sockaddr_in *, struct iovec *, int, int);
  294. static int message_handler_mcast (struct sockaddr_in *, struct iovec *, int, int);
  295. static int message_handler_memb_attempt_join (struct sockaddr_in *, struct iovec *, int, int);
  296. static int message_handler_memb_join (struct sockaddr_in *, struct iovec *, int, int);
  297. static int message_handler_memb_form_token (struct sockaddr_in *, struct iovec *, int, int);
  298. static void memb_conf_id_build (struct memb_conf_id *, struct in_addr);
  299. static int recv_handler (poll_handle handle, int fd, int revents, void *data);
  300. static int netif_determine (struct sockaddr_in *bindnet, struct sockaddr_in *bound_to, char *name);
  301. static int gmi_build_sockets (struct sockaddr_in *sockaddr_mcast,
  302. struct sockaddr_in *sockaddr_bindnet,
  303. int *fd_mcast,
  304. int *fd_uni,
  305. struct sockaddr_in *bound_to);
  306. static int memb_state_gather_enter (void);
  307. static void pending_queues_deliver (void);
  308. static int orf_token_mcast (struct orf_token *orf_token,
  309. int fcc_mcasts_allowed, struct sockaddr_in *system_from);
  310. static void queues_pend_delv_memb_new (void);
  311. static void calculate_group_arut (struct orf_token *orf_token);
  312. static int messages_free (int group_arut);
  313. struct message_handlers gmi_message_handlers = {
  314. 5,
  315. {
  316. message_handler_orf_token,
  317. message_handler_mcast,
  318. message_handler_memb_attempt_join,
  319. message_handler_memb_join,
  320. message_handler_memb_form_token
  321. }
  322. };
  323. void gmi_log_printf_init (
  324. void (*log_printf) (int , char *, ...),
  325. int log_level_error,
  326. int log_level_warning,
  327. int log_level_notice,
  328. int log_level_debug)
  329. {
  330. gmi_log_level_error = log_level_error;
  331. gmi_log_level_warning = log_level_warning;
  332. gmi_log_level_notice = log_level_notice;
  333. gmi_log_level_debug = log_level_debug;
  334. gmi_log_printf = log_printf;
  335. }
  336. /*
  337. * Exported interfaces
  338. */
  339. int gmi_init (
  340. struct sockaddr_in *sockaddr_mcast,
  341. struct sockaddr_in *sockaddr_bindnet,
  342. poll_handle *poll_handle,
  343. struct sockaddr_in *sockaddr_boundto)
  344. {
  345. int i;
  346. int res;
  347. memcpy (&sockaddr_in_mcast, sockaddr_mcast, sizeof (struct sockaddr_in));
  348. memset (&memb_next, 0, sizeof (struct sockaddr_in));
  349. memset (iov_buffer, 0, MESSAGE_SIZE_MAX);
  350. for (i = 0; i < PRIORITY_MAX; i++) {
  351. queue_init (&queues_pend_trans[i], QUEUE_PEND_TRANS_SIZE_MAX,
  352. sizeof (struct gmi_pend_trans_item));
  353. }
  354. sq_init (&queue_rtr_items, QUEUE_RTR_ITEMS_SIZE_MAX, sizeof (struct gmi_rtr_item), 0);
  355. /*
  356. * Create and bind the multicast and unicast sockets
  357. */
  358. res = gmi_build_sockets (sockaddr_mcast,
  359. sockaddr_bindnet,
  360. &gmi_fd_mcast,
  361. &gmi_fd_token,
  362. sockaddr_boundto);
  363. memcpy (&gmi_bound_to, sockaddr_boundto, sizeof (struct sockaddr_in));
  364. /*
  365. * This stuff depends on gmi_build_sockets
  366. */
  367. memcpy (&memb_list[0], sockaddr_boundto, sizeof (struct sockaddr_in));
  368. memb_conf_id_build (&memb_conf_id, sockaddr_boundto->sin_addr);
  369. memcpy (&memb_form_token_conf_id, &memb_conf_id, sizeof (struct memb_conf_id));
  370. printf ("mcast is %d token is %d\n", gmi_fd_mcast, gmi_fd_token);
  371. gmi_poll_handle = poll_handle;
  372. poll_dispatch_add (*gmi_poll_handle, gmi_fd_mcast, POLLIN, 0, recv_handler);
  373. poll_dispatch_add (*gmi_poll_handle, gmi_fd_token, POLLIN, 0, recv_handler);
  374. memb_state_gather_enter ();
  375. memset (&memb_next, 0, sizeof (struct sockaddr_in));
  376. queues_pend_delv_memb_new ();
  377. return (0);
  378. }
  379. int gmi_join (
  380. struct gmi_groupname *groupname,
  381. void (*deliver_fn) (
  382. struct gmi_groupname *groupname,
  383. struct iovec *iovec,
  384. int iov_len),
  385. void (*confchg_fn) (
  386. struct sockaddr_in *member_list, int member_list_entries,
  387. struct sockaddr_in *left_list, int left_list_entries,
  388. struct sockaddr_in *joined_list, int joined_list_entries),
  389. gmi_join_handle *handle_out) {
  390. gmi_deliver_fn = deliver_fn;
  391. gmi_confchg_fn = confchg_fn;
  392. *handle_out = 0;
  393. return (0);
  394. }
  395. int local_host_seq_count = 0;
  396. int gmi_leave (
  397. gmi_join_handle handle_join);
  398. static int gmi_pend_trans_item_store (
  399. struct gmi_groupname *groupname,
  400. struct iovec *iovec,
  401. int iov_len,
  402. int priority,
  403. short packet_number, short packet_count)
  404. {
  405. int i, j;
  406. struct gmi_pend_trans_item gmi_pend_trans_item;
  407. memset (&gmi_pend_trans_item, 0, sizeof (struct gmi_pend_trans_item));
  408. /*
  409. * Store pending item
  410. */
  411. gmi_pend_trans_item.mcast = malloc (sizeof (struct mcast));
  412. if (gmi_pend_trans_item.mcast == 0) {
  413. goto error_mcast;
  414. }
  415. /*
  416. * Set mcast header
  417. */
  418. gmi_pend_trans_item.mcast->header.type = MESSAGE_TYPE_MCAST;
  419. gmi_pend_trans_item.mcast->priority = priority;
  420. gmi_pend_trans_item.mcast->packet_number = packet_number;
  421. gmi_pend_trans_item.mcast->packet_count = packet_count;
  422. gmi_pend_trans_item.mcast->packet_seq = local_host_seq_count++;
  423. gmi_pend_trans_item.mcast->source.s_addr = gmi_bound_to.sin_addr.s_addr;
  424. memcpy (&gmi_pend_trans_item.mcast->groupname, groupname,
  425. sizeof (struct gmi_groupname));
  426. for (i = 0; i < iov_len; i++) {
  427. gmi_pend_trans_item.iovec[i].iov_base = malloc (iovec[i].iov_len);
  428. if (gmi_pend_trans_item.iovec[i].iov_base == 0) {
  429. goto error_iovec;
  430. }
  431. memset (gmi_pend_trans_item.iovec[i].iov_base, 0, iovec[i].iov_len);
  432. memcpy (gmi_pend_trans_item.iovec[i].iov_base, iovec[i].iov_base,
  433. iovec[i].iov_len);
  434. gmi_pend_trans_item.iovec[i].iov_len = iovec[i].iov_len;
  435. }
  436. gmi_pend_trans_item.iov_len = iov_len;
  437. gmi_log_printf (gmi_log_level_debug, "mcasted message added to pending queue\n");
  438. queue_item_add (&queues_pend_trans[priority], &gmi_pend_trans_item);
  439. return (0);
  440. error_iovec:
  441. for (j = 0; j < i; j++) {
  442. free (gmi_pend_trans_item.iovec[j].iov_base);
  443. }
  444. return (-1);
  445. error_mcast:
  446. return (0);
  447. }
  448. /*
  449. * MTU - multicast message header - IP header - UDP header
  450. *
  451. * On lossy switches, making use of the DF UDP flag can lead to loss of
  452. * forward progress. So the packets must be fragmented by the algorithm
  453. * and reassembled at the receiver.
  454. */
  455. #define FRAGMENT_SIZE (1500 - sizeof (struct mcast) - 20 - 8)
  456. static void timer_function_single_member (void *data);
  457. /*
  458. * With only a single member, multicast messages as if an orf token was
  459. * delivered. This is done as part of the main event loop by specifying
  460. * a timer with an immediate expiration. This is a little suboptimal
  461. * since poll starts afresh. If more messages are waiting to be
  462. * self-delivered, queue the timer function again until there are no
  463. * more waiting messages.
  464. */
  465. static void single_member_deliver (void)
  466. {
  467. poll_timer_delete (*gmi_poll_handle, timer_single_member);
  468. timer_single_member = 0;
  469. poll_timer_add (*gmi_poll_handle, 0, 0,
  470. timer_function_single_member, &timer_single_member);
  471. }
  472. static void timer_function_single_member (void *data)
  473. {
  474. struct orf_token orf_token;
  475. int more_messages;
  476. memset (&orf_token, 0, sizeof (struct orf_token));
  477. orf_token.header.seqid = gmi_arut;
  478. orf_token.header.type = MESSAGE_TYPE_ORF_TOKEN;
  479. orf_token.group_arut = gmi_arut;
  480. orf_token.rtr_list_entries = 0;
  481. more_messages = orf_token_mcast (&orf_token, 99, &memb_local_sockaddr_in);
  482. calculate_group_arut (&orf_token);
  483. messages_free (gmi_arut);
  484. /*
  485. * Queue delivery again if more messages are available
  486. */
  487. if (more_messages) {
  488. single_member_deliver ();
  489. }
  490. }
  491. int gmi_mcast (
  492. struct gmi_groupname *groupname,
  493. struct iovec *iovec,
  494. int iov_len,
  495. int priority)
  496. {
  497. int res;
  498. struct iovec copied_iovec;
  499. struct iovec pending_iovecs[MAXIOVS];
  500. int pending_iovec_entries = 0;
  501. int iovec_entry = 0;
  502. int total_size;
  503. int packet_size;
  504. int i;
  505. int packet_number = 0;
  506. int packet_count = 0;
  507. packet_size = FRAGMENT_SIZE;
  508. gmi_log_printf (gmi_log_level_debug, "MCASTING MESSAGE\n");
  509. memset (pending_iovecs, 0, sizeof (struct iovec) * MAXIOVS);
  510. /*
  511. * Determine size of total message
  512. */
  513. total_size = 0;
  514. for (i = 0; i < iov_len; i++) {
  515. total_size += iovec[i].iov_len;
  516. assert (iovec[i].iov_len < MESSAGE_SIZE_MAX);
  517. }
  518. packet_count = (total_size / packet_size);
  519. gmi_log_printf (gmi_log_level_debug, "Message size is %d\n", total_size);
  520. /*
  521. * Break message up into individual packets and publish them
  522. */
  523. copied_iovec.iov_base = iovec[0].iov_base;
  524. copied_iovec.iov_len = iovec[0].iov_len;
  525. packet_size = 0;
  526. pending_iovec_entries = 0;
  527. iovec_entry = 0;
  528. do {
  529. if (copied_iovec.iov_len + packet_size > FRAGMENT_SIZE) {
  530. pending_iovecs[pending_iovec_entries].iov_base = copied_iovec.iov_base;
  531. pending_iovecs[pending_iovec_entries].iov_len = FRAGMENT_SIZE - packet_size;
  532. copied_iovec.iov_base += FRAGMENT_SIZE - packet_size;
  533. copied_iovec.iov_len -= FRAGMENT_SIZE - packet_size;
  534. packet_size += pending_iovecs[pending_iovec_entries].iov_len;
  535. } else {
  536. pending_iovecs[pending_iovec_entries].iov_base = copied_iovec.iov_base;
  537. pending_iovecs[pending_iovec_entries].iov_len = copied_iovec.iov_len;
  538. packet_size += copied_iovec.iov_len;
  539. iovec_entry += 1; /* this must be before copied_iovec */
  540. copied_iovec.iov_base = iovec[iovec_entry].iov_base;
  541. copied_iovec.iov_len = iovec[iovec_entry].iov_len;
  542. }
  543. pending_iovec_entries += 1;
  544. if (packet_size >= FRAGMENT_SIZE || packet_size == total_size) {
  545. #ifdef DEBUGa
  546. for (i = 0; i < pending_iovec_entries; i++) {
  547. assert (pending_iovecs[i].iov_len < MESSAGE_SIZE_MAX);
  548. assert (pending_iovecs[i].iov_len >= 0);
  549. printf ("iovecs[%d] %x %d\n", i, pending_iovecs[i].iov_base, pending_iovecs[i].iov_len);
  550. calced_total += pending_iovecs[i].iov_len;
  551. }
  552. printf ("CALCULATED TOTAL is %d\n", calced_total);
  553. #endif
  554. total_size -= packet_size;
  555. assert (total_size >= 0);
  556. res = gmi_pend_trans_item_store (groupname, pending_iovecs,
  557. pending_iovec_entries, priority, packet_number, packet_count);
  558. pending_iovec_entries = 0;
  559. iovec_entry = 0;
  560. packet_size = 0;
  561. packet_number += 1;
  562. }
  563. } while (total_size > 0);
  564. /*
  565. * The queued messages are sent in orf_token_mcast, not this function
  566. * But if this processor is the only node, it must deliver the messages
  567. * for self-delivery requirements because orf_token_mcast is only called
  568. * on reception of a token
  569. */
  570. if (memb_list_entries == 1) {
  571. single_member_deliver ();
  572. }
  573. return (0);
  574. }
  575. static int netif_determine (struct sockaddr_in *bindnet,
  576. struct sockaddr_in *bound_to,
  577. char *ifname)
  578. {
  579. struct sockaddr_in *sockaddr_in;
  580. int id_fd;
  581. struct ifconf ifc;
  582. int numreqs = 0;
  583. int res;
  584. int i;
  585. in_addr_t mask_addr;
  586. /*
  587. * Generate list of local interfaces in ifc.ifc_req structure
  588. */
  589. id_fd = socket (AF_INET, SOCK_STREAM, 0);
  590. ifc.ifc_buf = 0;
  591. do {
  592. numreqs += 32;
  593. ifc.ifc_len = sizeof (struct ifreq) * numreqs;
  594. ifc.ifc_buf = (void *)realloc(ifc.ifc_buf, ifc.ifc_len);
  595. res = ioctl (id_fd, SIOCGIFCONF, &ifc);
  596. if (res < 0) {
  597. close (id_fd);
  598. return -1;
  599. }
  600. } while (ifc.ifc_len == sizeof (struct ifreq) * numreqs);
  601. res = -1;
  602. /*
  603. * Find interface address to bind to
  604. */
  605. for (i = 0; i < ifc.ifc_len / sizeof (struct ifreq); i++) {
  606. sockaddr_in = (struct sockaddr_in *)&ifc.ifc_ifcu.ifcu_req[i].ifr_ifru.ifru_addr;
  607. mask_addr = inet_addr ("255.255.255.0");
  608. if ((sockaddr_in->sin_family == AF_INET) &&
  609. (sockaddr_in->sin_addr.s_addr & mask_addr) ==
  610. (bindnet->sin_addr.s_addr & mask_addr)) {
  611. bound_to->sin_addr.s_addr = sockaddr_in->sin_addr.s_addr;
  612. strcpy (ifname, ifc.ifc_ifcu.ifcu_req[i].ifr_ifrn.ifrn_name);
  613. res = i;
  614. break; /* for */
  615. }
  616. }
  617. free (ifc.ifc_buf);
  618. close (id_fd);
  619. return (res);
  620. }
  621. static int gmi_build_sockets (struct sockaddr_in *sockaddr_mcast,
  622. struct sockaddr_in *sockaddr_bindnet,
  623. int *fd_mcast,
  624. int *fd_uni,
  625. struct sockaddr_in *bound_to)
  626. {
  627. struct ip_mreq mreq;
  628. struct sockaddr_in sockaddr_in;
  629. char flag;
  630. struct ifreq interface;
  631. int res;
  632. memset (&mreq, 0, sizeof (struct ip_mreq));
  633. memset (&interface, 0, sizeof (struct ifreq));
  634. /*
  635. * Determine the ip address bound to and the interface name
  636. */
  637. res = netif_determine (sockaddr_bindnet,
  638. bound_to,
  639. interface.ifr_ifrn.ifrn_name);
  640. if (res == -1) {
  641. return (-1);
  642. }
  643. /* TODO this should be somewhere else */
  644. memb_local_sockaddr_in.sin_addr.s_addr = bound_to->sin_addr.s_addr;
  645. memb_local_sockaddr_in.sin_family = AF_INET;
  646. memb_local_sockaddr_in.sin_port = sockaddr_mcast->sin_port;
  647. /*
  648. * Create multicast socket
  649. */
  650. *fd_mcast = socket (AF_INET, SOCK_DGRAM, 0);
  651. if (*fd_mcast == -1) {
  652. perror ("socket");
  653. return (-1);
  654. }
  655. /*
  656. * Bind the multicast socket to the correct device (eth0, eth1)
  657. */
  658. if (setsockopt(*fd_mcast, SOL_SOCKET, SO_BINDTODEVICE,
  659. (char *)&interface, sizeof(interface)) < 0) {
  660. gmi_log_printf (gmi_log_level_warning, "Could not bind to device for multicast, group messaging may not work properly. (%s)\n", strerror (errno));
  661. }
  662. /*
  663. * Bind to multicast socket used for multicast send/receives
  664. */
  665. sockaddr_in.sin_family = AF_INET;
  666. sockaddr_in.sin_addr.s_addr = sockaddr_mcast->sin_addr.s_addr;
  667. sockaddr_in.sin_port = sockaddr_mcast->sin_port;
  668. res = bind (*fd_mcast, (struct sockaddr *)&sockaddr_in,
  669. sizeof (struct sockaddr_in));
  670. if (res == -1) {
  671. perror ("bind failed");
  672. return (-1);
  673. }
  674. /*
  675. * Setup unicast socket
  676. */
  677. *fd_uni = socket (AF_INET, SOCK_DGRAM, 0);
  678. if (*fd_uni == -1) {
  679. perror ("socket2");
  680. return (-1);
  681. }
  682. /*
  683. * Bind to unicast socket used for token send/receives
  684. * This has the side effect of binding to the correct interface
  685. */
  686. sockaddr_in.sin_addr.s_addr = bound_to->sin_addr.s_addr;
  687. res = bind (*fd_uni, (struct sockaddr *)&sockaddr_in,
  688. sizeof (struct sockaddr_in));
  689. if (res == -1) {
  690. perror ("bind2 failed");
  691. return (-1);
  692. }
  693. #ifdef CONFIG_USE_BROADCAST
  694. /* This config option doesn't work */
  695. {
  696. int on = 1;
  697. setsockopt (*fd_mcast, SOL_SOCKET, SO_BROADCAST, (char *)&on, sizeof (on));
  698. }
  699. #else
  700. /*
  701. * Join group membership on socket
  702. */
  703. mreq.imr_multiaddr.s_addr = sockaddr_mcast->sin_addr.s_addr;
  704. mreq.imr_interface.s_addr = bound_to->sin_addr.s_addr;
  705. res = setsockopt (*fd_mcast, IPPROTO_IP, IP_ADD_MEMBERSHIP,
  706. &mreq, sizeof (mreq));
  707. if (res == -1) {
  708. perror ("join multicast group failed");
  709. return (-1);
  710. }
  711. #endif
  712. /*
  713. * Turn off multicast loopback since we know what messages we have sent
  714. */
  715. flag = 0;
  716. res = setsockopt (*fd_mcast, IPPROTO_IP, IP_MULTICAST_LOOP,
  717. &flag, sizeof (flag));
  718. if (res == -1) {
  719. perror ("turn off loopback");
  720. return (-1);
  721. }
  722. return (0);
  723. }
  724. /*
  725. * Misc Management
  726. */
  727. int in_addr_compare (const void *a, const void *b) {
  728. struct in_addr *in_addr_a = (struct in_addr *)a;
  729. struct in_addr *in_addr_b = (struct in_addr *)b;
  730. return (in_addr_a->s_addr > in_addr_b->s_addr);
  731. }
  732. /*
  733. * ORF Token Management
  734. */
  735. /*
  736. * Recast message to mcast group if it is available
  737. */
  738. int orf_token_remcast (int seqid) {
  739. struct msghdr msg_mcast;
  740. struct gmi_rtr_item *gmi_rtr_item;
  741. int res;
  742. struct mcast *mcast;
  743. #ifdef DEBUG
  744. printf ("remulticasting %d\n", seqid);
  745. #endif
  746. /*
  747. * Get RTR item at seqid, if not available, return
  748. */
  749. res = sq_item_get (&queue_rtr_items, seqid, (void **)&gmi_rtr_item);
  750. if (res != 0) {
  751. return -1;
  752. }
  753. mcast = (struct mcast *)gmi_rtr_item->iovec[0].iov_base;
  754. /*
  755. * Build multicast message
  756. */
  757. msg_mcast.msg_name = (caddr_t)&sockaddr_in_mcast;
  758. msg_mcast.msg_namelen = sizeof (struct sockaddr_in);
  759. msg_mcast.msg_iov = gmi_rtr_item->iovec;
  760. msg_mcast.msg_iovlen = gmi_rtr_item->iov_len;
  761. msg_mcast.msg_control = 0;
  762. msg_mcast.msg_controllen = 0;
  763. msg_mcast.msg_flags = 0;
  764. /*
  765. * Multicast message
  766. */
  767. res = sendmsg (gmi_fd_mcast, &msg_mcast, MSG_NOSIGNAL | MSG_DONTWAIT);
  768. if (res == -1) {
  769. printf ("error during remulticast %d %d %d\n", seqid, errno, gmi_rtr_item->iov_len);
  770. return (-1);
  771. }
  772. stats_sent += res;
  773. return (0);
  774. }
  775. int last_group_arut = 0;
  776. int last_released = 0;
  777. int set_arut = -1;
  778. /*
  779. * Brake output multicasts if the missing window is too large
  780. */
  781. int gmi_brake;
  782. static int messages_free (int group_arut)
  783. {
  784. struct gmi_rtr_item *gmi_rtr_item_p;
  785. int i, j;
  786. int res;
  787. int lesser;
  788. // TODO printf ("group arut %d last_group-arut %d gmi_dut %d barrier %d\n", group_arut, last_group_arut, gmi_dut, gmi_barrier_seq);
  789. /*
  790. * Determine braking value (when messages + MISSING_MCAST_WINDOW, stop sending messages)
  791. */
  792. gmi_brake = group_arut;
  793. if (gmi_brake > last_group_arut) {
  794. gmi_brake = last_group_arut;
  795. }
  796. /*
  797. * Determine low water mark for messages to be freed
  798. */
  799. lesser = gmi_brake;
  800. if (lesser > gmi_adut) {
  801. lesser = gmi_adut;
  802. }
  803. //printf ("Freeing lesser %d %d %d\n", lesser, group_arut, last_group_arut);
  804. //printf ("lesser %d gropu arut %d last group arut %d\n", lesser, group_arut, last_group_arut);
  805. /*
  806. * return early if no messages can be freed
  807. */
  808. /*
  809. if (last_released + 1 == lesser) {
  810. return (0);
  811. }
  812. */
  813. /*
  814. * Release retransmit list items if group arut indicates they are transmitted
  815. */
  816. for (i = last_released; i <= lesser; i++) {
  817. res = sq_item_get (&queue_rtr_items, i, (void **)&gmi_rtr_item_p);
  818. if (res == 0) {
  819. for (j = 0; j < gmi_rtr_item_p->iov_len; j++) {
  820. free (gmi_rtr_item_p->iovec[j].iov_base);
  821. gmi_rtr_item_p->iovec[j].iov_base = (void *)0xdeadbeef;
  822. gmi_rtr_item_p->iovec[j].iov_len = i;
  823. }
  824. }
  825. last_released = i + 1;
  826. }
  827. sq_items_release (&queue_rtr_items, lesser);
  828. gmi_log_printf (gmi_log_level_debug, "releasing messages up to and including %d\n", lesser);
  829. return (0);
  830. }
  831. /*
  832. * Multicasts pending messages onto the ring (requires orf_token possession)
  833. */
  834. static int orf_token_mcast (
  835. struct orf_token *orf_token,
  836. int fcc_mcasts_allowed,
  837. struct sockaddr_in *system_from)
  838. {
  839. struct msghdr msg_mcast;
  840. struct gmi_rtr_item gmi_rtr_item;
  841. struct gmi_pend_trans_item *gmi_pend_trans_item = 0;
  842. int res = 0;
  843. int orf_token_seqid;
  844. struct mcast *mcast;
  845. int last_packet = 1;
  846. struct queue *queue_pend_trans;
  847. /*
  848. * Disallow multicasts unless state is operational
  849. */
  850. if (memb_state != MEMB_STATE_OPERATIONAL) {
  851. return (0);
  852. }
  853. /*
  854. * If received a token with a higher sequence number,
  855. * set highest seq so retransmits can happen at end of
  856. * message stream
  857. */
  858. if (orf_token->header.seqid > gmi_highest_seq) {
  859. gmi_highest_seq = orf_token->header.seqid;
  860. }
  861. orf_token_seqid = orf_token->header.seqid;
  862. queue_pend_trans = &queues_pend_trans[gmi_pend_queue_priority];
  863. for (fcc_mcast_current = 0; fcc_mcast_current < fcc_mcasts_allowed; fcc_mcast_current++) {
  864. /*
  865. * determine which pending queue to take message
  866. * from if this is not a message fragment
  867. */
  868. if (gmi_fragment == 0) {
  869. gmi_pend_queue_priority = 0;
  870. do {
  871. queue_pend_trans = &queues_pend_trans[gmi_pend_queue_priority];
  872. if (queue_is_empty (queue_pend_trans)) {
  873. gmi_pend_queue_priority++;
  874. } else {
  875. break; /* from do - found first queue with data */
  876. }
  877. } while (gmi_pend_queue_priority < PRIORITY_MAX);
  878. }
  879. if (gmi_pend_queue_priority == PRIORITY_MAX) {
  880. break; /* all queues are empty, break from for */
  881. }
  882. // printf ("selecting pending queue %d\n", gmi_pend_queue_priority);
  883. gmi_pend_trans_item = (struct gmi_pend_trans_item *)queue_item_get (queue_pend_trans);
  884. /* preincrement required by algo */
  885. gmi_pend_trans_item->mcast->header.seqid = ++orf_token->header.seqid;
  886. // UNDO printf ("multicasting seqid %d\n", gmi_pend_trans_item->mcast->header.seqid);
  887. last_packet = (gmi_pend_trans_item->mcast->packet_number ==
  888. gmi_pend_trans_item->mcast->packet_count);
  889. //printf ("last packet is %d current mcast %d\n", last_packet, fcc_mcast_current);
  890. /*
  891. * Build IO vector
  892. */
  893. memset (&gmi_rtr_item, 0, sizeof (struct gmi_rtr_item));
  894. gmi_rtr_item.iovec[0].iov_base = gmi_pend_trans_item->mcast;
  895. gmi_rtr_item.iovec[0].iov_len = sizeof (struct mcast);
  896. mcast = gmi_rtr_item.iovec[0].iov_base;
  897. /*
  898. * Is this a fragment of a message
  899. */
  900. if (mcast->packet_number == mcast->packet_count) {
  901. gmi_fragment = 0;
  902. } else {
  903. gmi_fragment = 1;
  904. }
  905. memcpy (&mcast->memb_conf_id, &memb_form_token_conf_id,
  906. sizeof (struct memb_conf_id));
  907. memcpy (&gmi_rtr_item.iovec[1], gmi_pend_trans_item->iovec,
  908. gmi_pend_trans_item->iov_len * sizeof (struct iovec));
  909. gmi_rtr_item.iov_len = gmi_pend_trans_item->iov_len + 1;
  910. assert (gmi_rtr_item.iov_len < 16);
  911. /*
  912. * Add message to retransmit queue
  913. */
  914. sq_item_add (&queue_rtr_items,
  915. &gmi_rtr_item, gmi_pend_trans_item->mcast->header.seqid);
  916. /*
  917. * Delete item from pending queue
  918. */
  919. queue_item_remove (queue_pend_trans);
  920. /*
  921. * Build multicast message
  922. */
  923. msg_mcast.msg_name = &sockaddr_in_mcast;
  924. msg_mcast.msg_namelen = sizeof (struct sockaddr_in);
  925. msg_mcast.msg_iov = gmi_rtr_item.iovec;
  926. msg_mcast.msg_iovlen = gmi_rtr_item.iov_len;
  927. msg_mcast.msg_control = 0;
  928. msg_mcast.msg_controllen = 0;
  929. msg_mcast.msg_flags = 0;
  930. /*
  931. * Multicast message
  932. */
  933. res = sendmsg (gmi_fd_mcast, &msg_mcast, MSG_NOSIGNAL | MSG_DONTWAIT);
  934. /*
  935. * An error here is recovered by the multicast algorithm
  936. */
  937. // TODO stats_sent isn't right below
  938. stats_sent += res;
  939. }
  940. assert (fcc_mcast_current < 100);
  941. #ifdef OUTA
  942. if (fcc_mcast_current > fcc_mcasts_allowed) {
  943. fcc_mcast_current = fcc_mcasts_allowed;
  944. }
  945. #endif
  946. /*
  947. * If messages mcasted, deliver any new messages to pending queues
  948. */
  949. if (fcc_mcast_current) {
  950. if (gmi_pend_trans_item->mcast->header.seqid > gmi_highest_seq) {
  951. gmi_highest_seq = gmi_pend_trans_item->mcast->header.seqid;
  952. }
  953. pending_queues_deliver ();
  954. //printf ("orf Token seqid is %d group %d\n", orf_token_seqid, orf_token->group_arut);
  955. #ifdef COMPILE_OUT
  956. if (orf_token_seqid == orf_token->group_arut) {
  957. //printf ("previous group arut #1 %d\n", orf_token->group_arut);
  958. orf_token->group_arut = orf_token_seqid + fcc_mcast_current;
  959. orf_token->addr_arut.s_addr = 0;
  960. }
  961. //printf ("reasing group arut to %d\n", orf_token->group_arut);
  962. #endif
  963. }
  964. /*
  965. * Return 1 if more messages are available for single node clusters
  966. */
  967. return (fcc_mcast_current == fcc_mcasts_allowed);
  968. }
  969. /*
  970. * Remulticasts messages in orf_token's retransmit list (requires orf_token)
  971. * Modify's orf_token's rtr to include retransmits required by this process
  972. */
  973. static void orf_token_rtr (
  974. struct orf_token *orf_token,
  975. int *fcc_allowed)
  976. {
  977. int res;
  978. int i, j;
  979. int found;
  980. #ifdef COMPLE_OUT
  981. printf ("Retransmit List %d\n", orf_token->rtr_list_entries);
  982. for (i = 0; i < orf_token->rtr_list_entries; i++) {
  983. printf ("%d ", orf_token->rtr_list[i].seqid);
  984. }
  985. printf ("\n");
  986. #endif
  987. /*
  988. * Retransmit messages on orf_token's RTR list from RTR queue
  989. */
  990. for (fcc_remcast_current = 0, i = 0;
  991. fcc_remcast_current <= *fcc_allowed && i < orf_token->rtr_list_entries;) {
  992. #ifdef COMPILE_OUT
  993. printf ("%d.%d.%d vs %d.%d.%d\n",
  994. orf_token->rtr_list[i].conf_id.rep.s_addr,
  995. orf_token->rtr_list[i].conf_id.tv.tv_sec,
  996. orf_token->rtr_list[i].conf_id.tv.tv_usec,
  997. memb_form_token_conf_id.rep.s_addr,
  998. memb_form_token_conf_id.tv.tv_sec,
  999. memb_form_token_conf_id.tv.tv_usec);
  1000. #endif
  1001. /*
  1002. * If this retransmit request isn't from this configuration,
  1003. * try next rtr entry
  1004. */
  1005. if (memcmp (&orf_token->rtr_list[i].conf_id, &memb_form_token_conf_id,
  1006. sizeof (struct memb_conf_id)) != 0) {
  1007. i++;
  1008. continue;
  1009. }
  1010. assert (orf_token->rtr_list[i].seqid > 0);
  1011. res = orf_token_remcast (orf_token->rtr_list[i].seqid);
  1012. if (res == 0) {
  1013. orf_token->rtr_list_entries -= 1;
  1014. assert (orf_token->rtr_list_entries >= 0);
  1015. memmove (&orf_token->rtr_list[i],
  1016. &orf_token->rtr_list[i + 1],
  1017. sizeof (struct rtr_item) * (orf_token->rtr_list_entries));
  1018. fcc_remcast_current++;
  1019. stats_remcasts++;
  1020. } else {
  1021. i++;
  1022. //printf ("couldn't remcast %d\n", i);
  1023. }
  1024. }
  1025. *fcc_allowed = *fcc_allowed - fcc_remcast_current - 1;
  1026. #ifdef COMPILE_OUT
  1027. for (i = 0; i < orf_token->rtr_list_entries; i++) {
  1028. assert (orf_token->rtr_list[i].seqid != -1);
  1029. }
  1030. #endif
  1031. /*
  1032. * Add messages to retransmit to RTR list
  1033. * but only retry if there is room in the retransmit list
  1034. */
  1035. for (i = gmi_arut + 1;
  1036. orf_token->rtr_list_entries < RTR_TOKEN_SIZE_MAX &&
  1037. // i <= orf_token->header.seqid; /* TODO this worked previously but not correct for EVS */
  1038. i <= gmi_highest_seq;
  1039. i++) {
  1040. res = sq_item_inuse (&queue_rtr_items, i);
  1041. if (res == 0) {
  1042. found = 0;
  1043. for (j = 0; j < orf_token->rtr_list_entries; j++) {
  1044. if (i == orf_token->rtr_list[j].seqid) {
  1045. found = 1;
  1046. }
  1047. }
  1048. if (found == 0) {
  1049. memcpy (&orf_token->rtr_list[orf_token->rtr_list_entries].conf_id,
  1050. &memb_form_token_conf_id, sizeof (struct memb_conf_id));
  1051. orf_token->rtr_list[orf_token->rtr_list_entries].seqid = i;
  1052. orf_token->rtr_list_entries++;
  1053. //printf ("adding to retransmit list %d\n", i);
  1054. }
  1055. }
  1056. }
  1057. }
  1058. /*
  1059. * Calculate flow control count
  1060. */
  1061. static void orf_token_fcc (
  1062. struct orf_token *orf_token)
  1063. {
  1064. orf_token->fcc = orf_token->fcc - fcc_mcast_last - fcc_remcast_last
  1065. + fcc_mcast_current + fcc_remcast_current;
  1066. fcc_mcast_last = fcc_mcast_current;
  1067. fcc_remcast_last = fcc_remcast_current;
  1068. }
  1069. static void queues_pend_delv_memb_new (void)
  1070. {
  1071. struct pend_delv pend_delv_new[MAX_MEMBERS];
  1072. int item_index = 0;
  1073. int i, j;
  1074. int found;
  1075. memset (pend_delv_new, 0, sizeof (struct pend_delv) * MAX_MEMBERS);
  1076. /*
  1077. * Build new pending list
  1078. */
  1079. for (i = 0; i < memb_list_entries_confchg; i++) {
  1080. found = 0;
  1081. for (j = 0; j < MAX_MEMBERS; j++) {
  1082. /*
  1083. * If membership item in queues pending delivery list, copy it
  1084. */
  1085. if (memb_list[i].sin_addr.s_addr == queues_pend_delv[j].ip.s_addr) {
  1086. memcpy (&pend_delv_new[item_index], &queues_pend_delv[j],
  1087. sizeof (struct pend_delv));
  1088. item_index += 1;
  1089. found = 1;
  1090. break; /* for j = */
  1091. }
  1092. }
  1093. /*
  1094. * If membership item not found in pending delivery list, make new entry
  1095. */
  1096. if (found == 0) {
  1097. queue_init (&pend_delv_new[item_index].queue, QUEUE_PEND_DELV_SIZE_MAX,
  1098. sizeof (struct gmi_pend_delv_item));
  1099. pend_delv_new[item_index].seqid = 0;
  1100. pend_delv_new[item_index].ip.s_addr = memb_list[i].sin_addr.s_addr;
  1101. item_index += 1;
  1102. }
  1103. }
  1104. /*
  1105. * Copy new list into system list
  1106. */
  1107. memcpy (queues_pend_delv, pend_delv_new,
  1108. sizeof (struct pend_delv) * MAX_MEMBERS);
  1109. for (i = 0; i < memb_list_entries_confchg; i++) {
  1110. /*
  1111. * If queue not empty, mark it for first delivery
  1112. * otherwise reset seqno
  1113. */
  1114. if (queue_is_empty (&queues_pend_delv[i].queue) == 0) {
  1115. queues_pend_delv[i].first_delivery = 1;
  1116. } else {
  1117. queues_pend_delv[i].seqid = 0;
  1118. }
  1119. }
  1120. }
  1121. static int orf_token_evs (
  1122. struct orf_token *orf_token,
  1123. int starting_group_arut)
  1124. {
  1125. int i, j;
  1126. struct sockaddr_in trans_memb_list[MAX_MEMBERS];
  1127. struct sockaddr_in left_list[MAX_MEMBERS];
  1128. struct sockaddr_in joined_list[MAX_MEMBERS];
  1129. int trans_memb_list_entries = 0;
  1130. int left_list_entries = 0;
  1131. int joined_list_entries = 0;
  1132. int found;
  1133. //printf ("group arut is %d %d %d %d\n", orf_token->header.seqid, orf_token->group_arut, gmi_arut, gmi_highest_seq);
  1134. /*
  1135. * We should only execute this function if we are in EVS membership state
  1136. */
  1137. if (memb_state != MEMB_STATE_EVS) {
  1138. return (0);
  1139. }
  1140. memset (trans_memb_list, 0, sizeof (struct sockaddr_in) * MAX_MEMBERS);
  1141. /*
  1142. * Delete form token timer since the token has been swallowed
  1143. */
  1144. poll_timer_delete (*gmi_poll_handle, timer_form_token_timeout);
  1145. timer_form_token_timeout = 0;
  1146. printf ("EVS STATE group arut %d gmi arut %d highest %d barrier %d starting group arut %d\n", orf_token->group_arut, gmi_arut, gmi_highest_seq, gmi_barrier_seq, starting_group_arut);
  1147. /*
  1148. * This node has reached highest seq, set local arut to barrier
  1149. */
  1150. if (gmi_arut == gmi_highest_seq) {
  1151. //printf ("setting arut to barrier %d\n", gmi_barrier_seq);
  1152. gmi_arut = gmi_barrier_seq;
  1153. }
  1154. /*
  1155. * Determine when EVS recovery has completed
  1156. */
  1157. //printf ("group arut is %d %d %d\n", orf_token->group_arut, gmi_arut, gmi_highest_seq);
  1158. // TODO
  1159. if (memb_state == MEMB_STATE_EVS && gmi_arut == gmi_barrier_seq && orf_token->group_arut == gmi_barrier_seq) {
  1160. gmi_log_printf (gmi_log_level_notice, "EVS recovery of messages complete, transitioning to operational.\n");
  1161. /*
  1162. * EVS recovery complete, reset local variables
  1163. */
  1164. gmi_arut = 0;
  1165. gmi_adut_old = gmi_adut;
  1166. gmi_adut = 0;
  1167. gmi_highest_seq_old = gmi_highest_seq;
  1168. gmi_highest_seq = 0;
  1169. last_group_arut = 0;
  1170. sq_reinit (&queue_rtr_items, 0);
  1171. memb_failed_list_entries = 0;
  1172. memb_state = MEMB_STATE_OPERATIONAL;
  1173. qsort (memb_form_token.member_list, memb_form_token.member_list_entries,
  1174. sizeof (struct in_addr), in_addr_compare);
  1175. /*
  1176. * Determine transitional configuration
  1177. */
  1178. for (i = 0; i < memb_list_entries_confchg; i++) {
  1179. for (found = 0, j = 0; j < memb_form_token.member_list_entries; j++) {
  1180. if (memb_list[i].sin_addr.s_addr == memb_form_token.member_list[j].s_addr) {
  1181. found = 1;
  1182. break;
  1183. }
  1184. }
  1185. if (found == 1) {
  1186. trans_memb_list[trans_memb_list_entries].sin_addr.s_addr = memb_list[i].sin_addr.s_addr;
  1187. trans_memb_list[trans_memb_list_entries].sin_family = AF_INET;
  1188. trans_memb_list[trans_memb_list_entries].sin_port = sockaddr_in_mcast.sin_port;
  1189. trans_memb_list_entries += 1;
  1190. }
  1191. }
  1192. /*
  1193. * Determine nodes that left the configuration
  1194. */
  1195. for (i = 0; i < memb_list_entries_confchg; i++) {
  1196. for (found = 0, j = 0; j < memb_form_token.member_list_entries; j++) {
  1197. if (memb_list[i].sin_addr.s_addr == memb_form_token.member_list[j].s_addr) {
  1198. found = 1;
  1199. break; /* for j = 0 */
  1200. }
  1201. }
  1202. /*
  1203. * Node left membership, add it to list
  1204. */
  1205. if (found == 0) {
  1206. left_list[left_list_entries].sin_addr.s_addr = memb_list[i].sin_addr.s_addr;
  1207. left_list[left_list_entries].sin_family = AF_INET;
  1208. left_list[left_list_entries].sin_port = sockaddr_in_mcast.sin_port;
  1209. left_list_entries += 1;
  1210. }
  1211. }
  1212. /*
  1213. * MAIN STEP:
  1214. * Deliver transitional configuration
  1215. */
  1216. if (gmi_confchg_fn &&
  1217. (trans_memb_list_entries != memb_list_entries ||
  1218. (memcmp (trans_memb_list, memb_list, sizeof (struct sockaddr_in) * memb_list_entries) != 0))) {
  1219. gmi_confchg_fn (trans_memb_list, trans_memb_list_entries,
  1220. left_list, left_list_entries,
  1221. 0, 0);
  1222. }
  1223. /*
  1224. * Determine nodes that joined the configuration
  1225. */
  1226. for (i = 0; i < memb_form_token.member_list_entries; i++) {
  1227. for (found = 0, j = 0; j < memb_list_entries_confchg; j++) {
  1228. if (memb_form_token.member_list[i].s_addr == memb_list[j].sin_addr.s_addr) {
  1229. found = 1;
  1230. break; /* for j = 0 */
  1231. }
  1232. }
  1233. /*
  1234. * Node joined membership, add it to list
  1235. */
  1236. if (found == 0) {
  1237. joined_list[joined_list_entries].sin_addr.s_addr = memb_form_token.member_list[i].s_addr;
  1238. joined_list[joined_list_entries].sin_family = AF_INET;
  1239. joined_list[joined_list_entries].sin_port = sockaddr_in_mcast.sin_port;
  1240. joined_list_entries += 1;
  1241. }
  1242. }
  1243. /*
  1244. * Install the form token's configuration into the local membership
  1245. */
  1246. for (i = 0; i < memb_form_token.member_list_entries; i++) {
  1247. memb_list[i].sin_addr.s_addr = memb_form_token.member_list[i].s_addr;
  1248. memb_list[i].sin_family = AF_INET;
  1249. memb_list[i].sin_port = sockaddr_in_mcast.sin_port;
  1250. }
  1251. /*
  1252. * Install pending delivery queues
  1253. */
  1254. memb_list_entries = memb_form_token.member_list_entries;
  1255. memb_list_entries_confchg = memb_list_entries;
  1256. queues_pend_delv_memb_new ();
  1257. /*
  1258. * Install new conf id
  1259. */
  1260. memcpy (&memb_conf_id, &memb_form_token.conf_id,
  1261. sizeof (struct memb_conf_id));
  1262. memcpy (&memb_form_token_conf_id, &memb_form_token.conf_id,
  1263. sizeof (struct memb_conf_id));
  1264. /*
  1265. * Deliver regular configuration
  1266. */
  1267. if (gmi_confchg_fn) {
  1268. gmi_confchg_fn (memb_list, memb_list_entries,
  1269. left_list, 0,
  1270. joined_list, joined_list_entries);
  1271. }
  1272. }
  1273. return (0);
  1274. }
  1275. int gwin = 90;
  1276. int pwin = 45;
  1277. static int orf_fcc_allowed (struct orf_token *token)
  1278. {
  1279. int allowed;
  1280. if (memb_state != MEMB_STATE_OPERATIONAL) {
  1281. return (0);
  1282. }
  1283. allowed = gwin + pwin - token->fcc;
  1284. if (allowed < 0) {
  1285. allowed = 0;
  1286. }
  1287. if (allowed > gwin) {
  1288. allowed = gwin;
  1289. }
  1290. if (allowed > pwin) {
  1291. allowed = pwin;
  1292. }
  1293. return (allowed);
  1294. }
  1295. void timer_function_form_token_timeout (void *data)
  1296. {
  1297. gmi_log_printf (gmi_log_level_warning, "Token loss in FORM state\n");
  1298. memb_list_entries = 1;
  1299. /*
  1300. * Add highest rep to failed list to ensure termination
  1301. */
  1302. memb_failed_list[memb_failed_list_entries++].s_addr =
  1303. memb_form_token.rep_list[memb_form_token.rep_list_entries].s_addr;
  1304. memb_state_gather_enter ();
  1305. }
  1306. void orf_timer_function_token_timeout (void *data)
  1307. {
  1308. switch (memb_state) {
  1309. case MEMB_STATE_OPERATIONAL:
  1310. gmi_log_printf (gmi_log_level_warning, "Token loss in OPERATIONAL.\n");
  1311. memb_conf_id.rep.s_addr = memb_local_sockaddr_in.sin_addr.s_addr;
  1312. memb_list_entries = 1;
  1313. memb_state_gather_enter ();
  1314. break;
  1315. case MEMB_STATE_GATHER:
  1316. case MEMB_STATE_COMMIT:
  1317. gmi_log_printf (gmi_log_level_warning, "Token loss in GATHER or COMMIT.\n");
  1318. memb_list_entries = 1;
  1319. break;
  1320. case MEMB_STATE_EVS:
  1321. gmi_log_printf (gmi_log_level_warning, "Token loss in EVS state\n");
  1322. memb_list_entries = 1;
  1323. memb_state_gather_enter ();
  1324. break;
  1325. default:
  1326. printf ("token loss in form state doesn't make sense here\n");
  1327. break;
  1328. }
  1329. }
  1330. /*
  1331. * Send orf_token to next member (requires orf_token)
  1332. */
  1333. static int orf_token_send (
  1334. struct orf_token *orf_token)
  1335. {
  1336. struct msghdr msg_orf_token;
  1337. struct iovec iovec_orf_token;
  1338. int res;
  1339. poll_timer_delete (*gmi_poll_handle, timer_orf_token_timeout);
  1340. poll_timer_add (*gmi_poll_handle, TIMEOUT_TOKEN, 0,
  1341. orf_timer_function_token_timeout, &timer_orf_token_timeout);
  1342. iovec_orf_token.iov_base = (char *)orf_token;
  1343. iovec_orf_token.iov_len = sizeof (struct orf_token);
  1344. msg_orf_token.msg_name = (caddr_t)&memb_next;
  1345. msg_orf_token.msg_namelen = sizeof (struct sockaddr_in);
  1346. msg_orf_token.msg_iov = &iovec_orf_token;
  1347. msg_orf_token.msg_iovlen = 1;
  1348. msg_orf_token.msg_control = 0;
  1349. msg_orf_token.msg_controllen = 0;
  1350. msg_orf_token.msg_flags = 0;
  1351. // THIS IS FOR TESTING ERRORS IN THE EVS STATE
  1352. //if ((memb_state == MEMB_STATE_EVS) && ((random () % 3) == 0)) {
  1353. //gmi_log_printf (gmi_log_level_debug, "CAUSING TOKEN LOSS AT EVS STATE\n");
  1354. // return (1);
  1355. //}
  1356. res = sendmsg (gmi_fd_token, &msg_orf_token, MSG_NOSIGNAL);
  1357. assert (res != -1);
  1358. /*
  1359. * res not used here errors are handled by algorithm
  1360. */
  1361. gmi_last_seqid = orf_token->header.seqid;
  1362. stats_sent += res;
  1363. return (res);
  1364. }
  1365. int orf_token_send_initial (void)
  1366. {
  1367. struct orf_token orf_token;
  1368. int res;
  1369. orf_token.header.seqid = 0;
  1370. orf_token.header.type = MESSAGE_TYPE_ORF_TOKEN;
  1371. orf_token.group_arut = gmi_highest_seq;
  1372. orf_token.addr_arut.s_addr = gmi_bound_to.sin_addr.s_addr;
  1373. orf_token.fcc = 0;
  1374. orf_token.rtr_list_entries = 0;
  1375. memset (orf_token.rtr_list, 0, sizeof (struct rtr_item) * RTR_TOKEN_SIZE_MAX);
  1376. res = orf_token_send (&orf_token);
  1377. return (res);
  1378. }
  1379. /*
  1380. * Membership Management
  1381. */
  1382. static int memb_join_send (void)
  1383. {
  1384. struct msghdr msghdr_join;
  1385. struct iovec iovec_join;
  1386. int res;
  1387. memb_join.header.seqid = 0;
  1388. memb_join.header.type = MESSAGE_TYPE_MEMB_JOIN;
  1389. /*
  1390. * copy current gather list to representatives list
  1391. */
  1392. if ((memb_gather_set_entries == memb_join.active_rep_list_entries) &&
  1393. (memcmp (memb_join.active_rep_list, memb_gather_set,
  1394. sizeof (struct in_addr) * memb_gather_set_entries) == 0) &&
  1395. (memb_failed_list_entries == memb_join.failed_rep_list_entries) &&
  1396. (memcmp (memb_join.failed_rep_list, memb_failed_list,
  1397. sizeof (struct in_addr) * memb_failed_list_entries) == 0)) {
  1398. return (0);
  1399. }
  1400. /*
  1401. * Copy active reps
  1402. */
  1403. memcpy (memb_join.active_rep_list, memb_gather_set,
  1404. sizeof (struct in_addr) * memb_gather_set_entries);
  1405. memb_join.active_rep_list_entries = memb_gather_set_entries;
  1406. /*
  1407. * Copy failed reps
  1408. */
  1409. memcpy (memb_join.failed_rep_list, memb_failed_list,
  1410. sizeof (struct in_addr) * memb_failed_list_entries);
  1411. memb_join.failed_rep_list_entries = memb_failed_list_entries;
  1412. iovec_join.iov_base = (char *)&memb_join;
  1413. iovec_join.iov_len = sizeof (struct memb_join);
  1414. msghdr_join.msg_name = (caddr_t)&sockaddr_in_mcast;
  1415. msghdr_join.msg_namelen = sizeof (struct sockaddr_in);
  1416. msghdr_join.msg_iov = &iovec_join;
  1417. msghdr_join.msg_iovlen = 1;
  1418. msghdr_join.msg_control = 0;
  1419. msghdr_join.msg_controllen = 0;
  1420. msghdr_join.msg_flags = 0;
  1421. res = sendmsg (gmi_fd_mcast, &msghdr_join, MSG_NOSIGNAL | MSG_DONTWAIT);
  1422. return (res);
  1423. }
  1424. static int memb_state_commit_enter (void);
  1425. /*
  1426. * Update gather_set[0].join_reps with list of failed members
  1427. */
  1428. void memb_gather_set_update_failed (struct in_addr *list, int list_entries)
  1429. {
  1430. int i;
  1431. int j;
  1432. /*
  1433. * Remove failed members from gather set
  1434. */
  1435. for (i = 0; i < list_entries; i++) {
  1436. for (j = 0; j < memb_gather_set_entries; j++) {
  1437. if (list[i].s_addr == memb_gather_set[j].s_addr) {
  1438. memb_gather_set_entries -= 1;
  1439. memcpy (&memb_gather_set[j],
  1440. &memb_gather_set[j + 1],
  1441. memb_gather_set_entries * sizeof (struct in_addr));
  1442. break; /* for j = 0 */
  1443. }
  1444. }
  1445. }
  1446. }
  1447. static void memb_timer_function_state_commit_timeout (void *data)
  1448. {
  1449. int i;
  1450. int j;
  1451. int k;
  1452. int found;
  1453. int add_to_failed = 1;
  1454. struct sockaddr_in left_list[MAX_MEMBERS];
  1455. int left_list_entries = 0;
  1456. memb_failed_list_entries = 0;
  1457. /*
  1458. * No entries responded in commit timeout period
  1459. */
  1460. if (memb_commit_set_entries == 0) {
  1461. /*
  1462. * memb_list_entries only set to 0 when token times out, in which case
  1463. * send a configuration change because no messages can be recovered in EVS
  1464. */
  1465. if (memb_list_entries == 1) {
  1466. gmi_log_printf (gmi_log_level_notice, "I am the only member.\n");
  1467. if (gmi_confchg_fn) {
  1468. /*
  1469. * Determine nodes that left the configuration
  1470. */
  1471. for (i = 0; i < memb_list_entries_confchg; i++) {
  1472. if (memb_local_sockaddr_in.sin_addr.s_addr != memb_list[i].sin_addr.s_addr) {
  1473. left_list[left_list_entries].sin_addr.s_addr = memb_list[i].sin_addr.s_addr;
  1474. left_list[left_list_entries].sin_family = AF_INET;
  1475. left_list[left_list_entries].sin_port = sockaddr_in_mcast.sin_port;
  1476. left_list_entries += 1;
  1477. }
  1478. }
  1479. gmi_confchg_fn (&memb_local_sockaddr_in, 1,
  1480. left_list, left_list_entries,
  1481. 0, 0);
  1482. }
  1483. } else {
  1484. gmi_log_printf (gmi_log_level_notice, "No members sent join, keeping old ring and transitioning to operational.\n");
  1485. }
  1486. memb_state = MEMB_STATE_OPERATIONAL;
  1487. return;
  1488. }
  1489. /*
  1490. * Find all failed members
  1491. */
  1492. for (i = 0; i < memb_gather_set_entries; i++) {
  1493. add_to_failed = 1;
  1494. for (j = 0; j < memb_commit_set_entries; j++) {
  1495. /*
  1496. * If gather entry not in commit rep list, add to failed
  1497. */
  1498. if (memb_gather_set[i].s_addr == memb_commit_set[j].rep.sin_addr.s_addr) {
  1499. add_to_failed = 0;
  1500. break; /* for found = 0 */
  1501. }
  1502. }
  1503. /*
  1504. * If gather entry not in commit set, add to failed set
  1505. */
  1506. for (found = 0, j = 0; j < memb_commit_set_entries; j++) {
  1507. for (k = 0; k < memb_commit_set[j].join_rep_list_entries; k++) {
  1508. if (memb_gather_set[i].s_addr == memb_commit_set[j].join_rep_list[k].s_addr) {
  1509. found = 1;
  1510. break;
  1511. }
  1512. }
  1513. if (found == 0) {
  1514. add_to_failed = 1;
  1515. break;
  1516. }
  1517. }
  1518. /*
  1519. * If local address, item found
  1520. */
  1521. if (memb_gather_set[i].s_addr == memb_local_sockaddr_in.sin_addr.s_addr) {
  1522. add_to_failed = 0;
  1523. }
  1524. if (add_to_failed == 1) {
  1525. memb_failed_list[memb_failed_list_entries++].s_addr =
  1526. memb_gather_set[i].s_addr;
  1527. }
  1528. }
  1529. memb_gather_set_update_failed (memb_failed_list, memb_failed_list_entries);
  1530. memb_state_commit_enter ();
  1531. }
  1532. static int memb_state_commit_enter (void)
  1533. {
  1534. int res;
  1535. memb_state = MEMB_STATE_COMMIT;
  1536. memb_commit_set_entries = 0;
  1537. res = memb_join_send();
  1538. poll_timer_delete (*gmi_poll_handle, timer_memb_state_gather_timeout);
  1539. timer_memb_state_gather_timeout = 0;
  1540. poll_timer_add (*gmi_poll_handle, TIMEOUT_STATE_COMMIT, 0,
  1541. memb_timer_function_state_commit_timeout, &timer_memb_state_commit_timeout);
  1542. return (res);
  1543. }
  1544. static void memb_timer_function_state_gather (void *data)
  1545. {
  1546. int i;
  1547. /*
  1548. * GATHER period expired, sort gather sets and send JOIN
  1549. */
  1550. memb_state_commit_enter ();
  1551. gmi_log_printf (gmi_log_level_debug, "GATHER timeout:\n");
  1552. for (i = 0; i < memb_gather_set_entries; i++) {
  1553. gmi_log_printf (gmi_log_level_debug, "host %d attempted to join %s\n", i, inet_ntoa (memb_gather_set[i]));
  1554. }
  1555. }
  1556. static void memb_print_commit_set (void)
  1557. {
  1558. int i, j;
  1559. gmi_log_printf (gmi_log_level_debug, "Gather list\n");
  1560. for (i = 0; i < memb_gather_set_entries; i++) {
  1561. gmi_log_printf (gmi_log_level_debug, "\tmember %d %s\n", i, inet_ntoa (memb_gather_set[i]));
  1562. }
  1563. for (i = 0; i < memb_commit_set_entries; i++) {
  1564. gmi_log_printf (gmi_log_level_debug, "Join from rep %d %s\n", i, inet_ntoa (memb_commit_set[i].rep.sin_addr));
  1565. for (j = 0; j < memb_commit_set[i].join_rep_list_entries; j++) {
  1566. gmi_log_printf (gmi_log_level_debug, "\tmember %d %s\n", j, inet_ntoa (memb_commit_set[i].join_rep_list[j]));
  1567. }
  1568. }
  1569. }
  1570. /*
  1571. * Determine if the commit phase has reached consensus
  1572. */
  1573. static int memb_state_consensus_commit (void)
  1574. {
  1575. int found;
  1576. int res;
  1577. int i, j;
  1578. /*
  1579. * Determine consensus
  1580. */
  1581. /*
  1582. * If all commit sets don't match gather set, no consensus
  1583. */
  1584. for (i = 0; i < memb_commit_set_entries; i++) {
  1585. /*
  1586. * If not same number of entries, no consensus
  1587. */
  1588. res = memb_gather_set_entries - memb_commit_set[i].join_rep_list_entries;
  1589. if (res != 0) {
  1590. return (0); /* no consensus */
  1591. }
  1592. /*
  1593. * If entries dont match, no consensus
  1594. */
  1595. res = memcmp (memb_gather_set, memb_commit_set[i].join_rep_list,
  1596. memb_gather_set_entries * sizeof (struct in_addr));
  1597. if (res != 0) {
  1598. return (0); /* no consensus */
  1599. }
  1600. }
  1601. /*
  1602. * If all reps from gather set represented in commit set, consensus
  1603. */
  1604. for (i = 0; i < memb_gather_set_entries; i++) {
  1605. found = 0;
  1606. for (j = 0; j < memb_commit_set_entries; j++) {
  1607. if (memb_gather_set[i].s_addr == memb_local_sockaddr_in.sin_addr.s_addr) {
  1608. found = 1;
  1609. break;
  1610. }
  1611. if (memb_gather_set[i].s_addr == memb_commit_set[j].rep.sin_addr.s_addr) {
  1612. found = 1;
  1613. break;
  1614. }
  1615. }
  1616. if (found == 0) {
  1617. return (0); /* no consensus, rep not found from gather set */
  1618. }
  1619. }
  1620. return (1); /* got consensus! */
  1621. }
  1622. /*
  1623. * Union commit_set_entry into gather set
  1624. */
  1625. static void memb_state_commit_union (int commit_set_entry)
  1626. {
  1627. int found;
  1628. int i, j;
  1629. for (i = 0; i < memb_commit_set[commit_set_entry].join_rep_list_entries; i++) {
  1630. for (found = 0, j = 0; j < memb_gather_set_entries; j++) {
  1631. if (memb_commit_set[commit_set_entry].join_rep_list[i].s_addr ==
  1632. memb_gather_set[j].s_addr) {
  1633. found = 1;
  1634. break;
  1635. }
  1636. }
  1637. if (found == 0) {
  1638. memb_gather_set[memb_gather_set_entries++].s_addr =
  1639. memb_commit_set[commit_set_entry].join_rep_list[i].s_addr;
  1640. /*
  1641. * Sort gather set
  1642. */
  1643. qsort (memb_gather_set, memb_gather_set_entries,
  1644. sizeof (struct in_addr), in_addr_compare);
  1645. }
  1646. }
  1647. }
  1648. static void memb_conf_id_build (
  1649. struct memb_conf_id *memb_conf_id,
  1650. struct in_addr memb_local_rep)
  1651. {
  1652. gettimeofday (&memb_conf_id->tv, NULL);
  1653. memb_conf_id->rep.s_addr = memb_local_rep.s_addr;
  1654. }
  1655. static void memb_form_token_update_highest_seq (
  1656. struct memb_form_token *form_token)
  1657. {
  1658. struct conf_desc *conf_desc;
  1659. int entry;
  1660. int found = 0;
  1661. for (entry = 0; entry < form_token->conf_desc_list_entries; entry++) {
  1662. if (memcmp (&form_token->conf_desc_list[entry].conf_id,
  1663. &memb_form_token_conf_id, sizeof (struct memb_conf_id)) == 0) {
  1664. found = 1;
  1665. break;
  1666. }
  1667. }
  1668. conf_desc = &form_token->conf_desc_list[entry];
  1669. if (found && gmi_highest_seq < conf_desc->highest_seq) {
  1670. gmi_highest_seq = conf_desc->highest_seq;
  1671. }
  1672. }
  1673. static void memb_form_token_conf_desc_build (
  1674. struct memb_form_token *form_token)
  1675. {
  1676. struct conf_desc *conf_desc;
  1677. int found = 0;
  1678. int entry = 0;
  1679. /*
  1680. * Determine if local configuration id is already present in form token
  1681. */
  1682. for (entry = 0; entry < form_token->conf_desc_list_entries; entry++) {
  1683. if (memcmp (&form_token->conf_desc_list[entry].conf_id,
  1684. &memb_form_token_conf_id, sizeof (struct memb_conf_id)) == 0) {
  1685. found = 1;
  1686. break;
  1687. }
  1688. }
  1689. conf_desc = &form_token->conf_desc_list[entry];
  1690. if (found == 0) {
  1691. /*
  1692. * Item not present, add item
  1693. */
  1694. conf_desc->highest_seq = gmi_highest_seq;
  1695. conf_desc->arut = gmi_arut;
  1696. // TODO holes not currently implemented conf_desc->hole_list_entries = 0;
  1697. memcpy (&conf_desc->conf_id,
  1698. &memb_form_token_conf_id, sizeof (struct memb_conf_id));
  1699. form_token->conf_desc_list_entries += 1;
  1700. } else {
  1701. /*
  1702. * Item already present, update arut, highest seq
  1703. */
  1704. if (conf_desc->arut > gmi_arut) {
  1705. conf_desc->arut = gmi_arut;
  1706. }
  1707. if (gmi_highest_seq > conf_desc->highest_seq) {
  1708. conf_desc->highest_seq = gmi_highest_seq;
  1709. }
  1710. }
  1711. #ifdef COMPILE_OUT
  1712. /*
  1713. * Build conf_desc->hole_list
  1714. */
  1715. printf ("conf desc build %d %d\n", gmi_arut, gmi_highest_seq);
  1716. conf_desc->hole_list_entries = 0;
  1717. for (i = gmi_arut; i < gmi_highest_seq; i++) {
  1718. assert (conf_desc->hole_list_entries < HOLE_LIST_MAX);
  1719. res = sq_item_get (&queue_rtr_items, i, (void **)&gmi_rtr_item_p);
  1720. if (res == 0) {
  1721. /*
  1722. * If item present, delete from hole list if it exists
  1723. */
  1724. for (j = 0; j < conf_desc->hole_list_entries; j++) {
  1725. if (conf_desc->hole_list[j] == i) {
  1726. memmove (&conf_desc->hole_list[j], &conf_desc->hole_list[j + 1],
  1727. sizeof (int) * (conf_desc->hole_list_entries - j - 1));
  1728. conf_desc->hole_list_entries -= 1;
  1729. printf ("reducing setting desc entries to %d\n", conf_desc->hole_list_entries);
  1730. break; /* from for (j = ... ) */
  1731. }
  1732. }
  1733. } else {
  1734. /*
  1735. * If item not present, add to hole list
  1736. */
  1737. conf_desc->hole_list[conf_desc->hole_list_entries] = i;
  1738. conf_desc->hole_list_entries += 1;
  1739. printf ("increasing setting desc entries to %d %d\n", conf_desc->hole_list_entries, i);
  1740. }
  1741. }
  1742. printf ("Conf desc build done\n");
  1743. #endif
  1744. }
  1745. static int memb_form_token_send (
  1746. struct memb_form_token *form_token)
  1747. {
  1748. struct msghdr msg_form_token;
  1749. struct iovec iovec_form_token;
  1750. int res;
  1751. /*
  1752. * Build message for sendmsg
  1753. */
  1754. iovec_form_token.iov_base = (char *)form_token;
  1755. iovec_form_token.iov_len = sizeof (struct memb_form_token);
  1756. msg_form_token.msg_name = (caddr_t)&memb_next;
  1757. msg_form_token.msg_namelen = sizeof (struct sockaddr_in);
  1758. msg_form_token.msg_iov = &iovec_form_token;
  1759. msg_form_token.msg_iovlen = 1;
  1760. msg_form_token.msg_control = 0;
  1761. msg_form_token.msg_controllen = 0;
  1762. msg_form_token.msg_flags = 0;
  1763. res = sendmsg (gmi_fd_token, &msg_form_token, MSG_NOSIGNAL | MSG_DONTWAIT);
  1764. /*
  1765. * res not used here, because orf token errors are handled by algorithm
  1766. */
  1767. stats_sent += res;
  1768. poll_timer_delete (*gmi_poll_handle, timer_orf_token_timeout);
  1769. timer_orf_token_timeout = 0;
  1770. poll_timer_delete (*gmi_poll_handle, timer_form_token_timeout);
  1771. poll_timer_add (*gmi_poll_handle, TIMEOUT_TOKEN, 0,
  1772. timer_function_form_token_timeout, &timer_form_token_timeout);
  1773. return (res);
  1774. }
  1775. int memb_form_token_send_initial (void)
  1776. {
  1777. struct memb_form_token form_token;
  1778. int res;
  1779. int i;
  1780. memset (&form_token, 0x00, sizeof (struct memb_form_token));
  1781. memb_state = MEMB_STATE_FORM;
  1782. /*
  1783. * Build form token
  1784. */
  1785. form_token.header.type = MESSAGE_TYPE_MEMB_FORM_TOKEN;
  1786. memcpy (form_token.rep_list,
  1787. memb_gather_set,
  1788. memb_gather_set_entries * sizeof (struct in_addr));
  1789. form_token.rep_list_entries = memb_gather_set_entries;
  1790. /*
  1791. * Add local member to entry
  1792. */
  1793. form_token.member_list[0].s_addr =
  1794. memb_local_sockaddr_in.sin_addr.s_addr;
  1795. form_token.member_list_entries = 1;
  1796. memb_conf_id_build (&form_token.conf_id, memb_local_sockaddr_in.sin_addr);
  1797. form_token.conf_desc_list_entries = 0;
  1798. memb_form_token_conf_desc_build (&form_token);
  1799. /*
  1800. * Send FORM to next member, or if no members in this configuration
  1801. * to next representative
  1802. */
  1803. if (memb_list_entries <= 1) {
  1804. memb_next.sin_addr.s_addr = memb_gather_set[1].s_addr;
  1805. } else {
  1806. for (i = 0; i < memb_list_entries; i++) {
  1807. if (memb_list[i].sin_addr.s_addr == memb_local_sockaddr_in.sin_addr.s_addr) {
  1808. memb_next.sin_addr.s_addr =
  1809. memb_list[i + 1].sin_addr.s_addr;
  1810. break;
  1811. }
  1812. }
  1813. }
  1814. // TODO assertion here about the 1 value
  1815. memb_next.sin_family = AF_INET;
  1816. memb_next.sin_port = sockaddr_in_mcast.sin_port;
  1817. res = memb_form_token_send (&form_token);
  1818. return (res);
  1819. }
  1820. void print_stats (void)
  1821. {
  1822. struct timeval tv_end;
  1823. gettimeofday (&tv_end, NULL);
  1824. gmi_log_printf (gmi_log_level_notice, "Bytes recv %d\n", stats_recv);
  1825. gmi_log_printf (gmi_log_level_notice, "Bytes sent %d\n", stats_sent);
  1826. gmi_log_printf (gmi_log_level_notice, "Messages delivered %d\n", stats_delv);
  1827. gmi_log_printf (gmi_log_level_notice, "Re-Mcasts %d\n", stats_remcasts);
  1828. gmi_log_printf (gmi_log_level_notice, "Tokens process %d\n", stats_orf_token);
  1829. }
  1830. /*
  1831. * Authenticates message using nonce, mac, and message body
  1832. */
  1833. static int gmi_msg_auth (struct iovec *iovec, int iov_len)
  1834. {
  1835. return (0);
  1836. }
  1837. int last_lowered = 1;
  1838. static void calculate_group_arut (struct orf_token *orf_token)
  1839. {
  1840. //printf ("group arut %d local arut %d gmi_gmi_highest seq %d\n", orf_token->group_arut, gmi_arut, gmi_highest_seq);
  1841. //printf ("last %d group arut %d last arut %d arut %d\n", last_lowered, orf_token->group_arut, last_group_arut, gmi_arut);
  1842. /*
  1843. * increase the group arut if we got back the same group
  1844. * because everyone has these messages
  1845. */
  1846. messages_free (orf_token->group_arut);
  1847. if (orf_token->addr_arut.s_addr == gmi_bound_to.sin_addr.s_addr) {
  1848. orf_token->group_arut = gmi_arut;
  1849. }
  1850. if (gmi_arut < orf_token->group_arut) {
  1851. orf_token->group_arut = gmi_arut;
  1852. orf_token->addr_arut.s_addr = gmi_bound_to.sin_addr.s_addr;
  1853. }
  1854. last_group_arut = orf_token->group_arut;
  1855. }
  1856. /*
  1857. * Message Handlers
  1858. */
  1859. /*
  1860. * message handler called when TOKEN message type received
  1861. */
  1862. static int message_handler_orf_token (
  1863. struct sockaddr_in *system_from,
  1864. struct iovec *iovec,
  1865. int iov_len,
  1866. int bytes_received)
  1867. {
  1868. struct orf_token *orf_token;
  1869. int transmits_allowed;
  1870. int starting_group_arut;
  1871. orf_token = iovec[0].iov_base;
  1872. #ifdef PRINT_STATS
  1873. if (orf_token->header.seqid > 10000) {
  1874. print_stats ();
  1875. }
  1876. #endif
  1877. if (memb_state == MEMB_STATE_FORM) {
  1878. gmi_log_printf (gmi_log_level_notice, "swallowing ORF token %d.\n", stats_orf_token);
  1879. poll_timer_delete (*gmi_poll_handle, timer_orf_token_timeout);
  1880. timer_orf_token_timeout = 0;
  1881. return (0);
  1882. }
  1883. //printf ("Got orf token from %s\n", inet_ntoa (system_from->sin_addr));
  1884. starting_group_arut = orf_token->group_arut;
  1885. stats_orf_token++;
  1886. transmits_allowed = orf_fcc_allowed (orf_token);
  1887. //printf ("retransmit allowed %d\n", transmits_allowed);
  1888. /*
  1889. * Retransmit failed messages and request retransmissions
  1890. */
  1891. orf_token_rtr (orf_token, &transmits_allowed);
  1892. //printf ("multicasts allowed %d\n", transmits_allowed);
  1893. /*
  1894. * TODO Ok this is ugly and I dont like it.
  1895. *
  1896. * Flow control to limit number of missing multicast messages
  1897. * on lossy switches, this could cause a large window between
  1898. * what is delivered locally and what is delivered remotely.
  1899. * This window could cause the hole list of the form token to
  1900. * be overrun or cause the form token to be large.
  1901. */
  1902. if ((gmi_brake + MISSING_MCAST_WINDOW) < orf_token->header.seqid) {
  1903. transmits_allowed = 0;
  1904. }
  1905. /*
  1906. * Set the group arut and free any messages that can be freed
  1907. */
  1908. if (memb_state != MEMB_STATE_EVS) {
  1909. calculate_group_arut (orf_token);
  1910. }
  1911. /*
  1912. * Multicast queued messages
  1913. */
  1914. orf_token_mcast (orf_token, transmits_allowed, system_from);
  1915. /*
  1916. * Calculate flow control count
  1917. */
  1918. orf_token_fcc (orf_token);
  1919. /*
  1920. * Deliver membership and messages required by EVS
  1921. */
  1922. orf_token_evs (orf_token, starting_group_arut);
  1923. if (memb_state == MEMB_STATE_EVS) {
  1924. calculate_group_arut (orf_token);
  1925. }
  1926. /*
  1927. * Transmit orf_token to next member
  1928. */
  1929. orf_token_send (orf_token);
  1930. return (0);
  1931. }
  1932. static int memb_state_gather_enter (void) {
  1933. struct msghdr msghdr_attempt_join;
  1934. struct iovec iovec_attempt_join;
  1935. struct memb_attempt_join memb_attempt_join;
  1936. int res = 0;
  1937. gmi_log_printf (gmi_log_level_notice, "entering GATHER state.\n");
  1938. memb_state = MEMB_STATE_GATHER;
  1939. /*
  1940. * Join message starts with no entries
  1941. */
  1942. memb_join.active_rep_list_entries = 0;
  1943. memb_join.failed_rep_list_entries = 0;
  1944. /*
  1945. * Copy local host info
  1946. */
  1947. memb_gather_set[0].s_addr = memb_local_sockaddr_in.sin_addr.s_addr;
  1948. memb_gather_set_entries = 1;
  1949. /*
  1950. * If this node is the representative, send attempt join
  1951. */
  1952. if (memb_local_sockaddr_in.sin_addr.s_addr == memb_conf_id.rep.s_addr) {
  1953. gmi_log_printf (gmi_log_level_notice, "SENDING attempt join because this node is ring rep.\n");
  1954. memb_attempt_join.header.seqid = 0;
  1955. memb_attempt_join.header.type = MESSAGE_TYPE_MEMB_ATTEMPT_JOIN;
  1956. iovec_attempt_join.iov_base = &memb_attempt_join;
  1957. iovec_attempt_join.iov_len = sizeof (struct memb_attempt_join);
  1958. msghdr_attempt_join.msg_name = &sockaddr_in_mcast;
  1959. msghdr_attempt_join.msg_namelen = sizeof (struct sockaddr_in);
  1960. msghdr_attempt_join.msg_iov = &iovec_attempt_join;
  1961. msghdr_attempt_join.msg_iovlen = 1;
  1962. msghdr_attempt_join.msg_control = 0;
  1963. msghdr_attempt_join.msg_controllen = 0;
  1964. msghdr_attempt_join.msg_flags = 0;
  1965. res = sendmsg (gmi_fd_mcast, &msghdr_attempt_join, MSG_NOSIGNAL | MSG_DONTWAIT);
  1966. /*
  1967. * res not checked here, there is nothing that can be done
  1968. * instead rely on the algorithm to recover from faults
  1969. */
  1970. }
  1971. poll_timer_delete (*gmi_poll_handle, timer_memb_state_gather_timeout);
  1972. poll_timer_add (*gmi_poll_handle, TIMEOUT_STATE_GATHER, 0,
  1973. memb_timer_function_state_gather, &timer_memb_state_gather_timeout);
  1974. return (res);
  1975. }
  1976. struct pend_delv *pend_delv_next_delivery_find (void)
  1977. {
  1978. struct pend_delv *pend_delv = 0;
  1979. int i;
  1980. /*
  1981. * Find first_delivery queue that is not mepty
  1982. * this sets the first pend_delv
  1983. */
  1984. for (i = 0; i < memb_list_entries_confchg; i++) {
  1985. if (queues_pend_delv[i].first_delivery &&
  1986. queue_is_empty (&queues_pend_delv[i].queue) == 0) {
  1987. pend_delv = &queues_pend_delv[i];
  1988. // printf ("Selecting first queue %s\n", inet_ntoa (pend_delv->ip));
  1989. break;
  1990. }
  1991. }
  1992. /*
  1993. * Search remaining pend_delv for first deliveries with
  1994. * smaller sequence numbers
  1995. */
  1996. for (++i; i < memb_list_entries_confchg; i++) {
  1997. assert (pend_delv);
  1998. if (queues_pend_delv[i].first_delivery &&
  1999. (queue_is_empty (&queues_pend_delv[i].queue) == 0) &&
  2000. (queues_pend_delv[i].seqid < pend_delv->seqid)) {
  2001. pend_delv = &queues_pend_delv[i];
  2002. // printf ("Selecting first queue %s\n", inet_ntoa (pend_delv->ip));
  2003. }
  2004. }
  2005. /*
  2006. * Found first_delivery queue that wasn't empty, return it
  2007. */
  2008. if (pend_delv) {
  2009. return (pend_delv);
  2010. }
  2011. /*
  2012. * No first delivery queues, repeat same
  2013. * process looking for any queue
  2014. */
  2015. for (i = 0; i < memb_list_entries_confchg; i++) {
  2016. if (queue_is_empty (&queues_pend_delv[i].queue) == 0) {
  2017. pend_delv = &queues_pend_delv[i];
  2018. break;
  2019. }
  2020. }
  2021. /*
  2022. * Find lowest sequence number queue
  2023. */
  2024. for (++i; i < memb_list_entries_confchg; i++) {
  2025. assert (pend_delv);
  2026. if ((queue_is_empty (&queues_pend_delv[i].queue) == 0) &&
  2027. (queues_pend_delv[i].seqid < pend_delv->seqid)) {
  2028. pend_delv = &queues_pend_delv[i];
  2029. }
  2030. }
  2031. return (pend_delv);
  2032. }
  2033. static int user_deliver ()
  2034. {
  2035. struct gmi_pend_delv_item *pend_delv_item;
  2036. int i = 0;
  2037. int res = 0;
  2038. struct iovec iovec_delv[256];
  2039. int iov_len_delv = 0;
  2040. struct mcast *mcast = 0;
  2041. int messages_delivered = 0;
  2042. struct pend_delv *pend_delv;
  2043. int retval = 0;
  2044. /*
  2045. * Find pend_delv with lowest sequence number. This pend_delv is
  2046. * the queue that should be delivered from next
  2047. */
  2048. pend_delv = pend_delv_next_delivery_find ();
  2049. //printf ("Delivering from queue %s\n", inet_ntoa (pend_delv->ip));
  2050. /*
  2051. * If a message was not assembled on the queue with the lowest
  2052. * sequence number, return since there is no reason to attempt assembly.
  2053. */
  2054. memset (iovec_delv, 0, sizeof (iovec_delv));
  2055. queue_item_iterator_init (&pend_delv->queue);
  2056. assert (queue_is_empty (&pend_delv->queue) == 0);
  2057. //printf ("Starting a packet assembly\n");
  2058. do {
  2059. pend_delv_item = queue_item_iterator_get (&pend_delv->queue);
  2060. mcast = pend_delv_item->iovec[0].iov_base;
  2061. assert (pend_delv_item);
  2062. assert (pend_delv_item->iovec[0].iov_len < MESSAGE_SIZE_MAX);
  2063. assert (pend_delv_item->iovec[0].iov_len != 0);
  2064. assert (pend_delv_item->iovec[0].iov_base != 0);
  2065. assert (mcast != (struct mcast *)0xdeadbeef);
  2066. assert (pend_delv->ip.s_addr == mcast->source.s_addr);
  2067. messages_delivered += 1;
  2068. /*
  2069. * Assemble io vector
  2070. */
  2071. if (pend_delv_item->iovec[0].iov_len == sizeof (struct mcast)) {
  2072. /*
  2073. * Copy iovec from second iovec if this is self-delivered
  2074. */
  2075. memcpy (&iovec_delv[iov_len_delv],
  2076. &pend_delv_item->iovec[1],
  2077. sizeof (struct iovec) * pend_delv_item->iov_len - 1);
  2078. iov_len_delv += pend_delv_item->iov_len - 1;
  2079. } else {
  2080. /*
  2081. * Copy iovec from first iovec if this is an external message
  2082. */
  2083. iovec_delv[iov_len_delv].iov_base =
  2084. pend_delv_item->iovec[0].iov_base + sizeof (struct mcast);
  2085. iovec_delv[iov_len_delv].iov_len =
  2086. pend_delv_item->iovec[0].iov_len - sizeof (struct mcast);
  2087. assert (iovec_delv[iov_len_delv].iov_len < MESSAGE_SIZE_MAX);
  2088. iov_len_delv += 1;
  2089. if (pend_delv_item->iov_len > 1) {
  2090. memcpy (&iovec_delv[iov_len_delv],
  2091. &pend_delv_item->iovec[1],
  2092. sizeof (struct iovec) * pend_delv_item->iov_len - 1);
  2093. iov_len_delv += pend_delv_item->iov_len - 1;
  2094. }
  2095. }
  2096. assert (iov_len_delv < 256);
  2097. assert (iov_len_delv > 0);
  2098. //printf ("Assembling from packet %d of %d of total %d\n",
  2099. // mcast->packet_number, mcast->packet_count, mcast->packet_seq);
  2100. /*
  2101. * Deliver message if this is the last packet
  2102. */
  2103. if (mcast->packet_number == mcast->packet_count) {
  2104. gmi_log_printf (gmi_log_level_debug, "Last packet, delivering iovec %d entries seq %d\n",
  2105. iov_len_delv, i);
  2106. gmi_deliver_fn (
  2107. &mcast->groupname,
  2108. iovec_delv,
  2109. iov_len_delv);
  2110. /*
  2111. * On the first message delivery:
  2112. * Free items in the pending queue up to the barrier message
  2113. * set gmi_adut to rut so that message_free may free any messages.
  2114. */
  2115. if (pend_delv->first_delivery) {
  2116. // printf ("releasing all messages up to %d\n", gmi_adut);
  2117. // TODO actually release the messages from the previous configuration
  2118. // TODO without a fix here, those messages are leaked
  2119. }
  2120. /*
  2121. * Because of the ordering guarantees, we are guaranteed that
  2122. * pend_delv->seqid on every invocation of user_deliver shall
  2123. * increase (or reset to zero). This allows us to set the
  2124. * low water mark (gmi_adut) for freeing of messages to atleast
  2125. * the beginning of this message.
  2126. */
  2127. gmi_adut = pend_delv->seqid;
  2128. /*
  2129. * Determine if there are more messages on this queue
  2130. */
  2131. res = queue_item_iterator_next (&pend_delv->queue);
  2132. if (res == 0) {
  2133. /*
  2134. * More items to deliver set queues seqid head so
  2135. * correct pending queue can be selected next time
  2136. */
  2137. pend_delv_item = queue_item_iterator_get (&pend_delv->queue);
  2138. mcast = pend_delv_item->iovec[0].iov_base;
  2139. pend_delv->seqid = mcast->header.seqid;
  2140. for (i = 0; i < messages_delivered; i++) {
  2141. queue_item_remove (&pend_delv->queue);
  2142. }
  2143. } else {
  2144. /*
  2145. * No more items to deliver
  2146. */
  2147. pend_delv->seqid = 0;
  2148. queue_reinit (&pend_delv->queue);
  2149. }
  2150. retval = 1;
  2151. break; /* From do loop */
  2152. }
  2153. res = queue_item_iterator_next (&pend_delv->queue);
  2154. } while (res == 0);
  2155. return (retval);
  2156. }
  2157. struct pend_delv *pend_delv_find (struct in_addr source)
  2158. {
  2159. struct pend_delv *pend_delv = 0;
  2160. int i;
  2161. for (i = 0; i < memb_list_entries_confchg; i++) {
  2162. if (source.s_addr == queues_pend_delv[i].ip.s_addr) {
  2163. pend_delv = &queues_pend_delv[i];
  2164. break;
  2165. }
  2166. }
  2167. return (pend_delv);
  2168. }
  2169. static int delivery_outstanding = 0;
  2170. static void pending_queues_deliver (void)
  2171. {
  2172. struct gmi_rtr_item *gmi_rtr_item_p;
  2173. int i;
  2174. int res;
  2175. struct mcast *mcast;
  2176. struct gmi_pend_delv_item pend_delv_item;
  2177. struct pend_delv *pend_delv;
  2178. int delivered;
  2179. //printf ("Delivering messages to pending queues\n");
  2180. /*
  2181. * Deliver messages in order from rtr queue to pending delivery queue
  2182. */
  2183. for (i = gmi_arut + 1; i <= gmi_highest_seq; i++) {
  2184. res = sq_item_get (&queue_rtr_items, i, (void **)&gmi_rtr_item_p);
  2185. /*
  2186. * If hole, stop assembly
  2187. */
  2188. if (res != 0) {
  2189. break;
  2190. }
  2191. assert (gmi_rtr_item_p->iovec[0].iov_len < MESSAGE_SIZE_MAX);
  2192. mcast = gmi_rtr_item_p->iovec[0].iov_base;
  2193. if (mcast == (struct mcast *)0xdeadbeef) {
  2194. printf ("seqid %d\n", gmi_rtr_item_p->iovec[0].iov_len);
  2195. }
  2196. assert (mcast != (struct mcast *)0xdeadbeef);
  2197. /*
  2198. * Message found
  2199. */
  2200. gmi_log_printf (gmi_log_level_debug,
  2201. "Delivering MCAST message with seqid %d to pending delivery queue\n",
  2202. mcast->header.seqid);
  2203. //printf ("Delivering MCAST from packet %d of %d of total %d seqid %d\n", mcast->packet_number, mcast->packet_count, mcast->packet_seq, mcast->header.seqid);
  2204. gmi_arut = i;
  2205. /*
  2206. * Create pending delivery item
  2207. */
  2208. pend_delv_item.iov_len = gmi_rtr_item_p->iov_len;
  2209. memcpy (&pend_delv_item.iovec, gmi_rtr_item_p->iovec,
  2210. sizeof (struct iovec) * gmi_rtr_item_p->iov_len);
  2211. assert (gmi_rtr_item_p->iov_len < MAXIOVS);
  2212. assert (mcast->source.s_addr != 0);
  2213. pend_delv = pend_delv_find (mcast->source);
  2214. assert (pend_delv != 0);
  2215. assert (pend_delv->ip.s_addr != 0);
  2216. if (mcast->packet_number == 0) {
  2217. pend_delv->seqid = mcast->header.seqid;
  2218. }
  2219. /*
  2220. * Add pending delivery item to pending delivery queue
  2221. */
  2222. queue_item_add (&pend_delv->queue, &pend_delv_item);
  2223. /*
  2224. * If message is complete, attempt delivery of all messages
  2225. * that are currently outstanding
  2226. */
  2227. if (mcast->packet_number == mcast->packet_count) {
  2228. //printf ("Starting delivery\n");
  2229. delivery_outstanding += 1;
  2230. do {
  2231. delivered = user_deliver ();
  2232. if (delivered) {
  2233. delivery_outstanding -= 1;
  2234. }
  2235. } while (delivery_outstanding && delivered);
  2236. }
  2237. }
  2238. //printf ("Done delivering messages to pending queues\n");
  2239. }
  2240. /*
  2241. * recv message handler called when MCAST message type received
  2242. */
  2243. static int message_handler_mcast (
  2244. struct sockaddr_in *system_from,
  2245. struct iovec *iovec,
  2246. int iov_len,
  2247. int bytes_received)
  2248. {
  2249. struct gmi_rtr_item gmi_rtr_item;
  2250. struct mcast *mcast;
  2251. mcast = iovec[0].iov_base;
  2252. /*
  2253. * Ignore multicasts for other configurations
  2254. * TODO shouldn't we enter gather here?
  2255. */
  2256. if (memcmp (&mcast->memb_conf_id,
  2257. &memb_form_token_conf_id, sizeof (struct memb_conf_id)) != 0) {
  2258. return (0);
  2259. }
  2260. /*
  2261. * Add mcast message to rtr queue if not already in rtr queue
  2262. * otherwise free io vectors
  2263. */
  2264. if (bytes_received > 0 && bytes_received < MESSAGE_SIZE_MAX &&
  2265. sq_item_inuse (&queue_rtr_items, mcast->header.seqid) == 0) {
  2266. /*
  2267. * Allocate new multicast memory block
  2268. * TODO we need to free this somewhere
  2269. */
  2270. gmi_rtr_item.iovec[0].iov_base = malloc (bytes_received);
  2271. if (gmi_rtr_item.iovec[0].iov_base == 0) {
  2272. return (-1); /* error here is corrected by the algorithm */
  2273. }
  2274. memcpy (gmi_rtr_item.iovec[0].iov_base, mcast, bytes_received);
  2275. gmi_rtr_item.iovec[0].iov_len = bytes_received;
  2276. assert (gmi_rtr_item.iovec[0].iov_len > 0);
  2277. assert (gmi_rtr_item.iovec[0].iov_len < MESSAGE_SIZE_MAX);
  2278. gmi_rtr_item.iov_len = 1;
  2279. if (mcast->header.seqid > gmi_highest_seq) {
  2280. gmi_highest_seq = mcast->header.seqid;
  2281. }
  2282. sq_item_add (&queue_rtr_items, &gmi_rtr_item, mcast->header.seqid);
  2283. }
  2284. pending_queues_deliver ();
  2285. return (0);
  2286. }
  2287. static int message_handler_memb_attempt_join (
  2288. struct sockaddr_in *system_from,
  2289. struct iovec *iov,
  2290. int iov_len,
  2291. int bytes_received)
  2292. {
  2293. int token_lost;
  2294. int found;
  2295. int i;
  2296. gmi_log_printf (gmi_log_level_notice, "Got attempt join from %s\n", inet_ntoa (system_from->sin_addr));
  2297. for (token_lost = 0, i = 0; i < memb_list_entries; i++) {
  2298. if (memb_list[i].sin_addr.s_addr == system_from->sin_addr.s_addr &&
  2299. memb_conf_id.rep.s_addr != system_from->sin_addr.s_addr) {
  2300. gmi_log_printf (gmi_log_level_debug, "ATTEMPT JOIN, token lost, taking attempt join msg.\n");
  2301. poll_timer_delete (*gmi_poll_handle, timer_orf_token_timeout);
  2302. timer_orf_token_timeout = 0;
  2303. memb_conf_id.rep.s_addr = memb_local_sockaddr_in.sin_addr.s_addr;
  2304. token_lost = 1;
  2305. break;
  2306. }
  2307. }
  2308. /*
  2309. * Not representative
  2310. */
  2311. if (token_lost == 0 &&
  2312. memb_conf_id.rep.s_addr != memb_local_sockaddr_in.sin_addr.s_addr) {
  2313. gmi_log_printf (gmi_log_level_notice, "not the rep for this ring, not handling attempt join.\n");
  2314. return (0);
  2315. }
  2316. switch (memb_state) {
  2317. case MEMB_STATE_OPERATIONAL:
  2318. case MEMB_STATE_COMMIT:
  2319. memb_state_gather_enter ();
  2320. /*
  2321. * Do NOT place break here, immediately execute gather attempt join
  2322. */
  2323. case MEMB_STATE_GATHER:
  2324. gmi_log_printf (gmi_log_level_debug, "ATTEMPT JOIN: state gather\n");
  2325. for (found = 0, i = 0; i < memb_gather_set_entries; i++) {
  2326. if (memb_gather_set[i].s_addr == system_from->sin_addr.s_addr) {
  2327. found = 1;
  2328. }
  2329. }
  2330. if (found == 0) {
  2331. memb_gather_set[memb_gather_set_entries++].s_addr = system_from->sin_addr.s_addr;
  2332. /*
  2333. * Sort gather set
  2334. */
  2335. qsort (memb_gather_set, memb_gather_set_entries,
  2336. sizeof (struct in_addr), in_addr_compare);
  2337. }
  2338. break;
  2339. default:
  2340. // TODO what about other states
  2341. gmi_log_printf (gmi_log_level_error, "memb_attempt_join: EVS or FORM state attempt join occured %d\n", memb_state);
  2342. }
  2343. return (0);
  2344. }
  2345. static int message_handler_memb_join (
  2346. struct sockaddr_in *system_from,
  2347. struct iovec *iovec,
  2348. int iov_len,
  2349. int bytes_received)
  2350. {
  2351. struct memb_join *memb_join;
  2352. int commit_entry;
  2353. int found;
  2354. int consensus;
  2355. /*
  2356. * Not representative
  2357. */
  2358. if (memb_conf_id.rep.s_addr != memb_local_sockaddr_in.sin_addr.s_addr) {
  2359. gmi_log_printf (gmi_log_level_debug, "not the rep for this ring, not handling join.\n");
  2360. return (0);
  2361. }
  2362. switch (memb_state) {
  2363. case MEMB_STATE_OPERATIONAL:
  2364. case MEMB_STATE_GATHER:
  2365. memb_state_commit_enter ();
  2366. /*
  2367. * do not place break in this case, immediately enter COMMIT state
  2368. */
  2369. case MEMB_STATE_COMMIT:
  2370. gmi_log_printf (gmi_log_level_debug, "JOIN in commit\n");
  2371. memb_join = (struct memb_join *)iovec[0].iov_base;
  2372. /*
  2373. * Find gather set that matches the system message was from
  2374. */
  2375. for (found = 0, commit_entry = 0; commit_entry < memb_commit_set_entries; commit_entry++) {
  2376. if (system_from->sin_addr.s_addr == memb_commit_set[commit_entry].rep.sin_addr.s_addr) {
  2377. found = 1;
  2378. break;
  2379. }
  2380. }
  2381. /*
  2382. * Add system from to commit sets if not currently in commit set
  2383. */
  2384. if (found == 0) {
  2385. memcpy (&memb_commit_set[commit_entry].rep, system_from, sizeof (struct sockaddr_in));
  2386. memb_commit_set_entries++;
  2387. }
  2388. /*
  2389. * Set gather join data
  2390. */
  2391. memcpy (memb_commit_set[commit_entry].join_rep_list, memb_join->active_rep_list,
  2392. sizeof (struct in_addr) * memb_join->active_rep_list_entries);
  2393. memb_commit_set[commit_entry].join_rep_list_entries = memb_join->active_rep_list_entries;
  2394. /*
  2395. * Union all entries into the gather set (join_rep_list[0])
  2396. */
  2397. memb_state_commit_union (commit_entry);
  2398. /*
  2399. * Send JOIN message, but only if gather set has changed
  2400. */
  2401. memb_join_send ();
  2402. /*
  2403. * If consensus, transition to FORM
  2404. */
  2405. memb_print_commit_set ();
  2406. consensus = memb_state_consensus_commit ();
  2407. if (consensus) {
  2408. gmi_log_printf (gmi_log_level_notice, "CONSENSUS reached!\n");
  2409. if (memb_local_sockaddr_in.sin_addr.s_addr == memb_gather_set[0].s_addr) {
  2410. gmi_log_printf (gmi_log_level_debug, "This node responsible for sending the FORM token.\n");
  2411. poll_timer_delete (*gmi_poll_handle, timer_memb_state_commit_timeout);
  2412. timer_memb_state_commit_timeout = 0;
  2413. memb_form_token_send_initial ();
  2414. }
  2415. }
  2416. break;
  2417. /*
  2418. * All other cases are ignored on JOINs
  2419. */
  2420. case MEMB_STATE_FORM:
  2421. gmi_log_printf (gmi_log_level_warning, "JOIN in form, ignoring since consensus reached in state machine.\n");
  2422. break;
  2423. default:
  2424. // TODO HANDLE THIS CASE
  2425. gmi_log_printf (gmi_log_level_debug, "memb_join: DEFAULT case %d, shouldn't happen!!\n", memb_state);
  2426. break;
  2427. }
  2428. return (0);
  2429. }
  2430. static int message_handler_memb_form_token (
  2431. struct sockaddr_in *system_from,
  2432. struct iovec *iovec,
  2433. int iov_len,
  2434. int bytes_received)
  2435. {
  2436. int i;
  2437. int local = 0;
  2438. int res = 0;
  2439. printf ("Got membership form token\n");
  2440. memcpy (&memb_form_token, iovec->iov_base, sizeof (struct memb_form_token));
  2441. poll_timer_delete (*gmi_poll_handle, timer_form_token_timeout);
  2442. timer_form_token_timeout = 0;
  2443. switch (memb_state) {
  2444. case MEMB_STATE_OPERATIONAL:
  2445. case MEMB_STATE_COMMIT:
  2446. memb_state = MEMB_STATE_FORM;
  2447. poll_timer_delete (*gmi_poll_handle, timer_memb_state_commit_timeout);
  2448. timer_memb_state_commit_timeout = 0;
  2449. /*
  2450. * Add member to entry
  2451. */
  2452. memb_form_token.member_list[memb_form_token.member_list_entries].s_addr =
  2453. memb_local_sockaddr_in.sin_addr.s_addr;
  2454. memb_form_token.member_list_entries++;
  2455. /*
  2456. * Modify the conf_id as necessary
  2457. */
  2458. memb_form_token_conf_desc_build (&memb_form_token);
  2459. /*
  2460. * Stop token timeout timer from firing
  2461. * If we are in FORM state, a previous FORM state member
  2462. * may have captured the ORF token and swallowed it
  2463. */
  2464. poll_timer_delete (*gmi_poll_handle, timer_orf_token_timeout);
  2465. timer_orf_token_timeout = 0;
  2466. /*
  2467. * Find next member
  2468. */
  2469. for (i = 0; i < memb_list_entries; i++) {
  2470. if (memb_list[i].sin_addr.s_addr == memb_local_sockaddr_in.sin_addr.s_addr) {
  2471. local = 1;
  2472. break;
  2473. }
  2474. }
  2475. if (memb_list_entries == 0) { /* 0 or 1 members and we are local */
  2476. local = 1;
  2477. }
  2478. if (local && (i + 1 < memb_list_entries)) {
  2479. memb_next.sin_addr.s_addr = memb_list[i + 1].sin_addr.s_addr;
  2480. } else {
  2481. /*
  2482. * Find next representative
  2483. */
  2484. for (i = 0; i < memb_form_token.rep_list_entries; i++) {
  2485. if (memb_conf_id.rep.s_addr ==
  2486. memb_form_token.rep_list[i].s_addr) {
  2487. break;
  2488. }
  2489. }
  2490. memb_next.sin_addr.s_addr =
  2491. memb_form_token.rep_list[(i + 1) % memb_form_token.rep_list_entries].s_addr;
  2492. }
  2493. memb_next.sin_family = AF_INET;
  2494. memb_next.sin_port = sockaddr_in_mcast.sin_port;
  2495. break;
  2496. case MEMB_STATE_FORM:
  2497. memb_state = MEMB_STATE_EVS;
  2498. memb_form_token_update_highest_seq (&memb_form_token);
  2499. /*
  2500. * FORM token has rotated once, now install local variables
  2501. *
  2502. * Set barrier sequence number
  2503. * Set original arut
  2504. */
  2505. gmi_barrier_seq = 0;
  2506. printf ("conf_desc_list %d\n", memb_form_token.conf_desc_list_entries);
  2507. for (i = 0; i < memb_form_token.conf_desc_list_entries; i++) {
  2508. printf ("highest seq %d %d\n", i, memb_form_token.conf_desc_list[i].highest_seq);
  2509. if (gmi_barrier_seq < memb_form_token.conf_desc_list[i].highest_seq) {
  2510. gmi_barrier_seq = memb_form_token.conf_desc_list[i].highest_seq;
  2511. printf ("setting barrier seq to %d\n", gmi_barrier_seq);
  2512. }
  2513. }
  2514. gmi_barrier_seq += 1;
  2515. printf ("setting barrier seq to %d\n", gmi_barrier_seq);
  2516. gmi_original_arut = gmi_arut;
  2517. /*
  2518. * Determine next ORF target
  2519. */
  2520. for (i = 0; i < memb_form_token.member_list_entries; i++) {
  2521. if (memb_local_sockaddr_in.sin_addr.s_addr == memb_form_token.member_list[i].s_addr) {
  2522. memb_next.sin_addr.s_addr =
  2523. memb_form_token.member_list[(i + 1) % memb_form_token.member_list_entries].s_addr;
  2524. memb_next.sin_family = AF_INET;
  2525. memb_next.sin_port = sockaddr_in_mcast.sin_port;
  2526. }
  2527. //ABRA
  2528. }
  2529. break;
  2530. case MEMB_STATE_EVS:
  2531. gmi_log_printf (gmi_log_level_debug, "Swallowing FORM token in EVS state.\n");
  2532. printf ("FORM CONF ENTRIES %d\n", memb_form_token.conf_desc_list_entries);
  2533. orf_token_send_initial();
  2534. return (0);
  2535. default:
  2536. // TODO
  2537. gmi_log_printf (gmi_log_level_error, "memb_form_token: default case, shouldn't happen.\n");
  2538. return (0);
  2539. }
  2540. res = memb_form_token_send (&memb_form_token);
  2541. return (res);
  2542. }
  2543. int recv_handler (poll_handle handle, int fd, int revents, void *data)
  2544. {
  2545. struct msghdr msg_recv;
  2546. struct message_header *message_header;
  2547. struct sockaddr_in system_from;
  2548. int res = 0;
  2549. int bytes_received;
  2550. /*
  2551. * Receive datagram
  2552. */
  2553. msg_recv.msg_name = &system_from;
  2554. msg_recv.msg_namelen = sizeof (struct sockaddr_in);
  2555. msg_recv.msg_iov = &gmi_iov_recv;
  2556. msg_recv.msg_iovlen = 1;
  2557. msg_recv.msg_control = 0;
  2558. msg_recv.msg_controllen = 0;
  2559. msg_recv.msg_flags = 0;
  2560. bytes_received = recvmsg (fd, &msg_recv, MSG_NOSIGNAL | MSG_DONTWAIT);
  2561. if (bytes_received == -1) {
  2562. return (0);
  2563. } else {
  2564. stats_recv += bytes_received;
  2565. }
  2566. /*
  2567. * Authenticate datagram
  2568. */
  2569. res = gmi_msg_auth (msg_recv.msg_iov, msg_recv.msg_iovlen);
  2570. if (res == -1) {
  2571. return 0;
  2572. }
  2573. if (stats_tv_start.tv_usec == 0) {
  2574. gettimeofday (&stats_tv_start, NULL);
  2575. }
  2576. /*
  2577. * Handle incoming message
  2578. */
  2579. message_header = (struct message_header *)msg_recv.msg_iov[0].iov_base;
  2580. gmi_message_handlers.handler_functions[message_header->type] (
  2581. &system_from,
  2582. msg_recv.msg_iov,
  2583. msg_recv.msg_iovlen,
  2584. bytes_received);
  2585. return (0);
  2586. }