link.c 78 KB

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