link.c 78 KB

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