link.c 86 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343
  1. /*
  2. * net/tipc/link.c: TIPC link code
  3. *
  4. * Copyright (c) 1996-2007, Ericsson AB
  5. * Copyright (c) 2004-2007, 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 "dbg.h"
  38. #include "link.h"
  39. #include "net.h"
  40. #include "node.h"
  41. #include "port.h"
  42. #include "addr.h"
  43. #include "node_subscr.h"
  44. #include "name_distr.h"
  45. #include "bearer.h"
  46. #include "name_table.h"
  47. #include "discover.h"
  48. #include "config.h"
  49. #include "bcast.h"
  50. /*
  51. * Out-of-range value for link session numbers
  52. */
  53. #define INVALID_SESSION 0x10000
  54. /*
  55. * Limit for deferred reception queue:
  56. */
  57. #define DEF_QUEUE_LIMIT 256u
  58. /*
  59. * Link state events:
  60. */
  61. #define STARTING_EVT 856384768 /* link processing trigger */
  62. #define TRAFFIC_MSG_EVT 560815u /* rx'd ??? */
  63. #define TIMEOUT_EVT 560817u /* link timer expired */
  64. /*
  65. * The following two 'message types' is really just implementation
  66. * data conveniently stored in the message header.
  67. * They must not be considered part of the protocol
  68. */
  69. #define OPEN_MSG 0
  70. #define CLOSED_MSG 1
  71. /*
  72. * State value stored in 'exp_msg_count'
  73. */
  74. #define START_CHANGEOVER 100000u
  75. /**
  76. * struct link_name - deconstructed link name
  77. * @addr_local: network address of node at this end
  78. * @if_local: name of interface at this end
  79. * @addr_peer: network address of node at far end
  80. * @if_peer: name of interface at far end
  81. */
  82. struct link_name {
  83. u32 addr_local;
  84. char if_local[TIPC_MAX_IF_NAME];
  85. u32 addr_peer;
  86. char if_peer[TIPC_MAX_IF_NAME];
  87. };
  88. #if 0
  89. /* LINK EVENT CODE IS NOT SUPPORTED AT PRESENT */
  90. /**
  91. * struct link_event - link up/down event notification
  92. */
  93. struct link_event {
  94. u32 addr;
  95. int up;
  96. void (*fcn)(u32, char *, int);
  97. char name[TIPC_MAX_LINK_NAME];
  98. };
  99. #endif
  100. static void link_handle_out_of_seq_msg(struct link *l_ptr,
  101. struct sk_buff *buf);
  102. static void link_recv_proto_msg(struct link *l_ptr, struct sk_buff *buf);
  103. static int link_recv_changeover_msg(struct link **l_ptr, struct sk_buff **buf);
  104. static void link_set_supervision_props(struct link *l_ptr, u32 tolerance);
  105. static int link_send_sections_long(struct port *sender,
  106. struct iovec const *msg_sect,
  107. u32 num_sect, u32 destnode);
  108. static void link_check_defragm_bufs(struct link *l_ptr);
  109. static void link_state_event(struct link *l_ptr, u32 event);
  110. static void link_reset_statistics(struct link *l_ptr);
  111. static void link_print(struct link *l_ptr, struct print_buf *buf,
  112. const char *str);
  113. /*
  114. * Debugging code used by link routines only
  115. *
  116. * When debugging link problems on a system that has multiple links,
  117. * the standard TIPC debugging routines may not be useful since they
  118. * allow the output from multiple links to be intermixed. For this reason
  119. * routines of the form "dbg_link_XXX()" have been created that will capture
  120. * debug info into a link's personal print buffer, which can then be dumped
  121. * into the TIPC system log (TIPC_LOG) upon request.
  122. *
  123. * To enable per-link debugging, use LINK_LOG_BUF_SIZE to specify the size
  124. * of the print buffer used by each link. If LINK_LOG_BUF_SIZE is set to 0,
  125. * the dbg_link_XXX() routines simply send their output to the standard
  126. * debug print buffer (DBG_OUTPUT), if it has been defined; this can be useful
  127. * when there is only a single link in the system being debugged.
  128. *
  129. * Notes:
  130. * - When enabled, LINK_LOG_BUF_SIZE should be set to at least TIPC_PB_MIN_SIZE
  131. * - "l_ptr" must be valid when using dbg_link_XXX() macros
  132. */
  133. #define LINK_LOG_BUF_SIZE 0
  134. #define dbg_link(fmt, arg...) \
  135. do { \
  136. if (LINK_LOG_BUF_SIZE) \
  137. tipc_printf(&l_ptr->print_buf, fmt, ## arg); \
  138. } while (0)
  139. #define dbg_link_msg(msg, txt) \
  140. do { \
  141. if (LINK_LOG_BUF_SIZE) \
  142. tipc_msg_dbg(&l_ptr->print_buf, msg, txt); \
  143. } while (0)
  144. #define dbg_link_state(txt) \
  145. do { \
  146. if (LINK_LOG_BUF_SIZE) \
  147. link_print(l_ptr, &l_ptr->print_buf, txt); \
  148. } while (0)
  149. #define dbg_link_dump() do { \
  150. if (LINK_LOG_BUF_SIZE) { \
  151. tipc_printf(LOG, "\n\nDumping link <%s>:\n", l_ptr->name); \
  152. tipc_printbuf_move(LOG, &l_ptr->print_buf); \
  153. } \
  154. } while (0)
  155. static void dbg_print_link(struct link *l_ptr, const char *str)
  156. {
  157. if (DBG_OUTPUT != TIPC_NULL)
  158. link_print(l_ptr, DBG_OUTPUT, str);
  159. }
  160. static void dbg_print_buf_chain(struct sk_buff *root_buf)
  161. {
  162. if (DBG_OUTPUT != TIPC_NULL) {
  163. struct sk_buff *buf = root_buf;
  164. while (buf) {
  165. msg_dbg(buf_msg(buf), "In chain: ");
  166. buf = buf->next;
  167. }
  168. }
  169. }
  170. /*
  171. * Simple link routines
  172. */
  173. static unsigned int align(unsigned int i)
  174. {
  175. return (i + 3) & ~3u;
  176. }
  177. static void link_init_max_pkt(struct link *l_ptr)
  178. {
  179. u32 max_pkt;
  180. max_pkt = (l_ptr->b_ptr->publ.mtu & ~3);
  181. if (max_pkt > MAX_MSG_SIZE)
  182. max_pkt = MAX_MSG_SIZE;
  183. l_ptr->max_pkt_target = max_pkt;
  184. if (l_ptr->max_pkt_target < MAX_PKT_DEFAULT)
  185. l_ptr->max_pkt = l_ptr->max_pkt_target;
  186. else
  187. l_ptr->max_pkt = MAX_PKT_DEFAULT;
  188. l_ptr->max_pkt_probes = 0;
  189. }
  190. static u32 link_next_sent(struct link *l_ptr)
  191. {
  192. if (l_ptr->next_out)
  193. return msg_seqno(buf_msg(l_ptr->next_out));
  194. return mod(l_ptr->next_out_no);
  195. }
  196. static u32 link_last_sent(struct link *l_ptr)
  197. {
  198. return mod(link_next_sent(l_ptr) - 1);
  199. }
  200. /*
  201. * Simple non-static link routines (i.e. referenced outside this file)
  202. */
  203. int tipc_link_is_up(struct link *l_ptr)
  204. {
  205. if (!l_ptr)
  206. return 0;
  207. return (link_working_working(l_ptr) || link_working_unknown(l_ptr));
  208. }
  209. int tipc_link_is_active(struct link *l_ptr)
  210. {
  211. return ((l_ptr->owner->active_links[0] == l_ptr) ||
  212. (l_ptr->owner->active_links[1] == l_ptr));
  213. }
  214. /**
  215. * link_name_validate - validate & (optionally) deconstruct link name
  216. * @name - ptr to link name string
  217. * @name_parts - ptr to area for link name components (or NULL if not needed)
  218. *
  219. * Returns 1 if link name is valid, otherwise 0.
  220. */
  221. static int link_name_validate(const char *name, struct link_name *name_parts)
  222. {
  223. char name_copy[TIPC_MAX_LINK_NAME];
  224. char *addr_local;
  225. char *if_local;
  226. char *addr_peer;
  227. char *if_peer;
  228. char dummy;
  229. u32 z_local, c_local, n_local;
  230. u32 z_peer, c_peer, n_peer;
  231. u32 if_local_len;
  232. u32 if_peer_len;
  233. /* copy link name & ensure length is OK */
  234. name_copy[TIPC_MAX_LINK_NAME - 1] = 0;
  235. /* need above in case non-Posix strncpy() doesn't pad with nulls */
  236. strncpy(name_copy, name, TIPC_MAX_LINK_NAME);
  237. if (name_copy[TIPC_MAX_LINK_NAME - 1] != 0)
  238. return 0;
  239. /* ensure all component parts of link name are present */
  240. addr_local = name_copy;
  241. if ((if_local = strchr(addr_local, ':')) == NULL)
  242. return 0;
  243. *(if_local++) = 0;
  244. if ((addr_peer = strchr(if_local, '-')) == NULL)
  245. return 0;
  246. *(addr_peer++) = 0;
  247. if_local_len = addr_peer - if_local;
  248. if ((if_peer = strchr(addr_peer, ':')) == NULL)
  249. return 0;
  250. *(if_peer++) = 0;
  251. if_peer_len = strlen(if_peer) + 1;
  252. /* validate component parts of link name */
  253. if ((sscanf(addr_local, "%u.%u.%u%c",
  254. &z_local, &c_local, &n_local, &dummy) != 3) ||
  255. (sscanf(addr_peer, "%u.%u.%u%c",
  256. &z_peer, &c_peer, &n_peer, &dummy) != 3) ||
  257. (z_local > 255) || (c_local > 4095) || (n_local > 4095) ||
  258. (z_peer > 255) || (c_peer > 4095) || (n_peer > 4095) ||
  259. (if_local_len <= 1) || (if_local_len > TIPC_MAX_IF_NAME) ||
  260. (if_peer_len <= 1) || (if_peer_len > TIPC_MAX_IF_NAME) ||
  261. (strspn(if_local, tipc_alphabet) != (if_local_len - 1)) ||
  262. (strspn(if_peer, tipc_alphabet) != (if_peer_len - 1)))
  263. return 0;
  264. /* return link name components, if necessary */
  265. if (name_parts) {
  266. name_parts->addr_local = tipc_addr(z_local, c_local, n_local);
  267. strcpy(name_parts->if_local, if_local);
  268. name_parts->addr_peer = tipc_addr(z_peer, c_peer, n_peer);
  269. strcpy(name_parts->if_peer, if_peer);
  270. }
  271. return 1;
  272. }
  273. /**
  274. * link_timeout - handle expiration of link timer
  275. * @l_ptr: pointer to link
  276. *
  277. * This routine must not grab "tipc_net_lock" to avoid a potential deadlock conflict
  278. * with tipc_link_delete(). (There is no risk that the node will be deleted by
  279. * another thread because tipc_link_delete() always cancels the link timer before
  280. * tipc_node_delete() is called.)
  281. */
  282. static void link_timeout(struct link *l_ptr)
  283. {
  284. tipc_node_lock(l_ptr->owner);
  285. /* update counters used in statistical profiling of send traffic */
  286. l_ptr->stats.accu_queue_sz += l_ptr->out_queue_size;
  287. l_ptr->stats.queue_sz_counts++;
  288. if (l_ptr->out_queue_size > l_ptr->stats.max_queue_sz)
  289. l_ptr->stats.max_queue_sz = l_ptr->out_queue_size;
  290. if (l_ptr->first_out) {
  291. struct tipc_msg *msg = buf_msg(l_ptr->first_out);
  292. u32 length = msg_size(msg);
  293. if ((msg_user(msg) == MSG_FRAGMENTER) &&
  294. (msg_type(msg) == FIRST_FRAGMENT)) {
  295. length = msg_size(msg_get_wrapped(msg));
  296. }
  297. if (length) {
  298. l_ptr->stats.msg_lengths_total += length;
  299. l_ptr->stats.msg_length_counts++;
  300. if (length <= 64)
  301. l_ptr->stats.msg_length_profile[0]++;
  302. else if (length <= 256)
  303. l_ptr->stats.msg_length_profile[1]++;
  304. else if (length <= 1024)
  305. l_ptr->stats.msg_length_profile[2]++;
  306. else if (length <= 4096)
  307. l_ptr->stats.msg_length_profile[3]++;
  308. else if (length <= 16384)
  309. l_ptr->stats.msg_length_profile[4]++;
  310. else if (length <= 32768)
  311. l_ptr->stats.msg_length_profile[5]++;
  312. else
  313. l_ptr->stats.msg_length_profile[6]++;
  314. }
  315. }
  316. /* do all other link processing performed on a periodic basis */
  317. link_check_defragm_bufs(l_ptr);
  318. link_state_event(l_ptr, TIMEOUT_EVT);
  319. if (l_ptr->next_out)
  320. tipc_link_push_queue(l_ptr);
  321. tipc_node_unlock(l_ptr->owner);
  322. }
  323. static void link_set_timer(struct link *l_ptr, u32 time)
  324. {
  325. k_start_timer(&l_ptr->timer, time);
  326. }
  327. /**
  328. * tipc_link_create - create a new link
  329. * @b_ptr: pointer to associated bearer
  330. * @peer: network address of node at other end of link
  331. * @media_addr: media address to use when sending messages over link
  332. *
  333. * Returns pointer to link.
  334. */
  335. struct link *tipc_link_create(struct bearer *b_ptr, const u32 peer,
  336. const struct tipc_media_addr *media_addr)
  337. {
  338. struct link *l_ptr;
  339. struct tipc_msg *msg;
  340. char *if_name;
  341. l_ptr = kzalloc(sizeof(*l_ptr), GFP_ATOMIC);
  342. if (!l_ptr) {
  343. warn("Link creation failed, no memory\n");
  344. return NULL;
  345. }
  346. if (LINK_LOG_BUF_SIZE) {
  347. char *pb = kmalloc(LINK_LOG_BUF_SIZE, GFP_ATOMIC);
  348. if (!pb) {
  349. kfree(l_ptr);
  350. warn("Link creation failed, no memory for print buffer\n");
  351. return NULL;
  352. }
  353. tipc_printbuf_init(&l_ptr->print_buf, pb, LINK_LOG_BUF_SIZE);
  354. }
  355. l_ptr->addr = peer;
  356. if_name = strchr(b_ptr->publ.name, ':') + 1;
  357. sprintf(l_ptr->name, "%u.%u.%u:%s-%u.%u.%u:",
  358. tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
  359. tipc_node(tipc_own_addr),
  360. if_name,
  361. tipc_zone(peer), tipc_cluster(peer), tipc_node(peer));
  362. /* note: peer i/f is appended to link name by reset/activate */
  363. memcpy(&l_ptr->media_addr, media_addr, sizeof(*media_addr));
  364. l_ptr->checkpoint = 1;
  365. l_ptr->b_ptr = b_ptr;
  366. link_set_supervision_props(l_ptr, b_ptr->media->tolerance);
  367. l_ptr->state = RESET_UNKNOWN;
  368. l_ptr->pmsg = (struct tipc_msg *)&l_ptr->proto_msg;
  369. msg = l_ptr->pmsg;
  370. tipc_msg_init(msg, LINK_PROTOCOL, RESET_MSG, INT_H_SIZE, l_ptr->addr);
  371. msg_set_size(msg, sizeof(l_ptr->proto_msg));
  372. msg_set_session(msg, (tipc_random & 0xffff));
  373. msg_set_bearer_id(msg, b_ptr->identity);
  374. strcpy((char *)msg_data(msg), if_name);
  375. l_ptr->priority = b_ptr->priority;
  376. tipc_link_set_queue_limits(l_ptr, b_ptr->media->window);
  377. link_init_max_pkt(l_ptr);
  378. l_ptr->next_out_no = 1;
  379. INIT_LIST_HEAD(&l_ptr->waiting_ports);
  380. link_reset_statistics(l_ptr);
  381. l_ptr->owner = tipc_node_attach_link(l_ptr);
  382. if (!l_ptr->owner) {
  383. if (LINK_LOG_BUF_SIZE)
  384. kfree(l_ptr->print_buf.buf);
  385. kfree(l_ptr);
  386. return NULL;
  387. }
  388. k_init_timer(&l_ptr->timer, (Handler)link_timeout, (unsigned long)l_ptr);
  389. list_add_tail(&l_ptr->link_list, &b_ptr->links);
  390. tipc_k_signal((Handler)tipc_link_start, (unsigned long)l_ptr);
  391. dbg("tipc_link_create(): tolerance = %u,cont intv = %u, abort_limit = %u\n",
  392. l_ptr->tolerance, l_ptr->continuity_interval, l_ptr->abort_limit);
  393. return l_ptr;
  394. }
  395. /**
  396. * tipc_link_delete - delete a link
  397. * @l_ptr: pointer to link
  398. *
  399. * Note: 'tipc_net_lock' is write_locked, bearer is locked.
  400. * This routine must not grab the node lock until after link timer cancellation
  401. * to avoid a potential deadlock situation.
  402. */
  403. void tipc_link_delete(struct link *l_ptr)
  404. {
  405. if (!l_ptr) {
  406. err("Attempt to delete non-existent link\n");
  407. return;
  408. }
  409. dbg("tipc_link_delete()\n");
  410. k_cancel_timer(&l_ptr->timer);
  411. tipc_node_lock(l_ptr->owner);
  412. tipc_link_reset(l_ptr);
  413. tipc_node_detach_link(l_ptr->owner, l_ptr);
  414. tipc_link_stop(l_ptr);
  415. list_del_init(&l_ptr->link_list);
  416. if (LINK_LOG_BUF_SIZE)
  417. kfree(l_ptr->print_buf.buf);
  418. tipc_node_unlock(l_ptr->owner);
  419. k_term_timer(&l_ptr->timer);
  420. kfree(l_ptr);
  421. }
  422. void tipc_link_start(struct link *l_ptr)
  423. {
  424. dbg("tipc_link_start %x\n", l_ptr);
  425. link_state_event(l_ptr, STARTING_EVT);
  426. }
  427. /**
  428. * link_schedule_port - schedule port for deferred sending
  429. * @l_ptr: pointer to link
  430. * @origport: reference to sending port
  431. * @sz: amount of data to be sent
  432. *
  433. * Schedules port for renewed sending of messages after link congestion
  434. * has abated.
  435. */
  436. static int link_schedule_port(struct link *l_ptr, u32 origport, u32 sz)
  437. {
  438. struct port *p_ptr;
  439. spin_lock_bh(&tipc_port_list_lock);
  440. p_ptr = tipc_port_lock(origport);
  441. if (p_ptr) {
  442. if (!p_ptr->wakeup)
  443. goto exit;
  444. if (!list_empty(&p_ptr->wait_list))
  445. goto exit;
  446. p_ptr->publ.congested = 1;
  447. p_ptr->waiting_pkts = 1 + ((sz - 1) / l_ptr->max_pkt);
  448. list_add_tail(&p_ptr->wait_list, &l_ptr->waiting_ports);
  449. l_ptr->stats.link_congs++;
  450. exit:
  451. tipc_port_unlock(p_ptr);
  452. }
  453. spin_unlock_bh(&tipc_port_list_lock);
  454. return -ELINKCONG;
  455. }
  456. void tipc_link_wakeup_ports(struct link *l_ptr, int all)
  457. {
  458. struct port *p_ptr;
  459. struct port *temp_p_ptr;
  460. int win = l_ptr->queue_limit[0] - l_ptr->out_queue_size;
  461. if (all)
  462. win = 100000;
  463. if (win <= 0)
  464. return;
  465. if (!spin_trylock_bh(&tipc_port_list_lock))
  466. return;
  467. if (link_congested(l_ptr))
  468. goto exit;
  469. list_for_each_entry_safe(p_ptr, temp_p_ptr, &l_ptr->waiting_ports,
  470. wait_list) {
  471. if (win <= 0)
  472. break;
  473. list_del_init(&p_ptr->wait_list);
  474. spin_lock_bh(p_ptr->publ.lock);
  475. p_ptr->publ.congested = 0;
  476. p_ptr->wakeup(&p_ptr->publ);
  477. win -= p_ptr->waiting_pkts;
  478. spin_unlock_bh(p_ptr->publ.lock);
  479. }
  480. exit:
  481. spin_unlock_bh(&tipc_port_list_lock);
  482. }
  483. /**
  484. * link_release_outqueue - purge link's outbound message queue
  485. * @l_ptr: pointer to link
  486. */
  487. static void link_release_outqueue(struct link *l_ptr)
  488. {
  489. struct sk_buff *buf = l_ptr->first_out;
  490. struct sk_buff *next;
  491. while (buf) {
  492. next = buf->next;
  493. buf_discard(buf);
  494. buf = next;
  495. }
  496. l_ptr->first_out = NULL;
  497. l_ptr->out_queue_size = 0;
  498. }
  499. /**
  500. * tipc_link_reset_fragments - purge link's inbound message fragments queue
  501. * @l_ptr: pointer to link
  502. */
  503. void tipc_link_reset_fragments(struct link *l_ptr)
  504. {
  505. struct sk_buff *buf = l_ptr->defragm_buf;
  506. struct sk_buff *next;
  507. while (buf) {
  508. next = buf->next;
  509. buf_discard(buf);
  510. buf = next;
  511. }
  512. l_ptr->defragm_buf = NULL;
  513. }
  514. /**
  515. * tipc_link_stop - purge all inbound and outbound messages associated with link
  516. * @l_ptr: pointer to link
  517. */
  518. void tipc_link_stop(struct link *l_ptr)
  519. {
  520. struct sk_buff *buf;
  521. struct sk_buff *next;
  522. buf = l_ptr->oldest_deferred_in;
  523. while (buf) {
  524. next = buf->next;
  525. buf_discard(buf);
  526. buf = next;
  527. }
  528. buf = l_ptr->first_out;
  529. while (buf) {
  530. next = buf->next;
  531. buf_discard(buf);
  532. buf = next;
  533. }
  534. tipc_link_reset_fragments(l_ptr);
  535. buf_discard(l_ptr->proto_msg_queue);
  536. l_ptr->proto_msg_queue = NULL;
  537. }
  538. #if 0
  539. /* LINK EVENT CODE IS NOT SUPPORTED AT PRESENT */
  540. static void link_recv_event(struct link_event *ev)
  541. {
  542. ev->fcn(ev->addr, ev->name, ev->up);
  543. kfree(ev);
  544. }
  545. static void link_send_event(void (*fcn)(u32 a, char *n, int up),
  546. struct link *l_ptr, int up)
  547. {
  548. struct link_event *ev;
  549. ev = kmalloc(sizeof(*ev), GFP_ATOMIC);
  550. if (!ev) {
  551. warn("Link event allocation failure\n");
  552. return;
  553. }
  554. ev->addr = l_ptr->addr;
  555. ev->up = up;
  556. ev->fcn = fcn;
  557. memcpy(ev->name, l_ptr->name, TIPC_MAX_LINK_NAME);
  558. tipc_k_signal((Handler)link_recv_event, (unsigned long)ev);
  559. }
  560. #else
  561. #define link_send_event(fcn, l_ptr, up) do { } while (0)
  562. #endif
  563. void tipc_link_reset(struct link *l_ptr)
  564. {
  565. struct sk_buff *buf;
  566. u32 prev_state = l_ptr->state;
  567. u32 checkpoint = l_ptr->next_in_no;
  568. int was_active_link = tipc_link_is_active(l_ptr);
  569. msg_set_session(l_ptr->pmsg, ((msg_session(l_ptr->pmsg) + 1) & 0xffff));
  570. /* Link is down, accept any session */
  571. l_ptr->peer_session = INVALID_SESSION;
  572. /* Prepare for max packet size negotiation */
  573. link_init_max_pkt(l_ptr);
  574. l_ptr->state = RESET_UNKNOWN;
  575. dbg_link_state("Resetting Link\n");
  576. if ((prev_state == RESET_UNKNOWN) || (prev_state == RESET_RESET))
  577. return;
  578. tipc_node_link_down(l_ptr->owner, l_ptr);
  579. tipc_bearer_remove_dest(l_ptr->b_ptr, l_ptr->addr);
  580. #if 0
  581. tipc_printf(TIPC_CONS, "\nReset link <%s>\n", l_ptr->name);
  582. dbg_link_dump();
  583. #endif
  584. if (was_active_link && tipc_node_has_active_links(l_ptr->owner) &&
  585. l_ptr->owner->permit_changeover) {
  586. l_ptr->reset_checkpoint = checkpoint;
  587. l_ptr->exp_msg_count = START_CHANGEOVER;
  588. }
  589. /* Clean up all queues: */
  590. link_release_outqueue(l_ptr);
  591. buf_discard(l_ptr->proto_msg_queue);
  592. l_ptr->proto_msg_queue = NULL;
  593. buf = l_ptr->oldest_deferred_in;
  594. while (buf) {
  595. struct sk_buff *next = buf->next;
  596. buf_discard(buf);
  597. buf = next;
  598. }
  599. if (!list_empty(&l_ptr->waiting_ports))
  600. tipc_link_wakeup_ports(l_ptr, 1);
  601. l_ptr->retransm_queue_head = 0;
  602. l_ptr->retransm_queue_size = 0;
  603. l_ptr->last_out = NULL;
  604. l_ptr->first_out = NULL;
  605. l_ptr->next_out = NULL;
  606. l_ptr->unacked_window = 0;
  607. l_ptr->checkpoint = 1;
  608. l_ptr->next_out_no = 1;
  609. l_ptr->deferred_inqueue_sz = 0;
  610. l_ptr->oldest_deferred_in = NULL;
  611. l_ptr->newest_deferred_in = NULL;
  612. l_ptr->fsm_msg_cnt = 0;
  613. l_ptr->stale_count = 0;
  614. link_reset_statistics(l_ptr);
  615. link_send_event(tipc_cfg_link_event, l_ptr, 0);
  616. if (!in_own_cluster(l_ptr->addr))
  617. link_send_event(tipc_disc_link_event, l_ptr, 0);
  618. }
  619. static void link_activate(struct link *l_ptr)
  620. {
  621. l_ptr->next_in_no = l_ptr->stats.recv_info = 1;
  622. tipc_node_link_up(l_ptr->owner, l_ptr);
  623. tipc_bearer_add_dest(l_ptr->b_ptr, l_ptr->addr);
  624. link_send_event(tipc_cfg_link_event, l_ptr, 1);
  625. if (!in_own_cluster(l_ptr->addr))
  626. link_send_event(tipc_disc_link_event, l_ptr, 1);
  627. }
  628. /**
  629. * link_state_event - link finite state machine
  630. * @l_ptr: pointer to link
  631. * @event: state machine event to process
  632. */
  633. static void link_state_event(struct link *l_ptr, unsigned event)
  634. {
  635. struct link *other;
  636. u32 cont_intv = l_ptr->continuity_interval;
  637. if (!l_ptr->started && (event != STARTING_EVT))
  638. return; /* Not yet. */
  639. if (link_blocked(l_ptr)) {
  640. if (event == TIMEOUT_EVT) {
  641. link_set_timer(l_ptr, cont_intv);
  642. }
  643. return; /* Changeover going on */
  644. }
  645. dbg_link("STATE_EV: <%s> ", l_ptr->name);
  646. switch (l_ptr->state) {
  647. case WORKING_WORKING:
  648. dbg_link("WW/");
  649. switch (event) {
  650. case TRAFFIC_MSG_EVT:
  651. dbg_link("TRF-");
  652. /* fall through */
  653. case ACTIVATE_MSG:
  654. dbg_link("ACT\n");
  655. break;
  656. case TIMEOUT_EVT:
  657. dbg_link("TIM ");
  658. if (l_ptr->next_in_no != l_ptr->checkpoint) {
  659. l_ptr->checkpoint = l_ptr->next_in_no;
  660. if (tipc_bclink_acks_missing(l_ptr->owner)) {
  661. tipc_link_send_proto_msg(l_ptr, STATE_MSG,
  662. 0, 0, 0, 0, 0);
  663. l_ptr->fsm_msg_cnt++;
  664. } else if (l_ptr->max_pkt < l_ptr->max_pkt_target) {
  665. tipc_link_send_proto_msg(l_ptr, STATE_MSG,
  666. 1, 0, 0, 0, 0);
  667. l_ptr->fsm_msg_cnt++;
  668. }
  669. link_set_timer(l_ptr, cont_intv);
  670. break;
  671. }
  672. dbg_link(" -> WU\n");
  673. l_ptr->state = WORKING_UNKNOWN;
  674. l_ptr->fsm_msg_cnt = 0;
  675. tipc_link_send_proto_msg(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
  676. l_ptr->fsm_msg_cnt++;
  677. link_set_timer(l_ptr, cont_intv / 4);
  678. break;
  679. case RESET_MSG:
  680. dbg_link("RES -> RR\n");
  681. info("Resetting link <%s>, requested by peer\n",
  682. l_ptr->name);
  683. tipc_link_reset(l_ptr);
  684. l_ptr->state = RESET_RESET;
  685. l_ptr->fsm_msg_cnt = 0;
  686. tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 0, 0, 0, 0, 0);
  687. l_ptr->fsm_msg_cnt++;
  688. link_set_timer(l_ptr, cont_intv);
  689. break;
  690. default:
  691. err("Unknown link event %u in WW state\n", event);
  692. }
  693. break;
  694. case WORKING_UNKNOWN:
  695. dbg_link("WU/");
  696. switch (event) {
  697. case TRAFFIC_MSG_EVT:
  698. dbg_link("TRF-");
  699. case ACTIVATE_MSG:
  700. dbg_link("ACT -> WW\n");
  701. l_ptr->state = WORKING_WORKING;
  702. l_ptr->fsm_msg_cnt = 0;
  703. link_set_timer(l_ptr, cont_intv);
  704. break;
  705. case RESET_MSG:
  706. dbg_link("RES -> RR\n");
  707. info("Resetting link <%s>, requested by peer "
  708. "while probing\n", l_ptr->name);
  709. tipc_link_reset(l_ptr);
  710. l_ptr->state = RESET_RESET;
  711. l_ptr->fsm_msg_cnt = 0;
  712. tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 0, 0, 0, 0, 0);
  713. l_ptr->fsm_msg_cnt++;
  714. link_set_timer(l_ptr, cont_intv);
  715. break;
  716. case TIMEOUT_EVT:
  717. dbg_link("TIM ");
  718. if (l_ptr->next_in_no != l_ptr->checkpoint) {
  719. dbg_link("-> WW\n");
  720. l_ptr->state = WORKING_WORKING;
  721. l_ptr->fsm_msg_cnt = 0;
  722. l_ptr->checkpoint = l_ptr->next_in_no;
  723. if (tipc_bclink_acks_missing(l_ptr->owner)) {
  724. tipc_link_send_proto_msg(l_ptr, STATE_MSG,
  725. 0, 0, 0, 0, 0);
  726. l_ptr->fsm_msg_cnt++;
  727. }
  728. link_set_timer(l_ptr, cont_intv);
  729. } else if (l_ptr->fsm_msg_cnt < l_ptr->abort_limit) {
  730. dbg_link("Probing %u/%u,timer = %u ms)\n",
  731. l_ptr->fsm_msg_cnt, l_ptr->abort_limit,
  732. cont_intv / 4);
  733. tipc_link_send_proto_msg(l_ptr, STATE_MSG,
  734. 1, 0, 0, 0, 0);
  735. l_ptr->fsm_msg_cnt++;
  736. link_set_timer(l_ptr, cont_intv / 4);
  737. } else { /* Link has failed */
  738. dbg_link("-> RU (%u probes unanswered)\n",
  739. l_ptr->fsm_msg_cnt);
  740. warn("Resetting link <%s>, peer not responding\n",
  741. l_ptr->name);
  742. tipc_link_reset(l_ptr);
  743. l_ptr->state = RESET_UNKNOWN;
  744. l_ptr->fsm_msg_cnt = 0;
  745. tipc_link_send_proto_msg(l_ptr, RESET_MSG,
  746. 0, 0, 0, 0, 0);
  747. l_ptr->fsm_msg_cnt++;
  748. link_set_timer(l_ptr, cont_intv);
  749. }
  750. break;
  751. default:
  752. err("Unknown link event %u in WU state\n", event);
  753. }
  754. break;
  755. case RESET_UNKNOWN:
  756. dbg_link("RU/");
  757. switch (event) {
  758. case TRAFFIC_MSG_EVT:
  759. dbg_link("TRF-\n");
  760. break;
  761. case ACTIVATE_MSG:
  762. other = l_ptr->owner->active_links[0];
  763. if (other && link_working_unknown(other)) {
  764. dbg_link("ACT\n");
  765. break;
  766. }
  767. dbg_link("ACT -> WW\n");
  768. l_ptr->state = WORKING_WORKING;
  769. l_ptr->fsm_msg_cnt = 0;
  770. link_activate(l_ptr);
  771. tipc_link_send_proto_msg(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
  772. l_ptr->fsm_msg_cnt++;
  773. link_set_timer(l_ptr, cont_intv);
  774. break;
  775. case RESET_MSG:
  776. dbg_link("RES\n");
  777. dbg_link(" -> RR\n");
  778. l_ptr->state = RESET_RESET;
  779. l_ptr->fsm_msg_cnt = 0;
  780. tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 1, 0, 0, 0, 0);
  781. l_ptr->fsm_msg_cnt++;
  782. link_set_timer(l_ptr, cont_intv);
  783. break;
  784. case STARTING_EVT:
  785. dbg_link("START-");
  786. l_ptr->started = 1;
  787. /* fall through */
  788. case TIMEOUT_EVT:
  789. dbg_link("TIM\n");
  790. tipc_link_send_proto_msg(l_ptr, RESET_MSG, 0, 0, 0, 0, 0);
  791. l_ptr->fsm_msg_cnt++;
  792. link_set_timer(l_ptr, cont_intv);
  793. break;
  794. default:
  795. err("Unknown link event %u in RU state\n", event);
  796. }
  797. break;
  798. case RESET_RESET:
  799. dbg_link("RR/ ");
  800. switch (event) {
  801. case TRAFFIC_MSG_EVT:
  802. dbg_link("TRF-");
  803. /* fall through */
  804. case ACTIVATE_MSG:
  805. other = l_ptr->owner->active_links[0];
  806. if (other && link_working_unknown(other)) {
  807. dbg_link("ACT\n");
  808. break;
  809. }
  810. dbg_link("ACT -> WW\n");
  811. l_ptr->state = WORKING_WORKING;
  812. l_ptr->fsm_msg_cnt = 0;
  813. link_activate(l_ptr);
  814. tipc_link_send_proto_msg(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
  815. l_ptr->fsm_msg_cnt++;
  816. link_set_timer(l_ptr, cont_intv);
  817. break;
  818. case RESET_MSG:
  819. dbg_link("RES\n");
  820. break;
  821. case TIMEOUT_EVT:
  822. dbg_link("TIM\n");
  823. tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 0, 0, 0, 0, 0);
  824. l_ptr->fsm_msg_cnt++;
  825. link_set_timer(l_ptr, cont_intv);
  826. dbg_link("fsm_msg_cnt %u\n", l_ptr->fsm_msg_cnt);
  827. break;
  828. default:
  829. err("Unknown link event %u in RR state\n", event);
  830. }
  831. break;
  832. default:
  833. err("Unknown link state %u/%u\n", l_ptr->state, event);
  834. }
  835. }
  836. /*
  837. * link_bundle_buf(): Append contents of a buffer to
  838. * the tail of an existing one.
  839. */
  840. static int link_bundle_buf(struct link *l_ptr,
  841. struct sk_buff *bundler,
  842. struct sk_buff *buf)
  843. {
  844. struct tipc_msg *bundler_msg = buf_msg(bundler);
  845. struct tipc_msg *msg = buf_msg(buf);
  846. u32 size = msg_size(msg);
  847. u32 bundle_size = msg_size(bundler_msg);
  848. u32 to_pos = align(bundle_size);
  849. u32 pad = to_pos - bundle_size;
  850. if (msg_user(bundler_msg) != MSG_BUNDLER)
  851. return 0;
  852. if (msg_type(bundler_msg) != OPEN_MSG)
  853. return 0;
  854. if (skb_tailroom(bundler) < (pad + size))
  855. return 0;
  856. if (l_ptr->max_pkt < (to_pos + size))
  857. return 0;
  858. skb_put(bundler, pad + size);
  859. skb_copy_to_linear_data_offset(bundler, to_pos, buf->data, size);
  860. msg_set_size(bundler_msg, to_pos + size);
  861. msg_set_msgcnt(bundler_msg, msg_msgcnt(bundler_msg) + 1);
  862. dbg("Packed msg # %u(%u octets) into pos %u in buf(#%u)\n",
  863. msg_msgcnt(bundler_msg), size, to_pos, msg_seqno(bundler_msg));
  864. msg_dbg(msg, "PACKD:");
  865. buf_discard(buf);
  866. l_ptr->stats.sent_bundled++;
  867. return 1;
  868. }
  869. static void link_add_to_outqueue(struct link *l_ptr,
  870. struct sk_buff *buf,
  871. struct tipc_msg *msg)
  872. {
  873. u32 ack = mod(l_ptr->next_in_no - 1);
  874. u32 seqno = mod(l_ptr->next_out_no++);
  875. msg_set_word(msg, 2, ((ack << 16) | seqno));
  876. msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
  877. buf->next = NULL;
  878. if (l_ptr->first_out) {
  879. l_ptr->last_out->next = buf;
  880. l_ptr->last_out = buf;
  881. } else
  882. l_ptr->first_out = l_ptr->last_out = buf;
  883. l_ptr->out_queue_size++;
  884. }
  885. /*
  886. * tipc_link_send_buf() is the 'full path' for messages, called from
  887. * inside TIPC when the 'fast path' in tipc_send_buf
  888. * has failed, and from link_send()
  889. */
  890. int tipc_link_send_buf(struct link *l_ptr, struct sk_buff *buf)
  891. {
  892. struct tipc_msg *msg = buf_msg(buf);
  893. u32 size = msg_size(msg);
  894. u32 dsz = msg_data_sz(msg);
  895. u32 queue_size = l_ptr->out_queue_size;
  896. u32 imp = tipc_msg_tot_importance(msg);
  897. u32 queue_limit = l_ptr->queue_limit[imp];
  898. u32 max_packet = l_ptr->max_pkt;
  899. msg_set_prevnode(msg, tipc_own_addr); /* If routed message */
  900. /* Match msg importance against queue limits: */
  901. if (unlikely(queue_size >= queue_limit)) {
  902. if (imp <= TIPC_CRITICAL_IMPORTANCE) {
  903. return link_schedule_port(l_ptr, msg_origport(msg),
  904. size);
  905. }
  906. msg_dbg(msg, "TIPC: Congestion, throwing away\n");
  907. buf_discard(buf);
  908. if (imp > CONN_MANAGER) {
  909. warn("Resetting link <%s>, send queue full", l_ptr->name);
  910. tipc_link_reset(l_ptr);
  911. }
  912. return dsz;
  913. }
  914. /* Fragmentation needed ? */
  915. if (size > max_packet)
  916. return tipc_link_send_long_buf(l_ptr, buf);
  917. /* Packet can be queued or sent: */
  918. if (queue_size > l_ptr->stats.max_queue_sz)
  919. l_ptr->stats.max_queue_sz = queue_size;
  920. if (likely(!tipc_bearer_congested(l_ptr->b_ptr, l_ptr) &&
  921. !link_congested(l_ptr))) {
  922. link_add_to_outqueue(l_ptr, buf, msg);
  923. if (likely(tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr))) {
  924. l_ptr->unacked_window = 0;
  925. } else {
  926. tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
  927. l_ptr->stats.bearer_congs++;
  928. l_ptr->next_out = buf;
  929. }
  930. return dsz;
  931. }
  932. /* Congestion: can message be bundled ?: */
  933. if ((msg_user(msg) != CHANGEOVER_PROTOCOL) &&
  934. (msg_user(msg) != MSG_FRAGMENTER)) {
  935. /* Try adding message to an existing bundle */
  936. if (l_ptr->next_out &&
  937. link_bundle_buf(l_ptr, l_ptr->last_out, buf)) {
  938. tipc_bearer_resolve_congestion(l_ptr->b_ptr, l_ptr);
  939. return dsz;
  940. }
  941. /* Try creating a new bundle */
  942. if (size <= max_packet * 2 / 3) {
  943. struct sk_buff *bundler = buf_acquire(max_packet);
  944. struct tipc_msg bundler_hdr;
  945. if (bundler) {
  946. tipc_msg_init(&bundler_hdr, MSG_BUNDLER, OPEN_MSG,
  947. INT_H_SIZE, l_ptr->addr);
  948. skb_copy_to_linear_data(bundler, &bundler_hdr,
  949. INT_H_SIZE);
  950. skb_trim(bundler, INT_H_SIZE);
  951. link_bundle_buf(l_ptr, bundler, buf);
  952. buf = bundler;
  953. msg = buf_msg(buf);
  954. l_ptr->stats.sent_bundles++;
  955. }
  956. }
  957. }
  958. if (!l_ptr->next_out)
  959. l_ptr->next_out = buf;
  960. link_add_to_outqueue(l_ptr, buf, msg);
  961. tipc_bearer_resolve_congestion(l_ptr->b_ptr, l_ptr);
  962. return dsz;
  963. }
  964. /*
  965. * tipc_link_send(): same as tipc_link_send_buf(), but the link to use has
  966. * not been selected yet, and the the owner node is not locked
  967. * Called by TIPC internal users, e.g. the name distributor
  968. */
  969. int tipc_link_send(struct sk_buff *buf, u32 dest, u32 selector)
  970. {
  971. struct link *l_ptr;
  972. struct tipc_node *n_ptr;
  973. int res = -ELINKCONG;
  974. read_lock_bh(&tipc_net_lock);
  975. n_ptr = tipc_node_select(dest, selector);
  976. if (n_ptr) {
  977. tipc_node_lock(n_ptr);
  978. l_ptr = n_ptr->active_links[selector & 1];
  979. if (l_ptr) {
  980. dbg("tipc_link_send: found link %x for dest %x\n", l_ptr, dest);
  981. res = tipc_link_send_buf(l_ptr, buf);
  982. } else {
  983. dbg("Attempt to send msg to unreachable node:\n");
  984. msg_dbg(buf_msg(buf),">>>");
  985. buf_discard(buf);
  986. }
  987. tipc_node_unlock(n_ptr);
  988. } else {
  989. dbg("Attempt to send msg to unknown node:\n");
  990. msg_dbg(buf_msg(buf),">>>");
  991. buf_discard(buf);
  992. }
  993. read_unlock_bh(&tipc_net_lock);
  994. return res;
  995. }
  996. /*
  997. * link_send_buf_fast: Entry for data messages where the
  998. * destination link is known and the header is complete,
  999. * inclusive total message length. Very time critical.
  1000. * Link is locked. Returns user data length.
  1001. */
  1002. static int link_send_buf_fast(struct link *l_ptr, struct sk_buff *buf,
  1003. u32 *used_max_pkt)
  1004. {
  1005. struct tipc_msg *msg = buf_msg(buf);
  1006. int res = msg_data_sz(msg);
  1007. if (likely(!link_congested(l_ptr))) {
  1008. if (likely(msg_size(msg) <= l_ptr->max_pkt)) {
  1009. if (likely(list_empty(&l_ptr->b_ptr->cong_links))) {
  1010. link_add_to_outqueue(l_ptr, buf, msg);
  1011. if (likely(tipc_bearer_send(l_ptr->b_ptr, buf,
  1012. &l_ptr->media_addr))) {
  1013. l_ptr->unacked_window = 0;
  1014. msg_dbg(msg,"SENT_FAST:");
  1015. return res;
  1016. }
  1017. dbg("failed sent fast...\n");
  1018. tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
  1019. l_ptr->stats.bearer_congs++;
  1020. l_ptr->next_out = buf;
  1021. return res;
  1022. }
  1023. }
  1024. else
  1025. *used_max_pkt = l_ptr->max_pkt;
  1026. }
  1027. return tipc_link_send_buf(l_ptr, buf); /* All other cases */
  1028. }
  1029. /*
  1030. * tipc_send_buf_fast: Entry for data messages where the
  1031. * destination node is known and the header is complete,
  1032. * inclusive total message length.
  1033. * Returns user data length.
  1034. */
  1035. int tipc_send_buf_fast(struct sk_buff *buf, u32 destnode)
  1036. {
  1037. struct link *l_ptr;
  1038. struct tipc_node *n_ptr;
  1039. int res;
  1040. u32 selector = msg_origport(buf_msg(buf)) & 1;
  1041. u32 dummy;
  1042. if (destnode == tipc_own_addr)
  1043. return tipc_port_recv_msg(buf);
  1044. read_lock_bh(&tipc_net_lock);
  1045. n_ptr = tipc_node_select(destnode, selector);
  1046. if (likely(n_ptr)) {
  1047. tipc_node_lock(n_ptr);
  1048. l_ptr = n_ptr->active_links[selector];
  1049. dbg("send_fast: buf %x selected %x, destnode = %x\n",
  1050. buf, l_ptr, destnode);
  1051. if (likely(l_ptr)) {
  1052. res = link_send_buf_fast(l_ptr, buf, &dummy);
  1053. tipc_node_unlock(n_ptr);
  1054. read_unlock_bh(&tipc_net_lock);
  1055. return res;
  1056. }
  1057. tipc_node_unlock(n_ptr);
  1058. }
  1059. read_unlock_bh(&tipc_net_lock);
  1060. res = msg_data_sz(buf_msg(buf));
  1061. tipc_reject_msg(buf, TIPC_ERR_NO_NODE);
  1062. return res;
  1063. }
  1064. /*
  1065. * tipc_link_send_sections_fast: Entry for messages where the
  1066. * destination processor is known and the header is complete,
  1067. * except for total message length.
  1068. * Returns user data length or errno.
  1069. */
  1070. int tipc_link_send_sections_fast(struct port *sender,
  1071. struct iovec const *msg_sect,
  1072. const u32 num_sect,
  1073. u32 destaddr)
  1074. {
  1075. struct tipc_msg *hdr = &sender->publ.phdr;
  1076. struct link *l_ptr;
  1077. struct sk_buff *buf;
  1078. struct tipc_node *node;
  1079. int res;
  1080. u32 selector = msg_origport(hdr) & 1;
  1081. again:
  1082. /*
  1083. * Try building message using port's max_pkt hint.
  1084. * (Must not hold any locks while building message.)
  1085. */
  1086. res = tipc_msg_build(hdr, msg_sect, num_sect, sender->publ.max_pkt,
  1087. !sender->user_port, &buf);
  1088. read_lock_bh(&tipc_net_lock);
  1089. node = tipc_node_select(destaddr, selector);
  1090. if (likely(node)) {
  1091. tipc_node_lock(node);
  1092. l_ptr = node->active_links[selector];
  1093. if (likely(l_ptr)) {
  1094. if (likely(buf)) {
  1095. res = link_send_buf_fast(l_ptr, buf,
  1096. &sender->publ.max_pkt);
  1097. if (unlikely(res < 0))
  1098. buf_discard(buf);
  1099. exit:
  1100. tipc_node_unlock(node);
  1101. read_unlock_bh(&tipc_net_lock);
  1102. return res;
  1103. }
  1104. /* Exit if build request was invalid */
  1105. if (unlikely(res < 0))
  1106. goto exit;
  1107. /* Exit if link (or bearer) is congested */
  1108. if (link_congested(l_ptr) ||
  1109. !list_empty(&l_ptr->b_ptr->cong_links)) {
  1110. res = link_schedule_port(l_ptr,
  1111. sender->publ.ref, res);
  1112. goto exit;
  1113. }
  1114. /*
  1115. * Message size exceeds max_pkt hint; update hint,
  1116. * then re-try fast path or fragment the message
  1117. */
  1118. sender->publ.max_pkt = l_ptr->max_pkt;
  1119. tipc_node_unlock(node);
  1120. read_unlock_bh(&tipc_net_lock);
  1121. if ((msg_hdr_sz(hdr) + res) <= sender->publ.max_pkt)
  1122. goto again;
  1123. return link_send_sections_long(sender, msg_sect,
  1124. num_sect, destaddr);
  1125. }
  1126. tipc_node_unlock(node);
  1127. }
  1128. read_unlock_bh(&tipc_net_lock);
  1129. /* Couldn't find a link to the destination node */
  1130. if (buf)
  1131. return tipc_reject_msg(buf, TIPC_ERR_NO_NODE);
  1132. if (res >= 0)
  1133. return tipc_port_reject_sections(sender, hdr, msg_sect, num_sect,
  1134. TIPC_ERR_NO_NODE);
  1135. return res;
  1136. }
  1137. /*
  1138. * link_send_sections_long(): Entry for long messages where the
  1139. * destination node is known and the header is complete,
  1140. * inclusive total message length.
  1141. * Link and bearer congestion status have been checked to be ok,
  1142. * and are ignored if they change.
  1143. *
  1144. * Note that fragments do not use the full link MTU so that they won't have
  1145. * to undergo refragmentation if link changeover causes them to be sent
  1146. * over another link with an additional tunnel header added as prefix.
  1147. * (Refragmentation will still occur if the other link has a smaller MTU.)
  1148. *
  1149. * Returns user data length or errno.
  1150. */
  1151. static int link_send_sections_long(struct port *sender,
  1152. struct iovec const *msg_sect,
  1153. u32 num_sect,
  1154. u32 destaddr)
  1155. {
  1156. struct link *l_ptr;
  1157. struct tipc_node *node;
  1158. struct tipc_msg *hdr = &sender->publ.phdr;
  1159. u32 dsz = msg_data_sz(hdr);
  1160. u32 max_pkt,fragm_sz,rest;
  1161. struct tipc_msg fragm_hdr;
  1162. struct sk_buff *buf,*buf_chain,*prev;
  1163. u32 fragm_crs,fragm_rest,hsz,sect_rest;
  1164. const unchar *sect_crs;
  1165. int curr_sect;
  1166. u32 fragm_no;
  1167. again:
  1168. fragm_no = 1;
  1169. max_pkt = sender->publ.max_pkt - INT_H_SIZE;
  1170. /* leave room for tunnel header in case of link changeover */
  1171. fragm_sz = max_pkt - INT_H_SIZE;
  1172. /* leave room for fragmentation header in each fragment */
  1173. rest = dsz;
  1174. fragm_crs = 0;
  1175. fragm_rest = 0;
  1176. sect_rest = 0;
  1177. sect_crs = NULL;
  1178. curr_sect = -1;
  1179. /* Prepare reusable fragment header: */
  1180. msg_dbg(hdr, ">FRAGMENTING>");
  1181. tipc_msg_init(&fragm_hdr, MSG_FRAGMENTER, FIRST_FRAGMENT,
  1182. INT_H_SIZE, msg_destnode(hdr));
  1183. msg_set_link_selector(&fragm_hdr, sender->publ.ref);
  1184. msg_set_size(&fragm_hdr, max_pkt);
  1185. msg_set_fragm_no(&fragm_hdr, 1);
  1186. /* Prepare header of first fragment: */
  1187. buf_chain = buf = buf_acquire(max_pkt);
  1188. if (!buf)
  1189. return -ENOMEM;
  1190. buf->next = NULL;
  1191. skb_copy_to_linear_data(buf, &fragm_hdr, INT_H_SIZE);
  1192. hsz = msg_hdr_sz(hdr);
  1193. skb_copy_to_linear_data_offset(buf, INT_H_SIZE, hdr, hsz);
  1194. msg_dbg(buf_msg(buf), ">BUILD>");
  1195. /* Chop up message: */
  1196. fragm_crs = INT_H_SIZE + hsz;
  1197. fragm_rest = fragm_sz - hsz;
  1198. do { /* For all sections */
  1199. u32 sz;
  1200. if (!sect_rest) {
  1201. sect_rest = msg_sect[++curr_sect].iov_len;
  1202. sect_crs = (const unchar *)msg_sect[curr_sect].iov_base;
  1203. }
  1204. if (sect_rest < fragm_rest)
  1205. sz = sect_rest;
  1206. else
  1207. sz = fragm_rest;
  1208. if (likely(!sender->user_port)) {
  1209. if (copy_from_user(buf->data + fragm_crs, sect_crs, sz)) {
  1210. error:
  1211. for (; buf_chain; buf_chain = buf) {
  1212. buf = buf_chain->next;
  1213. buf_discard(buf_chain);
  1214. }
  1215. return -EFAULT;
  1216. }
  1217. } else
  1218. skb_copy_to_linear_data_offset(buf, fragm_crs,
  1219. sect_crs, sz);
  1220. sect_crs += sz;
  1221. sect_rest -= sz;
  1222. fragm_crs += sz;
  1223. fragm_rest -= sz;
  1224. rest -= sz;
  1225. if (!fragm_rest && rest) {
  1226. /* Initiate new fragment: */
  1227. if (rest <= fragm_sz) {
  1228. fragm_sz = rest;
  1229. msg_set_type(&fragm_hdr,LAST_FRAGMENT);
  1230. } else {
  1231. msg_set_type(&fragm_hdr, FRAGMENT);
  1232. }
  1233. msg_set_size(&fragm_hdr, fragm_sz + INT_H_SIZE);
  1234. msg_set_fragm_no(&fragm_hdr, ++fragm_no);
  1235. prev = buf;
  1236. buf = buf_acquire(fragm_sz + INT_H_SIZE);
  1237. if (!buf)
  1238. goto error;
  1239. buf->next = NULL;
  1240. prev->next = buf;
  1241. skb_copy_to_linear_data(buf, &fragm_hdr, INT_H_SIZE);
  1242. fragm_crs = INT_H_SIZE;
  1243. fragm_rest = fragm_sz;
  1244. msg_dbg(buf_msg(buf)," >BUILD>");
  1245. }
  1246. }
  1247. while (rest > 0);
  1248. /*
  1249. * Now we have a buffer chain. Select a link and check
  1250. * that packet size is still OK
  1251. */
  1252. node = tipc_node_select(destaddr, sender->publ.ref & 1);
  1253. if (likely(node)) {
  1254. tipc_node_lock(node);
  1255. l_ptr = node->active_links[sender->publ.ref & 1];
  1256. if (!l_ptr) {
  1257. tipc_node_unlock(node);
  1258. goto reject;
  1259. }
  1260. if (l_ptr->max_pkt < max_pkt) {
  1261. sender->publ.max_pkt = l_ptr->max_pkt;
  1262. tipc_node_unlock(node);
  1263. for (; buf_chain; buf_chain = buf) {
  1264. buf = buf_chain->next;
  1265. buf_discard(buf_chain);
  1266. }
  1267. goto again;
  1268. }
  1269. } else {
  1270. reject:
  1271. for (; buf_chain; buf_chain = buf) {
  1272. buf = buf_chain->next;
  1273. buf_discard(buf_chain);
  1274. }
  1275. return tipc_port_reject_sections(sender, hdr, msg_sect, num_sect,
  1276. TIPC_ERR_NO_NODE);
  1277. }
  1278. /* Append whole chain to send queue: */
  1279. buf = buf_chain;
  1280. l_ptr->long_msg_seq_no = mod(l_ptr->long_msg_seq_no + 1);
  1281. if (!l_ptr->next_out)
  1282. l_ptr->next_out = buf_chain;
  1283. l_ptr->stats.sent_fragmented++;
  1284. while (buf) {
  1285. struct sk_buff *next = buf->next;
  1286. struct tipc_msg *msg = buf_msg(buf);
  1287. l_ptr->stats.sent_fragments++;
  1288. msg_set_long_msgno(msg, l_ptr->long_msg_seq_no);
  1289. link_add_to_outqueue(l_ptr, buf, msg);
  1290. msg_dbg(msg, ">ADD>");
  1291. buf = next;
  1292. }
  1293. /* Send it, if possible: */
  1294. tipc_link_push_queue(l_ptr);
  1295. tipc_node_unlock(node);
  1296. return dsz;
  1297. }
  1298. /*
  1299. * tipc_link_push_packet: Push one unsent packet to the media
  1300. */
  1301. u32 tipc_link_push_packet(struct link *l_ptr)
  1302. {
  1303. struct sk_buff *buf = l_ptr->first_out;
  1304. u32 r_q_size = l_ptr->retransm_queue_size;
  1305. u32 r_q_head = l_ptr->retransm_queue_head;
  1306. /* Step to position where retransmission failed, if any, */
  1307. /* consider that buffers may have been released in meantime */
  1308. if (r_q_size && buf) {
  1309. u32 last = lesser(mod(r_q_head + r_q_size),
  1310. link_last_sent(l_ptr));
  1311. u32 first = msg_seqno(buf_msg(buf));
  1312. while (buf && less(first, r_q_head)) {
  1313. first = mod(first + 1);
  1314. buf = buf->next;
  1315. }
  1316. l_ptr->retransm_queue_head = r_q_head = first;
  1317. l_ptr->retransm_queue_size = r_q_size = mod(last - first);
  1318. }
  1319. /* Continue retransmission now, if there is anything: */
  1320. if (r_q_size && buf) {
  1321. msg_set_ack(buf_msg(buf), mod(l_ptr->next_in_no - 1));
  1322. msg_set_bcast_ack(buf_msg(buf), l_ptr->owner->bclink.last_in);
  1323. if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
  1324. msg_dbg(buf_msg(buf), ">DEF-RETR>");
  1325. l_ptr->retransm_queue_head = mod(++r_q_head);
  1326. l_ptr->retransm_queue_size = --r_q_size;
  1327. l_ptr->stats.retransmitted++;
  1328. return 0;
  1329. } else {
  1330. l_ptr->stats.bearer_congs++;
  1331. msg_dbg(buf_msg(buf), "|>DEF-RETR>");
  1332. return PUSH_FAILED;
  1333. }
  1334. }
  1335. /* Send deferred protocol message, if any: */
  1336. buf = l_ptr->proto_msg_queue;
  1337. if (buf) {
  1338. msg_set_ack(buf_msg(buf), mod(l_ptr->next_in_no - 1));
  1339. msg_set_bcast_ack(buf_msg(buf),l_ptr->owner->bclink.last_in);
  1340. if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
  1341. msg_dbg(buf_msg(buf), ">DEF-PROT>");
  1342. l_ptr->unacked_window = 0;
  1343. buf_discard(buf);
  1344. l_ptr->proto_msg_queue = NULL;
  1345. return 0;
  1346. } else {
  1347. msg_dbg(buf_msg(buf), "|>DEF-PROT>");
  1348. l_ptr->stats.bearer_congs++;
  1349. return PUSH_FAILED;
  1350. }
  1351. }
  1352. /* Send one deferred data message, if send window not full: */
  1353. buf = l_ptr->next_out;
  1354. if (buf) {
  1355. struct tipc_msg *msg = buf_msg(buf);
  1356. u32 next = msg_seqno(msg);
  1357. u32 first = msg_seqno(buf_msg(l_ptr->first_out));
  1358. if (mod(next - first) < l_ptr->queue_limit[0]) {
  1359. msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
  1360. msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
  1361. if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
  1362. if (msg_user(msg) == MSG_BUNDLER)
  1363. msg_set_type(msg, CLOSED_MSG);
  1364. msg_dbg(msg, ">PUSH-DATA>");
  1365. l_ptr->next_out = buf->next;
  1366. return 0;
  1367. } else {
  1368. msg_dbg(msg, "|PUSH-DATA|");
  1369. l_ptr->stats.bearer_congs++;
  1370. return PUSH_FAILED;
  1371. }
  1372. }
  1373. }
  1374. return PUSH_FINISHED;
  1375. }
  1376. /*
  1377. * push_queue(): push out the unsent messages of a link where
  1378. * congestion has abated. Node is locked
  1379. */
  1380. void tipc_link_push_queue(struct link *l_ptr)
  1381. {
  1382. u32 res;
  1383. if (tipc_bearer_congested(l_ptr->b_ptr, l_ptr))
  1384. return;
  1385. do {
  1386. res = tipc_link_push_packet(l_ptr);
  1387. } while (!res);
  1388. if (res == PUSH_FAILED)
  1389. tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
  1390. }
  1391. static void link_reset_all(unsigned long addr)
  1392. {
  1393. struct tipc_node *n_ptr;
  1394. char addr_string[16];
  1395. u32 i;
  1396. read_lock_bh(&tipc_net_lock);
  1397. n_ptr = tipc_node_find((u32)addr);
  1398. if (!n_ptr) {
  1399. read_unlock_bh(&tipc_net_lock);
  1400. return; /* node no longer exists */
  1401. }
  1402. tipc_node_lock(n_ptr);
  1403. warn("Resetting all links to %s\n",
  1404. tipc_addr_string_fill(addr_string, n_ptr->addr));
  1405. for (i = 0; i < MAX_BEARERS; i++) {
  1406. if (n_ptr->links[i]) {
  1407. link_print(n_ptr->links[i], TIPC_OUTPUT,
  1408. "Resetting link\n");
  1409. tipc_link_reset(n_ptr->links[i]);
  1410. }
  1411. }
  1412. tipc_node_unlock(n_ptr);
  1413. read_unlock_bh(&tipc_net_lock);
  1414. }
  1415. static void link_retransmit_failure(struct link *l_ptr, struct sk_buff *buf)
  1416. {
  1417. struct tipc_msg *msg = buf_msg(buf);
  1418. warn("Retransmission failure on link <%s>\n", l_ptr->name);
  1419. tipc_msg_dbg(TIPC_OUTPUT, msg, ">RETR-FAIL>");
  1420. if (l_ptr->addr) {
  1421. /* Handle failure on standard link */
  1422. link_print(l_ptr, TIPC_OUTPUT, "Resetting link\n");
  1423. tipc_link_reset(l_ptr);
  1424. } else {
  1425. /* Handle failure on broadcast link */
  1426. struct tipc_node *n_ptr;
  1427. char addr_string[16];
  1428. tipc_printf(TIPC_OUTPUT, "Msg seq number: %u, ", msg_seqno(msg));
  1429. tipc_printf(TIPC_OUTPUT, "Outstanding acks: %lu\n",
  1430. (unsigned long) TIPC_SKB_CB(buf)->handle);
  1431. n_ptr = l_ptr->owner->next;
  1432. tipc_node_lock(n_ptr);
  1433. tipc_addr_string_fill(addr_string, n_ptr->addr);
  1434. tipc_printf(TIPC_OUTPUT, "Multicast link info for %s\n", addr_string);
  1435. tipc_printf(TIPC_OUTPUT, "Supported: %d, ", n_ptr->bclink.supported);
  1436. tipc_printf(TIPC_OUTPUT, "Acked: %u\n", n_ptr->bclink.acked);
  1437. tipc_printf(TIPC_OUTPUT, "Last in: %u, ", n_ptr->bclink.last_in);
  1438. tipc_printf(TIPC_OUTPUT, "Gap after: %u, ", n_ptr->bclink.gap_after);
  1439. tipc_printf(TIPC_OUTPUT, "Gap to: %u\n", n_ptr->bclink.gap_to);
  1440. tipc_printf(TIPC_OUTPUT, "Nack sync: %u\n\n", n_ptr->bclink.nack_sync);
  1441. tipc_k_signal((Handler)link_reset_all, (unsigned long)n_ptr->addr);
  1442. tipc_node_unlock(n_ptr);
  1443. l_ptr->stale_count = 0;
  1444. }
  1445. }
  1446. void tipc_link_retransmit(struct link *l_ptr, struct sk_buff *buf,
  1447. u32 retransmits)
  1448. {
  1449. struct tipc_msg *msg;
  1450. if (!buf)
  1451. return;
  1452. msg = buf_msg(buf);
  1453. dbg("Retransmitting %u in link %x\n", retransmits, l_ptr);
  1454. if (tipc_bearer_congested(l_ptr->b_ptr, l_ptr)) {
  1455. if (l_ptr->retransm_queue_size == 0) {
  1456. msg_dbg(msg, ">NO_RETR->BCONG>");
  1457. dbg_print_link(l_ptr, " ");
  1458. l_ptr->retransm_queue_head = msg_seqno(msg);
  1459. l_ptr->retransm_queue_size = retransmits;
  1460. } else {
  1461. err("Unexpected retransmit on link %s (qsize=%d)\n",
  1462. l_ptr->name, l_ptr->retransm_queue_size);
  1463. }
  1464. return;
  1465. } else {
  1466. /* Detect repeated retransmit failures on uncongested bearer */
  1467. if (l_ptr->last_retransmitted == msg_seqno(msg)) {
  1468. if (++l_ptr->stale_count > 100) {
  1469. link_retransmit_failure(l_ptr, buf);
  1470. return;
  1471. }
  1472. } else {
  1473. l_ptr->last_retransmitted = msg_seqno(msg);
  1474. l_ptr->stale_count = 1;
  1475. }
  1476. }
  1477. while (retransmits && (buf != l_ptr->next_out) && buf) {
  1478. msg = buf_msg(buf);
  1479. msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
  1480. msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
  1481. if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
  1482. msg_dbg(buf_msg(buf), ">RETR>");
  1483. buf = buf->next;
  1484. retransmits--;
  1485. l_ptr->stats.retransmitted++;
  1486. } else {
  1487. tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
  1488. l_ptr->stats.bearer_congs++;
  1489. l_ptr->retransm_queue_head = msg_seqno(buf_msg(buf));
  1490. l_ptr->retransm_queue_size = retransmits;
  1491. return;
  1492. }
  1493. }
  1494. l_ptr->retransm_queue_head = l_ptr->retransm_queue_size = 0;
  1495. }
  1496. /**
  1497. * link_insert_deferred_queue - insert deferred messages back into receive chain
  1498. */
  1499. static struct sk_buff *link_insert_deferred_queue(struct link *l_ptr,
  1500. struct sk_buff *buf)
  1501. {
  1502. u32 seq_no;
  1503. if (l_ptr->oldest_deferred_in == NULL)
  1504. return buf;
  1505. seq_no = msg_seqno(buf_msg(l_ptr->oldest_deferred_in));
  1506. if (seq_no == mod(l_ptr->next_in_no)) {
  1507. l_ptr->newest_deferred_in->next = buf;
  1508. buf = l_ptr->oldest_deferred_in;
  1509. l_ptr->oldest_deferred_in = NULL;
  1510. l_ptr->deferred_inqueue_sz = 0;
  1511. }
  1512. return buf;
  1513. }
  1514. /**
  1515. * link_recv_buf_validate - validate basic format of received message
  1516. *
  1517. * This routine ensures a TIPC message has an acceptable header, and at least
  1518. * as much data as the header indicates it should. The routine also ensures
  1519. * that the entire message header is stored in the main fragment of the message
  1520. * buffer, to simplify future access to message header fields.
  1521. *
  1522. * Note: Having extra info present in the message header or data areas is OK.
  1523. * TIPC will ignore the excess, under the assumption that it is optional info
  1524. * introduced by a later release of the protocol.
  1525. */
  1526. static int link_recv_buf_validate(struct sk_buff *buf)
  1527. {
  1528. static u32 min_data_hdr_size[8] = {
  1529. SHORT_H_SIZE, MCAST_H_SIZE, LONG_H_SIZE, DIR_MSG_H_SIZE,
  1530. MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE
  1531. };
  1532. struct tipc_msg *msg;
  1533. u32 tipc_hdr[2];
  1534. u32 size;
  1535. u32 hdr_size;
  1536. u32 min_hdr_size;
  1537. if (unlikely(buf->len < MIN_H_SIZE))
  1538. return 0;
  1539. msg = skb_header_pointer(buf, 0, sizeof(tipc_hdr), tipc_hdr);
  1540. if (msg == NULL)
  1541. return 0;
  1542. if (unlikely(msg_version(msg) != TIPC_VERSION))
  1543. return 0;
  1544. size = msg_size(msg);
  1545. hdr_size = msg_hdr_sz(msg);
  1546. min_hdr_size = msg_isdata(msg) ?
  1547. min_data_hdr_size[msg_type(msg)] : INT_H_SIZE;
  1548. if (unlikely((hdr_size < min_hdr_size) ||
  1549. (size < hdr_size) ||
  1550. (buf->len < size) ||
  1551. (size - hdr_size > TIPC_MAX_USER_MSG_SIZE)))
  1552. return 0;
  1553. return pskb_may_pull(buf, hdr_size);
  1554. }
  1555. /**
  1556. * tipc_recv_msg - process TIPC messages arriving from off-node
  1557. * @head: pointer to message buffer chain
  1558. * @tb_ptr: pointer to bearer message arrived on
  1559. *
  1560. * Invoked with no locks held. Bearer pointer must point to a valid bearer
  1561. * structure (i.e. cannot be NULL), but bearer can be inactive.
  1562. */
  1563. void tipc_recv_msg(struct sk_buff *head, struct tipc_bearer *tb_ptr)
  1564. {
  1565. read_lock_bh(&tipc_net_lock);
  1566. while (head) {
  1567. struct bearer *b_ptr = (struct bearer *)tb_ptr;
  1568. struct tipc_node *n_ptr;
  1569. struct link *l_ptr;
  1570. struct sk_buff *crs;
  1571. struct sk_buff *buf = head;
  1572. struct tipc_msg *msg;
  1573. u32 seq_no;
  1574. u32 ackd;
  1575. u32 released = 0;
  1576. int type;
  1577. head = head->next;
  1578. /* Ensure bearer is still enabled */
  1579. if (unlikely(!b_ptr->active))
  1580. goto cont;
  1581. /* Ensure message is well-formed */
  1582. if (unlikely(!link_recv_buf_validate(buf)))
  1583. goto cont;
  1584. /* Ensure message data is a single contiguous unit */
  1585. if (unlikely(buf_linearize(buf))) {
  1586. goto cont;
  1587. }
  1588. /* Handle arrival of a non-unicast link message */
  1589. msg = buf_msg(buf);
  1590. if (unlikely(msg_non_seq(msg))) {
  1591. if (msg_user(msg) == LINK_CONFIG)
  1592. tipc_disc_recv_msg(buf, b_ptr);
  1593. else
  1594. tipc_bclink_recv_pkt(buf);
  1595. continue;
  1596. }
  1597. if (unlikely(!msg_short(msg) &&
  1598. (msg_destnode(msg) != tipc_own_addr)))
  1599. goto cont;
  1600. /* Discard non-routeable messages destined for another node */
  1601. if (unlikely(!msg_isdata(msg) &&
  1602. (msg_destnode(msg) != tipc_own_addr))) {
  1603. if ((msg_user(msg) != CONN_MANAGER) &&
  1604. (msg_user(msg) != MSG_FRAGMENTER))
  1605. goto cont;
  1606. }
  1607. /* Locate unicast link endpoint that should handle message */
  1608. n_ptr = tipc_node_find(msg_prevnode(msg));
  1609. if (unlikely(!n_ptr))
  1610. goto cont;
  1611. tipc_node_lock(n_ptr);
  1612. l_ptr = n_ptr->links[b_ptr->identity];
  1613. if (unlikely(!l_ptr)) {
  1614. tipc_node_unlock(n_ptr);
  1615. goto cont;
  1616. }
  1617. /* Validate message sequence number info */
  1618. seq_no = msg_seqno(msg);
  1619. ackd = msg_ack(msg);
  1620. /* Release acked messages */
  1621. if (less(n_ptr->bclink.acked, msg_bcast_ack(msg))) {
  1622. if (tipc_node_is_up(n_ptr) && n_ptr->bclink.supported)
  1623. tipc_bclink_acknowledge(n_ptr, msg_bcast_ack(msg));
  1624. }
  1625. crs = l_ptr->first_out;
  1626. while ((crs != l_ptr->next_out) &&
  1627. less_eq(msg_seqno(buf_msg(crs)), ackd)) {
  1628. struct sk_buff *next = crs->next;
  1629. buf_discard(crs);
  1630. crs = next;
  1631. released++;
  1632. }
  1633. if (released) {
  1634. l_ptr->first_out = crs;
  1635. l_ptr->out_queue_size -= released;
  1636. }
  1637. /* Try sending any messages link endpoint has pending */
  1638. if (unlikely(l_ptr->next_out))
  1639. tipc_link_push_queue(l_ptr);
  1640. if (unlikely(!list_empty(&l_ptr->waiting_ports)))
  1641. tipc_link_wakeup_ports(l_ptr, 0);
  1642. if (unlikely(++l_ptr->unacked_window >= TIPC_MIN_LINK_WIN)) {
  1643. l_ptr->stats.sent_acks++;
  1644. tipc_link_send_proto_msg(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
  1645. }
  1646. /* Now (finally!) process the incoming message */
  1647. protocol_check:
  1648. if (likely(link_working_working(l_ptr))) {
  1649. if (likely(seq_no == mod(l_ptr->next_in_no))) {
  1650. l_ptr->next_in_no++;
  1651. if (unlikely(l_ptr->oldest_deferred_in))
  1652. head = link_insert_deferred_queue(l_ptr,
  1653. head);
  1654. if (likely(msg_is_dest(msg, tipc_own_addr))) {
  1655. deliver:
  1656. if (likely(msg_isdata(msg))) {
  1657. tipc_node_unlock(n_ptr);
  1658. tipc_port_recv_msg(buf);
  1659. continue;
  1660. }
  1661. switch (msg_user(msg)) {
  1662. case MSG_BUNDLER:
  1663. l_ptr->stats.recv_bundles++;
  1664. l_ptr->stats.recv_bundled +=
  1665. msg_msgcnt(msg);
  1666. tipc_node_unlock(n_ptr);
  1667. tipc_link_recv_bundle(buf);
  1668. continue;
  1669. case ROUTE_DISTRIBUTOR:
  1670. tipc_node_unlock(n_ptr);
  1671. tipc_cltr_recv_routing_table(buf);
  1672. continue;
  1673. case NAME_DISTRIBUTOR:
  1674. tipc_node_unlock(n_ptr);
  1675. tipc_named_recv(buf);
  1676. continue;
  1677. case CONN_MANAGER:
  1678. tipc_node_unlock(n_ptr);
  1679. tipc_port_recv_proto_msg(buf);
  1680. continue;
  1681. case MSG_FRAGMENTER:
  1682. l_ptr->stats.recv_fragments++;
  1683. if (tipc_link_recv_fragment(&l_ptr->defragm_buf,
  1684. &buf, &msg)) {
  1685. l_ptr->stats.recv_fragmented++;
  1686. goto deliver;
  1687. }
  1688. break;
  1689. case CHANGEOVER_PROTOCOL:
  1690. type = msg_type(msg);
  1691. if (link_recv_changeover_msg(&l_ptr, &buf)) {
  1692. msg = buf_msg(buf);
  1693. seq_no = msg_seqno(msg);
  1694. if (type == ORIGINAL_MSG)
  1695. goto deliver;
  1696. goto protocol_check;
  1697. }
  1698. break;
  1699. }
  1700. }
  1701. tipc_node_unlock(n_ptr);
  1702. tipc_net_route_msg(buf);
  1703. continue;
  1704. }
  1705. link_handle_out_of_seq_msg(l_ptr, buf);
  1706. head = link_insert_deferred_queue(l_ptr, head);
  1707. tipc_node_unlock(n_ptr);
  1708. continue;
  1709. }
  1710. if (msg_user(msg) == LINK_PROTOCOL) {
  1711. link_recv_proto_msg(l_ptr, buf);
  1712. head = link_insert_deferred_queue(l_ptr, head);
  1713. tipc_node_unlock(n_ptr);
  1714. continue;
  1715. }
  1716. msg_dbg(msg,"NSEQ<REC<");
  1717. link_state_event(l_ptr, TRAFFIC_MSG_EVT);
  1718. if (link_working_working(l_ptr)) {
  1719. /* Re-insert in front of queue */
  1720. msg_dbg(msg,"RECV-REINS:");
  1721. buf->next = head;
  1722. head = buf;
  1723. tipc_node_unlock(n_ptr);
  1724. continue;
  1725. }
  1726. tipc_node_unlock(n_ptr);
  1727. cont:
  1728. buf_discard(buf);
  1729. }
  1730. read_unlock_bh(&tipc_net_lock);
  1731. }
  1732. /*
  1733. * link_defer_buf(): Sort a received out-of-sequence packet
  1734. * into the deferred reception queue.
  1735. * Returns the increase of the queue length,i.e. 0 or 1
  1736. */
  1737. u32 tipc_link_defer_pkt(struct sk_buff **head,
  1738. struct sk_buff **tail,
  1739. struct sk_buff *buf)
  1740. {
  1741. struct sk_buff *prev = NULL;
  1742. struct sk_buff *crs = *head;
  1743. u32 seq_no = msg_seqno(buf_msg(buf));
  1744. buf->next = NULL;
  1745. /* Empty queue ? */
  1746. if (*head == NULL) {
  1747. *head = *tail = buf;
  1748. return 1;
  1749. }
  1750. /* Last ? */
  1751. if (less(msg_seqno(buf_msg(*tail)), seq_no)) {
  1752. (*tail)->next = buf;
  1753. *tail = buf;
  1754. return 1;
  1755. }
  1756. /* Scan through queue and sort it in */
  1757. do {
  1758. struct tipc_msg *msg = buf_msg(crs);
  1759. if (less(seq_no, msg_seqno(msg))) {
  1760. buf->next = crs;
  1761. if (prev)
  1762. prev->next = buf;
  1763. else
  1764. *head = buf;
  1765. return 1;
  1766. }
  1767. if (seq_no == msg_seqno(msg)) {
  1768. break;
  1769. }
  1770. prev = crs;
  1771. crs = crs->next;
  1772. }
  1773. while (crs);
  1774. /* Message is a duplicate of an existing message */
  1775. buf_discard(buf);
  1776. return 0;
  1777. }
  1778. /**
  1779. * link_handle_out_of_seq_msg - handle arrival of out-of-sequence packet
  1780. */
  1781. static void link_handle_out_of_seq_msg(struct link *l_ptr,
  1782. struct sk_buff *buf)
  1783. {
  1784. u32 seq_no = msg_seqno(buf_msg(buf));
  1785. if (likely(msg_user(buf_msg(buf)) == LINK_PROTOCOL)) {
  1786. link_recv_proto_msg(l_ptr, buf);
  1787. return;
  1788. }
  1789. dbg("rx OOS msg: seq_no %u, expecting %u (%u)\n",
  1790. seq_no, mod(l_ptr->next_in_no), l_ptr->next_in_no);
  1791. /* Record OOS packet arrival (force mismatch on next timeout) */
  1792. l_ptr->checkpoint--;
  1793. /*
  1794. * Discard packet if a duplicate; otherwise add it to deferred queue
  1795. * and notify peer of gap as per protocol specification
  1796. */
  1797. if (less(seq_no, mod(l_ptr->next_in_no))) {
  1798. l_ptr->stats.duplicates++;
  1799. buf_discard(buf);
  1800. return;
  1801. }
  1802. if (tipc_link_defer_pkt(&l_ptr->oldest_deferred_in,
  1803. &l_ptr->newest_deferred_in, buf)) {
  1804. l_ptr->deferred_inqueue_sz++;
  1805. l_ptr->stats.deferred_recv++;
  1806. if ((l_ptr->deferred_inqueue_sz % 16) == 1)
  1807. tipc_link_send_proto_msg(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
  1808. } else
  1809. l_ptr->stats.duplicates++;
  1810. }
  1811. /*
  1812. * Send protocol message to the other endpoint.
  1813. */
  1814. void tipc_link_send_proto_msg(struct link *l_ptr, u32 msg_typ, int probe_msg,
  1815. u32 gap, u32 tolerance, u32 priority, u32 ack_mtu)
  1816. {
  1817. struct sk_buff *buf = NULL;
  1818. struct tipc_msg *msg = l_ptr->pmsg;
  1819. u32 msg_size = sizeof(l_ptr->proto_msg);
  1820. if (link_blocked(l_ptr))
  1821. return;
  1822. msg_set_type(msg, msg_typ);
  1823. msg_set_net_plane(msg, l_ptr->b_ptr->net_plane);
  1824. msg_set_bcast_ack(msg, mod(l_ptr->owner->bclink.last_in));
  1825. msg_set_last_bcast(msg, tipc_bclink_get_last_sent());
  1826. if (msg_typ == STATE_MSG) {
  1827. u32 next_sent = mod(l_ptr->next_out_no);
  1828. if (!tipc_link_is_up(l_ptr))
  1829. return;
  1830. if (l_ptr->next_out)
  1831. next_sent = msg_seqno(buf_msg(l_ptr->next_out));
  1832. msg_set_next_sent(msg, next_sent);
  1833. if (l_ptr->oldest_deferred_in) {
  1834. u32 rec = msg_seqno(buf_msg(l_ptr->oldest_deferred_in));
  1835. gap = mod(rec - mod(l_ptr->next_in_no));
  1836. }
  1837. msg_set_seq_gap(msg, gap);
  1838. if (gap)
  1839. l_ptr->stats.sent_nacks++;
  1840. msg_set_link_tolerance(msg, tolerance);
  1841. msg_set_linkprio(msg, priority);
  1842. msg_set_max_pkt(msg, ack_mtu);
  1843. msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
  1844. msg_set_probe(msg, probe_msg != 0);
  1845. if (probe_msg) {
  1846. u32 mtu = l_ptr->max_pkt;
  1847. if ((mtu < l_ptr->max_pkt_target) &&
  1848. link_working_working(l_ptr) &&
  1849. l_ptr->fsm_msg_cnt) {
  1850. msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
  1851. if (l_ptr->max_pkt_probes == 10) {
  1852. l_ptr->max_pkt_target = (msg_size - 4);
  1853. l_ptr->max_pkt_probes = 0;
  1854. msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
  1855. }
  1856. l_ptr->max_pkt_probes++;
  1857. }
  1858. l_ptr->stats.sent_probes++;
  1859. }
  1860. l_ptr->stats.sent_states++;
  1861. } else { /* RESET_MSG or ACTIVATE_MSG */
  1862. msg_set_ack(msg, mod(l_ptr->reset_checkpoint - 1));
  1863. msg_set_seq_gap(msg, 0);
  1864. msg_set_next_sent(msg, 1);
  1865. msg_set_link_tolerance(msg, l_ptr->tolerance);
  1866. msg_set_linkprio(msg, l_ptr->priority);
  1867. msg_set_max_pkt(msg, l_ptr->max_pkt_target);
  1868. }
  1869. if (tipc_node_has_redundant_links(l_ptr->owner)) {
  1870. msg_set_redundant_link(msg);
  1871. } else {
  1872. msg_clear_redundant_link(msg);
  1873. }
  1874. msg_set_linkprio(msg, l_ptr->priority);
  1875. /* Ensure sequence number will not fit : */
  1876. msg_set_seqno(msg, mod(l_ptr->next_out_no + (0xffff/2)));
  1877. /* Congestion? */
  1878. if (tipc_bearer_congested(l_ptr->b_ptr, l_ptr)) {
  1879. if (!l_ptr->proto_msg_queue) {
  1880. l_ptr->proto_msg_queue =
  1881. buf_acquire(sizeof(l_ptr->proto_msg));
  1882. }
  1883. buf = l_ptr->proto_msg_queue;
  1884. if (!buf)
  1885. return;
  1886. skb_copy_to_linear_data(buf, msg, sizeof(l_ptr->proto_msg));
  1887. return;
  1888. }
  1889. msg_set_timestamp(msg, jiffies_to_msecs(jiffies));
  1890. /* Message can be sent */
  1891. msg_dbg(msg, ">>");
  1892. buf = buf_acquire(msg_size);
  1893. if (!buf)
  1894. return;
  1895. skb_copy_to_linear_data(buf, msg, sizeof(l_ptr->proto_msg));
  1896. msg_set_size(buf_msg(buf), msg_size);
  1897. if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
  1898. l_ptr->unacked_window = 0;
  1899. buf_discard(buf);
  1900. return;
  1901. }
  1902. /* New congestion */
  1903. tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
  1904. l_ptr->proto_msg_queue = buf;
  1905. l_ptr->stats.bearer_congs++;
  1906. }
  1907. /*
  1908. * Receive protocol message :
  1909. * Note that network plane id propagates through the network, and may
  1910. * change at any time. The node with lowest address rules
  1911. */
  1912. static void link_recv_proto_msg(struct link *l_ptr, struct sk_buff *buf)
  1913. {
  1914. u32 rec_gap = 0;
  1915. u32 max_pkt_info;
  1916. u32 max_pkt_ack;
  1917. u32 msg_tol;
  1918. struct tipc_msg *msg = buf_msg(buf);
  1919. dbg("AT(%u):", jiffies_to_msecs(jiffies));
  1920. msg_dbg(msg, "<<");
  1921. if (link_blocked(l_ptr))
  1922. goto exit;
  1923. /* record unnumbered packet arrival (force mismatch on next timeout) */
  1924. l_ptr->checkpoint--;
  1925. if (l_ptr->b_ptr->net_plane != msg_net_plane(msg))
  1926. if (tipc_own_addr > msg_prevnode(msg))
  1927. l_ptr->b_ptr->net_plane = msg_net_plane(msg);
  1928. l_ptr->owner->permit_changeover = msg_redundant_link(msg);
  1929. switch (msg_type(msg)) {
  1930. case RESET_MSG:
  1931. if (!link_working_unknown(l_ptr) &&
  1932. (l_ptr->peer_session != INVALID_SESSION)) {
  1933. if (msg_session(msg) == l_ptr->peer_session) {
  1934. dbg("Duplicate RESET: %u<->%u\n",
  1935. msg_session(msg), l_ptr->peer_session);
  1936. break; /* duplicate: ignore */
  1937. }
  1938. }
  1939. /* fall thru' */
  1940. case ACTIVATE_MSG:
  1941. /* Update link settings according other endpoint's values */
  1942. strcpy((strrchr(l_ptr->name, ':') + 1), (char *)msg_data(msg));
  1943. if ((msg_tol = msg_link_tolerance(msg)) &&
  1944. (msg_tol > l_ptr->tolerance))
  1945. link_set_supervision_props(l_ptr, msg_tol);
  1946. if (msg_linkprio(msg) > l_ptr->priority)
  1947. l_ptr->priority = msg_linkprio(msg);
  1948. max_pkt_info = msg_max_pkt(msg);
  1949. if (max_pkt_info) {
  1950. if (max_pkt_info < l_ptr->max_pkt_target)
  1951. l_ptr->max_pkt_target = max_pkt_info;
  1952. if (l_ptr->max_pkt > l_ptr->max_pkt_target)
  1953. l_ptr->max_pkt = l_ptr->max_pkt_target;
  1954. } else {
  1955. l_ptr->max_pkt = l_ptr->max_pkt_target;
  1956. }
  1957. l_ptr->owner->bclink.supported = (max_pkt_info != 0);
  1958. link_state_event(l_ptr, msg_type(msg));
  1959. l_ptr->peer_session = msg_session(msg);
  1960. l_ptr->peer_bearer_id = msg_bearer_id(msg);
  1961. /* Synchronize broadcast sequence numbers */
  1962. if (!tipc_node_has_redundant_links(l_ptr->owner)) {
  1963. l_ptr->owner->bclink.last_in = mod(msg_last_bcast(msg));
  1964. }
  1965. break;
  1966. case STATE_MSG:
  1967. if ((msg_tol = msg_link_tolerance(msg)))
  1968. link_set_supervision_props(l_ptr, msg_tol);
  1969. if (msg_linkprio(msg) &&
  1970. (msg_linkprio(msg) != l_ptr->priority)) {
  1971. warn("Resetting link <%s>, priority change %u->%u\n",
  1972. l_ptr->name, l_ptr->priority, msg_linkprio(msg));
  1973. l_ptr->priority = msg_linkprio(msg);
  1974. tipc_link_reset(l_ptr); /* Enforce change to take effect */
  1975. break;
  1976. }
  1977. link_state_event(l_ptr, TRAFFIC_MSG_EVT);
  1978. l_ptr->stats.recv_states++;
  1979. if (link_reset_unknown(l_ptr))
  1980. break;
  1981. if (less_eq(mod(l_ptr->next_in_no), msg_next_sent(msg))) {
  1982. rec_gap = mod(msg_next_sent(msg) -
  1983. mod(l_ptr->next_in_no));
  1984. }
  1985. max_pkt_ack = msg_max_pkt(msg);
  1986. if (max_pkt_ack > l_ptr->max_pkt) {
  1987. dbg("Link <%s> updated MTU %u -> %u\n",
  1988. l_ptr->name, l_ptr->max_pkt, max_pkt_ack);
  1989. l_ptr->max_pkt = max_pkt_ack;
  1990. l_ptr->max_pkt_probes = 0;
  1991. }
  1992. max_pkt_ack = 0;
  1993. if (msg_probe(msg)) {
  1994. l_ptr->stats.recv_probes++;
  1995. if (msg_size(msg) > sizeof(l_ptr->proto_msg)) {
  1996. max_pkt_ack = msg_size(msg);
  1997. }
  1998. }
  1999. /* Protocol message before retransmits, reduce loss risk */
  2000. tipc_bclink_check_gap(l_ptr->owner, msg_last_bcast(msg));
  2001. if (rec_gap || (msg_probe(msg))) {
  2002. tipc_link_send_proto_msg(l_ptr, STATE_MSG,
  2003. 0, rec_gap, 0, 0, max_pkt_ack);
  2004. }
  2005. if (msg_seq_gap(msg)) {
  2006. msg_dbg(msg, "With Gap:");
  2007. l_ptr->stats.recv_nacks++;
  2008. tipc_link_retransmit(l_ptr, l_ptr->first_out,
  2009. msg_seq_gap(msg));
  2010. }
  2011. break;
  2012. default:
  2013. msg_dbg(buf_msg(buf), "<DISCARDING UNKNOWN<");
  2014. }
  2015. exit:
  2016. buf_discard(buf);
  2017. }
  2018. /*
  2019. * tipc_link_tunnel(): Send one message via a link belonging to
  2020. * another bearer. Owner node is locked.
  2021. */
  2022. void tipc_link_tunnel(struct link *l_ptr,
  2023. struct tipc_msg *tunnel_hdr,
  2024. struct tipc_msg *msg,
  2025. u32 selector)
  2026. {
  2027. struct link *tunnel;
  2028. struct sk_buff *buf;
  2029. u32 length = msg_size(msg);
  2030. tunnel = l_ptr->owner->active_links[selector & 1];
  2031. if (!tipc_link_is_up(tunnel)) {
  2032. warn("Link changeover error, "
  2033. "tunnel link no longer available\n");
  2034. return;
  2035. }
  2036. msg_set_size(tunnel_hdr, length + INT_H_SIZE);
  2037. buf = buf_acquire(length + INT_H_SIZE);
  2038. if (!buf) {
  2039. warn("Link changeover error, "
  2040. "unable to send tunnel msg\n");
  2041. return;
  2042. }
  2043. skb_copy_to_linear_data(buf, tunnel_hdr, INT_H_SIZE);
  2044. skb_copy_to_linear_data_offset(buf, INT_H_SIZE, msg, length);
  2045. dbg("%c->%c:", l_ptr->b_ptr->net_plane, tunnel->b_ptr->net_plane);
  2046. msg_dbg(buf_msg(buf), ">SEND>");
  2047. tipc_link_send_buf(tunnel, buf);
  2048. }
  2049. /*
  2050. * changeover(): Send whole message queue via the remaining link
  2051. * Owner node is locked.
  2052. */
  2053. void tipc_link_changeover(struct link *l_ptr)
  2054. {
  2055. u32 msgcount = l_ptr->out_queue_size;
  2056. struct sk_buff *crs = l_ptr->first_out;
  2057. struct link *tunnel = l_ptr->owner->active_links[0];
  2058. struct tipc_msg tunnel_hdr;
  2059. int split_bundles;
  2060. if (!tunnel)
  2061. return;
  2062. if (!l_ptr->owner->permit_changeover) {
  2063. warn("Link changeover error, "
  2064. "peer did not permit changeover\n");
  2065. return;
  2066. }
  2067. tipc_msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
  2068. ORIGINAL_MSG, INT_H_SIZE, l_ptr->addr);
  2069. msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
  2070. msg_set_msgcnt(&tunnel_hdr, msgcount);
  2071. dbg("Link changeover requires %u tunnel messages\n", msgcount);
  2072. if (!l_ptr->first_out) {
  2073. struct sk_buff *buf;
  2074. buf = buf_acquire(INT_H_SIZE);
  2075. if (buf) {
  2076. skb_copy_to_linear_data(buf, &tunnel_hdr, INT_H_SIZE);
  2077. msg_set_size(&tunnel_hdr, INT_H_SIZE);
  2078. dbg("%c->%c:", l_ptr->b_ptr->net_plane,
  2079. tunnel->b_ptr->net_plane);
  2080. msg_dbg(&tunnel_hdr, "EMPTY>SEND>");
  2081. tipc_link_send_buf(tunnel, buf);
  2082. } else {
  2083. warn("Link changeover error, "
  2084. "unable to send changeover msg\n");
  2085. }
  2086. return;
  2087. }
  2088. split_bundles = (l_ptr->owner->active_links[0] !=
  2089. l_ptr->owner->active_links[1]);
  2090. while (crs) {
  2091. struct tipc_msg *msg = buf_msg(crs);
  2092. if ((msg_user(msg) == MSG_BUNDLER) && split_bundles) {
  2093. struct tipc_msg *m = msg_get_wrapped(msg);
  2094. unchar* pos = (unchar*)m;
  2095. msgcount = msg_msgcnt(msg);
  2096. while (msgcount--) {
  2097. msg_set_seqno(m,msg_seqno(msg));
  2098. tipc_link_tunnel(l_ptr, &tunnel_hdr, m,
  2099. msg_link_selector(m));
  2100. pos += align(msg_size(m));
  2101. m = (struct tipc_msg *)pos;
  2102. }
  2103. } else {
  2104. tipc_link_tunnel(l_ptr, &tunnel_hdr, msg,
  2105. msg_link_selector(msg));
  2106. }
  2107. crs = crs->next;
  2108. }
  2109. }
  2110. void tipc_link_send_duplicate(struct link *l_ptr, struct link *tunnel)
  2111. {
  2112. struct sk_buff *iter;
  2113. struct tipc_msg tunnel_hdr;
  2114. tipc_msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
  2115. DUPLICATE_MSG, INT_H_SIZE, l_ptr->addr);
  2116. msg_set_msgcnt(&tunnel_hdr, l_ptr->out_queue_size);
  2117. msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
  2118. iter = l_ptr->first_out;
  2119. while (iter) {
  2120. struct sk_buff *outbuf;
  2121. struct tipc_msg *msg = buf_msg(iter);
  2122. u32 length = msg_size(msg);
  2123. if (msg_user(msg) == MSG_BUNDLER)
  2124. msg_set_type(msg, CLOSED_MSG);
  2125. msg_set_ack(msg, mod(l_ptr->next_in_no - 1)); /* Update */
  2126. msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
  2127. msg_set_size(&tunnel_hdr, length + INT_H_SIZE);
  2128. outbuf = buf_acquire(length + INT_H_SIZE);
  2129. if (outbuf == NULL) {
  2130. warn("Link changeover error, "
  2131. "unable to send duplicate msg\n");
  2132. return;
  2133. }
  2134. skb_copy_to_linear_data(outbuf, &tunnel_hdr, INT_H_SIZE);
  2135. skb_copy_to_linear_data_offset(outbuf, INT_H_SIZE, iter->data,
  2136. length);
  2137. dbg("%c->%c:", l_ptr->b_ptr->net_plane,
  2138. tunnel->b_ptr->net_plane);
  2139. msg_dbg(buf_msg(outbuf), ">SEND>");
  2140. tipc_link_send_buf(tunnel, outbuf);
  2141. if (!tipc_link_is_up(l_ptr))
  2142. return;
  2143. iter = iter->next;
  2144. }
  2145. }
  2146. /**
  2147. * buf_extract - extracts embedded TIPC message from another message
  2148. * @skb: encapsulating message buffer
  2149. * @from_pos: offset to extract from
  2150. *
  2151. * Returns a new message buffer containing an embedded message. The
  2152. * encapsulating message itself is left unchanged.
  2153. */
  2154. static struct sk_buff *buf_extract(struct sk_buff *skb, u32 from_pos)
  2155. {
  2156. struct tipc_msg *msg = (struct tipc_msg *)(skb->data + from_pos);
  2157. u32 size = msg_size(msg);
  2158. struct sk_buff *eb;
  2159. eb = buf_acquire(size);
  2160. if (eb)
  2161. skb_copy_to_linear_data(eb, msg, size);
  2162. return eb;
  2163. }
  2164. /*
  2165. * link_recv_changeover_msg(): Receive tunneled packet sent
  2166. * via other link. Node is locked. Return extracted buffer.
  2167. */
  2168. static int link_recv_changeover_msg(struct link **l_ptr,
  2169. struct sk_buff **buf)
  2170. {
  2171. struct sk_buff *tunnel_buf = *buf;
  2172. struct link *dest_link;
  2173. struct tipc_msg *msg;
  2174. struct tipc_msg *tunnel_msg = buf_msg(tunnel_buf);
  2175. u32 msg_typ = msg_type(tunnel_msg);
  2176. u32 msg_count = msg_msgcnt(tunnel_msg);
  2177. dest_link = (*l_ptr)->owner->links[msg_bearer_id(tunnel_msg)];
  2178. if (!dest_link) {
  2179. msg_dbg(tunnel_msg, "NOLINK/<REC<");
  2180. goto exit;
  2181. }
  2182. if (dest_link == *l_ptr) {
  2183. err("Unexpected changeover message on link <%s>\n",
  2184. (*l_ptr)->name);
  2185. goto exit;
  2186. }
  2187. dbg("%c<-%c:", dest_link->b_ptr->net_plane,
  2188. (*l_ptr)->b_ptr->net_plane);
  2189. *l_ptr = dest_link;
  2190. msg = msg_get_wrapped(tunnel_msg);
  2191. if (msg_typ == DUPLICATE_MSG) {
  2192. if (less(msg_seqno(msg), mod(dest_link->next_in_no))) {
  2193. msg_dbg(tunnel_msg, "DROP/<REC<");
  2194. goto exit;
  2195. }
  2196. *buf = buf_extract(tunnel_buf,INT_H_SIZE);
  2197. if (*buf == NULL) {
  2198. warn("Link changeover error, duplicate msg dropped\n");
  2199. goto exit;
  2200. }
  2201. msg_dbg(tunnel_msg, "TNL<REC<");
  2202. buf_discard(tunnel_buf);
  2203. return 1;
  2204. }
  2205. /* First original message ?: */
  2206. if (tipc_link_is_up(dest_link)) {
  2207. msg_dbg(tunnel_msg, "UP/FIRST/<REC<");
  2208. info("Resetting link <%s>, changeover initiated by peer\n",
  2209. dest_link->name);
  2210. tipc_link_reset(dest_link);
  2211. dest_link->exp_msg_count = msg_count;
  2212. dbg("Expecting %u tunnelled messages\n", msg_count);
  2213. if (!msg_count)
  2214. goto exit;
  2215. } else if (dest_link->exp_msg_count == START_CHANGEOVER) {
  2216. msg_dbg(tunnel_msg, "BLK/FIRST/<REC<");
  2217. dest_link->exp_msg_count = msg_count;
  2218. dbg("Expecting %u tunnelled messages\n", msg_count);
  2219. if (!msg_count)
  2220. goto exit;
  2221. }
  2222. /* Receive original message */
  2223. if (dest_link->exp_msg_count == 0) {
  2224. warn("Link switchover error, "
  2225. "got too many tunnelled messages\n");
  2226. msg_dbg(tunnel_msg, "OVERDUE/DROP/<REC<");
  2227. dbg_print_link(dest_link, "LINK:");
  2228. goto exit;
  2229. }
  2230. dest_link->exp_msg_count--;
  2231. if (less(msg_seqno(msg), dest_link->reset_checkpoint)) {
  2232. msg_dbg(tunnel_msg, "DROP/DUPL/<REC<");
  2233. goto exit;
  2234. } else {
  2235. *buf = buf_extract(tunnel_buf, INT_H_SIZE);
  2236. if (*buf != NULL) {
  2237. msg_dbg(tunnel_msg, "TNL<REC<");
  2238. buf_discard(tunnel_buf);
  2239. return 1;
  2240. } else {
  2241. warn("Link changeover error, original msg dropped\n");
  2242. }
  2243. }
  2244. exit:
  2245. *buf = NULL;
  2246. buf_discard(tunnel_buf);
  2247. return 0;
  2248. }
  2249. /*
  2250. * Bundler functionality:
  2251. */
  2252. void tipc_link_recv_bundle(struct sk_buff *buf)
  2253. {
  2254. u32 msgcount = msg_msgcnt(buf_msg(buf));
  2255. u32 pos = INT_H_SIZE;
  2256. struct sk_buff *obuf;
  2257. msg_dbg(buf_msg(buf), "<BNDL<: ");
  2258. while (msgcount--) {
  2259. obuf = buf_extract(buf, pos);
  2260. if (obuf == NULL) {
  2261. warn("Link unable to unbundle message(s)\n");
  2262. break;
  2263. }
  2264. pos += align(msg_size(buf_msg(obuf)));
  2265. msg_dbg(buf_msg(obuf), " /");
  2266. tipc_net_route_msg(obuf);
  2267. }
  2268. buf_discard(buf);
  2269. }
  2270. /*
  2271. * Fragmentation/defragmentation:
  2272. */
  2273. /*
  2274. * tipc_link_send_long_buf: Entry for buffers needing fragmentation.
  2275. * The buffer is complete, inclusive total message length.
  2276. * Returns user data length.
  2277. */
  2278. int tipc_link_send_long_buf(struct link *l_ptr, struct sk_buff *buf)
  2279. {
  2280. struct tipc_msg *inmsg = buf_msg(buf);
  2281. struct tipc_msg fragm_hdr;
  2282. u32 insize = msg_size(inmsg);
  2283. u32 dsz = msg_data_sz(inmsg);
  2284. unchar *crs = buf->data;
  2285. u32 rest = insize;
  2286. u32 pack_sz = l_ptr->max_pkt;
  2287. u32 fragm_sz = pack_sz - INT_H_SIZE;
  2288. u32 fragm_no = 1;
  2289. u32 destaddr;
  2290. if (msg_short(inmsg))
  2291. destaddr = l_ptr->addr;
  2292. else
  2293. destaddr = msg_destnode(inmsg);
  2294. if (msg_routed(inmsg))
  2295. msg_set_prevnode(inmsg, tipc_own_addr);
  2296. /* Prepare reusable fragment header: */
  2297. tipc_msg_init(&fragm_hdr, MSG_FRAGMENTER, FIRST_FRAGMENT,
  2298. INT_H_SIZE, destaddr);
  2299. msg_set_link_selector(&fragm_hdr, msg_link_selector(inmsg));
  2300. msg_set_long_msgno(&fragm_hdr, mod(l_ptr->long_msg_seq_no++));
  2301. msg_set_fragm_no(&fragm_hdr, fragm_no);
  2302. l_ptr->stats.sent_fragmented++;
  2303. /* Chop up message: */
  2304. while (rest > 0) {
  2305. struct sk_buff *fragm;
  2306. if (rest <= fragm_sz) {
  2307. fragm_sz = rest;
  2308. msg_set_type(&fragm_hdr, LAST_FRAGMENT);
  2309. }
  2310. fragm = buf_acquire(fragm_sz + INT_H_SIZE);
  2311. if (fragm == NULL) {
  2312. warn("Link unable to fragment message\n");
  2313. dsz = -ENOMEM;
  2314. goto exit;
  2315. }
  2316. msg_set_size(&fragm_hdr, fragm_sz + INT_H_SIZE);
  2317. skb_copy_to_linear_data(fragm, &fragm_hdr, INT_H_SIZE);
  2318. skb_copy_to_linear_data_offset(fragm, INT_H_SIZE, crs,
  2319. fragm_sz);
  2320. /* Send queued messages first, if any: */
  2321. l_ptr->stats.sent_fragments++;
  2322. tipc_link_send_buf(l_ptr, fragm);
  2323. if (!tipc_link_is_up(l_ptr))
  2324. return dsz;
  2325. msg_set_fragm_no(&fragm_hdr, ++fragm_no);
  2326. rest -= fragm_sz;
  2327. crs += fragm_sz;
  2328. msg_set_type(&fragm_hdr, FRAGMENT);
  2329. }
  2330. exit:
  2331. buf_discard(buf);
  2332. return dsz;
  2333. }
  2334. /*
  2335. * A pending message being re-assembled must store certain values
  2336. * to handle subsequent fragments correctly. The following functions
  2337. * help storing these values in unused, available fields in the
  2338. * pending message. This makes dynamic memory allocation unecessary.
  2339. */
  2340. static void set_long_msg_seqno(struct sk_buff *buf, u32 seqno)
  2341. {
  2342. msg_set_seqno(buf_msg(buf), seqno);
  2343. }
  2344. static u32 get_fragm_size(struct sk_buff *buf)
  2345. {
  2346. return msg_ack(buf_msg(buf));
  2347. }
  2348. static void set_fragm_size(struct sk_buff *buf, u32 sz)
  2349. {
  2350. msg_set_ack(buf_msg(buf), sz);
  2351. }
  2352. static u32 get_expected_frags(struct sk_buff *buf)
  2353. {
  2354. return msg_bcast_ack(buf_msg(buf));
  2355. }
  2356. static void set_expected_frags(struct sk_buff *buf, u32 exp)
  2357. {
  2358. msg_set_bcast_ack(buf_msg(buf), exp);
  2359. }
  2360. static u32 get_timer_cnt(struct sk_buff *buf)
  2361. {
  2362. return msg_reroute_cnt(buf_msg(buf));
  2363. }
  2364. static void incr_timer_cnt(struct sk_buff *buf)
  2365. {
  2366. msg_incr_reroute_cnt(buf_msg(buf));
  2367. }
  2368. /*
  2369. * tipc_link_recv_fragment(): Called with node lock on. Returns
  2370. * the reassembled buffer if message is complete.
  2371. */
  2372. int tipc_link_recv_fragment(struct sk_buff **pending, struct sk_buff **fb,
  2373. struct tipc_msg **m)
  2374. {
  2375. struct sk_buff *prev = NULL;
  2376. struct sk_buff *fbuf = *fb;
  2377. struct tipc_msg *fragm = buf_msg(fbuf);
  2378. struct sk_buff *pbuf = *pending;
  2379. u32 long_msg_seq_no = msg_long_msgno(fragm);
  2380. *fb = NULL;
  2381. msg_dbg(fragm,"FRG<REC<");
  2382. /* Is there an incomplete message waiting for this fragment? */
  2383. while (pbuf && ((msg_seqno(buf_msg(pbuf)) != long_msg_seq_no) ||
  2384. (msg_orignode(fragm) != msg_orignode(buf_msg(pbuf))))) {
  2385. prev = pbuf;
  2386. pbuf = pbuf->next;
  2387. }
  2388. if (!pbuf && (msg_type(fragm) == FIRST_FRAGMENT)) {
  2389. struct tipc_msg *imsg = (struct tipc_msg *)msg_data(fragm);
  2390. u32 msg_sz = msg_size(imsg);
  2391. u32 fragm_sz = msg_data_sz(fragm);
  2392. u32 exp_fragm_cnt = msg_sz/fragm_sz + !!(msg_sz % fragm_sz);
  2393. u32 max = TIPC_MAX_USER_MSG_SIZE + LONG_H_SIZE;
  2394. if (msg_type(imsg) == TIPC_MCAST_MSG)
  2395. max = TIPC_MAX_USER_MSG_SIZE + MCAST_H_SIZE;
  2396. if (msg_size(imsg) > max) {
  2397. msg_dbg(fragm,"<REC<Oversized: ");
  2398. buf_discard(fbuf);
  2399. return 0;
  2400. }
  2401. pbuf = buf_acquire(msg_size(imsg));
  2402. if (pbuf != NULL) {
  2403. pbuf->next = *pending;
  2404. *pending = pbuf;
  2405. skb_copy_to_linear_data(pbuf, imsg,
  2406. msg_data_sz(fragm));
  2407. /* Prepare buffer for subsequent fragments. */
  2408. set_long_msg_seqno(pbuf, long_msg_seq_no);
  2409. set_fragm_size(pbuf,fragm_sz);
  2410. set_expected_frags(pbuf,exp_fragm_cnt - 1);
  2411. } else {
  2412. warn("Link unable to reassemble fragmented message\n");
  2413. }
  2414. buf_discard(fbuf);
  2415. return 0;
  2416. } else if (pbuf && (msg_type(fragm) != FIRST_FRAGMENT)) {
  2417. u32 dsz = msg_data_sz(fragm);
  2418. u32 fsz = get_fragm_size(pbuf);
  2419. u32 crs = ((msg_fragm_no(fragm) - 1) * fsz);
  2420. u32 exp_frags = get_expected_frags(pbuf) - 1;
  2421. skb_copy_to_linear_data_offset(pbuf, crs,
  2422. msg_data(fragm), dsz);
  2423. buf_discard(fbuf);
  2424. /* Is message complete? */
  2425. if (exp_frags == 0) {
  2426. if (prev)
  2427. prev->next = pbuf->next;
  2428. else
  2429. *pending = pbuf->next;
  2430. msg_reset_reroute_cnt(buf_msg(pbuf));
  2431. *fb = pbuf;
  2432. *m = buf_msg(pbuf);
  2433. return 1;
  2434. }
  2435. set_expected_frags(pbuf,exp_frags);
  2436. return 0;
  2437. }
  2438. dbg(" Discarding orphan fragment %x\n",fbuf);
  2439. msg_dbg(fragm,"ORPHAN:");
  2440. dbg("Pending long buffers:\n");
  2441. dbg_print_buf_chain(*pending);
  2442. buf_discard(fbuf);
  2443. return 0;
  2444. }
  2445. /**
  2446. * link_check_defragm_bufs - flush stale incoming message fragments
  2447. * @l_ptr: pointer to link
  2448. */
  2449. static void link_check_defragm_bufs(struct link *l_ptr)
  2450. {
  2451. struct sk_buff *prev = NULL;
  2452. struct sk_buff *next = NULL;
  2453. struct sk_buff *buf = l_ptr->defragm_buf;
  2454. if (!buf)
  2455. return;
  2456. if (!link_working_working(l_ptr))
  2457. return;
  2458. while (buf) {
  2459. u32 cnt = get_timer_cnt(buf);
  2460. next = buf->next;
  2461. if (cnt < 4) {
  2462. incr_timer_cnt(buf);
  2463. prev = buf;
  2464. } else {
  2465. dbg(" Discarding incomplete long buffer\n");
  2466. msg_dbg(buf_msg(buf), "LONG:");
  2467. dbg_print_link(l_ptr, "curr:");
  2468. dbg("Pending long buffers:\n");
  2469. dbg_print_buf_chain(l_ptr->defragm_buf);
  2470. if (prev)
  2471. prev->next = buf->next;
  2472. else
  2473. l_ptr->defragm_buf = buf->next;
  2474. buf_discard(buf);
  2475. }
  2476. buf = next;
  2477. }
  2478. }
  2479. static void link_set_supervision_props(struct link *l_ptr, u32 tolerance)
  2480. {
  2481. l_ptr->tolerance = tolerance;
  2482. l_ptr->continuity_interval =
  2483. ((tolerance / 4) > 500) ? 500 : tolerance / 4;
  2484. l_ptr->abort_limit = tolerance / (l_ptr->continuity_interval / 4);
  2485. }
  2486. void tipc_link_set_queue_limits(struct link *l_ptr, u32 window)
  2487. {
  2488. /* Data messages from this node, inclusive FIRST_FRAGM */
  2489. l_ptr->queue_limit[TIPC_LOW_IMPORTANCE] = window;
  2490. l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE] = (window / 3) * 4;
  2491. l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE] = (window / 3) * 5;
  2492. l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE] = (window / 3) * 6;
  2493. /* Transiting data messages,inclusive FIRST_FRAGM */
  2494. l_ptr->queue_limit[TIPC_LOW_IMPORTANCE + 4] = 300;
  2495. l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE + 4] = 600;
  2496. l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE + 4] = 900;
  2497. l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE + 4] = 1200;
  2498. l_ptr->queue_limit[CONN_MANAGER] = 1200;
  2499. l_ptr->queue_limit[ROUTE_DISTRIBUTOR] = 1200;
  2500. l_ptr->queue_limit[CHANGEOVER_PROTOCOL] = 2500;
  2501. l_ptr->queue_limit[NAME_DISTRIBUTOR] = 3000;
  2502. /* FRAGMENT and LAST_FRAGMENT packets */
  2503. l_ptr->queue_limit[MSG_FRAGMENTER] = 4000;
  2504. }
  2505. /**
  2506. * link_find_link - locate link by name
  2507. * @name - ptr to link name string
  2508. * @node - ptr to area to be filled with ptr to associated node
  2509. *
  2510. * Caller must hold 'tipc_net_lock' to ensure node and bearer are not deleted;
  2511. * this also prevents link deletion.
  2512. *
  2513. * Returns pointer to link (or 0 if invalid link name).
  2514. */
  2515. static struct link *link_find_link(const char *name, struct tipc_node **node)
  2516. {
  2517. struct link_name link_name_parts;
  2518. struct bearer *b_ptr;
  2519. struct link *l_ptr;
  2520. if (!link_name_validate(name, &link_name_parts))
  2521. return NULL;
  2522. b_ptr = tipc_bearer_find_interface(link_name_parts.if_local);
  2523. if (!b_ptr)
  2524. return NULL;
  2525. *node = tipc_node_find(link_name_parts.addr_peer);
  2526. if (!*node)
  2527. return NULL;
  2528. l_ptr = (*node)->links[b_ptr->identity];
  2529. if (!l_ptr || strcmp(l_ptr->name, name))
  2530. return NULL;
  2531. return l_ptr;
  2532. }
  2533. struct sk_buff *tipc_link_cmd_config(const void *req_tlv_area, int req_tlv_space,
  2534. u16 cmd)
  2535. {
  2536. struct tipc_link_config *args;
  2537. u32 new_value;
  2538. struct link *l_ptr;
  2539. struct tipc_node *node;
  2540. int res;
  2541. if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_CONFIG))
  2542. return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
  2543. args = (struct tipc_link_config *)TLV_DATA(req_tlv_area);
  2544. new_value = ntohl(args->value);
  2545. if (!strcmp(args->name, tipc_bclink_name)) {
  2546. if ((cmd == TIPC_CMD_SET_LINK_WINDOW) &&
  2547. (tipc_bclink_set_queue_limits(new_value) == 0))
  2548. return tipc_cfg_reply_none();
  2549. return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
  2550. " (cannot change setting on broadcast link)");
  2551. }
  2552. read_lock_bh(&tipc_net_lock);
  2553. l_ptr = link_find_link(args->name, &node);
  2554. if (!l_ptr) {
  2555. read_unlock_bh(&tipc_net_lock);
  2556. return tipc_cfg_reply_error_string("link not found");
  2557. }
  2558. tipc_node_lock(node);
  2559. res = -EINVAL;
  2560. switch (cmd) {
  2561. case TIPC_CMD_SET_LINK_TOL:
  2562. if ((new_value >= TIPC_MIN_LINK_TOL) &&
  2563. (new_value <= TIPC_MAX_LINK_TOL)) {
  2564. link_set_supervision_props(l_ptr, new_value);
  2565. tipc_link_send_proto_msg(l_ptr, STATE_MSG,
  2566. 0, 0, new_value, 0, 0);
  2567. res = 0;
  2568. }
  2569. break;
  2570. case TIPC_CMD_SET_LINK_PRI:
  2571. if ((new_value >= TIPC_MIN_LINK_PRI) &&
  2572. (new_value <= TIPC_MAX_LINK_PRI)) {
  2573. l_ptr->priority = new_value;
  2574. tipc_link_send_proto_msg(l_ptr, STATE_MSG,
  2575. 0, 0, 0, new_value, 0);
  2576. res = 0;
  2577. }
  2578. break;
  2579. case TIPC_CMD_SET_LINK_WINDOW:
  2580. if ((new_value >= TIPC_MIN_LINK_WIN) &&
  2581. (new_value <= TIPC_MAX_LINK_WIN)) {
  2582. tipc_link_set_queue_limits(l_ptr, new_value);
  2583. res = 0;
  2584. }
  2585. break;
  2586. }
  2587. tipc_node_unlock(node);
  2588. read_unlock_bh(&tipc_net_lock);
  2589. if (res)
  2590. return tipc_cfg_reply_error_string("cannot change link setting");
  2591. return tipc_cfg_reply_none();
  2592. }
  2593. /**
  2594. * link_reset_statistics - reset link statistics
  2595. * @l_ptr: pointer to link
  2596. */
  2597. static void link_reset_statistics(struct link *l_ptr)
  2598. {
  2599. memset(&l_ptr->stats, 0, sizeof(l_ptr->stats));
  2600. l_ptr->stats.sent_info = l_ptr->next_out_no;
  2601. l_ptr->stats.recv_info = l_ptr->next_in_no;
  2602. }
  2603. struct sk_buff *tipc_link_cmd_reset_stats(const void *req_tlv_area, int req_tlv_space)
  2604. {
  2605. char *link_name;
  2606. struct link *l_ptr;
  2607. struct tipc_node *node;
  2608. if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
  2609. return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
  2610. link_name = (char *)TLV_DATA(req_tlv_area);
  2611. if (!strcmp(link_name, tipc_bclink_name)) {
  2612. if (tipc_bclink_reset_stats())
  2613. return tipc_cfg_reply_error_string("link not found");
  2614. return tipc_cfg_reply_none();
  2615. }
  2616. read_lock_bh(&tipc_net_lock);
  2617. l_ptr = link_find_link(link_name, &node);
  2618. if (!l_ptr) {
  2619. read_unlock_bh(&tipc_net_lock);
  2620. return tipc_cfg_reply_error_string("link not found");
  2621. }
  2622. tipc_node_lock(node);
  2623. link_reset_statistics(l_ptr);
  2624. tipc_node_unlock(node);
  2625. read_unlock_bh(&tipc_net_lock);
  2626. return tipc_cfg_reply_none();
  2627. }
  2628. /**
  2629. * percent - convert count to a percentage of total (rounding up or down)
  2630. */
  2631. static u32 percent(u32 count, u32 total)
  2632. {
  2633. return (count * 100 + (total / 2)) / total;
  2634. }
  2635. /**
  2636. * tipc_link_stats - print link statistics
  2637. * @name: link name
  2638. * @buf: print buffer area
  2639. * @buf_size: size of print buffer area
  2640. *
  2641. * Returns length of print buffer data string (or 0 if error)
  2642. */
  2643. static int tipc_link_stats(const char *name, char *buf, const u32 buf_size)
  2644. {
  2645. struct print_buf pb;
  2646. struct link *l_ptr;
  2647. struct tipc_node *node;
  2648. char *status;
  2649. u32 profile_total = 0;
  2650. if (!strcmp(name, tipc_bclink_name))
  2651. return tipc_bclink_stats(buf, buf_size);
  2652. tipc_printbuf_init(&pb, buf, buf_size);
  2653. read_lock_bh(&tipc_net_lock);
  2654. l_ptr = link_find_link(name, &node);
  2655. if (!l_ptr) {
  2656. read_unlock_bh(&tipc_net_lock);
  2657. return 0;
  2658. }
  2659. tipc_node_lock(node);
  2660. if (tipc_link_is_active(l_ptr))
  2661. status = "ACTIVE";
  2662. else if (tipc_link_is_up(l_ptr))
  2663. status = "STANDBY";
  2664. else
  2665. status = "DEFUNCT";
  2666. tipc_printf(&pb, "Link <%s>\n"
  2667. " %s MTU:%u Priority:%u Tolerance:%u ms"
  2668. " Window:%u packets\n",
  2669. l_ptr->name, status, l_ptr->max_pkt,
  2670. l_ptr->priority, l_ptr->tolerance, l_ptr->queue_limit[0]);
  2671. tipc_printf(&pb, " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
  2672. l_ptr->next_in_no - l_ptr->stats.recv_info,
  2673. l_ptr->stats.recv_fragments,
  2674. l_ptr->stats.recv_fragmented,
  2675. l_ptr->stats.recv_bundles,
  2676. l_ptr->stats.recv_bundled);
  2677. tipc_printf(&pb, " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
  2678. l_ptr->next_out_no - l_ptr->stats.sent_info,
  2679. l_ptr->stats.sent_fragments,
  2680. l_ptr->stats.sent_fragmented,
  2681. l_ptr->stats.sent_bundles,
  2682. l_ptr->stats.sent_bundled);
  2683. profile_total = l_ptr->stats.msg_length_counts;
  2684. if (!profile_total)
  2685. profile_total = 1;
  2686. tipc_printf(&pb, " TX profile sample:%u packets average:%u octets\n"
  2687. " 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% "
  2688. "-16354:%u%% -32768:%u%% -66000:%u%%\n",
  2689. l_ptr->stats.msg_length_counts,
  2690. l_ptr->stats.msg_lengths_total / profile_total,
  2691. percent(l_ptr->stats.msg_length_profile[0], profile_total),
  2692. percent(l_ptr->stats.msg_length_profile[1], profile_total),
  2693. percent(l_ptr->stats.msg_length_profile[2], profile_total),
  2694. percent(l_ptr->stats.msg_length_profile[3], profile_total),
  2695. percent(l_ptr->stats.msg_length_profile[4], profile_total),
  2696. percent(l_ptr->stats.msg_length_profile[5], profile_total),
  2697. percent(l_ptr->stats.msg_length_profile[6], profile_total));
  2698. tipc_printf(&pb, " RX states:%u probes:%u naks:%u defs:%u dups:%u\n",
  2699. l_ptr->stats.recv_states,
  2700. l_ptr->stats.recv_probes,
  2701. l_ptr->stats.recv_nacks,
  2702. l_ptr->stats.deferred_recv,
  2703. l_ptr->stats.duplicates);
  2704. tipc_printf(&pb, " TX states:%u probes:%u naks:%u acks:%u dups:%u\n",
  2705. l_ptr->stats.sent_states,
  2706. l_ptr->stats.sent_probes,
  2707. l_ptr->stats.sent_nacks,
  2708. l_ptr->stats.sent_acks,
  2709. l_ptr->stats.retransmitted);
  2710. tipc_printf(&pb, " Congestion bearer:%u link:%u Send queue max:%u avg:%u\n",
  2711. l_ptr->stats.bearer_congs,
  2712. l_ptr->stats.link_congs,
  2713. l_ptr->stats.max_queue_sz,
  2714. l_ptr->stats.queue_sz_counts
  2715. ? (l_ptr->stats.accu_queue_sz / l_ptr->stats.queue_sz_counts)
  2716. : 0);
  2717. tipc_node_unlock(node);
  2718. read_unlock_bh(&tipc_net_lock);
  2719. return tipc_printbuf_validate(&pb);
  2720. }
  2721. #define MAX_LINK_STATS_INFO 2000
  2722. struct sk_buff *tipc_link_cmd_show_stats(const void *req_tlv_area, int req_tlv_space)
  2723. {
  2724. struct sk_buff *buf;
  2725. struct tlv_desc *rep_tlv;
  2726. int str_len;
  2727. if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
  2728. return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
  2729. buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_LINK_STATS_INFO));
  2730. if (!buf)
  2731. return NULL;
  2732. rep_tlv = (struct tlv_desc *)buf->data;
  2733. str_len = tipc_link_stats((char *)TLV_DATA(req_tlv_area),
  2734. (char *)TLV_DATA(rep_tlv), MAX_LINK_STATS_INFO);
  2735. if (!str_len) {
  2736. buf_discard(buf);
  2737. return tipc_cfg_reply_error_string("link not found");
  2738. }
  2739. skb_put(buf, TLV_SPACE(str_len));
  2740. TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
  2741. return buf;
  2742. }
  2743. #if 0
  2744. int link_control(const char *name, u32 op, u32 val)
  2745. {
  2746. int res = -EINVAL;
  2747. struct link *l_ptr;
  2748. u32 bearer_id;
  2749. struct tipc_node * node;
  2750. u32 a;
  2751. a = link_name2addr(name, &bearer_id);
  2752. read_lock_bh(&tipc_net_lock);
  2753. node = tipc_node_find(a);
  2754. if (node) {
  2755. tipc_node_lock(node);
  2756. l_ptr = node->links[bearer_id];
  2757. if (l_ptr) {
  2758. if (op == TIPC_REMOVE_LINK) {
  2759. struct bearer *b_ptr = l_ptr->b_ptr;
  2760. spin_lock_bh(&b_ptr->publ.lock);
  2761. tipc_link_delete(l_ptr);
  2762. spin_unlock_bh(&b_ptr->publ.lock);
  2763. }
  2764. if (op == TIPC_CMD_BLOCK_LINK) {
  2765. tipc_link_reset(l_ptr);
  2766. l_ptr->blocked = 1;
  2767. }
  2768. if (op == TIPC_CMD_UNBLOCK_LINK) {
  2769. l_ptr->blocked = 0;
  2770. }
  2771. res = 0;
  2772. }
  2773. tipc_node_unlock(node);
  2774. }
  2775. read_unlock_bh(&tipc_net_lock);
  2776. return res;
  2777. }
  2778. #endif
  2779. /**
  2780. * tipc_link_get_max_pkt - get maximum packet size to use when sending to destination
  2781. * @dest: network address of destination node
  2782. * @selector: used to select from set of active links
  2783. *
  2784. * If no active link can be found, uses default maximum packet size.
  2785. */
  2786. u32 tipc_link_get_max_pkt(u32 dest, u32 selector)
  2787. {
  2788. struct tipc_node *n_ptr;
  2789. struct link *l_ptr;
  2790. u32 res = MAX_PKT_DEFAULT;
  2791. if (dest == tipc_own_addr)
  2792. return MAX_MSG_SIZE;
  2793. read_lock_bh(&tipc_net_lock);
  2794. n_ptr = tipc_node_select(dest, selector);
  2795. if (n_ptr) {
  2796. tipc_node_lock(n_ptr);
  2797. l_ptr = n_ptr->active_links[selector & 1];
  2798. if (l_ptr)
  2799. res = l_ptr->max_pkt;
  2800. tipc_node_unlock(n_ptr);
  2801. }
  2802. read_unlock_bh(&tipc_net_lock);
  2803. return res;
  2804. }
  2805. #if 0
  2806. static void link_dump_rec_queue(struct link *l_ptr)
  2807. {
  2808. struct sk_buff *crs;
  2809. if (!l_ptr->oldest_deferred_in) {
  2810. info("Reception queue empty\n");
  2811. return;
  2812. }
  2813. info("Contents of Reception queue:\n");
  2814. crs = l_ptr->oldest_deferred_in;
  2815. while (crs) {
  2816. if (crs->data == (void *)0x0000a3a3) {
  2817. info("buffer %x invalid\n", crs);
  2818. return;
  2819. }
  2820. msg_dbg(buf_msg(crs), "In rec queue:\n");
  2821. crs = crs->next;
  2822. }
  2823. }
  2824. #endif
  2825. static void link_dump_send_queue(struct link *l_ptr)
  2826. {
  2827. if (l_ptr->next_out) {
  2828. info("\nContents of unsent queue:\n");
  2829. dbg_print_buf_chain(l_ptr->next_out);
  2830. }
  2831. info("\nContents of send queue:\n");
  2832. if (l_ptr->first_out) {
  2833. dbg_print_buf_chain(l_ptr->first_out);
  2834. }
  2835. info("Empty send queue\n");
  2836. }
  2837. static void link_print(struct link *l_ptr, struct print_buf *buf,
  2838. const char *str)
  2839. {
  2840. tipc_printf(buf, str);
  2841. if (link_reset_reset(l_ptr) || link_reset_unknown(l_ptr))
  2842. return;
  2843. tipc_printf(buf, "Link %x<%s>:",
  2844. l_ptr->addr, l_ptr->b_ptr->publ.name);
  2845. tipc_printf(buf, ": NXO(%u):", mod(l_ptr->next_out_no));
  2846. tipc_printf(buf, "NXI(%u):", mod(l_ptr->next_in_no));
  2847. tipc_printf(buf, "SQUE");
  2848. if (l_ptr->first_out) {
  2849. tipc_printf(buf, "[%u..", msg_seqno(buf_msg(l_ptr->first_out)));
  2850. if (l_ptr->next_out)
  2851. tipc_printf(buf, "%u..",
  2852. msg_seqno(buf_msg(l_ptr->next_out)));
  2853. tipc_printf(buf, "%u]", msg_seqno(buf_msg(l_ptr->last_out)));
  2854. if ((mod(msg_seqno(buf_msg(l_ptr->last_out)) -
  2855. msg_seqno(buf_msg(l_ptr->first_out)))
  2856. != (l_ptr->out_queue_size - 1)) ||
  2857. (l_ptr->last_out->next != NULL)) {
  2858. tipc_printf(buf, "\nSend queue inconsistency\n");
  2859. tipc_printf(buf, "first_out= %x ", l_ptr->first_out);
  2860. tipc_printf(buf, "next_out= %x ", l_ptr->next_out);
  2861. tipc_printf(buf, "last_out= %x ", l_ptr->last_out);
  2862. link_dump_send_queue(l_ptr);
  2863. }
  2864. } else
  2865. tipc_printf(buf, "[]");
  2866. tipc_printf(buf, "SQSIZ(%u)", l_ptr->out_queue_size);
  2867. if (l_ptr->oldest_deferred_in) {
  2868. u32 o = msg_seqno(buf_msg(l_ptr->oldest_deferred_in));
  2869. u32 n = msg_seqno(buf_msg(l_ptr->newest_deferred_in));
  2870. tipc_printf(buf, ":RQUE[%u..%u]", o, n);
  2871. if (l_ptr->deferred_inqueue_sz != mod((n + 1) - o)) {
  2872. tipc_printf(buf, ":RQSIZ(%u)",
  2873. l_ptr->deferred_inqueue_sz);
  2874. }
  2875. }
  2876. if (link_working_unknown(l_ptr))
  2877. tipc_printf(buf, ":WU");
  2878. if (link_reset_reset(l_ptr))
  2879. tipc_printf(buf, ":RR");
  2880. if (link_reset_unknown(l_ptr))
  2881. tipc_printf(buf, ":RU");
  2882. if (link_working_working(l_ptr))
  2883. tipc_printf(buf, ":WW");
  2884. tipc_printf(buf, "\n");
  2885. }