link.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135
  1. /*
  2. * net/tipc/link.c: TIPC link code
  3. *
  4. * Copyright (c) 1996-2007, 2012-2016, 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 "subscr.h"
  38. #include "link.h"
  39. #include "bcast.h"
  40. #include "socket.h"
  41. #include "name_distr.h"
  42. #include "discover.h"
  43. #include "netlink.h"
  44. #include "monitor.h"
  45. #include <linux/pkt_sched.h>
  46. struct tipc_stats {
  47. u32 sent_pkts;
  48. u32 recv_pkts;
  49. u32 sent_states;
  50. u32 recv_states;
  51. u32 sent_probes;
  52. u32 recv_probes;
  53. u32 sent_nacks;
  54. u32 recv_nacks;
  55. u32 sent_acks;
  56. u32 sent_bundled;
  57. u32 sent_bundles;
  58. u32 recv_bundled;
  59. u32 recv_bundles;
  60. u32 retransmitted;
  61. u32 sent_fragmented;
  62. u32 sent_fragments;
  63. u32 recv_fragmented;
  64. u32 recv_fragments;
  65. u32 link_congs; /* # port sends blocked by congestion */
  66. u32 deferred_recv;
  67. u32 duplicates;
  68. u32 max_queue_sz; /* send queue size high water mark */
  69. u32 accu_queue_sz; /* used for send queue size profiling */
  70. u32 queue_sz_counts; /* used for send queue size profiling */
  71. u32 msg_length_counts; /* used for message length profiling */
  72. u32 msg_lengths_total; /* used for message length profiling */
  73. u32 msg_length_profile[7]; /* used for msg. length profiling */
  74. };
  75. /**
  76. * struct tipc_link - TIPC link data structure
  77. * @addr: network address of link's peer node
  78. * @name: link name character string
  79. * @media_addr: media address to use when sending messages over link
  80. * @timer: link timer
  81. * @net: pointer to namespace struct
  82. * @refcnt: reference counter for permanent references (owner node & timer)
  83. * @peer_session: link session # being used by peer end of link
  84. * @peer_bearer_id: bearer id used by link's peer endpoint
  85. * @bearer_id: local bearer id used by link
  86. * @tolerance: minimum link continuity loss needed to reset link [in ms]
  87. * @abort_limit: # of unacknowledged continuity probes needed to reset link
  88. * @state: current state of link FSM
  89. * @peer_caps: bitmap describing capabilities of peer node
  90. * @silent_intv_cnt: # of timer intervals without any reception from peer
  91. * @proto_msg: template for control messages generated by link
  92. * @pmsg: convenience pointer to "proto_msg" field
  93. * @priority: current link priority
  94. * @net_plane: current link network plane ('A' through 'H')
  95. * @mon_state: cookie with information needed by link monitor
  96. * @backlog_limit: backlog queue congestion thresholds (indexed by importance)
  97. * @exp_msg_count: # of tunnelled messages expected during link changeover
  98. * @reset_rcv_checkpt: seq # of last acknowledged message at time of link reset
  99. * @mtu: current maximum packet size for this link
  100. * @advertised_mtu: advertised own mtu when link is being established
  101. * @transmitq: queue for sent, non-acked messages
  102. * @backlogq: queue for messages waiting to be sent
  103. * @snt_nxt: next sequence number to use for outbound messages
  104. * @last_retransmitted: sequence number of most recently retransmitted message
  105. * @stale_count: # of identical retransmit requests made by peer
  106. * @ackers: # of peers that needs to ack each packet before it can be released
  107. * @acked: # last packet acked by a certain peer. Used for broadcast.
  108. * @rcv_nxt: next sequence number to expect for inbound messages
  109. * @deferred_queue: deferred queue saved OOS b'cast message received from node
  110. * @unacked_window: # of inbound messages rx'd without ack'ing back to peer
  111. * @inputq: buffer queue for messages to be delivered upwards
  112. * @namedq: buffer queue for name table messages to be delivered upwards
  113. * @next_out: ptr to first unsent outbound message in queue
  114. * @wakeupq: linked list of wakeup msgs waiting for link congestion to abate
  115. * @long_msg_seq_no: next identifier to use for outbound fragmented messages
  116. * @reasm_buf: head of partially reassembled inbound message fragments
  117. * @bc_rcvr: marks that this is a broadcast receiver link
  118. * @stats: collects statistics regarding link activity
  119. */
  120. struct tipc_link {
  121. u32 addr;
  122. char name[TIPC_MAX_LINK_NAME];
  123. struct net *net;
  124. /* Management and link supervision data */
  125. u32 peer_session;
  126. u32 session;
  127. u32 peer_bearer_id;
  128. u32 bearer_id;
  129. u32 tolerance;
  130. u32 abort_limit;
  131. u32 state;
  132. u16 peer_caps;
  133. bool active;
  134. u32 silent_intv_cnt;
  135. char if_name[TIPC_MAX_IF_NAME];
  136. u32 priority;
  137. char net_plane;
  138. struct tipc_mon_state mon_state;
  139. u16 rst_cnt;
  140. /* Failover/synch */
  141. u16 drop_point;
  142. struct sk_buff *failover_reasm_skb;
  143. /* Max packet negotiation */
  144. u16 mtu;
  145. u16 advertised_mtu;
  146. /* Sending */
  147. struct sk_buff_head transmq;
  148. struct sk_buff_head backlogq;
  149. struct {
  150. u16 len;
  151. u16 limit;
  152. } backlog[5];
  153. u16 snd_nxt;
  154. u16 last_retransm;
  155. u16 window;
  156. u32 stale_count;
  157. /* Reception */
  158. u16 rcv_nxt;
  159. u32 rcv_unacked;
  160. struct sk_buff_head deferdq;
  161. struct sk_buff_head *inputq;
  162. struct sk_buff_head *namedq;
  163. /* Congestion handling */
  164. struct sk_buff_head wakeupq;
  165. /* Fragmentation/reassembly */
  166. struct sk_buff *reasm_buf;
  167. /* Broadcast */
  168. u16 ackers;
  169. u16 acked;
  170. struct tipc_link *bc_rcvlink;
  171. struct tipc_link *bc_sndlink;
  172. unsigned long prev_retr;
  173. u16 prev_from;
  174. u16 prev_to;
  175. u8 nack_state;
  176. bool bc_peer_is_up;
  177. /* Statistics */
  178. struct tipc_stats stats;
  179. };
  180. /*
  181. * Error message prefixes
  182. */
  183. static const char *link_co_err = "Link tunneling error, ";
  184. static const char *link_rst_msg = "Resetting link ";
  185. /* Send states for broadcast NACKs
  186. */
  187. enum {
  188. BC_NACK_SND_CONDITIONAL,
  189. BC_NACK_SND_UNCONDITIONAL,
  190. BC_NACK_SND_SUPPRESS,
  191. };
  192. #define TIPC_BC_RETR_LIMIT 10 /* [ms] */
  193. /*
  194. * Interval between NACKs when packets arrive out of order
  195. */
  196. #define TIPC_NACK_INTV (TIPC_MIN_LINK_WIN * 2)
  197. /* Wildcard value for link session numbers. When it is known that
  198. * peer endpoint is down, any session number must be accepted.
  199. */
  200. #define ANY_SESSION 0x10000
  201. /* Link FSM states:
  202. */
  203. enum {
  204. LINK_ESTABLISHED = 0xe,
  205. LINK_ESTABLISHING = 0xe << 4,
  206. LINK_RESET = 0x1 << 8,
  207. LINK_RESETTING = 0x2 << 12,
  208. LINK_PEER_RESET = 0xd << 16,
  209. LINK_FAILINGOVER = 0xf << 20,
  210. LINK_SYNCHING = 0xc << 24
  211. };
  212. /* Link FSM state checking routines
  213. */
  214. static int link_is_up(struct tipc_link *l)
  215. {
  216. return l->state & (LINK_ESTABLISHED | LINK_SYNCHING);
  217. }
  218. static int tipc_link_proto_rcv(struct tipc_link *l, struct sk_buff *skb,
  219. struct sk_buff_head *xmitq);
  220. static void tipc_link_build_proto_msg(struct tipc_link *l, int mtyp, bool probe,
  221. u16 rcvgap, int tolerance, int priority,
  222. struct sk_buff_head *xmitq);
  223. static void link_print(struct tipc_link *l, const char *str);
  224. static int tipc_link_build_nack_msg(struct tipc_link *l,
  225. struct sk_buff_head *xmitq);
  226. static void tipc_link_build_bc_init_msg(struct tipc_link *l,
  227. struct sk_buff_head *xmitq);
  228. static bool tipc_link_release_pkts(struct tipc_link *l, u16 to);
  229. /*
  230. * Simple non-static link routines (i.e. referenced outside this file)
  231. */
  232. bool tipc_link_is_up(struct tipc_link *l)
  233. {
  234. return link_is_up(l);
  235. }
  236. bool tipc_link_peer_is_down(struct tipc_link *l)
  237. {
  238. return l->state == LINK_PEER_RESET;
  239. }
  240. bool tipc_link_is_reset(struct tipc_link *l)
  241. {
  242. return l->state & (LINK_RESET | LINK_FAILINGOVER | LINK_ESTABLISHING);
  243. }
  244. bool tipc_link_is_establishing(struct tipc_link *l)
  245. {
  246. return l->state == LINK_ESTABLISHING;
  247. }
  248. bool tipc_link_is_synching(struct tipc_link *l)
  249. {
  250. return l->state == LINK_SYNCHING;
  251. }
  252. bool tipc_link_is_failingover(struct tipc_link *l)
  253. {
  254. return l->state == LINK_FAILINGOVER;
  255. }
  256. bool tipc_link_is_blocked(struct tipc_link *l)
  257. {
  258. return l->state & (LINK_RESETTING | LINK_PEER_RESET | LINK_FAILINGOVER);
  259. }
  260. static bool link_is_bc_sndlink(struct tipc_link *l)
  261. {
  262. return !l->bc_sndlink;
  263. }
  264. static bool link_is_bc_rcvlink(struct tipc_link *l)
  265. {
  266. return ((l->bc_rcvlink == l) && !link_is_bc_sndlink(l));
  267. }
  268. int tipc_link_is_active(struct tipc_link *l)
  269. {
  270. return l->active;
  271. }
  272. void tipc_link_set_active(struct tipc_link *l, bool active)
  273. {
  274. l->active = active;
  275. }
  276. u32 tipc_link_id(struct tipc_link *l)
  277. {
  278. return l->peer_bearer_id << 16 | l->bearer_id;
  279. }
  280. int tipc_link_window(struct tipc_link *l)
  281. {
  282. return l->window;
  283. }
  284. int tipc_link_prio(struct tipc_link *l)
  285. {
  286. return l->priority;
  287. }
  288. unsigned long tipc_link_tolerance(struct tipc_link *l)
  289. {
  290. return l->tolerance;
  291. }
  292. struct sk_buff_head *tipc_link_inputq(struct tipc_link *l)
  293. {
  294. return l->inputq;
  295. }
  296. char tipc_link_plane(struct tipc_link *l)
  297. {
  298. return l->net_plane;
  299. }
  300. void tipc_link_add_bc_peer(struct tipc_link *snd_l,
  301. struct tipc_link *uc_l,
  302. struct sk_buff_head *xmitq)
  303. {
  304. struct tipc_link *rcv_l = uc_l->bc_rcvlink;
  305. snd_l->ackers++;
  306. rcv_l->acked = snd_l->snd_nxt - 1;
  307. snd_l->state = LINK_ESTABLISHED;
  308. tipc_link_build_bc_init_msg(uc_l, xmitq);
  309. }
  310. void tipc_link_remove_bc_peer(struct tipc_link *snd_l,
  311. struct tipc_link *rcv_l,
  312. struct sk_buff_head *xmitq)
  313. {
  314. u16 ack = snd_l->snd_nxt - 1;
  315. snd_l->ackers--;
  316. rcv_l->bc_peer_is_up = true;
  317. rcv_l->state = LINK_ESTABLISHED;
  318. tipc_link_bc_ack_rcv(rcv_l, ack, xmitq);
  319. tipc_link_reset(rcv_l);
  320. rcv_l->state = LINK_RESET;
  321. if (!snd_l->ackers) {
  322. tipc_link_reset(snd_l);
  323. snd_l->state = LINK_RESET;
  324. __skb_queue_purge(xmitq);
  325. }
  326. }
  327. int tipc_link_bc_peers(struct tipc_link *l)
  328. {
  329. return l->ackers;
  330. }
  331. u16 link_bc_rcv_gap(struct tipc_link *l)
  332. {
  333. struct sk_buff *skb = skb_peek(&l->deferdq);
  334. u16 gap = 0;
  335. if (more(l->snd_nxt, l->rcv_nxt))
  336. gap = l->snd_nxt - l->rcv_nxt;
  337. if (skb)
  338. gap = buf_seqno(skb) - l->rcv_nxt;
  339. return gap;
  340. }
  341. void tipc_link_set_mtu(struct tipc_link *l, int mtu)
  342. {
  343. l->mtu = mtu;
  344. }
  345. int tipc_link_mtu(struct tipc_link *l)
  346. {
  347. return l->mtu;
  348. }
  349. u16 tipc_link_rcv_nxt(struct tipc_link *l)
  350. {
  351. return l->rcv_nxt;
  352. }
  353. u16 tipc_link_acked(struct tipc_link *l)
  354. {
  355. return l->acked;
  356. }
  357. char *tipc_link_name(struct tipc_link *l)
  358. {
  359. return l->name;
  360. }
  361. /**
  362. * tipc_link_create - create a new link
  363. * @n: pointer to associated node
  364. * @if_name: associated interface name
  365. * @bearer_id: id (index) of associated bearer
  366. * @tolerance: link tolerance to be used by link
  367. * @net_plane: network plane (A,B,c..) this link belongs to
  368. * @mtu: mtu to be advertised by link
  369. * @priority: priority to be used by link
  370. * @window: send window to be used by link
  371. * @session: session to be used by link
  372. * @ownnode: identity of own node
  373. * @peer: node id of peer node
  374. * @peer_caps: bitmap describing peer node capabilities
  375. * @bc_sndlink: the namespace global link used for broadcast sending
  376. * @bc_rcvlink: the peer specific link used for broadcast reception
  377. * @inputq: queue to put messages ready for delivery
  378. * @namedq: queue to put binding table update messages ready for delivery
  379. * @link: return value, pointer to put the created link
  380. *
  381. * Returns true if link was created, otherwise false
  382. */
  383. bool tipc_link_create(struct net *net, char *if_name, int bearer_id,
  384. int tolerance, char net_plane, u32 mtu, int priority,
  385. int window, u32 session, u32 ownnode, u32 peer,
  386. u16 peer_caps,
  387. struct tipc_link *bc_sndlink,
  388. struct tipc_link *bc_rcvlink,
  389. struct sk_buff_head *inputq,
  390. struct sk_buff_head *namedq,
  391. struct tipc_link **link)
  392. {
  393. struct tipc_link *l;
  394. l = kzalloc(sizeof(*l), GFP_ATOMIC);
  395. if (!l)
  396. return false;
  397. *link = l;
  398. l->session = session;
  399. /* Note: peer i/f name is completed by reset/activate message */
  400. sprintf(l->name, "%u.%u.%u:%s-%u.%u.%u:unknown",
  401. tipc_zone(ownnode), tipc_cluster(ownnode), tipc_node(ownnode),
  402. if_name, tipc_zone(peer), tipc_cluster(peer), tipc_node(peer));
  403. strcpy(l->if_name, if_name);
  404. l->addr = peer;
  405. l->peer_caps = peer_caps;
  406. l->net = net;
  407. l->peer_session = ANY_SESSION;
  408. l->bearer_id = bearer_id;
  409. l->tolerance = tolerance;
  410. l->net_plane = net_plane;
  411. l->advertised_mtu = mtu;
  412. l->mtu = mtu;
  413. l->priority = priority;
  414. tipc_link_set_queue_limits(l, window);
  415. l->ackers = 1;
  416. l->bc_sndlink = bc_sndlink;
  417. l->bc_rcvlink = bc_rcvlink;
  418. l->inputq = inputq;
  419. l->namedq = namedq;
  420. l->state = LINK_RESETTING;
  421. __skb_queue_head_init(&l->transmq);
  422. __skb_queue_head_init(&l->backlogq);
  423. __skb_queue_head_init(&l->deferdq);
  424. skb_queue_head_init(&l->wakeupq);
  425. skb_queue_head_init(l->inputq);
  426. return true;
  427. }
  428. /**
  429. * tipc_link_bc_create - create new link to be used for broadcast
  430. * @n: pointer to associated node
  431. * @mtu: mtu to be used
  432. * @window: send window to be used
  433. * @inputq: queue to put messages ready for delivery
  434. * @namedq: queue to put binding table update messages ready for delivery
  435. * @link: return value, pointer to put the created link
  436. *
  437. * Returns true if link was created, otherwise false
  438. */
  439. bool tipc_link_bc_create(struct net *net, u32 ownnode, u32 peer,
  440. int mtu, int window, u16 peer_caps,
  441. struct sk_buff_head *inputq,
  442. struct sk_buff_head *namedq,
  443. struct tipc_link *bc_sndlink,
  444. struct tipc_link **link)
  445. {
  446. struct tipc_link *l;
  447. if (!tipc_link_create(net, "", MAX_BEARERS, 0, 'Z', mtu, 0, window,
  448. 0, ownnode, peer, peer_caps, bc_sndlink,
  449. NULL, inputq, namedq, link))
  450. return false;
  451. l = *link;
  452. strcpy(l->name, tipc_bclink_name);
  453. tipc_link_reset(l);
  454. l->state = LINK_RESET;
  455. l->ackers = 0;
  456. l->bc_rcvlink = l;
  457. /* Broadcast send link is always up */
  458. if (link_is_bc_sndlink(l))
  459. l->state = LINK_ESTABLISHED;
  460. /* Disable replicast if even a single peer doesn't support it */
  461. if (link_is_bc_rcvlink(l) && !(peer_caps & TIPC_BCAST_RCAST))
  462. tipc_bcast_disable_rcast(net);
  463. return true;
  464. }
  465. /**
  466. * tipc_link_fsm_evt - link finite state machine
  467. * @l: pointer to link
  468. * @evt: state machine event to be processed
  469. */
  470. int tipc_link_fsm_evt(struct tipc_link *l, int evt)
  471. {
  472. int rc = 0;
  473. switch (l->state) {
  474. case LINK_RESETTING:
  475. switch (evt) {
  476. case LINK_PEER_RESET_EVT:
  477. l->state = LINK_PEER_RESET;
  478. break;
  479. case LINK_RESET_EVT:
  480. l->state = LINK_RESET;
  481. break;
  482. case LINK_FAILURE_EVT:
  483. case LINK_FAILOVER_BEGIN_EVT:
  484. case LINK_ESTABLISH_EVT:
  485. case LINK_FAILOVER_END_EVT:
  486. case LINK_SYNCH_BEGIN_EVT:
  487. case LINK_SYNCH_END_EVT:
  488. default:
  489. goto illegal_evt;
  490. }
  491. break;
  492. case LINK_RESET:
  493. switch (evt) {
  494. case LINK_PEER_RESET_EVT:
  495. l->state = LINK_ESTABLISHING;
  496. break;
  497. case LINK_FAILOVER_BEGIN_EVT:
  498. l->state = LINK_FAILINGOVER;
  499. case LINK_FAILURE_EVT:
  500. case LINK_RESET_EVT:
  501. case LINK_ESTABLISH_EVT:
  502. case LINK_FAILOVER_END_EVT:
  503. break;
  504. case LINK_SYNCH_BEGIN_EVT:
  505. case LINK_SYNCH_END_EVT:
  506. default:
  507. goto illegal_evt;
  508. }
  509. break;
  510. case LINK_PEER_RESET:
  511. switch (evt) {
  512. case LINK_RESET_EVT:
  513. l->state = LINK_ESTABLISHING;
  514. break;
  515. case LINK_PEER_RESET_EVT:
  516. case LINK_ESTABLISH_EVT:
  517. case LINK_FAILURE_EVT:
  518. break;
  519. case LINK_SYNCH_BEGIN_EVT:
  520. case LINK_SYNCH_END_EVT:
  521. case LINK_FAILOVER_BEGIN_EVT:
  522. case LINK_FAILOVER_END_EVT:
  523. default:
  524. goto illegal_evt;
  525. }
  526. break;
  527. case LINK_FAILINGOVER:
  528. switch (evt) {
  529. case LINK_FAILOVER_END_EVT:
  530. l->state = LINK_RESET;
  531. break;
  532. case LINK_PEER_RESET_EVT:
  533. case LINK_RESET_EVT:
  534. case LINK_ESTABLISH_EVT:
  535. case LINK_FAILURE_EVT:
  536. break;
  537. case LINK_FAILOVER_BEGIN_EVT:
  538. case LINK_SYNCH_BEGIN_EVT:
  539. case LINK_SYNCH_END_EVT:
  540. default:
  541. goto illegal_evt;
  542. }
  543. break;
  544. case LINK_ESTABLISHING:
  545. switch (evt) {
  546. case LINK_ESTABLISH_EVT:
  547. l->state = LINK_ESTABLISHED;
  548. break;
  549. case LINK_FAILOVER_BEGIN_EVT:
  550. l->state = LINK_FAILINGOVER;
  551. break;
  552. case LINK_RESET_EVT:
  553. l->state = LINK_RESET;
  554. break;
  555. case LINK_FAILURE_EVT:
  556. case LINK_PEER_RESET_EVT:
  557. case LINK_SYNCH_BEGIN_EVT:
  558. case LINK_FAILOVER_END_EVT:
  559. break;
  560. case LINK_SYNCH_END_EVT:
  561. default:
  562. goto illegal_evt;
  563. }
  564. break;
  565. case LINK_ESTABLISHED:
  566. switch (evt) {
  567. case LINK_PEER_RESET_EVT:
  568. l->state = LINK_PEER_RESET;
  569. rc |= TIPC_LINK_DOWN_EVT;
  570. break;
  571. case LINK_FAILURE_EVT:
  572. l->state = LINK_RESETTING;
  573. rc |= TIPC_LINK_DOWN_EVT;
  574. break;
  575. case LINK_RESET_EVT:
  576. l->state = LINK_RESET;
  577. break;
  578. case LINK_ESTABLISH_EVT:
  579. case LINK_SYNCH_END_EVT:
  580. break;
  581. case LINK_SYNCH_BEGIN_EVT:
  582. l->state = LINK_SYNCHING;
  583. break;
  584. case LINK_FAILOVER_BEGIN_EVT:
  585. case LINK_FAILOVER_END_EVT:
  586. default:
  587. goto illegal_evt;
  588. }
  589. break;
  590. case LINK_SYNCHING:
  591. switch (evt) {
  592. case LINK_PEER_RESET_EVT:
  593. l->state = LINK_PEER_RESET;
  594. rc |= TIPC_LINK_DOWN_EVT;
  595. break;
  596. case LINK_FAILURE_EVT:
  597. l->state = LINK_RESETTING;
  598. rc |= TIPC_LINK_DOWN_EVT;
  599. break;
  600. case LINK_RESET_EVT:
  601. l->state = LINK_RESET;
  602. break;
  603. case LINK_ESTABLISH_EVT:
  604. case LINK_SYNCH_BEGIN_EVT:
  605. break;
  606. case LINK_SYNCH_END_EVT:
  607. l->state = LINK_ESTABLISHED;
  608. break;
  609. case LINK_FAILOVER_BEGIN_EVT:
  610. case LINK_FAILOVER_END_EVT:
  611. default:
  612. goto illegal_evt;
  613. }
  614. break;
  615. default:
  616. pr_err("Unknown FSM state %x in %s\n", l->state, l->name);
  617. }
  618. return rc;
  619. illegal_evt:
  620. pr_err("Illegal FSM event %x in state %x on link %s\n",
  621. evt, l->state, l->name);
  622. return rc;
  623. }
  624. /* link_profile_stats - update statistical profiling of traffic
  625. */
  626. static void link_profile_stats(struct tipc_link *l)
  627. {
  628. struct sk_buff *skb;
  629. struct tipc_msg *msg;
  630. int length;
  631. /* Update counters used in statistical profiling of send traffic */
  632. l->stats.accu_queue_sz += skb_queue_len(&l->transmq);
  633. l->stats.queue_sz_counts++;
  634. skb = skb_peek(&l->transmq);
  635. if (!skb)
  636. return;
  637. msg = buf_msg(skb);
  638. length = msg_size(msg);
  639. if (msg_user(msg) == MSG_FRAGMENTER) {
  640. if (msg_type(msg) != FIRST_FRAGMENT)
  641. return;
  642. length = msg_size(msg_get_wrapped(msg));
  643. }
  644. l->stats.msg_lengths_total += length;
  645. l->stats.msg_length_counts++;
  646. if (length <= 64)
  647. l->stats.msg_length_profile[0]++;
  648. else if (length <= 256)
  649. l->stats.msg_length_profile[1]++;
  650. else if (length <= 1024)
  651. l->stats.msg_length_profile[2]++;
  652. else if (length <= 4096)
  653. l->stats.msg_length_profile[3]++;
  654. else if (length <= 16384)
  655. l->stats.msg_length_profile[4]++;
  656. else if (length <= 32768)
  657. l->stats.msg_length_profile[5]++;
  658. else
  659. l->stats.msg_length_profile[6]++;
  660. }
  661. /* tipc_link_timeout - perform periodic task as instructed from node timeout
  662. */
  663. int tipc_link_timeout(struct tipc_link *l, struct sk_buff_head *xmitq)
  664. {
  665. int mtyp = 0;
  666. int rc = 0;
  667. bool state = false;
  668. bool probe = false;
  669. bool setup = false;
  670. u16 bc_snt = l->bc_sndlink->snd_nxt - 1;
  671. u16 bc_acked = l->bc_rcvlink->acked;
  672. struct tipc_mon_state *mstate = &l->mon_state;
  673. switch (l->state) {
  674. case LINK_ESTABLISHED:
  675. case LINK_SYNCHING:
  676. mtyp = STATE_MSG;
  677. link_profile_stats(l);
  678. tipc_mon_get_state(l->net, l->addr, mstate, l->bearer_id);
  679. if (mstate->reset || (l->silent_intv_cnt > l->abort_limit))
  680. return tipc_link_fsm_evt(l, LINK_FAILURE_EVT);
  681. state = bc_acked != bc_snt;
  682. state |= l->bc_rcvlink->rcv_unacked;
  683. state |= l->rcv_unacked;
  684. state |= !skb_queue_empty(&l->transmq);
  685. state |= !skb_queue_empty(&l->deferdq);
  686. probe = mstate->probing;
  687. probe |= l->silent_intv_cnt;
  688. if (probe || mstate->monitoring)
  689. l->silent_intv_cnt++;
  690. break;
  691. case LINK_RESET:
  692. setup = l->rst_cnt++ <= 4;
  693. setup |= !(l->rst_cnt % 16);
  694. mtyp = RESET_MSG;
  695. break;
  696. case LINK_ESTABLISHING:
  697. setup = true;
  698. mtyp = ACTIVATE_MSG;
  699. break;
  700. case LINK_PEER_RESET:
  701. case LINK_RESETTING:
  702. case LINK_FAILINGOVER:
  703. break;
  704. default:
  705. break;
  706. }
  707. if (state || probe || setup)
  708. tipc_link_build_proto_msg(l, mtyp, probe, 0, 0, 0, xmitq);
  709. return rc;
  710. }
  711. /**
  712. * link_schedule_user - schedule a message sender for wakeup after congestion
  713. * @l: congested link
  714. * @hdr: header of message that is being sent
  715. * Create pseudo msg to send back to user when congestion abates
  716. */
  717. static int link_schedule_user(struct tipc_link *l, struct tipc_msg *hdr)
  718. {
  719. u32 dnode = tipc_own_addr(l->net);
  720. u32 dport = msg_origport(hdr);
  721. struct sk_buff *skb;
  722. /* Create and schedule wakeup pseudo message */
  723. skb = tipc_msg_create(SOCK_WAKEUP, 0, INT_H_SIZE, 0,
  724. dnode, l->addr, dport, 0, 0);
  725. if (!skb)
  726. return -ENOBUFS;
  727. msg_set_dest_droppable(buf_msg(skb), true);
  728. TIPC_SKB_CB(skb)->chain_imp = msg_importance(hdr);
  729. skb_queue_tail(&l->wakeupq, skb);
  730. l->stats.link_congs++;
  731. return -ELINKCONG;
  732. }
  733. /**
  734. * link_prepare_wakeup - prepare users for wakeup after congestion
  735. * @l: congested link
  736. * Wake up a number of waiting users, as permitted by available space
  737. * in the send queue
  738. */
  739. void link_prepare_wakeup(struct tipc_link *l)
  740. {
  741. struct sk_buff *skb, *tmp;
  742. int imp, i = 0;
  743. skb_queue_walk_safe(&l->wakeupq, skb, tmp) {
  744. imp = TIPC_SKB_CB(skb)->chain_imp;
  745. if (l->backlog[imp].len < l->backlog[imp].limit) {
  746. skb_unlink(skb, &l->wakeupq);
  747. skb_queue_tail(l->inputq, skb);
  748. } else if (i++ > 10) {
  749. break;
  750. }
  751. }
  752. }
  753. void tipc_link_reset(struct tipc_link *l)
  754. {
  755. l->peer_session = ANY_SESSION;
  756. l->session++;
  757. l->mtu = l->advertised_mtu;
  758. __skb_queue_purge(&l->transmq);
  759. __skb_queue_purge(&l->deferdq);
  760. skb_queue_splice_init(&l->wakeupq, l->inputq);
  761. __skb_queue_purge(&l->backlogq);
  762. l->backlog[TIPC_LOW_IMPORTANCE].len = 0;
  763. l->backlog[TIPC_MEDIUM_IMPORTANCE].len = 0;
  764. l->backlog[TIPC_HIGH_IMPORTANCE].len = 0;
  765. l->backlog[TIPC_CRITICAL_IMPORTANCE].len = 0;
  766. l->backlog[TIPC_SYSTEM_IMPORTANCE].len = 0;
  767. kfree_skb(l->reasm_buf);
  768. kfree_skb(l->failover_reasm_skb);
  769. l->reasm_buf = NULL;
  770. l->failover_reasm_skb = NULL;
  771. l->rcv_unacked = 0;
  772. l->snd_nxt = 1;
  773. l->rcv_nxt = 1;
  774. l->acked = 0;
  775. l->silent_intv_cnt = 0;
  776. l->rst_cnt = 0;
  777. l->stale_count = 0;
  778. l->bc_peer_is_up = false;
  779. memset(&l->mon_state, 0, sizeof(l->mon_state));
  780. tipc_link_reset_stats(l);
  781. }
  782. /**
  783. * tipc_link_xmit(): enqueue buffer list according to queue situation
  784. * @link: link to use
  785. * @list: chain of buffers containing message
  786. * @xmitq: returned list of packets to be sent by caller
  787. *
  788. * Consumes the buffer chain.
  789. * Returns 0 if success, or errno: -ELINKCONG, -EMSGSIZE or -ENOBUFS
  790. * Messages at TIPC_SYSTEM_IMPORTANCE are always accepted
  791. */
  792. int tipc_link_xmit(struct tipc_link *l, struct sk_buff_head *list,
  793. struct sk_buff_head *xmitq)
  794. {
  795. struct tipc_msg *hdr = buf_msg(skb_peek(list));
  796. unsigned int maxwin = l->window;
  797. int imp = msg_importance(hdr);
  798. unsigned int mtu = l->mtu;
  799. u16 ack = l->rcv_nxt - 1;
  800. u16 seqno = l->snd_nxt;
  801. u16 bc_ack = l->bc_rcvlink->rcv_nxt - 1;
  802. struct sk_buff_head *transmq = &l->transmq;
  803. struct sk_buff_head *backlogq = &l->backlogq;
  804. struct sk_buff *skb, *_skb, *bskb;
  805. int pkt_cnt = skb_queue_len(list);
  806. int rc = 0;
  807. if (unlikely(msg_size(hdr) > mtu)) {
  808. skb_queue_purge(list);
  809. return -EMSGSIZE;
  810. }
  811. /* Allow oversubscription of one data msg per source at congestion */
  812. if (unlikely(l->backlog[imp].len >= l->backlog[imp].limit)) {
  813. if (imp == TIPC_SYSTEM_IMPORTANCE) {
  814. pr_warn("%s<%s>, link overflow", link_rst_msg, l->name);
  815. return -ENOBUFS;
  816. }
  817. rc = link_schedule_user(l, hdr);
  818. }
  819. if (pkt_cnt > 1) {
  820. l->stats.sent_fragmented++;
  821. l->stats.sent_fragments += pkt_cnt;
  822. }
  823. /* Prepare each packet for sending, and add to relevant queue: */
  824. while (skb_queue_len(list)) {
  825. skb = skb_peek(list);
  826. hdr = buf_msg(skb);
  827. msg_set_seqno(hdr, seqno);
  828. msg_set_ack(hdr, ack);
  829. msg_set_bcast_ack(hdr, bc_ack);
  830. if (likely(skb_queue_len(transmq) < maxwin)) {
  831. _skb = skb_clone(skb, GFP_ATOMIC);
  832. if (!_skb) {
  833. skb_queue_purge(list);
  834. return -ENOBUFS;
  835. }
  836. __skb_dequeue(list);
  837. __skb_queue_tail(transmq, skb);
  838. __skb_queue_tail(xmitq, _skb);
  839. TIPC_SKB_CB(skb)->ackers = l->ackers;
  840. l->rcv_unacked = 0;
  841. l->stats.sent_pkts++;
  842. seqno++;
  843. continue;
  844. }
  845. if (tipc_msg_bundle(skb_peek_tail(backlogq), hdr, mtu)) {
  846. kfree_skb(__skb_dequeue(list));
  847. l->stats.sent_bundled++;
  848. continue;
  849. }
  850. if (tipc_msg_make_bundle(&bskb, hdr, mtu, l->addr)) {
  851. kfree_skb(__skb_dequeue(list));
  852. __skb_queue_tail(backlogq, bskb);
  853. l->backlog[msg_importance(buf_msg(bskb))].len++;
  854. l->stats.sent_bundled++;
  855. l->stats.sent_bundles++;
  856. continue;
  857. }
  858. l->backlog[imp].len += skb_queue_len(list);
  859. skb_queue_splice_tail_init(list, backlogq);
  860. }
  861. l->snd_nxt = seqno;
  862. return rc;
  863. }
  864. void tipc_link_advance_backlog(struct tipc_link *l, struct sk_buff_head *xmitq)
  865. {
  866. struct sk_buff *skb, *_skb;
  867. struct tipc_msg *hdr;
  868. u16 seqno = l->snd_nxt;
  869. u16 ack = l->rcv_nxt - 1;
  870. u16 bc_ack = l->bc_rcvlink->rcv_nxt - 1;
  871. while (skb_queue_len(&l->transmq) < l->window) {
  872. skb = skb_peek(&l->backlogq);
  873. if (!skb)
  874. break;
  875. _skb = skb_clone(skb, GFP_ATOMIC);
  876. if (!_skb)
  877. break;
  878. __skb_dequeue(&l->backlogq);
  879. hdr = buf_msg(skb);
  880. l->backlog[msg_importance(hdr)].len--;
  881. __skb_queue_tail(&l->transmq, skb);
  882. __skb_queue_tail(xmitq, _skb);
  883. TIPC_SKB_CB(skb)->ackers = l->ackers;
  884. msg_set_seqno(hdr, seqno);
  885. msg_set_ack(hdr, ack);
  886. msg_set_bcast_ack(hdr, bc_ack);
  887. l->rcv_unacked = 0;
  888. l->stats.sent_pkts++;
  889. seqno++;
  890. }
  891. l->snd_nxt = seqno;
  892. }
  893. static void link_retransmit_failure(struct tipc_link *l, struct sk_buff *skb)
  894. {
  895. struct tipc_msg *hdr = buf_msg(skb);
  896. pr_warn("Retransmission failure on link <%s>\n", l->name);
  897. link_print(l, "State of link ");
  898. pr_info("Failed msg: usr %u, typ %u, len %u, err %u\n",
  899. msg_user(hdr), msg_type(hdr), msg_size(hdr), msg_errcode(hdr));
  900. pr_info("sqno %u, prev: %x, src: %x\n",
  901. msg_seqno(hdr), msg_prevnode(hdr), msg_orignode(hdr));
  902. }
  903. int tipc_link_retrans(struct tipc_link *l, struct tipc_link *nacker,
  904. u16 from, u16 to, struct sk_buff_head *xmitq)
  905. {
  906. struct sk_buff *_skb, *skb = skb_peek(&l->transmq);
  907. struct tipc_msg *hdr;
  908. u16 ack = l->rcv_nxt - 1;
  909. u16 bc_ack = l->bc_rcvlink->rcv_nxt - 1;
  910. if (!skb)
  911. return 0;
  912. /* Detect repeated retransmit failures on same packet */
  913. if (nacker->last_retransm != buf_seqno(skb)) {
  914. nacker->last_retransm = buf_seqno(skb);
  915. nacker->stale_count = 1;
  916. } else if (++nacker->stale_count > 100) {
  917. link_retransmit_failure(l, skb);
  918. nacker->stale_count = 0;
  919. if (link_is_bc_sndlink(l))
  920. return TIPC_LINK_DOWN_EVT;
  921. return tipc_link_fsm_evt(l, LINK_FAILURE_EVT);
  922. }
  923. /* Move forward to where retransmission should start */
  924. skb_queue_walk(&l->transmq, skb) {
  925. if (!less(buf_seqno(skb), from))
  926. break;
  927. }
  928. skb_queue_walk_from(&l->transmq, skb) {
  929. if (more(buf_seqno(skb), to))
  930. break;
  931. hdr = buf_msg(skb);
  932. _skb = __pskb_copy(skb, MIN_H_SIZE, GFP_ATOMIC);
  933. if (!_skb)
  934. return 0;
  935. hdr = buf_msg(_skb);
  936. msg_set_ack(hdr, ack);
  937. msg_set_bcast_ack(hdr, bc_ack);
  938. _skb->priority = TC_PRIO_CONTROL;
  939. __skb_queue_tail(xmitq, _skb);
  940. l->stats.retransmitted++;
  941. }
  942. return 0;
  943. }
  944. /* tipc_data_input - deliver data and name distr msgs to upper layer
  945. *
  946. * Consumes buffer if message is of right type
  947. * Node lock must be held
  948. */
  949. static bool tipc_data_input(struct tipc_link *l, struct sk_buff *skb,
  950. struct sk_buff_head *inputq)
  951. {
  952. struct tipc_msg *hdr = buf_msg(skb);
  953. switch (msg_user(hdr)) {
  954. case TIPC_LOW_IMPORTANCE:
  955. case TIPC_MEDIUM_IMPORTANCE:
  956. case TIPC_HIGH_IMPORTANCE:
  957. case TIPC_CRITICAL_IMPORTANCE:
  958. if (unlikely(msg_mcast(hdr))) {
  959. skb_queue_tail(l->bc_rcvlink->inputq, skb);
  960. return true;
  961. }
  962. case CONN_MANAGER:
  963. case GROUP_PROTOCOL:
  964. skb_queue_tail(inputq, skb);
  965. return true;
  966. case NAME_DISTRIBUTOR:
  967. l->bc_rcvlink->state = LINK_ESTABLISHED;
  968. skb_queue_tail(l->namedq, skb);
  969. return true;
  970. case MSG_BUNDLER:
  971. case TUNNEL_PROTOCOL:
  972. case MSG_FRAGMENTER:
  973. case BCAST_PROTOCOL:
  974. return false;
  975. default:
  976. pr_warn("Dropping received illegal msg type\n");
  977. kfree_skb(skb);
  978. return false;
  979. };
  980. }
  981. /* tipc_link_input - process packet that has passed link protocol check
  982. *
  983. * Consumes buffer
  984. */
  985. static int tipc_link_input(struct tipc_link *l, struct sk_buff *skb,
  986. struct sk_buff_head *inputq)
  987. {
  988. struct tipc_msg *hdr = buf_msg(skb);
  989. struct sk_buff **reasm_skb = &l->reasm_buf;
  990. struct sk_buff *iskb;
  991. struct sk_buff_head tmpq;
  992. int usr = msg_user(hdr);
  993. int rc = 0;
  994. int pos = 0;
  995. int ipos = 0;
  996. if (unlikely(usr == TUNNEL_PROTOCOL)) {
  997. if (msg_type(hdr) == SYNCH_MSG) {
  998. __skb_queue_purge(&l->deferdq);
  999. goto drop;
  1000. }
  1001. if (!tipc_msg_extract(skb, &iskb, &ipos))
  1002. return rc;
  1003. kfree_skb(skb);
  1004. skb = iskb;
  1005. hdr = buf_msg(skb);
  1006. if (less(msg_seqno(hdr), l->drop_point))
  1007. goto drop;
  1008. if (tipc_data_input(l, skb, inputq))
  1009. return rc;
  1010. usr = msg_user(hdr);
  1011. reasm_skb = &l->failover_reasm_skb;
  1012. }
  1013. if (usr == MSG_BUNDLER) {
  1014. skb_queue_head_init(&tmpq);
  1015. l->stats.recv_bundles++;
  1016. l->stats.recv_bundled += msg_msgcnt(hdr);
  1017. while (tipc_msg_extract(skb, &iskb, &pos))
  1018. tipc_data_input(l, iskb, &tmpq);
  1019. tipc_skb_queue_splice_tail(&tmpq, inputq);
  1020. return 0;
  1021. } else if (usr == MSG_FRAGMENTER) {
  1022. l->stats.recv_fragments++;
  1023. if (tipc_buf_append(reasm_skb, &skb)) {
  1024. l->stats.recv_fragmented++;
  1025. tipc_data_input(l, skb, inputq);
  1026. } else if (!*reasm_skb && !link_is_bc_rcvlink(l)) {
  1027. pr_warn_ratelimited("Unable to build fragment list\n");
  1028. return tipc_link_fsm_evt(l, LINK_FAILURE_EVT);
  1029. }
  1030. return 0;
  1031. } else if (usr == BCAST_PROTOCOL) {
  1032. tipc_bcast_lock(l->net);
  1033. tipc_link_bc_init_rcv(l->bc_rcvlink, hdr);
  1034. tipc_bcast_unlock(l->net);
  1035. }
  1036. drop:
  1037. kfree_skb(skb);
  1038. return 0;
  1039. }
  1040. static bool tipc_link_release_pkts(struct tipc_link *l, u16 acked)
  1041. {
  1042. bool released = false;
  1043. struct sk_buff *skb, *tmp;
  1044. skb_queue_walk_safe(&l->transmq, skb, tmp) {
  1045. if (more(buf_seqno(skb), acked))
  1046. break;
  1047. __skb_unlink(skb, &l->transmq);
  1048. kfree_skb(skb);
  1049. released = true;
  1050. }
  1051. return released;
  1052. }
  1053. /* tipc_link_build_state_msg: prepare link state message for transmission
  1054. *
  1055. * Note that sending of broadcast ack is coordinated among nodes, to reduce
  1056. * risk of ack storms towards the sender
  1057. */
  1058. int tipc_link_build_state_msg(struct tipc_link *l, struct sk_buff_head *xmitq)
  1059. {
  1060. if (!l)
  1061. return 0;
  1062. /* Broadcast ACK must be sent via a unicast link => defer to caller */
  1063. if (link_is_bc_rcvlink(l)) {
  1064. if (((l->rcv_nxt ^ tipc_own_addr(l->net)) & 0xf) != 0xf)
  1065. return 0;
  1066. l->rcv_unacked = 0;
  1067. /* Use snd_nxt to store peer's snd_nxt in broadcast rcv link */
  1068. l->snd_nxt = l->rcv_nxt;
  1069. return TIPC_LINK_SND_STATE;
  1070. }
  1071. /* Unicast ACK */
  1072. l->rcv_unacked = 0;
  1073. l->stats.sent_acks++;
  1074. tipc_link_build_proto_msg(l, STATE_MSG, 0, 0, 0, 0, xmitq);
  1075. return 0;
  1076. }
  1077. /* tipc_link_build_reset_msg: prepare link RESET or ACTIVATE message
  1078. */
  1079. void tipc_link_build_reset_msg(struct tipc_link *l, struct sk_buff_head *xmitq)
  1080. {
  1081. int mtyp = RESET_MSG;
  1082. struct sk_buff *skb;
  1083. if (l->state == LINK_ESTABLISHING)
  1084. mtyp = ACTIVATE_MSG;
  1085. tipc_link_build_proto_msg(l, mtyp, 0, 0, 0, 0, xmitq);
  1086. /* Inform peer that this endpoint is going down if applicable */
  1087. skb = skb_peek_tail(xmitq);
  1088. if (skb && (l->state == LINK_RESET))
  1089. msg_set_peer_stopping(buf_msg(skb), 1);
  1090. }
  1091. /* tipc_link_build_nack_msg: prepare link nack message for transmission
  1092. * Note that sending of broadcast NACK is coordinated among nodes, to
  1093. * reduce the risk of NACK storms towards the sender
  1094. */
  1095. static int tipc_link_build_nack_msg(struct tipc_link *l,
  1096. struct sk_buff_head *xmitq)
  1097. {
  1098. u32 def_cnt = ++l->stats.deferred_recv;
  1099. int match1, match2;
  1100. if (link_is_bc_rcvlink(l)) {
  1101. match1 = def_cnt & 0xf;
  1102. match2 = tipc_own_addr(l->net) & 0xf;
  1103. if (match1 == match2)
  1104. return TIPC_LINK_SND_STATE;
  1105. return 0;
  1106. }
  1107. if ((skb_queue_len(&l->deferdq) == 1) || !(def_cnt % TIPC_NACK_INTV))
  1108. tipc_link_build_proto_msg(l, STATE_MSG, 0, 0, 0, 0, xmitq);
  1109. return 0;
  1110. }
  1111. /* tipc_link_rcv - process TIPC packets/messages arriving from off-node
  1112. * @l: the link that should handle the message
  1113. * @skb: TIPC packet
  1114. * @xmitq: queue to place packets to be sent after this call
  1115. */
  1116. int tipc_link_rcv(struct tipc_link *l, struct sk_buff *skb,
  1117. struct sk_buff_head *xmitq)
  1118. {
  1119. struct sk_buff_head *defq = &l->deferdq;
  1120. struct tipc_msg *hdr;
  1121. u16 seqno, rcv_nxt, win_lim;
  1122. int rc = 0;
  1123. do {
  1124. hdr = buf_msg(skb);
  1125. seqno = msg_seqno(hdr);
  1126. rcv_nxt = l->rcv_nxt;
  1127. win_lim = rcv_nxt + TIPC_MAX_LINK_WIN;
  1128. /* Verify and update link state */
  1129. if (unlikely(msg_user(hdr) == LINK_PROTOCOL))
  1130. return tipc_link_proto_rcv(l, skb, xmitq);
  1131. if (unlikely(!link_is_up(l))) {
  1132. if (l->state == LINK_ESTABLISHING)
  1133. rc = TIPC_LINK_UP_EVT;
  1134. goto drop;
  1135. }
  1136. /* Don't send probe at next timeout expiration */
  1137. l->silent_intv_cnt = 0;
  1138. /* Drop if outside receive window */
  1139. if (unlikely(less(seqno, rcv_nxt) || more(seqno, win_lim))) {
  1140. l->stats.duplicates++;
  1141. goto drop;
  1142. }
  1143. /* Forward queues and wake up waiting users */
  1144. if (likely(tipc_link_release_pkts(l, msg_ack(hdr)))) {
  1145. tipc_link_advance_backlog(l, xmitq);
  1146. if (unlikely(!skb_queue_empty(&l->wakeupq)))
  1147. link_prepare_wakeup(l);
  1148. }
  1149. /* Defer delivery if sequence gap */
  1150. if (unlikely(seqno != rcv_nxt)) {
  1151. __tipc_skb_queue_sorted(defq, seqno, skb);
  1152. rc |= tipc_link_build_nack_msg(l, xmitq);
  1153. break;
  1154. }
  1155. /* Deliver packet */
  1156. l->rcv_nxt++;
  1157. l->stats.recv_pkts++;
  1158. if (!tipc_data_input(l, skb, l->inputq))
  1159. rc |= tipc_link_input(l, skb, l->inputq);
  1160. if (unlikely(++l->rcv_unacked >= TIPC_MIN_LINK_WIN))
  1161. rc |= tipc_link_build_state_msg(l, xmitq);
  1162. if (unlikely(rc & ~TIPC_LINK_SND_STATE))
  1163. break;
  1164. } while ((skb = __skb_dequeue(defq)));
  1165. return rc;
  1166. drop:
  1167. kfree_skb(skb);
  1168. return rc;
  1169. }
  1170. static void tipc_link_build_proto_msg(struct tipc_link *l, int mtyp, bool probe,
  1171. u16 rcvgap, int tolerance, int priority,
  1172. struct sk_buff_head *xmitq)
  1173. {
  1174. struct tipc_link *bcl = l->bc_rcvlink;
  1175. struct sk_buff *skb;
  1176. struct tipc_msg *hdr;
  1177. struct sk_buff_head *dfq = &l->deferdq;
  1178. bool node_up = link_is_up(bcl);
  1179. struct tipc_mon_state *mstate = &l->mon_state;
  1180. int dlen = 0;
  1181. void *data;
  1182. /* Don't send protocol message during reset or link failover */
  1183. if (tipc_link_is_blocked(l))
  1184. return;
  1185. if (!tipc_link_is_up(l) && (mtyp == STATE_MSG))
  1186. return;
  1187. if (!skb_queue_empty(dfq))
  1188. rcvgap = buf_seqno(skb_peek(dfq)) - l->rcv_nxt;
  1189. skb = tipc_msg_create(LINK_PROTOCOL, mtyp, INT_H_SIZE,
  1190. tipc_max_domain_size, l->addr,
  1191. tipc_own_addr(l->net), 0, 0, 0);
  1192. if (!skb)
  1193. return;
  1194. hdr = buf_msg(skb);
  1195. data = msg_data(hdr);
  1196. msg_set_session(hdr, l->session);
  1197. msg_set_bearer_id(hdr, l->bearer_id);
  1198. msg_set_net_plane(hdr, l->net_plane);
  1199. msg_set_next_sent(hdr, l->snd_nxt);
  1200. msg_set_ack(hdr, l->rcv_nxt - 1);
  1201. msg_set_bcast_ack(hdr, bcl->rcv_nxt - 1);
  1202. msg_set_bc_ack_invalid(hdr, !node_up);
  1203. msg_set_last_bcast(hdr, l->bc_sndlink->snd_nxt - 1);
  1204. msg_set_link_tolerance(hdr, tolerance);
  1205. msg_set_linkprio(hdr, priority);
  1206. msg_set_redundant_link(hdr, node_up);
  1207. msg_set_seq_gap(hdr, 0);
  1208. msg_set_seqno(hdr, l->snd_nxt + U16_MAX / 2);
  1209. if (mtyp == STATE_MSG) {
  1210. msg_set_seq_gap(hdr, rcvgap);
  1211. msg_set_bc_gap(hdr, link_bc_rcv_gap(bcl));
  1212. msg_set_probe(hdr, probe);
  1213. tipc_mon_prep(l->net, data, &dlen, mstate, l->bearer_id);
  1214. msg_set_size(hdr, INT_H_SIZE + dlen);
  1215. skb_trim(skb, INT_H_SIZE + dlen);
  1216. l->stats.sent_states++;
  1217. l->rcv_unacked = 0;
  1218. } else {
  1219. /* RESET_MSG or ACTIVATE_MSG */
  1220. msg_set_max_pkt(hdr, l->advertised_mtu);
  1221. strcpy(data, l->if_name);
  1222. msg_set_size(hdr, INT_H_SIZE + TIPC_MAX_IF_NAME);
  1223. skb_trim(skb, INT_H_SIZE + TIPC_MAX_IF_NAME);
  1224. }
  1225. if (probe)
  1226. l->stats.sent_probes++;
  1227. if (rcvgap)
  1228. l->stats.sent_nacks++;
  1229. skb->priority = TC_PRIO_CONTROL;
  1230. __skb_queue_tail(xmitq, skb);
  1231. }
  1232. /* tipc_link_tnl_prepare(): prepare and return a list of tunnel packets
  1233. * with contents of the link's transmit and backlog queues.
  1234. */
  1235. void tipc_link_tnl_prepare(struct tipc_link *l, struct tipc_link *tnl,
  1236. int mtyp, struct sk_buff_head *xmitq)
  1237. {
  1238. struct sk_buff *skb, *tnlskb;
  1239. struct tipc_msg *hdr, tnlhdr;
  1240. struct sk_buff_head *queue = &l->transmq;
  1241. struct sk_buff_head tmpxq, tnlq;
  1242. u16 pktlen, pktcnt, seqno = l->snd_nxt;
  1243. if (!tnl)
  1244. return;
  1245. skb_queue_head_init(&tnlq);
  1246. skb_queue_head_init(&tmpxq);
  1247. /* At least one packet required for safe algorithm => add dummy */
  1248. skb = tipc_msg_create(TIPC_LOW_IMPORTANCE, TIPC_DIRECT_MSG,
  1249. BASIC_H_SIZE, 0, l->addr, tipc_own_addr(l->net),
  1250. 0, 0, TIPC_ERR_NO_PORT);
  1251. if (!skb) {
  1252. pr_warn("%sunable to create tunnel packet\n", link_co_err);
  1253. return;
  1254. }
  1255. skb_queue_tail(&tnlq, skb);
  1256. tipc_link_xmit(l, &tnlq, &tmpxq);
  1257. __skb_queue_purge(&tmpxq);
  1258. /* Initialize reusable tunnel packet header */
  1259. tipc_msg_init(tipc_own_addr(l->net), &tnlhdr, TUNNEL_PROTOCOL,
  1260. mtyp, INT_H_SIZE, l->addr);
  1261. pktcnt = skb_queue_len(&l->transmq) + skb_queue_len(&l->backlogq);
  1262. msg_set_msgcnt(&tnlhdr, pktcnt);
  1263. msg_set_bearer_id(&tnlhdr, l->peer_bearer_id);
  1264. tnl:
  1265. /* Wrap each packet into a tunnel packet */
  1266. skb_queue_walk(queue, skb) {
  1267. hdr = buf_msg(skb);
  1268. if (queue == &l->backlogq)
  1269. msg_set_seqno(hdr, seqno++);
  1270. pktlen = msg_size(hdr);
  1271. msg_set_size(&tnlhdr, pktlen + INT_H_SIZE);
  1272. tnlskb = tipc_buf_acquire(pktlen + INT_H_SIZE, GFP_ATOMIC);
  1273. if (!tnlskb) {
  1274. pr_warn("%sunable to send packet\n", link_co_err);
  1275. return;
  1276. }
  1277. skb_copy_to_linear_data(tnlskb, &tnlhdr, INT_H_SIZE);
  1278. skb_copy_to_linear_data_offset(tnlskb, INT_H_SIZE, hdr, pktlen);
  1279. __skb_queue_tail(&tnlq, tnlskb);
  1280. }
  1281. if (queue != &l->backlogq) {
  1282. queue = &l->backlogq;
  1283. goto tnl;
  1284. }
  1285. tipc_link_xmit(tnl, &tnlq, xmitq);
  1286. if (mtyp == FAILOVER_MSG) {
  1287. tnl->drop_point = l->rcv_nxt;
  1288. tnl->failover_reasm_skb = l->reasm_buf;
  1289. l->reasm_buf = NULL;
  1290. }
  1291. }
  1292. /* tipc_link_proto_rcv(): receive link level protocol message :
  1293. * Note that network plane id propagates through the network, and may
  1294. * change at any time. The node with lowest numerical id determines
  1295. * network plane
  1296. */
  1297. static int tipc_link_proto_rcv(struct tipc_link *l, struct sk_buff *skb,
  1298. struct sk_buff_head *xmitq)
  1299. {
  1300. struct tipc_msg *hdr = buf_msg(skb);
  1301. u16 rcvgap = 0;
  1302. u16 ack = msg_ack(hdr);
  1303. u16 gap = msg_seq_gap(hdr);
  1304. u16 peers_snd_nxt = msg_next_sent(hdr);
  1305. u16 peers_tol = msg_link_tolerance(hdr);
  1306. u16 peers_prio = msg_linkprio(hdr);
  1307. u16 rcv_nxt = l->rcv_nxt;
  1308. u16 dlen = msg_data_sz(hdr);
  1309. int mtyp = msg_type(hdr);
  1310. void *data;
  1311. char *if_name;
  1312. int rc = 0;
  1313. if (tipc_link_is_blocked(l) || !xmitq)
  1314. goto exit;
  1315. if (tipc_own_addr(l->net) > msg_prevnode(hdr))
  1316. l->net_plane = msg_net_plane(hdr);
  1317. skb_linearize(skb);
  1318. hdr = buf_msg(skb);
  1319. data = msg_data(hdr);
  1320. switch (mtyp) {
  1321. case RESET_MSG:
  1322. /* Ignore duplicate RESET with old session number */
  1323. if ((less_eq(msg_session(hdr), l->peer_session)) &&
  1324. (l->peer_session != ANY_SESSION))
  1325. break;
  1326. /* fall thru' */
  1327. case ACTIVATE_MSG:
  1328. /* Complete own link name with peer's interface name */
  1329. if_name = strrchr(l->name, ':') + 1;
  1330. if (sizeof(l->name) - (if_name - l->name) <= TIPC_MAX_IF_NAME)
  1331. break;
  1332. if (msg_data_sz(hdr) < TIPC_MAX_IF_NAME)
  1333. break;
  1334. strncpy(if_name, data, TIPC_MAX_IF_NAME);
  1335. /* Update own tolerance if peer indicates a non-zero value */
  1336. if (in_range(peers_tol, TIPC_MIN_LINK_TOL, TIPC_MAX_LINK_TOL))
  1337. l->tolerance = peers_tol;
  1338. /* Update own priority if peer's priority is higher */
  1339. if (in_range(peers_prio, l->priority + 1, TIPC_MAX_LINK_PRI))
  1340. l->priority = peers_prio;
  1341. /* ACTIVATE_MSG serves as PEER_RESET if link is already down */
  1342. if (msg_peer_stopping(hdr))
  1343. rc = tipc_link_fsm_evt(l, LINK_FAILURE_EVT);
  1344. else if ((mtyp == RESET_MSG) || !link_is_up(l))
  1345. rc = tipc_link_fsm_evt(l, LINK_PEER_RESET_EVT);
  1346. /* ACTIVATE_MSG takes up link if it was already locally reset */
  1347. if ((mtyp == ACTIVATE_MSG) && (l->state == LINK_ESTABLISHING))
  1348. rc = TIPC_LINK_UP_EVT;
  1349. l->peer_session = msg_session(hdr);
  1350. l->peer_bearer_id = msg_bearer_id(hdr);
  1351. if (l->mtu > msg_max_pkt(hdr))
  1352. l->mtu = msg_max_pkt(hdr);
  1353. break;
  1354. case STATE_MSG:
  1355. /* Update own tolerance if peer indicates a non-zero value */
  1356. if (in_range(peers_tol, TIPC_MIN_LINK_TOL, TIPC_MAX_LINK_TOL))
  1357. l->tolerance = peers_tol;
  1358. /* Update own prio if peer indicates a different value */
  1359. if ((peers_prio != l->priority) &&
  1360. in_range(peers_prio, 1, TIPC_MAX_LINK_PRI)) {
  1361. l->priority = peers_prio;
  1362. rc = tipc_link_fsm_evt(l, LINK_FAILURE_EVT);
  1363. }
  1364. l->silent_intv_cnt = 0;
  1365. l->stats.recv_states++;
  1366. if (msg_probe(hdr))
  1367. l->stats.recv_probes++;
  1368. if (!link_is_up(l)) {
  1369. if (l->state == LINK_ESTABLISHING)
  1370. rc = TIPC_LINK_UP_EVT;
  1371. break;
  1372. }
  1373. tipc_mon_rcv(l->net, data, dlen, l->addr,
  1374. &l->mon_state, l->bearer_id);
  1375. /* Send NACK if peer has sent pkts we haven't received yet */
  1376. if (more(peers_snd_nxt, rcv_nxt) && !tipc_link_is_synching(l))
  1377. rcvgap = peers_snd_nxt - l->rcv_nxt;
  1378. if (rcvgap || (msg_probe(hdr)))
  1379. tipc_link_build_proto_msg(l, STATE_MSG, 0, rcvgap,
  1380. 0, 0, xmitq);
  1381. tipc_link_release_pkts(l, ack);
  1382. /* If NACK, retransmit will now start at right position */
  1383. if (gap) {
  1384. rc = tipc_link_retrans(l, l, ack + 1, ack + gap, xmitq);
  1385. l->stats.recv_nacks++;
  1386. }
  1387. tipc_link_advance_backlog(l, xmitq);
  1388. if (unlikely(!skb_queue_empty(&l->wakeupq)))
  1389. link_prepare_wakeup(l);
  1390. }
  1391. exit:
  1392. kfree_skb(skb);
  1393. return rc;
  1394. }
  1395. /* tipc_link_build_bc_proto_msg() - create broadcast protocol message
  1396. */
  1397. static bool tipc_link_build_bc_proto_msg(struct tipc_link *l, bool bcast,
  1398. u16 peers_snd_nxt,
  1399. struct sk_buff_head *xmitq)
  1400. {
  1401. struct sk_buff *skb;
  1402. struct tipc_msg *hdr;
  1403. struct sk_buff *dfrd_skb = skb_peek(&l->deferdq);
  1404. u16 ack = l->rcv_nxt - 1;
  1405. u16 gap_to = peers_snd_nxt - 1;
  1406. skb = tipc_msg_create(BCAST_PROTOCOL, STATE_MSG, INT_H_SIZE,
  1407. 0, l->addr, tipc_own_addr(l->net), 0, 0, 0);
  1408. if (!skb)
  1409. return false;
  1410. hdr = buf_msg(skb);
  1411. msg_set_last_bcast(hdr, l->bc_sndlink->snd_nxt - 1);
  1412. msg_set_bcast_ack(hdr, ack);
  1413. msg_set_bcgap_after(hdr, ack);
  1414. if (dfrd_skb)
  1415. gap_to = buf_seqno(dfrd_skb) - 1;
  1416. msg_set_bcgap_to(hdr, gap_to);
  1417. msg_set_non_seq(hdr, bcast);
  1418. __skb_queue_tail(xmitq, skb);
  1419. return true;
  1420. }
  1421. /* tipc_link_build_bc_init_msg() - synchronize broadcast link endpoints.
  1422. *
  1423. * Give a newly added peer node the sequence number where it should
  1424. * start receiving and acking broadcast packets.
  1425. */
  1426. static void tipc_link_build_bc_init_msg(struct tipc_link *l,
  1427. struct sk_buff_head *xmitq)
  1428. {
  1429. struct sk_buff_head list;
  1430. __skb_queue_head_init(&list);
  1431. if (!tipc_link_build_bc_proto_msg(l->bc_rcvlink, false, 0, &list))
  1432. return;
  1433. msg_set_bc_ack_invalid(buf_msg(skb_peek(&list)), true);
  1434. tipc_link_xmit(l, &list, xmitq);
  1435. }
  1436. /* tipc_link_bc_init_rcv - receive initial broadcast synch data from peer
  1437. */
  1438. void tipc_link_bc_init_rcv(struct tipc_link *l, struct tipc_msg *hdr)
  1439. {
  1440. int mtyp = msg_type(hdr);
  1441. u16 peers_snd_nxt = msg_bc_snd_nxt(hdr);
  1442. if (link_is_up(l))
  1443. return;
  1444. if (msg_user(hdr) == BCAST_PROTOCOL) {
  1445. l->rcv_nxt = peers_snd_nxt;
  1446. l->state = LINK_ESTABLISHED;
  1447. return;
  1448. }
  1449. if (l->peer_caps & TIPC_BCAST_SYNCH)
  1450. return;
  1451. if (msg_peer_node_is_up(hdr))
  1452. return;
  1453. /* Compatibility: accept older, less safe initial synch data */
  1454. if ((mtyp == RESET_MSG) || (mtyp == ACTIVATE_MSG))
  1455. l->rcv_nxt = peers_snd_nxt;
  1456. }
  1457. /* link_bc_retr eval()- check if the indicated range can be retransmitted now
  1458. * - Adjust permitted range if there is overlap with previous retransmission
  1459. */
  1460. static bool link_bc_retr_eval(struct tipc_link *l, u16 *from, u16 *to)
  1461. {
  1462. unsigned long elapsed = jiffies_to_msecs(jiffies - l->prev_retr);
  1463. if (less(*to, *from))
  1464. return false;
  1465. /* New retransmission request */
  1466. if ((elapsed > TIPC_BC_RETR_LIMIT) ||
  1467. less(*to, l->prev_from) || more(*from, l->prev_to)) {
  1468. l->prev_from = *from;
  1469. l->prev_to = *to;
  1470. l->prev_retr = jiffies;
  1471. return true;
  1472. }
  1473. /* Inside range of previous retransmit */
  1474. if (!less(*from, l->prev_from) && !more(*to, l->prev_to))
  1475. return false;
  1476. /* Fully or partially outside previous range => exclude overlap */
  1477. if (less(*from, l->prev_from)) {
  1478. *to = l->prev_from - 1;
  1479. l->prev_from = *from;
  1480. }
  1481. if (more(*to, l->prev_to)) {
  1482. *from = l->prev_to + 1;
  1483. l->prev_to = *to;
  1484. }
  1485. l->prev_retr = jiffies;
  1486. return true;
  1487. }
  1488. /* tipc_link_bc_sync_rcv - update rcv link according to peer's send state
  1489. */
  1490. int tipc_link_bc_sync_rcv(struct tipc_link *l, struct tipc_msg *hdr,
  1491. struct sk_buff_head *xmitq)
  1492. {
  1493. struct tipc_link *snd_l = l->bc_sndlink;
  1494. u16 peers_snd_nxt = msg_bc_snd_nxt(hdr);
  1495. u16 from = msg_bcast_ack(hdr) + 1;
  1496. u16 to = from + msg_bc_gap(hdr) - 1;
  1497. int rc = 0;
  1498. if (!link_is_up(l))
  1499. return rc;
  1500. if (!msg_peer_node_is_up(hdr))
  1501. return rc;
  1502. /* Open when peer ackowledges our bcast init msg (pkt #1) */
  1503. if (msg_ack(hdr))
  1504. l->bc_peer_is_up = true;
  1505. if (!l->bc_peer_is_up)
  1506. return rc;
  1507. l->stats.recv_nacks++;
  1508. /* Ignore if peers_snd_nxt goes beyond receive window */
  1509. if (more(peers_snd_nxt, l->rcv_nxt + l->window))
  1510. return rc;
  1511. if (link_bc_retr_eval(snd_l, &from, &to))
  1512. rc = tipc_link_retrans(snd_l, l, from, to, xmitq);
  1513. l->snd_nxt = peers_snd_nxt;
  1514. if (link_bc_rcv_gap(l))
  1515. rc |= TIPC_LINK_SND_STATE;
  1516. /* Return now if sender supports nack via STATE messages */
  1517. if (l->peer_caps & TIPC_BCAST_STATE_NACK)
  1518. return rc;
  1519. /* Otherwise, be backwards compatible */
  1520. if (!more(peers_snd_nxt, l->rcv_nxt)) {
  1521. l->nack_state = BC_NACK_SND_CONDITIONAL;
  1522. return 0;
  1523. }
  1524. /* Don't NACK if one was recently sent or peeked */
  1525. if (l->nack_state == BC_NACK_SND_SUPPRESS) {
  1526. l->nack_state = BC_NACK_SND_UNCONDITIONAL;
  1527. return 0;
  1528. }
  1529. /* Conditionally delay NACK sending until next synch rcv */
  1530. if (l->nack_state == BC_NACK_SND_CONDITIONAL) {
  1531. l->nack_state = BC_NACK_SND_UNCONDITIONAL;
  1532. if ((peers_snd_nxt - l->rcv_nxt) < TIPC_MIN_LINK_WIN)
  1533. return 0;
  1534. }
  1535. /* Send NACK now but suppress next one */
  1536. tipc_link_build_bc_proto_msg(l, true, peers_snd_nxt, xmitq);
  1537. l->nack_state = BC_NACK_SND_SUPPRESS;
  1538. return 0;
  1539. }
  1540. void tipc_link_bc_ack_rcv(struct tipc_link *l, u16 acked,
  1541. struct sk_buff_head *xmitq)
  1542. {
  1543. struct sk_buff *skb, *tmp;
  1544. struct tipc_link *snd_l = l->bc_sndlink;
  1545. if (!link_is_up(l) || !l->bc_peer_is_up)
  1546. return;
  1547. if (!more(acked, l->acked))
  1548. return;
  1549. /* Skip over packets peer has already acked */
  1550. skb_queue_walk(&snd_l->transmq, skb) {
  1551. if (more(buf_seqno(skb), l->acked))
  1552. break;
  1553. }
  1554. /* Update/release the packets peer is acking now */
  1555. skb_queue_walk_from_safe(&snd_l->transmq, skb, tmp) {
  1556. if (more(buf_seqno(skb), acked))
  1557. break;
  1558. if (!--TIPC_SKB_CB(skb)->ackers) {
  1559. __skb_unlink(skb, &snd_l->transmq);
  1560. kfree_skb(skb);
  1561. }
  1562. }
  1563. l->acked = acked;
  1564. tipc_link_advance_backlog(snd_l, xmitq);
  1565. if (unlikely(!skb_queue_empty(&snd_l->wakeupq)))
  1566. link_prepare_wakeup(snd_l);
  1567. }
  1568. /* tipc_link_bc_nack_rcv(): receive broadcast nack message
  1569. * This function is here for backwards compatibility, since
  1570. * no BCAST_PROTOCOL/STATE messages occur from TIPC v2.5.
  1571. */
  1572. int tipc_link_bc_nack_rcv(struct tipc_link *l, struct sk_buff *skb,
  1573. struct sk_buff_head *xmitq)
  1574. {
  1575. struct tipc_msg *hdr = buf_msg(skb);
  1576. u32 dnode = msg_destnode(hdr);
  1577. int mtyp = msg_type(hdr);
  1578. u16 acked = msg_bcast_ack(hdr);
  1579. u16 from = acked + 1;
  1580. u16 to = msg_bcgap_to(hdr);
  1581. u16 peers_snd_nxt = to + 1;
  1582. int rc = 0;
  1583. kfree_skb(skb);
  1584. if (!tipc_link_is_up(l) || !l->bc_peer_is_up)
  1585. return 0;
  1586. if (mtyp != STATE_MSG)
  1587. return 0;
  1588. if (dnode == tipc_own_addr(l->net)) {
  1589. tipc_link_bc_ack_rcv(l, acked, xmitq);
  1590. rc = tipc_link_retrans(l->bc_sndlink, l, from, to, xmitq);
  1591. l->stats.recv_nacks++;
  1592. return rc;
  1593. }
  1594. /* Msg for other node => suppress own NACK at next sync if applicable */
  1595. if (more(peers_snd_nxt, l->rcv_nxt) && !less(l->rcv_nxt, from))
  1596. l->nack_state = BC_NACK_SND_SUPPRESS;
  1597. return 0;
  1598. }
  1599. void tipc_link_set_queue_limits(struct tipc_link *l, u32 win)
  1600. {
  1601. int max_bulk = TIPC_MAX_PUBLICATIONS / (l->mtu / ITEM_SIZE);
  1602. l->window = win;
  1603. l->backlog[TIPC_LOW_IMPORTANCE].limit = max_t(u16, 50, win);
  1604. l->backlog[TIPC_MEDIUM_IMPORTANCE].limit = max_t(u16, 100, win * 2);
  1605. l->backlog[TIPC_HIGH_IMPORTANCE].limit = max_t(u16, 150, win * 3);
  1606. l->backlog[TIPC_CRITICAL_IMPORTANCE].limit = max_t(u16, 200, win * 4);
  1607. l->backlog[TIPC_SYSTEM_IMPORTANCE].limit = max_bulk;
  1608. }
  1609. /**
  1610. * link_reset_stats - reset link statistics
  1611. * @l: pointer to link
  1612. */
  1613. void tipc_link_reset_stats(struct tipc_link *l)
  1614. {
  1615. memset(&l->stats, 0, sizeof(l->stats));
  1616. }
  1617. static void link_print(struct tipc_link *l, const char *str)
  1618. {
  1619. struct sk_buff *hskb = skb_peek(&l->transmq);
  1620. u16 head = hskb ? msg_seqno(buf_msg(hskb)) : l->snd_nxt - 1;
  1621. u16 tail = l->snd_nxt - 1;
  1622. pr_info("%s Link <%s> state %x\n", str, l->name, l->state);
  1623. pr_info("XMTQ: %u [%u-%u], BKLGQ: %u, SNDNX: %u, RCVNX: %u\n",
  1624. skb_queue_len(&l->transmq), head, tail,
  1625. skb_queue_len(&l->backlogq), l->snd_nxt, l->rcv_nxt);
  1626. }
  1627. /* Parse and validate nested (link) properties valid for media, bearer and link
  1628. */
  1629. int tipc_nl_parse_link_prop(struct nlattr *prop, struct nlattr *props[])
  1630. {
  1631. int err;
  1632. err = nla_parse_nested(props, TIPC_NLA_PROP_MAX, prop,
  1633. tipc_nl_prop_policy, NULL);
  1634. if (err)
  1635. return err;
  1636. if (props[TIPC_NLA_PROP_PRIO]) {
  1637. u32 prio;
  1638. prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
  1639. if (prio > TIPC_MAX_LINK_PRI)
  1640. return -EINVAL;
  1641. }
  1642. if (props[TIPC_NLA_PROP_TOL]) {
  1643. u32 tol;
  1644. tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
  1645. if ((tol < TIPC_MIN_LINK_TOL) || (tol > TIPC_MAX_LINK_TOL))
  1646. return -EINVAL;
  1647. }
  1648. if (props[TIPC_NLA_PROP_WIN]) {
  1649. u32 win;
  1650. win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
  1651. if ((win < TIPC_MIN_LINK_WIN) || (win > TIPC_MAX_LINK_WIN))
  1652. return -EINVAL;
  1653. }
  1654. return 0;
  1655. }
  1656. static int __tipc_nl_add_stats(struct sk_buff *skb, struct tipc_stats *s)
  1657. {
  1658. int i;
  1659. struct nlattr *stats;
  1660. struct nla_map {
  1661. u32 key;
  1662. u32 val;
  1663. };
  1664. struct nla_map map[] = {
  1665. {TIPC_NLA_STATS_RX_INFO, 0},
  1666. {TIPC_NLA_STATS_RX_FRAGMENTS, s->recv_fragments},
  1667. {TIPC_NLA_STATS_RX_FRAGMENTED, s->recv_fragmented},
  1668. {TIPC_NLA_STATS_RX_BUNDLES, s->recv_bundles},
  1669. {TIPC_NLA_STATS_RX_BUNDLED, s->recv_bundled},
  1670. {TIPC_NLA_STATS_TX_INFO, 0},
  1671. {TIPC_NLA_STATS_TX_FRAGMENTS, s->sent_fragments},
  1672. {TIPC_NLA_STATS_TX_FRAGMENTED, s->sent_fragmented},
  1673. {TIPC_NLA_STATS_TX_BUNDLES, s->sent_bundles},
  1674. {TIPC_NLA_STATS_TX_BUNDLED, s->sent_bundled},
  1675. {TIPC_NLA_STATS_MSG_PROF_TOT, (s->msg_length_counts) ?
  1676. s->msg_length_counts : 1},
  1677. {TIPC_NLA_STATS_MSG_LEN_CNT, s->msg_length_counts},
  1678. {TIPC_NLA_STATS_MSG_LEN_TOT, s->msg_lengths_total},
  1679. {TIPC_NLA_STATS_MSG_LEN_P0, s->msg_length_profile[0]},
  1680. {TIPC_NLA_STATS_MSG_LEN_P1, s->msg_length_profile[1]},
  1681. {TIPC_NLA_STATS_MSG_LEN_P2, s->msg_length_profile[2]},
  1682. {TIPC_NLA_STATS_MSG_LEN_P3, s->msg_length_profile[3]},
  1683. {TIPC_NLA_STATS_MSG_LEN_P4, s->msg_length_profile[4]},
  1684. {TIPC_NLA_STATS_MSG_LEN_P5, s->msg_length_profile[5]},
  1685. {TIPC_NLA_STATS_MSG_LEN_P6, s->msg_length_profile[6]},
  1686. {TIPC_NLA_STATS_RX_STATES, s->recv_states},
  1687. {TIPC_NLA_STATS_RX_PROBES, s->recv_probes},
  1688. {TIPC_NLA_STATS_RX_NACKS, s->recv_nacks},
  1689. {TIPC_NLA_STATS_RX_DEFERRED, s->deferred_recv},
  1690. {TIPC_NLA_STATS_TX_STATES, s->sent_states},
  1691. {TIPC_NLA_STATS_TX_PROBES, s->sent_probes},
  1692. {TIPC_NLA_STATS_TX_NACKS, s->sent_nacks},
  1693. {TIPC_NLA_STATS_TX_ACKS, s->sent_acks},
  1694. {TIPC_NLA_STATS_RETRANSMITTED, s->retransmitted},
  1695. {TIPC_NLA_STATS_DUPLICATES, s->duplicates},
  1696. {TIPC_NLA_STATS_LINK_CONGS, s->link_congs},
  1697. {TIPC_NLA_STATS_MAX_QUEUE, s->max_queue_sz},
  1698. {TIPC_NLA_STATS_AVG_QUEUE, s->queue_sz_counts ?
  1699. (s->accu_queue_sz / s->queue_sz_counts) : 0}
  1700. };
  1701. stats = nla_nest_start(skb, TIPC_NLA_LINK_STATS);
  1702. if (!stats)
  1703. return -EMSGSIZE;
  1704. for (i = 0; i < ARRAY_SIZE(map); i++)
  1705. if (nla_put_u32(skb, map[i].key, map[i].val))
  1706. goto msg_full;
  1707. nla_nest_end(skb, stats);
  1708. return 0;
  1709. msg_full:
  1710. nla_nest_cancel(skb, stats);
  1711. return -EMSGSIZE;
  1712. }
  1713. /* Caller should hold appropriate locks to protect the link */
  1714. int __tipc_nl_add_link(struct net *net, struct tipc_nl_msg *msg,
  1715. struct tipc_link *link, int nlflags)
  1716. {
  1717. int err;
  1718. void *hdr;
  1719. struct nlattr *attrs;
  1720. struct nlattr *prop;
  1721. struct tipc_net *tn = net_generic(net, tipc_net_id);
  1722. hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
  1723. nlflags, TIPC_NL_LINK_GET);
  1724. if (!hdr)
  1725. return -EMSGSIZE;
  1726. attrs = nla_nest_start(msg->skb, TIPC_NLA_LINK);
  1727. if (!attrs)
  1728. goto msg_full;
  1729. if (nla_put_string(msg->skb, TIPC_NLA_LINK_NAME, link->name))
  1730. goto attr_msg_full;
  1731. if (nla_put_u32(msg->skb, TIPC_NLA_LINK_DEST,
  1732. tipc_cluster_mask(tn->own_addr)))
  1733. goto attr_msg_full;
  1734. if (nla_put_u32(msg->skb, TIPC_NLA_LINK_MTU, link->mtu))
  1735. goto attr_msg_full;
  1736. if (nla_put_u32(msg->skb, TIPC_NLA_LINK_RX, link->stats.recv_pkts))
  1737. goto attr_msg_full;
  1738. if (nla_put_u32(msg->skb, TIPC_NLA_LINK_TX, link->stats.sent_pkts))
  1739. goto attr_msg_full;
  1740. if (tipc_link_is_up(link))
  1741. if (nla_put_flag(msg->skb, TIPC_NLA_LINK_UP))
  1742. goto attr_msg_full;
  1743. if (link->active)
  1744. if (nla_put_flag(msg->skb, TIPC_NLA_LINK_ACTIVE))
  1745. goto attr_msg_full;
  1746. prop = nla_nest_start(msg->skb, TIPC_NLA_LINK_PROP);
  1747. if (!prop)
  1748. goto attr_msg_full;
  1749. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, link->priority))
  1750. goto prop_msg_full;
  1751. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_TOL, link->tolerance))
  1752. goto prop_msg_full;
  1753. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN,
  1754. link->window))
  1755. goto prop_msg_full;
  1756. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, link->priority))
  1757. goto prop_msg_full;
  1758. nla_nest_end(msg->skb, prop);
  1759. err = __tipc_nl_add_stats(msg->skb, &link->stats);
  1760. if (err)
  1761. goto attr_msg_full;
  1762. nla_nest_end(msg->skb, attrs);
  1763. genlmsg_end(msg->skb, hdr);
  1764. return 0;
  1765. prop_msg_full:
  1766. nla_nest_cancel(msg->skb, prop);
  1767. attr_msg_full:
  1768. nla_nest_cancel(msg->skb, attrs);
  1769. msg_full:
  1770. genlmsg_cancel(msg->skb, hdr);
  1771. return -EMSGSIZE;
  1772. }
  1773. static int __tipc_nl_add_bc_link_stat(struct sk_buff *skb,
  1774. struct tipc_stats *stats)
  1775. {
  1776. int i;
  1777. struct nlattr *nest;
  1778. struct nla_map {
  1779. __u32 key;
  1780. __u32 val;
  1781. };
  1782. struct nla_map map[] = {
  1783. {TIPC_NLA_STATS_RX_INFO, stats->recv_pkts},
  1784. {TIPC_NLA_STATS_RX_FRAGMENTS, stats->recv_fragments},
  1785. {TIPC_NLA_STATS_RX_FRAGMENTED, stats->recv_fragmented},
  1786. {TIPC_NLA_STATS_RX_BUNDLES, stats->recv_bundles},
  1787. {TIPC_NLA_STATS_RX_BUNDLED, stats->recv_bundled},
  1788. {TIPC_NLA_STATS_TX_INFO, stats->sent_pkts},
  1789. {TIPC_NLA_STATS_TX_FRAGMENTS, stats->sent_fragments},
  1790. {TIPC_NLA_STATS_TX_FRAGMENTED, stats->sent_fragmented},
  1791. {TIPC_NLA_STATS_TX_BUNDLES, stats->sent_bundles},
  1792. {TIPC_NLA_STATS_TX_BUNDLED, stats->sent_bundled},
  1793. {TIPC_NLA_STATS_RX_NACKS, stats->recv_nacks},
  1794. {TIPC_NLA_STATS_RX_DEFERRED, stats->deferred_recv},
  1795. {TIPC_NLA_STATS_TX_NACKS, stats->sent_nacks},
  1796. {TIPC_NLA_STATS_TX_ACKS, stats->sent_acks},
  1797. {TIPC_NLA_STATS_RETRANSMITTED, stats->retransmitted},
  1798. {TIPC_NLA_STATS_DUPLICATES, stats->duplicates},
  1799. {TIPC_NLA_STATS_LINK_CONGS, stats->link_congs},
  1800. {TIPC_NLA_STATS_MAX_QUEUE, stats->max_queue_sz},
  1801. {TIPC_NLA_STATS_AVG_QUEUE, stats->queue_sz_counts ?
  1802. (stats->accu_queue_sz / stats->queue_sz_counts) : 0}
  1803. };
  1804. nest = nla_nest_start(skb, TIPC_NLA_LINK_STATS);
  1805. if (!nest)
  1806. return -EMSGSIZE;
  1807. for (i = 0; i < ARRAY_SIZE(map); i++)
  1808. if (nla_put_u32(skb, map[i].key, map[i].val))
  1809. goto msg_full;
  1810. nla_nest_end(skb, nest);
  1811. return 0;
  1812. msg_full:
  1813. nla_nest_cancel(skb, nest);
  1814. return -EMSGSIZE;
  1815. }
  1816. int tipc_nl_add_bc_link(struct net *net, struct tipc_nl_msg *msg)
  1817. {
  1818. int err;
  1819. void *hdr;
  1820. struct nlattr *attrs;
  1821. struct nlattr *prop;
  1822. struct tipc_net *tn = net_generic(net, tipc_net_id);
  1823. struct tipc_link *bcl = tn->bcl;
  1824. if (!bcl)
  1825. return 0;
  1826. tipc_bcast_lock(net);
  1827. hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
  1828. NLM_F_MULTI, TIPC_NL_LINK_GET);
  1829. if (!hdr) {
  1830. tipc_bcast_unlock(net);
  1831. return -EMSGSIZE;
  1832. }
  1833. attrs = nla_nest_start(msg->skb, TIPC_NLA_LINK);
  1834. if (!attrs)
  1835. goto msg_full;
  1836. /* The broadcast link is always up */
  1837. if (nla_put_flag(msg->skb, TIPC_NLA_LINK_UP))
  1838. goto attr_msg_full;
  1839. if (nla_put_flag(msg->skb, TIPC_NLA_LINK_BROADCAST))
  1840. goto attr_msg_full;
  1841. if (nla_put_string(msg->skb, TIPC_NLA_LINK_NAME, bcl->name))
  1842. goto attr_msg_full;
  1843. if (nla_put_u32(msg->skb, TIPC_NLA_LINK_RX, 0))
  1844. goto attr_msg_full;
  1845. if (nla_put_u32(msg->skb, TIPC_NLA_LINK_TX, 0))
  1846. goto attr_msg_full;
  1847. prop = nla_nest_start(msg->skb, TIPC_NLA_LINK_PROP);
  1848. if (!prop)
  1849. goto attr_msg_full;
  1850. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN, bcl->window))
  1851. goto prop_msg_full;
  1852. nla_nest_end(msg->skb, prop);
  1853. err = __tipc_nl_add_bc_link_stat(msg->skb, &bcl->stats);
  1854. if (err)
  1855. goto attr_msg_full;
  1856. tipc_bcast_unlock(net);
  1857. nla_nest_end(msg->skb, attrs);
  1858. genlmsg_end(msg->skb, hdr);
  1859. return 0;
  1860. prop_msg_full:
  1861. nla_nest_cancel(msg->skb, prop);
  1862. attr_msg_full:
  1863. nla_nest_cancel(msg->skb, attrs);
  1864. msg_full:
  1865. tipc_bcast_unlock(net);
  1866. genlmsg_cancel(msg->skb, hdr);
  1867. return -EMSGSIZE;
  1868. }
  1869. void tipc_link_set_tolerance(struct tipc_link *l, u32 tol,
  1870. struct sk_buff_head *xmitq)
  1871. {
  1872. l->tolerance = tol;
  1873. tipc_link_build_proto_msg(l, STATE_MSG, 0, 0, tol, 0, xmitq);
  1874. }
  1875. void tipc_link_set_prio(struct tipc_link *l, u32 prio,
  1876. struct sk_buff_head *xmitq)
  1877. {
  1878. l->priority = prio;
  1879. tipc_link_build_proto_msg(l, STATE_MSG, 0, 0, 0, prio, xmitq);
  1880. }
  1881. void tipc_link_set_abort_limit(struct tipc_link *l, u32 limit)
  1882. {
  1883. l->abort_limit = limit;
  1884. }