link.c 73 KB

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