link.c 83 KB

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