link.c 70 KB

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