link.c 81 KB

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