link.c 84 KB

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