link.c 80 KB

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