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
);
296 static int port_unreliable(struct port
*p_ptr
)
298 return msg_src_droppable(&p_ptr
->publ
.phdr
);
301 int tipc_portunreliable(u32 ref
, unsigned int *isunreliable
)
305 p_ptr
= tipc_port_lock(ref
);
308 *isunreliable
= port_unreliable(p_ptr
);
309 tipc_port_unlock(p_ptr
);
313 int tipc_set_portunreliable(u32 ref
, unsigned int isunreliable
)
317 p_ptr
= tipc_port_lock(ref
);
320 msg_set_src_droppable(&p_ptr
->publ
.phdr
, (isunreliable
!= 0));
321 tipc_port_unlock(p_ptr
);
325 static int port_unreturnable(struct port
*p_ptr
)
327 return msg_dest_droppable(&p_ptr
->publ
.phdr
);
330 int tipc_portunreturnable(u32 ref
, unsigned int *isunrejectable
)
334 p_ptr
= tipc_port_lock(ref
);
337 *isunrejectable
= port_unreturnable(p_ptr
);
338 tipc_port_unlock(p_ptr
);
342 int tipc_set_portunreturnable(u32 ref
, unsigned int isunrejectable
)
346 p_ptr
= tipc_port_lock(ref
);
349 msg_set_dest_droppable(&p_ptr
->publ
.phdr
, (isunrejectable
!= 0));
350 tipc_port_unlock(p_ptr
);
355 * port_build_proto_msg(): build a port level protocol
356 * or a connection abortion message. Called with
359 static struct sk_buff
*port_build_proto_msg(u32 destport
, u32 destnode
,
360 u32 origport
, u32 orignode
,
361 u32 usr
, u32 type
, u32 err
,
365 struct tipc_msg
*msg
;
367 buf
= tipc_buf_acquire(LONG_H_SIZE
);
370 tipc_msg_init(msg
, usr
, type
, LONG_H_SIZE
, destnode
);
371 msg_set_errcode(msg
, err
);
372 msg_set_destport(msg
, destport
);
373 msg_set_origport(msg
, origport
);
374 msg_set_orignode(msg
, orignode
);
375 msg_set_transp_seqno(msg
, seqno
);
376 msg_set_msgcnt(msg
, ack
);
377 msg_dbg(msg
, "PORT>SEND>:");
382 int tipc_reject_msg(struct sk_buff
*buf
, u32 err
)
384 struct tipc_msg
*msg
= buf_msg(buf
);
385 struct sk_buff
*rbuf
;
386 struct tipc_msg
*rmsg
;
388 u32 imp
= msg_importance(msg
);
389 u32 data_sz
= msg_data_sz(msg
);
391 if (data_sz
> MAX_REJECT_SIZE
)
392 data_sz
= MAX_REJECT_SIZE
;
393 if (msg_connected(msg
) && (imp
< TIPC_CRITICAL_IMPORTANCE
))
395 msg_dbg(msg
, "port->rej: ");
397 /* discard rejected message if it shouldn't be returned to sender */
398 if (msg_errcode(msg
) || msg_dest_droppable(msg
)) {
403 /* construct rejected message */
405 hdr_sz
= MCAST_H_SIZE
;
407 hdr_sz
= LONG_H_SIZE
;
408 rbuf
= tipc_buf_acquire(data_sz
+ hdr_sz
);
413 rmsg
= buf_msg(rbuf
);
414 tipc_msg_init(rmsg
, imp
, msg_type(msg
), hdr_sz
, msg_orignode(msg
));
415 msg_set_errcode(rmsg
, err
);
416 msg_set_destport(rmsg
, msg_origport(msg
));
417 msg_set_origport(rmsg
, msg_destport(msg
));
418 if (msg_short(msg
)) {
419 msg_set_orignode(rmsg
, tipc_own_addr
);
420 /* leave name type & instance as zeroes */
422 msg_set_orignode(rmsg
, msg_destnode(msg
));
423 msg_set_nametype(rmsg
, msg_nametype(msg
));
424 msg_set_nameinst(rmsg
, msg_nameinst(msg
));
426 msg_set_size(rmsg
, data_sz
+ hdr_sz
);
427 skb_copy_to_linear_data_offset(rbuf
, hdr_sz
, msg_data(msg
), data_sz
);
429 /* send self-abort message when rejecting on a connected port */
430 if (msg_connected(msg
)) {
431 struct sk_buff
*abuf
= NULL
;
432 struct port
*p_ptr
= tipc_port_lock(msg_destport(msg
));
435 if (p_ptr
->publ
.connected
)
436 abuf
= port_build_self_abort_msg(p_ptr
, err
);
437 tipc_port_unlock(p_ptr
);
439 tipc_net_route_msg(abuf
);
442 /* send rejected message */
444 tipc_net_route_msg(rbuf
);
448 int tipc_port_reject_sections(struct port
*p_ptr
, struct tipc_msg
*hdr
,
449 struct iovec
const *msg_sect
, u32 num_sect
,
455 res
= tipc_msg_build(hdr
, msg_sect
, num_sect
, MAX_MSG_SIZE
,
456 !p_ptr
->user_port
, &buf
);
460 return tipc_reject_msg(buf
, err
);
463 static void port_timeout(unsigned long ref
)
465 struct port
*p_ptr
= tipc_port_lock(ref
);
466 struct sk_buff
*buf
= NULL
;
471 if (!p_ptr
->publ
.connected
) {
472 tipc_port_unlock(p_ptr
);
476 /* Last probe answered ? */
477 if (p_ptr
->probing_state
== PROBING
) {
478 buf
= port_build_self_abort_msg(p_ptr
, TIPC_ERR_NO_PORT
);
480 buf
= port_build_proto_msg(port_peerport(p_ptr
),
481 port_peernode(p_ptr
),
487 port_out_seqno(p_ptr
),
489 port_incr_out_seqno(p_ptr
);
490 p_ptr
->probing_state
= PROBING
;
491 k_start_timer(&p_ptr
->timer
, p_ptr
->probing_interval
);
493 tipc_port_unlock(p_ptr
);
494 tipc_net_route_msg(buf
);
498 static void port_handle_node_down(unsigned long ref
)
500 struct port
*p_ptr
= tipc_port_lock(ref
);
501 struct sk_buff
* buf
= NULL
;
505 buf
= port_build_self_abort_msg(p_ptr
, TIPC_ERR_NO_NODE
);
506 tipc_port_unlock(p_ptr
);
507 tipc_net_route_msg(buf
);
511 static struct sk_buff
*port_build_self_abort_msg(struct port
*p_ptr
, u32 err
)
513 u32 imp
= msg_importance(&p_ptr
->publ
.phdr
);
515 if (!p_ptr
->publ
.connected
)
517 if (imp
< TIPC_CRITICAL_IMPORTANCE
)
519 return port_build_proto_msg(p_ptr
->publ
.ref
,
521 port_peerport(p_ptr
),
522 port_peernode(p_ptr
),
526 p_ptr
->last_in_seqno
+ 1,
531 static struct sk_buff
*port_build_peer_abort_msg(struct port
*p_ptr
, u32 err
)
533 u32 imp
= msg_importance(&p_ptr
->publ
.phdr
);
535 if (!p_ptr
->publ
.connected
)
537 if (imp
< TIPC_CRITICAL_IMPORTANCE
)
539 return port_build_proto_msg(port_peerport(p_ptr
),
540 port_peernode(p_ptr
),
546 port_out_seqno(p_ptr
),
550 void tipc_port_recv_proto_msg(struct sk_buff
*buf
)
552 struct tipc_msg
*msg
= buf_msg(buf
);
553 struct port
*p_ptr
= tipc_port_lock(msg_destport(msg
));
555 struct sk_buff
*r_buf
= NULL
;
556 struct sk_buff
*abort_buf
= NULL
;
558 msg_dbg(msg
, "PORT<RECV<:");
561 err
= TIPC_ERR_NO_PORT
;
562 } else if (p_ptr
->publ
.connected
) {
563 if ((port_peernode(p_ptr
) != msg_orignode(msg
)) ||
564 (port_peerport(p_ptr
) != msg_origport(msg
))) {
565 err
= TIPC_ERR_NO_PORT
;
566 } else if (msg_type(msg
) == CONN_ACK
) {
567 int wakeup
= tipc_port_congested(p_ptr
) &&
568 p_ptr
->publ
.congested
&&
570 p_ptr
->acked
+= msg_msgcnt(msg
);
571 if (tipc_port_congested(p_ptr
))
573 p_ptr
->publ
.congested
= 0;
576 p_ptr
->wakeup(&p_ptr
->publ
);
579 } else if (p_ptr
->publ
.published
) {
580 err
= TIPC_ERR_NO_PORT
;
583 r_buf
= port_build_proto_msg(msg_origport(msg
),
587 TIPC_HIGH_IMPORTANCE
,
596 if (msg_type(msg
) == CONN_PROBE
) {
597 r_buf
= port_build_proto_msg(msg_origport(msg
),
604 port_out_seqno(p_ptr
),
607 p_ptr
->probing_state
= CONFIRMED
;
608 port_incr_out_seqno(p_ptr
);
611 tipc_port_unlock(p_ptr
);
612 tipc_net_route_msg(r_buf
);
613 tipc_net_route_msg(abort_buf
);
617 static void port_print(struct port
*p_ptr
, struct print_buf
*buf
, int full_id
)
619 struct publication
*publ
;
622 tipc_printf(buf
, "<%u.%u.%u:%u>:",
623 tipc_zone(tipc_own_addr
), tipc_cluster(tipc_own_addr
),
624 tipc_node(tipc_own_addr
), p_ptr
->publ
.ref
);
626 tipc_printf(buf
, "%-10u:", p_ptr
->publ
.ref
);
628 if (p_ptr
->publ
.connected
) {
629 u32 dport
= port_peerport(p_ptr
);
630 u32 destnode
= port_peernode(p_ptr
);
632 tipc_printf(buf
, " connected to <%u.%u.%u:%u>",
633 tipc_zone(destnode
), tipc_cluster(destnode
),
634 tipc_node(destnode
), dport
);
635 if (p_ptr
->publ
.conn_type
!= 0)
636 tipc_printf(buf
, " via {%u,%u}",
637 p_ptr
->publ
.conn_type
,
638 p_ptr
->publ
.conn_instance
);
640 else if (p_ptr
->publ
.published
) {
641 tipc_printf(buf
, " bound to");
642 list_for_each_entry(publ
, &p_ptr
->publications
, pport_list
) {
643 if (publ
->lower
== publ
->upper
)
644 tipc_printf(buf
, " {%u,%u}", publ
->type
,
647 tipc_printf(buf
, " {%u,%u,%u}", publ
->type
,
648 publ
->lower
, publ
->upper
);
651 tipc_printf(buf
, "\n");
654 #define MAX_PORT_QUERY 32768
656 struct sk_buff
*tipc_port_get_ports(void)
659 struct tlv_desc
*rep_tlv
;
664 buf
= tipc_cfg_reply_alloc(TLV_SPACE(MAX_PORT_QUERY
));
667 rep_tlv
= (struct tlv_desc
*)buf
->data
;
669 tipc_printbuf_init(&pb
, TLV_DATA(rep_tlv
), MAX_PORT_QUERY
);
670 spin_lock_bh(&tipc_port_list_lock
);
671 list_for_each_entry(p_ptr
, &ports
, port_list
) {
672 spin_lock_bh(p_ptr
->publ
.lock
);
673 port_print(p_ptr
, &pb
, 0);
674 spin_unlock_bh(p_ptr
->publ
.lock
);
676 spin_unlock_bh(&tipc_port_list_lock
);
677 str_len
= tipc_printbuf_validate(&pb
);
679 skb_put(buf
, TLV_SPACE(str_len
));
680 TLV_SET(rep_tlv
, TIPC_TLV_ULTRA_STRING
, NULL
, str_len
);
685 void tipc_port_reinit(void)
688 struct tipc_msg
*msg
;
690 spin_lock_bh(&tipc_port_list_lock
);
691 list_for_each_entry(p_ptr
, &ports
, port_list
) {
692 msg
= &p_ptr
->publ
.phdr
;
693 if (msg_orignode(msg
) == tipc_own_addr
)
695 msg_set_prevnode(msg
, tipc_own_addr
);
696 msg_set_orignode(msg
, tipc_own_addr
);
698 spin_unlock_bh(&tipc_port_list_lock
);
703 * port_dispatcher_sigh(): Signal handler for messages destinated
704 * to the tipc_port interface.
707 static void port_dispatcher_sigh(void *dummy
)
711 spin_lock_bh(&queue_lock
);
712 buf
= msg_queue_head
;
713 msg_queue_head
= NULL
;
714 spin_unlock_bh(&queue_lock
);
718 struct user_port
*up_ptr
;
719 struct tipc_portid orig
;
720 struct tipc_name_seq dseq
;
726 struct sk_buff
*next
= buf
->next
;
727 struct tipc_msg
*msg
= buf_msg(buf
);
728 u32 dref
= msg_destport(msg
);
730 message_type
= msg_type(msg
);
731 if (message_type
> TIPC_DIRECT_MSG
)
732 goto reject
; /* Unsupported message type */
734 p_ptr
= tipc_port_lock(dref
);
736 goto reject
; /* Port deleted while msg in queue */
738 orig
.ref
= msg_origport(msg
);
739 orig
.node
= msg_orignode(msg
);
740 up_ptr
= p_ptr
->user_port
;
741 usr_handle
= up_ptr
->usr_handle
;
742 connected
= p_ptr
->publ
.connected
;
743 published
= p_ptr
->publ
.published
;
745 if (unlikely(msg_errcode(msg
)))
748 switch (message_type
) {
751 tipc_conn_msg_event cb
= up_ptr
->conn_msg_cb
;
752 u32 peer_port
= port_peerport(p_ptr
);
753 u32 peer_node
= port_peernode(p_ptr
);
755 tipc_port_unlock(p_ptr
);
758 if (unlikely(!connected
)) {
759 if (tipc_connect2port(dref
, &orig
))
761 } else if ((msg_origport(msg
) != peer_port
) ||
762 (msg_orignode(msg
) != peer_node
))
764 if (unlikely(++p_ptr
->publ
.conn_unacked
>=
765 TIPC_FLOW_CONTROL_WIN
))
766 tipc_acknowledge(dref
,
767 p_ptr
->publ
.conn_unacked
);
768 skb_pull(buf
, msg_hdr_sz(msg
));
769 cb(usr_handle
, dref
, &buf
, msg_data(msg
),
773 case TIPC_DIRECT_MSG
:{
774 tipc_msg_event cb
= up_ptr
->msg_cb
;
776 tipc_port_unlock(p_ptr
);
777 if (unlikely(!cb
|| connected
))
779 skb_pull(buf
, msg_hdr_sz(msg
));
780 cb(usr_handle
, dref
, &buf
, msg_data(msg
),
781 msg_data_sz(msg
), msg_importance(msg
),
786 case TIPC_NAMED_MSG
:{
787 tipc_named_msg_event cb
= up_ptr
->named_msg_cb
;
789 tipc_port_unlock(p_ptr
);
790 if (unlikely(!cb
|| connected
|| !published
))
792 dseq
.type
= msg_nametype(msg
);
793 dseq
.lower
= msg_nameinst(msg
);
794 dseq
.upper
= (message_type
== TIPC_NAMED_MSG
)
795 ? dseq
.lower
: msg_nameupper(msg
);
796 skb_pull(buf
, msg_hdr_sz(msg
));
797 cb(usr_handle
, dref
, &buf
, msg_data(msg
),
798 msg_data_sz(msg
), msg_importance(msg
),
808 switch (message_type
) {
811 tipc_conn_shutdown_event cb
=
813 u32 peer_port
= port_peerport(p_ptr
);
814 u32 peer_node
= port_peernode(p_ptr
);
816 tipc_port_unlock(p_ptr
);
817 if (!cb
|| !connected
)
819 if ((msg_origport(msg
) != peer_port
) ||
820 (msg_orignode(msg
) != peer_node
))
822 tipc_disconnect(dref
);
823 skb_pull(buf
, msg_hdr_sz(msg
));
824 cb(usr_handle
, dref
, &buf
, msg_data(msg
),
825 msg_data_sz(msg
), msg_errcode(msg
));
828 case TIPC_DIRECT_MSG
:{
829 tipc_msg_err_event cb
= up_ptr
->err_cb
;
831 tipc_port_unlock(p_ptr
);
832 if (!cb
|| connected
)
834 skb_pull(buf
, msg_hdr_sz(msg
));
835 cb(usr_handle
, dref
, &buf
, msg_data(msg
),
836 msg_data_sz(msg
), msg_errcode(msg
), &orig
);
840 case TIPC_NAMED_MSG
:{
841 tipc_named_msg_err_event cb
=
842 up_ptr
->named_err_cb
;
844 tipc_port_unlock(p_ptr
);
845 if (!cb
|| connected
)
847 dseq
.type
= msg_nametype(msg
);
848 dseq
.lower
= msg_nameinst(msg
);
849 dseq
.upper
= (message_type
== TIPC_NAMED_MSG
)
850 ? dseq
.lower
: msg_nameupper(msg
);
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
), &dseq
);
862 tipc_reject_msg(buf
, TIPC_ERR_NO_PORT
);
868 * port_dispatcher(): Dispatcher for messages destinated
869 * to the tipc_port interface. Called with port locked.
872 static u32
port_dispatcher(struct tipc_port
*dummy
, struct sk_buff
*buf
)
875 spin_lock_bh(&queue_lock
);
876 if (msg_queue_head
) {
877 msg_queue_tail
->next
= buf
;
878 msg_queue_tail
= buf
;
880 msg_queue_tail
= msg_queue_head
= buf
;
881 tipc_k_signal((Handler
)port_dispatcher_sigh
, 0);
883 spin_unlock_bh(&queue_lock
);
888 * Wake up port after congestion: Called with port locked,
892 static void port_wakeup_sh(unsigned long ref
)
895 struct user_port
*up_ptr
;
896 tipc_continue_event cb
= NULL
;
899 p_ptr
= tipc_port_lock(ref
);
901 up_ptr
= p_ptr
->user_port
;
903 cb
= up_ptr
->continue_event_cb
;
904 uh
= up_ptr
->usr_handle
;
906 tipc_port_unlock(p_ptr
);
913 static void port_wakeup(struct tipc_port
*p_ptr
)
915 tipc_k_signal((Handler
)port_wakeup_sh
, p_ptr
->ref
);
918 void tipc_acknowledge(u32 ref
, u32 ack
)
921 struct sk_buff
*buf
= NULL
;
923 p_ptr
= tipc_port_lock(ref
);
926 if (p_ptr
->publ
.connected
) {
927 p_ptr
->publ
.conn_unacked
-= ack
;
928 buf
= port_build_proto_msg(port_peerport(p_ptr
),
929 port_peernode(p_ptr
),
935 port_out_seqno(p_ptr
),
938 tipc_port_unlock(p_ptr
);
939 tipc_net_route_msg(buf
);
943 * tipc_createport(): user level call. Will add port to
944 * registry if non-zero user_ref.
947 int tipc_createport(u32 user_ref
,
949 unsigned int importance
,
950 tipc_msg_err_event error_cb
,
951 tipc_named_msg_err_event named_error_cb
,
952 tipc_conn_shutdown_event conn_error_cb
,
953 tipc_msg_event msg_cb
,
954 tipc_named_msg_event named_msg_cb
,
955 tipc_conn_msg_event conn_msg_cb
,
956 tipc_continue_event continue_event_cb
,/* May be zero */
959 struct user_port
*up_ptr
;
962 up_ptr
= kmalloc(sizeof(*up_ptr
), GFP_ATOMIC
);
964 warn("Port creation failed, no memory\n");
967 p_ptr
= (struct port
*)tipc_createport_raw(NULL
, port_dispatcher
,
968 port_wakeup
, importance
);
974 p_ptr
->user_port
= up_ptr
;
975 up_ptr
->user_ref
= user_ref
;
976 up_ptr
->usr_handle
= usr_handle
;
977 up_ptr
->ref
= p_ptr
->publ
.ref
;
978 up_ptr
->err_cb
= error_cb
;
979 up_ptr
->named_err_cb
= named_error_cb
;
980 up_ptr
->conn_err_cb
= conn_error_cb
;
981 up_ptr
->msg_cb
= msg_cb
;
982 up_ptr
->named_msg_cb
= named_msg_cb
;
983 up_ptr
->conn_msg_cb
= conn_msg_cb
;
984 up_ptr
->continue_event_cb
= continue_event_cb
;
985 INIT_LIST_HEAD(&up_ptr
->uport_list
);
986 tipc_reg_add_port(up_ptr
);
987 *portref
= p_ptr
->publ
.ref
;
988 tipc_port_unlock(p_ptr
);
992 int tipc_ownidentity(u32 ref
, struct tipc_portid
*id
)
995 id
->node
= tipc_own_addr
;
999 int tipc_portimportance(u32 ref
, unsigned int *importance
)
1003 p_ptr
= tipc_port_lock(ref
);
1006 *importance
= (unsigned int)msg_importance(&p_ptr
->publ
.phdr
);
1007 tipc_port_unlock(p_ptr
);
1011 int tipc_set_portimportance(u32 ref
, unsigned int imp
)
1015 if (imp
> TIPC_CRITICAL_IMPORTANCE
)
1018 p_ptr
= tipc_port_lock(ref
);
1021 msg_set_importance(&p_ptr
->publ
.phdr
, (u32
)imp
);
1022 tipc_port_unlock(p_ptr
);
1027 int tipc_publish(u32 ref
, unsigned int scope
, struct tipc_name_seq
const *seq
)
1030 struct publication
*publ
;
1034 p_ptr
= tipc_port_lock(ref
);
1038 dbg("tipc_publ %u, p_ptr = %x, conn = %x, scope = %x, "
1039 "lower = %u, upper = %u\n",
1040 ref
, p_ptr
, p_ptr
->publ
.connected
, scope
, seq
->lower
, seq
->upper
);
1041 if (p_ptr
->publ
.connected
)
1043 if (seq
->lower
> seq
->upper
)
1045 if ((scope
< TIPC_ZONE_SCOPE
) || (scope
> TIPC_NODE_SCOPE
))
1047 key
= ref
+ p_ptr
->pub_count
+ 1;
1052 publ
= tipc_nametbl_publish(seq
->type
, seq
->lower
, seq
->upper
,
1053 scope
, p_ptr
->publ
.ref
, key
);
1055 list_add(&publ
->pport_list
, &p_ptr
->publications
);
1057 p_ptr
->publ
.published
= 1;
1061 tipc_port_unlock(p_ptr
);
1065 int tipc_withdraw(u32 ref
, unsigned int scope
, struct tipc_name_seq
const *seq
)
1068 struct publication
*publ
;
1069 struct publication
*tpubl
;
1072 p_ptr
= tipc_port_lock(ref
);
1076 list_for_each_entry_safe(publ
, tpubl
,
1077 &p_ptr
->publications
, pport_list
) {
1078 tipc_nametbl_withdraw(publ
->type
, publ
->lower
,
1079 publ
->ref
, publ
->key
);
1083 list_for_each_entry_safe(publ
, tpubl
,
1084 &p_ptr
->publications
, pport_list
) {
1085 if (publ
->scope
!= scope
)
1087 if (publ
->type
!= seq
->type
)
1089 if (publ
->lower
!= seq
->lower
)
1091 if (publ
->upper
!= seq
->upper
)
1093 tipc_nametbl_withdraw(publ
->type
, publ
->lower
,
1094 publ
->ref
, publ
->key
);
1099 if (list_empty(&p_ptr
->publications
))
1100 p_ptr
->publ
.published
= 0;
1101 tipc_port_unlock(p_ptr
);
1105 int tipc_connect2port(u32 ref
, struct tipc_portid
const *peer
)
1108 struct tipc_msg
*msg
;
1111 p_ptr
= tipc_port_lock(ref
);
1114 if (p_ptr
->publ
.published
|| p_ptr
->publ
.connected
)
1119 msg
= &p_ptr
->publ
.phdr
;
1120 msg_set_destnode(msg
, peer
->node
);
1121 msg_set_destport(msg
, peer
->ref
);
1122 msg_set_orignode(msg
, tipc_own_addr
);
1123 msg_set_origport(msg
, p_ptr
->publ
.ref
);
1124 msg_set_transp_seqno(msg
, 42);
1125 msg_set_type(msg
, TIPC_CONN_MSG
);
1126 if (!may_route(peer
->node
))
1127 msg_set_hdr_sz(msg
, SHORT_H_SIZE
);
1129 msg_set_hdr_sz(msg
, LONG_H_SIZE
);
1131 p_ptr
->probing_interval
= PROBING_INTERVAL
;
1132 p_ptr
->probing_state
= CONFIRMED
;
1133 p_ptr
->publ
.connected
= 1;
1134 k_start_timer(&p_ptr
->timer
, p_ptr
->probing_interval
);
1136 tipc_nodesub_subscribe(&p_ptr
->subscription
,peer
->node
,
1137 (void *)(unsigned long)ref
,
1138 (net_ev_handler
)port_handle_node_down
);
1141 tipc_port_unlock(p_ptr
);
1142 p_ptr
->publ
.max_pkt
= tipc_link_get_max_pkt(peer
->node
, ref
);
1147 * tipc_disconnect_port - disconnect port from peer
1149 * Port must be locked.
1152 int tipc_disconnect_port(struct tipc_port
*tp_ptr
)
1156 if (tp_ptr
->connected
) {
1157 tp_ptr
->connected
= 0;
1158 /* let timer expire on it's own to avoid deadlock! */
1159 tipc_nodesub_unsubscribe(
1160 &((struct port
*)tp_ptr
)->subscription
);
1169 * tipc_disconnect(): Disconnect port form peer.
1170 * This is a node local operation.
1173 int tipc_disconnect(u32 ref
)
1178 p_ptr
= tipc_port_lock(ref
);
1181 res
= tipc_disconnect_port((struct tipc_port
*)p_ptr
);
1182 tipc_port_unlock(p_ptr
);
1187 * tipc_shutdown(): Send a SHUTDOWN msg to peer and disconnect
1189 int tipc_shutdown(u32 ref
)
1192 struct sk_buff
*buf
= NULL
;
1194 p_ptr
= tipc_port_lock(ref
);
1198 if (p_ptr
->publ
.connected
) {
1199 u32 imp
= msg_importance(&p_ptr
->publ
.phdr
);
1200 if (imp
< TIPC_CRITICAL_IMPORTANCE
)
1202 buf
= port_build_proto_msg(port_peerport(p_ptr
),
1203 port_peernode(p_ptr
),
1209 port_out_seqno(p_ptr
),
1212 tipc_port_unlock(p_ptr
);
1213 tipc_net_route_msg(buf
);
1214 return tipc_disconnect(ref
);
1218 * tipc_port_recv_sections(): Concatenate and deliver sectioned
1219 * message for this node.
1222 static int tipc_port_recv_sections(struct port
*sender
, unsigned int num_sect
,
1223 struct iovec
const *msg_sect
)
1225 struct sk_buff
*buf
;
1228 res
= tipc_msg_build(&sender
->publ
.phdr
, msg_sect
, num_sect
,
1229 MAX_MSG_SIZE
, !sender
->user_port
, &buf
);
1231 tipc_port_recv_msg(buf
);
1236 * tipc_send - send message sections on connection
1239 int tipc_send(u32 ref
, unsigned int num_sect
, struct iovec
const *msg_sect
)
1245 p_ptr
= tipc_port_deref(ref
);
1246 if (!p_ptr
|| !p_ptr
->publ
.connected
)
1249 p_ptr
->publ
.congested
= 1;
1250 if (!tipc_port_congested(p_ptr
)) {
1251 destnode
= port_peernode(p_ptr
);
1252 if (likely(destnode
!= tipc_own_addr
))
1253 res
= tipc_link_send_sections_fast(p_ptr
, msg_sect
, num_sect
,
1256 res
= tipc_port_recv_sections(p_ptr
, num_sect
, msg_sect
);
1258 if (likely(res
!= -ELINKCONG
)) {
1259 port_incr_out_seqno(p_ptr
);
1260 p_ptr
->publ
.congested
= 0;
1265 if (port_unreliable(p_ptr
)) {
1266 p_ptr
->publ
.congested
= 0;
1267 /* Just calculate msg length and return */
1268 return tipc_msg_calc_data_size(msg_sect
, num_sect
);
1274 * tipc_forward2name - forward message sections to port name
1277 static int tipc_forward2name(u32 ref
,
1278 struct tipc_name
const *name
,
1281 struct iovec
const *msg_sect
,
1282 struct tipc_portid
const *orig
,
1283 unsigned int importance
)
1286 struct tipc_msg
*msg
;
1287 u32 destnode
= domain
;
1291 p_ptr
= tipc_port_deref(ref
);
1292 if (!p_ptr
|| p_ptr
->publ
.connected
)
1295 msg
= &p_ptr
->publ
.phdr
;
1296 msg_set_type(msg
, TIPC_NAMED_MSG
);
1297 msg_set_orignode(msg
, orig
->node
);
1298 msg_set_origport(msg
, orig
->ref
);
1299 msg_set_hdr_sz(msg
, LONG_H_SIZE
);
1300 msg_set_nametype(msg
, name
->type
);
1301 msg_set_nameinst(msg
, name
->instance
);
1302 msg_set_lookup_scope(msg
, tipc_addr_scope(domain
));
1303 if (importance
<= TIPC_CRITICAL_IMPORTANCE
)
1304 msg_set_importance(msg
,importance
);
1305 destport
= tipc_nametbl_translate(name
->type
, name
->instance
, &destnode
);
1306 msg_set_destnode(msg
, destnode
);
1307 msg_set_destport(msg
, destport
);
1309 if (likely(destport
)) {
1311 if (likely(destnode
== tipc_own_addr
))
1312 return tipc_port_recv_sections(p_ptr
, num_sect
, msg_sect
);
1313 res
= tipc_link_send_sections_fast(p_ptr
, msg_sect
, num_sect
,
1315 if (likely(res
!= -ELINKCONG
))
1317 if (port_unreliable(p_ptr
)) {
1318 /* Just calculate msg length and return */
1319 return tipc_msg_calc_data_size(msg_sect
, num_sect
);
1323 return tipc_port_reject_sections(p_ptr
, msg
, msg_sect
, num_sect
,
1328 * tipc_send2name - send message sections to port name
1331 int tipc_send2name(u32 ref
,
1332 struct tipc_name
const *name
,
1333 unsigned int domain
,
1334 unsigned int num_sect
,
1335 struct iovec
const *msg_sect
)
1337 struct tipc_portid orig
;
1340 orig
.node
= tipc_own_addr
;
1341 return tipc_forward2name(ref
, name
, domain
, num_sect
, msg_sect
, &orig
,
1342 TIPC_PORT_IMPORTANCE
);
1346 * tipc_forward2port - forward message sections to port identity
1349 static int tipc_forward2port(u32 ref
,
1350 struct tipc_portid
const *dest
,
1351 unsigned int num_sect
,
1352 struct iovec
const *msg_sect
,
1353 struct tipc_portid
const *orig
,
1354 unsigned int importance
)
1357 struct tipc_msg
*msg
;
1360 p_ptr
= tipc_port_deref(ref
);
1361 if (!p_ptr
|| p_ptr
->publ
.connected
)
1364 msg
= &p_ptr
->publ
.phdr
;
1365 msg_set_type(msg
, TIPC_DIRECT_MSG
);
1366 msg_set_orignode(msg
, orig
->node
);
1367 msg_set_origport(msg
, orig
->ref
);
1368 msg_set_destnode(msg
, dest
->node
);
1369 msg_set_destport(msg
, dest
->ref
);
1370 msg_set_hdr_sz(msg
, DIR_MSG_H_SIZE
);
1371 if (importance
<= TIPC_CRITICAL_IMPORTANCE
)
1372 msg_set_importance(msg
, importance
);
1374 if (dest
->node
== tipc_own_addr
)
1375 return tipc_port_recv_sections(p_ptr
, num_sect
, msg_sect
);
1376 res
= tipc_link_send_sections_fast(p_ptr
, msg_sect
, num_sect
, dest
->node
);
1377 if (likely(res
!= -ELINKCONG
))
1379 if (port_unreliable(p_ptr
)) {
1380 /* Just calculate msg length and return */
1381 return tipc_msg_calc_data_size(msg_sect
, num_sect
);
1387 * tipc_send2port - send message sections to port identity
1390 int tipc_send2port(u32 ref
,
1391 struct tipc_portid
const *dest
,
1392 unsigned int num_sect
,
1393 struct iovec
const *msg_sect
)
1395 struct tipc_portid orig
;
1398 orig
.node
= tipc_own_addr
;
1399 return tipc_forward2port(ref
, dest
, num_sect
, msg_sect
, &orig
,
1400 TIPC_PORT_IMPORTANCE
);
1404 * tipc_forward_buf2port - forward message buffer to port identity
1406 static int tipc_forward_buf2port(u32 ref
,
1407 struct tipc_portid
const *dest
,
1408 struct sk_buff
*buf
,
1410 struct tipc_portid
const *orig
,
1411 unsigned int importance
)
1414 struct tipc_msg
*msg
;
1417 p_ptr
= (struct port
*)tipc_ref_deref(ref
);
1418 if (!p_ptr
|| p_ptr
->publ
.connected
)
1421 msg
= &p_ptr
->publ
.phdr
;
1422 msg_set_type(msg
, TIPC_DIRECT_MSG
);
1423 msg_set_orignode(msg
, orig
->node
);
1424 msg_set_origport(msg
, orig
->ref
);
1425 msg_set_destnode(msg
, dest
->node
);
1426 msg_set_destport(msg
, dest
->ref
);
1427 msg_set_hdr_sz(msg
, DIR_MSG_H_SIZE
);
1428 if (importance
<= TIPC_CRITICAL_IMPORTANCE
)
1429 msg_set_importance(msg
, importance
);
1430 msg_set_size(msg
, DIR_MSG_H_SIZE
+ dsz
);
1431 if (skb_cow(buf
, DIR_MSG_H_SIZE
))
1434 skb_push(buf
, DIR_MSG_H_SIZE
);
1435 skb_copy_to_linear_data(buf
, msg
, DIR_MSG_H_SIZE
);
1436 msg_dbg(msg
, "buf2port: ");
1438 if (dest
->node
== tipc_own_addr
)
1439 return tipc_port_recv_msg(buf
);
1440 res
= tipc_send_buf_fast(buf
, dest
->node
);
1441 if (likely(res
!= -ELINKCONG
))
1443 if (port_unreliable(p_ptr
))
1449 * tipc_send_buf2port - send message buffer to port identity
1452 int tipc_send_buf2port(u32 ref
,
1453 struct tipc_portid
const *dest
,
1454 struct sk_buff
*buf
,
1457 struct tipc_portid orig
;
1460 orig
.node
= tipc_own_addr
;
1461 return tipc_forward_buf2port(ref
, dest
, buf
, dsz
, &orig
,
1462 TIPC_PORT_IMPORTANCE
);