2 * net/tipc/port.c: TIPC port code
4 * Copyright (c) 1992-2007, Ericsson AB
5 * Copyright (c) 2004-2008, 2010-2011, 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.
40 #include "name_table.h"
42 /* Connection management: */
43 #define PROBING_INTERVAL 3600000 /* [ms] => 1 h */
47 #define MAX_REJECT_SIZE 1024
49 static struct sk_buff
*msg_queue_head
;
50 static struct sk_buff
*msg_queue_tail
;
52 DEFINE_SPINLOCK(tipc_port_list_lock
);
53 static DEFINE_SPINLOCK(queue_lock
);
55 static LIST_HEAD(ports
);
56 static void port_handle_node_down(unsigned long ref
);
57 static struct sk_buff
*port_build_self_abort_msg(struct tipc_port
*, u32 err
);
58 static struct sk_buff
*port_build_peer_abort_msg(struct tipc_port
*, u32 err
);
59 static void port_timeout(unsigned long ref
);
62 static u32
port_peernode(struct tipc_port
*p_ptr
)
64 return msg_destnode(&p_ptr
->phdr
);
67 static u32
port_peerport(struct tipc_port
*p_ptr
)
69 return msg_destport(&p_ptr
->phdr
);
73 * tipc_multicast - send a multicast message to local and remote destinations
76 int tipc_multicast(u32 ref
, struct tipc_name_seq
const *seq
,
77 u32 num_sect
, struct iovec
const *msg_sect
,
78 unsigned int total_len
)
82 struct sk_buff
*ibuf
= NULL
;
83 struct port_list dports
= {0, NULL
, };
84 struct tipc_port
*oport
= tipc_port_deref(ref
);
91 /* Create multicast message */
94 msg_set_type(hdr
, TIPC_MCAST_MSG
);
95 msg_set_lookup_scope(hdr
, TIPC_CLUSTER_SCOPE
);
96 msg_set_destport(hdr
, 0);
97 msg_set_destnode(hdr
, 0);
98 msg_set_nametype(hdr
, seq
->type
);
99 msg_set_namelower(hdr
, seq
->lower
);
100 msg_set_nameupper(hdr
, seq
->upper
);
101 msg_set_hdr_sz(hdr
, MCAST_H_SIZE
);
102 res
= tipc_msg_build(hdr
, msg_sect
, num_sect
, total_len
, MAX_MSG_SIZE
,
103 !oport
->user_port
, &buf
);
107 /* Figure out where to send multicast message */
109 ext_targets
= tipc_nametbl_mc_translate(seq
->type
, seq
->lower
, seq
->upper
,
110 TIPC_NODE_SCOPE
, &dports
);
112 /* Send message to destinations (duplicate it only if necessary) */
115 if (dports
.count
!= 0) {
116 ibuf
= skb_copy(buf
, GFP_ATOMIC
);
118 tipc_port_list_free(&dports
);
123 res
= tipc_bclink_send_msg(buf
);
124 if ((res
< 0) && (dports
.count
!= 0))
132 tipc_port_recv_mcast(ibuf
, &dports
);
134 tipc_port_list_free(&dports
);
140 * tipc_port_recv_mcast - deliver multicast message to all destination ports
142 * If there is no port list, perform a lookup to create one
145 void tipc_port_recv_mcast(struct sk_buff
*buf
, struct port_list
*dp
)
147 struct tipc_msg
*msg
;
148 struct port_list dports
= {0, NULL
, };
149 struct port_list
*item
= dp
;
154 /* Create destination port list, if one wasn't supplied */
157 tipc_nametbl_mc_translate(msg_nametype(msg
),
165 /* Deliver a copy of message to each destination port */
167 if (dp
->count
!= 0) {
168 msg_set_destnode(msg
, tipc_own_addr
);
169 if (dp
->count
== 1) {
170 msg_set_destport(msg
, dp
->ports
[0]);
171 tipc_port_recv_msg(buf
);
172 tipc_port_list_free(dp
);
175 for (; cnt
< dp
->count
; cnt
++) {
176 int index
= cnt
% PLSIZE
;
177 struct sk_buff
*b
= skb_clone(buf
, GFP_ATOMIC
);
180 warn("Unable to deliver multicast message(s)\n");
183 if ((index
== 0) && (cnt
!= 0))
185 msg_set_destport(buf_msg(b
), item
->ports
[index
]);
186 tipc_port_recv_msg(b
);
191 tipc_port_list_free(dp
);
195 * tipc_createport_raw - create a generic TIPC port
197 * Returns pointer to (locked) TIPC port, or NULL if unable to create it
200 struct tipc_port
*tipc_createport_raw(void *usr_handle
,
201 u32 (*dispatcher
)(struct tipc_port
*, struct sk_buff
*),
202 void (*wakeup
)(struct tipc_port
*),
203 const u32 importance
)
205 struct tipc_port
*p_ptr
;
206 struct tipc_msg
*msg
;
209 p_ptr
= kzalloc(sizeof(*p_ptr
), GFP_ATOMIC
);
211 warn("Port creation failed, no memory\n");
214 ref
= tipc_ref_acquire(p_ptr
, &p_ptr
->lock
);
216 warn("Port creation failed, reference table exhausted\n");
221 p_ptr
->usr_handle
= usr_handle
;
222 p_ptr
->max_pkt
= MAX_PKT_DEFAULT
;
225 tipc_msg_init(msg
, importance
, TIPC_NAMED_MSG
, LONG_H_SIZE
, 0);
226 msg_set_origport(msg
, ref
);
227 INIT_LIST_HEAD(&p_ptr
->wait_list
);
228 INIT_LIST_HEAD(&p_ptr
->subscription
.nodesub_list
);
229 p_ptr
->dispatcher
= dispatcher
;
230 p_ptr
->wakeup
= wakeup
;
231 p_ptr
->user_port
= NULL
;
232 k_init_timer(&p_ptr
->timer
, (Handler
)port_timeout
, ref
);
233 spin_lock_bh(&tipc_port_list_lock
);
234 INIT_LIST_HEAD(&p_ptr
->publications
);
235 INIT_LIST_HEAD(&p_ptr
->port_list
);
236 list_add_tail(&p_ptr
->port_list
, &ports
);
237 spin_unlock_bh(&tipc_port_list_lock
);
241 int tipc_deleteport(u32 ref
)
243 struct tipc_port
*p_ptr
;
244 struct sk_buff
*buf
= NULL
;
246 tipc_withdraw(ref
, 0, NULL
);
247 p_ptr
= tipc_port_lock(ref
);
251 tipc_ref_discard(ref
);
252 tipc_port_unlock(p_ptr
);
254 k_cancel_timer(&p_ptr
->timer
);
255 if (p_ptr
->connected
) {
256 buf
= port_build_peer_abort_msg(p_ptr
, TIPC_ERR_NO_PORT
);
257 tipc_nodesub_unsubscribe(&p_ptr
->subscription
);
259 kfree(p_ptr
->user_port
);
261 spin_lock_bh(&tipc_port_list_lock
);
262 list_del(&p_ptr
->port_list
);
263 list_del(&p_ptr
->wait_list
);
264 spin_unlock_bh(&tipc_port_list_lock
);
265 k_term_timer(&p_ptr
->timer
);
267 tipc_net_route_msg(buf
);
271 static int port_unreliable(struct tipc_port
*p_ptr
)
273 return msg_src_droppable(&p_ptr
->phdr
);
276 int tipc_portunreliable(u32 ref
, unsigned int *isunreliable
)
278 struct tipc_port
*p_ptr
;
280 p_ptr
= tipc_port_lock(ref
);
283 *isunreliable
= port_unreliable(p_ptr
);
284 tipc_port_unlock(p_ptr
);
288 int tipc_set_portunreliable(u32 ref
, unsigned int isunreliable
)
290 struct tipc_port
*p_ptr
;
292 p_ptr
= tipc_port_lock(ref
);
295 msg_set_src_droppable(&p_ptr
->phdr
, (isunreliable
!= 0));
296 tipc_port_unlock(p_ptr
);
300 static int port_unreturnable(struct tipc_port
*p_ptr
)
302 return msg_dest_droppable(&p_ptr
->phdr
);
305 int tipc_portunreturnable(u32 ref
, unsigned int *isunrejectable
)
307 struct tipc_port
*p_ptr
;
309 p_ptr
= tipc_port_lock(ref
);
312 *isunrejectable
= port_unreturnable(p_ptr
);
313 tipc_port_unlock(p_ptr
);
317 int tipc_set_portunreturnable(u32 ref
, unsigned int isunrejectable
)
319 struct tipc_port
*p_ptr
;
321 p_ptr
= tipc_port_lock(ref
);
324 msg_set_dest_droppable(&p_ptr
->phdr
, (isunrejectable
!= 0));
325 tipc_port_unlock(p_ptr
);
330 * port_build_proto_msg(): build a port level protocol
331 * or a connection abortion message. Called with
334 static struct sk_buff
*port_build_proto_msg(u32 destport
, u32 destnode
,
335 u32 origport
, u32 orignode
,
336 u32 usr
, u32 type
, u32 err
,
340 struct tipc_msg
*msg
;
342 buf
= tipc_buf_acquire(LONG_H_SIZE
);
345 tipc_msg_init(msg
, usr
, type
, LONG_H_SIZE
, destnode
);
346 msg_set_errcode(msg
, err
);
347 msg_set_destport(msg
, destport
);
348 msg_set_origport(msg
, origport
);
349 msg_set_orignode(msg
, orignode
);
350 msg_set_msgcnt(msg
, ack
);
355 int tipc_reject_msg(struct sk_buff
*buf
, u32 err
)
357 struct tipc_msg
*msg
= buf_msg(buf
);
358 struct sk_buff
*rbuf
;
359 struct tipc_msg
*rmsg
;
361 u32 imp
= msg_importance(msg
);
362 u32 data_sz
= msg_data_sz(msg
);
364 if (data_sz
> MAX_REJECT_SIZE
)
365 data_sz
= MAX_REJECT_SIZE
;
366 if (msg_connected(msg
) && (imp
< TIPC_CRITICAL_IMPORTANCE
))
369 /* discard rejected message if it shouldn't be returned to sender */
370 if (msg_errcode(msg
) || msg_dest_droppable(msg
)) {
375 /* construct rejected message */
377 hdr_sz
= MCAST_H_SIZE
;
379 hdr_sz
= LONG_H_SIZE
;
380 rbuf
= tipc_buf_acquire(data_sz
+ hdr_sz
);
385 rmsg
= buf_msg(rbuf
);
386 tipc_msg_init(rmsg
, imp
, msg_type(msg
), hdr_sz
, msg_orignode(msg
));
387 msg_set_errcode(rmsg
, err
);
388 msg_set_destport(rmsg
, msg_origport(msg
));
389 msg_set_origport(rmsg
, msg_destport(msg
));
390 if (msg_short(msg
)) {
391 msg_set_orignode(rmsg
, tipc_own_addr
);
392 /* leave name type & instance as zeroes */
394 msg_set_orignode(rmsg
, msg_destnode(msg
));
395 msg_set_nametype(rmsg
, msg_nametype(msg
));
396 msg_set_nameinst(rmsg
, msg_nameinst(msg
));
398 msg_set_size(rmsg
, data_sz
+ hdr_sz
);
399 skb_copy_to_linear_data_offset(rbuf
, hdr_sz
, msg_data(msg
), data_sz
);
401 /* send self-abort message when rejecting on a connected port */
402 if (msg_connected(msg
)) {
403 struct sk_buff
*abuf
= NULL
;
404 struct tipc_port
*p_ptr
= tipc_port_lock(msg_destport(msg
));
407 if (p_ptr
->connected
)
408 abuf
= port_build_self_abort_msg(p_ptr
, err
);
409 tipc_port_unlock(p_ptr
);
411 tipc_net_route_msg(abuf
);
414 /* send rejected message */
416 tipc_net_route_msg(rbuf
);
420 int tipc_port_reject_sections(struct tipc_port
*p_ptr
, struct tipc_msg
*hdr
,
421 struct iovec
const *msg_sect
, u32 num_sect
,
422 unsigned int total_len
, int err
)
427 res
= tipc_msg_build(hdr
, msg_sect
, num_sect
, total_len
, MAX_MSG_SIZE
,
428 !p_ptr
->user_port
, &buf
);
432 return tipc_reject_msg(buf
, err
);
435 static void port_timeout(unsigned long ref
)
437 struct tipc_port
*p_ptr
= tipc_port_lock(ref
);
438 struct sk_buff
*buf
= NULL
;
443 if (!p_ptr
->connected
) {
444 tipc_port_unlock(p_ptr
);
448 /* Last probe answered ? */
449 if (p_ptr
->probing_state
== PROBING
) {
450 buf
= port_build_self_abort_msg(p_ptr
, TIPC_ERR_NO_PORT
);
452 buf
= port_build_proto_msg(port_peerport(p_ptr
),
453 port_peernode(p_ptr
),
460 p_ptr
->probing_state
= PROBING
;
461 k_start_timer(&p_ptr
->timer
, p_ptr
->probing_interval
);
463 tipc_port_unlock(p_ptr
);
464 tipc_net_route_msg(buf
);
468 static void port_handle_node_down(unsigned long ref
)
470 struct tipc_port
*p_ptr
= tipc_port_lock(ref
);
471 struct sk_buff
*buf
= NULL
;
475 buf
= port_build_self_abort_msg(p_ptr
, TIPC_ERR_NO_NODE
);
476 tipc_port_unlock(p_ptr
);
477 tipc_net_route_msg(buf
);
481 static struct sk_buff
*port_build_self_abort_msg(struct tipc_port
*p_ptr
, u32 err
)
483 u32 imp
= msg_importance(&p_ptr
->phdr
);
485 if (!p_ptr
->connected
)
487 if (imp
< TIPC_CRITICAL_IMPORTANCE
)
489 return port_build_proto_msg(p_ptr
->ref
,
491 port_peerport(p_ptr
),
492 port_peernode(p_ptr
),
500 static struct sk_buff
*port_build_peer_abort_msg(struct tipc_port
*p_ptr
, u32 err
)
502 u32 imp
= msg_importance(&p_ptr
->phdr
);
504 if (!p_ptr
->connected
)
506 if (imp
< TIPC_CRITICAL_IMPORTANCE
)
508 return port_build_proto_msg(port_peerport(p_ptr
),
509 port_peernode(p_ptr
),
518 void tipc_port_recv_proto_msg(struct sk_buff
*buf
)
520 struct tipc_msg
*msg
= buf_msg(buf
);
521 struct tipc_port
*p_ptr
= tipc_port_lock(msg_destport(msg
));
523 struct sk_buff
*r_buf
= NULL
;
524 struct sk_buff
*abort_buf
= NULL
;
527 err
= TIPC_ERR_NO_PORT
;
528 } else if (p_ptr
->connected
) {
529 if ((port_peernode(p_ptr
) != msg_orignode(msg
)) ||
530 (port_peerport(p_ptr
) != msg_origport(msg
))) {
531 err
= TIPC_ERR_NO_PORT
;
532 } else if (msg_type(msg
) == CONN_ACK
) {
533 int wakeup
= tipc_port_congested(p_ptr
) &&
536 p_ptr
->acked
+= msg_msgcnt(msg
);
537 if (tipc_port_congested(p_ptr
))
539 p_ptr
->congested
= 0;
542 p_ptr
->wakeup(p_ptr
);
545 } else if (p_ptr
->published
) {
546 err
= TIPC_ERR_NO_PORT
;
549 r_buf
= port_build_proto_msg(msg_origport(msg
),
553 TIPC_HIGH_IMPORTANCE
,
561 if (msg_type(msg
) == CONN_PROBE
) {
562 r_buf
= port_build_proto_msg(msg_origport(msg
),
571 p_ptr
->probing_state
= CONFIRMED
;
574 tipc_port_unlock(p_ptr
);
575 tipc_net_route_msg(r_buf
);
576 tipc_net_route_msg(abort_buf
);
580 static void port_print(struct tipc_port
*p_ptr
, struct print_buf
*buf
, int full_id
)
582 struct publication
*publ
;
585 tipc_printf(buf
, "<%u.%u.%u:%u>:",
586 tipc_zone(tipc_own_addr
), tipc_cluster(tipc_own_addr
),
587 tipc_node(tipc_own_addr
), p_ptr
->ref
);
589 tipc_printf(buf
, "%-10u:", p_ptr
->ref
);
591 if (p_ptr
->connected
) {
592 u32 dport
= port_peerport(p_ptr
);
593 u32 destnode
= port_peernode(p_ptr
);
595 tipc_printf(buf
, " connected to <%u.%u.%u:%u>",
596 tipc_zone(destnode
), tipc_cluster(destnode
),
597 tipc_node(destnode
), dport
);
598 if (p_ptr
->conn_type
!= 0)
599 tipc_printf(buf
, " via {%u,%u}",
601 p_ptr
->conn_instance
);
602 } else if (p_ptr
->published
) {
603 tipc_printf(buf
, " bound to");
604 list_for_each_entry(publ
, &p_ptr
->publications
, pport_list
) {
605 if (publ
->lower
== publ
->upper
)
606 tipc_printf(buf
, " {%u,%u}", publ
->type
,
609 tipc_printf(buf
, " {%u,%u,%u}", publ
->type
,
610 publ
->lower
, publ
->upper
);
613 tipc_printf(buf
, "\n");
616 #define MAX_PORT_QUERY 32768
618 struct sk_buff
*tipc_port_get_ports(void)
621 struct tlv_desc
*rep_tlv
;
623 struct tipc_port
*p_ptr
;
626 buf
= tipc_cfg_reply_alloc(TLV_SPACE(MAX_PORT_QUERY
));
629 rep_tlv
= (struct tlv_desc
*)buf
->data
;
631 tipc_printbuf_init(&pb
, TLV_DATA(rep_tlv
), MAX_PORT_QUERY
);
632 spin_lock_bh(&tipc_port_list_lock
);
633 list_for_each_entry(p_ptr
, &ports
, port_list
) {
634 spin_lock_bh(p_ptr
->lock
);
635 port_print(p_ptr
, &pb
, 0);
636 spin_unlock_bh(p_ptr
->lock
);
638 spin_unlock_bh(&tipc_port_list_lock
);
639 str_len
= tipc_printbuf_validate(&pb
);
641 skb_put(buf
, TLV_SPACE(str_len
));
642 TLV_SET(rep_tlv
, TIPC_TLV_ULTRA_STRING
, NULL
, str_len
);
647 void tipc_port_reinit(void)
649 struct tipc_port
*p_ptr
;
650 struct tipc_msg
*msg
;
652 spin_lock_bh(&tipc_port_list_lock
);
653 list_for_each_entry(p_ptr
, &ports
, port_list
) {
655 if (msg_orignode(msg
) == tipc_own_addr
)
657 msg_set_prevnode(msg
, tipc_own_addr
);
658 msg_set_orignode(msg
, tipc_own_addr
);
660 spin_unlock_bh(&tipc_port_list_lock
);
665 * port_dispatcher_sigh(): Signal handler for messages destinated
666 * to the tipc_port interface.
669 static void port_dispatcher_sigh(void *dummy
)
673 spin_lock_bh(&queue_lock
);
674 buf
= msg_queue_head
;
675 msg_queue_head
= NULL
;
676 spin_unlock_bh(&queue_lock
);
679 struct tipc_port
*p_ptr
;
680 struct user_port
*up_ptr
;
681 struct tipc_portid orig
;
682 struct tipc_name_seq dseq
;
688 struct sk_buff
*next
= buf
->next
;
689 struct tipc_msg
*msg
= buf_msg(buf
);
690 u32 dref
= msg_destport(msg
);
692 message_type
= msg_type(msg
);
693 if (message_type
> TIPC_DIRECT_MSG
)
694 goto reject
; /* Unsupported message type */
696 p_ptr
= tipc_port_lock(dref
);
698 goto reject
; /* Port deleted while msg in queue */
700 orig
.ref
= msg_origport(msg
);
701 orig
.node
= msg_orignode(msg
);
702 up_ptr
= p_ptr
->user_port
;
703 usr_handle
= up_ptr
->usr_handle
;
704 connected
= p_ptr
->connected
;
705 published
= p_ptr
->published
;
707 if (unlikely(msg_errcode(msg
)))
710 switch (message_type
) {
713 tipc_conn_msg_event cb
= up_ptr
->conn_msg_cb
;
714 u32 peer_port
= port_peerport(p_ptr
);
715 u32 peer_node
= port_peernode(p_ptr
);
718 tipc_port_unlock(p_ptr
);
721 if (unlikely(!connected
)) {
722 if (tipc_connect2port(dref
, &orig
))
724 } else if ((msg_origport(msg
) != peer_port
) ||
725 (msg_orignode(msg
) != peer_node
))
727 dsz
= msg_data_sz(msg
);
729 (++p_ptr
->conn_unacked
>=
730 TIPC_FLOW_CONTROL_WIN
)))
731 tipc_acknowledge(dref
,
732 p_ptr
->conn_unacked
);
733 skb_pull(buf
, msg_hdr_sz(msg
));
734 cb(usr_handle
, dref
, &buf
, msg_data(msg
), dsz
);
737 case TIPC_DIRECT_MSG
:{
738 tipc_msg_event cb
= up_ptr
->msg_cb
;
740 tipc_port_unlock(p_ptr
);
741 if (unlikely(!cb
|| connected
))
743 skb_pull(buf
, msg_hdr_sz(msg
));
744 cb(usr_handle
, dref
, &buf
, msg_data(msg
),
745 msg_data_sz(msg
), msg_importance(msg
),
750 case TIPC_NAMED_MSG
:{
751 tipc_named_msg_event cb
= up_ptr
->named_msg_cb
;
753 tipc_port_unlock(p_ptr
);
754 if (unlikely(!cb
|| connected
|| !published
))
756 dseq
.type
= msg_nametype(msg
);
757 dseq
.lower
= msg_nameinst(msg
);
758 dseq
.upper
= (message_type
== TIPC_NAMED_MSG
)
759 ? dseq
.lower
: msg_nameupper(msg
);
760 skb_pull(buf
, msg_hdr_sz(msg
));
761 cb(usr_handle
, dref
, &buf
, msg_data(msg
),
762 msg_data_sz(msg
), msg_importance(msg
),
772 switch (message_type
) {
775 tipc_conn_shutdown_event cb
=
777 u32 peer_port
= port_peerport(p_ptr
);
778 u32 peer_node
= port_peernode(p_ptr
);
780 tipc_port_unlock(p_ptr
);
781 if (!cb
|| !connected
)
783 if ((msg_origport(msg
) != peer_port
) ||
784 (msg_orignode(msg
) != peer_node
))
786 tipc_disconnect(dref
);
787 skb_pull(buf
, msg_hdr_sz(msg
));
788 cb(usr_handle
, dref
, &buf
, msg_data(msg
),
789 msg_data_sz(msg
), msg_errcode(msg
));
792 case TIPC_DIRECT_MSG
:{
793 tipc_msg_err_event cb
= up_ptr
->err_cb
;
795 tipc_port_unlock(p_ptr
);
796 if (!cb
|| connected
)
798 skb_pull(buf
, msg_hdr_sz(msg
));
799 cb(usr_handle
, dref
, &buf
, msg_data(msg
),
800 msg_data_sz(msg
), msg_errcode(msg
), &orig
);
804 case TIPC_NAMED_MSG
:{
805 tipc_named_msg_err_event cb
=
806 up_ptr
->named_err_cb
;
808 tipc_port_unlock(p_ptr
);
809 if (!cb
|| connected
)
811 dseq
.type
= msg_nametype(msg
);
812 dseq
.lower
= msg_nameinst(msg
);
813 dseq
.upper
= (message_type
== TIPC_NAMED_MSG
)
814 ? dseq
.lower
: msg_nameupper(msg
);
815 skb_pull(buf
, msg_hdr_sz(msg
));
816 cb(usr_handle
, dref
, &buf
, msg_data(msg
),
817 msg_data_sz(msg
), msg_errcode(msg
), &dseq
);
826 tipc_reject_msg(buf
, TIPC_ERR_NO_PORT
);
832 * port_dispatcher(): Dispatcher for messages destinated
833 * to the tipc_port interface. Called with port locked.
836 static u32
port_dispatcher(struct tipc_port
*dummy
, struct sk_buff
*buf
)
839 spin_lock_bh(&queue_lock
);
840 if (msg_queue_head
) {
841 msg_queue_tail
->next
= buf
;
842 msg_queue_tail
= buf
;
844 msg_queue_tail
= msg_queue_head
= buf
;
845 tipc_k_signal((Handler
)port_dispatcher_sigh
, 0);
847 spin_unlock_bh(&queue_lock
);
852 * Wake up port after congestion: Called with port locked,
856 static void port_wakeup_sh(unsigned long ref
)
858 struct tipc_port
*p_ptr
;
859 struct user_port
*up_ptr
;
860 tipc_continue_event cb
= NULL
;
863 p_ptr
= tipc_port_lock(ref
);
865 up_ptr
= p_ptr
->user_port
;
867 cb
= up_ptr
->continue_event_cb
;
868 uh
= up_ptr
->usr_handle
;
870 tipc_port_unlock(p_ptr
);
877 static void port_wakeup(struct tipc_port
*p_ptr
)
879 tipc_k_signal((Handler
)port_wakeup_sh
, p_ptr
->ref
);
882 void tipc_acknowledge(u32 ref
, u32 ack
)
884 struct tipc_port
*p_ptr
;
885 struct sk_buff
*buf
= NULL
;
887 p_ptr
= tipc_port_lock(ref
);
890 if (p_ptr
->connected
) {
891 p_ptr
->conn_unacked
-= ack
;
892 buf
= port_build_proto_msg(port_peerport(p_ptr
),
893 port_peernode(p_ptr
),
901 tipc_port_unlock(p_ptr
);
902 tipc_net_route_msg(buf
);
906 * tipc_createport(): user level call.
909 int tipc_createport(void *usr_handle
,
910 unsigned int importance
,
911 tipc_msg_err_event error_cb
,
912 tipc_named_msg_err_event named_error_cb
,
913 tipc_conn_shutdown_event conn_error_cb
,
914 tipc_msg_event msg_cb
,
915 tipc_named_msg_event named_msg_cb
,
916 tipc_conn_msg_event conn_msg_cb
,
917 tipc_continue_event continue_event_cb
,/* May be zero */
920 struct user_port
*up_ptr
;
921 struct tipc_port
*p_ptr
;
923 up_ptr
= kmalloc(sizeof(*up_ptr
), GFP_ATOMIC
);
925 warn("Port creation failed, no memory\n");
928 p_ptr
= (struct tipc_port
*)tipc_createport_raw(NULL
, port_dispatcher
,
929 port_wakeup
, importance
);
935 p_ptr
->user_port
= up_ptr
;
936 up_ptr
->usr_handle
= usr_handle
;
937 up_ptr
->ref
= p_ptr
->ref
;
938 up_ptr
->err_cb
= error_cb
;
939 up_ptr
->named_err_cb
= named_error_cb
;
940 up_ptr
->conn_err_cb
= conn_error_cb
;
941 up_ptr
->msg_cb
= msg_cb
;
942 up_ptr
->named_msg_cb
= named_msg_cb
;
943 up_ptr
->conn_msg_cb
= conn_msg_cb
;
944 up_ptr
->continue_event_cb
= continue_event_cb
;
945 *portref
= p_ptr
->ref
;
946 tipc_port_unlock(p_ptr
);
950 int tipc_portimportance(u32 ref
, unsigned int *importance
)
952 struct tipc_port
*p_ptr
;
954 p_ptr
= tipc_port_lock(ref
);
957 *importance
= (unsigned int)msg_importance(&p_ptr
->phdr
);
958 tipc_port_unlock(p_ptr
);
962 int tipc_set_portimportance(u32 ref
, unsigned int imp
)
964 struct tipc_port
*p_ptr
;
966 if (imp
> TIPC_CRITICAL_IMPORTANCE
)
969 p_ptr
= tipc_port_lock(ref
);
972 msg_set_importance(&p_ptr
->phdr
, (u32
)imp
);
973 tipc_port_unlock(p_ptr
);
978 int tipc_publish(u32 ref
, unsigned int scope
, struct tipc_name_seq
const *seq
)
980 struct tipc_port
*p_ptr
;
981 struct publication
*publ
;
985 p_ptr
= tipc_port_lock(ref
);
989 if (p_ptr
->connected
)
991 if (seq
->lower
> seq
->upper
)
993 if ((scope
< TIPC_ZONE_SCOPE
) || (scope
> TIPC_NODE_SCOPE
))
995 key
= ref
+ p_ptr
->pub_count
+ 1;
1000 publ
= tipc_nametbl_publish(seq
->type
, seq
->lower
, seq
->upper
,
1001 scope
, p_ptr
->ref
, key
);
1003 list_add(&publ
->pport_list
, &p_ptr
->publications
);
1005 p_ptr
->published
= 1;
1009 tipc_port_unlock(p_ptr
);
1013 int tipc_withdraw(u32 ref
, unsigned int scope
, struct tipc_name_seq
const *seq
)
1015 struct tipc_port
*p_ptr
;
1016 struct publication
*publ
;
1017 struct publication
*tpubl
;
1020 p_ptr
= tipc_port_lock(ref
);
1024 list_for_each_entry_safe(publ
, tpubl
,
1025 &p_ptr
->publications
, pport_list
) {
1026 tipc_nametbl_withdraw(publ
->type
, publ
->lower
,
1027 publ
->ref
, publ
->key
);
1031 list_for_each_entry_safe(publ
, tpubl
,
1032 &p_ptr
->publications
, pport_list
) {
1033 if (publ
->scope
!= scope
)
1035 if (publ
->type
!= seq
->type
)
1037 if (publ
->lower
!= seq
->lower
)
1039 if (publ
->upper
!= seq
->upper
)
1041 tipc_nametbl_withdraw(publ
->type
, publ
->lower
,
1042 publ
->ref
, publ
->key
);
1047 if (list_empty(&p_ptr
->publications
))
1048 p_ptr
->published
= 0;
1049 tipc_port_unlock(p_ptr
);
1053 int tipc_connect2port(u32 ref
, struct tipc_portid
const *peer
)
1055 struct tipc_port
*p_ptr
;
1056 struct tipc_msg
*msg
;
1059 p_ptr
= tipc_port_lock(ref
);
1062 if (p_ptr
->published
|| p_ptr
->connected
)
1068 msg_set_destnode(msg
, peer
->node
);
1069 msg_set_destport(msg
, peer
->ref
);
1070 msg_set_orignode(msg
, tipc_own_addr
);
1071 msg_set_origport(msg
, p_ptr
->ref
);
1072 msg_set_type(msg
, TIPC_CONN_MSG
);
1073 msg_set_lookup_scope(msg
, 0);
1074 msg_set_hdr_sz(msg
, SHORT_H_SIZE
);
1076 p_ptr
->probing_interval
= PROBING_INTERVAL
;
1077 p_ptr
->probing_state
= CONFIRMED
;
1078 p_ptr
->connected
= 1;
1079 k_start_timer(&p_ptr
->timer
, p_ptr
->probing_interval
);
1081 tipc_nodesub_subscribe(&p_ptr
->subscription
, peer
->node
,
1082 (void *)(unsigned long)ref
,
1083 (net_ev_handler
)port_handle_node_down
);
1086 tipc_port_unlock(p_ptr
);
1087 p_ptr
->max_pkt
= tipc_link_get_max_pkt(peer
->node
, ref
);
1092 * tipc_disconnect_port - disconnect port from peer
1094 * Port must be locked.
1097 int tipc_disconnect_port(struct tipc_port
*tp_ptr
)
1101 if (tp_ptr
->connected
) {
1102 tp_ptr
->connected
= 0;
1103 /* let timer expire on it's own to avoid deadlock! */
1104 tipc_nodesub_unsubscribe(
1105 &((struct tipc_port
*)tp_ptr
)->subscription
);
1114 * tipc_disconnect(): Disconnect port form peer.
1115 * This is a node local operation.
1118 int tipc_disconnect(u32 ref
)
1120 struct tipc_port
*p_ptr
;
1123 p_ptr
= tipc_port_lock(ref
);
1126 res
= tipc_disconnect_port((struct tipc_port
*)p_ptr
);
1127 tipc_port_unlock(p_ptr
);
1132 * tipc_shutdown(): Send a SHUTDOWN msg to peer and disconnect
1134 int tipc_shutdown(u32 ref
)
1136 struct tipc_port
*p_ptr
;
1137 struct sk_buff
*buf
= NULL
;
1139 p_ptr
= tipc_port_lock(ref
);
1143 if (p_ptr
->connected
) {
1144 u32 imp
= msg_importance(&p_ptr
->phdr
);
1145 if (imp
< TIPC_CRITICAL_IMPORTANCE
)
1147 buf
= port_build_proto_msg(port_peerport(p_ptr
),
1148 port_peernode(p_ptr
),
1156 tipc_port_unlock(p_ptr
);
1157 tipc_net_route_msg(buf
);
1158 return tipc_disconnect(ref
);
1162 * tipc_port_recv_sections(): Concatenate and deliver sectioned
1163 * message for this node.
1166 static int tipc_port_recv_sections(struct tipc_port
*sender
, unsigned int num_sect
,
1167 struct iovec
const *msg_sect
,
1168 unsigned int total_len
)
1170 struct sk_buff
*buf
;
1173 res
= tipc_msg_build(&sender
->phdr
, msg_sect
, num_sect
, total_len
,
1174 MAX_MSG_SIZE
, !sender
->user_port
, &buf
);
1176 tipc_port_recv_msg(buf
);
1181 * tipc_send - send message sections on connection
1184 int tipc_send(u32 ref
, unsigned int num_sect
, struct iovec
const *msg_sect
,
1185 unsigned int total_len
)
1187 struct tipc_port
*p_ptr
;
1191 p_ptr
= tipc_port_deref(ref
);
1192 if (!p_ptr
|| !p_ptr
->connected
)
1195 p_ptr
->congested
= 1;
1196 if (!tipc_port_congested(p_ptr
)) {
1197 destnode
= port_peernode(p_ptr
);
1198 if (likely(destnode
!= tipc_own_addr
))
1199 res
= tipc_link_send_sections_fast(p_ptr
, msg_sect
, num_sect
,
1200 total_len
, destnode
);
1202 res
= tipc_port_recv_sections(p_ptr
, num_sect
, msg_sect
,
1205 if (likely(res
!= -ELINKCONG
)) {
1206 p_ptr
->congested
= 0;
1212 if (port_unreliable(p_ptr
)) {
1213 p_ptr
->congested
= 0;
1220 * tipc_send2name - send message sections to port name
1223 int tipc_send2name(u32 ref
, struct tipc_name
const *name
, unsigned int domain
,
1224 unsigned int num_sect
, struct iovec
const *msg_sect
,
1225 unsigned int total_len
)
1227 struct tipc_port
*p_ptr
;
1228 struct tipc_msg
*msg
;
1229 u32 destnode
= domain
;
1233 p_ptr
= tipc_port_deref(ref
);
1234 if (!p_ptr
|| p_ptr
->connected
)
1238 msg_set_type(msg
, TIPC_NAMED_MSG
);
1239 msg_set_orignode(msg
, tipc_own_addr
);
1240 msg_set_origport(msg
, ref
);
1241 msg_set_hdr_sz(msg
, LONG_H_SIZE
);
1242 msg_set_nametype(msg
, name
->type
);
1243 msg_set_nameinst(msg
, name
->instance
);
1244 msg_set_lookup_scope(msg
, tipc_addr_scope(domain
));
1245 destport
= tipc_nametbl_translate(name
->type
, name
->instance
, &destnode
);
1246 msg_set_destnode(msg
, destnode
);
1247 msg_set_destport(msg
, destport
);
1249 if (likely(destport
)) {
1250 if (likely(destnode
== tipc_own_addr
))
1251 res
= tipc_port_recv_sections(p_ptr
, num_sect
,
1252 msg_sect
, total_len
);
1254 res
= tipc_link_send_sections_fast(p_ptr
, msg_sect
,
1255 num_sect
, total_len
,
1257 if (likely(res
!= -ELINKCONG
)) {
1262 if (port_unreliable(p_ptr
)) {
1267 return tipc_port_reject_sections(p_ptr
, msg
, msg_sect
, num_sect
,
1268 total_len
, TIPC_ERR_NO_NAME
);
1272 * tipc_send2port - send message sections to port identity
1275 int tipc_send2port(u32 ref
, struct tipc_portid
const *dest
,
1276 unsigned int num_sect
, struct iovec
const *msg_sect
,
1277 unsigned int total_len
)
1279 struct tipc_port
*p_ptr
;
1280 struct tipc_msg
*msg
;
1283 p_ptr
= tipc_port_deref(ref
);
1284 if (!p_ptr
|| p_ptr
->connected
)
1288 msg_set_type(msg
, TIPC_DIRECT_MSG
);
1289 msg_set_lookup_scope(msg
, 0);
1290 msg_set_orignode(msg
, tipc_own_addr
);
1291 msg_set_origport(msg
, ref
);
1292 msg_set_destnode(msg
, dest
->node
);
1293 msg_set_destport(msg
, dest
->ref
);
1294 msg_set_hdr_sz(msg
, DIR_MSG_H_SIZE
);
1296 if (dest
->node
== tipc_own_addr
)
1297 res
= tipc_port_recv_sections(p_ptr
, num_sect
, msg_sect
,
1300 res
= tipc_link_send_sections_fast(p_ptr
, msg_sect
, num_sect
,
1301 total_len
, dest
->node
);
1302 if (likely(res
!= -ELINKCONG
)) {
1307 if (port_unreliable(p_ptr
)) {
1314 * tipc_send_buf2port - send message buffer to port identity
1317 int tipc_send_buf2port(u32 ref
, struct tipc_portid
const *dest
,
1318 struct sk_buff
*buf
, unsigned int dsz
)
1320 struct tipc_port
*p_ptr
;
1321 struct tipc_msg
*msg
;
1324 p_ptr
= (struct tipc_port
*)tipc_ref_deref(ref
);
1325 if (!p_ptr
|| p_ptr
->connected
)
1329 msg_set_type(msg
, TIPC_DIRECT_MSG
);
1330 msg_set_orignode(msg
, tipc_own_addr
);
1331 msg_set_origport(msg
, ref
);
1332 msg_set_destnode(msg
, dest
->node
);
1333 msg_set_destport(msg
, dest
->ref
);
1334 msg_set_hdr_sz(msg
, DIR_MSG_H_SIZE
);
1335 msg_set_size(msg
, DIR_MSG_H_SIZE
+ dsz
);
1336 if (skb_cow(buf
, DIR_MSG_H_SIZE
))
1339 skb_push(buf
, DIR_MSG_H_SIZE
);
1340 skb_copy_to_linear_data(buf
, msg
, DIR_MSG_H_SIZE
);
1342 if (dest
->node
== tipc_own_addr
)
1343 res
= tipc_port_recv_msg(buf
);
1345 res
= tipc_send_buf_fast(buf
, dest
->node
);
1346 if (likely(res
!= -ELINKCONG
)) {
1351 if (port_unreliable(p_ptr
))