link.c 65 KB

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