link.c 54 KB

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