port.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704
  1. /*
  2. * net/tipc/port.c: TIPC port code
  3. *
  4. * Copyright (c) 2003-2005, Ericsson Research Canada
  5. * Copyright (c) 2004-2005, Wind River Systems
  6. * Copyright (c) 2005-2006, Ericsson AB
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions are met:
  11. *
  12. * Redistributions of source code must retain the above copyright notice, this
  13. * list of conditions and the following disclaimer.
  14. * Redistributions in binary form must reproduce the above copyright notice,
  15. * this list of conditions and the following disclaimer in the documentation
  16. * and/or other materials provided with the distribution.
  17. * Neither the names of the copyright holders nor the names of its
  18. * contributors may be used to endorse or promote products derived from this
  19. * software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  22. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  25. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  26. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  27. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  28. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  29. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  30. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  31. * POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. #include "core.h"
  34. #include "config.h"
  35. #include "dbg.h"
  36. #include "port.h"
  37. #include "addr.h"
  38. #include "link.h"
  39. #include "node.h"
  40. #include "port.h"
  41. #include "name_table.h"
  42. #include "user_reg.h"
  43. #include "msg.h"
  44. #include "bcast.h"
  45. /* Connection management: */
  46. #define PROBING_INTERVAL 3600000 /* [ms] => 1 h */
  47. #define CONFIRMED 0
  48. #define PROBING 1
  49. #define MAX_REJECT_SIZE 1024
  50. static struct sk_buff *msg_queue_head = 0;
  51. static struct sk_buff *msg_queue_tail = 0;
  52. spinlock_t port_list_lock = SPIN_LOCK_UNLOCKED;
  53. static spinlock_t queue_lock = SPIN_LOCK_UNLOCKED;
  54. LIST_HEAD(ports);
  55. static void port_handle_node_down(unsigned long ref);
  56. static struct sk_buff* port_build_self_abort_msg(struct port *,u32 err);
  57. static struct sk_buff* port_build_peer_abort_msg(struct port *,u32 err);
  58. static void port_timeout(unsigned long ref);
  59. static inline u32 port_peernode(struct port *p_ptr)
  60. {
  61. return msg_destnode(&p_ptr->publ.phdr);
  62. }
  63. static inline u32 port_peerport(struct port *p_ptr)
  64. {
  65. return msg_destport(&p_ptr->publ.phdr);
  66. }
  67. static inline u32 port_out_seqno(struct port *p_ptr)
  68. {
  69. return msg_transp_seqno(&p_ptr->publ.phdr);
  70. }
  71. static inline void port_set_out_seqno(struct port *p_ptr, u32 seqno)
  72. {
  73. msg_set_transp_seqno(&p_ptr->publ.phdr,seqno);
  74. }
  75. static inline void port_incr_out_seqno(struct port *p_ptr)
  76. {
  77. struct tipc_msg *m = &p_ptr->publ.phdr;
  78. if (likely(!msg_routed(m)))
  79. return;
  80. msg_set_transp_seqno(m, (msg_transp_seqno(m) + 1));
  81. }
  82. /**
  83. * tipc_multicast - send a multicast message to local and remote destinations
  84. */
  85. int tipc_multicast(u32 ref, struct tipc_name_seq const *seq, u32 domain,
  86. u32 num_sect, struct iovec const *msg_sect)
  87. {
  88. struct tipc_msg *hdr;
  89. struct sk_buff *buf;
  90. struct sk_buff *ibuf = NULL;
  91. struct port_list dports = {0, NULL, };
  92. struct port *oport = port_deref(ref);
  93. int ext_targets;
  94. int res;
  95. if (unlikely(!oport))
  96. return -EINVAL;
  97. /* Create multicast message */
  98. hdr = &oport->publ.phdr;
  99. msg_set_type(hdr, TIPC_MCAST_MSG);
  100. msg_set_nametype(hdr, seq->type);
  101. msg_set_namelower(hdr, seq->lower);
  102. msg_set_nameupper(hdr, seq->upper);
  103. msg_set_hdr_sz(hdr, MCAST_H_SIZE);
  104. res = msg_build(hdr, msg_sect, num_sect, MAX_MSG_SIZE,
  105. !oport->user_port, &buf);
  106. if (unlikely(!buf))
  107. return res;
  108. /* Figure out where to send multicast message */
  109. ext_targets = nametbl_mc_translate(seq->type, seq->lower, seq->upper,
  110. TIPC_NODE_SCOPE, &dports);
  111. /* Send message to destinations (duplicate it only if necessary) */
  112. if (ext_targets) {
  113. if (dports.count != 0) {
  114. ibuf = skb_copy(buf, GFP_ATOMIC);
  115. if (ibuf == NULL) {
  116. port_list_free(&dports);
  117. buf_discard(buf);
  118. return -ENOMEM;
  119. }
  120. }
  121. res = bclink_send_msg(buf);
  122. if ((res < 0) && (dports.count != 0)) {
  123. buf_discard(ibuf);
  124. }
  125. } else {
  126. ibuf = buf;
  127. }
  128. if (res >= 0) {
  129. if (ibuf)
  130. port_recv_mcast(ibuf, &dports);
  131. } else {
  132. port_list_free(&dports);
  133. }
  134. return res;
  135. }
  136. /**
  137. * port_recv_mcast - deliver multicast message to all destination ports
  138. *
  139. * If there is no port list, perform a lookup to create one
  140. */
  141. void port_recv_mcast(struct sk_buff *buf, struct port_list *dp)
  142. {
  143. struct tipc_msg* msg;
  144. struct port_list dports = {0, NULL, };
  145. struct port_list *item = dp;
  146. int cnt = 0;
  147. assert(buf);
  148. msg = buf_msg(buf);
  149. /* Create destination port list, if one wasn't supplied */
  150. if (dp == NULL) {
  151. nametbl_mc_translate(msg_nametype(msg),
  152. msg_namelower(msg),
  153. msg_nameupper(msg),
  154. TIPC_CLUSTER_SCOPE,
  155. &dports);
  156. item = dp = &dports;
  157. }
  158. /* Deliver a copy of message to each destination port */
  159. if (dp->count != 0) {
  160. if (dp->count == 1) {
  161. msg_set_destport(msg, dp->ports[0]);
  162. port_recv_msg(buf);
  163. port_list_free(dp);
  164. return;
  165. }
  166. for (; cnt < dp->count; cnt++) {
  167. int index = cnt % PLSIZE;
  168. struct sk_buff *b = skb_clone(buf, GFP_ATOMIC);
  169. if (b == NULL) {
  170. warn("Buffer allocation failure\n");
  171. msg_dbg(msg, "LOST:");
  172. goto exit;
  173. }
  174. if ((index == 0) && (cnt != 0)) {
  175. item = item->next;
  176. }
  177. msg_set_destport(buf_msg(b),item->ports[index]);
  178. port_recv_msg(b);
  179. }
  180. }
  181. exit:
  182. buf_discard(buf);
  183. port_list_free(dp);
  184. }
  185. /**
  186. * tipc_createport_raw - create a native TIPC port
  187. *
  188. * Returns local port reference
  189. */
  190. u32 tipc_createport_raw(void *usr_handle,
  191. u32 (*dispatcher)(struct tipc_port *, struct sk_buff *),
  192. void (*wakeup)(struct tipc_port *),
  193. const u32 importance)
  194. {
  195. struct port *p_ptr;
  196. struct tipc_msg *msg;
  197. u32 ref;
  198. p_ptr = kmalloc(sizeof(*p_ptr), GFP_ATOMIC);
  199. if (p_ptr == NULL) {
  200. warn("Memory squeeze; failed to create port\n");
  201. return 0;
  202. }
  203. memset(p_ptr, 0, sizeof(*p_ptr));
  204. ref = ref_acquire(p_ptr, &p_ptr->publ.lock);
  205. if (!ref) {
  206. warn("Reference Table Exhausted\n");
  207. kfree(p_ptr);
  208. return 0;
  209. }
  210. port_lock(ref);
  211. p_ptr->publ.ref = ref;
  212. msg = &p_ptr->publ.phdr;
  213. msg_init(msg, DATA_LOW, TIPC_NAMED_MSG, TIPC_OK, LONG_H_SIZE, 0);
  214. msg_set_orignode(msg, tipc_own_addr);
  215. msg_set_prevnode(msg, tipc_own_addr);
  216. msg_set_origport(msg, ref);
  217. msg_set_importance(msg,importance);
  218. p_ptr->last_in_seqno = 41;
  219. p_ptr->sent = 1;
  220. p_ptr->publ.usr_handle = usr_handle;
  221. INIT_LIST_HEAD(&p_ptr->wait_list);
  222. INIT_LIST_HEAD(&p_ptr->subscription.nodesub_list);
  223. p_ptr->congested_link = 0;
  224. p_ptr->max_pkt = MAX_PKT_DEFAULT;
  225. p_ptr->dispatcher = dispatcher;
  226. p_ptr->wakeup = wakeup;
  227. p_ptr->user_port = 0;
  228. k_init_timer(&p_ptr->timer, (Handler)port_timeout, ref);
  229. spin_lock_bh(&port_list_lock);
  230. INIT_LIST_HEAD(&p_ptr->publications);
  231. INIT_LIST_HEAD(&p_ptr->port_list);
  232. list_add_tail(&p_ptr->port_list, &ports);
  233. spin_unlock_bh(&port_list_lock);
  234. port_unlock(p_ptr);
  235. return ref;
  236. }
  237. int tipc_deleteport(u32 ref)
  238. {
  239. struct port *p_ptr;
  240. struct sk_buff *buf = 0;
  241. tipc_withdraw(ref, 0, 0);
  242. p_ptr = port_lock(ref);
  243. if (!p_ptr)
  244. return -EINVAL;
  245. ref_discard(ref);
  246. port_unlock(p_ptr);
  247. k_cancel_timer(&p_ptr->timer);
  248. if (p_ptr->publ.connected) {
  249. buf = port_build_peer_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
  250. nodesub_unsubscribe(&p_ptr->subscription);
  251. }
  252. if (p_ptr->user_port) {
  253. reg_remove_port(p_ptr->user_port);
  254. kfree(p_ptr->user_port);
  255. }
  256. spin_lock_bh(&port_list_lock);
  257. list_del(&p_ptr->port_list);
  258. list_del(&p_ptr->wait_list);
  259. spin_unlock_bh(&port_list_lock);
  260. k_term_timer(&p_ptr->timer);
  261. kfree(p_ptr);
  262. dbg("Deleted port %u\n", ref);
  263. net_route_msg(buf);
  264. return TIPC_OK;
  265. }
  266. /**
  267. * tipc_get_port() - return port associated with 'ref'
  268. *
  269. * Note: Port is not locked.
  270. */
  271. struct tipc_port *tipc_get_port(const u32 ref)
  272. {
  273. return (struct tipc_port *)ref_deref(ref);
  274. }
  275. /**
  276. * tipc_get_handle - return user handle associated to port 'ref'
  277. */
  278. void *tipc_get_handle(const u32 ref)
  279. {
  280. struct port *p_ptr;
  281. void * handle;
  282. p_ptr = port_lock(ref);
  283. if (!p_ptr)
  284. return 0;
  285. handle = p_ptr->publ.usr_handle;
  286. port_unlock(p_ptr);
  287. return handle;
  288. }
  289. static inline int port_unreliable(struct port *p_ptr)
  290. {
  291. return msg_src_droppable(&p_ptr->publ.phdr);
  292. }
  293. int tipc_portunreliable(u32 ref, unsigned int *isunreliable)
  294. {
  295. struct port *p_ptr;
  296. p_ptr = port_lock(ref);
  297. if (!p_ptr)
  298. return -EINVAL;
  299. *isunreliable = port_unreliable(p_ptr);
  300. spin_unlock_bh(p_ptr->publ.lock);
  301. return TIPC_OK;
  302. }
  303. int tipc_set_portunreliable(u32 ref, unsigned int isunreliable)
  304. {
  305. struct port *p_ptr;
  306. p_ptr = port_lock(ref);
  307. if (!p_ptr)
  308. return -EINVAL;
  309. msg_set_src_droppable(&p_ptr->publ.phdr, (isunreliable != 0));
  310. port_unlock(p_ptr);
  311. return TIPC_OK;
  312. }
  313. static inline int port_unreturnable(struct port *p_ptr)
  314. {
  315. return msg_dest_droppable(&p_ptr->publ.phdr);
  316. }
  317. int tipc_portunreturnable(u32 ref, unsigned int *isunrejectable)
  318. {
  319. struct port *p_ptr;
  320. p_ptr = port_lock(ref);
  321. if (!p_ptr)
  322. return -EINVAL;
  323. *isunrejectable = port_unreturnable(p_ptr);
  324. spin_unlock_bh(p_ptr->publ.lock);
  325. return TIPC_OK;
  326. }
  327. int tipc_set_portunreturnable(u32 ref, unsigned int isunrejectable)
  328. {
  329. struct port *p_ptr;
  330. p_ptr = port_lock(ref);
  331. if (!p_ptr)
  332. return -EINVAL;
  333. msg_set_dest_droppable(&p_ptr->publ.phdr, (isunrejectable != 0));
  334. port_unlock(p_ptr);
  335. return TIPC_OK;
  336. }
  337. /*
  338. * port_build_proto_msg(): build a port level protocol
  339. * or a connection abortion message. Called with
  340. * tipc_port lock on.
  341. */
  342. static struct sk_buff *port_build_proto_msg(u32 destport, u32 destnode,
  343. u32 origport, u32 orignode,
  344. u32 usr, u32 type, u32 err,
  345. u32 seqno, u32 ack)
  346. {
  347. struct sk_buff *buf;
  348. struct tipc_msg *msg;
  349. buf = buf_acquire(LONG_H_SIZE);
  350. if (buf) {
  351. msg = buf_msg(buf);
  352. msg_init(msg, usr, type, err, LONG_H_SIZE, destnode);
  353. msg_set_destport(msg, destport);
  354. msg_set_origport(msg, origport);
  355. msg_set_destnode(msg, destnode);
  356. msg_set_orignode(msg, orignode);
  357. msg_set_transp_seqno(msg, seqno);
  358. msg_set_msgcnt(msg, ack);
  359. msg_dbg(msg, "PORT>SEND>:");
  360. }
  361. return buf;
  362. }
  363. int tipc_set_msg_option(struct tipc_port *tp_ptr, const char *opt, const u32 sz)
  364. {
  365. msg_expand(&tp_ptr->phdr, msg_destnode(&tp_ptr->phdr));
  366. msg_set_options(&tp_ptr->phdr, opt, sz);
  367. return TIPC_OK;
  368. }
  369. int tipc_reject_msg(struct sk_buff *buf, u32 err)
  370. {
  371. struct tipc_msg *msg = buf_msg(buf);
  372. struct sk_buff *rbuf;
  373. struct tipc_msg *rmsg;
  374. int hdr_sz;
  375. u32 imp = msg_importance(msg);
  376. u32 data_sz = msg_data_sz(msg);
  377. if (data_sz > MAX_REJECT_SIZE)
  378. data_sz = MAX_REJECT_SIZE;
  379. if (msg_connected(msg) && (imp < TIPC_CRITICAL_IMPORTANCE))
  380. imp++;
  381. msg_dbg(msg, "port->rej: ");
  382. /* discard rejected message if it shouldn't be returned to sender */
  383. if (msg_errcode(msg) || msg_dest_droppable(msg)) {
  384. buf_discard(buf);
  385. return data_sz;
  386. }
  387. /* construct rejected message */
  388. if (msg_mcast(msg))
  389. hdr_sz = MCAST_H_SIZE;
  390. else
  391. hdr_sz = LONG_H_SIZE;
  392. rbuf = buf_acquire(data_sz + hdr_sz);
  393. if (rbuf == NULL) {
  394. buf_discard(buf);
  395. return data_sz;
  396. }
  397. rmsg = buf_msg(rbuf);
  398. msg_init(rmsg, imp, msg_type(msg), err, hdr_sz, msg_orignode(msg));
  399. msg_set_destport(rmsg, msg_origport(msg));
  400. msg_set_prevnode(rmsg, tipc_own_addr);
  401. msg_set_origport(rmsg, msg_destport(msg));
  402. if (msg_short(msg))
  403. msg_set_orignode(rmsg, tipc_own_addr);
  404. else
  405. msg_set_orignode(rmsg, msg_destnode(msg));
  406. msg_set_size(rmsg, data_sz + hdr_sz);
  407. msg_set_nametype(rmsg, msg_nametype(msg));
  408. msg_set_nameinst(rmsg, msg_nameinst(msg));
  409. memcpy(rbuf->data + hdr_sz, msg_data(msg), data_sz);
  410. /* send self-abort message when rejecting on a connected port */
  411. if (msg_connected(msg)) {
  412. struct sk_buff *abuf = 0;
  413. struct port *p_ptr = port_lock(msg_destport(msg));
  414. if (p_ptr) {
  415. if (p_ptr->publ.connected)
  416. abuf = port_build_self_abort_msg(p_ptr, err);
  417. port_unlock(p_ptr);
  418. }
  419. net_route_msg(abuf);
  420. }
  421. /* send rejected message */
  422. buf_discard(buf);
  423. net_route_msg(rbuf);
  424. return data_sz;
  425. }
  426. int port_reject_sections(struct port *p_ptr, struct tipc_msg *hdr,
  427. struct iovec const *msg_sect, u32 num_sect,
  428. int err)
  429. {
  430. struct sk_buff *buf;
  431. int res;
  432. res = msg_build(hdr, msg_sect, num_sect, MAX_MSG_SIZE,
  433. !p_ptr->user_port, &buf);
  434. if (!buf)
  435. return res;
  436. return tipc_reject_msg(buf, err);
  437. }
  438. static void port_timeout(unsigned long ref)
  439. {
  440. struct port *p_ptr = port_lock(ref);
  441. struct sk_buff *buf = 0;
  442. if (!p_ptr || !p_ptr->publ.connected)
  443. return;
  444. /* Last probe answered ? */
  445. if (p_ptr->probing_state == PROBING) {
  446. buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
  447. } else {
  448. buf = port_build_proto_msg(port_peerport(p_ptr),
  449. port_peernode(p_ptr),
  450. p_ptr->publ.ref,
  451. tipc_own_addr,
  452. CONN_MANAGER,
  453. CONN_PROBE,
  454. TIPC_OK,
  455. port_out_seqno(p_ptr),
  456. 0);
  457. port_incr_out_seqno(p_ptr);
  458. p_ptr->probing_state = PROBING;
  459. k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
  460. }
  461. port_unlock(p_ptr);
  462. net_route_msg(buf);
  463. }
  464. static void port_handle_node_down(unsigned long ref)
  465. {
  466. struct port *p_ptr = port_lock(ref);
  467. struct sk_buff* buf = 0;
  468. if (!p_ptr)
  469. return;
  470. buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_NODE);
  471. port_unlock(p_ptr);
  472. net_route_msg(buf);
  473. }
  474. static struct sk_buff *port_build_self_abort_msg(struct port *p_ptr, u32 err)
  475. {
  476. u32 imp = msg_importance(&p_ptr->publ.phdr);
  477. if (!p_ptr->publ.connected)
  478. return 0;
  479. if (imp < TIPC_CRITICAL_IMPORTANCE)
  480. imp++;
  481. return port_build_proto_msg(p_ptr->publ.ref,
  482. tipc_own_addr,
  483. port_peerport(p_ptr),
  484. port_peernode(p_ptr),
  485. imp,
  486. TIPC_CONN_MSG,
  487. err,
  488. p_ptr->last_in_seqno + 1,
  489. 0);
  490. }
  491. static struct sk_buff *port_build_peer_abort_msg(struct port *p_ptr, u32 err)
  492. {
  493. u32 imp = msg_importance(&p_ptr->publ.phdr);
  494. if (!p_ptr->publ.connected)
  495. return 0;
  496. if (imp < TIPC_CRITICAL_IMPORTANCE)
  497. imp++;
  498. return port_build_proto_msg(port_peerport(p_ptr),
  499. port_peernode(p_ptr),
  500. p_ptr->publ.ref,
  501. tipc_own_addr,
  502. imp,
  503. TIPC_CONN_MSG,
  504. err,
  505. port_out_seqno(p_ptr),
  506. 0);
  507. }
  508. void port_recv_proto_msg(struct sk_buff *buf)
  509. {
  510. struct tipc_msg *msg = buf_msg(buf);
  511. struct port *p_ptr = port_lock(msg_destport(msg));
  512. u32 err = TIPC_OK;
  513. struct sk_buff *r_buf = 0;
  514. struct sk_buff *abort_buf = 0;
  515. msg_dbg(msg, "PORT<RECV<:");
  516. if (!p_ptr) {
  517. err = TIPC_ERR_NO_PORT;
  518. } else if (p_ptr->publ.connected) {
  519. if (port_peernode(p_ptr) != msg_orignode(msg))
  520. err = TIPC_ERR_NO_PORT;
  521. if (port_peerport(p_ptr) != msg_origport(msg))
  522. err = TIPC_ERR_NO_PORT;
  523. if (!err && msg_routed(msg)) {
  524. u32 seqno = msg_transp_seqno(msg);
  525. u32 myno = ++p_ptr->last_in_seqno;
  526. if (seqno != myno) {
  527. err = TIPC_ERR_NO_PORT;
  528. abort_buf = port_build_self_abort_msg(p_ptr, err);
  529. }
  530. }
  531. if (msg_type(msg) == CONN_ACK) {
  532. int wakeup = port_congested(p_ptr) &&
  533. p_ptr->publ.congested &&
  534. p_ptr->wakeup;
  535. p_ptr->acked += msg_msgcnt(msg);
  536. if (port_congested(p_ptr))
  537. goto exit;
  538. p_ptr->publ.congested = 0;
  539. if (!wakeup)
  540. goto exit;
  541. p_ptr->wakeup(&p_ptr->publ);
  542. goto exit;
  543. }
  544. } else if (p_ptr->publ.published) {
  545. err = TIPC_ERR_NO_PORT;
  546. }
  547. if (err) {
  548. r_buf = port_build_proto_msg(msg_origport(msg),
  549. msg_orignode(msg),
  550. msg_destport(msg),
  551. tipc_own_addr,
  552. DATA_HIGH,
  553. TIPC_CONN_MSG,
  554. err,
  555. 0,
  556. 0);
  557. goto exit;
  558. }
  559. /* All is fine */
  560. if (msg_type(msg) == CONN_PROBE) {
  561. r_buf = port_build_proto_msg(msg_origport(msg),
  562. msg_orignode(msg),
  563. msg_destport(msg),
  564. tipc_own_addr,
  565. CONN_MANAGER,
  566. CONN_PROBE_REPLY,
  567. TIPC_OK,
  568. port_out_seqno(p_ptr),
  569. 0);
  570. }
  571. p_ptr->probing_state = CONFIRMED;
  572. port_incr_out_seqno(p_ptr);
  573. exit:
  574. if (p_ptr)
  575. port_unlock(p_ptr);
  576. net_route_msg(r_buf);
  577. net_route_msg(abort_buf);
  578. buf_discard(buf);
  579. }
  580. static void port_print(struct port *p_ptr, struct print_buf *buf, int full_id)
  581. {
  582. struct publication *publ;
  583. if (full_id)
  584. tipc_printf(buf, "<%u.%u.%u:%u>:",
  585. tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
  586. tipc_node(tipc_own_addr), p_ptr->publ.ref);
  587. else
  588. tipc_printf(buf, "%-10u:", p_ptr->publ.ref);
  589. if (p_ptr->publ.connected) {
  590. u32 dport = port_peerport(p_ptr);
  591. u32 destnode = port_peernode(p_ptr);
  592. tipc_printf(buf, " connected to <%u.%u.%u:%u>",
  593. tipc_zone(destnode), tipc_cluster(destnode),
  594. tipc_node(destnode), dport);
  595. if (p_ptr->publ.conn_type != 0)
  596. tipc_printf(buf, " via {%u,%u}",
  597. p_ptr->publ.conn_type,
  598. p_ptr->publ.conn_instance);
  599. }
  600. else if (p_ptr->publ.published) {
  601. tipc_printf(buf, " bound to");
  602. list_for_each_entry(publ, &p_ptr->publications, pport_list) {
  603. if (publ->lower == publ->upper)
  604. tipc_printf(buf, " {%u,%u}", publ->type,
  605. publ->lower);
  606. else
  607. tipc_printf(buf, " {%u,%u,%u}", publ->type,
  608. publ->lower, publ->upper);
  609. }
  610. }
  611. tipc_printf(buf, "\n");
  612. }
  613. #define MAX_PORT_QUERY 32768
  614. struct sk_buff *port_get_ports(void)
  615. {
  616. struct sk_buff *buf;
  617. struct tlv_desc *rep_tlv;
  618. struct print_buf pb;
  619. struct port *p_ptr;
  620. int str_len;
  621. buf = cfg_reply_alloc(TLV_SPACE(MAX_PORT_QUERY));
  622. if (!buf)
  623. return NULL;
  624. rep_tlv = (struct tlv_desc *)buf->data;
  625. printbuf_init(&pb, TLV_DATA(rep_tlv), MAX_PORT_QUERY);
  626. spin_lock_bh(&port_list_lock);
  627. list_for_each_entry(p_ptr, &ports, port_list) {
  628. spin_lock_bh(p_ptr->publ.lock);
  629. port_print(p_ptr, &pb, 0);
  630. spin_unlock_bh(p_ptr->publ.lock);
  631. }
  632. spin_unlock_bh(&port_list_lock);
  633. str_len = printbuf_validate(&pb);
  634. skb_put(buf, TLV_SPACE(str_len));
  635. TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
  636. return buf;
  637. }
  638. #if 0
  639. #define MAX_PORT_STATS 2000
  640. struct sk_buff *port_show_stats(const void *req_tlv_area, int req_tlv_space)
  641. {
  642. u32 ref;
  643. struct port *p_ptr;
  644. struct sk_buff *buf;
  645. struct tlv_desc *rep_tlv;
  646. struct print_buf pb;
  647. int str_len;
  648. if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_PORT_REF))
  649. return cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
  650. ref = *(u32 *)TLV_DATA(req_tlv_area);
  651. ref = ntohl(ref);
  652. p_ptr = port_lock(ref);
  653. if (!p_ptr)
  654. return cfg_reply_error_string("port not found");
  655. buf = cfg_reply_alloc(TLV_SPACE(MAX_PORT_STATS));
  656. if (!buf) {
  657. port_unlock(p_ptr);
  658. return NULL;
  659. }
  660. rep_tlv = (struct tlv_desc *)buf->data;
  661. printbuf_init(&pb, TLV_DATA(rep_tlv), MAX_PORT_STATS);
  662. port_print(p_ptr, &pb, 1);
  663. /* NEED TO FILL IN ADDITIONAL PORT STATISTICS HERE */
  664. port_unlock(p_ptr);
  665. str_len = printbuf_validate(&pb);
  666. skb_put(buf, TLV_SPACE(str_len));
  667. TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
  668. return buf;
  669. }
  670. #endif
  671. void port_reinit(void)
  672. {
  673. struct port *p_ptr;
  674. struct tipc_msg *msg;
  675. spin_lock_bh(&port_list_lock);
  676. list_for_each_entry(p_ptr, &ports, port_list) {
  677. msg = &p_ptr->publ.phdr;
  678. if (msg_orignode(msg) == tipc_own_addr)
  679. break;
  680. msg_set_orignode(msg, tipc_own_addr);
  681. }
  682. spin_unlock_bh(&port_list_lock);
  683. }
  684. /*
  685. * port_dispatcher_sigh(): Signal handler for messages destinated
  686. * to the tipc_port interface.
  687. */
  688. static void port_dispatcher_sigh(void *dummy)
  689. {
  690. struct sk_buff *buf;
  691. spin_lock_bh(&queue_lock);
  692. buf = msg_queue_head;
  693. msg_queue_head = 0;
  694. spin_unlock_bh(&queue_lock);
  695. while (buf) {
  696. struct port *p_ptr;
  697. struct user_port *up_ptr;
  698. struct tipc_portid orig;
  699. struct tipc_name_seq dseq;
  700. void *usr_handle;
  701. int connected;
  702. int published;
  703. struct sk_buff *next = buf->next;
  704. struct tipc_msg *msg = buf_msg(buf);
  705. u32 dref = msg_destport(msg);
  706. p_ptr = port_lock(dref);
  707. if (!p_ptr) {
  708. /* Port deleted while msg in queue */
  709. tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
  710. buf = next;
  711. continue;
  712. }
  713. orig.ref = msg_origport(msg);
  714. orig.node = msg_orignode(msg);
  715. up_ptr = p_ptr->user_port;
  716. usr_handle = up_ptr->usr_handle;
  717. connected = p_ptr->publ.connected;
  718. published = p_ptr->publ.published;
  719. if (unlikely(msg_errcode(msg)))
  720. goto err;
  721. switch (msg_type(msg)) {
  722. case TIPC_CONN_MSG:{
  723. tipc_conn_msg_event cb = up_ptr->conn_msg_cb;
  724. u32 peer_port = port_peerport(p_ptr);
  725. u32 peer_node = port_peernode(p_ptr);
  726. spin_unlock_bh(p_ptr->publ.lock);
  727. if (unlikely(!connected)) {
  728. if (unlikely(published))
  729. goto reject;
  730. tipc_connect2port(dref,&orig);
  731. }
  732. if (unlikely(msg_origport(msg) != peer_port))
  733. goto reject;
  734. if (unlikely(msg_orignode(msg) != peer_node))
  735. goto reject;
  736. if (unlikely(!cb))
  737. goto reject;
  738. if (unlikely(++p_ptr->publ.conn_unacked >=
  739. TIPC_FLOW_CONTROL_WIN))
  740. tipc_acknowledge(dref,
  741. p_ptr->publ.conn_unacked);
  742. skb_pull(buf, msg_hdr_sz(msg));
  743. cb(usr_handle, dref, &buf, msg_data(msg),
  744. msg_data_sz(msg));
  745. break;
  746. }
  747. case TIPC_DIRECT_MSG:{
  748. tipc_msg_event cb = up_ptr->msg_cb;
  749. spin_unlock_bh(p_ptr->publ.lock);
  750. if (unlikely(connected))
  751. goto reject;
  752. if (unlikely(!cb))
  753. goto reject;
  754. skb_pull(buf, msg_hdr_sz(msg));
  755. cb(usr_handle, dref, &buf, msg_data(msg),
  756. msg_data_sz(msg), msg_importance(msg),
  757. &orig);
  758. break;
  759. }
  760. case TIPC_NAMED_MSG:{
  761. tipc_named_msg_event cb = up_ptr->named_msg_cb;
  762. spin_unlock_bh(p_ptr->publ.lock);
  763. if (unlikely(connected))
  764. goto reject;
  765. if (unlikely(!cb))
  766. goto reject;
  767. if (unlikely(!published))
  768. goto reject;
  769. dseq.type = msg_nametype(msg);
  770. dseq.lower = msg_nameinst(msg);
  771. dseq.upper = dseq.lower;
  772. skb_pull(buf, msg_hdr_sz(msg));
  773. cb(usr_handle, dref, &buf, msg_data(msg),
  774. msg_data_sz(msg), msg_importance(msg),
  775. &orig, &dseq);
  776. break;
  777. }
  778. }
  779. if (buf)
  780. buf_discard(buf);
  781. buf = next;
  782. continue;
  783. err:
  784. switch (msg_type(msg)) {
  785. case TIPC_CONN_MSG:{
  786. tipc_conn_shutdown_event cb =
  787. up_ptr->conn_err_cb;
  788. u32 peer_port = port_peerport(p_ptr);
  789. u32 peer_node = port_peernode(p_ptr);
  790. spin_unlock_bh(p_ptr->publ.lock);
  791. if (!connected || !cb)
  792. break;
  793. if (msg_origport(msg) != peer_port)
  794. break;
  795. if (msg_orignode(msg) != peer_node)
  796. break;
  797. tipc_disconnect(dref);
  798. skb_pull(buf, msg_hdr_sz(msg));
  799. cb(usr_handle, dref, &buf, msg_data(msg),
  800. msg_data_sz(msg), msg_errcode(msg));
  801. break;
  802. }
  803. case TIPC_DIRECT_MSG:{
  804. tipc_msg_err_event cb = up_ptr->err_cb;
  805. spin_unlock_bh(p_ptr->publ.lock);
  806. if (connected || !cb)
  807. break;
  808. skb_pull(buf, msg_hdr_sz(msg));
  809. cb(usr_handle, dref, &buf, msg_data(msg),
  810. msg_data_sz(msg), msg_errcode(msg), &orig);
  811. break;
  812. }
  813. case TIPC_NAMED_MSG:{
  814. tipc_named_msg_err_event cb =
  815. up_ptr->named_err_cb;
  816. spin_unlock_bh(p_ptr->publ.lock);
  817. if (connected || !cb)
  818. break;
  819. dseq.type = msg_nametype(msg);
  820. dseq.lower = msg_nameinst(msg);
  821. dseq.upper = dseq.lower;
  822. skb_pull(buf, msg_hdr_sz(msg));
  823. cb(usr_handle, dref, &buf, msg_data(msg),
  824. msg_data_sz(msg), msg_errcode(msg), &dseq);
  825. break;
  826. }
  827. }
  828. if (buf)
  829. buf_discard(buf);
  830. buf = next;
  831. continue;
  832. reject:
  833. tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
  834. buf = next;
  835. }
  836. }
  837. /*
  838. * port_dispatcher(): Dispatcher for messages destinated
  839. * to the tipc_port interface. Called with port locked.
  840. */
  841. static u32 port_dispatcher(struct tipc_port *dummy, struct sk_buff *buf)
  842. {
  843. buf->next = NULL;
  844. spin_lock_bh(&queue_lock);
  845. if (msg_queue_head) {
  846. msg_queue_tail->next = buf;
  847. msg_queue_tail = buf;
  848. } else {
  849. msg_queue_tail = msg_queue_head = buf;
  850. k_signal((Handler)port_dispatcher_sigh, 0);
  851. }
  852. spin_unlock_bh(&queue_lock);
  853. return TIPC_OK;
  854. }
  855. /*
  856. * Wake up port after congestion: Called with port locked,
  857. *
  858. */
  859. static void port_wakeup_sh(unsigned long ref)
  860. {
  861. struct port *p_ptr;
  862. struct user_port *up_ptr;
  863. tipc_continue_event cb = 0;
  864. void *uh = 0;
  865. p_ptr = port_lock(ref);
  866. if (p_ptr) {
  867. up_ptr = p_ptr->user_port;
  868. if (up_ptr) {
  869. cb = up_ptr->continue_event_cb;
  870. uh = up_ptr->usr_handle;
  871. }
  872. port_unlock(p_ptr);
  873. }
  874. if (cb)
  875. cb(uh, ref);
  876. }
  877. static void port_wakeup(struct tipc_port *p_ptr)
  878. {
  879. k_signal((Handler)port_wakeup_sh, p_ptr->ref);
  880. }
  881. void tipc_acknowledge(u32 ref, u32 ack)
  882. {
  883. struct port *p_ptr;
  884. struct sk_buff *buf = 0;
  885. p_ptr = port_lock(ref);
  886. if (!p_ptr)
  887. return;
  888. if (p_ptr->publ.connected) {
  889. p_ptr->publ.conn_unacked -= ack;
  890. buf = port_build_proto_msg(port_peerport(p_ptr),
  891. port_peernode(p_ptr),
  892. ref,
  893. tipc_own_addr,
  894. CONN_MANAGER,
  895. CONN_ACK,
  896. TIPC_OK,
  897. port_out_seqno(p_ptr),
  898. ack);
  899. }
  900. port_unlock(p_ptr);
  901. net_route_msg(buf);
  902. }
  903. /*
  904. * tipc_createport(): user level call. Will add port to
  905. * registry if non-zero user_ref.
  906. */
  907. int tipc_createport(u32 user_ref,
  908. void *usr_handle,
  909. unsigned int importance,
  910. tipc_msg_err_event error_cb,
  911. tipc_named_msg_err_event named_error_cb,
  912. tipc_conn_shutdown_event conn_error_cb,
  913. tipc_msg_event msg_cb,
  914. tipc_named_msg_event named_msg_cb,
  915. tipc_conn_msg_event conn_msg_cb,
  916. tipc_continue_event continue_event_cb,/* May be zero */
  917. u32 *portref)
  918. {
  919. struct user_port *up_ptr;
  920. struct port *p_ptr;
  921. u32 ref;
  922. up_ptr = (struct user_port *)kmalloc(sizeof(*up_ptr), GFP_ATOMIC);
  923. if (up_ptr == NULL) {
  924. return -ENOMEM;
  925. }
  926. ref = tipc_createport_raw(0, port_dispatcher, port_wakeup, importance);
  927. p_ptr = port_lock(ref);
  928. if (!p_ptr) {
  929. kfree(up_ptr);
  930. return -ENOMEM;
  931. }
  932. p_ptr->user_port = up_ptr;
  933. up_ptr->user_ref = user_ref;
  934. up_ptr->usr_handle = usr_handle;
  935. up_ptr->ref = p_ptr->publ.ref;
  936. up_ptr->err_cb = error_cb;
  937. up_ptr->named_err_cb = named_error_cb;
  938. up_ptr->conn_err_cb = conn_error_cb;
  939. up_ptr->msg_cb = msg_cb;
  940. up_ptr->named_msg_cb = named_msg_cb;
  941. up_ptr->conn_msg_cb = conn_msg_cb;
  942. up_ptr->continue_event_cb = continue_event_cb;
  943. INIT_LIST_HEAD(&up_ptr->uport_list);
  944. reg_add_port(up_ptr);
  945. *portref = p_ptr->publ.ref;
  946. dbg(" tipc_createport: %x with ref %u\n", p_ptr, p_ptr->publ.ref);
  947. port_unlock(p_ptr);
  948. return TIPC_OK;
  949. }
  950. int tipc_ownidentity(u32 ref, struct tipc_portid *id)
  951. {
  952. id->ref = ref;
  953. id->node = tipc_own_addr;
  954. return TIPC_OK;
  955. }
  956. int tipc_portimportance(u32 ref, unsigned int *importance)
  957. {
  958. struct port *p_ptr;
  959. p_ptr = port_lock(ref);
  960. if (!p_ptr)
  961. return -EINVAL;
  962. *importance = (unsigned int)msg_importance(&p_ptr->publ.phdr);
  963. spin_unlock_bh(p_ptr->publ.lock);
  964. return TIPC_OK;
  965. }
  966. int tipc_set_portimportance(u32 ref, unsigned int imp)
  967. {
  968. struct port *p_ptr;
  969. if (imp > TIPC_CRITICAL_IMPORTANCE)
  970. return -EINVAL;
  971. p_ptr = port_lock(ref);
  972. if (!p_ptr)
  973. return -EINVAL;
  974. msg_set_importance(&p_ptr->publ.phdr, (u32)imp);
  975. spin_unlock_bh(p_ptr->publ.lock);
  976. return TIPC_OK;
  977. }
  978. int tipc_publish(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
  979. {
  980. struct port *p_ptr;
  981. struct publication *publ;
  982. u32 key;
  983. int res = -EINVAL;
  984. p_ptr = port_lock(ref);
  985. dbg("tipc_publ %u, p_ptr = %x, conn = %x, scope = %x, "
  986. "lower = %u, upper = %u\n",
  987. ref, p_ptr, p_ptr->publ.connected, scope, seq->lower, seq->upper);
  988. if (!p_ptr)
  989. return -EINVAL;
  990. if (p_ptr->publ.connected)
  991. goto exit;
  992. if (seq->lower > seq->upper)
  993. goto exit;
  994. if ((scope < TIPC_ZONE_SCOPE) || (scope > TIPC_NODE_SCOPE))
  995. goto exit;
  996. key = ref + p_ptr->pub_count + 1;
  997. if (key == ref) {
  998. res = -EADDRINUSE;
  999. goto exit;
  1000. }
  1001. publ = nametbl_publish(seq->type, seq->lower, seq->upper,
  1002. scope, p_ptr->publ.ref, key);
  1003. if (publ) {
  1004. list_add(&publ->pport_list, &p_ptr->publications);
  1005. p_ptr->pub_count++;
  1006. p_ptr->publ.published = 1;
  1007. res = TIPC_OK;
  1008. }
  1009. exit:
  1010. port_unlock(p_ptr);
  1011. return res;
  1012. }
  1013. int tipc_withdraw(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
  1014. {
  1015. struct port *p_ptr;
  1016. struct publication *publ;
  1017. struct publication *tpubl;
  1018. int res = -EINVAL;
  1019. p_ptr = port_lock(ref);
  1020. if (!p_ptr)
  1021. return -EINVAL;
  1022. if (!p_ptr->publ.published)
  1023. goto exit;
  1024. if (!seq) {
  1025. list_for_each_entry_safe(publ, tpubl,
  1026. &p_ptr->publications, pport_list) {
  1027. nametbl_withdraw(publ->type, publ->lower,
  1028. publ->ref, publ->key);
  1029. }
  1030. res = TIPC_OK;
  1031. } else {
  1032. list_for_each_entry_safe(publ, tpubl,
  1033. &p_ptr->publications, pport_list) {
  1034. if (publ->scope != scope)
  1035. continue;
  1036. if (publ->type != seq->type)
  1037. continue;
  1038. if (publ->lower != seq->lower)
  1039. continue;
  1040. if (publ->upper != seq->upper)
  1041. break;
  1042. nametbl_withdraw(publ->type, publ->lower,
  1043. publ->ref, publ->key);
  1044. res = TIPC_OK;
  1045. break;
  1046. }
  1047. }
  1048. if (list_empty(&p_ptr->publications))
  1049. p_ptr->publ.published = 0;
  1050. exit:
  1051. port_unlock(p_ptr);
  1052. return res;
  1053. }
  1054. int tipc_connect2port(u32 ref, struct tipc_portid const *peer)
  1055. {
  1056. struct port *p_ptr;
  1057. struct tipc_msg *msg;
  1058. int res = -EINVAL;
  1059. p_ptr = port_lock(ref);
  1060. if (!p_ptr)
  1061. return -EINVAL;
  1062. if (p_ptr->publ.published || p_ptr->publ.connected)
  1063. goto exit;
  1064. if (!peer->ref)
  1065. goto exit;
  1066. msg = &p_ptr->publ.phdr;
  1067. msg_set_destnode(msg, peer->node);
  1068. msg_set_destport(msg, peer->ref);
  1069. msg_set_orignode(msg, tipc_own_addr);
  1070. msg_set_origport(msg, p_ptr->publ.ref);
  1071. msg_set_transp_seqno(msg, 42);
  1072. msg_set_type(msg, TIPC_CONN_MSG);
  1073. if (!may_route(peer->node))
  1074. msg_set_hdr_sz(msg, SHORT_H_SIZE);
  1075. else
  1076. msg_set_hdr_sz(msg, LONG_H_SIZE);
  1077. p_ptr->probing_interval = PROBING_INTERVAL;
  1078. p_ptr->probing_state = CONFIRMED;
  1079. p_ptr->publ.connected = 1;
  1080. k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
  1081. nodesub_subscribe(&p_ptr->subscription,peer->node, (void *)ref,
  1082. (net_ev_handler)port_handle_node_down);
  1083. res = TIPC_OK;
  1084. exit:
  1085. port_unlock(p_ptr);
  1086. p_ptr->max_pkt = link_get_max_pkt(peer->node, ref);
  1087. return res;
  1088. }
  1089. /*
  1090. * tipc_disconnect(): Disconnect port form peer.
  1091. * This is a node local operation.
  1092. */
  1093. int tipc_disconnect(u32 ref)
  1094. {
  1095. struct port *p_ptr;
  1096. int res = -ENOTCONN;
  1097. p_ptr = port_lock(ref);
  1098. if (!p_ptr)
  1099. return -EINVAL;
  1100. if (p_ptr->publ.connected) {
  1101. p_ptr->publ.connected = 0;
  1102. /* let timer expire on it's own to avoid deadlock! */
  1103. nodesub_unsubscribe(&p_ptr->subscription);
  1104. res = TIPC_OK;
  1105. }
  1106. port_unlock(p_ptr);
  1107. return res;
  1108. }
  1109. /*
  1110. * tipc_shutdown(): Send a SHUTDOWN msg to peer and disconnect
  1111. */
  1112. int tipc_shutdown(u32 ref)
  1113. {
  1114. struct port *p_ptr;
  1115. struct sk_buff *buf = 0;
  1116. p_ptr = port_lock(ref);
  1117. if (!p_ptr)
  1118. return -EINVAL;
  1119. if (p_ptr->publ.connected) {
  1120. u32 imp = msg_importance(&p_ptr->publ.phdr);
  1121. if (imp < TIPC_CRITICAL_IMPORTANCE)
  1122. imp++;
  1123. buf = port_build_proto_msg(port_peerport(p_ptr),
  1124. port_peernode(p_ptr),
  1125. ref,
  1126. tipc_own_addr,
  1127. imp,
  1128. TIPC_CONN_MSG,
  1129. TIPC_CONN_SHUTDOWN,
  1130. port_out_seqno(p_ptr),
  1131. 0);
  1132. }
  1133. port_unlock(p_ptr);
  1134. net_route_msg(buf);
  1135. return tipc_disconnect(ref);
  1136. }
  1137. int tipc_isconnected(u32 ref, int *isconnected)
  1138. {
  1139. struct port *p_ptr;
  1140. p_ptr = port_lock(ref);
  1141. if (!p_ptr)
  1142. return -EINVAL;
  1143. *isconnected = p_ptr->publ.connected;
  1144. port_unlock(p_ptr);
  1145. return TIPC_OK;
  1146. }
  1147. int tipc_peer(u32 ref, struct tipc_portid *peer)
  1148. {
  1149. struct port *p_ptr;
  1150. int res;
  1151. p_ptr = port_lock(ref);
  1152. if (!p_ptr)
  1153. return -EINVAL;
  1154. if (p_ptr->publ.connected) {
  1155. peer->ref = port_peerport(p_ptr);
  1156. peer->node = port_peernode(p_ptr);
  1157. res = TIPC_OK;
  1158. } else
  1159. res = -ENOTCONN;
  1160. port_unlock(p_ptr);
  1161. return res;
  1162. }
  1163. int tipc_ref_valid(u32 ref)
  1164. {
  1165. /* Works irrespective of type */
  1166. return !!ref_deref(ref);
  1167. }
  1168. /*
  1169. * port_recv_sections(): Concatenate and deliver sectioned
  1170. * message for this node.
  1171. */
  1172. int port_recv_sections(struct port *sender, unsigned int num_sect,
  1173. struct iovec const *msg_sect)
  1174. {
  1175. struct sk_buff *buf;
  1176. int res;
  1177. res = msg_build(&sender->publ.phdr, msg_sect, num_sect,
  1178. MAX_MSG_SIZE, !sender->user_port, &buf);
  1179. if (likely(buf))
  1180. port_recv_msg(buf);
  1181. return res;
  1182. }
  1183. /**
  1184. * tipc_send - send message sections on connection
  1185. */
  1186. int tipc_send(u32 ref, unsigned int num_sect, struct iovec const *msg_sect)
  1187. {
  1188. struct port *p_ptr;
  1189. u32 destnode;
  1190. int res;
  1191. p_ptr = port_deref(ref);
  1192. if (!p_ptr || !p_ptr->publ.connected)
  1193. return -EINVAL;
  1194. p_ptr->publ.congested = 1;
  1195. if (!port_congested(p_ptr)) {
  1196. destnode = port_peernode(p_ptr);
  1197. if (likely(destnode != tipc_own_addr))
  1198. res = link_send_sections_fast(p_ptr, msg_sect, num_sect,
  1199. destnode);
  1200. else
  1201. res = port_recv_sections(p_ptr, num_sect, msg_sect);
  1202. if (likely(res != -ELINKCONG)) {
  1203. port_incr_out_seqno(p_ptr);
  1204. p_ptr->publ.congested = 0;
  1205. p_ptr->sent++;
  1206. return res;
  1207. }
  1208. }
  1209. if (port_unreliable(p_ptr)) {
  1210. p_ptr->publ.congested = 0;
  1211. /* Just calculate msg length and return */
  1212. return msg_calc_data_size(msg_sect, num_sect);
  1213. }
  1214. return -ELINKCONG;
  1215. }
  1216. /**
  1217. * tipc_send_buf - send message buffer on connection
  1218. */
  1219. int tipc_send_buf(u32 ref, struct sk_buff *buf, unsigned int dsz)
  1220. {
  1221. struct port *p_ptr;
  1222. struct tipc_msg *msg;
  1223. u32 destnode;
  1224. u32 hsz;
  1225. u32 sz;
  1226. u32 res;
  1227. p_ptr = port_deref(ref);
  1228. if (!p_ptr || !p_ptr->publ.connected)
  1229. return -EINVAL;
  1230. msg = &p_ptr->publ.phdr;
  1231. hsz = msg_hdr_sz(msg);
  1232. sz = hsz + dsz;
  1233. msg_set_size(msg, sz);
  1234. if (skb_cow(buf, hsz))
  1235. return -ENOMEM;
  1236. skb_push(buf, hsz);
  1237. memcpy(buf->data, (unchar *)msg, hsz);
  1238. destnode = msg_destnode(msg);
  1239. p_ptr->publ.congested = 1;
  1240. if (!port_congested(p_ptr)) {
  1241. if (likely(destnode != tipc_own_addr))
  1242. res = tipc_send_buf_fast(buf, destnode);
  1243. else {
  1244. port_recv_msg(buf);
  1245. res = sz;
  1246. }
  1247. if (likely(res != -ELINKCONG)) {
  1248. port_incr_out_seqno(p_ptr);
  1249. p_ptr->sent++;
  1250. p_ptr->publ.congested = 0;
  1251. return res;
  1252. }
  1253. }
  1254. if (port_unreliable(p_ptr)) {
  1255. p_ptr->publ.congested = 0;
  1256. return dsz;
  1257. }
  1258. return -ELINKCONG;
  1259. }
  1260. /**
  1261. * tipc_forward2name - forward message sections to port name
  1262. */
  1263. int tipc_forward2name(u32 ref,
  1264. struct tipc_name const *name,
  1265. u32 domain,
  1266. u32 num_sect,
  1267. struct iovec const *msg_sect,
  1268. struct tipc_portid const *orig,
  1269. unsigned int importance)
  1270. {
  1271. struct port *p_ptr;
  1272. struct tipc_msg *msg;
  1273. u32 destnode = domain;
  1274. u32 destport = 0;
  1275. int res;
  1276. p_ptr = port_deref(ref);
  1277. if (!p_ptr || p_ptr->publ.connected)
  1278. return -EINVAL;
  1279. msg = &p_ptr->publ.phdr;
  1280. msg_set_type(msg, TIPC_NAMED_MSG);
  1281. msg_set_orignode(msg, orig->node);
  1282. msg_set_origport(msg, orig->ref);
  1283. msg_set_hdr_sz(msg, LONG_H_SIZE);
  1284. msg_set_nametype(msg, name->type);
  1285. msg_set_nameinst(msg, name->instance);
  1286. msg_set_lookup_scope(msg, addr_scope(domain));
  1287. if (importance <= TIPC_CRITICAL_IMPORTANCE)
  1288. msg_set_importance(msg,importance);
  1289. destport = nametbl_translate(name->type, name->instance, &destnode);
  1290. msg_set_destnode(msg, destnode);
  1291. msg_set_destport(msg, destport);
  1292. if (likely(destport || destnode)) {
  1293. p_ptr->sent++;
  1294. if (likely(destnode == tipc_own_addr))
  1295. return port_recv_sections(p_ptr, num_sect, msg_sect);
  1296. res = link_send_sections_fast(p_ptr, msg_sect, num_sect,
  1297. destnode);
  1298. if (likely(res != -ELINKCONG))
  1299. return res;
  1300. if (port_unreliable(p_ptr)) {
  1301. /* Just calculate msg length and return */
  1302. return msg_calc_data_size(msg_sect, num_sect);
  1303. }
  1304. return -ELINKCONG;
  1305. }
  1306. return port_reject_sections(p_ptr, msg, msg_sect, num_sect,
  1307. TIPC_ERR_NO_NAME);
  1308. }
  1309. /**
  1310. * tipc_send2name - send message sections to port name
  1311. */
  1312. int tipc_send2name(u32 ref,
  1313. struct tipc_name const *name,
  1314. unsigned int domain,
  1315. unsigned int num_sect,
  1316. struct iovec const *msg_sect)
  1317. {
  1318. struct tipc_portid orig;
  1319. orig.ref = ref;
  1320. orig.node = tipc_own_addr;
  1321. return tipc_forward2name(ref, name, domain, num_sect, msg_sect, &orig,
  1322. TIPC_PORT_IMPORTANCE);
  1323. }
  1324. /**
  1325. * tipc_forward_buf2name - forward message buffer to port name
  1326. */
  1327. int tipc_forward_buf2name(u32 ref,
  1328. struct tipc_name const *name,
  1329. u32 domain,
  1330. struct sk_buff *buf,
  1331. unsigned int dsz,
  1332. struct tipc_portid const *orig,
  1333. unsigned int importance)
  1334. {
  1335. struct port *p_ptr;
  1336. struct tipc_msg *msg;
  1337. u32 destnode = domain;
  1338. u32 destport = 0;
  1339. int res;
  1340. p_ptr = (struct port *)ref_deref(ref);
  1341. if (!p_ptr || p_ptr->publ.connected)
  1342. return -EINVAL;
  1343. msg = &p_ptr->publ.phdr;
  1344. if (importance <= TIPC_CRITICAL_IMPORTANCE)
  1345. msg_set_importance(msg, importance);
  1346. msg_set_type(msg, TIPC_NAMED_MSG);
  1347. msg_set_orignode(msg, orig->node);
  1348. msg_set_origport(msg, orig->ref);
  1349. msg_set_nametype(msg, name->type);
  1350. msg_set_nameinst(msg, name->instance);
  1351. msg_set_lookup_scope(msg, addr_scope(domain));
  1352. msg_set_hdr_sz(msg, LONG_H_SIZE);
  1353. msg_set_size(msg, LONG_H_SIZE + dsz);
  1354. destport = nametbl_translate(name->type, name->instance, &destnode);
  1355. msg_set_destnode(msg, destnode);
  1356. msg_set_destport(msg, destport);
  1357. msg_dbg(msg, "forw2name ==> ");
  1358. if (skb_cow(buf, LONG_H_SIZE))
  1359. return -ENOMEM;
  1360. skb_push(buf, LONG_H_SIZE);
  1361. memcpy(buf->data, (unchar *)msg, LONG_H_SIZE);
  1362. msg_dbg(buf_msg(buf),"PREP:");
  1363. if (likely(destport || destnode)) {
  1364. p_ptr->sent++;
  1365. if (destnode == tipc_own_addr)
  1366. return port_recv_msg(buf);
  1367. res = tipc_send_buf_fast(buf, destnode);
  1368. if (likely(res != -ELINKCONG))
  1369. return res;
  1370. if (port_unreliable(p_ptr))
  1371. return dsz;
  1372. return -ELINKCONG;
  1373. }
  1374. return tipc_reject_msg(buf, TIPC_ERR_NO_NAME);
  1375. }
  1376. /**
  1377. * tipc_send_buf2name - send message buffer to port name
  1378. */
  1379. int tipc_send_buf2name(u32 ref,
  1380. struct tipc_name const *dest,
  1381. u32 domain,
  1382. struct sk_buff *buf,
  1383. unsigned int dsz)
  1384. {
  1385. struct tipc_portid orig;
  1386. orig.ref = ref;
  1387. orig.node = tipc_own_addr;
  1388. return tipc_forward_buf2name(ref, dest, domain, buf, dsz, &orig,
  1389. TIPC_PORT_IMPORTANCE);
  1390. }
  1391. /**
  1392. * tipc_forward2port - forward message sections to port identity
  1393. */
  1394. int tipc_forward2port(u32 ref,
  1395. struct tipc_portid const *dest,
  1396. unsigned int num_sect,
  1397. struct iovec const *msg_sect,
  1398. struct tipc_portid const *orig,
  1399. unsigned int importance)
  1400. {
  1401. struct port *p_ptr;
  1402. struct tipc_msg *msg;
  1403. int res;
  1404. p_ptr = port_deref(ref);
  1405. if (!p_ptr || p_ptr->publ.connected)
  1406. return -EINVAL;
  1407. msg = &p_ptr->publ.phdr;
  1408. msg_set_type(msg, TIPC_DIRECT_MSG);
  1409. msg_set_orignode(msg, orig->node);
  1410. msg_set_origport(msg, orig->ref);
  1411. msg_set_destnode(msg, dest->node);
  1412. msg_set_destport(msg, dest->ref);
  1413. msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
  1414. if (importance <= TIPC_CRITICAL_IMPORTANCE)
  1415. msg_set_importance(msg, importance);
  1416. p_ptr->sent++;
  1417. if (dest->node == tipc_own_addr)
  1418. return port_recv_sections(p_ptr, num_sect, msg_sect);
  1419. res = link_send_sections_fast(p_ptr, msg_sect, num_sect, dest->node);
  1420. if (likely(res != -ELINKCONG))
  1421. return res;
  1422. if (port_unreliable(p_ptr)) {
  1423. /* Just calculate msg length and return */
  1424. return msg_calc_data_size(msg_sect, num_sect);
  1425. }
  1426. return -ELINKCONG;
  1427. }
  1428. /**
  1429. * tipc_send2port - send message sections to port identity
  1430. */
  1431. int tipc_send2port(u32 ref,
  1432. struct tipc_portid const *dest,
  1433. unsigned int num_sect,
  1434. struct iovec const *msg_sect)
  1435. {
  1436. struct tipc_portid orig;
  1437. orig.ref = ref;
  1438. orig.node = tipc_own_addr;
  1439. return tipc_forward2port(ref, dest, num_sect, msg_sect, &orig,
  1440. TIPC_PORT_IMPORTANCE);
  1441. }
  1442. /**
  1443. * tipc_forward_buf2port - forward message buffer to port identity
  1444. */
  1445. int tipc_forward_buf2port(u32 ref,
  1446. struct tipc_portid const *dest,
  1447. struct sk_buff *buf,
  1448. unsigned int dsz,
  1449. struct tipc_portid const *orig,
  1450. unsigned int importance)
  1451. {
  1452. struct port *p_ptr;
  1453. struct tipc_msg *msg;
  1454. int res;
  1455. p_ptr = (struct port *)ref_deref(ref);
  1456. if (!p_ptr || p_ptr->publ.connected)
  1457. return -EINVAL;
  1458. msg = &p_ptr->publ.phdr;
  1459. msg_set_type(msg, TIPC_DIRECT_MSG);
  1460. msg_set_orignode(msg, orig->node);
  1461. msg_set_origport(msg, orig->ref);
  1462. msg_set_destnode(msg, dest->node);
  1463. msg_set_destport(msg, dest->ref);
  1464. msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
  1465. if (importance <= TIPC_CRITICAL_IMPORTANCE)
  1466. msg_set_importance(msg, importance);
  1467. msg_set_size(msg, DIR_MSG_H_SIZE + dsz);
  1468. if (skb_cow(buf, DIR_MSG_H_SIZE))
  1469. return -ENOMEM;
  1470. skb_push(buf, DIR_MSG_H_SIZE);
  1471. memcpy(buf->data, (unchar *)msg, DIR_MSG_H_SIZE);
  1472. msg_dbg(msg, "buf2port: ");
  1473. p_ptr->sent++;
  1474. if (dest->node == tipc_own_addr)
  1475. return port_recv_msg(buf);
  1476. res = tipc_send_buf_fast(buf, dest->node);
  1477. if (likely(res != -ELINKCONG))
  1478. return res;
  1479. if (port_unreliable(p_ptr))
  1480. return dsz;
  1481. return -ELINKCONG;
  1482. }
  1483. /**
  1484. * tipc_send_buf2port - send message buffer to port identity
  1485. */
  1486. int tipc_send_buf2port(u32 ref,
  1487. struct tipc_portid const *dest,
  1488. struct sk_buff *buf,
  1489. unsigned int dsz)
  1490. {
  1491. struct tipc_portid orig;
  1492. orig.ref = ref;
  1493. orig.node = tipc_own_addr;
  1494. return tipc_forward_buf2port(ref, dest, buf, dsz, &orig,
  1495. TIPC_PORT_IMPORTANCE);
  1496. }