link.c 54 KB

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