link.c 87 KB

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