2 * net/tipc/port.c: TIPC port code
4 * Copyright (c) 1992-2007, Ericsson AB
5 * Copyright (c) 2004-2008, 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 = tipc_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 pointer to (locked) TIPC port, or NULL if unable to create it
219 struct tipc_port *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");
240 p_ptr->publ.usr_handle = usr_handle;
241 p_ptr->publ.max_pkt = MAX_PKT_DEFAULT;
242 p_ptr->publ.ref = ref;
243 msg = &p_ptr->publ.phdr;
244 tipc_msg_init(msg, importance, TIPC_NAMED_MSG, LONG_H_SIZE, 0);
245 msg_set_origport(msg, ref);
246 p_ptr->last_in_seqno = 41;
248 INIT_LIST_HEAD(&p_ptr->wait_list);
249 INIT_LIST_HEAD(&p_ptr->subscription.nodesub_list);
250 p_ptr->dispatcher = dispatcher;
251 p_ptr->wakeup = wakeup;
252 p_ptr->user_port = NULL;
253 k_init_timer(&p_ptr->timer, (Handler)port_timeout, ref);
254 spin_lock_bh(&tipc_port_list_lock);
255 INIT_LIST_HEAD(&p_ptr->publications);
256 INIT_LIST_HEAD(&p_ptr->port_list);
257 list_add_tail(&p_ptr->port_list, &ports);
258 spin_unlock_bh(&tipc_port_list_lock);
259 return &(p_ptr->publ);
262 int tipc_deleteport(u32 ref)
265 struct sk_buff *buf = NULL;
267 tipc_withdraw(ref, 0, NULL);
268 p_ptr = tipc_port_lock(ref);
272 tipc_ref_discard(ref);
273 tipc_port_unlock(p_ptr);
275 k_cancel_timer(&p_ptr->timer);
276 if (p_ptr->publ.connected) {
277 buf = port_build_peer_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
278 tipc_nodesub_unsubscribe(&p_ptr->subscription);
280 if (p_ptr->user_port) {
281 tipc_reg_remove_port(p_ptr->user_port);
282 kfree(p_ptr->user_port);
285 spin_lock_bh(&tipc_port_list_lock);
286 list_del(&p_ptr->port_list);
287 list_del(&p_ptr->wait_list);
288 spin_unlock_bh(&tipc_port_list_lock);
289 k_term_timer(&p_ptr->timer);
291 dbg("Deleted port %u\n", ref);
292 tipc_net_route_msg(buf);
297 * tipc_get_port() - return port associated with 'ref'
299 * Note: Port is not locked.
302 struct tipc_port *tipc_get_port(const u32 ref)
304 return (struct tipc_port *)tipc_ref_deref(ref);
308 * tipc_get_handle - return user handle associated to port 'ref'
311 void *tipc_get_handle(const u32 ref)
316 p_ptr = tipc_port_lock(ref);
319 handle = p_ptr->publ.usr_handle;
320 tipc_port_unlock(p_ptr);
324 static int port_unreliable(struct port *p_ptr)
326 return msg_src_droppable(&p_ptr->publ.phdr);
329 int tipc_portunreliable(u32 ref, unsigned int *isunreliable)
333 p_ptr = tipc_port_lock(ref);
336 *isunreliable = port_unreliable(p_ptr);
337 tipc_port_unlock(p_ptr);
341 int tipc_set_portunreliable(u32 ref, unsigned int isunreliable)
345 p_ptr = tipc_port_lock(ref);
348 msg_set_src_droppable(&p_ptr->publ.phdr, (isunreliable != 0));
349 tipc_port_unlock(p_ptr);
353 static int port_unreturnable(struct port *p_ptr)
355 return msg_dest_droppable(&p_ptr->publ.phdr);
358 int tipc_portunreturnable(u32 ref, unsigned int *isunrejectable)
362 p_ptr = tipc_port_lock(ref);
365 *isunrejectable = port_unreturnable(p_ptr);
366 tipc_port_unlock(p_ptr);
370 int tipc_set_portunreturnable(u32 ref, unsigned int isunrejectable)
374 p_ptr = tipc_port_lock(ref);
377 msg_set_dest_droppable(&p_ptr->publ.phdr, (isunrejectable != 0));
378 tipc_port_unlock(p_ptr);
383 * port_build_proto_msg(): build a port level protocol
384 * or a connection abortion message. Called with
387 static struct sk_buff *port_build_proto_msg(u32 destport, u32 destnode,
388 u32 origport, u32 orignode,
389 u32 usr, u32 type, u32 err,
393 struct tipc_msg *msg;
395 buf = buf_acquire(LONG_H_SIZE);
398 tipc_msg_init(msg, usr, type, LONG_H_SIZE, destnode);
399 msg_set_errcode(msg, err);
400 msg_set_destport(msg, destport);
401 msg_set_origport(msg, origport);
402 msg_set_orignode(msg, orignode);
403 msg_set_transp_seqno(msg, seqno);
404 msg_set_msgcnt(msg, ack);
405 msg_dbg(msg, "PORT>SEND>:");
410 int tipc_reject_msg(struct sk_buff *buf, u32 err)
412 struct tipc_msg *msg = buf_msg(buf);
413 struct sk_buff *rbuf;
414 struct tipc_msg *rmsg;
416 u32 imp = msg_importance(msg);
417 u32 data_sz = msg_data_sz(msg);
419 if (data_sz > MAX_REJECT_SIZE)
420 data_sz = MAX_REJECT_SIZE;
421 if (msg_connected(msg) && (imp < TIPC_CRITICAL_IMPORTANCE))
423 msg_dbg(msg, "port->rej: ");
425 /* discard rejected message if it shouldn't be returned to sender */
426 if (msg_errcode(msg) || msg_dest_droppable(msg)) {
431 /* construct rejected message */
433 hdr_sz = MCAST_H_SIZE;
435 hdr_sz = LONG_H_SIZE;
436 rbuf = buf_acquire(data_sz + hdr_sz);
441 rmsg = buf_msg(rbuf);
442 tipc_msg_init(rmsg, imp, msg_type(msg), hdr_sz, msg_orignode(msg));
443 msg_set_errcode(rmsg, err);
444 msg_set_destport(rmsg, msg_origport(msg));
445 msg_set_origport(rmsg, msg_destport(msg));
446 if (msg_short(msg)) {
447 msg_set_orignode(rmsg, tipc_own_addr);
448 /* leave name type & instance as zeroes */
450 msg_set_orignode(rmsg, msg_destnode(msg));
451 msg_set_nametype(rmsg, msg_nametype(msg));
452 msg_set_nameinst(rmsg, msg_nameinst(msg));
454 msg_set_size(rmsg, data_sz + hdr_sz);
455 skb_copy_to_linear_data_offset(rbuf, hdr_sz, msg_data(msg), data_sz);
457 /* send self-abort message when rejecting on a connected port */
458 if (msg_connected(msg)) {
459 struct sk_buff *abuf = NULL;
460 struct port *p_ptr = tipc_port_lock(msg_destport(msg));
463 if (p_ptr->publ.connected)
464 abuf = port_build_self_abort_msg(p_ptr, err);
465 tipc_port_unlock(p_ptr);
467 tipc_net_route_msg(abuf);
470 /* send rejected message */
472 tipc_net_route_msg(rbuf);
476 int tipc_port_reject_sections(struct port *p_ptr, struct tipc_msg *hdr,
477 struct iovec const *msg_sect, u32 num_sect,
483 res = tipc_msg_build(hdr, msg_sect, num_sect, MAX_MSG_SIZE,
484 !p_ptr->user_port, &buf);
488 return tipc_reject_msg(buf, err);
491 static void port_timeout(unsigned long ref)
493 struct port *p_ptr = tipc_port_lock(ref);
494 struct sk_buff *buf = NULL;
499 if (!p_ptr->publ.connected) {
500 tipc_port_unlock(p_ptr);
504 /* Last probe answered ? */
505 if (p_ptr->probing_state == PROBING) {
506 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
508 buf = port_build_proto_msg(port_peerport(p_ptr),
509 port_peernode(p_ptr),
515 port_out_seqno(p_ptr),
517 port_incr_out_seqno(p_ptr);
518 p_ptr->probing_state = PROBING;
519 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
521 tipc_port_unlock(p_ptr);
522 tipc_net_route_msg(buf);
526 static void port_handle_node_down(unsigned long ref)
528 struct port *p_ptr = tipc_port_lock(ref);
529 struct sk_buff* buf = NULL;
533 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_NODE);
534 tipc_port_unlock(p_ptr);
535 tipc_net_route_msg(buf);
539 static struct sk_buff *port_build_self_abort_msg(struct port *p_ptr, u32 err)
541 u32 imp = msg_importance(&p_ptr->publ.phdr);
543 if (!p_ptr->publ.connected)
545 if (imp < TIPC_CRITICAL_IMPORTANCE)
547 return port_build_proto_msg(p_ptr->publ.ref,
549 port_peerport(p_ptr),
550 port_peernode(p_ptr),
554 p_ptr->last_in_seqno + 1,
559 static struct sk_buff *port_build_peer_abort_msg(struct port *p_ptr, u32 err)
561 u32 imp = msg_importance(&p_ptr->publ.phdr);
563 if (!p_ptr->publ.connected)
565 if (imp < TIPC_CRITICAL_IMPORTANCE)
567 return port_build_proto_msg(port_peerport(p_ptr),
568 port_peernode(p_ptr),
574 port_out_seqno(p_ptr),
578 void tipc_port_recv_proto_msg(struct sk_buff *buf)
580 struct tipc_msg *msg = buf_msg(buf);
581 struct port *p_ptr = tipc_port_lock(msg_destport(msg));
583 struct sk_buff *r_buf = NULL;
584 struct sk_buff *abort_buf = NULL;
586 msg_dbg(msg, "PORT<RECV<:");
589 err = TIPC_ERR_NO_PORT;
590 } else if (p_ptr->publ.connected) {
591 if ((port_peernode(p_ptr) != msg_orignode(msg)) ||
592 (port_peerport(p_ptr) != msg_origport(msg))) {
593 err = TIPC_ERR_NO_PORT;
594 } else if (msg_type(msg) == CONN_ACK) {
595 int wakeup = tipc_port_congested(p_ptr) &&
596 p_ptr->publ.congested &&
598 p_ptr->acked += msg_msgcnt(msg);
599 if (tipc_port_congested(p_ptr))
601 p_ptr->publ.congested = 0;
604 p_ptr->wakeup(&p_ptr->publ);
607 } else if (p_ptr->publ.published) {
608 err = TIPC_ERR_NO_PORT;
611 r_buf = port_build_proto_msg(msg_origport(msg),
615 TIPC_HIGH_IMPORTANCE,
624 if (msg_type(msg) == CONN_PROBE) {
625 r_buf = port_build_proto_msg(msg_origport(msg),
632 port_out_seqno(p_ptr),
635 p_ptr->probing_state = CONFIRMED;
636 port_incr_out_seqno(p_ptr);
639 tipc_port_unlock(p_ptr);
640 tipc_net_route_msg(r_buf);
641 tipc_net_route_msg(abort_buf);
645 static void port_print(struct port *p_ptr, struct print_buf *buf, int full_id)
647 struct publication *publ;
650 tipc_printf(buf, "<%u.%u.%u:%u>:",
651 tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
652 tipc_node(tipc_own_addr), p_ptr->publ.ref);
654 tipc_printf(buf, "%-10u:", p_ptr->publ.ref);
656 if (p_ptr->publ.connected) {
657 u32 dport = port_peerport(p_ptr);
658 u32 destnode = port_peernode(p_ptr);
660 tipc_printf(buf, " connected to <%u.%u.%u:%u>",
661 tipc_zone(destnode), tipc_cluster(destnode),
662 tipc_node(destnode), dport);
663 if (p_ptr->publ.conn_type != 0)
664 tipc_printf(buf, " via {%u,%u}",
665 p_ptr->publ.conn_type,
666 p_ptr->publ.conn_instance);
668 else if (p_ptr->publ.published) {
669 tipc_printf(buf, " bound to");
670 list_for_each_entry(publ, &p_ptr->publications, pport_list) {
671 if (publ->lower == publ->upper)
672 tipc_printf(buf, " {%u,%u}", publ->type,
675 tipc_printf(buf, " {%u,%u,%u}", publ->type,
676 publ->lower, publ->upper);
679 tipc_printf(buf, "\n");
682 #define MAX_PORT_QUERY 32768
684 struct sk_buff *tipc_port_get_ports(void)
687 struct tlv_desc *rep_tlv;
692 buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_PORT_QUERY));
695 rep_tlv = (struct tlv_desc *)buf->data;
697 tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), MAX_PORT_QUERY);
698 spin_lock_bh(&tipc_port_list_lock);
699 list_for_each_entry(p_ptr, &ports, port_list) {
700 spin_lock_bh(p_ptr->publ.lock);
701 port_print(p_ptr, &pb, 0);
702 spin_unlock_bh(p_ptr->publ.lock);
704 spin_unlock_bh(&tipc_port_list_lock);
705 str_len = tipc_printbuf_validate(&pb);
707 skb_put(buf, TLV_SPACE(str_len));
708 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
713 void tipc_port_reinit(void)
716 struct tipc_msg *msg;
718 spin_lock_bh(&tipc_port_list_lock);
719 list_for_each_entry(p_ptr, &ports, port_list) {
720 msg = &p_ptr->publ.phdr;
721 if (msg_orignode(msg) == tipc_own_addr)
723 msg_set_prevnode(msg, tipc_own_addr);
724 msg_set_orignode(msg, tipc_own_addr);
726 spin_unlock_bh(&tipc_port_list_lock);
731 * port_dispatcher_sigh(): Signal handler for messages destinated
732 * to the tipc_port interface.
735 static void port_dispatcher_sigh(void *dummy)
739 spin_lock_bh(&queue_lock);
740 buf = msg_queue_head;
741 msg_queue_head = NULL;
742 spin_unlock_bh(&queue_lock);
746 struct user_port *up_ptr;
747 struct tipc_portid orig;
748 struct tipc_name_seq dseq;
754 struct sk_buff *next = buf->next;
755 struct tipc_msg *msg = buf_msg(buf);
756 u32 dref = msg_destport(msg);
758 message_type = msg_type(msg);
759 if (message_type > TIPC_DIRECT_MSG)
760 goto reject; /* Unsupported message type */
762 p_ptr = tipc_port_lock(dref);
764 goto reject; /* Port deleted while msg in queue */
766 orig.ref = msg_origport(msg);
767 orig.node = msg_orignode(msg);
768 up_ptr = p_ptr->user_port;
769 usr_handle = up_ptr->usr_handle;
770 connected = p_ptr->publ.connected;
771 published = p_ptr->publ.published;
773 if (unlikely(msg_errcode(msg)))
776 switch (message_type) {
779 tipc_conn_msg_event cb = up_ptr->conn_msg_cb;
780 u32 peer_port = port_peerport(p_ptr);
781 u32 peer_node = port_peernode(p_ptr);
783 tipc_port_unlock(p_ptr);
786 if (unlikely(!connected)) {
787 if (tipc_connect2port(dref, &orig))
789 } else if ((msg_origport(msg) != peer_port) ||
790 (msg_orignode(msg) != peer_node))
792 if (unlikely(++p_ptr->publ.conn_unacked >=
793 TIPC_FLOW_CONTROL_WIN))
794 tipc_acknowledge(dref,
795 p_ptr->publ.conn_unacked);
796 skb_pull(buf, msg_hdr_sz(msg));
797 cb(usr_handle, dref, &buf, msg_data(msg),
801 case TIPC_DIRECT_MSG:{
802 tipc_msg_event cb = up_ptr->msg_cb;
804 tipc_port_unlock(p_ptr);
805 if (unlikely(!cb || connected))
807 skb_pull(buf, msg_hdr_sz(msg));
808 cb(usr_handle, dref, &buf, msg_data(msg),
809 msg_data_sz(msg), msg_importance(msg),
814 case TIPC_NAMED_MSG:{
815 tipc_named_msg_event cb = up_ptr->named_msg_cb;
817 tipc_port_unlock(p_ptr);
818 if (unlikely(!cb || connected || !published))
820 dseq.type = msg_nametype(msg);
821 dseq.lower = msg_nameinst(msg);
822 dseq.upper = (message_type == TIPC_NAMED_MSG)
823 ? dseq.lower : msg_nameupper(msg);
824 skb_pull(buf, msg_hdr_sz(msg));
825 cb(usr_handle, dref, &buf, msg_data(msg),
826 msg_data_sz(msg), msg_importance(msg),
836 switch (message_type) {
839 tipc_conn_shutdown_event cb =
841 u32 peer_port = port_peerport(p_ptr);
842 u32 peer_node = port_peernode(p_ptr);
844 tipc_port_unlock(p_ptr);
845 if (!cb || !connected)
847 if ((msg_origport(msg) != peer_port) ||
848 (msg_orignode(msg) != peer_node))
850 tipc_disconnect(dref);
851 skb_pull(buf, msg_hdr_sz(msg));
852 cb(usr_handle, dref, &buf, msg_data(msg),
853 msg_data_sz(msg), msg_errcode(msg));
856 case TIPC_DIRECT_MSG:{
857 tipc_msg_err_event cb = up_ptr->err_cb;
859 tipc_port_unlock(p_ptr);
860 if (!cb || connected)
862 skb_pull(buf, msg_hdr_sz(msg));
863 cb(usr_handle, dref, &buf, msg_data(msg),
864 msg_data_sz(msg), msg_errcode(msg), &orig);
868 case TIPC_NAMED_MSG:{
869 tipc_named_msg_err_event cb =
870 up_ptr->named_err_cb;
872 tipc_port_unlock(p_ptr);
873 if (!cb || connected)
875 dseq.type = msg_nametype(msg);
876 dseq.lower = msg_nameinst(msg);
877 dseq.upper = (message_type == TIPC_NAMED_MSG)
878 ? dseq.lower : msg_nameupper(msg);
879 skb_pull(buf, msg_hdr_sz(msg));
880 cb(usr_handle, dref, &buf, msg_data(msg),
881 msg_data_sz(msg), msg_errcode(msg), &dseq);
890 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
896 * port_dispatcher(): Dispatcher for messages destinated
897 * to the tipc_port interface. Called with port locked.
900 static u32 port_dispatcher(struct tipc_port *dummy, struct sk_buff *buf)
903 spin_lock_bh(&queue_lock);
904 if (msg_queue_head) {
905 msg_queue_tail->next = buf;
906 msg_queue_tail = buf;
908 msg_queue_tail = msg_queue_head = buf;
909 tipc_k_signal((Handler)port_dispatcher_sigh, 0);
911 spin_unlock_bh(&queue_lock);
916 * Wake up port after congestion: Called with port locked,
920 static void port_wakeup_sh(unsigned long ref)
923 struct user_port *up_ptr;
924 tipc_continue_event cb = NULL;
927 p_ptr = tipc_port_lock(ref);
929 up_ptr = p_ptr->user_port;
931 cb = up_ptr->continue_event_cb;
932 uh = up_ptr->usr_handle;
934 tipc_port_unlock(p_ptr);
941 static void port_wakeup(struct tipc_port *p_ptr)
943 tipc_k_signal((Handler)port_wakeup_sh, p_ptr->ref);
946 void tipc_acknowledge(u32 ref, u32 ack)
949 struct sk_buff *buf = NULL;
951 p_ptr = tipc_port_lock(ref);
954 if (p_ptr->publ.connected) {
955 p_ptr->publ.conn_unacked -= ack;
956 buf = port_build_proto_msg(port_peerport(p_ptr),
957 port_peernode(p_ptr),
963 port_out_seqno(p_ptr),
966 tipc_port_unlock(p_ptr);
967 tipc_net_route_msg(buf);
971 * tipc_createport(): user level call. Will add port to
972 * registry if non-zero user_ref.
975 int tipc_createport(u32 user_ref,
977 unsigned int importance,
978 tipc_msg_err_event error_cb,
979 tipc_named_msg_err_event named_error_cb,
980 tipc_conn_shutdown_event conn_error_cb,
981 tipc_msg_event msg_cb,
982 tipc_named_msg_event named_msg_cb,
983 tipc_conn_msg_event conn_msg_cb,
984 tipc_continue_event continue_event_cb,/* May be zero */
987 struct user_port *up_ptr;
990 up_ptr = kmalloc(sizeof(*up_ptr), GFP_ATOMIC);
992 warn("Port creation failed, no memory\n");
995 p_ptr = (struct port *)tipc_createport_raw(NULL, port_dispatcher,
996 port_wakeup, importance);
1002 p_ptr->user_port = up_ptr;
1003 up_ptr->user_ref = user_ref;
1004 up_ptr->usr_handle = usr_handle;
1005 up_ptr->ref = p_ptr->publ.ref;
1006 up_ptr->err_cb = error_cb;
1007 up_ptr->named_err_cb = named_error_cb;
1008 up_ptr->conn_err_cb = conn_error_cb;
1009 up_ptr->msg_cb = msg_cb;
1010 up_ptr->named_msg_cb = named_msg_cb;
1011 up_ptr->conn_msg_cb = conn_msg_cb;
1012 up_ptr->continue_event_cb = continue_event_cb;
1013 INIT_LIST_HEAD(&up_ptr->uport_list);
1014 tipc_reg_add_port(up_ptr);
1015 *portref = p_ptr->publ.ref;
1016 tipc_port_unlock(p_ptr);
1020 int tipc_ownidentity(u32 ref, struct tipc_portid *id)
1023 id->node = tipc_own_addr;
1027 int tipc_portimportance(u32 ref, unsigned int *importance)
1031 p_ptr = tipc_port_lock(ref);
1034 *importance = (unsigned int)msg_importance(&p_ptr->publ.phdr);
1035 tipc_port_unlock(p_ptr);
1039 int tipc_set_portimportance(u32 ref, unsigned int imp)
1043 if (imp > TIPC_CRITICAL_IMPORTANCE)
1046 p_ptr = tipc_port_lock(ref);
1049 msg_set_importance(&p_ptr->publ.phdr, (u32)imp);
1050 tipc_port_unlock(p_ptr);
1055 int tipc_publish(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
1058 struct publication *publ;
1062 p_ptr = tipc_port_lock(ref);
1066 dbg("tipc_publ %u, p_ptr = %x, conn = %x, scope = %x, "
1067 "lower = %u, upper = %u\n",
1068 ref, p_ptr, p_ptr->publ.connected, scope, seq->lower, seq->upper);
1069 if (p_ptr->publ.connected)
1071 if (seq->lower > seq->upper)
1073 if ((scope < TIPC_ZONE_SCOPE) || (scope > TIPC_NODE_SCOPE))
1075 key = ref + p_ptr->pub_count + 1;
1080 publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
1081 scope, p_ptr->publ.ref, key);
1083 list_add(&publ->pport_list, &p_ptr->publications);
1085 p_ptr->publ.published = 1;
1089 tipc_port_unlock(p_ptr);
1093 int tipc_withdraw(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
1096 struct publication *publ;
1097 struct publication *tpubl;
1100 p_ptr = tipc_port_lock(ref);
1104 list_for_each_entry_safe(publ, tpubl,
1105 &p_ptr->publications, pport_list) {
1106 tipc_nametbl_withdraw(publ->type, publ->lower,
1107 publ->ref, publ->key);
1111 list_for_each_entry_safe(publ, tpubl,
1112 &p_ptr->publications, pport_list) {
1113 if (publ->scope != scope)
1115 if (publ->type != seq->type)
1117 if (publ->lower != seq->lower)
1119 if (publ->upper != seq->upper)
1121 tipc_nametbl_withdraw(publ->type, publ->lower,
1122 publ->ref, publ->key);
1127 if (list_empty(&p_ptr->publications))
1128 p_ptr->publ.published = 0;
1129 tipc_port_unlock(p_ptr);
1133 int tipc_connect2port(u32 ref, struct tipc_portid const *peer)
1136 struct tipc_msg *msg;
1139 p_ptr = tipc_port_lock(ref);
1142 if (p_ptr->publ.published || p_ptr->publ.connected)
1147 msg = &p_ptr->publ.phdr;
1148 msg_set_destnode(msg, peer->node);
1149 msg_set_destport(msg, peer->ref);
1150 msg_set_orignode(msg, tipc_own_addr);
1151 msg_set_origport(msg, p_ptr->publ.ref);
1152 msg_set_transp_seqno(msg, 42);
1153 msg_set_type(msg, TIPC_CONN_MSG);
1154 if (!may_route(peer->node))
1155 msg_set_hdr_sz(msg, SHORT_H_SIZE);
1157 msg_set_hdr_sz(msg, LONG_H_SIZE);
1159 p_ptr->probing_interval = PROBING_INTERVAL;
1160 p_ptr->probing_state = CONFIRMED;
1161 p_ptr->publ.connected = 1;
1162 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
1164 tipc_nodesub_subscribe(&p_ptr->subscription,peer->node,
1165 (void *)(unsigned long)ref,
1166 (net_ev_handler)port_handle_node_down);
1169 tipc_port_unlock(p_ptr);
1170 p_ptr->publ.max_pkt = tipc_link_get_max_pkt(peer->node, ref);
1175 * tipc_disconnect_port - disconnect port from peer
1177 * Port must be locked.
1180 int tipc_disconnect_port(struct tipc_port *tp_ptr)
1184 if (tp_ptr->connected) {
1185 tp_ptr->connected = 0;
1186 /* let timer expire on it's own to avoid deadlock! */
1187 tipc_nodesub_unsubscribe(
1188 &((struct port *)tp_ptr)->subscription);
1197 * tipc_disconnect(): Disconnect port form peer.
1198 * This is a node local operation.
1201 int tipc_disconnect(u32 ref)
1206 p_ptr = tipc_port_lock(ref);
1209 res = tipc_disconnect_port((struct tipc_port *)p_ptr);
1210 tipc_port_unlock(p_ptr);
1215 * tipc_shutdown(): Send a SHUTDOWN msg to peer and disconnect
1217 int tipc_shutdown(u32 ref)
1220 struct sk_buff *buf = NULL;
1222 p_ptr = tipc_port_lock(ref);
1226 if (p_ptr->publ.connected) {
1227 u32 imp = msg_importance(&p_ptr->publ.phdr);
1228 if (imp < TIPC_CRITICAL_IMPORTANCE)
1230 buf = port_build_proto_msg(port_peerport(p_ptr),
1231 port_peernode(p_ptr),
1237 port_out_seqno(p_ptr),
1240 tipc_port_unlock(p_ptr);
1241 tipc_net_route_msg(buf);
1242 return tipc_disconnect(ref);
1245 int tipc_isconnected(u32 ref, int *isconnected)
1249 p_ptr = tipc_port_lock(ref);
1252 *isconnected = p_ptr->publ.connected;
1253 tipc_port_unlock(p_ptr);
1257 int tipc_peer(u32 ref, struct tipc_portid *peer)
1262 p_ptr = tipc_port_lock(ref);
1265 if (p_ptr->publ.connected) {
1266 peer->ref = port_peerport(p_ptr);
1267 peer->node = port_peernode(p_ptr);
1271 tipc_port_unlock(p_ptr);
1275 int tipc_ref_valid(u32 ref)
1277 /* Works irrespective of type */
1278 return !!tipc_ref_deref(ref);
1283 * tipc_port_recv_sections(): Concatenate and deliver sectioned
1284 * message for this node.
1287 int tipc_port_recv_sections(struct port *sender, unsigned int num_sect,
1288 struct iovec const *msg_sect)
1290 struct sk_buff *buf;
1293 res = tipc_msg_build(&sender->publ.phdr, msg_sect, num_sect,
1294 MAX_MSG_SIZE, !sender->user_port, &buf);
1296 tipc_port_recv_msg(buf);
1301 * tipc_send - send message sections on connection
1304 int tipc_send(u32 ref, unsigned int num_sect, struct iovec const *msg_sect)
1310 p_ptr = tipc_port_deref(ref);
1311 if (!p_ptr || !p_ptr->publ.connected)
1314 p_ptr->publ.congested = 1;
1315 if (!tipc_port_congested(p_ptr)) {
1316 destnode = port_peernode(p_ptr);
1317 if (likely(destnode != tipc_own_addr))
1318 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
1321 res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
1323 if (likely(res != -ELINKCONG)) {
1324 port_incr_out_seqno(p_ptr);
1325 p_ptr->publ.congested = 0;
1330 if (port_unreliable(p_ptr)) {
1331 p_ptr->publ.congested = 0;
1332 /* Just calculate msg length and return */
1333 return tipc_msg_calc_data_size(msg_sect, num_sect);
1339 * tipc_send_buf - send message buffer on connection
1342 int tipc_send_buf(u32 ref, struct sk_buff *buf, unsigned int dsz)
1345 struct tipc_msg *msg;
1351 p_ptr = tipc_port_deref(ref);
1352 if (!p_ptr || !p_ptr->publ.connected)
1355 msg = &p_ptr->publ.phdr;
1356 hsz = msg_hdr_sz(msg);
1358 msg_set_size(msg, sz);
1359 if (skb_cow(buf, hsz))
1363 skb_copy_to_linear_data(buf, msg, hsz);
1364 destnode = msg_destnode(msg);
1365 p_ptr->publ.congested = 1;
1366 if (!tipc_port_congested(p_ptr)) {
1367 if (likely(destnode != tipc_own_addr))
1368 res = tipc_send_buf_fast(buf, destnode);
1370 tipc_port_recv_msg(buf);
1373 if (likely(res != -ELINKCONG)) {
1374 port_incr_out_seqno(p_ptr);
1376 p_ptr->publ.congested = 0;
1380 if (port_unreliable(p_ptr)) {
1381 p_ptr->publ.congested = 0;
1388 * tipc_forward2name - forward message sections to port name
1391 int tipc_forward2name(u32 ref,
1392 struct tipc_name const *name,
1395 struct iovec const *msg_sect,
1396 struct tipc_portid const *orig,
1397 unsigned int importance)
1400 struct tipc_msg *msg;
1401 u32 destnode = domain;
1405 p_ptr = tipc_port_deref(ref);
1406 if (!p_ptr || p_ptr->publ.connected)
1409 msg = &p_ptr->publ.phdr;
1410 msg_set_type(msg, TIPC_NAMED_MSG);
1411 msg_set_orignode(msg, orig->node);
1412 msg_set_origport(msg, orig->ref);
1413 msg_set_hdr_sz(msg, LONG_H_SIZE);
1414 msg_set_nametype(msg, name->type);
1415 msg_set_nameinst(msg, name->instance);
1416 msg_set_lookup_scope(msg, tipc_addr_scope(domain));
1417 if (importance <= TIPC_CRITICAL_IMPORTANCE)
1418 msg_set_importance(msg,importance);
1419 destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
1420 msg_set_destnode(msg, destnode);
1421 msg_set_destport(msg, destport);
1423 if (likely(destport)) {
1425 if (likely(destnode == tipc_own_addr))
1426 return tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
1427 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
1429 if (likely(res != -ELINKCONG))
1431 if (port_unreliable(p_ptr)) {
1432 /* Just calculate msg length and return */
1433 return tipc_msg_calc_data_size(msg_sect, num_sect);
1437 return tipc_port_reject_sections(p_ptr, msg, msg_sect, num_sect,
1442 * tipc_send2name - send message sections to port name
1445 int tipc_send2name(u32 ref,
1446 struct tipc_name const *name,
1447 unsigned int domain,
1448 unsigned int num_sect,
1449 struct iovec const *msg_sect)
1451 struct tipc_portid orig;
1454 orig.node = tipc_own_addr;
1455 return tipc_forward2name(ref, name, domain, num_sect, msg_sect, &orig,
1456 TIPC_PORT_IMPORTANCE);
1460 * tipc_forward_buf2name - forward message buffer to port name
1463 int tipc_forward_buf2name(u32 ref,
1464 struct tipc_name const *name,
1466 struct sk_buff *buf,
1468 struct tipc_portid const *orig,
1469 unsigned int importance)
1472 struct tipc_msg *msg;
1473 u32 destnode = domain;
1477 p_ptr = (struct port *)tipc_ref_deref(ref);
1478 if (!p_ptr || p_ptr->publ.connected)
1481 msg = &p_ptr->publ.phdr;
1482 if (importance <= TIPC_CRITICAL_IMPORTANCE)
1483 msg_set_importance(msg, importance);
1484 msg_set_type(msg, TIPC_NAMED_MSG);
1485 msg_set_orignode(msg, orig->node);
1486 msg_set_origport(msg, orig->ref);
1487 msg_set_nametype(msg, name->type);
1488 msg_set_nameinst(msg, name->instance);
1489 msg_set_lookup_scope(msg, tipc_addr_scope(domain));
1490 msg_set_hdr_sz(msg, LONG_H_SIZE);
1491 msg_set_size(msg, LONG_H_SIZE + dsz);
1492 destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
1493 msg_set_destnode(msg, destnode);
1494 msg_set_destport(msg, destport);
1495 msg_dbg(msg, "forw2name ==> ");
1496 if (skb_cow(buf, LONG_H_SIZE))
1498 skb_push(buf, LONG_H_SIZE);
1499 skb_copy_to_linear_data(buf, msg, LONG_H_SIZE);
1500 msg_dbg(buf_msg(buf),"PREP:");
1501 if (likely(destport)) {
1503 if (destnode == tipc_own_addr)
1504 return tipc_port_recv_msg(buf);
1505 res = tipc_send_buf_fast(buf, destnode);
1506 if (likely(res != -ELINKCONG))
1508 if (port_unreliable(p_ptr))
1512 return tipc_reject_msg(buf, TIPC_ERR_NO_NAME);
1516 * tipc_send_buf2name - send message buffer to port name
1519 int tipc_send_buf2name(u32 ref,
1520 struct tipc_name const *dest,
1522 struct sk_buff *buf,
1525 struct tipc_portid orig;
1528 orig.node = tipc_own_addr;
1529 return tipc_forward_buf2name(ref, dest, domain, buf, dsz, &orig,
1530 TIPC_PORT_IMPORTANCE);
1534 * tipc_forward2port - forward message sections to port identity
1537 int tipc_forward2port(u32 ref,
1538 struct tipc_portid const *dest,
1539 unsigned int num_sect,
1540 struct iovec const *msg_sect,
1541 struct tipc_portid const *orig,
1542 unsigned int importance)
1545 struct tipc_msg *msg;
1548 p_ptr = tipc_port_deref(ref);
1549 if (!p_ptr || p_ptr->publ.connected)
1552 msg = &p_ptr->publ.phdr;
1553 msg_set_type(msg, TIPC_DIRECT_MSG);
1554 msg_set_orignode(msg, orig->node);
1555 msg_set_origport(msg, orig->ref);
1556 msg_set_destnode(msg, dest->node);
1557 msg_set_destport(msg, dest->ref);
1558 msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
1559 if (importance <= TIPC_CRITICAL_IMPORTANCE)
1560 msg_set_importance(msg, importance);
1562 if (dest->node == tipc_own_addr)
1563 return tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
1564 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect, dest->node);
1565 if (likely(res != -ELINKCONG))
1567 if (port_unreliable(p_ptr)) {
1568 /* Just calculate msg length and return */
1569 return tipc_msg_calc_data_size(msg_sect, num_sect);
1575 * tipc_send2port - send message sections to port identity
1578 int tipc_send2port(u32 ref,
1579 struct tipc_portid const *dest,
1580 unsigned int num_sect,
1581 struct iovec const *msg_sect)
1583 struct tipc_portid orig;
1586 orig.node = tipc_own_addr;
1587 return tipc_forward2port(ref, dest, num_sect, msg_sect, &orig,
1588 TIPC_PORT_IMPORTANCE);
1592 * tipc_forward_buf2port - forward message buffer to port identity
1594 int tipc_forward_buf2port(u32 ref,
1595 struct tipc_portid const *dest,
1596 struct sk_buff *buf,
1598 struct tipc_portid const *orig,
1599 unsigned int importance)
1602 struct tipc_msg *msg;
1605 p_ptr = (struct port *)tipc_ref_deref(ref);
1606 if (!p_ptr || p_ptr->publ.connected)
1609 msg = &p_ptr->publ.phdr;
1610 msg_set_type(msg, TIPC_DIRECT_MSG);
1611 msg_set_orignode(msg, orig->node);
1612 msg_set_origport(msg, orig->ref);
1613 msg_set_destnode(msg, dest->node);
1614 msg_set_destport(msg, dest->ref);
1615 msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
1616 if (importance <= TIPC_CRITICAL_IMPORTANCE)
1617 msg_set_importance(msg, importance);
1618 msg_set_size(msg, DIR_MSG_H_SIZE + dsz);
1619 if (skb_cow(buf, DIR_MSG_H_SIZE))
1622 skb_push(buf, DIR_MSG_H_SIZE);
1623 skb_copy_to_linear_data(buf, msg, DIR_MSG_H_SIZE);
1624 msg_dbg(msg, "buf2port: ");
1626 if (dest->node == tipc_own_addr)
1627 return tipc_port_recv_msg(buf);
1628 res = tipc_send_buf_fast(buf, dest->node);
1629 if (likely(res != -ELINKCONG))
1631 if (port_unreliable(p_ptr))
1637 * tipc_send_buf2port - send message buffer to port identity
1640 int tipc_send_buf2port(u32 ref,
1641 struct tipc_portid const *dest,
1642 struct sk_buff *buf,
1645 struct tipc_portid orig;
1648 orig.node = tipc_own_addr;
1649 return tipc_forward_buf2port(ref, dest, buf, dsz, &orig,
1650 TIPC_PORT_IMPORTANCE);