link.c 78 KB

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