link.c 80 KB

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