port.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350
  1. /*
  2. * net/tipc/port.c: TIPC port code
  3. *
  4. * Copyright (c) 1992-2007, Ericsson AB
  5. * Copyright (c) 2004-2008, 2010-2011, 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 "config.h"
  38. #include "port.h"
  39. #include "name_table.h"
  40. /* Connection management: */
  41. #define PROBING_INTERVAL 3600000 /* [ms] => 1 h */
  42. #define CONFIRMED 0
  43. #define PROBING 1
  44. #define MAX_REJECT_SIZE 1024
  45. static struct sk_buff *msg_queue_head;
  46. static struct sk_buff *msg_queue_tail;
  47. DEFINE_SPINLOCK(tipc_port_list_lock);
  48. static DEFINE_SPINLOCK(queue_lock);
  49. static LIST_HEAD(ports);
  50. static void port_handle_node_down(unsigned long ref);
  51. static struct sk_buff *port_build_self_abort_msg(struct tipc_port *, u32 err);
  52. static struct sk_buff *port_build_peer_abort_msg(struct tipc_port *, u32 err);
  53. static void port_timeout(unsigned long ref);
  54. static u32 port_peernode(struct tipc_port *p_ptr)
  55. {
  56. return msg_destnode(&p_ptr->phdr);
  57. }
  58. static u32 port_peerport(struct tipc_port *p_ptr)
  59. {
  60. return msg_destport(&p_ptr->phdr);
  61. }
  62. /**
  63. * tipc_multicast - send a multicast message to local and remote destinations
  64. */
  65. int tipc_multicast(u32 ref, struct tipc_name_seq const *seq,
  66. u32 num_sect, struct iovec const *msg_sect)
  67. {
  68. struct tipc_msg *hdr;
  69. struct sk_buff *buf;
  70. struct sk_buff *ibuf = NULL;
  71. struct port_list dports = {0, NULL, };
  72. struct tipc_port *oport = tipc_port_deref(ref);
  73. int ext_targets;
  74. int res;
  75. if (unlikely(!oport))
  76. return -EINVAL;
  77. /* Create multicast message */
  78. hdr = &oport->phdr;
  79. msg_set_type(hdr, TIPC_MCAST_MSG);
  80. msg_set_lookup_scope(hdr, TIPC_CLUSTER_SCOPE);
  81. msg_set_destport(hdr, 0);
  82. msg_set_destnode(hdr, 0);
  83. msg_set_nametype(hdr, seq->type);
  84. msg_set_namelower(hdr, seq->lower);
  85. msg_set_nameupper(hdr, seq->upper);
  86. msg_set_hdr_sz(hdr, MCAST_H_SIZE);
  87. res = tipc_msg_build(hdr, msg_sect, num_sect, MAX_MSG_SIZE,
  88. !oport->user_port, &buf);
  89. if (unlikely(!buf))
  90. return res;
  91. /* Figure out where to send multicast message */
  92. ext_targets = tipc_nametbl_mc_translate(seq->type, seq->lower, seq->upper,
  93. TIPC_NODE_SCOPE, &dports);
  94. /* Send message to destinations (duplicate it only if necessary) */
  95. if (ext_targets) {
  96. if (dports.count != 0) {
  97. ibuf = skb_copy(buf, GFP_ATOMIC);
  98. if (ibuf == NULL) {
  99. tipc_port_list_free(&dports);
  100. buf_discard(buf);
  101. return -ENOMEM;
  102. }
  103. }
  104. res = tipc_bclink_send_msg(buf);
  105. if ((res < 0) && (dports.count != 0))
  106. buf_discard(ibuf);
  107. } else {
  108. ibuf = buf;
  109. }
  110. if (res >= 0) {
  111. if (ibuf)
  112. tipc_port_recv_mcast(ibuf, &dports);
  113. } else {
  114. tipc_port_list_free(&dports);
  115. }
  116. return res;
  117. }
  118. /**
  119. * tipc_port_recv_mcast - deliver multicast message to all destination ports
  120. *
  121. * If there is no port list, perform a lookup to create one
  122. */
  123. void tipc_port_recv_mcast(struct sk_buff *buf, struct port_list *dp)
  124. {
  125. struct tipc_msg *msg;
  126. struct port_list dports = {0, NULL, };
  127. struct port_list *item = dp;
  128. int cnt = 0;
  129. msg = buf_msg(buf);
  130. /* Create destination port list, if one wasn't supplied */
  131. if (dp == NULL) {
  132. tipc_nametbl_mc_translate(msg_nametype(msg),
  133. msg_namelower(msg),
  134. msg_nameupper(msg),
  135. TIPC_CLUSTER_SCOPE,
  136. &dports);
  137. item = dp = &dports;
  138. }
  139. /* Deliver a copy of message to each destination port */
  140. if (dp->count != 0) {
  141. msg_set_destnode(msg, tipc_own_addr);
  142. if (dp->count == 1) {
  143. msg_set_destport(msg, dp->ports[0]);
  144. tipc_port_recv_msg(buf);
  145. tipc_port_list_free(dp);
  146. return;
  147. }
  148. for (; cnt < dp->count; cnt++) {
  149. int index = cnt % PLSIZE;
  150. struct sk_buff *b = skb_clone(buf, GFP_ATOMIC);
  151. if (b == NULL) {
  152. warn("Unable to deliver multicast message(s)\n");
  153. goto exit;
  154. }
  155. if ((index == 0) && (cnt != 0))
  156. item = item->next;
  157. msg_set_destport(buf_msg(b), item->ports[index]);
  158. tipc_port_recv_msg(b);
  159. }
  160. }
  161. exit:
  162. buf_discard(buf);
  163. tipc_port_list_free(dp);
  164. }
  165. /**
  166. * tipc_createport_raw - create a generic TIPC port
  167. *
  168. * Returns pointer to (locked) TIPC port, or NULL if unable to create it
  169. */
  170. struct tipc_port *tipc_createport_raw(void *usr_handle,
  171. u32 (*dispatcher)(struct tipc_port *, struct sk_buff *),
  172. void (*wakeup)(struct tipc_port *),
  173. const u32 importance)
  174. {
  175. struct tipc_port *p_ptr;
  176. struct tipc_msg *msg;
  177. u32 ref;
  178. p_ptr = kzalloc(sizeof(*p_ptr), GFP_ATOMIC);
  179. if (!p_ptr) {
  180. warn("Port creation failed, no memory\n");
  181. return NULL;
  182. }
  183. ref = tipc_ref_acquire(p_ptr, &p_ptr->lock);
  184. if (!ref) {
  185. warn("Port creation failed, reference table exhausted\n");
  186. kfree(p_ptr);
  187. return NULL;
  188. }
  189. p_ptr->usr_handle = usr_handle;
  190. p_ptr->max_pkt = MAX_PKT_DEFAULT;
  191. p_ptr->ref = ref;
  192. msg = &p_ptr->phdr;
  193. tipc_msg_init(msg, importance, TIPC_NAMED_MSG, LONG_H_SIZE, 0);
  194. msg_set_origport(msg, ref);
  195. INIT_LIST_HEAD(&p_ptr->wait_list);
  196. INIT_LIST_HEAD(&p_ptr->subscription.nodesub_list);
  197. p_ptr->dispatcher = dispatcher;
  198. p_ptr->wakeup = wakeup;
  199. p_ptr->user_port = NULL;
  200. k_init_timer(&p_ptr->timer, (Handler)port_timeout, ref);
  201. spin_lock_bh(&tipc_port_list_lock);
  202. INIT_LIST_HEAD(&p_ptr->publications);
  203. INIT_LIST_HEAD(&p_ptr->port_list);
  204. list_add_tail(&p_ptr->port_list, &ports);
  205. spin_unlock_bh(&tipc_port_list_lock);
  206. return p_ptr;
  207. }
  208. int tipc_deleteport(u32 ref)
  209. {
  210. struct tipc_port *p_ptr;
  211. struct sk_buff *buf = NULL;
  212. tipc_withdraw(ref, 0, NULL);
  213. p_ptr = tipc_port_lock(ref);
  214. if (!p_ptr)
  215. return -EINVAL;
  216. tipc_ref_discard(ref);
  217. tipc_port_unlock(p_ptr);
  218. k_cancel_timer(&p_ptr->timer);
  219. if (p_ptr->connected) {
  220. buf = port_build_peer_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
  221. tipc_nodesub_unsubscribe(&p_ptr->subscription);
  222. }
  223. kfree(p_ptr->user_port);
  224. spin_lock_bh(&tipc_port_list_lock);
  225. list_del(&p_ptr->port_list);
  226. list_del(&p_ptr->wait_list);
  227. spin_unlock_bh(&tipc_port_list_lock);
  228. k_term_timer(&p_ptr->timer);
  229. kfree(p_ptr);
  230. tipc_net_route_msg(buf);
  231. return 0;
  232. }
  233. static int port_unreliable(struct tipc_port *p_ptr)
  234. {
  235. return msg_src_droppable(&p_ptr->phdr);
  236. }
  237. int tipc_portunreliable(u32 ref, unsigned int *isunreliable)
  238. {
  239. struct tipc_port *p_ptr;
  240. p_ptr = tipc_port_lock(ref);
  241. if (!p_ptr)
  242. return -EINVAL;
  243. *isunreliable = port_unreliable(p_ptr);
  244. tipc_port_unlock(p_ptr);
  245. return 0;
  246. }
  247. int tipc_set_portunreliable(u32 ref, unsigned int isunreliable)
  248. {
  249. struct tipc_port *p_ptr;
  250. p_ptr = tipc_port_lock(ref);
  251. if (!p_ptr)
  252. return -EINVAL;
  253. msg_set_src_droppable(&p_ptr->phdr, (isunreliable != 0));
  254. tipc_port_unlock(p_ptr);
  255. return 0;
  256. }
  257. static int port_unreturnable(struct tipc_port *p_ptr)
  258. {
  259. return msg_dest_droppable(&p_ptr->phdr);
  260. }
  261. int tipc_portunreturnable(u32 ref, unsigned int *isunrejectable)
  262. {
  263. struct tipc_port *p_ptr;
  264. p_ptr = tipc_port_lock(ref);
  265. if (!p_ptr)
  266. return -EINVAL;
  267. *isunrejectable = port_unreturnable(p_ptr);
  268. tipc_port_unlock(p_ptr);
  269. return 0;
  270. }
  271. int tipc_set_portunreturnable(u32 ref, unsigned int isunrejectable)
  272. {
  273. struct tipc_port *p_ptr;
  274. p_ptr = tipc_port_lock(ref);
  275. if (!p_ptr)
  276. return -EINVAL;
  277. msg_set_dest_droppable(&p_ptr->phdr, (isunrejectable != 0));
  278. tipc_port_unlock(p_ptr);
  279. return 0;
  280. }
  281. /*
  282. * port_build_proto_msg(): build a port level protocol
  283. * or a connection abortion message. Called with
  284. * tipc_port lock on.
  285. */
  286. static struct sk_buff *port_build_proto_msg(u32 destport, u32 destnode,
  287. u32 origport, u32 orignode,
  288. u32 usr, u32 type, u32 err,
  289. u32 ack)
  290. {
  291. struct sk_buff *buf;
  292. struct tipc_msg *msg;
  293. buf = tipc_buf_acquire(LONG_H_SIZE);
  294. if (buf) {
  295. msg = buf_msg(buf);
  296. tipc_msg_init(msg, usr, type, LONG_H_SIZE, destnode);
  297. msg_set_errcode(msg, err);
  298. msg_set_destport(msg, destport);
  299. msg_set_origport(msg, origport);
  300. msg_set_orignode(msg, orignode);
  301. msg_set_msgcnt(msg, ack);
  302. }
  303. return buf;
  304. }
  305. int tipc_reject_msg(struct sk_buff *buf, u32 err)
  306. {
  307. struct tipc_msg *msg = buf_msg(buf);
  308. struct sk_buff *rbuf;
  309. struct tipc_msg *rmsg;
  310. int hdr_sz;
  311. u32 imp = msg_importance(msg);
  312. u32 data_sz = msg_data_sz(msg);
  313. if (data_sz > MAX_REJECT_SIZE)
  314. data_sz = MAX_REJECT_SIZE;
  315. if (msg_connected(msg) && (imp < TIPC_CRITICAL_IMPORTANCE))
  316. imp++;
  317. /* discard rejected message if it shouldn't be returned to sender */
  318. if (msg_errcode(msg) || msg_dest_droppable(msg)) {
  319. buf_discard(buf);
  320. return data_sz;
  321. }
  322. /* construct rejected message */
  323. if (msg_mcast(msg))
  324. hdr_sz = MCAST_H_SIZE;
  325. else
  326. hdr_sz = LONG_H_SIZE;
  327. rbuf = tipc_buf_acquire(data_sz + hdr_sz);
  328. if (rbuf == NULL) {
  329. buf_discard(buf);
  330. return data_sz;
  331. }
  332. rmsg = buf_msg(rbuf);
  333. tipc_msg_init(rmsg, imp, msg_type(msg), hdr_sz, msg_orignode(msg));
  334. msg_set_errcode(rmsg, err);
  335. msg_set_destport(rmsg, msg_origport(msg));
  336. msg_set_origport(rmsg, msg_destport(msg));
  337. if (msg_short(msg)) {
  338. msg_set_orignode(rmsg, tipc_own_addr);
  339. /* leave name type & instance as zeroes */
  340. } else {
  341. msg_set_orignode(rmsg, msg_destnode(msg));
  342. msg_set_nametype(rmsg, msg_nametype(msg));
  343. msg_set_nameinst(rmsg, msg_nameinst(msg));
  344. }
  345. msg_set_size(rmsg, data_sz + hdr_sz);
  346. skb_copy_to_linear_data_offset(rbuf, hdr_sz, msg_data(msg), data_sz);
  347. /* send self-abort message when rejecting on a connected port */
  348. if (msg_connected(msg)) {
  349. struct sk_buff *abuf = NULL;
  350. struct tipc_port *p_ptr = tipc_port_lock(msg_destport(msg));
  351. if (p_ptr) {
  352. if (p_ptr->connected)
  353. abuf = port_build_self_abort_msg(p_ptr, err);
  354. tipc_port_unlock(p_ptr);
  355. }
  356. tipc_net_route_msg(abuf);
  357. }
  358. /* send rejected message */
  359. buf_discard(buf);
  360. tipc_net_route_msg(rbuf);
  361. return data_sz;
  362. }
  363. int tipc_port_reject_sections(struct tipc_port *p_ptr, struct tipc_msg *hdr,
  364. struct iovec const *msg_sect, u32 num_sect,
  365. int err)
  366. {
  367. struct sk_buff *buf;
  368. int res;
  369. res = tipc_msg_build(hdr, msg_sect, num_sect, MAX_MSG_SIZE,
  370. !p_ptr->user_port, &buf);
  371. if (!buf)
  372. return res;
  373. return tipc_reject_msg(buf, err);
  374. }
  375. static void port_timeout(unsigned long ref)
  376. {
  377. struct tipc_port *p_ptr = tipc_port_lock(ref);
  378. struct sk_buff *buf = NULL;
  379. if (!p_ptr)
  380. return;
  381. if (!p_ptr->connected) {
  382. tipc_port_unlock(p_ptr);
  383. return;
  384. }
  385. /* Last probe answered ? */
  386. if (p_ptr->probing_state == PROBING) {
  387. buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
  388. } else {
  389. buf = port_build_proto_msg(port_peerport(p_ptr),
  390. port_peernode(p_ptr),
  391. p_ptr->ref,
  392. tipc_own_addr,
  393. CONN_MANAGER,
  394. CONN_PROBE,
  395. TIPC_OK,
  396. 0);
  397. p_ptr->probing_state = PROBING;
  398. k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
  399. }
  400. tipc_port_unlock(p_ptr);
  401. tipc_net_route_msg(buf);
  402. }
  403. static void port_handle_node_down(unsigned long ref)
  404. {
  405. struct tipc_port *p_ptr = tipc_port_lock(ref);
  406. struct sk_buff *buf = NULL;
  407. if (!p_ptr)
  408. return;
  409. buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_NODE);
  410. tipc_port_unlock(p_ptr);
  411. tipc_net_route_msg(buf);
  412. }
  413. static struct sk_buff *port_build_self_abort_msg(struct tipc_port *p_ptr, u32 err)
  414. {
  415. u32 imp = msg_importance(&p_ptr->phdr);
  416. if (!p_ptr->connected)
  417. return NULL;
  418. if (imp < TIPC_CRITICAL_IMPORTANCE)
  419. imp++;
  420. return port_build_proto_msg(p_ptr->ref,
  421. tipc_own_addr,
  422. port_peerport(p_ptr),
  423. port_peernode(p_ptr),
  424. imp,
  425. TIPC_CONN_MSG,
  426. err,
  427. 0);
  428. }
  429. static struct sk_buff *port_build_peer_abort_msg(struct tipc_port *p_ptr, u32 err)
  430. {
  431. u32 imp = msg_importance(&p_ptr->phdr);
  432. if (!p_ptr->connected)
  433. return NULL;
  434. if (imp < TIPC_CRITICAL_IMPORTANCE)
  435. imp++;
  436. return port_build_proto_msg(port_peerport(p_ptr),
  437. port_peernode(p_ptr),
  438. p_ptr->ref,
  439. tipc_own_addr,
  440. imp,
  441. TIPC_CONN_MSG,
  442. err,
  443. 0);
  444. }
  445. void tipc_port_recv_proto_msg(struct sk_buff *buf)
  446. {
  447. struct tipc_msg *msg = buf_msg(buf);
  448. struct tipc_port *p_ptr = tipc_port_lock(msg_destport(msg));
  449. u32 err = TIPC_OK;
  450. struct sk_buff *r_buf = NULL;
  451. struct sk_buff *abort_buf = NULL;
  452. if (!p_ptr) {
  453. err = TIPC_ERR_NO_PORT;
  454. } else if (p_ptr->connected) {
  455. if ((port_peernode(p_ptr) != msg_orignode(msg)) ||
  456. (port_peerport(p_ptr) != msg_origport(msg))) {
  457. err = TIPC_ERR_NO_PORT;
  458. } else if (msg_type(msg) == CONN_ACK) {
  459. int wakeup = tipc_port_congested(p_ptr) &&
  460. p_ptr->congested &&
  461. p_ptr->wakeup;
  462. p_ptr->acked += msg_msgcnt(msg);
  463. if (tipc_port_congested(p_ptr))
  464. goto exit;
  465. p_ptr->congested = 0;
  466. if (!wakeup)
  467. goto exit;
  468. p_ptr->wakeup(p_ptr);
  469. goto exit;
  470. }
  471. } else if (p_ptr->published) {
  472. err = TIPC_ERR_NO_PORT;
  473. }
  474. if (err) {
  475. r_buf = port_build_proto_msg(msg_origport(msg),
  476. msg_orignode(msg),
  477. msg_destport(msg),
  478. tipc_own_addr,
  479. TIPC_HIGH_IMPORTANCE,
  480. TIPC_CONN_MSG,
  481. err,
  482. 0);
  483. goto exit;
  484. }
  485. /* All is fine */
  486. if (msg_type(msg) == CONN_PROBE) {
  487. r_buf = port_build_proto_msg(msg_origport(msg),
  488. msg_orignode(msg),
  489. msg_destport(msg),
  490. tipc_own_addr,
  491. CONN_MANAGER,
  492. CONN_PROBE_REPLY,
  493. TIPC_OK,
  494. 0);
  495. }
  496. p_ptr->probing_state = CONFIRMED;
  497. exit:
  498. if (p_ptr)
  499. tipc_port_unlock(p_ptr);
  500. tipc_net_route_msg(r_buf);
  501. tipc_net_route_msg(abort_buf);
  502. buf_discard(buf);
  503. }
  504. static void port_print(struct tipc_port *p_ptr, struct print_buf *buf, int full_id)
  505. {
  506. struct publication *publ;
  507. if (full_id)
  508. tipc_printf(buf, "<%u.%u.%u:%u>:",
  509. tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
  510. tipc_node(tipc_own_addr), p_ptr->ref);
  511. else
  512. tipc_printf(buf, "%-10u:", p_ptr->ref);
  513. if (p_ptr->connected) {
  514. u32 dport = port_peerport(p_ptr);
  515. u32 destnode = port_peernode(p_ptr);
  516. tipc_printf(buf, " connected to <%u.%u.%u:%u>",
  517. tipc_zone(destnode), tipc_cluster(destnode),
  518. tipc_node(destnode), dport);
  519. if (p_ptr->conn_type != 0)
  520. tipc_printf(buf, " via {%u,%u}",
  521. p_ptr->conn_type,
  522. p_ptr->conn_instance);
  523. } else if (p_ptr->published) {
  524. tipc_printf(buf, " bound to");
  525. list_for_each_entry(publ, &p_ptr->publications, pport_list) {
  526. if (publ->lower == publ->upper)
  527. tipc_printf(buf, " {%u,%u}", publ->type,
  528. publ->lower);
  529. else
  530. tipc_printf(buf, " {%u,%u,%u}", publ->type,
  531. publ->lower, publ->upper);
  532. }
  533. }
  534. tipc_printf(buf, "\n");
  535. }
  536. #define MAX_PORT_QUERY 32768
  537. struct sk_buff *tipc_port_get_ports(void)
  538. {
  539. struct sk_buff *buf;
  540. struct tlv_desc *rep_tlv;
  541. struct print_buf pb;
  542. struct tipc_port *p_ptr;
  543. int str_len;
  544. buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_PORT_QUERY));
  545. if (!buf)
  546. return NULL;
  547. rep_tlv = (struct tlv_desc *)buf->data;
  548. tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), MAX_PORT_QUERY);
  549. spin_lock_bh(&tipc_port_list_lock);
  550. list_for_each_entry(p_ptr, &ports, port_list) {
  551. spin_lock_bh(p_ptr->lock);
  552. port_print(p_ptr, &pb, 0);
  553. spin_unlock_bh(p_ptr->lock);
  554. }
  555. spin_unlock_bh(&tipc_port_list_lock);
  556. str_len = tipc_printbuf_validate(&pb);
  557. skb_put(buf, TLV_SPACE(str_len));
  558. TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
  559. return buf;
  560. }
  561. void tipc_port_reinit(void)
  562. {
  563. struct tipc_port *p_ptr;
  564. struct tipc_msg *msg;
  565. spin_lock_bh(&tipc_port_list_lock);
  566. list_for_each_entry(p_ptr, &ports, port_list) {
  567. msg = &p_ptr->phdr;
  568. if (msg_orignode(msg) == tipc_own_addr)
  569. break;
  570. msg_set_prevnode(msg, tipc_own_addr);
  571. msg_set_orignode(msg, tipc_own_addr);
  572. }
  573. spin_unlock_bh(&tipc_port_list_lock);
  574. }
  575. /*
  576. * port_dispatcher_sigh(): Signal handler for messages destinated
  577. * to the tipc_port interface.
  578. */
  579. static void port_dispatcher_sigh(void *dummy)
  580. {
  581. struct sk_buff *buf;
  582. spin_lock_bh(&queue_lock);
  583. buf = msg_queue_head;
  584. msg_queue_head = NULL;
  585. spin_unlock_bh(&queue_lock);
  586. while (buf) {
  587. struct tipc_port *p_ptr;
  588. struct user_port *up_ptr;
  589. struct tipc_portid orig;
  590. struct tipc_name_seq dseq;
  591. void *usr_handle;
  592. int connected;
  593. int published;
  594. u32 message_type;
  595. struct sk_buff *next = buf->next;
  596. struct tipc_msg *msg = buf_msg(buf);
  597. u32 dref = msg_destport(msg);
  598. message_type = msg_type(msg);
  599. if (message_type > TIPC_DIRECT_MSG)
  600. goto reject; /* Unsupported message type */
  601. p_ptr = tipc_port_lock(dref);
  602. if (!p_ptr)
  603. goto reject; /* Port deleted while msg in queue */
  604. orig.ref = msg_origport(msg);
  605. orig.node = msg_orignode(msg);
  606. up_ptr = p_ptr->user_port;
  607. usr_handle = up_ptr->usr_handle;
  608. connected = p_ptr->connected;
  609. published = p_ptr->published;
  610. if (unlikely(msg_errcode(msg)))
  611. goto err;
  612. switch (message_type) {
  613. case TIPC_CONN_MSG:{
  614. tipc_conn_msg_event cb = up_ptr->conn_msg_cb;
  615. u32 peer_port = port_peerport(p_ptr);
  616. u32 peer_node = port_peernode(p_ptr);
  617. u32 dsz;
  618. tipc_port_unlock(p_ptr);
  619. if (unlikely(!cb))
  620. goto reject;
  621. if (unlikely(!connected)) {
  622. if (tipc_connect2port(dref, &orig))
  623. goto reject;
  624. } else if ((msg_origport(msg) != peer_port) ||
  625. (msg_orignode(msg) != peer_node))
  626. goto reject;
  627. dsz = msg_data_sz(msg);
  628. if (unlikely(dsz &&
  629. (++p_ptr->conn_unacked >=
  630. TIPC_FLOW_CONTROL_WIN)))
  631. tipc_acknowledge(dref,
  632. p_ptr->conn_unacked);
  633. skb_pull(buf, msg_hdr_sz(msg));
  634. cb(usr_handle, dref, &buf, msg_data(msg), dsz);
  635. break;
  636. }
  637. case TIPC_DIRECT_MSG:{
  638. tipc_msg_event cb = up_ptr->msg_cb;
  639. tipc_port_unlock(p_ptr);
  640. if (unlikely(!cb || connected))
  641. goto reject;
  642. skb_pull(buf, msg_hdr_sz(msg));
  643. cb(usr_handle, dref, &buf, msg_data(msg),
  644. msg_data_sz(msg), msg_importance(msg),
  645. &orig);
  646. break;
  647. }
  648. case TIPC_MCAST_MSG:
  649. case TIPC_NAMED_MSG:{
  650. tipc_named_msg_event cb = up_ptr->named_msg_cb;
  651. tipc_port_unlock(p_ptr);
  652. if (unlikely(!cb || connected || !published))
  653. goto reject;
  654. dseq.type = msg_nametype(msg);
  655. dseq.lower = msg_nameinst(msg);
  656. dseq.upper = (message_type == TIPC_NAMED_MSG)
  657. ? dseq.lower : msg_nameupper(msg);
  658. skb_pull(buf, msg_hdr_sz(msg));
  659. cb(usr_handle, dref, &buf, msg_data(msg),
  660. msg_data_sz(msg), msg_importance(msg),
  661. &orig, &dseq);
  662. break;
  663. }
  664. }
  665. if (buf)
  666. buf_discard(buf);
  667. buf = next;
  668. continue;
  669. err:
  670. switch (message_type) {
  671. case TIPC_CONN_MSG:{
  672. tipc_conn_shutdown_event cb =
  673. up_ptr->conn_err_cb;
  674. u32 peer_port = port_peerport(p_ptr);
  675. u32 peer_node = port_peernode(p_ptr);
  676. tipc_port_unlock(p_ptr);
  677. if (!cb || !connected)
  678. break;
  679. if ((msg_origport(msg) != peer_port) ||
  680. (msg_orignode(msg) != peer_node))
  681. break;
  682. tipc_disconnect(dref);
  683. skb_pull(buf, msg_hdr_sz(msg));
  684. cb(usr_handle, dref, &buf, msg_data(msg),
  685. msg_data_sz(msg), msg_errcode(msg));
  686. break;
  687. }
  688. case TIPC_DIRECT_MSG:{
  689. tipc_msg_err_event cb = up_ptr->err_cb;
  690. tipc_port_unlock(p_ptr);
  691. if (!cb || connected)
  692. break;
  693. skb_pull(buf, msg_hdr_sz(msg));
  694. cb(usr_handle, dref, &buf, msg_data(msg),
  695. msg_data_sz(msg), msg_errcode(msg), &orig);
  696. break;
  697. }
  698. case TIPC_MCAST_MSG:
  699. case TIPC_NAMED_MSG:{
  700. tipc_named_msg_err_event cb =
  701. up_ptr->named_err_cb;
  702. tipc_port_unlock(p_ptr);
  703. if (!cb || connected)
  704. break;
  705. dseq.type = msg_nametype(msg);
  706. dseq.lower = msg_nameinst(msg);
  707. dseq.upper = (message_type == TIPC_NAMED_MSG)
  708. ? dseq.lower : msg_nameupper(msg);
  709. skb_pull(buf, msg_hdr_sz(msg));
  710. cb(usr_handle, dref, &buf, msg_data(msg),
  711. msg_data_sz(msg), msg_errcode(msg), &dseq);
  712. break;
  713. }
  714. }
  715. if (buf)
  716. buf_discard(buf);
  717. buf = next;
  718. continue;
  719. reject:
  720. tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
  721. buf = next;
  722. }
  723. }
  724. /*
  725. * port_dispatcher(): Dispatcher for messages destinated
  726. * to the tipc_port interface. Called with port locked.
  727. */
  728. static u32 port_dispatcher(struct tipc_port *dummy, struct sk_buff *buf)
  729. {
  730. buf->next = NULL;
  731. spin_lock_bh(&queue_lock);
  732. if (msg_queue_head) {
  733. msg_queue_tail->next = buf;
  734. msg_queue_tail = buf;
  735. } else {
  736. msg_queue_tail = msg_queue_head = buf;
  737. tipc_k_signal((Handler)port_dispatcher_sigh, 0);
  738. }
  739. spin_unlock_bh(&queue_lock);
  740. return 0;
  741. }
  742. /*
  743. * Wake up port after congestion: Called with port locked,
  744. *
  745. */
  746. static void port_wakeup_sh(unsigned long ref)
  747. {
  748. struct tipc_port *p_ptr;
  749. struct user_port *up_ptr;
  750. tipc_continue_event cb = NULL;
  751. void *uh = NULL;
  752. p_ptr = tipc_port_lock(ref);
  753. if (p_ptr) {
  754. up_ptr = p_ptr->user_port;
  755. if (up_ptr) {
  756. cb = up_ptr->continue_event_cb;
  757. uh = up_ptr->usr_handle;
  758. }
  759. tipc_port_unlock(p_ptr);
  760. }
  761. if (cb)
  762. cb(uh, ref);
  763. }
  764. static void port_wakeup(struct tipc_port *p_ptr)
  765. {
  766. tipc_k_signal((Handler)port_wakeup_sh, p_ptr->ref);
  767. }
  768. void tipc_acknowledge(u32 ref, u32 ack)
  769. {
  770. struct tipc_port *p_ptr;
  771. struct sk_buff *buf = NULL;
  772. p_ptr = tipc_port_lock(ref);
  773. if (!p_ptr)
  774. return;
  775. if (p_ptr->connected) {
  776. p_ptr->conn_unacked -= ack;
  777. buf = port_build_proto_msg(port_peerport(p_ptr),
  778. port_peernode(p_ptr),
  779. ref,
  780. tipc_own_addr,
  781. CONN_MANAGER,
  782. CONN_ACK,
  783. TIPC_OK,
  784. ack);
  785. }
  786. tipc_port_unlock(p_ptr);
  787. tipc_net_route_msg(buf);
  788. }
  789. /*
  790. * tipc_createport(): user level call.
  791. */
  792. int tipc_createport(void *usr_handle,
  793. unsigned int importance,
  794. tipc_msg_err_event error_cb,
  795. tipc_named_msg_err_event named_error_cb,
  796. tipc_conn_shutdown_event conn_error_cb,
  797. tipc_msg_event msg_cb,
  798. tipc_named_msg_event named_msg_cb,
  799. tipc_conn_msg_event conn_msg_cb,
  800. tipc_continue_event continue_event_cb,/* May be zero */
  801. u32 *portref)
  802. {
  803. struct user_port *up_ptr;
  804. struct tipc_port *p_ptr;
  805. up_ptr = kmalloc(sizeof(*up_ptr), GFP_ATOMIC);
  806. if (!up_ptr) {
  807. warn("Port creation failed, no memory\n");
  808. return -ENOMEM;
  809. }
  810. p_ptr = (struct tipc_port *)tipc_createport_raw(NULL, port_dispatcher,
  811. port_wakeup, importance);
  812. if (!p_ptr) {
  813. kfree(up_ptr);
  814. return -ENOMEM;
  815. }
  816. p_ptr->user_port = up_ptr;
  817. up_ptr->usr_handle = usr_handle;
  818. up_ptr->ref = p_ptr->ref;
  819. up_ptr->err_cb = error_cb;
  820. up_ptr->named_err_cb = named_error_cb;
  821. up_ptr->conn_err_cb = conn_error_cb;
  822. up_ptr->msg_cb = msg_cb;
  823. up_ptr->named_msg_cb = named_msg_cb;
  824. up_ptr->conn_msg_cb = conn_msg_cb;
  825. up_ptr->continue_event_cb = continue_event_cb;
  826. *portref = p_ptr->ref;
  827. tipc_port_unlock(p_ptr);
  828. return 0;
  829. }
  830. int tipc_portimportance(u32 ref, unsigned int *importance)
  831. {
  832. struct tipc_port *p_ptr;
  833. p_ptr = tipc_port_lock(ref);
  834. if (!p_ptr)
  835. return -EINVAL;
  836. *importance = (unsigned int)msg_importance(&p_ptr->phdr);
  837. tipc_port_unlock(p_ptr);
  838. return 0;
  839. }
  840. int tipc_set_portimportance(u32 ref, unsigned int imp)
  841. {
  842. struct tipc_port *p_ptr;
  843. if (imp > TIPC_CRITICAL_IMPORTANCE)
  844. return -EINVAL;
  845. p_ptr = tipc_port_lock(ref);
  846. if (!p_ptr)
  847. return -EINVAL;
  848. msg_set_importance(&p_ptr->phdr, (u32)imp);
  849. tipc_port_unlock(p_ptr);
  850. return 0;
  851. }
  852. int tipc_publish(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
  853. {
  854. struct tipc_port *p_ptr;
  855. struct publication *publ;
  856. u32 key;
  857. int res = -EINVAL;
  858. p_ptr = tipc_port_lock(ref);
  859. if (!p_ptr)
  860. return -EINVAL;
  861. if (p_ptr->connected)
  862. goto exit;
  863. if (seq->lower > seq->upper)
  864. goto exit;
  865. if ((scope < TIPC_ZONE_SCOPE) || (scope > TIPC_NODE_SCOPE))
  866. goto exit;
  867. key = ref + p_ptr->pub_count + 1;
  868. if (key == ref) {
  869. res = -EADDRINUSE;
  870. goto exit;
  871. }
  872. publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
  873. scope, p_ptr->ref, key);
  874. if (publ) {
  875. list_add(&publ->pport_list, &p_ptr->publications);
  876. p_ptr->pub_count++;
  877. p_ptr->published = 1;
  878. res = 0;
  879. }
  880. exit:
  881. tipc_port_unlock(p_ptr);
  882. return res;
  883. }
  884. int tipc_withdraw(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
  885. {
  886. struct tipc_port *p_ptr;
  887. struct publication *publ;
  888. struct publication *tpubl;
  889. int res = -EINVAL;
  890. p_ptr = tipc_port_lock(ref);
  891. if (!p_ptr)
  892. return -EINVAL;
  893. if (!seq) {
  894. list_for_each_entry_safe(publ, tpubl,
  895. &p_ptr->publications, pport_list) {
  896. tipc_nametbl_withdraw(publ->type, publ->lower,
  897. publ->ref, publ->key);
  898. }
  899. res = 0;
  900. } else {
  901. list_for_each_entry_safe(publ, tpubl,
  902. &p_ptr->publications, pport_list) {
  903. if (publ->scope != scope)
  904. continue;
  905. if (publ->type != seq->type)
  906. continue;
  907. if (publ->lower != seq->lower)
  908. continue;
  909. if (publ->upper != seq->upper)
  910. break;
  911. tipc_nametbl_withdraw(publ->type, publ->lower,
  912. publ->ref, publ->key);
  913. res = 0;
  914. break;
  915. }
  916. }
  917. if (list_empty(&p_ptr->publications))
  918. p_ptr->published = 0;
  919. tipc_port_unlock(p_ptr);
  920. return res;
  921. }
  922. int tipc_connect2port(u32 ref, struct tipc_portid const *peer)
  923. {
  924. struct tipc_port *p_ptr;
  925. struct tipc_msg *msg;
  926. int res = -EINVAL;
  927. p_ptr = tipc_port_lock(ref);
  928. if (!p_ptr)
  929. return -EINVAL;
  930. if (p_ptr->published || p_ptr->connected)
  931. goto exit;
  932. if (!peer->ref)
  933. goto exit;
  934. msg = &p_ptr->phdr;
  935. msg_set_destnode(msg, peer->node);
  936. msg_set_destport(msg, peer->ref);
  937. msg_set_orignode(msg, tipc_own_addr);
  938. msg_set_origport(msg, p_ptr->ref);
  939. msg_set_type(msg, TIPC_CONN_MSG);
  940. msg_set_lookup_scope(msg, 0);
  941. msg_set_hdr_sz(msg, SHORT_H_SIZE);
  942. p_ptr->probing_interval = PROBING_INTERVAL;
  943. p_ptr->probing_state = CONFIRMED;
  944. p_ptr->connected = 1;
  945. k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
  946. tipc_nodesub_subscribe(&p_ptr->subscription, peer->node,
  947. (void *)(unsigned long)ref,
  948. (net_ev_handler)port_handle_node_down);
  949. res = 0;
  950. exit:
  951. tipc_port_unlock(p_ptr);
  952. p_ptr->max_pkt = tipc_link_get_max_pkt(peer->node, ref);
  953. return res;
  954. }
  955. /**
  956. * tipc_disconnect_port - disconnect port from peer
  957. *
  958. * Port must be locked.
  959. */
  960. int tipc_disconnect_port(struct tipc_port *tp_ptr)
  961. {
  962. int res;
  963. if (tp_ptr->connected) {
  964. tp_ptr->connected = 0;
  965. /* let timer expire on it's own to avoid deadlock! */
  966. tipc_nodesub_unsubscribe(
  967. &((struct tipc_port *)tp_ptr)->subscription);
  968. res = 0;
  969. } else {
  970. res = -ENOTCONN;
  971. }
  972. return res;
  973. }
  974. /*
  975. * tipc_disconnect(): Disconnect port form peer.
  976. * This is a node local operation.
  977. */
  978. int tipc_disconnect(u32 ref)
  979. {
  980. struct tipc_port *p_ptr;
  981. int res;
  982. p_ptr = tipc_port_lock(ref);
  983. if (!p_ptr)
  984. return -EINVAL;
  985. res = tipc_disconnect_port((struct tipc_port *)p_ptr);
  986. tipc_port_unlock(p_ptr);
  987. return res;
  988. }
  989. /*
  990. * tipc_shutdown(): Send a SHUTDOWN msg to peer and disconnect
  991. */
  992. int tipc_shutdown(u32 ref)
  993. {
  994. struct tipc_port *p_ptr;
  995. struct sk_buff *buf = NULL;
  996. p_ptr = tipc_port_lock(ref);
  997. if (!p_ptr)
  998. return -EINVAL;
  999. if (p_ptr->connected) {
  1000. u32 imp = msg_importance(&p_ptr->phdr);
  1001. if (imp < TIPC_CRITICAL_IMPORTANCE)
  1002. imp++;
  1003. buf = port_build_proto_msg(port_peerport(p_ptr),
  1004. port_peernode(p_ptr),
  1005. ref,
  1006. tipc_own_addr,
  1007. imp,
  1008. TIPC_CONN_MSG,
  1009. TIPC_CONN_SHUTDOWN,
  1010. 0);
  1011. }
  1012. tipc_port_unlock(p_ptr);
  1013. tipc_net_route_msg(buf);
  1014. return tipc_disconnect(ref);
  1015. }
  1016. /*
  1017. * tipc_port_recv_sections(): Concatenate and deliver sectioned
  1018. * message for this node.
  1019. */
  1020. static int tipc_port_recv_sections(struct tipc_port *sender, unsigned int num_sect,
  1021. struct iovec const *msg_sect)
  1022. {
  1023. struct sk_buff *buf;
  1024. int res;
  1025. res = tipc_msg_build(&sender->phdr, msg_sect, num_sect,
  1026. MAX_MSG_SIZE, !sender->user_port, &buf);
  1027. if (likely(buf))
  1028. tipc_port_recv_msg(buf);
  1029. return res;
  1030. }
  1031. /**
  1032. * tipc_send - send message sections on connection
  1033. */
  1034. int tipc_send(u32 ref, unsigned int num_sect, struct iovec const *msg_sect)
  1035. {
  1036. struct tipc_port *p_ptr;
  1037. u32 destnode;
  1038. int res;
  1039. p_ptr = tipc_port_deref(ref);
  1040. if (!p_ptr || !p_ptr->connected)
  1041. return -EINVAL;
  1042. p_ptr->congested = 1;
  1043. if (!tipc_port_congested(p_ptr)) {
  1044. destnode = port_peernode(p_ptr);
  1045. if (likely(destnode != tipc_own_addr))
  1046. res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
  1047. destnode);
  1048. else
  1049. res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
  1050. if (likely(res != -ELINKCONG)) {
  1051. p_ptr->congested = 0;
  1052. if (res > 0)
  1053. p_ptr->sent++;
  1054. return res;
  1055. }
  1056. }
  1057. if (port_unreliable(p_ptr)) {
  1058. p_ptr->congested = 0;
  1059. /* Just calculate msg length and return */
  1060. return tipc_msg_calc_data_size(msg_sect, num_sect);
  1061. }
  1062. return -ELINKCONG;
  1063. }
  1064. /**
  1065. * tipc_send2name - send message sections to port name
  1066. */
  1067. int tipc_send2name(u32 ref, struct tipc_name const *name, unsigned int domain,
  1068. unsigned int num_sect, struct iovec const *msg_sect)
  1069. {
  1070. struct tipc_port *p_ptr;
  1071. struct tipc_msg *msg;
  1072. u32 destnode = domain;
  1073. u32 destport;
  1074. int res;
  1075. p_ptr = tipc_port_deref(ref);
  1076. if (!p_ptr || p_ptr->connected)
  1077. return -EINVAL;
  1078. msg = &p_ptr->phdr;
  1079. msg_set_type(msg, TIPC_NAMED_MSG);
  1080. msg_set_orignode(msg, tipc_own_addr);
  1081. msg_set_origport(msg, ref);
  1082. msg_set_hdr_sz(msg, LONG_H_SIZE);
  1083. msg_set_nametype(msg, name->type);
  1084. msg_set_nameinst(msg, name->instance);
  1085. msg_set_lookup_scope(msg, tipc_addr_scope(domain));
  1086. destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
  1087. msg_set_destnode(msg, destnode);
  1088. msg_set_destport(msg, destport);
  1089. if (likely(destport)) {
  1090. if (likely(destnode == tipc_own_addr))
  1091. res = tipc_port_recv_sections(p_ptr, num_sect,
  1092. msg_sect);
  1093. else
  1094. res = tipc_link_send_sections_fast(p_ptr, msg_sect,
  1095. num_sect, destnode);
  1096. if (likely(res != -ELINKCONG)) {
  1097. if (res > 0)
  1098. p_ptr->sent++;
  1099. return res;
  1100. }
  1101. if (port_unreliable(p_ptr)) {
  1102. /* Just calculate msg length and return */
  1103. return tipc_msg_calc_data_size(msg_sect, num_sect);
  1104. }
  1105. return -ELINKCONG;
  1106. }
  1107. return tipc_port_reject_sections(p_ptr, msg, msg_sect, num_sect,
  1108. TIPC_ERR_NO_NAME);
  1109. }
  1110. /**
  1111. * tipc_send2port - send message sections to port identity
  1112. */
  1113. int tipc_send2port(u32 ref, struct tipc_portid const *dest,
  1114. unsigned int num_sect, struct iovec const *msg_sect)
  1115. {
  1116. struct tipc_port *p_ptr;
  1117. struct tipc_msg *msg;
  1118. int res;
  1119. p_ptr = tipc_port_deref(ref);
  1120. if (!p_ptr || p_ptr->connected)
  1121. return -EINVAL;
  1122. msg = &p_ptr->phdr;
  1123. msg_set_type(msg, TIPC_DIRECT_MSG);
  1124. msg_set_lookup_scope(msg, 0);
  1125. msg_set_orignode(msg, tipc_own_addr);
  1126. msg_set_origport(msg, ref);
  1127. msg_set_destnode(msg, dest->node);
  1128. msg_set_destport(msg, dest->ref);
  1129. msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
  1130. if (dest->node == tipc_own_addr)
  1131. res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
  1132. else
  1133. res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
  1134. dest->node);
  1135. if (likely(res != -ELINKCONG)) {
  1136. if (res > 0)
  1137. p_ptr->sent++;
  1138. return res;
  1139. }
  1140. if (port_unreliable(p_ptr)) {
  1141. /* Just calculate msg length and return */
  1142. return tipc_msg_calc_data_size(msg_sect, num_sect);
  1143. }
  1144. return -ELINKCONG;
  1145. }
  1146. /**
  1147. * tipc_send_buf2port - send message buffer to port identity
  1148. */
  1149. int tipc_send_buf2port(u32 ref, struct tipc_portid const *dest,
  1150. struct sk_buff *buf, unsigned int dsz)
  1151. {
  1152. struct tipc_port *p_ptr;
  1153. struct tipc_msg *msg;
  1154. int res;
  1155. p_ptr = (struct tipc_port *)tipc_ref_deref(ref);
  1156. if (!p_ptr || p_ptr->connected)
  1157. return -EINVAL;
  1158. msg = &p_ptr->phdr;
  1159. msg_set_type(msg, TIPC_DIRECT_MSG);
  1160. msg_set_orignode(msg, tipc_own_addr);
  1161. msg_set_origport(msg, ref);
  1162. msg_set_destnode(msg, dest->node);
  1163. msg_set_destport(msg, dest->ref);
  1164. msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
  1165. msg_set_size(msg, DIR_MSG_H_SIZE + dsz);
  1166. if (skb_cow(buf, DIR_MSG_H_SIZE))
  1167. return -ENOMEM;
  1168. skb_push(buf, DIR_MSG_H_SIZE);
  1169. skb_copy_to_linear_data(buf, msg, DIR_MSG_H_SIZE);
  1170. if (dest->node == tipc_own_addr)
  1171. res = tipc_port_recv_msg(buf);
  1172. else
  1173. res = tipc_send_buf_fast(buf, dest->node);
  1174. if (likely(res != -ELINKCONG)) {
  1175. if (res > 0)
  1176. p_ptr->sent++;
  1177. return res;
  1178. }
  1179. if (port_unreliable(p_ptr))
  1180. return dsz;
  1181. return -ELINKCONG;
  1182. }