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 native TIPC port
216 * Returns local port reference
219 u32 tipc_createport_raw(void *usr_handle,
220 u32 (*dispatcher)(struct tipc_port *, struct sk_buff *),
221 void (*wakeup)(struct tipc_port *),
222 const u32 importance)
225 struct tipc_msg *msg;
228 p_ptr = kzalloc(sizeof(*p_ptr), GFP_ATOMIC);
230 warn("Port creation failed, no memory\n");
233 ref = tipc_ref_acquire(p_ptr, &p_ptr->publ.lock);
235 warn("Port creation failed, reference table exhausted\n");
241 p_ptr->publ.usr_handle = usr_handle;
242 p_ptr->publ.max_pkt = MAX_PKT_DEFAULT;
243 p_ptr->publ.ref = ref;
244 msg = &p_ptr->publ.phdr;
245 msg_init(msg, DATA_LOW, TIPC_NAMED_MSG, TIPC_OK, LONG_H_SIZE, 0);
246 msg_set_orignode(msg, tipc_own_addr);
247 msg_set_prevnode(msg, tipc_own_addr);
248 msg_set_origport(msg, ref);
249 msg_set_importance(msg,importance);
250 p_ptr->last_in_seqno = 41;
252 INIT_LIST_HEAD(&p_ptr->wait_list);
253 INIT_LIST_HEAD(&p_ptr->subscription.nodesub_list);
254 p_ptr->congested_link = NULL;
255 p_ptr->dispatcher = dispatcher;
256 p_ptr->wakeup = wakeup;
257 p_ptr->user_port = NULL;
258 k_init_timer(&p_ptr->timer, (Handler)port_timeout, ref);
259 spin_lock_bh(&tipc_port_list_lock);
260 INIT_LIST_HEAD(&p_ptr->publications);
261 INIT_LIST_HEAD(&p_ptr->port_list);
262 list_add_tail(&p_ptr->port_list, &ports);
263 spin_unlock_bh(&tipc_port_list_lock);
264 tipc_port_unlock(p_ptr);
268 int tipc_deleteport(u32 ref)
271 struct sk_buff *buf = NULL;
273 tipc_withdraw(ref, 0, NULL);
274 p_ptr = tipc_port_lock(ref);
278 tipc_ref_discard(ref);
279 tipc_port_unlock(p_ptr);
281 k_cancel_timer(&p_ptr->timer);
282 if (p_ptr->publ.connected) {
283 buf = port_build_peer_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
284 tipc_nodesub_unsubscribe(&p_ptr->subscription);
286 if (p_ptr->user_port) {
287 tipc_reg_remove_port(p_ptr->user_port);
288 kfree(p_ptr->user_port);
291 spin_lock_bh(&tipc_port_list_lock);
292 list_del(&p_ptr->port_list);
293 list_del(&p_ptr->wait_list);
294 spin_unlock_bh(&tipc_port_list_lock);
295 k_term_timer(&p_ptr->timer);
297 dbg("Deleted port %u\n", ref);
298 tipc_net_route_msg(buf);
303 * tipc_get_port() - return port associated with 'ref'
305 * Note: Port is not locked.
308 struct tipc_port *tipc_get_port(const u32 ref)
310 return (struct tipc_port *)tipc_ref_deref(ref);
314 * tipc_get_handle - return user handle associated to port 'ref'
317 void *tipc_get_handle(const u32 ref)
322 p_ptr = tipc_port_lock(ref);
325 handle = p_ptr->publ.usr_handle;
326 tipc_port_unlock(p_ptr);
330 static int port_unreliable(struct port *p_ptr)
332 return msg_src_droppable(&p_ptr->publ.phdr);
335 int tipc_portunreliable(u32 ref, unsigned int *isunreliable)
339 p_ptr = tipc_port_lock(ref);
342 *isunreliable = port_unreliable(p_ptr);
343 tipc_port_unlock(p_ptr);
347 int tipc_set_portunreliable(u32 ref, unsigned int isunreliable)
351 p_ptr = tipc_port_lock(ref);
354 msg_set_src_droppable(&p_ptr->publ.phdr, (isunreliable != 0));
355 tipc_port_unlock(p_ptr);
359 static int port_unreturnable(struct port *p_ptr)
361 return msg_dest_droppable(&p_ptr->publ.phdr);
364 int tipc_portunreturnable(u32 ref, unsigned int *isunrejectable)
368 p_ptr = tipc_port_lock(ref);
371 *isunrejectable = port_unreturnable(p_ptr);
372 tipc_port_unlock(p_ptr);
376 int tipc_set_portunreturnable(u32 ref, unsigned int isunrejectable)
380 p_ptr = tipc_port_lock(ref);
383 msg_set_dest_droppable(&p_ptr->publ.phdr, (isunrejectable != 0));
384 tipc_port_unlock(p_ptr);
389 * port_build_proto_msg(): build a port level protocol
390 * or a connection abortion message. Called with
393 static struct sk_buff *port_build_proto_msg(u32 destport, u32 destnode,
394 u32 origport, u32 orignode,
395 u32 usr, u32 type, u32 err,
399 struct tipc_msg *msg;
401 buf = buf_acquire(LONG_H_SIZE);
404 msg_init(msg, usr, type, err, LONG_H_SIZE, destnode);
405 msg_set_destport(msg, destport);
406 msg_set_origport(msg, origport);
407 msg_set_destnode(msg, destnode);
408 msg_set_orignode(msg, orignode);
409 msg_set_transp_seqno(msg, seqno);
410 msg_set_msgcnt(msg, ack);
411 msg_dbg(msg, "PORT>SEND>:");
416 int tipc_reject_msg(struct sk_buff *buf, u32 err)
418 struct tipc_msg *msg = buf_msg(buf);
419 struct sk_buff *rbuf;
420 struct tipc_msg *rmsg;
422 u32 imp = msg_importance(msg);
423 u32 data_sz = msg_data_sz(msg);
425 if (data_sz > MAX_REJECT_SIZE)
426 data_sz = MAX_REJECT_SIZE;
427 if (msg_connected(msg) && (imp < TIPC_CRITICAL_IMPORTANCE))
429 msg_dbg(msg, "port->rej: ");
431 /* discard rejected message if it shouldn't be returned to sender */
432 if (msg_errcode(msg) || msg_dest_droppable(msg)) {
437 /* construct rejected message */
439 hdr_sz = MCAST_H_SIZE;
441 hdr_sz = LONG_H_SIZE;
442 rbuf = buf_acquire(data_sz + hdr_sz);
447 rmsg = buf_msg(rbuf);
448 msg_init(rmsg, imp, msg_type(msg), err, hdr_sz, msg_orignode(msg));
449 msg_set_destport(rmsg, msg_origport(msg));
450 msg_set_prevnode(rmsg, tipc_own_addr);
451 msg_set_origport(rmsg, msg_destport(msg));
453 msg_set_orignode(rmsg, tipc_own_addr);
455 msg_set_orignode(rmsg, msg_destnode(msg));
456 msg_set_size(rmsg, data_sz + hdr_sz);
457 msg_set_nametype(rmsg, msg_nametype(msg));
458 msg_set_nameinst(rmsg, msg_nameinst(msg));
459 skb_copy_to_linear_data_offset(rbuf, hdr_sz, msg_data(msg), data_sz);
461 /* send self-abort message when rejecting on a connected port */
462 if (msg_connected(msg)) {
463 struct sk_buff *abuf = NULL;
464 struct port *p_ptr = tipc_port_lock(msg_destport(msg));
467 if (p_ptr->publ.connected)
468 abuf = port_build_self_abort_msg(p_ptr, err);
469 tipc_port_unlock(p_ptr);
471 tipc_net_route_msg(abuf);
474 /* send rejected message */
476 tipc_net_route_msg(rbuf);
480 int tipc_port_reject_sections(struct port *p_ptr, struct tipc_msg *hdr,
481 struct iovec const *msg_sect, u32 num_sect,
487 res = msg_build(hdr, msg_sect, num_sect, MAX_MSG_SIZE,
488 !p_ptr->user_port, &buf);
492 return tipc_reject_msg(buf, err);
495 static void port_timeout(unsigned long ref)
497 struct port *p_ptr = tipc_port_lock(ref);
498 struct sk_buff *buf = NULL;
503 if (!p_ptr->publ.connected) {
504 tipc_port_unlock(p_ptr);
508 /* Last probe answered ? */
509 if (p_ptr->probing_state == PROBING) {
510 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
512 buf = port_build_proto_msg(port_peerport(p_ptr),
513 port_peernode(p_ptr),
519 port_out_seqno(p_ptr),
521 port_incr_out_seqno(p_ptr);
522 p_ptr->probing_state = PROBING;
523 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
525 tipc_port_unlock(p_ptr);
526 tipc_net_route_msg(buf);
530 static void port_handle_node_down(unsigned long ref)
532 struct port *p_ptr = tipc_port_lock(ref);
533 struct sk_buff* buf = NULL;
537 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_NODE);
538 tipc_port_unlock(p_ptr);
539 tipc_net_route_msg(buf);
543 static struct sk_buff *port_build_self_abort_msg(struct port *p_ptr, u32 err)
545 u32 imp = msg_importance(&p_ptr->publ.phdr);
547 if (!p_ptr->publ.connected)
549 if (imp < TIPC_CRITICAL_IMPORTANCE)
551 return port_build_proto_msg(p_ptr->publ.ref,
553 port_peerport(p_ptr),
554 port_peernode(p_ptr),
558 p_ptr->last_in_seqno + 1,
563 static struct sk_buff *port_build_peer_abort_msg(struct port *p_ptr, u32 err)
565 u32 imp = msg_importance(&p_ptr->publ.phdr);
567 if (!p_ptr->publ.connected)
569 if (imp < TIPC_CRITICAL_IMPORTANCE)
571 return port_build_proto_msg(port_peerport(p_ptr),
572 port_peernode(p_ptr),
578 port_out_seqno(p_ptr),
582 void tipc_port_recv_proto_msg(struct sk_buff *buf)
584 struct tipc_msg *msg = buf_msg(buf);
585 struct port *p_ptr = tipc_port_lock(msg_destport(msg));
587 struct sk_buff *r_buf = NULL;
588 struct sk_buff *abort_buf = NULL;
590 msg_dbg(msg, "PORT<RECV<:");
593 err = TIPC_ERR_NO_PORT;
594 } else if (p_ptr->publ.connected) {
595 if (port_peernode(p_ptr) != msg_orignode(msg))
596 err = TIPC_ERR_NO_PORT;
597 if (port_peerport(p_ptr) != msg_origport(msg))
598 err = TIPC_ERR_NO_PORT;
599 if (!err && msg_routed(msg)) {
600 u32 seqno = msg_transp_seqno(msg);
601 u32 myno = ++p_ptr->last_in_seqno;
603 err = TIPC_ERR_NO_PORT;
604 abort_buf = port_build_self_abort_msg(p_ptr, err);
607 if (msg_type(msg) == CONN_ACK) {
608 int wakeup = tipc_port_congested(p_ptr) &&
609 p_ptr->publ.congested &&
611 p_ptr->acked += msg_msgcnt(msg);
612 if (tipc_port_congested(p_ptr))
614 p_ptr->publ.congested = 0;
617 p_ptr->wakeup(&p_ptr->publ);
620 } else if (p_ptr->publ.published) {
621 err = TIPC_ERR_NO_PORT;
624 r_buf = port_build_proto_msg(msg_origport(msg),
637 if (msg_type(msg) == CONN_PROBE) {
638 r_buf = port_build_proto_msg(msg_origport(msg),
645 port_out_seqno(p_ptr),
648 p_ptr->probing_state = CONFIRMED;
649 port_incr_out_seqno(p_ptr);
652 tipc_port_unlock(p_ptr);
653 tipc_net_route_msg(r_buf);
654 tipc_net_route_msg(abort_buf);
658 static void port_print(struct port *p_ptr, struct print_buf *buf, int full_id)
660 struct publication *publ;
663 tipc_printf(buf, "<%u.%u.%u:%u>:",
664 tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
665 tipc_node(tipc_own_addr), p_ptr->publ.ref);
667 tipc_printf(buf, "%-10u:", p_ptr->publ.ref);
669 if (p_ptr->publ.connected) {
670 u32 dport = port_peerport(p_ptr);
671 u32 destnode = port_peernode(p_ptr);
673 tipc_printf(buf, " connected to <%u.%u.%u:%u>",
674 tipc_zone(destnode), tipc_cluster(destnode),
675 tipc_node(destnode), dport);
676 if (p_ptr->publ.conn_type != 0)
677 tipc_printf(buf, " via {%u,%u}",
678 p_ptr->publ.conn_type,
679 p_ptr->publ.conn_instance);
681 else if (p_ptr->publ.published) {
682 tipc_printf(buf, " bound to");
683 list_for_each_entry(publ, &p_ptr->publications, pport_list) {
684 if (publ->lower == publ->upper)
685 tipc_printf(buf, " {%u,%u}", publ->type,
688 tipc_printf(buf, " {%u,%u,%u}", publ->type,
689 publ->lower, publ->upper);
692 tipc_printf(buf, "\n");
695 #define MAX_PORT_QUERY 32768
697 struct sk_buff *tipc_port_get_ports(void)
700 struct tlv_desc *rep_tlv;
705 buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_PORT_QUERY));
708 rep_tlv = (struct tlv_desc *)buf->data;
710 tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), MAX_PORT_QUERY);
711 spin_lock_bh(&tipc_port_list_lock);
712 list_for_each_entry(p_ptr, &ports, port_list) {
713 spin_lock_bh(p_ptr->publ.lock);
714 port_print(p_ptr, &pb, 0);
715 spin_unlock_bh(p_ptr->publ.lock);
717 spin_unlock_bh(&tipc_port_list_lock);
718 str_len = tipc_printbuf_validate(&pb);
720 skb_put(buf, TLV_SPACE(str_len));
721 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
728 #define MAX_PORT_STATS 2000
730 struct sk_buff *port_show_stats(const void *req_tlv_area, int req_tlv_space)
735 struct tlv_desc *rep_tlv;
739 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_PORT_REF))
740 return cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
742 ref = *(u32 *)TLV_DATA(req_tlv_area);
745 p_ptr = tipc_port_lock(ref);
747 return cfg_reply_error_string("port not found");
749 buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_PORT_STATS));
751 tipc_port_unlock(p_ptr);
754 rep_tlv = (struct tlv_desc *)buf->data;
756 tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), MAX_PORT_STATS);
757 port_print(p_ptr, &pb, 1);
758 /* NEED TO FILL IN ADDITIONAL PORT STATISTICS HERE */
759 tipc_port_unlock(p_ptr);
760 str_len = tipc_printbuf_validate(&pb);
762 skb_put(buf, TLV_SPACE(str_len));
763 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
770 void tipc_port_reinit(void)
773 struct tipc_msg *msg;
775 spin_lock_bh(&tipc_port_list_lock);
776 list_for_each_entry(p_ptr, &ports, port_list) {
777 msg = &p_ptr->publ.phdr;
778 if (msg_orignode(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);
840 if (unlikely(!connected)) {
841 if (unlikely(published))
843 tipc_connect2port(dref,&orig);
845 if (unlikely(msg_origport(msg) != peer_port))
847 if (unlikely(msg_orignode(msg) != peer_node))
851 if (unlikely(++p_ptr->publ.conn_unacked >=
852 TIPC_FLOW_CONTROL_WIN))
853 tipc_acknowledge(dref,
854 p_ptr->publ.conn_unacked);
855 skb_pull(buf, msg_hdr_sz(msg));
856 cb(usr_handle, dref, &buf, msg_data(msg),
860 case TIPC_DIRECT_MSG:{
861 tipc_msg_event cb = up_ptr->msg_cb;
863 tipc_port_unlock(p_ptr);
864 if (unlikely(connected))
868 skb_pull(buf, msg_hdr_sz(msg));
869 cb(usr_handle, dref, &buf, msg_data(msg),
870 msg_data_sz(msg), msg_importance(msg),
875 case TIPC_NAMED_MSG:{
876 tipc_named_msg_event cb = up_ptr->named_msg_cb;
878 tipc_port_unlock(p_ptr);
879 if (unlikely(connected))
883 if (unlikely(!published))
885 dseq.type = msg_nametype(msg);
886 dseq.lower = msg_nameinst(msg);
887 dseq.upper = (message_type == TIPC_NAMED_MSG)
888 ? dseq.lower : msg_nameupper(msg);
889 skb_pull(buf, msg_hdr_sz(msg));
890 cb(usr_handle, dref, &buf, msg_data(msg),
891 msg_data_sz(msg), msg_importance(msg),
901 switch (message_type) {
904 tipc_conn_shutdown_event cb =
906 u32 peer_port = port_peerport(p_ptr);
907 u32 peer_node = port_peernode(p_ptr);
909 tipc_port_unlock(p_ptr);
910 if (!connected || !cb)
912 if (msg_origport(msg) != peer_port)
914 if (msg_orignode(msg) != peer_node)
916 tipc_disconnect(dref);
917 skb_pull(buf, msg_hdr_sz(msg));
918 cb(usr_handle, dref, &buf, msg_data(msg),
919 msg_data_sz(msg), msg_errcode(msg));
922 case TIPC_DIRECT_MSG:{
923 tipc_msg_err_event cb = up_ptr->err_cb;
925 tipc_port_unlock(p_ptr);
926 if (connected || !cb)
928 skb_pull(buf, msg_hdr_sz(msg));
929 cb(usr_handle, dref, &buf, msg_data(msg),
930 msg_data_sz(msg), msg_errcode(msg), &orig);
934 case TIPC_NAMED_MSG:{
935 tipc_named_msg_err_event cb =
936 up_ptr->named_err_cb;
938 tipc_port_unlock(p_ptr);
939 if (connected || !cb)
941 dseq.type = msg_nametype(msg);
942 dseq.lower = msg_nameinst(msg);
943 dseq.upper = (message_type == TIPC_NAMED_MSG)
944 ? dseq.lower : msg_nameupper(msg);
945 skb_pull(buf, msg_hdr_sz(msg));
946 cb(usr_handle, dref, &buf, msg_data(msg),
947 msg_data_sz(msg), msg_errcode(msg), &dseq);
956 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
962 * port_dispatcher(): Dispatcher for messages destinated
963 * to the tipc_port interface. Called with port locked.
966 static u32 port_dispatcher(struct tipc_port *dummy, struct sk_buff *buf)
969 spin_lock_bh(&queue_lock);
970 if (msg_queue_head) {
971 msg_queue_tail->next = buf;
972 msg_queue_tail = buf;
974 msg_queue_tail = msg_queue_head = buf;
975 tipc_k_signal((Handler)port_dispatcher_sigh, 0);
977 spin_unlock_bh(&queue_lock);
982 * Wake up port after congestion: Called with port locked,
986 static void port_wakeup_sh(unsigned long ref)
989 struct user_port *up_ptr;
990 tipc_continue_event cb = NULL;
993 p_ptr = tipc_port_lock(ref);
995 up_ptr = p_ptr->user_port;
997 cb = up_ptr->continue_event_cb;
998 uh = up_ptr->usr_handle;
1000 tipc_port_unlock(p_ptr);
1007 static void port_wakeup(struct tipc_port *p_ptr)
1009 tipc_k_signal((Handler)port_wakeup_sh, p_ptr->ref);
1012 void tipc_acknowledge(u32 ref, u32 ack)
1015 struct sk_buff *buf = NULL;
1017 p_ptr = tipc_port_lock(ref);
1020 if (p_ptr->publ.connected) {
1021 p_ptr->publ.conn_unacked -= ack;
1022 buf = port_build_proto_msg(port_peerport(p_ptr),
1023 port_peernode(p_ptr),
1029 port_out_seqno(p_ptr),
1032 tipc_port_unlock(p_ptr);
1033 tipc_net_route_msg(buf);
1037 * tipc_createport(): user level call. Will add port to
1038 * registry if non-zero user_ref.
1041 int tipc_createport(u32 user_ref,
1043 unsigned int importance,
1044 tipc_msg_err_event error_cb,
1045 tipc_named_msg_err_event named_error_cb,
1046 tipc_conn_shutdown_event conn_error_cb,
1047 tipc_msg_event msg_cb,
1048 tipc_named_msg_event named_msg_cb,
1049 tipc_conn_msg_event conn_msg_cb,
1050 tipc_continue_event continue_event_cb,/* May be zero */
1053 struct user_port *up_ptr;
1057 up_ptr = kmalloc(sizeof(*up_ptr), GFP_ATOMIC);
1059 warn("Port creation failed, no memory\n");
1062 ref = tipc_createport_raw(NULL, port_dispatcher, port_wakeup, importance);
1063 p_ptr = tipc_port_lock(ref);
1069 p_ptr->user_port = up_ptr;
1070 up_ptr->user_ref = user_ref;
1071 up_ptr->usr_handle = usr_handle;
1072 up_ptr->ref = p_ptr->publ.ref;
1073 up_ptr->err_cb = error_cb;
1074 up_ptr->named_err_cb = named_error_cb;
1075 up_ptr->conn_err_cb = conn_error_cb;
1076 up_ptr->msg_cb = msg_cb;
1077 up_ptr->named_msg_cb = named_msg_cb;
1078 up_ptr->conn_msg_cb = conn_msg_cb;
1079 up_ptr->continue_event_cb = continue_event_cb;
1080 INIT_LIST_HEAD(&up_ptr->uport_list);
1081 tipc_reg_add_port(up_ptr);
1082 *portref = p_ptr->publ.ref;
1083 dbg(" tipc_createport: %x with ref %u\n", p_ptr, p_ptr->publ.ref);
1084 tipc_port_unlock(p_ptr);
1088 int tipc_ownidentity(u32 ref, struct tipc_portid *id)
1091 id->node = tipc_own_addr;
1095 int tipc_portimportance(u32 ref, unsigned int *importance)
1099 p_ptr = tipc_port_lock(ref);
1102 *importance = (unsigned int)msg_importance(&p_ptr->publ.phdr);
1103 tipc_port_unlock(p_ptr);
1107 int tipc_set_portimportance(u32 ref, unsigned int imp)
1111 if (imp > TIPC_CRITICAL_IMPORTANCE)
1114 p_ptr = tipc_port_lock(ref);
1117 msg_set_importance(&p_ptr->publ.phdr, (u32)imp);
1118 tipc_port_unlock(p_ptr);
1123 int tipc_publish(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
1126 struct publication *publ;
1130 p_ptr = tipc_port_lock(ref);
1134 dbg("tipc_publ %u, p_ptr = %x, conn = %x, scope = %x, "
1135 "lower = %u, upper = %u\n",
1136 ref, p_ptr, p_ptr->publ.connected, scope, seq->lower, seq->upper);
1137 if (p_ptr->publ.connected)
1139 if (seq->lower > seq->upper)
1141 if ((scope < TIPC_ZONE_SCOPE) || (scope > TIPC_NODE_SCOPE))
1143 key = ref + p_ptr->pub_count + 1;
1148 publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
1149 scope, p_ptr->publ.ref, key);
1151 list_add(&publ->pport_list, &p_ptr->publications);
1153 p_ptr->publ.published = 1;
1157 tipc_port_unlock(p_ptr);
1161 int tipc_withdraw(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
1164 struct publication *publ;
1165 struct publication *tpubl;
1168 p_ptr = tipc_port_lock(ref);
1172 list_for_each_entry_safe(publ, tpubl,
1173 &p_ptr->publications, pport_list) {
1174 tipc_nametbl_withdraw(publ->type, publ->lower,
1175 publ->ref, publ->key);
1179 list_for_each_entry_safe(publ, tpubl,
1180 &p_ptr->publications, pport_list) {
1181 if (publ->scope != scope)
1183 if (publ->type != seq->type)
1185 if (publ->lower != seq->lower)
1187 if (publ->upper != seq->upper)
1189 tipc_nametbl_withdraw(publ->type, publ->lower,
1190 publ->ref, publ->key);
1195 if (list_empty(&p_ptr->publications))
1196 p_ptr->publ.published = 0;
1197 tipc_port_unlock(p_ptr);
1201 int tipc_connect2port(u32 ref, struct tipc_portid const *peer)
1204 struct tipc_msg *msg;
1207 p_ptr = tipc_port_lock(ref);
1210 if (p_ptr->publ.published || p_ptr->publ.connected)
1215 msg = &p_ptr->publ.phdr;
1216 msg_set_destnode(msg, peer->node);
1217 msg_set_destport(msg, peer->ref);
1218 msg_set_orignode(msg, tipc_own_addr);
1219 msg_set_origport(msg, p_ptr->publ.ref);
1220 msg_set_transp_seqno(msg, 42);
1221 msg_set_type(msg, TIPC_CONN_MSG);
1222 if (!may_route(peer->node))
1223 msg_set_hdr_sz(msg, SHORT_H_SIZE);
1225 msg_set_hdr_sz(msg, LONG_H_SIZE);
1227 p_ptr->probing_interval = PROBING_INTERVAL;
1228 p_ptr->probing_state = CONFIRMED;
1229 p_ptr->publ.connected = 1;
1230 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
1232 tipc_nodesub_subscribe(&p_ptr->subscription,peer->node,
1233 (void *)(unsigned long)ref,
1234 (net_ev_handler)port_handle_node_down);
1237 tipc_port_unlock(p_ptr);
1238 p_ptr->publ.max_pkt = tipc_link_get_max_pkt(peer->node, ref);
1243 * tipc_disconnect(): Disconnect port form peer.
1244 * This is a node local operation.
1247 int tipc_disconnect(u32 ref)
1250 int res = -ENOTCONN;
1252 p_ptr = tipc_port_lock(ref);
1255 if (p_ptr->publ.connected) {
1256 p_ptr->publ.connected = 0;
1257 /* let timer expire on it's own to avoid deadlock! */
1258 tipc_nodesub_unsubscribe(&p_ptr->subscription);
1261 tipc_port_unlock(p_ptr);
1266 * tipc_shutdown(): Send a SHUTDOWN msg to peer and disconnect
1268 int tipc_shutdown(u32 ref)
1271 struct sk_buff *buf = NULL;
1273 p_ptr = tipc_port_lock(ref);
1277 if (p_ptr->publ.connected) {
1278 u32 imp = msg_importance(&p_ptr->publ.phdr);
1279 if (imp < TIPC_CRITICAL_IMPORTANCE)
1281 buf = port_build_proto_msg(port_peerport(p_ptr),
1282 port_peernode(p_ptr),
1288 port_out_seqno(p_ptr),
1291 tipc_port_unlock(p_ptr);
1292 tipc_net_route_msg(buf);
1293 return tipc_disconnect(ref);
1296 int tipc_isconnected(u32 ref, int *isconnected)
1300 p_ptr = tipc_port_lock(ref);
1303 *isconnected = p_ptr->publ.connected;
1304 tipc_port_unlock(p_ptr);
1308 int tipc_peer(u32 ref, struct tipc_portid *peer)
1313 p_ptr = tipc_port_lock(ref);
1316 if (p_ptr->publ.connected) {
1317 peer->ref = port_peerport(p_ptr);
1318 peer->node = port_peernode(p_ptr);
1322 tipc_port_unlock(p_ptr);
1326 int tipc_ref_valid(u32 ref)
1328 /* Works irrespective of type */
1329 return !!tipc_ref_deref(ref);
1334 * tipc_port_recv_sections(): Concatenate and deliver sectioned
1335 * message for this node.
1338 int tipc_port_recv_sections(struct port *sender, unsigned int num_sect,
1339 struct iovec const *msg_sect)
1341 struct sk_buff *buf;
1344 res = msg_build(&sender->publ.phdr, msg_sect, num_sect,
1345 MAX_MSG_SIZE, !sender->user_port, &buf);
1347 tipc_port_recv_msg(buf);
1352 * tipc_send - send message sections on connection
1355 int tipc_send(u32 ref, unsigned int num_sect, struct iovec const *msg_sect)
1361 p_ptr = tipc_port_deref(ref);
1362 if (!p_ptr || !p_ptr->publ.connected)
1365 p_ptr->publ.congested = 1;
1366 if (!tipc_port_congested(p_ptr)) {
1367 destnode = port_peernode(p_ptr);
1368 if (likely(destnode != tipc_own_addr))
1369 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
1372 res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
1374 if (likely(res != -ELINKCONG)) {
1375 port_incr_out_seqno(p_ptr);
1376 p_ptr->publ.congested = 0;
1381 if (port_unreliable(p_ptr)) {
1382 p_ptr->publ.congested = 0;
1383 /* Just calculate msg length and return */
1384 return msg_calc_data_size(msg_sect, num_sect);
1390 * tipc_send_buf - send message buffer on connection
1393 int tipc_send_buf(u32 ref, struct sk_buff *buf, unsigned int dsz)
1396 struct tipc_msg *msg;
1402 p_ptr = tipc_port_deref(ref);
1403 if (!p_ptr || !p_ptr->publ.connected)
1406 msg = &p_ptr->publ.phdr;
1407 hsz = msg_hdr_sz(msg);
1409 msg_set_size(msg, sz);
1410 if (skb_cow(buf, hsz))
1414 skb_copy_to_linear_data(buf, msg, hsz);
1415 destnode = msg_destnode(msg);
1416 p_ptr->publ.congested = 1;
1417 if (!tipc_port_congested(p_ptr)) {
1418 if (likely(destnode != tipc_own_addr))
1419 res = tipc_send_buf_fast(buf, destnode);
1421 tipc_port_recv_msg(buf);
1424 if (likely(res != -ELINKCONG)) {
1425 port_incr_out_seqno(p_ptr);
1427 p_ptr->publ.congested = 0;
1431 if (port_unreliable(p_ptr)) {
1432 p_ptr->publ.congested = 0;
1439 * tipc_forward2name - forward message sections to port name
1442 int tipc_forward2name(u32 ref,
1443 struct tipc_name const *name,
1446 struct iovec const *msg_sect,
1447 struct tipc_portid const *orig,
1448 unsigned int importance)
1451 struct tipc_msg *msg;
1452 u32 destnode = domain;
1456 p_ptr = tipc_port_deref(ref);
1457 if (!p_ptr || p_ptr->publ.connected)
1460 msg = &p_ptr->publ.phdr;
1461 msg_set_type(msg, TIPC_NAMED_MSG);
1462 msg_set_orignode(msg, orig->node);
1463 msg_set_origport(msg, orig->ref);
1464 msg_set_hdr_sz(msg, LONG_H_SIZE);
1465 msg_set_nametype(msg, name->type);
1466 msg_set_nameinst(msg, name->instance);
1467 msg_set_lookup_scope(msg, addr_scope(domain));
1468 if (importance <= TIPC_CRITICAL_IMPORTANCE)
1469 msg_set_importance(msg,importance);
1470 destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
1471 msg_set_destnode(msg, destnode);
1472 msg_set_destport(msg, destport);
1474 if (likely(destport || destnode)) {
1476 if (likely(destnode == tipc_own_addr))
1477 return tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
1478 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
1480 if (likely(res != -ELINKCONG))
1482 if (port_unreliable(p_ptr)) {
1483 /* Just calculate msg length and return */
1484 return msg_calc_data_size(msg_sect, num_sect);
1488 return tipc_port_reject_sections(p_ptr, msg, msg_sect, num_sect,
1493 * tipc_send2name - send message sections to port name
1496 int tipc_send2name(u32 ref,
1497 struct tipc_name const *name,
1498 unsigned int domain,
1499 unsigned int num_sect,
1500 struct iovec const *msg_sect)
1502 struct tipc_portid orig;
1505 orig.node = tipc_own_addr;
1506 return tipc_forward2name(ref, name, domain, num_sect, msg_sect, &orig,
1507 TIPC_PORT_IMPORTANCE);
1511 * tipc_forward_buf2name - forward message buffer to port name
1514 int tipc_forward_buf2name(u32 ref,
1515 struct tipc_name const *name,
1517 struct sk_buff *buf,
1519 struct tipc_portid const *orig,
1520 unsigned int importance)
1523 struct tipc_msg *msg;
1524 u32 destnode = domain;
1528 p_ptr = (struct port *)tipc_ref_deref(ref);
1529 if (!p_ptr || p_ptr->publ.connected)
1532 msg = &p_ptr->publ.phdr;
1533 if (importance <= TIPC_CRITICAL_IMPORTANCE)
1534 msg_set_importance(msg, importance);
1535 msg_set_type(msg, TIPC_NAMED_MSG);
1536 msg_set_orignode(msg, orig->node);
1537 msg_set_origport(msg, orig->ref);
1538 msg_set_nametype(msg, name->type);
1539 msg_set_nameinst(msg, name->instance);
1540 msg_set_lookup_scope(msg, addr_scope(domain));
1541 msg_set_hdr_sz(msg, LONG_H_SIZE);
1542 msg_set_size(msg, LONG_H_SIZE + dsz);
1543 destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
1544 msg_set_destnode(msg, destnode);
1545 msg_set_destport(msg, destport);
1546 msg_dbg(msg, "forw2name ==> ");
1547 if (skb_cow(buf, LONG_H_SIZE))
1549 skb_push(buf, LONG_H_SIZE);
1550 skb_copy_to_linear_data(buf, msg, LONG_H_SIZE);
1551 msg_dbg(buf_msg(buf),"PREP:");
1552 if (likely(destport || destnode)) {
1554 if (destnode == tipc_own_addr)
1555 return tipc_port_recv_msg(buf);
1556 res = tipc_send_buf_fast(buf, destnode);
1557 if (likely(res != -ELINKCONG))
1559 if (port_unreliable(p_ptr))
1563 return tipc_reject_msg(buf, TIPC_ERR_NO_NAME);
1567 * tipc_send_buf2name - send message buffer to port name
1570 int tipc_send_buf2name(u32 ref,
1571 struct tipc_name const *dest,
1573 struct sk_buff *buf,
1576 struct tipc_portid orig;
1579 orig.node = tipc_own_addr;
1580 return tipc_forward_buf2name(ref, dest, domain, buf, dsz, &orig,
1581 TIPC_PORT_IMPORTANCE);
1585 * tipc_forward2port - forward message sections to port identity
1588 int tipc_forward2port(u32 ref,
1589 struct tipc_portid const *dest,
1590 unsigned int num_sect,
1591 struct iovec const *msg_sect,
1592 struct tipc_portid const *orig,
1593 unsigned int importance)
1596 struct tipc_msg *msg;
1599 p_ptr = tipc_port_deref(ref);
1600 if (!p_ptr || p_ptr->publ.connected)
1603 msg = &p_ptr->publ.phdr;
1604 msg_set_type(msg, TIPC_DIRECT_MSG);
1605 msg_set_orignode(msg, orig->node);
1606 msg_set_origport(msg, orig->ref);
1607 msg_set_destnode(msg, dest->node);
1608 msg_set_destport(msg, dest->ref);
1609 msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
1610 if (importance <= TIPC_CRITICAL_IMPORTANCE)
1611 msg_set_importance(msg, importance);
1613 if (dest->node == tipc_own_addr)
1614 return tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
1615 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect, dest->node);
1616 if (likely(res != -ELINKCONG))
1618 if (port_unreliable(p_ptr)) {
1619 /* Just calculate msg length and return */
1620 return msg_calc_data_size(msg_sect, num_sect);
1626 * tipc_send2port - send message sections to port identity
1629 int tipc_send2port(u32 ref,
1630 struct tipc_portid const *dest,
1631 unsigned int num_sect,
1632 struct iovec const *msg_sect)
1634 struct tipc_portid orig;
1637 orig.node = tipc_own_addr;
1638 return tipc_forward2port(ref, dest, num_sect, msg_sect, &orig,
1639 TIPC_PORT_IMPORTANCE);
1643 * tipc_forward_buf2port - forward message buffer to port identity
1645 int tipc_forward_buf2port(u32 ref,
1646 struct tipc_portid const *dest,
1647 struct sk_buff *buf,
1649 struct tipc_portid const *orig,
1650 unsigned int importance)
1653 struct tipc_msg *msg;
1656 p_ptr = (struct port *)tipc_ref_deref(ref);
1657 if (!p_ptr || p_ptr->publ.connected)
1660 msg = &p_ptr->publ.phdr;
1661 msg_set_type(msg, TIPC_DIRECT_MSG);
1662 msg_set_orignode(msg, orig->node);
1663 msg_set_origport(msg, orig->ref);
1664 msg_set_destnode(msg, dest->node);
1665 msg_set_destport(msg, dest->ref);
1666 msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
1667 if (importance <= TIPC_CRITICAL_IMPORTANCE)
1668 msg_set_importance(msg, importance);
1669 msg_set_size(msg, DIR_MSG_H_SIZE + dsz);
1670 if (skb_cow(buf, DIR_MSG_H_SIZE))
1673 skb_push(buf, DIR_MSG_H_SIZE);
1674 skb_copy_to_linear_data(buf, msg, DIR_MSG_H_SIZE);
1675 msg_dbg(msg, "buf2port: ");
1677 if (dest->node == tipc_own_addr)
1678 return tipc_port_recv_msg(buf);
1679 res = tipc_send_buf_fast(buf, dest->node);
1680 if (likely(res != -ELINKCONG))
1682 if (port_unreliable(p_ptr))
1688 * tipc_send_buf2port - send message buffer to port identity
1691 int tipc_send_buf2port(u32 ref,
1692 struct tipc_portid const *dest,
1693 struct sk_buff *buf,
1696 struct tipc_portid orig;
1699 orig.node = tipc_own_addr;
1700 return tipc_forward_buf2port(ref, dest, buf, dsz, &orig,
1701 TIPC_PORT_IMPORTANCE);