KVM: SVM: Implement infrastructure for TSC_RATE_MSR
[linux/fpc-iii.git] / net / tipc / port.c
blob6ff78f9c7d65a83623e870aa16522c3ca6219c3c
1 /*
2 * net/tipc/port.c: TIPC port code
4 * Copyright (c) 1992-2007, Ericsson AB
5 * Copyright (c) 2004-2008, 2010-2011, Wind River Systems
6 * All rights reserved.
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.
37 #include "core.h"
38 #include "config.h"
39 #include "port.h"
40 #include "name_table.h"
42 /* Connection management: */
43 #define PROBING_INTERVAL 3600000 /* [ms] => 1 h */
44 #define CONFIRMED 0
45 #define PROBING 1
47 #define MAX_REJECT_SIZE 1024
49 static struct sk_buff *msg_queue_head;
50 static struct sk_buff *msg_queue_tail;
52 DEFINE_SPINLOCK(tipc_port_list_lock);
53 static DEFINE_SPINLOCK(queue_lock);
55 static LIST_HEAD(ports);
56 static void port_handle_node_down(unsigned long ref);
57 static struct sk_buff *port_build_self_abort_msg(struct tipc_port *, u32 err);
58 static struct sk_buff *port_build_peer_abort_msg(struct tipc_port *, u32 err);
59 static void port_timeout(unsigned long ref);
62 static u32 port_peernode(struct tipc_port *p_ptr)
64 return msg_destnode(&p_ptr->phdr);
67 static u32 port_peerport(struct tipc_port *p_ptr)
69 return msg_destport(&p_ptr->phdr);
72 /**
73 * tipc_multicast - send a multicast message to local and remote destinations
76 int tipc_multicast(u32 ref, struct tipc_name_seq const *seq,
77 u32 num_sect, struct iovec const *msg_sect)
79 struct tipc_msg *hdr;
80 struct sk_buff *buf;
81 struct sk_buff *ibuf = NULL;
82 struct port_list dports = {0, NULL, };
83 struct tipc_port *oport = tipc_port_deref(ref);
84 int ext_targets;
85 int res;
87 if (unlikely(!oport))
88 return -EINVAL;
90 /* Create multicast message */
92 hdr = &oport->phdr;
93 msg_set_type(hdr, TIPC_MCAST_MSG);
94 msg_set_nametype(hdr, seq->type);
95 msg_set_namelower(hdr, seq->lower);
96 msg_set_nameupper(hdr, seq->upper);
97 msg_set_hdr_sz(hdr, MCAST_H_SIZE);
98 res = tipc_msg_build(hdr, msg_sect, num_sect, MAX_MSG_SIZE,
99 !oport->user_port, &buf);
100 if (unlikely(!buf))
101 return res;
103 /* Figure out where to send multicast message */
105 ext_targets = tipc_nametbl_mc_translate(seq->type, seq->lower, seq->upper,
106 TIPC_NODE_SCOPE, &dports);
108 /* Send message to destinations (duplicate it only if necessary) */
110 if (ext_targets) {
111 if (dports.count != 0) {
112 ibuf = skb_copy(buf, GFP_ATOMIC);
113 if (ibuf == NULL) {
114 tipc_port_list_free(&dports);
115 buf_discard(buf);
116 return -ENOMEM;
119 res = tipc_bclink_send_msg(buf);
120 if ((res < 0) && (dports.count != 0))
121 buf_discard(ibuf);
122 } else {
123 ibuf = buf;
126 if (res >= 0) {
127 if (ibuf)
128 tipc_port_recv_mcast(ibuf, &dports);
129 } else {
130 tipc_port_list_free(&dports);
132 return res;
136 * tipc_port_recv_mcast - deliver multicast message to all destination ports
138 * If there is no port list, perform a lookup to create one
141 void tipc_port_recv_mcast(struct sk_buff *buf, struct port_list *dp)
143 struct tipc_msg *msg;
144 struct port_list dports = {0, NULL, };
145 struct port_list *item = dp;
146 int cnt = 0;
148 msg = buf_msg(buf);
150 /* Create destination port list, if one wasn't supplied */
152 if (dp == NULL) {
153 tipc_nametbl_mc_translate(msg_nametype(msg),
154 msg_namelower(msg),
155 msg_nameupper(msg),
156 TIPC_CLUSTER_SCOPE,
157 &dports);
158 item = dp = &dports;
161 /* Deliver a copy of message to each destination port */
163 if (dp->count != 0) {
164 if (dp->count == 1) {
165 msg_set_destport(msg, dp->ports[0]);
166 tipc_port_recv_msg(buf);
167 tipc_port_list_free(dp);
168 return;
170 for (; cnt < dp->count; cnt++) {
171 int index = cnt % PLSIZE;
172 struct sk_buff *b = skb_clone(buf, GFP_ATOMIC);
174 if (b == NULL) {
175 warn("Unable to deliver multicast message(s)\n");
176 goto exit;
178 if ((index == 0) && (cnt != 0))
179 item = item->next;
180 msg_set_destport(buf_msg(b), item->ports[index]);
181 tipc_port_recv_msg(b);
184 exit:
185 buf_discard(buf);
186 tipc_port_list_free(dp);
190 * tipc_createport_raw - create a generic TIPC port
192 * Returns pointer to (locked) TIPC port, or NULL if unable to create it
195 struct tipc_port *tipc_createport_raw(void *usr_handle,
196 u32 (*dispatcher)(struct tipc_port *, struct sk_buff *),
197 void (*wakeup)(struct tipc_port *),
198 const u32 importance)
200 struct tipc_port *p_ptr;
201 struct tipc_msg *msg;
202 u32 ref;
204 p_ptr = kzalloc(sizeof(*p_ptr), GFP_ATOMIC);
205 if (!p_ptr) {
206 warn("Port creation failed, no memory\n");
207 return NULL;
209 ref = tipc_ref_acquire(p_ptr, &p_ptr->lock);
210 if (!ref) {
211 warn("Port creation failed, reference table exhausted\n");
212 kfree(p_ptr);
213 return NULL;
216 p_ptr->usr_handle = usr_handle;
217 p_ptr->max_pkt = MAX_PKT_DEFAULT;
218 p_ptr->ref = ref;
219 msg = &p_ptr->phdr;
220 tipc_msg_init(msg, importance, TIPC_NAMED_MSG, LONG_H_SIZE, 0);
221 msg_set_origport(msg, ref);
222 INIT_LIST_HEAD(&p_ptr->wait_list);
223 INIT_LIST_HEAD(&p_ptr->subscription.nodesub_list);
224 p_ptr->dispatcher = dispatcher;
225 p_ptr->wakeup = wakeup;
226 p_ptr->user_port = NULL;
227 k_init_timer(&p_ptr->timer, (Handler)port_timeout, ref);
228 spin_lock_bh(&tipc_port_list_lock);
229 INIT_LIST_HEAD(&p_ptr->publications);
230 INIT_LIST_HEAD(&p_ptr->port_list);
231 list_add_tail(&p_ptr->port_list, &ports);
232 spin_unlock_bh(&tipc_port_list_lock);
233 return p_ptr;
236 int tipc_deleteport(u32 ref)
238 struct tipc_port *p_ptr;
239 struct sk_buff *buf = NULL;
241 tipc_withdraw(ref, 0, NULL);
242 p_ptr = tipc_port_lock(ref);
243 if (!p_ptr)
244 return -EINVAL;
246 tipc_ref_discard(ref);
247 tipc_port_unlock(p_ptr);
249 k_cancel_timer(&p_ptr->timer);
250 if (p_ptr->connected) {
251 buf = port_build_peer_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
252 tipc_nodesub_unsubscribe(&p_ptr->subscription);
254 kfree(p_ptr->user_port);
256 spin_lock_bh(&tipc_port_list_lock);
257 list_del(&p_ptr->port_list);
258 list_del(&p_ptr->wait_list);
259 spin_unlock_bh(&tipc_port_list_lock);
260 k_term_timer(&p_ptr->timer);
261 kfree(p_ptr);
262 tipc_net_route_msg(buf);
263 return 0;
266 static int port_unreliable(struct tipc_port *p_ptr)
268 return msg_src_droppable(&p_ptr->phdr);
271 int tipc_portunreliable(u32 ref, unsigned int *isunreliable)
273 struct tipc_port *p_ptr;
275 p_ptr = tipc_port_lock(ref);
276 if (!p_ptr)
277 return -EINVAL;
278 *isunreliable = port_unreliable(p_ptr);
279 tipc_port_unlock(p_ptr);
280 return 0;
283 int tipc_set_portunreliable(u32 ref, unsigned int isunreliable)
285 struct tipc_port *p_ptr;
287 p_ptr = tipc_port_lock(ref);
288 if (!p_ptr)
289 return -EINVAL;
290 msg_set_src_droppable(&p_ptr->phdr, (isunreliable != 0));
291 tipc_port_unlock(p_ptr);
292 return 0;
295 static int port_unreturnable(struct tipc_port *p_ptr)
297 return msg_dest_droppable(&p_ptr->phdr);
300 int tipc_portunreturnable(u32 ref, unsigned int *isunrejectable)
302 struct tipc_port *p_ptr;
304 p_ptr = tipc_port_lock(ref);
305 if (!p_ptr)
306 return -EINVAL;
307 *isunrejectable = port_unreturnable(p_ptr);
308 tipc_port_unlock(p_ptr);
309 return 0;
312 int tipc_set_portunreturnable(u32 ref, unsigned int isunrejectable)
314 struct tipc_port *p_ptr;
316 p_ptr = tipc_port_lock(ref);
317 if (!p_ptr)
318 return -EINVAL;
319 msg_set_dest_droppable(&p_ptr->phdr, (isunrejectable != 0));
320 tipc_port_unlock(p_ptr);
321 return 0;
325 * port_build_proto_msg(): build a port level protocol
326 * or a connection abortion message. Called with
327 * tipc_port lock on.
329 static struct sk_buff *port_build_proto_msg(u32 destport, u32 destnode,
330 u32 origport, u32 orignode,
331 u32 usr, u32 type, u32 err,
332 u32 ack)
334 struct sk_buff *buf;
335 struct tipc_msg *msg;
337 buf = tipc_buf_acquire(LONG_H_SIZE);
338 if (buf) {
339 msg = buf_msg(buf);
340 tipc_msg_init(msg, usr, type, LONG_H_SIZE, destnode);
341 msg_set_errcode(msg, err);
342 msg_set_destport(msg, destport);
343 msg_set_origport(msg, origport);
344 msg_set_orignode(msg, orignode);
345 msg_set_msgcnt(msg, ack);
347 return buf;
350 int tipc_reject_msg(struct sk_buff *buf, u32 err)
352 struct tipc_msg *msg = buf_msg(buf);
353 struct sk_buff *rbuf;
354 struct tipc_msg *rmsg;
355 int hdr_sz;
356 u32 imp = msg_importance(msg);
357 u32 data_sz = msg_data_sz(msg);
359 if (data_sz > MAX_REJECT_SIZE)
360 data_sz = MAX_REJECT_SIZE;
361 if (msg_connected(msg) && (imp < TIPC_CRITICAL_IMPORTANCE))
362 imp++;
364 /* discard rejected message if it shouldn't be returned to sender */
365 if (msg_errcode(msg) || msg_dest_droppable(msg)) {
366 buf_discard(buf);
367 return data_sz;
370 /* construct rejected message */
371 if (msg_mcast(msg))
372 hdr_sz = MCAST_H_SIZE;
373 else
374 hdr_sz = LONG_H_SIZE;
375 rbuf = tipc_buf_acquire(data_sz + hdr_sz);
376 if (rbuf == NULL) {
377 buf_discard(buf);
378 return data_sz;
380 rmsg = buf_msg(rbuf);
381 tipc_msg_init(rmsg, imp, msg_type(msg), hdr_sz, msg_orignode(msg));
382 msg_set_errcode(rmsg, err);
383 msg_set_destport(rmsg, msg_origport(msg));
384 msg_set_origport(rmsg, msg_destport(msg));
385 if (msg_short(msg)) {
386 msg_set_orignode(rmsg, tipc_own_addr);
387 /* leave name type & instance as zeroes */
388 } else {
389 msg_set_orignode(rmsg, msg_destnode(msg));
390 msg_set_nametype(rmsg, msg_nametype(msg));
391 msg_set_nameinst(rmsg, msg_nameinst(msg));
393 msg_set_size(rmsg, data_sz + hdr_sz);
394 skb_copy_to_linear_data_offset(rbuf, hdr_sz, msg_data(msg), data_sz);
396 /* send self-abort message when rejecting on a connected port */
397 if (msg_connected(msg)) {
398 struct sk_buff *abuf = NULL;
399 struct tipc_port *p_ptr = tipc_port_lock(msg_destport(msg));
401 if (p_ptr) {
402 if (p_ptr->connected)
403 abuf = port_build_self_abort_msg(p_ptr, err);
404 tipc_port_unlock(p_ptr);
406 tipc_net_route_msg(abuf);
409 /* send rejected message */
410 buf_discard(buf);
411 tipc_net_route_msg(rbuf);
412 return data_sz;
415 int tipc_port_reject_sections(struct tipc_port *p_ptr, struct tipc_msg *hdr,
416 struct iovec const *msg_sect, u32 num_sect,
417 int err)
419 struct sk_buff *buf;
420 int res;
422 res = tipc_msg_build(hdr, msg_sect, num_sect, MAX_MSG_SIZE,
423 !p_ptr->user_port, &buf);
424 if (!buf)
425 return res;
427 return tipc_reject_msg(buf, err);
430 static void port_timeout(unsigned long ref)
432 struct tipc_port *p_ptr = tipc_port_lock(ref);
433 struct sk_buff *buf = NULL;
435 if (!p_ptr)
436 return;
438 if (!p_ptr->connected) {
439 tipc_port_unlock(p_ptr);
440 return;
443 /* Last probe answered ? */
444 if (p_ptr->probing_state == PROBING) {
445 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
446 } else {
447 buf = port_build_proto_msg(port_peerport(p_ptr),
448 port_peernode(p_ptr),
449 p_ptr->ref,
450 tipc_own_addr,
451 CONN_MANAGER,
452 CONN_PROBE,
453 TIPC_OK,
455 p_ptr->probing_state = PROBING;
456 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
458 tipc_port_unlock(p_ptr);
459 tipc_net_route_msg(buf);
463 static void port_handle_node_down(unsigned long ref)
465 struct tipc_port *p_ptr = tipc_port_lock(ref);
466 struct sk_buff *buf = NULL;
468 if (!p_ptr)
469 return;
470 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_NODE);
471 tipc_port_unlock(p_ptr);
472 tipc_net_route_msg(buf);
476 static struct sk_buff *port_build_self_abort_msg(struct tipc_port *p_ptr, u32 err)
478 u32 imp = msg_importance(&p_ptr->phdr);
480 if (!p_ptr->connected)
481 return NULL;
482 if (imp < TIPC_CRITICAL_IMPORTANCE)
483 imp++;
484 return port_build_proto_msg(p_ptr->ref,
485 tipc_own_addr,
486 port_peerport(p_ptr),
487 port_peernode(p_ptr),
488 imp,
489 TIPC_CONN_MSG,
490 err,
495 static struct sk_buff *port_build_peer_abort_msg(struct tipc_port *p_ptr, u32 err)
497 u32 imp = msg_importance(&p_ptr->phdr);
499 if (!p_ptr->connected)
500 return NULL;
501 if (imp < TIPC_CRITICAL_IMPORTANCE)
502 imp++;
503 return port_build_proto_msg(port_peerport(p_ptr),
504 port_peernode(p_ptr),
505 p_ptr->ref,
506 tipc_own_addr,
507 imp,
508 TIPC_CONN_MSG,
509 err,
513 void tipc_port_recv_proto_msg(struct sk_buff *buf)
515 struct tipc_msg *msg = buf_msg(buf);
516 struct tipc_port *p_ptr = tipc_port_lock(msg_destport(msg));
517 u32 err = TIPC_OK;
518 struct sk_buff *r_buf = NULL;
519 struct sk_buff *abort_buf = NULL;
521 if (!p_ptr) {
522 err = TIPC_ERR_NO_PORT;
523 } else if (p_ptr->connected) {
524 if ((port_peernode(p_ptr) != msg_orignode(msg)) ||
525 (port_peerport(p_ptr) != msg_origport(msg))) {
526 err = TIPC_ERR_NO_PORT;
527 } else if (msg_type(msg) == CONN_ACK) {
528 int wakeup = tipc_port_congested(p_ptr) &&
529 p_ptr->congested &&
530 p_ptr->wakeup;
531 p_ptr->acked += msg_msgcnt(msg);
532 if (tipc_port_congested(p_ptr))
533 goto exit;
534 p_ptr->congested = 0;
535 if (!wakeup)
536 goto exit;
537 p_ptr->wakeup(p_ptr);
538 goto exit;
540 } else if (p_ptr->published) {
541 err = TIPC_ERR_NO_PORT;
543 if (err) {
544 r_buf = port_build_proto_msg(msg_origport(msg),
545 msg_orignode(msg),
546 msg_destport(msg),
547 tipc_own_addr,
548 TIPC_HIGH_IMPORTANCE,
549 TIPC_CONN_MSG,
550 err,
552 goto exit;
555 /* All is fine */
556 if (msg_type(msg) == CONN_PROBE) {
557 r_buf = port_build_proto_msg(msg_origport(msg),
558 msg_orignode(msg),
559 msg_destport(msg),
560 tipc_own_addr,
561 CONN_MANAGER,
562 CONN_PROBE_REPLY,
563 TIPC_OK,
566 p_ptr->probing_state = CONFIRMED;
567 exit:
568 if (p_ptr)
569 tipc_port_unlock(p_ptr);
570 tipc_net_route_msg(r_buf);
571 tipc_net_route_msg(abort_buf);
572 buf_discard(buf);
575 static void port_print(struct tipc_port *p_ptr, struct print_buf *buf, int full_id)
577 struct publication *publ;
579 if (full_id)
580 tipc_printf(buf, "<%u.%u.%u:%u>:",
581 tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
582 tipc_node(tipc_own_addr), p_ptr->ref);
583 else
584 tipc_printf(buf, "%-10u:", p_ptr->ref);
586 if (p_ptr->connected) {
587 u32 dport = port_peerport(p_ptr);
588 u32 destnode = port_peernode(p_ptr);
590 tipc_printf(buf, " connected to <%u.%u.%u:%u>",
591 tipc_zone(destnode), tipc_cluster(destnode),
592 tipc_node(destnode), dport);
593 if (p_ptr->conn_type != 0)
594 tipc_printf(buf, " via {%u,%u}",
595 p_ptr->conn_type,
596 p_ptr->conn_instance);
597 } else if (p_ptr->published) {
598 tipc_printf(buf, " bound to");
599 list_for_each_entry(publ, &p_ptr->publications, pport_list) {
600 if (publ->lower == publ->upper)
601 tipc_printf(buf, " {%u,%u}", publ->type,
602 publ->lower);
603 else
604 tipc_printf(buf, " {%u,%u,%u}", publ->type,
605 publ->lower, publ->upper);
608 tipc_printf(buf, "\n");
611 #define MAX_PORT_QUERY 32768
613 struct sk_buff *tipc_port_get_ports(void)
615 struct sk_buff *buf;
616 struct tlv_desc *rep_tlv;
617 struct print_buf pb;
618 struct tipc_port *p_ptr;
619 int str_len;
621 buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_PORT_QUERY));
622 if (!buf)
623 return NULL;
624 rep_tlv = (struct tlv_desc *)buf->data;
626 tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), MAX_PORT_QUERY);
627 spin_lock_bh(&tipc_port_list_lock);
628 list_for_each_entry(p_ptr, &ports, port_list) {
629 spin_lock_bh(p_ptr->lock);
630 port_print(p_ptr, &pb, 0);
631 spin_unlock_bh(p_ptr->lock);
633 spin_unlock_bh(&tipc_port_list_lock);
634 str_len = tipc_printbuf_validate(&pb);
636 skb_put(buf, TLV_SPACE(str_len));
637 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
639 return buf;
642 void tipc_port_reinit(void)
644 struct tipc_port *p_ptr;
645 struct tipc_msg *msg;
647 spin_lock_bh(&tipc_port_list_lock);
648 list_for_each_entry(p_ptr, &ports, port_list) {
649 msg = &p_ptr->phdr;
650 if (msg_orignode(msg) == tipc_own_addr)
651 break;
652 msg_set_prevnode(msg, tipc_own_addr);
653 msg_set_orignode(msg, tipc_own_addr);
655 spin_unlock_bh(&tipc_port_list_lock);
660 * port_dispatcher_sigh(): Signal handler for messages destinated
661 * to the tipc_port interface.
664 static void port_dispatcher_sigh(void *dummy)
666 struct sk_buff *buf;
668 spin_lock_bh(&queue_lock);
669 buf = msg_queue_head;
670 msg_queue_head = NULL;
671 spin_unlock_bh(&queue_lock);
673 while (buf) {
674 struct tipc_port *p_ptr;
675 struct user_port *up_ptr;
676 struct tipc_portid orig;
677 struct tipc_name_seq dseq;
678 void *usr_handle;
679 int connected;
680 int published;
681 u32 message_type;
683 struct sk_buff *next = buf->next;
684 struct tipc_msg *msg = buf_msg(buf);
685 u32 dref = msg_destport(msg);
687 message_type = msg_type(msg);
688 if (message_type > TIPC_DIRECT_MSG)
689 goto reject; /* Unsupported message type */
691 p_ptr = tipc_port_lock(dref);
692 if (!p_ptr)
693 goto reject; /* Port deleted while msg in queue */
695 orig.ref = msg_origport(msg);
696 orig.node = msg_orignode(msg);
697 up_ptr = p_ptr->user_port;
698 usr_handle = up_ptr->usr_handle;
699 connected = p_ptr->connected;
700 published = p_ptr->published;
702 if (unlikely(msg_errcode(msg)))
703 goto err;
705 switch (message_type) {
707 case TIPC_CONN_MSG:{
708 tipc_conn_msg_event cb = up_ptr->conn_msg_cb;
709 u32 peer_port = port_peerport(p_ptr);
710 u32 peer_node = port_peernode(p_ptr);
711 u32 dsz;
713 tipc_port_unlock(p_ptr);
714 if (unlikely(!cb))
715 goto reject;
716 if (unlikely(!connected)) {
717 if (tipc_connect2port(dref, &orig))
718 goto reject;
719 } else if ((msg_origport(msg) != peer_port) ||
720 (msg_orignode(msg) != peer_node))
721 goto reject;
722 dsz = msg_data_sz(msg);
723 if (unlikely(dsz &&
724 (++p_ptr->conn_unacked >=
725 TIPC_FLOW_CONTROL_WIN)))
726 tipc_acknowledge(dref,
727 p_ptr->conn_unacked);
728 skb_pull(buf, msg_hdr_sz(msg));
729 cb(usr_handle, dref, &buf, msg_data(msg), dsz);
730 break;
732 case TIPC_DIRECT_MSG:{
733 tipc_msg_event cb = up_ptr->msg_cb;
735 tipc_port_unlock(p_ptr);
736 if (unlikely(!cb || connected))
737 goto reject;
738 skb_pull(buf, msg_hdr_sz(msg));
739 cb(usr_handle, dref, &buf, msg_data(msg),
740 msg_data_sz(msg), msg_importance(msg),
741 &orig);
742 break;
744 case TIPC_MCAST_MSG:
745 case TIPC_NAMED_MSG:{
746 tipc_named_msg_event cb = up_ptr->named_msg_cb;
748 tipc_port_unlock(p_ptr);
749 if (unlikely(!cb || connected || !published))
750 goto reject;
751 dseq.type = msg_nametype(msg);
752 dseq.lower = msg_nameinst(msg);
753 dseq.upper = (message_type == TIPC_NAMED_MSG)
754 ? dseq.lower : msg_nameupper(msg);
755 skb_pull(buf, msg_hdr_sz(msg));
756 cb(usr_handle, dref, &buf, msg_data(msg),
757 msg_data_sz(msg), msg_importance(msg),
758 &orig, &dseq);
759 break;
762 if (buf)
763 buf_discard(buf);
764 buf = next;
765 continue;
766 err:
767 switch (message_type) {
769 case TIPC_CONN_MSG:{
770 tipc_conn_shutdown_event cb =
771 up_ptr->conn_err_cb;
772 u32 peer_port = port_peerport(p_ptr);
773 u32 peer_node = port_peernode(p_ptr);
775 tipc_port_unlock(p_ptr);
776 if (!cb || !connected)
777 break;
778 if ((msg_origport(msg) != peer_port) ||
779 (msg_orignode(msg) != peer_node))
780 break;
781 tipc_disconnect(dref);
782 skb_pull(buf, msg_hdr_sz(msg));
783 cb(usr_handle, dref, &buf, msg_data(msg),
784 msg_data_sz(msg), msg_errcode(msg));
785 break;
787 case TIPC_DIRECT_MSG:{
788 tipc_msg_err_event cb = up_ptr->err_cb;
790 tipc_port_unlock(p_ptr);
791 if (!cb || connected)
792 break;
793 skb_pull(buf, msg_hdr_sz(msg));
794 cb(usr_handle, dref, &buf, msg_data(msg),
795 msg_data_sz(msg), msg_errcode(msg), &orig);
796 break;
798 case TIPC_MCAST_MSG:
799 case TIPC_NAMED_MSG:{
800 tipc_named_msg_err_event cb =
801 up_ptr->named_err_cb;
803 tipc_port_unlock(p_ptr);
804 if (!cb || connected)
805 break;
806 dseq.type = msg_nametype(msg);
807 dseq.lower = msg_nameinst(msg);
808 dseq.upper = (message_type == TIPC_NAMED_MSG)
809 ? dseq.lower : msg_nameupper(msg);
810 skb_pull(buf, msg_hdr_sz(msg));
811 cb(usr_handle, dref, &buf, msg_data(msg),
812 msg_data_sz(msg), msg_errcode(msg), &dseq);
813 break;
816 if (buf)
817 buf_discard(buf);
818 buf = next;
819 continue;
820 reject:
821 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
822 buf = next;
827 * port_dispatcher(): Dispatcher for messages destinated
828 * to the tipc_port interface. Called with port locked.
831 static u32 port_dispatcher(struct tipc_port *dummy, struct sk_buff *buf)
833 buf->next = NULL;
834 spin_lock_bh(&queue_lock);
835 if (msg_queue_head) {
836 msg_queue_tail->next = buf;
837 msg_queue_tail = buf;
838 } else {
839 msg_queue_tail = msg_queue_head = buf;
840 tipc_k_signal((Handler)port_dispatcher_sigh, 0);
842 spin_unlock_bh(&queue_lock);
843 return 0;
847 * Wake up port after congestion: Called with port locked,
851 static void port_wakeup_sh(unsigned long ref)
853 struct tipc_port *p_ptr;
854 struct user_port *up_ptr;
855 tipc_continue_event cb = NULL;
856 void *uh = NULL;
858 p_ptr = tipc_port_lock(ref);
859 if (p_ptr) {
860 up_ptr = p_ptr->user_port;
861 if (up_ptr) {
862 cb = up_ptr->continue_event_cb;
863 uh = up_ptr->usr_handle;
865 tipc_port_unlock(p_ptr);
867 if (cb)
868 cb(uh, ref);
872 static void port_wakeup(struct tipc_port *p_ptr)
874 tipc_k_signal((Handler)port_wakeup_sh, p_ptr->ref);
877 void tipc_acknowledge(u32 ref, u32 ack)
879 struct tipc_port *p_ptr;
880 struct sk_buff *buf = NULL;
882 p_ptr = tipc_port_lock(ref);
883 if (!p_ptr)
884 return;
885 if (p_ptr->connected) {
886 p_ptr->conn_unacked -= ack;
887 buf = port_build_proto_msg(port_peerport(p_ptr),
888 port_peernode(p_ptr),
889 ref,
890 tipc_own_addr,
891 CONN_MANAGER,
892 CONN_ACK,
893 TIPC_OK,
894 ack);
896 tipc_port_unlock(p_ptr);
897 tipc_net_route_msg(buf);
901 * tipc_createport(): user level call.
904 int tipc_createport(void *usr_handle,
905 unsigned int importance,
906 tipc_msg_err_event error_cb,
907 tipc_named_msg_err_event named_error_cb,
908 tipc_conn_shutdown_event conn_error_cb,
909 tipc_msg_event msg_cb,
910 tipc_named_msg_event named_msg_cb,
911 tipc_conn_msg_event conn_msg_cb,
912 tipc_continue_event continue_event_cb,/* May be zero */
913 u32 *portref)
915 struct user_port *up_ptr;
916 struct tipc_port *p_ptr;
918 up_ptr = kmalloc(sizeof(*up_ptr), GFP_ATOMIC);
919 if (!up_ptr) {
920 warn("Port creation failed, no memory\n");
921 return -ENOMEM;
923 p_ptr = (struct tipc_port *)tipc_createport_raw(NULL, port_dispatcher,
924 port_wakeup, importance);
925 if (!p_ptr) {
926 kfree(up_ptr);
927 return -ENOMEM;
930 p_ptr->user_port = up_ptr;
931 up_ptr->usr_handle = usr_handle;
932 up_ptr->ref = p_ptr->ref;
933 up_ptr->err_cb = error_cb;
934 up_ptr->named_err_cb = named_error_cb;
935 up_ptr->conn_err_cb = conn_error_cb;
936 up_ptr->msg_cb = msg_cb;
937 up_ptr->named_msg_cb = named_msg_cb;
938 up_ptr->conn_msg_cb = conn_msg_cb;
939 up_ptr->continue_event_cb = continue_event_cb;
940 *portref = p_ptr->ref;
941 tipc_port_unlock(p_ptr);
942 return 0;
945 int tipc_portimportance(u32 ref, unsigned int *importance)
947 struct tipc_port *p_ptr;
949 p_ptr = tipc_port_lock(ref);
950 if (!p_ptr)
951 return -EINVAL;
952 *importance = (unsigned int)msg_importance(&p_ptr->phdr);
953 tipc_port_unlock(p_ptr);
954 return 0;
957 int tipc_set_portimportance(u32 ref, unsigned int imp)
959 struct tipc_port *p_ptr;
961 if (imp > TIPC_CRITICAL_IMPORTANCE)
962 return -EINVAL;
964 p_ptr = tipc_port_lock(ref);
965 if (!p_ptr)
966 return -EINVAL;
967 msg_set_importance(&p_ptr->phdr, (u32)imp);
968 tipc_port_unlock(p_ptr);
969 return 0;
973 int tipc_publish(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
975 struct tipc_port *p_ptr;
976 struct publication *publ;
977 u32 key;
978 int res = -EINVAL;
980 p_ptr = tipc_port_lock(ref);
981 if (!p_ptr)
982 return -EINVAL;
984 if (p_ptr->connected)
985 goto exit;
986 if (seq->lower > seq->upper)
987 goto exit;
988 if ((scope < TIPC_ZONE_SCOPE) || (scope > TIPC_NODE_SCOPE))
989 goto exit;
990 key = ref + p_ptr->pub_count + 1;
991 if (key == ref) {
992 res = -EADDRINUSE;
993 goto exit;
995 publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
996 scope, p_ptr->ref, key);
997 if (publ) {
998 list_add(&publ->pport_list, &p_ptr->publications);
999 p_ptr->pub_count++;
1000 p_ptr->published = 1;
1001 res = 0;
1003 exit:
1004 tipc_port_unlock(p_ptr);
1005 return res;
1008 int tipc_withdraw(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
1010 struct tipc_port *p_ptr;
1011 struct publication *publ;
1012 struct publication *tpubl;
1013 int res = -EINVAL;
1015 p_ptr = tipc_port_lock(ref);
1016 if (!p_ptr)
1017 return -EINVAL;
1018 if (!seq) {
1019 list_for_each_entry_safe(publ, tpubl,
1020 &p_ptr->publications, pport_list) {
1021 tipc_nametbl_withdraw(publ->type, publ->lower,
1022 publ->ref, publ->key);
1024 res = 0;
1025 } else {
1026 list_for_each_entry_safe(publ, tpubl,
1027 &p_ptr->publications, pport_list) {
1028 if (publ->scope != scope)
1029 continue;
1030 if (publ->type != seq->type)
1031 continue;
1032 if (publ->lower != seq->lower)
1033 continue;
1034 if (publ->upper != seq->upper)
1035 break;
1036 tipc_nametbl_withdraw(publ->type, publ->lower,
1037 publ->ref, publ->key);
1038 res = 0;
1039 break;
1042 if (list_empty(&p_ptr->publications))
1043 p_ptr->published = 0;
1044 tipc_port_unlock(p_ptr);
1045 return res;
1048 int tipc_connect2port(u32 ref, struct tipc_portid const *peer)
1050 struct tipc_port *p_ptr;
1051 struct tipc_msg *msg;
1052 int res = -EINVAL;
1054 p_ptr = tipc_port_lock(ref);
1055 if (!p_ptr)
1056 return -EINVAL;
1057 if (p_ptr->published || p_ptr->connected)
1058 goto exit;
1059 if (!peer->ref)
1060 goto exit;
1062 msg = &p_ptr->phdr;
1063 msg_set_destnode(msg, peer->node);
1064 msg_set_destport(msg, peer->ref);
1065 msg_set_orignode(msg, tipc_own_addr);
1066 msg_set_origport(msg, p_ptr->ref);
1067 msg_set_type(msg, TIPC_CONN_MSG);
1068 msg_set_hdr_sz(msg, SHORT_H_SIZE);
1070 p_ptr->probing_interval = PROBING_INTERVAL;
1071 p_ptr->probing_state = CONFIRMED;
1072 p_ptr->connected = 1;
1073 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
1075 tipc_nodesub_subscribe(&p_ptr->subscription, peer->node,
1076 (void *)(unsigned long)ref,
1077 (net_ev_handler)port_handle_node_down);
1078 res = 0;
1079 exit:
1080 tipc_port_unlock(p_ptr);
1081 p_ptr->max_pkt = tipc_link_get_max_pkt(peer->node, ref);
1082 return res;
1086 * tipc_disconnect_port - disconnect port from peer
1088 * Port must be locked.
1091 int tipc_disconnect_port(struct tipc_port *tp_ptr)
1093 int res;
1095 if (tp_ptr->connected) {
1096 tp_ptr->connected = 0;
1097 /* let timer expire on it's own to avoid deadlock! */
1098 tipc_nodesub_unsubscribe(
1099 &((struct tipc_port *)tp_ptr)->subscription);
1100 res = 0;
1101 } else {
1102 res = -ENOTCONN;
1104 return res;
1108 * tipc_disconnect(): Disconnect port form peer.
1109 * This is a node local operation.
1112 int tipc_disconnect(u32 ref)
1114 struct tipc_port *p_ptr;
1115 int res;
1117 p_ptr = tipc_port_lock(ref);
1118 if (!p_ptr)
1119 return -EINVAL;
1120 res = tipc_disconnect_port((struct tipc_port *)p_ptr);
1121 tipc_port_unlock(p_ptr);
1122 return res;
1126 * tipc_shutdown(): Send a SHUTDOWN msg to peer and disconnect
1128 int tipc_shutdown(u32 ref)
1130 struct tipc_port *p_ptr;
1131 struct sk_buff *buf = NULL;
1133 p_ptr = tipc_port_lock(ref);
1134 if (!p_ptr)
1135 return -EINVAL;
1137 if (p_ptr->connected) {
1138 u32 imp = msg_importance(&p_ptr->phdr);
1139 if (imp < TIPC_CRITICAL_IMPORTANCE)
1140 imp++;
1141 buf = port_build_proto_msg(port_peerport(p_ptr),
1142 port_peernode(p_ptr),
1143 ref,
1144 tipc_own_addr,
1145 imp,
1146 TIPC_CONN_MSG,
1147 TIPC_CONN_SHUTDOWN,
1150 tipc_port_unlock(p_ptr);
1151 tipc_net_route_msg(buf);
1152 return tipc_disconnect(ref);
1156 * tipc_port_recv_sections(): Concatenate and deliver sectioned
1157 * message for this node.
1160 static int tipc_port_recv_sections(struct tipc_port *sender, unsigned int num_sect,
1161 struct iovec const *msg_sect)
1163 struct sk_buff *buf;
1164 int res;
1166 res = tipc_msg_build(&sender->phdr, msg_sect, num_sect,
1167 MAX_MSG_SIZE, !sender->user_port, &buf);
1168 if (likely(buf))
1169 tipc_port_recv_msg(buf);
1170 return res;
1174 * tipc_send - send message sections on connection
1177 int tipc_send(u32 ref, unsigned int num_sect, struct iovec const *msg_sect)
1179 struct tipc_port *p_ptr;
1180 u32 destnode;
1181 int res;
1183 p_ptr = tipc_port_deref(ref);
1184 if (!p_ptr || !p_ptr->connected)
1185 return -EINVAL;
1187 p_ptr->congested = 1;
1188 if (!tipc_port_congested(p_ptr)) {
1189 destnode = port_peernode(p_ptr);
1190 if (likely(destnode != tipc_own_addr))
1191 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
1192 destnode);
1193 else
1194 res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
1196 if (likely(res != -ELINKCONG)) {
1197 p_ptr->congested = 0;
1198 if (res > 0)
1199 p_ptr->sent++;
1200 return res;
1203 if (port_unreliable(p_ptr)) {
1204 p_ptr->congested = 0;
1205 /* Just calculate msg length and return */
1206 return tipc_msg_calc_data_size(msg_sect, num_sect);
1208 return -ELINKCONG;
1212 * tipc_send2name - send message sections to port name
1215 int tipc_send2name(u32 ref, struct tipc_name const *name, unsigned int domain,
1216 unsigned int num_sect, struct iovec const *msg_sect)
1218 struct tipc_port *p_ptr;
1219 struct tipc_msg *msg;
1220 u32 destnode = domain;
1221 u32 destport;
1222 int res;
1224 p_ptr = tipc_port_deref(ref);
1225 if (!p_ptr || p_ptr->connected)
1226 return -EINVAL;
1228 msg = &p_ptr->phdr;
1229 msg_set_type(msg, TIPC_NAMED_MSG);
1230 msg_set_orignode(msg, tipc_own_addr);
1231 msg_set_origport(msg, ref);
1232 msg_set_hdr_sz(msg, LONG_H_SIZE);
1233 msg_set_nametype(msg, name->type);
1234 msg_set_nameinst(msg, name->instance);
1235 msg_set_lookup_scope(msg, tipc_addr_scope(domain));
1236 destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
1237 msg_set_destnode(msg, destnode);
1238 msg_set_destport(msg, destport);
1240 if (likely(destport)) {
1241 if (likely(destnode == tipc_own_addr))
1242 res = tipc_port_recv_sections(p_ptr, num_sect,
1243 msg_sect);
1244 else
1245 res = tipc_link_send_sections_fast(p_ptr, msg_sect,
1246 num_sect, destnode);
1247 if (likely(res != -ELINKCONG)) {
1248 if (res > 0)
1249 p_ptr->sent++;
1250 return res;
1252 if (port_unreliable(p_ptr)) {
1253 /* Just calculate msg length and return */
1254 return tipc_msg_calc_data_size(msg_sect, num_sect);
1256 return -ELINKCONG;
1258 return tipc_port_reject_sections(p_ptr, msg, msg_sect, num_sect,
1259 TIPC_ERR_NO_NAME);
1263 * tipc_send2port - send message sections to port identity
1266 int tipc_send2port(u32 ref, struct tipc_portid const *dest,
1267 unsigned int num_sect, struct iovec const *msg_sect)
1269 struct tipc_port *p_ptr;
1270 struct tipc_msg *msg;
1271 int res;
1273 p_ptr = tipc_port_deref(ref);
1274 if (!p_ptr || p_ptr->connected)
1275 return -EINVAL;
1277 msg = &p_ptr->phdr;
1278 msg_set_type(msg, TIPC_DIRECT_MSG);
1279 msg_set_orignode(msg, tipc_own_addr);
1280 msg_set_origport(msg, ref);
1281 msg_set_destnode(msg, dest->node);
1282 msg_set_destport(msg, dest->ref);
1283 msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
1285 if (dest->node == tipc_own_addr)
1286 res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
1287 else
1288 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
1289 dest->node);
1290 if (likely(res != -ELINKCONG)) {
1291 if (res > 0)
1292 p_ptr->sent++;
1293 return res;
1295 if (port_unreliable(p_ptr)) {
1296 /* Just calculate msg length and return */
1297 return tipc_msg_calc_data_size(msg_sect, num_sect);
1299 return -ELINKCONG;
1303 * tipc_send_buf2port - send message buffer to port identity
1306 int tipc_send_buf2port(u32 ref, struct tipc_portid const *dest,
1307 struct sk_buff *buf, unsigned int dsz)
1309 struct tipc_port *p_ptr;
1310 struct tipc_msg *msg;
1311 int res;
1313 p_ptr = (struct tipc_port *)tipc_ref_deref(ref);
1314 if (!p_ptr || p_ptr->connected)
1315 return -EINVAL;
1317 msg = &p_ptr->phdr;
1318 msg_set_type(msg, TIPC_DIRECT_MSG);
1319 msg_set_orignode(msg, tipc_own_addr);
1320 msg_set_origport(msg, ref);
1321 msg_set_destnode(msg, dest->node);
1322 msg_set_destport(msg, dest->ref);
1323 msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
1324 msg_set_size(msg, DIR_MSG_H_SIZE + dsz);
1325 if (skb_cow(buf, DIR_MSG_H_SIZE))
1326 return -ENOMEM;
1328 skb_push(buf, DIR_MSG_H_SIZE);
1329 skb_copy_to_linear_data(buf, msg, DIR_MSG_H_SIZE);
1331 if (dest->node == tipc_own_addr)
1332 res = tipc_port_recv_msg(buf);
1333 else
1334 res = tipc_send_buf_fast(buf, dest->node);
1335 if (likely(res != -ELINKCONG)) {
1336 if (res > 0)
1337 p_ptr->sent++;
1338 return res;
1340 if (port_unreliable(p_ptr))
1341 return dsz;
1342 return -ELINKCONG;