link.c 78 KB

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