link.c 85 KB

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