link.c 82 KB

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