link.c 63 KB

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