link.c 78 KB

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