gmi.c 79 KB

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