link.c 82 KB

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