link.c 85 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279
  1. /*
  2. * net/tipc/link.c: TIPC link code
  3. *
  4. * Copyright (c) 1996-2006, Ericsson AB
  5. * Copyright (c) 2004-2006, Wind River Systems
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the names of the copyright holders nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * Alternatively, this software may be distributed under the terms of the
  21. * GNU General Public License ("GPL") version 2 as published by the Free
  22. * Software Foundation.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  28. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  31. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  32. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  33. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  34. * POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. #include "core.h"
  37. #include "dbg.h"
  38. #include "link.h"
  39. #include "net.h"
  40. #include "node.h"
  41. #include "port.h"
  42. #include "addr.h"
  43. #include "node_subscr.h"
  44. #include "name_distr.h"
  45. #include "bearer.h"
  46. #include "name_table.h"
  47. #include "discover.h"
  48. #include "config.h"
  49. #include "bcast.h"
  50. /*
  51. * Limit for deferred reception queue:
  52. */
  53. #define DEF_QUEUE_LIMIT 256u
  54. /*
  55. * Link state events:
  56. */
  57. #define STARTING_EVT 856384768 /* link processing trigger */
  58. #define TRAFFIC_MSG_EVT 560815u /* rx'd ??? */
  59. #define TIMEOUT_EVT 560817u /* link timer expired */
  60. /*
  61. * The following two 'message types' is really just implementation
  62. * data conveniently stored in the message header.
  63. * They must not be considered part of the protocol
  64. */
  65. #define OPEN_MSG 0
  66. #define CLOSED_MSG 1
  67. /*
  68. * State value stored in 'exp_msg_count'
  69. */
  70. #define START_CHANGEOVER 100000u
  71. /**
  72. * struct link_name - deconstructed link name
  73. * @addr_local: network address of node at this end
  74. * @if_local: name of interface at this end
  75. * @addr_peer: network address of node at far end
  76. * @if_peer: name of interface at far end
  77. */
  78. struct link_name {
  79. u32 addr_local;
  80. char if_local[TIPC_MAX_IF_NAME];
  81. u32 addr_peer;
  82. char if_peer[TIPC_MAX_IF_NAME];
  83. };
  84. #if 0
  85. /* LINK EVENT CODE IS NOT SUPPORTED AT PRESENT */
  86. /**
  87. * struct link_event - link up/down event notification
  88. */
  89. struct link_event {
  90. u32 addr;
  91. int up;
  92. void (*fcn)(u32, char *, int);
  93. char name[TIPC_MAX_LINK_NAME];
  94. };
  95. #endif
  96. static void link_handle_out_of_seq_msg(struct link *l_ptr,
  97. struct sk_buff *buf);
  98. static void link_recv_proto_msg(struct link *l_ptr, struct sk_buff *buf);
  99. static int link_recv_changeover_msg(struct link **l_ptr, struct sk_buff **buf);
  100. static void link_set_supervision_props(struct link *l_ptr, u32 tolerance);
  101. static int link_send_sections_long(struct port *sender,
  102. struct iovec const *msg_sect,
  103. u32 num_sect, u32 destnode);
  104. static void link_check_defragm_bufs(struct link *l_ptr);
  105. static void link_state_event(struct link *l_ptr, u32 event);
  106. static void link_reset_statistics(struct link *l_ptr);
  107. static void link_print(struct link *l_ptr, struct print_buf *buf,
  108. const char *str);
  109. /*
  110. * Debugging code used by link routines only
  111. *
  112. * When debugging link problems on a system that has multiple links,
  113. * the standard TIPC debugging routines may not be useful since they
  114. * allow the output from multiple links to be intermixed. For this reason
  115. * routines of the form "dbg_link_XXX()" have been created that will capture
  116. * debug info into a link's personal print buffer, which can then be dumped
  117. * into the TIPC system log (TIPC_LOG) upon request.
  118. *
  119. * To enable per-link debugging, use LINK_LOG_BUF_SIZE to specify the size
  120. * of the print buffer used by each link. If LINK_LOG_BUF_SIZE is set to 0,
  121. * the dbg_link_XXX() routines simply send their output to the standard
  122. * debug print buffer (DBG_OUTPUT), if it has been defined; this can be useful
  123. * when there is only a single link in the system being debugged.
  124. *
  125. * Notes:
  126. * - When enabled, LINK_LOG_BUF_SIZE should be set to at least TIPC_PB_MIN_SIZE
  127. * - "l_ptr" must be valid when using dbg_link_XXX() macros
  128. */
  129. #define LINK_LOG_BUF_SIZE 0
  130. #define dbg_link(fmt, arg...) do {if (LINK_LOG_BUF_SIZE) tipc_printf(&l_ptr->print_buf, fmt, ## arg); } while(0)
  131. #define dbg_link_msg(msg, txt) do {if (LINK_LOG_BUF_SIZE) tipc_msg_print(&l_ptr->print_buf, msg, txt); } while(0)
  132. #define dbg_link_state(txt) do {if (LINK_LOG_BUF_SIZE) link_print(l_ptr, &l_ptr->print_buf, txt); } while(0)
  133. #define dbg_link_dump() do { \
  134. if (LINK_LOG_BUF_SIZE) { \
  135. tipc_printf(LOG, "\n\nDumping link <%s>:\n", l_ptr->name); \
  136. tipc_printbuf_move(LOG, &l_ptr->print_buf); \
  137. } \
  138. } while (0)
  139. static void dbg_print_link(struct link *l_ptr, const char *str)
  140. {
  141. if (DBG_OUTPUT != TIPC_NULL)
  142. link_print(l_ptr, DBG_OUTPUT, str);
  143. }
  144. static void dbg_print_buf_chain(struct sk_buff *root_buf)
  145. {
  146. if (DBG_OUTPUT != TIPC_NULL) {
  147. struct sk_buff *buf = root_buf;
  148. while (buf) {
  149. msg_dbg(buf_msg(buf), "In chain: ");
  150. buf = buf->next;
  151. }
  152. }
  153. }
  154. /*
  155. * Simple link routines
  156. */
  157. static unsigned int align(unsigned int i)
  158. {
  159. return (i + 3) & ~3u;
  160. }
  161. static int link_working_working(struct link *l_ptr)
  162. {
  163. return (l_ptr->state == WORKING_WORKING);
  164. }
  165. static int link_working_unknown(struct link *l_ptr)
  166. {
  167. return (l_ptr->state == WORKING_UNKNOWN);
  168. }
  169. static int link_reset_unknown(struct link *l_ptr)
  170. {
  171. return (l_ptr->state == RESET_UNKNOWN);
  172. }
  173. static int link_reset_reset(struct link *l_ptr)
  174. {
  175. return (l_ptr->state == RESET_RESET);
  176. }
  177. static int link_blocked(struct link *l_ptr)
  178. {
  179. return (l_ptr->exp_msg_count || l_ptr->blocked);
  180. }
  181. static int link_congested(struct link *l_ptr)
  182. {
  183. return (l_ptr->out_queue_size >= l_ptr->queue_limit[0]);
  184. }
  185. static u32 link_max_pkt(struct link *l_ptr)
  186. {
  187. return l_ptr->max_pkt;
  188. }
  189. static void link_init_max_pkt(struct link *l_ptr)
  190. {
  191. u32 max_pkt;
  192. max_pkt = (l_ptr->b_ptr->publ.mtu & ~3);
  193. if (max_pkt > MAX_MSG_SIZE)
  194. max_pkt = MAX_MSG_SIZE;
  195. l_ptr->max_pkt_target = max_pkt;
  196. if (l_ptr->max_pkt_target < MAX_PKT_DEFAULT)
  197. l_ptr->max_pkt = l_ptr->max_pkt_target;
  198. else
  199. l_ptr->max_pkt = MAX_PKT_DEFAULT;
  200. l_ptr->max_pkt_probes = 0;
  201. }
  202. static u32 link_next_sent(struct link *l_ptr)
  203. {
  204. if (l_ptr->next_out)
  205. return msg_seqno(buf_msg(l_ptr->next_out));
  206. return mod(l_ptr->next_out_no);
  207. }
  208. static u32 link_last_sent(struct link *l_ptr)
  209. {
  210. return mod(link_next_sent(l_ptr) - 1);
  211. }
  212. /*
  213. * Simple non-static link routines (i.e. referenced outside this file)
  214. */
  215. int tipc_link_is_up(struct link *l_ptr)
  216. {
  217. if (!l_ptr)
  218. return 0;
  219. return (link_working_working(l_ptr) || link_working_unknown(l_ptr));
  220. }
  221. int tipc_link_is_active(struct link *l_ptr)
  222. {
  223. return ((l_ptr->owner->active_links[0] == l_ptr) ||
  224. (l_ptr->owner->active_links[1] == l_ptr));
  225. }
  226. /**
  227. * link_name_validate - validate & (optionally) deconstruct link name
  228. * @name - ptr to link name string
  229. * @name_parts - ptr to area for link name components (or NULL if not needed)
  230. *
  231. * Returns 1 if link name is valid, otherwise 0.
  232. */
  233. static int link_name_validate(const char *name, struct link_name *name_parts)
  234. {
  235. char name_copy[TIPC_MAX_LINK_NAME];
  236. char *addr_local;
  237. char *if_local;
  238. char *addr_peer;
  239. char *if_peer;
  240. char dummy;
  241. u32 z_local, c_local, n_local;
  242. u32 z_peer, c_peer, n_peer;
  243. u32 if_local_len;
  244. u32 if_peer_len;
  245. /* copy link name & ensure length is OK */
  246. name_copy[TIPC_MAX_LINK_NAME - 1] = 0;
  247. /* need above in case non-Posix strncpy() doesn't pad with nulls */
  248. strncpy(name_copy, name, TIPC_MAX_LINK_NAME);
  249. if (name_copy[TIPC_MAX_LINK_NAME - 1] != 0)
  250. return 0;
  251. /* ensure all component parts of link name are present */
  252. addr_local = name_copy;
  253. if ((if_local = strchr(addr_local, ':')) == NULL)
  254. return 0;
  255. *(if_local++) = 0;
  256. if ((addr_peer = strchr(if_local, '-')) == NULL)
  257. return 0;
  258. *(addr_peer++) = 0;
  259. if_local_len = addr_peer - if_local;
  260. if ((if_peer = strchr(addr_peer, ':')) == NULL)
  261. return 0;
  262. *(if_peer++) = 0;
  263. if_peer_len = strlen(if_peer) + 1;
  264. /* validate component parts of link name */
  265. if ((sscanf(addr_local, "%u.%u.%u%c",
  266. &z_local, &c_local, &n_local, &dummy) != 3) ||
  267. (sscanf(addr_peer, "%u.%u.%u%c",
  268. &z_peer, &c_peer, &n_peer, &dummy) != 3) ||
  269. (z_local > 255) || (c_local > 4095) || (n_local > 4095) ||
  270. (z_peer > 255) || (c_peer > 4095) || (n_peer > 4095) ||
  271. (if_local_len <= 1) || (if_local_len > TIPC_MAX_IF_NAME) ||
  272. (if_peer_len <= 1) || (if_peer_len > TIPC_MAX_IF_NAME) ||
  273. (strspn(if_local, tipc_alphabet) != (if_local_len - 1)) ||
  274. (strspn(if_peer, tipc_alphabet) != (if_peer_len - 1)))
  275. return 0;
  276. /* return link name components, if necessary */
  277. if (name_parts) {
  278. name_parts->addr_local = tipc_addr(z_local, c_local, n_local);
  279. strcpy(name_parts->if_local, if_local);
  280. name_parts->addr_peer = tipc_addr(z_peer, c_peer, n_peer);
  281. strcpy(name_parts->if_peer, if_peer);
  282. }
  283. return 1;
  284. }
  285. /**
  286. * link_timeout - handle expiration of link timer
  287. * @l_ptr: pointer to link
  288. *
  289. * This routine must not grab "tipc_net_lock" to avoid a potential deadlock conflict
  290. * with tipc_link_delete(). (There is no risk that the node will be deleted by
  291. * another thread because tipc_link_delete() always cancels the link timer before
  292. * tipc_node_delete() is called.)
  293. */
  294. static void link_timeout(struct link *l_ptr)
  295. {
  296. tipc_node_lock(l_ptr->owner);
  297. /* update counters used in statistical profiling of send traffic */
  298. l_ptr->stats.accu_queue_sz += l_ptr->out_queue_size;
  299. l_ptr->stats.queue_sz_counts++;
  300. if (l_ptr->out_queue_size > l_ptr->stats.max_queue_sz)
  301. l_ptr->stats.max_queue_sz = l_ptr->out_queue_size;
  302. if (l_ptr->first_out) {
  303. struct tipc_msg *msg = buf_msg(l_ptr->first_out);
  304. u32 length = msg_size(msg);
  305. if ((msg_user(msg) == MSG_FRAGMENTER)
  306. && (msg_type(msg) == FIRST_FRAGMENT)) {
  307. length = msg_size(msg_get_wrapped(msg));
  308. }
  309. if (length) {
  310. l_ptr->stats.msg_lengths_total += length;
  311. l_ptr->stats.msg_length_counts++;
  312. if (length <= 64)
  313. l_ptr->stats.msg_length_profile[0]++;
  314. else if (length <= 256)
  315. l_ptr->stats.msg_length_profile[1]++;
  316. else if (length <= 1024)
  317. l_ptr->stats.msg_length_profile[2]++;
  318. else if (length <= 4096)
  319. l_ptr->stats.msg_length_profile[3]++;
  320. else if (length <= 16384)
  321. l_ptr->stats.msg_length_profile[4]++;
  322. else if (length <= 32768)
  323. l_ptr->stats.msg_length_profile[5]++;
  324. else
  325. l_ptr->stats.msg_length_profile[6]++;
  326. }
  327. }
  328. /* do all other link processing performed on a periodic basis */
  329. link_check_defragm_bufs(l_ptr);
  330. link_state_event(l_ptr, TIMEOUT_EVT);
  331. if (l_ptr->next_out)
  332. tipc_link_push_queue(l_ptr);
  333. tipc_node_unlock(l_ptr->owner);
  334. }
  335. static void link_set_timer(struct link *l_ptr, u32 time)
  336. {
  337. k_start_timer(&l_ptr->timer, time);
  338. }
  339. /**
  340. * tipc_link_create - create a new link
  341. * @b_ptr: pointer to associated bearer
  342. * @peer: network address of node at other end of link
  343. * @media_addr: media address to use when sending messages over link
  344. *
  345. * Returns pointer to link.
  346. */
  347. struct link *tipc_link_create(struct bearer *b_ptr, const u32 peer,
  348. const struct tipc_media_addr *media_addr)
  349. {
  350. struct link *l_ptr;
  351. struct tipc_msg *msg;
  352. char *if_name;
  353. l_ptr = kzalloc(sizeof(*l_ptr), GFP_ATOMIC);
  354. if (!l_ptr) {
  355. warn("Link creation failed, no memory\n");
  356. return NULL;
  357. }
  358. l_ptr->addr = peer;
  359. if_name = strchr(b_ptr->publ.name, ':') + 1;
  360. sprintf(l_ptr->name, "%u.%u.%u:%s-%u.%u.%u:",
  361. tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
  362. tipc_node(tipc_own_addr),
  363. if_name,
  364. tipc_zone(peer), tipc_cluster(peer), tipc_node(peer));
  365. /* note: peer i/f is appended to link name by reset/activate */
  366. memcpy(&l_ptr->media_addr, media_addr, sizeof(*media_addr));
  367. k_init_timer(&l_ptr->timer, (Handler)link_timeout, (unsigned long)l_ptr);
  368. list_add_tail(&l_ptr->link_list, &b_ptr->links);
  369. l_ptr->checkpoint = 1;
  370. l_ptr->b_ptr = b_ptr;
  371. link_set_supervision_props(l_ptr, b_ptr->media->tolerance);
  372. l_ptr->state = RESET_UNKNOWN;
  373. l_ptr->pmsg = (struct tipc_msg *)&l_ptr->proto_msg;
  374. msg = l_ptr->pmsg;
  375. msg_init(msg, LINK_PROTOCOL, RESET_MSG, TIPC_OK, INT_H_SIZE, l_ptr->addr);
  376. msg_set_size(msg, sizeof(l_ptr->proto_msg));
  377. msg_set_session(msg, tipc_random);
  378. msg_set_bearer_id(msg, b_ptr->identity);
  379. strcpy((char *)msg_data(msg), if_name);
  380. l_ptr->priority = b_ptr->priority;
  381. tipc_link_set_queue_limits(l_ptr, b_ptr->media->window);
  382. link_init_max_pkt(l_ptr);
  383. l_ptr->next_out_no = 1;
  384. INIT_LIST_HEAD(&l_ptr->waiting_ports);
  385. link_reset_statistics(l_ptr);
  386. l_ptr->owner = tipc_node_attach_link(l_ptr);
  387. if (!l_ptr->owner) {
  388. kfree(l_ptr);
  389. return NULL;
  390. }
  391. if (LINK_LOG_BUF_SIZE) {
  392. char *pb = kmalloc(LINK_LOG_BUF_SIZE, GFP_ATOMIC);
  393. if (!pb) {
  394. kfree(l_ptr);
  395. warn("Link creation failed, no memory for print buffer\n");
  396. return NULL;
  397. }
  398. tipc_printbuf_init(&l_ptr->print_buf, pb, LINK_LOG_BUF_SIZE);
  399. }
  400. tipc_k_signal((Handler)tipc_link_start, (unsigned long)l_ptr);
  401. dbg("tipc_link_create(): tolerance = %u,cont intv = %u, abort_limit = %u\n",
  402. l_ptr->tolerance, l_ptr->continuity_interval, l_ptr->abort_limit);
  403. return l_ptr;
  404. }
  405. /**
  406. * tipc_link_delete - delete a link
  407. * @l_ptr: pointer to link
  408. *
  409. * Note: 'tipc_net_lock' is write_locked, bearer is locked.
  410. * This routine must not grab the node lock until after link timer cancellation
  411. * to avoid a potential deadlock situation.
  412. */
  413. void tipc_link_delete(struct link *l_ptr)
  414. {
  415. if (!l_ptr) {
  416. err("Attempt to delete non-existent link\n");
  417. return;
  418. }
  419. dbg("tipc_link_delete()\n");
  420. k_cancel_timer(&l_ptr->timer);
  421. tipc_node_lock(l_ptr->owner);
  422. tipc_link_reset(l_ptr);
  423. tipc_node_detach_link(l_ptr->owner, l_ptr);
  424. tipc_link_stop(l_ptr);
  425. list_del_init(&l_ptr->link_list);
  426. if (LINK_LOG_BUF_SIZE)
  427. kfree(l_ptr->print_buf.buf);
  428. tipc_node_unlock(l_ptr->owner);
  429. k_term_timer(&l_ptr->timer);
  430. kfree(l_ptr);
  431. }
  432. void tipc_link_start(struct link *l_ptr)
  433. {
  434. dbg("tipc_link_start %x\n", l_ptr);
  435. link_state_event(l_ptr, STARTING_EVT);
  436. }
  437. /**
  438. * link_schedule_port - schedule port for deferred sending
  439. * @l_ptr: pointer to link
  440. * @origport: reference to sending port
  441. * @sz: amount of data to be sent
  442. *
  443. * Schedules port for renewed sending of messages after link congestion
  444. * has abated.
  445. */
  446. static int link_schedule_port(struct link *l_ptr, u32 origport, u32 sz)
  447. {
  448. struct port *p_ptr;
  449. spin_lock_bh(&tipc_port_list_lock);
  450. p_ptr = tipc_port_lock(origport);
  451. if (p_ptr) {
  452. if (!p_ptr->wakeup)
  453. goto exit;
  454. if (!list_empty(&p_ptr->wait_list))
  455. goto exit;
  456. p_ptr->congested_link = l_ptr;
  457. p_ptr->publ.congested = 1;
  458. p_ptr->waiting_pkts = 1 + ((sz - 1) / link_max_pkt(l_ptr));
  459. list_add_tail(&p_ptr->wait_list, &l_ptr->waiting_ports);
  460. l_ptr->stats.link_congs++;
  461. exit:
  462. tipc_port_unlock(p_ptr);
  463. }
  464. spin_unlock_bh(&tipc_port_list_lock);
  465. return -ELINKCONG;
  466. }
  467. void tipc_link_wakeup_ports(struct link *l_ptr, int all)
  468. {
  469. struct port *p_ptr;
  470. struct port *temp_p_ptr;
  471. int win = l_ptr->queue_limit[0] - l_ptr->out_queue_size;
  472. if (all)
  473. win = 100000;
  474. if (win <= 0)
  475. return;
  476. if (!spin_trylock_bh(&tipc_port_list_lock))
  477. return;
  478. if (link_congested(l_ptr))
  479. goto exit;
  480. list_for_each_entry_safe(p_ptr, temp_p_ptr, &l_ptr->waiting_ports,
  481. wait_list) {
  482. if (win <= 0)
  483. break;
  484. list_del_init(&p_ptr->wait_list);
  485. p_ptr->congested_link = NULL;
  486. spin_lock_bh(p_ptr->publ.lock);
  487. p_ptr->publ.congested = 0;
  488. p_ptr->wakeup(&p_ptr->publ);
  489. win -= p_ptr->waiting_pkts;
  490. spin_unlock_bh(p_ptr->publ.lock);
  491. }
  492. exit:
  493. spin_unlock_bh(&tipc_port_list_lock);
  494. }
  495. /**
  496. * link_release_outqueue - purge link's outbound message queue
  497. * @l_ptr: pointer to link
  498. */
  499. static void link_release_outqueue(struct link *l_ptr)
  500. {
  501. struct sk_buff *buf = l_ptr->first_out;
  502. struct sk_buff *next;
  503. while (buf) {
  504. next = buf->next;
  505. buf_discard(buf);
  506. buf = next;
  507. }
  508. l_ptr->first_out = NULL;
  509. l_ptr->out_queue_size = 0;
  510. }
  511. /**
  512. * tipc_link_reset_fragments - purge link's inbound message fragments queue
  513. * @l_ptr: pointer to link
  514. */
  515. void tipc_link_reset_fragments(struct link *l_ptr)
  516. {
  517. struct sk_buff *buf = l_ptr->defragm_buf;
  518. struct sk_buff *next;
  519. while (buf) {
  520. next = buf->next;
  521. buf_discard(buf);
  522. buf = next;
  523. }
  524. l_ptr->defragm_buf = NULL;
  525. }
  526. /**
  527. * tipc_link_stop - purge all inbound and outbound messages associated with link
  528. * @l_ptr: pointer to link
  529. */
  530. void tipc_link_stop(struct link *l_ptr)
  531. {
  532. struct sk_buff *buf;
  533. struct sk_buff *next;
  534. buf = l_ptr->oldest_deferred_in;
  535. while (buf) {
  536. next = buf->next;
  537. buf_discard(buf);
  538. buf = next;
  539. }
  540. buf = l_ptr->first_out;
  541. while (buf) {
  542. next = buf->next;
  543. buf_discard(buf);
  544. buf = next;
  545. }
  546. tipc_link_reset_fragments(l_ptr);
  547. buf_discard(l_ptr->proto_msg_queue);
  548. l_ptr->proto_msg_queue = NULL;
  549. }
  550. #if 0
  551. /* LINK EVENT CODE IS NOT SUPPORTED AT PRESENT */
  552. static void link_recv_event(struct link_event *ev)
  553. {
  554. ev->fcn(ev->addr, ev->name, ev->up);
  555. kfree(ev);
  556. }
  557. static void link_send_event(void (*fcn)(u32 a, char *n, int up),
  558. struct link *l_ptr, int up)
  559. {
  560. struct link_event *ev;
  561. ev = kmalloc(sizeof(*ev), GFP_ATOMIC);
  562. if (!ev) {
  563. warn("Link event allocation failure\n");
  564. return;
  565. }
  566. ev->addr = l_ptr->addr;
  567. ev->up = up;
  568. ev->fcn = fcn;
  569. memcpy(ev->name, l_ptr->name, TIPC_MAX_LINK_NAME);
  570. tipc_k_signal((Handler)link_recv_event, (unsigned long)ev);
  571. }
  572. #else
  573. #define link_send_event(fcn, l_ptr, up) do { } while (0)
  574. #endif
  575. void tipc_link_reset(struct link *l_ptr)
  576. {
  577. struct sk_buff *buf;
  578. u32 prev_state = l_ptr->state;
  579. u32 checkpoint = l_ptr->next_in_no;
  580. int was_active_link = tipc_link_is_active(l_ptr);
  581. msg_set_session(l_ptr->pmsg, msg_session(l_ptr->pmsg) + 1);
  582. /* Link is down, accept any session: */
  583. l_ptr->peer_session = 0;
  584. /* Prepare for max packet size negotiation */
  585. link_init_max_pkt(l_ptr);
  586. l_ptr->state = RESET_UNKNOWN;
  587. dbg_link_state("Resetting Link\n");
  588. if ((prev_state == RESET_UNKNOWN) || (prev_state == RESET_RESET))
  589. return;
  590. tipc_node_link_down(l_ptr->owner, l_ptr);
  591. tipc_bearer_remove_dest(l_ptr->b_ptr, l_ptr->addr);
  592. #if 0
  593. tipc_printf(TIPC_CONS, "\nReset link <%s>\n", l_ptr->name);
  594. dbg_link_dump();
  595. #endif
  596. if (was_active_link && tipc_node_has_active_links(l_ptr->owner) &&
  597. l_ptr->owner->permit_changeover) {
  598. l_ptr->reset_checkpoint = checkpoint;
  599. l_ptr->exp_msg_count = START_CHANGEOVER;
  600. }
  601. /* Clean up all queues: */
  602. link_release_outqueue(l_ptr);
  603. buf_discard(l_ptr->proto_msg_queue);
  604. l_ptr->proto_msg_queue = NULL;
  605. buf = l_ptr->oldest_deferred_in;
  606. while (buf) {
  607. struct sk_buff *next = buf->next;
  608. buf_discard(buf);
  609. buf = next;
  610. }
  611. if (!list_empty(&l_ptr->waiting_ports))
  612. tipc_link_wakeup_ports(l_ptr, 1);
  613. l_ptr->retransm_queue_head = 0;
  614. l_ptr->retransm_queue_size = 0;
  615. l_ptr->last_out = NULL;
  616. l_ptr->first_out = NULL;
  617. l_ptr->next_out = NULL;
  618. l_ptr->unacked_window = 0;
  619. l_ptr->checkpoint = 1;
  620. l_ptr->next_out_no = 1;
  621. l_ptr->deferred_inqueue_sz = 0;
  622. l_ptr->oldest_deferred_in = NULL;
  623. l_ptr->newest_deferred_in = NULL;
  624. l_ptr->fsm_msg_cnt = 0;
  625. l_ptr->stale_count = 0;
  626. link_reset_statistics(l_ptr);
  627. link_send_event(tipc_cfg_link_event, l_ptr, 0);
  628. if (!in_own_cluster(l_ptr->addr))
  629. link_send_event(tipc_disc_link_event, l_ptr, 0);
  630. }
  631. static void link_activate(struct link *l_ptr)
  632. {
  633. l_ptr->next_in_no = l_ptr->stats.recv_info = 1;
  634. tipc_node_link_up(l_ptr->owner, l_ptr);
  635. tipc_bearer_add_dest(l_ptr->b_ptr, l_ptr->addr);
  636. link_send_event(tipc_cfg_link_event, l_ptr, 1);
  637. if (!in_own_cluster(l_ptr->addr))
  638. link_send_event(tipc_disc_link_event, l_ptr, 1);
  639. }
  640. /**
  641. * link_state_event - link finite state machine
  642. * @l_ptr: pointer to link
  643. * @event: state machine event to process
  644. */
  645. static void link_state_event(struct link *l_ptr, unsigned event)
  646. {
  647. struct link *other;
  648. u32 cont_intv = l_ptr->continuity_interval;
  649. if (!l_ptr->started && (event != STARTING_EVT))
  650. return; /* Not yet. */
  651. if (link_blocked(l_ptr)) {
  652. if (event == TIMEOUT_EVT) {
  653. link_set_timer(l_ptr, cont_intv);
  654. }
  655. return; /* Changeover going on */
  656. }
  657. dbg_link("STATE_EV: <%s> ", l_ptr->name);
  658. switch (l_ptr->state) {
  659. case WORKING_WORKING:
  660. dbg_link("WW/");
  661. switch (event) {
  662. case TRAFFIC_MSG_EVT:
  663. dbg_link("TRF-");
  664. /* fall through */
  665. case ACTIVATE_MSG:
  666. dbg_link("ACT\n");
  667. break;
  668. case TIMEOUT_EVT:
  669. dbg_link("TIM ");
  670. if (l_ptr->next_in_no != l_ptr->checkpoint) {
  671. l_ptr->checkpoint = l_ptr->next_in_no;
  672. if (tipc_bclink_acks_missing(l_ptr->owner)) {
  673. tipc_link_send_proto_msg(l_ptr, STATE_MSG,
  674. 0, 0, 0, 0, 0);
  675. l_ptr->fsm_msg_cnt++;
  676. } else if (l_ptr->max_pkt < l_ptr->max_pkt_target) {
  677. tipc_link_send_proto_msg(l_ptr, STATE_MSG,
  678. 1, 0, 0, 0, 0);
  679. l_ptr->fsm_msg_cnt++;
  680. }
  681. link_set_timer(l_ptr, cont_intv);
  682. break;
  683. }
  684. dbg_link(" -> WU\n");
  685. l_ptr->state = WORKING_UNKNOWN;
  686. l_ptr->fsm_msg_cnt = 0;
  687. tipc_link_send_proto_msg(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
  688. l_ptr->fsm_msg_cnt++;
  689. link_set_timer(l_ptr, cont_intv / 4);
  690. break;
  691. case RESET_MSG:
  692. dbg_link("RES -> RR\n");
  693. info("Resetting link <%s>, requested by peer\n",
  694. l_ptr->name);
  695. tipc_link_reset(l_ptr);
  696. l_ptr->state = RESET_RESET;
  697. l_ptr->fsm_msg_cnt = 0;
  698. tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 0, 0, 0, 0, 0);
  699. l_ptr->fsm_msg_cnt++;
  700. link_set_timer(l_ptr, cont_intv);
  701. break;
  702. default:
  703. err("Unknown link event %u in WW state\n", event);
  704. }
  705. break;
  706. case WORKING_UNKNOWN:
  707. dbg_link("WU/");
  708. switch (event) {
  709. case TRAFFIC_MSG_EVT:
  710. dbg_link("TRF-");
  711. case ACTIVATE_MSG:
  712. dbg_link("ACT -> WW\n");
  713. l_ptr->state = WORKING_WORKING;
  714. l_ptr->fsm_msg_cnt = 0;
  715. link_set_timer(l_ptr, cont_intv);
  716. break;
  717. case RESET_MSG:
  718. dbg_link("RES -> RR\n");
  719. info("Resetting link <%s>, requested by peer "
  720. "while probing\n", l_ptr->name);
  721. tipc_link_reset(l_ptr);
  722. l_ptr->state = RESET_RESET;
  723. l_ptr->fsm_msg_cnt = 0;
  724. tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 0, 0, 0, 0, 0);
  725. l_ptr->fsm_msg_cnt++;
  726. link_set_timer(l_ptr, cont_intv);
  727. break;
  728. case TIMEOUT_EVT:
  729. dbg_link("TIM ");
  730. if (l_ptr->next_in_no != l_ptr->checkpoint) {
  731. dbg_link("-> WW \n");
  732. l_ptr->state = WORKING_WORKING;
  733. l_ptr->fsm_msg_cnt = 0;
  734. l_ptr->checkpoint = l_ptr->next_in_no;
  735. if (tipc_bclink_acks_missing(l_ptr->owner)) {
  736. tipc_link_send_proto_msg(l_ptr, STATE_MSG,
  737. 0, 0, 0, 0, 0);
  738. l_ptr->fsm_msg_cnt++;
  739. }
  740. link_set_timer(l_ptr, cont_intv);
  741. } else if (l_ptr->fsm_msg_cnt < l_ptr->abort_limit) {
  742. dbg_link("Probing %u/%u,timer = %u ms)\n",
  743. l_ptr->fsm_msg_cnt, l_ptr->abort_limit,
  744. cont_intv / 4);
  745. tipc_link_send_proto_msg(l_ptr, STATE_MSG,
  746. 1, 0, 0, 0, 0);
  747. l_ptr->fsm_msg_cnt++;
  748. link_set_timer(l_ptr, cont_intv / 4);
  749. } else { /* Link has failed */
  750. dbg_link("-> RU (%u probes unanswered)\n",
  751. l_ptr->fsm_msg_cnt);
  752. warn("Resetting link <%s>, peer not responding\n",
  753. l_ptr->name);
  754. tipc_link_reset(l_ptr);
  755. l_ptr->state = RESET_UNKNOWN;
  756. l_ptr->fsm_msg_cnt = 0;
  757. tipc_link_send_proto_msg(l_ptr, RESET_MSG,
  758. 0, 0, 0, 0, 0);
  759. l_ptr->fsm_msg_cnt++;
  760. link_set_timer(l_ptr, cont_intv);
  761. }
  762. break;
  763. default:
  764. err("Unknown link event %u in WU state\n", event);
  765. }
  766. break;
  767. case RESET_UNKNOWN:
  768. dbg_link("RU/");
  769. switch (event) {
  770. case TRAFFIC_MSG_EVT:
  771. dbg_link("TRF-\n");
  772. break;
  773. case ACTIVATE_MSG:
  774. other = l_ptr->owner->active_links[0];
  775. if (other && link_working_unknown(other)) {
  776. dbg_link("ACT\n");
  777. break;
  778. }
  779. dbg_link("ACT -> WW\n");
  780. l_ptr->state = WORKING_WORKING;
  781. l_ptr->fsm_msg_cnt = 0;
  782. link_activate(l_ptr);
  783. tipc_link_send_proto_msg(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
  784. l_ptr->fsm_msg_cnt++;
  785. link_set_timer(l_ptr, cont_intv);
  786. break;
  787. case RESET_MSG:
  788. dbg_link("RES \n");
  789. dbg_link(" -> RR\n");
  790. l_ptr->state = RESET_RESET;
  791. l_ptr->fsm_msg_cnt = 0;
  792. tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 1, 0, 0, 0, 0);
  793. l_ptr->fsm_msg_cnt++;
  794. link_set_timer(l_ptr, cont_intv);
  795. break;
  796. case STARTING_EVT:
  797. dbg_link("START-");
  798. l_ptr->started = 1;
  799. /* fall through */
  800. case TIMEOUT_EVT:
  801. dbg_link("TIM \n");
  802. tipc_link_send_proto_msg(l_ptr, RESET_MSG, 0, 0, 0, 0, 0);
  803. l_ptr->fsm_msg_cnt++;
  804. link_set_timer(l_ptr, cont_intv);
  805. break;
  806. default:
  807. err("Unknown link event %u in RU state\n", event);
  808. }
  809. break;
  810. case RESET_RESET:
  811. dbg_link("RR/ ");
  812. switch (event) {
  813. case TRAFFIC_MSG_EVT:
  814. dbg_link("TRF-");
  815. /* fall through */
  816. case ACTIVATE_MSG:
  817. other = l_ptr->owner->active_links[0];
  818. if (other && link_working_unknown(other)) {
  819. dbg_link("ACT\n");
  820. break;
  821. }
  822. dbg_link("ACT -> WW\n");
  823. l_ptr->state = WORKING_WORKING;
  824. l_ptr->fsm_msg_cnt = 0;
  825. link_activate(l_ptr);
  826. tipc_link_send_proto_msg(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
  827. l_ptr->fsm_msg_cnt++;
  828. link_set_timer(l_ptr, cont_intv);
  829. break;
  830. case RESET_MSG:
  831. dbg_link("RES\n");
  832. break;
  833. case TIMEOUT_EVT:
  834. dbg_link("TIM\n");
  835. tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 0, 0, 0, 0, 0);
  836. l_ptr->fsm_msg_cnt++;
  837. link_set_timer(l_ptr, cont_intv);
  838. dbg_link("fsm_msg_cnt %u\n", l_ptr->fsm_msg_cnt);
  839. break;
  840. default:
  841. err("Unknown link event %u in RR state\n", event);
  842. }
  843. break;
  844. default:
  845. err("Unknown link state %u/%u\n", l_ptr->state, event);
  846. }
  847. }
  848. /*
  849. * link_bundle_buf(): Append contents of a buffer to
  850. * the tail of an existing one.
  851. */
  852. static int link_bundle_buf(struct link *l_ptr,
  853. struct sk_buff *bundler,
  854. struct sk_buff *buf)
  855. {
  856. struct tipc_msg *bundler_msg = buf_msg(bundler);
  857. struct tipc_msg *msg = buf_msg(buf);
  858. u32 size = msg_size(msg);
  859. u32 bundle_size = msg_size(bundler_msg);
  860. u32 to_pos = align(bundle_size);
  861. u32 pad = to_pos - bundle_size;
  862. if (msg_user(bundler_msg) != MSG_BUNDLER)
  863. return 0;
  864. if (msg_type(bundler_msg) != OPEN_MSG)
  865. return 0;
  866. if (skb_tailroom(bundler) < (pad + size))
  867. return 0;
  868. if (link_max_pkt(l_ptr) < (to_pos + size))
  869. return 0;
  870. skb_put(bundler, pad + size);
  871. memcpy(bundler->data + to_pos, buf->data, size);
  872. msg_set_size(bundler_msg, to_pos + size);
  873. msg_set_msgcnt(bundler_msg, msg_msgcnt(bundler_msg) + 1);
  874. dbg("Packed msg # %u(%u octets) into pos %u in buf(#%u)\n",
  875. msg_msgcnt(bundler_msg), size, to_pos, msg_seqno(bundler_msg));
  876. msg_dbg(msg, "PACKD:");
  877. buf_discard(buf);
  878. l_ptr->stats.sent_bundled++;
  879. return 1;
  880. }
  881. static void link_add_to_outqueue(struct link *l_ptr,
  882. struct sk_buff *buf,
  883. struct tipc_msg *msg)
  884. {
  885. u32 ack = mod(l_ptr->next_in_no - 1);
  886. u32 seqno = mod(l_ptr->next_out_no++);
  887. msg_set_word(msg, 2, ((ack << 16) | seqno));
  888. msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
  889. buf->next = NULL;
  890. if (l_ptr->first_out) {
  891. l_ptr->last_out->next = buf;
  892. l_ptr->last_out = buf;
  893. } else
  894. l_ptr->first_out = l_ptr->last_out = buf;
  895. l_ptr->out_queue_size++;
  896. }
  897. /*
  898. * tipc_link_send_buf() is the 'full path' for messages, called from
  899. * inside TIPC when the 'fast path' in tipc_send_buf
  900. * has failed, and from link_send()
  901. */
  902. int tipc_link_send_buf(struct link *l_ptr, struct sk_buff *buf)
  903. {
  904. struct tipc_msg *msg = buf_msg(buf);
  905. u32 size = msg_size(msg);
  906. u32 dsz = msg_data_sz(msg);
  907. u32 queue_size = l_ptr->out_queue_size;
  908. u32 imp = msg_tot_importance(msg);
  909. u32 queue_limit = l_ptr->queue_limit[imp];
  910. u32 max_packet = link_max_pkt(l_ptr);
  911. msg_set_prevnode(msg, tipc_own_addr); /* If routed message */
  912. /* Match msg importance against queue limits: */
  913. if (unlikely(queue_size >= queue_limit)) {
  914. if (imp <= TIPC_CRITICAL_IMPORTANCE) {
  915. return link_schedule_port(l_ptr, msg_origport(msg),
  916. size);
  917. }
  918. msg_dbg(msg, "TIPC: Congestion, throwing away\n");
  919. buf_discard(buf);
  920. if (imp > CONN_MANAGER) {
  921. warn("Resetting link <%s>, send queue full", l_ptr->name);
  922. tipc_link_reset(l_ptr);
  923. }
  924. return dsz;
  925. }
  926. /* Fragmentation needed ? */
  927. if (size > max_packet)
  928. return tipc_link_send_long_buf(l_ptr, buf);
  929. /* Packet can be queued or sent: */
  930. if (queue_size > l_ptr->stats.max_queue_sz)
  931. l_ptr->stats.max_queue_sz = queue_size;
  932. if (likely(!tipc_bearer_congested(l_ptr->b_ptr, l_ptr) &&
  933. !link_congested(l_ptr))) {
  934. link_add_to_outqueue(l_ptr, buf, msg);
  935. if (likely(tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr))) {
  936. l_ptr->unacked_window = 0;
  937. } else {
  938. tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
  939. l_ptr->stats.bearer_congs++;
  940. l_ptr->next_out = buf;
  941. }
  942. return dsz;
  943. }
  944. /* Congestion: can message be bundled ?: */
  945. if ((msg_user(msg) != CHANGEOVER_PROTOCOL) &&
  946. (msg_user(msg) != MSG_FRAGMENTER)) {
  947. /* Try adding message to an existing bundle */
  948. if (l_ptr->next_out &&
  949. link_bundle_buf(l_ptr, l_ptr->last_out, buf)) {
  950. tipc_bearer_resolve_congestion(l_ptr->b_ptr, l_ptr);
  951. return dsz;
  952. }
  953. /* Try creating a new bundle */
  954. if (size <= max_packet * 2 / 3) {
  955. struct sk_buff *bundler = buf_acquire(max_packet);
  956. struct tipc_msg bundler_hdr;
  957. if (bundler) {
  958. msg_init(&bundler_hdr, MSG_BUNDLER, OPEN_MSG,
  959. TIPC_OK, INT_H_SIZE, l_ptr->addr);
  960. memcpy(bundler->data, (unchar *)&bundler_hdr,
  961. INT_H_SIZE);
  962. skb_trim(bundler, INT_H_SIZE);
  963. link_bundle_buf(l_ptr, bundler, buf);
  964. buf = bundler;
  965. msg = buf_msg(buf);
  966. l_ptr->stats.sent_bundles++;
  967. }
  968. }
  969. }
  970. if (!l_ptr->next_out)
  971. l_ptr->next_out = buf;
  972. link_add_to_outqueue(l_ptr, buf, msg);
  973. tipc_bearer_resolve_congestion(l_ptr->b_ptr, l_ptr);
  974. return dsz;
  975. }
  976. /*
  977. * tipc_link_send(): same as tipc_link_send_buf(), but the link to use has
  978. * not been selected yet, and the the owner node is not locked
  979. * Called by TIPC internal users, e.g. the name distributor
  980. */
  981. int tipc_link_send(struct sk_buff *buf, u32 dest, u32 selector)
  982. {
  983. struct link *l_ptr;
  984. struct node *n_ptr;
  985. int res = -ELINKCONG;
  986. read_lock_bh(&tipc_net_lock);
  987. n_ptr = tipc_node_select(dest, selector);
  988. if (n_ptr) {
  989. tipc_node_lock(n_ptr);
  990. l_ptr = n_ptr->active_links[selector & 1];
  991. if (l_ptr) {
  992. dbg("tipc_link_send: found link %x for dest %x\n", l_ptr, dest);
  993. res = tipc_link_send_buf(l_ptr, buf);
  994. } else {
  995. dbg("Attempt to send msg to unreachable node:\n");
  996. msg_dbg(buf_msg(buf),">>>");
  997. buf_discard(buf);
  998. }
  999. tipc_node_unlock(n_ptr);
  1000. } else {
  1001. dbg("Attempt to send msg to unknown node:\n");
  1002. msg_dbg(buf_msg(buf),">>>");
  1003. buf_discard(buf);
  1004. }
  1005. read_unlock_bh(&tipc_net_lock);
  1006. return res;
  1007. }
  1008. /*
  1009. * link_send_buf_fast: Entry for data messages where the
  1010. * destination link is known and the header is complete,
  1011. * inclusive total message length. Very time critical.
  1012. * Link is locked. Returns user data length.
  1013. */
  1014. static int link_send_buf_fast(struct link *l_ptr, struct sk_buff *buf,
  1015. u32 *used_max_pkt)
  1016. {
  1017. struct tipc_msg *msg = buf_msg(buf);
  1018. int res = msg_data_sz(msg);
  1019. if (likely(!link_congested(l_ptr))) {
  1020. if (likely(msg_size(msg) <= link_max_pkt(l_ptr))) {
  1021. if (likely(list_empty(&l_ptr->b_ptr->cong_links))) {
  1022. link_add_to_outqueue(l_ptr, buf, msg);
  1023. if (likely(tipc_bearer_send(l_ptr->b_ptr, buf,
  1024. &l_ptr->media_addr))) {
  1025. l_ptr->unacked_window = 0;
  1026. msg_dbg(msg,"SENT_FAST:");
  1027. return res;
  1028. }
  1029. dbg("failed sent fast...\n");
  1030. tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
  1031. l_ptr->stats.bearer_congs++;
  1032. l_ptr->next_out = buf;
  1033. return res;
  1034. }
  1035. }
  1036. else
  1037. *used_max_pkt = link_max_pkt(l_ptr);
  1038. }
  1039. return tipc_link_send_buf(l_ptr, buf); /* All other cases */
  1040. }
  1041. /*
  1042. * tipc_send_buf_fast: Entry for data messages where the
  1043. * destination node is known and the header is complete,
  1044. * inclusive total message length.
  1045. * Returns user data length.
  1046. */
  1047. int tipc_send_buf_fast(struct sk_buff *buf, u32 destnode)
  1048. {
  1049. struct link *l_ptr;
  1050. struct node *n_ptr;
  1051. int res;
  1052. u32 selector = msg_origport(buf_msg(buf)) & 1;
  1053. u32 dummy;
  1054. if (destnode == tipc_own_addr)
  1055. return tipc_port_recv_msg(buf);
  1056. read_lock_bh(&tipc_net_lock);
  1057. n_ptr = tipc_node_select(destnode, selector);
  1058. if (likely(n_ptr)) {
  1059. tipc_node_lock(n_ptr);
  1060. l_ptr = n_ptr->active_links[selector];
  1061. dbg("send_fast: buf %x selected %x, destnode = %x\n",
  1062. buf, l_ptr, destnode);
  1063. if (likely(l_ptr)) {
  1064. res = link_send_buf_fast(l_ptr, buf, &dummy);
  1065. tipc_node_unlock(n_ptr);
  1066. read_unlock_bh(&tipc_net_lock);
  1067. return res;
  1068. }
  1069. tipc_node_unlock(n_ptr);
  1070. }
  1071. read_unlock_bh(&tipc_net_lock);
  1072. res = msg_data_sz(buf_msg(buf));
  1073. tipc_reject_msg(buf, TIPC_ERR_NO_NODE);
  1074. return res;
  1075. }
  1076. /*
  1077. * tipc_link_send_sections_fast: Entry for messages where the
  1078. * destination processor is known and the header is complete,
  1079. * except for total message length.
  1080. * Returns user data length or errno.
  1081. */
  1082. int tipc_link_send_sections_fast(struct port *sender,
  1083. struct iovec const *msg_sect,
  1084. const u32 num_sect,
  1085. u32 destaddr)
  1086. {
  1087. struct tipc_msg *hdr = &sender->publ.phdr;
  1088. struct link *l_ptr;
  1089. struct sk_buff *buf;
  1090. struct node *node;
  1091. int res;
  1092. u32 selector = msg_origport(hdr) & 1;
  1093. again:
  1094. /*
  1095. * Try building message using port's max_pkt hint.
  1096. * (Must not hold any locks while building message.)
  1097. */
  1098. res = msg_build(hdr, msg_sect, num_sect, sender->max_pkt,
  1099. !sender->user_port, &buf);
  1100. read_lock_bh(&tipc_net_lock);
  1101. node = tipc_node_select(destaddr, selector);
  1102. if (likely(node)) {
  1103. tipc_node_lock(node);
  1104. l_ptr = node->active_links[selector];
  1105. if (likely(l_ptr)) {
  1106. if (likely(buf)) {
  1107. res = link_send_buf_fast(l_ptr, buf,
  1108. &sender->max_pkt);
  1109. if (unlikely(res < 0))
  1110. buf_discard(buf);
  1111. exit:
  1112. tipc_node_unlock(node);
  1113. read_unlock_bh(&tipc_net_lock);
  1114. return res;
  1115. }
  1116. /* Exit if build request was invalid */
  1117. if (unlikely(res < 0))
  1118. goto exit;
  1119. /* Exit if link (or bearer) is congested */
  1120. if (link_congested(l_ptr) ||
  1121. !list_empty(&l_ptr->b_ptr->cong_links)) {
  1122. res = link_schedule_port(l_ptr,
  1123. sender->publ.ref, res);
  1124. goto exit;
  1125. }
  1126. /*
  1127. * Message size exceeds max_pkt hint; update hint,
  1128. * then re-try fast path or fragment the message
  1129. */
  1130. sender->max_pkt = link_max_pkt(l_ptr);
  1131. tipc_node_unlock(node);
  1132. read_unlock_bh(&tipc_net_lock);
  1133. if ((msg_hdr_sz(hdr) + res) <= sender->max_pkt)
  1134. goto again;
  1135. return link_send_sections_long(sender, msg_sect,
  1136. num_sect, destaddr);
  1137. }
  1138. tipc_node_unlock(node);
  1139. }
  1140. read_unlock_bh(&tipc_net_lock);
  1141. /* Couldn't find a link to the destination node */
  1142. if (buf)
  1143. return tipc_reject_msg(buf, TIPC_ERR_NO_NODE);
  1144. if (res >= 0)
  1145. return tipc_port_reject_sections(sender, hdr, msg_sect, num_sect,
  1146. TIPC_ERR_NO_NODE);
  1147. return res;
  1148. }
  1149. /*
  1150. * link_send_sections_long(): Entry for long messages where the
  1151. * destination node is known and the header is complete,
  1152. * inclusive total message length.
  1153. * Link and bearer congestion status have been checked to be ok,
  1154. * and are ignored if they change.
  1155. *
  1156. * Note that fragments do not use the full link MTU so that they won't have
  1157. * to undergo refragmentation if link changeover causes them to be sent
  1158. * over another link with an additional tunnel header added as prefix.
  1159. * (Refragmentation will still occur if the other link has a smaller MTU.)
  1160. *
  1161. * Returns user data length or errno.
  1162. */
  1163. static int link_send_sections_long(struct port *sender,
  1164. struct iovec const *msg_sect,
  1165. u32 num_sect,
  1166. u32 destaddr)
  1167. {
  1168. struct link *l_ptr;
  1169. struct node *node;
  1170. struct tipc_msg *hdr = &sender->publ.phdr;
  1171. u32 dsz = msg_data_sz(hdr);
  1172. u32 max_pkt,fragm_sz,rest;
  1173. struct tipc_msg fragm_hdr;
  1174. struct sk_buff *buf,*buf_chain,*prev;
  1175. u32 fragm_crs,fragm_rest,hsz,sect_rest;
  1176. const unchar *sect_crs;
  1177. int curr_sect;
  1178. u32 fragm_no;
  1179. again:
  1180. fragm_no = 1;
  1181. max_pkt = sender->max_pkt - INT_H_SIZE;
  1182. /* leave room for tunnel header in case of link changeover */
  1183. fragm_sz = max_pkt - INT_H_SIZE;
  1184. /* leave room for fragmentation header in each fragment */
  1185. rest = dsz;
  1186. fragm_crs = 0;
  1187. fragm_rest = 0;
  1188. sect_rest = 0;
  1189. sect_crs = NULL;
  1190. curr_sect = -1;
  1191. /* Prepare reusable fragment header: */
  1192. msg_dbg(hdr, ">FRAGMENTING>");
  1193. msg_init(&fragm_hdr, MSG_FRAGMENTER, FIRST_FRAGMENT,
  1194. TIPC_OK, INT_H_SIZE, msg_destnode(hdr));
  1195. msg_set_link_selector(&fragm_hdr, sender->publ.ref);
  1196. msg_set_size(&fragm_hdr, max_pkt);
  1197. msg_set_fragm_no(&fragm_hdr, 1);
  1198. /* Prepare header of first fragment: */
  1199. buf_chain = buf = buf_acquire(max_pkt);
  1200. if (!buf)
  1201. return -ENOMEM;
  1202. buf->next = NULL;
  1203. memcpy(buf->data, (unchar *)&fragm_hdr, INT_H_SIZE);
  1204. hsz = msg_hdr_sz(hdr);
  1205. memcpy(buf->data + INT_H_SIZE, (unchar *)hdr, hsz);
  1206. msg_dbg(buf_msg(buf), ">BUILD>");
  1207. /* Chop up message: */
  1208. fragm_crs = INT_H_SIZE + hsz;
  1209. fragm_rest = fragm_sz - hsz;
  1210. do { /* For all sections */
  1211. u32 sz;
  1212. if (!sect_rest) {
  1213. sect_rest = msg_sect[++curr_sect].iov_len;
  1214. sect_crs = (const unchar *)msg_sect[curr_sect].iov_base;
  1215. }
  1216. if (sect_rest < fragm_rest)
  1217. sz = sect_rest;
  1218. else
  1219. sz = fragm_rest;
  1220. if (likely(!sender->user_port)) {
  1221. if (copy_from_user(buf->data + fragm_crs, sect_crs, sz)) {
  1222. error:
  1223. for (; buf_chain; buf_chain = buf) {
  1224. buf = buf_chain->next;
  1225. buf_discard(buf_chain);
  1226. }
  1227. return -EFAULT;
  1228. }
  1229. } else
  1230. memcpy(buf->data + fragm_crs, sect_crs, sz);
  1231. sect_crs += sz;
  1232. sect_rest -= sz;
  1233. fragm_crs += sz;
  1234. fragm_rest -= sz;
  1235. rest -= sz;
  1236. if (!fragm_rest && rest) {
  1237. /* Initiate new fragment: */
  1238. if (rest <= fragm_sz) {
  1239. fragm_sz = rest;
  1240. msg_set_type(&fragm_hdr,LAST_FRAGMENT);
  1241. } else {
  1242. msg_set_type(&fragm_hdr, FRAGMENT);
  1243. }
  1244. msg_set_size(&fragm_hdr, fragm_sz + INT_H_SIZE);
  1245. msg_set_fragm_no(&fragm_hdr, ++fragm_no);
  1246. prev = buf;
  1247. buf = buf_acquire(fragm_sz + INT_H_SIZE);
  1248. if (!buf)
  1249. goto error;
  1250. buf->next = NULL;
  1251. prev->next = buf;
  1252. memcpy(buf->data, (unchar *)&fragm_hdr, INT_H_SIZE);
  1253. fragm_crs = INT_H_SIZE;
  1254. fragm_rest = fragm_sz;
  1255. msg_dbg(buf_msg(buf)," >BUILD>");
  1256. }
  1257. }
  1258. while (rest > 0);
  1259. /*
  1260. * Now we have a buffer chain. Select a link and check
  1261. * that packet size is still OK
  1262. */
  1263. node = tipc_node_select(destaddr, sender->publ.ref & 1);
  1264. if (likely(node)) {
  1265. tipc_node_lock(node);
  1266. l_ptr = node->active_links[sender->publ.ref & 1];
  1267. if (!l_ptr) {
  1268. tipc_node_unlock(node);
  1269. goto reject;
  1270. }
  1271. if (link_max_pkt(l_ptr) < max_pkt) {
  1272. sender->max_pkt = link_max_pkt(l_ptr);
  1273. tipc_node_unlock(node);
  1274. for (; buf_chain; buf_chain = buf) {
  1275. buf = buf_chain->next;
  1276. buf_discard(buf_chain);
  1277. }
  1278. goto again;
  1279. }
  1280. } else {
  1281. reject:
  1282. for (; buf_chain; buf_chain = buf) {
  1283. buf = buf_chain->next;
  1284. buf_discard(buf_chain);
  1285. }
  1286. return tipc_port_reject_sections(sender, hdr, msg_sect, num_sect,
  1287. TIPC_ERR_NO_NODE);
  1288. }
  1289. /* Append whole chain to send queue: */
  1290. buf = buf_chain;
  1291. l_ptr->long_msg_seq_no = mod(l_ptr->long_msg_seq_no + 1);
  1292. if (!l_ptr->next_out)
  1293. l_ptr->next_out = buf_chain;
  1294. l_ptr->stats.sent_fragmented++;
  1295. while (buf) {
  1296. struct sk_buff *next = buf->next;
  1297. struct tipc_msg *msg = buf_msg(buf);
  1298. l_ptr->stats.sent_fragments++;
  1299. msg_set_long_msgno(msg, l_ptr->long_msg_seq_no);
  1300. link_add_to_outqueue(l_ptr, buf, msg);
  1301. msg_dbg(msg, ">ADD>");
  1302. buf = next;
  1303. }
  1304. /* Send it, if possible: */
  1305. tipc_link_push_queue(l_ptr);
  1306. tipc_node_unlock(node);
  1307. return dsz;
  1308. }
  1309. /*
  1310. * tipc_link_push_packet: Push one unsent packet to the media
  1311. */
  1312. u32 tipc_link_push_packet(struct link *l_ptr)
  1313. {
  1314. struct sk_buff *buf = l_ptr->first_out;
  1315. u32 r_q_size = l_ptr->retransm_queue_size;
  1316. u32 r_q_head = l_ptr->retransm_queue_head;
  1317. /* Step to position where retransmission failed, if any, */
  1318. /* consider that buffers may have been released in meantime */
  1319. if (r_q_size && buf) {
  1320. u32 last = lesser(mod(r_q_head + r_q_size),
  1321. link_last_sent(l_ptr));
  1322. u32 first = msg_seqno(buf_msg(buf));
  1323. while (buf && less(first, r_q_head)) {
  1324. first = mod(first + 1);
  1325. buf = buf->next;
  1326. }
  1327. l_ptr->retransm_queue_head = r_q_head = first;
  1328. l_ptr->retransm_queue_size = r_q_size = mod(last - first);
  1329. }
  1330. /* Continue retransmission now, if there is anything: */
  1331. if (r_q_size && buf && !skb_cloned(buf)) {
  1332. msg_set_ack(buf_msg(buf), mod(l_ptr->next_in_no - 1));
  1333. msg_set_bcast_ack(buf_msg(buf), l_ptr->owner->bclink.last_in);
  1334. if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
  1335. msg_dbg(buf_msg(buf), ">DEF-RETR>");
  1336. l_ptr->retransm_queue_head = mod(++r_q_head);
  1337. l_ptr->retransm_queue_size = --r_q_size;
  1338. l_ptr->stats.retransmitted++;
  1339. return TIPC_OK;
  1340. } else {
  1341. l_ptr->stats.bearer_congs++;
  1342. msg_dbg(buf_msg(buf), "|>DEF-RETR>");
  1343. return PUSH_FAILED;
  1344. }
  1345. }
  1346. /* Send deferred protocol message, if any: */
  1347. buf = l_ptr->proto_msg_queue;
  1348. if (buf) {
  1349. msg_set_ack(buf_msg(buf), mod(l_ptr->next_in_no - 1));
  1350. msg_set_bcast_ack(buf_msg(buf),l_ptr->owner->bclink.last_in);
  1351. if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
  1352. msg_dbg(buf_msg(buf), ">DEF-PROT>");
  1353. l_ptr->unacked_window = 0;
  1354. buf_discard(buf);
  1355. l_ptr->proto_msg_queue = NULL;
  1356. return TIPC_OK;
  1357. } else {
  1358. msg_dbg(buf_msg(buf), "|>DEF-PROT>");
  1359. l_ptr->stats.bearer_congs++;
  1360. return PUSH_FAILED;
  1361. }
  1362. }
  1363. /* Send one deferred data message, if send window not full: */
  1364. buf = l_ptr->next_out;
  1365. if (buf) {
  1366. struct tipc_msg *msg = buf_msg(buf);
  1367. u32 next = msg_seqno(msg);
  1368. u32 first = msg_seqno(buf_msg(l_ptr->first_out));
  1369. if (mod(next - first) < l_ptr->queue_limit[0]) {
  1370. msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
  1371. msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
  1372. if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
  1373. if (msg_user(msg) == MSG_BUNDLER)
  1374. msg_set_type(msg, CLOSED_MSG);
  1375. msg_dbg(msg, ">PUSH-DATA>");
  1376. l_ptr->next_out = buf->next;
  1377. return TIPC_OK;
  1378. } else {
  1379. msg_dbg(msg, "|PUSH-DATA|");
  1380. l_ptr->stats.bearer_congs++;
  1381. return PUSH_FAILED;
  1382. }
  1383. }
  1384. }
  1385. return PUSH_FINISHED;
  1386. }
  1387. /*
  1388. * push_queue(): push out the unsent messages of a link where
  1389. * congestion has abated. Node is locked
  1390. */
  1391. void tipc_link_push_queue(struct link *l_ptr)
  1392. {
  1393. u32 res;
  1394. if (tipc_bearer_congested(l_ptr->b_ptr, l_ptr))
  1395. return;
  1396. do {
  1397. res = tipc_link_push_packet(l_ptr);
  1398. }
  1399. while (res == TIPC_OK);
  1400. if (res == PUSH_FAILED)
  1401. tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
  1402. }
  1403. static void link_reset_all(unsigned long addr)
  1404. {
  1405. struct node *n_ptr;
  1406. char addr_string[16];
  1407. u32 i;
  1408. read_lock_bh(&tipc_net_lock);
  1409. n_ptr = tipc_node_find((u32)addr);
  1410. if (!n_ptr) {
  1411. read_unlock_bh(&tipc_net_lock);
  1412. return; /* node no longer exists */
  1413. }
  1414. tipc_node_lock(n_ptr);
  1415. warn("Resetting all links to %s\n",
  1416. addr_string_fill(addr_string, n_ptr->addr));
  1417. for (i = 0; i < MAX_BEARERS; i++) {
  1418. if (n_ptr->links[i]) {
  1419. link_print(n_ptr->links[i], TIPC_OUTPUT,
  1420. "Resetting link\n");
  1421. tipc_link_reset(n_ptr->links[i]);
  1422. }
  1423. }
  1424. tipc_node_unlock(n_ptr);
  1425. read_unlock_bh(&tipc_net_lock);
  1426. }
  1427. static void link_retransmit_failure(struct link *l_ptr, struct sk_buff *buf)
  1428. {
  1429. struct tipc_msg *msg = buf_msg(buf);
  1430. warn("Retransmission failure on link <%s>\n", l_ptr->name);
  1431. tipc_msg_print(TIPC_OUTPUT, msg, ">RETR-FAIL>");
  1432. if (l_ptr->addr) {
  1433. /* Handle failure on standard link */
  1434. link_print(l_ptr, TIPC_OUTPUT, "Resetting link\n");
  1435. tipc_link_reset(l_ptr);
  1436. } else {
  1437. /* Handle failure on broadcast link */
  1438. struct node *n_ptr;
  1439. char addr_string[16];
  1440. tipc_printf(TIPC_OUTPUT, "Msg seq number: %u, ", msg_seqno(msg));
  1441. tipc_printf(TIPC_OUTPUT, "Outstanding acks: %lu\n",
  1442. (unsigned long) TIPC_SKB_CB(buf)->handle);
  1443. n_ptr = l_ptr->owner->next;
  1444. tipc_node_lock(n_ptr);
  1445. addr_string_fill(addr_string, n_ptr->addr);
  1446. tipc_printf(TIPC_OUTPUT, "Multicast link info for %s\n", addr_string);
  1447. tipc_printf(TIPC_OUTPUT, "Supported: %d, ", n_ptr->bclink.supported);
  1448. tipc_printf(TIPC_OUTPUT, "Acked: %u\n", n_ptr->bclink.acked);
  1449. tipc_printf(TIPC_OUTPUT, "Last in: %u, ", n_ptr->bclink.last_in);
  1450. tipc_printf(TIPC_OUTPUT, "Gap after: %u, ", n_ptr->bclink.gap_after);
  1451. tipc_printf(TIPC_OUTPUT, "Gap to: %u\n", n_ptr->bclink.gap_to);
  1452. tipc_printf(TIPC_OUTPUT, "Nack sync: %u\n\n", n_ptr->bclink.nack_sync);
  1453. tipc_k_signal((Handler)link_reset_all, (unsigned long)n_ptr->addr);
  1454. tipc_node_unlock(n_ptr);
  1455. l_ptr->stale_count = 0;
  1456. }
  1457. }
  1458. void tipc_link_retransmit(struct link *l_ptr, struct sk_buff *buf,
  1459. u32 retransmits)
  1460. {
  1461. struct tipc_msg *msg;
  1462. if (!buf)
  1463. return;
  1464. msg = buf_msg(buf);
  1465. dbg("Retransmitting %u in link %x\n", retransmits, l_ptr);
  1466. if (tipc_bearer_congested(l_ptr->b_ptr, l_ptr)) {
  1467. if (!skb_cloned(buf)) {
  1468. msg_dbg(msg, ">NO_RETR->BCONG>");
  1469. dbg_print_link(l_ptr, " ");
  1470. l_ptr->retransm_queue_head = msg_seqno(msg);
  1471. l_ptr->retransm_queue_size = retransmits;
  1472. return;
  1473. } else {
  1474. /* Don't retransmit if driver already has the buffer */
  1475. }
  1476. } else {
  1477. /* Detect repeated retransmit failures on uncongested bearer */
  1478. if (l_ptr->last_retransmitted == msg_seqno(msg)) {
  1479. if (++l_ptr->stale_count > 100) {
  1480. link_retransmit_failure(l_ptr, buf);
  1481. return;
  1482. }
  1483. } else {
  1484. l_ptr->last_retransmitted = msg_seqno(msg);
  1485. l_ptr->stale_count = 1;
  1486. }
  1487. }
  1488. while (retransmits && (buf != l_ptr->next_out) && buf && !skb_cloned(buf)) {
  1489. msg = buf_msg(buf);
  1490. msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
  1491. msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
  1492. if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
  1493. msg_dbg(buf_msg(buf), ">RETR>");
  1494. buf = buf->next;
  1495. retransmits--;
  1496. l_ptr->stats.retransmitted++;
  1497. } else {
  1498. tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
  1499. l_ptr->stats.bearer_congs++;
  1500. l_ptr->retransm_queue_head = msg_seqno(buf_msg(buf));
  1501. l_ptr->retransm_queue_size = retransmits;
  1502. return;
  1503. }
  1504. }
  1505. l_ptr->retransm_queue_head = l_ptr->retransm_queue_size = 0;
  1506. }
  1507. /*
  1508. * link_recv_non_seq: Receive packets which are outside
  1509. * the link sequence flow
  1510. */
  1511. static void link_recv_non_seq(struct sk_buff *buf)
  1512. {
  1513. struct tipc_msg *msg = buf_msg(buf);
  1514. if (msg_user(msg) == LINK_CONFIG)
  1515. tipc_disc_recv_msg(buf);
  1516. else
  1517. tipc_bclink_recv_pkt(buf);
  1518. }
  1519. /**
  1520. * link_insert_deferred_queue - insert deferred messages back into receive chain
  1521. */
  1522. static struct sk_buff *link_insert_deferred_queue(struct link *l_ptr,
  1523. struct sk_buff *buf)
  1524. {
  1525. u32 seq_no;
  1526. if (l_ptr->oldest_deferred_in == NULL)
  1527. return buf;
  1528. seq_no = msg_seqno(buf_msg(l_ptr->oldest_deferred_in));
  1529. if (seq_no == mod(l_ptr->next_in_no)) {
  1530. l_ptr->newest_deferred_in->next = buf;
  1531. buf = l_ptr->oldest_deferred_in;
  1532. l_ptr->oldest_deferred_in = NULL;
  1533. l_ptr->deferred_inqueue_sz = 0;
  1534. }
  1535. return buf;
  1536. }
  1537. void tipc_recv_msg(struct sk_buff *head, struct tipc_bearer *tb_ptr)
  1538. {
  1539. read_lock_bh(&tipc_net_lock);
  1540. while (head) {
  1541. struct bearer *b_ptr;
  1542. struct node *n_ptr;
  1543. struct link *l_ptr;
  1544. struct sk_buff *crs;
  1545. struct sk_buff *buf = head;
  1546. struct tipc_msg *msg = buf_msg(buf);
  1547. u32 seq_no = msg_seqno(msg);
  1548. u32 ackd = msg_ack(msg);
  1549. u32 released = 0;
  1550. int type;
  1551. b_ptr = (struct bearer *)tb_ptr;
  1552. TIPC_SKB_CB(buf)->handle = b_ptr;
  1553. head = head->next;
  1554. if (unlikely(msg_version(msg) != TIPC_VERSION))
  1555. goto cont;
  1556. #if 0
  1557. if (msg_user(msg) != LINK_PROTOCOL)
  1558. #endif
  1559. msg_dbg(msg,"<REC<");
  1560. if (unlikely(msg_non_seq(msg))) {
  1561. link_recv_non_seq(buf);
  1562. continue;
  1563. }
  1564. if (unlikely(!msg_short(msg) &&
  1565. (msg_destnode(msg) != tipc_own_addr)))
  1566. goto cont;
  1567. n_ptr = tipc_node_find(msg_prevnode(msg));
  1568. if (unlikely(!n_ptr))
  1569. goto cont;
  1570. tipc_node_lock(n_ptr);
  1571. l_ptr = n_ptr->links[b_ptr->identity];
  1572. if (unlikely(!l_ptr)) {
  1573. tipc_node_unlock(n_ptr);
  1574. goto cont;
  1575. }
  1576. /*
  1577. * Release acked messages
  1578. */
  1579. if (less(n_ptr->bclink.acked, msg_bcast_ack(msg))) {
  1580. if (tipc_node_is_up(n_ptr) && n_ptr->bclink.supported)
  1581. tipc_bclink_acknowledge(n_ptr, msg_bcast_ack(msg));
  1582. }
  1583. crs = l_ptr->first_out;
  1584. while ((crs != l_ptr->next_out) &&
  1585. less_eq(msg_seqno(buf_msg(crs)), ackd)) {
  1586. struct sk_buff *next = crs->next;
  1587. buf_discard(crs);
  1588. crs = next;
  1589. released++;
  1590. }
  1591. if (released) {
  1592. l_ptr->first_out = crs;
  1593. l_ptr->out_queue_size -= released;
  1594. }
  1595. if (unlikely(l_ptr->next_out))
  1596. tipc_link_push_queue(l_ptr);
  1597. if (unlikely(!list_empty(&l_ptr->waiting_ports)))
  1598. tipc_link_wakeup_ports(l_ptr, 0);
  1599. if (unlikely(++l_ptr->unacked_window >= TIPC_MIN_LINK_WIN)) {
  1600. l_ptr->stats.sent_acks++;
  1601. tipc_link_send_proto_msg(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
  1602. }
  1603. protocol_check:
  1604. if (likely(link_working_working(l_ptr))) {
  1605. if (likely(seq_no == mod(l_ptr->next_in_no))) {
  1606. l_ptr->next_in_no++;
  1607. if (unlikely(l_ptr->oldest_deferred_in))
  1608. head = link_insert_deferred_queue(l_ptr,
  1609. head);
  1610. if (likely(msg_is_dest(msg, tipc_own_addr))) {
  1611. deliver:
  1612. if (likely(msg_isdata(msg))) {
  1613. tipc_node_unlock(n_ptr);
  1614. tipc_port_recv_msg(buf);
  1615. continue;
  1616. }
  1617. switch (msg_user(msg)) {
  1618. case MSG_BUNDLER:
  1619. l_ptr->stats.recv_bundles++;
  1620. l_ptr->stats.recv_bundled +=
  1621. msg_msgcnt(msg);
  1622. tipc_node_unlock(n_ptr);
  1623. tipc_link_recv_bundle(buf);
  1624. continue;
  1625. case ROUTE_DISTRIBUTOR:
  1626. tipc_node_unlock(n_ptr);
  1627. tipc_cltr_recv_routing_table(buf);
  1628. continue;
  1629. case NAME_DISTRIBUTOR:
  1630. tipc_node_unlock(n_ptr);
  1631. tipc_named_recv(buf);
  1632. continue;
  1633. case CONN_MANAGER:
  1634. tipc_node_unlock(n_ptr);
  1635. tipc_port_recv_proto_msg(buf);
  1636. continue;
  1637. case MSG_FRAGMENTER:
  1638. l_ptr->stats.recv_fragments++;
  1639. if (tipc_link_recv_fragment(&l_ptr->defragm_buf,
  1640. &buf, &msg)) {
  1641. l_ptr->stats.recv_fragmented++;
  1642. goto deliver;
  1643. }
  1644. break;
  1645. case CHANGEOVER_PROTOCOL:
  1646. type = msg_type(msg);
  1647. if (link_recv_changeover_msg(&l_ptr, &buf)) {
  1648. msg = buf_msg(buf);
  1649. seq_no = msg_seqno(msg);
  1650. TIPC_SKB_CB(buf)->handle
  1651. = b_ptr;
  1652. if (type == ORIGINAL_MSG)
  1653. goto deliver;
  1654. goto protocol_check;
  1655. }
  1656. break;
  1657. }
  1658. }
  1659. tipc_node_unlock(n_ptr);
  1660. tipc_net_route_msg(buf);
  1661. continue;
  1662. }
  1663. link_handle_out_of_seq_msg(l_ptr, buf);
  1664. head = link_insert_deferred_queue(l_ptr, head);
  1665. tipc_node_unlock(n_ptr);
  1666. continue;
  1667. }
  1668. if (msg_user(msg) == LINK_PROTOCOL) {
  1669. link_recv_proto_msg(l_ptr, buf);
  1670. head = link_insert_deferred_queue(l_ptr, head);
  1671. tipc_node_unlock(n_ptr);
  1672. continue;
  1673. }
  1674. msg_dbg(msg,"NSEQ<REC<");
  1675. link_state_event(l_ptr, TRAFFIC_MSG_EVT);
  1676. if (link_working_working(l_ptr)) {
  1677. /* Re-insert in front of queue */
  1678. msg_dbg(msg,"RECV-REINS:");
  1679. buf->next = head;
  1680. head = buf;
  1681. tipc_node_unlock(n_ptr);
  1682. continue;
  1683. }
  1684. tipc_node_unlock(n_ptr);
  1685. cont:
  1686. buf_discard(buf);
  1687. }
  1688. read_unlock_bh(&tipc_net_lock);
  1689. }
  1690. /*
  1691. * link_defer_buf(): Sort a received out-of-sequence packet
  1692. * into the deferred reception queue.
  1693. * Returns the increase of the queue length,i.e. 0 or 1
  1694. */
  1695. u32 tipc_link_defer_pkt(struct sk_buff **head,
  1696. struct sk_buff **tail,
  1697. struct sk_buff *buf)
  1698. {
  1699. struct sk_buff *prev = NULL;
  1700. struct sk_buff *crs = *head;
  1701. u32 seq_no = msg_seqno(buf_msg(buf));
  1702. buf->next = NULL;
  1703. /* Empty queue ? */
  1704. if (*head == NULL) {
  1705. *head = *tail = buf;
  1706. return 1;
  1707. }
  1708. /* Last ? */
  1709. if (less(msg_seqno(buf_msg(*tail)), seq_no)) {
  1710. (*tail)->next = buf;
  1711. *tail = buf;
  1712. return 1;
  1713. }
  1714. /* Scan through queue and sort it in */
  1715. do {
  1716. struct tipc_msg *msg = buf_msg(crs);
  1717. if (less(seq_no, msg_seqno(msg))) {
  1718. buf->next = crs;
  1719. if (prev)
  1720. prev->next = buf;
  1721. else
  1722. *head = buf;
  1723. return 1;
  1724. }
  1725. if (seq_no == msg_seqno(msg)) {
  1726. break;
  1727. }
  1728. prev = crs;
  1729. crs = crs->next;
  1730. }
  1731. while (crs);
  1732. /* Message is a duplicate of an existing message */
  1733. buf_discard(buf);
  1734. return 0;
  1735. }
  1736. /**
  1737. * link_handle_out_of_seq_msg - handle arrival of out-of-sequence packet
  1738. */
  1739. static void link_handle_out_of_seq_msg(struct link *l_ptr,
  1740. struct sk_buff *buf)
  1741. {
  1742. u32 seq_no = msg_seqno(buf_msg(buf));
  1743. if (likely(msg_user(buf_msg(buf)) == LINK_PROTOCOL)) {
  1744. link_recv_proto_msg(l_ptr, buf);
  1745. return;
  1746. }
  1747. dbg("rx OOS msg: seq_no %u, expecting %u (%u)\n",
  1748. seq_no, mod(l_ptr->next_in_no), l_ptr->next_in_no);
  1749. /* Record OOS packet arrival (force mismatch on next timeout) */
  1750. l_ptr->checkpoint--;
  1751. /*
  1752. * Discard packet if a duplicate; otherwise add it to deferred queue
  1753. * and notify peer of gap as per protocol specification
  1754. */
  1755. if (less(seq_no, mod(l_ptr->next_in_no))) {
  1756. l_ptr->stats.duplicates++;
  1757. buf_discard(buf);
  1758. return;
  1759. }
  1760. if (tipc_link_defer_pkt(&l_ptr->oldest_deferred_in,
  1761. &l_ptr->newest_deferred_in, buf)) {
  1762. l_ptr->deferred_inqueue_sz++;
  1763. l_ptr->stats.deferred_recv++;
  1764. if ((l_ptr->deferred_inqueue_sz % 16) == 1)
  1765. tipc_link_send_proto_msg(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
  1766. } else
  1767. l_ptr->stats.duplicates++;
  1768. }
  1769. /*
  1770. * Send protocol message to the other endpoint.
  1771. */
  1772. void tipc_link_send_proto_msg(struct link *l_ptr, u32 msg_typ, int probe_msg,
  1773. u32 gap, u32 tolerance, u32 priority, u32 ack_mtu)
  1774. {
  1775. struct sk_buff *buf = NULL;
  1776. struct tipc_msg *msg = l_ptr->pmsg;
  1777. u32 msg_size = sizeof(l_ptr->proto_msg);
  1778. if (link_blocked(l_ptr))
  1779. return;
  1780. msg_set_type(msg, msg_typ);
  1781. msg_set_net_plane(msg, l_ptr->b_ptr->net_plane);
  1782. msg_set_bcast_ack(msg, mod(l_ptr->owner->bclink.last_in));
  1783. msg_set_last_bcast(msg, tipc_bclink_get_last_sent());
  1784. if (msg_typ == STATE_MSG) {
  1785. u32 next_sent = mod(l_ptr->next_out_no);
  1786. if (!tipc_link_is_up(l_ptr))
  1787. return;
  1788. if (l_ptr->next_out)
  1789. next_sent = msg_seqno(buf_msg(l_ptr->next_out));
  1790. msg_set_next_sent(msg, next_sent);
  1791. if (l_ptr->oldest_deferred_in) {
  1792. u32 rec = msg_seqno(buf_msg(l_ptr->oldest_deferred_in));
  1793. gap = mod(rec - mod(l_ptr->next_in_no));
  1794. }
  1795. msg_set_seq_gap(msg, gap);
  1796. if (gap)
  1797. l_ptr->stats.sent_nacks++;
  1798. msg_set_link_tolerance(msg, tolerance);
  1799. msg_set_linkprio(msg, priority);
  1800. msg_set_max_pkt(msg, ack_mtu);
  1801. msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
  1802. msg_set_probe(msg, probe_msg != 0);
  1803. if (probe_msg) {
  1804. u32 mtu = l_ptr->max_pkt;
  1805. if ((mtu < l_ptr->max_pkt_target) &&
  1806. link_working_working(l_ptr) &&
  1807. l_ptr->fsm_msg_cnt) {
  1808. msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
  1809. if (l_ptr->max_pkt_probes == 10) {
  1810. l_ptr->max_pkt_target = (msg_size - 4);
  1811. l_ptr->max_pkt_probes = 0;
  1812. msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
  1813. }
  1814. l_ptr->max_pkt_probes++;
  1815. }
  1816. l_ptr->stats.sent_probes++;
  1817. }
  1818. l_ptr->stats.sent_states++;
  1819. } else { /* RESET_MSG or ACTIVATE_MSG */
  1820. msg_set_ack(msg, mod(l_ptr->reset_checkpoint - 1));
  1821. msg_set_seq_gap(msg, 0);
  1822. msg_set_next_sent(msg, 1);
  1823. msg_set_link_tolerance(msg, l_ptr->tolerance);
  1824. msg_set_linkprio(msg, l_ptr->priority);
  1825. msg_set_max_pkt(msg, l_ptr->max_pkt_target);
  1826. }
  1827. if (tipc_node_has_redundant_links(l_ptr->owner)) {
  1828. msg_set_redundant_link(msg);
  1829. } else {
  1830. msg_clear_redundant_link(msg);
  1831. }
  1832. msg_set_linkprio(msg, l_ptr->priority);
  1833. /* Ensure sequence number will not fit : */
  1834. msg_set_seqno(msg, mod(l_ptr->next_out_no + (0xffff/2)));
  1835. /* Congestion? */
  1836. if (tipc_bearer_congested(l_ptr->b_ptr, l_ptr)) {
  1837. if (!l_ptr->proto_msg_queue) {
  1838. l_ptr->proto_msg_queue =
  1839. buf_acquire(sizeof(l_ptr->proto_msg));
  1840. }
  1841. buf = l_ptr->proto_msg_queue;
  1842. if (!buf)
  1843. return;
  1844. memcpy(buf->data, (unchar *)msg, sizeof(l_ptr->proto_msg));
  1845. return;
  1846. }
  1847. msg_set_timestamp(msg, jiffies_to_msecs(jiffies));
  1848. /* Message can be sent */
  1849. msg_dbg(msg, ">>");
  1850. buf = buf_acquire(msg_size);
  1851. if (!buf)
  1852. return;
  1853. memcpy(buf->data, (unchar *)msg, sizeof(l_ptr->proto_msg));
  1854. msg_set_size(buf_msg(buf), msg_size);
  1855. if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
  1856. l_ptr->unacked_window = 0;
  1857. buf_discard(buf);
  1858. return;
  1859. }
  1860. /* New congestion */
  1861. tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
  1862. l_ptr->proto_msg_queue = buf;
  1863. l_ptr->stats.bearer_congs++;
  1864. }
  1865. /*
  1866. * Receive protocol message :
  1867. * Note that network plane id propagates through the network, and may
  1868. * change at any time. The node with lowest address rules
  1869. */
  1870. static void link_recv_proto_msg(struct link *l_ptr, struct sk_buff *buf)
  1871. {
  1872. u32 rec_gap = 0;
  1873. u32 max_pkt_info;
  1874. u32 max_pkt_ack;
  1875. u32 msg_tol;
  1876. struct tipc_msg *msg = buf_msg(buf);
  1877. dbg("AT(%u):", jiffies_to_msecs(jiffies));
  1878. msg_dbg(msg, "<<");
  1879. if (link_blocked(l_ptr))
  1880. goto exit;
  1881. /* record unnumbered packet arrival (force mismatch on next timeout) */
  1882. l_ptr->checkpoint--;
  1883. if (l_ptr->b_ptr->net_plane != msg_net_plane(msg))
  1884. if (tipc_own_addr > msg_prevnode(msg))
  1885. l_ptr->b_ptr->net_plane = msg_net_plane(msg);
  1886. l_ptr->owner->permit_changeover = msg_redundant_link(msg);
  1887. switch (msg_type(msg)) {
  1888. case RESET_MSG:
  1889. if (!link_working_unknown(l_ptr) && l_ptr->peer_session) {
  1890. if (msg_session(msg) == l_ptr->peer_session) {
  1891. dbg("Duplicate RESET: %u<->%u\n",
  1892. msg_session(msg), l_ptr->peer_session);
  1893. break; /* duplicate: ignore */
  1894. }
  1895. }
  1896. /* fall thru' */
  1897. case ACTIVATE_MSG:
  1898. /* Update link settings according other endpoint's values */
  1899. strcpy((strrchr(l_ptr->name, ':') + 1), (char *)msg_data(msg));
  1900. if ((msg_tol = msg_link_tolerance(msg)) &&
  1901. (msg_tol > l_ptr->tolerance))
  1902. link_set_supervision_props(l_ptr, msg_tol);
  1903. if (msg_linkprio(msg) > l_ptr->priority)
  1904. l_ptr->priority = msg_linkprio(msg);
  1905. max_pkt_info = msg_max_pkt(msg);
  1906. if (max_pkt_info) {
  1907. if (max_pkt_info < l_ptr->max_pkt_target)
  1908. l_ptr->max_pkt_target = max_pkt_info;
  1909. if (l_ptr->max_pkt > l_ptr->max_pkt_target)
  1910. l_ptr->max_pkt = l_ptr->max_pkt_target;
  1911. } else {
  1912. l_ptr->max_pkt = l_ptr->max_pkt_target;
  1913. }
  1914. l_ptr->owner->bclink.supported = (max_pkt_info != 0);
  1915. link_state_event(l_ptr, msg_type(msg));
  1916. l_ptr->peer_session = msg_session(msg);
  1917. l_ptr->peer_bearer_id = msg_bearer_id(msg);
  1918. /* Synchronize broadcast sequence numbers */
  1919. if (!tipc_node_has_redundant_links(l_ptr->owner)) {
  1920. l_ptr->owner->bclink.last_in = mod(msg_last_bcast(msg));
  1921. }
  1922. break;
  1923. case STATE_MSG:
  1924. if ((msg_tol = msg_link_tolerance(msg)))
  1925. link_set_supervision_props(l_ptr, msg_tol);
  1926. if (msg_linkprio(msg) &&
  1927. (msg_linkprio(msg) != l_ptr->priority)) {
  1928. warn("Resetting link <%s>, priority change %u->%u\n",
  1929. l_ptr->name, l_ptr->priority, msg_linkprio(msg));
  1930. l_ptr->priority = msg_linkprio(msg);
  1931. tipc_link_reset(l_ptr); /* Enforce change to take effect */
  1932. break;
  1933. }
  1934. link_state_event(l_ptr, TRAFFIC_MSG_EVT);
  1935. l_ptr->stats.recv_states++;
  1936. if (link_reset_unknown(l_ptr))
  1937. break;
  1938. if (less_eq(mod(l_ptr->next_in_no), msg_next_sent(msg))) {
  1939. rec_gap = mod(msg_next_sent(msg) -
  1940. mod(l_ptr->next_in_no));
  1941. }
  1942. max_pkt_ack = msg_max_pkt(msg);
  1943. if (max_pkt_ack > l_ptr->max_pkt) {
  1944. dbg("Link <%s> updated MTU %u -> %u\n",
  1945. l_ptr->name, l_ptr->max_pkt, max_pkt_ack);
  1946. l_ptr->max_pkt = max_pkt_ack;
  1947. l_ptr->max_pkt_probes = 0;
  1948. }
  1949. max_pkt_ack = 0;
  1950. if (msg_probe(msg)) {
  1951. l_ptr->stats.recv_probes++;
  1952. if (msg_size(msg) > sizeof(l_ptr->proto_msg)) {
  1953. max_pkt_ack = msg_size(msg);
  1954. }
  1955. }
  1956. /* Protocol message before retransmits, reduce loss risk */
  1957. tipc_bclink_check_gap(l_ptr->owner, msg_last_bcast(msg));
  1958. if (rec_gap || (msg_probe(msg))) {
  1959. tipc_link_send_proto_msg(l_ptr, STATE_MSG,
  1960. 0, rec_gap, 0, 0, max_pkt_ack);
  1961. }
  1962. if (msg_seq_gap(msg)) {
  1963. msg_dbg(msg, "With Gap:");
  1964. l_ptr->stats.recv_nacks++;
  1965. tipc_link_retransmit(l_ptr, l_ptr->first_out,
  1966. msg_seq_gap(msg));
  1967. }
  1968. break;
  1969. default:
  1970. msg_dbg(buf_msg(buf), "<DISCARDING UNKNOWN<");
  1971. }
  1972. exit:
  1973. buf_discard(buf);
  1974. }
  1975. /*
  1976. * tipc_link_tunnel(): Send one message via a link belonging to
  1977. * another bearer. Owner node is locked.
  1978. */
  1979. void tipc_link_tunnel(struct link *l_ptr,
  1980. struct tipc_msg *tunnel_hdr,
  1981. struct tipc_msg *msg,
  1982. u32 selector)
  1983. {
  1984. struct link *tunnel;
  1985. struct sk_buff *buf;
  1986. u32 length = msg_size(msg);
  1987. tunnel = l_ptr->owner->active_links[selector & 1];
  1988. if (!tipc_link_is_up(tunnel)) {
  1989. warn("Link changeover error, "
  1990. "tunnel link no longer available\n");
  1991. return;
  1992. }
  1993. msg_set_size(tunnel_hdr, length + INT_H_SIZE);
  1994. buf = buf_acquire(length + INT_H_SIZE);
  1995. if (!buf) {
  1996. warn("Link changeover error, "
  1997. "unable to send tunnel msg\n");
  1998. return;
  1999. }
  2000. memcpy(buf->data, (unchar *)tunnel_hdr, INT_H_SIZE);
  2001. memcpy(buf->data + INT_H_SIZE, (unchar *)msg, length);
  2002. dbg("%c->%c:", l_ptr->b_ptr->net_plane, tunnel->b_ptr->net_plane);
  2003. msg_dbg(buf_msg(buf), ">SEND>");
  2004. tipc_link_send_buf(tunnel, buf);
  2005. }
  2006. /*
  2007. * changeover(): Send whole message queue via the remaining link
  2008. * Owner node is locked.
  2009. */
  2010. void tipc_link_changeover(struct link *l_ptr)
  2011. {
  2012. u32 msgcount = l_ptr->out_queue_size;
  2013. struct sk_buff *crs = l_ptr->first_out;
  2014. struct link *tunnel = l_ptr->owner->active_links[0];
  2015. struct tipc_msg tunnel_hdr;
  2016. int split_bundles;
  2017. if (!tunnel)
  2018. return;
  2019. if (!l_ptr->owner->permit_changeover) {
  2020. warn("Link changeover error, "
  2021. "peer did not permit changeover\n");
  2022. return;
  2023. }
  2024. msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
  2025. ORIGINAL_MSG, TIPC_OK, INT_H_SIZE, l_ptr->addr);
  2026. msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
  2027. msg_set_msgcnt(&tunnel_hdr, msgcount);
  2028. dbg("Link changeover requires %u tunnel messages\n", msgcount);
  2029. if (!l_ptr->first_out) {
  2030. struct sk_buff *buf;
  2031. buf = buf_acquire(INT_H_SIZE);
  2032. if (buf) {
  2033. memcpy(buf->data, (unchar *)&tunnel_hdr, INT_H_SIZE);
  2034. msg_set_size(&tunnel_hdr, INT_H_SIZE);
  2035. dbg("%c->%c:", l_ptr->b_ptr->net_plane,
  2036. tunnel->b_ptr->net_plane);
  2037. msg_dbg(&tunnel_hdr, "EMPTY>SEND>");
  2038. tipc_link_send_buf(tunnel, buf);
  2039. } else {
  2040. warn("Link changeover error, "
  2041. "unable to send changeover msg\n");
  2042. }
  2043. return;
  2044. }
  2045. split_bundles = (l_ptr->owner->active_links[0] !=
  2046. l_ptr->owner->active_links[1]);
  2047. while (crs) {
  2048. struct tipc_msg *msg = buf_msg(crs);
  2049. if ((msg_user(msg) == MSG_BUNDLER) && split_bundles) {
  2050. u32 msgcount = msg_msgcnt(msg);
  2051. struct tipc_msg *m = msg_get_wrapped(msg);
  2052. unchar* pos = (unchar*)m;
  2053. while (msgcount--) {
  2054. msg_set_seqno(m,msg_seqno(msg));
  2055. tipc_link_tunnel(l_ptr, &tunnel_hdr, m,
  2056. msg_link_selector(m));
  2057. pos += align(msg_size(m));
  2058. m = (struct tipc_msg *)pos;
  2059. }
  2060. } else {
  2061. tipc_link_tunnel(l_ptr, &tunnel_hdr, msg,
  2062. msg_link_selector(msg));
  2063. }
  2064. crs = crs->next;
  2065. }
  2066. }
  2067. void tipc_link_send_duplicate(struct link *l_ptr, struct link *tunnel)
  2068. {
  2069. struct sk_buff *iter;
  2070. struct tipc_msg tunnel_hdr;
  2071. msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
  2072. DUPLICATE_MSG, TIPC_OK, INT_H_SIZE, l_ptr->addr);
  2073. msg_set_msgcnt(&tunnel_hdr, l_ptr->out_queue_size);
  2074. msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
  2075. iter = l_ptr->first_out;
  2076. while (iter) {
  2077. struct sk_buff *outbuf;
  2078. struct tipc_msg *msg = buf_msg(iter);
  2079. u32 length = msg_size(msg);
  2080. if (msg_user(msg) == MSG_BUNDLER)
  2081. msg_set_type(msg, CLOSED_MSG);
  2082. msg_set_ack(msg, mod(l_ptr->next_in_no - 1)); /* Update */
  2083. msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
  2084. msg_set_size(&tunnel_hdr, length + INT_H_SIZE);
  2085. outbuf = buf_acquire(length + INT_H_SIZE);
  2086. if (outbuf == NULL) {
  2087. warn("Link changeover error, "
  2088. "unable to send duplicate msg\n");
  2089. return;
  2090. }
  2091. memcpy(outbuf->data, (unchar *)&tunnel_hdr, INT_H_SIZE);
  2092. memcpy(outbuf->data + INT_H_SIZE, iter->data, length);
  2093. dbg("%c->%c:", l_ptr->b_ptr->net_plane,
  2094. tunnel->b_ptr->net_plane);
  2095. msg_dbg(buf_msg(outbuf), ">SEND>");
  2096. tipc_link_send_buf(tunnel, outbuf);
  2097. if (!tipc_link_is_up(l_ptr))
  2098. return;
  2099. iter = iter->next;
  2100. }
  2101. }
  2102. /**
  2103. * buf_extract - extracts embedded TIPC message from another message
  2104. * @skb: encapsulating message buffer
  2105. * @from_pos: offset to extract from
  2106. *
  2107. * Returns a new message buffer containing an embedded message. The
  2108. * encapsulating message itself is left unchanged.
  2109. */
  2110. static struct sk_buff *buf_extract(struct sk_buff *skb, u32 from_pos)
  2111. {
  2112. struct tipc_msg *msg = (struct tipc_msg *)(skb->data + from_pos);
  2113. u32 size = msg_size(msg);
  2114. struct sk_buff *eb;
  2115. eb = buf_acquire(size);
  2116. if (eb)
  2117. memcpy(eb->data, (unchar *)msg, size);
  2118. return eb;
  2119. }
  2120. /*
  2121. * link_recv_changeover_msg(): Receive tunneled packet sent
  2122. * via other link. Node is locked. Return extracted buffer.
  2123. */
  2124. static int link_recv_changeover_msg(struct link **l_ptr,
  2125. struct sk_buff **buf)
  2126. {
  2127. struct sk_buff *tunnel_buf = *buf;
  2128. struct link *dest_link;
  2129. struct tipc_msg *msg;
  2130. struct tipc_msg *tunnel_msg = buf_msg(tunnel_buf);
  2131. u32 msg_typ = msg_type(tunnel_msg);
  2132. u32 msg_count = msg_msgcnt(tunnel_msg);
  2133. dest_link = (*l_ptr)->owner->links[msg_bearer_id(tunnel_msg)];
  2134. if (!dest_link) {
  2135. msg_dbg(tunnel_msg, "NOLINK/<REC<");
  2136. goto exit;
  2137. }
  2138. if (dest_link == *l_ptr) {
  2139. err("Unexpected changeover message on link <%s>\n",
  2140. (*l_ptr)->name);
  2141. goto exit;
  2142. }
  2143. dbg("%c<-%c:", dest_link->b_ptr->net_plane,
  2144. (*l_ptr)->b_ptr->net_plane);
  2145. *l_ptr = dest_link;
  2146. msg = msg_get_wrapped(tunnel_msg);
  2147. if (msg_typ == DUPLICATE_MSG) {
  2148. if (less(msg_seqno(msg), mod(dest_link->next_in_no))) {
  2149. msg_dbg(tunnel_msg, "DROP/<REC<");
  2150. goto exit;
  2151. }
  2152. *buf = buf_extract(tunnel_buf,INT_H_SIZE);
  2153. if (*buf == NULL) {
  2154. warn("Link changeover error, duplicate msg dropped\n");
  2155. goto exit;
  2156. }
  2157. msg_dbg(tunnel_msg, "TNL<REC<");
  2158. buf_discard(tunnel_buf);
  2159. return 1;
  2160. }
  2161. /* First original message ?: */
  2162. if (tipc_link_is_up(dest_link)) {
  2163. msg_dbg(tunnel_msg, "UP/FIRST/<REC<");
  2164. info("Resetting link <%s>, changeover initiated by peer\n",
  2165. dest_link->name);
  2166. tipc_link_reset(dest_link);
  2167. dest_link->exp_msg_count = msg_count;
  2168. dbg("Expecting %u tunnelled messages\n", msg_count);
  2169. if (!msg_count)
  2170. goto exit;
  2171. } else if (dest_link->exp_msg_count == START_CHANGEOVER) {
  2172. msg_dbg(tunnel_msg, "BLK/FIRST/<REC<");
  2173. dest_link->exp_msg_count = msg_count;
  2174. dbg("Expecting %u tunnelled messages\n", msg_count);
  2175. if (!msg_count)
  2176. goto exit;
  2177. }
  2178. /* Receive original message */
  2179. if (dest_link->exp_msg_count == 0) {
  2180. warn("Link switchover error, "
  2181. "got too many tunnelled messages\n");
  2182. msg_dbg(tunnel_msg, "OVERDUE/DROP/<REC<");
  2183. dbg_print_link(dest_link, "LINK:");
  2184. goto exit;
  2185. }
  2186. dest_link->exp_msg_count--;
  2187. if (less(msg_seqno(msg), dest_link->reset_checkpoint)) {
  2188. msg_dbg(tunnel_msg, "DROP/DUPL/<REC<");
  2189. goto exit;
  2190. } else {
  2191. *buf = buf_extract(tunnel_buf, INT_H_SIZE);
  2192. if (*buf != NULL) {
  2193. msg_dbg(tunnel_msg, "TNL<REC<");
  2194. buf_discard(tunnel_buf);
  2195. return 1;
  2196. } else {
  2197. warn("Link changeover error, original msg dropped\n");
  2198. }
  2199. }
  2200. exit:
  2201. *buf = NULL;
  2202. buf_discard(tunnel_buf);
  2203. return 0;
  2204. }
  2205. /*
  2206. * Bundler functionality:
  2207. */
  2208. void tipc_link_recv_bundle(struct sk_buff *buf)
  2209. {
  2210. u32 msgcount = msg_msgcnt(buf_msg(buf));
  2211. u32 pos = INT_H_SIZE;
  2212. struct sk_buff *obuf;
  2213. msg_dbg(buf_msg(buf), "<BNDL<: ");
  2214. while (msgcount--) {
  2215. obuf = buf_extract(buf, pos);
  2216. if (obuf == NULL) {
  2217. warn("Link unable to unbundle message(s)\n");
  2218. break;
  2219. };
  2220. pos += align(msg_size(buf_msg(obuf)));
  2221. msg_dbg(buf_msg(obuf), " /");
  2222. tipc_net_route_msg(obuf);
  2223. }
  2224. buf_discard(buf);
  2225. }
  2226. /*
  2227. * Fragmentation/defragmentation:
  2228. */
  2229. /*
  2230. * tipc_link_send_long_buf: Entry for buffers needing fragmentation.
  2231. * The buffer is complete, inclusive total message length.
  2232. * Returns user data length.
  2233. */
  2234. int tipc_link_send_long_buf(struct link *l_ptr, struct sk_buff *buf)
  2235. {
  2236. struct tipc_msg *inmsg = buf_msg(buf);
  2237. struct tipc_msg fragm_hdr;
  2238. u32 insize = msg_size(inmsg);
  2239. u32 dsz = msg_data_sz(inmsg);
  2240. unchar *crs = buf->data;
  2241. u32 rest = insize;
  2242. u32 pack_sz = link_max_pkt(l_ptr);
  2243. u32 fragm_sz = pack_sz - INT_H_SIZE;
  2244. u32 fragm_no = 1;
  2245. u32 destaddr = msg_destnode(inmsg);
  2246. if (msg_short(inmsg))
  2247. destaddr = l_ptr->addr;
  2248. if (msg_routed(inmsg))
  2249. msg_set_prevnode(inmsg, tipc_own_addr);
  2250. /* Prepare reusable fragment header: */
  2251. msg_init(&fragm_hdr, MSG_FRAGMENTER, FIRST_FRAGMENT,
  2252. TIPC_OK, INT_H_SIZE, destaddr);
  2253. msg_set_link_selector(&fragm_hdr, msg_link_selector(inmsg));
  2254. msg_set_long_msgno(&fragm_hdr, mod(l_ptr->long_msg_seq_no++));
  2255. msg_set_fragm_no(&fragm_hdr, fragm_no);
  2256. l_ptr->stats.sent_fragmented++;
  2257. /* Chop up message: */
  2258. while (rest > 0) {
  2259. struct sk_buff *fragm;
  2260. if (rest <= fragm_sz) {
  2261. fragm_sz = rest;
  2262. msg_set_type(&fragm_hdr, LAST_FRAGMENT);
  2263. }
  2264. fragm = buf_acquire(fragm_sz + INT_H_SIZE);
  2265. if (fragm == NULL) {
  2266. warn("Link unable to fragment message\n");
  2267. dsz = -ENOMEM;
  2268. goto exit;
  2269. }
  2270. msg_set_size(&fragm_hdr, fragm_sz + INT_H_SIZE);
  2271. memcpy(fragm->data, (unchar *)&fragm_hdr, INT_H_SIZE);
  2272. memcpy(fragm->data + INT_H_SIZE, crs, fragm_sz);
  2273. /* Send queued messages first, if any: */
  2274. l_ptr->stats.sent_fragments++;
  2275. tipc_link_send_buf(l_ptr, fragm);
  2276. if (!tipc_link_is_up(l_ptr))
  2277. return dsz;
  2278. msg_set_fragm_no(&fragm_hdr, ++fragm_no);
  2279. rest -= fragm_sz;
  2280. crs += fragm_sz;
  2281. msg_set_type(&fragm_hdr, FRAGMENT);
  2282. }
  2283. exit:
  2284. buf_discard(buf);
  2285. return dsz;
  2286. }
  2287. /*
  2288. * A pending message being re-assembled must store certain values
  2289. * to handle subsequent fragments correctly. The following functions
  2290. * help storing these values in unused, available fields in the
  2291. * pending message. This makes dynamic memory allocation unecessary.
  2292. */
  2293. static void set_long_msg_seqno(struct sk_buff *buf, u32 seqno)
  2294. {
  2295. msg_set_seqno(buf_msg(buf), seqno);
  2296. }
  2297. static u32 get_fragm_size(struct sk_buff *buf)
  2298. {
  2299. return msg_ack(buf_msg(buf));
  2300. }
  2301. static void set_fragm_size(struct sk_buff *buf, u32 sz)
  2302. {
  2303. msg_set_ack(buf_msg(buf), sz);
  2304. }
  2305. static u32 get_expected_frags(struct sk_buff *buf)
  2306. {
  2307. return msg_bcast_ack(buf_msg(buf));
  2308. }
  2309. static void set_expected_frags(struct sk_buff *buf, u32 exp)
  2310. {
  2311. msg_set_bcast_ack(buf_msg(buf), exp);
  2312. }
  2313. static u32 get_timer_cnt(struct sk_buff *buf)
  2314. {
  2315. return msg_reroute_cnt(buf_msg(buf));
  2316. }
  2317. static void incr_timer_cnt(struct sk_buff *buf)
  2318. {
  2319. msg_incr_reroute_cnt(buf_msg(buf));
  2320. }
  2321. /*
  2322. * tipc_link_recv_fragment(): Called with node lock on. Returns
  2323. * the reassembled buffer if message is complete.
  2324. */
  2325. int tipc_link_recv_fragment(struct sk_buff **pending, struct sk_buff **fb,
  2326. struct tipc_msg **m)
  2327. {
  2328. struct sk_buff *prev = NULL;
  2329. struct sk_buff *fbuf = *fb;
  2330. struct tipc_msg *fragm = buf_msg(fbuf);
  2331. struct sk_buff *pbuf = *pending;
  2332. u32 long_msg_seq_no = msg_long_msgno(fragm);
  2333. *fb = NULL;
  2334. msg_dbg(fragm,"FRG<REC<");
  2335. /* Is there an incomplete message waiting for this fragment? */
  2336. while (pbuf && ((msg_seqno(buf_msg(pbuf)) != long_msg_seq_no)
  2337. || (msg_orignode(fragm) != msg_orignode(buf_msg(pbuf))))) {
  2338. prev = pbuf;
  2339. pbuf = pbuf->next;
  2340. }
  2341. if (!pbuf && (msg_type(fragm) == FIRST_FRAGMENT)) {
  2342. struct tipc_msg *imsg = (struct tipc_msg *)msg_data(fragm);
  2343. u32 msg_sz = msg_size(imsg);
  2344. u32 fragm_sz = msg_data_sz(fragm);
  2345. u32 exp_fragm_cnt = msg_sz/fragm_sz + !!(msg_sz % fragm_sz);
  2346. u32 max = TIPC_MAX_USER_MSG_SIZE + LONG_H_SIZE;
  2347. if (msg_type(imsg) == TIPC_MCAST_MSG)
  2348. max = TIPC_MAX_USER_MSG_SIZE + MCAST_H_SIZE;
  2349. if (msg_size(imsg) > max) {
  2350. msg_dbg(fragm,"<REC<Oversized: ");
  2351. buf_discard(fbuf);
  2352. return 0;
  2353. }
  2354. pbuf = buf_acquire(msg_size(imsg));
  2355. if (pbuf != NULL) {
  2356. pbuf->next = *pending;
  2357. *pending = pbuf;
  2358. memcpy(pbuf->data, (unchar *)imsg, msg_data_sz(fragm));
  2359. /* Prepare buffer for subsequent fragments. */
  2360. set_long_msg_seqno(pbuf, long_msg_seq_no);
  2361. set_fragm_size(pbuf,fragm_sz);
  2362. set_expected_frags(pbuf,exp_fragm_cnt - 1);
  2363. } else {
  2364. warn("Link unable to reassemble fragmented message\n");
  2365. }
  2366. buf_discard(fbuf);
  2367. return 0;
  2368. } else if (pbuf && (msg_type(fragm) != FIRST_FRAGMENT)) {
  2369. u32 dsz = msg_data_sz(fragm);
  2370. u32 fsz = get_fragm_size(pbuf);
  2371. u32 crs = ((msg_fragm_no(fragm) - 1) * fsz);
  2372. u32 exp_frags = get_expected_frags(pbuf) - 1;
  2373. memcpy(pbuf->data + crs, msg_data(fragm), dsz);
  2374. buf_discard(fbuf);
  2375. /* Is message complete? */
  2376. if (exp_frags == 0) {
  2377. if (prev)
  2378. prev->next = pbuf->next;
  2379. else
  2380. *pending = pbuf->next;
  2381. msg_reset_reroute_cnt(buf_msg(pbuf));
  2382. *fb = pbuf;
  2383. *m = buf_msg(pbuf);
  2384. return 1;
  2385. }
  2386. set_expected_frags(pbuf,exp_frags);
  2387. return 0;
  2388. }
  2389. dbg(" Discarding orphan fragment %x\n",fbuf);
  2390. msg_dbg(fragm,"ORPHAN:");
  2391. dbg("Pending long buffers:\n");
  2392. dbg_print_buf_chain(*pending);
  2393. buf_discard(fbuf);
  2394. return 0;
  2395. }
  2396. /**
  2397. * link_check_defragm_bufs - flush stale incoming message fragments
  2398. * @l_ptr: pointer to link
  2399. */
  2400. static void link_check_defragm_bufs(struct link *l_ptr)
  2401. {
  2402. struct sk_buff *prev = NULL;
  2403. struct sk_buff *next = NULL;
  2404. struct sk_buff *buf = l_ptr->defragm_buf;
  2405. if (!buf)
  2406. return;
  2407. if (!link_working_working(l_ptr))
  2408. return;
  2409. while (buf) {
  2410. u32 cnt = get_timer_cnt(buf);
  2411. next = buf->next;
  2412. if (cnt < 4) {
  2413. incr_timer_cnt(buf);
  2414. prev = buf;
  2415. } else {
  2416. dbg(" Discarding incomplete long buffer\n");
  2417. msg_dbg(buf_msg(buf), "LONG:");
  2418. dbg_print_link(l_ptr, "curr:");
  2419. dbg("Pending long buffers:\n");
  2420. dbg_print_buf_chain(l_ptr->defragm_buf);
  2421. if (prev)
  2422. prev->next = buf->next;
  2423. else
  2424. l_ptr->defragm_buf = buf->next;
  2425. buf_discard(buf);
  2426. }
  2427. buf = next;
  2428. }
  2429. }
  2430. static void link_set_supervision_props(struct link *l_ptr, u32 tolerance)
  2431. {
  2432. l_ptr->tolerance = tolerance;
  2433. l_ptr->continuity_interval =
  2434. ((tolerance / 4) > 500) ? 500 : tolerance / 4;
  2435. l_ptr->abort_limit = tolerance / (l_ptr->continuity_interval / 4);
  2436. }
  2437. void tipc_link_set_queue_limits(struct link *l_ptr, u32 window)
  2438. {
  2439. /* Data messages from this node, inclusive FIRST_FRAGM */
  2440. l_ptr->queue_limit[DATA_LOW] = window;
  2441. l_ptr->queue_limit[DATA_MEDIUM] = (window / 3) * 4;
  2442. l_ptr->queue_limit[DATA_HIGH] = (window / 3) * 5;
  2443. l_ptr->queue_limit[DATA_CRITICAL] = (window / 3) * 6;
  2444. /* Transiting data messages,inclusive FIRST_FRAGM */
  2445. l_ptr->queue_limit[DATA_LOW + 4] = 300;
  2446. l_ptr->queue_limit[DATA_MEDIUM + 4] = 600;
  2447. l_ptr->queue_limit[DATA_HIGH + 4] = 900;
  2448. l_ptr->queue_limit[DATA_CRITICAL + 4] = 1200;
  2449. l_ptr->queue_limit[CONN_MANAGER] = 1200;
  2450. l_ptr->queue_limit[ROUTE_DISTRIBUTOR] = 1200;
  2451. l_ptr->queue_limit[CHANGEOVER_PROTOCOL] = 2500;
  2452. l_ptr->queue_limit[NAME_DISTRIBUTOR] = 3000;
  2453. /* FRAGMENT and LAST_FRAGMENT packets */
  2454. l_ptr->queue_limit[MSG_FRAGMENTER] = 4000;
  2455. }
  2456. /**
  2457. * link_find_link - locate link by name
  2458. * @name - ptr to link name string
  2459. * @node - ptr to area to be filled with ptr to associated node
  2460. *
  2461. * Caller must hold 'tipc_net_lock' to ensure node and bearer are not deleted;
  2462. * this also prevents link deletion.
  2463. *
  2464. * Returns pointer to link (or 0 if invalid link name).
  2465. */
  2466. static struct link *link_find_link(const char *name, struct node **node)
  2467. {
  2468. struct link_name link_name_parts;
  2469. struct bearer *b_ptr;
  2470. struct link *l_ptr;
  2471. if (!link_name_validate(name, &link_name_parts))
  2472. return NULL;
  2473. b_ptr = tipc_bearer_find_interface(link_name_parts.if_local);
  2474. if (!b_ptr)
  2475. return NULL;
  2476. *node = tipc_node_find(link_name_parts.addr_peer);
  2477. if (!*node)
  2478. return NULL;
  2479. l_ptr = (*node)->links[b_ptr->identity];
  2480. if (!l_ptr || strcmp(l_ptr->name, name))
  2481. return NULL;
  2482. return l_ptr;
  2483. }
  2484. struct sk_buff *tipc_link_cmd_config(const void *req_tlv_area, int req_tlv_space,
  2485. u16 cmd)
  2486. {
  2487. struct tipc_link_config *args;
  2488. u32 new_value;
  2489. struct link *l_ptr;
  2490. struct node *node;
  2491. int res;
  2492. if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_CONFIG))
  2493. return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
  2494. args = (struct tipc_link_config *)TLV_DATA(req_tlv_area);
  2495. new_value = ntohl(args->value);
  2496. if (!strcmp(args->name, tipc_bclink_name)) {
  2497. if ((cmd == TIPC_CMD_SET_LINK_WINDOW) &&
  2498. (tipc_bclink_set_queue_limits(new_value) == 0))
  2499. return tipc_cfg_reply_none();
  2500. return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
  2501. " (cannot change setting on broadcast link)");
  2502. }
  2503. read_lock_bh(&tipc_net_lock);
  2504. l_ptr = link_find_link(args->name, &node);
  2505. if (!l_ptr) {
  2506. read_unlock_bh(&tipc_net_lock);
  2507. return tipc_cfg_reply_error_string("link not found");
  2508. }
  2509. tipc_node_lock(node);
  2510. res = -EINVAL;
  2511. switch (cmd) {
  2512. case TIPC_CMD_SET_LINK_TOL:
  2513. if ((new_value >= TIPC_MIN_LINK_TOL) &&
  2514. (new_value <= TIPC_MAX_LINK_TOL)) {
  2515. link_set_supervision_props(l_ptr, new_value);
  2516. tipc_link_send_proto_msg(l_ptr, STATE_MSG,
  2517. 0, 0, new_value, 0, 0);
  2518. res = TIPC_OK;
  2519. }
  2520. break;
  2521. case TIPC_CMD_SET_LINK_PRI:
  2522. if ((new_value >= TIPC_MIN_LINK_PRI) &&
  2523. (new_value <= TIPC_MAX_LINK_PRI)) {
  2524. l_ptr->priority = new_value;
  2525. tipc_link_send_proto_msg(l_ptr, STATE_MSG,
  2526. 0, 0, 0, new_value, 0);
  2527. res = TIPC_OK;
  2528. }
  2529. break;
  2530. case TIPC_CMD_SET_LINK_WINDOW:
  2531. if ((new_value >= TIPC_MIN_LINK_WIN) &&
  2532. (new_value <= TIPC_MAX_LINK_WIN)) {
  2533. tipc_link_set_queue_limits(l_ptr, new_value);
  2534. res = TIPC_OK;
  2535. }
  2536. break;
  2537. }
  2538. tipc_node_unlock(node);
  2539. read_unlock_bh(&tipc_net_lock);
  2540. if (res)
  2541. return tipc_cfg_reply_error_string("cannot change link setting");
  2542. return tipc_cfg_reply_none();
  2543. }
  2544. /**
  2545. * link_reset_statistics - reset link statistics
  2546. * @l_ptr: pointer to link
  2547. */
  2548. static void link_reset_statistics(struct link *l_ptr)
  2549. {
  2550. memset(&l_ptr->stats, 0, sizeof(l_ptr->stats));
  2551. l_ptr->stats.sent_info = l_ptr->next_out_no;
  2552. l_ptr->stats.recv_info = l_ptr->next_in_no;
  2553. }
  2554. struct sk_buff *tipc_link_cmd_reset_stats(const void *req_tlv_area, int req_tlv_space)
  2555. {
  2556. char *link_name;
  2557. struct link *l_ptr;
  2558. struct node *node;
  2559. if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
  2560. return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
  2561. link_name = (char *)TLV_DATA(req_tlv_area);
  2562. if (!strcmp(link_name, tipc_bclink_name)) {
  2563. if (tipc_bclink_reset_stats())
  2564. return tipc_cfg_reply_error_string("link not found");
  2565. return tipc_cfg_reply_none();
  2566. }
  2567. read_lock_bh(&tipc_net_lock);
  2568. l_ptr = link_find_link(link_name, &node);
  2569. if (!l_ptr) {
  2570. read_unlock_bh(&tipc_net_lock);
  2571. return tipc_cfg_reply_error_string("link not found");
  2572. }
  2573. tipc_node_lock(node);
  2574. link_reset_statistics(l_ptr);
  2575. tipc_node_unlock(node);
  2576. read_unlock_bh(&tipc_net_lock);
  2577. return tipc_cfg_reply_none();
  2578. }
  2579. /**
  2580. * percent - convert count to a percentage of total (rounding up or down)
  2581. */
  2582. static u32 percent(u32 count, u32 total)
  2583. {
  2584. return (count * 100 + (total / 2)) / total;
  2585. }
  2586. /**
  2587. * tipc_link_stats - print link statistics
  2588. * @name: link name
  2589. * @buf: print buffer area
  2590. * @buf_size: size of print buffer area
  2591. *
  2592. * Returns length of print buffer data string (or 0 if error)
  2593. */
  2594. static int tipc_link_stats(const char *name, char *buf, const u32 buf_size)
  2595. {
  2596. struct print_buf pb;
  2597. struct link *l_ptr;
  2598. struct node *node;
  2599. char *status;
  2600. u32 profile_total = 0;
  2601. if (!strcmp(name, tipc_bclink_name))
  2602. return tipc_bclink_stats(buf, buf_size);
  2603. tipc_printbuf_init(&pb, buf, buf_size);
  2604. read_lock_bh(&tipc_net_lock);
  2605. l_ptr = link_find_link(name, &node);
  2606. if (!l_ptr) {
  2607. read_unlock_bh(&tipc_net_lock);
  2608. return 0;
  2609. }
  2610. tipc_node_lock(node);
  2611. if (tipc_link_is_active(l_ptr))
  2612. status = "ACTIVE";
  2613. else if (tipc_link_is_up(l_ptr))
  2614. status = "STANDBY";
  2615. else
  2616. status = "DEFUNCT";
  2617. tipc_printf(&pb, "Link <%s>\n"
  2618. " %s MTU:%u Priority:%u Tolerance:%u ms"
  2619. " Window:%u packets\n",
  2620. l_ptr->name, status, link_max_pkt(l_ptr),
  2621. l_ptr->priority, l_ptr->tolerance, l_ptr->queue_limit[0]);
  2622. tipc_printf(&pb, " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
  2623. l_ptr->next_in_no - l_ptr->stats.recv_info,
  2624. l_ptr->stats.recv_fragments,
  2625. l_ptr->stats.recv_fragmented,
  2626. l_ptr->stats.recv_bundles,
  2627. l_ptr->stats.recv_bundled);
  2628. tipc_printf(&pb, " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
  2629. l_ptr->next_out_no - l_ptr->stats.sent_info,
  2630. l_ptr->stats.sent_fragments,
  2631. l_ptr->stats.sent_fragmented,
  2632. l_ptr->stats.sent_bundles,
  2633. l_ptr->stats.sent_bundled);
  2634. profile_total = l_ptr->stats.msg_length_counts;
  2635. if (!profile_total)
  2636. profile_total = 1;
  2637. tipc_printf(&pb, " TX profile sample:%u packets average:%u octets\n"
  2638. " 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% "
  2639. "-16354:%u%% -32768:%u%% -66000:%u%%\n",
  2640. l_ptr->stats.msg_length_counts,
  2641. l_ptr->stats.msg_lengths_total / profile_total,
  2642. percent(l_ptr->stats.msg_length_profile[0], profile_total),
  2643. percent(l_ptr->stats.msg_length_profile[1], profile_total),
  2644. percent(l_ptr->stats.msg_length_profile[2], profile_total),
  2645. percent(l_ptr->stats.msg_length_profile[3], profile_total),
  2646. percent(l_ptr->stats.msg_length_profile[4], profile_total),
  2647. percent(l_ptr->stats.msg_length_profile[5], profile_total),
  2648. percent(l_ptr->stats.msg_length_profile[6], profile_total));
  2649. tipc_printf(&pb, " RX states:%u probes:%u naks:%u defs:%u dups:%u\n",
  2650. l_ptr->stats.recv_states,
  2651. l_ptr->stats.recv_probes,
  2652. l_ptr->stats.recv_nacks,
  2653. l_ptr->stats.deferred_recv,
  2654. l_ptr->stats.duplicates);
  2655. tipc_printf(&pb, " TX states:%u probes:%u naks:%u acks:%u dups:%u\n",
  2656. l_ptr->stats.sent_states,
  2657. l_ptr->stats.sent_probes,
  2658. l_ptr->stats.sent_nacks,
  2659. l_ptr->stats.sent_acks,
  2660. l_ptr->stats.retransmitted);
  2661. tipc_printf(&pb, " Congestion bearer:%u link:%u Send queue max:%u avg:%u\n",
  2662. l_ptr->stats.bearer_congs,
  2663. l_ptr->stats.link_congs,
  2664. l_ptr->stats.max_queue_sz,
  2665. l_ptr->stats.queue_sz_counts
  2666. ? (l_ptr->stats.accu_queue_sz / l_ptr->stats.queue_sz_counts)
  2667. : 0);
  2668. tipc_node_unlock(node);
  2669. read_unlock_bh(&tipc_net_lock);
  2670. return tipc_printbuf_validate(&pb);
  2671. }
  2672. #define MAX_LINK_STATS_INFO 2000
  2673. struct sk_buff *tipc_link_cmd_show_stats(const void *req_tlv_area, int req_tlv_space)
  2674. {
  2675. struct sk_buff *buf;
  2676. struct tlv_desc *rep_tlv;
  2677. int str_len;
  2678. if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
  2679. return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
  2680. buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_LINK_STATS_INFO));
  2681. if (!buf)
  2682. return NULL;
  2683. rep_tlv = (struct tlv_desc *)buf->data;
  2684. str_len = tipc_link_stats((char *)TLV_DATA(req_tlv_area),
  2685. (char *)TLV_DATA(rep_tlv), MAX_LINK_STATS_INFO);
  2686. if (!str_len) {
  2687. buf_discard(buf);
  2688. return tipc_cfg_reply_error_string("link not found");
  2689. }
  2690. skb_put(buf, TLV_SPACE(str_len));
  2691. TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
  2692. return buf;
  2693. }
  2694. #if 0
  2695. int link_control(const char *name, u32 op, u32 val)
  2696. {
  2697. int res = -EINVAL;
  2698. struct link *l_ptr;
  2699. u32 bearer_id;
  2700. struct node * node;
  2701. u32 a;
  2702. a = link_name2addr(name, &bearer_id);
  2703. read_lock_bh(&tipc_net_lock);
  2704. node = tipc_node_find(a);
  2705. if (node) {
  2706. tipc_node_lock(node);
  2707. l_ptr = node->links[bearer_id];
  2708. if (l_ptr) {
  2709. if (op == TIPC_REMOVE_LINK) {
  2710. struct bearer *b_ptr = l_ptr->b_ptr;
  2711. spin_lock_bh(&b_ptr->publ.lock);
  2712. tipc_link_delete(l_ptr);
  2713. spin_unlock_bh(&b_ptr->publ.lock);
  2714. }
  2715. if (op == TIPC_CMD_BLOCK_LINK) {
  2716. tipc_link_reset(l_ptr);
  2717. l_ptr->blocked = 1;
  2718. }
  2719. if (op == TIPC_CMD_UNBLOCK_LINK) {
  2720. l_ptr->blocked = 0;
  2721. }
  2722. res = TIPC_OK;
  2723. }
  2724. tipc_node_unlock(node);
  2725. }
  2726. read_unlock_bh(&tipc_net_lock);
  2727. return res;
  2728. }
  2729. #endif
  2730. /**
  2731. * tipc_link_get_max_pkt - get maximum packet size to use when sending to destination
  2732. * @dest: network address of destination node
  2733. * @selector: used to select from set of active links
  2734. *
  2735. * If no active link can be found, uses default maximum packet size.
  2736. */
  2737. u32 tipc_link_get_max_pkt(u32 dest, u32 selector)
  2738. {
  2739. struct node *n_ptr;
  2740. struct link *l_ptr;
  2741. u32 res = MAX_PKT_DEFAULT;
  2742. if (dest == tipc_own_addr)
  2743. return MAX_MSG_SIZE;
  2744. read_lock_bh(&tipc_net_lock);
  2745. n_ptr = tipc_node_select(dest, selector);
  2746. if (n_ptr) {
  2747. tipc_node_lock(n_ptr);
  2748. l_ptr = n_ptr->active_links[selector & 1];
  2749. if (l_ptr)
  2750. res = link_max_pkt(l_ptr);
  2751. tipc_node_unlock(n_ptr);
  2752. }
  2753. read_unlock_bh(&tipc_net_lock);
  2754. return res;
  2755. }
  2756. #if 0
  2757. static void link_dump_rec_queue(struct link *l_ptr)
  2758. {
  2759. struct sk_buff *crs;
  2760. if (!l_ptr->oldest_deferred_in) {
  2761. info("Reception queue empty\n");
  2762. return;
  2763. }
  2764. info("Contents of Reception queue:\n");
  2765. crs = l_ptr->oldest_deferred_in;
  2766. while (crs) {
  2767. if (crs->data == (void *)0x0000a3a3) {
  2768. info("buffer %x invalid\n", crs);
  2769. return;
  2770. }
  2771. msg_dbg(buf_msg(crs), "In rec queue: \n");
  2772. crs = crs->next;
  2773. }
  2774. }
  2775. #endif
  2776. static void link_dump_send_queue(struct link *l_ptr)
  2777. {
  2778. if (l_ptr->next_out) {
  2779. info("\nContents of unsent queue:\n");
  2780. dbg_print_buf_chain(l_ptr->next_out);
  2781. }
  2782. info("\nContents of send queue:\n");
  2783. if (l_ptr->first_out) {
  2784. dbg_print_buf_chain(l_ptr->first_out);
  2785. }
  2786. info("Empty send queue\n");
  2787. }
  2788. static void link_print(struct link *l_ptr, struct print_buf *buf,
  2789. const char *str)
  2790. {
  2791. tipc_printf(buf, str);
  2792. if (link_reset_reset(l_ptr) || link_reset_unknown(l_ptr))
  2793. return;
  2794. tipc_printf(buf, "Link %x<%s>:",
  2795. l_ptr->addr, l_ptr->b_ptr->publ.name);
  2796. tipc_printf(buf, ": NXO(%u):", mod(l_ptr->next_out_no));
  2797. tipc_printf(buf, "NXI(%u):", mod(l_ptr->next_in_no));
  2798. tipc_printf(buf, "SQUE");
  2799. if (l_ptr->first_out) {
  2800. tipc_printf(buf, "[%u..", msg_seqno(buf_msg(l_ptr->first_out)));
  2801. if (l_ptr->next_out)
  2802. tipc_printf(buf, "%u..",
  2803. msg_seqno(buf_msg(l_ptr->next_out)));
  2804. tipc_printf(buf, "%u]",
  2805. msg_seqno(buf_msg
  2806. (l_ptr->last_out)), l_ptr->out_queue_size);
  2807. if ((mod(msg_seqno(buf_msg(l_ptr->last_out)) -
  2808. msg_seqno(buf_msg(l_ptr->first_out)))
  2809. != (l_ptr->out_queue_size - 1))
  2810. || (l_ptr->last_out->next != 0)) {
  2811. tipc_printf(buf, "\nSend queue inconsistency\n");
  2812. tipc_printf(buf, "first_out= %x ", l_ptr->first_out);
  2813. tipc_printf(buf, "next_out= %x ", l_ptr->next_out);
  2814. tipc_printf(buf, "last_out= %x ", l_ptr->last_out);
  2815. link_dump_send_queue(l_ptr);
  2816. }
  2817. } else
  2818. tipc_printf(buf, "[]");
  2819. tipc_printf(buf, "SQSIZ(%u)", l_ptr->out_queue_size);
  2820. if (l_ptr->oldest_deferred_in) {
  2821. u32 o = msg_seqno(buf_msg(l_ptr->oldest_deferred_in));
  2822. u32 n = msg_seqno(buf_msg(l_ptr->newest_deferred_in));
  2823. tipc_printf(buf, ":RQUE[%u..%u]", o, n);
  2824. if (l_ptr->deferred_inqueue_sz != mod((n + 1) - o)) {
  2825. tipc_printf(buf, ":RQSIZ(%u)",
  2826. l_ptr->deferred_inqueue_sz);
  2827. }
  2828. }
  2829. if (link_working_unknown(l_ptr))
  2830. tipc_printf(buf, ":WU");
  2831. if (link_reset_reset(l_ptr))
  2832. tipc_printf(buf, ":RR");
  2833. if (link_reset_unknown(l_ptr))
  2834. tipc_printf(buf, ":RU");
  2835. if (link_working_working(l_ptr))
  2836. tipc_printf(buf, ":WW");
  2837. tipc_printf(buf, "\n");
  2838. }