link.c 73 KB

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