2 * net/tipc/port.c: TIPC port code
4 * Copyright (c) 1992-2007, Ericsson AB
5 * Copyright (c) 2004-2008, 2010-2011, 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 static struct sk_buff *msg_queue_head;
50 static struct sk_buff *msg_queue_tail;
52 DEFINE_SPINLOCK(tipc_port_list_lock);
53 static DEFINE_SPINLOCK(queue_lock);
55 static LIST_HEAD(ports);
56 static void port_handle_node_down(unsigned long ref);
57 static struct sk_buff *port_build_self_abort_msg(struct tipc_port *, u32 err);
58 static struct sk_buff *port_build_peer_abort_msg(struct tipc_port *, u32 err);
59 static void port_timeout(unsigned long ref);
62 static inline u32 port_peernode(struct tipc_port *p_ptr)
64 return msg_destnode(&p_ptr->phdr);
67 static inline u32 port_peerport(struct tipc_port *p_ptr)
69 return msg_destport(&p_ptr->phdr);
73 * tipc_multicast - send a multicast message to local and remote destinations
76 int tipc_multicast(u32 ref, struct tipc_name_seq const *seq,
77 u32 num_sect, struct iovec const *msg_sect,
78 unsigned int total_len)
82 struct sk_buff *ibuf = NULL;
83 struct tipc_port_list dports = {0, NULL, };
84 struct tipc_port *oport = tipc_port_deref(ref);
91 /* Create multicast message */
94 msg_set_type(hdr, TIPC_MCAST_MSG);
95 msg_set_lookup_scope(hdr, TIPC_CLUSTER_SCOPE);
96 msg_set_destport(hdr, 0);
97 msg_set_destnode(hdr, 0);
98 msg_set_nametype(hdr, seq->type);
99 msg_set_namelower(hdr, seq->lower);
100 msg_set_nameupper(hdr, seq->upper);
101 msg_set_hdr_sz(hdr, MCAST_H_SIZE);
102 res = tipc_msg_build(hdr, msg_sect, num_sect, total_len, MAX_MSG_SIZE,
103 !oport->user_port, &buf);
107 /* Figure out where to send multicast message */
109 ext_targets = tipc_nametbl_mc_translate(seq->type, seq->lower, seq->upper,
110 TIPC_NODE_SCOPE, &dports);
112 /* Send message to destinations (duplicate it only if necessary) */
115 if (dports.count != 0) {
116 ibuf = skb_copy(buf, GFP_ATOMIC);
118 tipc_port_list_free(&dports);
123 res = tipc_bclink_send_msg(buf);
124 if ((res < 0) && (dports.count != 0))
132 tipc_port_recv_mcast(ibuf, &dports);
134 tipc_port_list_free(&dports);
140 * tipc_port_recv_mcast - deliver multicast message to all destination ports
142 * If there is no port list, perform a lookup to create one
145 void tipc_port_recv_mcast(struct sk_buff *buf, struct tipc_port_list *dp)
147 struct tipc_msg *msg;
148 struct tipc_port_list dports = {0, NULL, };
149 struct tipc_port_list *item = dp;
154 /* Create destination port list, if one wasn't supplied */
157 tipc_nametbl_mc_translate(msg_nametype(msg),
165 /* Deliver a copy of message to each destination port */
167 if (dp->count != 0) {
168 msg_set_destnode(msg, tipc_own_addr);
169 if (dp->count == 1) {
170 msg_set_destport(msg, dp->ports[0]);
171 tipc_port_recv_msg(buf);
172 tipc_port_list_free(dp);
175 for (; cnt < dp->count; cnt++) {
176 int index = cnt % PLSIZE;
177 struct sk_buff *b = skb_clone(buf, GFP_ATOMIC);
180 warn("Unable to deliver multicast message(s)\n");
183 if ((index == 0) && (cnt != 0))
185 msg_set_destport(buf_msg(b), item->ports[index]);
186 tipc_port_recv_msg(b);
191 tipc_port_list_free(dp);
195 * tipc_createport_raw - create a generic TIPC port
197 * Returns pointer to (locked) TIPC port, or NULL if unable to create it
200 struct tipc_port *tipc_createport_raw(void *usr_handle,
201 u32 (*dispatcher)(struct tipc_port *, struct sk_buff *),
202 void (*wakeup)(struct tipc_port *),
203 const u32 importance)
205 struct tipc_port *p_ptr;
206 struct tipc_msg *msg;
209 p_ptr = kzalloc(sizeof(*p_ptr), GFP_ATOMIC);
211 warn("Port creation failed, no memory\n");
214 ref = tipc_ref_acquire(p_ptr, &p_ptr->lock);
216 warn("Port creation failed, reference table exhausted\n");
221 p_ptr->usr_handle = usr_handle;
222 p_ptr->max_pkt = MAX_PKT_DEFAULT;
224 INIT_LIST_HEAD(&p_ptr->wait_list);
225 INIT_LIST_HEAD(&p_ptr->subscription.nodesub_list);
226 p_ptr->dispatcher = dispatcher;
227 p_ptr->wakeup = wakeup;
228 p_ptr->user_port = NULL;
229 k_init_timer(&p_ptr->timer, (Handler)port_timeout, ref);
230 INIT_LIST_HEAD(&p_ptr->publications);
231 INIT_LIST_HEAD(&p_ptr->port_list);
234 * Must hold port list lock while initializing message header template
235 * to ensure a change to node's own network address doesn't result
236 * in template containing out-dated network address information
239 spin_lock_bh(&tipc_port_list_lock);
241 tipc_msg_init(msg, importance, TIPC_NAMED_MSG, NAMED_H_SIZE, 0);
242 msg_set_origport(msg, ref);
243 list_add_tail(&p_ptr->port_list, &ports);
244 spin_unlock_bh(&tipc_port_list_lock);
248 int tipc_deleteport(u32 ref)
250 struct tipc_port *p_ptr;
251 struct sk_buff *buf = NULL;
253 tipc_withdraw(ref, 0, NULL);
254 p_ptr = tipc_port_lock(ref);
258 tipc_ref_discard(ref);
259 tipc_port_unlock(p_ptr);
261 k_cancel_timer(&p_ptr->timer);
262 if (p_ptr->connected) {
263 buf = port_build_peer_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
264 tipc_nodesub_unsubscribe(&p_ptr->subscription);
266 kfree(p_ptr->user_port);
268 spin_lock_bh(&tipc_port_list_lock);
269 list_del(&p_ptr->port_list);
270 list_del(&p_ptr->wait_list);
271 spin_unlock_bh(&tipc_port_list_lock);
272 k_term_timer(&p_ptr->timer);
274 tipc_net_route_msg(buf);
278 static int port_unreliable(struct tipc_port *p_ptr)
280 return msg_src_droppable(&p_ptr->phdr);
283 int tipc_portunreliable(u32 ref, unsigned int *isunreliable)
285 struct tipc_port *p_ptr;
287 p_ptr = tipc_port_lock(ref);
290 *isunreliable = port_unreliable(p_ptr);
291 tipc_port_unlock(p_ptr);
295 int tipc_set_portunreliable(u32 ref, unsigned int isunreliable)
297 struct tipc_port *p_ptr;
299 p_ptr = tipc_port_lock(ref);
302 msg_set_src_droppable(&p_ptr->phdr, (isunreliable != 0));
303 tipc_port_unlock(p_ptr);
307 static int port_unreturnable(struct tipc_port *p_ptr)
309 return msg_dest_droppable(&p_ptr->phdr);
312 int tipc_portunreturnable(u32 ref, unsigned int *isunrejectable)
314 struct tipc_port *p_ptr;
316 p_ptr = tipc_port_lock(ref);
319 *isunrejectable = port_unreturnable(p_ptr);
320 tipc_port_unlock(p_ptr);
324 int tipc_set_portunreturnable(u32 ref, unsigned int isunrejectable)
326 struct tipc_port *p_ptr;
328 p_ptr = tipc_port_lock(ref);
331 msg_set_dest_droppable(&p_ptr->phdr, (isunrejectable != 0));
332 tipc_port_unlock(p_ptr);
337 * port_build_proto_msg(): create connection protocol message for port
339 * On entry the port must be locked and connected.
341 static struct sk_buff *port_build_proto_msg(struct tipc_port *p_ptr,
345 struct tipc_msg *msg;
347 buf = tipc_buf_acquire(INT_H_SIZE);
350 tipc_msg_init(msg, CONN_MANAGER, type, INT_H_SIZE,
351 port_peernode(p_ptr));
352 msg_set_destport(msg, port_peerport(p_ptr));
353 msg_set_origport(msg, p_ptr->ref);
354 msg_set_msgcnt(msg, ack);
359 int tipc_reject_msg(struct sk_buff *buf, u32 err)
361 struct tipc_msg *msg = buf_msg(buf);
362 struct sk_buff *rbuf;
363 struct tipc_msg *rmsg;
366 u32 data_sz = msg_data_sz(msg);
370 /* discard rejected message if it shouldn't be returned to sender */
372 if (WARN(!msg_isdata(msg),
373 "attempt to reject message with user=%u", msg_user(msg))) {
377 if (msg_errcode(msg) || msg_dest_droppable(msg))
381 * construct returned message by copying rejected message header and
382 * data (or subset), then updating header fields that need adjusting
385 hdr_sz = msg_hdr_sz(msg);
386 rmsg_sz = hdr_sz + min_t(u32, data_sz, MAX_REJECT_SIZE);
388 rbuf = tipc_buf_acquire(rmsg_sz);
392 rmsg = buf_msg(rbuf);
393 skb_copy_to_linear_data(rbuf, msg, rmsg_sz);
395 if (msg_connected(rmsg)) {
396 imp = msg_importance(rmsg);
397 if (imp < TIPC_CRITICAL_IMPORTANCE)
398 msg_set_importance(rmsg, ++imp);
400 msg_set_non_seq(rmsg, 0);
401 msg_set_size(rmsg, rmsg_sz);
402 msg_set_errcode(rmsg, err);
403 msg_set_prevnode(rmsg, tipc_own_addr);
404 msg_swap_words(rmsg, 4, 5);
405 if (!msg_short(rmsg))
406 msg_swap_words(rmsg, 6, 7);
408 /* send self-abort message when rejecting on a connected port */
409 if (msg_connected(msg)) {
410 struct tipc_port *p_ptr = tipc_port_lock(msg_destport(msg));
413 struct sk_buff *abuf = NULL;
415 if (p_ptr->connected)
416 abuf = port_build_self_abort_msg(p_ptr, err);
417 tipc_port_unlock(p_ptr);
418 tipc_net_route_msg(abuf);
422 /* send returned message & dispose of rejected message */
424 src_node = msg_prevnode(msg);
425 if (src_node == tipc_own_addr)
426 tipc_port_recv_msg(rbuf);
428 tipc_link_send(rbuf, src_node, msg_link_selector(rmsg));
434 int tipc_port_reject_sections(struct tipc_port *p_ptr, struct tipc_msg *hdr,
435 struct iovec const *msg_sect, u32 num_sect,
436 unsigned int total_len, int err)
441 res = tipc_msg_build(hdr, msg_sect, num_sect, total_len, MAX_MSG_SIZE,
442 !p_ptr->user_port, &buf);
446 return tipc_reject_msg(buf, err);
449 static void port_timeout(unsigned long ref)
451 struct tipc_port *p_ptr = tipc_port_lock(ref);
452 struct sk_buff *buf = NULL;
457 if (!p_ptr->connected) {
458 tipc_port_unlock(p_ptr);
462 /* Last probe answered ? */
463 if (p_ptr->probing_state == PROBING) {
464 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
466 buf = port_build_proto_msg(p_ptr, CONN_PROBE, 0);
467 p_ptr->probing_state = PROBING;
468 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
470 tipc_port_unlock(p_ptr);
471 tipc_net_route_msg(buf);
475 static void port_handle_node_down(unsigned long ref)
477 struct tipc_port *p_ptr = tipc_port_lock(ref);
478 struct sk_buff *buf = NULL;
482 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_NODE);
483 tipc_port_unlock(p_ptr);
484 tipc_net_route_msg(buf);
488 static struct sk_buff *port_build_self_abort_msg(struct tipc_port *p_ptr, u32 err)
490 struct sk_buff *buf = port_build_peer_abort_msg(p_ptr, err);
493 struct tipc_msg *msg = buf_msg(buf);
494 msg_swap_words(msg, 4, 5);
495 msg_swap_words(msg, 6, 7);
501 static struct sk_buff *port_build_peer_abort_msg(struct tipc_port *p_ptr, u32 err)
504 struct tipc_msg *msg;
507 if (!p_ptr->connected)
510 buf = tipc_buf_acquire(BASIC_H_SIZE);
513 memcpy(msg, &p_ptr->phdr, BASIC_H_SIZE);
514 msg_set_hdr_sz(msg, BASIC_H_SIZE);
515 msg_set_size(msg, BASIC_H_SIZE);
516 imp = msg_importance(msg);
517 if (imp < TIPC_CRITICAL_IMPORTANCE)
518 msg_set_importance(msg, ++imp);
519 msg_set_errcode(msg, err);
524 void tipc_port_recv_proto_msg(struct sk_buff *buf)
526 struct tipc_msg *msg = buf_msg(buf);
527 struct tipc_port *p_ptr;
528 struct sk_buff *r_buf = NULL;
529 u32 orignode = msg_orignode(msg);
530 u32 origport = msg_origport(msg);
531 u32 destport = msg_destport(msg);
534 /* Validate connection */
536 p_ptr = tipc_port_lock(destport);
537 if (!p_ptr || !p_ptr->connected ||
538 (port_peernode(p_ptr) != orignode) ||
539 (port_peerport(p_ptr) != origport)) {
540 r_buf = tipc_buf_acquire(BASIC_H_SIZE);
542 msg = buf_msg(r_buf);
543 tipc_msg_init(msg, TIPC_HIGH_IMPORTANCE, TIPC_CONN_MSG,
544 BASIC_H_SIZE, orignode);
545 msg_set_errcode(msg, TIPC_ERR_NO_PORT);
546 msg_set_origport(msg, destport);
547 msg_set_destport(msg, origport);
550 tipc_port_unlock(p_ptr);
554 /* Process protocol message sent by peer */
556 switch (msg_type(msg)) {
558 wakeable = tipc_port_congested(p_ptr) && p_ptr->congested &&
560 p_ptr->acked += msg_msgcnt(msg);
561 if (!tipc_port_congested(p_ptr)) {
562 p_ptr->congested = 0;
564 p_ptr->wakeup(p_ptr);
568 r_buf = port_build_proto_msg(p_ptr, CONN_PROBE_REPLY, 0);
571 /* CONN_PROBE_REPLY or unrecognized - no action required */
574 p_ptr->probing_state = CONFIRMED;
575 tipc_port_unlock(p_ptr);
577 tipc_net_route_msg(r_buf);
581 static void port_print(struct tipc_port *p_ptr, struct print_buf *buf, int full_id)
583 struct publication *publ;
586 tipc_printf(buf, "<%u.%u.%u:%u>:",
587 tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
588 tipc_node(tipc_own_addr), p_ptr->ref);
590 tipc_printf(buf, "%-10u:", p_ptr->ref);
592 if (p_ptr->connected) {
593 u32 dport = port_peerport(p_ptr);
594 u32 destnode = port_peernode(p_ptr);
596 tipc_printf(buf, " connected to <%u.%u.%u:%u>",
597 tipc_zone(destnode), tipc_cluster(destnode),
598 tipc_node(destnode), dport);
599 if (p_ptr->conn_type != 0)
600 tipc_printf(buf, " via {%u,%u}",
602 p_ptr->conn_instance);
603 } else if (p_ptr->published) {
604 tipc_printf(buf, " bound to");
605 list_for_each_entry(publ, &p_ptr->publications, pport_list) {
606 if (publ->lower == publ->upper)
607 tipc_printf(buf, " {%u,%u}", publ->type,
610 tipc_printf(buf, " {%u,%u,%u}", publ->type,
611 publ->lower, publ->upper);
614 tipc_printf(buf, "\n");
617 #define MAX_PORT_QUERY 32768
619 struct sk_buff *tipc_port_get_ports(void)
622 struct tlv_desc *rep_tlv;
624 struct tipc_port *p_ptr;
627 buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_PORT_QUERY));
630 rep_tlv = (struct tlv_desc *)buf->data;
632 tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), MAX_PORT_QUERY);
633 spin_lock_bh(&tipc_port_list_lock);
634 list_for_each_entry(p_ptr, &ports, port_list) {
635 spin_lock_bh(p_ptr->lock);
636 port_print(p_ptr, &pb, 0);
637 spin_unlock_bh(p_ptr->lock);
639 spin_unlock_bh(&tipc_port_list_lock);
640 str_len = tipc_printbuf_validate(&pb);
642 skb_put(buf, TLV_SPACE(str_len));
643 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
648 void tipc_port_reinit(void)
650 struct tipc_port *p_ptr;
651 struct tipc_msg *msg;
653 spin_lock_bh(&tipc_port_list_lock);
654 list_for_each_entry(p_ptr, &ports, port_list) {
656 msg_set_prevnode(msg, tipc_own_addr);
657 msg_set_orignode(msg, tipc_own_addr);
659 spin_unlock_bh(&tipc_port_list_lock);
664 * port_dispatcher_sigh(): Signal handler for messages destinated
665 * to the tipc_port interface.
668 static void port_dispatcher_sigh(void *dummy)
672 spin_lock_bh(&queue_lock);
673 buf = msg_queue_head;
674 msg_queue_head = NULL;
675 spin_unlock_bh(&queue_lock);
678 struct tipc_port *p_ptr;
679 struct user_port *up_ptr;
680 struct tipc_portid orig;
681 struct tipc_name_seq dseq;
687 struct sk_buff *next = buf->next;
688 struct tipc_msg *msg = buf_msg(buf);
689 u32 dref = msg_destport(msg);
691 message_type = msg_type(msg);
692 if (message_type > TIPC_DIRECT_MSG)
693 goto reject; /* Unsupported message type */
695 p_ptr = tipc_port_lock(dref);
697 goto reject; /* Port deleted while msg in queue */
699 orig.ref = msg_origport(msg);
700 orig.node = msg_orignode(msg);
701 up_ptr = p_ptr->user_port;
702 usr_handle = up_ptr->usr_handle;
703 connected = p_ptr->connected;
704 published = p_ptr->published;
706 if (unlikely(msg_errcode(msg)))
709 switch (message_type) {
712 tipc_conn_msg_event cb = up_ptr->conn_msg_cb;
713 u32 peer_port = port_peerport(p_ptr);
714 u32 peer_node = port_peernode(p_ptr);
717 tipc_port_unlock(p_ptr);
720 if (unlikely(!connected)) {
721 if (tipc_connect2port(dref, &orig))
723 } else if ((msg_origport(msg) != peer_port) ||
724 (msg_orignode(msg) != peer_node))
726 dsz = msg_data_sz(msg);
728 (++p_ptr->conn_unacked >=
729 TIPC_FLOW_CONTROL_WIN)))
730 tipc_acknowledge(dref,
731 p_ptr->conn_unacked);
732 skb_pull(buf, msg_hdr_sz(msg));
733 cb(usr_handle, dref, &buf, msg_data(msg), dsz);
736 case TIPC_DIRECT_MSG:{
737 tipc_msg_event cb = up_ptr->msg_cb;
739 tipc_port_unlock(p_ptr);
740 if (unlikely(!cb || connected))
742 skb_pull(buf, msg_hdr_sz(msg));
743 cb(usr_handle, dref, &buf, msg_data(msg),
744 msg_data_sz(msg), msg_importance(msg),
749 case TIPC_NAMED_MSG:{
750 tipc_named_msg_event cb = up_ptr->named_msg_cb;
752 tipc_port_unlock(p_ptr);
753 if (unlikely(!cb || connected || !published))
755 dseq.type = msg_nametype(msg);
756 dseq.lower = msg_nameinst(msg);
757 dseq.upper = (message_type == TIPC_NAMED_MSG)
758 ? dseq.lower : msg_nameupper(msg);
759 skb_pull(buf, msg_hdr_sz(msg));
760 cb(usr_handle, dref, &buf, msg_data(msg),
761 msg_data_sz(msg), msg_importance(msg),
771 switch (message_type) {
774 tipc_conn_shutdown_event cb =
776 u32 peer_port = port_peerport(p_ptr);
777 u32 peer_node = port_peernode(p_ptr);
779 tipc_port_unlock(p_ptr);
780 if (!cb || !connected)
782 if ((msg_origport(msg) != peer_port) ||
783 (msg_orignode(msg) != peer_node))
785 tipc_disconnect(dref);
786 skb_pull(buf, msg_hdr_sz(msg));
787 cb(usr_handle, dref, &buf, msg_data(msg),
788 msg_data_sz(msg), msg_errcode(msg));
791 case TIPC_DIRECT_MSG:{
792 tipc_msg_err_event cb = up_ptr->err_cb;
794 tipc_port_unlock(p_ptr);
795 if (!cb || connected)
797 skb_pull(buf, msg_hdr_sz(msg));
798 cb(usr_handle, dref, &buf, msg_data(msg),
799 msg_data_sz(msg), msg_errcode(msg), &orig);
803 case TIPC_NAMED_MSG:{
804 tipc_named_msg_err_event cb =
805 up_ptr->named_err_cb;
807 tipc_port_unlock(p_ptr);
808 if (!cb || connected)
810 dseq.type = msg_nametype(msg);
811 dseq.lower = msg_nameinst(msg);
812 dseq.upper = (message_type == TIPC_NAMED_MSG)
813 ? dseq.lower : msg_nameupper(msg);
814 skb_pull(buf, msg_hdr_sz(msg));
815 cb(usr_handle, dref, &buf, msg_data(msg),
816 msg_data_sz(msg), msg_errcode(msg), &dseq);
825 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
831 * port_dispatcher(): Dispatcher for messages destinated
832 * to the tipc_port interface. Called with port locked.
835 static u32 port_dispatcher(struct tipc_port *dummy, struct sk_buff *buf)
838 spin_lock_bh(&queue_lock);
839 if (msg_queue_head) {
840 msg_queue_tail->next = buf;
841 msg_queue_tail = buf;
843 msg_queue_tail = msg_queue_head = buf;
844 tipc_k_signal((Handler)port_dispatcher_sigh, 0);
846 spin_unlock_bh(&queue_lock);
851 * Wake up port after congestion: Called with port locked,
855 static void port_wakeup_sh(unsigned long ref)
857 struct tipc_port *p_ptr;
858 struct user_port *up_ptr;
859 tipc_continue_event cb = NULL;
862 p_ptr = tipc_port_lock(ref);
864 up_ptr = p_ptr->user_port;
866 cb = up_ptr->continue_event_cb;
867 uh = up_ptr->usr_handle;
869 tipc_port_unlock(p_ptr);
876 static void port_wakeup(struct tipc_port *p_ptr)
878 tipc_k_signal((Handler)port_wakeup_sh, p_ptr->ref);
881 void tipc_acknowledge(u32 ref, u32 ack)
883 struct tipc_port *p_ptr;
884 struct sk_buff *buf = NULL;
886 p_ptr = tipc_port_lock(ref);
889 if (p_ptr->connected) {
890 p_ptr->conn_unacked -= ack;
891 buf = port_build_proto_msg(p_ptr, CONN_ACK, ack);
893 tipc_port_unlock(p_ptr);
894 tipc_net_route_msg(buf);
898 * tipc_createport(): user level call.
901 int tipc_createport(void *usr_handle,
902 unsigned int importance,
903 tipc_msg_err_event error_cb,
904 tipc_named_msg_err_event named_error_cb,
905 tipc_conn_shutdown_event conn_error_cb,
906 tipc_msg_event msg_cb,
907 tipc_named_msg_event named_msg_cb,
908 tipc_conn_msg_event conn_msg_cb,
909 tipc_continue_event continue_event_cb,/* May be zero */
912 struct user_port *up_ptr;
913 struct tipc_port *p_ptr;
915 up_ptr = kmalloc(sizeof(*up_ptr), GFP_ATOMIC);
917 warn("Port creation failed, no memory\n");
920 p_ptr = (struct tipc_port *)tipc_createport_raw(NULL, port_dispatcher,
921 port_wakeup, importance);
927 p_ptr->user_port = up_ptr;
928 up_ptr->usr_handle = usr_handle;
929 up_ptr->ref = p_ptr->ref;
930 up_ptr->err_cb = error_cb;
931 up_ptr->named_err_cb = named_error_cb;
932 up_ptr->conn_err_cb = conn_error_cb;
933 up_ptr->msg_cb = msg_cb;
934 up_ptr->named_msg_cb = named_msg_cb;
935 up_ptr->conn_msg_cb = conn_msg_cb;
936 up_ptr->continue_event_cb = continue_event_cb;
937 *portref = p_ptr->ref;
938 tipc_port_unlock(p_ptr);
942 int tipc_portimportance(u32 ref, unsigned int *importance)
944 struct tipc_port *p_ptr;
946 p_ptr = tipc_port_lock(ref);
949 *importance = (unsigned int)msg_importance(&p_ptr->phdr);
950 tipc_port_unlock(p_ptr);
954 int tipc_set_portimportance(u32 ref, unsigned int imp)
956 struct tipc_port *p_ptr;
958 if (imp > TIPC_CRITICAL_IMPORTANCE)
961 p_ptr = tipc_port_lock(ref);
964 msg_set_importance(&p_ptr->phdr, (u32)imp);
965 tipc_port_unlock(p_ptr);
970 int tipc_publish(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
972 struct tipc_port *p_ptr;
973 struct publication *publ;
977 p_ptr = tipc_port_lock(ref);
981 if (p_ptr->connected)
983 if (seq->lower > seq->upper)
985 if ((scope < TIPC_ZONE_SCOPE) || (scope > TIPC_NODE_SCOPE))
987 key = ref + p_ptr->pub_count + 1;
992 publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
993 scope, p_ptr->ref, key);
995 list_add(&publ->pport_list, &p_ptr->publications);
997 p_ptr->published = 1;
1001 tipc_port_unlock(p_ptr);
1005 int tipc_withdraw(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
1007 struct tipc_port *p_ptr;
1008 struct publication *publ;
1009 struct publication *tpubl;
1012 p_ptr = tipc_port_lock(ref);
1016 list_for_each_entry_safe(publ, tpubl,
1017 &p_ptr->publications, pport_list) {
1018 tipc_nametbl_withdraw(publ->type, publ->lower,
1019 publ->ref, publ->key);
1023 list_for_each_entry_safe(publ, tpubl,
1024 &p_ptr->publications, pport_list) {
1025 if (publ->scope != scope)
1027 if (publ->type != seq->type)
1029 if (publ->lower != seq->lower)
1031 if (publ->upper != seq->upper)
1033 tipc_nametbl_withdraw(publ->type, publ->lower,
1034 publ->ref, publ->key);
1039 if (list_empty(&p_ptr->publications))
1040 p_ptr->published = 0;
1041 tipc_port_unlock(p_ptr);
1045 int tipc_connect2port(u32 ref, struct tipc_portid const *peer)
1047 struct tipc_port *p_ptr;
1048 struct tipc_msg *msg;
1051 p_ptr = tipc_port_lock(ref);
1054 if (p_ptr->published || p_ptr->connected)
1060 msg_set_destnode(msg, peer->node);
1061 msg_set_destport(msg, peer->ref);
1062 msg_set_type(msg, TIPC_CONN_MSG);
1063 msg_set_lookup_scope(msg, 0);
1064 msg_set_hdr_sz(msg, SHORT_H_SIZE);
1066 p_ptr->probing_interval = PROBING_INTERVAL;
1067 p_ptr->probing_state = CONFIRMED;
1068 p_ptr->connected = 1;
1069 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
1071 tipc_nodesub_subscribe(&p_ptr->subscription, peer->node,
1072 (void *)(unsigned long)ref,
1073 (net_ev_handler)port_handle_node_down);
1076 tipc_port_unlock(p_ptr);
1077 p_ptr->max_pkt = tipc_link_get_max_pkt(peer->node, ref);
1082 * tipc_disconnect_port - disconnect port from peer
1084 * Port must be locked.
1087 int tipc_disconnect_port(struct tipc_port *tp_ptr)
1091 if (tp_ptr->connected) {
1092 tp_ptr->connected = 0;
1093 /* let timer expire on it's own to avoid deadlock! */
1094 tipc_nodesub_unsubscribe(
1095 &((struct tipc_port *)tp_ptr)->subscription);
1104 * tipc_disconnect(): Disconnect port form peer.
1105 * This is a node local operation.
1108 int tipc_disconnect(u32 ref)
1110 struct tipc_port *p_ptr;
1113 p_ptr = tipc_port_lock(ref);
1116 res = tipc_disconnect_port((struct tipc_port *)p_ptr);
1117 tipc_port_unlock(p_ptr);
1122 * tipc_shutdown(): Send a SHUTDOWN msg to peer and disconnect
1124 int tipc_shutdown(u32 ref)
1126 struct tipc_port *p_ptr;
1127 struct sk_buff *buf = NULL;
1129 p_ptr = tipc_port_lock(ref);
1133 buf = port_build_peer_abort_msg(p_ptr, TIPC_CONN_SHUTDOWN);
1134 tipc_port_unlock(p_ptr);
1135 tipc_net_route_msg(buf);
1136 return tipc_disconnect(ref);
1140 * tipc_port_recv_msg - receive message from lower layer and deliver to port user
1143 int tipc_port_recv_msg(struct sk_buff *buf)
1145 struct tipc_port *p_ptr;
1146 struct tipc_msg *msg = buf_msg(buf);
1147 u32 destport = msg_destport(msg);
1148 u32 dsz = msg_data_sz(msg);
1151 /* forward unresolved named message */
1152 if (unlikely(!destport)) {
1153 tipc_net_route_msg(buf);
1157 /* validate destination & pass to port, otherwise reject message */
1158 p_ptr = tipc_port_lock(destport);
1159 if (likely(p_ptr)) {
1160 if (likely(p_ptr->connected)) {
1161 if ((unlikely(msg_origport(msg) !=
1162 port_peerport(p_ptr))) ||
1163 (unlikely(msg_orignode(msg) !=
1164 port_peernode(p_ptr))) ||
1165 (unlikely(!msg_connected(msg)))) {
1166 err = TIPC_ERR_NO_PORT;
1167 tipc_port_unlock(p_ptr);
1171 err = p_ptr->dispatcher(p_ptr, buf);
1172 tipc_port_unlock(p_ptr);
1176 err = TIPC_ERR_NO_PORT;
1179 return tipc_reject_msg(buf, err);
1183 * tipc_port_recv_sections(): Concatenate and deliver sectioned
1184 * message for this node.
1187 static int tipc_port_recv_sections(struct tipc_port *sender, unsigned int num_sect,
1188 struct iovec const *msg_sect,
1189 unsigned int total_len)
1191 struct sk_buff *buf;
1194 res = tipc_msg_build(&sender->phdr, msg_sect, num_sect, total_len,
1195 MAX_MSG_SIZE, !sender->user_port, &buf);
1197 tipc_port_recv_msg(buf);
1202 * tipc_send - send message sections on connection
1205 int tipc_send(u32 ref, unsigned int num_sect, struct iovec const *msg_sect,
1206 unsigned int total_len)
1208 struct tipc_port *p_ptr;
1212 p_ptr = tipc_port_deref(ref);
1213 if (!p_ptr || !p_ptr->connected)
1216 p_ptr->congested = 1;
1217 if (!tipc_port_congested(p_ptr)) {
1218 destnode = port_peernode(p_ptr);
1219 if (likely(destnode != tipc_own_addr))
1220 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
1221 total_len, destnode);
1223 res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect,
1226 if (likely(res != -ELINKCONG)) {
1227 p_ptr->congested = 0;
1233 if (port_unreliable(p_ptr)) {
1234 p_ptr->congested = 0;
1241 * tipc_send2name - send message sections to port name
1244 int tipc_send2name(u32 ref, struct tipc_name const *name, unsigned int domain,
1245 unsigned int num_sect, struct iovec const *msg_sect,
1246 unsigned int total_len)
1248 struct tipc_port *p_ptr;
1249 struct tipc_msg *msg;
1250 u32 destnode = domain;
1254 p_ptr = tipc_port_deref(ref);
1255 if (!p_ptr || p_ptr->connected)
1259 msg_set_type(msg, TIPC_NAMED_MSG);
1260 msg_set_hdr_sz(msg, NAMED_H_SIZE);
1261 msg_set_nametype(msg, name->type);
1262 msg_set_nameinst(msg, name->instance);
1263 msg_set_lookup_scope(msg, tipc_addr_scope(domain));
1264 destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
1265 msg_set_destnode(msg, destnode);
1266 msg_set_destport(msg, destport);
1268 if (likely(destport || destnode)) {
1269 if (likely(destnode == tipc_own_addr))
1270 res = tipc_port_recv_sections(p_ptr, num_sect,
1271 msg_sect, total_len);
1273 res = tipc_link_send_sections_fast(p_ptr, msg_sect,
1274 num_sect, total_len,
1276 if (likely(res != -ELINKCONG)) {
1281 if (port_unreliable(p_ptr)) {
1286 return tipc_port_reject_sections(p_ptr, msg, msg_sect, num_sect,
1287 total_len, TIPC_ERR_NO_NAME);
1291 * tipc_send2port - send message sections to port identity
1294 int tipc_send2port(u32 ref, struct tipc_portid const *dest,
1295 unsigned int num_sect, struct iovec const *msg_sect,
1296 unsigned int total_len)
1298 struct tipc_port *p_ptr;
1299 struct tipc_msg *msg;
1302 p_ptr = tipc_port_deref(ref);
1303 if (!p_ptr || p_ptr->connected)
1307 msg_set_type(msg, TIPC_DIRECT_MSG);
1308 msg_set_lookup_scope(msg, 0);
1309 msg_set_destnode(msg, dest->node);
1310 msg_set_destport(msg, dest->ref);
1311 msg_set_hdr_sz(msg, BASIC_H_SIZE);
1313 if (dest->node == tipc_own_addr)
1314 res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect,
1317 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
1318 total_len, dest->node);
1319 if (likely(res != -ELINKCONG)) {
1324 if (port_unreliable(p_ptr)) {
1331 * tipc_send_buf2port - send message buffer to port identity
1334 int tipc_send_buf2port(u32 ref, struct tipc_portid const *dest,
1335 struct sk_buff *buf, unsigned int dsz)
1337 struct tipc_port *p_ptr;
1338 struct tipc_msg *msg;
1341 p_ptr = (struct tipc_port *)tipc_ref_deref(ref);
1342 if (!p_ptr || p_ptr->connected)
1346 msg_set_type(msg, TIPC_DIRECT_MSG);
1347 msg_set_destnode(msg, dest->node);
1348 msg_set_destport(msg, dest->ref);
1349 msg_set_hdr_sz(msg, BASIC_H_SIZE);
1350 msg_set_size(msg, BASIC_H_SIZE + dsz);
1351 if (skb_cow(buf, BASIC_H_SIZE))
1354 skb_push(buf, BASIC_H_SIZE);
1355 skb_copy_to_linear_data(buf, msg, BASIC_H_SIZE);
1357 if (dest->node == tipc_own_addr)
1358 res = tipc_port_recv_msg(buf);
1360 res = tipc_send_buf_fast(buf, dest->node);
1361 if (likely(res != -ELINKCONG)) {
1366 if (port_unreliable(p_ptr))