link.c 78 KB

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