link.c 53 KB

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