2 * net/tipc/port.c: TIPC port code
4 * Copyright (c) 1992-2007, Ericsson AB
5 * Copyright (c) 2004-2007, 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.
44 #include "name_table.h"
49 /* Connection management: */
50 #define PROBING_INTERVAL 3600000 /* [ms] => 1 h */
54 #define MAX_REJECT_SIZE 1024
56 static struct sk_buff *msg_queue_head = NULL;
57 static struct sk_buff *msg_queue_tail = NULL;
59 DEFINE_SPINLOCK(tipc_port_list_lock);
60 static DEFINE_SPINLOCK(queue_lock);
62 static LIST_HEAD(ports);
63 static void port_handle_node_down(unsigned long ref);
64 static struct sk_buff* port_build_self_abort_msg(struct port *,u32 err);
65 static struct sk_buff* port_build_peer_abort_msg(struct port *,u32 err);
66 static void port_timeout(unsigned long ref);
69 static u32 port_peernode(struct port *p_ptr)
71 return msg_destnode(&p_ptr->publ.phdr);
74 static u32 port_peerport(struct port *p_ptr)
76 return msg_destport(&p_ptr->publ.phdr);
79 static u32 port_out_seqno(struct port *p_ptr)
81 return msg_transp_seqno(&p_ptr->publ.phdr);
84 static void port_incr_out_seqno(struct port *p_ptr)
86 struct tipc_msg *m = &p_ptr->publ.phdr;
88 if (likely(!msg_routed(m)))
90 msg_set_transp_seqno(m, (msg_transp_seqno(m) + 1));
94 * tipc_multicast - send a multicast message to local and remote destinations
97 int tipc_multicast(u32 ref, struct tipc_name_seq const *seq, u32 domain,
98 u32 num_sect, struct iovec const *msg_sect)
100 struct tipc_msg *hdr;
102 struct sk_buff *ibuf = NULL;
103 struct port_list dports = {0, NULL, };
104 struct port *oport = tipc_port_deref(ref);
108 if (unlikely(!oport))
111 /* Create multicast message */
113 hdr = &oport->publ.phdr;
114 msg_set_type(hdr, TIPC_MCAST_MSG);
115 msg_set_nametype(hdr, seq->type);
116 msg_set_namelower(hdr, seq->lower);
117 msg_set_nameupper(hdr, seq->upper);
118 msg_set_hdr_sz(hdr, MCAST_H_SIZE);
119 res = msg_build(hdr, msg_sect, num_sect, MAX_MSG_SIZE,
120 !oport->user_port, &buf);
124 /* Figure out where to send multicast message */
126 ext_targets = tipc_nametbl_mc_translate(seq->type, seq->lower, seq->upper,
127 TIPC_NODE_SCOPE, &dports);
129 /* Send message to destinations (duplicate it only if necessary) */
132 if (dports.count != 0) {
133 ibuf = skb_copy(buf, GFP_ATOMIC);
135 tipc_port_list_free(&dports);
140 res = tipc_bclink_send_msg(buf);
141 if ((res < 0) && (dports.count != 0)) {
150 tipc_port_recv_mcast(ibuf, &dports);
152 tipc_port_list_free(&dports);
158 * tipc_port_recv_mcast - deliver multicast message to all destination ports
160 * If there is no port list, perform a lookup to create one
163 void tipc_port_recv_mcast(struct sk_buff *buf, struct port_list *dp)
165 struct tipc_msg* msg;
166 struct port_list dports = {0, NULL, };
167 struct port_list *item = dp;
172 /* Create destination port list, if one wasn't supplied */
175 tipc_nametbl_mc_translate(msg_nametype(msg),
183 /* Deliver a copy of message to each destination port */
185 if (dp->count != 0) {
186 if (dp->count == 1) {
187 msg_set_destport(msg, dp->ports[0]);
188 tipc_port_recv_msg(buf);
189 tipc_port_list_free(dp);
192 for (; cnt < dp->count; cnt++) {
193 int index = cnt % PLSIZE;
194 struct sk_buff *b = skb_clone(buf, GFP_ATOMIC);
197 warn("Unable to deliver multicast message(s)\n");
198 msg_dbg(msg, "LOST:");
201 if ((index == 0) && (cnt != 0)) {
204 msg_set_destport(buf_msg(b),item->ports[index]);
205 tipc_port_recv_msg(b);
210 tipc_port_list_free(dp);
214 * tipc_createport_raw - create a generic TIPC port
216 * Returns port reference, or 0 if unable to create it
218 * Note: The newly created port is returned in the locked state.
221 u32 tipc_createport_raw(void *usr_handle,
222 u32 (*dispatcher)(struct tipc_port *, struct sk_buff *),
223 void (*wakeup)(struct tipc_port *),
224 const u32 importance,
225 struct tipc_port **tp_ptr)
228 struct tipc_msg *msg;
231 p_ptr = kzalloc(sizeof(*p_ptr), GFP_ATOMIC);
233 warn("Port creation failed, no memory\n");
236 ref = tipc_ref_acquire(p_ptr, &p_ptr->publ.lock);
238 warn("Port creation failed, reference table exhausted\n");
243 p_ptr->publ.usr_handle = usr_handle;
244 p_ptr->publ.max_pkt = MAX_PKT_DEFAULT;
245 p_ptr->publ.ref = ref;
246 msg = &p_ptr->publ.phdr;
247 msg_init(msg, importance, TIPC_NAMED_MSG, LONG_H_SIZE, 0);
248 msg_set_origport(msg, ref);
249 p_ptr->last_in_seqno = 41;
251 INIT_LIST_HEAD(&p_ptr->wait_list);
252 INIT_LIST_HEAD(&p_ptr->subscription.nodesub_list);
253 p_ptr->congested_link = NULL;
254 p_ptr->dispatcher = dispatcher;
255 p_ptr->wakeup = wakeup;
256 p_ptr->user_port = NULL;
257 k_init_timer(&p_ptr->timer, (Handler)port_timeout, ref);
258 spin_lock_bh(&tipc_port_list_lock);
259 INIT_LIST_HEAD(&p_ptr->publications);
260 INIT_LIST_HEAD(&p_ptr->port_list);
261 list_add_tail(&p_ptr->port_list, &ports);
262 spin_unlock_bh(&tipc_port_list_lock);
263 *tp_ptr = &p_ptr->publ;
267 int tipc_deleteport(u32 ref)
270 struct sk_buff *buf = NULL;
272 tipc_withdraw(ref, 0, NULL);
273 p_ptr = tipc_port_lock(ref);
277 tipc_ref_discard(ref);
278 tipc_port_unlock(p_ptr);
280 k_cancel_timer(&p_ptr->timer);
281 if (p_ptr->publ.connected) {
282 buf = port_build_peer_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
283 tipc_nodesub_unsubscribe(&p_ptr->subscription);
285 if (p_ptr->user_port) {
286 tipc_reg_remove_port(p_ptr->user_port);
287 kfree(p_ptr->user_port);
290 spin_lock_bh(&tipc_port_list_lock);
291 list_del(&p_ptr->port_list);
292 list_del(&p_ptr->wait_list);
293 spin_unlock_bh(&tipc_port_list_lock);
294 k_term_timer(&p_ptr->timer);
296 dbg("Deleted port %u\n", ref);
297 tipc_net_route_msg(buf);
302 * tipc_get_port() - return port associated with 'ref'
304 * Note: Port is not locked.
307 struct tipc_port *tipc_get_port(const u32 ref)
309 return (struct tipc_port *)tipc_ref_deref(ref);
313 * tipc_get_handle - return user handle associated to port 'ref'
316 void *tipc_get_handle(const u32 ref)
321 p_ptr = tipc_port_lock(ref);
324 handle = p_ptr->publ.usr_handle;
325 tipc_port_unlock(p_ptr);
329 static int port_unreliable(struct port *p_ptr)
331 return msg_src_droppable(&p_ptr->publ.phdr);
334 int tipc_portunreliable(u32 ref, unsigned int *isunreliable)
338 p_ptr = tipc_port_lock(ref);
341 *isunreliable = port_unreliable(p_ptr);
342 tipc_port_unlock(p_ptr);
346 int tipc_set_portunreliable(u32 ref, unsigned int isunreliable)
350 p_ptr = tipc_port_lock(ref);
353 msg_set_src_droppable(&p_ptr->publ.phdr, (isunreliable != 0));
354 tipc_port_unlock(p_ptr);
358 static int port_unreturnable(struct port *p_ptr)
360 return msg_dest_droppable(&p_ptr->publ.phdr);
363 int tipc_portunreturnable(u32 ref, unsigned int *isunrejectable)
367 p_ptr = tipc_port_lock(ref);
370 *isunrejectable = port_unreturnable(p_ptr);
371 tipc_port_unlock(p_ptr);
375 int tipc_set_portunreturnable(u32 ref, unsigned int isunrejectable)
379 p_ptr = tipc_port_lock(ref);
382 msg_set_dest_droppable(&p_ptr->publ.phdr, (isunrejectable != 0));
383 tipc_port_unlock(p_ptr);
388 * port_build_proto_msg(): build a port level protocol
389 * or a connection abortion message. Called with
392 static struct sk_buff *port_build_proto_msg(u32 destport, u32 destnode,
393 u32 origport, u32 orignode,
394 u32 usr, u32 type, u32 err,
398 struct tipc_msg *msg;
400 buf = buf_acquire(LONG_H_SIZE);
403 msg_init(msg, usr, type, LONG_H_SIZE, destnode);
404 msg_set_errcode(msg, err);
405 msg_set_destport(msg, destport);
406 msg_set_origport(msg, origport);
407 msg_set_orignode(msg, orignode);
408 msg_set_transp_seqno(msg, seqno);
409 msg_set_msgcnt(msg, ack);
410 msg_dbg(msg, "PORT>SEND>:");
415 int tipc_reject_msg(struct sk_buff *buf, u32 err)
417 struct tipc_msg *msg = buf_msg(buf);
418 struct sk_buff *rbuf;
419 struct tipc_msg *rmsg;
421 u32 imp = msg_importance(msg);
422 u32 data_sz = msg_data_sz(msg);
424 if (data_sz > MAX_REJECT_SIZE)
425 data_sz = MAX_REJECT_SIZE;
426 if (msg_connected(msg) && (imp < TIPC_CRITICAL_IMPORTANCE))
428 msg_dbg(msg, "port->rej: ");
430 /* discard rejected message if it shouldn't be returned to sender */
431 if (msg_errcode(msg) || msg_dest_droppable(msg)) {
436 /* construct rejected message */
438 hdr_sz = MCAST_H_SIZE;
440 hdr_sz = LONG_H_SIZE;
441 rbuf = buf_acquire(data_sz + hdr_sz);
446 rmsg = buf_msg(rbuf);
447 msg_init(rmsg, imp, msg_type(msg), hdr_sz, msg_orignode(msg));
448 msg_set_errcode(rmsg, err);
449 msg_set_destport(rmsg, msg_origport(msg));
450 msg_set_origport(rmsg, msg_destport(msg));
452 msg_set_orignode(rmsg, tipc_own_addr);
454 msg_set_orignode(rmsg, msg_destnode(msg));
455 msg_set_size(rmsg, data_sz + hdr_sz);
456 msg_set_nametype(rmsg, msg_nametype(msg));
457 msg_set_nameinst(rmsg, msg_nameinst(msg));
458 skb_copy_to_linear_data_offset(rbuf, hdr_sz, msg_data(msg), data_sz);
460 /* send self-abort message when rejecting on a connected port */
461 if (msg_connected(msg)) {
462 struct sk_buff *abuf = NULL;
463 struct port *p_ptr = tipc_port_lock(msg_destport(msg));
466 if (p_ptr->publ.connected)
467 abuf = port_build_self_abort_msg(p_ptr, err);
468 tipc_port_unlock(p_ptr);
470 tipc_net_route_msg(abuf);
473 /* send rejected message */
475 tipc_net_route_msg(rbuf);
479 int tipc_port_reject_sections(struct port *p_ptr, struct tipc_msg *hdr,
480 struct iovec const *msg_sect, u32 num_sect,
486 res = msg_build(hdr, msg_sect, num_sect, MAX_MSG_SIZE,
487 !p_ptr->user_port, &buf);
491 return tipc_reject_msg(buf, err);
494 static void port_timeout(unsigned long ref)
496 struct port *p_ptr = tipc_port_lock(ref);
497 struct sk_buff *buf = NULL;
502 if (!p_ptr->publ.connected) {
503 tipc_port_unlock(p_ptr);
507 /* Last probe answered ? */
508 if (p_ptr->probing_state == PROBING) {
509 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
511 buf = port_build_proto_msg(port_peerport(p_ptr),
512 port_peernode(p_ptr),
518 port_out_seqno(p_ptr),
520 port_incr_out_seqno(p_ptr);
521 p_ptr->probing_state = PROBING;
522 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
524 tipc_port_unlock(p_ptr);
525 tipc_net_route_msg(buf);
529 static void port_handle_node_down(unsigned long ref)
531 struct port *p_ptr = tipc_port_lock(ref);
532 struct sk_buff* buf = NULL;
536 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_NODE);
537 tipc_port_unlock(p_ptr);
538 tipc_net_route_msg(buf);
542 static struct sk_buff *port_build_self_abort_msg(struct port *p_ptr, u32 err)
544 u32 imp = msg_importance(&p_ptr->publ.phdr);
546 if (!p_ptr->publ.connected)
548 if (imp < TIPC_CRITICAL_IMPORTANCE)
550 return port_build_proto_msg(p_ptr->publ.ref,
552 port_peerport(p_ptr),
553 port_peernode(p_ptr),
557 p_ptr->last_in_seqno + 1,
562 static struct sk_buff *port_build_peer_abort_msg(struct port *p_ptr, u32 err)
564 u32 imp = msg_importance(&p_ptr->publ.phdr);
566 if (!p_ptr->publ.connected)
568 if (imp < TIPC_CRITICAL_IMPORTANCE)
570 return port_build_proto_msg(port_peerport(p_ptr),
571 port_peernode(p_ptr),
577 port_out_seqno(p_ptr),
581 void tipc_port_recv_proto_msg(struct sk_buff *buf)
583 struct tipc_msg *msg = buf_msg(buf);
584 struct port *p_ptr = tipc_port_lock(msg_destport(msg));
586 struct sk_buff *r_buf = NULL;
587 struct sk_buff *abort_buf = NULL;
589 msg_dbg(msg, "PORT<RECV<:");
592 err = TIPC_ERR_NO_PORT;
593 } else if (p_ptr->publ.connected) {
594 if (port_peernode(p_ptr) != msg_orignode(msg))
595 err = TIPC_ERR_NO_PORT;
596 if (port_peerport(p_ptr) != msg_origport(msg))
597 err = TIPC_ERR_NO_PORT;
598 if (!err && msg_routed(msg)) {
599 u32 seqno = msg_transp_seqno(msg);
600 u32 myno = ++p_ptr->last_in_seqno;
602 err = TIPC_ERR_NO_PORT;
603 abort_buf = port_build_self_abort_msg(p_ptr, err);
606 if (msg_type(msg) == CONN_ACK) {
607 int wakeup = tipc_port_congested(p_ptr) &&
608 p_ptr->publ.congested &&
610 p_ptr->acked += msg_msgcnt(msg);
611 if (tipc_port_congested(p_ptr))
613 p_ptr->publ.congested = 0;
616 p_ptr->wakeup(&p_ptr->publ);
619 } else if (p_ptr->publ.published) {
620 err = TIPC_ERR_NO_PORT;
623 r_buf = port_build_proto_msg(msg_origport(msg),
627 TIPC_HIGH_IMPORTANCE,
636 if (msg_type(msg) == CONN_PROBE) {
637 r_buf = port_build_proto_msg(msg_origport(msg),
644 port_out_seqno(p_ptr),
647 p_ptr->probing_state = CONFIRMED;
648 port_incr_out_seqno(p_ptr);
651 tipc_port_unlock(p_ptr);
652 tipc_net_route_msg(r_buf);
653 tipc_net_route_msg(abort_buf);
657 static void port_print(struct port *p_ptr, struct print_buf *buf, int full_id)
659 struct publication *publ;
662 tipc_printf(buf, "<%u.%u.%u:%u>:",
663 tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
664 tipc_node(tipc_own_addr), p_ptr->publ.ref);
666 tipc_printf(buf, "%-10u:", p_ptr->publ.ref);
668 if (p_ptr->publ.connected) {
669 u32 dport = port_peerport(p_ptr);
670 u32 destnode = port_peernode(p_ptr);
672 tipc_printf(buf, " connected to <%u.%u.%u:%u>",
673 tipc_zone(destnode), tipc_cluster(destnode),
674 tipc_node(destnode), dport);
675 if (p_ptr->publ.conn_type != 0)
676 tipc_printf(buf, " via {%u,%u}",
677 p_ptr->publ.conn_type,
678 p_ptr->publ.conn_instance);
680 else if (p_ptr->publ.published) {
681 tipc_printf(buf, " bound to");
682 list_for_each_entry(publ, &p_ptr->publications, pport_list) {
683 if (publ->lower == publ->upper)
684 tipc_printf(buf, " {%u,%u}", publ->type,
687 tipc_printf(buf, " {%u,%u,%u}", publ->type,
688 publ->lower, publ->upper);
691 tipc_printf(buf, "\n");
694 #define MAX_PORT_QUERY 32768
696 struct sk_buff *tipc_port_get_ports(void)
699 struct tlv_desc *rep_tlv;
704 buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_PORT_QUERY));
707 rep_tlv = (struct tlv_desc *)buf->data;
709 tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), MAX_PORT_QUERY);
710 spin_lock_bh(&tipc_port_list_lock);
711 list_for_each_entry(p_ptr, &ports, port_list) {
712 spin_lock_bh(p_ptr->publ.lock);
713 port_print(p_ptr, &pb, 0);
714 spin_unlock_bh(p_ptr->publ.lock);
716 spin_unlock_bh(&tipc_port_list_lock);
717 str_len = tipc_printbuf_validate(&pb);
719 skb_put(buf, TLV_SPACE(str_len));
720 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
727 #define MAX_PORT_STATS 2000
729 struct sk_buff *port_show_stats(const void *req_tlv_area, int req_tlv_space)
734 struct tlv_desc *rep_tlv;
738 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_PORT_REF))
739 return cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
741 ref = *(u32 *)TLV_DATA(req_tlv_area);
744 p_ptr = tipc_port_lock(ref);
746 return cfg_reply_error_string("port not found");
748 buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_PORT_STATS));
750 tipc_port_unlock(p_ptr);
753 rep_tlv = (struct tlv_desc *)buf->data;
755 tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), MAX_PORT_STATS);
756 port_print(p_ptr, &pb, 1);
757 /* NEED TO FILL IN ADDITIONAL PORT STATISTICS HERE */
758 tipc_port_unlock(p_ptr);
759 str_len = tipc_printbuf_validate(&pb);
761 skb_put(buf, TLV_SPACE(str_len));
762 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
769 void tipc_port_reinit(void)
772 struct tipc_msg *msg;
774 spin_lock_bh(&tipc_port_list_lock);
775 list_for_each_entry(p_ptr, &ports, port_list) {
776 msg = &p_ptr->publ.phdr;
777 if (msg_orignode(msg) == tipc_own_addr)
779 msg_set_prevnode(msg, tipc_own_addr);
780 msg_set_orignode(msg, tipc_own_addr);
782 spin_unlock_bh(&tipc_port_list_lock);
787 * port_dispatcher_sigh(): Signal handler for messages destinated
788 * to the tipc_port interface.
791 static void port_dispatcher_sigh(void *dummy)
795 spin_lock_bh(&queue_lock);
796 buf = msg_queue_head;
797 msg_queue_head = NULL;
798 spin_unlock_bh(&queue_lock);
802 struct user_port *up_ptr;
803 struct tipc_portid orig;
804 struct tipc_name_seq dseq;
810 struct sk_buff *next = buf->next;
811 struct tipc_msg *msg = buf_msg(buf);
812 u32 dref = msg_destport(msg);
814 message_type = msg_type(msg);
815 if (message_type > TIPC_DIRECT_MSG)
816 goto reject; /* Unsupported message type */
818 p_ptr = tipc_port_lock(dref);
820 goto reject; /* Port deleted while msg in queue */
822 orig.ref = msg_origport(msg);
823 orig.node = msg_orignode(msg);
824 up_ptr = p_ptr->user_port;
825 usr_handle = up_ptr->usr_handle;
826 connected = p_ptr->publ.connected;
827 published = p_ptr->publ.published;
829 if (unlikely(msg_errcode(msg)))
832 switch (message_type) {
835 tipc_conn_msg_event cb = up_ptr->conn_msg_cb;
836 u32 peer_port = port_peerport(p_ptr);
837 u32 peer_node = port_peernode(p_ptr);
839 tipc_port_unlock(p_ptr);
842 if (unlikely(!connected)) {
843 if (tipc_connect2port(dref, &orig))
845 } else if ((msg_origport(msg) != peer_port) ||
846 (msg_orignode(msg) != peer_node))
848 if (unlikely(++p_ptr->publ.conn_unacked >=
849 TIPC_FLOW_CONTROL_WIN))
850 tipc_acknowledge(dref,
851 p_ptr->publ.conn_unacked);
852 skb_pull(buf, msg_hdr_sz(msg));
853 cb(usr_handle, dref, &buf, msg_data(msg),
857 case TIPC_DIRECT_MSG:{
858 tipc_msg_event cb = up_ptr->msg_cb;
860 tipc_port_unlock(p_ptr);
861 if (unlikely(!cb || connected))
863 skb_pull(buf, msg_hdr_sz(msg));
864 cb(usr_handle, dref, &buf, msg_data(msg),
865 msg_data_sz(msg), msg_importance(msg),
870 case TIPC_NAMED_MSG:{
871 tipc_named_msg_event cb = up_ptr->named_msg_cb;
873 tipc_port_unlock(p_ptr);
874 if (unlikely(!cb || connected || !published))
876 dseq.type = msg_nametype(msg);
877 dseq.lower = msg_nameinst(msg);
878 dseq.upper = (message_type == TIPC_NAMED_MSG)
879 ? dseq.lower : msg_nameupper(msg);
880 skb_pull(buf, msg_hdr_sz(msg));
881 cb(usr_handle, dref, &buf, msg_data(msg),
882 msg_data_sz(msg), msg_importance(msg),
892 switch (message_type) {
895 tipc_conn_shutdown_event cb =
897 u32 peer_port = port_peerport(p_ptr);
898 u32 peer_node = port_peernode(p_ptr);
900 tipc_port_unlock(p_ptr);
901 if (!cb || !connected)
903 if ((msg_origport(msg) != peer_port) ||
904 (msg_orignode(msg) != peer_node))
906 tipc_disconnect(dref);
907 skb_pull(buf, msg_hdr_sz(msg));
908 cb(usr_handle, dref, &buf, msg_data(msg),
909 msg_data_sz(msg), msg_errcode(msg));
912 case TIPC_DIRECT_MSG:{
913 tipc_msg_err_event cb = up_ptr->err_cb;
915 tipc_port_unlock(p_ptr);
916 if (!cb || connected)
918 skb_pull(buf, msg_hdr_sz(msg));
919 cb(usr_handle, dref, &buf, msg_data(msg),
920 msg_data_sz(msg), msg_errcode(msg), &orig);
924 case TIPC_NAMED_MSG:{
925 tipc_named_msg_err_event cb =
926 up_ptr->named_err_cb;
928 tipc_port_unlock(p_ptr);
929 if (!cb || connected)
931 dseq.type = msg_nametype(msg);
932 dseq.lower = msg_nameinst(msg);
933 dseq.upper = (message_type == TIPC_NAMED_MSG)
934 ? dseq.lower : msg_nameupper(msg);
935 skb_pull(buf, msg_hdr_sz(msg));
936 cb(usr_handle, dref, &buf, msg_data(msg),
937 msg_data_sz(msg), msg_errcode(msg), &dseq);
946 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
952 * port_dispatcher(): Dispatcher for messages destinated
953 * to the tipc_port interface. Called with port locked.
956 static u32 port_dispatcher(struct tipc_port *dummy, struct sk_buff *buf)
959 spin_lock_bh(&queue_lock);
960 if (msg_queue_head) {
961 msg_queue_tail->next = buf;
962 msg_queue_tail = buf;
964 msg_queue_tail = msg_queue_head = buf;
965 tipc_k_signal((Handler)port_dispatcher_sigh, 0);
967 spin_unlock_bh(&queue_lock);
972 * Wake up port after congestion: Called with port locked,
976 static void port_wakeup_sh(unsigned long ref)
979 struct user_port *up_ptr;
980 tipc_continue_event cb = NULL;
983 p_ptr = tipc_port_lock(ref);
985 up_ptr = p_ptr->user_port;
987 cb = up_ptr->continue_event_cb;
988 uh = up_ptr->usr_handle;
990 tipc_port_unlock(p_ptr);
997 static void port_wakeup(struct tipc_port *p_ptr)
999 tipc_k_signal((Handler)port_wakeup_sh, p_ptr->ref);
1002 void tipc_acknowledge(u32 ref, u32 ack)
1005 struct sk_buff *buf = NULL;
1007 p_ptr = tipc_port_lock(ref);
1010 if (p_ptr->publ.connected) {
1011 p_ptr->publ.conn_unacked -= ack;
1012 buf = port_build_proto_msg(port_peerport(p_ptr),
1013 port_peernode(p_ptr),
1019 port_out_seqno(p_ptr),
1022 tipc_port_unlock(p_ptr);
1023 tipc_net_route_msg(buf);
1027 * tipc_createport(): user level call. Will add port to
1028 * registry if non-zero user_ref.
1031 int tipc_createport(u32 user_ref,
1033 unsigned int importance,
1034 tipc_msg_err_event error_cb,
1035 tipc_named_msg_err_event named_error_cb,
1036 tipc_conn_shutdown_event conn_error_cb,
1037 tipc_msg_event msg_cb,
1038 tipc_named_msg_event named_msg_cb,
1039 tipc_conn_msg_event conn_msg_cb,
1040 tipc_continue_event continue_event_cb,/* May be zero */
1043 struct user_port *up_ptr;
1045 struct tipc_port *tp_ptr;
1048 up_ptr = kmalloc(sizeof(*up_ptr), GFP_ATOMIC);
1050 warn("Port creation failed, no memory\n");
1053 ref = tipc_createport_raw(NULL, port_dispatcher, port_wakeup,
1054 importance, &tp_ptr);
1059 p_ptr = (struct port *)tp_ptr;
1061 p_ptr->user_port = up_ptr;
1062 up_ptr->user_ref = user_ref;
1063 up_ptr->usr_handle = usr_handle;
1064 up_ptr->ref = p_ptr->publ.ref;
1065 up_ptr->err_cb = error_cb;
1066 up_ptr->named_err_cb = named_error_cb;
1067 up_ptr->conn_err_cb = conn_error_cb;
1068 up_ptr->msg_cb = msg_cb;
1069 up_ptr->named_msg_cb = named_msg_cb;
1070 up_ptr->conn_msg_cb = conn_msg_cb;
1071 up_ptr->continue_event_cb = continue_event_cb;
1072 INIT_LIST_HEAD(&up_ptr->uport_list);
1073 tipc_reg_add_port(up_ptr);
1074 *portref = p_ptr->publ.ref;
1075 dbg(" tipc_createport: %x with ref %u\n", p_ptr, p_ptr->publ.ref);
1076 tipc_port_unlock(p_ptr);
1080 int tipc_ownidentity(u32 ref, struct tipc_portid *id)
1083 id->node = tipc_own_addr;
1087 int tipc_portimportance(u32 ref, unsigned int *importance)
1091 p_ptr = tipc_port_lock(ref);
1094 *importance = (unsigned int)msg_importance(&p_ptr->publ.phdr);
1095 tipc_port_unlock(p_ptr);
1099 int tipc_set_portimportance(u32 ref, unsigned int imp)
1103 if (imp > TIPC_CRITICAL_IMPORTANCE)
1106 p_ptr = tipc_port_lock(ref);
1109 msg_set_importance(&p_ptr->publ.phdr, (u32)imp);
1110 tipc_port_unlock(p_ptr);
1115 int tipc_publish(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
1118 struct publication *publ;
1122 p_ptr = tipc_port_lock(ref);
1126 dbg("tipc_publ %u, p_ptr = %x, conn = %x, scope = %x, "
1127 "lower = %u, upper = %u\n",
1128 ref, p_ptr, p_ptr->publ.connected, scope, seq->lower, seq->upper);
1129 if (p_ptr->publ.connected)
1131 if (seq->lower > seq->upper)
1133 if ((scope < TIPC_ZONE_SCOPE) || (scope > TIPC_NODE_SCOPE))
1135 key = ref + p_ptr->pub_count + 1;
1140 publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
1141 scope, p_ptr->publ.ref, key);
1143 list_add(&publ->pport_list, &p_ptr->publications);
1145 p_ptr->publ.published = 1;
1149 tipc_port_unlock(p_ptr);
1153 int tipc_withdraw(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
1156 struct publication *publ;
1157 struct publication *tpubl;
1160 p_ptr = tipc_port_lock(ref);
1164 list_for_each_entry_safe(publ, tpubl,
1165 &p_ptr->publications, pport_list) {
1166 tipc_nametbl_withdraw(publ->type, publ->lower,
1167 publ->ref, publ->key);
1171 list_for_each_entry_safe(publ, tpubl,
1172 &p_ptr->publications, pport_list) {
1173 if (publ->scope != scope)
1175 if (publ->type != seq->type)
1177 if (publ->lower != seq->lower)
1179 if (publ->upper != seq->upper)
1181 tipc_nametbl_withdraw(publ->type, publ->lower,
1182 publ->ref, publ->key);
1187 if (list_empty(&p_ptr->publications))
1188 p_ptr->publ.published = 0;
1189 tipc_port_unlock(p_ptr);
1193 int tipc_connect2port(u32 ref, struct tipc_portid const *peer)
1196 struct tipc_msg *msg;
1199 p_ptr = tipc_port_lock(ref);
1202 if (p_ptr->publ.published || p_ptr->publ.connected)
1207 msg = &p_ptr->publ.phdr;
1208 msg_set_destnode(msg, peer->node);
1209 msg_set_destport(msg, peer->ref);
1210 msg_set_orignode(msg, tipc_own_addr);
1211 msg_set_origport(msg, p_ptr->publ.ref);
1212 msg_set_transp_seqno(msg, 42);
1213 msg_set_type(msg, TIPC_CONN_MSG);
1214 if (!may_route(peer->node))
1215 msg_set_hdr_sz(msg, SHORT_H_SIZE);
1217 msg_set_hdr_sz(msg, LONG_H_SIZE);
1219 p_ptr->probing_interval = PROBING_INTERVAL;
1220 p_ptr->probing_state = CONFIRMED;
1221 p_ptr->publ.connected = 1;
1222 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
1224 tipc_nodesub_subscribe(&p_ptr->subscription,peer->node,
1225 (void *)(unsigned long)ref,
1226 (net_ev_handler)port_handle_node_down);
1229 tipc_port_unlock(p_ptr);
1230 p_ptr->publ.max_pkt = tipc_link_get_max_pkt(peer->node, ref);
1235 * tipc_disconnect_port - disconnect port from peer
1237 * Port must be locked.
1240 int tipc_disconnect_port(struct tipc_port *tp_ptr)
1244 if (tp_ptr->connected) {
1245 tp_ptr->connected = 0;
1246 /* let timer expire on it's own to avoid deadlock! */
1247 tipc_nodesub_unsubscribe(
1248 &((struct port *)tp_ptr)->subscription);
1257 * tipc_disconnect(): Disconnect port form peer.
1258 * This is a node local operation.
1261 int tipc_disconnect(u32 ref)
1266 p_ptr = tipc_port_lock(ref);
1269 res = tipc_disconnect_port((struct tipc_port *)p_ptr);
1270 tipc_port_unlock(p_ptr);
1275 * tipc_shutdown(): Send a SHUTDOWN msg to peer and disconnect
1277 int tipc_shutdown(u32 ref)
1280 struct sk_buff *buf = NULL;
1282 p_ptr = tipc_port_lock(ref);
1286 if (p_ptr->publ.connected) {
1287 u32 imp = msg_importance(&p_ptr->publ.phdr);
1288 if (imp < TIPC_CRITICAL_IMPORTANCE)
1290 buf = port_build_proto_msg(port_peerport(p_ptr),
1291 port_peernode(p_ptr),
1297 port_out_seqno(p_ptr),
1300 tipc_port_unlock(p_ptr);
1301 tipc_net_route_msg(buf);
1302 return tipc_disconnect(ref);
1305 int tipc_isconnected(u32 ref, int *isconnected)
1309 p_ptr = tipc_port_lock(ref);
1312 *isconnected = p_ptr->publ.connected;
1313 tipc_port_unlock(p_ptr);
1317 int tipc_peer(u32 ref, struct tipc_portid *peer)
1322 p_ptr = tipc_port_lock(ref);
1325 if (p_ptr->publ.connected) {
1326 peer->ref = port_peerport(p_ptr);
1327 peer->node = port_peernode(p_ptr);
1331 tipc_port_unlock(p_ptr);
1335 int tipc_ref_valid(u32 ref)
1337 /* Works irrespective of type */
1338 return !!tipc_ref_deref(ref);
1343 * tipc_port_recv_sections(): Concatenate and deliver sectioned
1344 * message for this node.
1347 int tipc_port_recv_sections(struct port *sender, unsigned int num_sect,
1348 struct iovec const *msg_sect)
1350 struct sk_buff *buf;
1353 res = msg_build(&sender->publ.phdr, msg_sect, num_sect,
1354 MAX_MSG_SIZE, !sender->user_port, &buf);
1356 tipc_port_recv_msg(buf);
1361 * tipc_send - send message sections on connection
1364 int tipc_send(u32 ref, unsigned int num_sect, struct iovec const *msg_sect)
1370 p_ptr = tipc_port_deref(ref);
1371 if (!p_ptr || !p_ptr->publ.connected)
1374 p_ptr->publ.congested = 1;
1375 if (!tipc_port_congested(p_ptr)) {
1376 destnode = port_peernode(p_ptr);
1377 if (likely(destnode != tipc_own_addr))
1378 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
1381 res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
1383 if (likely(res != -ELINKCONG)) {
1384 port_incr_out_seqno(p_ptr);
1385 p_ptr->publ.congested = 0;
1390 if (port_unreliable(p_ptr)) {
1391 p_ptr->publ.congested = 0;
1392 /* Just calculate msg length and return */
1393 return msg_calc_data_size(msg_sect, num_sect);
1399 * tipc_send_buf - send message buffer on connection
1402 int tipc_send_buf(u32 ref, struct sk_buff *buf, unsigned int dsz)
1405 struct tipc_msg *msg;
1411 p_ptr = tipc_port_deref(ref);
1412 if (!p_ptr || !p_ptr->publ.connected)
1415 msg = &p_ptr->publ.phdr;
1416 hsz = msg_hdr_sz(msg);
1418 msg_set_size(msg, sz);
1419 if (skb_cow(buf, hsz))
1423 skb_copy_to_linear_data(buf, msg, hsz);
1424 destnode = msg_destnode(msg);
1425 p_ptr->publ.congested = 1;
1426 if (!tipc_port_congested(p_ptr)) {
1427 if (likely(destnode != tipc_own_addr))
1428 res = tipc_send_buf_fast(buf, destnode);
1430 tipc_port_recv_msg(buf);
1433 if (likely(res != -ELINKCONG)) {
1434 port_incr_out_seqno(p_ptr);
1436 p_ptr->publ.congested = 0;
1440 if (port_unreliable(p_ptr)) {
1441 p_ptr->publ.congested = 0;
1448 * tipc_forward2name - forward message sections to port name
1451 int tipc_forward2name(u32 ref,
1452 struct tipc_name const *name,
1455 struct iovec const *msg_sect,
1456 struct tipc_portid const *orig,
1457 unsigned int importance)
1460 struct tipc_msg *msg;
1461 u32 destnode = domain;
1465 p_ptr = tipc_port_deref(ref);
1466 if (!p_ptr || p_ptr->publ.connected)
1469 msg = &p_ptr->publ.phdr;
1470 msg_set_type(msg, TIPC_NAMED_MSG);
1471 msg_set_orignode(msg, orig->node);
1472 msg_set_origport(msg, orig->ref);
1473 msg_set_hdr_sz(msg, LONG_H_SIZE);
1474 msg_set_nametype(msg, name->type);
1475 msg_set_nameinst(msg, name->instance);
1476 msg_set_lookup_scope(msg, addr_scope(domain));
1477 if (importance <= TIPC_CRITICAL_IMPORTANCE)
1478 msg_set_importance(msg,importance);
1479 destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
1480 msg_set_destnode(msg, destnode);
1481 msg_set_destport(msg, destport);
1483 if (likely(destport || destnode)) {
1485 if (likely(destnode == tipc_own_addr))
1486 return tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
1487 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
1489 if (likely(res != -ELINKCONG))
1491 if (port_unreliable(p_ptr)) {
1492 /* Just calculate msg length and return */
1493 return msg_calc_data_size(msg_sect, num_sect);
1497 return tipc_port_reject_sections(p_ptr, msg, msg_sect, num_sect,
1502 * tipc_send2name - send message sections to port name
1505 int tipc_send2name(u32 ref,
1506 struct tipc_name const *name,
1507 unsigned int domain,
1508 unsigned int num_sect,
1509 struct iovec const *msg_sect)
1511 struct tipc_portid orig;
1514 orig.node = tipc_own_addr;
1515 return tipc_forward2name(ref, name, domain, num_sect, msg_sect, &orig,
1516 TIPC_PORT_IMPORTANCE);
1520 * tipc_forward_buf2name - forward message buffer to port name
1523 int tipc_forward_buf2name(u32 ref,
1524 struct tipc_name const *name,
1526 struct sk_buff *buf,
1528 struct tipc_portid const *orig,
1529 unsigned int importance)
1532 struct tipc_msg *msg;
1533 u32 destnode = domain;
1537 p_ptr = (struct port *)tipc_ref_deref(ref);
1538 if (!p_ptr || p_ptr->publ.connected)
1541 msg = &p_ptr->publ.phdr;
1542 if (importance <= TIPC_CRITICAL_IMPORTANCE)
1543 msg_set_importance(msg, importance);
1544 msg_set_type(msg, TIPC_NAMED_MSG);
1545 msg_set_orignode(msg, orig->node);
1546 msg_set_origport(msg, orig->ref);
1547 msg_set_nametype(msg, name->type);
1548 msg_set_nameinst(msg, name->instance);
1549 msg_set_lookup_scope(msg, addr_scope(domain));
1550 msg_set_hdr_sz(msg, LONG_H_SIZE);
1551 msg_set_size(msg, LONG_H_SIZE + dsz);
1552 destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
1553 msg_set_destnode(msg, destnode);
1554 msg_set_destport(msg, destport);
1555 msg_dbg(msg, "forw2name ==> ");
1556 if (skb_cow(buf, LONG_H_SIZE))
1558 skb_push(buf, LONG_H_SIZE);
1559 skb_copy_to_linear_data(buf, msg, LONG_H_SIZE);
1560 msg_dbg(buf_msg(buf),"PREP:");
1561 if (likely(destport || destnode)) {
1563 if (destnode == tipc_own_addr)
1564 return tipc_port_recv_msg(buf);
1565 res = tipc_send_buf_fast(buf, destnode);
1566 if (likely(res != -ELINKCONG))
1568 if (port_unreliable(p_ptr))
1572 return tipc_reject_msg(buf, TIPC_ERR_NO_NAME);
1576 * tipc_send_buf2name - send message buffer to port name
1579 int tipc_send_buf2name(u32 ref,
1580 struct tipc_name const *dest,
1582 struct sk_buff *buf,
1585 struct tipc_portid orig;
1588 orig.node = tipc_own_addr;
1589 return tipc_forward_buf2name(ref, dest, domain, buf, dsz, &orig,
1590 TIPC_PORT_IMPORTANCE);
1594 * tipc_forward2port - forward message sections to port identity
1597 int tipc_forward2port(u32 ref,
1598 struct tipc_portid const *dest,
1599 unsigned int num_sect,
1600 struct iovec const *msg_sect,
1601 struct tipc_portid const *orig,
1602 unsigned int importance)
1605 struct tipc_msg *msg;
1608 p_ptr = tipc_port_deref(ref);
1609 if (!p_ptr || p_ptr->publ.connected)
1612 msg = &p_ptr->publ.phdr;
1613 msg_set_type(msg, TIPC_DIRECT_MSG);
1614 msg_set_orignode(msg, orig->node);
1615 msg_set_origport(msg, orig->ref);
1616 msg_set_destnode(msg, dest->node);
1617 msg_set_destport(msg, dest->ref);
1618 msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
1619 if (importance <= TIPC_CRITICAL_IMPORTANCE)
1620 msg_set_importance(msg, importance);
1622 if (dest->node == tipc_own_addr)
1623 return tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
1624 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect, dest->node);
1625 if (likely(res != -ELINKCONG))
1627 if (port_unreliable(p_ptr)) {
1628 /* Just calculate msg length and return */
1629 return msg_calc_data_size(msg_sect, num_sect);
1635 * tipc_send2port - send message sections to port identity
1638 int tipc_send2port(u32 ref,
1639 struct tipc_portid const *dest,
1640 unsigned int num_sect,
1641 struct iovec const *msg_sect)
1643 struct tipc_portid orig;
1646 orig.node = tipc_own_addr;
1647 return tipc_forward2port(ref, dest, num_sect, msg_sect, &orig,
1648 TIPC_PORT_IMPORTANCE);
1652 * tipc_forward_buf2port - forward message buffer to port identity
1654 int tipc_forward_buf2port(u32 ref,
1655 struct tipc_portid const *dest,
1656 struct sk_buff *buf,
1658 struct tipc_portid const *orig,
1659 unsigned int importance)
1662 struct tipc_msg *msg;
1665 p_ptr = (struct port *)tipc_ref_deref(ref);
1666 if (!p_ptr || p_ptr->publ.connected)
1669 msg = &p_ptr->publ.phdr;
1670 msg_set_type(msg, TIPC_DIRECT_MSG);
1671 msg_set_orignode(msg, orig->node);
1672 msg_set_origport(msg, orig->ref);
1673 msg_set_destnode(msg, dest->node);
1674 msg_set_destport(msg, dest->ref);
1675 msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
1676 if (importance <= TIPC_CRITICAL_IMPORTANCE)
1677 msg_set_importance(msg, importance);
1678 msg_set_size(msg, DIR_MSG_H_SIZE + dsz);
1679 if (skb_cow(buf, DIR_MSG_H_SIZE))
1682 skb_push(buf, DIR_MSG_H_SIZE);
1683 skb_copy_to_linear_data(buf, msg, DIR_MSG_H_SIZE);
1684 msg_dbg(msg, "buf2port: ");
1686 if (dest->node == tipc_own_addr)
1687 return tipc_port_recv_msg(buf);
1688 res = tipc_send_buf_fast(buf, dest->node);
1689 if (likely(res != -ELINKCONG))
1691 if (port_unreliable(p_ptr))
1697 * tipc_send_buf2port - send message buffer to port identity
1700 int tipc_send_buf2port(u32 ref,
1701 struct tipc_portid const *dest,
1702 struct sk_buff *buf,
1705 struct tipc_portid orig;
1708 orig.node = tipc_own_addr;
1709 return tipc_forward_buf2port(ref, dest, buf, dsz, &orig,
1710 TIPC_PORT_IMPORTANCE);