link.c 87 KB

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