link.c 78 KB

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