link.c 79 KB

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