link.c 85 KB

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