link.c 78 KB

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