link.c 74 KB

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