link.c 78 KB

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