link.c 78 KB

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