link.c 85 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283
  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. if (unlikely(msg_non_seq(msg))) {
  1564. link_recv_non_seq(buf);
  1565. continue;
  1566. }
  1567. if (unlikely(!msg_short(msg) &&
  1568. (msg_destnode(msg) != tipc_own_addr)))
  1569. goto cont;
  1570. n_ptr = tipc_node_find(msg_prevnode(msg));
  1571. if (unlikely(!n_ptr))
  1572. goto cont;
  1573. tipc_node_lock(n_ptr);
  1574. l_ptr = n_ptr->links[b_ptr->identity];
  1575. if (unlikely(!l_ptr)) {
  1576. tipc_node_unlock(n_ptr);
  1577. goto cont;
  1578. }
  1579. /*
  1580. * Release acked messages
  1581. */
  1582. if (less(n_ptr->bclink.acked, msg_bcast_ack(msg))) {
  1583. if (tipc_node_is_up(n_ptr) && n_ptr->bclink.supported)
  1584. tipc_bclink_acknowledge(n_ptr, msg_bcast_ack(msg));
  1585. }
  1586. crs = l_ptr->first_out;
  1587. while ((crs != l_ptr->next_out) &&
  1588. less_eq(msg_seqno(buf_msg(crs)), ackd)) {
  1589. struct sk_buff *next = crs->next;
  1590. buf_discard(crs);
  1591. crs = next;
  1592. released++;
  1593. }
  1594. if (released) {
  1595. l_ptr->first_out = crs;
  1596. l_ptr->out_queue_size -= released;
  1597. }
  1598. if (unlikely(l_ptr->next_out))
  1599. tipc_link_push_queue(l_ptr);
  1600. if (unlikely(!list_empty(&l_ptr->waiting_ports)))
  1601. tipc_link_wakeup_ports(l_ptr, 0);
  1602. if (unlikely(++l_ptr->unacked_window >= TIPC_MIN_LINK_WIN)) {
  1603. l_ptr->stats.sent_acks++;
  1604. tipc_link_send_proto_msg(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
  1605. }
  1606. protocol_check:
  1607. if (likely(link_working_working(l_ptr))) {
  1608. if (likely(seq_no == mod(l_ptr->next_in_no))) {
  1609. l_ptr->next_in_no++;
  1610. if (unlikely(l_ptr->oldest_deferred_in))
  1611. head = link_insert_deferred_queue(l_ptr,
  1612. head);
  1613. if (likely(msg_is_dest(msg, tipc_own_addr))) {
  1614. deliver:
  1615. if (likely(msg_isdata(msg))) {
  1616. tipc_node_unlock(n_ptr);
  1617. tipc_port_recv_msg(buf);
  1618. continue;
  1619. }
  1620. switch (msg_user(msg)) {
  1621. case MSG_BUNDLER:
  1622. l_ptr->stats.recv_bundles++;
  1623. l_ptr->stats.recv_bundled +=
  1624. msg_msgcnt(msg);
  1625. tipc_node_unlock(n_ptr);
  1626. tipc_link_recv_bundle(buf);
  1627. continue;
  1628. case ROUTE_DISTRIBUTOR:
  1629. tipc_node_unlock(n_ptr);
  1630. tipc_cltr_recv_routing_table(buf);
  1631. continue;
  1632. case NAME_DISTRIBUTOR:
  1633. tipc_node_unlock(n_ptr);
  1634. tipc_named_recv(buf);
  1635. continue;
  1636. case CONN_MANAGER:
  1637. tipc_node_unlock(n_ptr);
  1638. tipc_port_recv_proto_msg(buf);
  1639. continue;
  1640. case MSG_FRAGMENTER:
  1641. l_ptr->stats.recv_fragments++;
  1642. if (tipc_link_recv_fragment(&l_ptr->defragm_buf,
  1643. &buf, &msg)) {
  1644. l_ptr->stats.recv_fragmented++;
  1645. goto deliver;
  1646. }
  1647. break;
  1648. case CHANGEOVER_PROTOCOL:
  1649. type = msg_type(msg);
  1650. if (link_recv_changeover_msg(&l_ptr, &buf)) {
  1651. msg = buf_msg(buf);
  1652. seq_no = msg_seqno(msg);
  1653. TIPC_SKB_CB(buf)->handle
  1654. = b_ptr;
  1655. if (type == ORIGINAL_MSG)
  1656. goto deliver;
  1657. goto protocol_check;
  1658. }
  1659. break;
  1660. }
  1661. }
  1662. tipc_node_unlock(n_ptr);
  1663. tipc_net_route_msg(buf);
  1664. continue;
  1665. }
  1666. link_handle_out_of_seq_msg(l_ptr, buf);
  1667. head = link_insert_deferred_queue(l_ptr, head);
  1668. tipc_node_unlock(n_ptr);
  1669. continue;
  1670. }
  1671. if (msg_user(msg) == LINK_PROTOCOL) {
  1672. link_recv_proto_msg(l_ptr, buf);
  1673. head = link_insert_deferred_queue(l_ptr, head);
  1674. tipc_node_unlock(n_ptr);
  1675. continue;
  1676. }
  1677. msg_dbg(msg,"NSEQ<REC<");
  1678. link_state_event(l_ptr, TRAFFIC_MSG_EVT);
  1679. if (link_working_working(l_ptr)) {
  1680. /* Re-insert in front of queue */
  1681. msg_dbg(msg,"RECV-REINS:");
  1682. buf->next = head;
  1683. head = buf;
  1684. tipc_node_unlock(n_ptr);
  1685. continue;
  1686. }
  1687. tipc_node_unlock(n_ptr);
  1688. cont:
  1689. buf_discard(buf);
  1690. }
  1691. read_unlock_bh(&tipc_net_lock);
  1692. }
  1693. /*
  1694. * link_defer_buf(): Sort a received out-of-sequence packet
  1695. * into the deferred reception queue.
  1696. * Returns the increase of the queue length,i.e. 0 or 1
  1697. */
  1698. u32 tipc_link_defer_pkt(struct sk_buff **head,
  1699. struct sk_buff **tail,
  1700. struct sk_buff *buf)
  1701. {
  1702. struct sk_buff *prev = NULL;
  1703. struct sk_buff *crs = *head;
  1704. u32 seq_no = msg_seqno(buf_msg(buf));
  1705. buf->next = NULL;
  1706. /* Empty queue ? */
  1707. if (*head == NULL) {
  1708. *head = *tail = buf;
  1709. return 1;
  1710. }
  1711. /* Last ? */
  1712. if (less(msg_seqno(buf_msg(*tail)), seq_no)) {
  1713. (*tail)->next = buf;
  1714. *tail = buf;
  1715. return 1;
  1716. }
  1717. /* Scan through queue and sort it in */
  1718. do {
  1719. struct tipc_msg *msg = buf_msg(crs);
  1720. if (less(seq_no, msg_seqno(msg))) {
  1721. buf->next = crs;
  1722. if (prev)
  1723. prev->next = buf;
  1724. else
  1725. *head = buf;
  1726. return 1;
  1727. }
  1728. if (seq_no == msg_seqno(msg)) {
  1729. break;
  1730. }
  1731. prev = crs;
  1732. crs = crs->next;
  1733. }
  1734. while (crs);
  1735. /* Message is a duplicate of an existing message */
  1736. buf_discard(buf);
  1737. return 0;
  1738. }
  1739. /**
  1740. * link_handle_out_of_seq_msg - handle arrival of out-of-sequence packet
  1741. */
  1742. static void link_handle_out_of_seq_msg(struct link *l_ptr,
  1743. struct sk_buff *buf)
  1744. {
  1745. u32 seq_no = msg_seqno(buf_msg(buf));
  1746. if (likely(msg_user(buf_msg(buf)) == LINK_PROTOCOL)) {
  1747. link_recv_proto_msg(l_ptr, buf);
  1748. return;
  1749. }
  1750. dbg("rx OOS msg: seq_no %u, expecting %u (%u)\n",
  1751. seq_no, mod(l_ptr->next_in_no), l_ptr->next_in_no);
  1752. /* Record OOS packet arrival (force mismatch on next timeout) */
  1753. l_ptr->checkpoint--;
  1754. /*
  1755. * Discard packet if a duplicate; otherwise add it to deferred queue
  1756. * and notify peer of gap as per protocol specification
  1757. */
  1758. if (less(seq_no, mod(l_ptr->next_in_no))) {
  1759. l_ptr->stats.duplicates++;
  1760. buf_discard(buf);
  1761. return;
  1762. }
  1763. if (tipc_link_defer_pkt(&l_ptr->oldest_deferred_in,
  1764. &l_ptr->newest_deferred_in, buf)) {
  1765. l_ptr->deferred_inqueue_sz++;
  1766. l_ptr->stats.deferred_recv++;
  1767. if ((l_ptr->deferred_inqueue_sz % 16) == 1)
  1768. tipc_link_send_proto_msg(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
  1769. } else
  1770. l_ptr->stats.duplicates++;
  1771. }
  1772. /*
  1773. * Send protocol message to the other endpoint.
  1774. */
  1775. void tipc_link_send_proto_msg(struct link *l_ptr, u32 msg_typ, int probe_msg,
  1776. u32 gap, u32 tolerance, u32 priority, u32 ack_mtu)
  1777. {
  1778. struct sk_buff *buf = NULL;
  1779. struct tipc_msg *msg = l_ptr->pmsg;
  1780. u32 msg_size = sizeof(l_ptr->proto_msg);
  1781. if (link_blocked(l_ptr))
  1782. return;
  1783. msg_set_type(msg, msg_typ);
  1784. msg_set_net_plane(msg, l_ptr->b_ptr->net_plane);
  1785. msg_set_bcast_ack(msg, mod(l_ptr->owner->bclink.last_in));
  1786. msg_set_last_bcast(msg, tipc_bclink_get_last_sent());
  1787. if (msg_typ == STATE_MSG) {
  1788. u32 next_sent = mod(l_ptr->next_out_no);
  1789. if (!tipc_link_is_up(l_ptr))
  1790. return;
  1791. if (l_ptr->next_out)
  1792. next_sent = msg_seqno(buf_msg(l_ptr->next_out));
  1793. msg_set_next_sent(msg, next_sent);
  1794. if (l_ptr->oldest_deferred_in) {
  1795. u32 rec = msg_seqno(buf_msg(l_ptr->oldest_deferred_in));
  1796. gap = mod(rec - mod(l_ptr->next_in_no));
  1797. }
  1798. msg_set_seq_gap(msg, gap);
  1799. if (gap)
  1800. l_ptr->stats.sent_nacks++;
  1801. msg_set_link_tolerance(msg, tolerance);
  1802. msg_set_linkprio(msg, priority);
  1803. msg_set_max_pkt(msg, ack_mtu);
  1804. msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
  1805. msg_set_probe(msg, probe_msg != 0);
  1806. if (probe_msg) {
  1807. u32 mtu = l_ptr->max_pkt;
  1808. if ((mtu < l_ptr->max_pkt_target) &&
  1809. link_working_working(l_ptr) &&
  1810. l_ptr->fsm_msg_cnt) {
  1811. msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
  1812. if (l_ptr->max_pkt_probes == 10) {
  1813. l_ptr->max_pkt_target = (msg_size - 4);
  1814. l_ptr->max_pkt_probes = 0;
  1815. msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
  1816. }
  1817. l_ptr->max_pkt_probes++;
  1818. }
  1819. l_ptr->stats.sent_probes++;
  1820. }
  1821. l_ptr->stats.sent_states++;
  1822. } else { /* RESET_MSG or ACTIVATE_MSG */
  1823. msg_set_ack(msg, mod(l_ptr->reset_checkpoint - 1));
  1824. msg_set_seq_gap(msg, 0);
  1825. msg_set_next_sent(msg, 1);
  1826. msg_set_link_tolerance(msg, l_ptr->tolerance);
  1827. msg_set_linkprio(msg, l_ptr->priority);
  1828. msg_set_max_pkt(msg, l_ptr->max_pkt_target);
  1829. }
  1830. if (tipc_node_has_redundant_links(l_ptr->owner)) {
  1831. msg_set_redundant_link(msg);
  1832. } else {
  1833. msg_clear_redundant_link(msg);
  1834. }
  1835. msg_set_linkprio(msg, l_ptr->priority);
  1836. /* Ensure sequence number will not fit : */
  1837. msg_set_seqno(msg, mod(l_ptr->next_out_no + (0xffff/2)));
  1838. /* Congestion? */
  1839. if (tipc_bearer_congested(l_ptr->b_ptr, l_ptr)) {
  1840. if (!l_ptr->proto_msg_queue) {
  1841. l_ptr->proto_msg_queue =
  1842. buf_acquire(sizeof(l_ptr->proto_msg));
  1843. }
  1844. buf = l_ptr->proto_msg_queue;
  1845. if (!buf)
  1846. return;
  1847. skb_copy_to_linear_data(buf, msg, sizeof(l_ptr->proto_msg));
  1848. return;
  1849. }
  1850. msg_set_timestamp(msg, jiffies_to_msecs(jiffies));
  1851. /* Message can be sent */
  1852. msg_dbg(msg, ">>");
  1853. buf = buf_acquire(msg_size);
  1854. if (!buf)
  1855. return;
  1856. skb_copy_to_linear_data(buf, msg, sizeof(l_ptr->proto_msg));
  1857. msg_set_size(buf_msg(buf), msg_size);
  1858. if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
  1859. l_ptr->unacked_window = 0;
  1860. buf_discard(buf);
  1861. return;
  1862. }
  1863. /* New congestion */
  1864. tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
  1865. l_ptr->proto_msg_queue = buf;
  1866. l_ptr->stats.bearer_congs++;
  1867. }
  1868. /*
  1869. * Receive protocol message :
  1870. * Note that network plane id propagates through the network, and may
  1871. * change at any time. The node with lowest address rules
  1872. */
  1873. static void link_recv_proto_msg(struct link *l_ptr, struct sk_buff *buf)
  1874. {
  1875. u32 rec_gap = 0;
  1876. u32 max_pkt_info;
  1877. u32 max_pkt_ack;
  1878. u32 msg_tol;
  1879. struct tipc_msg *msg = buf_msg(buf);
  1880. dbg("AT(%u):", jiffies_to_msecs(jiffies));
  1881. msg_dbg(msg, "<<");
  1882. if (link_blocked(l_ptr))
  1883. goto exit;
  1884. /* record unnumbered packet arrival (force mismatch on next timeout) */
  1885. l_ptr->checkpoint--;
  1886. if (l_ptr->b_ptr->net_plane != msg_net_plane(msg))
  1887. if (tipc_own_addr > msg_prevnode(msg))
  1888. l_ptr->b_ptr->net_plane = msg_net_plane(msg);
  1889. l_ptr->owner->permit_changeover = msg_redundant_link(msg);
  1890. switch (msg_type(msg)) {
  1891. case RESET_MSG:
  1892. if (!link_working_unknown(l_ptr) && l_ptr->peer_session) {
  1893. if (msg_session(msg) == l_ptr->peer_session) {
  1894. dbg("Duplicate RESET: %u<->%u\n",
  1895. msg_session(msg), l_ptr->peer_session);
  1896. break; /* duplicate: ignore */
  1897. }
  1898. }
  1899. /* fall thru' */
  1900. case ACTIVATE_MSG:
  1901. /* Update link settings according other endpoint's values */
  1902. strcpy((strrchr(l_ptr->name, ':') + 1), (char *)msg_data(msg));
  1903. if ((msg_tol = msg_link_tolerance(msg)) &&
  1904. (msg_tol > l_ptr->tolerance))
  1905. link_set_supervision_props(l_ptr, msg_tol);
  1906. if (msg_linkprio(msg) > l_ptr->priority)
  1907. l_ptr->priority = msg_linkprio(msg);
  1908. max_pkt_info = msg_max_pkt(msg);
  1909. if (max_pkt_info) {
  1910. if (max_pkt_info < l_ptr->max_pkt_target)
  1911. l_ptr->max_pkt_target = max_pkt_info;
  1912. if (l_ptr->max_pkt > l_ptr->max_pkt_target)
  1913. l_ptr->max_pkt = l_ptr->max_pkt_target;
  1914. } else {
  1915. l_ptr->max_pkt = l_ptr->max_pkt_target;
  1916. }
  1917. l_ptr->owner->bclink.supported = (max_pkt_info != 0);
  1918. link_state_event(l_ptr, msg_type(msg));
  1919. l_ptr->peer_session = msg_session(msg);
  1920. l_ptr->peer_bearer_id = msg_bearer_id(msg);
  1921. /* Synchronize broadcast sequence numbers */
  1922. if (!tipc_node_has_redundant_links(l_ptr->owner)) {
  1923. l_ptr->owner->bclink.last_in = mod(msg_last_bcast(msg));
  1924. }
  1925. break;
  1926. case STATE_MSG:
  1927. if ((msg_tol = msg_link_tolerance(msg)))
  1928. link_set_supervision_props(l_ptr, msg_tol);
  1929. if (msg_linkprio(msg) &&
  1930. (msg_linkprio(msg) != l_ptr->priority)) {
  1931. warn("Resetting link <%s>, priority change %u->%u\n",
  1932. l_ptr->name, l_ptr->priority, msg_linkprio(msg));
  1933. l_ptr->priority = msg_linkprio(msg);
  1934. tipc_link_reset(l_ptr); /* Enforce change to take effect */
  1935. break;
  1936. }
  1937. link_state_event(l_ptr, TRAFFIC_MSG_EVT);
  1938. l_ptr->stats.recv_states++;
  1939. if (link_reset_unknown(l_ptr))
  1940. break;
  1941. if (less_eq(mod(l_ptr->next_in_no), msg_next_sent(msg))) {
  1942. rec_gap = mod(msg_next_sent(msg) -
  1943. mod(l_ptr->next_in_no));
  1944. }
  1945. max_pkt_ack = msg_max_pkt(msg);
  1946. if (max_pkt_ack > l_ptr->max_pkt) {
  1947. dbg("Link <%s> updated MTU %u -> %u\n",
  1948. l_ptr->name, l_ptr->max_pkt, max_pkt_ack);
  1949. l_ptr->max_pkt = max_pkt_ack;
  1950. l_ptr->max_pkt_probes = 0;
  1951. }
  1952. max_pkt_ack = 0;
  1953. if (msg_probe(msg)) {
  1954. l_ptr->stats.recv_probes++;
  1955. if (msg_size(msg) > sizeof(l_ptr->proto_msg)) {
  1956. max_pkt_ack = msg_size(msg);
  1957. }
  1958. }
  1959. /* Protocol message before retransmits, reduce loss risk */
  1960. tipc_bclink_check_gap(l_ptr->owner, msg_last_bcast(msg));
  1961. if (rec_gap || (msg_probe(msg))) {
  1962. tipc_link_send_proto_msg(l_ptr, STATE_MSG,
  1963. 0, rec_gap, 0, 0, max_pkt_ack);
  1964. }
  1965. if (msg_seq_gap(msg)) {
  1966. msg_dbg(msg, "With Gap:");
  1967. l_ptr->stats.recv_nacks++;
  1968. tipc_link_retransmit(l_ptr, l_ptr->first_out,
  1969. msg_seq_gap(msg));
  1970. }
  1971. break;
  1972. default:
  1973. msg_dbg(buf_msg(buf), "<DISCARDING UNKNOWN<");
  1974. }
  1975. exit:
  1976. buf_discard(buf);
  1977. }
  1978. /*
  1979. * tipc_link_tunnel(): Send one message via a link belonging to
  1980. * another bearer. Owner node is locked.
  1981. */
  1982. void tipc_link_tunnel(struct link *l_ptr,
  1983. struct tipc_msg *tunnel_hdr,
  1984. struct tipc_msg *msg,
  1985. u32 selector)
  1986. {
  1987. struct link *tunnel;
  1988. struct sk_buff *buf;
  1989. u32 length = msg_size(msg);
  1990. tunnel = l_ptr->owner->active_links[selector & 1];
  1991. if (!tipc_link_is_up(tunnel)) {
  1992. warn("Link changeover error, "
  1993. "tunnel link no longer available\n");
  1994. return;
  1995. }
  1996. msg_set_size(tunnel_hdr, length + INT_H_SIZE);
  1997. buf = buf_acquire(length + INT_H_SIZE);
  1998. if (!buf) {
  1999. warn("Link changeover error, "
  2000. "unable to send tunnel msg\n");
  2001. return;
  2002. }
  2003. skb_copy_to_linear_data(buf, tunnel_hdr, INT_H_SIZE);
  2004. skb_copy_to_linear_data_offset(buf, INT_H_SIZE, msg, length);
  2005. dbg("%c->%c:", l_ptr->b_ptr->net_plane, tunnel->b_ptr->net_plane);
  2006. msg_dbg(buf_msg(buf), ">SEND>");
  2007. tipc_link_send_buf(tunnel, buf);
  2008. }
  2009. /*
  2010. * changeover(): Send whole message queue via the remaining link
  2011. * Owner node is locked.
  2012. */
  2013. void tipc_link_changeover(struct link *l_ptr)
  2014. {
  2015. u32 msgcount = l_ptr->out_queue_size;
  2016. struct sk_buff *crs = l_ptr->first_out;
  2017. struct link *tunnel = l_ptr->owner->active_links[0];
  2018. struct tipc_msg tunnel_hdr;
  2019. int split_bundles;
  2020. if (!tunnel)
  2021. return;
  2022. if (!l_ptr->owner->permit_changeover) {
  2023. warn("Link changeover error, "
  2024. "peer did not permit changeover\n");
  2025. return;
  2026. }
  2027. msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
  2028. ORIGINAL_MSG, TIPC_OK, INT_H_SIZE, l_ptr->addr);
  2029. msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
  2030. msg_set_msgcnt(&tunnel_hdr, msgcount);
  2031. dbg("Link changeover requires %u tunnel messages\n", msgcount);
  2032. if (!l_ptr->first_out) {
  2033. struct sk_buff *buf;
  2034. buf = buf_acquire(INT_H_SIZE);
  2035. if (buf) {
  2036. skb_copy_to_linear_data(buf, &tunnel_hdr, INT_H_SIZE);
  2037. msg_set_size(&tunnel_hdr, INT_H_SIZE);
  2038. dbg("%c->%c:", l_ptr->b_ptr->net_plane,
  2039. tunnel->b_ptr->net_plane);
  2040. msg_dbg(&tunnel_hdr, "EMPTY>SEND>");
  2041. tipc_link_send_buf(tunnel, buf);
  2042. } else {
  2043. warn("Link changeover error, "
  2044. "unable to send changeover msg\n");
  2045. }
  2046. return;
  2047. }
  2048. split_bundles = (l_ptr->owner->active_links[0] !=
  2049. l_ptr->owner->active_links[1]);
  2050. while (crs) {
  2051. struct tipc_msg *msg = buf_msg(crs);
  2052. if ((msg_user(msg) == MSG_BUNDLER) && split_bundles) {
  2053. struct tipc_msg *m = msg_get_wrapped(msg);
  2054. unchar* pos = (unchar*)m;
  2055. msgcount = msg_msgcnt(msg);
  2056. while (msgcount--) {
  2057. msg_set_seqno(m,msg_seqno(msg));
  2058. tipc_link_tunnel(l_ptr, &tunnel_hdr, m,
  2059. msg_link_selector(m));
  2060. pos += align(msg_size(m));
  2061. m = (struct tipc_msg *)pos;
  2062. }
  2063. } else {
  2064. tipc_link_tunnel(l_ptr, &tunnel_hdr, msg,
  2065. msg_link_selector(msg));
  2066. }
  2067. crs = crs->next;
  2068. }
  2069. }
  2070. void tipc_link_send_duplicate(struct link *l_ptr, struct link *tunnel)
  2071. {
  2072. struct sk_buff *iter;
  2073. struct tipc_msg tunnel_hdr;
  2074. msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
  2075. DUPLICATE_MSG, TIPC_OK, INT_H_SIZE, l_ptr->addr);
  2076. msg_set_msgcnt(&tunnel_hdr, l_ptr->out_queue_size);
  2077. msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
  2078. iter = l_ptr->first_out;
  2079. while (iter) {
  2080. struct sk_buff *outbuf;
  2081. struct tipc_msg *msg = buf_msg(iter);
  2082. u32 length = msg_size(msg);
  2083. if (msg_user(msg) == MSG_BUNDLER)
  2084. msg_set_type(msg, CLOSED_MSG);
  2085. msg_set_ack(msg, mod(l_ptr->next_in_no - 1)); /* Update */
  2086. msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
  2087. msg_set_size(&tunnel_hdr, length + INT_H_SIZE);
  2088. outbuf = buf_acquire(length + INT_H_SIZE);
  2089. if (outbuf == NULL) {
  2090. warn("Link changeover error, "
  2091. "unable to send duplicate msg\n");
  2092. return;
  2093. }
  2094. skb_copy_to_linear_data(outbuf, &tunnel_hdr, INT_H_SIZE);
  2095. skb_copy_to_linear_data_offset(outbuf, INT_H_SIZE, iter->data,
  2096. length);
  2097. dbg("%c->%c:", l_ptr->b_ptr->net_plane,
  2098. tunnel->b_ptr->net_plane);
  2099. msg_dbg(buf_msg(outbuf), ">SEND>");
  2100. tipc_link_send_buf(tunnel, outbuf);
  2101. if (!tipc_link_is_up(l_ptr))
  2102. return;
  2103. iter = iter->next;
  2104. }
  2105. }
  2106. /**
  2107. * buf_extract - extracts embedded TIPC message from another message
  2108. * @skb: encapsulating message buffer
  2109. * @from_pos: offset to extract from
  2110. *
  2111. * Returns a new message buffer containing an embedded message. The
  2112. * encapsulating message itself is left unchanged.
  2113. */
  2114. static struct sk_buff *buf_extract(struct sk_buff *skb, u32 from_pos)
  2115. {
  2116. struct tipc_msg *msg = (struct tipc_msg *)(skb->data + from_pos);
  2117. u32 size = msg_size(msg);
  2118. struct sk_buff *eb;
  2119. eb = buf_acquire(size);
  2120. if (eb)
  2121. skb_copy_to_linear_data(eb, msg, size);
  2122. return eb;
  2123. }
  2124. /*
  2125. * link_recv_changeover_msg(): Receive tunneled packet sent
  2126. * via other link. Node is locked. Return extracted buffer.
  2127. */
  2128. static int link_recv_changeover_msg(struct link **l_ptr,
  2129. struct sk_buff **buf)
  2130. {
  2131. struct sk_buff *tunnel_buf = *buf;
  2132. struct link *dest_link;
  2133. struct tipc_msg *msg;
  2134. struct tipc_msg *tunnel_msg = buf_msg(tunnel_buf);
  2135. u32 msg_typ = msg_type(tunnel_msg);
  2136. u32 msg_count = msg_msgcnt(tunnel_msg);
  2137. dest_link = (*l_ptr)->owner->links[msg_bearer_id(tunnel_msg)];
  2138. if (!dest_link) {
  2139. msg_dbg(tunnel_msg, "NOLINK/<REC<");
  2140. goto exit;
  2141. }
  2142. if (dest_link == *l_ptr) {
  2143. err("Unexpected changeover message on link <%s>\n",
  2144. (*l_ptr)->name);
  2145. goto exit;
  2146. }
  2147. dbg("%c<-%c:", dest_link->b_ptr->net_plane,
  2148. (*l_ptr)->b_ptr->net_plane);
  2149. *l_ptr = dest_link;
  2150. msg = msg_get_wrapped(tunnel_msg);
  2151. if (msg_typ == DUPLICATE_MSG) {
  2152. if (less(msg_seqno(msg), mod(dest_link->next_in_no))) {
  2153. msg_dbg(tunnel_msg, "DROP/<REC<");
  2154. goto exit;
  2155. }
  2156. *buf = buf_extract(tunnel_buf,INT_H_SIZE);
  2157. if (*buf == NULL) {
  2158. warn("Link changeover error, duplicate msg dropped\n");
  2159. goto exit;
  2160. }
  2161. msg_dbg(tunnel_msg, "TNL<REC<");
  2162. buf_discard(tunnel_buf);
  2163. return 1;
  2164. }
  2165. /* First original message ?: */
  2166. if (tipc_link_is_up(dest_link)) {
  2167. msg_dbg(tunnel_msg, "UP/FIRST/<REC<");
  2168. info("Resetting link <%s>, changeover initiated by peer\n",
  2169. dest_link->name);
  2170. tipc_link_reset(dest_link);
  2171. dest_link->exp_msg_count = msg_count;
  2172. dbg("Expecting %u tunnelled messages\n", msg_count);
  2173. if (!msg_count)
  2174. goto exit;
  2175. } else if (dest_link->exp_msg_count == START_CHANGEOVER) {
  2176. msg_dbg(tunnel_msg, "BLK/FIRST/<REC<");
  2177. dest_link->exp_msg_count = msg_count;
  2178. dbg("Expecting %u tunnelled messages\n", msg_count);
  2179. if (!msg_count)
  2180. goto exit;
  2181. }
  2182. /* Receive original message */
  2183. if (dest_link->exp_msg_count == 0) {
  2184. warn("Link switchover error, "
  2185. "got too many tunnelled messages\n");
  2186. msg_dbg(tunnel_msg, "OVERDUE/DROP/<REC<");
  2187. dbg_print_link(dest_link, "LINK:");
  2188. goto exit;
  2189. }
  2190. dest_link->exp_msg_count--;
  2191. if (less(msg_seqno(msg), dest_link->reset_checkpoint)) {
  2192. msg_dbg(tunnel_msg, "DROP/DUPL/<REC<");
  2193. goto exit;
  2194. } else {
  2195. *buf = buf_extract(tunnel_buf, INT_H_SIZE);
  2196. if (*buf != NULL) {
  2197. msg_dbg(tunnel_msg, "TNL<REC<");
  2198. buf_discard(tunnel_buf);
  2199. return 1;
  2200. } else {
  2201. warn("Link changeover error, original msg dropped\n");
  2202. }
  2203. }
  2204. exit:
  2205. *buf = NULL;
  2206. buf_discard(tunnel_buf);
  2207. return 0;
  2208. }
  2209. /*
  2210. * Bundler functionality:
  2211. */
  2212. void tipc_link_recv_bundle(struct sk_buff *buf)
  2213. {
  2214. u32 msgcount = msg_msgcnt(buf_msg(buf));
  2215. u32 pos = INT_H_SIZE;
  2216. struct sk_buff *obuf;
  2217. msg_dbg(buf_msg(buf), "<BNDL<: ");
  2218. while (msgcount--) {
  2219. obuf = buf_extract(buf, pos);
  2220. if (obuf == NULL) {
  2221. warn("Link unable to unbundle message(s)\n");
  2222. break;
  2223. }
  2224. pos += align(msg_size(buf_msg(obuf)));
  2225. msg_dbg(buf_msg(obuf), " /");
  2226. tipc_net_route_msg(obuf);
  2227. }
  2228. buf_discard(buf);
  2229. }
  2230. /*
  2231. * Fragmentation/defragmentation:
  2232. */
  2233. /*
  2234. * tipc_link_send_long_buf: Entry for buffers needing fragmentation.
  2235. * The buffer is complete, inclusive total message length.
  2236. * Returns user data length.
  2237. */
  2238. int tipc_link_send_long_buf(struct link *l_ptr, struct sk_buff *buf)
  2239. {
  2240. struct tipc_msg *inmsg = buf_msg(buf);
  2241. struct tipc_msg fragm_hdr;
  2242. u32 insize = msg_size(inmsg);
  2243. u32 dsz = msg_data_sz(inmsg);
  2244. unchar *crs = buf->data;
  2245. u32 rest = insize;
  2246. u32 pack_sz = link_max_pkt(l_ptr);
  2247. u32 fragm_sz = pack_sz - INT_H_SIZE;
  2248. u32 fragm_no = 1;
  2249. u32 destaddr = msg_destnode(inmsg);
  2250. if (msg_short(inmsg))
  2251. destaddr = l_ptr->addr;
  2252. if (msg_routed(inmsg))
  2253. msg_set_prevnode(inmsg, tipc_own_addr);
  2254. /* Prepare reusable fragment header: */
  2255. msg_init(&fragm_hdr, MSG_FRAGMENTER, FIRST_FRAGMENT,
  2256. TIPC_OK, INT_H_SIZE, destaddr);
  2257. msg_set_link_selector(&fragm_hdr, msg_link_selector(inmsg));
  2258. msg_set_long_msgno(&fragm_hdr, mod(l_ptr->long_msg_seq_no++));
  2259. msg_set_fragm_no(&fragm_hdr, fragm_no);
  2260. l_ptr->stats.sent_fragmented++;
  2261. /* Chop up message: */
  2262. while (rest > 0) {
  2263. struct sk_buff *fragm;
  2264. if (rest <= fragm_sz) {
  2265. fragm_sz = rest;
  2266. msg_set_type(&fragm_hdr, LAST_FRAGMENT);
  2267. }
  2268. fragm = buf_acquire(fragm_sz + INT_H_SIZE);
  2269. if (fragm == NULL) {
  2270. warn("Link unable to fragment message\n");
  2271. dsz = -ENOMEM;
  2272. goto exit;
  2273. }
  2274. msg_set_size(&fragm_hdr, fragm_sz + INT_H_SIZE);
  2275. skb_copy_to_linear_data(fragm, &fragm_hdr, INT_H_SIZE);
  2276. skb_copy_to_linear_data_offset(fragm, INT_H_SIZE, crs,
  2277. fragm_sz);
  2278. /* Send queued messages first, if any: */
  2279. l_ptr->stats.sent_fragments++;
  2280. tipc_link_send_buf(l_ptr, fragm);
  2281. if (!tipc_link_is_up(l_ptr))
  2282. return dsz;
  2283. msg_set_fragm_no(&fragm_hdr, ++fragm_no);
  2284. rest -= fragm_sz;
  2285. crs += fragm_sz;
  2286. msg_set_type(&fragm_hdr, FRAGMENT);
  2287. }
  2288. exit:
  2289. buf_discard(buf);
  2290. return dsz;
  2291. }
  2292. /*
  2293. * A pending message being re-assembled must store certain values
  2294. * to handle subsequent fragments correctly. The following functions
  2295. * help storing these values in unused, available fields in the
  2296. * pending message. This makes dynamic memory allocation unecessary.
  2297. */
  2298. static void set_long_msg_seqno(struct sk_buff *buf, u32 seqno)
  2299. {
  2300. msg_set_seqno(buf_msg(buf), seqno);
  2301. }
  2302. static u32 get_fragm_size(struct sk_buff *buf)
  2303. {
  2304. return msg_ack(buf_msg(buf));
  2305. }
  2306. static void set_fragm_size(struct sk_buff *buf, u32 sz)
  2307. {
  2308. msg_set_ack(buf_msg(buf), sz);
  2309. }
  2310. static u32 get_expected_frags(struct sk_buff *buf)
  2311. {
  2312. return msg_bcast_ack(buf_msg(buf));
  2313. }
  2314. static void set_expected_frags(struct sk_buff *buf, u32 exp)
  2315. {
  2316. msg_set_bcast_ack(buf_msg(buf), exp);
  2317. }
  2318. static u32 get_timer_cnt(struct sk_buff *buf)
  2319. {
  2320. return msg_reroute_cnt(buf_msg(buf));
  2321. }
  2322. static void incr_timer_cnt(struct sk_buff *buf)
  2323. {
  2324. msg_incr_reroute_cnt(buf_msg(buf));
  2325. }
  2326. /*
  2327. * tipc_link_recv_fragment(): Called with node lock on. Returns
  2328. * the reassembled buffer if message is complete.
  2329. */
  2330. int tipc_link_recv_fragment(struct sk_buff **pending, struct sk_buff **fb,
  2331. struct tipc_msg **m)
  2332. {
  2333. struct sk_buff *prev = NULL;
  2334. struct sk_buff *fbuf = *fb;
  2335. struct tipc_msg *fragm = buf_msg(fbuf);
  2336. struct sk_buff *pbuf = *pending;
  2337. u32 long_msg_seq_no = msg_long_msgno(fragm);
  2338. *fb = NULL;
  2339. msg_dbg(fragm,"FRG<REC<");
  2340. /* Is there an incomplete message waiting for this fragment? */
  2341. while (pbuf && ((msg_seqno(buf_msg(pbuf)) != long_msg_seq_no)
  2342. || (msg_orignode(fragm) != msg_orignode(buf_msg(pbuf))))) {
  2343. prev = pbuf;
  2344. pbuf = pbuf->next;
  2345. }
  2346. if (!pbuf && (msg_type(fragm) == FIRST_FRAGMENT)) {
  2347. struct tipc_msg *imsg = (struct tipc_msg *)msg_data(fragm);
  2348. u32 msg_sz = msg_size(imsg);
  2349. u32 fragm_sz = msg_data_sz(fragm);
  2350. u32 exp_fragm_cnt = msg_sz/fragm_sz + !!(msg_sz % fragm_sz);
  2351. u32 max = TIPC_MAX_USER_MSG_SIZE + LONG_H_SIZE;
  2352. if (msg_type(imsg) == TIPC_MCAST_MSG)
  2353. max = TIPC_MAX_USER_MSG_SIZE + MCAST_H_SIZE;
  2354. if (msg_size(imsg) > max) {
  2355. msg_dbg(fragm,"<REC<Oversized: ");
  2356. buf_discard(fbuf);
  2357. return 0;
  2358. }
  2359. pbuf = buf_acquire(msg_size(imsg));
  2360. if (pbuf != NULL) {
  2361. pbuf->next = *pending;
  2362. *pending = pbuf;
  2363. skb_copy_to_linear_data(pbuf, imsg,
  2364. msg_data_sz(fragm));
  2365. /* Prepare buffer for subsequent fragments. */
  2366. set_long_msg_seqno(pbuf, long_msg_seq_no);
  2367. set_fragm_size(pbuf,fragm_sz);
  2368. set_expected_frags(pbuf,exp_fragm_cnt - 1);
  2369. } else {
  2370. warn("Link unable to reassemble fragmented message\n");
  2371. }
  2372. buf_discard(fbuf);
  2373. return 0;
  2374. } else if (pbuf && (msg_type(fragm) != FIRST_FRAGMENT)) {
  2375. u32 dsz = msg_data_sz(fragm);
  2376. u32 fsz = get_fragm_size(pbuf);
  2377. u32 crs = ((msg_fragm_no(fragm) - 1) * fsz);
  2378. u32 exp_frags = get_expected_frags(pbuf) - 1;
  2379. skb_copy_to_linear_data_offset(pbuf, crs,
  2380. msg_data(fragm), dsz);
  2381. buf_discard(fbuf);
  2382. /* Is message complete? */
  2383. if (exp_frags == 0) {
  2384. if (prev)
  2385. prev->next = pbuf->next;
  2386. else
  2387. *pending = pbuf->next;
  2388. msg_reset_reroute_cnt(buf_msg(pbuf));
  2389. *fb = pbuf;
  2390. *m = buf_msg(pbuf);
  2391. return 1;
  2392. }
  2393. set_expected_frags(pbuf,exp_frags);
  2394. return 0;
  2395. }
  2396. dbg(" Discarding orphan fragment %x\n",fbuf);
  2397. msg_dbg(fragm,"ORPHAN:");
  2398. dbg("Pending long buffers:\n");
  2399. dbg_print_buf_chain(*pending);
  2400. buf_discard(fbuf);
  2401. return 0;
  2402. }
  2403. /**
  2404. * link_check_defragm_bufs - flush stale incoming message fragments
  2405. * @l_ptr: pointer to link
  2406. */
  2407. static void link_check_defragm_bufs(struct link *l_ptr)
  2408. {
  2409. struct sk_buff *prev = NULL;
  2410. struct sk_buff *next = NULL;
  2411. struct sk_buff *buf = l_ptr->defragm_buf;
  2412. if (!buf)
  2413. return;
  2414. if (!link_working_working(l_ptr))
  2415. return;
  2416. while (buf) {
  2417. u32 cnt = get_timer_cnt(buf);
  2418. next = buf->next;
  2419. if (cnt < 4) {
  2420. incr_timer_cnt(buf);
  2421. prev = buf;
  2422. } else {
  2423. dbg(" Discarding incomplete long buffer\n");
  2424. msg_dbg(buf_msg(buf), "LONG:");
  2425. dbg_print_link(l_ptr, "curr:");
  2426. dbg("Pending long buffers:\n");
  2427. dbg_print_buf_chain(l_ptr->defragm_buf);
  2428. if (prev)
  2429. prev->next = buf->next;
  2430. else
  2431. l_ptr->defragm_buf = buf->next;
  2432. buf_discard(buf);
  2433. }
  2434. buf = next;
  2435. }
  2436. }
  2437. static void link_set_supervision_props(struct link *l_ptr, u32 tolerance)
  2438. {
  2439. l_ptr->tolerance = tolerance;
  2440. l_ptr->continuity_interval =
  2441. ((tolerance / 4) > 500) ? 500 : tolerance / 4;
  2442. l_ptr->abort_limit = tolerance / (l_ptr->continuity_interval / 4);
  2443. }
  2444. void tipc_link_set_queue_limits(struct link *l_ptr, u32 window)
  2445. {
  2446. /* Data messages from this node, inclusive FIRST_FRAGM */
  2447. l_ptr->queue_limit[DATA_LOW] = window;
  2448. l_ptr->queue_limit[DATA_MEDIUM] = (window / 3) * 4;
  2449. l_ptr->queue_limit[DATA_HIGH] = (window / 3) * 5;
  2450. l_ptr->queue_limit[DATA_CRITICAL] = (window / 3) * 6;
  2451. /* Transiting data messages,inclusive FIRST_FRAGM */
  2452. l_ptr->queue_limit[DATA_LOW + 4] = 300;
  2453. l_ptr->queue_limit[DATA_MEDIUM + 4] = 600;
  2454. l_ptr->queue_limit[DATA_HIGH + 4] = 900;
  2455. l_ptr->queue_limit[DATA_CRITICAL + 4] = 1200;
  2456. l_ptr->queue_limit[CONN_MANAGER] = 1200;
  2457. l_ptr->queue_limit[ROUTE_DISTRIBUTOR] = 1200;
  2458. l_ptr->queue_limit[CHANGEOVER_PROTOCOL] = 2500;
  2459. l_ptr->queue_limit[NAME_DISTRIBUTOR] = 3000;
  2460. /* FRAGMENT and LAST_FRAGMENT packets */
  2461. l_ptr->queue_limit[MSG_FRAGMENTER] = 4000;
  2462. }
  2463. /**
  2464. * link_find_link - locate link by name
  2465. * @name - ptr to link name string
  2466. * @node - ptr to area to be filled with ptr to associated node
  2467. *
  2468. * Caller must hold 'tipc_net_lock' to ensure node and bearer are not deleted;
  2469. * this also prevents link deletion.
  2470. *
  2471. * Returns pointer to link (or 0 if invalid link name).
  2472. */
  2473. static struct link *link_find_link(const char *name, struct node **node)
  2474. {
  2475. struct link_name link_name_parts;
  2476. struct bearer *b_ptr;
  2477. struct link *l_ptr;
  2478. if (!link_name_validate(name, &link_name_parts))
  2479. return NULL;
  2480. b_ptr = tipc_bearer_find_interface(link_name_parts.if_local);
  2481. if (!b_ptr)
  2482. return NULL;
  2483. *node = tipc_node_find(link_name_parts.addr_peer);
  2484. if (!*node)
  2485. return NULL;
  2486. l_ptr = (*node)->links[b_ptr->identity];
  2487. if (!l_ptr || strcmp(l_ptr->name, name))
  2488. return NULL;
  2489. return l_ptr;
  2490. }
  2491. struct sk_buff *tipc_link_cmd_config(const void *req_tlv_area, int req_tlv_space,
  2492. u16 cmd)
  2493. {
  2494. struct tipc_link_config *args;
  2495. u32 new_value;
  2496. struct link *l_ptr;
  2497. struct node *node;
  2498. int res;
  2499. if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_CONFIG))
  2500. return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
  2501. args = (struct tipc_link_config *)TLV_DATA(req_tlv_area);
  2502. new_value = ntohl(args->value);
  2503. if (!strcmp(args->name, tipc_bclink_name)) {
  2504. if ((cmd == TIPC_CMD_SET_LINK_WINDOW) &&
  2505. (tipc_bclink_set_queue_limits(new_value) == 0))
  2506. return tipc_cfg_reply_none();
  2507. return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
  2508. " (cannot change setting on broadcast link)");
  2509. }
  2510. read_lock_bh(&tipc_net_lock);
  2511. l_ptr = link_find_link(args->name, &node);
  2512. if (!l_ptr) {
  2513. read_unlock_bh(&tipc_net_lock);
  2514. return tipc_cfg_reply_error_string("link not found");
  2515. }
  2516. tipc_node_lock(node);
  2517. res = -EINVAL;
  2518. switch (cmd) {
  2519. case TIPC_CMD_SET_LINK_TOL:
  2520. if ((new_value >= TIPC_MIN_LINK_TOL) &&
  2521. (new_value <= TIPC_MAX_LINK_TOL)) {
  2522. link_set_supervision_props(l_ptr, new_value);
  2523. tipc_link_send_proto_msg(l_ptr, STATE_MSG,
  2524. 0, 0, new_value, 0, 0);
  2525. res = TIPC_OK;
  2526. }
  2527. break;
  2528. case TIPC_CMD_SET_LINK_PRI:
  2529. if ((new_value >= TIPC_MIN_LINK_PRI) &&
  2530. (new_value <= TIPC_MAX_LINK_PRI)) {
  2531. l_ptr->priority = new_value;
  2532. tipc_link_send_proto_msg(l_ptr, STATE_MSG,
  2533. 0, 0, 0, new_value, 0);
  2534. res = TIPC_OK;
  2535. }
  2536. break;
  2537. case TIPC_CMD_SET_LINK_WINDOW:
  2538. if ((new_value >= TIPC_MIN_LINK_WIN) &&
  2539. (new_value <= TIPC_MAX_LINK_WIN)) {
  2540. tipc_link_set_queue_limits(l_ptr, new_value);
  2541. res = TIPC_OK;
  2542. }
  2543. break;
  2544. }
  2545. tipc_node_unlock(node);
  2546. read_unlock_bh(&tipc_net_lock);
  2547. if (res)
  2548. return tipc_cfg_reply_error_string("cannot change link setting");
  2549. return tipc_cfg_reply_none();
  2550. }
  2551. /**
  2552. * link_reset_statistics - reset link statistics
  2553. * @l_ptr: pointer to link
  2554. */
  2555. static void link_reset_statistics(struct link *l_ptr)
  2556. {
  2557. memset(&l_ptr->stats, 0, sizeof(l_ptr->stats));
  2558. l_ptr->stats.sent_info = l_ptr->next_out_no;
  2559. l_ptr->stats.recv_info = l_ptr->next_in_no;
  2560. }
  2561. struct sk_buff *tipc_link_cmd_reset_stats(const void *req_tlv_area, int req_tlv_space)
  2562. {
  2563. char *link_name;
  2564. struct link *l_ptr;
  2565. struct node *node;
  2566. if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
  2567. return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
  2568. link_name = (char *)TLV_DATA(req_tlv_area);
  2569. if (!strcmp(link_name, tipc_bclink_name)) {
  2570. if (tipc_bclink_reset_stats())
  2571. return tipc_cfg_reply_error_string("link not found");
  2572. return tipc_cfg_reply_none();
  2573. }
  2574. read_lock_bh(&tipc_net_lock);
  2575. l_ptr = link_find_link(link_name, &node);
  2576. if (!l_ptr) {
  2577. read_unlock_bh(&tipc_net_lock);
  2578. return tipc_cfg_reply_error_string("link not found");
  2579. }
  2580. tipc_node_lock(node);
  2581. link_reset_statistics(l_ptr);
  2582. tipc_node_unlock(node);
  2583. read_unlock_bh(&tipc_net_lock);
  2584. return tipc_cfg_reply_none();
  2585. }
  2586. /**
  2587. * percent - convert count to a percentage of total (rounding up or down)
  2588. */
  2589. static u32 percent(u32 count, u32 total)
  2590. {
  2591. return (count * 100 + (total / 2)) / total;
  2592. }
  2593. /**
  2594. * tipc_link_stats - print link statistics
  2595. * @name: link name
  2596. * @buf: print buffer area
  2597. * @buf_size: size of print buffer area
  2598. *
  2599. * Returns length of print buffer data string (or 0 if error)
  2600. */
  2601. static int tipc_link_stats(const char *name, char *buf, const u32 buf_size)
  2602. {
  2603. struct print_buf pb;
  2604. struct link *l_ptr;
  2605. struct node *node;
  2606. char *status;
  2607. u32 profile_total = 0;
  2608. if (!strcmp(name, tipc_bclink_name))
  2609. return tipc_bclink_stats(buf, buf_size);
  2610. tipc_printbuf_init(&pb, buf, buf_size);
  2611. read_lock_bh(&tipc_net_lock);
  2612. l_ptr = link_find_link(name, &node);
  2613. if (!l_ptr) {
  2614. read_unlock_bh(&tipc_net_lock);
  2615. return 0;
  2616. }
  2617. tipc_node_lock(node);
  2618. if (tipc_link_is_active(l_ptr))
  2619. status = "ACTIVE";
  2620. else if (tipc_link_is_up(l_ptr))
  2621. status = "STANDBY";
  2622. else
  2623. status = "DEFUNCT";
  2624. tipc_printf(&pb, "Link <%s>\n"
  2625. " %s MTU:%u Priority:%u Tolerance:%u ms"
  2626. " Window:%u packets\n",
  2627. l_ptr->name, status, link_max_pkt(l_ptr),
  2628. l_ptr->priority, l_ptr->tolerance, l_ptr->queue_limit[0]);
  2629. tipc_printf(&pb, " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
  2630. l_ptr->next_in_no - l_ptr->stats.recv_info,
  2631. l_ptr->stats.recv_fragments,
  2632. l_ptr->stats.recv_fragmented,
  2633. l_ptr->stats.recv_bundles,
  2634. l_ptr->stats.recv_bundled);
  2635. tipc_printf(&pb, " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
  2636. l_ptr->next_out_no - l_ptr->stats.sent_info,
  2637. l_ptr->stats.sent_fragments,
  2638. l_ptr->stats.sent_fragmented,
  2639. l_ptr->stats.sent_bundles,
  2640. l_ptr->stats.sent_bundled);
  2641. profile_total = l_ptr->stats.msg_length_counts;
  2642. if (!profile_total)
  2643. profile_total = 1;
  2644. tipc_printf(&pb, " TX profile sample:%u packets average:%u octets\n"
  2645. " 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% "
  2646. "-16354:%u%% -32768:%u%% -66000:%u%%\n",
  2647. l_ptr->stats.msg_length_counts,
  2648. l_ptr->stats.msg_lengths_total / profile_total,
  2649. percent(l_ptr->stats.msg_length_profile[0], profile_total),
  2650. percent(l_ptr->stats.msg_length_profile[1], profile_total),
  2651. percent(l_ptr->stats.msg_length_profile[2], profile_total),
  2652. percent(l_ptr->stats.msg_length_profile[3], profile_total),
  2653. percent(l_ptr->stats.msg_length_profile[4], profile_total),
  2654. percent(l_ptr->stats.msg_length_profile[5], profile_total),
  2655. percent(l_ptr->stats.msg_length_profile[6], profile_total));
  2656. tipc_printf(&pb, " RX states:%u probes:%u naks:%u defs:%u dups:%u\n",
  2657. l_ptr->stats.recv_states,
  2658. l_ptr->stats.recv_probes,
  2659. l_ptr->stats.recv_nacks,
  2660. l_ptr->stats.deferred_recv,
  2661. l_ptr->stats.duplicates);
  2662. tipc_printf(&pb, " TX states:%u probes:%u naks:%u acks:%u dups:%u\n",
  2663. l_ptr->stats.sent_states,
  2664. l_ptr->stats.sent_probes,
  2665. l_ptr->stats.sent_nacks,
  2666. l_ptr->stats.sent_acks,
  2667. l_ptr->stats.retransmitted);
  2668. tipc_printf(&pb, " Congestion bearer:%u link:%u Send queue max:%u avg:%u\n",
  2669. l_ptr->stats.bearer_congs,
  2670. l_ptr->stats.link_congs,
  2671. l_ptr->stats.max_queue_sz,
  2672. l_ptr->stats.queue_sz_counts
  2673. ? (l_ptr->stats.accu_queue_sz / l_ptr->stats.queue_sz_counts)
  2674. : 0);
  2675. tipc_node_unlock(node);
  2676. read_unlock_bh(&tipc_net_lock);
  2677. return tipc_printbuf_validate(&pb);
  2678. }
  2679. #define MAX_LINK_STATS_INFO 2000
  2680. struct sk_buff *tipc_link_cmd_show_stats(const void *req_tlv_area, int req_tlv_space)
  2681. {
  2682. struct sk_buff *buf;
  2683. struct tlv_desc *rep_tlv;
  2684. int str_len;
  2685. if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
  2686. return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
  2687. buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_LINK_STATS_INFO));
  2688. if (!buf)
  2689. return NULL;
  2690. rep_tlv = (struct tlv_desc *)buf->data;
  2691. str_len = tipc_link_stats((char *)TLV_DATA(req_tlv_area),
  2692. (char *)TLV_DATA(rep_tlv), MAX_LINK_STATS_INFO);
  2693. if (!str_len) {
  2694. buf_discard(buf);
  2695. return tipc_cfg_reply_error_string("link not found");
  2696. }
  2697. skb_put(buf, TLV_SPACE(str_len));
  2698. TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
  2699. return buf;
  2700. }
  2701. #if 0
  2702. int link_control(const char *name, u32 op, u32 val)
  2703. {
  2704. int res = -EINVAL;
  2705. struct link *l_ptr;
  2706. u32 bearer_id;
  2707. struct node * node;
  2708. u32 a;
  2709. a = link_name2addr(name, &bearer_id);
  2710. read_lock_bh(&tipc_net_lock);
  2711. node = tipc_node_find(a);
  2712. if (node) {
  2713. tipc_node_lock(node);
  2714. l_ptr = node->links[bearer_id];
  2715. if (l_ptr) {
  2716. if (op == TIPC_REMOVE_LINK) {
  2717. struct bearer *b_ptr = l_ptr->b_ptr;
  2718. spin_lock_bh(&b_ptr->publ.lock);
  2719. tipc_link_delete(l_ptr);
  2720. spin_unlock_bh(&b_ptr->publ.lock);
  2721. }
  2722. if (op == TIPC_CMD_BLOCK_LINK) {
  2723. tipc_link_reset(l_ptr);
  2724. l_ptr->blocked = 1;
  2725. }
  2726. if (op == TIPC_CMD_UNBLOCK_LINK) {
  2727. l_ptr->blocked = 0;
  2728. }
  2729. res = TIPC_OK;
  2730. }
  2731. tipc_node_unlock(node);
  2732. }
  2733. read_unlock_bh(&tipc_net_lock);
  2734. return res;
  2735. }
  2736. #endif
  2737. /**
  2738. * tipc_link_get_max_pkt - get maximum packet size to use when sending to destination
  2739. * @dest: network address of destination node
  2740. * @selector: used to select from set of active links
  2741. *
  2742. * If no active link can be found, uses default maximum packet size.
  2743. */
  2744. u32 tipc_link_get_max_pkt(u32 dest, u32 selector)
  2745. {
  2746. struct node *n_ptr;
  2747. struct link *l_ptr;
  2748. u32 res = MAX_PKT_DEFAULT;
  2749. if (dest == tipc_own_addr)
  2750. return MAX_MSG_SIZE;
  2751. read_lock_bh(&tipc_net_lock);
  2752. n_ptr = tipc_node_select(dest, selector);
  2753. if (n_ptr) {
  2754. tipc_node_lock(n_ptr);
  2755. l_ptr = n_ptr->active_links[selector & 1];
  2756. if (l_ptr)
  2757. res = link_max_pkt(l_ptr);
  2758. tipc_node_unlock(n_ptr);
  2759. }
  2760. read_unlock_bh(&tipc_net_lock);
  2761. return res;
  2762. }
  2763. #if 0
  2764. static void link_dump_rec_queue(struct link *l_ptr)
  2765. {
  2766. struct sk_buff *crs;
  2767. if (!l_ptr->oldest_deferred_in) {
  2768. info("Reception queue empty\n");
  2769. return;
  2770. }
  2771. info("Contents of Reception queue:\n");
  2772. crs = l_ptr->oldest_deferred_in;
  2773. while (crs) {
  2774. if (crs->data == (void *)0x0000a3a3) {
  2775. info("buffer %x invalid\n", crs);
  2776. return;
  2777. }
  2778. msg_dbg(buf_msg(crs), "In rec queue: \n");
  2779. crs = crs->next;
  2780. }
  2781. }
  2782. #endif
  2783. static void link_dump_send_queue(struct link *l_ptr)
  2784. {
  2785. if (l_ptr->next_out) {
  2786. info("\nContents of unsent queue:\n");
  2787. dbg_print_buf_chain(l_ptr->next_out);
  2788. }
  2789. info("\nContents of send queue:\n");
  2790. if (l_ptr->first_out) {
  2791. dbg_print_buf_chain(l_ptr->first_out);
  2792. }
  2793. info("Empty send queue\n");
  2794. }
  2795. static void link_print(struct link *l_ptr, struct print_buf *buf,
  2796. const char *str)
  2797. {
  2798. tipc_printf(buf, str);
  2799. if (link_reset_reset(l_ptr) || link_reset_unknown(l_ptr))
  2800. return;
  2801. tipc_printf(buf, "Link %x<%s>:",
  2802. l_ptr->addr, l_ptr->b_ptr->publ.name);
  2803. tipc_printf(buf, ": NXO(%u):", mod(l_ptr->next_out_no));
  2804. tipc_printf(buf, "NXI(%u):", mod(l_ptr->next_in_no));
  2805. tipc_printf(buf, "SQUE");
  2806. if (l_ptr->first_out) {
  2807. tipc_printf(buf, "[%u..", msg_seqno(buf_msg(l_ptr->first_out)));
  2808. if (l_ptr->next_out)
  2809. tipc_printf(buf, "%u..",
  2810. msg_seqno(buf_msg(l_ptr->next_out)));
  2811. tipc_printf(buf, "%u]",
  2812. msg_seqno(buf_msg
  2813. (l_ptr->last_out)), l_ptr->out_queue_size);
  2814. if ((mod(msg_seqno(buf_msg(l_ptr->last_out)) -
  2815. msg_seqno(buf_msg(l_ptr->first_out)))
  2816. != (l_ptr->out_queue_size - 1))
  2817. || (l_ptr->last_out->next != 0)) {
  2818. tipc_printf(buf, "\nSend queue inconsistency\n");
  2819. tipc_printf(buf, "first_out= %x ", l_ptr->first_out);
  2820. tipc_printf(buf, "next_out= %x ", l_ptr->next_out);
  2821. tipc_printf(buf, "last_out= %x ", l_ptr->last_out);
  2822. link_dump_send_queue(l_ptr);
  2823. }
  2824. } else
  2825. tipc_printf(buf, "[]");
  2826. tipc_printf(buf, "SQSIZ(%u)", l_ptr->out_queue_size);
  2827. if (l_ptr->oldest_deferred_in) {
  2828. u32 o = msg_seqno(buf_msg(l_ptr->oldest_deferred_in));
  2829. u32 n = msg_seqno(buf_msg(l_ptr->newest_deferred_in));
  2830. tipc_printf(buf, ":RQUE[%u..%u]", o, n);
  2831. if (l_ptr->deferred_inqueue_sz != mod((n + 1) - o)) {
  2832. tipc_printf(buf, ":RQSIZ(%u)",
  2833. l_ptr->deferred_inqueue_sz);
  2834. }
  2835. }
  2836. if (link_working_unknown(l_ptr))
  2837. tipc_printf(buf, ":WU");
  2838. if (link_reset_reset(l_ptr))
  2839. tipc_printf(buf, ":RR");
  2840. if (link_reset_unknown(l_ptr))
  2841. tipc_printf(buf, ":RU");
  2842. if (link_working_working(l_ptr))
  2843. tipc_printf(buf, ":WW");
  2844. tipc_printf(buf, "\n");
  2845. }