link.c 87 KB

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