link.c 87 KB

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