link.c 53 KB

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