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;
173 /* Create destination port list, if one wasn't supplied */
176 tipc_nametbl_mc_translate(msg_nametype(msg),
184 /* Deliver a copy of message to each destination port */
186 if (dp->count != 0) {
187 if (dp->count == 1) {
188 msg_set_destport(msg, dp->ports[0]);
189 tipc_port_recv_msg(buf);
190 tipc_port_list_free(dp);
193 for (; cnt < dp->count; cnt++) {
194 int index = cnt % PLSIZE;
195 struct sk_buff *b = skb_clone(buf, GFP_ATOMIC);
198 warn("Unable to deliver multicast message(s)\n");
199 msg_dbg(msg, "LOST:");
202 if ((index == 0) && (cnt != 0)) {
205 msg_set_destport(buf_msg(b),item->ports[index]);
206 tipc_port_recv_msg(b);
211 tipc_port_list_free(dp);
215 * tipc_createport_raw - create a native TIPC port
217 * Returns local port reference
220 u32 tipc_createport_raw(void *usr_handle,
221 u32 (*dispatcher)(struct tipc_port *, struct sk_buff *),
222 void (*wakeup)(struct tipc_port *),
223 const u32 importance)
226 struct tipc_msg *msg;
229 p_ptr = kmalloc(sizeof(*p_ptr), GFP_ATOMIC);
231 warn("Port creation failed, no memory\n");
234 memset(p_ptr, 0, sizeof(*p_ptr));
235 ref = tipc_ref_acquire(p_ptr, &p_ptr->publ.lock);
237 warn("Port creation failed, reference table exhausted\n");
243 p_ptr->publ.ref = ref;
244 msg = &p_ptr->publ.phdr;
245 msg_init(msg, DATA_LOW, TIPC_NAMED_MSG, TIPC_OK, LONG_H_SIZE, 0);
246 msg_set_orignode(msg, tipc_own_addr);
247 msg_set_prevnode(msg, tipc_own_addr);
248 msg_set_origport(msg, ref);
249 msg_set_importance(msg,importance);
250 p_ptr->last_in_seqno = 41;
252 p_ptr->publ.usr_handle = usr_handle;
253 INIT_LIST_HEAD(&p_ptr->wait_list);
254 INIT_LIST_HEAD(&p_ptr->subscription.nodesub_list);
255 p_ptr->congested_link = NULL;
256 p_ptr->max_pkt = MAX_PKT_DEFAULT;
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 tipc_port_unlock(p_ptr);
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 spin_unlock_bh(p_ptr->publ.lock);
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 spin_unlock_bh(p_ptr->publ.lock);
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, err, LONG_H_SIZE, destnode);
407 msg_set_destport(msg, destport);
408 msg_set_origport(msg, origport);
409 msg_set_destnode(msg, destnode);
410 msg_set_orignode(msg, orignode);
411 msg_set_transp_seqno(msg, seqno);
412 msg_set_msgcnt(msg, ack);
413 msg_dbg(msg, "PORT>SEND>:");
418 int tipc_set_msg_option(struct tipc_port *tp_ptr, const char *opt, const u32 sz)
420 msg_expand(&tp_ptr->phdr, msg_destnode(&tp_ptr->phdr));
421 msg_set_options(&tp_ptr->phdr, opt, sz);
425 int tipc_reject_msg(struct sk_buff *buf, u32 err)
427 struct tipc_msg *msg = buf_msg(buf);
428 struct sk_buff *rbuf;
429 struct tipc_msg *rmsg;
431 u32 imp = msg_importance(msg);
432 u32 data_sz = msg_data_sz(msg);
434 if (data_sz > MAX_REJECT_SIZE)
435 data_sz = MAX_REJECT_SIZE;
436 if (msg_connected(msg) && (imp < TIPC_CRITICAL_IMPORTANCE))
438 msg_dbg(msg, "port->rej: ");
440 /* discard rejected message if it shouldn't be returned to sender */
441 if (msg_errcode(msg) || msg_dest_droppable(msg)) {
446 /* construct rejected message */
448 hdr_sz = MCAST_H_SIZE;
450 hdr_sz = LONG_H_SIZE;
451 rbuf = buf_acquire(data_sz + hdr_sz);
456 rmsg = buf_msg(rbuf);
457 msg_init(rmsg, imp, msg_type(msg), err, hdr_sz, msg_orignode(msg));
458 msg_set_destport(rmsg, msg_origport(msg));
459 msg_set_prevnode(rmsg, tipc_own_addr);
460 msg_set_origport(rmsg, msg_destport(msg));
462 msg_set_orignode(rmsg, tipc_own_addr);
464 msg_set_orignode(rmsg, msg_destnode(msg));
465 msg_set_size(rmsg, data_sz + hdr_sz);
466 msg_set_nametype(rmsg, msg_nametype(msg));
467 msg_set_nameinst(rmsg, msg_nameinst(msg));
468 memcpy(rbuf->data + hdr_sz, msg_data(msg), data_sz);
470 /* send self-abort message when rejecting on a connected port */
471 if (msg_connected(msg)) {
472 struct sk_buff *abuf = NULL;
473 struct port *p_ptr = tipc_port_lock(msg_destport(msg));
476 if (p_ptr->publ.connected)
477 abuf = port_build_self_abort_msg(p_ptr, err);
478 tipc_port_unlock(p_ptr);
480 tipc_net_route_msg(abuf);
483 /* send rejected message */
485 tipc_net_route_msg(rbuf);
489 int tipc_port_reject_sections(struct port *p_ptr, struct tipc_msg *hdr,
490 struct iovec const *msg_sect, u32 num_sect,
496 res = msg_build(hdr, msg_sect, num_sect, MAX_MSG_SIZE,
497 !p_ptr->user_port, &buf);
501 return tipc_reject_msg(buf, err);
504 static void port_timeout(unsigned long ref)
506 struct port *p_ptr = tipc_port_lock(ref);
507 struct sk_buff *buf = NULL;
509 if (!p_ptr || !p_ptr->publ.connected)
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),
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_orignode(msg, tipc_own_addr);
786 spin_unlock_bh(&tipc_port_list_lock);
791 * port_dispatcher_sigh(): Signal handler for messages destinated
792 * to the tipc_port interface.
795 static void port_dispatcher_sigh(void *dummy)
799 spin_lock_bh(&queue_lock);
800 buf = msg_queue_head;
801 msg_queue_head = NULL;
802 spin_unlock_bh(&queue_lock);
806 struct user_port *up_ptr;
807 struct tipc_portid orig;
808 struct tipc_name_seq dseq;
814 struct sk_buff *next = buf->next;
815 struct tipc_msg *msg = buf_msg(buf);
816 u32 dref = msg_destport(msg);
818 message_type = msg_type(msg);
819 if (message_type > TIPC_DIRECT_MSG)
820 goto reject; /* Unsupported message type */
822 p_ptr = tipc_port_lock(dref);
824 goto reject; /* Port deleted while msg in queue */
826 orig.ref = msg_origport(msg);
827 orig.node = msg_orignode(msg);
828 up_ptr = p_ptr->user_port;
829 usr_handle = up_ptr->usr_handle;
830 connected = p_ptr->publ.connected;
831 published = p_ptr->publ.published;
833 if (unlikely(msg_errcode(msg)))
836 switch (message_type) {
839 tipc_conn_msg_event cb = up_ptr->conn_msg_cb;
840 u32 peer_port = port_peerport(p_ptr);
841 u32 peer_node = port_peernode(p_ptr);
843 spin_unlock_bh(p_ptr->publ.lock);
844 if (unlikely(!connected)) {
845 if (unlikely(published))
847 tipc_connect2port(dref,&orig);
849 if (unlikely(msg_origport(msg) != peer_port))
851 if (unlikely(msg_orignode(msg) != peer_node))
855 if (unlikely(++p_ptr->publ.conn_unacked >=
856 TIPC_FLOW_CONTROL_WIN))
857 tipc_acknowledge(dref,
858 p_ptr->publ.conn_unacked);
859 skb_pull(buf, msg_hdr_sz(msg));
860 cb(usr_handle, dref, &buf, msg_data(msg),
864 case TIPC_DIRECT_MSG:{
865 tipc_msg_event cb = up_ptr->msg_cb;
867 spin_unlock_bh(p_ptr->publ.lock);
868 if (unlikely(connected))
872 skb_pull(buf, msg_hdr_sz(msg));
873 cb(usr_handle, dref, &buf, msg_data(msg),
874 msg_data_sz(msg), msg_importance(msg),
879 case TIPC_NAMED_MSG:{
880 tipc_named_msg_event cb = up_ptr->named_msg_cb;
882 spin_unlock_bh(p_ptr->publ.lock);
883 if (unlikely(connected))
887 if (unlikely(!published))
889 dseq.type = msg_nametype(msg);
890 dseq.lower = msg_nameinst(msg);
891 dseq.upper = (message_type == TIPC_NAMED_MSG)
892 ? dseq.lower : msg_nameupper(msg);
893 skb_pull(buf, msg_hdr_sz(msg));
894 cb(usr_handle, dref, &buf, msg_data(msg),
895 msg_data_sz(msg), msg_importance(msg),
905 switch (message_type) {
908 tipc_conn_shutdown_event cb =
910 u32 peer_port = port_peerport(p_ptr);
911 u32 peer_node = port_peernode(p_ptr);
913 spin_unlock_bh(p_ptr->publ.lock);
914 if (!connected || !cb)
916 if (msg_origport(msg) != peer_port)
918 if (msg_orignode(msg) != peer_node)
920 tipc_disconnect(dref);
921 skb_pull(buf, msg_hdr_sz(msg));
922 cb(usr_handle, dref, &buf, msg_data(msg),
923 msg_data_sz(msg), msg_errcode(msg));
926 case TIPC_DIRECT_MSG:{
927 tipc_msg_err_event cb = up_ptr->err_cb;
929 spin_unlock_bh(p_ptr->publ.lock);
930 if (connected || !cb)
932 skb_pull(buf, msg_hdr_sz(msg));
933 cb(usr_handle, dref, &buf, msg_data(msg),
934 msg_data_sz(msg), msg_errcode(msg), &orig);
938 case TIPC_NAMED_MSG:{
939 tipc_named_msg_err_event cb =
940 up_ptr->named_err_cb;
942 spin_unlock_bh(p_ptr->publ.lock);
943 if (connected || !cb)
945 dseq.type = msg_nametype(msg);
946 dseq.lower = msg_nameinst(msg);
947 dseq.upper = (message_type == TIPC_NAMED_MSG)
948 ? dseq.lower : msg_nameupper(msg);
949 skb_pull(buf, msg_hdr_sz(msg));
950 cb(usr_handle, dref, &buf, msg_data(msg),
951 msg_data_sz(msg), msg_errcode(msg), &dseq);
960 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
966 * port_dispatcher(): Dispatcher for messages destinated
967 * to the tipc_port interface. Called with port locked.
970 static u32 port_dispatcher(struct tipc_port *dummy, struct sk_buff *buf)
973 spin_lock_bh(&queue_lock);
974 if (msg_queue_head) {
975 msg_queue_tail->next = buf;
976 msg_queue_tail = buf;
978 msg_queue_tail = msg_queue_head = buf;
979 tipc_k_signal((Handler)port_dispatcher_sigh, 0);
981 spin_unlock_bh(&queue_lock);
986 * Wake up port after congestion: Called with port locked,
990 static void port_wakeup_sh(unsigned long ref)
993 struct user_port *up_ptr;
994 tipc_continue_event cb = NULL;
997 p_ptr = tipc_port_lock(ref);
999 up_ptr = p_ptr->user_port;
1001 cb = up_ptr->continue_event_cb;
1002 uh = up_ptr->usr_handle;
1004 tipc_port_unlock(p_ptr);
1011 static void port_wakeup(struct tipc_port *p_ptr)
1013 tipc_k_signal((Handler)port_wakeup_sh, p_ptr->ref);
1016 void tipc_acknowledge(u32 ref, u32 ack)
1019 struct sk_buff *buf = NULL;
1021 p_ptr = tipc_port_lock(ref);
1024 if (p_ptr->publ.connected) {
1025 p_ptr->publ.conn_unacked -= ack;
1026 buf = port_build_proto_msg(port_peerport(p_ptr),
1027 port_peernode(p_ptr),
1033 port_out_seqno(p_ptr),
1036 tipc_port_unlock(p_ptr);
1037 tipc_net_route_msg(buf);
1041 * tipc_createport(): user level call. Will add port to
1042 * registry if non-zero user_ref.
1045 int tipc_createport(u32 user_ref,
1047 unsigned int importance,
1048 tipc_msg_err_event error_cb,
1049 tipc_named_msg_err_event named_error_cb,
1050 tipc_conn_shutdown_event conn_error_cb,
1051 tipc_msg_event msg_cb,
1052 tipc_named_msg_event named_msg_cb,
1053 tipc_conn_msg_event conn_msg_cb,
1054 tipc_continue_event continue_event_cb,/* May be zero */
1057 struct user_port *up_ptr;
1061 up_ptr = (struct user_port *)kmalloc(sizeof(*up_ptr), GFP_ATOMIC);
1063 warn("Port creation failed, no memory\n");
1066 ref = tipc_createport_raw(NULL, port_dispatcher, port_wakeup, importance);
1067 p_ptr = tipc_port_lock(ref);
1073 p_ptr->user_port = up_ptr;
1074 up_ptr->user_ref = user_ref;
1075 up_ptr->usr_handle = usr_handle;
1076 up_ptr->ref = p_ptr->publ.ref;
1077 up_ptr->err_cb = error_cb;
1078 up_ptr->named_err_cb = named_error_cb;
1079 up_ptr->conn_err_cb = conn_error_cb;
1080 up_ptr->msg_cb = msg_cb;
1081 up_ptr->named_msg_cb = named_msg_cb;
1082 up_ptr->conn_msg_cb = conn_msg_cb;
1083 up_ptr->continue_event_cb = continue_event_cb;
1084 INIT_LIST_HEAD(&up_ptr->uport_list);
1085 tipc_reg_add_port(up_ptr);
1086 *portref = p_ptr->publ.ref;
1087 dbg(" tipc_createport: %x with ref %u\n", p_ptr, p_ptr->publ.ref);
1088 tipc_port_unlock(p_ptr);
1092 int tipc_ownidentity(u32 ref, struct tipc_portid *id)
1095 id->node = tipc_own_addr;
1099 int tipc_portimportance(u32 ref, unsigned int *importance)
1103 p_ptr = tipc_port_lock(ref);
1106 *importance = (unsigned int)msg_importance(&p_ptr->publ.phdr);
1107 spin_unlock_bh(p_ptr->publ.lock);
1111 int tipc_set_portimportance(u32 ref, unsigned int imp)
1115 if (imp > TIPC_CRITICAL_IMPORTANCE)
1118 p_ptr = tipc_port_lock(ref);
1121 msg_set_importance(&p_ptr->publ.phdr, (u32)imp);
1122 spin_unlock_bh(p_ptr->publ.lock);
1127 int tipc_publish(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
1130 struct publication *publ;
1134 p_ptr = tipc_port_lock(ref);
1135 dbg("tipc_publ %u, p_ptr = %x, conn = %x, scope = %x, "
1136 "lower = %u, upper = %u\n",
1137 ref, p_ptr, p_ptr->publ.connected, scope, seq->lower, seq->upper);
1140 if (p_ptr->publ.connected)
1142 if (seq->lower > seq->upper)
1144 if ((scope < TIPC_ZONE_SCOPE) || (scope > TIPC_NODE_SCOPE))
1146 key = ref + p_ptr->pub_count + 1;
1151 publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
1152 scope, p_ptr->publ.ref, key);
1154 list_add(&publ->pport_list, &p_ptr->publications);
1156 p_ptr->publ.published = 1;
1160 tipc_port_unlock(p_ptr);
1164 int tipc_withdraw(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
1167 struct publication *publ;
1168 struct publication *tpubl;
1171 p_ptr = tipc_port_lock(ref);
1175 list_for_each_entry_safe(publ, tpubl,
1176 &p_ptr->publications, pport_list) {
1177 tipc_nametbl_withdraw(publ->type, publ->lower,
1178 publ->ref, publ->key);
1182 list_for_each_entry_safe(publ, tpubl,
1183 &p_ptr->publications, pport_list) {
1184 if (publ->scope != scope)
1186 if (publ->type != seq->type)
1188 if (publ->lower != seq->lower)
1190 if (publ->upper != seq->upper)
1192 tipc_nametbl_withdraw(publ->type, publ->lower,
1193 publ->ref, publ->key);
1198 if (list_empty(&p_ptr->publications))
1199 p_ptr->publ.published = 0;
1200 tipc_port_unlock(p_ptr);
1204 int tipc_connect2port(u32 ref, struct tipc_portid const *peer)
1207 struct tipc_msg *msg;
1210 p_ptr = tipc_port_lock(ref);
1213 if (p_ptr->publ.published || p_ptr->publ.connected)
1218 msg = &p_ptr->publ.phdr;
1219 msg_set_destnode(msg, peer->node);
1220 msg_set_destport(msg, peer->ref);
1221 msg_set_orignode(msg, tipc_own_addr);
1222 msg_set_origport(msg, p_ptr->publ.ref);
1223 msg_set_transp_seqno(msg, 42);
1224 msg_set_type(msg, TIPC_CONN_MSG);
1225 if (!may_route(peer->node))
1226 msg_set_hdr_sz(msg, SHORT_H_SIZE);
1228 msg_set_hdr_sz(msg, LONG_H_SIZE);
1230 p_ptr->probing_interval = PROBING_INTERVAL;
1231 p_ptr->probing_state = CONFIRMED;
1232 p_ptr->publ.connected = 1;
1233 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
1235 tipc_nodesub_subscribe(&p_ptr->subscription,peer->node,
1236 (void *)(unsigned long)ref,
1237 (net_ev_handler)port_handle_node_down);
1240 tipc_port_unlock(p_ptr);
1241 p_ptr->max_pkt = tipc_link_get_max_pkt(peer->node, ref);
1246 * tipc_disconnect(): Disconnect port form peer.
1247 * This is a node local operation.
1250 int tipc_disconnect(u32 ref)
1253 int res = -ENOTCONN;
1255 p_ptr = tipc_port_lock(ref);
1258 if (p_ptr->publ.connected) {
1259 p_ptr->publ.connected = 0;
1260 /* let timer expire on it's own to avoid deadlock! */
1261 tipc_nodesub_unsubscribe(&p_ptr->subscription);
1264 tipc_port_unlock(p_ptr);
1269 * tipc_shutdown(): Send a SHUTDOWN msg to peer and disconnect
1271 int tipc_shutdown(u32 ref)
1274 struct sk_buff *buf = NULL;
1276 p_ptr = tipc_port_lock(ref);
1280 if (p_ptr->publ.connected) {
1281 u32 imp = msg_importance(&p_ptr->publ.phdr);
1282 if (imp < TIPC_CRITICAL_IMPORTANCE)
1284 buf = port_build_proto_msg(port_peerport(p_ptr),
1285 port_peernode(p_ptr),
1291 port_out_seqno(p_ptr),
1294 tipc_port_unlock(p_ptr);
1295 tipc_net_route_msg(buf);
1296 return tipc_disconnect(ref);
1299 int tipc_isconnected(u32 ref, int *isconnected)
1303 p_ptr = tipc_port_lock(ref);
1306 *isconnected = p_ptr->publ.connected;
1307 tipc_port_unlock(p_ptr);
1311 int tipc_peer(u32 ref, struct tipc_portid *peer)
1316 p_ptr = tipc_port_lock(ref);
1319 if (p_ptr->publ.connected) {
1320 peer->ref = port_peerport(p_ptr);
1321 peer->node = port_peernode(p_ptr);
1325 tipc_port_unlock(p_ptr);
1329 int tipc_ref_valid(u32 ref)
1331 /* Works irrespective of type */
1332 return !!tipc_ref_deref(ref);
1337 * tipc_port_recv_sections(): Concatenate and deliver sectioned
1338 * message for this node.
1341 int tipc_port_recv_sections(struct port *sender, unsigned int num_sect,
1342 struct iovec const *msg_sect)
1344 struct sk_buff *buf;
1347 res = msg_build(&sender->publ.phdr, msg_sect, num_sect,
1348 MAX_MSG_SIZE, !sender->user_port, &buf);
1350 tipc_port_recv_msg(buf);
1355 * tipc_send - send message sections on connection
1358 int tipc_send(u32 ref, unsigned int num_sect, struct iovec const *msg_sect)
1364 p_ptr = tipc_port_deref(ref);
1365 if (!p_ptr || !p_ptr->publ.connected)
1368 p_ptr->publ.congested = 1;
1369 if (!tipc_port_congested(p_ptr)) {
1370 destnode = port_peernode(p_ptr);
1371 if (likely(destnode != tipc_own_addr))
1372 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
1375 res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
1377 if (likely(res != -ELINKCONG)) {
1378 port_incr_out_seqno(p_ptr);
1379 p_ptr->publ.congested = 0;
1384 if (port_unreliable(p_ptr)) {
1385 p_ptr->publ.congested = 0;
1386 /* Just calculate msg length and return */
1387 return msg_calc_data_size(msg_sect, num_sect);
1393 * tipc_send_buf - send message buffer on connection
1396 int tipc_send_buf(u32 ref, struct sk_buff *buf, unsigned int dsz)
1399 struct tipc_msg *msg;
1405 p_ptr = tipc_port_deref(ref);
1406 if (!p_ptr || !p_ptr->publ.connected)
1409 msg = &p_ptr->publ.phdr;
1410 hsz = msg_hdr_sz(msg);
1412 msg_set_size(msg, sz);
1413 if (skb_cow(buf, hsz))
1417 memcpy(buf->data, (unchar *)msg, hsz);
1418 destnode = msg_destnode(msg);
1419 p_ptr->publ.congested = 1;
1420 if (!tipc_port_congested(p_ptr)) {
1421 if (likely(destnode != tipc_own_addr))
1422 res = tipc_send_buf_fast(buf, destnode);
1424 tipc_port_recv_msg(buf);
1427 if (likely(res != -ELINKCONG)) {
1428 port_incr_out_seqno(p_ptr);
1430 p_ptr->publ.congested = 0;
1434 if (port_unreliable(p_ptr)) {
1435 p_ptr->publ.congested = 0;
1442 * tipc_forward2name - forward message sections to port name
1445 int tipc_forward2name(u32 ref,
1446 struct tipc_name const *name,
1449 struct iovec const *msg_sect,
1450 struct tipc_portid const *orig,
1451 unsigned int importance)
1454 struct tipc_msg *msg;
1455 u32 destnode = domain;
1459 p_ptr = tipc_port_deref(ref);
1460 if (!p_ptr || p_ptr->publ.connected)
1463 msg = &p_ptr->publ.phdr;
1464 msg_set_type(msg, TIPC_NAMED_MSG);
1465 msg_set_orignode(msg, orig->node);
1466 msg_set_origport(msg, orig->ref);
1467 msg_set_hdr_sz(msg, LONG_H_SIZE);
1468 msg_set_nametype(msg, name->type);
1469 msg_set_nameinst(msg, name->instance);
1470 msg_set_lookup_scope(msg, addr_scope(domain));
1471 if (importance <= TIPC_CRITICAL_IMPORTANCE)
1472 msg_set_importance(msg,importance);
1473 destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
1474 msg_set_destnode(msg, destnode);
1475 msg_set_destport(msg, destport);
1477 if (likely(destport || destnode)) {
1479 if (likely(destnode == tipc_own_addr))
1480 return tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
1481 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
1483 if (likely(res != -ELINKCONG))
1485 if (port_unreliable(p_ptr)) {
1486 /* Just calculate msg length and return */
1487 return msg_calc_data_size(msg_sect, num_sect);
1491 return tipc_port_reject_sections(p_ptr, msg, msg_sect, num_sect,
1496 * tipc_send2name - send message sections to port name
1499 int tipc_send2name(u32 ref,
1500 struct tipc_name const *name,
1501 unsigned int domain,
1502 unsigned int num_sect,
1503 struct iovec const *msg_sect)
1505 struct tipc_portid orig;
1508 orig.node = tipc_own_addr;
1509 return tipc_forward2name(ref, name, domain, num_sect, msg_sect, &orig,
1510 TIPC_PORT_IMPORTANCE);
1514 * tipc_forward_buf2name - forward message buffer to port name
1517 int tipc_forward_buf2name(u32 ref,
1518 struct tipc_name const *name,
1520 struct sk_buff *buf,
1522 struct tipc_portid const *orig,
1523 unsigned int importance)
1526 struct tipc_msg *msg;
1527 u32 destnode = domain;
1531 p_ptr = (struct port *)tipc_ref_deref(ref);
1532 if (!p_ptr || p_ptr->publ.connected)
1535 msg = &p_ptr->publ.phdr;
1536 if (importance <= TIPC_CRITICAL_IMPORTANCE)
1537 msg_set_importance(msg, importance);
1538 msg_set_type(msg, TIPC_NAMED_MSG);
1539 msg_set_orignode(msg, orig->node);
1540 msg_set_origport(msg, orig->ref);
1541 msg_set_nametype(msg, name->type);
1542 msg_set_nameinst(msg, name->instance);
1543 msg_set_lookup_scope(msg, addr_scope(domain));
1544 msg_set_hdr_sz(msg, LONG_H_SIZE);
1545 msg_set_size(msg, LONG_H_SIZE + dsz);
1546 destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
1547 msg_set_destnode(msg, destnode);
1548 msg_set_destport(msg, destport);
1549 msg_dbg(msg, "forw2name ==> ");
1550 if (skb_cow(buf, LONG_H_SIZE))
1552 skb_push(buf, LONG_H_SIZE);
1553 memcpy(buf->data, (unchar *)msg, LONG_H_SIZE);
1554 msg_dbg(buf_msg(buf),"PREP:");
1555 if (likely(destport || destnode)) {
1557 if (destnode == tipc_own_addr)
1558 return tipc_port_recv_msg(buf);
1559 res = tipc_send_buf_fast(buf, destnode);
1560 if (likely(res != -ELINKCONG))
1562 if (port_unreliable(p_ptr))
1566 return tipc_reject_msg(buf, TIPC_ERR_NO_NAME);
1570 * tipc_send_buf2name - send message buffer to port name
1573 int tipc_send_buf2name(u32 ref,
1574 struct tipc_name const *dest,
1576 struct sk_buff *buf,
1579 struct tipc_portid orig;
1582 orig.node = tipc_own_addr;
1583 return tipc_forward_buf2name(ref, dest, domain, buf, dsz, &orig,
1584 TIPC_PORT_IMPORTANCE);
1588 * tipc_forward2port - forward message sections to port identity
1591 int tipc_forward2port(u32 ref,
1592 struct tipc_portid const *dest,
1593 unsigned int num_sect,
1594 struct iovec const *msg_sect,
1595 struct tipc_portid const *orig,
1596 unsigned int importance)
1599 struct tipc_msg *msg;
1602 p_ptr = tipc_port_deref(ref);
1603 if (!p_ptr || p_ptr->publ.connected)
1606 msg = &p_ptr->publ.phdr;
1607 msg_set_type(msg, TIPC_DIRECT_MSG);
1608 msg_set_orignode(msg, orig->node);
1609 msg_set_origport(msg, orig->ref);
1610 msg_set_destnode(msg, dest->node);
1611 msg_set_destport(msg, dest->ref);
1612 msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
1613 if (importance <= TIPC_CRITICAL_IMPORTANCE)
1614 msg_set_importance(msg, importance);
1616 if (dest->node == tipc_own_addr)
1617 return tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
1618 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect, dest->node);
1619 if (likely(res != -ELINKCONG))
1621 if (port_unreliable(p_ptr)) {
1622 /* Just calculate msg length and return */
1623 return msg_calc_data_size(msg_sect, num_sect);
1629 * tipc_send2port - send message sections to port identity
1632 int tipc_send2port(u32 ref,
1633 struct tipc_portid const *dest,
1634 unsigned int num_sect,
1635 struct iovec const *msg_sect)
1637 struct tipc_portid orig;
1640 orig.node = tipc_own_addr;
1641 return tipc_forward2port(ref, dest, num_sect, msg_sect, &orig,
1642 TIPC_PORT_IMPORTANCE);
1646 * tipc_forward_buf2port - forward message buffer to port identity
1648 int tipc_forward_buf2port(u32 ref,
1649 struct tipc_portid const *dest,
1650 struct sk_buff *buf,
1652 struct tipc_portid const *orig,
1653 unsigned int importance)
1656 struct tipc_msg *msg;
1659 p_ptr = (struct port *)tipc_ref_deref(ref);
1660 if (!p_ptr || p_ptr->publ.connected)
1663 msg = &p_ptr->publ.phdr;
1664 msg_set_type(msg, TIPC_DIRECT_MSG);
1665 msg_set_orignode(msg, orig->node);
1666 msg_set_origport(msg, orig->ref);
1667 msg_set_destnode(msg, dest->node);
1668 msg_set_destport(msg, dest->ref);
1669 msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
1670 if (importance <= TIPC_CRITICAL_IMPORTANCE)
1671 msg_set_importance(msg, importance);
1672 msg_set_size(msg, DIR_MSG_H_SIZE + dsz);
1673 if (skb_cow(buf, DIR_MSG_H_SIZE))
1676 skb_push(buf, DIR_MSG_H_SIZE);
1677 memcpy(buf->data, (unchar *)msg, DIR_MSG_H_SIZE);
1678 msg_dbg(msg, "buf2port: ");
1680 if (dest->node == tipc_own_addr)
1681 return tipc_port_recv_msg(buf);
1682 res = tipc_send_buf_fast(buf, dest->node);
1683 if (likely(res != -ELINKCONG))
1685 if (port_unreliable(p_ptr))
1691 * tipc_send_buf2port - send message buffer to port identity
1694 int tipc_send_buf2port(u32 ref,
1695 struct tipc_portid const *dest,
1696 struct sk_buff *buf,
1699 struct tipc_portid orig;
1702 orig.node = tipc_own_addr;
1703 return tipc_forward_buf2port(ref, dest, buf, dsz, &orig,
1704 TIPC_PORT_IMPORTANCE);