2 * net/tipc/port.c: TIPC port code
4 * Copyright (c) 1992-2007, 2014, Ericsson AB
5 * Copyright (c) 2004-2008, 2010-2013, Wind River Systems
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the names of the copyright holders nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
20 * Alternatively, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") version 2 as published by the Free
22 * Software Foundation.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
40 #include "name_table.h"
43 /* Connection management: */
44 #define PROBING_INTERVAL 3600000 /* [ms] => 1 h */
46 #define MAX_REJECT_SIZE 1024
48 DEFINE_SPINLOCK(tipc_port_list_lock);
50 static LIST_HEAD(ports);
51 static void port_handle_node_down(unsigned long ref);
52 static struct sk_buff *port_build_self_abort_msg(struct tipc_port *, u32 err);
53 static struct sk_buff *port_build_peer_abort_msg(struct tipc_port *, u32 err);
54 static void port_timeout(unsigned long ref);
57 * tipc_port_peer_msg - verify message was sent by connected port's peer
59 * Handles cases where the node's network address has changed from
60 * the default of <0.0.0> to its configured setting.
62 int tipc_port_peer_msg(struct tipc_port *p_ptr, struct tipc_msg *msg)
67 if (msg_origport(msg) != tipc_port_peerport(p_ptr))
70 orignode = msg_orignode(msg);
71 peernode = tipc_port_peernode(p_ptr);
72 return (orignode == peernode) ||
73 (!orignode && (peernode == tipc_own_addr)) ||
74 (!peernode && (orignode == tipc_own_addr));
78 * tipc_port_mcast_xmit - send a multicast message to local and remote
81 int tipc_port_mcast_xmit(struct tipc_port *oport,
82 struct tipc_name_seq const *seq,
83 struct iovec const *msg_sect,
88 struct sk_buff *ibuf = NULL;
89 struct tipc_port_list dports = {0, NULL, };
93 /* Create multicast message */
95 msg_set_type(hdr, TIPC_MCAST_MSG);
96 msg_set_lookup_scope(hdr, TIPC_CLUSTER_SCOPE);
97 msg_set_destport(hdr, 0);
98 msg_set_destnode(hdr, 0);
99 msg_set_nametype(hdr, seq->type);
100 msg_set_namelower(hdr, seq->lower);
101 msg_set_nameupper(hdr, seq->upper);
102 msg_set_hdr_sz(hdr, MCAST_H_SIZE);
103 res = tipc_msg_build(hdr, msg_sect, len, MAX_MSG_SIZE, &buf);
107 /* Figure out where to send multicast message */
108 ext_targets = tipc_nametbl_mc_translate(seq->type, seq->lower, seq->upper,
109 TIPC_NODE_SCOPE, &dports);
111 /* Send message to destinations (duplicate it only if necessary) */
113 if (dports.count != 0) {
114 ibuf = skb_copy(buf, GFP_ATOMIC);
116 tipc_port_list_free(&dports);
121 res = tipc_bclink_xmit(buf);
122 if ((res < 0) && (dports.count != 0))
130 tipc_port_mcast_rcv(ibuf, &dports);
132 tipc_port_list_free(&dports);
138 * tipc_port_mcast_rcv - deliver multicast message to all destination ports
140 * If there is no port list, perform a lookup to create one
142 void tipc_port_mcast_rcv(struct sk_buff *buf, struct tipc_port_list *dp)
144 struct tipc_msg *msg;
145 struct tipc_port_list dports = {0, NULL, };
146 struct tipc_port_list *item = dp;
151 /* Create destination port list, if one wasn't supplied */
153 tipc_nametbl_mc_translate(msg_nametype(msg),
161 /* Deliver a copy of message to each destination port */
162 if (dp->count != 0) {
163 msg_set_destnode(msg, tipc_own_addr);
164 if (dp->count == 1) {
165 msg_set_destport(msg, dp->ports[0]);
167 tipc_port_list_free(dp);
170 for (; cnt < dp->count; cnt++) {
171 int index = cnt % PLSIZE;
172 struct sk_buff *b = skb_clone(buf, GFP_ATOMIC);
175 pr_warn("Unable to deliver multicast message(s)\n");
178 if ((index == 0) && (cnt != 0))
180 msg_set_destport(buf_msg(b), item->ports[index]);
186 tipc_port_list_free(dp);
189 /* tipc_port_init - intiate TIPC port and lock it
191 * Returns obtained reference if initialization is successful, zero otherwise
193 u32 tipc_port_init(struct tipc_port *p_ptr,
194 const unsigned int importance)
196 struct tipc_msg *msg;
199 ref = tipc_ref_acquire(p_ptr, &p_ptr->lock);
201 pr_warn("Port registration failed, ref. table exhausted\n");
205 p_ptr->max_pkt = MAX_PKT_DEFAULT;
207 INIT_LIST_HEAD(&p_ptr->wait_list);
208 INIT_LIST_HEAD(&p_ptr->subscription.nodesub_list);
209 k_init_timer(&p_ptr->timer, (Handler)port_timeout, ref);
210 INIT_LIST_HEAD(&p_ptr->publications);
211 INIT_LIST_HEAD(&p_ptr->port_list);
214 * Must hold port list lock while initializing message header template
215 * to ensure a change to node's own network address doesn't result
216 * in template containing out-dated network address information
218 spin_lock_bh(&tipc_port_list_lock);
220 tipc_msg_init(msg, importance, TIPC_NAMED_MSG, NAMED_H_SIZE, 0);
221 msg_set_origport(msg, ref);
222 list_add_tail(&p_ptr->port_list, &ports);
223 spin_unlock_bh(&tipc_port_list_lock);
227 void tipc_port_destroy(struct tipc_port *p_ptr)
229 struct sk_buff *buf = NULL;
230 struct tipc_msg *msg = NULL;
233 tipc_withdraw(p_ptr, 0, NULL);
235 spin_lock_bh(p_ptr->lock);
236 tipc_ref_discard(p_ptr->ref);
237 spin_unlock_bh(p_ptr->lock);
239 k_cancel_timer(&p_ptr->timer);
240 if (p_ptr->connected) {
241 buf = port_build_peer_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
242 tipc_nodesub_unsubscribe(&p_ptr->subscription);
244 peer = msg_destnode(msg);
245 tipc_link_xmit2(buf, peer, msg_link_selector(msg));
247 spin_lock_bh(&tipc_port_list_lock);
248 list_del(&p_ptr->port_list);
249 list_del(&p_ptr->wait_list);
250 spin_unlock_bh(&tipc_port_list_lock);
251 k_term_timer(&p_ptr->timer);
255 * port_build_proto_msg(): create connection protocol message for port
257 * On entry the port must be locked and connected.
259 static struct sk_buff *port_build_proto_msg(struct tipc_port *p_ptr,
263 struct tipc_msg *msg;
265 buf = tipc_buf_acquire(INT_H_SIZE);
268 tipc_msg_init(msg, CONN_MANAGER, type, INT_H_SIZE,
269 tipc_port_peernode(p_ptr));
270 msg_set_destport(msg, tipc_port_peerport(p_ptr));
271 msg_set_origport(msg, p_ptr->ref);
272 msg_set_msgcnt(msg, ack);
278 static void port_timeout(unsigned long ref)
280 struct tipc_port *p_ptr = tipc_port_lock(ref);
281 struct sk_buff *buf = NULL;
282 struct tipc_msg *msg = NULL;
287 if (!p_ptr->connected) {
288 tipc_port_unlock(p_ptr);
292 /* Last probe answered ? */
293 if (p_ptr->probing_state == TIPC_CONN_PROBING) {
294 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
296 buf = port_build_proto_msg(p_ptr, CONN_PROBE, 0);
297 p_ptr->probing_state = TIPC_CONN_PROBING;
298 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
300 tipc_port_unlock(p_ptr);
302 tipc_link_xmit2(buf, msg_destnode(msg), msg_link_selector(msg));
306 static void port_handle_node_down(unsigned long ref)
308 struct tipc_port *p_ptr = tipc_port_lock(ref);
309 struct sk_buff *buf = NULL;
310 struct tipc_msg *msg = NULL;
314 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_NODE);
315 tipc_port_unlock(p_ptr);
317 tipc_link_xmit2(buf, msg_destnode(msg), msg_link_selector(msg));
321 static struct sk_buff *port_build_self_abort_msg(struct tipc_port *p_ptr, u32 err)
323 struct sk_buff *buf = port_build_peer_abort_msg(p_ptr, err);
326 struct tipc_msg *msg = buf_msg(buf);
327 msg_swap_words(msg, 4, 5);
328 msg_swap_words(msg, 6, 7);
335 static struct sk_buff *port_build_peer_abort_msg(struct tipc_port *p_ptr, u32 err)
338 struct tipc_msg *msg;
341 if (!p_ptr->connected)
344 buf = tipc_buf_acquire(BASIC_H_SIZE);
347 memcpy(msg, &p_ptr->phdr, BASIC_H_SIZE);
348 msg_set_hdr_sz(msg, BASIC_H_SIZE);
349 msg_set_size(msg, BASIC_H_SIZE);
350 imp = msg_importance(msg);
351 if (imp < TIPC_CRITICAL_IMPORTANCE)
352 msg_set_importance(msg, ++imp);
353 msg_set_errcode(msg, err);
359 static int port_print(struct tipc_port *p_ptr, char *buf, int len, int full_id)
361 struct publication *publ;
365 ret = tipc_snprintf(buf, len, "<%u.%u.%u:%u>:",
366 tipc_zone(tipc_own_addr),
367 tipc_cluster(tipc_own_addr),
368 tipc_node(tipc_own_addr), p_ptr->ref);
370 ret = tipc_snprintf(buf, len, "%-10u:", p_ptr->ref);
372 if (p_ptr->connected) {
373 u32 dport = tipc_port_peerport(p_ptr);
374 u32 destnode = tipc_port_peernode(p_ptr);
376 ret += tipc_snprintf(buf + ret, len - ret,
377 " connected to <%u.%u.%u:%u>",
379 tipc_cluster(destnode),
380 tipc_node(destnode), dport);
381 if (p_ptr->conn_type != 0)
382 ret += tipc_snprintf(buf + ret, len - ret,
383 " via {%u,%u}", p_ptr->conn_type,
384 p_ptr->conn_instance);
385 } else if (p_ptr->published) {
386 ret += tipc_snprintf(buf + ret, len - ret, " bound to");
387 list_for_each_entry(publ, &p_ptr->publications, pport_list) {
388 if (publ->lower == publ->upper)
389 ret += tipc_snprintf(buf + ret, len - ret,
390 " {%u,%u}", publ->type,
393 ret += tipc_snprintf(buf + ret, len - ret,
394 " {%u,%u,%u}", publ->type,
395 publ->lower, publ->upper);
398 ret += tipc_snprintf(buf + ret, len - ret, "\n");
402 struct sk_buff *tipc_port_get_ports(void)
405 struct tlv_desc *rep_tlv;
408 struct tipc_port *p_ptr;
411 buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
414 rep_tlv = (struct tlv_desc *)buf->data;
415 pb = TLV_DATA(rep_tlv);
416 pb_len = ULTRA_STRING_MAX_LEN;
418 spin_lock_bh(&tipc_port_list_lock);
419 list_for_each_entry(p_ptr, &ports, port_list) {
420 spin_lock_bh(p_ptr->lock);
421 str_len += port_print(p_ptr, pb, pb_len, 0);
422 spin_unlock_bh(p_ptr->lock);
424 spin_unlock_bh(&tipc_port_list_lock);
425 str_len += 1; /* for "\0" */
426 skb_put(buf, TLV_SPACE(str_len));
427 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
432 void tipc_port_reinit(void)
434 struct tipc_port *p_ptr;
435 struct tipc_msg *msg;
437 spin_lock_bh(&tipc_port_list_lock);
438 list_for_each_entry(p_ptr, &ports, port_list) {
440 msg_set_prevnode(msg, tipc_own_addr);
441 msg_set_orignode(msg, tipc_own_addr);
443 spin_unlock_bh(&tipc_port_list_lock);
446 void tipc_acknowledge(u32 ref, u32 ack)
448 struct tipc_port *p_ptr;
449 struct sk_buff *buf = NULL;
450 struct tipc_msg *msg;
452 p_ptr = tipc_port_lock(ref);
455 if (p_ptr->connected)
456 buf = port_build_proto_msg(p_ptr, CONN_ACK, ack);
458 tipc_port_unlock(p_ptr);
462 tipc_link_xmit2(buf, msg_destnode(msg), msg_link_selector(msg));
465 int tipc_publish(struct tipc_port *p_ptr, unsigned int scope,
466 struct tipc_name_seq const *seq)
468 struct publication *publ;
471 if (p_ptr->connected)
473 key = p_ptr->ref + p_ptr->pub_count + 1;
474 if (key == p_ptr->ref)
477 publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
478 scope, p_ptr->ref, key);
480 list_add(&publ->pport_list, &p_ptr->publications);
482 p_ptr->published = 1;
488 int tipc_withdraw(struct tipc_port *p_ptr, unsigned int scope,
489 struct tipc_name_seq const *seq)
491 struct publication *publ;
492 struct publication *tpubl;
496 list_for_each_entry_safe(publ, tpubl,
497 &p_ptr->publications, pport_list) {
498 tipc_nametbl_withdraw(publ->type, publ->lower,
499 publ->ref, publ->key);
503 list_for_each_entry_safe(publ, tpubl,
504 &p_ptr->publications, pport_list) {
505 if (publ->scope != scope)
507 if (publ->type != seq->type)
509 if (publ->lower != seq->lower)
511 if (publ->upper != seq->upper)
513 tipc_nametbl_withdraw(publ->type, publ->lower,
514 publ->ref, publ->key);
519 if (list_empty(&p_ptr->publications))
520 p_ptr->published = 0;
524 int tipc_port_connect(u32 ref, struct tipc_portid const *peer)
526 struct tipc_port *p_ptr;
529 p_ptr = tipc_port_lock(ref);
532 res = __tipc_port_connect(ref, p_ptr, peer);
533 tipc_port_unlock(p_ptr);
538 * __tipc_port_connect - connect to a remote peer
540 * Port must be locked.
542 int __tipc_port_connect(u32 ref, struct tipc_port *p_ptr,
543 struct tipc_portid const *peer)
545 struct tipc_msg *msg;
548 if (p_ptr->published || p_ptr->connected)
554 msg_set_destnode(msg, peer->node);
555 msg_set_destport(msg, peer->ref);
556 msg_set_type(msg, TIPC_CONN_MSG);
557 msg_set_lookup_scope(msg, 0);
558 msg_set_hdr_sz(msg, SHORT_H_SIZE);
560 p_ptr->probing_interval = PROBING_INTERVAL;
561 p_ptr->probing_state = TIPC_CONN_OK;
562 p_ptr->connected = 1;
563 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
565 tipc_nodesub_subscribe(&p_ptr->subscription, peer->node,
566 (void *)(unsigned long)ref,
567 (net_ev_handler)port_handle_node_down);
570 p_ptr->max_pkt = tipc_node_get_mtu(peer->node, ref);
575 * __tipc_disconnect - disconnect port from peer
577 * Port must be locked.
579 int __tipc_port_disconnect(struct tipc_port *tp_ptr)
581 if (tp_ptr->connected) {
582 tp_ptr->connected = 0;
583 /* let timer expire on it's own to avoid deadlock! */
584 tipc_nodesub_unsubscribe(&tp_ptr->subscription);
592 * tipc_port_disconnect(): Disconnect port form peer.
593 * This is a node local operation.
595 int tipc_port_disconnect(u32 ref)
597 struct tipc_port *p_ptr;
600 p_ptr = tipc_port_lock(ref);
603 res = __tipc_port_disconnect(p_ptr);
604 tipc_port_unlock(p_ptr);
609 * tipc_port_shutdown(): Send a SHUTDOWN msg to peer and disconnect
611 int tipc_port_shutdown(u32 ref)
613 struct tipc_msg *msg;
614 struct tipc_port *p_ptr;
615 struct sk_buff *buf = NULL;
617 p_ptr = tipc_port_lock(ref);
621 buf = port_build_peer_abort_msg(p_ptr, TIPC_CONN_SHUTDOWN);
622 tipc_port_unlock(p_ptr);
624 tipc_link_xmit2(buf, msg_destnode(msg), msg_link_selector(msg));
625 return tipc_port_disconnect(ref);