link.c 57 KB

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