link.c 81 KB

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