link.c 81 KB

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