link.c 81 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159
  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 msg_seqno(buf_msg(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 = msg_seqno(buf_msg(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 = msg_seqno(buf_msg(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 = msg_seqno(buf_msg(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 = msg_seqno(buf_msg(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 (less(n_ptr->bclink.acked, msg_bcast_ack(msg))) {
  1483. if (tipc_node_is_up(n_ptr) && n_ptr->bclink.supported)
  1484. tipc_bclink_acknowledge(n_ptr, msg_bcast_ack(msg));
  1485. }
  1486. crs = l_ptr->first_out;
  1487. while ((crs != l_ptr->next_out) &&
  1488. less_eq(msg_seqno(buf_msg(crs)), ackd)) {
  1489. struct sk_buff *next = crs->next;
  1490. buf_discard(crs);
  1491. crs = next;
  1492. released++;
  1493. }
  1494. if (released) {
  1495. l_ptr->first_out = crs;
  1496. l_ptr->out_queue_size -= released;
  1497. }
  1498. /* Try sending any messages link endpoint has pending */
  1499. if (unlikely(l_ptr->next_out))
  1500. tipc_link_push_queue(l_ptr);
  1501. if (unlikely(!list_empty(&l_ptr->waiting_ports)))
  1502. tipc_link_wakeup_ports(l_ptr, 0);
  1503. if (unlikely(++l_ptr->unacked_window >= TIPC_MIN_LINK_WIN)) {
  1504. l_ptr->stats.sent_acks++;
  1505. tipc_link_send_proto_msg(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
  1506. }
  1507. /* Now (finally!) process the incoming message */
  1508. protocol_check:
  1509. if (likely(link_working_working(l_ptr))) {
  1510. if (likely(seq_no == mod(l_ptr->next_in_no))) {
  1511. l_ptr->next_in_no++;
  1512. if (unlikely(l_ptr->oldest_deferred_in))
  1513. head = link_insert_deferred_queue(l_ptr,
  1514. head);
  1515. if (likely(msg_is_dest(msg, tipc_own_addr))) {
  1516. deliver:
  1517. if (likely(msg_isdata(msg))) {
  1518. tipc_node_unlock(n_ptr);
  1519. tipc_port_recv_msg(buf);
  1520. continue;
  1521. }
  1522. switch (msg_user(msg)) {
  1523. case MSG_BUNDLER:
  1524. l_ptr->stats.recv_bundles++;
  1525. l_ptr->stats.recv_bundled +=
  1526. msg_msgcnt(msg);
  1527. tipc_node_unlock(n_ptr);
  1528. tipc_link_recv_bundle(buf);
  1529. continue;
  1530. case NAME_DISTRIBUTOR:
  1531. tipc_node_unlock(n_ptr);
  1532. tipc_named_recv(buf);
  1533. continue;
  1534. case CONN_MANAGER:
  1535. tipc_node_unlock(n_ptr);
  1536. tipc_port_recv_proto_msg(buf);
  1537. continue;
  1538. case MSG_FRAGMENTER:
  1539. l_ptr->stats.recv_fragments++;
  1540. if (tipc_link_recv_fragment(&l_ptr->defragm_buf,
  1541. &buf, &msg)) {
  1542. l_ptr->stats.recv_fragmented++;
  1543. goto deliver;
  1544. }
  1545. break;
  1546. case CHANGEOVER_PROTOCOL:
  1547. type = msg_type(msg);
  1548. if (link_recv_changeover_msg(&l_ptr, &buf)) {
  1549. msg = buf_msg(buf);
  1550. seq_no = msg_seqno(msg);
  1551. if (type == ORIGINAL_MSG)
  1552. goto deliver;
  1553. goto protocol_check;
  1554. }
  1555. break;
  1556. default:
  1557. buf_discard(buf);
  1558. buf = NULL;
  1559. break;
  1560. }
  1561. }
  1562. tipc_node_unlock(n_ptr);
  1563. tipc_net_route_msg(buf);
  1564. continue;
  1565. }
  1566. link_handle_out_of_seq_msg(l_ptr, buf);
  1567. head = link_insert_deferred_queue(l_ptr, head);
  1568. tipc_node_unlock(n_ptr);
  1569. continue;
  1570. }
  1571. if (msg_user(msg) == LINK_PROTOCOL) {
  1572. link_recv_proto_msg(l_ptr, buf);
  1573. head = link_insert_deferred_queue(l_ptr, head);
  1574. tipc_node_unlock(n_ptr);
  1575. continue;
  1576. }
  1577. link_state_event(l_ptr, TRAFFIC_MSG_EVT);
  1578. if (link_working_working(l_ptr)) {
  1579. /* Re-insert in front of queue */
  1580. buf->next = head;
  1581. head = buf;
  1582. tipc_node_unlock(n_ptr);
  1583. continue;
  1584. }
  1585. tipc_node_unlock(n_ptr);
  1586. cont:
  1587. buf_discard(buf);
  1588. }
  1589. read_unlock_bh(&tipc_net_lock);
  1590. }
  1591. /*
  1592. * link_defer_buf(): Sort a received out-of-sequence packet
  1593. * into the deferred reception queue.
  1594. * Returns the increase of the queue length,i.e. 0 or 1
  1595. */
  1596. u32 tipc_link_defer_pkt(struct sk_buff **head,
  1597. struct sk_buff **tail,
  1598. struct sk_buff *buf)
  1599. {
  1600. struct sk_buff *prev = NULL;
  1601. struct sk_buff *crs = *head;
  1602. u32 seq_no = msg_seqno(buf_msg(buf));
  1603. buf->next = NULL;
  1604. /* Empty queue ? */
  1605. if (*head == NULL) {
  1606. *head = *tail = buf;
  1607. return 1;
  1608. }
  1609. /* Last ? */
  1610. if (less(msg_seqno(buf_msg(*tail)), seq_no)) {
  1611. (*tail)->next = buf;
  1612. *tail = buf;
  1613. return 1;
  1614. }
  1615. /* Scan through queue and sort it in */
  1616. do {
  1617. struct tipc_msg *msg = buf_msg(crs);
  1618. if (less(seq_no, msg_seqno(msg))) {
  1619. buf->next = crs;
  1620. if (prev)
  1621. prev->next = buf;
  1622. else
  1623. *head = buf;
  1624. return 1;
  1625. }
  1626. if (seq_no == msg_seqno(msg))
  1627. break;
  1628. prev = crs;
  1629. crs = crs->next;
  1630. } while (crs);
  1631. /* Message is a duplicate of an existing message */
  1632. buf_discard(buf);
  1633. return 0;
  1634. }
  1635. /**
  1636. * link_handle_out_of_seq_msg - handle arrival of out-of-sequence packet
  1637. */
  1638. static void link_handle_out_of_seq_msg(struct link *l_ptr,
  1639. struct sk_buff *buf)
  1640. {
  1641. u32 seq_no = msg_seqno(buf_msg(buf));
  1642. if (likely(msg_user(buf_msg(buf)) == LINK_PROTOCOL)) {
  1643. link_recv_proto_msg(l_ptr, buf);
  1644. return;
  1645. }
  1646. /* Record OOS packet arrival (force mismatch on next timeout) */
  1647. l_ptr->checkpoint--;
  1648. /*
  1649. * Discard packet if a duplicate; otherwise add it to deferred queue
  1650. * and notify peer of gap as per protocol specification
  1651. */
  1652. if (less(seq_no, mod(l_ptr->next_in_no))) {
  1653. l_ptr->stats.duplicates++;
  1654. buf_discard(buf);
  1655. return;
  1656. }
  1657. if (tipc_link_defer_pkt(&l_ptr->oldest_deferred_in,
  1658. &l_ptr->newest_deferred_in, buf)) {
  1659. l_ptr->deferred_inqueue_sz++;
  1660. l_ptr->stats.deferred_recv++;
  1661. if ((l_ptr->deferred_inqueue_sz % 16) == 1)
  1662. tipc_link_send_proto_msg(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
  1663. } else
  1664. l_ptr->stats.duplicates++;
  1665. }
  1666. /*
  1667. * Send protocol message to the other endpoint.
  1668. */
  1669. void tipc_link_send_proto_msg(struct link *l_ptr, u32 msg_typ, int probe_msg,
  1670. u32 gap, u32 tolerance, u32 priority, u32 ack_mtu)
  1671. {
  1672. struct sk_buff *buf = NULL;
  1673. struct tipc_msg *msg = l_ptr->pmsg;
  1674. u32 msg_size = sizeof(l_ptr->proto_msg);
  1675. int r_flag;
  1676. if (link_blocked(l_ptr))
  1677. return;
  1678. /* Abort non-RESET send if communication with node is prohibited */
  1679. if ((l_ptr->owner->block_setup) && (msg_typ != RESET_MSG))
  1680. return;
  1681. msg_set_type(msg, msg_typ);
  1682. msg_set_net_plane(msg, l_ptr->b_ptr->net_plane);
  1683. msg_set_bcast_ack(msg, mod(l_ptr->owner->bclink.last_in));
  1684. msg_set_last_bcast(msg, tipc_bclink_get_last_sent());
  1685. if (msg_typ == STATE_MSG) {
  1686. u32 next_sent = mod(l_ptr->next_out_no);
  1687. if (!tipc_link_is_up(l_ptr))
  1688. return;
  1689. if (l_ptr->next_out)
  1690. next_sent = msg_seqno(buf_msg(l_ptr->next_out));
  1691. msg_set_next_sent(msg, next_sent);
  1692. if (l_ptr->oldest_deferred_in) {
  1693. u32 rec = msg_seqno(buf_msg(l_ptr->oldest_deferred_in));
  1694. gap = mod(rec - mod(l_ptr->next_in_no));
  1695. }
  1696. msg_set_seq_gap(msg, gap);
  1697. if (gap)
  1698. l_ptr->stats.sent_nacks++;
  1699. msg_set_link_tolerance(msg, tolerance);
  1700. msg_set_linkprio(msg, priority);
  1701. msg_set_max_pkt(msg, ack_mtu);
  1702. msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
  1703. msg_set_probe(msg, probe_msg != 0);
  1704. if (probe_msg) {
  1705. u32 mtu = l_ptr->max_pkt;
  1706. if ((mtu < l_ptr->max_pkt_target) &&
  1707. link_working_working(l_ptr) &&
  1708. l_ptr->fsm_msg_cnt) {
  1709. msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
  1710. if (l_ptr->max_pkt_probes == 10) {
  1711. l_ptr->max_pkt_target = (msg_size - 4);
  1712. l_ptr->max_pkt_probes = 0;
  1713. msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
  1714. }
  1715. l_ptr->max_pkt_probes++;
  1716. }
  1717. l_ptr->stats.sent_probes++;
  1718. }
  1719. l_ptr->stats.sent_states++;
  1720. } else { /* RESET_MSG or ACTIVATE_MSG */
  1721. msg_set_ack(msg, mod(l_ptr->reset_checkpoint - 1));
  1722. msg_set_seq_gap(msg, 0);
  1723. msg_set_next_sent(msg, 1);
  1724. msg_set_probe(msg, 0);
  1725. msg_set_link_tolerance(msg, l_ptr->tolerance);
  1726. msg_set_linkprio(msg, l_ptr->priority);
  1727. msg_set_max_pkt(msg, l_ptr->max_pkt_target);
  1728. }
  1729. r_flag = (l_ptr->owner->working_links > tipc_link_is_up(l_ptr));
  1730. msg_set_redundant_link(msg, r_flag);
  1731. msg_set_linkprio(msg, l_ptr->priority);
  1732. /* Ensure sequence number will not fit : */
  1733. msg_set_seqno(msg, mod(l_ptr->next_out_no + (0xffff/2)));
  1734. /* Congestion? */
  1735. if (tipc_bearer_congested(l_ptr->b_ptr, l_ptr)) {
  1736. if (!l_ptr->proto_msg_queue) {
  1737. l_ptr->proto_msg_queue =
  1738. tipc_buf_acquire(sizeof(l_ptr->proto_msg));
  1739. }
  1740. buf = l_ptr->proto_msg_queue;
  1741. if (!buf)
  1742. return;
  1743. skb_copy_to_linear_data(buf, msg, sizeof(l_ptr->proto_msg));
  1744. return;
  1745. }
  1746. /* Message can be sent */
  1747. buf = tipc_buf_acquire(msg_size);
  1748. if (!buf)
  1749. return;
  1750. skb_copy_to_linear_data(buf, msg, sizeof(l_ptr->proto_msg));
  1751. msg_set_size(buf_msg(buf), msg_size);
  1752. if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
  1753. l_ptr->unacked_window = 0;
  1754. buf_discard(buf);
  1755. return;
  1756. }
  1757. /* New congestion */
  1758. tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
  1759. l_ptr->proto_msg_queue = buf;
  1760. l_ptr->stats.bearer_congs++;
  1761. }
  1762. /*
  1763. * Receive protocol message :
  1764. * Note that network plane id propagates through the network, and may
  1765. * change at any time. The node with lowest address rules
  1766. */
  1767. static void link_recv_proto_msg(struct link *l_ptr, struct sk_buff *buf)
  1768. {
  1769. u32 rec_gap = 0;
  1770. u32 max_pkt_info;
  1771. u32 max_pkt_ack;
  1772. u32 msg_tol;
  1773. struct tipc_msg *msg = buf_msg(buf);
  1774. if (link_blocked(l_ptr))
  1775. goto exit;
  1776. /* record unnumbered packet arrival (force mismatch on next timeout) */
  1777. l_ptr->checkpoint--;
  1778. if (l_ptr->b_ptr->net_plane != msg_net_plane(msg))
  1779. if (tipc_own_addr > msg_prevnode(msg))
  1780. l_ptr->b_ptr->net_plane = msg_net_plane(msg);
  1781. l_ptr->owner->permit_changeover = msg_redundant_link(msg);
  1782. switch (msg_type(msg)) {
  1783. case RESET_MSG:
  1784. if (!link_working_unknown(l_ptr) &&
  1785. (l_ptr->peer_session != INVALID_SESSION)) {
  1786. if (less_eq(msg_session(msg), l_ptr->peer_session))
  1787. break; /* duplicate or old reset: ignore */
  1788. }
  1789. if (!msg_redundant_link(msg) && (link_working_working(l_ptr) ||
  1790. link_working_unknown(l_ptr))) {
  1791. /*
  1792. * peer has lost contact -- don't allow peer's links
  1793. * to reactivate before we recognize loss & clean up
  1794. */
  1795. l_ptr->owner->block_setup = WAIT_NODE_DOWN;
  1796. }
  1797. /* fall thru' */
  1798. case ACTIVATE_MSG:
  1799. /* Update link settings according other endpoint's values */
  1800. strcpy((strrchr(l_ptr->name, ':') + 1), (char *)msg_data(msg));
  1801. msg_tol = msg_link_tolerance(msg);
  1802. if (msg_tol > l_ptr->tolerance)
  1803. link_set_supervision_props(l_ptr, msg_tol);
  1804. if (msg_linkprio(msg) > l_ptr->priority)
  1805. l_ptr->priority = msg_linkprio(msg);
  1806. max_pkt_info = msg_max_pkt(msg);
  1807. if (max_pkt_info) {
  1808. if (max_pkt_info < l_ptr->max_pkt_target)
  1809. l_ptr->max_pkt_target = max_pkt_info;
  1810. if (l_ptr->max_pkt > l_ptr->max_pkt_target)
  1811. l_ptr->max_pkt = l_ptr->max_pkt_target;
  1812. } else {
  1813. l_ptr->max_pkt = l_ptr->max_pkt_target;
  1814. }
  1815. l_ptr->owner->bclink.supported = (max_pkt_info != 0);
  1816. link_state_event(l_ptr, msg_type(msg));
  1817. l_ptr->peer_session = msg_session(msg);
  1818. l_ptr->peer_bearer_id = msg_bearer_id(msg);
  1819. /* Synchronize broadcast sequence numbers */
  1820. if (!tipc_node_redundant_links(l_ptr->owner))
  1821. l_ptr->owner->bclink.last_in = mod(msg_last_bcast(msg));
  1822. break;
  1823. case STATE_MSG:
  1824. msg_tol = msg_link_tolerance(msg);
  1825. if (msg_tol)
  1826. link_set_supervision_props(l_ptr, msg_tol);
  1827. if (msg_linkprio(msg) &&
  1828. (msg_linkprio(msg) != l_ptr->priority)) {
  1829. warn("Resetting link <%s>, priority change %u->%u\n",
  1830. l_ptr->name, l_ptr->priority, msg_linkprio(msg));
  1831. l_ptr->priority = msg_linkprio(msg);
  1832. tipc_link_reset(l_ptr); /* Enforce change to take effect */
  1833. break;
  1834. }
  1835. link_state_event(l_ptr, TRAFFIC_MSG_EVT);
  1836. l_ptr->stats.recv_states++;
  1837. if (link_reset_unknown(l_ptr))
  1838. break;
  1839. if (less_eq(mod(l_ptr->next_in_no), msg_next_sent(msg))) {
  1840. rec_gap = mod(msg_next_sent(msg) -
  1841. mod(l_ptr->next_in_no));
  1842. }
  1843. max_pkt_ack = msg_max_pkt(msg);
  1844. if (max_pkt_ack > l_ptr->max_pkt) {
  1845. l_ptr->max_pkt = max_pkt_ack;
  1846. l_ptr->max_pkt_probes = 0;
  1847. }
  1848. max_pkt_ack = 0;
  1849. if (msg_probe(msg)) {
  1850. l_ptr->stats.recv_probes++;
  1851. if (msg_size(msg) > sizeof(l_ptr->proto_msg))
  1852. max_pkt_ack = msg_size(msg);
  1853. }
  1854. /* Protocol message before retransmits, reduce loss risk */
  1855. tipc_bclink_check_gap(l_ptr->owner, msg_last_bcast(msg));
  1856. if (rec_gap || (msg_probe(msg))) {
  1857. tipc_link_send_proto_msg(l_ptr, STATE_MSG,
  1858. 0, rec_gap, 0, 0, max_pkt_ack);
  1859. }
  1860. if (msg_seq_gap(msg)) {
  1861. l_ptr->stats.recv_nacks++;
  1862. tipc_link_retransmit(l_ptr, l_ptr->first_out,
  1863. msg_seq_gap(msg));
  1864. }
  1865. break;
  1866. }
  1867. exit:
  1868. buf_discard(buf);
  1869. }
  1870. /*
  1871. * tipc_link_tunnel(): Send one message via a link belonging to
  1872. * another bearer. Owner node is locked.
  1873. */
  1874. static void tipc_link_tunnel(struct link *l_ptr,
  1875. struct tipc_msg *tunnel_hdr,
  1876. struct tipc_msg *msg,
  1877. u32 selector)
  1878. {
  1879. struct link *tunnel;
  1880. struct sk_buff *buf;
  1881. u32 length = msg_size(msg);
  1882. tunnel = l_ptr->owner->active_links[selector & 1];
  1883. if (!tipc_link_is_up(tunnel)) {
  1884. warn("Link changeover error, "
  1885. "tunnel link no longer available\n");
  1886. return;
  1887. }
  1888. msg_set_size(tunnel_hdr, length + INT_H_SIZE);
  1889. buf = tipc_buf_acquire(length + INT_H_SIZE);
  1890. if (!buf) {
  1891. warn("Link changeover error, "
  1892. "unable to send tunnel msg\n");
  1893. return;
  1894. }
  1895. skb_copy_to_linear_data(buf, tunnel_hdr, INT_H_SIZE);
  1896. skb_copy_to_linear_data_offset(buf, INT_H_SIZE, msg, length);
  1897. tipc_link_send_buf(tunnel, buf);
  1898. }
  1899. /*
  1900. * changeover(): Send whole message queue via the remaining link
  1901. * Owner node is locked.
  1902. */
  1903. void tipc_link_changeover(struct link *l_ptr)
  1904. {
  1905. u32 msgcount = l_ptr->out_queue_size;
  1906. struct sk_buff *crs = l_ptr->first_out;
  1907. struct link *tunnel = l_ptr->owner->active_links[0];
  1908. struct tipc_msg tunnel_hdr;
  1909. int split_bundles;
  1910. if (!tunnel)
  1911. return;
  1912. if (!l_ptr->owner->permit_changeover) {
  1913. warn("Link changeover error, "
  1914. "peer did not permit changeover\n");
  1915. return;
  1916. }
  1917. tipc_msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
  1918. ORIGINAL_MSG, INT_H_SIZE, l_ptr->addr);
  1919. msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
  1920. msg_set_msgcnt(&tunnel_hdr, msgcount);
  1921. if (!l_ptr->first_out) {
  1922. struct sk_buff *buf;
  1923. buf = tipc_buf_acquire(INT_H_SIZE);
  1924. if (buf) {
  1925. skb_copy_to_linear_data(buf, &tunnel_hdr, INT_H_SIZE);
  1926. msg_set_size(&tunnel_hdr, INT_H_SIZE);
  1927. tipc_link_send_buf(tunnel, buf);
  1928. } else {
  1929. warn("Link changeover error, "
  1930. "unable to send changeover msg\n");
  1931. }
  1932. return;
  1933. }
  1934. split_bundles = (l_ptr->owner->active_links[0] !=
  1935. l_ptr->owner->active_links[1]);
  1936. while (crs) {
  1937. struct tipc_msg *msg = buf_msg(crs);
  1938. if ((msg_user(msg) == MSG_BUNDLER) && split_bundles) {
  1939. struct tipc_msg *m = msg_get_wrapped(msg);
  1940. unchar *pos = (unchar *)m;
  1941. msgcount = msg_msgcnt(msg);
  1942. while (msgcount--) {
  1943. msg_set_seqno(m, msg_seqno(msg));
  1944. tipc_link_tunnel(l_ptr, &tunnel_hdr, m,
  1945. msg_link_selector(m));
  1946. pos += align(msg_size(m));
  1947. m = (struct tipc_msg *)pos;
  1948. }
  1949. } else {
  1950. tipc_link_tunnel(l_ptr, &tunnel_hdr, msg,
  1951. msg_link_selector(msg));
  1952. }
  1953. crs = crs->next;
  1954. }
  1955. }
  1956. void tipc_link_send_duplicate(struct link *l_ptr, struct link *tunnel)
  1957. {
  1958. struct sk_buff *iter;
  1959. struct tipc_msg tunnel_hdr;
  1960. tipc_msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
  1961. DUPLICATE_MSG, INT_H_SIZE, l_ptr->addr);
  1962. msg_set_msgcnt(&tunnel_hdr, l_ptr->out_queue_size);
  1963. msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
  1964. iter = l_ptr->first_out;
  1965. while (iter) {
  1966. struct sk_buff *outbuf;
  1967. struct tipc_msg *msg = buf_msg(iter);
  1968. u32 length = msg_size(msg);
  1969. if (msg_user(msg) == MSG_BUNDLER)
  1970. msg_set_type(msg, CLOSED_MSG);
  1971. msg_set_ack(msg, mod(l_ptr->next_in_no - 1)); /* Update */
  1972. msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
  1973. msg_set_size(&tunnel_hdr, length + INT_H_SIZE);
  1974. outbuf = tipc_buf_acquire(length + INT_H_SIZE);
  1975. if (outbuf == NULL) {
  1976. warn("Link changeover error, "
  1977. "unable to send duplicate msg\n");
  1978. return;
  1979. }
  1980. skb_copy_to_linear_data(outbuf, &tunnel_hdr, INT_H_SIZE);
  1981. skb_copy_to_linear_data_offset(outbuf, INT_H_SIZE, iter->data,
  1982. length);
  1983. tipc_link_send_buf(tunnel, outbuf);
  1984. if (!tipc_link_is_up(l_ptr))
  1985. return;
  1986. iter = iter->next;
  1987. }
  1988. }
  1989. /**
  1990. * buf_extract - extracts embedded TIPC message from another message
  1991. * @skb: encapsulating message buffer
  1992. * @from_pos: offset to extract from
  1993. *
  1994. * Returns a new message buffer containing an embedded message. The
  1995. * encapsulating message itself is left unchanged.
  1996. */
  1997. static struct sk_buff *buf_extract(struct sk_buff *skb, u32 from_pos)
  1998. {
  1999. struct tipc_msg *msg = (struct tipc_msg *)(skb->data + from_pos);
  2000. u32 size = msg_size(msg);
  2001. struct sk_buff *eb;
  2002. eb = tipc_buf_acquire(size);
  2003. if (eb)
  2004. skb_copy_to_linear_data(eb, msg, size);
  2005. return eb;
  2006. }
  2007. /*
  2008. * link_recv_changeover_msg(): Receive tunneled packet sent
  2009. * via other link. Node is locked. Return extracted buffer.
  2010. */
  2011. static int link_recv_changeover_msg(struct link **l_ptr,
  2012. struct sk_buff **buf)
  2013. {
  2014. struct sk_buff *tunnel_buf = *buf;
  2015. struct link *dest_link;
  2016. struct tipc_msg *msg;
  2017. struct tipc_msg *tunnel_msg = buf_msg(tunnel_buf);
  2018. u32 msg_typ = msg_type(tunnel_msg);
  2019. u32 msg_count = msg_msgcnt(tunnel_msg);
  2020. dest_link = (*l_ptr)->owner->links[msg_bearer_id(tunnel_msg)];
  2021. if (!dest_link)
  2022. goto exit;
  2023. if (dest_link == *l_ptr) {
  2024. err("Unexpected changeover message on link <%s>\n",
  2025. (*l_ptr)->name);
  2026. goto exit;
  2027. }
  2028. *l_ptr = dest_link;
  2029. msg = msg_get_wrapped(tunnel_msg);
  2030. if (msg_typ == DUPLICATE_MSG) {
  2031. if (less(msg_seqno(msg), mod(dest_link->next_in_no)))
  2032. goto exit;
  2033. *buf = buf_extract(tunnel_buf, INT_H_SIZE);
  2034. if (*buf == NULL) {
  2035. warn("Link changeover error, duplicate msg dropped\n");
  2036. goto exit;
  2037. }
  2038. buf_discard(tunnel_buf);
  2039. return 1;
  2040. }
  2041. /* First original message ?: */
  2042. if (tipc_link_is_up(dest_link)) {
  2043. info("Resetting link <%s>, changeover initiated by peer\n",
  2044. dest_link->name);
  2045. tipc_link_reset(dest_link);
  2046. dest_link->exp_msg_count = msg_count;
  2047. if (!msg_count)
  2048. goto exit;
  2049. } else if (dest_link->exp_msg_count == START_CHANGEOVER) {
  2050. dest_link->exp_msg_count = msg_count;
  2051. if (!msg_count)
  2052. goto exit;
  2053. }
  2054. /* Receive original message */
  2055. if (dest_link->exp_msg_count == 0) {
  2056. warn("Link switchover error, "
  2057. "got too many tunnelled messages\n");
  2058. goto exit;
  2059. }
  2060. dest_link->exp_msg_count--;
  2061. if (less(msg_seqno(msg), dest_link->reset_checkpoint)) {
  2062. goto exit;
  2063. } else {
  2064. *buf = buf_extract(tunnel_buf, INT_H_SIZE);
  2065. if (*buf != NULL) {
  2066. buf_discard(tunnel_buf);
  2067. return 1;
  2068. } else {
  2069. warn("Link changeover error, original msg dropped\n");
  2070. }
  2071. }
  2072. exit:
  2073. *buf = NULL;
  2074. buf_discard(tunnel_buf);
  2075. return 0;
  2076. }
  2077. /*
  2078. * Bundler functionality:
  2079. */
  2080. void tipc_link_recv_bundle(struct sk_buff *buf)
  2081. {
  2082. u32 msgcount = msg_msgcnt(buf_msg(buf));
  2083. u32 pos = INT_H_SIZE;
  2084. struct sk_buff *obuf;
  2085. while (msgcount--) {
  2086. obuf = buf_extract(buf, pos);
  2087. if (obuf == NULL) {
  2088. warn("Link unable to unbundle message(s)\n");
  2089. break;
  2090. }
  2091. pos += align(msg_size(buf_msg(obuf)));
  2092. tipc_net_route_msg(obuf);
  2093. }
  2094. buf_discard(buf);
  2095. }
  2096. /*
  2097. * Fragmentation/defragmentation:
  2098. */
  2099. /*
  2100. * link_send_long_buf: Entry for buffers needing fragmentation.
  2101. * The buffer is complete, inclusive total message length.
  2102. * Returns user data length.
  2103. */
  2104. static int link_send_long_buf(struct link *l_ptr, struct sk_buff *buf)
  2105. {
  2106. struct sk_buff *buf_chain = NULL;
  2107. struct sk_buff *buf_chain_tail = (struct sk_buff *)&buf_chain;
  2108. struct tipc_msg *inmsg = buf_msg(buf);
  2109. struct tipc_msg fragm_hdr;
  2110. u32 insize = msg_size(inmsg);
  2111. u32 dsz = msg_data_sz(inmsg);
  2112. unchar *crs = buf->data;
  2113. u32 rest = insize;
  2114. u32 pack_sz = l_ptr->max_pkt;
  2115. u32 fragm_sz = pack_sz - INT_H_SIZE;
  2116. u32 fragm_no = 0;
  2117. u32 destaddr;
  2118. if (msg_short(inmsg))
  2119. destaddr = l_ptr->addr;
  2120. else
  2121. destaddr = msg_destnode(inmsg);
  2122. /* Prepare reusable fragment header: */
  2123. tipc_msg_init(&fragm_hdr, MSG_FRAGMENTER, FIRST_FRAGMENT,
  2124. INT_H_SIZE, destaddr);
  2125. /* Chop up message: */
  2126. while (rest > 0) {
  2127. struct sk_buff *fragm;
  2128. if (rest <= fragm_sz) {
  2129. fragm_sz = rest;
  2130. msg_set_type(&fragm_hdr, LAST_FRAGMENT);
  2131. }
  2132. fragm = tipc_buf_acquire(fragm_sz + INT_H_SIZE);
  2133. if (fragm == NULL) {
  2134. buf_discard(buf);
  2135. while (buf_chain) {
  2136. buf = buf_chain;
  2137. buf_chain = buf_chain->next;
  2138. buf_discard(buf);
  2139. }
  2140. return -ENOMEM;
  2141. }
  2142. msg_set_size(&fragm_hdr, fragm_sz + INT_H_SIZE);
  2143. fragm_no++;
  2144. msg_set_fragm_no(&fragm_hdr, fragm_no);
  2145. skb_copy_to_linear_data(fragm, &fragm_hdr, INT_H_SIZE);
  2146. skb_copy_to_linear_data_offset(fragm, INT_H_SIZE, crs,
  2147. fragm_sz);
  2148. buf_chain_tail->next = fragm;
  2149. buf_chain_tail = fragm;
  2150. rest -= fragm_sz;
  2151. crs += fragm_sz;
  2152. msg_set_type(&fragm_hdr, FRAGMENT);
  2153. }
  2154. buf_discard(buf);
  2155. /* Append chain of fragments to send queue & send them */
  2156. l_ptr->long_msg_seq_no++;
  2157. link_add_chain_to_outqueue(l_ptr, buf_chain, l_ptr->long_msg_seq_no);
  2158. l_ptr->stats.sent_fragments += fragm_no;
  2159. l_ptr->stats.sent_fragmented++;
  2160. tipc_link_push_queue(l_ptr);
  2161. return dsz;
  2162. }
  2163. /*
  2164. * A pending message being re-assembled must store certain values
  2165. * to handle subsequent fragments correctly. The following functions
  2166. * help storing these values in unused, available fields in the
  2167. * pending message. This makes dynamic memory allocation unnecessary.
  2168. */
  2169. static void set_long_msg_seqno(struct sk_buff *buf, u32 seqno)
  2170. {
  2171. msg_set_seqno(buf_msg(buf), seqno);
  2172. }
  2173. static u32 get_fragm_size(struct sk_buff *buf)
  2174. {
  2175. return msg_ack(buf_msg(buf));
  2176. }
  2177. static void set_fragm_size(struct sk_buff *buf, u32 sz)
  2178. {
  2179. msg_set_ack(buf_msg(buf), sz);
  2180. }
  2181. static u32 get_expected_frags(struct sk_buff *buf)
  2182. {
  2183. return msg_bcast_ack(buf_msg(buf));
  2184. }
  2185. static void set_expected_frags(struct sk_buff *buf, u32 exp)
  2186. {
  2187. msg_set_bcast_ack(buf_msg(buf), exp);
  2188. }
  2189. static u32 get_timer_cnt(struct sk_buff *buf)
  2190. {
  2191. return msg_reroute_cnt(buf_msg(buf));
  2192. }
  2193. static void incr_timer_cnt(struct sk_buff *buf)
  2194. {
  2195. msg_incr_reroute_cnt(buf_msg(buf));
  2196. }
  2197. /*
  2198. * tipc_link_recv_fragment(): Called with node lock on. Returns
  2199. * the reassembled buffer if message is complete.
  2200. */
  2201. int tipc_link_recv_fragment(struct sk_buff **pending, struct sk_buff **fb,
  2202. struct tipc_msg **m)
  2203. {
  2204. struct sk_buff *prev = NULL;
  2205. struct sk_buff *fbuf = *fb;
  2206. struct tipc_msg *fragm = buf_msg(fbuf);
  2207. struct sk_buff *pbuf = *pending;
  2208. u32 long_msg_seq_no = msg_long_msgno(fragm);
  2209. *fb = NULL;
  2210. /* Is there an incomplete message waiting for this fragment? */
  2211. while (pbuf && ((msg_seqno(buf_msg(pbuf)) != long_msg_seq_no) ||
  2212. (msg_orignode(fragm) != msg_orignode(buf_msg(pbuf))))) {
  2213. prev = pbuf;
  2214. pbuf = pbuf->next;
  2215. }
  2216. if (!pbuf && (msg_type(fragm) == FIRST_FRAGMENT)) {
  2217. struct tipc_msg *imsg = (struct tipc_msg *)msg_data(fragm);
  2218. u32 msg_sz = msg_size(imsg);
  2219. u32 fragm_sz = msg_data_sz(fragm);
  2220. u32 exp_fragm_cnt = msg_sz/fragm_sz + !!(msg_sz % fragm_sz);
  2221. u32 max = TIPC_MAX_USER_MSG_SIZE + NAMED_H_SIZE;
  2222. if (msg_type(imsg) == TIPC_MCAST_MSG)
  2223. max = TIPC_MAX_USER_MSG_SIZE + MCAST_H_SIZE;
  2224. if (msg_size(imsg) > max) {
  2225. buf_discard(fbuf);
  2226. return 0;
  2227. }
  2228. pbuf = tipc_buf_acquire(msg_size(imsg));
  2229. if (pbuf != NULL) {
  2230. pbuf->next = *pending;
  2231. *pending = pbuf;
  2232. skb_copy_to_linear_data(pbuf, imsg,
  2233. msg_data_sz(fragm));
  2234. /* Prepare buffer for subsequent fragments. */
  2235. set_long_msg_seqno(pbuf, long_msg_seq_no);
  2236. set_fragm_size(pbuf, fragm_sz);
  2237. set_expected_frags(pbuf, exp_fragm_cnt - 1);
  2238. } else {
  2239. warn("Link unable to reassemble fragmented message\n");
  2240. }
  2241. buf_discard(fbuf);
  2242. return 0;
  2243. } else if (pbuf && (msg_type(fragm) != FIRST_FRAGMENT)) {
  2244. u32 dsz = msg_data_sz(fragm);
  2245. u32 fsz = get_fragm_size(pbuf);
  2246. u32 crs = ((msg_fragm_no(fragm) - 1) * fsz);
  2247. u32 exp_frags = get_expected_frags(pbuf) - 1;
  2248. skb_copy_to_linear_data_offset(pbuf, crs,
  2249. msg_data(fragm), dsz);
  2250. buf_discard(fbuf);
  2251. /* Is message complete? */
  2252. if (exp_frags == 0) {
  2253. if (prev)
  2254. prev->next = pbuf->next;
  2255. else
  2256. *pending = pbuf->next;
  2257. msg_reset_reroute_cnt(buf_msg(pbuf));
  2258. *fb = pbuf;
  2259. *m = buf_msg(pbuf);
  2260. return 1;
  2261. }
  2262. set_expected_frags(pbuf, exp_frags);
  2263. return 0;
  2264. }
  2265. buf_discard(fbuf);
  2266. return 0;
  2267. }
  2268. /**
  2269. * link_check_defragm_bufs - flush stale incoming message fragments
  2270. * @l_ptr: pointer to link
  2271. */
  2272. static void link_check_defragm_bufs(struct link *l_ptr)
  2273. {
  2274. struct sk_buff *prev = NULL;
  2275. struct sk_buff *next = NULL;
  2276. struct sk_buff *buf = l_ptr->defragm_buf;
  2277. if (!buf)
  2278. return;
  2279. if (!link_working_working(l_ptr))
  2280. return;
  2281. while (buf) {
  2282. u32 cnt = get_timer_cnt(buf);
  2283. next = buf->next;
  2284. if (cnt < 4) {
  2285. incr_timer_cnt(buf);
  2286. prev = buf;
  2287. } else {
  2288. if (prev)
  2289. prev->next = buf->next;
  2290. else
  2291. l_ptr->defragm_buf = buf->next;
  2292. buf_discard(buf);
  2293. }
  2294. buf = next;
  2295. }
  2296. }
  2297. static void link_set_supervision_props(struct link *l_ptr, u32 tolerance)
  2298. {
  2299. if ((tolerance < TIPC_MIN_LINK_TOL) || (tolerance > TIPC_MAX_LINK_TOL))
  2300. return;
  2301. l_ptr->tolerance = tolerance;
  2302. l_ptr->continuity_interval =
  2303. ((tolerance / 4) > 500) ? 500 : tolerance / 4;
  2304. l_ptr->abort_limit = tolerance / (l_ptr->continuity_interval / 4);
  2305. }
  2306. void tipc_link_set_queue_limits(struct link *l_ptr, u32 window)
  2307. {
  2308. /* Data messages from this node, inclusive FIRST_FRAGM */
  2309. l_ptr->queue_limit[TIPC_LOW_IMPORTANCE] = window;
  2310. l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE] = (window / 3) * 4;
  2311. l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE] = (window / 3) * 5;
  2312. l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE] = (window / 3) * 6;
  2313. /* Transiting data messages,inclusive FIRST_FRAGM */
  2314. l_ptr->queue_limit[TIPC_LOW_IMPORTANCE + 4] = 300;
  2315. l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE + 4] = 600;
  2316. l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE + 4] = 900;
  2317. l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE + 4] = 1200;
  2318. l_ptr->queue_limit[CONN_MANAGER] = 1200;
  2319. l_ptr->queue_limit[CHANGEOVER_PROTOCOL] = 2500;
  2320. l_ptr->queue_limit[NAME_DISTRIBUTOR] = 3000;
  2321. /* FRAGMENT and LAST_FRAGMENT packets */
  2322. l_ptr->queue_limit[MSG_FRAGMENTER] = 4000;
  2323. }
  2324. /**
  2325. * link_find_link - locate link by name
  2326. * @name - ptr to link name string
  2327. * @node - ptr to area to be filled with ptr to associated node
  2328. *
  2329. * Caller must hold 'tipc_net_lock' to ensure node and bearer are not deleted;
  2330. * this also prevents link deletion.
  2331. *
  2332. * Returns pointer to link (or 0 if invalid link name).
  2333. */
  2334. static struct link *link_find_link(const char *name, struct tipc_node **node)
  2335. {
  2336. struct link_name link_name_parts;
  2337. struct tipc_bearer *b_ptr;
  2338. struct link *l_ptr;
  2339. if (!link_name_validate(name, &link_name_parts))
  2340. return NULL;
  2341. b_ptr = tipc_bearer_find_interface(link_name_parts.if_local);
  2342. if (!b_ptr)
  2343. return NULL;
  2344. *node = tipc_node_find(link_name_parts.addr_peer);
  2345. if (!*node)
  2346. return NULL;
  2347. l_ptr = (*node)->links[b_ptr->identity];
  2348. if (!l_ptr || strcmp(l_ptr->name, name))
  2349. return NULL;
  2350. return l_ptr;
  2351. }
  2352. /**
  2353. * link_value_is_valid -- validate proposed link tolerance/priority/window
  2354. *
  2355. * @cmd - value type (TIPC_CMD_SET_LINK_*)
  2356. * @new_value - the new value
  2357. *
  2358. * Returns 1 if value is within range, 0 if not.
  2359. */
  2360. static int link_value_is_valid(u16 cmd, u32 new_value)
  2361. {
  2362. switch (cmd) {
  2363. case TIPC_CMD_SET_LINK_TOL:
  2364. return (new_value >= TIPC_MIN_LINK_TOL) &&
  2365. (new_value <= TIPC_MAX_LINK_TOL);
  2366. case TIPC_CMD_SET_LINK_PRI:
  2367. return (new_value <= TIPC_MAX_LINK_PRI);
  2368. case TIPC_CMD_SET_LINK_WINDOW:
  2369. return (new_value >= TIPC_MIN_LINK_WIN) &&
  2370. (new_value <= TIPC_MAX_LINK_WIN);
  2371. }
  2372. return 0;
  2373. }
  2374. /**
  2375. * link_cmd_set_value - change priority/tolerance/window for link/bearer/media
  2376. * @name - ptr to link, bearer, or media name
  2377. * @new_value - new value of link, bearer, or media setting
  2378. * @cmd - which link, bearer, or media attribute to set (TIPC_CMD_SET_LINK_*)
  2379. *
  2380. * Caller must hold 'tipc_net_lock' to ensure link/bearer/media is not deleted.
  2381. *
  2382. * Returns 0 if value updated and negative value on error.
  2383. */
  2384. static int link_cmd_set_value(const char *name, u32 new_value, u16 cmd)
  2385. {
  2386. struct tipc_node *node;
  2387. struct link *l_ptr;
  2388. struct tipc_bearer *b_ptr;
  2389. struct media *m_ptr;
  2390. l_ptr = link_find_link(name, &node);
  2391. if (l_ptr) {
  2392. /*
  2393. * acquire node lock for tipc_link_send_proto_msg().
  2394. * see "TIPC locking policy" in net.c.
  2395. */
  2396. tipc_node_lock(node);
  2397. switch (cmd) {
  2398. case TIPC_CMD_SET_LINK_TOL:
  2399. link_set_supervision_props(l_ptr, new_value);
  2400. tipc_link_send_proto_msg(l_ptr,
  2401. STATE_MSG, 0, 0, new_value, 0, 0);
  2402. break;
  2403. case TIPC_CMD_SET_LINK_PRI:
  2404. l_ptr->priority = new_value;
  2405. tipc_link_send_proto_msg(l_ptr,
  2406. STATE_MSG, 0, 0, 0, new_value, 0);
  2407. break;
  2408. case TIPC_CMD_SET_LINK_WINDOW:
  2409. tipc_link_set_queue_limits(l_ptr, new_value);
  2410. break;
  2411. }
  2412. tipc_node_unlock(node);
  2413. return 0;
  2414. }
  2415. b_ptr = tipc_bearer_find(name);
  2416. if (b_ptr) {
  2417. switch (cmd) {
  2418. case TIPC_CMD_SET_LINK_TOL:
  2419. b_ptr->tolerance = new_value;
  2420. return 0;
  2421. case TIPC_CMD_SET_LINK_PRI:
  2422. b_ptr->priority = new_value;
  2423. return 0;
  2424. case TIPC_CMD_SET_LINK_WINDOW:
  2425. b_ptr->window = new_value;
  2426. return 0;
  2427. }
  2428. return -EINVAL;
  2429. }
  2430. m_ptr = tipc_media_find(name);
  2431. if (!m_ptr)
  2432. return -ENODEV;
  2433. switch (cmd) {
  2434. case TIPC_CMD_SET_LINK_TOL:
  2435. m_ptr->tolerance = new_value;
  2436. return 0;
  2437. case TIPC_CMD_SET_LINK_PRI:
  2438. m_ptr->priority = new_value;
  2439. return 0;
  2440. case TIPC_CMD_SET_LINK_WINDOW:
  2441. m_ptr->window = new_value;
  2442. return 0;
  2443. }
  2444. return -EINVAL;
  2445. }
  2446. struct sk_buff *tipc_link_cmd_config(const void *req_tlv_area, int req_tlv_space,
  2447. u16 cmd)
  2448. {
  2449. struct tipc_link_config *args;
  2450. u32 new_value;
  2451. int res;
  2452. if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_CONFIG))
  2453. return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
  2454. args = (struct tipc_link_config *)TLV_DATA(req_tlv_area);
  2455. new_value = ntohl(args->value);
  2456. if (!link_value_is_valid(cmd, new_value))
  2457. return tipc_cfg_reply_error_string(
  2458. "cannot change, value invalid");
  2459. if (!strcmp(args->name, tipc_bclink_name)) {
  2460. if ((cmd == TIPC_CMD_SET_LINK_WINDOW) &&
  2461. (tipc_bclink_set_queue_limits(new_value) == 0))
  2462. return tipc_cfg_reply_none();
  2463. return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
  2464. " (cannot change setting on broadcast link)");
  2465. }
  2466. read_lock_bh(&tipc_net_lock);
  2467. res = link_cmd_set_value(args->name, new_value, cmd);
  2468. read_unlock_bh(&tipc_net_lock);
  2469. if (res)
  2470. return tipc_cfg_reply_error_string("cannot change link setting");
  2471. return tipc_cfg_reply_none();
  2472. }
  2473. /**
  2474. * link_reset_statistics - reset link statistics
  2475. * @l_ptr: pointer to link
  2476. */
  2477. static void link_reset_statistics(struct link *l_ptr)
  2478. {
  2479. memset(&l_ptr->stats, 0, sizeof(l_ptr->stats));
  2480. l_ptr->stats.sent_info = l_ptr->next_out_no;
  2481. l_ptr->stats.recv_info = l_ptr->next_in_no;
  2482. }
  2483. struct sk_buff *tipc_link_cmd_reset_stats(const void *req_tlv_area, int req_tlv_space)
  2484. {
  2485. char *link_name;
  2486. struct link *l_ptr;
  2487. struct tipc_node *node;
  2488. if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
  2489. return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
  2490. link_name = (char *)TLV_DATA(req_tlv_area);
  2491. if (!strcmp(link_name, tipc_bclink_name)) {
  2492. if (tipc_bclink_reset_stats())
  2493. return tipc_cfg_reply_error_string("link not found");
  2494. return tipc_cfg_reply_none();
  2495. }
  2496. read_lock_bh(&tipc_net_lock);
  2497. l_ptr = link_find_link(link_name, &node);
  2498. if (!l_ptr) {
  2499. read_unlock_bh(&tipc_net_lock);
  2500. return tipc_cfg_reply_error_string("link not found");
  2501. }
  2502. tipc_node_lock(node);
  2503. link_reset_statistics(l_ptr);
  2504. tipc_node_unlock(node);
  2505. read_unlock_bh(&tipc_net_lock);
  2506. return tipc_cfg_reply_none();
  2507. }
  2508. /**
  2509. * percent - convert count to a percentage of total (rounding up or down)
  2510. */
  2511. static u32 percent(u32 count, u32 total)
  2512. {
  2513. return (count * 100 + (total / 2)) / total;
  2514. }
  2515. /**
  2516. * tipc_link_stats - print link statistics
  2517. * @name: link name
  2518. * @buf: print buffer area
  2519. * @buf_size: size of print buffer area
  2520. *
  2521. * Returns length of print buffer data string (or 0 if error)
  2522. */
  2523. static int tipc_link_stats(const char *name, char *buf, const u32 buf_size)
  2524. {
  2525. struct print_buf pb;
  2526. struct link *l_ptr;
  2527. struct tipc_node *node;
  2528. char *status;
  2529. u32 profile_total = 0;
  2530. if (!strcmp(name, tipc_bclink_name))
  2531. return tipc_bclink_stats(buf, buf_size);
  2532. tipc_printbuf_init(&pb, buf, buf_size);
  2533. read_lock_bh(&tipc_net_lock);
  2534. l_ptr = link_find_link(name, &node);
  2535. if (!l_ptr) {
  2536. read_unlock_bh(&tipc_net_lock);
  2537. return 0;
  2538. }
  2539. tipc_node_lock(node);
  2540. if (tipc_link_is_active(l_ptr))
  2541. status = "ACTIVE";
  2542. else if (tipc_link_is_up(l_ptr))
  2543. status = "STANDBY";
  2544. else
  2545. status = "DEFUNCT";
  2546. tipc_printf(&pb, "Link <%s>\n"
  2547. " %s MTU:%u Priority:%u Tolerance:%u ms"
  2548. " Window:%u packets\n",
  2549. l_ptr->name, status, l_ptr->max_pkt,
  2550. l_ptr->priority, l_ptr->tolerance, l_ptr->queue_limit[0]);
  2551. tipc_printf(&pb, " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
  2552. l_ptr->next_in_no - l_ptr->stats.recv_info,
  2553. l_ptr->stats.recv_fragments,
  2554. l_ptr->stats.recv_fragmented,
  2555. l_ptr->stats.recv_bundles,
  2556. l_ptr->stats.recv_bundled);
  2557. tipc_printf(&pb, " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
  2558. l_ptr->next_out_no - l_ptr->stats.sent_info,
  2559. l_ptr->stats.sent_fragments,
  2560. l_ptr->stats.sent_fragmented,
  2561. l_ptr->stats.sent_bundles,
  2562. l_ptr->stats.sent_bundled);
  2563. profile_total = l_ptr->stats.msg_length_counts;
  2564. if (!profile_total)
  2565. profile_total = 1;
  2566. tipc_printf(&pb, " TX profile sample:%u packets average:%u octets\n"
  2567. " 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% "
  2568. "-16384:%u%% -32768:%u%% -66000:%u%%\n",
  2569. l_ptr->stats.msg_length_counts,
  2570. l_ptr->stats.msg_lengths_total / profile_total,
  2571. percent(l_ptr->stats.msg_length_profile[0], profile_total),
  2572. percent(l_ptr->stats.msg_length_profile[1], profile_total),
  2573. percent(l_ptr->stats.msg_length_profile[2], profile_total),
  2574. percent(l_ptr->stats.msg_length_profile[3], profile_total),
  2575. percent(l_ptr->stats.msg_length_profile[4], profile_total),
  2576. percent(l_ptr->stats.msg_length_profile[5], profile_total),
  2577. percent(l_ptr->stats.msg_length_profile[6], profile_total));
  2578. tipc_printf(&pb, " RX states:%u probes:%u naks:%u defs:%u dups:%u\n",
  2579. l_ptr->stats.recv_states,
  2580. l_ptr->stats.recv_probes,
  2581. l_ptr->stats.recv_nacks,
  2582. l_ptr->stats.deferred_recv,
  2583. l_ptr->stats.duplicates);
  2584. tipc_printf(&pb, " TX states:%u probes:%u naks:%u acks:%u dups:%u\n",
  2585. l_ptr->stats.sent_states,
  2586. l_ptr->stats.sent_probes,
  2587. l_ptr->stats.sent_nacks,
  2588. l_ptr->stats.sent_acks,
  2589. l_ptr->stats.retransmitted);
  2590. tipc_printf(&pb, " Congestion bearer:%u link:%u Send queue max:%u avg:%u\n",
  2591. l_ptr->stats.bearer_congs,
  2592. l_ptr->stats.link_congs,
  2593. l_ptr->stats.max_queue_sz,
  2594. l_ptr->stats.queue_sz_counts
  2595. ? (l_ptr->stats.accu_queue_sz / l_ptr->stats.queue_sz_counts)
  2596. : 0);
  2597. tipc_node_unlock(node);
  2598. read_unlock_bh(&tipc_net_lock);
  2599. return tipc_printbuf_validate(&pb);
  2600. }
  2601. #define MAX_LINK_STATS_INFO 2000
  2602. struct sk_buff *tipc_link_cmd_show_stats(const void *req_tlv_area, int req_tlv_space)
  2603. {
  2604. struct sk_buff *buf;
  2605. struct tlv_desc *rep_tlv;
  2606. int str_len;
  2607. if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
  2608. return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
  2609. buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_LINK_STATS_INFO));
  2610. if (!buf)
  2611. return NULL;
  2612. rep_tlv = (struct tlv_desc *)buf->data;
  2613. str_len = tipc_link_stats((char *)TLV_DATA(req_tlv_area),
  2614. (char *)TLV_DATA(rep_tlv), MAX_LINK_STATS_INFO);
  2615. if (!str_len) {
  2616. buf_discard(buf);
  2617. return tipc_cfg_reply_error_string("link not found");
  2618. }
  2619. skb_put(buf, TLV_SPACE(str_len));
  2620. TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
  2621. return buf;
  2622. }
  2623. /**
  2624. * tipc_link_get_max_pkt - get maximum packet size to use when sending to destination
  2625. * @dest: network address of destination node
  2626. * @selector: used to select from set of active links
  2627. *
  2628. * If no active link can be found, uses default maximum packet size.
  2629. */
  2630. u32 tipc_link_get_max_pkt(u32 dest, u32 selector)
  2631. {
  2632. struct tipc_node *n_ptr;
  2633. struct link *l_ptr;
  2634. u32 res = MAX_PKT_DEFAULT;
  2635. if (dest == tipc_own_addr)
  2636. return MAX_MSG_SIZE;
  2637. read_lock_bh(&tipc_net_lock);
  2638. n_ptr = tipc_node_find(dest);
  2639. if (n_ptr) {
  2640. tipc_node_lock(n_ptr);
  2641. l_ptr = n_ptr->active_links[selector & 1];
  2642. if (l_ptr)
  2643. res = l_ptr->max_pkt;
  2644. tipc_node_unlock(n_ptr);
  2645. }
  2646. read_unlock_bh(&tipc_net_lock);
  2647. return res;
  2648. }
  2649. static void link_print(struct link *l_ptr, const char *str)
  2650. {
  2651. char print_area[256];
  2652. struct print_buf pb;
  2653. struct print_buf *buf = &pb;
  2654. tipc_printbuf_init(buf, print_area, sizeof(print_area));
  2655. tipc_printf(buf, str);
  2656. tipc_printf(buf, "Link %x<%s>:",
  2657. l_ptr->addr, l_ptr->b_ptr->name);
  2658. #ifdef CONFIG_TIPC_DEBUG
  2659. if (link_reset_reset(l_ptr) || link_reset_unknown(l_ptr))
  2660. goto print_state;
  2661. tipc_printf(buf, ": NXO(%u):", mod(l_ptr->next_out_no));
  2662. tipc_printf(buf, "NXI(%u):", mod(l_ptr->next_in_no));
  2663. tipc_printf(buf, "SQUE");
  2664. if (l_ptr->first_out) {
  2665. tipc_printf(buf, "[%u..", msg_seqno(buf_msg(l_ptr->first_out)));
  2666. if (l_ptr->next_out)
  2667. tipc_printf(buf, "%u..",
  2668. msg_seqno(buf_msg(l_ptr->next_out)));
  2669. tipc_printf(buf, "%u]", msg_seqno(buf_msg(l_ptr->last_out)));
  2670. if ((mod(msg_seqno(buf_msg(l_ptr->last_out)) -
  2671. msg_seqno(buf_msg(l_ptr->first_out)))
  2672. != (l_ptr->out_queue_size - 1)) ||
  2673. (l_ptr->last_out->next != NULL)) {
  2674. tipc_printf(buf, "\nSend queue inconsistency\n");
  2675. tipc_printf(buf, "first_out= %p ", l_ptr->first_out);
  2676. tipc_printf(buf, "next_out= %p ", l_ptr->next_out);
  2677. tipc_printf(buf, "last_out= %p ", l_ptr->last_out);
  2678. }
  2679. } else
  2680. tipc_printf(buf, "[]");
  2681. tipc_printf(buf, "SQSIZ(%u)", l_ptr->out_queue_size);
  2682. if (l_ptr->oldest_deferred_in) {
  2683. u32 o = msg_seqno(buf_msg(l_ptr->oldest_deferred_in));
  2684. u32 n = msg_seqno(buf_msg(l_ptr->newest_deferred_in));
  2685. tipc_printf(buf, ":RQUE[%u..%u]", o, n);
  2686. if (l_ptr->deferred_inqueue_sz != mod((n + 1) - o)) {
  2687. tipc_printf(buf, ":RQSIZ(%u)",
  2688. l_ptr->deferred_inqueue_sz);
  2689. }
  2690. }
  2691. print_state:
  2692. #endif
  2693. if (link_working_unknown(l_ptr))
  2694. tipc_printf(buf, ":WU");
  2695. else if (link_reset_reset(l_ptr))
  2696. tipc_printf(buf, ":RR");
  2697. else if (link_reset_unknown(l_ptr))
  2698. tipc_printf(buf, ":RU");
  2699. else if (link_working_working(l_ptr))
  2700. tipc_printf(buf, ":WW");
  2701. tipc_printf(buf, "\n");
  2702. tipc_printbuf_validate(buf);
  2703. info("%s", print_area);
  2704. }