link.c 84 KB

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