link.c 82 KB

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