2 * net/tipc/port.c: TIPC port code
4 * Copyright (c) 1992-2006, Ericsson AB
5 * Copyright (c) 2004-2005, Wind River Systems
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the names of the copyright holders nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
20 * Alternatively, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") version 2 as published by the Free
22 * Software Foundation.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
45 #include "name_table.h"
50 /* Connection management: */
51 #define PROBING_INTERVAL 3600000 /* [ms] => 1 h */
55 #define MAX_REJECT_SIZE 1024
57 static struct sk_buff *msg_queue_head = NULL;
58 static struct sk_buff *msg_queue_tail = NULL;
60 spinlock_t tipc_port_list_lock = SPIN_LOCK_UNLOCKED;
61 static spinlock_t queue_lock = SPIN_LOCK_UNLOCKED;
63 static LIST_HEAD(ports);
64 static void port_handle_node_down(unsigned long ref);
65 static struct sk_buff* port_build_self_abort_msg(struct port *,u32 err);
66 static struct sk_buff* port_build_peer_abort_msg(struct port *,u32 err);
67 static void port_timeout(unsigned long ref);
70 static u32 port_peernode(struct port *p_ptr)
72 return msg_destnode(&p_ptr->publ.phdr);
75 static u32 port_peerport(struct port *p_ptr)
77 return msg_destport(&p_ptr->publ.phdr);
80 static u32 port_out_seqno(struct port *p_ptr)
82 return msg_transp_seqno(&p_ptr->publ.phdr);
85 static void port_incr_out_seqno(struct port *p_ptr)
87 struct tipc_msg *m = &p_ptr->publ.phdr;
89 if (likely(!msg_routed(m)))
91 msg_set_transp_seqno(m, (msg_transp_seqno(m) + 1));
95 * tipc_multicast - send a multicast message to local and remote destinations
98 int tipc_multicast(u32 ref, struct tipc_name_seq const *seq, u32 domain,
99 u32 num_sect, struct iovec const *msg_sect)
101 struct tipc_msg *hdr;
103 struct sk_buff *ibuf = NULL;
104 struct port_list dports = {0, NULL, };
105 struct port *oport = tipc_port_deref(ref);
109 if (unlikely(!oport))
112 /* Create multicast message */
114 hdr = &oport->publ.phdr;
115 msg_set_type(hdr, TIPC_MCAST_MSG);
116 msg_set_nametype(hdr, seq->type);
117 msg_set_namelower(hdr, seq->lower);
118 msg_set_nameupper(hdr, seq->upper);
119 msg_set_hdr_sz(hdr, MCAST_H_SIZE);
120 res = msg_build(hdr, msg_sect, num_sect, MAX_MSG_SIZE,
121 !oport->user_port, &buf);
125 /* Figure out where to send multicast message */
127 ext_targets = tipc_nametbl_mc_translate(seq->type, seq->lower, seq->upper,
128 TIPC_NODE_SCOPE, &dports);
130 /* Send message to destinations (duplicate it only if necessary) */
133 if (dports.count != 0) {
134 ibuf = skb_copy(buf, GFP_ATOMIC);
136 tipc_port_list_free(&dports);
141 res = tipc_bclink_send_msg(buf);
142 if ((res < 0) && (dports.count != 0)) {
151 tipc_port_recv_mcast(ibuf, &dports);
153 tipc_port_list_free(&dports);
159 * tipc_port_recv_mcast - deliver multicast message to all destination ports
161 * If there is no port list, perform a lookup to create one
164 void tipc_port_recv_mcast(struct sk_buff *buf, struct port_list *dp)
166 struct tipc_msg* msg;
167 struct port_list dports = {0, NULL, };
168 struct port_list *item = dp;
174 /* Create destination port list, if one wasn't supplied */
177 tipc_nametbl_mc_translate(msg_nametype(msg),
185 /* Deliver a copy of message to each destination port */
187 if (dp->count != 0) {
188 if (dp->count == 1) {
189 msg_set_destport(msg, dp->ports[0]);
190 tipc_port_recv_msg(buf);
191 tipc_port_list_free(dp);
194 for (; cnt < dp->count; cnt++) {
195 int index = cnt % PLSIZE;
196 struct sk_buff *b = skb_clone(buf, GFP_ATOMIC);
199 warn("Buffer allocation failure\n");
200 msg_dbg(msg, "LOST:");
203 if ((index == 0) && (cnt != 0)) {
206 msg_set_destport(buf_msg(b),item->ports[index]);
207 tipc_port_recv_msg(b);
212 tipc_port_list_free(dp);
216 * tipc_createport_raw - create a native TIPC port
218 * Returns local port reference
221 u32 tipc_createport_raw(void *usr_handle,
222 u32 (*dispatcher)(struct tipc_port *, struct sk_buff *),
223 void (*wakeup)(struct tipc_port *),
224 const u32 importance)
227 struct tipc_msg *msg;
230 p_ptr = kmalloc(sizeof(*p_ptr), GFP_ATOMIC);
232 warn("Memory squeeze; failed to create port\n");
235 memset(p_ptr, 0, sizeof(*p_ptr));
236 ref = tipc_ref_acquire(p_ptr, &p_ptr->publ.lock);
238 warn("Reference Table Exhausted\n");
244 p_ptr->publ.ref = ref;
245 msg = &p_ptr->publ.phdr;
246 msg_init(msg, DATA_LOW, TIPC_NAMED_MSG, TIPC_OK, LONG_H_SIZE, 0);
247 msg_set_orignode(msg, tipc_own_addr);
248 msg_set_prevnode(msg, tipc_own_addr);
249 msg_set_origport(msg, ref);
250 msg_set_importance(msg,importance);
251 p_ptr->last_in_seqno = 41;
253 p_ptr->publ.usr_handle = usr_handle;
254 INIT_LIST_HEAD(&p_ptr->wait_list);
255 INIT_LIST_HEAD(&p_ptr->subscription.nodesub_list);
256 p_ptr->congested_link = NULL;
257 p_ptr->max_pkt = MAX_PKT_DEFAULT;
258 p_ptr->dispatcher = dispatcher;
259 p_ptr->wakeup = wakeup;
260 p_ptr->user_port = NULL;
261 k_init_timer(&p_ptr->timer, (Handler)port_timeout, ref);
262 spin_lock_bh(&tipc_port_list_lock);
263 INIT_LIST_HEAD(&p_ptr->publications);
264 INIT_LIST_HEAD(&p_ptr->port_list);
265 list_add_tail(&p_ptr->port_list, &ports);
266 spin_unlock_bh(&tipc_port_list_lock);
267 tipc_port_unlock(p_ptr);
271 int tipc_deleteport(u32 ref)
274 struct sk_buff *buf = NULL;
276 tipc_withdraw(ref, 0, NULL);
277 p_ptr = tipc_port_lock(ref);
281 tipc_ref_discard(ref);
282 tipc_port_unlock(p_ptr);
284 k_cancel_timer(&p_ptr->timer);
285 if (p_ptr->publ.connected) {
286 buf = port_build_peer_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
287 tipc_nodesub_unsubscribe(&p_ptr->subscription);
289 if (p_ptr->user_port) {
290 tipc_reg_remove_port(p_ptr->user_port);
291 kfree(p_ptr->user_port);
294 spin_lock_bh(&tipc_port_list_lock);
295 list_del(&p_ptr->port_list);
296 list_del(&p_ptr->wait_list);
297 spin_unlock_bh(&tipc_port_list_lock);
298 k_term_timer(&p_ptr->timer);
300 dbg("Deleted port %u\n", ref);
301 tipc_net_route_msg(buf);
306 * tipc_get_port() - return port associated with 'ref'
308 * Note: Port is not locked.
311 struct tipc_port *tipc_get_port(const u32 ref)
313 return (struct tipc_port *)tipc_ref_deref(ref);
317 * tipc_get_handle - return user handle associated to port 'ref'
320 void *tipc_get_handle(const u32 ref)
325 p_ptr = tipc_port_lock(ref);
328 handle = p_ptr->publ.usr_handle;
329 tipc_port_unlock(p_ptr);
333 static int port_unreliable(struct port *p_ptr)
335 return msg_src_droppable(&p_ptr->publ.phdr);
338 int tipc_portunreliable(u32 ref, unsigned int *isunreliable)
342 p_ptr = tipc_port_lock(ref);
345 *isunreliable = port_unreliable(p_ptr);
346 spin_unlock_bh(p_ptr->publ.lock);
350 int tipc_set_portunreliable(u32 ref, unsigned int isunreliable)
354 p_ptr = tipc_port_lock(ref);
357 msg_set_src_droppable(&p_ptr->publ.phdr, (isunreliable != 0));
358 tipc_port_unlock(p_ptr);
362 static int port_unreturnable(struct port *p_ptr)
364 return msg_dest_droppable(&p_ptr->publ.phdr);
367 int tipc_portunreturnable(u32 ref, unsigned int *isunrejectable)
371 p_ptr = tipc_port_lock(ref);
374 *isunrejectable = port_unreturnable(p_ptr);
375 spin_unlock_bh(p_ptr->publ.lock);
379 int tipc_set_portunreturnable(u32 ref, unsigned int isunrejectable)
383 p_ptr = tipc_port_lock(ref);
386 msg_set_dest_droppable(&p_ptr->publ.phdr, (isunrejectable != 0));
387 tipc_port_unlock(p_ptr);
392 * port_build_proto_msg(): build a port level protocol
393 * or a connection abortion message. Called with
396 static struct sk_buff *port_build_proto_msg(u32 destport, u32 destnode,
397 u32 origport, u32 orignode,
398 u32 usr, u32 type, u32 err,
402 struct tipc_msg *msg;
404 buf = buf_acquire(LONG_H_SIZE);
407 msg_init(msg, usr, type, err, LONG_H_SIZE, destnode);
408 msg_set_destport(msg, destport);
409 msg_set_origport(msg, origport);
410 msg_set_destnode(msg, destnode);
411 msg_set_orignode(msg, orignode);
412 msg_set_transp_seqno(msg, seqno);
413 msg_set_msgcnt(msg, ack);
414 msg_dbg(msg, "PORT>SEND>:");
419 int tipc_set_msg_option(struct tipc_port *tp_ptr, const char *opt, const u32 sz)
421 msg_expand(&tp_ptr->phdr, msg_destnode(&tp_ptr->phdr));
422 msg_set_options(&tp_ptr->phdr, opt, sz);
426 int tipc_reject_msg(struct sk_buff *buf, u32 err)
428 struct tipc_msg *msg = buf_msg(buf);
429 struct sk_buff *rbuf;
430 struct tipc_msg *rmsg;
432 u32 imp = msg_importance(msg);
433 u32 data_sz = msg_data_sz(msg);
435 if (data_sz > MAX_REJECT_SIZE)
436 data_sz = MAX_REJECT_SIZE;
437 if (msg_connected(msg) && (imp < TIPC_CRITICAL_IMPORTANCE))
439 msg_dbg(msg, "port->rej: ");
441 /* discard rejected message if it shouldn't be returned to sender */
442 if (msg_errcode(msg) || msg_dest_droppable(msg)) {
447 /* construct rejected message */
449 hdr_sz = MCAST_H_SIZE;
451 hdr_sz = LONG_H_SIZE;
452 rbuf = buf_acquire(data_sz + hdr_sz);
457 rmsg = buf_msg(rbuf);
458 msg_init(rmsg, imp, msg_type(msg), err, hdr_sz, msg_orignode(msg));
459 msg_set_destport(rmsg, msg_origport(msg));
460 msg_set_prevnode(rmsg, tipc_own_addr);
461 msg_set_origport(rmsg, msg_destport(msg));
463 msg_set_orignode(rmsg, tipc_own_addr);
465 msg_set_orignode(rmsg, msg_destnode(msg));
466 msg_set_size(rmsg, data_sz + hdr_sz);
467 msg_set_nametype(rmsg, msg_nametype(msg));
468 msg_set_nameinst(rmsg, msg_nameinst(msg));
469 memcpy(rbuf->data + hdr_sz, msg_data(msg), data_sz);
471 /* send self-abort message when rejecting on a connected port */
472 if (msg_connected(msg)) {
473 struct sk_buff *abuf = NULL;
474 struct port *p_ptr = tipc_port_lock(msg_destport(msg));
477 if (p_ptr->publ.connected)
478 abuf = port_build_self_abort_msg(p_ptr, err);
479 tipc_port_unlock(p_ptr);
481 tipc_net_route_msg(abuf);
484 /* send rejected message */
486 tipc_net_route_msg(rbuf);
490 int tipc_port_reject_sections(struct port *p_ptr, struct tipc_msg *hdr,
491 struct iovec const *msg_sect, u32 num_sect,
497 res = msg_build(hdr, msg_sect, num_sect, MAX_MSG_SIZE,
498 !p_ptr->user_port, &buf);
502 return tipc_reject_msg(buf, err);
505 static void port_timeout(unsigned long ref)
507 struct port *p_ptr = tipc_port_lock(ref);
508 struct sk_buff *buf = NULL;
510 if (!p_ptr || !p_ptr->publ.connected)
513 /* Last probe answered ? */
514 if (p_ptr->probing_state == PROBING) {
515 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
517 buf = port_build_proto_msg(port_peerport(p_ptr),
518 port_peernode(p_ptr),
524 port_out_seqno(p_ptr),
526 port_incr_out_seqno(p_ptr);
527 p_ptr->probing_state = PROBING;
528 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
530 tipc_port_unlock(p_ptr);
531 tipc_net_route_msg(buf);
535 static void port_handle_node_down(unsigned long ref)
537 struct port *p_ptr = tipc_port_lock(ref);
538 struct sk_buff* buf = NULL;
542 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_NODE);
543 tipc_port_unlock(p_ptr);
544 tipc_net_route_msg(buf);
548 static struct sk_buff *port_build_self_abort_msg(struct port *p_ptr, u32 err)
550 u32 imp = msg_importance(&p_ptr->publ.phdr);
552 if (!p_ptr->publ.connected)
554 if (imp < TIPC_CRITICAL_IMPORTANCE)
556 return port_build_proto_msg(p_ptr->publ.ref,
558 port_peerport(p_ptr),
559 port_peernode(p_ptr),
563 p_ptr->last_in_seqno + 1,
568 static struct sk_buff *port_build_peer_abort_msg(struct port *p_ptr, u32 err)
570 u32 imp = msg_importance(&p_ptr->publ.phdr);
572 if (!p_ptr->publ.connected)
574 if (imp < TIPC_CRITICAL_IMPORTANCE)
576 return port_build_proto_msg(port_peerport(p_ptr),
577 port_peernode(p_ptr),
583 port_out_seqno(p_ptr),
587 void tipc_port_recv_proto_msg(struct sk_buff *buf)
589 struct tipc_msg *msg = buf_msg(buf);
590 struct port *p_ptr = tipc_port_lock(msg_destport(msg));
592 struct sk_buff *r_buf = NULL;
593 struct sk_buff *abort_buf = NULL;
595 msg_dbg(msg, "PORT<RECV<:");
598 err = TIPC_ERR_NO_PORT;
599 } else if (p_ptr->publ.connected) {
600 if (port_peernode(p_ptr) != msg_orignode(msg))
601 err = TIPC_ERR_NO_PORT;
602 if (port_peerport(p_ptr) != msg_origport(msg))
603 err = TIPC_ERR_NO_PORT;
604 if (!err && msg_routed(msg)) {
605 u32 seqno = msg_transp_seqno(msg);
606 u32 myno = ++p_ptr->last_in_seqno;
608 err = TIPC_ERR_NO_PORT;
609 abort_buf = port_build_self_abort_msg(p_ptr, err);
612 if (msg_type(msg) == CONN_ACK) {
613 int wakeup = tipc_port_congested(p_ptr) &&
614 p_ptr->publ.congested &&
616 p_ptr->acked += msg_msgcnt(msg);
617 if (tipc_port_congested(p_ptr))
619 p_ptr->publ.congested = 0;
622 p_ptr->wakeup(&p_ptr->publ);
625 } else if (p_ptr->publ.published) {
626 err = TIPC_ERR_NO_PORT;
629 r_buf = port_build_proto_msg(msg_origport(msg),
642 if (msg_type(msg) == CONN_PROBE) {
643 r_buf = port_build_proto_msg(msg_origport(msg),
650 port_out_seqno(p_ptr),
653 p_ptr->probing_state = CONFIRMED;
654 port_incr_out_seqno(p_ptr);
657 tipc_port_unlock(p_ptr);
658 tipc_net_route_msg(r_buf);
659 tipc_net_route_msg(abort_buf);
663 static void port_print(struct port *p_ptr, struct print_buf *buf, int full_id)
665 struct publication *publ;
668 tipc_printf(buf, "<%u.%u.%u:%u>:",
669 tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
670 tipc_node(tipc_own_addr), p_ptr->publ.ref);
672 tipc_printf(buf, "%-10u:", p_ptr->publ.ref);
674 if (p_ptr->publ.connected) {
675 u32 dport = port_peerport(p_ptr);
676 u32 destnode = port_peernode(p_ptr);
678 tipc_printf(buf, " connected to <%u.%u.%u:%u>",
679 tipc_zone(destnode), tipc_cluster(destnode),
680 tipc_node(destnode), dport);
681 if (p_ptr->publ.conn_type != 0)
682 tipc_printf(buf, " via {%u,%u}",
683 p_ptr->publ.conn_type,
684 p_ptr->publ.conn_instance);
686 else if (p_ptr->publ.published) {
687 tipc_printf(buf, " bound to");
688 list_for_each_entry(publ, &p_ptr->publications, pport_list) {
689 if (publ->lower == publ->upper)
690 tipc_printf(buf, " {%u,%u}", publ->type,
693 tipc_printf(buf, " {%u,%u,%u}", publ->type,
694 publ->lower, publ->upper);
697 tipc_printf(buf, "\n");
700 #define MAX_PORT_QUERY 32768
702 struct sk_buff *tipc_port_get_ports(void)
705 struct tlv_desc *rep_tlv;
710 buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_PORT_QUERY));
713 rep_tlv = (struct tlv_desc *)buf->data;
715 tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), MAX_PORT_QUERY);
716 spin_lock_bh(&tipc_port_list_lock);
717 list_for_each_entry(p_ptr, &ports, port_list) {
718 spin_lock_bh(p_ptr->publ.lock);
719 port_print(p_ptr, &pb, 0);
720 spin_unlock_bh(p_ptr->publ.lock);
722 spin_unlock_bh(&tipc_port_list_lock);
723 str_len = tipc_printbuf_validate(&pb);
725 skb_put(buf, TLV_SPACE(str_len));
726 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
733 #define MAX_PORT_STATS 2000
735 struct sk_buff *port_show_stats(const void *req_tlv_area, int req_tlv_space)
740 struct tlv_desc *rep_tlv;
744 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_PORT_REF))
745 return cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
747 ref = *(u32 *)TLV_DATA(req_tlv_area);
750 p_ptr = tipc_port_lock(ref);
752 return cfg_reply_error_string("port not found");
754 buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_PORT_STATS));
756 tipc_port_unlock(p_ptr);
759 rep_tlv = (struct tlv_desc *)buf->data;
761 tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), MAX_PORT_STATS);
762 port_print(p_ptr, &pb, 1);
763 /* NEED TO FILL IN ADDITIONAL PORT STATISTICS HERE */
764 tipc_port_unlock(p_ptr);
765 str_len = tipc_printbuf_validate(&pb);
767 skb_put(buf, TLV_SPACE(str_len));
768 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
775 void tipc_port_reinit(void)
778 struct tipc_msg *msg;
780 spin_lock_bh(&tipc_port_list_lock);
781 list_for_each_entry(p_ptr, &ports, port_list) {
782 msg = &p_ptr->publ.phdr;
783 if (msg_orignode(msg) == tipc_own_addr)
785 msg_set_orignode(msg, tipc_own_addr);
787 spin_unlock_bh(&tipc_port_list_lock);
792 * port_dispatcher_sigh(): Signal handler for messages destinated
793 * to the tipc_port interface.
796 static void port_dispatcher_sigh(void *dummy)
800 spin_lock_bh(&queue_lock);
801 buf = msg_queue_head;
802 msg_queue_head = NULL;
803 spin_unlock_bh(&queue_lock);
807 struct user_port *up_ptr;
808 struct tipc_portid orig;
809 struct tipc_name_seq dseq;
815 struct sk_buff *next = buf->next;
816 struct tipc_msg *msg = buf_msg(buf);
817 u32 dref = msg_destport(msg);
819 message_type = msg_type(msg);
820 if (message_type > TIPC_DIRECT_MSG)
821 goto reject; /* Unsupported message type */
823 p_ptr = tipc_port_lock(dref);
825 goto reject; /* Port deleted while msg in queue */
827 orig.ref = msg_origport(msg);
828 orig.node = msg_orignode(msg);
829 up_ptr = p_ptr->user_port;
830 usr_handle = up_ptr->usr_handle;
831 connected = p_ptr->publ.connected;
832 published = p_ptr->publ.published;
834 if (unlikely(msg_errcode(msg)))
837 switch (message_type) {
840 tipc_conn_msg_event cb = up_ptr->conn_msg_cb;
841 u32 peer_port = port_peerport(p_ptr);
842 u32 peer_node = port_peernode(p_ptr);
844 spin_unlock_bh(p_ptr->publ.lock);
845 if (unlikely(!connected)) {
846 if (unlikely(published))
848 tipc_connect2port(dref,&orig);
850 if (unlikely(msg_origport(msg) != peer_port))
852 if (unlikely(msg_orignode(msg) != peer_node))
856 if (unlikely(++p_ptr->publ.conn_unacked >=
857 TIPC_FLOW_CONTROL_WIN))
858 tipc_acknowledge(dref,
859 p_ptr->publ.conn_unacked);
860 skb_pull(buf, msg_hdr_sz(msg));
861 cb(usr_handle, dref, &buf, msg_data(msg),
865 case TIPC_DIRECT_MSG:{
866 tipc_msg_event cb = up_ptr->msg_cb;
868 spin_unlock_bh(p_ptr->publ.lock);
869 if (unlikely(connected))
873 skb_pull(buf, msg_hdr_sz(msg));
874 cb(usr_handle, dref, &buf, msg_data(msg),
875 msg_data_sz(msg), msg_importance(msg),
880 case TIPC_NAMED_MSG:{
881 tipc_named_msg_event cb = up_ptr->named_msg_cb;
883 spin_unlock_bh(p_ptr->publ.lock);
884 if (unlikely(connected))
888 if (unlikely(!published))
890 dseq.type = msg_nametype(msg);
891 dseq.lower = msg_nameinst(msg);
892 dseq.upper = (message_type == TIPC_NAMED_MSG)
893 ? dseq.lower : msg_nameupper(msg);
894 skb_pull(buf, msg_hdr_sz(msg));
895 cb(usr_handle, dref, &buf, msg_data(msg),
896 msg_data_sz(msg), msg_importance(msg),
906 switch (message_type) {
909 tipc_conn_shutdown_event cb =
911 u32 peer_port = port_peerport(p_ptr);
912 u32 peer_node = port_peernode(p_ptr);
914 spin_unlock_bh(p_ptr->publ.lock);
915 if (!connected || !cb)
917 if (msg_origport(msg) != peer_port)
919 if (msg_orignode(msg) != peer_node)
921 tipc_disconnect(dref);
922 skb_pull(buf, msg_hdr_sz(msg));
923 cb(usr_handle, dref, &buf, msg_data(msg),
924 msg_data_sz(msg), msg_errcode(msg));
927 case TIPC_DIRECT_MSG:{
928 tipc_msg_err_event cb = up_ptr->err_cb;
930 spin_unlock_bh(p_ptr->publ.lock);
931 if (connected || !cb)
933 skb_pull(buf, msg_hdr_sz(msg));
934 cb(usr_handle, dref, &buf, msg_data(msg),
935 msg_data_sz(msg), msg_errcode(msg), &orig);
939 case TIPC_NAMED_MSG:{
940 tipc_named_msg_err_event cb =
941 up_ptr->named_err_cb;
943 spin_unlock_bh(p_ptr->publ.lock);
944 if (connected || !cb)
946 dseq.type = msg_nametype(msg);
947 dseq.lower = msg_nameinst(msg);
948 dseq.upper = (message_type == TIPC_NAMED_MSG)
949 ? dseq.lower : msg_nameupper(msg);
950 skb_pull(buf, msg_hdr_sz(msg));
951 cb(usr_handle, dref, &buf, msg_data(msg),
952 msg_data_sz(msg), msg_errcode(msg), &dseq);
961 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
967 * port_dispatcher(): Dispatcher for messages destinated
968 * to the tipc_port interface. Called with port locked.
971 static u32 port_dispatcher(struct tipc_port *dummy, struct sk_buff *buf)
974 spin_lock_bh(&queue_lock);
975 if (msg_queue_head) {
976 msg_queue_tail->next = buf;
977 msg_queue_tail = buf;
979 msg_queue_tail = msg_queue_head = buf;
980 tipc_k_signal((Handler)port_dispatcher_sigh, 0);
982 spin_unlock_bh(&queue_lock);
987 * Wake up port after congestion: Called with port locked,
991 static void port_wakeup_sh(unsigned long ref)
994 struct user_port *up_ptr;
995 tipc_continue_event cb = NULL;
998 p_ptr = tipc_port_lock(ref);
1000 up_ptr = p_ptr->user_port;
1002 cb = up_ptr->continue_event_cb;
1003 uh = up_ptr->usr_handle;
1005 tipc_port_unlock(p_ptr);
1012 static void port_wakeup(struct tipc_port *p_ptr)
1014 tipc_k_signal((Handler)port_wakeup_sh, p_ptr->ref);
1017 void tipc_acknowledge(u32 ref, u32 ack)
1020 struct sk_buff *buf = NULL;
1022 p_ptr = tipc_port_lock(ref);
1025 if (p_ptr->publ.connected) {
1026 p_ptr->publ.conn_unacked -= ack;
1027 buf = port_build_proto_msg(port_peerport(p_ptr),
1028 port_peernode(p_ptr),
1034 port_out_seqno(p_ptr),
1037 tipc_port_unlock(p_ptr);
1038 tipc_net_route_msg(buf);
1042 * tipc_createport(): user level call. Will add port to
1043 * registry if non-zero user_ref.
1046 int tipc_createport(u32 user_ref,
1048 unsigned int importance,
1049 tipc_msg_err_event error_cb,
1050 tipc_named_msg_err_event named_error_cb,
1051 tipc_conn_shutdown_event conn_error_cb,
1052 tipc_msg_event msg_cb,
1053 tipc_named_msg_event named_msg_cb,
1054 tipc_conn_msg_event conn_msg_cb,
1055 tipc_continue_event continue_event_cb,/* May be zero */
1058 struct user_port *up_ptr;
1062 up_ptr = (struct user_port *)kmalloc(sizeof(*up_ptr), GFP_ATOMIC);
1063 if (up_ptr == NULL) {
1064 warn("Port creation failed, no memory\n");
1067 ref = tipc_createport_raw(NULL, port_dispatcher, port_wakeup, importance);
1068 p_ptr = tipc_port_lock(ref);
1074 p_ptr->user_port = up_ptr;
1075 up_ptr->user_ref = user_ref;
1076 up_ptr->usr_handle = usr_handle;
1077 up_ptr->ref = p_ptr->publ.ref;
1078 up_ptr->err_cb = error_cb;
1079 up_ptr->named_err_cb = named_error_cb;
1080 up_ptr->conn_err_cb = conn_error_cb;
1081 up_ptr->msg_cb = msg_cb;
1082 up_ptr->named_msg_cb = named_msg_cb;
1083 up_ptr->conn_msg_cb = conn_msg_cb;
1084 up_ptr->continue_event_cb = continue_event_cb;
1085 INIT_LIST_HEAD(&up_ptr->uport_list);
1086 tipc_reg_add_port(up_ptr);
1087 *portref = p_ptr->publ.ref;
1088 dbg(" tipc_createport: %x with ref %u\n", p_ptr, p_ptr->publ.ref);
1089 tipc_port_unlock(p_ptr);
1093 int tipc_ownidentity(u32 ref, struct tipc_portid *id)
1096 id->node = tipc_own_addr;
1100 int tipc_portimportance(u32 ref, unsigned int *importance)
1104 p_ptr = tipc_port_lock(ref);
1107 *importance = (unsigned int)msg_importance(&p_ptr->publ.phdr);
1108 spin_unlock_bh(p_ptr->publ.lock);
1112 int tipc_set_portimportance(u32 ref, unsigned int imp)
1116 if (imp > TIPC_CRITICAL_IMPORTANCE)
1119 p_ptr = tipc_port_lock(ref);
1122 msg_set_importance(&p_ptr->publ.phdr, (u32)imp);
1123 spin_unlock_bh(p_ptr->publ.lock);
1128 int tipc_publish(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
1131 struct publication *publ;
1135 p_ptr = tipc_port_lock(ref);
1136 dbg("tipc_publ %u, p_ptr = %x, conn = %x, scope = %x, "
1137 "lower = %u, upper = %u\n",
1138 ref, p_ptr, p_ptr->publ.connected, scope, seq->lower, seq->upper);
1141 if (p_ptr->publ.connected)
1143 if (seq->lower > seq->upper)
1145 if ((scope < TIPC_ZONE_SCOPE) || (scope > TIPC_NODE_SCOPE))
1147 key = ref + p_ptr->pub_count + 1;
1152 publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
1153 scope, p_ptr->publ.ref, key);
1155 list_add(&publ->pport_list, &p_ptr->publications);
1157 p_ptr->publ.published = 1;
1161 tipc_port_unlock(p_ptr);
1165 int tipc_withdraw(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
1168 struct publication *publ;
1169 struct publication *tpubl;
1172 p_ptr = tipc_port_lock(ref);
1176 list_for_each_entry_safe(publ, tpubl,
1177 &p_ptr->publications, pport_list) {
1178 tipc_nametbl_withdraw(publ->type, publ->lower,
1179 publ->ref, publ->key);
1183 list_for_each_entry_safe(publ, tpubl,
1184 &p_ptr->publications, pport_list) {
1185 if (publ->scope != scope)
1187 if (publ->type != seq->type)
1189 if (publ->lower != seq->lower)
1191 if (publ->upper != seq->upper)
1193 tipc_nametbl_withdraw(publ->type, publ->lower,
1194 publ->ref, publ->key);
1199 if (list_empty(&p_ptr->publications))
1200 p_ptr->publ.published = 0;
1201 tipc_port_unlock(p_ptr);
1205 int tipc_connect2port(u32 ref, struct tipc_portid const *peer)
1208 struct tipc_msg *msg;
1211 p_ptr = tipc_port_lock(ref);
1214 if (p_ptr->publ.published || p_ptr->publ.connected)
1219 msg = &p_ptr->publ.phdr;
1220 msg_set_destnode(msg, peer->node);
1221 msg_set_destport(msg, peer->ref);
1222 msg_set_orignode(msg, tipc_own_addr);
1223 msg_set_origport(msg, p_ptr->publ.ref);
1224 msg_set_transp_seqno(msg, 42);
1225 msg_set_type(msg, TIPC_CONN_MSG);
1226 if (!may_route(peer->node))
1227 msg_set_hdr_sz(msg, SHORT_H_SIZE);
1229 msg_set_hdr_sz(msg, LONG_H_SIZE);
1231 p_ptr->probing_interval = PROBING_INTERVAL;
1232 p_ptr->probing_state = CONFIRMED;
1233 p_ptr->publ.connected = 1;
1234 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
1236 tipc_nodesub_subscribe(&p_ptr->subscription,peer->node,
1237 (void *)(unsigned long)ref,
1238 (net_ev_handler)port_handle_node_down);
1241 tipc_port_unlock(p_ptr);
1242 p_ptr->max_pkt = tipc_link_get_max_pkt(peer->node, ref);
1247 * tipc_disconnect(): Disconnect port form peer.
1248 * This is a node local operation.
1251 int tipc_disconnect(u32 ref)
1254 int res = -ENOTCONN;
1256 p_ptr = tipc_port_lock(ref);
1259 if (p_ptr->publ.connected) {
1260 p_ptr->publ.connected = 0;
1261 /* let timer expire on it's own to avoid deadlock! */
1262 tipc_nodesub_unsubscribe(&p_ptr->subscription);
1265 tipc_port_unlock(p_ptr);
1270 * tipc_shutdown(): Send a SHUTDOWN msg to peer and disconnect
1272 int tipc_shutdown(u32 ref)
1275 struct sk_buff *buf = NULL;
1277 p_ptr = tipc_port_lock(ref);
1281 if (p_ptr->publ.connected) {
1282 u32 imp = msg_importance(&p_ptr->publ.phdr);
1283 if (imp < TIPC_CRITICAL_IMPORTANCE)
1285 buf = port_build_proto_msg(port_peerport(p_ptr),
1286 port_peernode(p_ptr),
1292 port_out_seqno(p_ptr),
1295 tipc_port_unlock(p_ptr);
1296 tipc_net_route_msg(buf);
1297 return tipc_disconnect(ref);
1300 int tipc_isconnected(u32 ref, int *isconnected)
1304 p_ptr = tipc_port_lock(ref);
1307 *isconnected = p_ptr->publ.connected;
1308 tipc_port_unlock(p_ptr);
1312 int tipc_peer(u32 ref, struct tipc_portid *peer)
1317 p_ptr = tipc_port_lock(ref);
1320 if (p_ptr->publ.connected) {
1321 peer->ref = port_peerport(p_ptr);
1322 peer->node = port_peernode(p_ptr);
1326 tipc_port_unlock(p_ptr);
1330 int tipc_ref_valid(u32 ref)
1332 /* Works irrespective of type */
1333 return !!tipc_ref_deref(ref);
1338 * tipc_port_recv_sections(): Concatenate and deliver sectioned
1339 * message for this node.
1342 int tipc_port_recv_sections(struct port *sender, unsigned int num_sect,
1343 struct iovec const *msg_sect)
1345 struct sk_buff *buf;
1348 res = msg_build(&sender->publ.phdr, msg_sect, num_sect,
1349 MAX_MSG_SIZE, !sender->user_port, &buf);
1351 tipc_port_recv_msg(buf);
1356 * tipc_send - send message sections on connection
1359 int tipc_send(u32 ref, unsigned int num_sect, struct iovec const *msg_sect)
1365 p_ptr = tipc_port_deref(ref);
1366 if (!p_ptr || !p_ptr->publ.connected)
1369 p_ptr->publ.congested = 1;
1370 if (!tipc_port_congested(p_ptr)) {
1371 destnode = port_peernode(p_ptr);
1372 if (likely(destnode != tipc_own_addr))
1373 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
1376 res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
1378 if (likely(res != -ELINKCONG)) {
1379 port_incr_out_seqno(p_ptr);
1380 p_ptr->publ.congested = 0;
1385 if (port_unreliable(p_ptr)) {
1386 p_ptr->publ.congested = 0;
1387 /* Just calculate msg length and return */
1388 return msg_calc_data_size(msg_sect, num_sect);
1394 * tipc_send_buf - send message buffer on connection
1397 int tipc_send_buf(u32 ref, struct sk_buff *buf, unsigned int dsz)
1400 struct tipc_msg *msg;
1406 p_ptr = tipc_port_deref(ref);
1407 if (!p_ptr || !p_ptr->publ.connected)
1410 msg = &p_ptr->publ.phdr;
1411 hsz = msg_hdr_sz(msg);
1413 msg_set_size(msg, sz);
1414 if (skb_cow(buf, hsz))
1418 memcpy(buf->data, (unchar *)msg, hsz);
1419 destnode = msg_destnode(msg);
1420 p_ptr->publ.congested = 1;
1421 if (!tipc_port_congested(p_ptr)) {
1422 if (likely(destnode != tipc_own_addr))
1423 res = tipc_send_buf_fast(buf, destnode);
1425 tipc_port_recv_msg(buf);
1428 if (likely(res != -ELINKCONG)) {
1429 port_incr_out_seqno(p_ptr);
1431 p_ptr->publ.congested = 0;
1435 if (port_unreliable(p_ptr)) {
1436 p_ptr->publ.congested = 0;
1443 * tipc_forward2name - forward message sections to port name
1446 int tipc_forward2name(u32 ref,
1447 struct tipc_name const *name,
1450 struct iovec const *msg_sect,
1451 struct tipc_portid const *orig,
1452 unsigned int importance)
1455 struct tipc_msg *msg;
1456 u32 destnode = domain;
1460 p_ptr = tipc_port_deref(ref);
1461 if (!p_ptr || p_ptr->publ.connected)
1464 msg = &p_ptr->publ.phdr;
1465 msg_set_type(msg, TIPC_NAMED_MSG);
1466 msg_set_orignode(msg, orig->node);
1467 msg_set_origport(msg, orig->ref);
1468 msg_set_hdr_sz(msg, LONG_H_SIZE);
1469 msg_set_nametype(msg, name->type);
1470 msg_set_nameinst(msg, name->instance);
1471 msg_set_lookup_scope(msg, addr_scope(domain));
1472 if (importance <= TIPC_CRITICAL_IMPORTANCE)
1473 msg_set_importance(msg,importance);
1474 destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
1475 msg_set_destnode(msg, destnode);
1476 msg_set_destport(msg, destport);
1478 if (likely(destport || destnode)) {
1480 if (likely(destnode == tipc_own_addr))
1481 return tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
1482 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
1484 if (likely(res != -ELINKCONG))
1486 if (port_unreliable(p_ptr)) {
1487 /* Just calculate msg length and return */
1488 return msg_calc_data_size(msg_sect, num_sect);
1492 return tipc_port_reject_sections(p_ptr, msg, msg_sect, num_sect,
1497 * tipc_send2name - send message sections to port name
1500 int tipc_send2name(u32 ref,
1501 struct tipc_name const *name,
1502 unsigned int domain,
1503 unsigned int num_sect,
1504 struct iovec const *msg_sect)
1506 struct tipc_portid orig;
1509 orig.node = tipc_own_addr;
1510 return tipc_forward2name(ref, name, domain, num_sect, msg_sect, &orig,
1511 TIPC_PORT_IMPORTANCE);
1515 * tipc_forward_buf2name - forward message buffer to port name
1518 int tipc_forward_buf2name(u32 ref,
1519 struct tipc_name const *name,
1521 struct sk_buff *buf,
1523 struct tipc_portid const *orig,
1524 unsigned int importance)
1527 struct tipc_msg *msg;
1528 u32 destnode = domain;
1532 p_ptr = (struct port *)tipc_ref_deref(ref);
1533 if (!p_ptr || p_ptr->publ.connected)
1536 msg = &p_ptr->publ.phdr;
1537 if (importance <= TIPC_CRITICAL_IMPORTANCE)
1538 msg_set_importance(msg, importance);
1539 msg_set_type(msg, TIPC_NAMED_MSG);
1540 msg_set_orignode(msg, orig->node);
1541 msg_set_origport(msg, orig->ref);
1542 msg_set_nametype(msg, name->type);
1543 msg_set_nameinst(msg, name->instance);
1544 msg_set_lookup_scope(msg, addr_scope(domain));
1545 msg_set_hdr_sz(msg, LONG_H_SIZE);
1546 msg_set_size(msg, LONG_H_SIZE + dsz);
1547 destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
1548 msg_set_destnode(msg, destnode);
1549 msg_set_destport(msg, destport);
1550 msg_dbg(msg, "forw2name ==> ");
1551 if (skb_cow(buf, LONG_H_SIZE))
1553 skb_push(buf, LONG_H_SIZE);
1554 memcpy(buf->data, (unchar *)msg, LONG_H_SIZE);
1555 msg_dbg(buf_msg(buf),"PREP:");
1556 if (likely(destport || destnode)) {
1558 if (destnode == tipc_own_addr)
1559 return tipc_port_recv_msg(buf);
1560 res = tipc_send_buf_fast(buf, destnode);
1561 if (likely(res != -ELINKCONG))
1563 if (port_unreliable(p_ptr))
1567 return tipc_reject_msg(buf, TIPC_ERR_NO_NAME);
1571 * tipc_send_buf2name - send message buffer to port name
1574 int tipc_send_buf2name(u32 ref,
1575 struct tipc_name const *dest,
1577 struct sk_buff *buf,
1580 struct tipc_portid orig;
1583 orig.node = tipc_own_addr;
1584 return tipc_forward_buf2name(ref, dest, domain, buf, dsz, &orig,
1585 TIPC_PORT_IMPORTANCE);
1589 * tipc_forward2port - forward message sections to port identity
1592 int tipc_forward2port(u32 ref,
1593 struct tipc_portid const *dest,
1594 unsigned int num_sect,
1595 struct iovec const *msg_sect,
1596 struct tipc_portid const *orig,
1597 unsigned int importance)
1600 struct tipc_msg *msg;
1603 p_ptr = tipc_port_deref(ref);
1604 if (!p_ptr || p_ptr->publ.connected)
1607 msg = &p_ptr->publ.phdr;
1608 msg_set_type(msg, TIPC_DIRECT_MSG);
1609 msg_set_orignode(msg, orig->node);
1610 msg_set_origport(msg, orig->ref);
1611 msg_set_destnode(msg, dest->node);
1612 msg_set_destport(msg, dest->ref);
1613 msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
1614 if (importance <= TIPC_CRITICAL_IMPORTANCE)
1615 msg_set_importance(msg, importance);
1617 if (dest->node == tipc_own_addr)
1618 return tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
1619 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect, dest->node);
1620 if (likely(res != -ELINKCONG))
1622 if (port_unreliable(p_ptr)) {
1623 /* Just calculate msg length and return */
1624 return msg_calc_data_size(msg_sect, num_sect);
1630 * tipc_send2port - send message sections to port identity
1633 int tipc_send2port(u32 ref,
1634 struct tipc_portid const *dest,
1635 unsigned int num_sect,
1636 struct iovec const *msg_sect)
1638 struct tipc_portid orig;
1641 orig.node = tipc_own_addr;
1642 return tipc_forward2port(ref, dest, num_sect, msg_sect, &orig,
1643 TIPC_PORT_IMPORTANCE);
1647 * tipc_forward_buf2port - forward message buffer to port identity
1649 int tipc_forward_buf2port(u32 ref,
1650 struct tipc_portid const *dest,
1651 struct sk_buff *buf,
1653 struct tipc_portid const *orig,
1654 unsigned int importance)
1657 struct tipc_msg *msg;
1660 p_ptr = (struct port *)tipc_ref_deref(ref);
1661 if (!p_ptr || p_ptr->publ.connected)
1664 msg = &p_ptr->publ.phdr;
1665 msg_set_type(msg, TIPC_DIRECT_MSG);
1666 msg_set_orignode(msg, orig->node);
1667 msg_set_origport(msg, orig->ref);
1668 msg_set_destnode(msg, dest->node);
1669 msg_set_destport(msg, dest->ref);
1670 msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
1671 if (importance <= TIPC_CRITICAL_IMPORTANCE)
1672 msg_set_importance(msg, importance);
1673 msg_set_size(msg, DIR_MSG_H_SIZE + dsz);
1674 if (skb_cow(buf, DIR_MSG_H_SIZE))
1677 skb_push(buf, DIR_MSG_H_SIZE);
1678 memcpy(buf->data, (unchar *)msg, DIR_MSG_H_SIZE);
1679 msg_dbg(msg, "buf2port: ");
1681 if (dest->node == tipc_own_addr)
1682 return tipc_port_recv_msg(buf);
1683 res = tipc_send_buf_fast(buf, dest->node);
1684 if (likely(res != -ELINKCONG))
1686 if (port_unreliable(p_ptr))
1692 * tipc_send_buf2port - send message buffer to port identity
1695 int tipc_send_buf2port(u32 ref,
1696 struct tipc_portid const *dest,
1697 struct sk_buff *buf,
1700 struct tipc_portid orig;
1703 orig.node = tipc_own_addr;
1704 return tipc_forward_buf2port(ref, dest, buf, dsz, &orig,
1705 TIPC_PORT_IMPORTANCE);