link.c 85 KB

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