2 * net/tipc/port.c: TIPC port code
4 * Copyright (c) 1992-2006, Ericsson AB
5 * Copyright (c) 2004-2005, 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.
45 #include "name_table.h"
50 /* Connection management: */
51 #define PROBING_INTERVAL 3600000 /* [ms] => 1 h */
55 #define MAX_REJECT_SIZE 1024
57 static struct sk_buff *msg_queue_head = NULL;
58 static struct sk_buff *msg_queue_tail = NULL;
60 DEFINE_SPINLOCK(tipc_port_list_lock);
61 static DEFINE_SPINLOCK(queue_lock);
63 static LIST_HEAD(ports);
64 static void port_handle_node_down(unsigned long ref);
65 static struct sk_buff* port_build_self_abort_msg(struct port *,u32 err);
66 static struct sk_buff* port_build_peer_abort_msg(struct port *,u32 err);
67 static void port_timeout(unsigned long ref);
70 static u32 port_peernode(struct port *p_ptr)
72 return msg_destnode(&p_ptr->publ.phdr);
75 static u32 port_peerport(struct port *p_ptr)
77 return msg_destport(&p_ptr->publ.phdr);
80 static u32 port_out_seqno(struct port *p_ptr)
82 return msg_transp_seqno(&p_ptr->publ.phdr);
85 static void port_incr_out_seqno(struct port *p_ptr)
87 struct tipc_msg *m = &p_ptr->publ.phdr;
89 if (likely(!msg_routed(m)))
91 msg_set_transp_seqno(m, (msg_transp_seqno(m) + 1));
95 * tipc_multicast - send a multicast message to local and remote destinations
98 int tipc_multicast(u32 ref, struct tipc_name_seq const *seq, u32 domain,
99 u32 num_sect, struct iovec const *msg_sect)
101 struct tipc_msg *hdr;
103 struct sk_buff *ibuf = NULL;
104 struct port_list dports = {0, NULL, };
105 struct port *oport = tipc_port_deref(ref);
109 if (unlikely(!oport))
112 /* Create multicast message */
114 hdr = &oport->publ.phdr;
115 msg_set_type(hdr, TIPC_MCAST_MSG);
116 msg_set_nametype(hdr, seq->type);
117 msg_set_namelower(hdr, seq->lower);
118 msg_set_nameupper(hdr, seq->upper);
119 msg_set_hdr_sz(hdr, MCAST_H_SIZE);
120 res = msg_build(hdr, msg_sect, num_sect, MAX_MSG_SIZE,
121 !oport->user_port, &buf);
125 /* Figure out where to send multicast message */
127 ext_targets = tipc_nametbl_mc_translate(seq->type, seq->lower, seq->upper,
128 TIPC_NODE_SCOPE, &dports);
130 /* Send message to destinations (duplicate it only if necessary) */
133 if (dports.count != 0) {
134 ibuf = skb_copy(buf, GFP_ATOMIC);
136 tipc_port_list_free(&dports);
141 res = tipc_bclink_send_msg(buf);
142 if ((res < 0) && (dports.count != 0)) {
151 tipc_port_recv_mcast(ibuf, &dports);
153 tipc_port_list_free(&dports);
159 * tipc_port_recv_mcast - deliver multicast message to all destination ports
161 * If there is no port list, perform a lookup to create one
164 void tipc_port_recv_mcast(struct sk_buff *buf, struct port_list *dp)
166 struct tipc_msg* msg;
167 struct port_list dports = {0, NULL, };
168 struct port_list *item = dp;
173 /* Create destination port list, if one wasn't supplied */
176 tipc_nametbl_mc_translate(msg_nametype(msg),
184 /* Deliver a copy of message to each destination port */
186 if (dp->count != 0) {
187 if (dp->count == 1) {
188 msg_set_destport(msg, dp->ports[0]);
189 tipc_port_recv_msg(buf);
190 tipc_port_list_free(dp);
193 for (; cnt < dp->count; cnt++) {
194 int index = cnt % PLSIZE;
195 struct sk_buff *b = skb_clone(buf, GFP_ATOMIC);
198 warn("Unable to deliver multicast message(s)\n");
199 msg_dbg(msg, "LOST:");
202 if ((index == 0) && (cnt != 0)) {
205 msg_set_destport(buf_msg(b),item->ports[index]);
206 tipc_port_recv_msg(b);
211 tipc_port_list_free(dp);
215 * tipc_createport_raw - create a native TIPC port
217 * Returns local port reference
220 u32 tipc_createport_raw(void *usr_handle,
221 u32 (*dispatcher)(struct tipc_port *, struct sk_buff *),
222 void (*wakeup)(struct tipc_port *),
223 const u32 importance)
226 struct tipc_msg *msg;
229 p_ptr = kzalloc(sizeof(*p_ptr), GFP_ATOMIC);
231 warn("Port creation failed, no memory\n");
234 ref = tipc_ref_acquire(p_ptr, &p_ptr->publ.lock);
236 warn("Port creation failed, reference table exhausted\n");
242 p_ptr->publ.ref = ref;
243 msg = &p_ptr->publ.phdr;
244 msg_init(msg, DATA_LOW, TIPC_NAMED_MSG, TIPC_OK, LONG_H_SIZE, 0);
245 msg_set_orignode(msg, tipc_own_addr);
246 msg_set_prevnode(msg, tipc_own_addr);
247 msg_set_origport(msg, ref);
248 msg_set_importance(msg,importance);
249 p_ptr->last_in_seqno = 41;
251 p_ptr->publ.usr_handle = usr_handle;
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->max_pkt = MAX_PKT_DEFAULT;
256 p_ptr->dispatcher = dispatcher;
257 p_ptr->wakeup = wakeup;
258 p_ptr->user_port = NULL;
259 k_init_timer(&p_ptr->timer, (Handler)port_timeout, ref);
260 spin_lock_bh(&tipc_port_list_lock);
261 INIT_LIST_HEAD(&p_ptr->publications);
262 INIT_LIST_HEAD(&p_ptr->port_list);
263 list_add_tail(&p_ptr->port_list, &ports);
264 spin_unlock_bh(&tipc_port_list_lock);
265 tipc_port_unlock(p_ptr);
269 int tipc_deleteport(u32 ref)
272 struct sk_buff *buf = NULL;
274 tipc_withdraw(ref, 0, NULL);
275 p_ptr = tipc_port_lock(ref);
279 tipc_ref_discard(ref);
280 tipc_port_unlock(p_ptr);
282 k_cancel_timer(&p_ptr->timer);
283 if (p_ptr->publ.connected) {
284 buf = port_build_peer_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
285 tipc_nodesub_unsubscribe(&p_ptr->subscription);
287 if (p_ptr->user_port) {
288 tipc_reg_remove_port(p_ptr->user_port);
289 kfree(p_ptr->user_port);
292 spin_lock_bh(&tipc_port_list_lock);
293 list_del(&p_ptr->port_list);
294 list_del(&p_ptr->wait_list);
295 spin_unlock_bh(&tipc_port_list_lock);
296 k_term_timer(&p_ptr->timer);
298 dbg("Deleted port %u\n", ref);
299 tipc_net_route_msg(buf);
304 * tipc_get_port() - return port associated with 'ref'
306 * Note: Port is not locked.
309 struct tipc_port *tipc_get_port(const u32 ref)
311 return (struct tipc_port *)tipc_ref_deref(ref);
315 * tipc_get_handle - return user handle associated to port 'ref'
318 void *tipc_get_handle(const u32 ref)
323 p_ptr = tipc_port_lock(ref);
326 handle = p_ptr->publ.usr_handle;
327 tipc_port_unlock(p_ptr);
331 static int port_unreliable(struct port *p_ptr)
333 return msg_src_droppable(&p_ptr->publ.phdr);
336 int tipc_portunreliable(u32 ref, unsigned int *isunreliable)
340 p_ptr = tipc_port_lock(ref);
343 *isunreliable = port_unreliable(p_ptr);
344 spin_unlock_bh(p_ptr->publ.lock);
348 int tipc_set_portunreliable(u32 ref, unsigned int isunreliable)
352 p_ptr = tipc_port_lock(ref);
355 msg_set_src_droppable(&p_ptr->publ.phdr, (isunreliable != 0));
356 tipc_port_unlock(p_ptr);
360 static int port_unreturnable(struct port *p_ptr)
362 return msg_dest_droppable(&p_ptr->publ.phdr);
365 int tipc_portunreturnable(u32 ref, unsigned int *isunrejectable)
369 p_ptr = tipc_port_lock(ref);
372 *isunrejectable = port_unreturnable(p_ptr);
373 spin_unlock_bh(p_ptr->publ.lock);
377 int tipc_set_portunreturnable(u32 ref, unsigned int isunrejectable)
381 p_ptr = tipc_port_lock(ref);
384 msg_set_dest_droppable(&p_ptr->publ.phdr, (isunrejectable != 0));
385 tipc_port_unlock(p_ptr);
390 * port_build_proto_msg(): build a port level protocol
391 * or a connection abortion message. Called with
394 static struct sk_buff *port_build_proto_msg(u32 destport, u32 destnode,
395 u32 origport, u32 orignode,
396 u32 usr, u32 type, u32 err,
400 struct tipc_msg *msg;
402 buf = buf_acquire(LONG_H_SIZE);
405 msg_init(msg, usr, type, err, LONG_H_SIZE, destnode);
406 msg_set_destport(msg, destport);
407 msg_set_origport(msg, origport);
408 msg_set_destnode(msg, destnode);
409 msg_set_orignode(msg, orignode);
410 msg_set_transp_seqno(msg, seqno);
411 msg_set_msgcnt(msg, ack);
412 msg_dbg(msg, "PORT>SEND>:");
417 int tipc_set_msg_option(struct tipc_port *tp_ptr, const char *opt, const u32 sz)
419 msg_expand(&tp_ptr->phdr, msg_destnode(&tp_ptr->phdr));
420 msg_set_options(&tp_ptr->phdr, opt, sz);
424 int tipc_reject_msg(struct sk_buff *buf, u32 err)
426 struct tipc_msg *msg = buf_msg(buf);
427 struct sk_buff *rbuf;
428 struct tipc_msg *rmsg;
430 u32 imp = msg_importance(msg);
431 u32 data_sz = msg_data_sz(msg);
433 if (data_sz > MAX_REJECT_SIZE)
434 data_sz = MAX_REJECT_SIZE;
435 if (msg_connected(msg) && (imp < TIPC_CRITICAL_IMPORTANCE))
437 msg_dbg(msg, "port->rej: ");
439 /* discard rejected message if it shouldn't be returned to sender */
440 if (msg_errcode(msg) || msg_dest_droppable(msg)) {
445 /* construct rejected message */
447 hdr_sz = MCAST_H_SIZE;
449 hdr_sz = LONG_H_SIZE;
450 rbuf = buf_acquire(data_sz + hdr_sz);
455 rmsg = buf_msg(rbuf);
456 msg_init(rmsg, imp, msg_type(msg), err, hdr_sz, msg_orignode(msg));
457 msg_set_destport(rmsg, msg_origport(msg));
458 msg_set_prevnode(rmsg, tipc_own_addr);
459 msg_set_origport(rmsg, msg_destport(msg));
461 msg_set_orignode(rmsg, tipc_own_addr);
463 msg_set_orignode(rmsg, msg_destnode(msg));
464 msg_set_size(rmsg, data_sz + hdr_sz);
465 msg_set_nametype(rmsg, msg_nametype(msg));
466 msg_set_nameinst(rmsg, msg_nameinst(msg));
467 skb_copy_to_linear_data_offset(rbuf, hdr_sz, msg_data(msg), data_sz);
469 /* send self-abort message when rejecting on a connected port */
470 if (msg_connected(msg)) {
471 struct sk_buff *abuf = NULL;
472 struct port *p_ptr = tipc_port_lock(msg_destport(msg));
475 if (p_ptr->publ.connected)
476 abuf = port_build_self_abort_msg(p_ptr, err);
477 tipc_port_unlock(p_ptr);
479 tipc_net_route_msg(abuf);
482 /* send rejected message */
484 tipc_net_route_msg(rbuf);
488 int tipc_port_reject_sections(struct port *p_ptr, struct tipc_msg *hdr,
489 struct iovec const *msg_sect, u32 num_sect,
495 res = msg_build(hdr, msg_sect, num_sect, MAX_MSG_SIZE,
496 !p_ptr->user_port, &buf);
500 return tipc_reject_msg(buf, err);
503 static void port_timeout(unsigned long ref)
505 struct port *p_ptr = tipc_port_lock(ref);
506 struct sk_buff *buf = NULL;
511 if (!p_ptr->publ.connected) {
512 tipc_port_unlock(p_ptr);
516 /* Last probe answered ? */
517 if (p_ptr->probing_state == PROBING) {
518 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
520 buf = port_build_proto_msg(port_peerport(p_ptr),
521 port_peernode(p_ptr),
527 port_out_seqno(p_ptr),
529 port_incr_out_seqno(p_ptr);
530 p_ptr->probing_state = PROBING;
531 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
533 tipc_port_unlock(p_ptr);
534 tipc_net_route_msg(buf);
538 static void port_handle_node_down(unsigned long ref)
540 struct port *p_ptr = tipc_port_lock(ref);
541 struct sk_buff* buf = NULL;
545 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_NODE);
546 tipc_port_unlock(p_ptr);
547 tipc_net_route_msg(buf);
551 static struct sk_buff *port_build_self_abort_msg(struct port *p_ptr, u32 err)
553 u32 imp = msg_importance(&p_ptr->publ.phdr);
555 if (!p_ptr->publ.connected)
557 if (imp < TIPC_CRITICAL_IMPORTANCE)
559 return port_build_proto_msg(p_ptr->publ.ref,
561 port_peerport(p_ptr),
562 port_peernode(p_ptr),
566 p_ptr->last_in_seqno + 1,
571 static struct sk_buff *port_build_peer_abort_msg(struct port *p_ptr, u32 err)
573 u32 imp = msg_importance(&p_ptr->publ.phdr);
575 if (!p_ptr->publ.connected)
577 if (imp < TIPC_CRITICAL_IMPORTANCE)
579 return port_build_proto_msg(port_peerport(p_ptr),
580 port_peernode(p_ptr),
586 port_out_seqno(p_ptr),
590 void tipc_port_recv_proto_msg(struct sk_buff *buf)
592 struct tipc_msg *msg = buf_msg(buf);
593 struct port *p_ptr = tipc_port_lock(msg_destport(msg));
595 struct sk_buff *r_buf = NULL;
596 struct sk_buff *abort_buf = NULL;
598 msg_dbg(msg, "PORT<RECV<:");
601 err = TIPC_ERR_NO_PORT;
602 } else if (p_ptr->publ.connected) {
603 if (port_peernode(p_ptr) != msg_orignode(msg))
604 err = TIPC_ERR_NO_PORT;
605 if (port_peerport(p_ptr) != msg_origport(msg))
606 err = TIPC_ERR_NO_PORT;
607 if (!err && msg_routed(msg)) {
608 u32 seqno = msg_transp_seqno(msg);
609 u32 myno = ++p_ptr->last_in_seqno;
611 err = TIPC_ERR_NO_PORT;
612 abort_buf = port_build_self_abort_msg(p_ptr, err);
615 if (msg_type(msg) == CONN_ACK) {
616 int wakeup = tipc_port_congested(p_ptr) &&
617 p_ptr->publ.congested &&
619 p_ptr->acked += msg_msgcnt(msg);
620 if (tipc_port_congested(p_ptr))
622 p_ptr->publ.congested = 0;
625 p_ptr->wakeup(&p_ptr->publ);
628 } else if (p_ptr->publ.published) {
629 err = TIPC_ERR_NO_PORT;
632 r_buf = port_build_proto_msg(msg_origport(msg),
645 if (msg_type(msg) == CONN_PROBE) {
646 r_buf = port_build_proto_msg(msg_origport(msg),
653 port_out_seqno(p_ptr),
656 p_ptr->probing_state = CONFIRMED;
657 port_incr_out_seqno(p_ptr);
660 tipc_port_unlock(p_ptr);
661 tipc_net_route_msg(r_buf);
662 tipc_net_route_msg(abort_buf);
666 static void port_print(struct port *p_ptr, struct print_buf *buf, int full_id)
668 struct publication *publ;
671 tipc_printf(buf, "<%u.%u.%u:%u>:",
672 tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
673 tipc_node(tipc_own_addr), p_ptr->publ.ref);
675 tipc_printf(buf, "%-10u:", p_ptr->publ.ref);
677 if (p_ptr->publ.connected) {
678 u32 dport = port_peerport(p_ptr);
679 u32 destnode = port_peernode(p_ptr);
681 tipc_printf(buf, " connected to <%u.%u.%u:%u>",
682 tipc_zone(destnode), tipc_cluster(destnode),
683 tipc_node(destnode), dport);
684 if (p_ptr->publ.conn_type != 0)
685 tipc_printf(buf, " via {%u,%u}",
686 p_ptr->publ.conn_type,
687 p_ptr->publ.conn_instance);
689 else if (p_ptr->publ.published) {
690 tipc_printf(buf, " bound to");
691 list_for_each_entry(publ, &p_ptr->publications, pport_list) {
692 if (publ->lower == publ->upper)
693 tipc_printf(buf, " {%u,%u}", publ->type,
696 tipc_printf(buf, " {%u,%u,%u}", publ->type,
697 publ->lower, publ->upper);
700 tipc_printf(buf, "\n");
703 #define MAX_PORT_QUERY 32768
705 struct sk_buff *tipc_port_get_ports(void)
708 struct tlv_desc *rep_tlv;
713 buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_PORT_QUERY));
716 rep_tlv = (struct tlv_desc *)buf->data;
718 tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), MAX_PORT_QUERY);
719 spin_lock_bh(&tipc_port_list_lock);
720 list_for_each_entry(p_ptr, &ports, port_list) {
721 spin_lock_bh(p_ptr->publ.lock);
722 port_print(p_ptr, &pb, 0);
723 spin_unlock_bh(p_ptr->publ.lock);
725 spin_unlock_bh(&tipc_port_list_lock);
726 str_len = tipc_printbuf_validate(&pb);
728 skb_put(buf, TLV_SPACE(str_len));
729 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
736 #define MAX_PORT_STATS 2000
738 struct sk_buff *port_show_stats(const void *req_tlv_area, int req_tlv_space)
743 struct tlv_desc *rep_tlv;
747 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_PORT_REF))
748 return cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
750 ref = *(u32 *)TLV_DATA(req_tlv_area);
753 p_ptr = tipc_port_lock(ref);
755 return cfg_reply_error_string("port not found");
757 buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_PORT_STATS));
759 tipc_port_unlock(p_ptr);
762 rep_tlv = (struct tlv_desc *)buf->data;
764 tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), MAX_PORT_STATS);
765 port_print(p_ptr, &pb, 1);
766 /* NEED TO FILL IN ADDITIONAL PORT STATISTICS HERE */
767 tipc_port_unlock(p_ptr);
768 str_len = tipc_printbuf_validate(&pb);
770 skb_put(buf, TLV_SPACE(str_len));
771 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
778 void tipc_port_reinit(void)
781 struct tipc_msg *msg;
783 spin_lock_bh(&tipc_port_list_lock);
784 list_for_each_entry(p_ptr, &ports, port_list) {
785 msg = &p_ptr->publ.phdr;
786 if (msg_orignode(msg) == tipc_own_addr)
788 msg_set_orignode(msg, tipc_own_addr);
790 spin_unlock_bh(&tipc_port_list_lock);
795 * port_dispatcher_sigh(): Signal handler for messages destinated
796 * to the tipc_port interface.
799 static void port_dispatcher_sigh(void *dummy)
803 spin_lock_bh(&queue_lock);
804 buf = msg_queue_head;
805 msg_queue_head = NULL;
806 spin_unlock_bh(&queue_lock);
810 struct user_port *up_ptr;
811 struct tipc_portid orig;
812 struct tipc_name_seq dseq;
818 struct sk_buff *next = buf->next;
819 struct tipc_msg *msg = buf_msg(buf);
820 u32 dref = msg_destport(msg);
822 message_type = msg_type(msg);
823 if (message_type > TIPC_DIRECT_MSG)
824 goto reject; /* Unsupported message type */
826 p_ptr = tipc_port_lock(dref);
828 goto reject; /* Port deleted while msg in queue */
830 orig.ref = msg_origport(msg);
831 orig.node = msg_orignode(msg);
832 up_ptr = p_ptr->user_port;
833 usr_handle = up_ptr->usr_handle;
834 connected = p_ptr->publ.connected;
835 published = p_ptr->publ.published;
837 if (unlikely(msg_errcode(msg)))
840 switch (message_type) {
843 tipc_conn_msg_event cb = up_ptr->conn_msg_cb;
844 u32 peer_port = port_peerport(p_ptr);
845 u32 peer_node = port_peernode(p_ptr);
847 spin_unlock_bh(p_ptr->publ.lock);
848 if (unlikely(!connected)) {
849 if (unlikely(published))
851 tipc_connect2port(dref,&orig);
853 if (unlikely(msg_origport(msg) != peer_port))
855 if (unlikely(msg_orignode(msg) != peer_node))
859 if (unlikely(++p_ptr->publ.conn_unacked >=
860 TIPC_FLOW_CONTROL_WIN))
861 tipc_acknowledge(dref,
862 p_ptr->publ.conn_unacked);
863 skb_pull(buf, msg_hdr_sz(msg));
864 cb(usr_handle, dref, &buf, msg_data(msg),
868 case TIPC_DIRECT_MSG:{
869 tipc_msg_event cb = up_ptr->msg_cb;
871 spin_unlock_bh(p_ptr->publ.lock);
872 if (unlikely(connected))
876 skb_pull(buf, msg_hdr_sz(msg));
877 cb(usr_handle, dref, &buf, msg_data(msg),
878 msg_data_sz(msg), msg_importance(msg),
883 case TIPC_NAMED_MSG:{
884 tipc_named_msg_event cb = up_ptr->named_msg_cb;
886 spin_unlock_bh(p_ptr->publ.lock);
887 if (unlikely(connected))
891 if (unlikely(!published))
893 dseq.type = msg_nametype(msg);
894 dseq.lower = msg_nameinst(msg);
895 dseq.upper = (message_type == TIPC_NAMED_MSG)
896 ? dseq.lower : msg_nameupper(msg);
897 skb_pull(buf, msg_hdr_sz(msg));
898 cb(usr_handle, dref, &buf, msg_data(msg),
899 msg_data_sz(msg), msg_importance(msg),
909 switch (message_type) {
912 tipc_conn_shutdown_event cb =
914 u32 peer_port = port_peerport(p_ptr);
915 u32 peer_node = port_peernode(p_ptr);
917 spin_unlock_bh(p_ptr->publ.lock);
918 if (!connected || !cb)
920 if (msg_origport(msg) != peer_port)
922 if (msg_orignode(msg) != peer_node)
924 tipc_disconnect(dref);
925 skb_pull(buf, msg_hdr_sz(msg));
926 cb(usr_handle, dref, &buf, msg_data(msg),
927 msg_data_sz(msg), msg_errcode(msg));
930 case TIPC_DIRECT_MSG:{
931 tipc_msg_err_event cb = up_ptr->err_cb;
933 spin_unlock_bh(p_ptr->publ.lock);
934 if (connected || !cb)
936 skb_pull(buf, msg_hdr_sz(msg));
937 cb(usr_handle, dref, &buf, msg_data(msg),
938 msg_data_sz(msg), msg_errcode(msg), &orig);
942 case TIPC_NAMED_MSG:{
943 tipc_named_msg_err_event cb =
944 up_ptr->named_err_cb;
946 spin_unlock_bh(p_ptr->publ.lock);
947 if (connected || !cb)
949 dseq.type = msg_nametype(msg);
950 dseq.lower = msg_nameinst(msg);
951 dseq.upper = (message_type == TIPC_NAMED_MSG)
952 ? dseq.lower : msg_nameupper(msg);
953 skb_pull(buf, msg_hdr_sz(msg));
954 cb(usr_handle, dref, &buf, msg_data(msg),
955 msg_data_sz(msg), msg_errcode(msg), &dseq);
964 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
970 * port_dispatcher(): Dispatcher for messages destinated
971 * to the tipc_port interface. Called with port locked.
974 static u32 port_dispatcher(struct tipc_port *dummy, struct sk_buff *buf)
977 spin_lock_bh(&queue_lock);
978 if (msg_queue_head) {
979 msg_queue_tail->next = buf;
980 msg_queue_tail = buf;
982 msg_queue_tail = msg_queue_head = buf;
983 tipc_k_signal((Handler)port_dispatcher_sigh, 0);
985 spin_unlock_bh(&queue_lock);
990 * Wake up port after congestion: Called with port locked,
994 static void port_wakeup_sh(unsigned long ref)
997 struct user_port *up_ptr;
998 tipc_continue_event cb = NULL;
1001 p_ptr = tipc_port_lock(ref);
1003 up_ptr = p_ptr->user_port;
1005 cb = up_ptr->continue_event_cb;
1006 uh = up_ptr->usr_handle;
1008 tipc_port_unlock(p_ptr);
1015 static void port_wakeup(struct tipc_port *p_ptr)
1017 tipc_k_signal((Handler)port_wakeup_sh, p_ptr->ref);
1020 void tipc_acknowledge(u32 ref, u32 ack)
1023 struct sk_buff *buf = NULL;
1025 p_ptr = tipc_port_lock(ref);
1028 if (p_ptr->publ.connected) {
1029 p_ptr->publ.conn_unacked -= ack;
1030 buf = port_build_proto_msg(port_peerport(p_ptr),
1031 port_peernode(p_ptr),
1037 port_out_seqno(p_ptr),
1040 tipc_port_unlock(p_ptr);
1041 tipc_net_route_msg(buf);
1045 * tipc_createport(): user level call. Will add port to
1046 * registry if non-zero user_ref.
1049 int tipc_createport(u32 user_ref,
1051 unsigned int importance,
1052 tipc_msg_err_event error_cb,
1053 tipc_named_msg_err_event named_error_cb,
1054 tipc_conn_shutdown_event conn_error_cb,
1055 tipc_msg_event msg_cb,
1056 tipc_named_msg_event named_msg_cb,
1057 tipc_conn_msg_event conn_msg_cb,
1058 tipc_continue_event continue_event_cb,/* May be zero */
1061 struct user_port *up_ptr;
1065 up_ptr = kmalloc(sizeof(*up_ptr), GFP_ATOMIC);
1067 warn("Port creation failed, no memory\n");
1070 ref = tipc_createport_raw(NULL, port_dispatcher, port_wakeup, importance);
1071 p_ptr = tipc_port_lock(ref);
1077 p_ptr->user_port = up_ptr;
1078 up_ptr->user_ref = user_ref;
1079 up_ptr->usr_handle = usr_handle;
1080 up_ptr->ref = p_ptr->publ.ref;
1081 up_ptr->err_cb = error_cb;
1082 up_ptr->named_err_cb = named_error_cb;
1083 up_ptr->conn_err_cb = conn_error_cb;
1084 up_ptr->msg_cb = msg_cb;
1085 up_ptr->named_msg_cb = named_msg_cb;
1086 up_ptr->conn_msg_cb = conn_msg_cb;
1087 up_ptr->continue_event_cb = continue_event_cb;
1088 INIT_LIST_HEAD(&up_ptr->uport_list);
1089 tipc_reg_add_port(up_ptr);
1090 *portref = p_ptr->publ.ref;
1091 dbg(" tipc_createport: %x with ref %u\n", p_ptr, p_ptr->publ.ref);
1092 tipc_port_unlock(p_ptr);
1096 int tipc_ownidentity(u32 ref, struct tipc_portid *id)
1099 id->node = tipc_own_addr;
1103 int tipc_portimportance(u32 ref, unsigned int *importance)
1107 p_ptr = tipc_port_lock(ref);
1110 *importance = (unsigned int)msg_importance(&p_ptr->publ.phdr);
1111 spin_unlock_bh(p_ptr->publ.lock);
1115 int tipc_set_portimportance(u32 ref, unsigned int imp)
1119 if (imp > TIPC_CRITICAL_IMPORTANCE)
1122 p_ptr = tipc_port_lock(ref);
1125 msg_set_importance(&p_ptr->publ.phdr, (u32)imp);
1126 spin_unlock_bh(p_ptr->publ.lock);
1131 int tipc_publish(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
1134 struct publication *publ;
1138 p_ptr = tipc_port_lock(ref);
1142 dbg("tipc_publ %u, p_ptr = %x, conn = %x, scope = %x, "
1143 "lower = %u, upper = %u\n",
1144 ref, p_ptr, p_ptr->publ.connected, scope, seq->lower, seq->upper);
1145 if (p_ptr->publ.connected)
1147 if (seq->lower > seq->upper)
1149 if ((scope < TIPC_ZONE_SCOPE) || (scope > TIPC_NODE_SCOPE))
1151 key = ref + p_ptr->pub_count + 1;
1156 publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
1157 scope, p_ptr->publ.ref, key);
1159 list_add(&publ->pport_list, &p_ptr->publications);
1161 p_ptr->publ.published = 1;
1165 tipc_port_unlock(p_ptr);
1169 int tipc_withdraw(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
1172 struct publication *publ;
1173 struct publication *tpubl;
1176 p_ptr = tipc_port_lock(ref);
1180 list_for_each_entry_safe(publ, tpubl,
1181 &p_ptr->publications, pport_list) {
1182 tipc_nametbl_withdraw(publ->type, publ->lower,
1183 publ->ref, publ->key);
1187 list_for_each_entry_safe(publ, tpubl,
1188 &p_ptr->publications, pport_list) {
1189 if (publ->scope != scope)
1191 if (publ->type != seq->type)
1193 if (publ->lower != seq->lower)
1195 if (publ->upper != seq->upper)
1197 tipc_nametbl_withdraw(publ->type, publ->lower,
1198 publ->ref, publ->key);
1203 if (list_empty(&p_ptr->publications))
1204 p_ptr->publ.published = 0;
1205 tipc_port_unlock(p_ptr);
1209 int tipc_connect2port(u32 ref, struct tipc_portid const *peer)
1212 struct tipc_msg *msg;
1215 p_ptr = tipc_port_lock(ref);
1218 if (p_ptr->publ.published || p_ptr->publ.connected)
1223 msg = &p_ptr->publ.phdr;
1224 msg_set_destnode(msg, peer->node);
1225 msg_set_destport(msg, peer->ref);
1226 msg_set_orignode(msg, tipc_own_addr);
1227 msg_set_origport(msg, p_ptr->publ.ref);
1228 msg_set_transp_seqno(msg, 42);
1229 msg_set_type(msg, TIPC_CONN_MSG);
1230 if (!may_route(peer->node))
1231 msg_set_hdr_sz(msg, SHORT_H_SIZE);
1233 msg_set_hdr_sz(msg, LONG_H_SIZE);
1235 p_ptr->probing_interval = PROBING_INTERVAL;
1236 p_ptr->probing_state = CONFIRMED;
1237 p_ptr->publ.connected = 1;
1238 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
1240 tipc_nodesub_subscribe(&p_ptr->subscription,peer->node,
1241 (void *)(unsigned long)ref,
1242 (net_ev_handler)port_handle_node_down);
1245 tipc_port_unlock(p_ptr);
1246 p_ptr->max_pkt = tipc_link_get_max_pkt(peer->node, ref);
1251 * tipc_disconnect(): Disconnect port form peer.
1252 * This is a node local operation.
1255 int tipc_disconnect(u32 ref)
1258 int res = -ENOTCONN;
1260 p_ptr = tipc_port_lock(ref);
1263 if (p_ptr->publ.connected) {
1264 p_ptr->publ.connected = 0;
1265 /* let timer expire on it's own to avoid deadlock! */
1266 tipc_nodesub_unsubscribe(&p_ptr->subscription);
1269 tipc_port_unlock(p_ptr);
1274 * tipc_shutdown(): Send a SHUTDOWN msg to peer and disconnect
1276 int tipc_shutdown(u32 ref)
1279 struct sk_buff *buf = NULL;
1281 p_ptr = tipc_port_lock(ref);
1285 if (p_ptr->publ.connected) {
1286 u32 imp = msg_importance(&p_ptr->publ.phdr);
1287 if (imp < TIPC_CRITICAL_IMPORTANCE)
1289 buf = port_build_proto_msg(port_peerport(p_ptr),
1290 port_peernode(p_ptr),
1296 port_out_seqno(p_ptr),
1299 tipc_port_unlock(p_ptr);
1300 tipc_net_route_msg(buf);
1301 return tipc_disconnect(ref);
1304 int tipc_isconnected(u32 ref, int *isconnected)
1308 p_ptr = tipc_port_lock(ref);
1311 *isconnected = p_ptr->publ.connected;
1312 tipc_port_unlock(p_ptr);
1316 int tipc_peer(u32 ref, struct tipc_portid *peer)
1321 p_ptr = tipc_port_lock(ref);
1324 if (p_ptr->publ.connected) {
1325 peer->ref = port_peerport(p_ptr);
1326 peer->node = port_peernode(p_ptr);
1330 tipc_port_unlock(p_ptr);
1334 int tipc_ref_valid(u32 ref)
1336 /* Works irrespective of type */
1337 return !!tipc_ref_deref(ref);
1342 * tipc_port_recv_sections(): Concatenate and deliver sectioned
1343 * message for this node.
1346 int tipc_port_recv_sections(struct port *sender, unsigned int num_sect,
1347 struct iovec const *msg_sect)
1349 struct sk_buff *buf;
1352 res = msg_build(&sender->publ.phdr, msg_sect, num_sect,
1353 MAX_MSG_SIZE, !sender->user_port, &buf);
1355 tipc_port_recv_msg(buf);
1360 * tipc_send - send message sections on connection
1363 int tipc_send(u32 ref, unsigned int num_sect, struct iovec const *msg_sect)
1369 p_ptr = tipc_port_deref(ref);
1370 if (!p_ptr || !p_ptr->publ.connected)
1373 p_ptr->publ.congested = 1;
1374 if (!tipc_port_congested(p_ptr)) {
1375 destnode = port_peernode(p_ptr);
1376 if (likely(destnode != tipc_own_addr))
1377 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
1380 res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
1382 if (likely(res != -ELINKCONG)) {
1383 port_incr_out_seqno(p_ptr);
1384 p_ptr->publ.congested = 0;
1389 if (port_unreliable(p_ptr)) {
1390 p_ptr->publ.congested = 0;
1391 /* Just calculate msg length and return */
1392 return msg_calc_data_size(msg_sect, num_sect);
1398 * tipc_send_buf - send message buffer on connection
1401 int tipc_send_buf(u32 ref, struct sk_buff *buf, unsigned int dsz)
1404 struct tipc_msg *msg;
1410 p_ptr = tipc_port_deref(ref);
1411 if (!p_ptr || !p_ptr->publ.connected)
1414 msg = &p_ptr->publ.phdr;
1415 hsz = msg_hdr_sz(msg);
1417 msg_set_size(msg, sz);
1418 if (skb_cow(buf, hsz))
1422 skb_copy_to_linear_data(buf, msg, hsz);
1423 destnode = msg_destnode(msg);
1424 p_ptr->publ.congested = 1;
1425 if (!tipc_port_congested(p_ptr)) {
1426 if (likely(destnode != tipc_own_addr))
1427 res = tipc_send_buf_fast(buf, destnode);
1429 tipc_port_recv_msg(buf);
1432 if (likely(res != -ELINKCONG)) {
1433 port_incr_out_seqno(p_ptr);
1435 p_ptr->publ.congested = 0;
1439 if (port_unreliable(p_ptr)) {
1440 p_ptr->publ.congested = 0;
1447 * tipc_forward2name - forward message sections to port name
1450 int tipc_forward2name(u32 ref,
1451 struct tipc_name const *name,
1454 struct iovec const *msg_sect,
1455 struct tipc_portid const *orig,
1456 unsigned int importance)
1459 struct tipc_msg *msg;
1460 u32 destnode = domain;
1464 p_ptr = tipc_port_deref(ref);
1465 if (!p_ptr || p_ptr->publ.connected)
1468 msg = &p_ptr->publ.phdr;
1469 msg_set_type(msg, TIPC_NAMED_MSG);
1470 msg_set_orignode(msg, orig->node);
1471 msg_set_origport(msg, orig->ref);
1472 msg_set_hdr_sz(msg, LONG_H_SIZE);
1473 msg_set_nametype(msg, name->type);
1474 msg_set_nameinst(msg, name->instance);
1475 msg_set_lookup_scope(msg, addr_scope(domain));
1476 if (importance <= TIPC_CRITICAL_IMPORTANCE)
1477 msg_set_importance(msg,importance);
1478 destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
1479 msg_set_destnode(msg, destnode);
1480 msg_set_destport(msg, destport);
1482 if (likely(destport || destnode)) {
1484 if (likely(destnode == tipc_own_addr))
1485 return tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
1486 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
1488 if (likely(res != -ELINKCONG))
1490 if (port_unreliable(p_ptr)) {
1491 /* Just calculate msg length and return */
1492 return msg_calc_data_size(msg_sect, num_sect);
1496 return tipc_port_reject_sections(p_ptr, msg, msg_sect, num_sect,
1501 * tipc_send2name - send message sections to port name
1504 int tipc_send2name(u32 ref,
1505 struct tipc_name const *name,
1506 unsigned int domain,
1507 unsigned int num_sect,
1508 struct iovec const *msg_sect)
1510 struct tipc_portid orig;
1513 orig.node = tipc_own_addr;
1514 return tipc_forward2name(ref, name, domain, num_sect, msg_sect, &orig,
1515 TIPC_PORT_IMPORTANCE);
1519 * tipc_forward_buf2name - forward message buffer to port name
1522 int tipc_forward_buf2name(u32 ref,
1523 struct tipc_name const *name,
1525 struct sk_buff *buf,
1527 struct tipc_portid const *orig,
1528 unsigned int importance)
1531 struct tipc_msg *msg;
1532 u32 destnode = domain;
1536 p_ptr = (struct port *)tipc_ref_deref(ref);
1537 if (!p_ptr || p_ptr->publ.connected)
1540 msg = &p_ptr->publ.phdr;
1541 if (importance <= TIPC_CRITICAL_IMPORTANCE)
1542 msg_set_importance(msg, importance);
1543 msg_set_type(msg, TIPC_NAMED_MSG);
1544 msg_set_orignode(msg, orig->node);
1545 msg_set_origport(msg, orig->ref);
1546 msg_set_nametype(msg, name->type);
1547 msg_set_nameinst(msg, name->instance);
1548 msg_set_lookup_scope(msg, addr_scope(domain));
1549 msg_set_hdr_sz(msg, LONG_H_SIZE);
1550 msg_set_size(msg, LONG_H_SIZE + dsz);
1551 destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
1552 msg_set_destnode(msg, destnode);
1553 msg_set_destport(msg, destport);
1554 msg_dbg(msg, "forw2name ==> ");
1555 if (skb_cow(buf, LONG_H_SIZE))
1557 skb_push(buf, LONG_H_SIZE);
1558 skb_copy_to_linear_data(buf, msg, LONG_H_SIZE);
1559 msg_dbg(buf_msg(buf),"PREP:");
1560 if (likely(destport || destnode)) {
1562 if (destnode == tipc_own_addr)
1563 return tipc_port_recv_msg(buf);
1564 res = tipc_send_buf_fast(buf, destnode);
1565 if (likely(res != -ELINKCONG))
1567 if (port_unreliable(p_ptr))
1571 return tipc_reject_msg(buf, TIPC_ERR_NO_NAME);
1575 * tipc_send_buf2name - send message buffer to port name
1578 int tipc_send_buf2name(u32 ref,
1579 struct tipc_name const *dest,
1581 struct sk_buff *buf,
1584 struct tipc_portid orig;
1587 orig.node = tipc_own_addr;
1588 return tipc_forward_buf2name(ref, dest, domain, buf, dsz, &orig,
1589 TIPC_PORT_IMPORTANCE);
1593 * tipc_forward2port - forward message sections to port identity
1596 int tipc_forward2port(u32 ref,
1597 struct tipc_portid const *dest,
1598 unsigned int num_sect,
1599 struct iovec const *msg_sect,
1600 struct tipc_portid const *orig,
1601 unsigned int importance)
1604 struct tipc_msg *msg;
1607 p_ptr = tipc_port_deref(ref);
1608 if (!p_ptr || p_ptr->publ.connected)
1611 msg = &p_ptr->publ.phdr;
1612 msg_set_type(msg, TIPC_DIRECT_MSG);
1613 msg_set_orignode(msg, orig->node);
1614 msg_set_origport(msg, orig->ref);
1615 msg_set_destnode(msg, dest->node);
1616 msg_set_destport(msg, dest->ref);
1617 msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
1618 if (importance <= TIPC_CRITICAL_IMPORTANCE)
1619 msg_set_importance(msg, importance);
1621 if (dest->node == tipc_own_addr)
1622 return tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
1623 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect, dest->node);
1624 if (likely(res != -ELINKCONG))
1626 if (port_unreliable(p_ptr)) {
1627 /* Just calculate msg length and return */
1628 return msg_calc_data_size(msg_sect, num_sect);
1634 * tipc_send2port - send message sections to port identity
1637 int tipc_send2port(u32 ref,
1638 struct tipc_portid const *dest,
1639 unsigned int num_sect,
1640 struct iovec const *msg_sect)
1642 struct tipc_portid orig;
1645 orig.node = tipc_own_addr;
1646 return tipc_forward2port(ref, dest, num_sect, msg_sect, &orig,
1647 TIPC_PORT_IMPORTANCE);
1651 * tipc_forward_buf2port - forward message buffer to port identity
1653 int tipc_forward_buf2port(u32 ref,
1654 struct tipc_portid const *dest,
1655 struct sk_buff *buf,
1657 struct tipc_portid const *orig,
1658 unsigned int importance)
1661 struct tipc_msg *msg;
1664 p_ptr = (struct port *)tipc_ref_deref(ref);
1665 if (!p_ptr || p_ptr->publ.connected)
1668 msg = &p_ptr->publ.phdr;
1669 msg_set_type(msg, TIPC_DIRECT_MSG);
1670 msg_set_orignode(msg, orig->node);
1671 msg_set_origport(msg, orig->ref);
1672 msg_set_destnode(msg, dest->node);
1673 msg_set_destport(msg, dest->ref);
1674 msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
1675 if (importance <= TIPC_CRITICAL_IMPORTANCE)
1676 msg_set_importance(msg, importance);
1677 msg_set_size(msg, DIR_MSG_H_SIZE + dsz);
1678 if (skb_cow(buf, DIR_MSG_H_SIZE))
1681 skb_push(buf, DIR_MSG_H_SIZE);
1682 skb_copy_to_linear_data(buf, msg, DIR_MSG_H_SIZE);
1683 msg_dbg(msg, "buf2port: ");
1685 if (dest->node == tipc_own_addr)
1686 return tipc_port_recv_msg(buf);
1687 res = tipc_send_buf_fast(buf, dest->node);
1688 if (likely(res != -ELINKCONG))
1690 if (port_unreliable(p_ptr))
1696 * tipc_send_buf2port - send message buffer to port identity
1699 int tipc_send_buf2port(u32 ref,
1700 struct tipc_portid const *dest,
1701 struct sk_buff *buf,
1704 struct tipc_portid orig;
1707 orig.node = tipc_own_addr;
1708 return tipc_forward_buf2port(ref, dest, buf, dsz, &orig,
1709 TIPC_PORT_IMPORTANCE);