link.c 78 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033
  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. read_lock_bh(&tipc_net_lock);
  900. n_ptr = tipc_node_find(destnode);
  901. if (likely(n_ptr)) {
  902. tipc_node_lock(n_ptr);
  903. l_ptr = n_ptr->active_links[selector];
  904. if (likely(l_ptr)) {
  905. res = link_send_buf_fast(l_ptr, buf, &dummy);
  906. tipc_node_unlock(n_ptr);
  907. read_unlock_bh(&tipc_net_lock);
  908. return res;
  909. }
  910. tipc_node_unlock(n_ptr);
  911. }
  912. read_unlock_bh(&tipc_net_lock);
  913. res = msg_data_sz(buf_msg(buf));
  914. tipc_reject_msg(buf, TIPC_ERR_NO_NODE);
  915. return res;
  916. }
  917. /*
  918. * tipc_link_send_sections_fast: Entry for messages where the
  919. * destination processor is known and the header is complete,
  920. * except for total message length.
  921. * Returns user data length or errno.
  922. */
  923. int tipc_link_send_sections_fast(struct tipc_port *sender,
  924. struct iovec const *msg_sect,
  925. const u32 num_sect,
  926. unsigned int total_len,
  927. u32 destaddr)
  928. {
  929. struct tipc_msg *hdr = &sender->phdr;
  930. struct link *l_ptr;
  931. struct sk_buff *buf;
  932. struct tipc_node *node;
  933. int res;
  934. u32 selector = msg_origport(hdr) & 1;
  935. again:
  936. /*
  937. * Try building message using port's max_pkt hint.
  938. * (Must not hold any locks while building message.)
  939. */
  940. res = tipc_msg_build(hdr, msg_sect, num_sect, total_len,
  941. sender->max_pkt, !sender->user_port, &buf);
  942. read_lock_bh(&tipc_net_lock);
  943. node = tipc_node_find(destaddr);
  944. if (likely(node)) {
  945. tipc_node_lock(node);
  946. l_ptr = node->active_links[selector];
  947. if (likely(l_ptr)) {
  948. if (likely(buf)) {
  949. res = link_send_buf_fast(l_ptr, buf,
  950. &sender->max_pkt);
  951. exit:
  952. tipc_node_unlock(node);
  953. read_unlock_bh(&tipc_net_lock);
  954. return res;
  955. }
  956. /* Exit if build request was invalid */
  957. if (unlikely(res < 0))
  958. goto exit;
  959. /* Exit if link (or bearer) is congested */
  960. if (link_congested(l_ptr) ||
  961. !list_empty(&l_ptr->b_ptr->cong_links)) {
  962. res = link_schedule_port(l_ptr,
  963. sender->ref, res);
  964. goto exit;
  965. }
  966. /*
  967. * Message size exceeds max_pkt hint; update hint,
  968. * then re-try fast path or fragment the message
  969. */
  970. sender->max_pkt = l_ptr->max_pkt;
  971. tipc_node_unlock(node);
  972. read_unlock_bh(&tipc_net_lock);
  973. if ((msg_hdr_sz(hdr) + res) <= sender->max_pkt)
  974. goto again;
  975. return link_send_sections_long(sender, msg_sect,
  976. num_sect, total_len,
  977. destaddr);
  978. }
  979. tipc_node_unlock(node);
  980. }
  981. read_unlock_bh(&tipc_net_lock);
  982. /* Couldn't find a link to the destination node */
  983. if (buf)
  984. return tipc_reject_msg(buf, TIPC_ERR_NO_NODE);
  985. if (res >= 0)
  986. return tipc_port_reject_sections(sender, hdr, msg_sect, num_sect,
  987. total_len, TIPC_ERR_NO_NODE);
  988. return res;
  989. }
  990. /*
  991. * link_send_sections_long(): Entry for long messages where the
  992. * destination node is known and the header is complete,
  993. * inclusive total message length.
  994. * Link and bearer congestion status have been checked to be ok,
  995. * and are ignored if they change.
  996. *
  997. * Note that fragments do not use the full link MTU so that they won't have
  998. * to undergo refragmentation if link changeover causes them to be sent
  999. * over another link with an additional tunnel header added as prefix.
  1000. * (Refragmentation will still occur if the other link has a smaller MTU.)
  1001. *
  1002. * Returns user data length or errno.
  1003. */
  1004. static int link_send_sections_long(struct tipc_port *sender,
  1005. struct iovec const *msg_sect,
  1006. u32 num_sect,
  1007. unsigned int total_len,
  1008. u32 destaddr)
  1009. {
  1010. struct link *l_ptr;
  1011. struct tipc_node *node;
  1012. struct tipc_msg *hdr = &sender->phdr;
  1013. u32 dsz = total_len;
  1014. u32 max_pkt, fragm_sz, rest;
  1015. struct tipc_msg fragm_hdr;
  1016. struct sk_buff *buf, *buf_chain, *prev;
  1017. u32 fragm_crs, fragm_rest, hsz, sect_rest;
  1018. const unchar *sect_crs;
  1019. int curr_sect;
  1020. u32 fragm_no;
  1021. again:
  1022. fragm_no = 1;
  1023. max_pkt = sender->max_pkt - INT_H_SIZE;
  1024. /* leave room for tunnel header in case of link changeover */
  1025. fragm_sz = max_pkt - INT_H_SIZE;
  1026. /* leave room for fragmentation header in each fragment */
  1027. rest = dsz;
  1028. fragm_crs = 0;
  1029. fragm_rest = 0;
  1030. sect_rest = 0;
  1031. sect_crs = NULL;
  1032. curr_sect = -1;
  1033. /* Prepare reusable fragment header: */
  1034. tipc_msg_init(&fragm_hdr, MSG_FRAGMENTER, FIRST_FRAGMENT,
  1035. INT_H_SIZE, msg_destnode(hdr));
  1036. msg_set_size(&fragm_hdr, max_pkt);
  1037. msg_set_fragm_no(&fragm_hdr, 1);
  1038. /* Prepare header of first fragment: */
  1039. buf_chain = buf = tipc_buf_acquire(max_pkt);
  1040. if (!buf)
  1041. return -ENOMEM;
  1042. buf->next = NULL;
  1043. skb_copy_to_linear_data(buf, &fragm_hdr, INT_H_SIZE);
  1044. hsz = msg_hdr_sz(hdr);
  1045. skb_copy_to_linear_data_offset(buf, INT_H_SIZE, hdr, hsz);
  1046. /* Chop up message: */
  1047. fragm_crs = INT_H_SIZE + hsz;
  1048. fragm_rest = fragm_sz - hsz;
  1049. do { /* For all sections */
  1050. u32 sz;
  1051. if (!sect_rest) {
  1052. sect_rest = msg_sect[++curr_sect].iov_len;
  1053. sect_crs = (const unchar *)msg_sect[curr_sect].iov_base;
  1054. }
  1055. if (sect_rest < fragm_rest)
  1056. sz = sect_rest;
  1057. else
  1058. sz = fragm_rest;
  1059. if (likely(!sender->user_port)) {
  1060. if (copy_from_user(buf->data + fragm_crs, sect_crs, sz)) {
  1061. error:
  1062. for (; buf_chain; buf_chain = buf) {
  1063. buf = buf_chain->next;
  1064. buf_discard(buf_chain);
  1065. }
  1066. return -EFAULT;
  1067. }
  1068. } else
  1069. skb_copy_to_linear_data_offset(buf, fragm_crs,
  1070. sect_crs, sz);
  1071. sect_crs += sz;
  1072. sect_rest -= sz;
  1073. fragm_crs += sz;
  1074. fragm_rest -= sz;
  1075. rest -= sz;
  1076. if (!fragm_rest && rest) {
  1077. /* Initiate new fragment: */
  1078. if (rest <= fragm_sz) {
  1079. fragm_sz = rest;
  1080. msg_set_type(&fragm_hdr, LAST_FRAGMENT);
  1081. } else {
  1082. msg_set_type(&fragm_hdr, FRAGMENT);
  1083. }
  1084. msg_set_size(&fragm_hdr, fragm_sz + INT_H_SIZE);
  1085. msg_set_fragm_no(&fragm_hdr, ++fragm_no);
  1086. prev = buf;
  1087. buf = tipc_buf_acquire(fragm_sz + INT_H_SIZE);
  1088. if (!buf)
  1089. goto error;
  1090. buf->next = NULL;
  1091. prev->next = buf;
  1092. skb_copy_to_linear_data(buf, &fragm_hdr, INT_H_SIZE);
  1093. fragm_crs = INT_H_SIZE;
  1094. fragm_rest = fragm_sz;
  1095. }
  1096. } while (rest > 0);
  1097. /*
  1098. * Now we have a buffer chain. Select a link and check
  1099. * that packet size is still OK
  1100. */
  1101. node = tipc_node_find(destaddr);
  1102. if (likely(node)) {
  1103. tipc_node_lock(node);
  1104. l_ptr = node->active_links[sender->ref & 1];
  1105. if (!l_ptr) {
  1106. tipc_node_unlock(node);
  1107. goto reject;
  1108. }
  1109. if (l_ptr->max_pkt < max_pkt) {
  1110. sender->max_pkt = l_ptr->max_pkt;
  1111. tipc_node_unlock(node);
  1112. for (; buf_chain; buf_chain = buf) {
  1113. buf = buf_chain->next;
  1114. buf_discard(buf_chain);
  1115. }
  1116. goto again;
  1117. }
  1118. } else {
  1119. reject:
  1120. for (; buf_chain; buf_chain = buf) {
  1121. buf = buf_chain->next;
  1122. buf_discard(buf_chain);
  1123. }
  1124. return tipc_port_reject_sections(sender, hdr, msg_sect, num_sect,
  1125. total_len, TIPC_ERR_NO_NODE);
  1126. }
  1127. /* Append chain of fragments to send queue & send them */
  1128. l_ptr->long_msg_seq_no++;
  1129. link_add_chain_to_outqueue(l_ptr, buf_chain, l_ptr->long_msg_seq_no);
  1130. l_ptr->stats.sent_fragments += fragm_no;
  1131. l_ptr->stats.sent_fragmented++;
  1132. tipc_link_push_queue(l_ptr);
  1133. tipc_node_unlock(node);
  1134. return dsz;
  1135. }
  1136. /*
  1137. * tipc_link_push_packet: Push one unsent packet to the media
  1138. */
  1139. u32 tipc_link_push_packet(struct link *l_ptr)
  1140. {
  1141. struct sk_buff *buf = l_ptr->first_out;
  1142. u32 r_q_size = l_ptr->retransm_queue_size;
  1143. u32 r_q_head = l_ptr->retransm_queue_head;
  1144. /* Step to position where retransmission failed, if any, */
  1145. /* consider that buffers may have been released in meantime */
  1146. if (r_q_size && buf) {
  1147. u32 last = lesser(mod(r_q_head + r_q_size),
  1148. link_last_sent(l_ptr));
  1149. u32 first = msg_seqno(buf_msg(buf));
  1150. while (buf && less(first, r_q_head)) {
  1151. first = mod(first + 1);
  1152. buf = buf->next;
  1153. }
  1154. l_ptr->retransm_queue_head = r_q_head = first;
  1155. l_ptr->retransm_queue_size = r_q_size = mod(last - first);
  1156. }
  1157. /* Continue retransmission now, if there is anything: */
  1158. if (r_q_size && buf) {
  1159. msg_set_ack(buf_msg(buf), mod(l_ptr->next_in_no - 1));
  1160. msg_set_bcast_ack(buf_msg(buf), l_ptr->owner->bclink.last_in);
  1161. if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
  1162. l_ptr->retransm_queue_head = mod(++r_q_head);
  1163. l_ptr->retransm_queue_size = --r_q_size;
  1164. l_ptr->stats.retransmitted++;
  1165. return 0;
  1166. } else {
  1167. l_ptr->stats.bearer_congs++;
  1168. return PUSH_FAILED;
  1169. }
  1170. }
  1171. /* Send deferred protocol message, if any: */
  1172. buf = l_ptr->proto_msg_queue;
  1173. if (buf) {
  1174. msg_set_ack(buf_msg(buf), mod(l_ptr->next_in_no - 1));
  1175. msg_set_bcast_ack(buf_msg(buf), l_ptr->owner->bclink.last_in);
  1176. if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
  1177. l_ptr->unacked_window = 0;
  1178. buf_discard(buf);
  1179. l_ptr->proto_msg_queue = NULL;
  1180. return 0;
  1181. } else {
  1182. l_ptr->stats.bearer_congs++;
  1183. return PUSH_FAILED;
  1184. }
  1185. }
  1186. /* Send one deferred data message, if send window not full: */
  1187. buf = l_ptr->next_out;
  1188. if (buf) {
  1189. struct tipc_msg *msg = buf_msg(buf);
  1190. u32 next = msg_seqno(msg);
  1191. u32 first = msg_seqno(buf_msg(l_ptr->first_out));
  1192. if (mod(next - first) < l_ptr->queue_limit[0]) {
  1193. msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
  1194. msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
  1195. if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
  1196. if (msg_user(msg) == MSG_BUNDLER)
  1197. msg_set_type(msg, CLOSED_MSG);
  1198. l_ptr->next_out = buf->next;
  1199. return 0;
  1200. } else {
  1201. l_ptr->stats.bearer_congs++;
  1202. return PUSH_FAILED;
  1203. }
  1204. }
  1205. }
  1206. return PUSH_FINISHED;
  1207. }
  1208. /*
  1209. * push_queue(): push out the unsent messages of a link where
  1210. * congestion has abated. Node is locked
  1211. */
  1212. void tipc_link_push_queue(struct link *l_ptr)
  1213. {
  1214. u32 res;
  1215. if (tipc_bearer_congested(l_ptr->b_ptr, l_ptr))
  1216. return;
  1217. do {
  1218. res = tipc_link_push_packet(l_ptr);
  1219. } while (!res);
  1220. if (res == PUSH_FAILED)
  1221. tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
  1222. }
  1223. static void link_reset_all(unsigned long addr)
  1224. {
  1225. struct tipc_node *n_ptr;
  1226. char addr_string[16];
  1227. u32 i;
  1228. read_lock_bh(&tipc_net_lock);
  1229. n_ptr = tipc_node_find((u32)addr);
  1230. if (!n_ptr) {
  1231. read_unlock_bh(&tipc_net_lock);
  1232. return; /* node no longer exists */
  1233. }
  1234. tipc_node_lock(n_ptr);
  1235. warn("Resetting all links to %s\n",
  1236. tipc_addr_string_fill(addr_string, n_ptr->addr));
  1237. for (i = 0; i < MAX_BEARERS; i++) {
  1238. if (n_ptr->links[i]) {
  1239. link_print(n_ptr->links[i], "Resetting link\n");
  1240. tipc_link_reset(n_ptr->links[i]);
  1241. }
  1242. }
  1243. tipc_node_unlock(n_ptr);
  1244. read_unlock_bh(&tipc_net_lock);
  1245. }
  1246. static void link_retransmit_failure(struct link *l_ptr, struct sk_buff *buf)
  1247. {
  1248. struct tipc_msg *msg = buf_msg(buf);
  1249. warn("Retransmission failure on link <%s>\n", l_ptr->name);
  1250. if (l_ptr->addr) {
  1251. /* Handle failure on standard link */
  1252. link_print(l_ptr, "Resetting link\n");
  1253. tipc_link_reset(l_ptr);
  1254. } else {
  1255. /* Handle failure on broadcast link */
  1256. struct tipc_node *n_ptr;
  1257. char addr_string[16];
  1258. info("Msg seq number: %u, ", msg_seqno(msg));
  1259. info("Outstanding acks: %lu\n",
  1260. (unsigned long) TIPC_SKB_CB(buf)->handle);
  1261. n_ptr = tipc_bclink_retransmit_to();
  1262. tipc_node_lock(n_ptr);
  1263. tipc_addr_string_fill(addr_string, n_ptr->addr);
  1264. info("Multicast link info for %s\n", addr_string);
  1265. info("Supported: %d, ", n_ptr->bclink.supported);
  1266. info("Acked: %u\n", n_ptr->bclink.acked);
  1267. info("Last in: %u, ", n_ptr->bclink.last_in);
  1268. info("Gap after: %u, ", n_ptr->bclink.gap_after);
  1269. info("Gap to: %u\n", n_ptr->bclink.gap_to);
  1270. info("Nack sync: %u\n\n", n_ptr->bclink.nack_sync);
  1271. tipc_k_signal((Handler)link_reset_all, (unsigned long)n_ptr->addr);
  1272. tipc_node_unlock(n_ptr);
  1273. l_ptr->stale_count = 0;
  1274. }
  1275. }
  1276. void tipc_link_retransmit(struct link *l_ptr, struct sk_buff *buf,
  1277. u32 retransmits)
  1278. {
  1279. struct tipc_msg *msg;
  1280. if (!buf)
  1281. return;
  1282. msg = buf_msg(buf);
  1283. if (tipc_bearer_congested(l_ptr->b_ptr, l_ptr)) {
  1284. if (l_ptr->retransm_queue_size == 0) {
  1285. l_ptr->retransm_queue_head = msg_seqno(msg);
  1286. l_ptr->retransm_queue_size = retransmits;
  1287. } else {
  1288. err("Unexpected retransmit on link %s (qsize=%d)\n",
  1289. l_ptr->name, l_ptr->retransm_queue_size);
  1290. }
  1291. return;
  1292. } else {
  1293. /* Detect repeated retransmit failures on uncongested bearer */
  1294. if (l_ptr->last_retransmitted == msg_seqno(msg)) {
  1295. if (++l_ptr->stale_count > 100) {
  1296. link_retransmit_failure(l_ptr, buf);
  1297. return;
  1298. }
  1299. } else {
  1300. l_ptr->last_retransmitted = msg_seqno(msg);
  1301. l_ptr->stale_count = 1;
  1302. }
  1303. }
  1304. while (retransmits && (buf != l_ptr->next_out) && buf) {
  1305. msg = buf_msg(buf);
  1306. msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
  1307. msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
  1308. if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
  1309. buf = buf->next;
  1310. retransmits--;
  1311. l_ptr->stats.retransmitted++;
  1312. } else {
  1313. tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
  1314. l_ptr->stats.bearer_congs++;
  1315. l_ptr->retransm_queue_head = msg_seqno(buf_msg(buf));
  1316. l_ptr->retransm_queue_size = retransmits;
  1317. return;
  1318. }
  1319. }
  1320. l_ptr->retransm_queue_head = l_ptr->retransm_queue_size = 0;
  1321. }
  1322. /**
  1323. * link_insert_deferred_queue - insert deferred messages back into receive chain
  1324. */
  1325. static struct sk_buff *link_insert_deferred_queue(struct link *l_ptr,
  1326. struct sk_buff *buf)
  1327. {
  1328. u32 seq_no;
  1329. if (l_ptr->oldest_deferred_in == NULL)
  1330. return buf;
  1331. seq_no = msg_seqno(buf_msg(l_ptr->oldest_deferred_in));
  1332. if (seq_no == mod(l_ptr->next_in_no)) {
  1333. l_ptr->newest_deferred_in->next = buf;
  1334. buf = l_ptr->oldest_deferred_in;
  1335. l_ptr->oldest_deferred_in = NULL;
  1336. l_ptr->deferred_inqueue_sz = 0;
  1337. }
  1338. return buf;
  1339. }
  1340. /**
  1341. * link_recv_buf_validate - validate basic format of received message
  1342. *
  1343. * This routine ensures a TIPC message has an acceptable header, and at least
  1344. * as much data as the header indicates it should. The routine also ensures
  1345. * that the entire message header is stored in the main fragment of the message
  1346. * buffer, to simplify future access to message header fields.
  1347. *
  1348. * Note: Having extra info present in the message header or data areas is OK.
  1349. * TIPC will ignore the excess, under the assumption that it is optional info
  1350. * introduced by a later release of the protocol.
  1351. */
  1352. static int link_recv_buf_validate(struct sk_buff *buf)
  1353. {
  1354. static u32 min_data_hdr_size[8] = {
  1355. SHORT_H_SIZE, MCAST_H_SIZE, NAMED_H_SIZE, BASIC_H_SIZE,
  1356. MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE
  1357. };
  1358. struct tipc_msg *msg;
  1359. u32 tipc_hdr[2];
  1360. u32 size;
  1361. u32 hdr_size;
  1362. u32 min_hdr_size;
  1363. if (unlikely(buf->len < MIN_H_SIZE))
  1364. return 0;
  1365. msg = skb_header_pointer(buf, 0, sizeof(tipc_hdr), tipc_hdr);
  1366. if (msg == NULL)
  1367. return 0;
  1368. if (unlikely(msg_version(msg) != TIPC_VERSION))
  1369. return 0;
  1370. size = msg_size(msg);
  1371. hdr_size = msg_hdr_sz(msg);
  1372. min_hdr_size = msg_isdata(msg) ?
  1373. min_data_hdr_size[msg_type(msg)] : INT_H_SIZE;
  1374. if (unlikely((hdr_size < min_hdr_size) ||
  1375. (size < hdr_size) ||
  1376. (buf->len < size) ||
  1377. (size - hdr_size > TIPC_MAX_USER_MSG_SIZE)))
  1378. return 0;
  1379. return pskb_may_pull(buf, hdr_size);
  1380. }
  1381. /**
  1382. * tipc_recv_msg - process TIPC messages arriving from off-node
  1383. * @head: pointer to message buffer chain
  1384. * @tb_ptr: pointer to bearer message arrived on
  1385. *
  1386. * Invoked with no locks held. Bearer pointer must point to a valid bearer
  1387. * structure (i.e. cannot be NULL), but bearer can be inactive.
  1388. */
  1389. void tipc_recv_msg(struct sk_buff *head, struct tipc_bearer *b_ptr)
  1390. {
  1391. read_lock_bh(&tipc_net_lock);
  1392. while (head) {
  1393. struct tipc_node *n_ptr;
  1394. struct link *l_ptr;
  1395. struct sk_buff *crs;
  1396. struct sk_buff *buf = head;
  1397. struct tipc_msg *msg;
  1398. u32 seq_no;
  1399. u32 ackd;
  1400. u32 released = 0;
  1401. int type;
  1402. head = head->next;
  1403. /* Ensure bearer is still enabled */
  1404. if (unlikely(!b_ptr->active))
  1405. goto cont;
  1406. /* Ensure message is well-formed */
  1407. if (unlikely(!link_recv_buf_validate(buf)))
  1408. goto cont;
  1409. /* Ensure message data is a single contiguous unit */
  1410. if (unlikely(buf_linearize(buf)))
  1411. goto cont;
  1412. /* Handle arrival of a non-unicast link message */
  1413. msg = buf_msg(buf);
  1414. if (unlikely(msg_non_seq(msg))) {
  1415. if (msg_user(msg) == LINK_CONFIG)
  1416. tipc_disc_recv_msg(buf, b_ptr);
  1417. else
  1418. tipc_bclink_recv_pkt(buf);
  1419. continue;
  1420. }
  1421. /* Discard unicast link messages destined for another node */
  1422. if (unlikely(!msg_short(msg) &&
  1423. (msg_destnode(msg) != tipc_own_addr)))
  1424. goto cont;
  1425. /* Locate neighboring node that sent message */
  1426. n_ptr = tipc_node_find(msg_prevnode(msg));
  1427. if (unlikely(!n_ptr))
  1428. goto cont;
  1429. tipc_node_lock(n_ptr);
  1430. /* Don't talk to neighbor during cleanup after last session */
  1431. if (n_ptr->cleanup_required) {
  1432. tipc_node_unlock(n_ptr);
  1433. goto cont;
  1434. }
  1435. /* Locate unicast link endpoint that should handle message */
  1436. l_ptr = n_ptr->links[b_ptr->identity];
  1437. if (unlikely(!l_ptr)) {
  1438. tipc_node_unlock(n_ptr);
  1439. goto cont;
  1440. }
  1441. /* Validate message sequence number info */
  1442. seq_no = msg_seqno(msg);
  1443. ackd = msg_ack(msg);
  1444. /* Release acked messages */
  1445. if (less(n_ptr->bclink.acked, msg_bcast_ack(msg))) {
  1446. if (tipc_node_is_up(n_ptr) && n_ptr->bclink.supported)
  1447. tipc_bclink_acknowledge(n_ptr, msg_bcast_ack(msg));
  1448. }
  1449. crs = l_ptr->first_out;
  1450. while ((crs != l_ptr->next_out) &&
  1451. less_eq(msg_seqno(buf_msg(crs)), ackd)) {
  1452. struct sk_buff *next = crs->next;
  1453. buf_discard(crs);
  1454. crs = next;
  1455. released++;
  1456. }
  1457. if (released) {
  1458. l_ptr->first_out = crs;
  1459. l_ptr->out_queue_size -= released;
  1460. }
  1461. /* Try sending any messages link endpoint has pending */
  1462. if (unlikely(l_ptr->next_out))
  1463. tipc_link_push_queue(l_ptr);
  1464. if (unlikely(!list_empty(&l_ptr->waiting_ports)))
  1465. tipc_link_wakeup_ports(l_ptr, 0);
  1466. if (unlikely(++l_ptr->unacked_window >= TIPC_MIN_LINK_WIN)) {
  1467. l_ptr->stats.sent_acks++;
  1468. tipc_link_send_proto_msg(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
  1469. }
  1470. /* Now (finally!) process the incoming message */
  1471. protocol_check:
  1472. if (likely(link_working_working(l_ptr))) {
  1473. if (likely(seq_no == mod(l_ptr->next_in_no))) {
  1474. l_ptr->next_in_no++;
  1475. if (unlikely(l_ptr->oldest_deferred_in))
  1476. head = link_insert_deferred_queue(l_ptr,
  1477. head);
  1478. if (likely(msg_is_dest(msg, tipc_own_addr))) {
  1479. deliver:
  1480. if (likely(msg_isdata(msg))) {
  1481. tipc_node_unlock(n_ptr);
  1482. tipc_port_recv_msg(buf);
  1483. continue;
  1484. }
  1485. switch (msg_user(msg)) {
  1486. case MSG_BUNDLER:
  1487. l_ptr->stats.recv_bundles++;
  1488. l_ptr->stats.recv_bundled +=
  1489. msg_msgcnt(msg);
  1490. tipc_node_unlock(n_ptr);
  1491. tipc_link_recv_bundle(buf);
  1492. continue;
  1493. case NAME_DISTRIBUTOR:
  1494. tipc_node_unlock(n_ptr);
  1495. tipc_named_recv(buf);
  1496. continue;
  1497. case CONN_MANAGER:
  1498. tipc_node_unlock(n_ptr);
  1499. tipc_port_recv_proto_msg(buf);
  1500. continue;
  1501. case MSG_FRAGMENTER:
  1502. l_ptr->stats.recv_fragments++;
  1503. if (tipc_link_recv_fragment(&l_ptr->defragm_buf,
  1504. &buf, &msg)) {
  1505. l_ptr->stats.recv_fragmented++;
  1506. goto deliver;
  1507. }
  1508. break;
  1509. case CHANGEOVER_PROTOCOL:
  1510. type = msg_type(msg);
  1511. if (link_recv_changeover_msg(&l_ptr, &buf)) {
  1512. msg = buf_msg(buf);
  1513. seq_no = msg_seqno(msg);
  1514. if (type == ORIGINAL_MSG)
  1515. goto deliver;
  1516. goto protocol_check;
  1517. }
  1518. break;
  1519. default:
  1520. buf_discard(buf);
  1521. buf = NULL;
  1522. break;
  1523. }
  1524. }
  1525. tipc_node_unlock(n_ptr);
  1526. tipc_net_route_msg(buf);
  1527. continue;
  1528. }
  1529. link_handle_out_of_seq_msg(l_ptr, buf);
  1530. head = link_insert_deferred_queue(l_ptr, head);
  1531. tipc_node_unlock(n_ptr);
  1532. continue;
  1533. }
  1534. if (msg_user(msg) == LINK_PROTOCOL) {
  1535. link_recv_proto_msg(l_ptr, buf);
  1536. head = link_insert_deferred_queue(l_ptr, head);
  1537. tipc_node_unlock(n_ptr);
  1538. continue;
  1539. }
  1540. link_state_event(l_ptr, TRAFFIC_MSG_EVT);
  1541. if (link_working_working(l_ptr)) {
  1542. /* Re-insert in front of queue */
  1543. buf->next = head;
  1544. head = buf;
  1545. tipc_node_unlock(n_ptr);
  1546. continue;
  1547. }
  1548. tipc_node_unlock(n_ptr);
  1549. cont:
  1550. buf_discard(buf);
  1551. }
  1552. read_unlock_bh(&tipc_net_lock);
  1553. }
  1554. /*
  1555. * link_defer_buf(): Sort a received out-of-sequence packet
  1556. * into the deferred reception queue.
  1557. * Returns the increase of the queue length,i.e. 0 or 1
  1558. */
  1559. u32 tipc_link_defer_pkt(struct sk_buff **head,
  1560. struct sk_buff **tail,
  1561. struct sk_buff *buf)
  1562. {
  1563. struct sk_buff *prev = NULL;
  1564. struct sk_buff *crs = *head;
  1565. u32 seq_no = msg_seqno(buf_msg(buf));
  1566. buf->next = NULL;
  1567. /* Empty queue ? */
  1568. if (*head == NULL) {
  1569. *head = *tail = buf;
  1570. return 1;
  1571. }
  1572. /* Last ? */
  1573. if (less(msg_seqno(buf_msg(*tail)), seq_no)) {
  1574. (*tail)->next = buf;
  1575. *tail = buf;
  1576. return 1;
  1577. }
  1578. /* Scan through queue and sort it in */
  1579. do {
  1580. struct tipc_msg *msg = buf_msg(crs);
  1581. if (less(seq_no, msg_seqno(msg))) {
  1582. buf->next = crs;
  1583. if (prev)
  1584. prev->next = buf;
  1585. else
  1586. *head = buf;
  1587. return 1;
  1588. }
  1589. if (seq_no == msg_seqno(msg))
  1590. break;
  1591. prev = crs;
  1592. crs = crs->next;
  1593. } while (crs);
  1594. /* Message is a duplicate of an existing message */
  1595. buf_discard(buf);
  1596. return 0;
  1597. }
  1598. /**
  1599. * link_handle_out_of_seq_msg - handle arrival of out-of-sequence packet
  1600. */
  1601. static void link_handle_out_of_seq_msg(struct link *l_ptr,
  1602. struct sk_buff *buf)
  1603. {
  1604. u32 seq_no = msg_seqno(buf_msg(buf));
  1605. if (likely(msg_user(buf_msg(buf)) == LINK_PROTOCOL)) {
  1606. link_recv_proto_msg(l_ptr, buf);
  1607. return;
  1608. }
  1609. /* Record OOS packet arrival (force mismatch on next timeout) */
  1610. l_ptr->checkpoint--;
  1611. /*
  1612. * Discard packet if a duplicate; otherwise add it to deferred queue
  1613. * and notify peer of gap as per protocol specification
  1614. */
  1615. if (less(seq_no, mod(l_ptr->next_in_no))) {
  1616. l_ptr->stats.duplicates++;
  1617. buf_discard(buf);
  1618. return;
  1619. }
  1620. if (tipc_link_defer_pkt(&l_ptr->oldest_deferred_in,
  1621. &l_ptr->newest_deferred_in, buf)) {
  1622. l_ptr->deferred_inqueue_sz++;
  1623. l_ptr->stats.deferred_recv++;
  1624. if ((l_ptr->deferred_inqueue_sz % 16) == 1)
  1625. tipc_link_send_proto_msg(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
  1626. } else
  1627. l_ptr->stats.duplicates++;
  1628. }
  1629. /*
  1630. * Send protocol message to the other endpoint.
  1631. */
  1632. void tipc_link_send_proto_msg(struct link *l_ptr, u32 msg_typ, int probe_msg,
  1633. u32 gap, u32 tolerance, u32 priority, u32 ack_mtu)
  1634. {
  1635. struct sk_buff *buf = NULL;
  1636. struct tipc_msg *msg = l_ptr->pmsg;
  1637. u32 msg_size = sizeof(l_ptr->proto_msg);
  1638. int r_flag;
  1639. if (link_blocked(l_ptr))
  1640. return;
  1641. msg_set_type(msg, msg_typ);
  1642. msg_set_net_plane(msg, l_ptr->b_ptr->net_plane);
  1643. msg_set_bcast_ack(msg, mod(l_ptr->owner->bclink.last_in));
  1644. msg_set_last_bcast(msg, tipc_bclink_get_last_sent());
  1645. if (msg_typ == STATE_MSG) {
  1646. u32 next_sent = mod(l_ptr->next_out_no);
  1647. if (!tipc_link_is_up(l_ptr))
  1648. return;
  1649. if (l_ptr->next_out)
  1650. next_sent = msg_seqno(buf_msg(l_ptr->next_out));
  1651. msg_set_next_sent(msg, next_sent);
  1652. if (l_ptr->oldest_deferred_in) {
  1653. u32 rec = msg_seqno(buf_msg(l_ptr->oldest_deferred_in));
  1654. gap = mod(rec - mod(l_ptr->next_in_no));
  1655. }
  1656. msg_set_seq_gap(msg, gap);
  1657. if (gap)
  1658. l_ptr->stats.sent_nacks++;
  1659. msg_set_link_tolerance(msg, tolerance);
  1660. msg_set_linkprio(msg, priority);
  1661. msg_set_max_pkt(msg, ack_mtu);
  1662. msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
  1663. msg_set_probe(msg, probe_msg != 0);
  1664. if (probe_msg) {
  1665. u32 mtu = l_ptr->max_pkt;
  1666. if ((mtu < l_ptr->max_pkt_target) &&
  1667. link_working_working(l_ptr) &&
  1668. l_ptr->fsm_msg_cnt) {
  1669. msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
  1670. if (l_ptr->max_pkt_probes == 10) {
  1671. l_ptr->max_pkt_target = (msg_size - 4);
  1672. l_ptr->max_pkt_probes = 0;
  1673. msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
  1674. }
  1675. l_ptr->max_pkt_probes++;
  1676. }
  1677. l_ptr->stats.sent_probes++;
  1678. }
  1679. l_ptr->stats.sent_states++;
  1680. } else { /* RESET_MSG or ACTIVATE_MSG */
  1681. msg_set_ack(msg, mod(l_ptr->reset_checkpoint - 1));
  1682. msg_set_seq_gap(msg, 0);
  1683. msg_set_next_sent(msg, 1);
  1684. msg_set_probe(msg, 0);
  1685. msg_set_link_tolerance(msg, l_ptr->tolerance);
  1686. msg_set_linkprio(msg, l_ptr->priority);
  1687. msg_set_max_pkt(msg, l_ptr->max_pkt_target);
  1688. }
  1689. r_flag = (l_ptr->owner->working_links > tipc_link_is_up(l_ptr));
  1690. msg_set_redundant_link(msg, r_flag);
  1691. msg_set_linkprio(msg, l_ptr->priority);
  1692. /* Ensure sequence number will not fit : */
  1693. msg_set_seqno(msg, mod(l_ptr->next_out_no + (0xffff/2)));
  1694. /* Congestion? */
  1695. if (tipc_bearer_congested(l_ptr->b_ptr, l_ptr)) {
  1696. if (!l_ptr->proto_msg_queue) {
  1697. l_ptr->proto_msg_queue =
  1698. tipc_buf_acquire(sizeof(l_ptr->proto_msg));
  1699. }
  1700. buf = l_ptr->proto_msg_queue;
  1701. if (!buf)
  1702. return;
  1703. skb_copy_to_linear_data(buf, msg, sizeof(l_ptr->proto_msg));
  1704. return;
  1705. }
  1706. /* Message can be sent */
  1707. buf = tipc_buf_acquire(msg_size);
  1708. if (!buf)
  1709. return;
  1710. skb_copy_to_linear_data(buf, msg, sizeof(l_ptr->proto_msg));
  1711. msg_set_size(buf_msg(buf), msg_size);
  1712. if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
  1713. l_ptr->unacked_window = 0;
  1714. buf_discard(buf);
  1715. return;
  1716. }
  1717. /* New congestion */
  1718. tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
  1719. l_ptr->proto_msg_queue = buf;
  1720. l_ptr->stats.bearer_congs++;
  1721. }
  1722. /*
  1723. * Receive protocol message :
  1724. * Note that network plane id propagates through the network, and may
  1725. * change at any time. The node with lowest address rules
  1726. */
  1727. static void link_recv_proto_msg(struct link *l_ptr, struct sk_buff *buf)
  1728. {
  1729. u32 rec_gap = 0;
  1730. u32 max_pkt_info;
  1731. u32 max_pkt_ack;
  1732. u32 msg_tol;
  1733. struct tipc_msg *msg = buf_msg(buf);
  1734. if (link_blocked(l_ptr))
  1735. goto exit;
  1736. /* record unnumbered packet arrival (force mismatch on next timeout) */
  1737. l_ptr->checkpoint--;
  1738. if (l_ptr->b_ptr->net_plane != msg_net_plane(msg))
  1739. if (tipc_own_addr > msg_prevnode(msg))
  1740. l_ptr->b_ptr->net_plane = msg_net_plane(msg);
  1741. l_ptr->owner->permit_changeover = msg_redundant_link(msg);
  1742. switch (msg_type(msg)) {
  1743. case RESET_MSG:
  1744. if (!link_working_unknown(l_ptr) &&
  1745. (l_ptr->peer_session != INVALID_SESSION)) {
  1746. if (less_eq(msg_session(msg), l_ptr->peer_session))
  1747. break; /* duplicate or old reset: ignore */
  1748. }
  1749. /* fall thru' */
  1750. case ACTIVATE_MSG:
  1751. /* Update link settings according other endpoint's values */
  1752. strcpy((strrchr(l_ptr->name, ':') + 1), (char *)msg_data(msg));
  1753. msg_tol = msg_link_tolerance(msg);
  1754. if (msg_tol > l_ptr->tolerance)
  1755. link_set_supervision_props(l_ptr, msg_tol);
  1756. if (msg_linkprio(msg) > l_ptr->priority)
  1757. l_ptr->priority = msg_linkprio(msg);
  1758. max_pkt_info = msg_max_pkt(msg);
  1759. if (max_pkt_info) {
  1760. if (max_pkt_info < l_ptr->max_pkt_target)
  1761. l_ptr->max_pkt_target = max_pkt_info;
  1762. if (l_ptr->max_pkt > l_ptr->max_pkt_target)
  1763. l_ptr->max_pkt = l_ptr->max_pkt_target;
  1764. } else {
  1765. l_ptr->max_pkt = l_ptr->max_pkt_target;
  1766. }
  1767. l_ptr->owner->bclink.supported = (max_pkt_info != 0);
  1768. link_state_event(l_ptr, msg_type(msg));
  1769. l_ptr->peer_session = msg_session(msg);
  1770. l_ptr->peer_bearer_id = msg_bearer_id(msg);
  1771. /* Synchronize broadcast sequence numbers */
  1772. if (!tipc_node_redundant_links(l_ptr->owner))
  1773. l_ptr->owner->bclink.last_in = mod(msg_last_bcast(msg));
  1774. break;
  1775. case STATE_MSG:
  1776. msg_tol = msg_link_tolerance(msg);
  1777. if (msg_tol)
  1778. link_set_supervision_props(l_ptr, msg_tol);
  1779. if (msg_linkprio(msg) &&
  1780. (msg_linkprio(msg) != l_ptr->priority)) {
  1781. warn("Resetting link <%s>, priority change %u->%u\n",
  1782. l_ptr->name, l_ptr->priority, msg_linkprio(msg));
  1783. l_ptr->priority = msg_linkprio(msg);
  1784. tipc_link_reset(l_ptr); /* Enforce change to take effect */
  1785. break;
  1786. }
  1787. link_state_event(l_ptr, TRAFFIC_MSG_EVT);
  1788. l_ptr->stats.recv_states++;
  1789. if (link_reset_unknown(l_ptr))
  1790. break;
  1791. if (less_eq(mod(l_ptr->next_in_no), msg_next_sent(msg))) {
  1792. rec_gap = mod(msg_next_sent(msg) -
  1793. mod(l_ptr->next_in_no));
  1794. }
  1795. max_pkt_ack = msg_max_pkt(msg);
  1796. if (max_pkt_ack > l_ptr->max_pkt) {
  1797. l_ptr->max_pkt = max_pkt_ack;
  1798. l_ptr->max_pkt_probes = 0;
  1799. }
  1800. max_pkt_ack = 0;
  1801. if (msg_probe(msg)) {
  1802. l_ptr->stats.recv_probes++;
  1803. if (msg_size(msg) > sizeof(l_ptr->proto_msg))
  1804. max_pkt_ack = msg_size(msg);
  1805. }
  1806. /* Protocol message before retransmits, reduce loss risk */
  1807. tipc_bclink_check_gap(l_ptr->owner, msg_last_bcast(msg));
  1808. if (rec_gap || (msg_probe(msg))) {
  1809. tipc_link_send_proto_msg(l_ptr, STATE_MSG,
  1810. 0, rec_gap, 0, 0, max_pkt_ack);
  1811. }
  1812. if (msg_seq_gap(msg)) {
  1813. l_ptr->stats.recv_nacks++;
  1814. tipc_link_retransmit(l_ptr, l_ptr->first_out,
  1815. msg_seq_gap(msg));
  1816. }
  1817. break;
  1818. }
  1819. exit:
  1820. buf_discard(buf);
  1821. }
  1822. /*
  1823. * tipc_link_tunnel(): Send one message via a link belonging to
  1824. * another bearer. Owner node is locked.
  1825. */
  1826. static void tipc_link_tunnel(struct link *l_ptr,
  1827. struct tipc_msg *tunnel_hdr,
  1828. struct tipc_msg *msg,
  1829. u32 selector)
  1830. {
  1831. struct link *tunnel;
  1832. struct sk_buff *buf;
  1833. u32 length = msg_size(msg);
  1834. tunnel = l_ptr->owner->active_links[selector & 1];
  1835. if (!tipc_link_is_up(tunnel)) {
  1836. warn("Link changeover error, "
  1837. "tunnel link no longer available\n");
  1838. return;
  1839. }
  1840. msg_set_size(tunnel_hdr, length + INT_H_SIZE);
  1841. buf = tipc_buf_acquire(length + INT_H_SIZE);
  1842. if (!buf) {
  1843. warn("Link changeover error, "
  1844. "unable to send tunnel msg\n");
  1845. return;
  1846. }
  1847. skb_copy_to_linear_data(buf, tunnel_hdr, INT_H_SIZE);
  1848. skb_copy_to_linear_data_offset(buf, INT_H_SIZE, msg, length);
  1849. tipc_link_send_buf(tunnel, buf);
  1850. }
  1851. /*
  1852. * changeover(): Send whole message queue via the remaining link
  1853. * Owner node is locked.
  1854. */
  1855. void tipc_link_changeover(struct link *l_ptr)
  1856. {
  1857. u32 msgcount = l_ptr->out_queue_size;
  1858. struct sk_buff *crs = l_ptr->first_out;
  1859. struct link *tunnel = l_ptr->owner->active_links[0];
  1860. struct tipc_msg tunnel_hdr;
  1861. int split_bundles;
  1862. if (!tunnel)
  1863. return;
  1864. if (!l_ptr->owner->permit_changeover) {
  1865. warn("Link changeover error, "
  1866. "peer did not permit changeover\n");
  1867. return;
  1868. }
  1869. tipc_msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
  1870. ORIGINAL_MSG, INT_H_SIZE, l_ptr->addr);
  1871. msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
  1872. msg_set_msgcnt(&tunnel_hdr, msgcount);
  1873. if (!l_ptr->first_out) {
  1874. struct sk_buff *buf;
  1875. buf = tipc_buf_acquire(INT_H_SIZE);
  1876. if (buf) {
  1877. skb_copy_to_linear_data(buf, &tunnel_hdr, INT_H_SIZE);
  1878. msg_set_size(&tunnel_hdr, INT_H_SIZE);
  1879. tipc_link_send_buf(tunnel, buf);
  1880. } else {
  1881. warn("Link changeover error, "
  1882. "unable to send changeover msg\n");
  1883. }
  1884. return;
  1885. }
  1886. split_bundles = (l_ptr->owner->active_links[0] !=
  1887. l_ptr->owner->active_links[1]);
  1888. while (crs) {
  1889. struct tipc_msg *msg = buf_msg(crs);
  1890. if ((msg_user(msg) == MSG_BUNDLER) && split_bundles) {
  1891. struct tipc_msg *m = msg_get_wrapped(msg);
  1892. unchar *pos = (unchar *)m;
  1893. msgcount = msg_msgcnt(msg);
  1894. while (msgcount--) {
  1895. msg_set_seqno(m, msg_seqno(msg));
  1896. tipc_link_tunnel(l_ptr, &tunnel_hdr, m,
  1897. msg_link_selector(m));
  1898. pos += align(msg_size(m));
  1899. m = (struct tipc_msg *)pos;
  1900. }
  1901. } else {
  1902. tipc_link_tunnel(l_ptr, &tunnel_hdr, msg,
  1903. msg_link_selector(msg));
  1904. }
  1905. crs = crs->next;
  1906. }
  1907. }
  1908. void tipc_link_send_duplicate(struct link *l_ptr, struct link *tunnel)
  1909. {
  1910. struct sk_buff *iter;
  1911. struct tipc_msg tunnel_hdr;
  1912. tipc_msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
  1913. DUPLICATE_MSG, INT_H_SIZE, l_ptr->addr);
  1914. msg_set_msgcnt(&tunnel_hdr, l_ptr->out_queue_size);
  1915. msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
  1916. iter = l_ptr->first_out;
  1917. while (iter) {
  1918. struct sk_buff *outbuf;
  1919. struct tipc_msg *msg = buf_msg(iter);
  1920. u32 length = msg_size(msg);
  1921. if (msg_user(msg) == MSG_BUNDLER)
  1922. msg_set_type(msg, CLOSED_MSG);
  1923. msg_set_ack(msg, mod(l_ptr->next_in_no - 1)); /* Update */
  1924. msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
  1925. msg_set_size(&tunnel_hdr, length + INT_H_SIZE);
  1926. outbuf = tipc_buf_acquire(length + INT_H_SIZE);
  1927. if (outbuf == NULL) {
  1928. warn("Link changeover error, "
  1929. "unable to send duplicate msg\n");
  1930. return;
  1931. }
  1932. skb_copy_to_linear_data(outbuf, &tunnel_hdr, INT_H_SIZE);
  1933. skb_copy_to_linear_data_offset(outbuf, INT_H_SIZE, iter->data,
  1934. length);
  1935. tipc_link_send_buf(tunnel, outbuf);
  1936. if (!tipc_link_is_up(l_ptr))
  1937. return;
  1938. iter = iter->next;
  1939. }
  1940. }
  1941. /**
  1942. * buf_extract - extracts embedded TIPC message from another message
  1943. * @skb: encapsulating message buffer
  1944. * @from_pos: offset to extract from
  1945. *
  1946. * Returns a new message buffer containing an embedded message. The
  1947. * encapsulating message itself is left unchanged.
  1948. */
  1949. static struct sk_buff *buf_extract(struct sk_buff *skb, u32 from_pos)
  1950. {
  1951. struct tipc_msg *msg = (struct tipc_msg *)(skb->data + from_pos);
  1952. u32 size = msg_size(msg);
  1953. struct sk_buff *eb;
  1954. eb = tipc_buf_acquire(size);
  1955. if (eb)
  1956. skb_copy_to_linear_data(eb, msg, size);
  1957. return eb;
  1958. }
  1959. /*
  1960. * link_recv_changeover_msg(): Receive tunneled packet sent
  1961. * via other link. Node is locked. Return extracted buffer.
  1962. */
  1963. static int link_recv_changeover_msg(struct link **l_ptr,
  1964. struct sk_buff **buf)
  1965. {
  1966. struct sk_buff *tunnel_buf = *buf;
  1967. struct link *dest_link;
  1968. struct tipc_msg *msg;
  1969. struct tipc_msg *tunnel_msg = buf_msg(tunnel_buf);
  1970. u32 msg_typ = msg_type(tunnel_msg);
  1971. u32 msg_count = msg_msgcnt(tunnel_msg);
  1972. dest_link = (*l_ptr)->owner->links[msg_bearer_id(tunnel_msg)];
  1973. if (!dest_link)
  1974. goto exit;
  1975. if (dest_link == *l_ptr) {
  1976. err("Unexpected changeover message on link <%s>\n",
  1977. (*l_ptr)->name);
  1978. goto exit;
  1979. }
  1980. *l_ptr = dest_link;
  1981. msg = msg_get_wrapped(tunnel_msg);
  1982. if (msg_typ == DUPLICATE_MSG) {
  1983. if (less(msg_seqno(msg), mod(dest_link->next_in_no)))
  1984. goto exit;
  1985. *buf = buf_extract(tunnel_buf, INT_H_SIZE);
  1986. if (*buf == NULL) {
  1987. warn("Link changeover error, duplicate msg dropped\n");
  1988. goto exit;
  1989. }
  1990. buf_discard(tunnel_buf);
  1991. return 1;
  1992. }
  1993. /* First original message ?: */
  1994. if (tipc_link_is_up(dest_link)) {
  1995. info("Resetting link <%s>, changeover initiated by peer\n",
  1996. dest_link->name);
  1997. tipc_link_reset(dest_link);
  1998. dest_link->exp_msg_count = msg_count;
  1999. if (!msg_count)
  2000. goto exit;
  2001. } else if (dest_link->exp_msg_count == START_CHANGEOVER) {
  2002. dest_link->exp_msg_count = msg_count;
  2003. if (!msg_count)
  2004. goto exit;
  2005. }
  2006. /* Receive original message */
  2007. if (dest_link->exp_msg_count == 0) {
  2008. warn("Link switchover error, "
  2009. "got too many tunnelled messages\n");
  2010. goto exit;
  2011. }
  2012. dest_link->exp_msg_count--;
  2013. if (less(msg_seqno(msg), dest_link->reset_checkpoint)) {
  2014. goto exit;
  2015. } else {
  2016. *buf = buf_extract(tunnel_buf, INT_H_SIZE);
  2017. if (*buf != NULL) {
  2018. buf_discard(tunnel_buf);
  2019. return 1;
  2020. } else {
  2021. warn("Link changeover error, original msg dropped\n");
  2022. }
  2023. }
  2024. exit:
  2025. *buf = NULL;
  2026. buf_discard(tunnel_buf);
  2027. return 0;
  2028. }
  2029. /*
  2030. * Bundler functionality:
  2031. */
  2032. void tipc_link_recv_bundle(struct sk_buff *buf)
  2033. {
  2034. u32 msgcount = msg_msgcnt(buf_msg(buf));
  2035. u32 pos = INT_H_SIZE;
  2036. struct sk_buff *obuf;
  2037. while (msgcount--) {
  2038. obuf = buf_extract(buf, pos);
  2039. if (obuf == NULL) {
  2040. warn("Link unable to unbundle message(s)\n");
  2041. break;
  2042. }
  2043. pos += align(msg_size(buf_msg(obuf)));
  2044. tipc_net_route_msg(obuf);
  2045. }
  2046. buf_discard(buf);
  2047. }
  2048. /*
  2049. * Fragmentation/defragmentation:
  2050. */
  2051. /*
  2052. * link_send_long_buf: Entry for buffers needing fragmentation.
  2053. * The buffer is complete, inclusive total message length.
  2054. * Returns user data length.
  2055. */
  2056. static int link_send_long_buf(struct link *l_ptr, struct sk_buff *buf)
  2057. {
  2058. struct sk_buff *buf_chain = NULL;
  2059. struct sk_buff *buf_chain_tail = (struct sk_buff *)&buf_chain;
  2060. struct tipc_msg *inmsg = buf_msg(buf);
  2061. struct tipc_msg fragm_hdr;
  2062. u32 insize = msg_size(inmsg);
  2063. u32 dsz = msg_data_sz(inmsg);
  2064. unchar *crs = buf->data;
  2065. u32 rest = insize;
  2066. u32 pack_sz = l_ptr->max_pkt;
  2067. u32 fragm_sz = pack_sz - INT_H_SIZE;
  2068. u32 fragm_no = 0;
  2069. u32 destaddr;
  2070. if (msg_short(inmsg))
  2071. destaddr = l_ptr->addr;
  2072. else
  2073. destaddr = msg_destnode(inmsg);
  2074. /* Prepare reusable fragment header: */
  2075. tipc_msg_init(&fragm_hdr, MSG_FRAGMENTER, FIRST_FRAGMENT,
  2076. INT_H_SIZE, destaddr);
  2077. /* Chop up message: */
  2078. while (rest > 0) {
  2079. struct sk_buff *fragm;
  2080. if (rest <= fragm_sz) {
  2081. fragm_sz = rest;
  2082. msg_set_type(&fragm_hdr, LAST_FRAGMENT);
  2083. }
  2084. fragm = tipc_buf_acquire(fragm_sz + INT_H_SIZE);
  2085. if (fragm == NULL) {
  2086. buf_discard(buf);
  2087. while (buf_chain) {
  2088. buf = buf_chain;
  2089. buf_chain = buf_chain->next;
  2090. buf_discard(buf);
  2091. }
  2092. return -ENOMEM;
  2093. }
  2094. msg_set_size(&fragm_hdr, fragm_sz + INT_H_SIZE);
  2095. fragm_no++;
  2096. msg_set_fragm_no(&fragm_hdr, fragm_no);
  2097. skb_copy_to_linear_data(fragm, &fragm_hdr, INT_H_SIZE);
  2098. skb_copy_to_linear_data_offset(fragm, INT_H_SIZE, crs,
  2099. fragm_sz);
  2100. buf_chain_tail->next = fragm;
  2101. buf_chain_tail = fragm;
  2102. rest -= fragm_sz;
  2103. crs += fragm_sz;
  2104. msg_set_type(&fragm_hdr, FRAGMENT);
  2105. }
  2106. buf_discard(buf);
  2107. /* Append chain of fragments to send queue & send them */
  2108. l_ptr->long_msg_seq_no++;
  2109. link_add_chain_to_outqueue(l_ptr, buf_chain, l_ptr->long_msg_seq_no);
  2110. l_ptr->stats.sent_fragments += fragm_no;
  2111. l_ptr->stats.sent_fragmented++;
  2112. tipc_link_push_queue(l_ptr);
  2113. return dsz;
  2114. }
  2115. /*
  2116. * A pending message being re-assembled must store certain values
  2117. * to handle subsequent fragments correctly. The following functions
  2118. * help storing these values in unused, available fields in the
  2119. * pending message. This makes dynamic memory allocation unnecessary.
  2120. */
  2121. static void set_long_msg_seqno(struct sk_buff *buf, u32 seqno)
  2122. {
  2123. msg_set_seqno(buf_msg(buf), seqno);
  2124. }
  2125. static u32 get_fragm_size(struct sk_buff *buf)
  2126. {
  2127. return msg_ack(buf_msg(buf));
  2128. }
  2129. static void set_fragm_size(struct sk_buff *buf, u32 sz)
  2130. {
  2131. msg_set_ack(buf_msg(buf), sz);
  2132. }
  2133. static u32 get_expected_frags(struct sk_buff *buf)
  2134. {
  2135. return msg_bcast_ack(buf_msg(buf));
  2136. }
  2137. static void set_expected_frags(struct sk_buff *buf, u32 exp)
  2138. {
  2139. msg_set_bcast_ack(buf_msg(buf), exp);
  2140. }
  2141. static u32 get_timer_cnt(struct sk_buff *buf)
  2142. {
  2143. return msg_reroute_cnt(buf_msg(buf));
  2144. }
  2145. static void incr_timer_cnt(struct sk_buff *buf)
  2146. {
  2147. msg_incr_reroute_cnt(buf_msg(buf));
  2148. }
  2149. /*
  2150. * tipc_link_recv_fragment(): Called with node lock on. Returns
  2151. * the reassembled buffer if message is complete.
  2152. */
  2153. int tipc_link_recv_fragment(struct sk_buff **pending, struct sk_buff **fb,
  2154. struct tipc_msg **m)
  2155. {
  2156. struct sk_buff *prev = NULL;
  2157. struct sk_buff *fbuf = *fb;
  2158. struct tipc_msg *fragm = buf_msg(fbuf);
  2159. struct sk_buff *pbuf = *pending;
  2160. u32 long_msg_seq_no = msg_long_msgno(fragm);
  2161. *fb = NULL;
  2162. /* Is there an incomplete message waiting for this fragment? */
  2163. while (pbuf && ((msg_seqno(buf_msg(pbuf)) != long_msg_seq_no) ||
  2164. (msg_orignode(fragm) != msg_orignode(buf_msg(pbuf))))) {
  2165. prev = pbuf;
  2166. pbuf = pbuf->next;
  2167. }
  2168. if (!pbuf && (msg_type(fragm) == FIRST_FRAGMENT)) {
  2169. struct tipc_msg *imsg = (struct tipc_msg *)msg_data(fragm);
  2170. u32 msg_sz = msg_size(imsg);
  2171. u32 fragm_sz = msg_data_sz(fragm);
  2172. u32 exp_fragm_cnt = msg_sz/fragm_sz + !!(msg_sz % fragm_sz);
  2173. u32 max = TIPC_MAX_USER_MSG_SIZE + NAMED_H_SIZE;
  2174. if (msg_type(imsg) == TIPC_MCAST_MSG)
  2175. max = TIPC_MAX_USER_MSG_SIZE + MCAST_H_SIZE;
  2176. if (msg_size(imsg) > max) {
  2177. buf_discard(fbuf);
  2178. return 0;
  2179. }
  2180. pbuf = tipc_buf_acquire(msg_size(imsg));
  2181. if (pbuf != NULL) {
  2182. pbuf->next = *pending;
  2183. *pending = pbuf;
  2184. skb_copy_to_linear_data(pbuf, imsg,
  2185. msg_data_sz(fragm));
  2186. /* Prepare buffer for subsequent fragments. */
  2187. set_long_msg_seqno(pbuf, long_msg_seq_no);
  2188. set_fragm_size(pbuf, fragm_sz);
  2189. set_expected_frags(pbuf, exp_fragm_cnt - 1);
  2190. } else {
  2191. warn("Link unable to reassemble fragmented message\n");
  2192. }
  2193. buf_discard(fbuf);
  2194. return 0;
  2195. } else if (pbuf && (msg_type(fragm) != FIRST_FRAGMENT)) {
  2196. u32 dsz = msg_data_sz(fragm);
  2197. u32 fsz = get_fragm_size(pbuf);
  2198. u32 crs = ((msg_fragm_no(fragm) - 1) * fsz);
  2199. u32 exp_frags = get_expected_frags(pbuf) - 1;
  2200. skb_copy_to_linear_data_offset(pbuf, crs,
  2201. msg_data(fragm), dsz);
  2202. buf_discard(fbuf);
  2203. /* Is message complete? */
  2204. if (exp_frags == 0) {
  2205. if (prev)
  2206. prev->next = pbuf->next;
  2207. else
  2208. *pending = pbuf->next;
  2209. msg_reset_reroute_cnt(buf_msg(pbuf));
  2210. *fb = pbuf;
  2211. *m = buf_msg(pbuf);
  2212. return 1;
  2213. }
  2214. set_expected_frags(pbuf, exp_frags);
  2215. return 0;
  2216. }
  2217. buf_discard(fbuf);
  2218. return 0;
  2219. }
  2220. /**
  2221. * link_check_defragm_bufs - flush stale incoming message fragments
  2222. * @l_ptr: pointer to link
  2223. */
  2224. static void link_check_defragm_bufs(struct link *l_ptr)
  2225. {
  2226. struct sk_buff *prev = NULL;
  2227. struct sk_buff *next = NULL;
  2228. struct sk_buff *buf = l_ptr->defragm_buf;
  2229. if (!buf)
  2230. return;
  2231. if (!link_working_working(l_ptr))
  2232. return;
  2233. while (buf) {
  2234. u32 cnt = get_timer_cnt(buf);
  2235. next = buf->next;
  2236. if (cnt < 4) {
  2237. incr_timer_cnt(buf);
  2238. prev = buf;
  2239. } else {
  2240. if (prev)
  2241. prev->next = buf->next;
  2242. else
  2243. l_ptr->defragm_buf = buf->next;
  2244. buf_discard(buf);
  2245. }
  2246. buf = next;
  2247. }
  2248. }
  2249. static void link_set_supervision_props(struct link *l_ptr, u32 tolerance)
  2250. {
  2251. if ((tolerance < TIPC_MIN_LINK_TOL) || (tolerance > TIPC_MAX_LINK_TOL))
  2252. return;
  2253. l_ptr->tolerance = tolerance;
  2254. l_ptr->continuity_interval =
  2255. ((tolerance / 4) > 500) ? 500 : tolerance / 4;
  2256. l_ptr->abort_limit = tolerance / (l_ptr->continuity_interval / 4);
  2257. }
  2258. void tipc_link_set_queue_limits(struct link *l_ptr, u32 window)
  2259. {
  2260. /* Data messages from this node, inclusive FIRST_FRAGM */
  2261. l_ptr->queue_limit[TIPC_LOW_IMPORTANCE] = window;
  2262. l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE] = (window / 3) * 4;
  2263. l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE] = (window / 3) * 5;
  2264. l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE] = (window / 3) * 6;
  2265. /* Transiting data messages,inclusive FIRST_FRAGM */
  2266. l_ptr->queue_limit[TIPC_LOW_IMPORTANCE + 4] = 300;
  2267. l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE + 4] = 600;
  2268. l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE + 4] = 900;
  2269. l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE + 4] = 1200;
  2270. l_ptr->queue_limit[CONN_MANAGER] = 1200;
  2271. l_ptr->queue_limit[CHANGEOVER_PROTOCOL] = 2500;
  2272. l_ptr->queue_limit[NAME_DISTRIBUTOR] = 3000;
  2273. /* FRAGMENT and LAST_FRAGMENT packets */
  2274. l_ptr->queue_limit[MSG_FRAGMENTER] = 4000;
  2275. }
  2276. /**
  2277. * link_find_link - locate link by name
  2278. * @name - ptr to link name string
  2279. * @node - ptr to area to be filled with ptr to associated node
  2280. *
  2281. * Caller must hold 'tipc_net_lock' to ensure node and bearer are not deleted;
  2282. * this also prevents link deletion.
  2283. *
  2284. * Returns pointer to link (or 0 if invalid link name).
  2285. */
  2286. static struct link *link_find_link(const char *name, struct tipc_node **node)
  2287. {
  2288. struct link_name link_name_parts;
  2289. struct tipc_bearer *b_ptr;
  2290. struct link *l_ptr;
  2291. if (!link_name_validate(name, &link_name_parts))
  2292. return NULL;
  2293. b_ptr = tipc_bearer_find_interface(link_name_parts.if_local);
  2294. if (!b_ptr)
  2295. return NULL;
  2296. *node = tipc_node_find(link_name_parts.addr_peer);
  2297. if (!*node)
  2298. return NULL;
  2299. l_ptr = (*node)->links[b_ptr->identity];
  2300. if (!l_ptr || strcmp(l_ptr->name, name))
  2301. return NULL;
  2302. return l_ptr;
  2303. }
  2304. struct sk_buff *tipc_link_cmd_config(const void *req_tlv_area, int req_tlv_space,
  2305. u16 cmd)
  2306. {
  2307. struct tipc_link_config *args;
  2308. u32 new_value;
  2309. struct link *l_ptr;
  2310. struct tipc_node *node;
  2311. int res;
  2312. if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_CONFIG))
  2313. return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
  2314. args = (struct tipc_link_config *)TLV_DATA(req_tlv_area);
  2315. new_value = ntohl(args->value);
  2316. if (!strcmp(args->name, tipc_bclink_name)) {
  2317. if ((cmd == TIPC_CMD_SET_LINK_WINDOW) &&
  2318. (tipc_bclink_set_queue_limits(new_value) == 0))
  2319. return tipc_cfg_reply_none();
  2320. return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
  2321. " (cannot change setting on broadcast link)");
  2322. }
  2323. read_lock_bh(&tipc_net_lock);
  2324. l_ptr = link_find_link(args->name, &node);
  2325. if (!l_ptr) {
  2326. read_unlock_bh(&tipc_net_lock);
  2327. return tipc_cfg_reply_error_string("link not found");
  2328. }
  2329. tipc_node_lock(node);
  2330. res = -EINVAL;
  2331. switch (cmd) {
  2332. case TIPC_CMD_SET_LINK_TOL:
  2333. if ((new_value >= TIPC_MIN_LINK_TOL) &&
  2334. (new_value <= TIPC_MAX_LINK_TOL)) {
  2335. link_set_supervision_props(l_ptr, new_value);
  2336. tipc_link_send_proto_msg(l_ptr, STATE_MSG,
  2337. 0, 0, new_value, 0, 0);
  2338. res = 0;
  2339. }
  2340. break;
  2341. case TIPC_CMD_SET_LINK_PRI:
  2342. if ((new_value >= TIPC_MIN_LINK_PRI) &&
  2343. (new_value <= TIPC_MAX_LINK_PRI)) {
  2344. l_ptr->priority = new_value;
  2345. tipc_link_send_proto_msg(l_ptr, STATE_MSG,
  2346. 0, 0, 0, new_value, 0);
  2347. res = 0;
  2348. }
  2349. break;
  2350. case TIPC_CMD_SET_LINK_WINDOW:
  2351. if ((new_value >= TIPC_MIN_LINK_WIN) &&
  2352. (new_value <= TIPC_MAX_LINK_WIN)) {
  2353. tipc_link_set_queue_limits(l_ptr, new_value);
  2354. res = 0;
  2355. }
  2356. break;
  2357. }
  2358. tipc_node_unlock(node);
  2359. read_unlock_bh(&tipc_net_lock);
  2360. if (res)
  2361. return tipc_cfg_reply_error_string("cannot change link setting");
  2362. return tipc_cfg_reply_none();
  2363. }
  2364. /**
  2365. * link_reset_statistics - reset link statistics
  2366. * @l_ptr: pointer to link
  2367. */
  2368. static void link_reset_statistics(struct link *l_ptr)
  2369. {
  2370. memset(&l_ptr->stats, 0, sizeof(l_ptr->stats));
  2371. l_ptr->stats.sent_info = l_ptr->next_out_no;
  2372. l_ptr->stats.recv_info = l_ptr->next_in_no;
  2373. }
  2374. struct sk_buff *tipc_link_cmd_reset_stats(const void *req_tlv_area, int req_tlv_space)
  2375. {
  2376. char *link_name;
  2377. struct link *l_ptr;
  2378. struct tipc_node *node;
  2379. if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
  2380. return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
  2381. link_name = (char *)TLV_DATA(req_tlv_area);
  2382. if (!strcmp(link_name, tipc_bclink_name)) {
  2383. if (tipc_bclink_reset_stats())
  2384. return tipc_cfg_reply_error_string("link not found");
  2385. return tipc_cfg_reply_none();
  2386. }
  2387. read_lock_bh(&tipc_net_lock);
  2388. l_ptr = link_find_link(link_name, &node);
  2389. if (!l_ptr) {
  2390. read_unlock_bh(&tipc_net_lock);
  2391. return tipc_cfg_reply_error_string("link not found");
  2392. }
  2393. tipc_node_lock(node);
  2394. link_reset_statistics(l_ptr);
  2395. tipc_node_unlock(node);
  2396. read_unlock_bh(&tipc_net_lock);
  2397. return tipc_cfg_reply_none();
  2398. }
  2399. /**
  2400. * percent - convert count to a percentage of total (rounding up or down)
  2401. */
  2402. static u32 percent(u32 count, u32 total)
  2403. {
  2404. return (count * 100 + (total / 2)) / total;
  2405. }
  2406. /**
  2407. * tipc_link_stats - print link statistics
  2408. * @name: link name
  2409. * @buf: print buffer area
  2410. * @buf_size: size of print buffer area
  2411. *
  2412. * Returns length of print buffer data string (or 0 if error)
  2413. */
  2414. static int tipc_link_stats(const char *name, char *buf, const u32 buf_size)
  2415. {
  2416. struct print_buf pb;
  2417. struct link *l_ptr;
  2418. struct tipc_node *node;
  2419. char *status;
  2420. u32 profile_total = 0;
  2421. if (!strcmp(name, tipc_bclink_name))
  2422. return tipc_bclink_stats(buf, buf_size);
  2423. tipc_printbuf_init(&pb, buf, buf_size);
  2424. read_lock_bh(&tipc_net_lock);
  2425. l_ptr = link_find_link(name, &node);
  2426. if (!l_ptr) {
  2427. read_unlock_bh(&tipc_net_lock);
  2428. return 0;
  2429. }
  2430. tipc_node_lock(node);
  2431. if (tipc_link_is_active(l_ptr))
  2432. status = "ACTIVE";
  2433. else if (tipc_link_is_up(l_ptr))
  2434. status = "STANDBY";
  2435. else
  2436. status = "DEFUNCT";
  2437. tipc_printf(&pb, "Link <%s>\n"
  2438. " %s MTU:%u Priority:%u Tolerance:%u ms"
  2439. " Window:%u packets\n",
  2440. l_ptr->name, status, l_ptr->max_pkt,
  2441. l_ptr->priority, l_ptr->tolerance, l_ptr->queue_limit[0]);
  2442. tipc_printf(&pb, " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
  2443. l_ptr->next_in_no - l_ptr->stats.recv_info,
  2444. l_ptr->stats.recv_fragments,
  2445. l_ptr->stats.recv_fragmented,
  2446. l_ptr->stats.recv_bundles,
  2447. l_ptr->stats.recv_bundled);
  2448. tipc_printf(&pb, " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
  2449. l_ptr->next_out_no - l_ptr->stats.sent_info,
  2450. l_ptr->stats.sent_fragments,
  2451. l_ptr->stats.sent_fragmented,
  2452. l_ptr->stats.sent_bundles,
  2453. l_ptr->stats.sent_bundled);
  2454. profile_total = l_ptr->stats.msg_length_counts;
  2455. if (!profile_total)
  2456. profile_total = 1;
  2457. tipc_printf(&pb, " TX profile sample:%u packets average:%u octets\n"
  2458. " 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% "
  2459. "-16384:%u%% -32768:%u%% -66000:%u%%\n",
  2460. l_ptr->stats.msg_length_counts,
  2461. l_ptr->stats.msg_lengths_total / profile_total,
  2462. percent(l_ptr->stats.msg_length_profile[0], profile_total),
  2463. percent(l_ptr->stats.msg_length_profile[1], profile_total),
  2464. percent(l_ptr->stats.msg_length_profile[2], profile_total),
  2465. percent(l_ptr->stats.msg_length_profile[3], profile_total),
  2466. percent(l_ptr->stats.msg_length_profile[4], profile_total),
  2467. percent(l_ptr->stats.msg_length_profile[5], profile_total),
  2468. percent(l_ptr->stats.msg_length_profile[6], profile_total));
  2469. tipc_printf(&pb, " RX states:%u probes:%u naks:%u defs:%u dups:%u\n",
  2470. l_ptr->stats.recv_states,
  2471. l_ptr->stats.recv_probes,
  2472. l_ptr->stats.recv_nacks,
  2473. l_ptr->stats.deferred_recv,
  2474. l_ptr->stats.duplicates);
  2475. tipc_printf(&pb, " TX states:%u probes:%u naks:%u acks:%u dups:%u\n",
  2476. l_ptr->stats.sent_states,
  2477. l_ptr->stats.sent_probes,
  2478. l_ptr->stats.sent_nacks,
  2479. l_ptr->stats.sent_acks,
  2480. l_ptr->stats.retransmitted);
  2481. tipc_printf(&pb, " Congestion bearer:%u link:%u Send queue max:%u avg:%u\n",
  2482. l_ptr->stats.bearer_congs,
  2483. l_ptr->stats.link_congs,
  2484. l_ptr->stats.max_queue_sz,
  2485. l_ptr->stats.queue_sz_counts
  2486. ? (l_ptr->stats.accu_queue_sz / l_ptr->stats.queue_sz_counts)
  2487. : 0);
  2488. tipc_node_unlock(node);
  2489. read_unlock_bh(&tipc_net_lock);
  2490. return tipc_printbuf_validate(&pb);
  2491. }
  2492. #define MAX_LINK_STATS_INFO 2000
  2493. struct sk_buff *tipc_link_cmd_show_stats(const void *req_tlv_area, int req_tlv_space)
  2494. {
  2495. struct sk_buff *buf;
  2496. struct tlv_desc *rep_tlv;
  2497. int str_len;
  2498. if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
  2499. return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
  2500. buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_LINK_STATS_INFO));
  2501. if (!buf)
  2502. return NULL;
  2503. rep_tlv = (struct tlv_desc *)buf->data;
  2504. str_len = tipc_link_stats((char *)TLV_DATA(req_tlv_area),
  2505. (char *)TLV_DATA(rep_tlv), MAX_LINK_STATS_INFO);
  2506. if (!str_len) {
  2507. buf_discard(buf);
  2508. return tipc_cfg_reply_error_string("link not found");
  2509. }
  2510. skb_put(buf, TLV_SPACE(str_len));
  2511. TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
  2512. return buf;
  2513. }
  2514. /**
  2515. * tipc_link_get_max_pkt - get maximum packet size to use when sending to destination
  2516. * @dest: network address of destination node
  2517. * @selector: used to select from set of active links
  2518. *
  2519. * If no active link can be found, uses default maximum packet size.
  2520. */
  2521. u32 tipc_link_get_max_pkt(u32 dest, u32 selector)
  2522. {
  2523. struct tipc_node *n_ptr;
  2524. struct link *l_ptr;
  2525. u32 res = MAX_PKT_DEFAULT;
  2526. if (dest == tipc_own_addr)
  2527. return MAX_MSG_SIZE;
  2528. read_lock_bh(&tipc_net_lock);
  2529. n_ptr = tipc_node_find(dest);
  2530. if (n_ptr) {
  2531. tipc_node_lock(n_ptr);
  2532. l_ptr = n_ptr->active_links[selector & 1];
  2533. if (l_ptr)
  2534. res = l_ptr->max_pkt;
  2535. tipc_node_unlock(n_ptr);
  2536. }
  2537. read_unlock_bh(&tipc_net_lock);
  2538. return res;
  2539. }
  2540. static void link_print(struct link *l_ptr, const char *str)
  2541. {
  2542. char print_area[256];
  2543. struct print_buf pb;
  2544. struct print_buf *buf = &pb;
  2545. tipc_printbuf_init(buf, print_area, sizeof(print_area));
  2546. tipc_printf(buf, str);
  2547. tipc_printf(buf, "Link %x<%s>:",
  2548. l_ptr->addr, l_ptr->b_ptr->name);
  2549. #ifdef CONFIG_TIPC_DEBUG
  2550. if (link_reset_reset(l_ptr) || link_reset_unknown(l_ptr))
  2551. goto print_state;
  2552. tipc_printf(buf, ": NXO(%u):", mod(l_ptr->next_out_no));
  2553. tipc_printf(buf, "NXI(%u):", mod(l_ptr->next_in_no));
  2554. tipc_printf(buf, "SQUE");
  2555. if (l_ptr->first_out) {
  2556. tipc_printf(buf, "[%u..", msg_seqno(buf_msg(l_ptr->first_out)));
  2557. if (l_ptr->next_out)
  2558. tipc_printf(buf, "%u..",
  2559. msg_seqno(buf_msg(l_ptr->next_out)));
  2560. tipc_printf(buf, "%u]", msg_seqno(buf_msg(l_ptr->last_out)));
  2561. if ((mod(msg_seqno(buf_msg(l_ptr->last_out)) -
  2562. msg_seqno(buf_msg(l_ptr->first_out)))
  2563. != (l_ptr->out_queue_size - 1)) ||
  2564. (l_ptr->last_out->next != NULL)) {
  2565. tipc_printf(buf, "\nSend queue inconsistency\n");
  2566. tipc_printf(buf, "first_out= %p ", l_ptr->first_out);
  2567. tipc_printf(buf, "next_out= %p ", l_ptr->next_out);
  2568. tipc_printf(buf, "last_out= %p ", l_ptr->last_out);
  2569. }
  2570. } else
  2571. tipc_printf(buf, "[]");
  2572. tipc_printf(buf, "SQSIZ(%u)", l_ptr->out_queue_size);
  2573. if (l_ptr->oldest_deferred_in) {
  2574. u32 o = msg_seqno(buf_msg(l_ptr->oldest_deferred_in));
  2575. u32 n = msg_seqno(buf_msg(l_ptr->newest_deferred_in));
  2576. tipc_printf(buf, ":RQUE[%u..%u]", o, n);
  2577. if (l_ptr->deferred_inqueue_sz != mod((n + 1) - o)) {
  2578. tipc_printf(buf, ":RQSIZ(%u)",
  2579. l_ptr->deferred_inqueue_sz);
  2580. }
  2581. }
  2582. print_state:
  2583. #endif
  2584. if (link_working_unknown(l_ptr))
  2585. tipc_printf(buf, ":WU");
  2586. else if (link_reset_reset(l_ptr))
  2587. tipc_printf(buf, ":RR");
  2588. else if (link_reset_unknown(l_ptr))
  2589. tipc_printf(buf, ":RU");
  2590. else if (link_working_working(l_ptr))
  2591. tipc_printf(buf, ":WW");
  2592. tipc_printf(buf, "\n");
  2593. tipc_printbuf_validate(buf);
  2594. info("%s", print_area);
  2595. }