link.c 73 KB

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