link.c 63 KB

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