2 * net/tipc/port.c: TIPC port code
4 * Copyright (c) 1992-2007, Ericsson AB
5 * Copyright (c) 2004-2007, Wind River Systems
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the names of the copyright holders nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
20 * Alternatively, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") version 2 as published by the Free
22 * Software Foundation.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
44 #include "name_table.h"
49 /* Connection management: */
50 #define PROBING_INTERVAL 3600000 /* [ms] => 1 h */
54 #define MAX_REJECT_SIZE 1024
56 static struct sk_buff *msg_queue_head = NULL;
57 static struct sk_buff *msg_queue_tail = NULL;
59 DEFINE_SPINLOCK(tipc_port_list_lock);
60 static DEFINE_SPINLOCK(queue_lock);
62 static LIST_HEAD(ports);
63 static void port_handle_node_down(unsigned long ref);
64 static struct sk_buff* port_build_self_abort_msg(struct port *,u32 err);
65 static struct sk_buff* port_build_peer_abort_msg(struct port *,u32 err);
66 static void port_timeout(unsigned long ref);
69 static u32 port_peernode(struct port *p_ptr)
71 return msg_destnode(&p_ptr->publ.phdr);
74 static u32 port_peerport(struct port *p_ptr)
76 return msg_destport(&p_ptr->publ.phdr);
79 static u32 port_out_seqno(struct port *p_ptr)
81 return msg_transp_seqno(&p_ptr->publ.phdr);
84 static void port_incr_out_seqno(struct port *p_ptr)
86 struct tipc_msg *m = &p_ptr->publ.phdr;
88 if (likely(!msg_routed(m)))
90 msg_set_transp_seqno(m, (msg_transp_seqno(m) + 1));
94 * tipc_multicast - send a multicast message to local and remote destinations
97 int tipc_multicast(u32 ref, struct tipc_name_seq const *seq, u32 domain,
98 u32 num_sect, struct iovec const *msg_sect)
100 struct tipc_msg *hdr;
102 struct sk_buff *ibuf = NULL;
103 struct port_list dports = {0, NULL, };
104 struct port *oport = tipc_port_deref(ref);
108 if (unlikely(!oport))
111 /* Create multicast message */
113 hdr = &oport->publ.phdr;
114 msg_set_type(hdr, TIPC_MCAST_MSG);
115 msg_set_nametype(hdr, seq->type);
116 msg_set_namelower(hdr, seq->lower);
117 msg_set_nameupper(hdr, seq->upper);
118 msg_set_hdr_sz(hdr, MCAST_H_SIZE);
119 res = msg_build(hdr, msg_sect, num_sect, MAX_MSG_SIZE,
120 !oport->user_port, &buf);
124 /* Figure out where to send multicast message */
126 ext_targets = tipc_nametbl_mc_translate(seq->type, seq->lower, seq->upper,
127 TIPC_NODE_SCOPE, &dports);
129 /* Send message to destinations (duplicate it only if necessary) */
132 if (dports.count != 0) {
133 ibuf = skb_copy(buf, GFP_ATOMIC);
135 tipc_port_list_free(&dports);
140 res = tipc_bclink_send_msg(buf);
141 if ((res < 0) && (dports.count != 0)) {
150 tipc_port_recv_mcast(ibuf, &dports);
152 tipc_port_list_free(&dports);
158 * tipc_port_recv_mcast - deliver multicast message to all destination ports
160 * If there is no port list, perform a lookup to create one
163 void tipc_port_recv_mcast(struct sk_buff *buf, struct port_list *dp)
165 struct tipc_msg* msg;
166 struct port_list dports = {0, NULL, };
167 struct port_list *item = dp;
172 /* Create destination port list, if one wasn't supplied */
175 tipc_nametbl_mc_translate(msg_nametype(msg),
183 /* Deliver a copy of message to each destination port */
185 if (dp->count != 0) {
186 if (dp->count == 1) {
187 msg_set_destport(msg, dp->ports[0]);
188 tipc_port_recv_msg(buf);
189 tipc_port_list_free(dp);
192 for (; cnt < dp->count; cnt++) {
193 int index = cnt % PLSIZE;
194 struct sk_buff *b = skb_clone(buf, GFP_ATOMIC);
197 warn("Unable to deliver multicast message(s)\n");
198 msg_dbg(msg, "LOST:");
201 if ((index == 0) && (cnt != 0)) {
204 msg_set_destport(buf_msg(b),item->ports[index]);
205 tipc_port_recv_msg(b);
210 tipc_port_list_free(dp);
214 * tipc_createport_raw - create a generic TIPC port
216 * Returns port reference, or 0 if unable to create it
218 * Note: The newly created port is returned in the locked state.
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,
225 struct tipc_port **tp_ptr)
228 struct tipc_msg *msg;
231 p_ptr = kzalloc(sizeof(*p_ptr), GFP_ATOMIC);
233 warn("Port creation failed, no memory\n");
236 ref = tipc_ref_acquire(p_ptr, &p_ptr->publ.lock);
238 warn("Port creation failed, reference table exhausted\n");
243 p_ptr->publ.usr_handle = usr_handle;
244 p_ptr->publ.max_pkt = MAX_PKT_DEFAULT;
245 p_ptr->publ.ref = ref;
246 msg = &p_ptr->publ.phdr;
247 msg_init(msg, TIPC_LOW_IMPORTANCE, TIPC_NAMED_MSG, LONG_H_SIZE, 0);
248 msg_set_orignode(msg, tipc_own_addr);
249 msg_set_prevnode(msg, tipc_own_addr);
250 msg_set_origport(msg, ref);
251 msg_set_importance(msg,importance);
252 p_ptr->last_in_seqno = 41;
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->dispatcher = dispatcher;
258 p_ptr->wakeup = wakeup;
259 p_ptr->user_port = NULL;
260 k_init_timer(&p_ptr->timer, (Handler)port_timeout, ref);
261 spin_lock_bh(&tipc_port_list_lock);
262 INIT_LIST_HEAD(&p_ptr->publications);
263 INIT_LIST_HEAD(&p_ptr->port_list);
264 list_add_tail(&p_ptr->port_list, &ports);
265 spin_unlock_bh(&tipc_port_list_lock);
266 *tp_ptr = &p_ptr->publ;
270 int tipc_deleteport(u32 ref)
273 struct sk_buff *buf = NULL;
275 tipc_withdraw(ref, 0, NULL);
276 p_ptr = tipc_port_lock(ref);
280 tipc_ref_discard(ref);
281 tipc_port_unlock(p_ptr);
283 k_cancel_timer(&p_ptr->timer);
284 if (p_ptr->publ.connected) {
285 buf = port_build_peer_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
286 tipc_nodesub_unsubscribe(&p_ptr->subscription);
288 if (p_ptr->user_port) {
289 tipc_reg_remove_port(p_ptr->user_port);
290 kfree(p_ptr->user_port);
293 spin_lock_bh(&tipc_port_list_lock);
294 list_del(&p_ptr->port_list);
295 list_del(&p_ptr->wait_list);
296 spin_unlock_bh(&tipc_port_list_lock);
297 k_term_timer(&p_ptr->timer);
299 dbg("Deleted port %u\n", ref);
300 tipc_net_route_msg(buf);
305 * tipc_get_port() - return port associated with 'ref'
307 * Note: Port is not locked.
310 struct tipc_port *tipc_get_port(const u32 ref)
312 return (struct tipc_port *)tipc_ref_deref(ref);
316 * tipc_get_handle - return user handle associated to port 'ref'
319 void *tipc_get_handle(const u32 ref)
324 p_ptr = tipc_port_lock(ref);
327 handle = p_ptr->publ.usr_handle;
328 tipc_port_unlock(p_ptr);
332 static int port_unreliable(struct port *p_ptr)
334 return msg_src_droppable(&p_ptr->publ.phdr);
337 int tipc_portunreliable(u32 ref, unsigned int *isunreliable)
341 p_ptr = tipc_port_lock(ref);
344 *isunreliable = port_unreliable(p_ptr);
345 tipc_port_unlock(p_ptr);
349 int tipc_set_portunreliable(u32 ref, unsigned int isunreliable)
353 p_ptr = tipc_port_lock(ref);
356 msg_set_src_droppable(&p_ptr->publ.phdr, (isunreliable != 0));
357 tipc_port_unlock(p_ptr);
361 static int port_unreturnable(struct port *p_ptr)
363 return msg_dest_droppable(&p_ptr->publ.phdr);
366 int tipc_portunreturnable(u32 ref, unsigned int *isunrejectable)
370 p_ptr = tipc_port_lock(ref);
373 *isunrejectable = port_unreturnable(p_ptr);
374 tipc_port_unlock(p_ptr);
378 int tipc_set_portunreturnable(u32 ref, unsigned int isunrejectable)
382 p_ptr = tipc_port_lock(ref);
385 msg_set_dest_droppable(&p_ptr->publ.phdr, (isunrejectable != 0));
386 tipc_port_unlock(p_ptr);
391 * port_build_proto_msg(): build a port level protocol
392 * or a connection abortion message. Called with
395 static struct sk_buff *port_build_proto_msg(u32 destport, u32 destnode,
396 u32 origport, u32 orignode,
397 u32 usr, u32 type, u32 err,
401 struct tipc_msg *msg;
403 buf = buf_acquire(LONG_H_SIZE);
406 msg_init(msg, usr, type, LONG_H_SIZE, destnode);
407 msg_set_errcode(msg, err);
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_reject_msg(struct sk_buff *buf, u32 err)
421 struct tipc_msg *msg = buf_msg(buf);
422 struct sk_buff *rbuf;
423 struct tipc_msg *rmsg;
425 u32 imp = msg_importance(msg);
426 u32 data_sz = msg_data_sz(msg);
428 if (data_sz > MAX_REJECT_SIZE)
429 data_sz = MAX_REJECT_SIZE;
430 if (msg_connected(msg) && (imp < TIPC_CRITICAL_IMPORTANCE))
432 msg_dbg(msg, "port->rej: ");
434 /* discard rejected message if it shouldn't be returned to sender */
435 if (msg_errcode(msg) || msg_dest_droppable(msg)) {
440 /* construct rejected message */
442 hdr_sz = MCAST_H_SIZE;
444 hdr_sz = LONG_H_SIZE;
445 rbuf = buf_acquire(data_sz + hdr_sz);
450 rmsg = buf_msg(rbuf);
451 msg_init(rmsg, imp, msg_type(msg), hdr_sz, msg_orignode(msg));
452 msg_set_errcode(rmsg, err);
453 msg_set_destport(rmsg, msg_origport(msg));
454 msg_set_prevnode(rmsg, tipc_own_addr);
455 msg_set_origport(rmsg, msg_destport(msg));
457 msg_set_orignode(rmsg, tipc_own_addr);
459 msg_set_orignode(rmsg, msg_destnode(msg));
460 msg_set_size(rmsg, data_sz + hdr_sz);
461 msg_set_nametype(rmsg, msg_nametype(msg));
462 msg_set_nameinst(rmsg, msg_nameinst(msg));
463 skb_copy_to_linear_data_offset(rbuf, hdr_sz, msg_data(msg), data_sz);
465 /* send self-abort message when rejecting on a connected port */
466 if (msg_connected(msg)) {
467 struct sk_buff *abuf = NULL;
468 struct port *p_ptr = tipc_port_lock(msg_destport(msg));
471 if (p_ptr->publ.connected)
472 abuf = port_build_self_abort_msg(p_ptr, err);
473 tipc_port_unlock(p_ptr);
475 tipc_net_route_msg(abuf);
478 /* send rejected message */
480 tipc_net_route_msg(rbuf);
484 int tipc_port_reject_sections(struct port *p_ptr, struct tipc_msg *hdr,
485 struct iovec const *msg_sect, u32 num_sect,
491 res = msg_build(hdr, msg_sect, num_sect, MAX_MSG_SIZE,
492 !p_ptr->user_port, &buf);
496 return tipc_reject_msg(buf, err);
499 static void port_timeout(unsigned long ref)
501 struct port *p_ptr = tipc_port_lock(ref);
502 struct sk_buff *buf = NULL;
507 if (!p_ptr->publ.connected) {
508 tipc_port_unlock(p_ptr);
512 /* Last probe answered ? */
513 if (p_ptr->probing_state == PROBING) {
514 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
516 buf = port_build_proto_msg(port_peerport(p_ptr),
517 port_peernode(p_ptr),
523 port_out_seqno(p_ptr),
525 port_incr_out_seqno(p_ptr);
526 p_ptr->probing_state = PROBING;
527 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
529 tipc_port_unlock(p_ptr);
530 tipc_net_route_msg(buf);
534 static void port_handle_node_down(unsigned long ref)
536 struct port *p_ptr = tipc_port_lock(ref);
537 struct sk_buff* buf = NULL;
541 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_NODE);
542 tipc_port_unlock(p_ptr);
543 tipc_net_route_msg(buf);
547 static struct sk_buff *port_build_self_abort_msg(struct port *p_ptr, u32 err)
549 u32 imp = msg_importance(&p_ptr->publ.phdr);
551 if (!p_ptr->publ.connected)
553 if (imp < TIPC_CRITICAL_IMPORTANCE)
555 return port_build_proto_msg(p_ptr->publ.ref,
557 port_peerport(p_ptr),
558 port_peernode(p_ptr),
562 p_ptr->last_in_seqno + 1,
567 static struct sk_buff *port_build_peer_abort_msg(struct port *p_ptr, u32 err)
569 u32 imp = msg_importance(&p_ptr->publ.phdr);
571 if (!p_ptr->publ.connected)
573 if (imp < TIPC_CRITICAL_IMPORTANCE)
575 return port_build_proto_msg(port_peerport(p_ptr),
576 port_peernode(p_ptr),
582 port_out_seqno(p_ptr),
586 void tipc_port_recv_proto_msg(struct sk_buff *buf)
588 struct tipc_msg *msg = buf_msg(buf);
589 struct port *p_ptr = tipc_port_lock(msg_destport(msg));
591 struct sk_buff *r_buf = NULL;
592 struct sk_buff *abort_buf = NULL;
594 msg_dbg(msg, "PORT<RECV<:");
597 err = TIPC_ERR_NO_PORT;
598 } else if (p_ptr->publ.connected) {
599 if (port_peernode(p_ptr) != msg_orignode(msg))
600 err = TIPC_ERR_NO_PORT;
601 if (port_peerport(p_ptr) != msg_origport(msg))
602 err = TIPC_ERR_NO_PORT;
603 if (!err && msg_routed(msg)) {
604 u32 seqno = msg_transp_seqno(msg);
605 u32 myno = ++p_ptr->last_in_seqno;
607 err = TIPC_ERR_NO_PORT;
608 abort_buf = port_build_self_abort_msg(p_ptr, err);
611 if (msg_type(msg) == CONN_ACK) {
612 int wakeup = tipc_port_congested(p_ptr) &&
613 p_ptr->publ.congested &&
615 p_ptr->acked += msg_msgcnt(msg);
616 if (tipc_port_congested(p_ptr))
618 p_ptr->publ.congested = 0;
621 p_ptr->wakeup(&p_ptr->publ);
624 } else if (p_ptr->publ.published) {
625 err = TIPC_ERR_NO_PORT;
628 r_buf = port_build_proto_msg(msg_origport(msg),
632 TIPC_HIGH_IMPORTANCE,
641 if (msg_type(msg) == CONN_PROBE) {
642 r_buf = port_build_proto_msg(msg_origport(msg),
649 port_out_seqno(p_ptr),
652 p_ptr->probing_state = CONFIRMED;
653 port_incr_out_seqno(p_ptr);
656 tipc_port_unlock(p_ptr);
657 tipc_net_route_msg(r_buf);
658 tipc_net_route_msg(abort_buf);
662 static void port_print(struct port *p_ptr, struct print_buf *buf, int full_id)
664 struct publication *publ;
667 tipc_printf(buf, "<%u.%u.%u:%u>:",
668 tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
669 tipc_node(tipc_own_addr), p_ptr->publ.ref);
671 tipc_printf(buf, "%-10u:", p_ptr->publ.ref);
673 if (p_ptr->publ.connected) {
674 u32 dport = port_peerport(p_ptr);
675 u32 destnode = port_peernode(p_ptr);
677 tipc_printf(buf, " connected to <%u.%u.%u:%u>",
678 tipc_zone(destnode), tipc_cluster(destnode),
679 tipc_node(destnode), dport);
680 if (p_ptr->publ.conn_type != 0)
681 tipc_printf(buf, " via {%u,%u}",
682 p_ptr->publ.conn_type,
683 p_ptr->publ.conn_instance);
685 else if (p_ptr->publ.published) {
686 tipc_printf(buf, " bound to");
687 list_for_each_entry(publ, &p_ptr->publications, pport_list) {
688 if (publ->lower == publ->upper)
689 tipc_printf(buf, " {%u,%u}", publ->type,
692 tipc_printf(buf, " {%u,%u,%u}", publ->type,
693 publ->lower, publ->upper);
696 tipc_printf(buf, "\n");
699 #define MAX_PORT_QUERY 32768
701 struct sk_buff *tipc_port_get_ports(void)
704 struct tlv_desc *rep_tlv;
709 buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_PORT_QUERY));
712 rep_tlv = (struct tlv_desc *)buf->data;
714 tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), MAX_PORT_QUERY);
715 spin_lock_bh(&tipc_port_list_lock);
716 list_for_each_entry(p_ptr, &ports, port_list) {
717 spin_lock_bh(p_ptr->publ.lock);
718 port_print(p_ptr, &pb, 0);
719 spin_unlock_bh(p_ptr->publ.lock);
721 spin_unlock_bh(&tipc_port_list_lock);
722 str_len = tipc_printbuf_validate(&pb);
724 skb_put(buf, TLV_SPACE(str_len));
725 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
732 #define MAX_PORT_STATS 2000
734 struct sk_buff *port_show_stats(const void *req_tlv_area, int req_tlv_space)
739 struct tlv_desc *rep_tlv;
743 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_PORT_REF))
744 return cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
746 ref = *(u32 *)TLV_DATA(req_tlv_area);
749 p_ptr = tipc_port_lock(ref);
751 return cfg_reply_error_string("port not found");
753 buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_PORT_STATS));
755 tipc_port_unlock(p_ptr);
758 rep_tlv = (struct tlv_desc *)buf->data;
760 tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), MAX_PORT_STATS);
761 port_print(p_ptr, &pb, 1);
762 /* NEED TO FILL IN ADDITIONAL PORT STATISTICS HERE */
763 tipc_port_unlock(p_ptr);
764 str_len = tipc_printbuf_validate(&pb);
766 skb_put(buf, TLV_SPACE(str_len));
767 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
774 void tipc_port_reinit(void)
777 struct tipc_msg *msg;
779 spin_lock_bh(&tipc_port_list_lock);
780 list_for_each_entry(p_ptr, &ports, port_list) {
781 msg = &p_ptr->publ.phdr;
782 if (msg_orignode(msg) == tipc_own_addr)
784 msg_set_prevnode(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 tipc_port_unlock(p_ptr);
847 if (unlikely(!connected)) {
848 if (tipc_connect2port(dref, &orig))
850 } else if ((msg_origport(msg) != peer_port) ||
851 (msg_orignode(msg) != peer_node))
853 if (unlikely(++p_ptr->publ.conn_unacked >=
854 TIPC_FLOW_CONTROL_WIN))
855 tipc_acknowledge(dref,
856 p_ptr->publ.conn_unacked);
857 skb_pull(buf, msg_hdr_sz(msg));
858 cb(usr_handle, dref, &buf, msg_data(msg),
862 case TIPC_DIRECT_MSG:{
863 tipc_msg_event cb = up_ptr->msg_cb;
865 tipc_port_unlock(p_ptr);
866 if (unlikely(!cb || connected))
868 skb_pull(buf, msg_hdr_sz(msg));
869 cb(usr_handle, dref, &buf, msg_data(msg),
870 msg_data_sz(msg), msg_importance(msg),
875 case TIPC_NAMED_MSG:{
876 tipc_named_msg_event cb = up_ptr->named_msg_cb;
878 tipc_port_unlock(p_ptr);
879 if (unlikely(!cb || connected || !published))
881 dseq.type = msg_nametype(msg);
882 dseq.lower = msg_nameinst(msg);
883 dseq.upper = (message_type == TIPC_NAMED_MSG)
884 ? dseq.lower : msg_nameupper(msg);
885 skb_pull(buf, msg_hdr_sz(msg));
886 cb(usr_handle, dref, &buf, msg_data(msg),
887 msg_data_sz(msg), msg_importance(msg),
897 switch (message_type) {
900 tipc_conn_shutdown_event cb =
902 u32 peer_port = port_peerport(p_ptr);
903 u32 peer_node = port_peernode(p_ptr);
905 tipc_port_unlock(p_ptr);
906 if (!cb || !connected)
908 if ((msg_origport(msg) != peer_port) ||
909 (msg_orignode(msg) != peer_node))
911 tipc_disconnect(dref);
912 skb_pull(buf, msg_hdr_sz(msg));
913 cb(usr_handle, dref, &buf, msg_data(msg),
914 msg_data_sz(msg), msg_errcode(msg));
917 case TIPC_DIRECT_MSG:{
918 tipc_msg_err_event cb = up_ptr->err_cb;
920 tipc_port_unlock(p_ptr);
921 if (!cb || connected)
923 skb_pull(buf, msg_hdr_sz(msg));
924 cb(usr_handle, dref, &buf, msg_data(msg),
925 msg_data_sz(msg), msg_errcode(msg), &orig);
929 case TIPC_NAMED_MSG:{
930 tipc_named_msg_err_event cb =
931 up_ptr->named_err_cb;
933 tipc_port_unlock(p_ptr);
934 if (!cb || connected)
936 dseq.type = msg_nametype(msg);
937 dseq.lower = msg_nameinst(msg);
938 dseq.upper = (message_type == TIPC_NAMED_MSG)
939 ? dseq.lower : msg_nameupper(msg);
940 skb_pull(buf, msg_hdr_sz(msg));
941 cb(usr_handle, dref, &buf, msg_data(msg),
942 msg_data_sz(msg), msg_errcode(msg), &dseq);
951 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
957 * port_dispatcher(): Dispatcher for messages destinated
958 * to the tipc_port interface. Called with port locked.
961 static u32 port_dispatcher(struct tipc_port *dummy, struct sk_buff *buf)
964 spin_lock_bh(&queue_lock);
965 if (msg_queue_head) {
966 msg_queue_tail->next = buf;
967 msg_queue_tail = buf;
969 msg_queue_tail = msg_queue_head = buf;
970 tipc_k_signal((Handler)port_dispatcher_sigh, 0);
972 spin_unlock_bh(&queue_lock);
977 * Wake up port after congestion: Called with port locked,
981 static void port_wakeup_sh(unsigned long ref)
984 struct user_port *up_ptr;
985 tipc_continue_event cb = NULL;
988 p_ptr = tipc_port_lock(ref);
990 up_ptr = p_ptr->user_port;
992 cb = up_ptr->continue_event_cb;
993 uh = up_ptr->usr_handle;
995 tipc_port_unlock(p_ptr);
1002 static void port_wakeup(struct tipc_port *p_ptr)
1004 tipc_k_signal((Handler)port_wakeup_sh, p_ptr->ref);
1007 void tipc_acknowledge(u32 ref, u32 ack)
1010 struct sk_buff *buf = NULL;
1012 p_ptr = tipc_port_lock(ref);
1015 if (p_ptr->publ.connected) {
1016 p_ptr->publ.conn_unacked -= ack;
1017 buf = port_build_proto_msg(port_peerport(p_ptr),
1018 port_peernode(p_ptr),
1024 port_out_seqno(p_ptr),
1027 tipc_port_unlock(p_ptr);
1028 tipc_net_route_msg(buf);
1032 * tipc_createport(): user level call. Will add port to
1033 * registry if non-zero user_ref.
1036 int tipc_createport(u32 user_ref,
1038 unsigned int importance,
1039 tipc_msg_err_event error_cb,
1040 tipc_named_msg_err_event named_error_cb,
1041 tipc_conn_shutdown_event conn_error_cb,
1042 tipc_msg_event msg_cb,
1043 tipc_named_msg_event named_msg_cb,
1044 tipc_conn_msg_event conn_msg_cb,
1045 tipc_continue_event continue_event_cb,/* May be zero */
1048 struct user_port *up_ptr;
1050 struct tipc_port *tp_ptr;
1053 up_ptr = kmalloc(sizeof(*up_ptr), GFP_ATOMIC);
1055 warn("Port creation failed, no memory\n");
1058 ref = tipc_createport_raw(NULL, port_dispatcher, port_wakeup,
1059 importance, &tp_ptr);
1064 p_ptr = (struct port *)tp_ptr;
1066 p_ptr->user_port = up_ptr;
1067 up_ptr->user_ref = user_ref;
1068 up_ptr->usr_handle = usr_handle;
1069 up_ptr->ref = p_ptr->publ.ref;
1070 up_ptr->err_cb = error_cb;
1071 up_ptr->named_err_cb = named_error_cb;
1072 up_ptr->conn_err_cb = conn_error_cb;
1073 up_ptr->msg_cb = msg_cb;
1074 up_ptr->named_msg_cb = named_msg_cb;
1075 up_ptr->conn_msg_cb = conn_msg_cb;
1076 up_ptr->continue_event_cb = continue_event_cb;
1077 INIT_LIST_HEAD(&up_ptr->uport_list);
1078 tipc_reg_add_port(up_ptr);
1079 *portref = p_ptr->publ.ref;
1080 dbg(" tipc_createport: %x with ref %u\n", p_ptr, p_ptr->publ.ref);
1081 tipc_port_unlock(p_ptr);
1085 int tipc_ownidentity(u32 ref, struct tipc_portid *id)
1088 id->node = tipc_own_addr;
1092 int tipc_portimportance(u32 ref, unsigned int *importance)
1096 p_ptr = tipc_port_lock(ref);
1099 *importance = (unsigned int)msg_importance(&p_ptr->publ.phdr);
1100 tipc_port_unlock(p_ptr);
1104 int tipc_set_portimportance(u32 ref, unsigned int imp)
1108 if (imp > TIPC_CRITICAL_IMPORTANCE)
1111 p_ptr = tipc_port_lock(ref);
1114 msg_set_importance(&p_ptr->publ.phdr, (u32)imp);
1115 tipc_port_unlock(p_ptr);
1120 int tipc_publish(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
1123 struct publication *publ;
1127 p_ptr = tipc_port_lock(ref);
1131 dbg("tipc_publ %u, p_ptr = %x, conn = %x, scope = %x, "
1132 "lower = %u, upper = %u\n",
1133 ref, p_ptr, p_ptr->publ.connected, scope, seq->lower, seq->upper);
1134 if (p_ptr->publ.connected)
1136 if (seq->lower > seq->upper)
1138 if ((scope < TIPC_ZONE_SCOPE) || (scope > TIPC_NODE_SCOPE))
1140 key = ref + p_ptr->pub_count + 1;
1145 publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
1146 scope, p_ptr->publ.ref, key);
1148 list_add(&publ->pport_list, &p_ptr->publications);
1150 p_ptr->publ.published = 1;
1154 tipc_port_unlock(p_ptr);
1158 int tipc_withdraw(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
1161 struct publication *publ;
1162 struct publication *tpubl;
1165 p_ptr = tipc_port_lock(ref);
1169 list_for_each_entry_safe(publ, tpubl,
1170 &p_ptr->publications, pport_list) {
1171 tipc_nametbl_withdraw(publ->type, publ->lower,
1172 publ->ref, publ->key);
1176 list_for_each_entry_safe(publ, tpubl,
1177 &p_ptr->publications, pport_list) {
1178 if (publ->scope != scope)
1180 if (publ->type != seq->type)
1182 if (publ->lower != seq->lower)
1184 if (publ->upper != seq->upper)
1186 tipc_nametbl_withdraw(publ->type, publ->lower,
1187 publ->ref, publ->key);
1192 if (list_empty(&p_ptr->publications))
1193 p_ptr->publ.published = 0;
1194 tipc_port_unlock(p_ptr);
1198 int tipc_connect2port(u32 ref, struct tipc_portid const *peer)
1201 struct tipc_msg *msg;
1204 p_ptr = tipc_port_lock(ref);
1207 if (p_ptr->publ.published || p_ptr->publ.connected)
1212 msg = &p_ptr->publ.phdr;
1213 msg_set_destnode(msg, peer->node);
1214 msg_set_destport(msg, peer->ref);
1215 msg_set_orignode(msg, tipc_own_addr);
1216 msg_set_origport(msg, p_ptr->publ.ref);
1217 msg_set_transp_seqno(msg, 42);
1218 msg_set_type(msg, TIPC_CONN_MSG);
1219 if (!may_route(peer->node))
1220 msg_set_hdr_sz(msg, SHORT_H_SIZE);
1222 msg_set_hdr_sz(msg, LONG_H_SIZE);
1224 p_ptr->probing_interval = PROBING_INTERVAL;
1225 p_ptr->probing_state = CONFIRMED;
1226 p_ptr->publ.connected = 1;
1227 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
1229 tipc_nodesub_subscribe(&p_ptr->subscription,peer->node,
1230 (void *)(unsigned long)ref,
1231 (net_ev_handler)port_handle_node_down);
1234 tipc_port_unlock(p_ptr);
1235 p_ptr->publ.max_pkt = tipc_link_get_max_pkt(peer->node, ref);
1240 * tipc_disconnect_port - disconnect port from peer
1242 * Port must be locked.
1245 int tipc_disconnect_port(struct tipc_port *tp_ptr)
1249 if (tp_ptr->connected) {
1250 tp_ptr->connected = 0;
1251 /* let timer expire on it's own to avoid deadlock! */
1252 tipc_nodesub_unsubscribe(
1253 &((struct port *)tp_ptr)->subscription);
1262 * tipc_disconnect(): Disconnect port form peer.
1263 * This is a node local operation.
1266 int tipc_disconnect(u32 ref)
1271 p_ptr = tipc_port_lock(ref);
1274 res = tipc_disconnect_port((struct tipc_port *)p_ptr);
1275 tipc_port_unlock(p_ptr);
1280 * tipc_shutdown(): Send a SHUTDOWN msg to peer and disconnect
1282 int tipc_shutdown(u32 ref)
1285 struct sk_buff *buf = NULL;
1287 p_ptr = tipc_port_lock(ref);
1291 if (p_ptr->publ.connected) {
1292 u32 imp = msg_importance(&p_ptr->publ.phdr);
1293 if (imp < TIPC_CRITICAL_IMPORTANCE)
1295 buf = port_build_proto_msg(port_peerport(p_ptr),
1296 port_peernode(p_ptr),
1302 port_out_seqno(p_ptr),
1305 tipc_port_unlock(p_ptr);
1306 tipc_net_route_msg(buf);
1307 return tipc_disconnect(ref);
1310 int tipc_isconnected(u32 ref, int *isconnected)
1314 p_ptr = tipc_port_lock(ref);
1317 *isconnected = p_ptr->publ.connected;
1318 tipc_port_unlock(p_ptr);
1322 int tipc_peer(u32 ref, struct tipc_portid *peer)
1327 p_ptr = tipc_port_lock(ref);
1330 if (p_ptr->publ.connected) {
1331 peer->ref = port_peerport(p_ptr);
1332 peer->node = port_peernode(p_ptr);
1336 tipc_port_unlock(p_ptr);
1340 int tipc_ref_valid(u32 ref)
1342 /* Works irrespective of type */
1343 return !!tipc_ref_deref(ref);
1348 * tipc_port_recv_sections(): Concatenate and deliver sectioned
1349 * message for this node.
1352 int tipc_port_recv_sections(struct port *sender, unsigned int num_sect,
1353 struct iovec const *msg_sect)
1355 struct sk_buff *buf;
1358 res = msg_build(&sender->publ.phdr, msg_sect, num_sect,
1359 MAX_MSG_SIZE, !sender->user_port, &buf);
1361 tipc_port_recv_msg(buf);
1366 * tipc_send - send message sections on connection
1369 int tipc_send(u32 ref, unsigned int num_sect, struct iovec const *msg_sect)
1375 p_ptr = tipc_port_deref(ref);
1376 if (!p_ptr || !p_ptr->publ.connected)
1379 p_ptr->publ.congested = 1;
1380 if (!tipc_port_congested(p_ptr)) {
1381 destnode = port_peernode(p_ptr);
1382 if (likely(destnode != tipc_own_addr))
1383 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
1386 res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
1388 if (likely(res != -ELINKCONG)) {
1389 port_incr_out_seqno(p_ptr);
1390 p_ptr->publ.congested = 0;
1395 if (port_unreliable(p_ptr)) {
1396 p_ptr->publ.congested = 0;
1397 /* Just calculate msg length and return */
1398 return msg_calc_data_size(msg_sect, num_sect);
1404 * tipc_send_buf - send message buffer on connection
1407 int tipc_send_buf(u32 ref, struct sk_buff *buf, unsigned int dsz)
1410 struct tipc_msg *msg;
1416 p_ptr = tipc_port_deref(ref);
1417 if (!p_ptr || !p_ptr->publ.connected)
1420 msg = &p_ptr->publ.phdr;
1421 hsz = msg_hdr_sz(msg);
1423 msg_set_size(msg, sz);
1424 if (skb_cow(buf, hsz))
1428 skb_copy_to_linear_data(buf, msg, hsz);
1429 destnode = msg_destnode(msg);
1430 p_ptr->publ.congested = 1;
1431 if (!tipc_port_congested(p_ptr)) {
1432 if (likely(destnode != tipc_own_addr))
1433 res = tipc_send_buf_fast(buf, destnode);
1435 tipc_port_recv_msg(buf);
1438 if (likely(res != -ELINKCONG)) {
1439 port_incr_out_seqno(p_ptr);
1441 p_ptr->publ.congested = 0;
1445 if (port_unreliable(p_ptr)) {
1446 p_ptr->publ.congested = 0;
1453 * tipc_forward2name - forward message sections to port name
1456 int tipc_forward2name(u32 ref,
1457 struct tipc_name const *name,
1460 struct iovec const *msg_sect,
1461 struct tipc_portid const *orig,
1462 unsigned int importance)
1465 struct tipc_msg *msg;
1466 u32 destnode = domain;
1470 p_ptr = tipc_port_deref(ref);
1471 if (!p_ptr || p_ptr->publ.connected)
1474 msg = &p_ptr->publ.phdr;
1475 msg_set_type(msg, TIPC_NAMED_MSG);
1476 msg_set_orignode(msg, orig->node);
1477 msg_set_origport(msg, orig->ref);
1478 msg_set_hdr_sz(msg, LONG_H_SIZE);
1479 msg_set_nametype(msg, name->type);
1480 msg_set_nameinst(msg, name->instance);
1481 msg_set_lookup_scope(msg, addr_scope(domain));
1482 if (importance <= TIPC_CRITICAL_IMPORTANCE)
1483 msg_set_importance(msg,importance);
1484 destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
1485 msg_set_destnode(msg, destnode);
1486 msg_set_destport(msg, destport);
1488 if (likely(destport || destnode)) {
1490 if (likely(destnode == tipc_own_addr))
1491 return tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
1492 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
1494 if (likely(res != -ELINKCONG))
1496 if (port_unreliable(p_ptr)) {
1497 /* Just calculate msg length and return */
1498 return msg_calc_data_size(msg_sect, num_sect);
1502 return tipc_port_reject_sections(p_ptr, msg, msg_sect, num_sect,
1507 * tipc_send2name - send message sections to port name
1510 int tipc_send2name(u32 ref,
1511 struct tipc_name const *name,
1512 unsigned int domain,
1513 unsigned int num_sect,
1514 struct iovec const *msg_sect)
1516 struct tipc_portid orig;
1519 orig.node = tipc_own_addr;
1520 return tipc_forward2name(ref, name, domain, num_sect, msg_sect, &orig,
1521 TIPC_PORT_IMPORTANCE);
1525 * tipc_forward_buf2name - forward message buffer to port name
1528 int tipc_forward_buf2name(u32 ref,
1529 struct tipc_name const *name,
1531 struct sk_buff *buf,
1533 struct tipc_portid const *orig,
1534 unsigned int importance)
1537 struct tipc_msg *msg;
1538 u32 destnode = domain;
1542 p_ptr = (struct port *)tipc_ref_deref(ref);
1543 if (!p_ptr || p_ptr->publ.connected)
1546 msg = &p_ptr->publ.phdr;
1547 if (importance <= TIPC_CRITICAL_IMPORTANCE)
1548 msg_set_importance(msg, importance);
1549 msg_set_type(msg, TIPC_NAMED_MSG);
1550 msg_set_orignode(msg, orig->node);
1551 msg_set_origport(msg, orig->ref);
1552 msg_set_nametype(msg, name->type);
1553 msg_set_nameinst(msg, name->instance);
1554 msg_set_lookup_scope(msg, addr_scope(domain));
1555 msg_set_hdr_sz(msg, LONG_H_SIZE);
1556 msg_set_size(msg, LONG_H_SIZE + dsz);
1557 destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
1558 msg_set_destnode(msg, destnode);
1559 msg_set_destport(msg, destport);
1560 msg_dbg(msg, "forw2name ==> ");
1561 if (skb_cow(buf, LONG_H_SIZE))
1563 skb_push(buf, LONG_H_SIZE);
1564 skb_copy_to_linear_data(buf, msg, LONG_H_SIZE);
1565 msg_dbg(buf_msg(buf),"PREP:");
1566 if (likely(destport || destnode)) {
1568 if (destnode == tipc_own_addr)
1569 return tipc_port_recv_msg(buf);
1570 res = tipc_send_buf_fast(buf, destnode);
1571 if (likely(res != -ELINKCONG))
1573 if (port_unreliable(p_ptr))
1577 return tipc_reject_msg(buf, TIPC_ERR_NO_NAME);
1581 * tipc_send_buf2name - send message buffer to port name
1584 int tipc_send_buf2name(u32 ref,
1585 struct tipc_name const *dest,
1587 struct sk_buff *buf,
1590 struct tipc_portid orig;
1593 orig.node = tipc_own_addr;
1594 return tipc_forward_buf2name(ref, dest, domain, buf, dsz, &orig,
1595 TIPC_PORT_IMPORTANCE);
1599 * tipc_forward2port - forward message sections to port identity
1602 int tipc_forward2port(u32 ref,
1603 struct tipc_portid const *dest,
1604 unsigned int num_sect,
1605 struct iovec const *msg_sect,
1606 struct tipc_portid const *orig,
1607 unsigned int importance)
1610 struct tipc_msg *msg;
1613 p_ptr = tipc_port_deref(ref);
1614 if (!p_ptr || p_ptr->publ.connected)
1617 msg = &p_ptr->publ.phdr;
1618 msg_set_type(msg, TIPC_DIRECT_MSG);
1619 msg_set_orignode(msg, orig->node);
1620 msg_set_origport(msg, orig->ref);
1621 msg_set_destnode(msg, dest->node);
1622 msg_set_destport(msg, dest->ref);
1623 msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
1624 if (importance <= TIPC_CRITICAL_IMPORTANCE)
1625 msg_set_importance(msg, importance);
1627 if (dest->node == tipc_own_addr)
1628 return tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
1629 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect, dest->node);
1630 if (likely(res != -ELINKCONG))
1632 if (port_unreliable(p_ptr)) {
1633 /* Just calculate msg length and return */
1634 return msg_calc_data_size(msg_sect, num_sect);
1640 * tipc_send2port - send message sections to port identity
1643 int tipc_send2port(u32 ref,
1644 struct tipc_portid const *dest,
1645 unsigned int num_sect,
1646 struct iovec const *msg_sect)
1648 struct tipc_portid orig;
1651 orig.node = tipc_own_addr;
1652 return tipc_forward2port(ref, dest, num_sect, msg_sect, &orig,
1653 TIPC_PORT_IMPORTANCE);
1657 * tipc_forward_buf2port - forward message buffer to port identity
1659 int tipc_forward_buf2port(u32 ref,
1660 struct tipc_portid const *dest,
1661 struct sk_buff *buf,
1663 struct tipc_portid const *orig,
1664 unsigned int importance)
1667 struct tipc_msg *msg;
1670 p_ptr = (struct port *)tipc_ref_deref(ref);
1671 if (!p_ptr || p_ptr->publ.connected)
1674 msg = &p_ptr->publ.phdr;
1675 msg_set_type(msg, TIPC_DIRECT_MSG);
1676 msg_set_orignode(msg, orig->node);
1677 msg_set_origport(msg, orig->ref);
1678 msg_set_destnode(msg, dest->node);
1679 msg_set_destport(msg, dest->ref);
1680 msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
1681 if (importance <= TIPC_CRITICAL_IMPORTANCE)
1682 msg_set_importance(msg, importance);
1683 msg_set_size(msg, DIR_MSG_H_SIZE + dsz);
1684 if (skb_cow(buf, DIR_MSG_H_SIZE))
1687 skb_push(buf, DIR_MSG_H_SIZE);
1688 skb_copy_to_linear_data(buf, msg, DIR_MSG_H_SIZE);
1689 msg_dbg(msg, "buf2port: ");
1691 if (dest->node == tipc_own_addr)
1692 return tipc_port_recv_msg(buf);
1693 res = tipc_send_buf_fast(buf, dest->node);
1694 if (likely(res != -ELINKCONG))
1696 if (port_unreliable(p_ptr))
1702 * tipc_send_buf2port - send message buffer to port identity
1705 int tipc_send_buf2port(u32 ref,
1706 struct tipc_portid const *dest,
1707 struct sk_buff *buf,
1710 struct tipc_portid orig;
1713 orig.node = tipc_own_addr;
1714 return tipc_forward_buf2port(ref, dest, buf, dsz, &orig,
1715 TIPC_PORT_IMPORTANCE);