link.c 78 KB

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