link.c 85 KB

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