link.c 57 KB

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