link.c 70 KB

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