link.c 85 KB

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