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 u32 port_peernode(struct tipc_port *p_ptr)
64 return msg_destnode(&p_ptr->phdr);
67 static 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)
81 struct sk_buff *ibuf = NULL;
82 struct port_list dports = {0, NULL, };
83 struct tipc_port *oport = tipc_port_deref(ref);
90 /* Create multicast message */
93 msg_set_type(hdr, TIPC_MCAST_MSG);
94 msg_set_nametype(hdr, seq->type);
95 msg_set_namelower(hdr, seq->lower);
96 msg_set_nameupper(hdr, seq->upper);
97 msg_set_hdr_sz(hdr, MCAST_H_SIZE);
98 res = tipc_msg_build(hdr, msg_sect, num_sect, MAX_MSG_SIZE,
99 !oport->user_port, &buf);
103 /* Figure out where to send multicast message */
105 ext_targets = tipc_nametbl_mc_translate(seq->type, seq->lower, seq->upper,
106 TIPC_NODE_SCOPE, &dports);
108 /* Send message to destinations (duplicate it only if necessary) */
111 if (dports.count != 0) {
112 ibuf = skb_copy(buf, GFP_ATOMIC);
114 tipc_port_list_free(&dports);
119 res = tipc_bclink_send_msg(buf);
120 if ((res < 0) && (dports.count != 0))
128 tipc_port_recv_mcast(ibuf, &dports);
130 tipc_port_list_free(&dports);
136 * tipc_port_recv_mcast - deliver multicast message to all destination ports
138 * If there is no port list, perform a lookup to create one
141 void tipc_port_recv_mcast(struct sk_buff *buf, struct port_list *dp)
143 struct tipc_msg *msg;
144 struct port_list dports = {0, NULL, };
145 struct port_list *item = dp;
150 /* Create destination port list, if one wasn't supplied */
153 tipc_nametbl_mc_translate(msg_nametype(msg),
161 /* Deliver a copy of message to each destination port */
163 if (dp->count != 0) {
164 if (dp->count == 1) {
165 msg_set_destport(msg, dp->ports[0]);
166 tipc_port_recv_msg(buf);
167 tipc_port_list_free(dp);
170 for (; cnt < dp->count; cnt++) {
171 int index = cnt % PLSIZE;
172 struct sk_buff *b = skb_clone(buf, GFP_ATOMIC);
175 warn("Unable to deliver multicast message(s)\n");
178 if ((index == 0) && (cnt != 0))
180 msg_set_destport(buf_msg(b), item->ports[index]);
181 tipc_port_recv_msg(b);
186 tipc_port_list_free(dp);
190 * tipc_createport_raw - create a generic TIPC port
192 * Returns pointer to (locked) TIPC port, or NULL if unable to create it
195 struct tipc_port *tipc_createport_raw(void *usr_handle,
196 u32 (*dispatcher)(struct tipc_port *, struct sk_buff *),
197 void (*wakeup)(struct tipc_port *),
198 const u32 importance)
200 struct tipc_port *p_ptr;
201 struct tipc_msg *msg;
204 p_ptr = kzalloc(sizeof(*p_ptr), GFP_ATOMIC);
206 warn("Port creation failed, no memory\n");
209 ref = tipc_ref_acquire(p_ptr, &p_ptr->lock);
211 warn("Port creation failed, reference table exhausted\n");
216 p_ptr->usr_handle = usr_handle;
217 p_ptr->max_pkt = MAX_PKT_DEFAULT;
220 tipc_msg_init(msg, importance, TIPC_NAMED_MSG, LONG_H_SIZE, 0);
221 msg_set_origport(msg, ref);
222 INIT_LIST_HEAD(&p_ptr->wait_list);
223 INIT_LIST_HEAD(&p_ptr->subscription.nodesub_list);
224 p_ptr->dispatcher = dispatcher;
225 p_ptr->wakeup = wakeup;
226 p_ptr->user_port = NULL;
227 k_init_timer(&p_ptr->timer, (Handler)port_timeout, ref);
228 spin_lock_bh(&tipc_port_list_lock);
229 INIT_LIST_HEAD(&p_ptr->publications);
230 INIT_LIST_HEAD(&p_ptr->port_list);
231 list_add_tail(&p_ptr->port_list, &ports);
232 spin_unlock_bh(&tipc_port_list_lock);
236 int tipc_deleteport(u32 ref)
238 struct tipc_port *p_ptr;
239 struct sk_buff *buf = NULL;
241 tipc_withdraw(ref, 0, NULL);
242 p_ptr = tipc_port_lock(ref);
246 tipc_ref_discard(ref);
247 tipc_port_unlock(p_ptr);
249 k_cancel_timer(&p_ptr->timer);
250 if (p_ptr->connected) {
251 buf = port_build_peer_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
252 tipc_nodesub_unsubscribe(&p_ptr->subscription);
254 kfree(p_ptr->user_port);
256 spin_lock_bh(&tipc_port_list_lock);
257 list_del(&p_ptr->port_list);
258 list_del(&p_ptr->wait_list);
259 spin_unlock_bh(&tipc_port_list_lock);
260 k_term_timer(&p_ptr->timer);
262 tipc_net_route_msg(buf);
266 static int port_unreliable(struct tipc_port *p_ptr)
268 return msg_src_droppable(&p_ptr->phdr);
271 int tipc_portunreliable(u32 ref, unsigned int *isunreliable)
273 struct tipc_port *p_ptr;
275 p_ptr = tipc_port_lock(ref);
278 *isunreliable = port_unreliable(p_ptr);
279 tipc_port_unlock(p_ptr);
283 int tipc_set_portunreliable(u32 ref, unsigned int isunreliable)
285 struct tipc_port *p_ptr;
287 p_ptr = tipc_port_lock(ref);
290 msg_set_src_droppable(&p_ptr->phdr, (isunreliable != 0));
291 tipc_port_unlock(p_ptr);
295 static int port_unreturnable(struct tipc_port *p_ptr)
297 return msg_dest_droppable(&p_ptr->phdr);
300 int tipc_portunreturnable(u32 ref, unsigned int *isunrejectable)
302 struct tipc_port *p_ptr;
304 p_ptr = tipc_port_lock(ref);
307 *isunrejectable = port_unreturnable(p_ptr);
308 tipc_port_unlock(p_ptr);
312 int tipc_set_portunreturnable(u32 ref, unsigned int isunrejectable)
314 struct tipc_port *p_ptr;
316 p_ptr = tipc_port_lock(ref);
319 msg_set_dest_droppable(&p_ptr->phdr, (isunrejectable != 0));
320 tipc_port_unlock(p_ptr);
325 * port_build_proto_msg(): build a port level protocol
326 * or a connection abortion message. Called with
329 static struct sk_buff *port_build_proto_msg(u32 destport, u32 destnode,
330 u32 origport, u32 orignode,
331 u32 usr, u32 type, u32 err,
335 struct tipc_msg *msg;
337 buf = tipc_buf_acquire(LONG_H_SIZE);
340 tipc_msg_init(msg, usr, type, LONG_H_SIZE, destnode);
341 msg_set_errcode(msg, err);
342 msg_set_destport(msg, destport);
343 msg_set_origport(msg, origport);
344 msg_set_orignode(msg, orignode);
345 msg_set_msgcnt(msg, ack);
350 int tipc_reject_msg(struct sk_buff *buf, u32 err)
352 struct tipc_msg *msg = buf_msg(buf);
353 struct sk_buff *rbuf;
354 struct tipc_msg *rmsg;
356 u32 imp = msg_importance(msg);
357 u32 data_sz = msg_data_sz(msg);
359 if (data_sz > MAX_REJECT_SIZE)
360 data_sz = MAX_REJECT_SIZE;
361 if (msg_connected(msg) && (imp < TIPC_CRITICAL_IMPORTANCE))
364 /* discard rejected message if it shouldn't be returned to sender */
365 if (msg_errcode(msg) || msg_dest_droppable(msg)) {
370 /* construct rejected message */
372 hdr_sz = MCAST_H_SIZE;
374 hdr_sz = LONG_H_SIZE;
375 rbuf = tipc_buf_acquire(data_sz + hdr_sz);
380 rmsg = buf_msg(rbuf);
381 tipc_msg_init(rmsg, imp, msg_type(msg), hdr_sz, msg_orignode(msg));
382 msg_set_errcode(rmsg, err);
383 msg_set_destport(rmsg, msg_origport(msg));
384 msg_set_origport(rmsg, msg_destport(msg));
385 if (msg_short(msg)) {
386 msg_set_orignode(rmsg, tipc_own_addr);
387 /* leave name type & instance as zeroes */
389 msg_set_orignode(rmsg, msg_destnode(msg));
390 msg_set_nametype(rmsg, msg_nametype(msg));
391 msg_set_nameinst(rmsg, msg_nameinst(msg));
393 msg_set_size(rmsg, data_sz + hdr_sz);
394 skb_copy_to_linear_data_offset(rbuf, hdr_sz, msg_data(msg), data_sz);
396 /* send self-abort message when rejecting on a connected port */
397 if (msg_connected(msg)) {
398 struct sk_buff *abuf = NULL;
399 struct tipc_port *p_ptr = tipc_port_lock(msg_destport(msg));
402 if (p_ptr->connected)
403 abuf = port_build_self_abort_msg(p_ptr, err);
404 tipc_port_unlock(p_ptr);
406 tipc_net_route_msg(abuf);
409 /* send rejected message */
411 tipc_net_route_msg(rbuf);
415 int tipc_port_reject_sections(struct tipc_port *p_ptr, struct tipc_msg *hdr,
416 struct iovec const *msg_sect, u32 num_sect,
422 res = tipc_msg_build(hdr, msg_sect, num_sect, MAX_MSG_SIZE,
423 !p_ptr->user_port, &buf);
427 return tipc_reject_msg(buf, err);
430 static void port_timeout(unsigned long ref)
432 struct tipc_port *p_ptr = tipc_port_lock(ref);
433 struct sk_buff *buf = NULL;
438 if (!p_ptr->connected) {
439 tipc_port_unlock(p_ptr);
443 /* Last probe answered ? */
444 if (p_ptr->probing_state == PROBING) {
445 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
447 buf = port_build_proto_msg(port_peerport(p_ptr),
448 port_peernode(p_ptr),
455 p_ptr->probing_state = PROBING;
456 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
458 tipc_port_unlock(p_ptr);
459 tipc_net_route_msg(buf);
463 static void port_handle_node_down(unsigned long ref)
465 struct tipc_port *p_ptr = tipc_port_lock(ref);
466 struct sk_buff *buf = NULL;
470 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_NODE);
471 tipc_port_unlock(p_ptr);
472 tipc_net_route_msg(buf);
476 static struct sk_buff *port_build_self_abort_msg(struct tipc_port *p_ptr, u32 err)
478 u32 imp = msg_importance(&p_ptr->phdr);
480 if (!p_ptr->connected)
482 if (imp < TIPC_CRITICAL_IMPORTANCE)
484 return port_build_proto_msg(p_ptr->ref,
486 port_peerport(p_ptr),
487 port_peernode(p_ptr),
495 static struct sk_buff *port_build_peer_abort_msg(struct tipc_port *p_ptr, u32 err)
497 u32 imp = msg_importance(&p_ptr->phdr);
499 if (!p_ptr->connected)
501 if (imp < TIPC_CRITICAL_IMPORTANCE)
503 return port_build_proto_msg(port_peerport(p_ptr),
504 port_peernode(p_ptr),
513 void tipc_port_recv_proto_msg(struct sk_buff *buf)
515 struct tipc_msg *msg = buf_msg(buf);
516 struct tipc_port *p_ptr = tipc_port_lock(msg_destport(msg));
518 struct sk_buff *r_buf = NULL;
519 struct sk_buff *abort_buf = NULL;
522 err = TIPC_ERR_NO_PORT;
523 } else if (p_ptr->connected) {
524 if ((port_peernode(p_ptr) != msg_orignode(msg)) ||
525 (port_peerport(p_ptr) != msg_origport(msg))) {
526 err = TIPC_ERR_NO_PORT;
527 } else if (msg_type(msg) == CONN_ACK) {
528 int wakeup = tipc_port_congested(p_ptr) &&
531 p_ptr->acked += msg_msgcnt(msg);
532 if (tipc_port_congested(p_ptr))
534 p_ptr->congested = 0;
537 p_ptr->wakeup(p_ptr);
540 } else if (p_ptr->published) {
541 err = TIPC_ERR_NO_PORT;
544 r_buf = port_build_proto_msg(msg_origport(msg),
548 TIPC_HIGH_IMPORTANCE,
556 if (msg_type(msg) == CONN_PROBE) {
557 r_buf = port_build_proto_msg(msg_origport(msg),
566 p_ptr->probing_state = CONFIRMED;
569 tipc_port_unlock(p_ptr);
570 tipc_net_route_msg(r_buf);
571 tipc_net_route_msg(abort_buf);
575 static void port_print(struct tipc_port *p_ptr, struct print_buf *buf, int full_id)
577 struct publication *publ;
580 tipc_printf(buf, "<%u.%u.%u:%u>:",
581 tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
582 tipc_node(tipc_own_addr), p_ptr->ref);
584 tipc_printf(buf, "%-10u:", p_ptr->ref);
586 if (p_ptr->connected) {
587 u32 dport = port_peerport(p_ptr);
588 u32 destnode = port_peernode(p_ptr);
590 tipc_printf(buf, " connected to <%u.%u.%u:%u>",
591 tipc_zone(destnode), tipc_cluster(destnode),
592 tipc_node(destnode), dport);
593 if (p_ptr->conn_type != 0)
594 tipc_printf(buf, " via {%u,%u}",
596 p_ptr->conn_instance);
597 } else if (p_ptr->published) {
598 tipc_printf(buf, " bound to");
599 list_for_each_entry(publ, &p_ptr->publications, pport_list) {
600 if (publ->lower == publ->upper)
601 tipc_printf(buf, " {%u,%u}", publ->type,
604 tipc_printf(buf, " {%u,%u,%u}", publ->type,
605 publ->lower, publ->upper);
608 tipc_printf(buf, "\n");
611 #define MAX_PORT_QUERY 32768
613 struct sk_buff *tipc_port_get_ports(void)
616 struct tlv_desc *rep_tlv;
618 struct tipc_port *p_ptr;
621 buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_PORT_QUERY));
624 rep_tlv = (struct tlv_desc *)buf->data;
626 tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), MAX_PORT_QUERY);
627 spin_lock_bh(&tipc_port_list_lock);
628 list_for_each_entry(p_ptr, &ports, port_list) {
629 spin_lock_bh(p_ptr->lock);
630 port_print(p_ptr, &pb, 0);
631 spin_unlock_bh(p_ptr->lock);
633 spin_unlock_bh(&tipc_port_list_lock);
634 str_len = tipc_printbuf_validate(&pb);
636 skb_put(buf, TLV_SPACE(str_len));
637 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
642 void tipc_port_reinit(void)
644 struct tipc_port *p_ptr;
645 struct tipc_msg *msg;
647 spin_lock_bh(&tipc_port_list_lock);
648 list_for_each_entry(p_ptr, &ports, port_list) {
650 if (msg_orignode(msg) == tipc_own_addr)
652 msg_set_prevnode(msg, tipc_own_addr);
653 msg_set_orignode(msg, tipc_own_addr);
655 spin_unlock_bh(&tipc_port_list_lock);
660 * port_dispatcher_sigh(): Signal handler for messages destinated
661 * to the tipc_port interface.
664 static void port_dispatcher_sigh(void *dummy)
668 spin_lock_bh(&queue_lock);
669 buf = msg_queue_head;
670 msg_queue_head = NULL;
671 spin_unlock_bh(&queue_lock);
674 struct tipc_port *p_ptr;
675 struct user_port *up_ptr;
676 struct tipc_portid orig;
677 struct tipc_name_seq dseq;
683 struct sk_buff *next = buf->next;
684 struct tipc_msg *msg = buf_msg(buf);
685 u32 dref = msg_destport(msg);
687 message_type = msg_type(msg);
688 if (message_type > TIPC_DIRECT_MSG)
689 goto reject; /* Unsupported message type */
691 p_ptr = tipc_port_lock(dref);
693 goto reject; /* Port deleted while msg in queue */
695 orig.ref = msg_origport(msg);
696 orig.node = msg_orignode(msg);
697 up_ptr = p_ptr->user_port;
698 usr_handle = up_ptr->usr_handle;
699 connected = p_ptr->connected;
700 published = p_ptr->published;
702 if (unlikely(msg_errcode(msg)))
705 switch (message_type) {
708 tipc_conn_msg_event cb = up_ptr->conn_msg_cb;
709 u32 peer_port = port_peerport(p_ptr);
710 u32 peer_node = port_peernode(p_ptr);
713 tipc_port_unlock(p_ptr);
716 if (unlikely(!connected)) {
717 if (tipc_connect2port(dref, &orig))
719 } else if ((msg_origport(msg) != peer_port) ||
720 (msg_orignode(msg) != peer_node))
722 dsz = msg_data_sz(msg);
724 (++p_ptr->conn_unacked >=
725 TIPC_FLOW_CONTROL_WIN)))
726 tipc_acknowledge(dref,
727 p_ptr->conn_unacked);
728 skb_pull(buf, msg_hdr_sz(msg));
729 cb(usr_handle, dref, &buf, msg_data(msg), dsz);
732 case TIPC_DIRECT_MSG:{
733 tipc_msg_event cb = up_ptr->msg_cb;
735 tipc_port_unlock(p_ptr);
736 if (unlikely(!cb || connected))
738 skb_pull(buf, msg_hdr_sz(msg));
739 cb(usr_handle, dref, &buf, msg_data(msg),
740 msg_data_sz(msg), msg_importance(msg),
745 case TIPC_NAMED_MSG:{
746 tipc_named_msg_event cb = up_ptr->named_msg_cb;
748 tipc_port_unlock(p_ptr);
749 if (unlikely(!cb || connected || !published))
751 dseq.type = msg_nametype(msg);
752 dseq.lower = msg_nameinst(msg);
753 dseq.upper = (message_type == TIPC_NAMED_MSG)
754 ? dseq.lower : msg_nameupper(msg);
755 skb_pull(buf, msg_hdr_sz(msg));
756 cb(usr_handle, dref, &buf, msg_data(msg),
757 msg_data_sz(msg), msg_importance(msg),
767 switch (message_type) {
770 tipc_conn_shutdown_event cb =
772 u32 peer_port = port_peerport(p_ptr);
773 u32 peer_node = port_peernode(p_ptr);
775 tipc_port_unlock(p_ptr);
776 if (!cb || !connected)
778 if ((msg_origport(msg) != peer_port) ||
779 (msg_orignode(msg) != peer_node))
781 tipc_disconnect(dref);
782 skb_pull(buf, msg_hdr_sz(msg));
783 cb(usr_handle, dref, &buf, msg_data(msg),
784 msg_data_sz(msg), msg_errcode(msg));
787 case TIPC_DIRECT_MSG:{
788 tipc_msg_err_event cb = up_ptr->err_cb;
790 tipc_port_unlock(p_ptr);
791 if (!cb || connected)
793 skb_pull(buf, msg_hdr_sz(msg));
794 cb(usr_handle, dref, &buf, msg_data(msg),
795 msg_data_sz(msg), msg_errcode(msg), &orig);
799 case TIPC_NAMED_MSG:{
800 tipc_named_msg_err_event cb =
801 up_ptr->named_err_cb;
803 tipc_port_unlock(p_ptr);
804 if (!cb || connected)
806 dseq.type = msg_nametype(msg);
807 dseq.lower = msg_nameinst(msg);
808 dseq.upper = (message_type == TIPC_NAMED_MSG)
809 ? dseq.lower : msg_nameupper(msg);
810 skb_pull(buf, msg_hdr_sz(msg));
811 cb(usr_handle, dref, &buf, msg_data(msg),
812 msg_data_sz(msg), msg_errcode(msg), &dseq);
821 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
827 * port_dispatcher(): Dispatcher for messages destinated
828 * to the tipc_port interface. Called with port locked.
831 static u32 port_dispatcher(struct tipc_port *dummy, struct sk_buff *buf)
834 spin_lock_bh(&queue_lock);
835 if (msg_queue_head) {
836 msg_queue_tail->next = buf;
837 msg_queue_tail = buf;
839 msg_queue_tail = msg_queue_head = buf;
840 tipc_k_signal((Handler)port_dispatcher_sigh, 0);
842 spin_unlock_bh(&queue_lock);
847 * Wake up port after congestion: Called with port locked,
851 static void port_wakeup_sh(unsigned long ref)
853 struct tipc_port *p_ptr;
854 struct user_port *up_ptr;
855 tipc_continue_event cb = NULL;
858 p_ptr = tipc_port_lock(ref);
860 up_ptr = p_ptr->user_port;
862 cb = up_ptr->continue_event_cb;
863 uh = up_ptr->usr_handle;
865 tipc_port_unlock(p_ptr);
872 static void port_wakeup(struct tipc_port *p_ptr)
874 tipc_k_signal((Handler)port_wakeup_sh, p_ptr->ref);
877 void tipc_acknowledge(u32 ref, u32 ack)
879 struct tipc_port *p_ptr;
880 struct sk_buff *buf = NULL;
882 p_ptr = tipc_port_lock(ref);
885 if (p_ptr->connected) {
886 p_ptr->conn_unacked -= ack;
887 buf = port_build_proto_msg(port_peerport(p_ptr),
888 port_peernode(p_ptr),
896 tipc_port_unlock(p_ptr);
897 tipc_net_route_msg(buf);
901 * tipc_createport(): user level call.
904 int tipc_createport(void *usr_handle,
905 unsigned int importance,
906 tipc_msg_err_event error_cb,
907 tipc_named_msg_err_event named_error_cb,
908 tipc_conn_shutdown_event conn_error_cb,
909 tipc_msg_event msg_cb,
910 tipc_named_msg_event named_msg_cb,
911 tipc_conn_msg_event conn_msg_cb,
912 tipc_continue_event continue_event_cb,/* May be zero */
915 struct user_port *up_ptr;
916 struct tipc_port *p_ptr;
918 up_ptr = kmalloc(sizeof(*up_ptr), GFP_ATOMIC);
920 warn("Port creation failed, no memory\n");
923 p_ptr = (struct tipc_port *)tipc_createport_raw(NULL, port_dispatcher,
924 port_wakeup, importance);
930 p_ptr->user_port = up_ptr;
931 up_ptr->usr_handle = usr_handle;
932 up_ptr->ref = p_ptr->ref;
933 up_ptr->err_cb = error_cb;
934 up_ptr->named_err_cb = named_error_cb;
935 up_ptr->conn_err_cb = conn_error_cb;
936 up_ptr->msg_cb = msg_cb;
937 up_ptr->named_msg_cb = named_msg_cb;
938 up_ptr->conn_msg_cb = conn_msg_cb;
939 up_ptr->continue_event_cb = continue_event_cb;
940 *portref = p_ptr->ref;
941 tipc_port_unlock(p_ptr);
945 int tipc_portimportance(u32 ref, unsigned int *importance)
947 struct tipc_port *p_ptr;
949 p_ptr = tipc_port_lock(ref);
952 *importance = (unsigned int)msg_importance(&p_ptr->phdr);
953 tipc_port_unlock(p_ptr);
957 int tipc_set_portimportance(u32 ref, unsigned int imp)
959 struct tipc_port *p_ptr;
961 if (imp > TIPC_CRITICAL_IMPORTANCE)
964 p_ptr = tipc_port_lock(ref);
967 msg_set_importance(&p_ptr->phdr, (u32)imp);
968 tipc_port_unlock(p_ptr);
973 int tipc_publish(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
975 struct tipc_port *p_ptr;
976 struct publication *publ;
980 p_ptr = tipc_port_lock(ref);
984 if (p_ptr->connected)
986 if (seq->lower > seq->upper)
988 if ((scope < TIPC_ZONE_SCOPE) || (scope > TIPC_NODE_SCOPE))
990 key = ref + p_ptr->pub_count + 1;
995 publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
996 scope, p_ptr->ref, key);
998 list_add(&publ->pport_list, &p_ptr->publications);
1000 p_ptr->published = 1;
1004 tipc_port_unlock(p_ptr);
1008 int tipc_withdraw(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
1010 struct tipc_port *p_ptr;
1011 struct publication *publ;
1012 struct publication *tpubl;
1015 p_ptr = tipc_port_lock(ref);
1019 list_for_each_entry_safe(publ, tpubl,
1020 &p_ptr->publications, pport_list) {
1021 tipc_nametbl_withdraw(publ->type, publ->lower,
1022 publ->ref, publ->key);
1026 list_for_each_entry_safe(publ, tpubl,
1027 &p_ptr->publications, pport_list) {
1028 if (publ->scope != scope)
1030 if (publ->type != seq->type)
1032 if (publ->lower != seq->lower)
1034 if (publ->upper != seq->upper)
1036 tipc_nametbl_withdraw(publ->type, publ->lower,
1037 publ->ref, publ->key);
1042 if (list_empty(&p_ptr->publications))
1043 p_ptr->published = 0;
1044 tipc_port_unlock(p_ptr);
1048 int tipc_connect2port(u32 ref, struct tipc_portid const *peer)
1050 struct tipc_port *p_ptr;
1051 struct tipc_msg *msg;
1054 p_ptr = tipc_port_lock(ref);
1057 if (p_ptr->published || p_ptr->connected)
1063 msg_set_destnode(msg, peer->node);
1064 msg_set_destport(msg, peer->ref);
1065 msg_set_orignode(msg, tipc_own_addr);
1066 msg_set_origport(msg, p_ptr->ref);
1067 msg_set_type(msg, TIPC_CONN_MSG);
1068 msg_set_hdr_sz(msg, SHORT_H_SIZE);
1070 p_ptr->probing_interval = PROBING_INTERVAL;
1071 p_ptr->probing_state = CONFIRMED;
1072 p_ptr->connected = 1;
1073 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
1075 tipc_nodesub_subscribe(&p_ptr->subscription, peer->node,
1076 (void *)(unsigned long)ref,
1077 (net_ev_handler)port_handle_node_down);
1080 tipc_port_unlock(p_ptr);
1081 p_ptr->max_pkt = tipc_link_get_max_pkt(peer->node, ref);
1086 * tipc_disconnect_port - disconnect port from peer
1088 * Port must be locked.
1091 int tipc_disconnect_port(struct tipc_port *tp_ptr)
1095 if (tp_ptr->connected) {
1096 tp_ptr->connected = 0;
1097 /* let timer expire on it's own to avoid deadlock! */
1098 tipc_nodesub_unsubscribe(
1099 &((struct tipc_port *)tp_ptr)->subscription);
1108 * tipc_disconnect(): Disconnect port form peer.
1109 * This is a node local operation.
1112 int tipc_disconnect(u32 ref)
1114 struct tipc_port *p_ptr;
1117 p_ptr = tipc_port_lock(ref);
1120 res = tipc_disconnect_port((struct tipc_port *)p_ptr);
1121 tipc_port_unlock(p_ptr);
1126 * tipc_shutdown(): Send a SHUTDOWN msg to peer and disconnect
1128 int tipc_shutdown(u32 ref)
1130 struct tipc_port *p_ptr;
1131 struct sk_buff *buf = NULL;
1133 p_ptr = tipc_port_lock(ref);
1137 if (p_ptr->connected) {
1138 u32 imp = msg_importance(&p_ptr->phdr);
1139 if (imp < TIPC_CRITICAL_IMPORTANCE)
1141 buf = port_build_proto_msg(port_peerport(p_ptr),
1142 port_peernode(p_ptr),
1150 tipc_port_unlock(p_ptr);
1151 tipc_net_route_msg(buf);
1152 return tipc_disconnect(ref);
1156 * tipc_port_recv_sections(): Concatenate and deliver sectioned
1157 * message for this node.
1160 static int tipc_port_recv_sections(struct tipc_port *sender, unsigned int num_sect,
1161 struct iovec const *msg_sect)
1163 struct sk_buff *buf;
1166 res = tipc_msg_build(&sender->phdr, msg_sect, num_sect,
1167 MAX_MSG_SIZE, !sender->user_port, &buf);
1169 tipc_port_recv_msg(buf);
1174 * tipc_send - send message sections on connection
1177 int tipc_send(u32 ref, unsigned int num_sect, struct iovec const *msg_sect)
1179 struct tipc_port *p_ptr;
1183 p_ptr = tipc_port_deref(ref);
1184 if (!p_ptr || !p_ptr->connected)
1187 p_ptr->congested = 1;
1188 if (!tipc_port_congested(p_ptr)) {
1189 destnode = port_peernode(p_ptr);
1190 if (likely(destnode != tipc_own_addr))
1191 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
1194 res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
1196 if (likely(res != -ELINKCONG)) {
1197 p_ptr->congested = 0;
1203 if (port_unreliable(p_ptr)) {
1204 p_ptr->congested = 0;
1205 /* Just calculate msg length and return */
1206 return tipc_msg_calc_data_size(msg_sect, num_sect);
1212 * tipc_send2name - send message sections to port name
1215 int tipc_send2name(u32 ref, struct tipc_name const *name, unsigned int domain,
1216 unsigned int num_sect, struct iovec const *msg_sect)
1218 struct tipc_port *p_ptr;
1219 struct tipc_msg *msg;
1220 u32 destnode = domain;
1224 p_ptr = tipc_port_deref(ref);
1225 if (!p_ptr || p_ptr->connected)
1229 msg_set_type(msg, TIPC_NAMED_MSG);
1230 msg_set_orignode(msg, tipc_own_addr);
1231 msg_set_origport(msg, ref);
1232 msg_set_hdr_sz(msg, LONG_H_SIZE);
1233 msg_set_nametype(msg, name->type);
1234 msg_set_nameinst(msg, name->instance);
1235 msg_set_lookup_scope(msg, tipc_addr_scope(domain));
1236 destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
1237 msg_set_destnode(msg, destnode);
1238 msg_set_destport(msg, destport);
1240 if (likely(destport)) {
1241 if (likely(destnode == tipc_own_addr))
1242 res = tipc_port_recv_sections(p_ptr, num_sect,
1245 res = tipc_link_send_sections_fast(p_ptr, msg_sect,
1246 num_sect, destnode);
1247 if (likely(res != -ELINKCONG)) {
1252 if (port_unreliable(p_ptr)) {
1253 /* Just calculate msg length and return */
1254 return tipc_msg_calc_data_size(msg_sect, num_sect);
1258 return tipc_port_reject_sections(p_ptr, msg, msg_sect, num_sect,
1263 * tipc_send2port - send message sections to port identity
1266 int tipc_send2port(u32 ref, struct tipc_portid const *dest,
1267 unsigned int num_sect, struct iovec const *msg_sect)
1269 struct tipc_port *p_ptr;
1270 struct tipc_msg *msg;
1273 p_ptr = tipc_port_deref(ref);
1274 if (!p_ptr || p_ptr->connected)
1278 msg_set_type(msg, TIPC_DIRECT_MSG);
1279 msg_set_orignode(msg, tipc_own_addr);
1280 msg_set_origport(msg, ref);
1281 msg_set_destnode(msg, dest->node);
1282 msg_set_destport(msg, dest->ref);
1283 msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
1285 if (dest->node == tipc_own_addr)
1286 res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
1288 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
1290 if (likely(res != -ELINKCONG)) {
1295 if (port_unreliable(p_ptr)) {
1296 /* Just calculate msg length and return */
1297 return tipc_msg_calc_data_size(msg_sect, num_sect);
1303 * tipc_send_buf2port - send message buffer to port identity
1306 int tipc_send_buf2port(u32 ref, struct tipc_portid const *dest,
1307 struct sk_buff *buf, unsigned int dsz)
1309 struct tipc_port *p_ptr;
1310 struct tipc_msg *msg;
1313 p_ptr = (struct tipc_port *)tipc_ref_deref(ref);
1314 if (!p_ptr || p_ptr->connected)
1318 msg_set_type(msg, TIPC_DIRECT_MSG);
1319 msg_set_orignode(msg, tipc_own_addr);
1320 msg_set_origport(msg, ref);
1321 msg_set_destnode(msg, dest->node);
1322 msg_set_destport(msg, dest->ref);
1323 msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
1324 msg_set_size(msg, DIR_MSG_H_SIZE + dsz);
1325 if (skb_cow(buf, DIR_MSG_H_SIZE))
1328 skb_push(buf, DIR_MSG_H_SIZE);
1329 skb_copy_to_linear_data(buf, msg, DIR_MSG_H_SIZE);
1331 if (dest->node == tipc_own_addr)
1332 res = tipc_port_recv_msg(buf);
1334 res = tipc_send_buf_fast(buf, dest->node);
1335 if (likely(res != -ELINKCONG)) {
1340 if (port_unreliable(p_ptr))