link.c 80 KB

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