link.c 72 KB

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