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.
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 port
*, u32 err
);
58 static struct sk_buff
*port_build_peer_abort_msg(struct port
*, u32 err
);
59 static void port_timeout(unsigned long ref
);
62 static u32
port_peernode(struct port
*p_ptr
)
64 return msg_destnode(&p_ptr
->publ
.phdr
);
67 static u32
port_peerport(struct port
*p_ptr
)
69 return msg_destport(&p_ptr
->publ
.phdr
);
72 static u32
port_out_seqno(struct port
*p_ptr
)
74 return msg_transp_seqno(&p_ptr
->publ
.phdr
);
77 static void port_incr_out_seqno(struct port
*p_ptr
)
79 struct tipc_msg
*m
= &p_ptr
->publ
.phdr
;
81 if (likely(!msg_routed(m
)))
83 msg_set_transp_seqno(m
, (msg_transp_seqno(m
) + 1));
87 * tipc_multicast - send a multicast message to local and remote destinations
90 int tipc_multicast(u32 ref
, struct tipc_name_seq
const *seq
,
91 u32 num_sect
, struct iovec
const *msg_sect
)
95 struct sk_buff
*ibuf
= NULL
;
96 struct port_list dports
= {0, NULL
, };
97 struct port
*oport
= tipc_port_deref(ref
);
101 if (unlikely(!oport
))
104 /* Create multicast message */
106 hdr
= &oport
->publ
.phdr
;
107 msg_set_type(hdr
, TIPC_MCAST_MSG
);
108 msg_set_nametype(hdr
, seq
->type
);
109 msg_set_namelower(hdr
, seq
->lower
);
110 msg_set_nameupper(hdr
, seq
->upper
);
111 msg_set_hdr_sz(hdr
, MCAST_H_SIZE
);
112 res
= tipc_msg_build(hdr
, msg_sect
, num_sect
, MAX_MSG_SIZE
,
113 !oport
->user_port
, &buf
);
117 /* Figure out where to send multicast message */
119 ext_targets
= tipc_nametbl_mc_translate(seq
->type
, seq
->lower
, seq
->upper
,
120 TIPC_NODE_SCOPE
, &dports
);
122 /* Send message to destinations (duplicate it only if necessary) */
125 if (dports
.count
!= 0) {
126 ibuf
= skb_copy(buf
, GFP_ATOMIC
);
128 tipc_port_list_free(&dports
);
133 res
= tipc_bclink_send_msg(buf
);
134 if ((res
< 0) && (dports
.count
!= 0))
142 tipc_port_recv_mcast(ibuf
, &dports
);
144 tipc_port_list_free(&dports
);
150 * tipc_port_recv_mcast - deliver multicast message to all destination ports
152 * If there is no port list, perform a lookup to create one
155 void tipc_port_recv_mcast(struct sk_buff
*buf
, struct port_list
*dp
)
157 struct tipc_msg
*msg
;
158 struct port_list dports
= {0, NULL
, };
159 struct port_list
*item
= dp
;
164 /* Create destination port list, if one wasn't supplied */
167 tipc_nametbl_mc_translate(msg_nametype(msg
),
175 /* Deliver a copy of message to each destination port */
177 if (dp
->count
!= 0) {
178 if (dp
->count
== 1) {
179 msg_set_destport(msg
, dp
->ports
[0]);
180 tipc_port_recv_msg(buf
);
181 tipc_port_list_free(dp
);
184 for (; cnt
< dp
->count
; cnt
++) {
185 int index
= cnt
% PLSIZE
;
186 struct sk_buff
*b
= skb_clone(buf
, GFP_ATOMIC
);
189 warn("Unable to deliver multicast message(s)\n");
192 if ((index
== 0) && (cnt
!= 0))
194 msg_set_destport(buf_msg(b
), item
->ports
[index
]);
195 tipc_port_recv_msg(b
);
200 tipc_port_list_free(dp
);
204 * tipc_createport_raw - create a generic TIPC port
206 * Returns pointer to (locked) TIPC port, or NULL if unable to create it
209 struct tipc_port
*tipc_createport_raw(void *usr_handle
,
210 u32 (*dispatcher
)(struct tipc_port
*, struct sk_buff
*),
211 void (*wakeup
)(struct tipc_port
*),
212 const u32 importance
)
215 struct tipc_msg
*msg
;
218 p_ptr
= kzalloc(sizeof(*p_ptr
), GFP_ATOMIC
);
220 warn("Port creation failed, no memory\n");
223 ref
= tipc_ref_acquire(p_ptr
, &p_ptr
->publ
.lock
);
225 warn("Port creation failed, reference table exhausted\n");
230 p_ptr
->publ
.usr_handle
= usr_handle
;
231 p_ptr
->publ
.max_pkt
= MAX_PKT_DEFAULT
;
232 p_ptr
->publ
.ref
= ref
;
233 msg
= &p_ptr
->publ
.phdr
;
234 tipc_msg_init(msg
, importance
, TIPC_NAMED_MSG
, LONG_H_SIZE
, 0);
235 msg_set_origport(msg
, ref
);
236 p_ptr
->last_in_seqno
= 41;
238 INIT_LIST_HEAD(&p_ptr
->wait_list
);
239 INIT_LIST_HEAD(&p_ptr
->subscription
.nodesub_list
);
240 p_ptr
->dispatcher
= dispatcher
;
241 p_ptr
->wakeup
= wakeup
;
242 p_ptr
->user_port
= NULL
;
243 k_init_timer(&p_ptr
->timer
, (Handler
)port_timeout
, ref
);
244 spin_lock_bh(&tipc_port_list_lock
);
245 INIT_LIST_HEAD(&p_ptr
->publications
);
246 INIT_LIST_HEAD(&p_ptr
->port_list
);
247 list_add_tail(&p_ptr
->port_list
, &ports
);
248 spin_unlock_bh(&tipc_port_list_lock
);
249 return &(p_ptr
->publ
);
252 int tipc_deleteport(u32 ref
)
255 struct sk_buff
*buf
= NULL
;
257 tipc_withdraw(ref
, 0, NULL
);
258 p_ptr
= tipc_port_lock(ref
);
262 tipc_ref_discard(ref
);
263 tipc_port_unlock(p_ptr
);
265 k_cancel_timer(&p_ptr
->timer
);
266 if (p_ptr
->publ
.connected
) {
267 buf
= port_build_peer_abort_msg(p_ptr
, TIPC_ERR_NO_PORT
);
268 tipc_nodesub_unsubscribe(&p_ptr
->subscription
);
270 kfree(p_ptr
->user_port
);
272 spin_lock_bh(&tipc_port_list_lock
);
273 list_del(&p_ptr
->port_list
);
274 list_del(&p_ptr
->wait_list
);
275 spin_unlock_bh(&tipc_port_list_lock
);
276 k_term_timer(&p_ptr
->timer
);
278 tipc_net_route_msg(buf
);
282 static int port_unreliable(struct port
*p_ptr
)
284 return msg_src_droppable(&p_ptr
->publ
.phdr
);
287 int tipc_portunreliable(u32 ref
, unsigned int *isunreliable
)
291 p_ptr
= tipc_port_lock(ref
);
294 *isunreliable
= port_unreliable(p_ptr
);
295 tipc_port_unlock(p_ptr
);
299 int tipc_set_portunreliable(u32 ref
, unsigned int isunreliable
)
303 p_ptr
= tipc_port_lock(ref
);
306 msg_set_src_droppable(&p_ptr
->publ
.phdr
, (isunreliable
!= 0));
307 tipc_port_unlock(p_ptr
);
311 static int port_unreturnable(struct port
*p_ptr
)
313 return msg_dest_droppable(&p_ptr
->publ
.phdr
);
316 int tipc_portunreturnable(u32 ref
, unsigned int *isunrejectable
)
320 p_ptr
= tipc_port_lock(ref
);
323 *isunrejectable
= port_unreturnable(p_ptr
);
324 tipc_port_unlock(p_ptr
);
328 int tipc_set_portunreturnable(u32 ref
, unsigned int isunrejectable
)
332 p_ptr
= tipc_port_lock(ref
);
335 msg_set_dest_droppable(&p_ptr
->publ
.phdr
, (isunrejectable
!= 0));
336 tipc_port_unlock(p_ptr
);
341 * port_build_proto_msg(): build a port level protocol
342 * or a connection abortion message. Called with
345 static struct sk_buff
*port_build_proto_msg(u32 destport
, u32 destnode
,
346 u32 origport
, u32 orignode
,
347 u32 usr
, u32 type
, u32 err
,
351 struct tipc_msg
*msg
;
353 buf
= tipc_buf_acquire(LONG_H_SIZE
);
356 tipc_msg_init(msg
, usr
, type
, LONG_H_SIZE
, destnode
);
357 msg_set_errcode(msg
, err
);
358 msg_set_destport(msg
, destport
);
359 msg_set_origport(msg
, origport
);
360 msg_set_orignode(msg
, orignode
);
361 msg_set_transp_seqno(msg
, seqno
);
362 msg_set_msgcnt(msg
, ack
);
367 int tipc_reject_msg(struct sk_buff
*buf
, u32 err
)
369 struct tipc_msg
*msg
= buf_msg(buf
);
370 struct sk_buff
*rbuf
;
371 struct tipc_msg
*rmsg
;
373 u32 imp
= msg_importance(msg
);
374 u32 data_sz
= msg_data_sz(msg
);
376 if (data_sz
> MAX_REJECT_SIZE
)
377 data_sz
= MAX_REJECT_SIZE
;
378 if (msg_connected(msg
) && (imp
< TIPC_CRITICAL_IMPORTANCE
))
381 /* discard rejected message if it shouldn't be returned to sender */
382 if (msg_errcode(msg
) || msg_dest_droppable(msg
)) {
387 /* construct rejected message */
389 hdr_sz
= MCAST_H_SIZE
;
391 hdr_sz
= LONG_H_SIZE
;
392 rbuf
= tipc_buf_acquire(data_sz
+ hdr_sz
);
397 rmsg
= buf_msg(rbuf
);
398 tipc_msg_init(rmsg
, imp
, msg_type(msg
), hdr_sz
, msg_orignode(msg
));
399 msg_set_errcode(rmsg
, err
);
400 msg_set_destport(rmsg
, msg_origport(msg
));
401 msg_set_origport(rmsg
, msg_destport(msg
));
402 if (msg_short(msg
)) {
403 msg_set_orignode(rmsg
, tipc_own_addr
);
404 /* leave name type & instance as zeroes */
406 msg_set_orignode(rmsg
, msg_destnode(msg
));
407 msg_set_nametype(rmsg
, msg_nametype(msg
));
408 msg_set_nameinst(rmsg
, msg_nameinst(msg
));
410 msg_set_size(rmsg
, data_sz
+ hdr_sz
);
411 skb_copy_to_linear_data_offset(rbuf
, hdr_sz
, msg_data(msg
), data_sz
);
413 /* send self-abort message when rejecting on a connected port */
414 if (msg_connected(msg
)) {
415 struct sk_buff
*abuf
= NULL
;
416 struct port
*p_ptr
= tipc_port_lock(msg_destport(msg
));
419 if (p_ptr
->publ
.connected
)
420 abuf
= port_build_self_abort_msg(p_ptr
, err
);
421 tipc_port_unlock(p_ptr
);
423 tipc_net_route_msg(abuf
);
426 /* send rejected message */
428 tipc_net_route_msg(rbuf
);
432 int tipc_port_reject_sections(struct port
*p_ptr
, struct tipc_msg
*hdr
,
433 struct iovec
const *msg_sect
, u32 num_sect
,
439 res
= tipc_msg_build(hdr
, msg_sect
, num_sect
, MAX_MSG_SIZE
,
440 !p_ptr
->user_port
, &buf
);
444 return tipc_reject_msg(buf
, err
);
447 static void port_timeout(unsigned long ref
)
449 struct port
*p_ptr
= tipc_port_lock(ref
);
450 struct sk_buff
*buf
= NULL
;
455 if (!p_ptr
->publ
.connected
) {
456 tipc_port_unlock(p_ptr
);
460 /* Last probe answered ? */
461 if (p_ptr
->probing_state
== PROBING
) {
462 buf
= port_build_self_abort_msg(p_ptr
, TIPC_ERR_NO_PORT
);
464 buf
= port_build_proto_msg(port_peerport(p_ptr
),
465 port_peernode(p_ptr
),
471 port_out_seqno(p_ptr
),
473 port_incr_out_seqno(p_ptr
);
474 p_ptr
->probing_state
= PROBING
;
475 k_start_timer(&p_ptr
->timer
, p_ptr
->probing_interval
);
477 tipc_port_unlock(p_ptr
);
478 tipc_net_route_msg(buf
);
482 static void port_handle_node_down(unsigned long ref
)
484 struct port
*p_ptr
= tipc_port_lock(ref
);
485 struct sk_buff
*buf
= NULL
;
489 buf
= port_build_self_abort_msg(p_ptr
, TIPC_ERR_NO_NODE
);
490 tipc_port_unlock(p_ptr
);
491 tipc_net_route_msg(buf
);
495 static struct sk_buff
*port_build_self_abort_msg(struct port
*p_ptr
, u32 err
)
497 u32 imp
= msg_importance(&p_ptr
->publ
.phdr
);
499 if (!p_ptr
->publ
.connected
)
501 if (imp
< TIPC_CRITICAL_IMPORTANCE
)
503 return port_build_proto_msg(p_ptr
->publ
.ref
,
505 port_peerport(p_ptr
),
506 port_peernode(p_ptr
),
510 p_ptr
->last_in_seqno
+ 1,
515 static struct sk_buff
*port_build_peer_abort_msg(struct port
*p_ptr
, u32 err
)
517 u32 imp
= msg_importance(&p_ptr
->publ
.phdr
);
519 if (!p_ptr
->publ
.connected
)
521 if (imp
< TIPC_CRITICAL_IMPORTANCE
)
523 return port_build_proto_msg(port_peerport(p_ptr
),
524 port_peernode(p_ptr
),
530 port_out_seqno(p_ptr
),
534 void tipc_port_recv_proto_msg(struct sk_buff
*buf
)
536 struct tipc_msg
*msg
= buf_msg(buf
);
537 struct port
*p_ptr
= tipc_port_lock(msg_destport(msg
));
539 struct sk_buff
*r_buf
= NULL
;
540 struct sk_buff
*abort_buf
= NULL
;
543 err
= TIPC_ERR_NO_PORT
;
544 } else if (p_ptr
->publ
.connected
) {
545 if ((port_peernode(p_ptr
) != msg_orignode(msg
)) ||
546 (port_peerport(p_ptr
) != msg_origport(msg
))) {
547 err
= TIPC_ERR_NO_PORT
;
548 } else if (msg_type(msg
) == CONN_ACK
) {
549 int wakeup
= tipc_port_congested(p_ptr
) &&
550 p_ptr
->publ
.congested
&&
552 p_ptr
->acked
+= msg_msgcnt(msg
);
553 if (tipc_port_congested(p_ptr
))
555 p_ptr
->publ
.congested
= 0;
558 p_ptr
->wakeup(&p_ptr
->publ
);
561 } else if (p_ptr
->publ
.published
) {
562 err
= TIPC_ERR_NO_PORT
;
565 r_buf
= port_build_proto_msg(msg_origport(msg
),
569 TIPC_HIGH_IMPORTANCE
,
578 if (msg_type(msg
) == CONN_PROBE
) {
579 r_buf
= port_build_proto_msg(msg_origport(msg
),
586 port_out_seqno(p_ptr
),
589 p_ptr
->probing_state
= CONFIRMED
;
590 port_incr_out_seqno(p_ptr
);
593 tipc_port_unlock(p_ptr
);
594 tipc_net_route_msg(r_buf
);
595 tipc_net_route_msg(abort_buf
);
599 static void port_print(struct port
*p_ptr
, struct print_buf
*buf
, int full_id
)
601 struct publication
*publ
;
604 tipc_printf(buf
, "<%u.%u.%u:%u>:",
605 tipc_zone(tipc_own_addr
), tipc_cluster(tipc_own_addr
),
606 tipc_node(tipc_own_addr
), p_ptr
->publ
.ref
);
608 tipc_printf(buf
, "%-10u:", p_ptr
->publ
.ref
);
610 if (p_ptr
->publ
.connected
) {
611 u32 dport
= port_peerport(p_ptr
);
612 u32 destnode
= port_peernode(p_ptr
);
614 tipc_printf(buf
, " connected to <%u.%u.%u:%u>",
615 tipc_zone(destnode
), tipc_cluster(destnode
),
616 tipc_node(destnode
), dport
);
617 if (p_ptr
->publ
.conn_type
!= 0)
618 tipc_printf(buf
, " via {%u,%u}",
619 p_ptr
->publ
.conn_type
,
620 p_ptr
->publ
.conn_instance
);
621 } else if (p_ptr
->publ
.published
) {
622 tipc_printf(buf
, " bound to");
623 list_for_each_entry(publ
, &p_ptr
->publications
, pport_list
) {
624 if (publ
->lower
== publ
->upper
)
625 tipc_printf(buf
, " {%u,%u}", publ
->type
,
628 tipc_printf(buf
, " {%u,%u,%u}", publ
->type
,
629 publ
->lower
, publ
->upper
);
632 tipc_printf(buf
, "\n");
635 #define MAX_PORT_QUERY 32768
637 struct sk_buff
*tipc_port_get_ports(void)
640 struct tlv_desc
*rep_tlv
;
645 buf
= tipc_cfg_reply_alloc(TLV_SPACE(MAX_PORT_QUERY
));
648 rep_tlv
= (struct tlv_desc
*)buf
->data
;
650 tipc_printbuf_init(&pb
, TLV_DATA(rep_tlv
), MAX_PORT_QUERY
);
651 spin_lock_bh(&tipc_port_list_lock
);
652 list_for_each_entry(p_ptr
, &ports
, port_list
) {
653 spin_lock_bh(p_ptr
->publ
.lock
);
654 port_print(p_ptr
, &pb
, 0);
655 spin_unlock_bh(p_ptr
->publ
.lock
);
657 spin_unlock_bh(&tipc_port_list_lock
);
658 str_len
= tipc_printbuf_validate(&pb
);
660 skb_put(buf
, TLV_SPACE(str_len
));
661 TLV_SET(rep_tlv
, TIPC_TLV_ULTRA_STRING
, NULL
, str_len
);
666 void tipc_port_reinit(void)
669 struct tipc_msg
*msg
;
671 spin_lock_bh(&tipc_port_list_lock
);
672 list_for_each_entry(p_ptr
, &ports
, port_list
) {
673 msg
= &p_ptr
->publ
.phdr
;
674 if (msg_orignode(msg
) == tipc_own_addr
)
676 msg_set_prevnode(msg
, tipc_own_addr
);
677 msg_set_orignode(msg
, tipc_own_addr
);
679 spin_unlock_bh(&tipc_port_list_lock
);
684 * port_dispatcher_sigh(): Signal handler for messages destinated
685 * to the tipc_port interface.
688 static void port_dispatcher_sigh(void *dummy
)
692 spin_lock_bh(&queue_lock
);
693 buf
= msg_queue_head
;
694 msg_queue_head
= NULL
;
695 spin_unlock_bh(&queue_lock
);
699 struct user_port
*up_ptr
;
700 struct tipc_portid orig
;
701 struct tipc_name_seq dseq
;
707 struct sk_buff
*next
= buf
->next
;
708 struct tipc_msg
*msg
= buf_msg(buf
);
709 u32 dref
= msg_destport(msg
);
711 message_type
= msg_type(msg
);
712 if (message_type
> TIPC_DIRECT_MSG
)
713 goto reject
; /* Unsupported message type */
715 p_ptr
= tipc_port_lock(dref
);
717 goto reject
; /* Port deleted while msg in queue */
719 orig
.ref
= msg_origport(msg
);
720 orig
.node
= msg_orignode(msg
);
721 up_ptr
= p_ptr
->user_port
;
722 usr_handle
= up_ptr
->usr_handle
;
723 connected
= p_ptr
->publ
.connected
;
724 published
= p_ptr
->publ
.published
;
726 if (unlikely(msg_errcode(msg
)))
729 switch (message_type
) {
732 tipc_conn_msg_event cb
= up_ptr
->conn_msg_cb
;
733 u32 peer_port
= port_peerport(p_ptr
);
734 u32 peer_node
= port_peernode(p_ptr
);
736 tipc_port_unlock(p_ptr
);
739 if (unlikely(!connected
)) {
740 if (tipc_connect2port(dref
, &orig
))
742 } else if ((msg_origport(msg
) != peer_port
) ||
743 (msg_orignode(msg
) != peer_node
))
745 if (unlikely(++p_ptr
->publ
.conn_unacked
>=
746 TIPC_FLOW_CONTROL_WIN
))
747 tipc_acknowledge(dref
,
748 p_ptr
->publ
.conn_unacked
);
749 skb_pull(buf
, msg_hdr_sz(msg
));
750 cb(usr_handle
, dref
, &buf
, msg_data(msg
),
754 case TIPC_DIRECT_MSG
:{
755 tipc_msg_event cb
= up_ptr
->msg_cb
;
757 tipc_port_unlock(p_ptr
);
758 if (unlikely(!cb
|| connected
))
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
),
767 case TIPC_NAMED_MSG
:{
768 tipc_named_msg_event cb
= up_ptr
->named_msg_cb
;
770 tipc_port_unlock(p_ptr
);
771 if (unlikely(!cb
|| connected
|| !published
))
773 dseq
.type
= msg_nametype(msg
);
774 dseq
.lower
= msg_nameinst(msg
);
775 dseq
.upper
= (message_type
== TIPC_NAMED_MSG
)
776 ? dseq
.lower
: msg_nameupper(msg
);
777 skb_pull(buf
, msg_hdr_sz(msg
));
778 cb(usr_handle
, dref
, &buf
, msg_data(msg
),
779 msg_data_sz(msg
), msg_importance(msg
),
789 switch (message_type
) {
792 tipc_conn_shutdown_event cb
=
794 u32 peer_port
= port_peerport(p_ptr
);
795 u32 peer_node
= port_peernode(p_ptr
);
797 tipc_port_unlock(p_ptr
);
798 if (!cb
|| !connected
)
800 if ((msg_origport(msg
) != peer_port
) ||
801 (msg_orignode(msg
) != peer_node
))
803 tipc_disconnect(dref
);
804 skb_pull(buf
, msg_hdr_sz(msg
));
805 cb(usr_handle
, dref
, &buf
, msg_data(msg
),
806 msg_data_sz(msg
), msg_errcode(msg
));
809 case TIPC_DIRECT_MSG
:{
810 tipc_msg_err_event cb
= up_ptr
->err_cb
;
812 tipc_port_unlock(p_ptr
);
813 if (!cb
|| connected
)
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
), &orig
);
821 case TIPC_NAMED_MSG
:{
822 tipc_named_msg_err_event cb
=
823 up_ptr
->named_err_cb
;
825 tipc_port_unlock(p_ptr
);
826 if (!cb
|| connected
)
828 dseq
.type
= msg_nametype(msg
);
829 dseq
.lower
= msg_nameinst(msg
);
830 dseq
.upper
= (message_type
== TIPC_NAMED_MSG
)
831 ? dseq
.lower
: msg_nameupper(msg
);
832 skb_pull(buf
, msg_hdr_sz(msg
));
833 cb(usr_handle
, dref
, &buf
, msg_data(msg
),
834 msg_data_sz(msg
), msg_errcode(msg
), &dseq
);
843 tipc_reject_msg(buf
, TIPC_ERR_NO_PORT
);
849 * port_dispatcher(): Dispatcher for messages destinated
850 * to the tipc_port interface. Called with port locked.
853 static u32
port_dispatcher(struct tipc_port
*dummy
, struct sk_buff
*buf
)
856 spin_lock_bh(&queue_lock
);
857 if (msg_queue_head
) {
858 msg_queue_tail
->next
= buf
;
859 msg_queue_tail
= buf
;
861 msg_queue_tail
= msg_queue_head
= buf
;
862 tipc_k_signal((Handler
)port_dispatcher_sigh
, 0);
864 spin_unlock_bh(&queue_lock
);
869 * Wake up port after congestion: Called with port locked,
873 static void port_wakeup_sh(unsigned long ref
)
876 struct user_port
*up_ptr
;
877 tipc_continue_event cb
= NULL
;
880 p_ptr
= tipc_port_lock(ref
);
882 up_ptr
= p_ptr
->user_port
;
884 cb
= up_ptr
->continue_event_cb
;
885 uh
= up_ptr
->usr_handle
;
887 tipc_port_unlock(p_ptr
);
894 static void port_wakeup(struct tipc_port
*p_ptr
)
896 tipc_k_signal((Handler
)port_wakeup_sh
, p_ptr
->ref
);
899 void tipc_acknowledge(u32 ref
, u32 ack
)
902 struct sk_buff
*buf
= NULL
;
904 p_ptr
= tipc_port_lock(ref
);
907 if (p_ptr
->publ
.connected
) {
908 p_ptr
->publ
.conn_unacked
-= ack
;
909 buf
= port_build_proto_msg(port_peerport(p_ptr
),
910 port_peernode(p_ptr
),
916 port_out_seqno(p_ptr
),
919 tipc_port_unlock(p_ptr
);
920 tipc_net_route_msg(buf
);
924 * tipc_createport(): user level call.
927 int tipc_createport(void *usr_handle
,
928 unsigned int importance
,
929 tipc_msg_err_event error_cb
,
930 tipc_named_msg_err_event named_error_cb
,
931 tipc_conn_shutdown_event conn_error_cb
,
932 tipc_msg_event msg_cb
,
933 tipc_named_msg_event named_msg_cb
,
934 tipc_conn_msg_event conn_msg_cb
,
935 tipc_continue_event continue_event_cb
,/* May be zero */
938 struct user_port
*up_ptr
;
941 up_ptr
= kmalloc(sizeof(*up_ptr
), GFP_ATOMIC
);
943 warn("Port creation failed, no memory\n");
946 p_ptr
= (struct port
*)tipc_createport_raw(NULL
, port_dispatcher
,
947 port_wakeup
, importance
);
953 p_ptr
->user_port
= up_ptr
;
954 up_ptr
->usr_handle
= usr_handle
;
955 up_ptr
->ref
= p_ptr
->publ
.ref
;
956 up_ptr
->err_cb
= error_cb
;
957 up_ptr
->named_err_cb
= named_error_cb
;
958 up_ptr
->conn_err_cb
= conn_error_cb
;
959 up_ptr
->msg_cb
= msg_cb
;
960 up_ptr
->named_msg_cb
= named_msg_cb
;
961 up_ptr
->conn_msg_cb
= conn_msg_cb
;
962 up_ptr
->continue_event_cb
= continue_event_cb
;
963 *portref
= p_ptr
->publ
.ref
;
964 tipc_port_unlock(p_ptr
);
968 int tipc_portimportance(u32 ref
, unsigned int *importance
)
972 p_ptr
= tipc_port_lock(ref
);
975 *importance
= (unsigned int)msg_importance(&p_ptr
->publ
.phdr
);
976 tipc_port_unlock(p_ptr
);
980 int tipc_set_portimportance(u32 ref
, unsigned int imp
)
984 if (imp
> TIPC_CRITICAL_IMPORTANCE
)
987 p_ptr
= tipc_port_lock(ref
);
990 msg_set_importance(&p_ptr
->publ
.phdr
, (u32
)imp
);
991 tipc_port_unlock(p_ptr
);
996 int tipc_publish(u32 ref
, unsigned int scope
, struct tipc_name_seq
const *seq
)
999 struct publication
*publ
;
1003 p_ptr
= tipc_port_lock(ref
);
1007 if (p_ptr
->publ
.connected
)
1009 if (seq
->lower
> seq
->upper
)
1011 if ((scope
< TIPC_ZONE_SCOPE
) || (scope
> TIPC_NODE_SCOPE
))
1013 key
= ref
+ p_ptr
->pub_count
+ 1;
1018 publ
= tipc_nametbl_publish(seq
->type
, seq
->lower
, seq
->upper
,
1019 scope
, p_ptr
->publ
.ref
, key
);
1021 list_add(&publ
->pport_list
, &p_ptr
->publications
);
1023 p_ptr
->publ
.published
= 1;
1027 tipc_port_unlock(p_ptr
);
1031 int tipc_withdraw(u32 ref
, unsigned int scope
, struct tipc_name_seq
const *seq
)
1034 struct publication
*publ
;
1035 struct publication
*tpubl
;
1038 p_ptr
= tipc_port_lock(ref
);
1042 list_for_each_entry_safe(publ
, tpubl
,
1043 &p_ptr
->publications
, pport_list
) {
1044 tipc_nametbl_withdraw(publ
->type
, publ
->lower
,
1045 publ
->ref
, publ
->key
);
1049 list_for_each_entry_safe(publ
, tpubl
,
1050 &p_ptr
->publications
, pport_list
) {
1051 if (publ
->scope
!= scope
)
1053 if (publ
->type
!= seq
->type
)
1055 if (publ
->lower
!= seq
->lower
)
1057 if (publ
->upper
!= seq
->upper
)
1059 tipc_nametbl_withdraw(publ
->type
, publ
->lower
,
1060 publ
->ref
, publ
->key
);
1065 if (list_empty(&p_ptr
->publications
))
1066 p_ptr
->publ
.published
= 0;
1067 tipc_port_unlock(p_ptr
);
1071 int tipc_connect2port(u32 ref
, struct tipc_portid
const *peer
)
1074 struct tipc_msg
*msg
;
1077 p_ptr
= tipc_port_lock(ref
);
1080 if (p_ptr
->publ
.published
|| p_ptr
->publ
.connected
)
1085 msg
= &p_ptr
->publ
.phdr
;
1086 msg_set_destnode(msg
, peer
->node
);
1087 msg_set_destport(msg
, peer
->ref
);
1088 msg_set_orignode(msg
, tipc_own_addr
);
1089 msg_set_origport(msg
, p_ptr
->publ
.ref
);
1090 msg_set_transp_seqno(msg
, 42);
1091 msg_set_type(msg
, TIPC_CONN_MSG
);
1092 msg_set_hdr_sz(msg
, SHORT_H_SIZE
);
1094 p_ptr
->probing_interval
= PROBING_INTERVAL
;
1095 p_ptr
->probing_state
= CONFIRMED
;
1096 p_ptr
->publ
.connected
= 1;
1097 k_start_timer(&p_ptr
->timer
, p_ptr
->probing_interval
);
1099 tipc_nodesub_subscribe(&p_ptr
->subscription
, peer
->node
,
1100 (void *)(unsigned long)ref
,
1101 (net_ev_handler
)port_handle_node_down
);
1104 tipc_port_unlock(p_ptr
);
1105 p_ptr
->publ
.max_pkt
= tipc_link_get_max_pkt(peer
->node
, ref
);
1110 * tipc_disconnect_port - disconnect port from peer
1112 * Port must be locked.
1115 int tipc_disconnect_port(struct tipc_port
*tp_ptr
)
1119 if (tp_ptr
->connected
) {
1120 tp_ptr
->connected
= 0;
1121 /* let timer expire on it's own to avoid deadlock! */
1122 tipc_nodesub_unsubscribe(
1123 &((struct port
*)tp_ptr
)->subscription
);
1132 * tipc_disconnect(): Disconnect port form peer.
1133 * This is a node local operation.
1136 int tipc_disconnect(u32 ref
)
1141 p_ptr
= tipc_port_lock(ref
);
1144 res
= tipc_disconnect_port((struct tipc_port
*)p_ptr
);
1145 tipc_port_unlock(p_ptr
);
1150 * tipc_shutdown(): Send a SHUTDOWN msg to peer and disconnect
1152 int tipc_shutdown(u32 ref
)
1155 struct sk_buff
*buf
= NULL
;
1157 p_ptr
= tipc_port_lock(ref
);
1161 if (p_ptr
->publ
.connected
) {
1162 u32 imp
= msg_importance(&p_ptr
->publ
.phdr
);
1163 if (imp
< TIPC_CRITICAL_IMPORTANCE
)
1165 buf
= port_build_proto_msg(port_peerport(p_ptr
),
1166 port_peernode(p_ptr
),
1172 port_out_seqno(p_ptr
),
1175 tipc_port_unlock(p_ptr
);
1176 tipc_net_route_msg(buf
);
1177 return tipc_disconnect(ref
);
1181 * tipc_port_recv_sections(): Concatenate and deliver sectioned
1182 * message for this node.
1185 static int tipc_port_recv_sections(struct port
*sender
, unsigned int num_sect
,
1186 struct iovec
const *msg_sect
)
1188 struct sk_buff
*buf
;
1191 res
= tipc_msg_build(&sender
->publ
.phdr
, msg_sect
, num_sect
,
1192 MAX_MSG_SIZE
, !sender
->user_port
, &buf
);
1194 tipc_port_recv_msg(buf
);
1199 * tipc_send - send message sections on connection
1202 int tipc_send(u32 ref
, unsigned int num_sect
, struct iovec
const *msg_sect
)
1208 p_ptr
= tipc_port_deref(ref
);
1209 if (!p_ptr
|| !p_ptr
->publ
.connected
)
1212 p_ptr
->publ
.congested
= 1;
1213 if (!tipc_port_congested(p_ptr
)) {
1214 destnode
= port_peernode(p_ptr
);
1215 if (likely(destnode
!= tipc_own_addr
))
1216 res
= tipc_link_send_sections_fast(p_ptr
, msg_sect
, num_sect
,
1219 res
= tipc_port_recv_sections(p_ptr
, num_sect
, msg_sect
);
1221 if (likely(res
!= -ELINKCONG
)) {
1222 port_incr_out_seqno(p_ptr
);
1223 p_ptr
->publ
.congested
= 0;
1228 if (port_unreliable(p_ptr
)) {
1229 p_ptr
->publ
.congested
= 0;
1230 /* Just calculate msg length and return */
1231 return tipc_msg_calc_data_size(msg_sect
, num_sect
);
1237 * tipc_send2name - send message sections to port name
1240 int tipc_send2name(u32 ref
, struct tipc_name
const *name
, unsigned int domain
,
1241 unsigned int num_sect
, struct iovec
const *msg_sect
)
1244 struct tipc_msg
*msg
;
1245 u32 destnode
= domain
;
1249 p_ptr
= tipc_port_deref(ref
);
1250 if (!p_ptr
|| p_ptr
->publ
.connected
)
1253 msg
= &p_ptr
->publ
.phdr
;
1254 msg_set_type(msg
, TIPC_NAMED_MSG
);
1255 msg_set_orignode(msg
, tipc_own_addr
);
1256 msg_set_origport(msg
, ref
);
1257 msg_set_hdr_sz(msg
, LONG_H_SIZE
);
1258 msg_set_nametype(msg
, name
->type
);
1259 msg_set_nameinst(msg
, name
->instance
);
1260 msg_set_lookup_scope(msg
, tipc_addr_scope(domain
));
1261 destport
= tipc_nametbl_translate(name
->type
, name
->instance
, &destnode
);
1262 msg_set_destnode(msg
, destnode
);
1263 msg_set_destport(msg
, destport
);
1265 if (likely(destport
)) {
1267 if (likely(destnode
== tipc_own_addr
))
1268 return tipc_port_recv_sections(p_ptr
, num_sect
, msg_sect
);
1269 res
= tipc_link_send_sections_fast(p_ptr
, msg_sect
, num_sect
,
1271 if (likely(res
!= -ELINKCONG
))
1273 if (port_unreliable(p_ptr
)) {
1274 /* Just calculate msg length and return */
1275 return tipc_msg_calc_data_size(msg_sect
, num_sect
);
1279 return tipc_port_reject_sections(p_ptr
, msg
, msg_sect
, num_sect
,
1284 * tipc_send2port - send message sections to port identity
1287 int tipc_send2port(u32 ref
, struct tipc_portid
const *dest
,
1288 unsigned int num_sect
, struct iovec
const *msg_sect
)
1291 struct tipc_msg
*msg
;
1294 p_ptr
= tipc_port_deref(ref
);
1295 if (!p_ptr
|| p_ptr
->publ
.connected
)
1298 msg
= &p_ptr
->publ
.phdr
;
1299 msg_set_type(msg
, TIPC_DIRECT_MSG
);
1300 msg_set_orignode(msg
, tipc_own_addr
);
1301 msg_set_origport(msg
, ref
);
1302 msg_set_destnode(msg
, dest
->node
);
1303 msg_set_destport(msg
, dest
->ref
);
1304 msg_set_hdr_sz(msg
, DIR_MSG_H_SIZE
);
1306 if (dest
->node
== tipc_own_addr
)
1307 return tipc_port_recv_sections(p_ptr
, num_sect
, msg_sect
);
1308 res
= tipc_link_send_sections_fast(p_ptr
, msg_sect
, num_sect
, dest
->node
);
1309 if (likely(res
!= -ELINKCONG
))
1311 if (port_unreliable(p_ptr
)) {
1312 /* Just calculate msg length and return */
1313 return tipc_msg_calc_data_size(msg_sect
, num_sect
);
1319 * tipc_send_buf2port - send message buffer to port identity
1322 int tipc_send_buf2port(u32 ref
, struct tipc_portid
const *dest
,
1323 struct sk_buff
*buf
, unsigned int dsz
)
1326 struct tipc_msg
*msg
;
1329 p_ptr
= (struct port
*)tipc_ref_deref(ref
);
1330 if (!p_ptr
|| p_ptr
->publ
.connected
)
1333 msg
= &p_ptr
->publ
.phdr
;
1334 msg_set_type(msg
, TIPC_DIRECT_MSG
);
1335 msg_set_orignode(msg
, tipc_own_addr
);
1336 msg_set_origport(msg
, ref
);
1337 msg_set_destnode(msg
, dest
->node
);
1338 msg_set_destport(msg
, dest
->ref
);
1339 msg_set_hdr_sz(msg
, DIR_MSG_H_SIZE
);
1340 msg_set_size(msg
, DIR_MSG_H_SIZE
+ dsz
);
1341 if (skb_cow(buf
, DIR_MSG_H_SIZE
))
1344 skb_push(buf
, DIR_MSG_H_SIZE
);
1345 skb_copy_to_linear_data(buf
, msg
, DIR_MSG_H_SIZE
);
1347 if (dest
->node
== tipc_own_addr
)
1348 return tipc_port_recv_msg(buf
);
1349 res
= tipc_send_buf_fast(buf
, dest
->node
);
1350 if (likely(res
!= -ELINKCONG
))
1352 if (port_unreliable(p_ptr
))