2 * net/tipc/port.c: TIPC port code
4 * Copyright (c) 1992-2007, Ericsson AB
5 * Copyright (c) 2004-2008, 2010-2013, Wind River Systems
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
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.
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.
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.
40 #include "name_table.h"
42 /* Connection management: */
43 #define PROBING_INTERVAL 3600000 /* [ms] => 1 h */
47 #define MAX_REJECT_SIZE 1024
49 DEFINE_SPINLOCK(tipc_port_list_lock);
51 static LIST_HEAD(ports);
52 static void port_handle_node_down(unsigned long ref);
53 static struct sk_buff *port_build_self_abort_msg(struct tipc_port *, u32 err);
54 static struct sk_buff *port_build_peer_abort_msg(struct tipc_port *, u32 err);
55 static void port_timeout(unsigned long ref);
58 static u32 port_peernode(struct tipc_port *p_ptr)
60 return msg_destnode(&p_ptr->phdr);
63 static u32 port_peerport(struct tipc_port *p_ptr)
65 return msg_destport(&p_ptr->phdr);
69 * tipc_port_peer_msg - verify message was sent by connected port's peer
71 * Handles cases where the node's network address has changed from
72 * the default of <0.0.0> to its configured setting.
74 int tipc_port_peer_msg(struct tipc_port *p_ptr, struct tipc_msg *msg)
79 if (msg_origport(msg) != port_peerport(p_ptr))
82 orignode = msg_orignode(msg);
83 peernode = port_peernode(p_ptr);
84 return (orignode == peernode) ||
85 (!orignode && (peernode == tipc_own_addr)) ||
86 (!peernode && (orignode == tipc_own_addr));
90 * tipc_multicast - send a multicast message to local and remote destinations
92 int tipc_multicast(u32 ref, struct tipc_name_seq const *seq,
93 u32 num_sect, struct iovec const *msg_sect,
94 unsigned int total_len)
98 struct sk_buff *ibuf = NULL;
99 struct tipc_port_list dports = {0, NULL, };
100 struct tipc_port *oport = tipc_port_deref(ref);
104 if (unlikely(!oport))
107 /* Create multicast message */
109 msg_set_type(hdr, TIPC_MCAST_MSG);
110 msg_set_lookup_scope(hdr, TIPC_CLUSTER_SCOPE);
111 msg_set_destport(hdr, 0);
112 msg_set_destnode(hdr, 0);
113 msg_set_nametype(hdr, seq->type);
114 msg_set_namelower(hdr, seq->lower);
115 msg_set_nameupper(hdr, seq->upper);
116 msg_set_hdr_sz(hdr, MCAST_H_SIZE);
117 res = tipc_msg_build(hdr, msg_sect, num_sect, total_len, MAX_MSG_SIZE,
122 /* Figure out where to send multicast message */
123 ext_targets = tipc_nametbl_mc_translate(seq->type, seq->lower, seq->upper,
124 TIPC_NODE_SCOPE, &dports);
126 /* Send message to destinations (duplicate it only if necessary) */
128 if (dports.count != 0) {
129 ibuf = skb_copy(buf, GFP_ATOMIC);
131 tipc_port_list_free(&dports);
136 res = tipc_bclink_send_msg(buf);
137 if ((res < 0) && (dports.count != 0))
145 tipc_port_recv_mcast(ibuf, &dports);
147 tipc_port_list_free(&dports);
153 * tipc_port_recv_mcast - deliver multicast message to all destination ports
155 * If there is no port list, perform a lookup to create one
157 void tipc_port_recv_mcast(struct sk_buff *buf, struct tipc_port_list *dp)
159 struct tipc_msg *msg;
160 struct tipc_port_list dports = {0, NULL, };
161 struct tipc_port_list *item = dp;
166 /* Create destination port list, if one wasn't supplied */
168 tipc_nametbl_mc_translate(msg_nametype(msg),
176 /* Deliver a copy of message to each destination port */
177 if (dp->count != 0) {
178 msg_set_destnode(msg, tipc_own_addr);
179 if (dp->count == 1) {
180 msg_set_destport(msg, dp->ports[0]);
181 tipc_port_recv_msg(buf);
182 tipc_port_list_free(dp);
185 for (; cnt < dp->count; cnt++) {
186 int index = cnt % PLSIZE;
187 struct sk_buff *b = skb_clone(buf, GFP_ATOMIC);
190 pr_warn("Unable to deliver multicast message(s)\n");
193 if ((index == 0) && (cnt != 0))
195 msg_set_destport(buf_msg(b), item->ports[index]);
196 tipc_port_recv_msg(b);
201 tipc_port_list_free(dp);
205 * tipc_createport - create a generic TIPC port
207 * Returns pointer to (locked) TIPC port, or NULL if unable to create it
209 struct tipc_port *tipc_createport(struct sock *sk,
210 u32 (*dispatcher)(struct tipc_port *,
212 void (*wakeup)(struct tipc_port *),
213 const u32 importance)
215 struct tipc_port *p_ptr;
216 struct tipc_msg *msg;
219 p_ptr = kzalloc(sizeof(*p_ptr), GFP_ATOMIC);
221 pr_warn("Port creation failed, no memory\n");
224 ref = tipc_ref_acquire(p_ptr, &p_ptr->lock);
226 pr_warn("Port creation failed, ref. table exhausted\n");
232 p_ptr->max_pkt = MAX_PKT_DEFAULT;
234 INIT_LIST_HEAD(&p_ptr->wait_list);
235 INIT_LIST_HEAD(&p_ptr->subscription.nodesub_list);
236 p_ptr->dispatcher = dispatcher;
237 p_ptr->wakeup = wakeup;
238 k_init_timer(&p_ptr->timer, (Handler)port_timeout, ref);
239 INIT_LIST_HEAD(&p_ptr->publications);
240 INIT_LIST_HEAD(&p_ptr->port_list);
243 * Must hold port list lock while initializing message header template
244 * to ensure a change to node's own network address doesn't result
245 * in template containing out-dated network address information
247 spin_lock_bh(&tipc_port_list_lock);
249 tipc_msg_init(msg, importance, TIPC_NAMED_MSG, NAMED_H_SIZE, 0);
250 msg_set_origport(msg, ref);
251 list_add_tail(&p_ptr->port_list, &ports);
252 spin_unlock_bh(&tipc_port_list_lock);
256 int tipc_deleteport(u32 ref)
258 struct tipc_port *p_ptr;
259 struct sk_buff *buf = NULL;
261 tipc_withdraw(ref, 0, NULL);
262 p_ptr = tipc_port_lock(ref);
266 tipc_ref_discard(ref);
267 tipc_port_unlock(p_ptr);
269 k_cancel_timer(&p_ptr->timer);
270 if (p_ptr->connected) {
271 buf = port_build_peer_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
272 tipc_nodesub_unsubscribe(&p_ptr->subscription);
275 spin_lock_bh(&tipc_port_list_lock);
276 list_del(&p_ptr->port_list);
277 list_del(&p_ptr->wait_list);
278 spin_unlock_bh(&tipc_port_list_lock);
279 k_term_timer(&p_ptr->timer);
281 tipc_net_route_msg(buf);
285 static int port_unreliable(struct tipc_port *p_ptr)
287 return msg_src_droppable(&p_ptr->phdr);
290 int tipc_portunreliable(u32 ref, unsigned int *isunreliable)
292 struct tipc_port *p_ptr;
294 p_ptr = tipc_port_lock(ref);
297 *isunreliable = port_unreliable(p_ptr);
298 tipc_port_unlock(p_ptr);
302 int tipc_set_portunreliable(u32 ref, unsigned int isunreliable)
304 struct tipc_port *p_ptr;
306 p_ptr = tipc_port_lock(ref);
309 msg_set_src_droppable(&p_ptr->phdr, (isunreliable != 0));
310 tipc_port_unlock(p_ptr);
314 static int port_unreturnable(struct tipc_port *p_ptr)
316 return msg_dest_droppable(&p_ptr->phdr);
319 int tipc_portunreturnable(u32 ref, unsigned int *isunrejectable)
321 struct tipc_port *p_ptr;
323 p_ptr = tipc_port_lock(ref);
326 *isunrejectable = port_unreturnable(p_ptr);
327 tipc_port_unlock(p_ptr);
331 int tipc_set_portunreturnable(u32 ref, unsigned int isunrejectable)
333 struct tipc_port *p_ptr;
335 p_ptr = tipc_port_lock(ref);
338 msg_set_dest_droppable(&p_ptr->phdr, (isunrejectable != 0));
339 tipc_port_unlock(p_ptr);
344 * port_build_proto_msg(): create connection protocol message for port
346 * On entry the port must be locked and connected.
348 static struct sk_buff *port_build_proto_msg(struct tipc_port *p_ptr,
352 struct tipc_msg *msg;
354 buf = tipc_buf_acquire(INT_H_SIZE);
357 tipc_msg_init(msg, CONN_MANAGER, type, INT_H_SIZE,
358 port_peernode(p_ptr));
359 msg_set_destport(msg, port_peerport(p_ptr));
360 msg_set_origport(msg, p_ptr->ref);
361 msg_set_msgcnt(msg, ack);
366 int tipc_reject_msg(struct sk_buff *buf, u32 err)
368 struct tipc_msg *msg = buf_msg(buf);
369 struct sk_buff *rbuf;
370 struct tipc_msg *rmsg;
373 u32 data_sz = msg_data_sz(msg);
377 /* discard rejected message if it shouldn't be returned to sender */
378 if (WARN(!msg_isdata(msg),
379 "attempt to reject message with user=%u", msg_user(msg))) {
383 if (msg_errcode(msg) || msg_dest_droppable(msg))
387 * construct returned message by copying rejected message header and
388 * data (or subset), then updating header fields that need adjusting
390 hdr_sz = msg_hdr_sz(msg);
391 rmsg_sz = hdr_sz + min_t(u32, data_sz, MAX_REJECT_SIZE);
393 rbuf = tipc_buf_acquire(rmsg_sz);
397 rmsg = buf_msg(rbuf);
398 skb_copy_to_linear_data(rbuf, msg, rmsg_sz);
400 if (msg_connected(rmsg)) {
401 imp = msg_importance(rmsg);
402 if (imp < TIPC_CRITICAL_IMPORTANCE)
403 msg_set_importance(rmsg, ++imp);
405 msg_set_non_seq(rmsg, 0);
406 msg_set_size(rmsg, rmsg_sz);
407 msg_set_errcode(rmsg, err);
408 msg_set_prevnode(rmsg, tipc_own_addr);
409 msg_swap_words(rmsg, 4, 5);
410 if (!msg_short(rmsg))
411 msg_swap_words(rmsg, 6, 7);
413 /* send self-abort message when rejecting on a connected port */
414 if (msg_connected(msg)) {
415 struct tipc_port *p_ptr = tipc_port_lock(msg_destport(msg));
418 struct sk_buff *abuf = NULL;
420 if (p_ptr->connected)
421 abuf = port_build_self_abort_msg(p_ptr, err);
422 tipc_port_unlock(p_ptr);
423 tipc_net_route_msg(abuf);
427 /* send returned message & dispose of rejected message */
428 src_node = msg_prevnode(msg);
429 if (in_own_node(src_node))
430 tipc_port_recv_msg(rbuf);
432 tipc_link_send(rbuf, src_node, msg_link_selector(rmsg));
438 int tipc_port_reject_sections(struct tipc_port *p_ptr, struct tipc_msg *hdr,
439 struct iovec const *msg_sect, u32 num_sect,
440 unsigned int total_len, int err)
445 res = tipc_msg_build(hdr, msg_sect, num_sect, total_len, MAX_MSG_SIZE,
450 return tipc_reject_msg(buf, err);
453 static void port_timeout(unsigned long ref)
455 struct tipc_port *p_ptr = tipc_port_lock(ref);
456 struct sk_buff *buf = NULL;
461 if (!p_ptr->connected) {
462 tipc_port_unlock(p_ptr);
466 /* Last probe answered ? */
467 if (p_ptr->probing_state == PROBING) {
468 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
470 buf = port_build_proto_msg(p_ptr, CONN_PROBE, 0);
471 p_ptr->probing_state = PROBING;
472 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
474 tipc_port_unlock(p_ptr);
475 tipc_net_route_msg(buf);
479 static void port_handle_node_down(unsigned long ref)
481 struct tipc_port *p_ptr = tipc_port_lock(ref);
482 struct sk_buff *buf = NULL;
486 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_NODE);
487 tipc_port_unlock(p_ptr);
488 tipc_net_route_msg(buf);
492 static struct sk_buff *port_build_self_abort_msg(struct tipc_port *p_ptr, u32 err)
494 struct sk_buff *buf = port_build_peer_abort_msg(p_ptr, err);
497 struct tipc_msg *msg = buf_msg(buf);
498 msg_swap_words(msg, 4, 5);
499 msg_swap_words(msg, 6, 7);
505 static struct sk_buff *port_build_peer_abort_msg(struct tipc_port *p_ptr, u32 err)
508 struct tipc_msg *msg;
511 if (!p_ptr->connected)
514 buf = tipc_buf_acquire(BASIC_H_SIZE);
517 memcpy(msg, &p_ptr->phdr, BASIC_H_SIZE);
518 msg_set_hdr_sz(msg, BASIC_H_SIZE);
519 msg_set_size(msg, BASIC_H_SIZE);
520 imp = msg_importance(msg);
521 if (imp < TIPC_CRITICAL_IMPORTANCE)
522 msg_set_importance(msg, ++imp);
523 msg_set_errcode(msg, err);
528 void tipc_port_recv_proto_msg(struct sk_buff *buf)
530 struct tipc_msg *msg = buf_msg(buf);
531 struct tipc_port *p_ptr;
532 struct sk_buff *r_buf = NULL;
533 u32 destport = msg_destport(msg);
536 /* Validate connection */
537 p_ptr = tipc_port_lock(destport);
538 if (!p_ptr || !p_ptr->connected || !tipc_port_peer_msg(p_ptr, msg)) {
539 r_buf = tipc_buf_acquire(BASIC_H_SIZE);
541 msg = buf_msg(r_buf);
542 tipc_msg_init(msg, TIPC_HIGH_IMPORTANCE, TIPC_CONN_MSG,
543 BASIC_H_SIZE, msg_orignode(msg));
544 msg_set_errcode(msg, TIPC_ERR_NO_PORT);
545 msg_set_origport(msg, destport);
546 msg_set_destport(msg, msg_origport(msg));
549 tipc_port_unlock(p_ptr);
553 /* Process protocol message sent by peer */
554 switch (msg_type(msg)) {
556 wakeable = tipc_port_congested(p_ptr) && p_ptr->congested &&
558 p_ptr->acked += msg_msgcnt(msg);
559 if (!tipc_port_congested(p_ptr)) {
560 p_ptr->congested = 0;
562 p_ptr->wakeup(p_ptr);
566 r_buf = port_build_proto_msg(p_ptr, CONN_PROBE_REPLY, 0);
569 /* CONN_PROBE_REPLY or unrecognized - no action required */
572 p_ptr->probing_state = CONFIRMED;
573 tipc_port_unlock(p_ptr);
575 tipc_net_route_msg(r_buf);
579 static int port_print(struct tipc_port *p_ptr, char *buf, int len, int full_id)
581 struct publication *publ;
585 ret = tipc_snprintf(buf, len, "<%u.%u.%u:%u>:",
586 tipc_zone(tipc_own_addr),
587 tipc_cluster(tipc_own_addr),
588 tipc_node(tipc_own_addr), p_ptr->ref);
590 ret = tipc_snprintf(buf, len, "%-10u:", p_ptr->ref);
592 if (p_ptr->connected) {
593 u32 dport = port_peerport(p_ptr);
594 u32 destnode = port_peernode(p_ptr);
596 ret += tipc_snprintf(buf + ret, len - ret,
597 " connected to <%u.%u.%u:%u>",
599 tipc_cluster(destnode),
600 tipc_node(destnode), dport);
601 if (p_ptr->conn_type != 0)
602 ret += tipc_snprintf(buf + ret, len - ret,
603 " via {%u,%u}", p_ptr->conn_type,
604 p_ptr->conn_instance);
605 } else if (p_ptr->published) {
606 ret += tipc_snprintf(buf + ret, len - ret, " bound to");
607 list_for_each_entry(publ, &p_ptr->publications, pport_list) {
608 if (publ->lower == publ->upper)
609 ret += tipc_snprintf(buf + ret, len - ret,
610 " {%u,%u}", publ->type,
613 ret += tipc_snprintf(buf + ret, len - ret,
614 " {%u,%u,%u}", publ->type,
615 publ->lower, publ->upper);
618 ret += tipc_snprintf(buf + ret, len - ret, "\n");
622 struct sk_buff *tipc_port_get_ports(void)
625 struct tlv_desc *rep_tlv;
628 struct tipc_port *p_ptr;
631 buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
634 rep_tlv = (struct tlv_desc *)buf->data;
635 pb = TLV_DATA(rep_tlv);
636 pb_len = ULTRA_STRING_MAX_LEN;
638 spin_lock_bh(&tipc_port_list_lock);
639 list_for_each_entry(p_ptr, &ports, port_list) {
640 spin_lock_bh(p_ptr->lock);
641 str_len += port_print(p_ptr, pb, pb_len, 0);
642 spin_unlock_bh(p_ptr->lock);
644 spin_unlock_bh(&tipc_port_list_lock);
645 str_len += 1; /* for "\0" */
646 skb_put(buf, TLV_SPACE(str_len));
647 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
652 void tipc_port_reinit(void)
654 struct tipc_port *p_ptr;
655 struct tipc_msg *msg;
657 spin_lock_bh(&tipc_port_list_lock);
658 list_for_each_entry(p_ptr, &ports, port_list) {
660 msg_set_prevnode(msg, tipc_own_addr);
661 msg_set_orignode(msg, tipc_own_addr);
663 spin_unlock_bh(&tipc_port_list_lock);
666 void tipc_acknowledge(u32 ref, u32 ack)
668 struct tipc_port *p_ptr;
669 struct sk_buff *buf = NULL;
671 p_ptr = tipc_port_lock(ref);
674 if (p_ptr->connected) {
675 p_ptr->conn_unacked -= ack;
676 buf = port_build_proto_msg(p_ptr, CONN_ACK, ack);
678 tipc_port_unlock(p_ptr);
679 tipc_net_route_msg(buf);
682 int tipc_portimportance(u32 ref, unsigned int *importance)
684 struct tipc_port *p_ptr;
686 p_ptr = tipc_port_lock(ref);
689 *importance = (unsigned int)msg_importance(&p_ptr->phdr);
690 tipc_port_unlock(p_ptr);
694 int tipc_set_portimportance(u32 ref, unsigned int imp)
696 struct tipc_port *p_ptr;
698 if (imp > TIPC_CRITICAL_IMPORTANCE)
701 p_ptr = tipc_port_lock(ref);
704 msg_set_importance(&p_ptr->phdr, (u32)imp);
705 tipc_port_unlock(p_ptr);
710 int tipc_publish(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
712 struct tipc_port *p_ptr;
713 struct publication *publ;
717 p_ptr = tipc_port_lock(ref);
721 if (p_ptr->connected)
723 key = ref + p_ptr->pub_count + 1;
728 publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
729 scope, p_ptr->ref, key);
731 list_add(&publ->pport_list, &p_ptr->publications);
733 p_ptr->published = 1;
737 tipc_port_unlock(p_ptr);
741 int tipc_withdraw(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
743 struct tipc_port *p_ptr;
744 struct publication *publ;
745 struct publication *tpubl;
748 p_ptr = tipc_port_lock(ref);
752 list_for_each_entry_safe(publ, tpubl,
753 &p_ptr->publications, pport_list) {
754 tipc_nametbl_withdraw(publ->type, publ->lower,
755 publ->ref, publ->key);
759 list_for_each_entry_safe(publ, tpubl,
760 &p_ptr->publications, pport_list) {
761 if (publ->scope != scope)
763 if (publ->type != seq->type)
765 if (publ->lower != seq->lower)
767 if (publ->upper != seq->upper)
769 tipc_nametbl_withdraw(publ->type, publ->lower,
770 publ->ref, publ->key);
775 if (list_empty(&p_ptr->publications))
776 p_ptr->published = 0;
777 tipc_port_unlock(p_ptr);
781 int tipc_connect(u32 ref, struct tipc_portid const *peer)
783 struct tipc_port *p_ptr;
786 p_ptr = tipc_port_lock(ref);
789 res = __tipc_connect(ref, p_ptr, peer);
790 tipc_port_unlock(p_ptr);
795 * __tipc_connect - connect to a remote peer
797 * Port must be locked.
799 int __tipc_connect(u32 ref, struct tipc_port *p_ptr,
800 struct tipc_portid const *peer)
802 struct tipc_msg *msg;
805 if (p_ptr->published || p_ptr->connected)
811 msg_set_destnode(msg, peer->node);
812 msg_set_destport(msg, peer->ref);
813 msg_set_type(msg, TIPC_CONN_MSG);
814 msg_set_lookup_scope(msg, 0);
815 msg_set_hdr_sz(msg, SHORT_H_SIZE);
817 p_ptr->probing_interval = PROBING_INTERVAL;
818 p_ptr->probing_state = CONFIRMED;
819 p_ptr->connected = 1;
820 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
822 tipc_nodesub_subscribe(&p_ptr->subscription, peer->node,
823 (void *)(unsigned long)ref,
824 (net_ev_handler)port_handle_node_down);
827 p_ptr->max_pkt = tipc_link_get_max_pkt(peer->node, ref);
832 * __tipc_disconnect - disconnect port from peer
834 * Port must be locked.
836 int __tipc_disconnect(struct tipc_port *tp_ptr)
840 if (tp_ptr->connected) {
841 tp_ptr->connected = 0;
842 /* let timer expire on it's own to avoid deadlock! */
843 tipc_nodesub_unsubscribe(&tp_ptr->subscription);
852 * tipc_disconnect(): Disconnect port form peer.
853 * This is a node local operation.
855 int tipc_disconnect(u32 ref)
857 struct tipc_port *p_ptr;
860 p_ptr = tipc_port_lock(ref);
863 res = __tipc_disconnect(p_ptr);
864 tipc_port_unlock(p_ptr);
869 * tipc_shutdown(): Send a SHUTDOWN msg to peer and disconnect
871 int tipc_shutdown(u32 ref)
873 struct tipc_port *p_ptr;
874 struct sk_buff *buf = NULL;
876 p_ptr = tipc_port_lock(ref);
880 buf = port_build_peer_abort_msg(p_ptr, TIPC_CONN_SHUTDOWN);
881 tipc_port_unlock(p_ptr);
882 tipc_net_route_msg(buf);
883 return tipc_disconnect(ref);
887 * tipc_port_recv_msg - receive message from lower layer and deliver to port user
889 int tipc_port_recv_msg(struct sk_buff *buf)
891 struct tipc_port *p_ptr;
892 struct tipc_msg *msg = buf_msg(buf);
893 u32 destport = msg_destport(msg);
894 u32 dsz = msg_data_sz(msg);
897 /* forward unresolved named message */
898 if (unlikely(!destport)) {
899 tipc_net_route_msg(buf);
903 /* validate destination & pass to port, otherwise reject message */
904 p_ptr = tipc_port_lock(destport);
906 err = p_ptr->dispatcher(p_ptr, buf);
907 tipc_port_unlock(p_ptr);
911 err = TIPC_ERR_NO_PORT;
914 return tipc_reject_msg(buf, err);
918 * tipc_port_recv_sections(): Concatenate and deliver sectioned
919 * message for this node.
921 static int tipc_port_recv_sections(struct tipc_port *sender, unsigned int num_sect,
922 struct iovec const *msg_sect,
923 unsigned int total_len)
928 res = tipc_msg_build(&sender->phdr, msg_sect, num_sect, total_len,
931 tipc_port_recv_msg(buf);
936 * tipc_send - send message sections on connection
938 int tipc_send(u32 ref, unsigned int num_sect, struct iovec const *msg_sect,
939 unsigned int total_len)
941 struct tipc_port *p_ptr;
945 p_ptr = tipc_port_deref(ref);
946 if (!p_ptr || !p_ptr->connected)
949 p_ptr->congested = 1;
950 if (!tipc_port_congested(p_ptr)) {
951 destnode = port_peernode(p_ptr);
952 if (likely(!in_own_node(destnode)))
953 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
954 total_len, destnode);
956 res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect,
959 if (likely(res != -ELINKCONG)) {
960 p_ptr->congested = 0;
966 if (port_unreliable(p_ptr)) {
967 p_ptr->congested = 0;
974 * tipc_send2name - send message sections to port name
976 int tipc_send2name(u32 ref, struct tipc_name const *name, unsigned int domain,
977 unsigned int num_sect, struct iovec const *msg_sect,
978 unsigned int total_len)
980 struct tipc_port *p_ptr;
981 struct tipc_msg *msg;
982 u32 destnode = domain;
986 p_ptr = tipc_port_deref(ref);
987 if (!p_ptr || p_ptr->connected)
991 msg_set_type(msg, TIPC_NAMED_MSG);
992 msg_set_hdr_sz(msg, NAMED_H_SIZE);
993 msg_set_nametype(msg, name->type);
994 msg_set_nameinst(msg, name->instance);
995 msg_set_lookup_scope(msg, tipc_addr_scope(domain));
996 destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
997 msg_set_destnode(msg, destnode);
998 msg_set_destport(msg, destport);
1000 if (likely(destport || destnode)) {
1001 if (likely(in_own_node(destnode)))
1002 res = tipc_port_recv_sections(p_ptr, num_sect,
1003 msg_sect, total_len);
1004 else if (tipc_own_addr)
1005 res = tipc_link_send_sections_fast(p_ptr, msg_sect,
1006 num_sect, total_len,
1009 res = tipc_port_reject_sections(p_ptr, msg, msg_sect,
1010 num_sect, total_len,
1012 if (likely(res != -ELINKCONG)) {
1017 if (port_unreliable(p_ptr)) {
1022 return tipc_port_reject_sections(p_ptr, msg, msg_sect, num_sect,
1023 total_len, TIPC_ERR_NO_NAME);
1027 * tipc_send2port - send message sections to port identity
1029 int tipc_send2port(u32 ref, struct tipc_portid const *dest,
1030 unsigned int num_sect, struct iovec const *msg_sect,
1031 unsigned int total_len)
1033 struct tipc_port *p_ptr;
1034 struct tipc_msg *msg;
1037 p_ptr = tipc_port_deref(ref);
1038 if (!p_ptr || p_ptr->connected)
1042 msg_set_type(msg, TIPC_DIRECT_MSG);
1043 msg_set_lookup_scope(msg, 0);
1044 msg_set_destnode(msg, dest->node);
1045 msg_set_destport(msg, dest->ref);
1046 msg_set_hdr_sz(msg, BASIC_H_SIZE);
1048 if (in_own_node(dest->node))
1049 res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect,
1051 else if (tipc_own_addr)
1052 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
1053 total_len, dest->node);
1055 res = tipc_port_reject_sections(p_ptr, msg, msg_sect, num_sect,
1056 total_len, TIPC_ERR_NO_NODE);
1057 if (likely(res != -ELINKCONG)) {
1062 if (port_unreliable(p_ptr)) {