link.c 54 KB

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