link.c 83 KB

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