link.c 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673
  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_names_xmit - send name table entries to new neighbor
  929. *
  930. * Send routine for bulk delivery of name table messages when contact
  931. * with a new neighbor occurs. No link congestion checking is performed
  932. * because name table messages *must* be delivered. The messages must be
  933. * small enough not to require fragmentation.
  934. * Called without any locks held.
  935. */
  936. void tipc_link_names_xmit(struct list_head *message_list, u32 dest)
  937. {
  938. struct tipc_node *n_ptr;
  939. struct tipc_link *l_ptr;
  940. struct sk_buff *buf;
  941. struct sk_buff *temp_buf;
  942. if (list_empty(message_list))
  943. return;
  944. n_ptr = tipc_node_find(dest);
  945. if (n_ptr) {
  946. tipc_node_lock(n_ptr);
  947. l_ptr = n_ptr->active_links[0];
  948. if (l_ptr) {
  949. /* convert circular list to linear list */
  950. ((struct sk_buff *)message_list->prev)->next = NULL;
  951. link_add_chain_to_outqueue(l_ptr,
  952. (struct sk_buff *)message_list->next, 0);
  953. tipc_link_push_queue(l_ptr);
  954. INIT_LIST_HEAD(message_list);
  955. }
  956. tipc_node_unlock(n_ptr);
  957. }
  958. /* discard the messages if they couldn't be sent */
  959. list_for_each_safe(buf, temp_buf, ((struct sk_buff *)message_list)) {
  960. list_del((struct list_head *)buf);
  961. kfree_skb(buf);
  962. }
  963. }
  964. /*
  965. * tipc_link_push_packet: Push one unsent packet to the media
  966. */
  967. static u32 tipc_link_push_packet(struct tipc_link *l_ptr)
  968. {
  969. struct sk_buff *buf = l_ptr->first_out;
  970. u32 r_q_size = l_ptr->retransm_queue_size;
  971. u32 r_q_head = l_ptr->retransm_queue_head;
  972. /* Step to position where retransmission failed, if any, */
  973. /* consider that buffers may have been released in meantime */
  974. if (r_q_size && buf) {
  975. u32 last = lesser(mod(r_q_head + r_q_size),
  976. link_last_sent(l_ptr));
  977. u32 first = buf_seqno(buf);
  978. while (buf && less(first, r_q_head)) {
  979. first = mod(first + 1);
  980. buf = buf->next;
  981. }
  982. l_ptr->retransm_queue_head = r_q_head = first;
  983. l_ptr->retransm_queue_size = r_q_size = mod(last - first);
  984. }
  985. /* Continue retransmission now, if there is anything: */
  986. if (r_q_size && buf) {
  987. msg_set_ack(buf_msg(buf), mod(l_ptr->next_in_no - 1));
  988. msg_set_bcast_ack(buf_msg(buf), l_ptr->owner->bclink.last_in);
  989. tipc_bearer_send(l_ptr->bearer_id, buf, &l_ptr->media_addr);
  990. l_ptr->retransm_queue_head = mod(++r_q_head);
  991. l_ptr->retransm_queue_size = --r_q_size;
  992. l_ptr->stats.retransmitted++;
  993. return 0;
  994. }
  995. /* Send deferred protocol message, if any: */
  996. buf = l_ptr->proto_msg_queue;
  997. if (buf) {
  998. msg_set_ack(buf_msg(buf), mod(l_ptr->next_in_no - 1));
  999. msg_set_bcast_ack(buf_msg(buf), l_ptr->owner->bclink.last_in);
  1000. tipc_bearer_send(l_ptr->bearer_id, buf, &l_ptr->media_addr);
  1001. l_ptr->unacked_window = 0;
  1002. kfree_skb(buf);
  1003. l_ptr->proto_msg_queue = NULL;
  1004. return 0;
  1005. }
  1006. /* Send one deferred data message, if send window not full: */
  1007. buf = l_ptr->next_out;
  1008. if (buf) {
  1009. struct tipc_msg *msg = buf_msg(buf);
  1010. u32 next = msg_seqno(msg);
  1011. u32 first = buf_seqno(l_ptr->first_out);
  1012. if (mod(next - first) < l_ptr->queue_limit[0]) {
  1013. msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
  1014. msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
  1015. tipc_bearer_send(l_ptr->bearer_id, buf,
  1016. &l_ptr->media_addr);
  1017. if (msg_user(msg) == MSG_BUNDLER)
  1018. msg_set_type(msg, BUNDLE_CLOSED);
  1019. l_ptr->next_out = buf->next;
  1020. return 0;
  1021. }
  1022. }
  1023. return 1;
  1024. }
  1025. /*
  1026. * push_queue(): push out the unsent messages of a link where
  1027. * congestion has abated. Node is locked
  1028. */
  1029. void tipc_link_push_queue(struct tipc_link *l_ptr)
  1030. {
  1031. u32 res;
  1032. do {
  1033. res = tipc_link_push_packet(l_ptr);
  1034. } while (!res);
  1035. }
  1036. void tipc_link_reset_all(struct tipc_node *node)
  1037. {
  1038. char addr_string[16];
  1039. u32 i;
  1040. tipc_node_lock(node);
  1041. pr_warn("Resetting all links to %s\n",
  1042. tipc_addr_string_fill(addr_string, node->addr));
  1043. for (i = 0; i < MAX_BEARERS; i++) {
  1044. if (node->links[i]) {
  1045. link_print(node->links[i], "Resetting link\n");
  1046. tipc_link_reset(node->links[i]);
  1047. }
  1048. }
  1049. tipc_node_unlock(node);
  1050. }
  1051. static void link_retransmit_failure(struct tipc_link *l_ptr,
  1052. struct sk_buff *buf)
  1053. {
  1054. struct tipc_msg *msg = buf_msg(buf);
  1055. pr_warn("Retransmission failure on link <%s>\n", l_ptr->name);
  1056. if (l_ptr->addr) {
  1057. /* Handle failure on standard link */
  1058. link_print(l_ptr, "Resetting link\n");
  1059. tipc_link_reset(l_ptr);
  1060. } else {
  1061. /* Handle failure on broadcast link */
  1062. struct tipc_node *n_ptr;
  1063. char addr_string[16];
  1064. pr_info("Msg seq number: %u, ", msg_seqno(msg));
  1065. pr_cont("Outstanding acks: %lu\n",
  1066. (unsigned long) TIPC_SKB_CB(buf)->handle);
  1067. n_ptr = tipc_bclink_retransmit_to();
  1068. tipc_node_lock(n_ptr);
  1069. tipc_addr_string_fill(addr_string, n_ptr->addr);
  1070. pr_info("Broadcast link info for %s\n", addr_string);
  1071. pr_info("Reception permitted: %d, Acked: %u\n",
  1072. n_ptr->bclink.recv_permitted,
  1073. n_ptr->bclink.acked);
  1074. pr_info("Last in: %u, Oos state: %u, Last sent: %u\n",
  1075. n_ptr->bclink.last_in,
  1076. n_ptr->bclink.oos_state,
  1077. n_ptr->bclink.last_sent);
  1078. tipc_node_unlock(n_ptr);
  1079. tipc_bclink_set_flags(TIPC_BCLINK_RESET);
  1080. l_ptr->stale_count = 0;
  1081. }
  1082. }
  1083. void tipc_link_retransmit(struct tipc_link *l_ptr, struct sk_buff *buf,
  1084. u32 retransmits)
  1085. {
  1086. struct tipc_msg *msg;
  1087. if (!buf)
  1088. return;
  1089. msg = buf_msg(buf);
  1090. /* Detect repeated retransmit failures */
  1091. if (l_ptr->last_retransmitted == msg_seqno(msg)) {
  1092. if (++l_ptr->stale_count > 100) {
  1093. link_retransmit_failure(l_ptr, buf);
  1094. return;
  1095. }
  1096. } else {
  1097. l_ptr->last_retransmitted = msg_seqno(msg);
  1098. l_ptr->stale_count = 1;
  1099. }
  1100. while (retransmits && (buf != l_ptr->next_out) && buf) {
  1101. msg = buf_msg(buf);
  1102. msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
  1103. msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
  1104. tipc_bearer_send(l_ptr->bearer_id, buf, &l_ptr->media_addr);
  1105. buf = buf->next;
  1106. retransmits--;
  1107. l_ptr->stats.retransmitted++;
  1108. }
  1109. l_ptr->retransm_queue_head = l_ptr->retransm_queue_size = 0;
  1110. }
  1111. /**
  1112. * link_insert_deferred_queue - insert deferred messages back into receive chain
  1113. */
  1114. static struct sk_buff *link_insert_deferred_queue(struct tipc_link *l_ptr,
  1115. struct sk_buff *buf)
  1116. {
  1117. u32 seq_no;
  1118. if (l_ptr->oldest_deferred_in == NULL)
  1119. return buf;
  1120. seq_no = buf_seqno(l_ptr->oldest_deferred_in);
  1121. if (seq_no == mod(l_ptr->next_in_no)) {
  1122. l_ptr->newest_deferred_in->next = buf;
  1123. buf = l_ptr->oldest_deferred_in;
  1124. l_ptr->oldest_deferred_in = NULL;
  1125. l_ptr->deferred_inqueue_sz = 0;
  1126. }
  1127. return buf;
  1128. }
  1129. /**
  1130. * link_recv_buf_validate - validate basic format of received message
  1131. *
  1132. * This routine ensures a TIPC message has an acceptable header, and at least
  1133. * as much data as the header indicates it should. The routine also ensures
  1134. * that the entire message header is stored in the main fragment of the message
  1135. * buffer, to simplify future access to message header fields.
  1136. *
  1137. * Note: Having extra info present in the message header or data areas is OK.
  1138. * TIPC will ignore the excess, under the assumption that it is optional info
  1139. * introduced by a later release of the protocol.
  1140. */
  1141. static int link_recv_buf_validate(struct sk_buff *buf)
  1142. {
  1143. static u32 min_data_hdr_size[8] = {
  1144. SHORT_H_SIZE, MCAST_H_SIZE, NAMED_H_SIZE, BASIC_H_SIZE,
  1145. MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE
  1146. };
  1147. struct tipc_msg *msg;
  1148. u32 tipc_hdr[2];
  1149. u32 size;
  1150. u32 hdr_size;
  1151. u32 min_hdr_size;
  1152. /* If this packet comes from the defer queue, the skb has already
  1153. * been validated
  1154. */
  1155. if (unlikely(TIPC_SKB_CB(buf)->deferred))
  1156. return 1;
  1157. if (unlikely(buf->len < MIN_H_SIZE))
  1158. return 0;
  1159. msg = skb_header_pointer(buf, 0, sizeof(tipc_hdr), tipc_hdr);
  1160. if (msg == NULL)
  1161. return 0;
  1162. if (unlikely(msg_version(msg) != TIPC_VERSION))
  1163. return 0;
  1164. size = msg_size(msg);
  1165. hdr_size = msg_hdr_sz(msg);
  1166. min_hdr_size = msg_isdata(msg) ?
  1167. min_data_hdr_size[msg_type(msg)] : INT_H_SIZE;
  1168. if (unlikely((hdr_size < min_hdr_size) ||
  1169. (size < hdr_size) ||
  1170. (buf->len < size) ||
  1171. (size - hdr_size > TIPC_MAX_USER_MSG_SIZE)))
  1172. return 0;
  1173. return pskb_may_pull(buf, hdr_size);
  1174. }
  1175. /**
  1176. * tipc_rcv - process TIPC packets/messages arriving from off-node
  1177. * @head: pointer to message buffer chain
  1178. * @b_ptr: pointer to bearer message arrived on
  1179. *
  1180. * Invoked with no locks held. Bearer pointer must point to a valid bearer
  1181. * structure (i.e. cannot be NULL), but bearer can be inactive.
  1182. */
  1183. void tipc_rcv(struct sk_buff *head, struct tipc_bearer *b_ptr)
  1184. {
  1185. while (head) {
  1186. struct tipc_node *n_ptr;
  1187. struct tipc_link *l_ptr;
  1188. struct sk_buff *crs;
  1189. struct sk_buff *buf = head;
  1190. struct tipc_msg *msg;
  1191. u32 seq_no;
  1192. u32 ackd;
  1193. u32 released = 0;
  1194. head = head->next;
  1195. buf->next = NULL;
  1196. /* Ensure message is well-formed */
  1197. if (unlikely(!link_recv_buf_validate(buf)))
  1198. goto discard;
  1199. /* Ensure message data is a single contiguous unit */
  1200. if (unlikely(skb_linearize(buf)))
  1201. goto discard;
  1202. /* Handle arrival of a non-unicast link message */
  1203. msg = buf_msg(buf);
  1204. if (unlikely(msg_non_seq(msg))) {
  1205. if (msg_user(msg) == LINK_CONFIG)
  1206. tipc_disc_rcv(buf, b_ptr);
  1207. else
  1208. tipc_bclink_rcv(buf);
  1209. continue;
  1210. }
  1211. /* Discard unicast link messages destined for another node */
  1212. if (unlikely(!msg_short(msg) &&
  1213. (msg_destnode(msg) != tipc_own_addr)))
  1214. goto discard;
  1215. /* Locate neighboring node that sent message */
  1216. n_ptr = tipc_node_find(msg_prevnode(msg));
  1217. if (unlikely(!n_ptr))
  1218. goto discard;
  1219. tipc_node_lock(n_ptr);
  1220. /* Locate unicast link endpoint that should handle message */
  1221. l_ptr = n_ptr->links[b_ptr->identity];
  1222. if (unlikely(!l_ptr))
  1223. goto unlock_discard;
  1224. /* Verify that communication with node is currently allowed */
  1225. if ((n_ptr->action_flags & TIPC_WAIT_PEER_LINKS_DOWN) &&
  1226. msg_user(msg) == LINK_PROTOCOL &&
  1227. (msg_type(msg) == RESET_MSG ||
  1228. msg_type(msg) == ACTIVATE_MSG) &&
  1229. !msg_redundant_link(msg))
  1230. n_ptr->action_flags &= ~TIPC_WAIT_PEER_LINKS_DOWN;
  1231. if (tipc_node_blocked(n_ptr))
  1232. goto unlock_discard;
  1233. /* Validate message sequence number info */
  1234. seq_no = msg_seqno(msg);
  1235. ackd = msg_ack(msg);
  1236. /* Release acked messages */
  1237. if (n_ptr->bclink.recv_permitted)
  1238. tipc_bclink_acknowledge(n_ptr, msg_bcast_ack(msg));
  1239. crs = l_ptr->first_out;
  1240. while ((crs != l_ptr->next_out) &&
  1241. less_eq(buf_seqno(crs), ackd)) {
  1242. struct sk_buff *next = crs->next;
  1243. kfree_skb(crs);
  1244. crs = next;
  1245. released++;
  1246. }
  1247. if (released) {
  1248. l_ptr->first_out = crs;
  1249. l_ptr->out_queue_size -= released;
  1250. }
  1251. /* Try sending any messages link endpoint has pending */
  1252. if (unlikely(l_ptr->next_out))
  1253. tipc_link_push_queue(l_ptr);
  1254. if (unlikely(!list_empty(&l_ptr->waiting_ports)))
  1255. tipc_link_wakeup_ports(l_ptr, 0);
  1256. /* Process the incoming packet */
  1257. if (unlikely(!link_working_working(l_ptr))) {
  1258. if (msg_user(msg) == LINK_PROTOCOL) {
  1259. tipc_link_proto_rcv(l_ptr, buf);
  1260. head = link_insert_deferred_queue(l_ptr, head);
  1261. tipc_node_unlock(n_ptr);
  1262. continue;
  1263. }
  1264. /* Traffic message. Conditionally activate link */
  1265. link_state_event(l_ptr, TRAFFIC_MSG_EVT);
  1266. if (link_working_working(l_ptr)) {
  1267. /* Re-insert buffer in front of queue */
  1268. buf->next = head;
  1269. head = buf;
  1270. tipc_node_unlock(n_ptr);
  1271. continue;
  1272. }
  1273. goto unlock_discard;
  1274. }
  1275. /* Link is now in state WORKING_WORKING */
  1276. if (unlikely(seq_no != mod(l_ptr->next_in_no))) {
  1277. link_handle_out_of_seq_msg(l_ptr, buf);
  1278. head = link_insert_deferred_queue(l_ptr, head);
  1279. tipc_node_unlock(n_ptr);
  1280. continue;
  1281. }
  1282. l_ptr->next_in_no++;
  1283. if (unlikely(l_ptr->oldest_deferred_in))
  1284. head = link_insert_deferred_queue(l_ptr, head);
  1285. if (unlikely(++l_ptr->unacked_window >= TIPC_MIN_LINK_WIN)) {
  1286. l_ptr->stats.sent_acks++;
  1287. tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
  1288. }
  1289. if (tipc_link_prepare_input(l_ptr, &buf)) {
  1290. tipc_node_unlock(n_ptr);
  1291. continue;
  1292. }
  1293. tipc_node_unlock(n_ptr);
  1294. msg = buf_msg(buf);
  1295. if (tipc_link_input(l_ptr, buf) != 0)
  1296. goto discard;
  1297. continue;
  1298. unlock_discard:
  1299. tipc_node_unlock(n_ptr);
  1300. discard:
  1301. kfree_skb(buf);
  1302. }
  1303. }
  1304. /**
  1305. * tipc_link_prepare_input - process TIPC link messages
  1306. *
  1307. * returns nonzero if the message was consumed
  1308. *
  1309. * Node lock must be held
  1310. */
  1311. static int tipc_link_prepare_input(struct tipc_link *l, struct sk_buff **buf)
  1312. {
  1313. struct tipc_node *n;
  1314. struct tipc_msg *msg;
  1315. int res = -EINVAL;
  1316. n = l->owner;
  1317. msg = buf_msg(*buf);
  1318. switch (msg_user(msg)) {
  1319. case CHANGEOVER_PROTOCOL:
  1320. if (tipc_link_tunnel_rcv(n, buf))
  1321. res = 0;
  1322. break;
  1323. case MSG_FRAGMENTER:
  1324. l->stats.recv_fragments++;
  1325. if (tipc_buf_append(&l->reasm_buf, buf)) {
  1326. l->stats.recv_fragmented++;
  1327. res = 0;
  1328. } else if (!l->reasm_buf) {
  1329. tipc_link_reset(l);
  1330. }
  1331. break;
  1332. case MSG_BUNDLER:
  1333. l->stats.recv_bundles++;
  1334. l->stats.recv_bundled += msg_msgcnt(msg);
  1335. res = 0;
  1336. break;
  1337. case NAME_DISTRIBUTOR:
  1338. n->bclink.recv_permitted = true;
  1339. res = 0;
  1340. break;
  1341. case BCAST_PROTOCOL:
  1342. tipc_link_sync_rcv(n, *buf);
  1343. break;
  1344. default:
  1345. res = 0;
  1346. }
  1347. return res;
  1348. }
  1349. /**
  1350. * tipc_link_input - Deliver message too higher layers
  1351. */
  1352. static int tipc_link_input(struct tipc_link *l, struct sk_buff *buf)
  1353. {
  1354. struct tipc_msg *msg = buf_msg(buf);
  1355. int res = 0;
  1356. switch (msg_user(msg)) {
  1357. case TIPC_LOW_IMPORTANCE:
  1358. case TIPC_MEDIUM_IMPORTANCE:
  1359. case TIPC_HIGH_IMPORTANCE:
  1360. case TIPC_CRITICAL_IMPORTANCE:
  1361. case CONN_MANAGER:
  1362. tipc_sk_rcv(buf);
  1363. break;
  1364. case NAME_DISTRIBUTOR:
  1365. tipc_named_rcv(buf);
  1366. break;
  1367. case MSG_BUNDLER:
  1368. tipc_link_bundle_rcv(buf);
  1369. break;
  1370. default:
  1371. res = -EINVAL;
  1372. }
  1373. return res;
  1374. }
  1375. /**
  1376. * tipc_link_defer_pkt - Add out-of-sequence message to deferred reception queue
  1377. *
  1378. * Returns increase in queue length (i.e. 0 or 1)
  1379. */
  1380. u32 tipc_link_defer_pkt(struct sk_buff **head, struct sk_buff **tail,
  1381. struct sk_buff *buf)
  1382. {
  1383. struct sk_buff *queue_buf;
  1384. struct sk_buff **prev;
  1385. u32 seq_no = buf_seqno(buf);
  1386. buf->next = NULL;
  1387. /* Empty queue ? */
  1388. if (*head == NULL) {
  1389. *head = *tail = buf;
  1390. return 1;
  1391. }
  1392. /* Last ? */
  1393. if (less(buf_seqno(*tail), seq_no)) {
  1394. (*tail)->next = buf;
  1395. *tail = buf;
  1396. return 1;
  1397. }
  1398. /* Locate insertion point in queue, then insert; discard if duplicate */
  1399. prev = head;
  1400. queue_buf = *head;
  1401. for (;;) {
  1402. u32 curr_seqno = buf_seqno(queue_buf);
  1403. if (seq_no == curr_seqno) {
  1404. kfree_skb(buf);
  1405. return 0;
  1406. }
  1407. if (less(seq_no, curr_seqno))
  1408. break;
  1409. prev = &queue_buf->next;
  1410. queue_buf = queue_buf->next;
  1411. }
  1412. buf->next = queue_buf;
  1413. *prev = buf;
  1414. return 1;
  1415. }
  1416. /*
  1417. * link_handle_out_of_seq_msg - handle arrival of out-of-sequence packet
  1418. */
  1419. static void link_handle_out_of_seq_msg(struct tipc_link *l_ptr,
  1420. struct sk_buff *buf)
  1421. {
  1422. u32 seq_no = buf_seqno(buf);
  1423. if (likely(msg_user(buf_msg(buf)) == LINK_PROTOCOL)) {
  1424. tipc_link_proto_rcv(l_ptr, buf);
  1425. return;
  1426. }
  1427. /* Record OOS packet arrival (force mismatch on next timeout) */
  1428. l_ptr->checkpoint--;
  1429. /*
  1430. * Discard packet if a duplicate; otherwise add it to deferred queue
  1431. * and notify peer of gap as per protocol specification
  1432. */
  1433. if (less(seq_no, mod(l_ptr->next_in_no))) {
  1434. l_ptr->stats.duplicates++;
  1435. kfree_skb(buf);
  1436. return;
  1437. }
  1438. if (tipc_link_defer_pkt(&l_ptr->oldest_deferred_in,
  1439. &l_ptr->newest_deferred_in, buf)) {
  1440. l_ptr->deferred_inqueue_sz++;
  1441. l_ptr->stats.deferred_recv++;
  1442. TIPC_SKB_CB(buf)->deferred = true;
  1443. if ((l_ptr->deferred_inqueue_sz % 16) == 1)
  1444. tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
  1445. } else
  1446. l_ptr->stats.duplicates++;
  1447. }
  1448. /*
  1449. * Send protocol message to the other endpoint.
  1450. */
  1451. void tipc_link_proto_xmit(struct tipc_link *l_ptr, u32 msg_typ, int probe_msg,
  1452. u32 gap, u32 tolerance, u32 priority, u32 ack_mtu)
  1453. {
  1454. struct sk_buff *buf = NULL;
  1455. struct tipc_msg *msg = l_ptr->pmsg;
  1456. u32 msg_size = sizeof(l_ptr->proto_msg);
  1457. int r_flag;
  1458. /* Discard any previous message that was deferred due to congestion */
  1459. if (l_ptr->proto_msg_queue) {
  1460. kfree_skb(l_ptr->proto_msg_queue);
  1461. l_ptr->proto_msg_queue = NULL;
  1462. }
  1463. /* Don't send protocol message during link changeover */
  1464. if (l_ptr->exp_msg_count)
  1465. return;
  1466. /* Abort non-RESET send if communication with node is prohibited */
  1467. if ((tipc_node_blocked(l_ptr->owner)) && (msg_typ != RESET_MSG))
  1468. return;
  1469. /* Create protocol message with "out-of-sequence" sequence number */
  1470. msg_set_type(msg, msg_typ);
  1471. msg_set_net_plane(msg, l_ptr->net_plane);
  1472. msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
  1473. msg_set_last_bcast(msg, tipc_bclink_get_last_sent());
  1474. if (msg_typ == STATE_MSG) {
  1475. u32 next_sent = mod(l_ptr->next_out_no);
  1476. if (!tipc_link_is_up(l_ptr))
  1477. return;
  1478. if (l_ptr->next_out)
  1479. next_sent = buf_seqno(l_ptr->next_out);
  1480. msg_set_next_sent(msg, next_sent);
  1481. if (l_ptr->oldest_deferred_in) {
  1482. u32 rec = buf_seqno(l_ptr->oldest_deferred_in);
  1483. gap = mod(rec - mod(l_ptr->next_in_no));
  1484. }
  1485. msg_set_seq_gap(msg, gap);
  1486. if (gap)
  1487. l_ptr->stats.sent_nacks++;
  1488. msg_set_link_tolerance(msg, tolerance);
  1489. msg_set_linkprio(msg, priority);
  1490. msg_set_max_pkt(msg, ack_mtu);
  1491. msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
  1492. msg_set_probe(msg, probe_msg != 0);
  1493. if (probe_msg) {
  1494. u32 mtu = l_ptr->max_pkt;
  1495. if ((mtu < l_ptr->max_pkt_target) &&
  1496. link_working_working(l_ptr) &&
  1497. l_ptr->fsm_msg_cnt) {
  1498. msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
  1499. if (l_ptr->max_pkt_probes == 10) {
  1500. l_ptr->max_pkt_target = (msg_size - 4);
  1501. l_ptr->max_pkt_probes = 0;
  1502. msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
  1503. }
  1504. l_ptr->max_pkt_probes++;
  1505. }
  1506. l_ptr->stats.sent_probes++;
  1507. }
  1508. l_ptr->stats.sent_states++;
  1509. } else { /* RESET_MSG or ACTIVATE_MSG */
  1510. msg_set_ack(msg, mod(l_ptr->reset_checkpoint - 1));
  1511. msg_set_seq_gap(msg, 0);
  1512. msg_set_next_sent(msg, 1);
  1513. msg_set_probe(msg, 0);
  1514. msg_set_link_tolerance(msg, l_ptr->tolerance);
  1515. msg_set_linkprio(msg, l_ptr->priority);
  1516. msg_set_max_pkt(msg, l_ptr->max_pkt_target);
  1517. }
  1518. r_flag = (l_ptr->owner->working_links > tipc_link_is_up(l_ptr));
  1519. msg_set_redundant_link(msg, r_flag);
  1520. msg_set_linkprio(msg, l_ptr->priority);
  1521. msg_set_size(msg, msg_size);
  1522. msg_set_seqno(msg, mod(l_ptr->next_out_no + (0xffff/2)));
  1523. buf = tipc_buf_acquire(msg_size);
  1524. if (!buf)
  1525. return;
  1526. skb_copy_to_linear_data(buf, msg, sizeof(l_ptr->proto_msg));
  1527. buf->priority = TC_PRIO_CONTROL;
  1528. tipc_bearer_send(l_ptr->bearer_id, buf, &l_ptr->media_addr);
  1529. l_ptr->unacked_window = 0;
  1530. kfree_skb(buf);
  1531. }
  1532. /*
  1533. * Receive protocol message :
  1534. * Note that network plane id propagates through the network, and may
  1535. * change at any time. The node with lowest address rules
  1536. */
  1537. static void tipc_link_proto_rcv(struct tipc_link *l_ptr, struct sk_buff *buf)
  1538. {
  1539. u32 rec_gap = 0;
  1540. u32 max_pkt_info;
  1541. u32 max_pkt_ack;
  1542. u32 msg_tol;
  1543. struct tipc_msg *msg = buf_msg(buf);
  1544. /* Discard protocol message during link changeover */
  1545. if (l_ptr->exp_msg_count)
  1546. goto exit;
  1547. if (l_ptr->net_plane != msg_net_plane(msg))
  1548. if (tipc_own_addr > msg_prevnode(msg))
  1549. l_ptr->net_plane = msg_net_plane(msg);
  1550. switch (msg_type(msg)) {
  1551. case RESET_MSG:
  1552. if (!link_working_unknown(l_ptr) &&
  1553. (l_ptr->peer_session != INVALID_SESSION)) {
  1554. if (less_eq(msg_session(msg), l_ptr->peer_session))
  1555. break; /* duplicate or old reset: ignore */
  1556. }
  1557. if (!msg_redundant_link(msg) && (link_working_working(l_ptr) ||
  1558. link_working_unknown(l_ptr))) {
  1559. /*
  1560. * peer has lost contact -- don't allow peer's links
  1561. * to reactivate before we recognize loss & clean up
  1562. */
  1563. l_ptr->owner->action_flags |= TIPC_WAIT_OWN_LINKS_DOWN;
  1564. }
  1565. link_state_event(l_ptr, RESET_MSG);
  1566. /* fall thru' */
  1567. case ACTIVATE_MSG:
  1568. /* Update link settings according other endpoint's values */
  1569. strcpy((strrchr(l_ptr->name, ':') + 1), (char *)msg_data(msg));
  1570. msg_tol = msg_link_tolerance(msg);
  1571. if (msg_tol > l_ptr->tolerance)
  1572. link_set_supervision_props(l_ptr, msg_tol);
  1573. if (msg_linkprio(msg) > l_ptr->priority)
  1574. l_ptr->priority = msg_linkprio(msg);
  1575. max_pkt_info = msg_max_pkt(msg);
  1576. if (max_pkt_info) {
  1577. if (max_pkt_info < l_ptr->max_pkt_target)
  1578. l_ptr->max_pkt_target = max_pkt_info;
  1579. if (l_ptr->max_pkt > l_ptr->max_pkt_target)
  1580. l_ptr->max_pkt = l_ptr->max_pkt_target;
  1581. } else {
  1582. l_ptr->max_pkt = l_ptr->max_pkt_target;
  1583. }
  1584. /* Synchronize broadcast link info, if not done previously */
  1585. if (!tipc_node_is_up(l_ptr->owner)) {
  1586. l_ptr->owner->bclink.last_sent =
  1587. l_ptr->owner->bclink.last_in =
  1588. msg_last_bcast(msg);
  1589. l_ptr->owner->bclink.oos_state = 0;
  1590. }
  1591. l_ptr->peer_session = msg_session(msg);
  1592. l_ptr->peer_bearer_id = msg_bearer_id(msg);
  1593. if (msg_type(msg) == ACTIVATE_MSG)
  1594. link_state_event(l_ptr, ACTIVATE_MSG);
  1595. break;
  1596. case STATE_MSG:
  1597. msg_tol = msg_link_tolerance(msg);
  1598. if (msg_tol)
  1599. link_set_supervision_props(l_ptr, msg_tol);
  1600. if (msg_linkprio(msg) &&
  1601. (msg_linkprio(msg) != l_ptr->priority)) {
  1602. pr_warn("%s<%s>, priority change %u->%u\n",
  1603. link_rst_msg, l_ptr->name, l_ptr->priority,
  1604. msg_linkprio(msg));
  1605. l_ptr->priority = msg_linkprio(msg);
  1606. tipc_link_reset(l_ptr); /* Enforce change to take effect */
  1607. break;
  1608. }
  1609. /* Record reception; force mismatch at next timeout: */
  1610. l_ptr->checkpoint--;
  1611. link_state_event(l_ptr, TRAFFIC_MSG_EVT);
  1612. l_ptr->stats.recv_states++;
  1613. if (link_reset_unknown(l_ptr))
  1614. break;
  1615. if (less_eq(mod(l_ptr->next_in_no), msg_next_sent(msg))) {
  1616. rec_gap = mod(msg_next_sent(msg) -
  1617. mod(l_ptr->next_in_no));
  1618. }
  1619. max_pkt_ack = msg_max_pkt(msg);
  1620. if (max_pkt_ack > l_ptr->max_pkt) {
  1621. l_ptr->max_pkt = max_pkt_ack;
  1622. l_ptr->max_pkt_probes = 0;
  1623. }
  1624. max_pkt_ack = 0;
  1625. if (msg_probe(msg)) {
  1626. l_ptr->stats.recv_probes++;
  1627. if (msg_size(msg) > sizeof(l_ptr->proto_msg))
  1628. max_pkt_ack = msg_size(msg);
  1629. }
  1630. /* Protocol message before retransmits, reduce loss risk */
  1631. if (l_ptr->owner->bclink.recv_permitted)
  1632. tipc_bclink_update_link_state(l_ptr->owner,
  1633. msg_last_bcast(msg));
  1634. if (rec_gap || (msg_probe(msg))) {
  1635. tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, rec_gap, 0,
  1636. 0, max_pkt_ack);
  1637. }
  1638. if (msg_seq_gap(msg)) {
  1639. l_ptr->stats.recv_nacks++;
  1640. tipc_link_retransmit(l_ptr, l_ptr->first_out,
  1641. msg_seq_gap(msg));
  1642. }
  1643. break;
  1644. }
  1645. exit:
  1646. kfree_skb(buf);
  1647. }
  1648. /* tipc_link_tunnel_xmit(): Tunnel one packet via a link belonging to
  1649. * a different bearer. Owner node is locked.
  1650. */
  1651. static void tipc_link_tunnel_xmit(struct tipc_link *l_ptr,
  1652. struct tipc_msg *tunnel_hdr,
  1653. struct tipc_msg *msg,
  1654. u32 selector)
  1655. {
  1656. struct tipc_link *tunnel;
  1657. struct sk_buff *buf;
  1658. u32 length = msg_size(msg);
  1659. tunnel = l_ptr->owner->active_links[selector & 1];
  1660. if (!tipc_link_is_up(tunnel)) {
  1661. pr_warn("%stunnel link no longer available\n", link_co_err);
  1662. return;
  1663. }
  1664. msg_set_size(tunnel_hdr, length + INT_H_SIZE);
  1665. buf = tipc_buf_acquire(length + INT_H_SIZE);
  1666. if (!buf) {
  1667. pr_warn("%sunable to send tunnel msg\n", link_co_err);
  1668. return;
  1669. }
  1670. skb_copy_to_linear_data(buf, tunnel_hdr, INT_H_SIZE);
  1671. skb_copy_to_linear_data_offset(buf, INT_H_SIZE, msg, length);
  1672. __tipc_link_xmit(tunnel, buf);
  1673. }
  1674. /* tipc_link_failover_send_queue(): A link has gone down, but a second
  1675. * link is still active. We can do failover. Tunnel the failing link's
  1676. * whole send queue via the remaining link. This way, we don't lose
  1677. * any packets, and sequence order is preserved for subsequent traffic
  1678. * sent over the remaining link. Owner node is locked.
  1679. */
  1680. void tipc_link_failover_send_queue(struct tipc_link *l_ptr)
  1681. {
  1682. u32 msgcount = l_ptr->out_queue_size;
  1683. struct sk_buff *crs = l_ptr->first_out;
  1684. struct tipc_link *tunnel = l_ptr->owner->active_links[0];
  1685. struct tipc_msg tunnel_hdr;
  1686. int split_bundles;
  1687. if (!tunnel)
  1688. return;
  1689. tipc_msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
  1690. ORIGINAL_MSG, INT_H_SIZE, l_ptr->addr);
  1691. msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
  1692. msg_set_msgcnt(&tunnel_hdr, msgcount);
  1693. if (!l_ptr->first_out) {
  1694. struct sk_buff *buf;
  1695. buf = tipc_buf_acquire(INT_H_SIZE);
  1696. if (buf) {
  1697. skb_copy_to_linear_data(buf, &tunnel_hdr, INT_H_SIZE);
  1698. msg_set_size(&tunnel_hdr, INT_H_SIZE);
  1699. __tipc_link_xmit(tunnel, buf);
  1700. } else {
  1701. pr_warn("%sunable to send changeover msg\n",
  1702. link_co_err);
  1703. }
  1704. return;
  1705. }
  1706. split_bundles = (l_ptr->owner->active_links[0] !=
  1707. l_ptr->owner->active_links[1]);
  1708. while (crs) {
  1709. struct tipc_msg *msg = buf_msg(crs);
  1710. if ((msg_user(msg) == MSG_BUNDLER) && split_bundles) {
  1711. struct tipc_msg *m = msg_get_wrapped(msg);
  1712. unchar *pos = (unchar *)m;
  1713. msgcount = msg_msgcnt(msg);
  1714. while (msgcount--) {
  1715. msg_set_seqno(m, msg_seqno(msg));
  1716. tipc_link_tunnel_xmit(l_ptr, &tunnel_hdr, m,
  1717. msg_link_selector(m));
  1718. pos += align(msg_size(m));
  1719. m = (struct tipc_msg *)pos;
  1720. }
  1721. } else {
  1722. tipc_link_tunnel_xmit(l_ptr, &tunnel_hdr, msg,
  1723. msg_link_selector(msg));
  1724. }
  1725. crs = crs->next;
  1726. }
  1727. }
  1728. /* tipc_link_dup_queue_xmit(): A second link has become active. Tunnel a
  1729. * duplicate of the first link's send queue via the new link. This way, we
  1730. * are guaranteed that currently queued packets from a socket are delivered
  1731. * before future traffic from the same socket, even if this is using the
  1732. * new link. The last arriving copy of each duplicate packet is dropped at
  1733. * the receiving end by the regular protocol check, so packet cardinality
  1734. * and sequence order is preserved per sender/receiver socket pair.
  1735. * Owner node is locked.
  1736. */
  1737. void tipc_link_dup_queue_xmit(struct tipc_link *l_ptr,
  1738. struct tipc_link *tunnel)
  1739. {
  1740. struct sk_buff *iter;
  1741. struct tipc_msg tunnel_hdr;
  1742. tipc_msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
  1743. DUPLICATE_MSG, INT_H_SIZE, l_ptr->addr);
  1744. msg_set_msgcnt(&tunnel_hdr, l_ptr->out_queue_size);
  1745. msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
  1746. iter = l_ptr->first_out;
  1747. while (iter) {
  1748. struct sk_buff *outbuf;
  1749. struct tipc_msg *msg = buf_msg(iter);
  1750. u32 length = msg_size(msg);
  1751. if (msg_user(msg) == MSG_BUNDLER)
  1752. msg_set_type(msg, CLOSED_MSG);
  1753. msg_set_ack(msg, mod(l_ptr->next_in_no - 1)); /* Update */
  1754. msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
  1755. msg_set_size(&tunnel_hdr, length + INT_H_SIZE);
  1756. outbuf = tipc_buf_acquire(length + INT_H_SIZE);
  1757. if (outbuf == NULL) {
  1758. pr_warn("%sunable to send duplicate msg\n",
  1759. link_co_err);
  1760. return;
  1761. }
  1762. skb_copy_to_linear_data(outbuf, &tunnel_hdr, INT_H_SIZE);
  1763. skb_copy_to_linear_data_offset(outbuf, INT_H_SIZE, iter->data,
  1764. length);
  1765. __tipc_link_xmit(tunnel, outbuf);
  1766. if (!tipc_link_is_up(l_ptr))
  1767. return;
  1768. iter = iter->next;
  1769. }
  1770. }
  1771. /**
  1772. * buf_extract - extracts embedded TIPC message from another message
  1773. * @skb: encapsulating message buffer
  1774. * @from_pos: offset to extract from
  1775. *
  1776. * Returns a new message buffer containing an embedded message. The
  1777. * encapsulating message itself is left unchanged.
  1778. */
  1779. static struct sk_buff *buf_extract(struct sk_buff *skb, u32 from_pos)
  1780. {
  1781. struct tipc_msg *msg = (struct tipc_msg *)(skb->data + from_pos);
  1782. u32 size = msg_size(msg);
  1783. struct sk_buff *eb;
  1784. eb = tipc_buf_acquire(size);
  1785. if (eb)
  1786. skb_copy_to_linear_data(eb, msg, size);
  1787. return eb;
  1788. }
  1789. /* tipc_link_dup_rcv(): Receive a tunnelled DUPLICATE_MSG packet.
  1790. * Owner node is locked.
  1791. */
  1792. static void tipc_link_dup_rcv(struct tipc_link *l_ptr,
  1793. struct sk_buff *t_buf)
  1794. {
  1795. struct sk_buff *buf;
  1796. if (!tipc_link_is_up(l_ptr))
  1797. return;
  1798. buf = buf_extract(t_buf, INT_H_SIZE);
  1799. if (buf == NULL) {
  1800. pr_warn("%sfailed to extract inner dup pkt\n", link_co_err);
  1801. return;
  1802. }
  1803. /* Add buffer to deferred queue, if applicable: */
  1804. link_handle_out_of_seq_msg(l_ptr, buf);
  1805. }
  1806. /* tipc_link_failover_rcv(): Receive a tunnelled ORIGINAL_MSG packet
  1807. * Owner node is locked.
  1808. */
  1809. static struct sk_buff *tipc_link_failover_rcv(struct tipc_link *l_ptr,
  1810. struct sk_buff *t_buf)
  1811. {
  1812. struct tipc_msg *t_msg = buf_msg(t_buf);
  1813. struct sk_buff *buf = NULL;
  1814. struct tipc_msg *msg;
  1815. if (tipc_link_is_up(l_ptr))
  1816. tipc_link_reset(l_ptr);
  1817. /* First failover packet? */
  1818. if (l_ptr->exp_msg_count == START_CHANGEOVER)
  1819. l_ptr->exp_msg_count = msg_msgcnt(t_msg);
  1820. /* Should there be an inner packet? */
  1821. if (l_ptr->exp_msg_count) {
  1822. l_ptr->exp_msg_count--;
  1823. buf = buf_extract(t_buf, INT_H_SIZE);
  1824. if (buf == NULL) {
  1825. pr_warn("%sno inner failover pkt\n", link_co_err);
  1826. goto exit;
  1827. }
  1828. msg = buf_msg(buf);
  1829. if (less(msg_seqno(msg), l_ptr->reset_checkpoint)) {
  1830. kfree_skb(buf);
  1831. buf = NULL;
  1832. goto exit;
  1833. }
  1834. if (msg_user(msg) == MSG_FRAGMENTER) {
  1835. l_ptr->stats.recv_fragments++;
  1836. tipc_buf_append(&l_ptr->reasm_buf, &buf);
  1837. }
  1838. }
  1839. exit:
  1840. if ((l_ptr->exp_msg_count == 0) && (l_ptr->flags & LINK_STOPPED)) {
  1841. tipc_node_detach_link(l_ptr->owner, l_ptr);
  1842. kfree(l_ptr);
  1843. }
  1844. return buf;
  1845. }
  1846. /* tipc_link_tunnel_rcv(): Receive a tunnelled packet, sent
  1847. * via other link as result of a failover (ORIGINAL_MSG) or
  1848. * a new active link (DUPLICATE_MSG). Failover packets are
  1849. * returned to the active link for delivery upwards.
  1850. * Owner node is locked.
  1851. */
  1852. static int tipc_link_tunnel_rcv(struct tipc_node *n_ptr,
  1853. struct sk_buff **buf)
  1854. {
  1855. struct sk_buff *t_buf = *buf;
  1856. struct tipc_link *l_ptr;
  1857. struct tipc_msg *t_msg = buf_msg(t_buf);
  1858. u32 bearer_id = msg_bearer_id(t_msg);
  1859. *buf = NULL;
  1860. if (bearer_id >= MAX_BEARERS)
  1861. goto exit;
  1862. l_ptr = n_ptr->links[bearer_id];
  1863. if (!l_ptr)
  1864. goto exit;
  1865. if (msg_type(t_msg) == DUPLICATE_MSG)
  1866. tipc_link_dup_rcv(l_ptr, t_buf);
  1867. else if (msg_type(t_msg) == ORIGINAL_MSG)
  1868. *buf = tipc_link_failover_rcv(l_ptr, t_buf);
  1869. else
  1870. pr_warn("%sunknown tunnel pkt received\n", link_co_err);
  1871. exit:
  1872. kfree_skb(t_buf);
  1873. return *buf != NULL;
  1874. }
  1875. /*
  1876. * Bundler functionality:
  1877. */
  1878. void tipc_link_bundle_rcv(struct sk_buff *buf)
  1879. {
  1880. u32 msgcount = msg_msgcnt(buf_msg(buf));
  1881. u32 pos = INT_H_SIZE;
  1882. struct sk_buff *obuf;
  1883. struct tipc_msg *omsg;
  1884. while (msgcount--) {
  1885. obuf = buf_extract(buf, pos);
  1886. if (obuf == NULL) {
  1887. pr_warn("Link unable to unbundle message(s)\n");
  1888. break;
  1889. }
  1890. omsg = buf_msg(obuf);
  1891. pos += align(msg_size(omsg));
  1892. if (msg_isdata(omsg) || (msg_user(omsg) == CONN_MANAGER)) {
  1893. tipc_sk_rcv(obuf);
  1894. } else if (msg_user(omsg) == NAME_DISTRIBUTOR) {
  1895. tipc_named_rcv(obuf);
  1896. } else {
  1897. pr_warn("Illegal bundled msg: %u\n", msg_user(omsg));
  1898. kfree_skb(obuf);
  1899. }
  1900. }
  1901. kfree_skb(buf);
  1902. }
  1903. /*
  1904. * Fragmentation/defragmentation:
  1905. */
  1906. /*
  1907. * tipc_link_frag_xmit: Entry for buffers needing fragmentation.
  1908. * The buffer is complete, inclusive total message length.
  1909. * Returns user data length.
  1910. */
  1911. static int tipc_link_frag_xmit(struct tipc_link *l_ptr, struct sk_buff *buf)
  1912. {
  1913. struct sk_buff *buf_chain = NULL;
  1914. struct sk_buff *buf_chain_tail = (struct sk_buff *)&buf_chain;
  1915. struct tipc_msg *inmsg = buf_msg(buf);
  1916. struct tipc_msg fragm_hdr;
  1917. u32 insize = msg_size(inmsg);
  1918. u32 dsz = msg_data_sz(inmsg);
  1919. unchar *crs = buf->data;
  1920. u32 rest = insize;
  1921. u32 pack_sz = l_ptr->max_pkt;
  1922. u32 fragm_sz = pack_sz - INT_H_SIZE;
  1923. u32 fragm_no = 0;
  1924. u32 destaddr;
  1925. if (msg_short(inmsg))
  1926. destaddr = l_ptr->addr;
  1927. else
  1928. destaddr = msg_destnode(inmsg);
  1929. /* Prepare reusable fragment header: */
  1930. tipc_msg_init(&fragm_hdr, MSG_FRAGMENTER, FIRST_FRAGMENT,
  1931. INT_H_SIZE, destaddr);
  1932. /* Chop up message: */
  1933. while (rest > 0) {
  1934. struct sk_buff *fragm;
  1935. if (rest <= fragm_sz) {
  1936. fragm_sz = rest;
  1937. msg_set_type(&fragm_hdr, LAST_FRAGMENT);
  1938. }
  1939. fragm = tipc_buf_acquire(fragm_sz + INT_H_SIZE);
  1940. if (fragm == NULL) {
  1941. kfree_skb(buf);
  1942. kfree_skb_list(buf_chain);
  1943. return -ENOMEM;
  1944. }
  1945. msg_set_size(&fragm_hdr, fragm_sz + INT_H_SIZE);
  1946. fragm_no++;
  1947. msg_set_fragm_no(&fragm_hdr, fragm_no);
  1948. skb_copy_to_linear_data(fragm, &fragm_hdr, INT_H_SIZE);
  1949. skb_copy_to_linear_data_offset(fragm, INT_H_SIZE, crs,
  1950. fragm_sz);
  1951. buf_chain_tail->next = fragm;
  1952. buf_chain_tail = fragm;
  1953. rest -= fragm_sz;
  1954. crs += fragm_sz;
  1955. msg_set_type(&fragm_hdr, FRAGMENT);
  1956. }
  1957. kfree_skb(buf);
  1958. /* Append chain of fragments to send queue & send them */
  1959. l_ptr->long_msg_seq_no++;
  1960. link_add_chain_to_outqueue(l_ptr, buf_chain, l_ptr->long_msg_seq_no);
  1961. l_ptr->stats.sent_fragments += fragm_no;
  1962. l_ptr->stats.sent_fragmented++;
  1963. tipc_link_push_queue(l_ptr);
  1964. return dsz;
  1965. }
  1966. static void link_set_supervision_props(struct tipc_link *l_ptr, u32 tolerance)
  1967. {
  1968. if ((tolerance < TIPC_MIN_LINK_TOL) || (tolerance > TIPC_MAX_LINK_TOL))
  1969. return;
  1970. l_ptr->tolerance = tolerance;
  1971. l_ptr->continuity_interval =
  1972. ((tolerance / 4) > 500) ? 500 : tolerance / 4;
  1973. l_ptr->abort_limit = tolerance / (l_ptr->continuity_interval / 4);
  1974. }
  1975. void tipc_link_set_queue_limits(struct tipc_link *l_ptr, u32 window)
  1976. {
  1977. /* Data messages from this node, inclusive FIRST_FRAGM */
  1978. l_ptr->queue_limit[TIPC_LOW_IMPORTANCE] = window;
  1979. l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE] = (window / 3) * 4;
  1980. l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE] = (window / 3) * 5;
  1981. l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE] = (window / 3) * 6;
  1982. /* Transiting data messages,inclusive FIRST_FRAGM */
  1983. l_ptr->queue_limit[TIPC_LOW_IMPORTANCE + 4] = 300;
  1984. l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE + 4] = 600;
  1985. l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE + 4] = 900;
  1986. l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE + 4] = 1200;
  1987. l_ptr->queue_limit[CONN_MANAGER] = 1200;
  1988. l_ptr->queue_limit[CHANGEOVER_PROTOCOL] = 2500;
  1989. l_ptr->queue_limit[NAME_DISTRIBUTOR] = 3000;
  1990. /* FRAGMENT and LAST_FRAGMENT packets */
  1991. l_ptr->queue_limit[MSG_FRAGMENTER] = 4000;
  1992. }
  1993. /* tipc_link_find_owner - locate owner node of link by link's name
  1994. * @name: pointer to link name string
  1995. * @bearer_id: pointer to index in 'node->links' array where the link was found.
  1996. *
  1997. * Returns pointer to node owning the link, or 0 if no matching link is found.
  1998. */
  1999. static struct tipc_node *tipc_link_find_owner(const char *link_name,
  2000. unsigned int *bearer_id)
  2001. {
  2002. struct tipc_link *l_ptr;
  2003. struct tipc_node *n_ptr;
  2004. struct tipc_node *found_node = 0;
  2005. int i;
  2006. *bearer_id = 0;
  2007. rcu_read_lock();
  2008. list_for_each_entry_rcu(n_ptr, &tipc_node_list, list) {
  2009. tipc_node_lock(n_ptr);
  2010. for (i = 0; i < MAX_BEARERS; i++) {
  2011. l_ptr = n_ptr->links[i];
  2012. if (l_ptr && !strcmp(l_ptr->name, link_name)) {
  2013. *bearer_id = i;
  2014. found_node = n_ptr;
  2015. break;
  2016. }
  2017. }
  2018. tipc_node_unlock(n_ptr);
  2019. if (found_node)
  2020. break;
  2021. }
  2022. rcu_read_unlock();
  2023. return found_node;
  2024. }
  2025. /**
  2026. * link_value_is_valid -- validate proposed link tolerance/priority/window
  2027. *
  2028. * @cmd: value type (TIPC_CMD_SET_LINK_*)
  2029. * @new_value: the new value
  2030. *
  2031. * Returns 1 if value is within range, 0 if not.
  2032. */
  2033. static int link_value_is_valid(u16 cmd, u32 new_value)
  2034. {
  2035. switch (cmd) {
  2036. case TIPC_CMD_SET_LINK_TOL:
  2037. return (new_value >= TIPC_MIN_LINK_TOL) &&
  2038. (new_value <= TIPC_MAX_LINK_TOL);
  2039. case TIPC_CMD_SET_LINK_PRI:
  2040. return (new_value <= TIPC_MAX_LINK_PRI);
  2041. case TIPC_CMD_SET_LINK_WINDOW:
  2042. return (new_value >= TIPC_MIN_LINK_WIN) &&
  2043. (new_value <= TIPC_MAX_LINK_WIN);
  2044. }
  2045. return 0;
  2046. }
  2047. /**
  2048. * link_cmd_set_value - change priority/tolerance/window for link/bearer/media
  2049. * @name: ptr to link, bearer, or media name
  2050. * @new_value: new value of link, bearer, or media setting
  2051. * @cmd: which link, bearer, or media attribute to set (TIPC_CMD_SET_LINK_*)
  2052. *
  2053. * Caller must hold RTNL lock to ensure link/bearer/media is not deleted.
  2054. *
  2055. * Returns 0 if value updated and negative value on error.
  2056. */
  2057. static int link_cmd_set_value(const char *name, u32 new_value, u16 cmd)
  2058. {
  2059. struct tipc_node *node;
  2060. struct tipc_link *l_ptr;
  2061. struct tipc_bearer *b_ptr;
  2062. struct tipc_media *m_ptr;
  2063. int bearer_id;
  2064. int res = 0;
  2065. node = tipc_link_find_owner(name, &bearer_id);
  2066. if (node) {
  2067. tipc_node_lock(node);
  2068. l_ptr = node->links[bearer_id];
  2069. if (l_ptr) {
  2070. switch (cmd) {
  2071. case TIPC_CMD_SET_LINK_TOL:
  2072. link_set_supervision_props(l_ptr, new_value);
  2073. tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0,
  2074. new_value, 0, 0);
  2075. break;
  2076. case TIPC_CMD_SET_LINK_PRI:
  2077. l_ptr->priority = new_value;
  2078. tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0,
  2079. 0, new_value, 0);
  2080. break;
  2081. case TIPC_CMD_SET_LINK_WINDOW:
  2082. tipc_link_set_queue_limits(l_ptr, new_value);
  2083. break;
  2084. default:
  2085. res = -EINVAL;
  2086. break;
  2087. }
  2088. }
  2089. tipc_node_unlock(node);
  2090. return res;
  2091. }
  2092. b_ptr = tipc_bearer_find(name);
  2093. if (b_ptr) {
  2094. switch (cmd) {
  2095. case TIPC_CMD_SET_LINK_TOL:
  2096. b_ptr->tolerance = new_value;
  2097. break;
  2098. case TIPC_CMD_SET_LINK_PRI:
  2099. b_ptr->priority = new_value;
  2100. break;
  2101. case TIPC_CMD_SET_LINK_WINDOW:
  2102. b_ptr->window = new_value;
  2103. break;
  2104. default:
  2105. res = -EINVAL;
  2106. break;
  2107. }
  2108. return res;
  2109. }
  2110. m_ptr = tipc_media_find(name);
  2111. if (!m_ptr)
  2112. return -ENODEV;
  2113. switch (cmd) {
  2114. case TIPC_CMD_SET_LINK_TOL:
  2115. m_ptr->tolerance = new_value;
  2116. break;
  2117. case TIPC_CMD_SET_LINK_PRI:
  2118. m_ptr->priority = new_value;
  2119. break;
  2120. case TIPC_CMD_SET_LINK_WINDOW:
  2121. m_ptr->window = new_value;
  2122. break;
  2123. default:
  2124. res = -EINVAL;
  2125. break;
  2126. }
  2127. return res;
  2128. }
  2129. struct sk_buff *tipc_link_cmd_config(const void *req_tlv_area, int req_tlv_space,
  2130. u16 cmd)
  2131. {
  2132. struct tipc_link_config *args;
  2133. u32 new_value;
  2134. int res;
  2135. if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_CONFIG))
  2136. return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
  2137. args = (struct tipc_link_config *)TLV_DATA(req_tlv_area);
  2138. new_value = ntohl(args->value);
  2139. if (!link_value_is_valid(cmd, new_value))
  2140. return tipc_cfg_reply_error_string(
  2141. "cannot change, value invalid");
  2142. if (!strcmp(args->name, tipc_bclink_name)) {
  2143. if ((cmd == TIPC_CMD_SET_LINK_WINDOW) &&
  2144. (tipc_bclink_set_queue_limits(new_value) == 0))
  2145. return tipc_cfg_reply_none();
  2146. return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
  2147. " (cannot change setting on broadcast link)");
  2148. }
  2149. res = link_cmd_set_value(args->name, new_value, cmd);
  2150. if (res)
  2151. return tipc_cfg_reply_error_string("cannot change link setting");
  2152. return tipc_cfg_reply_none();
  2153. }
  2154. /**
  2155. * link_reset_statistics - reset link statistics
  2156. * @l_ptr: pointer to link
  2157. */
  2158. static void link_reset_statistics(struct tipc_link *l_ptr)
  2159. {
  2160. memset(&l_ptr->stats, 0, sizeof(l_ptr->stats));
  2161. l_ptr->stats.sent_info = l_ptr->next_out_no;
  2162. l_ptr->stats.recv_info = l_ptr->next_in_no;
  2163. }
  2164. struct sk_buff *tipc_link_cmd_reset_stats(const void *req_tlv_area, int req_tlv_space)
  2165. {
  2166. char *link_name;
  2167. struct tipc_link *l_ptr;
  2168. struct tipc_node *node;
  2169. unsigned int bearer_id;
  2170. if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
  2171. return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
  2172. link_name = (char *)TLV_DATA(req_tlv_area);
  2173. if (!strcmp(link_name, tipc_bclink_name)) {
  2174. if (tipc_bclink_reset_stats())
  2175. return tipc_cfg_reply_error_string("link not found");
  2176. return tipc_cfg_reply_none();
  2177. }
  2178. node = tipc_link_find_owner(link_name, &bearer_id);
  2179. if (!node)
  2180. return tipc_cfg_reply_error_string("link not found");
  2181. tipc_node_lock(node);
  2182. l_ptr = node->links[bearer_id];
  2183. if (!l_ptr) {
  2184. tipc_node_unlock(node);
  2185. return tipc_cfg_reply_error_string("link not found");
  2186. }
  2187. link_reset_statistics(l_ptr);
  2188. tipc_node_unlock(node);
  2189. return tipc_cfg_reply_none();
  2190. }
  2191. /**
  2192. * percent - convert count to a percentage of total (rounding up or down)
  2193. */
  2194. static u32 percent(u32 count, u32 total)
  2195. {
  2196. return (count * 100 + (total / 2)) / total;
  2197. }
  2198. /**
  2199. * tipc_link_stats - print link statistics
  2200. * @name: link name
  2201. * @buf: print buffer area
  2202. * @buf_size: size of print buffer area
  2203. *
  2204. * Returns length of print buffer data string (or 0 if error)
  2205. */
  2206. static int tipc_link_stats(const char *name, char *buf, const u32 buf_size)
  2207. {
  2208. struct tipc_link *l;
  2209. struct tipc_stats *s;
  2210. struct tipc_node *node;
  2211. char *status;
  2212. u32 profile_total = 0;
  2213. unsigned int bearer_id;
  2214. int ret;
  2215. if (!strcmp(name, tipc_bclink_name))
  2216. return tipc_bclink_stats(buf, buf_size);
  2217. node = tipc_link_find_owner(name, &bearer_id);
  2218. if (!node)
  2219. return 0;
  2220. tipc_node_lock(node);
  2221. l = node->links[bearer_id];
  2222. if (!l) {
  2223. tipc_node_unlock(node);
  2224. return 0;
  2225. }
  2226. s = &l->stats;
  2227. if (tipc_link_is_active(l))
  2228. status = "ACTIVE";
  2229. else if (tipc_link_is_up(l))
  2230. status = "STANDBY";
  2231. else
  2232. status = "DEFUNCT";
  2233. ret = tipc_snprintf(buf, buf_size, "Link <%s>\n"
  2234. " %s MTU:%u Priority:%u Tolerance:%u ms"
  2235. " Window:%u packets\n",
  2236. l->name, status, l->max_pkt, l->priority,
  2237. l->tolerance, l->queue_limit[0]);
  2238. ret += tipc_snprintf(buf + ret, buf_size - ret,
  2239. " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
  2240. l->next_in_no - s->recv_info, s->recv_fragments,
  2241. s->recv_fragmented, s->recv_bundles,
  2242. s->recv_bundled);
  2243. ret += tipc_snprintf(buf + ret, buf_size - ret,
  2244. " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
  2245. l->next_out_no - s->sent_info, s->sent_fragments,
  2246. s->sent_fragmented, s->sent_bundles,
  2247. s->sent_bundled);
  2248. profile_total = s->msg_length_counts;
  2249. if (!profile_total)
  2250. profile_total = 1;
  2251. ret += tipc_snprintf(buf + ret, buf_size - ret,
  2252. " TX profile sample:%u packets average:%u octets\n"
  2253. " 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% "
  2254. "-16384:%u%% -32768:%u%% -66000:%u%%\n",
  2255. s->msg_length_counts,
  2256. s->msg_lengths_total / profile_total,
  2257. percent(s->msg_length_profile[0], profile_total),
  2258. percent(s->msg_length_profile[1], profile_total),
  2259. percent(s->msg_length_profile[2], profile_total),
  2260. percent(s->msg_length_profile[3], profile_total),
  2261. percent(s->msg_length_profile[4], profile_total),
  2262. percent(s->msg_length_profile[5], profile_total),
  2263. percent(s->msg_length_profile[6], profile_total));
  2264. ret += tipc_snprintf(buf + ret, buf_size - ret,
  2265. " RX states:%u probes:%u naks:%u defs:%u"
  2266. " dups:%u\n", s->recv_states, s->recv_probes,
  2267. s->recv_nacks, s->deferred_recv, s->duplicates);
  2268. ret += tipc_snprintf(buf + ret, buf_size - ret,
  2269. " TX states:%u probes:%u naks:%u acks:%u"
  2270. " dups:%u\n", s->sent_states, s->sent_probes,
  2271. s->sent_nacks, s->sent_acks, s->retransmitted);
  2272. ret += tipc_snprintf(buf + ret, buf_size - ret,
  2273. " Congestion link:%u Send queue"
  2274. " max:%u avg:%u\n", s->link_congs,
  2275. s->max_queue_sz, s->queue_sz_counts ?
  2276. (s->accu_queue_sz / s->queue_sz_counts) : 0);
  2277. tipc_node_unlock(node);
  2278. return ret;
  2279. }
  2280. struct sk_buff *tipc_link_cmd_show_stats(const void *req_tlv_area, int req_tlv_space)
  2281. {
  2282. struct sk_buff *buf;
  2283. struct tlv_desc *rep_tlv;
  2284. int str_len;
  2285. int pb_len;
  2286. char *pb;
  2287. if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
  2288. return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
  2289. buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
  2290. if (!buf)
  2291. return NULL;
  2292. rep_tlv = (struct tlv_desc *)buf->data;
  2293. pb = TLV_DATA(rep_tlv);
  2294. pb_len = ULTRA_STRING_MAX_LEN;
  2295. str_len = tipc_link_stats((char *)TLV_DATA(req_tlv_area),
  2296. pb, pb_len);
  2297. if (!str_len) {
  2298. kfree_skb(buf);
  2299. return tipc_cfg_reply_error_string("link not found");
  2300. }
  2301. str_len += 1; /* for "\0" */
  2302. skb_put(buf, TLV_SPACE(str_len));
  2303. TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
  2304. return buf;
  2305. }
  2306. /**
  2307. * tipc_link_get_max_pkt - get maximum packet size to use when sending to destination
  2308. * @dest: network address of destination node
  2309. * @selector: used to select from set of active links
  2310. *
  2311. * If no active link can be found, uses default maximum packet size.
  2312. */
  2313. u32 tipc_link_get_max_pkt(u32 dest, u32 selector)
  2314. {
  2315. struct tipc_node *n_ptr;
  2316. struct tipc_link *l_ptr;
  2317. u32 res = MAX_PKT_DEFAULT;
  2318. if (dest == tipc_own_addr)
  2319. return MAX_MSG_SIZE;
  2320. n_ptr = tipc_node_find(dest);
  2321. if (n_ptr) {
  2322. tipc_node_lock(n_ptr);
  2323. l_ptr = n_ptr->active_links[selector & 1];
  2324. if (l_ptr)
  2325. res = l_ptr->max_pkt;
  2326. tipc_node_unlock(n_ptr);
  2327. }
  2328. return res;
  2329. }
  2330. static void link_print(struct tipc_link *l_ptr, const char *str)
  2331. {
  2332. struct tipc_bearer *b_ptr;
  2333. rcu_read_lock();
  2334. b_ptr = rcu_dereference_rtnl(bearer_list[l_ptr->bearer_id]);
  2335. if (b_ptr)
  2336. pr_info("%s Link %x<%s>:", str, l_ptr->addr, b_ptr->name);
  2337. rcu_read_unlock();
  2338. if (link_working_unknown(l_ptr))
  2339. pr_cont(":WU\n");
  2340. else if (link_reset_reset(l_ptr))
  2341. pr_cont(":RR\n");
  2342. else if (link_reset_unknown(l_ptr))
  2343. pr_cont(":RU\n");
  2344. else if (link_working_working(l_ptr))
  2345. pr_cont(":WW\n");
  2346. else
  2347. pr_cont("\n");
  2348. }