link.c 81 KB

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