link.c 78 KB

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